2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
20 #define get_param(ip, n) (!((n) & 1) ? get_lo((ip) + (n) / 2) : get_hi((ip) + (n) / 2))
21 #define param_size(n) (((n) + 1) / 2)
23 /* A cast to uintptr_t because of gcc optimizer inefficiency */
24 #define get_param(ip, n) ((uintptr_t)(ip)[n])
25 #define param_size(n) (n)
27 #define get_param(ip, n) get_unaligned_32(&(ip)[(n) * 2])
28 #define param_size(n) ((n) * 2)
31 #define get_i_param(n) get_param(ip + 1, n)
32 #define ADVANCE_I_PARAM(n) ADVANCE_IP(1 + param_size(n))
36 static const code_t attr_cold *cat(free_parameters_,ARG_MODE)(frame_s *fp, const code_t *ip, frame_t n_entries)
39 if (get_param(ip, 1) & OPCODE_FLAG_FREE_ARGUMENT)
40 frame_free_and_clear(fp, get_param(ip, 0));
41 ADVANCE_IP(param_size(2));
48 #define move_scalar(utype) \
50 frame_t slot_1 = get_i_param(0); \
51 frame_t slot_r = get_i_param(1); \
52 ajla_assert(!frame_test_flag(fp, slot_r), (file_line, "scalar move: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
54 if (unlikely(frame_test_and_clear_flag(fp, slot_1))) { \
55 frame_set_pointer(fp, slot_r, *frame_pointer(fp, slot_1));\
56 pointer_poison(frame_pointer(fp, slot_1)); \
58 *frame_slot(fp, slot_r, utype) = \
59 *frame_slot(fp, slot_1, utype); \
65 #define copy_scalar(utype) \
67 frame_t slot_1 = get_i_param(0); \
68 frame_t slot_r = get_i_param(1); \
69 ajla_assert(!frame_test_flag(fp, slot_r), (file_line, "scalar copy: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
71 if (unlikely(frame_test_flag(fp, slot_1))) { \
72 pointer_t ptr = *frame_pointer(fp, slot_1); \
73 frame_set_pointer(fp, slot_r, ptr); \
74 pointer_reference_owned(ptr); \
76 *frame_slot(fp, slot_r, utype) = \
77 *frame_slot(fp, slot_1, utype); \
84 #define DEFINE_FIXED_REAL_BINARY_OPCODE(tc, type, op) \
86 cat3(OPCODE_,tc,OP) + \
87 cat4(OPCODE_,tc,OP_,op) * cat3(OPCODE_,tc,OP_MULT) + \
88 cat4(OPCODE_,tc,TYPE_,type) * cat3(OPCODE_,tc,TYPE_MULT),\
89 cat7(tc,binary_,op,_,type,_,ARG_MODE), \
90 frame_t slot_1 = get_i_param(0); \
91 frame_t slot_2 = get_i_param(1); \
92 frame_t slot_r = get_i_param(2); \
93 ajla_assert(slot_r == slot_1 || slot_r == slot_2 || !frame_test_flag(fp, slot_r), (file_line, "fixed binary: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
94 if (unlikely(frame_test_2(fp, slot_1, slot_2))) { \
95 goto cat(escape_fixed_binary_thunk_,ARG_MODE); \
100 val1 = frame_var(fp, slot_1); \
101 val2 = frame_var(fp, slot_2); \
102 result = frame_var(fp, slot_r); \
103 barrier_aliasing(); \
104 if (unlikely(!cat5(tc,binary_,op,_,type)(val1, val2, result))) {\
105 barrier_aliasing(); \
106 goto cat(escape_fixed_binary_thunk_,ARG_MODE); \
108 barrier_aliasing(); \
109 ADVANCE_I_PARAM(4); \
113 #define DEFINE_FIXED_REAL_UNARY_OPCODE(tc, type, op) \
115 cat3(OPCODE_,tc,OP) + \
116 cat4(OPCODE_,tc,OP_,op) * cat3(OPCODE_,tc,OP_MULT) + \
117 cat4(OPCODE_,tc,TYPE_,type) * cat3(OPCODE_,tc,TYPE_MULT),\
118 cat7(tc,unary_,op,_,type,_,ARG_MODE), \
119 frame_t slot_1 = get_i_param(0); \
120 frame_t slot_r = get_i_param(1); \
121 ajla_assert(slot_r == slot_1 || !frame_test_flag(fp, slot_r), (file_line, "fixed unary: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
122 if (unlikely(frame_test_flag(fp, slot_1))) { \
123 goto cat(escape_fixed_unary_thunk_,ARG_MODE); \
127 val1 = frame_var(fp, slot_1); \
128 result = frame_var(fp, slot_r); \
129 barrier_aliasing(); \
130 cat5(tc,unary_,op,_,type)(val1, result); \
131 barrier_aliasing(); \
132 ADVANCE_I_PARAM(3); \
136 #define DEFINE_FIXED_TO_INT(type, utype) \
139 OPCODE_FIXED_OP_to_int * OPCODE_FIXED_OP_MULT + \
140 cat(OPCODE_FIXED_TYPE_,type) * OPCODE_FIXED_TYPE_MULT, \
141 cat4(FIXED_unary_to_int_,type,_,ARG_MODE), \
142 frame_t slot_1 = get_i_param(0); \
143 frame_t slot_r = get_i_param(1); \
144 if (unlikely(frame_test_flag(fp, slot_1))) { \
145 goto cat(escape_convert_thunk_,ARG_MODE); \
149 barrier_aliasing(); \
150 val1 = *frame_slot(fp, slot_1, type); \
151 r = (int_default_t)val1; \
152 if (unlikely(r != val1)) \
153 frame_set_pointer(fp, slot_r, convert_fixed_to_mpint(val1, false));\
155 *frame_slot(fp, slot_r, int_default_t) = r; \
156 barrier_aliasing(); \
157 ADVANCE_I_PARAM(3); \
161 #define DEFINE_FIXED_UTO_INT(type, utype) \
164 OPCODE_FIXED_OP_uto_int * OPCODE_FIXED_OP_MULT + \
165 cat(OPCODE_FIXED_TYPE_,type) * OPCODE_FIXED_TYPE_MULT, \
166 cat4(FIXED_unary_uto_int_,type,_,ARG_MODE), \
167 frame_t slot_1 = get_i_param(0); \
168 frame_t slot_r = get_i_param(1); \
169 if (unlikely(frame_test_flag(fp, slot_1))) { \
170 goto cat(escape_convert_thunk_,ARG_MODE); \
174 barrier_aliasing(); \
175 val1 = *frame_slot(fp, slot_1, utype); \
176 r = (int_default_t)val1; \
177 if (unlikely((utype)r != val1) || unlikely(r < 0)) \
178 frame_set_pointer(fp, slot_r, convert_fixed_to_mpint(val1, true));\
180 *frame_slot(fp, slot_r, int_default_t) = r; \
181 barrier_aliasing(); \
182 ADVANCE_I_PARAM(3); \
186 #define DEFINE_FIXED_FROM_INT(type, utype) \
189 OPCODE_FIXED_OP_from_int * OPCODE_FIXED_OP_MULT + \
190 cat(OPCODE_FIXED_TYPE_,type) * OPCODE_FIXED_TYPE_MULT, \
191 cat4(FIXED_unary_from_int_,type,_,ARG_MODE), \
192 frame_t slot_1 = get_i_param(0); \
193 frame_t slot_r = get_i_param(1); \
194 if (unlikely(frame_test_flag(fp, slot_1))) { \
195 goto cat(escape_convert_thunk_,ARG_MODE); \
197 int_default_t val1; \
199 barrier_aliasing(); \
200 val1 = *frame_slot(fp, slot_1, int_default_t); \
202 if (unlikely(r != val1)) \
203 frame_set_pointer(fp, slot_r, pointer_error(error_ajla(EC_SYNC, AJLA_ERROR_DOESNT_FIT), fp, ip pass_file_line));\
205 *frame_slot(fp, slot_r, type) = r; \
206 barrier_aliasing(); \
207 ADVANCE_I_PARAM(3); \
211 #define DEFINE_FIXED_UFROM_INT(type, utype) \
214 OPCODE_FIXED_OP_ufrom_int * OPCODE_FIXED_OP_MULT + \
215 cat(OPCODE_FIXED_TYPE_,type) * OPCODE_FIXED_TYPE_MULT, \
216 cat4(FIXED_unary_ufrom_int_,type,_,ARG_MODE), \
217 frame_t slot_1 = get_i_param(0); \
218 frame_t slot_r = get_i_param(1); \
219 if (unlikely(frame_test_flag(fp, slot_1))) { \
220 goto cat(escape_convert_thunk_,ARG_MODE); \
222 int_default_t val1; \
224 barrier_aliasing(); \
225 val1 = *frame_slot(fp, slot_1, int_default_t); \
227 if (unlikely((int_default_t)r != val1) || unlikely(val1 < 0))\
228 frame_set_pointer(fp, slot_r, pointer_error(error_ajla(EC_SYNC, AJLA_ERROR_DOESNT_FIT), fp, ip pass_file_line));\
230 *frame_slot(fp, slot_r, type) = r; \
231 barrier_aliasing(); \
232 ADVANCE_I_PARAM(3); \
236 #define DEFINE_SCALAR_MOVE_OPCODE(tc, type) \
238 cat3(OPCODE_,tc,OP) + \
239 cat3(OPCODE_,tc,OP_move) * cat3(OPCODE_,tc,OP_MULT) + \
240 cat4(OPCODE_,tc,TYPE_,type) * cat3(OPCODE_,tc,TYPE_MULT),\
241 cat5(tc,move_,type,_,ARG_MODE), \
245 #define DEFINE_SCALAR_COPY_OPCODE(tc, type) \
247 cat3(OPCODE_,tc,OP) + \
248 cat3(OPCODE_,tc,OP_copy) * cat3(OPCODE_,tc,OP_MULT) + \
249 cat4(OPCODE_,tc,TYPE_,type) * cat3(OPCODE_,tc,TYPE_MULT),\
250 cat5(tc,copy_,type,_,ARG_MODE), \
254 #define DEFINE_FIXED_LDC_OPCODE(cls, type, utype, opc, shrt) \
256 OPCODE_##cls##_OP + \
257 OPCODE_##cls##_OP_##opc * OPCODE_##cls##_OP_MULT + \
258 cat(OPCODE_##cls##_TYPE_,type) * OPCODE_##cls##_TYPE_MULT,\
259 cat6(fixed_,opc,_,type,_,ARG_MODE), \
263 slot = get_i_param(0); \
264 ajla_assert(!frame_test_flag(fp, slot), (file_line, "ldc fixed: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot));\
265 val = frame_slot(fp, slot, utype); \
266 barrier_aliasing(); \
267 sz = cat(fixed_ldc_,type)(val, ip + 1 + param_size(1), shrt); \
268 barrier_aliasing(); \
269 ADVANCE_I_PARAM(1); \
273 #define DEFINE_FIXED_OPCODES(n, type, utype, sz, bits) \
274 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, add) \
275 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, subtract) \
276 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, multiply) \
277 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, divide) \
278 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, udivide) \
279 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, modulo) \
280 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, umodulo) \
281 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, power) \
282 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, and) \
283 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, or) \
284 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, xor) \
285 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, shl) \
286 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, shr) \
287 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, ushr) \
288 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, rol) \
289 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, ror) \
290 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, bts) \
291 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, btr) \
292 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, btc) \
293 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, equal) \
294 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, not_equal)\
295 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, less) \
296 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, less_equal)\
297 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, uless) \
298 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, uless_equal)\
299 DEFINE_FIXED_REAL_BINARY_OPCODE(FIXED_, type, bt) \
300 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, not) \
301 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, neg) \
302 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, inc) \
303 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, dec) \
304 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, bswap) \
305 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, brev) \
306 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, bsf) \
307 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, bsr) \
308 DEFINE_FIXED_REAL_UNARY_OPCODE(FIXED_, type, popcnt) \
309 DEFINE_FIXED_TO_INT(type, utype) \
310 DEFINE_FIXED_UTO_INT(type, utype) \
311 DEFINE_FIXED_FROM_INT(type, utype) \
312 DEFINE_FIXED_UFROM_INT(type, utype) \
313 DEFINE_SCALAR_MOVE_OPCODE(FIXED_, type) \
314 DEFINE_SCALAR_COPY_OPCODE(FIXED_, type) \
315 DEFINE_FIXED_LDC_OPCODE(FIXED, type, utype, ldc, false)
317 for_all_fixed(DEFINE_FIXED_OPCODES)
319 #if TYPE_FIXED_N >= 3
320 DEFINE_FIXED_LDC_OPCODE(FIXED, int32_t, uint32_t, ldc16, true)
321 #if TYPE_FIXED_N >= 4
322 DEFINE_FIXED_LDC_OPCODE(FIXED, int64_t, uint64_t, ldc16, true)
323 #if TYPE_FIXED_N >= 5
324 DEFINE_FIXED_LDC_OPCODE(FIXED, int128_t, uint128_t, ldc16, true)
330 #define DEFINE_REAL_TO_INT(type, op) \
333 cat(OPCODE_REAL_OP_,op) * OPCODE_REAL_OP_MULT + \
334 cat(OPCODE_REAL_TYPE_,type) * OPCODE_REAL_TYPE_MULT, \
335 cat6(REAL_unary_,op,_,type,_,ARG_MODE), \
336 frame_t slot_1 = get_i_param(0); \
337 frame_t slot_r = get_i_param(1); \
338 if (unlikely(frame_test_flag(fp, slot_1))) { \
339 goto cat(escape_convert_thunk_,ARG_MODE); \
342 barrier_aliasing(); \
343 ret = cat4(REAL_unary_,op,_,type)(frame_slot(fp, slot_1, type), frame_slot(fp, slot_r, int_default_t));\
344 barrier_aliasing(); \
345 if (unlikely(!ret)) \
346 frame_set_pointer(fp, slot_r, convert_real_to_mpint(fp, slot_1, frame_get_type_of_local(fp, slot_1)));\
347 ADVANCE_I_PARAM(3); \
351 #define DEFINE_REAL_FROM_INT(type, op) \
354 cat(OPCODE_REAL_OP_,op) * OPCODE_REAL_OP_MULT + \
355 cat(OPCODE_REAL_TYPE_,type) * OPCODE_REAL_TYPE_MULT, \
356 cat6(REAL_unary_,op,_,type,_,ARG_MODE), \
357 frame_t slot_1 = get_i_param(0); \
358 frame_t slot_r = get_i_param(1); \
359 if (unlikely(frame_test_flag(fp, slot_1))) { \
360 goto cat(escape_convert_thunk_,ARG_MODE); \
362 barrier_aliasing(); \
363 cat4(REAL_unary_,op,_,type)(frame_slot(fp, slot_1, int_default_t), frame_slot(fp, slot_r, type));\
364 barrier_aliasing(); \
365 ADVANCE_I_PARAM(3); \
370 #define DEFINE_REAL_OPCODES(n, type, ntype, pack, unpack)\
371 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, add) \
372 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, subtract) \
373 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, multiply) \
374 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, divide) \
375 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, modulo) \
376 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, power) \
377 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, ldexp) \
378 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, atan2) \
379 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, equal) \
380 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, not_equal) \
381 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, less) \
382 DEFINE_FIXED_REAL_BINARY_OPCODE(REAL_, type, less_equal)\
383 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, neg) \
384 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, sqrt) \
385 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, cbrt) \
386 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, sin) \
387 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, cos) \
388 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, tan) \
389 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, asin) \
390 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, acos) \
391 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, atan) \
392 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, sinh) \
393 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, cosh) \
394 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, tanh) \
395 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, asinh) \
396 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, acosh) \
397 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, atanh) \
398 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, exp2) \
399 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, exp) \
400 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, exp10) \
401 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, log2) \
402 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, log) \
403 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, log10) \
404 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, round) \
405 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, ceil) \
406 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, floor) \
407 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, trunc) \
408 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, fract) \
409 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, mantissa) \
410 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, exponent) \
411 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, next_number)\
412 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, prev_number)\
413 DEFINE_REAL_TO_INT(type, to_int) \
414 DEFINE_REAL_FROM_INT(type, from_int) \
415 DEFINE_FIXED_REAL_UNARY_OPCODE(REAL_, type, is_exception)\
416 DEFINE_SCALAR_MOVE_OPCODE(REAL_, type) \
417 DEFINE_SCALAR_COPY_OPCODE(REAL_, type) \
418 DEFINE_FIXED_LDC_OPCODE(REAL, type, type, ldc, false)
420 for_all_real(DEFINE_REAL_OPCODES, for_all_empty)
423 DEFINE_LABEL(cat(escape_fixed_binary_thunk_,ARG_MODE),
426 ex = thunk_fixed_operator(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2), get_i_param(3));
427 if (ex == POINTER_FOLLOW_THUNK_GO)
429 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
430 RELOAD_EX_POSITION(ex);
434 DEFINE_LABEL(cat(escape_fixed_unary_thunk_,ARG_MODE),
437 ex = thunk_fixed_operator(fp, ip, get_i_param(0), NO_FRAME_T, get_i_param(1), get_i_param(2));
438 if (ex == POINTER_FOLLOW_THUNK_GO)
440 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
441 RELOAD_EX_POSITION(ex);
445 DEFINE_LABEL(cat(escape_convert_thunk_,ARG_MODE),
448 ex = thunk_convert(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2));
449 if (ex == POINTER_FOLLOW_THUNK_GO)
451 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
452 RELOAD_EX_POSITION(ex);
457 bool (attr_fastcall *function_int_binary)(const mpint_t *s1, const mpint_t *s2, mpint_t *r, ajla_error_t *err);
458 bool (attr_fastcall *function_int_binary_logical)(const mpint_t *s1, const mpint_t *s2, ajla_flat_option_t *r, ajla_error_t *err);
459 bool (attr_fastcall *function_int_unary)(const mpint_t *s1, mpint_t *r, ajla_error_t *err);
462 #define DEFINE_INT_BINARY_OPCODE(type, op) \
465 cat(OPCODE_INT_OP_,op) * OPCODE_INT_OP_MULT + \
466 cat(OPCODE_INT_TYPE_,type) * OPCODE_INT_TYPE_MULT, \
467 cat6(int_binary_,op,_,type,_,ARG_MODE), \
468 frame_t slot_1 = get_i_param(0); \
469 frame_t slot_2 = get_i_param(1); \
470 frame_t slot_r = get_i_param(2); \
471 ajla_assert(slot_r == slot_1 || slot_r == slot_2 || !frame_test_flag(fp, slot_r), (file_line, "int binary: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
472 if (unlikely(frame_test_2(fp, slot_1, slot_2))) { \
473 cat6(escape_int_binary_,op,_,type,_,ARG_MODE): \
474 function_int_binary = cat(mpint_,op); \
475 goto cat(escape_int_binary_thunk_,ARG_MODE); \
480 val1 = frame_var(fp, slot_1); \
481 val2 = frame_var(fp, slot_2); \
482 result = frame_var(fp, slot_r); \
483 barrier_aliasing(); \
484 if (unlikely(!cat4(INT_binary_,op,_,type)(val1, val2, result))) {\
485 barrier_aliasing(); \
486 goto cat6(escape_int_binary_,op,_,type,_,ARG_MODE);\
488 barrier_aliasing(); \
489 ADVANCE_I_PARAM(4); \
493 #define DEFINE_INT_BINARY_LOGICAL_OPCODE(type, op) \
496 cat(OPCODE_INT_OP_,op) * OPCODE_INT_OP_MULT + \
497 cat(OPCODE_INT_TYPE_,type) * OPCODE_INT_TYPE_MULT, \
498 cat6(int_binary_,op,_,type,_,ARG_MODE), \
499 frame_t slot_1 = get_i_param(0); \
500 frame_t slot_2 = get_i_param(1); \
501 frame_t slot_r = get_i_param(2); \
502 ajla_assert(slot_r == slot_1 || slot_r == slot_2 || !frame_test_flag(fp, slot_r), (file_line, "int logical: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
503 if (unlikely(frame_test_2(fp, slot_1, slot_2))) { \
504 cat6(escape_int_binary_logical_,op,_,type,_,ARG_MODE): \
505 function_int_binary_logical = cat(mpint_,op); \
506 goto cat(escape_int_binary_logical_thunk_,ARG_MODE); \
511 val1 = frame_var(fp, slot_1); \
512 val2 = frame_var(fp, slot_2); \
513 result = frame_var(fp, slot_r); \
514 barrier_aliasing(); \
515 if (unlikely(!cat4(INT_binary_,op,_,type)(val1, val2, result))) {\
516 barrier_aliasing(); \
517 goto cat6(escape_int_binary_logical_,op,_,type,_,ARG_MODE);\
519 barrier_aliasing(); \
520 ADVANCE_I_PARAM(4); \
524 #define DEFINE_INT_UNARY_OPCODE(type, op) \
527 cat(OPCODE_INT_OP_,op) * OPCODE_INT_OP_MULT + \
528 cat(OPCODE_INT_TYPE_,type) * OPCODE_INT_TYPE_MULT, \
529 cat6(int_binary_,op,_,type,_,ARG_MODE), \
530 frame_t slot_1 = get_i_param(0); \
531 frame_t slot_r = get_i_param(1); \
532 ajla_assert(slot_r == slot_1 || !frame_test_flag(fp, slot_r), (file_line, "int unary: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
533 if (unlikely(frame_test_flag(fp, slot_1))) { \
534 cat6(escape_int_unary_,op,_,type,_,ARG_MODE): \
535 function_int_unary = cat(mpint_,op); \
536 goto cat(escape_int_unary_thunk_,ARG_MODE); \
540 val1 = frame_var(fp, slot_1); \
541 result = frame_var(fp, slot_r); \
542 barrier_aliasing(); \
543 if (unlikely(!cat4(INT_unary_,op,_,type)(val1, result))) {\
544 barrier_aliasing(); \
545 goto cat6(escape_int_unary_,op,_,type,_,ARG_MODE);\
547 barrier_aliasing(); \
548 ADVANCE_I_PARAM(3); \
552 #define DEFINE_INT_TO_INT(type) \
555 OPCODE_INT_OP_to_int * OPCODE_INT_OP_MULT + \
556 cat(OPCODE_INT_TYPE_,type) * OPCODE_INT_TYPE_MULT, \
557 cat4(INT_unary_to_int_,type,_,ARG_MODE), \
558 frame_t slot_1 = get_i_param(0); \
559 frame_t slot_r = get_i_param(1); \
560 if (unlikely(frame_test_flag(fp, slot_1))) { \
561 goto cat(escape_convert_thunk_,ARG_MODE); \
565 barrier_aliasing(); \
566 val1 = *frame_slot(fp, slot_1, type); \
567 r = (int_default_t)val1; \
568 if (unlikely(r != val1)) \
569 frame_set_pointer(fp, slot_r, convert_fixed_to_mpint(val1, false));\
571 *frame_slot(fp, slot_r, int_default_t) = r; \
572 barrier_aliasing(); \
573 ADVANCE_I_PARAM(3); \
577 #define DEFINE_INT_FROM_INT(typeid, type) \
580 OPCODE_INT_OP_from_int * OPCODE_INT_OP_MULT + \
581 cat(OPCODE_INT_TYPE_,type) * OPCODE_INT_TYPE_MULT, \
582 cat4(INT_unary_from_int_,type,_,ARG_MODE), \
583 frame_t slot_1 = get_i_param(0); \
584 frame_t slot_r = get_i_param(1); \
585 if (unlikely(frame_test_flag(fp, slot_1))) { \
586 goto cat(escape_convert_thunk_,ARG_MODE); \
588 int_default_t val1; \
590 barrier_aliasing(); \
591 val1 = *frame_slot(fp, slot_1, int_default_t); \
593 if (unlikely(r != val1)) \
594 frame_set_pointer(fp, slot_r, convert_fixed_to_mpint(val1, false));\
596 *frame_slot(fp, slot_r, type) = r; \
597 barrier_aliasing(); \
598 ADVANCE_I_PARAM(3); \
602 #define DEFINE_INT_LDC_OPCODE(type, opc, shrt) \
605 OPCODE_INT_OP_##opc * OPCODE_INT_OP_MULT + \
606 cat(OPCODE_INT_TYPE_,type) * OPCODE_INT_TYPE_MULT, \
607 cat6(int_,opc,_,type,_,ARG_MODE), \
611 slot = get_i_param(0); \
612 ajla_assert(!frame_test_flag(fp, slot), (file_line, "ldc int: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot));\
613 val = frame_slot(fp, slot, type); \
614 barrier_aliasing(); \
615 sz = cat(int_ldc_,type)(val, ip + 1 + param_size(1), shrt); \
616 barrier_aliasing(); \
617 ADVANCE_I_PARAM(1); \
621 #define DEFINE_INT_OPCODES(typeid, s, u, sz, bits) \
622 DEFINE_INT_BINARY_OPCODE(s, add) \
623 DEFINE_INT_BINARY_OPCODE(s, subtract) \
624 DEFINE_INT_BINARY_OPCODE(s, multiply) \
625 DEFINE_INT_BINARY_OPCODE(s, divide) \
626 DEFINE_INT_BINARY_OPCODE(s, modulo) \
627 DEFINE_INT_BINARY_OPCODE(s, power) \
628 DEFINE_INT_BINARY_OPCODE(s, and) \
629 DEFINE_INT_BINARY_OPCODE(s, or) \
630 DEFINE_INT_BINARY_OPCODE(s, xor) \
631 DEFINE_INT_BINARY_OPCODE(s, shl) \
632 DEFINE_INT_BINARY_OPCODE(s, shr) \
633 DEFINE_INT_BINARY_OPCODE(s, bts) \
634 DEFINE_INT_BINARY_OPCODE(s, btr) \
635 DEFINE_INT_BINARY_OPCODE(s, btc) \
636 DEFINE_INT_BINARY_LOGICAL_OPCODE(s, equal) \
637 DEFINE_INT_BINARY_LOGICAL_OPCODE(s, not_equal) \
638 DEFINE_INT_BINARY_LOGICAL_OPCODE(s, less) \
639 DEFINE_INT_BINARY_LOGICAL_OPCODE(s, less_equal) \
640 DEFINE_INT_BINARY_LOGICAL_OPCODE(s, bt) \
641 DEFINE_INT_UNARY_OPCODE(s, not) \
642 DEFINE_INT_UNARY_OPCODE(s, neg) \
643 DEFINE_INT_UNARY_OPCODE(s, inc) \
644 DEFINE_INT_UNARY_OPCODE(s, dec) \
645 DEFINE_INT_UNARY_OPCODE(s, bsf) \
646 DEFINE_INT_UNARY_OPCODE(s, bsr) \
647 DEFINE_INT_UNARY_OPCODE(s, popcnt) \
648 DEFINE_INT_TO_INT(s) \
649 DEFINE_INT_FROM_INT(typeid, s) \
650 DEFINE_SCALAR_MOVE_OPCODE(INT_, s) \
651 DEFINE_SCALAR_COPY_OPCODE(INT_, s) \
652 DEFINE_INT_LDC_OPCODE(s, ldc, false) \
653 DEFINE_INT_LDC_OPCODE(s, ldc16, true)
655 for_all_int(DEFINE_INT_OPCODES, for_all_empty)
657 DEFINE_LABEL(cat(escape_int_binary_thunk_,ARG_MODE),
660 ex = thunk_int_binary_operator(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2), get_i_param(3), function_int_binary);
661 if (ex == POINTER_FOLLOW_THUNK_GO)
663 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
664 RELOAD_EX_POSITION(ex);
667 DEFINE_LABEL(cat(escape_int_binary_logical_thunk_,ARG_MODE),
670 ex = thunk_int_binary_logical_operator(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2), get_i_param(3), function_int_binary_logical);
671 if (ex == POINTER_FOLLOW_THUNK_GO)
673 else if (ex != POINTER_FOLLOW_THUNK_RETRY)
674 RELOAD_EX_POSITION(ex);
677 DEFINE_LABEL(cat(escape_int_unary_thunk_,ARG_MODE),
680 ex = thunk_int_unary_operator(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2), function_int_unary);
681 if (ex == POINTER_FOLLOW_THUNK_GO)
683 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
684 RELOAD_EX_POSITION(ex);
687 #define file_inc "ipret-a1.inc"
688 #include "for-fix.inc"
690 #define file_inc "ipret-a2.inc"
691 #include "for-int.inc"
693 #define file_inc "ipret-a3.inc"
694 #include "for-real.inc"
699 #define DEFINE_BOOL_BINARY_OPCODE(op, operator) \
702 cat(OPCODE_BOOL_OP_,op) * OPCODE_BOOL_OP_MULT, \
703 cat4(bool_binary_,op,_,ARG_MODE), \
704 frame_t slot_1 = get_i_param(0); \
705 frame_t slot_2 = get_i_param(1); \
706 frame_t slot_r = get_i_param(2); \
708 if (unlikely(frame_test_2(fp, slot_1, slot_2))) { \
709 ajla_assert(slot_r == slot_1 || slot_r == slot_2 || !frame_test_flag(fp, slot_r), (file_line, "bool binary: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
710 goto cat(escape_bool_binary_thunk_,ARG_MODE); \
712 ajla_flat_option_t val1; \
713 ajla_flat_option_t val2; \
714 ajla_flat_option_t result; \
715 barrier_aliasing(); \
716 val1 = *frame_slot(fp, slot_1, ajla_flat_option_t); \
717 val2 = *frame_slot(fp, slot_2, ajla_flat_option_t); \
718 result = val1 operator val2; \
719 *frame_slot(fp, slot_r, ajla_flat_option_t) = result; \
720 barrier_aliasing(); \
721 ADVANCE_I_PARAM(4); \
725 #define DEFINE_BOOL_UNARY_OPCODE(op, operator) \
728 cat(OPCODE_BOOL_OP_,op) * OPCODE_BOOL_OP_MULT, \
729 cat4(bool_unary_,op,_,ARG_MODE), \
730 frame_t slot_1 = get_i_param(0); \
731 frame_t slot_r = get_i_param(1); \
733 ajla_assert(slot_r == slot_1 || !frame_test_flag(fp, slot_r), (file_line, "bool unary: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));\
734 if (unlikely(frame_test_flag(fp, slot_1))) { \
735 goto cat(escape_bool_unary_thunk_,ARG_MODE); \
737 ajla_flat_option_t val1; \
738 ajla_flat_option_t result; \
739 barrier_aliasing(); \
740 val1 = *frame_slot(fp, slot_1, ajla_flat_option_t); \
741 result = val1 operator; \
742 *frame_slot(fp, slot_r, ajla_flat_option_t) = result; \
743 barrier_aliasing(); \
744 ADVANCE_I_PARAM(3); \
748 DEFINE_BOOL_BINARY_OPCODE(and, &)
749 DEFINE_BOOL_BINARY_OPCODE(or, |)
750 DEFINE_BOOL_BINARY_OPCODE(equal, ==)
751 DEFINE_BOOL_BINARY_OPCODE(not_equal, ^)
752 DEFINE_BOOL_BINARY_OPCODE(less, <)
753 DEFINE_BOOL_BINARY_OPCODE(less_equal, <=)
754 DEFINE_BOOL_UNARY_OPCODE(not, ^ 1)
756 DEFINE_LABEL(cat(escape_bool_binary_thunk_,ARG_MODE),
759 ex = thunk_bool_operator(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2), get_i_param(3));
760 if (ex == POINTER_FOLLOW_THUNK_GO)
762 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
763 RELOAD_EX_POSITION(ex);
766 DEFINE_LABEL(cat(escape_bool_unary_thunk_,ARG_MODE),
769 ex = thunk_bool_operator(fp, ip, get_i_param(0), NO_FRAME_T, get_i_param(1), get_i_param(2));
770 if (ex == POINTER_FOLLOW_THUNK_GO)
772 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
773 RELOAD_EX_POSITION(ex);
777 DEFINE_OPCODE_START_LBL(OPCODE_BOOL_OP + OPCODE_BOOL_OP_move, cat(bool_move_,ARG_MODE))
779 move_scalar(ajla_flat_option_t);
781 DEFINE_OPCODE_END(OPCODE_BOOL_OP + OPCODE_BOOL_OP_move)
783 DEFINE_OPCODE_START_LBL(OPCODE_BOOL_OP + OPCODE_BOOL_OP_copy, cat(bool_copy_,ARG_MODE))
785 copy_scalar(ajla_flat_option_t);
787 DEFINE_OPCODE_END(OPCODE_BOOL_OP + OPCODE_BOOL_OP_copy)
790 DEFINE_OPCODE_START(OPCODE_INT_LDC_LONG)
794 slot = get_i_param(0);
795 sz = ipret_int_ldc_long(fp, slot, ip + 1 + param_size(1));
799 DEFINE_OPCODE_END(OPCODE_INT_LDC_LONG)
802 DEFINE_OPCODE_START(OPCODE_IS_EXCEPTION)
804 frame_t slot_1 = get_i_param(0);
805 frame_t slot_r = get_i_param(1);
806 ajla_assert(slot_r == slot_1 || !frame_test_flag(fp, slot_r), (file_line, "is exception: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));
807 if (!frame_variable_is_flat(fp, slot_1)) {
808 void *ex = is_thunk_operator(fp, ip, slot_1, slot_r, get_i_param(2));
809 if (ex == POINTER_FOLLOW_THUNK_GO)
811 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
812 RELOAD_EX_POSITION(ex);
815 *frame_slot(fp, slot_r, ajla_flat_option_t) = 0;
820 DEFINE_OPCODE_END(OPCODE_IS_EXCEPTION)
822 DEFINE_OPCODE_START(OPCODE_EXCEPTION_CLASS)
825 ex = thunk_get_param(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2), 0);
826 if (likely(ex == POINTER_FOLLOW_THUNK_GO))
828 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
829 RELOAD_EX_POSITION(ex);
831 DEFINE_OPCODE_END(OPCODE_EXCEPTION_CLASS)
833 DEFINE_OPCODE_START(OPCODE_EXCEPTION_TYPE)
836 ex = thunk_get_param(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2), 1);
837 if (likely(ex == POINTER_FOLLOW_THUNK_GO))
839 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
840 RELOAD_EX_POSITION(ex);
842 DEFINE_OPCODE_END(OPCODE_EXCEPTION_TYPE)
844 DEFINE_OPCODE_START(OPCODE_EXCEPTION_AUX)
847 ex = thunk_get_param(fp, ip, get_i_param(0), get_i_param(1), get_i_param(2), 2);
848 if (likely(ex == POINTER_FOLLOW_THUNK_GO))
850 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
851 RELOAD_EX_POSITION(ex);
853 DEFINE_OPCODE_END(OPCODE_EXCEPTION_AUX)
855 DEFINE_OPCODE_START(OPCODE_SYSTEM_PROPERTY)
858 ex = ipret_get_system_property(fp, ip, get_i_param(0), get_i_param(1));
859 if (likely(ex == POINTER_FOLLOW_THUNK_GO))
861 else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY))
862 RELOAD_EX_POSITION(ex);
864 DEFINE_OPCODE_END(OPCODE_SYSTEM_PROPERTY)
867 DEFINE_OPCODE_START(OPCODE_FLAT_MOVE)
869 frame_t slot_1 = get_i_param(0);
870 frame_t slot_r = get_i_param(1);
871 if (likely(frame_variable_is_flat(fp, slot_1))) {
872 memcpy(frame_var(fp, slot_r), frame_var(fp, slot_1), frame_get_type_of_local(fp, slot_1)->size);
874 pointer_t ptr = frame_get_pointer_reference(fp, slot_1, true);
875 frame_set_pointer(fp, slot_r, ptr);
879 DEFINE_OPCODE_END(OPCODE_FLAT_MOVE)
881 DEFINE_OPCODE_START(OPCODE_FLAT_COPY)
883 frame_t slot_1 = get_i_param(0);
884 frame_t slot_r = get_i_param(1);
885 if (likely(frame_variable_is_flat(fp, slot_1))) {
886 memcpy(frame_var(fp, slot_r), frame_var(fp, slot_1), frame_get_type_of_local(fp, slot_1)->size);
888 pointer_t ptr = frame_get_pointer_reference(fp, slot_1, false);
889 frame_set_pointer(fp, slot_r, ptr);
893 DEFINE_OPCODE_END(OPCODE_FLAT_COPY)
895 DEFINE_OPCODE_START(OPCODE_REF_MOVE)
897 frame_t slot_1 = get_i_param(0);
898 frame_t slot_r = get_i_param(1);
899 pointer_t ptr = frame_get_pointer_reference(fp, slot_1, true);
900 pointer_poison(frame_pointer(fp, slot_1));
901 frame_set_pointer(fp, slot_r, ptr);
904 DEFINE_OPCODE_END(OPCODE_REF_MOVE)
906 DEFINE_OPCODE_START(OPCODE_REF_MOVE_CLEAR)
908 frame_t slot_1 = get_i_param(0);
909 frame_t slot_r = get_i_param(1);
910 pointer_t ptr = frame_get_pointer_reference(fp, slot_1, true);
911 frame_set_pointer(fp, slot_r, ptr);
914 DEFINE_OPCODE_END(OPCODE_REF_MOVE_CLEAR)
916 DEFINE_OPCODE_START(OPCODE_REF_COPY)
918 frame_t slot_1 = get_i_param(0);
919 frame_t slot_r = get_i_param(1);
920 pointer_t ptr = frame_get_pointer_reference(fp, slot_1, false);
921 frame_set_pointer(fp, slot_r, ptr);
924 DEFINE_OPCODE_END(OPCODE_REF_COPY)
926 DEFINE_OPCODE_START(OPCODE_BOX_MOVE_CLEAR)
928 frame_t slot_1 = get_i_param(0);
929 frame_t slot_r = get_i_param(1);
930 pointer_t ptr = ipret_copy_variable_to_pointer(fp, slot_1, true);
931 *frame_pointer(fp, slot_1) = pointer_empty();
932 frame_set_pointer(fp, slot_r, ptr);
935 DEFINE_OPCODE_END(OPCODE_BOX_MOVE_CLEAR)
937 DEFINE_OPCODE_START(OPCODE_BOX_COPY)
939 frame_t slot_1 = get_i_param(0);
940 frame_t slot_r = get_i_param(1);
941 pointer_t ptr = ipret_copy_variable_to_pointer(fp, slot_1, false);
942 frame_set_pointer(fp, slot_r, ptr);
945 DEFINE_OPCODE_END(OPCODE_BOX_COPY)
947 DEFINE_OPCODE_START(OPCODE_TAKE_BORROWED)
949 frame_t slot = get_i_param(0);
950 if (!frame_test_and_set_flag(fp, slot)) {
951 pointer_t ptr = *frame_pointer(fp, slot);
952 pointer_reference_owned(ptr);
956 DEFINE_OPCODE_END(OPCODE_TAKE_BORROWED)
959 DEFINE_OPCODE_START(OPCODE_DEREFERENCE)
961 frame_t slot = get_i_param(0);
964 struct data *fn = get_frame(fp)->function;
965 const struct type *type = frame_get_type_of_local(fp, slot);
966 bool may_be_borrowed = da(fn,function)->local_variables[slot].may_be_borrowed;
967 if (!frame_test_flag(fp, slot) && !may_be_borrowed && !TYPE_IS_FLAT(type))
968 internal(file_line, "dereference: %s: the value is unexpectedly borrowed", da(fn,function)->function_name);
971 frame_free(fp, slot);
972 pointer_poison(frame_pointer(fp, slot));
975 DEFINE_OPCODE_END(OPCODE_DEREFERENCE)
977 DEFINE_OPCODE_START(OPCODE_DEREFERENCE_CLEAR)
979 frame_t slot = get_i_param(0);
982 struct data *fn = get_frame(fp)->function;
983 const struct type *type = frame_get_type_of_local(fp, slot);
984 bool may_be_borrowed = da(fn,function)->local_variables[slot].may_be_borrowed;
985 if (!frame_test_flag(fp, slot) && !may_be_borrowed && !TYPE_IS_FLAT(type))
986 internal(file_line, "dereference clear: %s: the value is unexpectedly borrowed", da(fn,function)->function_name);
989 frame_free_and_clear(fp, slot);
992 DEFINE_OPCODE_END(OPCODE_DEREFERENCE_CLEAR)
994 DEFINE_OPCODE_START(OPCODE_EVAL)
996 frame_t slot = get_i_param(0);
997 if (frame_test_flag(fp, slot)) {
998 pointer_t *ptr = frame_pointer(fp, slot);
999 struct data attr_unused *result;
1000 /*cat(eval_again_,ARG_MODE):*/
1001 pointer_follow(ptr, true, result, PF_WAIT, fp, ip,
1002 RELOAD_EX_POSITION(ex_),
1003 goto cat(eval_skip_,ARG_MODE));
1005 if (unlikely(da_tag(result) == DATA_TAG_array_incomplete)) {
1006 ptr = &da(result,array_incomplete)->next;
1007 goto cat(eval_again_,ARG_MODE);
1010 cat(eval_skip_,ARG_MODE):;
1014 DEFINE_OPCODE_END(OPCODE_EVAL)
1017 DEFINE_OPCODE_START(OPCODE_ESCAPE_NONFLAT)
1019 frame_t n = get_i_param(0);
1021 ADVANCE_IP(n * ARG_MODE);
1023 DEFINE_OPCODE_END(OPCODE_ESCAPE_NONFLAT)
1025 DEFINE_OPCODE_START(OPCODE_CHECKPOINT)
1027 const code_t attr_unused *orig_ip = ip;
1028 frame_t attr_unused id;
1030 if (SIZEOF_IP_T == 2) {
1033 } else if (SIZEOF_IP_T == 4) {
1034 id = get_unaligned_32(&ip[1]);
1039 len = get_param(ip, 0);
1040 ADVANCE_IP((len + 1) * ARG_MODE);
1042 if (unlikely(tick_elapsed(&ts)) && likely(frame_execution_control(fp) != NULL)) {
1043 ipret_checkpoint_forced;
1046 struct data *fn = get_frame(fp)->function;
1050 struct data *codegen;
1051 if (unlikely(load_relaxed(&da(fn,function)->codegen_failed)))
1052 goto cat(checkpoint_exit_,ARG_MODE);
1054 pointer_follow(&da(fn,function)->codegen, false, codegen, PF_WAIT, fp, orig_ip,
1055 RELOAD_EX_POSITION(ex_),
1056 store_relaxed(&da(fn,function)->codegen_failed, 1);
1057 goto cat(checkpoint_exit_,ARG_MODE);
1060 pointer_follow(&da(fn,function)->codegen, false, codegen, PF_SPARK, NULL, 0,
1061 /*debug("sparked: %s %p", da(fn,function)->function_name, ex_);*/
1063 goto cat(checkpoint_exit_,ARG_MODE),
1064 store_relaxed(&da(fn,function)->codegen_failed, 1);
1065 goto cat(checkpoint_exit_,ARG_MODE);
1069 /*for (frame_t l = MIN_USEABLE_SLOT; l < function_n_variables(fn); l++) {
1070 if (da(fn,function)->local_variables_flags[l].must_be_flat) {
1071 if (unlikely(frame_test_flag(fp, l)))
1072 goto cat(checkpoint_exit_,ARG_MODE);
1076 /*debug("running optimized code: %s, %p, %x, %p, %p, %p, %u, %p", da(fn,function)->function_name, da(codegen,codegen)->unoptimized_code_base, id, da(codegen,codegen)->unoptimized_code[id], fp, &cg_upcall_vector, ts, da(codegen,codegen)->unoptimized_code[id]);*/
1077 /*debug("fp[0] = %lx", *(long *)((char *)fp + 0));
1078 debug("fp[8] = %lx", *(long *)((char *)fp + 8));
1079 debug("fp[16] = %lx", *(long *)((char *)fp + 16));
1080 debug("fp[24] = %lx", *(long *)((char *)fp + 24));
1081 debug("fp[32] = %lx", *(long *)((char *)fp + 32));
1082 debug("fp[40] = %lx", *(long *)((char *)fp + 40));*/
1084 uintptr_t *stub = (void *)da(codegen,codegen)->unoptimized_code[id];
1085 debug("entry: %p, %lx %lx %lx %lx", stub, stub[0], stub[1], stub[2], stub[3]);
1087 /*__asm__ volatile("nopr %r6");*/
1088 /*__asm__ volatile("xnop");*/
1089 /*__asm__ volatile("cover");*/
1090 /*debug("calling: %p, %p, %lx, %lx", da(codegen,codegen)->unoptimized_code[id], codegen_entry, ((long *)codegen_entry)[0], ((long *)codegen_entry)[1]);*/
1091 /*debug("calling: %p, %p, %lx, %lx", da(codegen,codegen)->unoptimized_code[id], codegen_entry, ((long *)((char *)codegen_entry - 2))[0], ((long *)((char *)codegen_entry - 2))[1]);*/
1092 r = codegen_entry(fp, &cg_upcall_vector, ts, da(codegen,codegen)->unoptimized_code[id]);
1093 #if defined(ARCH_X86_32) || defined(ARCH_ARM32) || defined(ARCH_MIPS32) || defined(ARCH_POWER32) || defined(ARCH_SPARC32)
1094 #if defined(C_LITTLE_ENDIAN) || defined(ARCH_MIPS_N32)
1095 new_fp = num_to_ptr(r & 0xffffffffU);
1098 new_fp = num_to_ptr(r >> 32);
1099 new_ip = r & 0xffffffffU;
1105 /*debug("ran optimized code: %s, %p, %lx -> %p, %x", da(fn,function)->function_name, fp, (unsigned long)(ip - da(fn,function)->code), new_fp, new_ip);*/
1106 if (unlikely(new_ip == (ip_t)-1)) {
1107 /*debug("reload on optimized code: %p", new_fp);*/
1108 RELOAD_EX_POSITION(new_fp);
1110 /*if (fp != new_fp) debug("switching function: %s -> %s", da(fn,function)->function_name, da(get_frame(new_fp)->function,function)->function_name);*/
1112 ip = da(get_frame(fp)->function,function)->code + new_ip;
1113 if (unlikely(profiling_escapes)) {
1114 profile_counter_t profiling_counter;
1115 fn = get_frame(fp)->function;
1116 profiling_counter = load_relaxed(&da(fn,function)->escape_data[new_ip].counter);
1117 profiling_counter++;
1118 store_relaxed(&da(fn,function)->escape_data[new_ip].counter, profiling_counter);
1122 goto cat(checkpoint_exit_,ARG_MODE);
1123 cat(checkpoint_exit_,ARG_MODE):;
1126 DEFINE_OPCODE_END(OPCODE_CHECKPOINT)
1130 DEFINE_OPCODE_START(OPCODE_JMP)
1132 if (SIZEOF_IP_T == 2) {
1133 int16_t offset = ip[1];
1135 ip = cast_ptr(const code_t *, cast_ptr(const char *, ip) + offset);
1136 } else if (SIZEOF_IP_T == 4) {
1137 int32_t offset = get_unaligned_32(&ip[1]);
1139 ip = cast_ptr(const code_t *, cast_ptr(const char *, ip) + offset);
1144 DEFINE_OPCODE_END(OPCODE_JMP)
1147 #if ARG_MODE == 0 && SIZEOF_IP_T > 2
1148 DEFINE_OPCODE_START(OPCODE_JMP_BACK_16)
1153 ip = cast_ptr(const code_t *, cast_ptr(const char *, ip) - offset);
1155 DEFINE_OPCODE_END(OPCODE_JMP_BACK_16)
1158 DEFINE_OPCODE_START(OPCODE_JMP_FALSE)
1162 ajla_flat_option_t val1;
1164 slot = get_i_param(0);
1165 if (unlikely(frame_test_flag(fp, slot))) {
1166 void *ex = thunk_bool_jump(fp, ip, slot);
1167 if (ex != POINTER_FOLLOW_THUNK_RETRY) {
1168 if (likely(ex != POINTER_FOLLOW_THUNK_EXCEPTION)) {
1169 RELOAD_EX_POSITION(ex);
1171 if (SIZEOF_IP_T == 2) {
1172 offset = ip[1 + param_size(1) + 1];
1175 } else if (SIZEOF_IP_T == 4) {
1176 offset = get_unaligned_32(&ip[1 + param_size(1) + 2]);
1182 ip = cast_ptr(const code_t *, cast_ptr(const char *, ip) + offset);
1183 goto cat(jmp_false_exception_,ARG_MODE);
1188 val1 = *frame_slot(fp, slot, ajla_flat_option_t);
1190 if (SIZEOF_IP_T == 2) {
1191 offset = ip[1 + param_size(1)];
1194 } else if (SIZEOF_IP_T == 4) {
1195 offset = get_unaligned_32(&ip[1 + param_size(1)]);
1202 ip = cast_ptr(const code_t *, cast_ptr(const char *, ip) + offset);
1204 cat(jmp_false_exception_,ARG_MODE):;
1206 DEFINE_OPCODE_END(OPCODE_JMP_FALSE)
1209 DEFINE_OPCODE_START(OPCODE_LABEL)
1213 DEFINE_OPCODE_END(OPCODE_LABEL)
1218 pointer_t *direct_data;
1219 pointer_t indirect_data;
1221 frame_t result_slot;
1222 struct thunk *function_error;
1225 DEFINE_OPCODE_START(OPCODE_LOAD_FN)
1227 struct data *new_reference;
1228 pointer_t result_ptr;
1230 n_arguments = get_i_param(0);
1231 result_slot = get_i_param(1);
1232 direct_data = da(get_frame(fp)->function,function)->local_directory[get_i_param(2)];
1233 indirect_data = pointer_empty(); /* avoid warning */
1237 cat(fn_copy_arguments_,ARG_MODE):
1239 new_reference = data_alloc_function_reference_mayfail(n_arguments, &ajla_error pass_file_line);
1240 if (unlikely(!new_reference)) {
1241 function_error = thunk_alloc_exception_error(ajla_error, NULL, fp, ip pass_file_line);
1243 pointer_dereference(indirect_data);
1244 /*cat(fn_set_error_,ARG_MODE):*/
1245 ip = cat(free_parameters_,ARG_MODE)(fp, ip, n_arguments);
1246 result_ptr = pointer_thunk(function_error);
1250 da(new_reference,function_reference)->is_indirect = false;
1251 da(new_reference,function_reference)->u.direct = direct_data;
1253 da(new_reference,function_reference)->is_indirect = true;
1254 da(new_reference,function_reference)->u.indirect = indirect_data;
1256 for (i = 0; i < n_arguments; i++) {
1257 ipret_fill_function_reference_from_slot(new_reference, i, fp, get_param(ip, 0), (get_param(ip, 1) & OPCODE_FLAG_FREE_ARGUMENT) != 0);
1258 ADVANCE_IP(param_size(2));
1260 result_ptr = pointer_data(new_reference);
1263 frame_set_pointer(fp, result_slot, result_ptr);
1265 DEFINE_OPCODE_END(OPCODE_LOAD_FN)
1267 DEFINE_OPCODE_START(OPCODE_CURRY)
1269 frame_t fn_ref_slot;
1271 n_arguments = get_i_param(0);
1272 result_slot = get_i_param(1);
1274 fn_ref_slot = get_i_param(2);
1276 indirect_data = frame_get_pointer_reference(fp, fn_ref_slot, (get_i_param(3) & OPCODE_FLAG_FREE_ARGUMENT) != 0);
1280 goto cat(fn_copy_arguments_,ARG_MODE);
1282 DEFINE_OPCODE_END(OPCODE_CURRY)
1288 arg_t n_return_values;
1289 struct thunk *function_error;
1290 struct data *function;
1291 const code_t *orig_ip;
1294 pointer_t *direct_function;
1295 pointer_t indirect_function;
1298 DEFINE_OPCODE_START(OPCODE_CALL)
1300 pointer_t *function_ptr;
1302 mode = CALL_MODE_NORMAL;
1303 cat(call_from_mode_,ARG_MODE):
1305 n_arguments = get_i_param(0);
1306 n_return_values = get_i_param(1);
1307 function_ptr = da(get_frame(fp)->function,function)->local_directory[get_i_param(2)];
1309 pointer_follow(function_ptr, false, function, PF_WAIT, fp, ip,
1310 RELOAD_EX_POSITION(ex_),
1311 function_error = thunk_;
1312 thunk_reference(function_error);
1314 goto cat(call_set_error_,ARG_MODE);
1317 ajla_assert(da(function,function)->n_arguments == n_arguments && da(function,function)->n_return_values == n_return_values,
1318 (file_line, "call %s->%s: the number of arguments does not match: %lu != %lu || %lu != %lu",
1319 da(get_frame(fp)->function,function)->function_name, da(function,function)->function_name,
1320 (unsigned long)da(function,function)->n_arguments, (unsigned long)n_arguments,
1321 (unsigned long)da(function,function)->n_return_values, (unsigned long)n_return_values));
1325 new_fp = frame_build(fp, function, &ajla_error);
1326 if (unlikely(!new_fp)) {
1327 cat(call_allocation_error_,ARG_MODE):
1328 function_error = thunk_alloc_exception_error(ajla_error, NULL, fp, ip pass_file_line);
1329 cat(call_set_error_,ARG_MODE):
1330 ip = cat(free_parameters_,ARG_MODE)(fp, ip, n_arguments);
1331 pointer_reference_owned_multiple(pointer_thunk(function_error), n_return_values - 1);
1332 i = n_return_values;
1334 frame_t slot_r = get_max_param(ip, 0);
1335 ADVANCE_IP(max_param_size(1) + 1);
1336 frame_set_pointer(fp, slot_r, pointer_thunk(function_error));
1341 fp = frame_up(new_fp);
1342 frame_init(new_fp, function, get_frame(fp)->timestamp, mode);
1345 cat(call_copy_arguments_,ARG_MODE):
1346 for (; i < da(function,function)->n_arguments; i++) {
1347 frame_t src_slot = get_param(ip, 0);
1348 frame_t dst_slot = da(function,function)->args[i].slot;
1349 unsigned char src_flag = (unsigned char)get_param(ip, 1);
1350 bool may_be_borrowed = da(function,function)->args[i].may_be_borrowed;
1351 if (may_be_borrowed && src_flag & OPCODE_CALL_MAY_LEND && !pointer_is_thunk(*frame_pointer(fp, src_slot))) {
1352 *frame_pointer(new_fp, dst_slot) = *frame_pointer(fp, src_slot);
1353 } else if (may_be_borrowed && src_flag & OPCODE_CALL_MAY_GIVE && !frame_test_flag(fp, src_slot) && !pointer_is_thunk(*frame_pointer(fp, src_slot))) {
1354 *frame_pointer(new_fp, dst_slot) = *frame_pointer(fp, src_slot);
1355 *frame_pointer(fp, src_slot) = pointer_empty();
1357 ipret_copy_variable(fp, src_slot, new_fp, dst_slot, (src_flag & OPCODE_FLAG_FREE_ARGUMENT) != 0);
1359 ADVANCE_IP(param_size(2));
1361 get_frame(new_fp)->previous_ip = frame_ip(fp, ip);
1364 ip = &da(function,function)->code[0];
1366 if (unlikely(mode == CALL_MODE_SPARK)) {
1367 ipret_checkpoint_forced;
1371 DEFINE_OPCODE_END(OPCODE_CALL)
1373 DEFINE_OPCODE_START(OPCODE_CALL_STRICT)
1375 mode = CALL_MODE_STRICT;
1376 goto cat(call_from_mode_,ARG_MODE);
1378 DEFINE_OPCODE_END(OPCODE_CALL_STRICT)
1380 DEFINE_OPCODE_START(OPCODE_CALL_SPARK)
1382 mode = CALL_MODE_SPARK;
1383 goto cat(call_from_mode_,ARG_MODE);
1385 DEFINE_OPCODE_END(OPCODE_CALL_SPARK)
1387 DEFINE_OPCODE_START(OPCODE_CALL_INDIRECT)
1389 frame_t fn_ref_slot;
1393 mode = CALL_MODE_NORMAL;
1394 cat(call_indirect_from_mode_,ARG_MODE):
1397 n_arguments = get_i_param(0);
1398 n_return_values = get_i_param(1);
1399 fn_ref_slot = get_i_param(2);
1400 deref = (get_i_param(3) & OPCODE_FLAG_FREE_ARGUMENT) != 0;
1403 ptr = frame_pointer(fp, fn_ref_slot);
1405 pointer_follow(ptr, false, function, PF_WAIT, fp, orig_ip,
1406 RELOAD_EX_POSITION(ex_),
1407 function_error = thunk_;
1408 thunk_reference(function_error);
1410 frame_free_and_clear(fp, fn_ref_slot);
1411 goto cat(call_set_error_,ARG_MODE)
1413 if (!da(function,function_reference)->is_indirect)
1415 ptr = &da(function,function_reference)->u.indirect;
1417 ptr = da(function,function_reference)->u.direct;
1418 pointer_follow(ptr, false, function, PF_WAIT, fp, orig_ip,
1419 RELOAD_EX_POSITION(ex_),
1420 function_error = thunk_;
1421 thunk_reference(function_error);
1423 frame_free_and_clear(fp, fn_ref_slot);
1424 goto cat(call_set_error_,ARG_MODE)
1427 new_fp = frame_build(fp, function, &ajla_error);
1428 if (unlikely(!new_fp)) {
1430 frame_free_and_clear(fp, fn_ref_slot);
1431 goto cat(call_allocation_error_,ARG_MODE);
1433 fp = frame_up(new_fp);
1434 frame_init(new_fp, function, get_frame(fp)->timestamp, mode);
1436 i = da(function,function)->n_arguments - n_arguments;
1438 copy_from_function_reference_to_frame(new_fp, pointer_get_data(*frame_pointer(fp, fn_ref_slot)), i, deref && frame_test_flag(fp, fn_ref_slot));
1440 frame_free_and_clear(fp, fn_ref_slot);
1442 goto cat(call_copy_arguments_,ARG_MODE);
1445 DEFINE_OPCODE_END(OPCODE_CALL_INDIRECT)
1447 DEFINE_OPCODE_START(OPCODE_CALL_INDIRECT_STRICT)
1449 mode = CALL_MODE_STRICT;
1450 goto cat(call_indirect_from_mode_,ARG_MODE);
1452 DEFINE_OPCODE_END(OPCODE_CALL_INDIRECT_STRICT)
1454 DEFINE_OPCODE_START(OPCODE_CALL_INDIRECT_SPARK)
1456 mode = CALL_MODE_SPARK;
1457 goto cat(call_indirect_from_mode_,ARG_MODE);
1459 DEFINE_OPCODE_END(OPCODE_CALL_INDIRECT_SPARK)
1461 DEFINE_OPCODE_START(OPCODE_CALL_LAZY)
1463 struct data *function_reference;
1464 struct thunk **results;
1466 n_arguments = get_i_param(0);
1467 n_return_values = get_i_param(1);
1468 direct_function = da(get_frame(fp)->function,function)->local_directory[get_i_param(2)];
1469 indirect_function = pointer_empty(); /* avoid warning */
1473 cat(call_lazy_copy_arguments_,ARG_MODE):
1474 results = mem_alloc_array_mayfail(mem_alloc_mayfail, struct thunk **, 0, 0, n_return_values, sizeof(struct thunk *), &ajla_error);
1475 if (unlikely(!results)) {
1476 if (!direct_function)
1477 pointer_dereference(indirect_function);
1478 goto cat(call_allocation_error_,ARG_MODE);
1481 if (!(function_reference = data_alloc_function_reference_mayfail(n_arguments, &ajla_error pass_file_line))) {
1482 if (!direct_function)
1483 pointer_dereference(indirect_function);
1485 goto cat(call_allocation_error_,ARG_MODE);
1487 if (direct_function) {
1488 da(function_reference,function_reference)->is_indirect = false;
1489 da(function_reference,function_reference)->u.direct = direct_function;
1491 da(function_reference,function_reference)->is_indirect = true;
1492 da(function_reference,function_reference)->u.indirect = indirect_function;
1495 if (!thunk_alloc_function_call(pointer_data(function_reference), n_return_values, results, &ajla_error)) {
1496 if (!direct_function)
1497 pointer_dereference(indirect_function);
1499 data_free_r1(function_reference);
1500 goto cat(call_allocation_error_,ARG_MODE);
1503 for (i = 0; i < n_arguments; i++) {
1504 frame_t src_slot = get_param(ip, 0);
1505 unsigned char src_flag = (unsigned char)get_param(ip, 1);
1506 ipret_fill_function_reference_from_slot(function_reference, i, fp, src_slot, (src_flag & OPCODE_FLAG_FREE_ARGUMENT) != 0);
1507 ADVANCE_IP(param_size(2));
1510 for (i = 0; i < n_return_values; i++) {
1511 frame_t slot_r = get_max_param(ip, 0);
1512 frame_set_pointer(fp, slot_r, pointer_thunk(results[i]));
1513 ADVANCE_IP(max_param_size(1) + 1);
1518 DEFINE_OPCODE_END(OPCODE_CALL_LAZY)
1520 DEFINE_OPCODE_START(OPCODE_CALL_INDIRECT_LAZY)
1522 frame_t fn_ref_slot;
1525 n_arguments = get_i_param(0);
1526 n_return_values = get_i_param(1);
1527 fn_ref_slot = get_i_param(2);
1528 deref = (get_i_param(3) & OPCODE_FLAG_FREE_ARGUMENT) != 0;
1531 indirect_function = frame_get_pointer_reference(fp, fn_ref_slot, deref);
1532 direct_function = NULL;
1534 goto cat(call_lazy_copy_arguments_,ARG_MODE);
1536 DEFINE_OPCODE_END(OPCODE_CALL_INDIRECT_LAZY)
1538 DEFINE_OPCODE_START(OPCODE_CALL_CACHE)
1540 struct ipret_call_cache_arg *arguments;
1541 frame_t *return_values;
1544 cat(call_cache_,ARG_MODE):
1546 n_arguments = get_i_param(0);
1547 n_return_values = get_i_param(1);
1548 direct_function = da(get_frame(fp)->function,function)->local_directory[get_i_param(2)];
1551 pointer_follow(direct_function, false, function, PF_WAIT, fp, orig_ip,
1552 RELOAD_EX_POSITION(ex_),
1553 function_error = thunk_;
1554 thunk_reference(function_error);
1555 goto cat(call_set_error_,ARG_MODE);
1557 ajla_assert(da(function,function)->n_arguments == n_arguments && da(function,function)->n_return_values == n_return_values,
1558 (file_line, "call_cache: the number of arguments does not match: %lu != %lu || %lu != %lu",
1559 (unsigned long)da(function,function)->n_arguments, (unsigned long)n_arguments,
1560 (unsigned long)da(function,function)->n_return_values, (unsigned long)n_return_values));
1562 arguments = mem_alloc_array_mayfail(mem_alloc_mayfail, struct ipret_call_cache_arg *, 0, 0, n_arguments, sizeof(struct ipret_call_cache_arg), &ajla_error);
1564 goto cat(call_allocation_error_,ARG_MODE);
1566 return_values = mem_alloc_array_mayfail(mem_alloc_mayfail, frame_t *, 0, 0, n_return_values, sizeof(frame_t), &ajla_error);
1567 if (!return_values) {
1568 mem_free(arguments);
1569 goto cat(call_allocation_error_,ARG_MODE);
1572 for (i = 0; i < n_arguments; i++) {
1573 frame_t src_slot = get_param(ip, 0);
1574 unsigned char src_flag = (unsigned char)get_param(ip, 1);
1575 arguments[i].f_arg = NULL;
1576 arguments[i].slot = src_slot;
1577 arguments[i].deref = (src_flag & OPCODE_FLAG_FREE_ARGUMENT) != 0;
1578 ADVANCE_IP(param_size(2));
1581 for (i = 0; i < n_return_values; i++) {
1582 frame_t slot_r = get_max_param(ip, 0);
1583 return_values[i] = slot_r;
1584 ADVANCE_IP(max_param_size(1) + 1);
1587 ex = ipret_call_cache(fp, orig_ip, direct_function, arguments, return_values, NO_FRAME_T);
1588 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO)) {
1589 RELOAD_EX_POSITION(ex);
1592 DEFINE_OPCODE_END(OPCODE_CALL_CACHE)
1594 DEFINE_OPCODE_START(OPCODE_CALL_SAVE)
1596 goto cat(call_cache_,ARG_MODE);
1598 DEFINE_OPCODE_END(OPCODE_CALL_SAVE)
1600 DEFINE_OPCODE_START(OPCODE_CALL_INDIRECT_CACHE)
1602 struct ipret_call_cache_arg *arguments;
1603 frame_t *return_values;
1606 frame_t fn_ref_slot;
1609 arg_t n_curried_arguments;
1611 cat(call_indirect_cache_,ARG_MODE):
1613 n_arguments = get_i_param(0);
1614 n_return_values = get_i_param(1);
1615 fn_ref_slot = get_i_param(2);
1616 deref = (get_i_param(3) & OPCODE_FLAG_FREE_ARGUMENT) != 0;
1619 n_curried_arguments = 0;
1620 ptr = frame_pointer(fp, fn_ref_slot);
1622 pointer_follow(ptr, false, function, PF_WAIT, fp, orig_ip,
1623 RELOAD_EX_POSITION(ex_),
1624 function_error = thunk_;
1625 thunk_reference(function_error);
1627 frame_free_and_clear(fp, fn_ref_slot);
1628 goto cat(call_set_error_,ARG_MODE)
1630 n_curried_arguments += da(function,function_reference)->n_curried_arguments;
1631 if (!da(function,function_reference)->is_indirect)
1633 ptr = &da(function,function_reference)->u.indirect;
1635 ptr = da(function,function_reference)->u.direct;
1636 pointer_follow(ptr, false, function, PF_WAIT, fp, orig_ip,
1637 RELOAD_EX_POSITION(ex_),
1638 function_error = thunk_;
1639 thunk_reference(function_error);
1641 frame_free_and_clear(fp, fn_ref_slot);
1642 goto cat(call_set_error_,ARG_MODE)
1645 arguments = mem_alloc_array_mayfail(mem_alloc_mayfail, struct ipret_call_cache_arg *, 0, 0, n_curried_arguments + n_arguments, sizeof(struct ipret_call_cache_arg), &ajla_error);
1648 frame_free_and_clear(fp, fn_ref_slot);
1649 goto cat(call_allocation_error_,ARG_MODE);
1651 return_values = mem_alloc_array_mayfail(mem_alloc_mayfail, frame_t *, 0, 0, n_return_values, sizeof(frame_t), &ajla_error);
1652 if (!return_values) {
1653 mem_free(arguments);
1655 frame_free_and_clear(fp, fn_ref_slot);
1656 goto cat(call_allocation_error_,ARG_MODE);
1659 i = n_curried_arguments;
1660 function = pointer_get_data(*frame_pointer(fp, fn_ref_slot));
1663 j = da(function,function_reference)->n_curried_arguments;
1666 arguments[i].f_arg = &da(function,function_reference)->arguments[j];
1667 arguments[i].deref = false;
1669 if (!da(function,function_reference)->is_indirect)
1671 function = pointer_get_data(da(function,function_reference)->u.indirect);
1674 for (i = n_curried_arguments; i < n_curried_arguments + n_arguments; i++) {
1675 frame_t src_slot = get_param(ip, 0);
1676 unsigned char src_flag = (unsigned char)get_param(ip, 1);
1677 arguments[i].f_arg = NULL;
1678 arguments[i].slot = src_slot;
1679 arguments[i].deref = (src_flag & OPCODE_FLAG_FREE_ARGUMENT) != 0;
1680 ADVANCE_IP(param_size(2));
1683 for (i = 0; i < n_return_values; i++) {
1684 frame_t slot_r = get_max_param(ip, 0);
1685 return_values[i] = slot_r;
1686 ADVANCE_IP(max_param_size(1) + 1);
1689 ex = ipret_call_cache(fp, orig_ip, ptr, arguments, return_values, deref ? fn_ref_slot : NO_FRAME_T);
1690 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO)) {
1691 RELOAD_EX_POSITION(ex);
1694 DEFINE_OPCODE_END(OPCODE_CALL_INDIRECT_CACHE)
1696 DEFINE_OPCODE_START(OPCODE_CALL_INDIRECT_SAVE)
1698 goto cat(call_indirect_cache_,ARG_MODE);
1700 DEFINE_OPCODE_END(OPCODE_CALL_INDIRECT_SAVE)
1704 DEFINE_OPCODE_START(OPCODE_RETURN)
1706 struct data *function;
1707 frame_s *previous_fp;
1708 const code_t *previous_ip;
1711 /*ADVANCE_I_PARAM(0);*/
1713 function = get_frame(fp)->function;
1714 previous_fp = frame_up(fp);
1715 if (unlikely(frame_is_top(previous_fp))) {
1716 struct execution_control *ex;
1719 struct stack_bottom *sb = frame_stack_bottom(previous_fp);
1722 frame_t src_slot = get_i_param(0);
1723 sb->ret = ipret_copy_variable_to_pointer(fp, src_slot, true);
1727 n = da(function,function)->n_return_values;
1730 frame_t src_slot = get_i_param(i * 2);
1731 frame_t flags = get_i_param(i * 2 + 1);
1732 t->u.function_call.results[i].ptr = ipret_copy_variable_to_pointer(fp, src_slot, (flags & OPCODE_FLAG_FREE_ARGUMENT) != 0);
1734 exx = thunk_terminate(t, n);
1736 if (exx != POINTER_FOLLOW_THUNK_EXIT)
1737 task_submit(exx, true);
1740 RELOAD_EX_POSITION(exx);
1744 get_frame(previous_fp)->timestamp = get_frame(fp)->timestamp;
1745 previous_ip = &da(get_frame(previous_fp)->function,function)->code[get_frame(fp)->previous_ip];
1747 n = da(function,function)->n_return_values;
1750 frame_t src_slot, dst_slot, flags;
1751 src_slot = get_i_param(i * 2);
1752 flags = get_i_param(i * 2 + 1);
1753 dst_slot = (frame_t)get_max_param(previous_ip, 0);
1754 previous_ip += max_param_size(1) + 1;
1755 ipret_copy_variable(fp, src_slot, previous_fp, dst_slot, (flags & OPCODE_FLAG_FREE_ARGUMENT) != 0);
1761 DEFINE_OPCODE_END(OPCODE_RETURN)
1764 DEFINE_OPCODE_START(OPCODE_STRUCTURED)
1766 const code_t *orig_ip = ip;
1768 frame_t struc, elem;
1769 pointer_t *struc_ptr;
1770 unsigned char *struc_flat;
1771 const struct type *t;
1772 bool optimize_elem_is_flat;
1777 pointer_t thunk_ptr;
1779 elem = get_i_param(1);
1780 optimize_elem_is_flat = frame_variable_is_flat(fp, elem);
1782 cat(structured_retry_,ARG_MODE):
1783 struc = get_i_param(0);
1786 t = frame_get_type_of_local(fp, struc);
1787 if (frame_variable_is_flat(fp, struc)) {
1788 if (!optimize_elem_is_flat) {
1789 frame_set_pointer(fp, struc, flat_to_data(frame_get_type_of_local(fp, struc), frame_var(fp, struc)));
1791 struc_ptr = frame_pointer(fp, struc);
1793 struc_flat = frame_var(fp, struc);
1794 struc_ptr = NULL; /* avoid warning */
1798 struc_ptr = frame_pointer(fp, struc);
1802 array_index_t array_index = index_invalid(); /* avoid warning */
1803 struct data *data = NULL; /* avoid warning */
1805 type = (unsigned char)get_param(ip, 0);
1806 arg = get_param(ip, 1);
1807 ADVANCE_IP(param_size(3));
1809 if ((type & OPCODE_STRUCTURED_MASK) == OPCODE_STRUCTURED_ARRAY) {
1810 void *ex = ipret_get_index(fp, orig_ip, fp, arg, NULL, &array_index, &thunk_ptr pass_file_line);
1811 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO)) {
1812 if (ex == POINTER_FOLLOW_THUNK_EXCEPTION)
1813 goto cat(structured_error_,ARG_MODE);
1814 RELOAD_EX_POSITION(ex);
1819 switch (type & OPCODE_STRUCTURED_MASK) {
1820 case OPCODE_STRUCTURED_RECORD: {
1821 const struct flat_record_definition_entry *e;
1822 ajla_assert(arg < flat_record_n_slots(type_def(t,flat_record)), (file_line, "structured flat record: invalid index: %"PRIuMAX" >= %"PRIuMAX"", (uintmax_t)arg, (uintmax_t)flat_record_n_slots(type_def(t,flat_record))));
1823 e = &type_def(t,flat_record)->entries[arg];
1825 struc_flat += e->flat_offset;
1828 case OPCODE_STRUCTURED_OPTION: {
1829 thunk_ptr = pointer_error(error_ajla(EC_SYNC, AJLA_ERROR_OPTION_DOESNT_MATCH), fp, ip pass_file_line);
1830 goto cat(structured_error_,ARG_MODE);
1832 case OPCODE_STRUCTURED_ARRAY: {
1833 if (unlikely(index_ge_int(array_index, type_def(t,flat_array)->n_elements))) {
1834 index_free(&array_index);
1835 thunk_ptr = pointer_error(error_ajla(EC_SYNC, AJLA_ERROR_INDEX_OUT_OF_RANGE), fp, ip pass_file_line);
1836 goto cat(structured_error_,ARG_MODE);
1838 t = type_def(t,flat_array)->base;
1839 struc_flat += t->size * index_to_int(array_index);
1840 index_free(&array_index);
1844 internal(file_line, "structured flat: invalid structure type %02x", type);
1847 cat(structured_restart_pointer_follow_,ARG_MODE):
1848 pointer_follow(struc_ptr, false, data, PF_WAIT, fp, orig_ip,
1849 if ((type & OPCODE_STRUCTURED_MASK) == OPCODE_STRUCTURED_ARRAY)
1850 index_free(&array_index);
1851 RELOAD_EX_POSITION(ex_),
1852 if ((type & OPCODE_STRUCTURED_MASK) == OPCODE_STRUCTURED_ARRAY)
1853 index_free(&array_index);
1854 goto cat(structured_read_to_the_end_,ARG_MODE);
1856 if (unlikely(!data_is_writable(data))) {
1857 struct_clone(struc_ptr);
1858 goto cat(structured_restart_pointer_follow_,ARG_MODE);
1860 switch (type & OPCODE_STRUCTURED_MASK) {
1861 case OPCODE_STRUCTURED_RECORD: {
1862 const struct record_definition *def = type_def(da(data,record)->definition,record);
1863 ajla_assert(arg < def->n_slots, (file_line, "structured record: invalid index: %"PRIuMAX" >= %"PRIuMAX"", (uintmax_t)arg, (uintmax_t)def->n_slots));
1864 t = def->types[arg];
1865 struc_ptr = frame_pointer(da_record_frame(data), arg);
1867 if (type & OPCODE_STRUCTURED_FLAG_END) {
1868 if (frame_variable_is_flat(fp, elem)) {
1869 if (TYPE_IS_FLAT(t)) {
1870 frame_free(da_record_frame(data), arg);
1871 struc_flat = frame_var(da_record_frame(data), arg);
1872 goto cat(structured_write_flat_,ARG_MODE);
1875 if (!frame_test_and_set_flag(da_record_frame(data), arg))
1876 goto cat(structured_write_ptr_,ARG_MODE);
1880 if (!frame_test_flag(da_record_frame(data), arg)) {
1881 struc_flat = frame_var(da_record_frame(data), arg);
1882 if (unlikely(!optimize_elem_is_flat)) {
1883 frame_set_pointer(da_record_frame(data), arg, flat_to_data(t, struc_flat));
1889 case OPCODE_STRUCTURED_OPTION: {
1890 if (unlikely(da(data,option)->option != arg)) {
1891 if (likely((type & OPCODE_STRUCTURED_FLAG_END) != 0)) {
1892 da(data,option)->option = arg;
1894 thunk_ptr = pointer_error(error_ajla(EC_SYNC, AJLA_ERROR_OPTION_DOESNT_MATCH), fp, ip pass_file_line);
1895 cat(structured_error_,ARG_MODE):
1897 pointer_dereference(thunk_ptr);
1898 optimize_elem_is_flat = false;
1900 goto cat(structured_retry_,ARG_MODE);
1902 pointer_dereference(*struc_ptr);
1903 *struc_ptr = thunk_ptr;
1904 goto cat(structured_read_to_the_end_,ARG_MODE);
1908 struc_ptr = &da(data,option)->pointer;
1911 case OPCODE_STRUCTURED_ARRAY: {
1912 unsigned array_flags;
1913 if (unlikely(da_tag(data) == DATA_TAG_array_incomplete)) {
1914 array_index_t len_first;
1916 if (array_incomplete_collapse(struc_ptr))
1917 goto cat(structured_restart_pointer_follow_,ARG_MODE);
1919 len_first = array_len(pointer_get_data(da(data,array_incomplete)->first));
1920 if (!index_ge_index(array_index, len_first)) {
1921 index_free(&len_first);
1922 struc_ptr = &da(data,array_incomplete)->first;
1923 goto cat(structured_restart_pointer_follow_,ARG_MODE);
1925 index_sub(&array_index, len_first);
1926 index_free(&len_first);
1927 struc_ptr = &da(data,array_incomplete)->next;
1928 goto cat(structured_restart_pointer_follow_,ARG_MODE);
1931 if (!optimize_elem_is_flat) {
1932 array_flags = ARRAY_MODIFY_NEED_PTR;
1933 } else if (type & OPCODE_STRUCTURED_FLAG_END) {
1934 array_flags = ARRAY_MODIFY_NEED_FLAT;
1935 t = frame_get_type_of_local(fp, elem);
1939 if (unlikely(!array_modify(struc_ptr, array_index, array_flags, &struc_ptr, &struc_flat, &t, fp, ip))) {
1940 goto cat(structured_read_to_the_end_,ARG_MODE);
1945 internal(file_line, "structured: invalid structure type %02x", type);
1948 } while (!(type & OPCODE_STRUCTURED_FLAG_END));
1950 cat(structured_write_flat_,ARG_MODE):
1951 memcpy_fast(struc_flat, frame_var(fp, elem), frame_get_type_of_local(fp, elem)->size);
1953 /* The pointer may be empty if we moved from an option without data to an option with data */
1954 if (likely(!pointer_is_empty(*struc_ptr)))
1955 pointer_dereference(*struc_ptr);
1956 if (frame_variable_is_flat(fp, elem)) {
1957 *struc_ptr = flat_to_data(frame_get_type_of_local(fp, elem), frame_var(fp, elem));
1959 cat(structured_write_ptr_,ARG_MODE):
1960 *struc_ptr = frame_get_pointer_reference(fp, elem, (type & OPCODE_STRUCTURED_FREE_VARIABLE) != 0);
1965 type = (unsigned char)get_param(ip, 0);
1966 arg = get_param(ip, 1);
1967 ADVANCE_IP(param_size(3));
1968 cat(structured_read_to_the_end_,ARG_MODE):;
1969 } while (!(type & OPCODE_STRUCTURED_FLAG_END));
1970 if (type & OPCODE_STRUCTURED_FREE_VARIABLE)
1971 frame_free(fp, elem);
1974 DEFINE_OPCODE_END(OPCODE_STRUCTURED)
1976 DEFINE_OPCODE_START(OPCODE_RECORD_CREATE)
1978 frame_t result_slot;
1979 arg_t n_entries, i, ii;
1980 const struct type *t;
1981 const struct record_definition *def;
1983 result_slot = get_i_param(0);
1984 n_entries = get_i_param(1);
1987 t = frame_get_type_of_local(fp, result_slot);
1988 if (t->tag == TYPE_TAG_flat_record) {
1989 const code_t *backup_ip;
1990 def = type_def(type_def(t,flat_record)->base,record);
1992 for (i = 0, ii = 0; i < n_entries; i++, ii++) {
1993 frame_t var_slot, record_slot;
1994 flat_size_t flat_offset;
1995 const struct type *flat_type;
1997 while (unlikely(record_definition_is_elided(def, ii)))
2000 var_slot = get_param(ip, 0);
2002 if (unlikely(!frame_variable_is_flat(fp, var_slot))) {
2004 goto cat(create_record_no_flat_,ARG_MODE);
2006 record_slot = record_definition_slot(def, ii);
2007 flat_offset = type_def(t,flat_record)->entries[record_slot].flat_offset;
2008 flat_type = type_def(t,flat_record)->entries[record_slot].subtype;
2009 ajla_assert(type_is_equal(frame_get_type_of_local(fp, var_slot), flat_type), (file_line, "record create (flat): copying between different types (%u,%u,%u) -> (%u,%u,%u)", frame_get_type_of_local(fp, var_slot)->tag, frame_get_type_of_local(fp, var_slot)->size, frame_get_type_of_local(fp, var_slot)->align, flat_type->tag, flat_type->size, flat_type->align));
2010 memcpy_fast(frame_var(fp, result_slot) + flat_offset, frame_var(fp, var_slot), flat_type->size);
2011 ADVANCE_IP(param_size(2));
2013 ajla_assert(!frame_test_flag(fp, result_slot), (file_line, "record create (flat): flag already set for destination slot %"PRIuMAX"", (uintmax_t)result_slot));
2015 struct data *result;
2016 def = type_def(t,record);
2017 cat(create_record_no_flat_,ARG_MODE):
2018 result = data_alloc_record_mayfail(def, &ajla_error pass_file_line);
2019 if (unlikely(!result)) {
2020 ip = cat(free_parameters_,ARG_MODE)(fp, ip, n_entries);
2021 frame_set_pointer(fp, result_slot, pointer_error(ajla_error, fp, ip pass_file_line));
2024 memset(da_record_frame(result), 0, bitmap_slots(def->n_slots) * slot_size);
2025 for (i = 0, ii = 0; i < n_entries; i++, ii++) {
2026 frame_t var_slot, record_slot;
2027 const struct type *rec_type, *var_type;
2029 while (unlikely(record_definition_is_elided(def, ii)))
2032 var_slot = get_param(ip, 0);
2033 record_slot = record_definition_slot(def, ii);
2034 rec_type = def->types[record_slot];
2035 var_type = frame_get_type_of_local(fp, var_slot);
2037 if (!frame_variable_is_flat(fp, var_slot)) {
2038 pointer_t ptr = frame_get_pointer_reference(fp, var_slot, (get_param(ip, 1) & OPCODE_FLAG_FREE_ARGUMENT) != 0);
2039 frame_set_pointer(da_record_frame(result), record_slot, ptr);
2040 } else if (TYPE_IS_FLAT(rec_type)) {
2041 ajla_assert(type_is_equal(var_type, rec_type), (file_line, "record create: copying between different types (%u,%u,%u) -> (%u,%u,%u)", var_type->tag, var_type->size, var_type->align, rec_type->tag, rec_type->size, rec_type->align));
2042 memcpy_fast(frame_var(da_record_frame(result), record_slot), frame_var(fp, var_slot), rec_type->size);
2044 pointer_t ptr = flat_to_data(var_type, frame_var(fp, var_slot));
2045 frame_set_pointer(da_record_frame(result), record_slot, ptr);
2048 ADVANCE_IP(param_size(2));
2050 frame_set_pointer(fp, result_slot, pointer_data(result));
2053 DEFINE_OPCODE_END(OPCODE_RECORD_CREATE)
2055 DEFINE_OPCODE_START(OPCODE_RECORD_LOAD)
2057 frame_t record, record_slot, result;
2059 const struct type *t;
2060 record = get_i_param(0);
2061 record_slot = get_i_param(1);
2062 result = get_i_param(2);
2063 flags = get_i_param(3);
2065 t = frame_get_type_of_local(fp, record);
2066 if (t->tag == TYPE_TAG_flat_record && !frame_test_flag(fp, record)) {
2067 const struct flat_record_definition_entry *ft = &type_def(t,flat_record)->entries[record_slot];
2068 if (likely(TYPE_IS_FLAT(frame_get_type_of_local(fp, result)))) {
2069 memcpy_fast(frame_var(fp, result), frame_var(fp, record) + ft->flat_offset, ft->subtype->size);
2071 pointer_t ptr = flat_to_data(ft->subtype, frame_var(fp, record) + ft->flat_offset);
2072 frame_set_pointer(fp, result, ptr);
2075 const struct type *rtype, *etype;
2078 pointer_follow(frame_pointer(fp, record), true, data, unlikely(flags & OPCODE_OP_FLAG_STRICT) ? PF_WAIT : PF_NOEVAL, fp, ip,
2079 if (!(flags & OPCODE_OP_FLAG_STRICT)) {
2080 ex_ = ipret_record_load_create_thunk(fp, ip, record, record_slot, result);
2081 if (ex_ == POINTER_FOLLOW_THUNK_GO)
2082 goto cat(record_load_end_,ARG_MODE);
2084 RELOAD_EX_POSITION(ex_),
2085 thunk_reference(thunk_);
2086 frame_set_pointer(fp, result, pointer_thunk(thunk_));
2087 goto cat(record_load_end_,ARG_MODE);
2089 rtype = da(data,record)->definition;
2090 etype = type_def(rtype,record)->types[record_slot];
2091 fr = da_record_frame(data);
2092 if (!frame_test_flag(fr, record_slot)) {
2093 if (likely(TYPE_IS_FLAT(frame_get_type_of_local(fp, result)))) {
2094 memcpy_fast(frame_var(fp, result), frame_var(fr, record_slot), etype->size);
2096 pointer_t ptr = flat_to_data(etype, frame_var(fr, record_slot));
2097 frame_set_pointer(fp, result, ptr);
2100 pointer_reference_maybe(fp, result, frame_pointer(fr, record_slot), flags);
2104 cat(record_load_end_,ARG_MODE):
2107 DEFINE_OPCODE_END(OPCODE_RECORD_CREATE)
2109 DEFINE_OPCODE_START(OPCODE_OPTION_CREATE_EMPTY_FLAT)
2111 frame_t slot_r = get_i_param(0);
2112 ajla_flat_option_t opt = (ajla_flat_option_t)get_i_param(1);
2113 ajla_assert(!frame_test_flag(fp, slot_r), (file_line, "option create empty flat: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));
2115 *frame_slot(fp, slot_r, ajla_flat_option_t) = opt;
2119 DEFINE_OPCODE_END(OPCODE_OPTION_CREATE_EMPTY_FLAT)
2121 DEFINE_OPCODE_START(OPCODE_OPTION_CREATE_EMPTY)
2126 d = data_alloc(option, &ajla_error);
2127 slot_r = get_i_param(0);
2128 if (likely(d != NULL)) {
2129 da(d,option)->option = get_i_param(1);
2130 da(d,option)->pointer = pointer_empty();
2131 frame_set_pointer(fp, slot_r, pointer_data(d));
2133 frame_set_pointer(fp, slot_r, pointer_error(ajla_error, fp, ip pass_file_line));
2137 DEFINE_OPCODE_END(OPCODE_OPTION_CREATE_EMPTY)
2139 DEFINE_OPCODE_START(OPCODE_OPTION_CREATE)
2142 frame_t slot_1, slot_r;
2145 slot_1 = get_i_param(2);
2146 if (!frame_variable_is_flat(fp, slot_1)) {
2147 ptr = frame_get_pointer_reference(fp, slot_1, (get_i_param(3) & OPCODE_FLAG_FREE_ARGUMENT) != 0);
2149 ptr = flat_to_data(frame_get_type_of_local(fp, slot_1), frame_var(fp, slot_1));
2152 d = data_alloc(option, &ajla_error);
2153 slot_r = get_i_param(0);
2154 if (likely(d != NULL)) {
2155 da(d,option)->option = get_i_param(1);
2156 da(d,option)->pointer = ptr;
2157 frame_set_pointer(fp, slot_r, pointer_data(d));
2159 pointer_dereference(ptr);
2160 frame_set_pointer(fp, slot_r, pointer_error(ajla_error, fp, ip pass_file_line));
2164 DEFINE_OPCODE_END(OPCODE_OPTION_CREATE)
2166 DEFINE_OPCODE_START(OPCODE_OPTION_LOAD)
2168 frame_t option, option_idx, result;
2173 option = get_i_param(0);
2174 option_idx = get_i_param(1);
2175 result = get_i_param(2);
2176 flags = get_i_param(3);
2178 if (unlikely(frame_variable_is_flat(fp, option)))
2179 goto cat(option_load_mismatch,ARG_MODE);
2181 pointer_follow(frame_pointer(fp, option), true, data, unlikely(flags & OPCODE_OP_FLAG_STRICT) ? PF_WAIT : PF_NOEVAL, fp, ip,
2182 if (!(flags & OPCODE_OP_FLAG_STRICT)) {
2183 ex_ = ipret_option_load_create_thunk(fp, ip, option, option_idx, result);
2184 if (ex_ == POINTER_FOLLOW_THUNK_GO)
2185 goto cat(option_load_end_,ARG_MODE);
2187 RELOAD_EX_POSITION(ex_),
2188 thunk_reference(thunk_);
2189 frame_set_pointer(fp, result, pointer_thunk(thunk_));
2190 goto cat(option_load_end_,ARG_MODE);
2193 if (unlikely(da(data,option)->option != option_idx)) {
2194 cat(option_load_mismatch,ARG_MODE):
2195 ptr = pointer_error(error_ajla(EC_SYNC, AJLA_ERROR_OPTION_DOESNT_MATCH), fp, ip pass_file_line);
2196 frame_set_pointer(fp, result, ptr);
2198 pointer_reference_maybe(fp, result, &da(data,option)->pointer, flags);
2201 cat(option_load_end_,ARG_MODE):
2204 DEFINE_OPCODE_END(OPCODE_OPTION_LOAD)
2209 ajla_option_t option;
2212 DEFINE_OPCODE_START(OPCODE_OPTION_TEST_FLAT)
2214 slot_1 = get_i_param(0);
2216 if (!frame_test_flag(fp, slot_1)) {
2218 option = *frame_slot(fp, slot_1, ajla_flat_option_t);
2220 goto cat(option_test_store_result_,ARG_MODE);
2222 goto cat(option_test_,ARG_MODE);
2224 DEFINE_OPCODE_END(OPCODE_OPTION_TEST_FLAT)
2226 DEFINE_OPCODE_START(OPCODE_OPTION_TEST)
2232 slot_1 = get_i_param(0);
2233 cat(option_test_,ARG_MODE):
2234 ptr = *frame_pointer(fp, slot_1);
2235 if (unlikely(pointer_is_thunk(ptr))) {
2237 ajla_assert(!frame_test_flag(fp, get_i_param(2)), (file_line, "option test: flag already set for destination slot %"PRIuMAX"", (uintmax_t)get_i_param(2)));
2238 ex = thunk_option_test(fp, ip, slot_1, get_i_param(1), get_i_param(2));
2239 if (ex == POINTER_FOLLOW_THUNK_GO) {
2242 } else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY)) {
2243 RELOAD_EX_POSITION(ex);
2247 data = pointer_get_data(ptr);
2248 option = da(data,option)->option;
2249 cat(option_test_store_result_,ARG_MODE):
2250 slot_r = get_i_param(2);
2252 ajla_assert(!frame_test_flag(fp, slot_r), (file_line, "option test: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));
2253 *frame_slot(fp, slot_r, ajla_flat_option_t) = option == (ajla_option_t)get_i_param(1);
2257 DEFINE_OPCODE_END(OPCODE_OPTION_TEST)
2259 DEFINE_OPCODE_START(OPCODE_OPTION_ORD_FLAT)
2261 slot_1 = get_i_param(0);
2263 if (!frame_test_flag(fp, slot_1)) {
2265 option = *frame_slot(fp, slot_1, ajla_flat_option_t);
2267 goto cat(option_ord_store_result_,ARG_MODE);
2269 goto cat(option_ord_,ARG_MODE);
2271 DEFINE_OPCODE_END(OPCODE_OPTION_ORD_FLAT)
2273 DEFINE_OPCODE_START(OPCODE_OPTION_ORD)
2279 slot_1 = get_i_param(0);
2280 cat(option_ord_,ARG_MODE):
2281 ptr = *frame_pointer(fp, slot_1);
2282 if (unlikely(pointer_is_thunk(ptr))) {
2284 ajla_assert(!frame_test_flag(fp, get_i_param(1)), (file_line, "option ord: flag already set for destination slot %"PRIuMAX"", (uintmax_t)get_i_param(2)));
2285 ex = thunk_option_ord(fp, ip, slot_1, get_i_param(1));
2286 if (ex == POINTER_FOLLOW_THUNK_GO) {
2289 } else if (unlikely(ex != POINTER_FOLLOW_THUNK_RETRY)) {
2290 RELOAD_EX_POSITION(ex);
2294 data = pointer_get_data(ptr);
2295 option = da(data,option)->option;
2296 cat(option_ord_store_result_,ARG_MODE):
2297 slot_r = get_i_param(1);
2299 ajla_assert(!frame_test_flag(fp, slot_r), (file_line, "option ord: flag already set for destination slot %"PRIuMAX"", (uintmax_t)slot_r));
2300 *frame_slot(fp, slot_r, int_default_t) = option;
2304 DEFINE_OPCODE_END(OPCODE_OPTION_ORD)
2309 DEFINE_OPCODE_START(OPCODE_ARRAY_CREATE)
2311 frame_t result_slot;
2315 result_slot = get_i_param(0);
2316 n_entries = get_i_param(1);
2319 ajla_assert(n_entries != 0, (file_line, "array create: zero entries"));
2325 const struct type *type = frame_get_type_of_local(fp, get_param(ip, 0));
2326 bool flat = frame_variable_is_flat(fp, get_param(ip, 0));
2327 for (n = 1; n < n_entries; n++) {
2328 if (frame_variable_is_flat(fp, get_param(ip, n * 2)) != flat)
2330 if (unlikely((int_default_t)(n + 1) < zero))
2335 a = data_alloc_array_flat_mayfail(type, n, n, false, &ajla_error pass_file_line);
2337 a = data_alloc_array_pointers_mayfail(n, n, &ajla_error pass_file_line);
2338 } while (unlikely(!a) && (n >>= 1));
2341 data_dereference(total);
2342 goto cat(array_create_error_,ARG_MODE);
2346 unsigned char *flat_ptr = da_array_flat(a);
2347 for (i = 0; i < n; i++) {
2348 frame_t var_slot = get_param(ip, 0);
2349 memcpy_fast(flat_ptr, frame_var(fp, var_slot), type->size);
2350 flat_ptr += type->size;
2351 ADVANCE_IP(param_size(2));
2354 for (i = 0; i < n; i++) {
2355 frame_t var_slot = get_param(ip, 0);
2356 pointer_t ptr = frame_get_pointer_reference(fp, var_slot, (get_param(ip, 1) & OPCODE_FLAG_FREE_ARGUMENT) != 0);
2357 da(a,array_pointers)->pointer[i] = ptr;
2358 ADVANCE_IP(param_size(2));
2361 if (likely(!total)) {
2364 total = array_join(total, a, &ajla_error);
2365 if (unlikely(!total)) {
2366 cat(array_create_error_,ARG_MODE):
2367 ip = cat(free_parameters_,ARG_MODE)(fp, ip, n_entries);
2368 frame_set_pointer(fp, result_slot, pointer_error(ajla_error, fp, ip pass_file_line));
2372 if (likely(!n_entries)) {
2373 frame_set_pointer(fp, result_slot, pointer_data(total));
2378 DEFINE_OPCODE_END(OPCODE_ARRAY_CREATE)
2380 DEFINE_OPCODE_START(OPCODE_ARRAY_CREATE_EMPTY_FLAT)
2382 frame_t result_slot;
2384 const struct type *type = da_type(get_frame(fp)->function, get_i_param(1));
2385 TYPE_TAG_VALIDATE(type->tag);
2387 a = data_alloc_array_flat_mayfail(type, 0, 0, false, &ajla_error pass_file_line);
2388 result_slot = get_i_param(0);
2389 frame_set_pointer(fp, result_slot, likely(a != NULL) ? pointer_data(a) : pointer_error(ajla_error, fp, ip pass_file_line));
2392 DEFINE_OPCODE_END(OPCODE_ARRAY_CREATE_EMPTY_FLAT)
2394 DEFINE_OPCODE_START(OPCODE_ARRAY_CREATE_EMPTY)
2396 frame_t result_slot;
2399 a = data_alloc_array_pointers_mayfail(0, 0, &ajla_error pass_file_line);
2400 result_slot = get_i_param(0);
2401 frame_set_pointer(fp, result_slot, likely(a != NULL) ? pointer_data(a) : pointer_error(ajla_error, fp, ip pass_file_line));
2404 DEFINE_OPCODE_END(OPCODE_ARRAY_CREATE_EMPTY)
2406 DEFINE_OPCODE_START(OPCODE_ARRAY_FILL)
2408 frame_t content_slot, length_slot, result_slot;
2409 unsigned char content_flag;
2410 array_index_t length;
2411 pointer_t result_ptr;
2414 content_slot = get_i_param(0);
2415 content_flag = get_i_param(1);
2416 length_slot = get_i_param(2);
2417 result_slot = get_i_param(3);
2419 ex = ipret_get_index(fp, ip, fp, length_slot, NULL, &length, &result_ptr pass_file_line);
2420 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO)) {
2421 if (ex == POINTER_FOLLOW_THUNK_EXCEPTION) {
2422 if (content_flag & OPCODE_FLAG_FREE_ARGUMENT)
2423 frame_free_and_clear(fp, content_slot);
2424 goto cat(array_fill_error_,ARG_MODE);
2426 RELOAD_EX_POSITION(ex);
2429 if (!frame_variable_is_flat(fp, content_slot)) {
2430 pointer_t ptr = frame_get_pointer_reference(fp, content_slot, (content_flag & OPCODE_FLAG_FREE_ARGUMENT) != 0);
2431 if (unlikely((content_flag & OPCODE_ARRAY_FILL_FLAG_SPARSE) != 0)) {
2432 result_ptr = array_create_sparse(length, ptr);
2434 result_ptr = array_create(length, NULL, NULL, ptr);
2437 const struct type *type = frame_get_type_of_local(fp, content_slot);
2439 if (unlikely((content_flag & OPCODE_ARRAY_FILL_FLAG_SPARSE) != 0) && likely(index_ge_int(length, 1))) {
2440 pointer_t ptr = flat_to_data(type, frame_var(fp, content_slot));
2441 result_ptr = array_create_sparse(length, ptr);
2443 if (TYPE_IS_FLAT(frame_get_type_of_local(fp, result_slot))) {
2444 unsigned char *result = frame_var(fp, result_slot);
2445 int_default_t l = index_to_int(length);
2446 index_free(&length);
2448 result = mempcpy(result, frame_var(fp, content_slot), type->size);
2450 goto cat(array_exit_,ARG_MODE);
2452 result_ptr = array_create(length, type, frame_var(fp, content_slot), pointer_empty());
2458 cat(array_fill_error_,ARG_MODE):
2459 frame_set_pointer(fp, result_slot, result_ptr);
2461 cat(array_exit_,ARG_MODE):
2464 DEFINE_OPCODE_END(OPCODE_ARRAY_FILL)
2466 DEFINE_OPCODE_START(OPCODE_ARRAY_STRING)
2468 frame_t result_slot, length;
2469 pointer_t result_ptr;
2471 result_slot = get_i_param(0);
2472 length = get_i_param(1);
2475 if (unlikely((int_default_t)length < (int_default_t)zero) ||
2476 unlikely((frame_t)(int_default_t)length != length)) {
2477 result_ptr = pointer_error(error_ajla(EC_ASYNC, AJLA_ERROR_SIZE_OVERFLOW), fp, ip pass_file_line);
2479 result_ptr = array_string(length, type_get_fixed(0, true), cast_ptr(unsigned char *, ip));
2481 frame_set_pointer(fp, result_slot, result_ptr);
2483 ADVANCE_IP((length + 1) >> 1);
2485 DEFINE_OPCODE_END(OPCODE_ARRAY_STRING)
2487 DEFINE_OPCODE_START(OPCODE_ARRAY_UNICODE)
2489 frame_t result_slot, length;
2490 pointer_t result_ptr;
2492 result_slot = get_i_param(0);
2493 length = get_i_param(1);
2496 if (unlikely((int_default_t)length < (int_default_t)zero) ||
2497 unlikely((frame_t)(int_default_t)length != length)) {
2498 result_ptr = pointer_error(error_ajla(EC_ASYNC, AJLA_ERROR_SIZE_OVERFLOW), fp, ip pass_file_line);
2500 result_ptr = array_string(length, type_get_int(2), cast_ptr(unsigned char *, ip));
2502 frame_set_pointer(fp, result_slot, result_ptr);
2504 ADVANCE_IP(length * 2);
2506 DEFINE_OPCODE_END(OPCODE_ARRAY_STRING)
2508 DEFINE_OPCODE_START(OPCODE_ARRAY_LOAD)
2510 frame_t array, idx, result;
2512 const struct type *t, *t_elem;
2514 array_index_t array_index;
2516 unsigned char *flat;
2518 array = get_i_param(0);
2519 idx = get_i_param(1);
2520 result = get_i_param(2);
2521 flags = get_i_param(3);
2523 ex = ipret_get_index(fp, ip, fp, idx, NULL, &array_index, &ptr pass_file_line);
2524 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO)) {
2525 if (ex == POINTER_FOLLOW_THUNK_EXCEPTION)
2526 goto cat(array_load_set_ptr_,ARG_MODE);
2527 RELOAD_EX_POSITION(ex);
2530 t = frame_get_type_of_local(fp, array);
2531 if (t->tag == TYPE_TAG_flat_array && !frame_test_flag(fp, array)) {
2532 const struct flat_array_definition *def = type_def(t,flat_array);
2533 if (unlikely(index_ge_int(array_index, def->n_elements))) {
2534 if (unlikely((flags & OPCODE_ARRAY_INDEX_IN_RANGE) != 0))
2535 internal(file_line, "array_load: flat array index out of range");
2536 index_free(&array_index);
2537 ptr = pointer_error(error_ajla(EC_SYNC, AJLA_ERROR_INDEX_OUT_OF_RANGE), fp, ip pass_file_line);
2538 goto cat(array_load_set_ptr_,ARG_MODE);
2541 flat = frame_var(fp, array) + t_elem->size * index_to_int(array_index);
2542 index_free(&array_index);
2543 goto cat(array_load_set_flat_,ARG_MODE);
2545 pointer_t *array_ptr = frame_pointer(fp, array);
2549 cat(array_restart_pointer_follow_,ARG_MODE):
2550 pointer_follow(array_ptr, false, data, unlikely(flags & OPCODE_OP_FLAG_STRICT) ? PF_WAIT : PF_NOEVAL, fp, ip,
2551 index_free(&array_index);
2552 if (!(flags & OPCODE_OP_FLAG_STRICT)) {
2553 ex_ = ipret_array_load_create_thunk(fp, ip, array, idx, result);
2554 if (ex_ == POINTER_FOLLOW_THUNK_GO)
2555 goto cat(array_load_end_,ARG_MODE);
2557 RELOAD_EX_POSITION(ex_),
2558 index_free(&array_index);
2559 thunk_reference(thunk_);
2560 ptr = pointer_thunk(thunk_);
2561 goto cat(array_load_set_ptr_,ARG_MODE);
2564 if (unlikely(da_tag(data) == DATA_TAG_array_incomplete)) {
2565 array_index_t len_first;
2567 if (array_ptr == frame_pointer(fp, array)) {
2568 if (!frame_test_and_set_flag(fp, array))
2569 data_reference(data);
2570 if (array_incomplete_collapse(array_ptr))
2571 goto cat(array_restart_pointer_follow_,ARG_MODE);
2574 len_first = array_len(pointer_get_data(da(data,array_incomplete)->first));
2575 if (!index_ge_index(array_index, len_first)) {
2576 index_free(&len_first);
2577 data = pointer_get_data(da(data,array_incomplete)->first);
2579 index_sub(&array_index, len_first);
2580 index_free(&len_first);
2581 array_ptr = &da(data,array_incomplete)->next;
2582 goto cat(array_restart_pointer_follow_,ARG_MODE);
2586 if (unlikely(!array_read(data, array_index, &pptr, &flat, &t_elem, NULL))) {
2587 if (unlikely((flags & OPCODE_ARRAY_INDEX_IN_RANGE) != 0))
2588 internal(file_line, "array_load: array index out of range");
2589 ptr = pointer_error(error_ajla(EC_SYNC, AJLA_ERROR_INDEX_OUT_OF_RANGE), fp, ip pass_file_line);
2590 goto cat(array_load_set_ptr_,ARG_MODE);
2593 pointer_reference_maybe(fp, result, pptr, flags);
2594 goto cat(array_load_end_,ARG_MODE);
2596 cat(array_load_set_flat_,ARG_MODE):
2597 if (likely(TYPE_IS_FLAT(frame_get_type_of_local(fp, result)))) {
2598 memcpy_fast(frame_var(fp, result), flat, t_elem->size);
2599 goto cat(array_load_end_,ARG_MODE);
2601 ptr = flat_to_data(t_elem, flat);
2606 cat(array_load_set_ptr_,ARG_MODE):
2607 frame_set_pointer(fp, result, ptr);
2609 cat(array_load_end_,ARG_MODE):
2612 DEFINE_OPCODE_END(OPCODE_ARRAY_LOAD)
2614 DEFINE_OPCODE_START(OPCODE_ARRAY_LEN)
2616 frame_t slot_a, slot_r;
2620 slot_a = get_i_param(0);
2621 slot_r = get_i_param(1);
2622 flags = get_i_param(2);
2624 ex = ipret_array_len(fp, ip, slot_r, slot_a, flags);
2625 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO))
2626 RELOAD_EX_POSITION(ex);
2630 DEFINE_OPCODE_END(OPCODE_ARRAY_LEN)
2632 DEFINE_OPCODE_START(OPCODE_ARRAY_LEN_GREATER_THAN)
2634 frame_t slot_a, slot_l, slot_r;
2638 slot_a = get_i_param(0);
2639 slot_l = get_i_param(1);
2640 slot_r = get_i_param(2);
2641 flags = get_i_param(3);
2643 ex = ipret_array_len_greater_than(fp, ip, slot_r, slot_a, slot_l, flags);
2644 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO))
2645 RELOAD_EX_POSITION(ex);
2649 DEFINE_OPCODE_END(OPCODE_ARRAY_LEN_GREATER_THAN)
2651 DEFINE_OPCODE_START(OPCODE_ARRAY_SUB)
2653 frame_t slot_a, slot_start, slot_end, slot_r;
2657 slot_a = get_i_param(0);
2658 slot_start = get_i_param(1);
2659 slot_end = get_i_param(2);
2660 slot_r = get_i_param(3);
2661 flags = get_i_param(4);
2663 ex = ipret_array_sub(fp, ip, slot_r, slot_a, slot_start, slot_end, flags);
2664 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO))
2665 RELOAD_EX_POSITION(ex);
2669 DEFINE_OPCODE_END(OPCODE_ARRAY_SUB)
2671 DEFINE_OPCODE_START(OPCODE_ARRAY_SKIP)
2673 frame_t slot_a, slot_start, slot_r;
2677 slot_a = get_i_param(0);
2678 slot_start = get_i_param(1);
2679 slot_r = get_i_param(2);
2680 flags = get_i_param(3);
2682 ex = ipret_array_skip(fp, ip, slot_r, slot_a, slot_start, flags);
2683 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO))
2684 RELOAD_EX_POSITION(ex);
2688 DEFINE_OPCODE_END(OPCODE_ARRAY_SKIP)
2690 DEFINE_OPCODE_START(OPCODE_ARRAY_APPEND)
2692 frame_t slot_r, slot_1, slot_2;
2696 slot_r = get_i_param(0);
2697 flags = get_i_param(1);
2698 slot_1 = get_i_param(2);
2699 slot_2 = get_i_param(3);
2701 ex = ipret_array_append(fp, ip, slot_r, slot_1, slot_2, flags);
2702 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO))
2703 RELOAD_EX_POSITION(ex);
2707 DEFINE_OPCODE_END(OPCODE_ARRAY_APPEND)
2709 DEFINE_OPCODE_START(OPCODE_ARRAY_APPEND_ONE_FLAT)
2711 frame_t slot_r, slot_1, slot_2;
2715 slot_r = get_i_param(0);
2716 flags = get_i_param(1);
2717 slot_1 = get_i_param(2);
2718 slot_2 = get_i_param(3);
2720 ex = ipret_array_append_one_flat(fp, ip, slot_r, slot_1, slot_2, flags);
2721 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO))
2722 RELOAD_EX_POSITION(ex);
2726 DEFINE_OPCODE_END(OPCODE_ARRAY_APPEND_ONE_FLAT)
2728 DEFINE_OPCODE_START(OPCODE_ARRAY_APPEND_ONE)
2730 frame_t slot_r, slot_1, slot_2;
2734 slot_r = get_i_param(0);
2735 flags = get_i_param(1);
2736 slot_1 = get_i_param(2);
2737 slot_2 = get_i_param(3);
2739 ex = ipret_array_append_one(fp, ip, slot_r, slot_1, slot_2, flags);
2740 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO))
2741 RELOAD_EX_POSITION(ex);
2745 DEFINE_OPCODE_END(OPCODE_ARRAY_APPEND_ONE)
2747 DEFINE_OPCODE_START(OPCODE_ARRAY_FLATTEN)
2749 frame_t slot_r, slot_1;
2753 slot_r = get_i_param(0);
2754 flags = get_i_param(1);
2755 slot_1 = get_i_param(2);
2757 ex = ipret_array_flatten(fp, ip, slot_r, slot_1, flags);
2758 if (unlikely(ex != POINTER_FOLLOW_THUNK_GO))
2759 RELOAD_EX_POSITION(ex);
2763 DEFINE_OPCODE_END(OPCODE_ARRAY_FLATTEN)
2767 DEFINE_OPCODE_START(OPCODE_IO)
2769 unsigned char io_code, n_outputs, n_inputs, n_params;
2772 io_code = get_i_param(0);
2773 n_outputs = get_i_param(1);
2774 n_inputs = get_i_param(2);
2775 n_params = get_i_param(3);
2777 ex = ipret_io(fp, ip, io_code, n_outputs, n_inputs, n_params);
2778 if (ex != POINTER_FOLLOW_THUNK_GO) {
2779 RELOAD_EX_POSITION(ex);
2781 ADVANCE_IP(3 + 2 * (n_outputs + n_inputs + n_params));
2784 DEFINE_OPCODE_END(OPCODE_IO)
2788 DEFINE_OPCODE_START(OPCODE_INTERNAL_FUNCTION)
2790 void *ex = function_call_internal(fp, ip);
2791 if (ex != POINTER_FOLLOW_THUNK_RETRY) {
2792 RELOAD_EX_POSITION(ex);
2795 DEFINE_OPCODE_END(OPCODE_INTERNAL_FUNCTION)
2799 DEFINE_OPCODE_START(OPCODE_UNREACHABLE)
2801 internal(file_line, "unreachable code");
2803 DEFINE_OPCODE_END(OPCODE_UNREACHABLE)
2807 DEFINE_OPCODE_START(OPCODE_EXIT_THREAD)
2809 struct execution_control *ex = frame_execution_control(fp);
2810 pointer_t *var_ptr = frame_pointer(fp, get_i_param(0));
2811 struct data attr_unused *data;
2813 pointer_follow(var_ptr, true, data, PF_WAIT, fp, ip,
2814 RELOAD_EX_POSITION(ex_),
2815 ajla_assert_lo(thunk_tag(thunk_) == THUNK_TAG_EXCEPTION, (file_line, "exit_thread: invalid thunk tag %u", thunk_tag(thunk_)));
2816 goto exit_ipret_thunk;
2820 ajla_assert_lo(ex->thunk == NULL, (file_line, "exit_thread: non-NULL thunk %p", ex->thunk));
2821 ex->current_frame = fp;
2822 ex->current_ip = frame_ip(fp, ip);
2823 execution_control_terminate(ex, *var_ptr);
2826 DEFINE_OPCODE_END(OPCODE_EXIT_THREAD)
2836 #undef ADVANCE_I_PARAM
2839 #undef DEFINE_OPCODE_START