]>
Commit | Line | Data |
---|---|---|
c896fe29 FB |
1 | /* |
2 | * Tiny Code Generator for QEMU | |
3 | * | |
4 | * Copyright (c) 2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
c896fe29 | 25 | /* define it to use liveness analysis (better code) */ |
8f2e8c07 | 26 | #define USE_TCG_OPTIMIZATIONS |
c896fe29 | 27 | |
757e725b | 28 | #include "qemu/osdep.h" |
cca82982 | 29 | |
813da627 RH |
30 | /* Define to jump the ELF file used to communicate with GDB. */ |
31 | #undef DEBUG_JIT | |
32 | ||
72fd2efb | 33 | #include "qemu/error-report.h" |
f348b6d1 | 34 | #include "qemu/cutils.h" |
1de7afc9 PB |
35 | #include "qemu/host-utils.h" |
36 | #include "qemu/timer.h" | |
c896fe29 | 37 | |
c5d3c498 | 38 | /* Note: the long term plan is to reduce the dependencies on the QEMU |
c896fe29 FB |
39 | CPU definitions. Currently they are used for qemu_ld/st |
40 | instructions */ | |
41 | #define NO_CPU_IO_DEFS | |
42 | #include "cpu.h" | |
c896fe29 | 43 | |
63c91552 PB |
44 | #include "exec/cpu-common.h" |
45 | #include "exec/exec-all.h" | |
46 | ||
c896fe29 | 47 | #include "tcg-op.h" |
813da627 | 48 | |
edee2579 | 49 | #if UINTPTR_MAX == UINT32_MAX |
813da627 | 50 | # define ELF_CLASS ELFCLASS32 |
edee2579 RH |
51 | #else |
52 | # define ELF_CLASS ELFCLASS64 | |
813da627 RH |
53 | #endif |
54 | #ifdef HOST_WORDS_BIGENDIAN | |
55 | # define ELF_DATA ELFDATA2MSB | |
56 | #else | |
57 | # define ELF_DATA ELFDATA2LSB | |
58 | #endif | |
59 | ||
c896fe29 | 60 | #include "elf.h" |
508127e2 | 61 | #include "exec/log.h" |
3468b59e | 62 | #include "sysemu/sysemu.h" |
c896fe29 | 63 | |
ce151109 PM |
64 | /* Forward declarations for functions declared in tcg-target.inc.c and |
65 | used here. */ | |
e4d58b41 | 66 | static void tcg_target_init(TCGContext *s); |
f69d277e | 67 | static const TCGTargetOpDef *tcg_target_op_def(TCGOpcode); |
e4d58b41 | 68 | static void tcg_target_qemu_prologue(TCGContext *s); |
6ac17786 | 69 | static bool patch_reloc(tcg_insn_unit *code_ptr, int type, |
2ba7fae2 | 70 | intptr_t value, intptr_t addend); |
c896fe29 | 71 | |
497a22eb RH |
72 | /* The CIE and FDE header definitions will be common to all hosts. */ |
73 | typedef struct { | |
74 | uint32_t len __attribute__((aligned((sizeof(void *))))); | |
75 | uint32_t id; | |
76 | uint8_t version; | |
77 | char augmentation[1]; | |
78 | uint8_t code_align; | |
79 | uint8_t data_align; | |
80 | uint8_t return_column; | |
81 | } DebugFrameCIE; | |
82 | ||
83 | typedef struct QEMU_PACKED { | |
84 | uint32_t len __attribute__((aligned((sizeof(void *))))); | |
85 | uint32_t cie_offset; | |
edee2579 RH |
86 | uintptr_t func_start; |
87 | uintptr_t func_len; | |
497a22eb RH |
88 | } DebugFrameFDEHeader; |
89 | ||
2c90784a RH |
90 | typedef struct QEMU_PACKED { |
91 | DebugFrameCIE cie; | |
92 | DebugFrameFDEHeader fde; | |
93 | } DebugFrameHeader; | |
94 | ||
813da627 | 95 | static void tcg_register_jit_int(void *buf, size_t size, |
2c90784a RH |
96 | const void *debug_frame, |
97 | size_t debug_frame_size) | |
813da627 RH |
98 | __attribute__((unused)); |
99 | ||
ce151109 | 100 | /* Forward declarations for functions declared and used in tcg-target.inc.c. */ |
069ea736 RH |
101 | static const char *target_parse_constraint(TCGArgConstraint *ct, |
102 | const char *ct_str, TCGType type); | |
2a534aff | 103 | static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, |
a05b5b9b | 104 | intptr_t arg2); |
2a534aff | 105 | static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg); |
c0ad3001 | 106 | static void tcg_out_movi(TCGContext *s, TCGType type, |
2a534aff | 107 | TCGReg ret, tcg_target_long arg); |
c0ad3001 SW |
108 | static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args, |
109 | const int *const_args); | |
d2fd745f RH |
110 | #if TCG_TARGET_MAYBE_vec |
111 | static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl, | |
112 | unsigned vece, const TCGArg *args, | |
113 | const int *const_args); | |
114 | #else | |
115 | static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl, | |
116 | unsigned vece, const TCGArg *args, | |
117 | const int *const_args) | |
118 | { | |
119 | g_assert_not_reached(); | |
120 | } | |
121 | #endif | |
2a534aff | 122 | static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1, |
a05b5b9b | 123 | intptr_t arg2); |
59d7c14e RH |
124 | static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, |
125 | TCGReg base, intptr_t ofs); | |
cf066674 | 126 | static void tcg_out_call(TCGContext *s, tcg_insn_unit *target); |
f6c6afc1 | 127 | static int tcg_target_const_match(tcg_target_long val, TCGType type, |
c0ad3001 | 128 | const TCGArgConstraint *arg_ct); |
659ef5cb RH |
129 | #ifdef TCG_TARGET_NEED_LDST_LABELS |
130 | static bool tcg_out_ldst_finalize(TCGContext *s); | |
131 | #endif | |
c896fe29 | 132 | |
a505785c EC |
133 | #define TCG_HIGHWATER 1024 |
134 | ||
df2cce29 EC |
135 | static TCGContext **tcg_ctxs; |
136 | static unsigned int n_tcg_ctxs; | |
1c2adb95 | 137 | TCGv_env cpu_env = 0; |
df2cce29 | 138 | |
be2cdc5e EC |
139 | struct tcg_region_tree { |
140 | QemuMutex lock; | |
141 | GTree *tree; | |
142 | /* padding to avoid false sharing is computed at run-time */ | |
143 | }; | |
144 | ||
e8feb96f EC |
145 | /* |
146 | * We divide code_gen_buffer into equally-sized "regions" that TCG threads | |
147 | * dynamically allocate from as demand dictates. Given appropriate region | |
148 | * sizing, this minimizes flushes even when some TCG threads generate a lot | |
149 | * more code than others. | |
150 | */ | |
151 | struct tcg_region_state { | |
152 | QemuMutex lock; | |
153 | ||
154 | /* fields set at init time */ | |
155 | void *start; | |
156 | void *start_aligned; | |
157 | void *end; | |
158 | size_t n; | |
159 | size_t size; /* size of one region */ | |
160 | size_t stride; /* .size + guard size */ | |
161 | ||
162 | /* fields protected by the lock */ | |
163 | size_t current; /* current region index */ | |
164 | size_t agg_size_full; /* aggregate size of full regions */ | |
165 | }; | |
166 | ||
167 | static struct tcg_region_state region; | |
be2cdc5e EC |
168 | /* |
169 | * This is an array of struct tcg_region_tree's, with padding. | |
170 | * We use void * to simplify the computation of region_trees[i]; each | |
171 | * struct is found every tree_size bytes. | |
172 | */ | |
173 | static void *region_trees; | |
174 | static size_t tree_size; | |
d2fd745f | 175 | static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT]; |
b1d8e52e | 176 | static TCGRegSet tcg_target_call_clobber_regs; |
c896fe29 | 177 | |
1813e175 | 178 | #if TCG_TARGET_INSN_UNIT_SIZE == 1 |
4196dca6 | 179 | static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v) |
c896fe29 FB |
180 | { |
181 | *s->code_ptr++ = v; | |
182 | } | |
183 | ||
4196dca6 PM |
184 | static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p, |
185 | uint8_t v) | |
5c53bb81 | 186 | { |
1813e175 | 187 | *p = v; |
5c53bb81 | 188 | } |
1813e175 | 189 | #endif |
5c53bb81 | 190 | |
1813e175 | 191 | #if TCG_TARGET_INSN_UNIT_SIZE <= 2 |
4196dca6 | 192 | static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v) |
c896fe29 | 193 | { |
1813e175 RH |
194 | if (TCG_TARGET_INSN_UNIT_SIZE == 2) { |
195 | *s->code_ptr++ = v; | |
196 | } else { | |
197 | tcg_insn_unit *p = s->code_ptr; | |
198 | memcpy(p, &v, sizeof(v)); | |
199 | s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE); | |
200 | } | |
c896fe29 FB |
201 | } |
202 | ||
4196dca6 PM |
203 | static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p, |
204 | uint16_t v) | |
5c53bb81 | 205 | { |
1813e175 RH |
206 | if (TCG_TARGET_INSN_UNIT_SIZE == 2) { |
207 | *p = v; | |
208 | } else { | |
209 | memcpy(p, &v, sizeof(v)); | |
210 | } | |
5c53bb81 | 211 | } |
1813e175 | 212 | #endif |
5c53bb81 | 213 | |
1813e175 | 214 | #if TCG_TARGET_INSN_UNIT_SIZE <= 4 |
4196dca6 | 215 | static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v) |
c896fe29 | 216 | { |
1813e175 RH |
217 | if (TCG_TARGET_INSN_UNIT_SIZE == 4) { |
218 | *s->code_ptr++ = v; | |
219 | } else { | |
220 | tcg_insn_unit *p = s->code_ptr; | |
221 | memcpy(p, &v, sizeof(v)); | |
222 | s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE); | |
223 | } | |
c896fe29 FB |
224 | } |
225 | ||
4196dca6 PM |
226 | static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p, |
227 | uint32_t v) | |
5c53bb81 | 228 | { |
1813e175 RH |
229 | if (TCG_TARGET_INSN_UNIT_SIZE == 4) { |
230 | *p = v; | |
231 | } else { | |
232 | memcpy(p, &v, sizeof(v)); | |
233 | } | |
5c53bb81 | 234 | } |
1813e175 | 235 | #endif |
5c53bb81 | 236 | |
1813e175 | 237 | #if TCG_TARGET_INSN_UNIT_SIZE <= 8 |
4196dca6 | 238 | static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v) |
ac26eb69 | 239 | { |
1813e175 RH |
240 | if (TCG_TARGET_INSN_UNIT_SIZE == 8) { |
241 | *s->code_ptr++ = v; | |
242 | } else { | |
243 | tcg_insn_unit *p = s->code_ptr; | |
244 | memcpy(p, &v, sizeof(v)); | |
245 | s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE); | |
246 | } | |
ac26eb69 RH |
247 | } |
248 | ||
4196dca6 PM |
249 | static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p, |
250 | uint64_t v) | |
5c53bb81 | 251 | { |
1813e175 RH |
252 | if (TCG_TARGET_INSN_UNIT_SIZE == 8) { |
253 | *p = v; | |
254 | } else { | |
255 | memcpy(p, &v, sizeof(v)); | |
256 | } | |
5c53bb81 | 257 | } |
1813e175 | 258 | #endif |
5c53bb81 | 259 | |
c896fe29 FB |
260 | /* label relocation processing */ |
261 | ||
1813e175 | 262 | static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type, |
bec16311 | 263 | TCGLabel *l, intptr_t addend) |
c896fe29 | 264 | { |
c896fe29 FB |
265 | TCGRelocation *r; |
266 | ||
c896fe29 | 267 | if (l->has_value) { |
623e265c PB |
268 | /* FIXME: This may break relocations on RISC targets that |
269 | modify instruction fields in place. The caller may not have | |
270 | written the initial value. */ | |
6ac17786 RH |
271 | bool ok = patch_reloc(code_ptr, type, l->u.value, addend); |
272 | tcg_debug_assert(ok); | |
c896fe29 FB |
273 | } else { |
274 | /* add a new relocation entry */ | |
275 | r = tcg_malloc(sizeof(TCGRelocation)); | |
276 | r->type = type; | |
277 | r->ptr = code_ptr; | |
278 | r->addend = addend; | |
279 | r->next = l->u.first_reloc; | |
280 | l->u.first_reloc = r; | |
281 | } | |
282 | } | |
283 | ||
bec16311 | 284 | static void tcg_out_label(TCGContext *s, TCGLabel *l, tcg_insn_unit *ptr) |
c896fe29 | 285 | { |
2ba7fae2 | 286 | intptr_t value = (intptr_t)ptr; |
1813e175 | 287 | TCGRelocation *r; |
c896fe29 | 288 | |
eabb7b91 | 289 | tcg_debug_assert(!l->has_value); |
1813e175 RH |
290 | |
291 | for (r = l->u.first_reloc; r != NULL; r = r->next) { | |
6ac17786 RH |
292 | bool ok = patch_reloc(r->ptr, r->type, value, r->addend); |
293 | tcg_debug_assert(ok); | |
c896fe29 | 294 | } |
1813e175 | 295 | |
c896fe29 | 296 | l->has_value = 1; |
1813e175 | 297 | l->u.value_ptr = ptr; |
c896fe29 FB |
298 | } |
299 | ||
42a268c2 | 300 | TCGLabel *gen_new_label(void) |
c896fe29 | 301 | { |
b1311c4a | 302 | TCGContext *s = tcg_ctx; |
51e3972c | 303 | TCGLabel *l = tcg_malloc(sizeof(TCGLabel)); |
c896fe29 | 304 | |
51e3972c RH |
305 | *l = (TCGLabel){ |
306 | .id = s->nb_labels++ | |
307 | }; | |
42a268c2 RH |
308 | |
309 | return l; | |
c896fe29 FB |
310 | } |
311 | ||
9f754620 RH |
312 | static void set_jmp_reset_offset(TCGContext *s, int which) |
313 | { | |
314 | size_t off = tcg_current_code_size(s); | |
315 | s->tb_jmp_reset_offset[which] = off; | |
316 | /* Make sure that we didn't overflow the stored offset. */ | |
317 | assert(s->tb_jmp_reset_offset[which] == off); | |
318 | } | |
319 | ||
ce151109 | 320 | #include "tcg-target.inc.c" |
c896fe29 | 321 | |
be2cdc5e EC |
322 | /* compare a pointer @ptr and a tb_tc @s */ |
323 | static int ptr_cmp_tb_tc(const void *ptr, const struct tb_tc *s) | |
324 | { | |
325 | if (ptr >= s->ptr + s->size) { | |
326 | return 1; | |
327 | } else if (ptr < s->ptr) { | |
328 | return -1; | |
329 | } | |
330 | return 0; | |
331 | } | |
332 | ||
333 | static gint tb_tc_cmp(gconstpointer ap, gconstpointer bp) | |
334 | { | |
335 | const struct tb_tc *a = ap; | |
336 | const struct tb_tc *b = bp; | |
337 | ||
338 | /* | |
339 | * When both sizes are set, we know this isn't a lookup. | |
340 | * This is the most likely case: every TB must be inserted; lookups | |
341 | * are a lot less frequent. | |
342 | */ | |
343 | if (likely(a->size && b->size)) { | |
344 | if (a->ptr > b->ptr) { | |
345 | return 1; | |
346 | } else if (a->ptr < b->ptr) { | |
347 | return -1; | |
348 | } | |
349 | /* a->ptr == b->ptr should happen only on deletions */ | |
350 | g_assert(a->size == b->size); | |
351 | return 0; | |
352 | } | |
353 | /* | |
354 | * All lookups have either .size field set to 0. | |
355 | * From the glib sources we see that @ap is always the lookup key. However | |
356 | * the docs provide no guarantee, so we just mark this case as likely. | |
357 | */ | |
358 | if (likely(a->size == 0)) { | |
359 | return ptr_cmp_tb_tc(a->ptr, b); | |
360 | } | |
361 | return ptr_cmp_tb_tc(b->ptr, a); | |
362 | } | |
363 | ||
364 | static void tcg_region_trees_init(void) | |
365 | { | |
366 | size_t i; | |
367 | ||
368 | tree_size = ROUND_UP(sizeof(struct tcg_region_tree), qemu_dcache_linesize); | |
369 | region_trees = qemu_memalign(qemu_dcache_linesize, region.n * tree_size); | |
370 | for (i = 0; i < region.n; i++) { | |
371 | struct tcg_region_tree *rt = region_trees + i * tree_size; | |
372 | ||
373 | qemu_mutex_init(&rt->lock); | |
374 | rt->tree = g_tree_new(tb_tc_cmp); | |
375 | } | |
376 | } | |
377 | ||
378 | static struct tcg_region_tree *tc_ptr_to_region_tree(void *p) | |
379 | { | |
380 | size_t region_idx; | |
381 | ||
382 | if (p < region.start_aligned) { | |
383 | region_idx = 0; | |
384 | } else { | |
385 | ptrdiff_t offset = p - region.start_aligned; | |
386 | ||
387 | if (offset > region.stride * (region.n - 1)) { | |
388 | region_idx = region.n - 1; | |
389 | } else { | |
390 | region_idx = offset / region.stride; | |
391 | } | |
392 | } | |
393 | return region_trees + region_idx * tree_size; | |
394 | } | |
395 | ||
396 | void tcg_tb_insert(TranslationBlock *tb) | |
397 | { | |
398 | struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr); | |
399 | ||
400 | qemu_mutex_lock(&rt->lock); | |
401 | g_tree_insert(rt->tree, &tb->tc, tb); | |
402 | qemu_mutex_unlock(&rt->lock); | |
403 | } | |
404 | ||
405 | void tcg_tb_remove(TranslationBlock *tb) | |
406 | { | |
407 | struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr); | |
408 | ||
409 | qemu_mutex_lock(&rt->lock); | |
410 | g_tree_remove(rt->tree, &tb->tc); | |
411 | qemu_mutex_unlock(&rt->lock); | |
412 | } | |
413 | ||
414 | /* | |
415 | * Find the TB 'tb' such that | |
416 | * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size | |
417 | * Return NULL if not found. | |
418 | */ | |
419 | TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr) | |
420 | { | |
421 | struct tcg_region_tree *rt = tc_ptr_to_region_tree((void *)tc_ptr); | |
422 | TranslationBlock *tb; | |
423 | struct tb_tc s = { .ptr = (void *)tc_ptr }; | |
424 | ||
425 | qemu_mutex_lock(&rt->lock); | |
426 | tb = g_tree_lookup(rt->tree, &s); | |
427 | qemu_mutex_unlock(&rt->lock); | |
428 | return tb; | |
429 | } | |
430 | ||
431 | static void tcg_region_tree_lock_all(void) | |
432 | { | |
433 | size_t i; | |
434 | ||
435 | for (i = 0; i < region.n; i++) { | |
436 | struct tcg_region_tree *rt = region_trees + i * tree_size; | |
437 | ||
438 | qemu_mutex_lock(&rt->lock); | |
439 | } | |
440 | } | |
441 | ||
442 | static void tcg_region_tree_unlock_all(void) | |
443 | { | |
444 | size_t i; | |
445 | ||
446 | for (i = 0; i < region.n; i++) { | |
447 | struct tcg_region_tree *rt = region_trees + i * tree_size; | |
448 | ||
449 | qemu_mutex_unlock(&rt->lock); | |
450 | } | |
451 | } | |
452 | ||
453 | void tcg_tb_foreach(GTraverseFunc func, gpointer user_data) | |
454 | { | |
455 | size_t i; | |
456 | ||
457 | tcg_region_tree_lock_all(); | |
458 | for (i = 0; i < region.n; i++) { | |
459 | struct tcg_region_tree *rt = region_trees + i * tree_size; | |
460 | ||
461 | g_tree_foreach(rt->tree, func, user_data); | |
462 | } | |
463 | tcg_region_tree_unlock_all(); | |
464 | } | |
465 | ||
466 | size_t tcg_nb_tbs(void) | |
467 | { | |
468 | size_t nb_tbs = 0; | |
469 | size_t i; | |
470 | ||
471 | tcg_region_tree_lock_all(); | |
472 | for (i = 0; i < region.n; i++) { | |
473 | struct tcg_region_tree *rt = region_trees + i * tree_size; | |
474 | ||
475 | nb_tbs += g_tree_nnodes(rt->tree); | |
476 | } | |
477 | tcg_region_tree_unlock_all(); | |
478 | return nb_tbs; | |
479 | } | |
480 | ||
481 | static void tcg_region_tree_reset_all(void) | |
482 | { | |
483 | size_t i; | |
484 | ||
485 | tcg_region_tree_lock_all(); | |
486 | for (i = 0; i < region.n; i++) { | |
487 | struct tcg_region_tree *rt = region_trees + i * tree_size; | |
488 | ||
489 | /* Increment the refcount first so that destroy acts as a reset */ | |
490 | g_tree_ref(rt->tree); | |
491 | g_tree_destroy(rt->tree); | |
492 | } | |
493 | tcg_region_tree_unlock_all(); | |
494 | } | |
495 | ||
e8feb96f EC |
496 | static void tcg_region_bounds(size_t curr_region, void **pstart, void **pend) |
497 | { | |
498 | void *start, *end; | |
499 | ||
500 | start = region.start_aligned + curr_region * region.stride; | |
501 | end = start + region.size; | |
502 | ||
503 | if (curr_region == 0) { | |
504 | start = region.start; | |
505 | } | |
506 | if (curr_region == region.n - 1) { | |
507 | end = region.end; | |
508 | } | |
509 | ||
510 | *pstart = start; | |
511 | *pend = end; | |
512 | } | |
513 | ||
514 | static void tcg_region_assign(TCGContext *s, size_t curr_region) | |
515 | { | |
516 | void *start, *end; | |
517 | ||
518 | tcg_region_bounds(curr_region, &start, &end); | |
519 | ||
520 | s->code_gen_buffer = start; | |
521 | s->code_gen_ptr = start; | |
522 | s->code_gen_buffer_size = end - start; | |
523 | s->code_gen_highwater = end - TCG_HIGHWATER; | |
524 | } | |
525 | ||
526 | static bool tcg_region_alloc__locked(TCGContext *s) | |
527 | { | |
528 | if (region.current == region.n) { | |
529 | return true; | |
530 | } | |
531 | tcg_region_assign(s, region.current); | |
532 | region.current++; | |
533 | return false; | |
534 | } | |
535 | ||
536 | /* | |
537 | * Request a new region once the one in use has filled up. | |
538 | * Returns true on error. | |
539 | */ | |
540 | static bool tcg_region_alloc(TCGContext *s) | |
541 | { | |
542 | bool err; | |
543 | /* read the region size now; alloc__locked will overwrite it on success */ | |
544 | size_t size_full = s->code_gen_buffer_size; | |
545 | ||
546 | qemu_mutex_lock(®ion.lock); | |
547 | err = tcg_region_alloc__locked(s); | |
548 | if (!err) { | |
549 | region.agg_size_full += size_full - TCG_HIGHWATER; | |
550 | } | |
551 | qemu_mutex_unlock(®ion.lock); | |
552 | return err; | |
553 | } | |
554 | ||
555 | /* | |
556 | * Perform a context's first region allocation. | |
557 | * This function does _not_ increment region.agg_size_full. | |
558 | */ | |
559 | static inline bool tcg_region_initial_alloc__locked(TCGContext *s) | |
560 | { | |
561 | return tcg_region_alloc__locked(s); | |
562 | } | |
563 | ||
564 | /* Call from a safe-work context */ | |
565 | void tcg_region_reset_all(void) | |
566 | { | |
3468b59e | 567 | unsigned int n_ctxs = atomic_read(&n_tcg_ctxs); |
e8feb96f EC |
568 | unsigned int i; |
569 | ||
570 | qemu_mutex_lock(®ion.lock); | |
571 | region.current = 0; | |
572 | region.agg_size_full = 0; | |
573 | ||
3468b59e EC |
574 | for (i = 0; i < n_ctxs; i++) { |
575 | TCGContext *s = atomic_read(&tcg_ctxs[i]); | |
576 | bool err = tcg_region_initial_alloc__locked(s); | |
e8feb96f EC |
577 | |
578 | g_assert(!err); | |
579 | } | |
580 | qemu_mutex_unlock(®ion.lock); | |
be2cdc5e EC |
581 | |
582 | tcg_region_tree_reset_all(); | |
e8feb96f EC |
583 | } |
584 | ||
3468b59e EC |
585 | #ifdef CONFIG_USER_ONLY |
586 | static size_t tcg_n_regions(void) | |
587 | { | |
588 | return 1; | |
589 | } | |
590 | #else | |
591 | /* | |
592 | * It is likely that some vCPUs will translate more code than others, so we | |
593 | * first try to set more regions than max_cpus, with those regions being of | |
594 | * reasonable size. If that's not possible we make do by evenly dividing | |
595 | * the code_gen_buffer among the vCPUs. | |
596 | */ | |
597 | static size_t tcg_n_regions(void) | |
598 | { | |
599 | size_t i; | |
600 | ||
601 | /* Use a single region if all we have is one vCPU thread */ | |
602 | if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) { | |
603 | return 1; | |
604 | } | |
605 | ||
606 | /* Try to have more regions than max_cpus, with each region being >= 2 MB */ | |
607 | for (i = 8; i > 0; i--) { | |
608 | size_t regions_per_thread = i; | |
609 | size_t region_size; | |
610 | ||
611 | region_size = tcg_init_ctx.code_gen_buffer_size; | |
612 | region_size /= max_cpus * regions_per_thread; | |
613 | ||
614 | if (region_size >= 2 * 1024u * 1024) { | |
615 | return max_cpus * regions_per_thread; | |
616 | } | |
617 | } | |
618 | /* If we can't, then just allocate one region per vCPU thread */ | |
619 | return max_cpus; | |
620 | } | |
621 | #endif | |
622 | ||
e8feb96f EC |
623 | /* |
624 | * Initializes region partitioning. | |
625 | * | |
626 | * Called at init time from the parent thread (i.e. the one calling | |
627 | * tcg_context_init), after the target's TCG globals have been set. | |
3468b59e EC |
628 | * |
629 | * Region partitioning works by splitting code_gen_buffer into separate regions, | |
630 | * and then assigning regions to TCG threads so that the threads can translate | |
631 | * code in parallel without synchronization. | |
632 | * | |
633 | * In softmmu the number of TCG threads is bounded by max_cpus, so we use at | |
634 | * least max_cpus regions in MTTCG. In !MTTCG we use a single region. | |
635 | * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...]) | |
636 | * must have been parsed before calling this function, since it calls | |
637 | * qemu_tcg_mttcg_enabled(). | |
638 | * | |
639 | * In user-mode we use a single region. Having multiple regions in user-mode | |
640 | * is not supported, because the number of vCPU threads (recall that each thread | |
641 | * spawned by the guest corresponds to a vCPU thread) is only bounded by the | |
642 | * OS, and usually this number is huge (tens of thousands is not uncommon). | |
643 | * Thus, given this large bound on the number of vCPU threads and the fact | |
644 | * that code_gen_buffer is allocated at compile-time, we cannot guarantee | |
645 | * that the availability of at least one region per vCPU thread. | |
646 | * | |
647 | * However, this user-mode limitation is unlikely to be a significant problem | |
648 | * in practice. Multi-threaded guests share most if not all of their translated | |
649 | * code, which makes parallel code generation less appealing than in softmmu. | |
e8feb96f EC |
650 | */ |
651 | void tcg_region_init(void) | |
652 | { | |
653 | void *buf = tcg_init_ctx.code_gen_buffer; | |
654 | void *aligned; | |
655 | size_t size = tcg_init_ctx.code_gen_buffer_size; | |
656 | size_t page_size = qemu_real_host_page_size; | |
657 | size_t region_size; | |
658 | size_t n_regions; | |
659 | size_t i; | |
660 | ||
3468b59e | 661 | n_regions = tcg_n_regions(); |
e8feb96f EC |
662 | |
663 | /* The first region will be 'aligned - buf' bytes larger than the others */ | |
664 | aligned = QEMU_ALIGN_PTR_UP(buf, page_size); | |
665 | g_assert(aligned < tcg_init_ctx.code_gen_buffer + size); | |
666 | /* | |
667 | * Make region_size a multiple of page_size, using aligned as the start. | |
668 | * As a result of this we might end up with a few extra pages at the end of | |
669 | * the buffer; we will assign those to the last region. | |
670 | */ | |
671 | region_size = (size - (aligned - buf)) / n_regions; | |
672 | region_size = QEMU_ALIGN_DOWN(region_size, page_size); | |
673 | ||
674 | /* A region must have at least 2 pages; one code, one guard */ | |
675 | g_assert(region_size >= 2 * page_size); | |
676 | ||
677 | /* init the region struct */ | |
678 | qemu_mutex_init(®ion.lock); | |
679 | region.n = n_regions; | |
680 | region.size = region_size - page_size; | |
681 | region.stride = region_size; | |
682 | region.start = buf; | |
683 | region.start_aligned = aligned; | |
684 | /* page-align the end, since its last page will be a guard page */ | |
685 | region.end = QEMU_ALIGN_PTR_DOWN(buf + size, page_size); | |
686 | /* account for that last guard page */ | |
687 | region.end -= page_size; | |
688 | ||
689 | /* set guard pages */ | |
690 | for (i = 0; i < region.n; i++) { | |
691 | void *start, *end; | |
692 | int rc; | |
693 | ||
694 | tcg_region_bounds(i, &start, &end); | |
695 | rc = qemu_mprotect_none(end, page_size); | |
696 | g_assert(!rc); | |
697 | } | |
698 | ||
be2cdc5e EC |
699 | tcg_region_trees_init(); |
700 | ||
3468b59e EC |
701 | /* In user-mode we support only one ctx, so do the initial allocation now */ |
702 | #ifdef CONFIG_USER_ONLY | |
e8feb96f EC |
703 | { |
704 | bool err = tcg_region_initial_alloc__locked(tcg_ctx); | |
705 | ||
706 | g_assert(!err); | |
707 | } | |
3468b59e EC |
708 | #endif |
709 | } | |
710 | ||
711 | /* | |
712 | * All TCG threads except the parent (i.e. the one that called tcg_context_init | |
713 | * and registered the target's TCG globals) must register with this function | |
714 | * before initiating translation. | |
715 | * | |
716 | * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation | |
717 | * of tcg_region_init() for the reasoning behind this. | |
718 | * | |
719 | * In softmmu each caller registers its context in tcg_ctxs[]. Note that in | |
720 | * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context | |
721 | * is not used anymore for translation once this function is called. | |
722 | * | |
723 | * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates | |
724 | * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode. | |
725 | */ | |
726 | #ifdef CONFIG_USER_ONLY | |
727 | void tcg_register_thread(void) | |
728 | { | |
729 | tcg_ctx = &tcg_init_ctx; | |
730 | } | |
731 | #else | |
732 | void tcg_register_thread(void) | |
733 | { | |
734 | TCGContext *s = g_malloc(sizeof(*s)); | |
735 | unsigned int i, n; | |
736 | bool err; | |
737 | ||
738 | *s = tcg_init_ctx; | |
739 | ||
740 | /* Relink mem_base. */ | |
741 | for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) { | |
742 | if (tcg_init_ctx.temps[i].mem_base) { | |
743 | ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps; | |
744 | tcg_debug_assert(b >= 0 && b < n); | |
745 | s->temps[i].mem_base = &s->temps[b]; | |
746 | } | |
747 | } | |
748 | ||
749 | /* Claim an entry in tcg_ctxs */ | |
750 | n = atomic_fetch_inc(&n_tcg_ctxs); | |
751 | g_assert(n < max_cpus); | |
752 | atomic_set(&tcg_ctxs[n], s); | |
753 | ||
754 | tcg_ctx = s; | |
755 | qemu_mutex_lock(®ion.lock); | |
756 | err = tcg_region_initial_alloc__locked(tcg_ctx); | |
757 | g_assert(!err); | |
758 | qemu_mutex_unlock(®ion.lock); | |
e8feb96f | 759 | } |
3468b59e | 760 | #endif /* !CONFIG_USER_ONLY */ |
e8feb96f EC |
761 | |
762 | /* | |
763 | * Returns the size (in bytes) of all translated code (i.e. from all regions) | |
764 | * currently in the cache. | |
765 | * See also: tcg_code_capacity() | |
766 | * Do not confuse with tcg_current_code_size(); that one applies to a single | |
767 | * TCG context. | |
768 | */ | |
769 | size_t tcg_code_size(void) | |
770 | { | |
3468b59e | 771 | unsigned int n_ctxs = atomic_read(&n_tcg_ctxs); |
e8feb96f EC |
772 | unsigned int i; |
773 | size_t total; | |
774 | ||
775 | qemu_mutex_lock(®ion.lock); | |
776 | total = region.agg_size_full; | |
3468b59e EC |
777 | for (i = 0; i < n_ctxs; i++) { |
778 | const TCGContext *s = atomic_read(&tcg_ctxs[i]); | |
e8feb96f EC |
779 | size_t size; |
780 | ||
781 | size = atomic_read(&s->code_gen_ptr) - s->code_gen_buffer; | |
782 | g_assert(size <= s->code_gen_buffer_size); | |
783 | total += size; | |
784 | } | |
785 | qemu_mutex_unlock(®ion.lock); | |
786 | return total; | |
787 | } | |
788 | ||
789 | /* | |
790 | * Returns the code capacity (in bytes) of the entire cache, i.e. including all | |
791 | * regions. | |
792 | * See also: tcg_code_size() | |
793 | */ | |
794 | size_t tcg_code_capacity(void) | |
795 | { | |
796 | size_t guard_size, capacity; | |
797 | ||
798 | /* no need for synchronization; these variables are set at init time */ | |
799 | guard_size = region.stride - region.size; | |
800 | capacity = region.end + guard_size - region.start; | |
801 | capacity -= region.n * (guard_size + TCG_HIGHWATER); | |
802 | return capacity; | |
803 | } | |
804 | ||
128ed227 EC |
805 | size_t tcg_tb_phys_invalidate_count(void) |
806 | { | |
807 | unsigned int n_ctxs = atomic_read(&n_tcg_ctxs); | |
808 | unsigned int i; | |
809 | size_t total = 0; | |
810 | ||
811 | for (i = 0; i < n_ctxs; i++) { | |
812 | const TCGContext *s = atomic_read(&tcg_ctxs[i]); | |
813 | ||
814 | total += atomic_read(&s->tb_phys_invalidate_count); | |
815 | } | |
816 | return total; | |
817 | } | |
818 | ||
c896fe29 FB |
819 | /* pool based memory allocation */ |
820 | void *tcg_malloc_internal(TCGContext *s, int size) | |
821 | { | |
822 | TCGPool *p; | |
823 | int pool_size; | |
824 | ||
825 | if (size > TCG_POOL_CHUNK_SIZE) { | |
826 | /* big malloc: insert a new pool (XXX: could optimize) */ | |
7267c094 | 827 | p = g_malloc(sizeof(TCGPool) + size); |
c896fe29 | 828 | p->size = size; |
4055299e KB |
829 | p->next = s->pool_first_large; |
830 | s->pool_first_large = p; | |
831 | return p->data; | |
c896fe29 FB |
832 | } else { |
833 | p = s->pool_current; | |
834 | if (!p) { | |
835 | p = s->pool_first; | |
836 | if (!p) | |
837 | goto new_pool; | |
838 | } else { | |
839 | if (!p->next) { | |
840 | new_pool: | |
841 | pool_size = TCG_POOL_CHUNK_SIZE; | |
7267c094 | 842 | p = g_malloc(sizeof(TCGPool) + pool_size); |
c896fe29 FB |
843 | p->size = pool_size; |
844 | p->next = NULL; | |
845 | if (s->pool_current) | |
846 | s->pool_current->next = p; | |
847 | else | |
848 | s->pool_first = p; | |
849 | } else { | |
850 | p = p->next; | |
851 | } | |
852 | } | |
853 | } | |
854 | s->pool_current = p; | |
855 | s->pool_cur = p->data + size; | |
856 | s->pool_end = p->data + p->size; | |
857 | return p->data; | |
858 | } | |
859 | ||
860 | void tcg_pool_reset(TCGContext *s) | |
861 | { | |
4055299e KB |
862 | TCGPool *p, *t; |
863 | for (p = s->pool_first_large; p; p = t) { | |
864 | t = p->next; | |
865 | g_free(p); | |
866 | } | |
867 | s->pool_first_large = NULL; | |
c896fe29 FB |
868 | s->pool_cur = s->pool_end = NULL; |
869 | s->pool_current = NULL; | |
870 | } | |
871 | ||
100b5e01 RH |
872 | typedef struct TCGHelperInfo { |
873 | void *func; | |
874 | const char *name; | |
afb49896 RH |
875 | unsigned flags; |
876 | unsigned sizemask; | |
100b5e01 RH |
877 | } TCGHelperInfo; |
878 | ||
2ef6175a RH |
879 | #include "exec/helper-proto.h" |
880 | ||
100b5e01 | 881 | static const TCGHelperInfo all_helpers[] = { |
2ef6175a | 882 | #include "exec/helper-tcg.h" |
100b5e01 | 883 | }; |
619205fd | 884 | static GHashTable *helper_table; |
100b5e01 | 885 | |
91478cef | 886 | static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)]; |
f69d277e | 887 | static void process_op_defs(TCGContext *s); |
1c2adb95 RH |
888 | static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type, |
889 | TCGReg reg, const char *name); | |
91478cef | 890 | |
c896fe29 FB |
891 | void tcg_context_init(TCGContext *s) |
892 | { | |
100b5e01 | 893 | int op, total_args, n, i; |
c896fe29 FB |
894 | TCGOpDef *def; |
895 | TCGArgConstraint *args_ct; | |
896 | int *sorted_args; | |
1c2adb95 | 897 | TCGTemp *ts; |
c896fe29 FB |
898 | |
899 | memset(s, 0, sizeof(*s)); | |
c896fe29 | 900 | s->nb_globals = 0; |
c70fbf0a | 901 | |
c896fe29 FB |
902 | /* Count total number of arguments and allocate the corresponding |
903 | space */ | |
904 | total_args = 0; | |
905 | for(op = 0; op < NB_OPS; op++) { | |
906 | def = &tcg_op_defs[op]; | |
907 | n = def->nb_iargs + def->nb_oargs; | |
908 | total_args += n; | |
909 | } | |
910 | ||
7267c094 AL |
911 | args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args); |
912 | sorted_args = g_malloc(sizeof(int) * total_args); | |
c896fe29 FB |
913 | |
914 | for(op = 0; op < NB_OPS; op++) { | |
915 | def = &tcg_op_defs[op]; | |
916 | def->args_ct = args_ct; | |
917 | def->sorted_args = sorted_args; | |
918 | n = def->nb_iargs + def->nb_oargs; | |
919 | sorted_args += n; | |
920 | args_ct += n; | |
921 | } | |
5cd8f621 RH |
922 | |
923 | /* Register helpers. */ | |
84fd9dd3 | 924 | /* Use g_direct_hash/equal for direct pointer comparisons on func. */ |
619205fd | 925 | helper_table = g_hash_table_new(NULL, NULL); |
84fd9dd3 | 926 | |
100b5e01 | 927 | for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) { |
84fd9dd3 | 928 | g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func, |
72866e82 | 929 | (gpointer)&all_helpers[i]); |
100b5e01 | 930 | } |
5cd8f621 | 931 | |
c896fe29 | 932 | tcg_target_init(s); |
f69d277e | 933 | process_op_defs(s); |
91478cef RH |
934 | |
935 | /* Reverse the order of the saved registers, assuming they're all at | |
936 | the start of tcg_target_reg_alloc_order. */ | |
937 | for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) { | |
938 | int r = tcg_target_reg_alloc_order[n]; | |
939 | if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) { | |
940 | break; | |
941 | } | |
942 | } | |
943 | for (i = 0; i < n; ++i) { | |
944 | indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i]; | |
945 | } | |
946 | for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) { | |
947 | indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i]; | |
948 | } | |
b1311c4a EC |
949 | |
950 | tcg_ctx = s; | |
3468b59e EC |
951 | /* |
952 | * In user-mode we simply share the init context among threads, since we | |
953 | * use a single region. See the documentation tcg_region_init() for the | |
954 | * reasoning behind this. | |
955 | * In softmmu we will have at most max_cpus TCG threads. | |
956 | */ | |
957 | #ifdef CONFIG_USER_ONLY | |
df2cce29 EC |
958 | tcg_ctxs = &tcg_ctx; |
959 | n_tcg_ctxs = 1; | |
3468b59e EC |
960 | #else |
961 | tcg_ctxs = g_new(TCGContext *, max_cpus); | |
962 | #endif | |
1c2adb95 RH |
963 | |
964 | tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0)); | |
965 | ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env"); | |
966 | cpu_env = temp_tcgv_ptr(ts); | |
9002ec79 | 967 | } |
b03cce8e | 968 | |
6e3b2bfd EC |
969 | /* |
970 | * Allocate TBs right before their corresponding translated code, making | |
971 | * sure that TBs and code are on different cache lines. | |
972 | */ | |
973 | TranslationBlock *tcg_tb_alloc(TCGContext *s) | |
974 | { | |
975 | uintptr_t align = qemu_icache_linesize; | |
976 | TranslationBlock *tb; | |
977 | void *next; | |
978 | ||
e8feb96f | 979 | retry: |
6e3b2bfd EC |
980 | tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align); |
981 | next = (void *)ROUND_UP((uintptr_t)(tb + 1), align); | |
982 | ||
983 | if (unlikely(next > s->code_gen_highwater)) { | |
e8feb96f EC |
984 | if (tcg_region_alloc(s)) { |
985 | return NULL; | |
986 | } | |
987 | goto retry; | |
6e3b2bfd | 988 | } |
e8feb96f | 989 | atomic_set(&s->code_gen_ptr, next); |
57a26946 | 990 | s->data_gen_ptr = NULL; |
6e3b2bfd EC |
991 | return tb; |
992 | } | |
993 | ||
9002ec79 RH |
994 | void tcg_prologue_init(TCGContext *s) |
995 | { | |
8163b749 RH |
996 | size_t prologue_size, total_size; |
997 | void *buf0, *buf1; | |
998 | ||
999 | /* Put the prologue at the beginning of code_gen_buffer. */ | |
1000 | buf0 = s->code_gen_buffer; | |
5b38ee31 | 1001 | total_size = s->code_gen_buffer_size; |
8163b749 RH |
1002 | s->code_ptr = buf0; |
1003 | s->code_buf = buf0; | |
5b38ee31 | 1004 | s->data_gen_ptr = NULL; |
8163b749 RH |
1005 | s->code_gen_prologue = buf0; |
1006 | ||
5b38ee31 RH |
1007 | /* Compute a high-water mark, at which we voluntarily flush the buffer |
1008 | and start over. The size here is arbitrary, significantly larger | |
1009 | than we expect the code generation for any one opcode to require. */ | |
1010 | s->code_gen_highwater = s->code_gen_buffer + (total_size - TCG_HIGHWATER); | |
1011 | ||
1012 | #ifdef TCG_TARGET_NEED_POOL_LABELS | |
1013 | s->pool_labels = NULL; | |
1014 | #endif | |
1015 | ||
8163b749 | 1016 | /* Generate the prologue. */ |
b03cce8e | 1017 | tcg_target_qemu_prologue(s); |
5b38ee31 RH |
1018 | |
1019 | #ifdef TCG_TARGET_NEED_POOL_LABELS | |
1020 | /* Allow the prologue to put e.g. guest_base into a pool entry. */ | |
1021 | { | |
1022 | bool ok = tcg_out_pool_finalize(s); | |
1023 | tcg_debug_assert(ok); | |
1024 | } | |
1025 | #endif | |
1026 | ||
8163b749 RH |
1027 | buf1 = s->code_ptr; |
1028 | flush_icache_range((uintptr_t)buf0, (uintptr_t)buf1); | |
1029 | ||
1030 | /* Deduct the prologue from the buffer. */ | |
1031 | prologue_size = tcg_current_code_size(s); | |
1032 | s->code_gen_ptr = buf1; | |
1033 | s->code_gen_buffer = buf1; | |
1034 | s->code_buf = buf1; | |
5b38ee31 | 1035 | total_size -= prologue_size; |
8163b749 RH |
1036 | s->code_gen_buffer_size = total_size; |
1037 | ||
8163b749 | 1038 | tcg_register_jit(s->code_gen_buffer, total_size); |
d6b64b2b RH |
1039 | |
1040 | #ifdef DEBUG_DISAS | |
1041 | if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { | |
1ee73216 | 1042 | qemu_log_lock(); |
8163b749 | 1043 | qemu_log("PROLOGUE: [size=%zu]\n", prologue_size); |
5b38ee31 RH |
1044 | if (s->data_gen_ptr) { |
1045 | size_t code_size = s->data_gen_ptr - buf0; | |
1046 | size_t data_size = prologue_size - code_size; | |
1047 | size_t i; | |
1048 | ||
1049 | log_disas(buf0, code_size); | |
1050 | ||
1051 | for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) { | |
1052 | if (sizeof(tcg_target_ulong) == 8) { | |
1053 | qemu_log("0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n", | |
1054 | (uintptr_t)s->data_gen_ptr + i, | |
1055 | *(uint64_t *)(s->data_gen_ptr + i)); | |
1056 | } else { | |
1057 | qemu_log("0x%08" PRIxPTR ": .long 0x%08x\n", | |
1058 | (uintptr_t)s->data_gen_ptr + i, | |
1059 | *(uint32_t *)(s->data_gen_ptr + i)); | |
1060 | } | |
1061 | } | |
1062 | } else { | |
1063 | log_disas(buf0, prologue_size); | |
1064 | } | |
d6b64b2b RH |
1065 | qemu_log("\n"); |
1066 | qemu_log_flush(); | |
1ee73216 | 1067 | qemu_log_unlock(); |
d6b64b2b RH |
1068 | } |
1069 | #endif | |
cedbcb01 EC |
1070 | |
1071 | /* Assert that goto_ptr is implemented completely. */ | |
1072 | if (TCG_TARGET_HAS_goto_ptr) { | |
1073 | tcg_debug_assert(s->code_gen_epilogue != NULL); | |
1074 | } | |
c896fe29 FB |
1075 | } |
1076 | ||
c896fe29 FB |
1077 | void tcg_func_start(TCGContext *s) |
1078 | { | |
1079 | tcg_pool_reset(s); | |
1080 | s->nb_temps = s->nb_globals; | |
0ec9eabc RH |
1081 | |
1082 | /* No temps have been previously allocated for size or locality. */ | |
1083 | memset(s->free_temps, 0, sizeof(s->free_temps)); | |
1084 | ||
abebf925 | 1085 | s->nb_ops = 0; |
c896fe29 FB |
1086 | s->nb_labels = 0; |
1087 | s->current_frame_offset = s->frame_start; | |
1088 | ||
0a209d4b RH |
1089 | #ifdef CONFIG_DEBUG_TCG |
1090 | s->goto_tb_issue_mask = 0; | |
1091 | #endif | |
1092 | ||
15fa08f8 RH |
1093 | QTAILQ_INIT(&s->ops); |
1094 | QTAILQ_INIT(&s->free_ops); | |
c896fe29 FB |
1095 | } |
1096 | ||
7ca4b752 RH |
1097 | static inline TCGTemp *tcg_temp_alloc(TCGContext *s) |
1098 | { | |
1099 | int n = s->nb_temps++; | |
1100 | tcg_debug_assert(n < TCG_MAX_TEMPS); | |
1101 | return memset(&s->temps[n], 0, sizeof(TCGTemp)); | |
1102 | } | |
1103 | ||
1104 | static inline TCGTemp *tcg_global_alloc(TCGContext *s) | |
1105 | { | |
fa477d25 RH |
1106 | TCGTemp *ts; |
1107 | ||
7ca4b752 RH |
1108 | tcg_debug_assert(s->nb_globals == s->nb_temps); |
1109 | s->nb_globals++; | |
fa477d25 RH |
1110 | ts = tcg_temp_alloc(s); |
1111 | ts->temp_global = 1; | |
1112 | ||
1113 | return ts; | |
c896fe29 FB |
1114 | } |
1115 | ||
085272b3 RH |
1116 | static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type, |
1117 | TCGReg reg, const char *name) | |
c896fe29 | 1118 | { |
c896fe29 | 1119 | TCGTemp *ts; |
c896fe29 | 1120 | |
b3a62939 | 1121 | if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) { |
c896fe29 | 1122 | tcg_abort(); |
b3a62939 | 1123 | } |
7ca4b752 RH |
1124 | |
1125 | ts = tcg_global_alloc(s); | |
c896fe29 FB |
1126 | ts->base_type = type; |
1127 | ts->type = type; | |
1128 | ts->fixed_reg = 1; | |
1129 | ts->reg = reg; | |
c896fe29 | 1130 | ts->name = name; |
c896fe29 | 1131 | tcg_regset_set_reg(s->reserved_regs, reg); |
7ca4b752 | 1132 | |
085272b3 | 1133 | return ts; |
a7812ae4 PB |
1134 | } |
1135 | ||
b6638662 | 1136 | void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size) |
b3a62939 | 1137 | { |
b3a62939 RH |
1138 | s->frame_start = start; |
1139 | s->frame_end = start + size; | |
085272b3 RH |
1140 | s->frame_temp |
1141 | = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame"); | |
b3a62939 RH |
1142 | } |
1143 | ||
085272b3 RH |
1144 | TCGTemp *tcg_global_mem_new_internal(TCGType type, TCGv_ptr base, |
1145 | intptr_t offset, const char *name) | |
c896fe29 | 1146 | { |
b1311c4a | 1147 | TCGContext *s = tcg_ctx; |
dc41aa7d | 1148 | TCGTemp *base_ts = tcgv_ptr_temp(base); |
7ca4b752 | 1149 | TCGTemp *ts = tcg_global_alloc(s); |
b3915dbb | 1150 | int indirect_reg = 0, bigendian = 0; |
7ca4b752 RH |
1151 | #ifdef HOST_WORDS_BIGENDIAN |
1152 | bigendian = 1; | |
1153 | #endif | |
c896fe29 | 1154 | |
b3915dbb | 1155 | if (!base_ts->fixed_reg) { |
5a18407f RH |
1156 | /* We do not support double-indirect registers. */ |
1157 | tcg_debug_assert(!base_ts->indirect_reg); | |
b3915dbb | 1158 | base_ts->indirect_base = 1; |
5a18407f RH |
1159 | s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64 |
1160 | ? 2 : 1); | |
1161 | indirect_reg = 1; | |
b3915dbb RH |
1162 | } |
1163 | ||
7ca4b752 RH |
1164 | if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) { |
1165 | TCGTemp *ts2 = tcg_global_alloc(s); | |
c896fe29 | 1166 | char buf[64]; |
7ca4b752 RH |
1167 | |
1168 | ts->base_type = TCG_TYPE_I64; | |
c896fe29 | 1169 | ts->type = TCG_TYPE_I32; |
b3915dbb | 1170 | ts->indirect_reg = indirect_reg; |
c896fe29 | 1171 | ts->mem_allocated = 1; |
b3a62939 | 1172 | ts->mem_base = base_ts; |
7ca4b752 | 1173 | ts->mem_offset = offset + bigendian * 4; |
c896fe29 FB |
1174 | pstrcpy(buf, sizeof(buf), name); |
1175 | pstrcat(buf, sizeof(buf), "_0"); | |
1176 | ts->name = strdup(buf); | |
c896fe29 | 1177 | |
7ca4b752 RH |
1178 | tcg_debug_assert(ts2 == ts + 1); |
1179 | ts2->base_type = TCG_TYPE_I64; | |
1180 | ts2->type = TCG_TYPE_I32; | |
b3915dbb | 1181 | ts2->indirect_reg = indirect_reg; |
7ca4b752 RH |
1182 | ts2->mem_allocated = 1; |
1183 | ts2->mem_base = base_ts; | |
1184 | ts2->mem_offset = offset + (1 - bigendian) * 4; | |
c896fe29 FB |
1185 | pstrcpy(buf, sizeof(buf), name); |
1186 | pstrcat(buf, sizeof(buf), "_1"); | |
120c1084 | 1187 | ts2->name = strdup(buf); |
7ca4b752 | 1188 | } else { |
c896fe29 FB |
1189 | ts->base_type = type; |
1190 | ts->type = type; | |
b3915dbb | 1191 | ts->indirect_reg = indirect_reg; |
c896fe29 | 1192 | ts->mem_allocated = 1; |
b3a62939 | 1193 | ts->mem_base = base_ts; |
c896fe29 | 1194 | ts->mem_offset = offset; |
c896fe29 | 1195 | ts->name = name; |
c896fe29 | 1196 | } |
085272b3 | 1197 | return ts; |
a7812ae4 PB |
1198 | } |
1199 | ||
5bfa8034 | 1200 | TCGTemp *tcg_temp_new_internal(TCGType type, bool temp_local) |
c896fe29 | 1201 | { |
b1311c4a | 1202 | TCGContext *s = tcg_ctx; |
c896fe29 | 1203 | TCGTemp *ts; |
641d5fbe | 1204 | int idx, k; |
c896fe29 | 1205 | |
0ec9eabc RH |
1206 | k = type + (temp_local ? TCG_TYPE_COUNT : 0); |
1207 | idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS); | |
1208 | if (idx < TCG_MAX_TEMPS) { | |
1209 | /* There is already an available temp with the right type. */ | |
1210 | clear_bit(idx, s->free_temps[k].l); | |
1211 | ||
e8996ee0 | 1212 | ts = &s->temps[idx]; |
e8996ee0 | 1213 | ts->temp_allocated = 1; |
7ca4b752 RH |
1214 | tcg_debug_assert(ts->base_type == type); |
1215 | tcg_debug_assert(ts->temp_local == temp_local); | |
e8996ee0 | 1216 | } else { |
7ca4b752 RH |
1217 | ts = tcg_temp_alloc(s); |
1218 | if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) { | |
1219 | TCGTemp *ts2 = tcg_temp_alloc(s); | |
1220 | ||
f6aa2f7d | 1221 | ts->base_type = type; |
e8996ee0 FB |
1222 | ts->type = TCG_TYPE_I32; |
1223 | ts->temp_allocated = 1; | |
641d5fbe | 1224 | ts->temp_local = temp_local; |
7ca4b752 RH |
1225 | |
1226 | tcg_debug_assert(ts2 == ts + 1); | |
1227 | ts2->base_type = TCG_TYPE_I64; | |
1228 | ts2->type = TCG_TYPE_I32; | |
1229 | ts2->temp_allocated = 1; | |
1230 | ts2->temp_local = temp_local; | |
1231 | } else { | |
e8996ee0 FB |
1232 | ts->base_type = type; |
1233 | ts->type = type; | |
1234 | ts->temp_allocated = 1; | |
641d5fbe | 1235 | ts->temp_local = temp_local; |
e8996ee0 | 1236 | } |
c896fe29 | 1237 | } |
27bfd83c PM |
1238 | |
1239 | #if defined(CONFIG_DEBUG_TCG) | |
1240 | s->temps_in_use++; | |
1241 | #endif | |
085272b3 | 1242 | return ts; |
c896fe29 FB |
1243 | } |
1244 | ||
d2fd745f RH |
1245 | TCGv_vec tcg_temp_new_vec(TCGType type) |
1246 | { | |
1247 | TCGTemp *t; | |
1248 | ||
1249 | #ifdef CONFIG_DEBUG_TCG | |
1250 | switch (type) { | |
1251 | case TCG_TYPE_V64: | |
1252 | assert(TCG_TARGET_HAS_v64); | |
1253 | break; | |
1254 | case TCG_TYPE_V128: | |
1255 | assert(TCG_TARGET_HAS_v128); | |
1256 | break; | |
1257 | case TCG_TYPE_V256: | |
1258 | assert(TCG_TARGET_HAS_v256); | |
1259 | break; | |
1260 | default: | |
1261 | g_assert_not_reached(); | |
1262 | } | |
1263 | #endif | |
1264 | ||
1265 | t = tcg_temp_new_internal(type, 0); | |
1266 | return temp_tcgv_vec(t); | |
1267 | } | |
1268 | ||
1269 | /* Create a new temp of the same type as an existing temp. */ | |
1270 | TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match) | |
1271 | { | |
1272 | TCGTemp *t = tcgv_vec_temp(match); | |
1273 | ||
1274 | tcg_debug_assert(t->temp_allocated != 0); | |
1275 | ||
1276 | t = tcg_temp_new_internal(t->base_type, 0); | |
1277 | return temp_tcgv_vec(t); | |
1278 | } | |
1279 | ||
5bfa8034 | 1280 | void tcg_temp_free_internal(TCGTemp *ts) |
c896fe29 | 1281 | { |
b1311c4a | 1282 | TCGContext *s = tcg_ctx; |
085272b3 | 1283 | int k, idx; |
c896fe29 | 1284 | |
27bfd83c PM |
1285 | #if defined(CONFIG_DEBUG_TCG) |
1286 | s->temps_in_use--; | |
1287 | if (s->temps_in_use < 0) { | |
1288 | fprintf(stderr, "More temporaries freed than allocated!\n"); | |
1289 | } | |
1290 | #endif | |
1291 | ||
085272b3 | 1292 | tcg_debug_assert(ts->temp_global == 0); |
eabb7b91 | 1293 | tcg_debug_assert(ts->temp_allocated != 0); |
e8996ee0 | 1294 | ts->temp_allocated = 0; |
0ec9eabc | 1295 | |
085272b3 | 1296 | idx = temp_idx(ts); |
18d13fa2 | 1297 | k = ts->base_type + (ts->temp_local ? TCG_TYPE_COUNT : 0); |
0ec9eabc | 1298 | set_bit(idx, s->free_temps[k].l); |
c896fe29 FB |
1299 | } |
1300 | ||
a7812ae4 | 1301 | TCGv_i32 tcg_const_i32(int32_t val) |
c896fe29 | 1302 | { |
a7812ae4 PB |
1303 | TCGv_i32 t0; |
1304 | t0 = tcg_temp_new_i32(); | |
e8996ee0 FB |
1305 | tcg_gen_movi_i32(t0, val); |
1306 | return t0; | |
1307 | } | |
c896fe29 | 1308 | |
a7812ae4 | 1309 | TCGv_i64 tcg_const_i64(int64_t val) |
e8996ee0 | 1310 | { |
a7812ae4 PB |
1311 | TCGv_i64 t0; |
1312 | t0 = tcg_temp_new_i64(); | |
e8996ee0 FB |
1313 | tcg_gen_movi_i64(t0, val); |
1314 | return t0; | |
c896fe29 FB |
1315 | } |
1316 | ||
a7812ae4 | 1317 | TCGv_i32 tcg_const_local_i32(int32_t val) |
bdffd4a9 | 1318 | { |
a7812ae4 PB |
1319 | TCGv_i32 t0; |
1320 | t0 = tcg_temp_local_new_i32(); | |
bdffd4a9 AJ |
1321 | tcg_gen_movi_i32(t0, val); |
1322 | return t0; | |
1323 | } | |
1324 | ||
a7812ae4 | 1325 | TCGv_i64 tcg_const_local_i64(int64_t val) |
bdffd4a9 | 1326 | { |
a7812ae4 PB |
1327 | TCGv_i64 t0; |
1328 | t0 = tcg_temp_local_new_i64(); | |
bdffd4a9 AJ |
1329 | tcg_gen_movi_i64(t0, val); |
1330 | return t0; | |
1331 | } | |
1332 | ||
27bfd83c PM |
1333 | #if defined(CONFIG_DEBUG_TCG) |
1334 | void tcg_clear_temp_count(void) | |
1335 | { | |
b1311c4a | 1336 | TCGContext *s = tcg_ctx; |
27bfd83c PM |
1337 | s->temps_in_use = 0; |
1338 | } | |
1339 | ||
1340 | int tcg_check_temp_count(void) | |
1341 | { | |
b1311c4a | 1342 | TCGContext *s = tcg_ctx; |
27bfd83c PM |
1343 | if (s->temps_in_use) { |
1344 | /* Clear the count so that we don't give another | |
1345 | * warning immediately next time around. | |
1346 | */ | |
1347 | s->temps_in_use = 0; | |
1348 | return 1; | |
1349 | } | |
1350 | return 0; | |
1351 | } | |
1352 | #endif | |
1353 | ||
be0f34b5 RH |
1354 | /* Return true if OP may appear in the opcode stream. |
1355 | Test the runtime variable that controls each opcode. */ | |
1356 | bool tcg_op_supported(TCGOpcode op) | |
1357 | { | |
d2fd745f RH |
1358 | const bool have_vec |
1359 | = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256; | |
1360 | ||
be0f34b5 RH |
1361 | switch (op) { |
1362 | case INDEX_op_discard: | |
1363 | case INDEX_op_set_label: | |
1364 | case INDEX_op_call: | |
1365 | case INDEX_op_br: | |
1366 | case INDEX_op_mb: | |
1367 | case INDEX_op_insn_start: | |
1368 | case INDEX_op_exit_tb: | |
1369 | case INDEX_op_goto_tb: | |
1370 | case INDEX_op_qemu_ld_i32: | |
1371 | case INDEX_op_qemu_st_i32: | |
1372 | case INDEX_op_qemu_ld_i64: | |
1373 | case INDEX_op_qemu_st_i64: | |
1374 | return true; | |
1375 | ||
1376 | case INDEX_op_goto_ptr: | |
1377 | return TCG_TARGET_HAS_goto_ptr; | |
1378 | ||
1379 | case INDEX_op_mov_i32: | |
1380 | case INDEX_op_movi_i32: | |
1381 | case INDEX_op_setcond_i32: | |
1382 | case INDEX_op_brcond_i32: | |
1383 | case INDEX_op_ld8u_i32: | |
1384 | case INDEX_op_ld8s_i32: | |
1385 | case INDEX_op_ld16u_i32: | |
1386 | case INDEX_op_ld16s_i32: | |
1387 | case INDEX_op_ld_i32: | |
1388 | case INDEX_op_st8_i32: | |
1389 | case INDEX_op_st16_i32: | |
1390 | case INDEX_op_st_i32: | |
1391 | case INDEX_op_add_i32: | |
1392 | case INDEX_op_sub_i32: | |
1393 | case INDEX_op_mul_i32: | |
1394 | case INDEX_op_and_i32: | |
1395 | case INDEX_op_or_i32: | |
1396 | case INDEX_op_xor_i32: | |
1397 | case INDEX_op_shl_i32: | |
1398 | case INDEX_op_shr_i32: | |
1399 | case INDEX_op_sar_i32: | |
1400 | return true; | |
1401 | ||
1402 | case INDEX_op_movcond_i32: | |
1403 | return TCG_TARGET_HAS_movcond_i32; | |
1404 | case INDEX_op_div_i32: | |
1405 | case INDEX_op_divu_i32: | |
1406 | return TCG_TARGET_HAS_div_i32; | |
1407 | case INDEX_op_rem_i32: | |
1408 | case INDEX_op_remu_i32: | |
1409 | return TCG_TARGET_HAS_rem_i32; | |
1410 | case INDEX_op_div2_i32: | |
1411 | case INDEX_op_divu2_i32: | |
1412 | return TCG_TARGET_HAS_div2_i32; | |
1413 | case INDEX_op_rotl_i32: | |
1414 | case INDEX_op_rotr_i32: | |
1415 | return TCG_TARGET_HAS_rot_i32; | |
1416 | case INDEX_op_deposit_i32: | |
1417 | return TCG_TARGET_HAS_deposit_i32; | |
1418 | case INDEX_op_extract_i32: | |
1419 | return TCG_TARGET_HAS_extract_i32; | |
1420 | case INDEX_op_sextract_i32: | |
1421 | return TCG_TARGET_HAS_sextract_i32; | |
1422 | case INDEX_op_add2_i32: | |
1423 | return TCG_TARGET_HAS_add2_i32; | |
1424 | case INDEX_op_sub2_i32: | |
1425 | return TCG_TARGET_HAS_sub2_i32; | |
1426 | case INDEX_op_mulu2_i32: | |
1427 | return TCG_TARGET_HAS_mulu2_i32; | |
1428 | case INDEX_op_muls2_i32: | |
1429 | return TCG_TARGET_HAS_muls2_i32; | |
1430 | case INDEX_op_muluh_i32: | |
1431 | return TCG_TARGET_HAS_muluh_i32; | |
1432 | case INDEX_op_mulsh_i32: | |
1433 | return TCG_TARGET_HAS_mulsh_i32; | |
1434 | case INDEX_op_ext8s_i32: | |
1435 | return TCG_TARGET_HAS_ext8s_i32; | |
1436 | case INDEX_op_ext16s_i32: | |
1437 | return TCG_TARGET_HAS_ext16s_i32; | |
1438 | case INDEX_op_ext8u_i32: | |
1439 | return TCG_TARGET_HAS_ext8u_i32; | |
1440 | case INDEX_op_ext16u_i32: | |
1441 | return TCG_TARGET_HAS_ext16u_i32; | |
1442 | case INDEX_op_bswap16_i32: | |
1443 | return TCG_TARGET_HAS_bswap16_i32; | |
1444 | case INDEX_op_bswap32_i32: | |
1445 | return TCG_TARGET_HAS_bswap32_i32; | |
1446 | case INDEX_op_not_i32: | |
1447 | return TCG_TARGET_HAS_not_i32; | |
1448 | case INDEX_op_neg_i32: | |
1449 | return TCG_TARGET_HAS_neg_i32; | |
1450 | case INDEX_op_andc_i32: | |
1451 | return TCG_TARGET_HAS_andc_i32; | |
1452 | case INDEX_op_orc_i32: | |
1453 | return TCG_TARGET_HAS_orc_i32; | |
1454 | case INDEX_op_eqv_i32: | |
1455 | return TCG_TARGET_HAS_eqv_i32; | |
1456 | case INDEX_op_nand_i32: | |
1457 | return TCG_TARGET_HAS_nand_i32; | |
1458 | case INDEX_op_nor_i32: | |
1459 | return TCG_TARGET_HAS_nor_i32; | |
1460 | case INDEX_op_clz_i32: | |
1461 | return TCG_TARGET_HAS_clz_i32; | |
1462 | case INDEX_op_ctz_i32: | |
1463 | return TCG_TARGET_HAS_ctz_i32; | |
1464 | case INDEX_op_ctpop_i32: | |
1465 | return TCG_TARGET_HAS_ctpop_i32; | |
1466 | ||
1467 | case INDEX_op_brcond2_i32: | |
1468 | case INDEX_op_setcond2_i32: | |
1469 | return TCG_TARGET_REG_BITS == 32; | |
1470 | ||
1471 | case INDEX_op_mov_i64: | |
1472 | case INDEX_op_movi_i64: | |
1473 | case INDEX_op_setcond_i64: | |
1474 | case INDEX_op_brcond_i64: | |
1475 | case INDEX_op_ld8u_i64: | |
1476 | case INDEX_op_ld8s_i64: | |
1477 | case INDEX_op_ld16u_i64: | |
1478 | case INDEX_op_ld16s_i64: | |
1479 | case INDEX_op_ld32u_i64: | |
1480 | case INDEX_op_ld32s_i64: | |
1481 | case INDEX_op_ld_i64: | |
1482 | case INDEX_op_st8_i64: | |
1483 | case INDEX_op_st16_i64: | |
1484 | case INDEX_op_st32_i64: | |
1485 | case INDEX_op_st_i64: | |
1486 | case INDEX_op_add_i64: | |
1487 | case INDEX_op_sub_i64: | |
1488 | case INDEX_op_mul_i64: | |
1489 | case INDEX_op_and_i64: | |
1490 | case INDEX_op_or_i64: | |
1491 | case INDEX_op_xor_i64: | |
1492 | case INDEX_op_shl_i64: | |
1493 | case INDEX_op_shr_i64: | |
1494 | case INDEX_op_sar_i64: | |
1495 | case INDEX_op_ext_i32_i64: | |
1496 | case INDEX_op_extu_i32_i64: | |
1497 | return TCG_TARGET_REG_BITS == 64; | |
1498 | ||
1499 | case INDEX_op_movcond_i64: | |
1500 | return TCG_TARGET_HAS_movcond_i64; | |
1501 | case INDEX_op_div_i64: | |
1502 | case INDEX_op_divu_i64: | |
1503 | return TCG_TARGET_HAS_div_i64; | |
1504 | case INDEX_op_rem_i64: | |
1505 | case INDEX_op_remu_i64: | |
1506 | return TCG_TARGET_HAS_rem_i64; | |
1507 | case INDEX_op_div2_i64: | |
1508 | case INDEX_op_divu2_i64: | |
1509 | return TCG_TARGET_HAS_div2_i64; | |
1510 | case INDEX_op_rotl_i64: | |
1511 | case INDEX_op_rotr_i64: | |
1512 | return TCG_TARGET_HAS_rot_i64; | |
1513 | case INDEX_op_deposit_i64: | |
1514 | return TCG_TARGET_HAS_deposit_i64; | |
1515 | case INDEX_op_extract_i64: | |
1516 | return TCG_TARGET_HAS_extract_i64; | |
1517 | case INDEX_op_sextract_i64: | |
1518 | return TCG_TARGET_HAS_sextract_i64; | |
1519 | case INDEX_op_extrl_i64_i32: | |
1520 | return TCG_TARGET_HAS_extrl_i64_i32; | |
1521 | case INDEX_op_extrh_i64_i32: | |
1522 | return TCG_TARGET_HAS_extrh_i64_i32; | |
1523 | case INDEX_op_ext8s_i64: | |
1524 | return TCG_TARGET_HAS_ext8s_i64; | |
1525 | case INDEX_op_ext16s_i64: | |
1526 | return TCG_TARGET_HAS_ext16s_i64; | |
1527 | case INDEX_op_ext32s_i64: | |
1528 | return TCG_TARGET_HAS_ext32s_i64; | |
1529 | case INDEX_op_ext8u_i64: | |
1530 | return TCG_TARGET_HAS_ext8u_i64; | |
1531 | case INDEX_op_ext16u_i64: | |
1532 | return TCG_TARGET_HAS_ext16u_i64; | |
1533 | case INDEX_op_ext32u_i64: | |
1534 | return TCG_TARGET_HAS_ext32u_i64; | |
1535 | case INDEX_op_bswap16_i64: | |
1536 | return TCG_TARGET_HAS_bswap16_i64; | |
1537 | case INDEX_op_bswap32_i64: | |
1538 | return TCG_TARGET_HAS_bswap32_i64; | |
1539 | case INDEX_op_bswap64_i64: | |
1540 | return TCG_TARGET_HAS_bswap64_i64; | |
1541 | case INDEX_op_not_i64: | |
1542 | return TCG_TARGET_HAS_not_i64; | |
1543 | case INDEX_op_neg_i64: | |
1544 | return TCG_TARGET_HAS_neg_i64; | |
1545 | case INDEX_op_andc_i64: | |
1546 | return TCG_TARGET_HAS_andc_i64; | |
1547 | case INDEX_op_orc_i64: | |
1548 | return TCG_TARGET_HAS_orc_i64; | |
1549 | case INDEX_op_eqv_i64: | |
1550 | return TCG_TARGET_HAS_eqv_i64; | |
1551 | case INDEX_op_nand_i64: | |
1552 | return TCG_TARGET_HAS_nand_i64; | |
1553 | case INDEX_op_nor_i64: | |
1554 | return TCG_TARGET_HAS_nor_i64; | |
1555 | case INDEX_op_clz_i64: | |
1556 | return TCG_TARGET_HAS_clz_i64; | |
1557 | case INDEX_op_ctz_i64: | |
1558 | return TCG_TARGET_HAS_ctz_i64; | |
1559 | case INDEX_op_ctpop_i64: | |
1560 | return TCG_TARGET_HAS_ctpop_i64; | |
1561 | case INDEX_op_add2_i64: | |
1562 | return TCG_TARGET_HAS_add2_i64; | |
1563 | case INDEX_op_sub2_i64: | |
1564 | return TCG_TARGET_HAS_sub2_i64; | |
1565 | case INDEX_op_mulu2_i64: | |
1566 | return TCG_TARGET_HAS_mulu2_i64; | |
1567 | case INDEX_op_muls2_i64: | |
1568 | return TCG_TARGET_HAS_muls2_i64; | |
1569 | case INDEX_op_muluh_i64: | |
1570 | return TCG_TARGET_HAS_muluh_i64; | |
1571 | case INDEX_op_mulsh_i64: | |
1572 | return TCG_TARGET_HAS_mulsh_i64; | |
1573 | ||
d2fd745f RH |
1574 | case INDEX_op_mov_vec: |
1575 | case INDEX_op_dup_vec: | |
1576 | case INDEX_op_dupi_vec: | |
1577 | case INDEX_op_ld_vec: | |
1578 | case INDEX_op_st_vec: | |
1579 | case INDEX_op_add_vec: | |
1580 | case INDEX_op_sub_vec: | |
1581 | case INDEX_op_and_vec: | |
1582 | case INDEX_op_or_vec: | |
1583 | case INDEX_op_xor_vec: | |
212be173 | 1584 | case INDEX_op_cmp_vec: |
d2fd745f RH |
1585 | return have_vec; |
1586 | case INDEX_op_dup2_vec: | |
1587 | return have_vec && TCG_TARGET_REG_BITS == 32; | |
1588 | case INDEX_op_not_vec: | |
1589 | return have_vec && TCG_TARGET_HAS_not_vec; | |
1590 | case INDEX_op_neg_vec: | |
1591 | return have_vec && TCG_TARGET_HAS_neg_vec; | |
1592 | case INDEX_op_andc_vec: | |
1593 | return have_vec && TCG_TARGET_HAS_andc_vec; | |
1594 | case INDEX_op_orc_vec: | |
1595 | return have_vec && TCG_TARGET_HAS_orc_vec; | |
3774030a RH |
1596 | case INDEX_op_mul_vec: |
1597 | return have_vec && TCG_TARGET_HAS_mul_vec; | |
d0ec9796 RH |
1598 | case INDEX_op_shli_vec: |
1599 | case INDEX_op_shri_vec: | |
1600 | case INDEX_op_sari_vec: | |
1601 | return have_vec && TCG_TARGET_HAS_shi_vec; | |
1602 | case INDEX_op_shls_vec: | |
1603 | case INDEX_op_shrs_vec: | |
1604 | case INDEX_op_sars_vec: | |
1605 | return have_vec && TCG_TARGET_HAS_shs_vec; | |
1606 | case INDEX_op_shlv_vec: | |
1607 | case INDEX_op_shrv_vec: | |
1608 | case INDEX_op_sarv_vec: | |
1609 | return have_vec && TCG_TARGET_HAS_shv_vec; | |
8afaf050 RH |
1610 | case INDEX_op_ssadd_vec: |
1611 | case INDEX_op_usadd_vec: | |
1612 | case INDEX_op_sssub_vec: | |
1613 | case INDEX_op_ussub_vec: | |
1614 | return have_vec && TCG_TARGET_HAS_sat_vec; | |
d2fd745f | 1615 | |
db432672 RH |
1616 | default: |
1617 | tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS); | |
1618 | return true; | |
be0f34b5 | 1619 | } |
be0f34b5 RH |
1620 | } |
1621 | ||
39cf05d3 FB |
1622 | /* Note: we convert the 64 bit args to 32 bit and do some alignment |
1623 | and endian swap. Maybe it would be better to do the alignment | |
1624 | and endian swap in tcg_reg_alloc_call(). */ | |
ae8b75dc | 1625 | void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args) |
c896fe29 | 1626 | { |
75e8b9b7 | 1627 | int i, real_args, nb_rets, pi; |
bbb8a1b4 | 1628 | unsigned sizemask, flags; |
afb49896 | 1629 | TCGHelperInfo *info; |
75e8b9b7 | 1630 | TCGOp *op; |
afb49896 | 1631 | |
619205fd | 1632 | info = g_hash_table_lookup(helper_table, (gpointer)func); |
bbb8a1b4 RH |
1633 | flags = info->flags; |
1634 | sizemask = info->sizemask; | |
2bece2c8 | 1635 | |
34b1a49c RH |
1636 | #if defined(__sparc__) && !defined(__arch64__) \ |
1637 | && !defined(CONFIG_TCG_INTERPRETER) | |
1638 | /* We have 64-bit values in one register, but need to pass as two | |
1639 | separate parameters. Split them. */ | |
1640 | int orig_sizemask = sizemask; | |
1641 | int orig_nargs = nargs; | |
1642 | TCGv_i64 retl, reth; | |
ae8b75dc | 1643 | TCGTemp *split_args[MAX_OPC_PARAM]; |
34b1a49c | 1644 | |
f764718d RH |
1645 | retl = NULL; |
1646 | reth = NULL; | |
34b1a49c | 1647 | if (sizemask != 0) { |
34b1a49c RH |
1648 | for (i = real_args = 0; i < nargs; ++i) { |
1649 | int is_64bit = sizemask & (1 << (i+1)*2); | |
1650 | if (is_64bit) { | |
085272b3 | 1651 | TCGv_i64 orig = temp_tcgv_i64(args[i]); |
34b1a49c RH |
1652 | TCGv_i32 h = tcg_temp_new_i32(); |
1653 | TCGv_i32 l = tcg_temp_new_i32(); | |
1654 | tcg_gen_extr_i64_i32(l, h, orig); | |
ae8b75dc RH |
1655 | split_args[real_args++] = tcgv_i32_temp(h); |
1656 | split_args[real_args++] = tcgv_i32_temp(l); | |
34b1a49c RH |
1657 | } else { |
1658 | split_args[real_args++] = args[i]; | |
1659 | } | |
1660 | } | |
1661 | nargs = real_args; | |
1662 | args = split_args; | |
1663 | sizemask = 0; | |
1664 | } | |
1665 | #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64 | |
2bece2c8 RH |
1666 | for (i = 0; i < nargs; ++i) { |
1667 | int is_64bit = sizemask & (1 << (i+1)*2); | |
1668 | int is_signed = sizemask & (2 << (i+1)*2); | |
1669 | if (!is_64bit) { | |
1670 | TCGv_i64 temp = tcg_temp_new_i64(); | |
085272b3 | 1671 | TCGv_i64 orig = temp_tcgv_i64(args[i]); |
2bece2c8 RH |
1672 | if (is_signed) { |
1673 | tcg_gen_ext32s_i64(temp, orig); | |
1674 | } else { | |
1675 | tcg_gen_ext32u_i64(temp, orig); | |
1676 | } | |
ae8b75dc | 1677 | args[i] = tcgv_i64_temp(temp); |
2bece2c8 RH |
1678 | } |
1679 | } | |
1680 | #endif /* TCG_TARGET_EXTEND_ARGS */ | |
1681 | ||
15fa08f8 | 1682 | op = tcg_emit_op(INDEX_op_call); |
75e8b9b7 RH |
1683 | |
1684 | pi = 0; | |
ae8b75dc | 1685 | if (ret != NULL) { |
34b1a49c RH |
1686 | #if defined(__sparc__) && !defined(__arch64__) \ |
1687 | && !defined(CONFIG_TCG_INTERPRETER) | |
1688 | if (orig_sizemask & 1) { | |
1689 | /* The 32-bit ABI is going to return the 64-bit value in | |
1690 | the %o0/%o1 register pair. Prepare for this by using | |
1691 | two return temporaries, and reassemble below. */ | |
1692 | retl = tcg_temp_new_i64(); | |
1693 | reth = tcg_temp_new_i64(); | |
ae8b75dc RH |
1694 | op->args[pi++] = tcgv_i64_arg(reth); |
1695 | op->args[pi++] = tcgv_i64_arg(retl); | |
34b1a49c RH |
1696 | nb_rets = 2; |
1697 | } else { | |
ae8b75dc | 1698 | op->args[pi++] = temp_arg(ret); |
34b1a49c RH |
1699 | nb_rets = 1; |
1700 | } | |
1701 | #else | |
1702 | if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) { | |
02eb19d0 | 1703 | #ifdef HOST_WORDS_BIGENDIAN |
ae8b75dc RH |
1704 | op->args[pi++] = temp_arg(ret + 1); |
1705 | op->args[pi++] = temp_arg(ret); | |
39cf05d3 | 1706 | #else |
ae8b75dc RH |
1707 | op->args[pi++] = temp_arg(ret); |
1708 | op->args[pi++] = temp_arg(ret + 1); | |
39cf05d3 | 1709 | #endif |
a7812ae4 | 1710 | nb_rets = 2; |
34b1a49c | 1711 | } else { |
ae8b75dc | 1712 | op->args[pi++] = temp_arg(ret); |
a7812ae4 | 1713 | nb_rets = 1; |
c896fe29 | 1714 | } |
34b1a49c | 1715 | #endif |
a7812ae4 PB |
1716 | } else { |
1717 | nb_rets = 0; | |
c896fe29 | 1718 | } |
cd9090aa | 1719 | TCGOP_CALLO(op) = nb_rets; |
75e8b9b7 | 1720 | |
a7812ae4 PB |
1721 | real_args = 0; |
1722 | for (i = 0; i < nargs; i++) { | |
2bece2c8 | 1723 | int is_64bit = sizemask & (1 << (i+1)*2); |
bbb8a1b4 | 1724 | if (TCG_TARGET_REG_BITS < 64 && is_64bit) { |
39cf05d3 FB |
1725 | #ifdef TCG_TARGET_CALL_ALIGN_ARGS |
1726 | /* some targets want aligned 64 bit args */ | |
ebd486d5 | 1727 | if (real_args & 1) { |
75e8b9b7 | 1728 | op->args[pi++] = TCG_CALL_DUMMY_ARG; |
ebd486d5 | 1729 | real_args++; |
39cf05d3 FB |
1730 | } |
1731 | #endif | |
c70fbf0a RH |
1732 | /* If stack grows up, then we will be placing successive |
1733 | arguments at lower addresses, which means we need to | |
1734 | reverse the order compared to how we would normally | |
1735 | treat either big or little-endian. For those arguments | |
1736 | that will wind up in registers, this still works for | |
1737 | HPPA (the only current STACK_GROWSUP target) since the | |
1738 | argument registers are *also* allocated in decreasing | |
1739 | order. If another such target is added, this logic may | |
1740 | have to get more complicated to differentiate between | |
1741 | stack arguments and register arguments. */ | |
02eb19d0 | 1742 | #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP) |
ae8b75dc RH |
1743 | op->args[pi++] = temp_arg(args[i] + 1); |
1744 | op->args[pi++] = temp_arg(args[i]); | |
c896fe29 | 1745 | #else |
ae8b75dc RH |
1746 | op->args[pi++] = temp_arg(args[i]); |
1747 | op->args[pi++] = temp_arg(args[i] + 1); | |
c896fe29 | 1748 | #endif |
a7812ae4 | 1749 | real_args += 2; |
2bece2c8 | 1750 | continue; |
c896fe29 | 1751 | } |
2bece2c8 | 1752 | |
ae8b75dc | 1753 | op->args[pi++] = temp_arg(args[i]); |
2bece2c8 | 1754 | real_args++; |
c896fe29 | 1755 | } |
75e8b9b7 RH |
1756 | op->args[pi++] = (uintptr_t)func; |
1757 | op->args[pi++] = flags; | |
cd9090aa | 1758 | TCGOP_CALLI(op) = real_args; |
a7812ae4 | 1759 | |
75e8b9b7 | 1760 | /* Make sure the fields didn't overflow. */ |
cd9090aa | 1761 | tcg_debug_assert(TCGOP_CALLI(op) == real_args); |
75e8b9b7 | 1762 | tcg_debug_assert(pi <= ARRAY_SIZE(op->args)); |
2bece2c8 | 1763 | |
34b1a49c RH |
1764 | #if defined(__sparc__) && !defined(__arch64__) \ |
1765 | && !defined(CONFIG_TCG_INTERPRETER) | |
1766 | /* Free all of the parts we allocated above. */ | |
1767 | for (i = real_args = 0; i < orig_nargs; ++i) { | |
1768 | int is_64bit = orig_sizemask & (1 << (i+1)*2); | |
1769 | if (is_64bit) { | |
085272b3 RH |
1770 | tcg_temp_free_internal(args[real_args++]); |
1771 | tcg_temp_free_internal(args[real_args++]); | |
34b1a49c RH |
1772 | } else { |
1773 | real_args++; | |
1774 | } | |
1775 | } | |
1776 | if (orig_sizemask & 1) { | |
1777 | /* The 32-bit ABI returned two 32-bit pieces. Re-assemble them. | |
1778 | Note that describing these as TCGv_i64 eliminates an unnecessary | |
1779 | zero-extension that tcg_gen_concat_i32_i64 would create. */ | |
085272b3 | 1780 | tcg_gen_concat32_i64(temp_tcgv_i64(ret), retl, reth); |
34b1a49c RH |
1781 | tcg_temp_free_i64(retl); |
1782 | tcg_temp_free_i64(reth); | |
1783 | } | |
1784 | #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64 | |
2bece2c8 RH |
1785 | for (i = 0; i < nargs; ++i) { |
1786 | int is_64bit = sizemask & (1 << (i+1)*2); | |
1787 | if (!is_64bit) { | |
085272b3 | 1788 | tcg_temp_free_internal(args[i]); |
2bece2c8 RH |
1789 | } |
1790 | } | |
1791 | #endif /* TCG_TARGET_EXTEND_ARGS */ | |
c896fe29 | 1792 | } |
c896fe29 | 1793 | |
8fcd3692 | 1794 | static void tcg_reg_alloc_start(TCGContext *s) |
c896fe29 | 1795 | { |
ac3b8891 | 1796 | int i, n; |
c896fe29 | 1797 | TCGTemp *ts; |
ac3b8891 RH |
1798 | |
1799 | for (i = 0, n = s->nb_globals; i < n; i++) { | |
c896fe29 | 1800 | ts = &s->temps[i]; |
ac3b8891 | 1801 | ts->val_type = (ts->fixed_reg ? TEMP_VAL_REG : TEMP_VAL_MEM); |
c896fe29 | 1802 | } |
ac3b8891 | 1803 | for (n = s->nb_temps; i < n; i++) { |
e8996ee0 | 1804 | ts = &s->temps[i]; |
ac3b8891 | 1805 | ts->val_type = (ts->temp_local ? TEMP_VAL_MEM : TEMP_VAL_DEAD); |
e8996ee0 FB |
1806 | ts->mem_allocated = 0; |
1807 | ts->fixed_reg = 0; | |
1808 | } | |
f8b2f202 RH |
1809 | |
1810 | memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp)); | |
c896fe29 FB |
1811 | } |
1812 | ||
f8b2f202 RH |
1813 | static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size, |
1814 | TCGTemp *ts) | |
c896fe29 | 1815 | { |
1807f4c4 | 1816 | int idx = temp_idx(ts); |
ac56dd48 | 1817 | |
fa477d25 | 1818 | if (ts->temp_global) { |
ac56dd48 | 1819 | pstrcpy(buf, buf_size, ts->name); |
f8b2f202 RH |
1820 | } else if (ts->temp_local) { |
1821 | snprintf(buf, buf_size, "loc%d", idx - s->nb_globals); | |
c896fe29 | 1822 | } else { |
f8b2f202 | 1823 | snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals); |
c896fe29 FB |
1824 | } |
1825 | return buf; | |
1826 | } | |
1827 | ||
43439139 RH |
1828 | static char *tcg_get_arg_str(TCGContext *s, char *buf, |
1829 | int buf_size, TCGArg arg) | |
f8b2f202 | 1830 | { |
43439139 | 1831 | return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg)); |
f8b2f202 RH |
1832 | } |
1833 | ||
6e085f72 RH |
1834 | /* Find helper name. */ |
1835 | static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val) | |
4dc81f28 | 1836 | { |
6e085f72 | 1837 | const char *ret = NULL; |
619205fd EC |
1838 | if (helper_table) { |
1839 | TCGHelperInfo *info = g_hash_table_lookup(helper_table, (gpointer)val); | |
72866e82 RH |
1840 | if (info) { |
1841 | ret = info->name; | |
1842 | } | |
4dc81f28 | 1843 | } |
6e085f72 | 1844 | return ret; |
4dc81f28 FB |
1845 | } |
1846 | ||
f48f3ede BS |
1847 | static const char * const cond_name[] = |
1848 | { | |
0aed257f RH |
1849 | [TCG_COND_NEVER] = "never", |
1850 | [TCG_COND_ALWAYS] = "always", | |
f48f3ede BS |
1851 | [TCG_COND_EQ] = "eq", |
1852 | [TCG_COND_NE] = "ne", | |
1853 | [TCG_COND_LT] = "lt", | |
1854 | [TCG_COND_GE] = "ge", | |
1855 | [TCG_COND_LE] = "le", | |
1856 | [TCG_COND_GT] = "gt", | |
1857 | [TCG_COND_LTU] = "ltu", | |
1858 | [TCG_COND_GEU] = "geu", | |
1859 | [TCG_COND_LEU] = "leu", | |
1860 | [TCG_COND_GTU] = "gtu" | |
1861 | }; | |
1862 | ||
f713d6ad RH |
1863 | static const char * const ldst_name[] = |
1864 | { | |
1865 | [MO_UB] = "ub", | |
1866 | [MO_SB] = "sb", | |
1867 | [MO_LEUW] = "leuw", | |
1868 | [MO_LESW] = "lesw", | |
1869 | [MO_LEUL] = "leul", | |
1870 | [MO_LESL] = "lesl", | |
1871 | [MO_LEQ] = "leq", | |
1872 | [MO_BEUW] = "beuw", | |
1873 | [MO_BESW] = "besw", | |
1874 | [MO_BEUL] = "beul", | |
1875 | [MO_BESL] = "besl", | |
1876 | [MO_BEQ] = "beq", | |
1877 | }; | |
1878 | ||
1f00b27f SS |
1879 | static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = { |
1880 | #ifdef ALIGNED_ONLY | |
1881 | [MO_UNALN >> MO_ASHIFT] = "un+", | |
1882 | [MO_ALIGN >> MO_ASHIFT] = "", | |
1883 | #else | |
1884 | [MO_UNALN >> MO_ASHIFT] = "", | |
1885 | [MO_ALIGN >> MO_ASHIFT] = "al+", | |
1886 | #endif | |
1887 | [MO_ALIGN_2 >> MO_ASHIFT] = "al2+", | |
1888 | [MO_ALIGN_4 >> MO_ASHIFT] = "al4+", | |
1889 | [MO_ALIGN_8 >> MO_ASHIFT] = "al8+", | |
1890 | [MO_ALIGN_16 >> MO_ASHIFT] = "al16+", | |
1891 | [MO_ALIGN_32 >> MO_ASHIFT] = "al32+", | |
1892 | [MO_ALIGN_64 >> MO_ASHIFT] = "al64+", | |
1893 | }; | |
1894 | ||
b016486e RH |
1895 | static inline bool tcg_regset_single(TCGRegSet d) |
1896 | { | |
1897 | return (d & (d - 1)) == 0; | |
1898 | } | |
1899 | ||
1900 | static inline TCGReg tcg_regset_first(TCGRegSet d) | |
1901 | { | |
1902 | if (TCG_TARGET_NB_REGS <= 32) { | |
1903 | return ctz32(d); | |
1904 | } else { | |
1905 | return ctz64(d); | |
1906 | } | |
1907 | } | |
1908 | ||
1894f69a | 1909 | static void tcg_dump_ops(TCGContext *s, bool have_prefs) |
c896fe29 | 1910 | { |
c896fe29 | 1911 | char buf[128]; |
c45cb8bb | 1912 | TCGOp *op; |
c45cb8bb | 1913 | |
15fa08f8 | 1914 | QTAILQ_FOREACH(op, &s->ops, link) { |
c45cb8bb RH |
1915 | int i, k, nb_oargs, nb_iargs, nb_cargs; |
1916 | const TCGOpDef *def; | |
c45cb8bb | 1917 | TCGOpcode c; |
bdfb460e | 1918 | int col = 0; |
c896fe29 | 1919 | |
c45cb8bb | 1920 | c = op->opc; |
c896fe29 | 1921 | def = &tcg_op_defs[c]; |
c45cb8bb | 1922 | |
765b842a | 1923 | if (c == INDEX_op_insn_start) { |
b016486e | 1924 | nb_oargs = 0; |
15fa08f8 | 1925 | col += qemu_log("\n ----"); |
9aef40ed RH |
1926 | |
1927 | for (i = 0; i < TARGET_INSN_START_WORDS; ++i) { | |
1928 | target_ulong a; | |
7e4597d7 | 1929 | #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS |
efee3746 | 1930 | a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]); |
7e4597d7 | 1931 | #else |
efee3746 | 1932 | a = op->args[i]; |
7e4597d7 | 1933 | #endif |
bdfb460e | 1934 | col += qemu_log(" " TARGET_FMT_lx, a); |
eeacee4d | 1935 | } |
7e4597d7 | 1936 | } else if (c == INDEX_op_call) { |
c896fe29 | 1937 | /* variable number of arguments */ |
cd9090aa RH |
1938 | nb_oargs = TCGOP_CALLO(op); |
1939 | nb_iargs = TCGOP_CALLI(op); | |
c896fe29 | 1940 | nb_cargs = def->nb_cargs; |
c896fe29 | 1941 | |
cf066674 | 1942 | /* function name, flags, out args */ |
bdfb460e | 1943 | col += qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name, |
efee3746 RH |
1944 | tcg_find_helper(s, op->args[nb_oargs + nb_iargs]), |
1945 | op->args[nb_oargs + nb_iargs + 1], nb_oargs); | |
cf066674 | 1946 | for (i = 0; i < nb_oargs; i++) { |
43439139 RH |
1947 | col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf), |
1948 | op->args[i])); | |
b03cce8e | 1949 | } |
cf066674 | 1950 | for (i = 0; i < nb_iargs; i++) { |
efee3746 | 1951 | TCGArg arg = op->args[nb_oargs + i]; |
cf066674 RH |
1952 | const char *t = "<dummy>"; |
1953 | if (arg != TCG_CALL_DUMMY_ARG) { | |
43439139 | 1954 | t = tcg_get_arg_str(s, buf, sizeof(buf), arg); |
eeacee4d | 1955 | } |
bdfb460e | 1956 | col += qemu_log(",%s", t); |
e8996ee0 | 1957 | } |
b03cce8e | 1958 | } else { |
bdfb460e | 1959 | col += qemu_log(" %s ", def->name); |
c45cb8bb RH |
1960 | |
1961 | nb_oargs = def->nb_oargs; | |
1962 | nb_iargs = def->nb_iargs; | |
1963 | nb_cargs = def->nb_cargs; | |
1964 | ||
d2fd745f RH |
1965 | if (def->flags & TCG_OPF_VECTOR) { |
1966 | col += qemu_log("v%d,e%d,", 64 << TCGOP_VECL(op), | |
1967 | 8 << TCGOP_VECE(op)); | |
1968 | } | |
1969 | ||
b03cce8e | 1970 | k = 0; |
c45cb8bb | 1971 | for (i = 0; i < nb_oargs; i++) { |
eeacee4d | 1972 | if (k != 0) { |
bdfb460e | 1973 | col += qemu_log(","); |
eeacee4d | 1974 | } |
43439139 RH |
1975 | col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf), |
1976 | op->args[k++])); | |
b03cce8e | 1977 | } |
c45cb8bb | 1978 | for (i = 0; i < nb_iargs; i++) { |
eeacee4d | 1979 | if (k != 0) { |
bdfb460e | 1980 | col += qemu_log(","); |
eeacee4d | 1981 | } |
43439139 RH |
1982 | col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf), |
1983 | op->args[k++])); | |
b03cce8e | 1984 | } |
be210acb RH |
1985 | switch (c) { |
1986 | case INDEX_op_brcond_i32: | |
be210acb | 1987 | case INDEX_op_setcond_i32: |
ffc5ea09 | 1988 | case INDEX_op_movcond_i32: |
ffc5ea09 | 1989 | case INDEX_op_brcond2_i32: |
be210acb | 1990 | case INDEX_op_setcond2_i32: |
ffc5ea09 | 1991 | case INDEX_op_brcond_i64: |
be210acb | 1992 | case INDEX_op_setcond_i64: |
ffc5ea09 | 1993 | case INDEX_op_movcond_i64: |
212be173 | 1994 | case INDEX_op_cmp_vec: |
efee3746 RH |
1995 | if (op->args[k] < ARRAY_SIZE(cond_name) |
1996 | && cond_name[op->args[k]]) { | |
1997 | col += qemu_log(",%s", cond_name[op->args[k++]]); | |
eeacee4d | 1998 | } else { |
efee3746 | 1999 | col += qemu_log(",$0x%" TCG_PRIlx, op->args[k++]); |
eeacee4d | 2000 | } |
f48f3ede | 2001 | i = 1; |
be210acb | 2002 | break; |
f713d6ad RH |
2003 | case INDEX_op_qemu_ld_i32: |
2004 | case INDEX_op_qemu_st_i32: | |
2005 | case INDEX_op_qemu_ld_i64: | |
2006 | case INDEX_op_qemu_st_i64: | |
59227d5d | 2007 | { |
efee3746 | 2008 | TCGMemOpIdx oi = op->args[k++]; |
59227d5d RH |
2009 | TCGMemOp op = get_memop(oi); |
2010 | unsigned ix = get_mmuidx(oi); | |
2011 | ||
59c4b7e8 | 2012 | if (op & ~(MO_AMASK | MO_BSWAP | MO_SSIZE)) { |
bdfb460e | 2013 | col += qemu_log(",$0x%x,%u", op, ix); |
59c4b7e8 | 2014 | } else { |
1f00b27f SS |
2015 | const char *s_al, *s_op; |
2016 | s_al = alignment_name[(op & MO_AMASK) >> MO_ASHIFT]; | |
59c4b7e8 | 2017 | s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)]; |
bdfb460e | 2018 | col += qemu_log(",%s%s,%u", s_al, s_op, ix); |
59227d5d RH |
2019 | } |
2020 | i = 1; | |
f713d6ad | 2021 | } |
f713d6ad | 2022 | break; |
be210acb | 2023 | default: |
f48f3ede | 2024 | i = 0; |
be210acb RH |
2025 | break; |
2026 | } | |
51e3972c RH |
2027 | switch (c) { |
2028 | case INDEX_op_set_label: | |
2029 | case INDEX_op_br: | |
2030 | case INDEX_op_brcond_i32: | |
2031 | case INDEX_op_brcond_i64: | |
2032 | case INDEX_op_brcond2_i32: | |
efee3746 RH |
2033 | col += qemu_log("%s$L%d", k ? "," : "", |
2034 | arg_label(op->args[k])->id); | |
51e3972c RH |
2035 | i++, k++; |
2036 | break; | |
2037 | default: | |
2038 | break; | |
2039 | } | |
2040 | for (; i < nb_cargs; i++, k++) { | |
efee3746 | 2041 | col += qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", op->args[k]); |
bdfb460e RH |
2042 | } |
2043 | } | |
bdfb460e | 2044 | |
1894f69a RH |
2045 | if (have_prefs || op->life) { |
2046 | for (; col < 40; ++col) { | |
bdfb460e RH |
2047 | putc(' ', qemu_logfile); |
2048 | } | |
1894f69a RH |
2049 | } |
2050 | ||
2051 | if (op->life) { | |
2052 | unsigned life = op->life; | |
bdfb460e RH |
2053 | |
2054 | if (life & (SYNC_ARG * 3)) { | |
2055 | qemu_log(" sync:"); | |
2056 | for (i = 0; i < 2; ++i) { | |
2057 | if (life & (SYNC_ARG << i)) { | |
2058 | qemu_log(" %d", i); | |
2059 | } | |
2060 | } | |
2061 | } | |
2062 | life /= DEAD_ARG; | |
2063 | if (life) { | |
2064 | qemu_log(" dead:"); | |
2065 | for (i = 0; life; ++i, life >>= 1) { | |
2066 | if (life & 1) { | |
2067 | qemu_log(" %d", i); | |
2068 | } | |
2069 | } | |
b03cce8e | 2070 | } |
c896fe29 | 2071 | } |
1894f69a RH |
2072 | |
2073 | if (have_prefs) { | |
2074 | for (i = 0; i < nb_oargs; ++i) { | |
2075 | TCGRegSet set = op->output_pref[i]; | |
2076 | ||
2077 | if (i == 0) { | |
2078 | qemu_log(" pref="); | |
2079 | } else { | |
2080 | qemu_log(","); | |
2081 | } | |
2082 | if (set == 0) { | |
2083 | qemu_log("none"); | |
2084 | } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) { | |
2085 | qemu_log("all"); | |
2086 | #ifdef CONFIG_DEBUG_TCG | |
2087 | } else if (tcg_regset_single(set)) { | |
2088 | TCGReg reg = tcg_regset_first(set); | |
2089 | qemu_log("%s", tcg_target_reg_names[reg]); | |
2090 | #endif | |
2091 | } else if (TCG_TARGET_NB_REGS <= 32) { | |
2092 | qemu_log("%#x", (uint32_t)set); | |
2093 | } else { | |
2094 | qemu_log("%#" PRIx64, (uint64_t)set); | |
2095 | } | |
2096 | } | |
2097 | } | |
2098 | ||
eeacee4d | 2099 | qemu_log("\n"); |
c896fe29 FB |
2100 | } |
2101 | } | |
2102 | ||
2103 | /* we give more priority to constraints with less registers */ | |
2104 | static int get_constraint_priority(const TCGOpDef *def, int k) | |
2105 | { | |
2106 | const TCGArgConstraint *arg_ct; | |
2107 | ||
2108 | int i, n; | |
2109 | arg_ct = &def->args_ct[k]; | |
2110 | if (arg_ct->ct & TCG_CT_ALIAS) { | |
2111 | /* an alias is equivalent to a single register */ | |
2112 | n = 1; | |
2113 | } else { | |
2114 | if (!(arg_ct->ct & TCG_CT_REG)) | |
2115 | return 0; | |
2116 | n = 0; | |
2117 | for(i = 0; i < TCG_TARGET_NB_REGS; i++) { | |
2118 | if (tcg_regset_test_reg(arg_ct->u.regs, i)) | |
2119 | n++; | |
2120 | } | |
2121 | } | |
2122 | return TCG_TARGET_NB_REGS - n + 1; | |
2123 | } | |
2124 | ||
2125 | /* sort from highest priority to lowest */ | |
2126 | static void sort_constraints(TCGOpDef *def, int start, int n) | |
2127 | { | |
2128 | int i, j, p1, p2, tmp; | |
2129 | ||
2130 | for(i = 0; i < n; i++) | |
2131 | def->sorted_args[start + i] = start + i; | |
2132 | if (n <= 1) | |
2133 | return; | |
2134 | for(i = 0; i < n - 1; i++) { | |
2135 | for(j = i + 1; j < n; j++) { | |
2136 | p1 = get_constraint_priority(def, def->sorted_args[start + i]); | |
2137 | p2 = get_constraint_priority(def, def->sorted_args[start + j]); | |
2138 | if (p1 < p2) { | |
2139 | tmp = def->sorted_args[start + i]; | |
2140 | def->sorted_args[start + i] = def->sorted_args[start + j]; | |
2141 | def->sorted_args[start + j] = tmp; | |
2142 | } | |
2143 | } | |
2144 | } | |
2145 | } | |
2146 | ||
f69d277e | 2147 | static void process_op_defs(TCGContext *s) |
c896fe29 | 2148 | { |
a9751609 | 2149 | TCGOpcode op; |
c896fe29 | 2150 | |
f69d277e RH |
2151 | for (op = 0; op < NB_OPS; op++) { |
2152 | TCGOpDef *def = &tcg_op_defs[op]; | |
2153 | const TCGTargetOpDef *tdefs; | |
069ea736 RH |
2154 | TCGType type; |
2155 | int i, nb_args; | |
f69d277e RH |
2156 | |
2157 | if (def->flags & TCG_OPF_NOT_PRESENT) { | |
2158 | continue; | |
2159 | } | |
2160 | ||
c896fe29 | 2161 | nb_args = def->nb_iargs + def->nb_oargs; |
f69d277e RH |
2162 | if (nb_args == 0) { |
2163 | continue; | |
2164 | } | |
2165 | ||
2166 | tdefs = tcg_target_op_def(op); | |
2167 | /* Missing TCGTargetOpDef entry. */ | |
2168 | tcg_debug_assert(tdefs != NULL); | |
2169 | ||
069ea736 | 2170 | type = (def->flags & TCG_OPF_64BIT ? TCG_TYPE_I64 : TCG_TYPE_I32); |
f69d277e RH |
2171 | for (i = 0; i < nb_args; i++) { |
2172 | const char *ct_str = tdefs->args_ct_str[i]; | |
2173 | /* Incomplete TCGTargetOpDef entry. */ | |
eabb7b91 | 2174 | tcg_debug_assert(ct_str != NULL); |
f69d277e | 2175 | |
ccb1bb66 | 2176 | def->args_ct[i].u.regs = 0; |
c896fe29 | 2177 | def->args_ct[i].ct = 0; |
17280ff4 RH |
2178 | while (*ct_str != '\0') { |
2179 | switch(*ct_str) { | |
2180 | case '0' ... '9': | |
2181 | { | |
2182 | int oarg = *ct_str - '0'; | |
2183 | tcg_debug_assert(ct_str == tdefs->args_ct_str[i]); | |
2184 | tcg_debug_assert(oarg < def->nb_oargs); | |
2185 | tcg_debug_assert(def->args_ct[oarg].ct & TCG_CT_REG); | |
2186 | /* TCG_CT_ALIAS is for the output arguments. | |
2187 | The input is tagged with TCG_CT_IALIAS. */ | |
2188 | def->args_ct[i] = def->args_ct[oarg]; | |
2189 | def->args_ct[oarg].ct |= TCG_CT_ALIAS; | |
2190 | def->args_ct[oarg].alias_index = i; | |
2191 | def->args_ct[i].ct |= TCG_CT_IALIAS; | |
2192 | def->args_ct[i].alias_index = oarg; | |
c896fe29 | 2193 | } |
17280ff4 RH |
2194 | ct_str++; |
2195 | break; | |
2196 | case '&': | |
2197 | def->args_ct[i].ct |= TCG_CT_NEWREG; | |
2198 | ct_str++; | |
2199 | break; | |
2200 | case 'i': | |
2201 | def->args_ct[i].ct |= TCG_CT_CONST; | |
2202 | ct_str++; | |
2203 | break; | |
2204 | default: | |
2205 | ct_str = target_parse_constraint(&def->args_ct[i], | |
2206 | ct_str, type); | |
2207 | /* Typo in TCGTargetOpDef constraint. */ | |
2208 | tcg_debug_assert(ct_str != NULL); | |
c896fe29 FB |
2209 | } |
2210 | } | |
2211 | } | |
2212 | ||
c68aaa18 | 2213 | /* TCGTargetOpDef entry with too much information? */ |
eabb7b91 | 2214 | tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL); |
c68aaa18 | 2215 | |
c896fe29 FB |
2216 | /* sort the constraints (XXX: this is just an heuristic) */ |
2217 | sort_constraints(def, 0, def->nb_oargs); | |
2218 | sort_constraints(def, def->nb_oargs, def->nb_iargs); | |
a9751609 | 2219 | } |
c896fe29 FB |
2220 | } |
2221 | ||
0c627cdc RH |
2222 | void tcg_op_remove(TCGContext *s, TCGOp *op) |
2223 | { | |
d88a117e RH |
2224 | TCGLabel *label; |
2225 | ||
2226 | switch (op->opc) { | |
2227 | case INDEX_op_br: | |
2228 | label = arg_label(op->args[0]); | |
2229 | label->refs--; | |
2230 | break; | |
2231 | case INDEX_op_brcond_i32: | |
2232 | case INDEX_op_brcond_i64: | |
2233 | label = arg_label(op->args[3]); | |
2234 | label->refs--; | |
2235 | break; | |
2236 | case INDEX_op_brcond2_i32: | |
2237 | label = arg_label(op->args[5]); | |
2238 | label->refs--; | |
2239 | break; | |
2240 | default: | |
2241 | break; | |
2242 | } | |
2243 | ||
15fa08f8 RH |
2244 | QTAILQ_REMOVE(&s->ops, op, link); |
2245 | QTAILQ_INSERT_TAIL(&s->free_ops, op, link); | |
abebf925 | 2246 | s->nb_ops--; |
0c627cdc RH |
2247 | |
2248 | #ifdef CONFIG_PROFILER | |
c3fac113 | 2249 | atomic_set(&s->prof.del_op_count, s->prof.del_op_count + 1); |
0c627cdc RH |
2250 | #endif |
2251 | } | |
2252 | ||
15fa08f8 | 2253 | static TCGOp *tcg_op_alloc(TCGOpcode opc) |
5a18407f | 2254 | { |
15fa08f8 RH |
2255 | TCGContext *s = tcg_ctx; |
2256 | TCGOp *op; | |
5a18407f | 2257 | |
15fa08f8 RH |
2258 | if (likely(QTAILQ_EMPTY(&s->free_ops))) { |
2259 | op = tcg_malloc(sizeof(TCGOp)); | |
2260 | } else { | |
2261 | op = QTAILQ_FIRST(&s->free_ops); | |
2262 | QTAILQ_REMOVE(&s->free_ops, op, link); | |
2263 | } | |
2264 | memset(op, 0, offsetof(TCGOp, link)); | |
2265 | op->opc = opc; | |
abebf925 | 2266 | s->nb_ops++; |
5a18407f | 2267 | |
15fa08f8 RH |
2268 | return op; |
2269 | } | |
2270 | ||
2271 | TCGOp *tcg_emit_op(TCGOpcode opc) | |
2272 | { | |
2273 | TCGOp *op = tcg_op_alloc(opc); | |
2274 | QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link); | |
2275 | return op; | |
2276 | } | |
5a18407f | 2277 | |
ac1043f6 | 2278 | TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op, TCGOpcode opc) |
15fa08f8 RH |
2279 | { |
2280 | TCGOp *new_op = tcg_op_alloc(opc); | |
2281 | QTAILQ_INSERT_BEFORE(old_op, new_op, link); | |
5a18407f RH |
2282 | return new_op; |
2283 | } | |
2284 | ||
ac1043f6 | 2285 | TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op, TCGOpcode opc) |
5a18407f | 2286 | { |
15fa08f8 RH |
2287 | TCGOp *new_op = tcg_op_alloc(opc); |
2288 | QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link); | |
5a18407f RH |
2289 | return new_op; |
2290 | } | |
2291 | ||
b4fc67c7 RH |
2292 | /* Reachable analysis : remove unreachable code. */ |
2293 | static void reachable_code_pass(TCGContext *s) | |
2294 | { | |
2295 | TCGOp *op, *op_next; | |
2296 | bool dead = false; | |
2297 | ||
2298 | QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { | |
2299 | bool remove = dead; | |
2300 | TCGLabel *label; | |
2301 | int call_flags; | |
2302 | ||
2303 | switch (op->opc) { | |
2304 | case INDEX_op_set_label: | |
2305 | label = arg_label(op->args[0]); | |
2306 | if (label->refs == 0) { | |
2307 | /* | |
2308 | * While there is an occasional backward branch, virtually | |
2309 | * all branches generated by the translators are forward. | |
2310 | * Which means that generally we will have already removed | |
2311 | * all references to the label that will be, and there is | |
2312 | * little to be gained by iterating. | |
2313 | */ | |
2314 | remove = true; | |
2315 | } else { | |
2316 | /* Once we see a label, insns become live again. */ | |
2317 | dead = false; | |
2318 | remove = false; | |
2319 | ||
2320 | /* | |
2321 | * Optimization can fold conditional branches to unconditional. | |
2322 | * If we find a label with one reference which is preceded by | |
2323 | * an unconditional branch to it, remove both. This needed to | |
2324 | * wait until the dead code in between them was removed. | |
2325 | */ | |
2326 | if (label->refs == 1) { | |
eae3eb3e | 2327 | TCGOp *op_prev = QTAILQ_PREV(op, link); |
b4fc67c7 RH |
2328 | if (op_prev->opc == INDEX_op_br && |
2329 | label == arg_label(op_prev->args[0])) { | |
2330 | tcg_op_remove(s, op_prev); | |
2331 | remove = true; | |
2332 | } | |
2333 | } | |
2334 | } | |
2335 | break; | |
2336 | ||
2337 | case INDEX_op_br: | |
2338 | case INDEX_op_exit_tb: | |
2339 | case INDEX_op_goto_ptr: | |
2340 | /* Unconditional branches; everything following is dead. */ | |
2341 | dead = true; | |
2342 | break; | |
2343 | ||
2344 | case INDEX_op_call: | |
2345 | /* Notice noreturn helper calls, raising exceptions. */ | |
2346 | call_flags = op->args[TCGOP_CALLO(op) + TCGOP_CALLI(op) + 1]; | |
2347 | if (call_flags & TCG_CALL_NO_RETURN) { | |
2348 | dead = true; | |
2349 | } | |
2350 | break; | |
2351 | ||
2352 | case INDEX_op_insn_start: | |
2353 | /* Never remove -- we need to keep these for unwind. */ | |
2354 | remove = false; | |
2355 | break; | |
2356 | ||
2357 | default: | |
2358 | break; | |
2359 | } | |
2360 | ||
2361 | if (remove) { | |
2362 | tcg_op_remove(s, op); | |
2363 | } | |
2364 | } | |
2365 | } | |
2366 | ||
c70fbf0a RH |
2367 | #define TS_DEAD 1 |
2368 | #define TS_MEM 2 | |
2369 | ||
5a18407f RH |
2370 | #define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n))) |
2371 | #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n))) | |
2372 | ||
25f49c5f RH |
2373 | /* For liveness_pass_1, the register preferences for a given temp. */ |
2374 | static inline TCGRegSet *la_temp_pref(TCGTemp *ts) | |
2375 | { | |
2376 | return ts->state_ptr; | |
2377 | } | |
2378 | ||
2379 | /* For liveness_pass_1, reset the preferences for a given temp to the | |
2380 | * maximal regset for its type. | |
2381 | */ | |
2382 | static inline void la_reset_pref(TCGTemp *ts) | |
2383 | { | |
2384 | *la_temp_pref(ts) | |
2385 | = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]); | |
2386 | } | |
2387 | ||
9c43b68d AJ |
2388 | /* liveness analysis: end of function: all temps are dead, and globals |
2389 | should be in memory. */ | |
2616c808 | 2390 | static void la_func_end(TCGContext *s, int ng, int nt) |
c896fe29 | 2391 | { |
b83eabea RH |
2392 | int i; |
2393 | ||
2394 | for (i = 0; i < ng; ++i) { | |
2395 | s->temps[i].state = TS_DEAD | TS_MEM; | |
25f49c5f | 2396 | la_reset_pref(&s->temps[i]); |
b83eabea RH |
2397 | } |
2398 | for (i = ng; i < nt; ++i) { | |
2399 | s->temps[i].state = TS_DEAD; | |
25f49c5f | 2400 | la_reset_pref(&s->temps[i]); |
b83eabea | 2401 | } |
c896fe29 FB |
2402 | } |
2403 | ||
9c43b68d AJ |
2404 | /* liveness analysis: end of basic block: all temps are dead, globals |
2405 | and local temps should be in memory. */ | |
2616c808 | 2406 | static void la_bb_end(TCGContext *s, int ng, int nt) |
641d5fbe | 2407 | { |
b83eabea | 2408 | int i; |
641d5fbe | 2409 | |
b83eabea RH |
2410 | for (i = 0; i < ng; ++i) { |
2411 | s->temps[i].state = TS_DEAD | TS_MEM; | |
25f49c5f | 2412 | la_reset_pref(&s->temps[i]); |
b83eabea RH |
2413 | } |
2414 | for (i = ng; i < nt; ++i) { | |
2415 | s->temps[i].state = (s->temps[i].temp_local | |
2416 | ? TS_DEAD | TS_MEM | |
2417 | : TS_DEAD); | |
25f49c5f | 2418 | la_reset_pref(&s->temps[i]); |
641d5fbe FB |
2419 | } |
2420 | } | |
2421 | ||
f65a061c RH |
2422 | /* liveness analysis: sync globals back to memory. */ |
2423 | static void la_global_sync(TCGContext *s, int ng) | |
2424 | { | |
2425 | int i; | |
2426 | ||
2427 | for (i = 0; i < ng; ++i) { | |
25f49c5f RH |
2428 | int state = s->temps[i].state; |
2429 | s->temps[i].state = state | TS_MEM; | |
2430 | if (state == TS_DEAD) { | |
2431 | /* If the global was previously dead, reset prefs. */ | |
2432 | la_reset_pref(&s->temps[i]); | |
2433 | } | |
f65a061c RH |
2434 | } |
2435 | } | |
2436 | ||
2437 | /* liveness analysis: sync globals back to memory and kill. */ | |
2438 | static void la_global_kill(TCGContext *s, int ng) | |
2439 | { | |
2440 | int i; | |
2441 | ||
2442 | for (i = 0; i < ng; i++) { | |
2443 | s->temps[i].state = TS_DEAD | TS_MEM; | |
25f49c5f RH |
2444 | la_reset_pref(&s->temps[i]); |
2445 | } | |
2446 | } | |
2447 | ||
2448 | /* liveness analysis: note live globals crossing calls. */ | |
2449 | static void la_cross_call(TCGContext *s, int nt) | |
2450 | { | |
2451 | TCGRegSet mask = ~tcg_target_call_clobber_regs; | |
2452 | int i; | |
2453 | ||
2454 | for (i = 0; i < nt; i++) { | |
2455 | TCGTemp *ts = &s->temps[i]; | |
2456 | if (!(ts->state & TS_DEAD)) { | |
2457 | TCGRegSet *pset = la_temp_pref(ts); | |
2458 | TCGRegSet set = *pset; | |
2459 | ||
2460 | set &= mask; | |
2461 | /* If the combination is not possible, restart. */ | |
2462 | if (set == 0) { | |
2463 | set = tcg_target_available_regs[ts->type] & mask; | |
2464 | } | |
2465 | *pset = set; | |
2466 | } | |
f65a061c RH |
2467 | } |
2468 | } | |
2469 | ||
a1b3c48d | 2470 | /* Liveness analysis : update the opc_arg_life array to tell if a |
c896fe29 FB |
2471 | given input arguments is dead. Instructions updating dead |
2472 | temporaries are removed. */ | |
b83eabea | 2473 | static void liveness_pass_1(TCGContext *s) |
c896fe29 | 2474 | { |
c70fbf0a | 2475 | int nb_globals = s->nb_globals; |
2616c808 | 2476 | int nb_temps = s->nb_temps; |
15fa08f8 | 2477 | TCGOp *op, *op_prev; |
25f49c5f RH |
2478 | TCGRegSet *prefs; |
2479 | int i; | |
2480 | ||
2481 | prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps); | |
2482 | for (i = 0; i < nb_temps; ++i) { | |
2483 | s->temps[i].state_ptr = prefs + i; | |
2484 | } | |
a1b3c48d | 2485 | |
ae36a246 | 2486 | /* ??? Should be redundant with the exit_tb that ends the TB. */ |
2616c808 | 2487 | la_func_end(s, nb_globals, nb_temps); |
c896fe29 | 2488 | |
eae3eb3e | 2489 | QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) { |
25f49c5f | 2490 | int nb_iargs, nb_oargs; |
c45cb8bb RH |
2491 | TCGOpcode opc_new, opc_new2; |
2492 | bool have_opc_new2; | |
a1b3c48d | 2493 | TCGLifeData arg_life = 0; |
25f49c5f | 2494 | TCGTemp *ts; |
c45cb8bb RH |
2495 | TCGOpcode opc = op->opc; |
2496 | const TCGOpDef *def = &tcg_op_defs[opc]; | |
2497 | ||
c45cb8bb | 2498 | switch (opc) { |
c896fe29 | 2499 | case INDEX_op_call: |
c6e113f5 FB |
2500 | { |
2501 | int call_flags; | |
25f49c5f | 2502 | int nb_call_regs; |
c896fe29 | 2503 | |
cd9090aa RH |
2504 | nb_oargs = TCGOP_CALLO(op); |
2505 | nb_iargs = TCGOP_CALLI(op); | |
efee3746 | 2506 | call_flags = op->args[nb_oargs + nb_iargs + 1]; |
c6e113f5 | 2507 | |
c45cb8bb | 2508 | /* pure functions can be removed if their result is unused */ |
78505279 | 2509 | if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) { |
cf066674 | 2510 | for (i = 0; i < nb_oargs; i++) { |
25f49c5f RH |
2511 | ts = arg_temp(op->args[i]); |
2512 | if (ts->state != TS_DEAD) { | |
c6e113f5 | 2513 | goto do_not_remove_call; |
9c43b68d | 2514 | } |
c6e113f5 | 2515 | } |
c45cb8bb | 2516 | goto do_remove; |
152c35aa RH |
2517 | } |
2518 | do_not_remove_call: | |
c896fe29 | 2519 | |
25f49c5f | 2520 | /* Output args are dead. */ |
152c35aa | 2521 | for (i = 0; i < nb_oargs; i++) { |
25f49c5f RH |
2522 | ts = arg_temp(op->args[i]); |
2523 | if (ts->state & TS_DEAD) { | |
152c35aa RH |
2524 | arg_life |= DEAD_ARG << i; |
2525 | } | |
25f49c5f | 2526 | if (ts->state & TS_MEM) { |
152c35aa | 2527 | arg_life |= SYNC_ARG << i; |
c6e113f5 | 2528 | } |
25f49c5f RH |
2529 | ts->state = TS_DEAD; |
2530 | la_reset_pref(ts); | |
2531 | ||
2532 | /* Not used -- it will be tcg_target_call_oarg_regs[i]. */ | |
2533 | op->output_pref[i] = 0; | |
152c35aa | 2534 | } |
78505279 | 2535 | |
152c35aa RH |
2536 | if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS | |
2537 | TCG_CALL_NO_READ_GLOBALS))) { | |
f65a061c | 2538 | la_global_kill(s, nb_globals); |
152c35aa | 2539 | } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) { |
f65a061c | 2540 | la_global_sync(s, nb_globals); |
152c35aa | 2541 | } |
b9c18f56 | 2542 | |
25f49c5f | 2543 | /* Record arguments that die in this helper. */ |
152c35aa | 2544 | for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { |
25f49c5f RH |
2545 | ts = arg_temp(op->args[i]); |
2546 | if (ts && ts->state & TS_DEAD) { | |
152c35aa | 2547 | arg_life |= DEAD_ARG << i; |
c6e113f5 | 2548 | } |
152c35aa | 2549 | } |
25f49c5f RH |
2550 | |
2551 | /* For all live registers, remove call-clobbered prefs. */ | |
2552 | la_cross_call(s, nb_temps); | |
2553 | ||
2554 | nb_call_regs = ARRAY_SIZE(tcg_target_call_iarg_regs); | |
2555 | ||
2556 | /* Input arguments are live for preceding opcodes. */ | |
2557 | for (i = 0; i < nb_iargs; i++) { | |
2558 | ts = arg_temp(op->args[i + nb_oargs]); | |
2559 | if (ts && ts->state & TS_DEAD) { | |
2560 | /* For those arguments that die, and will be allocated | |
2561 | * in registers, clear the register set for that arg, | |
2562 | * to be filled in below. For args that will be on | |
2563 | * the stack, reset to any available reg. | |
2564 | */ | |
2565 | *la_temp_pref(ts) | |
2566 | = (i < nb_call_regs ? 0 : | |
2567 | tcg_target_available_regs[ts->type]); | |
2568 | ts->state &= ~TS_DEAD; | |
2569 | } | |
2570 | } | |
2571 | ||
2572 | /* For each input argument, add its input register to prefs. | |
2573 | If a temp is used once, this produces a single set bit. */ | |
2574 | for (i = 0; i < MIN(nb_call_regs, nb_iargs); i++) { | |
2575 | ts = arg_temp(op->args[i + nb_oargs]); | |
2576 | if (ts) { | |
2577 | tcg_regset_set_reg(*la_temp_pref(ts), | |
2578 | tcg_target_call_iarg_regs[i]); | |
c19f47bf | 2579 | } |
c896fe29 | 2580 | } |
c896fe29 | 2581 | } |
c896fe29 | 2582 | break; |
765b842a | 2583 | case INDEX_op_insn_start: |
c896fe29 | 2584 | break; |
5ff9d6a4 | 2585 | case INDEX_op_discard: |
5ff9d6a4 | 2586 | /* mark the temporary as dead */ |
25f49c5f RH |
2587 | ts = arg_temp(op->args[0]); |
2588 | ts->state = TS_DEAD; | |
2589 | la_reset_pref(ts); | |
5ff9d6a4 | 2590 | break; |
1305c451 RH |
2591 | |
2592 | case INDEX_op_add2_i32: | |
c45cb8bb | 2593 | opc_new = INDEX_op_add_i32; |
f1fae40c | 2594 | goto do_addsub2; |
1305c451 | 2595 | case INDEX_op_sub2_i32: |
c45cb8bb | 2596 | opc_new = INDEX_op_sub_i32; |
f1fae40c RH |
2597 | goto do_addsub2; |
2598 | case INDEX_op_add2_i64: | |
c45cb8bb | 2599 | opc_new = INDEX_op_add_i64; |
f1fae40c RH |
2600 | goto do_addsub2; |
2601 | case INDEX_op_sub2_i64: | |
c45cb8bb | 2602 | opc_new = INDEX_op_sub_i64; |
f1fae40c | 2603 | do_addsub2: |
1305c451 RH |
2604 | nb_iargs = 4; |
2605 | nb_oargs = 2; | |
2606 | /* Test if the high part of the operation is dead, but not | |
2607 | the low part. The result can be optimized to a simple | |
2608 | add or sub. This happens often for x86_64 guest when the | |
2609 | cpu mode is set to 32 bit. */ | |
b83eabea RH |
2610 | if (arg_temp(op->args[1])->state == TS_DEAD) { |
2611 | if (arg_temp(op->args[0])->state == TS_DEAD) { | |
1305c451 RH |
2612 | goto do_remove; |
2613 | } | |
c45cb8bb RH |
2614 | /* Replace the opcode and adjust the args in place, |
2615 | leaving 3 unused args at the end. */ | |
2616 | op->opc = opc = opc_new; | |
efee3746 RH |
2617 | op->args[1] = op->args[2]; |
2618 | op->args[2] = op->args[4]; | |
1305c451 RH |
2619 | /* Fall through and mark the single-word operation live. */ |
2620 | nb_iargs = 2; | |
2621 | nb_oargs = 1; | |
2622 | } | |
2623 | goto do_not_remove; | |
2624 | ||
1414968a | 2625 | case INDEX_op_mulu2_i32: |
c45cb8bb RH |
2626 | opc_new = INDEX_op_mul_i32; |
2627 | opc_new2 = INDEX_op_muluh_i32; | |
2628 | have_opc_new2 = TCG_TARGET_HAS_muluh_i32; | |
03271524 | 2629 | goto do_mul2; |
f1fae40c | 2630 | case INDEX_op_muls2_i32: |
c45cb8bb RH |
2631 | opc_new = INDEX_op_mul_i32; |
2632 | opc_new2 = INDEX_op_mulsh_i32; | |
2633 | have_opc_new2 = TCG_TARGET_HAS_mulsh_i32; | |
f1fae40c RH |
2634 | goto do_mul2; |
2635 | case INDEX_op_mulu2_i64: | |
c45cb8bb RH |
2636 | opc_new = INDEX_op_mul_i64; |
2637 | opc_new2 = INDEX_op_muluh_i64; | |
2638 | have_opc_new2 = TCG_TARGET_HAS_muluh_i64; | |
03271524 | 2639 | goto do_mul2; |
f1fae40c | 2640 | case INDEX_op_muls2_i64: |
c45cb8bb RH |
2641 | opc_new = INDEX_op_mul_i64; |
2642 | opc_new2 = INDEX_op_mulsh_i64; | |
2643 | have_opc_new2 = TCG_TARGET_HAS_mulsh_i64; | |
03271524 | 2644 | goto do_mul2; |
f1fae40c | 2645 | do_mul2: |
1414968a RH |
2646 | nb_iargs = 2; |
2647 | nb_oargs = 2; | |
b83eabea RH |
2648 | if (arg_temp(op->args[1])->state == TS_DEAD) { |
2649 | if (arg_temp(op->args[0])->state == TS_DEAD) { | |
03271524 | 2650 | /* Both parts of the operation are dead. */ |
1414968a RH |
2651 | goto do_remove; |
2652 | } | |
03271524 | 2653 | /* The high part of the operation is dead; generate the low. */ |
c45cb8bb | 2654 | op->opc = opc = opc_new; |
efee3746 RH |
2655 | op->args[1] = op->args[2]; |
2656 | op->args[2] = op->args[3]; | |
b83eabea | 2657 | } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) { |
c45cb8bb RH |
2658 | /* The low part of the operation is dead; generate the high. */ |
2659 | op->opc = opc = opc_new2; | |
efee3746 RH |
2660 | op->args[0] = op->args[1]; |
2661 | op->args[1] = op->args[2]; | |
2662 | op->args[2] = op->args[3]; | |
03271524 RH |
2663 | } else { |
2664 | goto do_not_remove; | |
1414968a | 2665 | } |
03271524 RH |
2666 | /* Mark the single-word operation live. */ |
2667 | nb_oargs = 1; | |
1414968a RH |
2668 | goto do_not_remove; |
2669 | ||
c896fe29 | 2670 | default: |
1305c451 | 2671 | /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */ |
49516bc0 AJ |
2672 | nb_iargs = def->nb_iargs; |
2673 | nb_oargs = def->nb_oargs; | |
c896fe29 | 2674 | |
49516bc0 AJ |
2675 | /* Test if the operation can be removed because all |
2676 | its outputs are dead. We assume that nb_oargs == 0 | |
2677 | implies side effects */ | |
2678 | if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) { | |
c45cb8bb | 2679 | for (i = 0; i < nb_oargs; i++) { |
b83eabea | 2680 | if (arg_temp(op->args[i])->state != TS_DEAD) { |
49516bc0 | 2681 | goto do_not_remove; |
9c43b68d | 2682 | } |
49516bc0 | 2683 | } |
152c35aa RH |
2684 | goto do_remove; |
2685 | } | |
2686 | goto do_not_remove; | |
49516bc0 | 2687 | |
152c35aa RH |
2688 | do_remove: |
2689 | tcg_op_remove(s, op); | |
2690 | break; | |
2691 | ||
2692 | do_not_remove: | |
152c35aa | 2693 | for (i = 0; i < nb_oargs; i++) { |
25f49c5f RH |
2694 | ts = arg_temp(op->args[i]); |
2695 | ||
2696 | /* Remember the preference of the uses that followed. */ | |
2697 | op->output_pref[i] = *la_temp_pref(ts); | |
2698 | ||
2699 | /* Output args are dead. */ | |
2700 | if (ts->state & TS_DEAD) { | |
152c35aa | 2701 | arg_life |= DEAD_ARG << i; |
49516bc0 | 2702 | } |
25f49c5f | 2703 | if (ts->state & TS_MEM) { |
152c35aa RH |
2704 | arg_life |= SYNC_ARG << i; |
2705 | } | |
25f49c5f RH |
2706 | ts->state = TS_DEAD; |
2707 | la_reset_pref(ts); | |
152c35aa | 2708 | } |
49516bc0 | 2709 | |
25f49c5f | 2710 | /* If end of basic block, update. */ |
ae36a246 RH |
2711 | if (def->flags & TCG_OPF_BB_EXIT) { |
2712 | la_func_end(s, nb_globals, nb_temps); | |
2713 | } else if (def->flags & TCG_OPF_BB_END) { | |
2616c808 | 2714 | la_bb_end(s, nb_globals, nb_temps); |
152c35aa | 2715 | } else if (def->flags & TCG_OPF_SIDE_EFFECTS) { |
f65a061c | 2716 | la_global_sync(s, nb_globals); |
25f49c5f RH |
2717 | if (def->flags & TCG_OPF_CALL_CLOBBER) { |
2718 | la_cross_call(s, nb_temps); | |
2719 | } | |
152c35aa RH |
2720 | } |
2721 | ||
25f49c5f | 2722 | /* Record arguments that die in this opcode. */ |
152c35aa | 2723 | for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { |
25f49c5f RH |
2724 | ts = arg_temp(op->args[i]); |
2725 | if (ts->state & TS_DEAD) { | |
152c35aa | 2726 | arg_life |= DEAD_ARG << i; |
c896fe29 | 2727 | } |
c896fe29 | 2728 | } |
25f49c5f RH |
2729 | |
2730 | /* Input arguments are live for preceding opcodes. */ | |
152c35aa | 2731 | for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { |
25f49c5f RH |
2732 | ts = arg_temp(op->args[i]); |
2733 | if (ts->state & TS_DEAD) { | |
2734 | /* For operands that were dead, initially allow | |
2735 | all regs for the type. */ | |
2736 | *la_temp_pref(ts) = tcg_target_available_regs[ts->type]; | |
2737 | ts->state &= ~TS_DEAD; | |
2738 | } | |
2739 | } | |
2740 | ||
2741 | /* Incorporate constraints for this operand. */ | |
2742 | switch (opc) { | |
2743 | case INDEX_op_mov_i32: | |
2744 | case INDEX_op_mov_i64: | |
2745 | /* Note that these are TCG_OPF_NOT_PRESENT and do not | |
2746 | have proper constraints. That said, special case | |
2747 | moves to propagate preferences backward. */ | |
2748 | if (IS_DEAD_ARG(1)) { | |
2749 | *la_temp_pref(arg_temp(op->args[0])) | |
2750 | = *la_temp_pref(arg_temp(op->args[1])); | |
2751 | } | |
2752 | break; | |
2753 | ||
2754 | default: | |
2755 | for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { | |
2756 | const TCGArgConstraint *ct = &def->args_ct[i]; | |
2757 | TCGRegSet set, *pset; | |
2758 | ||
2759 | ts = arg_temp(op->args[i]); | |
2760 | pset = la_temp_pref(ts); | |
2761 | set = *pset; | |
2762 | ||
2763 | set &= ct->u.regs; | |
2764 | if (ct->ct & TCG_CT_IALIAS) { | |
2765 | set &= op->output_pref[ct->alias_index]; | |
2766 | } | |
2767 | /* If the combination is not possible, restart. */ | |
2768 | if (set == 0) { | |
2769 | set = ct->u.regs; | |
2770 | } | |
2771 | *pset = set; | |
2772 | } | |
2773 | break; | |
152c35aa | 2774 | } |
c896fe29 FB |
2775 | break; |
2776 | } | |
bee158cb | 2777 | op->life = arg_life; |
1ff0a2c5 | 2778 | } |
c896fe29 | 2779 | } |
c896fe29 | 2780 | |
5a18407f | 2781 | /* Liveness analysis: Convert indirect regs to direct temporaries. */ |
b83eabea | 2782 | static bool liveness_pass_2(TCGContext *s) |
5a18407f RH |
2783 | { |
2784 | int nb_globals = s->nb_globals; | |
15fa08f8 | 2785 | int nb_temps, i; |
5a18407f | 2786 | bool changes = false; |
15fa08f8 | 2787 | TCGOp *op, *op_next; |
5a18407f | 2788 | |
5a18407f RH |
2789 | /* Create a temporary for each indirect global. */ |
2790 | for (i = 0; i < nb_globals; ++i) { | |
2791 | TCGTemp *its = &s->temps[i]; | |
2792 | if (its->indirect_reg) { | |
2793 | TCGTemp *dts = tcg_temp_alloc(s); | |
2794 | dts->type = its->type; | |
2795 | dts->base_type = its->base_type; | |
b83eabea RH |
2796 | its->state_ptr = dts; |
2797 | } else { | |
2798 | its->state_ptr = NULL; | |
5a18407f | 2799 | } |
b83eabea RH |
2800 | /* All globals begin dead. */ |
2801 | its->state = TS_DEAD; | |
2802 | } | |
2803 | for (nb_temps = s->nb_temps; i < nb_temps; ++i) { | |
2804 | TCGTemp *its = &s->temps[i]; | |
2805 | its->state_ptr = NULL; | |
2806 | its->state = TS_DEAD; | |
5a18407f | 2807 | } |
5a18407f | 2808 | |
15fa08f8 | 2809 | QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { |
5a18407f RH |
2810 | TCGOpcode opc = op->opc; |
2811 | const TCGOpDef *def = &tcg_op_defs[opc]; | |
2812 | TCGLifeData arg_life = op->life; | |
2813 | int nb_iargs, nb_oargs, call_flags; | |
b83eabea | 2814 | TCGTemp *arg_ts, *dir_ts; |
5a18407f | 2815 | |
5a18407f | 2816 | if (opc == INDEX_op_call) { |
cd9090aa RH |
2817 | nb_oargs = TCGOP_CALLO(op); |
2818 | nb_iargs = TCGOP_CALLI(op); | |
efee3746 | 2819 | call_flags = op->args[nb_oargs + nb_iargs + 1]; |
5a18407f RH |
2820 | } else { |
2821 | nb_iargs = def->nb_iargs; | |
2822 | nb_oargs = def->nb_oargs; | |
2823 | ||
2824 | /* Set flags similar to how calls require. */ | |
2825 | if (def->flags & TCG_OPF_BB_END) { | |
2826 | /* Like writing globals: save_globals */ | |
2827 | call_flags = 0; | |
2828 | } else if (def->flags & TCG_OPF_SIDE_EFFECTS) { | |
2829 | /* Like reading globals: sync_globals */ | |
2830 | call_flags = TCG_CALL_NO_WRITE_GLOBALS; | |
2831 | } else { | |
2832 | /* No effect on globals. */ | |
2833 | call_flags = (TCG_CALL_NO_READ_GLOBALS | | |
2834 | TCG_CALL_NO_WRITE_GLOBALS); | |
2835 | } | |
2836 | } | |
2837 | ||
2838 | /* Make sure that input arguments are available. */ | |
2839 | for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { | |
b83eabea RH |
2840 | arg_ts = arg_temp(op->args[i]); |
2841 | if (arg_ts) { | |
2842 | dir_ts = arg_ts->state_ptr; | |
2843 | if (dir_ts && arg_ts->state == TS_DEAD) { | |
2844 | TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32 | |
5a18407f RH |
2845 | ? INDEX_op_ld_i32 |
2846 | : INDEX_op_ld_i64); | |
ac1043f6 | 2847 | TCGOp *lop = tcg_op_insert_before(s, op, lopc); |
5a18407f | 2848 | |
b83eabea RH |
2849 | lop->args[0] = temp_arg(dir_ts); |
2850 | lop->args[1] = temp_arg(arg_ts->mem_base); | |
2851 | lop->args[2] = arg_ts->mem_offset; | |
5a18407f RH |
2852 | |
2853 | /* Loaded, but synced with memory. */ | |
b83eabea | 2854 | arg_ts->state = TS_MEM; |
5a18407f RH |
2855 | } |
2856 | } | |
2857 | } | |
2858 | ||
2859 | /* Perform input replacement, and mark inputs that became dead. | |
2860 | No action is required except keeping temp_state up to date | |
2861 | so that we reload when needed. */ | |
2862 | for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { | |
b83eabea RH |
2863 | arg_ts = arg_temp(op->args[i]); |
2864 | if (arg_ts) { | |
2865 | dir_ts = arg_ts->state_ptr; | |
2866 | if (dir_ts) { | |
2867 | op->args[i] = temp_arg(dir_ts); | |
5a18407f RH |
2868 | changes = true; |
2869 | if (IS_DEAD_ARG(i)) { | |
b83eabea | 2870 | arg_ts->state = TS_DEAD; |
5a18407f RH |
2871 | } |
2872 | } | |
2873 | } | |
2874 | } | |
2875 | ||
2876 | /* Liveness analysis should ensure that the following are | |
2877 | all correct, for call sites and basic block end points. */ | |
2878 | if (call_flags & TCG_CALL_NO_READ_GLOBALS) { | |
2879 | /* Nothing to do */ | |
2880 | } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) { | |
2881 | for (i = 0; i < nb_globals; ++i) { | |
2882 | /* Liveness should see that globals are synced back, | |
2883 | that is, either TS_DEAD or TS_MEM. */ | |
b83eabea RH |
2884 | arg_ts = &s->temps[i]; |
2885 | tcg_debug_assert(arg_ts->state_ptr == 0 | |
2886 | || arg_ts->state != 0); | |
5a18407f RH |
2887 | } |
2888 | } else { | |
2889 | for (i = 0; i < nb_globals; ++i) { | |
2890 | /* Liveness should see that globals are saved back, | |
2891 | that is, TS_DEAD, waiting to be reloaded. */ | |
b83eabea RH |
2892 | arg_ts = &s->temps[i]; |
2893 | tcg_debug_assert(arg_ts->state_ptr == 0 | |
2894 | || arg_ts->state == TS_DEAD); | |
5a18407f RH |
2895 | } |
2896 | } | |
2897 | ||
2898 | /* Outputs become available. */ | |
2899 | for (i = 0; i < nb_oargs; i++) { | |
b83eabea RH |
2900 | arg_ts = arg_temp(op->args[i]); |
2901 | dir_ts = arg_ts->state_ptr; | |
2902 | if (!dir_ts) { | |
5a18407f RH |
2903 | continue; |
2904 | } | |
b83eabea | 2905 | op->args[i] = temp_arg(dir_ts); |
5a18407f RH |
2906 | changes = true; |
2907 | ||
2908 | /* The output is now live and modified. */ | |
b83eabea | 2909 | arg_ts->state = 0; |
5a18407f RH |
2910 | |
2911 | /* Sync outputs upon their last write. */ | |
2912 | if (NEED_SYNC_ARG(i)) { | |
b83eabea | 2913 | TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32 |
5a18407f RH |
2914 | ? INDEX_op_st_i32 |
2915 | : INDEX_op_st_i64); | |
ac1043f6 | 2916 | TCGOp *sop = tcg_op_insert_after(s, op, sopc); |
5a18407f | 2917 | |
b83eabea RH |
2918 | sop->args[0] = temp_arg(dir_ts); |
2919 | sop->args[1] = temp_arg(arg_ts->mem_base); | |
2920 | sop->args[2] = arg_ts->mem_offset; | |
5a18407f | 2921 | |
b83eabea | 2922 | arg_ts->state = TS_MEM; |
5a18407f RH |
2923 | } |
2924 | /* Drop outputs that are dead. */ | |
2925 | if (IS_DEAD_ARG(i)) { | |
b83eabea | 2926 | arg_ts->state = TS_DEAD; |
5a18407f RH |
2927 | } |
2928 | } | |
2929 | } | |
2930 | ||
2931 | return changes; | |
2932 | } | |
2933 | ||
8d8fdbae | 2934 | #ifdef CONFIG_DEBUG_TCG |
c896fe29 FB |
2935 | static void dump_regs(TCGContext *s) |
2936 | { | |
2937 | TCGTemp *ts; | |
2938 | int i; | |
2939 | char buf[64]; | |
2940 | ||
2941 | for(i = 0; i < s->nb_temps; i++) { | |
2942 | ts = &s->temps[i]; | |
43439139 | 2943 | printf(" %10s: ", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts)); |
c896fe29 FB |
2944 | switch(ts->val_type) { |
2945 | case TEMP_VAL_REG: | |
2946 | printf("%s", tcg_target_reg_names[ts->reg]); | |
2947 | break; | |
2948 | case TEMP_VAL_MEM: | |
b3a62939 RH |
2949 | printf("%d(%s)", (int)ts->mem_offset, |
2950 | tcg_target_reg_names[ts->mem_base->reg]); | |
c896fe29 FB |
2951 | break; |
2952 | case TEMP_VAL_CONST: | |
2953 | printf("$0x%" TCG_PRIlx, ts->val); | |
2954 | break; | |
2955 | case TEMP_VAL_DEAD: | |
2956 | printf("D"); | |
2957 | break; | |
2958 | default: | |
2959 | printf("???"); | |
2960 | break; | |
2961 | } | |
2962 | printf("\n"); | |
2963 | } | |
2964 | ||
2965 | for(i = 0; i < TCG_TARGET_NB_REGS; i++) { | |
f8b2f202 | 2966 | if (s->reg_to_temp[i] != NULL) { |
c896fe29 FB |
2967 | printf("%s: %s\n", |
2968 | tcg_target_reg_names[i], | |
f8b2f202 | 2969 | tcg_get_arg_str_ptr(s, buf, sizeof(buf), s->reg_to_temp[i])); |
c896fe29 FB |
2970 | } |
2971 | } | |
2972 | } | |
2973 | ||
2974 | static void check_regs(TCGContext *s) | |
2975 | { | |
869938ae | 2976 | int reg; |
b6638662 | 2977 | int k; |
c896fe29 FB |
2978 | TCGTemp *ts; |
2979 | char buf[64]; | |
2980 | ||
f8b2f202 RH |
2981 | for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { |
2982 | ts = s->reg_to_temp[reg]; | |
2983 | if (ts != NULL) { | |
2984 | if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) { | |
c896fe29 FB |
2985 | printf("Inconsistency for register %s:\n", |
2986 | tcg_target_reg_names[reg]); | |
b03cce8e | 2987 | goto fail; |
c896fe29 FB |
2988 | } |
2989 | } | |
2990 | } | |
f8b2f202 | 2991 | for (k = 0; k < s->nb_temps; k++) { |
c896fe29 | 2992 | ts = &s->temps[k]; |
f8b2f202 RH |
2993 | if (ts->val_type == TEMP_VAL_REG && !ts->fixed_reg |
2994 | && s->reg_to_temp[ts->reg] != ts) { | |
2995 | printf("Inconsistency for temp %s:\n", | |
2996 | tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts)); | |
b03cce8e | 2997 | fail: |
f8b2f202 RH |
2998 | printf("reg state:\n"); |
2999 | dump_regs(s); | |
3000 | tcg_abort(); | |
c896fe29 FB |
3001 | } |
3002 | } | |
3003 | } | |
3004 | #endif | |
3005 | ||
2272e4a7 | 3006 | static void temp_allocate_frame(TCGContext *s, TCGTemp *ts) |
c896fe29 | 3007 | { |
9b9c37c3 RH |
3008 | #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64) |
3009 | /* Sparc64 stack is accessed with offset of 2047 */ | |
b591dc59 BS |
3010 | s->current_frame_offset = (s->current_frame_offset + |
3011 | (tcg_target_long)sizeof(tcg_target_long) - 1) & | |
3012 | ~(sizeof(tcg_target_long) - 1); | |
f44c9960 | 3013 | #endif |
b591dc59 BS |
3014 | if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) > |
3015 | s->frame_end) { | |
5ff9d6a4 | 3016 | tcg_abort(); |
b591dc59 | 3017 | } |
c896fe29 | 3018 | ts->mem_offset = s->current_frame_offset; |
b3a62939 | 3019 | ts->mem_base = s->frame_temp; |
c896fe29 | 3020 | ts->mem_allocated = 1; |
e2c6d1b4 | 3021 | s->current_frame_offset += sizeof(tcg_target_long); |
c896fe29 FB |
3022 | } |
3023 | ||
b722452a | 3024 | static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet); |
b3915dbb | 3025 | |
59d7c14e RH |
3026 | /* Mark a temporary as free or dead. If 'free_or_dead' is negative, |
3027 | mark it free; otherwise mark it dead. */ | |
3028 | static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead) | |
7f6ceedf | 3029 | { |
59d7c14e RH |
3030 | if (ts->fixed_reg) { |
3031 | return; | |
3032 | } | |
3033 | if (ts->val_type == TEMP_VAL_REG) { | |
3034 | s->reg_to_temp[ts->reg] = NULL; | |
3035 | } | |
3036 | ts->val_type = (free_or_dead < 0 | |
3037 | || ts->temp_local | |
fa477d25 | 3038 | || ts->temp_global |
59d7c14e RH |
3039 | ? TEMP_VAL_MEM : TEMP_VAL_DEAD); |
3040 | } | |
7f6ceedf | 3041 | |
59d7c14e RH |
3042 | /* Mark a temporary as dead. */ |
3043 | static inline void temp_dead(TCGContext *s, TCGTemp *ts) | |
3044 | { | |
3045 | temp_free_or_dead(s, ts, 1); | |
3046 | } | |
3047 | ||
3048 | /* Sync a temporary to memory. 'allocated_regs' is used in case a temporary | |
3049 | registers needs to be allocated to store a constant. If 'free_or_dead' | |
3050 | is non-zero, subsequently release the temporary; if it is positive, the | |
3051 | temp is dead; if it is negative, the temp is free. */ | |
98b4e186 RH |
3052 | static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs, |
3053 | TCGRegSet preferred_regs, int free_or_dead) | |
59d7c14e RH |
3054 | { |
3055 | if (ts->fixed_reg) { | |
3056 | return; | |
3057 | } | |
3058 | if (!ts->mem_coherent) { | |
7f6ceedf | 3059 | if (!ts->mem_allocated) { |
2272e4a7 | 3060 | temp_allocate_frame(s, ts); |
59d7c14e | 3061 | } |
59d7c14e RH |
3062 | switch (ts->val_type) { |
3063 | case TEMP_VAL_CONST: | |
3064 | /* If we're going to free the temp immediately, then we won't | |
3065 | require it later in a register, so attempt to store the | |
3066 | constant to memory directly. */ | |
3067 | if (free_or_dead | |
3068 | && tcg_out_sti(s, ts->type, ts->val, | |
3069 | ts->mem_base->reg, ts->mem_offset)) { | |
3070 | break; | |
3071 | } | |
3072 | temp_load(s, ts, tcg_target_available_regs[ts->type], | |
98b4e186 | 3073 | allocated_regs, preferred_regs); |
59d7c14e RH |
3074 | /* fallthrough */ |
3075 | ||
3076 | case TEMP_VAL_REG: | |
3077 | tcg_out_st(s, ts->type, ts->reg, | |
3078 | ts->mem_base->reg, ts->mem_offset); | |
3079 | break; | |
3080 | ||
3081 | case TEMP_VAL_MEM: | |
3082 | break; | |
3083 | ||
3084 | case TEMP_VAL_DEAD: | |
3085 | default: | |
3086 | tcg_abort(); | |
3087 | } | |
3088 | ts->mem_coherent = 1; | |
3089 | } | |
3090 | if (free_or_dead) { | |
3091 | temp_free_or_dead(s, ts, free_or_dead); | |
7f6ceedf | 3092 | } |
7f6ceedf AJ |
3093 | } |
3094 | ||
c896fe29 | 3095 | /* free register 'reg' by spilling the corresponding temporary if necessary */ |
b3915dbb | 3096 | static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs) |
c896fe29 | 3097 | { |
f8b2f202 | 3098 | TCGTemp *ts = s->reg_to_temp[reg]; |
f8b2f202 | 3099 | if (ts != NULL) { |
98b4e186 | 3100 | temp_sync(s, ts, allocated_regs, 0, -1); |
c896fe29 FB |
3101 | } |
3102 | } | |
3103 | ||
b016486e RH |
3104 | /** |
3105 | * tcg_reg_alloc: | |
3106 | * @required_regs: Set of registers in which we must allocate. | |
3107 | * @allocated_regs: Set of registers which must be avoided. | |
3108 | * @preferred_regs: Set of registers we should prefer. | |
3109 | * @rev: True if we search the registers in "indirect" order. | |
3110 | * | |
3111 | * The allocated register must be in @required_regs & ~@allocated_regs, | |
3112 | * but if we can put it in @preferred_regs we may save a move later. | |
3113 | */ | |
3114 | static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs, | |
3115 | TCGRegSet allocated_regs, | |
3116 | TCGRegSet preferred_regs, bool rev) | |
c896fe29 | 3117 | { |
b016486e RH |
3118 | int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order); |
3119 | TCGRegSet reg_ct[2]; | |
91478cef | 3120 | const int *order; |
c896fe29 | 3121 | |
b016486e RH |
3122 | reg_ct[1] = required_regs & ~allocated_regs; |
3123 | tcg_debug_assert(reg_ct[1] != 0); | |
3124 | reg_ct[0] = reg_ct[1] & preferred_regs; | |
3125 | ||
3126 | /* Skip the preferred_regs option if it cannot be satisfied, | |
3127 | or if the preference made no difference. */ | |
3128 | f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1]; | |
3129 | ||
91478cef | 3130 | order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order; |
c896fe29 | 3131 | |
b016486e RH |
3132 | /* Try free registers, preferences first. */ |
3133 | for (j = f; j < 2; j++) { | |
3134 | TCGRegSet set = reg_ct[j]; | |
3135 | ||
3136 | if (tcg_regset_single(set)) { | |
3137 | /* One register in the set. */ | |
3138 | TCGReg reg = tcg_regset_first(set); | |
3139 | if (s->reg_to_temp[reg] == NULL) { | |
3140 | return reg; | |
3141 | } | |
3142 | } else { | |
3143 | for (i = 0; i < n; i++) { | |
3144 | TCGReg reg = order[i]; | |
3145 | if (s->reg_to_temp[reg] == NULL && | |
3146 | tcg_regset_test_reg(set, reg)) { | |
3147 | return reg; | |
3148 | } | |
3149 | } | |
3150 | } | |
c896fe29 FB |
3151 | } |
3152 | ||
b016486e RH |
3153 | /* We must spill something. */ |
3154 | for (j = f; j < 2; j++) { | |
3155 | TCGRegSet set = reg_ct[j]; | |
3156 | ||
3157 | if (tcg_regset_single(set)) { | |
3158 | /* One register in the set. */ | |
3159 | TCGReg reg = tcg_regset_first(set); | |
b3915dbb | 3160 | tcg_reg_free(s, reg, allocated_regs); |
c896fe29 | 3161 | return reg; |
b016486e RH |
3162 | } else { |
3163 | for (i = 0; i < n; i++) { | |
3164 | TCGReg reg = order[i]; | |
3165 | if (tcg_regset_test_reg(set, reg)) { | |
3166 | tcg_reg_free(s, reg, allocated_regs); | |
3167 | return reg; | |
3168 | } | |
3169 | } | |
c896fe29 FB |
3170 | } |
3171 | } | |
3172 | ||
3173 | tcg_abort(); | |
3174 | } | |
3175 | ||
40ae5c62 RH |
3176 | /* Make sure the temporary is in a register. If needed, allocate the register |
3177 | from DESIRED while avoiding ALLOCATED. */ | |
3178 | static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs, | |
b722452a | 3179 | TCGRegSet allocated_regs, TCGRegSet preferred_regs) |
40ae5c62 RH |
3180 | { |
3181 | TCGReg reg; | |
3182 | ||
3183 | switch (ts->val_type) { | |
3184 | case TEMP_VAL_REG: | |
3185 | return; | |
3186 | case TEMP_VAL_CONST: | |
b016486e | 3187 | reg = tcg_reg_alloc(s, desired_regs, allocated_regs, |
b722452a | 3188 | preferred_regs, ts->indirect_base); |
40ae5c62 RH |
3189 | tcg_out_movi(s, ts->type, reg, ts->val); |
3190 | ts->mem_coherent = 0; | |
3191 | break; | |
3192 | case TEMP_VAL_MEM: | |
b016486e | 3193 | reg = tcg_reg_alloc(s, desired_regs, allocated_regs, |
b722452a | 3194 | preferred_regs, ts->indirect_base); |
40ae5c62 RH |
3195 | tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset); |
3196 | ts->mem_coherent = 1; | |
3197 | break; | |
3198 | case TEMP_VAL_DEAD: | |
3199 | default: | |
3200 | tcg_abort(); | |
3201 | } | |
3202 | ts->reg = reg; | |
3203 | ts->val_type = TEMP_VAL_REG; | |
3204 | s->reg_to_temp[reg] = ts; | |
3205 | } | |
3206 | ||
59d7c14e RH |
3207 | /* Save a temporary to memory. 'allocated_regs' is used in case a |
3208 | temporary registers needs to be allocated to store a constant. */ | |
3209 | static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs) | |
1ad80729 | 3210 | { |
5a18407f RH |
3211 | /* The liveness analysis already ensures that globals are back |
3212 | in memory. Keep an tcg_debug_assert for safety. */ | |
3213 | tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || ts->fixed_reg); | |
1ad80729 AJ |
3214 | } |
3215 | ||
9814dd27 | 3216 | /* save globals to their canonical location and assume they can be |
e8996ee0 FB |
3217 | modified be the following code. 'allocated_regs' is used in case a |
3218 | temporary registers needs to be allocated to store a constant. */ | |
3219 | static void save_globals(TCGContext *s, TCGRegSet allocated_regs) | |
c896fe29 | 3220 | { |
ac3b8891 | 3221 | int i, n; |
c896fe29 | 3222 | |
ac3b8891 | 3223 | for (i = 0, n = s->nb_globals; i < n; i++) { |
b13eb728 | 3224 | temp_save(s, &s->temps[i], allocated_regs); |
c896fe29 | 3225 | } |
e5097dc8 FB |
3226 | } |
3227 | ||
3d5c5f87 AJ |
3228 | /* sync globals to their canonical location and assume they can be |
3229 | read by the following code. 'allocated_regs' is used in case a | |
3230 | temporary registers needs to be allocated to store a constant. */ | |
3231 | static void sync_globals(TCGContext *s, TCGRegSet allocated_regs) | |
3232 | { | |
ac3b8891 | 3233 | int i, n; |
3d5c5f87 | 3234 | |
ac3b8891 | 3235 | for (i = 0, n = s->nb_globals; i < n; i++) { |
12b9b11a | 3236 | TCGTemp *ts = &s->temps[i]; |
5a18407f RH |
3237 | tcg_debug_assert(ts->val_type != TEMP_VAL_REG |
3238 | || ts->fixed_reg | |
3239 | || ts->mem_coherent); | |
3d5c5f87 AJ |
3240 | } |
3241 | } | |
3242 | ||
e5097dc8 | 3243 | /* at the end of a basic block, we assume all temporaries are dead and |
e8996ee0 FB |
3244 | all globals are stored at their canonical location. */ |
3245 | static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs) | |
e5097dc8 | 3246 | { |
e5097dc8 FB |
3247 | int i; |
3248 | ||
b13eb728 RH |
3249 | for (i = s->nb_globals; i < s->nb_temps; i++) { |
3250 | TCGTemp *ts = &s->temps[i]; | |
641d5fbe | 3251 | if (ts->temp_local) { |
b13eb728 | 3252 | temp_save(s, ts, allocated_regs); |
641d5fbe | 3253 | } else { |
5a18407f RH |
3254 | /* The liveness analysis already ensures that temps are dead. |
3255 | Keep an tcg_debug_assert for safety. */ | |
3256 | tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD); | |
c896fe29 FB |
3257 | } |
3258 | } | |
e8996ee0 FB |
3259 | |
3260 | save_globals(s, allocated_regs); | |
c896fe29 FB |
3261 | } |
3262 | ||
0fe4fca4 | 3263 | static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots, |
ba87719c RH |
3264 | tcg_target_ulong val, TCGLifeData arg_life, |
3265 | TCGRegSet preferred_regs) | |
e8996ee0 | 3266 | { |
e8996ee0 | 3267 | if (ots->fixed_reg) { |
59d7c14e | 3268 | /* For fixed registers, we do not do any constant propagation. */ |
e8996ee0 | 3269 | tcg_out_movi(s, ots->type, ots->reg, val); |
59d7c14e | 3270 | return; |
e8996ee0 | 3271 | } |
59d7c14e RH |
3272 | |
3273 | /* The movi is not explicitly generated here. */ | |
3274 | if (ots->val_type == TEMP_VAL_REG) { | |
3275 | s->reg_to_temp[ots->reg] = NULL; | |
ec7a869d | 3276 | } |
59d7c14e RH |
3277 | ots->val_type = TEMP_VAL_CONST; |
3278 | ots->val = val; | |
3279 | ots->mem_coherent = 0; | |
3280 | if (NEED_SYNC_ARG(0)) { | |
ba87719c | 3281 | temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0)); |
59d7c14e | 3282 | } else if (IS_DEAD_ARG(0)) { |
f8bf00f1 | 3283 | temp_dead(s, ots); |
4c4e1ab2 | 3284 | } |
e8996ee0 FB |
3285 | } |
3286 | ||
dd186292 | 3287 | static void tcg_reg_alloc_movi(TCGContext *s, const TCGOp *op) |
0fe4fca4 | 3288 | { |
43439139 | 3289 | TCGTemp *ots = arg_temp(op->args[0]); |
dd186292 | 3290 | tcg_target_ulong val = op->args[1]; |
0fe4fca4 | 3291 | |
69e3706d | 3292 | tcg_reg_alloc_do_movi(s, ots, val, op->life, op->output_pref[0]); |
0fe4fca4 PB |
3293 | } |
3294 | ||
dd186292 | 3295 | static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op) |
c896fe29 | 3296 | { |
dd186292 | 3297 | const TCGLifeData arg_life = op->life; |
69e3706d | 3298 | TCGRegSet allocated_regs, preferred_regs; |
c896fe29 | 3299 | TCGTemp *ts, *ots; |
450445d5 | 3300 | TCGType otype, itype; |
c896fe29 | 3301 | |
d21369f5 | 3302 | allocated_regs = s->reserved_regs; |
69e3706d | 3303 | preferred_regs = op->output_pref[0]; |
43439139 RH |
3304 | ots = arg_temp(op->args[0]); |
3305 | ts = arg_temp(op->args[1]); | |
450445d5 RH |
3306 | |
3307 | /* Note that otype != itype for no-op truncation. */ | |
3308 | otype = ots->type; | |
3309 | itype = ts->type; | |
c29c1d7e | 3310 | |
0fe4fca4 PB |
3311 | if (ts->val_type == TEMP_VAL_CONST) { |
3312 | /* propagate constant or generate sti */ | |
3313 | tcg_target_ulong val = ts->val; | |
3314 | if (IS_DEAD_ARG(1)) { | |
3315 | temp_dead(s, ts); | |
3316 | } | |
69e3706d | 3317 | tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs); |
0fe4fca4 PB |
3318 | return; |
3319 | } | |
3320 | ||
3321 | /* If the source value is in memory we're going to be forced | |
3322 | to have it in a register in order to perform the copy. Copy | |
3323 | the SOURCE value into its own register first, that way we | |
3324 | don't have to reload SOURCE the next time it is used. */ | |
3325 | if (ts->val_type == TEMP_VAL_MEM) { | |
69e3706d RH |
3326 | temp_load(s, ts, tcg_target_available_regs[itype], |
3327 | allocated_regs, preferred_regs); | |
c29c1d7e | 3328 | } |
c896fe29 | 3329 | |
0fe4fca4 | 3330 | tcg_debug_assert(ts->val_type == TEMP_VAL_REG); |
c29c1d7e AJ |
3331 | if (IS_DEAD_ARG(0) && !ots->fixed_reg) { |
3332 | /* mov to a non-saved dead register makes no sense (even with | |
3333 | liveness analysis disabled). */ | |
eabb7b91 | 3334 | tcg_debug_assert(NEED_SYNC_ARG(0)); |
c29c1d7e | 3335 | if (!ots->mem_allocated) { |
2272e4a7 | 3336 | temp_allocate_frame(s, ots); |
c29c1d7e | 3337 | } |
b3a62939 | 3338 | tcg_out_st(s, otype, ts->reg, ots->mem_base->reg, ots->mem_offset); |
c29c1d7e | 3339 | if (IS_DEAD_ARG(1)) { |
f8bf00f1 | 3340 | temp_dead(s, ts); |
c29c1d7e | 3341 | } |
f8bf00f1 | 3342 | temp_dead(s, ots); |
c29c1d7e | 3343 | } else { |
866cb6cb | 3344 | if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) { |
c896fe29 | 3345 | /* the mov can be suppressed */ |
c29c1d7e | 3346 | if (ots->val_type == TEMP_VAL_REG) { |
f8b2f202 | 3347 | s->reg_to_temp[ots->reg] = NULL; |
c29c1d7e AJ |
3348 | } |
3349 | ots->reg = ts->reg; | |
f8bf00f1 | 3350 | temp_dead(s, ts); |
c896fe29 | 3351 | } else { |
c29c1d7e AJ |
3352 | if (ots->val_type != TEMP_VAL_REG) { |
3353 | /* When allocating a new register, make sure to not spill the | |
3354 | input one. */ | |
3355 | tcg_regset_set_reg(allocated_regs, ts->reg); | |
450445d5 | 3356 | ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype], |
69e3706d | 3357 | allocated_regs, preferred_regs, |
b016486e | 3358 | ots->indirect_base); |
c896fe29 | 3359 | } |
450445d5 | 3360 | tcg_out_mov(s, otype, ots->reg, ts->reg); |
c896fe29 | 3361 | } |
c29c1d7e AJ |
3362 | ots->val_type = TEMP_VAL_REG; |
3363 | ots->mem_coherent = 0; | |
f8b2f202 | 3364 | s->reg_to_temp[ots->reg] = ots; |
c29c1d7e | 3365 | if (NEED_SYNC_ARG(0)) { |
98b4e186 | 3366 | temp_sync(s, ots, allocated_regs, 0, 0); |
c896fe29 | 3367 | } |
ec7a869d | 3368 | } |
c896fe29 FB |
3369 | } |
3370 | ||
dd186292 | 3371 | static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op) |
c896fe29 | 3372 | { |
dd186292 RH |
3373 | const TCGLifeData arg_life = op->life; |
3374 | const TCGOpDef * const def = &tcg_op_defs[op->opc]; | |
82790a87 RH |
3375 | TCGRegSet i_allocated_regs; |
3376 | TCGRegSet o_allocated_regs; | |
b6638662 RH |
3377 | int i, k, nb_iargs, nb_oargs; |
3378 | TCGReg reg; | |
c896fe29 FB |
3379 | TCGArg arg; |
3380 | const TCGArgConstraint *arg_ct; | |
3381 | TCGTemp *ts; | |
3382 | TCGArg new_args[TCG_MAX_OP_ARGS]; | |
3383 | int const_args[TCG_MAX_OP_ARGS]; | |
3384 | ||
3385 | nb_oargs = def->nb_oargs; | |
3386 | nb_iargs = def->nb_iargs; | |
3387 | ||
3388 | /* copy constants */ | |
3389 | memcpy(new_args + nb_oargs + nb_iargs, | |
dd186292 | 3390 | op->args + nb_oargs + nb_iargs, |
c896fe29 FB |
3391 | sizeof(TCGArg) * def->nb_cargs); |
3392 | ||
d21369f5 RH |
3393 | i_allocated_regs = s->reserved_regs; |
3394 | o_allocated_regs = s->reserved_regs; | |
82790a87 | 3395 | |
c896fe29 | 3396 | /* satisfy input constraints */ |
dd186292 | 3397 | for (k = 0; k < nb_iargs; k++) { |
d62816f2 RH |
3398 | TCGRegSet i_preferred_regs, o_preferred_regs; |
3399 | ||
c896fe29 | 3400 | i = def->sorted_args[nb_oargs + k]; |
dd186292 | 3401 | arg = op->args[i]; |
c896fe29 | 3402 | arg_ct = &def->args_ct[i]; |
43439139 | 3403 | ts = arg_temp(arg); |
40ae5c62 RH |
3404 | |
3405 | if (ts->val_type == TEMP_VAL_CONST | |
3406 | && tcg_target_const_match(ts->val, ts->type, arg_ct)) { | |
3407 | /* constant is OK for instruction */ | |
3408 | const_args[i] = 1; | |
3409 | new_args[i] = ts->val; | |
d62816f2 | 3410 | continue; |
c896fe29 | 3411 | } |
40ae5c62 | 3412 | |
d62816f2 | 3413 | i_preferred_regs = o_preferred_regs = 0; |
5ff9d6a4 | 3414 | if (arg_ct->ct & TCG_CT_IALIAS) { |
d62816f2 | 3415 | o_preferred_regs = op->output_pref[arg_ct->alias_index]; |
5ff9d6a4 FB |
3416 | if (ts->fixed_reg) { |
3417 | /* if fixed register, we must allocate a new register | |
3418 | if the alias is not the same register */ | |
d62816f2 | 3419 | if (arg != op->args[arg_ct->alias_index]) { |
5ff9d6a4 | 3420 | goto allocate_in_reg; |
d62816f2 | 3421 | } |
5ff9d6a4 FB |
3422 | } else { |
3423 | /* if the input is aliased to an output and if it is | |
3424 | not dead after the instruction, we must allocate | |
3425 | a new register and move it */ | |
866cb6cb | 3426 | if (!IS_DEAD_ARG(i)) { |
5ff9d6a4 | 3427 | goto allocate_in_reg; |
866cb6cb | 3428 | } |
d62816f2 | 3429 | |
7e1df267 AJ |
3430 | /* check if the current register has already been allocated |
3431 | for another input aliased to an output */ | |
d62816f2 RH |
3432 | if (ts->val_type == TEMP_VAL_REG) { |
3433 | int k2, i2; | |
3434 | reg = ts->reg; | |
3435 | for (k2 = 0 ; k2 < k ; k2++) { | |
3436 | i2 = def->sorted_args[nb_oargs + k2]; | |
3437 | if ((def->args_ct[i2].ct & TCG_CT_IALIAS) && | |
3438 | reg == new_args[i2]) { | |
3439 | goto allocate_in_reg; | |
3440 | } | |
7e1df267 AJ |
3441 | } |
3442 | } | |
d62816f2 | 3443 | i_preferred_regs = o_preferred_regs; |
5ff9d6a4 | 3444 | } |
c896fe29 | 3445 | } |
d62816f2 RH |
3446 | |
3447 | temp_load(s, ts, arg_ct->u.regs, i_allocated_regs, i_preferred_regs); | |
c896fe29 | 3448 | reg = ts->reg; |
d62816f2 | 3449 | |
c896fe29 FB |
3450 | if (tcg_regset_test_reg(arg_ct->u.regs, reg)) { |
3451 | /* nothing to do : the constraint is satisfied */ | |
3452 | } else { | |
3453 | allocate_in_reg: | |
3454 | /* allocate a new register matching the constraint | |
3455 | and move the temporary register into it */ | |
d62816f2 RH |
3456 | temp_load(s, ts, tcg_target_available_regs[ts->type], |
3457 | i_allocated_regs, 0); | |
82790a87 | 3458 | reg = tcg_reg_alloc(s, arg_ct->u.regs, i_allocated_regs, |
d62816f2 | 3459 | o_preferred_regs, ts->indirect_base); |
3b6dac34 | 3460 | tcg_out_mov(s, ts->type, reg, ts->reg); |
c896fe29 | 3461 | } |
c896fe29 FB |
3462 | new_args[i] = reg; |
3463 | const_args[i] = 0; | |
82790a87 | 3464 | tcg_regset_set_reg(i_allocated_regs, reg); |
c896fe29 FB |
3465 | } |
3466 | ||
a52ad07e AJ |
3467 | /* mark dead temporaries and free the associated registers */ |
3468 | for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { | |
3469 | if (IS_DEAD_ARG(i)) { | |
43439139 | 3470 | temp_dead(s, arg_temp(op->args[i])); |
a52ad07e AJ |
3471 | } |
3472 | } | |
3473 | ||
e8996ee0 | 3474 | if (def->flags & TCG_OPF_BB_END) { |
82790a87 | 3475 | tcg_reg_alloc_bb_end(s, i_allocated_regs); |
e8996ee0 | 3476 | } else { |
e8996ee0 FB |
3477 | if (def->flags & TCG_OPF_CALL_CLOBBER) { |
3478 | /* XXX: permit generic clobber register list ? */ | |
c8074023 RH |
3479 | for (i = 0; i < TCG_TARGET_NB_REGS; i++) { |
3480 | if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) { | |
82790a87 | 3481 | tcg_reg_free(s, i, i_allocated_regs); |
e8996ee0 | 3482 | } |
c896fe29 | 3483 | } |
3d5c5f87 AJ |
3484 | } |
3485 | if (def->flags & TCG_OPF_SIDE_EFFECTS) { | |
3486 | /* sync globals if the op has side effects and might trigger | |
3487 | an exception. */ | |
82790a87 | 3488 | sync_globals(s, i_allocated_regs); |
c896fe29 | 3489 | } |
e8996ee0 FB |
3490 | |
3491 | /* satisfy the output constraints */ | |
e8996ee0 FB |
3492 | for(k = 0; k < nb_oargs; k++) { |
3493 | i = def->sorted_args[k]; | |
dd186292 | 3494 | arg = op->args[i]; |
e8996ee0 | 3495 | arg_ct = &def->args_ct[i]; |
43439139 | 3496 | ts = arg_temp(arg); |
17280ff4 RH |
3497 | if ((arg_ct->ct & TCG_CT_ALIAS) |
3498 | && !const_args[arg_ct->alias_index]) { | |
e8996ee0 | 3499 | reg = new_args[arg_ct->alias_index]; |
82790a87 RH |
3500 | } else if (arg_ct->ct & TCG_CT_NEWREG) { |
3501 | reg = tcg_reg_alloc(s, arg_ct->u.regs, | |
3502 | i_allocated_regs | o_allocated_regs, | |
69e3706d | 3503 | op->output_pref[k], ts->indirect_base); |
e8996ee0 FB |
3504 | } else { |
3505 | /* if fixed register, we try to use it */ | |
3506 | reg = ts->reg; | |
3507 | if (ts->fixed_reg && | |
3508 | tcg_regset_test_reg(arg_ct->u.regs, reg)) { | |
3509 | goto oarg_end; | |
3510 | } | |
82790a87 | 3511 | reg = tcg_reg_alloc(s, arg_ct->u.regs, o_allocated_regs, |
69e3706d | 3512 | op->output_pref[k], ts->indirect_base); |
c896fe29 | 3513 | } |
82790a87 | 3514 | tcg_regset_set_reg(o_allocated_regs, reg); |
e8996ee0 FB |
3515 | /* if a fixed register is used, then a move will be done afterwards */ |
3516 | if (!ts->fixed_reg) { | |
ec7a869d | 3517 | if (ts->val_type == TEMP_VAL_REG) { |
f8b2f202 | 3518 | s->reg_to_temp[ts->reg] = NULL; |
ec7a869d AJ |
3519 | } |
3520 | ts->val_type = TEMP_VAL_REG; | |
3521 | ts->reg = reg; | |
3522 | /* temp value is modified, so the value kept in memory is | |
3523 | potentially not the same */ | |
3524 | ts->mem_coherent = 0; | |
f8b2f202 | 3525 | s->reg_to_temp[reg] = ts; |
e8996ee0 FB |
3526 | } |
3527 | oarg_end: | |
3528 | new_args[i] = reg; | |
c896fe29 | 3529 | } |
c896fe29 FB |
3530 | } |
3531 | ||
c896fe29 | 3532 | /* emit instruction */ |
d2fd745f RH |
3533 | if (def->flags & TCG_OPF_VECTOR) { |
3534 | tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op), | |
3535 | new_args, const_args); | |
3536 | } else { | |
3537 | tcg_out_op(s, op->opc, new_args, const_args); | |
3538 | } | |
3539 | ||
c896fe29 FB |
3540 | /* move the outputs in the correct register if needed */ |
3541 | for(i = 0; i < nb_oargs; i++) { | |
43439139 | 3542 | ts = arg_temp(op->args[i]); |
c896fe29 FB |
3543 | reg = new_args[i]; |
3544 | if (ts->fixed_reg && ts->reg != reg) { | |
3b6dac34 | 3545 | tcg_out_mov(s, ts->type, ts->reg, reg); |
c896fe29 | 3546 | } |
ec7a869d | 3547 | if (NEED_SYNC_ARG(i)) { |
98b4e186 | 3548 | temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i)); |
59d7c14e | 3549 | } else if (IS_DEAD_ARG(i)) { |
f8bf00f1 | 3550 | temp_dead(s, ts); |
ec7a869d | 3551 | } |
c896fe29 FB |
3552 | } |
3553 | } | |
3554 | ||
b03cce8e FB |
3555 | #ifdef TCG_TARGET_STACK_GROWSUP |
3556 | #define STACK_DIR(x) (-(x)) | |
3557 | #else | |
3558 | #define STACK_DIR(x) (x) | |
3559 | #endif | |
3560 | ||
dd186292 | 3561 | static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op) |
c896fe29 | 3562 | { |
cd9090aa RH |
3563 | const int nb_oargs = TCGOP_CALLO(op); |
3564 | const int nb_iargs = TCGOP_CALLI(op); | |
dd186292 | 3565 | const TCGLifeData arg_life = op->life; |
b6638662 RH |
3566 | int flags, nb_regs, i; |
3567 | TCGReg reg; | |
cf066674 | 3568 | TCGArg arg; |
c896fe29 | 3569 | TCGTemp *ts; |
d3452f1f RH |
3570 | intptr_t stack_offset; |
3571 | size_t call_stack_size; | |
cf066674 RH |
3572 | tcg_insn_unit *func_addr; |
3573 | int allocate_args; | |
c896fe29 | 3574 | TCGRegSet allocated_regs; |
c896fe29 | 3575 | |
dd186292 RH |
3576 | func_addr = (tcg_insn_unit *)(intptr_t)op->args[nb_oargs + nb_iargs]; |
3577 | flags = op->args[nb_oargs + nb_iargs + 1]; | |
c896fe29 | 3578 | |
6e17d0c5 | 3579 | nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs); |
c45cb8bb RH |
3580 | if (nb_regs > nb_iargs) { |
3581 | nb_regs = nb_iargs; | |
cf066674 | 3582 | } |
c896fe29 FB |
3583 | |
3584 | /* assign stack slots first */ | |
c45cb8bb | 3585 | call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long); |
c896fe29 FB |
3586 | call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) & |
3587 | ~(TCG_TARGET_STACK_ALIGN - 1); | |
b03cce8e FB |
3588 | allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE); |
3589 | if (allocate_args) { | |
345649c0 BS |
3590 | /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed, |
3591 | preallocate call stack */ | |
3592 | tcg_abort(); | |
b03cce8e | 3593 | } |
39cf05d3 FB |
3594 | |
3595 | stack_offset = TCG_TARGET_CALL_STACK_OFFSET; | |
dd186292 RH |
3596 | for (i = nb_regs; i < nb_iargs; i++) { |
3597 | arg = op->args[nb_oargs + i]; | |
39cf05d3 FB |
3598 | #ifdef TCG_TARGET_STACK_GROWSUP |
3599 | stack_offset -= sizeof(tcg_target_long); | |
3600 | #endif | |
3601 | if (arg != TCG_CALL_DUMMY_ARG) { | |
43439139 | 3602 | ts = arg_temp(arg); |
40ae5c62 | 3603 | temp_load(s, ts, tcg_target_available_regs[ts->type], |
b722452a | 3604 | s->reserved_regs, 0); |
40ae5c62 | 3605 | tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset); |
c896fe29 | 3606 | } |
39cf05d3 FB |
3607 | #ifndef TCG_TARGET_STACK_GROWSUP |
3608 | stack_offset += sizeof(tcg_target_long); | |
3609 | #endif | |
c896fe29 FB |
3610 | } |
3611 | ||
3612 | /* assign input registers */ | |
d21369f5 | 3613 | allocated_regs = s->reserved_regs; |
dd186292 RH |
3614 | for (i = 0; i < nb_regs; i++) { |
3615 | arg = op->args[nb_oargs + i]; | |
39cf05d3 | 3616 | if (arg != TCG_CALL_DUMMY_ARG) { |
43439139 | 3617 | ts = arg_temp(arg); |
39cf05d3 | 3618 | reg = tcg_target_call_iarg_regs[i]; |
40ae5c62 | 3619 | |
39cf05d3 FB |
3620 | if (ts->val_type == TEMP_VAL_REG) { |
3621 | if (ts->reg != reg) { | |
4250da10 | 3622 | tcg_reg_free(s, reg, allocated_regs); |
3b6dac34 | 3623 | tcg_out_mov(s, ts->type, reg, ts->reg); |
39cf05d3 | 3624 | } |
39cf05d3 | 3625 | } else { |
ccb1bb66 | 3626 | TCGRegSet arg_set = 0; |
40ae5c62 | 3627 | |
4250da10 | 3628 | tcg_reg_free(s, reg, allocated_regs); |
40ae5c62 | 3629 | tcg_regset_set_reg(arg_set, reg); |
b722452a | 3630 | temp_load(s, ts, arg_set, allocated_regs, 0); |
c896fe29 | 3631 | } |
40ae5c62 | 3632 | |
39cf05d3 | 3633 | tcg_regset_set_reg(allocated_regs, reg); |
c896fe29 | 3634 | } |
c896fe29 FB |
3635 | } |
3636 | ||
c896fe29 | 3637 | /* mark dead temporaries and free the associated registers */ |
dd186292 | 3638 | for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { |
866cb6cb | 3639 | if (IS_DEAD_ARG(i)) { |
43439139 | 3640 | temp_dead(s, arg_temp(op->args[i])); |
c896fe29 FB |
3641 | } |
3642 | } | |
3643 | ||
3644 | /* clobber call registers */ | |
c8074023 RH |
3645 | for (i = 0; i < TCG_TARGET_NB_REGS; i++) { |
3646 | if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) { | |
b3915dbb | 3647 | tcg_reg_free(s, i, allocated_regs); |
c896fe29 FB |
3648 | } |
3649 | } | |
78505279 AJ |
3650 | |
3651 | /* Save globals if they might be written by the helper, sync them if | |
3652 | they might be read. */ | |
3653 | if (flags & TCG_CALL_NO_READ_GLOBALS) { | |
3654 | /* Nothing to do */ | |
3655 | } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) { | |
3656 | sync_globals(s, allocated_regs); | |
3657 | } else { | |
b9c18f56 AJ |
3658 | save_globals(s, allocated_regs); |
3659 | } | |
c896fe29 | 3660 | |
cf066674 | 3661 | tcg_out_call(s, func_addr); |
c896fe29 FB |
3662 | |
3663 | /* assign output registers and emit moves if needed */ | |
3664 | for(i = 0; i < nb_oargs; i++) { | |
dd186292 | 3665 | arg = op->args[i]; |
43439139 | 3666 | ts = arg_temp(arg); |
c896fe29 | 3667 | reg = tcg_target_call_oarg_regs[i]; |
eabb7b91 | 3668 | tcg_debug_assert(s->reg_to_temp[reg] == NULL); |
34b1a49c | 3669 | |
c896fe29 FB |
3670 | if (ts->fixed_reg) { |
3671 | if (ts->reg != reg) { | |
3b6dac34 | 3672 | tcg_out_mov(s, ts->type, ts->reg, reg); |
c896fe29 FB |
3673 | } |
3674 | } else { | |
ec7a869d | 3675 | if (ts->val_type == TEMP_VAL_REG) { |
f8b2f202 | 3676 | s->reg_to_temp[ts->reg] = NULL; |
ec7a869d AJ |
3677 | } |
3678 | ts->val_type = TEMP_VAL_REG; | |
3679 | ts->reg = reg; | |
3680 | ts->mem_coherent = 0; | |
f8b2f202 | 3681 | s->reg_to_temp[reg] = ts; |
ec7a869d | 3682 | if (NEED_SYNC_ARG(i)) { |
98b4e186 | 3683 | temp_sync(s, ts, allocated_regs, 0, IS_DEAD_ARG(i)); |
59d7c14e | 3684 | } else if (IS_DEAD_ARG(i)) { |
f8bf00f1 | 3685 | temp_dead(s, ts); |
8c11ad25 | 3686 | } |
c896fe29 FB |
3687 | } |
3688 | } | |
c896fe29 FB |
3689 | } |
3690 | ||
3691 | #ifdef CONFIG_PROFILER | |
3692 | ||
c3fac113 EC |
3693 | /* avoid copy/paste errors */ |
3694 | #define PROF_ADD(to, from, field) \ | |
3695 | do { \ | |
3696 | (to)->field += atomic_read(&((from)->field)); \ | |
3697 | } while (0) | |
3698 | ||
3699 | #define PROF_MAX(to, from, field) \ | |
3700 | do { \ | |
3701 | typeof((from)->field) val__ = atomic_read(&((from)->field)); \ | |
3702 | if (val__ > (to)->field) { \ | |
3703 | (to)->field = val__; \ | |
3704 | } \ | |
3705 | } while (0) | |
3706 | ||
3707 | /* Pass in a zero'ed @prof */ | |
3708 | static inline | |
3709 | void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table) | |
3710 | { | |
3468b59e | 3711 | unsigned int n_ctxs = atomic_read(&n_tcg_ctxs); |
c3fac113 EC |
3712 | unsigned int i; |
3713 | ||
3468b59e EC |
3714 | for (i = 0; i < n_ctxs; i++) { |
3715 | TCGContext *s = atomic_read(&tcg_ctxs[i]); | |
3716 | const TCGProfile *orig = &s->prof; | |
c3fac113 EC |
3717 | |
3718 | if (counters) { | |
72fd2efb | 3719 | PROF_ADD(prof, orig, cpu_exec_time); |
c3fac113 EC |
3720 | PROF_ADD(prof, orig, tb_count1); |
3721 | PROF_ADD(prof, orig, tb_count); | |
3722 | PROF_ADD(prof, orig, op_count); | |
3723 | PROF_MAX(prof, orig, op_count_max); | |
3724 | PROF_ADD(prof, orig, temp_count); | |
3725 | PROF_MAX(prof, orig, temp_count_max); | |
3726 | PROF_ADD(prof, orig, del_op_count); | |
3727 | PROF_ADD(prof, orig, code_in_len); | |
3728 | PROF_ADD(prof, orig, code_out_len); | |
3729 | PROF_ADD(prof, orig, search_out_len); | |
3730 | PROF_ADD(prof, orig, interm_time); | |
3731 | PROF_ADD(prof, orig, code_time); | |
3732 | PROF_ADD(prof, orig, la_time); | |
3733 | PROF_ADD(prof, orig, opt_time); | |
3734 | PROF_ADD(prof, orig, restore_count); | |
3735 | PROF_ADD(prof, orig, restore_time); | |
3736 | } | |
3737 | if (table) { | |
3738 | int i; | |
3739 | ||
3740 | for (i = 0; i < NB_OPS; i++) { | |
3741 | PROF_ADD(prof, orig, table_op_count[i]); | |
3742 | } | |
3743 | } | |
3744 | } | |
3745 | } | |
3746 | ||
3747 | #undef PROF_ADD | |
3748 | #undef PROF_MAX | |
3749 | ||
3750 | static void tcg_profile_snapshot_counters(TCGProfile *prof) | |
3751 | { | |
3752 | tcg_profile_snapshot(prof, true, false); | |
3753 | } | |
3754 | ||
3755 | static void tcg_profile_snapshot_table(TCGProfile *prof) | |
3756 | { | |
3757 | tcg_profile_snapshot(prof, false, true); | |
3758 | } | |
c896fe29 | 3759 | |
246ae24d | 3760 | void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf) |
c896fe29 | 3761 | { |
c3fac113 | 3762 | TCGProfile prof = {}; |
c896fe29 | 3763 | int i; |
d70724ce | 3764 | |
c3fac113 | 3765 | tcg_profile_snapshot_table(&prof); |
15fc7daa | 3766 | for (i = 0; i < NB_OPS; i++) { |
246ae24d | 3767 | cpu_fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, |
c3fac113 | 3768 | prof.table_op_count[i]); |
c896fe29 | 3769 | } |
c896fe29 | 3770 | } |
72fd2efb EC |
3771 | |
3772 | int64_t tcg_cpu_exec_time(void) | |
3773 | { | |
3774 | unsigned int n_ctxs = atomic_read(&n_tcg_ctxs); | |
3775 | unsigned int i; | |
3776 | int64_t ret = 0; | |
3777 | ||
3778 | for (i = 0; i < n_ctxs; i++) { | |
3779 | const TCGContext *s = atomic_read(&tcg_ctxs[i]); | |
3780 | const TCGProfile *prof = &s->prof; | |
3781 | ||
3782 | ret += atomic_read(&prof->cpu_exec_time); | |
3783 | } | |
3784 | return ret; | |
3785 | } | |
246ae24d MF |
3786 | #else |
3787 | void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf) | |
3788 | { | |
3789 | cpu_fprintf(f, "[TCG profiler not compiled]\n"); | |
3790 | } | |
72fd2efb EC |
3791 | |
3792 | int64_t tcg_cpu_exec_time(void) | |
3793 | { | |
3794 | error_report("%s: TCG profiler not compiled", __func__); | |
3795 | exit(EXIT_FAILURE); | |
3796 | } | |
c896fe29 FB |
3797 | #endif |
3798 | ||
3799 | ||
5bd2ec3d | 3800 | int tcg_gen_code(TCGContext *s, TranslationBlock *tb) |
c896fe29 | 3801 | { |
c3fac113 EC |
3802 | #ifdef CONFIG_PROFILER |
3803 | TCGProfile *prof = &s->prof; | |
3804 | #endif | |
15fa08f8 RH |
3805 | int i, num_insns; |
3806 | TCGOp *op; | |
c896fe29 | 3807 | |
04fe6400 RH |
3808 | #ifdef CONFIG_PROFILER |
3809 | { | |
c1f543b7 | 3810 | int n = 0; |
04fe6400 | 3811 | |
15fa08f8 RH |
3812 | QTAILQ_FOREACH(op, &s->ops, link) { |
3813 | n++; | |
3814 | } | |
c3fac113 EC |
3815 | atomic_set(&prof->op_count, prof->op_count + n); |
3816 | if (n > prof->op_count_max) { | |
3817 | atomic_set(&prof->op_count_max, n); | |
04fe6400 RH |
3818 | } |
3819 | ||
3820 | n = s->nb_temps; | |
c3fac113 EC |
3821 | atomic_set(&prof->temp_count, prof->temp_count + n); |
3822 | if (n > prof->temp_count_max) { | |
3823 | atomic_set(&prof->temp_count_max, n); | |
04fe6400 RH |
3824 | } |
3825 | } | |
3826 | #endif | |
3827 | ||
c896fe29 | 3828 | #ifdef DEBUG_DISAS |
d977e1c2 AB |
3829 | if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP) |
3830 | && qemu_log_in_addr_range(tb->pc))) { | |
1ee73216 | 3831 | qemu_log_lock(); |
93fcfe39 | 3832 | qemu_log("OP:\n"); |
1894f69a | 3833 | tcg_dump_ops(s, false); |
93fcfe39 | 3834 | qemu_log("\n"); |
1ee73216 | 3835 | qemu_log_unlock(); |
c896fe29 FB |
3836 | } |
3837 | #endif | |
3838 | ||
c5cc28ff | 3839 | #ifdef CONFIG_PROFILER |
c3fac113 | 3840 | atomic_set(&prof->opt_time, prof->opt_time - profile_getclock()); |
c5cc28ff AJ |
3841 | #endif |
3842 | ||
8f2e8c07 | 3843 | #ifdef USE_TCG_OPTIMIZATIONS |
c45cb8bb | 3844 | tcg_optimize(s); |
8f2e8c07 KB |
3845 | #endif |
3846 | ||
a23a9ec6 | 3847 | #ifdef CONFIG_PROFILER |
c3fac113 EC |
3848 | atomic_set(&prof->opt_time, prof->opt_time + profile_getclock()); |
3849 | atomic_set(&prof->la_time, prof->la_time - profile_getclock()); | |
a23a9ec6 | 3850 | #endif |
c5cc28ff | 3851 | |
b4fc67c7 | 3852 | reachable_code_pass(s); |
b83eabea | 3853 | liveness_pass_1(s); |
5a18407f | 3854 | |
b83eabea | 3855 | if (s->nb_indirects > 0) { |
5a18407f | 3856 | #ifdef DEBUG_DISAS |
b83eabea RH |
3857 | if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND) |
3858 | && qemu_log_in_addr_range(tb->pc))) { | |
3859 | qemu_log_lock(); | |
3860 | qemu_log("OP before indirect lowering:\n"); | |
1894f69a | 3861 | tcg_dump_ops(s, false); |
b83eabea RH |
3862 | qemu_log("\n"); |
3863 | qemu_log_unlock(); | |
3864 | } | |
5a18407f | 3865 | #endif |
b83eabea RH |
3866 | /* Replace indirect temps with direct temps. */ |
3867 | if (liveness_pass_2(s)) { | |
3868 | /* If changes were made, re-run liveness. */ | |
3869 | liveness_pass_1(s); | |
5a18407f RH |
3870 | } |
3871 | } | |
c5cc28ff | 3872 | |
a23a9ec6 | 3873 | #ifdef CONFIG_PROFILER |
c3fac113 | 3874 | atomic_set(&prof->la_time, prof->la_time + profile_getclock()); |
a23a9ec6 | 3875 | #endif |
c896fe29 FB |
3876 | |
3877 | #ifdef DEBUG_DISAS | |
d977e1c2 AB |
3878 | if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT) |
3879 | && qemu_log_in_addr_range(tb->pc))) { | |
1ee73216 | 3880 | qemu_log_lock(); |
c5cc28ff | 3881 | qemu_log("OP after optimization and liveness analysis:\n"); |
1894f69a | 3882 | tcg_dump_ops(s, true); |
93fcfe39 | 3883 | qemu_log("\n"); |
1ee73216 | 3884 | qemu_log_unlock(); |
c896fe29 FB |
3885 | } |
3886 | #endif | |
3887 | ||
3888 | tcg_reg_alloc_start(s); | |
3889 | ||
e7e168f4 EC |
3890 | s->code_buf = tb->tc.ptr; |
3891 | s->code_ptr = tb->tc.ptr; | |
c896fe29 | 3892 | |
659ef5cb | 3893 | #ifdef TCG_TARGET_NEED_LDST_LABELS |
6001f772 | 3894 | QSIMPLEQ_INIT(&s->ldst_labels); |
659ef5cb | 3895 | #endif |
57a26946 RH |
3896 | #ifdef TCG_TARGET_NEED_POOL_LABELS |
3897 | s->pool_labels = NULL; | |
3898 | #endif | |
9ecefc84 | 3899 | |
fca8a500 | 3900 | num_insns = -1; |
15fa08f8 | 3901 | QTAILQ_FOREACH(op, &s->ops, link) { |
c45cb8bb | 3902 | TCGOpcode opc = op->opc; |
b3db8758 | 3903 | |
c896fe29 | 3904 | #ifdef CONFIG_PROFILER |
c3fac113 | 3905 | atomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1); |
c896fe29 | 3906 | #endif |
c45cb8bb RH |
3907 | |
3908 | switch (opc) { | |
c896fe29 | 3909 | case INDEX_op_mov_i32: |
c896fe29 | 3910 | case INDEX_op_mov_i64: |
d2fd745f | 3911 | case INDEX_op_mov_vec: |
dd186292 | 3912 | tcg_reg_alloc_mov(s, op); |
c896fe29 | 3913 | break; |
e8996ee0 | 3914 | case INDEX_op_movi_i32: |
e8996ee0 | 3915 | case INDEX_op_movi_i64: |
d2fd745f | 3916 | case INDEX_op_dupi_vec: |
dd186292 | 3917 | tcg_reg_alloc_movi(s, op); |
e8996ee0 | 3918 | break; |
765b842a | 3919 | case INDEX_op_insn_start: |
fca8a500 | 3920 | if (num_insns >= 0) { |
9f754620 RH |
3921 | size_t off = tcg_current_code_size(s); |
3922 | s->gen_insn_end_off[num_insns] = off; | |
3923 | /* Assert that we do not overflow our stored offset. */ | |
3924 | assert(s->gen_insn_end_off[num_insns] == off); | |
fca8a500 RH |
3925 | } |
3926 | num_insns++; | |
bad729e2 RH |
3927 | for (i = 0; i < TARGET_INSN_START_WORDS; ++i) { |
3928 | target_ulong a; | |
3929 | #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS | |
efee3746 | 3930 | a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]); |
bad729e2 | 3931 | #else |
efee3746 | 3932 | a = op->args[i]; |
bad729e2 | 3933 | #endif |
fca8a500 | 3934 | s->gen_insn_data[num_insns][i] = a; |
bad729e2 | 3935 | } |
c896fe29 | 3936 | break; |
5ff9d6a4 | 3937 | case INDEX_op_discard: |
43439139 | 3938 | temp_dead(s, arg_temp(op->args[0])); |
5ff9d6a4 | 3939 | break; |
c896fe29 | 3940 | case INDEX_op_set_label: |
e8996ee0 | 3941 | tcg_reg_alloc_bb_end(s, s->reserved_regs); |
efee3746 | 3942 | tcg_out_label(s, arg_label(op->args[0]), s->code_ptr); |
c896fe29 FB |
3943 | break; |
3944 | case INDEX_op_call: | |
dd186292 | 3945 | tcg_reg_alloc_call(s, op); |
c45cb8bb | 3946 | break; |
c896fe29 | 3947 | default: |
25c4d9cc | 3948 | /* Sanity check that we've not introduced any unhandled opcodes. */ |
be0f34b5 | 3949 | tcg_debug_assert(tcg_op_supported(opc)); |
c896fe29 FB |
3950 | /* Note: in order to speed up the code, it would be much |
3951 | faster to have specialized register allocator functions for | |
3952 | some common argument patterns */ | |
dd186292 | 3953 | tcg_reg_alloc_op(s, op); |
c896fe29 FB |
3954 | break; |
3955 | } | |
8d8fdbae | 3956 | #ifdef CONFIG_DEBUG_TCG |
c896fe29 FB |
3957 | check_regs(s); |
3958 | #endif | |
b125f9dc RH |
3959 | /* Test for (pending) buffer overflow. The assumption is that any |
3960 | one operation beginning below the high water mark cannot overrun | |
3961 | the buffer completely. Thus we can test for overflow after | |
3962 | generating code without having to check during generation. */ | |
644da9b3 | 3963 | if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) { |
b125f9dc RH |
3964 | return -1; |
3965 | } | |
c896fe29 | 3966 | } |
fca8a500 RH |
3967 | tcg_debug_assert(num_insns >= 0); |
3968 | s->gen_insn_end_off[num_insns] = tcg_current_code_size(s); | |
c45cb8bb | 3969 | |
b76f0d8c | 3970 | /* Generate TB finalization at the end of block */ |
659ef5cb RH |
3971 | #ifdef TCG_TARGET_NEED_LDST_LABELS |
3972 | if (!tcg_out_ldst_finalize(s)) { | |
23dceda6 RH |
3973 | return -1; |
3974 | } | |
659ef5cb | 3975 | #endif |
57a26946 RH |
3976 | #ifdef TCG_TARGET_NEED_POOL_LABELS |
3977 | if (!tcg_out_pool_finalize(s)) { | |
3978 | return -1; | |
3979 | } | |
3980 | #endif | |
c896fe29 FB |
3981 | |
3982 | /* flush instruction cache */ | |
1813e175 | 3983 | flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr); |
2aeabc08 | 3984 | |
1813e175 | 3985 | return tcg_current_code_size(s); |
c896fe29 FB |
3986 | } |
3987 | ||
a23a9ec6 | 3988 | #ifdef CONFIG_PROFILER |
405cf9ff | 3989 | void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf) |
a23a9ec6 | 3990 | { |
c3fac113 EC |
3991 | TCGProfile prof = {}; |
3992 | const TCGProfile *s; | |
3993 | int64_t tb_count; | |
3994 | int64_t tb_div_count; | |
3995 | int64_t tot; | |
3996 | ||
3997 | tcg_profile_snapshot_counters(&prof); | |
3998 | s = &prof; | |
3999 | tb_count = s->tb_count; | |
4000 | tb_div_count = tb_count ? tb_count : 1; | |
4001 | tot = s->interm_time + s->code_time; | |
a23a9ec6 | 4002 | |
a23a9ec6 FB |
4003 | cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n", |
4004 | tot, tot / 2.4e9); | |
4005 | cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n", | |
fca8a500 RH |
4006 | tb_count, s->tb_count1 - tb_count, |
4007 | (double)(s->tb_count1 - s->tb_count) | |
4008 | / (s->tb_count1 ? s->tb_count1 : 1) * 100.0); | |
a23a9ec6 | 4009 | cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n", |
fca8a500 | 4010 | (double)s->op_count / tb_div_count, s->op_count_max); |
a23a9ec6 | 4011 | cpu_fprintf(f, "deleted ops/TB %0.2f\n", |
fca8a500 | 4012 | (double)s->del_op_count / tb_div_count); |
a23a9ec6 | 4013 | cpu_fprintf(f, "avg temps/TB %0.2f max=%d\n", |
fca8a500 RH |
4014 | (double)s->temp_count / tb_div_count, s->temp_count_max); |
4015 | cpu_fprintf(f, "avg host code/TB %0.1f\n", | |
4016 | (double)s->code_out_len / tb_div_count); | |
4017 | cpu_fprintf(f, "avg search data/TB %0.1f\n", | |
4018 | (double)s->search_out_len / tb_div_count); | |
a23a9ec6 FB |
4019 | |
4020 | cpu_fprintf(f, "cycles/op %0.1f\n", | |
4021 | s->op_count ? (double)tot / s->op_count : 0); | |
4022 | cpu_fprintf(f, "cycles/in byte %0.1f\n", | |
4023 | s->code_in_len ? (double)tot / s->code_in_len : 0); | |
4024 | cpu_fprintf(f, "cycles/out byte %0.1f\n", | |
4025 | s->code_out_len ? (double)tot / s->code_out_len : 0); | |
fca8a500 RH |
4026 | cpu_fprintf(f, "cycles/search byte %0.1f\n", |
4027 | s->search_out_len ? (double)tot / s->search_out_len : 0); | |
4028 | if (tot == 0) { | |
a23a9ec6 | 4029 | tot = 1; |
fca8a500 | 4030 | } |
a23a9ec6 FB |
4031 | cpu_fprintf(f, " gen_interm time %0.1f%%\n", |
4032 | (double)s->interm_time / tot * 100.0); | |
4033 | cpu_fprintf(f, " gen_code time %0.1f%%\n", | |
4034 | (double)s->code_time / tot * 100.0); | |
c5cc28ff AJ |
4035 | cpu_fprintf(f, "optim./code time %0.1f%%\n", |
4036 | (double)s->opt_time / (s->code_time ? s->code_time : 1) | |
4037 | * 100.0); | |
a23a9ec6 FB |
4038 | cpu_fprintf(f, "liveness/code time %0.1f%%\n", |
4039 | (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0); | |
4040 | cpu_fprintf(f, "cpu_restore count %" PRId64 "\n", | |
4041 | s->restore_count); | |
4042 | cpu_fprintf(f, " avg cycles %0.1f\n", | |
4043 | s->restore_count ? (double)s->restore_time / s->restore_count : 0); | |
a23a9ec6 FB |
4044 | } |
4045 | #else | |
405cf9ff | 4046 | void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf) |
a23a9ec6 | 4047 | { |
24bf7b3a | 4048 | cpu_fprintf(f, "[TCG profiler not compiled]\n"); |
a23a9ec6 FB |
4049 | } |
4050 | #endif | |
813da627 RH |
4051 | |
4052 | #ifdef ELF_HOST_MACHINE | |
5872bbf2 RH |
4053 | /* In order to use this feature, the backend needs to do three things: |
4054 | ||
4055 | (1) Define ELF_HOST_MACHINE to indicate both what value to | |
4056 | put into the ELF image and to indicate support for the feature. | |
4057 | ||
4058 | (2) Define tcg_register_jit. This should create a buffer containing | |
4059 | the contents of a .debug_frame section that describes the post- | |
4060 | prologue unwind info for the tcg machine. | |
4061 | ||
4062 | (3) Call tcg_register_jit_int, with the constructed .debug_frame. | |
4063 | */ | |
813da627 RH |
4064 | |
4065 | /* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */ | |
4066 | typedef enum { | |
4067 | JIT_NOACTION = 0, | |
4068 | JIT_REGISTER_FN, | |
4069 | JIT_UNREGISTER_FN | |
4070 | } jit_actions_t; | |
4071 | ||
4072 | struct jit_code_entry { | |
4073 | struct jit_code_entry *next_entry; | |
4074 | struct jit_code_entry *prev_entry; | |
4075 | const void *symfile_addr; | |
4076 | uint64_t symfile_size; | |
4077 | }; | |
4078 | ||
4079 | struct jit_descriptor { | |
4080 | uint32_t version; | |
4081 | uint32_t action_flag; | |
4082 | struct jit_code_entry *relevant_entry; | |
4083 | struct jit_code_entry *first_entry; | |
4084 | }; | |
4085 | ||
4086 | void __jit_debug_register_code(void) __attribute__((noinline)); | |
4087 | void __jit_debug_register_code(void) | |
4088 | { | |
4089 | asm(""); | |
4090 | } | |
4091 | ||
4092 | /* Must statically initialize the version, because GDB may check | |
4093 | the version before we can set it. */ | |
4094 | struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 }; | |
4095 | ||
4096 | /* End GDB interface. */ | |
4097 | ||
4098 | static int find_string(const char *strtab, const char *str) | |
4099 | { | |
4100 | const char *p = strtab + 1; | |
4101 | ||
4102 | while (1) { | |
4103 | if (strcmp(p, str) == 0) { | |
4104 | return p - strtab; | |
4105 | } | |
4106 | p += strlen(p) + 1; | |
4107 | } | |
4108 | } | |
4109 | ||
5872bbf2 | 4110 | static void tcg_register_jit_int(void *buf_ptr, size_t buf_size, |
2c90784a RH |
4111 | const void *debug_frame, |
4112 | size_t debug_frame_size) | |
813da627 | 4113 | { |
5872bbf2 RH |
4114 | struct __attribute__((packed)) DebugInfo { |
4115 | uint32_t len; | |
4116 | uint16_t version; | |
4117 | uint32_t abbrev; | |
4118 | uint8_t ptr_size; | |
4119 | uint8_t cu_die; | |
4120 | uint16_t cu_lang; | |
4121 | uintptr_t cu_low_pc; | |
4122 | uintptr_t cu_high_pc; | |
4123 | uint8_t fn_die; | |
4124 | char fn_name[16]; | |
4125 | uintptr_t fn_low_pc; | |
4126 | uintptr_t fn_high_pc; | |
4127 | uint8_t cu_eoc; | |
4128 | }; | |
813da627 RH |
4129 | |
4130 | struct ElfImage { | |
4131 | ElfW(Ehdr) ehdr; | |
4132 | ElfW(Phdr) phdr; | |
5872bbf2 RH |
4133 | ElfW(Shdr) shdr[7]; |
4134 | ElfW(Sym) sym[2]; | |
4135 | struct DebugInfo di; | |
4136 | uint8_t da[24]; | |
4137 | char str[80]; | |
4138 | }; | |
4139 | ||
4140 | struct ElfImage *img; | |
4141 | ||
4142 | static const struct ElfImage img_template = { | |
4143 | .ehdr = { | |
4144 | .e_ident[EI_MAG0] = ELFMAG0, | |
4145 | .e_ident[EI_MAG1] = ELFMAG1, | |
4146 | .e_ident[EI_MAG2] = ELFMAG2, | |
4147 | .e_ident[EI_MAG3] = ELFMAG3, | |
4148 | .e_ident[EI_CLASS] = ELF_CLASS, | |
4149 | .e_ident[EI_DATA] = ELF_DATA, | |
4150 | .e_ident[EI_VERSION] = EV_CURRENT, | |
4151 | .e_type = ET_EXEC, | |
4152 | .e_machine = ELF_HOST_MACHINE, | |
4153 | .e_version = EV_CURRENT, | |
4154 | .e_phoff = offsetof(struct ElfImage, phdr), | |
4155 | .e_shoff = offsetof(struct ElfImage, shdr), | |
4156 | .e_ehsize = sizeof(ElfW(Shdr)), | |
4157 | .e_phentsize = sizeof(ElfW(Phdr)), | |
4158 | .e_phnum = 1, | |
4159 | .e_shentsize = sizeof(ElfW(Shdr)), | |
4160 | .e_shnum = ARRAY_SIZE(img->shdr), | |
4161 | .e_shstrndx = ARRAY_SIZE(img->shdr) - 1, | |
abbb3eae RH |
4162 | #ifdef ELF_HOST_FLAGS |
4163 | .e_flags = ELF_HOST_FLAGS, | |
4164 | #endif | |
4165 | #ifdef ELF_OSABI | |
4166 | .e_ident[EI_OSABI] = ELF_OSABI, | |
4167 | #endif | |
5872bbf2 RH |
4168 | }, |
4169 | .phdr = { | |
4170 | .p_type = PT_LOAD, | |
4171 | .p_flags = PF_X, | |
4172 | }, | |
4173 | .shdr = { | |
4174 | [0] = { .sh_type = SHT_NULL }, | |
4175 | /* Trick: The contents of code_gen_buffer are not present in | |
4176 | this fake ELF file; that got allocated elsewhere. Therefore | |
4177 | we mark .text as SHT_NOBITS (similar to .bss) so that readers | |
4178 | will not look for contents. We can record any address. */ | |
4179 | [1] = { /* .text */ | |
4180 | .sh_type = SHT_NOBITS, | |
4181 | .sh_flags = SHF_EXECINSTR | SHF_ALLOC, | |
4182 | }, | |
4183 | [2] = { /* .debug_info */ | |
4184 | .sh_type = SHT_PROGBITS, | |
4185 | .sh_offset = offsetof(struct ElfImage, di), | |
4186 | .sh_size = sizeof(struct DebugInfo), | |
4187 | }, | |
4188 | [3] = { /* .debug_abbrev */ | |
4189 | .sh_type = SHT_PROGBITS, | |
4190 | .sh_offset = offsetof(struct ElfImage, da), | |
4191 | .sh_size = sizeof(img->da), | |
4192 | }, | |
4193 | [4] = { /* .debug_frame */ | |
4194 | .sh_type = SHT_PROGBITS, | |
4195 | .sh_offset = sizeof(struct ElfImage), | |
4196 | }, | |
4197 | [5] = { /* .symtab */ | |
4198 | .sh_type = SHT_SYMTAB, | |
4199 | .sh_offset = offsetof(struct ElfImage, sym), | |
4200 | .sh_size = sizeof(img->sym), | |
4201 | .sh_info = 1, | |
4202 | .sh_link = ARRAY_SIZE(img->shdr) - 1, | |
4203 | .sh_entsize = sizeof(ElfW(Sym)), | |
4204 | }, | |
4205 | [6] = { /* .strtab */ | |
4206 | .sh_type = SHT_STRTAB, | |
4207 | .sh_offset = offsetof(struct ElfImage, str), | |
4208 | .sh_size = sizeof(img->str), | |
4209 | } | |
4210 | }, | |
4211 | .sym = { | |
4212 | [1] = { /* code_gen_buffer */ | |
4213 | .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC), | |
4214 | .st_shndx = 1, | |
4215 | } | |
4216 | }, | |
4217 | .di = { | |
4218 | .len = sizeof(struct DebugInfo) - 4, | |
4219 | .version = 2, | |
4220 | .ptr_size = sizeof(void *), | |
4221 | .cu_die = 1, | |
4222 | .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */ | |
4223 | .fn_die = 2, | |
4224 | .fn_name = "code_gen_buffer" | |
4225 | }, | |
4226 | .da = { | |
4227 | 1, /* abbrev number (the cu) */ | |
4228 | 0x11, 1, /* DW_TAG_compile_unit, has children */ | |
4229 | 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */ | |
4230 | 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */ | |
4231 | 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */ | |
4232 | 0, 0, /* end of abbrev */ | |
4233 | 2, /* abbrev number (the fn) */ | |
4234 | 0x2e, 0, /* DW_TAG_subprogram, no children */ | |
4235 | 0x3, 0x8, /* DW_AT_name, DW_FORM_string */ | |
4236 | 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */ | |
4237 | 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */ | |
4238 | 0, 0, /* end of abbrev */ | |
4239 | 0 /* no more abbrev */ | |
4240 | }, | |
4241 | .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0" | |
4242 | ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer", | |
813da627 RH |
4243 | }; |
4244 | ||
4245 | /* We only need a single jit entry; statically allocate it. */ | |
4246 | static struct jit_code_entry one_entry; | |
4247 | ||
5872bbf2 | 4248 | uintptr_t buf = (uintptr_t)buf_ptr; |
813da627 | 4249 | size_t img_size = sizeof(struct ElfImage) + debug_frame_size; |
2c90784a | 4250 | DebugFrameHeader *dfh; |
813da627 | 4251 | |
5872bbf2 RH |
4252 | img = g_malloc(img_size); |
4253 | *img = img_template; | |
813da627 | 4254 | |
5872bbf2 RH |
4255 | img->phdr.p_vaddr = buf; |
4256 | img->phdr.p_paddr = buf; | |
4257 | img->phdr.p_memsz = buf_size; | |
813da627 | 4258 | |
813da627 | 4259 | img->shdr[1].sh_name = find_string(img->str, ".text"); |
5872bbf2 | 4260 | img->shdr[1].sh_addr = buf; |
813da627 RH |
4261 | img->shdr[1].sh_size = buf_size; |
4262 | ||
5872bbf2 RH |
4263 | img->shdr[2].sh_name = find_string(img->str, ".debug_info"); |
4264 | img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev"); | |
4265 | ||
4266 | img->shdr[4].sh_name = find_string(img->str, ".debug_frame"); | |
4267 | img->shdr[4].sh_size = debug_frame_size; | |
4268 | ||
4269 | img->shdr[5].sh_name = find_string(img->str, ".symtab"); | |
4270 | img->shdr[6].sh_name = find_string(img->str, ".strtab"); | |
4271 | ||
4272 | img->sym[1].st_name = find_string(img->str, "code_gen_buffer"); | |
4273 | img->sym[1].st_value = buf; | |
4274 | img->sym[1].st_size = buf_size; | |
813da627 | 4275 | |
5872bbf2 | 4276 | img->di.cu_low_pc = buf; |
45aba097 | 4277 | img->di.cu_high_pc = buf + buf_size; |
5872bbf2 | 4278 | img->di.fn_low_pc = buf; |
45aba097 | 4279 | img->di.fn_high_pc = buf + buf_size; |
813da627 | 4280 | |
2c90784a RH |
4281 | dfh = (DebugFrameHeader *)(img + 1); |
4282 | memcpy(dfh, debug_frame, debug_frame_size); | |
4283 | dfh->fde.func_start = buf; | |
4284 | dfh->fde.func_len = buf_size; | |
4285 | ||
813da627 RH |
4286 | #ifdef DEBUG_JIT |
4287 | /* Enable this block to be able to debug the ELF image file creation. | |
4288 | One can use readelf, objdump, or other inspection utilities. */ | |
4289 | { | |
4290 | FILE *f = fopen("/tmp/qemu.jit", "w+b"); | |
4291 | if (f) { | |
5872bbf2 | 4292 | if (fwrite(img, img_size, 1, f) != img_size) { |
813da627 RH |
4293 | /* Avoid stupid unused return value warning for fwrite. */ |
4294 | } | |
4295 | fclose(f); | |
4296 | } | |
4297 | } | |
4298 | #endif | |
4299 | ||
4300 | one_entry.symfile_addr = img; | |
4301 | one_entry.symfile_size = img_size; | |
4302 | ||
4303 | __jit_debug_descriptor.action_flag = JIT_REGISTER_FN; | |
4304 | __jit_debug_descriptor.relevant_entry = &one_entry; | |
4305 | __jit_debug_descriptor.first_entry = &one_entry; | |
4306 | __jit_debug_register_code(); | |
4307 | } | |
4308 | #else | |
5872bbf2 RH |
4309 | /* No support for the feature. Provide the entry point expected by exec.c, |
4310 | and implement the internal function we declared earlier. */ | |
813da627 RH |
4311 | |
4312 | static void tcg_register_jit_int(void *buf, size_t size, | |
2c90784a RH |
4313 | const void *debug_frame, |
4314 | size_t debug_frame_size) | |
813da627 RH |
4315 | { |
4316 | } | |
4317 | ||
4318 | void tcg_register_jit(void *buf, size_t buf_size) | |
4319 | { | |
4320 | } | |
4321 | #endif /* ELF_HOST_MACHINE */ | |
db432672 RH |
4322 | |
4323 | #if !TCG_TARGET_MAYBE_vec | |
4324 | void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...) | |
4325 | { | |
4326 | g_assert_not_reached(); | |
4327 | } | |
4328 | #endif |