xref: /qemu/include/tcg/tcg.h (revision 747bd69d)
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 #ifndef TCG_H
26 #define TCG_H
27 
28 #include "cpu.h"
29 #include "exec/memop.h"
30 #include "exec/memopidx.h"
31 #include "qemu/bitops.h"
32 #include "qemu/plugin.h"
33 #include "qemu/queue.h"
34 #include "tcg/tcg-mo.h"
35 #include "tcg-target-reg-bits.h"
36 #include "tcg-target.h"
37 #include "tcg/tcg-cond.h"
38 #include "tcg/debug-assert.h"
39 
40 /* XXX: make safe guess about sizes */
41 #define MAX_OP_PER_INSTR 266
42 
43 #define MAX_CALL_IARGS  7
44 
45 #define CPU_TEMP_BUF_NLONGS 128
46 #define TCG_STATIC_FRAME_SIZE  (CPU_TEMP_BUF_NLONGS * sizeof(long))
47 
48 #if TCG_TARGET_REG_BITS == 32
49 typedef int32_t tcg_target_long;
50 typedef uint32_t tcg_target_ulong;
51 #define TCG_PRIlx PRIx32
52 #define TCG_PRIld PRId32
53 #elif TCG_TARGET_REG_BITS == 64
54 typedef int64_t tcg_target_long;
55 typedef uint64_t tcg_target_ulong;
56 #define TCG_PRIlx PRIx64
57 #define TCG_PRIld PRId64
58 #else
59 #error unsupported
60 #endif
61 
62 #if TCG_TARGET_NB_REGS <= 32
63 typedef uint32_t TCGRegSet;
64 #elif TCG_TARGET_NB_REGS <= 64
65 typedef uint64_t TCGRegSet;
66 #else
67 #error unsupported
68 #endif
69 
70 #if TCG_TARGET_REG_BITS == 32
71 /* Turn some undef macros into false macros.  */
72 #define TCG_TARGET_HAS_extrl_i64_i32    0
73 #define TCG_TARGET_HAS_extrh_i64_i32    0
74 #define TCG_TARGET_HAS_div_i64          0
75 #define TCG_TARGET_HAS_rem_i64          0
76 #define TCG_TARGET_HAS_div2_i64         0
77 #define TCG_TARGET_HAS_rot_i64          0
78 #define TCG_TARGET_HAS_ext8s_i64        0
79 #define TCG_TARGET_HAS_ext16s_i64       0
80 #define TCG_TARGET_HAS_ext32s_i64       0
81 #define TCG_TARGET_HAS_ext8u_i64        0
82 #define TCG_TARGET_HAS_ext16u_i64       0
83 #define TCG_TARGET_HAS_ext32u_i64       0
84 #define TCG_TARGET_HAS_bswap16_i64      0
85 #define TCG_TARGET_HAS_bswap32_i64      0
86 #define TCG_TARGET_HAS_bswap64_i64      0
87 #define TCG_TARGET_HAS_neg_i64          0
88 #define TCG_TARGET_HAS_not_i64          0
89 #define TCG_TARGET_HAS_andc_i64         0
90 #define TCG_TARGET_HAS_orc_i64          0
91 #define TCG_TARGET_HAS_eqv_i64          0
92 #define TCG_TARGET_HAS_nand_i64         0
93 #define TCG_TARGET_HAS_nor_i64          0
94 #define TCG_TARGET_HAS_clz_i64          0
95 #define TCG_TARGET_HAS_ctz_i64          0
96 #define TCG_TARGET_HAS_ctpop_i64        0
97 #define TCG_TARGET_HAS_deposit_i64      0
98 #define TCG_TARGET_HAS_extract_i64      0
99 #define TCG_TARGET_HAS_sextract_i64     0
100 #define TCG_TARGET_HAS_extract2_i64     0
101 #define TCG_TARGET_HAS_movcond_i64      0
102 #define TCG_TARGET_HAS_add2_i64         0
103 #define TCG_TARGET_HAS_sub2_i64         0
104 #define TCG_TARGET_HAS_mulu2_i64        0
105 #define TCG_TARGET_HAS_muls2_i64        0
106 #define TCG_TARGET_HAS_muluh_i64        0
107 #define TCG_TARGET_HAS_mulsh_i64        0
108 /* Turn some undef macros into true macros.  */
109 #define TCG_TARGET_HAS_add2_i32         1
110 #define TCG_TARGET_HAS_sub2_i32         1
111 #endif
112 
113 #ifndef TCG_TARGET_deposit_i32_valid
114 #define TCG_TARGET_deposit_i32_valid(ofs, len) 1
115 #endif
116 #ifndef TCG_TARGET_deposit_i64_valid
117 #define TCG_TARGET_deposit_i64_valid(ofs, len) 1
118 #endif
119 #ifndef TCG_TARGET_extract_i32_valid
120 #define TCG_TARGET_extract_i32_valid(ofs, len) 1
121 #endif
122 #ifndef TCG_TARGET_extract_i64_valid
123 #define TCG_TARGET_extract_i64_valid(ofs, len) 1
124 #endif
125 
126 /* Only one of DIV or DIV2 should be defined.  */
127 #if defined(TCG_TARGET_HAS_div_i32)
128 #define TCG_TARGET_HAS_div2_i32         0
129 #elif defined(TCG_TARGET_HAS_div2_i32)
130 #define TCG_TARGET_HAS_div_i32          0
131 #define TCG_TARGET_HAS_rem_i32          0
132 #endif
133 #if defined(TCG_TARGET_HAS_div_i64)
134 #define TCG_TARGET_HAS_div2_i64         0
135 #elif defined(TCG_TARGET_HAS_div2_i64)
136 #define TCG_TARGET_HAS_div_i64          0
137 #define TCG_TARGET_HAS_rem_i64          0
138 #endif
139 
140 #if !defined(TCG_TARGET_HAS_v64) \
141     && !defined(TCG_TARGET_HAS_v128) \
142     && !defined(TCG_TARGET_HAS_v256)
143 #define TCG_TARGET_MAYBE_vec            0
144 #define TCG_TARGET_HAS_abs_vec          0
145 #define TCG_TARGET_HAS_neg_vec          0
146 #define TCG_TARGET_HAS_not_vec          0
147 #define TCG_TARGET_HAS_andc_vec         0
148 #define TCG_TARGET_HAS_orc_vec          0
149 #define TCG_TARGET_HAS_nand_vec         0
150 #define TCG_TARGET_HAS_nor_vec          0
151 #define TCG_TARGET_HAS_eqv_vec          0
152 #define TCG_TARGET_HAS_roti_vec         0
153 #define TCG_TARGET_HAS_rots_vec         0
154 #define TCG_TARGET_HAS_rotv_vec         0
155 #define TCG_TARGET_HAS_shi_vec          0
156 #define TCG_TARGET_HAS_shs_vec          0
157 #define TCG_TARGET_HAS_shv_vec          0
158 #define TCG_TARGET_HAS_mul_vec          0
159 #define TCG_TARGET_HAS_sat_vec          0
160 #define TCG_TARGET_HAS_minmax_vec       0
161 #define TCG_TARGET_HAS_bitsel_vec       0
162 #define TCG_TARGET_HAS_cmpsel_vec       0
163 #else
164 #define TCG_TARGET_MAYBE_vec            1
165 #endif
166 #ifndef TCG_TARGET_HAS_v64
167 #define TCG_TARGET_HAS_v64              0
168 #endif
169 #ifndef TCG_TARGET_HAS_v128
170 #define TCG_TARGET_HAS_v128             0
171 #endif
172 #ifndef TCG_TARGET_HAS_v256
173 #define TCG_TARGET_HAS_v256             0
174 #endif
175 
176 typedef enum TCGOpcode {
177 #define DEF(name, oargs, iargs, cargs, flags) INDEX_op_ ## name,
178 #include "tcg/tcg-opc.h"
179 #undef DEF
180     NB_OPS,
181 } TCGOpcode;
182 
183 #define tcg_regset_set_reg(d, r)   ((d) |= (TCGRegSet)1 << (r))
184 #define tcg_regset_reset_reg(d, r) ((d) &= ~((TCGRegSet)1 << (r)))
185 #define tcg_regset_test_reg(d, r)  (((d) >> (r)) & 1)
186 
187 #ifndef TCG_TARGET_INSN_UNIT_SIZE
188 # error "Missing TCG_TARGET_INSN_UNIT_SIZE"
189 #elif TCG_TARGET_INSN_UNIT_SIZE == 1
190 typedef uint8_t tcg_insn_unit;
191 #elif TCG_TARGET_INSN_UNIT_SIZE == 2
192 typedef uint16_t tcg_insn_unit;
193 #elif TCG_TARGET_INSN_UNIT_SIZE == 4
194 typedef uint32_t tcg_insn_unit;
195 #elif TCG_TARGET_INSN_UNIT_SIZE == 8
196 typedef uint64_t tcg_insn_unit;
197 #else
198 /* The port better have done this.  */
199 #endif
200 
201 typedef struct TCGRelocation TCGRelocation;
202 struct TCGRelocation {
203     QSIMPLEQ_ENTRY(TCGRelocation) next;
204     tcg_insn_unit *ptr;
205     intptr_t addend;
206     int type;
207 };
208 
209 typedef struct TCGOp TCGOp;
210 typedef struct TCGLabelUse TCGLabelUse;
211 struct TCGLabelUse {
212     QSIMPLEQ_ENTRY(TCGLabelUse) next;
213     TCGOp *op;
214 };
215 
216 typedef struct TCGLabel TCGLabel;
217 struct TCGLabel {
218     bool present;
219     bool has_value;
220     uint16_t id;
221     union {
222         uintptr_t value;
223         const tcg_insn_unit *value_ptr;
224     } u;
225     QSIMPLEQ_HEAD(, TCGLabelUse) branches;
226     QSIMPLEQ_HEAD(, TCGRelocation) relocs;
227     QSIMPLEQ_ENTRY(TCGLabel) next;
228 };
229 
230 typedef struct TCGPool {
231     struct TCGPool *next;
232     int size;
233     uint8_t data[] __attribute__ ((aligned));
234 } TCGPool;
235 
236 #define TCG_POOL_CHUNK_SIZE 32768
237 
238 #define TCG_MAX_TEMPS 512
239 #define TCG_MAX_INSNS 512
240 
241 /* when the size of the arguments of a called function is smaller than
242    this value, they are statically allocated in the TB stack frame */
243 #define TCG_STATIC_CALL_ARGS_SIZE 128
244 
245 typedef enum TCGType {
246     TCG_TYPE_I32,
247     TCG_TYPE_I64,
248     TCG_TYPE_I128,
249 
250     TCG_TYPE_V64,
251     TCG_TYPE_V128,
252     TCG_TYPE_V256,
253 
254     /* Number of different types (integer not enum) */
255 #define TCG_TYPE_COUNT  (TCG_TYPE_V256 + 1)
256 
257     /* An alias for the size of the host register.  */
258 #if TCG_TARGET_REG_BITS == 32
259     TCG_TYPE_REG = TCG_TYPE_I32,
260 #else
261     TCG_TYPE_REG = TCG_TYPE_I64,
262 #endif
263 
264     /* An alias for the size of the native pointer.  */
265 #if UINTPTR_MAX == UINT32_MAX
266     TCG_TYPE_PTR = TCG_TYPE_I32,
267 #else
268     TCG_TYPE_PTR = TCG_TYPE_I64,
269 #endif
270 } TCGType;
271 
272 /**
273  * tcg_type_size
274  * @t: type
275  *
276  * Return the size of the type in bytes.
277  */
278 static inline int tcg_type_size(TCGType t)
279 {
280     unsigned i = t;
281     if (i >= TCG_TYPE_V64) {
282         tcg_debug_assert(i < TCG_TYPE_COUNT);
283         i -= TCG_TYPE_V64 - 1;
284     }
285     return 4 << i;
286 }
287 
288 /**
289  * get_alignment_bits
290  * @memop: MemOp value
291  *
292  * Extract the alignment size from the memop.
293  */
294 static inline unsigned get_alignment_bits(MemOp memop)
295 {
296     unsigned a = memop & MO_AMASK;
297 
298     if (a == MO_UNALN) {
299         /* No alignment required.  */
300         a = 0;
301     } else if (a == MO_ALIGN) {
302         /* A natural alignment requirement.  */
303         a = memop & MO_SIZE;
304     } else {
305         /* A specific alignment requirement.  */
306         a = a >> MO_ASHIFT;
307     }
308 #if defined(CONFIG_SOFTMMU)
309     /* The requested alignment cannot overlap the TLB flags.  */
310     tcg_debug_assert((TLB_FLAGS_MASK & ((1 << a) - 1)) == 0);
311 #endif
312     return a;
313 }
314 
315 typedef tcg_target_ulong TCGArg;
316 
317 /* Define type and accessor macros for TCG variables.
318 
319    TCG variables are the inputs and outputs of TCG ops, as described
320    in tcg/README. Target CPU front-end code uses these types to deal
321    with TCG variables as it emits TCG code via the tcg_gen_* functions.
322    They come in several flavours:
323     * TCGv_i32  : 32 bit integer type
324     * TCGv_i64  : 64 bit integer type
325     * TCGv_i128 : 128 bit integer type
326     * TCGv_ptr  : a host pointer type
327     * TCGv_vec  : a host vector type; the exact size is not exposed
328                   to the CPU front-end code.
329     * TCGv      : an integer type the same size as target_ulong
330                   (an alias for either TCGv_i32 or TCGv_i64)
331    The compiler's type checking will complain if you mix them
332    up and pass the wrong sized TCGv to a function.
333 
334    Users of tcg_gen_* don't need to know about any of the internal
335    details of these, and should treat them as opaque types.
336    You won't be able to look inside them in a debugger either.
337 
338    Internal implementation details follow:
339 
340    Note that there is no definition of the structs TCGv_i32_d etc anywhere.
341    This is deliberate, because the values we store in variables of type
342    TCGv_i32 are not really pointers-to-structures. They're just small
343    integers, but keeping them in pointer types like this means that the
344    compiler will complain if you accidentally pass a TCGv_i32 to a
345    function which takes a TCGv_i64, and so on. Only the internals of
346    TCG need to care about the actual contents of the types.  */
347 
348 typedef struct TCGv_i32_d *TCGv_i32;
349 typedef struct TCGv_i64_d *TCGv_i64;
350 typedef struct TCGv_i128_d *TCGv_i128;
351 typedef struct TCGv_ptr_d *TCGv_ptr;
352 typedef struct TCGv_vec_d *TCGv_vec;
353 typedef TCGv_ptr TCGv_env;
354 
355 /* call flags */
356 /* Helper does not read globals (either directly or through an exception). It
357    implies TCG_CALL_NO_WRITE_GLOBALS. */
358 #define TCG_CALL_NO_READ_GLOBALS    0x0001
359 /* Helper does not write globals */
360 #define TCG_CALL_NO_WRITE_GLOBALS   0x0002
361 /* Helper can be safely suppressed if the return value is not used. */
362 #define TCG_CALL_NO_SIDE_EFFECTS    0x0004
363 /* Helper is G_NORETURN.  */
364 #define TCG_CALL_NO_RETURN          0x0008
365 /* Helper is part of Plugins.  */
366 #define TCG_CALL_PLUGIN             0x0010
367 
368 /* convenience version of most used call flags */
369 #define TCG_CALL_NO_RWG         TCG_CALL_NO_READ_GLOBALS
370 #define TCG_CALL_NO_WG          TCG_CALL_NO_WRITE_GLOBALS
371 #define TCG_CALL_NO_SE          TCG_CALL_NO_SIDE_EFFECTS
372 #define TCG_CALL_NO_RWG_SE      (TCG_CALL_NO_RWG | TCG_CALL_NO_SE)
373 #define TCG_CALL_NO_WG_SE       (TCG_CALL_NO_WG | TCG_CALL_NO_SE)
374 
375 /*
376  * Flags for the bswap opcodes.
377  * If IZ, the input is zero-extended, otherwise unknown.
378  * If OZ or OS, the output is zero- or sign-extended respectively,
379  * otherwise the high bits are undefined.
380  */
381 enum {
382     TCG_BSWAP_IZ = 1,
383     TCG_BSWAP_OZ = 2,
384     TCG_BSWAP_OS = 4,
385 };
386 
387 typedef enum TCGTempVal {
388     TEMP_VAL_DEAD,
389     TEMP_VAL_REG,
390     TEMP_VAL_MEM,
391     TEMP_VAL_CONST,
392 } TCGTempVal;
393 
394 typedef enum TCGTempKind {
395     /*
396      * Temp is dead at the end of the extended basic block (EBB),
397      * the single-entry multiple-exit region that falls through
398      * conditional branches.
399      */
400     TEMP_EBB,
401     /* Temp is live across the entire translation block, but dead at end. */
402     TEMP_TB,
403     /* Temp is live across the entire translation block, and between them. */
404     TEMP_GLOBAL,
405     /* Temp is in a fixed register. */
406     TEMP_FIXED,
407     /* Temp is a fixed constant. */
408     TEMP_CONST,
409 } TCGTempKind;
410 
411 typedef struct TCGTemp {
412     TCGReg reg:8;
413     TCGTempVal val_type:8;
414     TCGType base_type:8;
415     TCGType type:8;
416     TCGTempKind kind:3;
417     unsigned int indirect_reg:1;
418     unsigned int indirect_base:1;
419     unsigned int mem_coherent:1;
420     unsigned int mem_allocated:1;
421     unsigned int temp_allocated:1;
422     unsigned int temp_subindex:1;
423 
424     int64_t val;
425     struct TCGTemp *mem_base;
426     intptr_t mem_offset;
427     const char *name;
428 
429     /* Pass-specific information that can be stored for a temporary.
430        One word worth of integer data, and one pointer to data
431        allocated separately.  */
432     uintptr_t state;
433     void *state_ptr;
434 } TCGTemp;
435 
436 typedef struct TCGContext TCGContext;
437 
438 typedef struct TCGTempSet {
439     unsigned long l[BITS_TO_LONGS(TCG_MAX_TEMPS)];
440 } TCGTempSet;
441 
442 /*
443  * With 1 128-bit output, a 32-bit host requires 4 output parameters,
444  * which leaves a maximum of 28 other slots.  Which is enough for 7
445  * 128-bit operands.
446  */
447 #define DEAD_ARG  (1 << 4)
448 #define SYNC_ARG  (1 << 0)
449 typedef uint32_t TCGLifeData;
450 
451 struct TCGOp {
452     TCGOpcode opc   : 8;
453     unsigned nargs  : 8;
454 
455     /* Parameters for this opcode.  See below.  */
456     unsigned param1 : 8;
457     unsigned param2 : 8;
458 
459     /* Lifetime data of the operands.  */
460     TCGLifeData life;
461 
462     /* Next and previous opcodes.  */
463     QTAILQ_ENTRY(TCGOp) link;
464 
465     /* Register preferences for the output(s).  */
466     TCGRegSet output_pref[2];
467 
468     /* Arguments for the opcode.  */
469     TCGArg args[];
470 };
471 
472 #define TCGOP_CALLI(X)    (X)->param1
473 #define TCGOP_CALLO(X)    (X)->param2
474 
475 #define TCGOP_VECL(X)     (X)->param1
476 #define TCGOP_VECE(X)     (X)->param2
477 
478 /* Make sure operands fit in the bitfields above.  */
479 QEMU_BUILD_BUG_ON(NB_OPS > (1 << 8));
480 
481 static inline TCGRegSet output_pref(const TCGOp *op, unsigned i)
482 {
483     return i < ARRAY_SIZE(op->output_pref) ? op->output_pref[i] : 0;
484 }
485 
486 typedef struct TCGProfile {
487     int64_t cpu_exec_time;
488     int64_t tb_count1;
489     int64_t tb_count;
490     int64_t op_count; /* total insn count */
491     int op_count_max; /* max insn per TB */
492     int temp_count_max;
493     int64_t temp_count;
494     int64_t del_op_count;
495     int64_t code_in_len;
496     int64_t code_out_len;
497     int64_t search_out_len;
498     int64_t interm_time;
499     int64_t code_time;
500     int64_t la_time;
501     int64_t opt_time;
502     int64_t restore_count;
503     int64_t restore_time;
504     int64_t table_op_count[NB_OPS];
505 } TCGProfile;
506 
507 struct TCGContext {
508     uint8_t *pool_cur, *pool_end;
509     TCGPool *pool_first, *pool_current, *pool_first_large;
510     int nb_labels;
511     int nb_globals;
512     int nb_temps;
513     int nb_indirects;
514     int nb_ops;
515     TCGType addr_type;            /* TCG_TYPE_I32 or TCG_TYPE_I64 */
516 
517 #ifdef CONFIG_SOFTMMU
518     int tlb_fast_offset;
519     int page_mask;
520     uint8_t page_bits;
521     uint8_t tlb_dyn_max_bits;
522 #endif
523     uint8_t insn_start_words;
524 
525     TCGRegSet reserved_regs;
526     intptr_t current_frame_offset;
527     intptr_t frame_start;
528     intptr_t frame_end;
529     TCGTemp *frame_temp;
530 
531     TranslationBlock *gen_tb;     /* tb for which code is being generated */
532     tcg_insn_unit *code_buf;      /* pointer for start of tb */
533     tcg_insn_unit *code_ptr;      /* pointer for running end of tb */
534 
535 #ifdef CONFIG_PROFILER
536     TCGProfile prof;
537 #endif
538 
539 #ifdef CONFIG_DEBUG_TCG
540     int goto_tb_issue_mask;
541     const TCGOpcode *vecop_list;
542 #endif
543 
544     /* Code generation.  Note that we specifically do not use tcg_insn_unit
545        here, because there's too much arithmetic throughout that relies
546        on addition and subtraction working on bytes.  Rely on the GCC
547        extension that allows arithmetic on void*.  */
548     void *code_gen_buffer;
549     size_t code_gen_buffer_size;
550     void *code_gen_ptr;
551     void *data_gen_ptr;
552 
553     /* Threshold to flush the translated code buffer.  */
554     void *code_gen_highwater;
555 
556     /* Track which vCPU triggers events */
557     CPUState *cpu;                      /* *_trans */
558 
559     /* These structures are private to tcg-target.c.inc.  */
560 #ifdef TCG_TARGET_NEED_LDST_LABELS
561     QSIMPLEQ_HEAD(, TCGLabelQemuLdst) ldst_labels;
562 #endif
563 #ifdef TCG_TARGET_NEED_POOL_LABELS
564     struct TCGLabelPoolData *pool_labels;
565 #endif
566 
567     TCGLabel *exitreq_label;
568 
569 #ifdef CONFIG_PLUGIN
570     /*
571      * We keep one plugin_tb struct per TCGContext. Note that on every TB
572      * translation we clear but do not free its contents; this way we
573      * avoid a lot of malloc/free churn, since after a few TB's it's
574      * unlikely that we'll need to allocate either more instructions or more
575      * space for instructions (for variable-instruction-length ISAs).
576      */
577     struct qemu_plugin_tb *plugin_tb;
578 
579     /* descriptor of the instruction being translated */
580     struct qemu_plugin_insn *plugin_insn;
581 #endif
582 
583     GHashTable *const_table[TCG_TYPE_COUNT];
584     TCGTempSet free_temps[TCG_TYPE_COUNT];
585     TCGTemp temps[TCG_MAX_TEMPS]; /* globals first, temps after */
586 
587     QTAILQ_HEAD(, TCGOp) ops, free_ops;
588     QSIMPLEQ_HEAD(, TCGLabel) labels;
589 
590     /* Tells which temporary holds a given register.
591        It does not take into account fixed registers */
592     TCGTemp *reg_to_temp[TCG_TARGET_NB_REGS];
593 
594     uint16_t gen_insn_end_off[TCG_MAX_INSNS];
595     uint64_t *gen_insn_data;
596 
597     /* Exit to translator on overflow. */
598     sigjmp_buf jmp_trans;
599 };
600 
601 static inline bool temp_readonly(TCGTemp *ts)
602 {
603     return ts->kind >= TEMP_FIXED;
604 }
605 
606 extern __thread TCGContext *tcg_ctx;
607 extern const void *tcg_code_gen_epilogue;
608 extern uintptr_t tcg_splitwx_diff;
609 extern TCGv_env cpu_env;
610 
611 bool in_code_gen_buffer(const void *p);
612 
613 #ifdef CONFIG_DEBUG_TCG
614 const void *tcg_splitwx_to_rx(void *rw);
615 void *tcg_splitwx_to_rw(const void *rx);
616 #else
617 static inline const void *tcg_splitwx_to_rx(void *rw)
618 {
619     return rw ? rw + tcg_splitwx_diff : NULL;
620 }
621 
622 static inline void *tcg_splitwx_to_rw(const void *rx)
623 {
624     return rx ? (void *)rx - tcg_splitwx_diff : NULL;
625 }
626 #endif
627 
628 static inline TCGArg temp_arg(TCGTemp *ts)
629 {
630     return (uintptr_t)ts;
631 }
632 
633 static inline TCGTemp *arg_temp(TCGArg a)
634 {
635     return (TCGTemp *)(uintptr_t)a;
636 }
637 
638 #ifdef CONFIG_DEBUG_TCG
639 size_t temp_idx(TCGTemp *ts);
640 TCGTemp *tcgv_i32_temp(TCGv_i32 v);
641 #else
642 static inline size_t temp_idx(TCGTemp *ts)
643 {
644     return ts - tcg_ctx->temps;
645 }
646 
647 /*
648  * Using the offset of a temporary, relative to TCGContext, rather than
649  * its index means that we don't use 0.  That leaves offset 0 free for
650  * a NULL representation without having to leave index 0 unused.
651  */
652 static inline TCGTemp *tcgv_i32_temp(TCGv_i32 v)
653 {
654     return (void *)tcg_ctx + (uintptr_t)v;
655 }
656 #endif
657 
658 static inline TCGTemp *tcgv_i64_temp(TCGv_i64 v)
659 {
660     return tcgv_i32_temp((TCGv_i32)v);
661 }
662 
663 static inline TCGTemp *tcgv_i128_temp(TCGv_i128 v)
664 {
665     return tcgv_i32_temp((TCGv_i32)v);
666 }
667 
668 static inline TCGTemp *tcgv_ptr_temp(TCGv_ptr v)
669 {
670     return tcgv_i32_temp((TCGv_i32)v);
671 }
672 
673 static inline TCGTemp *tcgv_vec_temp(TCGv_vec v)
674 {
675     return tcgv_i32_temp((TCGv_i32)v);
676 }
677 
678 static inline TCGArg tcgv_i32_arg(TCGv_i32 v)
679 {
680     return temp_arg(tcgv_i32_temp(v));
681 }
682 
683 static inline TCGArg tcgv_i64_arg(TCGv_i64 v)
684 {
685     return temp_arg(tcgv_i64_temp(v));
686 }
687 
688 static inline TCGArg tcgv_i128_arg(TCGv_i128 v)
689 {
690     return temp_arg(tcgv_i128_temp(v));
691 }
692 
693 static inline TCGArg tcgv_ptr_arg(TCGv_ptr v)
694 {
695     return temp_arg(tcgv_ptr_temp(v));
696 }
697 
698 static inline TCGArg tcgv_vec_arg(TCGv_vec v)
699 {
700     return temp_arg(tcgv_vec_temp(v));
701 }
702 
703 static inline TCGv_i32 temp_tcgv_i32(TCGTemp *t)
704 {
705     (void)temp_idx(t); /* trigger embedded assert */
706     return (TCGv_i32)((void *)t - (void *)tcg_ctx);
707 }
708 
709 static inline TCGv_i64 temp_tcgv_i64(TCGTemp *t)
710 {
711     return (TCGv_i64)temp_tcgv_i32(t);
712 }
713 
714 static inline TCGv_i128 temp_tcgv_i128(TCGTemp *t)
715 {
716     return (TCGv_i128)temp_tcgv_i32(t);
717 }
718 
719 static inline TCGv_ptr temp_tcgv_ptr(TCGTemp *t)
720 {
721     return (TCGv_ptr)temp_tcgv_i32(t);
722 }
723 
724 static inline TCGv_vec temp_tcgv_vec(TCGTemp *t)
725 {
726     return (TCGv_vec)temp_tcgv_i32(t);
727 }
728 
729 static inline TCGArg tcg_get_insn_param(TCGOp *op, int arg)
730 {
731     return op->args[arg];
732 }
733 
734 static inline void tcg_set_insn_param(TCGOp *op, int arg, TCGArg v)
735 {
736     op->args[arg] = v;
737 }
738 
739 static inline uint64_t tcg_get_insn_start_param(TCGOp *op, int arg)
740 {
741     if (TCG_TARGET_REG_BITS == 64) {
742         return tcg_get_insn_param(op, arg);
743     } else {
744         return deposit64(tcg_get_insn_param(op, arg * 2), 32, 32,
745                          tcg_get_insn_param(op, arg * 2 + 1));
746     }
747 }
748 
749 static inline void tcg_set_insn_start_param(TCGOp *op, int arg, uint64_t v)
750 {
751     if (TCG_TARGET_REG_BITS == 64) {
752         tcg_set_insn_param(op, arg, v);
753     } else {
754         tcg_set_insn_param(op, arg * 2, v);
755         tcg_set_insn_param(op, arg * 2 + 1, v >> 32);
756     }
757 }
758 
759 /* The last op that was emitted.  */
760 static inline TCGOp *tcg_last_op(void)
761 {
762     return QTAILQ_LAST(&tcg_ctx->ops);
763 }
764 
765 /* Test for whether to terminate the TB for using too many opcodes.  */
766 static inline bool tcg_op_buf_full(void)
767 {
768     /* This is not a hard limit, it merely stops translation when
769      * we have produced "enough" opcodes.  We want to limit TB size
770      * such that a RISC host can reasonably use a 16-bit signed
771      * branch within the TB.  We also need to be mindful of the
772      * 16-bit unsigned offsets, TranslationBlock.jmp_reset_offset[]
773      * and TCGContext.gen_insn_end_off[].
774      */
775     return tcg_ctx->nb_ops >= 4000;
776 }
777 
778 /* pool based memory allocation */
779 
780 /* user-mode: mmap_lock must be held for tcg_malloc_internal. */
781 void *tcg_malloc_internal(TCGContext *s, int size);
782 void tcg_pool_reset(TCGContext *s);
783 TranslationBlock *tcg_tb_alloc(TCGContext *s);
784 
785 void tcg_region_reset_all(void);
786 
787 size_t tcg_code_size(void);
788 size_t tcg_code_capacity(void);
789 
790 void tcg_tb_insert(TranslationBlock *tb);
791 void tcg_tb_remove(TranslationBlock *tb);
792 TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr);
793 void tcg_tb_foreach(GTraverseFunc func, gpointer user_data);
794 size_t tcg_nb_tbs(void);
795 
796 /* user-mode: Called with mmap_lock held.  */
797 static inline void *tcg_malloc(int size)
798 {
799     TCGContext *s = tcg_ctx;
800     uint8_t *ptr, *ptr_end;
801 
802     /* ??? This is a weak placeholder for minimum malloc alignment.  */
803     size = QEMU_ALIGN_UP(size, 8);
804 
805     ptr = s->pool_cur;
806     ptr_end = ptr + size;
807     if (unlikely(ptr_end > s->pool_end)) {
808         return tcg_malloc_internal(tcg_ctx, size);
809     } else {
810         s->pool_cur = ptr_end;
811         return ptr;
812     }
813 }
814 
815 void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus);
816 void tcg_register_thread(void);
817 void tcg_prologue_init(TCGContext *s);
818 void tcg_func_start(TCGContext *s);
819 
820 int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start);
821 
822 void tb_target_set_jmp_target(const TranslationBlock *, int,
823                               uintptr_t, uintptr_t);
824 
825 void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size);
826 
827 TCGTemp *tcg_global_mem_new_internal(TCGType, TCGv_ptr,
828                                      intptr_t, const char *);
829 TCGTemp *tcg_temp_new_internal(TCGType, TCGTempKind);
830 TCGv_vec tcg_temp_new_vec(TCGType type);
831 TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match);
832 
833 static inline TCGv_i32 tcg_global_mem_new_i32(TCGv_ptr reg, intptr_t offset,
834                                               const char *name)
835 {
836     TCGTemp *t = tcg_global_mem_new_internal(TCG_TYPE_I32, reg, offset, name);
837     return temp_tcgv_i32(t);
838 }
839 
840 static inline TCGv_i32 tcg_temp_new_i32(void)
841 {
842     TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_I32, TEMP_TB);
843     return temp_tcgv_i32(t);
844 }
845 
846 static inline TCGv_i64 tcg_global_mem_new_i64(TCGv_ptr reg, intptr_t offset,
847                                               const char *name)
848 {
849     TCGTemp *t = tcg_global_mem_new_internal(TCG_TYPE_I64, reg, offset, name);
850     return temp_tcgv_i64(t);
851 }
852 
853 static inline TCGv_i64 tcg_temp_new_i64(void)
854 {
855     TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_I64, TEMP_TB);
856     return temp_tcgv_i64(t);
857 }
858 
859 static inline TCGv_i128 tcg_temp_new_i128(void)
860 {
861     TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_I128, TEMP_TB);
862     return temp_tcgv_i128(t);
863 }
864 
865 static inline TCGv_ptr tcg_global_mem_new_ptr(TCGv_ptr reg, intptr_t offset,
866                                               const char *name)
867 {
868     TCGTemp *t = tcg_global_mem_new_internal(TCG_TYPE_PTR, reg, offset, name);
869     return temp_tcgv_ptr(t);
870 }
871 
872 static inline TCGv_ptr tcg_temp_new_ptr(void)
873 {
874     TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_PTR, TEMP_TB);
875     return temp_tcgv_ptr(t);
876 }
877 
878 int64_t tcg_cpu_exec_time(void);
879 void tcg_dump_info(GString *buf);
880 void tcg_dump_op_count(GString *buf);
881 
882 #define TCG_CT_CONST  1 /* any constant of register size */
883 
884 typedef struct TCGArgConstraint {
885     unsigned ct : 16;
886     unsigned alias_index : 4;
887     unsigned sort_index : 4;
888     unsigned pair_index : 4;
889     unsigned pair : 2;  /* 0: none, 1: first, 2: second, 3: second alias */
890     bool oalias : 1;
891     bool ialias : 1;
892     bool newreg : 1;
893     TCGRegSet regs;
894 } TCGArgConstraint;
895 
896 #define TCG_MAX_OP_ARGS 16
897 
898 /* Bits for TCGOpDef->flags, 8 bits available, all used.  */
899 enum {
900     /* Instruction exits the translation block.  */
901     TCG_OPF_BB_EXIT      = 0x01,
902     /* Instruction defines the end of a basic block.  */
903     TCG_OPF_BB_END       = 0x02,
904     /* Instruction clobbers call registers and potentially update globals.  */
905     TCG_OPF_CALL_CLOBBER = 0x04,
906     /* Instruction has side effects: it cannot be removed if its outputs
907        are not used, and might trigger exceptions.  */
908     TCG_OPF_SIDE_EFFECTS = 0x08,
909     /* Instruction operands are 64-bits (otherwise 32-bits).  */
910     TCG_OPF_64BIT        = 0x10,
911     /* Instruction is optional and not implemented by the host, or insn
912        is generic and should not be implemened by the host.  */
913     TCG_OPF_NOT_PRESENT  = 0x20,
914     /* Instruction operands are vectors.  */
915     TCG_OPF_VECTOR       = 0x40,
916     /* Instruction is a conditional branch. */
917     TCG_OPF_COND_BRANCH  = 0x80
918 };
919 
920 typedef struct TCGOpDef {
921     const char *name;
922     uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
923     uint8_t flags;
924     TCGArgConstraint *args_ct;
925 } TCGOpDef;
926 
927 extern TCGOpDef tcg_op_defs[];
928 extern const size_t tcg_op_defs_max;
929 
930 typedef struct TCGTargetOpDef {
931     TCGOpcode op;
932     const char *args_ct_str[TCG_MAX_OP_ARGS];
933 } TCGTargetOpDef;
934 
935 bool tcg_op_supported(TCGOpcode op);
936 
937 void tcg_gen_call0(TCGHelperInfo *, TCGTemp *ret);
938 void tcg_gen_call1(TCGHelperInfo *, TCGTemp *ret, TCGTemp *);
939 void tcg_gen_call2(TCGHelperInfo *, TCGTemp *ret, TCGTemp *, TCGTemp *);
940 void tcg_gen_call3(TCGHelperInfo *, TCGTemp *ret, TCGTemp *,
941                    TCGTemp *, TCGTemp *);
942 void tcg_gen_call4(TCGHelperInfo *, TCGTemp *ret, TCGTemp *, TCGTemp *,
943                    TCGTemp *, TCGTemp *);
944 void tcg_gen_call5(TCGHelperInfo *, TCGTemp *ret, TCGTemp *, TCGTemp *,
945                    TCGTemp *, TCGTemp *, TCGTemp *);
946 void tcg_gen_call6(TCGHelperInfo *, TCGTemp *ret, TCGTemp *, TCGTemp *,
947                    TCGTemp *, TCGTemp *, TCGTemp *, TCGTemp *);
948 void tcg_gen_call7(TCGHelperInfo *, TCGTemp *ret, TCGTemp *, TCGTemp *,
949                    TCGTemp *, TCGTemp *, TCGTemp *, TCGTemp *, TCGTemp *);
950 
951 TCGOp *tcg_emit_op(TCGOpcode opc, unsigned nargs);
952 void tcg_op_remove(TCGContext *s, TCGOp *op);
953 TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *op,
954                             TCGOpcode opc, unsigned nargs);
955 TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *op,
956                            TCGOpcode opc, unsigned nargs);
957 
958 /**
959  * tcg_remove_ops_after:
960  * @op: target operation
961  *
962  * Discard any opcodes emitted since @op.  Expected usage is to save
963  * a starting point with tcg_last_op(), speculatively emit opcodes,
964  * then decide whether or not to keep those opcodes after the fact.
965  */
966 void tcg_remove_ops_after(TCGOp *op);
967 
968 void tcg_optimize(TCGContext *s);
969 
970 /*
971  * Locate or create a read-only temporary that is a constant.
972  * This kind of temporary need not be freed, but for convenience
973  * will be silently ignored by tcg_temp_free_*.
974  */
975 TCGTemp *tcg_constant_internal(TCGType type, int64_t val);
976 
977 static inline TCGv_i32 tcg_constant_i32(int32_t val)
978 {
979     return temp_tcgv_i32(tcg_constant_internal(TCG_TYPE_I32, val));
980 }
981 
982 static inline TCGv_i64 tcg_constant_i64(int64_t val)
983 {
984     return temp_tcgv_i64(tcg_constant_internal(TCG_TYPE_I64, val));
985 }
986 
987 TCGv_vec tcg_constant_vec(TCGType type, unsigned vece, int64_t val);
988 TCGv_vec tcg_constant_vec_matching(TCGv_vec match, unsigned vece, int64_t val);
989 
990 #if UINTPTR_MAX == UINT32_MAX
991 # define tcg_constant_ptr(x)     ((TCGv_ptr)tcg_constant_i32((intptr_t)(x)))
992 #else
993 # define tcg_constant_ptr(x)     ((TCGv_ptr)tcg_constant_i64((intptr_t)(x)))
994 #endif
995 
996 TCGLabel *gen_new_label(void);
997 
998 /**
999  * label_arg
1000  * @l: label
1001  *
1002  * Encode a label for storage in the TCG opcode stream.
1003  */
1004 
1005 static inline TCGArg label_arg(TCGLabel *l)
1006 {
1007     return (uintptr_t)l;
1008 }
1009 
1010 /**
1011  * arg_label
1012  * @i: value
1013  *
1014  * The opposite of label_arg.  Retrieve a label from the
1015  * encoding of the TCG opcode stream.
1016  */
1017 
1018 static inline TCGLabel *arg_label(TCGArg i)
1019 {
1020     return (TCGLabel *)(uintptr_t)i;
1021 }
1022 
1023 /**
1024  * tcg_ptr_byte_diff
1025  * @a, @b: addresses to be differenced
1026  *
1027  * There are many places within the TCG backends where we need a byte
1028  * difference between two pointers.  While this can be accomplished
1029  * with local casting, it's easy to get wrong -- especially if one is
1030  * concerned with the signedness of the result.
1031  *
1032  * This version relies on GCC's void pointer arithmetic to get the
1033  * correct result.
1034  */
1035 
1036 static inline ptrdiff_t tcg_ptr_byte_diff(const void *a, const void *b)
1037 {
1038     return a - b;
1039 }
1040 
1041 /**
1042  * tcg_pcrel_diff
1043  * @s: the tcg context
1044  * @target: address of the target
1045  *
1046  * Produce a pc-relative difference, from the current code_ptr
1047  * to the destination address.
1048  */
1049 
1050 static inline ptrdiff_t tcg_pcrel_diff(TCGContext *s, const void *target)
1051 {
1052     return tcg_ptr_byte_diff(target, tcg_splitwx_to_rx(s->code_ptr));
1053 }
1054 
1055 /**
1056  * tcg_tbrel_diff
1057  * @s: the tcg context
1058  * @target: address of the target
1059  *
1060  * Produce a difference, from the beginning of the current TB code
1061  * to the destination address.
1062  */
1063 static inline ptrdiff_t tcg_tbrel_diff(TCGContext *s, const void *target)
1064 {
1065     return tcg_ptr_byte_diff(target, tcg_splitwx_to_rx(s->code_buf));
1066 }
1067 
1068 /**
1069  * tcg_current_code_size
1070  * @s: the tcg context
1071  *
1072  * Compute the current code size within the translation block.
1073  * This is used to fill in qemu's data structures for goto_tb.
1074  */
1075 
1076 static inline size_t tcg_current_code_size(TCGContext *s)
1077 {
1078     return tcg_ptr_byte_diff(s->code_ptr, s->code_buf);
1079 }
1080 
1081 /**
1082  * tcg_qemu_tb_exec:
1083  * @env: pointer to CPUArchState for the CPU
1084  * @tb_ptr: address of generated code for the TB to execute
1085  *
1086  * Start executing code from a given translation block.
1087  * Where translation blocks have been linked, execution
1088  * may proceed from the given TB into successive ones.
1089  * Control eventually returns only when some action is needed
1090  * from the top-level loop: either control must pass to a TB
1091  * which has not yet been directly linked, or an asynchronous
1092  * event such as an interrupt needs handling.
1093  *
1094  * Return: The return value is the value passed to the corresponding
1095  * tcg_gen_exit_tb() at translation time of the last TB attempted to execute.
1096  * The value is either zero or a 4-byte aligned pointer to that TB combined
1097  * with additional information in its two least significant bits. The
1098  * additional information is encoded as follows:
1099  *  0, 1: the link between this TB and the next is via the specified
1100  *        TB index (0 or 1). That is, we left the TB via (the equivalent
1101  *        of) "goto_tb <index>". The main loop uses this to determine
1102  *        how to link the TB just executed to the next.
1103  *  2:    we are using instruction counting code generation, and we
1104  *        did not start executing this TB because the instruction counter
1105  *        would hit zero midway through it. In this case the pointer
1106  *        returned is the TB we were about to execute, and the caller must
1107  *        arrange to execute the remaining count of instructions.
1108  *  3:    we stopped because the CPU's exit_request flag was set
1109  *        (usually meaning that there is an interrupt that needs to be
1110  *        handled). The pointer returned is the TB we were about to execute
1111  *        when we noticed the pending exit request.
1112  *
1113  * If the bottom two bits indicate an exit-via-index then the CPU
1114  * state is correctly synchronised and ready for execution of the next
1115  * TB (and in particular the guest PC is the address to execute next).
1116  * Otherwise, we gave up on execution of this TB before it started, and
1117  * the caller must fix up the CPU state by calling the CPU's
1118  * synchronize_from_tb() method with the TB pointer we return (falling
1119  * back to calling the CPU's set_pc method with tb->pb if no
1120  * synchronize_from_tb() method exists).
1121  *
1122  * Note that TCG targets may use a different definition of tcg_qemu_tb_exec
1123  * to this default (which just calls the prologue.code emitted by
1124  * tcg_target_qemu_prologue()).
1125  */
1126 #define TB_EXIT_MASK      3
1127 #define TB_EXIT_IDX0      0
1128 #define TB_EXIT_IDX1      1
1129 #define TB_EXIT_IDXMAX    1
1130 #define TB_EXIT_REQUESTED 3
1131 
1132 #ifdef CONFIG_TCG_INTERPRETER
1133 uintptr_t tcg_qemu_tb_exec(CPUArchState *env, const void *tb_ptr);
1134 #else
1135 typedef uintptr_t tcg_prologue_fn(CPUArchState *env, const void *tb_ptr);
1136 extern tcg_prologue_fn *tcg_qemu_tb_exec;
1137 #endif
1138 
1139 void tcg_register_jit(const void *buf, size_t buf_size);
1140 
1141 #if TCG_TARGET_MAYBE_vec
1142 /* Return zero if the tuple (opc, type, vece) is unsupportable;
1143    return > 0 if it is directly supportable;
1144    return < 0 if we must call tcg_expand_vec_op.  */
1145 int tcg_can_emit_vec_op(TCGOpcode, TCGType, unsigned);
1146 #else
1147 static inline int tcg_can_emit_vec_op(TCGOpcode o, TCGType t, unsigned ve)
1148 {
1149     return 0;
1150 }
1151 #endif
1152 
1153 /* Expand the tuple (opc, type, vece) on the given arguments.  */
1154 void tcg_expand_vec_op(TCGOpcode, TCGType, unsigned, TCGArg, ...);
1155 
1156 /* Replicate a constant C accoring to the log2 of the element size.  */
1157 uint64_t dup_const(unsigned vece, uint64_t c);
1158 
1159 #define dup_const(VECE, C)                                         \
1160     (__builtin_constant_p(VECE)                                    \
1161      ? (  (VECE) == MO_8  ? 0x0101010101010101ull * (uint8_t)(C)   \
1162         : (VECE) == MO_16 ? 0x0001000100010001ull * (uint16_t)(C)  \
1163         : (VECE) == MO_32 ? 0x0000000100000001ull * (uint32_t)(C)  \
1164         : (VECE) == MO_64 ? (uint64_t)(C)                          \
1165         : (qemu_build_not_reached_always(), 0))                    \
1166      : dup_const(VECE, C))
1167 
1168 #ifdef CONFIG_DEBUG_TCG
1169 void tcg_assert_listed_vecop(TCGOpcode);
1170 #else
1171 static inline void tcg_assert_listed_vecop(TCGOpcode op) { }
1172 #endif
1173 
1174 static inline const TCGOpcode *tcg_swap_vecop_list(const TCGOpcode *n)
1175 {
1176 #ifdef CONFIG_DEBUG_TCG
1177     const TCGOpcode *o = tcg_ctx->vecop_list;
1178     tcg_ctx->vecop_list = n;
1179     return o;
1180 #else
1181     return NULL;
1182 #endif
1183 }
1184 
1185 bool tcg_can_emit_vecop_list(const TCGOpcode *, TCGType, unsigned);
1186 
1187 #endif /* TCG_H */
1188