2
0

tcg-ops.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. .. _tcg-ops-ref:
  2. *******************************
  3. TCG Intermediate Representation
  4. *******************************
  5. Introduction
  6. ============
  7. TCG (Tiny Code Generator) began as a generic backend for a C compiler.
  8. It was simplified to be used in QEMU. It also has its roots in the
  9. QOP code generator written by Paul Brook.
  10. Definitions
  11. ===========
  12. The TCG *target* is the architecture for which we generate the code.
  13. It is of course not the same as the "target" of QEMU which is the
  14. emulated architecture. As TCG started as a generic C backend used
  15. for cross compiling, the assumption was that TCG target might be
  16. different from the host, although this is never the case for QEMU.
  17. In this document, we use *guest* to specify what architecture we are
  18. emulating; *target* always means the TCG target, the machine on which
  19. we are running QEMU.
  20. An operation with *undefined behavior* may result in a crash.
  21. An operation with *unspecified behavior* shall not crash. However,
  22. the result may be one of several possibilities so may be considered
  23. an *undefined result*.
  24. Basic Blocks
  25. ============
  26. A TCG *basic block* is a single entry, multiple exit region which
  27. corresponds to a list of instructions terminated by a label, or
  28. any branch instruction.
  29. A TCG *extended basic block* is a single entry, multiple exit region
  30. which corresponds to a list of instructions terminated by a label or
  31. an unconditional branch. Specifically, an extended basic block is
  32. a sequence of basic blocks connected by the fall-through paths of
  33. zero or more conditional branch instructions.
  34. Operations
  35. ==========
  36. TCG instructions or *ops* operate on TCG *variables*, both of which
  37. are strongly typed. Each instruction has a fixed number of output
  38. variable operands, input variable operands and constant operands.
  39. Vector instructions have a field specifying the element size within
  40. the vector. The notable exception is the call instruction which has
  41. a variable number of outputs and inputs.
  42. In the textual form, output operands usually come first, followed by
  43. input operands, followed by constant operands. The output type is
  44. included in the instruction name. Constants are prefixed with a '$'.
  45. .. code-block:: none
  46. add_i32 t0, t1, t2 /* (t0 <- t1 + t2) */
  47. Variables
  48. =========
  49. * ``TEMP_FIXED``
  50. There is one TCG *fixed global* variable, ``cpu_env``, which is
  51. live in all translation blocks, and holds a pointer to ``CPUArchState``.
  52. This variable is held in a host cpu register at all times in all
  53. translation blocks.
  54. * ``TEMP_GLOBAL``
  55. A TCG *global* is a variable which is live in all translation blocks,
  56. and corresponds to memory location that is within ``CPUArchState``.
  57. These may be specified as an offset from ``cpu_env``, in which case
  58. they are called *direct globals*, or may be specified as an offset
  59. from a direct global, in which case they are called *indirect globals*.
  60. Even indirect globals should still reference memory within
  61. ``CPUArchState``. All TCG globals are defined during
  62. ``TCGCPUOps.initialize``, before any translation blocks are generated.
  63. * ``TEMP_CONST``
  64. A TCG *constant* is a variable which is live throughout the entire
  65. translation block, and contains a constant value. These variables
  66. are allocated on demand during translation and are hashed so that
  67. there is exactly one variable holding a given value.
  68. * ``TEMP_TB``
  69. A TCG *translation block temporary* is a variable which is live
  70. throughout the entire translation block, but dies on any exit.
  71. These temporaries are allocated explicitly during translation.
  72. * ``TEMP_EBB``
  73. A TCG *extended basic block temporary* is a variable which is live
  74. throughout an extended basic block, but dies on any exit.
  75. These temporaries are allocated explicitly during translation.
  76. Types
  77. =====
  78. * ``TCG_TYPE_I32``
  79. A 32-bit integer.
  80. * ``TCG_TYPE_I64``
  81. A 64-bit integer. For 32-bit hosts, such variables are split into a pair
  82. of variables with ``type=TCG_TYPE_I32`` and ``base_type=TCG_TYPE_I64``.
  83. The ``temp_subindex`` for each indicates where it falls within the
  84. host-endian representation.
  85. * ``TCG_TYPE_PTR``
  86. An alias for ``TCG_TYPE_I32`` or ``TCG_TYPE_I64``, depending on the size
  87. of a pointer for the host.
  88. * ``TCG_TYPE_REG``
  89. An alias for ``TCG_TYPE_I32`` or ``TCG_TYPE_I64``, depending on the size
  90. of the integer registers for the host. This may be larger
  91. than ``TCG_TYPE_PTR`` depending on the host ABI.
  92. * ``TCG_TYPE_I128``
  93. A 128-bit integer. For all hosts, such variables are split into a number
  94. of variables with ``type=TCG_TYPE_REG`` and ``base_type=TCG_TYPE_I128``.
  95. The ``temp_subindex`` for each indicates where it falls within the
  96. host-endian representation.
  97. * ``TCG_TYPE_V64``
  98. A 64-bit vector. This type is valid only if the TCG target
  99. sets ``TCG_TARGET_HAS_v64``.
  100. * ``TCG_TYPE_V128``
  101. A 128-bit vector. This type is valid only if the TCG target
  102. sets ``TCG_TARGET_HAS_v128``.
  103. * ``TCG_TYPE_V256``
  104. A 256-bit vector. This type is valid only if the TCG target
  105. sets ``TCG_TARGET_HAS_v256``.
  106. Helpers
  107. =======
  108. Helpers are registered in a guest-specific ``helper.h``,
  109. which is processed to generate ``tcg_gen_helper_*`` functions.
  110. With these functions it is possible to call a function taking
  111. i32, i64, i128 or pointer types.
  112. By default, before calling a helper, all globals are stored at their
  113. canonical location. By default, the helper is allowed to modify the
  114. CPU state (including the state represented by tcg globals)
  115. or may raise an exception. This default can be overridden using the
  116. following function modifiers:
  117. * ``TCG_CALL_NO_WRITE_GLOBALS``
  118. The helper does not modify any globals, but may read them.
  119. Globals will be saved to their canonical location before calling helpers,
  120. but need not be reloaded afterwards.
  121. * ``TCG_CALL_NO_READ_GLOBALS``
  122. The helper does not read globals, either directly or via an exception.
  123. They will not be saved to their canonical locations before calling
  124. the helper. This implies ``TCG_CALL_NO_WRITE_GLOBALS``.
  125. * ``TCG_CALL_NO_SIDE_EFFECTS``
  126. The call to the helper function may be removed if the return value is
  127. not used. This means that it may not modify any CPU state nor may it
  128. raise an exception.
  129. Code Optimizations
  130. ==================
  131. When generating instructions, you can count on at least the following
  132. optimizations:
  133. - Single instructions are simplified, e.g.
  134. .. code-block:: none
  135. and_i32 t0, t0, $0xffffffff
  136. is suppressed.
  137. - A liveness analysis is done at the basic block level. The
  138. information is used to suppress moves from a dead variable to
  139. another one. It is also used to remove instructions which compute
  140. dead results. The later is especially useful for condition code
  141. optimization in QEMU.
  142. In the following example:
  143. .. code-block:: none
  144. add_i32 t0, t1, t2
  145. add_i32 t0, t0, $1
  146. mov_i32 t0, $1
  147. only the last instruction is kept.
  148. Instruction Reference
  149. =====================
  150. Function call
  151. -------------
  152. .. list-table::
  153. * - call *<ret>* *<params>* ptr
  154. - | call function 'ptr' (pointer type)
  155. |
  156. | *<ret>* optional 32 bit or 64 bit return value
  157. | *<params>* optional 32 bit or 64 bit parameters
  158. Jumps/Labels
  159. ------------
  160. .. list-table::
  161. * - set_label $label
  162. - | Define label 'label' at the current program point.
  163. * - br $label
  164. - | Jump to label.
  165. * - brcond_i32/i64 *t0*, *t1*, *cond*, *label*
  166. - | Conditional jump if *t0* *cond* *t1* is true. *cond* can be:
  167. |
  168. | ``TCG_COND_EQ``
  169. | ``TCG_COND_NE``
  170. | ``TCG_COND_LT /* signed */``
  171. | ``TCG_COND_GE /* signed */``
  172. | ``TCG_COND_LE /* signed */``
  173. | ``TCG_COND_GT /* signed */``
  174. | ``TCG_COND_LTU /* unsigned */``
  175. | ``TCG_COND_GEU /* unsigned */``
  176. | ``TCG_COND_LEU /* unsigned */``
  177. | ``TCG_COND_GTU /* unsigned */``
  178. | ``TCG_COND_TSTEQ /* t1 & t2 == 0 */``
  179. | ``TCG_COND_TSTNE /* t1 & t2 != 0 */``
  180. Arithmetic
  181. ----------
  182. .. list-table::
  183. * - add_i32/i64 *t0*, *t1*, *t2*
  184. - | *t0* = *t1* + *t2*
  185. * - sub_i32/i64 *t0*, *t1*, *t2*
  186. - | *t0* = *t1* - *t2*
  187. * - neg_i32/i64 *t0*, *t1*
  188. - | *t0* = -*t1* (two's complement)
  189. * - mul_i32/i64 *t0*, *t1*, *t2*
  190. - | *t0* = *t1* * *t2*
  191. * - div_i32/i64 *t0*, *t1*, *t2*
  192. - | *t0* = *t1* / *t2* (signed)
  193. | Undefined behavior if division by zero or overflow.
  194. * - divu_i32/i64 *t0*, *t1*, *t2*
  195. - | *t0* = *t1* / *t2* (unsigned)
  196. | Undefined behavior if division by zero.
  197. * - rem_i32/i64 *t0*, *t1*, *t2*
  198. - | *t0* = *t1* % *t2* (signed)
  199. | Undefined behavior if division by zero or overflow.
  200. * - remu_i32/i64 *t0*, *t1*, *t2*
  201. - | *t0* = *t1* % *t2* (unsigned)
  202. | Undefined behavior if division by zero.
  203. Logical
  204. -------
  205. .. list-table::
  206. * - and_i32/i64 *t0*, *t1*, *t2*
  207. - | *t0* = *t1* & *t2*
  208. * - or_i32/i64 *t0*, *t1*, *t2*
  209. - | *t0* = *t1* | *t2*
  210. * - xor_i32/i64 *t0*, *t1*, *t2*
  211. - | *t0* = *t1* ^ *t2*
  212. * - not_i32/i64 *t0*, *t1*
  213. - | *t0* = ~\ *t1*
  214. * - andc_i32/i64 *t0*, *t1*, *t2*
  215. - | *t0* = *t1* & ~\ *t2*
  216. * - eqv_i32/i64 *t0*, *t1*, *t2*
  217. - | *t0* = ~(*t1* ^ *t2*), or equivalently, *t0* = *t1* ^ ~\ *t2*
  218. * - nand_i32/i64 *t0*, *t1*, *t2*
  219. - | *t0* = ~(*t1* & *t2*)
  220. * - nor_i32/i64 *t0*, *t1*, *t2*
  221. - | *t0* = ~(*t1* | *t2*)
  222. * - orc_i32/i64 *t0*, *t1*, *t2*
  223. - | *t0* = *t1* | ~\ *t2*
  224. * - clz_i32/i64 *t0*, *t1*, *t2*
  225. - | *t0* = *t1* ? clz(*t1*) : *t2*
  226. * - ctz_i32/i64 *t0*, *t1*, *t2*
  227. - | *t0* = *t1* ? ctz(*t1*) : *t2*
  228. * - ctpop_i32/i64 *t0*, *t1*
  229. - | *t0* = number of bits set in *t1*
  230. |
  231. | With *ctpop* short for "count population", matching
  232. | the function name used in ``include/qemu/host-utils.h``.
  233. Shifts/Rotates
  234. --------------
  235. .. list-table::
  236. * - shl_i32/i64 *t0*, *t1*, *t2*
  237. - | *t0* = *t1* << *t2*
  238. | Unspecified behavior if *t2* < 0 or *t2* >= 32 (resp 64)
  239. * - shr_i32/i64 *t0*, *t1*, *t2*
  240. - | *t0* = *t1* >> *t2* (unsigned)
  241. | Unspecified behavior if *t2* < 0 or *t2* >= 32 (resp 64)
  242. * - sar_i32/i64 *t0*, *t1*, *t2*
  243. - | *t0* = *t1* >> *t2* (signed)
  244. | Unspecified behavior if *t2* < 0 or *t2* >= 32 (resp 64)
  245. * - rotl_i32/i64 *t0*, *t1*, *t2*
  246. - | Rotation of *t2* bits to the left
  247. | Unspecified behavior if *t2* < 0 or *t2* >= 32 (resp 64)
  248. * - rotr_i32/i64 *t0*, *t1*, *t2*
  249. - | Rotation of *t2* bits to the right.
  250. | Unspecified behavior if *t2* < 0 or *t2* >= 32 (resp 64)
  251. Misc
  252. ----
  253. .. list-table::
  254. * - mov_i32/i64 *t0*, *t1*
  255. - | *t0* = *t1*
  256. | Move *t1* to *t0* (both operands must have the same type).
  257. * - ext8s_i32/i64 *t0*, *t1*
  258. ext8u_i32/i64 *t0*, *t1*
  259. ext16s_i32/i64 *t0*, *t1*
  260. ext16u_i32/i64 *t0*, *t1*
  261. ext32s_i64 *t0*, *t1*
  262. ext32u_i64 *t0*, *t1*
  263. - | 8, 16 or 32 bit sign/zero extension (both operands must have the same type)
  264. * - bswap16_i32/i64 *t0*, *t1*, *flags*
  265. - | 16 bit byte swap on the low bits of a 32/64 bit input.
  266. |
  267. | If *flags* & ``TCG_BSWAP_IZ``, then *t1* is known to be zero-extended from bit 15.
  268. | If *flags* & ``TCG_BSWAP_OZ``, then *t0* will be zero-extended from bit 15.
  269. | If *flags* & ``TCG_BSWAP_OS``, then *t0* will be sign-extended from bit 15.
  270. |
  271. | If neither ``TCG_BSWAP_OZ`` nor ``TCG_BSWAP_OS`` are set, then the bits of *t0* above bit 15 may contain any value.
  272. * - bswap32_i64 *t0*, *t1*, *flags*
  273. - | 32 bit byte swap on a 64-bit value. The flags are the same as for bswap16,
  274. except they apply from bit 31 instead of bit 15.
  275. * - bswap32_i32 *t0*, *t1*, *flags*
  276. bswap64_i64 *t0*, *t1*, *flags*
  277. - | 32/64 bit byte swap. The flags are ignored, but still present
  278. for consistency with the other bswap opcodes.
  279. * - discard_i32/i64 *t0*
  280. - | Indicate that the value of *t0* won't be used later. It is useful to
  281. force dead code elimination.
  282. * - deposit_i32/i64 *dest*, *t1*, *t2*, *pos*, *len*
  283. - | Deposit *t2* as a bitfield into *t1*, placing the result in *dest*.
  284. |
  285. | The bitfield is described by *pos*/*len*, which are immediate values:
  286. |
  287. | *len* - the length of the bitfield
  288. | *pos* - the position of the first bit, counting from the LSB
  289. |
  290. | For example, "deposit_i32 dest, t1, t2, 8, 4" indicates a 4-bit field
  291. at bit 8. This operation would be equivalent to
  292. |
  293. | *dest* = (*t1* & ~0x0f00) | ((*t2* << 8) & 0x0f00)
  294. * - extract_i32/i64 *dest*, *t1*, *pos*, *len*
  295. sextract_i32/i64 *dest*, *t1*, *pos*, *len*
  296. - | Extract a bitfield from *t1*, placing the result in *dest*.
  297. |
  298. | The bitfield is described by *pos*/*len*, which are immediate values,
  299. as above for deposit. For extract_*, the result will be extended
  300. to the left with zeros; for sextract_*, the result will be extended
  301. to the left with copies of the bitfield sign bit at *pos* + *len* - 1.
  302. |
  303. | For example, "sextract_i32 dest, t1, 8, 4" indicates a 4-bit field
  304. at bit 8. This operation would be equivalent to
  305. |
  306. | *dest* = (*t1* << 20) >> 28
  307. |
  308. | (using an arithmetic right shift).
  309. * - extract2_i32/i64 *dest*, *t1*, *t2*, *pos*
  310. - | For N = {32,64}, extract an N-bit quantity from the concatenation
  311. of *t2*:*t1*, beginning at *pos*. The tcg_gen_extract2_{i32,i64} expander
  312. accepts 0 <= *pos* <= N as inputs. The backend code generator will
  313. not see either 0 or N as inputs for these opcodes.
  314. * - extrl_i64_i32 *t0*, *t1*
  315. - | For 64-bit hosts only, extract the low 32-bits of input *t1* and place it
  316. into 32-bit output *t0*. Depending on the host, this may be a simple move,
  317. or may require additional canonicalization.
  318. * - extrh_i64_i32 *t0*, *t1*
  319. - | For 64-bit hosts only, extract the high 32-bits of input *t1* and place it
  320. into 32-bit output *t0*. Depending on the host, this may be a simple shift,
  321. or may require additional canonicalization.
  322. Conditional moves
  323. -----------------
  324. .. list-table::
  325. * - setcond_i32/i64 *dest*, *t1*, *t2*, *cond*
  326. - | *dest* = (*t1* *cond* *t2*)
  327. |
  328. | Set *dest* to 1 if (*t1* *cond* *t2*) is true, otherwise set to 0.
  329. * - negsetcond_i32/i64 *dest*, *t1*, *t2*, *cond*
  330. - | *dest* = -(*t1* *cond* *t2*)
  331. |
  332. | Set *dest* to -1 if (*t1* *cond* *t2*) is true, otherwise set to 0.
  333. * - movcond_i32/i64 *dest*, *c1*, *c2*, *v1*, *v2*, *cond*
  334. - | *dest* = (*c1* *cond* *c2* ? *v1* : *v2*)
  335. |
  336. | Set *dest* to *v1* if (*c1* *cond* *c2*) is true, otherwise set to *v2*.
  337. Type conversions
  338. ----------------
  339. .. list-table::
  340. * - ext_i32_i64 *t0*, *t1*
  341. - | Convert *t1* (32 bit) to *t0* (64 bit) and does sign extension
  342. * - extu_i32_i64 *t0*, *t1*
  343. - | Convert *t1* (32 bit) to *t0* (64 bit) and does zero extension
  344. * - trunc_i64_i32 *t0*, *t1*
  345. - | Truncate *t1* (64 bit) to *t0* (32 bit)
  346. * - concat_i32_i64 *t0*, *t1*, *t2*
  347. - | Construct *t0* (64-bit) taking the low half from *t1* (32 bit) and the high half
  348. from *t2* (32 bit).
  349. * - concat32_i64 *t0*, *t1*, *t2*
  350. - | Construct *t0* (64-bit) taking the low half from *t1* (64 bit) and the high half
  351. from *t2* (64 bit).
  352. Load/Store
  353. ----------
  354. .. list-table::
  355. * - ld_i32/i64 *t0*, *t1*, *offset*
  356. ld8s_i32/i64 *t0*, *t1*, *offset*
  357. ld8u_i32/i64 *t0*, *t1*, *offset*
  358. ld16s_i32/i64 *t0*, *t1*, *offset*
  359. ld16u_i32/i64 *t0*, *t1*, *offset*
  360. ld32s_i64 t0, *t1*, *offset*
  361. ld32u_i64 t0, *t1*, *offset*
  362. - | *t0* = read(*t1* + *offset*)
  363. |
  364. | Load 8, 16, 32 or 64 bits with or without sign extension from host memory.
  365. *offset* must be a constant.
  366. * - st_i32/i64 *t0*, *t1*, *offset*
  367. st8_i32/i64 *t0*, *t1*, *offset*
  368. st16_i32/i64 *t0*, *t1*, *offset*
  369. st32_i64 *t0*, *t1*, *offset*
  370. - | write(*t0*, *t1* + *offset*)
  371. |
  372. | Write 8, 16, 32 or 64 bits to host memory.
  373. All this opcodes assume that the pointed host memory doesn't correspond
  374. to a global. In the latter case the behaviour is unpredictable.
  375. Multiword arithmetic support
  376. ----------------------------
  377. .. list-table::
  378. * - add2_i32/i64 *t0_low*, *t0_high*, *t1_low*, *t1_high*, *t2_low*, *t2_high*
  379. sub2_i32/i64 *t0_low*, *t0_high*, *t1_low*, *t1_high*, *t2_low*, *t2_high*
  380. - | Similar to add/sub, except that the double-word inputs *t1* and *t2* are
  381. formed from two single-word arguments, and the double-word output *t0*
  382. is returned in two single-word outputs.
  383. * - mulu2_i32/i64 *t0_low*, *t0_high*, *t1*, *t2*
  384. - | Similar to mul, except two unsigned inputs *t1* and *t2* yielding the full
  385. double-word product *t0*. The latter is returned in two single-word outputs.
  386. * - muls2_i32/i64 *t0_low*, *t0_high*, *t1*, *t2*
  387. - | Similar to mulu2, except the two inputs *t1* and *t2* are signed.
  388. * - mulsh_i32/i64 *t0*, *t1*, *t2*
  389. muluh_i32/i64 *t0*, *t1*, *t2*
  390. - | Provide the high part of a signed or unsigned multiply, respectively.
  391. |
  392. | If mulu2/muls2 are not provided by the backend, the tcg-op generator
  393. can obtain the same results by emitting a pair of opcodes, mul + muluh/mulsh.
  394. Memory Barrier support
  395. ----------------------
  396. .. list-table::
  397. * - mb *<$arg>*
  398. - | Generate a target memory barrier instruction to ensure memory ordering
  399. as being enforced by a corresponding guest memory barrier instruction.
  400. |
  401. | The ordering enforced by the backend may be stricter than the ordering
  402. required by the guest. It cannot be weaker. This opcode takes a constant
  403. argument which is required to generate the appropriate barrier
  404. instruction. The backend should take care to emit the target barrier
  405. instruction only when necessary i.e., for SMP guests and when MTTCG is
  406. enabled.
  407. |
  408. | The guest translators should generate this opcode for all guest instructions
  409. which have ordering side effects.
  410. |
  411. | Please see :ref:`atomics-ref` for more information on memory barriers.
  412. 64-bit guest on 32-bit host support
  413. -----------------------------------
  414. The following opcodes are internal to TCG. Thus they are to be implemented by
  415. 32-bit host code generators, but are not to be emitted by guest translators.
  416. They are emitted as needed by inline functions within ``tcg-op.h``.
  417. .. list-table::
  418. * - brcond2_i32 *t0_low*, *t0_high*, *t1_low*, *t1_high*, *cond*, *label*
  419. - | Similar to brcond, except that the 64-bit values *t0* and *t1*
  420. are formed from two 32-bit arguments.
  421. * - setcond2_i32 *dest*, *t1_low*, *t1_high*, *t2_low*, *t2_high*, *cond*
  422. - | Similar to setcond, except that the 64-bit values *t1* and *t2* are
  423. formed from two 32-bit arguments. The result is a 32-bit value.
  424. QEMU specific operations
  425. ------------------------
  426. .. list-table::
  427. * - exit_tb *t0*
  428. - | Exit the current TB and return the value *t0* (word type).
  429. * - goto_tb *index*
  430. - | Exit the current TB and jump to the TB index *index* (constant) if the
  431. current TB was linked to this TB. Otherwise execute the next
  432. instructions. Only indices 0 and 1 are valid and tcg_gen_goto_tb may be issued
  433. at most once with each slot index per TB.
  434. * - lookup_and_goto_ptr *tb_addr*
  435. - | Look up a TB address *tb_addr* and jump to it if valid. If not valid,
  436. jump to the TCG epilogue to go back to the exec loop.
  437. |
  438. | This operation is optional. If the TCG backend does not implement the
  439. goto_ptr opcode, emitting this op is equivalent to emitting exit_tb(0).
  440. * - qemu_ld_i32/i64/i128 *t0*, *t1*, *flags*, *memidx*
  441. qemu_st_i32/i64/i128 *t0*, *t1*, *flags*, *memidx*
  442. qemu_st8_i32 *t0*, *t1*, *flags*, *memidx*
  443. - | Load data at the guest address *t1* into *t0*, or store data in *t0* at guest
  444. address *t1*. The _i32/_i64/_i128 size applies to the size of the input/output
  445. register *t0* only. The address *t1* is always sized according to the guest,
  446. and the width of the memory operation is controlled by *flags*.
  447. |
  448. | Both *t0* and *t1* may be split into little-endian ordered pairs of registers
  449. if dealing with 64-bit quantities on a 32-bit host, or 128-bit quantities on
  450. a 64-bit host.
  451. |
  452. | The *memidx* selects the qemu tlb index to use (e.g. user or kernel access).
  453. The flags are the MemOp bits, selecting the sign, width, and endianness
  454. of the memory access.
  455. |
  456. | For a 32-bit host, qemu_ld/st_i64 is guaranteed to only be used with a
  457. 64-bit memory access specified in *flags*.
  458. |
  459. | For qemu_ld/st_i128, these are only supported for a 64-bit host.
  460. |
  461. | For i386, qemu_st8_i32 is exactly like qemu_st_i32, except the size of
  462. the memory operation is known to be 8-bit. This allows the backend to
  463. provide a different set of register constraints.
  464. Host vector operations
  465. ----------------------
  466. All of the vector ops have two parameters, ``TCGOP_TYPE`` & ``TCGOP_VECE``.
  467. The former specifies the length of the vector as a TCGType; the latter
  468. specifies the length of the element (if applicable) in log2 8-bit units.
  469. .. list-table::
  470. * - mov_vec *v0*, *v1*
  471. ld_vec *v0*, *t1*
  472. st_vec *v0*, *t1*
  473. - | Move, load and store.
  474. * - dup_vec *v0*, *r1*
  475. - | Duplicate the low N bits of *r1* into TYPE/VECE copies across *v0*.
  476. * - dupi_vec *v0*, *c*
  477. - | Similarly, for a constant.
  478. | Smaller values will be replicated to host register size by the expanders.
  479. * - dup2_vec *v0*, *r1*, *r2*
  480. - | Duplicate *r2*:*r1* into TYPE/64 copies across *v0*. This opcode is
  481. only present for 32-bit hosts.
  482. * - add_vec *v0*, *v1*, *v2*
  483. - | *v0* = *v1* + *v2*, in elements across the vector.
  484. * - sub_vec *v0*, *v1*, *v2*
  485. - | Similarly, *v0* = *v1* - *v2*.
  486. * - mul_vec *v0*, *v1*, *v2*
  487. - | Similarly, *v0* = *v1* * *v2*.
  488. * - neg_vec *v0*, *v1*
  489. - | Similarly, *v0* = -*v1*.
  490. * - abs_vec *v0*, *v1*
  491. - | Similarly, *v0* = *v1* < 0 ? -*v1* : *v1*, in elements across the vector.
  492. * - smin_vec *v0*, *v1*, *v2*
  493. umin_vec *v0*, *v1*, *v2*
  494. - | Similarly, *v0* = MIN(*v1*, *v2*), for signed and unsigned element types.
  495. * - smax_vec *v0*, *v1*, *v2*
  496. umax_vec *v0*, *v1*, *v2*
  497. - | Similarly, *v0* = MAX(*v1*, *v2*), for signed and unsigned element types.
  498. * - ssadd_vec *v0*, *v1*, *v2*
  499. sssub_vec *v0*, *v1*, *v2*
  500. usadd_vec *v0*, *v1*, *v2*
  501. ussub_vec *v0*, *v1*, *v2*
  502. - | Signed and unsigned saturating addition and subtraction.
  503. |
  504. | If the true result is not representable within the element type, the
  505. element is set to the minimum or maximum value for the type.
  506. * - and_vec *v0*, *v1*, *v2*
  507. or_vec *v0*, *v1*, *v2*
  508. xor_vec *v0*, *v1*, *v2*
  509. andc_vec *v0*, *v1*, *v2*
  510. orc_vec *v0*, *v1*, *v2*
  511. not_vec *v0*, *v1*
  512. - | Similarly, logical operations with and without complement.
  513. |
  514. | Note that VECE is unused.
  515. * - shli_vec *v0*, *v1*, *i2*
  516. shls_vec *v0*, *v1*, *s2*
  517. - | Shift all elements from v1 by a scalar *i2*/*s2*. I.e.
  518. .. code-block:: c
  519. for (i = 0; i < TYPE/VECE; ++i) {
  520. v0[i] = v1[i] << s2;
  521. }
  522. * - shri_vec *v0*, *v1*, *i2*
  523. sari_vec *v0*, *v1*, *i2*
  524. rotli_vec *v0*, *v1*, *i2*
  525. shrs_vec *v0*, *v1*, *s2*
  526. sars_vec *v0*, *v1*, *s2*
  527. - | Similarly for logical and arithmetic right shift, and left rotate.
  528. * - shlv_vec *v0*, *v1*, *v2*
  529. - | Shift elements from *v1* by elements from *v2*. I.e.
  530. .. code-block:: c
  531. for (i = 0; i < TYPE/VECE; ++i) {
  532. v0[i] = v1[i] << v2[i];
  533. }
  534. * - shrv_vec *v0*, *v1*, *v2*
  535. sarv_vec *v0*, *v1*, *v2*
  536. rotlv_vec *v0*, *v1*, *v2*
  537. rotrv_vec *v0*, *v1*, *v2*
  538. - | Similarly for logical and arithmetic right shift, and rotates.
  539. * - cmp_vec *v0*, *v1*, *v2*, *cond*
  540. - | Compare vectors by element, storing -1 for true and 0 for false.
  541. * - bitsel_vec *v0*, *v1*, *v2*, *v3*
  542. - | Bitwise select, *v0* = (*v2* & *v1*) | (*v3* & ~\ *v1*), across the entire vector.
  543. * - cmpsel_vec *v0*, *c1*, *c2*, *v3*, *v4*, *cond*
  544. - | Select elements based on comparison results:
  545. .. code-block:: c
  546. for (i = 0; i < n; ++i) {
  547. v0[i] = (c1[i] cond c2[i]) ? v3[i] : v4[i].
  548. }
  549. **Note 1**: Some shortcuts are defined when the last operand is known to be
  550. a constant (e.g. addi for add, movi for mov).
  551. **Note 2**: When using TCG, the opcodes must never be generated directly
  552. as some of them may not be available as "real" opcodes. Always use the
  553. function tcg_gen_xxx(args).
  554. Backend
  555. =======
  556. ``tcg-target.h`` contains the target specific definitions. ``tcg-target.c.inc``
  557. contains the target specific code; it is #included by ``tcg/tcg.c``, rather
  558. than being a standalone C file.
  559. Assumptions
  560. -----------
  561. The target word size (``TCG_TARGET_REG_BITS``) is expected to be 32 bit or
  562. 64 bit. It is expected that the pointer has the same size as the word.
  563. On a 32 bit target, all 64 bit operations are converted to 32 bits. A
  564. few specific operations must be implemented to allow it (see add2_i32,
  565. sub2_i32, brcond2_i32).
  566. On a 64 bit target, the values are transferred between 32 and 64-bit
  567. registers using the following ops:
  568. - extrl_i64_i32
  569. - extrh_i64_i32
  570. - ext_i32_i64
  571. - extu_i32_i64
  572. They ensure that the values are correctly truncated or extended when
  573. moved from a 32-bit to a 64-bit register or vice-versa. Note that the
  574. extrl_i64_i32 and extrh_i64_i32 are optional ops. It is not necessary
  575. to implement them if all the following conditions are met:
  576. - 64-bit registers can hold 32-bit values
  577. - 32-bit values in a 64-bit register do not need to stay zero or
  578. sign extended
  579. - all 32-bit TCG ops ignore the high part of 64-bit registers
  580. Floating point operations are not supported in this version. A
  581. previous incarnation of the code generator had full support of them,
  582. but it is better to concentrate on integer operations first.
  583. Constraints
  584. ----------------
  585. GCC like constraints are used to define the constraints of every
  586. instruction. Memory constraints are not supported in this
  587. version. Aliases are specified in the input operands as for GCC.
  588. The same register may be used for both an input and an output, even when
  589. they are not explicitly aliased. If an op expands to multiple target
  590. instructions then care must be taken to avoid clobbering input values.
  591. GCC style "early clobber" outputs are supported, with '``&``'.
  592. A target can define specific register or constant constraints. If an
  593. operation uses a constant input constraint which does not allow all
  594. constants, it must also accept registers in order to have a fallback.
  595. The constraint '``i``' is defined generically to accept any constant.
  596. The constraint '``r``' is not defined generically, but is consistently
  597. used by each backend to indicate all registers. If ``TCG_REG_ZERO``
  598. is defined by the backend, the constraint '``z``' is defined generically
  599. to map constant 0 to the hardware zero register.
  600. The movi_i32 and movi_i64 operations must accept any constants.
  601. The mov_i32 and mov_i64 operations must accept any registers of the
  602. same type.
  603. The ld/st/sti instructions must accept signed 32 bit constant offsets.
  604. This can be implemented by reserving a specific register in which to
  605. compute the address if the offset is too big.
  606. The ld/st instructions must accept any destination (ld) or source (st)
  607. register.
  608. The sti instruction may fail if it cannot store the given constant.
  609. Function call assumptions
  610. -------------------------
  611. - The only supported types for parameters and return value are: 32 and
  612. 64 bit integers and pointer.
  613. - The stack grows downwards.
  614. - The first N parameters are passed in registers.
  615. - The next parameters are passed on the stack by storing them as words.
  616. - Some registers are clobbered during the call.
  617. - The function can return 0 or 1 value in registers. On a 32 bit
  618. target, functions must be able to return 2 values in registers for
  619. 64 bit return type.
  620. Recommended coding rules for best performance
  621. =============================================
  622. - Use globals to represent the parts of the QEMU CPU state which are
  623. often modified, e.g. the integer registers and the condition
  624. codes. TCG will be able to use host registers to store them.
  625. - Don't hesitate to use helpers for complicated or seldom used guest
  626. instructions. There is little performance advantage in using TCG to
  627. implement guest instructions taking more than about twenty TCG
  628. instructions. Note that this rule of thumb is more applicable to
  629. helpers doing complex logic or arithmetic, where the C compiler has
  630. scope to do a good job of optimisation; it is less relevant where
  631. the instruction is mostly doing loads and stores, and in those cases
  632. inline TCG may still be faster for longer sequences.
  633. - Use the 'discard' instruction if you know that TCG won't be able to
  634. prove that a given global is "dead" at a given program point. The
  635. x86 guest uses it to improve the condition codes optimisation.