lj_asm_ppc.h 74 KB

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