blob: a37181c8998bb2a15217be9908e387b1c0c09f59 [file] [log] [blame]
bellardc896fe22008-02-01 10:05:41 +00001/*
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 */
Richard Hendersone58eb532013-08-27 13:13:44 -070024
25#ifndef TCG_H
26#define TCG_H
27
Paolo Bonzini33c11872016-03-15 16:58:45 +010028#include "cpu.h"
Tony Nguyen14776ab2019-08-24 04:10:58 +100029#include "exec/memop.h"
Paolo Bonzini00f6da62016-03-15 13:16:36 +010030#include "exec/tb-context.h"
Richard Henderson0ec9eab2013-09-19 12:16:45 -070031#include "qemu/bitops.h"
Richard Henderson15fa08f2017-11-02 15:19:14 +010032#include "qemu/queue.h"
Alex Bennée20937142017-02-23 18:29:07 +000033#include "tcg-mo.h"
Richard Henderson78cd7b82013-08-20 14:41:29 -070034#include "tcg-target.h"
Richard Hendersone6cd4bb2018-08-15 16:31:47 -070035#include "qemu/int128.h"
Richard Henderson78cd7b82013-08-20 14:41:29 -070036
Paolo Bonzini00f6da62016-03-15 13:16:36 +010037/* XXX: make safe guess about sizes */
38#define MAX_OP_PER_INSTR 266
39
40#if HOST_LONG_BITS == 32
41#define MAX_OPC_PARAM_PER_ARG 2
42#else
43#define MAX_OPC_PARAM_PER_ARG 1
44#endif
Richard Henderson1df3caa2017-12-13 16:52:57 -060045#define MAX_OPC_PARAM_IARGS 6
Paolo Bonzini00f6da62016-03-15 13:16:36 +010046#define MAX_OPC_PARAM_OARGS 1
47#define MAX_OPC_PARAM_ARGS (MAX_OPC_PARAM_IARGS + MAX_OPC_PARAM_OARGS)
48
49/* A Call op needs up to 4 + 2N parameters on 32-bit archs,
50 * and up to 4 + N parameters on 64-bit archs
51 * (N = number of input arguments + output arguments). */
52#define MAX_OPC_PARAM (4 + (MAX_OPC_PARAM_PER_ARG * MAX_OPC_PARAM_ARGS))
Paolo Bonzini00f6da62016-03-15 13:16:36 +010053
Peter Crosthwaite6e0b0732015-05-30 23:11:34 -070054#define CPU_TEMP_BUF_NLONGS 128
55
Richard Henderson78cd7b82013-08-20 14:41:29 -070056/* Default target word size to pointer size. */
57#ifndef TCG_TARGET_REG_BITS
58# if UINTPTR_MAX == UINT32_MAX
59# define TCG_TARGET_REG_BITS 32
60# elif UINTPTR_MAX == UINT64_MAX
61# define TCG_TARGET_REG_BITS 64
62# else
63# error Unknown pointer size for tcg target
64# endif
Stefan Weil817b8382011-09-17 22:00:27 +020065#endif
66
bellardc896fe22008-02-01 10:05:41 +000067#if TCG_TARGET_REG_BITS == 32
68typedef int32_t tcg_target_long;
69typedef uint32_t tcg_target_ulong;
70#define TCG_PRIlx PRIx32
71#define TCG_PRIld PRId32
72#elif TCG_TARGET_REG_BITS == 64
73typedef int64_t tcg_target_long;
74typedef uint64_t tcg_target_ulong;
75#define TCG_PRIlx PRIx64
76#define TCG_PRIld PRId64
77#else
78#error unsupported
79#endif
80
KONRAD Frederic8d4e9142017-02-23 18:29:08 +000081/* Oversized TCG guests make things like MTTCG hard
82 * as we can't use atomics for cputlb updates.
83 */
84#if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
85#define TCG_OVERSIZED_GUEST 1
86#else
87#define TCG_OVERSIZED_GUEST 0
88#endif
89
bellardc896fe22008-02-01 10:05:41 +000090#if TCG_TARGET_NB_REGS <= 32
91typedef uint32_t TCGRegSet;
92#elif TCG_TARGET_NB_REGS <= 64
93typedef uint64_t TCGRegSet;
94#else
95#error unsupported
96#endif
97
Richard Henderson25c4d9c2011-08-17 14:11:46 -070098#if TCG_TARGET_REG_BITS == 32
Richard Hendersone6a72732013-02-19 23:51:49 -080099/* Turn some undef macros into false macros. */
Richard Henderson609ad702015-07-24 07:16:00 -0700100#define TCG_TARGET_HAS_extrl_i64_i32 0
101#define TCG_TARGET_HAS_extrh_i64_i32 0
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700102#define TCG_TARGET_HAS_div_i64 0
Richard Hendersonca675f42013-03-11 22:41:47 -0700103#define TCG_TARGET_HAS_rem_i64 0
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700104#define TCG_TARGET_HAS_div2_i64 0
105#define TCG_TARGET_HAS_rot_i64 0
106#define TCG_TARGET_HAS_ext8s_i64 0
107#define TCG_TARGET_HAS_ext16s_i64 0
108#define TCG_TARGET_HAS_ext32s_i64 0
109#define TCG_TARGET_HAS_ext8u_i64 0
110#define TCG_TARGET_HAS_ext16u_i64 0
111#define TCG_TARGET_HAS_ext32u_i64 0
112#define TCG_TARGET_HAS_bswap16_i64 0
113#define TCG_TARGET_HAS_bswap32_i64 0
114#define TCG_TARGET_HAS_bswap64_i64 0
115#define TCG_TARGET_HAS_neg_i64 0
116#define TCG_TARGET_HAS_not_i64 0
117#define TCG_TARGET_HAS_andc_i64 0
118#define TCG_TARGET_HAS_orc_i64 0
119#define TCG_TARGET_HAS_eqv_i64 0
120#define TCG_TARGET_HAS_nand_i64 0
121#define TCG_TARGET_HAS_nor_i64 0
Richard Henderson0e28d002016-11-16 09:23:28 +0100122#define TCG_TARGET_HAS_clz_i64 0
123#define TCG_TARGET_HAS_ctz_i64 0
Richard Hendersona768e4e2016-11-21 11:13:39 +0100124#define TCG_TARGET_HAS_ctpop_i64 0
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700125#define TCG_TARGET_HAS_deposit_i64 0
Richard Henderson7ec8bab2016-10-14 12:04:32 -0500126#define TCG_TARGET_HAS_extract_i64 0
127#define TCG_TARGET_HAS_sextract_i64 0
Richard Hendersonfce12962019-02-25 10:29:25 -0800128#define TCG_TARGET_HAS_extract2_i64 0
Richard Hendersonffc5ea02012-09-21 10:13:34 -0700129#define TCG_TARGET_HAS_movcond_i64 0
Richard Hendersond7156f72013-02-19 23:51:52 -0800130#define TCG_TARGET_HAS_add2_i64 0
131#define TCG_TARGET_HAS_sub2_i64 0
132#define TCG_TARGET_HAS_mulu2_i64 0
Richard Henderson4d3203f2013-02-19 23:51:53 -0800133#define TCG_TARGET_HAS_muls2_i64 0
Richard Henderson03271522013-08-14 14:35:56 -0700134#define TCG_TARGET_HAS_muluh_i64 0
135#define TCG_TARGET_HAS_mulsh_i64 0
Richard Hendersone6a72732013-02-19 23:51:49 -0800136/* Turn some undef macros into true macros. */
137#define TCG_TARGET_HAS_add2_i32 1
138#define TCG_TARGET_HAS_sub2_i32 1
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700139#endif
140
Jan Kiszkaa4773322011-09-29 18:52:11 +0200141#ifndef TCG_TARGET_deposit_i32_valid
142#define TCG_TARGET_deposit_i32_valid(ofs, len) 1
143#endif
144#ifndef TCG_TARGET_deposit_i64_valid
145#define TCG_TARGET_deposit_i64_valid(ofs, len) 1
146#endif
Richard Henderson7ec8bab2016-10-14 12:04:32 -0500147#ifndef TCG_TARGET_extract_i32_valid
148#define TCG_TARGET_extract_i32_valid(ofs, len) 1
149#endif
150#ifndef TCG_TARGET_extract_i64_valid
151#define TCG_TARGET_extract_i64_valid(ofs, len) 1
152#endif
Jan Kiszkaa4773322011-09-29 18:52:11 +0200153
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700154/* Only one of DIV or DIV2 should be defined. */
155#if defined(TCG_TARGET_HAS_div_i32)
156#define TCG_TARGET_HAS_div2_i32 0
157#elif defined(TCG_TARGET_HAS_div2_i32)
158#define TCG_TARGET_HAS_div_i32 0
Richard Hendersonca675f42013-03-11 22:41:47 -0700159#define TCG_TARGET_HAS_rem_i32 0
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700160#endif
161#if defined(TCG_TARGET_HAS_div_i64)
162#define TCG_TARGET_HAS_div2_i64 0
163#elif defined(TCG_TARGET_HAS_div2_i64)
164#define TCG_TARGET_HAS_div_i64 0
Richard Hendersonca675f42013-03-11 22:41:47 -0700165#define TCG_TARGET_HAS_rem_i64 0
Richard Henderson25c4d9c2011-08-17 14:11:46 -0700166#endif
167
Richard Hendersondf9ebea2014-03-26 10:59:14 -0700168/* For 32-bit targets, some sort of unsigned widening multiply is required. */
169#if TCG_TARGET_REG_BITS == 32 \
170 && !(defined(TCG_TARGET_HAS_mulu2_i32) \
171 || defined(TCG_TARGET_HAS_muluh_i32))
172# error "Missing unsigned widening multiply"
173#endif
174
Richard Hendersond2fd7452017-09-14 13:53:46 -0700175#if !defined(TCG_TARGET_HAS_v64) \
176 && !defined(TCG_TARGET_HAS_v128) \
177 && !defined(TCG_TARGET_HAS_v256)
178#define TCG_TARGET_MAYBE_vec 0
Richard Hendersonbcefc902019-04-17 13:53:02 -1000179#define TCG_TARGET_HAS_abs_vec 0
Richard Hendersond2fd7452017-09-14 13:53:46 -0700180#define TCG_TARGET_HAS_neg_vec 0
181#define TCG_TARGET_HAS_not_vec 0
182#define TCG_TARGET_HAS_andc_vec 0
183#define TCG_TARGET_HAS_orc_vec 0
Richard Hendersond0ec9792017-11-17 14:35:11 +0100184#define TCG_TARGET_HAS_shi_vec 0
185#define TCG_TARGET_HAS_shs_vec 0
186#define TCG_TARGET_HAS_shv_vec 0
Richard Henderson37740302017-11-21 10:11:14 +0100187#define TCG_TARGET_HAS_mul_vec 0
Richard Henderson8afaf052018-12-17 18:01:47 -0800188#define TCG_TARGET_HAS_sat_vec 0
Richard Hendersondd0a0fc2018-12-17 19:35:46 -0800189#define TCG_TARGET_HAS_minmax_vec 0
Richard Henderson38dc1292019-04-30 11:02:23 -0700190#define TCG_TARGET_HAS_bitsel_vec 0
Richard Hendersonf75da292019-04-30 13:01:12 -0700191#define TCG_TARGET_HAS_cmpsel_vec 0
Richard Hendersond2fd7452017-09-14 13:53:46 -0700192#else
193#define TCG_TARGET_MAYBE_vec 1
194#endif
195#ifndef TCG_TARGET_HAS_v64
196#define TCG_TARGET_HAS_v64 0
197#endif
198#ifndef TCG_TARGET_HAS_v128
199#define TCG_TARGET_HAS_v128 0
200#endif
201#ifndef TCG_TARGET_HAS_v256
202#define TCG_TARGET_HAS_v256 0
203#endif
204
Richard Henderson9aef40e2015-08-30 09:21:33 -0700205#ifndef TARGET_INSN_START_EXTRA_WORDS
206# define TARGET_INSN_START_WORDS 1
207#else
208# define TARGET_INSN_START_WORDS (1 + TARGET_INSN_START_EXTRA_WORDS)
209#endif
210
Richard Hendersona9751602010-03-19 11:12:29 -0700211typedef enum TCGOpcode {
Aurelien Jarnoc61aaf72010-06-03 19:40:04 +0200212#define DEF(name, oargs, iargs, cargs, flags) INDEX_op_ ## name,
bellardc896fe22008-02-01 10:05:41 +0000213#include "tcg-opc.h"
214#undef DEF
215 NB_OPS,
Richard Hendersona9751602010-03-19 11:12:29 -0700216} TCGOpcode;
bellardc896fe22008-02-01 10:05:41 +0000217
Richard Henderson80a8b9a2017-09-11 12:50:42 -0700218#define tcg_regset_set_reg(d, r) ((d) |= (TCGRegSet)1 << (r))
219#define tcg_regset_reset_reg(d, r) ((d) &= ~((TCGRegSet)1 << (r)))
220#define tcg_regset_test_reg(d, r) (((d) >> (r)) & 1)
bellardc896fe22008-02-01 10:05:41 +0000221
Richard Henderson1813e172014-03-28 12:56:22 -0700222#ifndef TCG_TARGET_INSN_UNIT_SIZE
Richard Henderson50533612014-04-28 12:01:23 -0700223# error "Missing TCG_TARGET_INSN_UNIT_SIZE"
224#elif TCG_TARGET_INSN_UNIT_SIZE == 1
Richard Henderson1813e172014-03-28 12:56:22 -0700225typedef uint8_t tcg_insn_unit;
226#elif TCG_TARGET_INSN_UNIT_SIZE == 2
227typedef uint16_t tcg_insn_unit;
228#elif TCG_TARGET_INSN_UNIT_SIZE == 4
229typedef uint32_t tcg_insn_unit;
230#elif TCG_TARGET_INSN_UNIT_SIZE == 8
231typedef uint64_t tcg_insn_unit;
232#else
233/* The port better have done this. */
234#endif
235
236
Paolo Bonzini8bff06a2016-07-15 18:27:40 +0200237#if defined CONFIG_DEBUG_TCG || defined QEMU_STATIC_ANALYSIS
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300238# define tcg_debug_assert(X) do { assert(X); } while (0)
Thomas Huth6fa2cef2018-12-03 13:48:19 +0100239#else
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300240# define tcg_debug_assert(X) \
241 do { if (!(X)) { __builtin_unreachable(); } } while (0)
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300242#endif
243
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700244typedef struct TCGRelocation TCGRelocation;
245struct TCGRelocation {
246 QSIMPLEQ_ENTRY(TCGRelocation) next;
Richard Henderson1813e172014-03-28 12:56:22 -0700247 tcg_insn_unit *ptr;
Richard Henderson2ba7fae22013-08-20 15:30:10 -0700248 intptr_t addend;
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700249 int type;
250};
bellardc896fe22008-02-01 10:05:41 +0000251
Richard Hendersonbef16ab2019-02-07 13:26:40 +0000252typedef struct TCGLabel TCGLabel;
253struct TCGLabel {
254 unsigned present : 1;
Richard Henderson51e39722015-02-13 18:51:05 -0800255 unsigned has_value : 1;
Richard Hendersonbef16ab2019-02-07 13:26:40 +0000256 unsigned id : 14;
Richard Hendersond88a1172018-11-26 12:47:28 -0800257 unsigned refs : 16;
bellardc896fe22008-02-01 10:05:41 +0000258 union {
Richard Henderson2ba7fae22013-08-20 15:30:10 -0700259 uintptr_t value;
Richard Henderson1813e172014-03-28 12:56:22 -0700260 tcg_insn_unit *value_ptr;
bellardc896fe22008-02-01 10:05:41 +0000261 } u;
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700262 QSIMPLEQ_HEAD(, TCGRelocation) relocs;
Richard Hendersonbef16ab2019-02-07 13:26:40 +0000263 QSIMPLEQ_ENTRY(TCGLabel) next;
Richard Hendersonbef16ab2019-02-07 13:26:40 +0000264};
bellardc896fe22008-02-01 10:05:41 +0000265
266typedef struct TCGPool {
267 struct TCGPool *next;
blueswir1c44f9452008-05-19 16:32:18 +0000268 int size;
269 uint8_t data[0] __attribute__ ((aligned));
bellardc896fe22008-02-01 10:05:41 +0000270} TCGPool;
271
272#define TCG_POOL_CHUNK_SIZE 32768
273
blueswir1c4071c92008-03-16 19:21:07 +0000274#define TCG_MAX_TEMPS 512
Richard Henderson190ce7f2015-08-31 14:34:41 -0700275#define TCG_MAX_INSNS 512
bellardc896fe22008-02-01 10:05:41 +0000276
bellardb03cce82008-05-10 10:52:05 +0000277/* when the size of the arguments of a called function is smaller than
278 this value, they are statically allocated in the TB stack frame */
279#define TCG_STATIC_CALL_ARGS_SIZE 128
280
Richard Hendersonc02244a2010-03-19 11:36:30 -0700281typedef enum TCGType {
282 TCG_TYPE_I32,
283 TCG_TYPE_I64,
Richard Hendersond2fd7452017-09-14 13:53:46 -0700284
285 TCG_TYPE_V64,
286 TCG_TYPE_V128,
287 TCG_TYPE_V256,
288
Richard Hendersonc02244a2010-03-19 11:36:30 -0700289 TCG_TYPE_COUNT, /* number of different types */
bellardc896fe22008-02-01 10:05:41 +0000290
Richard Henderson3b6dac32010-06-02 17:26:55 -0700291 /* An alias for the size of the host register. */
bellardc896fe22008-02-01 10:05:41 +0000292#if TCG_TARGET_REG_BITS == 32
Richard Henderson3b6dac32010-06-02 17:26:55 -0700293 TCG_TYPE_REG = TCG_TYPE_I32,
bellardc896fe22008-02-01 10:05:41 +0000294#else
Richard Henderson3b6dac32010-06-02 17:26:55 -0700295 TCG_TYPE_REG = TCG_TYPE_I64,
bellardc896fe22008-02-01 10:05:41 +0000296#endif
Richard Henderson3b6dac32010-06-02 17:26:55 -0700297
Richard Hendersond2898372013-08-20 14:48:46 -0700298 /* An alias for the size of the native pointer. */
299#if UINTPTR_MAX == UINT32_MAX
300 TCG_TYPE_PTR = TCG_TYPE_I32,
301#else
302 TCG_TYPE_PTR = TCG_TYPE_I64,
303#endif
Richard Henderson3b6dac32010-06-02 17:26:55 -0700304
305 /* An alias for the size of the target "long", aka register. */
Richard Hendersonc02244a2010-03-19 11:36:30 -0700306#if TARGET_LONG_BITS == 64
307 TCG_TYPE_TL = TCG_TYPE_I64,
308#else
309 TCG_TYPE_TL = TCG_TYPE_I32,
310#endif
311} TCGType;
bellardc896fe22008-02-01 10:05:41 +0000312
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300313/**
314 * get_alignment_bits
Tony Nguyen14776ab2019-08-24 04:10:58 +1000315 * @memop: MemOp value
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300316 *
317 * Extract the alignment size from the memop.
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300318 */
Tony Nguyen14776ab2019-08-24 04:10:58 +1000319static inline unsigned get_alignment_bits(MemOp memop)
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300320{
Richard Henderson85aa8082016-07-14 12:43:06 -0700321 unsigned a = memop & MO_AMASK;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300322
323 if (a == MO_UNALN) {
Richard Henderson85aa8082016-07-14 12:43:06 -0700324 /* No alignment required. */
325 a = 0;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300326 } else if (a == MO_ALIGN) {
Richard Henderson85aa8082016-07-14 12:43:06 -0700327 /* A natural alignment requirement. */
328 a = memop & MO_SIZE;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300329 } else {
Richard Henderson85aa8082016-07-14 12:43:06 -0700330 /* A specific alignment requirement. */
331 a = a >> MO_ASHIFT;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300332 }
333#if defined(CONFIG_SOFTMMU)
334 /* The requested alignment cannot overlap the TLB flags. */
Richard Henderson85aa8082016-07-14 12:43:06 -0700335 tcg_debug_assert((TLB_FLAGS_MASK & ((1 << a) - 1)) == 0);
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300336#endif
Richard Henderson85aa8082016-07-14 12:43:06 -0700337 return a;
Sergey Sorokin1f00b272016-06-23 21:16:46 +0300338}
339
bellardc896fe22008-02-01 10:05:41 +0000340typedef tcg_target_ulong TCGArg;
341
Peter Maydella40d4702016-10-21 17:38:42 +0100342/* Define type and accessor macros for TCG variables.
343
344 TCG variables are the inputs and outputs of TCG ops, as described
345 in tcg/README. Target CPU front-end code uses these types to deal
346 with TCG variables as it emits TCG code via the tcg_gen_* functions.
347 They come in several flavours:
348 * TCGv_i32 : 32 bit integer type
349 * TCGv_i64 : 64 bit integer type
350 * TCGv_ptr : a host pointer type
Richard Hendersond2fd7452017-09-14 13:53:46 -0700351 * TCGv_vec : a host vector type; the exact size is not exposed
352 to the CPU front-end code.
Peter Maydella40d4702016-10-21 17:38:42 +0100353 * TCGv : an integer type the same size as target_ulong
354 (an alias for either TCGv_i32 or TCGv_i64)
355 The compiler's type checking will complain if you mix them
356 up and pass the wrong sized TCGv to a function.
357
358 Users of tcg_gen_* don't need to know about any of the internal
359 details of these, and should treat them as opaque types.
360 You won't be able to look inside them in a debugger either.
361
362 Internal implementation details follow:
363
364 Note that there is no definition of the structs TCGv_i32_d etc anywhere.
365 This is deliberate, because the values we store in variables of type
366 TCGv_i32 are not really pointers-to-structures. They're just small
367 integers, but keeping them in pointer types like this means that the
368 compiler will complain if you accidentally pass a TCGv_i32 to a
369 function which takes a TCGv_i64, and so on. Only the internals of
Richard Hendersondc41aa72017-10-20 00:30:24 -0700370 TCG need to care about the actual contents of the types. */
pbrookac56dd42008-02-03 19:56:33 +0000371
Richard Hendersonb6c73a62014-09-16 09:51:46 -0700372typedef struct TCGv_i32_d *TCGv_i32;
373typedef struct TCGv_i64_d *TCGv_i64;
374typedef struct TCGv_ptr_d *TCGv_ptr;
Richard Hendersond2fd7452017-09-14 13:53:46 -0700375typedef struct TCGv_vec_d *TCGv_vec;
LluĂ­s Vilanova1bcea732016-02-25 17:43:15 +0100376typedef TCGv_ptr TCGv_env;
LluĂ­s Vilanova5d4e1a12016-02-25 17:43:21 +0100377#if TARGET_LONG_BITS == 32
378#define TCGv TCGv_i32
379#elif TARGET_LONG_BITS == 64
380#define TCGv TCGv_i64
381#else
382#error Unhandled TARGET_LONG_BITS value
383#endif
pbrookac56dd42008-02-03 19:56:33 +0000384
bellardc896fe22008-02-01 10:05:41 +0000385/* call flags */
Aurelien Jarno78505272012-10-09 21:53:08 +0200386/* Helper does not read globals (either directly or through an exception). It
387 implies TCG_CALL_NO_WRITE_GLOBALS. */
Richard Henderson3b503522018-11-26 10:37:34 -0800388#define TCG_CALL_NO_READ_GLOBALS 0x0001
Aurelien Jarno78505272012-10-09 21:53:08 +0200389/* Helper does not write globals */
Richard Henderson3b503522018-11-26 10:37:34 -0800390#define TCG_CALL_NO_WRITE_GLOBALS 0x0002
Aurelien Jarno78505272012-10-09 21:53:08 +0200391/* Helper can be safely suppressed if the return value is not used. */
Richard Henderson3b503522018-11-26 10:37:34 -0800392#define TCG_CALL_NO_SIDE_EFFECTS 0x0004
Richard Henderson15d74092018-11-26 11:32:38 -0800393/* Helper is QEMU_NORETURN. */
394#define TCG_CALL_NO_RETURN 0x0008
Aurelien Jarno78505272012-10-09 21:53:08 +0200395
396/* convenience version of most used call flags */
397#define TCG_CALL_NO_RWG TCG_CALL_NO_READ_GLOBALS
398#define TCG_CALL_NO_WG TCG_CALL_NO_WRITE_GLOBALS
399#define TCG_CALL_NO_SE TCG_CALL_NO_SIDE_EFFECTS
400#define TCG_CALL_NO_RWG_SE (TCG_CALL_NO_RWG | TCG_CALL_NO_SE)
401#define TCG_CALL_NO_WG_SE (TCG_CALL_NO_WG | TCG_CALL_NO_SE)
402
Richard Hendersone89b28a2017-10-20 12:08:19 -0700403/* Used to align parameters. See the comment before tcgv_i32_temp. */
404#define TCG_CALL_DUMMY_ARG ((TCGArg)0)
bellard39cf05d2008-05-22 14:59:57 +0000405
Stefan Weila93cf9d2012-11-02 08:29:53 +0100406/* Conditions. Note that these are laid out for easy manipulation by
407 the functions below:
Richard Henderson0aed2572012-09-24 14:21:40 -0700408 bit 0 is used for inverting;
409 bit 1 is signed,
410 bit 2 is unsigned,
411 bit 3 is used with bit 0 for swapping signed/unsigned. */
bellardc896fe22008-02-01 10:05:41 +0000412typedef enum {
Richard Henderson0aed2572012-09-24 14:21:40 -0700413 /* non-signed */
414 TCG_COND_NEVER = 0 | 0 | 0 | 0,
415 TCG_COND_ALWAYS = 0 | 0 | 0 | 1,
416 TCG_COND_EQ = 8 | 0 | 0 | 0,
417 TCG_COND_NE = 8 | 0 | 0 | 1,
418 /* signed */
419 TCG_COND_LT = 0 | 0 | 2 | 0,
420 TCG_COND_GE = 0 | 0 | 2 | 1,
421 TCG_COND_LE = 8 | 0 | 2 | 0,
422 TCG_COND_GT = 8 | 0 | 2 | 1,
bellardc896fe22008-02-01 10:05:41 +0000423 /* unsigned */
Richard Henderson0aed2572012-09-24 14:21:40 -0700424 TCG_COND_LTU = 0 | 4 | 0 | 0,
425 TCG_COND_GEU = 0 | 4 | 0 | 1,
426 TCG_COND_LEU = 8 | 4 | 0 | 0,
427 TCG_COND_GTU = 8 | 4 | 0 | 1,
bellardc896fe22008-02-01 10:05:41 +0000428} TCGCond;
429
Richard Henderson1c086222010-02-09 12:33:09 -0800430/* Invert the sense of the comparison. */
Richard Henderson401d4662010-01-07 10:15:20 -0800431static inline TCGCond tcg_invert_cond(TCGCond c)
432{
433 return (TCGCond)(c ^ 1);
434}
435
Richard Henderson1c086222010-02-09 12:33:09 -0800436/* Swap the operands in a comparison. */
437static inline TCGCond tcg_swap_cond(TCGCond c)
438{
Richard Henderson0aed2572012-09-24 14:21:40 -0700439 return c & 6 ? (TCGCond)(c ^ 9) : c;
Richard Henderson1c086222010-02-09 12:33:09 -0800440}
441
Richard Hendersond1e321b2012-09-24 14:21:41 -0700442/* Create an "unsigned" version of a "signed" comparison. */
Richard Hendersonff44c2f2009-12-27 09:09:41 +0000443static inline TCGCond tcg_unsigned_cond(TCGCond c)
444{
Richard Henderson0aed2572012-09-24 14:21:40 -0700445 return c & 2 ? (TCGCond)(c ^ 6) : c;
Richard Hendersonff44c2f2009-12-27 09:09:41 +0000446}
447
Richard Henderson923ed172017-11-20 14:47:02 +0100448/* Create a "signed" version of an "unsigned" comparison. */
449static inline TCGCond tcg_signed_cond(TCGCond c)
450{
451 return c & 4 ? (TCGCond)(c ^ 6) : c;
452}
453
Richard Hendersond1e321b2012-09-24 14:21:41 -0700454/* Must a comparison be considered unsigned? */
Richard Hendersonbcc66562012-09-24 14:21:39 -0700455static inline bool is_unsigned_cond(TCGCond c)
456{
Richard Henderson0aed2572012-09-24 14:21:40 -0700457 return (c & 4) != 0;
Richard Hendersonbcc66562012-09-24 14:21:39 -0700458}
459
Richard Hendersond1e321b2012-09-24 14:21:41 -0700460/* Create a "high" version of a double-word comparison.
461 This removes equality from a LTE or GTE comparison. */
462static inline TCGCond tcg_high_cond(TCGCond c)
463{
464 switch (c) {
465 case TCG_COND_GE:
466 case TCG_COND_LE:
467 case TCG_COND_GEU:
468 case TCG_COND_LEU:
469 return (TCGCond)(c ^ 8);
470 default:
471 return c;
472 }
473}
474
Emilio G. Cota00c8fa92015-04-02 20:07:53 -0400475typedef enum TCGTempVal {
476 TEMP_VAL_DEAD,
477 TEMP_VAL_REG,
478 TEMP_VAL_MEM,
479 TEMP_VAL_CONST,
480} TCGTempVal;
bellardc896fe22008-02-01 10:05:41 +0000481
bellardc896fe22008-02-01 10:05:41 +0000482typedef struct TCGTemp {
Richard Hendersonb6638662013-09-18 14:54:45 -0700483 TCGReg reg:8;
Emilio G. Cota00c8fa92015-04-02 20:07:53 -0400484 TCGTempVal val_type:8;
485 TCGType base_type:8;
486 TCGType type:8;
bellardc896fe22008-02-01 10:05:41 +0000487 unsigned int fixed_reg:1;
Richard Hendersonb3915db2013-09-19 10:36:18 -0700488 unsigned int indirect_reg:1;
489 unsigned int indirect_base:1;
bellardc896fe22008-02-01 10:05:41 +0000490 unsigned int mem_coherent:1;
491 unsigned int mem_allocated:1;
Richard Hendersonfa477d22016-11-02 11:20:15 -0600492 /* If true, the temp is saved across both basic blocks and
493 translation blocks. */
494 unsigned int temp_global:1;
495 /* If true, the temp is saved across basic blocks but dead
496 at the end of translation blocks. If false, the temp is
497 dead at the end of basic blocks. */
498 unsigned int temp_local:1;
499 unsigned int temp_allocated:1;
Emilio G. Cota00c8fa92015-04-02 20:07:53 -0400500
501 tcg_target_long val;
Richard Hendersonb3a62932013-09-18 14:12:53 -0700502 struct TCGTemp *mem_base;
Emilio G. Cota00c8fa92015-04-02 20:07:53 -0400503 intptr_t mem_offset;
bellardc896fe22008-02-01 10:05:41 +0000504 const char *name;
Richard Hendersonb83eabe2016-11-01 15:56:04 -0600505
506 /* Pass-specific information that can be stored for a temporary.
507 One word worth of integer data, and one pointer to data
508 allocated separately. */
509 uintptr_t state;
510 void *state_ptr;
bellardc896fe22008-02-01 10:05:41 +0000511} TCGTemp;
512
bellardc896fe22008-02-01 10:05:41 +0000513typedef struct TCGContext TCGContext;
514
Richard Henderson0ec9eab2013-09-19 12:16:45 -0700515typedef struct TCGTempSet {
516 unsigned long l[BITS_TO_LONGS(TCG_MAX_TEMPS)];
517} TCGTempSet;
518
Richard Hendersona1b3c482016-06-22 15:46:09 -0700519/* While we limit helpers to 6 arguments, for 32-bit hosts, with padding,
520 this imples a max of 6*2 (64-bit in) + 2 (64-bit out) = 14 operands.
521 There are never more than 2 outputs, which means that we can store all
522 dead + sync data within 16 bits. */
523#define DEAD_ARG 4
524#define SYNC_ARG 1
525typedef uint16_t TCGLifeData;
526
Richard Henderson75e8b9b2016-12-08 10:52:57 -0800527/* The layout here is designed to avoid a bitfield crossing of
528 a 32-bit boundary, which would cause GCC to add extra padding. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700529typedef struct TCGOp {
Richard Hendersonbee158c2016-06-22 20:43:29 -0700530 TCGOpcode opc : 8; /* 8 */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700531
Richard Hendersoncd9090a2017-11-14 13:02:51 +0100532 /* Parameters for this opcode. See below. */
533 unsigned param1 : 4; /* 12 */
534 unsigned param2 : 4; /* 16 */
Richard Hendersonbee158c2016-06-22 20:43:29 -0700535
Richard Hendersonbee158c2016-06-22 20:43:29 -0700536 /* Lifetime data of the operands. */
Richard Henderson15fa08f2017-11-02 15:19:14 +0100537 unsigned life : 16; /* 32 */
538
539 /* Next and previous opcodes. */
540 QTAILQ_ENTRY(TCGOp) link;
Richard Henderson75e8b9b2016-12-08 10:52:57 -0800541
542 /* Arguments for the opcode. */
543 TCGArg args[MAX_OPC_PARAM];
Richard Henderson69e37062018-11-27 07:44:51 -0800544
545 /* Register preferences for the output(s). */
546 TCGRegSet output_pref[2];
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700547} TCGOp;
548
Richard Hendersoncd9090a2017-11-14 13:02:51 +0100549#define TCGOP_CALLI(X) (X)->param1
550#define TCGOP_CALLO(X) (X)->param2
551
Richard Hendersond2fd7452017-09-14 13:53:46 -0700552#define TCGOP_VECL(X) (X)->param1
553#define TCGOP_VECE(X) (X)->param2
554
Richard Hendersondcb8e752016-06-22 19:42:31 -0700555/* Make sure operands fit in the bitfields above. */
556QEMU_BUILD_BUG_ON(NB_OPS > (1 << 8));
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700557
Emilio G. Cotac3fac112017-07-05 19:35:06 -0400558typedef struct TCGProfile {
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400559 int64_t cpu_exec_time;
Emilio G. Cotac3fac112017-07-05 19:35:06 -0400560 int64_t tb_count1;
561 int64_t tb_count;
562 int64_t op_count; /* total insn count */
563 int op_count_max; /* max insn per TB */
Emilio G. Cotac3fac112017-07-05 19:35:06 -0400564 int temp_count_max;
Emilio G. Cotadd1d7da2018-10-10 10:48:52 -0400565 int64_t temp_count;
Emilio G. Cotac3fac112017-07-05 19:35:06 -0400566 int64_t del_op_count;
567 int64_t code_in_len;
568 int64_t code_out_len;
569 int64_t search_out_len;
570 int64_t interm_time;
571 int64_t code_time;
572 int64_t la_time;
573 int64_t opt_time;
574 int64_t restore_count;
575 int64_t restore_time;
576 int64_t table_op_count[NB_OPS];
577} TCGProfile;
578
bellardc896fe22008-02-01 10:05:41 +0000579struct TCGContext {
580 uint8_t *pool_cur, *pool_end;
Kirill Batuzov40552992012-03-02 13:22:17 +0400581 TCGPool *pool_first, *pool_current, *pool_first_large;
bellardc896fe22008-02-01 10:05:41 +0000582 int nb_labels;
bellardc896fe22008-02-01 10:05:41 +0000583 int nb_globals;
584 int nb_temps;
Richard Henderson5a184072016-06-23 20:34:33 -0700585 int nb_indirects;
Richard Hendersonabebf922018-05-08 19:18:59 +0000586 int nb_ops;
bellardc896fe22008-02-01 10:05:41 +0000587
588 /* goto_tb support */
Richard Henderson1813e172014-03-28 12:56:22 -0700589 tcg_insn_unit *code_buf;
Sergey Fedorovf3091012016-04-10 23:35:45 +0300590 uint16_t *tb_jmp_reset_offset; /* tb->jmp_reset_offset */
Richard Hendersona8583392017-07-31 22:02:31 -0700591 uintptr_t *tb_jmp_insn_offset; /* tb->jmp_target_arg if direct_jump */
592 uintptr_t *tb_jmp_target_addr; /* tb->jmp_target_arg if !direct_jump */
bellardc896fe22008-02-01 10:05:41 +0000593
bellardc896fe22008-02-01 10:05:41 +0000594 TCGRegSet reserved_regs;
Emilio G. Cotae82d5a22017-07-16 15:13:52 -0400595 uint32_t tb_cflags; /* cflags of the current TB */
Richard Hendersone2c6d1b2013-08-20 15:12:31 -0700596 intptr_t current_frame_offset;
597 intptr_t frame_start;
598 intptr_t frame_end;
Richard Hendersonb3a62932013-09-18 14:12:53 -0700599 TCGTemp *frame_temp;
bellardc896fe22008-02-01 10:05:41 +0000600
Richard Henderson1813e172014-03-28 12:56:22 -0700601 tcg_insn_unit *code_ptr;
bellardc896fe22008-02-01 10:05:41 +0000602
bellarda23a9ec2008-05-23 09:52:20 +0000603#ifdef CONFIG_PROFILER
Emilio G. Cotac3fac112017-07-05 19:35:06 -0400604 TCGProfile prof;
bellarda23a9ec2008-05-23 09:52:20 +0000605#endif
Peter Maydell27bfd832011-03-06 21:39:53 +0000606
607#ifdef CONFIG_DEBUG_TCG
608 int temps_in_use;
Richard Henderson0a209d42012-09-21 17:18:16 -0700609 int goto_tb_issue_mask;
Richard Henderson53229a72019-03-17 00:27:29 +0000610 const TCGOpcode *vecop_list;
Peter Maydell27bfd832011-03-06 21:39:53 +0000611#endif
Yeongkyoon Leeb76f0d82012-10-31 16:04:25 +0900612
Richard Henderson1813e172014-03-28 12:56:22 -0700613 /* Code generation. Note that we specifically do not use tcg_insn_unit
614 here, because there's too much arithmetic throughout that relies
615 on addition and subtraction working on bytes. Rely on the GCC
616 extension that allows arithmetic on void*. */
Richard Henderson1813e172014-03-28 12:56:22 -0700617 void *code_gen_prologue;
Emilio G. Cotacedbcb02017-04-26 23:29:14 -0400618 void *code_gen_epilogue;
Richard Henderson1813e172014-03-28 12:56:22 -0700619 void *code_gen_buffer;
Evgeny Voevodin0b0d3322013-02-01 01:47:22 +0700620 size_t code_gen_buffer_size;
Richard Henderson1813e172014-03-28 12:56:22 -0700621 void *code_gen_ptr;
Richard Henderson57a26942017-07-30 13:13:21 -0700622 void *data_gen_ptr;
Evgeny Voevodin0b0d3322013-02-01 01:47:22 +0700623
Richard Hendersonb125f9d2015-09-22 13:01:15 -0700624 /* Threshold to flush the translated code buffer. */
625 void *code_gen_highwater;
626
Emilio G. Cota128ed222017-08-01 15:11:12 -0400627 size_t tb_phys_invalidate_count;
628
LluĂ­s Vilanova7c255042016-06-09 19:31:41 +0200629 /* Track which vCPU triggers events */
630 CPUState *cpu; /* *_trans */
LluĂ­s Vilanova7c255042016-06-09 19:31:41 +0200631
Richard Henderson659ef5c2017-07-30 12:30:41 -0700632 /* These structures are private to tcg-target.inc.c. */
633#ifdef TCG_TARGET_NEED_LDST_LABELS
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100634 QSIMPLEQ_HEAD(, TCGLabelQemuLdst) ldst_labels;
Richard Henderson659ef5c2017-07-30 12:30:41 -0700635#endif
Richard Henderson57a26942017-07-30 13:13:21 -0700636#ifdef TCG_TARGET_NEED_POOL_LABELS
637 struct TCGLabelPoolData *pool_labels;
638#endif
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700639
Emilio G. Cota26689782017-07-04 13:54:21 -0400640 TCGLabel *exitreq_label;
641
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700642 TCGTempSet free_temps[TCG_TYPE_COUNT * 2];
643 TCGTemp temps[TCG_MAX_TEMPS]; /* globals first, temps after */
644
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100645 QTAILQ_HEAD(, TCGOp) ops, free_ops;
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700646 QSIMPLEQ_HEAD(, TCGLabel) labels;
Richard Henderson15fa08f2017-11-02 15:19:14 +0100647
Richard Hendersonf8b2f202013-09-18 15:21:56 -0700648 /* Tells which temporary holds a given register.
649 It does not take into account fixed registers */
650 TCGTemp *reg_to_temp[TCG_TARGET_NB_REGS];
Richard Hendersonc45cb8b2014-09-19 13:49:15 -0700651
Richard Hendersonfca8a502015-09-01 19:11:45 -0700652 uint16_t gen_insn_end_off[TCG_MAX_INSNS];
653 target_ulong gen_insn_data[TCG_MAX_INSNS][TARGET_INSN_START_WORDS];
bellardc896fe22008-02-01 10:05:41 +0000654};
655
Emilio G. Cotab1311c42017-07-12 17:15:52 -0400656extern TCGContext tcg_init_ctx;
Emilio G. Cota3468b592017-07-19 18:57:58 -0400657extern __thread TCGContext *tcg_ctx;
Richard Henderson1c2adb92017-10-10 14:34:37 -0700658extern TCGv_env cpu_env;
bellardc896fe22008-02-01 10:05:41 +0000659
Richard Henderson1807f4c2017-06-20 12:24:57 -0700660static inline size_t temp_idx(TCGTemp *ts)
661{
Emilio G. Cotab1311c42017-07-12 17:15:52 -0400662 ptrdiff_t n = ts - tcg_ctx->temps;
663 tcg_debug_assert(n >= 0 && n < tcg_ctx->nb_temps);
Richard Henderson1807f4c2017-06-20 12:24:57 -0700664 return n;
665}
666
667static inline TCGArg temp_arg(TCGTemp *ts)
668{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700669 return (uintptr_t)ts;
Richard Henderson1807f4c2017-06-20 12:24:57 -0700670}
671
Richard Henderson43439132017-06-19 23:18:10 -0700672static inline TCGTemp *arg_temp(TCGArg a)
673{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700674 return (TCGTemp *)(uintptr_t)a;
Richard Henderson43439132017-06-19 23:18:10 -0700675}
676
Richard Hendersone89b28a2017-10-20 12:08:19 -0700677/* Using the offset of a temporary, relative to TCGContext, rather than
678 its index means that we don't use 0. That leaves offset 0 free for
679 a NULL representation without having to leave index 0 unused. */
680static inline TCGTemp *tcgv_i32_temp(TCGv_i32 v)
Richard Henderson63490392017-06-20 13:43:15 -0700681{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700682 uintptr_t o = (uintptr_t)v;
Emilio G. Cotab1311c42017-07-12 17:15:52 -0400683 TCGTemp *t = (void *)tcg_ctx + o;
Richard Hendersone89b28a2017-10-20 12:08:19 -0700684 tcg_debug_assert(offsetof(TCGContext, temps[temp_idx(t)]) == o);
685 return t;
Richard Henderson63490392017-06-20 13:43:15 -0700686}
687
Richard Hendersone89b28a2017-10-20 12:08:19 -0700688static inline TCGTemp *tcgv_i64_temp(TCGv_i64 v)
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700689{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700690 return tcgv_i32_temp((TCGv_i32)v);
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700691}
692
Richard Hendersone89b28a2017-10-20 12:08:19 -0700693static inline TCGTemp *tcgv_ptr_temp(TCGv_ptr v)
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700694{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700695 return tcgv_i32_temp((TCGv_i32)v);
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700696}
697
Richard Hendersond2fd7452017-09-14 13:53:46 -0700698static inline TCGTemp *tcgv_vec_temp(TCGv_vec v)
699{
700 return tcgv_i32_temp((TCGv_i32)v);
701}
702
Richard Hendersone89b28a2017-10-20 12:08:19 -0700703static inline TCGArg tcgv_i32_arg(TCGv_i32 v)
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700704{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700705 return temp_arg(tcgv_i32_temp(v));
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700706}
707
Richard Hendersone89b28a2017-10-20 12:08:19 -0700708static inline TCGArg tcgv_i64_arg(TCGv_i64 v)
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700709{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700710 return temp_arg(tcgv_i64_temp(v));
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700711}
712
Richard Hendersone89b28a2017-10-20 12:08:19 -0700713static inline TCGArg tcgv_ptr_arg(TCGv_ptr v)
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700714{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700715 return temp_arg(tcgv_ptr_temp(v));
Richard Hendersonae8b75d2017-10-15 13:27:56 -0700716}
717
Richard Hendersond2fd7452017-09-14 13:53:46 -0700718static inline TCGArg tcgv_vec_arg(TCGv_vec v)
719{
720 return temp_arg(tcgv_vec_temp(v));
721}
722
Richard Henderson085272b2017-10-20 00:05:45 -0700723static inline TCGv_i32 temp_tcgv_i32(TCGTemp *t)
724{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700725 (void)temp_idx(t); /* trigger embedded assert */
Emilio G. Cotab1311c42017-07-12 17:15:52 -0400726 return (TCGv_i32)((void *)t - (void *)tcg_ctx);
Richard Henderson085272b2017-10-20 00:05:45 -0700727}
728
729static inline TCGv_i64 temp_tcgv_i64(TCGTemp *t)
730{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700731 return (TCGv_i64)temp_tcgv_i32(t);
Richard Henderson085272b2017-10-20 00:05:45 -0700732}
733
734static inline TCGv_ptr temp_tcgv_ptr(TCGTemp *t)
735{
Richard Hendersone89b28a2017-10-20 12:08:19 -0700736 return (TCGv_ptr)temp_tcgv_i32(t);
Richard Henderson085272b2017-10-20 00:05:45 -0700737}
738
Richard Hendersond2fd7452017-09-14 13:53:46 -0700739static inline TCGv_vec temp_tcgv_vec(TCGTemp *t)
740{
741 return (TCGv_vec)temp_tcgv_i32(t);
742}
743
Richard Hendersondc41aa72017-10-20 00:30:24 -0700744#if TCG_TARGET_REG_BITS == 32
745static inline TCGv_i32 TCGV_LOW(TCGv_i64 t)
746{
747 return temp_tcgv_i32(tcgv_i64_temp(t));
748}
749
750static inline TCGv_i32 TCGV_HIGH(TCGv_i64 t)
751{
752 return temp_tcgv_i32(tcgv_i64_temp(t) + 1);
753}
754#endif
755
Richard Henderson15fa08f2017-11-02 15:19:14 +0100756static inline void tcg_set_insn_param(TCGOp *op, int arg, TCGArg v)
Edgar E. Iglesias1d414782016-05-12 13:22:26 +0100757{
Richard Henderson15fa08f2017-11-02 15:19:14 +0100758 op->args[arg] = v;
Edgar E. Iglesias1d414782016-05-12 13:22:26 +0100759}
760
Richard Henderson9743cd52018-04-10 13:02:26 +0100761static inline void tcg_set_insn_start_param(TCGOp *op, int arg, target_ulong v)
762{
763#if TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
764 tcg_set_insn_param(op, arg, v);
765#else
766 tcg_set_insn_param(op, arg * 2, v);
767 tcg_set_insn_param(op, arg * 2 + 1, v >> 32);
768#endif
769}
770
Richard Henderson15fa08f2017-11-02 15:19:14 +0100771/* The last op that was emitted. */
772static inline TCGOp *tcg_last_op(void)
Richard Hendersonfe700ad2014-03-30 15:36:56 -0700773{
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100774 return QTAILQ_LAST(&tcg_ctx->ops);
Richard Hendersonfe700ad2014-03-30 15:36:56 -0700775}
776
777/* Test for whether to terminate the TB for using too many opcodes. */
778static inline bool tcg_op_buf_full(void)
779{
Richard Hendersonabebf922018-05-08 19:18:59 +0000780 /* This is not a hard limit, it merely stops translation when
781 * we have produced "enough" opcodes. We want to limit TB size
782 * such that a RISC host can reasonably use a 16-bit signed
Richard Henderson9f754622018-06-14 19:57:03 -1000783 * branch within the TB. We also need to be mindful of the
784 * 16-bit unsigned offsets, TranslationBlock.jmp_reset_offset[]
785 * and TCGContext.gen_insn_end_off[].
Richard Hendersonabebf922018-05-08 19:18:59 +0000786 */
Richard Henderson9f754622018-06-14 19:57:03 -1000787 return tcg_ctx->nb_ops >= 4000;
Richard Hendersonfe700ad2014-03-30 15:36:56 -0700788}
789
bellardc896fe22008-02-01 10:05:41 +0000790/* pool based memory allocation */
791
Emilio G. Cota0ac20312017-08-04 23:46:31 -0400792/* user-mode: mmap_lock must be held for tcg_malloc_internal. */
bellardc896fe22008-02-01 10:05:41 +0000793void *tcg_malloc_internal(TCGContext *s, int size);
794void tcg_pool_reset(TCGContext *s);
Emilio G. Cota6e3b2bf2017-06-06 19:12:25 -0400795TranslationBlock *tcg_tb_alloc(TCGContext *s);
bellardc896fe22008-02-01 10:05:41 +0000796
Emilio G. Cotae8feb962017-07-07 19:24:20 -0400797void tcg_region_init(void);
798void tcg_region_reset_all(void);
799
800size_t tcg_code_size(void);
801size_t tcg_code_capacity(void);
802
Emilio G. Cotabe2cdc52017-07-26 16:58:05 -0400803void tcg_tb_insert(TranslationBlock *tb);
804void tcg_tb_remove(TranslationBlock *tb);
Emilio G. Cota128ed222017-08-01 15:11:12 -0400805size_t tcg_tb_phys_invalidate_count(void);
Emilio G. Cotabe2cdc52017-07-26 16:58:05 -0400806TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr);
807void tcg_tb_foreach(GTraverseFunc func, gpointer user_data);
808size_t tcg_nb_tbs(void);
809
Emilio G. Cota0ac20312017-08-04 23:46:31 -0400810/* user-mode: Called with mmap_lock held. */
bellardc896fe22008-02-01 10:05:41 +0000811static inline void *tcg_malloc(int size)
812{
Emilio G. Cotab1311c42017-07-12 17:15:52 -0400813 TCGContext *s = tcg_ctx;
bellardc896fe22008-02-01 10:05:41 +0000814 uint8_t *ptr, *ptr_end;
Richard Henderson13aaef62017-08-02 14:50:04 -0700815
816 /* ??? This is a weak placeholder for minimum malloc alignment. */
817 size = QEMU_ALIGN_UP(size, 8);
818
bellardc896fe22008-02-01 10:05:41 +0000819 ptr = s->pool_cur;
820 ptr_end = ptr + size;
821 if (unlikely(ptr_end > s->pool_end)) {
Emilio G. Cotab1311c42017-07-12 17:15:52 -0400822 return tcg_malloc_internal(tcg_ctx, size);
bellardc896fe22008-02-01 10:05:41 +0000823 } else {
824 s->pool_cur = ptr_end;
825 return ptr;
826 }
827}
828
829void tcg_context_init(TCGContext *s);
Emilio G. Cota3468b592017-07-19 18:57:58 -0400830void tcg_register_thread(void);
Richard Henderson9002ec72010-05-06 08:50:41 -0700831void tcg_prologue_init(TCGContext *s);
bellardc896fe22008-02-01 10:05:41 +0000832void tcg_func_start(TCGContext *s);
833
Alex Bennée5bd2ec32016-03-15 14:30:16 +0000834int tcg_gen_code(TCGContext *s, TranslationBlock *tb);
bellardc896fe22008-02-01 10:05:41 +0000835
Richard Hendersonb6638662013-09-18 14:54:45 -0700836void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size);
pbrooka7812ae2008-11-17 14:43:54 +0000837
Richard Henderson085272b2017-10-20 00:05:45 -0700838TCGTemp *tcg_global_mem_new_internal(TCGType, TCGv_ptr,
839 intptr_t, const char *);
Richard Henderson5bfa8032018-02-22 18:17:57 -0800840TCGTemp *tcg_temp_new_internal(TCGType, bool);
841void tcg_temp_free_internal(TCGTemp *);
Richard Hendersond2fd7452017-09-14 13:53:46 -0700842TCGv_vec tcg_temp_new_vec(TCGType type);
843TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match);
Richard Hendersone1ccc052013-09-18 12:53:09 -0700844
Richard Henderson5bfa8032018-02-22 18:17:57 -0800845static inline void tcg_temp_free_i32(TCGv_i32 arg)
846{
847 tcg_temp_free_internal(tcgv_i32_temp(arg));
848}
849
850static inline void tcg_temp_free_i64(TCGv_i64 arg)
851{
852 tcg_temp_free_internal(tcgv_i64_temp(arg));
853}
854
855static inline void tcg_temp_free_ptr(TCGv_ptr arg)
856{
857 tcg_temp_free_internal(tcgv_ptr_temp(arg));
858}
859
860static inline void tcg_temp_free_vec(TCGv_vec arg)
861{
862 tcg_temp_free_internal(tcgv_vec_temp(arg));
863}
Richard Hendersone1ccc052013-09-18 12:53:09 -0700864
Richard Hendersone1ccc052013-09-18 12:53:09 -0700865static inline TCGv_i32 tcg_global_mem_new_i32(TCGv_ptr reg, intptr_t offset,
866 const char *name)
867{
Richard Henderson085272b2017-10-20 00:05:45 -0700868 TCGTemp *t = tcg_global_mem_new_internal(TCG_TYPE_I32, reg, offset, name);
869 return temp_tcgv_i32(t);
Richard Hendersone1ccc052013-09-18 12:53:09 -0700870}
871
pbrooka7812ae2008-11-17 14:43:54 +0000872static inline TCGv_i32 tcg_temp_new_i32(void)
bellard641d5fb2008-05-25 17:24:00 +0000873{
Richard Henderson5bfa8032018-02-22 18:17:57 -0800874 TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_I32, false);
875 return temp_tcgv_i32(t);
bellard641d5fb2008-05-25 17:24:00 +0000876}
Richard Hendersone1ccc052013-09-18 12:53:09 -0700877
pbrooka7812ae2008-11-17 14:43:54 +0000878static inline TCGv_i32 tcg_temp_local_new_i32(void)
bellard641d5fb2008-05-25 17:24:00 +0000879{
Richard Henderson5bfa8032018-02-22 18:17:57 -0800880 TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_I32, true);
881 return temp_tcgv_i32(t);
bellard641d5fb2008-05-25 17:24:00 +0000882}
pbrooka7812ae2008-11-17 14:43:54 +0000883
Richard Hendersone1ccc052013-09-18 12:53:09 -0700884static inline TCGv_i64 tcg_global_mem_new_i64(TCGv_ptr reg, intptr_t offset,
885 const char *name)
886{
Richard Henderson085272b2017-10-20 00:05:45 -0700887 TCGTemp *t = tcg_global_mem_new_internal(TCG_TYPE_I64, reg, offset, name);
888 return temp_tcgv_i64(t);
Richard Hendersone1ccc052013-09-18 12:53:09 -0700889}
890
pbrooka7812ae2008-11-17 14:43:54 +0000891static inline TCGv_i64 tcg_temp_new_i64(void)
892{
Richard Henderson5bfa8032018-02-22 18:17:57 -0800893 TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_I64, false);
894 return temp_tcgv_i64(t);
pbrooka7812ae2008-11-17 14:43:54 +0000895}
Richard Hendersone1ccc052013-09-18 12:53:09 -0700896
pbrooka7812ae2008-11-17 14:43:54 +0000897static inline TCGv_i64 tcg_temp_local_new_i64(void)
898{
Richard Henderson5bfa8032018-02-22 18:17:57 -0800899 TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_I64, true);
900 return temp_tcgv_i64(t);
901}
902
903static inline TCGv_ptr tcg_global_mem_new_ptr(TCGv_ptr reg, intptr_t offset,
904 const char *name)
905{
906 TCGTemp *t = tcg_global_mem_new_internal(TCG_TYPE_PTR, reg, offset, name);
907 return temp_tcgv_ptr(t);
908}
909
910static inline TCGv_ptr tcg_temp_new_ptr(void)
911{
912 TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_PTR, false);
913 return temp_tcgv_ptr(t);
914}
915
916static inline TCGv_ptr tcg_temp_local_new_ptr(void)
917{
918 TCGTemp *t = tcg_temp_new_internal(TCG_TYPE_PTR, true);
919 return temp_tcgv_ptr(t);
pbrooka7812ae2008-11-17 14:43:54 +0000920}
pbrooka7812ae2008-11-17 14:43:54 +0000921
Peter Maydell27bfd832011-03-06 21:39:53 +0000922#if defined(CONFIG_DEBUG_TCG)
923/* If you call tcg_clear_temp_count() at the start of a section of
924 * code which is not supposed to leak any TCG temporaries, then
925 * calling tcg_check_temp_count() at the end of the section will
926 * return 1 if the section did in fact leak a temporary.
927 */
928void tcg_clear_temp_count(void);
929int tcg_check_temp_count(void);
930#else
931#define tcg_clear_temp_count() do { } while (0)
932#define tcg_check_temp_count() 0
933#endif
934
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400935int64_t tcg_cpu_exec_time(void);
Markus Armbruster3de2faa2019-04-17 21:17:52 +0200936void tcg_dump_info(void);
Markus Armbrusterd4c51a02019-04-17 21:17:51 +0200937void tcg_dump_op_count(void);
bellardc896fe22008-02-01 10:05:41 +0000938
939#define TCG_CT_ALIAS 0x80
940#define TCG_CT_IALIAS 0x40
Richard Henderson82790a82016-11-18 08:35:03 +0100941#define TCG_CT_NEWREG 0x20 /* output requires a new register */
bellardc896fe22008-02-01 10:05:41 +0000942#define TCG_CT_REG 0x01
943#define TCG_CT_CONST 0x02 /* any constant of register size */
944
945typedef struct TCGArgConstraint {
bellard5ff9d6a2008-02-04 00:37:54 +0000946 uint16_t ct;
947 uint8_t alias_index;
bellardc896fe22008-02-01 10:05:41 +0000948 union {
949 TCGRegSet regs;
950 } u;
951} TCGArgConstraint;
952
953#define TCG_MAX_OP_ARGS 16
954
Richard Henderson8399ad52011-08-17 14:11:45 -0700955/* Bits for TCGOpDef->flags, 8 bits available. */
956enum {
Richard Hendersonae36a242018-11-27 13:45:08 -0800957 /* Instruction exits the translation block. */
958 TCG_OPF_BB_EXIT = 0x01,
Richard Henderson8399ad52011-08-17 14:11:45 -0700959 /* Instruction defines the end of a basic block. */
Richard Hendersonae36a242018-11-27 13:45:08 -0800960 TCG_OPF_BB_END = 0x02,
Richard Henderson8399ad52011-08-17 14:11:45 -0700961 /* Instruction clobbers call registers and potentially update globals. */
Richard Hendersonae36a242018-11-27 13:45:08 -0800962 TCG_OPF_CALL_CLOBBER = 0x04,
Aurelien Jarno3d5c5f82012-10-09 21:53:08 +0200963 /* Instruction has side effects: it cannot be removed if its outputs
964 are not used, and might trigger exceptions. */
Richard Hendersonae36a242018-11-27 13:45:08 -0800965 TCG_OPF_SIDE_EFFECTS = 0x08,
Richard Henderson8399ad52011-08-17 14:11:45 -0700966 /* Instruction operands are 64-bits (otherwise 32-bits). */
Richard Hendersonae36a242018-11-27 13:45:08 -0800967 TCG_OPF_64BIT = 0x10,
Richard Hendersonc1a61f62013-05-02 11:57:40 +0100968 /* Instruction is optional and not implemented by the host, or insn
969 is generic and should not be implemened by the host. */
Richard Hendersonae36a242018-11-27 13:45:08 -0800970 TCG_OPF_NOT_PRESENT = 0x20,
Richard Hendersond2fd7452017-09-14 13:53:46 -0700971 /* Instruction operands are vectors. */
Richard Hendersonae36a242018-11-27 13:45:08 -0800972 TCG_OPF_VECTOR = 0x40,
Richard Henderson8399ad52011-08-17 14:11:45 -0700973};
bellardc896fe22008-02-01 10:05:41 +0000974
975typedef struct TCGOpDef {
976 const char *name;
977 uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
978 uint8_t flags;
bellardc896fe22008-02-01 10:05:41 +0000979 TCGArgConstraint *args_ct;
980 int *sorted_args;
Stefan Weilc68aaa12010-02-15 17:17:21 +0100981#if defined(CONFIG_DEBUG_TCG)
982 int used;
983#endif
bellardc896fe22008-02-01 10:05:41 +0000984} TCGOpDef;
Richard Henderson8399ad52011-08-17 14:11:45 -0700985
986extern TCGOpDef tcg_op_defs[];
Stefan Weil2a243742011-09-29 18:33:21 +0200987extern const size_t tcg_op_defs_max;
988
bellardc896fe22008-02-01 10:05:41 +0000989typedef struct TCGTargetOpDef {
Richard Hendersona9751602010-03-19 11:12:29 -0700990 TCGOpcode op;
bellardc896fe22008-02-01 10:05:41 +0000991 const char *args_ct_str[TCG_MAX_OP_ARGS];
992} TCGTargetOpDef;
993
bellardc896fe22008-02-01 10:05:41 +0000994#define tcg_abort() \
995do {\
996 fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
997 abort();\
998} while (0)
999
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001000bool tcg_op_supported(TCGOpcode op);
1001
Richard Hendersonae8b75d2017-10-15 13:27:56 -07001002void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args);
pbrooka7812ae2008-11-17 14:43:54 +00001003
Richard Henderson15fa08f2017-11-02 15:19:14 +01001004TCGOp *tcg_emit_op(TCGOpcode opc);
Richard Henderson0c627cd2014-03-30 16:51:54 -07001005void tcg_op_remove(TCGContext *s, TCGOp *op);
Emilio G. Cotaac1043f2018-12-09 14:37:19 -05001006TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *op, TCGOpcode opc);
1007TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *op, TCGOpcode opc);
Richard Henderson5a184072016-06-23 20:34:33 -07001008
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07001009void tcg_optimize(TCGContext *s);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04001010
pbrooka7812ae2008-11-17 14:43:54 +00001011TCGv_i32 tcg_const_i32(int32_t val);
1012TCGv_i64 tcg_const_i64(int64_t val);
1013TCGv_i32 tcg_const_local_i32(int32_t val);
1014TCGv_i64 tcg_const_local_i64(int64_t val);
Richard Hendersond2fd7452017-09-14 13:53:46 -07001015TCGv_vec tcg_const_zeros_vec(TCGType);
1016TCGv_vec tcg_const_ones_vec(TCGType);
1017TCGv_vec tcg_const_zeros_vec_matching(TCGv_vec);
1018TCGv_vec tcg_const_ones_vec_matching(TCGv_vec);
pbrooka7812ae2008-11-17 14:43:54 +00001019
Richard Henderson5bfa8032018-02-22 18:17:57 -08001020#if UINTPTR_MAX == UINT32_MAX
1021# define tcg_const_ptr(x) ((TCGv_ptr)tcg_const_i32((intptr_t)(x)))
1022# define tcg_const_local_ptr(x) ((TCGv_ptr)tcg_const_local_i32((intptr_t)(x)))
1023#else
1024# define tcg_const_ptr(x) ((TCGv_ptr)tcg_const_i64((intptr_t)(x)))
1025# define tcg_const_local_ptr(x) ((TCGv_ptr)tcg_const_local_i64((intptr_t)(x)))
1026#endif
1027
Richard Henderson42a268c2015-02-13 12:51:55 -08001028TCGLabel *gen_new_label(void);
1029
1030/**
1031 * label_arg
1032 * @l: label
1033 *
1034 * Encode a label for storage in the TCG opcode stream.
1035 */
1036
1037static inline TCGArg label_arg(TCGLabel *l)
1038{
Richard Henderson51e39722015-02-13 18:51:05 -08001039 return (uintptr_t)l;
Richard Henderson42a268c2015-02-13 12:51:55 -08001040}
1041
1042/**
1043 * arg_label
1044 * @i: value
1045 *
1046 * The opposite of label_arg. Retrieve a label from the
1047 * encoding of the TCG opcode stream.
1048 */
1049
Richard Henderson51e39722015-02-13 18:51:05 -08001050static inline TCGLabel *arg_label(TCGArg i)
Richard Henderson42a268c2015-02-13 12:51:55 -08001051{
Richard Henderson51e39722015-02-13 18:51:05 -08001052 return (TCGLabel *)(uintptr_t)i;
Richard Henderson42a268c2015-02-13 12:51:55 -08001053}
1054
Peter Maydell09800112013-02-22 18:10:00 +00001055/**
Richard Henderson52a1f642014-03-31 14:27:27 -07001056 * tcg_ptr_byte_diff
1057 * @a, @b: addresses to be differenced
1058 *
1059 * There are many places within the TCG backends where we need a byte
1060 * difference between two pointers. While this can be accomplished
1061 * with local casting, it's easy to get wrong -- especially if one is
1062 * concerned with the signedness of the result.
1063 *
1064 * This version relies on GCC's void pointer arithmetic to get the
1065 * correct result.
1066 */
1067
1068static inline ptrdiff_t tcg_ptr_byte_diff(void *a, void *b)
1069{
1070 return a - b;
1071}
1072
1073/**
1074 * tcg_pcrel_diff
1075 * @s: the tcg context
1076 * @target: address of the target
1077 *
1078 * Produce a pc-relative difference, from the current code_ptr
1079 * to the destination address.
1080 */
1081
1082static inline ptrdiff_t tcg_pcrel_diff(TCGContext *s, void *target)
1083{
1084 return tcg_ptr_byte_diff(target, s->code_ptr);
1085}
1086
1087/**
1088 * tcg_current_code_size
1089 * @s: the tcg context
1090 *
1091 * Compute the current code size within the translation block.
1092 * This is used to fill in qemu's data structures for goto_tb.
1093 */
1094
1095static inline size_t tcg_current_code_size(TCGContext *s)
1096{
1097 return tcg_ptr_byte_diff(s->code_ptr, s->code_buf);
1098}
1099
Tony Nguyen14776ab2019-08-24 04:10:58 +10001100/* Combine the MemOp and mmu_idx parameters into a single value. */
Richard Henderson59227d52015-05-12 11:51:44 -07001101typedef uint32_t TCGMemOpIdx;
1102
1103/**
1104 * make_memop_idx
1105 * @op: memory operation
1106 * @idx: mmu index
1107 *
1108 * Encode these values into a single parameter.
1109 */
Tony Nguyen14776ab2019-08-24 04:10:58 +10001110static inline TCGMemOpIdx make_memop_idx(MemOp op, unsigned idx)
Richard Henderson59227d52015-05-12 11:51:44 -07001111{
1112 tcg_debug_assert(idx <= 15);
1113 return (op << 4) | idx;
1114}
1115
1116/**
1117 * get_memop
1118 * @oi: combined op/idx parameter
1119 *
1120 * Extract the memory operation from the combined value.
1121 */
Tony Nguyen14776ab2019-08-24 04:10:58 +10001122static inline MemOp get_memop(TCGMemOpIdx oi)
Richard Henderson59227d52015-05-12 11:51:44 -07001123{
1124 return oi >> 4;
1125}
1126
1127/**
1128 * get_mmuidx
1129 * @oi: combined op/idx parameter
1130 *
1131 * Extract the mmu index from the combined value.
1132 */
1133static inline unsigned get_mmuidx(TCGMemOpIdx oi)
1134{
1135 return oi & 15;
1136}
1137
Richard Henderson52a1f642014-03-31 14:27:27 -07001138/**
Peter Maydell09800112013-02-22 18:10:00 +00001139 * tcg_qemu_tb_exec:
Sergey Fedorov819af242016-04-21 15:58:23 +03001140 * @env: pointer to CPUArchState for the CPU
Peter Maydell09800112013-02-22 18:10:00 +00001141 * @tb_ptr: address of generated code for the TB to execute
1142 *
1143 * Start executing code from a given translation block.
1144 * Where translation blocks have been linked, execution
1145 * may proceed from the given TB into successive ones.
1146 * Control eventually returns only when some action is needed
1147 * from the top-level loop: either control must pass to a TB
1148 * which has not yet been directly linked, or an asynchronous
1149 * event such as an interrupt needs handling.
1150 *
Sergey Fedorov819af242016-04-21 15:58:23 +03001151 * Return: The return value is the value passed to the corresponding
1152 * tcg_gen_exit_tb() at translation time of the last TB attempted to execute.
1153 * The value is either zero or a 4-byte aligned pointer to that TB combined
1154 * with additional information in its two least significant bits. The
1155 * additional information is encoded as follows:
Peter Maydell09800112013-02-22 18:10:00 +00001156 * 0, 1: the link between this TB and the next is via the specified
1157 * TB index (0 or 1). That is, we left the TB via (the equivalent
1158 * of) "goto_tb <index>". The main loop uses this to determine
1159 * how to link the TB just executed to the next.
1160 * 2: we are using instruction counting code generation, and we
1161 * did not start executing this TB because the instruction counter
Sergey Fedorov819af242016-04-21 15:58:23 +03001162 * would hit zero midway through it. In this case the pointer
Peter Maydell09800112013-02-22 18:10:00 +00001163 * returned is the TB we were about to execute, and the caller must
1164 * arrange to execute the remaining count of instructions.
Peter Maydell378df4b2013-02-22 18:10:03 +00001165 * 3: we stopped because the CPU's exit_request flag was set
1166 * (usually meaning that there is an interrupt that needs to be
Sergey Fedorov819af242016-04-21 15:58:23 +03001167 * handled). The pointer returned is the TB we were about to execute
1168 * when we noticed the pending exit request.
Peter Maydell09800112013-02-22 18:10:00 +00001169 *
1170 * If the bottom two bits indicate an exit-via-index then the CPU
1171 * state is correctly synchronised and ready for execution of the next
1172 * TB (and in particular the guest PC is the address to execute next).
1173 * Otherwise, we gave up on execution of this TB before it started, and
Peter Crosthwaitefee068e2015-04-29 00:52:21 -07001174 * the caller must fix up the CPU state by calling the CPU's
Sergey Fedorov819af242016-04-21 15:58:23 +03001175 * synchronize_from_tb() method with the TB pointer we return (falling
Peter Crosthwaitefee068e2015-04-29 00:52:21 -07001176 * back to calling the CPU's set_pc method with tb->pb if no
1177 * synchronize_from_tb() method exists).
Peter Maydell09800112013-02-22 18:10:00 +00001178 *
1179 * Note that TCG targets may use a different definition of tcg_qemu_tb_exec
1180 * to this default (which just calls the prologue.code emitted by
1181 * tcg_target_qemu_prologue()).
1182 */
Richard Henderson07ea28b2018-05-30 18:06:23 -07001183#define TB_EXIT_MASK 3
1184#define TB_EXIT_IDX0 0
1185#define TB_EXIT_IDX1 1
1186#define TB_EXIT_IDXMAX 1
Peter Maydell378df4b2013-02-22 18:10:03 +00001187#define TB_EXIT_REQUESTED 3
Peter Maydell09800112013-02-22 18:10:00 +00001188
Paolo Bonzini5a58e882015-05-19 09:59:34 +02001189#ifdef HAVE_TCG_QEMU_TB_EXEC
1190uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr);
1191#else
Stefan Weilce285b12011-09-30 21:23:06 +02001192# define tcg_qemu_tb_exec(env, tb_ptr) \
Emilio G. Cotab1311c42017-07-12 17:15:52 -04001193 ((uintptr_t (*)(void *, void *))tcg_ctx->code_gen_prologue)(env, tb_ptr)
bellard932a6902008-05-30 20:56:52 +00001194#endif
Richard Henderson813da622012-03-19 12:25:11 -07001195
1196void tcg_register_jit(void *buf, size_t buf_size);
Yeongkyoon Leeb76f0d82012-10-31 16:04:25 +09001197
Richard Hendersondb432672017-09-15 14:11:45 -07001198#if TCG_TARGET_MAYBE_vec
1199/* Return zero if the tuple (opc, type, vece) is unsupportable;
1200 return > 0 if it is directly supportable;
1201 return < 0 if we must call tcg_expand_vec_op. */
1202int tcg_can_emit_vec_op(TCGOpcode, TCGType, unsigned);
1203#else
1204static inline int tcg_can_emit_vec_op(TCGOpcode o, TCGType t, unsigned ve)
1205{
1206 return 0;
1207}
1208#endif
1209
1210/* Expand the tuple (opc, type, vece) on the given arguments. */
1211void tcg_expand_vec_op(TCGOpcode, TCGType, unsigned, TCGArg, ...);
1212
1213/* Replicate a constant C accoring to the log2 of the element size. */
1214uint64_t dup_const(unsigned vece, uint64_t c);
1215
1216#define dup_const(VECE, C) \
1217 (__builtin_constant_p(VECE) \
1218 ? ( (VECE) == MO_8 ? 0x0101010101010101ull * (uint8_t)(C) \
1219 : (VECE) == MO_16 ? 0x0001000100010001ull * (uint16_t)(C) \
1220 : (VECE) == MO_32 ? 0x0000000100000001ull * (uint32_t)(C) \
1221 : dup_const(VECE, C)) \
1222 : dup_const(VECE, C))
1223
1224
Richard Hendersone58eb532013-08-27 13:13:44 -07001225/*
1226 * Memory helpers that will be used by TCG generated code.
1227 */
1228#ifdef CONFIG_SOFTMMU
Richard Hendersonc8f94df2013-08-27 14:09:14 -07001229/* Value zero-extended to tcg register size. */
1230tcg_target_ulong helper_ret_ldub_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001231 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001232tcg_target_ulong helper_le_lduw_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001233 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001234tcg_target_ulong helper_le_ldul_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001235 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001236uint64_t helper_le_ldq_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001237 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001238tcg_target_ulong helper_be_lduw_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001239 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001240tcg_target_ulong helper_be_ldul_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001241 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001242uint64_t helper_be_ldq_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001243 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Hendersone58eb532013-08-27 13:13:44 -07001244
Richard Hendersonc8f94df2013-08-27 14:09:14 -07001245/* Value sign-extended to tcg register size. */
1246tcg_target_ulong helper_ret_ldsb_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001247 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001248tcg_target_ulong helper_le_ldsw_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001249 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001250tcg_target_ulong helper_le_ldsl_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001251 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001252tcg_target_ulong helper_be_ldsw_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001253 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001254tcg_target_ulong helper_be_ldsl_mmu(CPUArchState *env, target_ulong addr,
Richard Henderson3972ef62015-05-13 09:10:33 -07001255 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Hendersonc8f94df2013-08-27 14:09:14 -07001256
Richard Hendersone58eb532013-08-27 13:13:44 -07001257void helper_ret_stb_mmu(CPUArchState *env, target_ulong addr, uint8_t val,
Richard Henderson3972ef62015-05-13 09:10:33 -07001258 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001259void helper_le_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val,
Richard Henderson3972ef62015-05-13 09:10:33 -07001260 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001261void helper_le_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val,
Richard Henderson3972ef62015-05-13 09:10:33 -07001262 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001263void helper_le_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val,
Richard Henderson3972ef62015-05-13 09:10:33 -07001264 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001265void helper_be_stw_mmu(CPUArchState *env, target_ulong addr, uint16_t val,
Richard Henderson3972ef62015-05-13 09:10:33 -07001266 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001267void helper_be_stl_mmu(CPUArchState *env, target_ulong addr, uint32_t val,
Richard Henderson3972ef62015-05-13 09:10:33 -07001268 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001269void helper_be_stq_mmu(CPUArchState *env, target_ulong addr, uint64_t val,
Richard Henderson3972ef62015-05-13 09:10:33 -07001270 TCGMemOpIdx oi, uintptr_t retaddr);
Richard Henderson867b3202013-09-04 11:45:20 -07001271
Pavel Dovgalyuk282dffc2015-07-10 12:56:50 +03001272uint8_t helper_ret_ldb_cmmu(CPUArchState *env, target_ulong addr,
1273 TCGMemOpIdx oi, uintptr_t retaddr);
1274uint16_t helper_le_ldw_cmmu(CPUArchState *env, target_ulong addr,
1275 TCGMemOpIdx oi, uintptr_t retaddr);
1276uint32_t helper_le_ldl_cmmu(CPUArchState *env, target_ulong addr,
1277 TCGMemOpIdx oi, uintptr_t retaddr);
1278uint64_t helper_le_ldq_cmmu(CPUArchState *env, target_ulong addr,
1279 TCGMemOpIdx oi, uintptr_t retaddr);
1280uint16_t helper_be_ldw_cmmu(CPUArchState *env, target_ulong addr,
1281 TCGMemOpIdx oi, uintptr_t retaddr);
1282uint32_t helper_be_ldl_cmmu(CPUArchState *env, target_ulong addr,
1283 TCGMemOpIdx oi, uintptr_t retaddr);
1284uint64_t helper_be_ldq_cmmu(CPUArchState *env, target_ulong addr,
1285 TCGMemOpIdx oi, uintptr_t retaddr);
1286
Richard Henderson867b3202013-09-04 11:45:20 -07001287/* Temporary aliases until backends are converted. */
1288#ifdef TARGET_WORDS_BIGENDIAN
1289# define helper_ret_ldsw_mmu helper_be_ldsw_mmu
1290# define helper_ret_lduw_mmu helper_be_lduw_mmu
1291# define helper_ret_ldsl_mmu helper_be_ldsl_mmu
1292# define helper_ret_ldul_mmu helper_be_ldul_mmu
Pavel Dovgalyuk282dffc2015-07-10 12:56:50 +03001293# define helper_ret_ldl_mmu helper_be_ldul_mmu
Richard Henderson867b3202013-09-04 11:45:20 -07001294# define helper_ret_ldq_mmu helper_be_ldq_mmu
1295# define helper_ret_stw_mmu helper_be_stw_mmu
1296# define helper_ret_stl_mmu helper_be_stl_mmu
1297# define helper_ret_stq_mmu helper_be_stq_mmu
Pavel Dovgalyuk282dffc2015-07-10 12:56:50 +03001298# define helper_ret_ldw_cmmu helper_be_ldw_cmmu
1299# define helper_ret_ldl_cmmu helper_be_ldl_cmmu
1300# define helper_ret_ldq_cmmu helper_be_ldq_cmmu
Richard Henderson867b3202013-09-04 11:45:20 -07001301#else
1302# define helper_ret_ldsw_mmu helper_le_ldsw_mmu
1303# define helper_ret_lduw_mmu helper_le_lduw_mmu
1304# define helper_ret_ldsl_mmu helper_le_ldsl_mmu
1305# define helper_ret_ldul_mmu helper_le_ldul_mmu
Pavel Dovgalyuk282dffc2015-07-10 12:56:50 +03001306# define helper_ret_ldl_mmu helper_le_ldul_mmu
Richard Henderson867b3202013-09-04 11:45:20 -07001307# define helper_ret_ldq_mmu helper_le_ldq_mmu
1308# define helper_ret_stw_mmu helper_le_stw_mmu
1309# define helper_ret_stl_mmu helper_le_stl_mmu
1310# define helper_ret_stq_mmu helper_le_stq_mmu
Pavel Dovgalyuk282dffc2015-07-10 12:56:50 +03001311# define helper_ret_ldw_cmmu helper_le_ldw_cmmu
1312# define helper_ret_ldl_cmmu helper_le_ldl_cmmu
1313# define helper_ret_ldq_cmmu helper_le_ldq_cmmu
Richard Henderson867b3202013-09-04 11:45:20 -07001314#endif
Richard Hendersone58eb532013-08-27 13:13:44 -07001315
Richard Hendersonc482cb12016-06-28 11:37:27 -07001316uint32_t helper_atomic_cmpxchgb_mmu(CPUArchState *env, target_ulong addr,
1317 uint32_t cmpv, uint32_t newv,
1318 TCGMemOpIdx oi, uintptr_t retaddr);
1319uint32_t helper_atomic_cmpxchgw_le_mmu(CPUArchState *env, target_ulong addr,
1320 uint32_t cmpv, uint32_t newv,
1321 TCGMemOpIdx oi, uintptr_t retaddr);
1322uint32_t helper_atomic_cmpxchgl_le_mmu(CPUArchState *env, target_ulong addr,
1323 uint32_t cmpv, uint32_t newv,
1324 TCGMemOpIdx oi, uintptr_t retaddr);
1325uint64_t helper_atomic_cmpxchgq_le_mmu(CPUArchState *env, target_ulong addr,
1326 uint64_t cmpv, uint64_t newv,
1327 TCGMemOpIdx oi, uintptr_t retaddr);
1328uint32_t helper_atomic_cmpxchgw_be_mmu(CPUArchState *env, target_ulong addr,
1329 uint32_t cmpv, uint32_t newv,
1330 TCGMemOpIdx oi, uintptr_t retaddr);
1331uint32_t helper_atomic_cmpxchgl_be_mmu(CPUArchState *env, target_ulong addr,
1332 uint32_t cmpv, uint32_t newv,
1333 TCGMemOpIdx oi, uintptr_t retaddr);
1334uint64_t helper_atomic_cmpxchgq_be_mmu(CPUArchState *env, target_ulong addr,
1335 uint64_t cmpv, uint64_t newv,
1336 TCGMemOpIdx oi, uintptr_t retaddr);
1337
1338#define GEN_ATOMIC_HELPER(NAME, TYPE, SUFFIX) \
1339TYPE helper_atomic_ ## NAME ## SUFFIX ## _mmu \
1340 (CPUArchState *env, target_ulong addr, TYPE val, \
1341 TCGMemOpIdx oi, uintptr_t retaddr);
1342
Richard Hendersondf79b992016-09-02 12:23:57 -07001343#ifdef CONFIG_ATOMIC64
Richard Hendersonc482cb12016-06-28 11:37:27 -07001344#define GEN_ATOMIC_HELPER_ALL(NAME) \
Richard Hendersondf79b992016-09-02 12:23:57 -07001345 GEN_ATOMIC_HELPER(NAME, uint32_t, b) \
Richard Hendersonc482cb12016-06-28 11:37:27 -07001346 GEN_ATOMIC_HELPER(NAME, uint32_t, w_le) \
Richard Hendersonc482cb12016-06-28 11:37:27 -07001347 GEN_ATOMIC_HELPER(NAME, uint32_t, w_be) \
Richard Hendersondf79b992016-09-02 12:23:57 -07001348 GEN_ATOMIC_HELPER(NAME, uint32_t, l_le) \
Richard Hendersonc482cb12016-06-28 11:37:27 -07001349 GEN_ATOMIC_HELPER(NAME, uint32_t, l_be) \
Richard Hendersondf79b992016-09-02 12:23:57 -07001350 GEN_ATOMIC_HELPER(NAME, uint64_t, q_le) \
Richard Hendersonc482cb12016-06-28 11:37:27 -07001351 GEN_ATOMIC_HELPER(NAME, uint64_t, q_be)
Richard Hendersondf79b992016-09-02 12:23:57 -07001352#else
1353#define GEN_ATOMIC_HELPER_ALL(NAME) \
1354 GEN_ATOMIC_HELPER(NAME, uint32_t, b) \
1355 GEN_ATOMIC_HELPER(NAME, uint32_t, w_le) \
1356 GEN_ATOMIC_HELPER(NAME, uint32_t, w_be) \
1357 GEN_ATOMIC_HELPER(NAME, uint32_t, l_le) \
1358 GEN_ATOMIC_HELPER(NAME, uint32_t, l_be)
1359#endif
Richard Hendersonc482cb12016-06-28 11:37:27 -07001360
1361GEN_ATOMIC_HELPER_ALL(fetch_add)
1362GEN_ATOMIC_HELPER_ALL(fetch_sub)
1363GEN_ATOMIC_HELPER_ALL(fetch_and)
1364GEN_ATOMIC_HELPER_ALL(fetch_or)
1365GEN_ATOMIC_HELPER_ALL(fetch_xor)
Richard Henderson5507c2b2018-05-10 18:10:57 +01001366GEN_ATOMIC_HELPER_ALL(fetch_smin)
1367GEN_ATOMIC_HELPER_ALL(fetch_umin)
1368GEN_ATOMIC_HELPER_ALL(fetch_smax)
1369GEN_ATOMIC_HELPER_ALL(fetch_umax)
Richard Hendersonc482cb12016-06-28 11:37:27 -07001370
1371GEN_ATOMIC_HELPER_ALL(add_fetch)
1372GEN_ATOMIC_HELPER_ALL(sub_fetch)
1373GEN_ATOMIC_HELPER_ALL(and_fetch)
1374GEN_ATOMIC_HELPER_ALL(or_fetch)
1375GEN_ATOMIC_HELPER_ALL(xor_fetch)
Richard Henderson5507c2b2018-05-10 18:10:57 +01001376GEN_ATOMIC_HELPER_ALL(smin_fetch)
1377GEN_ATOMIC_HELPER_ALL(umin_fetch)
1378GEN_ATOMIC_HELPER_ALL(smax_fetch)
1379GEN_ATOMIC_HELPER_ALL(umax_fetch)
Richard Hendersonc482cb12016-06-28 11:37:27 -07001380
1381GEN_ATOMIC_HELPER_ALL(xchg)
1382
1383#undef GEN_ATOMIC_HELPER_ALL
1384#undef GEN_ATOMIC_HELPER
Richard Hendersone58eb532013-08-27 13:13:44 -07001385#endif /* CONFIG_SOFTMMU */
1386
Richard Hendersone6cd4bb2018-08-15 16:31:47 -07001387/*
1388 * These aren't really a "proper" helpers because TCG cannot manage Int128.
1389 * However, use the same format as the others, for use by the backends.
1390 *
1391 * The cmpxchg functions are only defined if HAVE_CMPXCHG128;
1392 * the ld/st functions are only defined if HAVE_ATOMIC128,
1393 * as defined by <qemu/atomic128.h>.
1394 */
Richard Henderson7ebee432016-06-29 21:10:59 -07001395Int128 helper_atomic_cmpxchgo_le_mmu(CPUArchState *env, target_ulong addr,
1396 Int128 cmpv, Int128 newv,
1397 TCGMemOpIdx oi, uintptr_t retaddr);
1398Int128 helper_atomic_cmpxchgo_be_mmu(CPUArchState *env, target_ulong addr,
1399 Int128 cmpv, Int128 newv,
1400 TCGMemOpIdx oi, uintptr_t retaddr);
1401
1402Int128 helper_atomic_ldo_le_mmu(CPUArchState *env, target_ulong addr,
1403 TCGMemOpIdx oi, uintptr_t retaddr);
1404Int128 helper_atomic_ldo_be_mmu(CPUArchState *env, target_ulong addr,
1405 TCGMemOpIdx oi, uintptr_t retaddr);
1406void helper_atomic_sto_le_mmu(CPUArchState *env, target_ulong addr, Int128 val,
1407 TCGMemOpIdx oi, uintptr_t retaddr);
1408void helper_atomic_sto_be_mmu(CPUArchState *env, target_ulong addr, Int128 val,
1409 TCGMemOpIdx oi, uintptr_t retaddr);
1410
Richard Henderson53229a72019-03-17 00:27:29 +00001411#ifdef CONFIG_DEBUG_TCG
1412void tcg_assert_listed_vecop(TCGOpcode);
1413#else
1414static inline void tcg_assert_listed_vecop(TCGOpcode op) { }
1415#endif
1416
1417static inline const TCGOpcode *tcg_swap_vecop_list(const TCGOpcode *n)
1418{
1419#ifdef CONFIG_DEBUG_TCG
1420 const TCGOpcode *o = tcg_ctx->vecop_list;
1421 tcg_ctx->vecop_list = n;
1422 return o;
1423#else
1424 return NULL;
1425#endif
1426}
1427
1428bool tcg_can_emit_vecop_list(const TCGOpcode *, TCGType, unsigned);
1429
Richard Hendersone58eb532013-08-27 13:13:44 -07001430#endif /* TCG_H */