x86id.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. /*
  2. * x86 identifier recognition and instruction handling
  3. *
  4. * Copyright (C) 2002-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <ctype.h>
  28. #include <util.h>
  29. #include <libyasm.h>
  30. #include <libyasm/phash.h>
  31. #include "modules/arch/x86/x86arch.h"
  32. static const char *cpu_find_reverse(unsigned int cpu0, unsigned int cpu1,
  33. unsigned int cpu2);
  34. /* Opcode modifiers. */
  35. #define MOD_Gap 0 /* Eats a parameter / does nothing */
  36. #define MOD_PreAdd 1 /* Parameter adds to "special" prefix */
  37. #define MOD_Op0Add 2 /* Parameter adds to opcode byte 0 */
  38. #define MOD_Op1Add 3 /* Parameter adds to opcode byte 1 */
  39. #define MOD_Op2Add 4 /* Parameter adds to opcode byte 2 */
  40. #define MOD_SpAdd 5 /* Parameter adds to "spare" value */
  41. #define MOD_OpSizeR 6 /* Parameter replaces opersize */
  42. #define MOD_Imm8 7 /* Parameter is included as immediate byte */
  43. #define MOD_AdSizeR 8 /* Parameter replaces addrsize (jmp only) */
  44. #define MOD_DOpS64R 9 /* Parameter replaces default 64-bit opersize */
  45. #define MOD_Op1AddSp 10 /* Parameter is added as "spare" to opcode byte 2 */
  46. #define MOD_SetVEX 11 /* Parameter replaces internal VEX prefix value */
  47. /* GAS suffix flags for instructions */
  48. enum x86_gas_suffix_flags {
  49. SUF_Z = 1<<0, /* no suffix */
  50. SUF_B = 1<<1,
  51. SUF_W = 1<<2,
  52. SUF_L = 1<<3,
  53. SUF_Q = 1<<4,
  54. SUF_S = 1<<5,
  55. SUF_MASK = SUF_Z|SUF_B|SUF_W|SUF_L|SUF_Q|SUF_S,
  56. /* Flags only used in x86_insn_info */
  57. GAS_ONLY = 1<<6, /* Only available in GAS mode */
  58. GAS_ILLEGAL = 1<<7, /* Illegal in GAS mode */
  59. GAS_NO_REV = 1<<8 /* Don't reverse operands in GAS mode */
  60. };
  61. /* Miscellaneous flag tests for instructions */
  62. enum x86_misc_flags {
  63. /* These are tested against BITS==64. */
  64. ONLY_64 = 1<<0, /* Only available in 64-bit mode */
  65. NOT_64 = 1<<1, /* Not available (invalid) in 64-bit mode */
  66. /* These are tested against whether the base instruction is an AVX one. */
  67. ONLY_AVX = 1<<2, /* Only available in AVX instruction */
  68. NOT_AVX = 1<<3 /* Not available (invalid) in AVX instruction */
  69. };
  70. enum x86_operand_type {
  71. OPT_Imm = 0, /* immediate */
  72. OPT_Reg = 1, /* any general purpose or FPU register */
  73. OPT_Mem = 2, /* memory */
  74. OPT_RM = 3, /* any general purpose or FPU register OR memory */
  75. OPT_SIMDReg = 4, /* any MMX or XMM register */
  76. OPT_SIMDRM = 5, /* any MMX or XMM register OR memory */
  77. OPT_SegReg = 6, /* any segment register */
  78. OPT_CRReg = 7, /* any CR register */
  79. OPT_DRReg = 8, /* any DR register */
  80. OPT_TRReg = 9, /* any TR register */
  81. OPT_ST0 = 10, /* ST0 */
  82. OPT_Areg = 11, /* AL/AX/EAX/RAX (depending on size) */
  83. OPT_Creg = 12, /* CL/CX/ECX/RCX (depending on size) */
  84. OPT_Dreg = 13, /* DL/DX/EDX/RDX (depending on size) */
  85. OPT_CS = 14, /* CS */
  86. OPT_DS = 15, /* DS */
  87. OPT_ES = 16, /* ES */
  88. OPT_FS = 17, /* FS */
  89. OPT_GS = 18, /* GS */
  90. OPT_SS = 19, /* SS */
  91. OPT_CR4 = 20, /* CR4 */
  92. /* memory offset (an EA, but with no registers allowed)
  93. * [special case for MOV opcode]
  94. */
  95. OPT_MemOffs = 21,
  96. OPT_Imm1 = 22, /* immediate, value=1 (for special-case shift) */
  97. /* immediate, does not contain SEG:OFF (for jmp/call) */
  98. OPT_ImmNotSegOff = 23,
  99. OPT_XMM0 = 24, /* XMM0 */
  100. /* AX/EAX/RAX memory operand only (EA) [special case for SVM opcodes]
  101. */
  102. OPT_MemrAX = 25,
  103. /* EAX memory operand only (EA) [special case for SVM skinit opcode] */
  104. OPT_MemEAX = 26,
  105. /* XMM VSIB memory operand */
  106. OPT_MemXMMIndex = 27,
  107. /* YMM VSIB memory operand */
  108. OPT_MemYMMIndex = 28
  109. };
  110. enum x86_operand_size {
  111. /* any size acceptable/no size spec acceptable (dep. on strict) */
  112. OPS_Any = 0,
  113. /* 8/16/32/64/80/128/256 bits (from user or reg size) */
  114. OPS_8 = 1,
  115. OPS_16 = 2,
  116. OPS_32 = 3,
  117. OPS_64 = 4,
  118. OPS_80 = 5,
  119. OPS_128 = 6,
  120. OPS_256 = 7,
  121. /* current BITS setting; when this is used the size matched
  122. * gets stored into the opersize as well.
  123. */
  124. OPS_BITS = 8
  125. };
  126. enum x86_operand_targetmod {
  127. OPTM_None = 0, /* no target mod acceptable */
  128. OPTM_Near = 1, /* NEAR */
  129. OPTM_Short = 2, /* SHORT */
  130. OPTM_Far = 3, /* FAR (or SEG:OFF immediate) */
  131. OPTM_To = 4 /* TO */
  132. };
  133. enum x86_operand_action {
  134. OPA_None = 0, /* does nothing (operand data is discarded) */
  135. OPA_EA = 1, /* operand data goes into ea field */
  136. OPA_Imm = 2, /* operand data goes into imm field */
  137. OPA_SImm = 3, /* operand data goes into sign-extended imm field */
  138. OPA_Spare = 4, /* operand data goes into "spare" field */
  139. OPA_Op0Add = 5, /* operand data is added to opcode byte 0 */
  140. OPA_Op1Add = 6, /* operand data is added to opcode byte 1 */
  141. /* operand data goes into BOTH ea and spare
  142. * (special case for imul opcode)
  143. */
  144. OPA_SpareEA = 7,
  145. /* relative jump (outputs a jmp instead of normal insn) */
  146. OPA_JmpRel = 8,
  147. /* operand size goes into address size (jmp only) */
  148. OPA_AdSizeR = 9,
  149. /* far jump (outputs a farjmp instead of normal insn) */
  150. OPA_JmpFar = 10,
  151. /* ea operand only sets address size (no actual ea field) */
  152. OPA_AdSizeEA = 11,
  153. OPA_VEX = 12, /* operand data goes into VEX/XOP "vvvv" field */
  154. /* operand data goes into BOTH VEX/XOP "vvvv" field and ea field */
  155. OPA_EAVEX = 13,
  156. /* operand data goes into BOTH VEX/XOP "vvvv" field and spare field */
  157. OPA_SpareVEX = 14,
  158. /* operand data goes into upper 4 bits of immediate byte (VEX is4 field) */
  159. OPA_VEXImmSrc = 15,
  160. /* operand data goes into bottom 4 bits of immediate byte
  161. * (currently only VEX imz2 field)
  162. */
  163. OPA_VEXImm = 16
  164. };
  165. enum x86_operand_post_action {
  166. OPAP_None = 0,
  167. /* sign-extended imm8 that could expand to a large imm16/32 */
  168. OPAP_SImm8 = 1,
  169. /* could become a short opcode mov with bits=64 and a32 prefix */
  170. OPAP_ShortMov = 2,
  171. /* forced 16-bit address size (override ignored, no prefix) */
  172. OPAP_A16 = 3,
  173. /* large imm64 that can become a sign-extended imm32 */
  174. OPAP_SImm32Avail = 4
  175. };
  176. typedef struct x86_info_operand {
  177. /* Operand types. These are more detailed than the "general" types for all
  178. * architectures, as they include the size, for instance.
  179. */
  180. /* general type (must be exact match, except for RM types): */
  181. unsigned int type:5;
  182. /* size (user-specified, or from register size) */
  183. unsigned int size:4;
  184. /* size implicit or explicit ("strictness" of size matching on
  185. * non-registers -- registers are always strictly matched):
  186. * 0 = user size must exactly match size above.
  187. * 1 = user size either unspecified or exactly match size above.
  188. */
  189. unsigned int relaxed:1;
  190. /* effective address size
  191. * 0 = any address size allowed except for 64-bit
  192. * 1 = only 64-bit address size allowed
  193. */
  194. unsigned int eas64:1;
  195. /* target modification */
  196. unsigned int targetmod:3;
  197. /* Actions: what to do with the operand if the instruction matches.
  198. * Essentially describes what part of the output bytecode gets the
  199. * operand. This may require conversion (e.g. a register going into
  200. * an ea field). Naturally, only one of each of these may be contained
  201. * in the operands of a single insn_info structure.
  202. */
  203. unsigned int action:5;
  204. /* Postponed actions: actions which can't be completed at
  205. * parse-time due to possibly dependent expressions. For these, some
  206. * additional data (stored in the second byte of the opcode with a
  207. * one-byte opcode) is passed to later stages of the assembler with
  208. * flags set to indicate postponed actions.
  209. */
  210. unsigned int post_action:3;
  211. } x86_info_operand;
  212. typedef struct x86_insn_info {
  213. /* GAS suffix flags */
  214. unsigned int gas_flags:9; /* Enabled for these GAS suffixes */
  215. /* Tests against BITS==64, AVX, and XOP */
  216. unsigned int misc_flags:5;
  217. /* The CPU feature flags needed to execute this instruction. This is OR'ed
  218. * with arch-specific data[2]. This combined value is compared with
  219. * cpu_enabled to see if all bits set here are set in cpu_enabled--if so,
  220. * the instruction is available on this CPU.
  221. */
  222. unsigned int cpu0:6;
  223. unsigned int cpu1:6;
  224. unsigned int cpu2:6;
  225. /* Opcode modifiers for variations of instruction. As each modifier reads
  226. * its parameter in LSB->MSB order from the arch-specific data[1] from the
  227. * lexer data, and the LSB of the arch-specific data[1] is reserved for the
  228. * count of insn_info structures in the instruction grouping, there can
  229. * only be a maximum of 3 modifiers.
  230. */
  231. unsigned char modifiers[3];
  232. /* Operand Size */
  233. unsigned char opersize;
  234. /* Default operand size in 64-bit mode (0 = 32-bit for readability). */
  235. unsigned char def_opersize_64;
  236. /* A special instruction prefix, used for some of the Intel SSE and SSE2
  237. * instructions. Intel calls these 3-byte opcodes, but in AMD64's 64-bit
  238. * mode, they're treated like normal prefixes (e.g. the REX prefix needs
  239. * to be *after* the F2/F3/66 "prefix").
  240. * (0=no special prefix)
  241. * 0xC0 - 0xCF indicate a VEX prefix, with the four LSBs holding "WLpp":
  242. * W: VEX.W field (meaning depends on opcode)
  243. * L: 0=128-bit, 1=256-bit
  244. * pp: SIMD prefix designation:
  245. * 00: None
  246. * 01: 66
  247. * 10: F3
  248. * 11: F2
  249. * 0x80 - 0x8F indicate a XOP prefix, with the four LSBs holding "WLpp":
  250. * same meanings as VEX prefix.
  251. */
  252. unsigned char special_prefix;
  253. /* The length of the basic opcode */
  254. unsigned char opcode_len;
  255. /* The basic 1-3 byte opcode (not including the special instruction
  256. * prefix).
  257. */
  258. unsigned char opcode[3];
  259. /* The 3-bit "spare" value (extended opcode) for the R/M byte field */
  260. unsigned char spare;
  261. /* The number of operands this form of the instruction takes */
  262. unsigned int num_operands:4;
  263. /* The index into the insn_operands array which contains the type of each
  264. * operand, see above
  265. */
  266. unsigned int operands_index:12;
  267. } x86_insn_info;
  268. typedef struct x86_id_insn {
  269. yasm_insn insn; /* base structure */
  270. /* instruction parse group - NULL if empty instruction (just prefixes) */
  271. /*@null@*/ const x86_insn_info *group;
  272. /* CPU feature flags enabled at the time of parsing the instruction */
  273. wordptr cpu_enabled;
  274. /* Modifier data */
  275. unsigned char mod_data[3];
  276. /* Number of elements in the instruction parse group */
  277. unsigned int num_info:8;
  278. /* BITS setting active at the time of parsing the instruction */
  279. unsigned int mode_bits:8;
  280. /* Suffix flags */
  281. unsigned int suffix:9;
  282. /* Tests against BITS==64 and AVX */
  283. unsigned int misc_flags:5;
  284. /* Parser enabled at the time of parsing the instruction */
  285. unsigned int parser:2;
  286. /* Strict forced setting at the time of parsing the instruction */
  287. unsigned int force_strict:1;
  288. /* Default rel setting at the time of parsing the instruction */
  289. unsigned int default_rel:1;
  290. } x86_id_insn;
  291. static void x86_id_insn_destroy(void *contents);
  292. static void x86_id_insn_print(const void *contents, FILE *f, int indent_level);
  293. static void x86_id_insn_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
  294. static const yasm_bytecode_callback x86_id_insn_callback = {
  295. x86_id_insn_destroy,
  296. x86_id_insn_print,
  297. x86_id_insn_finalize,
  298. NULL,
  299. yasm_bc_calc_len_common,
  300. yasm_bc_expand_common,
  301. yasm_bc_tobytes_common,
  302. YASM_BC_SPECIAL_INSN
  303. };
  304. #include "x86insns.c"
  305. /* Looks for the first SIMD register match for the purposes of VSIB matching.
  306. * Full legality checking is performed in EA code.
  307. */
  308. static int
  309. x86_expr_contains_simd_cb(const yasm_expr__item *ei, void *d)
  310. {
  311. int ymm = *((int *)d);
  312. if (ei->type != YASM_EXPR_REG)
  313. return 0;
  314. switch ((x86_expritem_reg_size)(ei->data.reg & ~0xFUL)) {
  315. case X86_XMMREG:
  316. if (!ymm)
  317. return 1;
  318. break;
  319. case X86_YMMREG:
  320. if (ymm)
  321. return 1;
  322. break;
  323. default:
  324. break;
  325. }
  326. return 0;
  327. }
  328. static int
  329. x86_expr_contains_simd(const yasm_expr *e, int ymm)
  330. {
  331. return yasm_expr__traverse_leaves_in_const(e, &ymm,
  332. x86_expr_contains_simd_cb);
  333. }
  334. static void
  335. x86_finalize_common(x86_common *common, const x86_insn_info *info,
  336. unsigned int mode_bits)
  337. {
  338. common->addrsize = 0;
  339. common->opersize = info->opersize;
  340. common->lockrep_pre = 0;
  341. common->acqrel_pre = 0;
  342. common->mode_bits = (unsigned char)mode_bits;
  343. }
  344. static void
  345. x86_finalize_opcode(x86_opcode *opcode, const x86_insn_info *info)
  346. {
  347. opcode->len = info->opcode_len;
  348. opcode->opcode[0] = info->opcode[0];
  349. opcode->opcode[1] = info->opcode[1];
  350. opcode->opcode[2] = info->opcode[2];
  351. }
  352. /* Clear operands so they don't get destroyed after we've copied references. */
  353. static void
  354. x86_id_insn_clear_operands(x86_id_insn *id_insn)
  355. {
  356. yasm_insn_operand *op = yasm_insn_ops_first(&id_insn->insn);
  357. while (op) {
  358. op->type = YASM_INSN__OPERAND_REG;
  359. op = yasm_insn_op_next(op);
  360. }
  361. }
  362. static void
  363. x86_finalize_jmpfar(yasm_bytecode *bc, yasm_bytecode *prev_bc,
  364. const x86_insn_info *info)
  365. {
  366. x86_id_insn *id_insn = (x86_id_insn *)bc->contents;
  367. unsigned char *mod_data = id_insn->mod_data;
  368. unsigned int mode_bits = id_insn->mode_bits;
  369. x86_jmpfar *jmpfar;
  370. yasm_insn_operand *op;
  371. unsigned int i;
  372. jmpfar = yasm_xmalloc(sizeof(x86_jmpfar));
  373. x86_finalize_common(&jmpfar->common, info, mode_bits);
  374. x86_finalize_opcode(&jmpfar->opcode, info);
  375. op = yasm_insn_ops_first(&id_insn->insn);
  376. if (op->type == YASM_INSN__OPERAND_IMM && op->seg) {
  377. /* SEG:OFF */
  378. if (yasm_value_finalize_expr(&jmpfar->segment, op->seg, prev_bc, 16))
  379. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  380. N_("jump target segment too complex"));
  381. if (yasm_value_finalize_expr(&jmpfar->offset, op->data.val, prev_bc,
  382. 0))
  383. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  384. N_("jump target offset too complex"));
  385. } else if (op->targetmod == X86_FAR) {
  386. /* "FAR imm" target needs to become "seg imm:imm". */
  387. yasm_expr *e = yasm_expr_create_branch(YASM_EXPR_SEG,
  388. yasm_expr_copy(op->data.val),
  389. op->data.val->line);
  390. if (yasm_value_finalize_expr(&jmpfar->offset, op->data.val, prev_bc, 0)
  391. || yasm_value_finalize_expr(&jmpfar->segment, e, prev_bc, 16))
  392. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  393. N_("jump target expression too complex"));
  394. } else if (yasm_insn_op_next(op)) {
  395. /* Two operand form (gas) */
  396. yasm_insn_operand *op2 = yasm_insn_op_next(op);
  397. if (yasm_value_finalize_expr(&jmpfar->segment, op->data.val, prev_bc,
  398. 16))
  399. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  400. N_("jump target segment too complex"));
  401. if (yasm_value_finalize_expr(&jmpfar->offset, op2->data.val, prev_bc,
  402. 0))
  403. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  404. N_("jump target offset too complex"));
  405. if (op2->size == OPS_BITS)
  406. jmpfar->common.opersize = (unsigned char)mode_bits;
  407. } else
  408. yasm_internal_error(N_("didn't get FAR expression in jmpfar"));
  409. /* Apply modifiers */
  410. for (i=0; i<NELEMS(info->modifiers); i++) {
  411. switch (info->modifiers[i]) {
  412. case MOD_Gap:
  413. break;
  414. case MOD_Op0Add:
  415. jmpfar->opcode.opcode[0] += mod_data[i];
  416. break;
  417. case MOD_Op1Add:
  418. jmpfar->opcode.opcode[1] += mod_data[i];
  419. break;
  420. case MOD_Op2Add:
  421. jmpfar->opcode.opcode[2] += mod_data[i];
  422. break;
  423. case MOD_Op1AddSp:
  424. jmpfar->opcode.opcode[1] += mod_data[i]<<3;
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. yasm_x86__bc_apply_prefixes((x86_common *)jmpfar, NULL,
  431. info->def_opersize_64,
  432. id_insn->insn.num_prefixes,
  433. id_insn->insn.prefixes);
  434. x86_id_insn_clear_operands(id_insn);
  435. /* Transform the bytecode */
  436. yasm_x86__bc_transform_jmpfar(bc, jmpfar);
  437. }
  438. static void
  439. x86_finalize_jmp(yasm_bytecode *bc, yasm_bytecode *prev_bc,
  440. const x86_insn_info *jinfo)
  441. {
  442. x86_id_insn *id_insn = (x86_id_insn *)bc->contents;
  443. x86_jmp *jmp;
  444. int num_info = id_insn->num_info;
  445. const x86_insn_info *info = id_insn->group;
  446. unsigned char *mod_data = id_insn->mod_data;
  447. unsigned int mode_bits = id_insn->mode_bits;
  448. /*unsigned char suffix = id_insn->suffix;*/
  449. yasm_insn_operand *op;
  450. static const unsigned char size_lookup[] =
  451. {0, 8, 16, 32, 64, 80, 128, 0, 0}; /* 256 not needed */
  452. unsigned int i;
  453. /* We know the target is in operand 0, but sanity check for Imm. */
  454. op = yasm_insn_ops_first(&id_insn->insn);
  455. if (op->type != YASM_INSN__OPERAND_IMM)
  456. yasm_internal_error(N_("invalid operand conversion"));
  457. jmp = yasm_xmalloc(sizeof(x86_jmp));
  458. x86_finalize_common(&jmp->common, jinfo, mode_bits);
  459. if (yasm_value_finalize_expr(&jmp->target, op->data.val, prev_bc, 0))
  460. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  461. N_("jump target expression too complex"));
  462. if (jmp->target.seg_of || jmp->target.rshift || jmp->target.curpos_rel)
  463. yasm_error_set(YASM_ERROR_VALUE, N_("invalid jump target"));
  464. yasm_value_set_curpos_rel(&jmp->target, bc, 0);
  465. jmp->target.jump_target = 1;
  466. /* See if the user explicitly specified short/near/far. */
  467. switch (insn_operands[jinfo->operands_index+0].targetmod) {
  468. case OPTM_Short:
  469. jmp->op_sel = JMP_SHORT_FORCED;
  470. break;
  471. case OPTM_Near:
  472. jmp->op_sel = JMP_NEAR_FORCED;
  473. break;
  474. default:
  475. jmp->op_sel = JMP_NONE;
  476. }
  477. /* Check for address size setting in second operand, if present */
  478. if (jinfo->num_operands > 1 &&
  479. insn_operands[jinfo->operands_index+1].action == OPA_AdSizeR)
  480. jmp->common.addrsize = (unsigned char)
  481. size_lookup[insn_operands[jinfo->operands_index+1].size];
  482. /* Check for address size override */
  483. for (i=0; i<NELEMS(jinfo->modifiers); i++) {
  484. if (jinfo->modifiers[i] == MOD_AdSizeR)
  485. jmp->common.addrsize = mod_data[i];
  486. }
  487. /* Scan through other infos for this insn looking for short/near versions.
  488. * Needs to match opersize and number of operands, also be within CPU.
  489. */
  490. jmp->shortop.len = 0;
  491. jmp->nearop.len = 0;
  492. for (; num_info>0 && (jmp->shortop.len == 0 || jmp->nearop.len == 0);
  493. num_info--, info++) {
  494. /* Match CPU */
  495. if (mode_bits != 64 && (info->misc_flags & ONLY_64))
  496. continue;
  497. if (mode_bits == 64 && (info->misc_flags & NOT_64))
  498. continue;
  499. if (!BitVector_bit_test(id_insn->cpu_enabled, info->cpu0) ||
  500. !BitVector_bit_test(id_insn->cpu_enabled, info->cpu1) ||
  501. !BitVector_bit_test(id_insn->cpu_enabled, info->cpu2))
  502. continue;
  503. if (info->num_operands == 0)
  504. continue;
  505. if (insn_operands[info->operands_index+0].action != OPA_JmpRel)
  506. continue;
  507. if (info->opersize != jmp->common.opersize)
  508. continue;
  509. switch (insn_operands[info->operands_index+0].targetmod) {
  510. case OPTM_Short:
  511. x86_finalize_opcode(&jmp->shortop, info);
  512. for (i=0; i<NELEMS(info->modifiers); i++) {
  513. if (info->modifiers[i] == MOD_Op0Add)
  514. jmp->shortop.opcode[0] += mod_data[i];
  515. }
  516. break;
  517. case OPTM_Near:
  518. x86_finalize_opcode(&jmp->nearop, info);
  519. for (i=0; i<NELEMS(info->modifiers); i++) {
  520. if (info->modifiers[i] == MOD_Op1Add)
  521. jmp->nearop.opcode[1] += mod_data[i];
  522. }
  523. break;
  524. }
  525. }
  526. if ((jmp->op_sel == JMP_SHORT_FORCED) && (jmp->shortop.len == 0))
  527. yasm_error_set(YASM_ERROR_TYPE,
  528. N_("no SHORT form of that jump instruction exists"));
  529. if ((jmp->op_sel == JMP_NEAR_FORCED) && (jmp->nearop.len == 0))
  530. yasm_error_set(YASM_ERROR_TYPE,
  531. N_("no NEAR form of that jump instruction exists"));
  532. if (jmp->op_sel == JMP_NONE) {
  533. if (jmp->nearop.len == 0)
  534. jmp->op_sel = JMP_SHORT_FORCED;
  535. if (jmp->shortop.len == 0)
  536. jmp->op_sel = JMP_NEAR_FORCED;
  537. }
  538. yasm_x86__bc_apply_prefixes((x86_common *)jmp, NULL,
  539. jinfo->def_opersize_64,
  540. id_insn->insn.num_prefixes,
  541. id_insn->insn.prefixes);
  542. x86_id_insn_clear_operands(id_insn);
  543. /* Transform the bytecode */
  544. yasm_x86__bc_transform_jmp(bc, jmp);
  545. }
  546. static const x86_insn_info *
  547. x86_find_match(x86_id_insn *id_insn, yasm_insn_operand **ops,
  548. yasm_insn_operand **rev_ops, const unsigned int *size_lookup,
  549. int bypass)
  550. {
  551. const x86_insn_info *info = id_insn->group;
  552. unsigned int num_info = id_insn->num_info;
  553. unsigned int suffix = id_insn->suffix;
  554. unsigned int mode_bits = id_insn->mode_bits;
  555. int found = 0;
  556. /* Just do a simple linear search through the info array for a match.
  557. * First match wins.
  558. */
  559. for (; num_info>0 && !found; num_info--, info++) {
  560. yasm_insn_operand *op, **use_ops;
  561. const x86_info_operand *info_ops =
  562. &insn_operands[info->operands_index];
  563. unsigned int gas_flags = info->gas_flags;
  564. unsigned int misc_flags = info->misc_flags;
  565. unsigned int size;
  566. int mismatch = 0;
  567. unsigned int i;
  568. /* Match CPU */
  569. if (mode_bits != 64 && (misc_flags & ONLY_64))
  570. continue;
  571. if (mode_bits == 64 && (misc_flags & NOT_64))
  572. continue;
  573. if (bypass != 8 &&
  574. (!BitVector_bit_test(id_insn->cpu_enabled, info->cpu0) ||
  575. !BitVector_bit_test(id_insn->cpu_enabled, info->cpu1) ||
  576. !BitVector_bit_test(id_insn->cpu_enabled, info->cpu2)))
  577. continue;
  578. /* Match # of operands */
  579. if (id_insn->insn.num_operands != info->num_operands)
  580. continue;
  581. /* Match AVX */
  582. if (!(id_insn->misc_flags & ONLY_AVX) && (misc_flags & ONLY_AVX))
  583. continue;
  584. if ((id_insn->misc_flags & ONLY_AVX) && (misc_flags & NOT_AVX))
  585. continue;
  586. /* Match parser mode */
  587. if ((gas_flags & GAS_ONLY) && id_insn->parser != X86_PARSER_GAS)
  588. continue;
  589. if ((gas_flags & GAS_ILLEGAL) && id_insn->parser == X86_PARSER_GAS)
  590. continue;
  591. /* Match suffix (if required) */
  592. if (id_insn->parser == X86_PARSER_GAS
  593. && ((suffix & SUF_MASK) & (gas_flags & SUF_MASK)) == 0)
  594. continue;
  595. /* Use reversed operands in GAS mode if not otherwise specified */
  596. use_ops = ops;
  597. if (id_insn->parser == X86_PARSER_GAS && !(gas_flags & GAS_NO_REV))
  598. use_ops = rev_ops;
  599. if (id_insn->insn.num_operands == 0) {
  600. found = 1; /* no operands -> must have a match here. */
  601. break;
  602. }
  603. /* Match each operand type and size */
  604. for (i = 0, op = use_ops[0]; op && i<info->num_operands && !mismatch;
  605. op = use_ops[++i]) {
  606. /* Check operand type */
  607. switch (info_ops[i].type) {
  608. case OPT_Imm:
  609. if (op->type != YASM_INSN__OPERAND_IMM)
  610. mismatch = 1;
  611. break;
  612. case OPT_RM:
  613. if (op->type == YASM_INSN__OPERAND_MEMORY)
  614. break;
  615. /*@fallthrough@*/
  616. case OPT_Reg:
  617. if (op->type != YASM_INSN__OPERAND_REG)
  618. mismatch = 1;
  619. else {
  620. switch ((x86_expritem_reg_size)(op->data.reg&~0xFUL)) {
  621. case X86_REG8:
  622. case X86_REG8X:
  623. case X86_REG16:
  624. case X86_REG32:
  625. case X86_REG64:
  626. case X86_FPUREG:
  627. break;
  628. default:
  629. mismatch = 1;
  630. break;
  631. }
  632. }
  633. break;
  634. case OPT_Mem:
  635. if (op->type != YASM_INSN__OPERAND_MEMORY)
  636. mismatch = 1;
  637. break;
  638. case OPT_SIMDRM:
  639. if (op->type == YASM_INSN__OPERAND_MEMORY)
  640. break;
  641. /*@fallthrough@*/
  642. case OPT_SIMDReg:
  643. if (op->type != YASM_INSN__OPERAND_REG)
  644. mismatch = 1;
  645. else {
  646. switch ((x86_expritem_reg_size)(op->data.reg&~0xFUL)) {
  647. case X86_MMXREG:
  648. case X86_XMMREG:
  649. case X86_YMMREG:
  650. break;
  651. default:
  652. mismatch = 1;
  653. break;
  654. }
  655. }
  656. break;
  657. case OPT_SegReg:
  658. if (op->type != YASM_INSN__OPERAND_SEGREG)
  659. mismatch = 1;
  660. break;
  661. case OPT_CRReg:
  662. if (op->type != YASM_INSN__OPERAND_REG ||
  663. (op->data.reg & ~0xFUL) != X86_CRREG)
  664. mismatch = 1;
  665. break;
  666. case OPT_DRReg:
  667. if (op->type != YASM_INSN__OPERAND_REG ||
  668. (op->data.reg & ~0xFUL) != X86_DRREG)
  669. mismatch = 1;
  670. break;
  671. case OPT_TRReg:
  672. if (op->type != YASM_INSN__OPERAND_REG ||
  673. (op->data.reg & ~0xFUL) != X86_TRREG)
  674. mismatch = 1;
  675. break;
  676. case OPT_ST0:
  677. if (op->type != YASM_INSN__OPERAND_REG ||
  678. op->data.reg != X86_FPUREG)
  679. mismatch = 1;
  680. break;
  681. case OPT_Areg:
  682. if (op->type != YASM_INSN__OPERAND_REG ||
  683. (info_ops[i].size == OPS_8 &&
  684. op->data.reg != (X86_REG8 | 0) &&
  685. op->data.reg != (X86_REG8X | 0)) ||
  686. (info_ops[i].size == OPS_16 &&
  687. op->data.reg != (X86_REG16 | 0)) ||
  688. (info_ops[i].size == OPS_32 &&
  689. op->data.reg != (X86_REG32 | 0)) ||
  690. (info_ops[i].size == OPS_64 &&
  691. op->data.reg != (X86_REG64 | 0)))
  692. mismatch = 1;
  693. break;
  694. case OPT_Creg:
  695. if (op->type != YASM_INSN__OPERAND_REG ||
  696. (info_ops[i].size == OPS_8 &&
  697. op->data.reg != (X86_REG8 | 1) &&
  698. op->data.reg != (X86_REG8X | 1)) ||
  699. (info_ops[i].size == OPS_16 &&
  700. op->data.reg != (X86_REG16 | 1)) ||
  701. (info_ops[i].size == OPS_32 &&
  702. op->data.reg != (X86_REG32 | 1)) ||
  703. (info_ops[i].size == OPS_64 &&
  704. op->data.reg != (X86_REG64 | 1)))
  705. mismatch = 1;
  706. break;
  707. case OPT_Dreg:
  708. if (op->type != YASM_INSN__OPERAND_REG ||
  709. (info_ops[i].size == OPS_8 &&
  710. op->data.reg != (X86_REG8 | 2) &&
  711. op->data.reg != (X86_REG8X | 2)) ||
  712. (info_ops[i].size == OPS_16 &&
  713. op->data.reg != (X86_REG16 | 2)) ||
  714. (info_ops[i].size == OPS_32 &&
  715. op->data.reg != (X86_REG32 | 2)) ||
  716. (info_ops[i].size == OPS_64 &&
  717. op->data.reg != (X86_REG64 | 2)))
  718. mismatch = 1;
  719. break;
  720. case OPT_CS:
  721. if (op->type != YASM_INSN__OPERAND_SEGREG ||
  722. (op->data.reg & 0xF) != 1)
  723. mismatch = 1;
  724. break;
  725. case OPT_DS:
  726. if (op->type != YASM_INSN__OPERAND_SEGREG ||
  727. (op->data.reg & 0xF) != 3)
  728. mismatch = 1;
  729. break;
  730. case OPT_ES:
  731. if (op->type != YASM_INSN__OPERAND_SEGREG ||
  732. (op->data.reg & 0xF) != 0)
  733. mismatch = 1;
  734. break;
  735. case OPT_FS:
  736. if (op->type != YASM_INSN__OPERAND_SEGREG ||
  737. (op->data.reg & 0xF) != 4)
  738. mismatch = 1;
  739. break;
  740. case OPT_GS:
  741. if (op->type != YASM_INSN__OPERAND_SEGREG ||
  742. (op->data.reg & 0xF) != 5)
  743. mismatch = 1;
  744. break;
  745. case OPT_SS:
  746. if (op->type != YASM_INSN__OPERAND_SEGREG ||
  747. (op->data.reg & 0xF) != 2)
  748. mismatch = 1;
  749. break;
  750. case OPT_CR4:
  751. if (op->type != YASM_INSN__OPERAND_REG ||
  752. op->data.reg != (X86_CRREG | 4))
  753. mismatch = 1;
  754. break;
  755. case OPT_MemOffs:
  756. if (op->type != YASM_INSN__OPERAND_MEMORY ||
  757. yasm_expr__contains(op->data.ea->disp.abs,
  758. YASM_EXPR_REG) ||
  759. op->data.ea->pc_rel ||
  760. (!op->data.ea->not_pc_rel && id_insn->default_rel &&
  761. op->data.ea->disp.size != 64))
  762. mismatch = 1;
  763. break;
  764. case OPT_Imm1:
  765. if (op->type == YASM_INSN__OPERAND_IMM) {
  766. const yasm_intnum *num;
  767. num = yasm_expr_get_intnum(&op->data.val, 0);
  768. if (!num || !yasm_intnum_is_pos1(num))
  769. mismatch = 1;
  770. } else
  771. mismatch = 1;
  772. break;
  773. case OPT_ImmNotSegOff:
  774. if (op->type != YASM_INSN__OPERAND_IMM ||
  775. op->targetmod != 0 || op->seg)
  776. mismatch = 1;
  777. break;
  778. case OPT_XMM0:
  779. if (op->type != YASM_INSN__OPERAND_REG ||
  780. op->data.reg != X86_XMMREG)
  781. mismatch = 1;
  782. break;
  783. case OPT_MemrAX: {
  784. const uintptr_t *regp;
  785. if (op->type != YASM_INSN__OPERAND_MEMORY ||
  786. !(regp = yasm_expr_get_reg(&op->data.ea->disp.abs, 0)) ||
  787. (*regp != (X86_REG16 | 0) &&
  788. *regp != (X86_REG32 | 0) &&
  789. *regp != (X86_REG64 | 0)))
  790. mismatch = 1;
  791. break;
  792. }
  793. case OPT_MemEAX: {
  794. const uintptr_t *regp;
  795. if (op->type != YASM_INSN__OPERAND_MEMORY ||
  796. !(regp = yasm_expr_get_reg(&op->data.ea->disp.abs, 0)) ||
  797. *regp != (X86_REG32 | 0))
  798. mismatch = 1;
  799. break;
  800. }
  801. case OPT_MemXMMIndex:
  802. if (op->type != YASM_INSN__OPERAND_MEMORY ||
  803. !x86_expr_contains_simd(op->data.ea->disp.abs, 0))
  804. mismatch = 1;
  805. break;
  806. case OPT_MemYMMIndex:
  807. if (op->type != YASM_INSN__OPERAND_MEMORY ||
  808. !x86_expr_contains_simd(op->data.ea->disp.abs, 1))
  809. mismatch = 1;
  810. break;
  811. default:
  812. yasm_internal_error(N_("invalid operand type"));
  813. }
  814. if (mismatch)
  815. break;
  816. /* Check operand size */
  817. size = size_lookup[info_ops[i].size];
  818. if (id_insn->parser == X86_PARSER_GAS) {
  819. /* Require relaxed operands for GAS mode (don't allow
  820. * per-operand sizing).
  821. */
  822. if (op->type == YASM_INSN__OPERAND_REG && op->size == 0) {
  823. /* Register size must exactly match */
  824. if (yasm_x86__get_reg_size(op->data.reg) != size)
  825. mismatch = 1;
  826. } else if ((info_ops[i].type == OPT_Imm
  827. || info_ops[i].type == OPT_ImmNotSegOff
  828. || info_ops[i].type == OPT_Imm1)
  829. && !info_ops[i].relaxed
  830. && info_ops[i].action != OPA_JmpRel)
  831. mismatch = 1;
  832. } else {
  833. if (op->type == YASM_INSN__OPERAND_REG && op->size == 0) {
  834. /* Register size must exactly match */
  835. if ((bypass == 4 && i == 0) || (bypass == 5 && i == 1)
  836. || (bypass == 6 && i == 2))
  837. ;
  838. else if (yasm_x86__get_reg_size(op->data.reg) != size)
  839. mismatch = 1;
  840. } else {
  841. if ((bypass == 1 && i == 0) || (bypass == 2 && i == 1)
  842. || (bypass == 3 && i == 2))
  843. ;
  844. else if (info_ops[i].relaxed) {
  845. /* Relaxed checking */
  846. if (size != 0 && op->size != size && op->size != 0)
  847. mismatch = 1;
  848. } else {
  849. /* Strict checking */
  850. if (op->size != size)
  851. mismatch = 1;
  852. }
  853. }
  854. }
  855. if (mismatch)
  856. break;
  857. /* Check for 64-bit effective address size in NASM mode */
  858. if (id_insn->parser != X86_PARSER_GAS &&
  859. op->type == YASM_INSN__OPERAND_MEMORY) {
  860. if (info_ops[i].eas64) {
  861. if (op->data.ea->disp.size != 64)
  862. mismatch = 1;
  863. } else if (op->data.ea->disp.size == 64)
  864. mismatch = 1;
  865. }
  866. if (mismatch)
  867. break;
  868. /* Check target modifier */
  869. switch (info_ops[i].targetmod) {
  870. case OPTM_None:
  871. if (op->targetmod != 0)
  872. mismatch = 1;
  873. break;
  874. case OPTM_Near:
  875. if (op->targetmod != X86_NEAR)
  876. mismatch = 1;
  877. break;
  878. case OPTM_Short:
  879. if (op->targetmod != X86_SHORT)
  880. mismatch = 1;
  881. break;
  882. case OPTM_Far:
  883. if (op->targetmod != X86_FAR)
  884. mismatch = 1;
  885. break;
  886. case OPTM_To:
  887. if (op->targetmod != X86_TO)
  888. mismatch = 1;
  889. break;
  890. default:
  891. yasm_internal_error(N_("invalid target modifier type"));
  892. }
  893. }
  894. if (!mismatch) {
  895. found = 1;
  896. break;
  897. }
  898. }
  899. if (!found)
  900. return NULL;
  901. return info;
  902. }
  903. static void
  904. x86_match_error(x86_id_insn *id_insn, yasm_insn_operand **ops,
  905. yasm_insn_operand **rev_ops, const unsigned int *size_lookup)
  906. {
  907. const x86_insn_info *i;
  908. int ni;
  909. int found;
  910. int bypass;
  911. /* Check for matching # of operands */
  912. found = 0;
  913. for (ni=id_insn->num_info, i=id_insn->group; ni>0; ni--, i++) {
  914. if (id_insn->insn.num_operands == i->num_operands) {
  915. found = 1;
  916. break;
  917. }
  918. }
  919. if (!found) {
  920. yasm_error_set(YASM_ERROR_TYPE, N_("invalid number of operands"));
  921. return;
  922. }
  923. for (bypass=1; bypass<9; bypass++) {
  924. i = x86_find_match(id_insn, ops, rev_ops, size_lookup, bypass);
  925. if (i)
  926. break;
  927. }
  928. switch (bypass) {
  929. case 1:
  930. case 4:
  931. yasm_error_set(YASM_ERROR_TYPE,
  932. N_("invalid size for operand %d"), 1);
  933. break;
  934. case 2:
  935. case 5:
  936. yasm_error_set(YASM_ERROR_TYPE,
  937. N_("invalid size for operand %d"), 2);
  938. break;
  939. case 3:
  940. case 6:
  941. yasm_error_set(YASM_ERROR_TYPE,
  942. N_("invalid size for operand %d"), 3);
  943. break;
  944. case 7:
  945. yasm_error_set(YASM_ERROR_TYPE,
  946. N_("one of source operand 1 or 3 must match dest operand"));
  947. break;
  948. case 8:
  949. {
  950. unsigned int cpu0 = i->cpu0, cpu1 = i->cpu1, cpu2 = i->cpu2;
  951. yasm_error_set(YASM_ERROR_TYPE,
  952. N_("requires CPU%s"),
  953. cpu_find_reverse(cpu0, cpu1, cpu2));
  954. break;
  955. }
  956. default:
  957. yasm_error_set(YASM_ERROR_TYPE,
  958. N_("invalid combination of opcode and operands"));
  959. }
  960. }
  961. static void
  962. x86_id_insn_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc)
  963. {
  964. x86_id_insn *id_insn = (x86_id_insn *)bc->contents;
  965. x86_insn *insn;
  966. const x86_insn_info *info = id_insn->group;
  967. unsigned int mode_bits = id_insn->mode_bits;
  968. unsigned char *mod_data = id_insn->mod_data;
  969. yasm_insn_operand *op, *ops[5], *rev_ops[5];
  970. /*@null@*/ yasm_expr *imm;
  971. unsigned char im_len;
  972. unsigned char im_sign;
  973. unsigned char spare;
  974. unsigned char vexdata, vexreg;
  975. unsigned int i;
  976. unsigned int size_lookup[] = {0, 8, 16, 32, 64, 80, 128, 256, 0};
  977. unsigned long do_postop = 0;
  978. size_lookup[OPS_BITS] = mode_bits;
  979. yasm_insn_finalize(&id_insn->insn);
  980. /* Build local array of operands from list, since we know we have a max
  981. * of 5 operands.
  982. */
  983. if (id_insn->insn.num_operands > 5) {
  984. yasm_error_set(YASM_ERROR_TYPE, N_("too many operands"));
  985. return;
  986. }
  987. ops[0] = ops[1] = ops[2] = ops[3] = ops[4] = NULL;
  988. for (i = 0, op = yasm_insn_ops_first(&id_insn->insn);
  989. op && i < id_insn->insn.num_operands;
  990. op = yasm_insn_op_next(op), i++)
  991. ops[i] = op;
  992. /* If we're running in GAS mode, build a reverse array of the operands
  993. * as most GAS instructions have reversed operands from Intel style.
  994. */
  995. if (id_insn->parser == X86_PARSER_GAS) {
  996. rev_ops[0] = rev_ops[1] = rev_ops[2] = rev_ops[3] = rev_ops[4] = NULL;
  997. for (i = id_insn->insn.num_operands-1,
  998. op = yasm_insn_ops_first(&id_insn->insn);
  999. op; op = yasm_insn_op_next(op), i--)
  1000. rev_ops[i] = op;
  1001. }
  1002. /* If we're running in GAS mode, look at the first insn_info to see
  1003. * if this is a relative jump (OPA_JmpRel). If so, run through the
  1004. * operands and adjust for dereferences / lack thereof.
  1005. */
  1006. if (id_insn->parser == X86_PARSER_GAS
  1007. && insn_operands[info->operands_index+0].action == OPA_JmpRel) {
  1008. for (i = 0, op = ops[0]; op; op = ops[++i]) {
  1009. if (!op->deref && (op->type == YASM_INSN__OPERAND_REG
  1010. || (op->type == YASM_INSN__OPERAND_MEMORY
  1011. && op->data.ea->strong)))
  1012. yasm_warn_set(YASM_WARN_GENERAL,
  1013. N_("indirect call without `*'"));
  1014. if (!op->deref && op->type == YASM_INSN__OPERAND_MEMORY
  1015. && !op->data.ea->strong) {
  1016. /* Memory that is not dereferenced, and not strong, is
  1017. * actually an immediate for the purposes of relative jumps.
  1018. */
  1019. if (op->data.ea->segreg != 0)
  1020. yasm_warn_set(YASM_WARN_GENERAL,
  1021. N_("skipping prefixes on this instruction"));
  1022. imm = op->data.ea->disp.abs;
  1023. op->data.ea->disp.abs = NULL;
  1024. yasm_x86__ea_destroy(op->data.ea);
  1025. op->type = YASM_INSN__OPERAND_IMM;
  1026. op->data.val = imm;
  1027. }
  1028. }
  1029. }
  1030. info = x86_find_match(id_insn, ops, rev_ops, size_lookup, 0);
  1031. if (!info) {
  1032. /* Didn't find a match */
  1033. x86_match_error(id_insn, ops, rev_ops, size_lookup);
  1034. return;
  1035. }
  1036. if (id_insn->insn.num_operands > 0) {
  1037. switch (insn_operands[info->operands_index+0].action) {
  1038. case OPA_JmpRel:
  1039. /* Shortcut to JmpRel */
  1040. x86_finalize_jmp(bc, prev_bc, info);
  1041. return;
  1042. case OPA_JmpFar:
  1043. /* Shortcut to JmpFar */
  1044. x86_finalize_jmpfar(bc, prev_bc, info);
  1045. return;
  1046. }
  1047. }
  1048. /* Copy what we can from info */
  1049. insn = yasm_xmalloc(sizeof(x86_insn));
  1050. x86_finalize_common(&insn->common, info, mode_bits);
  1051. x86_finalize_opcode(&insn->opcode, info);
  1052. insn->x86_ea = NULL;
  1053. imm = NULL;
  1054. insn->def_opersize_64 = info->def_opersize_64;
  1055. insn->special_prefix = info->special_prefix;
  1056. spare = info->spare;
  1057. vexdata = 0;
  1058. vexreg = 0;
  1059. im_len = 0;
  1060. im_sign = 0;
  1061. insn->postop = X86_POSTOP_NONE;
  1062. insn->rex = 0;
  1063. /* Move VEX/XOP data (stored in special prefix) to separate location to
  1064. * allow overriding of special prefix by modifiers.
  1065. */
  1066. if ((insn->special_prefix & 0xF0) == 0xC0 ||
  1067. (insn->special_prefix & 0xF0) == 0x80) {
  1068. vexdata = insn->special_prefix;
  1069. insn->special_prefix = 0;
  1070. }
  1071. /* Apply modifiers */
  1072. for (i=0; i<NELEMS(info->modifiers); i++) {
  1073. switch (info->modifiers[i]) {
  1074. case MOD_Gap:
  1075. break;
  1076. case MOD_PreAdd:
  1077. insn->special_prefix += mod_data[i];
  1078. break;
  1079. case MOD_Op0Add:
  1080. insn->opcode.opcode[0] += mod_data[i];
  1081. break;
  1082. case MOD_Op1Add:
  1083. insn->opcode.opcode[1] += mod_data[i];
  1084. break;
  1085. case MOD_Op2Add:
  1086. insn->opcode.opcode[2] += mod_data[i];
  1087. break;
  1088. case MOD_SpAdd:
  1089. spare += mod_data[i];
  1090. break;
  1091. case MOD_OpSizeR:
  1092. insn->common.opersize = mod_data[i];
  1093. break;
  1094. case MOD_Imm8:
  1095. imm = yasm_expr_create_ident(yasm_expr_int(
  1096. yasm_intnum_create_uint(mod_data[i])), bc->line);
  1097. im_len = 8;
  1098. break;
  1099. case MOD_DOpS64R:
  1100. insn->def_opersize_64 = mod_data[i];
  1101. break;
  1102. case MOD_Op1AddSp:
  1103. insn->opcode.opcode[1] += mod_data[i]<<3;
  1104. break;
  1105. case MOD_SetVEX:
  1106. vexdata = mod_data[i];
  1107. break;
  1108. default:
  1109. break;
  1110. }
  1111. }
  1112. /* In 64-bit mode, if opersize is 64 and default is not 64,
  1113. * force REX byte.
  1114. */
  1115. if (mode_bits == 64 && insn->common.opersize == 64 &&
  1116. insn->def_opersize_64 != 64)
  1117. insn->rex = 0x48;
  1118. /* Go through operands and assign */
  1119. if (id_insn->insn.num_operands > 0) {
  1120. yasm_insn_operand **use_ops = ops;
  1121. const x86_info_operand *info_ops =
  1122. &insn_operands[info->operands_index];
  1123. /* Use reversed operands in GAS mode if not otherwise specified */
  1124. if (id_insn->parser == X86_PARSER_GAS
  1125. && !(info->gas_flags & GAS_NO_REV))
  1126. use_ops = rev_ops;
  1127. for (i = 0, op = use_ops[0]; op && i<info->num_operands;
  1128. op = use_ops[++i]) {
  1129. switch (info_ops[i].action) {
  1130. case OPA_None:
  1131. /* Throw away the operand contents */
  1132. switch (op->type) {
  1133. case YASM_INSN__OPERAND_REG:
  1134. case YASM_INSN__OPERAND_SEGREG:
  1135. break;
  1136. case YASM_INSN__OPERAND_MEMORY:
  1137. yasm_x86__ea_destroy(op->data.ea);
  1138. break;
  1139. case YASM_INSN__OPERAND_IMM:
  1140. yasm_expr_destroy(op->data.val);
  1141. break;
  1142. }
  1143. break;
  1144. case OPA_EA:
  1145. switch (op->type) {
  1146. case YASM_INSN__OPERAND_REG:
  1147. insn->x86_ea =
  1148. yasm_x86__ea_create_reg(insn->x86_ea,
  1149. (unsigned long)op->data.reg, &insn->rex,
  1150. mode_bits);
  1151. break;
  1152. case YASM_INSN__OPERAND_SEGREG:
  1153. yasm_internal_error(
  1154. N_("invalid operand conversion"));
  1155. case YASM_INSN__OPERAND_MEMORY:
  1156. if (op->seg)
  1157. yasm_error_set(YASM_ERROR_VALUE,
  1158. N_("invalid segment in effective address"));
  1159. insn->x86_ea = (x86_effaddr *)op->data.ea;
  1160. if (info_ops[i].type == OPT_MemOffs)
  1161. /* Special-case for MOV MemOffs instruction */
  1162. yasm_x86__ea_set_disponly(insn->x86_ea);
  1163. else if (info_ops[i].type == OPT_MemXMMIndex) {
  1164. /* Remember VSIB mode */
  1165. insn->x86_ea->vsib_mode = 1;
  1166. insn->x86_ea->need_sib = 1;
  1167. } else if (info_ops[i].type == OPT_MemYMMIndex) {
  1168. /* Remember VSIB mode */
  1169. insn->x86_ea->vsib_mode = 2;
  1170. insn->x86_ea->need_sib = 1;
  1171. } else if (id_insn->default_rel &&
  1172. !op->data.ea->not_pc_rel &&
  1173. op->data.ea->segreg != 0x6404 &&
  1174. op->data.ea->segreg != 0x6505 &&
  1175. !yasm_expr__contains(
  1176. op->data.ea->disp.abs, YASM_EXPR_REG))
  1177. /* Enable default PC-rel if no regs and segreg
  1178. * is not FS or GS.
  1179. */
  1180. insn->x86_ea->ea.pc_rel = 1;
  1181. break;
  1182. case YASM_INSN__OPERAND_IMM:
  1183. insn->x86_ea =
  1184. yasm_x86__ea_create_imm(insn->x86_ea,
  1185. op->data.val,
  1186. size_lookup[info_ops[i].size]);
  1187. break;
  1188. }
  1189. break;
  1190. case OPA_EAVEX:
  1191. if (op->type != YASM_INSN__OPERAND_REG)
  1192. yasm_internal_error(N_("invalid operand conversion"));
  1193. insn->x86_ea =
  1194. yasm_x86__ea_create_reg(insn->x86_ea,
  1195. (unsigned long)op->data.reg, &insn->rex, mode_bits);
  1196. vexreg = op->data.reg & 0xF;
  1197. break;
  1198. case OPA_Imm:
  1199. if (op->seg)
  1200. yasm_error_set(YASM_ERROR_VALUE,
  1201. N_("immediate does not support segment"));
  1202. if (op->type == YASM_INSN__OPERAND_IMM) {
  1203. imm = op->data.val;
  1204. im_len = size_lookup[info_ops[i].size];
  1205. } else
  1206. yasm_internal_error(N_("invalid operand conversion"));
  1207. break;
  1208. case OPA_SImm:
  1209. if (op->seg)
  1210. yasm_error_set(YASM_ERROR_VALUE,
  1211. N_("immediate does not support segment"));
  1212. if (op->type == YASM_INSN__OPERAND_IMM) {
  1213. imm = op->data.val;
  1214. im_len = size_lookup[info_ops[i].size];
  1215. im_sign = 1;
  1216. } else
  1217. yasm_internal_error(N_("invalid operand conversion"));
  1218. break;
  1219. case OPA_Spare:
  1220. if (op->type == YASM_INSN__OPERAND_SEGREG)
  1221. spare = (unsigned char)(op->data.reg&7);
  1222. else if (op->type == YASM_INSN__OPERAND_REG) {
  1223. if (yasm_x86__set_rex_from_reg(&insn->rex, &spare,
  1224. op->data.reg, mode_bits, X86_REX_R))
  1225. return;
  1226. } else
  1227. yasm_internal_error(N_("invalid operand conversion"));
  1228. break;
  1229. case OPA_SpareVEX:
  1230. if (op->type != YASM_INSN__OPERAND_REG)
  1231. yasm_internal_error(N_("invalid operand conversion"));
  1232. if (yasm_x86__set_rex_from_reg(&insn->rex, &spare,
  1233. op->data.reg, mode_bits, X86_REX_R))
  1234. return;
  1235. vexreg = op->data.reg & 0xF;
  1236. break;
  1237. case OPA_Op0Add:
  1238. if (op->type == YASM_INSN__OPERAND_REG) {
  1239. unsigned char opadd;
  1240. if (yasm_x86__set_rex_from_reg(&insn->rex, &opadd,
  1241. op->data.reg, mode_bits, X86_REX_B))
  1242. return;
  1243. insn->opcode.opcode[0] += opadd;
  1244. } else
  1245. yasm_internal_error(N_("invalid operand conversion"));
  1246. break;
  1247. case OPA_Op1Add:
  1248. if (op->type == YASM_INSN__OPERAND_REG) {
  1249. unsigned char opadd;
  1250. if (yasm_x86__set_rex_from_reg(&insn->rex, &opadd,
  1251. op->data.reg, mode_bits, X86_REX_B))
  1252. return;
  1253. insn->opcode.opcode[1] += opadd;
  1254. } else
  1255. yasm_internal_error(N_("invalid operand conversion"));
  1256. break;
  1257. case OPA_SpareEA:
  1258. if (op->type == YASM_INSN__OPERAND_REG) {
  1259. insn->x86_ea =
  1260. yasm_x86__ea_create_reg(insn->x86_ea,
  1261. (unsigned long)op->data.reg, &insn->rex,
  1262. mode_bits);
  1263. if (!insn->x86_ea ||
  1264. yasm_x86__set_rex_from_reg(&insn->rex, &spare,
  1265. op->data.reg, mode_bits, X86_REX_R)) {
  1266. if (insn->x86_ea)
  1267. yasm_xfree(insn->x86_ea);
  1268. yasm_xfree(insn);
  1269. return;
  1270. }
  1271. } else
  1272. yasm_internal_error(N_("invalid operand conversion"));
  1273. break;
  1274. case OPA_AdSizeEA: {
  1275. const uintptr_t *regp = NULL;
  1276. /* Only implement this for OPT_MemrAX and OPT_MemEAX
  1277. * for now.
  1278. */
  1279. if (op->type != YASM_INSN__OPERAND_MEMORY ||
  1280. !(regp = yasm_expr_get_reg(&op->data.ea->disp.abs, 0)))
  1281. yasm_internal_error(N_("invalid operand conversion"));
  1282. /* 64-bit mode does not allow 16-bit addresses */
  1283. if (mode_bits == 64 && *regp == (X86_REG16 | 0))
  1284. yasm_error_set(YASM_ERROR_TYPE,
  1285. N_("16-bit addresses not supported in 64-bit mode"));
  1286. else if (*regp == (X86_REG16 | 0))
  1287. insn->common.addrsize = 16;
  1288. else if (*regp == (X86_REG32 | 0))
  1289. insn->common.addrsize = 32;
  1290. else if (mode_bits == 64 && *regp == (X86_REG64 | 0))
  1291. insn->common.addrsize = 64;
  1292. else
  1293. yasm_error_set(YASM_ERROR_TYPE,
  1294. N_("unsupported address size"));
  1295. yasm_x86__ea_destroy(op->data.ea);
  1296. break;
  1297. }
  1298. case OPA_VEX:
  1299. if (op->type != YASM_INSN__OPERAND_REG)
  1300. yasm_internal_error(N_("invalid operand conversion"));
  1301. vexreg = op->data.reg & 0xF;
  1302. break;
  1303. case OPA_VEXImmSrc:
  1304. if (op->type != YASM_INSN__OPERAND_REG)
  1305. yasm_internal_error(N_("invalid operand conversion"));
  1306. if (!imm) {
  1307. imm = yasm_expr_create_ident(
  1308. yasm_expr_int(
  1309. yasm_intnum_create_uint((op->data.reg << 4)
  1310. & 0xF0)),
  1311. bc->line);
  1312. } else {
  1313. imm = yasm_expr_create(
  1314. YASM_EXPR_OR,
  1315. yasm_expr_expr(yasm_expr_create(
  1316. YASM_EXPR_AND,
  1317. yasm_expr_expr(imm),
  1318. yasm_expr_int(yasm_intnum_create_uint(0x0F)),
  1319. bc->line)),
  1320. yasm_expr_int(
  1321. yasm_intnum_create_uint((op->data.reg << 4)
  1322. & 0xF0)),
  1323. bc->line);
  1324. }
  1325. im_len = 8;
  1326. break;
  1327. case OPA_VEXImm:
  1328. if (op->type != YASM_INSN__OPERAND_IMM)
  1329. yasm_internal_error(N_("invalid operand conversion"));
  1330. if (!imm)
  1331. imm = op->data.val;
  1332. else {
  1333. imm = yasm_expr_create(
  1334. YASM_EXPR_OR,
  1335. yasm_expr_expr(yasm_expr_create(
  1336. YASM_EXPR_AND,
  1337. yasm_expr_expr(op->data.val),
  1338. yasm_expr_int(yasm_intnum_create_uint(0x0F)),
  1339. bc->line)),
  1340. yasm_expr_expr(yasm_expr_create(
  1341. YASM_EXPR_AND,
  1342. yasm_expr_expr(imm),
  1343. yasm_expr_int(yasm_intnum_create_uint(0xF0)),
  1344. bc->line)),
  1345. bc->line);
  1346. }
  1347. im_len = 8;
  1348. break;
  1349. default:
  1350. yasm_internal_error(N_("unknown operand action"));
  1351. }
  1352. if (info_ops[i].size == OPS_BITS)
  1353. insn->common.opersize = (unsigned char)mode_bits;
  1354. switch (info_ops[i].post_action) {
  1355. case OPAP_None:
  1356. break;
  1357. case OPAP_SImm8:
  1358. /* Check operand strictness; if strict and non-8-bit,
  1359. * pre-emptively expand to full size.
  1360. * For unspecified size case, still optimize.
  1361. */
  1362. if (!(id_insn->force_strict || op->strict)
  1363. || op->size == 0)
  1364. insn->postop = X86_POSTOP_SIGNEXT_IMM8;
  1365. else if (op->size != 8) {
  1366. insn->opcode.opcode[0] =
  1367. insn->opcode.opcode[insn->opcode.len];
  1368. insn->opcode.len = 1;
  1369. }
  1370. break;
  1371. case OPAP_ShortMov:
  1372. do_postop = OPAP_ShortMov;
  1373. break;
  1374. case OPAP_A16:
  1375. insn->postop = X86_POSTOP_ADDRESS16;
  1376. break;
  1377. case OPAP_SImm32Avail:
  1378. do_postop = OPAP_SImm32Avail;
  1379. break;
  1380. default:
  1381. yasm_internal_error(
  1382. N_("unknown operand postponed action"));
  1383. }
  1384. }
  1385. }
  1386. if (insn->x86_ea) {
  1387. yasm_x86__ea_init(insn->x86_ea, spare, prev_bc);
  1388. for (i=0; i<id_insn->insn.num_segregs; i++)
  1389. yasm_ea_set_segreg(&insn->x86_ea->ea, id_insn->insn.segregs[i]);
  1390. } else if (id_insn->insn.num_segregs > 0 && insn->special_prefix == 0) {
  1391. if (id_insn->insn.num_segregs > 1)
  1392. yasm_warn_set(YASM_WARN_GENERAL,
  1393. N_("multiple segment overrides, using leftmost"));
  1394. insn->special_prefix = (unsigned char)
  1395. (id_insn->insn.segregs[id_insn->insn.num_segregs-1]>>8);
  1396. } else if (id_insn->insn.num_segregs > 0)
  1397. yasm_internal_error(N_("unhandled segment prefix"));
  1398. if (imm) {
  1399. insn->imm = yasm_xmalloc(sizeof(yasm_value));
  1400. if (yasm_value_finalize_expr(insn->imm, imm, prev_bc, im_len))
  1401. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  1402. N_("immediate expression too complex"));
  1403. insn->imm->sign = im_sign;
  1404. } else
  1405. insn->imm = NULL;
  1406. yasm_x86__bc_apply_prefixes((x86_common *)insn, &insn->rex,
  1407. insn->def_opersize_64,
  1408. id_insn->insn.num_prefixes,
  1409. id_insn->insn.prefixes);
  1410. if (insn->postop == X86_POSTOP_ADDRESS16 && insn->common.addrsize) {
  1411. yasm_warn_set(YASM_WARN_GENERAL, N_("address size override ignored"));
  1412. insn->common.addrsize = 0;
  1413. }
  1414. /* Handle non-span-dependent post-ops here */
  1415. switch (do_postop) {
  1416. case OPAP_ShortMov:
  1417. /* Long (modrm+sib) mov instructions in amd64 can be optimized into
  1418. * short mov instructions if a 32-bit address override is applied in
  1419. * 64-bit mode to an EA of just an offset (no registers) and the
  1420. * target register is al/ax/eax/rax.
  1421. *
  1422. * We don't want to do this if we're in default rel mode.
  1423. */
  1424. if (!id_insn->default_rel &&
  1425. insn->common.mode_bits == 64 &&
  1426. insn->common.addrsize == 32 &&
  1427. (!insn->x86_ea->ea.disp.abs ||
  1428. !yasm_expr__contains(insn->x86_ea->ea.disp.abs,
  1429. YASM_EXPR_REG))) {
  1430. yasm_x86__ea_set_disponly(insn->x86_ea);
  1431. /* Make the short form permanent. */
  1432. insn->opcode.opcode[0] = insn->opcode.opcode[1];
  1433. }
  1434. insn->opcode.opcode[1] = 0; /* avoid possible confusion */
  1435. break;
  1436. case OPAP_SImm32Avail:
  1437. /* Used for 64-bit mov immediate, which can take a sign-extended
  1438. * imm32 as well as imm64 values. The imm32 form is put in the
  1439. * second byte of the opcode and its ModRM byte is put in the third
  1440. * byte of the opcode.
  1441. */
  1442. if (!insn->imm->abs ||
  1443. (yasm_expr_get_intnum(&insn->imm->abs, 0) &&
  1444. yasm_intnum_check_size(
  1445. yasm_expr_get_intnum(&insn->imm->abs, 0), 32, 0, 1))) {
  1446. /* Throwaway REX byte */
  1447. unsigned char rex_temp = 0;
  1448. /* Build ModRM EA - CAUTION: this depends on
  1449. * opcode 0 being a mov instruction!
  1450. */
  1451. insn->x86_ea = yasm_x86__ea_create_reg(insn->x86_ea,
  1452. (unsigned long)insn->opcode.opcode[0]-0xB8, &rex_temp, 64);
  1453. /* Make the imm32s form permanent. */
  1454. insn->opcode.opcode[0] = insn->opcode.opcode[1];
  1455. insn->imm->size = 32;
  1456. }
  1457. insn->opcode.opcode[1] = 0; /* avoid possible confusion */
  1458. break;
  1459. default:
  1460. break;
  1461. }
  1462. /* Convert to VEX/XOP prefixes if requested.
  1463. * To save space in the insn structure, the VEX/XOP prefix is written into
  1464. * special_prefix and the first 2 bytes of the instruction are set to
  1465. * the second two VEX/XOP bytes. During calc_len() it may be shortened to
  1466. * one VEX byte (this can only be done after knowledge of REX value); this
  1467. * further optimization is not possible for XOP.
  1468. */
  1469. if (vexdata) {
  1470. int xop = ((vexdata & 0xF0) == 0x80);
  1471. unsigned char vex1 = 0xE0; /* R=X=B=1, mmmmm=0 */
  1472. unsigned char vex2;
  1473. if (xop) {
  1474. /* Look at the first bytes of the opcode for the XOP mmmmm field.
  1475. * Leave R=X=B=1 for now.
  1476. */
  1477. if (insn->opcode.opcode[0] != 0x08 &&
  1478. insn->opcode.opcode[0] != 0x09 &&
  1479. insn->opcode.opcode[0] != 0x0A)
  1480. yasm_internal_error(N_("first opcode byte of XOP must be 0x08, 0x09, or 0x0A"));
  1481. vex1 |= insn->opcode.opcode[0];
  1482. /* Move opcode byte back one byte to make room for XOP prefix. */
  1483. insn->opcode.opcode[2] = insn->opcode.opcode[1];
  1484. } else {
  1485. /* Look at the first bytes of the opcode to see what leading bytes
  1486. * to encode in the VEX mmmmm field. Leave R=X=B=1 for now.
  1487. */
  1488. if (insn->opcode.opcode[0] != 0x0F)
  1489. yasm_internal_error(N_("first opcode byte of VEX must be 0x0F"));
  1490. if (insn->opcode.opcode[1] == 0x38)
  1491. vex1 |= 0x02; /* implied 0x0F 0x38 */
  1492. else if (insn->opcode.opcode[1] == 0x3A)
  1493. vex1 |= 0x03; /* implied 0x0F 0x3A */
  1494. else {
  1495. /* Originally a 0F-only opcode; move opcode byte back one
  1496. * position to make room for VEX prefix.
  1497. */
  1498. insn->opcode.opcode[2] = insn->opcode.opcode[1];
  1499. vex1 |= 0x01; /* implied 0x0F */
  1500. }
  1501. }
  1502. /* Check for update of special prefix by modifiers */
  1503. if (insn->special_prefix != 0) {
  1504. vexdata &= ~0x03;
  1505. switch (insn->special_prefix) {
  1506. case 0x66:
  1507. vexdata |= 0x01;
  1508. break;
  1509. case 0xF3:
  1510. vexdata |= 0x02;
  1511. break;
  1512. case 0xF2:
  1513. vexdata |= 0x03;
  1514. break;
  1515. default:
  1516. yasm_internal_error(N_("unrecognized special prefix"));
  1517. }
  1518. }
  1519. /* 2nd VEX byte is WvvvvLpp.
  1520. * W, L, pp come from vexdata
  1521. * vvvv comes from 1s complement of vexreg
  1522. */
  1523. vex2 = (((vexdata & 0x8) << 4) | /* W */
  1524. ((15 - (vexreg & 0xF)) << 3) | /* vvvv */
  1525. (vexdata & 0x7)); /* Lpp */
  1526. /* Save to special_prefix and opcode */
  1527. insn->special_prefix = xop ? 0x8F : 0xC4; /* VEX/XOP prefix */
  1528. insn->opcode.opcode[0] = vex1;
  1529. insn->opcode.opcode[1] = vex2;
  1530. insn->opcode.len = 3; /* two prefix bytes and 1 opcode byte */
  1531. }
  1532. x86_id_insn_clear_operands(id_insn);
  1533. /* Transform the bytecode */
  1534. yasm_x86__bc_transform_insn(bc, insn);
  1535. }
  1536. /* Static parse data structure for instructions */
  1537. typedef struct insnprefix_parse_data {
  1538. const char *name;
  1539. /* instruction parse group - NULL if prefix */
  1540. /*@null@*/ const x86_insn_info *group;
  1541. /* For instruction, number of elements in group.
  1542. * For prefix, prefix type shifted right by 8.
  1543. */
  1544. unsigned int num_info:8;
  1545. /* For instruction, GAS suffix flags.
  1546. * For prefix, prefix value.
  1547. */
  1548. unsigned int flags:8;
  1549. /* Instruction modifier data. */
  1550. unsigned int mod_data0:8;
  1551. unsigned int mod_data1:8;
  1552. unsigned int mod_data2:8;
  1553. /* Tests against BITS==64 and AVX */
  1554. unsigned int misc_flags:6;
  1555. /* CPU flags */
  1556. unsigned int cpu0:6;
  1557. unsigned int cpu1:6;
  1558. unsigned int cpu2:6;
  1559. } insnprefix_parse_data;
  1560. /* Pull in all parse data */
  1561. #include "x86insn_nasm.c"
  1562. #include "x86insn_gas.c"
  1563. static const char *
  1564. cpu_find_reverse(unsigned int cpu0, unsigned int cpu1, unsigned int cpu2)
  1565. {
  1566. static char cpuname[200];
  1567. wordptr cpu = BitVector_Create(128, TRUE);
  1568. if (cpu0 != CPU_Any)
  1569. BitVector_Bit_On(cpu, cpu0);
  1570. if (cpu1 != CPU_Any)
  1571. BitVector_Bit_On(cpu, cpu1);
  1572. if (cpu2 != CPU_Any)
  1573. BitVector_Bit_On(cpu, cpu2);
  1574. cpuname[0] = '\0';
  1575. if (BitVector_bit_test(cpu, CPU_Prot))
  1576. strcat(cpuname, " Protected");
  1577. if (BitVector_bit_test(cpu, CPU_Undoc))
  1578. strcat(cpuname, " Undocumented");
  1579. if (BitVector_bit_test(cpu, CPU_Obs))
  1580. strcat(cpuname, " Obsolete");
  1581. if (BitVector_bit_test(cpu, CPU_Priv))
  1582. strcat(cpuname, " Privileged");
  1583. if (BitVector_bit_test(cpu, CPU_FPU))
  1584. strcat(cpuname, " FPU");
  1585. if (BitVector_bit_test(cpu, CPU_MMX))
  1586. strcat(cpuname, " MMX");
  1587. if (BitVector_bit_test(cpu, CPU_SSE))
  1588. strcat(cpuname, " SSE");
  1589. if (BitVector_bit_test(cpu, CPU_SSE2))
  1590. strcat(cpuname, " SSE2");
  1591. if (BitVector_bit_test(cpu, CPU_SSE3))
  1592. strcat(cpuname, " SSE3");
  1593. if (BitVector_bit_test(cpu, CPU_3DNow))
  1594. strcat(cpuname, " 3DNow");
  1595. if (BitVector_bit_test(cpu, CPU_Cyrix))
  1596. strcat(cpuname, " Cyrix");
  1597. if (BitVector_bit_test(cpu, CPU_AMD))
  1598. strcat(cpuname, " AMD");
  1599. if (BitVector_bit_test(cpu, CPU_SMM))
  1600. strcat(cpuname, " SMM");
  1601. if (BitVector_bit_test(cpu, CPU_SVM))
  1602. strcat(cpuname, " SVM");
  1603. if (BitVector_bit_test(cpu, CPU_PadLock))
  1604. strcat(cpuname, " PadLock");
  1605. if (BitVector_bit_test(cpu, CPU_EM64T))
  1606. strcat(cpuname, " EM64T");
  1607. if (BitVector_bit_test(cpu, CPU_SSSE3))
  1608. strcat(cpuname, " SSSE3");
  1609. if (BitVector_bit_test(cpu, CPU_SSE41))
  1610. strcat(cpuname, " SSE4.1");
  1611. if (BitVector_bit_test(cpu, CPU_SSE42))
  1612. strcat(cpuname, " SSE4.2");
  1613. if (BitVector_bit_test(cpu, CPU_186))
  1614. strcat(cpuname, " 186");
  1615. if (BitVector_bit_test(cpu, CPU_286))
  1616. strcat(cpuname, " 286");
  1617. if (BitVector_bit_test(cpu, CPU_386))
  1618. strcat(cpuname, " 386");
  1619. if (BitVector_bit_test(cpu, CPU_486))
  1620. strcat(cpuname, " 486");
  1621. if (BitVector_bit_test(cpu, CPU_586))
  1622. strcat(cpuname, " 586");
  1623. if (BitVector_bit_test(cpu, CPU_686))
  1624. strcat(cpuname, " 686");
  1625. if (BitVector_bit_test(cpu, CPU_P3))
  1626. strcat(cpuname, " P3");
  1627. if (BitVector_bit_test(cpu, CPU_P4))
  1628. strcat(cpuname, " P4");
  1629. if (BitVector_bit_test(cpu, CPU_IA64))
  1630. strcat(cpuname, " IA64");
  1631. if (BitVector_bit_test(cpu, CPU_K6))
  1632. strcat(cpuname, " K6");
  1633. if (BitVector_bit_test(cpu, CPU_Athlon))
  1634. strcat(cpuname, " Athlon");
  1635. if (BitVector_bit_test(cpu, CPU_Hammer))
  1636. strcat(cpuname, " Hammer");
  1637. BitVector_Destroy(cpu);
  1638. return cpuname;
  1639. }
  1640. yasm_arch_insnprefix
  1641. yasm_x86__parse_check_insnprefix(yasm_arch *arch, const char *id,
  1642. size_t id_len, unsigned long line,
  1643. yasm_bytecode **bc, uintptr_t *prefix)
  1644. {
  1645. yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch;
  1646. /*@null@*/ const insnprefix_parse_data *pdata;
  1647. size_t i;
  1648. static char lcaseid[17];
  1649. *bc = (yasm_bytecode *)NULL;
  1650. *prefix = 0;
  1651. if (id_len > 16)
  1652. return YASM_ARCH_NOTINSNPREFIX;
  1653. for (i=0; i<id_len; i++)
  1654. lcaseid[i] = tolower(id[i]);
  1655. lcaseid[id_len] = '\0';
  1656. switch (PARSER(arch_x86)) {
  1657. case X86_PARSER_NASM:
  1658. pdata = insnprefix_nasm_find(lcaseid, id_len);
  1659. break;
  1660. case X86_PARSER_TASM:
  1661. pdata = insnprefix_nasm_find(lcaseid, id_len);
  1662. break;
  1663. case X86_PARSER_GAS:
  1664. pdata = insnprefix_gas_find(lcaseid, id_len);
  1665. break;
  1666. default:
  1667. pdata = NULL;
  1668. }
  1669. if (!pdata)
  1670. return YASM_ARCH_NOTINSNPREFIX;
  1671. if (pdata->group) {
  1672. x86_id_insn *id_insn;
  1673. wordptr cpu_enabled = arch_x86->cpu_enables[arch_x86->active_cpu];
  1674. unsigned int cpu0, cpu1, cpu2;
  1675. if (arch_x86->mode_bits != 64 && (pdata->misc_flags & ONLY_64)) {
  1676. yasm_warn_set(YASM_WARN_GENERAL,
  1677. N_("`%s' is an instruction in 64-bit mode"), id);
  1678. return YASM_ARCH_NOTINSNPREFIX;
  1679. }
  1680. if (arch_x86->mode_bits == 64 && (pdata->misc_flags & NOT_64)) {
  1681. yasm_error_set(YASM_ERROR_GENERAL,
  1682. N_("`%s' invalid in 64-bit mode"), id);
  1683. id_insn = yasm_xmalloc(sizeof(x86_id_insn));
  1684. yasm_insn_initialize(&id_insn->insn);
  1685. id_insn->group = not64_insn;
  1686. id_insn->cpu_enabled = cpu_enabled;
  1687. id_insn->mod_data[0] = 0;
  1688. id_insn->mod_data[1] = 0;
  1689. id_insn->mod_data[2] = 0;
  1690. id_insn->num_info = NELEMS(not64_insn);
  1691. id_insn->mode_bits = arch_x86->mode_bits;
  1692. id_insn->suffix = 0;
  1693. id_insn->misc_flags = 0;
  1694. id_insn->parser = PARSER(arch_x86);
  1695. id_insn->force_strict = arch_x86->force_strict != 0;
  1696. id_insn->default_rel = arch_x86->default_rel != 0;
  1697. *bc = yasm_bc_create_common(&x86_id_insn_callback, id_insn, line);
  1698. return YASM_ARCH_INSN;
  1699. }
  1700. cpu0 = pdata->cpu0;
  1701. cpu1 = pdata->cpu1;
  1702. cpu2 = pdata->cpu2;
  1703. if (!BitVector_bit_test(cpu_enabled, cpu0) ||
  1704. !BitVector_bit_test(cpu_enabled, cpu1) ||
  1705. !BitVector_bit_test(cpu_enabled, cpu2)) {
  1706. yasm_warn_set(YASM_WARN_GENERAL,
  1707. N_("`%s' is an instruction in CPU%s"), id,
  1708. cpu_find_reverse(cpu0, cpu1, cpu2));
  1709. return YASM_ARCH_NOTINSNPREFIX;
  1710. }
  1711. id_insn = yasm_xmalloc(sizeof(x86_id_insn));
  1712. yasm_insn_initialize(&id_insn->insn);
  1713. id_insn->group = pdata->group;
  1714. id_insn->cpu_enabled = cpu_enabled;
  1715. id_insn->mod_data[0] = pdata->mod_data0;
  1716. id_insn->mod_data[1] = pdata->mod_data1;
  1717. id_insn->mod_data[2] = pdata->mod_data2;
  1718. id_insn->num_info = pdata->num_info;
  1719. id_insn->mode_bits = arch_x86->mode_bits;
  1720. id_insn->suffix = pdata->flags;
  1721. id_insn->misc_flags = pdata->misc_flags;
  1722. id_insn->parser = PARSER(arch_x86);
  1723. id_insn->force_strict = arch_x86->force_strict != 0;
  1724. id_insn->default_rel = arch_x86->default_rel != 0;
  1725. *bc = yasm_bc_create_common(&x86_id_insn_callback, id_insn, line);
  1726. return YASM_ARCH_INSN;
  1727. } else {
  1728. unsigned long type = pdata->num_info<<8;
  1729. unsigned long value = pdata->flags;
  1730. if (arch_x86->mode_bits == 64 && type == X86_OPERSIZE && value == 32) {
  1731. yasm_error_set(YASM_ERROR_GENERAL,
  1732. N_("Cannot override data size to 32 bits in 64-bit mode"));
  1733. return YASM_ARCH_NOTINSNPREFIX;
  1734. }
  1735. if (arch_x86->mode_bits == 64 && type == X86_ADDRSIZE && value == 16) {
  1736. yasm_error_set(YASM_ERROR_GENERAL,
  1737. N_("Cannot override address size to 16 bits in 64-bit mode"));
  1738. return YASM_ARCH_NOTINSNPREFIX;
  1739. }
  1740. if (arch_x86->mode_bits != 64 && (pdata->misc_flags & ONLY_64)) {
  1741. yasm_warn_set(YASM_WARN_GENERAL,
  1742. N_("`%s' is a prefix in 64-bit mode"), id);
  1743. return YASM_ARCH_NOTINSNPREFIX;
  1744. }
  1745. *prefix = type|value;
  1746. return YASM_ARCH_PREFIX;
  1747. }
  1748. }
  1749. static void
  1750. x86_id_insn_destroy(void *contents)
  1751. {
  1752. x86_id_insn *id_insn = (x86_id_insn *)contents;
  1753. yasm_insn_delete(&id_insn->insn, yasm_x86__ea_destroy);
  1754. yasm_xfree(contents);
  1755. }
  1756. static void
  1757. x86_id_insn_print(const void *contents, FILE *f, int indent_level)
  1758. {
  1759. const x86_id_insn *id_insn = (const x86_id_insn *)contents;
  1760. yasm_insn_print(&id_insn->insn, f, indent_level);
  1761. /*TODO*/
  1762. }
  1763. /*@only@*/ yasm_bytecode *
  1764. yasm_x86__create_empty_insn(yasm_arch *arch, unsigned long line)
  1765. {
  1766. yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch;
  1767. x86_id_insn *id_insn = yasm_xmalloc(sizeof(x86_id_insn));
  1768. yasm_insn_initialize(&id_insn->insn);
  1769. id_insn->group = empty_insn;
  1770. id_insn->cpu_enabled = arch_x86->cpu_enables[arch_x86->active_cpu];
  1771. id_insn->mod_data[0] = 0;
  1772. id_insn->mod_data[1] = 0;
  1773. id_insn->mod_data[2] = 0;
  1774. id_insn->num_info = NELEMS(empty_insn);
  1775. id_insn->mode_bits = arch_x86->mode_bits;
  1776. id_insn->suffix = (PARSER(arch_x86) == X86_PARSER_GAS) ? SUF_Z : 0;
  1777. id_insn->misc_flags = 0;
  1778. id_insn->parser = PARSER(arch_x86);
  1779. id_insn->force_strict = arch_x86->force_strict != 0;
  1780. id_insn->default_rel = arch_x86->default_rel != 0;
  1781. return yasm_bc_create_common(&x86_id_insn_callback, id_insn, line);
  1782. }