Fixed build problems caused by merge
[dyninst.git] / instructionAPI / src / InstructionDecoder-x86.C
1 /*
2  * See the dyninst/COPYRIGHT file for copyright information.
3  * 
4  * We provide the Paradyn Tools (below described as "Paradyn")
5  * on an AS IS basis, and do not warrant its validity or performance.
6  * We reserve the right to update, modify, or discontinue this
7  * software at any time.  We shall have no obligation to supply such
8  * updates or modifications or any other form of support to you.
9  * 
10  * By your use of Paradyn, you understand and agree that we (or any
11  * other person or entity with proprietary rights in Paradyn) are
12  * under no obligation to provide either maintenance services,
13  * update services, notices of latent defects, or correction of
14  * defects for Paradyn.
15  * 
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  * 
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  * 
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29  */
30
31 #define INSIDE_INSTRUCTION_API
32
33 #include "common/src/Types.h"
34 #include "InstructionDecoder-x86.h"
35 #include "Expression.h"
36 #include "common/src/arch-x86.h"
37 #include "Register.h"
38 #include "Dereference.h"
39 #include "Immediate.h" 
40 #include "BinaryFunction.h"
41 #include "common/src/singleton_object_pool.h"
42
43 // #define VEX_DEBUG
44
45 using namespace std;
46 using namespace NS_x86;
47 namespace Dyninst
48 {
49     namespace InstructionAPI
50     {
51     
52         bool readsOperand(unsigned int opsema, unsigned int i)
53         {
54             switch(opsema) {
55                 case s1R2R:
56                     return (i == 0 || i == 1);
57                 case s1R:
58                 case s1RW:
59                     return i == 0;
60                 case s1W:
61                     return false;
62                 case s1W2RW:
63                 case s1W2R:   // second operand read, first operand written (e.g. mov)
64                     return i == 1;
65                 case s1RW2R:  // two operands read, first written (e.g. add)
66                 case s1RW2RW: // e.g. xchg
67                 case s1R2RW:
68                     return i == 0 || i == 1;
69                 case s1W2R3R: // e.g. imul
70                 case s1W2RW3R: // some mul
71                 case s1W2R3RW: // (stack) push & pop
72                     return i == 1 || i == 2;
73                 case s1W2W3R: // e.g. les
74                     return i == 2;
75                 case s1RW2R3RW:
76                 case s1RW2R3R: // shld/shrd
77                 case s1RW2RW3R: // [i]div, cmpxch8b
78                 case s1R2R3R:
79                     return i == 0 || i == 1 || i == 2;
80                 case s1W2R3R4R:
81                     return i == 1 || i == 2 || i == 3;
82                 case s1RW2R3R4R:
83                     return i == 0 || i == 1 || i == 2 || i == 3;
84                 case sNONE:
85                 default:
86                     return false;
87             }
88       
89         }
90       
91         bool writesOperand(unsigned int opsema, unsigned int i)
92         {
93             switch(opsema) {
94                 case s1R2R:
95                 case s1R:
96                     return false;
97                 case s1RW:
98                 case s1W:
99                 case s1W2R:   // second operand read, first operand written (e.g. mov)
100                 case s1RW2R:  // two operands read, first written (e.g. add)
101                 case s1W2R3R: // e.g. imul
102                 case s1RW2R3R: // shld/shrd
103                 case s1RW2R3R4R:
104                   return i == 0;
105                 case s1R2RW:
106                   return i == 1;
107                 case s1W2RW:
108                 case s1RW2RW: // e.g. xchg
109                 case s1W2RW3R: // some mul
110                 case s1W2W3R: // e.g. les
111                 case s1RW2RW3R: // [i]div, cmpxch8b
112                   return i == 0 || i == 1;
113                 case s1W2R3RW: // (stack) push & pop
114                   return i == 0 || i == 2;
115                 case s1RW2R3RW:
116                   return i == 0 || i == 2;
117                 case sNONE:
118                 default:
119                     return false;
120             }
121         }
122
123
124     __thread NS_x86::ia32_instruction* InstructionDecoder_x86::decodedInstruction = NULL;
125     __thread ia32_locations* InstructionDecoder_x86::locs = NULL;
126     __thread bool InstructionDecoder_x86::sizePrefixPresent = false;
127     __thread bool InstructionDecoder_x86::addrSizePrefixPresent = false;
128     INSTRUCTION_EXPORT InstructionDecoder_x86::InstructionDecoder_x86(Architecture a) :
129       InstructionDecoderImpl(a)
130     {
131       if(a == Arch_x86_64) setMode(true);
132       
133     }
134     INSTRUCTION_EXPORT InstructionDecoder_x86::~InstructionDecoder_x86()
135     {
136
137     }
138     static const unsigned char modrm_use_sib = 4;
139     
140     INSTRUCTION_EXPORT void InstructionDecoder_x86::setMode(bool is64)
141     {
142         ia32_set_mode_64(is64);
143     }
144     
145       Expression::Ptr InstructionDecoder_x86::makeSIBExpression(const InstructionDecoder::buffer& b)
146     {
147         unsigned scale;
148         Register index;
149         Register base;
150         Result_Type registerType = ia32_is_mode_64() ? u64 : u32;
151
152         int op_type = ia32_is_mode_64() ? op_q : op_d;
153         decode_SIB(locs->sib_byte, scale, index, base);
154
155         Expression::Ptr scaleAST(make_shared(singleton_object_pool<Immediate>::construct(Result(u8, dword_t(scale)))));
156         Expression::Ptr indexAST(make_shared(singleton_object_pool<RegisterAST>::construct(makeRegisterID(index, op_type,
157                                     locs->rex_x))));
158         Expression::Ptr baseAST;
159         if(base == 0x05)
160         {
161             switch(locs->modrm_mod)
162             {
163                 case 0x00:
164                     baseAST = decodeImmediate(op_d, b.start + locs->sib_position + 1, true);
165                     break;
166                 case 0x01: 
167                 case 0x02: 
168                     baseAST = make_shared(singleton_object_pool<RegisterAST>::construct(makeRegisterID(base, 
169                                                                                                op_type,
170                                                                                                locs->rex_b)));
171                     break;
172                 case 0x03:
173                 default:
174                     assert(0);
175                     break;
176             };
177         }
178         else
179         {
180             baseAST = make_shared(singleton_object_pool<RegisterAST>::construct(makeRegisterID(base, 
181                                                                                                op_type,
182                                                                                                locs->rex_b)));
183         }
184
185         if(index == 0x04 && (!(ia32_is_mode_64()) || !(locs->rex_x)))
186         {
187             return baseAST;
188         }
189         return makeAddExpression(baseAST, makeMultiplyExpression(indexAST, scaleAST, registerType), registerType);
190     }
191
192       Expression::Ptr InstructionDecoder_x86::makeModRMExpression(const InstructionDecoder::buffer& b,
193                                                                   unsigned int opType)
194     {
195        unsigned int regType = op_d;
196        Result_Type aw;
197        if(ia32_is_mode_64())
198        {
199            if(addrSizePrefixPresent) {
200                aw = u32;
201            } else {
202                aw = u64;
203                regType = op_q;
204            }
205        }
206        else
207        {
208            if(!addrSizePrefixPresent) {
209                aw = u32;
210            } else {
211                aw = u16;
212                regType = op_w;
213            }
214        }
215         if (opType == op_lea) {
216             // For an LEA, aw (address width) is insufficient, use makeSizeType
217             aw = makeSizeType(opType);
218         }
219         Expression::Ptr e =
220             makeRegisterExpression(makeRegisterID(locs->modrm_rm, regType, locs->rex_b));
221         switch(locs->modrm_mod)
222         {
223             case 0:
224                 if(locs->modrm_rm == modrm_use_sib) {
225                     e = makeSIBExpression(b);
226                 }
227                 if(locs->modrm_rm == 0x5 && !addrSizePrefixPresent)
228                 {
229                     assert(locs->opcode_position > -1);
230                     if(ia32_is_mode_64())
231                     {
232                         e = makeAddExpression(makeRegisterExpression(x86_64::rip),
233                                             getModRMDisplacement(b), aw);
234                     }
235                     else
236                     {
237                         e = getModRMDisplacement(b);
238                     }
239         
240                 }
241                 if(locs->modrm_rm == 0x6 && addrSizePrefixPresent)
242                 {
243                     e = getModRMDisplacement(b);
244                 }
245                 if(opType == op_lea)
246                 {
247                     return e;
248                 }
249                 return makeDereferenceExpression(e, makeSizeType(opType));
250                 assert(0);
251                 break;
252             case 1:
253             case 2:
254             {
255                 if(locs->modrm_rm == modrm_use_sib) {
256                     e = makeSIBExpression(b);
257                 }
258                 Expression::Ptr disp_e = makeAddExpression(e, getModRMDisplacement(b), aw);
259                 if(opType == op_lea)
260                 {
261                     return disp_e;
262                 }
263                 return makeDereferenceExpression(disp_e, makeSizeType(opType));
264             }
265             assert(0);
266             break;
267             case 3:
268                 return makeRegisterExpression(makeRegisterID(locs->modrm_rm, opType, locs->rex_b));
269             default:
270                 return Expression::Ptr();
271         
272         };
273         // can't get here, but make the compiler happy...
274         assert(0);
275         return Expression::Ptr();
276     }
277
278     Expression::Ptr InstructionDecoder_x86::decodeImmediate(unsigned int opType, const unsigned char* immStart, 
279                                                             bool isSigned)
280     {
281         // rex_w indicates we need to sign-extend also.
282         isSigned = isSigned || locs->rex_w;
283         
284         switch(opType)
285         {
286             case op_b:
287                 return Immediate::makeImmediate(Result(isSigned ? s8 : u8 ,*(const byte_t*)(immStart)));
288                 break;
289             case op_d:
290                 return Immediate::makeImmediate(Result(isSigned ? s32 : u32,*(const dword_t*)(immStart)));
291             case op_w:
292                 return Immediate::makeImmediate(Result(isSigned ? s16 : u16,*(const word_t*)(immStart)));
293                 break;
294             case op_q:
295                 return Immediate::makeImmediate(Result(isSigned ? s64 : u64,*(const int64_t*)(immStart)));
296                 break;
297             case op_v:
298                 if (locs->rex_w || isDefault64Insn()) {
299                     return Immediate::makeImmediate(Result(isSigned ? s64 : u64,*(const int64_t*)(immStart)));
300                 }
301                 //if(!sizePrefixPresent)
302                 //{
303                     return Immediate::makeImmediate(Result(isSigned ? s32 : u32,*(const dword_t*)(immStart)));
304                     //}
305                     //else
306                     //{
307                     //return Immediate::makeImmediate(Result(isSigned ? s16 : u16,*(const word_t*)(immStart)));
308                     //}
309                 break;
310             case op_z:
311                 // 32 bit mode & no prefix, or 16 bit mode & prefix => 32 bit
312                 // 16 bit mode, no prefix or 32 bit mode, prefix => 16 bit
313                 //if(!addrSizePrefixPresent)
314                 //{
315                     return Immediate::makeImmediate(Result(isSigned ? s32 : u32,*(const dword_t*)(immStart)));
316                     //}
317                     //else
318                     //{
319                     //return Immediate::makeImmediate(Result(isSigned ? s16 : u16,*(const word_t*)(immStart)));
320                     //}
321                 break;
322             case op_p:
323                 // 32 bit mode & no prefix, or 16 bit mode & prefix => 48 bit
324                 // 16 bit mode, no prefix or 32 bit mode, prefix => 32 bit
325                 if(!sizePrefixPresent)
326                 {
327                     return Immediate::makeImmediate(Result(isSigned ? s48 : u48,*(const int64_t*)(immStart)));
328                 }
329                 else
330                 {
331                     return Immediate::makeImmediate(Result(isSigned ? s32 : u32,*(const dword_t*)(immStart)));
332                 }
333
334                 break;
335             case op_a:
336             case op_dq:
337             case op_pd:
338             case op_ps:
339             case op_s:
340             case op_si:
341             case op_lea:
342             case op_allgprs:
343             case op_512:
344             case op_c:
345                 assert(!"Can't happen: opType unexpected for valid ways to decode an immediate");
346                 return Expression::Ptr();
347             default:
348                 assert(!"Can't happen: opType out of range");
349                 return Expression::Ptr();
350         }
351     }
352     
353     Expression::Ptr InstructionDecoder_x86::getModRMDisplacement(const InstructionDecoder::buffer& b)
354     {
355         int disp_pos;
356
357         if(locs->sib_position != -1)
358         {
359             disp_pos = locs->sib_position + 1;
360         }
361         else
362         {
363             disp_pos = locs->modrm_position + 1;
364         }
365         switch(locs->modrm_mod)
366         {
367             case 1:
368                 return make_shared(singleton_object_pool<Immediate>::construct(Result(s8, (*(const byte_t*)(b.start +
369                         disp_pos)))));
370                 break;
371             case 2:
372                 if(0 && sizePrefixPresent)
373                 {
374                     return make_shared(singleton_object_pool<Immediate>::construct(Result(s16, *((const word_t*)(b.start +
375                             disp_pos)))));
376                 }
377                 else
378                 {
379                     return make_shared(singleton_object_pool<Immediate>::construct(Result(s32, *((const dword_t*)(b.start +
380                             disp_pos)))));
381                 }
382                 break;
383             case 0:
384                 // In 16-bit mode, the word displacement is modrm r/m 6
385                 if(sizePrefixPresent && !ia32_is_mode_64())
386                 {
387                     if(locs->modrm_rm == 6)
388                     {
389                         return make_shared(singleton_object_pool<Immediate>::construct(Result(s16,
390                                            *((const dword_t*)(b.start + disp_pos)))));
391                     }
392                     // TODO FIXME; this was decoding wrong, but I'm not sure
393                     // why...
394                     else if (locs->modrm_rm == 5) {
395                         assert(b.start + disp_pos + 4 <= b.end);
396                         return make_shared(singleton_object_pool<Immediate>::construct(Result(s32,
397                                            *((const dword_t*)(b.start + disp_pos)))));
398                     } else {
399                         assert(b.start + disp_pos + 1 <= b.end);
400                         return make_shared(singleton_object_pool<Immediate>::construct(Result(s8, 0)));
401                     }
402                     break;
403                 }
404                 // ...and in 32-bit mode, the dword displacement is modrm r/m 5
405                 else
406                 {
407                     if(locs->modrm_rm == 5)
408                     {
409                         if (b.start + disp_pos + 4 <= b.end) 
410                             return make_shared(singleton_object_pool<Immediate>::construct(Result(s32,
411                                                *((const dword_t*)(b.start + disp_pos)))));
412                         else
413                             return make_shared(singleton_object_pool<Immediate>::construct(Result()));
414                     }
415                     else
416                     {
417                         if (b.start + disp_pos + 1 <= b.end)
418                             return make_shared(singleton_object_pool<Immediate>::construct(Result(s8, 0)));
419                         else
420                             return make_shared(singleton_object_pool<Immediate>::construct(Result()));
421                     }
422                     break;
423                 }
424             default:
425                 assert(b.start + disp_pos + 1 <= b.end);
426                 return make_shared(singleton_object_pool<Immediate>::construct(Result(s8, 0)));
427                 break;
428         }
429     }
430
431     enum intelRegBanks
432     {
433         b_8bitNoREX = 0,
434         b_16bit,
435         b_32bit,
436         b_segment,
437         b_64bit,
438         b_xmm_set0, /* XMM0 -> XMM 7 */
439         b_xmm_set1, /* XMM8 -> XMM 15 */
440         b_xmm_set2, /* XMM16 -> XMM 23 */
441         b_xmm_set3, /* XMM24 -> XMM 31 */
442         b_ymm_set0, /* YMM0 -> YMM 7 */
443         b_ymm_set1, /* YMM8 -> YMM 15 */
444         b_ymm_set2, /* YMM16 -> YMM 23 */
445         b_ymm_set3, /* YMM24 -> YMM 31 */
446         b_zmm_set0, /* ZMM0 -> ZMM 7 */
447         b_zmm_set1, /* ZMM8 -> ZMM 15 */
448         b_zmm_set2, /* ZMM16 -> ZMM 23 */
449         b_zmm_set3, /* ZMM24 -> ZMM 31 */
450         b_mm,
451         b_cr,
452         b_dr,
453         b_tr,
454         b_amd64ext,
455         b_8bitWithREX,
456         b_fpstack,
457             amd64_ext_8,
458             amd64_ext_16,
459             amd64_ext_32,
460
461         b_invalid /* should remain the final entry */
462     };
463
464     static MachRegister IntelRegTable32[][8] = {
465         { x86::al, x86::cl, x86::dl, x86::bl, x86::ah, x86::ch, x86::dh, x86::bh }, /* b_8bitNoREX */
466         { x86::ax, x86::cx, x86::dx, x86::bx, x86::sp, x86::bp, x86::si, x86::di }, /* b_16bit */
467         { x86::eax, x86::ecx, x86::edx, x86::ebx, x86::esp, x86::ebp, x86::esi, x86::edi }, /* b_32bit */
468         { x86::es, x86::cs, x86::ss, x86::ds, x86::fs, x86::gs, InvalidReg, InvalidReg }, /* b_segment */
469         { x86_64::rax, x86_64::rcx, x86_64::rdx, x86_64::rbx, x86_64::rsp, x86_64::rbp, x86_64::rsi, x86_64::rdi }, /* b_64bit */
470         { x86::xmm0, x86::xmm1, x86::xmm2, x86::xmm3, x86::xmm4, x86::xmm5, x86::xmm6, x86::xmm7 }, /* b_xmm_set0 */
471         { x86_64::xmm8, x86_64::xmm9, x86_64::xmm10, x86_64::xmm11, x86_64::xmm12, x86_64::xmm13, x86_64::xmm14, x86_64::xmm15 }, /* b_xmm_set1 */
472         { x86_64::xmm16, x86_64::xmm17, x86_64::xmm18, x86_64::xmm19, x86_64::xmm20, x86_64::xmm21, x86_64::xmm22, x86_64::xmm23 }, /* b_xmm_set2 */
473         { x86_64::xmm24, x86_64::xmm25, x86_64::xmm26, x86_64::xmm27, x86_64::xmm28, x86_64::xmm29, x86_64::xmm30, x86_64::xmm31 }, /* b_xmm_set3 */
474         { x86_64::ymm0, x86_64::ymm1, x86_64::ymm2, x86_64::ymm3, x86_64::ymm4, x86_64::ymm5, x86_64::ymm6, x86_64::ymm7 }, /* b_ymm_set0 */
475         { x86_64::ymm8, x86_64::ymm9, x86_64::ymm10, x86_64::ymm11, x86_64::ymm12, x86_64::ymm13, x86_64::ymm14, x86_64::ymm15 }, /* b_ymm_set1 */
476         { x86_64::ymm16, x86_64::ymm17, x86_64::ymm18, x86_64::ymm19, x86_64::ymm20, x86_64::ymm21, x86_64::ymm22, x86_64::ymm23 }, /* b_ymm_set2 */
477         { x86_64::ymm24, x86_64::ymm25, x86_64::ymm26, x86_64::ymm27, x86_64::ymm28, x86_64::ymm29, x86_64::ymm30, x86_64::ymm31 }, /* b_ymm_set3 */
478         { x86_64::zmm0, x86_64::zmm1, x86_64::zmm2, x86_64::zmm3, x86_64::zmm4, x86_64::zmm5, x86_64::zmm6, x86_64::zmm7 }, /* b_zmm_set0 */
479         { x86_64::zmm8, x86_64::zmm9, x86_64::zmm10, x86_64::zmm11, x86_64::zmm12, x86_64::zmm13, x86_64::zmm14, x86_64::zmm15 }, /* b_zmm_set1 */
480         { x86_64::zmm16, x86_64::zmm17, x86_64::zmm18, x86_64::zmm19, x86_64::zmm20, x86_64::zmm21, x86_64::zmm22, x86_64::zmm23 }, /* b_zmm_set2 */
481         { x86_64::zmm24, x86_64::zmm25, x86_64::zmm26, x86_64::zmm27, x86_64::zmm28, x86_64::zmm29, x86_64::zmm30, x86_64::zmm31 }, /* b_zmm_set3 */
482         { x86::mm0, x86::mm1, x86::mm2, x86::mm3, x86::mm4, x86::mm5, x86::mm6, x86::mm7 },
483         { x86::cr0, x86::cr1, x86::cr2, x86::cr3, x86::cr4, x86::cr5, x86::cr6, x86::cr7 },
484         { x86::dr0, x86::dr1, x86::dr2, x86::dr3, x86::dr4, x86::dr5, x86::dr6, x86::dr7 },
485         { x86::tr0, x86::tr1, x86::tr2, x86::tr3, x86::tr4, x86::tr5, x86::tr6, x86::tr7 },
486         { x86_64::r8, x86_64::r9, x86_64::r10, x86_64::r11, x86_64::r12, x86_64::r13, x86_64::r14, x86_64::r15 },
487         { x86_64::al, x86_64::cl, x86_64::dl, x86_64::bl, x86_64::spl, x86_64::bpl, x86_64::sil, x86_64::dil },
488         { x86::st0, x86::st1, x86::st2, x86::st3, x86::st4, x86::st5, x86::st6, x86::st7 }
489     };
490
491     static MachRegister IntelRegTable64[][8] = {
492         { x86_64::al, x86_64::cl, x86_64::dl, x86_64::bl, x86_64::ah, x86_64::ch, x86_64::dh, x86_64::bh },
493         { x86_64::ax, x86_64::cx, x86_64::dx, x86_64::bx, x86_64::sp, x86_64::bp, x86_64::si, x86_64::di },
494         { x86_64::eax, x86_64::ecx, x86_64::edx, x86_64::ebx, x86_64::esp, x86_64::ebp, x86_64::esi, x86_64::edi },
495         { x86_64::es, x86_64::cs, x86_64::ss, x86_64::ds, x86_64::fs, x86_64::gs, InvalidReg, InvalidReg },
496         { x86_64::rax, x86_64::rcx, x86_64::rdx, x86_64::rbx, x86_64::rsp, x86_64::rbp, x86_64::rsi, x86_64::rdi },
497         { x86_64::xmm0, x86_64::xmm1, x86_64::xmm2, x86_64::xmm3, x86_64::xmm4, x86_64::xmm5, x86_64::xmm6, x86_64::xmm7 }, /* b_xmm_set0 */
498         { x86_64::xmm8, x86_64::xmm9, x86_64::xmm10, x86_64::xmm11, x86_64::xmm12, x86_64::xmm13, x86_64::xmm14, x86_64::xmm15 }, /* b_xmm_set1 */
499         { x86_64::xmm16, x86_64::xmm17, x86_64::xmm18, x86_64::xmm19, x86_64::xmm20, x86_64::xmm21, x86_64::xmm22, x86_64::xmm23 }, /* b_xmm_set2 */
500         { x86_64::xmm24, x86_64::xmm25, x86_64::xmm26, x86_64::xmm27, x86_64::xmm28, x86_64::xmm29, x86_64::xmm30, x86_64::xmm31 }, /* b_xmm_set3 */
501         { x86_64::ymm0, x86_64::ymm1, x86_64::ymm2, x86_64::ymm3, x86_64::ymm4, x86_64::ymm5, x86_64::ymm6, x86_64::ymm7 }, /* b_ymm_set0 */
502         { x86_64::ymm8, x86_64::ymm9, x86_64::ymm10, x86_64::ymm11, x86_64::ymm12, x86_64::ymm13, x86_64::ymm14, x86_64::ymm15 }, /* b_ymm_set1 */
503         { x86_64::ymm16, x86_64::ymm17, x86_64::ymm18, x86_64::ymm19, x86_64::ymm20, x86_64::ymm21, x86_64::ymm22, x86_64::ymm23 }, /* b_ymm_set2 */
504         { x86_64::ymm24, x86_64::ymm25, x86_64::ymm26, x86_64::ymm27, x86_64::ymm28, x86_64::ymm29, x86_64::ymm30, x86_64::ymm31 }, /* b_ymm_set3 */
505         { x86_64::zmm0, x86_64::zmm1, x86_64::zmm2, x86_64::zmm3, x86_64::zmm4, x86_64::zmm5, x86_64::zmm6, x86_64::zmm7 }, /* b_zmm_set0 */
506         { x86_64::zmm8, x86_64::zmm9, x86_64::zmm10, x86_64::zmm11, x86_64::zmm12, x86_64::zmm13, x86_64::zmm14, x86_64::zmm15 }, /* b_zmm_set1 */
507         { x86_64::zmm16, x86_64::zmm17, x86_64::zmm18, x86_64::zmm19, x86_64::zmm20, x86_64::zmm21, x86_64::zmm22, x86_64::zmm23 }, /* b_zmm_set2 */
508         { x86_64::zmm24, x86_64::zmm25, x86_64::zmm26, x86_64::zmm27, x86_64::zmm28, x86_64::zmm29, x86_64::zmm30, x86_64::zmm31 }, /* b_zmm_set3 */
509         { x86_64::mm0, x86_64::mm1, x86_64::mm2, x86_64::mm3, x86_64::mm4, x86_64::mm5, x86_64::mm6, x86_64::mm7 },
510         { x86_64::cr0, x86_64::cr1, x86_64::cr2, x86_64::cr3, x86_64::cr4, x86_64::cr5, x86_64::cr6, x86_64::cr7 },
511         { x86_64::dr0, x86_64::dr1, x86_64::dr2, x86_64::dr3, x86_64::dr4, x86_64::dr5, x86_64::dr6, x86_64::dr7 },
512         { x86_64::tr0, x86_64::tr1, x86_64::tr2, x86_64::tr3, x86_64::tr4, x86_64::tr5, x86_64::tr6, x86_64::tr7 },
513         { x86_64::r8, x86_64::r9, x86_64::r10, x86_64::r11, x86_64::r12, x86_64::r13, x86_64::r14, x86_64::r15 },
514         { x86_64::al, x86_64::cl, x86_64::dl, x86_64::bl, x86_64::spl, x86_64::bpl, x86_64::sil, x86_64::dil },
515         { x86_64::st0, x86_64::st1, x86_64::st2, x86_64::st3, x86_64::st4, x86_64::st5, x86_64::st6, x86_64::st7 },
516             { x86_64::r8b, x86_64::r9b, x86_64::r10b, x86_64::r11b, x86_64::r12b, x86_64::r13b, x86_64::r14b, x86_64::r15b },
517             { x86_64::r8w, x86_64::r9w, x86_64::r10w, x86_64::r11w, x86_64::r12w, x86_64::r13w, x86_64::r14w, x86_64::r15w },
518             { x86_64::r8d, x86_64::r9d, x86_64::r10d, x86_64::r11d, x86_64::r12d, x86_64::r13d, x86_64::r14d, x86_64::r15d },
519     };
520
521   /* Uses the appropriate lookup table based on the 
522      decoder architecture */
523   class IntelRegTable_access {
524     public:
525         inline MachRegister operator()(Architecture arch,
526                                        intelRegBanks bank,
527                                        int index)
528         {
529             assert(index >= 0 && index < 8);
530     
531             if(arch == Arch_x86_64)
532                 return IntelRegTable64[bank][index];
533             else if(arch == Arch_x86) 
534             {
535               if(bank > b_fpstack) return InvalidReg;
536               return IntelRegTable32[bank][index];
537             }
538             assert(0);
539             return InvalidReg;
540         }
541
542   };
543   static IntelRegTable_access IntelRegTable;
544
545       bool InstructionDecoder_x86::isDefault64Insn()
546       {
547         switch(m_Operation->getID())
548         {
549         case e_jmp:
550         case e_pop:
551         case e_push:
552         case e_call:
553           return true;
554         default:
555           return false;
556         }
557         
558       }
559       
560
561     MachRegister InstructionDecoder_x86::makeRegisterID(unsigned int intelReg, unsigned int opType,
562                                         bool isExtendedReg)
563     {
564         MachRegister retVal;
565         
566
567         if(isExtendedReg)
568         {
569             switch(opType)
570             {
571                 case op_q:  
572                     retVal = IntelRegTable(m_Arch,b_amd64ext,intelReg);
573                     break;
574                 case op_d:
575                     retVal = IntelRegTable(m_Arch,amd64_ext_32,intelReg);
576                     break;
577                 case op_w:
578                     retVal = IntelRegTable(m_Arch,amd64_ext_16,intelReg);
579                     break;
580                 case op_b:
581                     retVal = IntelRegTable(m_Arch,amd64_ext_8,intelReg);
582                     break;
583                 case op_v:
584                     if (locs->rex_w || isDefault64Insn())
585                         retVal = IntelRegTable(m_Arch, b_amd64ext, intelReg);
586                     else if (!sizePrefixPresent)
587                         retVal = IntelRegTable(m_Arch, amd64_ext_32, intelReg);
588                     //else
589                     //    retVal = IntelRegTable(m_Arch, amd64_ext_16, intelReg);
590                     break;      
591                 case op_p:
592                 case op_z:
593                     //              if (!sizePrefixPresent)
594                         retVal = IntelRegTable(m_Arch, amd64_ext_32, intelReg);
595                         //                  else
596                         //  retVal = IntelRegTable(m_Arch, amd64_ext_16, intelReg);
597                     break;
598             case op_f:
599             case op_dbl:
600                 // extended reg ignored on FP regs
601                 retVal = IntelRegTable(m_Arch, b_fpstack,intelReg);
602                 break;
603                 default:
604                     retVal = InvalidReg;
605             }
606         }
607         /* Promotion to 64-bit only applies to the operand types
608            that are varible (c,v,z). Ignoring c and z because they
609            do the right thing on 32- and 64-bit code.
610         else if(locs->rex_w)
611         {
612             // AMD64 with 64-bit operands
613             retVal = IntelRegTable[b_64bit][intelReg];
614         }
615         */
616         else
617         {
618             switch(opType)
619             {
620                 case op_v:
621                   if(locs->rex_w || isDefault64Insn())
622                         retVal = IntelRegTable(m_Arch,b_64bit,intelReg);
623                     else
624                         retVal = IntelRegTable(m_Arch,b_32bit,intelReg);
625                     break;
626                 case op_b:
627                     if (locs->rex_byte & 0x40) {
628                         retVal = IntelRegTable(m_Arch,b_8bitWithREX,intelReg);
629                     } else {
630                         retVal = IntelRegTable(m_Arch,b_8bitNoREX,intelReg);
631                     }
632                     break;
633                 case op_q:
634                     retVal = IntelRegTable(m_Arch,b_64bit,intelReg);
635                     break;
636                 case op_w:
637                     retVal = IntelRegTable(m_Arch,b_16bit,intelReg);
638                     break;
639                 case op_f:
640                 case op_dbl:
641                     retVal = IntelRegTable(m_Arch,b_fpstack,intelReg);
642                     break;
643                 case op_d:
644                 case op_si:
645                     retVal = IntelRegTable(m_Arch,b_32bit,intelReg);
646                     break;
647                 default:
648                     retVal = IntelRegTable(m_Arch,b_32bit,intelReg);
649                     break;
650             }
651         }
652
653         if (!ia32_is_mode_64()) {
654           if ((retVal.val() & 0x00ffffff) == 0x0001000c)
655             assert(0);
656         }
657
658         return MachRegister((retVal.val() & ~retVal.getArchitecture()) | m_Arch);
659     }
660     
661     Result_Type InstructionDecoder_x86::makeSizeType(unsigned int opType)
662     {
663         switch(opType)
664         {
665             case op_b:
666             case op_c:
667                 return u8;
668             case op_d:
669             case op_ss:
670             case op_allgprs:
671             case op_si:
672                 return u32;
673             case op_w:
674             case op_a:
675                 return u16;
676             case op_q:
677             case op_sd:
678                 return u64;
679             case op_v:
680             case op_lea:
681             case op_z:
682                 if (locs->rex_w) 
683                 {
684                     return u64;
685                 }
686                 //if(ia32_is_mode_64() || !sizePrefixPresent)
687                 //{
688                     return u32;
689                     //}
690                     //else
691                     //{
692                     //return u16;
693                     //}
694                 break;
695             case op_y:
696                 if(ia32_is_mode_64())
697                         return u64;
698                 else
699                         return u32;
700                 break;
701             case op_p:
702                 // book says operand size; arch-x86 says word + word * operand size
703                 if(!ia32_is_mode_64() ^ sizePrefixPresent)
704                 {
705                     return u48;
706                 }
707                 else
708                 {
709                     return u32;
710                 }
711             case op_dq:
712             case op_qq:
713                 return u64;
714             case op_512:
715                 return m512;
716             case op_pi:
717             case op_ps:
718             case op_pd:
719                 return dbl128;
720             case op_s:
721                 return u48;
722             case op_f:
723                 return sp_float;
724             case op_dbl:
725                 return dp_float;
726             case op_14:
727                 return m14;
728             default:
729                 assert(!"Can't happen!");
730                 return u8;
731         }
732     }
733
734     enum AVX_Regtype { AVX_XMM = 0, AVX_YMM, AVX_ZMM, AVX_NONE };
735     #define AVX_TYPE_OKAY(type) ((type) >= AVX_XMM && (type) <= AVX_ZMM)
736     /** 
737      * Decode an avx register based on the type of prefix. Returns true if the
738      * given configuration is invalid and should be rejected.
739      */
740     bool decodeAVX(intelRegBanks& bank, int* bank_index, int regnum, AVX_Regtype type, ia32_prefixes& pref, unsigned int admet)
741     {
742
743         /* Check to see if this is just a normal MMX register access */
744         if(type >= AVX_NONE || type < 0)
745         {
746 #ifdef VEX_DEBUG
747             printf("VEX OPERAND:  REGNUM: %d  ", regnum);
748             printf("REG_TYPE: AVX_NONE (%d)\n", type);
749 #endif
750             /* Only registers XMM0 - XMM15 are usable */
751
752             /* The register must be valid */
753             if(regnum < 0) 
754                 return true;
755
756             if(regnum < 8)
757             {
758                 bank = b_xmm_set0;
759                 *bank_index = regnum;
760             } else if(regnum < 16)
761             {
762                 bank = b_xmm_set1;
763                 *bank_index = regnum - 8;
764             } else {
765                 /* Value is out of the valid range */
766                 return true;
767             }
768
769             /* Return success */
770             return false;
771         }
772
773         switch(admet)
774         {
775             case am_V: case am_YV: case am_XV:
776                 switch(pref.vex_type)
777                 {
778                     case VEX_TYPE_EVEX:
779                         regnum |= pref.vex_R << 4;
780                         regnum |= pref.vex_r << 3;
781                         break;
782                     default:break;
783                 }
784                 break;
785             case am_U: case am_YU: case am_XU:
786             case am_W: case am_YW: case am_XW:
787                 switch(pref.vex_type)
788                 {
789                     case VEX_TYPE_EVEX:
790                         regnum |= pref.vex_x << 4;
791                         regnum |= pref.vex_b << 3;
792                         break;
793                     default: break;
794                 }
795                 break;
796             default: break;
797         }
798 #ifdef VEX_DEBUG
799         printf("VEX OPERAND:  REGNUM: %d  ", regnum);
800 #endif
801
802         /* Operand is potentially XMM, YMM or ZMM */
803         int setnum = 0;
804         if(regnum < 8)
805         {
806             setnum = 0;
807             *bank_index = regnum;
808         } else if(regnum < 16)
809         {
810             setnum = 1;
811             *bank_index = regnum - 8;
812         } else if(regnum < 24)
813         {
814             setnum = 2;
815             *bank_index = regnum - 16;
816         } else if(regnum < 32){
817             setnum = 3;
818             *bank_index = regnum - 24;
819         } else {
820 #ifdef VEX_DEBUG
821             printf("AVX REGISTER NUMBER:   %d   is invalid!!\n", regnum);
822 #endif
823             return false;
824         }
825
826         switch(type)
827         {
828             case AVX_XMM:
829 #ifdef VEX_DEBUG
830                 printf("REG_TYPE: AVX_XMM (%d)\n", type);
831 #endif
832                 if(setnum == 0)
833                     bank = b_xmm_set0;
834                 else if(setnum == 1)
835                     bank = b_xmm_set1;
836                 else if(setnum == 2)
837                     bank = b_xmm_set2;
838                 else if(setnum == 3)
839                     bank = b_xmm_set3;
840                 else return true;
841                 break;
842             case AVX_YMM:
843 #ifdef VEX_DEBUG
844                 printf("REG_TYPE: AVX_YMM (%d)\n", type);
845 #endif
846                 if(setnum == 0)
847                     bank = b_ymm_set0;
848                 else if(setnum == 1)
849                     bank = b_ymm_set1;
850                 else if(setnum == 2)
851                     bank = b_ymm_set2;
852                 else if(setnum == 3)
853                     bank = b_ymm_set3;
854                 else return true;
855                 break;
856             case AVX_ZMM:
857 #ifdef VEX_DEBUG
858                 printf("REG_TYPE: AVX_ZMM (%d)\n", type);
859 #endif
860                 if(setnum == 0)
861                     bank = b_zmm_set0;
862                 else if(setnum == 1)
863                     bank = b_zmm_set1;
864                 else if(setnum == 2)
865                     bank = b_zmm_set2;
866                 else if(setnum == 3)
867                     bank = b_zmm_set3;
868                 else return true;
869                 break;
870             default:
871                 return true;
872         }
873
874         /* Return Success */
875         return false;
876     }
877
878     bool InstructionDecoder_x86::decodeOneOperand(const InstructionDecoder::buffer& b,
879                                                   const ia32_operand& operand,
880                                                   int & imm_index, /* immediate operand index */
881                                                   const Instruction* insn_to_complete, bool isRead, bool isWritten)
882     {
883         bool isCFT = false;
884         bool isCall = false;
885         bool isConditional = false;
886         InsnCategory cat = insn_to_complete->getCategory();
887
888         if(cat == c_BranchInsn || cat == c_CallInsn)
889             {
890             isCFT = true;
891             if(cat == c_CallInsn)
892             {
893                 isCall = true;
894             }
895             }
896
897         if(cat == c_BranchInsn && insn_to_complete->getOperation().getID() != e_jmp) 
898         {
899                 isConditional = true;
900         }
901
902         /* There must be a preliminary decoded instruction */
903         if(!decodedInstruction)
904             assert(!"No decoded instruction!\n");
905
906         unsigned int optype = operand.optype;
907         AVX_Regtype avx_type = AVX_NONE; /* The AVX register type (if VEX prefixed) */
908         intelRegBanks bank = b_invalid; /* Specifies an AVX bank to use for register decoding */
909         int bank_index = -1; /* Specifies a bank index for an AVX register */
910         ia32_prefixes& pref = *decodedInstruction->getPrefix();
911         int regnum; /* Used to keep track of some register positions */
912
913         if (sizePrefixPresent
914             && ((optype == op_v) || (optype == op_z))
915             && (operand.admet != am_J))
916         {
917             optype = op_w;
918         }
919
920         if(pref.vex_present)
921         {
922             /* Get the AVX type from the prefix */
923             avx_type = (AVX_Regtype)pref.vex_ll;
924         }
925
926         if (sizePrefixPresent && ((optype == op_v) 
927                 || (optype == op_z)) && (operand.admet != am_J)) 
928         {
929             optype = op_w;
930         }
931
932         if(optype == op_y) 
933         {
934             if(ia32_is_mode_64() && locs->rex_w)
935             {
936                 optype = op_q;
937             } else {
938                 optype = op_d;
939             }
940         }
941      
942         switch(operand.admet)
943         {
944                         case 0:
945                                 // No operand
946                                 {
947                                         // fprintf(stderr, "ERROR: Instruction with mismatched operands. Raw bytes: ");
948                                         // for(unsigned int i = 0; i < decodedInstruction->getSize(); i++) {
949                                         //      fprintf(stderr, "%x ", b.start[i]);
950                                         // }
951                                         // fprintf(stderr, "\n");*/
952                                         assert(!"Mismatched number of operands--check tables");
953                                         return false;
954                                 }
955                 return false;
956             case am_A:
957                 {
958                         // am_A only shows up as a far call/jump.  Position 1 should be universally safe.
959                         Expression::Ptr addr(decodeImmediate(optype, b.start + 1));
960                         insn_to_complete->addSuccessor(addr, isCall, false, false, false);
961                 }
962                         break;
963
964             case am_C:
965                 {
966                     Expression::Ptr op(makeRegisterExpression(
967                             IntelRegTable(m_Arch,b_cr,locs->modrm_reg)));
968                     insn_to_complete->appendOperand(op, isRead, isWritten);
969                 }
970                 break;
971
972             case am_D:
973                 {
974                     Expression::Ptr op(makeRegisterExpression(
975                                 IntelRegTable(m_Arch,b_dr,locs->modrm_reg)));
976                     insn_to_complete->appendOperand(op, isRead, isWritten);
977                 }
978                 break;
979
980             case am_E:
981                 // am_M is like am_E, except that mod of 0x03 should 
982                 // never occur (am_M specified memory,
983                 // mod of 0x03 specifies direct register access).
984             case am_M:
985                 // am_R is the inverse of am_M; it should only have a mod of 3
986             case am_R:
987                 // can be am_R or am_M  
988                         case am_RM:     
989                 if(isCFT)
990                 {
991                     insn_to_complete->addSuccessor(
992                             makeModRMExpression(b, optype), 
993                             isCall, true, false, false);
994                 } else {
995                     insn_to_complete->appendOperand(
996                             makeModRMExpression(b, optype), 
997                             isRead, isWritten);
998                 }
999                 break;
1000
1001             case am_F:
1002                 {
1003                     Expression::Ptr op(makeRegisterExpression(x86::flags));
1004                     insn_to_complete->appendOperand(op, isRead, isWritten);
1005                 }
1006                 break;
1007
1008             case am_G:
1009                 {
1010                     Expression::Ptr op(makeRegisterExpression(
1011                             makeRegisterID(locs->modrm_reg, optype, locs->rex_r)));
1012                     insn_to_complete->appendOperand(op, isRead, isWritten);
1013                 }
1014                 break;
1015
1016             case am_H: /* Could be XMM, YMM or ZMM */
1017                 /* Make sure this register class is valid for VEX */
1018                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1019                     return false;
1020
1021                 /* Grab the correct bank and bank index for this type of register */
1022                 if(decodeAVX(bank, &bank_index, pref.vex_vvvv_reg, avx_type, pref, operand.admet))
1023                     return false;
1024
1025                 /* Append the operand */
1026                 insn_to_complete->appendOperand(makeRegisterExpression(
1027                         IntelRegTable(m_Arch, bank, bank_index)), 
1028                         isRead, isWritten);
1029                 break;
1030
1031                         case am_I:
1032                 insn_to_complete->appendOperand(decodeImmediate(optype, b.start +
1033                 locs->imm_position[imm_index++]),
1034                 isRead, isWritten);
1035                 break;
1036             case am_J:
1037                 {
1038                     Expression::Ptr Offset(decodeImmediate(optype,
1039                         b.start + locs->imm_position[imm_index++],
1040                         true));
1041                     Expression::Ptr EIP(makeRegisterExpression(MachRegister::getPC(m_Arch)));
1042                     Expression::Ptr InsnSize(
1043                                                         make_shared(singleton_object_pool<Immediate>::construct(Result(u8,
1044                                                         decodedInstruction->getSize()))));
1045                     Expression::Ptr postEIP(makeAddExpression(EIP, InsnSize, u32));
1046                     Expression::Ptr op(makeAddExpression(Offset, postEIP, u32));
1047                     insn_to_complete->addSuccessor(op, isCall, false, isConditional, false);
1048                     if (isConditional)
1049                         insn_to_complete->addSuccessor(postEIP, false, false, true, true);
1050                 }
1051                 break;
1052                         case am_O:
1053                                 {
1054                                         // Address/offset width, which is *not* what's encoded by the optype...
1055                                         // The deref's width is what's actually encoded here.
1056                                         int pseudoOpType;
1057                                         switch(locs->address_size)
1058                                         {
1059                                                 case 1:
1060                                                         pseudoOpType = op_b;
1061                                                         break;
1062                                                 case 2:
1063                                                         pseudoOpType = op_w;
1064                                                         break;
1065                                                 case 4:
1066                                                         pseudoOpType = op_d;
1067                                                         break;
1068                                                 case 0:
1069                                                         if(m_Arch == Arch_x86_64) {
1070                                                                 if(!addrSizePrefixPresent)
1071                                                                         pseudoOpType = op_q;
1072                                                                 else
1073                                                                         pseudoOpType = op_d;
1074                                                                 } else {
1075                                                                         pseudoOpType = op_v;
1076                                                         }
1077                                                         break;
1078                                                 default:
1079                                                         assert(!"Bad address size, should be 0, 1, 2, or 4!");
1080                                                         pseudoOpType = op_b;
1081                                                         break;
1082                                         }
1083                 
1084                                         int offset_position = locs->opcode_position;
1085                                         if(locs->modrm_position > offset_position && locs->modrm_operand <
1086                                                 (int)(insn_to_complete->m_Operands.size()))
1087                                         {
1088                                                 offset_position = locs->modrm_position;
1089                                         }
1090                 
1091                                         if(locs->sib_position > offset_position)
1092                                         {
1093                                                 offset_position = locs->sib_position;
1094                                         }
1095                 
1096                                         offset_position++;
1097                                         insn_to_complete->appendOperand(makeDereferenceExpression(
1098                                                         decodeImmediate(pseudoOpType, b.start + offset_position), 
1099                                                         makeSizeType(optype)), isRead, isWritten);
1100                                 }
1101                                 break;
1102
1103                         case am_P:
1104                                 insn_to_complete->appendOperand(makeRegisterExpression(
1105                                         IntelRegTable(m_Arch,b_mm,locs->modrm_reg)),
1106                                         isRead, isWritten);
1107                                 break;
1108
1109                         case am_Q:
1110                                 switch(locs->modrm_mod)
1111                                 {
1112                                         // direct dereference
1113                                         case 0x00:
1114                                         case 0x01:
1115                                         case 0x02:
1116                                                 insn_to_complete->appendOperand(makeModRMExpression(b, optype), 
1117                                                                 isRead, isWritten);
1118                                                 break;
1119                                         case 0x03:
1120                                                 // use of actual register
1121                                                 insn_to_complete->appendOperand(makeRegisterExpression(
1122                                                                         IntelRegTable(m_Arch,b_mm,locs->modrm_rm)),
1123                                                                         isRead, isWritten);
1124                                                 break;
1125                                         default:
1126                                                 assert(!"2-bit value modrm_mod out of range");
1127                                                 break;
1128                                 };
1129                                 break;
1130
1131                         case am_S:
1132                                 // Segment register in modrm reg field.
1133                                 insn_to_complete->appendOperand(makeRegisterExpression(
1134                                         IntelRegTable(m_Arch,b_segment,locs->modrm_reg)),
1135                                         isRead, isWritten);
1136                                 break;
1137
1138                         case am_UM:
1139                                 switch(locs->modrm_mod)
1140                                 {
1141                                         // direct dereference
1142                                         case 0x00:
1143                                         case 0x01:
1144                                         case 0x02:
1145                                                 insn_to_complete->appendOperand(
1146                                                         makeModRMExpression(b, makeSizeType(optype)),
1147                                                         isRead, isWritten);
1148                                                 break;
1149                                         case 0x03:
1150                                                 // use of actual register (am_U)
1151                                                 {
1152                                                         /* Is this a vex prefixed instruction? */
1153                                         if(pref.vex_present)
1154                                         {
1155                                         if(!AVX_TYPE_OKAY(avx_type))
1156                                                 return false;
1157                                         }
1158                         
1159                                         /* Grab the register bank and index */
1160                                         if(decodeAVX(bank, &bank_index, locs->modrm_rm, AVX_XMM, 
1161                                                                                 pref, operand.admet))
1162                                                                 return false;
1163                         
1164                                         /* Append the operand */
1165                                         insn_to_complete->appendOperand(makeRegisterExpression(
1166                                                                         IntelRegTable(m_Arch, bank, bank_index)), 
1167                                                                         isRead, isWritten);     
1168                                                         break;
1169                                                 }
1170                                         default:
1171                                                 assert(!"2-bit value modrm_mod out of range");
1172                                                 break;
1173                                 };
1174                                 break;
1175
1176                         case am_XH: /* Must be XMM */
1177                                 /* Make sure we are using a valid VEX register class */
1178                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1179                     return false;
1180
1181                 /* Constrain register type to only the XMM banks */
1182                 avx_type = AVX_XMM;
1183
1184                 /* Grab the correct bank and bank index for this type of register */
1185                 if(decodeAVX(bank, &bank_index, pref.vex_vvvv_reg, avx_type, pref, operand.admet))
1186                     return false;
1187
1188                 insn_to_complete->appendOperand(makeRegisterExpression(
1189                         IntelRegTable(m_Arch, bank, bank_index)), 
1190                         isRead, isWritten);
1191                 break;
1192
1193             case am_YH: /* Could be XMM or YMM */
1194                 /* Make sure we are using a valid VEX register class */
1195                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1196                     return false;
1197
1198                 /* Constrain to only XMM or YMM registers */
1199                 if(avx_type != AVX_XMM && avx_type != AVX_YMM)
1200                     avx_type = AVX_YMM;
1201
1202                 /* Grab the correct bank and bank index for this type of register */
1203                 if(decodeAVX(bank, &bank_index, pref.vex_vvvv_reg, avx_type, pref, operand.admet))
1204                     return false;
1205
1206                 /* Append the operand */
1207                 insn_to_complete->appendOperand(makeRegisterExpression(
1208                         IntelRegTable(m_Arch, bank, bank_index)), 
1209                         isRead, isWritten);
1210                 break;
1211
1212             case am_U: /* Could be XMM, YMM, or ZMM (or possibly non VEX)*/
1213
1214                 /* Is this a vex prefixed instruction? */  
1215                 if(pref.vex_present)
1216                 {
1217                     if(!AVX_TYPE_OKAY(avx_type))
1218                         return false;
1219                 }
1220
1221                 /* Grab the register bank and index */
1222                 if(decodeAVX(bank, &bank_index, locs->modrm_rm, AVX_XMM, pref, operand.admet))
1223                     return false;
1224
1225                 /* Append the operand */
1226                 insn_to_complete->appendOperand(makeRegisterExpression(IntelRegTable(m_Arch, bank, bank_index)), isRead, isWritten);
1227                 break;
1228             case am_XU: /* Must be XMM (must be VEX) */
1229                 /* Make sure this register class is valid */
1230                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1231                     return false;
1232   
1233                 /* Constrain register to XMM banks only */        
1234                 avx_type = AVX_XMM;
1235
1236                 /* Get the register bank and index for this register */
1237                 if(decodeAVX(bank, &bank_index, locs->modrm_rm, avx_type, pref, operand.admet))
1238                     return false;
1239
1240                 /* Append the operand */
1241                 insn_to_complete->appendOperand(makeRegisterExpression(IntelRegTable(m_Arch, bank, bank_index)), isRead, isWritten);
1242                 break;
1243             case am_YU: /* Must be XMM or YMM (must be VEX) */
1244                 /* Make sure this register class is valid */
1245                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1246                     return false;
1247
1248                 /* Constrain to either XMM or YMM registers */
1249                 if(avx_type != AVX_XMM && avx_type != AVX_YMM)
1250                     avx_type = AVX_YMM;
1251
1252                 /* Get the register bank and index */
1253                 if(decodeAVX(bank, &bank_index, locs->modrm_rm, avx_type, pref, operand.admet))
1254                     return false;
1255
1256                 /* Append the operand */
1257                 insn_to_complete->appendOperand(makeRegisterExpression(IntelRegTable(m_Arch, bank, bank_index)), isRead, isWritten);
1258                 break;
1259             case am_V: /* Could be XMM, YMM or ZMM (possibly non VEX)*/
1260                 /* Is this a vex prefixed instruction? */
1261                 if(pref.vex_present && !AVX_TYPE_OKAY(avx_type))
1262                     return false;
1263
1264                 /* Get the base register number */
1265                 regnum = locs->modrm_reg;
1266
1267                 /* Get the register bank and the index */
1268                 if(decodeAVX(bank, &bank_index, regnum, avx_type, pref, operand.admet))
1269                     return false;
1270
1271                 /* Append the operand */
1272                 insn_to_complete->appendOperand(makeRegisterExpression(IntelRegTable(m_Arch, bank, bank_index)), isRead, isWritten);
1273                 break;
1274             case am_XV: /* Must be XMM (must be VEX) */
1275
1276                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1277                     return false;
1278
1279                 regnum = locs->modrm_reg;
1280
1281                 /* Get the register bank and the index */
1282                 if(decodeAVX(bank, &bank_index, regnum, avx_type, pref, operand.admet))
1283                     return false;
1284
1285                 /* Append the operand */
1286                 insn_to_complete->appendOperand(makeRegisterExpression(IntelRegTable(m_Arch, bank, bank_index)), isRead, isWritten);
1287                 break;
1288             case am_YV: /* Must be XMM or YMM (must be VEX) */
1289                 /* Make sure this register class is valid */
1290                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1291                     return false;
1292
1293                 regnum = locs->modrm_reg;
1294
1295                 /* Constrain to either XMM or YMM registers */
1296                 if(avx_type != AVX_XMM && avx_type != AVX_YMM)
1297                     avx_type = AVX_YMM;
1298
1299                 /* Get the register bank and index */
1300                 if(decodeAVX(bank, &bank_index, regnum, avx_type, pref, operand.admet))
1301                     return false;
1302
1303                 /* Append the operand */
1304                 insn_to_complete->appendOperand(makeRegisterExpression(IntelRegTable(m_Arch, bank, bank_index)), isRead, isWritten);
1305                 break;
1306             case am_W: /* Could be XMM, YMM, or ZMM (or possibly not VEX) */
1307
1308                                 if(pref.vex_present)
1309                                 {
1310                                         if(!AVX_TYPE_OKAY(avx_type))
1311                                         return false;
1312                                 }
1313                                 
1314                                 switch(locs->modrm_mod)
1315                                 {
1316                                         /* Direct dereference */
1317                                         case 0x00:
1318                                         case 0x01:
1319                                         case 0x02:
1320                                                 insn_to_complete->appendOperand(
1321                                                         makeModRMExpression(b, makeSizeType(optype)), isRead, isWritten);
1322                                                 break;
1323                                         case 0x03:
1324                                                 /* Just the register is used */
1325                                                 if(decodeAVX(bank, &bank_index, locs->modrm_rm, 
1326                                                                         avx_type, pref, operand.admet))
1327                                                         return false;
1328
1329                                                 insn_to_complete->appendOperand(
1330                                                                 makeRegisterExpression(IntelRegTable(
1331                                                                                 m_Arch, bank, bank_index)), isRead, isWritten);
1332                                                 break;
1333                                         default:
1334                                                 assert(!"2-bit value modrm_mod out of range");
1335                                                 break;
1336                                 }
1337                                 break;
1338             case am_XW: /* Must be XMM (must be VEX) */
1339
1340                 /* Make sure this vex is okay */
1341                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1342                     return false;
1343          
1344                 /* Constrain to the XMM banks */ 
1345                 avx_type = AVX_XMM;
1346
1347                 switch(locs->modrm_mod)
1348                             {
1349                     /* Direct dereference */
1350                     case 0x00:
1351                     case 0x01:
1352                     case 0x02:
1353                         insn_to_complete->appendOperand(makeModRMExpression(b, makeSizeType(optype)), isRead, isWritten);
1354                         break;
1355                     case 0x03:
1356                         /* Just the register is used */
1357                         if(decodeAVX(bank, &bank_index, locs->modrm_rm, avx_type, pref, operand.admet))
1358                             return false;
1359                         insn_to_complete->appendOperand(makeRegisterExpression(IntelRegTable(m_Arch, bank, bank_index)), isRead, isWritten);
1360                         break;
1361                     default:
1362                         assert(!"2-bit value modrm_mod out of range");
1363                                 break;
1364                             }
1365                 break;
1366             case am_YW: /* Must be either YMM or XMM (must be VEX) */
1367
1368                 /* Make sure the register class is okay and we have a vex prefix */
1369                 if(!AVX_TYPE_OKAY(avx_type) || !pref.vex_present)
1370                     return false;
1371
1372                 /* Constrain to either XMM or YMM registers */
1373                 if(avx_type != AVX_XMM && avx_type != AVX_YMM)
1374                     avx_type = AVX_YMM;
1375
1376                 switch(locs->modrm_mod)
1377                 {
1378                     /* Direct dereference */
1379                     case 0x00:
1380                     case 0x01:
1381                     case 0x02:
1382                         insn_to_complete->appendOperand(makeModRMExpression(b, makeSizeType(optype)), isRead, isWritten);
1383                         break;
1384                     case 0x03:
1385                         /* Just the register is used */
1386                         if(decodeAVX(bank, &bank_index, locs->modrm_rm, avx_type, pref, operand.admet))
1387                             return false;
1388
1389                         /* Append the operand */
1390                         insn_to_complete->appendOperand(makeRegisterExpression(IntelRegTable(m_Arch, bank, bank_index)), isRead, isWritten);
1391                         break;
1392                             default:
1393                                 assert(!"2-bit value modrm_mod out of range");
1394                                 break;
1395                 }
1396                                 break;
1397                         case am_X:
1398                                 {
1399                                         MachRegister si_reg;
1400                                         if(m_Arch == Arch_x86)
1401                                         {
1402                                                 if(addrSizePrefixPresent)
1403                                                 {
1404                                                         si_reg = x86::si;
1405                                                 } else {
1406                                                         si_reg = x86::esi;
1407                                                 }
1408                                         } else {
1409                                                 if(addrSizePrefixPresent)
1410                                                 {
1411                                                         si_reg = x86_64::esi;
1412                                                 } else {
1413                                                         si_reg = x86_64::rsi;
1414                                                 }
1415                                         }
1416                                         
1417                                         Expression::Ptr ds(makeRegisterExpression(
1418                                                                 m_Arch == Arch_x86 ? x86::ds : x86_64::ds));
1419                                         Expression::Ptr si(makeRegisterExpression(si_reg));
1420                                         Expression::Ptr segmentOffset(make_shared
1421                                                         (singleton_object_pool<Immediate>::construct(Result(u32, 0x10))));
1422                                         Expression::Ptr ds_segment = makeMultiplyExpression(
1423                                                         ds, segmentOffset, u32);
1424                                         Expression::Ptr ds_si = makeAddExpression(ds_segment, si, u32);
1425                                         insn_to_complete->appendOperand(
1426                                                         makeDereferenceExpression(ds_si, makeSizeType(optype)), 
1427                                                         isRead, isWritten);
1428                                 }
1429                                 break;
1430                         case am_Y:
1431                                 {
1432                                         MachRegister di_reg;
1433                                         if(m_Arch == Arch_x86)
1434                                         {
1435                                                 if(addrSizePrefixPresent)
1436                                                 {
1437                                                         di_reg = x86::di;
1438                                                 } else {
1439                                                         di_reg = x86::edi;
1440                                                 }
1441                                         } else {
1442                                                 if(addrSizePrefixPresent)
1443                                                 {
1444                                                         di_reg = x86_64::edi;
1445                                                 } else {
1446                                                         di_reg = x86_64::rdi;
1447                                                 }
1448                                         }
1449
1450                                         Expression::Ptr es(makeRegisterExpression(
1451                                                                 m_Arch == Arch_x86 ? x86::es : x86_64::es));
1452                                         Expression::Ptr di(makeRegisterExpression(di_reg));
1453                                         
1454                                         Immediate::Ptr imm(make_shared(
1455                                                                 singleton_object_pool<Immediate>::construct(Result(u32, 0x10))));
1456                                         Expression::Ptr es_segment(
1457                                                         makeMultiplyExpression(es,imm, u32));
1458                                         Expression::Ptr es_di(makeAddExpression(es_segment, di, u32));
1459                                         insn_to_complete->appendOperand(
1460                                                         makeDereferenceExpression(es_di, makeSizeType(optype)),
1461                                                         isRead, isWritten);
1462                                         
1463                                 }
1464                                 break;
1465                         case am_tworeghack:
1466                         if(optype == op_edxeax)
1467                         {
1468                             Expression::Ptr edx(makeRegisterExpression(m_Arch == Arch_x86 ? x86::edx : x86_64::edx));
1469                             Expression::Ptr eax(makeRegisterExpression(m_Arch == Arch_x86 ? x86::eax : x86_64::eax));
1470                     Expression::Ptr highAddr = makeMultiplyExpression(edx, Immediate::makeImmediate(Result(u64, 2^32)), u64);
1471                             Expression::Ptr addr = makeAddExpression(highAddr, eax, u64);
1472                             Expression::Ptr op = makeDereferenceExpression(addr, u64);
1473                             insn_to_complete->appendOperand(op, isRead, isWritten);
1474                 } else if (optype == op_ecxebx)
1475                         {
1476                             Expression::Ptr ecx(makeRegisterExpression(m_Arch == Arch_x86 ? x86::ecx : x86_64::ecx));
1477                             Expression::Ptr ebx(makeRegisterExpression(m_Arch == Arch_x86 ? x86::ebx : x86_64::ebx));
1478                             Expression::Ptr highAddr = makeMultiplyExpression(ecx,
1479                                     Immediate::makeImmediate(Result(u64, 2^32)), u64);
1480                             Expression::Ptr addr = makeAddExpression(highAddr, ebx, u64);
1481                             Expression::Ptr op = makeDereferenceExpression(addr, u64);
1482                             insn_to_complete->appendOperand(op, isRead, isWritten);
1483                         }
1484                     break;
1485                     
1486                     case am_reg:
1487                     {
1488                         MachRegister r(optype);
1489                         int size = r.size();
1490                     if((m_Arch == Arch_x86_64) && (r.regClass() == (unsigned int)x86::GPR) && (size == 4))
1491                         {
1492                             int reg_size = isDefault64Insn() ? op_q : op_v;
1493                             if(sizePrefixPresent)
1494                             {
1495                                 reg_size = op_w;
1496                             }
1497                             // implicit regs are not extended
1498                             r = makeRegisterID((r.val() & 0xFF), reg_size, false);
1499                             entryID entryid = decodedInstruction->getEntry()->getID(locs);
1500                             if(locs->rex_b && insn_to_complete->m_Operands.empty() &&
1501                                (entryid == e_push || entryid == e_pop || entryid == e_xchg || ((*(b.start + locs->opcode_position) & 0xf0) == 0xb0)))
1502                             {
1503                                 r = MachRegister((r.val()) | x86_64::r8.val());
1504                                 assert(r.name() != "<INVALID_REG>");
1505                             }
1506                     } else {
1507                             r = MachRegister((r.val() & ~r.getArchitecture()) | m_Arch);
1508                             
1509                             entryID entryid = decodedInstruction->getEntry()->getID(locs);
1510                             if(insn_to_complete->m_Operands.empty() && 
1511                                (entryid == e_push || entryid == e_pop || entryid == e_xchg || ((*(b.start + locs->opcode_position) & 0xf0) == 0xb0) ) )
1512                             {
1513                                 unsigned int opcode_byte = *(b.start+locs->opcode_position);
1514                                 unsigned int reg_id = (opcode_byte & 0x07);
1515                                 if(locs->rex_b) 
1516                                 {
1517                                     // FP stack registers are not affected by the rex_b bit in AM_REG.
1518                                     if(r.regClass() == (unsigned) x86::GPR)
1519                                     {
1520                                         int reg_op_type = op_d;
1521                                         switch(size)
1522                                         {
1523                                         case 1:
1524                                             reg_op_type = op_b;
1525                                             break;
1526                                         case 2:
1527                                             reg_op_type = op_w;
1528                                             break;
1529                                         case 8:
1530                                             reg_op_type = op_q;
1531                                             break;
1532                                         default:
1533                                             break;
1534                                         }
1535
1536                                         r = makeRegisterID(reg_id, reg_op_type, true);
1537                                         assert(r.name() != "<INVALID_REG>");
1538                                     }
1539                             } else if((r.size() == 1) && (locs->rex_byte & 0x40))
1540                                 {
1541                                     r = makeRegisterID(reg_id, op_b, false);
1542                                     assert(r.name() != "<INVALID_REG>");
1543                                 }
1544                             }
1545
1546                         if(sizePrefixPresent && (r.regClass() == (unsigned int)x86::GPR) && r.size() >= 4)
1547                             {
1548                                 r = MachRegister((r.val() & ~x86::FULL) | x86::W_REG);
1549                                 assert(r.name() != "<INVALID_REG>");
1550                             }
1551                         }
1552                         Expression::Ptr op(makeRegisterExpression(r));
1553                         insn_to_complete->appendOperand(op, isRead, isWritten);
1554                     }
1555                     break;
1556                 case am_stackH:
1557                 case am_stackP:
1558                 // handled elsewhere
1559                     break;
1560                 case am_allgprs:
1561                     if(m_Arch == Arch_x86)
1562                     {
1563                         insn_to_complete->appendOperand(makeRegisterExpression(x86::eax), isRead, isWritten);
1564                         insn_to_complete->appendOperand(makeRegisterExpression(x86::ecx), isRead, isWritten);
1565                         insn_to_complete->appendOperand(makeRegisterExpression(x86::edx), isRead, isWritten);
1566                         insn_to_complete->appendOperand(makeRegisterExpression(x86::ebx), isRead, isWritten);
1567                         insn_to_complete->appendOperand(makeRegisterExpression(x86::esp), isRead, isWritten);
1568                         insn_to_complete->appendOperand(makeRegisterExpression(x86::ebp), isRead, isWritten);
1569                         insn_to_complete->appendOperand(makeRegisterExpression(x86::esi), isRead, isWritten);
1570                         insn_to_complete->appendOperand(makeRegisterExpression(x86::edi), isRead, isWritten);
1571                 } else {
1572                         insn_to_complete->appendOperand(makeRegisterExpression(x86_64::eax), isRead, isWritten);
1573                         insn_to_complete->appendOperand(makeRegisterExpression(x86_64::ecx), isRead, isWritten);
1574                         insn_to_complete->appendOperand(makeRegisterExpression(x86_64::edx), isRead, isWritten);
1575                         insn_to_complete->appendOperand(makeRegisterExpression(x86_64::ebx), isRead, isWritten);
1576                         insn_to_complete->appendOperand(makeRegisterExpression(x86_64::esp), isRead, isWritten);
1577                         insn_to_complete->appendOperand(makeRegisterExpression(x86_64::ebp), isRead, isWritten);
1578                         insn_to_complete->appendOperand(makeRegisterExpression(x86_64::esi), isRead, isWritten);
1579                         insn_to_complete->appendOperand(makeRegisterExpression(x86_64::edi), isRead, isWritten);
1580                     }
1581                     break;
1582             case am_ImplImm:
1583                   insn_to_complete->appendOperand(Immediate::makeImmediate(Result(makeSizeType(optype), 1)), isRead, isWritten);
1584                   break;
1585                 default:
1586                     printf("decodeOneOperand() called with unknown addressing method %d\n", operand.admet);
1587                     // assert(0);
1588                 return false;
1589         }
1590       
1591                 return true;
1592             }
1593
1594     extern ia32_entry invalid;
1595     void InstructionDecoder_x86::doIA32Decode(InstructionDecoder::buffer& b)
1596     {
1597         if(decodedInstruction == NULL)
1598         {
1599             decodedInstruction = reinterpret_cast<ia32_instruction*>(malloc(sizeof(ia32_instruction)));
1600             assert(decodedInstruction);
1601         }
1602         if(locs == NULL)
1603         {
1604             locs = reinterpret_cast<ia32_locations*>(malloc(sizeof(ia32_locations)));
1605             assert(locs);
1606         }
1607         locs = new(locs) ia32_locations; //reinit();
1608         assert(locs->sib_position == -1);
1609         decodedInstruction = new (decodedInstruction) ia32_instruction(NULL, NULL, locs);
1610         ia32_decode(IA32_DECODE_PREFIXES, b.start, *decodedInstruction);
1611         sizePrefixPresent = (decodedInstruction->getPrefix()->getOperSzPrefix() == 0x66);
1612         if (decodedInstruction->getPrefix()->rexW()) {
1613            // as per 2.2.1.2 - rex.w overrides 66h
1614            sizePrefixPresent = false;
1615         }
1616         addrSizePrefixPresent = (decodedInstruction->getPrefix()->getAddrSzPrefix() == 0x67);
1617         static ia32_entry invalid = { e_No_Entry, 0, 0, false, { {0,0}, {0,0}, {0,0} }, 0, 0 };
1618         if(decodedInstruction->getEntry()) {
1619             // check prefix validity
1620             // lock prefix only allowed on certain insns.
1621             // TODO: refine further to check memory written operand
1622             if(decodedInstruction->getPrefix()->getPrefix(0) == PREFIX_LOCK)
1623             {
1624                 switch(decodedInstruction->getEntry()->id)
1625                 {
1626                 case e_add:
1627                 case e_adc:
1628                 case e_and:
1629                 case e_btc:
1630                 case e_btr:
1631                 case e_bts:
1632                 case e_cmpxch:
1633                 case e_cmpxch8b:
1634                 case e_dec:
1635                 case e_inc:
1636                 case e_neg:
1637                 case e_not:
1638                 case e_or:
1639                 case e_sbb:
1640                 case e_sub:
1641                 case e_xor:
1642                 case e_xadd:
1643                 case e_xchg:
1644                     break;
1645                 default:
1646                     m_Operation =make_shared(singleton_object_pool<Operation>::construct(&invalid,
1647                                     decodedInstruction->getPrefix(), locs, m_Arch));
1648                     return;
1649                 }
1650             }
1651             m_Operation = make_shared(singleton_object_pool<Operation>::construct(decodedInstruction->getEntry(),
1652                                     decodedInstruction->getPrefix(), locs, m_Arch));
1653             
1654       } else {
1655                 // Gap parsing can trigger this case; in particular, when it encounters prefixes in an invalid order.
1656                 // Notably, if a REX prefix (0x40-0x48) appears followed by another prefix (0x66, 0x67, etc)
1657                 // we'll reject the instruction as invalid and send it back with no entry.  Since this is a common
1658                 // byte sequence to see in, for example, ASCII strings, we want to simply accept this and move on, not
1659                 // yell at the user.
1660             m_Operation = make_shared(singleton_object_pool<Operation>::construct(&invalid,
1661                                     decodedInstruction->getPrefix(), locs, m_Arch));
1662         }
1663
1664     }
1665     
1666     void InstructionDecoder_x86::decodeOpcode(InstructionDecoder::buffer& b)
1667     {
1668         doIA32Decode(b);
1669         b.start += decodedInstruction->getSize();
1670     }
1671     
1672         bool InstructionDecoder_x86::decodeOperands(const Instruction* insn_to_complete)
1673     {
1674        int imm_index = 0; // handle multiple immediate operands
1675         if(!decodedInstruction || !decodedInstruction->getEntry()) return false;
1676         unsigned int opsema = decodedInstruction->getEntry()->opsema & 0xFF;
1677         InstructionDecoder::buffer b(insn_to_complete->ptr(), insn_to_complete->size());
1678
1679         if (decodedInstruction->getEntry()->getID() == e_ret_near ||
1680             decodedInstruction->getEntry()->getID() == e_ret_far) {
1681            Expression::Ptr ret_addr = makeDereferenceExpression(makeRegisterExpression(ia32_is_mode_64() ? x86_64::rsp : x86::esp), 
1682                                                                 ia32_is_mode_64() ? u64 : u32);
1683            insn_to_complete->addSuccessor(ret_addr, false, true, false, false);
1684         }
1685
1686         for(int i = 0; i < 3; i++)
1687         {
1688             if(decodedInstruction->getEntry()->operands[i].admet == 0 && 
1689                decodedInstruction->getEntry()->operands[i].optype == 0)
1690                 return true;
1691             if(!decodeOneOperand(b,
1692                                  decodedInstruction->getEntry()->operands[i], 
1693                                  imm_index, 
1694                                  insn_to_complete, 
1695                                  readsOperand(opsema, i),
1696                                  writesOperand(opsema, i)))
1697             {
1698                 return false;
1699             }
1700         }
1701
1702         /* Does this instruction have a 4th operand? */
1703         if((decodedInstruction->getEntry()->opsema & 0xFFFF) >= s4OP)
1704         {
1705           if(!decodeOneOperand(b,
1706             {am_I, op_b}, /* This is always an IMM8 */
1707             imm_index,
1708             insn_to_complete,
1709             readsOperand(opsema, 3),
1710             writesOperand(opsema, 3)))
1711             {
1712                 return false;
1713             }
1714         }
1715     
1716         return true;
1717     }
1718
1719     
1720       INSTRUCTION_EXPORT Instruction::Ptr InstructionDecoder_x86::decode(InstructionDecoder::buffer& b)
1721     {
1722         return InstructionDecoderImpl::decode(b);
1723     }
1724     void InstructionDecoder_x86::doDelayedDecode(const Instruction* insn_to_complete)
1725     {
1726       InstructionDecoder::buffer b(insn_to_complete->ptr(), insn_to_complete->size());
1727       //insn_to_complete->m_Operands.reserve(4);
1728       doIA32Decode(b);        
1729       decodeOperands(insn_to_complete);
1730     }
1731     
1732 };
1733 };
1734