]> git.proxmox.com Git - mirror_qemu.git/blob - tcg/tcg.c
tcg: Move tcg_temp_new_*, tcg_global_mem_new_* out of line
[mirror_qemu.git] / tcg / tcg.c
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
25 #include "qemu/osdep.h"
26
27 /* Define to jump the ELF file used to communicate with GDB. */
28 #undef DEBUG_JIT
29
30 #include "qemu/error-report.h"
31 #include "qemu/cutils.h"
32 #include "qemu/host-utils.h"
33 #include "qemu/qemu-print.h"
34 #include "qemu/cacheflush.h"
35 #include "qemu/cacheinfo.h"
36 #include "qemu/timer.h"
37 #include "exec/translation-block.h"
38 #include "exec/tlb-common.h"
39 #include "tcg/startup.h"
40 #include "tcg/tcg-op-common.h"
41
42 #if UINTPTR_MAX == UINT32_MAX
43 # define ELF_CLASS ELFCLASS32
44 #else
45 # define ELF_CLASS ELFCLASS64
46 #endif
47 #if HOST_BIG_ENDIAN
48 # define ELF_DATA ELFDATA2MSB
49 #else
50 # define ELF_DATA ELFDATA2LSB
51 #endif
52
53 #include "elf.h"
54 #include "exec/log.h"
55 #include "tcg/tcg-ldst.h"
56 #include "tcg/tcg-temp-internal.h"
57 #include "tcg-internal.h"
58 #include "accel/tcg/perf.h"
59 #ifdef CONFIG_USER_ONLY
60 #include "exec/user/guest-base.h"
61 #endif
62
63 /* Forward declarations for functions declared in tcg-target.c.inc and
64 used here. */
65 static void tcg_target_init(TCGContext *s);
66 static void tcg_target_qemu_prologue(TCGContext *s);
67 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
68 intptr_t value, intptr_t addend);
69
70 /* The CIE and FDE header definitions will be common to all hosts. */
71 typedef struct {
72 uint32_t len __attribute__((aligned((sizeof(void *)))));
73 uint32_t id;
74 uint8_t version;
75 char augmentation[1];
76 uint8_t code_align;
77 uint8_t data_align;
78 uint8_t return_column;
79 } DebugFrameCIE;
80
81 typedef struct QEMU_PACKED {
82 uint32_t len __attribute__((aligned((sizeof(void *)))));
83 uint32_t cie_offset;
84 uintptr_t func_start;
85 uintptr_t func_len;
86 } DebugFrameFDEHeader;
87
88 typedef struct QEMU_PACKED {
89 DebugFrameCIE cie;
90 DebugFrameFDEHeader fde;
91 } DebugFrameHeader;
92
93 typedef struct TCGLabelQemuLdst {
94 bool is_ld; /* qemu_ld: true, qemu_st: false */
95 MemOpIdx oi;
96 TCGType type; /* result type of a load */
97 TCGReg addrlo_reg; /* reg index for low word of guest virtual addr */
98 TCGReg addrhi_reg; /* reg index for high word of guest virtual addr */
99 TCGReg datalo_reg; /* reg index for low word to be loaded or stored */
100 TCGReg datahi_reg; /* reg index for high word to be loaded or stored */
101 const tcg_insn_unit *raddr; /* addr of the next IR of qemu_ld/st IR */
102 tcg_insn_unit *label_ptr[2]; /* label pointers to be updated */
103 QSIMPLEQ_ENTRY(TCGLabelQemuLdst) next;
104 } TCGLabelQemuLdst;
105
106 static void tcg_register_jit_int(const void *buf, size_t size,
107 const void *debug_frame,
108 size_t debug_frame_size)
109 __attribute__((unused));
110
111 /* Forward declarations for functions declared and used in tcg-target.c.inc. */
112 static void tcg_out_tb_start(TCGContext *s);
113 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
114 intptr_t arg2);
115 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
116 static void tcg_out_movi(TCGContext *s, TCGType type,
117 TCGReg ret, tcg_target_long arg);
118 static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
119 static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
120 static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg);
121 static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg);
122 static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg);
123 static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg);
124 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg);
125 static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg);
126 static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg);
127 static void tcg_out_addi_ptr(TCGContext *s, TCGReg, TCGReg, tcg_target_long);
128 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2);
129 static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg);
130 static void tcg_out_goto_tb(TCGContext *s, int which);
131 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
132 const TCGArg args[TCG_MAX_OP_ARGS],
133 const int const_args[TCG_MAX_OP_ARGS]);
134 #if TCG_TARGET_MAYBE_vec
135 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
136 TCGReg dst, TCGReg src);
137 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
138 TCGReg dst, TCGReg base, intptr_t offset);
139 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
140 TCGReg dst, int64_t arg);
141 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
142 unsigned vecl, unsigned vece,
143 const TCGArg args[TCG_MAX_OP_ARGS],
144 const int const_args[TCG_MAX_OP_ARGS]);
145 #else
146 static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
147 TCGReg dst, TCGReg src)
148 {
149 g_assert_not_reached();
150 }
151 static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
152 TCGReg dst, TCGReg base, intptr_t offset)
153 {
154 g_assert_not_reached();
155 }
156 static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
157 TCGReg dst, int64_t arg)
158 {
159 g_assert_not_reached();
160 }
161 static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
162 unsigned vecl, unsigned vece,
163 const TCGArg args[TCG_MAX_OP_ARGS],
164 const int const_args[TCG_MAX_OP_ARGS])
165 {
166 g_assert_not_reached();
167 }
168 #endif
169 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
170 intptr_t arg2);
171 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
172 TCGReg base, intptr_t ofs);
173 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target,
174 const TCGHelperInfo *info);
175 static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot);
176 static bool tcg_target_const_match(int64_t val, TCGType type, int ct, int vece);
177 #ifdef TCG_TARGET_NEED_LDST_LABELS
178 static int tcg_out_ldst_finalize(TCGContext *s);
179 #endif
180
181 #ifndef CONFIG_USER_ONLY
182 #define guest_base ({ qemu_build_not_reached(); (uintptr_t)0; })
183 #endif
184
185 typedef struct TCGLdstHelperParam {
186 TCGReg (*ra_gen)(TCGContext *s, const TCGLabelQemuLdst *l, int arg_reg);
187 unsigned ntmp;
188 int tmp[3];
189 } TCGLdstHelperParam;
190
191 static void tcg_out_ld_helper_args(TCGContext *s, const TCGLabelQemuLdst *l,
192 const TCGLdstHelperParam *p)
193 __attribute__((unused));
194 static void tcg_out_ld_helper_ret(TCGContext *s, const TCGLabelQemuLdst *l,
195 bool load_sign, const TCGLdstHelperParam *p)
196 __attribute__((unused));
197 static void tcg_out_st_helper_args(TCGContext *s, const TCGLabelQemuLdst *l,
198 const TCGLdstHelperParam *p)
199 __attribute__((unused));
200
201 static void * const qemu_ld_helpers[MO_SSIZE + 1] __attribute__((unused)) = {
202 [MO_UB] = helper_ldub_mmu,
203 [MO_SB] = helper_ldsb_mmu,
204 [MO_UW] = helper_lduw_mmu,
205 [MO_SW] = helper_ldsw_mmu,
206 [MO_UL] = helper_ldul_mmu,
207 [MO_UQ] = helper_ldq_mmu,
208 #if TCG_TARGET_REG_BITS == 64
209 [MO_SL] = helper_ldsl_mmu,
210 [MO_128] = helper_ld16_mmu,
211 #endif
212 };
213
214 static void * const qemu_st_helpers[MO_SIZE + 1] __attribute__((unused)) = {
215 [MO_8] = helper_stb_mmu,
216 [MO_16] = helper_stw_mmu,
217 [MO_32] = helper_stl_mmu,
218 [MO_64] = helper_stq_mmu,
219 #if TCG_TARGET_REG_BITS == 64
220 [MO_128] = helper_st16_mmu,
221 #endif
222 };
223
224 typedef struct {
225 MemOp atom; /* lg2 bits of atomicity required */
226 MemOp align; /* lg2 bits of alignment to use */
227 } TCGAtomAlign;
228
229 static TCGAtomAlign atom_and_align_for_opc(TCGContext *s, MemOp opc,
230 MemOp host_atom, bool allow_two_ops)
231 __attribute__((unused));
232
233 #ifdef CONFIG_USER_ONLY
234 bool tcg_use_softmmu;
235 #endif
236
237 TCGContext tcg_init_ctx;
238 __thread TCGContext *tcg_ctx;
239
240 TCGContext **tcg_ctxs;
241 unsigned int tcg_cur_ctxs;
242 unsigned int tcg_max_ctxs;
243 TCGv_env tcg_env;
244 const void *tcg_code_gen_epilogue;
245 uintptr_t tcg_splitwx_diff;
246
247 #ifndef CONFIG_TCG_INTERPRETER
248 tcg_prologue_fn *tcg_qemu_tb_exec;
249 #endif
250
251 static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
252 static TCGRegSet tcg_target_call_clobber_regs;
253
254 #if TCG_TARGET_INSN_UNIT_SIZE == 1
255 static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
256 {
257 *s->code_ptr++ = v;
258 }
259
260 static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
261 uint8_t v)
262 {
263 *p = v;
264 }
265 #endif
266
267 #if TCG_TARGET_INSN_UNIT_SIZE <= 2
268 static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
269 {
270 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
271 *s->code_ptr++ = v;
272 } else {
273 tcg_insn_unit *p = s->code_ptr;
274 memcpy(p, &v, sizeof(v));
275 s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
276 }
277 }
278
279 static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
280 uint16_t v)
281 {
282 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
283 *p = v;
284 } else {
285 memcpy(p, &v, sizeof(v));
286 }
287 }
288 #endif
289
290 #if TCG_TARGET_INSN_UNIT_SIZE <= 4
291 static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
292 {
293 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
294 *s->code_ptr++ = v;
295 } else {
296 tcg_insn_unit *p = s->code_ptr;
297 memcpy(p, &v, sizeof(v));
298 s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
299 }
300 }
301
302 static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
303 uint32_t v)
304 {
305 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
306 *p = v;
307 } else {
308 memcpy(p, &v, sizeof(v));
309 }
310 }
311 #endif
312
313 #if TCG_TARGET_INSN_UNIT_SIZE <= 8
314 static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
315 {
316 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
317 *s->code_ptr++ = v;
318 } else {
319 tcg_insn_unit *p = s->code_ptr;
320 memcpy(p, &v, sizeof(v));
321 s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
322 }
323 }
324
325 static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
326 uint64_t v)
327 {
328 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
329 *p = v;
330 } else {
331 memcpy(p, &v, sizeof(v));
332 }
333 }
334 #endif
335
336 /* label relocation processing */
337
338 static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
339 TCGLabel *l, intptr_t addend)
340 {
341 TCGRelocation *r = tcg_malloc(sizeof(TCGRelocation));
342
343 r->type = type;
344 r->ptr = code_ptr;
345 r->addend = addend;
346 QSIMPLEQ_INSERT_TAIL(&l->relocs, r, next);
347 }
348
349 static void tcg_out_label(TCGContext *s, TCGLabel *l)
350 {
351 tcg_debug_assert(!l->has_value);
352 l->has_value = 1;
353 l->u.value_ptr = tcg_splitwx_to_rx(s->code_ptr);
354 }
355
356 TCGLabel *gen_new_label(void)
357 {
358 TCGContext *s = tcg_ctx;
359 TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
360
361 memset(l, 0, sizeof(TCGLabel));
362 l->id = s->nb_labels++;
363 QSIMPLEQ_INIT(&l->branches);
364 QSIMPLEQ_INIT(&l->relocs);
365
366 QSIMPLEQ_INSERT_TAIL(&s->labels, l, next);
367
368 return l;
369 }
370
371 static bool tcg_resolve_relocs(TCGContext *s)
372 {
373 TCGLabel *l;
374
375 QSIMPLEQ_FOREACH(l, &s->labels, next) {
376 TCGRelocation *r;
377 uintptr_t value = l->u.value;
378
379 QSIMPLEQ_FOREACH(r, &l->relocs, next) {
380 if (!patch_reloc(r->ptr, r->type, value, r->addend)) {
381 return false;
382 }
383 }
384 }
385 return true;
386 }
387
388 static void set_jmp_reset_offset(TCGContext *s, int which)
389 {
390 /*
391 * We will check for overflow at the end of the opcode loop in
392 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
393 */
394 s->gen_tb->jmp_reset_offset[which] = tcg_current_code_size(s);
395 }
396
397 static void G_GNUC_UNUSED set_jmp_insn_offset(TCGContext *s, int which)
398 {
399 /*
400 * We will check for overflow at the end of the opcode loop in
401 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
402 */
403 s->gen_tb->jmp_insn_offset[which] = tcg_current_code_size(s);
404 }
405
406 static uintptr_t G_GNUC_UNUSED get_jmp_target_addr(TCGContext *s, int which)
407 {
408 /*
409 * Return the read-execute version of the pointer, for the benefit
410 * of any pc-relative addressing mode.
411 */
412 return (uintptr_t)tcg_splitwx_to_rx(&s->gen_tb->jmp_target_addr[which]);
413 }
414
415 static int __attribute__((unused))
416 tlb_mask_table_ofs(TCGContext *s, int which)
417 {
418 return (offsetof(CPUNegativeOffsetState, tlb.f[which]) -
419 sizeof(CPUNegativeOffsetState));
420 }
421
422 /* Signal overflow, starting over with fewer guest insns. */
423 static G_NORETURN
424 void tcg_raise_tb_overflow(TCGContext *s)
425 {
426 siglongjmp(s->jmp_trans, -2);
427 }
428
429 /*
430 * Used by tcg_out_movext{1,2} to hold the arguments for tcg_out_movext.
431 * By the time we arrive at tcg_out_movext1, @dst is always a TCGReg.
432 *
433 * However, tcg_out_helper_load_slots reuses this field to hold an
434 * argument slot number (which may designate a argument register or an
435 * argument stack slot), converting to TCGReg once all arguments that
436 * are destined for the stack are processed.
437 */
438 typedef struct TCGMovExtend {
439 unsigned dst;
440 TCGReg src;
441 TCGType dst_type;
442 TCGType src_type;
443 MemOp src_ext;
444 } TCGMovExtend;
445
446 /**
447 * tcg_out_movext -- move and extend
448 * @s: tcg context
449 * @dst_type: integral type for destination
450 * @dst: destination register
451 * @src_type: integral type for source
452 * @src_ext: extension to apply to source
453 * @src: source register
454 *
455 * Move or extend @src into @dst, depending on @src_ext and the types.
456 */
457 static void tcg_out_movext(TCGContext *s, TCGType dst_type, TCGReg dst,
458 TCGType src_type, MemOp src_ext, TCGReg src)
459 {
460 switch (src_ext) {
461 case MO_UB:
462 tcg_out_ext8u(s, dst, src);
463 break;
464 case MO_SB:
465 tcg_out_ext8s(s, dst_type, dst, src);
466 break;
467 case MO_UW:
468 tcg_out_ext16u(s, dst, src);
469 break;
470 case MO_SW:
471 tcg_out_ext16s(s, dst_type, dst, src);
472 break;
473 case MO_UL:
474 case MO_SL:
475 if (dst_type == TCG_TYPE_I32) {
476 if (src_type == TCG_TYPE_I32) {
477 tcg_out_mov(s, TCG_TYPE_I32, dst, src);
478 } else {
479 tcg_out_extrl_i64_i32(s, dst, src);
480 }
481 } else if (src_type == TCG_TYPE_I32) {
482 if (src_ext & MO_SIGN) {
483 tcg_out_exts_i32_i64(s, dst, src);
484 } else {
485 tcg_out_extu_i32_i64(s, dst, src);
486 }
487 } else {
488 if (src_ext & MO_SIGN) {
489 tcg_out_ext32s(s, dst, src);
490 } else {
491 tcg_out_ext32u(s, dst, src);
492 }
493 }
494 break;
495 case MO_UQ:
496 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
497 if (dst_type == TCG_TYPE_I32) {
498 tcg_out_extrl_i64_i32(s, dst, src);
499 } else {
500 tcg_out_mov(s, TCG_TYPE_I64, dst, src);
501 }
502 break;
503 default:
504 g_assert_not_reached();
505 }
506 }
507
508 /* Minor variations on a theme, using a structure. */
509 static void tcg_out_movext1_new_src(TCGContext *s, const TCGMovExtend *i,
510 TCGReg src)
511 {
512 tcg_out_movext(s, i->dst_type, i->dst, i->src_type, i->src_ext, src);
513 }
514
515 static void tcg_out_movext1(TCGContext *s, const TCGMovExtend *i)
516 {
517 tcg_out_movext1_new_src(s, i, i->src);
518 }
519
520 /**
521 * tcg_out_movext2 -- move and extend two pair
522 * @s: tcg context
523 * @i1: first move description
524 * @i2: second move description
525 * @scratch: temporary register, or -1 for none
526 *
527 * As tcg_out_movext, for both @i1 and @i2, caring for overlap
528 * between the sources and destinations.
529 */
530
531 static void tcg_out_movext2(TCGContext *s, const TCGMovExtend *i1,
532 const TCGMovExtend *i2, int scratch)
533 {
534 TCGReg src1 = i1->src;
535 TCGReg src2 = i2->src;
536
537 if (i1->dst != src2) {
538 tcg_out_movext1(s, i1);
539 tcg_out_movext1(s, i2);
540 return;
541 }
542 if (i2->dst == src1) {
543 TCGType src1_type = i1->src_type;
544 TCGType src2_type = i2->src_type;
545
546 if (tcg_out_xchg(s, MAX(src1_type, src2_type), src1, src2)) {
547 /* The data is now in the correct registers, now extend. */
548 src1 = i2->src;
549 src2 = i1->src;
550 } else {
551 tcg_debug_assert(scratch >= 0);
552 tcg_out_mov(s, src1_type, scratch, src1);
553 src1 = scratch;
554 }
555 }
556 tcg_out_movext1_new_src(s, i2, src2);
557 tcg_out_movext1_new_src(s, i1, src1);
558 }
559
560 /**
561 * tcg_out_movext3 -- move and extend three pair
562 * @s: tcg context
563 * @i1: first move description
564 * @i2: second move description
565 * @i3: third move description
566 * @scratch: temporary register, or -1 for none
567 *
568 * As tcg_out_movext, for all of @i1, @i2 and @i3, caring for overlap
569 * between the sources and destinations.
570 */
571
572 static void tcg_out_movext3(TCGContext *s, const TCGMovExtend *i1,
573 const TCGMovExtend *i2, const TCGMovExtend *i3,
574 int scratch)
575 {
576 TCGReg src1 = i1->src;
577 TCGReg src2 = i2->src;
578 TCGReg src3 = i3->src;
579
580 if (i1->dst != src2 && i1->dst != src3) {
581 tcg_out_movext1(s, i1);
582 tcg_out_movext2(s, i2, i3, scratch);
583 return;
584 }
585 if (i2->dst != src1 && i2->dst != src3) {
586 tcg_out_movext1(s, i2);
587 tcg_out_movext2(s, i1, i3, scratch);
588 return;
589 }
590 if (i3->dst != src1 && i3->dst != src2) {
591 tcg_out_movext1(s, i3);
592 tcg_out_movext2(s, i1, i2, scratch);
593 return;
594 }
595
596 /*
597 * There is a cycle. Since there are only 3 nodes, the cycle is
598 * either "clockwise" or "anti-clockwise", and can be solved with
599 * a single scratch or two xchg.
600 */
601 if (i1->dst == src2 && i2->dst == src3 && i3->dst == src1) {
602 /* "Clockwise" */
603 if (tcg_out_xchg(s, MAX(i1->src_type, i2->src_type), src1, src2)) {
604 tcg_out_xchg(s, MAX(i2->src_type, i3->src_type), src2, src3);
605 /* The data is now in the correct registers, now extend. */
606 tcg_out_movext1_new_src(s, i1, i1->dst);
607 tcg_out_movext1_new_src(s, i2, i2->dst);
608 tcg_out_movext1_new_src(s, i3, i3->dst);
609 } else {
610 tcg_debug_assert(scratch >= 0);
611 tcg_out_mov(s, i1->src_type, scratch, src1);
612 tcg_out_movext1(s, i3);
613 tcg_out_movext1(s, i2);
614 tcg_out_movext1_new_src(s, i1, scratch);
615 }
616 } else if (i1->dst == src3 && i2->dst == src1 && i3->dst == src2) {
617 /* "Anti-clockwise" */
618 if (tcg_out_xchg(s, MAX(i2->src_type, i3->src_type), src2, src3)) {
619 tcg_out_xchg(s, MAX(i1->src_type, i2->src_type), src1, src2);
620 /* The data is now in the correct registers, now extend. */
621 tcg_out_movext1_new_src(s, i1, i1->dst);
622 tcg_out_movext1_new_src(s, i2, i2->dst);
623 tcg_out_movext1_new_src(s, i3, i3->dst);
624 } else {
625 tcg_debug_assert(scratch >= 0);
626 tcg_out_mov(s, i1->src_type, scratch, src1);
627 tcg_out_movext1(s, i2);
628 tcg_out_movext1(s, i3);
629 tcg_out_movext1_new_src(s, i1, scratch);
630 }
631 } else {
632 g_assert_not_reached();
633 }
634 }
635
636 #define C_PFX1(P, A) P##A
637 #define C_PFX2(P, A, B) P##A##_##B
638 #define C_PFX3(P, A, B, C) P##A##_##B##_##C
639 #define C_PFX4(P, A, B, C, D) P##A##_##B##_##C##_##D
640 #define C_PFX5(P, A, B, C, D, E) P##A##_##B##_##C##_##D##_##E
641 #define C_PFX6(P, A, B, C, D, E, F) P##A##_##B##_##C##_##D##_##E##_##F
642
643 /* Define an enumeration for the various combinations. */
644
645 #define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1),
646 #define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2),
647 #define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3),
648 #define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4),
649
650 #define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1),
651 #define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2),
652 #define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3),
653 #define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4),
654
655 #define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2),
656 #define C_N2_I1(O1, O2, I1) C_PFX3(c_n2_i1_, O1, O2, I1),
657
658 #define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1),
659 #define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2),
660 #define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3),
661 #define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4),
662 #define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_n1_o1_i4_, O1, O2, I1, I2, I3, I4),
663
664 typedef enum {
665 #include "tcg-target-con-set.h"
666 } TCGConstraintSetIndex;
667
668 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode);
669
670 #undef C_O0_I1
671 #undef C_O0_I2
672 #undef C_O0_I3
673 #undef C_O0_I4
674 #undef C_O1_I1
675 #undef C_O1_I2
676 #undef C_O1_I3
677 #undef C_O1_I4
678 #undef C_N1_I2
679 #undef C_N2_I1
680 #undef C_O2_I1
681 #undef C_O2_I2
682 #undef C_O2_I3
683 #undef C_O2_I4
684 #undef C_N1_O1_I4
685
686 /* Put all of the constraint sets into an array, indexed by the enum. */
687
688 #define C_O0_I1(I1) { .args_ct_str = { #I1 } },
689 #define C_O0_I2(I1, I2) { .args_ct_str = { #I1, #I2 } },
690 #define C_O0_I3(I1, I2, I3) { .args_ct_str = { #I1, #I2, #I3 } },
691 #define C_O0_I4(I1, I2, I3, I4) { .args_ct_str = { #I1, #I2, #I3, #I4 } },
692
693 #define C_O1_I1(O1, I1) { .args_ct_str = { #O1, #I1 } },
694 #define C_O1_I2(O1, I1, I2) { .args_ct_str = { #O1, #I1, #I2 } },
695 #define C_O1_I3(O1, I1, I2, I3) { .args_ct_str = { #O1, #I1, #I2, #I3 } },
696 #define C_O1_I4(O1, I1, I2, I3, I4) { .args_ct_str = { #O1, #I1, #I2, #I3, #I4 } },
697
698 #define C_N1_I2(O1, I1, I2) { .args_ct_str = { "&" #O1, #I1, #I2 } },
699 #define C_N2_I1(O1, O2, I1) { .args_ct_str = { "&" #O1, "&" #O2, #I1 } },
700
701 #define C_O2_I1(O1, O2, I1) { .args_ct_str = { #O1, #O2, #I1 } },
702 #define C_O2_I2(O1, O2, I1, I2) { .args_ct_str = { #O1, #O2, #I1, #I2 } },
703 #define C_O2_I3(O1, O2, I1, I2, I3) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3 } },
704 #define C_O2_I4(O1, O2, I1, I2, I3, I4) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3, #I4 } },
705 #define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) { .args_ct_str = { "&" #O1, #O2, #I1, #I2, #I3, #I4 } },
706
707 static const TCGTargetOpDef constraint_sets[] = {
708 #include "tcg-target-con-set.h"
709 };
710
711
712 #undef C_O0_I1
713 #undef C_O0_I2
714 #undef C_O0_I3
715 #undef C_O0_I4
716 #undef C_O1_I1
717 #undef C_O1_I2
718 #undef C_O1_I3
719 #undef C_O1_I4
720 #undef C_N1_I2
721 #undef C_N2_I1
722 #undef C_O2_I1
723 #undef C_O2_I2
724 #undef C_O2_I3
725 #undef C_O2_I4
726 #undef C_N1_O1_I4
727
728 /* Expand the enumerator to be returned from tcg_target_op_def(). */
729
730 #define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1)
731 #define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2)
732 #define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3)
733 #define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4)
734
735 #define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1)
736 #define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2)
737 #define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3)
738 #define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4)
739
740 #define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2)
741 #define C_N2_I1(O1, O2, I1) C_PFX3(c_n2_i1_, O1, O2, I1)
742
743 #define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1)
744 #define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2)
745 #define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3)
746 #define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4)
747 #define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_n1_o1_i4_, O1, O2, I1, I2, I3, I4)
748
749 #include "tcg-target.c.inc"
750
751 #ifndef CONFIG_TCG_INTERPRETER
752 /* Validate CPUTLBDescFast placement. */
753 QEMU_BUILD_BUG_ON((int)(offsetof(CPUNegativeOffsetState, tlb.f[0]) -
754 sizeof(CPUNegativeOffsetState))
755 < MIN_TLB_MASK_TABLE_OFS);
756 #endif
757
758 static void alloc_tcg_plugin_context(TCGContext *s)
759 {
760 #ifdef CONFIG_PLUGIN
761 s->plugin_tb = g_new0(struct qemu_plugin_tb, 1);
762 s->plugin_tb->insns =
763 g_ptr_array_new_with_free_func(qemu_plugin_insn_cleanup_fn);
764 #endif
765 }
766
767 /*
768 * All TCG threads except the parent (i.e. the one that called tcg_context_init
769 * and registered the target's TCG globals) must register with this function
770 * before initiating translation.
771 *
772 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
773 * of tcg_region_init() for the reasoning behind this.
774 *
775 * In system-mode each caller registers its context in tcg_ctxs[]. Note that in
776 * system-mode tcg_ctxs[] does not track tcg_ctx_init, since the initial context
777 * is not used anymore for translation once this function is called.
778 *
779 * Not tracking tcg_init_ctx in tcg_ctxs[] in system-mode keeps code that
780 * iterates over the array (e.g. tcg_code_size() the same for both system/user
781 * modes.
782 */
783 #ifdef CONFIG_USER_ONLY
784 void tcg_register_thread(void)
785 {
786 tcg_ctx = &tcg_init_ctx;
787 }
788 #else
789 void tcg_register_thread(void)
790 {
791 TCGContext *s = g_malloc(sizeof(*s));
792 unsigned int i, n;
793
794 *s = tcg_init_ctx;
795
796 /* Relink mem_base. */
797 for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
798 if (tcg_init_ctx.temps[i].mem_base) {
799 ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
800 tcg_debug_assert(b >= 0 && b < n);
801 s->temps[i].mem_base = &s->temps[b];
802 }
803 }
804
805 /* Claim an entry in tcg_ctxs */
806 n = qatomic_fetch_inc(&tcg_cur_ctxs);
807 g_assert(n < tcg_max_ctxs);
808 qatomic_set(&tcg_ctxs[n], s);
809
810 if (n > 0) {
811 alloc_tcg_plugin_context(s);
812 tcg_region_initial_alloc(s);
813 }
814
815 tcg_ctx = s;
816 }
817 #endif /* !CONFIG_USER_ONLY */
818
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) */
827 p = g_malloc(sizeof(TCGPool) + size);
828 p->size = size;
829 p->next = s->pool_first_large;
830 s->pool_first_large = p;
831 return p->data;
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;
842 p = g_malloc(sizeof(TCGPool) + pool_size);
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 }
850 } else {
851 p = p->next;
852 }
853 }
854 }
855 s->pool_current = p;
856 s->pool_cur = p->data + size;
857 s->pool_end = p->data + p->size;
858 return p->data;
859 }
860
861 void tcg_pool_reset(TCGContext *s)
862 {
863 TCGPool *p, *t;
864 for (p = s->pool_first_large; p; p = t) {
865 t = p->next;
866 g_free(p);
867 }
868 s->pool_first_large = NULL;
869 s->pool_cur = s->pool_end = NULL;
870 s->pool_current = NULL;
871 }
872
873 /*
874 * Create TCGHelperInfo structures for "tcg/tcg-ldst.h" functions,
875 * akin to what "exec/helper-tcg.h" does with DEF_HELPER_FLAGS_N.
876 * We only use these for layout in tcg_out_ld_helper_ret and
877 * tcg_out_st_helper_args, and share them between several of
878 * the helpers, with the end result that it's easier to build manually.
879 */
880
881 #if TCG_TARGET_REG_BITS == 32
882 # define dh_typecode_ttl dh_typecode_i32
883 #else
884 # define dh_typecode_ttl dh_typecode_i64
885 #endif
886
887 static TCGHelperInfo info_helper_ld32_mmu = {
888 .flags = TCG_CALL_NO_WG,
889 .typemask = dh_typemask(ttl, 0) /* return tcg_target_ulong */
890 | dh_typemask(env, 1)
891 | dh_typemask(i64, 2) /* uint64_t addr */
892 | dh_typemask(i32, 3) /* unsigned oi */
893 | dh_typemask(ptr, 4) /* uintptr_t ra */
894 };
895
896 static TCGHelperInfo info_helper_ld64_mmu = {
897 .flags = TCG_CALL_NO_WG,
898 .typemask = dh_typemask(i64, 0) /* return uint64_t */
899 | dh_typemask(env, 1)
900 | dh_typemask(i64, 2) /* uint64_t addr */
901 | dh_typemask(i32, 3) /* unsigned oi */
902 | dh_typemask(ptr, 4) /* uintptr_t ra */
903 };
904
905 static TCGHelperInfo info_helper_ld128_mmu = {
906 .flags = TCG_CALL_NO_WG,
907 .typemask = dh_typemask(i128, 0) /* return Int128 */
908 | dh_typemask(env, 1)
909 | dh_typemask(i64, 2) /* uint64_t addr */
910 | dh_typemask(i32, 3) /* unsigned oi */
911 | dh_typemask(ptr, 4) /* uintptr_t ra */
912 };
913
914 static TCGHelperInfo info_helper_st32_mmu = {
915 .flags = TCG_CALL_NO_WG,
916 .typemask = dh_typemask(void, 0)
917 | dh_typemask(env, 1)
918 | dh_typemask(i64, 2) /* uint64_t addr */
919 | dh_typemask(i32, 3) /* uint32_t data */
920 | dh_typemask(i32, 4) /* unsigned oi */
921 | dh_typemask(ptr, 5) /* uintptr_t ra */
922 };
923
924 static TCGHelperInfo info_helper_st64_mmu = {
925 .flags = TCG_CALL_NO_WG,
926 .typemask = dh_typemask(void, 0)
927 | dh_typemask(env, 1)
928 | dh_typemask(i64, 2) /* uint64_t addr */
929 | dh_typemask(i64, 3) /* uint64_t data */
930 | dh_typemask(i32, 4) /* unsigned oi */
931 | dh_typemask(ptr, 5) /* uintptr_t ra */
932 };
933
934 static TCGHelperInfo info_helper_st128_mmu = {
935 .flags = TCG_CALL_NO_WG,
936 .typemask = dh_typemask(void, 0)
937 | dh_typemask(env, 1)
938 | dh_typemask(i64, 2) /* uint64_t addr */
939 | dh_typemask(i128, 3) /* Int128 data */
940 | dh_typemask(i32, 4) /* unsigned oi */
941 | dh_typemask(ptr, 5) /* uintptr_t ra */
942 };
943
944 #ifdef CONFIG_TCG_INTERPRETER
945 static ffi_type *typecode_to_ffi(int argmask)
946 {
947 /*
948 * libffi does not support __int128_t, so we have forced Int128
949 * to use the structure definition instead of the builtin type.
950 */
951 static ffi_type *ffi_type_i128_elements[3] = {
952 &ffi_type_uint64,
953 &ffi_type_uint64,
954 NULL
955 };
956 static ffi_type ffi_type_i128 = {
957 .size = 16,
958 .alignment = __alignof__(Int128),
959 .type = FFI_TYPE_STRUCT,
960 .elements = ffi_type_i128_elements,
961 };
962
963 switch (argmask) {
964 case dh_typecode_void:
965 return &ffi_type_void;
966 case dh_typecode_i32:
967 return &ffi_type_uint32;
968 case dh_typecode_s32:
969 return &ffi_type_sint32;
970 case dh_typecode_i64:
971 return &ffi_type_uint64;
972 case dh_typecode_s64:
973 return &ffi_type_sint64;
974 case dh_typecode_ptr:
975 return &ffi_type_pointer;
976 case dh_typecode_i128:
977 return &ffi_type_i128;
978 }
979 g_assert_not_reached();
980 }
981
982 static ffi_cif *init_ffi_layout(TCGHelperInfo *info)
983 {
984 unsigned typemask = info->typemask;
985 struct {
986 ffi_cif cif;
987 ffi_type *args[];
988 } *ca;
989 ffi_status status;
990 int nargs;
991
992 /* Ignoring the return type, find the last non-zero field. */
993 nargs = 32 - clz32(typemask >> 3);
994 nargs = DIV_ROUND_UP(nargs, 3);
995 assert(nargs <= MAX_CALL_IARGS);
996
997 ca = g_malloc0(sizeof(*ca) + nargs * sizeof(ffi_type *));
998 ca->cif.rtype = typecode_to_ffi(typemask & 7);
999 ca->cif.nargs = nargs;
1000
1001 if (nargs != 0) {
1002 ca->cif.arg_types = ca->args;
1003 for (int j = 0; j < nargs; ++j) {
1004 int typecode = extract32(typemask, (j + 1) * 3, 3);
1005 ca->args[j] = typecode_to_ffi(typecode);
1006 }
1007 }
1008
1009 status = ffi_prep_cif(&ca->cif, FFI_DEFAULT_ABI, nargs,
1010 ca->cif.rtype, ca->cif.arg_types);
1011 assert(status == FFI_OK);
1012
1013 return &ca->cif;
1014 }
1015
1016 #define HELPER_INFO_INIT(I) (&(I)->cif)
1017 #define HELPER_INFO_INIT_VAL(I) init_ffi_layout(I)
1018 #else
1019 #define HELPER_INFO_INIT(I) (&(I)->init)
1020 #define HELPER_INFO_INIT_VAL(I) 1
1021 #endif /* CONFIG_TCG_INTERPRETER */
1022
1023 static inline bool arg_slot_reg_p(unsigned arg_slot)
1024 {
1025 /*
1026 * Split the sizeof away from the comparison to avoid Werror from
1027 * "unsigned < 0 is always false", when iarg_regs is empty.
1028 */
1029 unsigned nreg = ARRAY_SIZE(tcg_target_call_iarg_regs);
1030 return arg_slot < nreg;
1031 }
1032
1033 static inline int arg_slot_stk_ofs(unsigned arg_slot)
1034 {
1035 unsigned max = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long);
1036 unsigned stk_slot = arg_slot - ARRAY_SIZE(tcg_target_call_iarg_regs);
1037
1038 tcg_debug_assert(stk_slot < max);
1039 return TCG_TARGET_CALL_STACK_OFFSET + stk_slot * sizeof(tcg_target_long);
1040 }
1041
1042 typedef struct TCGCumulativeArgs {
1043 int arg_idx; /* tcg_gen_callN args[] */
1044 int info_in_idx; /* TCGHelperInfo in[] */
1045 int arg_slot; /* regs+stack slot */
1046 int ref_slot; /* stack slots for references */
1047 } TCGCumulativeArgs;
1048
1049 static void layout_arg_even(TCGCumulativeArgs *cum)
1050 {
1051 cum->arg_slot += cum->arg_slot & 1;
1052 }
1053
1054 static void layout_arg_1(TCGCumulativeArgs *cum, TCGHelperInfo *info,
1055 TCGCallArgumentKind kind)
1056 {
1057 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1058
1059 *loc = (TCGCallArgumentLoc){
1060 .kind = kind,
1061 .arg_idx = cum->arg_idx,
1062 .arg_slot = cum->arg_slot,
1063 };
1064 cum->info_in_idx++;
1065 cum->arg_slot++;
1066 }
1067
1068 static void layout_arg_normal_n(TCGCumulativeArgs *cum,
1069 TCGHelperInfo *info, int n)
1070 {
1071 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1072
1073 for (int i = 0; i < n; ++i) {
1074 /* Layout all using the same arg_idx, adjusting the subindex. */
1075 loc[i] = (TCGCallArgumentLoc){
1076 .kind = TCG_CALL_ARG_NORMAL,
1077 .arg_idx = cum->arg_idx,
1078 .tmp_subindex = i,
1079 .arg_slot = cum->arg_slot + i,
1080 };
1081 }
1082 cum->info_in_idx += n;
1083 cum->arg_slot += n;
1084 }
1085
1086 static void layout_arg_by_ref(TCGCumulativeArgs *cum, TCGHelperInfo *info)
1087 {
1088 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1089 int n = 128 / TCG_TARGET_REG_BITS;
1090
1091 /* The first subindex carries the pointer. */
1092 layout_arg_1(cum, info, TCG_CALL_ARG_BY_REF);
1093
1094 /*
1095 * The callee is allowed to clobber memory associated with
1096 * structure pass by-reference. Therefore we must make copies.
1097 * Allocate space from "ref_slot", which will be adjusted to
1098 * follow the parameters on the stack.
1099 */
1100 loc[0].ref_slot = cum->ref_slot;
1101
1102 /*
1103 * Subsequent words also go into the reference slot, but
1104 * do not accumulate into the regular arguments.
1105 */
1106 for (int i = 1; i < n; ++i) {
1107 loc[i] = (TCGCallArgumentLoc){
1108 .kind = TCG_CALL_ARG_BY_REF_N,
1109 .arg_idx = cum->arg_idx,
1110 .tmp_subindex = i,
1111 .ref_slot = cum->ref_slot + i,
1112 };
1113 }
1114 cum->info_in_idx += n - 1; /* i=0 accounted for in layout_arg_1 */
1115 cum->ref_slot += n;
1116 }
1117
1118 static void init_call_layout(TCGHelperInfo *info)
1119 {
1120 int max_reg_slots = ARRAY_SIZE(tcg_target_call_iarg_regs);
1121 int max_stk_slots = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long);
1122 unsigned typemask = info->typemask;
1123 unsigned typecode;
1124 TCGCumulativeArgs cum = { };
1125
1126 /*
1127 * Parse and place any function return value.
1128 */
1129 typecode = typemask & 7;
1130 switch (typecode) {
1131 case dh_typecode_void:
1132 info->nr_out = 0;
1133 break;
1134 case dh_typecode_i32:
1135 case dh_typecode_s32:
1136 case dh_typecode_ptr:
1137 info->nr_out = 1;
1138 info->out_kind = TCG_CALL_RET_NORMAL;
1139 break;
1140 case dh_typecode_i64:
1141 case dh_typecode_s64:
1142 info->nr_out = 64 / TCG_TARGET_REG_BITS;
1143 info->out_kind = TCG_CALL_RET_NORMAL;
1144 /* Query the last register now to trigger any assert early. */
1145 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1);
1146 break;
1147 case dh_typecode_i128:
1148 info->nr_out = 128 / TCG_TARGET_REG_BITS;
1149 info->out_kind = TCG_TARGET_CALL_RET_I128;
1150 switch (TCG_TARGET_CALL_RET_I128) {
1151 case TCG_CALL_RET_NORMAL:
1152 /* Query the last register now to trigger any assert early. */
1153 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1);
1154 break;
1155 case TCG_CALL_RET_BY_VEC:
1156 /* Query the single register now to trigger any assert early. */
1157 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0);
1158 break;
1159 case TCG_CALL_RET_BY_REF:
1160 /*
1161 * Allocate the first argument to the output.
1162 * We don't need to store this anywhere, just make it
1163 * unavailable for use in the input loop below.
1164 */
1165 cum.arg_slot = 1;
1166 break;
1167 default:
1168 qemu_build_not_reached();
1169 }
1170 break;
1171 default:
1172 g_assert_not_reached();
1173 }
1174
1175 /*
1176 * Parse and place function arguments.
1177 */
1178 for (typemask >>= 3; typemask; typemask >>= 3, cum.arg_idx++) {
1179 TCGCallArgumentKind kind;
1180 TCGType type;
1181
1182 typecode = typemask & 7;
1183 switch (typecode) {
1184 case dh_typecode_i32:
1185 case dh_typecode_s32:
1186 type = TCG_TYPE_I32;
1187 break;
1188 case dh_typecode_i64:
1189 case dh_typecode_s64:
1190 type = TCG_TYPE_I64;
1191 break;
1192 case dh_typecode_ptr:
1193 type = TCG_TYPE_PTR;
1194 break;
1195 case dh_typecode_i128:
1196 type = TCG_TYPE_I128;
1197 break;
1198 default:
1199 g_assert_not_reached();
1200 }
1201
1202 switch (type) {
1203 case TCG_TYPE_I32:
1204 switch (TCG_TARGET_CALL_ARG_I32) {
1205 case TCG_CALL_ARG_EVEN:
1206 layout_arg_even(&cum);
1207 /* fall through */
1208 case TCG_CALL_ARG_NORMAL:
1209 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL);
1210 break;
1211 case TCG_CALL_ARG_EXTEND:
1212 kind = TCG_CALL_ARG_EXTEND_U + (typecode & 1);
1213 layout_arg_1(&cum, info, kind);
1214 break;
1215 default:
1216 qemu_build_not_reached();
1217 }
1218 break;
1219
1220 case TCG_TYPE_I64:
1221 switch (TCG_TARGET_CALL_ARG_I64) {
1222 case TCG_CALL_ARG_EVEN:
1223 layout_arg_even(&cum);
1224 /* fall through */
1225 case TCG_CALL_ARG_NORMAL:
1226 if (TCG_TARGET_REG_BITS == 32) {
1227 layout_arg_normal_n(&cum, info, 2);
1228 } else {
1229 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL);
1230 }
1231 break;
1232 default:
1233 qemu_build_not_reached();
1234 }
1235 break;
1236
1237 case TCG_TYPE_I128:
1238 switch (TCG_TARGET_CALL_ARG_I128) {
1239 case TCG_CALL_ARG_EVEN:
1240 layout_arg_even(&cum);
1241 /* fall through */
1242 case TCG_CALL_ARG_NORMAL:
1243 layout_arg_normal_n(&cum, info, 128 / TCG_TARGET_REG_BITS);
1244 break;
1245 case TCG_CALL_ARG_BY_REF:
1246 layout_arg_by_ref(&cum, info);
1247 break;
1248 default:
1249 qemu_build_not_reached();
1250 }
1251 break;
1252
1253 default:
1254 g_assert_not_reached();
1255 }
1256 }
1257 info->nr_in = cum.info_in_idx;
1258
1259 /* Validate that we didn't overrun the input array. */
1260 assert(cum.info_in_idx <= ARRAY_SIZE(info->in));
1261 /* Validate the backend has enough argument space. */
1262 assert(cum.arg_slot <= max_reg_slots + max_stk_slots);
1263
1264 /*
1265 * Relocate the "ref_slot" area to the end of the parameters.
1266 * Minimizing this stack offset helps code size for x86,
1267 * which has a signed 8-bit offset encoding.
1268 */
1269 if (cum.ref_slot != 0) {
1270 int ref_base = 0;
1271
1272 if (cum.arg_slot > max_reg_slots) {
1273 int align = __alignof(Int128) / sizeof(tcg_target_long);
1274
1275 ref_base = cum.arg_slot - max_reg_slots;
1276 if (align > 1) {
1277 ref_base = ROUND_UP(ref_base, align);
1278 }
1279 }
1280 assert(ref_base + cum.ref_slot <= max_stk_slots);
1281 ref_base += max_reg_slots;
1282
1283 if (ref_base != 0) {
1284 for (int i = cum.info_in_idx - 1; i >= 0; --i) {
1285 TCGCallArgumentLoc *loc = &info->in[i];
1286 switch (loc->kind) {
1287 case TCG_CALL_ARG_BY_REF:
1288 case TCG_CALL_ARG_BY_REF_N:
1289 loc->ref_slot += ref_base;
1290 break;
1291 default:
1292 break;
1293 }
1294 }
1295 }
1296 }
1297 }
1298
1299 static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
1300 static void process_op_defs(TCGContext *s);
1301 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1302 TCGReg reg, const char *name);
1303
1304 static void tcg_context_init(unsigned max_cpus)
1305 {
1306 TCGContext *s = &tcg_init_ctx;
1307 int op, total_args, n, i;
1308 TCGOpDef *def;
1309 TCGArgConstraint *args_ct;
1310 TCGTemp *ts;
1311
1312 memset(s, 0, sizeof(*s));
1313 s->nb_globals = 0;
1314
1315 /* Count total number of arguments and allocate the corresponding
1316 space */
1317 total_args = 0;
1318 for(op = 0; op < NB_OPS; op++) {
1319 def = &tcg_op_defs[op];
1320 n = def->nb_iargs + def->nb_oargs;
1321 total_args += n;
1322 }
1323
1324 args_ct = g_new0(TCGArgConstraint, total_args);
1325
1326 for(op = 0; op < NB_OPS; op++) {
1327 def = &tcg_op_defs[op];
1328 def->args_ct = args_ct;
1329 n = def->nb_iargs + def->nb_oargs;
1330 args_ct += n;
1331 }
1332
1333 init_call_layout(&info_helper_ld32_mmu);
1334 init_call_layout(&info_helper_ld64_mmu);
1335 init_call_layout(&info_helper_ld128_mmu);
1336 init_call_layout(&info_helper_st32_mmu);
1337 init_call_layout(&info_helper_st64_mmu);
1338 init_call_layout(&info_helper_st128_mmu);
1339
1340 tcg_target_init(s);
1341 process_op_defs(s);
1342
1343 /* Reverse the order of the saved registers, assuming they're all at
1344 the start of tcg_target_reg_alloc_order. */
1345 for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
1346 int r = tcg_target_reg_alloc_order[n];
1347 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
1348 break;
1349 }
1350 }
1351 for (i = 0; i < n; ++i) {
1352 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
1353 }
1354 for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
1355 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
1356 }
1357
1358 alloc_tcg_plugin_context(s);
1359
1360 tcg_ctx = s;
1361 /*
1362 * In user-mode we simply share the init context among threads, since we
1363 * use a single region. See the documentation tcg_region_init() for the
1364 * reasoning behind this.
1365 * In system-mode we will have at most max_cpus TCG threads.
1366 */
1367 #ifdef CONFIG_USER_ONLY
1368 tcg_ctxs = &tcg_ctx;
1369 tcg_cur_ctxs = 1;
1370 tcg_max_ctxs = 1;
1371 #else
1372 tcg_max_ctxs = max_cpus;
1373 tcg_ctxs = g_new0(TCGContext *, max_cpus);
1374 #endif
1375
1376 tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
1377 ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
1378 tcg_env = temp_tcgv_ptr(ts);
1379 }
1380
1381 void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus)
1382 {
1383 tcg_context_init(max_cpus);
1384 tcg_region_init(tb_size, splitwx, max_cpus);
1385 }
1386
1387 /*
1388 * Allocate TBs right before their corresponding translated code, making
1389 * sure that TBs and code are on different cache lines.
1390 */
1391 TranslationBlock *tcg_tb_alloc(TCGContext *s)
1392 {
1393 uintptr_t align = qemu_icache_linesize;
1394 TranslationBlock *tb;
1395 void *next;
1396
1397 retry:
1398 tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
1399 next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
1400
1401 if (unlikely(next > s->code_gen_highwater)) {
1402 if (tcg_region_alloc(s)) {
1403 return NULL;
1404 }
1405 goto retry;
1406 }
1407 qatomic_set(&s->code_gen_ptr, next);
1408 s->data_gen_ptr = NULL;
1409 return tb;
1410 }
1411
1412 void tcg_prologue_init(void)
1413 {
1414 TCGContext *s = tcg_ctx;
1415 size_t prologue_size;
1416
1417 s->code_ptr = s->code_gen_ptr;
1418 s->code_buf = s->code_gen_ptr;
1419 s->data_gen_ptr = NULL;
1420
1421 #ifndef CONFIG_TCG_INTERPRETER
1422 tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx(s->code_ptr);
1423 #endif
1424
1425 #ifdef TCG_TARGET_NEED_POOL_LABELS
1426 s->pool_labels = NULL;
1427 #endif
1428
1429 qemu_thread_jit_write();
1430 /* Generate the prologue. */
1431 tcg_target_qemu_prologue(s);
1432
1433 #ifdef TCG_TARGET_NEED_POOL_LABELS
1434 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1435 {
1436 int result = tcg_out_pool_finalize(s);
1437 tcg_debug_assert(result == 0);
1438 }
1439 #endif
1440
1441 prologue_size = tcg_current_code_size(s);
1442 perf_report_prologue(s->code_gen_ptr, prologue_size);
1443
1444 #ifndef CONFIG_TCG_INTERPRETER
1445 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
1446 (uintptr_t)s->code_buf, prologue_size);
1447 #endif
1448
1449 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
1450 FILE *logfile = qemu_log_trylock();
1451 if (logfile) {
1452 fprintf(logfile, "PROLOGUE: [size=%zu]\n", prologue_size);
1453 if (s->data_gen_ptr) {
1454 size_t code_size = s->data_gen_ptr - s->code_gen_ptr;
1455 size_t data_size = prologue_size - code_size;
1456 size_t i;
1457
1458 disas(logfile, s->code_gen_ptr, code_size);
1459
1460 for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
1461 if (sizeof(tcg_target_ulong) == 8) {
1462 fprintf(logfile,
1463 "0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n",
1464 (uintptr_t)s->data_gen_ptr + i,
1465 *(uint64_t *)(s->data_gen_ptr + i));
1466 } else {
1467 fprintf(logfile,
1468 "0x%08" PRIxPTR ": .long 0x%08x\n",
1469 (uintptr_t)s->data_gen_ptr + i,
1470 *(uint32_t *)(s->data_gen_ptr + i));
1471 }
1472 }
1473 } else {
1474 disas(logfile, s->code_gen_ptr, prologue_size);
1475 }
1476 fprintf(logfile, "\n");
1477 qemu_log_unlock(logfile);
1478 }
1479 }
1480
1481 #ifndef CONFIG_TCG_INTERPRETER
1482 /*
1483 * Assert that goto_ptr is implemented completely, setting an epilogue.
1484 * For tci, we use NULL as the signal to return from the interpreter,
1485 * so skip this check.
1486 */
1487 tcg_debug_assert(tcg_code_gen_epilogue != NULL);
1488 #endif
1489
1490 tcg_region_prologue_set(s);
1491 }
1492
1493 void tcg_func_start(TCGContext *s)
1494 {
1495 tcg_pool_reset(s);
1496 s->nb_temps = s->nb_globals;
1497
1498 /* No temps have been previously allocated for size or locality. */
1499 memset(s->free_temps, 0, sizeof(s->free_temps));
1500
1501 /* No constant temps have been previously allocated. */
1502 for (int i = 0; i < TCG_TYPE_COUNT; ++i) {
1503 if (s->const_table[i]) {
1504 g_hash_table_remove_all(s->const_table[i]);
1505 }
1506 }
1507
1508 s->nb_ops = 0;
1509 s->nb_labels = 0;
1510 s->current_frame_offset = s->frame_start;
1511
1512 #ifdef CONFIG_DEBUG_TCG
1513 s->goto_tb_issue_mask = 0;
1514 #endif
1515
1516 QTAILQ_INIT(&s->ops);
1517 QTAILQ_INIT(&s->free_ops);
1518 QSIMPLEQ_INIT(&s->labels);
1519
1520 tcg_debug_assert(s->addr_type == TCG_TYPE_I32 ||
1521 s->addr_type == TCG_TYPE_I64);
1522
1523 tcg_debug_assert(s->insn_start_words > 0);
1524 }
1525
1526 static TCGTemp *tcg_temp_alloc(TCGContext *s)
1527 {
1528 int n = s->nb_temps++;
1529
1530 if (n >= TCG_MAX_TEMPS) {
1531 tcg_raise_tb_overflow(s);
1532 }
1533 return memset(&s->temps[n], 0, sizeof(TCGTemp));
1534 }
1535
1536 static TCGTemp *tcg_global_alloc(TCGContext *s)
1537 {
1538 TCGTemp *ts;
1539
1540 tcg_debug_assert(s->nb_globals == s->nb_temps);
1541 tcg_debug_assert(s->nb_globals < TCG_MAX_TEMPS);
1542 s->nb_globals++;
1543 ts = tcg_temp_alloc(s);
1544 ts->kind = TEMP_GLOBAL;
1545
1546 return ts;
1547 }
1548
1549 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1550 TCGReg reg, const char *name)
1551 {
1552 TCGTemp *ts;
1553
1554 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
1555
1556 ts = tcg_global_alloc(s);
1557 ts->base_type = type;
1558 ts->type = type;
1559 ts->kind = TEMP_FIXED;
1560 ts->reg = reg;
1561 ts->name = name;
1562 tcg_regset_set_reg(s->reserved_regs, reg);
1563
1564 return ts;
1565 }
1566
1567 void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
1568 {
1569 s->frame_start = start;
1570 s->frame_end = start + size;
1571 s->frame_temp
1572 = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
1573 }
1574
1575 static TCGTemp *tcg_global_mem_new_internal(TCGv_ptr base, intptr_t offset,
1576 const char *name, TCGType type)
1577 {
1578 TCGContext *s = tcg_ctx;
1579 TCGTemp *base_ts = tcgv_ptr_temp(base);
1580 TCGTemp *ts = tcg_global_alloc(s);
1581 int indirect_reg = 0;
1582
1583 switch (base_ts->kind) {
1584 case TEMP_FIXED:
1585 break;
1586 case TEMP_GLOBAL:
1587 /* We do not support double-indirect registers. */
1588 tcg_debug_assert(!base_ts->indirect_reg);
1589 base_ts->indirect_base = 1;
1590 s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64
1591 ? 2 : 1);
1592 indirect_reg = 1;
1593 break;
1594 default:
1595 g_assert_not_reached();
1596 }
1597
1598 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1599 TCGTemp *ts2 = tcg_global_alloc(s);
1600 char buf[64];
1601
1602 ts->base_type = TCG_TYPE_I64;
1603 ts->type = TCG_TYPE_I32;
1604 ts->indirect_reg = indirect_reg;
1605 ts->mem_allocated = 1;
1606 ts->mem_base = base_ts;
1607 ts->mem_offset = offset;
1608 pstrcpy(buf, sizeof(buf), name);
1609 pstrcat(buf, sizeof(buf), "_0");
1610 ts->name = strdup(buf);
1611
1612 tcg_debug_assert(ts2 == ts + 1);
1613 ts2->base_type = TCG_TYPE_I64;
1614 ts2->type = TCG_TYPE_I32;
1615 ts2->indirect_reg = indirect_reg;
1616 ts2->mem_allocated = 1;
1617 ts2->mem_base = base_ts;
1618 ts2->mem_offset = offset + 4;
1619 ts2->temp_subindex = 1;
1620 pstrcpy(buf, sizeof(buf), name);
1621 pstrcat(buf, sizeof(buf), "_1");
1622 ts2->name = strdup(buf);
1623 } else {
1624 ts->base_type = type;
1625 ts->type = type;
1626 ts->indirect_reg = indirect_reg;
1627 ts->mem_allocated = 1;
1628 ts->mem_base = base_ts;
1629 ts->mem_offset = offset;
1630 ts->name = name;
1631 }
1632 return ts;
1633 }
1634
1635 TCGv_i32 tcg_global_mem_new_i32(TCGv_ptr reg, intptr_t off, const char *name)
1636 {
1637 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_I32);
1638 return temp_tcgv_i32(ts);
1639 }
1640
1641 TCGv_i64 tcg_global_mem_new_i64(TCGv_ptr reg, intptr_t off, const char *name)
1642 {
1643 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_I64);
1644 return temp_tcgv_i64(ts);
1645 }
1646
1647 TCGv_ptr tcg_global_mem_new_ptr(TCGv_ptr reg, intptr_t off, const char *name)
1648 {
1649 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_PTR);
1650 return temp_tcgv_ptr(ts);
1651 }
1652
1653 static TCGTemp *tcg_temp_new_internal(TCGType type, TCGTempKind kind)
1654 {
1655 TCGContext *s = tcg_ctx;
1656 TCGTemp *ts;
1657 int n;
1658
1659 if (kind == TEMP_EBB) {
1660 int idx = find_first_bit(s->free_temps[type].l, TCG_MAX_TEMPS);
1661
1662 if (idx < TCG_MAX_TEMPS) {
1663 /* There is already an available temp with the right type. */
1664 clear_bit(idx, s->free_temps[type].l);
1665
1666 ts = &s->temps[idx];
1667 ts->temp_allocated = 1;
1668 tcg_debug_assert(ts->base_type == type);
1669 tcg_debug_assert(ts->kind == kind);
1670 return ts;
1671 }
1672 } else {
1673 tcg_debug_assert(kind == TEMP_TB);
1674 }
1675
1676 switch (type) {
1677 case TCG_TYPE_I32:
1678 case TCG_TYPE_V64:
1679 case TCG_TYPE_V128:
1680 case TCG_TYPE_V256:
1681 n = 1;
1682 break;
1683 case TCG_TYPE_I64:
1684 n = 64 / TCG_TARGET_REG_BITS;
1685 break;
1686 case TCG_TYPE_I128:
1687 n = 128 / TCG_TARGET_REG_BITS;
1688 break;
1689 default:
1690 g_assert_not_reached();
1691 }
1692
1693 ts = tcg_temp_alloc(s);
1694 ts->base_type = type;
1695 ts->temp_allocated = 1;
1696 ts->kind = kind;
1697
1698 if (n == 1) {
1699 ts->type = type;
1700 } else {
1701 ts->type = TCG_TYPE_REG;
1702
1703 for (int i = 1; i < n; ++i) {
1704 TCGTemp *ts2 = tcg_temp_alloc(s);
1705
1706 tcg_debug_assert(ts2 == ts + i);
1707 ts2->base_type = type;
1708 ts2->type = TCG_TYPE_REG;
1709 ts2->temp_allocated = 1;
1710 ts2->temp_subindex = i;
1711 ts2->kind = kind;
1712 }
1713 }
1714 return ts;
1715 }
1716
1717 TCGv_i32 tcg_temp_new_i32(void)
1718 {
1719 return temp_tcgv_i32(tcg_temp_new_internal(TCG_TYPE_I32, TEMP_TB));
1720 }
1721
1722 TCGv_i32 tcg_temp_ebb_new_i32(void)
1723 {
1724 return temp_tcgv_i32(tcg_temp_new_internal(TCG_TYPE_I32, TEMP_EBB));
1725 }
1726
1727 TCGv_i64 tcg_temp_new_i64(void)
1728 {
1729 return temp_tcgv_i64(tcg_temp_new_internal(TCG_TYPE_I64, TEMP_TB));
1730 }
1731
1732 TCGv_i64 tcg_temp_ebb_new_i64(void)
1733 {
1734 return temp_tcgv_i64(tcg_temp_new_internal(TCG_TYPE_I64, TEMP_EBB));
1735 }
1736
1737 TCGv_ptr tcg_temp_new_ptr(void)
1738 {
1739 return temp_tcgv_ptr(tcg_temp_new_internal(TCG_TYPE_PTR, TEMP_TB));
1740 }
1741
1742 TCGv_ptr tcg_temp_ebb_new_ptr(void)
1743 {
1744 return temp_tcgv_ptr(tcg_temp_new_internal(TCG_TYPE_PTR, TEMP_EBB));
1745 }
1746
1747 TCGv_i128 tcg_temp_new_i128(void)
1748 {
1749 return temp_tcgv_i128(tcg_temp_new_internal(TCG_TYPE_I128, TEMP_TB));
1750 }
1751
1752 TCGv_i128 tcg_temp_ebb_new_i128(void)
1753 {
1754 return temp_tcgv_i128(tcg_temp_new_internal(TCG_TYPE_I128, TEMP_EBB));
1755 }
1756
1757 TCGv_vec tcg_temp_new_vec(TCGType type)
1758 {
1759 TCGTemp *t;
1760
1761 #ifdef CONFIG_DEBUG_TCG
1762 switch (type) {
1763 case TCG_TYPE_V64:
1764 assert(TCG_TARGET_HAS_v64);
1765 break;
1766 case TCG_TYPE_V128:
1767 assert(TCG_TARGET_HAS_v128);
1768 break;
1769 case TCG_TYPE_V256:
1770 assert(TCG_TARGET_HAS_v256);
1771 break;
1772 default:
1773 g_assert_not_reached();
1774 }
1775 #endif
1776
1777 t = tcg_temp_new_internal(type, TEMP_EBB);
1778 return temp_tcgv_vec(t);
1779 }
1780
1781 /* Create a new temp of the same type as an existing temp. */
1782 TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match)
1783 {
1784 TCGTemp *t = tcgv_vec_temp(match);
1785
1786 tcg_debug_assert(t->temp_allocated != 0);
1787
1788 t = tcg_temp_new_internal(t->base_type, TEMP_EBB);
1789 return temp_tcgv_vec(t);
1790 }
1791
1792 void tcg_temp_free_internal(TCGTemp *ts)
1793 {
1794 TCGContext *s = tcg_ctx;
1795
1796 switch (ts->kind) {
1797 case TEMP_CONST:
1798 case TEMP_TB:
1799 /* Silently ignore free. */
1800 break;
1801 case TEMP_EBB:
1802 tcg_debug_assert(ts->temp_allocated != 0);
1803 ts->temp_allocated = 0;
1804 set_bit(temp_idx(ts), s->free_temps[ts->base_type].l);
1805 break;
1806 default:
1807 /* It never made sense to free TEMP_FIXED or TEMP_GLOBAL. */
1808 g_assert_not_reached();
1809 }
1810 }
1811
1812 TCGTemp *tcg_constant_internal(TCGType type, int64_t val)
1813 {
1814 TCGContext *s = tcg_ctx;
1815 GHashTable *h = s->const_table[type];
1816 TCGTemp *ts;
1817
1818 if (h == NULL) {
1819 h = g_hash_table_new(g_int64_hash, g_int64_equal);
1820 s->const_table[type] = h;
1821 }
1822
1823 ts = g_hash_table_lookup(h, &val);
1824 if (ts == NULL) {
1825 int64_t *val_ptr;
1826
1827 ts = tcg_temp_alloc(s);
1828
1829 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1830 TCGTemp *ts2 = tcg_temp_alloc(s);
1831
1832 tcg_debug_assert(ts2 == ts + 1);
1833
1834 ts->base_type = TCG_TYPE_I64;
1835 ts->type = TCG_TYPE_I32;
1836 ts->kind = TEMP_CONST;
1837 ts->temp_allocated = 1;
1838
1839 ts2->base_type = TCG_TYPE_I64;
1840 ts2->type = TCG_TYPE_I32;
1841 ts2->kind = TEMP_CONST;
1842 ts2->temp_allocated = 1;
1843 ts2->temp_subindex = 1;
1844
1845 /*
1846 * Retain the full value of the 64-bit constant in the low
1847 * part, so that the hash table works. Actual uses will
1848 * truncate the value to the low part.
1849 */
1850 ts[HOST_BIG_ENDIAN].val = val;
1851 ts[!HOST_BIG_ENDIAN].val = val >> 32;
1852 val_ptr = &ts[HOST_BIG_ENDIAN].val;
1853 } else {
1854 ts->base_type = type;
1855 ts->type = type;
1856 ts->kind = TEMP_CONST;
1857 ts->temp_allocated = 1;
1858 ts->val = val;
1859 val_ptr = &ts->val;
1860 }
1861 g_hash_table_insert(h, val_ptr, ts);
1862 }
1863
1864 return ts;
1865 }
1866
1867 TCGv_i32 tcg_constant_i32(int32_t val)
1868 {
1869 return temp_tcgv_i32(tcg_constant_internal(TCG_TYPE_I32, val));
1870 }
1871
1872 TCGv_i64 tcg_constant_i64(int64_t val)
1873 {
1874 return temp_tcgv_i64(tcg_constant_internal(TCG_TYPE_I64, val));
1875 }
1876
1877 TCGv_ptr tcg_constant_ptr_int(intptr_t val)
1878 {
1879 return temp_tcgv_ptr(tcg_constant_internal(TCG_TYPE_PTR, val));
1880 }
1881
1882 TCGv_vec tcg_constant_vec(TCGType type, unsigned vece, int64_t val)
1883 {
1884 val = dup_const(vece, val);
1885 return temp_tcgv_vec(tcg_constant_internal(type, val));
1886 }
1887
1888 TCGv_vec tcg_constant_vec_matching(TCGv_vec match, unsigned vece, int64_t val)
1889 {
1890 TCGTemp *t = tcgv_vec_temp(match);
1891
1892 tcg_debug_assert(t->temp_allocated != 0);
1893 return tcg_constant_vec(t->base_type, vece, val);
1894 }
1895
1896 #ifdef CONFIG_DEBUG_TCG
1897 size_t temp_idx(TCGTemp *ts)
1898 {
1899 ptrdiff_t n = ts - tcg_ctx->temps;
1900 assert(n >= 0 && n < tcg_ctx->nb_temps);
1901 return n;
1902 }
1903
1904 TCGTemp *tcgv_i32_temp(TCGv_i32 v)
1905 {
1906 uintptr_t o = (uintptr_t)v - offsetof(TCGContext, temps);
1907
1908 assert(o < sizeof(TCGTemp) * tcg_ctx->nb_temps);
1909 assert(o % sizeof(TCGTemp) == 0);
1910
1911 return (void *)tcg_ctx + (uintptr_t)v;
1912 }
1913 #endif /* CONFIG_DEBUG_TCG */
1914
1915 /* Return true if OP may appear in the opcode stream.
1916 Test the runtime variable that controls each opcode. */
1917 bool tcg_op_supported(TCGOpcode op)
1918 {
1919 const bool have_vec
1920 = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
1921
1922 switch (op) {
1923 case INDEX_op_discard:
1924 case INDEX_op_set_label:
1925 case INDEX_op_call:
1926 case INDEX_op_br:
1927 case INDEX_op_mb:
1928 case INDEX_op_insn_start:
1929 case INDEX_op_exit_tb:
1930 case INDEX_op_goto_tb:
1931 case INDEX_op_goto_ptr:
1932 case INDEX_op_qemu_ld_a32_i32:
1933 case INDEX_op_qemu_ld_a64_i32:
1934 case INDEX_op_qemu_st_a32_i32:
1935 case INDEX_op_qemu_st_a64_i32:
1936 case INDEX_op_qemu_ld_a32_i64:
1937 case INDEX_op_qemu_ld_a64_i64:
1938 case INDEX_op_qemu_st_a32_i64:
1939 case INDEX_op_qemu_st_a64_i64:
1940 return true;
1941
1942 case INDEX_op_qemu_st8_a32_i32:
1943 case INDEX_op_qemu_st8_a64_i32:
1944 return TCG_TARGET_HAS_qemu_st8_i32;
1945
1946 case INDEX_op_qemu_ld_a32_i128:
1947 case INDEX_op_qemu_ld_a64_i128:
1948 case INDEX_op_qemu_st_a32_i128:
1949 case INDEX_op_qemu_st_a64_i128:
1950 return TCG_TARGET_HAS_qemu_ldst_i128;
1951
1952 case INDEX_op_mov_i32:
1953 case INDEX_op_setcond_i32:
1954 case INDEX_op_brcond_i32:
1955 case INDEX_op_ld8u_i32:
1956 case INDEX_op_ld8s_i32:
1957 case INDEX_op_ld16u_i32:
1958 case INDEX_op_ld16s_i32:
1959 case INDEX_op_ld_i32:
1960 case INDEX_op_st8_i32:
1961 case INDEX_op_st16_i32:
1962 case INDEX_op_st_i32:
1963 case INDEX_op_add_i32:
1964 case INDEX_op_sub_i32:
1965 case INDEX_op_mul_i32:
1966 case INDEX_op_and_i32:
1967 case INDEX_op_or_i32:
1968 case INDEX_op_xor_i32:
1969 case INDEX_op_shl_i32:
1970 case INDEX_op_shr_i32:
1971 case INDEX_op_sar_i32:
1972 return true;
1973
1974 case INDEX_op_negsetcond_i32:
1975 return TCG_TARGET_HAS_negsetcond_i32;
1976 case INDEX_op_movcond_i32:
1977 return TCG_TARGET_HAS_movcond_i32;
1978 case INDEX_op_div_i32:
1979 case INDEX_op_divu_i32:
1980 return TCG_TARGET_HAS_div_i32;
1981 case INDEX_op_rem_i32:
1982 case INDEX_op_remu_i32:
1983 return TCG_TARGET_HAS_rem_i32;
1984 case INDEX_op_div2_i32:
1985 case INDEX_op_divu2_i32:
1986 return TCG_TARGET_HAS_div2_i32;
1987 case INDEX_op_rotl_i32:
1988 case INDEX_op_rotr_i32:
1989 return TCG_TARGET_HAS_rot_i32;
1990 case INDEX_op_deposit_i32:
1991 return TCG_TARGET_HAS_deposit_i32;
1992 case INDEX_op_extract_i32:
1993 return TCG_TARGET_HAS_extract_i32;
1994 case INDEX_op_sextract_i32:
1995 return TCG_TARGET_HAS_sextract_i32;
1996 case INDEX_op_extract2_i32:
1997 return TCG_TARGET_HAS_extract2_i32;
1998 case INDEX_op_add2_i32:
1999 return TCG_TARGET_HAS_add2_i32;
2000 case INDEX_op_sub2_i32:
2001 return TCG_TARGET_HAS_sub2_i32;
2002 case INDEX_op_mulu2_i32:
2003 return TCG_TARGET_HAS_mulu2_i32;
2004 case INDEX_op_muls2_i32:
2005 return TCG_TARGET_HAS_muls2_i32;
2006 case INDEX_op_muluh_i32:
2007 return TCG_TARGET_HAS_muluh_i32;
2008 case INDEX_op_mulsh_i32:
2009 return TCG_TARGET_HAS_mulsh_i32;
2010 case INDEX_op_ext8s_i32:
2011 return TCG_TARGET_HAS_ext8s_i32;
2012 case INDEX_op_ext16s_i32:
2013 return TCG_TARGET_HAS_ext16s_i32;
2014 case INDEX_op_ext8u_i32:
2015 return TCG_TARGET_HAS_ext8u_i32;
2016 case INDEX_op_ext16u_i32:
2017 return TCG_TARGET_HAS_ext16u_i32;
2018 case INDEX_op_bswap16_i32:
2019 return TCG_TARGET_HAS_bswap16_i32;
2020 case INDEX_op_bswap32_i32:
2021 return TCG_TARGET_HAS_bswap32_i32;
2022 case INDEX_op_not_i32:
2023 return TCG_TARGET_HAS_not_i32;
2024 case INDEX_op_neg_i32:
2025 return TCG_TARGET_HAS_neg_i32;
2026 case INDEX_op_andc_i32:
2027 return TCG_TARGET_HAS_andc_i32;
2028 case INDEX_op_orc_i32:
2029 return TCG_TARGET_HAS_orc_i32;
2030 case INDEX_op_eqv_i32:
2031 return TCG_TARGET_HAS_eqv_i32;
2032 case INDEX_op_nand_i32:
2033 return TCG_TARGET_HAS_nand_i32;
2034 case INDEX_op_nor_i32:
2035 return TCG_TARGET_HAS_nor_i32;
2036 case INDEX_op_clz_i32:
2037 return TCG_TARGET_HAS_clz_i32;
2038 case INDEX_op_ctz_i32:
2039 return TCG_TARGET_HAS_ctz_i32;
2040 case INDEX_op_ctpop_i32:
2041 return TCG_TARGET_HAS_ctpop_i32;
2042
2043 case INDEX_op_brcond2_i32:
2044 case INDEX_op_setcond2_i32:
2045 return TCG_TARGET_REG_BITS == 32;
2046
2047 case INDEX_op_mov_i64:
2048 case INDEX_op_setcond_i64:
2049 case INDEX_op_brcond_i64:
2050 case INDEX_op_ld8u_i64:
2051 case INDEX_op_ld8s_i64:
2052 case INDEX_op_ld16u_i64:
2053 case INDEX_op_ld16s_i64:
2054 case INDEX_op_ld32u_i64:
2055 case INDEX_op_ld32s_i64:
2056 case INDEX_op_ld_i64:
2057 case INDEX_op_st8_i64:
2058 case INDEX_op_st16_i64:
2059 case INDEX_op_st32_i64:
2060 case INDEX_op_st_i64:
2061 case INDEX_op_add_i64:
2062 case INDEX_op_sub_i64:
2063 case INDEX_op_mul_i64:
2064 case INDEX_op_and_i64:
2065 case INDEX_op_or_i64:
2066 case INDEX_op_xor_i64:
2067 case INDEX_op_shl_i64:
2068 case INDEX_op_shr_i64:
2069 case INDEX_op_sar_i64:
2070 case INDEX_op_ext_i32_i64:
2071 case INDEX_op_extu_i32_i64:
2072 return TCG_TARGET_REG_BITS == 64;
2073
2074 case INDEX_op_negsetcond_i64:
2075 return TCG_TARGET_HAS_negsetcond_i64;
2076 case INDEX_op_movcond_i64:
2077 return TCG_TARGET_HAS_movcond_i64;
2078 case INDEX_op_div_i64:
2079 case INDEX_op_divu_i64:
2080 return TCG_TARGET_HAS_div_i64;
2081 case INDEX_op_rem_i64:
2082 case INDEX_op_remu_i64:
2083 return TCG_TARGET_HAS_rem_i64;
2084 case INDEX_op_div2_i64:
2085 case INDEX_op_divu2_i64:
2086 return TCG_TARGET_HAS_div2_i64;
2087 case INDEX_op_rotl_i64:
2088 case INDEX_op_rotr_i64:
2089 return TCG_TARGET_HAS_rot_i64;
2090 case INDEX_op_deposit_i64:
2091 return TCG_TARGET_HAS_deposit_i64;
2092 case INDEX_op_extract_i64:
2093 return TCG_TARGET_HAS_extract_i64;
2094 case INDEX_op_sextract_i64:
2095 return TCG_TARGET_HAS_sextract_i64;
2096 case INDEX_op_extract2_i64:
2097 return TCG_TARGET_HAS_extract2_i64;
2098 case INDEX_op_extrl_i64_i32:
2099 case INDEX_op_extrh_i64_i32:
2100 return TCG_TARGET_HAS_extr_i64_i32;
2101 case INDEX_op_ext8s_i64:
2102 return TCG_TARGET_HAS_ext8s_i64;
2103 case INDEX_op_ext16s_i64:
2104 return TCG_TARGET_HAS_ext16s_i64;
2105 case INDEX_op_ext32s_i64:
2106 return TCG_TARGET_HAS_ext32s_i64;
2107 case INDEX_op_ext8u_i64:
2108 return TCG_TARGET_HAS_ext8u_i64;
2109 case INDEX_op_ext16u_i64:
2110 return TCG_TARGET_HAS_ext16u_i64;
2111 case INDEX_op_ext32u_i64:
2112 return TCG_TARGET_HAS_ext32u_i64;
2113 case INDEX_op_bswap16_i64:
2114 return TCG_TARGET_HAS_bswap16_i64;
2115 case INDEX_op_bswap32_i64:
2116 return TCG_TARGET_HAS_bswap32_i64;
2117 case INDEX_op_bswap64_i64:
2118 return TCG_TARGET_HAS_bswap64_i64;
2119 case INDEX_op_not_i64:
2120 return TCG_TARGET_HAS_not_i64;
2121 case INDEX_op_neg_i64:
2122 return TCG_TARGET_HAS_neg_i64;
2123 case INDEX_op_andc_i64:
2124 return TCG_TARGET_HAS_andc_i64;
2125 case INDEX_op_orc_i64:
2126 return TCG_TARGET_HAS_orc_i64;
2127 case INDEX_op_eqv_i64:
2128 return TCG_TARGET_HAS_eqv_i64;
2129 case INDEX_op_nand_i64:
2130 return TCG_TARGET_HAS_nand_i64;
2131 case INDEX_op_nor_i64:
2132 return TCG_TARGET_HAS_nor_i64;
2133 case INDEX_op_clz_i64:
2134 return TCG_TARGET_HAS_clz_i64;
2135 case INDEX_op_ctz_i64:
2136 return TCG_TARGET_HAS_ctz_i64;
2137 case INDEX_op_ctpop_i64:
2138 return TCG_TARGET_HAS_ctpop_i64;
2139 case INDEX_op_add2_i64:
2140 return TCG_TARGET_HAS_add2_i64;
2141 case INDEX_op_sub2_i64:
2142 return TCG_TARGET_HAS_sub2_i64;
2143 case INDEX_op_mulu2_i64:
2144 return TCG_TARGET_HAS_mulu2_i64;
2145 case INDEX_op_muls2_i64:
2146 return TCG_TARGET_HAS_muls2_i64;
2147 case INDEX_op_muluh_i64:
2148 return TCG_TARGET_HAS_muluh_i64;
2149 case INDEX_op_mulsh_i64:
2150 return TCG_TARGET_HAS_mulsh_i64;
2151
2152 case INDEX_op_mov_vec:
2153 case INDEX_op_dup_vec:
2154 case INDEX_op_dupm_vec:
2155 case INDEX_op_ld_vec:
2156 case INDEX_op_st_vec:
2157 case INDEX_op_add_vec:
2158 case INDEX_op_sub_vec:
2159 case INDEX_op_and_vec:
2160 case INDEX_op_or_vec:
2161 case INDEX_op_xor_vec:
2162 case INDEX_op_cmp_vec:
2163 return have_vec;
2164 case INDEX_op_dup2_vec:
2165 return have_vec && TCG_TARGET_REG_BITS == 32;
2166 case INDEX_op_not_vec:
2167 return have_vec && TCG_TARGET_HAS_not_vec;
2168 case INDEX_op_neg_vec:
2169 return have_vec && TCG_TARGET_HAS_neg_vec;
2170 case INDEX_op_abs_vec:
2171 return have_vec && TCG_TARGET_HAS_abs_vec;
2172 case INDEX_op_andc_vec:
2173 return have_vec && TCG_TARGET_HAS_andc_vec;
2174 case INDEX_op_orc_vec:
2175 return have_vec && TCG_TARGET_HAS_orc_vec;
2176 case INDEX_op_nand_vec:
2177 return have_vec && TCG_TARGET_HAS_nand_vec;
2178 case INDEX_op_nor_vec:
2179 return have_vec && TCG_TARGET_HAS_nor_vec;
2180 case INDEX_op_eqv_vec:
2181 return have_vec && TCG_TARGET_HAS_eqv_vec;
2182 case INDEX_op_mul_vec:
2183 return have_vec && TCG_TARGET_HAS_mul_vec;
2184 case INDEX_op_shli_vec:
2185 case INDEX_op_shri_vec:
2186 case INDEX_op_sari_vec:
2187 return have_vec && TCG_TARGET_HAS_shi_vec;
2188 case INDEX_op_shls_vec:
2189 case INDEX_op_shrs_vec:
2190 case INDEX_op_sars_vec:
2191 return have_vec && TCG_TARGET_HAS_shs_vec;
2192 case INDEX_op_shlv_vec:
2193 case INDEX_op_shrv_vec:
2194 case INDEX_op_sarv_vec:
2195 return have_vec && TCG_TARGET_HAS_shv_vec;
2196 case INDEX_op_rotli_vec:
2197 return have_vec && TCG_TARGET_HAS_roti_vec;
2198 case INDEX_op_rotls_vec:
2199 return have_vec && TCG_TARGET_HAS_rots_vec;
2200 case INDEX_op_rotlv_vec:
2201 case INDEX_op_rotrv_vec:
2202 return have_vec && TCG_TARGET_HAS_rotv_vec;
2203 case INDEX_op_ssadd_vec:
2204 case INDEX_op_usadd_vec:
2205 case INDEX_op_sssub_vec:
2206 case INDEX_op_ussub_vec:
2207 return have_vec && TCG_TARGET_HAS_sat_vec;
2208 case INDEX_op_smin_vec:
2209 case INDEX_op_umin_vec:
2210 case INDEX_op_smax_vec:
2211 case INDEX_op_umax_vec:
2212 return have_vec && TCG_TARGET_HAS_minmax_vec;
2213 case INDEX_op_bitsel_vec:
2214 return have_vec && TCG_TARGET_HAS_bitsel_vec;
2215 case INDEX_op_cmpsel_vec:
2216 return have_vec && TCG_TARGET_HAS_cmpsel_vec;
2217
2218 default:
2219 tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
2220 return true;
2221 }
2222 }
2223
2224 static TCGOp *tcg_op_alloc(TCGOpcode opc, unsigned nargs);
2225
2226 static void tcg_gen_callN(TCGHelperInfo *info, TCGTemp *ret, TCGTemp **args)
2227 {
2228 TCGv_i64 extend_free[MAX_CALL_IARGS];
2229 int n_extend = 0;
2230 TCGOp *op;
2231 int i, n, pi = 0, total_args;
2232
2233 if (unlikely(g_once_init_enter(HELPER_INFO_INIT(info)))) {
2234 init_call_layout(info);
2235 g_once_init_leave(HELPER_INFO_INIT(info), HELPER_INFO_INIT_VAL(info));
2236 }
2237
2238 total_args = info->nr_out + info->nr_in + 2;
2239 op = tcg_op_alloc(INDEX_op_call, total_args);
2240
2241 #ifdef CONFIG_PLUGIN
2242 /* Flag helpers that may affect guest state */
2243 if (tcg_ctx->plugin_insn &&
2244 !(info->flags & TCG_CALL_PLUGIN) &&
2245 !(info->flags & TCG_CALL_NO_SIDE_EFFECTS)) {
2246 tcg_ctx->plugin_insn->calls_helpers = true;
2247 }
2248 #endif
2249
2250 TCGOP_CALLO(op) = n = info->nr_out;
2251 switch (n) {
2252 case 0:
2253 tcg_debug_assert(ret == NULL);
2254 break;
2255 case 1:
2256 tcg_debug_assert(ret != NULL);
2257 op->args[pi++] = temp_arg(ret);
2258 break;
2259 case 2:
2260 case 4:
2261 tcg_debug_assert(ret != NULL);
2262 tcg_debug_assert(ret->base_type == ret->type + ctz32(n));
2263 tcg_debug_assert(ret->temp_subindex == 0);
2264 for (i = 0; i < n; ++i) {
2265 op->args[pi++] = temp_arg(ret + i);
2266 }
2267 break;
2268 default:
2269 g_assert_not_reached();
2270 }
2271
2272 TCGOP_CALLI(op) = n = info->nr_in;
2273 for (i = 0; i < n; i++) {
2274 const TCGCallArgumentLoc *loc = &info->in[i];
2275 TCGTemp *ts = args[loc->arg_idx] + loc->tmp_subindex;
2276
2277 switch (loc->kind) {
2278 case TCG_CALL_ARG_NORMAL:
2279 case TCG_CALL_ARG_BY_REF:
2280 case TCG_CALL_ARG_BY_REF_N:
2281 op->args[pi++] = temp_arg(ts);
2282 break;
2283
2284 case TCG_CALL_ARG_EXTEND_U:
2285 case TCG_CALL_ARG_EXTEND_S:
2286 {
2287 TCGv_i64 temp = tcg_temp_ebb_new_i64();
2288 TCGv_i32 orig = temp_tcgv_i32(ts);
2289
2290 if (loc->kind == TCG_CALL_ARG_EXTEND_S) {
2291 tcg_gen_ext_i32_i64(temp, orig);
2292 } else {
2293 tcg_gen_extu_i32_i64(temp, orig);
2294 }
2295 op->args[pi++] = tcgv_i64_arg(temp);
2296 extend_free[n_extend++] = temp;
2297 }
2298 break;
2299
2300 default:
2301 g_assert_not_reached();
2302 }
2303 }
2304 op->args[pi++] = (uintptr_t)info->func;
2305 op->args[pi++] = (uintptr_t)info;
2306 tcg_debug_assert(pi == total_args);
2307
2308 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
2309
2310 tcg_debug_assert(n_extend < ARRAY_SIZE(extend_free));
2311 for (i = 0; i < n_extend; ++i) {
2312 tcg_temp_free_i64(extend_free[i]);
2313 }
2314 }
2315
2316 void tcg_gen_call0(TCGHelperInfo *info, TCGTemp *ret)
2317 {
2318 tcg_gen_callN(info, ret, NULL);
2319 }
2320
2321 void tcg_gen_call1(TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1)
2322 {
2323 tcg_gen_callN(info, ret, &t1);
2324 }
2325
2326 void tcg_gen_call2(TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1, TCGTemp *t2)
2327 {
2328 TCGTemp *args[2] = { t1, t2 };
2329 tcg_gen_callN(info, ret, args);
2330 }
2331
2332 void tcg_gen_call3(TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1,
2333 TCGTemp *t2, TCGTemp *t3)
2334 {
2335 TCGTemp *args[3] = { t1, t2, t3 };
2336 tcg_gen_callN(info, ret, args);
2337 }
2338
2339 void tcg_gen_call4(TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1,
2340 TCGTemp *t2, TCGTemp *t3, TCGTemp *t4)
2341 {
2342 TCGTemp *args[4] = { t1, t2, t3, t4 };
2343 tcg_gen_callN(info, ret, args);
2344 }
2345
2346 void tcg_gen_call5(TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1,
2347 TCGTemp *t2, TCGTemp *t3, TCGTemp *t4, TCGTemp *t5)
2348 {
2349 TCGTemp *args[5] = { t1, t2, t3, t4, t5 };
2350 tcg_gen_callN(info, ret, args);
2351 }
2352
2353 void tcg_gen_call6(TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1, TCGTemp *t2,
2354 TCGTemp *t3, TCGTemp *t4, TCGTemp *t5, TCGTemp *t6)
2355 {
2356 TCGTemp *args[6] = { t1, t2, t3, t4, t5, t6 };
2357 tcg_gen_callN(info, ret, args);
2358 }
2359
2360 void tcg_gen_call7(TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1,
2361 TCGTemp *t2, TCGTemp *t3, TCGTemp *t4,
2362 TCGTemp *t5, TCGTemp *t6, TCGTemp *t7)
2363 {
2364 TCGTemp *args[7] = { t1, t2, t3, t4, t5, t6, t7 };
2365 tcg_gen_callN(info, ret, args);
2366 }
2367
2368 static void tcg_reg_alloc_start(TCGContext *s)
2369 {
2370 int i, n;
2371
2372 for (i = 0, n = s->nb_temps; i < n; i++) {
2373 TCGTemp *ts = &s->temps[i];
2374 TCGTempVal val = TEMP_VAL_MEM;
2375
2376 switch (ts->kind) {
2377 case TEMP_CONST:
2378 val = TEMP_VAL_CONST;
2379 break;
2380 case TEMP_FIXED:
2381 val = TEMP_VAL_REG;
2382 break;
2383 case TEMP_GLOBAL:
2384 break;
2385 case TEMP_EBB:
2386 val = TEMP_VAL_DEAD;
2387 /* fall through */
2388 case TEMP_TB:
2389 ts->mem_allocated = 0;
2390 break;
2391 default:
2392 g_assert_not_reached();
2393 }
2394 ts->val_type = val;
2395 }
2396
2397 memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
2398 }
2399
2400 static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
2401 TCGTemp *ts)
2402 {
2403 int idx = temp_idx(ts);
2404
2405 switch (ts->kind) {
2406 case TEMP_FIXED:
2407 case TEMP_GLOBAL:
2408 pstrcpy(buf, buf_size, ts->name);
2409 break;
2410 case TEMP_TB:
2411 snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
2412 break;
2413 case TEMP_EBB:
2414 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
2415 break;
2416 case TEMP_CONST:
2417 switch (ts->type) {
2418 case TCG_TYPE_I32:
2419 snprintf(buf, buf_size, "$0x%x", (int32_t)ts->val);
2420 break;
2421 #if TCG_TARGET_REG_BITS > 32
2422 case TCG_TYPE_I64:
2423 snprintf(buf, buf_size, "$0x%" PRIx64, ts->val);
2424 break;
2425 #endif
2426 case TCG_TYPE_V64:
2427 case TCG_TYPE_V128:
2428 case TCG_TYPE_V256:
2429 snprintf(buf, buf_size, "v%d$0x%" PRIx64,
2430 64 << (ts->type - TCG_TYPE_V64), ts->val);
2431 break;
2432 default:
2433 g_assert_not_reached();
2434 }
2435 break;
2436 }
2437 return buf;
2438 }
2439
2440 static char *tcg_get_arg_str(TCGContext *s, char *buf,
2441 int buf_size, TCGArg arg)
2442 {
2443 return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg));
2444 }
2445
2446 static const char * const cond_name[] =
2447 {
2448 [TCG_COND_NEVER] = "never",
2449 [TCG_COND_ALWAYS] = "always",
2450 [TCG_COND_EQ] = "eq",
2451 [TCG_COND_NE] = "ne",
2452 [TCG_COND_LT] = "lt",
2453 [TCG_COND_GE] = "ge",
2454 [TCG_COND_LE] = "le",
2455 [TCG_COND_GT] = "gt",
2456 [TCG_COND_LTU] = "ltu",
2457 [TCG_COND_GEU] = "geu",
2458 [TCG_COND_LEU] = "leu",
2459 [TCG_COND_GTU] = "gtu"
2460 };
2461
2462 static const char * const ldst_name[(MO_BSWAP | MO_SSIZE) + 1] =
2463 {
2464 [MO_UB] = "ub",
2465 [MO_SB] = "sb",
2466 [MO_LEUW] = "leuw",
2467 [MO_LESW] = "lesw",
2468 [MO_LEUL] = "leul",
2469 [MO_LESL] = "lesl",
2470 [MO_LEUQ] = "leq",
2471 [MO_BEUW] = "beuw",
2472 [MO_BESW] = "besw",
2473 [MO_BEUL] = "beul",
2474 [MO_BESL] = "besl",
2475 [MO_BEUQ] = "beq",
2476 [MO_128 + MO_BE] = "beo",
2477 [MO_128 + MO_LE] = "leo",
2478 };
2479
2480 static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = {
2481 [MO_UNALN >> MO_ASHIFT] = "un+",
2482 [MO_ALIGN >> MO_ASHIFT] = "al+",
2483 [MO_ALIGN_2 >> MO_ASHIFT] = "al2+",
2484 [MO_ALIGN_4 >> MO_ASHIFT] = "al4+",
2485 [MO_ALIGN_8 >> MO_ASHIFT] = "al8+",
2486 [MO_ALIGN_16 >> MO_ASHIFT] = "al16+",
2487 [MO_ALIGN_32 >> MO_ASHIFT] = "al32+",
2488 [MO_ALIGN_64 >> MO_ASHIFT] = "al64+",
2489 };
2490
2491 static const char * const atom_name[(MO_ATOM_MASK >> MO_ATOM_SHIFT) + 1] = {
2492 [MO_ATOM_IFALIGN >> MO_ATOM_SHIFT] = "",
2493 [MO_ATOM_IFALIGN_PAIR >> MO_ATOM_SHIFT] = "pair+",
2494 [MO_ATOM_WITHIN16 >> MO_ATOM_SHIFT] = "w16+",
2495 [MO_ATOM_WITHIN16_PAIR >> MO_ATOM_SHIFT] = "w16p+",
2496 [MO_ATOM_SUBALIGN >> MO_ATOM_SHIFT] = "sub+",
2497 [MO_ATOM_NONE >> MO_ATOM_SHIFT] = "noat+",
2498 };
2499
2500 static const char bswap_flag_name[][6] = {
2501 [TCG_BSWAP_IZ] = "iz",
2502 [TCG_BSWAP_OZ] = "oz",
2503 [TCG_BSWAP_OS] = "os",
2504 [TCG_BSWAP_IZ | TCG_BSWAP_OZ] = "iz,oz",
2505 [TCG_BSWAP_IZ | TCG_BSWAP_OS] = "iz,os",
2506 };
2507
2508 static inline bool tcg_regset_single(TCGRegSet d)
2509 {
2510 return (d & (d - 1)) == 0;
2511 }
2512
2513 static inline TCGReg tcg_regset_first(TCGRegSet d)
2514 {
2515 if (TCG_TARGET_NB_REGS <= 32) {
2516 return ctz32(d);
2517 } else {
2518 return ctz64(d);
2519 }
2520 }
2521
2522 /* Return only the number of characters output -- no error return. */
2523 #define ne_fprintf(...) \
2524 ({ int ret_ = fprintf(__VA_ARGS__); ret_ >= 0 ? ret_ : 0; })
2525
2526 static void tcg_dump_ops(TCGContext *s, FILE *f, bool have_prefs)
2527 {
2528 char buf[128];
2529 TCGOp *op;
2530
2531 QTAILQ_FOREACH(op, &s->ops, link) {
2532 int i, k, nb_oargs, nb_iargs, nb_cargs;
2533 const TCGOpDef *def;
2534 TCGOpcode c;
2535 int col = 0;
2536
2537 c = op->opc;
2538 def = &tcg_op_defs[c];
2539
2540 if (c == INDEX_op_insn_start) {
2541 nb_oargs = 0;
2542 col += ne_fprintf(f, "\n ----");
2543
2544 for (i = 0, k = s->insn_start_words; i < k; ++i) {
2545 col += ne_fprintf(f, " %016" PRIx64,
2546 tcg_get_insn_start_param(op, i));
2547 }
2548 } else if (c == INDEX_op_call) {
2549 const TCGHelperInfo *info = tcg_call_info(op);
2550 void *func = tcg_call_func(op);
2551
2552 /* variable number of arguments */
2553 nb_oargs = TCGOP_CALLO(op);
2554 nb_iargs = TCGOP_CALLI(op);
2555 nb_cargs = def->nb_cargs;
2556
2557 col += ne_fprintf(f, " %s ", def->name);
2558
2559 /*
2560 * Print the function name from TCGHelperInfo, if available.
2561 * Note that plugins have a template function for the info,
2562 * but the actual function pointer comes from the plugin.
2563 */
2564 if (func == info->func) {
2565 col += ne_fprintf(f, "%s", info->name);
2566 } else {
2567 col += ne_fprintf(f, "plugin(%p)", func);
2568 }
2569
2570 col += ne_fprintf(f, ",$0x%x,$%d", info->flags, nb_oargs);
2571 for (i = 0; i < nb_oargs; i++) {
2572 col += ne_fprintf(f, ",%s", tcg_get_arg_str(s, buf, sizeof(buf),
2573 op->args[i]));
2574 }
2575 for (i = 0; i < nb_iargs; i++) {
2576 TCGArg arg = op->args[nb_oargs + i];
2577 const char *t = tcg_get_arg_str(s, buf, sizeof(buf), arg);
2578 col += ne_fprintf(f, ",%s", t);
2579 }
2580 } else {
2581 col += ne_fprintf(f, " %s ", def->name);
2582
2583 nb_oargs = def->nb_oargs;
2584 nb_iargs = def->nb_iargs;
2585 nb_cargs = def->nb_cargs;
2586
2587 if (def->flags & TCG_OPF_VECTOR) {
2588 col += ne_fprintf(f, "v%d,e%d,", 64 << TCGOP_VECL(op),
2589 8 << TCGOP_VECE(op));
2590 }
2591
2592 k = 0;
2593 for (i = 0; i < nb_oargs; i++) {
2594 const char *sep = k ? "," : "";
2595 col += ne_fprintf(f, "%s%s", sep,
2596 tcg_get_arg_str(s, buf, sizeof(buf),
2597 op->args[k++]));
2598 }
2599 for (i = 0; i < nb_iargs; i++) {
2600 const char *sep = k ? "," : "";
2601 col += ne_fprintf(f, "%s%s", sep,
2602 tcg_get_arg_str(s, buf, sizeof(buf),
2603 op->args[k++]));
2604 }
2605 switch (c) {
2606 case INDEX_op_brcond_i32:
2607 case INDEX_op_setcond_i32:
2608 case INDEX_op_negsetcond_i32:
2609 case INDEX_op_movcond_i32:
2610 case INDEX_op_brcond2_i32:
2611 case INDEX_op_setcond2_i32:
2612 case INDEX_op_brcond_i64:
2613 case INDEX_op_setcond_i64:
2614 case INDEX_op_negsetcond_i64:
2615 case INDEX_op_movcond_i64:
2616 case INDEX_op_cmp_vec:
2617 case INDEX_op_cmpsel_vec:
2618 if (op->args[k] < ARRAY_SIZE(cond_name)
2619 && cond_name[op->args[k]]) {
2620 col += ne_fprintf(f, ",%s", cond_name[op->args[k++]]);
2621 } else {
2622 col += ne_fprintf(f, ",$0x%" TCG_PRIlx, op->args[k++]);
2623 }
2624 i = 1;
2625 break;
2626 case INDEX_op_qemu_ld_a32_i32:
2627 case INDEX_op_qemu_ld_a64_i32:
2628 case INDEX_op_qemu_st_a32_i32:
2629 case INDEX_op_qemu_st_a64_i32:
2630 case INDEX_op_qemu_st8_a32_i32:
2631 case INDEX_op_qemu_st8_a64_i32:
2632 case INDEX_op_qemu_ld_a32_i64:
2633 case INDEX_op_qemu_ld_a64_i64:
2634 case INDEX_op_qemu_st_a32_i64:
2635 case INDEX_op_qemu_st_a64_i64:
2636 case INDEX_op_qemu_ld_a32_i128:
2637 case INDEX_op_qemu_ld_a64_i128:
2638 case INDEX_op_qemu_st_a32_i128:
2639 case INDEX_op_qemu_st_a64_i128:
2640 {
2641 const char *s_al, *s_op, *s_at;
2642 MemOpIdx oi = op->args[k++];
2643 MemOp mop = get_memop(oi);
2644 unsigned ix = get_mmuidx(oi);
2645
2646 s_al = alignment_name[(mop & MO_AMASK) >> MO_ASHIFT];
2647 s_op = ldst_name[mop & (MO_BSWAP | MO_SSIZE)];
2648 s_at = atom_name[(mop & MO_ATOM_MASK) >> MO_ATOM_SHIFT];
2649 mop &= ~(MO_AMASK | MO_BSWAP | MO_SSIZE | MO_ATOM_MASK);
2650
2651 /* If all fields are accounted for, print symbolically. */
2652 if (!mop && s_al && s_op && s_at) {
2653 col += ne_fprintf(f, ",%s%s%s,%u",
2654 s_at, s_al, s_op, ix);
2655 } else {
2656 mop = get_memop(oi);
2657 col += ne_fprintf(f, ",$0x%x,%u", mop, ix);
2658 }
2659 i = 1;
2660 }
2661 break;
2662 case INDEX_op_bswap16_i32:
2663 case INDEX_op_bswap16_i64:
2664 case INDEX_op_bswap32_i32:
2665 case INDEX_op_bswap32_i64:
2666 case INDEX_op_bswap64_i64:
2667 {
2668 TCGArg flags = op->args[k];
2669 const char *name = NULL;
2670
2671 if (flags < ARRAY_SIZE(bswap_flag_name)) {
2672 name = bswap_flag_name[flags];
2673 }
2674 if (name) {
2675 col += ne_fprintf(f, ",%s", name);
2676 } else {
2677 col += ne_fprintf(f, ",$0x%" TCG_PRIlx, flags);
2678 }
2679 i = k = 1;
2680 }
2681 break;
2682 default:
2683 i = 0;
2684 break;
2685 }
2686 switch (c) {
2687 case INDEX_op_set_label:
2688 case INDEX_op_br:
2689 case INDEX_op_brcond_i32:
2690 case INDEX_op_brcond_i64:
2691 case INDEX_op_brcond2_i32:
2692 col += ne_fprintf(f, "%s$L%d", k ? "," : "",
2693 arg_label(op->args[k])->id);
2694 i++, k++;
2695 break;
2696 case INDEX_op_mb:
2697 {
2698 TCGBar membar = op->args[k];
2699 const char *b_op, *m_op;
2700
2701 switch (membar & TCG_BAR_SC) {
2702 case 0:
2703 b_op = "none";
2704 break;
2705 case TCG_BAR_LDAQ:
2706 b_op = "acq";
2707 break;
2708 case TCG_BAR_STRL:
2709 b_op = "rel";
2710 break;
2711 case TCG_BAR_SC:
2712 b_op = "seq";
2713 break;
2714 default:
2715 g_assert_not_reached();
2716 }
2717
2718 switch (membar & TCG_MO_ALL) {
2719 case 0:
2720 m_op = "none";
2721 break;
2722 case TCG_MO_LD_LD:
2723 m_op = "rr";
2724 break;
2725 case TCG_MO_LD_ST:
2726 m_op = "rw";
2727 break;
2728 case TCG_MO_ST_LD:
2729 m_op = "wr";
2730 break;
2731 case TCG_MO_ST_ST:
2732 m_op = "ww";
2733 break;
2734 case TCG_MO_LD_LD | TCG_MO_LD_ST:
2735 m_op = "rr+rw";
2736 break;
2737 case TCG_MO_LD_LD | TCG_MO_ST_LD:
2738 m_op = "rr+wr";
2739 break;
2740 case TCG_MO_LD_LD | TCG_MO_ST_ST:
2741 m_op = "rr+ww";
2742 break;
2743 case TCG_MO_LD_ST | TCG_MO_ST_LD:
2744 m_op = "rw+wr";
2745 break;
2746 case TCG_MO_LD_ST | TCG_MO_ST_ST:
2747 m_op = "rw+ww";
2748 break;
2749 case TCG_MO_ST_LD | TCG_MO_ST_ST:
2750 m_op = "wr+ww";
2751 break;
2752 case TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_LD:
2753 m_op = "rr+rw+wr";
2754 break;
2755 case TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST:
2756 m_op = "rr+rw+ww";
2757 break;
2758 case TCG_MO_LD_LD | TCG_MO_ST_LD | TCG_MO_ST_ST:
2759 m_op = "rr+wr+ww";
2760 break;
2761 case TCG_MO_LD_ST | TCG_MO_ST_LD | TCG_MO_ST_ST:
2762 m_op = "rw+wr+ww";
2763 break;
2764 case TCG_MO_ALL:
2765 m_op = "all";
2766 break;
2767 default:
2768 g_assert_not_reached();
2769 }
2770
2771 col += ne_fprintf(f, "%s%s:%s", (k ? "," : ""), b_op, m_op);
2772 i++, k++;
2773 }
2774 break;
2775 default:
2776 break;
2777 }
2778 for (; i < nb_cargs; i++, k++) {
2779 col += ne_fprintf(f, "%s$0x%" TCG_PRIlx, k ? "," : "",
2780 op->args[k]);
2781 }
2782 }
2783
2784 if (have_prefs || op->life) {
2785 for (; col < 40; ++col) {
2786 putc(' ', f);
2787 }
2788 }
2789
2790 if (op->life) {
2791 unsigned life = op->life;
2792
2793 if (life & (SYNC_ARG * 3)) {
2794 ne_fprintf(f, " sync:");
2795 for (i = 0; i < 2; ++i) {
2796 if (life & (SYNC_ARG << i)) {
2797 ne_fprintf(f, " %d", i);
2798 }
2799 }
2800 }
2801 life /= DEAD_ARG;
2802 if (life) {
2803 ne_fprintf(f, " dead:");
2804 for (i = 0; life; ++i, life >>= 1) {
2805 if (life & 1) {
2806 ne_fprintf(f, " %d", i);
2807 }
2808 }
2809 }
2810 }
2811
2812 if (have_prefs) {
2813 for (i = 0; i < nb_oargs; ++i) {
2814 TCGRegSet set = output_pref(op, i);
2815
2816 if (i == 0) {
2817 ne_fprintf(f, " pref=");
2818 } else {
2819 ne_fprintf(f, ",");
2820 }
2821 if (set == 0) {
2822 ne_fprintf(f, "none");
2823 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
2824 ne_fprintf(f, "all");
2825 #ifdef CONFIG_DEBUG_TCG
2826 } else if (tcg_regset_single(set)) {
2827 TCGReg reg = tcg_regset_first(set);
2828 ne_fprintf(f, "%s", tcg_target_reg_names[reg]);
2829 #endif
2830 } else if (TCG_TARGET_NB_REGS <= 32) {
2831 ne_fprintf(f, "0x%x", (uint32_t)set);
2832 } else {
2833 ne_fprintf(f, "0x%" PRIx64, (uint64_t)set);
2834 }
2835 }
2836 }
2837
2838 putc('\n', f);
2839 }
2840 }
2841
2842 /* we give more priority to constraints with less registers */
2843 static int get_constraint_priority(const TCGOpDef *def, int k)
2844 {
2845 const TCGArgConstraint *arg_ct = &def->args_ct[k];
2846 int n = ctpop64(arg_ct->regs);
2847
2848 /*
2849 * Sort constraints of a single register first, which includes output
2850 * aliases (which must exactly match the input already allocated).
2851 */
2852 if (n == 1 || arg_ct->oalias) {
2853 return INT_MAX;
2854 }
2855
2856 /*
2857 * Sort register pairs next, first then second immediately after.
2858 * Arbitrarily sort multiple pairs by the index of the first reg;
2859 * there shouldn't be many pairs.
2860 */
2861 switch (arg_ct->pair) {
2862 case 1:
2863 case 3:
2864 return (k + 1) * 2;
2865 case 2:
2866 return (arg_ct->pair_index + 1) * 2 - 1;
2867 }
2868
2869 /* Finally, sort by decreasing register count. */
2870 assert(n > 1);
2871 return -n;
2872 }
2873
2874 /* sort from highest priority to lowest */
2875 static void sort_constraints(TCGOpDef *def, int start, int n)
2876 {
2877 int i, j;
2878 TCGArgConstraint *a = def->args_ct;
2879
2880 for (i = 0; i < n; i++) {
2881 a[start + i].sort_index = start + i;
2882 }
2883 if (n <= 1) {
2884 return;
2885 }
2886 for (i = 0; i < n - 1; i++) {
2887 for (j = i + 1; j < n; j++) {
2888 int p1 = get_constraint_priority(def, a[start + i].sort_index);
2889 int p2 = get_constraint_priority(def, a[start + j].sort_index);
2890 if (p1 < p2) {
2891 int tmp = a[start + i].sort_index;
2892 a[start + i].sort_index = a[start + j].sort_index;
2893 a[start + j].sort_index = tmp;
2894 }
2895 }
2896 }
2897 }
2898
2899 static void process_op_defs(TCGContext *s)
2900 {
2901 TCGOpcode op;
2902
2903 for (op = 0; op < NB_OPS; op++) {
2904 TCGOpDef *def = &tcg_op_defs[op];
2905 const TCGTargetOpDef *tdefs;
2906 bool saw_alias_pair = false;
2907 int i, o, i2, o2, nb_args;
2908
2909 if (def->flags & TCG_OPF_NOT_PRESENT) {
2910 continue;
2911 }
2912
2913 nb_args = def->nb_iargs + def->nb_oargs;
2914 if (nb_args == 0) {
2915 continue;
2916 }
2917
2918 /*
2919 * Macro magic should make it impossible, but double-check that
2920 * the array index is in range. Since the signness of an enum
2921 * is implementation defined, force the result to unsigned.
2922 */
2923 unsigned con_set = tcg_target_op_def(op);
2924 tcg_debug_assert(con_set < ARRAY_SIZE(constraint_sets));
2925 tdefs = &constraint_sets[con_set];
2926
2927 for (i = 0; i < nb_args; i++) {
2928 const char *ct_str = tdefs->args_ct_str[i];
2929 bool input_p = i >= def->nb_oargs;
2930
2931 /* Incomplete TCGTargetOpDef entry. */
2932 tcg_debug_assert(ct_str != NULL);
2933
2934 switch (*ct_str) {
2935 case '0' ... '9':
2936 o = *ct_str - '0';
2937 tcg_debug_assert(input_p);
2938 tcg_debug_assert(o < def->nb_oargs);
2939 tcg_debug_assert(def->args_ct[o].regs != 0);
2940 tcg_debug_assert(!def->args_ct[o].oalias);
2941 def->args_ct[i] = def->args_ct[o];
2942 /* The output sets oalias. */
2943 def->args_ct[o].oalias = 1;
2944 def->args_ct[o].alias_index = i;
2945 /* The input sets ialias. */
2946 def->args_ct[i].ialias = 1;
2947 def->args_ct[i].alias_index = o;
2948 if (def->args_ct[i].pair) {
2949 saw_alias_pair = true;
2950 }
2951 tcg_debug_assert(ct_str[1] == '\0');
2952 continue;
2953
2954 case '&':
2955 tcg_debug_assert(!input_p);
2956 def->args_ct[i].newreg = true;
2957 ct_str++;
2958 break;
2959
2960 case 'p': /* plus */
2961 /* Allocate to the register after the previous. */
2962 tcg_debug_assert(i > (input_p ? def->nb_oargs : 0));
2963 o = i - 1;
2964 tcg_debug_assert(!def->args_ct[o].pair);
2965 tcg_debug_assert(!def->args_ct[o].ct);
2966 def->args_ct[i] = (TCGArgConstraint){
2967 .pair = 2,
2968 .pair_index = o,
2969 .regs = def->args_ct[o].regs << 1,
2970 };
2971 def->args_ct[o].pair = 1;
2972 def->args_ct[o].pair_index = i;
2973 tcg_debug_assert(ct_str[1] == '\0');
2974 continue;
2975
2976 case 'm': /* minus */
2977 /* Allocate to the register before the previous. */
2978 tcg_debug_assert(i > (input_p ? def->nb_oargs : 0));
2979 o = i - 1;
2980 tcg_debug_assert(!def->args_ct[o].pair);
2981 tcg_debug_assert(!def->args_ct[o].ct);
2982 def->args_ct[i] = (TCGArgConstraint){
2983 .pair = 1,
2984 .pair_index = o,
2985 .regs = def->args_ct[o].regs >> 1,
2986 };
2987 def->args_ct[o].pair = 2;
2988 def->args_ct[o].pair_index = i;
2989 tcg_debug_assert(ct_str[1] == '\0');
2990 continue;
2991 }
2992
2993 do {
2994 switch (*ct_str) {
2995 case 'i':
2996 def->args_ct[i].ct |= TCG_CT_CONST;
2997 break;
2998
2999 /* Include all of the target-specific constraints. */
3000
3001 #undef CONST
3002 #define CONST(CASE, MASK) \
3003 case CASE: def->args_ct[i].ct |= MASK; break;
3004 #define REGS(CASE, MASK) \
3005 case CASE: def->args_ct[i].regs |= MASK; break;
3006
3007 #include "tcg-target-con-str.h"
3008
3009 #undef REGS
3010 #undef CONST
3011 default:
3012 case '0' ... '9':
3013 case '&':
3014 case 'p':
3015 case 'm':
3016 /* Typo in TCGTargetOpDef constraint. */
3017 g_assert_not_reached();
3018 }
3019 } while (*++ct_str != '\0');
3020 }
3021
3022 /* TCGTargetOpDef entry with too much information? */
3023 tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
3024
3025 /*
3026 * Fix up output pairs that are aliased with inputs.
3027 * When we created the alias, we copied pair from the output.
3028 * There are three cases:
3029 * (1a) Pairs of inputs alias pairs of outputs.
3030 * (1b) One input aliases the first of a pair of outputs.
3031 * (2) One input aliases the second of a pair of outputs.
3032 *
3033 * Case 1a is handled by making sure that the pair_index'es are
3034 * properly updated so that they appear the same as a pair of inputs.
3035 *
3036 * Case 1b is handled by setting the pair_index of the input to
3037 * itself, simply so it doesn't point to an unrelated argument.
3038 * Since we don't encounter the "second" during the input allocation
3039 * phase, nothing happens with the second half of the input pair.
3040 *
3041 * Case 2 is handled by setting the second input to pair=3, the
3042 * first output to pair=3, and the pair_index'es to match.
3043 */
3044 if (saw_alias_pair) {
3045 for (i = def->nb_oargs; i < nb_args; i++) {
3046 /*
3047 * Since [0-9pm] must be alone in the constraint string,
3048 * the only way they can both be set is if the pair comes
3049 * from the output alias.
3050 */
3051 if (!def->args_ct[i].ialias) {
3052 continue;
3053 }
3054 switch (def->args_ct[i].pair) {
3055 case 0:
3056 break;
3057 case 1:
3058 o = def->args_ct[i].alias_index;
3059 o2 = def->args_ct[o].pair_index;
3060 tcg_debug_assert(def->args_ct[o].pair == 1);
3061 tcg_debug_assert(def->args_ct[o2].pair == 2);
3062 if (def->args_ct[o2].oalias) {
3063 /* Case 1a */
3064 i2 = def->args_ct[o2].alias_index;
3065 tcg_debug_assert(def->args_ct[i2].pair == 2);
3066 def->args_ct[i2].pair_index = i;
3067 def->args_ct[i].pair_index = i2;
3068 } else {
3069 /* Case 1b */
3070 def->args_ct[i].pair_index = i;
3071 }
3072 break;
3073 case 2:
3074 o = def->args_ct[i].alias_index;
3075 o2 = def->args_ct[o].pair_index;
3076 tcg_debug_assert(def->args_ct[o].pair == 2);
3077 tcg_debug_assert(def->args_ct[o2].pair == 1);
3078 if (def->args_ct[o2].oalias) {
3079 /* Case 1a */
3080 i2 = def->args_ct[o2].alias_index;
3081 tcg_debug_assert(def->args_ct[i2].pair == 1);
3082 def->args_ct[i2].pair_index = i;
3083 def->args_ct[i].pair_index = i2;
3084 } else {
3085 /* Case 2 */
3086 def->args_ct[i].pair = 3;
3087 def->args_ct[o2].pair = 3;
3088 def->args_ct[i].pair_index = o2;
3089 def->args_ct[o2].pair_index = i;
3090 }
3091 break;
3092 default:
3093 g_assert_not_reached();
3094 }
3095 }
3096 }
3097
3098 /* sort the constraints (XXX: this is just an heuristic) */
3099 sort_constraints(def, 0, def->nb_oargs);
3100 sort_constraints(def, def->nb_oargs, def->nb_iargs);
3101 }
3102 }
3103
3104 static void remove_label_use(TCGOp *op, int idx)
3105 {
3106 TCGLabel *label = arg_label(op->args[idx]);
3107 TCGLabelUse *use;
3108
3109 QSIMPLEQ_FOREACH(use, &label->branches, next) {
3110 if (use->op == op) {
3111 QSIMPLEQ_REMOVE(&label->branches, use, TCGLabelUse, next);
3112 return;
3113 }
3114 }
3115 g_assert_not_reached();
3116 }
3117
3118 void tcg_op_remove(TCGContext *s, TCGOp *op)
3119 {
3120 switch (op->opc) {
3121 case INDEX_op_br:
3122 remove_label_use(op, 0);
3123 break;
3124 case INDEX_op_brcond_i32:
3125 case INDEX_op_brcond_i64:
3126 remove_label_use(op, 3);
3127 break;
3128 case INDEX_op_brcond2_i32:
3129 remove_label_use(op, 5);
3130 break;
3131 default:
3132 break;
3133 }
3134
3135 QTAILQ_REMOVE(&s->ops, op, link);
3136 QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
3137 s->nb_ops--;
3138 }
3139
3140 void tcg_remove_ops_after(TCGOp *op)
3141 {
3142 TCGContext *s = tcg_ctx;
3143
3144 while (true) {
3145 TCGOp *last = tcg_last_op();
3146 if (last == op) {
3147 return;
3148 }
3149 tcg_op_remove(s, last);
3150 }
3151 }
3152
3153 static TCGOp *tcg_op_alloc(TCGOpcode opc, unsigned nargs)
3154 {
3155 TCGContext *s = tcg_ctx;
3156 TCGOp *op = NULL;
3157
3158 if (unlikely(!QTAILQ_EMPTY(&s->free_ops))) {
3159 QTAILQ_FOREACH(op, &s->free_ops, link) {
3160 if (nargs <= op->nargs) {
3161 QTAILQ_REMOVE(&s->free_ops, op, link);
3162 nargs = op->nargs;
3163 goto found;
3164 }
3165 }
3166 }
3167
3168 /* Most opcodes have 3 or 4 operands: reduce fragmentation. */
3169 nargs = MAX(4, nargs);
3170 op = tcg_malloc(sizeof(TCGOp) + sizeof(TCGArg) * nargs);
3171
3172 found:
3173 memset(op, 0, offsetof(TCGOp, link));
3174 op->opc = opc;
3175 op->nargs = nargs;
3176
3177 /* Check for bitfield overflow. */
3178 tcg_debug_assert(op->nargs == nargs);
3179
3180 s->nb_ops++;
3181 return op;
3182 }
3183
3184 TCGOp *tcg_emit_op(TCGOpcode opc, unsigned nargs)
3185 {
3186 TCGOp *op = tcg_op_alloc(opc, nargs);
3187 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
3188 return op;
3189 }
3190
3191 TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op,
3192 TCGOpcode opc, unsigned nargs)
3193 {
3194 TCGOp *new_op = tcg_op_alloc(opc, nargs);
3195 QTAILQ_INSERT_BEFORE(old_op, new_op, link);
3196 return new_op;
3197 }
3198
3199 TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op,
3200 TCGOpcode opc, unsigned nargs)
3201 {
3202 TCGOp *new_op = tcg_op_alloc(opc, nargs);
3203 QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link);
3204 return new_op;
3205 }
3206
3207 static void move_label_uses(TCGLabel *to, TCGLabel *from)
3208 {
3209 TCGLabelUse *u;
3210
3211 QSIMPLEQ_FOREACH(u, &from->branches, next) {
3212 TCGOp *op = u->op;
3213 switch (op->opc) {
3214 case INDEX_op_br:
3215 op->args[0] = label_arg(to);
3216 break;
3217 case INDEX_op_brcond_i32:
3218 case INDEX_op_brcond_i64:
3219 op->args[3] = label_arg(to);
3220 break;
3221 case INDEX_op_brcond2_i32:
3222 op->args[5] = label_arg(to);
3223 break;
3224 default:
3225 g_assert_not_reached();
3226 }
3227 }
3228
3229 QSIMPLEQ_CONCAT(&to->branches, &from->branches);
3230 }
3231
3232 /* Reachable analysis : remove unreachable code. */
3233 static void __attribute__((noinline))
3234 reachable_code_pass(TCGContext *s)
3235 {
3236 TCGOp *op, *op_next, *op_prev;
3237 bool dead = false;
3238
3239 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
3240 bool remove = dead;
3241 TCGLabel *label;
3242
3243 switch (op->opc) {
3244 case INDEX_op_set_label:
3245 label = arg_label(op->args[0]);
3246
3247 /*
3248 * Note that the first op in the TB is always a load,
3249 * so there is always something before a label.
3250 */
3251 op_prev = QTAILQ_PREV(op, link);
3252
3253 /*
3254 * If we find two sequential labels, move all branches to
3255 * reference the second label and remove the first label.
3256 * Do this before branch to next optimization, so that the
3257 * middle label is out of the way.
3258 */
3259 if (op_prev->opc == INDEX_op_set_label) {
3260 move_label_uses(label, arg_label(op_prev->args[0]));
3261 tcg_op_remove(s, op_prev);
3262 op_prev = QTAILQ_PREV(op, link);
3263 }
3264
3265 /*
3266 * Optimization can fold conditional branches to unconditional.
3267 * If we find a label which is preceded by an unconditional
3268 * branch to next, remove the branch. We couldn't do this when
3269 * processing the branch because any dead code between the branch
3270 * and label had not yet been removed.
3271 */
3272 if (op_prev->opc == INDEX_op_br &&
3273 label == arg_label(op_prev->args[0])) {
3274 tcg_op_remove(s, op_prev);
3275 /* Fall through means insns become live again. */
3276 dead = false;
3277 }
3278
3279 if (QSIMPLEQ_EMPTY(&label->branches)) {
3280 /*
3281 * While there is an occasional backward branch, virtually
3282 * all branches generated by the translators are forward.
3283 * Which means that generally we will have already removed
3284 * all references to the label that will be, and there is
3285 * little to be gained by iterating.
3286 */
3287 remove = true;
3288 } else {
3289 /* Once we see a label, insns become live again. */
3290 dead = false;
3291 remove = false;
3292 }
3293 break;
3294
3295 case INDEX_op_br:
3296 case INDEX_op_exit_tb:
3297 case INDEX_op_goto_ptr:
3298 /* Unconditional branches; everything following is dead. */
3299 dead = true;
3300 break;
3301
3302 case INDEX_op_call:
3303 /* Notice noreturn helper calls, raising exceptions. */
3304 if (tcg_call_flags(op) & TCG_CALL_NO_RETURN) {
3305 dead = true;
3306 }
3307 break;
3308
3309 case INDEX_op_insn_start:
3310 /* Never remove -- we need to keep these for unwind. */
3311 remove = false;
3312 break;
3313
3314 default:
3315 break;
3316 }
3317
3318 if (remove) {
3319 tcg_op_remove(s, op);
3320 }
3321 }
3322 }
3323
3324 #define TS_DEAD 1
3325 #define TS_MEM 2
3326
3327 #define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
3328 #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
3329
3330 /* For liveness_pass_1, the register preferences for a given temp. */
3331 static inline TCGRegSet *la_temp_pref(TCGTemp *ts)
3332 {
3333 return ts->state_ptr;
3334 }
3335
3336 /* For liveness_pass_1, reset the preferences for a given temp to the
3337 * maximal regset for its type.
3338 */
3339 static inline void la_reset_pref(TCGTemp *ts)
3340 {
3341 *la_temp_pref(ts)
3342 = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]);
3343 }
3344
3345 /* liveness analysis: end of function: all temps are dead, and globals
3346 should be in memory. */
3347 static void la_func_end(TCGContext *s, int ng, int nt)
3348 {
3349 int i;
3350
3351 for (i = 0; i < ng; ++i) {
3352 s->temps[i].state = TS_DEAD | TS_MEM;
3353 la_reset_pref(&s->temps[i]);
3354 }
3355 for (i = ng; i < nt; ++i) {
3356 s->temps[i].state = TS_DEAD;
3357 la_reset_pref(&s->temps[i]);
3358 }
3359 }
3360
3361 /* liveness analysis: end of basic block: all temps are dead, globals
3362 and local temps should be in memory. */
3363 static void la_bb_end(TCGContext *s, int ng, int nt)
3364 {
3365 int i;
3366
3367 for (i = 0; i < nt; ++i) {
3368 TCGTemp *ts = &s->temps[i];
3369 int state;
3370
3371 switch (ts->kind) {
3372 case TEMP_FIXED:
3373 case TEMP_GLOBAL:
3374 case TEMP_TB:
3375 state = TS_DEAD | TS_MEM;
3376 break;
3377 case TEMP_EBB:
3378 case TEMP_CONST:
3379 state = TS_DEAD;
3380 break;
3381 default:
3382 g_assert_not_reached();
3383 }
3384 ts->state = state;
3385 la_reset_pref(ts);
3386 }
3387 }
3388
3389 /* liveness analysis: sync globals back to memory. */
3390 static void la_global_sync(TCGContext *s, int ng)
3391 {
3392 int i;
3393
3394 for (i = 0; i < ng; ++i) {
3395 int state = s->temps[i].state;
3396 s->temps[i].state = state | TS_MEM;
3397 if (state == TS_DEAD) {
3398 /* If the global was previously dead, reset prefs. */
3399 la_reset_pref(&s->temps[i]);
3400 }
3401 }
3402 }
3403
3404 /*
3405 * liveness analysis: conditional branch: all temps are dead unless
3406 * explicitly live-across-conditional-branch, globals and local temps
3407 * should be synced.
3408 */
3409 static void la_bb_sync(TCGContext *s, int ng, int nt)
3410 {
3411 la_global_sync(s, ng);
3412
3413 for (int i = ng; i < nt; ++i) {
3414 TCGTemp *ts = &s->temps[i];
3415 int state;
3416
3417 switch (ts->kind) {
3418 case TEMP_TB:
3419 state = ts->state;
3420 ts->state = state | TS_MEM;
3421 if (state != TS_DEAD) {
3422 continue;
3423 }
3424 break;
3425 case TEMP_EBB:
3426 case TEMP_CONST:
3427 continue;
3428 default:
3429 g_assert_not_reached();
3430 }
3431 la_reset_pref(&s->temps[i]);
3432 }
3433 }
3434
3435 /* liveness analysis: sync globals back to memory and kill. */
3436 static void la_global_kill(TCGContext *s, int ng)
3437 {
3438 int i;
3439
3440 for (i = 0; i < ng; i++) {
3441 s->temps[i].state = TS_DEAD | TS_MEM;
3442 la_reset_pref(&s->temps[i]);
3443 }
3444 }
3445
3446 /* liveness analysis: note live globals crossing calls. */
3447 static void la_cross_call(TCGContext *s, int nt)
3448 {
3449 TCGRegSet mask = ~tcg_target_call_clobber_regs;
3450 int i;
3451
3452 for (i = 0; i < nt; i++) {
3453 TCGTemp *ts = &s->temps[i];
3454 if (!(ts->state & TS_DEAD)) {
3455 TCGRegSet *pset = la_temp_pref(ts);
3456 TCGRegSet set = *pset;
3457
3458 set &= mask;
3459 /* If the combination is not possible, restart. */
3460 if (set == 0) {
3461 set = tcg_target_available_regs[ts->type] & mask;
3462 }
3463 *pset = set;
3464 }
3465 }
3466 }
3467
3468 /*
3469 * Liveness analysis: Verify the lifetime of TEMP_TB, and reduce
3470 * to TEMP_EBB, if possible.
3471 */
3472 static void __attribute__((noinline))
3473 liveness_pass_0(TCGContext *s)
3474 {
3475 void * const multiple_ebb = (void *)(uintptr_t)-1;
3476 int nb_temps = s->nb_temps;
3477 TCGOp *op, *ebb;
3478
3479 for (int i = s->nb_globals; i < nb_temps; ++i) {
3480 s->temps[i].state_ptr = NULL;
3481 }
3482
3483 /*
3484 * Represent each EBB by the op at which it begins. In the case of
3485 * the first EBB, this is the first op, otherwise it is a label.
3486 * Collect the uses of each TEMP_TB: NULL for unused, EBB for use
3487 * within a single EBB, else MULTIPLE_EBB.
3488 */
3489 ebb = QTAILQ_FIRST(&s->ops);
3490 QTAILQ_FOREACH(op, &s->ops, link) {
3491 const TCGOpDef *def;
3492 int nb_oargs, nb_iargs;
3493
3494 switch (op->opc) {
3495 case INDEX_op_set_label:
3496 ebb = op;
3497 continue;
3498 case INDEX_op_discard:
3499 continue;
3500 case INDEX_op_call:
3501 nb_oargs = TCGOP_CALLO(op);
3502 nb_iargs = TCGOP_CALLI(op);
3503 break;
3504 default:
3505 def = &tcg_op_defs[op->opc];
3506 nb_oargs = def->nb_oargs;
3507 nb_iargs = def->nb_iargs;
3508 break;
3509 }
3510
3511 for (int i = 0; i < nb_oargs + nb_iargs; ++i) {
3512 TCGTemp *ts = arg_temp(op->args[i]);
3513
3514 if (ts->kind != TEMP_TB) {
3515 continue;
3516 }
3517 if (ts->state_ptr == NULL) {
3518 ts->state_ptr = ebb;
3519 } else if (ts->state_ptr != ebb) {
3520 ts->state_ptr = multiple_ebb;
3521 }
3522 }
3523 }
3524
3525 /*
3526 * For TEMP_TB that turned out not to be used beyond one EBB,
3527 * reduce the liveness to TEMP_EBB.
3528 */
3529 for (int i = s->nb_globals; i < nb_temps; ++i) {
3530 TCGTemp *ts = &s->temps[i];
3531 if (ts->kind == TEMP_TB && ts->state_ptr != multiple_ebb) {
3532 ts->kind = TEMP_EBB;
3533 }
3534 }
3535 }
3536
3537 /* Liveness analysis : update the opc_arg_life array to tell if a
3538 given input arguments is dead. Instructions updating dead
3539 temporaries are removed. */
3540 static void __attribute__((noinline))
3541 liveness_pass_1(TCGContext *s)
3542 {
3543 int nb_globals = s->nb_globals;
3544 int nb_temps = s->nb_temps;
3545 TCGOp *op, *op_prev;
3546 TCGRegSet *prefs;
3547 int i;
3548
3549 prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps);
3550 for (i = 0; i < nb_temps; ++i) {
3551 s->temps[i].state_ptr = prefs + i;
3552 }
3553
3554 /* ??? Should be redundant with the exit_tb that ends the TB. */
3555 la_func_end(s, nb_globals, nb_temps);
3556
3557 QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) {
3558 int nb_iargs, nb_oargs;
3559 TCGOpcode opc_new, opc_new2;
3560 bool have_opc_new2;
3561 TCGLifeData arg_life = 0;
3562 TCGTemp *ts;
3563 TCGOpcode opc = op->opc;
3564 const TCGOpDef *def = &tcg_op_defs[opc];
3565
3566 switch (opc) {
3567 case INDEX_op_call:
3568 {
3569 const TCGHelperInfo *info = tcg_call_info(op);
3570 int call_flags = tcg_call_flags(op);
3571
3572 nb_oargs = TCGOP_CALLO(op);
3573 nb_iargs = TCGOP_CALLI(op);
3574
3575 /* pure functions can be removed if their result is unused */
3576 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
3577 for (i = 0; i < nb_oargs; i++) {
3578 ts = arg_temp(op->args[i]);
3579 if (ts->state != TS_DEAD) {
3580 goto do_not_remove_call;
3581 }
3582 }
3583 goto do_remove;
3584 }
3585 do_not_remove_call:
3586
3587 /* Output args are dead. */
3588 for (i = 0; i < nb_oargs; i++) {
3589 ts = arg_temp(op->args[i]);
3590 if (ts->state & TS_DEAD) {
3591 arg_life |= DEAD_ARG << i;
3592 }
3593 if (ts->state & TS_MEM) {
3594 arg_life |= SYNC_ARG << i;
3595 }
3596 ts->state = TS_DEAD;
3597 la_reset_pref(ts);
3598 }
3599
3600 /* Not used -- it will be tcg_target_call_oarg_reg(). */
3601 memset(op->output_pref, 0, sizeof(op->output_pref));
3602
3603 if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
3604 TCG_CALL_NO_READ_GLOBALS))) {
3605 la_global_kill(s, nb_globals);
3606 } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
3607 la_global_sync(s, nb_globals);
3608 }
3609
3610 /* Record arguments that die in this helper. */
3611 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
3612 ts = arg_temp(op->args[i]);
3613 if (ts->state & TS_DEAD) {
3614 arg_life |= DEAD_ARG << i;
3615 }
3616 }
3617
3618 /* For all live registers, remove call-clobbered prefs. */
3619 la_cross_call(s, nb_temps);
3620
3621 /*
3622 * Input arguments are live for preceding opcodes.
3623 *
3624 * For those arguments that die, and will be allocated in
3625 * registers, clear the register set for that arg, to be
3626 * filled in below. For args that will be on the stack,
3627 * reset to any available reg. Process arguments in reverse
3628 * order so that if a temp is used more than once, the stack
3629 * reset to max happens before the register reset to 0.
3630 */
3631 for (i = nb_iargs - 1; i >= 0; i--) {
3632 const TCGCallArgumentLoc *loc = &info->in[i];
3633 ts = arg_temp(op->args[nb_oargs + i]);
3634
3635 if (ts->state & TS_DEAD) {
3636 switch (loc->kind) {
3637 case TCG_CALL_ARG_NORMAL:
3638 case TCG_CALL_ARG_EXTEND_U:
3639 case TCG_CALL_ARG_EXTEND_S:
3640 if (arg_slot_reg_p(loc->arg_slot)) {
3641 *la_temp_pref(ts) = 0;
3642 break;
3643 }
3644 /* fall through */
3645 default:
3646 *la_temp_pref(ts) =
3647 tcg_target_available_regs[ts->type];
3648 break;
3649 }
3650 ts->state &= ~TS_DEAD;
3651 }
3652 }
3653
3654 /*
3655 * For each input argument, add its input register to prefs.
3656 * If a temp is used once, this produces a single set bit;
3657 * if a temp is used multiple times, this produces a set.
3658 */
3659 for (i = 0; i < nb_iargs; i++) {
3660 const TCGCallArgumentLoc *loc = &info->in[i];
3661 ts = arg_temp(op->args[nb_oargs + i]);
3662
3663 switch (loc->kind) {
3664 case TCG_CALL_ARG_NORMAL:
3665 case TCG_CALL_ARG_EXTEND_U:
3666 case TCG_CALL_ARG_EXTEND_S:
3667 if (arg_slot_reg_p(loc->arg_slot)) {
3668 tcg_regset_set_reg(*la_temp_pref(ts),
3669 tcg_target_call_iarg_regs[loc->arg_slot]);
3670 }
3671 break;
3672 default:
3673 break;
3674 }
3675 }
3676 }
3677 break;
3678 case INDEX_op_insn_start:
3679 break;
3680 case INDEX_op_discard:
3681 /* mark the temporary as dead */
3682 ts = arg_temp(op->args[0]);
3683 ts->state = TS_DEAD;
3684 la_reset_pref(ts);
3685 break;
3686
3687 case INDEX_op_add2_i32:
3688 opc_new = INDEX_op_add_i32;
3689 goto do_addsub2;
3690 case INDEX_op_sub2_i32:
3691 opc_new = INDEX_op_sub_i32;
3692 goto do_addsub2;
3693 case INDEX_op_add2_i64:
3694 opc_new = INDEX_op_add_i64;
3695 goto do_addsub2;
3696 case INDEX_op_sub2_i64:
3697 opc_new = INDEX_op_sub_i64;
3698 do_addsub2:
3699 nb_iargs = 4;
3700 nb_oargs = 2;
3701 /* Test if the high part of the operation is dead, but not
3702 the low part. The result can be optimized to a simple
3703 add or sub. This happens often for x86_64 guest when the
3704 cpu mode is set to 32 bit. */
3705 if (arg_temp(op->args[1])->state == TS_DEAD) {
3706 if (arg_temp(op->args[0])->state == TS_DEAD) {
3707 goto do_remove;
3708 }
3709 /* Replace the opcode and adjust the args in place,
3710 leaving 3 unused args at the end. */
3711 op->opc = opc = opc_new;
3712 op->args[1] = op->args[2];
3713 op->args[2] = op->args[4];
3714 /* Fall through and mark the single-word operation live. */
3715 nb_iargs = 2;
3716 nb_oargs = 1;
3717 }
3718 goto do_not_remove;
3719
3720 case INDEX_op_mulu2_i32:
3721 opc_new = INDEX_op_mul_i32;
3722 opc_new2 = INDEX_op_muluh_i32;
3723 have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
3724 goto do_mul2;
3725 case INDEX_op_muls2_i32:
3726 opc_new = INDEX_op_mul_i32;
3727 opc_new2 = INDEX_op_mulsh_i32;
3728 have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
3729 goto do_mul2;
3730 case INDEX_op_mulu2_i64:
3731 opc_new = INDEX_op_mul_i64;
3732 opc_new2 = INDEX_op_muluh_i64;
3733 have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
3734 goto do_mul2;
3735 case INDEX_op_muls2_i64:
3736 opc_new = INDEX_op_mul_i64;
3737 opc_new2 = INDEX_op_mulsh_i64;
3738 have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
3739 goto do_mul2;
3740 do_mul2:
3741 nb_iargs = 2;
3742 nb_oargs = 2;
3743 if (arg_temp(op->args[1])->state == TS_DEAD) {
3744 if (arg_temp(op->args[0])->state == TS_DEAD) {
3745 /* Both parts of the operation are dead. */
3746 goto do_remove;
3747 }
3748 /* The high part of the operation is dead; generate the low. */
3749 op->opc = opc = opc_new;
3750 op->args[1] = op->args[2];
3751 op->args[2] = op->args[3];
3752 } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) {
3753 /* The low part of the operation is dead; generate the high. */
3754 op->opc = opc = opc_new2;
3755 op->args[0] = op->args[1];
3756 op->args[1] = op->args[2];
3757 op->args[2] = op->args[3];
3758 } else {
3759 goto do_not_remove;
3760 }
3761 /* Mark the single-word operation live. */
3762 nb_oargs = 1;
3763 goto do_not_remove;
3764
3765 default:
3766 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
3767 nb_iargs = def->nb_iargs;
3768 nb_oargs = def->nb_oargs;
3769
3770 /* Test if the operation can be removed because all
3771 its outputs are dead. We assume that nb_oargs == 0
3772 implies side effects */
3773 if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
3774 for (i = 0; i < nb_oargs; i++) {
3775 if (arg_temp(op->args[i])->state != TS_DEAD) {
3776 goto do_not_remove;
3777 }
3778 }
3779 goto do_remove;
3780 }
3781 goto do_not_remove;
3782
3783 do_remove:
3784 tcg_op_remove(s, op);
3785 break;
3786
3787 do_not_remove:
3788 for (i = 0; i < nb_oargs; i++) {
3789 ts = arg_temp(op->args[i]);
3790
3791 /* Remember the preference of the uses that followed. */
3792 if (i < ARRAY_SIZE(op->output_pref)) {
3793 op->output_pref[i] = *la_temp_pref(ts);
3794 }
3795
3796 /* Output args are dead. */
3797 if (ts->state & TS_DEAD) {
3798 arg_life |= DEAD_ARG << i;
3799 }
3800 if (ts->state & TS_MEM) {
3801 arg_life |= SYNC_ARG << i;
3802 }
3803 ts->state = TS_DEAD;
3804 la_reset_pref(ts);
3805 }
3806
3807 /* If end of basic block, update. */
3808 if (def->flags & TCG_OPF_BB_EXIT) {
3809 la_func_end(s, nb_globals, nb_temps);
3810 } else if (def->flags & TCG_OPF_COND_BRANCH) {
3811 la_bb_sync(s, nb_globals, nb_temps);
3812 } else if (def->flags & TCG_OPF_BB_END) {
3813 la_bb_end(s, nb_globals, nb_temps);
3814 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3815 la_global_sync(s, nb_globals);
3816 if (def->flags & TCG_OPF_CALL_CLOBBER) {
3817 la_cross_call(s, nb_temps);
3818 }
3819 }
3820
3821 /* Record arguments that die in this opcode. */
3822 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3823 ts = arg_temp(op->args[i]);
3824 if (ts->state & TS_DEAD) {
3825 arg_life |= DEAD_ARG << i;
3826 }
3827 }
3828
3829 /* Input arguments are live for preceding opcodes. */
3830 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3831 ts = arg_temp(op->args[i]);
3832 if (ts->state & TS_DEAD) {
3833 /* For operands that were dead, initially allow
3834 all regs for the type. */
3835 *la_temp_pref(ts) = tcg_target_available_regs[ts->type];
3836 ts->state &= ~TS_DEAD;
3837 }
3838 }
3839
3840 /* Incorporate constraints for this operand. */
3841 switch (opc) {
3842 case INDEX_op_mov_i32:
3843 case INDEX_op_mov_i64:
3844 /* Note that these are TCG_OPF_NOT_PRESENT and do not
3845 have proper constraints. That said, special case
3846 moves to propagate preferences backward. */
3847 if (IS_DEAD_ARG(1)) {
3848 *la_temp_pref(arg_temp(op->args[0]))
3849 = *la_temp_pref(arg_temp(op->args[1]));
3850 }
3851 break;
3852
3853 default:
3854 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3855 const TCGArgConstraint *ct = &def->args_ct[i];
3856 TCGRegSet set, *pset;
3857
3858 ts = arg_temp(op->args[i]);
3859 pset = la_temp_pref(ts);
3860 set = *pset;
3861
3862 set &= ct->regs;
3863 if (ct->ialias) {
3864 set &= output_pref(op, ct->alias_index);
3865 }
3866 /* If the combination is not possible, restart. */
3867 if (set == 0) {
3868 set = ct->regs;
3869 }
3870 *pset = set;
3871 }
3872 break;
3873 }
3874 break;
3875 }
3876 op->life = arg_life;
3877 }
3878 }
3879
3880 /* Liveness analysis: Convert indirect regs to direct temporaries. */
3881 static bool __attribute__((noinline))
3882 liveness_pass_2(TCGContext *s)
3883 {
3884 int nb_globals = s->nb_globals;
3885 int nb_temps, i;
3886 bool changes = false;
3887 TCGOp *op, *op_next;
3888
3889 /* Create a temporary for each indirect global. */
3890 for (i = 0; i < nb_globals; ++i) {
3891 TCGTemp *its = &s->temps[i];
3892 if (its->indirect_reg) {
3893 TCGTemp *dts = tcg_temp_alloc(s);
3894 dts->type = its->type;
3895 dts->base_type = its->base_type;
3896 dts->temp_subindex = its->temp_subindex;
3897 dts->kind = TEMP_EBB;
3898 its->state_ptr = dts;
3899 } else {
3900 its->state_ptr = NULL;
3901 }
3902 /* All globals begin dead. */
3903 its->state = TS_DEAD;
3904 }
3905 for (nb_temps = s->nb_temps; i < nb_temps; ++i) {
3906 TCGTemp *its = &s->temps[i];
3907 its->state_ptr = NULL;
3908 its->state = TS_DEAD;
3909 }
3910
3911 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
3912 TCGOpcode opc = op->opc;
3913 const TCGOpDef *def = &tcg_op_defs[opc];
3914 TCGLifeData arg_life = op->life;
3915 int nb_iargs, nb_oargs, call_flags;
3916 TCGTemp *arg_ts, *dir_ts;
3917
3918 if (opc == INDEX_op_call) {
3919 nb_oargs = TCGOP_CALLO(op);
3920 nb_iargs = TCGOP_CALLI(op);
3921 call_flags = tcg_call_flags(op);
3922 } else {
3923 nb_iargs = def->nb_iargs;
3924 nb_oargs = def->nb_oargs;
3925
3926 /* Set flags similar to how calls require. */
3927 if (def->flags & TCG_OPF_COND_BRANCH) {
3928 /* Like reading globals: sync_globals */
3929 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
3930 } else if (def->flags & TCG_OPF_BB_END) {
3931 /* Like writing globals: save_globals */
3932 call_flags = 0;
3933 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3934 /* Like reading globals: sync_globals */
3935 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
3936 } else {
3937 /* No effect on globals. */
3938 call_flags = (TCG_CALL_NO_READ_GLOBALS |
3939 TCG_CALL_NO_WRITE_GLOBALS);
3940 }
3941 }
3942
3943 /* Make sure that input arguments are available. */
3944 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
3945 arg_ts = arg_temp(op->args[i]);
3946 dir_ts = arg_ts->state_ptr;
3947 if (dir_ts && arg_ts->state == TS_DEAD) {
3948 TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32
3949 ? INDEX_op_ld_i32
3950 : INDEX_op_ld_i64);
3951 TCGOp *lop = tcg_op_insert_before(s, op, lopc, 3);
3952
3953 lop->args[0] = temp_arg(dir_ts);
3954 lop->args[1] = temp_arg(arg_ts->mem_base);
3955 lop->args[2] = arg_ts->mem_offset;
3956
3957 /* Loaded, but synced with memory. */
3958 arg_ts->state = TS_MEM;
3959 }
3960 }
3961
3962 /* Perform input replacement, and mark inputs that became dead.
3963 No action is required except keeping temp_state up to date
3964 so that we reload when needed. */
3965 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
3966 arg_ts = arg_temp(op->args[i]);
3967 dir_ts = arg_ts->state_ptr;
3968 if (dir_ts) {
3969 op->args[i] = temp_arg(dir_ts);
3970 changes = true;
3971 if (IS_DEAD_ARG(i)) {
3972 arg_ts->state = TS_DEAD;
3973 }
3974 }
3975 }
3976
3977 /* Liveness analysis should ensure that the following are
3978 all correct, for call sites and basic block end points. */
3979 if (call_flags & TCG_CALL_NO_READ_GLOBALS) {
3980 /* Nothing to do */
3981 } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) {
3982 for (i = 0; i < nb_globals; ++i) {
3983 /* Liveness should see that globals are synced back,
3984 that is, either TS_DEAD or TS_MEM. */
3985 arg_ts = &s->temps[i];
3986 tcg_debug_assert(arg_ts->state_ptr == 0
3987 || arg_ts->state != 0);
3988 }
3989 } else {
3990 for (i = 0; i < nb_globals; ++i) {
3991 /* Liveness should see that globals are saved back,
3992 that is, TS_DEAD, waiting to be reloaded. */
3993 arg_ts = &s->temps[i];
3994 tcg_debug_assert(arg_ts->state_ptr == 0
3995 || arg_ts->state == TS_DEAD);
3996 }
3997 }
3998
3999 /* Outputs become available. */
4000 if (opc == INDEX_op_mov_i32 || opc == INDEX_op_mov_i64) {
4001 arg_ts = arg_temp(op->args[0]);
4002 dir_ts = arg_ts->state_ptr;
4003 if (dir_ts) {
4004 op->args[0] = temp_arg(dir_ts);
4005 changes = true;
4006
4007 /* The output is now live and modified. */
4008 arg_ts->state = 0;
4009
4010 if (NEED_SYNC_ARG(0)) {
4011 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
4012 ? INDEX_op_st_i32
4013 : INDEX_op_st_i64);
4014 TCGOp *sop = tcg_op_insert_after(s, op, sopc, 3);
4015 TCGTemp *out_ts = dir_ts;
4016
4017 if (IS_DEAD_ARG(0)) {
4018 out_ts = arg_temp(op->args[1]);
4019 arg_ts->state = TS_DEAD;
4020 tcg_op_remove(s, op);
4021 } else {
4022 arg_ts->state = TS_MEM;
4023 }
4024
4025 sop->args[0] = temp_arg(out_ts);
4026 sop->args[1] = temp_arg(arg_ts->mem_base);
4027 sop->args[2] = arg_ts->mem_offset;
4028 } else {
4029 tcg_debug_assert(!IS_DEAD_ARG(0));
4030 }
4031 }
4032 } else {
4033 for (i = 0; i < nb_oargs; i++) {
4034 arg_ts = arg_temp(op->args[i]);
4035 dir_ts = arg_ts->state_ptr;
4036 if (!dir_ts) {
4037 continue;
4038 }
4039 op->args[i] = temp_arg(dir_ts);
4040 changes = true;
4041
4042 /* The output is now live and modified. */
4043 arg_ts->state = 0;
4044
4045 /* Sync outputs upon their last write. */
4046 if (NEED_SYNC_ARG(i)) {
4047 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
4048 ? INDEX_op_st_i32
4049 : INDEX_op_st_i64);
4050 TCGOp *sop = tcg_op_insert_after(s, op, sopc, 3);
4051
4052 sop->args[0] = temp_arg(dir_ts);
4053 sop->args[1] = temp_arg(arg_ts->mem_base);
4054 sop->args[2] = arg_ts->mem_offset;
4055
4056 arg_ts->state = TS_MEM;
4057 }
4058 /* Drop outputs that are dead. */
4059 if (IS_DEAD_ARG(i)) {
4060 arg_ts->state = TS_DEAD;
4061 }
4062 }
4063 }
4064 }
4065
4066 return changes;
4067 }
4068
4069 static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
4070 {
4071 intptr_t off;
4072 int size, align;
4073
4074 /* When allocating an object, look at the full type. */
4075 size = tcg_type_size(ts->base_type);
4076 switch (ts->base_type) {
4077 case TCG_TYPE_I32:
4078 align = 4;
4079 break;
4080 case TCG_TYPE_I64:
4081 case TCG_TYPE_V64:
4082 align = 8;
4083 break;
4084 case TCG_TYPE_I128:
4085 case TCG_TYPE_V128:
4086 case TCG_TYPE_V256:
4087 /*
4088 * Note that we do not require aligned storage for V256,
4089 * and that we provide alignment for I128 to match V128,
4090 * even if that's above what the host ABI requires.
4091 */
4092 align = 16;
4093 break;
4094 default:
4095 g_assert_not_reached();
4096 }
4097
4098 /*
4099 * Assume the stack is sufficiently aligned.
4100 * This affects e.g. ARM NEON, where we have 8 byte stack alignment
4101 * and do not require 16 byte vector alignment. This seems slightly
4102 * easier than fully parameterizing the above switch statement.
4103 */
4104 align = MIN(TCG_TARGET_STACK_ALIGN, align);
4105 off = ROUND_UP(s->current_frame_offset, align);
4106
4107 /* If we've exhausted the stack frame, restart with a smaller TB. */
4108 if (off + size > s->frame_end) {
4109 tcg_raise_tb_overflow(s);
4110 }
4111 s->current_frame_offset = off + size;
4112 #if defined(__sparc__)
4113 off += TCG_TARGET_STACK_BIAS;
4114 #endif
4115
4116 /* If the object was subdivided, assign memory to all the parts. */
4117 if (ts->base_type != ts->type) {
4118 int part_size = tcg_type_size(ts->type);
4119 int part_count = size / part_size;
4120
4121 /*
4122 * Each part is allocated sequentially in tcg_temp_new_internal.
4123 * Jump back to the first part by subtracting the current index.
4124 */
4125 ts -= ts->temp_subindex;
4126 for (int i = 0; i < part_count; ++i) {
4127 ts[i].mem_offset = off + i * part_size;
4128 ts[i].mem_base = s->frame_temp;
4129 ts[i].mem_allocated = 1;
4130 }
4131 } else {
4132 ts->mem_offset = off;
4133 ts->mem_base = s->frame_temp;
4134 ts->mem_allocated = 1;
4135 }
4136 }
4137
4138 /* Assign @reg to @ts, and update reg_to_temp[]. */
4139 static void set_temp_val_reg(TCGContext *s, TCGTemp *ts, TCGReg reg)
4140 {
4141 if (ts->val_type == TEMP_VAL_REG) {
4142 TCGReg old = ts->reg;
4143 tcg_debug_assert(s->reg_to_temp[old] == ts);
4144 if (old == reg) {
4145 return;
4146 }
4147 s->reg_to_temp[old] = NULL;
4148 }
4149 tcg_debug_assert(s->reg_to_temp[reg] == NULL);
4150 s->reg_to_temp[reg] = ts;
4151 ts->val_type = TEMP_VAL_REG;
4152 ts->reg = reg;
4153 }
4154
4155 /* Assign a non-register value type to @ts, and update reg_to_temp[]. */
4156 static void set_temp_val_nonreg(TCGContext *s, TCGTemp *ts, TCGTempVal type)
4157 {
4158 tcg_debug_assert(type != TEMP_VAL_REG);
4159 if (ts->val_type == TEMP_VAL_REG) {
4160 TCGReg reg = ts->reg;
4161 tcg_debug_assert(s->reg_to_temp[reg] == ts);
4162 s->reg_to_temp[reg] = NULL;
4163 }
4164 ts->val_type = type;
4165 }
4166
4167 static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet);
4168
4169 /* Mark a temporary as free or dead. If 'free_or_dead' is negative,
4170 mark it free; otherwise mark it dead. */
4171 static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead)
4172 {
4173 TCGTempVal new_type;
4174
4175 switch (ts->kind) {
4176 case TEMP_FIXED:
4177 return;
4178 case TEMP_GLOBAL:
4179 case TEMP_TB:
4180 new_type = TEMP_VAL_MEM;
4181 break;
4182 case TEMP_EBB:
4183 new_type = free_or_dead < 0 ? TEMP_VAL_MEM : TEMP_VAL_DEAD;
4184 break;
4185 case TEMP_CONST:
4186 new_type = TEMP_VAL_CONST;
4187 break;
4188 default:
4189 g_assert_not_reached();
4190 }
4191 set_temp_val_nonreg(s, ts, new_type);
4192 }
4193
4194 /* Mark a temporary as dead. */
4195 static inline void temp_dead(TCGContext *s, TCGTemp *ts)
4196 {
4197 temp_free_or_dead(s, ts, 1);
4198 }
4199
4200 /* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
4201 registers needs to be allocated to store a constant. If 'free_or_dead'
4202 is non-zero, subsequently release the temporary; if it is positive, the
4203 temp is dead; if it is negative, the temp is free. */
4204 static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs,
4205 TCGRegSet preferred_regs, int free_or_dead)
4206 {
4207 if (!temp_readonly(ts) && !ts->mem_coherent) {
4208 if (!ts->mem_allocated) {
4209 temp_allocate_frame(s, ts);
4210 }
4211 switch (ts->val_type) {
4212 case TEMP_VAL_CONST:
4213 /* If we're going to free the temp immediately, then we won't
4214 require it later in a register, so attempt to store the
4215 constant to memory directly. */
4216 if (free_or_dead
4217 && tcg_out_sti(s, ts->type, ts->val,
4218 ts->mem_base->reg, ts->mem_offset)) {
4219 break;
4220 }
4221 temp_load(s, ts, tcg_target_available_regs[ts->type],
4222 allocated_regs, preferred_regs);
4223 /* fallthrough */
4224
4225 case TEMP_VAL_REG:
4226 tcg_out_st(s, ts->type, ts->reg,
4227 ts->mem_base->reg, ts->mem_offset);
4228 break;
4229
4230 case TEMP_VAL_MEM:
4231 break;
4232
4233 case TEMP_VAL_DEAD:
4234 default:
4235 g_assert_not_reached();
4236 }
4237 ts->mem_coherent = 1;
4238 }
4239 if (free_or_dead) {
4240 temp_free_or_dead(s, ts, free_or_dead);
4241 }
4242 }
4243
4244 /* free register 'reg' by spilling the corresponding temporary if necessary */
4245 static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
4246 {
4247 TCGTemp *ts = s->reg_to_temp[reg];
4248 if (ts != NULL) {
4249 temp_sync(s, ts, allocated_regs, 0, -1);
4250 }
4251 }
4252
4253 /**
4254 * tcg_reg_alloc:
4255 * @required_regs: Set of registers in which we must allocate.
4256 * @allocated_regs: Set of registers which must be avoided.
4257 * @preferred_regs: Set of registers we should prefer.
4258 * @rev: True if we search the registers in "indirect" order.
4259 *
4260 * The allocated register must be in @required_regs & ~@allocated_regs,
4261 * but if we can put it in @preferred_regs we may save a move later.
4262 */
4263 static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs,
4264 TCGRegSet allocated_regs,
4265 TCGRegSet preferred_regs, bool rev)
4266 {
4267 int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
4268 TCGRegSet reg_ct[2];
4269 const int *order;
4270
4271 reg_ct[1] = required_regs & ~allocated_regs;
4272 tcg_debug_assert(reg_ct[1] != 0);
4273 reg_ct[0] = reg_ct[1] & preferred_regs;
4274
4275 /* Skip the preferred_regs option if it cannot be satisfied,
4276 or if the preference made no difference. */
4277 f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
4278
4279 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
4280
4281 /* Try free registers, preferences first. */
4282 for (j = f; j < 2; j++) {
4283 TCGRegSet set = reg_ct[j];
4284
4285 if (tcg_regset_single(set)) {
4286 /* One register in the set. */
4287 TCGReg reg = tcg_regset_first(set);
4288 if (s->reg_to_temp[reg] == NULL) {
4289 return reg;
4290 }
4291 } else {
4292 for (i = 0; i < n; i++) {
4293 TCGReg reg = order[i];
4294 if (s->reg_to_temp[reg] == NULL &&
4295 tcg_regset_test_reg(set, reg)) {
4296 return reg;
4297 }
4298 }
4299 }
4300 }
4301
4302 /* We must spill something. */
4303 for (j = f; j < 2; j++) {
4304 TCGRegSet set = reg_ct[j];
4305
4306 if (tcg_regset_single(set)) {
4307 /* One register in the set. */
4308 TCGReg reg = tcg_regset_first(set);
4309 tcg_reg_free(s, reg, allocated_regs);
4310 return reg;
4311 } else {
4312 for (i = 0; i < n; i++) {
4313 TCGReg reg = order[i];
4314 if (tcg_regset_test_reg(set, reg)) {
4315 tcg_reg_free(s, reg, allocated_regs);
4316 return reg;
4317 }
4318 }
4319 }
4320 }
4321
4322 g_assert_not_reached();
4323 }
4324
4325 static TCGReg tcg_reg_alloc_pair(TCGContext *s, TCGRegSet required_regs,
4326 TCGRegSet allocated_regs,
4327 TCGRegSet preferred_regs, bool rev)
4328 {
4329 int i, j, k, fmin, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
4330 TCGRegSet reg_ct[2];
4331 const int *order;
4332
4333 /* Ensure that if I is not in allocated_regs, I+1 is not either. */
4334 reg_ct[1] = required_regs & ~(allocated_regs | (allocated_regs >> 1));
4335 tcg_debug_assert(reg_ct[1] != 0);
4336 reg_ct[0] = reg_ct[1] & preferred_regs;
4337
4338 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
4339
4340 /*
4341 * Skip the preferred_regs option if it cannot be satisfied,
4342 * or if the preference made no difference.
4343 */
4344 k = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
4345
4346 /*
4347 * Minimize the number of flushes by looking for 2 free registers first,
4348 * then a single flush, then two flushes.
4349 */
4350 for (fmin = 2; fmin >= 0; fmin--) {
4351 for (j = k; j < 2; j++) {
4352 TCGRegSet set = reg_ct[j];
4353
4354 for (i = 0; i < n; i++) {
4355 TCGReg reg = order[i];
4356
4357 if (tcg_regset_test_reg(set, reg)) {
4358 int f = !s->reg_to_temp[reg] + !s->reg_to_temp[reg + 1];
4359 if (f >= fmin) {
4360 tcg_reg_free(s, reg, allocated_regs);
4361 tcg_reg_free(s, reg + 1, allocated_regs);
4362 return reg;
4363 }
4364 }
4365 }
4366 }
4367 }
4368 g_assert_not_reached();
4369 }
4370
4371 /* Make sure the temporary is in a register. If needed, allocate the register
4372 from DESIRED while avoiding ALLOCATED. */
4373 static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
4374 TCGRegSet allocated_regs, TCGRegSet preferred_regs)
4375 {
4376 TCGReg reg;
4377
4378 switch (ts->val_type) {
4379 case TEMP_VAL_REG:
4380 return;
4381 case TEMP_VAL_CONST:
4382 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
4383 preferred_regs, ts->indirect_base);
4384 if (ts->type <= TCG_TYPE_I64) {
4385 tcg_out_movi(s, ts->type, reg, ts->val);
4386 } else {
4387 uint64_t val = ts->val;
4388 MemOp vece = MO_64;
4389
4390 /*
4391 * Find the minimal vector element that matches the constant.
4392 * The targets will, in general, have to do this search anyway,
4393 * do this generically.
4394 */
4395 if (val == dup_const(MO_8, val)) {
4396 vece = MO_8;
4397 } else if (val == dup_const(MO_16, val)) {
4398 vece = MO_16;
4399 } else if (val == dup_const(MO_32, val)) {
4400 vece = MO_32;
4401 }
4402
4403 tcg_out_dupi_vec(s, ts->type, vece, reg, ts->val);
4404 }
4405 ts->mem_coherent = 0;
4406 break;
4407 case TEMP_VAL_MEM:
4408 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
4409 preferred_regs, ts->indirect_base);
4410 tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
4411 ts->mem_coherent = 1;
4412 break;
4413 case TEMP_VAL_DEAD:
4414 default:
4415 g_assert_not_reached();
4416 }
4417 set_temp_val_reg(s, ts, reg);
4418 }
4419
4420 /* Save a temporary to memory. 'allocated_regs' is used in case a
4421 temporary registers needs to be allocated to store a constant. */
4422 static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
4423 {
4424 /* The liveness analysis already ensures that globals are back
4425 in memory. Keep an tcg_debug_assert for safety. */
4426 tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || temp_readonly(ts));
4427 }
4428
4429 /* save globals to their canonical location and assume they can be
4430 modified be the following code. 'allocated_regs' is used in case a
4431 temporary registers needs to be allocated to store a constant. */
4432 static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
4433 {
4434 int i, n;
4435
4436 for (i = 0, n = s->nb_globals; i < n; i++) {
4437 temp_save(s, &s->temps[i], allocated_regs);
4438 }
4439 }
4440
4441 /* sync globals to their canonical location and assume they can be
4442 read by the following code. 'allocated_regs' is used in case a
4443 temporary registers needs to be allocated to store a constant. */
4444 static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
4445 {
4446 int i, n;
4447
4448 for (i = 0, n = s->nb_globals; i < n; i++) {
4449 TCGTemp *ts = &s->temps[i];
4450 tcg_debug_assert(ts->val_type != TEMP_VAL_REG
4451 || ts->kind == TEMP_FIXED
4452 || ts->mem_coherent);
4453 }
4454 }
4455
4456 /* at the end of a basic block, we assume all temporaries are dead and
4457 all globals are stored at their canonical location. */
4458 static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
4459 {
4460 int i;
4461
4462 for (i = s->nb_globals; i < s->nb_temps; i++) {
4463 TCGTemp *ts = &s->temps[i];
4464
4465 switch (ts->kind) {
4466 case TEMP_TB:
4467 temp_save(s, ts, allocated_regs);
4468 break;
4469 case TEMP_EBB:
4470 /* The liveness analysis already ensures that temps are dead.
4471 Keep an tcg_debug_assert for safety. */
4472 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
4473 break;
4474 case TEMP_CONST:
4475 /* Similarly, we should have freed any allocated register. */
4476 tcg_debug_assert(ts->val_type == TEMP_VAL_CONST);
4477 break;
4478 default:
4479 g_assert_not_reached();
4480 }
4481 }
4482
4483 save_globals(s, allocated_regs);
4484 }
4485
4486 /*
4487 * At a conditional branch, we assume all temporaries are dead unless
4488 * explicitly live-across-conditional-branch; all globals and local
4489 * temps are synced to their location.
4490 */
4491 static void tcg_reg_alloc_cbranch(TCGContext *s, TCGRegSet allocated_regs)
4492 {
4493 sync_globals(s, allocated_regs);
4494
4495 for (int i = s->nb_globals; i < s->nb_temps; i++) {
4496 TCGTemp *ts = &s->temps[i];
4497 /*
4498 * The liveness analysis already ensures that temps are dead.
4499 * Keep tcg_debug_asserts for safety.
4500 */
4501 switch (ts->kind) {
4502 case TEMP_TB:
4503 tcg_debug_assert(ts->val_type != TEMP_VAL_REG || ts->mem_coherent);
4504 break;
4505 case TEMP_EBB:
4506 case TEMP_CONST:
4507 break;
4508 default:
4509 g_assert_not_reached();
4510 }
4511 }
4512 }
4513
4514 /*
4515 * Specialized code generation for INDEX_op_mov_* with a constant.
4516 */
4517 static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots,
4518 tcg_target_ulong val, TCGLifeData arg_life,
4519 TCGRegSet preferred_regs)
4520 {
4521 /* ENV should not be modified. */
4522 tcg_debug_assert(!temp_readonly(ots));
4523
4524 /* The movi is not explicitly generated here. */
4525 set_temp_val_nonreg(s, ots, TEMP_VAL_CONST);
4526 ots->val = val;
4527 ots->mem_coherent = 0;
4528 if (NEED_SYNC_ARG(0)) {
4529 temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0));
4530 } else if (IS_DEAD_ARG(0)) {
4531 temp_dead(s, ots);
4532 }
4533 }
4534
4535 /*
4536 * Specialized code generation for INDEX_op_mov_*.
4537 */
4538 static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op)
4539 {
4540 const TCGLifeData arg_life = op->life;
4541 TCGRegSet allocated_regs, preferred_regs;
4542 TCGTemp *ts, *ots;
4543 TCGType otype, itype;
4544 TCGReg oreg, ireg;
4545
4546 allocated_regs = s->reserved_regs;
4547 preferred_regs = output_pref(op, 0);
4548 ots = arg_temp(op->args[0]);
4549 ts = arg_temp(op->args[1]);
4550
4551 /* ENV should not be modified. */
4552 tcg_debug_assert(!temp_readonly(ots));
4553
4554 /* Note that otype != itype for no-op truncation. */
4555 otype = ots->type;
4556 itype = ts->type;
4557
4558 if (ts->val_type == TEMP_VAL_CONST) {
4559 /* propagate constant or generate sti */
4560 tcg_target_ulong val = ts->val;
4561 if (IS_DEAD_ARG(1)) {
4562 temp_dead(s, ts);
4563 }
4564 tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
4565 return;
4566 }
4567
4568 /* If the source value is in memory we're going to be forced
4569 to have it in a register in order to perform the copy. Copy
4570 the SOURCE value into its own register first, that way we
4571 don't have to reload SOURCE the next time it is used. */
4572 if (ts->val_type == TEMP_VAL_MEM) {
4573 temp_load(s, ts, tcg_target_available_regs[itype],
4574 allocated_regs, preferred_regs);
4575 }
4576 tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
4577 ireg = ts->reg;
4578
4579 if (IS_DEAD_ARG(0)) {
4580 /* mov to a non-saved dead register makes no sense (even with
4581 liveness analysis disabled). */
4582 tcg_debug_assert(NEED_SYNC_ARG(0));
4583 if (!ots->mem_allocated) {
4584 temp_allocate_frame(s, ots);
4585 }
4586 tcg_out_st(s, otype, ireg, ots->mem_base->reg, ots->mem_offset);
4587 if (IS_DEAD_ARG(1)) {
4588 temp_dead(s, ts);
4589 }
4590 temp_dead(s, ots);
4591 return;
4592 }
4593
4594 if (IS_DEAD_ARG(1) && ts->kind != TEMP_FIXED) {
4595 /*
4596 * The mov can be suppressed. Kill input first, so that it
4597 * is unlinked from reg_to_temp, then set the output to the
4598 * reg that we saved from the input.
4599 */
4600 temp_dead(s, ts);
4601 oreg = ireg;
4602 } else {
4603 if (ots->val_type == TEMP_VAL_REG) {
4604 oreg = ots->reg;
4605 } else {
4606 /* Make sure to not spill the input register during allocation. */
4607 oreg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
4608 allocated_regs | ((TCGRegSet)1 << ireg),
4609 preferred_regs, ots->indirect_base);
4610 }
4611 if (!tcg_out_mov(s, otype, oreg, ireg)) {
4612 /*
4613 * Cross register class move not supported.
4614 * Store the source register into the destination slot
4615 * and leave the destination temp as TEMP_VAL_MEM.
4616 */
4617 assert(!temp_readonly(ots));
4618 if (!ts->mem_allocated) {
4619 temp_allocate_frame(s, ots);
4620 }
4621 tcg_out_st(s, ts->type, ireg, ots->mem_base->reg, ots->mem_offset);
4622 set_temp_val_nonreg(s, ts, TEMP_VAL_MEM);
4623 ots->mem_coherent = 1;
4624 return;
4625 }
4626 }
4627 set_temp_val_reg(s, ots, oreg);
4628 ots->mem_coherent = 0;
4629
4630 if (NEED_SYNC_ARG(0)) {
4631 temp_sync(s, ots, allocated_regs, 0, 0);
4632 }
4633 }
4634
4635 /*
4636 * Specialized code generation for INDEX_op_dup_vec.
4637 */
4638 static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op)
4639 {
4640 const TCGLifeData arg_life = op->life;
4641 TCGRegSet dup_out_regs, dup_in_regs;
4642 TCGTemp *its, *ots;
4643 TCGType itype, vtype;
4644 unsigned vece;
4645 int lowpart_ofs;
4646 bool ok;
4647
4648 ots = arg_temp(op->args[0]);
4649 its = arg_temp(op->args[1]);
4650
4651 /* ENV should not be modified. */
4652 tcg_debug_assert(!temp_readonly(ots));
4653
4654 itype = its->type;
4655 vece = TCGOP_VECE(op);
4656 vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
4657
4658 if (its->val_type == TEMP_VAL_CONST) {
4659 /* Propagate constant via movi -> dupi. */
4660 tcg_target_ulong val = its->val;
4661 if (IS_DEAD_ARG(1)) {
4662 temp_dead(s, its);
4663 }
4664 tcg_reg_alloc_do_movi(s, ots, val, arg_life, output_pref(op, 0));
4665 return;
4666 }
4667
4668 dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
4669 dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1].regs;
4670
4671 /* Allocate the output register now. */
4672 if (ots->val_type != TEMP_VAL_REG) {
4673 TCGRegSet allocated_regs = s->reserved_regs;
4674 TCGReg oreg;
4675
4676 if (!IS_DEAD_ARG(1) && its->val_type == TEMP_VAL_REG) {
4677 /* Make sure to not spill the input register. */
4678 tcg_regset_set_reg(allocated_regs, its->reg);
4679 }
4680 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
4681 output_pref(op, 0), ots->indirect_base);
4682 set_temp_val_reg(s, ots, oreg);
4683 }
4684
4685 switch (its->val_type) {
4686 case TEMP_VAL_REG:
4687 /*
4688 * The dup constriaints must be broad, covering all possible VECE.
4689 * However, tcg_op_dup_vec() gets to see the VECE and we allow it
4690 * to fail, indicating that extra moves are required for that case.
4691 */
4692 if (tcg_regset_test_reg(dup_in_regs, its->reg)) {
4693 if (tcg_out_dup_vec(s, vtype, vece, ots->reg, its->reg)) {
4694 goto done;
4695 }
4696 /* Try again from memory or a vector input register. */
4697 }
4698 if (!its->mem_coherent) {
4699 /*
4700 * The input register is not synced, and so an extra store
4701 * would be required to use memory. Attempt an integer-vector
4702 * register move first. We do not have a TCGRegSet for this.
4703 */
4704 if (tcg_out_mov(s, itype, ots->reg, its->reg)) {
4705 break;
4706 }
4707 /* Sync the temp back to its slot and load from there. */
4708 temp_sync(s, its, s->reserved_regs, 0, 0);
4709 }
4710 /* fall through */
4711
4712 case TEMP_VAL_MEM:
4713 lowpart_ofs = 0;
4714 if (HOST_BIG_ENDIAN) {
4715 lowpart_ofs = tcg_type_size(itype) - (1 << vece);
4716 }
4717 if (tcg_out_dupm_vec(s, vtype, vece, ots->reg, its->mem_base->reg,
4718 its->mem_offset + lowpart_ofs)) {
4719 goto done;
4720 }
4721 /* Load the input into the destination vector register. */
4722 tcg_out_ld(s, itype, ots->reg, its->mem_base->reg, its->mem_offset);
4723 break;
4724
4725 default:
4726 g_assert_not_reached();
4727 }
4728
4729 /* We now have a vector input register, so dup must succeed. */
4730 ok = tcg_out_dup_vec(s, vtype, vece, ots->reg, ots->reg);
4731 tcg_debug_assert(ok);
4732
4733 done:
4734 ots->mem_coherent = 0;
4735 if (IS_DEAD_ARG(1)) {
4736 temp_dead(s, its);
4737 }
4738 if (NEED_SYNC_ARG(0)) {
4739 temp_sync(s, ots, s->reserved_regs, 0, 0);
4740 }
4741 if (IS_DEAD_ARG(0)) {
4742 temp_dead(s, ots);
4743 }
4744 }
4745
4746 static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
4747 {
4748 const TCGLifeData arg_life = op->life;
4749 const TCGOpDef * const def = &tcg_op_defs[op->opc];
4750 TCGRegSet i_allocated_regs;
4751 TCGRegSet o_allocated_regs;
4752 int i, k, nb_iargs, nb_oargs;
4753 TCGReg reg;
4754 TCGArg arg;
4755 const TCGArgConstraint *arg_ct;
4756 TCGTemp *ts;
4757 TCGArg new_args[TCG_MAX_OP_ARGS];
4758 int const_args[TCG_MAX_OP_ARGS];
4759
4760 nb_oargs = def->nb_oargs;
4761 nb_iargs = def->nb_iargs;
4762
4763 /* copy constants */
4764 memcpy(new_args + nb_oargs + nb_iargs,
4765 op->args + nb_oargs + nb_iargs,
4766 sizeof(TCGArg) * def->nb_cargs);
4767
4768 i_allocated_regs = s->reserved_regs;
4769 o_allocated_regs = s->reserved_regs;
4770
4771 /* satisfy input constraints */
4772 for (k = 0; k < nb_iargs; k++) {
4773 TCGRegSet i_preferred_regs, i_required_regs;
4774 bool allocate_new_reg, copyto_new_reg;
4775 TCGTemp *ts2;
4776 int i1, i2;
4777
4778 i = def->args_ct[nb_oargs + k].sort_index;
4779 arg = op->args[i];
4780 arg_ct = &def->args_ct[i];
4781 ts = arg_temp(arg);
4782
4783 if (ts->val_type == TEMP_VAL_CONST
4784 && tcg_target_const_match(ts->val, ts->type, arg_ct->ct, TCGOP_VECE(op))) {
4785 /* constant is OK for instruction */
4786 const_args[i] = 1;
4787 new_args[i] = ts->val;
4788 continue;
4789 }
4790
4791 reg = ts->reg;
4792 i_preferred_regs = 0;
4793 i_required_regs = arg_ct->regs;
4794 allocate_new_reg = false;
4795 copyto_new_reg = false;
4796
4797 switch (arg_ct->pair) {
4798 case 0: /* not paired */
4799 if (arg_ct->ialias) {
4800 i_preferred_regs = output_pref(op, arg_ct->alias_index);
4801
4802 /*
4803 * If the input is readonly, then it cannot also be an
4804 * output and aliased to itself. If the input is not
4805 * dead after the instruction, we must allocate a new
4806 * register and move it.
4807 */
4808 if (temp_readonly(ts) || !IS_DEAD_ARG(i)
4809 || def->args_ct[arg_ct->alias_index].newreg) {
4810 allocate_new_reg = true;
4811 } else if (ts->val_type == TEMP_VAL_REG) {
4812 /*
4813 * Check if the current register has already been
4814 * allocated for another input.
4815 */
4816 allocate_new_reg =
4817 tcg_regset_test_reg(i_allocated_regs, reg);
4818 }
4819 }
4820 if (!allocate_new_reg) {
4821 temp_load(s, ts, i_required_regs, i_allocated_regs,
4822 i_preferred_regs);
4823 reg = ts->reg;
4824 allocate_new_reg = !tcg_regset_test_reg(i_required_regs, reg);
4825 }
4826 if (allocate_new_reg) {
4827 /*
4828 * Allocate a new register matching the constraint
4829 * and move the temporary register into it.
4830 */
4831 temp_load(s, ts, tcg_target_available_regs[ts->type],
4832 i_allocated_regs, 0);
4833 reg = tcg_reg_alloc(s, i_required_regs, i_allocated_regs,
4834 i_preferred_regs, ts->indirect_base);
4835 copyto_new_reg = true;
4836 }
4837 break;
4838
4839 case 1:
4840 /* First of an input pair; if i1 == i2, the second is an output. */
4841 i1 = i;
4842 i2 = arg_ct->pair_index;
4843 ts2 = i1 != i2 ? arg_temp(op->args[i2]) : NULL;
4844
4845 /*
4846 * It is easier to default to allocating a new pair
4847 * and to identify a few cases where it's not required.
4848 */
4849 if (arg_ct->ialias) {
4850 i_preferred_regs = output_pref(op, arg_ct->alias_index);
4851 if (IS_DEAD_ARG(i1) &&
4852 IS_DEAD_ARG(i2) &&
4853 !temp_readonly(ts) &&
4854 ts->val_type == TEMP_VAL_REG &&
4855 ts->reg < TCG_TARGET_NB_REGS - 1 &&
4856 tcg_regset_test_reg(i_required_regs, reg) &&
4857 !tcg_regset_test_reg(i_allocated_regs, reg) &&
4858 !tcg_regset_test_reg(i_allocated_regs, reg + 1) &&
4859 (ts2
4860 ? ts2->val_type == TEMP_VAL_REG &&
4861 ts2->reg == reg + 1 &&
4862 !temp_readonly(ts2)
4863 : s->reg_to_temp[reg + 1] == NULL)) {
4864 break;
4865 }
4866 } else {
4867 /* Without aliasing, the pair must also be an input. */
4868 tcg_debug_assert(ts2);
4869 if (ts->val_type == TEMP_VAL_REG &&
4870 ts2->val_type == TEMP_VAL_REG &&
4871 ts2->reg == reg + 1 &&
4872 tcg_regset_test_reg(i_required_regs, reg)) {
4873 break;
4874 }
4875 }
4876 reg = tcg_reg_alloc_pair(s, i_required_regs, i_allocated_regs,
4877 0, ts->indirect_base);
4878 goto do_pair;
4879
4880 case 2: /* pair second */
4881 reg = new_args[arg_ct->pair_index] + 1;
4882 goto do_pair;
4883
4884 case 3: /* ialias with second output, no first input */
4885 tcg_debug_assert(arg_ct->ialias);
4886 i_preferred_regs = output_pref(op, arg_ct->alias_index);
4887
4888 if (IS_DEAD_ARG(i) &&
4889 !temp_readonly(ts) &&
4890 ts->val_type == TEMP_VAL_REG &&
4891 reg > 0 &&
4892 s->reg_to_temp[reg - 1] == NULL &&
4893 tcg_regset_test_reg(i_required_regs, reg) &&
4894 !tcg_regset_test_reg(i_allocated_regs, reg) &&
4895 !tcg_regset_test_reg(i_allocated_regs, reg - 1)) {
4896 tcg_regset_set_reg(i_allocated_regs, reg - 1);
4897 break;
4898 }
4899 reg = tcg_reg_alloc_pair(s, i_required_regs >> 1,
4900 i_allocated_regs, 0,
4901 ts->indirect_base);
4902 tcg_regset_set_reg(i_allocated_regs, reg);
4903 reg += 1;
4904 goto do_pair;
4905
4906 do_pair:
4907 /*
4908 * If an aliased input is not dead after the instruction,
4909 * we must allocate a new register and move it.
4910 */
4911 if (arg_ct->ialias && (!IS_DEAD_ARG(i) || temp_readonly(ts))) {
4912 TCGRegSet t_allocated_regs = i_allocated_regs;
4913
4914 /*
4915 * Because of the alias, and the continued life, make sure
4916 * that the temp is somewhere *other* than the reg pair,
4917 * and we get a copy in reg.
4918 */
4919 tcg_regset_set_reg(t_allocated_regs, reg);
4920 tcg_regset_set_reg(t_allocated_regs, reg + 1);
4921 if (ts->val_type == TEMP_VAL_REG && ts->reg == reg) {
4922 /* If ts was already in reg, copy it somewhere else. */
4923 TCGReg nr;
4924 bool ok;
4925
4926 tcg_debug_assert(ts->kind != TEMP_FIXED);
4927 nr = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
4928 t_allocated_regs, 0, ts->indirect_base);
4929 ok = tcg_out_mov(s, ts->type, nr, reg);
4930 tcg_debug_assert(ok);
4931
4932 set_temp_val_reg(s, ts, nr);
4933 } else {
4934 temp_load(s, ts, tcg_target_available_regs[ts->type],
4935 t_allocated_regs, 0);
4936 copyto_new_reg = true;
4937 }
4938 } else {
4939 /* Preferably allocate to reg, otherwise copy. */
4940 i_required_regs = (TCGRegSet)1 << reg;
4941 temp_load(s, ts, i_required_regs, i_allocated_regs,
4942 i_preferred_regs);
4943 copyto_new_reg = ts->reg != reg;
4944 }
4945 break;
4946
4947 default:
4948 g_assert_not_reached();
4949 }
4950
4951 if (copyto_new_reg) {
4952 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
4953 /*
4954 * Cross register class move not supported. Sync the
4955 * temp back to its slot and load from there.
4956 */
4957 temp_sync(s, ts, i_allocated_regs, 0, 0);
4958 tcg_out_ld(s, ts->type, reg,
4959 ts->mem_base->reg, ts->mem_offset);
4960 }
4961 }
4962 new_args[i] = reg;
4963 const_args[i] = 0;
4964 tcg_regset_set_reg(i_allocated_regs, reg);
4965 }
4966
4967 /* mark dead temporaries and free the associated registers */
4968 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
4969 if (IS_DEAD_ARG(i)) {
4970 temp_dead(s, arg_temp(op->args[i]));
4971 }
4972 }
4973
4974 if (def->flags & TCG_OPF_COND_BRANCH) {
4975 tcg_reg_alloc_cbranch(s, i_allocated_regs);
4976 } else if (def->flags & TCG_OPF_BB_END) {
4977 tcg_reg_alloc_bb_end(s, i_allocated_regs);
4978 } else {
4979 if (def->flags & TCG_OPF_CALL_CLOBBER) {
4980 /* XXX: permit generic clobber register list ? */
4981 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
4982 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
4983 tcg_reg_free(s, i, i_allocated_regs);
4984 }
4985 }
4986 }
4987 if (def->flags & TCG_OPF_SIDE_EFFECTS) {
4988 /* sync globals if the op has side effects and might trigger
4989 an exception. */
4990 sync_globals(s, i_allocated_regs);
4991 }
4992
4993 /* satisfy the output constraints */
4994 for(k = 0; k < nb_oargs; k++) {
4995 i = def->args_ct[k].sort_index;
4996 arg = op->args[i];
4997 arg_ct = &def->args_ct[i];
4998 ts = arg_temp(arg);
4999
5000 /* ENV should not be modified. */
5001 tcg_debug_assert(!temp_readonly(ts));
5002
5003 switch (arg_ct->pair) {
5004 case 0: /* not paired */
5005 if (arg_ct->oalias && !const_args[arg_ct->alias_index]) {
5006 reg = new_args[arg_ct->alias_index];
5007 } else if (arg_ct->newreg) {
5008 reg = tcg_reg_alloc(s, arg_ct->regs,
5009 i_allocated_regs | o_allocated_regs,
5010 output_pref(op, k), ts->indirect_base);
5011 } else {
5012 reg = tcg_reg_alloc(s, arg_ct->regs, o_allocated_regs,
5013 output_pref(op, k), ts->indirect_base);
5014 }
5015 break;
5016
5017 case 1: /* first of pair */
5018 tcg_debug_assert(!arg_ct->newreg);
5019 if (arg_ct->oalias) {
5020 reg = new_args[arg_ct->alias_index];
5021 break;
5022 }
5023 reg = tcg_reg_alloc_pair(s, arg_ct->regs, o_allocated_regs,
5024 output_pref(op, k), ts->indirect_base);
5025 break;
5026
5027 case 2: /* second of pair */
5028 tcg_debug_assert(!arg_ct->newreg);
5029 if (arg_ct->oalias) {
5030 reg = new_args[arg_ct->alias_index];
5031 } else {
5032 reg = new_args[arg_ct->pair_index] + 1;
5033 }
5034 break;
5035
5036 case 3: /* first of pair, aliasing with a second input */
5037 tcg_debug_assert(!arg_ct->newreg);
5038 reg = new_args[arg_ct->pair_index] - 1;
5039 break;
5040
5041 default:
5042 g_assert_not_reached();
5043 }
5044 tcg_regset_set_reg(o_allocated_regs, reg);
5045 set_temp_val_reg(s, ts, reg);
5046 ts->mem_coherent = 0;
5047 new_args[i] = reg;
5048 }
5049 }
5050
5051 /* emit instruction */
5052 switch (op->opc) {
5053 case INDEX_op_ext8s_i32:
5054 tcg_out_ext8s(s, TCG_TYPE_I32, new_args[0], new_args[1]);
5055 break;
5056 case INDEX_op_ext8s_i64:
5057 tcg_out_ext8s(s, TCG_TYPE_I64, new_args[0], new_args[1]);
5058 break;
5059 case INDEX_op_ext8u_i32:
5060 case INDEX_op_ext8u_i64:
5061 tcg_out_ext8u(s, new_args[0], new_args[1]);
5062 break;
5063 case INDEX_op_ext16s_i32:
5064 tcg_out_ext16s(s, TCG_TYPE_I32, new_args[0], new_args[1]);
5065 break;
5066 case INDEX_op_ext16s_i64:
5067 tcg_out_ext16s(s, TCG_TYPE_I64, new_args[0], new_args[1]);
5068 break;
5069 case INDEX_op_ext16u_i32:
5070 case INDEX_op_ext16u_i64:
5071 tcg_out_ext16u(s, new_args[0], new_args[1]);
5072 break;
5073 case INDEX_op_ext32s_i64:
5074 tcg_out_ext32s(s, new_args[0], new_args[1]);
5075 break;
5076 case INDEX_op_ext32u_i64:
5077 tcg_out_ext32u(s, new_args[0], new_args[1]);
5078 break;
5079 case INDEX_op_ext_i32_i64:
5080 tcg_out_exts_i32_i64(s, new_args[0], new_args[1]);
5081 break;
5082 case INDEX_op_extu_i32_i64:
5083 tcg_out_extu_i32_i64(s, new_args[0], new_args[1]);
5084 break;
5085 case INDEX_op_extrl_i64_i32:
5086 tcg_out_extrl_i64_i32(s, new_args[0], new_args[1]);
5087 break;
5088 default:
5089 if (def->flags & TCG_OPF_VECTOR) {
5090 tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
5091 new_args, const_args);
5092 } else {
5093 tcg_out_op(s, op->opc, new_args, const_args);
5094 }
5095 break;
5096 }
5097
5098 /* move the outputs in the correct register if needed */
5099 for(i = 0; i < nb_oargs; i++) {
5100 ts = arg_temp(op->args[i]);
5101
5102 /* ENV should not be modified. */
5103 tcg_debug_assert(!temp_readonly(ts));
5104
5105 if (NEED_SYNC_ARG(i)) {
5106 temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
5107 } else if (IS_DEAD_ARG(i)) {
5108 temp_dead(s, ts);
5109 }
5110 }
5111 }
5112
5113 static bool tcg_reg_alloc_dup2(TCGContext *s, const TCGOp *op)
5114 {
5115 const TCGLifeData arg_life = op->life;
5116 TCGTemp *ots, *itsl, *itsh;
5117 TCGType vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
5118
5119 /* This opcode is only valid for 32-bit hosts, for 64-bit elements. */
5120 tcg_debug_assert(TCG_TARGET_REG_BITS == 32);
5121 tcg_debug_assert(TCGOP_VECE(op) == MO_64);
5122
5123 ots = arg_temp(op->args[0]);
5124 itsl = arg_temp(op->args[1]);
5125 itsh = arg_temp(op->args[2]);
5126
5127 /* ENV should not be modified. */
5128 tcg_debug_assert(!temp_readonly(ots));
5129
5130 /* Allocate the output register now. */
5131 if (ots->val_type != TEMP_VAL_REG) {
5132 TCGRegSet allocated_regs = s->reserved_regs;
5133 TCGRegSet dup_out_regs =
5134 tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
5135 TCGReg oreg;
5136
5137 /* Make sure to not spill the input registers. */
5138 if (!IS_DEAD_ARG(1) && itsl->val_type == TEMP_VAL_REG) {
5139 tcg_regset_set_reg(allocated_regs, itsl->reg);
5140 }
5141 if (!IS_DEAD_ARG(2) && itsh->val_type == TEMP_VAL_REG) {
5142 tcg_regset_set_reg(allocated_regs, itsh->reg);
5143 }
5144
5145 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
5146 output_pref(op, 0), ots->indirect_base);
5147 set_temp_val_reg(s, ots, oreg);
5148 }
5149
5150 /* Promote dup2 of immediates to dupi_vec. */
5151 if (itsl->val_type == TEMP_VAL_CONST && itsh->val_type == TEMP_VAL_CONST) {
5152 uint64_t val = deposit64(itsl->val, 32, 32, itsh->val);
5153 MemOp vece = MO_64;
5154
5155 if (val == dup_const(MO_8, val)) {
5156 vece = MO_8;
5157 } else if (val == dup_const(MO_16, val)) {
5158 vece = MO_16;
5159 } else if (val == dup_const(MO_32, val)) {
5160 vece = MO_32;
5161 }
5162
5163 tcg_out_dupi_vec(s, vtype, vece, ots->reg, val);
5164 goto done;
5165 }
5166
5167 /* If the two inputs form one 64-bit value, try dupm_vec. */
5168 if (itsl->temp_subindex == HOST_BIG_ENDIAN &&
5169 itsh->temp_subindex == !HOST_BIG_ENDIAN &&
5170 itsl == itsh + (HOST_BIG_ENDIAN ? 1 : -1)) {
5171 TCGTemp *its = itsl - HOST_BIG_ENDIAN;
5172
5173 temp_sync(s, its + 0, s->reserved_regs, 0, 0);
5174 temp_sync(s, its + 1, s->reserved_regs, 0, 0);
5175
5176 if (tcg_out_dupm_vec(s, vtype, MO_64, ots->reg,
5177 its->mem_base->reg, its->mem_offset)) {
5178 goto done;
5179 }
5180 }
5181
5182 /* Fall back to generic expansion. */
5183 return false;
5184
5185 done:
5186 ots->mem_coherent = 0;
5187 if (IS_DEAD_ARG(1)) {
5188 temp_dead(s, itsl);
5189 }
5190 if (IS_DEAD_ARG(2)) {
5191 temp_dead(s, itsh);
5192 }
5193 if (NEED_SYNC_ARG(0)) {
5194 temp_sync(s, ots, s->reserved_regs, 0, IS_DEAD_ARG(0));
5195 } else if (IS_DEAD_ARG(0)) {
5196 temp_dead(s, ots);
5197 }
5198 return true;
5199 }
5200
5201 static void load_arg_reg(TCGContext *s, TCGReg reg, TCGTemp *ts,
5202 TCGRegSet allocated_regs)
5203 {
5204 if (ts->val_type == TEMP_VAL_REG) {
5205 if (ts->reg != reg) {
5206 tcg_reg_free(s, reg, allocated_regs);
5207 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
5208 /*
5209 * Cross register class move not supported. Sync the
5210 * temp back to its slot and load from there.
5211 */
5212 temp_sync(s, ts, allocated_regs, 0, 0);
5213 tcg_out_ld(s, ts->type, reg,
5214 ts->mem_base->reg, ts->mem_offset);
5215 }
5216 }
5217 } else {
5218 TCGRegSet arg_set = 0;
5219
5220 tcg_reg_free(s, reg, allocated_regs);
5221 tcg_regset_set_reg(arg_set, reg);
5222 temp_load(s, ts, arg_set, allocated_regs, 0);
5223 }
5224 }
5225
5226 static void load_arg_stk(TCGContext *s, unsigned arg_slot, TCGTemp *ts,
5227 TCGRegSet allocated_regs)
5228 {
5229 /*
5230 * When the destination is on the stack, load up the temp and store.
5231 * If there are many call-saved registers, the temp might live to
5232 * see another use; otherwise it'll be discarded.
5233 */
5234 temp_load(s, ts, tcg_target_available_regs[ts->type], allocated_regs, 0);
5235 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK,
5236 arg_slot_stk_ofs(arg_slot));
5237 }
5238
5239 static void load_arg_normal(TCGContext *s, const TCGCallArgumentLoc *l,
5240 TCGTemp *ts, TCGRegSet *allocated_regs)
5241 {
5242 if (arg_slot_reg_p(l->arg_slot)) {
5243 TCGReg reg = tcg_target_call_iarg_regs[l->arg_slot];
5244 load_arg_reg(s, reg, ts, *allocated_regs);
5245 tcg_regset_set_reg(*allocated_regs, reg);
5246 } else {
5247 load_arg_stk(s, l->arg_slot, ts, *allocated_regs);
5248 }
5249 }
5250
5251 static void load_arg_ref(TCGContext *s, unsigned arg_slot, TCGReg ref_base,
5252 intptr_t ref_off, TCGRegSet *allocated_regs)
5253 {
5254 TCGReg reg;
5255
5256 if (arg_slot_reg_p(arg_slot)) {
5257 reg = tcg_target_call_iarg_regs[arg_slot];
5258 tcg_reg_free(s, reg, *allocated_regs);
5259 tcg_out_addi_ptr(s, reg, ref_base, ref_off);
5260 tcg_regset_set_reg(*allocated_regs, reg);
5261 } else {
5262 reg = tcg_reg_alloc(s, tcg_target_available_regs[TCG_TYPE_PTR],
5263 *allocated_regs, 0, false);
5264 tcg_out_addi_ptr(s, reg, ref_base, ref_off);
5265 tcg_out_st(s, TCG_TYPE_PTR, reg, TCG_REG_CALL_STACK,
5266 arg_slot_stk_ofs(arg_slot));
5267 }
5268 }
5269
5270 static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
5271 {
5272 const int nb_oargs = TCGOP_CALLO(op);
5273 const int nb_iargs = TCGOP_CALLI(op);
5274 const TCGLifeData arg_life = op->life;
5275 const TCGHelperInfo *info = tcg_call_info(op);
5276 TCGRegSet allocated_regs = s->reserved_regs;
5277 int i;
5278
5279 /*
5280 * Move inputs into place in reverse order,
5281 * so that we place stacked arguments first.
5282 */
5283 for (i = nb_iargs - 1; i >= 0; --i) {
5284 const TCGCallArgumentLoc *loc = &info->in[i];
5285 TCGTemp *ts = arg_temp(op->args[nb_oargs + i]);
5286
5287 switch (loc->kind) {
5288 case TCG_CALL_ARG_NORMAL:
5289 case TCG_CALL_ARG_EXTEND_U:
5290 case TCG_CALL_ARG_EXTEND_S:
5291 load_arg_normal(s, loc, ts, &allocated_regs);
5292 break;
5293 case TCG_CALL_ARG_BY_REF:
5294 load_arg_stk(s, loc->ref_slot, ts, allocated_regs);
5295 load_arg_ref(s, loc->arg_slot, TCG_REG_CALL_STACK,
5296 arg_slot_stk_ofs(loc->ref_slot),
5297 &allocated_regs);
5298 break;
5299 case TCG_CALL_ARG_BY_REF_N:
5300 load_arg_stk(s, loc->ref_slot, ts, allocated_regs);
5301 break;
5302 default:
5303 g_assert_not_reached();
5304 }
5305 }
5306
5307 /* Mark dead temporaries and free the associated registers. */
5308 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
5309 if (IS_DEAD_ARG(i)) {
5310 temp_dead(s, arg_temp(op->args[i]));
5311 }
5312 }
5313
5314 /* Clobber call registers. */
5315 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
5316 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
5317 tcg_reg_free(s, i, allocated_regs);
5318 }
5319 }
5320
5321 /*
5322 * Save globals if they might be written by the helper,
5323 * sync them if they might be read.
5324 */
5325 if (info->flags & TCG_CALL_NO_READ_GLOBALS) {
5326 /* Nothing to do */
5327 } else if (info->flags & TCG_CALL_NO_WRITE_GLOBALS) {
5328 sync_globals(s, allocated_regs);
5329 } else {
5330 save_globals(s, allocated_regs);
5331 }
5332
5333 /*
5334 * If the ABI passes a pointer to the returned struct as the first
5335 * argument, load that now. Pass a pointer to the output home slot.
5336 */
5337 if (info->out_kind == TCG_CALL_RET_BY_REF) {
5338 TCGTemp *ts = arg_temp(op->args[0]);
5339
5340 if (!ts->mem_allocated) {
5341 temp_allocate_frame(s, ts);
5342 }
5343 load_arg_ref(s, 0, ts->mem_base->reg, ts->mem_offset, &allocated_regs);
5344 }
5345
5346 tcg_out_call(s, tcg_call_func(op), info);
5347
5348 /* Assign output registers and emit moves if needed. */
5349 switch (info->out_kind) {
5350 case TCG_CALL_RET_NORMAL:
5351 for (i = 0; i < nb_oargs; i++) {
5352 TCGTemp *ts = arg_temp(op->args[i]);
5353 TCGReg reg = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, i);
5354
5355 /* ENV should not be modified. */
5356 tcg_debug_assert(!temp_readonly(ts));
5357
5358 set_temp_val_reg(s, ts, reg);
5359 ts->mem_coherent = 0;
5360 }
5361 break;
5362
5363 case TCG_CALL_RET_BY_VEC:
5364 {
5365 TCGTemp *ts = arg_temp(op->args[0]);
5366
5367 tcg_debug_assert(ts->base_type == TCG_TYPE_I128);
5368 tcg_debug_assert(ts->temp_subindex == 0);
5369 if (!ts->mem_allocated) {
5370 temp_allocate_frame(s, ts);
5371 }
5372 tcg_out_st(s, TCG_TYPE_V128,
5373 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0),
5374 ts->mem_base->reg, ts->mem_offset);
5375 }
5376 /* fall through to mark all parts in memory */
5377
5378 case TCG_CALL_RET_BY_REF:
5379 /* The callee has performed a write through the reference. */
5380 for (i = 0; i < nb_oargs; i++) {
5381 TCGTemp *ts = arg_temp(op->args[i]);
5382 ts->val_type = TEMP_VAL_MEM;
5383 }
5384 break;
5385
5386 default:
5387 g_assert_not_reached();
5388 }
5389
5390 /* Flush or discard output registers as needed. */
5391 for (i = 0; i < nb_oargs; i++) {
5392 TCGTemp *ts = arg_temp(op->args[i]);
5393 if (NEED_SYNC_ARG(i)) {
5394 temp_sync(s, ts, s->reserved_regs, 0, IS_DEAD_ARG(i));
5395 } else if (IS_DEAD_ARG(i)) {
5396 temp_dead(s, ts);
5397 }
5398 }
5399 }
5400
5401 /**
5402 * atom_and_align_for_opc:
5403 * @s: tcg context
5404 * @opc: memory operation code
5405 * @host_atom: MO_ATOM_{IFALIGN,WITHIN16,SUBALIGN} for host operations
5406 * @allow_two_ops: true if we are prepared to issue two operations
5407 *
5408 * Return the alignment and atomicity to use for the inline fast path
5409 * for the given memory operation. The alignment may be larger than
5410 * that specified in @opc, and the correct alignment will be diagnosed
5411 * by the slow path helper.
5412 *
5413 * If @allow_two_ops, the host is prepared to test for 2x alignment,
5414 * and issue two loads or stores for subalignment.
5415 */
5416 static TCGAtomAlign atom_and_align_for_opc(TCGContext *s, MemOp opc,
5417 MemOp host_atom, bool allow_two_ops)
5418 {
5419 MemOp align = get_alignment_bits(opc);
5420 MemOp size = opc & MO_SIZE;
5421 MemOp half = size ? size - 1 : 0;
5422 MemOp atmax;
5423 MemOp atom;
5424
5425 /* When serialized, no further atomicity required. */
5426 if (s->gen_tb->cflags & CF_PARALLEL) {
5427 atom = opc & MO_ATOM_MASK;
5428 } else {
5429 atom = MO_ATOM_NONE;
5430 }
5431
5432 switch (atom) {
5433 case MO_ATOM_NONE:
5434 /* The operation requires no specific atomicity. */
5435 atmax = MO_8;
5436 break;
5437
5438 case MO_ATOM_IFALIGN:
5439 atmax = size;
5440 break;
5441
5442 case MO_ATOM_IFALIGN_PAIR:
5443 atmax = half;
5444 break;
5445
5446 case MO_ATOM_WITHIN16:
5447 atmax = size;
5448 if (size == MO_128) {
5449 /* Misalignment implies !within16, and therefore no atomicity. */
5450 } else if (host_atom != MO_ATOM_WITHIN16) {
5451 /* The host does not implement within16, so require alignment. */
5452 align = MAX(align, size);
5453 }
5454 break;
5455
5456 case MO_ATOM_WITHIN16_PAIR:
5457 atmax = size;
5458 /*
5459 * Misalignment implies !within16, and therefore half atomicity.
5460 * Any host prepared for two operations can implement this with
5461 * half alignment.
5462 */
5463 if (host_atom != MO_ATOM_WITHIN16 && allow_two_ops) {
5464 align = MAX(align, half);
5465 }
5466 break;
5467
5468 case MO_ATOM_SUBALIGN:
5469 atmax = size;
5470 if (host_atom != MO_ATOM_SUBALIGN) {
5471 /* If unaligned but not odd, there are subobjects up to half. */
5472 if (allow_two_ops) {
5473 align = MAX(align, half);
5474 } else {
5475 align = MAX(align, size);
5476 }
5477 }
5478 break;
5479
5480 default:
5481 g_assert_not_reached();
5482 }
5483
5484 return (TCGAtomAlign){ .atom = atmax, .align = align };
5485 }
5486
5487 /*
5488 * Similarly for qemu_ld/st slow path helpers.
5489 * We must re-implement tcg_gen_callN and tcg_reg_alloc_call simultaneously,
5490 * using only the provided backend tcg_out_* functions.
5491 */
5492
5493 static int tcg_out_helper_stk_ofs(TCGType type, unsigned slot)
5494 {
5495 int ofs = arg_slot_stk_ofs(slot);
5496
5497 /*
5498 * Each stack slot is TCG_TARGET_LONG_BITS. If the host does not
5499 * require extension to uint64_t, adjust the address for uint32_t.
5500 */
5501 if (HOST_BIG_ENDIAN &&
5502 TCG_TARGET_REG_BITS == 64 &&
5503 type == TCG_TYPE_I32) {
5504 ofs += 4;
5505 }
5506 return ofs;
5507 }
5508
5509 static void tcg_out_helper_load_slots(TCGContext *s,
5510 unsigned nmov, TCGMovExtend *mov,
5511 const TCGLdstHelperParam *parm)
5512 {
5513 unsigned i;
5514 TCGReg dst3;
5515
5516 /*
5517 * Start from the end, storing to the stack first.
5518 * This frees those registers, so we need not consider overlap.
5519 */
5520 for (i = nmov; i-- > 0; ) {
5521 unsigned slot = mov[i].dst;
5522
5523 if (arg_slot_reg_p(slot)) {
5524 goto found_reg;
5525 }
5526
5527 TCGReg src = mov[i].src;
5528 TCGType dst_type = mov[i].dst_type;
5529 MemOp dst_mo = dst_type == TCG_TYPE_I32 ? MO_32 : MO_64;
5530
5531 /* The argument is going onto the stack; extend into scratch. */
5532 if ((mov[i].src_ext & MO_SIZE) != dst_mo) {
5533 tcg_debug_assert(parm->ntmp != 0);
5534 mov[i].dst = src = parm->tmp[0];
5535 tcg_out_movext1(s, &mov[i]);
5536 }
5537
5538 tcg_out_st(s, dst_type, src, TCG_REG_CALL_STACK,
5539 tcg_out_helper_stk_ofs(dst_type, slot));
5540 }
5541 return;
5542
5543 found_reg:
5544 /*
5545 * The remaining arguments are in registers.
5546 * Convert slot numbers to argument registers.
5547 */
5548 nmov = i + 1;
5549 for (i = 0; i < nmov; ++i) {
5550 mov[i].dst = tcg_target_call_iarg_regs[mov[i].dst];
5551 }
5552
5553 switch (nmov) {
5554 case 4:
5555 /* The backend must have provided enough temps for the worst case. */
5556 tcg_debug_assert(parm->ntmp >= 2);
5557
5558 dst3 = mov[3].dst;
5559 for (unsigned j = 0; j < 3; ++j) {
5560 if (dst3 == mov[j].src) {
5561 /*
5562 * Conflict. Copy the source to a temporary, perform the
5563 * remaining moves, then the extension from our scratch
5564 * on the way out.
5565 */
5566 TCGReg scratch = parm->tmp[1];
5567
5568 tcg_out_mov(s, mov[3].src_type, scratch, mov[3].src);
5569 tcg_out_movext3(s, mov, mov + 1, mov + 2, parm->tmp[0]);
5570 tcg_out_movext1_new_src(s, &mov[3], scratch);
5571 break;
5572 }
5573 }
5574
5575 /* No conflicts: perform this move and continue. */
5576 tcg_out_movext1(s, &mov[3]);
5577 /* fall through */
5578
5579 case 3:
5580 tcg_out_movext3(s, mov, mov + 1, mov + 2,
5581 parm->ntmp ? parm->tmp[0] : -1);
5582 break;
5583 case 2:
5584 tcg_out_movext2(s, mov, mov + 1,
5585 parm->ntmp ? parm->tmp[0] : -1);
5586 break;
5587 case 1:
5588 tcg_out_movext1(s, mov);
5589 break;
5590 default:
5591 g_assert_not_reached();
5592 }
5593 }
5594
5595 static void tcg_out_helper_load_imm(TCGContext *s, unsigned slot,
5596 TCGType type, tcg_target_long imm,
5597 const TCGLdstHelperParam *parm)
5598 {
5599 if (arg_slot_reg_p(slot)) {
5600 tcg_out_movi(s, type, tcg_target_call_iarg_regs[slot], imm);
5601 } else {
5602 int ofs = tcg_out_helper_stk_ofs(type, slot);
5603 if (!tcg_out_sti(s, type, imm, TCG_REG_CALL_STACK, ofs)) {
5604 tcg_debug_assert(parm->ntmp != 0);
5605 tcg_out_movi(s, type, parm->tmp[0], imm);
5606 tcg_out_st(s, type, parm->tmp[0], TCG_REG_CALL_STACK, ofs);
5607 }
5608 }
5609 }
5610
5611 static void tcg_out_helper_load_common_args(TCGContext *s,
5612 const TCGLabelQemuLdst *ldst,
5613 const TCGLdstHelperParam *parm,
5614 const TCGHelperInfo *info,
5615 unsigned next_arg)
5616 {
5617 TCGMovExtend ptr_mov = {
5618 .dst_type = TCG_TYPE_PTR,
5619 .src_type = TCG_TYPE_PTR,
5620 .src_ext = sizeof(void *) == 4 ? MO_32 : MO_64
5621 };
5622 const TCGCallArgumentLoc *loc = &info->in[0];
5623 TCGType type;
5624 unsigned slot;
5625 tcg_target_ulong imm;
5626
5627 /*
5628 * Handle env, which is always first.
5629 */
5630 ptr_mov.dst = loc->arg_slot;
5631 ptr_mov.src = TCG_AREG0;
5632 tcg_out_helper_load_slots(s, 1, &ptr_mov, parm);
5633
5634 /*
5635 * Handle oi.
5636 */
5637 imm = ldst->oi;
5638 loc = &info->in[next_arg];
5639 type = TCG_TYPE_I32;
5640 switch (loc->kind) {
5641 case TCG_CALL_ARG_NORMAL:
5642 break;
5643 case TCG_CALL_ARG_EXTEND_U:
5644 case TCG_CALL_ARG_EXTEND_S:
5645 /* No extension required for MemOpIdx. */
5646 tcg_debug_assert(imm <= INT32_MAX);
5647 type = TCG_TYPE_REG;
5648 break;
5649 default:
5650 g_assert_not_reached();
5651 }
5652 tcg_out_helper_load_imm(s, loc->arg_slot, type, imm, parm);
5653 next_arg++;
5654
5655 /*
5656 * Handle ra.
5657 */
5658 loc = &info->in[next_arg];
5659 slot = loc->arg_slot;
5660 if (parm->ra_gen) {
5661 int arg_reg = -1;
5662 TCGReg ra_reg;
5663
5664 if (arg_slot_reg_p(slot)) {
5665 arg_reg = tcg_target_call_iarg_regs[slot];
5666 }
5667 ra_reg = parm->ra_gen(s, ldst, arg_reg);
5668
5669 ptr_mov.dst = slot;
5670 ptr_mov.src = ra_reg;
5671 tcg_out_helper_load_slots(s, 1, &ptr_mov, parm);
5672 } else {
5673 imm = (uintptr_t)ldst->raddr;
5674 tcg_out_helper_load_imm(s, slot, TCG_TYPE_PTR, imm, parm);
5675 }
5676 }
5677
5678 static unsigned tcg_out_helper_add_mov(TCGMovExtend *mov,
5679 const TCGCallArgumentLoc *loc,
5680 TCGType dst_type, TCGType src_type,
5681 TCGReg lo, TCGReg hi)
5682 {
5683 MemOp reg_mo;
5684
5685 if (dst_type <= TCG_TYPE_REG) {
5686 MemOp src_ext;
5687
5688 switch (loc->kind) {
5689 case TCG_CALL_ARG_NORMAL:
5690 src_ext = src_type == TCG_TYPE_I32 ? MO_32 : MO_64;
5691 break;
5692 case TCG_CALL_ARG_EXTEND_U:
5693 dst_type = TCG_TYPE_REG;
5694 src_ext = MO_UL;
5695 break;
5696 case TCG_CALL_ARG_EXTEND_S:
5697 dst_type = TCG_TYPE_REG;
5698 src_ext = MO_SL;
5699 break;
5700 default:
5701 g_assert_not_reached();
5702 }
5703
5704 mov[0].dst = loc->arg_slot;
5705 mov[0].dst_type = dst_type;
5706 mov[0].src = lo;
5707 mov[0].src_type = src_type;
5708 mov[0].src_ext = src_ext;
5709 return 1;
5710 }
5711
5712 if (TCG_TARGET_REG_BITS == 32) {
5713 assert(dst_type == TCG_TYPE_I64);
5714 reg_mo = MO_32;
5715 } else {
5716 assert(dst_type == TCG_TYPE_I128);
5717 reg_mo = MO_64;
5718 }
5719
5720 mov[0].dst = loc[HOST_BIG_ENDIAN].arg_slot;
5721 mov[0].src = lo;
5722 mov[0].dst_type = TCG_TYPE_REG;
5723 mov[0].src_type = TCG_TYPE_REG;
5724 mov[0].src_ext = reg_mo;
5725
5726 mov[1].dst = loc[!HOST_BIG_ENDIAN].arg_slot;
5727 mov[1].src = hi;
5728 mov[1].dst_type = TCG_TYPE_REG;
5729 mov[1].src_type = TCG_TYPE_REG;
5730 mov[1].src_ext = reg_mo;
5731
5732 return 2;
5733 }
5734
5735 static void tcg_out_ld_helper_args(TCGContext *s, const TCGLabelQemuLdst *ldst,
5736 const TCGLdstHelperParam *parm)
5737 {
5738 const TCGHelperInfo *info;
5739 const TCGCallArgumentLoc *loc;
5740 TCGMovExtend mov[2];
5741 unsigned next_arg, nmov;
5742 MemOp mop = get_memop(ldst->oi);
5743
5744 switch (mop & MO_SIZE) {
5745 case MO_8:
5746 case MO_16:
5747 case MO_32:
5748 info = &info_helper_ld32_mmu;
5749 break;
5750 case MO_64:
5751 info = &info_helper_ld64_mmu;
5752 break;
5753 case MO_128:
5754 info = &info_helper_ld128_mmu;
5755 break;
5756 default:
5757 g_assert_not_reached();
5758 }
5759
5760 /* Defer env argument. */
5761 next_arg = 1;
5762
5763 loc = &info->in[next_arg];
5764 if (TCG_TARGET_REG_BITS == 32 && s->addr_type == TCG_TYPE_I32) {
5765 /*
5766 * 32-bit host with 32-bit guest: zero-extend the guest address
5767 * to 64-bits for the helper by storing the low part, then
5768 * load a zero for the high part.
5769 */
5770 tcg_out_helper_add_mov(mov, loc + HOST_BIG_ENDIAN,
5771 TCG_TYPE_I32, TCG_TYPE_I32,
5772 ldst->addrlo_reg, -1);
5773 tcg_out_helper_load_slots(s, 1, mov, parm);
5774
5775 tcg_out_helper_load_imm(s, loc[!HOST_BIG_ENDIAN].arg_slot,
5776 TCG_TYPE_I32, 0, parm);
5777 next_arg += 2;
5778 } else {
5779 nmov = tcg_out_helper_add_mov(mov, loc, TCG_TYPE_I64, s->addr_type,
5780 ldst->addrlo_reg, ldst->addrhi_reg);
5781 tcg_out_helper_load_slots(s, nmov, mov, parm);
5782 next_arg += nmov;
5783 }
5784
5785 switch (info->out_kind) {
5786 case TCG_CALL_RET_NORMAL:
5787 case TCG_CALL_RET_BY_VEC:
5788 break;
5789 case TCG_CALL_RET_BY_REF:
5790 /*
5791 * The return reference is in the first argument slot.
5792 * We need memory in which to return: re-use the top of stack.
5793 */
5794 {
5795 int ofs_slot0 = TCG_TARGET_CALL_STACK_OFFSET;
5796
5797 if (arg_slot_reg_p(0)) {
5798 tcg_out_addi_ptr(s, tcg_target_call_iarg_regs[0],
5799 TCG_REG_CALL_STACK, ofs_slot0);
5800 } else {
5801 tcg_debug_assert(parm->ntmp != 0);
5802 tcg_out_addi_ptr(s, parm->tmp[0],
5803 TCG_REG_CALL_STACK, ofs_slot0);
5804 tcg_out_st(s, TCG_TYPE_PTR, parm->tmp[0],
5805 TCG_REG_CALL_STACK, ofs_slot0);
5806 }
5807 }
5808 break;
5809 default:
5810 g_assert_not_reached();
5811 }
5812
5813 tcg_out_helper_load_common_args(s, ldst, parm, info, next_arg);
5814 }
5815
5816 static void tcg_out_ld_helper_ret(TCGContext *s, const TCGLabelQemuLdst *ldst,
5817 bool load_sign,
5818 const TCGLdstHelperParam *parm)
5819 {
5820 MemOp mop = get_memop(ldst->oi);
5821 TCGMovExtend mov[2];
5822 int ofs_slot0;
5823
5824 switch (ldst->type) {
5825 case TCG_TYPE_I64:
5826 if (TCG_TARGET_REG_BITS == 32) {
5827 break;
5828 }
5829 /* fall through */
5830
5831 case TCG_TYPE_I32:
5832 mov[0].dst = ldst->datalo_reg;
5833 mov[0].src = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, 0);
5834 mov[0].dst_type = ldst->type;
5835 mov[0].src_type = TCG_TYPE_REG;
5836
5837 /*
5838 * If load_sign, then we allowed the helper to perform the
5839 * appropriate sign extension to tcg_target_ulong, and all
5840 * we need now is a plain move.
5841 *
5842 * If they do not, then we expect the relevant extension
5843 * instruction to be no more expensive than a move, and
5844 * we thus save the icache etc by only using one of two
5845 * helper functions.
5846 */
5847 if (load_sign || !(mop & MO_SIGN)) {
5848 if (TCG_TARGET_REG_BITS == 32 || ldst->type == TCG_TYPE_I32) {
5849 mov[0].src_ext = MO_32;
5850 } else {
5851 mov[0].src_ext = MO_64;
5852 }
5853 } else {
5854 mov[0].src_ext = mop & MO_SSIZE;
5855 }
5856 tcg_out_movext1(s, mov);
5857 return;
5858
5859 case TCG_TYPE_I128:
5860 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
5861 ofs_slot0 = TCG_TARGET_CALL_STACK_OFFSET;
5862 switch (TCG_TARGET_CALL_RET_I128) {
5863 case TCG_CALL_RET_NORMAL:
5864 break;
5865 case TCG_CALL_RET_BY_VEC:
5866 tcg_out_st(s, TCG_TYPE_V128,
5867 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0),
5868 TCG_REG_CALL_STACK, ofs_slot0);
5869 /* fall through */
5870 case TCG_CALL_RET_BY_REF:
5871 tcg_out_ld(s, TCG_TYPE_I64, ldst->datalo_reg,
5872 TCG_REG_CALL_STACK, ofs_slot0 + 8 * HOST_BIG_ENDIAN);
5873 tcg_out_ld(s, TCG_TYPE_I64, ldst->datahi_reg,
5874 TCG_REG_CALL_STACK, ofs_slot0 + 8 * !HOST_BIG_ENDIAN);
5875 return;
5876 default:
5877 g_assert_not_reached();
5878 }
5879 break;
5880
5881 default:
5882 g_assert_not_reached();
5883 }
5884
5885 mov[0].dst = ldst->datalo_reg;
5886 mov[0].src =
5887 tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, HOST_BIG_ENDIAN);
5888 mov[0].dst_type = TCG_TYPE_REG;
5889 mov[0].src_type = TCG_TYPE_REG;
5890 mov[0].src_ext = TCG_TARGET_REG_BITS == 32 ? MO_32 : MO_64;
5891
5892 mov[1].dst = ldst->datahi_reg;
5893 mov[1].src =
5894 tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, !HOST_BIG_ENDIAN);
5895 mov[1].dst_type = TCG_TYPE_REG;
5896 mov[1].src_type = TCG_TYPE_REG;
5897 mov[1].src_ext = TCG_TARGET_REG_BITS == 32 ? MO_32 : MO_64;
5898
5899 tcg_out_movext2(s, mov, mov + 1, parm->ntmp ? parm->tmp[0] : -1);
5900 }
5901
5902 static void tcg_out_st_helper_args(TCGContext *s, const TCGLabelQemuLdst *ldst,
5903 const TCGLdstHelperParam *parm)
5904 {
5905 const TCGHelperInfo *info;
5906 const TCGCallArgumentLoc *loc;
5907 TCGMovExtend mov[4];
5908 TCGType data_type;
5909 unsigned next_arg, nmov, n;
5910 MemOp mop = get_memop(ldst->oi);
5911
5912 switch (mop & MO_SIZE) {
5913 case MO_8:
5914 case MO_16:
5915 case MO_32:
5916 info = &info_helper_st32_mmu;
5917 data_type = TCG_TYPE_I32;
5918 break;
5919 case MO_64:
5920 info = &info_helper_st64_mmu;
5921 data_type = TCG_TYPE_I64;
5922 break;
5923 case MO_128:
5924 info = &info_helper_st128_mmu;
5925 data_type = TCG_TYPE_I128;
5926 break;
5927 default:
5928 g_assert_not_reached();
5929 }
5930
5931 /* Defer env argument. */
5932 next_arg = 1;
5933 nmov = 0;
5934
5935 /* Handle addr argument. */
5936 loc = &info->in[next_arg];
5937 if (TCG_TARGET_REG_BITS == 32 && s->addr_type == TCG_TYPE_I32) {
5938 /*
5939 * 32-bit host with 32-bit guest: zero-extend the guest address
5940 * to 64-bits for the helper by storing the low part. Later,
5941 * after we have processed the register inputs, we will load a
5942 * zero for the high part.
5943 */
5944 tcg_out_helper_add_mov(mov, loc + HOST_BIG_ENDIAN,
5945 TCG_TYPE_I32, TCG_TYPE_I32,
5946 ldst->addrlo_reg, -1);
5947 next_arg += 2;
5948 nmov += 1;
5949 } else {
5950 n = tcg_out_helper_add_mov(mov, loc, TCG_TYPE_I64, s->addr_type,
5951 ldst->addrlo_reg, ldst->addrhi_reg);
5952 next_arg += n;
5953 nmov += n;
5954 }
5955
5956 /* Handle data argument. */
5957 loc = &info->in[next_arg];
5958 switch (loc->kind) {
5959 case TCG_CALL_ARG_NORMAL:
5960 case TCG_CALL_ARG_EXTEND_U:
5961 case TCG_CALL_ARG_EXTEND_S:
5962 n = tcg_out_helper_add_mov(mov + nmov, loc, data_type, ldst->type,
5963 ldst->datalo_reg, ldst->datahi_reg);
5964 next_arg += n;
5965 nmov += n;
5966 tcg_out_helper_load_slots(s, nmov, mov, parm);
5967 break;
5968
5969 case TCG_CALL_ARG_BY_REF:
5970 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
5971 tcg_debug_assert(data_type == TCG_TYPE_I128);
5972 tcg_out_st(s, TCG_TYPE_I64,
5973 HOST_BIG_ENDIAN ? ldst->datahi_reg : ldst->datalo_reg,
5974 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc[0].ref_slot));
5975 tcg_out_st(s, TCG_TYPE_I64,
5976 HOST_BIG_ENDIAN ? ldst->datalo_reg : ldst->datahi_reg,
5977 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc[1].ref_slot));
5978
5979 tcg_out_helper_load_slots(s, nmov, mov, parm);
5980
5981 if (arg_slot_reg_p(loc->arg_slot)) {
5982 tcg_out_addi_ptr(s, tcg_target_call_iarg_regs[loc->arg_slot],
5983 TCG_REG_CALL_STACK,
5984 arg_slot_stk_ofs(loc->ref_slot));
5985 } else {
5986 tcg_debug_assert(parm->ntmp != 0);
5987 tcg_out_addi_ptr(s, parm->tmp[0], TCG_REG_CALL_STACK,
5988 arg_slot_stk_ofs(loc->ref_slot));
5989 tcg_out_st(s, TCG_TYPE_PTR, parm->tmp[0],
5990 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc->arg_slot));
5991 }
5992 next_arg += 2;
5993 break;
5994
5995 default:
5996 g_assert_not_reached();
5997 }
5998
5999 if (TCG_TARGET_REG_BITS == 32 && s->addr_type == TCG_TYPE_I32) {
6000 /* Zero extend the address by loading a zero for the high part. */
6001 loc = &info->in[1 + !HOST_BIG_ENDIAN];
6002 tcg_out_helper_load_imm(s, loc->arg_slot, TCG_TYPE_I32, 0, parm);
6003 }
6004
6005 tcg_out_helper_load_common_args(s, ldst, parm, info, next_arg);
6006 }
6007
6008 int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)
6009 {
6010 int i, start_words, num_insns;
6011 TCGOp *op;
6012
6013 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
6014 && qemu_log_in_addr_range(pc_start))) {
6015 FILE *logfile = qemu_log_trylock();
6016 if (logfile) {
6017 fprintf(logfile, "OP:\n");
6018 tcg_dump_ops(s, logfile, false);
6019 fprintf(logfile, "\n");
6020 qemu_log_unlock(logfile);
6021 }
6022 }
6023
6024 #ifdef CONFIG_DEBUG_TCG
6025 /* Ensure all labels referenced have been emitted. */
6026 {
6027 TCGLabel *l;
6028 bool error = false;
6029
6030 QSIMPLEQ_FOREACH(l, &s->labels, next) {
6031 if (unlikely(!l->present) && !QSIMPLEQ_EMPTY(&l->branches)) {
6032 qemu_log_mask(CPU_LOG_TB_OP,
6033 "$L%d referenced but not present.\n", l->id);
6034 error = true;
6035 }
6036 }
6037 assert(!error);
6038 }
6039 #endif
6040
6041 tcg_optimize(s);
6042
6043 reachable_code_pass(s);
6044 liveness_pass_0(s);
6045 liveness_pass_1(s);
6046
6047 if (s->nb_indirects > 0) {
6048 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
6049 && qemu_log_in_addr_range(pc_start))) {
6050 FILE *logfile = qemu_log_trylock();
6051 if (logfile) {
6052 fprintf(logfile, "OP before indirect lowering:\n");
6053 tcg_dump_ops(s, logfile, false);
6054 fprintf(logfile, "\n");
6055 qemu_log_unlock(logfile);
6056 }
6057 }
6058
6059 /* Replace indirect temps with direct temps. */
6060 if (liveness_pass_2(s)) {
6061 /* If changes were made, re-run liveness. */
6062 liveness_pass_1(s);
6063 }
6064 }
6065
6066 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
6067 && qemu_log_in_addr_range(pc_start))) {
6068 FILE *logfile = qemu_log_trylock();
6069 if (logfile) {
6070 fprintf(logfile, "OP after optimization and liveness analysis:\n");
6071 tcg_dump_ops(s, logfile, true);
6072 fprintf(logfile, "\n");
6073 qemu_log_unlock(logfile);
6074 }
6075 }
6076
6077 /* Initialize goto_tb jump offsets. */
6078 tb->jmp_reset_offset[0] = TB_JMP_OFFSET_INVALID;
6079 tb->jmp_reset_offset[1] = TB_JMP_OFFSET_INVALID;
6080 tb->jmp_insn_offset[0] = TB_JMP_OFFSET_INVALID;
6081 tb->jmp_insn_offset[1] = TB_JMP_OFFSET_INVALID;
6082
6083 tcg_reg_alloc_start(s);
6084
6085 /*
6086 * Reset the buffer pointers when restarting after overflow.
6087 * TODO: Move this into translate-all.c with the rest of the
6088 * buffer management. Having only this done here is confusing.
6089 */
6090 s->code_buf = tcg_splitwx_to_rw(tb->tc.ptr);
6091 s->code_ptr = s->code_buf;
6092
6093 #ifdef TCG_TARGET_NEED_LDST_LABELS
6094 QSIMPLEQ_INIT(&s->ldst_labels);
6095 #endif
6096 #ifdef TCG_TARGET_NEED_POOL_LABELS
6097 s->pool_labels = NULL;
6098 #endif
6099
6100 start_words = s->insn_start_words;
6101 s->gen_insn_data =
6102 tcg_malloc(sizeof(uint64_t) * s->gen_tb->icount * start_words);
6103
6104 tcg_out_tb_start(s);
6105
6106 num_insns = -1;
6107 QTAILQ_FOREACH(op, &s->ops, link) {
6108 TCGOpcode opc = op->opc;
6109
6110 switch (opc) {
6111 case INDEX_op_mov_i32:
6112 case INDEX_op_mov_i64:
6113 case INDEX_op_mov_vec:
6114 tcg_reg_alloc_mov(s, op);
6115 break;
6116 case INDEX_op_dup_vec:
6117 tcg_reg_alloc_dup(s, op);
6118 break;
6119 case INDEX_op_insn_start:
6120 if (num_insns >= 0) {
6121 size_t off = tcg_current_code_size(s);
6122 s->gen_insn_end_off[num_insns] = off;
6123 /* Assert that we do not overflow our stored offset. */
6124 assert(s->gen_insn_end_off[num_insns] == off);
6125 }
6126 num_insns++;
6127 for (i = 0; i < start_words; ++i) {
6128 s->gen_insn_data[num_insns * start_words + i] =
6129 tcg_get_insn_start_param(op, i);
6130 }
6131 break;
6132 case INDEX_op_discard:
6133 temp_dead(s, arg_temp(op->args[0]));
6134 break;
6135 case INDEX_op_set_label:
6136 tcg_reg_alloc_bb_end(s, s->reserved_regs);
6137 tcg_out_label(s, arg_label(op->args[0]));
6138 break;
6139 case INDEX_op_call:
6140 tcg_reg_alloc_call(s, op);
6141 break;
6142 case INDEX_op_exit_tb:
6143 tcg_out_exit_tb(s, op->args[0]);
6144 break;
6145 case INDEX_op_goto_tb:
6146 tcg_out_goto_tb(s, op->args[0]);
6147 break;
6148 case INDEX_op_dup2_vec:
6149 if (tcg_reg_alloc_dup2(s, op)) {
6150 break;
6151 }
6152 /* fall through */
6153 default:
6154 /* Sanity check that we've not introduced any unhandled opcodes. */
6155 tcg_debug_assert(tcg_op_supported(opc));
6156 /* Note: in order to speed up the code, it would be much
6157 faster to have specialized register allocator functions for
6158 some common argument patterns */
6159 tcg_reg_alloc_op(s, op);
6160 break;
6161 }
6162 /* Test for (pending) buffer overflow. The assumption is that any
6163 one operation beginning below the high water mark cannot overrun
6164 the buffer completely. Thus we can test for overflow after
6165 generating code without having to check during generation. */
6166 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
6167 return -1;
6168 }
6169 /* Test for TB overflow, as seen by gen_insn_end_off. */
6170 if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) {
6171 return -2;
6172 }
6173 }
6174 tcg_debug_assert(num_insns + 1 == s->gen_tb->icount);
6175 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
6176
6177 /* Generate TB finalization at the end of block */
6178 #ifdef TCG_TARGET_NEED_LDST_LABELS
6179 i = tcg_out_ldst_finalize(s);
6180 if (i < 0) {
6181 return i;
6182 }
6183 #endif
6184 #ifdef TCG_TARGET_NEED_POOL_LABELS
6185 i = tcg_out_pool_finalize(s);
6186 if (i < 0) {
6187 return i;
6188 }
6189 #endif
6190 if (!tcg_resolve_relocs(s)) {
6191 return -2;
6192 }
6193
6194 #ifndef CONFIG_TCG_INTERPRETER
6195 /* flush instruction cache */
6196 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
6197 (uintptr_t)s->code_buf,
6198 tcg_ptr_byte_diff(s->code_ptr, s->code_buf));
6199 #endif
6200
6201 return tcg_current_code_size(s);
6202 }
6203
6204 #ifdef ELF_HOST_MACHINE
6205 /* In order to use this feature, the backend needs to do three things:
6206
6207 (1) Define ELF_HOST_MACHINE to indicate both what value to
6208 put into the ELF image and to indicate support for the feature.
6209
6210 (2) Define tcg_register_jit. This should create a buffer containing
6211 the contents of a .debug_frame section that describes the post-
6212 prologue unwind info for the tcg machine.
6213
6214 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
6215 */
6216
6217 /* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
6218 typedef enum {
6219 JIT_NOACTION = 0,
6220 JIT_REGISTER_FN,
6221 JIT_UNREGISTER_FN
6222 } jit_actions_t;
6223
6224 struct jit_code_entry {
6225 struct jit_code_entry *next_entry;
6226 struct jit_code_entry *prev_entry;
6227 const void *symfile_addr;
6228 uint64_t symfile_size;
6229 };
6230
6231 struct jit_descriptor {
6232 uint32_t version;
6233 uint32_t action_flag;
6234 struct jit_code_entry *relevant_entry;
6235 struct jit_code_entry *first_entry;
6236 };
6237
6238 void __jit_debug_register_code(void) __attribute__((noinline));
6239 void __jit_debug_register_code(void)
6240 {
6241 asm("");
6242 }
6243
6244 /* Must statically initialize the version, because GDB may check
6245 the version before we can set it. */
6246 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
6247
6248 /* End GDB interface. */
6249
6250 static int find_string(const char *strtab, const char *str)
6251 {
6252 const char *p = strtab + 1;
6253
6254 while (1) {
6255 if (strcmp(p, str) == 0) {
6256 return p - strtab;
6257 }
6258 p += strlen(p) + 1;
6259 }
6260 }
6261
6262 static void tcg_register_jit_int(const void *buf_ptr, size_t buf_size,
6263 const void *debug_frame,
6264 size_t debug_frame_size)
6265 {
6266 struct __attribute__((packed)) DebugInfo {
6267 uint32_t len;
6268 uint16_t version;
6269 uint32_t abbrev;
6270 uint8_t ptr_size;
6271 uint8_t cu_die;
6272 uint16_t cu_lang;
6273 uintptr_t cu_low_pc;
6274 uintptr_t cu_high_pc;
6275 uint8_t fn_die;
6276 char fn_name[16];
6277 uintptr_t fn_low_pc;
6278 uintptr_t fn_high_pc;
6279 uint8_t cu_eoc;
6280 };
6281
6282 struct ElfImage {
6283 ElfW(Ehdr) ehdr;
6284 ElfW(Phdr) phdr;
6285 ElfW(Shdr) shdr[7];
6286 ElfW(Sym) sym[2];
6287 struct DebugInfo di;
6288 uint8_t da[24];
6289 char str[80];
6290 };
6291
6292 struct ElfImage *img;
6293
6294 static const struct ElfImage img_template = {
6295 .ehdr = {
6296 .e_ident[EI_MAG0] = ELFMAG0,
6297 .e_ident[EI_MAG1] = ELFMAG1,
6298 .e_ident[EI_MAG2] = ELFMAG2,
6299 .e_ident[EI_MAG3] = ELFMAG3,
6300 .e_ident[EI_CLASS] = ELF_CLASS,
6301 .e_ident[EI_DATA] = ELF_DATA,
6302 .e_ident[EI_VERSION] = EV_CURRENT,
6303 .e_type = ET_EXEC,
6304 .e_machine = ELF_HOST_MACHINE,
6305 .e_version = EV_CURRENT,
6306 .e_phoff = offsetof(struct ElfImage, phdr),
6307 .e_shoff = offsetof(struct ElfImage, shdr),
6308 .e_ehsize = sizeof(ElfW(Shdr)),
6309 .e_phentsize = sizeof(ElfW(Phdr)),
6310 .e_phnum = 1,
6311 .e_shentsize = sizeof(ElfW(Shdr)),
6312 .e_shnum = ARRAY_SIZE(img->shdr),
6313 .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
6314 #ifdef ELF_HOST_FLAGS
6315 .e_flags = ELF_HOST_FLAGS,
6316 #endif
6317 #ifdef ELF_OSABI
6318 .e_ident[EI_OSABI] = ELF_OSABI,
6319 #endif
6320 },
6321 .phdr = {
6322 .p_type = PT_LOAD,
6323 .p_flags = PF_X,
6324 },
6325 .shdr = {
6326 [0] = { .sh_type = SHT_NULL },
6327 /* Trick: The contents of code_gen_buffer are not present in
6328 this fake ELF file; that got allocated elsewhere. Therefore
6329 we mark .text as SHT_NOBITS (similar to .bss) so that readers
6330 will not look for contents. We can record any address. */
6331 [1] = { /* .text */
6332 .sh_type = SHT_NOBITS,
6333 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
6334 },
6335 [2] = { /* .debug_info */
6336 .sh_type = SHT_PROGBITS,
6337 .sh_offset = offsetof(struct ElfImage, di),
6338 .sh_size = sizeof(struct DebugInfo),
6339 },
6340 [3] = { /* .debug_abbrev */
6341 .sh_type = SHT_PROGBITS,
6342 .sh_offset = offsetof(struct ElfImage, da),
6343 .sh_size = sizeof(img->da),
6344 },
6345 [4] = { /* .debug_frame */
6346 .sh_type = SHT_PROGBITS,
6347 .sh_offset = sizeof(struct ElfImage),
6348 },
6349 [5] = { /* .symtab */
6350 .sh_type = SHT_SYMTAB,
6351 .sh_offset = offsetof(struct ElfImage, sym),
6352 .sh_size = sizeof(img->sym),
6353 .sh_info = 1,
6354 .sh_link = ARRAY_SIZE(img->shdr) - 1,
6355 .sh_entsize = sizeof(ElfW(Sym)),
6356 },
6357 [6] = { /* .strtab */
6358 .sh_type = SHT_STRTAB,
6359 .sh_offset = offsetof(struct ElfImage, str),
6360 .sh_size = sizeof(img->str),
6361 }
6362 },
6363 .sym = {
6364 [1] = { /* code_gen_buffer */
6365 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
6366 .st_shndx = 1,
6367 }
6368 },
6369 .di = {
6370 .len = sizeof(struct DebugInfo) - 4,
6371 .version = 2,
6372 .ptr_size = sizeof(void *),
6373 .cu_die = 1,
6374 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
6375 .fn_die = 2,
6376 .fn_name = "code_gen_buffer"
6377 },
6378 .da = {
6379 1, /* abbrev number (the cu) */
6380 0x11, 1, /* DW_TAG_compile_unit, has children */
6381 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
6382 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
6383 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
6384 0, 0, /* end of abbrev */
6385 2, /* abbrev number (the fn) */
6386 0x2e, 0, /* DW_TAG_subprogram, no children */
6387 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
6388 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
6389 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
6390 0, 0, /* end of abbrev */
6391 0 /* no more abbrev */
6392 },
6393 .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
6394 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
6395 };
6396
6397 /* We only need a single jit entry; statically allocate it. */
6398 static struct jit_code_entry one_entry;
6399
6400 uintptr_t buf = (uintptr_t)buf_ptr;
6401 size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
6402 DebugFrameHeader *dfh;
6403
6404 img = g_malloc(img_size);
6405 *img = img_template;
6406
6407 img->phdr.p_vaddr = buf;
6408 img->phdr.p_paddr = buf;
6409 img->phdr.p_memsz = buf_size;
6410
6411 img->shdr[1].sh_name = find_string(img->str, ".text");
6412 img->shdr[1].sh_addr = buf;
6413 img->shdr[1].sh_size = buf_size;
6414
6415 img->shdr[2].sh_name = find_string(img->str, ".debug_info");
6416 img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
6417
6418 img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
6419 img->shdr[4].sh_size = debug_frame_size;
6420
6421 img->shdr[5].sh_name = find_string(img->str, ".symtab");
6422 img->shdr[6].sh_name = find_string(img->str, ".strtab");
6423
6424 img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
6425 img->sym[1].st_value = buf;
6426 img->sym[1].st_size = buf_size;
6427
6428 img->di.cu_low_pc = buf;
6429 img->di.cu_high_pc = buf + buf_size;
6430 img->di.fn_low_pc = buf;
6431 img->di.fn_high_pc = buf + buf_size;
6432
6433 dfh = (DebugFrameHeader *)(img + 1);
6434 memcpy(dfh, debug_frame, debug_frame_size);
6435 dfh->fde.func_start = buf;
6436 dfh->fde.func_len = buf_size;
6437
6438 #ifdef DEBUG_JIT
6439 /* Enable this block to be able to debug the ELF image file creation.
6440 One can use readelf, objdump, or other inspection utilities. */
6441 {
6442 g_autofree char *jit = g_strdup_printf("%s/qemu.jit", g_get_tmp_dir());
6443 FILE *f = fopen(jit, "w+b");
6444 if (f) {
6445 if (fwrite(img, img_size, 1, f) != img_size) {
6446 /* Avoid stupid unused return value warning for fwrite. */
6447 }
6448 fclose(f);
6449 }
6450 }
6451 #endif
6452
6453 one_entry.symfile_addr = img;
6454 one_entry.symfile_size = img_size;
6455
6456 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
6457 __jit_debug_descriptor.relevant_entry = &one_entry;
6458 __jit_debug_descriptor.first_entry = &one_entry;
6459 __jit_debug_register_code();
6460 }
6461 #else
6462 /* No support for the feature. Provide the entry point expected by exec.c,
6463 and implement the internal function we declared earlier. */
6464
6465 static void tcg_register_jit_int(const void *buf, size_t size,
6466 const void *debug_frame,
6467 size_t debug_frame_size)
6468 {
6469 }
6470
6471 void tcg_register_jit(const void *buf, size_t buf_size)
6472 {
6473 }
6474 #endif /* ELF_HOST_MACHINE */
6475
6476 #if !TCG_TARGET_MAYBE_vec
6477 void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
6478 {
6479 g_assert_not_reached();
6480 }
6481 #endif