Debian Codegraph @

rpython/jit/backend/arm/codebuilder.py

raw symbols SCIP
1 from rpython.jit.backend.arm import conditions as cond
2 from rpython.jit.backend.arm import registers as reg
3 from rpython.jit.backend.arm.arch import WORD, PC_OFFSET
4 from rpython.jit.backend.arm.instruction_builder import define_instructions
5 from rpython.jit.backend.llsupport.asmmemmgr import BlockBuilderMixin
6 from rpython.rlib.objectmodel import we_are_translated
7 from rpython.rtyper.lltypesystem import lltype, rffi, llmemory
8 from rpython.tool.udir import udir
9 from rpython.jit.backend.detect_cpu import autodetect
10
11 clear_cache = rffi.llexternal(
12 "__clear_cache",
13 [llmemory.Address, llmemory.Address],
14 lltype.Void,
15 _nowrapper=True,
16 sandboxsafe=True)
17
18
19 class AbstractARMBuilder(object):
20 def __init__(self, arch_version=7):
21 self.arch_version = arch_version
22
23 def NOP(self):
24 self.MOV_rr(0, 0)
25
26 def PUSH(self, regs, cond=cond.AL):
27 assert reg.sp.value not in regs
28 instr = 0
29 if len(regs) == 1:
30 instr = cond << 28 | 0x52D0004 | (regs[0] & 0xF) << 12
31 else:
32 instr = self._encode_reg_list(cond << 28 | 0x92D << 16, regs)
33 self.write32(instr)
34
35 def STM(self, base, regs, write_back=False, cond=cond.AL):
36 assert len(regs) > 0
37 instr = (cond << 28
38 | 0x11 << 23
39 | (1 if write_back else 0) << 21
40 | (base & 0xF) << 16)
41 instr = self._encode_reg_list(instr, regs)
42 self.write32(instr)
43
44 def LDM(self, base, regs, write_back=False, cond=cond.AL):
45 assert len(regs) > 0
46 instr = (cond << 28
47 | 0x11 << 23
48 | (1 if write_back else 0) << 21
49 | 1 << 20
50 | (base & 0xF) << 16)
51 instr = self._encode_reg_list(instr, regs)
52 self.write32(instr)
53
54 def VSTM(self, base, regs, write_back=False, cond=cond.AL):
55 # encoding T1
56 P = 0
57 U = 1
58 nregs = len(regs)
59 assert nregs > 0 and nregs <= 16
60 freg = regs[0]
61 D = (freg & 0x10) >> 4
62 Dd = (freg & 0xF)
63 nregs *= 2
64 instr = (cond << 28
65 | 3 << 26
66 | P << 24
67 | U << 23
68 | D << 22
69 | (1 if write_back else 0) << 21
70 | (base & 0xF) << 16
71 | Dd << 12
72 | 0xB << 8
73 | nregs)
74 self.write32(instr)
75
76 def VLDM(self, base, regs, write_back=False, cond=cond.AL):
77 # encoding T1
78 P = 0
79 U = 1
80 nregs = len(regs)
81 assert nregs > 0 and nregs <= 16
82 freg = regs[0]
83 D = (freg & 0x10) >> 4
84 Dd = (freg & 0xF)
85 nregs *= 2
86 instr = (cond << 28
87 | 3 << 26
88 | P << 24
89 | U << 23
90 | D << 22
91 | (1 if write_back else 0) << 21
92 | 1 << 20
93 | (base & 0xF) << 16
94 | Dd << 12
95 | 0xB << 8
96 | nregs)
97 self.write32(instr)
98
99 def VPUSH(self, regs, cond=cond.AL):
100 nregs = len(regs)
101 assert nregs > 0 and nregs <= 16
102 freg = regs[0]
103 D = (freg & 0x10) >> 4
104 Dd = (freg & 0xF)
105 nregs *= 2
106 instr = (cond << 28
107 | 0xD2D << 16
108 | D << 22
109 | Dd << 12
110 | 0xB << 8
111 | nregs)
112 self.write32(instr)
113
114 def VPOP(self, regs, cond=cond.AL):
115 nregs = len(regs)
116 assert nregs > 0 and nregs <= 16
117 freg = regs[0]
118 D = (freg & 0x10) >> 4
119 Dd = (freg & 0xF)
120 nregs *= 2
121 instr = (cond << 28
122 | 0xCBD << 16
123 | D << 22
124 | Dd << 12
125 | 0xB << 8
126 | nregs)
127 self.write32(instr)
128
129 def VMOV_rc(self, rt, rt2, dm, cond=cond.AL):
130 """This instruction copies two words from two ARM core registers into a
131 doubleword extension register, or from a doubleword extension register
132 to two ARM core registers.
133 """
134 op = 1
135 instr = (cond << 28
136 | 0xC << 24
137 | 0x4 << 20
138 | op << 20
139 | (rt2 & 0xF) << 16
140 | (rt & 0xF) << 12
141 | 0xB << 8
142 | 0x1 << 4
143 | (dm & 0xF))
144 self.write32(instr)
145
146 # VMOV<c> <Dm>, <Rt>, <Rt2>
147 def VMOV_cr(self, dm, rt, rt2, cond=cond.AL):
148 """This instruction copies two words from two ARM core registers into a
149 doubleword extension register, or from a doubleword extension register
150 to two ARM core registers.
151 """
152 op = 0
153 instr = (cond << 28
154 | 0xC << 24
155 | 0x4 << 20
156 | op << 20
157 | (rt2 & 0xF) << 16
158 | (rt & 0xF) << 12
159 | 0xB << 8
160 | 0x1 << 4
161 | (dm & 0xF))
162 self.write32(instr)
163
164 def VMOV_sc(self, dest, src):
165 """move a single precision vfp register[src] to a core reg[dest]"""
166 self._VMOV_32bit(src, dest, to_arm_register=1)
167
168 def VMOV_cs(self, dest, src):
169 """move a core register[src] to a single precision vfp
170 register[dest]"""
171 self._VMOV_32bit(dest, src, to_arm_register=0)
172
173 def _VMOV_32bit(self, float_reg, core_reg, to_arm_register, cond=cond.AL):
174 """This instruction transfers the contents of a single-precision VFP
175 register to an ARM core register, or the contents of an ARM core
176 register to a single-precision VFP register.
177 """
178 instr = (cond << 28
179 | 0xE << 24
180 | to_arm_register << 20
181 | ((float_reg >> 1) & 0xF) << 16
182 | core_reg << 12
183 | 0xA << 8
184 | (float_reg & 0x1) << 7
185 | 1 << 4)
186 self.write32(instr)
187
188 def VMOV_cc(self, dd, dm, cond=cond.AL):
189 sz = 1 # for 64-bit mode
190 instr = (cond << 28
191 | 0xEB << 20
192 | (dd & 0xF) << 12
193 | 0x5 << 9
194 | (sz & 0x1) << 8
195 | 0x1 << 6
196 | (dm & 0xF))
197 self.write32(instr)
198
199 def VCVT_float_to_int(self, target, source, cond=cond.AL):
200 opc2 = 0x5
201 sz = 1
202 self._VCVT(target, source, cond, opc2, sz)
203
204 def VCVT_int_to_float(self, target, source, cond=cond.AL):
205 self._VCVT(target, source, cond, 0, 1)
206
207 def _VCVT(self, target, source, cond, opc2, sz):
208 # A8.6.295
209 to_integer = (opc2 >> 2) & 1
210 if to_integer:
211 D = target & 1
212 target >>= 1
213 M = (source >> 4) & 1
214 else:
215 M = source & 1
216 source >>= 1
217 D = (target >> 4) & 1
218 op = 1
219 instr = (cond << 28
220 | 0xEB8 << 16
221 | D << 22
222 | opc2 << 16
223 | (target & 0xF) << 12
224 | 0x5 << 9
225 | sz << 8
226 | op << 7
227 | 1 << 6
228 | M << 5
229 | (source & 0xF))
230 self.write32(instr)
231
232 def _VCVT_single_double(self, target, source, cond, sz):
233 # double_to_single = (sz == '1');
234 D = target & 1 if sz else (target >> 4) & 1
235 M = (source >> 4) & 1 if sz else source & 1
236 instr = (cond << 28
237 | 0xEB7 << 16
238 | 0xAC << 4
239 | D << 22
240 | (target & 0xF) << 12
241 | sz << 8
242 | M << 5
243 | (source & 0xF))
244 self.write32(instr)
245
246 def VCVT_f64_f32(self, target, source, cond=cond.AL):
247 self._VCVT_single_double(target, source, cond, 1)
248
249 def VCVT_f32_f64(self, target, source, cond=cond.AL):
250 self._VCVT_single_double(target, source, cond, 0)
251
252 def POP(self, regs, cond=cond.AL):
253 instr = self._encode_reg_list(cond << 28 | 0x8BD << 16, regs)
254 self.write32(instr)
255
256 def BKPT(self):
257 """Unconditional breakpoint"""
258 self.write32(cond.AL << 28 | 0x1200070)
259
260 # corresponds to the instruction vmrs APSR_nzcv, fpscr
261 def VMRS(self, cond=cond.AL):
262 self.write32(cond << 28 | 0xEF1FA10)
263
264 def B(self, target, c=cond.AL):
265 self.gen_load_int(reg.ip.value, target, cond=c)
266 self.BX(reg.ip.value, c=c)
267
268 def BX(self, reg, c=cond.AL):
269 self.write32(c << 28 | 0x12FFF1 << 4 | (reg & 0xF))
270
271 def B_offs(self, target_ofs, c=cond.AL):
272 pos = self.currpos()
273 target_ofs = target_ofs - (pos + PC_OFFSET)
274 assert target_ofs & 0x3 == 0
275 self.write32(c << 28 | 0xA << 24 | (target_ofs >> 2) & 0xFFFFFF)
276
277 def BL(self, addr, c=cond.AL):
278 target = rffi.cast(rffi.INT, addr)
279 self.gen_load_int(reg.ip.value, target, cond=c)
280 self.BLX(reg.ip.value, c)
281
282 def BLX(self, reg, c=cond.AL):
283 self.write32(c << 28 | 0x12FFF3 << 4 | (reg & 0xF))
284
285 def MOVT_ri(self, rd, imm16, c=cond.AL):
286 """Move Top writes an immediate value to the top halfword of the
287 destination register. It does not affect the contents of the bottom
288 halfword."""
289 self.write32(c << 28
290 | 0x3 << 24
291 | (1 << 22)
292 | ((imm16 >> 12) & 0xF) << 16
293 | (rd & 0xF) << 12
294 | imm16 & 0xFFF)
295
296 def MOVW_ri(self, rd, imm16, c=cond.AL):
297 """Encoding A2 of MOV, that allow to load a 16 bit constant"""
298 self.write32(c << 28
299 | 0x3 << 24
300 | ((imm16 >> 12) & 0xF) << 16
301 | (rd & 0xF) << 12
302 | imm16 & 0xFFF)
303
304 def SXTB_rr(self, rd, rm, c=cond.AL):
305 self.write32(c << 28
306 | 0x06AF0070
307 | (rd & 0xF) << 12
308 | (rm & 0xF))
309
310 def SXTH_rr(self, rd, rm, c=cond.AL):
311 self.write32(c << 28
312 | 0x06BF0070
313 | (rd & 0xF) << 12
314 | (rm & 0xF))
315
316 def LDREX(self, rt, rn, c=cond.AL):
317 self.write32(c << 28
318 | 0x01900f9f
319 | (rt & 0xF) << 12
320 | (rn & 0xF) << 16)
321
322 def STREX(self, rd, rt, rn, c=cond.AL):
323 """rd must not be the same register as rt or rn"""
324 self.write32(c << 28
325 | 0x01800f90
326 | (rt & 0xF)
327 | (rd & 0xF) << 12
328 | (rn & 0xF) << 16)
329
330 def DMB(self):
331 # ARMv7 only. I guess ARMv6 CPUs cannot be used in symmetric
332 # multi-processing at all? That would make this instruction unneeded.
333 # note: 'cond' is only permitted on Thumb here, but don't
334 # write literally 0xf57ff05f, because it's larger than 31 bits
335 c = cond.AL
336 self.write32(c << 28
337 | 0x157ff05f)
338
339 FMDRR = VMOV_cr # uh, there are synonyms?
340 FMRRD = VMOV_rc
341
342 def _encode_reg_list(self, instr, regs):
343 for reg in regs:
344 instr |= 0x1 << reg
345 return instr
346
347 def _encode_imm(self, imm):
348 u = 1
349 if imm < 0:
350 u = 0
351 imm = -imm
352 return u, imm
353
354 def write32(self, word):
355 self.writechar(chr(word & 0xFF))
356 self.writechar(chr((word >> 8) & 0xFF))
357 self.writechar(chr((word >> 16) & 0xFF))
358 self.writechar(chr((word >> 24) & 0xFF))
359
360 def writechar(self, char):
361 raise NotImplementedError
362
363 def currpos(self):
364 raise NotImplementedError
365
366 def gen_load_int(self, r, value, cond=cond.AL):
367 if self.arch_version < 7:
368 self.gen_load_int_v6(r, value, cond)
369 else:
370 self.gen_load_int_v7(r, value, cond)
371
372 def gen_load_int_v7(self, r, value, cond=cond.AL):
373 """r is the register number, value is the value to be loaded to the
374 register"""
375 bottom = value & 0xFFFF
376 top = value >> 16
377 self.MOVW_ri(r, bottom, cond)
378 if top:
379 self.MOVT_ri(r, top, cond)
380
381 def gen_load_int_v6(self, r, value, cond=cond.AL):
382 from rpython.jit.backend.arm.conditions import AL
383 if cond != AL or 0 <= value <= 0xFFFF:
384 self._load_by_shifting(r, value, cond)
385 else:
386 self.LDR_ri(r, reg.pc.value)
387 self.MOV_rr(reg.pc.value, reg.pc.value)
388 self.write32(value)
389
390 def get_max_size_of_gen_load_int(self):
391 return 4 if self.arch_version < 7 else 2
392
393 ofs_shift = zip(range(8, 25, 8), range(12, 0, -4))
394
395 def _load_by_shifting(self, r, value, c=cond.AL):
396 # to be sure it is only called for the correct cases
397 assert c != cond.AL or 0 <= value <= 0xFFFF
398 self.MOV_ri(r, (value & 0xFF), cond=c)
399 for offset, shift in self.ofs_shift:
400 b = (value >> offset) & 0xFF
401 if b == 0:
402 continue
403 t = b | (shift << 8)
404 self.ORR_ri(r, r, imm=t, cond=c)
405
406
407 class OverwritingBuilder(AbstractARMBuilder):
408 def __init__(self, cb, start, size):
409 AbstractARMBuilder.__init__(self, cb.arch_version)
410 self.cb = cb
411 self.index = start
412 self.end = start + size
413
414 def currpos(self):
415 return self.index
416
417 def writechar(self, char):
418 assert self.index <= self.end
419 self.cb.overwrite(self.index, char)
420 self.index += 1
421
422
423 class InstrBuilder(BlockBuilderMixin, AbstractARMBuilder):
424
425 def __init__(self, arch_version=7):
426 AbstractARMBuilder.__init__(self, arch_version)
427 self.init_block_builder()
428 #
429 # ResOperation --> offset in the assembly.
430 # ops_offset[None] represents the beginning of the code after the last op
431 # (i.e., the tail of the loop)
432 self.ops_offset = {}
433
434 def mark_op(self, op):
435 pos = self.get_relative_pos()
436 self.ops_offset[op] = pos
437
438 def _dump_trace(self, addr, name, formatter=-1):
439 if not we_are_translated():
440 if formatter != -1:
441 name = name % formatter
442 dir = udir.ensure('asm', dir=True)
443 f = dir.join(name).open('wb')
444 data = rffi.cast(rffi.CCHARP, addr)
445 for i in range(self.currpos()):
446 f.write(data[i])
447 f.close()
448
449 def clear_cache(self, addr):
450 if we_are_translated():
451 startaddr = rffi.cast(llmemory.Address, addr)
452 endaddr = rffi.cast(llmemory.Address,
453 addr + self.get_relative_pos())
454 clear_cache(startaddr, endaddr)
455
456 def copy_to_raw_memory(self, addr):
457 self._copy_to_raw_memory(addr)
458 self.clear_cache(addr)
459 self._dump(addr, "jit-backend-dump", 'arm')
460
461 def currpos(self):
462 return self.get_relative_pos()
463
464
465 define_instructions(AbstractARMBuilder)