Debian Codegraph @

rpython/jit/backend/llsupport/jitframe.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
2 from rpython.rtyper.annlowlevel import llhelper
3 from rpython.rlib.objectmodel import specialize
4 from rpython.rlib.debug import ll_assert
5 from rpython.rlib.objectmodel import enforceargs
6 from rpython.rlib import rgc
7
8 SIZEOFSIGNED = rffi.sizeof(lltype.Signed)
9 IS_32BIT = (SIZEOFSIGNED == 4)
10
11 # this is an info that only depends on the assembler executed, copied from
12 # compiled loop token (in fact we could use this as a compiled loop token
13 # XXX do this
14
15 GCMAP = lltype.Array(lltype.Unsigned)
16 NULLGCMAP = lltype.nullptr(GCMAP)
17
18 @enforceargs(None, int, int)
19 def jitframeinfo_update_depth(jfi, base_ofs, new_depth):
20 if new_depth > jfi.jfi_frame_depth:
21 jfi.jfi_frame_depth = new_depth
22 jfi.jfi_frame_size = base_ofs + new_depth * SIZEOFSIGNED
23
24 def jitframeinfo_clear(jfi):
25 jfi.jfi_frame_size = 0
26 jfi.jfi_frame_depth = 0
27
28 JITFRAMEINFO_SIZE = 2 * SIZEOFSIGNED # make sure this stays correct
29
30 JITFRAMEINFO = lltype.Struct(
31 'JITFRAMEINFO',
32 # the depth of the frame
33 ('jfi_frame_depth', lltype.Signed),
34 # the total size of the frame, in bytes
35 ('jfi_frame_size', lltype.Signed),
36 adtmeths = {
37 'update_frame_depth': jitframeinfo_update_depth,
38 'clear': jitframeinfo_clear,
39 },
40 )
41
42 NULLFRAMEINFO = lltype.nullptr(JITFRAMEINFO)
43 JITFRAMEINFOPTR = lltype.Ptr(JITFRAMEINFO)
44
45 # the JITFRAME that's stored on the heap. See backend/<backend>/arch.py for
46 # detailed explanation how it is on your architecture
47
48 def jitframe_allocate(frame_info):
49 rgc.register_custom_trace_hook(JITFRAME, lambda_jitframe_trace)
50 frame = lltype.malloc(JITFRAME, frame_info.jfi_frame_depth)
51 frame.jf_frame_info = frame_info
52 return frame
53
54 def jitframe_resolve(frame):
55 while frame.jf_forward:
56 frame = frame.jf_forward
57 return frame
58
59 JITFRAME = lltype.GcForwardReference()
60
61 JITFRAME.become(lltype.GcStruct(
62 'JITFRAME',
63 ('jf_frame_info', lltype.Ptr(JITFRAMEINFO)),
64 # Once the execute_token() returns, the field 'jf_descr' stores the
65 # descr of the last executed operation (either a GUARD, or FINISH).
66 # This field is also set immediately before doing CALL_MAY_FORCE
67 # or CALL_ASSEMBLER.
68 ('jf_descr', llmemory.GCREF),
69 # guard_not_forced descr
70 ('jf_force_descr', llmemory.GCREF),
71 # a map of GC pointers
72 ('jf_gcmap', lltype.Ptr(GCMAP)),
73 # For the front-end: a GCREF for the savedata
74 ('jf_savedata', llmemory.GCREF),
75 # For GUARD_(NO)_EXCEPTION and GUARD_NOT_FORCED: the exception we
76 # got. (Note that in case of a regular FINISH generated from
77 # RPython code that finishes the function with an exception, the
78 # exception is not stored there, but is simply kept as a variable there)
79 ('jf_guard_exc', llmemory.GCREF),
80 # in case the frame got reallocated, we have to forward it somewhere
81 ('jf_forward', lltype.Ptr(JITFRAME)),
82 # the actual frame
83 ('jf_frame', lltype.Array(lltype.Signed)),
84 # note that we keep length field, because it's crucial to have the data
85 # about GCrefs here and not in frame info which might change
86 adtmeths = {
87 'allocate': jitframe_allocate,
88 'resolve': jitframe_resolve,
89 },
90 rtti = True,
91 ))
92
93 @specialize.memo()
94 def getofs(name):
95 return llmemory.offsetof(JITFRAME, name)
96
97 GCMAPLENGTHOFS = llmemory.arraylengthoffset(GCMAP)
98 GCMAPBASEOFS = llmemory.itemoffsetof(GCMAP, 0)
99 BASEITEMOFS = llmemory.itemoffsetof(JITFRAME.jf_frame, 0)
100 LENGTHOFS = llmemory.arraylengthoffset(JITFRAME.jf_frame)
101 SIGN_SIZE = llmemory.sizeof(lltype.Signed)
102 UNSIGN_SIZE = llmemory.sizeof(lltype.Unsigned)
103
104 def jitframe_trace(gc, obj_addr, callback, arg1, arg2):
105 gc._trace_callback(callback, arg1, arg2, obj_addr + getofs('jf_descr'))
106 gc._trace_callback(callback, arg1, arg2,obj_addr + getofs('jf_force_descr'))
107 gc._trace_callback(callback, arg1, arg2, obj_addr + getofs('jf_savedata'))
108 gc._trace_callback(callback, arg1, arg2, obj_addr + getofs('jf_guard_exc'))
109 gc._trace_callback(callback, arg1, arg2, obj_addr + getofs('jf_forward'))
110
111 if IS_32BIT:
112 MAX = 32
113 else:
114 MAX = 64
115 gcmap = (obj_addr + getofs('jf_gcmap')).address[0]
116 if not gcmap:
117 return # done
118 gcmap_lgt = (gcmap + GCMAPLENGTHOFS).signed[0]
119 no = 0
120 while no < gcmap_lgt:
121 cur = (gcmap + GCMAPBASEOFS + UNSIGN_SIZE * no).unsigned[0]
122 bitindex = 0
123 while bitindex < MAX:
124 if cur & (1 << bitindex):
125 # the 'bitindex' is set in 'cur'
126 index = no * SIZEOFSIGNED * 8 + bitindex
127 # sanity check
128 frame_lgt = (obj_addr + getofs('jf_frame') + LENGTHOFS) \
129 .signed[0]
130 ll_assert(index < frame_lgt, "bogus frame field get")
131 gc._trace_callback(callback, arg1, arg2,
132 obj_addr + getofs('jf_frame') +
133 BASEITEMOFS + SIGN_SIZE * index)
134 bitindex += 1
135 no += 1
136 lambda_jitframe_trace = lambda: jitframe_trace
137
138 JITFRAMEPTR = lltype.Ptr(JITFRAME)