Debian Codegraph @

rpython/jit/backend/riscv/opassembler.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 from rpython.jit.backend.llsupport.assembler import BaseAssembler, GuardToken
4 from rpython.jit.backend.llsupport.descr import CallDescr, unpack_arraydescr
5 from rpython.jit.backend.llsupport.gcmap import allocate_gcmap
6 from rpython.jit.backend.riscv import registers as r
7 from rpython.jit.backend.riscv.arch import (
8 ABI_STACK_ALIGN, FLEN, INST_SIZE, JITFRAME_FIXED_SIZE, XLEN)
9 from rpython.jit.backend.riscv.callbuilder import RISCVCallBuilder
10 from rpython.jit.backend.riscv.codebuilder import (
11 AbstractRISCVBuilder, BRANCH_BUILDER, OverwritingBuilder)
12 from rpython.jit.backend.riscv.instruction_util import (
13 COND_BEQ, COND_BNE, COND_INVALID, check_imm_arg, check_simm21_arg)
14 from rpython.jit.backend.riscv.rounding_modes import DYN, RTZ
15 from rpython.jit.metainterp.history import (
16 AbstractFailDescr, ConstInt, FLOAT, INT, REF, TargetToken, VOID)
17 from rpython.jit.metainterp.resoperation import rop
18 from rpython.rlib.objectmodel import we_are_translated
19 from rpython.rlib.rarithmetic import r_uint
20 from rpython.rtyper import rclass
21 from rpython.rtyper.lltypesystem import lltype, rffi
22 from rpython.rtyper.lltypesystem.lloperation import llop
23
24
25 class OpAssembler(BaseAssembler):
26 def emit_op_int_add(self, op, arglocs):
27 l0, l1, res = arglocs
28 assert not l0.is_imm()
29 if l1.is_imm():
30 self.mc.ADDI(res.value, l0.value, l1.value)
31 else:
32 self.mc.ADD(res.value, l0.value, l1.value)
33
34 emit_op_nursery_ptr_increment = emit_op_int_add
35
36 def emit_op_int_sub(self, op, arglocs):
37 l0, l1, res = arglocs
38 assert not l0.is_imm()
39 if l1.is_imm():
40 self.mc.ADDI(res.value, l0.value, -l1.value)
41 else:
42 self.mc.SUB(res.value, l0.value, l1.value)
43
44 def emit_op_int_mul(self, op, arglocs):
45 l0, l1, res = arglocs
46 self.mc.MUL(res.value, l0.value, l1.value)
47
48 def emit_op_uint_mul_high(self, op, arglocs):
49 l0, l1, res = arglocs
50 self.mc.MULHU(res.value, l0.value, l1.value)
51
52 def emit_op_int_and(self, op, arglocs):
53 l0, l1, res = arglocs
54 assert not l0.is_imm()
55 if l1.is_imm():
56 self.mc.ANDI(res.value, l0.value, l1.value)
57 else:
58 self.mc.AND(res.value, l0.value, l1.value)
59
60 def emit_op_int_or(self, op, arglocs):
61 l0, l1, res = arglocs
62 assert not l0.is_imm()
63 if l1.is_imm():
64 self.mc.ORI(res.value, l0.value, l1.value)
65 else:
66 self.mc.OR(res.value, l0.value, l1.value)
67
68 def emit_op_int_xor(self, op, arglocs):
69 l0, l1, res = arglocs
70 assert not l0.is_imm()
71 if l1.is_imm():
72 self.mc.XORI(res.value, l0.value, l1.value)
73 else:
74 self.mc.XOR(res.value, l0.value, l1.value)
75
76 def emit_op_int_lshift(self, op, arglocs):
77 l0, l1, res = arglocs
78 assert not l0.is_imm()
79 if l1.is_imm():
80 self.mc.SLLI(res.value, l0.value, l1.value)
81 else:
82 self.mc.SLL(res.value, l0.value, l1.value)
83
84 def emit_op_int_rshift(self, op, arglocs):
85 l0, l1, res = arglocs
86 assert not l0.is_imm()
87 if l1.is_imm():
88 self.mc.SRAI(res.value, l0.value, l1.value)
89 else:
90 self.mc.SRA(res.value, l0.value, l1.value)
91
92 def emit_op_uint_rshift(self, op, arglocs):
93 l0, l1, res = arglocs
94 assert not l0.is_imm()
95 if l1.is_imm():
96 self.mc.SRLI(res.value, l0.value, l1.value)
97 else:
98 self.mc.SRL(res.value, l0.value, l1.value)
99
100 def _emit_op_int_lt(self, op, arglocs):
101 l0, l1, res = arglocs
102 assert not l0.is_imm()
103 if l1.is_imm():
104 self.mc.SLTI(res.value, l0.value, l1.value)
105 else:
106 self.mc.SLT(res.value, l0.value, l1.value)
107
108 emit_op_int_lt = _emit_op_int_lt
109 emit_op_int_gt = _emit_op_int_lt
110
111 emit_comp_op_int_lt = emit_op_int_lt
112 emit_comp_op_int_gt = emit_op_int_gt
113
114 def _emit_op_int_le(self, op, arglocs):
115 l0, l1, res = arglocs
116 if l1.is_imm():
117 self.mc.SLTI(res.value, l0.value, l1.value + 1)
118 else:
119 if l0.is_imm():
120 self.mc.SLTI(res.value, l1.value, l0.value)
121 else:
122 self.mc.SLT(res.value, l1.value, l0.value)
123 self.mc.XORI(res.value, res.value, 1)
124
125 emit_op_int_le = _emit_op_int_le
126 emit_op_int_ge = _emit_op_int_le
127
128 emit_comp_op_int_le = emit_op_int_le
129 emit_comp_op_int_ge = emit_op_int_ge
130
131 def _emit_op_uint_lt(self, op, arglocs):
132 l0, l1, res = arglocs
133 assert not l0.is_imm()
134 if l1.is_imm():
135 self.mc.SLTIU(res.value, l0.value, l1.value)
136 else:
137 self.mc.SLTU(res.value, l0.value, l1.value)
138
139 emit_op_uint_lt = _emit_op_uint_lt
140 emit_op_uint_gt = _emit_op_uint_lt
141
142 emit_comp_op_uint_lt = emit_op_uint_lt
143 emit_comp_op_uint_gt = emit_op_uint_gt
144
145 def _emit_op_uint_le(self, op, arglocs):
146 l0, l1, res = arglocs
147 if l1.is_imm():
148 if l1.value == -1:
149 # uint_le(x, -1) is always true.
150 self.mc.load_int_imm(res.value, 1)
151 else:
152 self.mc.SLTIU(res.value, l0.value, l1.value + 1)
153 else:
154 if l0.is_imm():
155 self.mc.SLTIU(res.value, l1.value, l0.value)
156 else:
157 self.mc.SLTU(res.value, l1.value, l0.value)
158 self.mc.XORI(res.value, res.value, 1)
159
160 emit_op_uint_le = _emit_op_uint_le
161 emit_op_uint_ge = _emit_op_uint_le
162
163 emit_comp_op_uint_le = emit_op_uint_le
164 emit_comp_op_uint_ge = emit_op_uint_ge
165
166 def emit_op_int_eq(self, op, arglocs):
167 l0, l1, res = arglocs
168 assert not l0.is_imm()
169 if l1.is_imm():
170 if l1.value == 0:
171 self.mc.SEQZ(res.value, l0.value)
172 else:
173 self.mc.XORI(res.value, l0.value, l1.value)
174 self.mc.SEQZ(res.value, res.value)
175 else:
176 self.mc.XOR(res.value, l0.value, l1.value)
177 self.mc.SEQZ(res.value, res.value)
178
179 emit_op_ptr_eq = emit_op_instance_ptr_eq = emit_op_int_eq
180
181 emit_comp_op_int_eq = emit_op_int_eq
182 emit_comp_op_ptr_eq = emit_op_int_eq
183
184 def emit_op_int_ne(self, op, arglocs):
185 l0, l1, res = arglocs
186 assert not l0.is_imm()
187 if l1.is_imm():
188 if l1.value == 0:
189 self.mc.SNEZ(res.value, l0.value)
190 else:
191 self.mc.XORI(res.value, l0.value, l1.value)
192 self.mc.SNEZ(res.value, res.value)
193 else:
194 self.mc.XOR(res.value, l0.value, l1.value)
195 self.mc.SNEZ(res.value, res.value)
196
197 emit_op_ptr_ne = emit_op_instance_ptr_ne = emit_op_int_ne
198
199 emit_comp_op_int_ne = emit_op_int_ne
200 emit_comp_op_ptr_ne = emit_op_int_ne
201
202 def emit_op_int_is_true(self, op, arglocs):
203 l0, res = arglocs
204 self.mc.SNEZ(res.value, l0.value)
205
206 emit_comp_op_int_is_true = emit_op_int_is_true
207
208 def emit_op_int_neg(self, op, arglocs):
209 l0, res = arglocs
210 self.mc.NEG(res.value, l0.value)
211
212 def emit_op_int_invert(self, op, arglocs):
213 l0, res = arglocs
214 self.mc.NOT(res.value, l0.value)
215
216 def emit_op_int_is_zero(self, op, arglocs):
217 l0, res = arglocs
218 self.mc.SEQZ(res.value, l0.value)
219
220 emit_comp_op_int_is_zero = emit_op_int_is_zero
221
222 def emit_op_int_force_ge_zero(self, op, arglocs):
223 l0, res = arglocs
224 self.mc.MV(res.value, l0.value)
225 self.mc.BGEZ(l0.value, 8) # Skip next instruction if l0 >= 0
226 self.mc.MV(res.value, r.zero.value)
227
228 def emit_op_int_signext(self, op, arglocs):
229 l0, l1, res = arglocs
230
231 assert l1.is_imm()
232 num_bytes = l1.value
233
234 if l0.is_stack():
235 offset = l0.value
236 if num_bytes == 1:
237 self.mc.LB(res.value, r.jfp.value, offset)
238 elif num_bytes == 2:
239 self.mc.LH(res.value, r.jfp.value, offset)
240 elif num_bytes == 4:
241 self.mc.LW(res.value, r.jfp.value, offset)
242 else:
243 assert 0, 'unexpected int_signext number of bytes'
244 else:
245 # Register-to-register sign extension
246 assert l0.is_core_reg()
247
248 assert num_bytes == 1 or num_bytes == 2 or num_bytes == 4
249 shift_amount = (XLEN - num_bytes) * 8
250
251 self.mc.SLLI(res.value, l0.value, shift_amount)
252 self.mc.SRAI(res.value, res.value, shift_amount)
253
254 def emit_op_float_add(self, op, arglocs):
255 l0, l1, res = arglocs
256 self.mc.FADD_D(res.value, l0.value, l1.value)
257
258 def emit_op_float_sub(self, op, arglocs):
259 l0, l1, res = arglocs
260 self.mc.FSUB_D(res.value, l0.value, l1.value)
261
262 def emit_op_float_mul(self, op, arglocs):
263 l0, l1, res = arglocs
264 self.mc.FMUL_D(res.value, l0.value, l1.value)
265
266 def emit_op_float_truediv(self, op, arglocs):
267 l0, l1, res = arglocs
268 self.mc.FDIV_D(res.value, l0.value, l1.value)
269
270 def _emit_op_float_lt(self, op, arglocs):
271 l0, l1, res = arglocs
272 self.mc.FLT_D(res.value, l0.value, l1.value)
273
274 emit_op_float_lt = _emit_op_float_lt
275 emit_op_float_gt = _emit_op_float_lt
276
277 emit_comp_op_float_lt = emit_op_float_lt
278 emit_comp_op_float_gt = emit_op_float_gt
279
280 def _emit_op_float_le(self, op, arglocs):
281 l0, l1, res = arglocs
282 self.mc.FLE_D(res.value, l0.value, l1.value)
283
284 emit_op_float_le = _emit_op_float_le
285 emit_op_float_ge = _emit_op_float_le
286
287 emit_comp_op_float_le = emit_op_float_le
288 emit_comp_op_float_ge = emit_op_float_ge
289
290 def emit_op_float_eq(self, op, arglocs):
291 l0, l1, res = arglocs
292 self.mc.FEQ_D(res.value, l0.value, l1.value)
293
294 emit_comp_op_float_eq = emit_op_float_eq
295
296 def emit_op_float_ne(self, op, arglocs):
297 l0, l1, res = arglocs
298 self.mc.FEQ_D(res.value, l0.value, l1.value)
299 self.mc.XORI(res.value, res.value, 1)
300
301 emit_comp_op_float_ne = emit_op_float_ne
302
303 def emit_op_float_neg(self, op, arglocs):
304 l0, res = arglocs
305 self.mc.FNEG_D(res.value, l0.value)
306
307 def emit_op_float_abs(self, op, arglocs):
308 l0, res = arglocs
309 self.mc.FABS_D(res.value, l0.value)
310
311 def emit_op_cast_float_to_int(self, op, arglocs):
312 l0, res = arglocs
313 self.mc.FCVT_L_D(res.value, l0.value, RTZ.value)
314
315 def emit_op_cast_int_to_float(self, op, arglocs):
316 l0, res = arglocs
317 self.mc.FCVT_D_L(res.value, l0.value, DYN.value)
318
319 def emit_op_convert_float_bytes_to_longlong(self, op, arglocs):
320 l0, res = arglocs
321 self.mc.FMV_X_D(res.value, l0.value)
322
323 def emit_op_convert_longlong_bytes_to_float(self, op, arglocs):
324 l0, res = arglocs
325 self.mc.FMV_D_X(res.value, l0.value)
326
327 def emit_op_gc_store(self, op, arglocs):
328 value_loc, base_loc, ofs_loc, size_loc = arglocs
329 self._store_to_mem(value_loc, base_loc, ofs_loc, size_loc.value)
330
331 def _emit_op_gc_load(self, op, arglocs):
332 base_loc, ofs_loc, res_loc, nsize_loc = arglocs
333 nsize = nsize_loc.value
334 signed = (nsize < 0)
335 self._load_from_mem(res_loc, base_loc, ofs_loc, abs(nsize), signed)
336
337 emit_op_gc_load_i = _emit_op_gc_load
338 emit_op_gc_load_r = _emit_op_gc_load
339 emit_op_gc_load_f = _emit_op_gc_load
340
341 def _emit_gc_indexed_full_offset(self, index_loc, ofs_loc):
342 # Compute the full offset.
343 #
344 # Note: result = (index * scale) + ofs, where scale == 1
345
346 scratch_reg = r.x31
347 assert index_loc.is_core_reg()
348
349 if ofs_loc.is_imm():
350 if ofs_loc.value == 0:
351 return index_loc
352 if check_imm_arg(ofs_loc.value):
353 self.mc.ADDI(scratch_reg.value, index_loc.value, ofs_loc.value)
354 else:
355 assert index_loc is not scratch_reg
356 self.mc.load_int_imm(scratch_reg.value, ofs_loc.value)
357 self.mc.ADD(scratch_reg.value, scratch_reg.value,
358 index_loc.value)
359 return scratch_reg
360
361 self.mc.ADD(scratch_reg.value, index_loc.value, ofs_loc.value)
362 return scratch_reg
363
364 def emit_op_gc_store_indexed(self, op, arglocs):
365 value_loc, base_loc, index_loc, ofs_loc, size_loc = arglocs
366
367 full_ofs_loc = self._emit_gc_indexed_full_offset(index_loc, ofs_loc)
368 self._store_to_mem(value_loc, base_loc, full_ofs_loc, size_loc.value)
369
370 def _emit_op_gc_load_indexed(self, op, arglocs):
371 base_loc, index_loc, ofs_loc, res_loc, nsize_loc = arglocs
372
373 nsize = nsize_loc.value
374 signed = (nsize < 0)
375
376 full_ofs_loc = self._emit_gc_indexed_full_offset(index_loc, ofs_loc)
377 self._load_from_mem(res_loc, base_loc, full_ofs_loc, abs(nsize),
378 signed)
379
380 emit_op_gc_load_indexed_i = _emit_op_gc_load_indexed
381 emit_op_gc_load_indexed_r = _emit_op_gc_load_indexed
382 emit_op_gc_load_indexed_f = _emit_op_gc_load_indexed
383
384 def _normalize_base_offset(self, base_loc, ofs_loc, scratch_reg):
385 # Normalize the calculation of `base + offset`.
386
387 if ofs_loc.is_core_reg():
388 self.mc.ADD(scratch_reg.value, base_loc.value, ofs_loc.value)
389 return (scratch_reg, 0)
390
391 assert ofs_loc.is_imm()
392 if check_imm_arg(ofs_loc.value):
393 return (base_loc, ofs_loc.value)
394 else:
395 assert base_loc is not scratch_reg
396 self.mc.load_int_imm(scratch_reg.value, ofs_loc.value)
397 self.mc.ADD(scratch_reg.value, scratch_reg.value,
398 base_loc.value)
399 return (scratch_reg, 0)
400
401 def _store_to_mem(self, value_loc, base_loc, ofs_loc, type_size):
402 # Store a value of size `type_size` at the address
403 # `base_ofs + ofs_loc`.
404 #
405 # Note: `ofs_loc` can be `scratch_reg`. Use with caution.
406
407 assert XLEN == 8 and FLEN == 8, 'implementation below assumes 64-bit'
408 assert base_loc.is_core_reg()
409
410 scratch_reg = r.x31
411 base_loc, ofs_int = self._normalize_base_offset(base_loc, ofs_loc,
412 scratch_reg)
413
414 if type_size == 8:
415 # 64-bit
416 if value_loc.is_float():
417 # 64-bit float
418 self.mc.FSD(value_loc.value, base_loc.value, ofs_int)
419 return
420 # 64-bit int
421 self.mc.SD(value_loc.value, base_loc.value, ofs_int)
422 return
423
424 if type_size == 4:
425 # 32-bit int
426 self.mc.SW(value_loc.value, base_loc.value, ofs_int)
427 return
428
429 if type_size == 2:
430 # 16-bit int
431 self.mc.SH(value_loc.value, base_loc.value, ofs_int)
432 return
433
434 assert type_size == 1
435 self.mc.SB(value_loc.value, base_loc.value, ofs_int)
436
437 def _load_from_mem(self, res_loc, base_loc, ofs_loc, type_size, signed):
438 # Load a value of `type_size` bytes, from the memory location
439 # `base_loc + ofs_loc`.
440
441 assert XLEN == 8 and FLEN == 8, 'implementation below assumes 64-bit'
442 assert base_loc.is_core_reg()
443
444 scratch_reg = r.x31
445 base_loc, ofs_int = self._normalize_base_offset(base_loc, ofs_loc,
446 scratch_reg)
447
448 if type_size == 8:
449 # 64-bit
450 if res_loc.is_float():
451 # 64-bit float
452 self.mc.FLD(res_loc.value, base_loc.value, ofs_int)
453 return
454 # 64-bit int
455 self.mc.LD(res_loc.value, base_loc.value, ofs_int)
456 return
457
458 if type_size == 4:
459 # 32-bit int
460 if signed:
461 self.mc.LW(res_loc.value, base_loc.value, ofs_int)
462 else:
463 self.mc.LWU(res_loc.value, base_loc.value, ofs_int)
464 return
465
466 if type_size == 2:
467 # 16-bit int
468 if signed:
469 self.mc.LH(res_loc.value, base_loc.value, ofs_int)
470 else:
471 self.mc.LHU(res_loc.value, base_loc.value, ofs_int)
472 return
473
474 assert type_size == 1
475 # 8-bit int
476 if signed:
477 self.mc.LB(res_loc.value, base_loc.value, ofs_int)
478 else:
479 self.mc.LBU(res_loc.value, base_loc.value, ofs_int)
480
481 def _build_guard_token(self, op, frame_depth, arglocs, offset):
482 descr = op.getdescr()
483 assert isinstance(descr, AbstractFailDescr)
484
485 gcmap = allocate_gcmap(self, frame_depth, JITFRAME_FIXED_SIZE)
486 faildescrindex = self.get_gcref_from_faildescr(descr)
487 return GuardToken(self.cpu, gcmap, descr,
488 failargs=op.getfailargs(), fail_locs=arglocs,
489 guard_opnum=op.getopnum(), frame_depth=frame_depth,
490 faildescrindex=faildescrindex)
491
492 def _emit_pending_guard(self, op, arglocs, is_guard_not_invalidated=False):
493 pos = self.mc.get_relative_pos()
494 guardtok = self._build_guard_token(op, arglocs[0].value, arglocs[1:],
495 pos)
496 guardtok.offset = pos
497 self.pending_guards.append(guardtok)
498 assert guardtok.guard_not_invalidated() == is_guard_not_invalidated
499 if is_guard_not_invalidated:
500 # For `GUARD_NOT_INVALIDATED`, just emit an no-op. It will be
501 # patched by `invalidate_loop` (in `runner.py`) when invalidation
502 # is needed.
503 self.mc.NOP()
504 else:
505 # Emit an `EBREAK` here and `process_pending_guards` will patch it
506 # with a branch to a recovery stub.
507 self.mc.EBREAK()
508
509 def emit_op_guard_true(self, op, arglocs):
510 l0 = arglocs[0]
511 self.mc.BNEZ(l0.value, 8)
512 self._emit_pending_guard(op, arglocs[1:])
513
514 emit_op_guard_nonnull = emit_op_guard_true
515
516 def emit_op_guard_false(self, op, arglocs):
517 l0 = arglocs[0]
518 self.mc.BEQZ(l0.value, 8)
519 self._emit_pending_guard(op, arglocs[1:])
520
521 emit_op_guard_isnull = emit_op_guard_false
522
523 def emit_op_guard_value(self, op, arglocs):
524 l0 = arglocs[0]
525 l1 = arglocs[1]
526 if l0.is_fp_reg():
527 self.mc.FEQ_D(r.x31.value, l0.value, l1.value)
528 self.mc.BNEZ(r.x31.value, 8)
529 else:
530 self.mc.BEQ(l0.value, l1.value, 8)
531 self._emit_pending_guard(op, arglocs[2:])
532
533 def _emit_guard_op_guard_bool_op(self, op, guard_op, arglocs,
534 guard_branch_inst):
535 l0 = arglocs[0]
536 l1 = arglocs[1]
537 BRANCH_BUILDER[guard_branch_inst](self.mc, l0.value, l1.value, 8)
538 self._emit_pending_guard(guard_op, arglocs[2:])
539
540 emit_guard_op_guard_true = _emit_guard_op_guard_bool_op
541 emit_guard_op_guard_false = _emit_guard_op_guard_bool_op
542
543 def _emit_guard_op_guard_overflow_op(self, op, guard_op, arglocs,
544 guard_branch_inst):
545 l0 = arglocs[0]
546 l1 = arglocs[1]
547 res = arglocs[2]
548 tmp0 = arglocs[3]
549 tmp1 = arglocs[4]
550
551 opnum = op.getopnum()
552 if opnum == rop.INT_ADD_OVF:
553 self.mc.ADD(res.value, l0.value, l1.value)
554 self.mc.SLT(tmp0.value, res.value, l0.value)
555 self.mc.SLTI(tmp1.value, l1.value, 0)
556 elif opnum == rop.INT_SUB_OVF:
557 self.mc.SUB(res.value, l0.value, l1.value)
558 self.mc.SLT(tmp0.value, res.value, l0.value)
559 self.mc.SGTZ(tmp1.value, l1.value)
560 elif opnum == rop.INT_MUL_OVF:
561 self.mc.MUL(res.value, l0.value, l1.value)
562 self.mc.MULH(tmp0.value, l0.value, l1.value)
563 self.mc.SRAI(tmp1.value, res.value, XLEN * 8 - 1)
564 else:
565 assert 0, 'unexpected overflow op'
566
567 BRANCH_BUILDER[guard_branch_inst](self.mc, tmp0.value, tmp1.value, 8)
568 self._emit_pending_guard(guard_op, arglocs[5:])
569
570 emit_guard_op_guard_overflow = _emit_guard_op_guard_overflow_op
571 emit_guard_op_guard_no_overflow = _emit_guard_op_guard_overflow_op
572
573 def _emit_load_typeid_from_obj(self, dest_loc, obj_loc):
574 # Note that the typeid half-word is at offset 0 on a little-endian
575 # machine; it would be at offset 2 or 4 on a big-endian machine.
576 if XLEN == 8:
577 self.mc.LWU(dest_loc.value, obj_loc.value, 0)
578 else:
579 self.mc.LHU(dest_loc.value, obj_loc.value, 0)
580
581 def _emit_cmp_guard_gc_type(self, obj_loc, expected_typeid,
582 success_branch_offset):
583 assert self.cpu.supports_guard_gc_type
584
585 scratch_reg = r.x31
586 scratch2_reg = r.ra
587 self._emit_load_typeid_from_obj(scratch_reg, obj_loc)
588 self.mc.load_int_imm(scratch2_reg.value, expected_typeid)
589 self.mc.BEQ(scratch_reg.value, scratch2_reg.value,
590 success_branch_offset)
591
592 def emit_op_guard_class(self, op, arglocs):
593 # Implements guard_class(obj, cls):
594 #
595 # if type(obj) != cls:
596 # goto guard_fail
597
598 obj_loc, cls_loc = arglocs[:2]
599 failargs = arglocs[2:]
600
601 scratch_reg = r.x31
602 scratch2_reg = r.ra
603 offset = self.cpu.vtable_offset
604 if offset is not None:
605 self.mc.load_int_from_base_plus_offset(scratch_reg.value,
606 obj_loc.value, offset)
607 self.mc.load_int_imm(scratch2_reg.value, cls_loc.value)
608 self.mc.BEQ(scratch_reg.value, scratch2_reg.value, 8)
609 else:
610 expected_typeid = (self.cpu.gc_ll_descr
611 .get_typeid_from_classptr_if_gcremovetypeptr(
612 cls_loc.value))
613 self._emit_cmp_guard_gc_type(obj_loc, expected_typeid,
614 success_branch_offset=8)
615 self._emit_pending_guard(op, failargs)
616
617 def emit_op_guard_nonnull_class(self, op, arglocs):
618 # Implements guard_nonnull_class(obj, cls):
619 #
620 # if !obj:
621 # goto guard_fail
622 # if type(obj) != cls:
623 # goto guard_fail
624
625 obj_loc, cls_loc = arglocs[:2]
626 failargs = arglocs[2:]
627
628 scratch_reg = r.x31
629 scratch2_reg = r.ra
630
631 # LABEL[guard_nonnull]:
632 # Patch Location: BEQZ obj, guard_fail
633 branch_inst_location = self.mc.get_relative_pos()
634 self.mc.EBREAK()
635
636 offset = self.cpu.vtable_offset
637 if offset is not None:
638 # Test `type(obj) == cls`
639 self.mc.load_int_from_base_plus_offset(scratch_reg.value,
640 obj_loc.value, offset)
641 self.mc.load_int_imm(scratch2_reg.value, cls_loc.value)
642 self.mc.BEQ(scratch_reg.value, scratch2_reg.value, 8)
643 else:
644 expected_typeid = (self.cpu.gc_ll_descr
645 .get_typeid_from_classptr_if_gcremovetypeptr(
646 cls_loc.value))
647 self._emit_cmp_guard_gc_type(obj_loc, expected_typeid,
648 success_branch_offset=8)
649
650 # LABEL[guard_fail]:
651 currpos = self.mc.get_relative_pos()
652 pmc = OverwritingBuilder(self.mc, branch_inst_location, INST_SIZE)
653 pmc.BEQZ(obj_loc.value, currpos - branch_inst_location)
654
655 self._emit_pending_guard(op, failargs)
656
657 def emit_op_guard_gc_type(self, op, arglocs):
658 obj_loc = arglocs[0]
659 expected_typeid_imm = arglocs[1]
660 failargs = arglocs[2:]
661
662 self._emit_cmp_guard_gc_type(obj_loc, expected_typeid_imm.value,
663 success_branch_offset=8)
664 self._emit_pending_guard(op, failargs)
665
666 def emit_op_guard_subclass(self, op, arglocs):
667 assert self.cpu.supports_guard_gc_type
668 obj_loc = arglocs[0]
669 check_against_class_loc = arglocs[1]
670 failargs = arglocs[2:]
671
672 scratch_reg = r.x31
673 scratch2_reg = r.ra
674
675 offset = self.cpu.vtable_offset
676 subclassrange_min_offset = self.cpu.subclassrange_min_offset
677 if offset is not None:
678 # Read this field to get the vtable pointer
679 self.mc.load_int_from_base_plus_offset(scratch_reg.value,
680 obj_loc.value, offset)
681 # Read the vtable's subclassrange_min field
682 self.mc.load_int_from_base_plus_offset(scratch_reg.value,
683 scratch_reg.value,
684 subclassrange_min_offset,
685 tmp=scratch2_reg)
686 else:
687 # Read the typeid
688 self._emit_load_typeid_from_obj(scratch_reg, obj_loc)
689 # Read the vtable's subclassrange_min field, as a single step with
690 # the correct offset.
691 base_type_info, shift_by, sizeof_ti = (
692 self.cpu.gc_ll_descr.get_translated_info_for_typeinfo())
693
694 self.mc.load_int_imm(scratch2_reg.value,
695 base_type_info + sizeof_ti +
696 subclassrange_min_offset)
697 if shift_by > 0:
698 self.mc.SLLI(scratch_reg.value, scratch_reg.value, shift_by)
699 self.mc.ADD(scratch_reg.value, scratch_reg.value,
700 scratch2_reg.value)
701 self.mc.load_int(scratch_reg.value, scratch_reg.value, 0)
702
703 # Get the two bounds to check against
704 vtable_ptr = check_against_class_loc.value
705 vtable_ptr = rffi.cast(rclass.CLASSTYPE, vtable_ptr)
706 check_min = vtable_ptr.subclassrange_min
707 check_max = vtable_ptr.subclassrange_max
708 assert check_max > check_min
709 check_diff = check_max - check_min - 1
710
711 # Check by doing the unsigned comparison (max - min - 1) >= (tmp - min)
712 self.mc.load_int_imm(scratch2_reg.value, check_min)
713 self.mc.SUB(scratch_reg.value, scratch_reg.value, scratch2_reg.value)
714 self.mc.load_int_imm(scratch2_reg.value, check_diff)
715 self.mc.BGEU(scratch2_reg.value, scratch_reg.value, 8)
716
717 self._emit_pending_guard(op, failargs)
718
719 def emit_op_guard_is_object(self, op, arglocs):
720 assert self.cpu.supports_guard_gc_type
721
722 obj_loc = arglocs[0]
723 failargs = arglocs[1:]
724
725 scratch_reg = r.x31
726 scratch2_reg = r.ra
727
728 # Read the typeid, fetch one byte of the field 'infobits' from the big
729 # typeinfo table, and check the flag 'T_IS_RPYTHON_INSTANCE'.
730 self._emit_load_typeid_from_obj(scratch_reg, obj_loc)
731
732 base_type_info, shift_by, sizeof_ti = (
733 self.cpu.gc_ll_descr.get_translated_info_for_typeinfo())
734 infobits_offset, IS_OBJECT_FLAG = (
735 self.cpu.gc_ll_descr.get_translated_info_for_guard_is_object())
736
737 self.mc.load_int_imm(scratch2_reg.value,
738 base_type_info + infobits_offset)
739 if shift_by > 0:
740 self.mc.SLLI(scratch_reg.value, scratch_reg.value, shift_by)
741 self.mc.ADD(scratch_reg.value, scratch_reg.value, scratch2_reg.value)
742 self.mc.LBU(scratch_reg.value, scratch_reg.value, 0)
743 self.mc.ANDI(scratch_reg.value, scratch_reg.value,
744 IS_OBJECT_FLAG & 0xff)
745 self.mc.BNEZ(scratch_reg.value, 8)
746 self._emit_pending_guard(op, failargs)
747
748 def emit_op_guard_not_invalidated(self, op, arglocs):
749 self._emit_pending_guard(op, arglocs, is_guard_not_invalidated=True)
750
751 def emit_op_guard_exception(self, op, arglocs):
752 expected_exc_tp_loc, res_exc_val_loc = arglocs[:2]
753 failargs = arglocs[2:]
754
755 # Load the type of the pending exception.
756 scratch_reg = r.x31
757 self.mc.load_int_imm(scratch_reg.value, self.cpu.pos_exception())
758 self.mc.load_int(scratch_reg.value, scratch_reg.value, 0)
759
760 # Compare the expected type and pending type.
761 self.mc.BEQ(scratch_reg.value, expected_exc_tp_loc.value, 8)
762 self._emit_pending_guard(op, failargs)
763
764 # Copy the exception value.
765 self._store_and_reset_exception(self.mc, res_exc_val_loc)
766
767 def emit_op_guard_no_exception(self, op, arglocs):
768 failargs = arglocs # All arglocs are for failargs
769
770 scratch_reg = r.x31
771 self.mc.load_int_imm(scratch_reg.value, self.cpu.pos_exception())
772 self.mc.load_int(scratch_reg.value, scratch_reg.value, 0)
773
774 self.mc.BEQZ(scratch_reg.value, 8)
775 self._emit_pending_guard(op, failargs)
776
777 # If the previous operation was a `COND_CALL`, overwrite its
778 # conditional jump to jump over this GUARD_NO_EXCEPTION as well.
779 if self._find_nearby_operation(-1).getopnum() == rop.COND_CALL:
780 cond_branch_addr, branch_inst, arg = self.previous_cond_call_branch
781 new_offset = self.mc.get_relative_pos() - cond_branch_addr
782 pmc = OverwritingBuilder(self.mc, cond_branch_addr, INST_SIZE)
783 BRANCH_BUILDER[branch_inst](pmc, arg.value, r.x0.value, new_offset)
784
785 def emit_op_save_exc_class(self, op, arglocs):
786 res = arglocs[0]
787 self.mc.load_int_imm(res.value, self.cpu.pos_exception())
788 self.mc.load_int(res.value, res.value, 0)
789
790 def emit_op_save_exception(self, op, arglocs):
791 self._store_and_reset_exception(self.mc, arglocs[0])
792
793 def emit_op_restore_exception(self, op, arglocs):
794 exc_tp_loc, exc_val_loc = arglocs
795 self._restore_exception(self.mc, exc_val_loc, exc_tp_loc)
796
797 def emit_op_check_memory_error(self, op, arglocs):
798 self.propagate_memoryerror_if_reg_is_null(arglocs[0])
799
800 def _emit_op_same_as(self, op, arglocs):
801 l0, res = arglocs
802 if l0 is not res:
803 self.regalloc_mov(l0, res)
804
805 emit_op_same_as_i = _emit_op_same_as
806 emit_op_same_as_r = _emit_op_same_as
807 emit_op_same_as_f = _emit_op_same_as
808 emit_op_cast_ptr_to_int = _emit_op_same_as
809 emit_op_cast_int_to_ptr = _emit_op_same_as
810
811 def math_sqrt(self, op, arglocs):
812 l1, res = arglocs
813 self.mc.FSQRT_D(res.value, l1.value)
814
815 def threadlocalref_get(self, op, arglocs):
816 res_loc = arglocs[0]
817
818 # Use `r.ra` as a scratch register.
819 #
820 # Note: Don't use `r.x31` here because `_load_from_mem` uses `r.x31` as
821 # a scratch register when it must add large offset immediate.
822 tls_reg = r.ra
823
824 # Load TLS address
825 self.mc.load_int(tls_reg.value, r.sp.value,
826 self.saved_threadlocal_addr)
827
828 # TLS field offset as an immediate
829 ofs_loc = self.imm(op.getarg(1).getint())
830
831 # TLS field type
832 calldescr = op.getdescr()
833 type_size = calldescr.get_result_size()
834 signed = calldescr.is_result_signed()
835
836 self._load_from_mem(res_loc, tls_reg, ofs_loc, type_size, signed)
837
838 def _emit_op_call(self, op, arglocs):
839 is_call_release_gil = rop.is_call_release_gil(op.getopnum())
840
841 # arglocs = [resloc, size, sign, funcloc, args...]
842 # -- or --
843 # arglocs = [resloc, size, sign, save_err_loc, funcloc, args...]
844
845 resloc = arglocs[0]
846 sizeloc = arglocs[1]
847 signloc = arglocs[2]
848 func_index = 3 + is_call_release_gil
849 funcloc = arglocs[func_index]
850
851 assert sizeloc.is_imm()
852 assert signloc.is_imm()
853
854 descr = op.getdescr()
855 assert isinstance(descr, CallDescr)
856
857 cb = RISCVCallBuilder(self, funcloc, arglocs[func_index + 1:], resloc,
858 descr.get_result_type(), sizeloc.value)
859 cb.callconv = descr.get_call_conv()
860 cb.argtypes = descr.get_arg_types()
861 cb.restype = descr.get_result_type()
862 cb.ressize = sizeloc.value
863 cb.ressign = signloc.value
864
865 if is_call_release_gil:
866 save_err_loc = arglocs[3]
867 assert save_err_loc.is_imm()
868 cb.emit_call_release_gil(save_err_loc.value)
869 else:
870 effectinfo = descr.get_extra_info()
871 if effectinfo is None or effectinfo.check_can_collect():
872 cb.emit()
873 else:
874 cb.emit_no_collect()
875
876 emit_op_call_i = _emit_op_call
877 emit_op_call_f = _emit_op_call
878 emit_op_call_r = _emit_op_call
879 emit_op_call_n = _emit_op_call
880
881 def _emit_op_cond_call(self, op, arglocs):
882 """Emit instructions for COND_CALL and COND_CALL_VALUE_I/R.
883
884 # cond_call(cond, func, *args)
885 cond = arglocs[0]
886 if cond != 0:
887 func(*args)
888
889 # res = cond_call_value(cond, func, *args)
890 # res = cond or func(*args)
891 cond, res = arglocs
892 if cond == 0:
893 res = func(*args)
894 """
895
896 if len(arglocs) == 2:
897 res_loc = arglocs[1] # cond_call_value
898 else:
899 res_loc = None # cond_call
900
901 # see x86.regalloc for why we skip res_loc in the gcmap
902 gcmap = self._regalloc.get_gcmap([res_loc])
903
904 # Save the conditional branch position (will be overwritten after we
905 # generate the instructions in the middle).
906 cond_branch_addr = self.mc.get_relative_pos()
907 self.mc.EBREAK()
908
909 self.push_gcmap(self.mc, gcmap)
910
911 # Load callee function address.
912 callee_func_addr_reg = r.x30
913 callee_func_addr = rffi.cast(lltype.Signed, op.getarg(1).getint())
914 self.mc.load_int_imm(callee_func_addr_reg.value, callee_func_addr)
915
916 # Check whether there are alive registers that we must reload after
917 # cond_call.
918 callee_only = False
919 floats = False
920 if self._regalloc is not None:
921 for reg in self._regalloc.rm.reg_bindings.values():
922 if reg not in self._regalloc.rm.save_around_call_regs:
923 break
924 else:
925 callee_only = True
926 if len(self._regalloc.fprm.reg_bindings):
927 floats = True
928
929 # Jump to cond_call trampoline.
930 trampoline_addr = self.cond_call_slowpath[floats * 2 + callee_only]
931 assert trampoline_addr
932 self.mc.load_int_imm(r.ra.value, trampoline_addr)
933 self.mc.JALR(r.ra.value, r.ra.value, 0)
934
935 # If this is a COND_CALL_VALUE, we need to move the result in place
936 # from its current location
937 if res_loc is not None:
938 self.mc.MV(res_loc.value, r.x30.value)
939
940 self.pop_gcmap(self.mc)
941
942 # Overwrite the conditional branch offset.
943 branch_dest_addr = self.mc.get_relative_pos()
944 branch_offset = branch_dest_addr - cond_branch_addr
945 pmc = OverwritingBuilder(self.mc, cond_branch_addr, INST_SIZE)
946 if res_loc is None:
947 pmc.BEQZ(arglocs[0].value, branch_offset)
948 self.previous_cond_call_branch = (cond_branch_addr, COND_BEQ,
949 arglocs[0])
950 else:
951 pmc.BNEZ(arglocs[0].value, branch_offset)
952 self.previous_cond_call_branch = (cond_branch_addr, COND_BNE,
953 arglocs[0])
954
955 emit_op_cond_call = _emit_op_cond_call
956 emit_op_cond_call_value_i = _emit_op_cond_call
957 emit_op_cond_call_value_r = _emit_op_cond_call
958
959 def emit_guard_op_cond_call(self, cond_op, op, arglocs, guard_branch_inst):
960 # TODO: Optimize guard_branch_inst when cond_op is using integer
961 # comparison.
962 assert guard_branch_inst == COND_INVALID
963
964 self._emit_op_cond_call(op, arglocs)
965
966 def _write_barrier_fastpath(self, mc, descr, arglocs, tmplocs, array=False,
967 is_frame=False):
968 # Emits the fast path for a GC write barrier.
969 #
970 # This function emits instructions that is equivalent to
971 # `write_barrier` or `write_barrier_from_array` in
972 # `rpython/memory/gc/incminimark.py`.
973 #
974 # This fast path `GCFLAG_TRACK_YOUNG_PTRS` (`jit_wb_if_flag`) and
975 # `GCFLAG_CARDS_SET` (`jit_wb_cards_set`). This is the overall
976 # structure of this function:
977 #
978 # if GCFLAG_TRACK_YOUNG_PTRS | GCFLAG_CARDS_SET:
979 # if GCFLAG_CARDS_SET:
980 # update_card_table()
981 # return
982 # write_barrier_slowpath()
983 # if GCFLAG_CARDS_SET:
984 # update_card_table()
985 #
986 # The card table is a utility data structure to assist generational GC.
987 # It is prepended to *large* arrays so that the garbage collector
988 # doesn't have to scan the complete array object if only some of them
989 # are updated. The card table maps `card_page_indices` (default: 128)
990 # indices into one bit, thus we need the byte/bit index calculation.
991 #
992 # The `write_barrier_slowpath` (`jit_remember_young_pointer_from_array`)
993 # only updates the `GCFLAGS_CARDS_SET` bit in its fast path, thus we
994 # must update card table afterward.
995 #
996 # OTOH, small arrays don't have card tables, thus we need the second
997 # `if GCFLAG_CARDS_SET` after returning from `write_barrier_slowpath`.
998
999 if we_are_translated():
1000 cls = self.cpu.gc_ll_descr.has_write_barrier_class()
1001 assert cls is not None and isinstance(descr, cls)
1002
1003 card_marking = 0
1004 mask = descr.jit_wb_if_flag_singlebyte
1005 if array and descr.jit_wb_cards_set != 0:
1006 # Asserts the assumptions the rest of the `_write_barrier_fastpath`
1007 # function and `self.wb_slowpath[n]`.
1008 assert (descr.jit_wb_cards_set_byteofs ==
1009 descr.jit_wb_if_flag_byteofs)
1010 assert descr.jit_wb_cards_set_singlebyte == -0x80
1011 card_marking = 1
1012 mask |= -0x80
1013
1014 loc_base = arglocs[0]
1015 assert loc_base.is_core_reg()
1016 assert not is_frame or loc_base is r.jfp
1017
1018 scratch_reg = r.x31
1019 mc.LBU(scratch_reg.value, loc_base.value, descr.jit_wb_if_flag_byteofs)
1020 mc.ANDI(scratch_reg.value, scratch_reg.value, mask & 0xff)
1021
1022 # Patch Location: BEQZ scratch_reg, end
1023 jz_location = mc.get_relative_pos()
1024 mc.EBREAK()
1025
1026 # For `cond_call_gc_wb_array`, also add another fast path:
1027 # If `GCFLAG_CARDS_SET`, then we can just update the card table (set
1028 # one bit) and done.
1029 js_location = 0
1030 if card_marking:
1031 # GCFLAG_CARDS_SET is in this byte at 0x80
1032 mc.ANDI(scratch_reg.value, scratch_reg.value, 0x80)
1033
1034 # Patch Location: BNEZ scratch_reg, update_card_table
1035 js_location = mc.get_relative_pos()
1036 mc.EBREAK()
1037
1038 # Write only a CALL to the helper prepared in advance, passing it as
1039 # argument the address of the structure we are writing into
1040 # (the first argument to COND_CALL_GC_WB).
1041 helper_num = card_marking
1042 if is_frame:
1043 helper_num = 4
1044 elif self._regalloc is not None and len(self._regalloc.fprm.reg_bindings):
1045 helper_num += 2 # Slowpath must spill float registers
1046
1047 if self.wb_slowpath[helper_num] == 0: # Tests only
1048 assert not we_are_translated()
1049 self.cpu.gc_ll_descr.write_barrier_descr = descr
1050 self._build_wb_slowpath(card_marking,
1051 bool(len(self._regalloc.fprm.reg_bindings)))
1052 assert self.wb_slowpath[helper_num] != 0
1053
1054 # Save `r.x10` to stack.
1055 stack_size = 0
1056 if loc_base is not r.x10:
1057 stack_size = ((XLEN * 1) + ABI_STACK_ALIGN - 1) // \
1058 ABI_STACK_ALIGN * ABI_STACK_ALIGN
1059 mc.ADDI(r.sp.value, r.sp.value, -stack_size)
1060 mc.store_int(r.x10.value, r.sp.value, 0)
1061 mc.MV(r.x10.value, loc_base.value)
1062 if is_frame:
1063 assert loc_base is r.jfp
1064
1065 # Call the slow path of the write barrier.
1066 mc.load_int_imm(r.ra.value, self.wb_slowpath[helper_num])
1067 mc.JALR(r.ra.value, r.ra.value, 0)
1068
1069 # Restore `r.x10` from stack.
1070 if loc_base is not r.x10:
1071 mc.load_int(r.x10.value, r.sp.value, 0)
1072 mc.ADDI(r.sp.value, r.sp.value, stack_size)
1073
1074 jns_location = 0
1075 if card_marking:
1076 # The helper ends again with a check of the flag in the object.
1077
1078 # After the `wb_slowpath`, we check `GCFLAG_CARDS_SET` again. If
1079 # `GCFLAG_CARDS_SET` isn't set, it implies that the object doesn't
1080 # have a card table (e.g. small array) and we can skip to the end.
1081
1082 # Patch Location: BEQZ x31, end_update_card_table
1083 jns_location = mc.get_relative_pos()
1084 mc.EBREAK()
1085
1086 # LABEL[update_card_table]:
1087
1088 # Patch the `js_location` above.
1089 offset = mc.get_relative_pos() - js_location
1090 pmc = OverwritingBuilder(mc, js_location, INST_SIZE)
1091 pmc.BNEZ(scratch_reg.value, offset)
1092
1093 # Update the card table if `GCFLAG_CARDS_SET` is set.
1094 loc_index = arglocs[1]
1095 assert loc_index.is_core_reg()
1096
1097 # Allocate scratch registers.
1098 scratch2_reg = r.ra
1099 scratch3_reg = tmplocs[0]
1100
1101 # byte_index:
1102 # scratch_reg = ~(index >> (descr.jit_wb_card_page_shift + lg2(8)))
1103 mc.SRLI(scratch_reg.value, loc_index.value,
1104 descr.jit_wb_card_page_shift + 3)
1105 mc.XORI(scratch_reg.value, scratch_reg.value, -1)
1106
1107 # byte_addr:
1108 mc.ADD(scratch_reg.value, loc_base.value, scratch_reg.value)
1109
1110 # bit_index:
1111 # scratch2_reg = (index >> descr.jit_wb_card_page_shift) & 0x7
1112 mc.SRLI(scratch2_reg.value, loc_index.value,
1113 descr.jit_wb_card_page_shift)
1114 mc.ANDI(scratch2_reg.value, scratch2_reg.value, 0x07)
1115
1116 # bit_mask:
1117 # scratch2_reg = (1 << scratch2_reg)
1118 mc.load_int_imm(scratch3_reg.value, 1)
1119 mc.SLL(scratch2_reg.value, scratch3_reg.value, scratch2_reg.value)
1120
1121 # Set the bit
1122 mc.LBU(scratch3_reg.value, scratch_reg.value, 0)
1123 mc.OR(scratch3_reg.value, scratch3_reg.value, scratch2_reg.value)
1124 mc.SB(scratch3_reg.value, scratch_reg.value, 0)
1125
1126 # LABEL[end_update_card_table]:
1127
1128 # Patch the `jns_location` above.
1129 offset = mc.get_relative_pos() - jns_location
1130 pmc = OverwritingBuilder(mc, jns_location, INST_SIZE)
1131 pmc.BEQZ(r.x31.value, offset)
1132
1133 # LABEL[end]:
1134
1135 # Patch `jz_location` above.
1136 offset = mc.get_relative_pos() - jz_location
1137 pmc = OverwritingBuilder(mc, jz_location, INST_SIZE)
1138 pmc.BEQZ(scratch_reg.value, offset)
1139
1140 def emit_op_cond_call_gc_wb(self, op, arglocs):
1141 tmplocs = arglocs[:1]
1142 arglocs = arglocs[1:]
1143 self._write_barrier_fastpath(self.mc, op.getdescr(), arglocs, tmplocs)
1144
1145 def emit_op_cond_call_gc_wb_array(self, op, arglocs):
1146 tmplocs = arglocs[:1]
1147 arglocs = arglocs[1:]
1148 self._write_barrier_fastpath(self.mc, op.getdescr(), arglocs, tmplocs,
1149 array=True)
1150
1151 def _store_force_index(self, guard_op):
1152 faildescr = guard_op.getdescr()
1153 faildescrindex = self.get_gcref_from_faildescr(faildescr)
1154 ofs = self.cpu.get_ofs_of_frame_field('jf_force_descr')
1155 scratch_reg = r.x31
1156 self.load_from_gc_table(scratch_reg.value, faildescrindex)
1157 self.mc.store_int(scratch_reg.value, r.jfp.value, ofs)
1158
1159 def _find_nearby_operation(self, delta):
1160 regalloc = self._regalloc
1161 return regalloc.operations[regalloc.rm.position + delta]
1162
1163 def emit_guard_op_guard_not_forced(self, call_op, guard_op, arglocs,
1164 num_arglocs):
1165 # arglocs is call_op_arglocs + guard_op_arglocs, split them
1166 if rop.is_call_assembler(call_op.getopnum()):
1167 if num_arglocs == 3:
1168 [argloc, vloc, result_loc] = arglocs[:3]
1169 else:
1170 [argloc, result_loc] = arglocs[:2]
1171 vloc = self.imm(0)
1172 guard_op_arglocs = arglocs[num_arglocs:]
1173 self._store_force_index(guard_op)
1174 # Note: In the `call_assembler` implementation:
1175 #
1176 # `tmploc` refers to the register holding the returned JITFrame
1177 # address, which is `r.x10` in RISC-V.
1178 #
1179 # `result_loc` refers to the register allocated for the returned
1180 # value of the `call_assembler` op. For now, it is the returned by
1181 # `after_call`, which returns `r.x10` for integers and `r.f10` for
1182 # floats.
1183 self.call_assembler(call_op, argloc, vloc, result_loc,
1184 tmploc=r.x10)
1185 else:
1186 assert num_arglocs == call_op.numargs() + 3
1187 call_op_arglocs = arglocs[0:num_arglocs]
1188 guard_op_arglocs = arglocs[num_arglocs:]
1189 self._store_force_index(guard_op)
1190 self._emit_op_call(call_op, call_op_arglocs)
1191
1192 # Implement guard_not_forced:
1193 #
1194 # if frame.jf_descr != 0:
1195 # goto guard_handler
1196 #
1197 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
1198 scratch_reg = r.x31
1199 self.mc.load_int(scratch_reg.value, r.jfp.value, ofs)
1200 self.mc.BEQZ(scratch_reg.value, 8)
1201 self._emit_pending_guard(guard_op, guard_op_arglocs)
1202
1203 def simple_call(self, fnloc, arglocs, result_loc=r.x10):
1204 if result_loc is None:
1205 result_type = VOID
1206 result_size = 0
1207 elif result_loc.is_fp_reg():
1208 result_type = FLOAT
1209 result_size = FLEN
1210 else:
1211 result_type = INT
1212 result_size = XLEN
1213 cb = RISCVCallBuilder(self, fnloc, arglocs, result_loc, result_type,
1214 result_size)
1215 cb.emit()
1216
1217 # Note: Read the `call_assembler` function in
1218 # `rpython/jit/backend/llsupport/assembler.py` to understand how these
1219 # `_call_assembler_*` functions work.
1220
1221 def _call_assembler_emit_call(self, addr, argloc, tmploc):
1222 # Move the threadlocal to r.x11.
1223 self.mc.load_int(r.x11.value, r.sp.value, self.saved_threadlocal_addr)
1224
1225 assert argloc is r.x10
1226 assert tmploc is r.x10
1227 self.simple_call(addr, [argloc, r.x11], result_loc=tmploc)
1228
1229 def _call_assembler_check_descr(self, expected_descr, tmploc):
1230 scratch_reg = r.x31
1231
1232 ofs = self.cpu.get_ofs_of_frame_field('jf_descr')
1233 self.mc.load_int(scratch_reg.value, tmploc.value, ofs)
1234
1235 if check_imm_arg(-expected_descr):
1236 self.mc.ADDI(scratch_reg.value, scratch_reg.value, -expected_descr)
1237 else:
1238 scratch2_reg = r.ra
1239 self.mc.load_int_imm(scratch2_reg.value, expected_descr)
1240 self.mc.SUB(scratch_reg.value, scratch_reg.value,
1241 scratch2_reg.value)
1242
1243 # Patch Location: BEQZ x31, load_result_fast_path
1244 pos = self.mc.get_relative_pos()
1245 self.mc.EBREAK()
1246 return pos
1247
1248 def _call_assembler_emit_helper_call(self, addr, arglocs, resloc):
1249 self.simple_call(addr, arglocs, result_loc=resloc)
1250
1251 def _call_assembler_patch_je(self, result_loc, jmp_location):
1252 # Patch Location: J end
1253 pos = self.mc.get_relative_pos()
1254 self.mc.EBREAK()
1255
1256 # LABEL[load_result_fast_path]:
1257
1258 # Patch the check_descr patch location.
1259 currpos = self.mc.get_relative_pos()
1260 pmc = OverwritingBuilder(self.mc, jmp_location, INST_SIZE)
1261 pmc.BEQZ(r.x31.value, currpos - jmp_location)
1262
1263 return pos
1264
1265 def _call_assembler_load_result(self, op, result_loc):
1266 # LABEL[load_result_fast_path]:
1267 if op.type != 'v':
1268 # Load the return value from the returned frame.
1269
1270 kind = op.type
1271 descr = self.cpu.getarraydescr_for_frame(kind)
1272 ofs = self.cpu.unpack_arraydescr(descr)
1273 # Note: The code above should be equivalent to
1274 # `ofs = self.cpu.get_baseofs_of_frame_field()` used in
1275 # `OP_FINISH`.
1276
1277 if kind == FLOAT:
1278 assert result_loc.is_fp_reg()
1279 self.mc.load_float(result_loc.value, r.x10.value, ofs)
1280 else:
1281 assert result_loc.is_core_reg()
1282 self.mc.load_int(result_loc.value, r.x10.value, ofs)
1283
1284 def _call_assembler_patch_jmp(self, jmp_location):
1285 # LABEL[end]:
1286 currpos = self.mc.get_relative_pos()
1287 pmc = OverwritingBuilder(self.mc, jmp_location, INST_SIZE)
1288 pmc.J(currpos - jmp_location)
1289
1290 def emit_op_guard_not_forced_2(self, op, arglocs):
1291 frame_depth = arglocs[0].value
1292 fail_locs = arglocs[1:]
1293 pos = self.mc.get_relative_pos()
1294 guardtok = self._build_guard_token(op, frame_depth, fail_locs, pos)
1295 self._finish_gcmap = guardtok.gcmap
1296 self._store_force_index(op)
1297 self.store_info_on_descr(pos, guardtok)
1298
1299 def emit_op_force_token(self, op, arglocs):
1300 self.mc.MV(arglocs[0].value, r.jfp.value)
1301
1302 def emit_op_load_from_gc_table(self, op, arglocs):
1303 res = arglocs[0]
1304 index = op.getarg(0).getint()
1305 self.load_from_gc_table(res.value, index)
1306
1307 def emit_op_zero_array(self, op, arglocs):
1308 # ZERO_ARRAY(base, start, size, scale_start, scale_size)
1309 #
1310 # Assume `base` is a byte pointer. Initialize
1311 # `base + (start * scale_start)` to
1312 # `base + (start * scale_start) + (size * scale_size)` with zeros.
1313
1314 assert len(arglocs) == 0, 'regalloc should not alloc for OP_ZERO_ARRAY'
1315 boxes = op.getarglist()
1316 base, start, size, scale_start, scale_size = boxes
1317
1318 # The scaling arguments should always be `ConstInt(1)` on RISC-V.
1319 assert isinstance(scale_start, ConstInt) and scale_start.getint() == 1
1320 assert isinstance(scale_size, ConstInt) and scale_size.getint() == 1
1321
1322 # Trivial case: size == 0, no code needed.
1323 if isinstance(size, ConstInt) and size.getint() == 0:
1324 return
1325
1326 item_size, base_ofs, _ = unpack_arraydescr(op.getdescr())
1327
1328 base_loc = self._regalloc.rm.make_sure_var_in_reg(base, boxes)
1329
1330 if isinstance(start, ConstInt):
1331 start_loc = None
1332 const_start = start.getint()
1333 assert const_start >= 0
1334 else:
1335 start_loc = self._regalloc.rm.make_sure_var_in_reg(start, boxes)
1336 const_start = -1
1337
1338 # `base_loc` and `start_loc` are in two regs here (or start_loc is an
1339 # immediate). Compute the `dstaddr_loc`, which is the raw address that
1340 # we will pass as first argument to `memset()`.
1341 dstaddr_loc = r.x31 # scratch_reg
1342 if const_start >= 0:
1343 ofs = base_ofs + const_start
1344 reg = base_loc
1345 else:
1346 self.mc.ADD(dstaddr_loc.value, base_loc.value, start_loc.value)
1347 ofs = base_ofs
1348 reg = dstaddr_loc
1349
1350 if check_imm_arg(ofs):
1351 self.mc.ADDI(dstaddr_loc.value, reg.value, ofs)
1352 else:
1353 scratch2_reg = r.ra
1354 self.mc.load_int_imm(scratch2_reg.value, ofs)
1355 self.mc.ADD(dstaddr_loc.value, reg.value, scratch2_reg.value)
1356
1357 # Use SB, SH, SW or SD based on whether the array item size is a
1358 # multiple of 1, 2, 4, or 8.
1359 if item_size & 1: item_size = 1
1360 elif item_size & 2: item_size = 2
1361 elif item_size & 4: item_size = 4
1362 else: item_size = XLEN
1363
1364 pre_align = 0
1365 single_store_size = item_size
1366 if item_size < XLEN and const_start >= 0:
1367 # Optimize SB/SH/SW into SW/SD.
1368 #
1369 # Assume that all arrays are naturally aligned to `XLEN` bytes, we
1370 # can increase the `item_size` to `XLEN` if (1) `start` is a
1371 # constant and (2) we emit pre-alignment zero initialization.
1372 pre_align = (-(const_start + base_ofs)) & (XLEN - 1)
1373 single_store_size = XLEN
1374
1375 # Inline limitation: An estimation on the number of instructions to be
1376 # emited to zero-initialize the array.
1377 #
1378 # RISC-V GCC `__builtin_memset(ptr, 0, n)` inlines up to 15
1379 # instructions. We decrease it by 5 if `pre_align` is not zero becuase
1380 # we need more pre/post-alignment store instructions.
1381 inline_limit = (15 if pre_align == 0 else 10) * single_store_size
1382
1383 if isinstance(size, ConstInt) and size.getint() <= inline_limit:
1384 # Implement zero_array with a series of store instructions,
1385 # starting at 'dstaddr_loc'.
1386
1387 # Note: In RISC-V, misaligned exception will only be raised when
1388 # the effective address (`reg[base] + imm`) is not aligned, thus we
1389 # don't have to insert `ADDI` to align `dstaddr` or `dst_i`.
1390
1391 total_size = size.getint()
1392
1393 # Pre-alignment zero initialization
1394 dst_i = 0
1395 if pre_align > 0:
1396 pre_align = min(pre_align, total_size)
1397 if pre_align & 1:
1398 self.mc.SB(r.x0.value, dstaddr_loc.value, dst_i)
1399 dst_i += 1
1400 if pre_align & 2:
1401 self.mc.SH(r.x0.value, dstaddr_loc.value, dst_i)
1402 dst_i += 2
1403 if pre_align & 4:
1404 self.mc.SW(r.x0.value, dstaddr_loc.value, dst_i)
1405 dst_i += 4
1406 total_size -= pre_align
1407
1408 # Zero initialization
1409 if single_store_size == 8:
1410 emit_store_inst = AbstractRISCVBuilder.SD
1411 elif single_store_size == 4:
1412 emit_store_inst = AbstractRISCVBuilder.SW
1413 elif single_store_size == 2:
1414 emit_store_inst = AbstractRISCVBuilder.SH
1415 else:
1416 emit_store_inst = AbstractRISCVBuilder.SB
1417
1418 while total_size >= single_store_size:
1419 emit_store_inst(self.mc, r.x0.value, dstaddr_loc.value, dst_i)
1420 dst_i += single_store_size
1421 total_size -= single_store_size
1422
1423 # Post-alignment zero initialization
1424 post_align = total_size
1425 if post_align & 4:
1426 self.mc.SW(r.x0.value, dstaddr_loc.value, dst_i)
1427 dst_i += 4
1428 if post_align & 2:
1429 self.mc.SH(r.x0.value, dstaddr_loc.value, dst_i)
1430 dst_i += 2
1431 if post_align & 1:
1432 self.mc.SB(r.x0.value, dstaddr_loc.value, dst_i)
1433 dst_i += 1
1434 else:
1435 if isinstance(size, ConstInt):
1436 size_loc = self.imm(size.getint())
1437 else:
1438 size_loc = self._regalloc.rm.make_sure_var_in_reg(size, boxes)
1439
1440 # Call `memset(dstaddr, 0, size)`
1441 malloc_func_adr = self.imm(self.memset_addr)
1442 malloc_arglocs = [dstaddr_loc, self.imm(0), size_loc]
1443 self._regalloc.before_call()
1444 cb = RISCVCallBuilder(self, malloc_func_adr, malloc_arglocs,
1445 resloc=None, restype=VOID, ressize=0)
1446 cb.emit_no_collect()
1447
1448 def emit_op_enter_portal_frame(self, op, arglocs):
1449 self.enter_portal_frame(op)
1450
1451 def emit_op_leave_portal_frame(self, op, arglocs):
1452 self.leave_portal_frame(op)
1453
1454 def emit_op_keepalive(self, op, arglocs):
1455 pass
1456
1457 def emit_op_jit_debug(self, op, arglocs):
1458 pass
1459
1460 def emit_op_increment_debug_counter(self, op, arglocs):
1461 base_loc = arglocs[0]
1462 scratch_reg = r.x31
1463 self.mc.load_int(scratch_reg.value, base_loc.value, 0)
1464 self.mc.ADDI(scratch_reg.value, scratch_reg.value, 1)
1465 self.mc.store_int(scratch_reg.value, base_loc.value, 0)
1466
1467 def emit_op_label(self, op, arglocs):
1468 pass
1469
1470 def emit_op_jump(self, op, arglocs):
1471 target_token = op.getdescr()
1472 assert isinstance(target_token, TargetToken)
1473 target = target_token._ll_loop_code
1474 if target_token in self.target_tokens_currently_compiling:
1475 relative_offset = target - self.mc.get_relative_pos()
1476 assert check_simm21_arg(relative_offset)
1477 self.mc.J(relative_offset)
1478 else:
1479 # Jump to the destination (absolute address).
1480 scratch_reg = r.x31
1481 self.mc.load_int_imm(scratch_reg.value, target)
1482 self.mc.JR(scratch_reg.value)
1483
1484 def emit_op_finish(self, op, arglocs):
1485 base_ofs = self.cpu.get_baseofs_of_frame_field()
1486 if len(arglocs) > 0:
1487 [return_val] = arglocs
1488 if return_val.is_fp_reg():
1489 self.mc.store_float(return_val.value, r.jfp.value, base_ofs)
1490 else:
1491 self.mc.store_int(return_val.value, r.jfp.value, base_ofs)
1492
1493 # Store `op.getdescr()` to `jf_descr`.
1494 faildescrindex = self.get_gcref_from_faildescr(op.getdescr())
1495 self.store_jf_descr(faildescrindex)
1496
1497 # Update `jf_gcmap`.
1498 if op.numargs() > 0 and op.getarg(0).type == REF:
1499 if self._finish_gcmap:
1500 # We're returning with a `op_guard_not_forced_2`. We need to
1501 # say that frame slot 0 (stored above) contains a reference
1502 # too.
1503 self._finish_gcmap[0] |= r_uint(1) << 0
1504 gcmap = self._finish_gcmap
1505 else:
1506 gcmap = self.gcmap_for_finish
1507 self.push_gcmap(self.mc, gcmap)
1508 elif self._finish_gcmap:
1509 # We're returning with a `op_guard_not_forced_2`.
1510 gcmap = self._finish_gcmap
1511 self.push_gcmap(self.mc, gcmap)
1512 else:
1513 # Note that 0 here is redundant, but I would rather keep that one
1514 # and kill all the others.
1515 ofs = self.cpu.get_ofs_of_frame_field('jf_gcmap')
1516 self.mc.store_int(r.x0.value, r.jfp.value, ofs)
1517
1518 self._call_footer(self.mc)
1519
1520
1521 def not_implemented_op(self, op, arglocs):
1522 llop.debug_print(lltype.Void,
1523 '[riscv/asm] %s not implemented' % op.getopname())
1524 raise NotImplementedError(op)
1525
1526 def not_implemented_comp_op(self, op, arglocs):
1527 llop.debug_print(lltype.Void,
1528 '[riscv/asm] %s not implemented' % op.getopname())
1529 raise NotImplementedError(op)
1530
1531 def not_implemented_guard_op(self, op, guard_op, arglocs, guard_branch_inst):
1532 llop.debug_print(lltype.Void,
1533 '[riscv/asm] %s not implemented' % op.getopname())
1534 raise NotImplementedError(op)
1535
1536 asm_operations = [not_implemented_op] * (rop._LAST + 1)
1537 asm_guard_operations = [not_implemented_guard_op] * (rop._LAST + 1)
1538 asm_comp_operations = [not_implemented_comp_op] * (rop._LAST + 1)
1539
1540 for name, value in OpAssembler.__dict__.iteritems():
1541 if name.startswith('emit_op_'):
1542 opname = name[len('emit_op_'):]
1543 num = getattr(rop, opname.upper())
1544 asm_operations[num] = value
1545 elif name.startswith('emit_guard_op_'):
1546 opname = name[len('emit_guard_op_'):]
1547 num = getattr(rop, opname.upper())
1548 asm_guard_operations[num] = value
1549 elif name.startswith('emit_comp_op_'):
1550 opname = name[len('emit_comp_op_'):]
1551 num = getattr(rop, opname.upper())
1552 asm_comp_operations[num] = value