lj_asm_arm64.h 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. /*
  2. ** ARM64 IR assembler (SSA IR -> machine code).
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Contributed by Djordje Kovacevic and Stefan Pejic from RT-RK.com.
  6. ** Sponsored by Cisco Systems, Inc.
  7. */
  8. /* -- Register allocator extensions --------------------------------------- */
  9. /* Allocate a register with a hint. */
  10. static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)
  11. {
  12. Reg r = IR(ref)->r;
  13. if (ra_noreg(r)) {
  14. if (!ra_hashint(r) && !iscrossref(as, ref))
  15. ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */
  16. r = ra_allocref(as, ref, allow);
  17. }
  18. ra_noweak(as, r);
  19. return r;
  20. }
  21. /* Allocate two source registers for three-operand instructions. */
  22. static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)
  23. {
  24. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
  25. Reg left = irl->r, right = irr->r;
  26. if (ra_hasreg(left)) {
  27. ra_noweak(as, left);
  28. if (ra_noreg(right))
  29. right = ra_allocref(as, ir->op2, rset_exclude(allow, left));
  30. else
  31. ra_noweak(as, right);
  32. } else if (ra_hasreg(right)) {
  33. ra_noweak(as, right);
  34. left = ra_allocref(as, ir->op1, rset_exclude(allow, right));
  35. } else if (ra_hashint(right)) {
  36. right = ra_allocref(as, ir->op2, allow);
  37. left = ra_alloc1(as, ir->op1, rset_exclude(allow, right));
  38. } else {
  39. left = ra_allocref(as, ir->op1, allow);
  40. right = ra_alloc1(as, ir->op2, rset_exclude(allow, left));
  41. }
  42. return left | (right << 8);
  43. }
  44. /* -- Guard handling ------------------------------------------------------ */
  45. /* Setup all needed exit stubs. */
  46. static void asm_exitstub_setup(ASMState *as, ExitNo nexits)
  47. {
  48. ExitNo i;
  49. MCode *mxp = as->mctop;
  50. if (mxp - (nexits + 3 + MCLIM_REDZONE) < as->mclim)
  51. asm_mclimit(as);
  52. /* 1: str lr,[sp]; bl ->vm_exit_handler; movz w0,traceno; bl <1; bl <1; ... */
  53. for (i = nexits-1; (int32_t)i >= 0; i--)
  54. *--mxp = A64I_LE(A64I_BL | A64F_S26(-3-i));
  55. *--mxp = A64I_LE(A64I_MOVZw | A64F_U16(as->T->traceno));
  56. mxp--;
  57. *mxp = A64I_LE(A64I_BL | A64F_S26(((MCode *)(void *)lj_vm_exit_handler-mxp)));
  58. *--mxp = A64I_LE(A64I_STRx | A64F_D(RID_LR) | A64F_N(RID_SP));
  59. as->mctop = mxp;
  60. }
  61. static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno)
  62. {
  63. /* Keep this in-sync with exitstub_trace_addr(). */
  64. return as->mctop + exitno + 3;
  65. }
  66. /* Emit conditional branch to exit for guard. */
  67. static void asm_guardcc(ASMState *as, A64CC cc)
  68. {
  69. MCode *target = asm_exitstub_addr(as, as->snapno);
  70. MCode *p = as->mcp;
  71. if (LJ_UNLIKELY(p == as->invmcp)) {
  72. as->loopinv = 1;
  73. *p = A64I_B | A64F_S26(target-p);
  74. emit_cond_branch(as, cc^1, p-1);
  75. return;
  76. }
  77. emit_cond_branch(as, cc, target);
  78. }
  79. /* Emit test and branch instruction to exit for guard, if in range. */
  80. static int asm_guardtnb(ASMState *as, A64Ins ai, Reg r, uint32_t bit)
  81. {
  82. MCode *target = asm_exitstub_addr(as, as->snapno);
  83. MCode *p = as->mcp;
  84. ptrdiff_t delta = target - p;
  85. if (LJ_UNLIKELY(p == as->invmcp)) {
  86. if (as->orignins > 1023) return 0; /* Delta might end up too large. */
  87. as->loopinv = 1;
  88. *p = A64I_B | A64F_S26(delta);
  89. ai ^= 0x01000000u;
  90. target = p-1;
  91. } else if (LJ_UNLIKELY(delta >= 0x1fff)) {
  92. return 0;
  93. }
  94. emit_tnb(as, ai, r, bit, target);
  95. return 1;
  96. }
  97. /* Emit compare and branch instruction to exit for guard. */
  98. static void asm_guardcnb(ASMState *as, A64Ins ai, Reg r)
  99. {
  100. MCode *target = asm_exitstub_addr(as, as->snapno);
  101. MCode *p = as->mcp;
  102. if (LJ_UNLIKELY(p == as->invmcp)) {
  103. as->loopinv = 1;
  104. *p = A64I_B | A64F_S26(target-p);
  105. emit_cnb(as, ai^0x01000000u, r, p-1);
  106. return;
  107. }
  108. emit_cnb(as, ai, r, target);
  109. }
  110. /* -- Operand fusion ------------------------------------------------------ */
  111. /* Limit linear search to this distance. Avoids O(n^2) behavior. */
  112. #define CONFLICT_SEARCH_LIM 31
  113. static int asm_isk32(ASMState *as, IRRef ref, int32_t *k)
  114. {
  115. if (irref_isk(ref)) {
  116. IRIns *ir = IR(ref);
  117. if (ir->o == IR_KNULL || !irt_is64(ir->t)) {
  118. *k = ir->i;
  119. return 1;
  120. } else if (checki32((int64_t)ir_k64(ir)->u64)) {
  121. *k = (int32_t)ir_k64(ir)->u64;
  122. return 1;
  123. }
  124. }
  125. return 0;
  126. }
  127. /* Check if there's no conflicting instruction between curins and ref. */
  128. static int noconflict(ASMState *as, IRRef ref, IROp conflict)
  129. {
  130. IRIns *ir = as->ir;
  131. IRRef i = as->curins;
  132. if (i > ref + CONFLICT_SEARCH_LIM)
  133. return 0; /* Give up, ref is too far away. */
  134. while (--i > ref)
  135. if (ir[i].o == conflict)
  136. return 0; /* Conflict found. */
  137. return 1; /* Ok, no conflict. */
  138. }
  139. /* Fuse the array base of colocated arrays. */
  140. static int32_t asm_fuseabase(ASMState *as, IRRef ref)
  141. {
  142. IRIns *ir = IR(ref);
  143. if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&
  144. !neverfuse(as) && noconflict(as, ref, IR_NEWREF))
  145. return (int32_t)sizeof(GCtab);
  146. return 0;
  147. }
  148. #define FUSE_REG 0x40000000
  149. /* Fuse array/hash/upvalue reference into register+offset operand. */
  150. static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow,
  151. A64Ins ins)
  152. {
  153. IRIns *ir = IR(ref);
  154. if (ra_noreg(ir->r)) {
  155. if (ir->o == IR_AREF) {
  156. if (mayfuse(as, ref)) {
  157. if (irref_isk(ir->op2)) {
  158. IRRef tab = IR(ir->op1)->op1;
  159. int32_t ofs = asm_fuseabase(as, tab);
  160. IRRef refa = ofs ? tab : ir->op1;
  161. ofs += 8*IR(ir->op2)->i;
  162. if (emit_checkofs(ins, ofs)) {
  163. *ofsp = ofs;
  164. return ra_alloc1(as, refa, allow);
  165. }
  166. } else {
  167. Reg base = ra_alloc1(as, ir->op1, allow);
  168. *ofsp = FUSE_REG|ra_alloc1(as, ir->op2, rset_exclude(allow, base));
  169. return base;
  170. }
  171. }
  172. } else if (ir->o == IR_HREFK) {
  173. if (mayfuse(as, ref)) {
  174. int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));
  175. if (emit_checkofs(ins, ofs)) {
  176. *ofsp = ofs;
  177. return ra_alloc1(as, ir->op1, allow);
  178. }
  179. }
  180. } else if (ir->o == IR_UREFC) {
  181. if (irref_isk(ir->op1)) {
  182. GCfunc *fn = ir_kfunc(IR(ir->op1));
  183. GCupval *uv = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv;
  184. int64_t ofs = glofs(as, &uv->tv);
  185. if (emit_checkofs(ins, ofs)) {
  186. *ofsp = (int32_t)ofs;
  187. return RID_GL;
  188. }
  189. }
  190. } else if (ir->o == IR_TMPREF) {
  191. *ofsp = (int32_t)glofs(as, &J2G(as->J)->tmptv);
  192. return RID_GL;
  193. }
  194. }
  195. *ofsp = 0;
  196. return ra_alloc1(as, ref, allow);
  197. }
  198. /* Fuse m operand into arithmetic/logic instructions. */
  199. static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
  200. {
  201. IRIns *ir = IR(ref);
  202. int logical = (ai & 0x1f000000) == 0x0a000000;
  203. if (ra_hasreg(ir->r)) {
  204. ra_noweak(as, ir->r);
  205. return A64F_M(ir->r);
  206. } else if (irref_isk(ref)) {
  207. int64_t k = get_k64val(as, ref);
  208. uint32_t m = logical ? emit_isk13(k, irt_is64(ir->t)) :
  209. emit_isk12(irt_is64(ir->t) ? k : (int32_t)k);
  210. if (m)
  211. return m;
  212. } else if (mayfuse(as, ref)) {
  213. if ((ir->o >= IR_BSHL && ir->o <= IR_BSAR && irref_isk(ir->op2)) ||
  214. (ir->o == IR_ADD && ir->op1 == ir->op2)) {
  215. A64Shift sh = ir->o == IR_BSHR ? A64SH_LSR :
  216. ir->o == IR_BSAR ? A64SH_ASR : A64SH_LSL;
  217. int shift = ir->o == IR_ADD ? 1 :
  218. (IR(ir->op2)->i & (irt_is64(ir->t) ? 63 : 31));
  219. IRIns *irl = IR(ir->op1);
  220. if (sh == A64SH_LSL &&
  221. irl->o == IR_CONV && !logical &&
  222. irl->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT) &&
  223. shift <= 4 &&
  224. canfuse(as, irl)) {
  225. Reg m = ra_alloc1(as, irl->op1, allow);
  226. return A64F_M(m) | A64F_EXSH(A64EX_SXTW, shift);
  227. } else {
  228. Reg m = ra_alloc1(as, ir->op1, allow);
  229. return A64F_M(m) | A64F_SH(sh, shift);
  230. }
  231. } else if (ir->o == IR_BROR && logical && irref_isk(ir->op2)) {
  232. Reg m = ra_alloc1(as, ir->op1, allow);
  233. int shift = (IR(ir->op2)->i & (irt_is64(ir->t) ? 63 : 31));
  234. return A64F_M(m) | A64F_SH(A64SH_ROR, shift);
  235. } else if (ir->o == IR_CONV && !logical &&
  236. ir->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT)) {
  237. Reg m = ra_alloc1(as, ir->op1, allow);
  238. return A64F_M(m) | A64F_EX(A64EX_SXTW);
  239. }
  240. }
  241. return A64F_M(ra_allocref(as, ref, allow));
  242. }
  243. /* Fuse XLOAD/XSTORE reference into load/store operand. */
  244. static void asm_fusexref(ASMState *as, A64Ins ai, Reg rd, IRRef ref,
  245. RegSet allow)
  246. {
  247. IRIns *ir = IR(ref);
  248. Reg base;
  249. int32_t ofs = 0;
  250. if (ra_noreg(ir->r) && canfuse(as, ir)) {
  251. if (ir->o == IR_ADD) {
  252. if (asm_isk32(as, ir->op2, &ofs) && emit_checkofs(ai, ofs)) {
  253. ref = ir->op1;
  254. } else {
  255. Reg rn, rm;
  256. IRRef lref = ir->op1, rref = ir->op2;
  257. IRIns *irl = IR(lref);
  258. if (mayfuse(as, irl->op1)) {
  259. unsigned int shift = 4;
  260. if (irl->o == IR_BSHL && irref_isk(irl->op2)) {
  261. shift = (IR(irl->op2)->i & 63);
  262. } else if (irl->o == IR_ADD && irl->op1 == irl->op2) {
  263. shift = 1;
  264. }
  265. if ((ai >> 30) == shift) {
  266. lref = irl->op1;
  267. irl = IR(lref);
  268. ai |= A64I_LS_SH;
  269. }
  270. }
  271. if (irl->o == IR_CONV &&
  272. irl->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT) &&
  273. canfuse(as, irl)) {
  274. lref = irl->op1;
  275. ai |= A64I_LS_SXTWx;
  276. } else {
  277. ai |= A64I_LS_LSLx;
  278. }
  279. rm = ra_alloc1(as, lref, allow);
  280. rn = ra_alloc1(as, rref, rset_exclude(allow, rm));
  281. emit_dnm(as, (ai^A64I_LS_R), (rd & 31), rn, rm);
  282. return;
  283. }
  284. } else if (ir->o == IR_STRREF) {
  285. if (asm_isk32(as, ir->op2, &ofs)) {
  286. ref = ir->op1;
  287. } else if (asm_isk32(as, ir->op1, &ofs)) {
  288. ref = ir->op2;
  289. } else {
  290. Reg refk = irref_isk(ir->op1) ? ir->op1 : ir->op2;
  291. Reg refv = irref_isk(ir->op1) ? ir->op2 : ir->op1;
  292. Reg rn = ra_alloc1(as, refv, allow);
  293. IRIns *irr = IR(refk);
  294. uint32_t m;
  295. if (irr+1 == ir && !ra_used(irr) &&
  296. irr->o == IR_ADD && irref_isk(irr->op2)) {
  297. ofs = sizeof(GCstr) + IR(irr->op2)->i;
  298. if (emit_checkofs(ai, ofs)) {
  299. Reg rm = ra_alloc1(as, irr->op1, rset_exclude(allow, rn));
  300. m = A64F_M(rm) | A64F_EX(A64EX_SXTW);
  301. goto skipopm;
  302. }
  303. }
  304. m = asm_fuseopm(as, 0, refk, rset_exclude(allow, rn));
  305. ofs = sizeof(GCstr);
  306. skipopm:
  307. emit_lso(as, ai, rd, rd, ofs);
  308. emit_dn(as, A64I_ADDx^m, rd, rn);
  309. return;
  310. }
  311. ofs += sizeof(GCstr);
  312. if (!emit_checkofs(ai, ofs)) {
  313. Reg rn = ra_alloc1(as, ref, allow);
  314. Reg rm = ra_allock(as, ofs, rset_exclude(allow, rn));
  315. emit_dnm(as, (ai^A64I_LS_R)|A64I_LS_UXTWx, rd, rn, rm);
  316. return;
  317. }
  318. }
  319. }
  320. base = ra_alloc1(as, ref, allow);
  321. emit_lso(as, ai, (rd & 31), base, ofs);
  322. }
  323. /* Fuse FP multiply-add/sub. */
  324. static int asm_fusemadd(ASMState *as, IRIns *ir, A64Ins ai, A64Ins air)
  325. {
  326. IRRef lref = ir->op1, rref = ir->op2;
  327. IRIns *irm;
  328. if ((as->flags & JIT_F_OPT_FMA) &&
  329. lref != rref &&
  330. ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&
  331. ra_noreg(irm->r)) ||
  332. (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&
  333. (rref = lref, ai = air, ra_noreg(irm->r))))) {
  334. Reg dest = ra_dest(as, ir, RSET_FPR);
  335. Reg add = ra_hintalloc(as, rref, dest, RSET_FPR);
  336. Reg left = ra_alloc2(as, irm,
  337. rset_exclude(rset_exclude(RSET_FPR, dest), add));
  338. Reg right = (left >> 8); left &= 255;
  339. emit_dnma(as, ai, (dest & 31), (left & 31), (right & 31), (add & 31));
  340. return 1;
  341. }
  342. return 0;
  343. }
  344. /* Fuse BAND + BSHL/BSHR into UBFM. */
  345. static int asm_fuseandshift(ASMState *as, IRIns *ir)
  346. {
  347. IRIns *irl = IR(ir->op1);
  348. lj_assertA(ir->o == IR_BAND, "bad usage");
  349. if (canfuse(as, irl) && irref_isk(ir->op2)) {
  350. uint64_t mask = get_k64val(as, ir->op2);
  351. if (irref_isk(irl->op2) && (irl->o == IR_BSHR || irl->o == IR_BSHL)) {
  352. int32_t shmask = irt_is64(irl->t) ? 63 : 31;
  353. int32_t shift = (IR(irl->op2)->i & shmask);
  354. int32_t imms = shift;
  355. if (irl->o == IR_BSHL) {
  356. mask >>= shift;
  357. shift = (shmask-shift+1) & shmask;
  358. imms = 0;
  359. }
  360. if (mask && !((mask+1) & mask)) { /* Contiguous 1-bits at the bottom. */
  361. Reg dest = ra_dest(as, ir, RSET_GPR);
  362. Reg left = ra_alloc1(as, irl->op1, RSET_GPR);
  363. A64Ins ai = shmask == 63 ? A64I_UBFMx : A64I_UBFMw;
  364. imms += 63 - emit_clz64(mask);
  365. if (imms > shmask) imms = shmask;
  366. emit_dn(as, ai | A64F_IMMS(imms) | A64F_IMMR(shift), dest, left);
  367. return 1;
  368. }
  369. }
  370. }
  371. return 0;
  372. }
  373. /* Fuse BOR(BSHL, BSHR) into EXTR/ROR. */
  374. static int asm_fuseorshift(ASMState *as, IRIns *ir)
  375. {
  376. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
  377. lj_assertA(ir->o == IR_BOR, "bad usage");
  378. if (canfuse(as, irl) && canfuse(as, irr) &&
  379. ((irl->o == IR_BSHR && irr->o == IR_BSHL) ||
  380. (irl->o == IR_BSHL && irr->o == IR_BSHR))) {
  381. if (irref_isk(irl->op2) && irref_isk(irr->op2)) {
  382. IRRef lref = irl->op1, rref = irr->op1;
  383. uint32_t lshift = IR(irl->op2)->i, rshift = IR(irr->op2)->i;
  384. if (irl->o == IR_BSHR) { /* BSHR needs to be the right operand. */
  385. uint32_t tmp2;
  386. IRRef tmp1 = lref; lref = rref; rref = tmp1;
  387. tmp2 = lshift; lshift = rshift; rshift = tmp2;
  388. }
  389. if (rshift + lshift == (irt_is64(ir->t) ? 64 : 32)) {
  390. A64Ins ai = irt_is64(ir->t) ? A64I_EXTRx : A64I_EXTRw;
  391. Reg dest = ra_dest(as, ir, RSET_GPR);
  392. Reg left = ra_alloc1(as, lref, RSET_GPR);
  393. Reg right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left));
  394. emit_dnm(as, ai | A64F_IMMS(rshift), dest, left, right);
  395. return 1;
  396. }
  397. }
  398. }
  399. return 0;
  400. }
  401. /* -- Calls --------------------------------------------------------------- */
  402. /* Generate a call to a C function. */
  403. static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
  404. {
  405. uint32_t n, nargs = CCI_XNARGS(ci);
  406. int32_t spofs = 0, spalign = LJ_HASFFI && LJ_TARGET_OSX ? 0 : 7;
  407. Reg gpr, fpr = REGARG_FIRSTFPR;
  408. if (ci->func)
  409. emit_call(as, ci->func);
  410. for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++)
  411. as->cost[gpr] = REGCOST(~0u, ASMREF_L);
  412. gpr = REGARG_FIRSTGPR;
  413. #if LJ_HASFFI && LJ_ABI_WIN
  414. if ((ci->flags & CCI_VARARG)) {
  415. fpr = REGARG_LASTFPR+1;
  416. }
  417. #endif
  418. for (n = 0; n < nargs; n++) { /* Setup args. */
  419. IRRef ref = args[n];
  420. IRIns *ir = IR(ref);
  421. if (ref) {
  422. if (irt_isfp(ir->t)) {
  423. if (fpr <= REGARG_LASTFPR) {
  424. lj_assertA(rset_test(as->freeset, fpr),
  425. "reg %d not free", fpr); /* Must have been evicted. */
  426. ra_leftov(as, fpr, ref);
  427. fpr++;
  428. #if LJ_HASFFI && LJ_ABI_WIN
  429. } else if ((ci->flags & CCI_VARARG) && (gpr <= REGARG_LASTGPR)) {
  430. Reg rf = ra_alloc1(as, ref, RSET_FPR);
  431. emit_dn(as, A64I_FMOV_R_D, gpr++, rf & 31);
  432. #endif
  433. } else {
  434. Reg r = ra_alloc1(as, ref, RSET_FPR);
  435. int32_t al = spalign;
  436. #if LJ_HASFFI && LJ_TARGET_OSX
  437. al |= irt_isnum(ir->t) ? 7 : 3;
  438. #endif
  439. spofs = (spofs + al) & ~al;
  440. if (LJ_BE && al >= 7 && !irt_isnum(ir->t)) spofs += 4, al -= 4;
  441. emit_spstore(as, ir, r, spofs);
  442. spofs += al + 1;
  443. }
  444. } else {
  445. if (gpr <= REGARG_LASTGPR) {
  446. lj_assertA(rset_test(as->freeset, gpr),
  447. "reg %d not free", gpr); /* Must have been evicted. */
  448. ra_leftov(as, gpr, ref);
  449. gpr++;
  450. } else {
  451. Reg r = ra_alloc1(as, ref, RSET_GPR);
  452. int32_t al = spalign;
  453. #if LJ_HASFFI && LJ_TARGET_OSX
  454. al |= irt_size(ir->t) - 1;
  455. #endif
  456. spofs = (spofs + al) & ~al;
  457. if (al >= 3) {
  458. if (LJ_BE && al >= 7 && !irt_is64(ir->t)) spofs += 4, al -= 4;
  459. emit_spstore(as, ir, r, spofs);
  460. } else {
  461. lj_assertA(al == 0 || al == 1, "size %d unexpected", al + 1);
  462. emit_lso(as, al ? A64I_STRH : A64I_STRB, r, RID_SP, spofs);
  463. }
  464. spofs += al + 1;
  465. }
  466. }
  467. #if LJ_HASFFI && LJ_TARGET_OSX
  468. } else { /* Marker for start of varargs. */
  469. gpr = REGARG_LASTGPR+1;
  470. fpr = REGARG_LASTFPR+1;
  471. spalign = 7;
  472. #endif
  473. }
  474. }
  475. }
  476. /* Setup result reg/sp for call. Evict scratch regs. */
  477. static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
  478. {
  479. RegSet drop = RSET_SCRATCH;
  480. int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t));
  481. if (ra_hasreg(ir->r))
  482. rset_clear(drop, ir->r); /* Dest reg handled below. */
  483. if (hiop && ra_hasreg((ir+1)->r))
  484. rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
  485. ra_evictset(as, drop); /* Evictions must be performed first. */
  486. if (ra_used(ir)) {
  487. lj_assertA(!irt_ispri(ir->t), "PRI dest");
  488. if (irt_isfp(ir->t)) {
  489. if (ci->flags & CCI_CASTU64) {
  490. Reg dest = ra_dest(as, ir, RSET_FPR) & 31;
  491. emit_dn(as, irt_isnum(ir->t) ? A64I_FMOV_D_R : A64I_FMOV_S_R,
  492. dest, RID_RET);
  493. } else {
  494. ra_destreg(as, ir, RID_FPRET);
  495. }
  496. } else if (hiop) {
  497. ra_destpair(as, ir);
  498. } else {
  499. ra_destreg(as, ir, RID_RET);
  500. }
  501. }
  502. UNUSED(ci);
  503. }
  504. static void asm_callx(ASMState *as, IRIns *ir)
  505. {
  506. IRRef args[CCI_NARGS_MAX*2];
  507. CCallInfo ci;
  508. IRRef func;
  509. IRIns *irf;
  510. ci.flags = asm_callx_flags(as, ir);
  511. asm_collectargs(as, ir, &ci, args);
  512. asm_setupresult(as, ir, &ci);
  513. func = ir->op2; irf = IR(func);
  514. if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
  515. if (irref_isk(func)) { /* Call to constant address. */
  516. ci.func = (ASMFunction)(ir_k64(irf)->u64);
  517. } else { /* Need a non-argument register for indirect calls. */
  518. Reg freg = ra_alloc1(as, func, RSET_RANGE(RID_X8, RID_MAX_GPR)-RSET_FIXED);
  519. emit_n(as, A64I_BLR_AUTH, freg);
  520. ci.func = (ASMFunction)(void *)0;
  521. }
  522. asm_gencall(as, &ci, args);
  523. }
  524. /* -- Returns ------------------------------------------------------------- */
  525. /* Return to lower frame. Guard that it goes to the right spot. */
  526. static void asm_retf(ASMState *as, IRIns *ir)
  527. {
  528. Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
  529. void *pc = ir_kptr(IR(ir->op2));
  530. int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));
  531. as->topslot -= (BCReg)delta;
  532. if ((int32_t)as->topslot < 0) as->topslot = 0;
  533. irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */
  534. emit_setgl(as, base, jit_base);
  535. emit_addptr(as, base, -8*delta);
  536. asm_guardcc(as, CC_NE);
  537. emit_nm(as, A64I_CMPx, RID_TMP,
  538. ra_allock(as, i64ptr(pc), rset_exclude(RSET_GPR, base)));
  539. emit_lso(as, A64I_LDRx, RID_TMP, base, -8);
  540. }
  541. /* -- Buffer operations --------------------------------------------------- */
  542. #if LJ_HASBUFFER
  543. static void asm_bufhdr_write(ASMState *as, Reg sb)
  544. {
  545. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb));
  546. IRIns irgc;
  547. irgc.ot = IRT(0, IRT_PGC); /* GC type. */
  548. emit_storeofs(as, &irgc, RID_TMP, sb, offsetof(SBuf, L));
  549. emit_dn(as, A64I_BFMx | A64F_IMMS(lj_fls(SBUF_MASK_FLAG)) | A64F_IMMR(0), RID_TMP, tmp);
  550. emit_getgl(as, RID_TMP, cur_L);
  551. emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L));
  552. }
  553. #endif
  554. /* -- Type conversions ---------------------------------------------------- */
  555. static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
  556. {
  557. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
  558. Reg dest = ra_dest(as, ir, RSET_GPR);
  559. asm_guardcc(as, CC_NE);
  560. emit_nm(as, A64I_FCMPd, (tmp & 31), (left & 31));
  561. emit_dn(as, A64I_FCVT_F64_S32, (tmp & 31), dest);
  562. emit_dn(as, A64I_FCVT_S32_F64, dest, (left & 31));
  563. }
  564. static void asm_tobit(ASMState *as, IRIns *ir)
  565. {
  566. RegSet allow = RSET_FPR;
  567. Reg left = ra_alloc1(as, ir->op1, allow);
  568. Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));
  569. Reg tmp = ra_scratch(as, rset_clear(allow, right));
  570. Reg dest = ra_dest(as, ir, RSET_GPR);
  571. emit_dn(as, A64I_FMOV_R_S, dest, (tmp & 31));
  572. emit_dnm(as, A64I_FADDd, (tmp & 31), (left & 31), (right & 31));
  573. }
  574. static void asm_conv(ASMState *as, IRIns *ir)
  575. {
  576. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
  577. int st64 = (st == IRT_I64 || st == IRT_U64 || st == IRT_P64);
  578. int stfp = (st == IRT_NUM || st == IRT_FLOAT);
  579. IRRef lref = ir->op1;
  580. lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV");
  581. if (irt_isfp(ir->t)) {
  582. Reg dest = ra_dest(as, ir, RSET_FPR);
  583. if (stfp) { /* FP to FP conversion. */
  584. emit_dn(as, st == IRT_NUM ? A64I_FCVT_F32_F64 : A64I_FCVT_F64_F32,
  585. (dest & 31), (ra_alloc1(as, lref, RSET_FPR) & 31));
  586. } else { /* Integer to FP conversion. */
  587. Reg left = ra_alloc1(as, lref, RSET_GPR);
  588. A64Ins ai = irt_isfloat(ir->t) ?
  589. (((IRT_IS64 >> st) & 1) ?
  590. (st == IRT_I64 ? A64I_FCVT_F32_S64 : A64I_FCVT_F32_U64) :
  591. (st == IRT_INT ? A64I_FCVT_F32_S32 : A64I_FCVT_F32_U32)) :
  592. (((IRT_IS64 >> st) & 1) ?
  593. (st == IRT_I64 ? A64I_FCVT_F64_S64 : A64I_FCVT_F64_U64) :
  594. (st == IRT_INT ? A64I_FCVT_F64_S32 : A64I_FCVT_F64_U32));
  595. emit_dn(as, ai, (dest & 31), left);
  596. }
  597. } else if (stfp) { /* FP to integer conversion. */
  598. if (irt_isguard(ir->t)) {
  599. /* Checked conversions are only supported from number to int. */
  600. lj_assertA(irt_isint(ir->t) && st == IRT_NUM,
  601. "bad type for checked CONV");
  602. asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
  603. } else {
  604. Reg left = ra_alloc1(as, lref, RSET_FPR);
  605. Reg dest = ra_dest(as, ir, RSET_GPR);
  606. A64Ins ai = irt_is64(ir->t) ?
  607. (st == IRT_NUM ?
  608. (irt_isi64(ir->t) ? A64I_FCVT_S64_F64 : A64I_FCVT_U64_F64) :
  609. (irt_isi64(ir->t) ? A64I_FCVT_S64_F32 : A64I_FCVT_U64_F32)) :
  610. (st == IRT_NUM ?
  611. (irt_isint(ir->t) ? A64I_FCVT_S32_F64 : A64I_FCVT_U32_F64) :
  612. (irt_isint(ir->t) ? A64I_FCVT_S32_F32 : A64I_FCVT_U32_F32));
  613. emit_dn(as, ai, dest, (left & 31));
  614. }
  615. } else if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
  616. Reg dest = ra_dest(as, ir, RSET_GPR);
  617. Reg left = ra_alloc1(as, lref, RSET_GPR);
  618. A64Ins ai = st == IRT_I8 ? A64I_SXTBw :
  619. st == IRT_U8 ? A64I_UXTBw :
  620. st == IRT_I16 ? A64I_SXTHw : A64I_UXTHw;
  621. lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT");
  622. emit_dn(as, ai, dest, left);
  623. } else {
  624. Reg dest = ra_dest(as, ir, RSET_GPR);
  625. if (irt_is64(ir->t)) {
  626. if (st64 || !(ir->op2 & IRCONV_SEXT)) {
  627. /* 64/64 bit no-op (cast) or 32 to 64 bit zero extension. */
  628. ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
  629. } else { /* 32 to 64 bit sign extension. */
  630. Reg left = ra_alloc1(as, lref, RSET_GPR);
  631. emit_dn(as, A64I_SXTW, dest, left);
  632. }
  633. } else {
  634. if (st64 && !(ir->op2 & IRCONV_NONE)) {
  635. /* This is either a 32 bit reg/reg mov which zeroes the hiword
  636. ** or a load of the loword from a 64 bit address.
  637. */
  638. Reg left = ra_alloc1(as, lref, RSET_GPR);
  639. emit_dm(as, A64I_MOVw, dest, left);
  640. } else { /* 32/32 bit no-op (cast). */
  641. ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
  642. }
  643. }
  644. }
  645. }
  646. static void asm_strto(ASMState *as, IRIns *ir)
  647. {
  648. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];
  649. IRRef args[2];
  650. Reg tmp;
  651. int32_t ofs = 0;
  652. ra_evictset(as, RSET_SCRATCH);
  653. if (ra_used(ir)) {
  654. if (ra_hasspill(ir->s)) {
  655. ofs = sps_scale(ir->s);
  656. if (ra_hasreg(ir->r)) {
  657. ra_free(as, ir->r);
  658. ra_modified(as, ir->r);
  659. emit_spload(as, ir, ir->r, ofs);
  660. }
  661. } else {
  662. Reg dest = ra_dest(as, ir, RSET_FPR);
  663. emit_lso(as, A64I_LDRd, (dest & 31), RID_SP, 0);
  664. }
  665. }
  666. asm_guardcnb(as, A64I_CBZ, RID_RET);
  667. args[0] = ir->op1; /* GCstr *str */
  668. args[1] = ASMREF_TMP1; /* TValue *n */
  669. asm_gencall(as, ci, args);
  670. tmp = ra_releasetmp(as, ASMREF_TMP1);
  671. emit_opk(as, A64I_ADDx, tmp, RID_SP, ofs, RSET_GPR);
  672. }
  673. /* -- Memory references --------------------------------------------------- */
  674. /* Store tagged value for ref at base+ofs. */
  675. static void asm_tvstore64(ASMState *as, Reg base, int32_t ofs, IRRef ref)
  676. {
  677. RegSet allow = rset_exclude(RSET_GPR, base);
  678. IRIns *ir = IR(ref);
  679. lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t),
  680. "store of IR type %d", irt_type(ir->t));
  681. if (irref_isk(ref)) {
  682. TValue k;
  683. lj_ir_kvalue(as->J->L, &k, ir);
  684. emit_lso(as, A64I_STRx, ra_allock(as, k.u64, allow), base, ofs);
  685. } else {
  686. Reg src = ra_alloc1(as, ref, allow);
  687. rset_clear(allow, src);
  688. if (irt_isinteger(ir->t)) {
  689. Reg type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47, allow);
  690. emit_lso(as, A64I_STRx, RID_TMP, base, ofs);
  691. emit_dnm(as, A64I_ADDx | A64F_EX(A64EX_UXTW), RID_TMP, type, src);
  692. } else {
  693. Reg type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
  694. emit_lso(as, A64I_STRx, RID_TMP, base, ofs);
  695. emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 47), RID_TMP, src, type);
  696. }
  697. }
  698. }
  699. /* Get pointer to TValue. */
  700. static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode)
  701. {
  702. if ((mode & IRTMPREF_IN1)) {
  703. IRIns *ir = IR(ref);
  704. if (irt_isnum(ir->t)) {
  705. if (irref_isk(ref) && !(mode & IRTMPREF_OUT1)) {
  706. /* Use the number constant itself as a TValue. */
  707. ra_allockreg(as, i64ptr(ir_knum(ir)), dest);
  708. return;
  709. }
  710. emit_lso(as, A64I_STRd, (ra_alloc1(as, ref, RSET_FPR) & 31), dest, 0);
  711. } else {
  712. asm_tvstore64(as, dest, 0, ref);
  713. }
  714. }
  715. /* g->tmptv holds the TValue(s). */
  716. emit_dn(as, A64I_ADDx^emit_isk12(glofs(as, &J2G(as->J)->tmptv)), dest, RID_GL);
  717. }
  718. static void asm_aref(ASMState *as, IRIns *ir)
  719. {
  720. Reg dest = ra_dest(as, ir, RSET_GPR);
  721. Reg idx, base;
  722. if (irref_isk(ir->op2)) {
  723. IRRef tab = IR(ir->op1)->op1;
  724. int32_t ofs = asm_fuseabase(as, tab);
  725. IRRef refa = ofs ? tab : ir->op1;
  726. uint32_t k = emit_isk12(ofs + 8*IR(ir->op2)->i);
  727. if (k) {
  728. base = ra_alloc1(as, refa, RSET_GPR);
  729. emit_dn(as, A64I_ADDx^k, dest, base);
  730. return;
  731. }
  732. }
  733. base = ra_alloc1(as, ir->op1, RSET_GPR);
  734. idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
  735. emit_dnm(as, A64I_ADDx | A64F_EXSH(A64EX_UXTW, 3), dest, base, idx);
  736. }
  737. /* Inlined hash lookup. Specialized for key type and for const keys.
  738. ** The equivalent C code is:
  739. ** Node *n = hashkey(t, key);
  740. ** do {
  741. ** if (lj_obj_equal(&n->key, key)) return &n->val;
  742. ** } while ((n = nextnode(n)));
  743. ** return niltv(L);
  744. */
  745. static void asm_href(ASMState *as, IRIns *ir, IROp merge)
  746. {
  747. RegSet allow = RSET_GPR;
  748. int destused = ra_used(ir);
  749. Reg dest = ra_dest(as, ir, allow);
  750. Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
  751. Reg tmp = RID_TMP, type = RID_NONE, key, tkey;
  752. IRRef refkey = ir->op2;
  753. IRIns *irkey = IR(refkey);
  754. int isk = irref_isk(refkey);
  755. IRType1 kt = irkey->t;
  756. uint32_t k = 0;
  757. uint32_t khash;
  758. MCLabel l_end, l_loop;
  759. rset_clear(allow, tab);
  760. /* Allocate register for tkey outside of the loop. */
  761. if (isk) {
  762. int64_t kk;
  763. if (irt_isaddr(kt)) {
  764. kk = ((int64_t)irt_toitype(kt) << 47) | irkey[1].tv.u64;
  765. } else if (irt_isnum(kt)) {
  766. kk = (int64_t)ir_knum(irkey)->u64;
  767. /* Assumes -0.0 is already canonicalized to +0.0. */
  768. } else {
  769. lj_assertA(irt_ispri(kt) && !irt_isnil(kt), "bad HREF key type");
  770. kk = ~((int64_t)~irt_toitype(kt) << 47);
  771. }
  772. k = emit_isk12(kk);
  773. tkey = k ? 0 : ra_allock(as, kk, allow);
  774. } else {
  775. tkey = ra_scratch(as, allow);
  776. }
  777. /* Key not found in chain: jump to exit (if merged) or load niltv. */
  778. l_end = emit_label(as);
  779. as->invmcp = NULL;
  780. if (merge == IR_NE) {
  781. asm_guardcc(as, CC_AL);
  782. } else if (destused) {
  783. uint32_t k12 = emit_isk12(offsetof(global_State, nilnode.val));
  784. lj_assertA(k12 != 0, "Cannot k12 encode niltv(L)");
  785. emit_dn(as, A64I_ADDx^k12, dest, RID_GL);
  786. }
  787. /* Follow hash chain until the end. */
  788. l_loop = --as->mcp;
  789. if (destused)
  790. emit_lso(as, A64I_LDRx, dest, dest, offsetof(Node, next));
  791. /* Type and value comparison. */
  792. if (merge == IR_EQ)
  793. asm_guardcc(as, CC_EQ);
  794. else
  795. emit_cond_branch(as, CC_EQ, l_end);
  796. emit_nm(as, A64I_CMPx^k, tmp, tkey);
  797. if (!destused)
  798. emit_lso(as, A64I_LDRx, dest, dest, offsetof(Node, next));
  799. emit_lso(as, A64I_LDRx, tmp, dest, offsetof(Node, key));
  800. *l_loop = A64I_X | A64I_CBNZ | A64F_S19(as->mcp - l_loop) | dest;
  801. /* Construct tkey as canonicalized or tagged key. */
  802. if (!isk) {
  803. if (irt_isnum(kt)) {
  804. key = ra_alloc1(as, refkey, RSET_FPR);
  805. emit_dnm(as, A64I_CSELx | A64F_CC(CC_EQ), tkey, RID_ZERO, tkey);
  806. /* A64I_FMOV_R_D from key to tkey done below. */
  807. } else {
  808. lj_assertA(irt_isaddr(kt), "bad HREF key type");
  809. key = ra_alloc1(as, refkey, allow);
  810. type = ra_allock(as, irt_toitype(kt) << 15, rset_clear(allow, key));
  811. emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 32), tkey, key, type);
  812. }
  813. }
  814. /* Load main position relative to tab->node into dest. */
  815. khash = isk ? ir_khash(as, irkey) : 1;
  816. if (khash == 0) {
  817. emit_lso(as, A64I_LDRx, dest, tab, offsetof(GCtab, node));
  818. } else {
  819. emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 3), dest, tmp, dest);
  820. emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 1), dest, dest, dest);
  821. emit_lso(as, A64I_LDRx, tmp, tab, offsetof(GCtab, node));
  822. if (isk) {
  823. Reg tmphash = ra_allock(as, khash, allow);
  824. emit_dnm(as, A64I_ANDw, dest, dest, tmphash);
  825. emit_lso(as, A64I_LDRw, dest, tab, offsetof(GCtab, hmask));
  826. } else if (irt_isstr(kt)) {
  827. emit_dnm(as, A64I_ANDw, dest, dest, tmp);
  828. emit_lso(as, A64I_LDRw, tmp, key, offsetof(GCstr, sid));
  829. emit_lso(as, A64I_LDRw, dest, tab, offsetof(GCtab, hmask));
  830. } else { /* Must match with hash*() in lj_tab.c. */
  831. emit_dnm(as, A64I_ANDw, dest, dest, tmp);
  832. emit_lso(as, A64I_LDRw, tmp, tab, offsetof(GCtab, hmask));
  833. emit_dnm(as, A64I_SUBw, dest, dest, tmp);
  834. emit_dnm(as, A64I_EXTRw | (A64F_IMMS(32-HASH_ROT3)), tmp, tmp, tmp);
  835. emit_dnm(as, A64I_EORw | A64F_SH(A64SH_ROR, 32-HASH_ROT2), dest, tmp, dest);
  836. emit_dnm(as, A64I_SUBw, tmp, tmp, dest);
  837. emit_dnm(as, A64I_EXTRw | (A64F_IMMS(32-HASH_ROT1)), dest, dest, dest);
  838. if (irt_isnum(kt)) {
  839. emit_dnm(as, A64I_EORw, tmp, tkey, dest);
  840. emit_dnm(as, A64I_ADDw, dest, dest, dest);
  841. emit_dn(as, A64I_LSRx | A64F_IMMR(32)|A64F_IMMS(32), dest, tkey);
  842. emit_nm(as, A64I_FCMPZd, (key & 31), 0);
  843. emit_dn(as, A64I_FMOV_R_D, tkey, (key & 31));
  844. } else {
  845. emit_dnm(as, A64I_EORw, tmp, key, dest);
  846. emit_dnm(as, A64I_EORx | A64F_SH(A64SH_LSR, 32), dest, type, key);
  847. }
  848. }
  849. }
  850. }
  851. static void asm_hrefk(ASMState *as, IRIns *ir)
  852. {
  853. IRIns *kslot = IR(ir->op2);
  854. IRIns *irkey = IR(kslot->op1);
  855. int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
  856. int32_t kofs = ofs + (int32_t)offsetof(Node, key);
  857. int bigofs = !emit_checkofs(A64I_LDRx, kofs);
  858. Reg dest = (ra_used(ir) || bigofs) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
  859. Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
  860. Reg idx = node;
  861. RegSet allow = rset_exclude(RSET_GPR, node);
  862. uint64_t k;
  863. lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot");
  864. if (bigofs) {
  865. idx = dest;
  866. rset_clear(allow, dest);
  867. kofs = (int32_t)offsetof(Node, key);
  868. } else if (ra_hasreg(dest)) {
  869. emit_opk(as, A64I_ADDx, dest, node, ofs, allow);
  870. }
  871. asm_guardcc(as, CC_NE);
  872. if (irt_ispri(irkey->t)) {
  873. k = ~((int64_t)~irt_toitype(irkey->t) << 47);
  874. } else if (irt_isnum(irkey->t)) {
  875. k = ir_knum(irkey)->u64;
  876. } else {
  877. k = ((uint64_t)irt_toitype(irkey->t) << 47) | (uint64_t)ir_kgc(irkey);
  878. }
  879. emit_nm(as, A64I_CMPx, RID_TMP, ra_allock(as, k, allow));
  880. emit_lso(as, A64I_LDRx, RID_TMP, idx, kofs);
  881. if (bigofs)
  882. emit_opk(as, A64I_ADDx, dest, node, ofs, rset_exclude(RSET_GPR, node));
  883. }
  884. static void asm_uref(ASMState *as, IRIns *ir)
  885. {
  886. Reg dest = ra_dest(as, ir, RSET_GPR);
  887. int guarded = (irt_t(ir->t) & (IRT_GUARD|IRT_TYPE)) == (IRT_GUARD|IRT_PGC);
  888. if (irref_isk(ir->op1) && !guarded) {
  889. GCfunc *fn = ir_kfunc(IR(ir->op1));
  890. MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
  891. emit_lsptr(as, A64I_LDRx, dest, v);
  892. } else {
  893. if (guarded)
  894. asm_guardcnb(as, ir->o == IR_UREFC ? A64I_CBZ : A64I_CBNZ, RID_TMP);
  895. if (ir->o == IR_UREFC)
  896. emit_opk(as, A64I_ADDx, dest, dest,
  897. (int32_t)offsetof(GCupval, tv), RSET_GPR);
  898. else
  899. emit_lso(as, A64I_LDRx, dest, dest, (int32_t)offsetof(GCupval, v));
  900. if (guarded)
  901. emit_lso(as, A64I_LDRB, RID_TMP, dest,
  902. (int32_t)offsetof(GCupval, closed));
  903. if (irref_isk(ir->op1)) {
  904. GCfunc *fn = ir_kfunc(IR(ir->op1));
  905. uint64_t k = gcrefu(fn->l.uvptr[(ir->op2 >> 8)]);
  906. emit_loadu64(as, dest, k);
  907. } else {
  908. emit_lso(as, A64I_LDRx, dest, ra_alloc1(as, ir->op1, RSET_GPR),
  909. (int32_t)offsetof(GCfuncL, uvptr) + 8*(int32_t)(ir->op2 >> 8));
  910. }
  911. }
  912. }
  913. static void asm_fref(ASMState *as, IRIns *ir)
  914. {
  915. UNUSED(as); UNUSED(ir);
  916. lj_assertA(!ra_used(ir), "unfused FREF");
  917. }
  918. static void asm_strref(ASMState *as, IRIns *ir)
  919. {
  920. RegSet allow = RSET_GPR;
  921. Reg dest = ra_dest(as, ir, allow);
  922. Reg base = ra_alloc1(as, ir->op1, allow);
  923. IRIns *irr = IR(ir->op2);
  924. int32_t ofs = sizeof(GCstr);
  925. uint32_t m;
  926. rset_clear(allow, base);
  927. if (irref_isk(ir->op2) && (m = emit_isk12(ofs + irr->i))) {
  928. emit_dn(as, A64I_ADDx^m, dest, base);
  929. } else {
  930. emit_dn(as, (A64I_ADDx^A64I_K12) | A64F_U12(ofs), dest, dest);
  931. emit_dnm(as, A64I_ADDx, dest, base, ra_alloc1(as, ir->op2, allow));
  932. }
  933. }
  934. /* -- Loads and stores ---------------------------------------------------- */
  935. static A64Ins asm_fxloadins(IRIns *ir)
  936. {
  937. switch (irt_type(ir->t)) {
  938. case IRT_I8: return A64I_LDRB ^ A64I_LS_S;
  939. case IRT_U8: return A64I_LDRB;
  940. case IRT_I16: return A64I_LDRH ^ A64I_LS_S;
  941. case IRT_U16: return A64I_LDRH;
  942. case IRT_NUM: return A64I_LDRd;
  943. case IRT_FLOAT: return A64I_LDRs;
  944. default: return irt_is64(ir->t) ? A64I_LDRx : A64I_LDRw;
  945. }
  946. }
  947. static A64Ins asm_fxstoreins(IRIns *ir)
  948. {
  949. switch (irt_type(ir->t)) {
  950. case IRT_I8: case IRT_U8: return A64I_STRB;
  951. case IRT_I16: case IRT_U16: return A64I_STRH;
  952. case IRT_NUM: return A64I_STRd;
  953. case IRT_FLOAT: return A64I_STRs;
  954. default: return irt_is64(ir->t) ? A64I_STRx : A64I_STRw;
  955. }
  956. }
  957. static void asm_fload(ASMState *as, IRIns *ir)
  958. {
  959. Reg dest = ra_dest(as, ir, RSET_GPR);
  960. Reg idx;
  961. A64Ins ai = asm_fxloadins(ir);
  962. int32_t ofs;
  963. if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */
  964. idx = RID_GL;
  965. ofs = (ir->op2 << 2) - GG_OFS(g);
  966. } else {
  967. idx = ra_alloc1(as, ir->op1, RSET_GPR);
  968. if (ir->op2 == IRFL_TAB_ARRAY) {
  969. ofs = asm_fuseabase(as, ir->op1);
  970. if (ofs) { /* Turn the t->array load into an add for colocated arrays. */
  971. emit_dn(as, (A64I_ADDx^A64I_K12) | A64F_U12(ofs), dest, idx);
  972. return;
  973. }
  974. }
  975. ofs = field_ofs[ir->op2];
  976. }
  977. emit_lso(as, ai, (dest & 31), idx, ofs);
  978. }
  979. static void asm_fstore(ASMState *as, IRIns *ir)
  980. {
  981. if (ir->r != RID_SINK) {
  982. Reg src = ra_alloc1(as, ir->op2, RSET_GPR);
  983. IRIns *irf = IR(ir->op1);
  984. Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));
  985. int32_t ofs = field_ofs[irf->op2];
  986. emit_lso(as, asm_fxstoreins(ir), (src & 31), idx, ofs);
  987. }
  988. }
  989. static void asm_xload(ASMState *as, IRIns *ir)
  990. {
  991. Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
  992. lj_assertA(!(ir->op2 & IRXLOAD_UNALIGNED), "unaligned XLOAD");
  993. asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR);
  994. }
  995. static void asm_xstore(ASMState *as, IRIns *ir)
  996. {
  997. if (ir->r != RID_SINK) {
  998. Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
  999. asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,
  1000. rset_exclude(RSET_GPR, src));
  1001. }
  1002. }
  1003. static void asm_ahuvload(ASMState *as, IRIns *ir)
  1004. {
  1005. Reg idx, tmp;
  1006. int32_t ofs = 0;
  1007. RegSet gpr = RSET_GPR, allow = irt_isnum(ir->t) ? RSET_FPR : RSET_GPR;
  1008. lj_assertA(irt_isnum(ir->t) || irt_ispri(ir->t) || irt_isaddr(ir->t) ||
  1009. irt_isint(ir->t),
  1010. "bad load type %d", irt_type(ir->t));
  1011. if (ra_used(ir)) {
  1012. Reg dest = ra_dest(as, ir, allow);
  1013. tmp = irt_isnum(ir->t) ? ra_scratch(as, rset_clear(gpr, dest)) : dest;
  1014. if (irt_isaddr(ir->t)) {
  1015. emit_dn(as, A64I_ANDx^emit_isk13(LJ_GCVMASK, 1), dest, dest);
  1016. } else if (irt_isnum(ir->t)) {
  1017. emit_dn(as, A64I_FMOV_D_R, (dest & 31), tmp);
  1018. } else if (irt_isint(ir->t)) {
  1019. emit_dm(as, A64I_MOVw, dest, dest);
  1020. }
  1021. } else {
  1022. tmp = ra_scratch(as, gpr);
  1023. }
  1024. idx = asm_fuseahuref(as, ir->op1, &ofs, rset_clear(gpr, tmp), A64I_LDRx);
  1025. rset_clear(gpr, idx);
  1026. if (ofs & FUSE_REG) rset_clear(gpr, ofs & 31);
  1027. if (ir->o == IR_VLOAD) ofs += 8 * ir->op2;
  1028. /* Always do the type check, even if the load result is unused. */
  1029. asm_guardcc(as, irt_isnum(ir->t) ? CC_LS : CC_NE);
  1030. if (irt_type(ir->t) >= IRT_NUM) {
  1031. lj_assertA(irt_isinteger(ir->t) || irt_isnum(ir->t),
  1032. "bad load type %d", irt_type(ir->t));
  1033. emit_nm(as, A64I_CMPx | A64F_SH(A64SH_LSR, 32),
  1034. ra_allock(as, LJ_TISNUM << 15, gpr), tmp);
  1035. } else if (irt_isaddr(ir->t)) {
  1036. emit_n(as, (A64I_CMNx^A64I_K12) | A64F_U12(-irt_toitype(ir->t)), RID_TMP);
  1037. emit_dn(as, A64I_ASRx | A64F_IMMR(47), RID_TMP, tmp);
  1038. } else if (irt_isnil(ir->t)) {
  1039. emit_n(as, (A64I_CMNx^A64I_K12) | A64F_U12(1), tmp);
  1040. } else {
  1041. emit_nm(as, A64I_CMPx | A64F_SH(A64SH_LSR, 32),
  1042. ra_allock(as, (irt_toitype(ir->t) << 15) | 0x7fff, gpr), tmp);
  1043. }
  1044. if (ofs & FUSE_REG)
  1045. emit_dnm(as, (A64I_LDRx^A64I_LS_R)|A64I_LS_UXTWx|A64I_LS_SH, tmp, idx, (ofs & 31));
  1046. else
  1047. emit_lso(as, A64I_LDRx, tmp, idx, ofs);
  1048. }
  1049. static void asm_ahustore(ASMState *as, IRIns *ir)
  1050. {
  1051. if (ir->r != RID_SINK) {
  1052. RegSet allow = RSET_GPR;
  1053. Reg idx, src = RID_NONE, tmp = RID_TMP, type = RID_NONE;
  1054. int32_t ofs = 0;
  1055. if (irt_isnum(ir->t)) {
  1056. src = ra_alloc1(as, ir->op2, RSET_FPR);
  1057. idx = asm_fuseahuref(as, ir->op1, &ofs, allow, A64I_STRd);
  1058. if (ofs & FUSE_REG)
  1059. emit_dnm(as, (A64I_STRd^A64I_LS_R)|A64I_LS_UXTWx|A64I_LS_SH, (src & 31), idx, (ofs &31));
  1060. else
  1061. emit_lso(as, A64I_STRd, (src & 31), idx, ofs);
  1062. } else {
  1063. if (!irt_ispri(ir->t)) {
  1064. src = ra_alloc1(as, ir->op2, allow);
  1065. rset_clear(allow, src);
  1066. if (irt_isinteger(ir->t))
  1067. type = ra_allock(as, (uint64_t)(int32_t)LJ_TISNUM << 47, allow);
  1068. else
  1069. type = ra_allock(as, irt_toitype(ir->t), allow);
  1070. } else {
  1071. tmp = type = ra_allock(as, ~((int64_t)~irt_toitype(ir->t)<<47), allow);
  1072. }
  1073. idx = asm_fuseahuref(as, ir->op1, &ofs, rset_exclude(allow, type),
  1074. A64I_STRx);
  1075. if (ofs & FUSE_REG)
  1076. emit_dnm(as, (A64I_STRx^A64I_LS_R)|A64I_LS_UXTWx|A64I_LS_SH, tmp, idx, (ofs & 31));
  1077. else
  1078. emit_lso(as, A64I_STRx, tmp, idx, ofs);
  1079. if (ra_hasreg(src)) {
  1080. if (irt_isinteger(ir->t)) {
  1081. emit_dnm(as, A64I_ADDx | A64F_EX(A64EX_UXTW), tmp, type, src);
  1082. } else {
  1083. emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 47), tmp, src, type);
  1084. }
  1085. }
  1086. }
  1087. }
  1088. }
  1089. static void asm_sload(ASMState *as, IRIns *ir)
  1090. {
  1091. int32_t ofs = 8*((int32_t)ir->op1-2);
  1092. IRType1 t = ir->t;
  1093. Reg dest = RID_NONE, base;
  1094. RegSet allow = RSET_GPR;
  1095. lj_assertA(!(ir->op2 & IRSLOAD_PARENT),
  1096. "bad parent SLOAD"); /* Handled by asm_head_side(). */
  1097. lj_assertA(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK),
  1098. "inconsistent SLOAD variant");
  1099. if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {
  1100. dest = ra_scratch(as, RSET_FPR);
  1101. asm_tointg(as, ir, dest);
  1102. t.irt = IRT_NUM; /* Continue with a regular number type check. */
  1103. } else if (ra_used(ir)) {
  1104. Reg tmp = RID_NONE;
  1105. if ((ir->op2 & IRSLOAD_CONVERT))
  1106. tmp = ra_scratch(as, irt_isint(t) ? RSET_FPR : RSET_GPR);
  1107. lj_assertA((irt_isnum(t)) || irt_isint(t) || irt_isaddr(t),
  1108. "bad SLOAD type %d", irt_type(t));
  1109. dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : allow);
  1110. base = ra_alloc1(as, REF_BASE, rset_clear(allow, dest));
  1111. if (irt_isaddr(t)) {
  1112. emit_dn(as, A64I_ANDx^emit_isk13(LJ_GCVMASK, 1), dest, dest);
  1113. } else if ((ir->op2 & IRSLOAD_CONVERT)) {
  1114. if (irt_isint(t)) {
  1115. emit_dn(as, A64I_FCVT_S32_F64, dest, (tmp & 31));
  1116. /* If value is already loaded for type check, move it to FPR. */
  1117. if ((ir->op2 & IRSLOAD_TYPECHECK))
  1118. emit_dn(as, A64I_FMOV_D_R, (tmp & 31), dest);
  1119. else
  1120. dest = tmp;
  1121. t.irt = IRT_NUM; /* Check for original type. */
  1122. } else {
  1123. emit_dn(as, A64I_FCVT_F64_S32, (dest & 31), tmp);
  1124. dest = tmp;
  1125. t.irt = IRT_INT; /* Check for original type. */
  1126. }
  1127. } else if (irt_isint(t) && (ir->op2 & IRSLOAD_TYPECHECK)) {
  1128. emit_dm(as, A64I_MOVw, dest, dest);
  1129. }
  1130. goto dotypecheck;
  1131. }
  1132. base = ra_alloc1(as, REF_BASE, allow);
  1133. dotypecheck:
  1134. rset_clear(allow, base);
  1135. if ((ir->op2 & IRSLOAD_TYPECHECK)) {
  1136. Reg tmp;
  1137. if (ra_hasreg(dest) && rset_test(RSET_GPR, dest)) {
  1138. tmp = dest;
  1139. } else {
  1140. tmp = ra_scratch(as, allow);
  1141. rset_clear(allow, tmp);
  1142. }
  1143. if (ra_hasreg(dest) && tmp != dest)
  1144. emit_dn(as, A64I_FMOV_D_R, (dest & 31), tmp);
  1145. /* Need type check, even if the load result is unused. */
  1146. asm_guardcc(as, irt_isnum(t) ? CC_LS : CC_NE);
  1147. if (irt_type(t) >= IRT_NUM) {
  1148. lj_assertA(irt_isinteger(t) || irt_isnum(t),
  1149. "bad SLOAD type %d", irt_type(t));
  1150. emit_nm(as, A64I_CMPx | A64F_SH(A64SH_LSR, 32),
  1151. ra_allock(as, (ir->op2 & IRSLOAD_KEYINDEX) ? LJ_KEYINDEX : (LJ_TISNUM << 15), allow), tmp);
  1152. } else if (irt_isnil(t)) {
  1153. emit_n(as, (A64I_CMNx^A64I_K12) | A64F_U12(1), tmp);
  1154. } else if (irt_ispri(t)) {
  1155. emit_nm(as, A64I_CMPx,
  1156. ra_allock(as, ~((int64_t)~irt_toitype(t) << 47) , allow), tmp);
  1157. } else {
  1158. emit_n(as, (A64I_CMNx^A64I_K12) | A64F_U12(-irt_toitype(t)), RID_TMP);
  1159. emit_dn(as, A64I_ASRx | A64F_IMMR(47), RID_TMP, tmp);
  1160. }
  1161. emit_lso(as, A64I_LDRx, tmp, base, ofs);
  1162. return;
  1163. }
  1164. if (ra_hasreg(dest)) {
  1165. emit_lso(as, irt_isnum(t) ? A64I_LDRd :
  1166. (irt_isint(t) ? A64I_LDRw : A64I_LDRx), (dest & 31), base,
  1167. ofs ^ ((LJ_BE && irt_isint(t) ? 4 : 0)));
  1168. }
  1169. }
  1170. /* -- Allocations --------------------------------------------------------- */
  1171. #if LJ_HASFFI
  1172. static void asm_cnew(ASMState *as, IRIns *ir)
  1173. {
  1174. CTState *cts = ctype_ctsG(J2G(as->J));
  1175. CTypeID id = (CTypeID)IR(ir->op1)->i;
  1176. CTSize sz;
  1177. CTInfo info = lj_ctype_info(cts, id, &sz);
  1178. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
  1179. IRRef args[4];
  1180. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
  1181. lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL),
  1182. "bad CNEW/CNEWI operands");
  1183. as->gcsteps++;
  1184. asm_setupresult(as, ir, ci); /* GCcdata * */
  1185. /* Initialize immutable cdata object. */
  1186. if (ir->o == IR_CNEWI) {
  1187. int32_t ofs = sizeof(GCcdata);
  1188. Reg r = ra_alloc1(as, ir->op2, allow);
  1189. lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz);
  1190. emit_lso(as, sz == 8 ? A64I_STRx : A64I_STRw, r, RID_RET, ofs);
  1191. } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */
  1192. ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];
  1193. args[0] = ASMREF_L; /* lua_State *L */
  1194. args[1] = ir->op1; /* CTypeID id */
  1195. args[2] = ir->op2; /* CTSize sz */
  1196. args[3] = ASMREF_TMP1; /* CTSize align */
  1197. asm_gencall(as, ci, args);
  1198. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));
  1199. return;
  1200. }
  1201. /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */
  1202. {
  1203. Reg r = (id < 65536) ? RID_X1 : ra_allock(as, id, allow);
  1204. emit_lso(as, A64I_STRB, RID_TMP, RID_RET, offsetof(GCcdata, gct));
  1205. emit_lso(as, A64I_STRH, r, RID_RET, offsetof(GCcdata, ctypeid));
  1206. emit_d(as, A64I_MOVZw | A64F_U16(~LJ_TCDATA), RID_TMP);
  1207. if (id < 65536) emit_d(as, A64I_MOVZw | A64F_U16(id), RID_X1);
  1208. }
  1209. args[0] = ASMREF_L; /* lua_State *L */
  1210. args[1] = ASMREF_TMP1; /* MSize size */
  1211. asm_gencall(as, ci, args);
  1212. ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
  1213. ra_releasetmp(as, ASMREF_TMP1));
  1214. }
  1215. #endif
  1216. /* -- Write barriers ------------------------------------------------------ */
  1217. static void asm_tbar(ASMState *as, IRIns *ir)
  1218. {
  1219. Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
  1220. Reg link = ra_scratch(as, rset_exclude(RSET_GPR, tab));
  1221. Reg mark = RID_TMP;
  1222. MCLabel l_end = emit_label(as);
  1223. emit_lso(as, A64I_STRB, mark, tab, (int32_t)offsetof(GCtab, marked));
  1224. /* Keep STRx in the middle to avoid LDP/STP fusion with surrounding code. */
  1225. emit_lso(as, A64I_STRx, link, tab, (int32_t)offsetof(GCtab, gclist));
  1226. emit_setgl(as, tab, gc.grayagain);
  1227. emit_dn(as, A64I_ANDw^emit_isk13(~LJ_GC_BLACK, 0), mark, mark);
  1228. emit_getgl(as, link, gc.grayagain);
  1229. emit_cond_branch(as, CC_EQ, l_end);
  1230. emit_n(as, A64I_TSTw^emit_isk13(LJ_GC_BLACK, 0), mark);
  1231. emit_lso(as, A64I_LDRB, mark, tab, (int32_t)offsetof(GCtab, marked));
  1232. }
  1233. static void asm_obar(ASMState *as, IRIns *ir)
  1234. {
  1235. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
  1236. IRRef args[2];
  1237. MCLabel l_end;
  1238. Reg obj, val, tmp;
  1239. /* No need for other object barriers (yet). */
  1240. lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type");
  1241. ra_evictset(as, RSET_SCRATCH);
  1242. l_end = emit_label(as);
  1243. args[0] = ASMREF_TMP1; /* global_State *g */
  1244. args[1] = ir->op1; /* TValue *tv */
  1245. asm_gencall(as, ci, args);
  1246. emit_dm(as, A64I_MOVx, ra_releasetmp(as, ASMREF_TMP1), RID_GL);
  1247. obj = IR(ir->op1)->r;
  1248. tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
  1249. emit_tnb(as, A64I_TBZ, tmp, lj_ffs(LJ_GC_BLACK), l_end);
  1250. emit_cond_branch(as, CC_EQ, l_end);
  1251. emit_n(as, A64I_TSTw^emit_isk13(LJ_GC_WHITES, 0), RID_TMP);
  1252. val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));
  1253. emit_lso(as, A64I_LDRB, tmp, obj,
  1254. (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));
  1255. emit_lso(as, A64I_LDRB, RID_TMP, val, (int32_t)offsetof(GChead, marked));
  1256. }
  1257. /* -- Arithmetic and logic operations ------------------------------------- */
  1258. static void asm_fparith(ASMState *as, IRIns *ir, A64Ins ai)
  1259. {
  1260. Reg dest = ra_dest(as, ir, RSET_FPR);
  1261. Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  1262. right = (left >> 8); left &= 255;
  1263. emit_dnm(as, ai, (dest & 31), (left & 31), (right & 31));
  1264. }
  1265. static void asm_fpunary(ASMState *as, IRIns *ir, A64Ins ai)
  1266. {
  1267. Reg dest = ra_dest(as, ir, RSET_FPR);
  1268. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
  1269. emit_dn(as, ai, (dest & 31), (left & 31));
  1270. }
  1271. static void asm_fpmath(ASMState *as, IRIns *ir)
  1272. {
  1273. IRFPMathOp fpm = (IRFPMathOp)ir->op2;
  1274. if (fpm == IRFPM_SQRT) {
  1275. asm_fpunary(as, ir, A64I_FSQRTd);
  1276. } else if (fpm <= IRFPM_TRUNC) {
  1277. asm_fpunary(as, ir, fpm == IRFPM_FLOOR ? A64I_FRINTMd :
  1278. fpm == IRFPM_CEIL ? A64I_FRINTPd : A64I_FRINTZd);
  1279. } else {
  1280. asm_callid(as, ir, IRCALL_lj_vm_floor + fpm);
  1281. }
  1282. }
  1283. static int asm_swapops(ASMState *as, IRRef lref, IRRef rref)
  1284. {
  1285. IRIns *ir;
  1286. if (irref_isk(rref))
  1287. return 0; /* Don't swap constants to the left. */
  1288. if (irref_isk(lref))
  1289. return 1; /* But swap constants to the right. */
  1290. ir = IR(rref);
  1291. if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) ||
  1292. (ir->o == IR_ADD && ir->op1 == ir->op2) ||
  1293. (ir->o == IR_CONV && ir->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT)))
  1294. return 0; /* Don't swap fusable operands to the left. */
  1295. ir = IR(lref);
  1296. if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) ||
  1297. (ir->o == IR_ADD && ir->op1 == ir->op2) ||
  1298. (ir->o == IR_CONV && ir->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT)))
  1299. return 1; /* But swap fusable operands to the right. */
  1300. return 0; /* Otherwise don't swap. */
  1301. }
  1302. static void asm_intop(ASMState *as, IRIns *ir, A64Ins ai)
  1303. {
  1304. IRRef lref = ir->op1, rref = ir->op2;
  1305. Reg left, dest = ra_dest(as, ir, RSET_GPR);
  1306. uint32_t m;
  1307. if ((ai & ~A64I_S) != A64I_SUBw && asm_swapops(as, lref, rref)) {
  1308. IRRef tmp = lref; lref = rref; rref = tmp;
  1309. }
  1310. left = ra_hintalloc(as, lref, dest, RSET_GPR);
  1311. if (irt_is64(ir->t)) ai |= A64I_X;
  1312. m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left));
  1313. if (irt_isguard(ir->t)) { /* For IR_ADDOV etc. */
  1314. asm_guardcc(as, CC_VS);
  1315. ai |= A64I_S;
  1316. }
  1317. emit_dn(as, ai^m, dest, left);
  1318. }
  1319. static void asm_intop_s(ASMState *as, IRIns *ir, A64Ins ai)
  1320. {
  1321. if (as->flagmcp == as->mcp) { /* Drop cmp r, #0. */
  1322. as->flagmcp = NULL;
  1323. as->mcp++;
  1324. ai |= A64I_S;
  1325. }
  1326. asm_intop(as, ir, ai);
  1327. }
  1328. static void asm_intneg(ASMState *as, IRIns *ir)
  1329. {
  1330. Reg dest = ra_dest(as, ir, RSET_GPR);
  1331. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1332. emit_dm(as, irt_is64(ir->t) ? A64I_NEGx : A64I_NEGw, dest, left);
  1333. }
  1334. /* NYI: use add/shift for MUL(OV) with constants. FOLD only does 2^k. */
  1335. static void asm_intmul(ASMState *as, IRIns *ir)
  1336. {
  1337. Reg dest = ra_dest(as, ir, RSET_GPR);
  1338. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1339. Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1340. if (irt_isguard(ir->t)) { /* IR_MULOV */
  1341. asm_guardcc(as, CC_NE);
  1342. emit_dm(as, A64I_MOVw, dest, dest); /* Zero-extend. */
  1343. emit_nm(as, A64I_CMPx | A64F_EX(A64EX_SXTW), dest, dest);
  1344. emit_dnm(as, A64I_SMULL, dest, right, left);
  1345. } else {
  1346. emit_dnm(as, irt_is64(ir->t) ? A64I_MULx : A64I_MULw, dest, left, right);
  1347. }
  1348. }
  1349. static void asm_add(ASMState *as, IRIns *ir)
  1350. {
  1351. if (irt_isnum(ir->t)) {
  1352. if (!asm_fusemadd(as, ir, A64I_FMADDd, A64I_FMADDd))
  1353. asm_fparith(as, ir, A64I_FADDd);
  1354. return;
  1355. }
  1356. asm_intop_s(as, ir, A64I_ADDw);
  1357. }
  1358. static void asm_sub(ASMState *as, IRIns *ir)
  1359. {
  1360. if (irt_isnum(ir->t)) {
  1361. if (!asm_fusemadd(as, ir, A64I_FNMSUBd, A64I_FMSUBd))
  1362. asm_fparith(as, ir, A64I_FSUBd);
  1363. return;
  1364. }
  1365. asm_intop_s(as, ir, A64I_SUBw);
  1366. }
  1367. static void asm_mul(ASMState *as, IRIns *ir)
  1368. {
  1369. if (irt_isnum(ir->t)) {
  1370. asm_fparith(as, ir, A64I_FMULd);
  1371. return;
  1372. }
  1373. asm_intmul(as, ir);
  1374. }
  1375. #define asm_addov(as, ir) asm_add(as, ir)
  1376. #define asm_subov(as, ir) asm_sub(as, ir)
  1377. #define asm_mulov(as, ir) asm_mul(as, ir)
  1378. #define asm_fpdiv(as, ir) asm_fparith(as, ir, A64I_FDIVd)
  1379. #define asm_abs(as, ir) asm_fpunary(as, ir, A64I_FABS)
  1380. static void asm_neg(ASMState *as, IRIns *ir)
  1381. {
  1382. if (irt_isnum(ir->t)) {
  1383. asm_fpunary(as, ir, A64I_FNEGd);
  1384. return;
  1385. }
  1386. asm_intneg(as, ir);
  1387. }
  1388. static void asm_band(ASMState *as, IRIns *ir)
  1389. {
  1390. A64Ins ai = A64I_ANDw;
  1391. if (asm_fuseandshift(as, ir))
  1392. return;
  1393. if (as->flagmcp == as->mcp) {
  1394. /* Try to drop cmp r, #0. */
  1395. as->flagmcp = NULL;
  1396. as->mcp++;
  1397. ai = A64I_ANDSw;
  1398. }
  1399. asm_intop(as, ir, ai);
  1400. }
  1401. static void asm_borbxor(ASMState *as, IRIns *ir, A64Ins ai)
  1402. {
  1403. IRRef lref = ir->op1, rref = ir->op2;
  1404. IRIns *irl = IR(lref), *irr = IR(rref);
  1405. if ((canfuse(as, irl) && irl->o == IR_BNOT && !irref_isk(rref)) ||
  1406. (canfuse(as, irr) && irr->o == IR_BNOT && !irref_isk(lref))) {
  1407. Reg left, dest = ra_dest(as, ir, RSET_GPR);
  1408. uint32_t m;
  1409. if (irl->o == IR_BNOT) {
  1410. IRRef tmp = lref; lref = rref; rref = tmp;
  1411. }
  1412. left = ra_alloc1(as, lref, RSET_GPR);
  1413. ai |= A64I_ON;
  1414. if (irt_is64(ir->t)) ai |= A64I_X;
  1415. m = asm_fuseopm(as, ai, IR(rref)->op1, rset_exclude(RSET_GPR, left));
  1416. emit_dn(as, ai^m, dest, left);
  1417. } else {
  1418. asm_intop(as, ir, ai);
  1419. }
  1420. }
  1421. static void asm_bor(ASMState *as, IRIns *ir)
  1422. {
  1423. if (asm_fuseorshift(as, ir))
  1424. return;
  1425. asm_borbxor(as, ir, A64I_ORRw);
  1426. }
  1427. #define asm_bxor(as, ir) asm_borbxor(as, ir, A64I_EORw)
  1428. static void asm_bnot(ASMState *as, IRIns *ir)
  1429. {
  1430. A64Ins ai = A64I_MVNw;
  1431. Reg dest = ra_dest(as, ir, RSET_GPR);
  1432. uint32_t m = asm_fuseopm(as, ai, ir->op1, RSET_GPR);
  1433. if (irt_is64(ir->t)) ai |= A64I_X;
  1434. emit_d(as, ai^m, dest);
  1435. }
  1436. static void asm_bswap(ASMState *as, IRIns *ir)
  1437. {
  1438. Reg dest = ra_dest(as, ir, RSET_GPR);
  1439. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1440. emit_dn(as, irt_is64(ir->t) ? A64I_REVx : A64I_REVw, dest, left);
  1441. }
  1442. static void asm_bitshift(ASMState *as, IRIns *ir, A64Ins ai, A64Shift sh)
  1443. {
  1444. int32_t shmask = irt_is64(ir->t) ? 63 : 31;
  1445. if (irref_isk(ir->op2)) { /* Constant shifts. */
  1446. Reg left, dest = ra_dest(as, ir, RSET_GPR);
  1447. int32_t shift = (IR(ir->op2)->i & shmask);
  1448. IRIns *irl = IR(ir->op1);
  1449. if (shmask == 63) ai += A64I_UBFMx - A64I_UBFMw;
  1450. /* Fuse BSHL + BSHR/BSAR into UBFM/SBFM aka UBFX/SBFX/UBFIZ/SBFIZ. */
  1451. if ((sh == A64SH_LSR || sh == A64SH_ASR) && canfuse(as, irl)) {
  1452. if (irl->o == IR_BSHL && irref_isk(irl->op2)) {
  1453. int32_t shift2 = (IR(irl->op2)->i & shmask);
  1454. shift = ((shift - shift2) & shmask);
  1455. shmask -= shift2;
  1456. ir = irl;
  1457. }
  1458. }
  1459. left = ra_alloc1(as, ir->op1, RSET_GPR);
  1460. switch (sh) {
  1461. case A64SH_LSL:
  1462. emit_dn(as, ai | A64F_IMMS(shmask-shift) |
  1463. A64F_IMMR((shmask-shift+1)&shmask), dest, left);
  1464. break;
  1465. case A64SH_LSR: case A64SH_ASR:
  1466. emit_dn(as, ai | A64F_IMMS(shmask) | A64F_IMMR(shift), dest, left);
  1467. break;
  1468. case A64SH_ROR:
  1469. emit_dnm(as, ai | A64F_IMMS(shift), dest, left, left);
  1470. break;
  1471. }
  1472. } else { /* Variable-length shifts. */
  1473. Reg dest = ra_dest(as, ir, RSET_GPR);
  1474. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1475. Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1476. emit_dnm(as, (shmask == 63 ? A64I_SHRx : A64I_SHRw) | A64F_BSH(sh), dest, left, right);
  1477. }
  1478. }
  1479. #define asm_bshl(as, ir) asm_bitshift(as, ir, A64I_UBFMw, A64SH_LSL)
  1480. #define asm_bshr(as, ir) asm_bitshift(as, ir, A64I_UBFMw, A64SH_LSR)
  1481. #define asm_bsar(as, ir) asm_bitshift(as, ir, A64I_SBFMw, A64SH_ASR)
  1482. #define asm_bror(as, ir) asm_bitshift(as, ir, A64I_EXTRw, A64SH_ROR)
  1483. #define asm_brol(as, ir) lj_assertA(0, "unexpected BROL")
  1484. static void asm_intmin_max(ASMState *as, IRIns *ir, A64CC cc)
  1485. {
  1486. Reg dest = ra_dest(as, ir, RSET_GPR);
  1487. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1488. Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1489. emit_dnm(as, A64I_CSELw|A64F_CC(cc), dest, left, right);
  1490. emit_nm(as, A64I_CMPw, left, right);
  1491. }
  1492. static void asm_fpmin_max(ASMState *as, IRIns *ir, A64CC fcc)
  1493. {
  1494. Reg dest = (ra_dest(as, ir, RSET_FPR) & 31);
  1495. Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  1496. right = ((left >> 8) & 31); left &= 31;
  1497. emit_dnm(as, A64I_FCSELd | A64F_CC(fcc), dest, right, left);
  1498. emit_nm(as, A64I_FCMPd, left, right);
  1499. }
  1500. static void asm_min_max(ASMState *as, IRIns *ir, A64CC cc, A64CC fcc)
  1501. {
  1502. if (irt_isnum(ir->t))
  1503. asm_fpmin_max(as, ir, fcc);
  1504. else
  1505. asm_intmin_max(as, ir, cc);
  1506. }
  1507. #define asm_min(as, ir) asm_min_max(as, ir, CC_LT, CC_PL)
  1508. #define asm_max(as, ir) asm_min_max(as, ir, CC_GT, CC_LE)
  1509. /* -- Comparisons --------------------------------------------------------- */
  1510. /* Map of comparisons to flags. ORDER IR. */
  1511. static const uint8_t asm_compmap[IR_ABC+1] = {
  1512. /* op FP swp int cc FP cc */
  1513. /* LT */ CC_GE + (CC_HS << 4),
  1514. /* GE x */ CC_LT + (CC_HI << 4),
  1515. /* LE */ CC_GT + (CC_HI << 4),
  1516. /* GT x */ CC_LE + (CC_HS << 4),
  1517. /* ULT x */ CC_HS + (CC_LS << 4),
  1518. /* UGE */ CC_LO + (CC_LO << 4),
  1519. /* ULE x */ CC_HI + (CC_LO << 4),
  1520. /* UGT */ CC_LS + (CC_LS << 4),
  1521. /* EQ */ CC_NE + (CC_NE << 4),
  1522. /* NE */ CC_EQ + (CC_EQ << 4),
  1523. /* ABC */ CC_LS + (CC_LS << 4) /* Same as UGT. */
  1524. };
  1525. /* FP comparisons. */
  1526. static void asm_fpcomp(ASMState *as, IRIns *ir)
  1527. {
  1528. Reg left, right;
  1529. A64Ins ai;
  1530. int swp = ((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1);
  1531. if (!swp && irref_isk(ir->op2) && ir_knum(IR(ir->op2))->u64 == 0) {
  1532. left = (ra_alloc1(as, ir->op1, RSET_FPR) & 31);
  1533. right = 0;
  1534. ai = A64I_FCMPZd;
  1535. } else {
  1536. left = ra_alloc2(as, ir, RSET_FPR);
  1537. if (swp) {
  1538. right = (left & 31); left = ((left >> 8) & 31);
  1539. } else {
  1540. right = ((left >> 8) & 31); left &= 31;
  1541. }
  1542. ai = A64I_FCMPd;
  1543. }
  1544. asm_guardcc(as, (asm_compmap[ir->o] >> 4));
  1545. emit_nm(as, ai, left, right);
  1546. }
  1547. /* Integer comparisons. */
  1548. static void asm_intcomp(ASMState *as, IRIns *ir)
  1549. {
  1550. A64CC oldcc, cc = (asm_compmap[ir->o] & 15);
  1551. A64Ins ai = irt_is64(ir->t) ? A64I_CMPx : A64I_CMPw;
  1552. IRRef lref = ir->op1, rref = ir->op2;
  1553. Reg left;
  1554. uint32_t m;
  1555. int cmpprev0 = 0;
  1556. lj_assertA(irt_is64(ir->t) || irt_isint(ir->t) ||
  1557. irt_isu32(ir->t) || irt_isaddr(ir->t) || irt_isu8(ir->t),
  1558. "bad comparison data type %d", irt_type(ir->t));
  1559. if (asm_swapops(as, lref, rref)) {
  1560. IRRef tmp = lref; lref = rref; rref = tmp;
  1561. if (cc >= CC_GE) cc ^= 7; /* LT <-> GT, LE <-> GE */
  1562. else if (cc > CC_NE) cc ^= 11; /* LO <-> HI, LS <-> HS */
  1563. }
  1564. oldcc = cc;
  1565. if (irref_isk(rref) && get_k64val(as, rref) == 0) {
  1566. IRIns *irl = IR(lref);
  1567. if (cc == CC_GE) cc = CC_PL;
  1568. else if (cc == CC_LT) cc = CC_MI;
  1569. else if (cc > CC_NE) goto nocombine; /* Other conds don't work with tst. */
  1570. cmpprev0 = (irl+1 == ir);
  1571. /* Combine and-cmp-bcc into tbz/tbnz or and-cmp into tst. */
  1572. if (cmpprev0 && irl->o == IR_BAND && !ra_used(irl)) {
  1573. IRRef blref = irl->op1, brref = irl->op2;
  1574. uint32_t m2 = 0;
  1575. Reg bleft;
  1576. if (asm_swapops(as, blref, brref)) {
  1577. Reg tmp = blref; blref = brref; brref = tmp;
  1578. }
  1579. bleft = ra_alloc1(as, blref, RSET_GPR);
  1580. if (irref_isk(brref)) {
  1581. uint64_t k = get_k64val(as, brref);
  1582. if (k && !(k & (k-1)) && (cc == CC_EQ || cc == CC_NE) &&
  1583. asm_guardtnb(as, cc == CC_EQ ? A64I_TBZ : A64I_TBNZ, bleft,
  1584. emit_ctz64(k)))
  1585. return;
  1586. m2 = emit_isk13(k, irt_is64(irl->t));
  1587. }
  1588. ai = (irt_is64(irl->t) ? A64I_TSTx : A64I_TSTw);
  1589. if (!m2)
  1590. m2 = asm_fuseopm(as, ai, brref, rset_exclude(RSET_GPR, bleft));
  1591. asm_guardcc(as, cc);
  1592. emit_n(as, ai^m2, bleft);
  1593. return;
  1594. }
  1595. if (cc == CC_EQ || cc == CC_NE) {
  1596. /* Combine cmp-bcc into cbz/cbnz. */
  1597. ai = cc == CC_EQ ? A64I_CBZ : A64I_CBNZ;
  1598. if (irt_is64(ir->t)) ai |= A64I_X;
  1599. asm_guardcnb(as, ai, ra_alloc1(as, lref, RSET_GPR));
  1600. return;
  1601. }
  1602. }
  1603. nocombine:
  1604. left = ra_alloc1(as, lref, RSET_GPR);
  1605. m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left));
  1606. asm_guardcc(as, cc);
  1607. emit_n(as, ai^m, left);
  1608. /* Signed comparison with zero and referencing previous ins? */
  1609. if (cmpprev0 && (oldcc <= CC_NE || oldcc >= CC_GE))
  1610. as->flagmcp = as->mcp; /* Allow elimination of the compare. */
  1611. }
  1612. static void asm_comp(ASMState *as, IRIns *ir)
  1613. {
  1614. if (irt_isnum(ir->t))
  1615. asm_fpcomp(as, ir);
  1616. else
  1617. asm_intcomp(as, ir);
  1618. }
  1619. #define asm_equal(as, ir) asm_comp(as, ir)
  1620. /* -- Split register ops -------------------------------------------------- */
  1621. /* Hiword op of a split 64/64 bit op. Previous op is the loword op. */
  1622. static void asm_hiop(ASMState *as, IRIns *ir)
  1623. {
  1624. /* HIOP is marked as a store because it needs its own DCE logic. */
  1625. int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
  1626. if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
  1627. if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
  1628. switch ((ir-1)->o) {
  1629. case IR_CALLN:
  1630. case IR_CALLL:
  1631. case IR_CALLS:
  1632. case IR_CALLXS:
  1633. if (!uselo)
  1634. ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
  1635. break;
  1636. default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break;
  1637. }
  1638. }
  1639. /* -- Profiling ----------------------------------------------------------- */
  1640. static void asm_prof(ASMState *as, IRIns *ir)
  1641. {
  1642. uint32_t k = emit_isk13(HOOK_PROFILE, 0);
  1643. lj_assertA(k != 0, "HOOK_PROFILE does not fit in K13");
  1644. UNUSED(ir);
  1645. asm_guardcc(as, CC_NE);
  1646. emit_n(as, A64I_TSTw^k, RID_TMP);
  1647. emit_lsptr(as, A64I_LDRB, RID_TMP, (void *)&J2G(as->J)->hookmask);
  1648. }
  1649. /* -- Stack handling ------------------------------------------------------ */
  1650. /* Check Lua stack size for overflow. Use exit handler as fallback. */
  1651. static void asm_stack_check(ASMState *as, BCReg topslot,
  1652. IRIns *irp, RegSet allow, ExitNo exitno)
  1653. {
  1654. uint32_t k;
  1655. Reg pbase = RID_BASE;
  1656. if (irp) {
  1657. pbase = irp->r;
  1658. if (!ra_hasreg(pbase))
  1659. pbase = allow ? (0x40 | rset_pickbot(allow)) : (0xC0 | RID_RET);
  1660. }
  1661. emit_cond_branch(as, CC_LS, asm_exitstub_addr(as, exitno));
  1662. if (pbase & 0x80) /* Restore temp. register. */
  1663. emit_lso(as, A64I_LDRx, (pbase & 31), RID_SP, 0);
  1664. k = emit_isk12((8*topslot));
  1665. lj_assertA(k, "slot offset %d does not fit in K12", 8*topslot);
  1666. emit_n(as, A64I_CMPx^k, RID_TMP);
  1667. emit_dnm(as, A64I_SUBx, RID_TMP, RID_TMP, (pbase & 31));
  1668. emit_lso(as, A64I_LDRx, RID_TMP, RID_TMP,
  1669. (int32_t)offsetof(lua_State, maxstack));
  1670. if (pbase & 0x40) {
  1671. emit_getgl(as, (pbase & 31), jit_base);
  1672. if (pbase & 0x80) /* Save temp register. */
  1673. emit_lso(as, A64I_STRx, (pbase & 31), RID_SP, 0);
  1674. }
  1675. emit_getgl(as, RID_TMP, cur_L);
  1676. }
  1677. /* Restore Lua stack from on-trace state. */
  1678. static void asm_stack_restore(ASMState *as, SnapShot *snap)
  1679. {
  1680. SnapEntry *map = &as->T->snapmap[snap->mapofs];
  1681. #ifdef LUA_USE_ASSERT
  1682. SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1-LJ_FR2];
  1683. #endif
  1684. MSize n, nent = snap->nent;
  1685. /* Store the value of all modified slots to the Lua stack. */
  1686. for (n = 0; n < nent; n++) {
  1687. SnapEntry sn = map[n];
  1688. BCReg s = snap_slot(sn);
  1689. int32_t ofs = 8*((int32_t)s-1-LJ_FR2);
  1690. IRRef ref = snap_ref(sn);
  1691. IRIns *ir = IR(ref);
  1692. if ((sn & SNAP_NORESTORE))
  1693. continue;
  1694. if ((sn & SNAP_KEYINDEX)) {
  1695. RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
  1696. Reg r = irref_isk(ref) ? ra_allock(as, ir->i, allow) :
  1697. ra_alloc1(as, ref, allow);
  1698. rset_clear(allow, r);
  1699. emit_lso(as, A64I_STRw, r, RID_BASE, ofs);
  1700. emit_lso(as, A64I_STRw, ra_allock(as, LJ_KEYINDEX, allow), RID_BASE, ofs+4);
  1701. } else if (irt_isnum(ir->t)) {
  1702. Reg src = ra_alloc1(as, ref, RSET_FPR);
  1703. emit_lso(as, A64I_STRd, (src & 31), RID_BASE, ofs);
  1704. } else {
  1705. asm_tvstore64(as, RID_BASE, ofs, ref);
  1706. }
  1707. checkmclim(as);
  1708. }
  1709. lj_assertA(map + nent == flinks, "inconsistent frames in snapshot");
  1710. }
  1711. /* -- GC handling --------------------------------------------------------- */
  1712. /* Marker to prevent patching the GC check exit. */
  1713. #define ARM64_NOPATCH_GC_CHECK \
  1714. (A64I_ORRx|A64F_D(RID_ZERO)|A64F_M(RID_ZERO)|A64F_N(RID_ZERO))
  1715. /* Check GC threshold and do one or more GC steps. */
  1716. static void asm_gc_check(ASMState *as)
  1717. {
  1718. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
  1719. IRRef args[2];
  1720. MCLabel l_end;
  1721. Reg tmp2;
  1722. ra_evictset(as, RSET_SCRATCH);
  1723. l_end = emit_label(as);
  1724. /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
  1725. asm_guardcnb(as, A64I_CBNZ, RID_RET); /* Assumes asm_snap_prep() is done. */
  1726. *--as->mcp = ARM64_NOPATCH_GC_CHECK;
  1727. args[0] = ASMREF_TMP1; /* global_State *g */
  1728. args[1] = ASMREF_TMP2; /* MSize steps */
  1729. asm_gencall(as, ci, args);
  1730. emit_dm(as, A64I_MOVx, ra_releasetmp(as, ASMREF_TMP1), RID_GL);
  1731. tmp2 = ra_releasetmp(as, ASMREF_TMP2);
  1732. emit_loadi(as, tmp2, as->gcsteps);
  1733. /* Jump around GC step if GC total < GC threshold. */
  1734. emit_cond_branch(as, CC_LS, l_end);
  1735. emit_nm(as, A64I_CMPx, RID_TMP, tmp2);
  1736. emit_getgl(as, tmp2, gc.threshold);
  1737. emit_getgl(as, RID_TMP, gc.total);
  1738. as->gcsteps = 0;
  1739. checkmclim(as);
  1740. }
  1741. /* -- Loop handling ------------------------------------------------------- */
  1742. /* Fixup the loop branch. */
  1743. static void asm_loop_fixup(ASMState *as)
  1744. {
  1745. MCode *p = as->mctop;
  1746. MCode *target = as->mcp;
  1747. if (as->loopinv) { /* Inverted loop branch? */
  1748. uint32_t mask = (p[-2] & 0x7e000000) == 0x36000000 ? 0x3fffu : 0x7ffffu;
  1749. ptrdiff_t delta = target - (p - 2);
  1750. /* asm_guard* already inverted the bcc/tnb/cnb and patched the final b. */
  1751. p[-2] |= ((uint32_t)delta & mask) << 5;
  1752. } else {
  1753. ptrdiff_t delta = target - (p - 1);
  1754. p[-1] = A64I_B | A64F_S26(delta);
  1755. }
  1756. }
  1757. /* Fixup the tail of the loop. */
  1758. static void asm_loop_tail_fixup(ASMState *as)
  1759. {
  1760. UNUSED(as); /* Nothing to do. */
  1761. }
  1762. /* -- Head of trace ------------------------------------------------------- */
  1763. /* Coalesce BASE register for a root trace. */
  1764. static void asm_head_root_base(ASMState *as)
  1765. {
  1766. IRIns *ir = IR(REF_BASE);
  1767. Reg r = ir->r;
  1768. if (ra_hasreg(r)) {
  1769. ra_free(as, r);
  1770. if (rset_test(as->modset, r) || irt_ismarked(ir->t))
  1771. ir->r = RID_INIT; /* No inheritance for modified BASE register. */
  1772. if (r != RID_BASE)
  1773. emit_movrr(as, ir, r, RID_BASE);
  1774. }
  1775. }
  1776. /* Coalesce BASE register for a side trace. */
  1777. static Reg asm_head_side_base(ASMState *as, IRIns *irp)
  1778. {
  1779. IRIns *ir = IR(REF_BASE);
  1780. Reg r = ir->r;
  1781. if (ra_hasreg(r)) {
  1782. ra_free(as, r);
  1783. if (rset_test(as->modset, r) || irt_ismarked(ir->t))
  1784. ir->r = RID_INIT; /* No inheritance for modified BASE register. */
  1785. if (irp->r == r) {
  1786. return r; /* Same BASE register already coalesced. */
  1787. } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {
  1788. /* Move from coalesced parent reg. */
  1789. emit_movrr(as, ir, r, irp->r);
  1790. return irp->r;
  1791. } else {
  1792. emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */
  1793. }
  1794. }
  1795. return RID_NONE;
  1796. }
  1797. /* -- Tail of trace ------------------------------------------------------- */
  1798. /* Fixup the tail code. */
  1799. static void asm_tail_fixup(ASMState *as, TraceNo lnk)
  1800. {
  1801. MCode *p = as->mctop;
  1802. MCode *target;
  1803. /* Undo the sp adjustment in BC_JLOOP when exiting to the interpreter. */
  1804. int32_t spadj = as->T->spadjust + (lnk ? 0 : sps_scale(SPS_FIXED));
  1805. if (spadj == 0) {
  1806. *--p = A64I_LE(A64I_NOP);
  1807. as->mctop = p;
  1808. } else {
  1809. /* Patch stack adjustment. */
  1810. uint32_t k = emit_isk12(spadj);
  1811. lj_assertA(k, "stack adjustment %d does not fit in K12", spadj);
  1812. p[-2] = (A64I_ADDx^k) | A64F_D(RID_SP) | A64F_N(RID_SP);
  1813. }
  1814. /* Patch exit branch. */
  1815. target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp;
  1816. p[-1] = A64I_B | A64F_S26((target-p)+1);
  1817. }
  1818. /* Prepare tail of code. */
  1819. static void asm_tail_prep(ASMState *as)
  1820. {
  1821. MCode *p = as->mctop - 1; /* Leave room for exit branch. */
  1822. if (as->loopref) {
  1823. as->invmcp = as->mcp = p;
  1824. } else {
  1825. as->mcp = p-1; /* Leave room for stack pointer adjustment. */
  1826. as->invmcp = NULL;
  1827. }
  1828. *p = 0; /* Prevent load/store merging. */
  1829. }
  1830. /* -- Trace setup --------------------------------------------------------- */
  1831. /* Ensure there are enough stack slots for call arguments. */
  1832. static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
  1833. {
  1834. #if LJ_HASFFI
  1835. uint32_t i, nargs = CCI_XNARGS(ci);
  1836. if (nargs > (REGARG_NUMGPR < REGARG_NUMFPR ? REGARG_NUMGPR : REGARG_NUMFPR) ||
  1837. (LJ_TARGET_OSX && (ci->flags & CCI_VARARG))) {
  1838. IRRef args[CCI_NARGS_MAX*2];
  1839. int ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
  1840. int spofs = 0, spalign = LJ_TARGET_OSX ? 0 : 7, nslots;
  1841. asm_collectargs(as, ir, ci, args);
  1842. #if LJ_ABI_WIN
  1843. if ((ci->flags & CCI_VARARG)) nfpr = 0;
  1844. #endif
  1845. for (i = 0; i < nargs; i++) {
  1846. int al = spalign;
  1847. if (!args[i]) {
  1848. #if LJ_TARGET_OSX
  1849. /* Marker for start of varaargs. */
  1850. nfpr = 0;
  1851. ngpr = 0;
  1852. spalign = 7;
  1853. #endif
  1854. } else if (irt_isfp(IR(args[i])->t)) {
  1855. if (nfpr > 0) { nfpr--; continue; }
  1856. #if LJ_ABI_WIN
  1857. if ((ci->flags & CCI_VARARG) && ngpr > 0) { ngpr--; continue; }
  1858. #elif LJ_TARGET_OSX
  1859. al |= irt_isnum(IR(args[i])->t) ? 7 : 3;
  1860. #endif
  1861. } else {
  1862. if (ngpr > 0) { ngpr--; continue; }
  1863. #if LJ_TARGET_OSX
  1864. al |= irt_size(IR(args[i])->t) - 1;
  1865. #endif
  1866. }
  1867. spofs = (spofs + 2*al+1) & ~al; /* Align and bump stack pointer. */
  1868. }
  1869. nslots = (spofs + 3) >> 2;
  1870. if (nslots > as->evenspill) /* Leave room for args in stack slots. */
  1871. as->evenspill = nslots;
  1872. }
  1873. #endif
  1874. return REGSP_HINT(irt_isfp(ir->t) ? RID_FPRET : RID_RET);
  1875. }
  1876. static void asm_setup_target(ASMState *as)
  1877. {
  1878. /* May need extra exit for asm_stack_check on side traces. */
  1879. asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0));
  1880. }
  1881. #if LJ_BE
  1882. /* ARM64 instructions are always little-endian. Swap for ARM64BE. */
  1883. static void asm_mcode_fixup(MCode *mcode, MSize size)
  1884. {
  1885. MCode *pe = (MCode *)((char *)mcode + size);
  1886. while (mcode < pe) {
  1887. MCode ins = *mcode;
  1888. *mcode++ = lj_bswap(ins);
  1889. }
  1890. }
  1891. #define LJ_TARGET_MCODE_FIXUP 1
  1892. #endif
  1893. /* -- Trace patching ------------------------------------------------------ */
  1894. /* Patch exit jumps of existing machine code to a new target. */
  1895. void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
  1896. {
  1897. MCode *p = T->mcode;
  1898. MCode *pe = (MCode *)((char *)p + T->szmcode);
  1899. MCode *cstart = NULL;
  1900. MCode *mcarea = lj_mcode_patch(J, p, 0);
  1901. MCode *px = exitstub_trace_addr(T, exitno);
  1902. int patchlong = 1;
  1903. /* Note: this assumes a trace exit is only ever patched once. */
  1904. for (; p < pe; p++) {
  1905. /* Look for exitstub branch, replace with branch to target. */
  1906. ptrdiff_t delta = target - p;
  1907. MCode ins = A64I_LE(*p);
  1908. if ((ins & 0xff000000u) == 0x54000000u &&
  1909. ((ins ^ ((px-p)<<5)) & 0x00ffffe0u) == 0) {
  1910. /* Patch bcc, if within range. */
  1911. if (A64F_S_OK(delta, 19)) {
  1912. *p = A64I_LE((ins & 0xff00001fu) | A64F_S19(delta));
  1913. if (!cstart) cstart = p;
  1914. }
  1915. } else if ((ins & 0xfc000000u) == 0x14000000u &&
  1916. ((ins ^ (px-p)) & 0x03ffffffu) == 0) {
  1917. /* Patch b. */
  1918. lj_assertJ(A64F_S_OK(delta, 26), "branch target out of range");
  1919. *p = A64I_LE((ins & 0xfc000000u) | A64F_S26(delta));
  1920. if (!cstart) cstart = p;
  1921. } else if ((ins & 0x7e000000u) == 0x34000000u &&
  1922. ((ins ^ ((px-p)<<5)) & 0x00ffffe0u) == 0) {
  1923. /* Patch cbz/cbnz, if within range. */
  1924. if (p[-1] == ARM64_NOPATCH_GC_CHECK) {
  1925. patchlong = 0;
  1926. } else if (A64F_S_OK(delta, 19)) {
  1927. *p = A64I_LE((ins & 0xff00001fu) | A64F_S19(delta));
  1928. if (!cstart) cstart = p;
  1929. }
  1930. } else if ((ins & 0x7e000000u) == 0x36000000u &&
  1931. ((ins ^ ((px-p)<<5)) & 0x0007ffe0u) == 0) {
  1932. /* Patch tbz/tbnz, if within range. */
  1933. if (A64F_S_OK(delta, 14)) {
  1934. *p = A64I_LE((ins & 0xfff8001fu) | A64F_S14(delta));
  1935. if (!cstart) cstart = p;
  1936. }
  1937. }
  1938. }
  1939. /* Always patch long-range branch in exit stub itself. Except, if we can't. */
  1940. if (patchlong) {
  1941. ptrdiff_t delta = target - px;
  1942. lj_assertJ(A64F_S_OK(delta, 26), "branch target out of range");
  1943. *px = A64I_B | A64F_S26(delta);
  1944. if (!cstart) cstart = px;
  1945. }
  1946. if (cstart) lj_mcode_sync(cstart, px+1);
  1947. lj_mcode_patch(J, mcarea, 1);
  1948. }