Debian Codegraph @

rpython/jit/backend/model.py

raw symbols SCIP
1 import weakref
2 from rpython.rlib.debug import debug_start, debug_print, debug_stop
3 from rpython.rtyper.lltypesystem import lltype, llmemory
4 from rpython.rtyper.rclass import OBJECTPTR
5 from rpython.jit.metainterp.history import ConstInt
6 from rpython.jit.metainterp.support import ptr2int
7
8 class CPUTotalTracker(object):
9 total_compiled_loops = 0
10 total_compiled_bridges = 0
11 total_freed_loops = 0
12 total_freed_bridges = 0
13
14 class AbstractCPU(object):
15 supports_floats = False
16 supports_longlong = False
17 # ^^^ This is only useful on 32-bit platforms. If True,
18 # longlongs are supported by the JIT, but stored as doubles.
19 # Boxes and Consts are BoxFloats and ConstFloats.
20 supports_singlefloats = False
21 supports_guard_gc_type = False
22 supports_load_effective_address = False
23
24 propagate_exception_descr = None
25
26 remove_gctypeptr = False
27
28 def __init__(self):
29 self.tracker = CPUTotalTracker()
30
31 def _freeze_(self):
32 return True
33
34 def setup_once(self):
35 """Called once by the front-end when the program starts."""
36 pass
37
38 def finish_once(self):
39 """Called once by the front-end when the program stops."""
40 pass
41
42 def get_all_loop_runs(self):
43 """ Function that will return number of times all the loops were run.
44 Requires earlier setting of set_debug(True), otherwise you won't
45 get the information.
46
47 Returns an instance of LOOP_RUN_CONTAINER from rlib.jit_hooks
48 """
49 raise NotImplementedError
50
51 def set_debug(self, value):
52 """ Enable or disable debugging info. Does nothing by default. Returns
53 the previous setting.
54 """
55 return False
56
57 def compile_loop(self, inputargs, operations, looptoken, jd_id=0,
58 unique_id=0, log=True, name='', logger=None):
59 """Assemble the given loop.
60 Should create and attach a fresh CompiledLoopToken to
61 looptoken.compiled_loop_token and stick extra attributes
62 on it to point to the compiled loop in assembler.
63 Returns either None or an instance of rpython.rlib.jit.AsmInfo.
64 """
65 raise NotImplementedError
66
67 def compile_bridge(self, faildescr, inputargs, operations,
68 original_loop_token, log=True, logger=None):
69 """Assemble the bridge.
70 The FailDescr is the descr of the original guard that failed.
71 Returns either None or an instance of rpython.rlib.jit.AsmInfo.
72 """
73 raise NotImplementedError
74
75 def dump_loop_token(self, looptoken):
76 """Print a disassembled version of looptoken to stdout"""
77 raise NotImplementedError
78
79 def execute_token(self, looptoken, *args):
80 """NOT_RPYTHON (for tests only)
81 Execute the generated code referenced by the looptoken.
82 When done, this returns a 'dead JIT frame' object that can
83 be inspected with the get_latest_xxx() methods.
84 """
85 argtypes = [lltype.typeOf(x) for x in args]
86 execute = self.make_execute_token(*argtypes)
87 return execute(looptoken, *args)
88
89 def make_execute_token(self, *argtypes):
90 """Must make and return an execute_token() function that will be
91 called with the given argtypes.
92 """
93 raise NotImplementedError
94
95 def get_latest_descr(self, deadframe):
96 """Returns the Descr for the last operation executed by the frame."""
97 raise NotImplementedError
98
99 def get_int_value(self, deadframe, index):
100 """Returns the value for the index'th argument to the
101 last executed operation (from 'fail_args' if it was a guard,
102 or from 'args' if it was a FINISH). Returns an int."""
103 raise NotImplementedError
104
105 def get_float_value(self, deadframe, index):
106 """Returns the value for the index'th argument to the
107 last executed operation (from 'fail_args' if it was a guard,
108 or from 'args' if it was a FINISH). Returns a FLOATSTORAGE."""
109 raise NotImplementedError
110
111 def get_ref_value(self, deadframe, index):
112 """Returns the value for the index'th argument to the
113 last executed operation (from 'fail_args' if it was a guard,
114 or from 'args' if it was a FINISH). Returns a GCREF."""
115 raise NotImplementedError
116
117 def grab_exc_value(self, deadframe):
118 """Return the exception set by the latest execute_token(),
119 when it exits due to a failure of a GUARD_EXCEPTION or
120 GUARD_NO_EXCEPTION. (Returns a GCREF)""" # XXX remove me
121 raise NotImplementedError
122
123 def set_savedata_ref(self, deadframe, data):
124 """For the front-end: store a GCREF on the deadframe object."""
125 raise NotImplementedError
126
127 def get_savedata_ref(self, deadframe):
128 """For the front-end: get the GCREF saved with set_savedata_ref()."""
129 raise NotImplementedError
130
131 def force(self, force_token):
132 """Take a 'force token' as produced by the FORCE_TOKEN operation,
133 and 'kill' the corresponding JIT frame, which should be somewhere
134 in the stack right now. Returns it as a dead frame object. When
135 we later return to the JIT frame, the next operation executed must
136 be a GUARD_NOT_FORCED, which will fail."""
137 raise NotImplementedError
138
139 def redirect_call_assembler(self, oldlooptoken, newlooptoken):
140 """Redirect oldlooptoken to newlooptoken. More precisely, it is
141 enough to redirect all CALL_ASSEMBLERs already compiled that call
142 oldlooptoken so that from now own they will call newlooptoken."""
143 raise NotImplementedError
144
145 def invalidate_loop(self, looptoken):
146 """Activate all GUARD_NOT_INVALIDATED in the loop and its attached
147 bridges. Before this call, all GUARD_NOT_INVALIDATED do nothing;
148 after this call, they all fail. Note that afterwards, if one such
149 guard fails often enough, it has a bridge attached to it; it is
150 possible then to re-call invalidate_loop() on the same looptoken,
151 which must invalidate all newer GUARD_NOT_INVALIDATED, but not the
152 old one that already has a bridge attached to it."""
153 raise NotImplementedError
154
155 def free_loop_and_bridges(self, compiled_loop_token):
156 """This method is called to free resources (machine code,
157 references to resume guards, etc.) allocated by the compilation
158 of a loop and all bridges attached to it. After this call, the
159 frontend cannot use this compiled loop any more; in fact, it
160 guarantees that at the point of the call to free_code_group(),
161 none of the corresponding assembler is currently running.
162 """
163 pass
164
165 def sizeof(self, S):
166 raise NotImplementedError
167
168 def fielddescrof(self, S, fieldname):
169 """Return the Descr corresponding to field 'fieldname' on the
170 structure 'S'. It is important that this function (at least)
171 caches the results."""
172 raise NotImplementedError
173
174 def interiorfielddescrof(self, A, fieldname):
175 raise NotImplementedError
176
177 def arraydescrof(self, A):
178 raise NotImplementedError
179
180 def calldescrof(self, FUNC, ARGS, RESULT, extrainfo):
181 # FUNC is the original function type, but ARGS is a list of types
182 # with Voids removed
183 raise NotImplementedError
184
185 def typedescrof(self, TYPE):
186 raise NotImplementedError
187
188 def unpack_arraydescr_size(self, arraydescr):
189 """
190 Return basesize, itemsize, is_signed
191 """
192 raise NotImplementedError
193
194 @staticmethod
195 def cast_int_to_ptr(x, TYPE):
196 x = llmemory.cast_int_to_adr(x)
197 return llmemory.cast_adr_to_ptr(x, TYPE)
198
199 def cls_of_box(self, box):
200 obj = lltype.cast_opaque_ptr(OBJECTPTR, box.getref_base())
201 return ConstInt(ptr2int(obj.typeptr))
202
203
204 # ---------- the backend-dependent operations ----------
205
206 # lltype specific operations
207 # --------------------------
208
209 def bh_getarrayitem_gc_i(self, array, index, arraydescr):
210 raise NotImplementedError
211 def bh_getarrayitem_gc_r(self, array, index, arraydescr):
212 raise NotImplementedError
213 def bh_getarrayitem_gc_f(self, array, index, arraydescr):
214 raise NotImplementedError
215
216 def bh_getfield_gc_i(self, struct, fielddescr):
217 raise NotImplementedError
218 def bh_getfield_gc_r(self, struct, fielddescr):
219 raise NotImplementedError
220 def bh_getfield_gc_f(self, struct, fielddescr):
221 raise NotImplementedError
222
223 def bh_getfield_raw_i(self, struct, fielddescr):
224 raise NotImplementedError
225 def bh_getfield_raw_r(self, struct, fielddescr):
226 raise NotImplementedError
227 def bh_getfield_raw_f(self, struct, fielddescr):
228 raise NotImplementedError
229
230 def bh_new(self, sizedescr):
231 raise NotImplementedError
232 def bh_new_with_vtable(self, vtable, sizedescr):
233 raise NotImplementedError
234 def bh_new_array(self, length, arraydescr):
235 raise NotImplementedError
236 def bh_newstr(self, length):
237 raise NotImplementedError
238 def bh_newunicode(self, length):
239 raise NotImplementedError
240
241 def bh_arraylen_gc(self, array, arraydescr):
242 raise NotImplementedError
243
244 def bh_classof(self, struct):
245 raise NotImplementedError
246
247 def bh_setarrayitem_gc_i(self, array, index, newvalue, arraydescr):
248 raise NotImplementedError
249 def bh_setarrayitem_gc_r(self, array, index, newvalue, arraydescr):
250 raise NotImplementedError
251 def bh_setarrayitem_gc_f(self, array, index, newvalue, arraydescr):
252 raise NotImplementedError
253
254 def bh_setfield_gc_i(self, struct, newvalue, fielddescr):
255 raise NotImplementedError
256 def bh_setfield_gc_r(self, struct, newvalue, fielddescr):
257 raise NotImplementedError
258 def bh_setfield_gc_f(self, struct, newvalue, fielddescr):
259 raise NotImplementedError
260
261 def bh_setfield_raw_i(self, struct, newvalue, fielddescr):
262 raise NotImplementedError
263 def bh_setfield_raw_f(self, struct, newvalue, fielddescr):
264 raise NotImplementedError
265
266 def bh_call_i(self, func, args_i, args_r, args_f, calldescr):
267 raise NotImplementedError
268 def bh_call_r(self, func, args_i, args_r, args_f, calldescr):
269 raise NotImplementedError
270 def bh_call_f(self, func, args_i, args_r, args_f, calldescr):
271 raise NotImplementedError
272 def bh_call_v(self, func, args_i, args_r, args_f, calldescr):
273 raise NotImplementedError
274
275 def bh_strlen(self, string):
276 raise NotImplementedError
277 def bh_strgetitem(self, string, index):
278 raise NotImplementedError
279 def bh_unicodelen(self, string):
280 raise NotImplementedError
281 def bh_unicodegetitem(self, string, index):
282 raise NotImplementedError
283 def bh_strsetitem(self, string, index, newvalue):
284 raise NotImplementedError
285 def bh_unicodesetitem(self, string, index, newvalue):
286 raise NotImplementedError
287 def bh_copystrcontent(self, src, dst, srcstart, dststart, length):
288 raise NotImplementedError
289 def bh_copyunicodecontent(self, src, dst, srcstart, dststart, length):
290 raise NotImplementedError
291
292 class CompiledLoopToken(object):
293 asmmemmgr_blocks = None
294 asmmemmgr_gcreftracers = None
295
296 def __init__(self, cpu, number):
297 cpu.tracker.total_compiled_loops += 1
298 self.cpu = cpu
299 self.number = number
300 self.bridges_count = 0
301 self.invalidate_positions = []
302 # a list of weakrefs to looptokens that has been redirected to
303 # this one
304 self.looptokens_redirected_to = []
305 debug_start("jit-mem-looptoken-alloc")
306 debug_print("allocating Loop #", self.number)
307 debug_stop("jit-mem-looptoken-alloc")
308
309 def compiling_a_bridge(self):
310 self.cpu.tracker.total_compiled_bridges += 1
311 self.bridges_count += 1
312 debug_start("jit-mem-looptoken-alloc")
313 debug_print("allocating Bridge #", self.bridges_count, "of Loop #", self.number)
314 debug_stop("jit-mem-looptoken-alloc")
315
316 def update_frame_info(self, oldlooptoken, baseofs):
317 new_fi = self.frame_info
318 new_loop_tokens = []
319 for ref in oldlooptoken.looptokens_redirected_to:
320 looptoken = ref()
321 if looptoken:
322 looptoken.frame_info.update_frame_depth(baseofs,
323 new_fi.jfi_frame_depth)
324 new_loop_tokens.append(ref)
325 oldlooptoken.frame_info.update_frame_depth(baseofs,
326 new_fi.jfi_frame_depth)
327 assert oldlooptoken is not None
328 new_loop_tokens.append(weakref.ref(oldlooptoken))
329 self.looptokens_redirected_to = new_loop_tokens
330
331 def __del__(self):
332 #debug_start("jit-mem-looptoken-free")
333 #debug_print("freeing Loop #", self.number, 'with',
334 # self.bridges_count, 'attached bridges')
335 self.cpu.free_loop_and_bridges(self)
336 self.cpu.tracker.total_freed_loops += 1
337 self.cpu.tracker.total_freed_bridges += self.bridges_count
338 #debug_stop("jit-mem-looptoken-free")