1c896fe29Sbellard /* 2c896fe29Sbellard * Tiny Code Generator for QEMU 3c896fe29Sbellard * 4c896fe29Sbellard * Copyright (c) 2008 Fabrice Bellard 5c896fe29Sbellard * 6c896fe29Sbellard * Permission is hereby granted, free of charge, to any person obtaining a copy 7c896fe29Sbellard * of this software and associated documentation files (the "Software"), to deal 8c896fe29Sbellard * in the Software without restriction, including without limitation the rights 9c896fe29Sbellard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10c896fe29Sbellard * copies of the Software, and to permit persons to whom the Software is 11c896fe29Sbellard * furnished to do so, subject to the following conditions: 12c896fe29Sbellard * 13c896fe29Sbellard * The above copyright notice and this permission notice shall be included in 14c896fe29Sbellard * all copies or substantial portions of the Software. 15c896fe29Sbellard * 16c896fe29Sbellard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17c896fe29Sbellard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18c896fe29Sbellard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19c896fe29Sbellard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20c896fe29Sbellard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21c896fe29Sbellard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22c896fe29Sbellard * THE SOFTWARE. 23c896fe29Sbellard */ 24c896fe29Sbellard 25c896fe29Sbellard /* define it to suppress various consistency checks (faster) */ 26c896fe29Sbellard #define NDEBUG 27c896fe29Sbellard 28c896fe29Sbellard /* define it to use liveness analysis (better code) */ 29c896fe29Sbellard #define USE_LIVENESS_ANALYSIS 30c896fe29Sbellard 31c896fe29Sbellard #include <assert.h> 32c896fe29Sbellard #include <stdarg.h> 33c896fe29Sbellard #include <stdlib.h> 34c896fe29Sbellard #include <stdio.h> 35c896fe29Sbellard #include <string.h> 36c896fe29Sbellard #include <inttypes.h> 373fe43da7Sbellard #ifdef _WIN32 383fe43da7Sbellard #include <malloc.h> 393fe43da7Sbellard #endif 40c896fe29Sbellard 41c896fe29Sbellard #include "config.h" 42ca10f867Saurel32 #include "qemu-common.h" 43c896fe29Sbellard 44c896fe29Sbellard /* Note: the long term plan is to reduce the dependancies on the QEMU 45c896fe29Sbellard CPU definitions. Currently they are used for qemu_ld/st 46c896fe29Sbellard instructions */ 47c896fe29Sbellard #define NO_CPU_IO_DEFS 48c896fe29Sbellard #include "cpu.h" 49c896fe29Sbellard #include "exec-all.h" 50c896fe29Sbellard 51c896fe29Sbellard #include "tcg-op.h" 52c896fe29Sbellard #include "elf.h" 53c896fe29Sbellard 54c896fe29Sbellard 55c896fe29Sbellard static void patch_reloc(uint8_t *code_ptr, int type, 56f54b3f92Saurel32 tcg_target_long value, tcg_target_long addend); 57c896fe29Sbellard 58c896fe29Sbellard TCGOpDef tcg_op_defs[] = { 59c896fe29Sbellard #define DEF(s, n, copy_size) { #s, 0, 0, n, n, 0, copy_size }, 60c896fe29Sbellard #define DEF2(s, iargs, oargs, cargs, flags) { #s, iargs, oargs, cargs, iargs + oargs + cargs, flags, 0 }, 61c896fe29Sbellard #include "tcg-opc.h" 62c896fe29Sbellard #undef DEF 63c896fe29Sbellard #undef DEF2 64c896fe29Sbellard }; 65c896fe29Sbellard 66c896fe29Sbellard TCGRegSet tcg_target_available_regs[2]; 67c896fe29Sbellard TCGRegSet tcg_target_call_clobber_regs; 68c896fe29Sbellard 69c896fe29Sbellard /* XXX: move that inside the context */ 70c896fe29Sbellard uint16_t *gen_opc_ptr; 71c896fe29Sbellard TCGArg *gen_opparam_ptr; 72c896fe29Sbellard 73c896fe29Sbellard static inline void tcg_out8(TCGContext *s, uint8_t v) 74c896fe29Sbellard { 75c896fe29Sbellard *s->code_ptr++ = v; 76c896fe29Sbellard } 77c896fe29Sbellard 78c896fe29Sbellard static inline void tcg_out16(TCGContext *s, uint16_t v) 79c896fe29Sbellard { 80c896fe29Sbellard *(uint16_t *)s->code_ptr = v; 81c896fe29Sbellard s->code_ptr += 2; 82c896fe29Sbellard } 83c896fe29Sbellard 84c896fe29Sbellard static inline void tcg_out32(TCGContext *s, uint32_t v) 85c896fe29Sbellard { 86c896fe29Sbellard *(uint32_t *)s->code_ptr = v; 87c896fe29Sbellard s->code_ptr += 4; 88c896fe29Sbellard } 89c896fe29Sbellard 90c896fe29Sbellard /* label relocation processing */ 91c896fe29Sbellard 92c896fe29Sbellard void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type, 93c896fe29Sbellard int label_index, long addend) 94c896fe29Sbellard { 95c896fe29Sbellard TCGLabel *l; 96c896fe29Sbellard TCGRelocation *r; 97c896fe29Sbellard 98c896fe29Sbellard l = &s->labels[label_index]; 99c896fe29Sbellard if (l->has_value) { 100623e265cSpbrook /* FIXME: This may break relocations on RISC targets that 101623e265cSpbrook modify instruction fields in place. The caller may not have 102623e265cSpbrook written the initial value. */ 103f54b3f92Saurel32 patch_reloc(code_ptr, type, l->u.value, addend); 104c896fe29Sbellard } else { 105c896fe29Sbellard /* add a new relocation entry */ 106c896fe29Sbellard r = tcg_malloc(sizeof(TCGRelocation)); 107c896fe29Sbellard r->type = type; 108c896fe29Sbellard r->ptr = code_ptr; 109c896fe29Sbellard r->addend = addend; 110c896fe29Sbellard r->next = l->u.first_reloc; 111c896fe29Sbellard l->u.first_reloc = r; 112c896fe29Sbellard } 113c896fe29Sbellard } 114c896fe29Sbellard 115c896fe29Sbellard static void tcg_out_label(TCGContext *s, int label_index, 116c896fe29Sbellard tcg_target_long value) 117c896fe29Sbellard { 118c896fe29Sbellard TCGLabel *l; 119c896fe29Sbellard TCGRelocation *r; 120c896fe29Sbellard 121c896fe29Sbellard l = &s->labels[label_index]; 122c896fe29Sbellard if (l->has_value) 123c896fe29Sbellard tcg_abort(); 124c896fe29Sbellard r = l->u.first_reloc; 125c896fe29Sbellard while (r != NULL) { 126f54b3f92Saurel32 patch_reloc(r->ptr, r->type, value, r->addend); 127c896fe29Sbellard r = r->next; 128c896fe29Sbellard } 129c896fe29Sbellard l->has_value = 1; 130c896fe29Sbellard l->u.value = value; 131c896fe29Sbellard } 132c896fe29Sbellard 133c896fe29Sbellard int gen_new_label(void) 134c896fe29Sbellard { 135c896fe29Sbellard TCGContext *s = &tcg_ctx; 136c896fe29Sbellard int idx; 137c896fe29Sbellard TCGLabel *l; 138c896fe29Sbellard 139c896fe29Sbellard if (s->nb_labels >= TCG_MAX_LABELS) 140c896fe29Sbellard tcg_abort(); 141c896fe29Sbellard idx = s->nb_labels++; 142c896fe29Sbellard l = &s->labels[idx]; 143c896fe29Sbellard l->has_value = 0; 144c896fe29Sbellard l->u.first_reloc = NULL; 145c896fe29Sbellard return idx; 146c896fe29Sbellard } 147c896fe29Sbellard 148c896fe29Sbellard #include "tcg-target.c" 149c896fe29Sbellard 150c896fe29Sbellard /* pool based memory allocation */ 151c896fe29Sbellard void *tcg_malloc_internal(TCGContext *s, int size) 152c896fe29Sbellard { 153c896fe29Sbellard TCGPool *p; 154c896fe29Sbellard int pool_size; 155c896fe29Sbellard 156c896fe29Sbellard if (size > TCG_POOL_CHUNK_SIZE) { 157c896fe29Sbellard /* big malloc: insert a new pool (XXX: could optimize) */ 158c896fe29Sbellard p = qemu_malloc(sizeof(TCGPool) + size); 159c896fe29Sbellard p->size = size; 160c896fe29Sbellard if (s->pool_current) 161c896fe29Sbellard s->pool_current->next = p; 162c896fe29Sbellard else 163c896fe29Sbellard s->pool_first = p; 164c896fe29Sbellard p->next = s->pool_current; 165c896fe29Sbellard } else { 166c896fe29Sbellard p = s->pool_current; 167c896fe29Sbellard if (!p) { 168c896fe29Sbellard p = s->pool_first; 169c896fe29Sbellard if (!p) 170c896fe29Sbellard goto new_pool; 171c896fe29Sbellard } else { 172c896fe29Sbellard if (!p->next) { 173c896fe29Sbellard new_pool: 174c896fe29Sbellard pool_size = TCG_POOL_CHUNK_SIZE; 175c896fe29Sbellard p = qemu_malloc(sizeof(TCGPool) + pool_size); 176c896fe29Sbellard p->size = pool_size; 177c896fe29Sbellard p->next = NULL; 178c896fe29Sbellard if (s->pool_current) 179c896fe29Sbellard s->pool_current->next = p; 180c896fe29Sbellard else 181c896fe29Sbellard s->pool_first = p; 182c896fe29Sbellard } else { 183c896fe29Sbellard p = p->next; 184c896fe29Sbellard } 185c896fe29Sbellard } 186c896fe29Sbellard } 187c896fe29Sbellard s->pool_current = p; 188c896fe29Sbellard s->pool_cur = p->data + size; 189c896fe29Sbellard s->pool_end = p->data + p->size; 190c896fe29Sbellard return p->data; 191c896fe29Sbellard } 192c896fe29Sbellard 193c896fe29Sbellard void tcg_pool_reset(TCGContext *s) 194c896fe29Sbellard { 195c896fe29Sbellard s->pool_cur = s->pool_end = NULL; 196c896fe29Sbellard s->pool_current = NULL; 197c896fe29Sbellard } 198c896fe29Sbellard 199c896fe29Sbellard /* free all the pool */ 200c896fe29Sbellard void tcg_pool_free(TCGContext *s) 201c896fe29Sbellard { 202c896fe29Sbellard TCGPool *p, *p1; 203c896fe29Sbellard 204c896fe29Sbellard for(p = s->pool_first; p != NULL; p = p1) { 205c896fe29Sbellard p1 = p->next; 206c896fe29Sbellard qemu_free(p); 207c896fe29Sbellard } 208c896fe29Sbellard s->pool_first = NULL; 209c896fe29Sbellard s->pool_cur = s->pool_end = NULL; 210c896fe29Sbellard } 211c896fe29Sbellard 212c896fe29Sbellard void tcg_context_init(TCGContext *s) 213c896fe29Sbellard { 214c896fe29Sbellard int op, total_args, n; 215c896fe29Sbellard TCGOpDef *def; 216c896fe29Sbellard TCGArgConstraint *args_ct; 217c896fe29Sbellard int *sorted_args; 218c896fe29Sbellard 219c896fe29Sbellard memset(s, 0, sizeof(*s)); 220c896fe29Sbellard s->temps = s->static_temps; 221c896fe29Sbellard s->nb_globals = 0; 222c896fe29Sbellard 223c896fe29Sbellard /* Count total number of arguments and allocate the corresponding 224c896fe29Sbellard space */ 225c896fe29Sbellard total_args = 0; 226c896fe29Sbellard for(op = 0; op < NB_OPS; op++) { 227c896fe29Sbellard def = &tcg_op_defs[op]; 228c896fe29Sbellard n = def->nb_iargs + def->nb_oargs; 229c896fe29Sbellard total_args += n; 230c896fe29Sbellard } 231c896fe29Sbellard 232c896fe29Sbellard args_ct = qemu_malloc(sizeof(TCGArgConstraint) * total_args); 233c896fe29Sbellard sorted_args = qemu_malloc(sizeof(int) * total_args); 234c896fe29Sbellard 235c896fe29Sbellard for(op = 0; op < NB_OPS; op++) { 236c896fe29Sbellard def = &tcg_op_defs[op]; 237c896fe29Sbellard def->args_ct = args_ct; 238c896fe29Sbellard def->sorted_args = sorted_args; 239c896fe29Sbellard n = def->nb_iargs + def->nb_oargs; 240c896fe29Sbellard sorted_args += n; 241c896fe29Sbellard args_ct += n; 242c896fe29Sbellard } 243c896fe29Sbellard 244c896fe29Sbellard tcg_target_init(s); 245b03cce8eSbellard 246b03cce8eSbellard /* init global prologue and epilogue */ 247b03cce8eSbellard s->code_buf = code_gen_prologue; 248b03cce8eSbellard s->code_ptr = s->code_buf; 249b03cce8eSbellard tcg_target_qemu_prologue(s); 250b03cce8eSbellard flush_icache_range((unsigned long)s->code_buf, 251b03cce8eSbellard (unsigned long)s->code_ptr); 252c896fe29Sbellard } 253c896fe29Sbellard 254c896fe29Sbellard void tcg_set_frame(TCGContext *s, int reg, 255c896fe29Sbellard tcg_target_long start, tcg_target_long size) 256c896fe29Sbellard { 257c896fe29Sbellard s->frame_start = start; 258c896fe29Sbellard s->frame_end = start + size; 259c896fe29Sbellard s->frame_reg = reg; 260c896fe29Sbellard } 261c896fe29Sbellard 262c896fe29Sbellard void tcg_func_start(TCGContext *s) 263c896fe29Sbellard { 264e8996ee0Sbellard int i; 265c896fe29Sbellard tcg_pool_reset(s); 266c896fe29Sbellard s->nb_temps = s->nb_globals; 267641d5fbeSbellard for(i = 0; i < (TCG_TYPE_COUNT * 2); i++) 268e8996ee0Sbellard s->first_free_temp[i] = -1; 269c896fe29Sbellard s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS); 270c896fe29Sbellard s->nb_labels = 0; 271c896fe29Sbellard s->current_frame_offset = s->frame_start; 272c896fe29Sbellard 273c896fe29Sbellard gen_opc_ptr = gen_opc_buf; 274c896fe29Sbellard gen_opparam_ptr = gen_opparam_buf; 275c896fe29Sbellard } 276c896fe29Sbellard 277c896fe29Sbellard static inline void tcg_temp_alloc(TCGContext *s, int n) 278c896fe29Sbellard { 279c896fe29Sbellard if (n > TCG_MAX_TEMPS) 280c896fe29Sbellard tcg_abort(); 281c896fe29Sbellard } 282c896fe29Sbellard 283ac56dd48Spbrook TCGv tcg_global_reg_new(TCGType type, int reg, const char *name) 284c896fe29Sbellard { 285c896fe29Sbellard TCGContext *s = &tcg_ctx; 286c896fe29Sbellard TCGTemp *ts; 287c896fe29Sbellard int idx; 288c896fe29Sbellard 289c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 290c896fe29Sbellard if (type != TCG_TYPE_I32) 291c896fe29Sbellard tcg_abort(); 292c896fe29Sbellard #endif 293c896fe29Sbellard if (tcg_regset_test_reg(s->reserved_regs, reg)) 294c896fe29Sbellard tcg_abort(); 295c896fe29Sbellard idx = s->nb_globals; 296c896fe29Sbellard tcg_temp_alloc(s, s->nb_globals + 1); 297c896fe29Sbellard ts = &s->temps[s->nb_globals]; 298c896fe29Sbellard ts->base_type = type; 299c896fe29Sbellard ts->type = type; 300c896fe29Sbellard ts->fixed_reg = 1; 301c896fe29Sbellard ts->reg = reg; 302c896fe29Sbellard ts->name = name; 303c896fe29Sbellard s->nb_globals++; 304c896fe29Sbellard tcg_regset_set_reg(s->reserved_regs, reg); 305ac56dd48Spbrook return MAKE_TCGV(idx); 306c896fe29Sbellard } 307c896fe29Sbellard 3086a8d7b76Sbellard #if TCG_TARGET_REG_BITS == 32 3096a8d7b76Sbellard /* temporary hack to avoid register shortage for tcg_qemu_st64() */ 3106a8d7b76Sbellard TCGv tcg_global_reg2_new_hack(TCGType type, int reg1, int reg2, 3116a8d7b76Sbellard const char *name) 3126a8d7b76Sbellard { 3136a8d7b76Sbellard TCGContext *s = &tcg_ctx; 3146a8d7b76Sbellard TCGTemp *ts; 3156a8d7b76Sbellard int idx; 3166a8d7b76Sbellard char buf[64]; 3176a8d7b76Sbellard 3186a8d7b76Sbellard if (type != TCG_TYPE_I64) 3196a8d7b76Sbellard tcg_abort(); 3206a8d7b76Sbellard idx = s->nb_globals; 3216a8d7b76Sbellard tcg_temp_alloc(s, s->nb_globals + 2); 3226a8d7b76Sbellard ts = &s->temps[s->nb_globals]; 3236a8d7b76Sbellard ts->base_type = type; 3246a8d7b76Sbellard ts->type = TCG_TYPE_I32; 3256a8d7b76Sbellard ts->fixed_reg = 1; 3266a8d7b76Sbellard ts->reg = reg1; 3276a8d7b76Sbellard pstrcpy(buf, sizeof(buf), name); 3286a8d7b76Sbellard pstrcat(buf, sizeof(buf), "_0"); 3296a8d7b76Sbellard ts->name = strdup(buf); 3306a8d7b76Sbellard 3316a8d7b76Sbellard ts++; 3326a8d7b76Sbellard ts->base_type = type; 3336a8d7b76Sbellard ts->type = TCG_TYPE_I32; 3346a8d7b76Sbellard ts->fixed_reg = 1; 3356a8d7b76Sbellard ts->reg = reg2; 3366a8d7b76Sbellard pstrcpy(buf, sizeof(buf), name); 3376a8d7b76Sbellard pstrcat(buf, sizeof(buf), "_1"); 3386a8d7b76Sbellard ts->name = strdup(buf); 3396a8d7b76Sbellard 3406a8d7b76Sbellard s->nb_globals += 2; 3416a8d7b76Sbellard return MAKE_TCGV(idx); 3426a8d7b76Sbellard } 3436a8d7b76Sbellard #endif 3446a8d7b76Sbellard 345ac56dd48Spbrook TCGv tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset, 346c896fe29Sbellard const char *name) 347c896fe29Sbellard { 348c896fe29Sbellard TCGContext *s = &tcg_ctx; 349c896fe29Sbellard TCGTemp *ts; 350c896fe29Sbellard int idx; 351c896fe29Sbellard 352c896fe29Sbellard idx = s->nb_globals; 353c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 354c896fe29Sbellard if (type == TCG_TYPE_I64) { 355c896fe29Sbellard char buf[64]; 356c896fe29Sbellard tcg_temp_alloc(s, s->nb_globals + 1); 357c896fe29Sbellard ts = &s->temps[s->nb_globals]; 358c896fe29Sbellard ts->base_type = type; 359c896fe29Sbellard ts->type = TCG_TYPE_I32; 360c896fe29Sbellard ts->fixed_reg = 0; 361c896fe29Sbellard ts->mem_allocated = 1; 362c896fe29Sbellard ts->mem_reg = reg; 363c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN 364c896fe29Sbellard ts->mem_offset = offset + 4; 365c896fe29Sbellard #else 366c896fe29Sbellard ts->mem_offset = offset; 367c896fe29Sbellard #endif 368c896fe29Sbellard pstrcpy(buf, sizeof(buf), name); 369c896fe29Sbellard pstrcat(buf, sizeof(buf), "_0"); 370c896fe29Sbellard ts->name = strdup(buf); 371c896fe29Sbellard ts++; 372c896fe29Sbellard 373c896fe29Sbellard ts->base_type = type; 374c896fe29Sbellard ts->type = TCG_TYPE_I32; 375c896fe29Sbellard ts->fixed_reg = 0; 376c896fe29Sbellard ts->mem_allocated = 1; 377c896fe29Sbellard ts->mem_reg = reg; 378c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN 379c896fe29Sbellard ts->mem_offset = offset; 380c896fe29Sbellard #else 381c896fe29Sbellard ts->mem_offset = offset + 4; 382c896fe29Sbellard #endif 383c896fe29Sbellard pstrcpy(buf, sizeof(buf), name); 384c896fe29Sbellard pstrcat(buf, sizeof(buf), "_1"); 385c896fe29Sbellard ts->name = strdup(buf); 386c896fe29Sbellard 387c896fe29Sbellard s->nb_globals += 2; 388c896fe29Sbellard } else 389c896fe29Sbellard #endif 390c896fe29Sbellard { 391c896fe29Sbellard tcg_temp_alloc(s, s->nb_globals + 1); 392c896fe29Sbellard ts = &s->temps[s->nb_globals]; 393c896fe29Sbellard ts->base_type = type; 394c896fe29Sbellard ts->type = type; 395c896fe29Sbellard ts->fixed_reg = 0; 396c896fe29Sbellard ts->mem_allocated = 1; 397c896fe29Sbellard ts->mem_reg = reg; 398c896fe29Sbellard ts->mem_offset = offset; 399c896fe29Sbellard ts->name = name; 400c896fe29Sbellard s->nb_globals++; 401c896fe29Sbellard } 402ac56dd48Spbrook return MAKE_TCGV(idx); 403c896fe29Sbellard } 404c896fe29Sbellard 405641d5fbeSbellard TCGv tcg_temp_new_internal(TCGType type, int temp_local) 406c896fe29Sbellard { 407c896fe29Sbellard TCGContext *s = &tcg_ctx; 408c896fe29Sbellard TCGTemp *ts; 409641d5fbeSbellard int idx, k; 410c896fe29Sbellard 411641d5fbeSbellard k = type; 412641d5fbeSbellard if (temp_local) 413641d5fbeSbellard k += TCG_TYPE_COUNT; 414641d5fbeSbellard idx = s->first_free_temp[k]; 415e8996ee0Sbellard if (idx != -1) { 416e8996ee0Sbellard /* There is already an available temp with the 417e8996ee0Sbellard right type */ 418e8996ee0Sbellard ts = &s->temps[idx]; 419641d5fbeSbellard s->first_free_temp[k] = ts->next_free_temp; 420e8996ee0Sbellard ts->temp_allocated = 1; 421641d5fbeSbellard assert(ts->temp_local == temp_local); 422e8996ee0Sbellard } else { 423c896fe29Sbellard idx = s->nb_temps; 424c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 425c896fe29Sbellard if (type == TCG_TYPE_I64) { 426c896fe29Sbellard tcg_temp_alloc(s, s->nb_temps + 1); 427c896fe29Sbellard ts = &s->temps[s->nb_temps]; 428c896fe29Sbellard ts->base_type = type; 429c896fe29Sbellard ts->type = TCG_TYPE_I32; 430e8996ee0Sbellard ts->temp_allocated = 1; 431641d5fbeSbellard ts->temp_local = temp_local; 432c896fe29Sbellard ts->name = NULL; 433c896fe29Sbellard ts++; 434c896fe29Sbellard ts->base_type = TCG_TYPE_I32; 435c896fe29Sbellard ts->type = TCG_TYPE_I32; 436e8996ee0Sbellard ts->temp_allocated = 1; 437641d5fbeSbellard ts->temp_local = temp_local; 438c896fe29Sbellard ts->name = NULL; 439c896fe29Sbellard s->nb_temps += 2; 440c896fe29Sbellard } else 441c896fe29Sbellard #endif 442c896fe29Sbellard { 443c896fe29Sbellard tcg_temp_alloc(s, s->nb_temps + 1); 444c896fe29Sbellard ts = &s->temps[s->nb_temps]; 445c896fe29Sbellard ts->base_type = type; 446c896fe29Sbellard ts->type = type; 447e8996ee0Sbellard ts->temp_allocated = 1; 448641d5fbeSbellard ts->temp_local = temp_local; 449c896fe29Sbellard ts->name = NULL; 450c896fe29Sbellard s->nb_temps++; 451c896fe29Sbellard } 452e8996ee0Sbellard } 453ac56dd48Spbrook return MAKE_TCGV(idx); 454c896fe29Sbellard } 455c896fe29Sbellard 456e8996ee0Sbellard void tcg_temp_free(TCGv arg) 457c896fe29Sbellard { 458c896fe29Sbellard TCGContext *s = &tcg_ctx; 459c896fe29Sbellard TCGTemp *ts; 460e8996ee0Sbellard int idx = GET_TCGV(arg); 461641d5fbeSbellard int k; 462c896fe29Sbellard 463e8996ee0Sbellard assert(idx >= s->nb_globals && idx < s->nb_temps); 464c896fe29Sbellard ts = &s->temps[idx]; 465e8996ee0Sbellard assert(ts->temp_allocated != 0); 466e8996ee0Sbellard ts->temp_allocated = 0; 467641d5fbeSbellard k = ts->base_type; 468641d5fbeSbellard if (ts->temp_local) 469641d5fbeSbellard k += TCG_TYPE_COUNT; 470641d5fbeSbellard ts->next_free_temp = s->first_free_temp[k]; 471641d5fbeSbellard s->first_free_temp[k] = idx; 472e8996ee0Sbellard } 473e8996ee0Sbellard 474e8996ee0Sbellard 475e8996ee0Sbellard TCGv tcg_const_i32(int32_t val) 476e8996ee0Sbellard { 477e8996ee0Sbellard TCGv t0; 478e8996ee0Sbellard t0 = tcg_temp_new(TCG_TYPE_I32); 479e8996ee0Sbellard tcg_gen_movi_i32(t0, val); 480e8996ee0Sbellard return t0; 481c896fe29Sbellard } 482c896fe29Sbellard 483ac56dd48Spbrook TCGv tcg_const_i64(int64_t val) 484c896fe29Sbellard { 485e8996ee0Sbellard TCGv t0; 486e8996ee0Sbellard t0 = tcg_temp_new(TCG_TYPE_I64); 487e8996ee0Sbellard tcg_gen_movi_i64(t0, val); 488e8996ee0Sbellard return t0; 489c896fe29Sbellard } 490c896fe29Sbellard 491c896fe29Sbellard void tcg_register_helper(void *func, const char *name) 492c896fe29Sbellard { 493c896fe29Sbellard TCGContext *s = &tcg_ctx; 494c896fe29Sbellard int n; 495c896fe29Sbellard if ((s->nb_helpers + 1) > s->allocated_helpers) { 496c896fe29Sbellard n = s->allocated_helpers; 497c896fe29Sbellard if (n == 0) { 498c896fe29Sbellard n = 4; 499c896fe29Sbellard } else { 500c896fe29Sbellard n *= 2; 501c896fe29Sbellard } 502c896fe29Sbellard s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo)); 503c896fe29Sbellard s->allocated_helpers = n; 504c896fe29Sbellard } 5054dc81f28Sbellard s->helpers[s->nb_helpers].func = (tcg_target_ulong)func; 506c896fe29Sbellard s->helpers[s->nb_helpers].name = name; 507c896fe29Sbellard s->nb_helpers++; 508c896fe29Sbellard } 509c896fe29Sbellard 510ac56dd48Spbrook static inline TCGType tcg_get_base_type(TCGContext *s, TCGv arg) 511c896fe29Sbellard { 512ac56dd48Spbrook return s->temps[GET_TCGV(arg)].base_type; 513c896fe29Sbellard } 514c896fe29Sbellard 515ac56dd48Spbrook static void tcg_gen_call_internal(TCGContext *s, TCGv func, 516c896fe29Sbellard unsigned int flags, 517ac56dd48Spbrook unsigned int nb_rets, const TCGv *rets, 518ac56dd48Spbrook unsigned int nb_params, const TCGv *params) 519c896fe29Sbellard { 520c896fe29Sbellard int i; 521c896fe29Sbellard *gen_opc_ptr++ = INDEX_op_call; 522c896fe29Sbellard *gen_opparam_ptr++ = (nb_rets << 16) | (nb_params + 1); 523c896fe29Sbellard for(i = 0; i < nb_rets; i++) { 524ac56dd48Spbrook *gen_opparam_ptr++ = GET_TCGV(rets[i]); 525c896fe29Sbellard } 526c896fe29Sbellard for(i = 0; i < nb_params; i++) { 527ac56dd48Spbrook *gen_opparam_ptr++ = GET_TCGV(params[i]); 528c896fe29Sbellard } 529ac56dd48Spbrook *gen_opparam_ptr++ = GET_TCGV(func); 530c896fe29Sbellard 531c896fe29Sbellard *gen_opparam_ptr++ = flags; 532c896fe29Sbellard /* total parameters, needed to go backward in the instruction stream */ 533c896fe29Sbellard *gen_opparam_ptr++ = 1 + nb_rets + nb_params + 3; 534c896fe29Sbellard } 535c896fe29Sbellard 536c896fe29Sbellard 537c896fe29Sbellard #if TCG_TARGET_REG_BITS < 64 53839cf05d3Sbellard /* Note: we convert the 64 bit args to 32 bit and do some alignment 53939cf05d3Sbellard and endian swap. Maybe it would be better to do the alignment 54039cf05d3Sbellard and endian swap in tcg_reg_alloc_call(). */ 541ac56dd48Spbrook void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags, 542ac56dd48Spbrook unsigned int nb_rets, const TCGv *rets, 543ac56dd48Spbrook unsigned int nb_params, const TCGv *args1) 544c896fe29Sbellard { 545ac56dd48Spbrook TCGv ret, *args2, rets_2[2], arg; 546c896fe29Sbellard int j, i, call_type; 547c896fe29Sbellard 548c896fe29Sbellard if (nb_rets == 1) { 549c896fe29Sbellard ret = rets[0]; 550c896fe29Sbellard if (tcg_get_base_type(s, ret) == TCG_TYPE_I64) { 551c896fe29Sbellard nb_rets = 2; 55239cf05d3Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN 55339cf05d3Sbellard rets_2[0] = TCGV_HIGH(ret); 55439cf05d3Sbellard rets_2[1] = ret; 55539cf05d3Sbellard #else 556c896fe29Sbellard rets_2[0] = ret; 557ac56dd48Spbrook rets_2[1] = TCGV_HIGH(ret); 55839cf05d3Sbellard #endif 559c896fe29Sbellard rets = rets_2; 560c896fe29Sbellard } 561c896fe29Sbellard } 56239cf05d3Sbellard args2 = alloca((nb_params * 3) * sizeof(TCGv)); 563c896fe29Sbellard j = 0; 564c896fe29Sbellard call_type = (flags & TCG_CALL_TYPE_MASK); 565c896fe29Sbellard for(i = 0; i < nb_params; i++) { 566c896fe29Sbellard arg = args1[i]; 567c896fe29Sbellard if (tcg_get_base_type(s, arg) == TCG_TYPE_I64) { 568c896fe29Sbellard #ifdef TCG_TARGET_I386 569c896fe29Sbellard /* REGPARM case: if the third parameter is 64 bit, it is 570c896fe29Sbellard allocated on the stack */ 571c896fe29Sbellard if (j == 2 && call_type == TCG_CALL_TYPE_REGPARM) { 572c896fe29Sbellard call_type = TCG_CALL_TYPE_REGPARM_2; 573c896fe29Sbellard flags = (flags & ~TCG_CALL_TYPE_MASK) | call_type; 574c896fe29Sbellard } 575c896fe29Sbellard args2[j++] = arg; 576ac56dd48Spbrook args2[j++] = TCGV_HIGH(arg); 577c896fe29Sbellard #else 57839cf05d3Sbellard #ifdef TCG_TARGET_CALL_ALIGN_ARGS 57939cf05d3Sbellard /* some targets want aligned 64 bit args */ 58039cf05d3Sbellard if (j & 1) { 58139cf05d3Sbellard args2[j++] = TCG_CALL_DUMMY_ARG; 58239cf05d3Sbellard } 58339cf05d3Sbellard #endif 584c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN 585a0d69e00Sblueswir1 args2[j++] = TCGV_HIGH(arg); 586c896fe29Sbellard args2[j++] = arg; 587c896fe29Sbellard #else 588c896fe29Sbellard args2[j++] = arg; 589ac56dd48Spbrook args2[j++] = TCGV_HIGH(arg); 590c896fe29Sbellard #endif 591c896fe29Sbellard #endif 592c896fe29Sbellard } else { 593c896fe29Sbellard args2[j++] = arg; 594c896fe29Sbellard } 595c896fe29Sbellard } 596c896fe29Sbellard tcg_gen_call_internal(s, func, flags, 597c896fe29Sbellard nb_rets, rets, j, args2); 598c896fe29Sbellard } 599c896fe29Sbellard #else 600ac56dd48Spbrook void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags, 601ac56dd48Spbrook unsigned int nb_rets, const TCGv *rets, 602ac56dd48Spbrook unsigned int nb_params, const TCGv *args1) 603c896fe29Sbellard { 604c896fe29Sbellard tcg_gen_call_internal(s, func, flags, 605c896fe29Sbellard nb_rets, rets, nb_params, args1); 606c896fe29Sbellard } 607c896fe29Sbellard #endif 608c896fe29Sbellard 609ac56dd48Spbrook #if TCG_TARGET_REG_BITS == 32 610ac56dd48Spbrook void tcg_gen_shifti_i64(TCGv ret, TCGv arg1, 611c896fe29Sbellard int c, int right, int arith) 612c896fe29Sbellard { 613cf60bce4Sbellard if (c == 0) { 614cf60bce4Sbellard tcg_gen_mov_i32(ret, arg1); 615cf60bce4Sbellard tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1)); 616cf60bce4Sbellard } else if (c >= 32) { 617c896fe29Sbellard c -= 32; 618c896fe29Sbellard if (right) { 619c896fe29Sbellard if (arith) { 620ac56dd48Spbrook tcg_gen_sari_i32(ret, TCGV_HIGH(arg1), c); 621ac56dd48Spbrook tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31); 622c896fe29Sbellard } else { 623ac56dd48Spbrook tcg_gen_shri_i32(ret, TCGV_HIGH(arg1), c); 624ac56dd48Spbrook tcg_gen_movi_i32(TCGV_HIGH(ret), 0); 625c896fe29Sbellard } 626c896fe29Sbellard } else { 627ac56dd48Spbrook tcg_gen_shli_i32(TCGV_HIGH(ret), arg1, c); 628c896fe29Sbellard tcg_gen_movi_i32(ret, 0); 629c896fe29Sbellard } 630c896fe29Sbellard } else { 631ac56dd48Spbrook TCGv t0, t1; 632c896fe29Sbellard 633c896fe29Sbellard t0 = tcg_temp_new(TCG_TYPE_I32); 634c896fe29Sbellard t1 = tcg_temp_new(TCG_TYPE_I32); 635c896fe29Sbellard if (right) { 636ac56dd48Spbrook tcg_gen_shli_i32(t0, TCGV_HIGH(arg1), 32 - c); 637c896fe29Sbellard if (arith) 638ac56dd48Spbrook tcg_gen_sari_i32(t1, TCGV_HIGH(arg1), c); 639c896fe29Sbellard else 640ac56dd48Spbrook tcg_gen_shri_i32(t1, TCGV_HIGH(arg1), c); 641c896fe29Sbellard tcg_gen_shri_i32(ret, arg1, c); 642c896fe29Sbellard tcg_gen_or_i32(ret, ret, t0); 643ac56dd48Spbrook tcg_gen_mov_i32(TCGV_HIGH(ret), t1); 644c896fe29Sbellard } else { 645c896fe29Sbellard tcg_gen_shri_i32(t0, arg1, 32 - c); 646c896fe29Sbellard /* Note: ret can be the same as arg1, so we use t1 */ 647c896fe29Sbellard tcg_gen_shli_i32(t1, arg1, c); 648ac56dd48Spbrook tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c); 649ac56dd48Spbrook tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t0); 650c896fe29Sbellard tcg_gen_mov_i32(ret, t1); 651c896fe29Sbellard } 652e8996ee0Sbellard tcg_temp_free(t0); 653e8996ee0Sbellard tcg_temp_free(t1); 654c896fe29Sbellard } 655c896fe29Sbellard } 656ac56dd48Spbrook #endif 657c896fe29Sbellard 658c896fe29Sbellard void tcg_reg_alloc_start(TCGContext *s) 659c896fe29Sbellard { 660c896fe29Sbellard int i; 661c896fe29Sbellard TCGTemp *ts; 662c896fe29Sbellard for(i = 0; i < s->nb_globals; i++) { 663c896fe29Sbellard ts = &s->temps[i]; 664c896fe29Sbellard if (ts->fixed_reg) { 665c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 666c896fe29Sbellard } else { 667c896fe29Sbellard ts->val_type = TEMP_VAL_MEM; 668c896fe29Sbellard } 669c896fe29Sbellard } 670e8996ee0Sbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 671e8996ee0Sbellard ts = &s->temps[i]; 672e8996ee0Sbellard ts->val_type = TEMP_VAL_DEAD; 673e8996ee0Sbellard ts->mem_allocated = 0; 674e8996ee0Sbellard ts->fixed_reg = 0; 675e8996ee0Sbellard } 676c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 677c896fe29Sbellard s->reg_to_temp[i] = -1; 678c896fe29Sbellard } 679c896fe29Sbellard } 680c896fe29Sbellard 681ac56dd48Spbrook static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size, 682ac56dd48Spbrook int idx) 683c896fe29Sbellard { 684c896fe29Sbellard TCGTemp *ts; 685ac56dd48Spbrook 686ac56dd48Spbrook ts = &s->temps[idx]; 687ac56dd48Spbrook if (idx < s->nb_globals) { 688ac56dd48Spbrook pstrcpy(buf, buf_size, ts->name); 689c896fe29Sbellard } else { 690641d5fbeSbellard if (ts->temp_local) 691641d5fbeSbellard snprintf(buf, buf_size, "loc%d", idx - s->nb_globals); 692641d5fbeSbellard else 693ac56dd48Spbrook snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals); 694c896fe29Sbellard } 695c896fe29Sbellard return buf; 696c896fe29Sbellard } 697c896fe29Sbellard 698ac56dd48Spbrook char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGv arg) 699ac56dd48Spbrook { 700ac56dd48Spbrook return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV(arg)); 701ac56dd48Spbrook } 702ac56dd48Spbrook 703e8996ee0Sbellard static int helper_cmp(const void *p1, const void *p2) 704e8996ee0Sbellard { 705e8996ee0Sbellard const TCGHelperInfo *th1 = p1; 706e8996ee0Sbellard const TCGHelperInfo *th2 = p2; 707e8996ee0Sbellard if (th1->func < th2->func) 708e8996ee0Sbellard return -1; 709e8996ee0Sbellard else if (th1->func == th2->func) 710e8996ee0Sbellard return 0; 711e8996ee0Sbellard else 712e8996ee0Sbellard return 1; 713e8996ee0Sbellard } 714e8996ee0Sbellard 715e8996ee0Sbellard /* find helper definition (Note: A hash table would be better) */ 7164dc81f28Sbellard static TCGHelperInfo *tcg_find_helper(TCGContext *s, tcg_target_ulong val) 7174dc81f28Sbellard { 718e8996ee0Sbellard int m, m_min, m_max; 719e8996ee0Sbellard TCGHelperInfo *th; 720e8996ee0Sbellard tcg_target_ulong v; 721e8996ee0Sbellard 722e8996ee0Sbellard if (unlikely(!s->helpers_sorted)) { 723e8996ee0Sbellard qsort(s->helpers, s->nb_helpers, sizeof(TCGHelperInfo), 724e8996ee0Sbellard helper_cmp); 725e8996ee0Sbellard s->helpers_sorted = 1; 726e8996ee0Sbellard } 727e8996ee0Sbellard 728e8996ee0Sbellard /* binary search */ 729e8996ee0Sbellard m_min = 0; 730e8996ee0Sbellard m_max = s->nb_helpers - 1; 731e8996ee0Sbellard while (m_min <= m_max) { 732e8996ee0Sbellard m = (m_min + m_max) >> 1; 733e8996ee0Sbellard th = &s->helpers[m]; 734e8996ee0Sbellard v = th->func; 735e8996ee0Sbellard if (v == val) 736e8996ee0Sbellard return th; 737e8996ee0Sbellard else if (val < v) { 738e8996ee0Sbellard m_max = m - 1; 739e8996ee0Sbellard } else { 740e8996ee0Sbellard m_min = m + 1; 741e8996ee0Sbellard } 7424dc81f28Sbellard } 7434dc81f28Sbellard return NULL; 7444dc81f28Sbellard } 7454dc81f28Sbellard 746c896fe29Sbellard void tcg_dump_ops(TCGContext *s, FILE *outfile) 747c896fe29Sbellard { 748c896fe29Sbellard const uint16_t *opc_ptr; 749c896fe29Sbellard const TCGArg *args; 750c896fe29Sbellard TCGArg arg; 7517e4597d7Sbellard int c, i, k, nb_oargs, nb_iargs, nb_cargs, first_insn; 752c896fe29Sbellard const TCGOpDef *def; 753c896fe29Sbellard char buf[128]; 754c896fe29Sbellard 7557e4597d7Sbellard first_insn = 1; 756c896fe29Sbellard opc_ptr = gen_opc_buf; 757c896fe29Sbellard args = gen_opparam_buf; 758c896fe29Sbellard while (opc_ptr < gen_opc_ptr) { 759c896fe29Sbellard c = *opc_ptr++; 760c896fe29Sbellard def = &tcg_op_defs[c]; 7617e4597d7Sbellard if (c == INDEX_op_debug_insn_start) { 7627e4597d7Sbellard uint64_t pc; 7637e4597d7Sbellard #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS 7647e4597d7Sbellard pc = ((uint64_t)args[1] << 32) | args[0]; 7657e4597d7Sbellard #else 7667e4597d7Sbellard pc = args[0]; 7677e4597d7Sbellard #endif 7687e4597d7Sbellard if (!first_insn) 7697e4597d7Sbellard fprintf(outfile, "\n"); 7707e4597d7Sbellard fprintf(outfile, " ---- 0x%" PRIx64, pc); 7717e4597d7Sbellard first_insn = 0; 7727e4597d7Sbellard nb_oargs = def->nb_oargs; 7737e4597d7Sbellard nb_iargs = def->nb_iargs; 7747e4597d7Sbellard nb_cargs = def->nb_cargs; 7757e4597d7Sbellard } else if (c == INDEX_op_call) { 776c896fe29Sbellard TCGArg arg; 7774dc81f28Sbellard 778c896fe29Sbellard /* variable number of arguments */ 779c896fe29Sbellard arg = *args++; 780c896fe29Sbellard nb_oargs = arg >> 16; 781c896fe29Sbellard nb_iargs = arg & 0xffff; 782c896fe29Sbellard nb_cargs = def->nb_cargs; 783b03cce8eSbellard 7847e4597d7Sbellard fprintf(outfile, " %s ", def->name); 7857e4597d7Sbellard 786b03cce8eSbellard /* function name */ 787b03cce8eSbellard fprintf(outfile, "%s", 788e8996ee0Sbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + nb_iargs - 1])); 789b03cce8eSbellard /* flags */ 790b03cce8eSbellard fprintf(outfile, ",$0x%" TCG_PRIlx, 791b03cce8eSbellard args[nb_oargs + nb_iargs]); 792b03cce8eSbellard /* nb out args */ 793b03cce8eSbellard fprintf(outfile, ",$%d", nb_oargs); 794b03cce8eSbellard for(i = 0; i < nb_oargs; i++) { 795b03cce8eSbellard fprintf(outfile, ","); 796b03cce8eSbellard fprintf(outfile, "%s", 797b03cce8eSbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[i])); 798b03cce8eSbellard } 799b03cce8eSbellard for(i = 0; i < (nb_iargs - 1); i++) { 800b03cce8eSbellard fprintf(outfile, ","); 80139cf05d3Sbellard if (args[nb_oargs + i] == TCG_CALL_DUMMY_ARG) { 80239cf05d3Sbellard fprintf(outfile, "<dummy>"); 80339cf05d3Sbellard } else { 804b03cce8eSbellard fprintf(outfile, "%s", 805b03cce8eSbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + i])); 806b03cce8eSbellard } 80739cf05d3Sbellard } 808e8996ee0Sbellard } else if (c == INDEX_op_movi_i32 809e8996ee0Sbellard #if TCG_TARGET_REG_BITS == 64 810e8996ee0Sbellard || c == INDEX_op_movi_i64 811e8996ee0Sbellard #endif 812e8996ee0Sbellard ) { 813e8996ee0Sbellard tcg_target_ulong val; 814e8996ee0Sbellard TCGHelperInfo *th; 815e8996ee0Sbellard 816e8996ee0Sbellard nb_oargs = def->nb_oargs; 817e8996ee0Sbellard nb_iargs = def->nb_iargs; 818e8996ee0Sbellard nb_cargs = def->nb_cargs; 819e8996ee0Sbellard fprintf(outfile, " %s %s,$", def->name, 820e8996ee0Sbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[0])); 821e8996ee0Sbellard val = args[1]; 822e8996ee0Sbellard th = tcg_find_helper(s, val); 823e8996ee0Sbellard if (th) { 824e8996ee0Sbellard fprintf(outfile, th->name); 825e8996ee0Sbellard } else { 826e8996ee0Sbellard if (c == INDEX_op_movi_i32) 827e8996ee0Sbellard fprintf(outfile, "0x%x", (uint32_t)val); 828e8996ee0Sbellard else 829e8996ee0Sbellard fprintf(outfile, "0x%" PRIx64 , (uint64_t)val); 830e8996ee0Sbellard } 831b03cce8eSbellard } else { 8327e4597d7Sbellard fprintf(outfile, " %s ", def->name); 833b03cce8eSbellard if (c == INDEX_op_nopn) { 834c896fe29Sbellard /* variable number of arguments */ 835c896fe29Sbellard nb_cargs = *args; 836c896fe29Sbellard nb_oargs = 0; 837c896fe29Sbellard nb_iargs = 0; 838c896fe29Sbellard } else { 839c896fe29Sbellard nb_oargs = def->nb_oargs; 840c896fe29Sbellard nb_iargs = def->nb_iargs; 841c896fe29Sbellard nb_cargs = def->nb_cargs; 842c896fe29Sbellard } 843c896fe29Sbellard 844c896fe29Sbellard k = 0; 845c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 846c896fe29Sbellard if (k != 0) 847c896fe29Sbellard fprintf(outfile, ","); 848ac56dd48Spbrook fprintf(outfile, "%s", 849ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++])); 850c896fe29Sbellard } 851c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 852c896fe29Sbellard if (k != 0) 853c896fe29Sbellard fprintf(outfile, ","); 854ac56dd48Spbrook fprintf(outfile, "%s", 855ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++])); 856c896fe29Sbellard } 857c896fe29Sbellard for(i = 0; i < nb_cargs; i++) { 858c896fe29Sbellard if (k != 0) 859c896fe29Sbellard fprintf(outfile, ","); 860c896fe29Sbellard arg = args[k++]; 861c896fe29Sbellard fprintf(outfile, "$0x%" TCG_PRIlx, arg); 862c896fe29Sbellard } 863b03cce8eSbellard } 864c896fe29Sbellard fprintf(outfile, "\n"); 865c896fe29Sbellard args += nb_iargs + nb_oargs + nb_cargs; 866c896fe29Sbellard } 867c896fe29Sbellard } 868c896fe29Sbellard 869c896fe29Sbellard /* we give more priority to constraints with less registers */ 870c896fe29Sbellard static int get_constraint_priority(const TCGOpDef *def, int k) 871c896fe29Sbellard { 872c896fe29Sbellard const TCGArgConstraint *arg_ct; 873c896fe29Sbellard 874c896fe29Sbellard int i, n; 875c896fe29Sbellard arg_ct = &def->args_ct[k]; 876c896fe29Sbellard if (arg_ct->ct & TCG_CT_ALIAS) { 877c896fe29Sbellard /* an alias is equivalent to a single register */ 878c896fe29Sbellard n = 1; 879c896fe29Sbellard } else { 880c896fe29Sbellard if (!(arg_ct->ct & TCG_CT_REG)) 881c896fe29Sbellard return 0; 882c896fe29Sbellard n = 0; 883c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 884c896fe29Sbellard if (tcg_regset_test_reg(arg_ct->u.regs, i)) 885c896fe29Sbellard n++; 886c896fe29Sbellard } 887c896fe29Sbellard } 888c896fe29Sbellard return TCG_TARGET_NB_REGS - n + 1; 889c896fe29Sbellard } 890c896fe29Sbellard 891c896fe29Sbellard /* sort from highest priority to lowest */ 892c896fe29Sbellard static void sort_constraints(TCGOpDef *def, int start, int n) 893c896fe29Sbellard { 894c896fe29Sbellard int i, j, p1, p2, tmp; 895c896fe29Sbellard 896c896fe29Sbellard for(i = 0; i < n; i++) 897c896fe29Sbellard def->sorted_args[start + i] = start + i; 898c896fe29Sbellard if (n <= 1) 899c896fe29Sbellard return; 900c896fe29Sbellard for(i = 0; i < n - 1; i++) { 901c896fe29Sbellard for(j = i + 1; j < n; j++) { 902c896fe29Sbellard p1 = get_constraint_priority(def, def->sorted_args[start + i]); 903c896fe29Sbellard p2 = get_constraint_priority(def, def->sorted_args[start + j]); 904c896fe29Sbellard if (p1 < p2) { 905c896fe29Sbellard tmp = def->sorted_args[start + i]; 906c896fe29Sbellard def->sorted_args[start + i] = def->sorted_args[start + j]; 907c896fe29Sbellard def->sorted_args[start + j] = tmp; 908c896fe29Sbellard } 909c896fe29Sbellard } 910c896fe29Sbellard } 911c896fe29Sbellard } 912c896fe29Sbellard 913c896fe29Sbellard void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs) 914c896fe29Sbellard { 915c896fe29Sbellard int op; 916c896fe29Sbellard TCGOpDef *def; 917c896fe29Sbellard const char *ct_str; 918c896fe29Sbellard int i, nb_args; 919c896fe29Sbellard 920c896fe29Sbellard for(;;) { 921c896fe29Sbellard if (tdefs->op < 0) 922c896fe29Sbellard break; 923c896fe29Sbellard op = tdefs->op; 924c896fe29Sbellard assert(op >= 0 && op < NB_OPS); 925c896fe29Sbellard def = &tcg_op_defs[op]; 926c896fe29Sbellard nb_args = def->nb_iargs + def->nb_oargs; 927c896fe29Sbellard for(i = 0; i < nb_args; i++) { 928c896fe29Sbellard ct_str = tdefs->args_ct_str[i]; 929c896fe29Sbellard tcg_regset_clear(def->args_ct[i].u.regs); 930c896fe29Sbellard def->args_ct[i].ct = 0; 931c896fe29Sbellard if (ct_str[0] >= '0' && ct_str[0] <= '9') { 932c896fe29Sbellard int oarg; 933c896fe29Sbellard oarg = ct_str[0] - '0'; 934c896fe29Sbellard assert(oarg < def->nb_oargs); 935c896fe29Sbellard assert(def->args_ct[oarg].ct & TCG_CT_REG); 936c896fe29Sbellard /* TCG_CT_ALIAS is for the output arguments. The input 9375ff9d6a4Sbellard argument is tagged with TCG_CT_IALIAS. */ 938c896fe29Sbellard def->args_ct[i] = def->args_ct[oarg]; 9395ff9d6a4Sbellard def->args_ct[oarg].ct = TCG_CT_ALIAS; 9405ff9d6a4Sbellard def->args_ct[oarg].alias_index = i; 941c896fe29Sbellard def->args_ct[i].ct |= TCG_CT_IALIAS; 9425ff9d6a4Sbellard def->args_ct[i].alias_index = oarg; 943c896fe29Sbellard } else { 944c896fe29Sbellard for(;;) { 945c896fe29Sbellard if (*ct_str == '\0') 946c896fe29Sbellard break; 947c896fe29Sbellard switch(*ct_str) { 948c896fe29Sbellard case 'i': 949c896fe29Sbellard def->args_ct[i].ct |= TCG_CT_CONST; 950c896fe29Sbellard ct_str++; 951c896fe29Sbellard break; 952c896fe29Sbellard default: 953c896fe29Sbellard if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) { 954c896fe29Sbellard fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n", 955c896fe29Sbellard ct_str, i, def->name); 956c896fe29Sbellard exit(1); 957c896fe29Sbellard } 958c896fe29Sbellard } 959c896fe29Sbellard } 960c896fe29Sbellard } 961c896fe29Sbellard } 962c896fe29Sbellard 963c896fe29Sbellard /* sort the constraints (XXX: this is just an heuristic) */ 964c896fe29Sbellard sort_constraints(def, 0, def->nb_oargs); 965c896fe29Sbellard sort_constraints(def, def->nb_oargs, def->nb_iargs); 966c896fe29Sbellard 967c896fe29Sbellard #if 0 968c896fe29Sbellard { 969c896fe29Sbellard int i; 970c896fe29Sbellard 971c896fe29Sbellard printf("%s: sorted=", def->name); 972c896fe29Sbellard for(i = 0; i < def->nb_oargs + def->nb_iargs; i++) 973c896fe29Sbellard printf(" %d", def->sorted_args[i]); 974c896fe29Sbellard printf("\n"); 975c896fe29Sbellard } 976c896fe29Sbellard #endif 977c896fe29Sbellard tdefs++; 978c896fe29Sbellard } 979c896fe29Sbellard 980c896fe29Sbellard } 981c896fe29Sbellard 982c896fe29Sbellard #ifdef USE_LIVENESS_ANALYSIS 983c896fe29Sbellard 984c896fe29Sbellard /* set a nop for an operation using 'nb_args' */ 985c896fe29Sbellard static inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr, 986c896fe29Sbellard TCGArg *args, int nb_args) 987c896fe29Sbellard { 988c896fe29Sbellard if (nb_args == 0) { 989c896fe29Sbellard *opc_ptr = INDEX_op_nop; 990c896fe29Sbellard } else { 991c896fe29Sbellard *opc_ptr = INDEX_op_nopn; 992c896fe29Sbellard args[0] = nb_args; 993c896fe29Sbellard args[nb_args - 1] = nb_args; 994c896fe29Sbellard } 995c896fe29Sbellard } 996c896fe29Sbellard 997641d5fbeSbellard /* liveness analysis: end of function: globals are live, temps are 998641d5fbeSbellard dead. */ 999641d5fbeSbellard /* XXX: at this stage, not used as there would be little gains because 1000641d5fbeSbellard most TBs end with a conditional jump. */ 1001641d5fbeSbellard static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps) 1002c896fe29Sbellard { 1003c896fe29Sbellard memset(dead_temps, 0, s->nb_globals); 1004c896fe29Sbellard memset(dead_temps + s->nb_globals, 1, s->nb_temps - s->nb_globals); 1005c896fe29Sbellard } 1006c896fe29Sbellard 1007641d5fbeSbellard /* liveness analysis: end of basic block: globals are live, temps are 1008641d5fbeSbellard dead, local temps are live. */ 1009641d5fbeSbellard static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps) 1010641d5fbeSbellard { 1011641d5fbeSbellard int i; 1012641d5fbeSbellard TCGTemp *ts; 1013641d5fbeSbellard 1014641d5fbeSbellard memset(dead_temps, 0, s->nb_globals); 1015641d5fbeSbellard ts = &s->temps[s->nb_globals]; 1016641d5fbeSbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 1017641d5fbeSbellard if (ts->temp_local) 1018641d5fbeSbellard dead_temps[i] = 0; 1019641d5fbeSbellard else 1020641d5fbeSbellard dead_temps[i] = 1; 1021641d5fbeSbellard ts++; 1022641d5fbeSbellard } 1023641d5fbeSbellard } 1024641d5fbeSbellard 1025c896fe29Sbellard /* Liveness analysis : update the opc_dead_iargs array to tell if a 1026c896fe29Sbellard given input arguments is dead. Instructions updating dead 1027c896fe29Sbellard temporaries are removed. */ 1028c896fe29Sbellard void tcg_liveness_analysis(TCGContext *s) 1029c896fe29Sbellard { 1030c896fe29Sbellard int i, op_index, op, nb_args, nb_iargs, nb_oargs, arg, nb_ops; 1031c896fe29Sbellard TCGArg *args; 1032c896fe29Sbellard const TCGOpDef *def; 1033c896fe29Sbellard uint8_t *dead_temps; 1034c896fe29Sbellard unsigned int dead_iargs; 1035c896fe29Sbellard 1036c896fe29Sbellard gen_opc_ptr++; /* skip end */ 1037c896fe29Sbellard 1038c896fe29Sbellard nb_ops = gen_opc_ptr - gen_opc_buf; 1039c896fe29Sbellard 1040c896fe29Sbellard /* XXX: make it really dynamic */ 1041c896fe29Sbellard s->op_dead_iargs = tcg_malloc(OPC_BUF_SIZE * sizeof(uint16_t)); 1042c896fe29Sbellard 1043c896fe29Sbellard dead_temps = tcg_malloc(s->nb_temps); 1044c896fe29Sbellard memset(dead_temps, 1, s->nb_temps); 1045c896fe29Sbellard 1046c896fe29Sbellard args = gen_opparam_ptr; 1047c896fe29Sbellard op_index = nb_ops - 1; 1048c896fe29Sbellard while (op_index >= 0) { 1049c896fe29Sbellard op = gen_opc_buf[op_index]; 1050c896fe29Sbellard def = &tcg_op_defs[op]; 1051c896fe29Sbellard switch(op) { 1052c896fe29Sbellard case INDEX_op_call: 1053c6e113f5Sbellard { 1054c6e113f5Sbellard int call_flags; 1055c6e113f5Sbellard 1056c896fe29Sbellard nb_args = args[-1]; 1057c896fe29Sbellard args -= nb_args; 1058c896fe29Sbellard nb_iargs = args[0] & 0xffff; 1059c896fe29Sbellard nb_oargs = args[0] >> 16; 1060c896fe29Sbellard args++; 1061c6e113f5Sbellard call_flags = args[nb_oargs + nb_iargs]; 1062c6e113f5Sbellard 1063c6e113f5Sbellard /* pure functions can be removed if their result is not 1064c6e113f5Sbellard used */ 1065c6e113f5Sbellard if (call_flags & TCG_CALL_PURE) { 1066c6e113f5Sbellard for(i = 0; i < nb_oargs; i++) { 1067c6e113f5Sbellard arg = args[i]; 1068c6e113f5Sbellard if (!dead_temps[arg]) 1069c6e113f5Sbellard goto do_not_remove_call; 1070c6e113f5Sbellard } 1071c6e113f5Sbellard tcg_set_nop(s, gen_opc_buf + op_index, 1072c6e113f5Sbellard args - 1, nb_args); 1073c6e113f5Sbellard } else { 1074c6e113f5Sbellard do_not_remove_call: 1075c896fe29Sbellard 1076c896fe29Sbellard /* output args are dead */ 1077c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1078c896fe29Sbellard arg = args[i]; 1079c896fe29Sbellard dead_temps[arg] = 1; 1080c896fe29Sbellard } 1081c896fe29Sbellard 1082c896fe29Sbellard /* globals are live (they may be used by the call) */ 1083c896fe29Sbellard memset(dead_temps, 0, s->nb_globals); 1084c896fe29Sbellard 1085c896fe29Sbellard /* input args are live */ 1086c896fe29Sbellard dead_iargs = 0; 1087c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 1088c896fe29Sbellard arg = args[i + nb_oargs]; 108939cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1090c896fe29Sbellard if (dead_temps[arg]) { 1091c896fe29Sbellard dead_iargs |= (1 << i); 1092c896fe29Sbellard } 1093c896fe29Sbellard dead_temps[arg] = 0; 1094c896fe29Sbellard } 109539cf05d3Sbellard } 1096c896fe29Sbellard s->op_dead_iargs[op_index] = dead_iargs; 1097c6e113f5Sbellard } 1098c896fe29Sbellard args--; 1099c6e113f5Sbellard } 1100c896fe29Sbellard break; 1101c896fe29Sbellard case INDEX_op_set_label: 1102c896fe29Sbellard args--; 1103c896fe29Sbellard /* mark end of basic block */ 1104c896fe29Sbellard tcg_la_bb_end(s, dead_temps); 1105c896fe29Sbellard break; 11067e4597d7Sbellard case INDEX_op_debug_insn_start: 11077e4597d7Sbellard args -= def->nb_args; 11087e4597d7Sbellard break; 1109c896fe29Sbellard case INDEX_op_nopn: 1110c896fe29Sbellard nb_args = args[-1]; 1111c896fe29Sbellard args -= nb_args; 1112c896fe29Sbellard break; 11135ff9d6a4Sbellard case INDEX_op_discard: 11145ff9d6a4Sbellard args--; 11155ff9d6a4Sbellard /* mark the temporary as dead */ 11165ff9d6a4Sbellard dead_temps[args[0]] = 1; 11175ff9d6a4Sbellard break; 1118c896fe29Sbellard case INDEX_op_end: 1119c896fe29Sbellard break; 1120c896fe29Sbellard /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */ 1121c896fe29Sbellard default: 1122c896fe29Sbellard if (op > INDEX_op_end) { 1123c896fe29Sbellard args -= def->nb_args; 1124c896fe29Sbellard nb_iargs = def->nb_iargs; 1125c896fe29Sbellard nb_oargs = def->nb_oargs; 1126c896fe29Sbellard 1127c896fe29Sbellard /* Test if the operation can be removed because all 11285ff9d6a4Sbellard its outputs are dead. We assume that nb_oargs == 0 11295ff9d6a4Sbellard implies side effects */ 11305ff9d6a4Sbellard if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) { 1131c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1132c896fe29Sbellard arg = args[i]; 1133c896fe29Sbellard if (!dead_temps[arg]) 1134c896fe29Sbellard goto do_not_remove; 1135c896fe29Sbellard } 1136c896fe29Sbellard tcg_set_nop(s, gen_opc_buf + op_index, args, def->nb_args); 1137c896fe29Sbellard #ifdef CONFIG_PROFILER 1138a23a9ec6Sbellard s->del_op_count++; 1139c896fe29Sbellard #endif 1140c896fe29Sbellard } else { 1141c896fe29Sbellard do_not_remove: 1142c896fe29Sbellard 1143c896fe29Sbellard /* output args are dead */ 1144c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1145c896fe29Sbellard arg = args[i]; 1146c896fe29Sbellard dead_temps[arg] = 1; 1147c896fe29Sbellard } 1148c896fe29Sbellard 1149c896fe29Sbellard /* if end of basic block, update */ 1150c896fe29Sbellard if (def->flags & TCG_OPF_BB_END) { 1151c896fe29Sbellard tcg_la_bb_end(s, dead_temps); 1152b03cce8eSbellard } else if (def->flags & TCG_OPF_CALL_CLOBBER) { 1153b03cce8eSbellard /* globals are live */ 1154b03cce8eSbellard memset(dead_temps, 0, s->nb_globals); 1155c896fe29Sbellard } 1156c896fe29Sbellard 1157c896fe29Sbellard /* input args are live */ 1158c896fe29Sbellard dead_iargs = 0; 1159c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 1160c896fe29Sbellard arg = args[i + nb_oargs]; 1161c896fe29Sbellard if (dead_temps[arg]) { 1162c896fe29Sbellard dead_iargs |= (1 << i); 1163c896fe29Sbellard } 1164c896fe29Sbellard dead_temps[arg] = 0; 1165c896fe29Sbellard } 1166c896fe29Sbellard s->op_dead_iargs[op_index] = dead_iargs; 1167c896fe29Sbellard } 1168c896fe29Sbellard } else { 1169c896fe29Sbellard /* legacy dyngen operations */ 1170c896fe29Sbellard args -= def->nb_args; 1171c896fe29Sbellard /* mark end of basic block */ 1172c896fe29Sbellard tcg_la_bb_end(s, dead_temps); 1173c896fe29Sbellard } 1174c896fe29Sbellard break; 1175c896fe29Sbellard } 1176c896fe29Sbellard op_index--; 1177c896fe29Sbellard } 1178c896fe29Sbellard 1179c896fe29Sbellard if (args != gen_opparam_buf) 1180c896fe29Sbellard tcg_abort(); 1181c896fe29Sbellard } 1182c896fe29Sbellard #else 1183c896fe29Sbellard /* dummy liveness analysis */ 1184c896fe29Sbellard void tcg_liveness_analysis(TCGContext *s) 1185c896fe29Sbellard { 1186c896fe29Sbellard int nb_ops; 1187c896fe29Sbellard nb_ops = gen_opc_ptr - gen_opc_buf; 1188c896fe29Sbellard 1189c896fe29Sbellard s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t)); 1190c896fe29Sbellard memset(s->op_dead_iargs, 0, nb_ops * sizeof(uint16_t)); 1191c896fe29Sbellard } 1192c896fe29Sbellard #endif 1193c896fe29Sbellard 1194c896fe29Sbellard #ifndef NDEBUG 1195c896fe29Sbellard static void dump_regs(TCGContext *s) 1196c896fe29Sbellard { 1197c896fe29Sbellard TCGTemp *ts; 1198c896fe29Sbellard int i; 1199c896fe29Sbellard char buf[64]; 1200c896fe29Sbellard 1201c896fe29Sbellard for(i = 0; i < s->nb_temps; i++) { 1202c896fe29Sbellard ts = &s->temps[i]; 1203ac56dd48Spbrook printf(" %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i)); 1204c896fe29Sbellard switch(ts->val_type) { 1205c896fe29Sbellard case TEMP_VAL_REG: 1206c896fe29Sbellard printf("%s", tcg_target_reg_names[ts->reg]); 1207c896fe29Sbellard break; 1208c896fe29Sbellard case TEMP_VAL_MEM: 1209c896fe29Sbellard printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]); 1210c896fe29Sbellard break; 1211c896fe29Sbellard case TEMP_VAL_CONST: 1212c896fe29Sbellard printf("$0x%" TCG_PRIlx, ts->val); 1213c896fe29Sbellard break; 1214c896fe29Sbellard case TEMP_VAL_DEAD: 1215c896fe29Sbellard printf("D"); 1216c896fe29Sbellard break; 1217c896fe29Sbellard default: 1218c896fe29Sbellard printf("???"); 1219c896fe29Sbellard break; 1220c896fe29Sbellard } 1221c896fe29Sbellard printf("\n"); 1222c896fe29Sbellard } 1223c896fe29Sbellard 1224c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 1225c896fe29Sbellard if (s->reg_to_temp[i] >= 0) { 1226c896fe29Sbellard printf("%s: %s\n", 1227c896fe29Sbellard tcg_target_reg_names[i], 1228ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i])); 1229c896fe29Sbellard } 1230c896fe29Sbellard } 1231c896fe29Sbellard } 1232c896fe29Sbellard 1233c896fe29Sbellard static void check_regs(TCGContext *s) 1234c896fe29Sbellard { 1235c896fe29Sbellard int reg, k; 1236c896fe29Sbellard TCGTemp *ts; 1237c896fe29Sbellard char buf[64]; 1238c896fe29Sbellard 1239c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1240c896fe29Sbellard k = s->reg_to_temp[reg]; 1241c896fe29Sbellard if (k >= 0) { 1242c896fe29Sbellard ts = &s->temps[k]; 1243c896fe29Sbellard if (ts->val_type != TEMP_VAL_REG || 1244c896fe29Sbellard ts->reg != reg) { 1245c896fe29Sbellard printf("Inconsistency for register %s:\n", 1246c896fe29Sbellard tcg_target_reg_names[reg]); 1247b03cce8eSbellard goto fail; 1248c896fe29Sbellard } 1249c896fe29Sbellard } 1250c896fe29Sbellard } 1251c896fe29Sbellard for(k = 0; k < s->nb_temps; k++) { 1252c896fe29Sbellard ts = &s->temps[k]; 1253c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG && 1254c896fe29Sbellard !ts->fixed_reg && 1255c896fe29Sbellard s->reg_to_temp[ts->reg] != k) { 1256c896fe29Sbellard printf("Inconsistency for temp %s:\n", 1257ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), k)); 1258b03cce8eSbellard fail: 1259c896fe29Sbellard printf("reg state:\n"); 1260c896fe29Sbellard dump_regs(s); 1261c896fe29Sbellard tcg_abort(); 1262c896fe29Sbellard } 1263c896fe29Sbellard } 1264c896fe29Sbellard } 1265c896fe29Sbellard #endif 1266c896fe29Sbellard 1267c896fe29Sbellard static void temp_allocate_frame(TCGContext *s, int temp) 1268c896fe29Sbellard { 1269c896fe29Sbellard TCGTemp *ts; 1270c896fe29Sbellard ts = &s->temps[temp]; 1271c896fe29Sbellard s->current_frame_offset = (s->current_frame_offset + sizeof(tcg_target_long) - 1) & ~(sizeof(tcg_target_long) - 1); 1272c896fe29Sbellard if (s->current_frame_offset + sizeof(tcg_target_long) > s->frame_end) 12735ff9d6a4Sbellard tcg_abort(); 1274c896fe29Sbellard ts->mem_offset = s->current_frame_offset; 1275c896fe29Sbellard ts->mem_reg = s->frame_reg; 1276c896fe29Sbellard ts->mem_allocated = 1; 1277c896fe29Sbellard s->current_frame_offset += sizeof(tcg_target_long); 1278c896fe29Sbellard } 1279c896fe29Sbellard 1280c896fe29Sbellard /* free register 'reg' by spilling the corresponding temporary if necessary */ 1281c896fe29Sbellard static void tcg_reg_free(TCGContext *s, int reg) 1282c896fe29Sbellard { 1283c896fe29Sbellard TCGTemp *ts; 1284c896fe29Sbellard int temp; 1285c896fe29Sbellard 1286c896fe29Sbellard temp = s->reg_to_temp[reg]; 1287c896fe29Sbellard if (temp != -1) { 1288c896fe29Sbellard ts = &s->temps[temp]; 1289c896fe29Sbellard assert(ts->val_type == TEMP_VAL_REG); 1290c896fe29Sbellard if (!ts->mem_coherent) { 1291c896fe29Sbellard if (!ts->mem_allocated) 1292c896fe29Sbellard temp_allocate_frame(s, temp); 1293e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1294c896fe29Sbellard } 1295c896fe29Sbellard ts->val_type = TEMP_VAL_MEM; 1296c896fe29Sbellard s->reg_to_temp[reg] = -1; 1297c896fe29Sbellard } 1298c896fe29Sbellard } 1299c896fe29Sbellard 1300c896fe29Sbellard /* Allocate a register belonging to reg1 & ~reg2 */ 1301c896fe29Sbellard static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2) 1302c896fe29Sbellard { 1303c896fe29Sbellard int i, reg; 1304c896fe29Sbellard TCGRegSet reg_ct; 1305c896fe29Sbellard 1306c896fe29Sbellard tcg_regset_andnot(reg_ct, reg1, reg2); 1307c896fe29Sbellard 1308c896fe29Sbellard /* first try free registers */ 13090954d0d9Sblueswir1 for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { 1310c896fe29Sbellard reg = tcg_target_reg_alloc_order[i]; 1311c896fe29Sbellard if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1) 1312c896fe29Sbellard return reg; 1313c896fe29Sbellard } 1314c896fe29Sbellard 1315c896fe29Sbellard /* XXX: do better spill choice */ 13160954d0d9Sblueswir1 for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { 1317c896fe29Sbellard reg = tcg_target_reg_alloc_order[i]; 1318c896fe29Sbellard if (tcg_regset_test_reg(reg_ct, reg)) { 1319c896fe29Sbellard tcg_reg_free(s, reg); 1320c896fe29Sbellard return reg; 1321c896fe29Sbellard } 1322c896fe29Sbellard } 1323c896fe29Sbellard 1324c896fe29Sbellard tcg_abort(); 1325c896fe29Sbellard } 1326c896fe29Sbellard 1327641d5fbeSbellard /* save a temporary to memory. 'allocated_regs' is used in case a 1328e8996ee0Sbellard temporary registers needs to be allocated to store a constant. */ 1329641d5fbeSbellard static void temp_save(TCGContext *s, int temp, TCGRegSet allocated_regs) 1330c896fe29Sbellard { 1331c896fe29Sbellard TCGTemp *ts; 1332641d5fbeSbellard int reg; 1333c896fe29Sbellard 1334641d5fbeSbellard ts = &s->temps[temp]; 1335c896fe29Sbellard if (!ts->fixed_reg) { 1336e8996ee0Sbellard switch(ts->val_type) { 1337e8996ee0Sbellard case TEMP_VAL_REG: 1338c896fe29Sbellard tcg_reg_free(s, ts->reg); 1339e8996ee0Sbellard break; 1340e8996ee0Sbellard case TEMP_VAL_DEAD: 1341e5097dc8Sbellard ts->val_type = TEMP_VAL_MEM; 1342e8996ee0Sbellard break; 1343e8996ee0Sbellard case TEMP_VAL_CONST: 1344e8996ee0Sbellard reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 1345e8996ee0Sbellard allocated_regs); 1346641d5fbeSbellard if (!ts->mem_allocated) 1347641d5fbeSbellard temp_allocate_frame(s, temp); 1348e8996ee0Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1349e8996ee0Sbellard tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1350e8996ee0Sbellard ts->val_type = TEMP_VAL_MEM; 1351e8996ee0Sbellard break; 1352e8996ee0Sbellard case TEMP_VAL_MEM: 1353e8996ee0Sbellard break; 1354e8996ee0Sbellard default: 1355e8996ee0Sbellard tcg_abort(); 1356c896fe29Sbellard } 1357c896fe29Sbellard } 1358c896fe29Sbellard } 1359641d5fbeSbellard 1360641d5fbeSbellard /* save globals to their cannonical location and assume they can be 1361641d5fbeSbellard modified be the following code. 'allocated_regs' is used in case a 1362641d5fbeSbellard temporary registers needs to be allocated to store a constant. */ 1363641d5fbeSbellard static void save_globals(TCGContext *s, TCGRegSet allocated_regs) 1364641d5fbeSbellard { 1365641d5fbeSbellard int i; 1366641d5fbeSbellard 1367641d5fbeSbellard for(i = 0; i < s->nb_globals; i++) { 1368641d5fbeSbellard temp_save(s, i, allocated_regs); 1369641d5fbeSbellard } 1370e5097dc8Sbellard } 1371e5097dc8Sbellard 1372e5097dc8Sbellard /* at the end of a basic block, we assume all temporaries are dead and 1373e8996ee0Sbellard all globals are stored at their canonical location. */ 1374e8996ee0Sbellard static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs) 1375e5097dc8Sbellard { 1376e5097dc8Sbellard TCGTemp *ts; 1377e5097dc8Sbellard int i; 1378e5097dc8Sbellard 1379c896fe29Sbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 1380c896fe29Sbellard ts = &s->temps[i]; 1381641d5fbeSbellard if (ts->temp_local) { 1382641d5fbeSbellard temp_save(s, i, allocated_regs); 1383641d5fbeSbellard } else { 1384c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1385c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1386c896fe29Sbellard } 1387c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1388c896fe29Sbellard } 1389641d5fbeSbellard } 1390e8996ee0Sbellard 1391e8996ee0Sbellard save_globals(s, allocated_regs); 1392c896fe29Sbellard } 1393c896fe29Sbellard 1394c896fe29Sbellard #define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1) 1395c896fe29Sbellard 1396e8996ee0Sbellard static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args) 1397e8996ee0Sbellard { 1398e8996ee0Sbellard TCGTemp *ots; 1399e8996ee0Sbellard tcg_target_ulong val; 1400e8996ee0Sbellard 1401e8996ee0Sbellard ots = &s->temps[args[0]]; 1402e8996ee0Sbellard val = args[1]; 1403e8996ee0Sbellard 1404e8996ee0Sbellard if (ots->fixed_reg) { 1405e8996ee0Sbellard /* for fixed registers, we do not do any constant 1406e8996ee0Sbellard propagation */ 1407e8996ee0Sbellard tcg_out_movi(s, ots->type, ots->reg, val); 1408e8996ee0Sbellard } else { 1409e8996ee0Sbellard /* The movi is not explicitely generated here */ 1410e8996ee0Sbellard if (ots->val_type == TEMP_VAL_REG) 1411e8996ee0Sbellard s->reg_to_temp[ots->reg] = -1; 1412e8996ee0Sbellard ots->val_type = TEMP_VAL_CONST; 1413e8996ee0Sbellard ots->val = val; 1414e8996ee0Sbellard } 1415e8996ee0Sbellard } 1416e8996ee0Sbellard 1417c896fe29Sbellard static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def, 1418c896fe29Sbellard const TCGArg *args, 1419c896fe29Sbellard unsigned int dead_iargs) 1420c896fe29Sbellard { 1421c896fe29Sbellard TCGTemp *ts, *ots; 1422c896fe29Sbellard int reg; 1423c896fe29Sbellard const TCGArgConstraint *arg_ct; 1424c896fe29Sbellard 1425c896fe29Sbellard ots = &s->temps[args[0]]; 1426c896fe29Sbellard ts = &s->temps[args[1]]; 1427c896fe29Sbellard arg_ct = &def->args_ct[0]; 1428c896fe29Sbellard 1429e8996ee0Sbellard /* XXX: always mark arg dead if IS_DEAD_IARG(0) */ 1430c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1431c896fe29Sbellard if (IS_DEAD_IARG(0) && !ts->fixed_reg && !ots->fixed_reg) { 1432c896fe29Sbellard /* the mov can be suppressed */ 1433c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) 1434c896fe29Sbellard s->reg_to_temp[ots->reg] = -1; 1435c896fe29Sbellard reg = ts->reg; 1436c896fe29Sbellard s->reg_to_temp[reg] = -1; 1437c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1438c896fe29Sbellard } else { 1439c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) { 1440c896fe29Sbellard reg = ots->reg; 1441c896fe29Sbellard } else { 1442c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); 1443c896fe29Sbellard } 1444c896fe29Sbellard if (ts->reg != reg) { 1445c896fe29Sbellard tcg_out_mov(s, reg, ts->reg); 1446c896fe29Sbellard } 1447c896fe29Sbellard } 1448c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1449c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) { 1450c896fe29Sbellard reg = ots->reg; 1451c896fe29Sbellard } else { 1452c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); 1453c896fe29Sbellard } 1454e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1455c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1456e8996ee0Sbellard if (ots->fixed_reg) { 1457c896fe29Sbellard reg = ots->reg; 1458c896fe29Sbellard tcg_out_movi(s, ots->type, reg, ts->val); 1459c896fe29Sbellard } else { 1460e8996ee0Sbellard /* propagate constant */ 1461e8996ee0Sbellard if (ots->val_type == TEMP_VAL_REG) 1462e8996ee0Sbellard s->reg_to_temp[ots->reg] = -1; 1463e8996ee0Sbellard ots->val_type = TEMP_VAL_CONST; 1464e8996ee0Sbellard ots->val = ts->val; 1465e8996ee0Sbellard return; 1466e8996ee0Sbellard } 1467e8996ee0Sbellard } else { 1468c896fe29Sbellard tcg_abort(); 1469c896fe29Sbellard } 1470c896fe29Sbellard s->reg_to_temp[reg] = args[0]; 1471c896fe29Sbellard ots->reg = reg; 1472c896fe29Sbellard ots->val_type = TEMP_VAL_REG; 1473c896fe29Sbellard ots->mem_coherent = 0; 1474c896fe29Sbellard } 1475c896fe29Sbellard 1476c896fe29Sbellard static void tcg_reg_alloc_op(TCGContext *s, 1477c896fe29Sbellard const TCGOpDef *def, int opc, 1478c896fe29Sbellard const TCGArg *args, 1479c896fe29Sbellard unsigned int dead_iargs) 1480c896fe29Sbellard { 1481c896fe29Sbellard TCGRegSet allocated_regs; 1482c896fe29Sbellard int i, k, nb_iargs, nb_oargs, reg; 1483c896fe29Sbellard TCGArg arg; 1484c896fe29Sbellard const TCGArgConstraint *arg_ct; 1485c896fe29Sbellard TCGTemp *ts; 1486c896fe29Sbellard TCGArg new_args[TCG_MAX_OP_ARGS]; 1487c896fe29Sbellard int const_args[TCG_MAX_OP_ARGS]; 1488c896fe29Sbellard 1489c896fe29Sbellard nb_oargs = def->nb_oargs; 1490c896fe29Sbellard nb_iargs = def->nb_iargs; 1491c896fe29Sbellard 1492c896fe29Sbellard /* copy constants */ 1493c896fe29Sbellard memcpy(new_args + nb_oargs + nb_iargs, 1494c896fe29Sbellard args + nb_oargs + nb_iargs, 1495c896fe29Sbellard sizeof(TCGArg) * def->nb_cargs); 1496c896fe29Sbellard 1497c896fe29Sbellard /* satisfy input constraints */ 1498c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1499c896fe29Sbellard for(k = 0; k < nb_iargs; k++) { 1500c896fe29Sbellard i = def->sorted_args[nb_oargs + k]; 1501c896fe29Sbellard arg = args[i]; 1502c896fe29Sbellard arg_ct = &def->args_ct[i]; 1503c896fe29Sbellard ts = &s->temps[arg]; 1504c896fe29Sbellard if (ts->val_type == TEMP_VAL_MEM) { 1505c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1506e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1507c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1508c896fe29Sbellard ts->reg = reg; 1509c896fe29Sbellard ts->mem_coherent = 1; 1510c896fe29Sbellard s->reg_to_temp[reg] = arg; 1511c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1512c896fe29Sbellard if (tcg_target_const_match(ts->val, arg_ct)) { 1513c896fe29Sbellard /* constant is OK for instruction */ 1514c896fe29Sbellard const_args[i] = 1; 1515c896fe29Sbellard new_args[i] = ts->val; 1516c896fe29Sbellard goto iarg_end; 1517c896fe29Sbellard } else { 1518c896fe29Sbellard /* need to move to a register */ 1519c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1520c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1521e8996ee0Sbellard ts->val_type = TEMP_VAL_REG; 1522e8996ee0Sbellard ts->reg = reg; 1523e8996ee0Sbellard ts->mem_coherent = 0; 1524e8996ee0Sbellard s->reg_to_temp[reg] = arg; 1525c896fe29Sbellard } 1526c896fe29Sbellard } 1527c896fe29Sbellard assert(ts->val_type == TEMP_VAL_REG); 15285ff9d6a4Sbellard if (arg_ct->ct & TCG_CT_IALIAS) { 15295ff9d6a4Sbellard if (ts->fixed_reg) { 15305ff9d6a4Sbellard /* if fixed register, we must allocate a new register 15315ff9d6a4Sbellard if the alias is not the same register */ 15325ff9d6a4Sbellard if (arg != args[arg_ct->alias_index]) 15335ff9d6a4Sbellard goto allocate_in_reg; 15345ff9d6a4Sbellard } else { 1535c896fe29Sbellard /* if the input is aliased to an output and if it is 1536c896fe29Sbellard not dead after the instruction, we must allocate 1537c896fe29Sbellard a new register and move it */ 15385ff9d6a4Sbellard if (!IS_DEAD_IARG(i - nb_oargs)) 1539c896fe29Sbellard goto allocate_in_reg; 1540c896fe29Sbellard } 15415ff9d6a4Sbellard } 1542c896fe29Sbellard reg = ts->reg; 1543c896fe29Sbellard if (tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1544c896fe29Sbellard /* nothing to do : the constraint is satisfied */ 1545c896fe29Sbellard } else { 1546c896fe29Sbellard allocate_in_reg: 1547c896fe29Sbellard /* allocate a new register matching the constraint 1548c896fe29Sbellard and move the temporary register into it */ 1549c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1550c896fe29Sbellard tcg_out_mov(s, reg, ts->reg); 1551c896fe29Sbellard } 1552c896fe29Sbellard new_args[i] = reg; 1553c896fe29Sbellard const_args[i] = 0; 1554c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1555c896fe29Sbellard iarg_end: ; 1556c896fe29Sbellard } 1557c896fe29Sbellard 1558e8996ee0Sbellard if (def->flags & TCG_OPF_BB_END) { 1559e8996ee0Sbellard tcg_reg_alloc_bb_end(s, allocated_regs); 1560e8996ee0Sbellard } else { 1561c896fe29Sbellard /* mark dead temporaries and free the associated registers */ 1562c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 1563c896fe29Sbellard arg = args[nb_oargs + i]; 1564c896fe29Sbellard if (IS_DEAD_IARG(i)) { 1565c896fe29Sbellard ts = &s->temps[arg]; 1566e8996ee0Sbellard if (!ts->fixed_reg) { 1567c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1568c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1569c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1570c896fe29Sbellard } 1571c896fe29Sbellard } 1572c896fe29Sbellard } 1573c896fe29Sbellard 1574c896fe29Sbellard if (def->flags & TCG_OPF_CALL_CLOBBER) { 1575b03cce8eSbellard /* XXX: permit generic clobber register list ? */ 1576c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1577c896fe29Sbellard if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) { 1578c896fe29Sbellard tcg_reg_free(s, reg); 1579c896fe29Sbellard } 1580c896fe29Sbellard } 1581b03cce8eSbellard /* XXX: for load/store we could do that only for the slow path 1582b03cce8eSbellard (i.e. when a memory callback is called) */ 1583b03cce8eSbellard 1584b03cce8eSbellard /* store globals and free associated registers (we assume the insn 1585b03cce8eSbellard can modify any global. */ 1586e8996ee0Sbellard save_globals(s, allocated_regs); 1587c896fe29Sbellard } 1588c896fe29Sbellard 1589c896fe29Sbellard /* satisfy the output constraints */ 1590c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1591c896fe29Sbellard for(k = 0; k < nb_oargs; k++) { 1592c896fe29Sbellard i = def->sorted_args[k]; 1593c896fe29Sbellard arg = args[i]; 1594c896fe29Sbellard arg_ct = &def->args_ct[i]; 1595c896fe29Sbellard ts = &s->temps[arg]; 1596c896fe29Sbellard if (arg_ct->ct & TCG_CT_ALIAS) { 15975ff9d6a4Sbellard reg = new_args[arg_ct->alias_index]; 1598c896fe29Sbellard } else { 1599c896fe29Sbellard /* if fixed register, we try to use it */ 1600c896fe29Sbellard reg = ts->reg; 1601c896fe29Sbellard if (ts->fixed_reg && 1602c896fe29Sbellard tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1603c896fe29Sbellard goto oarg_end; 1604c896fe29Sbellard } 1605c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1606c896fe29Sbellard } 1607c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1608c896fe29Sbellard /* if a fixed register is used, then a move will be done afterwards */ 1609c896fe29Sbellard if (!ts->fixed_reg) { 1610c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1611c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1612c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1613c896fe29Sbellard ts->reg = reg; 1614c896fe29Sbellard /* temp value is modified, so the value kept in memory is 1615c896fe29Sbellard potentially not the same */ 1616c896fe29Sbellard ts->mem_coherent = 0; 1617c896fe29Sbellard s->reg_to_temp[reg] = arg; 1618c896fe29Sbellard } 1619c896fe29Sbellard oarg_end: 1620c896fe29Sbellard new_args[i] = reg; 1621c896fe29Sbellard } 1622e8996ee0Sbellard } 1623c896fe29Sbellard 1624c896fe29Sbellard /* emit instruction */ 1625c896fe29Sbellard tcg_out_op(s, opc, new_args, const_args); 1626c896fe29Sbellard 1627c896fe29Sbellard /* move the outputs in the correct register if needed */ 1628c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1629c896fe29Sbellard ts = &s->temps[args[i]]; 1630c896fe29Sbellard reg = new_args[i]; 1631c896fe29Sbellard if (ts->fixed_reg && ts->reg != reg) { 1632c896fe29Sbellard tcg_out_mov(s, ts->reg, reg); 1633c896fe29Sbellard } 1634c896fe29Sbellard } 1635c896fe29Sbellard } 1636c896fe29Sbellard 1637b03cce8eSbellard #ifdef TCG_TARGET_STACK_GROWSUP 1638b03cce8eSbellard #define STACK_DIR(x) (-(x)) 1639b03cce8eSbellard #else 1640b03cce8eSbellard #define STACK_DIR(x) (x) 1641b03cce8eSbellard #endif 1642b03cce8eSbellard 1643c896fe29Sbellard static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def, 1644c896fe29Sbellard int opc, const TCGArg *args, 1645c896fe29Sbellard unsigned int dead_iargs) 1646c896fe29Sbellard { 1647c896fe29Sbellard int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params; 1648c896fe29Sbellard TCGArg arg, func_arg; 1649c896fe29Sbellard TCGTemp *ts; 1650f54b3f92Saurel32 tcg_target_long stack_offset, call_stack_size, func_addr; 1651b03cce8eSbellard int const_func_arg, allocate_args; 1652c896fe29Sbellard TCGRegSet allocated_regs; 1653c896fe29Sbellard const TCGArgConstraint *arg_ct; 1654c896fe29Sbellard 1655c896fe29Sbellard arg = *args++; 1656c896fe29Sbellard 1657c896fe29Sbellard nb_oargs = arg >> 16; 1658c896fe29Sbellard nb_iargs = arg & 0xffff; 1659c896fe29Sbellard nb_params = nb_iargs - 1; 1660c896fe29Sbellard 1661c896fe29Sbellard flags = args[nb_oargs + nb_iargs]; 1662c896fe29Sbellard 1663c896fe29Sbellard nb_regs = tcg_target_get_call_iarg_regs_count(flags); 1664c896fe29Sbellard if (nb_regs > nb_params) 1665c896fe29Sbellard nb_regs = nb_params; 1666c896fe29Sbellard 1667c896fe29Sbellard /* assign stack slots first */ 1668c896fe29Sbellard /* XXX: preallocate call stack */ 1669c896fe29Sbellard call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long); 1670c896fe29Sbellard call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) & 1671c896fe29Sbellard ~(TCG_TARGET_STACK_ALIGN - 1); 1672b03cce8eSbellard allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE); 1673b03cce8eSbellard if (allocate_args) { 1674b03cce8eSbellard tcg_out_addi(s, TCG_REG_CALL_STACK, -STACK_DIR(call_stack_size)); 1675b03cce8eSbellard } 167639cf05d3Sbellard 167739cf05d3Sbellard stack_offset = TCG_TARGET_CALL_STACK_OFFSET; 1678c896fe29Sbellard for(i = nb_regs; i < nb_params; i++) { 1679c896fe29Sbellard arg = args[nb_oargs + i]; 168039cf05d3Sbellard #ifdef TCG_TARGET_STACK_GROWSUP 168139cf05d3Sbellard stack_offset -= sizeof(tcg_target_long); 168239cf05d3Sbellard #endif 168339cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1684c896fe29Sbellard ts = &s->temps[arg]; 1685c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1686e4d5434cSblueswir1 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset); 1687c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1688c896fe29Sbellard reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 1689c896fe29Sbellard s->reserved_regs); 1690c896fe29Sbellard /* XXX: not correct if reading values from the stack */ 1691e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1692e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset); 1693c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1694c896fe29Sbellard reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 1695c896fe29Sbellard s->reserved_regs); 1696c896fe29Sbellard /* XXX: sign extend may be needed on some targets */ 1697c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1698e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset); 1699c896fe29Sbellard } else { 1700c896fe29Sbellard tcg_abort(); 1701c896fe29Sbellard } 170239cf05d3Sbellard } 170339cf05d3Sbellard #ifndef TCG_TARGET_STACK_GROWSUP 170439cf05d3Sbellard stack_offset += sizeof(tcg_target_long); 170539cf05d3Sbellard #endif 1706c896fe29Sbellard } 1707c896fe29Sbellard 1708c896fe29Sbellard /* assign input registers */ 1709c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1710c896fe29Sbellard for(i = 0; i < nb_regs; i++) { 1711c896fe29Sbellard arg = args[nb_oargs + i]; 171239cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1713c896fe29Sbellard ts = &s->temps[arg]; 1714c896fe29Sbellard reg = tcg_target_call_iarg_regs[i]; 1715c896fe29Sbellard tcg_reg_free(s, reg); 1716c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1717c896fe29Sbellard if (ts->reg != reg) { 1718c896fe29Sbellard tcg_out_mov(s, reg, ts->reg); 1719c896fe29Sbellard } 1720c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1721e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1722c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1723c896fe29Sbellard /* XXX: sign extend ? */ 1724c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1725c896fe29Sbellard } else { 1726c896fe29Sbellard tcg_abort(); 1727c896fe29Sbellard } 1728c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1729c896fe29Sbellard } 173039cf05d3Sbellard } 1731c896fe29Sbellard 1732c896fe29Sbellard /* assign function address */ 1733c896fe29Sbellard func_arg = args[nb_oargs + nb_iargs - 1]; 1734c896fe29Sbellard arg_ct = &def->args_ct[0]; 1735c896fe29Sbellard ts = &s->temps[func_arg]; 1736f54b3f92Saurel32 func_addr = ts->val; 1737c896fe29Sbellard const_func_arg = 0; 1738c896fe29Sbellard if (ts->val_type == TEMP_VAL_MEM) { 1739c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1740e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1741c896fe29Sbellard func_arg = reg; 1742e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1743c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_REG) { 1744c896fe29Sbellard reg = ts->reg; 1745c896fe29Sbellard if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1746c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1747c896fe29Sbellard tcg_out_mov(s, reg, ts->reg); 1748c896fe29Sbellard } 1749c896fe29Sbellard func_arg = reg; 1750e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1751c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1752f54b3f92Saurel32 if (tcg_target_const_match(func_addr, arg_ct)) { 1753c896fe29Sbellard const_func_arg = 1; 1754f54b3f92Saurel32 func_arg = func_addr; 1755c896fe29Sbellard } else { 1756c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1757f54b3f92Saurel32 tcg_out_movi(s, ts->type, reg, func_addr); 1758c896fe29Sbellard func_arg = reg; 1759e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1760c896fe29Sbellard } 1761c896fe29Sbellard } else { 1762c896fe29Sbellard tcg_abort(); 1763c896fe29Sbellard } 1764c896fe29Sbellard 1765e8996ee0Sbellard 1766c896fe29Sbellard /* mark dead temporaries and free the associated registers */ 1767c6e113f5Sbellard for(i = 0; i < nb_iargs; i++) { 1768c896fe29Sbellard arg = args[nb_oargs + i]; 1769c896fe29Sbellard if (IS_DEAD_IARG(i)) { 1770c896fe29Sbellard ts = &s->temps[arg]; 1771e8996ee0Sbellard if (!ts->fixed_reg) { 1772c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1773c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1774c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1775c896fe29Sbellard } 1776c896fe29Sbellard } 1777c896fe29Sbellard } 1778c896fe29Sbellard 1779c896fe29Sbellard /* clobber call registers */ 1780c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1781c896fe29Sbellard if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) { 1782c896fe29Sbellard tcg_reg_free(s, reg); 1783c896fe29Sbellard } 1784c896fe29Sbellard } 1785c896fe29Sbellard 1786c896fe29Sbellard /* store globals and free associated registers (we assume the call 1787c896fe29Sbellard can modify any global. */ 1788e8996ee0Sbellard save_globals(s, allocated_regs); 1789c896fe29Sbellard 1790c896fe29Sbellard tcg_out_op(s, opc, &func_arg, &const_func_arg); 1791c896fe29Sbellard 1792b03cce8eSbellard if (allocate_args) { 1793b03cce8eSbellard tcg_out_addi(s, TCG_REG_CALL_STACK, STACK_DIR(call_stack_size)); 1794b03cce8eSbellard } 1795c896fe29Sbellard 1796c896fe29Sbellard /* assign output registers and emit moves if needed */ 1797c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1798c896fe29Sbellard arg = args[i]; 1799c896fe29Sbellard ts = &s->temps[arg]; 1800c896fe29Sbellard reg = tcg_target_call_oarg_regs[i]; 1801e8996ee0Sbellard assert(s->reg_to_temp[reg] == -1); 1802c896fe29Sbellard if (ts->fixed_reg) { 1803c896fe29Sbellard if (ts->reg != reg) { 1804c896fe29Sbellard tcg_out_mov(s, ts->reg, reg); 1805c896fe29Sbellard } 1806c896fe29Sbellard } else { 1807c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1808c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1809c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1810c896fe29Sbellard ts->reg = reg; 1811c896fe29Sbellard ts->mem_coherent = 0; 1812c896fe29Sbellard s->reg_to_temp[reg] = arg; 1813c896fe29Sbellard } 1814c896fe29Sbellard } 1815c896fe29Sbellard 1816c896fe29Sbellard return nb_iargs + nb_oargs + def->nb_cargs + 1; 1817c896fe29Sbellard } 1818c896fe29Sbellard 1819c896fe29Sbellard #ifdef CONFIG_PROFILER 1820c896fe29Sbellard 1821c896fe29Sbellard static int64_t dyngen_table_op_count[NB_OPS]; 1822c896fe29Sbellard 1823c896fe29Sbellard void dump_op_count(void) 1824c896fe29Sbellard { 1825c896fe29Sbellard int i; 1826c896fe29Sbellard FILE *f; 1827c896fe29Sbellard f = fopen("/tmp/op1.log", "w"); 1828c896fe29Sbellard for(i = 0; i < INDEX_op_end; i++) { 1829c896fe29Sbellard fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]); 1830c896fe29Sbellard } 1831c896fe29Sbellard fclose(f); 1832c896fe29Sbellard f = fopen("/tmp/op2.log", "w"); 1833c896fe29Sbellard for(i = INDEX_op_end; i < NB_OPS; i++) { 1834c896fe29Sbellard fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]); 1835c896fe29Sbellard } 1836c896fe29Sbellard fclose(f); 1837c896fe29Sbellard } 1838c896fe29Sbellard #endif 1839c896fe29Sbellard 1840c896fe29Sbellard 1841c896fe29Sbellard static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf, 18422ba1eeb6Spbrook long search_pc) 1843c896fe29Sbellard { 1844*b314f270Sbellard int opc, op_index; 1845c896fe29Sbellard const TCGOpDef *def; 1846c896fe29Sbellard unsigned int dead_iargs; 1847c896fe29Sbellard const TCGArg *args; 1848c896fe29Sbellard 1849c896fe29Sbellard #ifdef DEBUG_DISAS 1850c896fe29Sbellard if (unlikely(loglevel & CPU_LOG_TB_OP)) { 1851c896fe29Sbellard fprintf(logfile, "OP:\n"); 1852c896fe29Sbellard tcg_dump_ops(s, logfile); 1853c896fe29Sbellard fprintf(logfile, "\n"); 1854c896fe29Sbellard } 1855c896fe29Sbellard #endif 1856c896fe29Sbellard 1857a23a9ec6Sbellard #ifdef CONFIG_PROFILER 1858a23a9ec6Sbellard s->la_time -= profile_getclock(); 1859a23a9ec6Sbellard #endif 1860c896fe29Sbellard tcg_liveness_analysis(s); 1861a23a9ec6Sbellard #ifdef CONFIG_PROFILER 1862a23a9ec6Sbellard s->la_time += profile_getclock(); 1863a23a9ec6Sbellard #endif 1864c896fe29Sbellard 1865c896fe29Sbellard #ifdef DEBUG_DISAS 1866c896fe29Sbellard if (unlikely(loglevel & CPU_LOG_TB_OP_OPT)) { 1867c896fe29Sbellard fprintf(logfile, "OP after la:\n"); 1868c896fe29Sbellard tcg_dump_ops(s, logfile); 1869c896fe29Sbellard fprintf(logfile, "\n"); 1870c896fe29Sbellard } 1871c896fe29Sbellard #endif 1872c896fe29Sbellard 1873c896fe29Sbellard tcg_reg_alloc_start(s); 1874c896fe29Sbellard 1875c896fe29Sbellard s->code_buf = gen_code_buf; 1876c896fe29Sbellard s->code_ptr = gen_code_buf; 1877c896fe29Sbellard 1878c896fe29Sbellard args = gen_opparam_buf; 1879c896fe29Sbellard op_index = 0; 1880b3db8758Sblueswir1 1881c896fe29Sbellard for(;;) { 1882c896fe29Sbellard opc = gen_opc_buf[op_index]; 1883c896fe29Sbellard #ifdef CONFIG_PROFILER 1884c896fe29Sbellard dyngen_table_op_count[opc]++; 1885c896fe29Sbellard #endif 1886c896fe29Sbellard def = &tcg_op_defs[opc]; 1887c896fe29Sbellard #if 0 1888c896fe29Sbellard printf("%s: %d %d %d\n", def->name, 1889c896fe29Sbellard def->nb_oargs, def->nb_iargs, def->nb_cargs); 1890c896fe29Sbellard // dump_regs(s); 1891c896fe29Sbellard #endif 1892c896fe29Sbellard switch(opc) { 1893c896fe29Sbellard case INDEX_op_mov_i32: 1894c896fe29Sbellard #if TCG_TARGET_REG_BITS == 64 1895c896fe29Sbellard case INDEX_op_mov_i64: 1896c896fe29Sbellard #endif 1897c896fe29Sbellard dead_iargs = s->op_dead_iargs[op_index]; 1898c896fe29Sbellard tcg_reg_alloc_mov(s, def, args, dead_iargs); 1899c896fe29Sbellard break; 1900e8996ee0Sbellard case INDEX_op_movi_i32: 1901e8996ee0Sbellard #if TCG_TARGET_REG_BITS == 64 1902e8996ee0Sbellard case INDEX_op_movi_i64: 1903e8996ee0Sbellard #endif 1904e8996ee0Sbellard tcg_reg_alloc_movi(s, args); 1905e8996ee0Sbellard break; 19067e4597d7Sbellard case INDEX_op_debug_insn_start: 19077e4597d7Sbellard /* debug instruction */ 19087e4597d7Sbellard break; 1909c896fe29Sbellard case INDEX_op_nop: 1910c896fe29Sbellard case INDEX_op_nop1: 1911c896fe29Sbellard case INDEX_op_nop2: 1912c896fe29Sbellard case INDEX_op_nop3: 1913c896fe29Sbellard break; 1914c896fe29Sbellard case INDEX_op_nopn: 1915c896fe29Sbellard args += args[0]; 1916c896fe29Sbellard goto next; 19175ff9d6a4Sbellard case INDEX_op_discard: 19185ff9d6a4Sbellard { 19195ff9d6a4Sbellard TCGTemp *ts; 19205ff9d6a4Sbellard ts = &s->temps[args[0]]; 19215ff9d6a4Sbellard /* mark the temporary as dead */ 1922e8996ee0Sbellard if (!ts->fixed_reg) { 19235ff9d6a4Sbellard if (ts->val_type == TEMP_VAL_REG) 19245ff9d6a4Sbellard s->reg_to_temp[ts->reg] = -1; 19255ff9d6a4Sbellard ts->val_type = TEMP_VAL_DEAD; 19265ff9d6a4Sbellard } 19275ff9d6a4Sbellard } 19285ff9d6a4Sbellard break; 1929c896fe29Sbellard case INDEX_op_set_label: 1930e8996ee0Sbellard tcg_reg_alloc_bb_end(s, s->reserved_regs); 1931c896fe29Sbellard tcg_out_label(s, args[0], (long)s->code_ptr); 1932c896fe29Sbellard break; 1933c896fe29Sbellard case INDEX_op_call: 1934c896fe29Sbellard dead_iargs = s->op_dead_iargs[op_index]; 1935c896fe29Sbellard args += tcg_reg_alloc_call(s, def, opc, args, dead_iargs); 1936c896fe29Sbellard goto next; 1937c896fe29Sbellard case INDEX_op_end: 1938c896fe29Sbellard goto the_end; 1939cf2be984Sblueswir1 1940bf6247fbSblueswir1 #ifdef CONFIG_DYNGEN_OP 1941c896fe29Sbellard case 0 ... INDEX_op_end - 1: 1942c896fe29Sbellard /* legacy dyngen ops */ 1943c896fe29Sbellard #ifdef CONFIG_PROFILER 1944a23a9ec6Sbellard s->old_op_count++; 1945c896fe29Sbellard #endif 1946e8996ee0Sbellard tcg_reg_alloc_bb_end(s, s->reserved_regs); 19472ba1eeb6Spbrook if (search_pc >= 0) { 1948c896fe29Sbellard s->code_ptr += def->copy_size; 1949c896fe29Sbellard args += def->nb_args; 1950c896fe29Sbellard } else { 1951c896fe29Sbellard args = dyngen_op(s, opc, args); 1952c896fe29Sbellard } 1953c896fe29Sbellard goto next; 1954cf2be984Sblueswir1 #endif 1955c896fe29Sbellard default: 1956c896fe29Sbellard /* Note: in order to speed up the code, it would be much 1957c896fe29Sbellard faster to have specialized register allocator functions for 1958c896fe29Sbellard some common argument patterns */ 1959c896fe29Sbellard dead_iargs = s->op_dead_iargs[op_index]; 1960c896fe29Sbellard tcg_reg_alloc_op(s, def, opc, args, dead_iargs); 1961c896fe29Sbellard break; 1962c896fe29Sbellard } 1963c896fe29Sbellard args += def->nb_args; 1964c896fe29Sbellard next: ; 19652ba1eeb6Spbrook if (search_pc >= 0 && search_pc < s->code_ptr - gen_code_buf) { 1966c896fe29Sbellard return op_index; 1967c896fe29Sbellard } 1968c896fe29Sbellard op_index++; 1969c896fe29Sbellard #ifndef NDEBUG 1970c896fe29Sbellard check_regs(s); 1971c896fe29Sbellard #endif 1972c896fe29Sbellard } 1973c896fe29Sbellard the_end: 1974c896fe29Sbellard return -1; 1975c896fe29Sbellard } 1976c896fe29Sbellard 1977c896fe29Sbellard int dyngen_code(TCGContext *s, uint8_t *gen_code_buf) 1978c896fe29Sbellard { 1979c896fe29Sbellard #ifdef CONFIG_PROFILER 1980c896fe29Sbellard { 1981c896fe29Sbellard int n; 1982c896fe29Sbellard n = (gen_opc_ptr - gen_opc_buf); 1983a23a9ec6Sbellard s->op_count += n; 1984a23a9ec6Sbellard if (n > s->op_count_max) 1985a23a9ec6Sbellard s->op_count_max = n; 1986a23a9ec6Sbellard 1987a23a9ec6Sbellard s->temp_count += s->nb_temps; 1988a23a9ec6Sbellard if (s->nb_temps > s->temp_count_max) 1989a23a9ec6Sbellard s->temp_count_max = s->nb_temps; 1990c896fe29Sbellard } 1991c896fe29Sbellard #endif 1992c896fe29Sbellard 19932ba1eeb6Spbrook tcg_gen_code_common(s, gen_code_buf, -1); 1994c896fe29Sbellard 1995c896fe29Sbellard /* flush instruction cache */ 1996c896fe29Sbellard flush_icache_range((unsigned long)gen_code_buf, 1997c896fe29Sbellard (unsigned long)s->code_ptr); 1998c896fe29Sbellard return s->code_ptr - gen_code_buf; 1999c896fe29Sbellard } 2000c896fe29Sbellard 20012ba1eeb6Spbrook /* Return the index of the micro operation such as the pc after is < 2002623e265cSpbrook offset bytes from the start of the TB. The contents of gen_code_buf must 2003623e265cSpbrook not be changed, though writing the same values is ok. 2004623e265cSpbrook Return -1 if not found. */ 2005623e265cSpbrook int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset) 2006c896fe29Sbellard { 2007623e265cSpbrook return tcg_gen_code_common(s, gen_code_buf, offset); 2008c896fe29Sbellard } 2009a23a9ec6Sbellard 2010a23a9ec6Sbellard #ifdef CONFIG_PROFILER 2011a23a9ec6Sbellard void tcg_dump_info(FILE *f, 2012a23a9ec6Sbellard int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) 2013a23a9ec6Sbellard { 2014a23a9ec6Sbellard TCGContext *s = &tcg_ctx; 2015a23a9ec6Sbellard int64_t tot; 2016a23a9ec6Sbellard 2017a23a9ec6Sbellard tot = s->interm_time + s->code_time; 2018a23a9ec6Sbellard cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n", 2019a23a9ec6Sbellard tot, tot / 2.4e9); 2020a23a9ec6Sbellard cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n", 2021a23a9ec6Sbellard s->tb_count, 2022a23a9ec6Sbellard s->tb_count1 - s->tb_count, 2023a23a9ec6Sbellard s->tb_count1 ? (double)(s->tb_count1 - s->tb_count) / s->tb_count1 * 100.0 : 0); 2024a23a9ec6Sbellard cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n", 2025a23a9ec6Sbellard s->tb_count ? (double)s->op_count / s->tb_count : 0, s->op_count_max); 2026a23a9ec6Sbellard cpu_fprintf(f, "old ops/total ops %0.1f%%\n", 2027a23a9ec6Sbellard s->op_count ? (double)s->old_op_count / s->op_count * 100.0 : 0); 2028a23a9ec6Sbellard cpu_fprintf(f, "deleted ops/TB %0.2f\n", 2029a23a9ec6Sbellard s->tb_count ? 2030a23a9ec6Sbellard (double)s->del_op_count / s->tb_count : 0); 2031a23a9ec6Sbellard cpu_fprintf(f, "avg temps/TB %0.2f max=%d\n", 2032a23a9ec6Sbellard s->tb_count ? 2033a23a9ec6Sbellard (double)s->temp_count / s->tb_count : 0, 2034a23a9ec6Sbellard s->temp_count_max); 2035a23a9ec6Sbellard 2036a23a9ec6Sbellard cpu_fprintf(f, "cycles/op %0.1f\n", 2037a23a9ec6Sbellard s->op_count ? (double)tot / s->op_count : 0); 2038a23a9ec6Sbellard cpu_fprintf(f, "cycles/in byte %0.1f\n", 2039a23a9ec6Sbellard s->code_in_len ? (double)tot / s->code_in_len : 0); 2040a23a9ec6Sbellard cpu_fprintf(f, "cycles/out byte %0.1f\n", 2041a23a9ec6Sbellard s->code_out_len ? (double)tot / s->code_out_len : 0); 2042a23a9ec6Sbellard if (tot == 0) 2043a23a9ec6Sbellard tot = 1; 2044a23a9ec6Sbellard cpu_fprintf(f, " gen_interm time %0.1f%%\n", 2045a23a9ec6Sbellard (double)s->interm_time / tot * 100.0); 2046a23a9ec6Sbellard cpu_fprintf(f, " gen_code time %0.1f%%\n", 2047a23a9ec6Sbellard (double)s->code_time / tot * 100.0); 2048a23a9ec6Sbellard cpu_fprintf(f, "liveness/code time %0.1f%%\n", 2049a23a9ec6Sbellard (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0); 2050a23a9ec6Sbellard cpu_fprintf(f, "cpu_restore count %" PRId64 "\n", 2051a23a9ec6Sbellard s->restore_count); 2052a23a9ec6Sbellard cpu_fprintf(f, " avg cycles %0.1f\n", 2053a23a9ec6Sbellard s->restore_count ? (double)s->restore_time / s->restore_count : 0); 2054a23a9ec6Sbellard { 2055a23a9ec6Sbellard extern void dump_op_count(void); 2056a23a9ec6Sbellard dump_op_count(); 2057a23a9ec6Sbellard } 2058a23a9ec6Sbellard } 2059a23a9ec6Sbellard #else 206024bf7b3aSbellard void tcg_dump_info(FILE *f, 2061a23a9ec6Sbellard int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) 2062a23a9ec6Sbellard { 206324bf7b3aSbellard cpu_fprintf(f, "[TCG profiler not compiled]\n"); 2064a23a9ec6Sbellard } 2065a23a9ec6Sbellard #endif 2066