assemble.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2018 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * assemble.c code generation for the Netwide Assembler
  35. *
  36. * Bytecode specification
  37. * ----------------------
  38. *
  39. *
  40. * Codes Mnemonic Explanation
  41. *
  42. * \0 terminates the code. (Unless it's a literal of course.)
  43. * \1..\4 that many literal bytes follow in the code stream
  44. * \5 add 4 to the primary operand number (b, low octdigit)
  45. * \6 add 4 to the secondary operand number (a, middle octdigit)
  46. * \7 add 4 to both the primary and the secondary operand number
  47. * \10..\13 a literal byte follows in the code stream, to be added
  48. * to the register value of operand 0..3
  49. * \14..\17 the position of index register operand in MIB (BND insns)
  50. * \20..\23 ib a byte immediate operand, from operand 0..3
  51. * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
  52. * \30..\33 iw a word immediate operand, from operand 0..3
  53. * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
  54. * assembly mode or the operand-size override on the operand
  55. * \40..\43 id a long immediate operand, from operand 0..3
  56. * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
  57. * depending on the address size of the instruction.
  58. * \50..\53 rel8 a byte relative operand, from operand 0..3
  59. * \54..\57 iq a qword immediate operand, from operand 0..3
  60. * \60..\63 rel16 a word relative operand, from operand 0..3
  61. * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
  62. * assembly mode or the operand-size override on the operand
  63. * \70..\73 rel32 a long relative operand, from operand 0..3
  64. * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
  65. * \1ab a ModRM, calculated on EA in operand a, with the spare
  66. * field the register value of operand b.
  67. * \172\ab the register number from operand a in bits 7..4, with
  68. * the 4-bit immediate from operand b in bits 3..0.
  69. * \173\xab the register number from operand a in bits 7..4, with
  70. * the value b in bits 3..0.
  71. * \174..\177 the register number from operand 0..3 in bits 7..4, and
  72. * an arbitrary value in bits 3..0 (assembled as zero.)
  73. * \2ab a ModRM, calculated on EA in operand a, with the spare
  74. * field equal to digit b.
  75. *
  76. * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
  77. * V field taken from operand 0..3.
  78. * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
  79. * V field set to 1111b.
  80. *
  81. * EVEX prefixes are followed by the sequence:
  82. * \cm\wlp\tup where cm is:
  83. * cc 00m mmm
  84. * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
  85. * and wlp is:
  86. * 00 wwl lpp
  87. * [l0] ll = 0 (.128, .lz)
  88. * [l1] ll = 1 (.256)
  89. * [l2] ll = 2 (.512)
  90. * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
  91. *
  92. * [w0] ww = 0 for W = 0
  93. * [w1] ww = 1 for W = 1
  94. * [wig] ww = 2 for W don't care (always assembled as 0)
  95. * [ww] ww = 3 for W used as REX.W
  96. *
  97. * [p0] pp = 0 for no prefix
  98. * [60] pp = 1 for legacy prefix 60
  99. * [f3] pp = 2
  100. * [f2] pp = 3
  101. *
  102. * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
  103. * (compressed displacement encoding)
  104. *
  105. * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
  106. * \260..\263 this instruction uses VEX/XOP rather than REX, with the
  107. * V field taken from operand 0..3.
  108. * \270 this instruction uses VEX/XOP rather than REX, with the
  109. * V field set to 1111b.
  110. *
  111. * VEX/XOP prefixes are followed by the sequence:
  112. * \tmm\wlp where mm is the M field; and wlp is:
  113. * 00 wwl lpp
  114. * [l0] ll = 0 for L = 0 (.128, .lz)
  115. * [l1] ll = 1 for L = 1 (.256)
  116. * [lig] ll = 2 for L don't care (always assembled as 0)
  117. *
  118. * [w0] ww = 0 for W = 0
  119. * [w1 ] ww = 1 for W = 1
  120. * [wig] ww = 2 for W don't care (always assembled as 0)
  121. * [ww] ww = 3 for W used as REX.W
  122. *
  123. * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
  124. *
  125. * \271 hlexr instruction takes XRELEASE (F3) with or without lock
  126. * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
  127. * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
  128. * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
  129. * to the operand size (if o16/o32/o64 present) or the bit size
  130. * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
  131. * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
  132. * \312 adf (disassembler only) invalid with non-default address size.
  133. * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
  134. * \314 norexb (disassembler only) invalid with REX.B
  135. * \315 norexx (disassembler only) invalid with REX.X
  136. * \316 norexr (disassembler only) invalid with REX.R
  137. * \317 norexw (disassembler only) invalid with REX.W
  138. * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
  139. * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
  140. * \322 odf indicates that this instruction is only valid when the
  141. * operand size is the default (instruction to disassembler,
  142. * generates no code in the assembler)
  143. * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
  144. * \324 o64 indicates 64-bit operand size requiring REX prefix.
  145. * \325 nohi instruction which always uses spl/bpl/sil/dil
  146. * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
  147. disassembler only; for SSE instructions.
  148. * \330 a literal byte follows in the code stream, to be added
  149. * to the condition code value of the instruction.
  150. * \331 norep instruction not valid with REP prefix. Hint for
  151. * disassembler only; for SSE instructions.
  152. * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
  153. * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
  154. * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
  155. * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
  156. * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
  157. * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
  158. * \336-\337 are still listed as prefixes in the disassembler.
  159. * \340 resb reserve <operand 0> bytes of uninitialized storage.
  160. * Operand 0 had better be a segmentless constant.
  161. * \341 wait this instruction needs a WAIT "prefix"
  162. * \360 np no SSE prefix (== \364\331)
  163. * \361 66 SSE prefix (== \366\331)
  164. * \364 !osp operand-size prefix (0x66) not permitted
  165. * \365 !asp address-size prefix (0x67) not permitted
  166. * \366 operand-size prefix (0x66) used as opcode extension
  167. * \367 address-size prefix (0x67) used as opcode extension
  168. * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
  169. * jmp8 370 is used for Jcc, 371 is used for JMP.
  170. * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
  171. * used for conditional jump over longer jump
  172. * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
  173. * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
  174. * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
  175. */
  176. #include "compiler.h"
  177. #include <stdio.h>
  178. #include <string.h>
  179. #include <stdlib.h>
  180. #include "nasm.h"
  181. #include "nasmlib.h"
  182. #include "error.h"
  183. #include "assemble.h"
  184. #include "insns.h"
  185. #include "tables.h"
  186. #include "disp8.h"
  187. #include "listing.h"
  188. enum match_result {
  189. /*
  190. * Matching errors. These should be sorted so that more specific
  191. * errors come later in the sequence.
  192. */
  193. MERR_INVALOP,
  194. MERR_OPSIZEMISSING,
  195. MERR_OPSIZEMISMATCH,
  196. MERR_BRNOTHERE,
  197. MERR_BRNUMMISMATCH,
  198. MERR_MASKNOTHERE,
  199. MERR_DECONOTHERE,
  200. MERR_BADCPU,
  201. MERR_BADMODE,
  202. MERR_BADHLE,
  203. MERR_ENCMISMATCH,
  204. MERR_BADBND,
  205. MERR_BADREPNE,
  206. MERR_REGSETSIZE,
  207. MERR_REGSET,
  208. /*
  209. * Matching success; the conditional ones first
  210. */
  211. MOK_JUMP, /* Matching OK but needs jmp_match() */
  212. MOK_GOOD /* Matching unconditionally OK */
  213. };
  214. typedef struct {
  215. enum ea_type type; /* what kind of EA is this? */
  216. int sib_present; /* is a SIB byte necessary? */
  217. int bytes; /* # of bytes of offset needed */
  218. int size; /* lazy - this is sib+bytes+1 */
  219. uint8_t modrm, sib, rex, rip; /* the bytes themselves */
  220. int8_t disp8; /* compressed displacement for EVEX */
  221. } ea;
  222. #define GEN_SIB(scale, index, base) \
  223. (((scale) << 6) | ((index) << 3) | ((base)))
  224. #define GEN_MODRM(mod, reg, rm) \
  225. (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
  226. static int64_t calcsize(int32_t, int64_t, int, insn *,
  227. const struct itemplate *);
  228. static int emit_prefix(struct out_data *data, const int bits, insn *ins);
  229. static void gencode(struct out_data *data, insn *ins);
  230. static enum match_result find_match(const struct itemplate **tempp,
  231. insn *instruction,
  232. int32_t segment, int64_t offset, int bits);
  233. static enum match_result matches(const struct itemplate *, insn *, int bits);
  234. static opflags_t regflag(const operand *);
  235. static int32_t regval(const operand *);
  236. static int rexflags(int, opflags_t, int);
  237. static int op_rexflags(const operand *, int);
  238. static int op_evexflags(const operand *, int, uint8_t);
  239. static void add_asp(insn *, int);
  240. static enum ea_type process_ea(operand *, ea *, int, int,
  241. opflags_t, insn *, const char **);
  242. static inline bool absolute_op(const struct operand *o)
  243. {
  244. return o->segment == NO_SEG && o->wrt == NO_SEG &&
  245. !(o->opflags & OPFLAG_RELATIVE);
  246. }
  247. static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
  248. {
  249. return ins->prefixes[pos] == prefix;
  250. }
  251. static void assert_no_prefix(insn * ins, enum prefix_pos pos)
  252. {
  253. if (ins->prefixes[pos])
  254. nasm_error(ERR_NONFATAL, "invalid %s prefix",
  255. prefix_name(ins->prefixes[pos]));
  256. }
  257. static const char *size_name(int size)
  258. {
  259. switch (size) {
  260. case 1:
  261. return "byte";
  262. case 2:
  263. return "word";
  264. case 4:
  265. return "dword";
  266. case 8:
  267. return "qword";
  268. case 10:
  269. return "tword";
  270. case 16:
  271. return "oword";
  272. case 32:
  273. return "yword";
  274. case 64:
  275. return "zword";
  276. default:
  277. return "???";
  278. }
  279. }
  280. static void warn_overflow(int size)
  281. {
  282. nasm_error(ERR_WARNING | ERR_PASS2 | WARN_NOV,
  283. "%s data exceeds bounds", size_name(size));
  284. }
  285. static void warn_overflow_const(int64_t data, int size)
  286. {
  287. if (overflow_general(data, size))
  288. warn_overflow(size);
  289. }
  290. static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
  291. {
  292. bool err;
  293. switch (sign) {
  294. case OUT_WRAP:
  295. err = overflow_general(data, size);
  296. break;
  297. case OUT_SIGNED:
  298. err = overflow_signed(data, size);
  299. break;
  300. case OUT_UNSIGNED:
  301. err = overflow_unsigned(data, size);
  302. break;
  303. default:
  304. panic();
  305. break;
  306. }
  307. if (err)
  308. warn_overflow(size);
  309. }
  310. /*
  311. * This routine wrappers the real output format's output routine,
  312. * in order to pass a copy of the data off to the listing file
  313. * generator at the same time, flatten unnecessary relocations,
  314. * and verify backend compatibility.
  315. */
  316. static void out(struct out_data *data)
  317. {
  318. static int32_t lineno = 0; /* static!!! */
  319. static const char *lnfname = NULL;
  320. union {
  321. uint8_t b[8];
  322. uint64_t q;
  323. } xdata;
  324. size_t asize, amax;
  325. uint64_t zeropad = 0;
  326. int64_t addrval;
  327. int32_t fixseg; /* Segment for which to produce fixed data */
  328. if (!data->size)
  329. return; /* Nothing to do */
  330. /*
  331. * Convert addresses to RAWDATA if possible
  332. * XXX: not all backends want this for global symbols!!!!
  333. */
  334. switch (data->type) {
  335. case OUT_ADDRESS:
  336. addrval = data->toffset;
  337. fixseg = NO_SEG; /* Absolute address is fixed data */
  338. goto address;
  339. case OUT_RELADDR:
  340. addrval = data->toffset - data->relbase;
  341. fixseg = data->segment; /* Our own segment is fixed data */
  342. goto address;
  343. address:
  344. nasm_assert(data->size <= 8);
  345. asize = data->size;
  346. amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
  347. if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
  348. data->twrt == NO_SEG) {
  349. warn_overflow_out(addrval, asize, data->sign);
  350. xdata.q = cpu_to_le64(addrval);
  351. data->data = xdata.b;
  352. data->type = OUT_RAWDATA;
  353. asize = amax = 0; /* No longer an address */
  354. }
  355. break;
  356. case OUT_SEGMENT:
  357. nasm_assert(data->size <= 8);
  358. asize = data->size;
  359. amax = 2;
  360. break;
  361. default:
  362. asize = amax = 0; /* Not an address */
  363. break;
  364. }
  365. /*
  366. * this call to src_get determines when we call the
  367. * debug-format-specific "linenum" function
  368. * it updates lineno and lnfname to the current values
  369. * returning 0 if "same as last time", -2 if lnfname
  370. * changed, and the amount by which lineno changed,
  371. * if it did. thus, these variables must be static
  372. */
  373. if (src_get(&lineno, &lnfname))
  374. dfmt->linenum(lnfname, lineno, data->segment);
  375. if (asize > amax) {
  376. if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
  377. nasm_error(ERR_NONFATAL,
  378. "%u-bit signed relocation unsupported by output format %s",
  379. (unsigned int)(asize << 3), ofmt->shortname);
  380. } else {
  381. nasm_error(ERR_WARNING | WARN_ZEXTRELOC,
  382. "%u-bit %s relocation zero-extended from %u bits",
  383. (unsigned int)(asize << 3),
  384. data->type == OUT_SEGMENT ? "segment" : "unsigned",
  385. (unsigned int)(amax << 3));
  386. }
  387. zeropad = data->size - amax;
  388. data->size = amax;
  389. }
  390. lfmt->output(data);
  391. ofmt->output(data);
  392. data->offset += data->size;
  393. data->insoffs += data->size;
  394. if (zeropad) {
  395. data->type = OUT_ZERODATA;
  396. data->size = zeropad;
  397. lfmt->output(data);
  398. ofmt->output(data);
  399. data->offset += zeropad;
  400. data->insoffs += zeropad;
  401. data->size += zeropad; /* Restore original size value */
  402. }
  403. }
  404. static inline void out_rawdata(struct out_data *data, const void *rawdata,
  405. size_t size)
  406. {
  407. data->type = OUT_RAWDATA;
  408. data->data = rawdata;
  409. data->size = size;
  410. out(data);
  411. }
  412. static void out_rawbyte(struct out_data *data, uint8_t byte)
  413. {
  414. data->type = OUT_RAWDATA;
  415. data->data = &byte;
  416. data->size = 1;
  417. out(data);
  418. }
  419. static inline void out_reserve(struct out_data *data, uint64_t size)
  420. {
  421. data->type = OUT_RESERVE;
  422. data->size = size;
  423. out(data);
  424. }
  425. static void out_segment(struct out_data *data, const struct operand *opx)
  426. {
  427. if (opx->opflags & OPFLAG_RELATIVE)
  428. nasm_error(ERR_NONFATAL, "segment references cannot be relative");
  429. data->type = OUT_SEGMENT;
  430. data->sign = OUT_UNSIGNED;
  431. data->size = 2;
  432. data->toffset = opx->offset;
  433. data->tsegment = ofmt->segbase(opx->segment | 1);
  434. data->twrt = opx->wrt;
  435. out(data);
  436. }
  437. static void out_imm(struct out_data *data, const struct operand *opx,
  438. int size, enum out_sign sign)
  439. {
  440. if (opx->segment != NO_SEG && (opx->segment & 1)) {
  441. /*
  442. * This is actually a segment reference, but eval() has
  443. * already called ofmt->segbase() for us. Sigh.
  444. */
  445. if (size < 2)
  446. nasm_error(ERR_NONFATAL, "segment reference must be 16 bits");
  447. data->type = OUT_SEGMENT;
  448. } else {
  449. data->type = (opx->opflags & OPFLAG_RELATIVE)
  450. ? OUT_RELADDR : OUT_ADDRESS;
  451. }
  452. data->sign = sign;
  453. data->toffset = opx->offset;
  454. data->tsegment = opx->segment;
  455. data->twrt = opx->wrt;
  456. /*
  457. * XXX: improve this if at some point in the future we can
  458. * distinguish the subtrahend in expressions like [foo - bar]
  459. * where bar is a symbol in the current segment. However, at the
  460. * current point, if OPFLAG_RELATIVE is set that subtraction has
  461. * already occurred.
  462. */
  463. data->relbase = 0;
  464. data->size = size;
  465. out(data);
  466. }
  467. static void out_reladdr(struct out_data *data, const struct operand *opx,
  468. int size)
  469. {
  470. if (opx->opflags & OPFLAG_RELATIVE)
  471. nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
  472. data->type = OUT_RELADDR;
  473. data->sign = OUT_SIGNED;
  474. data->size = size;
  475. data->toffset = opx->offset;
  476. data->tsegment = opx->segment;
  477. data->twrt = opx->wrt;
  478. data->relbase = data->offset + (data->inslen - data->insoffs);
  479. out(data);
  480. }
  481. static bool jmp_match(int32_t segment, int64_t offset, int bits,
  482. insn * ins, const struct itemplate *temp)
  483. {
  484. int64_t isize;
  485. const uint8_t *code = temp->code;
  486. uint8_t c = code[0];
  487. bool is_byte;
  488. if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
  489. return false;
  490. if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
  491. return false;
  492. if (optimizing.level < 0 && c == 0371)
  493. return false;
  494. isize = calcsize(segment, offset, bits, ins, temp);
  495. if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
  496. /* Be optimistic in pass 1 */
  497. return true;
  498. if (ins->oprs[0].segment != segment)
  499. return false;
  500. isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
  501. is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
  502. if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
  503. /* jmp short (opcode eb) cannot be used with bnd prefix. */
  504. ins->prefixes[PPS_REP] = P_none;
  505. nasm_error(ERR_WARNING | WARN_BND | ERR_PASS2 ,
  506. "jmp short does not init bnd regs - bnd prefix dropped.");
  507. }
  508. return is_byte;
  509. }
  510. /* This is totally just a wild guess what is reasonable... */
  511. #define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
  512. int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
  513. {
  514. struct out_data data;
  515. const struct itemplate *temp;
  516. enum match_result m;
  517. int64_t wsize; /* size for DB etc. */
  518. nasm_zero(data);
  519. data.offset = start;
  520. data.segment = segment;
  521. data.itemp = NULL;
  522. data.bits = bits;
  523. wsize = db_bytes(instruction->opcode);
  524. if (wsize == -1)
  525. return 0;
  526. if (wsize) {
  527. extop *e;
  528. list_for_each(e, instruction->eops) {
  529. if (e->type == EOT_DB_NUMBER) {
  530. if (wsize > 8) {
  531. nasm_error(ERR_NONFATAL,
  532. "integer supplied to a DT, DO, DY or DZ"
  533. " instruction");
  534. } else {
  535. data.insoffs = 0;
  536. data.inslen = data.size = wsize;
  537. data.toffset = e->offset;
  538. data.twrt = e->wrt;
  539. data.relbase = 0;
  540. if (e->segment != NO_SEG && (e->segment & 1)) {
  541. data.tsegment = e->segment;
  542. data.type = OUT_SEGMENT;
  543. data.sign = OUT_UNSIGNED;
  544. } else {
  545. data.tsegment = e->segment;
  546. data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
  547. data.sign = OUT_WRAP;
  548. }
  549. out(&data);
  550. }
  551. } else if (e->type == EOT_DB_STRING ||
  552. e->type == EOT_DB_STRING_FREE) {
  553. int align = e->stringlen % wsize;
  554. if (align)
  555. align = wsize - align;
  556. data.insoffs = 0;
  557. data.inslen = e->stringlen + align;
  558. out_rawdata(&data, e->stringval, e->stringlen);
  559. out_rawdata(&data, zero_buffer, align);
  560. }
  561. }
  562. } else if (instruction->opcode == I_INCBIN) {
  563. const char *fname = instruction->eops->stringval;
  564. FILE *fp;
  565. size_t t = instruction->times; /* INCBIN handles TIMES by itself */
  566. off_t base = 0;
  567. off_t len;
  568. const void *map = NULL;
  569. char *buf = NULL;
  570. size_t blk = 0; /* Buffered I/O block size */
  571. size_t m = 0; /* Bytes last read */
  572. if (!t)
  573. goto done;
  574. fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
  575. if (!fp) {
  576. nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
  577. fname);
  578. goto done;
  579. }
  580. len = nasm_file_size(fp);
  581. if (len == (off_t)-1) {
  582. nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
  583. fname);
  584. goto close_done;
  585. }
  586. if (instruction->eops->next) {
  587. base = instruction->eops->next->offset;
  588. if (base >= len) {
  589. len = 0;
  590. } else {
  591. len -= base;
  592. if (instruction->eops->next->next &&
  593. len > (off_t)instruction->eops->next->next->offset)
  594. len = (off_t)instruction->eops->next->next->offset;
  595. }
  596. }
  597. lfmt->set_offset(data.offset);
  598. lfmt->uplevel(LIST_INCBIN);
  599. if (!len)
  600. goto end_incbin;
  601. /* Try to map file data */
  602. map = nasm_map_file(fp, base, len);
  603. if (!map) {
  604. blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
  605. buf = nasm_malloc(blk);
  606. }
  607. while (t--) {
  608. /*
  609. * Consider these irrelevant for INCBIN, since it is fully
  610. * possible that these might be (way) bigger than an int
  611. * can hold; there is, however, no reason to widen these
  612. * types just for INCBIN. data.inslen == 0 signals to the
  613. * backend that these fields are meaningless, if at all
  614. * needed.
  615. */
  616. data.insoffs = 0;
  617. data.inslen = 0;
  618. if (map) {
  619. out_rawdata(&data, map, len);
  620. } else if ((off_t)m == len) {
  621. out_rawdata(&data, buf, len);
  622. } else {
  623. off_t l = len;
  624. if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
  625. nasm_error(ERR_NONFATAL,
  626. "`incbin': unable to seek on file `%s'",
  627. fname);
  628. goto end_incbin;
  629. }
  630. while (l > 0) {
  631. m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
  632. if (!m || feof(fp)) {
  633. /*
  634. * This shouldn't happen unless the file
  635. * actually changes while we are reading
  636. * it.
  637. */
  638. nasm_error(ERR_NONFATAL,
  639. "`incbin': unexpected EOF while"
  640. " reading file `%s'", fname);
  641. goto end_incbin;
  642. }
  643. out_rawdata(&data, buf, m);
  644. l -= m;
  645. }
  646. }
  647. }
  648. end_incbin:
  649. lfmt->downlevel(LIST_INCBIN);
  650. if (instruction->times > 1) {
  651. lfmt->uplevel(LIST_TIMES);
  652. lfmt->downlevel(LIST_TIMES);
  653. }
  654. if (ferror(fp)) {
  655. nasm_error(ERR_NONFATAL,
  656. "`incbin': error while"
  657. " reading file `%s'", fname);
  658. }
  659. close_done:
  660. if (buf)
  661. nasm_free(buf);
  662. if (map)
  663. nasm_unmap_file(map, len);
  664. fclose(fp);
  665. done:
  666. instruction->times = 1; /* Tell the upper layer not to iterate */
  667. ;
  668. } else {
  669. /* "Real" instruction */
  670. /* Check to see if we need an address-size prefix */
  671. add_asp(instruction, bits);
  672. m = find_match(&temp, instruction, data.segment, data.offset, bits);
  673. if (m == MOK_GOOD) {
  674. /* Matches! */
  675. int64_t insn_size = calcsize(data.segment, data.offset,
  676. bits, instruction, temp);
  677. nasm_assert(insn_size >= 0);
  678. data.itemp = temp;
  679. data.bits = bits;
  680. data.insoffs = 0;
  681. data.inslen = insn_size;
  682. gencode(&data, instruction);
  683. nasm_assert(data.insoffs == insn_size);
  684. } else {
  685. /* No match */
  686. switch (m) {
  687. case MERR_OPSIZEMISSING:
  688. nasm_error(ERR_NONFATAL, "operation size not specified");
  689. break;
  690. case MERR_OPSIZEMISMATCH:
  691. nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
  692. break;
  693. case MERR_BRNOTHERE:
  694. nasm_error(ERR_NONFATAL,
  695. "broadcast not permitted on this operand");
  696. break;
  697. case MERR_BRNUMMISMATCH:
  698. nasm_error(ERR_NONFATAL,
  699. "mismatch in the number of broadcasting elements");
  700. break;
  701. case MERR_MASKNOTHERE:
  702. nasm_error(ERR_NONFATAL,
  703. "mask not permitted on this operand");
  704. break;
  705. case MERR_DECONOTHERE:
  706. nasm_error(ERR_NONFATAL, "unsupported mode decorator for instruction");
  707. break;
  708. case MERR_BADCPU:
  709. nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
  710. break;
  711. case MERR_BADMODE:
  712. nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
  713. bits);
  714. break;
  715. case MERR_ENCMISMATCH:
  716. nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
  717. break;
  718. case MERR_BADBND:
  719. nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
  720. break;
  721. case MERR_BADREPNE:
  722. nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
  723. (has_prefix(instruction, PPS_REP, P_REPNE) ?
  724. "repne" : "repnz"));
  725. break;
  726. case MERR_REGSETSIZE:
  727. nasm_error(ERR_NONFATAL, "invalid register set size");
  728. break;
  729. case MERR_REGSET:
  730. nasm_error(ERR_NONFATAL, "register set not valid for operand");
  731. break;
  732. default:
  733. nasm_error(ERR_NONFATAL,
  734. "invalid combination of opcode and operands");
  735. break;
  736. }
  737. instruction->times = 1; /* Avoid repeated error messages */
  738. }
  739. }
  740. return data.offset - start;
  741. }
  742. int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
  743. {
  744. const struct itemplate *temp;
  745. enum match_result m;
  746. if (instruction->opcode == I_none)
  747. return 0;
  748. if (opcode_is_db(instruction->opcode)) {
  749. extop *e;
  750. int32_t isize, osize, wsize;
  751. isize = 0;
  752. wsize = db_bytes(instruction->opcode);
  753. nasm_assert(wsize > 0);
  754. list_for_each(e, instruction->eops) {
  755. int32_t align;
  756. osize = 0;
  757. if (e->type == EOT_DB_NUMBER) {
  758. osize = 1;
  759. warn_overflow_const(e->offset, wsize);
  760. } else if (e->type == EOT_DB_STRING ||
  761. e->type == EOT_DB_STRING_FREE)
  762. osize = e->stringlen;
  763. align = (-osize) % wsize;
  764. if (align < 0)
  765. align += wsize;
  766. isize += osize + align;
  767. }
  768. return isize;
  769. }
  770. if (instruction->opcode == I_INCBIN) {
  771. const char *fname = instruction->eops->stringval;
  772. off_t len;
  773. len = nasm_file_size_by_path(fname);
  774. if (len == (off_t)-1) {
  775. nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
  776. fname);
  777. return 0;
  778. }
  779. if (instruction->eops->next) {
  780. if (len <= (off_t)instruction->eops->next->offset) {
  781. len = 0;
  782. } else {
  783. len -= instruction->eops->next->offset;
  784. if (instruction->eops->next->next &&
  785. len > (off_t)instruction->eops->next->next->offset) {
  786. len = (off_t)instruction->eops->next->next->offset;
  787. }
  788. }
  789. }
  790. len *= instruction->times;
  791. instruction->times = 1; /* Tell the upper layer to not iterate */
  792. return len;
  793. }
  794. /* Check to see if we need an address-size prefix */
  795. add_asp(instruction, bits);
  796. m = find_match(&temp, instruction, segment, offset, bits);
  797. if (m == MOK_GOOD) {
  798. /* we've matched an instruction. */
  799. return calcsize(segment, offset, bits, instruction, temp);
  800. } else {
  801. return -1; /* didn't match any instruction */
  802. }
  803. }
  804. static void bad_hle_warn(const insn * ins, uint8_t hleok)
  805. {
  806. enum prefixes rep_pfx = ins->prefixes[PPS_REP];
  807. enum whatwarn { w_none, w_lock, w_inval } ww;
  808. static const enum whatwarn warn[2][4] =
  809. {
  810. { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
  811. { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
  812. };
  813. unsigned int n;
  814. n = (unsigned int)rep_pfx - P_XACQUIRE;
  815. if (n > 1)
  816. return; /* Not XACQUIRE/XRELEASE */
  817. ww = warn[n][hleok];
  818. if (!is_class(MEMORY, ins->oprs[0].type))
  819. ww = w_inval; /* HLE requires operand 0 to be memory */
  820. switch (ww) {
  821. case w_none:
  822. break;
  823. case w_lock:
  824. if (ins->prefixes[PPS_LOCK] != P_LOCK) {
  825. nasm_error(ERR_WARNING | WARN_HLE | ERR_PASS2,
  826. "%s with this instruction requires lock",
  827. prefix_name(rep_pfx));
  828. }
  829. break;
  830. case w_inval:
  831. nasm_error(ERR_WARNING | WARN_HLE | ERR_PASS2,
  832. "%s invalid with this instruction",
  833. prefix_name(rep_pfx));
  834. break;
  835. }
  836. }
  837. /* Common construct */
  838. #define case3(x) case (x): case (x)+1: case (x)+2
  839. #define case4(x) case3(x): case (x)+3
  840. static int64_t calcsize(int32_t segment, int64_t offset, int bits,
  841. insn * ins, const struct itemplate *temp)
  842. {
  843. const uint8_t *codes = temp->code;
  844. int64_t length = 0;
  845. uint8_t c;
  846. int rex_mask = ~0;
  847. int op1, op2;
  848. struct operand *opx;
  849. uint8_t opex = 0;
  850. enum ea_type eat;
  851. uint8_t hleok = 0;
  852. bool lockcheck = true;
  853. enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
  854. const char *errmsg;
  855. ins->rex = 0; /* Ensure REX is reset */
  856. eat = EA_SCALAR; /* Expect a scalar EA */
  857. memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
  858. if (ins->prefixes[PPS_OSIZE] == P_O64)
  859. ins->rex |= REX_W;
  860. (void)segment; /* Don't warn that this parameter is unused */
  861. (void)offset; /* Don't warn that this parameter is unused */
  862. while (*codes) {
  863. c = *codes++;
  864. op1 = (c & 3) + ((opex & 1) << 2);
  865. op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
  866. opx = &ins->oprs[op1];
  867. opex = 0; /* For the next iteration */
  868. switch (c) {
  869. case4(01):
  870. codes += c, length += c;
  871. break;
  872. case3(05):
  873. opex = c;
  874. break;
  875. case4(010):
  876. ins->rex |=
  877. op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
  878. codes++, length++;
  879. break;
  880. case4(014):
  881. /* this is an index reg of MIB operand */
  882. mib_index = opx->basereg;
  883. break;
  884. case4(020):
  885. case4(024):
  886. length++;
  887. break;
  888. case4(030):
  889. length += 2;
  890. break;
  891. case4(034):
  892. if (opx->type & (BITS16 | BITS32 | BITS64))
  893. length += (opx->type & BITS16) ? 2 : 4;
  894. else
  895. length += (bits == 16) ? 2 : 4;
  896. break;
  897. case4(040):
  898. length += 4;
  899. break;
  900. case4(044):
  901. length += ins->addr_size >> 3;
  902. break;
  903. case4(050):
  904. length++;
  905. break;
  906. case4(054):
  907. length += 8; /* MOV reg64/imm */
  908. break;
  909. case4(060):
  910. length += 2;
  911. break;
  912. case4(064):
  913. if (opx->type & (BITS16 | BITS32 | BITS64))
  914. length += (opx->type & BITS16) ? 2 : 4;
  915. else
  916. length += (bits == 16) ? 2 : 4;
  917. break;
  918. case4(070):
  919. length += 4;
  920. break;
  921. case4(074):
  922. length += 2;
  923. break;
  924. case 0172:
  925. case 0173:
  926. codes++;
  927. length++;
  928. break;
  929. case4(0174):
  930. length++;
  931. break;
  932. case4(0240):
  933. ins->rex |= REX_EV;
  934. ins->vexreg = regval(opx);
  935. ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
  936. ins->vex_cm = *codes++;
  937. ins->vex_wlp = *codes++;
  938. ins->evex_tuple = (*codes++ - 0300);
  939. break;
  940. case 0250:
  941. ins->rex |= REX_EV;
  942. ins->vexreg = 0;
  943. ins->vex_cm = *codes++;
  944. ins->vex_wlp = *codes++;
  945. ins->evex_tuple = (*codes++ - 0300);
  946. break;
  947. case4(0254):
  948. length += 4;
  949. break;
  950. case4(0260):
  951. ins->rex |= REX_V;
  952. ins->vexreg = regval(opx);
  953. ins->vex_cm = *codes++;
  954. ins->vex_wlp = *codes++;
  955. break;
  956. case 0270:
  957. ins->rex |= REX_V;
  958. ins->vexreg = 0;
  959. ins->vex_cm = *codes++;
  960. ins->vex_wlp = *codes++;
  961. break;
  962. case3(0271):
  963. hleok = c & 3;
  964. break;
  965. case4(0274):
  966. length++;
  967. break;
  968. case4(0300):
  969. break;
  970. case 0310:
  971. if (bits == 64)
  972. return -1;
  973. length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
  974. break;
  975. case 0311:
  976. length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
  977. break;
  978. case 0312:
  979. break;
  980. case 0313:
  981. if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
  982. has_prefix(ins, PPS_ASIZE, P_A32))
  983. return -1;
  984. break;
  985. case4(0314):
  986. break;
  987. case 0320:
  988. {
  989. enum prefixes pfx = ins->prefixes[PPS_OSIZE];
  990. if (pfx == P_O16)
  991. break;
  992. if (pfx != P_none)
  993. nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
  994. else
  995. ins->prefixes[PPS_OSIZE] = P_O16;
  996. break;
  997. }
  998. case 0321:
  999. {
  1000. enum prefixes pfx = ins->prefixes[PPS_OSIZE];
  1001. if (pfx == P_O32)
  1002. break;
  1003. if (pfx != P_none)
  1004. nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
  1005. else
  1006. ins->prefixes[PPS_OSIZE] = P_O32;
  1007. break;
  1008. }
  1009. case 0322:
  1010. break;
  1011. case 0323:
  1012. rex_mask &= ~REX_W;
  1013. break;
  1014. case 0324:
  1015. ins->rex |= REX_W;
  1016. break;
  1017. case 0325:
  1018. ins->rex |= REX_NH;
  1019. break;
  1020. case 0326:
  1021. break;
  1022. case 0330:
  1023. codes++, length++;
  1024. break;
  1025. case 0331:
  1026. break;
  1027. case 0332:
  1028. case 0333:
  1029. length++;
  1030. break;
  1031. case 0334:
  1032. ins->rex |= REX_L;
  1033. break;
  1034. case 0335:
  1035. break;
  1036. case 0336:
  1037. if (!ins->prefixes[PPS_REP])
  1038. ins->prefixes[PPS_REP] = P_REP;
  1039. break;
  1040. case 0337:
  1041. if (!ins->prefixes[PPS_REP])
  1042. ins->prefixes[PPS_REP] = P_REPNE;
  1043. break;
  1044. case 0340:
  1045. if (!absolute_op(&ins->oprs[0]))
  1046. nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
  1047. " quantity of BSS space");
  1048. else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
  1049. nasm_error(ERR_WARNING | ERR_PASS1,
  1050. "forward reference in RESx can have unpredictable results");
  1051. else
  1052. length += ins->oprs[0].offset;
  1053. break;
  1054. case 0341:
  1055. if (!ins->prefixes[PPS_WAIT])
  1056. ins->prefixes[PPS_WAIT] = P_WAIT;
  1057. break;
  1058. case 0360:
  1059. break;
  1060. case 0361:
  1061. length++;
  1062. break;
  1063. case 0364:
  1064. case 0365:
  1065. break;
  1066. case 0366:
  1067. case 0367:
  1068. length++;
  1069. break;
  1070. case 0370:
  1071. case 0371:
  1072. break;
  1073. case 0373:
  1074. length++;
  1075. break;
  1076. case 0374:
  1077. eat = EA_XMMVSIB;
  1078. break;
  1079. case 0375:
  1080. eat = EA_YMMVSIB;
  1081. break;
  1082. case 0376:
  1083. eat = EA_ZMMVSIB;
  1084. break;
  1085. case4(0100):
  1086. case4(0110):
  1087. case4(0120):
  1088. case4(0130):
  1089. case4(0200):
  1090. case4(0204):
  1091. case4(0210):
  1092. case4(0214):
  1093. case4(0220):
  1094. case4(0224):
  1095. case4(0230):
  1096. case4(0234):
  1097. {
  1098. ea ea_data;
  1099. int rfield;
  1100. opflags_t rflags;
  1101. struct operand *opy = &ins->oprs[op2];
  1102. struct operand *op_er_sae;
  1103. ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
  1104. if (c <= 0177) {
  1105. /* pick rfield from operand b (opx) */
  1106. rflags = regflag(opx);
  1107. rfield = nasm_regvals[opx->basereg];
  1108. } else {
  1109. rflags = 0;
  1110. rfield = c & 7;
  1111. }
  1112. /* EVEX.b1 : evex_brerop contains the operand position */
  1113. op_er_sae = (ins->evex_brerop >= 0 ?
  1114. &ins->oprs[ins->evex_brerop] : NULL);
  1115. if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
  1116. /* set EVEX.b */
  1117. ins->evex_p[2] |= EVEX_P2B;
  1118. if (op_er_sae->decoflags & ER) {
  1119. /* set EVEX.RC (rounding control) */
  1120. ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
  1121. & EVEX_P2RC;
  1122. }
  1123. } else {
  1124. /* set EVEX.L'L (vector length) */
  1125. ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
  1126. ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
  1127. if (opy->decoflags & BRDCAST_MASK) {
  1128. /* set EVEX.b */
  1129. ins->evex_p[2] |= EVEX_P2B;
  1130. }
  1131. }
  1132. if (itemp_has(temp, IF_MIB)) {
  1133. opy->eaflags |= EAF_MIB;
  1134. /*
  1135. * if a separate form of MIB (ICC style) is used,
  1136. * the index reg info is merged into mem operand
  1137. */
  1138. if (mib_index != R_none) {
  1139. opy->indexreg = mib_index;
  1140. opy->scale = 1;
  1141. opy->hintbase = mib_index;
  1142. opy->hinttype = EAH_NOTBASE;
  1143. }
  1144. }
  1145. if (process_ea(opy, &ea_data, bits,
  1146. rfield, rflags, ins, &errmsg) != eat) {
  1147. nasm_error(ERR_NONFATAL, "%s", errmsg);
  1148. return -1;
  1149. } else {
  1150. ins->rex |= ea_data.rex;
  1151. length += ea_data.size;
  1152. }
  1153. }
  1154. break;
  1155. default:
  1156. nasm_panic(0, "internal instruction table corrupt"
  1157. ": instruction code \\%o (0x%02X) given", c, c);
  1158. break;
  1159. }
  1160. }
  1161. ins->rex &= rex_mask;
  1162. if (ins->rex & REX_NH) {
  1163. if (ins->rex & REX_H) {
  1164. nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
  1165. return -1;
  1166. }
  1167. ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
  1168. }
  1169. switch (ins->prefixes[PPS_VEX]) {
  1170. case P_EVEX:
  1171. if (!(ins->rex & REX_EV))
  1172. return -1;
  1173. break;
  1174. case P_VEX3:
  1175. case P_VEX2:
  1176. if (!(ins->rex & REX_V))
  1177. return -1;
  1178. break;
  1179. default:
  1180. break;
  1181. }
  1182. if (ins->rex & (REX_V | REX_EV)) {
  1183. int bad32 = REX_R|REX_W|REX_X|REX_B;
  1184. if (ins->rex & REX_H) {
  1185. nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
  1186. return -1;
  1187. }
  1188. switch (ins->vex_wlp & 060) {
  1189. case 000:
  1190. case 040:
  1191. ins->rex &= ~REX_W;
  1192. break;
  1193. case 020:
  1194. ins->rex |= REX_W;
  1195. bad32 &= ~REX_W;
  1196. break;
  1197. case 060:
  1198. /* Follow REX_W */
  1199. break;
  1200. }
  1201. if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
  1202. nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
  1203. return -1;
  1204. } else if (!(ins->rex & REX_EV) &&
  1205. ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
  1206. nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
  1207. return -1;
  1208. }
  1209. if (ins->rex & REX_EV)
  1210. length += 4;
  1211. else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
  1212. ins->prefixes[PPS_VEX] == P_VEX3)
  1213. length += 3;
  1214. else
  1215. length += 2;
  1216. } else if (ins->rex & REX_MASK) {
  1217. if (ins->rex & REX_H) {
  1218. nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
  1219. return -1;
  1220. } else if (bits == 64) {
  1221. length++;
  1222. } else if ((ins->rex & REX_L) &&
  1223. !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
  1224. iflag_cpu_level_ok(&cpu, IF_X86_64)) {
  1225. /* LOCK-as-REX.R */
  1226. assert_no_prefix(ins, PPS_LOCK);
  1227. lockcheck = false; /* Already errored, no need for warning */
  1228. length++;
  1229. } else {
  1230. nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
  1231. return -1;
  1232. }
  1233. }
  1234. if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
  1235. (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
  1236. nasm_error(ERR_WARNING | WARN_LOCK | ERR_PASS2 ,
  1237. "instruction is not lockable");
  1238. }
  1239. bad_hle_warn(ins, hleok);
  1240. /*
  1241. * when BND prefix is set by DEFAULT directive,
  1242. * BND prefix is added to every appropriate instruction line
  1243. * unless it is overridden by NOBND prefix.
  1244. */
  1245. if (globalbnd &&
  1246. (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
  1247. ins->prefixes[PPS_REP] = P_BND;
  1248. /*
  1249. * Add length of legacy prefixes
  1250. */
  1251. length += emit_prefix(NULL, bits, ins);
  1252. return length;
  1253. }
  1254. static inline void emit_rex(struct out_data *data, insn *ins)
  1255. {
  1256. if (data->bits == 64) {
  1257. if ((ins->rex & REX_MASK) &&
  1258. !(ins->rex & (REX_V | REX_EV)) &&
  1259. !ins->rex_done) {
  1260. uint8_t rex = (ins->rex & REX_MASK) | REX_P;
  1261. out_rawbyte(data, rex);
  1262. ins->rex_done = true;
  1263. }
  1264. }
  1265. }
  1266. static int emit_prefix(struct out_data *data, const int bits, insn *ins)
  1267. {
  1268. int bytes = 0;
  1269. int j;
  1270. for (j = 0; j < MAXPREFIX; j++) {
  1271. uint8_t c = 0;
  1272. switch (ins->prefixes[j]) {
  1273. case P_WAIT:
  1274. c = 0x9B;
  1275. break;
  1276. case P_LOCK:
  1277. c = 0xF0;
  1278. break;
  1279. case P_REPNE:
  1280. case P_REPNZ:
  1281. case P_XACQUIRE:
  1282. case P_BND:
  1283. c = 0xF2;
  1284. break;
  1285. case P_REPE:
  1286. case P_REPZ:
  1287. case P_REP:
  1288. case P_XRELEASE:
  1289. c = 0xF3;
  1290. break;
  1291. case R_CS:
  1292. if (bits == 64) {
  1293. nasm_error(ERR_WARNING | ERR_PASS2,
  1294. "cs segment base generated, but will be ignored in 64-bit mode");
  1295. }
  1296. c = 0x2E;
  1297. break;
  1298. case R_DS:
  1299. if (bits == 64) {
  1300. nasm_error(ERR_WARNING | ERR_PASS2,
  1301. "ds segment base generated, but will be ignored in 64-bit mode");
  1302. }
  1303. c = 0x3E;
  1304. break;
  1305. case R_ES:
  1306. if (bits == 64) {
  1307. nasm_error(ERR_WARNING | ERR_PASS2,
  1308. "es segment base generated, but will be ignored in 64-bit mode");
  1309. }
  1310. c = 0x26;
  1311. break;
  1312. case R_FS:
  1313. c = 0x64;
  1314. break;
  1315. case R_GS:
  1316. c = 0x65;
  1317. break;
  1318. case R_SS:
  1319. if (bits == 64) {
  1320. nasm_error(ERR_WARNING | ERR_PASS2,
  1321. "ss segment base generated, but will be ignored in 64-bit mode");
  1322. }
  1323. c = 0x36;
  1324. break;
  1325. case R_SEGR6:
  1326. case R_SEGR7:
  1327. nasm_error(ERR_NONFATAL,
  1328. "segr6 and segr7 cannot be used as prefixes");
  1329. break;
  1330. case P_A16:
  1331. if (bits == 64) {
  1332. nasm_error(ERR_NONFATAL,
  1333. "16-bit addressing is not supported "
  1334. "in 64-bit mode");
  1335. } else if (bits != 16)
  1336. c = 0x67;
  1337. break;
  1338. case P_A32:
  1339. if (bits != 32)
  1340. c = 0x67;
  1341. break;
  1342. case P_A64:
  1343. if (bits != 64) {
  1344. nasm_error(ERR_NONFATAL,
  1345. "64-bit addressing is only supported "
  1346. "in 64-bit mode");
  1347. }
  1348. break;
  1349. case P_ASP:
  1350. c = 0x67;
  1351. break;
  1352. case P_O16:
  1353. if (bits != 16)
  1354. c = 0x66;
  1355. break;
  1356. case P_O32:
  1357. if (bits == 16)
  1358. c = 0x66;
  1359. break;
  1360. case P_O64:
  1361. /* REX.W */
  1362. break;
  1363. case P_OSP:
  1364. c = 0x66;
  1365. break;
  1366. case P_EVEX:
  1367. case P_VEX3:
  1368. case P_VEX2:
  1369. case P_NOBND:
  1370. case P_none:
  1371. break;
  1372. default:
  1373. nasm_panic(0, "invalid instruction prefix");
  1374. }
  1375. if (c) {
  1376. if (data)
  1377. out_rawbyte(data, c);
  1378. bytes++;
  1379. }
  1380. }
  1381. return bytes;
  1382. }
  1383. static void gencode(struct out_data *data, insn *ins)
  1384. {
  1385. uint8_t c;
  1386. uint8_t bytes[4];
  1387. int64_t size;
  1388. int op1, op2;
  1389. struct operand *opx;
  1390. const uint8_t *codes = data->itemp->code;
  1391. uint8_t opex = 0;
  1392. enum ea_type eat = EA_SCALAR;
  1393. int r;
  1394. const int bits = data->bits;
  1395. const char *errmsg;
  1396. ins->rex_done = false;
  1397. emit_prefix(data, bits, ins);
  1398. while (*codes) {
  1399. c = *codes++;
  1400. op1 = (c & 3) + ((opex & 1) << 2);
  1401. op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
  1402. opx = &ins->oprs[op1];
  1403. opex = 0; /* For the next iteration */
  1404. switch (c) {
  1405. case 01:
  1406. case 02:
  1407. case 03:
  1408. case 04:
  1409. emit_rex(data, ins);
  1410. out_rawdata(data, codes, c);
  1411. codes += c;
  1412. break;
  1413. case 05:
  1414. case 06:
  1415. case 07:
  1416. opex = c;
  1417. break;
  1418. case4(010):
  1419. emit_rex(data, ins);
  1420. out_rawbyte(data, *codes++ + (regval(opx) & 7));
  1421. break;
  1422. case4(014):
  1423. break;
  1424. case4(020):
  1425. out_imm(data, opx, 1, OUT_WRAP);
  1426. break;
  1427. case4(024):
  1428. out_imm(data, opx, 1, OUT_UNSIGNED);
  1429. break;
  1430. case4(030):
  1431. out_imm(data, opx, 2, OUT_WRAP);
  1432. break;
  1433. case4(034):
  1434. if (opx->type & (BITS16 | BITS32))
  1435. size = (opx->type & BITS16) ? 2 : 4;
  1436. else
  1437. size = (bits == 16) ? 2 : 4;
  1438. out_imm(data, opx, size, OUT_WRAP);
  1439. break;
  1440. case4(040):
  1441. out_imm(data, opx, 4, OUT_WRAP);
  1442. break;
  1443. case4(044):
  1444. size = ins->addr_size >> 3;
  1445. out_imm(data, opx, size, OUT_WRAP);
  1446. break;
  1447. case4(050):
  1448. if (opx->segment == data->segment) {
  1449. int64_t delta = opx->offset - data->offset
  1450. - (data->inslen - data->insoffs);
  1451. if (delta > 127 || delta < -128)
  1452. nasm_error(ERR_NONFATAL, "short jump is out of range");
  1453. }
  1454. out_reladdr(data, opx, 1);
  1455. break;
  1456. case4(054):
  1457. out_imm(data, opx, 8, OUT_WRAP);
  1458. break;
  1459. case4(060):
  1460. out_reladdr(data, opx, 2);
  1461. break;
  1462. case4(064):
  1463. if (opx->type & (BITS16 | BITS32 | BITS64))
  1464. size = (opx->type & BITS16) ? 2 : 4;
  1465. else
  1466. size = (bits == 16) ? 2 : 4;
  1467. out_reladdr(data, opx, size);
  1468. break;
  1469. case4(070):
  1470. out_reladdr(data, opx, 4);
  1471. break;
  1472. case4(074):
  1473. if (opx->segment == NO_SEG)
  1474. nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
  1475. " relocatable");
  1476. out_segment(data, opx);
  1477. break;
  1478. case 0172:
  1479. {
  1480. int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
  1481. const struct operand *opy;
  1482. c = *codes++;
  1483. opx = &ins->oprs[c >> 3];
  1484. opy = &ins->oprs[c & 7];
  1485. if (!absolute_op(opy)) {
  1486. nasm_error(ERR_NONFATAL,
  1487. "non-absolute expression not permitted as argument %d",
  1488. c & 7);
  1489. } else if (opy->offset & ~mask) {
  1490. nasm_error(ERR_WARNING | ERR_PASS2 | WARN_NOV,
  1491. "is4 argument exceeds bounds");
  1492. }
  1493. c = opy->offset & mask;
  1494. goto emit_is4;
  1495. }
  1496. case 0173:
  1497. c = *codes++;
  1498. opx = &ins->oprs[c >> 4];
  1499. c &= 15;
  1500. goto emit_is4;
  1501. case4(0174):
  1502. c = 0;
  1503. emit_is4:
  1504. r = nasm_regvals[opx->basereg];
  1505. out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
  1506. break;
  1507. case4(0254):
  1508. if (absolute_op(opx) &&
  1509. (int32_t)opx->offset != (int64_t)opx->offset) {
  1510. nasm_error(ERR_WARNING | ERR_PASS2 | WARN_NOV,
  1511. "signed dword immediate exceeds bounds");
  1512. }
  1513. out_imm(data, opx, 4, OUT_SIGNED);
  1514. break;
  1515. case4(0240):
  1516. case 0250:
  1517. codes += 3;
  1518. ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
  1519. EVEX_P2Z | EVEX_P2AAA, 2);
  1520. ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
  1521. bytes[0] = 0x62;
  1522. /* EVEX.X can be set by either REX or EVEX for different reasons */
  1523. bytes[1] = ((((ins->rex & 7) << 5) |
  1524. (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
  1525. (ins->vex_cm & EVEX_P0MM);
  1526. bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
  1527. ((~ins->vexreg & 15) << 3) |
  1528. (1 << 2) | (ins->vex_wlp & 3);
  1529. bytes[3] = ins->evex_p[2];
  1530. out_rawdata(data, bytes, 4);
  1531. break;
  1532. case4(0260):
  1533. case 0270:
  1534. codes += 2;
  1535. if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
  1536. ins->prefixes[PPS_VEX] == P_VEX3) {
  1537. bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
  1538. bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
  1539. bytes[2] = ((ins->rex & REX_W) << (7-3)) |
  1540. ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
  1541. out_rawdata(data, bytes, 3);
  1542. } else {
  1543. bytes[0] = 0xc5;
  1544. bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
  1545. ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
  1546. out_rawdata(data, bytes, 2);
  1547. }
  1548. break;
  1549. case 0271:
  1550. case 0272:
  1551. case 0273:
  1552. break;
  1553. case4(0274):
  1554. {
  1555. uint64_t uv, um;
  1556. int s;
  1557. if (absolute_op(opx)) {
  1558. if (ins->rex & REX_W)
  1559. s = 64;
  1560. else if (ins->prefixes[PPS_OSIZE] == P_O16)
  1561. s = 16;
  1562. else if (ins->prefixes[PPS_OSIZE] == P_O32)
  1563. s = 32;
  1564. else
  1565. s = bits;
  1566. um = (uint64_t)2 << (s-1);
  1567. uv = opx->offset;
  1568. if (uv > 127 && uv < (uint64_t)-128 &&
  1569. (uv < um-128 || uv > um-1)) {
  1570. /* If this wasn't explicitly byte-sized, warn as though we
  1571. * had fallen through to the imm16/32/64 case.
  1572. */
  1573. nasm_error(ERR_WARNING | ERR_PASS2 | WARN_NOV,
  1574. "%s value exceeds bounds",
  1575. (opx->type & BITS8) ? "signed byte" :
  1576. s == 16 ? "word" :
  1577. s == 32 ? "dword" :
  1578. "signed dword");
  1579. }
  1580. /* Output as a raw byte to avoid byte overflow check */
  1581. out_rawbyte(data, (uint8_t)uv);
  1582. } else {
  1583. out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
  1584. }
  1585. break;
  1586. }
  1587. case4(0300):
  1588. break;
  1589. case 0310:
  1590. if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
  1591. out_rawbyte(data, 0x67);
  1592. break;
  1593. case 0311:
  1594. if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
  1595. out_rawbyte(data, 0x67);
  1596. break;
  1597. case 0312:
  1598. break;
  1599. case 0313:
  1600. ins->rex = 0;
  1601. break;
  1602. case4(0314):
  1603. break;
  1604. case 0320:
  1605. case 0321:
  1606. break;
  1607. case 0322:
  1608. case 0323:
  1609. break;
  1610. case 0324:
  1611. ins->rex |= REX_W;
  1612. break;
  1613. case 0325:
  1614. break;
  1615. case 0326:
  1616. break;
  1617. case 0330:
  1618. out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
  1619. break;
  1620. case 0331:
  1621. break;
  1622. case 0332:
  1623. case 0333:
  1624. out_rawbyte(data, c - 0332 + 0xF2);
  1625. break;
  1626. case 0334:
  1627. if (ins->rex & REX_R)
  1628. out_rawbyte(data, 0xF0);
  1629. ins->rex &= ~(REX_L|REX_R);
  1630. break;
  1631. case 0335:
  1632. break;
  1633. case 0336:
  1634. case 0337:
  1635. break;
  1636. case 0340:
  1637. if (ins->oprs[0].segment != NO_SEG)
  1638. nasm_panic(0, "non-constant BSS size in pass two");
  1639. out_reserve(data, ins->oprs[0].offset);
  1640. break;
  1641. case 0341:
  1642. break;
  1643. case 0360:
  1644. break;
  1645. case 0361:
  1646. out_rawbyte(data, 0x66);
  1647. break;
  1648. case 0364:
  1649. case 0365:
  1650. break;
  1651. case 0366:
  1652. case 0367:
  1653. out_rawbyte(data, c - 0366 + 0x66);
  1654. break;
  1655. case3(0370):
  1656. break;
  1657. case 0373:
  1658. out_rawbyte(data, bits == 16 ? 3 : 5);
  1659. break;
  1660. case 0374:
  1661. eat = EA_XMMVSIB;
  1662. break;
  1663. case 0375:
  1664. eat = EA_YMMVSIB;
  1665. break;
  1666. case 0376:
  1667. eat = EA_ZMMVSIB;
  1668. break;
  1669. case4(0100):
  1670. case4(0110):
  1671. case4(0120):
  1672. case4(0130):
  1673. case4(0200):
  1674. case4(0204):
  1675. case4(0210):
  1676. case4(0214):
  1677. case4(0220):
  1678. case4(0224):
  1679. case4(0230):
  1680. case4(0234):
  1681. {
  1682. ea ea_data;
  1683. int rfield;
  1684. opflags_t rflags;
  1685. uint8_t *p;
  1686. struct operand *opy = &ins->oprs[op2];
  1687. if (c <= 0177) {
  1688. /* pick rfield from operand b (opx) */
  1689. rflags = regflag(opx);
  1690. rfield = nasm_regvals[opx->basereg];
  1691. } else {
  1692. /* rfield is constant */
  1693. rflags = 0;
  1694. rfield = c & 7;
  1695. }
  1696. if (process_ea(opy, &ea_data, bits,
  1697. rfield, rflags, ins, &errmsg) != eat)
  1698. nasm_error(ERR_NONFATAL, "%s", errmsg);
  1699. p = bytes;
  1700. *p++ = ea_data.modrm;
  1701. if (ea_data.sib_present)
  1702. *p++ = ea_data.sib;
  1703. out_rawdata(data, bytes, p - bytes);
  1704. /*
  1705. * Make sure the address gets the right offset in case
  1706. * the line breaks in the .lst file (BR 1197827)
  1707. */
  1708. if (ea_data.bytes) {
  1709. /* use compressed displacement, if available */
  1710. if (ea_data.disp8) {
  1711. out_rawbyte(data, ea_data.disp8);
  1712. } else if (ea_data.rip) {
  1713. out_reladdr(data, opy, ea_data.bytes);
  1714. } else {
  1715. int asize = ins->addr_size >> 3;
  1716. if (overflow_general(opy->offset, asize) ||
  1717. signed_bits(opy->offset, ins->addr_size) !=
  1718. signed_bits(opy->offset, ea_data.bytes << 3))
  1719. warn_overflow(ea_data.bytes);
  1720. out_imm(data, opy, ea_data.bytes,
  1721. (asize > ea_data.bytes)
  1722. ? OUT_SIGNED : OUT_WRAP);
  1723. }
  1724. }
  1725. }
  1726. break;
  1727. default:
  1728. nasm_panic(0, "internal instruction table corrupt"
  1729. ": instruction code \\%o (0x%02X) given", c, c);
  1730. break;
  1731. }
  1732. }
  1733. }
  1734. static opflags_t regflag(const operand * o)
  1735. {
  1736. if (!is_register(o->basereg))
  1737. nasm_panic(0, "invalid operand passed to regflag()");
  1738. return nasm_reg_flags[o->basereg];
  1739. }
  1740. static int32_t regval(const operand * o)
  1741. {
  1742. if (!is_register(o->basereg))
  1743. nasm_panic(0, "invalid operand passed to regval()");
  1744. return nasm_regvals[o->basereg];
  1745. }
  1746. static int op_rexflags(const operand * o, int mask)
  1747. {
  1748. opflags_t flags;
  1749. int val;
  1750. if (!is_register(o->basereg))
  1751. nasm_panic(0, "invalid operand passed to op_rexflags()");
  1752. flags = nasm_reg_flags[o->basereg];
  1753. val = nasm_regvals[o->basereg];
  1754. return rexflags(val, flags, mask);
  1755. }
  1756. static int rexflags(int val, opflags_t flags, int mask)
  1757. {
  1758. int rex = 0;
  1759. if (val >= 0 && (val & 8))
  1760. rex |= REX_B|REX_X|REX_R;
  1761. if (flags & BITS64)
  1762. rex |= REX_W;
  1763. if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
  1764. rex |= REX_H;
  1765. else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
  1766. rex |= REX_P;
  1767. return rex & mask;
  1768. }
  1769. static int evexflags(int val, decoflags_t deco,
  1770. int mask, uint8_t byte)
  1771. {
  1772. int evex = 0;
  1773. switch (byte) {
  1774. case 0:
  1775. if (val >= 0 && (val & 16))
  1776. evex |= (EVEX_P0RP | EVEX_P0X);
  1777. break;
  1778. case 2:
  1779. if (val >= 0 && (val & 16))
  1780. evex |= EVEX_P2VP;
  1781. if (deco & Z)
  1782. evex |= EVEX_P2Z;
  1783. if (deco & OPMASK_MASK)
  1784. evex |= deco & EVEX_P2AAA;
  1785. break;
  1786. }
  1787. return evex & mask;
  1788. }
  1789. static int op_evexflags(const operand * o, int mask, uint8_t byte)
  1790. {
  1791. int val;
  1792. val = nasm_regvals[o->basereg];
  1793. return evexflags(val, o->decoflags, mask, byte);
  1794. }
  1795. static enum match_result find_match(const struct itemplate **tempp,
  1796. insn *instruction,
  1797. int32_t segment, int64_t offset, int bits)
  1798. {
  1799. const struct itemplate *temp;
  1800. enum match_result m, merr;
  1801. opflags_t xsizeflags[MAX_OPERANDS];
  1802. bool opsizemissing = false;
  1803. int8_t broadcast = instruction->evex_brerop;
  1804. int i;
  1805. /* broadcasting uses a different data element size */
  1806. for (i = 0; i < instruction->operands; i++)
  1807. if (i == broadcast)
  1808. xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
  1809. else
  1810. xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
  1811. merr = MERR_INVALOP;
  1812. for (temp = nasm_instructions[instruction->opcode];
  1813. temp->opcode != I_none; temp++) {
  1814. m = matches(temp, instruction, bits);
  1815. if (m == MOK_JUMP) {
  1816. if (jmp_match(segment, offset, bits, instruction, temp))
  1817. m = MOK_GOOD;
  1818. else
  1819. m = MERR_INVALOP;
  1820. } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
  1821. /*
  1822. * Missing operand size and a candidate for fuzzy matching...
  1823. */
  1824. for (i = 0; i < temp->operands; i++)
  1825. if (i == broadcast)
  1826. xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
  1827. else
  1828. xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
  1829. opsizemissing = true;
  1830. }
  1831. if (m > merr)
  1832. merr = m;
  1833. if (merr == MOK_GOOD)
  1834. goto done;
  1835. }
  1836. /* No match, but see if we can get a fuzzy operand size match... */
  1837. if (!opsizemissing)
  1838. goto done;
  1839. for (i = 0; i < instruction->operands; i++) {
  1840. /*
  1841. * We ignore extrinsic operand sizes on registers, so we should
  1842. * never try to fuzzy-match on them. This also resolves the case
  1843. * when we have e.g. "xmmrm128" in two different positions.
  1844. */
  1845. if (is_class(REGISTER, instruction->oprs[i].type))
  1846. continue;
  1847. /* This tests if xsizeflags[i] has more than one bit set */
  1848. if ((xsizeflags[i] & (xsizeflags[i]-1)))
  1849. goto done; /* No luck */
  1850. if (i == broadcast) {
  1851. instruction->oprs[i].decoflags |= xsizeflags[i];
  1852. instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
  1853. BITS32 : BITS64);
  1854. } else {
  1855. instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
  1856. }
  1857. }
  1858. /* Try matching again... */
  1859. for (temp = nasm_instructions[instruction->opcode];
  1860. temp->opcode != I_none; temp++) {
  1861. m = matches(temp, instruction, bits);
  1862. if (m == MOK_JUMP) {
  1863. if (jmp_match(segment, offset, bits, instruction, temp))
  1864. m = MOK_GOOD;
  1865. else
  1866. m = MERR_INVALOP;
  1867. }
  1868. if (m > merr)
  1869. merr = m;
  1870. if (merr == MOK_GOOD)
  1871. goto done;
  1872. }
  1873. done:
  1874. *tempp = temp;
  1875. return merr;
  1876. }
  1877. static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
  1878. {
  1879. unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
  1880. uint8_t brcast_num;
  1881. if (brsize > BITS64)
  1882. nasm_error(ERR_FATAL,
  1883. "size of broadcasting element is greater than 64 bits");
  1884. /*
  1885. * The shift term is to take care of the extra BITS80 inserted
  1886. * between BITS64 and BITS128.
  1887. */
  1888. brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
  1889. >> (opsize > (BITS64 >> SIZE_SHIFT));
  1890. return brcast_num;
  1891. }
  1892. static enum match_result matches(const struct itemplate *itemp,
  1893. insn *instruction, int bits)
  1894. {
  1895. opflags_t size[MAX_OPERANDS], asize;
  1896. bool opsizemissing = false;
  1897. int i, oprs;
  1898. /*
  1899. * Check the opcode
  1900. */
  1901. if (itemp->opcode != instruction->opcode)
  1902. return MERR_INVALOP;
  1903. /*
  1904. * Count the operands
  1905. */
  1906. if (itemp->operands != instruction->operands)
  1907. return MERR_INVALOP;
  1908. /*
  1909. * Is it legal?
  1910. */
  1911. if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
  1912. return MERR_INVALOP;
  1913. /*
  1914. * {evex} available?
  1915. */
  1916. switch (instruction->prefixes[PPS_VEX]) {
  1917. case P_EVEX:
  1918. if (!itemp_has(itemp, IF_EVEX))
  1919. return MERR_ENCMISMATCH;
  1920. break;
  1921. case P_VEX3:
  1922. case P_VEX2:
  1923. if (!itemp_has(itemp, IF_VEX))
  1924. return MERR_ENCMISMATCH;
  1925. break;
  1926. default:
  1927. break;
  1928. }
  1929. /*
  1930. * Check that no spurious colons or TOs are present
  1931. */
  1932. for (i = 0; i < itemp->operands; i++)
  1933. if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
  1934. return MERR_INVALOP;
  1935. /*
  1936. * Process size flags
  1937. */
  1938. switch (itemp_smask(itemp)) {
  1939. case IF_GENBIT(IF_SB):
  1940. asize = BITS8;
  1941. break;
  1942. case IF_GENBIT(IF_SW):
  1943. asize = BITS16;
  1944. break;
  1945. case IF_GENBIT(IF_SD):
  1946. asize = BITS32;
  1947. break;
  1948. case IF_GENBIT(IF_SQ):
  1949. asize = BITS64;
  1950. break;
  1951. case IF_GENBIT(IF_SO):
  1952. asize = BITS128;
  1953. break;
  1954. case IF_GENBIT(IF_SY):
  1955. asize = BITS256;
  1956. break;
  1957. case IF_GENBIT(IF_SZ):
  1958. asize = BITS512;
  1959. break;
  1960. case IF_GENBIT(IF_SIZE):
  1961. switch (bits) {
  1962. case 16:
  1963. asize = BITS16;
  1964. break;
  1965. case 32:
  1966. asize = BITS32;
  1967. break;
  1968. case 64:
  1969. asize = BITS64;
  1970. break;
  1971. default:
  1972. asize = 0;
  1973. break;
  1974. }
  1975. break;
  1976. default:
  1977. asize = 0;
  1978. break;
  1979. }
  1980. if (itemp_armask(itemp)) {
  1981. /* S- flags only apply to a specific operand */
  1982. i = itemp_arg(itemp);
  1983. memset(size, 0, sizeof size);
  1984. size[i] = asize;
  1985. } else {
  1986. /* S- flags apply to all operands */
  1987. for (i = 0; i < MAX_OPERANDS; i++)
  1988. size[i] = asize;
  1989. }
  1990. /*
  1991. * Check that the operand flags all match up,
  1992. * it's a bit tricky so lets be verbose:
  1993. *
  1994. * 1) Find out the size of operand. If instruction
  1995. * doesn't have one specified -- we're trying to
  1996. * guess it either from template (IF_S* flag) or
  1997. * from code bits.
  1998. *
  1999. * 2) If template operand do not match the instruction OR
  2000. * template has an operand size specified AND this size differ
  2001. * from which instruction has (perhaps we got it from code bits)
  2002. * we are:
  2003. * a) Check that only size of instruction and operand is differ
  2004. * other characteristics do match
  2005. * b) Perhaps it's a register specified in instruction so
  2006. * for such a case we just mark that operand as "size
  2007. * missing" and this will turn on fuzzy operand size
  2008. * logic facility (handled by a caller)
  2009. */
  2010. for (i = 0; i < itemp->operands; i++) {
  2011. opflags_t type = instruction->oprs[i].type;
  2012. decoflags_t deco = instruction->oprs[i].decoflags;
  2013. decoflags_t ideco = itemp->deco[i];
  2014. bool is_broadcast = deco & BRDCAST_MASK;
  2015. uint8_t brcast_num = 0;
  2016. opflags_t template_opsize, insn_opsize;
  2017. if (!(type & SIZE_MASK))
  2018. type |= size[i];
  2019. insn_opsize = type & SIZE_MASK;
  2020. if (!is_broadcast) {
  2021. template_opsize = itemp->opd[i] & SIZE_MASK;
  2022. } else {
  2023. decoflags_t deco_brsize = ideco & BRSIZE_MASK;
  2024. if (~ideco & BRDCAST_MASK)
  2025. return MERR_BRNOTHERE;
  2026. /*
  2027. * when broadcasting, the element size depends on
  2028. * the instruction type. decorator flag should match.
  2029. */
  2030. if (deco_brsize) {
  2031. template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
  2032. /* calculate the proper number : {1to<brcast_num>} */
  2033. brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
  2034. } else {
  2035. template_opsize = 0;
  2036. }
  2037. }
  2038. if (~ideco & deco & OPMASK_MASK)
  2039. return MERR_MASKNOTHERE;
  2040. if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
  2041. return MERR_DECONOTHERE;
  2042. if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
  2043. return MERR_INVALOP;
  2044. if (~itemp->opd[i] & type & REGSET_MASK)
  2045. return (itemp->opd[i] & REGSET_MASK)
  2046. ? MERR_REGSETSIZE : MERR_REGSET;
  2047. if (template_opsize) {
  2048. if (template_opsize != insn_opsize) {
  2049. if (insn_opsize) {
  2050. return MERR_INVALOP;
  2051. } else if (!is_class(REGISTER, type)) {
  2052. /*
  2053. * Note: we don't honor extrinsic operand sizes for registers,
  2054. * so "missing operand size" for a register should be
  2055. * considered a wildcard match rather than an error.
  2056. */
  2057. opsizemissing = true;
  2058. }
  2059. } else if (is_broadcast &&
  2060. (brcast_num !=
  2061. (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
  2062. /*
  2063. * broadcasting opsize matches but the number of repeated memory
  2064. * element does not match.
  2065. * if 64b double precision float is broadcasted to ymm (256b),
  2066. * broadcasting decorator must be {1to4}.
  2067. */
  2068. return MERR_BRNUMMISMATCH;
  2069. }
  2070. }
  2071. }
  2072. if (opsizemissing)
  2073. return MERR_OPSIZEMISSING;
  2074. /*
  2075. * Check operand sizes
  2076. */
  2077. if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
  2078. oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
  2079. for (i = 0; i < oprs; i++) {
  2080. asize = itemp->opd[i] & SIZE_MASK;
  2081. if (asize) {
  2082. for (i = 0; i < oprs; i++)
  2083. size[i] = asize;
  2084. break;
  2085. }
  2086. }
  2087. } else {
  2088. oprs = itemp->operands;
  2089. }
  2090. for (i = 0; i < itemp->operands; i++) {
  2091. if (!(itemp->opd[i] & SIZE_MASK) &&
  2092. (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
  2093. return MERR_OPSIZEMISMATCH;
  2094. }
  2095. /*
  2096. * Check template is okay at the set cpu level
  2097. */
  2098. if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
  2099. return MERR_BADCPU;
  2100. /*
  2101. * Verify the appropriate long mode flag.
  2102. */
  2103. if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
  2104. return MERR_BADMODE;
  2105. /*
  2106. * If we have a HLE prefix, look for the NOHLE flag
  2107. */
  2108. if (itemp_has(itemp, IF_NOHLE) &&
  2109. (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
  2110. has_prefix(instruction, PPS_REP, P_XRELEASE)))
  2111. return MERR_BADHLE;
  2112. /*
  2113. * Check if special handling needed for Jumps
  2114. */
  2115. if ((itemp->code[0] & ~1) == 0370)
  2116. return MOK_JUMP;
  2117. /*
  2118. * Check if BND prefix is allowed.
  2119. * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
  2120. */
  2121. if (!itemp_has(itemp, IF_BND) &&
  2122. (has_prefix(instruction, PPS_REP, P_BND) ||
  2123. has_prefix(instruction, PPS_REP, P_NOBND)))
  2124. return MERR_BADBND;
  2125. else if (itemp_has(itemp, IF_BND) &&
  2126. (has_prefix(instruction, PPS_REP, P_REPNE) ||
  2127. has_prefix(instruction, PPS_REP, P_REPNZ)))
  2128. return MERR_BADREPNE;
  2129. return MOK_GOOD;
  2130. }
  2131. /*
  2132. * Check if ModR/M.mod should/can be 01.
  2133. * - EAF_BYTEOFFS is set
  2134. * - offset can fit in a byte when EVEX is not used
  2135. * - offset can be compressed when EVEX is used
  2136. */
  2137. #define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
  2138. (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
  2139. is_disp8n(input, ins, &output->disp8) : \
  2140. input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
  2141. o <= 127 && seg == NO_SEG && !forw_ref)))
  2142. static enum ea_type process_ea(operand *input, ea *output, int bits,
  2143. int rfield, opflags_t rflags, insn *ins,
  2144. const char **errmsg)
  2145. {
  2146. bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
  2147. int addrbits = ins->addr_size;
  2148. int eaflags = input->eaflags;
  2149. *errmsg = "invalid effective address"; /* Default error message */
  2150. output->type = EA_SCALAR;
  2151. output->rip = false;
  2152. output->disp8 = 0;
  2153. /* REX flags for the rfield operand */
  2154. output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
  2155. /* EVEX.R' flag for the REG operand */
  2156. ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
  2157. if (is_class(REGISTER, input->type)) {
  2158. /*
  2159. * It's a direct register.
  2160. */
  2161. if (!is_register(input->basereg))
  2162. goto err;
  2163. if (!is_reg_class(REG_EA, input->basereg))
  2164. goto err;
  2165. /* broadcasting is not available with a direct register operand. */
  2166. if (input->decoflags & BRDCAST_MASK) {
  2167. *errmsg = "broadcast not allowed with register operand";
  2168. goto err;
  2169. }
  2170. output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
  2171. ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
  2172. output->sib_present = false; /* no SIB necessary */
  2173. output->bytes = 0; /* no offset necessary either */
  2174. output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
  2175. } else {
  2176. /*
  2177. * It's a memory reference.
  2178. */
  2179. /* Embedded rounding or SAE is not available with a mem ref operand. */
  2180. if (input->decoflags & (ER | SAE)) {
  2181. *errmsg = "embedded rounding is available only with "
  2182. "register-register operations";
  2183. goto err;
  2184. }
  2185. if (input->basereg == -1 &&
  2186. (input->indexreg == -1 || input->scale == 0)) {
  2187. /*
  2188. * It's a pure offset.
  2189. */
  2190. if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
  2191. if (input->segment == NO_SEG ||
  2192. (input->opflags & OPFLAG_RELATIVE)) {
  2193. nasm_error(ERR_WARNING | ERR_PASS2,
  2194. "absolute address can not be RIP-relative");
  2195. input->type &= ~IP_REL;
  2196. input->type |= MEMORY;
  2197. }
  2198. }
  2199. if (bits == 64 &&
  2200. !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
  2201. *errmsg = "RIP-relative addressing is prohibited for MIB";
  2202. goto err;
  2203. }
  2204. if (eaflags & EAF_BYTEOFFS ||
  2205. (eaflags & EAF_WORDOFFS &&
  2206. input->disp_size != (addrbits != 16 ? 32 : 16))) {
  2207. nasm_error(ERR_WARNING | ERR_PASS1,
  2208. "displacement size ignored on absolute address");
  2209. }
  2210. if (bits == 64 && (~input->type & IP_REL)) {
  2211. output->sib_present = true;
  2212. output->sib = GEN_SIB(0, 4, 5);
  2213. output->bytes = 4;
  2214. output->modrm = GEN_MODRM(0, rfield, 4);
  2215. output->rip = false;
  2216. } else {
  2217. output->sib_present = false;
  2218. output->bytes = (addrbits != 16 ? 4 : 2);
  2219. output->modrm = GEN_MODRM(0, rfield,
  2220. (addrbits != 16 ? 5 : 6));
  2221. output->rip = bits == 64;
  2222. }
  2223. } else {
  2224. /*
  2225. * It's an indirection.
  2226. */
  2227. int i = input->indexreg, b = input->basereg, s = input->scale;
  2228. int32_t seg = input->segment;
  2229. int hb = input->hintbase, ht = input->hinttype;
  2230. int t, it, bt; /* register numbers */
  2231. opflags_t x, ix, bx; /* register flags */
  2232. if (s == 0)
  2233. i = -1; /* make this easy, at least */
  2234. if (is_register(i)) {
  2235. it = nasm_regvals[i];
  2236. ix = nasm_reg_flags[i];
  2237. } else {
  2238. it = -1;
  2239. ix = 0;
  2240. }
  2241. if (is_register(b)) {
  2242. bt = nasm_regvals[b];
  2243. bx = nasm_reg_flags[b];
  2244. } else {
  2245. bt = -1;
  2246. bx = 0;
  2247. }
  2248. /* if either one are a vector register... */
  2249. if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
  2250. opflags_t sok = BITS32 | BITS64;
  2251. int32_t o = input->offset;
  2252. int mod, scale, index, base;
  2253. /*
  2254. * For a vector SIB, one has to be a vector and the other,
  2255. * if present, a GPR. The vector must be the index operand.
  2256. */
  2257. if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
  2258. if (s == 0)
  2259. s = 1;
  2260. else if (s != 1)
  2261. goto err;
  2262. t = bt, bt = it, it = t;
  2263. x = bx, bx = ix, ix = x;
  2264. }
  2265. if (bt != -1) {
  2266. if (REG_GPR & ~bx)
  2267. goto err;
  2268. if (!(REG64 & ~bx) || !(REG32 & ~bx))
  2269. sok &= bx;
  2270. else
  2271. goto err;
  2272. }
  2273. /*
  2274. * While we're here, ensure the user didn't specify
  2275. * WORD or QWORD
  2276. */
  2277. if (input->disp_size == 16 || input->disp_size == 64)
  2278. goto err;
  2279. if (addrbits == 16 ||
  2280. (addrbits == 32 && !(sok & BITS32)) ||
  2281. (addrbits == 64 && !(sok & BITS64)))
  2282. goto err;
  2283. output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
  2284. : ((ix & YMMREG & ~REG_EA)
  2285. ? EA_YMMVSIB : EA_XMMVSIB));
  2286. output->rex |= rexflags(it, ix, REX_X);
  2287. output->rex |= rexflags(bt, bx, REX_B);
  2288. ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
  2289. index = it & 7; /* it is known to be != -1 */
  2290. switch (s) {
  2291. case 1:
  2292. scale = 0;
  2293. break;
  2294. case 2:
  2295. scale = 1;
  2296. break;
  2297. case 4:
  2298. scale = 2;
  2299. break;
  2300. case 8:
  2301. scale = 3;
  2302. break;
  2303. default: /* then what the smeg is it? */
  2304. goto err; /* panic */
  2305. }
  2306. if (bt == -1) {
  2307. base = 5;
  2308. mod = 0;
  2309. } else {
  2310. base = (bt & 7);
  2311. if (base != REG_NUM_EBP && o == 0 &&
  2312. seg == NO_SEG && !forw_ref &&
  2313. !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
  2314. mod = 0;
  2315. else if (IS_MOD_01())
  2316. mod = 1;
  2317. else
  2318. mod = 2;
  2319. }
  2320. output->sib_present = true;
  2321. output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
  2322. output->modrm = GEN_MODRM(mod, rfield, 4);
  2323. output->sib = GEN_SIB(scale, index, base);
  2324. } else if ((ix|bx) & (BITS32|BITS64)) {
  2325. /*
  2326. * it must be a 32/64-bit memory reference. Firstly we have
  2327. * to check that all registers involved are type E/Rxx.
  2328. */
  2329. opflags_t sok = BITS32 | BITS64;
  2330. int32_t o = input->offset;
  2331. if (it != -1) {
  2332. if (!(REG64 & ~ix) || !(REG32 & ~ix))
  2333. sok &= ix;
  2334. else
  2335. goto err;
  2336. }
  2337. if (bt != -1) {
  2338. if (REG_GPR & ~bx)
  2339. goto err; /* Invalid register */
  2340. if (~sok & bx & SIZE_MASK)
  2341. goto err; /* Invalid size */
  2342. sok &= bx;
  2343. }
  2344. /*
  2345. * While we're here, ensure the user didn't specify
  2346. * WORD or QWORD
  2347. */
  2348. if (input->disp_size == 16 || input->disp_size == 64)
  2349. goto err;
  2350. if (addrbits == 16 ||
  2351. (addrbits == 32 && !(sok & BITS32)) ||
  2352. (addrbits == 64 && !(sok & BITS64)))
  2353. goto err;
  2354. /* now reorganize base/index */
  2355. if (s == 1 && bt != it && bt != -1 && it != -1 &&
  2356. ((hb == b && ht == EAH_NOTBASE) ||
  2357. (hb == i && ht == EAH_MAKEBASE))) {
  2358. /* swap if hints say so */
  2359. t = bt, bt = it, it = t;
  2360. x = bx, bx = ix, ix = x;
  2361. }
  2362. if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
  2363. /* make single reg base, unless hint */
  2364. bt = it, bx = ix, it = -1, ix = 0;
  2365. }
  2366. if (eaflags & EAF_MIB) {
  2367. /* only for mib operands */
  2368. if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
  2369. /*
  2370. * make a single reg index [reg*1].
  2371. * gas uses this form for an explicit index register.
  2372. */
  2373. it = bt, ix = bx, bt = -1, bx = 0, s = 1;
  2374. }
  2375. if ((ht == EAH_SUMMED) && bt == -1) {
  2376. /* separate once summed index into [base, index] */
  2377. bt = it, bx = ix, s--;
  2378. }
  2379. } else {
  2380. if (((s == 2 && it != REG_NUM_ESP &&
  2381. (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
  2382. s == 3 || s == 5 || s == 9) && bt == -1) {
  2383. /* convert 3*EAX to EAX+2*EAX */
  2384. bt = it, bx = ix, s--;
  2385. }
  2386. if (it == -1 && (bt & 7) != REG_NUM_ESP &&
  2387. (eaflags & EAF_TIMESTWO) &&
  2388. (hb == b && ht == EAH_NOTBASE)) {
  2389. /*
  2390. * convert [NOSPLIT EAX*1]
  2391. * to sib format with 0x0 displacement - [EAX*1+0].
  2392. */
  2393. it = bt, ix = bx, bt = -1, bx = 0, s = 1;
  2394. }
  2395. }
  2396. if (s == 1 && it == REG_NUM_ESP) {
  2397. /* swap ESP into base if scale is 1 */
  2398. t = it, it = bt, bt = t;
  2399. x = ix, ix = bx, bx = x;
  2400. }
  2401. if (it == REG_NUM_ESP ||
  2402. (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
  2403. goto err; /* wrong, for various reasons */
  2404. output->rex |= rexflags(it, ix, REX_X);
  2405. output->rex |= rexflags(bt, bx, REX_B);
  2406. if (it == -1 && (bt & 7) != REG_NUM_ESP) {
  2407. /* no SIB needed */
  2408. int mod, rm;
  2409. if (bt == -1) {
  2410. rm = 5;
  2411. mod = 0;
  2412. } else {
  2413. rm = (bt & 7);
  2414. if (rm != REG_NUM_EBP && o == 0 &&
  2415. seg == NO_SEG && !forw_ref &&
  2416. !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
  2417. mod = 0;
  2418. else if (IS_MOD_01())
  2419. mod = 1;
  2420. else
  2421. mod = 2;
  2422. }
  2423. output->sib_present = false;
  2424. output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
  2425. output->modrm = GEN_MODRM(mod, rfield, rm);
  2426. } else {
  2427. /* we need a SIB */
  2428. int mod, scale, index, base;
  2429. if (it == -1)
  2430. index = 4, s = 1;
  2431. else
  2432. index = (it & 7);
  2433. switch (s) {
  2434. case 1:
  2435. scale = 0;
  2436. break;
  2437. case 2:
  2438. scale = 1;
  2439. break;
  2440. case 4:
  2441. scale = 2;
  2442. break;
  2443. case 8:
  2444. scale = 3;
  2445. break;
  2446. default: /* then what the smeg is it? */
  2447. goto err; /* panic */
  2448. }
  2449. if (bt == -1) {
  2450. base = 5;
  2451. mod = 0;
  2452. } else {
  2453. base = (bt & 7);
  2454. if (base != REG_NUM_EBP && o == 0 &&
  2455. seg == NO_SEG && !forw_ref &&
  2456. !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
  2457. mod = 0;
  2458. else if (IS_MOD_01())
  2459. mod = 1;
  2460. else
  2461. mod = 2;
  2462. }
  2463. output->sib_present = true;
  2464. output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
  2465. output->modrm = GEN_MODRM(mod, rfield, 4);
  2466. output->sib = GEN_SIB(scale, index, base);
  2467. }
  2468. } else { /* it's 16-bit */
  2469. int mod, rm;
  2470. int16_t o = input->offset;
  2471. /* check for 64-bit long mode */
  2472. if (addrbits == 64)
  2473. goto err;
  2474. /* check all registers are BX, BP, SI or DI */
  2475. if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
  2476. (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
  2477. goto err;
  2478. /* ensure the user didn't specify DWORD/QWORD */
  2479. if (input->disp_size == 32 || input->disp_size == 64)
  2480. goto err;
  2481. if (s != 1 && i != -1)
  2482. goto err; /* no can do, in 16-bit EA */
  2483. if (b == -1 && i != -1) {
  2484. int tmp = b;
  2485. b = i;
  2486. i = tmp;
  2487. } /* swap */
  2488. if ((b == R_SI || b == R_DI) && i != -1) {
  2489. int tmp = b;
  2490. b = i;
  2491. i = tmp;
  2492. }
  2493. /* have BX/BP as base, SI/DI index */
  2494. if (b == i)
  2495. goto err; /* shouldn't ever happen, in theory */
  2496. if (i != -1 && b != -1 &&
  2497. (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
  2498. goto err; /* invalid combinations */
  2499. if (b == -1) /* pure offset: handled above */
  2500. goto err; /* so if it gets to here, panic! */
  2501. rm = -1;
  2502. if (i != -1)
  2503. switch (i * 256 + b) {
  2504. case R_SI * 256 + R_BX:
  2505. rm = 0;
  2506. break;
  2507. case R_DI * 256 + R_BX:
  2508. rm = 1;
  2509. break;
  2510. case R_SI * 256 + R_BP:
  2511. rm = 2;
  2512. break;
  2513. case R_DI * 256 + R_BP:
  2514. rm = 3;
  2515. break;
  2516. } else
  2517. switch (b) {
  2518. case R_SI:
  2519. rm = 4;
  2520. break;
  2521. case R_DI:
  2522. rm = 5;
  2523. break;
  2524. case R_BP:
  2525. rm = 6;
  2526. break;
  2527. case R_BX:
  2528. rm = 7;
  2529. break;
  2530. }
  2531. if (rm == -1) /* can't happen, in theory */
  2532. goto err; /* so panic if it does */
  2533. if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
  2534. !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
  2535. mod = 0;
  2536. else if (IS_MOD_01())
  2537. mod = 1;
  2538. else
  2539. mod = 2;
  2540. output->sib_present = false; /* no SIB - it's 16-bit */
  2541. output->bytes = mod; /* bytes of offset needed */
  2542. output->modrm = GEN_MODRM(mod, rfield, rm);
  2543. }
  2544. }
  2545. }
  2546. output->size = 1 + output->sib_present + output->bytes;
  2547. return output->type;
  2548. err:
  2549. return output->type = EA_INVALID;
  2550. }
  2551. static void add_asp(insn *ins, int addrbits)
  2552. {
  2553. int j, valid;
  2554. int defdisp;
  2555. valid = (addrbits == 64) ? 64|32 : 32|16;
  2556. switch (ins->prefixes[PPS_ASIZE]) {
  2557. case P_A16:
  2558. valid &= 16;
  2559. break;
  2560. case P_A32:
  2561. valid &= 32;
  2562. break;
  2563. case P_A64:
  2564. valid &= 64;
  2565. break;
  2566. case P_ASP:
  2567. valid &= (addrbits == 32) ? 16 : 32;
  2568. break;
  2569. default:
  2570. break;
  2571. }
  2572. for (j = 0; j < ins->operands; j++) {
  2573. if (is_class(MEMORY, ins->oprs[j].type)) {
  2574. opflags_t i, b;
  2575. /* Verify as Register */
  2576. if (!is_register(ins->oprs[j].indexreg))
  2577. i = 0;
  2578. else
  2579. i = nasm_reg_flags[ins->oprs[j].indexreg];
  2580. /* Verify as Register */
  2581. if (!is_register(ins->oprs[j].basereg))
  2582. b = 0;
  2583. else
  2584. b = nasm_reg_flags[ins->oprs[j].basereg];
  2585. if (ins->oprs[j].scale == 0)
  2586. i = 0;
  2587. if (!i && !b) {
  2588. int ds = ins->oprs[j].disp_size;
  2589. if ((addrbits != 64 && ds > 8) ||
  2590. (addrbits == 64 && ds == 16))
  2591. valid &= ds;
  2592. } else {
  2593. if (!(REG16 & ~b))
  2594. valid &= 16;
  2595. if (!(REG32 & ~b))
  2596. valid &= 32;
  2597. if (!(REG64 & ~b))
  2598. valid &= 64;
  2599. if (!(REG16 & ~i))
  2600. valid &= 16;
  2601. if (!(REG32 & ~i))
  2602. valid &= 32;
  2603. if (!(REG64 & ~i))
  2604. valid &= 64;
  2605. }
  2606. }
  2607. }
  2608. if (valid & addrbits) {
  2609. ins->addr_size = addrbits;
  2610. } else if (valid & ((addrbits == 32) ? 16 : 32)) {
  2611. /* Add an address size prefix */
  2612. ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
  2613. ins->addr_size = (addrbits == 32) ? 16 : 32;
  2614. } else {
  2615. /* Impossible... */
  2616. nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
  2617. ins->addr_size = addrbits; /* Error recovery */
  2618. }
  2619. defdisp = ins->addr_size == 16 ? 16 : 32;
  2620. for (j = 0; j < ins->operands; j++) {
  2621. if (!(MEM_OFFS & ~ins->oprs[j].type) &&
  2622. (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
  2623. /*
  2624. * mem_offs sizes must match the address size; if not,
  2625. * strip the MEM_OFFS bit and match only EA instructions
  2626. */
  2627. ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
  2628. }
  2629. }
  2630. }