Commit | Line | Data |
---|---|---|
4a71579b | 1 | /* |
79bfeef6 | 2 | * Copyright (C) 2012-2023 Free Software Foundation, Inc. |
4a71579b PC |
3 | * |
4 | * This file is part of GNU lightning. | |
5 | * | |
6 | * GNU lightning is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU Lesser General Public License as published | |
8 | * by the Free Software Foundation; either version 3, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | * GNU lightning is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
14 | * License for more details. | |
15 | * | |
16 | * Authors: | |
17 | * Paulo Cesar Pereira de Andrade | |
18 | */ | |
19 | ||
20 | #ifndef _jit_private_h | |
21 | #define _jit_private_h | |
22 | ||
23 | #if HAVE_CONFIG_H | |
24 | # include "config.h" | |
25 | #endif | |
26 | ||
27 | #include <assert.h> | |
28 | #include <limits.h> | |
29 | #include <stdio.h> | |
30 | ||
31 | #ifdef STDC_HEADERS | |
32 | # include <stddef.h> | |
33 | #else | |
34 | # if !defined(offsetof) | |
35 | # define offsetof(type, field) ((char *)&((type *)0)->field - (char *)0) | |
36 | # endif | |
37 | #endif | |
38 | ||
39 | #if defined(__GNUC__) | |
40 | # define maybe_unused __attribute__ ((unused)) | |
41 | # define unlikely(exprn) __builtin_expect(!!(exprn), 0) | |
42 | # define likely(exprn) __builtin_expect(!!(exprn), 1) | |
43 | # if (__GNUC__ >= 4) | |
44 | # define PUBLIC __attribute__ ((visibility("default"))) | |
45 | # define HIDDEN __attribute__ ((visibility("hidden"))) | |
46 | # else | |
47 | # define PUBLIC /**/ | |
48 | # define HIDDEN /**/ | |
49 | # endif | |
50 | #else | |
51 | # define maybe_unused /**/ | |
52 | # define unlikely(exprn) exprn | |
53 | # define likely(exprn) exprn | |
54 | # define PUBLIC /**/ | |
55 | # define HIDDEN /**/ | |
56 | #endif | |
57 | ||
79bfeef6 PC |
58 | #if PACKED_STACK || STRONG_TYPE_CHECKING |
59 | # define assert_arg_type(code, expect) \ | |
60 | do assert((code) == (expect)); while (0) | |
61 | # define assert_putarg_type(code, expect) \ | |
62 | do \ | |
63 | assert((((code) - jit_code_putargr_c) >> 2) == \ | |
64 | ((expect) - jit_code_arg_c)); \ | |
65 | while (0) | |
66 | #else | |
67 | # define assert_arg_type(code, expect) \ | |
68 | do assert((int)(code) == (int)(expect) || \ | |
69 | (code) == jit_code_arg); while (0) | |
70 | # define assert_putarg_type(code, expect) \ | |
71 | do \ | |
72 | assert(((((code) - jit_code_putargr_c) >> 2) == \ | |
73 | ((expect) - jit_code_arg_c)) || \ | |
74 | ((code) == jit_code_arg)); \ | |
75 | while (0) | |
76 | #endif | |
77 | ||
4a71579b PC |
78 | #define rc(value) jit_class_##value |
79 | #define rn(reg) (jit_regno(_rvs[jit_regno(reg)].spec)) | |
80 | ||
81 | #if defined(__i386__) || defined(__x86_64__) | |
82 | # define JIT_SP _RSP | |
83 | # define JIT_RET _RAX | |
84 | # if __X32 | |
85 | # define JIT_FRET _ST0 | |
86 | typedef jit_uint32_t jit_regset_t; | |
87 | # else | |
88 | # if __CYGWIN__ || _WIN32 | |
89 | # define JIT_RA0 _RCX | |
90 | # else | |
91 | # define JIT_RA0 _RDI | |
92 | # endif | |
93 | # define JIT_FA0 _XMM0 | |
94 | # define JIT_FRET _XMM0 | |
95 | typedef jit_uint64_t jit_regset_t; | |
96 | # endif | |
97 | #elif defined(__mips__) | |
98 | # define JIT_RA0 _A0 | |
99 | # define JIT_FA0 _F12 | |
100 | # define JIT_SP _SP | |
101 | # define JIT_RET _V0 | |
102 | # define JIT_FRET _F0 | |
103 | typedef jit_uint64_t jit_regset_t; | |
104 | #elif defined(__arm__) | |
105 | # define JIT_RA0 _R0 | |
106 | # define JIT_FA0 _D0 | |
107 | # define JIT_SP _R13 | |
108 | # define JIT_RET _R0 | |
109 | # if defined(__ARM_PCS_VFP) | |
110 | # define JIT_FRET _D0 | |
111 | # else | |
112 | # define JIT_FRET _R0 | |
113 | # endif | |
114 | typedef jit_uint64_t jit_regset_t; | |
115 | #elif defined(__powerpc__) | |
116 | # define JIT_RA0 _R3 | |
117 | # define JIT_FA0 _F1 | |
118 | # define JIT_SP _R1 | |
119 | # define JIT_RET _R3 | |
120 | # define JIT_FRET _F1 | |
121 | typedef jit_uint64_t jit_regset_t; | |
122 | #elif defined(__sparc__) | |
123 | # define JIT_SP _SP | |
124 | # define JIT_RET _I0 | |
125 | # define JIT_FRET _F0 | |
126 | # if __WORDSIZE == 32 | |
127 | typedef jit_uint64_t jit_regset_t; | |
128 | # else | |
129 | typedef struct { | |
130 | jit_uint64_t rl; | |
131 | jit_uint64_t rh; | |
132 | } jit_regset_t; | |
133 | # endif | |
134 | #elif defined(__ia64__) | |
135 | # define JIT_SP _R12 | |
136 | # define JIT_RET _R8 | |
137 | # define JIT_FRET _F8 | |
138 | typedef struct { | |
139 | jit_uint64_t rl; | |
140 | jit_uint64_t rh; | |
141 | jit_uint64_t fl; | |
142 | jit_uint64_t fh; | |
143 | } jit_regset_t; | |
144 | #elif defined(__hppa__) | |
145 | # define JIT_SP _R30 | |
146 | # define JIT_RET _R28 | |
147 | # define JIT_FRET _F4 | |
148 | typedef jit_uint64_t jit_regset_t; | |
149 | #elif defined(__aarch64__) | |
150 | # define JIT_RA0 _R0 | |
151 | # define JIT_FA0 _V0 | |
152 | # define JIT_SP _SP | |
153 | # define JIT_RET _R0 | |
154 | # define JIT_FRET _V0 | |
155 | typedef jit_uint64_t jit_regset_t; | |
156 | #elif defined(__s390__) || defined(__s390x__) | |
157 | # define JIT_SP _R15 | |
158 | # define JIT_RET _R2 | |
159 | # define JIT_FRET _F0 | |
160 | typedef jit_uint32_t jit_regset_t; | |
161 | #elif defined(__alpha__) | |
162 | # define JIT_SP _SP | |
163 | # define JIT_RET _V0 | |
164 | # define JIT_FRET _F0 | |
165 | typedef jit_uint64_t jit_regset_t; | |
166 | #elif defined(__riscv) | |
167 | # define JIT_RA0 _A0 | |
168 | # define JIT_FA0 _FA0 | |
169 | # define JIT_SP _SP | |
170 | # define JIT_RET _A0 | |
171 | # define JIT_FRET _FA0 | |
172 | typedef jit_uint64_t jit_regset_t; | |
24d91c0d PC |
173 | #elif defined(__loongarch__) |
174 | # define JIT_RA0 _A0 | |
175 | # define JIT_FA0 _FA0 | |
176 | # define JIT_SP _SP | |
177 | # define JIT_RET _A0 | |
178 | # define JIT_FRET _FA0 | |
179 | typedef jit_uint64_t jit_regset_t; | |
4a71579b PC |
180 | #endif |
181 | ||
182 | #define jit_data(u,v,w) _jit_data(_jit,u,v,w) | |
183 | extern jit_node_t *_jit_data(jit_state_t*, const void*, | |
184 | jit_word_t, jit_int32_t); | |
185 | ||
186 | #define jit_size(vector) (sizeof(vector) / sizeof((vector)[0])) | |
187 | ||
188 | #define jit_reg_free_p(regno) \ | |
189 | (!jit_regset_tstbit(&_jitc->reglive, regno) && \ | |
190 | !jit_regset_tstbit(&_jitc->regarg, regno) && \ | |
191 | !jit_regset_tstbit(&_jitc->regsav, regno)) | |
192 | ||
193 | #define jit_reg_free_if_spill_p(regno) \ | |
194 | (!jit_regset_tstbit(&_jitc->regarg, regno) && \ | |
195 | !jit_regset_tstbit(&_jitc->regsav, regno)) | |
196 | ||
79bfeef6 | 197 | #define jit_code_inc_synth(code) \ |
4a71579b | 198 | do { \ |
79bfeef6 | 199 | (void)jit_new_node(code); \ |
4a71579b PC |
200 | jit_synth_inc(); \ |
201 | } while (0) | |
79bfeef6 PC |
202 | #define jit_inc_synth(name) \ |
203 | jit_code_inc_synth(jit_code_##name) | |
204 | #define jit_code_inc_synth_w(code, u) \ | |
4a71579b | 205 | do { \ |
79bfeef6 | 206 | (void)jit_new_node_w(code, u); \ |
4a71579b PC |
207 | jit_synth_inc(); \ |
208 | } while (0) | |
79bfeef6 PC |
209 | #define jit_inc_synth_w(name, u) \ |
210 | jit_code_inc_synth_w(jit_code_##name, u) | |
211 | #define jit_code_inc_synth_f(code, u) \ | |
4a71579b | 212 | do { \ |
79bfeef6 | 213 | (void)jit_new_node_f(code, u); \ |
4a71579b PC |
214 | jit_synth_inc(); \ |
215 | } while (0) | |
79bfeef6 PC |
216 | #define jit_inc_synth_f(name, u) \ |
217 | jit_code_inc_synth_f(jit_code_##name, u) | |
218 | #define jit_code_inc_synth_d(code, u) \ | |
4a71579b | 219 | do { \ |
79bfeef6 | 220 | (void)jit_new_node_d(code, u); \ |
4a71579b PC |
221 | jit_synth_inc(); \ |
222 | } while (0) | |
79bfeef6 PC |
223 | #define jit_inc_synth_d(name, u) \ |
224 | jit_code_inc_synth_d(jit_code_##name, u) | |
225 | #define jit_code_inc_synth_ww(code, u, v) \ | |
4a71579b | 226 | do { \ |
79bfeef6 | 227 | (void)jit_new_node_ww(code, u, v); \ |
4a71579b PC |
228 | jit_synth_inc(); \ |
229 | } while (0) | |
79bfeef6 PC |
230 | #define jit_inc_synth_ww(name, u, v) \ |
231 | jit_code_inc_synth_ww(jit_code_##name, u, v) | |
232 | #define jit_code_inc_synth_wp(code, u, v) \ | |
4a71579b | 233 | do { \ |
79bfeef6 | 234 | (void)jit_new_node_wp(code, u, v); \ |
4a71579b PC |
235 | jit_synth_inc(); \ |
236 | } while (0) | |
79bfeef6 PC |
237 | #define jit_inc_synth_wp(name, u, v) \ |
238 | jit_code_inc_synth_wp(jit_code_##name, u, v) | |
239 | #define jit_code_inc_synth_fp(code, u, v) \ | |
4a71579b | 240 | do { \ |
79bfeef6 | 241 | (void)jit_new_node_fp(code, u, v); \ |
4a71579b PC |
242 | jit_synth_inc(); \ |
243 | } while (0) | |
79bfeef6 PC |
244 | #define jit_inc_synth_fp(name, u, v) \ |
245 | jit_code_inc_synth_fp(jit_code_##name, u, v) | |
246 | #define jit_code_inc_synth_dp(code, u, v) \ | |
4a71579b | 247 | do { \ |
79bfeef6 | 248 | (void)jit_new_node_dp(code, u, v); \ |
4a71579b PC |
249 | jit_synth_inc(); \ |
250 | } while (0) | |
79bfeef6 PC |
251 | #define jit_inc_synth_dp(name, u, v) \ |
252 | jit_code_inc_synth_dp(jit_code_##name, u, v) | |
4a71579b PC |
253 | #define jit_dec_synth() jit_synth_dec() |
254 | ||
79bfeef6 PC |
255 | #define jit_link_alist(node) \ |
256 | do { \ | |
257 | node->link = _jitc->function->alist; \ | |
258 | _jitc->function->alist = node; \ | |
259 | } while (0) | |
260 | #define jit_check_frame() \ | |
261 | do { \ | |
262 | if (!_jitc->function->need_frame) { \ | |
263 | _jitc->again = 1; \ | |
264 | _jitc->function->need_frame = 1; \ | |
265 | } \ | |
266 | } while (0) | |
267 | #define jit_diffsize() (stack_framesize - _jitc->framesize) | |
268 | #define jit_framesize() (stack_framesize - jit_diffsize()) | |
269 | #define jit_selfsize() (_jitc->function->self.size - jit_diffsize()) | |
270 | ||
4a71579b PC |
271 | #define jit_link_prolog() \ |
272 | do { \ | |
273 | _jitc->tail->link = _jitc->function->prolog->link; \ | |
274 | _jitc->function->prolog->link = _jitc->tail; \ | |
275 | } while (0) | |
276 | #define jit_link_prepare() \ | |
277 | do { \ | |
278 | _jitc->tail->link = _jitc->prepare->link; \ | |
279 | _jitc->prepare->link = _jitc->tail; \ | |
280 | } while (0) | |
281 | #define jit_link_reverse(where) \ | |
282 | do { \ | |
283 | jit_node_t *tmp, *tail = 0; \ | |
284 | while (where) { \ | |
285 | tmp = (where)->link; \ | |
286 | (where)->link = tail; \ | |
287 | tail = where; \ | |
288 | where = tmp; \ | |
289 | } \ | |
290 | where = tail; \ | |
291 | } while (0); | |
292 | ||
293 | /* | |
294 | * Private jit_class bitmasks | |
295 | */ | |
296 | #define jit_class_named 0x00400000 /* hit must be the named reg */ | |
297 | #define jit_class_nospill 0x00800000 /* hint to fail if need spill */ | |
298 | #define jit_class_sft 0x01000000 /* not a hardware register */ | |
299 | #define jit_class_rg8 0x04000000 /* x86 8 bits */ | |
300 | #define jit_class_xpr 0x80000000 /* float / vector */ | |
301 | /* Used on sparc64 where %f0-%f31 can be encode for single float | |
302 | * but %f32 to %f62 only as double precision */ | |
79bfeef6 PC |
303 | #define jit_class_sng 0x00010000 /* Single precision float */ |
304 | #define jit_class_dbl 0x00020000 /* Only double precision float */ | |
4a71579b PC |
305 | #define jit_regno_patch 0x00008000 /* this is a register |
306 | * returned by a "user" call | |
307 | * to jit_get_reg() */ | |
308 | ||
309 | #define jit_call_default 0 | |
310 | #define jit_call_varargs 1 | |
311 | ||
312 | #define jit_kind_register 1 | |
313 | #define jit_kind_code 2 | |
314 | #define jit_kind_word 3 | |
315 | #define jit_kind_float32 4 | |
316 | #define jit_kind_float64 5 | |
317 | ||
318 | #define jit_cc_a0_reg 0x00000001 /* arg0 is a register */ | |
319 | #define jit_cc_a0_chg 0x00000002 /* arg0 is modified */ | |
320 | #define jit_cc_a0_jmp 0x00000004 /* arg0 is a jump target */ | |
321 | #define jit_cc_a0_rlh 0x00000008 /* arg0 is a register pair */ | |
322 | #define jit_cc_a0_int 0x00000010 /* arg0 is immediate word */ | |
323 | #define jit_cc_a0_flt 0x00000020 /* arg0 is immediate float */ | |
324 | #define jit_cc_a0_dbl 0x00000040 /* arg0 is immediate double */ | |
325 | #define jit_cc_a0_arg 0x00000080 /* arg1 is an argument int id */ | |
40a44dcb PC |
326 | #define jit_cc_a0_cnd 0x00000100 /* arg1 is a conditinally set register */ |
327 | #define jit_cc_a1_reg 0x00000200 /* arg1 is a register */ | |
328 | #define jit_cc_a1_chg 0x00000400 /* arg1 is modified */ | |
4a71579b PC |
329 | #define jit_cc_a1_int 0x00001000 /* arg1 is immediate word */ |
330 | #define jit_cc_a1_flt 0x00002000 /* arg1 is immediate float */ | |
331 | #define jit_cc_a1_dbl 0x00004000 /* arg1 is immediate double */ | |
332 | #define jit_cc_a1_arg 0x00008000 /* arg1 is an argument node */ | |
333 | #define jit_cc_a2_reg 0x00010000 /* arg2 is a register */ | |
334 | #define jit_cc_a2_chg 0x00020000 /* arg2 is modified */ | |
335 | #define jit_cc_a2_int 0x00100000 /* arg2 is immediate word */ | |
336 | #define jit_cc_a2_flt 0x00200000 /* arg2 is immediate float */ | |
337 | #define jit_cc_a2_dbl 0x00400000 /* arg2 is immediate double */ | |
ba3814c1 | 338 | #define jit_cc_a2_rlh 0x00800000 /* arg2 is a register pair */ |
4a71579b PC |
339 | |
340 | #if __ia64__ || (__sparc__ && __WORDSIZE == 64) | |
341 | extern void | |
342 | jit_regset_com(jit_regset_t*, jit_regset_t*); | |
343 | ||
344 | extern void | |
345 | jit_regset_and(jit_regset_t*, jit_regset_t*, jit_regset_t*); | |
346 | ||
347 | extern void | |
348 | jit_regset_ior(jit_regset_t*, jit_regset_t*, jit_regset_t*); | |
349 | ||
350 | extern void | |
351 | jit_regset_xor(jit_regset_t*, jit_regset_t*, jit_regset_t*); | |
352 | ||
353 | extern void | |
354 | jit_regset_set(jit_regset_t*, jit_regset_t*); | |
355 | ||
356 | extern void | |
357 | jit_regset_set_mask(jit_regset_t*, jit_int32_t); | |
358 | ||
359 | extern jit_bool_t | |
360 | jit_regset_cmp_ui(jit_regset_t*, jit_word_t); | |
361 | ||
362 | extern void | |
363 | jit_regset_set_ui(jit_regset_t*, jit_word_t); | |
364 | ||
365 | extern jit_bool_t | |
366 | jit_regset_set_p(jit_regset_t*); | |
367 | ||
368 | extern void | |
369 | jit_regset_clrbit(jit_regset_t*, jit_int32_t); | |
370 | ||
371 | extern void | |
372 | jit_regset_setbit(jit_regset_t*, jit_int32_t); | |
373 | ||
374 | extern jit_bool_t | |
375 | jit_regset_tstbit(jit_regset_t*, jit_int32_t); | |
376 | # if __sparc__ && __WORDSIZE == 64 | |
377 | # define jit_regset_new(set) \ | |
378 | do { (set)->rl = (set)->rh = 0; } while (0) | |
379 | # define jit_regset_del(set) \ | |
380 | do { (set)->rl = (set)->rh = 0; } while (0) | |
381 | # else | |
382 | # define jit_regset_new(set) \ | |
383 | do { (set)->rl = (set)->rh = (set)->fl = (set)->fh = 0; } while (0) | |
384 | # define jit_regset_del(set) \ | |
385 | do { (set)->rl = (set)->rh = (set)->fl = (set)->fh = 0; } while (0) | |
386 | # endif | |
387 | #else | |
388 | # define jit_regset_com(u, v) (*(u) = ~*(v)) | |
389 | # define jit_regset_and(u, v, w) (*(u) = *(v) & *(w)) | |
390 | # define jit_regset_ior(u, v, w) (*(u) = *(v) | *(w)) | |
391 | # define jit_regset_xor(u, v, w) (*(u) = *(v) ^ *(w)) | |
392 | # define jit_regset_set(u, v) (*(u) = *(v)) | |
393 | # define jit_regset_set_mask(u, v) (*(u) = (1LL << (v)) - 1) | |
394 | # define jit_regset_cmp_ui(u, v) (*(u) != (v)) | |
395 | # define jit_regset_set_ui(u, v) (*(u) = (v)) | |
396 | # define jit_regset_set_p(set) (*set) | |
397 | # define jit_regset_clrbit(set, bit) (*(set) &= ~(1LL << (bit))) | |
398 | # define jit_regset_setbit(set, bit) (*(set) |= 1LL << (bit)) | |
399 | # define jit_regset_tstbit(set, bit) (*(set) & (1LL << (bit))) | |
400 | # define jit_regset_new(set) (*(set) = 0) | |
401 | # define jit_regset_del(set) (*(set) = 0) | |
402 | #endif | |
403 | extern unsigned long | |
404 | jit_regset_scan1(jit_regset_t*, jit_int32_t); | |
405 | ||
406 | #define jit_reglive_setup() \ | |
407 | do { \ | |
408 | jit_regset_set_ui(&_jitc->reglive, 0); \ | |
409 | jit_regset_set_ui(&_jitc->regmask, 0); \ | |
410 | } while (0) | |
411 | ||
412 | /* | |
413 | * Types | |
414 | */ | |
415 | typedef union jit_data jit_data_t; | |
416 | typedef struct jit_note jit_note_t; | |
417 | typedef struct jit_line jit_line_t; | |
418 | typedef struct jit_block jit_block_t; | |
419 | typedef struct jit_value jit_value_t; | |
420 | typedef struct jit_compiler jit_compiler_t; | |
421 | typedef struct jit_function jit_function_t; | |
422 | typedef struct jit_register jit_register_t; | |
423 | #if __arm__ | |
424 | # if DISASSEMBLER | |
425 | typedef struct jit_data_info jit_data_info_t; | |
426 | # endif | |
c0c16242 PC |
427 | #elif __riscv |
428 | typedef struct jit_const jit_const_t; | |
4a71579b PC |
429 | #endif |
430 | ||
431 | union jit_data { | |
432 | struct { | |
433 | #if __BYTE_ORDER == __LITTLE_ENDIAN | |
434 | jit_int32_t l; | |
435 | jit_int32_t h; | |
436 | #else | |
437 | jit_int32_t h; | |
438 | jit_int32_t l; | |
439 | #endif | |
440 | } q; | |
441 | jit_word_t w; | |
442 | jit_float32_t f; | |
443 | jit_float64_t d; | |
444 | jit_pointer_t p; | |
445 | jit_node_t *n; | |
446 | }; | |
447 | ||
448 | struct jit_note { | |
449 | jit_uint8_t *code; | |
450 | char *name; | |
451 | jit_line_t *lines; | |
452 | jit_word_t length; | |
453 | jit_word_t size; /* of code */ | |
454 | }; | |
455 | ||
456 | struct jit_line { | |
457 | char *file; | |
458 | jit_int32_t *linenos; | |
459 | jit_int32_t *offsets; | |
460 | jit_word_t length; | |
461 | }; | |
462 | ||
463 | struct jit_node { | |
464 | jit_node_t *next; | |
465 | jit_code_t code; | |
466 | jit_uint16_t flag; | |
467 | jit_uint16_t offset; /* Used if DEVEL_DISASSEMBLER */ | |
468 | jit_data_t u; | |
469 | jit_data_t v; | |
470 | jit_data_t w; | |
471 | jit_node_t *link; | |
472 | }; | |
473 | ||
474 | struct jit_block { | |
475 | jit_node_t *label; | |
476 | jit_regset_t reglive; | |
477 | jit_regset_t regmask; | |
c0c16242 PC |
478 | jit_bool_t again; /* Flag need to rebuild regset masks |
479 | * due to changes in live and unknown | |
480 | * state. */ | |
4a71579b PC |
481 | }; |
482 | ||
483 | struct jit_value { | |
484 | jit_int32_t kind; | |
485 | jit_code_t code; | |
486 | jit_data_t base; | |
487 | jit_data_t disp; | |
488 | }; | |
489 | ||
490 | typedef struct { | |
491 | #if __arm__ | |
492 | jit_word_t kind; | |
493 | #endif | |
494 | jit_word_t inst; | |
495 | jit_node_t *node; | |
496 | } jit_patch_t; | |
497 | ||
498 | #if __arm__ && DISASSEMBLER | |
499 | struct jit_data_info { | |
500 | jit_uword_t code; /* pointer in code buffer */ | |
501 | jit_word_t length; /* length of constant vector */ | |
502 | }; | |
c0c16242 PC |
503 | #elif __riscv && __WORDSIZE == 64 |
504 | struct jit_const { | |
505 | jit_word_t value; | |
506 | jit_word_t address; | |
507 | jit_const_t *next; | |
508 | }; | |
4a71579b PC |
509 | #endif |
510 | ||
511 | struct jit_function { | |
512 | struct { | |
513 | jit_int32_t argi; | |
514 | jit_int32_t argf; | |
515 | jit_int32_t size; | |
516 | jit_int32_t aoff; | |
517 | jit_int32_t alen; | |
518 | jit_int32_t call; | |
519 | jit_int32_t argn; /* for debug output */ | |
520 | } self; | |
521 | struct { | |
522 | jit_int32_t argi; | |
523 | jit_int32_t argf; | |
524 | jit_int32_t size; | |
525 | jit_int32_t call; | |
526 | } call; | |
527 | jit_node_t *prolog; | |
528 | jit_node_t *epilog; | |
79bfeef6 | 529 | jit_node_t *alist; |
4a71579b PC |
530 | jit_int32_t *regoff; |
531 | jit_regset_t regset; | |
532 | jit_int32_t stack; | |
79bfeef6 PC |
533 | #if defined(__i386__) || defined(__x86_64__) |
534 | jit_int32_t cvt_offset; /* allocai'd offset for x87<->xmm or | |
535 | * fpr<->gpr transfer using the stack */ | |
536 | #endif | |
4a71579b PC |
537 | |
538 | /* Helper for common jit generation pattern, used in GNU Smalltalk | |
539 | * and possibly others, where a static frame layout is required or | |
540 | * assumed. */ | |
541 | jit_int32_t frame; | |
542 | jit_uint32_t define_frame : 1; | |
543 | jit_uint32_t assume_frame : 1; | |
544 | ||
79bfeef6 PC |
545 | jit_uint32_t need_frame : 1; /* need frame pointer? */ |
546 | jit_uint32_t need_stack : 1; /* need stack pointer? */ | |
547 | jit_uint32_t need_return : 1; /* not a leaf function */ | |
548 | ||
4a71579b PC |
549 | /* alloca offset offset */ |
550 | jit_int32_t aoffoff; | |
551 | /* uses allocar flag */ | |
552 | jit_uint32_t allocar : 1; | |
553 | ||
79bfeef6 PC |
554 | #if __arm__ |
555 | /* If will, or might use float registers and vfp is not available. | |
556 | * Use the first 64 bytes always, as the access to the virtual float | |
557 | * registers use hardcoded instructions that can only reach 64 byte | |
558 | * displacements, and to keep code simpler, do not use temporaries. */ | |
559 | jit_uint32_t swf_offset : 1; | |
560 | /* If need to call C functions for some operation, or variadic function */ | |
561 | jit_uint32_t save_reg_args : 1; | |
562 | #endif | |
563 | ||
4a71579b PC |
564 | /* varargs state offsets */ |
565 | jit_int32_t vaoff; /* offset of jit_va_list */ | |
566 | jit_int32_t vagp; /* first gp va argument */ | |
567 | jit_int32_t vafp; /* first fp va argument */ | |
568 | }; | |
569 | ||
570 | /* data used only during jit generation */ | |
571 | struct jit_compiler { | |
572 | #if __ia64__ | |
573 | struct { | |
574 | jit_uint64_t i : 41; | |
575 | jit_uint64_t t : 4; | |
576 | } inst[3]; | |
577 | jit_regset_t regs; /* changed regs since last stop */ | |
578 | jit_int32_t pred; /* changed preds last stop */ | |
579 | jit_int32_t ioff; /* offset in inst vector */ | |
580 | jit_int32_t rout; /* first output register */ | |
581 | jit_int32_t breg; /* base register for prolog/epilog */ | |
582 | #endif | |
79bfeef6 PC |
583 | #if __mips__ |
584 | struct { | |
585 | jit_int32_t op; /* pending instruction, candidate | |
586 | * to be inserted in a delay slot */ | |
587 | jit_bool_t pend; /* non zero if need to emit op */ | |
588 | } inst; | |
589 | #endif | |
4a71579b | 590 | #if __mips__ || __ia64__ || __alpha__ || \ |
24d91c0d | 591 | (__sparc__ && __WORDSIZE == 64) || __riscv || __loongarch__ |
4a71579b PC |
592 | jit_int32_t carry; |
593 | #define jit_carry _jitc->carry | |
594 | #endif | |
595 | jit_node_t *head; | |
596 | jit_node_t *tail; | |
597 | jit_node_t *prepare; /* inside prepare/finish* block */ | |
598 | jit_uint32_t realize : 1; /* jit_realize() called? */ | |
599 | jit_uint32_t dataset : 1; /* jit_dataset() called? */ | |
600 | jit_uint32_t done : 1; /* emit state finished */ | |
601 | jit_uint32_t emit : 1; /* emit state entered */ | |
602 | jit_uint32_t again : 1; /* start over emiting function */ | |
603 | jit_uint32_t synth : 8; /* emiting synthesized instructions */ | |
604 | #if DEBUG | |
605 | jit_uint32_t getreg : 1; | |
606 | #endif | |
607 | jit_uint32_t no_data : 1; | |
608 | jit_uint32_t no_note : 1; | |
79bfeef6 PC |
609 | jit_int32_t framesize; /* space for callee save registers, |
610 | * frame pointer and return address */ | |
4a71579b PC |
611 | jit_int32_t reglen; /* number of registers */ |
612 | jit_regset_t regarg; /* cannot allocate */ | |
613 | jit_regset_t regsav; /* automatic spill only once */ | |
614 | jit_regset_t reglive; /* known live registers at some point */ | |
615 | jit_regset_t regmask; /* register mask to update reglive */ | |
79bfeef6 | 616 | jit_regset_t explive; /* explicitly marked as live */ |
4a71579b PC |
617 | struct { |
618 | jit_uint8_t *end; | |
619 | } code; | |
620 | struct { | |
621 | jit_uint8_t *ptr; | |
622 | jit_node_t **table; /* very simple hash table */ | |
623 | jit_word_t size; /* number of vectors in table */ | |
624 | jit_word_t count; /* number of hash table entries */ | |
625 | jit_word_t offset; /* offset in bytes in ptr */ | |
626 | } data; | |
627 | jit_node_t **spill; | |
628 | jit_int32_t *gen; /* ssa like "register version" */ | |
629 | jit_value_t *values; /* temporary jit_value_t vector */ | |
630 | struct { | |
631 | jit_block_t *ptr; | |
632 | jit_word_t offset; | |
633 | jit_word_t length; | |
634 | } blocks; /* basic blocks */ | |
635 | struct { | |
636 | jit_patch_t *ptr; | |
637 | jit_word_t offset; | |
638 | jit_word_t length; | |
639 | } patches; /* forward patch information */ | |
640 | jit_function_t *function; /* current function */ | |
641 | struct { | |
642 | jit_function_t *ptr; | |
643 | jit_word_t offset; | |
644 | jit_word_t length; | |
645 | } functions; /* prolog/epilogue offsets in code */ | |
646 | struct { | |
647 | jit_node_t **ptr; | |
648 | jit_word_t offset; | |
649 | jit_word_t length; | |
650 | } pool; | |
651 | jit_node_t *list; | |
652 | struct { | |
653 | jit_node_t *head; /* first note node */ | |
654 | jit_node_t *tail; /* linked list insertion */ | |
655 | /* fields to store temporary state information */ | |
656 | jit_word_t size; | |
657 | jit_node_t *name; | |
658 | jit_node_t *note; | |
659 | jit_uint8_t *base; | |
660 | } note; | |
661 | #if __arm__ | |
662 | /* prevent using thumb instructions that set flags? */ | |
663 | jit_uint32_t no_set_flags : 1; | |
664 | # if DISASSEMBLER | |
665 | struct { | |
666 | jit_data_info_t *ptr; | |
667 | jit_word_t offset; | |
668 | jit_word_t length; | |
669 | } data_info; /* constant pools information */ | |
670 | # endif | |
671 | /* Note that this field is somewhat hackish, but required by most | |
672 | * ways to implement jit, unless implementing a pure one function | |
673 | * per jit, as most times it needs to start the jit buffer with a | |
674 | * jump where the "main" prolog starts, and because the initial | |
675 | * code is in "arm mode", need to make an "arm mode" patch on that | |
676 | * jump. A good example is the test suite assembler, where most | |
677 | * test cases start with a "jmpi main" call. */ | |
678 | jit_uword_t thumb; | |
679 | struct { | |
680 | jit_uint8_t *data; /* pointer to code */ | |
681 | jit_word_t size; /* size data */ | |
682 | jit_word_t offset; /* pending patches */ | |
683 | jit_word_t length; /* number of pending constants */ | |
684 | jit_int32_t values[1024]; /* pending constants */ | |
685 | jit_word_t patches[2048]; | |
686 | } consts; | |
687 | #elif (__powerpc__ && _CALL_AIXDESC) || __ia64__ | |
688 | /* Keep track of prolog addresses, just for the sake of making | |
689 | * jit that starts with a jump to a "main" label work like other | |
690 | * backends. */ | |
691 | struct { | |
692 | jit_word_t *ptr; | |
693 | jit_word_t offset; | |
694 | jit_word_t length; | |
695 | } prolog; | |
696 | jit_bool_t jump; | |
c0c16242 PC |
697 | #elif __riscv && __WORDSIZE == 64 |
698 | struct { | |
699 | /* Hash table for constants to be resolved and patched */ | |
700 | struct { | |
701 | jit_const_t **table; /* very simple hash table */ | |
702 | jit_word_t size; /* number of vectors in table */ | |
703 | jit_word_t count; /* number of distinct entries */ | |
704 | } hash; | |
705 | struct { | |
706 | jit_const_t **ptr; /* keep a single pointer */ | |
707 | jit_const_t *list; /* free list */ | |
708 | jit_word_t length; /* length of pool */ | |
709 | } pool; | |
710 | /* Linear list for constants that cannot be encoded easily */ | |
711 | struct { | |
712 | jit_word_t *instrs; /* list of direct movi instructions */ | |
713 | jit_word_t *values; /* list of direct movi constants */ | |
714 | jit_word_t offset; /* offset in instrs/values vector */ | |
715 | jit_word_t length; /* length of instrs/values vector */ | |
716 | } vector; | |
717 | } consts; | |
4a71579b PC |
718 | #endif |
719 | #if GET_JIT_SIZE | |
720 | /* Temporary storage to calculate instructions length */ | |
721 | jit_word_t size; | |
722 | /* Global flag for code buffer heuristic size computation */ | |
723 | jit_word_t mult; | |
724 | /* Pointer to code to prevent miscalculation if reallocating buffer */ | |
725 | jit_uint8_t *cptr; | |
726 | #endif | |
727 | }; | |
728 | ||
729 | #define _jitc _jit->comp | |
730 | struct jit_state { | |
731 | union { | |
732 | jit_uint8_t *uc; | |
733 | jit_uint16_t *us; | |
734 | jit_uint32_t *ui; | |
735 | jit_uint64_t *ul; | |
736 | jit_word_t w; | |
737 | } pc; | |
738 | struct { | |
739 | jit_uint8_t *ptr; | |
740 | jit_word_t length; | |
79bfeef6 PC |
741 | /* PROTECTED bytes starting at PTR are mprotect'd. */ |
742 | jit_word_t protected; | |
4a71579b PC |
743 | } code; |
744 | struct { | |
745 | jit_uint8_t *ptr; | |
746 | jit_word_t length; | |
747 | } data; | |
748 | struct { | |
749 | jit_note_t *ptr; | |
750 | jit_word_t length; | |
751 | } note; | |
752 | jit_compiler_t *comp; | |
753 | /* Flags to know if user did set the code and data buffers */ | |
754 | jit_uint32_t user_code : 1; | |
755 | jit_uint32_t user_data : 1; | |
756 | }; | |
757 | ||
758 | struct jit_register { | |
759 | jit_reg_t spec; | |
760 | char *name; | |
761 | }; | |
762 | ||
763 | /* | |
764 | * Prototypes | |
765 | */ | |
766 | extern void jit_get_cpu(void); | |
767 | ||
768 | #define jit_init() _jit_init(_jit) | |
769 | extern void _jit_init(jit_state_t*); | |
770 | ||
771 | #define jit_synth_inc() _jit_synth_inc(_jit) | |
772 | extern void _jit_synth_inc(jit_state_t*); | |
773 | ||
774 | #define jit_new_node_no_link(u) _jit_new_node_no_link(_jit, u) | |
775 | extern jit_node_t *_jit_new_node_no_link(jit_state_t*, jit_code_t); | |
776 | ||
777 | #define jit_link_node(u) _jit_link_node(_jit, u) | |
778 | extern void _jit_link_node(jit_state_t*, jit_node_t*); | |
779 | ||
780 | #define jit_link_label(l) _jit_link_label(_jit,l) | |
781 | extern void | |
782 | _jit_link_label(jit_state_t*,jit_node_t*); | |
783 | ||
784 | #define jit_synth_dec() _jit_synth_dec(_jit) | |
785 | extern void _jit_synth_dec(jit_state_t*); | |
786 | ||
787 | #define jit_reglive(node) _jit_reglive(_jit, node) | |
788 | extern void | |
789 | _jit_reglive(jit_state_t*, jit_node_t*); | |
790 | ||
791 | #define jit_regarg_set(n,v) _jit_regarg_set(_jit,n,v) | |
792 | extern void | |
793 | _jit_regarg_set(jit_state_t*, jit_node_t*, jit_int32_t); | |
794 | ||
795 | #define jit_regarg_clr(n,v) _jit_regarg_clr(_jit,n,v) | |
796 | extern void | |
797 | _jit_regarg_clr(jit_state_t*, jit_node_t*, jit_int32_t); | |
798 | ||
4a71579b PC |
799 | #define jit_save(reg) _jit_save(_jit, reg) |
800 | extern void | |
801 | _jit_save(jit_state_t*, jit_int32_t); | |
802 | ||
803 | #define jit_load(reg) _jit_load(_jit, reg) | |
804 | extern void | |
805 | _jit_load(jit_state_t*, jit_int32_t); | |
806 | ||
807 | #define jit_trampoline(u,v) _jit_trampoline(_jit, u, v) | |
808 | extern void _jit_trampoline(jit_state_t*, jit_int32_t, jit_bool_t); | |
809 | ||
810 | #define jit_optimize() _jit_optimize(_jit) | |
811 | extern void | |
812 | _jit_optimize(jit_state_t*); | |
813 | ||
814 | #define jit_classify(code) _jit_classify(_jit, code) | |
815 | extern jit_int32_t | |
816 | _jit_classify(jit_state_t*, jit_code_t); | |
817 | ||
818 | #define jit_regarg_p(n, r) _jit_regarg_p(_jit, n, r) | |
819 | extern jit_bool_t | |
820 | _jit_regarg_p(jit_state_t*, jit_node_t*, jit_int32_t); | |
821 | ||
822 | #define emit_code() _emit_code(_jit) | |
823 | extern jit_pointer_t | |
824 | _emit_code(jit_state_t*); | |
825 | ||
826 | extern void | |
827 | jit_flush(void *fptr, void *tptr); | |
828 | ||
829 | #define emit_ldxi(r0, r1, i0) _emit_ldxi(_jit, r0, r1, i0) | |
830 | extern void | |
831 | _emit_ldxi(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t); | |
832 | ||
833 | #define emit_stxi(i0, r0, r1) _emit_stxi(_jit, i0, r0, r1) | |
834 | extern void | |
835 | _emit_stxi(jit_state_t*, jit_word_t, jit_int32_t, jit_int32_t); | |
836 | ||
837 | #define emit_ldxi_d(r0, r1, i0) _emit_ldxi_d(_jit, r0, r1, i0) | |
838 | extern void | |
839 | _emit_ldxi_d(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t); | |
840 | ||
841 | #define emit_stxi_d(i0, r0, r1) _emit_stxi_d(_jit, i0, r0, r1) | |
842 | extern void | |
843 | _emit_stxi_d(jit_state_t*, jit_word_t, jit_int32_t, jit_int32_t); | |
844 | ||
40a44dcb | 845 | extern void jit_init_print(void); |
4a71579b PC |
846 | extern void jit_init_debug(const char*); |
847 | extern void jit_finish_debug(void); | |
848 | ||
849 | extern void jit_init_note(void); | |
850 | extern void jit_finish_note(void); | |
851 | #define jit_set_note(n,u,v,w) _jit_set_note(_jit, n, u, v, w) | |
852 | extern void _jit_set_note(jit_state_t*, jit_note_t*, char*, int, jit_int32_t); | |
853 | #define jit_annotate() _jit_annotate(_jit) | |
854 | extern void _jit_annotate(jit_state_t*); | |
855 | ||
856 | #define jit_print_node(u) _jit_print_node(_jit,u) | |
857 | extern void _jit_print_node(jit_state_t*,jit_node_t*); | |
858 | ||
859 | extern jit_pointer_t jit_memcpy(jit_pointer_t,const void*,jit_word_t); | |
860 | extern jit_pointer_t jit_memmove(jit_pointer_t,const void*,jit_word_t); | |
861 | extern void jit_alloc(jit_pointer_t*, jit_word_t); | |
862 | extern void jit_realloc(jit_pointer_t*, jit_word_t, jit_word_t); | |
863 | void jit_free(jit_pointer_t*); | |
864 | ||
865 | extern void jit_init_size(void); | |
866 | extern void jit_finish_size(void); | |
867 | ||
868 | #if GET_JIT_SIZE | |
869 | # define jit_size_prepare() _jit_size_prepare(_jit) | |
870 | extern void | |
871 | _jit_size_prepare(jit_state_t*); | |
872 | ||
873 | # define jit_size_collect(node) _jit_size_collect(_jit, node) | |
874 | extern void | |
875 | _jit_size_collect(jit_state_t*, jit_node_t*); | |
876 | #else | |
877 | # define jit_get_size() _jit_get_size(_jit) | |
878 | extern jit_word_t | |
879 | _jit_get_size(jit_state_t*); | |
880 | #endif | |
881 | ||
882 | extern jit_word_t | |
883 | jit_get_max_instr(void); | |
884 | ||
885 | /* | |
886 | * Externs | |
887 | */ | |
888 | extern jit_register_t _rvs[]; | |
889 | ||
890 | #endif /* _jit_private_h */ |