Branch data Line data Source code
1 : : // This file is a part of Julia. License is MIT: https://julialang.org/license
2 : :
3 : : #include <stdlib.h>
4 : : #include <setjmp.h>
5 : : #ifdef _OS_WINDOWS_
6 : : #include <malloc.h>
7 : : #endif
8 : : #include "julia.h"
9 : : #include "julia_internal.h"
10 : : #include "builtin_proto.h"
11 : : #include "julia_assert.h"
12 : :
13 : : #ifdef __cplusplus
14 : : extern "C" {
15 : : #endif
16 : :
17 : : typedef struct {
18 : : jl_code_info_t *src; // contains the names and number of slots
19 : : jl_method_instance_t *mi; // MethodInstance we're executing, or NULL if toplevel
20 : : jl_module_t *module; // context for globals
21 : : jl_value_t **locals; // slots for holding local slots and ssavalues
22 : : jl_svec_t *sparam_vals; // method static parameters, if eval-ing a method body
23 : : size_t ip; // Leak the currently-evaluating statement index to backtrace capture
24 : : int preevaluation; // use special rules for pre-evaluating expressions (deprecated--only for ccall handling)
25 : : int continue_at; // statement index to jump to after leaving exception handler (0 if none)
26 : : } interpreter_state;
27 : :
28 : :
29 : : // general alloca rules are incompatible on C and C++, so define a macro that deals with the difference
30 : : #ifdef __cplusplus
31 : : #define JL_CPPALLOCA(var,n) \
32 : : var = (decltype(var))alloca((n))
33 : : #else
34 : : #define JL_CPPALLOCA(var,n) \
35 : : JL_GCC_IGNORE_START("-Wc++-compat") \
36 : : var = alloca((n)); \
37 : : JL_GCC_IGNORE_STOP
38 : : #endif
39 : :
40 : : #ifdef __clang_gcanalyzer__
41 : :
42 : : extern void JL_GC_ENABLEFRAME(interpreter_state*) JL_NOTSAFEPOINT;
43 : :
44 : : // This is necessary, because otherwise the analyzer considers this undefined
45 : : // behavior and terminates the exploration
46 : : #define JL_GC_PUSHFRAME(frame,locals,n) \
47 : : JL_CPPALLOCA(frame, sizeof(*frame)+((n) * sizeof(jl_value_t*))); \
48 : : memset(&frame[1], 0, sizeof(void*) * n); \
49 : : _JL_GC_PUSHARGS((jl_value_t**)&frame[1], n); \
50 : : locals = (jl_value_t**)&frame[1];
51 : :
52 : : #else
53 : :
54 : : #define JL_GC_ENCODE_PUSHFRAME(n) ((((size_t)(n))<<2)|2)
55 : :
56 : : #define JL_GC_PUSHFRAME(frame,locals,n) \
57 : : JL_CPPALLOCA(frame, sizeof(*frame)+(((n)+3)*sizeof(jl_value_t*))); \
58 : : ((void**)&frame[1])[0] = NULL; \
59 : : ((void**)&frame[1])[1] = (void*)JL_GC_ENCODE_PUSHFRAME(n); \
60 : : ((void**)&frame[1])[2] = jl_pgcstack; \
61 : : memset(&((void**)&frame[1])[3], 0, (n)*sizeof(jl_value_t*)); \
62 : : jl_pgcstack = (jl_gcframe_t*)&(((void**)&frame[1])[1]); \
63 : : locals = &((jl_value_t**)&frame[1])[3];
64 : :
65 : : // we define this separately so that we can populate the frame before we add it to the backtrace
66 : : // it's recommended to mark the containing function with NOINLINE, though not essential
67 : : #define JL_GC_ENABLEFRAME(frame) \
68 : : ((void**)&frame[1])[0] = __builtin_frame_address(0);
69 : :
70 : : #endif
71 : :
72 : :
73 : : static jl_value_t *eval_value(jl_value_t *e, interpreter_state *s);
74 : : static jl_value_t *eval_body(jl_array_t *stmts, interpreter_state *s, size_t ip, int toplevel);
75 : :
76 : : // method definition form
77 : :
78 : 140918 : static jl_value_t *eval_methoddef(jl_expr_t *ex, interpreter_state *s)
79 : : {
80 : 140918 : jl_value_t **args = jl_array_ptr_data(ex->args);
81 : :
82 : : // generic function definition
83 [ + + ]: 140918 : if (jl_expr_nargs(ex) == 1) {
84 : 71464 : jl_value_t **args = jl_array_ptr_data(ex->args);
85 : 71464 : jl_sym_t *fname = (jl_sym_t*)args[0];
86 : 71464 : jl_module_t *modu = s->module;
87 [ + + ]: 71464 : if (jl_is_globalref(fname)) {
88 : 14 : modu = jl_globalref_mod(fname);
89 : 14 : fname = jl_globalref_name(fname);
90 : : }
91 [ - + ]: 71464 : if (!jl_is_symbol(fname)) {
92 : 0 : jl_error("method: invalid declaration");
93 : : }
94 : 71464 : jl_value_t *bp_owner = (jl_value_t*)modu;
95 : 71464 : jl_binding_t *b = jl_get_binding_for_method_def(modu, fname);
96 : 71463 : _Atomic(jl_value_t*) *bp = &b->value;
97 : 71463 : jl_value_t *gf = jl_generic_function_def(b->name, b->owner, bp, bp_owner, b);
98 : 71463 : return gf;
99 : : }
100 : :
101 : 69454 : jl_value_t *atypes = NULL, *meth = NULL, *fname = NULL;
102 : 69454 : JL_GC_PUSH3(&atypes, &meth, &fname);
103 : :
104 : 69454 : fname = eval_value(args[0], s);
105 : 69454 : jl_methtable_t *mt = NULL;
106 [ + + ]: 69454 : if (jl_typeis(fname, jl_methtable_type)) {
107 : 7 : mt = (jl_methtable_t*)fname;
108 : : }
109 : 69454 : atypes = eval_value(args[1], s);
110 : 69454 : meth = eval_value(args[2], s);
111 : 69454 : jl_method_def((jl_svec_t*)atypes, mt, (jl_code_info_t*)meth, s->module);
112 : 69441 : JL_GC_POP();
113 : 69441 : return jl_nothing;
114 : : }
115 : :
116 : : // expression evaluator
117 : :
118 : 32530800 : static jl_value_t *do_call(jl_value_t **args, size_t nargs, interpreter_state *s)
119 : : {
120 : : jl_value_t **argv;
121 [ - + ]: 32530800 : assert(nargs >= 1);
122 : 32530800 : JL_GC_PUSHARGS(argv, nargs);
123 : : size_t i;
124 [ + + ]: 132396000 : for (i = 0; i < nargs; i++)
125 : 99864800 : argv[i] = eval_value(args[i], s);
126 : 32530900 : jl_value_t *result = jl_apply(argv, nargs);
127 : 32434700 : JL_GC_POP();
128 : 32434700 : return result;
129 : : }
130 : :
131 : 0 : static jl_value_t *do_invoke(jl_value_t **args, size_t nargs, interpreter_state *s)
132 : : {
133 : : jl_value_t **argv;
134 [ # # ]: 0 : assert(nargs >= 2);
135 : 0 : JL_GC_PUSHARGS(argv, nargs - 1);
136 : : size_t i;
137 [ # # ]: 0 : for (i = 1; i < nargs; i++)
138 : 0 : argv[i] = eval_value(args[i], s);
139 : 0 : jl_method_instance_t *meth = (jl_method_instance_t*)args[0];
140 [ # # ]: 0 : assert(jl_is_method_instance(meth));
141 : 0 : jl_value_t *result = jl_invoke(argv[1], &argv[2], nargs - 2, meth);
142 : 0 : JL_GC_POP();
143 : 0 : return result;
144 : : }
145 : :
146 : 682757000 : jl_value_t *jl_eval_global_var(jl_module_t *m, jl_sym_t *e)
147 : : {
148 : 682757000 : jl_value_t *v = jl_get_global(m, e);
149 [ + + ]: 682757000 : if (v == NULL)
150 : 26 : jl_undefined_var_error(e);
151 : 682757000 : return v;
152 : : }
153 : :
154 : 105244000 : static int jl_source_nslots(jl_code_info_t *src) JL_NOTSAFEPOINT
155 : : {
156 : 105244000 : return jl_array_len(src->slotflags);
157 : : }
158 : :
159 : 34351000 : static int jl_source_nssavalues(jl_code_info_t *src) JL_NOTSAFEPOINT
160 : : {
161 [ + + ]: 34351000 : return jl_is_long(src->ssavaluetypes) ? jl_unbox_long(src->ssavaluetypes) : jl_array_len(src->ssavaluetypes);
162 : : }
163 : :
164 : 33015300 : static void eval_stmt_value(jl_value_t *stmt, interpreter_state *s)
165 : : {
166 : 33015300 : jl_value_t *res = eval_value(stmt, s);
167 : 32919200 : s->locals[jl_source_nslots(s->src) + s->ip] = res;
168 : 32919200 : }
169 : :
170 : 154759000 : static jl_value_t *eval_value(jl_value_t *e, interpreter_state *s)
171 : : {
172 : 154759000 : jl_code_info_t *src = s->src;
173 [ + + ]: 154759000 : if (jl_is_ssavalue(e)) {
174 : 33932300 : ssize_t id = ((jl_ssavalue_t*)e)->id - 1;
175 [ + - + - : 33932300 : if (src == NULL || id >= jl_source_nssavalues(src) || id < 0 || s->locals == NULL)
+ - - + ]
176 : 0 : jl_error("access to invalid SSAValue");
177 : : else
178 : 33932300 : return s->locals[jl_source_nslots(src) + id];
179 : : }
180 [ + + + # : 120827000 : if (jl_is_slot(e) || jl_is_argument(e)) {
# + ]
181 : 24247900 : ssize_t n = jl_slot_number(e);
182 [ + # + - : 24247900 : if (src == NULL || n > jl_source_nslots(src) || n < 1 || s->locals == NULL)
+ - - + ]
183 : 0 : jl_error("access to invalid slot number");
184 : 24248000 : jl_value_t *v = s->locals[n - 1];
185 [ + + ]: 24248000 : if (v == NULL)
186 : 3 : jl_undefined_var_error((jl_sym_t*)jl_array_ptr_ref(src->slotnames, n - 1));
187 : 24248000 : return v;
188 : : }
189 [ + + ]: 96579200 : if (jl_is_quotenode(e)) {
190 : 12470000 : return jl_quotenode_value(e);
191 : : }
192 [ + + ]: 84109200 : if (jl_is_globalref(e)) {
193 : 26936300 : return jl_eval_global_var(jl_globalref_mod(e), jl_globalref_name(e));
194 : : }
195 [ + + ]: 57172900 : if (jl_is_symbol(e)) { // bare symbols appear in toplevel exprs not wrapped in `thunk`
196 : 9063970 : return jl_eval_global_var(s->module, (jl_sym_t*)e);
197 : : }
198 [ - + ]: 48108900 : if (jl_is_pinode(e)) {
199 : 0 : jl_value_t *val = eval_value(jl_fieldref_noalloc(e, 0), s);
200 : : #ifndef JL_NDEBUG
201 : 0 : JL_GC_PUSH1(&val);
202 : 0 : jl_typeassert(val, jl_fieldref_noalloc(e, 1));
203 : 0 : JL_GC_POP();
204 : : #endif
205 : 0 : return val;
206 : : }
207 [ + # + # : 48108900 : assert(!jl_is_phinode(e) && !jl_is_phicnode(e) && !jl_is_upsilonnode(e) && "malformed IR");
+ # ]
208 [ + + ]: 48109000 : if (!jl_is_expr(e))
209 : 14930300 : return e;
210 : 33178700 : jl_expr_t *ex = (jl_expr_t*)e;
211 : 33178700 : jl_value_t **args = jl_array_ptr_data(ex->args);
212 : 33178700 : size_t nargs = jl_array_len(ex->args);
213 : 33178700 : jl_sym_t *head = ex->head;
214 [ + + ]: 33178700 : if (head == jl_call_sym) {
215 : 32530800 : return do_call(args, nargs, s);
216 : : }
217 [ - + ]: 647890 : else if (head == jl_invoke_sym) {
218 : 0 : return do_invoke(args, nargs, s);
219 : : }
220 [ - + ]: 647890 : else if (head == jl_invoke_modify_sym) {
221 : 0 : return do_call(args + 1, nargs - 1, s);
222 : : }
223 [ + + ]: 647890 : else if (head == jl_isdefined_sym) {
224 : 2239 : jl_value_t *sym = args[0];
225 : 2239 : int defined = 0;
226 [ + + + - : 2239 : if (jl_is_slot(sym) || jl_is_argument(sym)) {
- + ]
227 : 2 : ssize_t n = jl_slot_number(sym);
228 [ + - + - : 2 : if (src == NULL || n > jl_source_nslots(src) || n < 1 || s->locals == NULL)
+ - - + ]
229 : 0 : jl_error("access to invalid slot number");
230 : 2 : defined = s->locals[n - 1] != NULL;
231 : : }
232 [ - + ]: 2237 : else if (jl_is_globalref(sym)) {
233 : 0 : defined = jl_boundp(jl_globalref_mod(sym), jl_globalref_name(sym));
234 : : }
235 [ + + ]: 2237 : else if (jl_is_symbol(sym)) {
236 : 2235 : defined = jl_boundp(s->module, (jl_sym_t*)sym);
237 : : }
238 [ + - + - ]: 4 : else if (jl_is_expr(sym) && ((jl_expr_t*)sym)->head == jl_static_parameter_sym) {
239 : 2 : ssize_t n = jl_unbox_long(jl_exprarg(sym, 0));
240 [ - + ]: 2 : assert(n > 0);
241 [ + - + - ]: 2 : if (s->sparam_vals && n <= jl_svec_len(s->sparam_vals)) {
242 : 2 : jl_value_t *sp = jl_svecref(s->sparam_vals, n - 1);
243 : 2 : defined = !jl_is_typevar(sp);
244 : : }
245 : : else {
246 : : // static parameter val unknown needs to be an error for ccall
247 : 0 : jl_error("could not determine static parameter value");
248 : : }
249 : : }
250 : : else {
251 : 0 : assert(0 && "malformed isdefined expression");
252 : : }
253 [ + + ]: 2239 : return defined ? jl_true : jl_false;
254 : : }
255 [ + + ]: 645651 : else if (head == jl_throw_undef_if_not_sym) {
256 : 4 : jl_value_t *cond = eval_value(args[1], s);
257 [ - + ]: 4 : assert(jl_is_bool(cond));
258 [ + - ]: 4 : if (cond == jl_false) {
259 : 4 : jl_sym_t *var = (jl_sym_t*)args[0];
260 [ - + ]: 4 : if (var == jl_getfield_undefref_sym)
261 : 0 : jl_throw(jl_undefref_exception);
262 : : else
263 : 4 : jl_undefined_var_error(var);
264 : : }
265 : 0 : return jl_nothing;
266 : : }
267 [ + + ]: 645647 : else if (head == jl_new_sym) {
268 : : jl_value_t **argv;
269 : 368453 : JL_GC_PUSHARGS(argv, nargs);
270 [ + + ]: 981048 : for (size_t i = 0; i < nargs; i++)
271 : 612595 : argv[i] = eval_value(args[i], s);
272 : 368453 : jl_value_t *v = jl_new_structv((jl_datatype_t*)argv[0], &argv[1], nargs - 1);
273 : 368448 : JL_GC_POP();
274 : 368448 : return v;
275 : : }
276 [ + + ]: 277194 : else if (head == jl_splatnew_sym) {
277 : : jl_value_t **argv;
278 : 18 : JL_GC_PUSHARGS(argv, 2);
279 : 18 : argv[0] = eval_value(args[0], s);
280 : 18 : argv[1] = eval_value(args[1], s);
281 : 18 : jl_value_t *v = jl_new_structt((jl_datatype_t*)argv[0], argv[1]);
282 : 16 : JL_GC_POP();
283 : 16 : return v;
284 : : }
285 [ + + ]: 277176 : else if (head == jl_new_opaque_closure_sym) {
286 : : jl_value_t **argv;
287 : 18 : JL_GC_PUSHARGS(argv, nargs);
288 [ + + ]: 90 : for (size_t i = 0; i < nargs; i++)
289 : 72 : argv[i] = eval_value(args[i], s);
290 [ - + ]: 18 : JL_NARGSV(new_opaque_closure, 4);
291 : 18 : jl_value_t *ret = (jl_value_t*)jl_new_opaque_closure((jl_tupletype_t*)argv[0], argv[1], argv[2],
292 : 18 : argv[3], argv+4, nargs-4);
293 : 12 : JL_GC_POP();
294 : 12 : return ret;
295 : : }
296 [ + + ]: 277158 : else if (head == jl_static_parameter_sym) {
297 : 22835 : ssize_t n = jl_unbox_long(args[0]);
298 [ - + ]: 22835 : assert(n > 0);
299 [ + - + - ]: 22835 : if (s->sparam_vals && n <= jl_svec_len(s->sparam_vals)) {
300 : 22835 : jl_value_t *sp = jl_svecref(s->sparam_vals, n - 1);
301 [ + + - + ]: 22835 : if (jl_is_typevar(sp) && !s->preevaluation)
302 : 0 : jl_undefined_var_error(((jl_tvar_t*)sp)->name);
303 : 22835 : return sp;
304 : : }
305 : : // static parameter val unknown needs to be an error for ccall
306 : 0 : jl_error("could not determine static parameter value");
307 : : }
308 [ + + ]: 254323 : else if (head == jl_copyast_sym) {
309 : 46426 : return jl_copy_ast(eval_value(args[0], s));
310 : : }
311 [ + + ]: 207897 : else if (head == jl_exc_sym) {
312 : 95897 : return jl_current_exception();
313 : : }
314 [ + + ]: 112000 : else if (head == jl_boundscheck_sym) {
315 : 32317 : return jl_true;
316 : : }
317 [ + # + - : 79683 : else if (head == jl_meta_sym || head == jl_coverageeffect_sym || head == jl_inbounds_sym || head == jl_loopinfo_sym ||
+ - + + ]
318 [ + - + - : 71540 : head == jl_aliasscope_sym || head == jl_popaliasscope_sym || head == jl_inline_sym || head == jl_noinline_sym) {
+ - - + ]
319 : 8143 : return jl_nothing;
320 : : }
321 [ + + + + ]: 71540 : else if (head == jl_gc_preserve_begin_sym || head == jl_gc_preserve_end_sym) {
322 : : // The interpreter generally keeps values that were assigned in this scope
323 : : // rooted. If the interpreter learns to be more aggressive here, we may
324 : : // want to explicitly root these values.
325 : 76 : return jl_nothing;
326 : : }
327 [ + - + - ]: 71464 : else if (head == jl_method_sym && nargs == 1) {
328 : 71464 : return eval_methoddef(ex, s);
329 : : }
330 [ # # ]: 0 : else if (head == jl_foreigncall_sym) {
331 : 0 : jl_error("`ccall` requires the compiler");
332 : : }
333 [ # # ]: 0 : else if (head == jl_cfunction_sym) {
334 : 0 : jl_error("`cfunction` requires the compiler");
335 : : }
336 : 0 : jl_errorf("unsupported or misplaced expression %s", jl_symbol_name(head));
337 : : abort();
338 : : }
339 : :
340 : : // phi nodes don't behave like proper instructions, so we require a special interpreter to handle them
341 : 59291400 : static size_t eval_phi(jl_array_t *stmts, interpreter_state *s, size_t ns, size_t to)
342 : : {
343 : 59291400 : size_t from = s->ip;
344 : 59291400 : size_t ip = to;
345 : 59291400 : unsigned nphi = 0;
346 [ + # ]: 59291400 : for (ip = to; ip < ns; ip++) {
347 : 59291400 : jl_value_t *e = jl_array_ptr_ref(stmts, ip);
348 [ + + ]: 59291400 : if (!jl_is_phinode(e))
349 : 59291400 : break;
350 : 57 : nphi += 1;
351 : : }
352 [ + + ]: 59291400 : if (nphi) {
353 : 9 : jl_value_t **dest = &s->locals[jl_source_nslots(s->src) + to];
354 : : jl_value_t **phis; // = (jl_value_t**)alloca(sizeof(jl_value_t*) * nphi);
355 : 9 : JL_GC_PUSHARGS(phis, nphi);
356 [ + + ]: 66 : for (unsigned i = 0; i < nphi; i++) {
357 : 57 : jl_value_t *e = jl_array_ptr_ref(stmts, to + i);
358 [ - + ]: 57 : assert(jl_is_phinode(e));
359 : 57 : jl_array_t *edges = (jl_array_t*)jl_fieldref_noalloc(e, 0);
360 : 57 : ssize_t edge = -1;
361 : 57 : size_t closest = to; // implicit edge has `to <= edge - 1 < to + i`
362 : : // this is because we could see the following IR (all 1-indexed):
363 : : // goto %3 unless %cond
364 : : // %2 = phi ...
365 : : // %3 = phi (1)[1 => %a], (2)[2 => %b]
366 : : // from = 1, to = closest = 2, i = 1 --> edge = 2, edge_from = 2, from = 2
367 [ + + ]: 125 : for (unsigned j = 0; j < jl_array_len(edges); ++j) {
368 : 68 : size_t edge_from = ((int32_t*)jl_array_data(edges))[j]; // 1-indexed
369 [ + + ]: 68 : if (edge_from == from + 1) {
370 [ + - ]: 31 : if (edge == -1)
371 : 31 : edge = j;
372 : : }
373 [ + + + + ]: 37 : else if (closest < edge_from && edge_from < (to + i + 1)) {
374 : : // if we found a nearer implicit branch from fall-through,
375 : : // that occurred since the last explicit branch,
376 : : // we should use the value from that edge instead
377 : 6 : edge = j;
378 : 6 : closest = edge_from;
379 : : }
380 : : }
381 : 57 : jl_value_t *val = NULL;
382 : 57 : unsigned n_oldphi = closest - to;
383 [ + + ]: 57 : if (n_oldphi) {
384 : : // promote this implicit branch to a basic block start
385 : : // and move all phi values to their position in edges
386 : : // note that we might have already processed some phi nodes
387 : : // in this basic block, so we need to be extra careful here
388 : : // to ignore those
389 [ + + ]: 24 : for (unsigned j = 0; j < n_oldphi; j++) {
390 : 18 : dest[j] = phis[j];
391 : : }
392 [ + + ]: 10 : for (unsigned j = n_oldphi; j < i; j++) {
393 : : // move the rest to the start of phis
394 : 4 : phis[j - n_oldphi] = phis[j];
395 : 4 : phis[j] = NULL;
396 : : }
397 : 6 : from = closest - 1;
398 : 6 : i -= n_oldphi;
399 : 6 : dest += n_oldphi;
400 : 6 : to += n_oldphi;
401 : 6 : nphi -= n_oldphi;
402 : : }
403 [ + + ]: 57 : if (edge != -1) {
404 : : // if edges list doesn't contain last branch, or the value is explicitly undefined
405 : : // then this value should be unused.
406 : 35 : jl_array_t *values = (jl_array_t*)jl_fieldref_noalloc(e, 1);
407 : 35 : val = jl_array_ptr_ref(values, edge);
408 [ + + ]: 35 : if (val)
409 : 30 : val = eval_value(val, s);
410 : : }
411 : 57 : phis[i] = val;
412 : : }
413 : : // now move all phi values to their position in edges
414 [ + + ]: 48 : for (unsigned j = 0; j < nphi; j++) {
415 : 39 : dest[j] = phis[j];
416 : : }
417 : 9 : JL_GC_POP();
418 : : }
419 : 59291400 : return ip;
420 : : }
421 : :
422 : 1697830 : static jl_value_t *eval_body(jl_array_t *stmts, interpreter_state *s, size_t ip, int toplevel)
423 : : {
424 : : jl_handler_t __eh;
425 : 1697830 : size_t ns = jl_array_len(stmts);
426 : 1697830 : jl_task_t *ct = jl_current_task;
427 : :
428 : 60666400 : while (1) {
429 : 62364200 : s->ip = ip;
430 [ - + ]: 62364200 : if (ip >= ns)
431 : 0 : jl_error("`body` expression must terminate in `return`. Use `block` instead.");
432 [ + + ]: 62364200 : if (toplevel)
433 : 60909100 : ct->world_age = jl_atomic_load_acquire(&jl_world_counter);
434 : 62364200 : jl_value_t *stmt = jl_array_ptr_ref(stmts, ip);
435 [ - + ]: 62364200 : assert(!jl_is_phinode(stmt));
436 : 62364200 : size_t next_ip = ip + 1;
437 [ + # + - ]: 62364200 : assert(!jl_is_phinode(stmt) && !jl_is_phicnode(stmt) && "malformed IR");
438 [ + + ]: 62364200 : if (jl_is_gotonode(stmt)) {
439 : 5266420 : next_ip = jl_gotonode_label(stmt) - 1;
440 : : }
441 [ + + ]: 57097800 : else if (jl_is_gotoifnot(stmt)) {
442 : 8324140 : jl_value_t *cond = eval_value(jl_gotoifnot_cond(stmt), s);
443 [ + + ]: 8324140 : if (cond == jl_false) {
444 : 4017780 : next_ip = jl_gotoifnot_label(stmt) - 1;
445 : : }
446 [ - + ]: 4306360 : else if (cond != jl_true) {
447 : 0 : jl_type_error("if", (jl_value_t*)jl_bool_type, cond);
448 : : }
449 : : }
450 [ + + ]: 48773700 : else if (jl_is_returnnode(stmt)) {
451 : 418511 : return eval_value(jl_returnnode_value(stmt), s);
452 : : }
453 [ + + ]: 48355200 : else if (jl_is_upsilonnode(stmt)) {
454 : 14 : jl_value_t *val = jl_fieldref_noalloc(stmt, 0);
455 [ + + ]: 14 : if (val)
456 : 6 : val = eval_value(val, s);
457 : 14 : jl_value_t *phic = s->locals[jl_source_nslots(s->src) + ip];
458 [ - + ]: 14 : assert(jl_is_ssavalue(phic));
459 : 14 : ssize_t id = ((jl_ssavalue_t*)phic)->id - 1;
460 : 14 : s->locals[jl_source_nslots(s->src) + id] = val;
461 : : }
462 [ + + ]: 48355100 : else if (jl_is_expr(stmt)) {
463 : : // Most exprs are allowed to end a BB by fall through
464 : 38932100 : jl_sym_t *head = ((jl_expr_t*)stmt)->head;
465 [ + + ]: 38932100 : if (head == jl_assign_sym) {
466 : 11991000 : jl_value_t *lhs = jl_exprarg(stmt, 0);
467 : 11991000 : jl_value_t *rhs = eval_value(jl_exprarg(stmt, 1), s);
468 [ + + - + ]: 11990900 : if (jl_is_slot(lhs)) {
469 : 11937300 : ssize_t n = jl_slot_number(lhs);
470 [ + - + - ]: 11937300 : assert(n <= jl_source_nslots(s->src) && n > 0);
471 : 11937300 : s->locals[n - 1] = rhs;
472 : : }
473 : : else {
474 : : jl_module_t *modu;
475 : : jl_sym_t *sym;
476 [ + + ]: 53680 : if (jl_is_globalref(lhs)) {
477 : 250 : modu = jl_globalref_mod(lhs);
478 : 250 : sym = jl_globalref_name(lhs);
479 : : }
480 : : else {
481 [ - + ]: 53430 : assert(jl_is_symbol(lhs));
482 : 53430 : modu = s->module;
483 : 53430 : sym = (jl_sym_t*)lhs;
484 : : }
485 : 53680 : JL_GC_PUSH1(&rhs);
486 : 53680 : jl_binding_t *b = jl_get_binding_wr_or_error(modu, sym);
487 : 53680 : jl_checked_assignment(b, rhs);
488 : 53668 : JL_GC_POP();
489 : : }
490 : : }
491 [ + + ]: 26941000 : else if (head == jl_enter_sym) {
492 : 1279100 : jl_enter_handler(&__eh);
493 : : // This is a bit tricky, but supports the implementation of PhiC nodes.
494 : : // They are conceptually slots, but the slot to store to doesn't get explicitly
495 : : // mentioned in the store (aka the "UpsilonNode") (this makes them integrate more
496 : : // nicely with the rest of the SSA representation). In a compiler, we would figure
497 : : // out which slot to store to at compile time when we encounter the statement. We
498 : : // can't quite do that here, but we do something similar: We scan the catch entry
499 : : // block (the only place where PhiC nodes may occur) to find all the Upsilons we
500 : : // can possibly encounter. Then, we remember which slot they store to (we abuse the
501 : : // SSA value result array for this purpose). TODO: We could do this only the first
502 : : // time we encounter a given enter.
503 : 1279100 : size_t catch_ip = jl_unbox_long(jl_exprarg(stmt, 0)) - 1;
504 [ + - ]: 1279100 : while (catch_ip < ns) {
505 : 1279100 : jl_value_t *phicnode = jl_array_ptr_ref(stmts, catch_ip);
506 [ + + ]: 1279100 : if (!jl_is_phicnode(phicnode))
507 : 1279100 : break;
508 : 8 : jl_array_t *values = (jl_array_t*)jl_fieldref_noalloc(phicnode, 0);
509 [ + + ]: 24 : for (size_t i = 0; i < jl_array_len(values); ++i) {
510 : 16 : jl_value_t *val = jl_array_ptr_ref(values, i);
511 [ - + ]: 16 : assert(jl_is_ssavalue(val));
512 : 16 : size_t upsilon = ((jl_ssavalue_t*)val)->id - 1;
513 [ - + ]: 16 : assert(jl_is_upsilonnode(jl_array_ptr_ref(stmts, upsilon)));
514 : 16 : s->locals[jl_source_nslots(s->src) + upsilon] = jl_box_ssavalue(catch_ip + 1);
515 : : }
516 : 8 : s->locals[jl_source_nslots(s->src) + catch_ip] = NULL;
517 : 8 : catch_ip += 1;
518 : : }
519 : : // store current top of exception stack for restore in pop_exception.
520 : 1279100 : s->locals[jl_source_nslots(s->src) + ip] = jl_box_ulong(jl_excstack_state());
521 [ + + ]: 1279100 : if (!jl_setjmp(__eh.eh_ctx, 1)) {
522 : 1279100 : return eval_body(stmts, s, next_ip, toplevel);
523 : : }
524 [ + + ]: 1375060 : else if (s->continue_at) { // means we reached a :leave expression
525 : 1279090 : ip = s->continue_at;
526 : 1279090 : s->continue_at = 0;
527 : 1279090 : continue;
528 : : }
529 : : else { // a real exception
530 : 95969 : ip = catch_ip;
531 : 95969 : continue;
532 : : }
533 : : }
534 [ + + ]: 25662000 : else if (head == jl_leave_sym) {
535 : 1279090 : int hand_n_leave = jl_unbox_long(jl_exprarg(stmt, 0));
536 [ - + ]: 1279090 : assert(hand_n_leave > 0);
537 : : // equivalent to jl_pop_handler(hand_n_leave), but retaining eh for longjmp:
538 : 1279090 : jl_handler_t *eh = ct->eh;
539 [ + + ]: 1279100 : while (--hand_n_leave > 0)
540 : 1 : eh = eh->prev;
541 : 1279090 : jl_eh_restore_state(eh);
542 : : // leave happens during normal control flow, but we must
543 : : // longjmp to pop the eval_body call for each enter.
544 : 1279090 : s->continue_at = next_ip;
545 : 1279090 : jl_longjmp(eh->eh_ctx, 1);
546 : : }
547 [ + + ]: 24382900 : else if (head == jl_pop_exception_sym) {
548 : 95946 : size_t prev_state = jl_unbox_ulong(eval_value(jl_exprarg(stmt, 0), s));
549 : 95946 : jl_restore_excstack(prev_state);
550 : : }
551 [ + + ]: 24286900 : else if (toplevel) {
552 [ + + + + ]: 23563700 : if (head == jl_method_sym && jl_expr_nargs(stmt) > 1) {
553 : 69454 : eval_methoddef((jl_expr_t*)stmt, s);
554 : : }
555 [ + + ]: 23494200 : else if (head == jl_toplevel_sym) {
556 : 61 : jl_value_t *res = jl_toplevel_eval(s->module, stmt);
557 : 61 : s->locals[jl_source_nslots(s->src) + s->ip] = res;
558 : : }
559 [ + + ]: 23494200 : else if (jl_is_toplevel_only_expr(stmt)) {
560 : 115993 : jl_toplevel_eval(s->module, stmt);
561 : : }
562 [ + + ]: 23378200 : else if (head == jl_meta_sym) {
563 [ + + + + ]: 144 : if (jl_expr_nargs(stmt) == 1 && jl_exprarg(stmt, 0) == (jl_value_t*)jl_nospecialize_sym) {
564 : 11 : jl_set_module_nospecialize(s->module, 1);
565 : : }
566 [ + + + + ]: 144 : if (jl_expr_nargs(stmt) == 1 && jl_exprarg(stmt, 0) == (jl_value_t*)jl_specialize_sym) {
567 : 5 : jl_set_module_nospecialize(s->module, 0);
568 : : }
569 [ + + ]: 144 : if (jl_expr_nargs(stmt) == 2) {
570 [ + + ]: 65 : if (jl_exprarg(stmt, 0) == (jl_value_t*)jl_optlevel_sym) {
571 [ + - ]: 23 : if (jl_is_long(jl_exprarg(stmt, 1))) {
572 : 23 : int n = jl_unbox_long(jl_exprarg(stmt, 1));
573 : 23 : jl_set_module_optlevel(s->module, n);
574 : : }
575 : : }
576 [ + + ]: 42 : else if (jl_exprarg(stmt, 0) == (jl_value_t*)jl_compile_sym) {
577 [ + - ]: 21 : if (jl_is_long(jl_exprarg(stmt, 1))) {
578 : 21 : jl_set_module_compile(s->module, jl_unbox_long(jl_exprarg(stmt, 1)));
579 : : }
580 : : }
581 [ + - ]: 21 : else if (jl_exprarg(stmt, 0) == (jl_value_t*)jl_infer_sym) {
582 [ + - ]: 21 : if (jl_is_long(jl_exprarg(stmt, 1))) {
583 : 21 : jl_set_module_infer(s->module, jl_unbox_long(jl_exprarg(stmt, 1)));
584 : : }
585 : : }
586 [ # # ]: 0 : else if (jl_exprarg(stmt, 0) == (jl_value_t*)jl_max_methods_sym) {
587 [ # # ]: 0 : if (jl_is_long(jl_exprarg(stmt, 1))) {
588 : 0 : jl_set_module_max_methods(s->module, jl_unbox_long(jl_exprarg(stmt, 1)));
589 : : }
590 : : }
591 : : }
592 : : }
593 : : else {
594 : 23378000 : eval_stmt_value(stmt, s);
595 : : }
596 : : }
597 : : else {
598 : 723237 : eval_stmt_value(stmt, s);
599 : : }
600 : : }
601 [ + + ]: 9423060 : else if (jl_is_newvarnode(stmt)) {
602 : 509120 : jl_value_t *var = jl_fieldref(stmt, 0);
603 [ - + - - ]: 509120 : assert(jl_is_slot(var));
604 : 509120 : ssize_t n = jl_slot_number(var);
605 [ + - + - ]: 509120 : assert(n <= jl_source_nslots(s->src) && n > 0);
606 : 509120 : s->locals[n - 1] = NULL;
607 : : }
608 [ + + - + ]: 8913940 : else if (toplevel && jl_is_linenode(stmt)) {
609 : 0 : jl_lineno = jl_linenode_line(stmt);
610 : : }
611 : : else {
612 : 8913940 : eval_stmt_value(stmt, s);
613 : : }
614 : 59291400 : ip = eval_phi(stmts, s, ns, next_ip);
615 : : }
616 : : abort();
617 : : }
618 : :
619 : : // preparing method IR for interpreter
620 : :
621 : 264172 : jl_code_info_t *jl_code_for_interpreter(jl_method_instance_t *mi)
622 : : {
623 : 264172 : jl_code_info_t *src = (jl_code_info_t*)mi->uninferred;
624 [ + - ]: 264172 : if (jl_is_method(mi->def.value)) {
625 [ + + - + ]: 264172 : if (!src || (jl_value_t*)src == jl_nothing) {
626 [ + - ]: 617 : if (mi->def.method->source) {
627 : 617 : src = (jl_code_info_t*)mi->def.method->source;
628 : : }
629 : : else {
630 [ # # ]: 0 : assert(mi->def.method->generator);
631 : 0 : src = jl_code_for_staged(mi);
632 : : }
633 : : }
634 [ + - + - ]: 264172 : if (src && (jl_value_t*)src != jl_nothing) {
635 : 264172 : JL_GC_PUSH1(&src);
636 : 264172 : src = jl_uncompress_ir(mi->def.method, NULL, (jl_array_t*)src);
637 : 264172 : mi->uninferred = (jl_value_t*)src;
638 : 264172 : jl_gc_wb(mi, src);
639 : 264172 : JL_GC_POP();
640 : : }
641 : : }
642 [ + - - + ]: 264172 : if (!src || !jl_is_code_info(src)) {
643 : 0 : jl_error("source missing for method called in interpreter");
644 : : }
645 : 264172 : return src;
646 : : }
647 : :
648 : : // interpreter entry points
649 : :
650 : 263555 : jl_value_t *NOINLINE jl_fptr_interpret_call(jl_value_t *f, jl_value_t **args, uint32_t nargs, jl_code_instance_t *codeinst)
651 : : {
652 : : interpreter_state *s;
653 : 263555 : jl_method_instance_t *mi = codeinst->def;
654 : 263555 : jl_code_info_t *src = jl_code_for_interpreter(mi);
655 : 263555 : jl_array_t *stmts = src->code;
656 [ - + ]: 263555 : assert(jl_typeis(stmts, jl_array_any_type));
657 : 263555 : unsigned nroots = jl_source_nslots(src) + jl_source_nssavalues(src) + 2;
658 : 263555 : jl_value_t **locals = NULL;
659 : 263555 : JL_GC_PUSHFRAME(s, locals, nroots);
660 : 263555 : locals[0] = (jl_value_t*)src;
661 : 263555 : locals[1] = (jl_value_t*)stmts;
662 : 263555 : s->locals = locals + 2;
663 : 263555 : s->src = src;
664 [ - + ]: 263555 : if (jl_is_module(mi->def.value)) {
665 : 0 : s->module = mi->def.module;
666 : : }
667 : : else {
668 : 263555 : s->module = mi->def.method->module;
669 : 263555 : size_t defargs = mi->def.method->nargs;
670 : 263555 : int isva = mi->def.method->isva ? 1 : 0;
671 : : size_t i;
672 : 263555 : s->locals[0] = f;
673 [ + + - + ]: 263555 : assert(isva ? nargs + 2 >= defargs : nargs + 1 == defargs);
674 [ + + ]: 751874 : for (i = 1; i < defargs - isva; i++)
675 : 488319 : s->locals[i] = args[i - 1];
676 [ + + ]: 263555 : if (isva) {
677 [ - + ]: 12326 : assert(defargs >= 2);
678 : 12326 : s->locals[defargs - 1] = jl_f_tuple(NULL, &args[defargs - 2], nargs + 2 - defargs);
679 : : }
680 : : }
681 : 263555 : s->sparam_vals = mi->sparam_vals;
682 : 263555 : s->preevaluation = 0;
683 : 263555 : s->continue_at = 0;
684 : 263555 : s->mi = mi;
685 : 263555 : JL_GC_ENABLEFRAME(s);
686 : 263555 : jl_value_t *r = eval_body(stmts, s, 0, 0);
687 : 263553 : JL_GC_POP();
688 : 263553 : return r;
689 : : }
690 : :
691 : : JL_DLLEXPORT jl_callptr_t jl_fptr_interpret_call_addr = &jl_fptr_interpret_call;
692 : :
693 : 0 : jl_value_t *jl_interpret_opaque_closure(jl_opaque_closure_t *oc, jl_value_t **args, size_t nargs)
694 : : {
695 : 0 : jl_method_t *source = oc->source;
696 : 0 : jl_code_info_t *code = jl_uncompress_ir(source, NULL, (jl_array_t*)source->source);
697 : : interpreter_state *s;
698 : 0 : unsigned nroots = jl_source_nslots(code) + jl_source_nssavalues(code) + 2;
699 : 0 : jl_task_t *ct = jl_current_task;
700 : 0 : size_t last_age = ct->world_age;
701 : 0 : ct->world_age = oc->world;
702 : 0 : jl_value_t **locals = NULL;
703 : 0 : JL_GC_PUSHFRAME(s, locals, nroots);
704 : 0 : locals[0] = (jl_value_t*)oc;
705 : : // The analyzer has some trouble with this
706 : 0 : locals[1] = (jl_value_t*)code;
707 : : JL_GC_PROMISE_ROOTED(code);
708 : 0 : locals[2] = (jl_value_t*)oc->captures;
709 : 0 : s->locals = locals + 2;
710 : 0 : s->src = code;
711 : 0 : s->module = source->module;
712 : 0 : s->sparam_vals = NULL;
713 : 0 : s->preevaluation = 0;
714 : 0 : s->continue_at = 0;
715 : 0 : s->mi = NULL;
716 : 0 : size_t defargs = source->nargs;
717 : 0 : int isva = source->isva;
718 [ # # # # ]: 0 : assert(isva ? nargs + 2 >= defargs : nargs + 1 == defargs);
719 [ # # ]: 0 : for (size_t i = 1; i < defargs - isva; i++)
720 : 0 : s->locals[i] = args[i - 1];
721 [ # # ]: 0 : if (isva) {
722 [ # # ]: 0 : assert(defargs >= 2);
723 : 0 : s->locals[defargs - 1] = jl_f_tuple(NULL, &args[defargs - 2], nargs + 2 - defargs);
724 : : }
725 : 0 : JL_GC_ENABLEFRAME(s);
726 : 0 : jl_value_t *r = eval_body(code->code, s, 0, 0);
727 : 0 : locals[0] = r; // GC root
728 : : JL_GC_PROMISE_ROOTED(r);
729 : 0 : jl_typeassert(r, jl_tparam1(jl_typeof(oc)));
730 : 0 : ct->world_age = last_age;
731 : 0 : JL_GC_POP();
732 : 0 : return r;
733 : : }
734 : :
735 : 155176 : jl_value_t *NOINLINE jl_interpret_toplevel_thunk(jl_module_t *m, jl_code_info_t *src)
736 : : {
737 : : interpreter_state *s;
738 : 155176 : unsigned nroots = jl_source_nslots(src) + jl_source_nssavalues(src);
739 : 155176 : JL_GC_PUSHFRAME(s, s->locals, nroots);
740 : 155176 : jl_array_t *stmts = src->code;
741 [ - + ]: 155176 : assert(jl_typeis(stmts, jl_array_any_type));
742 : 155176 : s->src = src;
743 : 155176 : s->module = m;
744 : 155176 : s->sparam_vals = jl_emptysvec;
745 : 155176 : s->continue_at = 0;
746 : 155176 : s->mi = NULL;
747 : 155176 : JL_GC_ENABLEFRAME(s);
748 : 155176 : jl_task_t *ct = jl_current_task;
749 : 155176 : size_t last_age = ct->world_age;
750 : 155176 : jl_value_t *r = eval_body(stmts, s, 0, 1);
751 : 154957 : ct->world_age = last_age;
752 : 154957 : JL_GC_POP();
753 : 154957 : return r;
754 : : }
755 : :
756 : : // deprecated: do not use this method in new code
757 : : // it uses special scoping / evaluation / error rules
758 : : // which should instead be handled in lowering
759 : 182291 : jl_value_t *NOINLINE jl_interpret_toplevel_expr_in(jl_module_t *m, jl_value_t *e, jl_code_info_t *src, jl_svec_t *sparam_vals)
760 : : {
761 : : interpreter_state *s;
762 : : jl_value_t **locals;
763 : 182291 : JL_GC_PUSHFRAME(s, locals, 0);
764 : : (void)locals;
765 : 182291 : s->src = src;
766 : 182291 : s->module = m;
767 : 182291 : s->sparam_vals = sparam_vals;
768 : 182291 : s->preevaluation = (sparam_vals != NULL);
769 : 182291 : s->continue_at = 0;
770 : 182291 : s->mi = NULL;
771 : 182291 : JL_GC_ENABLEFRAME(s);
772 : 182291 : jl_value_t *v = eval_value(e, s);
773 [ - + ]: 182287 : assert(v);
774 : 182287 : JL_GC_POP();
775 : 182287 : return v;
776 : : }
777 : :
778 : 307145 : JL_DLLEXPORT size_t jl_capture_interp_frame(jl_bt_element_t *bt_entry,
779 : : void *stateend, size_t space_remaining)
780 : : {
781 : 307145 : interpreter_state *s = &((interpreter_state*)stateend)[-1];
782 : 307145 : int need_module = !s->mi;
783 [ + + ]: 307145 : int required_space = need_module ? 4 : 3;
784 [ - + ]: 307145 : if (space_remaining < required_space)
785 : 0 : return 0; // Should not happen
786 [ + + ]: 307145 : size_t njlvalues = need_module ? 2 : 1;
787 : 307145 : uintptr_t entry_tags = jl_bt_entry_descriptor(njlvalues, 0, JL_BT_INTERP_FRAME_TAG, s->ip);
788 : 307145 : bt_entry[0].uintptr = JL_BT_NON_PTR_ENTRY;
789 : 307145 : bt_entry[1].uintptr = entry_tags;
790 [ + + ]: 612270 : bt_entry[2].jlvalue = s->mi ? (jl_value_t*)s->mi :
791 [ + + ]: 305125 : s->src ? (jl_value_t*)s->src : (jl_value_t*)jl_nothing;
792 [ + + ]: 307145 : if (need_module) {
793 : : // If we only have a CodeInfo (s->src), we are in a top level thunk and
794 : : // need to record the module separately.
795 : 305125 : bt_entry[3].jlvalue = (jl_value_t*)s->module;
796 : : }
797 : 307145 : return required_space;
798 : : }
799 : :
800 : :
801 : : #ifdef __cplusplus
802 : : }
803 : : #endif
|