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 use liveness analysis (better code) */ 26c896fe29Sbellard #define USE_LIVENESS_ANALYSIS 27c896fe29Sbellard 28cca82982Saurel32 #include "config.h" 29cca82982Saurel32 30a6c6f76cSBlue Swirl #if !defined(CONFIG_DEBUG_TCG) && !defined(NDEBUG) 31cca82982Saurel32 /* define it to suppress various consistency checks (faster) */ 32cca82982Saurel32 #define NDEBUG 33cca82982Saurel32 #endif 34cca82982Saurel32 35c896fe29Sbellard #include <stdarg.h> 36c896fe29Sbellard #include <stdlib.h> 37c896fe29Sbellard #include <stdio.h> 38c896fe29Sbellard #include <string.h> 39c896fe29Sbellard #include <inttypes.h> 403fe43da7Sbellard #ifdef _WIN32 413fe43da7Sbellard #include <malloc.h> 423fe43da7Sbellard #endif 43b29fe3edSmalc #ifdef _AIX 44b29fe3edSmalc #include <alloca.h> 45b29fe3edSmalc #endif 46c896fe29Sbellard 47ca10f867Saurel32 #include "qemu-common.h" 48902b3d5cSmalc #include "cache-utils.h" 49379f6698SPaul Brook #include "host-utils.h" 50c896fe29Sbellard 51c896fe29Sbellard /* Note: the long term plan is to reduce the dependancies on the QEMU 52c896fe29Sbellard CPU definitions. Currently they are used for qemu_ld/st 53c896fe29Sbellard instructions */ 54c896fe29Sbellard #define NO_CPU_IO_DEFS 55c896fe29Sbellard #include "cpu.h" 56c896fe29Sbellard #include "exec-all.h" 57c896fe29Sbellard 58c896fe29Sbellard #include "tcg-op.h" 59c896fe29Sbellard #include "elf.h" 60c896fe29Sbellard 61379f6698SPaul Brook #if defined(CONFIG_USE_GUEST_BASE) && !defined(TCG_TARGET_HAS_GUEST_BASE) 62379f6698SPaul Brook #error GUEST_BASE not supported on this host. 63379f6698SPaul Brook #endif 64c896fe29Sbellard 65c896fe29Sbellard static void patch_reloc(uint8_t *code_ptr, int type, 66f54b3f92Saurel32 tcg_target_long value, tcg_target_long addend); 67c896fe29Sbellard 682edd089fSblueswir1 static TCGOpDef tcg_op_defs[] = { 69c896fe29Sbellard #define DEF(s, n, copy_size) { #s, 0, 0, n, n, 0, copy_size }, 706692b043SAurelien Jarno #define DEF2(s, oargs, iargs, cargs, flags) { #s, oargs, iargs, cargs, iargs + oargs + cargs, flags, 0 }, 71c896fe29Sbellard #include "tcg-opc.h" 72c896fe29Sbellard #undef DEF 73c896fe29Sbellard #undef DEF2 74c896fe29Sbellard }; 75c896fe29Sbellard 76b1d8e52eSblueswir1 static TCGRegSet tcg_target_available_regs[2]; 77b1d8e52eSblueswir1 static TCGRegSet tcg_target_call_clobber_regs; 78c896fe29Sbellard 79c896fe29Sbellard /* XXX: move that inside the context */ 80c896fe29Sbellard uint16_t *gen_opc_ptr; 81c896fe29Sbellard TCGArg *gen_opparam_ptr; 82c896fe29Sbellard 83c896fe29Sbellard static inline void tcg_out8(TCGContext *s, uint8_t v) 84c896fe29Sbellard { 85c896fe29Sbellard *s->code_ptr++ = v; 86c896fe29Sbellard } 87c896fe29Sbellard 88c896fe29Sbellard static inline void tcg_out16(TCGContext *s, uint16_t v) 89c896fe29Sbellard { 90c896fe29Sbellard *(uint16_t *)s->code_ptr = v; 91c896fe29Sbellard s->code_ptr += 2; 92c896fe29Sbellard } 93c896fe29Sbellard 94c896fe29Sbellard static inline void tcg_out32(TCGContext *s, uint32_t v) 95c896fe29Sbellard { 96c896fe29Sbellard *(uint32_t *)s->code_ptr = v; 97c896fe29Sbellard s->code_ptr += 4; 98c896fe29Sbellard } 99c896fe29Sbellard 100c896fe29Sbellard /* label relocation processing */ 101c896fe29Sbellard 102c896fe29Sbellard void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type, 103c896fe29Sbellard int label_index, long addend) 104c896fe29Sbellard { 105c896fe29Sbellard TCGLabel *l; 106c896fe29Sbellard TCGRelocation *r; 107c896fe29Sbellard 108c896fe29Sbellard l = &s->labels[label_index]; 109c896fe29Sbellard if (l->has_value) { 110623e265cSpbrook /* FIXME: This may break relocations on RISC targets that 111623e265cSpbrook modify instruction fields in place. The caller may not have 112623e265cSpbrook written the initial value. */ 113f54b3f92Saurel32 patch_reloc(code_ptr, type, l->u.value, addend); 114c896fe29Sbellard } else { 115c896fe29Sbellard /* add a new relocation entry */ 116c896fe29Sbellard r = tcg_malloc(sizeof(TCGRelocation)); 117c896fe29Sbellard r->type = type; 118c896fe29Sbellard r->ptr = code_ptr; 119c896fe29Sbellard r->addend = addend; 120c896fe29Sbellard r->next = l->u.first_reloc; 121c896fe29Sbellard l->u.first_reloc = r; 122c896fe29Sbellard } 123c896fe29Sbellard } 124c896fe29Sbellard 125c896fe29Sbellard static void tcg_out_label(TCGContext *s, int label_index, 126c896fe29Sbellard tcg_target_long value) 127c896fe29Sbellard { 128c896fe29Sbellard TCGLabel *l; 129c896fe29Sbellard TCGRelocation *r; 130c896fe29Sbellard 131c896fe29Sbellard l = &s->labels[label_index]; 132c896fe29Sbellard if (l->has_value) 133c896fe29Sbellard tcg_abort(); 134c896fe29Sbellard r = l->u.first_reloc; 135c896fe29Sbellard while (r != NULL) { 136f54b3f92Saurel32 patch_reloc(r->ptr, r->type, value, r->addend); 137c896fe29Sbellard r = r->next; 138c896fe29Sbellard } 139c896fe29Sbellard l->has_value = 1; 140c896fe29Sbellard l->u.value = value; 141c896fe29Sbellard } 142c896fe29Sbellard 143c896fe29Sbellard int gen_new_label(void) 144c896fe29Sbellard { 145c896fe29Sbellard TCGContext *s = &tcg_ctx; 146c896fe29Sbellard int idx; 147c896fe29Sbellard TCGLabel *l; 148c896fe29Sbellard 149c896fe29Sbellard if (s->nb_labels >= TCG_MAX_LABELS) 150c896fe29Sbellard tcg_abort(); 151c896fe29Sbellard idx = s->nb_labels++; 152c896fe29Sbellard l = &s->labels[idx]; 153c896fe29Sbellard l->has_value = 0; 154c896fe29Sbellard l->u.first_reloc = NULL; 155c896fe29Sbellard return idx; 156c896fe29Sbellard } 157c896fe29Sbellard 158c896fe29Sbellard #include "tcg-target.c" 159c896fe29Sbellard 160c896fe29Sbellard /* pool based memory allocation */ 161c896fe29Sbellard void *tcg_malloc_internal(TCGContext *s, int size) 162c896fe29Sbellard { 163c896fe29Sbellard TCGPool *p; 164c896fe29Sbellard int pool_size; 165c896fe29Sbellard 166c896fe29Sbellard if (size > TCG_POOL_CHUNK_SIZE) { 167c896fe29Sbellard /* big malloc: insert a new pool (XXX: could optimize) */ 168c896fe29Sbellard p = qemu_malloc(sizeof(TCGPool) + size); 169c896fe29Sbellard p->size = size; 170c896fe29Sbellard if (s->pool_current) 171c896fe29Sbellard s->pool_current->next = p; 172c896fe29Sbellard else 173c896fe29Sbellard s->pool_first = p; 174c896fe29Sbellard p->next = s->pool_current; 175c896fe29Sbellard } else { 176c896fe29Sbellard p = s->pool_current; 177c896fe29Sbellard if (!p) { 178c896fe29Sbellard p = s->pool_first; 179c896fe29Sbellard if (!p) 180c896fe29Sbellard goto new_pool; 181c896fe29Sbellard } else { 182c896fe29Sbellard if (!p->next) { 183c896fe29Sbellard new_pool: 184c896fe29Sbellard pool_size = TCG_POOL_CHUNK_SIZE; 185c896fe29Sbellard p = qemu_malloc(sizeof(TCGPool) + pool_size); 186c896fe29Sbellard p->size = pool_size; 187c896fe29Sbellard p->next = NULL; 188c896fe29Sbellard if (s->pool_current) 189c896fe29Sbellard s->pool_current->next = p; 190c896fe29Sbellard else 191c896fe29Sbellard s->pool_first = p; 192c896fe29Sbellard } else { 193c896fe29Sbellard p = p->next; 194c896fe29Sbellard } 195c896fe29Sbellard } 196c896fe29Sbellard } 197c896fe29Sbellard s->pool_current = p; 198c896fe29Sbellard s->pool_cur = p->data + size; 199c896fe29Sbellard s->pool_end = p->data + p->size; 200c896fe29Sbellard return p->data; 201c896fe29Sbellard } 202c896fe29Sbellard 203c896fe29Sbellard void tcg_pool_reset(TCGContext *s) 204c896fe29Sbellard { 205c896fe29Sbellard s->pool_cur = s->pool_end = NULL; 206c896fe29Sbellard s->pool_current = NULL; 207c896fe29Sbellard } 208c896fe29Sbellard 209c896fe29Sbellard void tcg_context_init(TCGContext *s) 210c896fe29Sbellard { 211c896fe29Sbellard int op, total_args, n; 212c896fe29Sbellard TCGOpDef *def; 213c896fe29Sbellard TCGArgConstraint *args_ct; 214c896fe29Sbellard int *sorted_args; 215c896fe29Sbellard 216c896fe29Sbellard memset(s, 0, sizeof(*s)); 217c896fe29Sbellard s->temps = s->static_temps; 218c896fe29Sbellard s->nb_globals = 0; 219c896fe29Sbellard 220c896fe29Sbellard /* Count total number of arguments and allocate the corresponding 221c896fe29Sbellard space */ 222c896fe29Sbellard total_args = 0; 223c896fe29Sbellard for(op = 0; op < NB_OPS; op++) { 224c896fe29Sbellard def = &tcg_op_defs[op]; 225c896fe29Sbellard n = def->nb_iargs + def->nb_oargs; 226c896fe29Sbellard total_args += n; 227c896fe29Sbellard } 228c896fe29Sbellard 229c896fe29Sbellard args_ct = qemu_malloc(sizeof(TCGArgConstraint) * total_args); 230c896fe29Sbellard sorted_args = qemu_malloc(sizeof(int) * total_args); 231c896fe29Sbellard 232c896fe29Sbellard for(op = 0; op < NB_OPS; op++) { 233c896fe29Sbellard def = &tcg_op_defs[op]; 234c896fe29Sbellard def->args_ct = args_ct; 235c896fe29Sbellard def->sorted_args = sorted_args; 236c896fe29Sbellard n = def->nb_iargs + def->nb_oargs; 237c896fe29Sbellard sorted_args += n; 238c896fe29Sbellard args_ct += n; 239c896fe29Sbellard } 240c896fe29Sbellard 241c896fe29Sbellard tcg_target_init(s); 242b03cce8eSbellard 243b03cce8eSbellard /* init global prologue and epilogue */ 244b03cce8eSbellard s->code_buf = code_gen_prologue; 245b03cce8eSbellard s->code_ptr = s->code_buf; 246b03cce8eSbellard tcg_target_qemu_prologue(s); 247b03cce8eSbellard flush_icache_range((unsigned long)s->code_buf, 248b03cce8eSbellard (unsigned long)s->code_ptr); 249c896fe29Sbellard } 250c896fe29Sbellard 251c896fe29Sbellard void tcg_set_frame(TCGContext *s, int reg, 252c896fe29Sbellard tcg_target_long start, tcg_target_long size) 253c896fe29Sbellard { 254c896fe29Sbellard s->frame_start = start; 255c896fe29Sbellard s->frame_end = start + size; 256c896fe29Sbellard s->frame_reg = reg; 257c896fe29Sbellard } 258c896fe29Sbellard 259c896fe29Sbellard void tcg_func_start(TCGContext *s) 260c896fe29Sbellard { 261e8996ee0Sbellard int i; 262c896fe29Sbellard tcg_pool_reset(s); 263c896fe29Sbellard s->nb_temps = s->nb_globals; 264641d5fbeSbellard for(i = 0; i < (TCG_TYPE_COUNT * 2); i++) 265e8996ee0Sbellard s->first_free_temp[i] = -1; 266c896fe29Sbellard s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS); 267c896fe29Sbellard s->nb_labels = 0; 268c896fe29Sbellard s->current_frame_offset = s->frame_start; 269c896fe29Sbellard 270c896fe29Sbellard gen_opc_ptr = gen_opc_buf; 271c896fe29Sbellard gen_opparam_ptr = gen_opparam_buf; 272c896fe29Sbellard } 273c896fe29Sbellard 274c896fe29Sbellard static inline void tcg_temp_alloc(TCGContext *s, int n) 275c896fe29Sbellard { 276c896fe29Sbellard if (n > TCG_MAX_TEMPS) 277c896fe29Sbellard tcg_abort(); 278c896fe29Sbellard } 279c896fe29Sbellard 280a7812ae4Spbrook static inline int tcg_global_reg_new_internal(TCGType type, int reg, 281a7812ae4Spbrook const char *name) 282c896fe29Sbellard { 283c896fe29Sbellard TCGContext *s = &tcg_ctx; 284c896fe29Sbellard TCGTemp *ts; 285c896fe29Sbellard int idx; 286c896fe29Sbellard 287c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 288c896fe29Sbellard if (type != TCG_TYPE_I32) 289c896fe29Sbellard tcg_abort(); 290c896fe29Sbellard #endif 291c896fe29Sbellard if (tcg_regset_test_reg(s->reserved_regs, reg)) 292c896fe29Sbellard tcg_abort(); 293c896fe29Sbellard idx = s->nb_globals; 294c896fe29Sbellard tcg_temp_alloc(s, s->nb_globals + 1); 295c896fe29Sbellard ts = &s->temps[s->nb_globals]; 296c896fe29Sbellard ts->base_type = type; 297c896fe29Sbellard ts->type = type; 298c896fe29Sbellard ts->fixed_reg = 1; 299c896fe29Sbellard ts->reg = reg; 300c896fe29Sbellard ts->name = name; 301c896fe29Sbellard s->nb_globals++; 302c896fe29Sbellard tcg_regset_set_reg(s->reserved_regs, reg); 303a7812ae4Spbrook return idx; 304a7812ae4Spbrook } 305a7812ae4Spbrook 306a7812ae4Spbrook TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name) 307a7812ae4Spbrook { 308a7812ae4Spbrook int idx; 309a7812ae4Spbrook 310a7812ae4Spbrook idx = tcg_global_reg_new_internal(TCG_TYPE_I32, reg, name); 311a7812ae4Spbrook return MAKE_TCGV_I32(idx); 312a7812ae4Spbrook } 313a7812ae4Spbrook 314a7812ae4Spbrook TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name) 315a7812ae4Spbrook { 316a7812ae4Spbrook int idx; 317a7812ae4Spbrook 318a7812ae4Spbrook idx = tcg_global_reg_new_internal(TCG_TYPE_I64, reg, name); 319a7812ae4Spbrook return MAKE_TCGV_I64(idx); 320c896fe29Sbellard } 321c896fe29Sbellard 322a7812ae4Spbrook static inline int tcg_global_mem_new_internal(TCGType type, int reg, 323a7812ae4Spbrook tcg_target_long offset, 324c896fe29Sbellard const char *name) 325c896fe29Sbellard { 326c896fe29Sbellard TCGContext *s = &tcg_ctx; 327c896fe29Sbellard TCGTemp *ts; 328c896fe29Sbellard int idx; 329c896fe29Sbellard 330c896fe29Sbellard idx = s->nb_globals; 331c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 332c896fe29Sbellard if (type == TCG_TYPE_I64) { 333c896fe29Sbellard char buf[64]; 334c588979bSths tcg_temp_alloc(s, s->nb_globals + 2); 335c896fe29Sbellard ts = &s->temps[s->nb_globals]; 336c896fe29Sbellard ts->base_type = type; 337c896fe29Sbellard ts->type = TCG_TYPE_I32; 338c896fe29Sbellard ts->fixed_reg = 0; 339c896fe29Sbellard ts->mem_allocated = 1; 340c896fe29Sbellard ts->mem_reg = reg; 341c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN 342c896fe29Sbellard ts->mem_offset = offset + 4; 343c896fe29Sbellard #else 344c896fe29Sbellard ts->mem_offset = offset; 345c896fe29Sbellard #endif 346c896fe29Sbellard pstrcpy(buf, sizeof(buf), name); 347c896fe29Sbellard pstrcat(buf, sizeof(buf), "_0"); 348c896fe29Sbellard ts->name = strdup(buf); 349c896fe29Sbellard ts++; 350c896fe29Sbellard 351c896fe29Sbellard ts->base_type = type; 352c896fe29Sbellard ts->type = TCG_TYPE_I32; 353c896fe29Sbellard ts->fixed_reg = 0; 354c896fe29Sbellard ts->mem_allocated = 1; 355c896fe29Sbellard ts->mem_reg = reg; 356c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN 357c896fe29Sbellard ts->mem_offset = offset; 358c896fe29Sbellard #else 359c896fe29Sbellard ts->mem_offset = offset + 4; 360c896fe29Sbellard #endif 361c896fe29Sbellard pstrcpy(buf, sizeof(buf), name); 362c896fe29Sbellard pstrcat(buf, sizeof(buf), "_1"); 363c896fe29Sbellard ts->name = strdup(buf); 364c896fe29Sbellard 365c896fe29Sbellard s->nb_globals += 2; 366c896fe29Sbellard } else 367c896fe29Sbellard #endif 368c896fe29Sbellard { 369c896fe29Sbellard tcg_temp_alloc(s, s->nb_globals + 1); 370c896fe29Sbellard ts = &s->temps[s->nb_globals]; 371c896fe29Sbellard ts->base_type = type; 372c896fe29Sbellard ts->type = type; 373c896fe29Sbellard ts->fixed_reg = 0; 374c896fe29Sbellard ts->mem_allocated = 1; 375c896fe29Sbellard ts->mem_reg = reg; 376c896fe29Sbellard ts->mem_offset = offset; 377c896fe29Sbellard ts->name = name; 378c896fe29Sbellard s->nb_globals++; 379c896fe29Sbellard } 380a7812ae4Spbrook return idx; 381c896fe29Sbellard } 382c896fe29Sbellard 383a7812ae4Spbrook TCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset, 384a7812ae4Spbrook const char *name) 385a7812ae4Spbrook { 386a7812ae4Spbrook int idx; 387a7812ae4Spbrook 388a7812ae4Spbrook idx = tcg_global_mem_new_internal(TCG_TYPE_I32, reg, offset, name); 389a7812ae4Spbrook return MAKE_TCGV_I32(idx); 390a7812ae4Spbrook } 391a7812ae4Spbrook 392a7812ae4Spbrook TCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset, 393a7812ae4Spbrook const char *name) 394a7812ae4Spbrook { 395a7812ae4Spbrook int idx; 396a7812ae4Spbrook 397a7812ae4Spbrook idx = tcg_global_mem_new_internal(TCG_TYPE_I64, reg, offset, name); 398a7812ae4Spbrook return MAKE_TCGV_I64(idx); 399a7812ae4Spbrook } 400a7812ae4Spbrook 401a7812ae4Spbrook static inline int tcg_temp_new_internal(TCGType type, int temp_local) 402c896fe29Sbellard { 403c896fe29Sbellard TCGContext *s = &tcg_ctx; 404c896fe29Sbellard TCGTemp *ts; 405641d5fbeSbellard int idx, k; 406c896fe29Sbellard 407641d5fbeSbellard k = type; 408641d5fbeSbellard if (temp_local) 409641d5fbeSbellard k += TCG_TYPE_COUNT; 410641d5fbeSbellard idx = s->first_free_temp[k]; 411e8996ee0Sbellard if (idx != -1) { 412e8996ee0Sbellard /* There is already an available temp with the 413e8996ee0Sbellard right type */ 414e8996ee0Sbellard ts = &s->temps[idx]; 415641d5fbeSbellard s->first_free_temp[k] = ts->next_free_temp; 416e8996ee0Sbellard ts->temp_allocated = 1; 417641d5fbeSbellard assert(ts->temp_local == temp_local); 418e8996ee0Sbellard } else { 419c896fe29Sbellard idx = s->nb_temps; 420c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 421c896fe29Sbellard if (type == TCG_TYPE_I64) { 4228df1ca4bSths tcg_temp_alloc(s, s->nb_temps + 2); 423c896fe29Sbellard ts = &s->temps[s->nb_temps]; 424c896fe29Sbellard ts->base_type = type; 425c896fe29Sbellard ts->type = TCG_TYPE_I32; 426e8996ee0Sbellard ts->temp_allocated = 1; 427641d5fbeSbellard ts->temp_local = temp_local; 428c896fe29Sbellard ts->name = NULL; 429c896fe29Sbellard ts++; 430c896fe29Sbellard ts->base_type = TCG_TYPE_I32; 431c896fe29Sbellard ts->type = TCG_TYPE_I32; 432e8996ee0Sbellard ts->temp_allocated = 1; 433641d5fbeSbellard ts->temp_local = temp_local; 434c896fe29Sbellard ts->name = NULL; 435c896fe29Sbellard s->nb_temps += 2; 436c896fe29Sbellard } else 437c896fe29Sbellard #endif 438c896fe29Sbellard { 439c896fe29Sbellard tcg_temp_alloc(s, s->nb_temps + 1); 440c896fe29Sbellard ts = &s->temps[s->nb_temps]; 441c896fe29Sbellard ts->base_type = type; 442c896fe29Sbellard ts->type = type; 443e8996ee0Sbellard ts->temp_allocated = 1; 444641d5fbeSbellard ts->temp_local = temp_local; 445c896fe29Sbellard ts->name = NULL; 446c896fe29Sbellard s->nb_temps++; 447c896fe29Sbellard } 448e8996ee0Sbellard } 449a7812ae4Spbrook return idx; 450c896fe29Sbellard } 451c896fe29Sbellard 452a7812ae4Spbrook TCGv_i32 tcg_temp_new_internal_i32(int temp_local) 453a7812ae4Spbrook { 454a7812ae4Spbrook int idx; 455a7812ae4Spbrook 456a7812ae4Spbrook idx = tcg_temp_new_internal(TCG_TYPE_I32, temp_local); 457a7812ae4Spbrook return MAKE_TCGV_I32(idx); 458a7812ae4Spbrook } 459a7812ae4Spbrook 460a7812ae4Spbrook TCGv_i64 tcg_temp_new_internal_i64(int temp_local) 461a7812ae4Spbrook { 462a7812ae4Spbrook int idx; 463a7812ae4Spbrook 464a7812ae4Spbrook idx = tcg_temp_new_internal(TCG_TYPE_I64, temp_local); 465a7812ae4Spbrook return MAKE_TCGV_I64(idx); 466a7812ae4Spbrook } 467a7812ae4Spbrook 468a7812ae4Spbrook static inline void tcg_temp_free_internal(int idx) 469c896fe29Sbellard { 470c896fe29Sbellard TCGContext *s = &tcg_ctx; 471c896fe29Sbellard TCGTemp *ts; 472641d5fbeSbellard int k; 473c896fe29Sbellard 474e8996ee0Sbellard assert(idx >= s->nb_globals && idx < s->nb_temps); 475c896fe29Sbellard ts = &s->temps[idx]; 476e8996ee0Sbellard assert(ts->temp_allocated != 0); 477e8996ee0Sbellard ts->temp_allocated = 0; 478641d5fbeSbellard k = ts->base_type; 479641d5fbeSbellard if (ts->temp_local) 480641d5fbeSbellard k += TCG_TYPE_COUNT; 481641d5fbeSbellard ts->next_free_temp = s->first_free_temp[k]; 482641d5fbeSbellard s->first_free_temp[k] = idx; 483e8996ee0Sbellard } 484e8996ee0Sbellard 485a7812ae4Spbrook void tcg_temp_free_i32(TCGv_i32 arg) 486e8996ee0Sbellard { 487a7812ae4Spbrook tcg_temp_free_internal(GET_TCGV_I32(arg)); 488a7812ae4Spbrook } 489a7812ae4Spbrook 490a7812ae4Spbrook void tcg_temp_free_i64(TCGv_i64 arg) 491a7812ae4Spbrook { 492a7812ae4Spbrook tcg_temp_free_internal(GET_TCGV_I64(arg)); 493a7812ae4Spbrook } 494a7812ae4Spbrook 495a7812ae4Spbrook TCGv_i32 tcg_const_i32(int32_t val) 496a7812ae4Spbrook { 497a7812ae4Spbrook TCGv_i32 t0; 498a7812ae4Spbrook t0 = tcg_temp_new_i32(); 499e8996ee0Sbellard tcg_gen_movi_i32(t0, val); 500e8996ee0Sbellard return t0; 501c896fe29Sbellard } 502c896fe29Sbellard 503a7812ae4Spbrook TCGv_i64 tcg_const_i64(int64_t val) 504c896fe29Sbellard { 505a7812ae4Spbrook TCGv_i64 t0; 506a7812ae4Spbrook t0 = tcg_temp_new_i64(); 507e8996ee0Sbellard tcg_gen_movi_i64(t0, val); 508e8996ee0Sbellard return t0; 509c896fe29Sbellard } 510c896fe29Sbellard 511a7812ae4Spbrook TCGv_i32 tcg_const_local_i32(int32_t val) 512bdffd4a9Saurel32 { 513a7812ae4Spbrook TCGv_i32 t0; 514a7812ae4Spbrook t0 = tcg_temp_local_new_i32(); 515bdffd4a9Saurel32 tcg_gen_movi_i32(t0, val); 516bdffd4a9Saurel32 return t0; 517bdffd4a9Saurel32 } 518bdffd4a9Saurel32 519a7812ae4Spbrook TCGv_i64 tcg_const_local_i64(int64_t val) 520bdffd4a9Saurel32 { 521a7812ae4Spbrook TCGv_i64 t0; 522a7812ae4Spbrook t0 = tcg_temp_local_new_i64(); 523bdffd4a9Saurel32 tcg_gen_movi_i64(t0, val); 524bdffd4a9Saurel32 return t0; 525bdffd4a9Saurel32 } 526bdffd4a9Saurel32 527c896fe29Sbellard void tcg_register_helper(void *func, const char *name) 528c896fe29Sbellard { 529c896fe29Sbellard TCGContext *s = &tcg_ctx; 530c896fe29Sbellard int n; 531c896fe29Sbellard if ((s->nb_helpers + 1) > s->allocated_helpers) { 532c896fe29Sbellard n = s->allocated_helpers; 533c896fe29Sbellard if (n == 0) { 534c896fe29Sbellard n = 4; 535c896fe29Sbellard } else { 536c896fe29Sbellard n *= 2; 537c896fe29Sbellard } 538c896fe29Sbellard s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo)); 539c896fe29Sbellard s->allocated_helpers = n; 540c896fe29Sbellard } 5414dc81f28Sbellard s->helpers[s->nb_helpers].func = (tcg_target_ulong)func; 542c896fe29Sbellard s->helpers[s->nb_helpers].name = name; 543c896fe29Sbellard s->nb_helpers++; 544c896fe29Sbellard } 545c896fe29Sbellard 54639cf05d3Sbellard /* Note: we convert the 64 bit args to 32 bit and do some alignment 54739cf05d3Sbellard and endian swap. Maybe it would be better to do the alignment 54839cf05d3Sbellard and endian swap in tcg_reg_alloc_call(). */ 549a7812ae4Spbrook void tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags, 550a7812ae4Spbrook int sizemask, TCGArg ret, int nargs, TCGArg *args) 551c896fe29Sbellard { 552a7812ae4Spbrook int call_type; 553a7812ae4Spbrook int i; 554a7812ae4Spbrook int real_args; 555a7812ae4Spbrook int nb_rets; 556a7812ae4Spbrook TCGArg *nparam; 557a7812ae4Spbrook *gen_opc_ptr++ = INDEX_op_call; 558a7812ae4Spbrook nparam = gen_opparam_ptr++; 559c896fe29Sbellard call_type = (flags & TCG_CALL_TYPE_MASK); 560a7812ae4Spbrook if (ret != TCG_CALL_DUMMY_ARG) { 561a7812ae4Spbrook #if TCG_TARGET_REG_BITS < 64 562a7812ae4Spbrook if (sizemask & 1) { 563a7812ae4Spbrook #ifdef TCG_TARGET_WORDS_BIGENDIAN 564a7812ae4Spbrook *gen_opparam_ptr++ = ret + 1; 565a7812ae4Spbrook *gen_opparam_ptr++ = ret; 566a7812ae4Spbrook #else 567a7812ae4Spbrook *gen_opparam_ptr++ = ret; 568a7812ae4Spbrook *gen_opparam_ptr++ = ret + 1; 569a7812ae4Spbrook #endif 570a7812ae4Spbrook nb_rets = 2; 571a7812ae4Spbrook } else 572a7812ae4Spbrook #endif 573a7812ae4Spbrook { 574a7812ae4Spbrook *gen_opparam_ptr++ = ret; 575a7812ae4Spbrook nb_rets = 1; 576a7812ae4Spbrook } 577a7812ae4Spbrook } else { 578a7812ae4Spbrook nb_rets = 0; 579a7812ae4Spbrook } 580a7812ae4Spbrook real_args = 0; 581a7812ae4Spbrook for (i = 0; i < nargs; i++) { 582a7812ae4Spbrook #if TCG_TARGET_REG_BITS < 64 583a7812ae4Spbrook if (sizemask & (2 << i)) { 584c896fe29Sbellard #ifdef TCG_TARGET_I386 585c896fe29Sbellard /* REGPARM case: if the third parameter is 64 bit, it is 586c896fe29Sbellard allocated on the stack */ 587a7812ae4Spbrook if (i == 2 && call_type == TCG_CALL_TYPE_REGPARM) { 588c896fe29Sbellard call_type = TCG_CALL_TYPE_REGPARM_2; 589c896fe29Sbellard flags = (flags & ~TCG_CALL_TYPE_MASK) | call_type; 590c896fe29Sbellard } 591a7812ae4Spbrook #endif 59239cf05d3Sbellard #ifdef TCG_TARGET_CALL_ALIGN_ARGS 59339cf05d3Sbellard /* some targets want aligned 64 bit args */ 594ebd486d5Smalc if (real_args & 1) { 595a7812ae4Spbrook *gen_opparam_ptr++ = TCG_CALL_DUMMY_ARG; 596ebd486d5Smalc real_args++; 59739cf05d3Sbellard } 59839cf05d3Sbellard #endif 5993f90f252SRichard Henderson /* If stack grows up, then we will be placing successive 6003f90f252SRichard Henderson arguments at lower addresses, which means we need to 6013f90f252SRichard Henderson reverse the order compared to how we would normally 6023f90f252SRichard Henderson treat either big or little-endian. For those arguments 6033f90f252SRichard Henderson that will wind up in registers, this still works for 6043f90f252SRichard Henderson HPPA (the only current STACK_GROWSUP target) since the 6053f90f252SRichard Henderson argument registers are *also* allocated in decreasing 6063f90f252SRichard Henderson order. If another such target is added, this logic may 6073f90f252SRichard Henderson have to get more complicated to differentiate between 6083f90f252SRichard Henderson stack arguments and register arguments. */ 6093f90f252SRichard Henderson #if defined(TCG_TARGET_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP) 610a7812ae4Spbrook *gen_opparam_ptr++ = args[i] + 1; 611a7812ae4Spbrook *gen_opparam_ptr++ = args[i]; 612c896fe29Sbellard #else 613a7812ae4Spbrook *gen_opparam_ptr++ = args[i]; 614a7812ae4Spbrook *gen_opparam_ptr++ = args[i] + 1; 615c896fe29Sbellard #endif 616a7812ae4Spbrook real_args += 2; 617a7812ae4Spbrook } else 618c896fe29Sbellard #endif 619c896fe29Sbellard { 620a7812ae4Spbrook *gen_opparam_ptr++ = args[i]; 621a7812ae4Spbrook real_args++; 622c896fe29Sbellard } 623a7812ae4Spbrook } 624a7812ae4Spbrook *gen_opparam_ptr++ = GET_TCGV_PTR(func); 625a7812ae4Spbrook 626a7812ae4Spbrook *gen_opparam_ptr++ = flags; 627a7812ae4Spbrook 628a7812ae4Spbrook *nparam = (nb_rets << 16) | (real_args + 1); 629a7812ae4Spbrook 630a7812ae4Spbrook /* total parameters, needed to go backward in the instruction stream */ 631a7812ae4Spbrook *gen_opparam_ptr++ = 1 + nb_rets + real_args + 3; 632a7812ae4Spbrook } 633c896fe29Sbellard 634ac56dd48Spbrook #if TCG_TARGET_REG_BITS == 32 635a7812ae4Spbrook void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1, 636c896fe29Sbellard int c, int right, int arith) 637c896fe29Sbellard { 638cf60bce4Sbellard if (c == 0) { 639a7812ae4Spbrook tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg1)); 640cf60bce4Sbellard tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1)); 641cf60bce4Sbellard } else if (c >= 32) { 642c896fe29Sbellard c -= 32; 643c896fe29Sbellard if (right) { 644c896fe29Sbellard if (arith) { 645a7812ae4Spbrook tcg_gen_sari_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c); 646ac56dd48Spbrook tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31); 647c896fe29Sbellard } else { 648a7812ae4Spbrook tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c); 649ac56dd48Spbrook tcg_gen_movi_i32(TCGV_HIGH(ret), 0); 650c896fe29Sbellard } 651c896fe29Sbellard } else { 652a7812ae4Spbrook tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_LOW(arg1), c); 653a7812ae4Spbrook tcg_gen_movi_i32(TCGV_LOW(ret), 0); 654c896fe29Sbellard } 655c896fe29Sbellard } else { 656a7812ae4Spbrook TCGv_i32 t0, t1; 657c896fe29Sbellard 658a7812ae4Spbrook t0 = tcg_temp_new_i32(); 659a7812ae4Spbrook t1 = tcg_temp_new_i32(); 660c896fe29Sbellard if (right) { 661ac56dd48Spbrook tcg_gen_shli_i32(t0, TCGV_HIGH(arg1), 32 - c); 662c896fe29Sbellard if (arith) 663ac56dd48Spbrook tcg_gen_sari_i32(t1, TCGV_HIGH(arg1), c); 664c896fe29Sbellard else 665ac56dd48Spbrook tcg_gen_shri_i32(t1, TCGV_HIGH(arg1), c); 666a7812ae4Spbrook tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_LOW(arg1), c); 667a7812ae4Spbrook tcg_gen_or_i32(TCGV_LOW(ret), TCGV_LOW(ret), t0); 668ac56dd48Spbrook tcg_gen_mov_i32(TCGV_HIGH(ret), t1); 669c896fe29Sbellard } else { 670a7812ae4Spbrook tcg_gen_shri_i32(t0, TCGV_LOW(arg1), 32 - c); 671c896fe29Sbellard /* Note: ret can be the same as arg1, so we use t1 */ 672a7812ae4Spbrook tcg_gen_shli_i32(t1, TCGV_LOW(arg1), c); 673ac56dd48Spbrook tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c); 674ac56dd48Spbrook tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t0); 675a7812ae4Spbrook tcg_gen_mov_i32(TCGV_LOW(ret), t1); 676c896fe29Sbellard } 677a7812ae4Spbrook tcg_temp_free_i32(t0); 678a7812ae4Spbrook tcg_temp_free_i32(t1); 679c896fe29Sbellard } 680c896fe29Sbellard } 681ac56dd48Spbrook #endif 682c896fe29Sbellard 683be210acbSRichard Henderson 6848fcd3692Sblueswir1 static void tcg_reg_alloc_start(TCGContext *s) 685c896fe29Sbellard { 686c896fe29Sbellard int i; 687c896fe29Sbellard TCGTemp *ts; 688c896fe29Sbellard for(i = 0; i < s->nb_globals; i++) { 689c896fe29Sbellard ts = &s->temps[i]; 690c896fe29Sbellard if (ts->fixed_reg) { 691c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 692c896fe29Sbellard } else { 693c896fe29Sbellard ts->val_type = TEMP_VAL_MEM; 694c896fe29Sbellard } 695c896fe29Sbellard } 696e8996ee0Sbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 697e8996ee0Sbellard ts = &s->temps[i]; 698e8996ee0Sbellard ts->val_type = TEMP_VAL_DEAD; 699e8996ee0Sbellard ts->mem_allocated = 0; 700e8996ee0Sbellard ts->fixed_reg = 0; 701e8996ee0Sbellard } 702c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 703c896fe29Sbellard s->reg_to_temp[i] = -1; 704c896fe29Sbellard } 705c896fe29Sbellard } 706c896fe29Sbellard 707ac56dd48Spbrook static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size, 708ac56dd48Spbrook int idx) 709c896fe29Sbellard { 710c896fe29Sbellard TCGTemp *ts; 711ac56dd48Spbrook 712ac56dd48Spbrook ts = &s->temps[idx]; 713ac56dd48Spbrook if (idx < s->nb_globals) { 714ac56dd48Spbrook pstrcpy(buf, buf_size, ts->name); 715c896fe29Sbellard } else { 716641d5fbeSbellard if (ts->temp_local) 717641d5fbeSbellard snprintf(buf, buf_size, "loc%d", idx - s->nb_globals); 718641d5fbeSbellard else 719ac56dd48Spbrook snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals); 720c896fe29Sbellard } 721c896fe29Sbellard return buf; 722c896fe29Sbellard } 723c896fe29Sbellard 724a7812ae4Spbrook char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg) 725ac56dd48Spbrook { 726a7812ae4Spbrook return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I32(arg)); 727a7812ae4Spbrook } 728a7812ae4Spbrook 729a7812ae4Spbrook char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg) 730a7812ae4Spbrook { 731a810a2deSblueswir1 return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I64(arg)); 732ac56dd48Spbrook } 733ac56dd48Spbrook 734e8996ee0Sbellard static int helper_cmp(const void *p1, const void *p2) 735e8996ee0Sbellard { 736e8996ee0Sbellard const TCGHelperInfo *th1 = p1; 737e8996ee0Sbellard const TCGHelperInfo *th2 = p2; 738e8996ee0Sbellard if (th1->func < th2->func) 739e8996ee0Sbellard return -1; 740e8996ee0Sbellard else if (th1->func == th2->func) 741e8996ee0Sbellard return 0; 742e8996ee0Sbellard else 743e8996ee0Sbellard return 1; 744e8996ee0Sbellard } 745e8996ee0Sbellard 746e8996ee0Sbellard /* find helper definition (Note: A hash table would be better) */ 7474dc81f28Sbellard static TCGHelperInfo *tcg_find_helper(TCGContext *s, tcg_target_ulong val) 7484dc81f28Sbellard { 749e8996ee0Sbellard int m, m_min, m_max; 750e8996ee0Sbellard TCGHelperInfo *th; 751e8996ee0Sbellard tcg_target_ulong v; 752e8996ee0Sbellard 753e8996ee0Sbellard if (unlikely(!s->helpers_sorted)) { 754e8996ee0Sbellard qsort(s->helpers, s->nb_helpers, sizeof(TCGHelperInfo), 755e8996ee0Sbellard helper_cmp); 756e8996ee0Sbellard s->helpers_sorted = 1; 757e8996ee0Sbellard } 758e8996ee0Sbellard 759e8996ee0Sbellard /* binary search */ 760e8996ee0Sbellard m_min = 0; 761e8996ee0Sbellard m_max = s->nb_helpers - 1; 762e8996ee0Sbellard while (m_min <= m_max) { 763e8996ee0Sbellard m = (m_min + m_max) >> 1; 764e8996ee0Sbellard th = &s->helpers[m]; 765e8996ee0Sbellard v = th->func; 766e8996ee0Sbellard if (v == val) 767e8996ee0Sbellard return th; 768e8996ee0Sbellard else if (val < v) { 769e8996ee0Sbellard m_max = m - 1; 770e8996ee0Sbellard } else { 771e8996ee0Sbellard m_min = m + 1; 772e8996ee0Sbellard } 7734dc81f28Sbellard } 7744dc81f28Sbellard return NULL; 7754dc81f28Sbellard } 7764dc81f28Sbellard 777f48f3edeSblueswir1 static const char * const cond_name[] = 778f48f3edeSblueswir1 { 779f48f3edeSblueswir1 [TCG_COND_EQ] = "eq", 780f48f3edeSblueswir1 [TCG_COND_NE] = "ne", 781f48f3edeSblueswir1 [TCG_COND_LT] = "lt", 782f48f3edeSblueswir1 [TCG_COND_GE] = "ge", 783f48f3edeSblueswir1 [TCG_COND_LE] = "le", 784f48f3edeSblueswir1 [TCG_COND_GT] = "gt", 785f48f3edeSblueswir1 [TCG_COND_LTU] = "ltu", 786f48f3edeSblueswir1 [TCG_COND_GEU] = "geu", 787f48f3edeSblueswir1 [TCG_COND_LEU] = "leu", 788f48f3edeSblueswir1 [TCG_COND_GTU] = "gtu" 789f48f3edeSblueswir1 }; 790f48f3edeSblueswir1 791c896fe29Sbellard void tcg_dump_ops(TCGContext *s, FILE *outfile) 792c896fe29Sbellard { 793c896fe29Sbellard const uint16_t *opc_ptr; 794c896fe29Sbellard const TCGArg *args; 795c896fe29Sbellard TCGArg arg; 796a9751609SRichard Henderson TCGOpcode c; 797a9751609SRichard Henderson int i, k, nb_oargs, nb_iargs, nb_cargs, first_insn; 798c896fe29Sbellard const TCGOpDef *def; 799c896fe29Sbellard char buf[128]; 800c896fe29Sbellard 8017e4597d7Sbellard first_insn = 1; 802c896fe29Sbellard opc_ptr = gen_opc_buf; 803c896fe29Sbellard args = gen_opparam_buf; 804c896fe29Sbellard while (opc_ptr < gen_opc_ptr) { 805c896fe29Sbellard c = *opc_ptr++; 806c896fe29Sbellard def = &tcg_op_defs[c]; 8077e4597d7Sbellard if (c == INDEX_op_debug_insn_start) { 8087e4597d7Sbellard uint64_t pc; 8097e4597d7Sbellard #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS 8107e4597d7Sbellard pc = ((uint64_t)args[1] << 32) | args[0]; 8117e4597d7Sbellard #else 8127e4597d7Sbellard pc = args[0]; 8137e4597d7Sbellard #endif 8147e4597d7Sbellard if (!first_insn) 8157e4597d7Sbellard fprintf(outfile, "\n"); 8167e4597d7Sbellard fprintf(outfile, " ---- 0x%" PRIx64, pc); 8177e4597d7Sbellard first_insn = 0; 8187e4597d7Sbellard nb_oargs = def->nb_oargs; 8197e4597d7Sbellard nb_iargs = def->nb_iargs; 8207e4597d7Sbellard nb_cargs = def->nb_cargs; 8217e4597d7Sbellard } else if (c == INDEX_op_call) { 822c896fe29Sbellard TCGArg arg; 8234dc81f28Sbellard 824c896fe29Sbellard /* variable number of arguments */ 825c896fe29Sbellard arg = *args++; 826c896fe29Sbellard nb_oargs = arg >> 16; 827c896fe29Sbellard nb_iargs = arg & 0xffff; 828c896fe29Sbellard nb_cargs = def->nb_cargs; 829b03cce8eSbellard 8307e4597d7Sbellard fprintf(outfile, " %s ", def->name); 8317e4597d7Sbellard 832b03cce8eSbellard /* function name */ 833b03cce8eSbellard fprintf(outfile, "%s", 834e8996ee0Sbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + nb_iargs - 1])); 835b03cce8eSbellard /* flags */ 836b03cce8eSbellard fprintf(outfile, ",$0x%" TCG_PRIlx, 837b03cce8eSbellard args[nb_oargs + nb_iargs]); 838b03cce8eSbellard /* nb out args */ 839b03cce8eSbellard fprintf(outfile, ",$%d", nb_oargs); 840b03cce8eSbellard for(i = 0; i < nb_oargs; i++) { 841b03cce8eSbellard fprintf(outfile, ","); 842b03cce8eSbellard fprintf(outfile, "%s", 843b03cce8eSbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[i])); 844b03cce8eSbellard } 845b03cce8eSbellard for(i = 0; i < (nb_iargs - 1); i++) { 846b03cce8eSbellard fprintf(outfile, ","); 84739cf05d3Sbellard if (args[nb_oargs + i] == TCG_CALL_DUMMY_ARG) { 84839cf05d3Sbellard fprintf(outfile, "<dummy>"); 84939cf05d3Sbellard } else { 850b03cce8eSbellard fprintf(outfile, "%s", 851b03cce8eSbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + i])); 852b03cce8eSbellard } 85339cf05d3Sbellard } 854e8996ee0Sbellard } else if (c == INDEX_op_movi_i32 855e8996ee0Sbellard #if TCG_TARGET_REG_BITS == 64 856e8996ee0Sbellard || c == INDEX_op_movi_i64 857e8996ee0Sbellard #endif 858e8996ee0Sbellard ) { 859e8996ee0Sbellard tcg_target_ulong val; 860e8996ee0Sbellard TCGHelperInfo *th; 861e8996ee0Sbellard 862e8996ee0Sbellard nb_oargs = def->nb_oargs; 863e8996ee0Sbellard nb_iargs = def->nb_iargs; 864e8996ee0Sbellard nb_cargs = def->nb_cargs; 865e8996ee0Sbellard fprintf(outfile, " %s %s,$", def->name, 866e8996ee0Sbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[0])); 867e8996ee0Sbellard val = args[1]; 868e8996ee0Sbellard th = tcg_find_helper(s, val); 869e8996ee0Sbellard if (th) { 8703e9a474eSaurel32 fprintf(outfile, "%s", th->name); 871e8996ee0Sbellard } else { 872e8996ee0Sbellard if (c == INDEX_op_movi_i32) 873e8996ee0Sbellard fprintf(outfile, "0x%x", (uint32_t)val); 874e8996ee0Sbellard else 875e8996ee0Sbellard fprintf(outfile, "0x%" PRIx64 , (uint64_t)val); 876e8996ee0Sbellard } 877b03cce8eSbellard } else { 8787e4597d7Sbellard fprintf(outfile, " %s ", def->name); 879b03cce8eSbellard if (c == INDEX_op_nopn) { 880c896fe29Sbellard /* variable number of arguments */ 881c896fe29Sbellard nb_cargs = *args; 882c896fe29Sbellard nb_oargs = 0; 883c896fe29Sbellard nb_iargs = 0; 884c896fe29Sbellard } else { 885c896fe29Sbellard nb_oargs = def->nb_oargs; 886c896fe29Sbellard nb_iargs = def->nb_iargs; 887c896fe29Sbellard nb_cargs = def->nb_cargs; 888c896fe29Sbellard } 889c896fe29Sbellard 890c896fe29Sbellard k = 0; 891c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 892c896fe29Sbellard if (k != 0) 893c896fe29Sbellard fprintf(outfile, ","); 894ac56dd48Spbrook fprintf(outfile, "%s", 895ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++])); 896c896fe29Sbellard } 897c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 898c896fe29Sbellard if (k != 0) 899c896fe29Sbellard fprintf(outfile, ","); 900ac56dd48Spbrook fprintf(outfile, "%s", 901ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++])); 902c896fe29Sbellard } 903be210acbSRichard Henderson switch (c) { 904be210acbSRichard Henderson case INDEX_op_brcond_i32: 905f48f3edeSblueswir1 #if TCG_TARGET_REG_BITS == 32 906be210acbSRichard Henderson case INDEX_op_brcond2_i32: 907f48f3edeSblueswir1 #elif TCG_TARGET_REG_BITS == 64 908be210acbSRichard Henderson case INDEX_op_brcond_i64: 909f48f3edeSblueswir1 #endif 910be210acbSRichard Henderson case INDEX_op_setcond_i32: 911be210acbSRichard Henderson #if TCG_TARGET_REG_BITS == 32 912be210acbSRichard Henderson case INDEX_op_setcond2_i32: 913be210acbSRichard Henderson #elif TCG_TARGET_REG_BITS == 64 914be210acbSRichard Henderson case INDEX_op_setcond_i64: 915be210acbSRichard Henderson #endif 916f48f3edeSblueswir1 if (args[k] < ARRAY_SIZE(cond_name) && cond_name[args[k]]) 917f48f3edeSblueswir1 fprintf(outfile, ",%s", cond_name[args[k++]]); 918f48f3edeSblueswir1 else 919f48f3edeSblueswir1 fprintf(outfile, ",$0x%" TCG_PRIlx, args[k++]); 920f48f3edeSblueswir1 i = 1; 921be210acbSRichard Henderson break; 922be210acbSRichard Henderson default: 923f48f3edeSblueswir1 i = 0; 924be210acbSRichard Henderson break; 925be210acbSRichard Henderson } 926f48f3edeSblueswir1 for(; i < nb_cargs; i++) { 927c896fe29Sbellard if (k != 0) 928c896fe29Sbellard fprintf(outfile, ","); 929c896fe29Sbellard arg = args[k++]; 930c896fe29Sbellard fprintf(outfile, "$0x%" TCG_PRIlx, arg); 931c896fe29Sbellard } 932b03cce8eSbellard } 933c896fe29Sbellard fprintf(outfile, "\n"); 934c896fe29Sbellard args += nb_iargs + nb_oargs + nb_cargs; 935c896fe29Sbellard } 936c896fe29Sbellard } 937c896fe29Sbellard 938c896fe29Sbellard /* we give more priority to constraints with less registers */ 939c896fe29Sbellard static int get_constraint_priority(const TCGOpDef *def, int k) 940c896fe29Sbellard { 941c896fe29Sbellard const TCGArgConstraint *arg_ct; 942c896fe29Sbellard 943c896fe29Sbellard int i, n; 944c896fe29Sbellard arg_ct = &def->args_ct[k]; 945c896fe29Sbellard if (arg_ct->ct & TCG_CT_ALIAS) { 946c896fe29Sbellard /* an alias is equivalent to a single register */ 947c896fe29Sbellard n = 1; 948c896fe29Sbellard } else { 949c896fe29Sbellard if (!(arg_ct->ct & TCG_CT_REG)) 950c896fe29Sbellard return 0; 951c896fe29Sbellard n = 0; 952c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 953c896fe29Sbellard if (tcg_regset_test_reg(arg_ct->u.regs, i)) 954c896fe29Sbellard n++; 955c896fe29Sbellard } 956c896fe29Sbellard } 957c896fe29Sbellard return TCG_TARGET_NB_REGS - n + 1; 958c896fe29Sbellard } 959c896fe29Sbellard 960c896fe29Sbellard /* sort from highest priority to lowest */ 961c896fe29Sbellard static void sort_constraints(TCGOpDef *def, int start, int n) 962c896fe29Sbellard { 963c896fe29Sbellard int i, j, p1, p2, tmp; 964c896fe29Sbellard 965c896fe29Sbellard for(i = 0; i < n; i++) 966c896fe29Sbellard def->sorted_args[start + i] = start + i; 967c896fe29Sbellard if (n <= 1) 968c896fe29Sbellard return; 969c896fe29Sbellard for(i = 0; i < n - 1; i++) { 970c896fe29Sbellard for(j = i + 1; j < n; j++) { 971c896fe29Sbellard p1 = get_constraint_priority(def, def->sorted_args[start + i]); 972c896fe29Sbellard p2 = get_constraint_priority(def, def->sorted_args[start + j]); 973c896fe29Sbellard if (p1 < p2) { 974c896fe29Sbellard tmp = def->sorted_args[start + i]; 975c896fe29Sbellard def->sorted_args[start + i] = def->sorted_args[start + j]; 976c896fe29Sbellard def->sorted_args[start + j] = tmp; 977c896fe29Sbellard } 978c896fe29Sbellard } 979c896fe29Sbellard } 980c896fe29Sbellard } 981c896fe29Sbellard 982c896fe29Sbellard void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs) 983c896fe29Sbellard { 984a9751609SRichard Henderson TCGOpcode op; 985c896fe29Sbellard TCGOpDef *def; 986c896fe29Sbellard const char *ct_str; 987c896fe29Sbellard int i, nb_args; 988c896fe29Sbellard 989c896fe29Sbellard for(;;) { 990a9751609SRichard Henderson if (tdefs->op == (TCGOpcode)-1) 991c896fe29Sbellard break; 992c896fe29Sbellard op = tdefs->op; 993c896fe29Sbellard assert(op >= 0 && op < NB_OPS); 994c896fe29Sbellard def = &tcg_op_defs[op]; 995c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG) 996c68aaa18SStefan Weil /* Duplicate entry in op definitions? */ 997c68aaa18SStefan Weil assert(!def->used); 998c68aaa18SStefan Weil def->used = 1; 999c68aaa18SStefan Weil #endif 1000c896fe29Sbellard nb_args = def->nb_iargs + def->nb_oargs; 1001c896fe29Sbellard for(i = 0; i < nb_args; i++) { 1002c896fe29Sbellard ct_str = tdefs->args_ct_str[i]; 1003c68aaa18SStefan Weil /* Incomplete TCGTargetOpDef entry? */ 1004c68aaa18SStefan Weil assert(ct_str != NULL); 1005c896fe29Sbellard tcg_regset_clear(def->args_ct[i].u.regs); 1006c896fe29Sbellard def->args_ct[i].ct = 0; 1007c896fe29Sbellard if (ct_str[0] >= '0' && ct_str[0] <= '9') { 1008c896fe29Sbellard int oarg; 1009c896fe29Sbellard oarg = ct_str[0] - '0'; 1010c896fe29Sbellard assert(oarg < def->nb_oargs); 1011c896fe29Sbellard assert(def->args_ct[oarg].ct & TCG_CT_REG); 1012c896fe29Sbellard /* TCG_CT_ALIAS is for the output arguments. The input 10135ff9d6a4Sbellard argument is tagged with TCG_CT_IALIAS. */ 1014c896fe29Sbellard def->args_ct[i] = def->args_ct[oarg]; 10155ff9d6a4Sbellard def->args_ct[oarg].ct = TCG_CT_ALIAS; 10165ff9d6a4Sbellard def->args_ct[oarg].alias_index = i; 1017c896fe29Sbellard def->args_ct[i].ct |= TCG_CT_IALIAS; 10185ff9d6a4Sbellard def->args_ct[i].alias_index = oarg; 1019c896fe29Sbellard } else { 1020c896fe29Sbellard for(;;) { 1021c896fe29Sbellard if (*ct_str == '\0') 1022c896fe29Sbellard break; 1023c896fe29Sbellard switch(*ct_str) { 1024c896fe29Sbellard case 'i': 1025c896fe29Sbellard def->args_ct[i].ct |= TCG_CT_CONST; 1026c896fe29Sbellard ct_str++; 1027c896fe29Sbellard break; 1028c896fe29Sbellard default: 1029c896fe29Sbellard if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) { 1030c896fe29Sbellard fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n", 1031c896fe29Sbellard ct_str, i, def->name); 1032c896fe29Sbellard exit(1); 1033c896fe29Sbellard } 1034c896fe29Sbellard } 1035c896fe29Sbellard } 1036c896fe29Sbellard } 1037c896fe29Sbellard } 1038c896fe29Sbellard 1039c68aaa18SStefan Weil /* TCGTargetOpDef entry with too much information? */ 1040c68aaa18SStefan Weil assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL); 1041c68aaa18SStefan Weil 1042c896fe29Sbellard /* sort the constraints (XXX: this is just an heuristic) */ 1043c896fe29Sbellard sort_constraints(def, 0, def->nb_oargs); 1044c896fe29Sbellard sort_constraints(def, def->nb_oargs, def->nb_iargs); 1045c896fe29Sbellard 1046c896fe29Sbellard #if 0 1047c896fe29Sbellard { 1048c896fe29Sbellard int i; 1049c896fe29Sbellard 1050c896fe29Sbellard printf("%s: sorted=", def->name); 1051c896fe29Sbellard for(i = 0; i < def->nb_oargs + def->nb_iargs; i++) 1052c896fe29Sbellard printf(" %d", def->sorted_args[i]); 1053c896fe29Sbellard printf("\n"); 1054c896fe29Sbellard } 1055c896fe29Sbellard #endif 1056c896fe29Sbellard tdefs++; 1057c896fe29Sbellard } 1058c896fe29Sbellard 1059c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG) 1060a9751609SRichard Henderson i = 0; 1061c68aaa18SStefan Weil for (op = 0; op < ARRAY_SIZE(tcg_op_defs); op++) { 1062c68aaa18SStefan Weil if (op < INDEX_op_call || op == INDEX_op_debug_insn_start) { 1063c68aaa18SStefan Weil /* Wrong entry in op definitions? */ 1064a9751609SRichard Henderson if (tcg_op_defs[op].used) { 1065a9751609SRichard Henderson fprintf(stderr, "Invalid op definition for %s\n", 1066a9751609SRichard Henderson tcg_op_defs[op].name); 1067a9751609SRichard Henderson i = 1; 1068a9751609SRichard Henderson } 1069c68aaa18SStefan Weil } else { 1070c68aaa18SStefan Weil /* Missing entry in op definitions? */ 1071a9751609SRichard Henderson if (!tcg_op_defs[op].used) { 1072a9751609SRichard Henderson fprintf(stderr, "Missing op definition for %s\n", 1073a9751609SRichard Henderson tcg_op_defs[op].name); 1074a9751609SRichard Henderson i = 1; 1075c68aaa18SStefan Weil } 1076c68aaa18SStefan Weil } 1077a9751609SRichard Henderson } 1078a9751609SRichard Henderson if (i == 1) { 1079a9751609SRichard Henderson tcg_abort(); 1080a9751609SRichard Henderson } 1081c68aaa18SStefan Weil #endif 1082c896fe29Sbellard } 1083c896fe29Sbellard 1084c896fe29Sbellard #ifdef USE_LIVENESS_ANALYSIS 1085c896fe29Sbellard 1086c896fe29Sbellard /* set a nop for an operation using 'nb_args' */ 1087c896fe29Sbellard static inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr, 1088c896fe29Sbellard TCGArg *args, int nb_args) 1089c896fe29Sbellard { 1090c896fe29Sbellard if (nb_args == 0) { 1091c896fe29Sbellard *opc_ptr = INDEX_op_nop; 1092c896fe29Sbellard } else { 1093c896fe29Sbellard *opc_ptr = INDEX_op_nopn; 1094c896fe29Sbellard args[0] = nb_args; 1095c896fe29Sbellard args[nb_args - 1] = nb_args; 1096c896fe29Sbellard } 1097c896fe29Sbellard } 1098c896fe29Sbellard 1099641d5fbeSbellard /* liveness analysis: end of function: globals are live, temps are 1100641d5fbeSbellard dead. */ 1101641d5fbeSbellard /* XXX: at this stage, not used as there would be little gains because 1102641d5fbeSbellard most TBs end with a conditional jump. */ 1103641d5fbeSbellard static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps) 1104c896fe29Sbellard { 1105c896fe29Sbellard memset(dead_temps, 0, s->nb_globals); 1106c896fe29Sbellard memset(dead_temps + s->nb_globals, 1, s->nb_temps - s->nb_globals); 1107c896fe29Sbellard } 1108c896fe29Sbellard 1109641d5fbeSbellard /* liveness analysis: end of basic block: globals are live, temps are 1110641d5fbeSbellard dead, local temps are live. */ 1111641d5fbeSbellard static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps) 1112641d5fbeSbellard { 1113641d5fbeSbellard int i; 1114641d5fbeSbellard TCGTemp *ts; 1115641d5fbeSbellard 1116641d5fbeSbellard memset(dead_temps, 0, s->nb_globals); 1117641d5fbeSbellard ts = &s->temps[s->nb_globals]; 1118641d5fbeSbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 1119641d5fbeSbellard if (ts->temp_local) 1120641d5fbeSbellard dead_temps[i] = 0; 1121641d5fbeSbellard else 1122641d5fbeSbellard dead_temps[i] = 1; 1123641d5fbeSbellard ts++; 1124641d5fbeSbellard } 1125641d5fbeSbellard } 1126641d5fbeSbellard 1127c896fe29Sbellard /* Liveness analysis : update the opc_dead_iargs array to tell if a 1128c896fe29Sbellard given input arguments is dead. Instructions updating dead 1129c896fe29Sbellard temporaries are removed. */ 11308fcd3692Sblueswir1 static void tcg_liveness_analysis(TCGContext *s) 1131c896fe29Sbellard { 1132a9751609SRichard Henderson int i, op_index, nb_args, nb_iargs, nb_oargs, arg, nb_ops; 1133a9751609SRichard Henderson TCGOpcode op; 1134c896fe29Sbellard TCGArg *args; 1135c896fe29Sbellard const TCGOpDef *def; 1136c896fe29Sbellard uint8_t *dead_temps; 1137c896fe29Sbellard unsigned int dead_iargs; 1138c896fe29Sbellard 1139c896fe29Sbellard gen_opc_ptr++; /* skip end */ 1140c896fe29Sbellard 1141c896fe29Sbellard nb_ops = gen_opc_ptr - gen_opc_buf; 1142c896fe29Sbellard 114394f4af02SAurelien Jarno s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t)); 1144c896fe29Sbellard 1145c896fe29Sbellard dead_temps = tcg_malloc(s->nb_temps); 1146c896fe29Sbellard memset(dead_temps, 1, s->nb_temps); 1147c896fe29Sbellard 1148c896fe29Sbellard args = gen_opparam_ptr; 1149c896fe29Sbellard op_index = nb_ops - 1; 1150c896fe29Sbellard while (op_index >= 0) { 1151c896fe29Sbellard op = gen_opc_buf[op_index]; 1152c896fe29Sbellard def = &tcg_op_defs[op]; 1153c896fe29Sbellard switch(op) { 1154c896fe29Sbellard case INDEX_op_call: 1155c6e113f5Sbellard { 1156c6e113f5Sbellard int call_flags; 1157c6e113f5Sbellard 1158c896fe29Sbellard nb_args = args[-1]; 1159c896fe29Sbellard args -= nb_args; 1160c896fe29Sbellard nb_iargs = args[0] & 0xffff; 1161c896fe29Sbellard nb_oargs = args[0] >> 16; 1162c896fe29Sbellard args++; 1163c6e113f5Sbellard call_flags = args[nb_oargs + nb_iargs]; 1164c6e113f5Sbellard 1165c6e113f5Sbellard /* pure functions can be removed if their result is not 1166c6e113f5Sbellard used */ 1167c6e113f5Sbellard if (call_flags & TCG_CALL_PURE) { 1168c6e113f5Sbellard for(i = 0; i < nb_oargs; i++) { 1169c6e113f5Sbellard arg = args[i]; 1170c6e113f5Sbellard if (!dead_temps[arg]) 1171c6e113f5Sbellard goto do_not_remove_call; 1172c6e113f5Sbellard } 1173c6e113f5Sbellard tcg_set_nop(s, gen_opc_buf + op_index, 1174c6e113f5Sbellard args - 1, nb_args); 1175c6e113f5Sbellard } else { 1176c6e113f5Sbellard do_not_remove_call: 1177c896fe29Sbellard 1178c896fe29Sbellard /* output args are dead */ 1179c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1180c896fe29Sbellard arg = args[i]; 1181c896fe29Sbellard dead_temps[arg] = 1; 1182c896fe29Sbellard } 1183c896fe29Sbellard 1184b9c18f56Saurel32 if (!(call_flags & TCG_CALL_CONST)) { 1185c896fe29Sbellard /* globals are live (they may be used by the call) */ 1186c896fe29Sbellard memset(dead_temps, 0, s->nb_globals); 1187b9c18f56Saurel32 } 1188c896fe29Sbellard 1189c896fe29Sbellard /* input args are live */ 1190c896fe29Sbellard dead_iargs = 0; 1191c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 1192c896fe29Sbellard arg = args[i + nb_oargs]; 119339cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1194c896fe29Sbellard if (dead_temps[arg]) { 1195c896fe29Sbellard dead_iargs |= (1 << i); 1196c896fe29Sbellard } 1197c896fe29Sbellard dead_temps[arg] = 0; 1198c896fe29Sbellard } 119939cf05d3Sbellard } 1200c896fe29Sbellard s->op_dead_iargs[op_index] = dead_iargs; 1201c6e113f5Sbellard } 1202c896fe29Sbellard args--; 1203c6e113f5Sbellard } 1204c896fe29Sbellard break; 1205c896fe29Sbellard case INDEX_op_set_label: 1206c896fe29Sbellard args--; 1207c896fe29Sbellard /* mark end of basic block */ 1208c896fe29Sbellard tcg_la_bb_end(s, dead_temps); 1209c896fe29Sbellard break; 12107e4597d7Sbellard case INDEX_op_debug_insn_start: 12117e4597d7Sbellard args -= def->nb_args; 12127e4597d7Sbellard break; 1213c896fe29Sbellard case INDEX_op_nopn: 1214c896fe29Sbellard nb_args = args[-1]; 1215c896fe29Sbellard args -= nb_args; 1216c896fe29Sbellard break; 12175ff9d6a4Sbellard case INDEX_op_discard: 12185ff9d6a4Sbellard args--; 12195ff9d6a4Sbellard /* mark the temporary as dead */ 12205ff9d6a4Sbellard dead_temps[args[0]] = 1; 12215ff9d6a4Sbellard break; 1222c896fe29Sbellard case INDEX_op_end: 1223c896fe29Sbellard break; 1224c896fe29Sbellard /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */ 1225c896fe29Sbellard default: 1226c896fe29Sbellard args -= def->nb_args; 1227c896fe29Sbellard nb_iargs = def->nb_iargs; 1228c896fe29Sbellard nb_oargs = def->nb_oargs; 1229c896fe29Sbellard 1230c896fe29Sbellard /* Test if the operation can be removed because all 12315ff9d6a4Sbellard its outputs are dead. We assume that nb_oargs == 0 12325ff9d6a4Sbellard implies side effects */ 12335ff9d6a4Sbellard if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) { 1234c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1235c896fe29Sbellard arg = args[i]; 1236c896fe29Sbellard if (!dead_temps[arg]) 1237c896fe29Sbellard goto do_not_remove; 1238c896fe29Sbellard } 1239c896fe29Sbellard tcg_set_nop(s, gen_opc_buf + op_index, args, def->nb_args); 1240c896fe29Sbellard #ifdef CONFIG_PROFILER 1241a23a9ec6Sbellard s->del_op_count++; 1242c896fe29Sbellard #endif 1243c896fe29Sbellard } else { 1244c896fe29Sbellard do_not_remove: 1245c896fe29Sbellard 1246c896fe29Sbellard /* output args are dead */ 1247c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1248c896fe29Sbellard arg = args[i]; 1249c896fe29Sbellard dead_temps[arg] = 1; 1250c896fe29Sbellard } 1251c896fe29Sbellard 1252c896fe29Sbellard /* if end of basic block, update */ 1253c896fe29Sbellard if (def->flags & TCG_OPF_BB_END) { 1254c896fe29Sbellard tcg_la_bb_end(s, dead_temps); 1255b03cce8eSbellard } else if (def->flags & TCG_OPF_CALL_CLOBBER) { 1256b03cce8eSbellard /* globals are live */ 1257b03cce8eSbellard memset(dead_temps, 0, s->nb_globals); 1258c896fe29Sbellard } 1259c896fe29Sbellard 1260c896fe29Sbellard /* input args are live */ 1261c896fe29Sbellard dead_iargs = 0; 1262c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 1263c896fe29Sbellard arg = args[i + nb_oargs]; 1264c896fe29Sbellard if (dead_temps[arg]) { 1265c896fe29Sbellard dead_iargs |= (1 << i); 1266c896fe29Sbellard } 1267c896fe29Sbellard dead_temps[arg] = 0; 1268c896fe29Sbellard } 1269c896fe29Sbellard s->op_dead_iargs[op_index] = dead_iargs; 1270c896fe29Sbellard } 1271c896fe29Sbellard break; 1272c896fe29Sbellard } 1273c896fe29Sbellard op_index--; 1274c896fe29Sbellard } 1275c896fe29Sbellard 1276c896fe29Sbellard if (args != gen_opparam_buf) 1277c896fe29Sbellard tcg_abort(); 1278c896fe29Sbellard } 1279c896fe29Sbellard #else 1280c896fe29Sbellard /* dummy liveness analysis */ 1281*655feed5Smalc static void tcg_liveness_analysis(TCGContext *s) 1282c896fe29Sbellard { 1283c896fe29Sbellard int nb_ops; 1284c896fe29Sbellard nb_ops = gen_opc_ptr - gen_opc_buf; 1285c896fe29Sbellard 1286c896fe29Sbellard s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t)); 1287c896fe29Sbellard memset(s->op_dead_iargs, 0, nb_ops * sizeof(uint16_t)); 1288c896fe29Sbellard } 1289c896fe29Sbellard #endif 1290c896fe29Sbellard 1291c896fe29Sbellard #ifndef NDEBUG 1292c896fe29Sbellard static void dump_regs(TCGContext *s) 1293c896fe29Sbellard { 1294c896fe29Sbellard TCGTemp *ts; 1295c896fe29Sbellard int i; 1296c896fe29Sbellard char buf[64]; 1297c896fe29Sbellard 1298c896fe29Sbellard for(i = 0; i < s->nb_temps; i++) { 1299c896fe29Sbellard ts = &s->temps[i]; 1300ac56dd48Spbrook printf(" %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i)); 1301c896fe29Sbellard switch(ts->val_type) { 1302c896fe29Sbellard case TEMP_VAL_REG: 1303c896fe29Sbellard printf("%s", tcg_target_reg_names[ts->reg]); 1304c896fe29Sbellard break; 1305c896fe29Sbellard case TEMP_VAL_MEM: 1306c896fe29Sbellard printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]); 1307c896fe29Sbellard break; 1308c896fe29Sbellard case TEMP_VAL_CONST: 1309c896fe29Sbellard printf("$0x%" TCG_PRIlx, ts->val); 1310c896fe29Sbellard break; 1311c896fe29Sbellard case TEMP_VAL_DEAD: 1312c896fe29Sbellard printf("D"); 1313c896fe29Sbellard break; 1314c896fe29Sbellard default: 1315c896fe29Sbellard printf("???"); 1316c896fe29Sbellard break; 1317c896fe29Sbellard } 1318c896fe29Sbellard printf("\n"); 1319c896fe29Sbellard } 1320c896fe29Sbellard 1321c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 1322c896fe29Sbellard if (s->reg_to_temp[i] >= 0) { 1323c896fe29Sbellard printf("%s: %s\n", 1324c896fe29Sbellard tcg_target_reg_names[i], 1325ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i])); 1326c896fe29Sbellard } 1327c896fe29Sbellard } 1328c896fe29Sbellard } 1329c896fe29Sbellard 1330c896fe29Sbellard static void check_regs(TCGContext *s) 1331c896fe29Sbellard { 1332c896fe29Sbellard int reg, k; 1333c896fe29Sbellard TCGTemp *ts; 1334c896fe29Sbellard char buf[64]; 1335c896fe29Sbellard 1336c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1337c896fe29Sbellard k = s->reg_to_temp[reg]; 1338c896fe29Sbellard if (k >= 0) { 1339c896fe29Sbellard ts = &s->temps[k]; 1340c896fe29Sbellard if (ts->val_type != TEMP_VAL_REG || 1341c896fe29Sbellard ts->reg != reg) { 1342c896fe29Sbellard printf("Inconsistency for register %s:\n", 1343c896fe29Sbellard tcg_target_reg_names[reg]); 1344b03cce8eSbellard goto fail; 1345c896fe29Sbellard } 1346c896fe29Sbellard } 1347c896fe29Sbellard } 1348c896fe29Sbellard for(k = 0; k < s->nb_temps; k++) { 1349c896fe29Sbellard ts = &s->temps[k]; 1350c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG && 1351c896fe29Sbellard !ts->fixed_reg && 1352c896fe29Sbellard s->reg_to_temp[ts->reg] != k) { 1353c896fe29Sbellard printf("Inconsistency for temp %s:\n", 1354ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), k)); 1355b03cce8eSbellard fail: 1356c896fe29Sbellard printf("reg state:\n"); 1357c896fe29Sbellard dump_regs(s); 1358c896fe29Sbellard tcg_abort(); 1359c896fe29Sbellard } 1360c896fe29Sbellard } 1361c896fe29Sbellard } 1362c896fe29Sbellard #endif 1363c896fe29Sbellard 1364c896fe29Sbellard static void temp_allocate_frame(TCGContext *s, int temp) 1365c896fe29Sbellard { 1366c896fe29Sbellard TCGTemp *ts; 1367c896fe29Sbellard ts = &s->temps[temp]; 1368c896fe29Sbellard s->current_frame_offset = (s->current_frame_offset + sizeof(tcg_target_long) - 1) & ~(sizeof(tcg_target_long) - 1); 1369c896fe29Sbellard if (s->current_frame_offset + sizeof(tcg_target_long) > s->frame_end) 13705ff9d6a4Sbellard tcg_abort(); 1371c896fe29Sbellard ts->mem_offset = s->current_frame_offset; 1372c896fe29Sbellard ts->mem_reg = s->frame_reg; 1373c896fe29Sbellard ts->mem_allocated = 1; 1374c896fe29Sbellard s->current_frame_offset += sizeof(tcg_target_long); 1375c896fe29Sbellard } 1376c896fe29Sbellard 1377c896fe29Sbellard /* free register 'reg' by spilling the corresponding temporary if necessary */ 1378c896fe29Sbellard static void tcg_reg_free(TCGContext *s, int reg) 1379c896fe29Sbellard { 1380c896fe29Sbellard TCGTemp *ts; 1381c896fe29Sbellard int temp; 1382c896fe29Sbellard 1383c896fe29Sbellard temp = s->reg_to_temp[reg]; 1384c896fe29Sbellard if (temp != -1) { 1385c896fe29Sbellard ts = &s->temps[temp]; 1386c896fe29Sbellard assert(ts->val_type == TEMP_VAL_REG); 1387c896fe29Sbellard if (!ts->mem_coherent) { 1388c896fe29Sbellard if (!ts->mem_allocated) 1389c896fe29Sbellard temp_allocate_frame(s, temp); 1390e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1391c896fe29Sbellard } 1392c896fe29Sbellard ts->val_type = TEMP_VAL_MEM; 1393c896fe29Sbellard s->reg_to_temp[reg] = -1; 1394c896fe29Sbellard } 1395c896fe29Sbellard } 1396c896fe29Sbellard 1397c896fe29Sbellard /* Allocate a register belonging to reg1 & ~reg2 */ 1398c896fe29Sbellard static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2) 1399c896fe29Sbellard { 1400c896fe29Sbellard int i, reg; 1401c896fe29Sbellard TCGRegSet reg_ct; 1402c896fe29Sbellard 1403c896fe29Sbellard tcg_regset_andnot(reg_ct, reg1, reg2); 1404c896fe29Sbellard 1405c896fe29Sbellard /* first try free registers */ 14060954d0d9Sblueswir1 for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { 1407c896fe29Sbellard reg = tcg_target_reg_alloc_order[i]; 1408c896fe29Sbellard if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1) 1409c896fe29Sbellard return reg; 1410c896fe29Sbellard } 1411c896fe29Sbellard 1412c896fe29Sbellard /* XXX: do better spill choice */ 14130954d0d9Sblueswir1 for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { 1414c896fe29Sbellard reg = tcg_target_reg_alloc_order[i]; 1415c896fe29Sbellard if (tcg_regset_test_reg(reg_ct, reg)) { 1416c896fe29Sbellard tcg_reg_free(s, reg); 1417c896fe29Sbellard return reg; 1418c896fe29Sbellard } 1419c896fe29Sbellard } 1420c896fe29Sbellard 1421c896fe29Sbellard tcg_abort(); 1422c896fe29Sbellard } 1423c896fe29Sbellard 1424641d5fbeSbellard /* save a temporary to memory. 'allocated_regs' is used in case a 1425e8996ee0Sbellard temporary registers needs to be allocated to store a constant. */ 1426641d5fbeSbellard static void temp_save(TCGContext *s, int temp, TCGRegSet allocated_regs) 1427c896fe29Sbellard { 1428c896fe29Sbellard TCGTemp *ts; 1429641d5fbeSbellard int reg; 1430c896fe29Sbellard 1431641d5fbeSbellard ts = &s->temps[temp]; 1432c896fe29Sbellard if (!ts->fixed_reg) { 1433e8996ee0Sbellard switch(ts->val_type) { 1434e8996ee0Sbellard case TEMP_VAL_REG: 1435c896fe29Sbellard tcg_reg_free(s, ts->reg); 1436e8996ee0Sbellard break; 1437e8996ee0Sbellard case TEMP_VAL_DEAD: 1438e5097dc8Sbellard ts->val_type = TEMP_VAL_MEM; 1439e8996ee0Sbellard break; 1440e8996ee0Sbellard case TEMP_VAL_CONST: 14416692b043SAurelien Jarno reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 14426692b043SAurelien Jarno allocated_regs); 1443d6859202SAurelien Jarno if (!ts->mem_allocated) 1444d6859202SAurelien Jarno temp_allocate_frame(s, temp); 1445e8996ee0Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1446e8996ee0Sbellard tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1447e8996ee0Sbellard ts->val_type = TEMP_VAL_MEM; 1448e8996ee0Sbellard break; 1449e8996ee0Sbellard case TEMP_VAL_MEM: 1450e8996ee0Sbellard break; 1451e8996ee0Sbellard default: 1452e8996ee0Sbellard tcg_abort(); 1453c896fe29Sbellard } 1454c896fe29Sbellard } 1455c896fe29Sbellard } 1456641d5fbeSbellard 1457641d5fbeSbellard /* save globals to their cannonical location and assume they can be 1458641d5fbeSbellard modified be the following code. 'allocated_regs' is used in case a 1459641d5fbeSbellard temporary registers needs to be allocated to store a constant. */ 1460641d5fbeSbellard static void save_globals(TCGContext *s, TCGRegSet allocated_regs) 1461641d5fbeSbellard { 1462641d5fbeSbellard int i; 1463641d5fbeSbellard 1464641d5fbeSbellard for(i = 0; i < s->nb_globals; i++) { 1465641d5fbeSbellard temp_save(s, i, allocated_regs); 1466641d5fbeSbellard } 1467e5097dc8Sbellard } 1468e5097dc8Sbellard 1469e5097dc8Sbellard /* at the end of a basic block, we assume all temporaries are dead and 1470e8996ee0Sbellard all globals are stored at their canonical location. */ 1471e8996ee0Sbellard static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs) 1472e5097dc8Sbellard { 1473e5097dc8Sbellard TCGTemp *ts; 1474e5097dc8Sbellard int i; 1475e5097dc8Sbellard 1476c896fe29Sbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 1477c896fe29Sbellard ts = &s->temps[i]; 1478641d5fbeSbellard if (ts->temp_local) { 1479641d5fbeSbellard temp_save(s, i, allocated_regs); 1480641d5fbeSbellard } else { 1481c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1482c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1483c896fe29Sbellard } 1484c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1485c896fe29Sbellard } 1486641d5fbeSbellard } 1487e8996ee0Sbellard 1488e8996ee0Sbellard save_globals(s, allocated_regs); 1489c896fe29Sbellard } 1490c896fe29Sbellard 1491c896fe29Sbellard #define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1) 1492c896fe29Sbellard 1493e8996ee0Sbellard static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args) 1494e8996ee0Sbellard { 1495e8996ee0Sbellard TCGTemp *ots; 1496e8996ee0Sbellard tcg_target_ulong val; 1497e8996ee0Sbellard 1498e8996ee0Sbellard ots = &s->temps[args[0]]; 1499e8996ee0Sbellard val = args[1]; 1500e8996ee0Sbellard 1501e8996ee0Sbellard if (ots->fixed_reg) { 1502e8996ee0Sbellard /* for fixed registers, we do not do any constant 1503e8996ee0Sbellard propagation */ 1504e8996ee0Sbellard tcg_out_movi(s, ots->type, ots->reg, val); 1505e8996ee0Sbellard } else { 15061235fc06Sths /* The movi is not explicitly generated here */ 1507e8996ee0Sbellard if (ots->val_type == TEMP_VAL_REG) 1508e8996ee0Sbellard s->reg_to_temp[ots->reg] = -1; 1509e8996ee0Sbellard ots->val_type = TEMP_VAL_CONST; 1510e8996ee0Sbellard ots->val = val; 1511e8996ee0Sbellard } 1512e8996ee0Sbellard } 1513e8996ee0Sbellard 1514c896fe29Sbellard static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def, 1515c896fe29Sbellard const TCGArg *args, 1516c896fe29Sbellard unsigned int dead_iargs) 1517c896fe29Sbellard { 1518c896fe29Sbellard TCGTemp *ts, *ots; 1519c896fe29Sbellard int reg; 1520c896fe29Sbellard const TCGArgConstraint *arg_ct; 1521c896fe29Sbellard 1522c896fe29Sbellard ots = &s->temps[args[0]]; 1523c896fe29Sbellard ts = &s->temps[args[1]]; 1524c896fe29Sbellard arg_ct = &def->args_ct[0]; 1525c896fe29Sbellard 1526e8996ee0Sbellard /* XXX: always mark arg dead if IS_DEAD_IARG(0) */ 1527c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1528c896fe29Sbellard if (IS_DEAD_IARG(0) && !ts->fixed_reg && !ots->fixed_reg) { 1529c896fe29Sbellard /* the mov can be suppressed */ 1530c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) 1531c896fe29Sbellard s->reg_to_temp[ots->reg] = -1; 1532c896fe29Sbellard reg = ts->reg; 1533c896fe29Sbellard s->reg_to_temp[reg] = -1; 1534c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1535c896fe29Sbellard } else { 1536c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) { 1537c896fe29Sbellard reg = ots->reg; 1538c896fe29Sbellard } else { 1539c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); 1540c896fe29Sbellard } 1541c896fe29Sbellard if (ts->reg != reg) { 1542c896fe29Sbellard tcg_out_mov(s, reg, ts->reg); 1543c896fe29Sbellard } 1544c896fe29Sbellard } 1545c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1546c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) { 1547c896fe29Sbellard reg = ots->reg; 1548c896fe29Sbellard } else { 1549c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); 1550c896fe29Sbellard } 1551e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1552c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1553e8996ee0Sbellard if (ots->fixed_reg) { 1554c896fe29Sbellard reg = ots->reg; 1555c896fe29Sbellard tcg_out_movi(s, ots->type, reg, ts->val); 1556c896fe29Sbellard } else { 1557e8996ee0Sbellard /* propagate constant */ 1558e8996ee0Sbellard if (ots->val_type == TEMP_VAL_REG) 1559e8996ee0Sbellard s->reg_to_temp[ots->reg] = -1; 1560e8996ee0Sbellard ots->val_type = TEMP_VAL_CONST; 1561e8996ee0Sbellard ots->val = ts->val; 1562e8996ee0Sbellard return; 1563e8996ee0Sbellard } 1564e8996ee0Sbellard } else { 1565c896fe29Sbellard tcg_abort(); 1566c896fe29Sbellard } 1567c896fe29Sbellard s->reg_to_temp[reg] = args[0]; 1568c896fe29Sbellard ots->reg = reg; 1569c896fe29Sbellard ots->val_type = TEMP_VAL_REG; 1570c896fe29Sbellard ots->mem_coherent = 0; 1571c896fe29Sbellard } 1572c896fe29Sbellard 1573c896fe29Sbellard static void tcg_reg_alloc_op(TCGContext *s, 1574a9751609SRichard Henderson const TCGOpDef *def, TCGOpcode opc, 1575c896fe29Sbellard const TCGArg *args, 1576c896fe29Sbellard unsigned int dead_iargs) 1577c896fe29Sbellard { 1578c896fe29Sbellard TCGRegSet allocated_regs; 1579c896fe29Sbellard int i, k, nb_iargs, nb_oargs, reg; 1580c896fe29Sbellard TCGArg arg; 1581c896fe29Sbellard const TCGArgConstraint *arg_ct; 1582c896fe29Sbellard TCGTemp *ts; 1583c896fe29Sbellard TCGArg new_args[TCG_MAX_OP_ARGS]; 1584c896fe29Sbellard int const_args[TCG_MAX_OP_ARGS]; 1585c896fe29Sbellard 1586c896fe29Sbellard nb_oargs = def->nb_oargs; 1587c896fe29Sbellard nb_iargs = def->nb_iargs; 1588c896fe29Sbellard 1589c896fe29Sbellard /* copy constants */ 1590c896fe29Sbellard memcpy(new_args + nb_oargs + nb_iargs, 1591c896fe29Sbellard args + nb_oargs + nb_iargs, 1592c896fe29Sbellard sizeof(TCGArg) * def->nb_cargs); 1593c896fe29Sbellard 1594c896fe29Sbellard /* satisfy input constraints */ 1595c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1596c896fe29Sbellard for(k = 0; k < nb_iargs; k++) { 1597c896fe29Sbellard i = def->sorted_args[nb_oargs + k]; 1598c896fe29Sbellard arg = args[i]; 1599c896fe29Sbellard arg_ct = &def->args_ct[i]; 1600c896fe29Sbellard ts = &s->temps[arg]; 1601c896fe29Sbellard if (ts->val_type == TEMP_VAL_MEM) { 1602c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1603e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1604c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1605c896fe29Sbellard ts->reg = reg; 1606c896fe29Sbellard ts->mem_coherent = 1; 1607c896fe29Sbellard s->reg_to_temp[reg] = arg; 1608c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1609c896fe29Sbellard if (tcg_target_const_match(ts->val, arg_ct)) { 1610c896fe29Sbellard /* constant is OK for instruction */ 1611c896fe29Sbellard const_args[i] = 1; 1612c896fe29Sbellard new_args[i] = ts->val; 1613c896fe29Sbellard goto iarg_end; 1614c896fe29Sbellard } else { 1615c896fe29Sbellard /* need to move to a register */ 1616c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1617c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1618e8996ee0Sbellard ts->val_type = TEMP_VAL_REG; 1619e8996ee0Sbellard ts->reg = reg; 1620e8996ee0Sbellard ts->mem_coherent = 0; 1621e8996ee0Sbellard s->reg_to_temp[reg] = arg; 1622c896fe29Sbellard } 1623c896fe29Sbellard } 1624c896fe29Sbellard assert(ts->val_type == TEMP_VAL_REG); 16255ff9d6a4Sbellard if (arg_ct->ct & TCG_CT_IALIAS) { 16265ff9d6a4Sbellard if (ts->fixed_reg) { 16275ff9d6a4Sbellard /* if fixed register, we must allocate a new register 16285ff9d6a4Sbellard if the alias is not the same register */ 16295ff9d6a4Sbellard if (arg != args[arg_ct->alias_index]) 16305ff9d6a4Sbellard goto allocate_in_reg; 16315ff9d6a4Sbellard } else { 1632c896fe29Sbellard /* if the input is aliased to an output and if it is 1633c896fe29Sbellard not dead after the instruction, we must allocate 1634c896fe29Sbellard a new register and move it */ 16355ff9d6a4Sbellard if (!IS_DEAD_IARG(i - nb_oargs)) 1636c896fe29Sbellard goto allocate_in_reg; 1637c896fe29Sbellard } 16385ff9d6a4Sbellard } 1639c896fe29Sbellard reg = ts->reg; 1640c896fe29Sbellard if (tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1641c896fe29Sbellard /* nothing to do : the constraint is satisfied */ 1642c896fe29Sbellard } else { 1643c896fe29Sbellard allocate_in_reg: 1644c896fe29Sbellard /* allocate a new register matching the constraint 1645c896fe29Sbellard and move the temporary register into it */ 1646c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1647c896fe29Sbellard tcg_out_mov(s, reg, ts->reg); 1648c896fe29Sbellard } 1649c896fe29Sbellard new_args[i] = reg; 1650c896fe29Sbellard const_args[i] = 0; 1651c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1652c896fe29Sbellard iarg_end: ; 1653c896fe29Sbellard } 1654c896fe29Sbellard 1655e8996ee0Sbellard if (def->flags & TCG_OPF_BB_END) { 1656e8996ee0Sbellard tcg_reg_alloc_bb_end(s, allocated_regs); 1657e8996ee0Sbellard } else { 1658c896fe29Sbellard /* mark dead temporaries and free the associated registers */ 1659c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 1660c896fe29Sbellard arg = args[nb_oargs + i]; 1661c896fe29Sbellard if (IS_DEAD_IARG(i)) { 1662c896fe29Sbellard ts = &s->temps[arg]; 1663e8996ee0Sbellard if (!ts->fixed_reg) { 1664c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1665c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1666c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1667c896fe29Sbellard } 1668c896fe29Sbellard } 1669c896fe29Sbellard } 1670c896fe29Sbellard 1671c896fe29Sbellard if (def->flags & TCG_OPF_CALL_CLOBBER) { 1672b03cce8eSbellard /* XXX: permit generic clobber register list ? */ 1673c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1674c896fe29Sbellard if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) { 1675c896fe29Sbellard tcg_reg_free(s, reg); 1676c896fe29Sbellard } 1677c896fe29Sbellard } 1678b03cce8eSbellard /* XXX: for load/store we could do that only for the slow path 1679b03cce8eSbellard (i.e. when a memory callback is called) */ 1680b03cce8eSbellard 1681b03cce8eSbellard /* store globals and free associated registers (we assume the insn 1682b03cce8eSbellard can modify any global. */ 1683e8996ee0Sbellard save_globals(s, allocated_regs); 1684c896fe29Sbellard } 1685c896fe29Sbellard 1686c896fe29Sbellard /* satisfy the output constraints */ 1687c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1688c896fe29Sbellard for(k = 0; k < nb_oargs; k++) { 1689c896fe29Sbellard i = def->sorted_args[k]; 1690c896fe29Sbellard arg = args[i]; 1691c896fe29Sbellard arg_ct = &def->args_ct[i]; 1692c896fe29Sbellard ts = &s->temps[arg]; 1693c896fe29Sbellard if (arg_ct->ct & TCG_CT_ALIAS) { 16945ff9d6a4Sbellard reg = new_args[arg_ct->alias_index]; 1695c896fe29Sbellard } else { 1696c896fe29Sbellard /* if fixed register, we try to use it */ 1697c896fe29Sbellard reg = ts->reg; 1698c896fe29Sbellard if (ts->fixed_reg && 1699c896fe29Sbellard tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1700c896fe29Sbellard goto oarg_end; 1701c896fe29Sbellard } 1702c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1703c896fe29Sbellard } 1704c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1705c896fe29Sbellard /* if a fixed register is used, then a move will be done afterwards */ 1706c896fe29Sbellard if (!ts->fixed_reg) { 1707c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1708c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1709c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1710c896fe29Sbellard ts->reg = reg; 1711c896fe29Sbellard /* temp value is modified, so the value kept in memory is 1712c896fe29Sbellard potentially not the same */ 1713c896fe29Sbellard ts->mem_coherent = 0; 1714c896fe29Sbellard s->reg_to_temp[reg] = arg; 1715c896fe29Sbellard } 1716c896fe29Sbellard oarg_end: 1717c896fe29Sbellard new_args[i] = reg; 1718c896fe29Sbellard } 1719e8996ee0Sbellard } 1720c896fe29Sbellard 1721c896fe29Sbellard /* emit instruction */ 1722c896fe29Sbellard tcg_out_op(s, opc, new_args, const_args); 1723c896fe29Sbellard 1724c896fe29Sbellard /* move the outputs in the correct register if needed */ 1725c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1726c896fe29Sbellard ts = &s->temps[args[i]]; 1727c896fe29Sbellard reg = new_args[i]; 1728c896fe29Sbellard if (ts->fixed_reg && ts->reg != reg) { 1729c896fe29Sbellard tcg_out_mov(s, ts->reg, reg); 1730c896fe29Sbellard } 1731c896fe29Sbellard } 1732c896fe29Sbellard } 1733c896fe29Sbellard 1734b03cce8eSbellard #ifdef TCG_TARGET_STACK_GROWSUP 1735b03cce8eSbellard #define STACK_DIR(x) (-(x)) 1736b03cce8eSbellard #else 1737b03cce8eSbellard #define STACK_DIR(x) (x) 1738b03cce8eSbellard #endif 1739b03cce8eSbellard 1740c896fe29Sbellard static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def, 1741a9751609SRichard Henderson TCGOpcode opc, const TCGArg *args, 1742c896fe29Sbellard unsigned int dead_iargs) 1743c896fe29Sbellard { 1744c896fe29Sbellard int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params; 1745c896fe29Sbellard TCGArg arg, func_arg; 1746c896fe29Sbellard TCGTemp *ts; 1747f54b3f92Saurel32 tcg_target_long stack_offset, call_stack_size, func_addr; 1748b03cce8eSbellard int const_func_arg, allocate_args; 1749c896fe29Sbellard TCGRegSet allocated_regs; 1750c896fe29Sbellard const TCGArgConstraint *arg_ct; 1751c896fe29Sbellard 1752c896fe29Sbellard arg = *args++; 1753c896fe29Sbellard 1754c896fe29Sbellard nb_oargs = arg >> 16; 1755c896fe29Sbellard nb_iargs = arg & 0xffff; 1756c896fe29Sbellard nb_params = nb_iargs - 1; 1757c896fe29Sbellard 1758c896fe29Sbellard flags = args[nb_oargs + nb_iargs]; 1759c896fe29Sbellard 1760c896fe29Sbellard nb_regs = tcg_target_get_call_iarg_regs_count(flags); 1761c896fe29Sbellard if (nb_regs > nb_params) 1762c896fe29Sbellard nb_regs = nb_params; 1763c896fe29Sbellard 1764c896fe29Sbellard /* assign stack slots first */ 1765c896fe29Sbellard /* XXX: preallocate call stack */ 1766c896fe29Sbellard call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long); 1767c896fe29Sbellard call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) & 1768c896fe29Sbellard ~(TCG_TARGET_STACK_ALIGN - 1); 1769b03cce8eSbellard allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE); 1770b03cce8eSbellard if (allocate_args) { 1771b03cce8eSbellard tcg_out_addi(s, TCG_REG_CALL_STACK, -STACK_DIR(call_stack_size)); 1772b03cce8eSbellard } 177339cf05d3Sbellard 177439cf05d3Sbellard stack_offset = TCG_TARGET_CALL_STACK_OFFSET; 1775c896fe29Sbellard for(i = nb_regs; i < nb_params; i++) { 1776c896fe29Sbellard arg = args[nb_oargs + i]; 177739cf05d3Sbellard #ifdef TCG_TARGET_STACK_GROWSUP 177839cf05d3Sbellard stack_offset -= sizeof(tcg_target_long); 177939cf05d3Sbellard #endif 178039cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1781c896fe29Sbellard ts = &s->temps[arg]; 1782c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1783e4d5434cSblueswir1 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset); 1784c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1785c896fe29Sbellard reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 1786c896fe29Sbellard s->reserved_regs); 1787c896fe29Sbellard /* XXX: not correct if reading values from the stack */ 1788e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1789e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset); 1790c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1791c896fe29Sbellard reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 1792c896fe29Sbellard s->reserved_regs); 1793c896fe29Sbellard /* XXX: sign extend may be needed on some targets */ 1794c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1795e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset); 1796c896fe29Sbellard } else { 1797c896fe29Sbellard tcg_abort(); 1798c896fe29Sbellard } 179939cf05d3Sbellard } 180039cf05d3Sbellard #ifndef TCG_TARGET_STACK_GROWSUP 180139cf05d3Sbellard stack_offset += sizeof(tcg_target_long); 180239cf05d3Sbellard #endif 1803c896fe29Sbellard } 1804c896fe29Sbellard 1805c896fe29Sbellard /* assign input registers */ 1806c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1807c896fe29Sbellard for(i = 0; i < nb_regs; i++) { 1808c896fe29Sbellard arg = args[nb_oargs + i]; 180939cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1810c896fe29Sbellard ts = &s->temps[arg]; 1811c896fe29Sbellard reg = tcg_target_call_iarg_regs[i]; 1812c896fe29Sbellard tcg_reg_free(s, reg); 1813c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1814c896fe29Sbellard if (ts->reg != reg) { 1815c896fe29Sbellard tcg_out_mov(s, reg, ts->reg); 1816c896fe29Sbellard } 1817c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1818e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1819c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1820c896fe29Sbellard /* XXX: sign extend ? */ 1821c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1822c896fe29Sbellard } else { 1823c896fe29Sbellard tcg_abort(); 1824c896fe29Sbellard } 1825c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1826c896fe29Sbellard } 182739cf05d3Sbellard } 1828c896fe29Sbellard 1829c896fe29Sbellard /* assign function address */ 1830c896fe29Sbellard func_arg = args[nb_oargs + nb_iargs - 1]; 1831c896fe29Sbellard arg_ct = &def->args_ct[0]; 1832c896fe29Sbellard ts = &s->temps[func_arg]; 1833f54b3f92Saurel32 func_addr = ts->val; 1834c896fe29Sbellard const_func_arg = 0; 1835c896fe29Sbellard if (ts->val_type == TEMP_VAL_MEM) { 1836c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1837e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1838c896fe29Sbellard func_arg = reg; 1839e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1840c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_REG) { 1841c896fe29Sbellard reg = ts->reg; 1842c896fe29Sbellard if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1843c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1844c896fe29Sbellard tcg_out_mov(s, reg, ts->reg); 1845c896fe29Sbellard } 1846c896fe29Sbellard func_arg = reg; 1847e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1848c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1849f54b3f92Saurel32 if (tcg_target_const_match(func_addr, arg_ct)) { 1850c896fe29Sbellard const_func_arg = 1; 1851f54b3f92Saurel32 func_arg = func_addr; 1852c896fe29Sbellard } else { 1853c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1854f54b3f92Saurel32 tcg_out_movi(s, ts->type, reg, func_addr); 1855c896fe29Sbellard func_arg = reg; 1856e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1857c896fe29Sbellard } 1858c896fe29Sbellard } else { 1859c896fe29Sbellard tcg_abort(); 1860c896fe29Sbellard } 1861c896fe29Sbellard 1862e8996ee0Sbellard 1863c896fe29Sbellard /* mark dead temporaries and free the associated registers */ 1864c6e113f5Sbellard for(i = 0; i < nb_iargs; i++) { 1865c896fe29Sbellard arg = args[nb_oargs + i]; 1866c896fe29Sbellard if (IS_DEAD_IARG(i)) { 1867c896fe29Sbellard ts = &s->temps[arg]; 1868e8996ee0Sbellard if (!ts->fixed_reg) { 1869c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1870c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1871c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1872c896fe29Sbellard } 1873c896fe29Sbellard } 1874c896fe29Sbellard } 1875c896fe29Sbellard 1876c896fe29Sbellard /* clobber call registers */ 1877c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1878c896fe29Sbellard if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) { 1879c896fe29Sbellard tcg_reg_free(s, reg); 1880c896fe29Sbellard } 1881c896fe29Sbellard } 1882c896fe29Sbellard 1883c896fe29Sbellard /* store globals and free associated registers (we assume the call 1884c896fe29Sbellard can modify any global. */ 1885b9c18f56Saurel32 if (!(flags & TCG_CALL_CONST)) { 1886e8996ee0Sbellard save_globals(s, allocated_regs); 1887b9c18f56Saurel32 } 1888c896fe29Sbellard 1889c896fe29Sbellard tcg_out_op(s, opc, &func_arg, &const_func_arg); 1890c896fe29Sbellard 1891b03cce8eSbellard if (allocate_args) { 1892b03cce8eSbellard tcg_out_addi(s, TCG_REG_CALL_STACK, STACK_DIR(call_stack_size)); 1893b03cce8eSbellard } 1894c896fe29Sbellard 1895c896fe29Sbellard /* assign output registers and emit moves if needed */ 1896c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1897c896fe29Sbellard arg = args[i]; 1898c896fe29Sbellard ts = &s->temps[arg]; 1899c896fe29Sbellard reg = tcg_target_call_oarg_regs[i]; 1900e8996ee0Sbellard assert(s->reg_to_temp[reg] == -1); 1901c896fe29Sbellard if (ts->fixed_reg) { 1902c896fe29Sbellard if (ts->reg != reg) { 1903c896fe29Sbellard tcg_out_mov(s, ts->reg, reg); 1904c896fe29Sbellard } 1905c896fe29Sbellard } else { 1906c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1907c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1908c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1909c896fe29Sbellard ts->reg = reg; 1910c896fe29Sbellard ts->mem_coherent = 0; 1911c896fe29Sbellard s->reg_to_temp[reg] = arg; 1912c896fe29Sbellard } 1913c896fe29Sbellard } 1914c896fe29Sbellard 1915c896fe29Sbellard return nb_iargs + nb_oargs + def->nb_cargs + 1; 1916c896fe29Sbellard } 1917c896fe29Sbellard 1918c896fe29Sbellard #ifdef CONFIG_PROFILER 1919c896fe29Sbellard 192054604f74Saurel32 static int64_t tcg_table_op_count[NB_OPS]; 1921c896fe29Sbellard 1922871e6c35SBlue Swirl static void dump_op_count(void) 1923c896fe29Sbellard { 1924c896fe29Sbellard int i; 1925c896fe29Sbellard FILE *f; 192654604f74Saurel32 f = fopen("/tmp/op.log", "w"); 1927c896fe29Sbellard for(i = INDEX_op_end; i < NB_OPS; i++) { 192854604f74Saurel32 fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, tcg_table_op_count[i]); 1929c896fe29Sbellard } 1930c896fe29Sbellard fclose(f); 1931c896fe29Sbellard } 1932c896fe29Sbellard #endif 1933c896fe29Sbellard 1934c896fe29Sbellard 1935c896fe29Sbellard static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf, 19362ba1eeb6Spbrook long search_pc) 1937c896fe29Sbellard { 1938a9751609SRichard Henderson TCGOpcode opc; 1939a9751609SRichard Henderson int op_index; 1940c896fe29Sbellard const TCGOpDef *def; 1941c896fe29Sbellard unsigned int dead_iargs; 1942c896fe29Sbellard const TCGArg *args; 1943c896fe29Sbellard 1944c896fe29Sbellard #ifdef DEBUG_DISAS 19458fec2b8cSaliguori if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) { 194693fcfe39Saliguori qemu_log("OP:\n"); 1947c896fe29Sbellard tcg_dump_ops(s, logfile); 194893fcfe39Saliguori qemu_log("\n"); 1949c896fe29Sbellard } 1950c896fe29Sbellard #endif 1951c896fe29Sbellard 1952a23a9ec6Sbellard #ifdef CONFIG_PROFILER 1953a23a9ec6Sbellard s->la_time -= profile_getclock(); 1954a23a9ec6Sbellard #endif 1955c896fe29Sbellard tcg_liveness_analysis(s); 1956a23a9ec6Sbellard #ifdef CONFIG_PROFILER 1957a23a9ec6Sbellard s->la_time += profile_getclock(); 1958a23a9ec6Sbellard #endif 1959c896fe29Sbellard 1960c896fe29Sbellard #ifdef DEBUG_DISAS 19618fec2b8cSaliguori if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT))) { 19626a957025SAurelien Jarno qemu_log("OP after liveness analysis:\n"); 1963c896fe29Sbellard tcg_dump_ops(s, logfile); 196493fcfe39Saliguori qemu_log("\n"); 1965c896fe29Sbellard } 1966c896fe29Sbellard #endif 1967c896fe29Sbellard 1968c896fe29Sbellard tcg_reg_alloc_start(s); 1969c896fe29Sbellard 1970c896fe29Sbellard s->code_buf = gen_code_buf; 1971c896fe29Sbellard s->code_ptr = gen_code_buf; 1972c896fe29Sbellard 1973c896fe29Sbellard args = gen_opparam_buf; 1974c896fe29Sbellard op_index = 0; 1975b3db8758Sblueswir1 1976c896fe29Sbellard for(;;) { 1977c896fe29Sbellard opc = gen_opc_buf[op_index]; 1978c896fe29Sbellard #ifdef CONFIG_PROFILER 197954604f74Saurel32 tcg_table_op_count[opc]++; 1980c896fe29Sbellard #endif 1981c896fe29Sbellard def = &tcg_op_defs[opc]; 1982c896fe29Sbellard #if 0 1983c896fe29Sbellard printf("%s: %d %d %d\n", def->name, 1984c896fe29Sbellard def->nb_oargs, def->nb_iargs, def->nb_cargs); 1985c896fe29Sbellard // dump_regs(s); 1986c896fe29Sbellard #endif 1987c896fe29Sbellard switch(opc) { 1988c896fe29Sbellard case INDEX_op_mov_i32: 1989c896fe29Sbellard #if TCG_TARGET_REG_BITS == 64 1990c896fe29Sbellard case INDEX_op_mov_i64: 1991c896fe29Sbellard #endif 1992c896fe29Sbellard dead_iargs = s->op_dead_iargs[op_index]; 1993c896fe29Sbellard tcg_reg_alloc_mov(s, def, args, dead_iargs); 1994c896fe29Sbellard break; 1995e8996ee0Sbellard case INDEX_op_movi_i32: 1996e8996ee0Sbellard #if TCG_TARGET_REG_BITS == 64 1997e8996ee0Sbellard case INDEX_op_movi_i64: 1998e8996ee0Sbellard #endif 1999e8996ee0Sbellard tcg_reg_alloc_movi(s, args); 2000e8996ee0Sbellard break; 20017e4597d7Sbellard case INDEX_op_debug_insn_start: 20027e4597d7Sbellard /* debug instruction */ 20037e4597d7Sbellard break; 2004c896fe29Sbellard case INDEX_op_nop: 2005c896fe29Sbellard case INDEX_op_nop1: 2006c896fe29Sbellard case INDEX_op_nop2: 2007c896fe29Sbellard case INDEX_op_nop3: 2008c896fe29Sbellard break; 2009c896fe29Sbellard case INDEX_op_nopn: 2010c896fe29Sbellard args += args[0]; 2011c896fe29Sbellard goto next; 20125ff9d6a4Sbellard case INDEX_op_discard: 20135ff9d6a4Sbellard { 20145ff9d6a4Sbellard TCGTemp *ts; 20155ff9d6a4Sbellard ts = &s->temps[args[0]]; 20165ff9d6a4Sbellard /* mark the temporary as dead */ 2017e8996ee0Sbellard if (!ts->fixed_reg) { 20185ff9d6a4Sbellard if (ts->val_type == TEMP_VAL_REG) 20195ff9d6a4Sbellard s->reg_to_temp[ts->reg] = -1; 20205ff9d6a4Sbellard ts->val_type = TEMP_VAL_DEAD; 20215ff9d6a4Sbellard } 20225ff9d6a4Sbellard } 20235ff9d6a4Sbellard break; 2024c896fe29Sbellard case INDEX_op_set_label: 2025e8996ee0Sbellard tcg_reg_alloc_bb_end(s, s->reserved_regs); 2026c896fe29Sbellard tcg_out_label(s, args[0], (long)s->code_ptr); 2027c896fe29Sbellard break; 2028c896fe29Sbellard case INDEX_op_call: 2029c896fe29Sbellard dead_iargs = s->op_dead_iargs[op_index]; 2030c896fe29Sbellard args += tcg_reg_alloc_call(s, def, opc, args, dead_iargs); 2031c896fe29Sbellard goto next; 2032c896fe29Sbellard case INDEX_op_end: 2033c896fe29Sbellard goto the_end; 2034c896fe29Sbellard default: 2035c896fe29Sbellard /* Note: in order to speed up the code, it would be much 2036c896fe29Sbellard faster to have specialized register allocator functions for 2037c896fe29Sbellard some common argument patterns */ 2038c896fe29Sbellard dead_iargs = s->op_dead_iargs[op_index]; 2039c896fe29Sbellard tcg_reg_alloc_op(s, def, opc, args, dead_iargs); 2040c896fe29Sbellard break; 2041c896fe29Sbellard } 2042c896fe29Sbellard args += def->nb_args; 20438df1ca4bSths next: 20442ba1eeb6Spbrook if (search_pc >= 0 && search_pc < s->code_ptr - gen_code_buf) { 2045c896fe29Sbellard return op_index; 2046c896fe29Sbellard } 2047c896fe29Sbellard op_index++; 2048c896fe29Sbellard #ifndef NDEBUG 2049c896fe29Sbellard check_regs(s); 2050c896fe29Sbellard #endif 2051c896fe29Sbellard } 2052c896fe29Sbellard the_end: 2053c896fe29Sbellard return -1; 2054c896fe29Sbellard } 2055c896fe29Sbellard 205654604f74Saurel32 int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf) 2057c896fe29Sbellard { 2058c896fe29Sbellard #ifdef CONFIG_PROFILER 2059c896fe29Sbellard { 2060c896fe29Sbellard int n; 2061c896fe29Sbellard n = (gen_opc_ptr - gen_opc_buf); 2062a23a9ec6Sbellard s->op_count += n; 2063a23a9ec6Sbellard if (n > s->op_count_max) 2064a23a9ec6Sbellard s->op_count_max = n; 2065a23a9ec6Sbellard 2066a23a9ec6Sbellard s->temp_count += s->nb_temps; 2067a23a9ec6Sbellard if (s->nb_temps > s->temp_count_max) 2068a23a9ec6Sbellard s->temp_count_max = s->nb_temps; 2069c896fe29Sbellard } 2070c896fe29Sbellard #endif 2071c896fe29Sbellard 20722ba1eeb6Spbrook tcg_gen_code_common(s, gen_code_buf, -1); 2073c896fe29Sbellard 2074c896fe29Sbellard /* flush instruction cache */ 2075c896fe29Sbellard flush_icache_range((unsigned long)gen_code_buf, 2076c896fe29Sbellard (unsigned long)s->code_ptr); 2077c896fe29Sbellard return s->code_ptr - gen_code_buf; 2078c896fe29Sbellard } 2079c896fe29Sbellard 20802ba1eeb6Spbrook /* Return the index of the micro operation such as the pc after is < 2081623e265cSpbrook offset bytes from the start of the TB. The contents of gen_code_buf must 2082623e265cSpbrook not be changed, though writing the same values is ok. 2083623e265cSpbrook Return -1 if not found. */ 208454604f74Saurel32 int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset) 2085c896fe29Sbellard { 2086623e265cSpbrook return tcg_gen_code_common(s, gen_code_buf, offset); 2087c896fe29Sbellard } 2088a23a9ec6Sbellard 2089a23a9ec6Sbellard #ifdef CONFIG_PROFILER 2090a23a9ec6Sbellard void tcg_dump_info(FILE *f, 2091a23a9ec6Sbellard int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) 2092a23a9ec6Sbellard { 2093a23a9ec6Sbellard TCGContext *s = &tcg_ctx; 2094a23a9ec6Sbellard int64_t tot; 2095a23a9ec6Sbellard 2096a23a9ec6Sbellard tot = s->interm_time + s->code_time; 2097a23a9ec6Sbellard cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n", 2098a23a9ec6Sbellard tot, tot / 2.4e9); 2099a23a9ec6Sbellard cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n", 2100a23a9ec6Sbellard s->tb_count, 2101a23a9ec6Sbellard s->tb_count1 - s->tb_count, 2102a23a9ec6Sbellard s->tb_count1 ? (double)(s->tb_count1 - s->tb_count) / s->tb_count1 * 100.0 : 0); 2103a23a9ec6Sbellard cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n", 2104a23a9ec6Sbellard s->tb_count ? (double)s->op_count / s->tb_count : 0, s->op_count_max); 2105a23a9ec6Sbellard cpu_fprintf(f, "deleted ops/TB %0.2f\n", 2106a23a9ec6Sbellard s->tb_count ? 2107a23a9ec6Sbellard (double)s->del_op_count / s->tb_count : 0); 2108a23a9ec6Sbellard cpu_fprintf(f, "avg temps/TB %0.2f max=%d\n", 2109a23a9ec6Sbellard s->tb_count ? 2110a23a9ec6Sbellard (double)s->temp_count / s->tb_count : 0, 2111a23a9ec6Sbellard s->temp_count_max); 2112a23a9ec6Sbellard 2113a23a9ec6Sbellard cpu_fprintf(f, "cycles/op %0.1f\n", 2114a23a9ec6Sbellard s->op_count ? (double)tot / s->op_count : 0); 2115a23a9ec6Sbellard cpu_fprintf(f, "cycles/in byte %0.1f\n", 2116a23a9ec6Sbellard s->code_in_len ? (double)tot / s->code_in_len : 0); 2117a23a9ec6Sbellard cpu_fprintf(f, "cycles/out byte %0.1f\n", 2118a23a9ec6Sbellard s->code_out_len ? (double)tot / s->code_out_len : 0); 2119a23a9ec6Sbellard if (tot == 0) 2120a23a9ec6Sbellard tot = 1; 2121a23a9ec6Sbellard cpu_fprintf(f, " gen_interm time %0.1f%%\n", 2122a23a9ec6Sbellard (double)s->interm_time / tot * 100.0); 2123a23a9ec6Sbellard cpu_fprintf(f, " gen_code time %0.1f%%\n", 2124a23a9ec6Sbellard (double)s->code_time / tot * 100.0); 2125a23a9ec6Sbellard cpu_fprintf(f, "liveness/code time %0.1f%%\n", 2126a23a9ec6Sbellard (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0); 2127a23a9ec6Sbellard cpu_fprintf(f, "cpu_restore count %" PRId64 "\n", 2128a23a9ec6Sbellard s->restore_count); 2129a23a9ec6Sbellard cpu_fprintf(f, " avg cycles %0.1f\n", 2130a23a9ec6Sbellard s->restore_count ? (double)s->restore_time / s->restore_count : 0); 2131871e6c35SBlue Swirl 2132a23a9ec6Sbellard dump_op_count(); 2133a23a9ec6Sbellard } 2134a23a9ec6Sbellard #else 213524bf7b3aSbellard void tcg_dump_info(FILE *f, 2136a23a9ec6Sbellard int (*cpu_fprintf)(FILE *f, const char *fmt, ...)) 2137a23a9ec6Sbellard { 213824bf7b3aSbellard cpu_fprintf(f, "[TCG profiler not compiled]\n"); 2139a23a9ec6Sbellard } 2140a23a9ec6Sbellard #endif 2141