lj_asm_mips.h 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815
  1. /*
  2. ** MIPS IR assembler (SSA IR -> machine code).
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. /* -- Register allocator extensions --------------------------------------- */
  6. /* Allocate a register with a hint. */
  7. static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)
  8. {
  9. Reg r = IR(ref)->r;
  10. if (ra_noreg(r)) {
  11. if (!ra_hashint(r) && !iscrossref(as, ref))
  12. ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */
  13. r = ra_allocref(as, ref, allow);
  14. }
  15. ra_noweak(as, r);
  16. return r;
  17. }
  18. /* Allocate a register or RID_ZERO. */
  19. static Reg ra_alloc1z(ASMState *as, IRRef ref, RegSet allow)
  20. {
  21. Reg r = IR(ref)->r;
  22. if (ra_noreg(r)) {
  23. if (!(allow & RSET_FPR) && irref_isk(ref) && get_kval(as, ref) == 0)
  24. return RID_ZERO;
  25. r = ra_allocref(as, ref, allow);
  26. } else {
  27. ra_noweak(as, r);
  28. }
  29. return r;
  30. }
  31. /* Allocate two source registers for three-operand instructions. */
  32. static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)
  33. {
  34. IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
  35. Reg left = irl->r, right = irr->r;
  36. if (ra_hasreg(left)) {
  37. ra_noweak(as, left);
  38. if (ra_noreg(right))
  39. right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left));
  40. else
  41. ra_noweak(as, right);
  42. } else if (ra_hasreg(right)) {
  43. ra_noweak(as, right);
  44. left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right));
  45. } else if (ra_hashint(right)) {
  46. right = ra_alloc1z(as, ir->op2, allow);
  47. left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right));
  48. } else {
  49. left = ra_alloc1z(as, ir->op1, allow);
  50. right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left));
  51. }
  52. return left | (right << 8);
  53. }
  54. /* -- Guard handling ------------------------------------------------------ */
  55. /* Need some spare long-range jump slots, for out-of-range branches. */
  56. #define MIPS_SPAREJUMP 4
  57. /* Setup spare long-range jump slots per mcarea. */
  58. static void asm_sparejump_setup(ASMState *as)
  59. {
  60. MCode *mxp = as->mctop;
  61. if ((char *)mxp == (char *)as->J->mcarea + as->J->szmcarea) {
  62. mxp -= MIPS_SPAREJUMP*2;
  63. lj_assertA(MIPSI_NOP == 0, "bad NOP");
  64. memset(mxp, 0, MIPS_SPAREJUMP*2*sizeof(MCode));
  65. as->mctop = mxp;
  66. }
  67. }
  68. static MCode *asm_sparejump_use(MCode *mcarea, MCode tjump)
  69. {
  70. MCode *mxp = (MCode *)((char *)mcarea + ((MCLink *)mcarea)->size);
  71. int slot = MIPS_SPAREJUMP;
  72. while (slot--) {
  73. mxp -= 2;
  74. if (*mxp == tjump) {
  75. return mxp;
  76. } else if (*mxp == MIPSI_NOP) {
  77. *mxp = tjump;
  78. return mxp;
  79. }
  80. }
  81. return NULL;
  82. }
  83. /* Setup exit stub after the end of each trace. */
  84. static void asm_exitstub_setup(ASMState *as)
  85. {
  86. MCode *mxp = as->mctop;
  87. /* sw TMP, 0(sp); j ->vm_exit_handler; li TMP, traceno */
  88. *--mxp = MIPSI_LI|MIPSF_T(RID_TMP)|as->T->traceno;
  89. *--mxp = MIPSI_J|((((uintptr_t)(void *)lj_vm_exit_handler)>>2)&0x03ffffffu);
  90. lj_assertA(((uintptr_t)mxp ^ (uintptr_t)(void *)lj_vm_exit_handler)>>28 == 0,
  91. "branch target out of range");
  92. *--mxp = MIPSI_SW|MIPSF_T(RID_TMP)|MIPSF_S(RID_SP)|0;
  93. as->mctop = mxp;
  94. }
  95. /* Keep this in-sync with exitstub_trace_addr(). */
  96. #define asm_exitstub_addr(as) ((as)->mctop)
  97. /* Emit conditional branch to exit for guard. */
  98. static void asm_guard(ASMState *as, MIPSIns mi, Reg rs, Reg rt)
  99. {
  100. MCode *target = asm_exitstub_addr(as);
  101. MCode *p = as->mcp;
  102. if (LJ_UNLIKELY(p == as->invmcp)) {
  103. as->invmcp = NULL;
  104. as->loopinv = 1;
  105. as->mcp = p+1;
  106. #if !LJ_TARGET_MIPSR6
  107. mi = mi ^ ((mi>>28) == 1 ? 0x04000000u : 0x00010000u); /* Invert cond. */
  108. #else
  109. mi = mi ^ ((mi>>28) == 1 ? 0x04000000u :
  110. (mi>>28) == 4 ? 0x00800000u : 0x00010000u); /* Invert cond. */
  111. #endif
  112. target = p; /* Patch target later in asm_loop_fixup. */
  113. }
  114. emit_ti(as, MIPSI_LI, RID_TMP, as->snapno);
  115. emit_branch(as, mi, rs, rt, target);
  116. }
  117. /* -- Operand fusion ------------------------------------------------------ */
  118. /* Limit linear search to this distance. Avoids O(n^2) behavior. */
  119. #define CONFLICT_SEARCH_LIM 31
  120. /* Check if there's no conflicting instruction between curins and ref. */
  121. static int noconflict(ASMState *as, IRRef ref, IROp conflict)
  122. {
  123. IRIns *ir = as->ir;
  124. IRRef i = as->curins;
  125. if (i > ref + CONFLICT_SEARCH_LIM)
  126. return 0; /* Give up, ref is too far away. */
  127. while (--i > ref)
  128. if (ir[i].o == conflict)
  129. return 0; /* Conflict found. */
  130. return 1; /* Ok, no conflict. */
  131. }
  132. /* Fuse the array base of colocated arrays. */
  133. static int32_t asm_fuseabase(ASMState *as, IRRef ref)
  134. {
  135. IRIns *ir = IR(ref);
  136. if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&
  137. !neverfuse(as) && noconflict(as, ref, IR_NEWREF))
  138. return (int32_t)sizeof(GCtab);
  139. return 0;
  140. }
  141. /* Fuse array/hash/upvalue reference into register+offset operand. */
  142. static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)
  143. {
  144. IRIns *ir = IR(ref);
  145. if (ra_noreg(ir->r)) {
  146. if (ir->o == IR_AREF) {
  147. if (mayfuse(as, ref)) {
  148. if (irref_isk(ir->op2)) {
  149. IRRef tab = IR(ir->op1)->op1;
  150. int32_t ofs = asm_fuseabase(as, tab);
  151. IRRef refa = ofs ? tab : ir->op1;
  152. ofs += 8*IR(ir->op2)->i;
  153. if (checki16(ofs)) {
  154. *ofsp = ofs;
  155. return ra_alloc1(as, refa, allow);
  156. }
  157. }
  158. }
  159. } else if (ir->o == IR_HREFK) {
  160. if (mayfuse(as, ref)) {
  161. int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));
  162. if (checki16(ofs)) {
  163. *ofsp = ofs;
  164. return ra_alloc1(as, ir->op1, allow);
  165. }
  166. }
  167. } else if (ir->o == IR_UREFC) {
  168. if (irref_isk(ir->op1)) {
  169. GCfunc *fn = ir_kfunc(IR(ir->op1));
  170. intptr_t ofs = (intptr_t)&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv;
  171. intptr_t jgl = (intptr_t)J2G(as->J);
  172. if ((uintptr_t)(ofs-jgl) < 65536) {
  173. *ofsp = ofs-jgl-32768;
  174. return RID_JGL;
  175. } else {
  176. *ofsp = (int16_t)ofs;
  177. return ra_allock(as, ofs-(int16_t)ofs, allow);
  178. }
  179. }
  180. } else if (ir->o == IR_TMPREF) {
  181. *ofsp = (int32_t)(offsetof(global_State, tmptv)-32768);
  182. return RID_JGL;
  183. }
  184. }
  185. *ofsp = 0;
  186. return ra_alloc1(as, ref, allow);
  187. }
  188. /* Fuse XLOAD/XSTORE reference into load/store operand. */
  189. static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref,
  190. RegSet allow, int32_t ofs)
  191. {
  192. IRIns *ir = IR(ref);
  193. Reg base;
  194. if (ra_noreg(ir->r) && canfuse(as, ir)) {
  195. if (ir->o == IR_ADD) {
  196. intptr_t ofs2;
  197. if (irref_isk(ir->op2) && (ofs2 = ofs + get_kval(as, ir->op2),
  198. checki16(ofs2))) {
  199. ref = ir->op1;
  200. ofs = (int32_t)ofs2;
  201. }
  202. } else if (ir->o == IR_STRREF) {
  203. intptr_t ofs2 = 65536;
  204. lj_assertA(ofs == 0, "bad usage");
  205. ofs = (int32_t)sizeof(GCstr);
  206. if (irref_isk(ir->op2)) {
  207. ofs2 = ofs + get_kval(as, ir->op2);
  208. ref = ir->op1;
  209. } else if (irref_isk(ir->op1)) {
  210. ofs2 = ofs + get_kval(as, ir->op1);
  211. ref = ir->op2;
  212. }
  213. if (!checki16(ofs2)) {
  214. /* NYI: Fuse ADD with constant. */
  215. Reg right, left = ra_alloc2(as, ir, allow);
  216. right = (left >> 8); left &= 255;
  217. emit_hsi(as, mi, rt, RID_TMP, ofs);
  218. emit_dst(as, MIPSI_AADDU, RID_TMP, left, right);
  219. return;
  220. }
  221. ofs = ofs2;
  222. }
  223. }
  224. base = ra_alloc1(as, ref, allow);
  225. emit_hsi(as, mi, rt, base, ofs);
  226. }
  227. /* -- Calls --------------------------------------------------------------- */
  228. /* Generate a call to a C function. */
  229. static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
  230. {
  231. uint32_t n, nargs = CCI_XNARGS(ci);
  232. int32_t ofs = LJ_32 ? 16 : 0;
  233. #if LJ_SOFTFP
  234. Reg gpr = REGARG_FIRSTGPR;
  235. #else
  236. Reg gpr, fpr = REGARG_FIRSTFPR;
  237. #endif
  238. if ((void *)ci->func)
  239. emit_call(as, (void *)ci->func, 1);
  240. #if !LJ_SOFTFP
  241. for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++)
  242. as->cost[gpr] = REGCOST(~0u, ASMREF_L);
  243. gpr = REGARG_FIRSTGPR;
  244. #endif
  245. for (n = 0; n < nargs; n++) { /* Setup args. */
  246. IRRef ref = args[n];
  247. if (ref) {
  248. IRIns *ir = IR(ref);
  249. #if !LJ_SOFTFP
  250. if (irt_isfp(ir->t) && fpr <= REGARG_LASTFPR &&
  251. !(ci->flags & CCI_VARARG)) {
  252. lj_assertA(rset_test(as->freeset, fpr),
  253. "reg %d not free", fpr); /* Already evicted. */
  254. ra_leftov(as, fpr, ref);
  255. fpr += LJ_32 ? 2 : 1;
  256. gpr += (LJ_32 && irt_isnum(ir->t)) ? 2 : 1;
  257. } else
  258. #endif
  259. {
  260. #if LJ_32 && !LJ_SOFTFP
  261. fpr = REGARG_LASTFPR+1;
  262. #endif
  263. if (LJ_32 && irt_isnum(ir->t)) gpr = (gpr+1) & ~1;
  264. if (gpr <= REGARG_LASTGPR) {
  265. lj_assertA(rset_test(as->freeset, gpr),
  266. "reg %d not free", gpr); /* Already evicted. */
  267. #if !LJ_SOFTFP
  268. if (irt_isfp(ir->t)) {
  269. RegSet of = as->freeset;
  270. Reg r;
  271. /* Workaround to protect argument GPRs from being used for remat. */
  272. as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1);
  273. r = ra_alloc1(as, ref, RSET_FPR);
  274. as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1));
  275. if (irt_isnum(ir->t)) {
  276. #if LJ_32
  277. emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?0:1), r+1);
  278. emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?1:0), r);
  279. lj_assertA(rset_test(as->freeset, gpr+1),
  280. "reg %d not free", gpr+1); /* Already evicted. */
  281. gpr += 2;
  282. #else
  283. emit_tg(as, MIPSI_DMFC1, gpr, r);
  284. gpr++; fpr++;
  285. #endif
  286. } else if (irt_isfloat(ir->t)) {
  287. emit_tg(as, MIPSI_MFC1, gpr, r);
  288. gpr++;
  289. #if LJ_64
  290. fpr++;
  291. #endif
  292. }
  293. } else
  294. #endif
  295. {
  296. ra_leftov(as, gpr, ref);
  297. gpr++;
  298. #if LJ_64 && !LJ_SOFTFP
  299. fpr++;
  300. #endif
  301. }
  302. } else {
  303. Reg r = ra_alloc1z(as, ref, !LJ_SOFTFP && irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
  304. #if LJ_32
  305. if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4;
  306. emit_spstore(as, ir, r, ofs);
  307. ofs += irt_isnum(ir->t) ? 8 : 4;
  308. #else
  309. emit_spstore(as, ir, r, ofs + ((LJ_BE && !irt_isfp(ir->t) && !irt_is64(ir->t)) ? 4 : 0));
  310. ofs += 8;
  311. #endif
  312. }
  313. }
  314. } else {
  315. #if !LJ_SOFTFP
  316. fpr = REGARG_LASTFPR+1;
  317. #endif
  318. if (gpr <= REGARG_LASTGPR) {
  319. gpr++;
  320. #if LJ_64 && !LJ_SOFTFP
  321. fpr++;
  322. #endif
  323. } else {
  324. ofs += LJ_32 ? 4 : 8;
  325. }
  326. }
  327. checkmclim(as);
  328. }
  329. }
  330. /* Setup result reg/sp for call. Evict scratch regs. */
  331. static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
  332. {
  333. RegSet drop = RSET_SCRATCH;
  334. int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t));
  335. #if !LJ_SOFTFP
  336. if ((ci->flags & CCI_NOFPRCLOBBER))
  337. drop &= ~RSET_FPR;
  338. #endif
  339. if (ra_hasreg(ir->r))
  340. rset_clear(drop, ir->r); /* Dest reg handled below. */
  341. if (hiop && ra_hasreg((ir+1)->r))
  342. rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
  343. ra_evictset(as, drop); /* Evictions must be performed first. */
  344. if (ra_used(ir)) {
  345. lj_assertA(!irt_ispri(ir->t), "PRI dest");
  346. if (!LJ_SOFTFP && irt_isfp(ir->t)) {
  347. if ((ci->flags & CCI_CASTU64)) {
  348. int32_t ofs = sps_scale(ir->s);
  349. Reg dest = ir->r;
  350. if (ra_hasreg(dest)) {
  351. ra_free(as, dest);
  352. ra_modified(as, dest);
  353. #if LJ_32
  354. emit_tg(as, MIPSI_MTC1, RID_RETHI, dest+1);
  355. emit_tg(as, MIPSI_MTC1, RID_RETLO, dest);
  356. #else
  357. emit_tg(as, MIPSI_DMTC1, RID_RET, dest);
  358. #endif
  359. }
  360. if (ofs) {
  361. #if LJ_32
  362. emit_tsi(as, MIPSI_SW, RID_RETLO, RID_SP, ofs+(LJ_BE?4:0));
  363. emit_tsi(as, MIPSI_SW, RID_RETHI, RID_SP, ofs+(LJ_BE?0:4));
  364. #else
  365. emit_tsi(as, MIPSI_SD, RID_RET, RID_SP, ofs);
  366. #endif
  367. }
  368. } else {
  369. ra_destreg(as, ir, RID_FPRET);
  370. }
  371. } else if (hiop) {
  372. ra_destpair(as, ir);
  373. } else {
  374. ra_destreg(as, ir, RID_RET);
  375. }
  376. }
  377. }
  378. static void asm_callx(ASMState *as, IRIns *ir)
  379. {
  380. IRRef args[CCI_NARGS_MAX*2];
  381. CCallInfo ci;
  382. IRRef func;
  383. IRIns *irf;
  384. ci.flags = asm_callx_flags(as, ir);
  385. asm_collectargs(as, ir, &ci, args);
  386. asm_setupresult(as, ir, &ci);
  387. func = ir->op2; irf = IR(func);
  388. if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
  389. if (irref_isk(func)) { /* Call to constant address. */
  390. ci.func = (ASMFunction)(void *)get_kval(as, func);
  391. } else { /* Need specific register for indirect calls. */
  392. Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR));
  393. MCode *p = as->mcp;
  394. if (r == RID_CFUNCADDR)
  395. *--p = MIPSI_NOP;
  396. else
  397. *--p = MIPSI_MOVE | MIPSF_D(RID_CFUNCADDR) | MIPSF_S(r);
  398. *--p = MIPSI_JALR | MIPSF_S(r);
  399. as->mcp = p;
  400. ci.func = (ASMFunction)(void *)0;
  401. }
  402. asm_gencall(as, &ci, args);
  403. }
  404. #if !LJ_SOFTFP
  405. static void asm_callround(ASMState *as, IRIns *ir, IRCallID id)
  406. {
  407. /* The modified regs must match with the *.dasc implementation. */
  408. RegSet drop = RID2RSET(RID_R1)|RID2RSET(RID_R12)|RID2RSET(RID_FPRET)|
  409. RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(REGARG_FIRSTFPR)
  410. #if LJ_TARGET_MIPSR6
  411. |RID2RSET(RID_F21)
  412. #endif
  413. ;
  414. if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);
  415. ra_evictset(as, drop);
  416. ra_destreg(as, ir, RID_FPRET);
  417. emit_call(as, (void *)lj_ir_callinfo[id].func, 0);
  418. ra_leftov(as, REGARG_FIRSTFPR, ir->op1);
  419. }
  420. #endif
  421. /* -- Returns ------------------------------------------------------------- */
  422. /* Return to lower frame. Guard that it goes to the right spot. */
  423. static void asm_retf(ASMState *as, IRIns *ir)
  424. {
  425. Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
  426. void *pc = ir_kptr(IR(ir->op2));
  427. int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));
  428. as->topslot -= (BCReg)delta;
  429. if ((int32_t)as->topslot < 0) as->topslot = 0;
  430. irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */
  431. emit_setgl(as, base, jit_base);
  432. emit_addptr(as, base, -8*delta);
  433. asm_guard(as, MIPSI_BNE, RID_TMP,
  434. ra_allock(as, igcptr(pc), rset_exclude(RSET_GPR, base)));
  435. emit_tsi(as, MIPSI_AL, RID_TMP, base, -8);
  436. }
  437. /* -- Buffer operations --------------------------------------------------- */
  438. #if LJ_HASBUFFER
  439. static void asm_bufhdr_write(ASMState *as, Reg sb)
  440. {
  441. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb));
  442. IRIns irgc;
  443. irgc.ot = IRT(0, IRT_PGC); /* GC type. */
  444. emit_storeofs(as, &irgc, RID_TMP, sb, offsetof(SBuf, L));
  445. if ((as->flags & JIT_F_MIPSXXR2)) {
  446. emit_tsml(as, LJ_64 ? MIPSI_DINS : MIPSI_INS, RID_TMP, tmp,
  447. lj_fls(SBUF_MASK_FLAG), 0);
  448. } else {
  449. emit_dst(as, MIPSI_OR, RID_TMP, RID_TMP, tmp);
  450. emit_tsi(as, MIPSI_ANDI, tmp, tmp, SBUF_MASK_FLAG);
  451. }
  452. emit_getgl(as, RID_TMP, cur_L);
  453. emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L));
  454. }
  455. #endif
  456. /* -- Type conversions ---------------------------------------------------- */
  457. #if !LJ_SOFTFP
  458. static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
  459. {
  460. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
  461. Reg dest = ra_dest(as, ir, RSET_GPR);
  462. #if !LJ_TARGET_MIPSR6
  463. asm_guard(as, MIPSI_BC1F, 0, 0);
  464. emit_fgh(as, MIPSI_C_EQ_D, 0, tmp, left);
  465. #else
  466. asm_guard(as, MIPSI_BC1EQZ, 0, (tmp&31));
  467. emit_fgh(as, MIPSI_CMP_EQ_D, tmp, tmp, left);
  468. #endif
  469. emit_fg(as, MIPSI_CVT_D_W, tmp, tmp);
  470. emit_tg(as, MIPSI_MFC1, dest, tmp);
  471. emit_fg(as, MIPSI_CVT_W_D, tmp, left);
  472. }
  473. static void asm_tobit(ASMState *as, IRIns *ir)
  474. {
  475. RegSet allow = RSET_FPR;
  476. Reg dest = ra_dest(as, ir, RSET_GPR);
  477. Reg left = ra_alloc1(as, ir->op1, allow);
  478. Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));
  479. Reg tmp = ra_scratch(as, rset_clear(allow, right));
  480. emit_tg(as, MIPSI_MFC1, dest, tmp);
  481. emit_fgh(as, MIPSI_ADD_D, tmp, left, right);
  482. }
  483. #elif LJ_64 /* && LJ_SOFTFP */
  484. static void asm_tointg(ASMState *as, IRIns *ir, Reg r)
  485. {
  486. /* The modified regs must match with the *.dasc implementation. */
  487. RegSet drop = RID2RSET(REGARG_FIRSTGPR)|RID2RSET(RID_RET)|RID2RSET(RID_RET+1)|
  488. RID2RSET(RID_R1)|RID2RSET(RID_R12);
  489. if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);
  490. ra_evictset(as, drop);
  491. /* Return values are in RID_RET (converted value) and RID_RET+1 (status). */
  492. ra_destreg(as, ir, RID_RET);
  493. asm_guard(as, MIPSI_BNE, RID_RET+1, RID_ZERO);
  494. emit_call(as, (void *)lj_ir_callinfo[IRCALL_lj_vm_tointg].func, 0);
  495. if (r == RID_NONE)
  496. ra_leftov(as, REGARG_FIRSTGPR, ir->op1);
  497. else if (r != REGARG_FIRSTGPR)
  498. emit_move(as, REGARG_FIRSTGPR, r);
  499. }
  500. static void asm_tobit(ASMState *as, IRIns *ir)
  501. {
  502. Reg dest = ra_dest(as, ir, RSET_GPR);
  503. emit_dta(as, MIPSI_SLL, dest, dest, 0);
  504. asm_callid(as, ir, IRCALL_lj_vm_tobit);
  505. }
  506. #endif
  507. static void asm_conv(ASMState *as, IRIns *ir)
  508. {
  509. IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
  510. #if !LJ_SOFTFP32
  511. int stfp = (st == IRT_NUM || st == IRT_FLOAT);
  512. #endif
  513. #if LJ_64
  514. int st64 = (st == IRT_I64 || st == IRT_U64 || st == IRT_P64);
  515. #endif
  516. IRRef lref = ir->op1;
  517. #if LJ_32
  518. /* 64 bit integer conversions are handled by SPLIT. */
  519. lj_assertA(!(irt_isint64(ir->t) || (st == IRT_I64 || st == IRT_U64)),
  520. "IR %04d has unsplit 64 bit type",
  521. (int)(ir - as->ir) - REF_BIAS);
  522. #endif
  523. #if LJ_SOFTFP32
  524. /* FP conversions are handled by SPLIT. */
  525. lj_assertA(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT),
  526. "IR %04d has FP type",
  527. (int)(ir - as->ir) - REF_BIAS);
  528. /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */
  529. #else
  530. lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV");
  531. #if !LJ_SOFTFP
  532. if (irt_isfp(ir->t)) {
  533. Reg dest = ra_dest(as, ir, RSET_FPR);
  534. if (stfp) { /* FP to FP conversion. */
  535. emit_fg(as, st == IRT_NUM ? MIPSI_CVT_S_D : MIPSI_CVT_D_S,
  536. dest, ra_alloc1(as, lref, RSET_FPR));
  537. } else if (st == IRT_U32) { /* U32 to FP conversion. */
  538. /* y = (x ^ 0x8000000) + 2147483648.0 */
  539. Reg left = ra_alloc1(as, lref, RSET_GPR);
  540. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest));
  541. if (irt_isfloat(ir->t))
  542. emit_fg(as, MIPSI_CVT_S_D, dest, dest);
  543. /* Must perform arithmetic with doubles to keep the precision. */
  544. emit_fgh(as, MIPSI_ADD_D, dest, dest, tmp);
  545. emit_fg(as, MIPSI_CVT_D_W, dest, dest);
  546. emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
  547. (void *)&as->J->k64[LJ_K64_2P31], RSET_GPR);
  548. emit_tg(as, MIPSI_MTC1, RID_TMP, dest);
  549. emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, left);
  550. emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000);
  551. #if LJ_64
  552. } else if(st == IRT_U64) { /* U64 to FP conversion. */
  553. /* if (x >= 1u<<63) y = (double)(int64_t)(x&(1u<<63)-1) + pow(2.0, 63) */
  554. Reg left = ra_alloc1(as, lref, RSET_GPR);
  555. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest));
  556. MCLabel l_end = emit_label(as);
  557. if (irt_isfloat(ir->t)) {
  558. emit_fgh(as, MIPSI_ADD_S, dest, dest, tmp);
  559. emit_lsptr(as, MIPSI_LWC1, (tmp & 31), (void *)&as->J->k32[LJ_K32_2P63],
  560. rset_exclude(RSET_GPR, left));
  561. emit_fg(as, MIPSI_CVT_S_L, dest, dest);
  562. } else {
  563. emit_fgh(as, MIPSI_ADD_D, dest, dest, tmp);
  564. emit_lsptr(as, MIPSI_LDC1, (tmp & 31), (void *)&as->J->k64[LJ_K64_2P63],
  565. rset_exclude(RSET_GPR, left));
  566. emit_fg(as, MIPSI_CVT_D_L, dest, dest);
  567. }
  568. emit_branch(as, MIPSI_BGEZ, left, RID_ZERO, l_end);
  569. emit_tg(as, MIPSI_DMTC1, RID_TMP, dest);
  570. emit_tsml(as, MIPSI_DEXTM, RID_TMP, left, 30, 0);
  571. #endif
  572. } else { /* Integer to FP conversion. */
  573. Reg left = ra_alloc1(as, lref, RSET_GPR);
  574. #if LJ_32
  575. emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W,
  576. dest, dest);
  577. emit_tg(as, MIPSI_MTC1, left, dest);
  578. #else
  579. MIPSIns mi = irt_isfloat(ir->t) ?
  580. (st64 ? MIPSI_CVT_S_L : MIPSI_CVT_S_W) :
  581. (st64 ? MIPSI_CVT_D_L : MIPSI_CVT_D_W);
  582. emit_fg(as, mi, dest, dest);
  583. emit_tg(as, st64 ? MIPSI_DMTC1 : MIPSI_MTC1, left, dest);
  584. #endif
  585. }
  586. } else if (stfp) { /* FP to integer conversion. */
  587. if (irt_isguard(ir->t)) {
  588. /* Checked conversions are only supported from number to int. */
  589. lj_assertA(irt_isint(ir->t) && st == IRT_NUM,
  590. "bad type for checked CONV");
  591. asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
  592. } else {
  593. Reg dest = ra_dest(as, ir, RSET_GPR);
  594. Reg left = ra_alloc1(as, lref, RSET_FPR);
  595. Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
  596. if (irt_isu32(ir->t)) { /* FP to U32 conversion. */
  597. /* y = (int)floor(x - 2147483648.0) ^ 0x80000000 */
  598. emit_dst(as, MIPSI_XOR, dest, dest, RID_TMP);
  599. emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000);
  600. emit_tg(as, MIPSI_MFC1, dest, tmp);
  601. emit_fg(as, st == IRT_FLOAT ? MIPSI_FLOOR_W_S : MIPSI_FLOOR_W_D,
  602. tmp, tmp);
  603. emit_fgh(as, st == IRT_FLOAT ? MIPSI_SUB_S : MIPSI_SUB_D,
  604. tmp, left, tmp);
  605. if (st == IRT_FLOAT)
  606. emit_lsptr(as, MIPSI_LWC1, (tmp & 31),
  607. (void *)&as->J->k32[LJ_K32_2P31], RSET_GPR);
  608. else
  609. emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
  610. (void *)&as->J->k64[LJ_K64_2P31], RSET_GPR);
  611. #if LJ_64
  612. } else if (irt_isu64(ir->t)) { /* FP to U64 conversion. */
  613. MCLabel l_end;
  614. emit_tg(as, MIPSI_DMFC1, dest, tmp);
  615. l_end = emit_label(as);
  616. /* For inputs >= 2^63 add -2^64 and convert again. */
  617. if (st == IRT_NUM) {
  618. emit_fg(as, MIPSI_TRUNC_L_D, tmp, tmp);
  619. emit_fgh(as, MIPSI_ADD_D, tmp, left, tmp);
  620. emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
  621. (void *)&as->J->k64[LJ_K64_M2P64],
  622. rset_exclude(RSET_GPR, dest));
  623. emit_fg(as, MIPSI_TRUNC_L_D, tmp, left); /* Delay slot. */
  624. #if !LJ_TARGET_MIPSR6
  625. emit_branch(as, MIPSI_BC1T, 0, 0, l_end);
  626. emit_fgh(as, MIPSI_C_OLT_D, 0, left, tmp);
  627. #else
  628. emit_branch(as, MIPSI_BC1NEZ, 0, (tmp&31), l_end);
  629. emit_fgh(as, MIPSI_CMP_LT_D, tmp, left, tmp);
  630. #endif
  631. emit_lsptr(as, MIPSI_LDC1, (tmp & 31),
  632. (void *)&as->J->k64[LJ_K64_2P63],
  633. rset_exclude(RSET_GPR, dest));
  634. } else {
  635. emit_fg(as, MIPSI_TRUNC_L_S, tmp, tmp);
  636. emit_fgh(as, MIPSI_ADD_S, tmp, left, tmp);
  637. emit_lsptr(as, MIPSI_LWC1, (tmp & 31),
  638. (void *)&as->J->k32[LJ_K32_M2P64],
  639. rset_exclude(RSET_GPR, dest));
  640. emit_fg(as, MIPSI_TRUNC_L_S, tmp, left); /* Delay slot. */
  641. #if !LJ_TARGET_MIPSR6
  642. emit_branch(as, MIPSI_BC1T, 0, 0, l_end);
  643. emit_fgh(as, MIPSI_C_OLT_S, 0, left, tmp);
  644. #else
  645. emit_branch(as, MIPSI_BC1NEZ, 0, (tmp&31), l_end);
  646. emit_fgh(as, MIPSI_CMP_LT_S, tmp, left, tmp);
  647. #endif
  648. emit_lsptr(as, MIPSI_LWC1, (tmp & 31),
  649. (void *)&as->J->k32[LJ_K32_2P63],
  650. rset_exclude(RSET_GPR, dest));
  651. }
  652. #endif
  653. } else {
  654. #if LJ_32
  655. emit_tg(as, MIPSI_MFC1, dest, tmp);
  656. emit_fg(as, st == IRT_FLOAT ? MIPSI_TRUNC_W_S : MIPSI_TRUNC_W_D,
  657. tmp, left);
  658. #else
  659. MIPSIns mi = irt_is64(ir->t) ?
  660. (st == IRT_NUM ? MIPSI_TRUNC_L_D : MIPSI_TRUNC_L_S) :
  661. (st == IRT_NUM ? MIPSI_TRUNC_W_D : MIPSI_TRUNC_W_S);
  662. emit_tg(as, irt_is64(ir->t) ? MIPSI_DMFC1 : MIPSI_MFC1, dest, tmp);
  663. emit_fg(as, mi, tmp, left);
  664. #endif
  665. }
  666. }
  667. } else
  668. #else
  669. if (irt_isfp(ir->t)) {
  670. #if LJ_64 && LJ_HASFFI
  671. if (stfp) { /* FP to FP conversion. */
  672. asm_callid(as, ir, irt_isnum(ir->t) ? IRCALL_softfp_f2d :
  673. IRCALL_softfp_d2f);
  674. } else { /* Integer to FP conversion. */
  675. IRCallID cid = ((IRT_IS64 >> st) & 1) ?
  676. (irt_isnum(ir->t) ?
  677. (st == IRT_I64 ? IRCALL_fp64_l2d : IRCALL_fp64_ul2d) :
  678. (st == IRT_I64 ? IRCALL_fp64_l2f : IRCALL_fp64_ul2f)) :
  679. (irt_isnum(ir->t) ?
  680. (st == IRT_INT ? IRCALL_softfp_i2d : IRCALL_softfp_ui2d) :
  681. (st == IRT_INT ? IRCALL_softfp_i2f : IRCALL_softfp_ui2f));
  682. asm_callid(as, ir, cid);
  683. }
  684. #else
  685. asm_callid(as, ir, IRCALL_softfp_i2d);
  686. #endif
  687. } else if (stfp) { /* FP to integer conversion. */
  688. if (irt_isguard(ir->t)) {
  689. /* Checked conversions are only supported from number to int. */
  690. lj_assertA(irt_isint(ir->t) && st == IRT_NUM,
  691. "bad type for checked CONV");
  692. asm_tointg(as, ir, RID_NONE);
  693. } else {
  694. IRCallID cid = irt_is64(ir->t) ?
  695. ((st == IRT_NUM) ?
  696. (irt_isi64(ir->t) ? IRCALL_fp64_d2l : IRCALL_fp64_d2ul) :
  697. (irt_isi64(ir->t) ? IRCALL_fp64_f2l : IRCALL_fp64_f2ul)) :
  698. ((st == IRT_NUM) ?
  699. (irt_isint(ir->t) ? IRCALL_softfp_d2i : IRCALL_softfp_d2ui) :
  700. (irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui));
  701. asm_callid(as, ir, cid);
  702. }
  703. } else
  704. #endif
  705. #endif
  706. {
  707. Reg dest = ra_dest(as, ir, RSET_GPR);
  708. if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
  709. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  710. lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT");
  711. if ((ir->op2 & IRCONV_SEXT)) {
  712. if (LJ_64 || (as->flags & JIT_F_MIPSXXR2)) {
  713. emit_dst(as, st == IRT_I8 ? MIPSI_SEB : MIPSI_SEH, dest, 0, left);
  714. } else {
  715. uint32_t shift = st == IRT_I8 ? 24 : 16;
  716. emit_dta(as, MIPSI_SRA, dest, dest, shift);
  717. emit_dta(as, MIPSI_SLL, dest, left, shift);
  718. }
  719. } else {
  720. emit_tsi(as, MIPSI_ANDI, dest, left,
  721. (int32_t)(st == IRT_U8 ? 0xff : 0xffff));
  722. }
  723. } else { /* 32/64 bit integer conversions. */
  724. #if LJ_32
  725. /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */
  726. ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
  727. #else
  728. if (irt_is64(ir->t)) {
  729. if (st64) {
  730. /* 64/64 bit no-op (cast)*/
  731. ra_leftov(as, dest, lref);
  732. } else {
  733. Reg left = ra_alloc1(as, lref, RSET_GPR);
  734. if ((ir->op2 & IRCONV_SEXT)) { /* 32 to 64 bit sign extension. */
  735. emit_dta(as, MIPSI_SLL, dest, left, 0);
  736. } else { /* 32 to 64 bit zero extension. */
  737. emit_tsml(as, MIPSI_DEXT, dest, left, 31, 0);
  738. }
  739. }
  740. } else {
  741. if (st64 && !(ir->op2 & IRCONV_NONE)) {
  742. /* This is either a 32 bit reg/reg mov which zeroes the hiword
  743. ** or a load of the loword from a 64 bit address.
  744. */
  745. Reg left = ra_alloc1(as, lref, RSET_GPR);
  746. emit_tsml(as, MIPSI_DEXT, dest, left, 31, 0);
  747. } else { /* 32/32 bit no-op (cast). */
  748. /* Do nothing, but may need to move regs. */
  749. ra_leftov(as, dest, lref);
  750. }
  751. }
  752. #endif
  753. }
  754. }
  755. }
  756. static void asm_strto(ASMState *as, IRIns *ir)
  757. {
  758. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];
  759. IRRef args[2];
  760. int32_t ofs = 0;
  761. #if LJ_SOFTFP32
  762. ra_evictset(as, RSET_SCRATCH);
  763. if (ra_used(ir)) {
  764. if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) &&
  765. (ir->s & 1) == LJ_BE && (ir->s ^ 1) == (ir+1)->s) {
  766. int i;
  767. for (i = 0; i < 2; i++) {
  768. Reg r = (ir+i)->r;
  769. if (ra_hasreg(r)) {
  770. ra_free(as, r);
  771. ra_modified(as, r);
  772. emit_spload(as, ir+i, r, sps_scale((ir+i)->s));
  773. }
  774. }
  775. ofs = sps_scale(ir->s & ~1);
  776. } else {
  777. Reg rhi = ra_dest(as, ir+1, RSET_GPR);
  778. Reg rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi));
  779. emit_tsi(as, MIPSI_LW, rhi, RID_SP, ofs+(LJ_BE?0:4));
  780. emit_tsi(as, MIPSI_LW, rlo, RID_SP, ofs+(LJ_BE?4:0));
  781. }
  782. }
  783. #else
  784. RegSet drop = RSET_SCRATCH;
  785. if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */
  786. ra_evictset(as, drop);
  787. ofs = sps_scale(ir->s);
  788. #endif
  789. asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO); /* Test return status. */
  790. args[0] = ir->op1; /* GCstr *str */
  791. args[1] = ASMREF_TMP1; /* TValue *n */
  792. asm_gencall(as, ci, args);
  793. /* Store the result to the spill slot or temp slots. */
  794. emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1),
  795. RID_SP, ofs);
  796. }
  797. /* -- Memory references --------------------------------------------------- */
  798. #if LJ_64
  799. /* Store tagged value for ref at base+ofs. */
  800. static void asm_tvstore64(ASMState *as, Reg base, int32_t ofs, IRRef ref)
  801. {
  802. RegSet allow = rset_exclude(RSET_GPR, base);
  803. IRIns *ir = IR(ref);
  804. lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t),
  805. "store of IR type %d", irt_type(ir->t));
  806. if (irref_isk(ref)) {
  807. TValue k;
  808. lj_ir_kvalue(as->J->L, &k, ir);
  809. emit_tsi(as, MIPSI_SD, ra_allock(as, (int64_t)k.u64, allow), base, ofs);
  810. } else {
  811. Reg src = ra_alloc1(as, ref, allow);
  812. Reg type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47,
  813. rset_exclude(allow, src));
  814. emit_tsi(as, MIPSI_SD, RID_TMP, base, ofs);
  815. if (irt_isinteger(ir->t)) {
  816. emit_dst(as, MIPSI_DADDU, RID_TMP, RID_TMP, type);
  817. emit_tsml(as, MIPSI_DEXT, RID_TMP, src, 31, 0);
  818. } else {
  819. emit_dst(as, MIPSI_DADDU, RID_TMP, src, type);
  820. }
  821. }
  822. }
  823. #endif
  824. /* Get pointer to TValue. */
  825. static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode)
  826. {
  827. int32_t tmpofs = (int32_t)(offsetof(global_State, tmptv)-32768);
  828. if ((mode & IRTMPREF_IN1)) {
  829. IRIns *ir = IR(ref);
  830. if (irt_isnum(ir->t)) {
  831. if ((mode & IRTMPREF_OUT1)) {
  832. #if LJ_SOFTFP
  833. emit_tsi(as, MIPSI_AADDIU, dest, RID_JGL, tmpofs);
  834. #if LJ_64
  835. emit_setgl(as, ra_alloc1(as, ref, RSET_GPR), tmptv.u64);
  836. #else
  837. lj_assertA(irref_isk(ref), "unsplit FP op");
  838. emit_setgl(as,
  839. ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, RSET_GPR),
  840. tmptv.u32.lo);
  841. emit_setgl(as,
  842. ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, RSET_GPR),
  843. tmptv.u32.hi);
  844. #endif
  845. #else
  846. Reg src = ra_alloc1(as, ref, RSET_FPR);
  847. emit_tsi(as, MIPSI_AADDIU, dest, RID_JGL, tmpofs);
  848. emit_tsi(as, MIPSI_SDC1, (src & 31), RID_JGL, tmpofs);
  849. #endif
  850. } else if (irref_isk(ref)) {
  851. /* Use the number constant itself as a TValue. */
  852. ra_allockreg(as, igcptr(ir_knum(ir)), dest);
  853. } else {
  854. #if LJ_SOFTFP32
  855. lj_assertA(0, "unsplit FP op");
  856. #else
  857. /* Otherwise force a spill and use the spill slot. */
  858. emit_tsi(as, MIPSI_AADDIU, dest, RID_SP, ra_spill(as, ir));
  859. #endif
  860. }
  861. } else {
  862. /* Otherwise use g->tmptv to hold the TValue. */
  863. #if LJ_32
  864. Reg type;
  865. emit_tsi(as, MIPSI_ADDIU, dest, RID_JGL, tmpofs);
  866. if (!irt_ispri(ir->t)) {
  867. Reg src = ra_alloc1(as, ref, RSET_GPR);
  868. emit_setgl(as, src, tmptv.gcr);
  869. }
  870. if (LJ_SOFTFP && (ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t))
  871. type = ra_alloc1(as, ref+1, RSET_GPR);
  872. else
  873. type = ra_allock(as, (int32_t)irt_toitype(ir->t), RSET_GPR);
  874. emit_setgl(as, type, tmptv.it);
  875. #else
  876. asm_tvstore64(as, dest, 0, ref);
  877. emit_tsi(as, MIPSI_DADDIU, dest, RID_JGL, tmpofs);
  878. #endif
  879. }
  880. } else {
  881. emit_tsi(as, MIPSI_AADDIU, dest, RID_JGL, tmpofs);
  882. }
  883. }
  884. static void asm_aref(ASMState *as, IRIns *ir)
  885. {
  886. Reg dest = ra_dest(as, ir, RSET_GPR);
  887. Reg idx, base;
  888. if (irref_isk(ir->op2)) {
  889. IRRef tab = IR(ir->op1)->op1;
  890. int32_t ofs = asm_fuseabase(as, tab);
  891. IRRef refa = ofs ? tab : ir->op1;
  892. ofs += 8*IR(ir->op2)->i;
  893. if (checki16(ofs)) {
  894. base = ra_alloc1(as, refa, RSET_GPR);
  895. emit_tsi(as, MIPSI_AADDIU, dest, base, ofs);
  896. return;
  897. }
  898. }
  899. base = ra_alloc1(as, ir->op1, RSET_GPR);
  900. idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
  901. #if !LJ_TARGET_MIPSR6
  902. emit_dst(as, MIPSI_AADDU, dest, RID_TMP, base);
  903. emit_dta(as, MIPSI_SLL, RID_TMP, idx, 3);
  904. #else
  905. emit_dst(as, MIPSI_ALSA | MIPSF_A(3-1), dest, idx, base);
  906. #endif
  907. }
  908. /* Inlined hash lookup. Specialized for key type and for const keys.
  909. ** The equivalent C code is:
  910. ** Node *n = hashkey(t, key);
  911. ** do {
  912. ** if (lj_obj_equal(&n->key, key)) return &n->val;
  913. ** } while ((n = nextnode(n)));
  914. ** return niltv(L);
  915. */
  916. static void asm_href(ASMState *as, IRIns *ir, IROp merge)
  917. {
  918. RegSet allow = RSET_GPR;
  919. int destused = ra_used(ir);
  920. Reg dest = ra_dest(as, ir, allow);
  921. Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
  922. Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1 = RID_TMP, tmp2;
  923. #if LJ_64
  924. Reg cmp64 = RID_NONE;
  925. #endif
  926. IRRef refkey = ir->op2;
  927. IRIns *irkey = IR(refkey);
  928. int isk = irref_isk(refkey);
  929. IRType1 kt = irkey->t;
  930. uint32_t khash;
  931. MCLabel l_end, l_loop, l_next;
  932. rset_clear(allow, tab);
  933. if (!LJ_SOFTFP && irt_isnum(kt)) {
  934. key = ra_alloc1(as, refkey, RSET_FPR);
  935. tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));
  936. } else {
  937. if (!irt_ispri(kt)) {
  938. key = ra_alloc1(as, refkey, allow);
  939. rset_clear(allow, key);
  940. }
  941. #if LJ_32
  942. if (LJ_SOFTFP && irkey[1].o == IR_HIOP) {
  943. if (ra_hasreg((irkey+1)->r)) {
  944. type = tmpnum = (irkey+1)->r;
  945. tmp1 = ra_scratch(as, allow);
  946. rset_clear(allow, tmp1);
  947. ra_noweak(as, tmpnum);
  948. } else {
  949. type = tmpnum = ra_allocref(as, refkey+1, allow);
  950. }
  951. rset_clear(allow, tmpnum);
  952. } else {
  953. type = ra_allock(as, (int32_t)irt_toitype(kt), allow);
  954. rset_clear(allow, type);
  955. }
  956. #endif
  957. }
  958. tmp2 = ra_scratch(as, allow);
  959. rset_clear(allow, tmp2);
  960. #if LJ_64
  961. if (LJ_SOFTFP || !irt_isnum(kt)) {
  962. /* Allocate cmp64 register used for 64-bit comparisons */
  963. if (LJ_SOFTFP && irt_isnum(kt)) {
  964. cmp64 = key;
  965. } else if (!isk && irt_isaddr(kt)) {
  966. cmp64 = tmp2;
  967. } else {
  968. int64_t k;
  969. if (isk && irt_isaddr(kt)) {
  970. k = ((int64_t)irt_toitype(kt) << 47) | irkey[1].tv.u64;
  971. } else {
  972. lj_assertA(irt_ispri(kt) && !irt_isnil(kt), "bad HREF key type");
  973. k = ~((int64_t)~irt_toitype(kt) << 47);
  974. }
  975. cmp64 = ra_allock(as, k, allow);
  976. rset_clear(allow, cmp64);
  977. }
  978. }
  979. #endif
  980. /* Key not found in chain: jump to exit (if merged) or load niltv. */
  981. l_end = emit_label(as);
  982. as->invmcp = NULL;
  983. if (merge == IR_NE)
  984. asm_guard(as, MIPSI_B, RID_ZERO, RID_ZERO);
  985. else if (destused)
  986. emit_loada(as, dest, niltvg(J2G(as->J)));
  987. /* Follow hash chain until the end. */
  988. emit_move(as, dest, tmp1);
  989. l_loop = --as->mcp;
  990. emit_tsi(as, MIPSI_AL, tmp1, dest, (int32_t)offsetof(Node, next));
  991. l_next = emit_label(as);
  992. /* Type and value comparison. */
  993. if (merge == IR_EQ) { /* Must match asm_guard(). */
  994. emit_ti(as, MIPSI_LI, RID_TMP, as->snapno);
  995. l_end = asm_exitstub_addr(as);
  996. }
  997. if (!LJ_SOFTFP && irt_isnum(kt)) {
  998. #if !LJ_TARGET_MIPSR6
  999. emit_branch(as, MIPSI_BC1T, 0, 0, l_end);
  1000. emit_fgh(as, MIPSI_C_EQ_D, 0, tmpnum, key);
  1001. #else
  1002. emit_branch(as, MIPSI_BC1NEZ, 0, (tmpnum&31), l_end);
  1003. emit_fgh(as, MIPSI_CMP_EQ_D, tmpnum, tmpnum, key);
  1004. #endif
  1005. *--as->mcp = MIPSI_NOP; /* Avoid NaN comparison overhead. */
  1006. emit_branch(as, MIPSI_BEQ, tmp1, RID_ZERO, l_next);
  1007. emit_tsi(as, MIPSI_SLTIU, tmp1, tmp1, (int32_t)LJ_TISNUM);
  1008. #if LJ_32
  1009. emit_hsi(as, MIPSI_LDC1, tmpnum, dest, (int32_t)offsetof(Node, key.n));
  1010. } else {
  1011. if (irt_ispri(kt)) {
  1012. emit_branch(as, MIPSI_BEQ, tmp1, type, l_end);
  1013. } else {
  1014. emit_branch(as, MIPSI_BEQ, tmp2, key, l_end);
  1015. emit_tsi(as, MIPSI_LW, tmp2, dest, (int32_t)offsetof(Node, key.gcr));
  1016. emit_branch(as, MIPSI_BNE, tmp1, type, l_next);
  1017. }
  1018. }
  1019. emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, key.it));
  1020. *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu);
  1021. #else
  1022. emit_dta(as, MIPSI_DSRA32, tmp1, tmp1, 15);
  1023. emit_tg(as, MIPSI_DMTC1, tmp1, tmpnum);
  1024. emit_tsi(as, MIPSI_LD, tmp1, dest, (int32_t)offsetof(Node, key.u64));
  1025. } else {
  1026. emit_branch(as, MIPSI_BEQ, tmp1, cmp64, l_end);
  1027. emit_tsi(as, MIPSI_LD, tmp1, dest, (int32_t)offsetof(Node, key.u64));
  1028. }
  1029. *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu);
  1030. if (!isk && irt_isaddr(kt)) {
  1031. type = ra_allock(as, (int64_t)irt_toitype(kt) << 47, allow);
  1032. emit_dst(as, MIPSI_DADDU, tmp2, key, type);
  1033. rset_clear(allow, type);
  1034. }
  1035. #endif
  1036. /* Load main position relative to tab->node into dest. */
  1037. khash = isk ? ir_khash(as, irkey) : 1;
  1038. if (khash == 0) {
  1039. emit_tsi(as, MIPSI_AL, dest, tab, (int32_t)offsetof(GCtab, node));
  1040. } else {
  1041. Reg tmphash = tmp1;
  1042. if (isk)
  1043. tmphash = ra_allock(as, khash, allow);
  1044. emit_dst(as, MIPSI_AADDU, dest, dest, tmp1);
  1045. lj_assertA(sizeof(Node) == 24, "bad Node size");
  1046. emit_dst(as, MIPSI_SUBU, tmp1, tmp2, tmp1);
  1047. emit_dta(as, MIPSI_SLL, tmp1, tmp1, 3);
  1048. emit_dta(as, MIPSI_SLL, tmp2, tmp1, 5);
  1049. emit_dst(as, MIPSI_AND, tmp1, tmp2, tmphash);
  1050. emit_tsi(as, MIPSI_AL, dest, tab, (int32_t)offsetof(GCtab, node));
  1051. emit_tsi(as, MIPSI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask));
  1052. if (isk) {
  1053. /* Nothing to do. */
  1054. } else if (irt_isstr(kt)) {
  1055. emit_tsi(as, MIPSI_LW, tmp1, key, (int32_t)offsetof(GCstr, sid));
  1056. } else { /* Must match with hash*() in lj_tab.c. */
  1057. emit_dst(as, MIPSI_SUBU, tmp1, tmp1, tmp2);
  1058. emit_rotr(as, tmp2, tmp2, dest, (-HASH_ROT3)&31);
  1059. emit_dst(as, MIPSI_XOR, tmp1, tmp1, tmp2);
  1060. emit_rotr(as, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&31);
  1061. emit_dst(as, MIPSI_SUBU, tmp2, tmp2, dest);
  1062. #if LJ_32
  1063. if (LJ_SOFTFP ? (irkey[1].o == IR_HIOP) : irt_isnum(kt)) {
  1064. emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1);
  1065. if ((as->flags & JIT_F_MIPSXXR2)) {
  1066. emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31);
  1067. } else {
  1068. emit_dst(as, MIPSI_OR, dest, dest, tmp1);
  1069. emit_dta(as, MIPSI_SLL, tmp1, tmp1, HASH_ROT1);
  1070. emit_dta(as, MIPSI_SRL, dest, tmp1, (-HASH_ROT1)&31);
  1071. }
  1072. emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1);
  1073. #if LJ_SOFTFP
  1074. emit_ds(as, MIPSI_MOVE, tmp1, type);
  1075. emit_ds(as, MIPSI_MOVE, tmp2, key);
  1076. #else
  1077. emit_tg(as, MIPSI_MFC1, tmp2, key);
  1078. emit_tg(as, MIPSI_MFC1, tmp1, key+1);
  1079. #endif
  1080. } else {
  1081. emit_dst(as, MIPSI_XOR, tmp2, key, tmp1);
  1082. emit_rotr(as, dest, tmp1, tmp2, (-HASH_ROT1)&31);
  1083. emit_dst(as, MIPSI_ADDU, tmp1, key, ra_allock(as, HASH_BIAS, allow));
  1084. }
  1085. #else
  1086. emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1);
  1087. emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31);
  1088. if (irt_isnum(kt)) {
  1089. emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1);
  1090. emit_dta(as, MIPSI_DSRA32, tmp1, LJ_SOFTFP ? key : tmp1, 0);
  1091. emit_dta(as, MIPSI_SLL, tmp2, LJ_SOFTFP ? key : tmp1, 0);
  1092. #if !LJ_SOFTFP
  1093. emit_tg(as, MIPSI_DMFC1, tmp1, key);
  1094. #endif
  1095. } else {
  1096. checkmclim(as);
  1097. emit_dta(as, MIPSI_DSRA32, tmp1, tmp1, 0);
  1098. emit_dta(as, MIPSI_SLL, tmp2, key, 0);
  1099. emit_dst(as, MIPSI_DADDU, tmp1, key, type);
  1100. }
  1101. #endif
  1102. }
  1103. }
  1104. }
  1105. static void asm_hrefk(ASMState *as, IRIns *ir)
  1106. {
  1107. IRIns *kslot = IR(ir->op2);
  1108. IRIns *irkey = IR(kslot->op1);
  1109. int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
  1110. int32_t kofs = ofs + (int32_t)offsetof(Node, key);
  1111. Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
  1112. Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
  1113. RegSet allow = rset_exclude(RSET_GPR, node);
  1114. Reg idx = node;
  1115. #if LJ_32
  1116. Reg key = RID_NONE, type = RID_TMP;
  1117. int32_t lo, hi;
  1118. #else
  1119. Reg key = ra_scratch(as, allow);
  1120. int64_t k;
  1121. #endif
  1122. lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot");
  1123. if (ofs > 32736) {
  1124. idx = dest;
  1125. rset_clear(allow, dest);
  1126. kofs = (int32_t)offsetof(Node, key);
  1127. } else if (ra_hasreg(dest)) {
  1128. emit_tsi(as, MIPSI_AADDIU, dest, node, ofs);
  1129. }
  1130. #if LJ_32
  1131. if (!irt_ispri(irkey->t)) {
  1132. key = ra_scratch(as, allow);
  1133. rset_clear(allow, key);
  1134. }
  1135. if (irt_isnum(irkey->t)) {
  1136. lo = (int32_t)ir_knum(irkey)->u32.lo;
  1137. hi = (int32_t)ir_knum(irkey)->u32.hi;
  1138. } else {
  1139. lo = irkey->i;
  1140. hi = irt_toitype(irkey->t);
  1141. if (!ra_hasreg(key))
  1142. goto nolo;
  1143. }
  1144. asm_guard(as, MIPSI_BNE, key, lo ? ra_allock(as, lo, allow) : RID_ZERO);
  1145. nolo:
  1146. asm_guard(as, MIPSI_BNE, type, hi ? ra_allock(as, hi, allow) : RID_ZERO);
  1147. if (ra_hasreg(key)) emit_tsi(as, MIPSI_LW, key, idx, kofs+(LJ_BE?4:0));
  1148. emit_tsi(as, MIPSI_LW, type, idx, kofs+(LJ_BE?0:4));
  1149. #else
  1150. if (irt_ispri(irkey->t)) {
  1151. lj_assertA(!irt_isnil(irkey->t), "bad HREFK key type");
  1152. k = ~((int64_t)~irt_toitype(irkey->t) << 47);
  1153. } else if (irt_isnum(irkey->t)) {
  1154. k = (int64_t)ir_knum(irkey)->u64;
  1155. } else {
  1156. k = ((int64_t)irt_toitype(irkey->t) << 47) | (int64_t)ir_kgc(irkey);
  1157. }
  1158. asm_guard(as, MIPSI_BNE, key, ra_allock(as, k, allow));
  1159. emit_tsi(as, MIPSI_LD, key, idx, kofs);
  1160. #endif
  1161. if (ofs > 32736)
  1162. emit_tsi(as, MIPSI_AADDU, dest, node, ra_allock(as, ofs, allow));
  1163. }
  1164. static void asm_uref(ASMState *as, IRIns *ir)
  1165. {
  1166. Reg dest = ra_dest(as, ir, RSET_GPR);
  1167. int guarded = (irt_t(ir->t) & (IRT_GUARD|IRT_TYPE)) == (IRT_GUARD|IRT_PGC);
  1168. if (irref_isk(ir->op1) && !guarded) {
  1169. GCfunc *fn = ir_kfunc(IR(ir->op1));
  1170. MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
  1171. emit_lsptr(as, MIPSI_AL, dest, v, RSET_GPR);
  1172. } else {
  1173. if (guarded)
  1174. asm_guard(as, ir->o == IR_UREFC ? MIPSI_BEQ : MIPSI_BNE, RID_TMP, RID_ZERO);
  1175. if (ir->o == IR_UREFC)
  1176. emit_tsi(as, MIPSI_AADDIU, dest, dest, (int32_t)offsetof(GCupval, tv));
  1177. else
  1178. emit_tsi(as, MIPSI_AL, dest, dest, (int32_t)offsetof(GCupval, v));
  1179. if (guarded)
  1180. emit_tsi(as, MIPSI_LBU, RID_TMP, dest, (int32_t)offsetof(GCupval, closed));
  1181. if (irref_isk(ir->op1)) {
  1182. GCfunc *fn = ir_kfunc(IR(ir->op1));
  1183. GCobj *o = gcref(fn->l.uvptr[(ir->op2 >> 8)]);
  1184. emit_loada(as, dest, o);
  1185. } else {
  1186. emit_tsi(as, MIPSI_AL, dest, ra_alloc1(as, ir->op1, RSET_GPR),
  1187. (int32_t)offsetof(GCfuncL, uvptr) +
  1188. (int32_t)sizeof(MRef) * (int32_t)(ir->op2 >> 8));
  1189. }
  1190. }
  1191. }
  1192. static void asm_fref(ASMState *as, IRIns *ir)
  1193. {
  1194. UNUSED(as); UNUSED(ir);
  1195. lj_assertA(!ra_used(ir), "unfused FREF");
  1196. }
  1197. static void asm_strref(ASMState *as, IRIns *ir)
  1198. {
  1199. #if LJ_32
  1200. Reg dest = ra_dest(as, ir, RSET_GPR);
  1201. IRRef ref = ir->op2, refk = ir->op1;
  1202. int32_t ofs = (int32_t)sizeof(GCstr);
  1203. Reg r;
  1204. if (irref_isk(ref)) {
  1205. IRRef tmp = refk; refk = ref; ref = tmp;
  1206. } else if (!irref_isk(refk)) {
  1207. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
  1208. IRIns *irr = IR(ir->op2);
  1209. if (ra_hasreg(irr->r)) {
  1210. ra_noweak(as, irr->r);
  1211. right = irr->r;
  1212. } else if (mayfuse(as, irr->op2) &&
  1213. irr->o == IR_ADD && irref_isk(irr->op2) &&
  1214. checki16(ofs + IR(irr->op2)->i)) {
  1215. ofs += IR(irr->op2)->i;
  1216. right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));
  1217. } else {
  1218. right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left));
  1219. }
  1220. emit_tsi(as, MIPSI_ADDIU, dest, dest, ofs);
  1221. emit_dst(as, MIPSI_ADDU, dest, left, right);
  1222. return;
  1223. }
  1224. r = ra_alloc1(as, ref, RSET_GPR);
  1225. ofs += IR(refk)->i;
  1226. if (checki16(ofs))
  1227. emit_tsi(as, MIPSI_ADDIU, dest, r, ofs);
  1228. else
  1229. emit_dst(as, MIPSI_ADDU, dest, r,
  1230. ra_allock(as, ofs, rset_exclude(RSET_GPR, r)));
  1231. #else
  1232. RegSet allow = RSET_GPR;
  1233. Reg dest = ra_dest(as, ir, allow);
  1234. Reg base = ra_alloc1(as, ir->op1, allow);
  1235. IRIns *irr = IR(ir->op2);
  1236. int32_t ofs = sizeof(GCstr);
  1237. rset_clear(allow, base);
  1238. if (irref_isk(ir->op2) && checki16(ofs + irr->i)) {
  1239. emit_tsi(as, MIPSI_DADDIU, dest, base, ofs + irr->i);
  1240. } else {
  1241. emit_tsi(as, MIPSI_DADDIU, dest, dest, ofs);
  1242. emit_dst(as, MIPSI_DADDU, dest, base, ra_alloc1(as, ir->op2, allow));
  1243. }
  1244. #endif
  1245. }
  1246. /* -- Loads and stores ---------------------------------------------------- */
  1247. static MIPSIns asm_fxloadins(ASMState *as, IRIns *ir)
  1248. {
  1249. UNUSED(as);
  1250. switch (irt_type(ir->t)) {
  1251. case IRT_I8: return MIPSI_LB;
  1252. case IRT_U8: return MIPSI_LBU;
  1253. case IRT_I16: return MIPSI_LH;
  1254. case IRT_U16: return MIPSI_LHU;
  1255. case IRT_NUM:
  1256. lj_assertA(!LJ_SOFTFP32, "unsplit FP op");
  1257. if (!LJ_SOFTFP) return MIPSI_LDC1;
  1258. /* fallthrough */
  1259. case IRT_FLOAT: if (!LJ_SOFTFP) return MIPSI_LWC1;
  1260. /* fallthrough */
  1261. default: return (LJ_64 && irt_is64(ir->t)) ? MIPSI_LD : MIPSI_LW;
  1262. }
  1263. }
  1264. static MIPSIns asm_fxstoreins(ASMState *as, IRIns *ir)
  1265. {
  1266. UNUSED(as);
  1267. switch (irt_type(ir->t)) {
  1268. case IRT_I8: case IRT_U8: return MIPSI_SB;
  1269. case IRT_I16: case IRT_U16: return MIPSI_SH;
  1270. case IRT_NUM:
  1271. lj_assertA(!LJ_SOFTFP32, "unsplit FP op");
  1272. if (!LJ_SOFTFP) return MIPSI_SDC1;
  1273. /* fallthrough */
  1274. case IRT_FLOAT: if (!LJ_SOFTFP) return MIPSI_SWC1;
  1275. /* fallthrough */
  1276. default: return (LJ_64 && irt_is64(ir->t)) ? MIPSI_SD : MIPSI_SW;
  1277. }
  1278. }
  1279. static void asm_fload(ASMState *as, IRIns *ir)
  1280. {
  1281. Reg dest = ra_dest(as, ir, RSET_GPR);
  1282. MIPSIns mi = asm_fxloadins(as, ir);
  1283. Reg idx;
  1284. int32_t ofs;
  1285. if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */
  1286. idx = RID_JGL;
  1287. ofs = (ir->op2 << 2) - 32768 - GG_OFS(g);
  1288. } else {
  1289. idx = ra_alloc1(as, ir->op1, RSET_GPR);
  1290. if (ir->op2 == IRFL_TAB_ARRAY) {
  1291. ofs = asm_fuseabase(as, ir->op1);
  1292. if (ofs) { /* Turn the t->array load into an add for colocated arrays. */
  1293. emit_tsi(as, MIPSI_AADDIU, dest, idx, ofs);
  1294. return;
  1295. }
  1296. }
  1297. ofs = field_ofs[ir->op2];
  1298. lj_assertA(!irt_isfp(ir->t), "bad FP FLOAD");
  1299. }
  1300. emit_tsi(as, mi, dest, idx, ofs);
  1301. }
  1302. static void asm_fstore(ASMState *as, IRIns *ir)
  1303. {
  1304. if (ir->r != RID_SINK) {
  1305. Reg src = ra_alloc1z(as, ir->op2, RSET_GPR);
  1306. IRIns *irf = IR(ir->op1);
  1307. Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));
  1308. int32_t ofs = field_ofs[irf->op2];
  1309. MIPSIns mi = asm_fxstoreins(as, ir);
  1310. lj_assertA(!irt_isfp(ir->t), "bad FP FSTORE");
  1311. emit_tsi(as, mi, src, idx, ofs);
  1312. }
  1313. }
  1314. static void asm_xload(ASMState *as, IRIns *ir)
  1315. {
  1316. Reg dest = ra_dest(as, ir,
  1317. (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);
  1318. lj_assertA(LJ_TARGET_UNALIGNED || !(ir->op2 & IRXLOAD_UNALIGNED),
  1319. "unaligned XLOAD");
  1320. asm_fusexref(as, asm_fxloadins(as, ir), dest, ir->op1, RSET_GPR, 0);
  1321. }
  1322. static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)
  1323. {
  1324. if (ir->r != RID_SINK) {
  1325. Reg src = ra_alloc1z(as, ir->op2,
  1326. (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);
  1327. asm_fusexref(as, asm_fxstoreins(as, ir), src, ir->op1,
  1328. rset_exclude(RSET_GPR, src), ofs);
  1329. }
  1330. }
  1331. #define asm_xstore(as, ir) asm_xstore_(as, ir, 0)
  1332. static void asm_ahuvload(ASMState *as, IRIns *ir)
  1333. {
  1334. int hiop = (LJ_SOFTFP32 && (ir+1)->o == IR_HIOP);
  1335. Reg dest = RID_NONE, type = RID_TMP, idx;
  1336. RegSet allow = RSET_GPR;
  1337. int32_t ofs = 0;
  1338. IRType1 t = ir->t;
  1339. if (hiop) {
  1340. t.irt = IRT_NUM;
  1341. if (ra_used(ir+1)) {
  1342. type = ra_dest(as, ir+1, allow);
  1343. rset_clear(allow, type);
  1344. }
  1345. }
  1346. if (ra_used(ir)) {
  1347. lj_assertA((LJ_SOFTFP32 ? 0 : irt_isnum(ir->t)) ||
  1348. irt_isint(ir->t) || irt_isaddr(ir->t),
  1349. "bad load type %d", irt_type(ir->t));
  1350. dest = ra_dest(as, ir, (!LJ_SOFTFP && irt_isnum(t)) ? RSET_FPR : allow);
  1351. rset_clear(allow, dest);
  1352. #if LJ_64
  1353. if (irt_isaddr(t))
  1354. emit_tsml(as, MIPSI_DEXTM, dest, dest, 14, 0);
  1355. else if (irt_isint(t))
  1356. emit_dta(as, MIPSI_SLL, dest, dest, 0);
  1357. #endif
  1358. }
  1359. idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
  1360. if (ir->o == IR_VLOAD) ofs += 8 * ir->op2;
  1361. rset_clear(allow, idx);
  1362. if (irt_isnum(t)) {
  1363. asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
  1364. emit_tsi(as, MIPSI_SLTIU, RID_TMP, type, (int32_t)LJ_TISNUM);
  1365. } else {
  1366. asm_guard(as, MIPSI_BNE, type,
  1367. ra_allock(as, (int32_t)irt_toitype(t), allow));
  1368. }
  1369. #if LJ_32
  1370. if (ra_hasreg(dest)) {
  1371. if (!LJ_SOFTFP && irt_isnum(t))
  1372. emit_hsi(as, MIPSI_LDC1, dest, idx, ofs);
  1373. else
  1374. emit_tsi(as, MIPSI_LW, dest, idx, ofs+(LJ_BE?4:0));
  1375. }
  1376. emit_tsi(as, MIPSI_LW, type, idx, ofs+(LJ_BE?0:4));
  1377. #else
  1378. if (ra_hasreg(dest)) {
  1379. if (!LJ_SOFTFP && irt_isnum(t)) {
  1380. emit_hsi(as, MIPSI_LDC1, dest, idx, ofs);
  1381. dest = type;
  1382. }
  1383. } else {
  1384. dest = type;
  1385. }
  1386. emit_dta(as, MIPSI_DSRA32, type, dest, 15);
  1387. emit_tsi(as, MIPSI_LD, dest, idx, ofs);
  1388. #endif
  1389. }
  1390. static void asm_ahustore(ASMState *as, IRIns *ir)
  1391. {
  1392. RegSet allow = RSET_GPR;
  1393. Reg idx, src = RID_NONE, type = RID_NONE;
  1394. int32_t ofs = 0;
  1395. if (ir->r == RID_SINK)
  1396. return;
  1397. if (!LJ_SOFTFP32 && irt_isnum(ir->t)) {
  1398. src = ra_alloc1(as, ir->op2, LJ_SOFTFP ? RSET_GPR : RSET_FPR);
  1399. idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
  1400. emit_hsi(as, LJ_SOFTFP ? MIPSI_SD : MIPSI_SDC1, src, idx, ofs);
  1401. } else {
  1402. #if LJ_32
  1403. if (!irt_ispri(ir->t)) {
  1404. src = ra_alloc1(as, ir->op2, allow);
  1405. rset_clear(allow, src);
  1406. }
  1407. if (LJ_SOFTFP && (ir+1)->o == IR_HIOP)
  1408. type = ra_alloc1(as, (ir+1)->op2, allow);
  1409. else
  1410. type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
  1411. rset_clear(allow, type);
  1412. idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
  1413. if (ra_hasreg(src))
  1414. emit_tsi(as, MIPSI_SW, src, idx, ofs+(LJ_BE?4:0));
  1415. emit_tsi(as, MIPSI_SW, type, idx, ofs+(LJ_BE?0:4));
  1416. #else
  1417. Reg tmp = RID_TMP;
  1418. if (irt_ispri(ir->t)) {
  1419. tmp = ra_allock(as, ~((int64_t)~irt_toitype(ir->t) << 47), allow);
  1420. rset_clear(allow, tmp);
  1421. } else {
  1422. src = ra_alloc1(as, ir->op2, allow);
  1423. rset_clear(allow, src);
  1424. type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47, allow);
  1425. rset_clear(allow, type);
  1426. }
  1427. idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
  1428. emit_tsi(as, MIPSI_SD, tmp, idx, ofs);
  1429. if (ra_hasreg(src)) {
  1430. if (irt_isinteger(ir->t)) {
  1431. emit_dst(as, MIPSI_DADDU, tmp, tmp, type);
  1432. emit_tsml(as, MIPSI_DEXT, tmp, src, 31, 0);
  1433. } else {
  1434. emit_dst(as, MIPSI_DADDU, tmp, src, type);
  1435. }
  1436. }
  1437. #endif
  1438. }
  1439. }
  1440. static void asm_sload(ASMState *as, IRIns *ir)
  1441. {
  1442. Reg dest = RID_NONE, type = RID_NONE, base;
  1443. RegSet allow = RSET_GPR;
  1444. IRType1 t = ir->t;
  1445. #if LJ_32
  1446. int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0);
  1447. int hiop = (LJ_SOFTFP32 && (ir+1)->o == IR_HIOP);
  1448. if (hiop)
  1449. t.irt = IRT_NUM;
  1450. #else
  1451. int32_t ofs = 8*((int32_t)ir->op1-2);
  1452. #endif
  1453. lj_assertA(!(ir->op2 & IRSLOAD_PARENT),
  1454. "bad parent SLOAD"); /* Handled by asm_head_side(). */
  1455. lj_assertA(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK),
  1456. "inconsistent SLOAD variant");
  1457. #if LJ_SOFTFP32
  1458. lj_assertA(!(ir->op2 & IRSLOAD_CONVERT),
  1459. "unsplit SLOAD convert"); /* Handled by LJ_SOFTFP SPLIT. */
  1460. if (hiop && ra_used(ir+1)) {
  1461. type = ra_dest(as, ir+1, allow);
  1462. rset_clear(allow, type);
  1463. }
  1464. #else
  1465. if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {
  1466. dest = ra_scratch(as, LJ_SOFTFP ? allow : RSET_FPR);
  1467. asm_tointg(as, ir, dest);
  1468. t.irt = IRT_NUM; /* Continue with a regular number type check. */
  1469. } else
  1470. #endif
  1471. if (ra_used(ir)) {
  1472. lj_assertA((LJ_SOFTFP32 ? 0 : irt_isnum(ir->t)) ||
  1473. irt_isint(ir->t) || irt_isaddr(ir->t),
  1474. "bad SLOAD type %d", irt_type(ir->t));
  1475. dest = ra_dest(as, ir, (!LJ_SOFTFP && irt_isnum(t)) ? RSET_FPR : allow);
  1476. rset_clear(allow, dest);
  1477. base = ra_alloc1(as, REF_BASE, allow);
  1478. rset_clear(allow, base);
  1479. if (!LJ_SOFTFP32 && (ir->op2 & IRSLOAD_CONVERT)) {
  1480. if (irt_isint(t)) {
  1481. Reg tmp = ra_scratch(as, LJ_SOFTFP ? RSET_GPR : RSET_FPR);
  1482. #if LJ_SOFTFP
  1483. ra_evictset(as, rset_exclude(RSET_SCRATCH, dest));
  1484. ra_destreg(as, ir, RID_RET);
  1485. emit_call(as, (void *)lj_ir_callinfo[IRCALL_softfp_d2i].func, 0);
  1486. if (tmp != REGARG_FIRSTGPR)
  1487. emit_move(as, REGARG_FIRSTGPR, tmp);
  1488. #else
  1489. emit_tg(as, MIPSI_MFC1, dest, tmp);
  1490. emit_fg(as, MIPSI_TRUNC_W_D, tmp, tmp);
  1491. #endif
  1492. dest = tmp;
  1493. t.irt = IRT_NUM; /* Check for original type. */
  1494. } else {
  1495. Reg tmp = ra_scratch(as, RSET_GPR);
  1496. #if LJ_SOFTFP
  1497. ra_evictset(as, rset_exclude(RSET_SCRATCH, dest));
  1498. ra_destreg(as, ir, RID_RET);
  1499. emit_call(as, (void *)lj_ir_callinfo[IRCALL_softfp_i2d].func, 0);
  1500. emit_dta(as, MIPSI_SLL, REGARG_FIRSTGPR, tmp, 0);
  1501. #else
  1502. emit_fg(as, MIPSI_CVT_D_W, dest, dest);
  1503. emit_tg(as, MIPSI_MTC1, tmp, dest);
  1504. #endif
  1505. dest = tmp;
  1506. t.irt = IRT_INT; /* Check for original type. */
  1507. }
  1508. }
  1509. #if LJ_64
  1510. else if (irt_isaddr(t)) {
  1511. /* Clear type from pointers. */
  1512. emit_tsml(as, MIPSI_DEXTM, dest, dest, 14, 0);
  1513. } else if (irt_isint(t) && (ir->op2 & IRSLOAD_TYPECHECK)) {
  1514. /* Sign-extend integers. */
  1515. emit_dta(as, MIPSI_SLL, dest, dest, 0);
  1516. }
  1517. #endif
  1518. goto dotypecheck;
  1519. }
  1520. base = ra_alloc1(as, REF_BASE, allow);
  1521. rset_clear(allow, base);
  1522. dotypecheck:
  1523. #if LJ_32
  1524. if ((ir->op2 & IRSLOAD_TYPECHECK)) {
  1525. if (ra_noreg(type))
  1526. type = RID_TMP;
  1527. if (irt_isnum(t)) {
  1528. asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
  1529. emit_tsi(as, MIPSI_SLTIU, RID_TMP, type, (int32_t)LJ_TISNUM);
  1530. } else {
  1531. Reg ktype = ra_allock(as, (ir->op2 & IRSLOAD_KEYINDEX) ? LJ_KEYINDEX : irt_toitype(t), allow);
  1532. asm_guard(as, MIPSI_BNE, type, ktype);
  1533. }
  1534. }
  1535. if (ra_hasreg(dest)) {
  1536. if (!LJ_SOFTFP && irt_isnum(t))
  1537. emit_hsi(as, MIPSI_LDC1, dest, base, ofs);
  1538. else
  1539. emit_tsi(as, MIPSI_LW, dest, base, ofs ^ (LJ_BE?4:0));
  1540. }
  1541. if (ra_hasreg(type))
  1542. emit_tsi(as, MIPSI_LW, type, base, ofs ^ (LJ_BE?0:4));
  1543. #else
  1544. if ((ir->op2 & IRSLOAD_TYPECHECK)) {
  1545. type = dest < RID_MAX_GPR ? dest : RID_TMP;
  1546. if (irt_ispri(t)) {
  1547. asm_guard(as, MIPSI_BNE, type,
  1548. ra_allock(as, ~((int64_t)~irt_toitype(t) << 47) , allow));
  1549. } else if ((ir->op2 & IRSLOAD_KEYINDEX)) {
  1550. asm_guard(as, MIPSI_BNE, RID_TMP,
  1551. ra_allock(as, (int32_t)LJ_KEYINDEX, allow));
  1552. emit_dta(as, MIPSI_DSRA32, RID_TMP, type, 0);
  1553. } else {
  1554. if (irt_isnum(t)) {
  1555. asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO);
  1556. emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)LJ_TISNUM);
  1557. if (!LJ_SOFTFP && ra_hasreg(dest))
  1558. emit_hsi(as, MIPSI_LDC1, dest, base, ofs);
  1559. } else {
  1560. asm_guard(as, MIPSI_BNE, RID_TMP,
  1561. ra_allock(as, (int32_t)irt_toitype(t), allow));
  1562. }
  1563. emit_dta(as, MIPSI_DSRA32, RID_TMP, type, 15);
  1564. }
  1565. emit_tsi(as, MIPSI_LD, type, base, ofs);
  1566. } else if (ra_hasreg(dest)) {
  1567. if (!LJ_SOFTFP && irt_isnum(t))
  1568. emit_hsi(as, MIPSI_LDC1, dest, base, ofs);
  1569. else
  1570. emit_tsi(as, irt_isint(t) ? MIPSI_LW : MIPSI_LD, dest, base,
  1571. ofs ^ ((LJ_BE && irt_isint(t)) ? 4 : 0));
  1572. }
  1573. #endif
  1574. }
  1575. /* -- Allocations --------------------------------------------------------- */
  1576. #if LJ_HASFFI
  1577. static void asm_cnew(ASMState *as, IRIns *ir)
  1578. {
  1579. CTState *cts = ctype_ctsG(J2G(as->J));
  1580. CTypeID id = (CTypeID)IR(ir->op1)->i;
  1581. CTSize sz;
  1582. CTInfo info = lj_ctype_info(cts, id, &sz);
  1583. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
  1584. IRRef args[4];
  1585. RegSet drop = RSET_SCRATCH;
  1586. lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL),
  1587. "bad CNEW/CNEWI operands");
  1588. as->gcsteps++;
  1589. if (ra_hasreg(ir->r))
  1590. rset_clear(drop, ir->r); /* Dest reg handled below. */
  1591. ra_evictset(as, drop);
  1592. if (ra_used(ir))
  1593. ra_destreg(as, ir, RID_RET); /* GCcdata * */
  1594. /* Initialize immutable cdata object. */
  1595. if (ir->o == IR_CNEWI) {
  1596. RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
  1597. #if LJ_32
  1598. int32_t ofs = sizeof(GCcdata);
  1599. if (sz == 8) {
  1600. ofs += 4;
  1601. lj_assertA((ir+1)->o == IR_HIOP, "expected HIOP for CNEWI");
  1602. if (LJ_LE) ir++;
  1603. }
  1604. for (;;) {
  1605. Reg r = ra_alloc1z(as, ir->op2, allow);
  1606. emit_tsi(as, MIPSI_SW, r, RID_RET, ofs);
  1607. rset_clear(allow, r);
  1608. if (ofs == sizeof(GCcdata)) break;
  1609. ofs -= 4; if (LJ_BE) ir++; else ir--;
  1610. }
  1611. #else
  1612. emit_tsi(as, sz == 8 ? MIPSI_SD : MIPSI_SW, ra_alloc1(as, ir->op2, allow),
  1613. RID_RET, sizeof(GCcdata));
  1614. #endif
  1615. lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz);
  1616. } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */
  1617. ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];
  1618. args[0] = ASMREF_L; /* lua_State *L */
  1619. args[1] = ir->op1; /* CTypeID id */
  1620. args[2] = ir->op2; /* CTSize sz */
  1621. args[3] = ASMREF_TMP1; /* CTSize align */
  1622. asm_gencall(as, ci, args);
  1623. emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));
  1624. return;
  1625. }
  1626. /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */
  1627. emit_tsi(as, MIPSI_SB, RID_RET+1, RID_RET, offsetof(GCcdata, gct));
  1628. emit_tsi(as, MIPSI_SH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid));
  1629. emit_ti(as, MIPSI_LI, RID_RET+1, ~LJ_TCDATA);
  1630. emit_ti(as, MIPSI_LI, RID_TMP, id); /* Lower 16 bit used. Sign-ext ok. */
  1631. args[0] = ASMREF_L; /* lua_State *L */
  1632. args[1] = ASMREF_TMP1; /* MSize size */
  1633. asm_gencall(as, ci, args);
  1634. ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
  1635. ra_releasetmp(as, ASMREF_TMP1));
  1636. }
  1637. #endif
  1638. /* -- Write barriers ------------------------------------------------------ */
  1639. static void asm_tbar(ASMState *as, IRIns *ir)
  1640. {
  1641. Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
  1642. Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));
  1643. Reg link = RID_TMP;
  1644. MCLabel l_end = emit_label(as);
  1645. emit_tsi(as, MIPSI_AS, link, tab, (int32_t)offsetof(GCtab, gclist));
  1646. emit_tsi(as, MIPSI_SB, mark, tab, (int32_t)offsetof(GCtab, marked));
  1647. emit_setgl(as, tab, gc.grayagain);
  1648. emit_getgl(as, link, gc.grayagain);
  1649. emit_dst(as, MIPSI_XOR, mark, mark, RID_TMP); /* Clear black bit. */
  1650. emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);
  1651. emit_tsi(as, MIPSI_ANDI, RID_TMP, mark, LJ_GC_BLACK);
  1652. emit_tsi(as, MIPSI_LBU, mark, tab, (int32_t)offsetof(GCtab, marked));
  1653. }
  1654. static void asm_obar(ASMState *as, IRIns *ir)
  1655. {
  1656. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
  1657. IRRef args[2];
  1658. MCLabel l_end;
  1659. Reg obj, val, tmp;
  1660. /* No need for other object barriers (yet). */
  1661. lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type");
  1662. ra_evictset(as, RSET_SCRATCH);
  1663. l_end = emit_label(as);
  1664. args[0] = ASMREF_TMP1; /* global_State *g */
  1665. args[1] = ir->op1; /* TValue *tv */
  1666. asm_gencall(as, ci, args);
  1667. emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
  1668. obj = IR(ir->op1)->r;
  1669. tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
  1670. emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);
  1671. emit_tsi(as, MIPSI_ANDI, tmp, tmp, LJ_GC_BLACK);
  1672. emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end);
  1673. emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, LJ_GC_WHITES);
  1674. val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));
  1675. emit_tsi(as, MIPSI_LBU, tmp, obj,
  1676. (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv));
  1677. emit_tsi(as, MIPSI_LBU, RID_TMP, val, (int32_t)offsetof(GChead, marked));
  1678. }
  1679. /* -- Arithmetic and logic operations ------------------------------------- */
  1680. #if !LJ_SOFTFP
  1681. static void asm_fparith(ASMState *as, IRIns *ir, MIPSIns mi)
  1682. {
  1683. Reg dest = ra_dest(as, ir, RSET_FPR);
  1684. Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  1685. right = (left >> 8); left &= 255;
  1686. emit_fgh(as, mi, dest, left, right);
  1687. }
  1688. static void asm_fpunary(ASMState *as, IRIns *ir, MIPSIns mi)
  1689. {
  1690. Reg dest = ra_dest(as, ir, RSET_FPR);
  1691. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
  1692. emit_fg(as, mi, dest, left);
  1693. }
  1694. #endif
  1695. #if !LJ_SOFTFP32
  1696. static void asm_fpmath(ASMState *as, IRIns *ir)
  1697. {
  1698. #if !LJ_SOFTFP
  1699. if (ir->op2 <= IRFPM_TRUNC)
  1700. asm_callround(as, ir, IRCALL_lj_vm_floor + ir->op2);
  1701. else if (ir->op2 == IRFPM_SQRT)
  1702. asm_fpunary(as, ir, MIPSI_SQRT_D);
  1703. else
  1704. #endif
  1705. asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2);
  1706. }
  1707. #endif
  1708. #if !LJ_SOFTFP
  1709. #define asm_fpadd(as, ir) asm_fparith(as, ir, MIPSI_ADD_D)
  1710. #define asm_fpsub(as, ir) asm_fparith(as, ir, MIPSI_SUB_D)
  1711. #define asm_fpmul(as, ir) asm_fparith(as, ir, MIPSI_MUL_D)
  1712. #elif LJ_64 /* && LJ_SOFTFP */
  1713. #define asm_fpadd(as, ir) asm_callid(as, ir, IRCALL_softfp_add)
  1714. #define asm_fpsub(as, ir) asm_callid(as, ir, IRCALL_softfp_sub)
  1715. #define asm_fpmul(as, ir) asm_callid(as, ir, IRCALL_softfp_mul)
  1716. #endif
  1717. static void asm_add(ASMState *as, IRIns *ir)
  1718. {
  1719. IRType1 t = ir->t;
  1720. #if !LJ_SOFTFP32
  1721. if (irt_isnum(t)) {
  1722. asm_fpadd(as, ir);
  1723. } else
  1724. #endif
  1725. {
  1726. /* TODO MIPSR6: Fuse ADD(BSHL(a,1-4),b) or ADD(ADD(a,a),b) to MIPSI_ALSA. */
  1727. Reg dest = ra_dest(as, ir, RSET_GPR);
  1728. Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1729. if (irref_isk(ir->op2)) {
  1730. intptr_t k = get_kval(as, ir->op2);
  1731. if (checki16(k)) {
  1732. emit_tsi(as, (LJ_64 && irt_is64(t)) ? MIPSI_DADDIU : MIPSI_ADDIU, dest,
  1733. left, k);
  1734. return;
  1735. }
  1736. }
  1737. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1738. emit_dst(as, (LJ_64 && irt_is64(t)) ? MIPSI_DADDU : MIPSI_ADDU, dest,
  1739. left, right);
  1740. }
  1741. }
  1742. static void asm_sub(ASMState *as, IRIns *ir)
  1743. {
  1744. #if !LJ_SOFTFP32
  1745. if (irt_isnum(ir->t)) {
  1746. asm_fpsub(as, ir);
  1747. } else
  1748. #endif
  1749. {
  1750. Reg dest = ra_dest(as, ir, RSET_GPR);
  1751. Reg right, left = ra_alloc2(as, ir, RSET_GPR);
  1752. right = (left >> 8); left &= 255;
  1753. emit_dst(as, (LJ_64 && irt_is64(ir->t)) ? MIPSI_DSUBU : MIPSI_SUBU, dest,
  1754. left, right);
  1755. }
  1756. }
  1757. static void asm_mul(ASMState *as, IRIns *ir)
  1758. {
  1759. #if !LJ_SOFTFP32
  1760. if (irt_isnum(ir->t)) {
  1761. asm_fpmul(as, ir);
  1762. } else
  1763. #endif
  1764. {
  1765. Reg dest = ra_dest(as, ir, RSET_GPR);
  1766. Reg right, left = ra_alloc2(as, ir, RSET_GPR);
  1767. right = (left >> 8); left &= 255;
  1768. if (LJ_64 && irt_is64(ir->t)) {
  1769. #if !LJ_TARGET_MIPSR6
  1770. emit_dst(as, MIPSI_MFLO, dest, 0, 0);
  1771. emit_dst(as, MIPSI_DMULT, 0, left, right);
  1772. #else
  1773. emit_dst(as, MIPSI_DMUL, dest, left, right);
  1774. #endif
  1775. } else {
  1776. emit_dst(as, MIPSI_MUL, dest, left, right);
  1777. }
  1778. }
  1779. }
  1780. #if !LJ_SOFTFP32
  1781. static void asm_fpdiv(ASMState *as, IRIns *ir)
  1782. {
  1783. #if !LJ_SOFTFP
  1784. asm_fparith(as, ir, MIPSI_DIV_D);
  1785. #else
  1786. asm_callid(as, ir, IRCALL_softfp_div);
  1787. #endif
  1788. }
  1789. #endif
  1790. static void asm_neg(ASMState *as, IRIns *ir)
  1791. {
  1792. #if !LJ_SOFTFP
  1793. if (irt_isnum(ir->t)) {
  1794. asm_fpunary(as, ir, MIPSI_NEG_D);
  1795. } else
  1796. #elif LJ_64 /* && LJ_SOFTFP */
  1797. if (irt_isnum(ir->t)) {
  1798. Reg dest = ra_dest(as, ir, RSET_GPR);
  1799. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1800. emit_dst(as, MIPSI_XOR, dest, left,
  1801. ra_allock(as, 0x8000000000000000ll, rset_exclude(RSET_GPR, dest)));
  1802. } else
  1803. #endif
  1804. {
  1805. Reg dest = ra_dest(as, ir, RSET_GPR);
  1806. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1807. emit_dst(as, (LJ_64 && irt_is64(ir->t)) ? MIPSI_DSUBU : MIPSI_SUBU, dest,
  1808. RID_ZERO, left);
  1809. }
  1810. }
  1811. #if !LJ_SOFTFP
  1812. #define asm_abs(as, ir) asm_fpunary(as, ir, MIPSI_ABS_D)
  1813. #elif LJ_64 /* && LJ_SOFTFP */
  1814. static void asm_abs(ASMState *as, IRIns *ir)
  1815. {
  1816. Reg dest = ra_dest(as, ir, RSET_GPR);
  1817. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1818. emit_tsml(as, MIPSI_DEXTM, dest, left, 30, 0);
  1819. }
  1820. #endif
  1821. static void asm_arithov(ASMState *as, IRIns *ir)
  1822. {
  1823. /* TODO MIPSR6: bovc/bnvc. Caveat: no delay slot to load RID_TMP. */
  1824. Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR);
  1825. lj_assertA(!irt_is64(ir->t), "bad usage");
  1826. if (irref_isk(ir->op2)) {
  1827. int k = IR(ir->op2)->i;
  1828. if (ir->o == IR_SUBOV) k = (int)(~(unsigned int)k+1u);
  1829. if (checki16(k)) { /* (dest < left) == (k >= 0 ? 1 : 0) */
  1830. left = ra_alloc1(as, ir->op1, RSET_GPR);
  1831. asm_guard(as, k >= 0 ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
  1832. emit_dst(as, MIPSI_SLT, RID_TMP, dest, dest == left ? RID_TMP : left);
  1833. emit_tsi(as, MIPSI_ADDIU, dest, left, k);
  1834. if (dest == left) emit_move(as, RID_TMP, left);
  1835. return;
  1836. }
  1837. }
  1838. left = ra_alloc2(as, ir, RSET_GPR);
  1839. right = (left >> 8); left &= 255;
  1840. tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left),
  1841. right), dest));
  1842. asm_guard(as, MIPSI_BLTZ, RID_TMP, 0);
  1843. emit_dst(as, MIPSI_AND, RID_TMP, RID_TMP, tmp);
  1844. if (ir->o == IR_ADDOV) { /* ((dest^left) & (dest^right)) < 0 */
  1845. emit_dst(as, MIPSI_XOR, RID_TMP, dest, dest == right ? RID_TMP : right);
  1846. } else { /* ((dest^left) & (dest^~right)) < 0 */
  1847. emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, dest);
  1848. emit_dst(as, MIPSI_NOR, RID_TMP, dest == right ? RID_TMP : right, RID_ZERO);
  1849. }
  1850. emit_dst(as, MIPSI_XOR, tmp, dest, dest == left ? RID_TMP : left);
  1851. emit_dst(as, ir->o == IR_ADDOV ? MIPSI_ADDU : MIPSI_SUBU, dest, left, right);
  1852. if (dest == left || dest == right)
  1853. emit_move(as, RID_TMP, dest == left ? left : right);
  1854. }
  1855. #define asm_addov(as, ir) asm_arithov(as, ir)
  1856. #define asm_subov(as, ir) asm_arithov(as, ir)
  1857. static void asm_mulov(ASMState *as, IRIns *ir)
  1858. {
  1859. Reg dest = ra_dest(as, ir, RSET_GPR);
  1860. Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR);
  1861. right = (left >> 8); left &= 255;
  1862. tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left),
  1863. right), dest));
  1864. asm_guard(as, MIPSI_BNE, RID_TMP, tmp);
  1865. emit_dta(as, MIPSI_SRA, RID_TMP, dest, 31);
  1866. #if !LJ_TARGET_MIPSR6
  1867. emit_dst(as, MIPSI_MFHI, tmp, 0, 0);
  1868. emit_dst(as, MIPSI_MFLO, dest, 0, 0);
  1869. emit_dst(as, MIPSI_MULT, 0, left, right);
  1870. #else
  1871. emit_dst(as, MIPSI_MUL, dest, left, right);
  1872. emit_dst(as, MIPSI_MUH, tmp, left, right);
  1873. #endif
  1874. }
  1875. #if LJ_32 && LJ_HASFFI
  1876. static void asm_add64(ASMState *as, IRIns *ir)
  1877. {
  1878. Reg dest = ra_dest(as, ir, RSET_GPR);
  1879. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
  1880. if (irref_isk(ir->op2)) {
  1881. int32_t k = IR(ir->op2)->i;
  1882. if (k == 0) {
  1883. emit_dst(as, MIPSI_ADDU, dest, left, RID_TMP);
  1884. goto loarith;
  1885. } else if (checki16(k)) {
  1886. emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP);
  1887. emit_tsi(as, MIPSI_ADDIU, dest, left, k);
  1888. goto loarith;
  1889. }
  1890. }
  1891. emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP);
  1892. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1893. emit_dst(as, MIPSI_ADDU, dest, left, right);
  1894. loarith:
  1895. ir--;
  1896. dest = ra_dest(as, ir, RSET_GPR);
  1897. left = ra_alloc1(as, ir->op1, RSET_GPR);
  1898. if (irref_isk(ir->op2)) {
  1899. int32_t k = IR(ir->op2)->i;
  1900. if (k == 0) {
  1901. if (dest != left)
  1902. emit_move(as, dest, left);
  1903. return;
  1904. } else if (checki16(k)) {
  1905. if (dest == left) {
  1906. Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, left));
  1907. emit_move(as, dest, tmp);
  1908. dest = tmp;
  1909. }
  1910. emit_dst(as, MIPSI_SLTU, RID_TMP, dest, left);
  1911. emit_tsi(as, MIPSI_ADDIU, dest, left, k);
  1912. return;
  1913. }
  1914. }
  1915. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  1916. if (dest == left && dest == right) {
  1917. Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));
  1918. emit_move(as, dest, tmp);
  1919. dest = tmp;
  1920. }
  1921. emit_dst(as, MIPSI_SLTU, RID_TMP, dest, dest == left ? right : left);
  1922. emit_dst(as, MIPSI_ADDU, dest, left, right);
  1923. }
  1924. static void asm_sub64(ASMState *as, IRIns *ir)
  1925. {
  1926. Reg dest = ra_dest(as, ir, RSET_GPR);
  1927. Reg right, left = ra_alloc2(as, ir, RSET_GPR);
  1928. right = (left >> 8); left &= 255;
  1929. emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP);
  1930. emit_dst(as, MIPSI_SUBU, dest, left, right);
  1931. ir--;
  1932. dest = ra_dest(as, ir, RSET_GPR);
  1933. left = ra_alloc2(as, ir, RSET_GPR);
  1934. right = (left >> 8); left &= 255;
  1935. if (dest == left) {
  1936. Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));
  1937. emit_move(as, dest, tmp);
  1938. dest = tmp;
  1939. }
  1940. emit_dst(as, MIPSI_SLTU, RID_TMP, left, dest);
  1941. emit_dst(as, MIPSI_SUBU, dest, left, right);
  1942. }
  1943. static void asm_neg64(ASMState *as, IRIns *ir)
  1944. {
  1945. Reg dest = ra_dest(as, ir, RSET_GPR);
  1946. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1947. emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP);
  1948. emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left);
  1949. ir--;
  1950. dest = ra_dest(as, ir, RSET_GPR);
  1951. left = ra_alloc1(as, ir->op1, RSET_GPR);
  1952. emit_dst(as, MIPSI_SLTU, RID_TMP, RID_ZERO, dest);
  1953. emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left);
  1954. }
  1955. #endif
  1956. static void asm_bnot(ASMState *as, IRIns *ir)
  1957. {
  1958. Reg left, right, dest = ra_dest(as, ir, RSET_GPR);
  1959. IRIns *irl = IR(ir->op1);
  1960. if (mayfuse(as, ir->op1) && irl->o == IR_BOR) {
  1961. left = ra_alloc2(as, irl, RSET_GPR);
  1962. right = (left >> 8); left &= 255;
  1963. } else {
  1964. left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  1965. right = RID_ZERO;
  1966. }
  1967. emit_dst(as, MIPSI_NOR, dest, left, right);
  1968. }
  1969. static void asm_bswap(ASMState *as, IRIns *ir)
  1970. {
  1971. Reg dest = ra_dest(as, ir, RSET_GPR);
  1972. Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
  1973. #if LJ_32
  1974. if ((as->flags & JIT_F_MIPSXXR2)) {
  1975. emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16);
  1976. emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left);
  1977. } else {
  1978. Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), dest));
  1979. emit_dst(as, MIPSI_OR, dest, dest, tmp);
  1980. emit_dst(as, MIPSI_OR, dest, dest, RID_TMP);
  1981. emit_tsi(as, MIPSI_ANDI, dest, dest, 0xff00);
  1982. emit_dta(as, MIPSI_SLL, RID_TMP, RID_TMP, 8);
  1983. emit_dta(as, MIPSI_SRL, dest, left, 8);
  1984. emit_tsi(as, MIPSI_ANDI, RID_TMP, left, 0xff00);
  1985. emit_dst(as, MIPSI_OR, tmp, tmp, RID_TMP);
  1986. emit_dta(as, MIPSI_SRL, tmp, left, 24);
  1987. emit_dta(as, MIPSI_SLL, RID_TMP, left, 24);
  1988. }
  1989. #else
  1990. if (irt_is64(ir->t)) {
  1991. emit_dst(as, MIPSI_DSHD, dest, 0, RID_TMP);
  1992. emit_dst(as, MIPSI_DSBH, RID_TMP, 0, left);
  1993. } else {
  1994. emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16);
  1995. emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left);
  1996. }
  1997. #endif
  1998. }
  1999. static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)
  2000. {
  2001. Reg dest = ra_dest(as, ir, RSET_GPR);
  2002. Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  2003. if (irref_isk(ir->op2)) {
  2004. intptr_t k = get_kval(as, ir->op2);
  2005. if (checku16(k)) {
  2006. emit_tsi(as, mik, dest, left, k);
  2007. return;
  2008. }
  2009. }
  2010. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  2011. emit_dst(as, mi, dest, left, right);
  2012. }
  2013. #define asm_band(as, ir) asm_bitop(as, ir, MIPSI_AND, MIPSI_ANDI)
  2014. #define asm_bor(as, ir) asm_bitop(as, ir, MIPSI_OR, MIPSI_ORI)
  2015. #define asm_bxor(as, ir) asm_bitop(as, ir, MIPSI_XOR, MIPSI_XORI)
  2016. static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik)
  2017. {
  2018. Reg dest = ra_dest(as, ir, RSET_GPR);
  2019. if (irref_isk(ir->op2)) { /* Constant shifts. */
  2020. uint32_t shift = (uint32_t)IR(ir->op2)->i;
  2021. if (LJ_64 && irt_is64(ir->t)) mik |= (shift & 32) ? MIPSI_D32 : MIPSI_D;
  2022. emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR),
  2023. (shift & 31));
  2024. } else {
  2025. Reg right, left = ra_alloc2(as, ir, RSET_GPR);
  2026. right = (left >> 8); left &= 255;
  2027. if (LJ_64 && irt_is64(ir->t)) mi |= MIPSI_DV;
  2028. emit_dst(as, mi, dest, right, left); /* Shift amount is in rs. */
  2029. }
  2030. }
  2031. #define asm_bshl(as, ir) asm_bitshift(as, ir, MIPSI_SLLV, MIPSI_SLL)
  2032. #define asm_bshr(as, ir) asm_bitshift(as, ir, MIPSI_SRLV, MIPSI_SRL)
  2033. #define asm_bsar(as, ir) asm_bitshift(as, ir, MIPSI_SRAV, MIPSI_SRA)
  2034. #define asm_brol(as, ir) lj_assertA(0, "unexpected BROL")
  2035. static void asm_bror(ASMState *as, IRIns *ir)
  2036. {
  2037. if (LJ_64 || (as->flags & JIT_F_MIPSXXR2)) {
  2038. asm_bitshift(as, ir, MIPSI_ROTRV, MIPSI_ROTR);
  2039. } else {
  2040. Reg dest = ra_dest(as, ir, RSET_GPR);
  2041. if (irref_isk(ir->op2)) { /* Constant shifts. */
  2042. uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31);
  2043. Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
  2044. emit_rotr(as, dest, left, RID_TMP, shift);
  2045. } else {
  2046. Reg right, left = ra_alloc2(as, ir, RSET_GPR);
  2047. right = (left >> 8); left &= 255;
  2048. emit_dst(as, MIPSI_OR, dest, dest, RID_TMP);
  2049. emit_dst(as, MIPSI_SRLV, dest, right, left);
  2050. emit_dst(as, MIPSI_SLLV, RID_TMP, RID_TMP, left);
  2051. emit_dst(as, MIPSI_SUBU, RID_TMP, ra_allock(as, 32, RSET_GPR), right);
  2052. }
  2053. }
  2054. }
  2055. #if LJ_SOFTFP
  2056. static void asm_sfpmin_max(ASMState *as, IRIns *ir)
  2057. {
  2058. CCallInfo ci = lj_ir_callinfo[(IROp)ir->o == IR_MIN ? IRCALL_lj_vm_sfmin : IRCALL_lj_vm_sfmax];
  2059. #if LJ_64
  2060. IRRef args[2];
  2061. args[0] = ir->op1;
  2062. args[1] = ir->op2;
  2063. #else
  2064. IRRef args[4];
  2065. args[0^LJ_BE] = ir->op1;
  2066. args[1^LJ_BE] = (ir+1)->op1;
  2067. args[2^LJ_BE] = ir->op2;
  2068. args[3^LJ_BE] = (ir+1)->op2;
  2069. #endif
  2070. asm_setupresult(as, ir, &ci);
  2071. emit_call(as, (void *)ci.func, 0);
  2072. ci.func = NULL;
  2073. asm_gencall(as, &ci, args);
  2074. }
  2075. #endif
  2076. static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
  2077. {
  2078. if (!LJ_SOFTFP32 && irt_isnum(ir->t)) {
  2079. #if LJ_SOFTFP
  2080. asm_sfpmin_max(as, ir);
  2081. #else
  2082. Reg dest = ra_dest(as, ir, RSET_FPR);
  2083. Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  2084. right = (left >> 8); left &= 255;
  2085. #if !LJ_TARGET_MIPSR6
  2086. if (dest == left) {
  2087. emit_fg(as, MIPSI_MOVF_D, dest, right);
  2088. } else {
  2089. emit_fg(as, MIPSI_MOVT_D, dest, left);
  2090. if (dest != right) emit_fg(as, MIPSI_MOV_D, dest, right);
  2091. }
  2092. emit_fgh(as, MIPSI_C_OLT_D, 0, ismax ? right : left, ismax ? left : right);
  2093. #else
  2094. emit_fgh(as, ismax ? MIPSI_MAX_D : MIPSI_MIN_D, dest, left, right);
  2095. #endif
  2096. #endif
  2097. } else {
  2098. Reg dest = ra_dest(as, ir, RSET_GPR);
  2099. Reg right, left = ra_alloc2(as, ir, RSET_GPR);
  2100. right = (left >> 8); left &= 255;
  2101. if (left == right) {
  2102. if (dest != left) emit_move(as, dest, left);
  2103. } else {
  2104. #if !LJ_TARGET_MIPSR6
  2105. if (dest == left) {
  2106. emit_dst(as, MIPSI_MOVN, dest, right, RID_TMP);
  2107. } else {
  2108. emit_dst(as, MIPSI_MOVZ, dest, left, RID_TMP);
  2109. if (dest != right) emit_move(as, dest, right);
  2110. }
  2111. #else
  2112. emit_dst(as, MIPSI_OR, dest, dest, RID_TMP);
  2113. if (dest != right) {
  2114. emit_dst(as, MIPSI_SELNEZ, RID_TMP, right, RID_TMP);
  2115. emit_dst(as, MIPSI_SELEQZ, dest, left, RID_TMP);
  2116. } else {
  2117. emit_dst(as, MIPSI_SELEQZ, RID_TMP, left, RID_TMP);
  2118. emit_dst(as, MIPSI_SELNEZ, dest, right, RID_TMP);
  2119. }
  2120. #endif
  2121. emit_dst(as, MIPSI_SLT, RID_TMP,
  2122. ismax ? left : right, ismax ? right : left);
  2123. }
  2124. }
  2125. }
  2126. #define asm_min(as, ir) asm_min_max(as, ir, 0)
  2127. #define asm_max(as, ir) asm_min_max(as, ir, 1)
  2128. /* -- Comparisons --------------------------------------------------------- */
  2129. #if LJ_SOFTFP
  2130. /* SFP comparisons. */
  2131. static void asm_sfpcomp(ASMState *as, IRIns *ir)
  2132. {
  2133. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp];
  2134. RegSet drop = RSET_SCRATCH;
  2135. Reg r;
  2136. #if LJ_64
  2137. IRRef args[2];
  2138. args[0] = ir->op1;
  2139. args[1] = ir->op2;
  2140. #else
  2141. IRRef args[4];
  2142. args[LJ_LE ? 0 : 1] = ir->op1; args[LJ_LE ? 1 : 0] = (ir+1)->op1;
  2143. args[LJ_LE ? 2 : 3] = ir->op2; args[LJ_LE ? 3 : 2] = (ir+1)->op2;
  2144. #endif
  2145. for (r = REGARG_FIRSTGPR; r <= REGARG_FIRSTGPR+(LJ_64?1:3); r++) {
  2146. if (!rset_test(as->freeset, r) &&
  2147. regcost_ref(as->cost[r]) == args[r-REGARG_FIRSTGPR])
  2148. rset_clear(drop, r);
  2149. }
  2150. ra_evictset(as, drop);
  2151. asm_setupresult(as, ir, ci);
  2152. switch ((IROp)ir->o) {
  2153. case IR_LT:
  2154. asm_guard(as, MIPSI_BGEZ, RID_RET, 0);
  2155. break;
  2156. case IR_ULT:
  2157. asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP);
  2158. emit_loadi(as, RID_TMP, 1);
  2159. asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO);
  2160. break;
  2161. case IR_GE:
  2162. asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP);
  2163. emit_loadi(as, RID_TMP, 2);
  2164. asm_guard(as, MIPSI_BLTZ, RID_RET, 0);
  2165. break;
  2166. case IR_LE:
  2167. asm_guard(as, MIPSI_BGTZ, RID_RET, 0);
  2168. break;
  2169. case IR_GT:
  2170. asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP);
  2171. emit_loadi(as, RID_TMP, 2);
  2172. asm_guard(as, MIPSI_BLEZ, RID_RET, 0);
  2173. break;
  2174. case IR_UGE:
  2175. asm_guard(as, MIPSI_BLTZ, RID_RET, 0);
  2176. break;
  2177. case IR_ULE:
  2178. asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP);
  2179. emit_loadi(as, RID_TMP, 1);
  2180. break;
  2181. case IR_UGT: case IR_ABC:
  2182. asm_guard(as, MIPSI_BLEZ, RID_RET, 0);
  2183. break;
  2184. case IR_EQ: case IR_NE:
  2185. asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_RET, RID_ZERO);
  2186. default:
  2187. break;
  2188. }
  2189. asm_gencall(as, ci, args);
  2190. }
  2191. #endif
  2192. static void asm_comp(ASMState *as, IRIns *ir)
  2193. {
  2194. /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */
  2195. IROp op = ir->o;
  2196. if (!LJ_SOFTFP32 && irt_isnum(ir->t)) {
  2197. #if LJ_SOFTFP
  2198. asm_sfpcomp(as, ir);
  2199. #else
  2200. #if !LJ_TARGET_MIPSR6
  2201. Reg right, left = ra_alloc2(as, ir, RSET_FPR);
  2202. right = (left >> 8); left &= 255;
  2203. asm_guard(as, (op&1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0);
  2204. emit_fgh(as, MIPSI_C_OLT_D + ((op&3) ^ ((op>>2)&1)), 0, left, right);
  2205. #else
  2206. Reg tmp, right, left = ra_alloc2(as, ir, RSET_FPR);
  2207. right = (left >> 8); left &= 255;
  2208. tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_FPR, left), right));
  2209. asm_guard(as, (op&1) ? MIPSI_BC1NEZ : MIPSI_BC1EQZ, 0, (tmp&31));
  2210. emit_fgh(as, MIPSI_CMP_LT_D + ((op&3) ^ ((op>>2)&1)), tmp, left, right);
  2211. #endif
  2212. #endif
  2213. } else {
  2214. Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
  2215. if (op == IR_ABC) op = IR_UGT;
  2216. if ((op&4) == 0 && irref_isk(ir->op2) && get_kval(as, ir->op2) == 0) {
  2217. MIPSIns mi = (op&2) ? ((op&1) ? MIPSI_BLEZ : MIPSI_BGTZ) :
  2218. ((op&1) ? MIPSI_BLTZ : MIPSI_BGEZ);
  2219. asm_guard(as, mi, left, 0);
  2220. } else {
  2221. if (irref_isk(ir->op2)) {
  2222. intptr_t k = get_kval(as, ir->op2);
  2223. if ((op&2)) k++;
  2224. if (checki16(k)) {
  2225. asm_guard(as, (op&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
  2226. emit_tsi(as, (op&4) ? MIPSI_SLTIU : MIPSI_SLTI,
  2227. RID_TMP, left, k);
  2228. return;
  2229. }
  2230. }
  2231. right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
  2232. asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
  2233. emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT,
  2234. RID_TMP, (op&2) ? right : left, (op&2) ? left : right);
  2235. }
  2236. }
  2237. }
  2238. static void asm_equal(ASMState *as, IRIns *ir)
  2239. {
  2240. Reg right, left = ra_alloc2(as, ir, (!LJ_SOFTFP && irt_isnum(ir->t)) ?
  2241. RSET_FPR : RSET_GPR);
  2242. right = (left >> 8); left &= 255;
  2243. if (!LJ_SOFTFP32 && irt_isnum(ir->t)) {
  2244. #if LJ_SOFTFP
  2245. asm_sfpcomp(as, ir);
  2246. #elif !LJ_TARGET_MIPSR6
  2247. asm_guard(as, (ir->o & 1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0);
  2248. emit_fgh(as, MIPSI_C_EQ_D, 0, left, right);
  2249. #else
  2250. Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_FPR, left), right));
  2251. asm_guard(as, (ir->o & 1) ? MIPSI_BC1NEZ : MIPSI_BC1EQZ, 0, (tmp&31));
  2252. emit_fgh(as, MIPSI_CMP_EQ_D, tmp, left, right);
  2253. #endif
  2254. } else {
  2255. asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, left, right);
  2256. }
  2257. }
  2258. #if LJ_32 && LJ_HASFFI
  2259. /* 64 bit integer comparisons. */
  2260. static void asm_comp64(ASMState *as, IRIns *ir)
  2261. {
  2262. /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */
  2263. IROp op = (ir-1)->o;
  2264. MCLabel l_end;
  2265. Reg rightlo, leftlo, righthi, lefthi = ra_alloc2(as, ir, RSET_GPR);
  2266. righthi = (lefthi >> 8); lefthi &= 255;
  2267. leftlo = ra_alloc2(as, ir-1,
  2268. rset_exclude(rset_exclude(RSET_GPR, lefthi), righthi));
  2269. rightlo = (leftlo >> 8); leftlo &= 255;
  2270. asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO);
  2271. l_end = emit_label(as);
  2272. if (lefthi != righthi)
  2273. emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, RID_TMP,
  2274. (op&2) ? righthi : lefthi, (op&2) ? lefthi : righthi);
  2275. emit_dst(as, MIPSI_SLTU, RID_TMP,
  2276. (op&2) ? rightlo : leftlo, (op&2) ? leftlo : rightlo);
  2277. if (lefthi != righthi)
  2278. emit_branch(as, MIPSI_BEQ, lefthi, righthi, l_end);
  2279. }
  2280. static void asm_comp64eq(ASMState *as, IRIns *ir)
  2281. {
  2282. Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR);
  2283. right = (left >> 8); left &= 255;
  2284. asm_guard(as, ((ir-1)->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_TMP, RID_ZERO);
  2285. tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right));
  2286. emit_dst(as, MIPSI_OR, RID_TMP, RID_TMP, tmp);
  2287. emit_dst(as, MIPSI_XOR, tmp, left, right);
  2288. left = ra_alloc2(as, ir-1, RSET_GPR);
  2289. right = (left >> 8); left &= 255;
  2290. emit_dst(as, MIPSI_XOR, RID_TMP, left, right);
  2291. }
  2292. #endif
  2293. /* -- Split register ops -------------------------------------------------- */
  2294. /* Hiword op of a split 32/32 or 64/64 bit op. Previous op is the loword op. */
  2295. static void asm_hiop(ASMState *as, IRIns *ir)
  2296. {
  2297. /* HIOP is marked as a store because it needs its own DCE logic. */
  2298. int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
  2299. if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
  2300. #if LJ_32 && (LJ_HASFFI || LJ_SOFTFP)
  2301. if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */
  2302. as->curins--; /* Always skip the CONV. */
  2303. #if LJ_HASFFI && !LJ_SOFTFP
  2304. if (usehi || uselo)
  2305. asm_conv64(as, ir);
  2306. return;
  2307. #endif
  2308. } else if ((ir-1)->o < IR_EQ) { /* 64 bit integer comparisons. ORDER IR. */
  2309. as->curins--; /* Always skip the loword comparison. */
  2310. #if LJ_SOFTFP
  2311. if (!irt_isint(ir->t)) {
  2312. asm_sfpcomp(as, ir-1);
  2313. return;
  2314. }
  2315. #endif
  2316. #if LJ_HASFFI
  2317. asm_comp64(as, ir);
  2318. #endif
  2319. return;
  2320. } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */
  2321. as->curins--; /* Always skip the loword comparison. */
  2322. #if LJ_SOFTFP
  2323. if (!irt_isint(ir->t)) {
  2324. asm_sfpcomp(as, ir-1);
  2325. return;
  2326. }
  2327. #endif
  2328. #if LJ_HASFFI
  2329. asm_comp64eq(as, ir);
  2330. #endif
  2331. return;
  2332. #if LJ_SOFTFP
  2333. } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) {
  2334. as->curins--; /* Always skip the loword min/max. */
  2335. if (uselo || usehi)
  2336. asm_sfpmin_max(as, ir-1);
  2337. return;
  2338. #endif
  2339. } else if ((ir-1)->o == IR_XSTORE) {
  2340. as->curins--; /* Handle both stores here. */
  2341. if ((ir-1)->r != RID_SINK) {
  2342. asm_xstore_(as, ir, LJ_LE ? 4 : 0);
  2343. asm_xstore_(as, ir-1, LJ_LE ? 0 : 4);
  2344. }
  2345. return;
  2346. }
  2347. #endif
  2348. if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
  2349. switch ((ir-1)->o) {
  2350. #if LJ_32 && LJ_HASFFI
  2351. case IR_ADD: as->curins--; asm_add64(as, ir); break;
  2352. case IR_SUB: as->curins--; asm_sub64(as, ir); break;
  2353. case IR_NEG: as->curins--; asm_neg64(as, ir); break;
  2354. case IR_CNEWI:
  2355. /* Nothing to do here. Handled by lo op itself. */
  2356. break;
  2357. #endif
  2358. #if LJ_32 && LJ_SOFTFP
  2359. case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD:
  2360. case IR_STRTO:
  2361. if (!uselo)
  2362. ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */
  2363. break;
  2364. case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR: case IR_TMPREF:
  2365. /* Nothing to do here. Handled by lo op itself. */
  2366. break;
  2367. #endif
  2368. case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS:
  2369. if (!uselo)
  2370. ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
  2371. break;
  2372. default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break;
  2373. }
  2374. }
  2375. /* -- Profiling ----------------------------------------------------------- */
  2376. static void asm_prof(ASMState *as, IRIns *ir)
  2377. {
  2378. UNUSED(ir);
  2379. asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO);
  2380. emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, HOOK_PROFILE);
  2381. emit_lsglptr(as, MIPSI_LBU, RID_TMP,
  2382. (int32_t)offsetof(global_State, hookmask));
  2383. }
  2384. /* -- Stack handling ------------------------------------------------------ */
  2385. /* Check Lua stack size for overflow. Use exit handler as fallback. */
  2386. static void asm_stack_check(ASMState *as, BCReg topslot,
  2387. IRIns *irp, RegSet allow, ExitNo exitno)
  2388. {
  2389. /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */
  2390. Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;
  2391. ExitNo oldsnap = as->snapno;
  2392. rset_clear(allow, pbase);
  2393. #if LJ_32
  2394. tmp = allow ? rset_pickbot(allow) :
  2395. (pbase == RID_RETHI ? RID_RETLO : RID_RETHI);
  2396. #else
  2397. tmp = allow ? rset_pickbot(allow) : RID_RET;
  2398. #endif
  2399. as->snapno = exitno;
  2400. asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO);
  2401. as->snapno = oldsnap;
  2402. if (allow == RSET_EMPTY) /* Restore temp. register. */
  2403. emit_tsi(as, MIPSI_AL, tmp, RID_SP, 0);
  2404. else
  2405. ra_modified(as, tmp);
  2406. emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot));
  2407. emit_dst(as, MIPSI_ASUBU, RID_TMP, tmp, pbase);
  2408. emit_tsi(as, MIPSI_AL, tmp, tmp, offsetof(lua_State, maxstack));
  2409. if (pbase == RID_TMP)
  2410. emit_getgl(as, RID_TMP, jit_base);
  2411. emit_getgl(as, tmp, cur_L);
  2412. if (allow == RSET_EMPTY) /* Spill temp. register. */
  2413. emit_tsi(as, MIPSI_AS, tmp, RID_SP, 0);
  2414. }
  2415. /* Restore Lua stack from on-trace state. */
  2416. static void asm_stack_restore(ASMState *as, SnapShot *snap)
  2417. {
  2418. SnapEntry *map = &as->T->snapmap[snap->mapofs];
  2419. #if LJ_32 || defined(LUA_USE_ASSERT)
  2420. SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1-LJ_FR2];
  2421. #endif
  2422. MSize n, nent = snap->nent;
  2423. /* Store the value of all modified slots to the Lua stack. */
  2424. for (n = 0; n < nent; n++) {
  2425. SnapEntry sn = map[n];
  2426. BCReg s = snap_slot(sn);
  2427. int32_t ofs = 8*((int32_t)s-1-LJ_FR2);
  2428. IRRef ref = snap_ref(sn);
  2429. IRIns *ir = IR(ref);
  2430. if ((sn & SNAP_NORESTORE))
  2431. continue;
  2432. if (irt_isnum(ir->t)) {
  2433. #if LJ_SOFTFP32
  2434. Reg tmp;
  2435. RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
  2436. /* LJ_SOFTFP: must be a number constant. */
  2437. lj_assertA(irref_isk(ref), "unsplit FP op");
  2438. tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, allow);
  2439. emit_tsi(as, MIPSI_SW, tmp, RID_BASE, ofs+(LJ_BE?4:0));
  2440. if (rset_test(as->freeset, tmp+1)) allow = RID2RSET(tmp+1);
  2441. tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, allow);
  2442. emit_tsi(as, MIPSI_SW, tmp, RID_BASE, ofs+(LJ_BE?0:4));
  2443. #elif LJ_SOFTFP /* && LJ_64 */
  2444. Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE));
  2445. emit_tsi(as, MIPSI_SD, src, RID_BASE, ofs);
  2446. #else
  2447. Reg src = ra_alloc1(as, ref, RSET_FPR);
  2448. emit_hsi(as, MIPSI_SDC1, src, RID_BASE, ofs);
  2449. #endif
  2450. } else {
  2451. #if LJ_32
  2452. RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
  2453. Reg type;
  2454. lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t),
  2455. "restore of IR type %d", irt_type(ir->t));
  2456. if (!irt_ispri(ir->t)) {
  2457. Reg src = ra_alloc1(as, ref, allow);
  2458. rset_clear(allow, src);
  2459. emit_tsi(as, MIPSI_SW, src, RID_BASE, ofs+(LJ_BE?4:0));
  2460. }
  2461. if ((sn & (SNAP_CONT|SNAP_FRAME))) {
  2462. if (s == 0) continue; /* Do not overwrite link to previous frame. */
  2463. type = ra_allock(as, (int32_t)(*flinks--), allow);
  2464. #if LJ_SOFTFP
  2465. } else if ((sn & SNAP_SOFTFPNUM)) {
  2466. type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPR, RID_BASE));
  2467. #endif
  2468. } else if ((sn & SNAP_KEYINDEX)) {
  2469. type = ra_allock(as, (int32_t)LJ_KEYINDEX, allow);
  2470. } else {
  2471. type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow);
  2472. }
  2473. emit_tsi(as, MIPSI_SW, type, RID_BASE, ofs+(LJ_BE?0:4));
  2474. #else
  2475. if ((sn & SNAP_KEYINDEX)) {
  2476. RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
  2477. int64_t kki = (int64_t)LJ_KEYINDEX << 32;
  2478. if (irref_isk(ref)) {
  2479. emit_tsi(as, MIPSI_SD,
  2480. ra_allock(as, kki | (int64_t)(uint32_t)ir->i, allow),
  2481. RID_BASE, ofs);
  2482. } else {
  2483. Reg src = ra_alloc1(as, ref, allow);
  2484. Reg rki = ra_allock(as, kki, rset_exclude(allow, src));
  2485. emit_tsi(as, MIPSI_SD, RID_TMP, RID_BASE, ofs);
  2486. emit_dst(as, MIPSI_DADDU, RID_TMP, src, rki);
  2487. }
  2488. } else {
  2489. asm_tvstore64(as, RID_BASE, ofs, ref);
  2490. }
  2491. #endif
  2492. }
  2493. checkmclim(as);
  2494. }
  2495. lj_assertA(map + nent == flinks, "inconsistent frames in snapshot");
  2496. }
  2497. /* -- GC handling --------------------------------------------------------- */
  2498. /* Marker to prevent patching the GC check exit. */
  2499. #define MIPS_NOPATCH_GC_CHECK MIPSI_OR
  2500. /* Check GC threshold and do one or more GC steps. */
  2501. static void asm_gc_check(ASMState *as)
  2502. {
  2503. const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
  2504. IRRef args[2];
  2505. MCLabel l_end;
  2506. Reg tmp;
  2507. ra_evictset(as, RSET_SCRATCH);
  2508. l_end = emit_label(as);
  2509. /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
  2510. /* Assumes asm_snap_prep() already done. */
  2511. asm_guard(as, MIPSI_BNE, RID_RET, RID_ZERO);
  2512. args[0] = ASMREF_TMP1; /* global_State *g */
  2513. args[1] = ASMREF_TMP2; /* MSize steps */
  2514. asm_gencall(as, ci, args);
  2515. l_end[-3] = MIPS_NOPATCH_GC_CHECK; /* Replace the nop after the call. */
  2516. emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768);
  2517. tmp = ra_releasetmp(as, ASMREF_TMP2);
  2518. emit_loadi(as, tmp, as->gcsteps);
  2519. /* Jump around GC step if GC total < GC threshold. */
  2520. emit_branch(as, MIPSI_BNE, RID_TMP, RID_ZERO, l_end);
  2521. emit_dst(as, MIPSI_SLTU, RID_TMP, RID_TMP, tmp);
  2522. emit_getgl(as, tmp, gc.threshold);
  2523. emit_getgl(as, RID_TMP, gc.total);
  2524. as->gcsteps = 0;
  2525. checkmclim(as);
  2526. }
  2527. /* -- Loop handling ------------------------------------------------------- */
  2528. /* Fixup the loop branch. */
  2529. static void asm_loop_fixup(ASMState *as)
  2530. {
  2531. MCode *p = as->mctop;
  2532. MCode *target = as->mcp;
  2533. p[-1] = MIPSI_NOP;
  2534. if (as->loopinv) { /* Inverted loop branch? */
  2535. /* asm_guard already inverted the cond branch. Only patch the target. */
  2536. p[-3] |= ((target-p+2) & 0x0000ffffu);
  2537. } else {
  2538. p[-2] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);
  2539. }
  2540. }
  2541. /* Fixup the tail of the loop. */
  2542. static void asm_loop_tail_fixup(ASMState *as)
  2543. {
  2544. if (as->loopinv) as->mctop--;
  2545. }
  2546. /* -- Head of trace ------------------------------------------------------- */
  2547. /* Coalesce BASE register for a root trace. */
  2548. static void asm_head_root_base(ASMState *as)
  2549. {
  2550. IRIns *ir = IR(REF_BASE);
  2551. Reg r = ir->r;
  2552. if (ra_hasreg(r)) {
  2553. ra_free(as, r);
  2554. if (rset_test(as->modset, r) || irt_ismarked(ir->t))
  2555. ir->r = RID_INIT; /* No inheritance for modified BASE register. */
  2556. if (r != RID_BASE)
  2557. emit_move(as, r, RID_BASE);
  2558. }
  2559. }
  2560. /* Coalesce BASE register for a side trace. */
  2561. static Reg asm_head_side_base(ASMState *as, IRIns *irp)
  2562. {
  2563. IRIns *ir = IR(REF_BASE);
  2564. Reg r = ir->r;
  2565. if (ra_hasreg(r)) {
  2566. ra_free(as, r);
  2567. if (rset_test(as->modset, r) || irt_ismarked(ir->t))
  2568. ir->r = RID_INIT; /* No inheritance for modified BASE register. */
  2569. if (irp->r == r) {
  2570. return r; /* Same BASE register already coalesced. */
  2571. } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {
  2572. emit_move(as, r, irp->r); /* Move from coalesced parent reg. */
  2573. return irp->r;
  2574. } else {
  2575. emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */
  2576. }
  2577. }
  2578. return RID_NONE;
  2579. }
  2580. /* -- Tail of trace ------------------------------------------------------- */
  2581. /* Fixup the tail code. */
  2582. static void asm_tail_fixup(ASMState *as, TraceNo lnk)
  2583. {
  2584. MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp;
  2585. int32_t spadj = as->T->spadjust;
  2586. MCode *p = as->mctop-1;
  2587. *p = spadj ? (MIPSI_AADDIU|MIPSF_T(RID_SP)|MIPSF_S(RID_SP)|spadj) : MIPSI_NOP;
  2588. p[-1] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);
  2589. }
  2590. /* Prepare tail of code. */
  2591. static void asm_tail_prep(ASMState *as)
  2592. {
  2593. as->mcp = as->mctop-2; /* Leave room for branch plus nop or stack adj. */
  2594. as->invmcp = as->loopref ? as->mcp : NULL;
  2595. }
  2596. /* -- Trace setup --------------------------------------------------------- */
  2597. /* Ensure there are enough stack slots for call arguments. */
  2598. static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
  2599. {
  2600. IRRef args[CCI_NARGS_MAX*2];
  2601. uint32_t i, nargs = CCI_XNARGS(ci);
  2602. #if LJ_32
  2603. int nslots = 4, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
  2604. #else
  2605. int nslots = 0, ngpr = REGARG_NUMGPR;
  2606. #endif
  2607. asm_collectargs(as, ir, ci, args);
  2608. for (i = 0; i < nargs; i++) {
  2609. #if LJ_32
  2610. if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t) &&
  2611. nfpr > 0 && !(ci->flags & CCI_VARARG)) {
  2612. nfpr--;
  2613. ngpr -= irt_isnum(IR(args[i])->t) ? 2 : 1;
  2614. } else if (!LJ_SOFTFP && args[i] && irt_isnum(IR(args[i])->t)) {
  2615. nfpr = 0;
  2616. ngpr = ngpr & ~1;
  2617. if (ngpr > 0) ngpr -= 2; else nslots = (nslots+3) & ~1;
  2618. } else {
  2619. nfpr = 0;
  2620. if (ngpr > 0) ngpr--; else nslots++;
  2621. }
  2622. #else
  2623. if (ngpr > 0) ngpr--; else nslots += 2;
  2624. #endif
  2625. }
  2626. if (nslots > as->evenspill) /* Leave room for args in stack slots. */
  2627. as->evenspill = nslots;
  2628. return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET);
  2629. }
  2630. static void asm_setup_target(ASMState *as)
  2631. {
  2632. asm_sparejump_setup(as);
  2633. asm_exitstub_setup(as);
  2634. }
  2635. /* -- Trace patching ------------------------------------------------------ */
  2636. /* Patch exit jumps of existing machine code to a new target. */
  2637. void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
  2638. {
  2639. MCode *p = T->mcode;
  2640. MCode *pe = (MCode *)((char *)p + T->szmcode);
  2641. MCode *px = exitstub_trace_addr(T, exitno);
  2642. MCode *cstart = NULL, *cstop = NULL;
  2643. MCode *mcarea = lj_mcode_patch(J, p, 0);
  2644. MCode exitload = MIPSI_LI | MIPSF_T(RID_TMP) | exitno;
  2645. MCode tjump = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu);
  2646. for (p++; p < pe; p++) {
  2647. if (*p == exitload) { /* Look for load of exit number. */
  2648. /* Look for exitstub branch. Yes, this covers all used branch variants. */
  2649. if (((p[-1] ^ (px-p)) & 0xffffu) == 0 &&
  2650. ((p[-1] & 0xf0000000u) == MIPSI_BEQ ||
  2651. (p[-1] & 0xfc1e0000u) == MIPSI_BLTZ ||
  2652. #if !LJ_TARGET_MIPSR6
  2653. (p[-1] & 0xffe00000u) == MIPSI_BC1F
  2654. #else
  2655. (p[-1] & 0xff600000u) == MIPSI_BC1EQZ
  2656. #endif
  2657. ) && p[-2] != MIPS_NOPATCH_GC_CHECK) {
  2658. ptrdiff_t delta = target - p;
  2659. if (((delta + 0x8000) >> 16) == 0) { /* Patch in-range branch. */
  2660. patchbranch:
  2661. p[-1] = (p[-1] & 0xffff0000u) | (delta & 0xffffu);
  2662. *p = MIPSI_NOP; /* Replace the load of the exit number. */
  2663. cstop = p+1;
  2664. if (!cstart) cstart = p-1;
  2665. } else { /* Branch out of range. Use spare jump slot in mcarea. */
  2666. MCode *mcjump = asm_sparejump_use(mcarea, tjump);
  2667. if (mcjump) {
  2668. lj_mcode_sync(mcjump, mcjump+1);
  2669. delta = mcjump - p;
  2670. if (((delta + 0x8000) >> 16) == 0) {
  2671. goto patchbranch;
  2672. } else {
  2673. lj_assertJ(0, "spare jump out of range: -Osizemcode too big");
  2674. }
  2675. }
  2676. /* Ignore jump slot overflow. Child trace is simply not attached. */
  2677. }
  2678. } else if (p+1 == pe) {
  2679. /* Patch NOP after code for inverted loop branch. Use of J is ok. */
  2680. lj_assertJ(p[1] == MIPSI_NOP, "expected NOP");
  2681. p[1] = tjump;
  2682. *p = MIPSI_NOP; /* Replace the load of the exit number. */
  2683. cstop = p+2;
  2684. if (!cstart) cstart = p+1;
  2685. }
  2686. }
  2687. }
  2688. if (cstart) lj_mcode_sync(cstart, cstop);
  2689. lj_mcode_patch(J, mcarea, 1);
  2690. }