Name |
Date |
Size |
#Lines |
LOC |
||
---|---|---|---|---|---|---|
.. | - | - | ||||
i386/ | 03-May-2024 | - | 2,434 | 1,959 | ||
LICENSE | D | 03-May-2024 | 146 | 4 | 3 | |
README | D | 03-May-2024 | 15.9 KiB | 539 | 350 | |
TODO | D | 03-May-2024 | 394 | 15 | 8 | |
optimize.c | D | 03-May-2024 | 35.5 KiB | 1,166 | 975 | |
tcg-be-ldst.h | D | 03-May-2024 | 3 KiB | 91 | 46 | |
tcg-be-null.h | D | 03-May-2024 | 1.4 KiB | 44 | 9 | |
tcg-op.h | D | 03-May-2024 | 92.3 KiB | 2,891 | 2,486 | |
tcg-opc.h | D | 03-May-2024 | 10.1 KiB | 287 | 223 | |
tcg-runtime.h | D | 03-May-2024 | 825 | 21 | 16 | |
tcg.c | D | 03-May-2024 | 89.5 KiB | 2,934 | 2,408 | |
tcg.h | D | 03-May-2024 | 27.8 KiB | 855 | 599 |
README
1Tiny Code Generator - Fabrice Bellard. 2 31) Introduction 4 5TCG (Tiny Code Generator) began as a generic backend for a C 6compiler. It was simplified to be used in QEMU. It also has its roots 7in the QOP code generator written by Paul Brook. 8 92) Definitions 10 11The TCG "target" is the architecture for which we generate the 12code. It is of course not the same as the "target" of QEMU which is 13the emulated architecture. As TCG started as a generic C backend used 14for cross compiling, it is assumed that the TCG target is different 15from the host, although it is never the case for QEMU. 16 17In this document, we use "guest" to specify what architecture we are 18emulating; "target" always means the TCG target, the machine on which 19we are running QEMU. 20 21A TCG "function" corresponds to a QEMU Translated Block (TB). 22 23A TCG "temporary" is a variable only live in a basic 24block. Temporaries are allocated explicitly in each function. 25 26A TCG "local temporary" is a variable only live in a function. Local 27temporaries are allocated explicitly in each function. 28 29A TCG "global" is a variable which is live in all the functions 30(equivalent of a C global variable). They are defined before the 31functions defined. A TCG global can be a memory location (e.g. a QEMU 32CPU register), a fixed host register (e.g. the QEMU CPU state pointer) 33or a memory location which is stored in a register outside QEMU TBs 34(not implemented yet). 35 36A TCG "basic block" corresponds to a list of instructions terminated 37by a branch instruction. 38 393) Intermediate representation 40 413.1) Introduction 42 43TCG instructions operate on variables which are temporaries, local 44temporaries or globals. TCG instructions and variables are strongly 45typed. Two types are supported: 32 bit integers and 64 bit 46integers. Pointers are defined as an alias to 32 bit or 64 bit 47integers depending on the TCG target word size. 48 49Each instruction has a fixed number of output variable operands, input 50variable operands and always constant operands. 51 52The notable exception is the call instruction which has a variable 53number of outputs and inputs. 54 55In the textual form, output operands usually come first, followed by 56input operands, followed by constant operands. The output type is 57included in the instruction name. Constants are prefixed with a '$'. 58 59add_i32 t0, t1, t2 (t0 <- t1 + t2) 60 613.2) Assumptions 62 63* Basic blocks 64 65- Basic blocks end after branches (e.g. brcond_i32 instruction), 66 goto_tb and exit_tb instructions. 67- Basic blocks start after the end of a previous basic block, or at a 68 set_label instruction. 69 70After the end of a basic block, the content of temporaries is 71destroyed, but local temporaries and globals are preserved. 72 73* Floating point types are not supported yet 74 75* Pointers: depending on the TCG target, pointer size is 32 bit or 64 76 bit. The type TCG_TYPE_PTR is an alias to TCG_TYPE_I32 or 77 TCG_TYPE_I64. 78 79* Helpers: 80 81Using the tcg_gen_helper_x_y it is possible to call any function 82taking i32, i64 or pointer types. By default, before calling a helper, 83all globals are stored at their canonical location and it is assumed 84that the function can modify them. By default, the helper is allowed to 85modify the CPU state or raise an exception. 86 87This can be overridden using the following function modifiers: 88- TCG_CALL_NO_READ_GLOBALS means that the helper does not read globals, 89 either directly or via an exception. They will not be saved to their 90 canonical locations before calling the helper. 91- TCG_CALL_NO_WRITE_GLOBALS means that the helper does not modify any globals. 92 They will only be saved to their canonical location before calling helpers, 93 but they won't be reloaded afterwise. 94- TCG_CALL_NO_SIDE_EFFECTS means that the call to the function is removed if 95 the return value is not used. 96 97Note that TCG_CALL_NO_READ_GLOBALS implies TCG_CALL_NO_WRITE_GLOBALS. 98 99On some TCG targets (e.g. x86), several calling conventions are 100supported. 101 102* Branches: 103 104Use the instruction 'br' to jump to a label. 105 1063.3) Code Optimizations 107 108When generating instructions, you can count on at least the following 109optimizations: 110 111- Single instructions are simplified, e.g. 112 113 and_i32 t0, t0, $0xffffffff 114 115 is suppressed. 116 117- A liveness analysis is done at the basic block level. The 118 information is used to suppress moves from a dead variable to 119 another one. It is also used to remove instructions which compute 120 dead results. The later is especially useful for condition code 121 optimization in QEMU. 122 123 In the following example: 124 125 add_i32 t0, t1, t2 126 add_i32 t0, t0, $1 127 mov_i32 t0, $1 128 129 only the last instruction is kept. 130 1313.4) Instruction Reference 132 133********* Function call 134 135* call <ret> <params> ptr 136 137call function 'ptr' (pointer type) 138 139<ret> optional 32 bit or 64 bit return value 140<params> optional 32 bit or 64 bit parameters 141 142********* Jumps/Labels 143 144* set_label $label 145 146Define label 'label' at the current program point. 147 148* br $label 149 150Jump to label. 151 152* brcond_i32/i64 t0, t1, cond, label 153 154Conditional jump if t0 cond t1 is true. cond can be: 155 TCG_COND_EQ 156 TCG_COND_NE 157 TCG_COND_LT /* signed */ 158 TCG_COND_GE /* signed */ 159 TCG_COND_LE /* signed */ 160 TCG_COND_GT /* signed */ 161 TCG_COND_LTU /* unsigned */ 162 TCG_COND_GEU /* unsigned */ 163 TCG_COND_LEU /* unsigned */ 164 TCG_COND_GTU /* unsigned */ 165 166********* Arithmetic 167 168* add_i32/i64 t0, t1, t2 169 170t0=t1+t2 171 172* sub_i32/i64 t0, t1, t2 173 174t0=t1-t2 175 176* neg_i32/i64 t0, t1 177 178t0=-t1 (two's complement) 179 180* mul_i32/i64 t0, t1, t2 181 182t0=t1*t2 183 184* div_i32/i64 t0, t1, t2 185 186t0=t1/t2 (signed). Undefined behavior if division by zero or overflow. 187 188* divu_i32/i64 t0, t1, t2 189 190t0=t1/t2 (unsigned). Undefined behavior if division by zero. 191 192* rem_i32/i64 t0, t1, t2 193 194t0=t1%t2 (signed). Undefined behavior if division by zero or overflow. 195 196* remu_i32/i64 t0, t1, t2 197 198t0=t1%t2 (unsigned). Undefined behavior if division by zero. 199 200********* Logical 201 202* and_i32/i64 t0, t1, t2 203 204t0=t1&t2 205 206* or_i32/i64 t0, t1, t2 207 208t0=t1|t2 209 210* xor_i32/i64 t0, t1, t2 211 212t0=t1^t2 213 214* not_i32/i64 t0, t1 215 216t0=~t1 217 218* andc_i32/i64 t0, t1, t2 219 220t0=t1&~t2 221 222* eqv_i32/i64 t0, t1, t2 223 224t0=~(t1^t2), or equivalently, t0=t1^~t2 225 226* nand_i32/i64 t0, t1, t2 227 228t0=~(t1&t2) 229 230* nor_i32/i64 t0, t1, t2 231 232t0=~(t1|t2) 233 234* orc_i32/i64 t0, t1, t2 235 236t0=t1|~t2 237 238********* Shifts/Rotates 239 240* shl_i32/i64 t0, t1, t2 241 242t0=t1 << t2. Undefined behavior if t2 < 0 or t2 >= 32 (resp 64) 243 244* shr_i32/i64 t0, t1, t2 245 246t0=t1 >> t2 (unsigned). Undefined behavior if t2 < 0 or t2 >= 32 (resp 64) 247 248* sar_i32/i64 t0, t1, t2 249 250t0=t1 >> t2 (signed). Undefined behavior if t2 < 0 or t2 >= 32 (resp 64) 251 252* rotl_i32/i64 t0, t1, t2 253 254Rotation of t2 bits to the left. Undefined behavior if t2 < 0 or t2 >= 32 (resp 64) 255 256* rotr_i32/i64 t0, t1, t2 257 258Rotation of t2 bits to the right. Undefined behavior if t2 < 0 or t2 >= 32 (resp 64) 259 260********* Misc 261 262* mov_i32/i64 t0, t1 263 264t0 = t1 265 266Move t1 to t0 (both operands must have the same type). 267 268* ext8s_i32/i64 t0, t1 269ext8u_i32/i64 t0, t1 270ext16s_i32/i64 t0, t1 271ext16u_i32/i64 t0, t1 272ext32s_i64 t0, t1 273ext32u_i64 t0, t1 274 2758, 16 or 32 bit sign/zero extension (both operands must have the same type) 276 277* bswap16_i32/i64 t0, t1 278 27916 bit byte swap on a 32/64 bit value. It assumes that the two/six high order 280bytes are set to zero. 281 282* bswap32_i32/i64 t0, t1 283 28432 bit byte swap on a 32/64 bit value. With a 64 bit value, it assumes that 285the four high order bytes are set to zero. 286 287* bswap64_i64 t0, t1 288 28964 bit byte swap 290 291* discard_i32/i64 t0 292 293Indicate that the value of t0 won't be used later. It is useful to 294force dead code elimination. 295 296* deposit_i32/i64 dest, t1, t2, pos, len 297 298Deposit T2 as a bitfield into T1, placing the result in DEST. 299The bitfield is described by POS/LEN, which are immediate values: 300 301 LEN - the length of the bitfield 302 POS - the position of the first bit, counting from the LSB 303 304For example, pos=8, len=4 indicates a 4-bit field at bit 8. 305This operation would be equivalent to 306 307 dest = (t1 & ~0x0f00) | ((t2 << 8) & 0x0f00) 308 309 310********* Conditional moves 311 312* setcond_i32/i64 dest, t1, t2, cond 313 314dest = (t1 cond t2) 315 316Set DEST to 1 if (T1 cond T2) is true, otherwise set to 0. 317 318* movcond_i32/i64 dest, c1, c2, v1, v2, cond 319 320dest = (c1 cond c2 ? v1 : v2) 321 322Set DEST to V1 if (C1 cond C2) is true, otherwise set to V2. 323 324********* Type conversions 325 326* ext_i32_i64 t0, t1 327Convert t1 (32 bit) to t0 (64 bit) and does sign extension 328 329* extu_i32_i64 t0, t1 330Convert t1 (32 bit) to t0 (64 bit) and does zero extension 331 332* trunc_i64_i32 t0, t1 333Truncate t1 (64 bit) to t0 (32 bit) 334 335* concat_i32_i64 t0, t1, t2 336Construct t0 (64-bit) taking the low half from t1 (32 bit) and the high half 337from t2 (32 bit). 338 339* concat32_i64 t0, t1, t2 340Construct t0 (64-bit) taking the low half from t1 (64 bit) and the high half 341from t2 (64 bit). 342 343********* Load/Store 344 345* ld_i32/i64 t0, t1, offset 346ld8s_i32/i64 t0, t1, offset 347ld8u_i32/i64 t0, t1, offset 348ld16s_i32/i64 t0, t1, offset 349ld16u_i32/i64 t0, t1, offset 350ld32s_i64 t0, t1, offset 351ld32u_i64 t0, t1, offset 352 353t0 = read(t1 + offset) 354Load 8, 16, 32 or 64 bits with or without sign extension from host memory. 355offset must be a constant. 356 357* st_i32/i64 t0, t1, offset 358st8_i32/i64 t0, t1, offset 359st16_i32/i64 t0, t1, offset 360st32_i64 t0, t1, offset 361 362write(t0, t1 + offset) 363Write 8, 16, 32 or 64 bits to host memory. 364 365All this opcodes assume that the pointed host memory doesn't correspond 366to a global. In the latter case the behaviour is unpredictable. 367 368********* Multiword arithmetic support 369 370* add2_i32/i64 t0_low, t0_high, t1_low, t1_high, t2_low, t2_high 371* sub2_i32/i64 t0_low, t0_high, t1_low, t1_high, t2_low, t2_high 372 373Similar to add/sub, except that the double-word inputs T1 and T2 are 374formed from two single-word arguments, and the double-word output T0 375is returned in two single-word outputs. 376 377* mulu2_i32/i64 t0_low, t0_high, t1, t2 378 379Similar to mul, except two unsigned inputs T1 and T2 yielding the full 380double-word product T0. The later is returned in two single-word outputs. 381 382* muls2_i32/i64 t0_low, t0_high, t1, t2 383 384Similar to mulu2, except the two inputs T1 and T2 are signed. 385 386********* 64-bit guest on 32-bit host support 387 388The following opcodes are internal to TCG. Thus they are to be implemented by 38932-bit host code generators, but are not to be emitted by guest translators. 390They are emitted as needed by inline functions within "tcg-op.h". 391 392* brcond2_i32 t0_low, t0_high, t1_low, t1_high, cond, label 393 394Similar to brcond, except that the 64-bit values T0 and T1 395are formed from two 32-bit arguments. 396 397* setcond2_i32 dest, t1_low, t1_high, t2_low, t2_high, cond 398 399Similar to setcond, except that the 64-bit values T1 and T2 are 400formed from two 32-bit arguments. The result is a 32-bit value. 401 402********* QEMU specific operations 403 404* exit_tb t0 405 406Exit the current TB and return the value t0 (word type). 407 408* goto_tb index 409 410Exit the current TB and jump to the TB index 'index' (constant) if the 411current TB was linked to this TB. Otherwise execute the next 412instructions. Only indices 0 and 1 are valid and tcg_gen_goto_tb may be issued 413at most once with each slot index per TB. 414 415* qemu_ld_i32/i64 t0, t1, flags, memidx 416* qemu_st_i32/i64 t0, t1, flags, memidx 417 418Load data at the guest address t1 into t0, or store data in t0 at guest 419address t1. The _i32/_i64 size applies to the size of the input/output 420register t0 only. The address t1 is always sized according to the guest, 421and the width of the memory operation is controlled by flags. 422 423Both t0 and t1 may be split into little-endian ordered pairs of registers 424if dealing with 64-bit quantities on a 32-bit host. 425 426The memidx selects the qemu tlb index to use (e.g. user or kernel access). 427The flags are the TCGMemOp bits, selecting the sign, width, and endianness 428of the memory access. 429 430For a 32-bit host, qemu_ld/st_i64 is guaranteed to only be used with a 43164-bit memory access specified in flags. 432 433********* 434 435Note 1: Some shortcuts are defined when the last operand is known to be 436a constant (e.g. addi for add, movi for mov). 437 438Note 2: When using TCG, the opcodes must never be generated directly 439as some of them may not be available as "real" opcodes. Always use the 440function tcg_gen_xxx(args). 441 4424) Backend 443 444tcg-target.h contains the target specific definitions. tcg-target.c 445contains the target specific code. 446 4474.1) Assumptions 448 449The target word size (TCG_TARGET_REG_BITS) is expected to be 32 bit or 45064 bit. It is expected that the pointer has the same size as the word. 451 452On a 32 bit target, all 64 bit operations are converted to 32 bits. A 453few specific operations must be implemented to allow it (see add2_i32, 454sub2_i32, brcond2_i32). 455 456Floating point operations are not supported in this version. A 457previous incarnation of the code generator had full support of them, 458but it is better to concentrate on integer operations first. 459 460On a 64 bit target, no assumption is made in TCG about the storage of 461the 32 bit values in 64 bit registers. 462 4634.2) Constraints 464 465GCC like constraints are used to define the constraints of every 466instruction. Memory constraints are not supported in this 467version. Aliases are specified in the input operands as for GCC. 468 469The same register may be used for both an input and an output, even when 470they are not explicitly aliased. If an op expands to multiple target 471instructions then care must be taken to avoid clobbering input values. 472GCC style "early clobber" outputs are not currently supported. 473 474A target can define specific register or constant constraints. If an 475operation uses a constant input constraint which does not allow all 476constants, it must also accept registers in order to have a fallback. 477 478The movi_i32 and movi_i64 operations must accept any constants. 479 480The mov_i32 and mov_i64 operations must accept any registers of the 481same type. 482 483The ld/st instructions must accept signed 32 bit constant offsets. It 484can be implemented by reserving a specific register to compute the 485address if the offset is too big. 486 487The ld/st instructions must accept any destination (ld) or source (st) 488register. 489 4904.3) Function call assumptions 491 492- The only supported types for parameters and return value are: 32 and 493 64 bit integers and pointer. 494- The stack grows downwards. 495- The first N parameters are passed in registers. 496- The next parameters are passed on the stack by storing them as words. 497- Some registers are clobbered during the call. 498- The function can return 0 or 1 value in registers. On a 32 bit 499 target, functions must be able to return 2 values in registers for 500 64 bit return type. 501 5025) Recommended coding rules for best performance 503 504- Use globals to represent the parts of the QEMU CPU state which are 505 often modified, e.g. the integer registers and the condition 506 codes. TCG will be able to use host registers to store them. 507 508- Avoid globals stored in fixed registers. They must be used only to 509 store the pointer to the CPU state and possibly to store a pointer 510 to a register window. 511 512- Use temporaries. Use local temporaries only when really needed, 513 e.g. when you need to use a value after a jump. Local temporaries 514 introduce a performance hit in the current TCG implementation: their 515 content is saved to memory at end of each basic block. 516 517- Free temporaries and local temporaries when they are no longer used 518 (tcg_temp_free). Since tcg_const_x() also creates a temporary, you 519 should free it after it is used. Freeing temporaries does not yield 520 a better generated code, but it reduces the memory usage of TCG and 521 the speed of the translation. 522 523- Don't hesitate to use helpers for complicated or seldom used guest 524 instructions. There is little performance advantage in using TCG to 525 implement guest instructions taking more than about twenty TCG 526 instructions. Note that this rule of thumb is more applicable to 527 helpers doing complex logic or arithmetic, where the C compiler has 528 scope to do a good job of optimisation; it is less relevant where 529 the instruction is mostly doing loads and stores, and in those cases 530 inline TCG may still be faster for longer sequences. 531 532- The hard limit on the number of TCG instructions you can generate 533 per guest instruction is set by MAX_OP_PER_INSTR in exec-all.h -- 534 you cannot exceed this without risking a buffer overrun. 535 536- Use the 'discard' instruction if you know that TCG won't be able to 537 prove that a given global is "dead" at a given program point. The 538 x86 guest uses it to improve the condition codes optimisation. 539