Debian Codegraph @

src/lj_asm_riscv64.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 ** RISC-V IR assembler (SSA IR -> machine code).
3 ** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h
4 */
5
6 /* -- Register allocator extensions --------------------------------------- */
7
8 /* Allocate a register with a hint. */
9 static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow)
10 {
11 Reg r = IR(ref)->r;
12 if (ra_noreg(r)) {
13 if (!ra_hashint(r) && !iscrossref(as, ref))
14 ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */
15 r = ra_allocref(as, ref, allow);
16 }
17 ra_noweak(as, r);
18 return r;
19 }
20
21 /* Allocate a register or RID_ZERO. */
22 static Reg ra_alloc1z(ASMState *as, IRRef ref, RegSet allow)
23 {
24 Reg r = IR(ref)->r;
25 if (ra_noreg(r)) {
26 if (!(allow & RSET_FPR) && irref_isk(ref) && get_kval(as, ref) == 0)
27 return RID_ZERO;
28 r = ra_allocref(as, ref, allow);
29 } else {
30 ra_noweak(as, r);
31 }
32 return r;
33 }
34
35 /* Allocate two source registers for three-operand instructions. */
36 static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow)
37 {
38 IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
39 Reg left = irl->r, right = irr->r;
40 if (ra_hasreg(left)) {
41 ra_noweak(as, left);
42 if (ra_noreg(right))
43 right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left));
44 else
45 ra_noweak(as, right);
46 } else if (ra_hasreg(right)) {
47 ra_noweak(as, right);
48 left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right));
49 } else if (ra_hashint(right)) {
50 right = ra_alloc1z(as, ir->op2, allow);
51 left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right));
52 } else {
53 left = ra_alloc1z(as, ir->op1, allow);
54 right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left));
55 }
56 return left | (right << 8);
57 }
58
59 /* -- Guard handling ------------------------------------------------------ */
60
61 /* Copied from MIPS, AUIPC+JALR is expensive to setup in-place */
62 #define RISCV_SPAREJUMP 4
63
64 /* Setup spare long-range jump (trampoline?) slots per mcarea. */
65
66 static void asm_sparejump_setup(ASMState *as)
67 {
68 MCode *mxp = as->mctop;
69 if ((char *)mxp == (char *)as->J->mcarea + as->J->szmcarea) {
70 for (int i = RISCV_SPAREJUMP*2; i--; )
71 *--mxp = RISCVI_EBREAK;
72 as->mctop = mxp;
73 }
74 }
75
76 static MCode *asm_sparejump_use(MCode *mcarea, MCode *target)
77 {
78 MCode *mxp = (MCode *)((char *)mcarea + ((MCLink *)mcarea)->size);
79 int slot = RISCV_SPAREJUMP;
80 RISCVIns tslot = RISCVI_EBREAK, tauipc, tjalr;
81 while (slot--) {
82 mxp -= 2;
83 ptrdiff_t delta = (char *)target - (char *)mxp;
84 tauipc = RISCVI_AUIPC | RISCVF_D(RID_TMP) | RISCVF_IMMU(RISCVF_HI(delta)),
85 tjalr = RISCVI_JALR | RISCVF_S1(RID_TMP) | RISCVF_IMMI(RISCVF_LO(delta));
86 if (mxp[0] == tauipc && mxp[1] == tjalr) {
87 return mxp;
88 } else if (mxp[0] == tslot) {
89 mxp[0] = tauipc, mxp[1] = tjalr;
90 return mxp;
91 }
92 }
93 return NULL;
94 }
95
96 /* Setup exit stub after the end of each trace. */
97 static void asm_exitstub_setup(ASMState *as, ExitNo nexits)
98 {
99 ExitNo i;
100 MCode *mxp = as->mctop;
101 if (mxp - (nexits + 4 + MCLIM_REDZONE) < as->mclim)
102 asm_mclimit(as);
103 for (i = nexits-1; (int32_t)i >= 0; i--)
104 *--mxp = RISCVI_JAL | RISCVF_D(RID_RA) | RISCVF_IMMJ((uintptr_t)(4*(-4-i)));
105 ptrdiff_t delta = (char *)lj_vm_exit_handler - (char *)(mxp-3);
106 /* 1: sw ra, 0(sp); auipc+jalr ->vm_exit_handler; lui x0, traceno; jal <1; jal <1; ... */
107 *--mxp = RISCVI_LUI | RISCVF_IMMU(as->T->traceno);
108 *--mxp = RISCVI_JALR | RISCVF_D(RID_RA) | RISCVF_S1(RID_TMP)
109 | RISCVF_IMMI(RISCVF_LO((uintptr_t)(void *)delta));
110 *--mxp = RISCVI_AUIPC | RISCVF_D(RID_TMP)
111 | RISCVF_IMMU(RISCVF_HI((uintptr_t)(void *)delta));
112 *--mxp = RISCVI_SD | RISCVF_S2(RID_RA) | RISCVF_S1(RID_SP);
113 as->mctop = mxp;
114 }
115
116 static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno)
117 {
118 /* Keep this in-sync with exitstub_trace_addr(). */
119 return as->mctop + exitno + 4;
120 }
121
122 /* Emit conditional branch to exit for guard. */
123 static void asm_guard(ASMState *as, RISCVIns riscvi, Reg rs1, Reg rs2)
124 {
125 MCode *target = asm_exitstub_addr(as, as->snapno);
126 MCode *p = as->mcp;
127 if (LJ_UNLIKELY(p == as->invmcp)) {
128 as->loopinv = 1;
129 as->mcp = ++p;
130 *p = RISCVI_JAL | RISCVF_IMMJ((char *)target - (char *)p);
131 riscvi = riscvi^RISCVF_FUNCT3(1); /* Invert cond. */
132 target = p - 1; /* Patch target later in asm_loop_fixup. */
133 }
134 ptrdiff_t delta = (char *)target - (char *)(p - 1);
135 *--p = RISCVI_JAL | RISCVF_IMMJ(delta);
136 *--p = (riscvi^RISCVF_FUNCT3(1)) | RISCVF_S1(rs1) | RISCVF_S2(rs2) | RISCVF_IMMB(8);
137 as->mcp = p;
138 }
139
140 /* -- Operand fusion ------------------------------------------------------ */
141
142 /* Limit linear search to this distance. Avoids O(n^2) behavior. */
143 #define CONFLICT_SEARCH_LIM 31
144
145 /* Check if there's no conflicting instruction between curins and ref. */
146 static int noconflict(ASMState *as, IRRef ref, IROp conflict)
147 {
148 IRIns *ir = as->ir;
149 IRRef i = as->curins;
150 if (i > ref + CONFLICT_SEARCH_LIM)
151 return 0; /* Give up, ref is too far away. */
152 while (--i > ref)
153 if (ir[i].o == conflict)
154 return 0; /* Conflict found. */
155 return 1; /* Ok, no conflict. */
156 }
157
158 /* Fuse the array base of colocated arrays. */
159 static int32_t asm_fuseabase(ASMState *as, IRRef ref)
160 {
161 IRIns *ir = IR(ref);
162 if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE &&
163 !neverfuse(as) && noconflict(as, ref, IR_NEWREF))
164 return (int32_t)sizeof(GCtab);
165 return 0;
166 }
167
168 /* Fuse array/hash/upvalue reference into register+offset operand. */
169 static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow)
170 {
171 IRIns *ir = IR(ref);
172 if (ra_noreg(ir->r)) {
173 if (ir->o == IR_AREF) {
174 if (mayfuse(as, ref)) {
175 if (irref_isk(ir->op2)) {
176 IRRef tab = IR(ir->op1)->op1;
177 int32_t ofs = asm_fuseabase(as, tab);
178 IRRef refa = ofs ? tab : ir->op1;
179 ofs += 8*IR(ir->op2)->i;
180 if (checki12(ofs)) {
181 *ofsp = ofs;
182 return ra_alloc1(as, refa, allow);
183 }
184 }
185 }
186 } else if (ir->o == IR_HREFK) {
187 if (mayfuse(as, ref)) {
188 int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node));
189 if (checki12(ofs)) {
190 *ofsp = ofs;
191 return ra_alloc1(as, ir->op1, allow);
192 }
193 }
194 } else if (ir->o == IR_UREFC) {
195 if (irref_isk(ir->op1)) {
196 GCfunc *fn = ir_kfunc(IR(ir->op1));
197 GCupval *uv = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv;
198 intptr_t ofs = ((intptr_t)((uintptr_t)(&uv->tv) - (uintptr_t)&J2GG(as->J)->g));
199 if (checki12(ofs)) {
200 *ofsp = (int32_t)ofs;
201 return RID_GL;
202 }
203 }
204 } else if (ir->o == IR_TMPREF) {
205 *ofsp = (int32_t)offsetof(global_State, tmptv);
206 return RID_GL;
207 }
208 }
209 *ofsp = 0;
210 return ra_alloc1(as, ref, allow);
211 }
212
213 /* Fuse XLOAD/XSTORE reference into load/store operand. */
214 static void asm_fusexref(ASMState *as, RISCVIns riscvi, Reg rd, IRRef ref,
215 RegSet allow, int32_t ofs)
216 {
217 IRIns *ir = IR(ref);
218 Reg base;
219 if (ra_noreg(ir->r) && canfuse(as, ir)) {
220 intptr_t ofs2;
221 if (ir->o == IR_ADD) {
222 if (irref_isk(ir->op2) && (ofs2 = ofs + get_kval(as, ir->op2),
223 checki12(ofs2))) {
224 ref = ir->op1;
225 ofs = (int32_t)ofs2;
226 }
227 } else if (ir->o == IR_STRREF) {
228 ofs2 = 4096;
229 lj_assertA(ofs == 0, "bad usage");
230 ofs = (int32_t)sizeof(GCstr);
231 if (irref_isk(ir->op2)) {
232 ofs2 = ofs + get_kval(as, ir->op2);
233 ref = ir->op1;
234 } else if (irref_isk(ir->op1)) {
235 ofs2 = ofs + get_kval(as, ir->op1);
236 ref = ir->op2;
237 }
238 if (!checki12(ofs2)) {
239 /* NYI: Fuse ADD with constant. */
240 Reg right, left = ra_alloc2(as, ir, allow);
241 right = (left >> 8); left &= 255;
242 emit_lso(as, riscvi, rd, RID_TMP, ofs);
243 emit_ds1s2(as, RISCVI_ADD, RID_TMP, left, right);
244 return;
245 }
246 ofs = ofs2;
247 }
248 }
249 base = ra_alloc1(as, ref, allow);
250 emit_lso(as, riscvi, rd, base, ofs);
251 }
252
253 /* Fuse Integer multiply-accumulate. */
254
255 static int asm_fusemac(ASMState *as, IRIns *ir, RISCVIns riscvi)
256 {
257 IRRef lref = ir->op1, rref = ir->op2;
258 IRIns *irm;
259 if (lref != rref &&
260 ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&
261 ra_noreg(irm->r)) ||
262 (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&
263 (rref = lref, ra_noreg(irm->r))))) {
264 Reg dest = ra_dest(as, ir, RSET_GPR);
265 Reg add = ra_hintalloc(as, rref, dest, RSET_GPR);
266 Reg left = ra_alloc2(as, irm,
267 rset_exclude(rset_exclude(RSET_GPR, dest), add));
268 Reg right = (left >> 8); left &= 255;
269 emit_ds1s2(as, riscvi, dest, left, right);
270 if (dest != add) emit_mv(as, dest, add);
271 return 1;
272 }
273 return 0;
274 }
275
276 /* Fuse FP multiply-add/sub. */
277
278 static int asm_fusemadd(ASMState *as, IRIns *ir, RISCVIns riscvi, RISCVIns riscvir)
279 {
280 IRRef lref = ir->op1, rref = ir->op2;
281 IRIns *irm;
282 if ((as->flags & JIT_F_OPT_FMA) &&
283 lref != rref &&
284 ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) &&
285 ra_noreg(irm->r)) ||
286 (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) &&
287 (rref = lref, riscvi = riscvir, ra_noreg(irm->r))))) {
288 Reg dest = ra_dest(as, ir, RSET_FPR);
289 Reg add = ra_hintalloc(as, rref, dest, RSET_FPR);
290 Reg left = ra_alloc2(as, irm,
291 rset_exclude(rset_exclude(RSET_FPR, dest), add));
292 Reg right = (left >> 8); left &= 255;
293 emit_ds1s2s3(as, riscvi, dest, left, right, add);
294 return 1;
295 }
296 return 0;
297 }
298 /* -- Calls --------------------------------------------------------------- */
299
300 /* Generate a call to a C function. */
301 static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args)
302 {
303 uint32_t n, nargs = CCI_XNARGS(ci);
304 int32_t ofs = 0;
305 Reg gpr, fpr = REGARG_FIRSTFPR;
306 if ((void *)ci->func)
307 emit_call(as, (void *)ci->func, 1);
308 for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++)
309 as->cost[gpr] = REGCOST(~0u, ASMREF_L);
310 gpr = REGARG_FIRSTGPR;
311 for (n = 0; n < nargs; n++) { /* Setup args. */
312 IRRef ref = args[n];
313 IRIns *ir = IR(ref);
314 if (ref) {
315 if (irt_isfp(ir->t)) {
316 if (fpr <= REGARG_LASTFPR) {
317 lj_assertA(rset_test(as->freeset, fpr),
318 "reg %d not free", fpr); /* Must have been evicted. */
319 ra_leftov(as, fpr, ref);
320 fpr++; if(ci->flags & CCI_VARARG) gpr++;
321 } else if (!(ci->flags & CCI_VARARG) && gpr <= REGARG_LASTGPR) {
322 lj_assertA(rset_test(as->freeset, gpr),
323 "reg %d not free", gpr); /* Must have been evicted. */
324 ra_leftov(as, gpr, ref);
325 gpr++;
326 } else {
327 Reg r = ra_alloc1(as, ref, RSET_FPR);
328 emit_spstore(as, ir, r, ofs);
329 ofs += 8;
330 }
331 } else {
332 if (gpr <= REGARG_LASTGPR) {
333 lj_assertA(rset_test(as->freeset, gpr),
334 "reg %d not free", gpr); /* Must have been evicted. */
335 ra_leftov(as, gpr, ref);
336 gpr++; if(ci->flags & CCI_VARARG) fpr++;
337 } else {
338 Reg r = ra_alloc1z(as, ref, RSET_GPR);
339 emit_spstore(as, ir, r, ofs);
340 ofs += 8;
341 }
342 }
343 }
344 }
345 }
346
347 /* Setup result reg/sp for call. Evict scratch regs. */
348 static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci)
349 {
350 RegSet drop = RSET_SCRATCH;
351 int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t));
352 if (ra_hasreg(ir->r))
353 rset_clear(drop, ir->r); /* Dest reg handled below. */
354 if (hiop && ra_hasreg((ir+1)->r))
355 rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */
356 ra_evictset(as, drop); /* Evictions must be performed first. */
357 if (ra_used(ir)) {
358 lj_assertA(!irt_ispri(ir->t), "PRI dest");
359 if (irt_isfp(ir->t)) {
360 if ((ci->flags & CCI_CASTU64)) {
361 Reg dest = ra_dest(as, ir, RSET_FPR);
362 emit_ds(as, irt_isnum(ir->t) ? RISCVI_FMV_D_X : RISCVI_FMV_W_X,
363 dest, RID_RET);
364 } else {
365 ra_destreg(as, ir, RID_FPRET);
366 }
367 } else if (hiop) {
368 ra_destpair(as, ir);
369 } else {
370 ra_destreg(as, ir, RID_RET);
371 }
372 }
373 }
374
375 static void asm_callx(ASMState *as, IRIns *ir)
376 {
377 IRRef args[CCI_NARGS_MAX*2];
378 CCallInfo ci;
379 IRRef func;
380 IRIns *irf;
381 ci.flags = asm_callx_flags(as, ir);
382 asm_collectargs(as, ir, &ci, args);
383 asm_setupresult(as, ir, &ci);
384 func = ir->op2; irf = IR(func);
385 if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); }
386 if (irref_isk(func)) { /* Call to constant address. */
387 ci.func = (ASMFunction)(void *)get_kval(as, func);
388 } else { /* Need specific register for indirect calls. */
389 Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR));
390 MCode *p = as->mcp;
391 *--p = RISCVI_JALR | RISCVF_D(RID_RA) | RISCVF_S1(r);
392 if (r == RID_CFUNCADDR)
393 *--p = RISCVI_ADDI | RISCVF_D(RID_CFUNCADDR) | RISCVF_S1(r);
394 else
395 *--p = RISCVI_MV | RISCVF_D(RID_CFUNCADDR) | RISCVF_S1(r);
396 as->mcp = p;
397 ci.func = (ASMFunction)(void *)0;
398 }
399 asm_gencall(as, &ci, args);
400 }
401
402 static void asm_callround(ASMState *as, IRIns *ir, IRCallID id)
403 {
404 /* The modified regs must match with the *.dasc implementation. */
405 RegSet drop = RID2RSET(RID_X6)|RID2RSET(RID_X7)|RID2RSET(RID_F10)|
406 RID2RSET(RID_F14)|RID2RSET(RID_F1)|RID2RSET(RID_F3)|
407 RID2RSET(RID_F4);
408 if (ra_hasreg(ir->r)) rset_clear(drop, ir->r);
409 ra_evictset(as, drop);
410 ra_destreg(as, ir, RID_FPRET);
411 emit_call(as, (void *)lj_ir_callinfo[id].func, 0);
412 ra_leftov(as, REGARG_FIRSTFPR, ir->op1);
413 }
414
415 /* -- Returns ------------------------------------------------------------- */
416
417 /* Return to lower frame. Guard that it goes to the right spot. */
418 static void asm_retf(ASMState *as, IRIns *ir)
419 {
420 Reg base = ra_alloc1(as, REF_BASE, RSET_GPR);
421 void *pc = ir_kptr(IR(ir->op2));
422 int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1));
423 as->topslot -= (BCReg)delta;
424 if ((int32_t)as->topslot < 0) as->topslot = 0;
425 irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */
426 emit_setgl(as, base, jit_base);
427 emit_addptr(as, base, -8*delta);
428 asm_guard(as, RISCVI_BNE, RID_TMP,
429 ra_allock(as, igcptr(pc), rset_exclude(RSET_GPR, base)));
430 emit_lso(as, RISCVI_LD, RID_TMP, base, -8);
431 }
432
433 /* -- Buffer operations --------------------------------------------------- */
434
435 #if LJ_HASBUFFER
436 static void asm_bufhdr_write(ASMState *as, Reg sb)
437 {
438 Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb));
439 IRIns irgc;
440 irgc.ot = IRT(0, IRT_PGC); /* GC type. */
441 emit_storeofs(as, &irgc, RID_TMP, sb, offsetof(SBuf, L));
442 emit_ds1s2(as, RISCVI_OR, RID_TMP, RID_TMP, tmp);
443 emit_dsi(as, RISCVI_ANDI, tmp, tmp, SBUF_MASK_FLAG);
444 emit_getgl(as, RID_TMP, cur_L);
445 emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L));
446 }
447 #endif
448
449 /* -- Type conversions ---------------------------------------------------- */
450
451 static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
452 {
453 Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left));
454 Reg dest = ra_dest(as, ir, RSET_GPR), cmp = ra_scratch(as, rset_exclude(RSET_GPR, dest));
455 asm_guard(as, RISCVI_BEQ, cmp, RID_ZERO);
456 emit_ds1s2(as, RISCVI_FEQ_D, cmp, tmp, left);
457 emit_ds(as, RISCVI_FCVT_D_W, tmp, dest);
458 emit_ds(as, RISCVI_FCVT_W_D, dest, left);
459 }
460
461 static void asm_tobit(ASMState *as, IRIns *ir)
462 {
463 RegSet allow = RSET_FPR;
464 Reg dest = ra_dest(as, ir, RSET_GPR);
465 Reg left = ra_alloc1(as, ir->op1, allow);
466 Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left));
467 Reg tmp = ra_scratch(as, rset_clear(allow, right));
468 emit_ds(as, RISCVI_FMV_X_W, dest, tmp);
469 emit_ds1s2(as, RISCVI_FADD_D, tmp, left, right);
470 }
471
472 static void asm_conv(ASMState *as, IRIns *ir)
473 {
474 IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK);
475 int st64 = (st == IRT_I64 || st == IRT_U64 || st == IRT_P64);
476 int stfp = (st == IRT_NUM || st == IRT_FLOAT);
477 IRRef lref = ir->op1;
478 lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV");
479 /* Use GPR to pass floating-point arguments */
480 if (irt_isfp(ir->t) && ir->r >= RID_X10 && ir->r <= RID_X17) {
481 Reg dest = ra_dest(as, ir, RSET_GPR);
482 Reg ftmp = ra_scratch(as, RSET_FPR);
483 if (stfp) { /* FP to FP conversion. */
484 emit_ds(as, st == IRT_NUM ? RISCVI_FMV_X_W : RISCVI_FMV_X_D, dest, ftmp);
485 emit_ds(as, st == IRT_NUM ? RISCVI_FCVT_S_D : RISCVI_FCVT_D_S,
486 ftmp, ra_alloc1(as, lref, RSET_FPR));
487 } else { /* Integer to FP conversion. */
488 Reg left = ra_alloc1(as, lref, RSET_GPR);
489 RISCVIns riscvi = irt_isfloat(ir->t) ?
490 (((IRT_IS64 >> st) & 1) ?
491 (st == IRT_I64 ? RISCVI_FCVT_S_L : RISCVI_FCVT_S_LU) :
492 (st == IRT_INT ? RISCVI_FCVT_S_W : RISCVI_FCVT_S_WU)) :
493 (((IRT_IS64 >> st) & 1) ?
494 (st == IRT_I64 ? RISCVI_FCVT_D_L : RISCVI_FCVT_D_LU) :
495 (st == IRT_INT ? RISCVI_FCVT_D_W : RISCVI_FCVT_D_WU));
496 emit_ds(as, st64 ? RISCVI_FMV_X_D : RISCVI_FMV_X_W, dest, ftmp);
497 emit_ds(as, riscvi, ftmp, left);
498 }
499 } else if (irt_isfp(ir->t)) {
500 Reg dest = ra_dest(as, ir, RSET_FPR);
501 if (stfp) { /* FP to FP conversion. */
502 emit_ds(as, st == IRT_NUM ? RISCVI_FCVT_S_D : RISCVI_FCVT_D_S,
503 dest, ra_alloc1(as, lref, RSET_FPR));
504 } else { /* Integer to FP conversion. */
505 Reg left = ra_alloc1(as, lref, RSET_GPR);
506 RISCVIns riscvi = irt_isfloat(ir->t) ?
507 (((IRT_IS64 >> st) & 1) ?
508 (st == IRT_I64 ? RISCVI_FCVT_S_L : RISCVI_FCVT_S_LU) :
509 (st == IRT_INT ? RISCVI_FCVT_S_W : RISCVI_FCVT_S_WU)) :
510 (((IRT_IS64 >> st) & 1) ?
511 (st == IRT_I64 ? RISCVI_FCVT_D_L : RISCVI_FCVT_D_LU) :
512 (st == IRT_INT ? RISCVI_FCVT_D_W : RISCVI_FCVT_D_WU));
513 emit_ds(as, riscvi, dest, left);
514 }
515 } else if (stfp) { /* FP to integer conversion. */
516 if (irt_isguard(ir->t)) {
517 /* Checked conversions are only supported from number to int. */
518 lj_assertA(irt_isint(ir->t) && st == IRT_NUM,
519 "bad type for checked CONV");
520 asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR));
521 } else {
522 Reg left = ra_alloc1(as, lref, RSET_FPR);
523 Reg dest = ra_dest(as, ir, RSET_GPR);
524 RISCVIns riscvi = irt_is64(ir->t) ?
525 (st == IRT_NUM ?
526 (irt_isi64(ir->t) ? RISCVI_FCVT_L_D : RISCVI_FCVT_LU_D) :
527 (irt_isi64(ir->t) ? RISCVI_FCVT_L_S : RISCVI_FCVT_LU_S)) :
528 (st == IRT_NUM ?
529 (irt_isint(ir->t) ? RISCVI_FCVT_W_D : RISCVI_FCVT_WU_D) :
530 (irt_isint(ir->t) ? RISCVI_FCVT_W_S : RISCVI_FCVT_WU_S));
531 emit_ds(as, riscvi|RISCVF_RM(RISCVRM_RTZ), dest, left);
532 }
533 } else if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */
534 Reg dest = ra_dest(as, ir, RSET_GPR);
535 Reg left = ra_alloc1(as, lref, RSET_GPR);
536 RISCVIns riscvi = st == IRT_I8 ? RISCVI_SEXT_B :
537 st == IRT_U8 ? RISCVI_ZEXT_B :
538 st == IRT_I16 ? RISCVI_SEXT_H : RISCVI_ZEXT_H;
539 lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT");
540 emit_ext(as, riscvi, dest, left);
541 } else { /* 32/64 bit integer conversions. */
542 Reg dest = ra_dest(as, ir, RSET_GPR);
543 if (irt_is64(ir->t)) {
544 if (st64) {
545 /* 64/64 bit no-op (cast)*/
546 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
547 } else { /* 32 to 64 bit sign extension. */
548 Reg left = ra_alloc1(as, lref, RSET_GPR);
549 if ((ir->op2 & IRCONV_SEXT)) { /* 32 to 64 bit sign extension. */
550 emit_ext(as, RISCVI_SEXT_W, dest, left);
551 } else { /* 32 to 64 bit zero extension. */
552 emit_ext(as, RISCVI_ZEXT_W, dest, left);
553 }
554 }
555 } else {
556 if (st64 && !(ir->op2 & IRCONV_NONE)) {
557 /* This is either a 32 bit reg/reg mov which zeroes the hiword
558 ** or a load of the loword from a 64 bit address.
559 */
560 Reg left = ra_alloc1(as, lref, RSET_GPR);
561 emit_ext(as, RISCVI_ZEXT_W, dest, left);
562 } else { /* 32/32 bit no-op (cast). */
563 ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */
564 }
565 }
566 }
567 }
568
569 static void asm_strto(ASMState *as, IRIns *ir)
570 {
571 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num];
572 IRRef args[2];
573 int32_t ofs = SPOFS_TMP;
574 RegSet drop = RSET_SCRATCH;
575 if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */
576 ra_evictset(as, drop);
577 if (ir->s) ofs = sps_scale(ir->s);
578 asm_guard(as, RISCVI_BEQ, RID_RET, RID_ZERO); /* Test return status. */
579 args[0] = ir->op1; /* GCstr *str */
580 args[1] = ASMREF_TMP1; /* TValue *n */
581 asm_gencall(as, ci, args);
582 /* Store the result to the spill slot or temp slots. */
583 Reg tmp = ra_releasetmp(as, ASMREF_TMP1);
584 emit_opk(as, RISCVI_ADDI, tmp, RID_SP, tmp, ofs);
585 }
586
587 /* -- Memory references --------------------------------------------------- */
588
589 /* Store tagged value for ref at base+ofs. */
590 static void asm_tvstore64(ASMState *as, Reg base, int32_t ofs, IRRef ref)
591 {
592 RegSet allow = rset_exclude(RSET_GPR, base);
593 IRIns *ir = IR(ref);
594 lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t),
595 "store of IR type %d", irt_type(ir->t));
596 if (irref_isk(ref)) {
597 TValue k;
598 lj_ir_kvalue(as->J->L, &k, ir);
599 emit_lso(as, RISCVI_SD, ra_allock(as, (int64_t)k.u64, allow), base, ofs);
600 } else {
601 Reg src = ra_alloc1(as, ref, allow);
602 rset_clear(allow, src);
603 Reg type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47, allow);
604 emit_lso(as, RISCVI_SD, RID_TMP, base, ofs);
605 if (irt_isinteger(ir->t)) {
606 emit_ds1s2(as, RISCVI_ADD, RID_TMP, RID_TMP, type);
607 emit_ext(as, RISCVI_ZEXT_W, RID_TMP, src);
608 } else {
609 emit_ds1s2(as, RISCVI_ADD, RID_TMP, src, type);
610 }
611 }
612 }
613
614 /* Get pointer to TValue. */
615 static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode) // todo-new
616 {
617 if ((mode & IRTMPREF_IN1)) {
618 IRIns *ir = IR(ref);
619 if (irt_isnum(ir->t)) {
620 if (irref_isk(ref) && !(mode & IRTMPREF_OUT1)) {
621 /* Use the number constant itself as a TValue. */
622 ra_allockreg(as, igcptr(ir_knum(ir)), dest);
623 return;
624 }
625 emit_lso(as, RISCVI_FSD, ra_alloc1(as, ref, RSET_FPR), dest, 0);
626 } else {
627 asm_tvstore64(as, dest, 0, ref);
628 }
629 }
630 /* g->tmptv holds the TValue(s). */
631 emit_opk(as, RISCVI_ADDI, dest, RID_GL, dest, offsetof(global_State, tmptv));
632 }
633
634 static void asm_aref(ASMState *as, IRIns *ir)
635 {
636 Reg dest = ra_dest(as, ir, RSET_GPR);
637 Reg idx, base;
638 if (irref_isk(ir->op2)) {
639 IRRef tab = IR(ir->op1)->op1;
640 int32_t ofs = asm_fuseabase(as, tab);
641 IRRef refa = ofs ? tab : ir->op1;
642 ofs += 8*IR(ir->op2)->i;
643 if (checki12(ofs)) {
644 base = ra_alloc1(as, refa, RSET_GPR);
645 emit_dsi(as, RISCVI_ADDI, dest, base, ofs);
646 return;
647 }
648 }
649 base = ra_alloc1(as, ir->op1, RSET_GPR);
650 idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base));
651 emit_sh3add(as, dest, base, idx, RID_TMP);
652 }
653
654 /* Inlined hash lookup. Specialized for key type and for const keys.
655 ** The equivalent C code is:
656 ** Node *n = hashkey(t, key);
657 ** do {
658 ** if (lj_obj_equal(&n->key, key)) return &n->val;
659 ** } while ((n = nextnode(n)));
660 ** return niltv(L);
661 */
662 static void asm_href(ASMState *as, IRIns *ir, IROp merge)
663 {
664 RegSet allow = RSET_GPR;
665 int destused = ra_used(ir);
666 Reg dest = ra_dest(as, ir, allow);
667 Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest));
668 Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1, tmp2;
669 Reg cmp64 = RID_NONE;
670 IRRef refkey = ir->op2;
671 IRIns *irkey = IR(refkey);
672 int isk = irref_isk(refkey);
673 IRType1 kt = irkey->t;
674 uint32_t khash;
675 MCLabel l_end, l_loop, l_next;
676 rset_clear(allow, tab);
677 tmp1 = ra_scratch(as, allow);
678 rset_clear(allow, tmp1);
679 tmp2 = ra_scratch(as, allow);
680 rset_clear(allow, tmp2);
681
682 if (irt_isnum(kt)) {
683 key = ra_alloc1(as, refkey, RSET_FPR);
684 tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key));
685 } else {
686 /* Allocate cmp64 register used for 64-bit comparisons */
687 if (!isk && irt_isaddr(kt)) {
688 cmp64 = tmp2;
689 } else {
690 int64_t k;
691 if (isk && irt_isaddr(kt)) {
692 k = ((int64_t)irt_toitype(kt) << 47) | irkey[1].tv.u64;
693 } else {
694 lj_assertA(irt_ispri(kt) && !irt_isnil(kt), "bad HREF key type");
695 k = ~((int64_t)~irt_toitype(kt) << 47);
696 }
697 cmp64 = ra_allock(as, k, allow);
698 rset_clear(allow, cmp64);
699 }
700 if (!irt_ispri(kt)) {
701 key = ra_alloc1(as, refkey, allow);
702 rset_clear(allow, key);
703 }
704 }
705
706 /* Key not found in chain: jump to exit (if merged) or load niltv. */
707 l_end = emit_label(as);
708 int is_lend_exit = 0;
709 as->invmcp = NULL;
710 if (merge == IR_NE)
711 asm_guard(as, RISCVI_BEQ, RID_ZERO, RID_ZERO);
712 else if (destused)
713 emit_loada(as, dest, niltvg(J2G(as->J)));
714
715 /* Follow hash chain until the end. */
716 l_loop = --as->mcp;
717 emit_mv(as, dest, tmp1);
718 emit_lso(as, RISCVI_LD, tmp1, dest, (int32_t)offsetof(Node, next));
719 l_next = emit_label(as);
720
721 /* Type and value comparison. */
722 if (merge == IR_EQ) { /* Must match asm_guard(). */
723 l_end = asm_exitstub_addr(as, as->snapno);
724 is_lend_exit = 1;
725 }
726 if (irt_isnum(kt)) {
727 emit_branch(as, RISCVI_BNE, tmp1, RID_ZERO, l_end, is_lend_exit);
728 emit_ds1s2(as, RISCVI_FEQ_D, tmp1, tmpnum, key);
729 emit_branch(as, RISCVI_BEQ, tmp1, RID_ZERO, l_next, 0);
730 emit_dsi(as, RISCVI_SLTIU, tmp1, tmp1, ((int32_t)LJ_TISNUM));
731 emit_dsshamt(as, RISCVI_SRAI, tmp1, tmp1, 47);
732 emit_ds(as, RISCVI_FMV_D_X, tmpnum, tmp1);
733 } else {
734 emit_branch(as, RISCVI_BEQ, tmp1, cmp64, l_end, is_lend_exit);
735 }
736 emit_lso(as, RISCVI_LD, tmp1, dest, (int32_t)offsetof(Node, key.u64));
737 *l_loop = RISCVI_BNE | RISCVF_S1(tmp1) | RISCVF_S2(RID_ZERO)
738 | RISCVF_IMMB((char *)as->mcp-(char *)l_loop);
739 if (!isk && irt_isaddr(kt)) {
740 type = ra_allock(as, (int64_t)irt_toitype(kt) << 47, allow);
741 emit_ds1s2(as, RISCVI_ADD, tmp2, key, type);
742 rset_clear(allow, type);
743 }
744
745 /* Load main position relative to tab->node into dest. */
746 khash = isk ? ir_khash(as, irkey) : 1;
747 if (khash == 0) {
748 emit_lso(as, RISCVI_LD, dest, tab, (int32_t)offsetof(GCtab, node));
749 } else {
750 Reg tmphash = tmp1;
751 if (isk)
752 tmphash = ra_allock(as, khash, allow);
753 /* node = tab->node + (idx*32-idx*8) */
754 emit_ds1s2(as, RISCVI_ADD, dest, dest, tmp1);
755 lj_assertA(sizeof(Node) == 24, "bad Node size");
756 emit_ds1s2(as, RISCVI_SUBW, tmp1, tmp2, tmp1);
757 emit_dsshamt(as, RISCVI_SLLIW, tmp1, tmp1, 3);
758 emit_dsshamt(as, RISCVI_SLLIW, tmp2, tmp1, 5);
759 emit_ds1s2(as, RISCVI_AND, tmp1, tmp2, tmphash); // idx = hi & tab->hmask
760 emit_lso(as, RISCVI_LD, dest, tab, (int32_t)offsetof(GCtab, node));
761 emit_lso(as, RISCVI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask));
762 if (isk) {
763 /* Nothing to do. */
764 } else if (irt_isstr(kt)) {
765 emit_lso(as, RISCVI_LW, tmp1, key, (int32_t)offsetof(GCstr, sid));
766 } else { /* Must match with hash*() in lj_tab.c. */
767 emit_ds1s2(as, RISCVI_SUBW, tmp1, tmp1, tmp2);
768 emit_roti(as, RISCVI_RORIW, tmp2, tmp2, dest, (-HASH_ROT3)&0x1f);
769 emit_ds1s2(as, RISCVI_XOR, tmp1, tmp1, tmp2);
770 emit_roti(as, RISCVI_RORIW, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&0x1f);
771 emit_ds1s2(as, RISCVI_SUBW, tmp2, tmp2, dest);
772 emit_ds1s2(as, RISCVI_XOR, tmp2, tmp2, tmp1);
773 emit_roti(as, RISCVI_RORIW, dest, tmp1, RID_TMP, (-HASH_ROT1)&0x1f);
774 if (irt_isnum(kt)) {
775 emit_dsshamt(as, RISCVI_SLLIW, tmp1, tmp1, 1);
776 emit_dsshamt(as, RISCVI_SRAI, tmp1, tmp1, 32); // hi
777 emit_ext(as, RISCVI_SEXT_W, tmp2, tmp1); // lo
778 emit_ds(as, RISCVI_FMV_X_D, tmp1, key);
779 } else {
780 checkmclim(as);
781 emit_dsshamt(as, RISCVI_SRAI, tmp1, tmp1, 32); // hi
782 emit_ext(as, RISCVI_SEXT_W, tmp2, key); // lo
783 emit_ds1s2(as, RISCVI_ADD, tmp1, key, type);
784 }
785 }
786 }
787 }
788
789 static void asm_hrefk(ASMState *as, IRIns *ir)
790 {
791 IRIns *kslot = IR(ir->op2);
792 IRIns *irkey = IR(kslot->op1);
793 int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node));
794 int32_t kofs = ofs + (int32_t)offsetof(Node, key);
795 int bigofs = !checki12(kofs);
796 Reg dest = (ra_used(ir) || bigofs) ? ra_dest(as, ir, RSET_GPR) : RID_NONE;
797 Reg node = ra_alloc1(as, ir->op1, RSET_GPR);
798 RegSet allow = rset_exclude(RSET_GPR, node);
799 Reg idx = node;
800 int64_t k;
801 lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot");
802 if (bigofs) {
803 idx = dest;
804 rset_clear(allow, dest);
805 kofs = (int32_t)offsetof(Node, key);
806 } else if (ra_hasreg(dest)) {
807 emit_dsi(as, RISCVI_ADDI, dest, node, ofs);
808 }
809 if (irt_ispri(irkey->t)) {
810 lj_assertA(!irt_isnil(irkey->t), "bad HREFK key type");
811 k = ~((int64_t)~irt_toitype(irkey->t) << 47);
812 } else if (irt_isnum(irkey->t)) {
813 k = (int64_t)ir_knum(irkey)->u64;
814 } else {
815 k = ((int64_t)irt_toitype(irkey->t) << 47) | (int64_t)ir_kgc(irkey);
816 }
817 asm_guard(as, RISCVI_BNE, RID_TMP, ra_allock(as, k, allow));
818 emit_lso(as, RISCVI_LD, RID_TMP, idx, kofs);
819 if (bigofs)
820 emit_ds1s2(as, RISCVI_ADD, dest, node, ra_allock(as, ofs, allow));
821 }
822
823 static void asm_uref(ASMState *as, IRIns *ir)
824 {
825 Reg dest = ra_dest(as, ir, RSET_GPR);
826 int guarded = (irt_t(ir->t) & (IRT_GUARD|IRT_TYPE)) == (IRT_GUARD|IRT_PGC);
827 if (irref_isk(ir->op1) && !guarded) {
828 GCfunc *fn = ir_kfunc(IR(ir->op1));
829 MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v;
830 emit_lsptr(as, RISCVI_LD, dest, v, RSET_GPR);
831 } else {
832 if (guarded)
833 asm_guard(as, ir->o == IR_UREFC ? RISCVI_BEQ : RISCVI_BNE, RID_TMP, RID_ZERO);
834 if (ir->o == IR_UREFC)
835 emit_dsi(as, RISCVI_ADDI, dest, dest, (int32_t)offsetof(GCupval, tv));
836 else
837 emit_lso(as, RISCVI_LD, dest, dest, (int32_t)offsetof(GCupval, v));
838 if (guarded)
839 emit_lso(as, RISCVI_LBU, RID_TMP, dest, (int32_t)offsetof(GCupval, closed));
840 if (irref_isk(ir->op1)) {
841 GCfunc *fn = ir_kfunc(IR(ir->op1));
842 GCobj *o = gcref(fn->l.uvptr[(ir->op2 >> 8)]);
843 emit_loada(as, dest, o);
844 } else {
845 emit_lso(as, RISCVI_LD, dest, ra_alloc1(as, ir->op1, RSET_GPR),
846 (int32_t)offsetof(GCfuncL, uvptr) +
847 (int32_t)sizeof(MRef) * (int32_t)(ir->op2 >> 8));
848 }
849 }
850 }
851
852 static void asm_fref(ASMState *as, IRIns *ir)
853 {
854 UNUSED(as); UNUSED(ir);
855 lj_assertA(!ra_used(ir), "unfused FREF");
856 }
857
858 static void asm_strref(ASMState *as, IRIns *ir)
859 {
860 RegSet allow = RSET_GPR;
861 Reg dest = ra_dest(as, ir, allow);
862 Reg base = ra_alloc1(as, ir->op1, allow);
863 IRIns *irr = IR(ir->op2);
864 int32_t ofs = sizeof(GCstr);
865 rset_clear(allow, base);
866 if (irref_isk(ir->op2) && checki12(ofs + irr->i)) {
867 emit_dsi(as, RISCVI_ADDI, dest, base, ofs + irr->i);
868 } else {
869 emit_dsi(as, RISCVI_ADDI, dest, dest, ofs);
870 emit_ds1s2(as, RISCVI_ADD, dest, base, ra_alloc1(as, ir->op2, allow));
871 }
872 }
873
874 /* -- Loads and stores ---------------------------------------------------- */
875
876 static RISCVIns asm_fxloadins(IRIns *ir)
877 {
878 switch (irt_type(ir->t)) {
879 case IRT_I8: return RISCVI_LB;
880 case IRT_U8: return RISCVI_LBU;
881 case IRT_I16: return RISCVI_LH;
882 case IRT_U16: return RISCVI_LHU;
883 case IRT_NUM: return RISCVI_FLD;
884 case IRT_FLOAT: return RISCVI_FLW;
885 default: return irt_is64(ir->t) ? RISCVI_LD : RISCVI_LW;
886 }
887 }
888
889 static RISCVIns asm_fxstoreins(IRIns *ir)
890 {
891 switch (irt_type(ir->t)) {
892 case IRT_I8: case IRT_U8: return RISCVI_SB;
893 case IRT_I16: case IRT_U16: return RISCVI_SH;
894 case IRT_NUM: return RISCVI_FSD;
895 case IRT_FLOAT: return RISCVI_FSW;
896 default: return irt_is64(ir->t) ? RISCVI_SD : RISCVI_SW;
897 }
898 }
899
900 static void asm_fload(ASMState *as, IRIns *ir)
901 {
902 RegSet allow = RSET_GPR;
903 Reg idx, dest = ra_dest(as, ir, allow);
904 rset_clear(allow, dest);
905 RISCVIns riscvi = asm_fxloadins(ir);
906 int32_t ofs;
907 if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */
908 idx = RID_GL;
909 ofs = (ir->op2 << 2) - GG_OFS(g);
910 } else {
911 idx = ra_alloc1(as, ir->op1, allow);
912 if (ir->op2 == IRFL_TAB_ARRAY) {
913 ofs = asm_fuseabase(as, ir->op1);
914 if (ofs) { /* Turn the t->array load into an add for colocated arrays. */
915 emit_dsi(as, RISCVI_ADDI, dest, idx, ofs);
916 return;
917 }
918 }
919 ofs = field_ofs[ir->op2];
920 lj_assertA(!irt_isfp(ir->t), "bad FP FLOAD");
921 }
922 rset_clear(allow, idx);
923 emit_lso(as, riscvi, dest, idx, ofs);
924 }
925
926 static void asm_fstore(ASMState *as, IRIns *ir)
927 {
928 if (ir->r != RID_SINK) {
929 Reg src = ra_alloc1z(as, ir->op2, RSET_GPR);
930 IRIns *irf = IR(ir->op1);
931 Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src));
932 int32_t ofs = field_ofs[irf->op2];
933 lj_assertA(!irt_isfp(ir->t), "bad FP FSTORE");
934 emit_lso(as, asm_fxstoreins(ir), src, idx, ofs);
935 }
936 }
937
938 static void asm_xload(ASMState *as, IRIns *ir)
939 {
940 Reg dest = ra_dest(as, ir, (irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR);
941 lj_assertA(LJ_TARGET_UNALIGNED || !(ir->op2 & IRXLOAD_UNALIGNED),
942 "unaligned XLOAD");
943 asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0);
944 }
945
946 static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs)
947 {
948 if (ir->r != RID_SINK) {
949 Reg src = ra_alloc1z(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR);
950 asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1,
951 rset_exclude(RSET_GPR, src), ofs);
952 }
953 }
954
955 #define asm_xstore(as, ir) asm_xstore_(as, ir, 0)
956
957 static void asm_ahuvload(ASMState *as, IRIns *ir)
958 {
959 Reg dest = RID_NONE, type = RID_TMP, idx;
960 RegSet allow = RSET_GPR;
961 int32_t ofs = 0;
962 IRType1 t = ir->t;
963 if (ra_used(ir)) {
964 lj_assertA((irt_isnum(ir->t)) || irt_isint(ir->t) || irt_isaddr(ir->t),
965 "bad load type %d", irt_type(ir->t));
966 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : allow);
967 rset_clear(allow, dest);
968 if (irt_isaddr(t)) {
969 emit_cleartp(as, dest, dest);
970 } else if (irt_isint(t))
971 emit_ext(as, RISCVI_SEXT_W, dest, dest);
972 }
973 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
974 if (ir->o == IR_VLOAD) ofs += 8 * ir->op2;
975 rset_clear(allow, idx);
976 if (irt_isnum(t)) {
977 asm_guard(as, RISCVI_BEQ, RID_TMP, RID_ZERO);
978 emit_dsi(as, RISCVI_SLTIU, RID_TMP, type, (int32_t)LJ_TISNUM);
979 } else {
980 asm_guard(as, RISCVI_BNE, type,
981 ra_allock(as, (int32_t)irt_toitype(t), allow));
982 }
983 if (ra_hasreg(dest)) {
984 if (irt_isnum(t)) {
985 emit_lso(as, RISCVI_FLD, dest, idx, ofs);
986 dest = type;
987 }
988 } else {
989 dest = type;
990 }
991 emit_dsshamt(as, RISCVI_SRAI, type, dest, 47);
992 emit_lso(as, RISCVI_LD, dest, idx, ofs);
993 }
994
995 static void asm_ahustore(ASMState *as, IRIns *ir)
996 {
997 RegSet allow = RSET_GPR;
998 Reg idx, src = RID_NONE, type = RID_NONE;
999 int32_t ofs = 0;
1000 if (ir->r == RID_SINK)
1001 return;
1002 if (irt_isnum(ir->t)) {
1003 src = ra_alloc1(as, ir->op2, RSET_FPR);
1004 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
1005 emit_lso(as, RISCVI_FSD, src, idx, ofs);
1006 } else {
1007 Reg tmp = RID_TMP;
1008 if (irt_ispri(ir->t)) {
1009 tmp = ra_allock(as, ~((int64_t)~irt_toitype(ir->t) << 47), allow);
1010 rset_clear(allow, tmp);
1011 } else {
1012 src = ra_alloc1(as, ir->op2, allow);
1013 rset_clear(allow, src);
1014 type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47, allow);
1015 rset_clear(allow, type);
1016 }
1017 idx = asm_fuseahuref(as, ir->op1, &ofs, allow);
1018 emit_lso(as, RISCVI_SD, tmp, idx, ofs);
1019 if (ra_hasreg(src)) {
1020 if (irt_isinteger(ir->t)) {
1021 emit_ds1s2(as, RISCVI_ADD, tmp, tmp, type);
1022 emit_ext(as, RISCVI_ZEXT_W, tmp, src);
1023 } else {
1024 emit_ds1s2(as, RISCVI_ADD, tmp, src, type);
1025 }
1026 }
1027 }
1028 }
1029
1030 static void asm_sload(ASMState *as, IRIns *ir)
1031 {
1032 Reg dest = RID_NONE, type = RID_NONE, base;
1033 RegSet allow = RSET_GPR;
1034 IRType1 t = ir->t;
1035 int32_t ofs = 8*((int32_t)ir->op1-2);
1036 lj_assertA(checki12(ofs), "sload IR operand out of range");
1037 lj_assertA(!(ir->op2 & IRSLOAD_PARENT),
1038 "bad parent SLOAD"); /* Handled by asm_head_side(). */
1039 lj_assertA(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK),
1040 "inconsistent SLOAD variant");
1041 if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) {
1042 dest = ra_scratch(as, RSET_FPR);
1043 asm_tointg(as, ir, dest);
1044 t.irt = IRT_NUM; /* Continue with a regular number type check. */
1045 } else if (ra_used(ir)) {
1046 Reg tmp = RID_NONE;
1047 if ((ir->op2 & IRSLOAD_CONVERT))
1048 tmp = ra_scratch(as, irt_isint(t) ? RSET_FPR : RSET_GPR);
1049 lj_assertA((irt_isnum(t)) || irt_isint(t) || irt_isaddr(t),
1050 "bad SLOAD type %d", irt_type(t));
1051 dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : allow);
1052 rset_clear(allow, dest);
1053 base = ra_alloc1(as, REF_BASE, allow);
1054 rset_clear(allow, base);
1055 if (irt_isaddr(t)) { /* Clear type from pointers. */
1056 emit_cleartp(as, dest, dest);
1057 } else if (ir->op2 & IRSLOAD_CONVERT) {
1058 if (irt_isint(t)) {
1059 emit_ds(as, RISCVI_FCVT_W_D|RISCVF_RM(RISCVRM_RTZ), dest, tmp);
1060 /* If value is already loaded for type check, move it to FPR. */
1061 if ((ir->op2 & IRSLOAD_TYPECHECK))
1062 emit_ds(as, RISCVI_FMV_D_X, tmp, dest);
1063 else
1064 dest = tmp;
1065 t.irt = IRT_NUM; /* Check for original type. */
1066 } else {
1067 emit_ds(as, RISCVI_FCVT_D_W, dest, tmp);
1068 dest = tmp;
1069 t.irt = IRT_INT; /* Check for original type. */
1070 }
1071 } else if (irt_isint(t) && (ir->op2 & IRSLOAD_TYPECHECK)) {
1072 /* Sign-extend integers. */
1073 emit_ext(as, RISCVI_SEXT_W, dest, dest);
1074 }
1075 goto dotypecheck;
1076 }
1077 base = ra_alloc1(as, REF_BASE, allow);
1078 rset_clear(allow, base);
1079 dotypecheck:
1080 if ((ir->op2 & IRSLOAD_TYPECHECK)) {
1081 type = dest < RID_MAX_GPR ? dest : RID_TMP;
1082 if (irt_ispri(t)) {
1083 asm_guard(as, RISCVI_BNE, type,
1084 ra_allock(as, ~((int64_t)~irt_toitype(t) << 47) , allow));
1085 } else if ((ir->op2 & IRSLOAD_KEYINDEX)) {
1086 asm_guard(as, RISCVI_BNE, RID_TMP,
1087 ra_allock(as, (int32_t)LJ_KEYINDEX, allow));
1088 emit_dsshamt(as, RISCVI_SRAI, RID_TMP, type, 32);
1089 } else {
1090 if (irt_isnum(t)) {
1091 asm_guard(as, RISCVI_BEQ, RID_TMP, RID_ZERO);
1092 emit_dsi(as, RISCVI_SLTIU, RID_TMP, RID_TMP, LJ_TISNUM);
1093 if (ra_hasreg(dest)) {
1094 emit_lso(as, RISCVI_FLD, dest, base, ofs);
1095 }
1096 } else {
1097 asm_guard(as, RISCVI_BNE, RID_TMP,
1098 ra_allock(as, (int32_t)irt_toitype(t), allow));
1099 }
1100 emit_dsshamt(as, RISCVI_SRAI, RID_TMP, type, 47);
1101 }
1102 emit_lso(as, RISCVI_LD, type, base, ofs);
1103 } else if (ra_hasreg(dest)) {
1104 emit_lso(as, irt_isnum(t) ? RISCVI_FLD :
1105 irt_isint(t) ? RISCVI_LW : RISCVI_LD,
1106 dest, base, ofs);
1107 }
1108 }
1109
1110 /* -- Allocations --------------------------------------------------------- */
1111
1112 #if LJ_HASFFI
1113 static void asm_cnew(ASMState *as, IRIns *ir)
1114 {
1115 CTState *cts = ctype_ctsG(J2G(as->J));
1116 CTypeID id = (CTypeID)IR(ir->op1)->i;
1117 CTSize sz;
1118 CTInfo info = lj_ctype_info(cts, id, &sz);
1119 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco];
1120 IRRef args[4];
1121 RegSet drop = RSET_SCRATCH;
1122 lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL),
1123 "bad CNEW/CNEWI operands");
1124
1125 as->gcsteps++;
1126 if (ra_hasreg(ir->r))
1127 rset_clear(drop, ir->r); /* Dest reg handled below. */
1128 ra_evictset(as, drop);
1129 if (ra_used(ir))
1130 ra_destreg(as, ir, RID_RET); /* GCcdata * */
1131
1132 /* Initialize immutable cdata object. */
1133 if (ir->o == IR_CNEWI) {
1134 RegSet allow = (RSET_GPR & ~RSET_SCRATCH);
1135 emit_lso(as, sz == 8 ? RISCVI_SD : RISCVI_SW, ra_alloc1(as, ir->op2, allow),
1136 RID_RET, (sizeof(GCcdata)));
1137 lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz);
1138 } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */
1139 ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv];
1140 args[0] = ASMREF_L; /* lua_State *L */
1141 args[1] = ir->op1; /* CTypeID id */
1142 args[2] = ir->op2; /* CTSize sz */
1143 args[3] = ASMREF_TMP1; /* CTSize align */
1144 asm_gencall(as, ci, args);
1145 emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info));
1146 return;
1147 }
1148
1149 /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */
1150 emit_lso(as, RISCVI_SB, RID_RET+1, RID_RET, (offsetof(GCcdata, gct)));
1151 emit_lso(as, RISCVI_SH, RID_TMP, RID_RET, (offsetof(GCcdata, ctypeid)));
1152 emit_loadk12(as, RID_RET+1, ~LJ_TCDATA);
1153 emit_loadk32(as, RID_TMP, id);
1154 args[0] = ASMREF_L; /* lua_State *L */
1155 args[1] = ASMREF_TMP1; /* MSize size */
1156 asm_gencall(as, ci, args);
1157 ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)),
1158 ra_releasetmp(as, ASMREF_TMP1));
1159 }
1160 #endif
1161
1162 /* -- Write barriers ------------------------------------------------------ */
1163
1164 static void asm_tbar(ASMState *as, IRIns *ir)
1165 {
1166 Reg tab = ra_alloc1(as, ir->op1, RSET_GPR);
1167 Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab));
1168 Reg link = RID_TMP;
1169 MCLabel l_end = emit_label(as);
1170 emit_lso(as, RISCVI_SD, link, tab, (int32_t)offsetof(GCtab, gclist));
1171 emit_lso(as, RISCVI_SB, mark, tab, (int32_t)offsetof(GCtab, marked));
1172 emit_setgl(as, tab, gc.grayagain); // make tab gray again
1173 emit_getgl(as, link, gc.grayagain);
1174 emit_branch(as, RISCVI_BEQ, RID_TMP, RID_ZERO, l_end, 0); // black: not jump
1175 emit_ds1s2(as, RISCVI_XOR, mark, mark, RID_TMP); // mark=0: gray
1176 emit_dsi(as, RISCVI_ANDI, RID_TMP, mark, LJ_GC_BLACK);
1177 emit_lso(as, RISCVI_LBU, mark, tab, ((int32_t)offsetof(GCtab, marked)));
1178 }
1179
1180 static void asm_obar(ASMState *as, IRIns *ir)
1181 {
1182 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv];
1183 IRRef args[2];
1184 MCLabel l_end;
1185 Reg obj, val, tmp;
1186 /* No need for other object barriers (yet). */
1187 lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type"); // Closed upvalue
1188 ra_evictset(as, RSET_SCRATCH);
1189 l_end = emit_label(as);
1190 args[0] = ASMREF_TMP1; /* global_State *g */
1191 args[1] = ir->op1; /* TValue *tv */
1192 asm_gencall(as, ci, args);
1193 emit_ds(as, RISCVI_MV, ra_releasetmp(as, ASMREF_TMP1), RID_GL);
1194 obj = IR(ir->op1)->r;
1195 tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj));
1196 emit_branch(as, RISCVI_BEQ, tmp, RID_ZERO, l_end, 0);
1197 emit_branch(as, RISCVI_BEQ, RID_TMP, RID_ZERO, l_end, 0); // black: jump
1198 emit_dsi(as, RISCVI_ANDI, tmp, tmp, LJ_GC_BLACK);
1199 emit_dsi(as, RISCVI_ANDI, RID_TMP, RID_TMP, LJ_GC_WHITES);
1200 val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj));
1201 emit_lso(as, RISCVI_LBU, tmp, obj,
1202 ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)));
1203 emit_lso(as, RISCVI_LBU, RID_TMP, val, ((int32_t)offsetof(GChead, marked)));
1204 }
1205
1206 /* -- Arithmetic and logic operations ------------------------------------- */
1207
1208 static void asm_fparith(ASMState *as, IRIns *ir, RISCVIns riscvi)
1209 {
1210 Reg dest = ra_dest(as, ir, RSET_FPR);
1211 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1212 right = (left >> 8); left &= 255;
1213 emit_ds1s2(as, riscvi, dest, left, right);
1214 }
1215
1216 static void asm_fpunary(ASMState *as, IRIns *ir, RISCVIns riscvi)
1217 {
1218 Reg dest = ra_dest(as, ir, RSET_FPR);
1219 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR);
1220 switch(riscvi) {
1221 case RISCVI_FSQRT_S: case RISCVI_FSQRT_D:
1222 emit_ds(as, riscvi, dest, left);
1223 break;
1224 case RISCVI_FMV_S: case RISCVI_FMV_D:
1225 case RISCVI_FABS_S: case RISCVI_FABS_D:
1226 case RISCVI_FNEG_S: case RISCVI_FNEG_D:
1227 emit_ds1s2(as, riscvi, dest, left, left);
1228 break;
1229 default:
1230 lj_assertA(0, "bad fp unary instruction");
1231 return;
1232 }
1233 }
1234
1235 static void asm_fpmath(ASMState *as, IRIns *ir)
1236 {
1237 IRFPMathOp fpm = (IRFPMathOp)ir->op2;
1238 if (fpm <= IRFPM_TRUNC)
1239 asm_callround(as, ir, IRCALL_lj_vm_floor + fpm);
1240 else if (fpm == IRFPM_SQRT)
1241 asm_fpunary(as, ir, RISCVI_FSQRT_D);
1242 else
1243 asm_callid(as, ir, IRCALL_lj_vm_floor + fpm);
1244 }
1245
1246 static void asm_add(ASMState *as, IRIns *ir)
1247 {
1248 IRType1 t = ir->t;
1249 if (irt_isnum(t)) {
1250 if (!asm_fusemadd(as, ir, RISCVI_FMADD_D, RISCVI_FMADD_D))
1251 asm_fparith(as, ir, RISCVI_FADD_D);
1252 return;
1253 } else {
1254 if ((as->flags & JIT_F_RVXThead) && asm_fusemac(as, ir, RISCVI_TH_MULA))
1255 return;
1256 Reg dest = ra_dest(as, ir, RSET_GPR);
1257 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1258 if (irref_isk(ir->op2)) {
1259 intptr_t k = get_kval(as, ir->op2);
1260 if (checki12(k)) {
1261 if (irt_is64(t)) {
1262 emit_dsi(as, RISCVI_ADDI, dest, left, k);
1263 } else {
1264 emit_dsi(as, RISCVI_ADDIW, dest, left, k);
1265 }
1266 return;
1267 }
1268 }
1269 Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1270 emit_ds1s2(as, irt_is64(t) ? RISCVI_ADD : RISCVI_ADDW, dest,
1271 left, right);
1272 }
1273 }
1274
1275 static void asm_sub(ASMState *as, IRIns *ir)
1276 {
1277 if (irt_isnum(ir->t)) {
1278 if (!asm_fusemadd(as, ir, RISCVI_FMSUB_D, RISCVI_FNMSUB_D))
1279 asm_fparith(as, ir, RISCVI_FSUB_D);
1280 return;
1281 } else {
1282 if ((as->flags & JIT_F_RVXThead) && asm_fusemac(as, ir, RISCVI_TH_MULS))
1283 return;
1284 Reg dest = ra_dest(as, ir, RSET_GPR);
1285 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1286 right = (left >> 8); left &= 255;
1287 emit_ds1s2(as, irt_is64(ir->t) ? RISCVI_SUB : RISCVI_SUBW, dest,
1288 left, right);
1289 }
1290 }
1291
1292 static void asm_mul(ASMState *as, IRIns *ir)
1293 {
1294 if (irt_isnum(ir->t)) {
1295 asm_fparith(as, ir, RISCVI_FMUL_D);
1296 } else {
1297 Reg dest = ra_dest(as, ir, RSET_GPR);
1298 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1299 right = (left >> 8); left &= 255;
1300 emit_ds1s2(as, irt_is64(ir->t) ? RISCVI_MUL : RISCVI_MULW, dest,
1301 left, right);
1302 }
1303 }
1304
1305 static void asm_fpdiv(ASMState *as, IRIns *ir)
1306 {
1307 asm_fparith(as, ir, RISCVI_FDIV_D);
1308 }
1309
1310 static void asm_neg(ASMState *as, IRIns *ir)
1311 {
1312 if (irt_isnum(ir->t)) {
1313 asm_fpunary(as, ir, RISCVI_FNEG_D);
1314 } else {
1315 Reg dest = ra_dest(as, ir, RSET_GPR);
1316 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1317 emit_ds1s2(as, irt_is64(ir->t) ? RISCVI_SUB : RISCVI_SUBW, dest,
1318 RID_ZERO, left);
1319 }
1320 }
1321
1322 #define asm_abs(as, ir) asm_fpunary(as, ir, RISCVI_FABS_D)
1323
1324 static void asm_arithov(ASMState *as, IRIns *ir)
1325 {
1326 Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR);
1327 lj_assertA(!irt_is64(ir->t), "bad usage");
1328 if (irref_isk(ir->op2)) {
1329 int k = IR(ir->op2)->i;
1330 if (ir->o == IR_SUBOV) k = (int)(~(unsigned int)k+1u);
1331 if (checki12(k)) { /* (dest < left) == (k >= 0 ? 1 : 0) */
1332 left = ra_alloc1(as, ir->op1, RSET_GPR);
1333 asm_guard(as, k >= 0 ? RISCVI_BLT : RISCVI_BGE, dest, dest == left ? RID_TMP : left);
1334 emit_dsi(as, RISCVI_ADDI, dest, left, k);
1335 if (dest == left) emit_mv(as, RID_TMP, left);
1336 return;
1337 }
1338 }
1339 left = ra_alloc2(as, ir, RSET_GPR);
1340 right = (left >> 8); left &= 255;
1341 tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left),
1342 right), dest));
1343 asm_guard(as, RISCVI_BLT, RID_TMP, RID_ZERO);
1344 emit_ds1s2(as, RISCVI_AND, RID_TMP, RID_TMP, tmp);
1345 if (ir->o == IR_ADDOV) { /* ((dest^left) & (dest^right)) < 0 */
1346 emit_ds1s2(as, RISCVI_XOR, RID_TMP, dest, dest == right ? RID_TMP : right);
1347 } else { /* ((dest^left) & (dest^~right)) < 0 */
1348 emit_xnor(as, RID_TMP, dest, dest == right ? RID_TMP : right);
1349 }
1350 emit_ds1s2(as, RISCVI_XOR, tmp, dest, dest == left ? RID_TMP : left);
1351 emit_ds1s2(as, ir->o == IR_ADDOV ? RISCVI_ADDW : RISCVI_SUBW, dest, left, right);
1352 if (dest == left || dest == right)
1353 emit_mv(as, RID_TMP, dest == left ? left : right);
1354 }
1355
1356 #define asm_addov(as, ir) asm_arithov(as, ir)
1357 #define asm_subov(as, ir) asm_arithov(as, ir)
1358
1359 static void asm_mulov(ASMState *as, IRIns *ir)
1360 {
1361 Reg dest = ra_dest(as, ir, RSET_GPR);
1362 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1363 right = (left >> 8); left &= 255;
1364 asm_guard(as, RISCVI_BNE, dest, RID_TMP);
1365 emit_ext(as, RISCVI_SEXT_W, dest, RID_TMP); // dest: [31:0]+signextend
1366 emit_ds1s2(as, RISCVI_MUL, RID_TMP, left, right); // RID_TMP: [63:0]
1367 }
1368
1369 static void asm_bnot(ASMState *as, IRIns *ir)
1370 {
1371 Reg left, right, dest = ra_dest(as, ir, RSET_GPR);
1372 IRIns *irl = IR(ir->op1);
1373 if (as->flags & JIT_F_RVZbb && mayfuse(as, ir->op1) && irl->o == IR_BXOR) {
1374 left = ra_alloc2(as, irl, RSET_GPR);
1375 right = (left >> 8); left &= 255;
1376 emit_ds1s2(as, RISCVI_XNOR, dest, left, right);
1377 } else {
1378 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1379 emit_ds(as, RISCVI_NOT, dest, left);
1380 }
1381 }
1382
1383 static void asm_bswap(ASMState *as, IRIns *ir)
1384 {
1385 Reg dest = ra_dest(as, ir, RSET_GPR);
1386 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1387 RegSet allow = rset_exclude(rset_exclude(RSET_GPR, dest), left);
1388 if (as->flags & JIT_F_RVZbb) {
1389 if (!irt_is64(ir->t))
1390 emit_dsshamt(as, RISCVI_SRAI, dest, dest, 32);
1391 emit_ds(as, RISCVI_REV8, dest, left);
1392 } else if (as->flags & JIT_F_RVXThead) {
1393 emit_ds(as, irt_is64(ir->t) ? RISCVI_TH_REV : RISCVI_TH_REVW,
1394 dest, left);
1395 } else if (irt_is64(ir->t)) {
1396 Reg tmp1, tmp2, tmp3, tmp4;
1397 tmp1 = ra_scratch(as, allow), allow = rset_exclude(allow, tmp1);
1398 tmp2 = ra_scratch(as, allow), allow = rset_exclude(allow, tmp2);
1399 tmp3 = ra_scratch(as, allow), allow = rset_exclude(allow, tmp3);
1400 tmp4 = ra_scratch(as, allow);
1401 emit_ds1s2(as, RISCVI_OR, dest, dest, tmp4);
1402 emit_ds1s2(as, RISCVI_OR, dest, dest, tmp3);
1403 emit_ds1s2(as, RISCVI_OR, dest, dest, tmp2);
1404 emit_dsshamt(as, RISCVI_SLLI, tmp4, tmp4, 40);
1405 emit_dsshamt(as, RISCVI_SLLI, dest, left, 56);
1406 emit_ds1s2(as, RISCVI_OR, tmp3, tmp1, tmp3);
1407 emit_ds1s2(as, RISCVI_AND, tmp4, left, RID_TMP);
1408 emit_dsshamt(as, RISCVI_SLLI, tmp3, tmp3, 32);
1409 emit_dsshamt(as, RISCVI_SLLI, tmp1, tmp1, 24);
1410 emit_dsshamt(as, RISCVI_SRLIW, tmp3, left, 24);
1411 emit_ds1s2(as, RISCVI_OR, tmp2, tmp3, tmp2);
1412 emit_ds1s2(as, RISCVI_AND, tmp1, left, tmp1);
1413 emit_ds1s2(as, RISCVI_OR, tmp3, tmp4, tmp3);
1414 emit_dsshamt(as, RISCVI_SLLI, tmp4, tmp4, 24);
1415 emit_dsshamt(as, RISCVI_SRLIW, tmp4, tmp4, 24);
1416 emit_ds1s2(as, RISCVI_AND, tmp3, tmp3, tmp1);
1417 emit_dsshamt(as, RISCVI_SRLI, tmp4, left, 8);
1418 emit_dsshamt(as, RISCVI_SRLI, tmp3, left, 24);
1419 emit_ds1s2(as, RISCVI_OR, tmp2, tmp2, tmp3);
1420 emit_du(as, RISCVI_LUI, tmp1, RISCVF_HI(0xff0000u));
1421 emit_ds1s2(as, RISCVI_AND, tmp2, tmp2, RID_TMP);
1422 emit_dsshamt(as, RISCVI_SRLI, tmp3, left, 56);
1423 emit_dsi(as, RISCVI_ADDI, RID_TMP, RID_TMP, RISCVF_LO(0xff00));
1424 emit_du(as, RISCVI_LUI, RID_TMP, RISCVF_HI(0xff00u));
1425 emit_dsshamt(as, RISCVI_SRLI, tmp2, left, 40);
1426 } else {
1427 Reg tmp1, tmp2;
1428 tmp1 = ra_scratch(as, allow), allow = rset_exclude(allow, tmp1);
1429 tmp2 = ra_scratch(as, allow);
1430 emit_ds1s2(as, RISCVI_OR, dest, dest, tmp2);
1431 emit_ds1s2(as, RISCVI_OR, dest, dest, tmp1);
1432 emit_dsshamt(as, RISCVI_SLLI, tmp2, RID_TMP, 8);
1433 emit_dsshamt(as, RISCVI_SLLIW, dest, left, 24);
1434 emit_ds1s2(as, RISCVI_OR, tmp1, tmp1, tmp2);
1435 emit_ds1s2(as, RISCVI_AND, RID_TMP, left, RID_TMP);
1436 emit_ds1s2(as, RISCVI_AND, tmp1, tmp1, RID_TMP);
1437 emit_dsshamt(as, RISCVI_SRLIW, tmp2, left, 24);
1438 emit_dsi(as, RISCVI_ADDI, RID_TMP, RID_TMP, RISCVF_LO(0xff00));
1439 emit_du(as, RISCVI_LUI, RID_TMP, RISCVF_HI(0xff00u));
1440 emit_dsshamt(as, RISCVI_SRLI, tmp1, left, 8);
1441 }
1442 }
1443
1444 static void asm_bitop(ASMState *as, IRIns *ir, RISCVIns riscvi, RISCVIns riscvik, RISCVIns riscvin)
1445 {
1446 Reg dest = ra_dest(as, ir, RSET_GPR);
1447 Reg left, right;
1448 IRIns *irl = IR(ir->op1), *irr = IR(ir->op2);
1449 if (irref_isk(ir->op2)) {
1450 intptr_t k = get_kval(as, ir->op2);
1451 if (checki12(k)) {
1452 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1453 emit_dsi(as, riscvik, dest, left, k);
1454 return;
1455 }
1456 } else if (as->flags & JIT_F_RVZbb) {
1457 if (mayfuse(as, ir->op1) && irl->o == IR_BNOT) {
1458 left = ra_alloc1(as, irl->op1, RSET_GPR);
1459 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1460 emit_ds1s2(as, riscvin, dest, right, left);
1461 return;
1462 } else if (mayfuse(as, ir->op2) && irr->o == IR_BNOT) {
1463 left = ra_alloc1(as, ir->op1, RSET_GPR);
1464 right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left));
1465 emit_ds1s2(as, riscvin, dest, left, right);
1466 return;
1467 }
1468 }
1469 left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1470 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1471 emit_ds1s2(as, riscvi, dest, left, right);
1472 }
1473
1474 #define asm_band(as, ir) asm_bitop(as, ir, RISCVI_AND, RISCVI_ANDI, RISCVI_ANDN)
1475 #define asm_bor(as, ir) asm_bitop(as, ir, RISCVI_OR, RISCVI_ORI, RISCVI_ORN)
1476 #define asm_bxor(as, ir) asm_bitop(as, ir, RISCVI_XOR, RISCVI_XORI, RISCVI_XNOR)
1477
1478 static void asm_bitshift(ASMState *as, IRIns *ir, RISCVIns riscvi, RISCVIns riscvik)
1479 {
1480 Reg dest = ra_dest(as, ir, RSET_GPR);
1481 Reg left = ra_alloc1(as, ir->op1, RSET_GPR);
1482 uint32_t shmsk = irt_is64(ir->t) ? 63 : 31;
1483 if (irref_isk(ir->op2)) { /* Constant shifts. */
1484 uint32_t shift = (uint32_t)(IR(ir->op2)->i & shmsk);
1485 switch (riscvik) {
1486 case RISCVI_SRAI: case RISCVI_SRLI: case RISCVI_SLLI:
1487 case RISCVI_SRAIW: case RISCVI_SLLIW: case RISCVI_SRLIW:
1488 emit_dsshamt(as, riscvik, dest, left, shift);
1489 break;
1490 case RISCVI_RORI: case RISCVI_RORIW:
1491 emit_roti(as, riscvik, dest, left, RID_TMP, shift);
1492 break;
1493 default:
1494 lj_assertA(0, "bad shift instruction");
1495 return;
1496 }
1497 } else {
1498 Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1499 switch (riscvi) {
1500 case RISCVI_SRA: case RISCVI_SRL: case RISCVI_SLL:
1501 case RISCVI_SRAW: case RISCVI_SRLW: case RISCVI_SLLW:
1502 emit_ds1s2(as, riscvi, dest, left, right);
1503 break;
1504 case RISCVI_ROR: case RISCVI_ROL:
1505 case RISCVI_RORW: case RISCVI_ROLW:
1506 emit_rot(as, riscvi, dest, left, right, RID_TMP);
1507 break;
1508 default:
1509 lj_assertA(0, "bad shift instruction");
1510 return;
1511 }
1512 }
1513 }
1514
1515 #define asm_bshl(as, ir) (irt_is64(ir->t) ? \
1516 asm_bitshift(as, ir, RISCVI_SLL, RISCVI_SLLI) : \
1517 asm_bitshift(as, ir, RISCVI_SLLW, RISCVI_SLLIW))
1518 #define asm_bshr(as, ir) (irt_is64(ir->t) ? \
1519 asm_bitshift(as, ir, RISCVI_SRL, RISCVI_SRLI) : \
1520 asm_bitshift(as, ir, RISCVI_SRLW, RISCVI_SRLIW))
1521 #define asm_bsar(as, ir) (irt_is64(ir->t) ? \
1522 asm_bitshift(as, ir, RISCVI_SRA, RISCVI_SRAI) : \
1523 asm_bitshift(as, ir, RISCVI_SRAW, RISCVI_SRAIW))
1524 #define asm_brol(as, ir) lj_assertA(0, "unexpected BROL")
1525 #define asm_bror(as, ir) (irt_is64(ir->t) ? \
1526 asm_bitshift(as, ir, RISCVI_ROR, RISCVI_RORI) : \
1527 asm_bitshift(as, ir, RISCVI_RORW, RISCVI_RORIW))
1528
1529 static void asm_min_max(ASMState *as, IRIns *ir, int ismax)
1530 {
1531 if (irt_isnum(ir->t)) {
1532 Reg dest = ra_dest(as, ir, RSET_FPR);
1533 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1534 right = (left >> 8); left &= 255;
1535 emit_ds1s2(as, ismax ? RISCVI_FMAX_D : RISCVI_FMIN_D, dest, left, right);
1536 } else {
1537 Reg dest = ra_dest(as, ir, RSET_GPR);
1538 Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR);
1539 Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1540 if (as->flags & JIT_F_RVZbb) {
1541 emit_ds1s2(as, ismax ? RISCVI_MAX : RISCVI_MIN, dest, left, right);
1542 } else {
1543 if (as->flags & JIT_F_RVXThead) {
1544 if (left == right) {
1545 if (dest != left) emit_mv(as, dest, left);
1546 } else {
1547 if (dest == left) {
1548 emit_ds1s2(as, RISCVI_TH_MVNEZ, dest, right, RID_TMP);
1549 } else {
1550 emit_ds1s2(as, RISCVI_TH_MVEQZ, dest, left, RID_TMP);
1551 if (dest != right) emit_mv(as, dest, right);
1552 }
1553 }
1554 } else {
1555 emit_ds1s2(as, RISCVI_OR, dest, dest, RID_TMP);
1556 if (dest != right) {
1557 emit_ds1s2(as, RISCVI_AND, RID_TMP, right, RID_TMP);
1558 emit_ds(as, RISCVI_NOT, RID_TMP, RID_TMP);
1559 emit_ds1s2(as, RISCVI_AND, dest, left, RID_TMP);
1560 } else {
1561 emit_ds1s2(as, RISCVI_AND, RID_TMP, left, RID_TMP);
1562 emit_ds(as, RISCVI_NOT, RID_TMP, RID_TMP);
1563 emit_ds1s2(as, RISCVI_AND, dest, right, RID_TMP);
1564 }
1565 emit_dsi(as, RISCVI_ADDI, RID_TMP, RID_TMP, -1);
1566 }
1567 emit_ds1s2(as, RISCVI_SLT, RID_TMP,
1568 ismax ? left : right, ismax ? right : left);
1569 }
1570 }
1571 }
1572
1573 #define asm_min(as, ir) asm_min_max(as, ir, 0)
1574 #define asm_max(as, ir) asm_min_max(as, ir, 1)
1575
1576 /* -- Comparisons --------------------------------------------------------- */
1577
1578 /* FP comparisons. */
1579 static void asm_fpcomp(ASMState *as, IRIns *ir)
1580 {
1581 IROp op = ir->o;
1582 Reg right, left = ra_alloc2(as, ir, RSET_FPR);
1583 right = (left >> 8); left &= 255;
1584 asm_guard(as, (op < IR_EQ ? (op&4) : (op&1))
1585 ? RISCVI_BNE : RISCVI_BEQ, RID_TMP, RID_ZERO);
1586 switch (op) {
1587 case IR_LT: case IR_UGE:
1588 emit_ds1s2(as, RISCVI_FLT_D, RID_TMP, left, right);
1589 break;
1590 case IR_LE: case IR_UGT: case IR_ABC:
1591 emit_ds1s2(as, RISCVI_FLE_D, RID_TMP, left, right);
1592 break;
1593 case IR_GT: case IR_ULE:
1594 emit_ds1s2(as, RISCVI_FLT_D, RID_TMP, right, left);
1595 break;
1596 case IR_GE: case IR_ULT:
1597 emit_ds1s2(as, RISCVI_FLE_D, RID_TMP, right, left);
1598 break;
1599 case IR_EQ: case IR_NE:
1600 emit_ds1s2(as, RISCVI_FEQ_D, RID_TMP, left, right);
1601 break;
1602 default:
1603 break;
1604 }
1605 }
1606
1607 /* Integer comparisons. */
1608 static void asm_intcomp(ASMState *as, IRIns *ir)
1609 {
1610 /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */
1611 /* 00 01 10 11 100 101 110 111 */
1612 IROp op = ir->o;
1613 Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR);
1614 if (op == IR_ABC) op = IR_UGT;
1615 if ((op&4) == 0 && irref_isk(ir->op2) && get_kval(as, ir->op2) == 0) {
1616 switch (op) {
1617 case IR_LT: asm_guard(as, RISCVI_BGE, left, RID_ZERO); break;
1618 case IR_GE: asm_guard(as, RISCVI_BLT, left, RID_ZERO); break;
1619 case IR_LE: asm_guard(as, RISCVI_BLT, RID_ZERO, left); break;
1620 case IR_GT: asm_guard(as, RISCVI_BGE, RID_ZERO, left); break;
1621 default: break;
1622 }
1623 return;
1624 }
1625 if (irref_isk(ir->op2)) {
1626 intptr_t k = get_kval(as, ir->op2);
1627 if ((op&2)) k++;
1628 if (checki12(k)) {
1629 asm_guard(as, (op&1) ? RISCVI_BNE : RISCVI_BEQ, RID_TMP, RID_ZERO);
1630 emit_dsi(as, (op&4) ? RISCVI_SLTIU : RISCVI_SLTI, RID_TMP, left, k);
1631 return;
1632 }
1633 }
1634 right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left));
1635 asm_guard(as, ((op&4) ? RISCVI_BGEU : RISCVI_BGE) ^ RISCVF_FUNCT3((op^(op>>1))&1),
1636 (op&2) ? right : left, (op&2) ? left : right);
1637 }
1638
1639 static void asm_comp(ASMState *as, IRIns *ir)
1640 {
1641 if (irt_isnum(ir->t))
1642 asm_fpcomp(as, ir);
1643 else
1644 asm_intcomp(as, ir);
1645 }
1646
1647 static void asm_equal(ASMState *as, IRIns *ir)
1648 {
1649 if (irt_isnum(ir->t)) {
1650 asm_fpcomp(as, ir);
1651 } else {
1652 Reg right, left = ra_alloc2(as, ir, RSET_GPR);
1653 right = (left >> 8); left &= 255;
1654 asm_guard(as, (ir->o & 1) ? RISCVI_BEQ : RISCVI_BNE, left, right);
1655 }
1656 }
1657
1658 /* -- Split register ops -------------------------------------------------- */
1659
1660 /* Hiword op of a split 64 bit op. Previous op must be the loword op. */
1661 static void asm_hiop(ASMState *as, IRIns *ir)
1662 {
1663 /* HIOP is marked as a store because it needs its own DCE logic. */
1664 int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */
1665 if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1;
1666 if (!usehi) return; /* Skip unused hiword op for all remaining ops. */
1667 switch ((ir-1)->o) {
1668 case IR_CALLN:
1669 case IR_CALLL:
1670 case IR_CALLS:
1671 case IR_CALLXS:
1672 if (!uselo)
1673 ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */
1674 break;
1675 default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break;
1676 }
1677 }
1678
1679 /* -- Profiling ----------------------------------------------------------- */
1680
1681 static void asm_prof(ASMState *as, IRIns *ir)
1682 {
1683 UNUSED(ir);
1684 asm_guard(as, RISCVI_BNE, RID_TMP, RID_ZERO);
1685 emit_dsi(as, RISCVI_ANDI, RID_TMP, RID_TMP, HOOK_PROFILE);
1686 emit_lsglptr(as, RISCVI_LBU, RID_TMP,
1687 (int32_t)offsetof(global_State, hookmask));
1688 }
1689
1690 /* -- Stack handling ------------------------------------------------------ */
1691
1692 /* Check Lua stack size for overflow. Use exit handler as fallback. */
1693 static void asm_stack_check(ASMState *as, BCReg topslot,
1694 IRIns *irp, RegSet allow, ExitNo exitno)
1695 {
1696 /* Try to get an unused temp register, otherwise spill/restore RID_RET*. */
1697 Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE;
1698 ExitNo oldsnap = as->snapno;
1699 rset_clear(allow, pbase);
1700 as->snapno = exitno;
1701 asm_guard(as, RISCVI_BNE, RID_TMP, RID_ZERO);
1702 as->snapno = oldsnap;
1703 if (allow) {
1704 tmp = rset_pickbot(allow);
1705 ra_modified(as, tmp);
1706 } else { // allow == RSET_EMPTY
1707 tmp = RID_RET;
1708 emit_lso(as, RISCVI_LD, tmp, RID_SP, 0); /* Restore tmp1 register. */
1709 }
1710 emit_dsi(as, RISCVI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot));
1711 emit_ds1s2(as, RISCVI_SUB, RID_TMP, tmp, pbase);
1712 emit_lso(as, RISCVI_LD, tmp, tmp, offsetof(lua_State, maxstack));
1713 if (pbase == RID_TMP)
1714 emit_getgl(as, RID_TMP, jit_base);
1715 emit_getgl(as, tmp, cur_L);
1716 if (allow == RSET_EMPTY) /* Spill temp register. */
1717 emit_lso(as, RISCVI_SD, tmp, RID_SP, 0);
1718 }
1719
1720 /* Restore Lua stack from on-trace state. */
1721 static void asm_stack_restore(ASMState *as, SnapShot *snap)
1722 {
1723 SnapEntry *map = &as->T->snapmap[snap->mapofs];
1724 #ifdef LUA_USE_ASSERT
1725 SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1-LJ_FR2];
1726 #endif
1727 MSize n, nent = snap->nent;
1728 /* Store the value of all modified slots to the Lua stack. */
1729 for (n = 0; n < nent; n++) {
1730 SnapEntry sn = map[n];
1731 BCReg s = snap_slot(sn);
1732 int32_t ofs = 8*((int32_t)s-1-LJ_FR2);
1733 IRRef ref = snap_ref(sn);
1734 IRIns *ir = IR(ref);
1735 if ((sn & SNAP_NORESTORE))
1736 continue;
1737 if (irt_isnum(ir->t)) {
1738 Reg src = ra_alloc1(as, ref, RSET_FPR);
1739 emit_lso(as, RISCVI_FSD, src, RID_BASE, ofs);
1740 } else {
1741 if ((sn & SNAP_KEYINDEX)) {
1742 RegSet allow = rset_exclude(RSET_GPR, RID_BASE);
1743 int64_t kki = (int64_t)LJ_KEYINDEX << 32;
1744 if (irref_isk(ref)) {
1745 emit_lso(as, RISCVI_SD,
1746 ra_allock(as, kki | (int64_t)(uint32_t)ir->i, allow),
1747 RID_BASE, ofs);
1748 } else {
1749 Reg src = ra_alloc1(as, ref, allow);
1750 Reg rki = ra_allock(as, kki, rset_exclude(allow, src));
1751 emit_lso(as, RISCVI_SD, RID_TMP, RID_BASE, ofs);
1752 emit_ds1s2(as, RISCVI_ADD, RID_TMP, src, rki);
1753 }
1754 } else {
1755 asm_tvstore64(as, RID_BASE, ofs, ref);
1756 }
1757 }
1758 checkmclim(as);
1759 }
1760 lj_assertA(map + nent == flinks, "inconsistent frames in snapshot");
1761 }
1762
1763 /* -- GC handling --------------------------------------------------------- */
1764
1765 /* Marker to prevent patching the GC check exit. */
1766 #define RISCV_NOPATCH_GC_CHECK \
1767 (RISCVI_OR|RISCVF_D(RID_TMP)|RISCVF_S1(RID_TMP)|RISCVF_S2(RID_TMP))
1768
1769 /* Check GC threshold and do one or more GC steps. */
1770 static void asm_gc_check(ASMState *as)
1771 {
1772 const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit];
1773 IRRef args[2];
1774 MCLabel l_end;
1775 Reg tmp;
1776 ra_evictset(as, RSET_SCRATCH);
1777 l_end = emit_label(as);
1778 /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */
1779 asm_guard(as, RISCVI_BNE, RID_RET, RID_ZERO); /* Assumes asm_snap_prep() already done. */
1780 *--as->mcp = RISCV_NOPATCH_GC_CHECK;
1781 args[0] = ASMREF_TMP1; /* global_State *g */
1782 args[1] = ASMREF_TMP2; /* MSize steps */
1783 asm_gencall(as, ci, args);
1784 emit_ds(as, RISCVI_MV, ra_releasetmp(as, ASMREF_TMP1), RID_GL);
1785 tmp = ra_releasetmp(as, ASMREF_TMP2);
1786 emit_loadi(as, tmp, as->gcsteps);
1787 /* Jump around GC step if GC total < GC threshold. */
1788 emit_branch(as, RISCVI_BLTU, RID_TMP, tmp, l_end, 0);
1789 emit_getgl(as, tmp, gc.threshold);
1790 emit_getgl(as, RID_TMP, gc.total);
1791 as->gcsteps = 0;
1792 checkmclim(as);
1793 }
1794
1795 /* -- Loop handling ------------------------------------------------------- */
1796
1797 /* Fixup the loop branch. */
1798 static void asm_loop_fixup(ASMState *as)
1799 {
1800 MCode *p = as->mctop;
1801 MCode *target = as->mcp;
1802 ptrdiff_t delta;
1803 if (as->loopinv) { /* Inverted loop branch? */
1804 delta = (char *)target - (char *)(p - 2);
1805 /* asm_guard* already inverted the branch, and patched the final b. */
1806 lj_assertA(checki21(delta), "branch target out of range");
1807 p[-2] = (p[-2]&0x00000fff) | RISCVF_IMMJ(delta);
1808 } else {
1809 /* J */
1810 delta = (char *)target - (char *)(p - 1);
1811 p[-1] = RISCVI_JAL | RISCVF_IMMJ(delta);
1812 }
1813 }
1814
1815 /* Fixup the tail of the loop. */
1816 static void asm_loop_tail_fixup(ASMState *as)
1817 {
1818 UNUSED(as); /* Nothing to do(?) */
1819 }
1820
1821 /* -- Head of trace ------------------------------------------------------- */
1822
1823 /* Coalesce BASE register for a root trace. */
1824 static void asm_head_root_base(ASMState *as)
1825 {
1826 IRIns *ir = IR(REF_BASE);
1827 Reg r = ir->r;
1828 if (ra_hasreg(r)) {
1829 ra_free(as, r);
1830 if (rset_test(as->modset, r) || irt_ismarked(ir->t))
1831 ir->r = RID_INIT; /* No inheritance for modified BASE register. */
1832 if (r != RID_BASE)
1833 emit_mv(as, r, RID_BASE);
1834 }
1835 }
1836
1837 /* Coalesce BASE register for a side trace. */
1838 static Reg asm_head_side_base(ASMState *as, IRIns *irp)
1839 {
1840 IRIns *ir = IR(REF_BASE);
1841 Reg r = ir->r;
1842 if (ra_hasreg(r)) {
1843 ra_free(as, r);
1844 if (rset_test(as->modset, r) || irt_ismarked(ir->t))
1845 ir->r = RID_INIT; /* No inheritance for modified BASE register. */
1846 if (irp->r == r) {
1847 return r; /* Same BASE register already coalesced. */
1848 } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) {
1849 emit_mv(as, r, irp->r); /* Move from coalesced parent reg. */
1850 return irp->r;
1851 } else {
1852 emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */
1853 }
1854 }
1855 return RID_NONE;
1856 }
1857
1858 /* -- Tail of trace ------------------------------------------------------- */
1859
1860 /* Fixup the tail code. */
1861 static void asm_tail_fixup(ASMState *as, TraceNo lnk)
1862 {
1863 MCode *p = as->mctop;
1864 MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp;
1865 int32_t spadj = as->T->spadjust;
1866 if (spadj == 0) {
1867 p[-3] = RISCVI_NOP;
1868 // as->mctop = p-2;
1869 } else {
1870 /* Patch stack adjustment. */
1871 p[-3] = RISCVI_ADDI | RISCVF_D(RID_SP) | RISCVF_S1(RID_SP) | RISCVF_IMMI(spadj);
1872 }
1873 /* Patch exit jump. */
1874 ptrdiff_t delta = (char *)target - (char *)(p - 2);
1875 p[-2] = RISCVI_AUIPC | RISCVF_D(RID_TMP) | RISCVF_IMMU(RISCVF_HI(delta));
1876 p[-1] = RISCVI_JALR | RISCVF_S1(RID_TMP) | RISCVF_IMMI(RISCVF_LO(delta));
1877 }
1878
1879 /* Prepare tail of code. */
1880 static void asm_tail_prep(ASMState *as)
1881 {
1882 MCode *p = as->mctop - 2; /* Leave room for exitstub. */
1883 if (as->loopref) {
1884 as->invmcp = as->mcp = p;
1885 } else {
1886 as->mcp = p-1; /* Leave room for stack pointer adjustment. */
1887 as->invmcp = NULL;
1888 }
1889 p[0] = p[1] = RISCVI_NOP; /* Prevent load/store merging. */
1890 }
1891
1892 /* -- Trace setup --------------------------------------------------------- */
1893
1894 /* Ensure there are enough stack slots for call arguments. */
1895 static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci)
1896 {
1897 IRRef args[CCI_NARGS_MAX*2];
1898 uint32_t i, nargs = CCI_XNARGS(ci);
1899 int nslots = 0, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR;
1900 asm_collectargs(as, ir, ci, args);
1901 for (i = 0; i < nargs; i++) {
1902 if (args[i] && irt_isfp(IR(args[i])->t)) {
1903 if (nfpr > 0) {
1904 nfpr--; if(ci->flags & CCI_VARARG) ngpr--;
1905 } else if (!(ci->flags & CCI_VARARG) && ngpr > 0) ngpr--;
1906 else nslots += 2;
1907 } else {
1908 if (ngpr > 0) {
1909 ngpr--; if(ci->flags & CCI_VARARG) nfpr--;
1910 } else nslots += 2;
1911 }
1912 }
1913 if (nslots > as->evenspill) /* Leave room for args in stack slots. */
1914 as->evenspill = nslots;
1915 return REGSP_HINT(irt_isfp(ir->t) ? RID_FPRET : RID_RET);
1916 }
1917
1918 static void asm_setup_target(ASMState *as)
1919 {
1920 asm_sparejump_setup(as);
1921 asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0));
1922 }
1923
1924 /* -- Trace patching ------------------------------------------------------ */
1925
1926 /* Patch exit jumps of existing machine code to a new target. */
1927 void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
1928 {
1929 MCode *p = T->mcode;
1930 MCode *pe = (MCode *)((char *)p + T->szmcode);
1931 MCode *px = exitstub_trace_addr(T, exitno);
1932 MCode *cstart = NULL;
1933 MCode *mcarea = lj_mcode_patch(J, p, 0);
1934
1935 for (; p < pe; p++) {
1936 /* Look for exitstub branch, replace with branch to target. */
1937 ptrdiff_t odelta = (char *)px - (char *)(p+1),
1938 ndelta = (char *)target - (char *)(p+1);
1939 if ((((p[0] ^ RISCVF_IMMB(8)) & 0xfe000f80u) == 0 &&
1940 ((p[0] & 0x0000007fu) == 0x63u) &&
1941 ((p[1] ^ RISCVF_IMMJ(odelta)) & 0xfffff000u) == 0 &&
1942 ((p[1] & 0x0000007fu) == 0x6fu) && p[-1] != RISCV_NOPATCH_GC_CHECK) ||
1943 (((p[1] ^ RISCVF_IMMJ(odelta)) & 0xfffff000u) == 0 &&
1944 ((p[1] & 0x0000007fu) == 0x6fu) && p[0] != RISCV_NOPATCH_GC_CHECK)) {
1945 lj_assertJ(checki32(ndelta), "branch target out of range");
1946 /* Patch jump, if within range. */
1947 patchbranch:
1948 if (checki21(ndelta)) { /* Patch jump */
1949 p[1] = RISCVI_JAL | RISCVF_IMMJ(ndelta);
1950 if (!cstart) cstart = p + 1;
1951 } else { /* Branch out of range. Use spare jump slot in mcarea. */
1952 MCode *mcjump = asm_sparejump_use(mcarea, target);
1953 if (mcjump) {
1954 lj_mcode_sync(mcjump, mcjump+2);
1955 ndelta = (char *)mcjump - (char *)(p+1);
1956 if (checki21(ndelta)) {
1957 goto patchbranch;
1958 } else {
1959 lj_assertJ(0, "spare jump out of range: -Osizemcode too big");
1960 }
1961 }
1962 /* Ignore jump slot overflow. Child trace is simply not attached. */
1963 }
1964 } else if (p+2 == pe) {
1965 if (p[0] == RISCVI_NOP && p[1] == RISCVI_NOP) {
1966 ptrdiff_t delta = (char *)target - (char *)p;
1967 lj_assertJ(checki32(delta), "jump target out of range");
1968 p[0] = RISCVI_AUIPC | RISCVF_D(RID_TMP) | RISCVF_IMMU(RISCVF_HI(delta));
1969 p[1] = RISCVI_JALR | RISCVF_S1(RID_TMP) | RISCVF_IMMI(RISCVF_LO(delta));
1970 if (!cstart) cstart = p;
1971 }
1972 }
1973 }
1974 if (cstart) lj_mcode_sync(cstart, px+1);
1975 lj_mcode_patch(J, mcarea, 1);
1976 }