lj_asm_arm.h 74 KB

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