lj_asm_arm.h 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  1. /*
  2. ** ARM IR assembler (SSA IR -> machine code).
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. /* -- Register allocator extensions --------------------------------------- */
  6. /* Allocate a register with a hint. */
  7. static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)
  8. {
  9. Reg r = IR(ref)->r;
  10. if (ra_noreg(r)) {
  11. if (!ra_hashint(r) && !iscrossref(as, ref))
  12. ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */
  13. r = ra_allocref(as, ref, allow);
  14. }
  15. ra_noweak(as, r);
  16. return r;
  17. }
  18. /* Allocate a scratch register pair. */
  19. static Reg ra_scratchpair(ASMState *as, RegSet allow)
  20. {
  21. RegSet pick1 = as->freeset & allow;
  22. RegSet pick2 = pick1 & (pick1 >> 1) & RSET_GPREVEN;
  23. Reg r;
  24. if (pick2) {
  25. r = rset_picktop(pick2);
  26. } else {
  27. RegSet pick = pick1 & (allow >> 1) & RSET_GPREVEN;
  28. if (pick) {
  29. r = rset_picktop(pick);
  30. ra_restore(as, regcost_ref(as->cost[r+1]));
  31. } else {
  32. pick = pick1 & (allow << 1) & RSET_GPRODD;
  33. if (pick) {
  34. r = ra_restore(as, regcost_ref(as->cost[rset_picktop(pick)-1]));
  35. } else {
  36. r = ra_evict(as, allow & (allow >> 1) & RSET_GPREVEN);
  37. ra_restore(as, regcost_ref(as->cost[r+1]));
  38. }
  39. }
  40. }
  41. lj_assertA(rset_test(RSET_GPREVEN, r), "odd reg %d", r);
  42. ra_modified(as, r);
  43. ra_modified(as, r+1);
  44. RA_DBGX((as, "scratchpair $r $r", r, r+1));
  45. return r;
  46. }
  47. #if !LJ_SOFTFP
  48. /* Allocate two source registers for three-operand instructions. */
  49. static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)
  50. {
  51. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
  52. Reg left = irl->r, right = irr->r;
  53. if (ra_hasreg(left)) {
  54. ra_noweak(as, left);
  55. if (ra_noreg(right))
  56. right = ra_allocref(as, ir->op2, rset_exclude(allow, left));
  57. else
  58. ra_noweak(as, right);
  59. } else if (ra_hasreg(right)) {
  60. ra_noweak(as, right);
  61. left = ra_allocref(as, ir->op1, rset_exclude(allow, right));
  62. } else if (ra_hashint(right)) {
  63. right = ra_allocref(as, ir->op2, allow);
  64. left = ra_alloc1(as, ir->op1, rset_exclude(allow, right));
  65. } else {
  66. left = ra_allocref(as, ir->op1, allow);
  67. right = ra_alloc1(as, ir->op2, rset_exclude(allow, left));
  68. }
  69. return left | (right << 8);
  70. }
  71. #endif
  72. /* -- Guard handling ------------------------------------------------------ */
  73. /* Generate an exit stub group at the bottom of the reserved MCode memory. */
  74. static MCode *asm_exitstub_gen(ASMState *as, ExitNo group)
  75. {
  76. MCode *mxp = as->mcbot;
  77. int i;
  78. if (mxp + 4*4+4*EXITSTUBS_PER_GROUP >= as->mctop)
  79. asm_mclimit(as);
  80. /* str lr, [sp]; bl ->vm_exit_handler; .long DISPATCH_address, group. */
  81. *mxp++ = ARMI_STR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_LR)|ARMF_N(RID_SP);
  82. *mxp = ARMI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)-2)&0x00ffffffu);
  83. mxp++;
  84. *mxp++ = (MCode)i32ptr(J2GG(as->J)->dispatch); /* DISPATCH address */
  85. *mxp++ = group*EXITSTUBS_PER_GROUP;
  86. for (i = 0; i < EXITSTUBS_PER_GROUP; i++)
  87. *mxp++ = ARMI_B|((-6-i)&0x00ffffffu);
  88. lj_mcode_sync(as->mcbot, mxp);
  89. lj_mcode_commitbot(as->J, mxp);
  90. as->mcbot = mxp;
  91. as->mclim = as->mcbot + MCLIM_REDZONE;
  92. return mxp - EXITSTUBS_PER_GROUP;
  93. }
  94. /* Setup all needed exit stubs. */
  95. static void asm_exitstub_setup(ASMState *as, ExitNo nexits)
  96. {
  97. ExitNo i;
  98. if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR)
  99. lj_trace_err(as->J, LJ_TRERR_SNAPOV);
  100. for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++)
  101. if (as->J->exitstubgroup[i] == NULL)
  102. as->J->exitstubgroup[i] = asm_exitstub_gen(as, i);
  103. }
  104. /* Emit conditional branch to exit for guard. */
  105. static void asm_guardcc(ASMState *as, ARMCC cc)
  106. {
  107. MCode *target = exitstub_addr(as->J, as->snapno);
  108. MCode *p = as->mcp;
  109. if (LJ_UNLIKELY(p == as->invmcp)) {
  110. as->loopinv = 1;
  111. *p = ARMI_BL | ((target-p-2) & 0x00ffffffu);
  112. emit_branch(as, ARMF_CC(ARMI_B, cc^1), p+1);
  113. return;
  114. }
  115. emit_branch(as, ARMF_CC(ARMI_BL, cc), target);
  116. }
  117. /* -- Operand fusion ------------------------------------------------------ */
  118. /* Limit linear search to this distance. Avoids O(n^2) behavior. */
  119. #define CONFLICT_SEARCH_LIM 31
  120. /* Check if there's no conflicting instruction between curins and ref. */
  121. static int noconflict(ASMState *as, IRRef ref, IROp conflict)
  122. {
  123. IRIns *ir = as->ir;
  124. IRRef i = as->curins;
  125. if (i > ref + CONFLICT_SEARCH_LIM)
  126. return 0; /* Give up, ref is too far away. */
  127. while (--i > ref)
  128. if (ir[i].o == conflict)
  129. return 0; /* Conflict found. */
  130. return 1; /* Ok, no conflict. */
  131. }
  132. /* Fuse the array base of colocated arrays. */
  133. static int32_t asm_fuseabase(ASMState *as, IRRef ref)
  134. {
  135. IRIns *ir = IR(ref);
  136. if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&
  137. !neverfuse(as) && noconflict(as, ref, IR_NEWREF))
  138. return (int32_t)sizeof(GCtab);
  139. return 0;
  140. }
  141. /* Fuse array/hash/upvalue reference into register+offset operand. */
  142. static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow,
  143. int lim)
  144. {
  145. IRIns *ir = IR(ref);
  146. if (ra_noreg(ir->r)) {
  147. if (ir->o == IR_AREF) {
  148. if (mayfuse(as, ref)) {
  149. if (irref_isk(ir->op2)) {
  150. IRRef tab = IR(ir->op1)->op1;
  151. int32_t ofs = asm_fuseabase(as, tab);
  152. IRRef refa = ofs ? tab : ir->op1;
  153. ofs += 8*IR(ir->op2)->i;
  154. if (ofs > -lim && ofs < lim) {
  155. *ofsp = ofs;
  156. return ra_alloc1(as, refa, allow);
  157. }
  158. }
  159. }
  160. } else if (ir->o == IR_HREFK) {
  161. if (mayfuse(as, ref)) {
  162. int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));
  163. if (ofs < lim) {
  164. *ofsp = ofs;
  165. return ra_alloc1(as, ir->op1, allow);
  166. }
  167. }
  168. } else if (ir->o == IR_UREFC) {
  169. if (irref_isk(ir->op1)) {
  170. GCfunc *fn = ir_kfunc(IR(ir->op1));
  171. int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv);
  172. *ofsp = (ofs & 255); /* Mask out less bits to allow LDRD. */
  173. return ra_allock(as, (ofs & ~255), allow);
  174. }
  175. } else if (ir->o == IR_TMPREF) {
  176. *ofsp = 0;
  177. return RID_SP;
  178. }
  179. }
  180. *ofsp = 0;
  181. return ra_alloc1(as, ref, allow);
  182. }
  183. /* Fuse m operand into arithmetic/logic instructions. */
  184. static uint32_t asm_fuseopm(ASMState *as, ARMIns ai, IRRef ref, RegSet allow)
  185. {
  186. IRIns *ir = IR(ref);
  187. if (ra_hasreg(ir->r)) {
  188. ra_noweak(as, ir->r);
  189. return ARMF_M(ir->r);
  190. } else if (irref_isk(ref)) {
  191. uint32_t k = emit_isk12(ai, ir->i);
  192. if (k)
  193. return k;
  194. } else if (mayfuse(as, ref)) {
  195. if (ir->o >= IR_BSHL && ir->o <= IR_BROR) {
  196. Reg m = ra_alloc1(as, ir->op1, allow);
  197. ARMShift sh = ir->o == IR_BSHL ? ARMSH_LSL :
  198. ir->o == IR_BSHR ? ARMSH_LSR :
  199. ir->o == IR_BSAR ? ARMSH_ASR : ARMSH_ROR;
  200. if (irref_isk(ir->op2)) {
  201. return m | ARMF_SH(sh, (IR(ir->op2)->i & 31));
  202. } else {
  203. Reg s = ra_alloc1(as, ir->op2, rset_exclude(allow, m));
  204. return m | ARMF_RSH(sh, s);
  205. }
  206. } else if (ir->o == IR_ADD && ir->op1 == ir->op2) {
  207. Reg m = ra_alloc1(as, ir->op1, allow);
  208. return m | ARMF_SH(ARMSH_LSL, 1);
  209. }
  210. }
  211. return ra_allocref(as, ref, allow);
  212. }
  213. /* Fuse shifts into loads/stores. Only bother with BSHL 2 => lsl #2. */
  214. static IRRef asm_fuselsl2(ASMState *as, IRRef ref)
  215. {
  216. IRIns *ir = IR(ref);
  217. if (ra_noreg(ir->r) && mayfuse(as, ref) && ir->o == IR_BSHL &&
  218. irref_isk(ir->op2) && IR(ir->op2)->i == 2)
  219. return ir->op1;
  220. return 0; /* No fusion. */
  221. }
  222. /* Fuse XLOAD/XSTORE reference into load/store operand. */
  223. static void asm_fusexref(ASMState *as, ARMIns ai, Reg rd, IRRef ref,
  224. RegSet allow, int32_t ofs)
  225. {
  226. IRIns *ir = IR(ref);
  227. Reg base;
  228. if (ra_noreg(ir->r) && canfuse(as, ir)) {
  229. int32_t lim = (!LJ_SOFTFP && (ai & 0x08000000)) ? 1024 :
  230. (ai & 0x04000000) ? 4096 : 256;
  231. if (ir->o == IR_ADD) {
  232. int32_t ofs2;
  233. if (irref_isk(ir->op2) &&
  234. (ofs2 = ofs + IR(ir->op2)->i) > -lim && ofs2 < lim &&
  235. (!(!LJ_SOFTFP && (ai & 0x08000000)) || !(ofs2 & 3))) {
  236. ofs = ofs2;
  237. ref = ir->op1;
  238. } else if (ofs == 0 && !(!LJ_SOFTFP && (ai & 0x08000000))) {
  239. IRRef lref = ir->op1, rref = ir->op2;
  240. Reg rn, rm;
  241. if ((ai & 0x04000000)) {
  242. IRRef sref = asm_fuselsl2(as, rref);
  243. if (sref) {
  244. rref = sref;
  245. ai |= ARMF_SH(ARMSH_LSL, 2);
  246. } else if ((sref = asm_fuselsl2(as, lref)) != 0) {
  247. lref = rref;
  248. rref = sref;
  249. ai |= ARMF_SH(ARMSH_LSL, 2);
  250. }
  251. }
  252. rn = ra_alloc1(as, lref, allow);
  253. rm = ra_alloc1(as, rref, rset_exclude(allow, rn));
  254. if ((ai & 0x04000000)) ai |= ARMI_LS_R;
  255. emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm);
  256. return;
  257. }
  258. } else if (ir->o == IR_STRREF && !(!LJ_SOFTFP && (ai & 0x08000000))) {
  259. lj_assertA(ofs == 0, "bad usage");
  260. ofs = (int32_t)sizeof(GCstr);
  261. if (irref_isk(ir->op2)) {
  262. ofs += IR(ir->op2)->i;
  263. ref = ir->op1;
  264. } else if (irref_isk(ir->op1)) {
  265. ofs += IR(ir->op1)->i;
  266. ref = ir->op2;
  267. } else {
  268. /* NYI: Fuse ADD with constant. */
  269. Reg rn = ra_alloc1(as, ir->op1, allow);
  270. uint32_t m = asm_fuseopm(as, 0, ir->op2, rset_exclude(allow, rn));
  271. if ((ai & 0x04000000))
  272. emit_lso(as, ai, rd, rd, ofs);
  273. else
  274. emit_lsox(as, ai, rd, rd, ofs);
  275. emit_dn(as, ARMI_ADD^m, rd, rn);
  276. return;
  277. }
  278. if (ofs <= -lim || ofs >= lim) {
  279. Reg rn = ra_alloc1(as, ref, allow);
  280. Reg rm = ra_allock(as, ofs, rset_exclude(allow, rn));
  281. if ((ai & 0x04000000)) ai |= ARMI_LS_R;
  282. emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm);
  283. return;
  284. }
  285. }
  286. }
  287. base = ra_alloc1(as, ref, allow);
  288. #if !LJ_SOFTFP
  289. if ((ai & 0x08000000))
  290. emit_vlso(as, ai, rd, base, ofs);
  291. else
  292. #endif
  293. if ((ai & 0x04000000))
  294. emit_lso(as, ai, rd, base, ofs);
  295. else
  296. emit_lsox(as, ai, rd, base, ofs);
  297. }
  298. #if !LJ_SOFTFP
  299. /*
  300. ** Fuse to multiply-add/sub instruction.
  301. ** VMLA rounds twice (UMA, not FMA) -- no need to check for JIT_F_OPT_FMA.
  302. ** VFMA needs VFPv4, which is uncommon on the remaining ARM32 targets.
  303. */
  304. static int asm_fusemadd(ASMState *as, IRIns *ir, ARMIns ai, ARMIns air)
  305. {
  306. IRRef lref = ir->op1, rref = ir->op2;
  307. IRIns *irm;
  308. if (lref != rref &&
  309. ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&
  310. ra_noreg(irm->r)) ||
  311. (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&
  312. (rref = lref, ai = air, ra_noreg(irm->r))))) {
  313. Reg dest = ra_dest(as, ir, RSET_FPR);
  314. Reg add = ra_hintalloc(as, rref, dest, RSET_FPR);
  315. Reg right, left = ra_alloc2(as, irm,
  316. rset_exclude(rset_exclude(RSET_FPR, dest), add));
  317. right = (left >> 8); left &= 255;
  318. emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15));
  319. if (dest != add) emit_dm(as, ARMI_VMOV_D, (dest & 15), (add & 15));
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. #endif
  325. /* -- Calls --------------------------------------------------------------- */
  326. /* Generate a call to a C function. */
  327. static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
  328. {
  329. uint32_t n, nargs = CCI_XNARGS(ci);
  330. int32_t ofs = 0;
  331. #if LJ_SOFTFP
  332. Reg gpr = REGARG_FIRSTGPR;
  333. #else
  334. Reg gpr, fpr = REGARG_FIRSTFPR, fprodd = 0;
  335. #endif
  336. if ((void *)ci->func)
  337. emit_call(as, (void *)ci->func);
  338. #if !LJ_SOFTFP
  339. for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++)
  340. as->cost[gpr] = REGCOST(~0u, ASMREF_L);
  341. gpr = REGARG_FIRSTGPR;
  342. #endif
  343. for (n = 0; n < nargs; n++) { /* Setup args. */
  344. IRRef ref = args[n];
  345. IRIns *ir = IR(ref);
  346. #if !LJ_SOFTFP
  347. if (ref && irt_isfp(ir->t)) {
  348. RegSet of = as->freeset;
  349. Reg src;
  350. if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) {
  351. if (irt_isnum(ir->t)) {
  352. if (fpr <= REGARG_LASTFPR) {
  353. ra_leftov(as, fpr, ref);
  354. fpr++;
  355. continue;
  356. }
  357. } else if (fprodd) { /* Ick. */
  358. src = ra_alloc1(as, ref, RSET_FPR);
  359. emit_dm(as, ARMI_VMOV_S, (fprodd & 15), (src & 15) | 0x00400000);
  360. fprodd = 0;
  361. continue;
  362. } else if (fpr <= REGARG_LASTFPR) {
  363. ra_leftov(as, fpr, ref);
  364. fprodd = fpr++;
  365. continue;
  366. }
  367. /* Workaround to protect argument GPRs from being used for remat. */
  368. as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1);
  369. src = ra_alloc1(as, ref, RSET_FPR); /* May alloc GPR to remat FPR. */
  370. as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1));
  371. fprodd = 0;
  372. goto stackfp;
  373. }
  374. /* Workaround to protect argument GPRs from being used for remat. */
  375. as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1);
  376. src = ra_alloc1(as, ref, RSET_FPR); /* May alloc GPR to remat FPR. */
  377. as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1));
  378. if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1u;
  379. if (gpr <= REGARG_LASTGPR) {
  380. lj_assertA(rset_test(as->freeset, gpr),
  381. "reg %d not free", gpr); /* Must have been evicted. */
  382. if (irt_isnum(ir->t)) {
  383. lj_assertA(rset_test(as->freeset, gpr+1),
  384. "reg %d not free", gpr+1); /* Ditto. */
  385. emit_dnm(as, ARMI_VMOV_RR_D, gpr, gpr+1, (src & 15));
  386. gpr += 2;
  387. } else {
  388. emit_dn(as, ARMI_VMOV_R_S, gpr, (src & 15));
  389. gpr++;
  390. }
  391. } else {
  392. stackfp:
  393. if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;
  394. emit_spstore(as, ir, src, ofs);
  395. ofs += irt_isnum(ir->t) ? 8 : 4;
  396. }
  397. } else
  398. #endif
  399. {
  400. if (gpr <= REGARG_LASTGPR) {
  401. lj_assertA(rset_test(as->freeset, gpr),
  402. "reg %d not free", gpr); /* Must have been evicted. */
  403. if (ref) ra_leftov(as, gpr, ref);
  404. gpr++;
  405. } else {
  406. if (ref) {
  407. Reg r = ra_alloc1(as, ref, RSET_GPR);
  408. emit_spstore(as, ir, r, ofs);
  409. }
  410. ofs += 4;
  411. }
  412. }
  413. }
  414. }
  415. /* Setup result reg/sp for call. Evict scratch regs. */
  416. static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
  417. {
  418. RegSet drop = RSET_SCRATCH;
  419. int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t));
  420. if (ra_hasreg(ir->r))
  421. rset_clear(drop, ir->r); /* Dest reg handled below. */
  422. if (hiop && ra_hasreg((ir+1)->r))
  423. rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
  424. ra_evictset(as, drop); /* Evictions must be performed first. */
  425. if (ra_used(ir)) {
  426. lj_assertA(!irt_ispri(ir->t), "PRI dest");
  427. if (!LJ_SOFTFP && irt_isfp(ir->t)) {
  428. if (LJ_ABI_SOFTFP || (ci->flags & (CCI_CASTU64|CCI_VARARG))) {
  429. Reg dest = (ra_dest(as, ir, RSET_FPR) & 15);
  430. if (irt_isnum(ir->t))
  431. emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, dest);
  432. else
  433. emit_dn(as, ARMI_VMOV_S_R, RID_RET, dest);
  434. } else {
  435. ra_destreg(as, ir, RID_FPRET);
  436. }
  437. } else if (hiop) {
  438. ra_destpair(as, ir);
  439. } else {
  440. ra_destreg(as, ir, RID_RET);
  441. }
  442. }
  443. UNUSED(ci);
  444. }
  445. static void asm_callx(ASMState *as, IRIns *ir)
  446. {
  447. IRRef args[CCI_NARGS_MAX*2];
  448. CCallInfo ci;
  449. IRRef func;
  450. IRIns *irf;
  451. ci.flags = asm_callx_flags(as, ir);
  452. asm_collectargs(as, ir, &ci, args);
  453. asm_setupresult(as, ir, &ci);
  454. func = ir->op2; irf = IR(func);
  455. if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
  456. if (irref_isk(func)) { /* Call to constant address. */
  457. ci.func = (ASMFunction)(void *)(irf->i);
  458. } else { /* Need a non-argument register for indirect calls. */
  459. Reg freg = ra_alloc1(as, func, RSET_RANGE(RID_R4, RID_R12+1));
  460. emit_m(as, ARMI_BLXr, freg);
  461. ci.func = (ASMFunction)(void *)0;
  462. }
  463. asm_gencall(as, &ci, args);
  464. }
  465. /* -- Returns ------------------------------------------------------------- */
  466. /* Return to lower frame. Guard that it goes to the right spot. */
  467. static void asm_retf(ASMState *as, IRIns *ir)
  468. {
  469. Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
  470. void *pc = ir_kptr(IR(ir->op2));
  471. int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));
  472. as->topslot -= (BCReg)delta;
  473. if ((int32_t)as->topslot < 0) as->topslot = 0;
  474. irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */
  475. /* Need to force a spill on REF_BASE now to update the stack slot. */
  476. emit_lso(as, ARMI_STR, base, RID_SP, ra_spill(as, IR(REF_BASE)));
  477. emit_setgl(as, base, jit_base);
  478. emit_addptr(as, base, -8*delta);
  479. asm_guardcc(as, CC_NE);
  480. emit_nm(as, ARMI_CMP, RID_TMP,
  481. ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base)));
  482. emit_lso(as, ARMI_LDR, RID_TMP, base, -4);
  483. }
  484. /* -- Buffer operations --------------------------------------------------- */
  485. #if LJ_HASBUFFER
  486. static void asm_bufhdr_write(ASMState *as, Reg sb)
  487. {
  488. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb));
  489. IRIns irgc;
  490. int32_t addr = i32ptr((void *)&J2G(as->J)->cur_L);
  491. irgc.ot = IRT(0, IRT_PGC); /* GC type. */
  492. emit_storeofs(as, &irgc, RID_TMP, sb, offsetof(SBuf, L));
  493. if ((as->flags & JIT_F_ARMV6T2)) {
  494. emit_dnm(as, ARMI_BFI, RID_TMP, lj_fls(SBUF_MASK_FLAG), tmp);
  495. } else {
  496. emit_dnm(as, ARMI_ORR, RID_TMP, RID_TMP, tmp);
  497. emit_dn(as, ARMI_AND|ARMI_K12|SBUF_MASK_FLAG, tmp, tmp);
  498. }
  499. emit_lso(as, ARMI_LDR, RID_TMP,
  500. ra_allock(as, (addr & ~4095),
  501. rset_exclude(rset_exclude(RSET_GPR, sb), tmp)),
  502. (addr & 4095));
  503. emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L));
  504. }
  505. #endif
  506. /* -- Type conversions ---------------------------------------------------- */
  507. #if !LJ_SOFTFP
  508. static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
  509. {
  510. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
  511. Reg dest = ra_dest(as, ir, RSET_GPR);
  512. asm_guardcc(as, CC_NE);
  513. emit_d(as, ARMI_VMRS, 0);
  514. emit_dm(as, ARMI_VCMP_D, (tmp & 15), (left & 15));
  515. emit_dm(as, ARMI_VCVT_F64_S32, (tmp & 15), (tmp & 15));
  516. emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15));
  517. emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (left & 15));
  518. }
  519. static void asm_tobit(ASMState *as, IRIns *ir)
  520. {
  521. RegSet allow = RSET_FPR;
  522. Reg left = ra_alloc1(as, ir->op1, allow);
  523. Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));
  524. Reg tmp = ra_scratch(as, rset_clear(allow, right));
  525. Reg dest = ra_dest(as, ir, RSET_GPR);
  526. emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15));
  527. emit_dnm(as, ARMI_VADD_D, (tmp & 15), (left & 15), (right & 15));
  528. }
  529. #endif
  530. static void asm_conv(ASMState *as, IRIns *ir)
  531. {
  532. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
  533. #if !LJ_SOFTFP
  534. int stfp = (st == IRT_NUM || st == IRT_FLOAT);
  535. #endif
  536. IRRef lref = ir->op1;
  537. /* 64 bit integer conversions are handled by SPLIT. */
  538. lj_assertA(!irt_isint64(ir->t) && !(st == IRT_I64 || st == IRT_U64),
  539. "IR %04d has unsplit 64 bit type",
  540. (int)(ir - as->ir) - REF_BIAS);
  541. #if LJ_SOFTFP
  542. /* FP conversions are handled by SPLIT. */
  543. lj_assertA(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT),
  544. "IR %04d has FP type",
  545. (int)(ir - as->ir) - REF_BIAS);
  546. /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */
  547. #else
  548. lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV");
  549. if (irt_isfp(ir->t)) {
  550. Reg dest = ra_dest(as, ir, RSET_FPR);
  551. if (stfp) { /* FP to FP conversion. */
  552. emit_dm(as, st == IRT_NUM ? ARMI_VCVT_F32_F64 : ARMI_VCVT_F64_F32,
  553. (dest & 15), (ra_alloc1(as, lref, RSET_FPR) & 15));
  554. } else { /* Integer to FP conversion. */
  555. Reg left = ra_alloc1(as, lref, RSET_GPR);
  556. ARMIns ai = irt_isfloat(ir->t) ?
  557. (st == IRT_INT ? ARMI_VCVT_F32_S32 : ARMI_VCVT_F32_U32) :
  558. (st == IRT_INT ? ARMI_VCVT_F64_S32 : ARMI_VCVT_F64_U32);
  559. emit_dm(as, ai, (dest & 15), (dest & 15));
  560. emit_dn(as, ARMI_VMOV_S_R, left, (dest & 15));
  561. }
  562. } else if (stfp) { /* FP to integer conversion. */
  563. if (irt_isguard(ir->t)) {
  564. /* Checked conversions are only supported from number to int. */
  565. lj_assertA(irt_isint(ir->t) && st == IRT_NUM,
  566. "bad type for checked CONV");
  567. asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
  568. } else {
  569. Reg left = ra_alloc1(as, lref, RSET_FPR);
  570. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
  571. Reg dest = ra_dest(as, ir, RSET_GPR);
  572. ARMIns ai;
  573. emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15));
  574. ai = irt_isint(ir->t) ?
  575. (st == IRT_NUM ? ARMI_VCVT_S32_F64 : ARMI_VCVT_S32_F32) :
  576. (st == IRT_NUM ? ARMI_VCVT_U32_F64 : ARMI_VCVT_U32_F32);
  577. emit_dm(as, ai, (tmp & 15), (left & 15));
  578. }
  579. } else
  580. #endif
  581. {
  582. Reg dest = ra_dest(as, ir, RSET_GPR);
  583. if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
  584. Reg left = ra_alloc1(as, lref, RSET_GPR);
  585. lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT");
  586. if ((as->flags & JIT_F_ARMV6)) {
  587. ARMIns ai = st == IRT_I8 ? ARMI_SXTB :
  588. st == IRT_U8 ? ARMI_UXTB :
  589. st == IRT_I16 ? ARMI_SXTH : ARMI_UXTH;
  590. emit_dm(as, ai, dest, left);
  591. } else if (st == IRT_U8) {
  592. emit_dn(as, ARMI_AND|ARMI_K12|255, dest, left);
  593. } else {
  594. uint32_t shift = st == IRT_I8 ? 24 : 16;
  595. ARMShift sh = st == IRT_U16 ? ARMSH_LSR : ARMSH_ASR;
  596. emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, RID_TMP);
  597. emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_LSL, shift), RID_TMP, left);
  598. }
  599. } else { /* Handle 32/32 bit no-op (cast). */
  600. ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
  601. }
  602. }
  603. }
  604. static void asm_strto(ASMState *as, IRIns *ir)
  605. {
  606. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];
  607. IRRef args[2];
  608. Reg rlo = 0, rhi = 0, tmp;
  609. int destused = ra_used(ir);
  610. int32_t ofs = 0;
  611. ra_evictset(as, RSET_SCRATCH);
  612. #if LJ_SOFTFP
  613. if (destused) {
  614. if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) &&
  615. (ir->s & 1) == 0 && ir->s + 1 == (ir+1)->s) {
  616. int i;
  617. for (i = 0; i < 2; i++) {
  618. Reg r = (ir+i)->r;
  619. if (ra_hasreg(r)) {
  620. ra_free(as, r);
  621. ra_modified(as, r);
  622. emit_spload(as, ir+i, r, sps_scale((ir+i)->s));
  623. }
  624. }
  625. ofs = sps_scale(ir->s);
  626. destused = 0;
  627. } else {
  628. rhi = ra_dest(as, ir+1, RSET_GPR);
  629. rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi));
  630. }
  631. }
  632. asm_guardcc(as, CC_EQ);
  633. if (destused) {
  634. emit_lso(as, ARMI_LDR, rhi, RID_SP, 4);
  635. emit_lso(as, ARMI_LDR, rlo, RID_SP, 0);
  636. }
  637. #else
  638. UNUSED(rhi);
  639. if (destused) {
  640. if (ra_hasspill(ir->s)) {
  641. ofs = sps_scale(ir->s);
  642. destused = 0;
  643. if (ra_hasreg(ir->r)) {
  644. ra_free(as, ir->r);
  645. ra_modified(as, ir->r);
  646. emit_spload(as, ir, ir->r, ofs);
  647. }
  648. } else {
  649. rlo = ra_dest(as, ir, RSET_FPR);
  650. }
  651. }
  652. asm_guardcc(as, CC_EQ);
  653. if (destused)
  654. emit_vlso(as, ARMI_VLDR_D, rlo, RID_SP, 0);
  655. #endif
  656. emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); /* Test return status. */
  657. args[0] = ir->op1; /* GCstr *str */
  658. args[1] = ASMREF_TMP1; /* TValue *n */
  659. asm_gencall(as, ci, args);
  660. tmp = ra_releasetmp(as, ASMREF_TMP1);
  661. if (ofs == 0)
  662. emit_dm(as, ARMI_MOV, tmp, RID_SP);
  663. else
  664. emit_opk(as, ARMI_ADD, tmp, RID_SP, ofs, RSET_GPR);
  665. }
  666. /* -- Memory references --------------------------------------------------- */
  667. /* Get pointer to TValue. */
  668. static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode)
  669. {
  670. if ((mode & IRTMPREF_IN1)) {
  671. IRIns *ir = IR(ref);
  672. if (irt_isnum(ir->t)) {
  673. if ((mode & IRTMPREF_OUT1)) {
  674. #if LJ_SOFTFP
  675. lj_assertA(irref_isk(ref), "unsplit FP op");
  676. emit_dm(as, ARMI_MOV, dest, RID_SP);
  677. emit_lso(as, ARMI_STR,
  678. ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, RSET_GPR),
  679. RID_SP, 0);
  680. emit_lso(as, ARMI_STR,
  681. ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, RSET_GPR),
  682. RID_SP, 4);
  683. #else
  684. Reg src = ra_alloc1(as, ref, RSET_FPR);
  685. emit_dm(as, ARMI_MOV, dest, RID_SP);
  686. emit_vlso(as, ARMI_VSTR_D, src, RID_SP, 0);
  687. #endif
  688. } else if (irref_isk(ref)) {
  689. /* Use the number constant itself as a TValue. */
  690. ra_allockreg(as, i32ptr(ir_knum(ir)), dest);
  691. } else {
  692. #if LJ_SOFTFP
  693. lj_assertA(0, "unsplit FP op");
  694. #else
  695. /* Otherwise force a spill and use the spill slot. */
  696. emit_opk(as, ARMI_ADD, dest, RID_SP, ra_spill(as, ir), RSET_GPR);
  697. #endif
  698. }
  699. } else {
  700. /* Otherwise use [sp] and [sp+4] to hold the TValue.
  701. ** This assumes the following call has max. 4 args.
  702. */
  703. Reg type;
  704. emit_dm(as, ARMI_MOV, dest, RID_SP);
  705. if (!irt_ispri(ir->t)) {
  706. Reg src = ra_alloc1(as, ref, RSET_GPR);
  707. emit_lso(as, ARMI_STR, src, RID_SP, 0);
  708. }
  709. if (LJ_SOFTFP && (ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t))
  710. type = ra_alloc1(as, ref+1, RSET_GPR);
  711. else
  712. type = ra_allock(as, irt_toitype(ir->t), RSET_GPR);
  713. emit_lso(as, ARMI_STR, type, RID_SP, 4);
  714. }
  715. } else {
  716. emit_dm(as, ARMI_MOV, dest, RID_SP);
  717. }
  718. }
  719. static void asm_aref(ASMState *as, IRIns *ir)
  720. {
  721. Reg dest = ra_dest(as, ir, RSET_GPR);
  722. Reg idx, base;
  723. if (irref_isk(ir->op2)) {
  724. IRRef tab = IR(ir->op1)->op1;
  725. int32_t ofs = asm_fuseabase(as, tab);
  726. IRRef refa = ofs ? tab : ir->op1;
  727. uint32_t k = emit_isk12(ARMI_ADD, ofs + 8*IR(ir->op2)->i);
  728. if (k) {
  729. base = ra_alloc1(as, refa, RSET_GPR);
  730. emit_dn(as, ARMI_ADD^k, dest, base);
  731. return;
  732. }
  733. }
  734. base = ra_alloc1(as, ir->op1, RSET_GPR);
  735. idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
  736. emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, base, idx);
  737. }
  738. /* Inlined hash lookup. Specialized for key type and for const keys.
  739. ** The equivalent C code is:
  740. ** Node *n = hashkey(t, key);
  741. ** do {
  742. ** if (lj_obj_equal(&n->key, key)) return &n->val;
  743. ** } while ((n = nextnode(n)));
  744. ** return niltv(L);
  745. */
  746. static void asm_href(ASMState *as, IRIns *ir, IROp merge)
  747. {
  748. RegSet allow = RSET_GPR;
  749. int destused = ra_used(ir);
  750. Reg dest = ra_dest(as, ir, allow);
  751. Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
  752. Reg key = 0, keyhi = 0, keynumhi = RID_NONE, tmp = RID_TMP;
  753. IRRef refkey = ir->op2;
  754. IRIns *irkey = IR(refkey);
  755. IRType1 kt = irkey->t;
  756. int32_t k = 0, khi = emit_isk12(ARMI_CMP, irt_toitype(kt));
  757. uint32_t khash;
  758. MCLabel l_end, l_loop;
  759. rset_clear(allow, tab);
  760. if (!irref_isk(refkey) || irt_isstr(kt)) {
  761. #if LJ_SOFTFP
  762. key = ra_alloc1(as, refkey, allow);
  763. rset_clear(allow, key);
  764. if (irkey[1].o == IR_HIOP) {
  765. if (ra_hasreg((irkey+1)->r)) {
  766. keynumhi = (irkey+1)->r;
  767. keyhi = RID_TMP;
  768. ra_noweak(as, keynumhi);
  769. } else {
  770. keyhi = keynumhi = ra_allocref(as, refkey+1, allow);
  771. }
  772. rset_clear(allow, keynumhi);
  773. khi = 0;
  774. }
  775. #else
  776. if (irt_isnum(kt)) {
  777. key = ra_scratch(as, allow);
  778. rset_clear(allow, key);
  779. keyhi = keynumhi = ra_scratch(as, allow);
  780. rset_clear(allow, keyhi);
  781. khi = 0;
  782. } else {
  783. key = ra_alloc1(as, refkey, allow);
  784. rset_clear(allow, key);
  785. }
  786. #endif
  787. } else if (irt_isnum(kt)) {
  788. int32_t val = (int32_t)ir_knum(irkey)->u32.lo;
  789. k = emit_isk12(ARMI_CMP, val);
  790. if (!k) {
  791. key = ra_allock(as, val, allow);
  792. rset_clear(allow, key);
  793. }
  794. val = (int32_t)ir_knum(irkey)->u32.hi;
  795. khi = emit_isk12(ARMI_CMP, val);
  796. if (!khi) {
  797. keyhi = ra_allock(as, val, allow);
  798. rset_clear(allow, keyhi);
  799. }
  800. } else if (!irt_ispri(kt)) {
  801. k = emit_isk12(ARMI_CMP, irkey->i);
  802. if (!k) {
  803. key = ra_alloc1(as, refkey, allow);
  804. rset_clear(allow, key);
  805. }
  806. }
  807. if (!irt_ispri(kt))
  808. tmp = ra_scratchpair(as, allow);
  809. /* Key not found in chain: jump to exit (if merged) or load niltv. */
  810. l_end = emit_label(as);
  811. as->invmcp = NULL;
  812. if (merge == IR_NE)
  813. asm_guardcc(as, CC_AL);
  814. else if (destused)
  815. emit_loada(as, dest, niltvg(J2G(as->J)));
  816. /* Follow hash chain until the end. */
  817. l_loop = --as->mcp;
  818. emit_n(as, ARMI_CMP|ARMI_K12|0, dest);
  819. emit_lso(as, ARMI_LDR, dest, dest, (int32_t)offsetof(Node, next));
  820. /* Type and value comparison. */
  821. if (merge == IR_EQ)
  822. asm_guardcc(as, CC_EQ);
  823. else
  824. emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end);
  825. if (!irt_ispri(kt)) {
  826. emit_nm(as, ARMF_CC(ARMI_CMP, CC_EQ)^k, tmp, key);
  827. emit_nm(as, ARMI_CMP^khi, tmp+1, keyhi);
  828. emit_lsox(as, ARMI_LDRD, tmp, dest, (int32_t)offsetof(Node, key));
  829. } else {
  830. emit_n(as, ARMI_CMP^khi, tmp);
  831. emit_lso(as, ARMI_LDR, tmp, dest, (int32_t)offsetof(Node, key.it));
  832. }
  833. *l_loop = ARMF_CC(ARMI_B, CC_NE) | ((as->mcp-l_loop-2) & 0x00ffffffu);
  834. /* Load main position relative to tab->node into dest. */
  835. khash = irref_isk(refkey) ? ir_khash(as, irkey) : 1;
  836. if (khash == 0) {
  837. emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node));
  838. } else {
  839. emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, dest, tmp);
  840. emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 1), tmp, tmp, tmp);
  841. if (irt_isstr(kt)) { /* Fetch of str->sid is cheaper than ra_allock. */
  842. emit_dnm(as, ARMI_AND, tmp, tmp+1, RID_TMP);
  843. emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node));
  844. emit_lso(as, ARMI_LDR, tmp+1, key, (int32_t)offsetof(GCstr, sid));
  845. emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask));
  846. } else if (irref_isk(refkey)) {
  847. emit_opk(as, ARMI_AND, tmp, RID_TMP, (int32_t)khash,
  848. rset_exclude(rset_exclude(RSET_GPR, tab), dest));
  849. emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node));
  850. emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask));
  851. } else { /* Must match with hash*() in lj_tab.c. */
  852. if (ra_hasreg(keynumhi)) { /* Canonicalize +-0.0 to 0.0. */
  853. if (keyhi == RID_TMP)
  854. emit_dm(as, ARMF_CC(ARMI_MOV, CC_NE), keyhi, keynumhi);
  855. emit_d(as, ARMF_CC(ARMI_MOV, CC_EQ)|ARMI_K12|0, keyhi);
  856. }
  857. emit_dnm(as, ARMI_AND, tmp, tmp, RID_TMP);
  858. emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT3), tmp, tmp, tmp+1);
  859. emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node));
  860. emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 32-((HASH_ROT2+HASH_ROT1)&31)),
  861. tmp, tmp+1, tmp);
  862. emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask));
  863. emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT1), tmp+1, tmp+1, tmp);
  864. if (ra_hasreg(keynumhi)) {
  865. emit_dnm(as, ARMI_EOR, tmp+1, tmp, key);
  866. emit_dnm(as, ARMI_ORR|ARMI_S, RID_TMP, tmp, key); /* Test for +-0.0. */
  867. emit_dnm(as, ARMI_ADD, tmp, keynumhi, keynumhi);
  868. #if !LJ_SOFTFP
  869. emit_dnm(as, ARMI_VMOV_RR_D, key, keynumhi,
  870. (ra_alloc1(as, refkey, RSET_FPR) & 15));
  871. #endif
  872. } else {
  873. emit_dnm(as, ARMI_EOR, tmp+1, tmp, key);
  874. emit_opk(as, ARMI_ADD, tmp, key, (int32_t)HASH_BIAS,
  875. rset_exclude(rset_exclude(RSET_GPR, tab), key));
  876. }
  877. }
  878. }
  879. }
  880. static void asm_hrefk(ASMState *as, IRIns *ir)
  881. {
  882. IRIns *kslot = IR(ir->op2);
  883. IRIns *irkey = IR(kslot->op1);
  884. int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
  885. int32_t kofs = ofs + (int32_t)offsetof(Node, key);
  886. Reg dest = (ra_used(ir) || ofs > 4095) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
  887. Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
  888. Reg key = RID_NONE, type = RID_TMP, idx = node;
  889. RegSet allow = rset_exclude(RSET_GPR, node);
  890. lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot");
  891. if (ofs > 4095) {
  892. idx = dest;
  893. rset_clear(allow, dest);
  894. kofs = (int32_t)offsetof(Node, key);
  895. } else if (ra_hasreg(dest)) {
  896. emit_opk(as, ARMI_ADD, dest, node, ofs, allow);
  897. }
  898. asm_guardcc(as, CC_NE);
  899. if (!irt_ispri(irkey->t)) {
  900. RegSet even = (as->freeset & allow);
  901. even = even & (even >> 1) & RSET_GPREVEN;
  902. if (even) {
  903. key = ra_scratch(as, even);
  904. if (rset_test(as->freeset, key+1)) {
  905. type = key+1;
  906. ra_modified(as, type);
  907. }
  908. } else {
  909. key = ra_scratch(as, allow);
  910. }
  911. rset_clear(allow, key);
  912. }
  913. rset_clear(allow, type);
  914. if (irt_isnum(irkey->t)) {
  915. emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, type,
  916. (int32_t)ir_knum(irkey)->u32.hi, allow);
  917. emit_opk(as, ARMI_CMP, 0, key,
  918. (int32_t)ir_knum(irkey)->u32.lo, allow);
  919. } else {
  920. if (ra_hasreg(key))
  921. emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, key, irkey->i, allow);
  922. emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype(irkey->t), type);
  923. }
  924. emit_lso(as, ARMI_LDR, type, idx, kofs+4);
  925. if (ra_hasreg(key)) emit_lso(as, ARMI_LDR, key, idx, kofs);
  926. if (ofs > 4095)
  927. emit_opk(as, ARMI_ADD, dest, node, ofs, RSET_GPR);
  928. }
  929. static void asm_uref(ASMState *as, IRIns *ir)
  930. {
  931. Reg dest = ra_dest(as, ir, RSET_GPR);
  932. int guarded = (irt_t(ir->t) & (IRT_GUARD|IRT_TYPE)) == (IRT_GUARD|IRT_PGC);
  933. if (irref_isk(ir->op1) && !guarded) {
  934. GCfunc *fn = ir_kfunc(IR(ir->op1));
  935. MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
  936. emit_lsptr(as, ARMI_LDR, dest, v);
  937. } else {
  938. if (guarded) {
  939. asm_guardcc(as, ir->o == IR_UREFC ? CC_NE : CC_EQ);
  940. emit_n(as, ARMI_CMP|ARMI_K12|1, RID_TMP);
  941. }
  942. if (ir->o == IR_UREFC)
  943. emit_opk(as, ARMI_ADD, dest, dest,
  944. (int32_t)offsetof(GCupval, tv), RSET_GPR);
  945. else
  946. emit_lso(as, ARMI_LDR, dest, dest, (int32_t)offsetof(GCupval, v));
  947. if (guarded)
  948. emit_lso(as, ARMI_LDRB, RID_TMP, dest,
  949. (int32_t)offsetof(GCupval, closed));
  950. if (irref_isk(ir->op1)) {
  951. GCfunc *fn = ir_kfunc(IR(ir->op1));
  952. int32_t k = (int32_t)gcrefu(fn->l.uvptr[(ir->op2 >> 8)]);
  953. emit_loadi(as, dest, k);
  954. } else {
  955. emit_lso(as, ARMI_LDR, dest, ra_alloc1(as, ir->op1, RSET_GPR),
  956. (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8));
  957. }
  958. }
  959. }
  960. static void asm_fref(ASMState *as, IRIns *ir)
  961. {
  962. UNUSED(as); UNUSED(ir);
  963. lj_assertA(!ra_used(ir), "unfused FREF");
  964. }
  965. static void asm_strref(ASMState *as, IRIns *ir)
  966. {
  967. Reg dest = ra_dest(as, ir, RSET_GPR);
  968. IRRef ref = ir->op2, refk = ir->op1;
  969. Reg r;
  970. if (irref_isk(ref)) {
  971. IRRef tmp = refk; refk = ref; ref = tmp;
  972. } else if (!irref_isk(refk)) {
  973. uint32_t k, m = ARMI_K12|sizeof(GCstr);
  974. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
  975. IRIns *irr = IR(ir->op2);
  976. if (ra_hasreg(irr->r)) {
  977. ra_noweak(as, irr->r);
  978. right = irr->r;
  979. } else if (mayfuse(as, irr->op2) &&
  980. irr->o == IR_ADD && irref_isk(irr->op2) &&
  981. (k = emit_isk12(ARMI_ADD,
  982. (int32_t)sizeof(GCstr) + IR(irr->op2)->i))) {
  983. m = k;
  984. right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));
  985. } else {
  986. right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));
  987. }
  988. emit_dn(as, ARMI_ADD^m, dest, dest);
  989. emit_dnm(as, ARMI_ADD, dest, left, right);
  990. return;
  991. }
  992. r = ra_alloc1(as, ref, RSET_GPR);
  993. emit_opk(as, ARMI_ADD, dest, r,
  994. sizeof(GCstr) + IR(refk)->i, rset_exclude(RSET_GPR, r));
  995. }
  996. /* -- Loads and stores ---------------------------------------------------- */
  997. static ARMIns asm_fxloadins(ASMState *as, IRIns *ir)
  998. {
  999. UNUSED(as);
  1000. switch (irt_type(ir->t)) {
  1001. case IRT_I8: return ARMI_LDRSB;
  1002. case IRT_U8: return ARMI_LDRB;
  1003. case IRT_I16: return ARMI_LDRSH;
  1004. case IRT_U16: return ARMI_LDRH;
  1005. case IRT_NUM: lj_assertA(!LJ_SOFTFP, "unsplit FP op"); return ARMI_VLDR_D;
  1006. case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VLDR_S; /* fallthrough */
  1007. default: return ARMI_LDR;
  1008. }
  1009. }
  1010. static ARMIns asm_fxstoreins(ASMState *as, IRIns *ir)
  1011. {
  1012. UNUSED(as);
  1013. switch (irt_type(ir->t)) {
  1014. case IRT_I8: case IRT_U8: return ARMI_STRB;
  1015. case IRT_I16: case IRT_U16: return ARMI_STRH;
  1016. case IRT_NUM: lj_assertA(!LJ_SOFTFP, "unsplit FP op"); return ARMI_VSTR_D;
  1017. case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VSTR_S; /* fallthrough */
  1018. default: return ARMI_STR;
  1019. }
  1020. }
  1021. static void asm_fload(ASMState *as, IRIns *ir)
  1022. {
  1023. Reg dest = ra_dest(as, ir, RSET_GPR);
  1024. ARMIns ai = asm_fxloadins(as, ir);
  1025. Reg idx;
  1026. int32_t ofs;
  1027. if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */
  1028. idx = ra_allock(as, (int32_t)(ir->op2<<2) + (int32_t)J2GG(as->J), RSET_GPR);
  1029. ofs = 0;
  1030. } else {
  1031. idx = ra_alloc1(as, ir->op1, RSET_GPR);
  1032. if (ir->op2 == IRFL_TAB_ARRAY) {
  1033. ofs = asm_fuseabase(as, ir->op1);
  1034. if (ofs) { /* Turn the t->array load into an add for colocated arrays. */
  1035. emit_dn(as, ARMI_ADD|ARMI_K12|ofs, dest, idx);
  1036. return;
  1037. }
  1038. }
  1039. ofs = field_ofs[ir->op2];
  1040. }
  1041. if ((ai & 0x04000000))
  1042. emit_lso(as, ai, dest, idx, ofs);
  1043. else
  1044. emit_lsox(as, ai, dest, idx, ofs);
  1045. }
  1046. static void asm_fstore(ASMState *as, IRIns *ir)
  1047. {
  1048. if (ir->r != RID_SINK) {
  1049. Reg src = ra_alloc1(as, ir->op2, RSET_GPR);
  1050. IRIns *irf = IR(ir->op1);
  1051. Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));
  1052. int32_t ofs = field_ofs[irf->op2];
  1053. ARMIns ai = asm_fxstoreins(as, ir);
  1054. if ((ai & 0x04000000))
  1055. emit_lso(as, ai, src, idx, ofs);
  1056. else
  1057. emit_lsox(as, ai, src, idx, ofs);
  1058. }
  1059. }
  1060. static void asm_xload(ASMState *as, IRIns *ir)
  1061. {
  1062. Reg dest = ra_dest(as, ir,
  1063. (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);
  1064. lj_assertA(!(ir->op2 & IRXLOAD_UNALIGNED), "unaligned XLOAD");
  1065. asm_fusexref(as, asm_fxloadins(as, ir), dest, ir->op1, RSET_GPR, 0);
  1066. }
  1067. static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)
  1068. {
  1069. if (ir->r != RID_SINK) {
  1070. Reg src = ra_alloc1(as, ir->op2,
  1071. (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);
  1072. asm_fusexref(as, asm_fxstoreins(as, ir), src, ir->op1,
  1073. rset_exclude(RSET_GPR, src), ofs);
  1074. }
  1075. }
  1076. #define asm_xstore(as, ir) asm_xstore_(as, ir, 0)
  1077. static void asm_ahuvload(ASMState *as, IRIns *ir)
  1078. {
  1079. int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP);
  1080. IRType t = hiop ? IRT_NUM : irt_type(ir->t);
  1081. Reg dest = RID_NONE, type = RID_NONE, idx;
  1082. RegSet allow = RSET_GPR;
  1083. int32_t ofs = 0;
  1084. if (hiop && ra_used(ir+1)) {
  1085. type = ra_dest(as, ir+1, allow);
  1086. rset_clear(allow, type);
  1087. }
  1088. if (ra_used(ir)) {
  1089. lj_assertA((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) ||
  1090. irt_isint(ir->t) || irt_isaddr(ir->t),
  1091. "bad load type %d", irt_type(ir->t));
  1092. dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow);
  1093. rset_clear(allow, dest);
  1094. }
  1095. idx = asm_fuseahuref(as, ir->op1, &ofs, allow,
  1096. (!LJ_SOFTFP && t == IRT_NUM) ? 1024 : 4096);
  1097. if (ir->o == IR_VLOAD) ofs += 8 * ir->op2;
  1098. if (!hiop || type == RID_NONE) {
  1099. rset_clear(allow, idx);
  1100. if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 &&
  1101. rset_test((as->freeset & allow), dest+1)) {
  1102. type = dest+1;
  1103. ra_modified(as, type);
  1104. } else {
  1105. type = RID_TMP;
  1106. }
  1107. }
  1108. asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE);
  1109. emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type);
  1110. if (ra_hasreg(dest)) {
  1111. #if !LJ_SOFTFP
  1112. if (t == IRT_NUM)
  1113. emit_vlso(as, ARMI_VLDR_D, dest, idx, ofs);
  1114. else
  1115. #endif
  1116. emit_lso(as, ARMI_LDR, dest, idx, ofs);
  1117. }
  1118. emit_lso(as, ARMI_LDR, type, idx, ofs+4);
  1119. }
  1120. static void asm_ahustore(ASMState *as, IRIns *ir)
  1121. {
  1122. if (ir->r != RID_SINK) {
  1123. RegSet allow = RSET_GPR;
  1124. Reg idx, src = RID_NONE, type = RID_NONE;
  1125. int32_t ofs = 0;
  1126. #if !LJ_SOFTFP
  1127. if (irt_isnum(ir->t)) {
  1128. src = ra_alloc1(as, ir->op2, RSET_FPR);
  1129. idx = asm_fuseahuref(as, ir->op1, &ofs, allow, 1024);
  1130. emit_vlso(as, ARMI_VSTR_D, src, idx, ofs);
  1131. } else
  1132. #endif
  1133. {
  1134. int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP);
  1135. if (!irt_ispri(ir->t)) {
  1136. src = ra_alloc1(as, ir->op2, allow);
  1137. rset_clear(allow, src);
  1138. }
  1139. if (hiop)
  1140. type = ra_alloc1(as, (ir+1)->op2, allow);
  1141. else
  1142. type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
  1143. idx = asm_fuseahuref(as, ir->op1, &ofs, rset_exclude(allow, type), 4096);
  1144. if (ra_hasreg(src)) emit_lso(as, ARMI_STR, src, idx, ofs);
  1145. emit_lso(as, ARMI_STR, type, idx, ofs+4);
  1146. }
  1147. }
  1148. }
  1149. static void asm_sload(ASMState *as, IRIns *ir)
  1150. {
  1151. int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0);
  1152. int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP);
  1153. IRType t = hiop ? IRT_NUM : irt_type(ir->t);
  1154. Reg dest = RID_NONE, type = RID_NONE, base;
  1155. RegSet allow = RSET_GPR;
  1156. lj_assertA(!(ir->op2 & IRSLOAD_PARENT),
  1157. "bad parent SLOAD"); /* Handled by asm_head_side(). */
  1158. lj_assertA(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK),
  1159. "inconsistent SLOAD variant");
  1160. #if LJ_SOFTFP
  1161. lj_assertA(!(ir->op2 & IRSLOAD_CONVERT),
  1162. "unsplit SLOAD convert"); /* Handled by LJ_SOFTFP SPLIT. */
  1163. if (hiop && ra_used(ir+1)) {
  1164. type = ra_dest(as, ir+1, allow);
  1165. rset_clear(allow, type);
  1166. }
  1167. #else
  1168. if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(ir->t) && t == IRT_INT) {
  1169. dest = ra_scratch(as, RSET_FPR);
  1170. asm_tointg(as, ir, dest);
  1171. t = IRT_NUM; /* Continue with a regular number type check. */
  1172. } else
  1173. #endif
  1174. if (ra_used(ir)) {
  1175. Reg tmp = RID_NONE;
  1176. if ((ir->op2 & IRSLOAD_CONVERT))
  1177. tmp = ra_scratch(as, t == IRT_INT ? RSET_FPR : RSET_GPR);
  1178. lj_assertA((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) ||
  1179. irt_isint(ir->t) || irt_isaddr(ir->t),
  1180. "bad SLOAD type %d", irt_type(ir->t));
  1181. dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow);
  1182. rset_clear(allow, dest);
  1183. base = ra_alloc1(as, REF_BASE, allow);
  1184. if ((ir->op2 & IRSLOAD_CONVERT)) {
  1185. if (t == IRT_INT) {
  1186. emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15));
  1187. emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (tmp & 15));
  1188. t = IRT_NUM; /* Check for original type. */
  1189. } else {
  1190. emit_dm(as, ARMI_VCVT_F64_S32, (dest & 15), (dest & 15));
  1191. emit_dn(as, ARMI_VMOV_S_R, tmp, (dest & 15));
  1192. t = IRT_INT; /* Check for original type. */
  1193. }
  1194. dest = tmp;
  1195. }
  1196. goto dotypecheck;
  1197. }
  1198. base = ra_alloc1(as, REF_BASE, allow);
  1199. dotypecheck:
  1200. rset_clear(allow, base);
  1201. if ((ir->op2 & IRSLOAD_TYPECHECK)) {
  1202. if (ra_noreg(type)) {
  1203. if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 &&
  1204. rset_test((as->freeset & allow), dest+1)) {
  1205. type = dest+1;
  1206. ra_modified(as, type);
  1207. } else {
  1208. type = RID_TMP;
  1209. }
  1210. }
  1211. asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE);
  1212. if ((ir->op2 & IRSLOAD_KEYINDEX)) {
  1213. emit_n(as, ARMI_CMN|ARMI_K12|1, type);
  1214. emit_dn(as, ARMI_EOR^emit_isk12(ARMI_EOR, ~LJ_KEYINDEX), type, type);
  1215. } else {
  1216. emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type);
  1217. }
  1218. }
  1219. if (ra_hasreg(dest)) {
  1220. #if !LJ_SOFTFP
  1221. if (t == IRT_NUM) {
  1222. if (ofs < 1024) {
  1223. emit_vlso(as, ARMI_VLDR_D, dest, base, ofs);
  1224. } else {
  1225. if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4);
  1226. emit_vlso(as, ARMI_VLDR_D, dest, RID_TMP, 0);
  1227. emit_opk(as, ARMI_ADD, RID_TMP, base, ofs, allow);
  1228. return;
  1229. }
  1230. } else
  1231. #endif
  1232. emit_lso(as, ARMI_LDR, dest, base, ofs);
  1233. }
  1234. if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4);
  1235. }
  1236. /* -- Allocations --------------------------------------------------------- */
  1237. #if LJ_HASFFI
  1238. static void asm_cnew(ASMState *as, IRIns *ir)
  1239. {
  1240. CTState *cts = ctype_ctsG(J2G(as->J));
  1241. CTypeID id = (CTypeID)IR(ir->op1)->i;
  1242. CTSize sz;
  1243. CTInfo info = lj_ctype_info(cts, id, &sz);
  1244. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
  1245. IRRef args[4];
  1246. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
  1247. RegSet drop = RSET_SCRATCH;
  1248. lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL),
  1249. "bad CNEW/CNEWI operands");
  1250. as->gcsteps++;
  1251. if (ra_hasreg(ir->r))
  1252. rset_clear(drop, ir->r); /* Dest reg handled below. */
  1253. ra_evictset(as, drop);
  1254. if (ra_used(ir))
  1255. ra_destreg(as, ir, RID_RET); /* GCcdata * */
  1256. /* Initialize immutable cdata object. */
  1257. if (ir->o == IR_CNEWI) {
  1258. int32_t ofs = sizeof(GCcdata);
  1259. lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz);
  1260. if (sz == 8) {
  1261. ofs += 4; ir++;
  1262. lj_assertA(ir->o == IR_HIOP, "expected HIOP for CNEWI");
  1263. }
  1264. for (;;) {
  1265. Reg r = ra_alloc1(as, ir->op2, allow);
  1266. emit_lso(as, ARMI_STR, r, RID_RET, ofs);
  1267. rset_clear(allow, r);
  1268. if (ofs == sizeof(GCcdata)) break;
  1269. ofs -= 4; ir--;
  1270. }
  1271. } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */
  1272. ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];
  1273. args[0] = ASMREF_L; /* lua_State *L */
  1274. args[1] = ir->op1; /* CTypeID id */
  1275. args[2] = ir->op2; /* CTSize sz */
  1276. args[3] = ASMREF_TMP1; /* CTSize align */
  1277. asm_gencall(as, ci, args);
  1278. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));
  1279. return;
  1280. }
  1281. /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */
  1282. {
  1283. uint32_t k = emit_isk12(ARMI_MOV, id);
  1284. Reg r = k ? RID_R1 : ra_allock(as, id, allow);
  1285. emit_lso(as, ARMI_STRB, RID_TMP, RID_RET, offsetof(GCcdata, gct));
  1286. emit_lsox(as, ARMI_STRH, r, RID_RET, offsetof(GCcdata, ctypeid));
  1287. emit_d(as, ARMI_MOV|ARMI_K12|~LJ_TCDATA, RID_TMP);
  1288. if (k) emit_d(as, ARMI_MOV^k, RID_R1);
  1289. }
  1290. args[0] = ASMREF_L; /* lua_State *L */
  1291. args[1] = ASMREF_TMP1; /* MSize size */
  1292. asm_gencall(as, ci, args);
  1293. ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
  1294. ra_releasetmp(as, ASMREF_TMP1));
  1295. }
  1296. #endif
  1297. /* -- Write barriers ------------------------------------------------------ */
  1298. static void asm_tbar(ASMState *as, IRIns *ir)
  1299. {
  1300. Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
  1301. Reg link = ra_scratch(as, rset_exclude(RSET_GPR, tab));
  1302. Reg gr = ra_allock(as, i32ptr(J2G(as->J)),
  1303. rset_exclude(rset_exclude(RSET_GPR, tab), link));
  1304. Reg mark = RID_TMP;
  1305. MCLabel l_end = emit_label(as);
  1306. emit_lso(as, ARMI_STR, link, tab, (int32_t)offsetof(GCtab, gclist));
  1307. emit_lso(as, ARMI_STRB, mark, tab, (int32_t)offsetof(GCtab, marked));
  1308. emit_lso(as, ARMI_STR, tab, gr,
  1309. (int32_t)offsetof(global_State, gc.grayagain));
  1310. emit_dn(as, ARMI_BIC|ARMI_K12|LJ_GC_BLACK, mark, mark);
  1311. emit_lso(as, ARMI_LDR, link, gr,
  1312. (int32_t)offsetof(global_State, gc.grayagain));
  1313. emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end);
  1314. emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_BLACK, mark);
  1315. emit_lso(as, ARMI_LDRB, mark, tab, (int32_t)offsetof(GCtab, marked));
  1316. }
  1317. static void asm_obar(ASMState *as, IRIns *ir)
  1318. {
  1319. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
  1320. IRRef args[2];
  1321. MCLabel l_end;
  1322. Reg obj, val, tmp;
  1323. /* No need for other object barriers (yet). */
  1324. lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type");
  1325. ra_evictset(as, RSET_SCRATCH);
  1326. l_end = emit_label(as);
  1327. args[0] = ASMREF_TMP1; /* global_State *g */
  1328. args[1] = ir->op1; /* TValue *tv */
  1329. asm_gencall(as, ci, args);
  1330. if ((l_end[-1] >> 28) == CC_AL)
  1331. l_end[-1] = ARMF_CC(l_end[-1], CC_NE);
  1332. else
  1333. emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end);
  1334. ra_allockreg(as, i32ptr(J2G(as->J)), ra_releasetmp(as, ASMREF_TMP1));
  1335. obj = IR(ir->op1)->r;
  1336. tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
  1337. emit_n(as, ARMF_CC(ARMI_TST, CC_NE)|ARMI_K12|LJ_GC_BLACK, tmp);
  1338. emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_WHITES, RID_TMP);
  1339. val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));
  1340. emit_lso(as, ARMI_LDRB, tmp, obj,
  1341. (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));
  1342. emit_lso(as, ARMI_LDRB, RID_TMP, val, (int32_t)offsetof(GChead, marked));
  1343. }
  1344. /* -- Arithmetic and logic operations ------------------------------------- */
  1345. #if !LJ_SOFTFP
  1346. static void asm_fparith(ASMState *as, IRIns *ir, ARMIns ai)
  1347. {
  1348. Reg dest = ra_dest(as, ir, RSET_FPR);
  1349. Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  1350. right = (left >> 8); left &= 255;
  1351. emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15));
  1352. }
  1353. static void asm_fpunary(ASMState *as, IRIns *ir, ARMIns ai)
  1354. {
  1355. Reg dest = ra_dest(as, ir, RSET_FPR);
  1356. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
  1357. emit_dm(as, ai, (dest & 15), (left & 15));
  1358. }
  1359. static void asm_callround(ASMState *as, IRIns *ir, int id)
  1360. {
  1361. /* The modified regs must match with the *.dasc implementation. */
  1362. RegSet drop = RID2RSET(RID_R0)|RID2RSET(RID_R1)|RID2RSET(RID_R2)|
  1363. RID2RSET(RID_R3)|RID2RSET(RID_R12);
  1364. RegSet of;
  1365. Reg dest, src;
  1366. ra_evictset(as, drop);
  1367. dest = ra_dest(as, ir, RSET_FPR);
  1368. emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, (dest & 15));
  1369. emit_call(as, id == IRFPM_FLOOR ? (void *)lj_vm_floor_sf :
  1370. id == IRFPM_CEIL ? (void *)lj_vm_ceil_sf :
  1371. (void *)lj_vm_trunc_sf);
  1372. /* Workaround to protect argument GPRs from being used for remat. */
  1373. of = as->freeset;
  1374. as->freeset &= ~RSET_RANGE(RID_R0, RID_R1+1);
  1375. as->cost[RID_R0] = as->cost[RID_R1] = REGCOST(~0u, ASMREF_L);
  1376. src = ra_alloc1(as, ir->op1, RSET_FPR); /* May alloc GPR to remat FPR. */
  1377. as->freeset |= (of & RSET_RANGE(RID_R0, RID_R1+1));
  1378. emit_dnm(as, ARMI_VMOV_RR_D, RID_R0, RID_R1, (src & 15));
  1379. }
  1380. static void asm_fpmath(ASMState *as, IRIns *ir)
  1381. {
  1382. if (ir->op2 <= IRFPM_TRUNC)
  1383. asm_callround(as, ir, ir->op2);
  1384. else if (ir->op2 == IRFPM_SQRT)
  1385. asm_fpunary(as, ir, ARMI_VSQRT_D);
  1386. else
  1387. asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);
  1388. }
  1389. #endif
  1390. static int asm_swapops(ASMState *as, IRRef lref, IRRef rref)
  1391. {
  1392. IRIns *ir;
  1393. if (irref_isk(rref))
  1394. return 0; /* Don't swap constants to the left. */
  1395. if (irref_isk(lref))
  1396. return 1; /* But swap constants to the right. */
  1397. ir = IR(rref);
  1398. if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) ||
  1399. (ir->o == IR_ADD && ir->op1 == ir->op2))
  1400. return 0; /* Don't swap fusable operands to the left. */
  1401. ir = IR(lref);
  1402. if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) ||
  1403. (ir->o == IR_ADD && ir->op1 == ir->op2))
  1404. return 1; /* But swap fusable operands to the right. */
  1405. return 0; /* Otherwise don't swap. */
  1406. }
  1407. static void asm_intop(ASMState *as, IRIns *ir, ARMIns ai)
  1408. {
  1409. IRRef lref = ir->op1, rref = ir->op2;
  1410. Reg left, dest = ra_dest(as, ir, RSET_GPR);
  1411. uint32_t m;
  1412. if (asm_swapops(as, lref, rref)) {
  1413. IRRef tmp = lref; lref = rref; rref = tmp;
  1414. if ((ai & ~ARMI_S) == ARMI_SUB || (ai & ~ARMI_S) == ARMI_SBC)
  1415. ai ^= (ARMI_SUB^ARMI_RSB);
  1416. }
  1417. left = ra_hintalloc(as, lref, dest, RSET_GPR);
  1418. m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left));
  1419. if (irt_isguard(ir->t)) { /* For IR_ADDOV etc. */
  1420. asm_guardcc(as, CC_VS);
  1421. ai |= ARMI_S;
  1422. }
  1423. emit_dn(as, ai^m, dest, left);
  1424. }
  1425. /* Try to drop cmp r, #0. */
  1426. static ARMIns asm_drop_cmp0(ASMState *as, ARMIns ai)
  1427. {
  1428. if (as->flagmcp == as->mcp) {
  1429. uint32_t cc = (as->mcp[1] >> 28);
  1430. as->flagmcp = NULL;
  1431. if (cc <= CC_NE) {
  1432. as->mcp++;
  1433. ai |= ARMI_S;
  1434. } else if (cc == CC_GE) {
  1435. *++as->mcp ^= ((CC_GE^CC_PL) << 28);
  1436. ai |= ARMI_S;
  1437. } else if (cc == CC_LT) {
  1438. *++as->mcp ^= ((CC_LT^CC_MI) << 28);
  1439. ai |= ARMI_S;
  1440. } /* else: other conds don't work in general. */
  1441. }
  1442. return ai;
  1443. }
  1444. static void asm_intop_s(ASMState *as, IRIns *ir, ARMIns ai)
  1445. {
  1446. asm_intop(as, ir, asm_drop_cmp0(as, ai));
  1447. }
  1448. static void asm_intneg(ASMState *as, IRIns *ir, ARMIns ai)
  1449. {
  1450. Reg dest = ra_dest(as, ir, RSET_GPR);
  1451. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1452. emit_dn(as, ai|ARMI_K12|0, dest, left);
  1453. }
  1454. /* NYI: use add/shift for MUL(OV) with constants. FOLD only does 2^k. */
  1455. static void asm_intmul(ASMState *as, IRIns *ir)
  1456. {
  1457. Reg dest = ra_dest(as, ir, RSET_GPR);
  1458. Reg left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, dest));
  1459. Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1460. Reg tmp = RID_NONE;
  1461. /* ARMv5 restriction: dest != left and dest_hi != left. */
  1462. if (dest == left && left != right) { left = right; right = dest; }
  1463. if (irt_isguard(ir->t)) { /* IR_MULOV */
  1464. if (!(as->flags & JIT_F_ARMV6) && dest == left)
  1465. tmp = left = ra_scratch(as, rset_exclude(RSET_GPR, left));
  1466. asm_guardcc(as, CC_NE);
  1467. emit_nm(as, ARMI_TEQ|ARMF_SH(ARMSH_ASR, 31), RID_TMP, dest);
  1468. emit_dnm(as, ARMI_SMULL|ARMF_S(right), dest, RID_TMP, left);
  1469. } else {
  1470. if (!(as->flags & JIT_F_ARMV6) && dest == left) tmp = left = RID_TMP;
  1471. emit_nm(as, ARMI_MUL|ARMF_S(right), dest, left);
  1472. }
  1473. /* Only need this for the dest == left == right case. */
  1474. if (ra_hasreg(tmp)) emit_dm(as, ARMI_MOV, tmp, right);
  1475. }
  1476. static void asm_add(ASMState *as, IRIns *ir)
  1477. {
  1478. #if !LJ_SOFTFP
  1479. if (irt_isnum(ir->t)) {
  1480. if (!asm_fusemadd(as, ir, ARMI_VMLA_D, ARMI_VMLA_D))
  1481. asm_fparith(as, ir, ARMI_VADD_D);
  1482. return;
  1483. }
  1484. #endif
  1485. asm_intop_s(as, ir, ARMI_ADD);
  1486. }
  1487. static void asm_sub(ASMState *as, IRIns *ir)
  1488. {
  1489. #if !LJ_SOFTFP
  1490. if (irt_isnum(ir->t)) {
  1491. if (!asm_fusemadd(as, ir, ARMI_VNMLS_D, ARMI_VMLS_D))
  1492. asm_fparith(as, ir, ARMI_VSUB_D);
  1493. return;
  1494. }
  1495. #endif
  1496. asm_intop_s(as, ir, ARMI_SUB);
  1497. }
  1498. static void asm_mul(ASMState *as, IRIns *ir)
  1499. {
  1500. #if !LJ_SOFTFP
  1501. if (irt_isnum(ir->t)) {
  1502. asm_fparith(as, ir, ARMI_VMUL_D);
  1503. return;
  1504. }
  1505. #endif
  1506. asm_intmul(as, ir);
  1507. }
  1508. #define asm_addov(as, ir) asm_add(as, ir)
  1509. #define asm_subov(as, ir) asm_sub(as, ir)
  1510. #define asm_mulov(as, ir) asm_mul(as, ir)
  1511. #if !LJ_SOFTFP
  1512. #define asm_fpdiv(as, ir) asm_fparith(as, ir, ARMI_VDIV_D)
  1513. #define asm_abs(as, ir) asm_fpunary(as, ir, ARMI_VABS_D)
  1514. #endif
  1515. static void asm_neg(ASMState *as, IRIns *ir)
  1516. {
  1517. #if !LJ_SOFTFP
  1518. if (irt_isnum(ir->t)) {
  1519. asm_fpunary(as, ir, ARMI_VNEG_D);
  1520. return;
  1521. }
  1522. #endif
  1523. asm_intneg(as, ir, ARMI_RSB);
  1524. }
  1525. static void asm_bitop(ASMState *as, IRIns *ir, ARMIns ai)
  1526. {
  1527. ai = asm_drop_cmp0(as, ai);
  1528. if (ir->op2 == 0) {
  1529. Reg dest = ra_dest(as, ir, RSET_GPR);
  1530. uint32_t m = asm_fuseopm(as, ai, ir->op1, RSET_GPR);
  1531. emit_d(as, ai^m, dest);
  1532. } else {
  1533. /* NYI: Turn BAND !k12 into uxtb, uxth or bfc or shl+shr. */
  1534. asm_intop(as, ir, ai);
  1535. }
  1536. }
  1537. #define asm_bnot(as, ir) asm_bitop(as, ir, ARMI_MVN)
  1538. static void asm_bswap(ASMState *as, IRIns *ir)
  1539. {
  1540. Reg dest = ra_dest(as, ir, RSET_GPR);
  1541. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1542. if ((as->flags & JIT_F_ARMV6)) {
  1543. emit_dm(as, ARMI_REV, dest, left);
  1544. } else {
  1545. Reg tmp2 = dest;
  1546. if (tmp2 == left)
  1547. tmp2 = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, dest), left));
  1548. emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_LSR, 8), dest, tmp2, RID_TMP);
  1549. emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_ROR, 8), tmp2, left);
  1550. emit_dn(as, ARMI_BIC|ARMI_K12|256*8|255, RID_TMP, RID_TMP);
  1551. emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 16), RID_TMP, left, left);
  1552. }
  1553. }
  1554. #define asm_band(as, ir) asm_bitop(as, ir, ARMI_AND)
  1555. #define asm_bor(as, ir) asm_bitop(as, ir, ARMI_ORR)
  1556. #define asm_bxor(as, ir) asm_bitop(as, ir, ARMI_EOR)
  1557. static void asm_bitshift(ASMState *as, IRIns *ir, ARMShift sh)
  1558. {
  1559. if (irref_isk(ir->op2)) { /* Constant shifts. */
  1560. /* NYI: Turn SHL+SHR or BAND+SHR into uxtb, uxth or ubfx. */
  1561. /* NYI: Turn SHL+ASR into sxtb, sxth or sbfx. */
  1562. Reg dest = ra_dest(as, ir, RSET_GPR);
  1563. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1564. int32_t shift = (IR(ir->op2)->i & 31);
  1565. emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, left);
  1566. } else {
  1567. Reg dest = ra_dest(as, ir, RSET_GPR);
  1568. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1569. Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1570. emit_dm(as, ARMI_MOV|ARMF_RSH(sh, right), dest, left);
  1571. }
  1572. }
  1573. #define asm_bshl(as, ir) asm_bitshift(as, ir, ARMSH_LSL)
  1574. #define asm_bshr(as, ir) asm_bitshift(as, ir, ARMSH_LSR)
  1575. #define asm_bsar(as, ir) asm_bitshift(as, ir, ARMSH_ASR)
  1576. #define asm_bror(as, ir) asm_bitshift(as, ir, ARMSH_ROR)
  1577. #define asm_brol(as, ir) lj_assertA(0, "unexpected BROL")
  1578. static void asm_intmin_max(ASMState *as, IRIns *ir, int cc)
  1579. {
  1580. uint32_t kcmp = 0, kmov = 0;
  1581. Reg dest = ra_dest(as, ir, RSET_GPR);
  1582. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1583. Reg right = 0;
  1584. if (irref_isk(ir->op2)) {
  1585. kcmp = emit_isk12(ARMI_CMP, IR(ir->op2)->i);
  1586. if (kcmp) kmov = emit_isk12(ARMI_MOV, IR(ir->op2)->i);
  1587. }
  1588. if (!kmov) {
  1589. kcmp = 0;
  1590. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1591. }
  1592. if (kmov || dest != right) {
  1593. emit_dm(as, ARMF_CC(ARMI_MOV, cc)^kmov, dest, right);
  1594. cc ^= 1; /* Must use opposite conditions for paired moves. */
  1595. } else {
  1596. cc ^= (CC_LT^CC_GT); /* Otherwise may swap CC_LT <-> CC_GT. */
  1597. }
  1598. if (dest != left) emit_dm(as, ARMF_CC(ARMI_MOV, cc), dest, left);
  1599. emit_nm(as, ARMI_CMP^kcmp, left, right);
  1600. }
  1601. #if LJ_SOFTFP
  1602. static void asm_sfpmin_max(ASMState *as, IRIns *ir, int cc)
  1603. {
  1604. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp];
  1605. RegSet drop = RSET_SCRATCH;
  1606. Reg r;
  1607. IRRef args[4];
  1608. args[0] = ir->op1; args[1] = (ir+1)->op1;
  1609. args[2] = ir->op2; args[3] = (ir+1)->op2;
  1610. /* __aeabi_cdcmple preserves r0-r3. */
  1611. if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);
  1612. if (ra_hasreg((ir+1)->r)) rset_clear(drop, (ir+1)->r);
  1613. if (!rset_test(as->freeset, RID_R2) &&
  1614. regcost_ref(as->cost[RID_R2]) == args[2]) rset_clear(drop, RID_R2);
  1615. if (!rset_test(as->freeset, RID_R3) &&
  1616. regcost_ref(as->cost[RID_R3]) == args[3]) rset_clear(drop, RID_R3);
  1617. ra_evictset(as, drop);
  1618. ra_destpair(as, ir);
  1619. emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETHI, RID_R3);
  1620. emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETLO, RID_R2);
  1621. emit_call(as, (void *)ci->func);
  1622. for (r = RID_R0; r <= RID_R3; r++)
  1623. ra_leftov(as, r, args[r-RID_R0]);
  1624. }
  1625. #else
  1626. static void asm_fpmin_max(ASMState *as, IRIns *ir, int cc)
  1627. {
  1628. Reg dest = (ra_dest(as, ir, RSET_FPR) & 15);
  1629. Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  1630. right = ((left >> 8) & 15); left &= 15;
  1631. if (dest != left) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc^1), dest, left);
  1632. if (dest != right) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc), dest, right);
  1633. emit_d(as, ARMI_VMRS, 0);
  1634. emit_dm(as, ARMI_VCMP_D, left, right);
  1635. }
  1636. #endif
  1637. static void asm_min_max(ASMState *as, IRIns *ir, int cc, int fcc)
  1638. {
  1639. #if LJ_SOFTFP
  1640. UNUSED(fcc);
  1641. #else
  1642. if (irt_isnum(ir->t))
  1643. asm_fpmin_max(as, ir, fcc);
  1644. else
  1645. #endif
  1646. asm_intmin_max(as, ir, cc);
  1647. }
  1648. #define asm_min(as, ir) asm_min_max(as, ir, CC_GT, CC_PL)
  1649. #define asm_max(as, ir) asm_min_max(as, ir, CC_LT, CC_LE)
  1650. /* -- Comparisons --------------------------------------------------------- */
  1651. /* Map of comparisons to flags. ORDER IR. */
  1652. static const uint8_t asm_compmap[IR_ABC+1] = {
  1653. /* op FP swp int cc FP cc */
  1654. /* LT */ CC_GE + (CC_HS << 4),
  1655. /* GE x */ CC_LT + (CC_HI << 4),
  1656. /* LE */ CC_GT + (CC_HI << 4),
  1657. /* GT x */ CC_LE + (CC_HS << 4),
  1658. /* ULT x */ CC_HS + (CC_LS << 4),
  1659. /* UGE */ CC_LO + (CC_LO << 4),
  1660. /* ULE x */ CC_HI + (CC_LO << 4),
  1661. /* UGT */ CC_LS + (CC_LS << 4),
  1662. /* EQ */ CC_NE + (CC_NE << 4),
  1663. /* NE */ CC_EQ + (CC_EQ << 4),
  1664. /* ABC */ CC_LS + (CC_LS << 4) /* Same as UGT. */
  1665. };
  1666. #if LJ_SOFTFP
  1667. /* FP comparisons. */
  1668. static void asm_sfpcomp(ASMState *as, IRIns *ir)
  1669. {
  1670. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp];
  1671. RegSet drop = RSET_SCRATCH;
  1672. Reg r;
  1673. IRRef args[4];
  1674. int swp = (((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1) << 1);
  1675. args[swp^0] = ir->op1; args[swp^1] = (ir+1)->op1;
  1676. args[swp^2] = ir->op2; args[swp^3] = (ir+1)->op2;
  1677. /* __aeabi_cdcmple preserves r0-r3. This helps to reduce spills. */
  1678. for (r = RID_R0; r <= RID_R3; r++)
  1679. if (!rset_test(as->freeset, r) &&
  1680. regcost_ref(as->cost[r]) == args[r-RID_R0]) rset_clear(drop, r);
  1681. ra_evictset(as, drop);
  1682. asm_guardcc(as, (asm_compmap[ir->o] >> 4));
  1683. emit_call(as, (void *)ci->func);
  1684. for (r = RID_R0; r <= RID_R3; r++)
  1685. ra_leftov(as, r, args[r-RID_R0]);
  1686. }
  1687. #else
  1688. /* FP comparisons. */
  1689. static void asm_fpcomp(ASMState *as, IRIns *ir)
  1690. {
  1691. Reg left, right;
  1692. ARMIns ai;
  1693. int swp = ((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1);
  1694. if (!swp && irref_isk(ir->op2) && ir_knum(IR(ir->op2))->u64 == 0) {
  1695. left = (ra_alloc1(as, ir->op1, RSET_FPR) & 15);
  1696. right = 0;
  1697. ai = ARMI_VCMPZ_D;
  1698. } else {
  1699. left = ra_alloc2(as, ir, RSET_FPR);
  1700. if (swp) {
  1701. right = (left & 15); left = ((left >> 8) & 15);
  1702. } else {
  1703. right = ((left >> 8) & 15); left &= 15;
  1704. }
  1705. ai = ARMI_VCMP_D;
  1706. }
  1707. asm_guardcc(as, (asm_compmap[ir->o] >> 4));
  1708. emit_d(as, ARMI_VMRS, 0);
  1709. emit_dm(as, ai, left, right);
  1710. }
  1711. #endif
  1712. /* Integer comparisons. */
  1713. static void asm_intcomp(ASMState *as, IRIns *ir)
  1714. {
  1715. ARMCC cc = (asm_compmap[ir->o] & 15);
  1716. IRRef lref = ir->op1, rref = ir->op2;
  1717. Reg left;
  1718. uint32_t m;
  1719. int cmpprev0 = 0;
  1720. lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t),
  1721. "bad comparison data type %d", irt_type(ir->t));
  1722. if (asm_swapops(as, lref, rref)) {
  1723. Reg tmp = lref; lref = rref; rref = tmp;
  1724. if (cc >= CC_GE) cc ^= 7; /* LT <-> GT, LE <-> GE */
  1725. else if (cc > CC_NE) cc ^= 11; /* LO <-> HI, LS <-> HS */
  1726. }
  1727. if (irref_isk(rref) && IR(rref)->i == 0) {
  1728. IRIns *irl = IR(lref);
  1729. cmpprev0 = (irl+1 == ir);
  1730. /* Combine comp(BAND(left, right), 0) into tst left, right. */
  1731. if (cmpprev0 && irl->o == IR_BAND && !ra_used(irl)) {
  1732. IRRef blref = irl->op1, brref = irl->op2;
  1733. uint32_t m2 = 0;
  1734. Reg bleft;
  1735. if (asm_swapops(as, blref, brref)) {
  1736. Reg tmp = blref; blref = brref; brref = tmp;
  1737. }
  1738. if (irref_isk(brref)) {
  1739. m2 = emit_isk12(ARMI_AND, IR(brref)->i);
  1740. if ((m2 & (ARMI_AND^ARMI_BIC)))
  1741. goto notst; /* Not beneficial if we miss a constant operand. */
  1742. }
  1743. if (cc == CC_GE) cc = CC_PL;
  1744. else if (cc == CC_LT) cc = CC_MI;
  1745. else if (cc > CC_NE) goto notst; /* Other conds don't work with tst. */
  1746. bleft = ra_alloc1(as, blref, RSET_GPR);
  1747. if (!m2) m2 = asm_fuseopm(as, 0, brref, rset_exclude(RSET_GPR, bleft));
  1748. asm_guardcc(as, cc);
  1749. emit_n(as, ARMI_TST^m2, bleft);
  1750. return;
  1751. }
  1752. }
  1753. notst:
  1754. left = ra_alloc1(as, lref, RSET_GPR);
  1755. m = asm_fuseopm(as, ARMI_CMP, rref, rset_exclude(RSET_GPR, left));
  1756. asm_guardcc(as, cc);
  1757. emit_n(as, ARMI_CMP^m, left);
  1758. /* Signed comparison with zero and referencing previous ins? */
  1759. if (cmpprev0 && (cc <= CC_NE || cc >= CC_GE))
  1760. as->flagmcp = as->mcp; /* Allow elimination of the compare. */
  1761. }
  1762. static void asm_comp(ASMState *as, IRIns *ir)
  1763. {
  1764. #if !LJ_SOFTFP
  1765. if (irt_isnum(ir->t))
  1766. asm_fpcomp(as, ir);
  1767. else
  1768. #endif
  1769. asm_intcomp(as, ir);
  1770. }
  1771. #define asm_equal(as, ir) asm_comp(as, ir)
  1772. #if LJ_HASFFI
  1773. /* 64 bit integer comparisons. */
  1774. static void asm_int64comp(ASMState *as, IRIns *ir)
  1775. {
  1776. int signedcomp = (ir->o <= IR_GT);
  1777. ARMCC cclo, cchi;
  1778. Reg leftlo, lefthi;
  1779. uint32_t mlo, mhi;
  1780. RegSet allow = RSET_GPR, oldfree;
  1781. /* Always use unsigned comparison for loword. */
  1782. cclo = asm_compmap[ir->o + (signedcomp ? 4 : 0)] & 15;
  1783. leftlo = ra_alloc1(as, ir->op1, allow);
  1784. oldfree = as->freeset;
  1785. mlo = asm_fuseopm(as, ARMI_CMP, ir->op2, rset_clear(allow, leftlo));
  1786. allow &= ~(oldfree & ~as->freeset); /* Update for allocs of asm_fuseopm. */
  1787. /* Use signed or unsigned comparison for hiword. */
  1788. cchi = asm_compmap[ir->o] & 15;
  1789. lefthi = ra_alloc1(as, (ir+1)->op1, allow);
  1790. mhi = asm_fuseopm(as, ARMI_CMP, (ir+1)->op2, rset_clear(allow, lefthi));
  1791. /* All register allocations must be performed _before_ this point. */
  1792. if (signedcomp) {
  1793. MCLabel l_around = emit_label(as);
  1794. asm_guardcc(as, cclo);
  1795. emit_n(as, ARMI_CMP^mlo, leftlo);
  1796. emit_branch(as, ARMF_CC(ARMI_B, CC_NE), l_around);
  1797. if (cchi == CC_GE || cchi == CC_LE) cchi ^= 6; /* GE -> GT, LE -> LT */
  1798. asm_guardcc(as, cchi);
  1799. } else {
  1800. asm_guardcc(as, cclo);
  1801. emit_n(as, ARMF_CC(ARMI_CMP, CC_EQ)^mlo, leftlo);
  1802. }
  1803. emit_n(as, ARMI_CMP^mhi, lefthi);
  1804. }
  1805. #endif
  1806. /* -- Split register ops -------------------------------------------------- */
  1807. /* Hiword op of a split 32/32 bit op. Previous op is the loword op. */
  1808. static void asm_hiop(ASMState *as, IRIns *ir)
  1809. {
  1810. /* HIOP is marked as a store because it needs its own DCE logic. */
  1811. int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
  1812. if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
  1813. #if LJ_HASFFI || LJ_SOFTFP
  1814. if ((ir-1)->o <= IR_NE) { /* 64 bit integer or FP comparisons. ORDER IR. */
  1815. as->curins--; /* Always skip the loword comparison. */
  1816. #if LJ_SOFTFP
  1817. if (!irt_isint(ir->t)) {
  1818. asm_sfpcomp(as, ir-1);
  1819. return;
  1820. }
  1821. #endif
  1822. #if LJ_HASFFI
  1823. asm_int64comp(as, ir-1);
  1824. #endif
  1825. return;
  1826. #if LJ_SOFTFP
  1827. } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) {
  1828. as->curins--; /* Always skip the loword min/max. */
  1829. if (uselo || usehi)
  1830. asm_sfpmin_max(as, ir-1, (ir-1)->o == IR_MIN ? CC_PL : CC_LE);
  1831. return;
  1832. #elif LJ_HASFFI
  1833. } else if ((ir-1)->o == IR_CONV) {
  1834. as->curins--; /* Always skip the CONV. */
  1835. if (usehi || uselo)
  1836. asm_conv64(as, ir);
  1837. return;
  1838. #endif
  1839. } else if ((ir-1)->o == IR_XSTORE) {
  1840. if ((ir-1)->r != RID_SINK)
  1841. asm_xstore_(as, ir, 4);
  1842. return;
  1843. }
  1844. #endif
  1845. if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
  1846. switch ((ir-1)->o) {
  1847. #if LJ_HASFFI
  1848. case IR_ADD:
  1849. as->curins--;
  1850. asm_intop(as, ir, ARMI_ADC);
  1851. asm_intop(as, ir-1, ARMI_ADD|ARMI_S);
  1852. break;
  1853. case IR_SUB:
  1854. as->curins--;
  1855. asm_intop(as, ir, ARMI_SBC);
  1856. asm_intop(as, ir-1, ARMI_SUB|ARMI_S);
  1857. break;
  1858. case IR_NEG:
  1859. as->curins--;
  1860. asm_intneg(as, ir, ARMI_RSC);
  1861. asm_intneg(as, ir-1, ARMI_RSB|ARMI_S);
  1862. break;
  1863. case IR_CNEWI:
  1864. /* Nothing to do here. Handled by lo op itself. */
  1865. break;
  1866. #endif
  1867. #if LJ_SOFTFP
  1868. case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:
  1869. case IR_STRTO:
  1870. if (!uselo)
  1871. ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */
  1872. break;
  1873. case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR: case IR_TMPREF:
  1874. /* Nothing to do here. Handled by lo op itself. */
  1875. break;
  1876. #endif
  1877. case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS:
  1878. if (!uselo)
  1879. ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
  1880. break;
  1881. default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break;
  1882. }
  1883. }
  1884. /* -- Profiling ----------------------------------------------------------- */
  1885. static void asm_prof(ASMState *as, IRIns *ir)
  1886. {
  1887. UNUSED(ir);
  1888. asm_guardcc(as, CC_NE);
  1889. emit_n(as, ARMI_TST|ARMI_K12|HOOK_PROFILE, RID_TMP);
  1890. emit_lsptr(as, ARMI_LDRB, RID_TMP, (void *)&J2G(as->J)->hookmask);
  1891. }
  1892. /* -- Stack handling ------------------------------------------------------ */
  1893. /* Check Lua stack size for overflow. Use exit handler as fallback. */
  1894. static void asm_stack_check(ASMState *as, BCReg topslot,
  1895. IRIns *irp, RegSet allow, ExitNo exitno)
  1896. {
  1897. int savereg = 0;
  1898. Reg pbase;
  1899. uint32_t k;
  1900. if (irp) {
  1901. if (!ra_hasspill(irp->s)) {
  1902. pbase = irp->r;
  1903. lj_assertA(ra_hasreg(pbase), "base reg lost");
  1904. } else if (allow) {
  1905. pbase = rset_pickbot(allow);
  1906. } else {
  1907. pbase = RID_RET;
  1908. savereg = 1;
  1909. }
  1910. } else {
  1911. pbase = RID_BASE;
  1912. }
  1913. emit_branch(as, ARMF_CC(ARMI_BL, CC_LS), exitstub_addr(as->J, exitno));
  1914. if (savereg)
  1915. emit_lso(as, ARMI_LDR, RID_RET, RID_SP, 0); /* Restore temp. register. */
  1916. k = emit_isk12(0, (int32_t)(8*topslot));
  1917. lj_assertA(k, "slot offset %d does not fit in K12", 8*topslot);
  1918. emit_n(as, ARMI_CMP^k, RID_TMP);
  1919. emit_dnm(as, ARMI_SUB, RID_TMP, RID_TMP, pbase);
  1920. emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP,
  1921. (int32_t)offsetof(lua_State, maxstack));
  1922. if (irp) { /* Must not spill arbitrary registers in head of side trace. */
  1923. int32_t i = i32ptr(&J2G(as->J)->cur_L);
  1924. if (ra_hasspill(irp->s))
  1925. emit_lso(as, ARMI_LDR, pbase, RID_SP, sps_scale(irp->s));
  1926. emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, (i & 4095));
  1927. if (savereg)
  1928. emit_lso(as, ARMI_STR, RID_RET, RID_SP, 0); /* Save temp. register. */
  1929. emit_loadi(as, RID_TMP, (i & ~4095));
  1930. } else {
  1931. emit_getgl(as, RID_TMP, cur_L);
  1932. }
  1933. }
  1934. /* Restore Lua stack from on-trace state. */
  1935. static void asm_stack_restore(ASMState *as, SnapShot *snap)
  1936. {
  1937. SnapEntry *map = &as->T->snapmap[snap->mapofs];
  1938. SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1];
  1939. MSize n, nent = snap->nent;
  1940. int32_t bias = 0;
  1941. /* Store the value of all modified slots to the Lua stack. */
  1942. for (n = 0; n < nent; n++) {
  1943. SnapEntry sn = map[n];
  1944. BCReg s = snap_slot(sn);
  1945. int32_t ofs = 8*((int32_t)s-1) - bias;
  1946. IRRef ref = snap_ref(sn);
  1947. IRIns *ir = IR(ref);
  1948. if ((sn & SNAP_NORESTORE))
  1949. continue;
  1950. if (irt_isnum(ir->t)) {
  1951. #if LJ_SOFTFP
  1952. RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE);
  1953. Reg tmp;
  1954. /* LJ_SOFTFP: must be a number constant. */
  1955. lj_assertA(irref_isk(ref), "unsplit FP op");
  1956. tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo,
  1957. rset_exclude(RSET_GPREVEN, RID_BASE));
  1958. emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs);
  1959. if (rset_test(as->freeset, tmp+1)) odd = RID2RSET(tmp+1);
  1960. tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, odd);
  1961. emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs+4);
  1962. #else
  1963. Reg src = ra_alloc1(as, ref, RSET_FPR);
  1964. if (LJ_UNLIKELY(ofs < -1020 || ofs > 1020)) {
  1965. int32_t adj = ofs & 0xffffff00; /* K12-friendly. */
  1966. bias += adj;
  1967. ofs -= adj;
  1968. emit_addptr(as, RID_BASE, -adj);
  1969. }
  1970. emit_vlso(as, ARMI_VSTR_D, src, RID_BASE, ofs);
  1971. #endif
  1972. } else {
  1973. RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE);
  1974. Reg type;
  1975. lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t),
  1976. "restore of IR type %d", irt_type(ir->t));
  1977. if (!irt_ispri(ir->t)) {
  1978. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPREVEN, RID_BASE));
  1979. emit_lso(as, ARMI_STR, src, RID_BASE, ofs);
  1980. if (rset_test(as->freeset, src+1)) odd = RID2RSET(src+1);
  1981. }
  1982. if ((sn & (SNAP_CONT|SNAP_FRAME))) {
  1983. if (s == 0) continue; /* Do not overwrite link to previous frame. */
  1984. type = ra_allock(as, (int32_t)(*flinks--), odd);
  1985. #if LJ_SOFTFP
  1986. } else if ((sn & SNAP_SOFTFPNUM)) {
  1987. type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPRODD, RID_BASE));
  1988. #endif
  1989. } else if ((sn & SNAP_KEYINDEX)) {
  1990. type = ra_allock(as, (int32_t)LJ_KEYINDEX, odd);
  1991. } else {
  1992. type = ra_allock(as, (int32_t)irt_toitype(ir->t), odd);
  1993. }
  1994. emit_lso(as, ARMI_STR, type, RID_BASE, ofs+4);
  1995. }
  1996. checkmclim(as);
  1997. }
  1998. emit_addptr(as, RID_BASE, bias);
  1999. lj_assertA(map + nent == flinks, "inconsistent frames in snapshot");
  2000. }
  2001. /* -- GC handling --------------------------------------------------------- */
  2002. /* Marker to prevent patching the GC check exit. */
  2003. #define ARM_NOPATCH_GC_CHECK (ARMI_BIC|ARMI_K12)
  2004. /* Check GC threshold and do one or more GC steps. */
  2005. static void asm_gc_check(ASMState *as)
  2006. {
  2007. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
  2008. IRRef args[2];
  2009. MCLabel l_end;
  2010. Reg tmp1, tmp2;
  2011. ra_evictset(as, RSET_SCRATCH);
  2012. l_end = emit_label(as);
  2013. /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
  2014. asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */
  2015. *--as->mcp = ARM_NOPATCH_GC_CHECK;
  2016. emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET);
  2017. args[0] = ASMREF_TMP1; /* global_State *g */
  2018. args[1] = ASMREF_TMP2; /* MSize steps */
  2019. asm_gencall(as, ci, args);
  2020. tmp1 = ra_releasetmp(as, ASMREF_TMP1);
  2021. tmp2 = ra_releasetmp(as, ASMREF_TMP2);
  2022. emit_loadi(as, tmp2, as->gcsteps);
  2023. /* Jump around GC step if GC total < GC threshold. */
  2024. emit_branch(as, ARMF_CC(ARMI_B, CC_LS), l_end);
  2025. emit_nm(as, ARMI_CMP, RID_TMP, tmp2);
  2026. emit_lso(as, ARMI_LDR, tmp2, tmp1,
  2027. (int32_t)offsetof(global_State, gc.threshold));
  2028. emit_lso(as, ARMI_LDR, RID_TMP, tmp1,
  2029. (int32_t)offsetof(global_State, gc.total));
  2030. ra_allockreg(as, i32ptr(J2G(as->J)), tmp1);
  2031. as->gcsteps = 0;
  2032. checkmclim(as);
  2033. }
  2034. /* -- Loop handling ------------------------------------------------------- */
  2035. /* Fixup the loop branch. */
  2036. static void asm_loop_fixup(ASMState *as)
  2037. {
  2038. MCode *p = as->mctop;
  2039. MCode *target = as->mcp;
  2040. if (as->loopinv) { /* Inverted loop branch? */
  2041. /* asm_guardcc already inverted the bcc and patched the final bl. */
  2042. p[-2] |= ((uint32_t)(target-p) & 0x00ffffffu);
  2043. } else {
  2044. p[-1] = ARMI_B | ((uint32_t)((target-p)-1) & 0x00ffffffu);
  2045. }
  2046. }
  2047. /* Fixup the tail of the loop. */
  2048. static void asm_loop_tail_fixup(ASMState *as)
  2049. {
  2050. UNUSED(as); /* Nothing to do. */
  2051. }
  2052. /* -- Head of trace ------------------------------------------------------- */
  2053. /* Reload L register from g->cur_L. */
  2054. static void asm_head_lreg(ASMState *as)
  2055. {
  2056. IRIns *ir = IR(ASMREF_L);
  2057. if (ra_used(ir)) {
  2058. Reg r = ra_dest(as, ir, RSET_GPR);
  2059. emit_getgl(as, r, cur_L);
  2060. ra_evictk(as);
  2061. }
  2062. }
  2063. /* Coalesce BASE register for a root trace. */
  2064. static void asm_head_root_base(ASMState *as)
  2065. {
  2066. IRIns *ir;
  2067. asm_head_lreg(as);
  2068. ir = IR(REF_BASE);
  2069. if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t)))
  2070. ra_spill(as, ir);
  2071. ra_destreg(as, ir, RID_BASE);
  2072. }
  2073. /* Coalesce BASE register for a side trace. */
  2074. static Reg asm_head_side_base(ASMState *as, IRIns *irp)
  2075. {
  2076. IRIns *ir;
  2077. asm_head_lreg(as);
  2078. ir = IR(REF_BASE);
  2079. if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t)))
  2080. ra_spill(as, ir);
  2081. if (ra_hasspill(irp->s)) {
  2082. return ra_dest(as, ir, RSET_GPR);
  2083. } else {
  2084. Reg r = irp->r;
  2085. lj_assertA(ra_hasreg(r), "base reg lost");
  2086. if (r != ir->r && !rset_test(as->freeset, r))
  2087. ra_restore(as, regcost_ref(as->cost[r]));
  2088. ra_destreg(as, ir, r);
  2089. return r;
  2090. }
  2091. }
  2092. /* -- Tail of trace ------------------------------------------------------- */
  2093. /* Fixup the tail code. */
  2094. static void asm_tail_fixup(ASMState *as, TraceNo lnk)
  2095. {
  2096. MCode *p = as->mctop;
  2097. MCode *target;
  2098. int32_t spadj = as->T->spadjust;
  2099. if (spadj == 0) {
  2100. as->mctop = --p;
  2101. } else {
  2102. /* Patch stack adjustment. */
  2103. uint32_t k = emit_isk12(ARMI_ADD, spadj);
  2104. lj_assertA(k, "stack adjustment %d does not fit in K12", spadj);
  2105. p[-2] = (ARMI_ADD^k) | ARMF_D(RID_SP) | ARMF_N(RID_SP);
  2106. }
  2107. /* Patch exit branch. */
  2108. target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;
  2109. p[-1] = ARMI_B|(((target-p)-1)&0x00ffffffu);
  2110. }
  2111. /* Prepare tail of code. */
  2112. static void asm_tail_prep(ASMState *as)
  2113. {
  2114. MCode *p = as->mctop - 1; /* Leave room for exit branch. */
  2115. if (as->loopref) {
  2116. as->invmcp = as->mcp = p;
  2117. } else {
  2118. as->mcp = p-1; /* Leave room for stack pointer adjustment. */
  2119. as->invmcp = NULL;
  2120. }
  2121. *p = 0; /* Prevent load/store merging. */
  2122. }
  2123. /* -- Trace setup --------------------------------------------------------- */
  2124. /* Ensure there are enough stack slots for call arguments. */
  2125. static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
  2126. {
  2127. IRRef args[CCI_NARGS_MAX*2];
  2128. uint32_t i, nargs = CCI_XNARGS(ci);
  2129. int nslots = 0, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR, fprodd = 0;
  2130. asm_collectargs(as, ir, ci, args);
  2131. for (i = 0; i < nargs; i++) {
  2132. if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t)) {
  2133. if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) {
  2134. if (irt_isnum(IR(args[i])->t)) {
  2135. if (nfpr > 0) nfpr--;
  2136. else fprodd = 0, nslots = (nslots + 3) & ~1;
  2137. } else {
  2138. if (fprodd) fprodd--;
  2139. else if (nfpr > 0) fprodd = 1, nfpr--;
  2140. else nslots++;
  2141. }
  2142. } else if (irt_isnum(IR(args[i])->t)) {
  2143. ngpr &= ~1;
  2144. if (ngpr > 0) ngpr -= 2; else nslots += 2;
  2145. } else {
  2146. if (ngpr > 0) ngpr--; else nslots++;
  2147. }
  2148. } else {
  2149. if (ngpr > 0) ngpr--; else nslots++;
  2150. }
  2151. }
  2152. if (nslots > as->evenspill) /* Leave room for args in stack slots. */
  2153. as->evenspill = nslots;
  2154. return REGSP_HINT(irt_isfp(ir->t) ? RID_FPRET : RID_RET);
  2155. }
  2156. static void asm_setup_target(ASMState *as)
  2157. {
  2158. /* May need extra exit for asm_stack_check on side traces. */
  2159. asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0));
  2160. }
  2161. /* -- Trace patching ------------------------------------------------------ */
  2162. /* Patch exit jumps of existing machine code to a new target. */
  2163. void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
  2164. {
  2165. MCode *p = T->mcode;
  2166. MCode *pe = (MCode *)((char *)p + T->szmcode);
  2167. MCode *cstart = NULL, *cend = p;
  2168. MCode *mcarea = lj_mcode_patch(J, p, 0);
  2169. MCode *px = exitstub_addr(J, exitno) - 2;
  2170. for (; p < pe; p++) {
  2171. /* Look for bl_cc exitstub, replace with b_cc target. */
  2172. uint32_t ins = *p;
  2173. if ((ins & 0x0f000000u) == 0x0b000000u && ins < 0xf0000000u &&
  2174. ((ins ^ (px-p)) & 0x00ffffffu) == 0 &&
  2175. p[-1] != ARM_NOPATCH_GC_CHECK) {
  2176. *p = (ins & 0xfe000000u) | (((target-p)-2) & 0x00ffffffu);
  2177. cend = p+1;
  2178. if (!cstart) cstart = p;
  2179. }
  2180. }
  2181. lj_assertJ(cstart != NULL, "exit stub %d not found", exitno);
  2182. lj_mcode_sync(cstart, cend);
  2183. lj_mcode_patch(J, mcarea, 1);
  2184. }