lj_asm_x86.h 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  1. /*
  2. ** x86/x64 IR assembler (SSA IR -> machine code).
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. /* -- Guard handling ------------------------------------------------------ */
  6. /* Generate an exit stub group at the bottom of the reserved MCode memory. */
  7. static MCode *asm_exitstub_gen(ASMState *as, ExitNo group)
  8. {
  9. ExitNo i, groupofs = (group*EXITSTUBS_PER_GROUP) & 0xff;
  10. MCode *mxp = as->mcbot;
  11. MCode *mxpstart = mxp;
  12. if (mxp + (2+2)*EXITSTUBS_PER_GROUP+8+5 >= as->mctop)
  13. asm_mclimit(as);
  14. /* Push low byte of exitno for each exit stub. */
  15. *mxp++ = XI_PUSHi8; *mxp++ = (MCode)groupofs;
  16. for (i = 1; i < EXITSTUBS_PER_GROUP; i++) {
  17. *mxp++ = XI_JMPs; *mxp++ = (MCode)((2+2)*(EXITSTUBS_PER_GROUP - i) - 2);
  18. *mxp++ = XI_PUSHi8; *mxp++ = (MCode)(groupofs + i);
  19. }
  20. /* Push the high byte of the exitno for each exit stub group. */
  21. *mxp++ = XI_PUSHi8; *mxp++ = (MCode)((group*EXITSTUBS_PER_GROUP)>>8);
  22. #if !LJ_GC64
  23. /* Store DISPATCH at original stack slot 0. Account for the two push ops. */
  24. *mxp++ = XI_MOVmi;
  25. *mxp++ = MODRM(XM_OFS8, 0, RID_ESP);
  26. *mxp++ = MODRM(XM_SCALE1, RID_ESP, RID_ESP);
  27. *mxp++ = 2*sizeof(void *);
  28. *(int32_t *)mxp = ptr2addr(J2GG(as->J)->dispatch); mxp += 4;
  29. #endif
  30. /* Jump to exit handler which fills in the ExitState. */
  31. *mxp++ = XI_JMP; mxp += 4;
  32. *((int32_t *)(mxp-4)) = jmprel(as->J, mxp, (MCode *)(void *)lj_vm_exit_handler);
  33. /* Commit the code for this group (even if assembly fails later on). */
  34. lj_mcode_commitbot(as->J, mxp);
  35. as->mcbot = mxp;
  36. as->mclim = as->mcbot + MCLIM_REDZONE;
  37. return mxpstart;
  38. }
  39. /* Setup all needed exit stubs. */
  40. static void asm_exitstub_setup(ASMState *as, ExitNo nexits)
  41. {
  42. ExitNo i;
  43. if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR)
  44. lj_trace_err(as->J, LJ_TRERR_SNAPOV);
  45. for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++)
  46. if (as->J->exitstubgroup[i] == NULL)
  47. as->J->exitstubgroup[i] = asm_exitstub_gen(as, i);
  48. }
  49. /* Emit conditional branch to exit for guard.
  50. ** It's important to emit this *after* all registers have been allocated,
  51. ** because rematerializations may invalidate the flags.
  52. */
  53. static void asm_guardcc(ASMState *as, int cc)
  54. {
  55. MCode *target = exitstub_addr(as->J, as->snapno);
  56. MCode *p = as->mcp;
  57. if (LJ_UNLIKELY(p == as->invmcp)) {
  58. as->loopinv = 1;
  59. *(int32_t *)(p+1) = jmprel(as->J, p+5, target);
  60. target = p;
  61. cc ^= 1;
  62. if (as->realign) {
  63. if (LJ_GC64 && LJ_UNLIKELY(as->mrm.base == RID_RIP))
  64. as->mrm.ofs += 2; /* Fixup RIP offset for pending fused load. */
  65. emit_sjcc(as, cc, target);
  66. return;
  67. }
  68. }
  69. if (LJ_GC64 && LJ_UNLIKELY(as->mrm.base == RID_RIP))
  70. as->mrm.ofs += 6; /* Fixup RIP offset for pending fused load. */
  71. emit_jcc(as, cc, target);
  72. }
  73. /* -- Memory operand fusion ----------------------------------------------- */
  74. /* Limit linear search to this distance. Avoids O(n^2) behavior. */
  75. #define CONFLICT_SEARCH_LIM 31
  76. /* Check if a reference is a signed 32 bit constant. */
  77. static int asm_isk32(ASMState *as, IRRef ref, int32_t *k)
  78. {
  79. if (irref_isk(ref)) {
  80. IRIns *ir = IR(ref);
  81. #if LJ_GC64
  82. if (ir->o == IR_KNULL || !irt_is64(ir->t)) {
  83. *k = ir->i;
  84. return 1;
  85. } else if (checki32((int64_t)ir_k64(ir)->u64)) {
  86. *k = (int32_t)ir_k64(ir)->u64;
  87. return 1;
  88. }
  89. #else
  90. if (ir->o != IR_KINT64) {
  91. *k = ir->i;
  92. return 1;
  93. } else if (checki32((int64_t)ir_kint64(ir)->u64)) {
  94. *k = (int32_t)ir_kint64(ir)->u64;
  95. return 1;
  96. }
  97. #endif
  98. }
  99. return 0;
  100. }
  101. /* Check if there's no conflicting instruction between curins and ref.
  102. ** Also avoid fusing loads if there are multiple references.
  103. */
  104. static int noconflict(ASMState *as, IRRef ref, IROp conflict, int check)
  105. {
  106. IRIns *ir = as->ir;
  107. IRRef i = as->curins;
  108. if (i > ref + CONFLICT_SEARCH_LIM)
  109. return 0; /* Give up, ref is too far away. */
  110. while (--i > ref) {
  111. if (ir[i].o == conflict)
  112. return 0; /* Conflict found. */
  113. else if ((check & 1) && (ir[i].o == IR_NEWREF || ir[i].o == IR_CALLS))
  114. return 0;
  115. else if ((check & 2) && (ir[i].op1 == ref || ir[i].op2 == ref))
  116. return 0;
  117. }
  118. return 1; /* Ok, no conflict. */
  119. }
  120. /* Fuse array base into memory operand. */
  121. static IRRef asm_fuseabase(ASMState *as, IRRef ref)
  122. {
  123. IRIns *irb = IR(ref);
  124. as->mrm.ofs = 0;
  125. if (irb->o == IR_FLOAD) {
  126. IRIns *ira = IR(irb->op1);
  127. lj_assertA(irb->op2 == IRFL_TAB_ARRAY, "expected FLOAD TAB_ARRAY");
  128. /* We can avoid the FLOAD of t->array for colocated arrays. */
  129. if (ira->o == IR_TNEW && ira->op1 <= LJ_MAX_COLOSIZE &&
  130. !neverfuse(as) && noconflict(as, irb->op1, IR_NEWREF, 0)) {
  131. as->mrm.ofs = (int32_t)sizeof(GCtab); /* Ofs to colocated array. */
  132. return irb->op1; /* Table obj. */
  133. }
  134. } else if (irb->o == IR_ADD && irref_isk(irb->op2)) {
  135. /* Fuse base offset (vararg load). */
  136. IRIns *irk = IR(irb->op2);
  137. as->mrm.ofs = irk->o == IR_KINT ? irk->i : (int32_t)ir_kint64(irk)->u64;
  138. return irb->op1;
  139. }
  140. return ref; /* Otherwise use the given array base. */
  141. }
  142. /* Fuse array reference into memory operand. */
  143. static void asm_fusearef(ASMState *as, IRIns *ir, RegSet allow)
  144. {
  145. IRIns *irx;
  146. lj_assertA(ir->o == IR_AREF, "expected AREF");
  147. as->mrm.base = (uint8_t)ra_alloc1(as, asm_fuseabase(as, ir->op1), allow);
  148. irx = IR(ir->op2);
  149. if (irref_isk(ir->op2)) {
  150. as->mrm.ofs += 8*irx->i;
  151. as->mrm.idx = RID_NONE;
  152. } else {
  153. rset_clear(allow, as->mrm.base);
  154. as->mrm.scale = XM_SCALE8;
  155. /* Fuse a constant ADD (e.g. t[i+1]) into the offset.
  156. ** Doesn't help much without ABCelim, but reduces register pressure.
  157. */
  158. if (!LJ_64 && /* Has bad effects with negative index on x64. */
  159. mayfuse(as, ir->op2) && ra_noreg(irx->r) &&
  160. irx->o == IR_ADD && irref_isk(irx->op2)) {
  161. as->mrm.ofs += 8*IR(irx->op2)->i;
  162. as->mrm.idx = (uint8_t)ra_alloc1(as, irx->op1, allow);
  163. } else {
  164. as->mrm.idx = (uint8_t)ra_alloc1(as, ir->op2, allow);
  165. }
  166. }
  167. }
  168. /* Fuse array/hash/upvalue reference into memory operand.
  169. ** Caveat: this may allocate GPRs for the base/idx registers. Be sure to
  170. ** pass the final allow mask, excluding any GPRs used for other inputs.
  171. ** In particular: 2-operand GPR instructions need to call ra_dest() first!
  172. */
  173. static void asm_fuseahuref(ASMState *as, IRRef ref, RegSet allow)
  174. {
  175. IRIns *ir = IR(ref);
  176. if (ra_noreg(ir->r)) {
  177. switch ((IROp)ir->o) {
  178. case IR_AREF:
  179. if (mayfuse(as, ref)) {
  180. asm_fusearef(as, ir, allow);
  181. return;
  182. }
  183. break;
  184. case IR_HREFK:
  185. if (mayfuse(as, ref)) {
  186. as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow);
  187. as->mrm.ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));
  188. as->mrm.idx = RID_NONE;
  189. return;
  190. }
  191. break;
  192. case IR_UREFC:
  193. if (irref_isk(ir->op1)) {
  194. GCfunc *fn = ir_kfunc(IR(ir->op1));
  195. GCupval *uv = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv;
  196. #if LJ_GC64
  197. int64_t ofs = dispofs(as, &uv->tv);
  198. if (checki32(ofs) && checki32(ofs+4)) {
  199. as->mrm.ofs = (int32_t)ofs;
  200. as->mrm.base = RID_DISPATCH;
  201. as->mrm.idx = RID_NONE;
  202. return;
  203. }
  204. #else
  205. as->mrm.ofs = ptr2addr(&uv->tv);
  206. as->mrm.base = as->mrm.idx = RID_NONE;
  207. return;
  208. #endif
  209. }
  210. break;
  211. case IR_TMPREF:
  212. #if LJ_GC64
  213. as->mrm.ofs = (int32_t)dispofs(as, &J2G(as->J)->tmptv);
  214. as->mrm.base = RID_DISPATCH;
  215. as->mrm.idx = RID_NONE;
  216. #else
  217. as->mrm.ofs = igcptr(&J2G(as->J)->tmptv);
  218. as->mrm.base = as->mrm.idx = RID_NONE;
  219. #endif
  220. return;
  221. default:
  222. break;
  223. }
  224. }
  225. as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow);
  226. as->mrm.ofs = 0;
  227. as->mrm.idx = RID_NONE;
  228. }
  229. /* Fuse FLOAD/FREF reference into memory operand. */
  230. static void asm_fusefref(ASMState *as, IRIns *ir, RegSet allow)
  231. {
  232. lj_assertA(ir->o == IR_FLOAD || ir->o == IR_FREF,
  233. "bad IR op %d", ir->o);
  234. as->mrm.idx = RID_NONE;
  235. if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */
  236. #if LJ_GC64
  237. as->mrm.ofs = (int32_t)(ir->op2 << 2) - GG_OFS(dispatch);
  238. as->mrm.base = RID_DISPATCH;
  239. #else
  240. as->mrm.ofs = (int32_t)(ir->op2 << 2) + ptr2addr(J2GG(as->J));
  241. as->mrm.base = RID_NONE;
  242. #endif
  243. return;
  244. }
  245. as->mrm.ofs = field_ofs[ir->op2];
  246. if (irref_isk(ir->op1)) {
  247. IRIns *op1 = IR(ir->op1);
  248. #if LJ_GC64
  249. if (ir->op1 == REF_NIL) {
  250. as->mrm.ofs -= GG_OFS(dispatch);
  251. as->mrm.base = RID_DISPATCH;
  252. return;
  253. } else if (op1->o == IR_KPTR || op1->o == IR_KKPTR) {
  254. intptr_t ofs = dispofs(as, ir_kptr(op1));
  255. if (checki32(as->mrm.ofs + ofs)) {
  256. as->mrm.ofs += (int32_t)ofs;
  257. as->mrm.base = RID_DISPATCH;
  258. return;
  259. }
  260. }
  261. #else
  262. as->mrm.ofs += op1->i;
  263. as->mrm.base = RID_NONE;
  264. return;
  265. #endif
  266. }
  267. as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow);
  268. }
  269. /* Fuse string reference into memory operand. */
  270. static void asm_fusestrref(ASMState *as, IRIns *ir, RegSet allow)
  271. {
  272. IRIns *irr;
  273. lj_assertA(ir->o == IR_STRREF, "bad IR op %d", ir->o);
  274. as->mrm.base = as->mrm.idx = RID_NONE;
  275. as->mrm.scale = XM_SCALE1;
  276. as->mrm.ofs = sizeof(GCstr);
  277. if (!LJ_GC64 && irref_isk(ir->op1)) {
  278. as->mrm.ofs += IR(ir->op1)->i;
  279. } else {
  280. Reg r = ra_alloc1(as, ir->op1, allow);
  281. rset_clear(allow, r);
  282. as->mrm.base = (uint8_t)r;
  283. }
  284. irr = IR(ir->op2);
  285. if (irref_isk(ir->op2)) {
  286. as->mrm.ofs += irr->i;
  287. } else {
  288. Reg r;
  289. /* Fuse a constant add into the offset, e.g. string.sub(s, i+10). */
  290. if (!LJ_64 && /* Has bad effects with negative index on x64. */
  291. mayfuse(as, ir->op2) && irr->o == IR_ADD && irref_isk(irr->op2)) {
  292. as->mrm.ofs += IR(irr->op2)->i;
  293. r = ra_alloc1(as, irr->op1, allow);
  294. } else {
  295. r = ra_alloc1(as, ir->op2, allow);
  296. }
  297. if (as->mrm.base == RID_NONE)
  298. as->mrm.base = (uint8_t)r;
  299. else
  300. as->mrm.idx = (uint8_t)r;
  301. }
  302. }
  303. static void asm_fusexref(ASMState *as, IRRef ref, RegSet allow)
  304. {
  305. IRIns *ir = IR(ref);
  306. as->mrm.idx = RID_NONE;
  307. if (ir->o == IR_KPTR || ir->o == IR_KKPTR) {
  308. #if LJ_GC64
  309. intptr_t ofs = dispofs(as, ir_kptr(ir));
  310. if (checki32(ofs)) {
  311. as->mrm.ofs = (int32_t)ofs;
  312. as->mrm.base = RID_DISPATCH;
  313. return;
  314. }
  315. } if (0) {
  316. #else
  317. as->mrm.ofs = ir->i;
  318. as->mrm.base = RID_NONE;
  319. } else if (ir->o == IR_STRREF) {
  320. asm_fusestrref(as, ir, allow);
  321. #endif
  322. } else {
  323. as->mrm.ofs = 0;
  324. if (canfuse(as, ir) && ir->o == IR_ADD && ra_noreg(ir->r)) {
  325. /* Gather (base+idx*sz)+ofs as emitted by cdata ptr/array indexing. */
  326. IRIns *irx;
  327. IRRef idx;
  328. Reg r;
  329. if (asm_isk32(as, ir->op2, &as->mrm.ofs)) { /* Recognize x+ofs. */
  330. ref = ir->op1;
  331. ir = IR(ref);
  332. if (!(ir->o == IR_ADD && canfuse(as, ir) && ra_noreg(ir->r)))
  333. goto noadd;
  334. }
  335. as->mrm.scale = XM_SCALE1;
  336. idx = ir->op1;
  337. ref = ir->op2;
  338. irx = IR(idx);
  339. if (!(irx->o == IR_BSHL || irx->o == IR_ADD)) { /* Try other operand. */
  340. idx = ir->op2;
  341. ref = ir->op1;
  342. irx = IR(idx);
  343. }
  344. if (canfuse(as, irx) && ra_noreg(irx->r)) {
  345. if (irx->o == IR_BSHL && irref_isk(irx->op2) && IR(irx->op2)->i <= 3) {
  346. /* Recognize idx<<b with b = 0-3, corresponding to sz = (1),2,4,8. */
  347. idx = irx->op1;
  348. as->mrm.scale = (uint8_t)(IR(irx->op2)->i << 6);
  349. } else if (irx->o == IR_ADD && irx->op1 == irx->op2) {
  350. /* FOLD does idx*2 ==> idx<<1 ==> idx+idx. */
  351. idx = irx->op1;
  352. as->mrm.scale = XM_SCALE2;
  353. }
  354. }
  355. r = ra_alloc1(as, idx, allow);
  356. rset_clear(allow, r);
  357. as->mrm.idx = (uint8_t)r;
  358. }
  359. noadd:
  360. as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow);
  361. }
  362. }
  363. /* Fuse load of 64 bit IR constant into memory operand. */
  364. static Reg asm_fuseloadk64(ASMState *as, IRIns *ir)
  365. {
  366. const uint64_t *k = &ir_k64(ir)->u64;
  367. if (!LJ_GC64 || checki32((intptr_t)k)) {
  368. as->mrm.ofs = ptr2addr(k);
  369. as->mrm.base = RID_NONE;
  370. #if LJ_GC64
  371. } else if (checki32(dispofs(as, k))) {
  372. as->mrm.ofs = (int32_t)dispofs(as, k);
  373. as->mrm.base = RID_DISPATCH;
  374. } else if (checki32(mcpofs(as, k)) && checki32(mcpofs(as, k+1)) &&
  375. checki32(mctopofs(as, k)) && checki32(mctopofs(as, k+1))) {
  376. as->mrm.ofs = (int32_t)mcpofs(as, k);
  377. as->mrm.base = RID_RIP;
  378. } else { /* Intern 64 bit constant at bottom of mcode. */
  379. if (ir->i) {
  380. lj_assertA(*k == *(uint64_t*)(as->mctop - ir->i),
  381. "bad interned 64 bit constant");
  382. } else {
  383. while ((uintptr_t)as->mcbot & 7) *as->mcbot++ = XI_INT3;
  384. *(uint64_t*)as->mcbot = *k;
  385. ir->i = (int32_t)(as->mctop - as->mcbot);
  386. as->mcbot += 8;
  387. as->mclim = as->mcbot + MCLIM_REDZONE;
  388. lj_mcode_commitbot(as->J, as->mcbot);
  389. }
  390. as->mrm.ofs = (int32_t)mcpofs(as, as->mctop - ir->i);
  391. as->mrm.base = RID_RIP;
  392. #endif
  393. }
  394. as->mrm.idx = RID_NONE;
  395. return RID_MRM;
  396. }
  397. /* Fuse load into memory operand.
  398. **
  399. ** Important caveat: this may emit RIP-relative loads! So don't place any
  400. ** code emitters between this function and the use of its result.
  401. ** The only permitted exception is asm_guardcc().
  402. */
  403. static Reg asm_fuseload(ASMState *as, IRRef ref, RegSet allow)
  404. {
  405. IRIns *ir = IR(ref);
  406. if (ra_hasreg(ir->r)) {
  407. if (allow != RSET_EMPTY) { /* Fast path. */
  408. ra_noweak(as, ir->r);
  409. return ir->r;
  410. }
  411. fusespill:
  412. /* Force a spill if only memory operands are allowed (asm_x87load). */
  413. as->mrm.base = RID_ESP;
  414. as->mrm.ofs = ra_spill(as, ir);
  415. as->mrm.idx = RID_NONE;
  416. return RID_MRM;
  417. }
  418. if (ir->o == IR_KNUM) {
  419. RegSet avail = as->freeset & ~as->modset & RSET_FPR;
  420. lj_assertA(allow != RSET_EMPTY, "no register allowed");
  421. if (!(avail & (avail-1))) /* Fuse if less than two regs available. */
  422. return asm_fuseloadk64(as, ir);
  423. } else if (ref == REF_BASE || ir->o == IR_KINT64) {
  424. RegSet avail = as->freeset & ~as->modset & RSET_GPR;
  425. lj_assertA(allow != RSET_EMPTY, "no register allowed");
  426. if (!(avail & (avail-1))) { /* Fuse if less than two regs available. */
  427. if (ref == REF_BASE) {
  428. #if LJ_GC64
  429. as->mrm.ofs = (int32_t)dispofs(as, &J2G(as->J)->jit_base);
  430. as->mrm.base = RID_DISPATCH;
  431. #else
  432. as->mrm.ofs = ptr2addr(&J2G(as->J)->jit_base);
  433. as->mrm.base = RID_NONE;
  434. #endif
  435. as->mrm.idx = RID_NONE;
  436. return RID_MRM;
  437. } else {
  438. return asm_fuseloadk64(as, ir);
  439. }
  440. }
  441. } else if (mayfuse(as, ref)) {
  442. RegSet xallow = (allow & RSET_GPR) ? allow : RSET_GPR;
  443. if (ir->o == IR_SLOAD) {
  444. if (!(ir->op2 & (IRSLOAD_PARENT|IRSLOAD_CONVERT)) &&
  445. noconflict(as, ref, IR_RETF, 2) &&
  446. !(LJ_GC64 && irt_isaddr(ir->t))) {
  447. as->mrm.base = (uint8_t)ra_alloc1(as, REF_BASE, xallow);
  448. as->mrm.ofs = 8*((int32_t)ir->op1-1-LJ_FR2) +
  449. (!LJ_FR2 && (ir->op2 & IRSLOAD_FRAME) ? 4 : 0);
  450. as->mrm.idx = RID_NONE;
  451. return RID_MRM;
  452. }
  453. } else if (ir->o == IR_FLOAD) {
  454. /* Generic fusion is only ok for 32 bit operand (but see asm_comp). */
  455. if ((irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)) &&
  456. noconflict(as, ref, IR_FSTORE, 2)) {
  457. asm_fusefref(as, ir, xallow);
  458. return RID_MRM;
  459. }
  460. } else if (ir->o == IR_ALOAD || ir->o == IR_HLOAD || ir->o == IR_ULOAD) {
  461. if (noconflict(as, ref, ir->o + IRDELTA_L2S, 2+(ir->o != IR_ULOAD)) &&
  462. !(LJ_GC64 && irt_isaddr(ir->t))) {
  463. asm_fuseahuref(as, ir->op1, xallow);
  464. return RID_MRM;
  465. }
  466. } else if (ir->o == IR_XLOAD) {
  467. /* Generic fusion is not ok for 8/16 bit operands (but see asm_comp).
  468. ** Fusing unaligned memory operands is ok on x86 (except for SIMD types).
  469. */
  470. if ((!irt_typerange(ir->t, IRT_I8, IRT_U16)) &&
  471. noconflict(as, ref, IR_XSTORE, 2)) {
  472. asm_fusexref(as, ir->op1, xallow);
  473. return RID_MRM;
  474. }
  475. } else if (ir->o == IR_VLOAD && IR(ir->op1)->o == IR_AREF &&
  476. !(LJ_GC64 && irt_isaddr(ir->t))) {
  477. asm_fuseahuref(as, ir->op1, xallow);
  478. as->mrm.ofs += 8 * ir->op2;
  479. return RID_MRM;
  480. }
  481. }
  482. if (ir->o == IR_FLOAD && ir->op1 == REF_NIL) {
  483. asm_fusefref(as, ir, RSET_EMPTY);
  484. return RID_MRM;
  485. }
  486. if (!(as->freeset & allow) && !emit_canremat(ref) &&
  487. (allow == RSET_EMPTY || ra_hasspill(ir->s) || iscrossref(as, ref)))
  488. goto fusespill;
  489. return ra_allocref(as, ref, allow);
  490. }
  491. #if LJ_64
  492. /* Don't fuse a 32 bit load into a 64 bit operation. */
  493. static Reg asm_fuseloadm(ASMState *as, IRRef ref, RegSet allow, int is64)
  494. {
  495. if (is64 && !irt_is64(IR(ref)->t))
  496. return ra_alloc1(as, ref, allow);
  497. return asm_fuseload(as, ref, allow);
  498. }
  499. #else
  500. #define asm_fuseloadm(as, ref, allow, is64) asm_fuseload(as, (ref), (allow))
  501. #endif
  502. /* -- Calls --------------------------------------------------------------- */
  503. /* Count the required number of stack slots for a call. */
  504. static int asm_count_call_slots(ASMState *as, const CCallInfo *ci, IRRef *args)
  505. {
  506. uint32_t i, nargs = CCI_XNARGS(ci);
  507. int nslots = 0;
  508. #if LJ_64
  509. if (LJ_ABI_WIN) {
  510. nslots = (int)(nargs*2); /* Only matters for more than four args. */
  511. } else {
  512. int ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
  513. for (i = 0; i < nargs; i++)
  514. if (args[i] && irt_isfp(IR(args[i])->t)) {
  515. if (nfpr > 0) nfpr--; else nslots += 2;
  516. } else {
  517. if (ngpr > 0) ngpr--; else nslots += 2;
  518. }
  519. }
  520. #else
  521. int ngpr = 0;
  522. if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL)
  523. ngpr = 2;
  524. else if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL)
  525. ngpr = 1;
  526. for (i = 0; i < nargs; i++)
  527. if (args[i] && irt_isfp(IR(args[i])->t)) {
  528. nslots += irt_isnum(IR(args[i])->t) ? 2 : 1;
  529. } else {
  530. if (ngpr > 0) ngpr--; else nslots++;
  531. }
  532. #endif
  533. return nslots;
  534. }
  535. /* Generate a call to a C function. */
  536. static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
  537. {
  538. uint32_t n, nargs = CCI_XNARGS(ci);
  539. int32_t ofs = STACKARG_OFS;
  540. #if LJ_64
  541. uint32_t gprs = REGARG_GPRS;
  542. Reg fpr = REGARG_FIRSTFPR;
  543. #if !LJ_ABI_WIN
  544. MCode *patchnfpr = NULL;
  545. #endif
  546. #else
  547. uint32_t gprs = 0;
  548. if ((ci->flags & CCI_CC_MASK) != CCI_CC_CDECL) {
  549. if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL)
  550. gprs = (REGARG_GPRS & 31);
  551. else if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL)
  552. gprs = REGARG_GPRS;
  553. }
  554. #endif
  555. if ((void *)ci->func)
  556. emit_call(as, ci->func);
  557. #if LJ_64
  558. if ((ci->flags & CCI_VARARG)) { /* Special handling for vararg calls. */
  559. #if LJ_ABI_WIN
  560. for (n = 0; n < 4 && n < nargs; n++) {
  561. IRIns *ir = IR(args[n]);
  562. if (irt_isfp(ir->t)) /* Duplicate FPRs in GPRs. */
  563. emit_rr(as, XO_MOVDto, (irt_isnum(ir->t) ? REX_64 : 0) | (fpr+n),
  564. ((gprs >> (n*5)) & 31)); /* Either MOVD or MOVQ. */
  565. }
  566. #else
  567. patchnfpr = --as->mcp; /* Indicate number of used FPRs in register al. */
  568. *--as->mcp = XI_MOVrib | RID_EAX;
  569. #endif
  570. }
  571. #endif
  572. for (n = 0; n < nargs; n++) { /* Setup args. */
  573. IRRef ref = args[n];
  574. IRIns *ir = IR(ref);
  575. Reg r;
  576. #if LJ_64 && LJ_ABI_WIN
  577. /* Windows/x64 argument registers are strictly positional. */
  578. r = irt_isfp(ir->t) ? (fpr <= REGARG_LASTFPR ? fpr : 0) : (gprs & 31);
  579. fpr++; gprs >>= 5;
  580. #elif LJ_64
  581. /* POSIX/x64 argument registers are used in order of appearance. */
  582. if (irt_isfp(ir->t)) {
  583. r = fpr <= REGARG_LASTFPR ? fpr++ : 0;
  584. } else {
  585. r = gprs & 31; gprs >>= 5;
  586. }
  587. #else
  588. if (ref && irt_isfp(ir->t)) {
  589. r = 0;
  590. } else {
  591. r = gprs & 31; gprs >>= 5;
  592. if (!ref) continue;
  593. }
  594. #endif
  595. if (r) { /* Argument is in a register. */
  596. if (r < RID_MAX_GPR && ref < ASMREF_TMP1) {
  597. #if LJ_64
  598. if (LJ_GC64 ? !(ir->o == IR_KINT || ir->o == IR_KNULL) : ir->o == IR_KINT64)
  599. emit_loadu64(as, r, ir_k64(ir)->u64);
  600. else
  601. #endif
  602. emit_loadi(as, r, ir->i);
  603. } else {
  604. /* Must have been evicted. */
  605. lj_assertA(rset_test(as->freeset, r), "reg %d not free", r);
  606. if (ra_hasreg(ir->r)) {
  607. ra_noweak(as, ir->r);
  608. emit_movrr(as, ir, r, ir->r);
  609. } else {
  610. ra_allocref(as, ref, RID2RSET(r));
  611. }
  612. }
  613. } else if (irt_isfp(ir->t)) { /* FP argument is on stack. */
  614. lj_assertA(!(irt_isfloat(ir->t) && irref_isk(ref)),
  615. "unexpected float constant");
  616. if (LJ_32 && (ofs & 4) && irref_isk(ref)) {
  617. /* Split stores for unaligned FP consts. */
  618. emit_movmroi(as, RID_ESP, ofs, (int32_t)ir_knum(ir)->u32.lo);
  619. emit_movmroi(as, RID_ESP, ofs+4, (int32_t)ir_knum(ir)->u32.hi);
  620. } else {
  621. r = ra_alloc1(as, ref, RSET_FPR);
  622. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto,
  623. r, RID_ESP, ofs);
  624. }
  625. ofs += (LJ_32 && irt_isfloat(ir->t)) ? 4 : 8;
  626. } else { /* Non-FP argument is on stack. */
  627. if (LJ_32 && ref < ASMREF_TMP1) {
  628. emit_movmroi(as, RID_ESP, ofs, ir->i);
  629. } else {
  630. r = ra_alloc1(as, ref, RSET_GPR);
  631. emit_movtomro(as, REX_64 + r, RID_ESP, ofs);
  632. }
  633. ofs += sizeof(intptr_t);
  634. }
  635. checkmclim(as);
  636. }
  637. #if LJ_64 && !LJ_ABI_WIN
  638. if (patchnfpr) *patchnfpr = fpr - REGARG_FIRSTFPR;
  639. #endif
  640. }
  641. /* Setup result reg/sp for call. Evict scratch regs. */
  642. static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
  643. {
  644. RegSet drop = RSET_SCRATCH;
  645. int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t));
  646. if ((ci->flags & CCI_NOFPRCLOBBER))
  647. drop &= ~RSET_FPR;
  648. if (ra_hasreg(ir->r))
  649. rset_clear(drop, ir->r); /* Dest reg handled below. */
  650. if (hiop && ra_hasreg((ir+1)->r))
  651. rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
  652. ra_evictset(as, drop); /* Evictions must be performed first. */
  653. if (ra_used(ir)) {
  654. if (irt_isfp(ir->t)) {
  655. int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */
  656. #if LJ_64
  657. if ((ci->flags & CCI_CASTU64)) {
  658. Reg dest = ir->r;
  659. if (ra_hasreg(dest)) {
  660. ra_free(as, dest);
  661. ra_modified(as, dest);
  662. emit_rr(as, XO_MOVD, dest|REX_64, RID_RET); /* Really MOVQ. */
  663. }
  664. if (ofs) emit_movtomro(as, RID_RET|REX_64, RID_ESP, ofs);
  665. } else {
  666. ra_destreg(as, ir, RID_FPRET);
  667. }
  668. #else
  669. /* Number result is in x87 st0 for x86 calling convention. */
  670. Reg dest = ir->r;
  671. if (ra_hasreg(dest)) {
  672. ra_free(as, dest);
  673. ra_modified(as, dest);
  674. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS,
  675. dest, RID_ESP, ofs);
  676. }
  677. if ((ci->flags & CCI_CASTU64)) {
  678. emit_movtomro(as, RID_RETLO, RID_ESP, ofs);
  679. emit_movtomro(as, RID_RETHI, RID_ESP, ofs+4);
  680. } else {
  681. emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd,
  682. irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs);
  683. }
  684. #endif
  685. } else if (hiop) {
  686. ra_destpair(as, ir);
  687. } else {
  688. lj_assertA(!irt_ispri(ir->t), "PRI dest");
  689. ra_destreg(as, ir, RID_RET);
  690. }
  691. } else if (LJ_32 && irt_isfp(ir->t) && !(ci->flags & CCI_CASTU64)) {
  692. emit_x87op(as, XI_FPOP); /* Pop unused result from x87 st0. */
  693. }
  694. }
  695. /* Return a constant function pointer or NULL for indirect calls. */
  696. static void *asm_callx_func(ASMState *as, IRIns *irf, IRRef func)
  697. {
  698. #if LJ_32
  699. UNUSED(as);
  700. if (irref_isk(func))
  701. return (void *)irf->i;
  702. #else
  703. if (irref_isk(func)) {
  704. MCode *p;
  705. if (irf->o == IR_KINT64)
  706. p = (MCode *)(void *)ir_k64(irf)->u64;
  707. else
  708. p = (MCode *)(void *)(uintptr_t)(uint32_t)irf->i;
  709. if (p - as->mcp == (int32_t)(p - as->mcp))
  710. return p; /* Call target is still in +-2GB range. */
  711. /* Avoid the indirect case of emit_call(). Try to hoist func addr. */
  712. }
  713. #endif
  714. return NULL;
  715. }
  716. static void asm_callx(ASMState *as, IRIns *ir)
  717. {
  718. IRRef args[CCI_NARGS_MAX*2];
  719. CCallInfo ci;
  720. IRRef func;
  721. IRIns *irf;
  722. int32_t spadj = 0;
  723. ci.flags = asm_callx_flags(as, ir);
  724. asm_collectargs(as, ir, &ci, args);
  725. asm_setupresult(as, ir, &ci);
  726. #if LJ_32
  727. /* Have to readjust stack after non-cdecl calls due to callee cleanup. */
  728. if ((ci.flags & CCI_CC_MASK) != CCI_CC_CDECL)
  729. spadj = 4 * asm_count_call_slots(as, &ci, args);
  730. #endif
  731. func = ir->op2; irf = IR(func);
  732. if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
  733. ci.func = (ASMFunction)asm_callx_func(as, irf, func);
  734. if (!(void *)ci.func) {
  735. /* Use a (hoistable) non-scratch register for indirect calls. */
  736. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
  737. Reg r = ra_alloc1(as, func, allow);
  738. if (LJ_32) emit_spsub(as, spadj); /* Above code may cause restores! */
  739. emit_rr(as, XO_GROUP5, XOg_CALL, r);
  740. } else if (LJ_32) {
  741. emit_spsub(as, spadj);
  742. }
  743. asm_gencall(as, &ci, args);
  744. }
  745. /* -- Returns ------------------------------------------------------------- */
  746. /* Return to lower frame. Guard that it goes to the right spot. */
  747. static void asm_retf(ASMState *as, IRIns *ir)
  748. {
  749. Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
  750. #if LJ_FR2
  751. Reg rpc = ra_scratch(as, rset_exclude(RSET_GPR, base));
  752. #endif
  753. void *pc = ir_kptr(IR(ir->op2));
  754. int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));
  755. as->topslot -= (BCReg)delta;
  756. if ((int32_t)as->topslot < 0) as->topslot = 0;
  757. irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */
  758. emit_setgl(as, base, jit_base);
  759. emit_addptr(as, base, -8*delta);
  760. asm_guardcc(as, CC_NE);
  761. #if LJ_FR2
  762. emit_rmro(as, XO_CMP, rpc|REX_GC64, base, -8);
  763. emit_loadu64(as, rpc, u64ptr(pc));
  764. #else
  765. emit_gmroi(as, XG_ARITHi(XOg_CMP), base, -4, ptr2addr(pc));
  766. #endif
  767. }
  768. /* -- Buffer operations --------------------------------------------------- */
  769. #if LJ_HASBUFFER
  770. static void asm_bufhdr_write(ASMState *as, Reg sb)
  771. {
  772. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb));
  773. IRIns irgc;
  774. irgc.ot = IRT(0, IRT_PGC); /* GC type. */
  775. emit_storeofs(as, &irgc, tmp, sb, offsetof(SBuf, L));
  776. emit_opgl(as, XO_ARITH(XOg_OR), tmp|REX_GC64, cur_L);
  777. emit_gri(as, XG_ARITHi(XOg_AND), tmp, SBUF_MASK_FLAG);
  778. emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L));
  779. }
  780. #endif
  781. /* -- Type conversions ---------------------------------------------------- */
  782. static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
  783. {
  784. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
  785. Reg dest = ra_dest(as, ir, RSET_GPR);
  786. asm_guardcc(as, CC_P);
  787. asm_guardcc(as, CC_NE);
  788. emit_rr(as, XO_UCOMISD, left, tmp);
  789. emit_rr(as, XO_CVTSI2SD, tmp, dest);
  790. emit_rr(as, XO_XORPS, tmp, tmp); /* Avoid partial register stall. */
  791. checkmclim(as);
  792. emit_rr(as, XO_CVTTSD2SI, dest, left);
  793. /* Can't fuse since left is needed twice. */
  794. }
  795. static void asm_tobit(ASMState *as, IRIns *ir)
  796. {
  797. Reg dest = ra_dest(as, ir, RSET_GPR);
  798. Reg tmp = ra_noreg(IR(ir->op1)->r) ?
  799. ra_alloc1(as, ir->op1, RSET_FPR) :
  800. ra_scratch(as, RSET_FPR);
  801. Reg right;
  802. emit_rr(as, XO_MOVDto, tmp, dest);
  803. right = asm_fuseload(as, ir->op2, rset_exclude(RSET_FPR, tmp));
  804. emit_mrm(as, XO_ADDSD, tmp, right);
  805. ra_left(as, tmp, ir->op1);
  806. }
  807. static void asm_conv(ASMState *as, IRIns *ir)
  808. {
  809. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
  810. int st64 = (st == IRT_I64 || st == IRT_U64 || (LJ_64 && st == IRT_P64));
  811. int stfp = (st == IRT_NUM || st == IRT_FLOAT);
  812. IRRef lref = ir->op1;
  813. lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV");
  814. lj_assertA(!(LJ_32 && (irt_isint64(ir->t) || st64)),
  815. "IR %04d has unsplit 64 bit type",
  816. (int)(ir - as->ir) - REF_BIAS);
  817. if (irt_isfp(ir->t)) {
  818. Reg dest = ra_dest(as, ir, RSET_FPR);
  819. if (stfp) { /* FP to FP conversion. */
  820. Reg left = asm_fuseload(as, lref, RSET_FPR);
  821. emit_mrm(as, st == IRT_NUM ? XO_CVTSD2SS : XO_CVTSS2SD, dest, left);
  822. if (left == dest) return; /* Avoid the XO_XORPS. */
  823. } else if (LJ_32 && st == IRT_U32) { /* U32 to FP conversion on x86. */
  824. /* number = (2^52+2^51 .. u32) - (2^52+2^51) */
  825. cTValue *k = &as->J->k64[LJ_K64_TOBIT];
  826. Reg bias = ra_scratch(as, rset_exclude(RSET_FPR, dest));
  827. if (irt_isfloat(ir->t))
  828. emit_rr(as, XO_CVTSD2SS, dest, dest);
  829. emit_rr(as, XO_SUBSD, dest, bias); /* Subtract 2^52+2^51 bias. */
  830. emit_rr(as, XO_XORPS, dest, bias); /* Merge bias and integer. */
  831. emit_rma(as, XO_MOVSD, bias, k);
  832. checkmclim(as);
  833. emit_mrm(as, XO_MOVD, dest, asm_fuseload(as, lref, RSET_GPR));
  834. return;
  835. } else { /* Integer to FP conversion. */
  836. Reg left = (LJ_64 && (st == IRT_U32 || st == IRT_U64)) ?
  837. ra_alloc1(as, lref, RSET_GPR) :
  838. asm_fuseloadm(as, lref, RSET_GPR, st64);
  839. if (LJ_64 && st == IRT_U64) {
  840. MCLabel l_end = emit_label(as);
  841. cTValue *k = &as->J->k64[LJ_K64_2P64];
  842. emit_rma(as, XO_ADDSD, dest, k); /* Add 2^64 to compensate. */
  843. emit_sjcc(as, CC_NS, l_end);
  844. emit_rr(as, XO_TEST, left|REX_64, left); /* Check if u64 >= 2^63. */
  845. }
  846. emit_mrm(as, irt_isnum(ir->t) ? XO_CVTSI2SD : XO_CVTSI2SS,
  847. dest|((LJ_64 && (st64 || st == IRT_U32)) ? REX_64 : 0), left);
  848. }
  849. emit_rr(as, XO_XORPS, dest, dest); /* Avoid partial register stall. */
  850. } else if (stfp) { /* FP to integer conversion. */
  851. if (irt_isguard(ir->t)) {
  852. /* Checked conversions are only supported from number to int. */
  853. lj_assertA(irt_isint(ir->t) && st == IRT_NUM,
  854. "bad type for checked CONV");
  855. asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
  856. } else {
  857. Reg dest = ra_dest(as, ir, RSET_GPR);
  858. x86Op op = st == IRT_NUM ? XO_CVTTSD2SI : XO_CVTTSS2SI;
  859. if (LJ_64 ? irt_isu64(ir->t) : irt_isu32(ir->t)) {
  860. /* LJ_64: For inputs >= 2^63 add -2^64, convert again. */
  861. /* LJ_32: For inputs >= 2^31 add -2^31, convert again and add 2^31. */
  862. Reg tmp = ra_noreg(IR(lref)->r) ? ra_alloc1(as, lref, RSET_FPR) :
  863. ra_scratch(as, RSET_FPR);
  864. MCLabel l_end = emit_label(as);
  865. if (LJ_32)
  866. emit_gri(as, XG_ARITHi(XOg_ADD), dest, (int32_t)0x80000000);
  867. emit_rr(as, op, dest|REX_64, tmp);
  868. if (st == IRT_NUM)
  869. emit_rma(as, XO_ADDSD, tmp, &as->J->k64[LJ_K64_M2P64_31]);
  870. else
  871. emit_rma(as, XO_ADDSS, tmp, &as->J->k32[LJ_K32_M2P64_31]);
  872. emit_sjcc(as, CC_NS, l_end);
  873. emit_rr(as, XO_TEST, dest|REX_64, dest); /* Check if dest negative. */
  874. emit_rr(as, op, dest|REX_64, tmp);
  875. ra_left(as, tmp, lref);
  876. } else {
  877. if (LJ_64 && irt_isu32(ir->t))
  878. emit_rr(as, XO_MOV, dest, dest); /* Zero hiword. */
  879. emit_mrm(as, op,
  880. dest|((LJ_64 &&
  881. (irt_is64(ir->t) || irt_isu32(ir->t))) ? REX_64 : 0),
  882. asm_fuseload(as, lref, RSET_FPR));
  883. }
  884. }
  885. } else if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
  886. Reg left, dest = ra_dest(as, ir, RSET_GPR);
  887. RegSet allow = RSET_GPR;
  888. x86Op op;
  889. lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT");
  890. if (st == IRT_I8) {
  891. op = XO_MOVSXb; allow = RSET_GPR8; dest |= FORCE_REX;
  892. } else if (st == IRT_U8) {
  893. op = XO_MOVZXb; allow = RSET_GPR8; dest |= FORCE_REX;
  894. } else if (st == IRT_I16) {
  895. op = XO_MOVSXw;
  896. } else {
  897. op = XO_MOVZXw;
  898. }
  899. left = asm_fuseload(as, lref, allow);
  900. /* Add extra MOV if source is already in wrong register. */
  901. if (!LJ_64 && left != RID_MRM && !rset_test(allow, left)) {
  902. Reg tmp = ra_scratch(as, allow);
  903. emit_rr(as, op, dest, tmp);
  904. emit_rr(as, XO_MOV, tmp, left);
  905. } else {
  906. emit_mrm(as, op, dest, left);
  907. }
  908. } else { /* 32/64 bit integer conversions. */
  909. if (LJ_32) { /* Only need to handle 32/32 bit no-op (cast) on x86. */
  910. Reg dest = ra_dest(as, ir, RSET_GPR);
  911. ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */
  912. } else if (irt_is64(ir->t)) {
  913. Reg dest = ra_dest(as, ir, RSET_GPR);
  914. if (st64 || !(ir->op2 & IRCONV_SEXT)) {
  915. /* 64/64 bit no-op (cast) or 32 to 64 bit zero extension. */
  916. ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */
  917. } else { /* 32 to 64 bit sign extension. */
  918. Reg left = asm_fuseload(as, lref, RSET_GPR);
  919. emit_mrm(as, XO_MOVSXd, dest|REX_64, left);
  920. }
  921. } else {
  922. Reg dest = ra_dest(as, ir, RSET_GPR);
  923. if (st64 && !(ir->op2 & IRCONV_NONE)) {
  924. Reg left = asm_fuseload(as, lref, RSET_GPR);
  925. /* This is either a 32 bit reg/reg mov which zeroes the hiword
  926. ** or a load of the loword from a 64 bit address.
  927. */
  928. emit_mrm(as, XO_MOV, dest, left);
  929. } else { /* 32/32 bit no-op (cast). */
  930. ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */
  931. }
  932. }
  933. }
  934. }
  935. #if LJ_32 && LJ_HASFFI
  936. /* No SSE conversions to/from 64 bit on x86, so resort to ugly x87 code. */
  937. /* 64 bit integer to FP conversion in 32 bit mode. */
  938. static void asm_conv_fp_int64(ASMState *as, IRIns *ir)
  939. {
  940. Reg hi = ra_alloc1(as, ir->op1, RSET_GPR);
  941. Reg lo = ra_alloc1(as, (ir-1)->op1, rset_exclude(RSET_GPR, hi));
  942. int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */
  943. Reg dest = ir->r;
  944. if (ra_hasreg(dest)) {
  945. ra_free(as, dest);
  946. ra_modified(as, dest);
  947. emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS, dest, RID_ESP, ofs);
  948. }
  949. emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd,
  950. irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs);
  951. if (((ir-1)->op2 & IRCONV_SRCMASK) == IRT_U64) {
  952. /* For inputs in [2^63,2^64-1] add 2^64 to compensate. */
  953. MCLabel l_end = emit_label(as);
  954. emit_rma(as, XO_FADDq, XOg_FADDq, &as->J->k64[LJ_K64_2P64]);
  955. emit_sjcc(as, CC_NS, l_end);
  956. emit_rr(as, XO_TEST, hi, hi); /* Check if u64 >= 2^63. */
  957. } else {
  958. lj_assertA(((ir-1)->op2 & IRCONV_SRCMASK) == IRT_I64, "bad type for CONV");
  959. }
  960. emit_rmro(as, XO_FILDq, XOg_FILDq, RID_ESP, 0);
  961. /* NYI: Avoid narrow-to-wide store-to-load forwarding stall. */
  962. emit_rmro(as, XO_MOVto, hi, RID_ESP, 4);
  963. emit_rmro(as, XO_MOVto, lo, RID_ESP, 0);
  964. }
  965. /* FP to 64 bit integer conversion in 32 bit mode. */
  966. static void asm_conv_int64_fp(ASMState *as, IRIns *ir)
  967. {
  968. IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK);
  969. IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH);
  970. Reg lo, hi;
  971. lj_assertA(st == IRT_NUM || st == IRT_FLOAT, "bad type for CONV");
  972. lj_assertA(dt == IRT_I64 || dt == IRT_U64, "bad type for CONV");
  973. hi = ra_dest(as, ir, RSET_GPR);
  974. lo = ra_dest(as, ir-1, rset_exclude(RSET_GPR, hi));
  975. if (ra_used(ir-1)) emit_rmro(as, XO_MOV, lo, RID_ESP, 0);
  976. /* NYI: Avoid wide-to-narrow store-to-load forwarding stall. */
  977. if (!(as->flags & JIT_F_SSE3)) { /* Set FPU rounding mode to default. */
  978. emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 4);
  979. emit_rmro(as, XO_MOVto, lo, RID_ESP, 4);
  980. emit_gri(as, XG_ARITHi(XOg_AND), lo, 0xf3ff);
  981. }
  982. if (dt == IRT_U64) {
  983. /* For inputs in [2^63,2^64-1] add -2^64 and convert again. */
  984. MCLabel l_pop, l_end = emit_label(as);
  985. emit_x87op(as, XI_FPOP);
  986. l_pop = emit_label(as);
  987. emit_sjmp(as, l_end);
  988. emit_rmro(as, XO_MOV, hi, RID_ESP, 4);
  989. if ((as->flags & JIT_F_SSE3))
  990. emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0);
  991. else
  992. emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0);
  993. emit_rma(as, XO_FADDq, XOg_FADDq, &as->J->k64[LJ_K64_M2P64]);
  994. emit_sjcc(as, CC_NS, l_pop);
  995. emit_rr(as, XO_TEST, hi, hi); /* Check if out-of-range (2^63). */
  996. }
  997. emit_rmro(as, XO_MOV, hi, RID_ESP, 4);
  998. if ((as->flags & JIT_F_SSE3)) { /* Truncation is easy with SSE3. */
  999. emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0);
  1000. } else { /* Otherwise set FPU rounding mode to truncate before the store. */
  1001. emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0);
  1002. emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 0);
  1003. emit_rmro(as, XO_MOVtow, lo, RID_ESP, 0);
  1004. emit_rmro(as, XO_ARITHw(XOg_OR), lo, RID_ESP, 0);
  1005. emit_loadi(as, lo, 0xc00);
  1006. emit_rmro(as, XO_FNSTCW, XOg_FNSTCW, RID_ESP, 0);
  1007. }
  1008. if (dt == IRT_U64)
  1009. emit_x87op(as, XI_FDUP);
  1010. emit_mrm(as, st == IRT_NUM ? XO_FLDq : XO_FLDd,
  1011. st == IRT_NUM ? XOg_FLDq: XOg_FLDd,
  1012. asm_fuseload(as, ir->op1, RSET_EMPTY));
  1013. }
  1014. static void asm_conv64(ASMState *as, IRIns *ir)
  1015. {
  1016. if (irt_isfp(ir->t))
  1017. asm_conv_fp_int64(as, ir);
  1018. else
  1019. asm_conv_int64_fp(as, ir);
  1020. }
  1021. #endif
  1022. static void asm_strto(ASMState *as, IRIns *ir)
  1023. {
  1024. /* Force a spill slot for the destination register (if any). */
  1025. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];
  1026. IRRef args[2];
  1027. RegSet drop = RSET_SCRATCH;
  1028. if ((drop & RSET_FPR) != RSET_FPR && ra_hasreg(ir->r))
  1029. rset_set(drop, ir->r); /* WIN64 doesn't spill all FPRs. */
  1030. ra_evictset(as, drop);
  1031. asm_guardcc(as, CC_E);
  1032. emit_rr(as, XO_TEST, RID_RET, RID_RET); /* Test return status. */
  1033. args[0] = ir->op1; /* GCstr *str */
  1034. args[1] = ASMREF_TMP1; /* TValue *n */
  1035. asm_gencall(as, ci, args);
  1036. /* Store the result to the spill slot or temp slots. */
  1037. emit_rmro(as, XO_LEA, ra_releasetmp(as, ASMREF_TMP1)|REX_64,
  1038. RID_ESP, sps_scale(ir->s));
  1039. }
  1040. /* -- Memory references --------------------------------------------------- */
  1041. /* Get pointer to TValue. */
  1042. static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode)
  1043. {
  1044. if ((mode & IRTMPREF_IN1)) {
  1045. IRIns *ir = IR(ref);
  1046. if (irt_isnum(ir->t)) {
  1047. if (irref_isk(ref) && !(mode & IRTMPREF_OUT1)) {
  1048. /* Use the number constant itself as a TValue. */
  1049. emit_loada(as, dest, ir_knum(ir));
  1050. return;
  1051. }
  1052. emit_rmro(as, XO_MOVSDto, ra_alloc1(as, ref, RSET_FPR), dest, 0);
  1053. } else {
  1054. #if LJ_GC64
  1055. if (irref_isk(ref)) {
  1056. TValue k;
  1057. lj_ir_kvalue(as->J->L, &k, ir);
  1058. emit_movmroi(as, dest, 4, k.u32.hi);
  1059. emit_movmroi(as, dest, 0, k.u32.lo);
  1060. } else {
  1061. /* TODO: 64 bit store + 32 bit load-modify-store is suboptimal. */
  1062. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, dest));
  1063. if (irt_is64(ir->t)) {
  1064. emit_u32(as, irt_toitype(ir->t) << 15);
  1065. emit_rmro(as, XO_ARITHi, XOg_OR, dest, 4);
  1066. } else {
  1067. emit_movmroi(as, dest, 4, (irt_toitype(ir->t) << 15));
  1068. }
  1069. emit_movtomro(as, REX_64IR(ir, src), dest, 0);
  1070. }
  1071. #else
  1072. if (!irref_isk(ref)) {
  1073. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, dest));
  1074. emit_movtomro(as, REX_64IR(ir, src), dest, 0);
  1075. } else if (!irt_ispri(ir->t)) {
  1076. emit_movmroi(as, dest, 0, ir->i);
  1077. }
  1078. if (!(LJ_64 && irt_islightud(ir->t)))
  1079. emit_movmroi(as, dest, 4, irt_toitype(ir->t));
  1080. #endif
  1081. }
  1082. }
  1083. emit_loada(as, dest, &J2G(as->J)->tmptv); /* g->tmptv holds the TValue(s). */
  1084. }
  1085. static void asm_aref(ASMState *as, IRIns *ir)
  1086. {
  1087. Reg dest = ra_dest(as, ir, RSET_GPR);
  1088. asm_fusearef(as, ir, RSET_GPR);
  1089. if (!(as->mrm.idx == RID_NONE && as->mrm.ofs == 0))
  1090. emit_mrm(as, XO_LEA, dest|REX_GC64, RID_MRM);
  1091. else if (as->mrm.base != dest)
  1092. emit_rr(as, XO_MOV, dest|REX_GC64, as->mrm.base);
  1093. }
  1094. /* Inlined hash lookup. Specialized for key type and for const keys.
  1095. ** The equivalent C code is:
  1096. ** Node *n = hashkey(t, key);
  1097. ** do {
  1098. ** if (lj_obj_equal(&n->key, key)) return &n->val;
  1099. ** } while ((n = nextnode(n)));
  1100. ** return niltv(L);
  1101. */
  1102. static void asm_href(ASMState *as, IRIns *ir, IROp merge)
  1103. {
  1104. RegSet allow = RSET_GPR;
  1105. int destused = ra_used(ir);
  1106. Reg dest = ra_dest(as, ir, allow);
  1107. Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
  1108. Reg key = RID_NONE, tmp = RID_NONE;
  1109. IRIns *irkey = IR(ir->op2);
  1110. int isk = irref_isk(ir->op2);
  1111. IRType1 kt = irkey->t;
  1112. uint32_t khash;
  1113. MCLabel l_end, l_loop, l_next;
  1114. if (!isk) {
  1115. rset_clear(allow, tab);
  1116. key = ra_alloc1(as, ir->op2, irt_isnum(kt) ? RSET_FPR : allow);
  1117. if (LJ_GC64 || !irt_isstr(kt))
  1118. tmp = ra_scratch(as, rset_exclude(allow, key));
  1119. }
  1120. /* Key not found in chain: jump to exit (if merged) or load niltv. */
  1121. l_end = emit_label(as);
  1122. if (merge == IR_NE)
  1123. asm_guardcc(as, CC_E); /* XI_JMP is not found by lj_asm_patchexit. */
  1124. else if (destused)
  1125. emit_loada(as, dest, niltvg(J2G(as->J)));
  1126. /* Follow hash chain until the end. */
  1127. l_loop = emit_sjcc_label(as, CC_NZ);
  1128. emit_rr(as, XO_TEST, dest|REX_GC64, dest);
  1129. emit_rmro(as, XO_MOV, dest|REX_GC64, dest, offsetof(Node, next));
  1130. l_next = emit_label(as);
  1131. /* Type and value comparison. */
  1132. if (merge == IR_EQ)
  1133. asm_guardcc(as, CC_E);
  1134. else
  1135. emit_sjcc(as, CC_E, l_end);
  1136. checkmclim(as);
  1137. if (irt_isnum(kt)) {
  1138. if (isk) {
  1139. /* Assumes -0.0 is already canonicalized to +0.0. */
  1140. emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.lo),
  1141. (int32_t)ir_knum(irkey)->u32.lo);
  1142. emit_sjcc(as, CC_NE, l_next);
  1143. emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.hi),
  1144. (int32_t)ir_knum(irkey)->u32.hi);
  1145. } else {
  1146. emit_sjcc(as, CC_P, l_next);
  1147. emit_rmro(as, XO_UCOMISD, key, dest, offsetof(Node, key.n));
  1148. emit_sjcc(as, CC_AE, l_next);
  1149. /* The type check avoids NaN penalties and complaints from Valgrind. */
  1150. #if LJ_64 && !LJ_GC64
  1151. emit_u32(as, LJ_TISNUM);
  1152. emit_rmro(as, XO_ARITHi, XOg_CMP, dest, offsetof(Node, key.it));
  1153. #else
  1154. emit_i8(as, LJ_TISNUM);
  1155. emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it));
  1156. #endif
  1157. }
  1158. #if LJ_64 && !LJ_GC64
  1159. } else if (irt_islightud(kt)) {
  1160. emit_rmro(as, XO_CMP, key|REX_64, dest, offsetof(Node, key.u64));
  1161. #endif
  1162. #if LJ_GC64
  1163. } else if (irt_isaddr(kt)) {
  1164. if (isk) {
  1165. TValue k;
  1166. k.u64 = ((uint64_t)irt_toitype(irkey->t) << 47) | irkey[1].tv.u64;
  1167. emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.lo),
  1168. k.u32.lo);
  1169. emit_sjcc(as, CC_NE, l_next);
  1170. emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.hi),
  1171. k.u32.hi);
  1172. } else {
  1173. emit_rmro(as, XO_CMP, tmp|REX_64, dest, offsetof(Node, key.u64));
  1174. }
  1175. } else {
  1176. lj_assertA(irt_ispri(kt) && !irt_isnil(kt), "bad HREF key type");
  1177. emit_u32(as, (irt_toitype(kt)<<15)|0x7fff);
  1178. emit_rmro(as, XO_ARITHi, XOg_CMP, dest, offsetof(Node, key.it));
  1179. #else
  1180. } else {
  1181. if (!irt_ispri(kt)) {
  1182. lj_assertA(irt_isaddr(kt), "bad HREF key type");
  1183. if (isk)
  1184. emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.gcr),
  1185. ptr2addr(ir_kgc(irkey)));
  1186. else
  1187. emit_rmro(as, XO_CMP, key, dest, offsetof(Node, key.gcr));
  1188. emit_sjcc(as, CC_NE, l_next);
  1189. }
  1190. lj_assertA(!irt_isnil(kt), "bad HREF key type");
  1191. emit_i8(as, irt_toitype(kt));
  1192. emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it));
  1193. #endif
  1194. }
  1195. emit_sfixup(as, l_loop);
  1196. #if LJ_GC64
  1197. if (!isk && irt_isaddr(kt)) {
  1198. emit_rr(as, XO_OR, tmp|REX_64, key);
  1199. emit_loadu64(as, tmp, (uint64_t)irt_toitype(kt) << 47);
  1200. }
  1201. #endif
  1202. /* Load main position relative to tab->node into dest. */
  1203. khash = isk ? ir_khash(as, irkey) : 1;
  1204. if (khash == 0) {
  1205. emit_rmro(as, XO_MOV, dest|REX_GC64, tab, offsetof(GCtab, node));
  1206. } else {
  1207. emit_rmro(as, XO_ARITH(XOg_ADD), dest|REX_GC64, tab, offsetof(GCtab,node));
  1208. emit_shifti(as, XOg_SHL, dest, 3);
  1209. emit_rmrxo(as, XO_LEA, dest, dest, dest, XM_SCALE2, 0);
  1210. if (isk) {
  1211. emit_gri(as, XG_ARITHi(XOg_AND), dest, (int32_t)khash);
  1212. emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask));
  1213. } else if (irt_isstr(kt)) {
  1214. emit_rmro(as, XO_ARITH(XOg_AND), dest, key, offsetof(GCstr, sid));
  1215. emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask));
  1216. } else { /* Must match with hashrot() in lj_tab.c. */
  1217. emit_rmro(as, XO_ARITH(XOg_AND), dest, tab, offsetof(GCtab, hmask));
  1218. emit_rr(as, XO_ARITH(XOg_SUB), dest, tmp);
  1219. emit_shifti(as, XOg_ROL, tmp, HASH_ROT3);
  1220. emit_rr(as, XO_ARITH(XOg_XOR), dest, tmp);
  1221. checkmclim(as);
  1222. emit_shifti(as, XOg_ROL, dest, HASH_ROT2);
  1223. emit_rr(as, XO_ARITH(XOg_SUB), tmp, dest);
  1224. emit_shifti(as, XOg_ROL, dest, HASH_ROT1);
  1225. emit_rr(as, XO_ARITH(XOg_XOR), tmp, dest);
  1226. if (irt_isnum(kt)) {
  1227. emit_rr(as, XO_ARITH(XOg_ADD), dest, dest);
  1228. #if LJ_64
  1229. emit_shifti(as, XOg_SHR|REX_64, dest, 32);
  1230. emit_rr(as, XO_MOV, tmp, dest);
  1231. emit_rr(as, XO_MOVDto, key|REX_64, dest);
  1232. #else
  1233. emit_rmro(as, XO_MOV, dest, RID_ESP, ra_spill(as, irkey)+4);
  1234. emit_rr(as, XO_MOVDto, key, tmp);
  1235. #endif
  1236. } else {
  1237. emit_rr(as, XO_MOV, tmp, key);
  1238. #if LJ_GC64
  1239. emit_gri(as, XG_ARITHi(XOg_XOR), dest, irt_toitype(kt) << 15);
  1240. if ((as->flags & JIT_F_BMI2)) {
  1241. emit_i8(as, 32);
  1242. emit_mrm(as, XV_RORX|VEX_64, dest, key);
  1243. } else {
  1244. emit_shifti(as, XOg_SHR|REX_64, dest, 32);
  1245. emit_rr(as, XO_MOV, dest|REX_64, key|REX_64);
  1246. }
  1247. #else
  1248. emit_rmro(as, XO_LEA, dest, key, HASH_BIAS);
  1249. #endif
  1250. }
  1251. }
  1252. }
  1253. }
  1254. static void asm_hrefk(ASMState *as, IRIns *ir)
  1255. {
  1256. IRIns *kslot = IR(ir->op2);
  1257. IRIns *irkey = IR(kslot->op1);
  1258. int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
  1259. Reg dest = ra_used(ir) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
  1260. Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
  1261. #if !LJ_64
  1262. MCLabel l_exit;
  1263. #endif
  1264. lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot");
  1265. if (ra_hasreg(dest)) {
  1266. if (ofs != 0) {
  1267. if (dest == node)
  1268. emit_gri(as, XG_ARITHi(XOg_ADD), dest|REX_GC64, ofs);
  1269. else
  1270. emit_rmro(as, XO_LEA, dest|REX_GC64, node, ofs);
  1271. } else if (dest != node) {
  1272. emit_rr(as, XO_MOV, dest|REX_GC64, node);
  1273. }
  1274. }
  1275. asm_guardcc(as, CC_NE);
  1276. #if LJ_64
  1277. if (!irt_ispri(irkey->t)) {
  1278. Reg key = ra_scratch(as, rset_exclude(RSET_GPR, node));
  1279. emit_rmro(as, XO_CMP, key|REX_64, node,
  1280. ofs + (int32_t)offsetof(Node, key.u64));
  1281. lj_assertA(irt_isnum(irkey->t) || irt_isgcv(irkey->t),
  1282. "bad HREFK key type");
  1283. /* Assumes -0.0 is already canonicalized to +0.0. */
  1284. emit_loadu64(as, key, irt_isnum(irkey->t) ? ir_knum(irkey)->u64 :
  1285. #if LJ_GC64
  1286. ((uint64_t)irt_toitype(irkey->t) << 47) |
  1287. (uint64_t)ir_kgc(irkey));
  1288. #else
  1289. ((uint64_t)irt_toitype(irkey->t) << 32) |
  1290. (uint64_t)(uint32_t)ptr2addr(ir_kgc(irkey)));
  1291. #endif
  1292. } else {
  1293. lj_assertA(!irt_isnil(irkey->t), "bad HREFK key type");
  1294. #if LJ_GC64
  1295. emit_i32(as, (irt_toitype(irkey->t)<<15)|0x7fff);
  1296. emit_rmro(as, XO_ARITHi, XOg_CMP, node,
  1297. ofs + (int32_t)offsetof(Node, key.it));
  1298. #else
  1299. emit_i8(as, irt_toitype(irkey->t));
  1300. emit_rmro(as, XO_ARITHi8, XOg_CMP, node,
  1301. ofs + (int32_t)offsetof(Node, key.it));
  1302. #endif
  1303. }
  1304. #else
  1305. l_exit = emit_label(as);
  1306. if (irt_isnum(irkey->t)) {
  1307. /* Assumes -0.0 is already canonicalized to +0.0. */
  1308. emit_gmroi(as, XG_ARITHi(XOg_CMP), node,
  1309. ofs + (int32_t)offsetof(Node, key.u32.lo),
  1310. (int32_t)ir_knum(irkey)->u32.lo);
  1311. emit_sjcc(as, CC_NE, l_exit);
  1312. emit_gmroi(as, XG_ARITHi(XOg_CMP), node,
  1313. ofs + (int32_t)offsetof(Node, key.u32.hi),
  1314. (int32_t)ir_knum(irkey)->u32.hi);
  1315. } else {
  1316. if (!irt_ispri(irkey->t)) {
  1317. lj_assertA(irt_isgcv(irkey->t), "bad HREFK key type");
  1318. emit_gmroi(as, XG_ARITHi(XOg_CMP), node,
  1319. ofs + (int32_t)offsetof(Node, key.gcr),
  1320. ptr2addr(ir_kgc(irkey)));
  1321. emit_sjcc(as, CC_NE, l_exit);
  1322. }
  1323. lj_assertA(!irt_isnil(irkey->t), "bad HREFK key type");
  1324. emit_i8(as, irt_toitype(irkey->t));
  1325. emit_rmro(as, XO_ARITHi8, XOg_CMP, node,
  1326. ofs + (int32_t)offsetof(Node, key.it));
  1327. }
  1328. #endif
  1329. }
  1330. static void asm_uref(ASMState *as, IRIns *ir)
  1331. {
  1332. Reg dest = ra_dest(as, ir, RSET_GPR);
  1333. int guarded = (irt_t(ir->t) & (IRT_GUARD|IRT_TYPE)) == (IRT_GUARD|IRT_PGC);
  1334. if (irref_isk(ir->op1) && !guarded) {
  1335. GCfunc *fn = ir_kfunc(IR(ir->op1));
  1336. MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
  1337. emit_rma(as, XO_MOV, dest|REX_GC64, v);
  1338. } else {
  1339. Reg uv = ra_scratch(as, RSET_GPR);
  1340. if (ir->o == IR_UREFC)
  1341. emit_rmro(as, XO_LEA, dest|REX_GC64, uv, offsetof(GCupval, tv));
  1342. else
  1343. emit_rmro(as, XO_MOV, dest|REX_GC64, uv, offsetof(GCupval, v));
  1344. if (guarded) {
  1345. asm_guardcc(as, ir->o == IR_UREFC ? CC_E : CC_NE);
  1346. emit_i8(as, 0);
  1347. emit_rmro(as, XO_ARITHib, XOg_CMP, uv, offsetof(GCupval, closed));
  1348. }
  1349. if (irref_isk(ir->op1)) {
  1350. GCfunc *fn = ir_kfunc(IR(ir->op1));
  1351. GCobj *o = gcref(fn->l.uvptr[(ir->op2 >> 8)]);
  1352. emit_loada(as, uv, o);
  1353. } else {
  1354. emit_rmro(as, XO_MOV, uv|REX_GC64, ra_alloc1(as, ir->op1, RSET_GPR),
  1355. (int32_t)offsetof(GCfuncL, uvptr) +
  1356. (int32_t)sizeof(MRef) * (int32_t)(ir->op2 >> 8));
  1357. }
  1358. }
  1359. }
  1360. static void asm_fref(ASMState *as, IRIns *ir)
  1361. {
  1362. Reg dest = ra_dest(as, ir, RSET_GPR);
  1363. asm_fusefref(as, ir, RSET_GPR);
  1364. emit_mrm(as, XO_LEA, dest, RID_MRM);
  1365. }
  1366. static void asm_strref(ASMState *as, IRIns *ir)
  1367. {
  1368. Reg dest = ra_dest(as, ir, RSET_GPR);
  1369. asm_fusestrref(as, ir, RSET_GPR);
  1370. if (as->mrm.base == RID_NONE)
  1371. emit_loadi(as, dest, as->mrm.ofs);
  1372. else if (as->mrm.base == dest && as->mrm.idx == RID_NONE)
  1373. emit_gri(as, XG_ARITHi(XOg_ADD), dest|REX_GC64, as->mrm.ofs);
  1374. else
  1375. emit_mrm(as, XO_LEA, dest|REX_GC64, RID_MRM);
  1376. }
  1377. /* -- Loads and stores ---------------------------------------------------- */
  1378. static void asm_fxload(ASMState *as, IRIns *ir)
  1379. {
  1380. Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
  1381. x86Op xo;
  1382. if (ir->o == IR_FLOAD)
  1383. asm_fusefref(as, ir, RSET_GPR);
  1384. else
  1385. asm_fusexref(as, ir->op1, RSET_GPR);
  1386. /* ir->op2 is ignored -- unaligned loads are ok on x86. */
  1387. switch (irt_type(ir->t)) {
  1388. case IRT_I8: xo = XO_MOVSXb; break;
  1389. case IRT_U8: xo = XO_MOVZXb; break;
  1390. case IRT_I16: xo = XO_MOVSXw; break;
  1391. case IRT_U16: xo = XO_MOVZXw; break;
  1392. case IRT_NUM: xo = XO_MOVSD; break;
  1393. case IRT_FLOAT: xo = XO_MOVSS; break;
  1394. default:
  1395. if (LJ_64 && irt_is64(ir->t))
  1396. dest |= REX_64;
  1397. else
  1398. lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t),
  1399. "unsplit 64 bit load");
  1400. xo = XO_MOV;
  1401. break;
  1402. }
  1403. emit_mrm(as, xo, dest, RID_MRM);
  1404. }
  1405. #define asm_fload(as, ir) asm_fxload(as, ir)
  1406. #define asm_xload(as, ir) asm_fxload(as, ir)
  1407. static void asm_fxstore(ASMState *as, IRIns *ir)
  1408. {
  1409. RegSet allow = RSET_GPR;
  1410. Reg src = RID_NONE, osrc = RID_NONE;
  1411. int32_t k = 0;
  1412. if (ir->r == RID_SINK)
  1413. return;
  1414. /* The IRT_I16/IRT_U16 stores should never be simplified for constant
  1415. ** values since mov word [mem], imm16 has a length-changing prefix.
  1416. */
  1417. if (irt_isi16(ir->t) || irt_isu16(ir->t) || irt_isfp(ir->t) ||
  1418. !asm_isk32(as, ir->op2, &k)) {
  1419. RegSet allow8 = irt_isfp(ir->t) ? RSET_FPR :
  1420. (irt_isi8(ir->t) || irt_isu8(ir->t)) ? RSET_GPR8 : RSET_GPR;
  1421. src = osrc = ra_alloc1(as, ir->op2, allow8);
  1422. if (!LJ_64 && !rset_test(allow8, src)) { /* Already in wrong register. */
  1423. rset_clear(allow, osrc);
  1424. src = ra_scratch(as, allow8);
  1425. }
  1426. rset_clear(allow, src);
  1427. }
  1428. if (ir->o == IR_FSTORE) {
  1429. asm_fusefref(as, IR(ir->op1), allow);
  1430. } else {
  1431. asm_fusexref(as, ir->op1, allow);
  1432. if (LJ_32 && ir->o == IR_HIOP) as->mrm.ofs += 4;
  1433. }
  1434. if (ra_hasreg(src)) {
  1435. x86Op xo;
  1436. switch (irt_type(ir->t)) {
  1437. case IRT_I8: case IRT_U8: xo = XO_MOVtob; src |= FORCE_REX; break;
  1438. case IRT_I16: case IRT_U16: xo = XO_MOVtow; break;
  1439. case IRT_NUM: xo = XO_MOVSDto; break;
  1440. case IRT_FLOAT: xo = XO_MOVSSto; break;
  1441. #if LJ_64 && !LJ_GC64
  1442. case IRT_LIGHTUD:
  1443. /* NYI: mask 64 bit lightuserdata. */
  1444. lj_assertA(0, "store of lightuserdata");
  1445. #endif
  1446. default:
  1447. if (LJ_64 && irt_is64(ir->t))
  1448. src |= REX_64;
  1449. else
  1450. lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t),
  1451. "unsplit 64 bit store");
  1452. xo = XO_MOVto;
  1453. break;
  1454. }
  1455. emit_mrm(as, xo, src, RID_MRM);
  1456. if (!LJ_64 && src != osrc) {
  1457. ra_noweak(as, osrc);
  1458. emit_rr(as, XO_MOV, src, osrc);
  1459. }
  1460. } else {
  1461. if (irt_isi8(ir->t) || irt_isu8(ir->t)) {
  1462. emit_i8(as, k);
  1463. emit_mrm(as, XO_MOVmib, 0, RID_MRM);
  1464. } else {
  1465. lj_assertA(irt_is64(ir->t) || irt_isint(ir->t) || irt_isu32(ir->t) ||
  1466. irt_isaddr(ir->t), "bad store type");
  1467. emit_i32(as, k);
  1468. emit_mrm(as, XO_MOVmi, REX_64IR(ir, 0), RID_MRM);
  1469. }
  1470. }
  1471. }
  1472. #define asm_fstore(as, ir) asm_fxstore(as, ir)
  1473. #define asm_xstore(as, ir) asm_fxstore(as, ir)
  1474. #if LJ_64 && !LJ_GC64
  1475. static Reg asm_load_lightud64(ASMState *as, IRIns *ir, int typecheck)
  1476. {
  1477. if (ra_used(ir) || typecheck) {
  1478. Reg dest = ra_dest(as, ir, RSET_GPR);
  1479. if (typecheck) {
  1480. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, dest));
  1481. asm_guardcc(as, CC_NE);
  1482. emit_i8(as, -2);
  1483. emit_rr(as, XO_ARITHi8, XOg_CMP, tmp);
  1484. emit_shifti(as, XOg_SAR|REX_64, tmp, 47);
  1485. emit_rr(as, XO_MOV, tmp|REX_64, dest);
  1486. }
  1487. return dest;
  1488. } else {
  1489. return RID_NONE;
  1490. }
  1491. }
  1492. #endif
  1493. static void asm_ahuvload(ASMState *as, IRIns *ir)
  1494. {
  1495. #if LJ_GC64
  1496. Reg tmp = RID_NONE;
  1497. #endif
  1498. lj_assertA(irt_isnum(ir->t) || irt_ispri(ir->t) || irt_isaddr(ir->t) ||
  1499. (LJ_DUALNUM && irt_isint(ir->t)),
  1500. "bad load type %d", irt_type(ir->t));
  1501. #if LJ_64 && !LJ_GC64
  1502. if (irt_islightud(ir->t)) {
  1503. Reg dest = asm_load_lightud64(as, ir, 1);
  1504. if (ra_hasreg(dest)) {
  1505. checkmclim(as);
  1506. asm_fuseahuref(as, ir->op1, RSET_GPR);
  1507. if (ir->o == IR_VLOAD) as->mrm.ofs += 8 * ir->op2;
  1508. emit_mrm(as, XO_MOV, dest|REX_64, RID_MRM);
  1509. }
  1510. return;
  1511. } else
  1512. #endif
  1513. if (ra_used(ir)) {
  1514. RegSet allow = irt_isnum(ir->t) ? RSET_FPR : RSET_GPR;
  1515. Reg dest = ra_dest(as, ir, allow);
  1516. asm_fuseahuref(as, ir->op1, RSET_GPR);
  1517. if (ir->o == IR_VLOAD) as->mrm.ofs += 8 * ir->op2;
  1518. #if LJ_GC64
  1519. if (irt_isaddr(ir->t)) {
  1520. emit_shifti(as, XOg_SHR|REX_64, dest, 17);
  1521. asm_guardcc(as, CC_NE);
  1522. emit_i8(as, irt_toitype(ir->t));
  1523. emit_rr(as, XO_ARITHi8, XOg_CMP, dest);
  1524. emit_i8(as, XI_O16);
  1525. if ((as->flags & JIT_F_BMI2)) {
  1526. emit_i8(as, 47);
  1527. emit_mrm(as, XV_RORX|VEX_64, dest, RID_MRM);
  1528. } else {
  1529. emit_shifti(as, XOg_ROR|REX_64, dest, 47);
  1530. emit_mrm(as, XO_MOV, dest|REX_64, RID_MRM);
  1531. }
  1532. return;
  1533. } else
  1534. #endif
  1535. emit_mrm(as, dest < RID_MAX_GPR ? XO_MOV : XO_MOVSD, dest, RID_MRM);
  1536. } else {
  1537. RegSet gpr = RSET_GPR;
  1538. #if LJ_GC64
  1539. if (irt_isaddr(ir->t)) {
  1540. tmp = ra_scratch(as, RSET_GPR);
  1541. gpr = rset_exclude(gpr, tmp);
  1542. }
  1543. #endif
  1544. asm_fuseahuref(as, ir->op1, gpr);
  1545. if (ir->o == IR_VLOAD) as->mrm.ofs += 8 * ir->op2;
  1546. }
  1547. /* Always do the type check, even if the load result is unused. */
  1548. as->mrm.ofs += 4;
  1549. asm_guardcc(as, irt_isnum(ir->t) ? CC_AE : CC_NE);
  1550. if (LJ_64 && irt_type(ir->t) >= IRT_NUM) {
  1551. lj_assertA(irt_isinteger(ir->t) || irt_isnum(ir->t),
  1552. "bad load type %d", irt_type(ir->t));
  1553. checkmclim(as);
  1554. #if LJ_GC64
  1555. emit_u32(as, LJ_TISNUM << 15);
  1556. #else
  1557. emit_u32(as, LJ_TISNUM);
  1558. #endif
  1559. emit_mrm(as, XO_ARITHi, XOg_CMP, RID_MRM);
  1560. #if LJ_GC64
  1561. } else if (irt_isaddr(ir->t)) {
  1562. as->mrm.ofs -= 4;
  1563. emit_i8(as, irt_toitype(ir->t));
  1564. emit_mrm(as, XO_ARITHi8, XOg_CMP, tmp);
  1565. emit_shifti(as, XOg_SAR|REX_64, tmp, 47);
  1566. emit_mrm(as, XO_MOV, tmp|REX_64, RID_MRM);
  1567. } else if (irt_isnil(ir->t)) {
  1568. as->mrm.ofs -= 4;
  1569. emit_i8(as, -1);
  1570. emit_mrm(as, XO_ARITHi8, XOg_CMP|REX_64, RID_MRM);
  1571. } else {
  1572. emit_u32(as, (irt_toitype(ir->t) << 15) | 0x7fff);
  1573. emit_mrm(as, XO_ARITHi, XOg_CMP, RID_MRM);
  1574. #else
  1575. } else {
  1576. emit_i8(as, irt_toitype(ir->t));
  1577. emit_mrm(as, XO_ARITHi8, XOg_CMP, RID_MRM);
  1578. #endif
  1579. }
  1580. }
  1581. static void asm_ahustore(ASMState *as, IRIns *ir)
  1582. {
  1583. if (ir->r == RID_SINK)
  1584. return;
  1585. if (irt_isnum(ir->t)) {
  1586. Reg src = ra_alloc1(as, ir->op2, RSET_FPR);
  1587. asm_fuseahuref(as, ir->op1, RSET_GPR);
  1588. emit_mrm(as, XO_MOVSDto, src, RID_MRM);
  1589. #if LJ_64 && !LJ_GC64
  1590. } else if (irt_islightud(ir->t)) {
  1591. Reg src = ra_alloc1(as, ir->op2, RSET_GPR);
  1592. asm_fuseahuref(as, ir->op1, rset_exclude(RSET_GPR, src));
  1593. emit_mrm(as, XO_MOVto, src|REX_64, RID_MRM);
  1594. #endif
  1595. #if LJ_GC64
  1596. } else if (irref_isk(ir->op2)) {
  1597. TValue k;
  1598. lj_ir_kvalue(as->J->L, &k, IR(ir->op2));
  1599. asm_fuseahuref(as, ir->op1, RSET_GPR);
  1600. if (tvisnil(&k)) {
  1601. emit_i32(as, -1);
  1602. emit_mrm(as, XO_MOVmi, REX_64, RID_MRM);
  1603. } else {
  1604. emit_u32(as, k.u32.lo);
  1605. emit_mrm(as, XO_MOVmi, 0, RID_MRM);
  1606. as->mrm.ofs += 4;
  1607. emit_u32(as, k.u32.hi);
  1608. emit_mrm(as, XO_MOVmi, 0, RID_MRM);
  1609. }
  1610. #endif
  1611. } else {
  1612. IRIns *irr = IR(ir->op2);
  1613. RegSet allow = RSET_GPR;
  1614. Reg src = RID_NONE;
  1615. if (!irref_isk(ir->op2)) {
  1616. src = ra_alloc1(as, ir->op2, allow);
  1617. rset_clear(allow, src);
  1618. }
  1619. asm_fuseahuref(as, ir->op1, allow);
  1620. if (ra_hasreg(src)) {
  1621. #if LJ_GC64
  1622. if (!(LJ_DUALNUM && irt_isinteger(ir->t))) {
  1623. /* TODO: 64 bit store + 32 bit load-modify-store is suboptimal. */
  1624. as->mrm.ofs += 4;
  1625. emit_u32(as, irt_toitype(ir->t) << 15);
  1626. emit_mrm(as, XO_ARITHi, XOg_OR, RID_MRM);
  1627. as->mrm.ofs -= 4;
  1628. emit_mrm(as, XO_MOVto, src|REX_64, RID_MRM);
  1629. return;
  1630. }
  1631. #endif
  1632. emit_mrm(as, XO_MOVto, src, RID_MRM);
  1633. } else if (!irt_ispri(irr->t)) {
  1634. lj_assertA(irt_isaddr(ir->t) || (LJ_DUALNUM && irt_isinteger(ir->t)),
  1635. "bad store type");
  1636. emit_i32(as, irr->i);
  1637. emit_mrm(as, XO_MOVmi, 0, RID_MRM);
  1638. }
  1639. as->mrm.ofs += 4;
  1640. #if LJ_GC64
  1641. lj_assertA(LJ_DUALNUM && irt_isinteger(ir->t), "bad store type");
  1642. emit_i32(as, LJ_TNUMX << 15);
  1643. #else
  1644. emit_i32(as, (int32_t)irt_toitype(ir->t));
  1645. #endif
  1646. emit_mrm(as, XO_MOVmi, 0, RID_MRM);
  1647. }
  1648. }
  1649. static void asm_sload(ASMState *as, IRIns *ir)
  1650. {
  1651. int32_t ofs = 8*((int32_t)ir->op1-1-LJ_FR2) +
  1652. (!LJ_FR2 && (ir->op2 & IRSLOAD_FRAME) ? 4 : 0);
  1653. IRType1 t = ir->t;
  1654. Reg base;
  1655. lj_assertA(!(ir->op2 & IRSLOAD_PARENT),
  1656. "bad parent SLOAD"); /* Handled by asm_head_side(). */
  1657. lj_assertA(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK),
  1658. "inconsistent SLOAD variant");
  1659. lj_assertA(LJ_DUALNUM ||
  1660. !irt_isint(t) ||
  1661. (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME|IRSLOAD_KEYINDEX)),
  1662. "bad SLOAD type");
  1663. if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {
  1664. Reg left = ra_scratch(as, RSET_FPR);
  1665. asm_tointg(as, ir, left); /* Frees dest reg. Do this before base alloc. */
  1666. base = ra_alloc1(as, REF_BASE, RSET_GPR);
  1667. emit_rmro(as, XO_MOVSD, left, base, ofs);
  1668. t.irt = IRT_NUM; /* Continue with a regular number type check. */
  1669. #if LJ_64 && !LJ_GC64
  1670. } else if (irt_islightud(t)) {
  1671. Reg dest = asm_load_lightud64(as, ir, (ir->op2 & IRSLOAD_TYPECHECK));
  1672. if (ra_hasreg(dest)) {
  1673. base = ra_alloc1(as, REF_BASE, RSET_GPR);
  1674. emit_rmro(as, XO_MOV, dest|REX_64, base, ofs);
  1675. }
  1676. return;
  1677. #endif
  1678. } else if (ra_used(ir)) {
  1679. RegSet allow = irt_isnum(t) ? RSET_FPR : RSET_GPR;
  1680. Reg dest = ra_dest(as, ir, allow);
  1681. base = ra_alloc1(as, REF_BASE, RSET_GPR);
  1682. lj_assertA(irt_isnum(t) || irt_isint(t) || irt_isaddr(t),
  1683. "bad SLOAD type %d", irt_type(t));
  1684. if ((ir->op2 & IRSLOAD_CONVERT)) {
  1685. t.irt = irt_isint(t) ? IRT_NUM : IRT_INT; /* Check for original type. */
  1686. emit_rmro(as, irt_isint(t) ? XO_CVTSI2SD : XO_CVTTSD2SI, dest, base, ofs);
  1687. } else {
  1688. #if LJ_GC64
  1689. if (irt_isaddr(t)) {
  1690. /* LJ_GC64 type check + tag removal without BMI2 and with BMI2:
  1691. **
  1692. ** mov r64, [addr] rorx r64, [addr], 47
  1693. ** ror r64, 47
  1694. ** cmp r16, itype cmp r16, itype
  1695. ** jne ->exit jne ->exit
  1696. ** shr r64, 16 shr r64, 16
  1697. */
  1698. emit_shifti(as, XOg_SHR|REX_64, dest, 17);
  1699. if ((ir->op2 & IRSLOAD_TYPECHECK)) {
  1700. asm_guardcc(as, CC_NE);
  1701. emit_i8(as, irt_toitype(t));
  1702. emit_rr(as, XO_ARITHi8, XOg_CMP, dest);
  1703. emit_i8(as, XI_O16);
  1704. }
  1705. if ((as->flags & JIT_F_BMI2)) {
  1706. emit_i8(as, 47);
  1707. emit_rmro(as, XV_RORX|VEX_64, dest, base, ofs);
  1708. } else {
  1709. if ((ir->op2 & IRSLOAD_TYPECHECK))
  1710. emit_shifti(as, XOg_ROR|REX_64, dest, 47);
  1711. else
  1712. emit_shifti(as, XOg_SHL|REX_64, dest, 17);
  1713. emit_rmro(as, XO_MOV, dest|REX_64, base, ofs);
  1714. }
  1715. return;
  1716. } else
  1717. #endif
  1718. emit_rmro(as, irt_isnum(t) ? XO_MOVSD : XO_MOV, dest, base, ofs);
  1719. }
  1720. } else {
  1721. if (!(ir->op2 & IRSLOAD_TYPECHECK))
  1722. return; /* No type check: avoid base alloc. */
  1723. base = ra_alloc1(as, REF_BASE, RSET_GPR);
  1724. }
  1725. if ((ir->op2 & IRSLOAD_TYPECHECK)) {
  1726. /* Need type check, even if the load result is unused. */
  1727. asm_guardcc(as, irt_isnum(t) ? CC_AE : CC_NE);
  1728. if ((LJ_64 && irt_type(t) >= IRT_NUM) || (ir->op2 & IRSLOAD_KEYINDEX)) {
  1729. lj_assertA(irt_isinteger(t) || irt_isnum(t),
  1730. "bad SLOAD type %d", irt_type(t));
  1731. emit_u32(as, (ir->op2 & IRSLOAD_KEYINDEX) ? LJ_KEYINDEX :
  1732. LJ_GC64 ? (LJ_TISNUM << 15) : LJ_TISNUM);
  1733. emit_rmro(as, XO_ARITHi, XOg_CMP, base, ofs+4);
  1734. #if LJ_GC64
  1735. } else if (irt_isnil(t)) {
  1736. /* LJ_GC64 type check for nil:
  1737. **
  1738. ** cmp qword [addr], -1
  1739. ** jne ->exit
  1740. */
  1741. emit_i8(as, -1);
  1742. emit_rmro(as, XO_ARITHi8, XOg_CMP|REX_64, base, ofs);
  1743. } else if (irt_ispri(t)) {
  1744. emit_u32(as, (irt_toitype(t) << 15) | 0x7fff);
  1745. emit_rmro(as, XO_ARITHi, XOg_CMP, base, ofs+4);
  1746. } else {
  1747. /* LJ_GC64 type check only:
  1748. **
  1749. ** mov r64, [addr]
  1750. ** sar r64, 47
  1751. ** cmp r32, itype
  1752. ** jne ->exit
  1753. */
  1754. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, base));
  1755. emit_i8(as, irt_toitype(t));
  1756. emit_rr(as, XO_ARITHi8, XOg_CMP, tmp);
  1757. emit_shifti(as, XOg_SAR|REX_64, tmp, 47);
  1758. emit_rmro(as, XO_MOV, tmp|REX_64, base, ofs);
  1759. #else
  1760. } else {
  1761. emit_i8(as, irt_toitype(t));
  1762. emit_rmro(as, XO_ARITHi8, XOg_CMP, base, ofs+4);
  1763. #endif
  1764. }
  1765. }
  1766. }
  1767. /* -- Allocations --------------------------------------------------------- */
  1768. #if LJ_HASFFI
  1769. static void asm_cnew(ASMState *as, IRIns *ir)
  1770. {
  1771. CTState *cts = ctype_ctsG(J2G(as->J));
  1772. CTypeID id = (CTypeID)IR(ir->op1)->i;
  1773. CTSize sz;
  1774. CTInfo info = lj_ctype_info(cts, id, &sz);
  1775. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
  1776. IRRef args[4];
  1777. lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL),
  1778. "bad CNEW/CNEWI operands");
  1779. as->gcsteps++;
  1780. asm_setupresult(as, ir, ci); /* GCcdata * */
  1781. /* Initialize immutable cdata object. */
  1782. if (ir->o == IR_CNEWI) {
  1783. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
  1784. #if LJ_64
  1785. Reg r64 = sz == 8 ? REX_64 : 0;
  1786. if (irref_isk(ir->op2)) {
  1787. IRIns *irk = IR(ir->op2);
  1788. uint64_t k = (irk->o == IR_KINT64 ||
  1789. (LJ_GC64 && (irk->o == IR_KPTR || irk->o == IR_KKPTR))) ?
  1790. ir_k64(irk)->u64 : (uint64_t)(uint32_t)irk->i;
  1791. if (sz == 4 || checki32((int64_t)k)) {
  1792. emit_i32(as, (int32_t)k);
  1793. emit_rmro(as, XO_MOVmi, r64, RID_RET, sizeof(GCcdata));
  1794. } else {
  1795. emit_movtomro(as, RID_ECX + r64, RID_RET, sizeof(GCcdata));
  1796. emit_loadu64(as, RID_ECX, k);
  1797. }
  1798. } else {
  1799. Reg r = ra_alloc1(as, ir->op2, allow);
  1800. emit_movtomro(as, r + r64, RID_RET, sizeof(GCcdata));
  1801. }
  1802. #else
  1803. int32_t ofs = sizeof(GCcdata);
  1804. if (sz == 8) {
  1805. ofs += 4; ir++;
  1806. lj_assertA(ir->o == IR_HIOP, "missing CNEWI HIOP");
  1807. }
  1808. do {
  1809. if (irref_isk(ir->op2)) {
  1810. emit_movmroi(as, RID_RET, ofs, IR(ir->op2)->i);
  1811. } else {
  1812. Reg r = ra_alloc1(as, ir->op2, allow);
  1813. emit_movtomro(as, r, RID_RET, ofs);
  1814. rset_clear(allow, r);
  1815. }
  1816. if (ofs == sizeof(GCcdata)) break;
  1817. ofs -= 4; ir--;
  1818. } while (1);
  1819. #endif
  1820. lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz);
  1821. } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */
  1822. ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];
  1823. args[0] = ASMREF_L; /* lua_State *L */
  1824. args[1] = ir->op1; /* CTypeID id */
  1825. args[2] = ir->op2; /* CTSize sz */
  1826. args[3] = ASMREF_TMP1; /* CTSize align */
  1827. asm_gencall(as, ci, args);
  1828. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));
  1829. return;
  1830. }
  1831. /* Combine initialization of marked, gct and ctypeid. */
  1832. emit_movtomro(as, RID_ECX, RID_RET, offsetof(GCcdata, marked));
  1833. emit_gri(as, XG_ARITHi(XOg_OR), RID_ECX,
  1834. (int32_t)((~LJ_TCDATA<<8)+(id<<16)));
  1835. emit_gri(as, XG_ARITHi(XOg_AND), RID_ECX, LJ_GC_WHITES);
  1836. emit_opgl(as, XO_MOVZXb, RID_ECX, gc.currentwhite);
  1837. args[0] = ASMREF_L; /* lua_State *L */
  1838. args[1] = ASMREF_TMP1; /* MSize size */
  1839. asm_gencall(as, ci, args);
  1840. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)(sz+sizeof(GCcdata)));
  1841. }
  1842. #endif
  1843. /* -- Write barriers ------------------------------------------------------ */
  1844. static void asm_tbar(ASMState *as, IRIns *ir)
  1845. {
  1846. Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
  1847. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, tab));
  1848. MCLabel l_end = emit_label(as);
  1849. emit_movtomro(as, tmp|REX_GC64, tab, offsetof(GCtab, gclist));
  1850. emit_setgl(as, tab, gc.grayagain);
  1851. emit_getgl(as, tmp, gc.grayagain);
  1852. emit_i8(as, ~LJ_GC_BLACK);
  1853. emit_rmro(as, XO_ARITHib, XOg_AND, tab, offsetof(GCtab, marked));
  1854. emit_sjcc(as, CC_Z, l_end);
  1855. emit_i8(as, LJ_GC_BLACK);
  1856. emit_rmro(as, XO_GROUP3b, XOg_TEST, tab, offsetof(GCtab, marked));
  1857. }
  1858. static void asm_obar(ASMState *as, IRIns *ir)
  1859. {
  1860. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
  1861. IRRef args[2];
  1862. MCLabel l_end;
  1863. Reg obj;
  1864. /* No need for other object barriers (yet). */
  1865. lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type");
  1866. ra_evictset(as, RSET_SCRATCH);
  1867. l_end = emit_label(as);
  1868. args[0] = ASMREF_TMP1; /* global_State *g */
  1869. args[1] = ir->op1; /* TValue *tv */
  1870. asm_gencall(as, ci, args);
  1871. emit_loada(as, ra_releasetmp(as, ASMREF_TMP1), J2G(as->J));
  1872. obj = IR(ir->op1)->r;
  1873. emit_sjcc(as, CC_Z, l_end);
  1874. emit_i8(as, LJ_GC_WHITES);
  1875. if (irref_isk(ir->op2)) {
  1876. GCobj *vp = ir_kgc(IR(ir->op2));
  1877. emit_rma(as, XO_GROUP3b, XOg_TEST, &vp->gch.marked);
  1878. } else {
  1879. Reg val = ra_alloc1(as, ir->op2, rset_exclude(RSET_SCRATCH&RSET_GPR, obj));
  1880. emit_rmro(as, XO_GROUP3b, XOg_TEST, val, (int32_t)offsetof(GChead, marked));
  1881. }
  1882. emit_sjcc(as, CC_Z, l_end);
  1883. emit_i8(as, LJ_GC_BLACK);
  1884. emit_rmro(as, XO_GROUP3b, XOg_TEST, obj,
  1885. (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));
  1886. }
  1887. /* -- FP/int arithmetic and logic operations ------------------------------ */
  1888. /* Load reference onto x87 stack. Force a spill to memory if needed. */
  1889. static void asm_x87load(ASMState *as, IRRef ref)
  1890. {
  1891. IRIns *ir = IR(ref);
  1892. if (ir->o == IR_KNUM) {
  1893. cTValue *tv = ir_knum(ir);
  1894. if (tvispzero(tv)) /* Use fldz only for +0. */
  1895. emit_x87op(as, XI_FLDZ);
  1896. else if (tvispone(tv))
  1897. emit_x87op(as, XI_FLD1);
  1898. else
  1899. emit_rma(as, XO_FLDq, XOg_FLDq, tv);
  1900. } else if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT && !ra_used(ir) &&
  1901. !irref_isk(ir->op1) && mayfuse(as, ir->op1)) {
  1902. IRIns *iri = IR(ir->op1);
  1903. emit_rmro(as, XO_FILDd, XOg_FILDd, RID_ESP, ra_spill(as, iri));
  1904. } else {
  1905. emit_mrm(as, XO_FLDq, XOg_FLDq, asm_fuseload(as, ref, RSET_EMPTY));
  1906. }
  1907. }
  1908. static void asm_fpmath(ASMState *as, IRIns *ir)
  1909. {
  1910. IRFPMathOp fpm = (IRFPMathOp)ir->op2;
  1911. if (fpm == IRFPM_SQRT) {
  1912. Reg dest = ra_dest(as, ir, RSET_FPR);
  1913. Reg left = asm_fuseload(as, ir->op1, RSET_FPR);
  1914. emit_mrm(as, XO_SQRTSD, dest, left);
  1915. } else if (fpm <= IRFPM_TRUNC) {
  1916. if (as->flags & JIT_F_SSE4_1) { /* SSE4.1 has a rounding instruction. */
  1917. Reg dest = ra_dest(as, ir, RSET_FPR);
  1918. Reg left = asm_fuseload(as, ir->op1, RSET_FPR);
  1919. /* ROUNDSD has a 4-byte opcode which doesn't fit in x86Op.
  1920. ** Let's pretend it's a 3-byte opcode, and compensate afterwards.
  1921. ** This is atrocious, but the alternatives are much worse.
  1922. */
  1923. /* Round down/up/trunc == 1001/1010/1011. */
  1924. emit_i8(as, 0x09 + fpm);
  1925. emit_mrm(as, XO_ROUNDSD, dest, left);
  1926. if (LJ_64 && as->mcp[1] != (MCode)(XO_ROUNDSD >> 16)) {
  1927. as->mcp[0] = as->mcp[1]; as->mcp[1] = 0x0f; /* Swap 0F and REX. */
  1928. }
  1929. *--as->mcp = 0x66; /* 1st byte of ROUNDSD opcode. */
  1930. } else { /* Call helper functions for SSE2 variant. */
  1931. /* The modified regs must match with the *.dasc implementation. */
  1932. RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX);
  1933. if (ra_hasreg(ir->r))
  1934. rset_clear(drop, ir->r); /* Dest reg handled below. */
  1935. ra_evictset(as, drop);
  1936. ra_destreg(as, ir, RID_XMM0);
  1937. emit_call(as, fpm == IRFPM_FLOOR ? lj_vm_floor_sse :
  1938. fpm == IRFPM_CEIL ? lj_vm_ceil_sse : lj_vm_trunc_sse);
  1939. ra_left(as, RID_XMM0, ir->op1);
  1940. }
  1941. } else {
  1942. asm_callid(as, ir, IRCALL_lj_vm_floor + fpm);
  1943. }
  1944. }
  1945. static void asm_ldexp(ASMState *as, IRIns *ir)
  1946. {
  1947. int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */
  1948. Reg dest = ir->r;
  1949. if (ra_hasreg(dest)) {
  1950. ra_free(as, dest);
  1951. ra_modified(as, dest);
  1952. emit_rmro(as, XO_MOVSD, dest, RID_ESP, ofs);
  1953. }
  1954. emit_rmro(as, XO_FSTPq, XOg_FSTPq, RID_ESP, ofs);
  1955. emit_x87op(as, XI_FPOP1);
  1956. emit_x87op(as, XI_FSCALE);
  1957. asm_x87load(as, ir->op1);
  1958. asm_x87load(as, ir->op2);
  1959. }
  1960. static int asm_swapops(ASMState *as, IRIns *ir)
  1961. {
  1962. IRIns *irl = IR(ir->op1);
  1963. IRIns *irr = IR(ir->op2);
  1964. lj_assertA(ra_noreg(irr->r), "bad usage");
  1965. if (!irm_iscomm(lj_ir_mode[ir->o]))
  1966. return 0; /* Can't swap non-commutative operations. */
  1967. if (irref_isk(ir->op2))
  1968. return 0; /* Don't swap constants to the left. */
  1969. if (ra_hasreg(irl->r))
  1970. return 1; /* Swap if left already has a register. */
  1971. if (ra_samehint(ir->r, irr->r))
  1972. return 1; /* Swap if dest and right have matching hints. */
  1973. if (as->curins > as->loopref) { /* In variant part? */
  1974. if (ir->op2 < as->loopref && !irt_isphi(irr->t))
  1975. return 0; /* Keep invariants on the right. */
  1976. if (ir->op1 < as->loopref && !irt_isphi(irl->t))
  1977. return 1; /* Swap invariants to the right. */
  1978. }
  1979. if (opisfusableload(irl->o))
  1980. return 1; /* Swap fusable loads to the right. */
  1981. return 0; /* Otherwise don't swap. */
  1982. }
  1983. static void asm_fparith(ASMState *as, IRIns *ir, x86Op xo)
  1984. {
  1985. IRRef lref = ir->op1;
  1986. IRRef rref = ir->op2;
  1987. RegSet allow = RSET_FPR;
  1988. Reg dest;
  1989. Reg right = IR(rref)->r;
  1990. if (ra_hasreg(right)) {
  1991. rset_clear(allow, right);
  1992. ra_noweak(as, right);
  1993. }
  1994. dest = ra_dest(as, ir, allow);
  1995. if (lref == rref) {
  1996. right = dest;
  1997. } else if (ra_noreg(right)) {
  1998. if (asm_swapops(as, ir)) {
  1999. IRRef tmp = lref; lref = rref; rref = tmp;
  2000. }
  2001. right = asm_fuseload(as, rref, rset_clear(allow, dest));
  2002. }
  2003. emit_mrm(as, xo, dest, right);
  2004. ra_left(as, dest, lref);
  2005. }
  2006. static void asm_intarith(ASMState *as, IRIns *ir, x86Arith xa)
  2007. {
  2008. IRRef lref = ir->op1;
  2009. IRRef rref = ir->op2;
  2010. RegSet allow = RSET_GPR;
  2011. Reg dest, right;
  2012. int32_t k = 0;
  2013. if (as->flagmcp == as->mcp) { /* Drop test r,r instruction. */
  2014. MCode *p = as->mcp + ((LJ_64 && *as->mcp < XI_TESTb) ? 3 : 2);
  2015. MCode *q = p[0] == 0x0f ? p+1 : p;
  2016. if ((*q & 15) < 14) {
  2017. if ((*q & 15) >= 12) *q -= 4; /* L <->S, NL <-> NS */
  2018. as->flagmcp = NULL;
  2019. as->mcp = p;
  2020. } /* else: cannot transform LE/NLE to cc without use of OF. */
  2021. }
  2022. right = IR(rref)->r;
  2023. if (ra_hasreg(right)) {
  2024. rset_clear(allow, right);
  2025. ra_noweak(as, right);
  2026. }
  2027. dest = ra_dest(as, ir, allow);
  2028. if (lref == rref) {
  2029. right = dest;
  2030. } else if (ra_noreg(right) && !asm_isk32(as, rref, &k)) {
  2031. if (asm_swapops(as, ir)) {
  2032. IRRef tmp = lref; lref = rref; rref = tmp;
  2033. }
  2034. right = asm_fuseloadm(as, rref, rset_clear(allow, dest), irt_is64(ir->t));
  2035. }
  2036. if (irt_isguard(ir->t)) /* For IR_ADDOV etc. */
  2037. asm_guardcc(as, CC_O);
  2038. if (xa != XOg_X_IMUL) {
  2039. if (ra_hasreg(right))
  2040. emit_mrm(as, XO_ARITH(xa), REX_64IR(ir, dest), right);
  2041. else
  2042. emit_gri(as, XG_ARITHi(xa), REX_64IR(ir, dest), k);
  2043. } else if (ra_hasreg(right)) { /* IMUL r, mrm. */
  2044. emit_mrm(as, XO_IMUL, REX_64IR(ir, dest), right);
  2045. } else { /* IMUL r, r, k. */
  2046. /* NYI: use lea/shl/add/sub (FOLD only does 2^k) depending on CPU. */
  2047. Reg left = asm_fuseloadm(as, lref, RSET_GPR, irt_is64(ir->t));
  2048. x86Op xo;
  2049. if (checki8(k)) { emit_i8(as, k); xo = XO_IMULi8;
  2050. } else { emit_i32(as, k); xo = XO_IMULi; }
  2051. emit_mrm(as, xo, REX_64IR(ir, dest), left);
  2052. return;
  2053. }
  2054. ra_left(as, dest, lref);
  2055. }
  2056. /* LEA is really a 4-operand ADD with an independent destination register,
  2057. ** up to two source registers and an immediate. One register can be scaled
  2058. ** by 1, 2, 4 or 8. This can be used to avoid moves or to fuse several
  2059. ** instructions.
  2060. **
  2061. ** Currently only a few common cases are supported:
  2062. ** - 3-operand ADD: y = a+b; y = a+k with a and b already allocated
  2063. ** - Left ADD fusion: y = (a+b)+k; y = (a+k)+b
  2064. ** - Right ADD fusion: y = a+(b+k)
  2065. ** The ommited variants have already been reduced by FOLD.
  2066. **
  2067. ** There are more fusion opportunities, like gathering shifts or joining
  2068. ** common references. But these are probably not worth the trouble, since
  2069. ** array indexing is not decomposed and already makes use of all fields
  2070. ** of the ModRM operand.
  2071. */
  2072. static int asm_lea(ASMState *as, IRIns *ir)
  2073. {
  2074. IRIns *irl = IR(ir->op1);
  2075. IRIns *irr = IR(ir->op2);
  2076. RegSet allow = RSET_GPR;
  2077. Reg dest;
  2078. as->mrm.base = as->mrm.idx = RID_NONE;
  2079. as->mrm.scale = XM_SCALE1;
  2080. as->mrm.ofs = 0;
  2081. if (ra_hasreg(irl->r)) {
  2082. rset_clear(allow, irl->r);
  2083. ra_noweak(as, irl->r);
  2084. as->mrm.base = irl->r;
  2085. if (irref_isk(ir->op2) || ra_hasreg(irr->r)) {
  2086. /* The PHI renaming logic does a better job in some cases. */
  2087. if (ra_hasreg(ir->r) &&
  2088. ((irt_isphi(irl->t) && as->phireg[ir->r] == ir->op1) ||
  2089. (irt_isphi(irr->t) && as->phireg[ir->r] == ir->op2)))
  2090. return 0;
  2091. if (irref_isk(ir->op2)) {
  2092. as->mrm.ofs = irr->i;
  2093. } else {
  2094. rset_clear(allow, irr->r);
  2095. ra_noweak(as, irr->r);
  2096. as->mrm.idx = irr->r;
  2097. }
  2098. } else if (irr->o == IR_ADD && mayfuse(as, ir->op2) &&
  2099. irref_isk(irr->op2)) {
  2100. Reg idx = ra_alloc1(as, irr->op1, allow);
  2101. rset_clear(allow, idx);
  2102. as->mrm.idx = (uint8_t)idx;
  2103. as->mrm.ofs = IR(irr->op2)->i;
  2104. } else {
  2105. return 0;
  2106. }
  2107. } else if (ir->op1 != ir->op2 && irl->o == IR_ADD && mayfuse(as, ir->op1) &&
  2108. (irref_isk(ir->op2) || irref_isk(irl->op2))) {
  2109. Reg idx, base = ra_alloc1(as, irl->op1, allow);
  2110. rset_clear(allow, base);
  2111. as->mrm.base = (uint8_t)base;
  2112. if (irref_isk(ir->op2)) {
  2113. as->mrm.ofs = irr->i;
  2114. idx = ra_alloc1(as, irl->op2, allow);
  2115. } else {
  2116. as->mrm.ofs = IR(irl->op2)->i;
  2117. idx = ra_alloc1(as, ir->op2, allow);
  2118. }
  2119. rset_clear(allow, idx);
  2120. as->mrm.idx = (uint8_t)idx;
  2121. } else {
  2122. return 0;
  2123. }
  2124. dest = ra_dest(as, ir, allow);
  2125. emit_mrm(as, XO_LEA, dest, RID_MRM);
  2126. return 1; /* Success. */
  2127. }
  2128. static void asm_add(ASMState *as, IRIns *ir)
  2129. {
  2130. if (irt_isnum(ir->t))
  2131. asm_fparith(as, ir, XO_ADDSD);
  2132. else if (as->flagmcp == as->mcp || irt_is64(ir->t) || !asm_lea(as, ir))
  2133. asm_intarith(as, ir, XOg_ADD);
  2134. }
  2135. static void asm_sub(ASMState *as, IRIns *ir)
  2136. {
  2137. if (irt_isnum(ir->t))
  2138. asm_fparith(as, ir, XO_SUBSD);
  2139. else /* Note: no need for LEA trick here. i-k is encoded as i+(-k). */
  2140. asm_intarith(as, ir, XOg_SUB);
  2141. }
  2142. static void asm_mul(ASMState *as, IRIns *ir)
  2143. {
  2144. if (irt_isnum(ir->t))
  2145. asm_fparith(as, ir, XO_MULSD);
  2146. else
  2147. asm_intarith(as, ir, XOg_X_IMUL);
  2148. }
  2149. #define asm_fpdiv(as, ir) asm_fparith(as, ir, XO_DIVSD)
  2150. static void asm_neg_not(ASMState *as, IRIns *ir, x86Group3 xg)
  2151. {
  2152. Reg dest = ra_dest(as, ir, RSET_GPR);
  2153. emit_rr(as, XO_GROUP3, REX_64IR(ir, xg), dest);
  2154. ra_left(as, dest, ir->op1);
  2155. }
  2156. static void asm_neg(ASMState *as, IRIns *ir)
  2157. {
  2158. if (irt_isnum(ir->t))
  2159. asm_fparith(as, ir, XO_XORPS);
  2160. else
  2161. asm_neg_not(as, ir, XOg_NEG);
  2162. }
  2163. #define asm_abs(as, ir) asm_fparith(as, ir, XO_ANDPS)
  2164. static void asm_intmin_max(ASMState *as, IRIns *ir, int cc)
  2165. {
  2166. Reg right, dest = ra_dest(as, ir, RSET_GPR);
  2167. IRRef lref = ir->op1, rref = ir->op2;
  2168. if (irref_isk(rref)) { lref = rref; rref = ir->op1; }
  2169. right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, dest));
  2170. emit_rr(as, XO_CMOV + (cc<<24), REX_64IR(ir, dest), right);
  2171. emit_rr(as, XO_CMP, REX_64IR(ir, dest), right);
  2172. ra_left(as, dest, lref);
  2173. }
  2174. static void asm_min(ASMState *as, IRIns *ir)
  2175. {
  2176. if (irt_isnum(ir->t))
  2177. asm_fparith(as, ir, XO_MINSD);
  2178. else
  2179. asm_intmin_max(as, ir, CC_G);
  2180. }
  2181. static void asm_max(ASMState *as, IRIns *ir)
  2182. {
  2183. if (irt_isnum(ir->t))
  2184. asm_fparith(as, ir, XO_MAXSD);
  2185. else
  2186. asm_intmin_max(as, ir, CC_L);
  2187. }
  2188. /* Note: don't use LEA for overflow-checking arithmetic! */
  2189. #define asm_addov(as, ir) asm_intarith(as, ir, XOg_ADD)
  2190. #define asm_subov(as, ir) asm_intarith(as, ir, XOg_SUB)
  2191. #define asm_mulov(as, ir) asm_intarith(as, ir, XOg_X_IMUL)
  2192. #define asm_bnot(as, ir) asm_neg_not(as, ir, XOg_NOT)
  2193. static void asm_bswap(ASMState *as, IRIns *ir)
  2194. {
  2195. Reg dest = ra_dest(as, ir, RSET_GPR);
  2196. as->mcp = emit_op(XO_BSWAP + ((dest&7) << 24),
  2197. REX_64IR(ir, 0), dest, 0, as->mcp, 1);
  2198. ra_left(as, dest, ir->op1);
  2199. }
  2200. #define asm_band(as, ir) asm_intarith(as, ir, XOg_AND)
  2201. #define asm_bor(as, ir) asm_intarith(as, ir, XOg_OR)
  2202. #define asm_bxor(as, ir) asm_intarith(as, ir, XOg_XOR)
  2203. static void asm_bitshift(ASMState *as, IRIns *ir, x86Shift xs, x86Op xv)
  2204. {
  2205. IRRef rref = ir->op2;
  2206. IRIns *irr = IR(rref);
  2207. Reg dest;
  2208. if (irref_isk(rref)) { /* Constant shifts. */
  2209. int shift;
  2210. dest = ra_dest(as, ir, RSET_GPR);
  2211. shift = irr->i & (irt_is64(ir->t) ? 63 : 31);
  2212. if (!xv && shift && (as->flags & JIT_F_BMI2)) {
  2213. Reg left = asm_fuseloadm(as, ir->op1, RSET_GPR, irt_is64(ir->t));
  2214. if (left != dest) { /* BMI2 rotate right by constant. */
  2215. emit_i8(as, xs == XOg_ROL ? -shift : shift);
  2216. emit_mrm(as, VEX_64IR(ir, XV_RORX), dest, left);
  2217. return;
  2218. }
  2219. }
  2220. switch (shift) {
  2221. case 0: break;
  2222. case 1: emit_rr(as, XO_SHIFT1, REX_64IR(ir, xs), dest); break;
  2223. default: emit_shifti(as, REX_64IR(ir, xs), dest, shift); break;
  2224. }
  2225. } else if ((as->flags & JIT_F_BMI2) && xv) { /* BMI2 variable shifts. */
  2226. Reg left, right;
  2227. dest = ra_dest(as, ir, RSET_GPR);
  2228. right = ra_alloc1(as, rref, RSET_GPR);
  2229. left = asm_fuseloadm(as, ir->op1, rset_exclude(RSET_GPR, right),
  2230. irt_is64(ir->t));
  2231. emit_mrm(as, VEX_64IR(ir, xv) ^ (right << 19), dest, left);
  2232. return;
  2233. } else { /* Variable shifts implicitly use register cl (i.e. ecx). */
  2234. Reg right;
  2235. dest = ra_dest(as, ir, rset_exclude(RSET_GPR, RID_ECX));
  2236. if (dest == RID_ECX) {
  2237. dest = ra_scratch(as, rset_exclude(RSET_GPR, RID_ECX));
  2238. emit_rr(as, XO_MOV, REX_64IR(ir, RID_ECX), dest);
  2239. }
  2240. right = irr->r;
  2241. if (ra_noreg(right))
  2242. right = ra_allocref(as, rref, RID2RSET(RID_ECX));
  2243. else if (right != RID_ECX)
  2244. ra_scratch(as, RID2RSET(RID_ECX));
  2245. emit_rr(as, XO_SHIFTcl, REX_64IR(ir, xs), dest);
  2246. ra_noweak(as, right);
  2247. if (right != RID_ECX)
  2248. emit_rr(as, XO_MOV, RID_ECX, right);
  2249. }
  2250. ra_left(as, dest, ir->op1);
  2251. /*
  2252. ** Note: avoid using the flags resulting from a shift or rotate!
  2253. ** All of them cause a partial flag stall, except for r,1 shifts
  2254. ** (but not rotates). And a shift count of 0 leaves the flags unmodified.
  2255. */
  2256. }
  2257. #define asm_bshl(as, ir) asm_bitshift(as, ir, XOg_SHL, XV_SHLX)
  2258. #define asm_bshr(as, ir) asm_bitshift(as, ir, XOg_SHR, XV_SHRX)
  2259. #define asm_bsar(as, ir) asm_bitshift(as, ir, XOg_SAR, XV_SARX)
  2260. #define asm_brol(as, ir) asm_bitshift(as, ir, XOg_ROL, 0)
  2261. #define asm_bror(as, ir) asm_bitshift(as, ir, XOg_ROR, 0)
  2262. /* -- Comparisons --------------------------------------------------------- */
  2263. /* Virtual flags for unordered FP comparisons. */
  2264. #define VCC_U 0x1000 /* Unordered. */
  2265. #define VCC_P 0x2000 /* Needs extra CC_P branch. */
  2266. #define VCC_S 0x4000 /* Swap avoids CC_P branch. */
  2267. #define VCC_PS (VCC_P|VCC_S)
  2268. /* Map of comparisons to flags. ORDER IR. */
  2269. #define COMPFLAGS(ci, cin, cu, cf) ((ci)+((cu)<<4)+((cin)<<8)+(cf))
  2270. static const uint16_t asm_compmap[IR_ABC+1] = {
  2271. /* signed non-eq unsigned flags */
  2272. /* LT */ COMPFLAGS(CC_GE, CC_G, CC_AE, VCC_PS),
  2273. /* GE */ COMPFLAGS(CC_L, CC_L, CC_B, 0),
  2274. /* LE */ COMPFLAGS(CC_G, CC_G, CC_A, VCC_PS),
  2275. /* GT */ COMPFLAGS(CC_LE, CC_L, CC_BE, 0),
  2276. /* ULT */ COMPFLAGS(CC_AE, CC_A, CC_AE, VCC_U),
  2277. /* UGE */ COMPFLAGS(CC_B, CC_B, CC_B, VCC_U|VCC_PS),
  2278. /* ULE */ COMPFLAGS(CC_A, CC_A, CC_A, VCC_U),
  2279. /* UGT */ COMPFLAGS(CC_BE, CC_B, CC_BE, VCC_U|VCC_PS),
  2280. /* EQ */ COMPFLAGS(CC_NE, CC_NE, CC_NE, VCC_P),
  2281. /* NE */ COMPFLAGS(CC_E, CC_E, CC_E, VCC_U|VCC_P),
  2282. /* ABC */ COMPFLAGS(CC_BE, CC_B, CC_BE, VCC_U|VCC_PS) /* Same as UGT. */
  2283. };
  2284. /* FP and integer comparisons. */
  2285. static void asm_comp(ASMState *as, IRIns *ir)
  2286. {
  2287. uint32_t cc = asm_compmap[ir->o];
  2288. if (irt_isnum(ir->t)) {
  2289. IRRef lref = ir->op1;
  2290. IRRef rref = ir->op2;
  2291. Reg left, right;
  2292. MCLabel l_around;
  2293. /*
  2294. ** An extra CC_P branch is required to preserve ordered/unordered
  2295. ** semantics for FP comparisons. This can be avoided by swapping
  2296. ** the operands and inverting the condition (except for EQ and UNE).
  2297. ** So always try to swap if possible.
  2298. **
  2299. ** Another option would be to swap operands to achieve better memory
  2300. ** operand fusion. But it's unlikely that this outweighs the cost
  2301. ** of the extra branches.
  2302. */
  2303. if (cc & VCC_S) { /* Swap? */
  2304. IRRef tmp = lref; lref = rref; rref = tmp;
  2305. cc ^= (VCC_PS|(5<<4)); /* A <-> B, AE <-> BE, PS <-> none */
  2306. }
  2307. left = ra_alloc1(as, lref, RSET_FPR);
  2308. l_around = emit_label(as);
  2309. asm_guardcc(as, cc >> 4);
  2310. if (cc & VCC_P) { /* Extra CC_P branch required? */
  2311. if (!(cc & VCC_U)) {
  2312. asm_guardcc(as, CC_P); /* Branch to exit for ordered comparisons. */
  2313. } else if (l_around != as->invmcp) {
  2314. emit_sjcc(as, CC_P, l_around); /* Branch around for unordered. */
  2315. } else {
  2316. /* Patched to mcloop by asm_loop_fixup. */
  2317. as->loopinv = 2;
  2318. if (as->realign)
  2319. emit_sjcc(as, CC_P, as->mcp);
  2320. else
  2321. emit_jcc(as, CC_P, as->mcp);
  2322. }
  2323. }
  2324. right = asm_fuseload(as, rref, rset_exclude(RSET_FPR, left));
  2325. emit_mrm(as, XO_UCOMISD, left, right);
  2326. } else {
  2327. IRRef lref = ir->op1, rref = ir->op2;
  2328. IROp leftop = (IROp)(IR(lref)->o);
  2329. Reg r64 = REX_64IR(ir, 0);
  2330. int32_t imm = 0;
  2331. lj_assertA(irt_is64(ir->t) || irt_isint(ir->t) ||
  2332. irt_isu32(ir->t) || irt_isaddr(ir->t) || irt_isu8(ir->t),
  2333. "bad comparison data type %d", irt_type(ir->t));
  2334. /* Swap constants (only for ABC) and fusable loads to the right. */
  2335. if (irref_isk(lref) || (!irref_isk(rref) && opisfusableload(leftop))) {
  2336. if ((cc & 0xc) == 0xc) cc ^= 0x53; /* L <-> G, LE <-> GE */
  2337. else if ((cc & 0xa) == 0x2) cc ^= 0x55; /* A <-> B, AE <-> BE */
  2338. lref = ir->op2; rref = ir->op1;
  2339. }
  2340. if (asm_isk32(as, rref, &imm)) {
  2341. IRIns *irl = IR(lref);
  2342. /* Check wether we can use test ins. Not for unsigned, since CF=0. */
  2343. int usetest = (imm == 0 && (cc & 0xa) != 0x2);
  2344. if (usetest && irl->o == IR_BAND && irl+1 == ir && !ra_used(irl)) {
  2345. /* Combine comp(BAND(ref, r/imm), 0) into test mrm, r/imm. */
  2346. Reg right, left = RID_NONE;
  2347. RegSet allow = RSET_GPR;
  2348. if (!asm_isk32(as, irl->op2, &imm)) {
  2349. left = ra_alloc1(as, irl->op2, allow);
  2350. rset_clear(allow, left);
  2351. } else { /* Try to Fuse IRT_I8/IRT_U8 loads, too. See below. */
  2352. IRIns *irll = IR(irl->op1);
  2353. if (opisfusableload((IROp)irll->o) &&
  2354. (irt_isi8(irll->t) || irt_isu8(irll->t))) {
  2355. IRType1 origt = irll->t; /* Temporarily flip types. */
  2356. irll->t.irt = (irll->t.irt & ~IRT_TYPE) | IRT_INT;
  2357. as->curins--; /* Skip to BAND to avoid failing in noconflict(). */
  2358. right = asm_fuseload(as, irl->op1, RSET_GPR);
  2359. as->curins++;
  2360. irll->t = origt;
  2361. if (right != RID_MRM) goto test_nofuse;
  2362. /* Fusion succeeded, emit test byte mrm, imm8. */
  2363. asm_guardcc(as, cc);
  2364. emit_i8(as, (imm & 0xff));
  2365. emit_mrm(as, XO_GROUP3b, XOg_TEST, RID_MRM);
  2366. return;
  2367. }
  2368. }
  2369. as->curins--; /* Skip to BAND to avoid failing in noconflict(). */
  2370. right = asm_fuseloadm(as, irl->op1, allow, r64);
  2371. as->curins++; /* Undo the above. */
  2372. test_nofuse:
  2373. asm_guardcc(as, cc);
  2374. if (ra_noreg(left)) {
  2375. emit_i32(as, imm);
  2376. emit_mrm(as, XO_GROUP3, r64 + XOg_TEST, right);
  2377. } else {
  2378. emit_mrm(as, XO_TEST, r64 + left, right);
  2379. }
  2380. } else {
  2381. Reg left;
  2382. if (opisfusableload((IROp)irl->o) &&
  2383. ((irt_isu8(irl->t) && checku8(imm)) ||
  2384. ((irt_isi8(irl->t) || irt_isi16(irl->t)) && checki8(imm)) ||
  2385. (irt_isu16(irl->t) && checku16(imm) && checki8((int16_t)imm)))) {
  2386. /* Only the IRT_INT case is fused by asm_fuseload.
  2387. ** The IRT_I8/IRT_U8 loads and some IRT_I16/IRT_U16 loads
  2388. ** are handled here.
  2389. ** Note that cmp word [mem], imm16 should not be generated,
  2390. ** since it has a length-changing prefix. Compares of a word
  2391. ** against a sign-extended imm8 are ok, however.
  2392. */
  2393. IRType1 origt = irl->t; /* Temporarily flip types. */
  2394. irl->t.irt = (irl->t.irt & ~IRT_TYPE) | IRT_INT;
  2395. left = asm_fuseload(as, lref, RSET_GPR);
  2396. irl->t = origt;
  2397. if (left == RID_MRM) { /* Fusion succeeded? */
  2398. if (irt_isu8(irl->t) || irt_isu16(irl->t))
  2399. cc >>= 4; /* Need unsigned compare. */
  2400. asm_guardcc(as, cc);
  2401. emit_i8(as, imm);
  2402. emit_mrm(as, (irt_isi8(origt) || irt_isu8(origt)) ?
  2403. XO_ARITHib : XO_ARITHiw8, r64 + XOg_CMP, RID_MRM);
  2404. return;
  2405. } /* Otherwise handle register case as usual. */
  2406. } else {
  2407. left = asm_fuseloadm(as, lref,
  2408. irt_isu8(ir->t) ? RSET_GPR8 : RSET_GPR, r64);
  2409. }
  2410. asm_guardcc(as, cc);
  2411. if (usetest && left != RID_MRM) {
  2412. /* Use test r,r instead of cmp r,0. */
  2413. x86Op xo = XO_TEST;
  2414. if (irt_isu8(ir->t)) {
  2415. lj_assertA(ir->o == IR_EQ || ir->o == IR_NE, "bad usage");
  2416. xo = XO_TESTb;
  2417. if (!rset_test(RSET_RANGE(RID_EAX, RID_EBX+1), left)) {
  2418. if (LJ_64) {
  2419. left |= FORCE_REX;
  2420. } else {
  2421. emit_i32(as, 0xff);
  2422. emit_mrm(as, XO_GROUP3, XOg_TEST, left);
  2423. return;
  2424. }
  2425. }
  2426. }
  2427. emit_rr(as, xo, r64 + left, left);
  2428. if (irl+1 == ir) /* Referencing previous ins? */
  2429. as->flagmcp = as->mcp; /* Set flag to drop test r,r if possible. */
  2430. } else {
  2431. emit_gmrmi(as, XG_ARITHi(XOg_CMP), r64 + left, imm);
  2432. }
  2433. }
  2434. } else {
  2435. Reg left = ra_alloc1(as, lref, RSET_GPR);
  2436. Reg right = asm_fuseloadm(as, rref, rset_exclude(RSET_GPR, left), r64);
  2437. asm_guardcc(as, cc);
  2438. emit_mrm(as, XO_CMP, r64 + left, right);
  2439. }
  2440. }
  2441. }
  2442. #define asm_equal(as, ir) asm_comp(as, ir)
  2443. #if LJ_32 && LJ_HASFFI
  2444. /* 64 bit integer comparisons in 32 bit mode. */
  2445. static void asm_comp_int64(ASMState *as, IRIns *ir)
  2446. {
  2447. uint32_t cc = asm_compmap[(ir-1)->o];
  2448. RegSet allow = RSET_GPR;
  2449. Reg lefthi = RID_NONE, leftlo = RID_NONE;
  2450. Reg righthi = RID_NONE, rightlo = RID_NONE;
  2451. MCLabel l_around;
  2452. x86ModRM mrm;
  2453. as->curins--; /* Skip loword ins. Avoids failing in noconflict(), too. */
  2454. /* Allocate/fuse hiword operands. */
  2455. if (irref_isk(ir->op2)) {
  2456. lefthi = asm_fuseload(as, ir->op1, allow);
  2457. } else {
  2458. lefthi = ra_alloc1(as, ir->op1, allow);
  2459. rset_clear(allow, lefthi);
  2460. righthi = asm_fuseload(as, ir->op2, allow);
  2461. if (righthi == RID_MRM) {
  2462. if (as->mrm.base != RID_NONE) rset_clear(allow, as->mrm.base);
  2463. if (as->mrm.idx != RID_NONE) rset_clear(allow, as->mrm.idx);
  2464. } else {
  2465. rset_clear(allow, righthi);
  2466. }
  2467. }
  2468. mrm = as->mrm; /* Save state for hiword instruction. */
  2469. /* Allocate/fuse loword operands. */
  2470. if (irref_isk((ir-1)->op2)) {
  2471. leftlo = asm_fuseload(as, (ir-1)->op1, allow);
  2472. } else {
  2473. leftlo = ra_alloc1(as, (ir-1)->op1, allow);
  2474. rset_clear(allow, leftlo);
  2475. rightlo = asm_fuseload(as, (ir-1)->op2, allow);
  2476. }
  2477. /* All register allocations must be performed _before_ this point. */
  2478. l_around = emit_label(as);
  2479. as->invmcp = as->flagmcp = NULL; /* Cannot use these optimizations. */
  2480. /* Loword comparison and branch. */
  2481. asm_guardcc(as, cc >> 4); /* Always use unsigned compare for loword. */
  2482. if (ra_noreg(rightlo)) {
  2483. int32_t imm = IR((ir-1)->op2)->i;
  2484. if (imm == 0 && ((cc >> 4) & 0xa) != 0x2 && leftlo != RID_MRM)
  2485. emit_rr(as, XO_TEST, leftlo, leftlo);
  2486. else
  2487. emit_gmrmi(as, XG_ARITHi(XOg_CMP), leftlo, imm);
  2488. } else {
  2489. emit_mrm(as, XO_CMP, leftlo, rightlo);
  2490. }
  2491. /* Hiword comparison and branches. */
  2492. if ((cc & 15) != CC_NE)
  2493. emit_sjcc(as, CC_NE, l_around); /* Hiword unequal: skip loword compare. */
  2494. if ((cc & 15) != CC_E)
  2495. asm_guardcc(as, cc >> 8); /* Hiword compare without equality check. */
  2496. as->mrm = mrm; /* Restore state. */
  2497. if (ra_noreg(righthi)) {
  2498. int32_t imm = IR(ir->op2)->i;
  2499. if (imm == 0 && (cc & 0xa) != 0x2 && lefthi != RID_MRM)
  2500. emit_rr(as, XO_TEST, lefthi, lefthi);
  2501. else
  2502. emit_gmrmi(as, XG_ARITHi(XOg_CMP), lefthi, imm);
  2503. } else {
  2504. emit_mrm(as, XO_CMP, lefthi, righthi);
  2505. }
  2506. }
  2507. #endif
  2508. /* -- Split register ops -------------------------------------------------- */
  2509. /* Hiword op of a split 32/32 or 64/64 bit op. Previous op is the loword op. */
  2510. static void asm_hiop(ASMState *as, IRIns *ir)
  2511. {
  2512. /* HIOP is marked as a store because it needs its own DCE logic. */
  2513. int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
  2514. if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
  2515. #if LJ_32 && LJ_HASFFI
  2516. if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */
  2517. as->curins--; /* Always skip the CONV. */
  2518. if (usehi || uselo)
  2519. asm_conv64(as, ir);
  2520. return;
  2521. } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */
  2522. asm_comp_int64(as, ir);
  2523. return;
  2524. } else if ((ir-1)->o == IR_XSTORE) {
  2525. if ((ir-1)->r != RID_SINK)
  2526. asm_fxstore(as, ir);
  2527. return;
  2528. }
  2529. #endif
  2530. if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
  2531. switch ((ir-1)->o) {
  2532. #if LJ_32 && LJ_HASFFI
  2533. case IR_ADD:
  2534. as->flagmcp = NULL;
  2535. as->curins--;
  2536. asm_intarith(as, ir, XOg_ADC);
  2537. asm_intarith(as, ir-1, XOg_ADD);
  2538. break;
  2539. case IR_SUB:
  2540. as->flagmcp = NULL;
  2541. as->curins--;
  2542. asm_intarith(as, ir, XOg_SBB);
  2543. asm_intarith(as, ir-1, XOg_SUB);
  2544. break;
  2545. case IR_NEG: {
  2546. Reg dest = ra_dest(as, ir, RSET_GPR);
  2547. emit_rr(as, XO_GROUP3, XOg_NEG, dest);
  2548. emit_i8(as, 0);
  2549. emit_rr(as, XO_ARITHi8, XOg_ADC, dest);
  2550. ra_left(as, dest, ir->op1);
  2551. as->curins--;
  2552. asm_neg_not(as, ir-1, XOg_NEG);
  2553. break;
  2554. }
  2555. case IR_CNEWI:
  2556. /* Nothing to do here. Handled by CNEWI itself. */
  2557. break;
  2558. #endif
  2559. case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS:
  2560. if (!uselo)
  2561. ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
  2562. break;
  2563. default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break;
  2564. }
  2565. }
  2566. /* -- Profiling ----------------------------------------------------------- */
  2567. static void asm_prof(ASMState *as, IRIns *ir)
  2568. {
  2569. UNUSED(ir);
  2570. asm_guardcc(as, CC_NE);
  2571. emit_i8(as, HOOK_PROFILE);
  2572. emit_rma(as, XO_GROUP3b, XOg_TEST, &J2G(as->J)->hookmask);
  2573. }
  2574. /* -- Stack handling ------------------------------------------------------ */
  2575. /* Check Lua stack size for overflow. Use exit handler as fallback. */
  2576. static void asm_stack_check(ASMState *as, BCReg topslot,
  2577. IRIns *irp, RegSet allow, ExitNo exitno)
  2578. {
  2579. /* Try to get an unused temp. register, otherwise spill/restore eax. */
  2580. Reg pbase = irp ? irp->r : RID_BASE;
  2581. Reg r = allow ? rset_pickbot(allow) : RID_EAX;
  2582. emit_jcc(as, CC_B, exitstub_addr(as->J, exitno));
  2583. if (allow == RSET_EMPTY) /* Restore temp. register. */
  2584. emit_rmro(as, XO_MOV, r|REX_64, RID_ESP, 0);
  2585. else
  2586. ra_modified(as, r);
  2587. emit_gri(as, XG_ARITHi(XOg_CMP), r|REX_GC64, (int32_t)(8*topslot));
  2588. if (ra_hasreg(pbase) && pbase != r)
  2589. emit_rr(as, XO_ARITH(XOg_SUB), r|REX_GC64, pbase);
  2590. else
  2591. #if LJ_GC64
  2592. emit_rmro(as, XO_ARITH(XOg_SUB), r|REX_64, RID_DISPATCH,
  2593. (int32_t)dispofs(as, &J2G(as->J)->jit_base));
  2594. #else
  2595. emit_rmro(as, XO_ARITH(XOg_SUB), r, RID_NONE,
  2596. ptr2addr(&J2G(as->J)->jit_base));
  2597. #endif
  2598. emit_rmro(as, XO_MOV, r|REX_GC64, r, offsetof(lua_State, maxstack));
  2599. emit_getgl(as, r, cur_L);
  2600. if (allow == RSET_EMPTY) /* Spill temp. register. */
  2601. emit_rmro(as, XO_MOVto, r|REX_64, RID_ESP, 0);
  2602. }
  2603. /* Restore Lua stack from on-trace state. */
  2604. static void asm_stack_restore(ASMState *as, SnapShot *snap)
  2605. {
  2606. SnapEntry *map = &as->T->snapmap[snap->mapofs];
  2607. #if !LJ_FR2 || defined(LUA_USE_ASSERT)
  2608. SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1-LJ_FR2];
  2609. #endif
  2610. MSize n, nent = snap->nent;
  2611. /* Store the value of all modified slots to the Lua stack. */
  2612. for (n = 0; n < nent; n++) {
  2613. SnapEntry sn = map[n];
  2614. BCReg s = snap_slot(sn);
  2615. int32_t ofs = 8*((int32_t)s-1-LJ_FR2);
  2616. IRRef ref = snap_ref(sn);
  2617. IRIns *ir = IR(ref);
  2618. if ((sn & SNAP_NORESTORE))
  2619. continue;
  2620. if ((sn & SNAP_KEYINDEX)) {
  2621. emit_movmroi(as, RID_BASE, ofs+4, LJ_KEYINDEX);
  2622. if (irref_isk(ref)) {
  2623. emit_movmroi(as, RID_BASE, ofs, ir->i);
  2624. } else {
  2625. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE));
  2626. emit_movtomro(as, src, RID_BASE, ofs);
  2627. }
  2628. } else if (irt_isnum(ir->t)) {
  2629. Reg src = ra_alloc1(as, ref, RSET_FPR);
  2630. emit_rmro(as, XO_MOVSDto, src, RID_BASE, ofs);
  2631. } else {
  2632. lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) ||
  2633. (LJ_DUALNUM && irt_isinteger(ir->t)),
  2634. "restore of IR type %d", irt_type(ir->t));
  2635. if (!irref_isk(ref)) {
  2636. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE));
  2637. #if LJ_GC64
  2638. if (irt_is64(ir->t)) {
  2639. /* TODO: 64 bit store + 32 bit load-modify-store is suboptimal. */
  2640. emit_u32(as, irt_toitype(ir->t) << 15);
  2641. emit_rmro(as, XO_ARITHi, XOg_OR, RID_BASE, ofs+4);
  2642. } else if (LJ_DUALNUM && irt_isinteger(ir->t)) {
  2643. emit_movmroi(as, RID_BASE, ofs+4, LJ_TISNUM << 15);
  2644. } else {
  2645. emit_movmroi(as, RID_BASE, ofs+4, (irt_toitype(ir->t)<<15)|0x7fff);
  2646. }
  2647. #endif
  2648. emit_movtomro(as, REX_64IR(ir, src), RID_BASE, ofs);
  2649. #if LJ_GC64
  2650. } else {
  2651. TValue k;
  2652. lj_ir_kvalue(as->J->L, &k, ir);
  2653. if (tvisnil(&k)) {
  2654. emit_i32(as, -1);
  2655. emit_rmro(as, XO_MOVmi, REX_64, RID_BASE, ofs);
  2656. } else {
  2657. emit_movmroi(as, RID_BASE, ofs+4, k.u32.hi);
  2658. emit_movmroi(as, RID_BASE, ofs, k.u32.lo);
  2659. }
  2660. #else
  2661. } else if (!irt_ispri(ir->t)) {
  2662. emit_movmroi(as, RID_BASE, ofs, ir->i);
  2663. #endif
  2664. }
  2665. if ((sn & (SNAP_CONT|SNAP_FRAME))) {
  2666. #if !LJ_FR2
  2667. if (s != 0) /* Do not overwrite link to previous frame. */
  2668. emit_movmroi(as, RID_BASE, ofs+4, (int32_t)(*flinks--));
  2669. #endif
  2670. #if !LJ_GC64
  2671. } else {
  2672. if (!(LJ_64 && irt_islightud(ir->t)))
  2673. emit_movmroi(as, RID_BASE, ofs+4, irt_toitype(ir->t));
  2674. #endif
  2675. }
  2676. }
  2677. checkmclim(as);
  2678. }
  2679. lj_assertA(map + nent == flinks, "inconsistent frames in snapshot");
  2680. }
  2681. /* -- GC handling --------------------------------------------------------- */
  2682. /* Check GC threshold and do one or more GC steps. */
  2683. static void asm_gc_check(ASMState *as)
  2684. {
  2685. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
  2686. IRRef args[2];
  2687. MCLabel l_end;
  2688. Reg tmp;
  2689. ra_evictset(as, RSET_SCRATCH);
  2690. l_end = emit_label(as);
  2691. /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
  2692. asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */
  2693. emit_rr(as, XO_TEST, RID_RET, RID_RET);
  2694. args[0] = ASMREF_TMP1; /* global_State *g */
  2695. args[1] = ASMREF_TMP2; /* MSize steps */
  2696. asm_gencall(as, ci, args);
  2697. tmp = ra_releasetmp(as, ASMREF_TMP1);
  2698. #if LJ_GC64
  2699. emit_rmro(as, XO_LEA, tmp|REX_64, RID_DISPATCH, GG_DISP2G);
  2700. #else
  2701. emit_loada(as, tmp, J2G(as->J));
  2702. #endif
  2703. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP2), as->gcsteps);
  2704. /* Jump around GC step if GC total < GC threshold. */
  2705. emit_sjcc(as, CC_B, l_end);
  2706. emit_opgl(as, XO_ARITH(XOg_CMP), tmp|REX_GC64, gc.threshold);
  2707. emit_getgl(as, tmp, gc.total);
  2708. as->gcsteps = 0;
  2709. checkmclim(as);
  2710. }
  2711. /* -- Loop handling ------------------------------------------------------- */
  2712. /* Fixup the loop branch. */
  2713. static void asm_loop_fixup(ASMState *as)
  2714. {
  2715. MCode *p = as->mctop;
  2716. MCode *target = as->mcp;
  2717. if (as->realign) { /* Realigned loops use short jumps. */
  2718. as->realign = NULL; /* Stop another retry. */
  2719. lj_assertA(((intptr_t)target & 15) == 0, "loop realign failed");
  2720. if (as->loopinv) { /* Inverted loop branch? */
  2721. p -= 5;
  2722. p[0] = XI_JMP;
  2723. lj_assertA(target - p >= -128, "loop realign failed");
  2724. p[-1] = (MCode)(target - p); /* Patch sjcc. */
  2725. if (as->loopinv == 2)
  2726. p[-3] = (MCode)(target - p + 2); /* Patch opt. short jp. */
  2727. } else {
  2728. lj_assertA(target - p >= -128, "loop realign failed");
  2729. p[-1] = (MCode)(int8_t)(target - p); /* Patch short jmp. */
  2730. p[-2] = XI_JMPs;
  2731. }
  2732. } else {
  2733. MCode *newloop;
  2734. p[-5] = XI_JMP;
  2735. if (as->loopinv) { /* Inverted loop branch? */
  2736. /* asm_guardcc already inverted the jcc and patched the jmp. */
  2737. p -= 5;
  2738. newloop = target+4;
  2739. *(int32_t *)(p-4) = (int32_t)(target - p); /* Patch jcc. */
  2740. if (as->loopinv == 2) {
  2741. *(int32_t *)(p-10) = (int32_t)(target - p + 6); /* Patch opt. jp. */
  2742. newloop = target+8;
  2743. }
  2744. } else { /* Otherwise just patch jmp. */
  2745. *(int32_t *)(p-4) = (int32_t)(target - p);
  2746. newloop = target+3;
  2747. }
  2748. /* Realign small loops and shorten the loop branch. */
  2749. if (newloop >= p - 128) {
  2750. as->realign = newloop; /* Force a retry and remember alignment. */
  2751. as->curins = as->stopins; /* Abort asm_trace now. */
  2752. as->T->nins = as->orignins; /* Remove any added renames. */
  2753. }
  2754. }
  2755. }
  2756. /* Fixup the tail of the loop. */
  2757. static void asm_loop_tail_fixup(ASMState *as)
  2758. {
  2759. UNUSED(as); /* Nothing to do. */
  2760. }
  2761. /* -- Head of trace ------------------------------------------------------- */
  2762. /* Coalesce BASE register for a root trace. */
  2763. static void asm_head_root_base(ASMState *as)
  2764. {
  2765. IRIns *ir = IR(REF_BASE);
  2766. Reg r = ir->r;
  2767. if (ra_hasreg(r)) {
  2768. ra_free(as, r);
  2769. if (rset_test(as->modset, r) || irt_ismarked(ir->t))
  2770. ir->r = RID_INIT; /* No inheritance for modified BASE register. */
  2771. if (r != RID_BASE)
  2772. emit_rr(as, XO_MOV, r|REX_GC64, RID_BASE);
  2773. }
  2774. }
  2775. /* Coalesce or reload BASE register for a side trace. */
  2776. static Reg asm_head_side_base(ASMState *as, IRIns *irp)
  2777. {
  2778. IRIns *ir = IR(REF_BASE);
  2779. Reg r = ir->r;
  2780. if (ra_hasreg(r)) {
  2781. ra_free(as, r);
  2782. if (rset_test(as->modset, r) || irt_ismarked(ir->t))
  2783. ir->r = RID_INIT; /* No inheritance for modified BASE register. */
  2784. if (irp->r == r) {
  2785. return r; /* Same BASE register already coalesced. */
  2786. } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {
  2787. /* Move from coalesced parent reg. */
  2788. emit_rr(as, XO_MOV, r|REX_GC64, irp->r);
  2789. return irp->r;
  2790. } else {
  2791. emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */
  2792. }
  2793. }
  2794. return RID_NONE;
  2795. }
  2796. /* -- Tail of trace ------------------------------------------------------- */
  2797. /* Fixup the tail code. */
  2798. static void asm_tail_fixup(ASMState *as, TraceNo lnk)
  2799. {
  2800. /* Note: don't use as->mcp swap + emit_*: emit_op overwrites more bytes. */
  2801. MCode *p = as->mctop;
  2802. MCode *target, *q;
  2803. int32_t spadj = as->T->spadjust;
  2804. if (spadj == 0) {
  2805. p -= LJ_64 ? 7 : 6;
  2806. } else {
  2807. MCode *p1;
  2808. /* Patch stack adjustment. */
  2809. if (checki8(spadj)) {
  2810. p -= 3;
  2811. p1 = p-6;
  2812. *p1 = (MCode)spadj;
  2813. } else {
  2814. p1 = p-9;
  2815. *(int32_t *)p1 = spadj;
  2816. }
  2817. #if LJ_64
  2818. p1[-3] = 0x48;
  2819. #endif
  2820. p1[-2] = (MCode)(checki8(spadj) ? XI_ARITHi8 : XI_ARITHi);
  2821. p1[-1] = MODRM(XM_REG, XOg_ADD, RID_ESP);
  2822. }
  2823. /* Patch exit branch. */
  2824. target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;
  2825. *(int32_t *)(p-4) = jmprel(as->J, p, target);
  2826. p[-5] = XI_JMP;
  2827. /* Drop unused mcode tail. Fill with NOPs to make the prefetcher happy. */
  2828. for (q = as->mctop-1; q >= p; q--)
  2829. *q = XI_NOP;
  2830. as->mctop = p;
  2831. }
  2832. /* Prepare tail of code. */
  2833. static void asm_tail_prep(ASMState *as)
  2834. {
  2835. MCode *p = as->mctop;
  2836. /* Realign and leave room for backwards loop branch or exit branch. */
  2837. if (as->realign) {
  2838. int i = ((int)(intptr_t)as->realign) & 15;
  2839. /* Fill unused mcode tail with NOPs to make the prefetcher happy. */
  2840. while (i-- > 0)
  2841. *--p = XI_NOP;
  2842. as->mctop = p;
  2843. p -= (as->loopinv ? 5 : 2); /* Space for short/near jmp. */
  2844. } else {
  2845. p -= 5; /* Space for exit branch (near jmp). */
  2846. }
  2847. if (as->loopref) {
  2848. as->invmcp = as->mcp = p;
  2849. } else {
  2850. /* Leave room for ESP adjustment: add esp, imm or lea esp, [esp+imm] */
  2851. as->mcp = p - (LJ_64 ? 7 : 6);
  2852. as->invmcp = NULL;
  2853. }
  2854. }
  2855. /* -- Trace setup --------------------------------------------------------- */
  2856. /* Ensure there are enough stack slots for call arguments. */
  2857. static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
  2858. {
  2859. IRRef args[CCI_NARGS_MAX*2];
  2860. int nslots;
  2861. asm_collectargs(as, ir, ci, args);
  2862. nslots = asm_count_call_slots(as, ci, args);
  2863. if (nslots > as->evenspill) /* Leave room for args in stack slots. */
  2864. as->evenspill = nslots;
  2865. #if LJ_64
  2866. return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);
  2867. #else
  2868. return irt_isfp(ir->t) ? REGSP_INIT : REGSP_HINT(RID_RET);
  2869. #endif
  2870. }
  2871. /* Target-specific setup. */
  2872. static void asm_setup_target(ASMState *as)
  2873. {
  2874. asm_exitstub_setup(as, as->T->nsnap);
  2875. as->mrm.base = 0;
  2876. }
  2877. /* -- Trace patching ------------------------------------------------------ */
  2878. static const uint8_t map_op1[256] = {
  2879. 0x92,0x92,0x92,0x92,0x52,0x45,0x51,0x51,0x92,0x92,0x92,0x92,0x52,0x45,0x51,0x20,
  2880. 0x92,0x92,0x92,0x92,0x52,0x45,0x51,0x51,0x92,0x92,0x92,0x92,0x52,0x45,0x51,0x51,
  2881. 0x92,0x92,0x92,0x92,0x52,0x45,0x10,0x51,0x92,0x92,0x92,0x92,0x52,0x45,0x10,0x51,
  2882. 0x92,0x92,0x92,0x92,0x52,0x45,0x10,0x51,0x92,0x92,0x92,0x92,0x52,0x45,0x10,0x51,
  2883. #if LJ_64
  2884. 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x14,
  2885. #else
  2886. 0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,
  2887. #endif
  2888. 0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,
  2889. 0x51,0x51,0x92,0x92,0x10,0x10,0x12,0x11,0x45,0x86,0x52,0x93,0x51,0x51,0x51,0x51,
  2890. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
  2891. 0x93,0x86,0x93,0x93,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,
  2892. 0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x47,0x51,0x51,0x51,0x51,0x51,
  2893. #if LJ_64
  2894. 0x59,0x59,0x59,0x59,0x51,0x51,0x51,0x51,0x52,0x45,0x51,0x51,0x51,0x51,0x51,0x51,
  2895. #else
  2896. 0x55,0x55,0x55,0x55,0x51,0x51,0x51,0x51,0x52,0x45,0x51,0x51,0x51,0x51,0x51,0x51,
  2897. #endif
  2898. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,
  2899. 0x93,0x93,0x53,0x51,0x70,0x71,0x93,0x86,0x54,0x51,0x53,0x51,0x51,0x52,0x51,0x51,
  2900. 0x92,0x92,0x92,0x92,0x52,0x52,0x51,0x51,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,
  2901. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x45,0x45,0x47,0x52,0x51,0x51,0x51,0x51,
  2902. 0x10,0x51,0x10,0x10,0x51,0x51,0x63,0x66,0x51,0x51,0x51,0x51,0x51,0x51,0x92,0x92
  2903. };
  2904. static const uint8_t map_op2[256] = {
  2905. 0x93,0x93,0x93,0x93,0x52,0x52,0x52,0x52,0x52,0x52,0x51,0x52,0x51,0x93,0x52,0x94,
  2906. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2907. 0x53,0x53,0x53,0x53,0x53,0x53,0x53,0x53,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2908. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x34,0x51,0x35,0x51,0x51,0x51,0x51,0x51,
  2909. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2910. 0x53,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2911. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2912. 0x94,0x54,0x54,0x54,0x93,0x93,0x93,0x52,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2913. 0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,
  2914. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2915. 0x52,0x52,0x52,0x93,0x94,0x93,0x51,0x51,0x52,0x52,0x52,0x93,0x94,0x93,0x93,0x93,
  2916. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x94,0x93,0x93,0x93,0x93,0x93,
  2917. 0x93,0x93,0x94,0x93,0x94,0x94,0x94,0x93,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
  2918. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2919. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  2920. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x52
  2921. };
  2922. static uint32_t asm_x86_inslen(const uint8_t* p)
  2923. {
  2924. uint32_t result = 0;
  2925. uint32_t prefixes = 0;
  2926. uint32_t x = map_op1[*p];
  2927. for (;;) {
  2928. switch (x >> 4) {
  2929. case 0: return result + x + (prefixes & 4);
  2930. case 1: prefixes |= x; x = map_op1[*++p]; result++; break;
  2931. case 2: x = map_op2[*++p]; break;
  2932. case 3: p++; goto mrm;
  2933. case 4: result -= (prefixes & 2); /* fallthrough */
  2934. case 5: return result + (x & 15);
  2935. case 6: /* Group 3. */
  2936. if (p[1] & 0x38) x = 2;
  2937. else if ((prefixes & 2) && (x == 0x66)) x = 4;
  2938. goto mrm;
  2939. case 7: /* VEX c4/c5. */
  2940. if (LJ_32 && p[1] < 0xc0) {
  2941. x = 2;
  2942. goto mrm;
  2943. }
  2944. if (x == 0x70) {
  2945. x = *++p & 0x1f;
  2946. result++;
  2947. if (x >= 2) {
  2948. p += 2;
  2949. result += 2;
  2950. goto mrm;
  2951. }
  2952. }
  2953. p++;
  2954. result++;
  2955. x = map_op2[*++p];
  2956. break;
  2957. case 8: result -= (prefixes & 2); /* fallthrough */
  2958. case 9: mrm: /* ModR/M and possibly SIB. */
  2959. result += (x & 15);
  2960. x = *++p;
  2961. switch (x >> 6) {
  2962. case 0: if ((x & 7) == 5) return result + 4; break;
  2963. case 1: result++; break;
  2964. case 2: result += 4; break;
  2965. case 3: return result;
  2966. }
  2967. if ((x & 7) == 4) {
  2968. result++;
  2969. if (x < 0x40 && (p[1] & 7) == 5) result += 4;
  2970. }
  2971. return result;
  2972. }
  2973. }
  2974. }
  2975. /* Patch exit jumps of existing machine code to a new target. */
  2976. void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
  2977. {
  2978. MCode *p = T->mcode;
  2979. MCode *mcarea = lj_mcode_patch(J, p, 0);
  2980. MSize len = T->szmcode;
  2981. MCode *px = exitstub_addr(J, exitno) - 6;
  2982. MCode *pe = p+len-6;
  2983. MCode *pgc = NULL;
  2984. #if LJ_GC64
  2985. uint32_t statei = (uint32_t)(GG_OFS(g.vmstate) - GG_OFS(dispatch));
  2986. #else
  2987. uint32_t statei = u32ptr(&J2G(J)->vmstate);
  2988. #endif
  2989. if (len > 5 && p[len-5] == XI_JMP && p+len-6 + *(int32_t *)(p+len-4) == px)
  2990. *(int32_t *)(p+len-4) = jmprel(J, p+len, target);
  2991. /* Do not patch parent exit for a stack check. Skip beyond vmstate update. */
  2992. for (; p < pe; p += asm_x86_inslen(p)) {
  2993. intptr_t ofs = LJ_GC64 ? (p[0] & 0xf0) == 0x40 : LJ_64;
  2994. if (*(uint32_t *)(p+2+ofs) == statei && p[ofs+LJ_GC64-LJ_64] == XI_MOVmi)
  2995. break;
  2996. }
  2997. lj_assertJ(p < pe, "instruction length decoder failed");
  2998. for (; p < pe; p += asm_x86_inslen(p)) {
  2999. if ((*(uint16_t *)p & 0xf0ff) == 0x800f && p + *(int32_t *)(p+2) == px &&
  3000. p != pgc) {
  3001. *(int32_t *)(p+2) = jmprel(J, p+6, target);
  3002. } else if (*p == XI_CALL &&
  3003. (void *)(p+5+*(int32_t *)(p+1)) == (void *)lj_gc_step_jit) {
  3004. pgc = p+7; /* Do not patch GC check exit. */
  3005. }
  3006. }
  3007. lj_mcode_sync(T->mcode, T->mcode + T->szmcode);
  3008. lj_mcode_patch(J, mcarea, 1);
  3009. }