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 278f2e8c07SKirill Batuzov #define USE_TCG_OPTIMIZATIONS 28c896fe29Sbellard 29cca82982Saurel32 #include "config.h" 30cca82982Saurel32 31a6c6f76cSBlue Swirl #if !defined(CONFIG_DEBUG_TCG) && !defined(NDEBUG) 32cca82982Saurel32 /* define it to suppress various consistency checks (faster) */ 33cca82982Saurel32 #define NDEBUG 34cca82982Saurel32 #endif 35cca82982Saurel32 36c896fe29Sbellard #include <stdarg.h> 37c896fe29Sbellard #include <stdlib.h> 38c896fe29Sbellard #include <stdio.h> 39c896fe29Sbellard #include <string.h> 40c896fe29Sbellard #include <inttypes.h> 413fe43da7Sbellard #ifdef _WIN32 423fe43da7Sbellard #include <malloc.h> 433fe43da7Sbellard #endif 44b29fe3edSmalc #ifdef _AIX 45b29fe3edSmalc #include <alloca.h> 46b29fe3edSmalc #endif 47c896fe29Sbellard 48ca10f867Saurel32 #include "qemu-common.h" 49902b3d5cSmalc #include "cache-utils.h" 50379f6698SPaul Brook #include "host-utils.h" 512d8ebcf9SRichard Henderson #include "qemu-timer.h" 52c896fe29Sbellard 53c896fe29Sbellard /* Note: the long term plan is to reduce the dependancies on the QEMU 54c896fe29Sbellard CPU definitions. Currently they are used for qemu_ld/st 55c896fe29Sbellard instructions */ 56c896fe29Sbellard #define NO_CPU_IO_DEFS 57c896fe29Sbellard #include "cpu.h" 58c896fe29Sbellard 59c896fe29Sbellard #include "tcg-op.h" 60c896fe29Sbellard #include "elf.h" 61c896fe29Sbellard 62379f6698SPaul Brook #if defined(CONFIG_USE_GUEST_BASE) && !defined(TCG_TARGET_HAS_GUEST_BASE) 63379f6698SPaul Brook #error GUEST_BASE not supported on this host. 64379f6698SPaul Brook #endif 65c896fe29Sbellard 66e4d58b41SRichard Henderson static void tcg_target_init(TCGContext *s); 67e4d58b41SRichard Henderson static void tcg_target_qemu_prologue(TCGContext *s); 68c896fe29Sbellard static void patch_reloc(uint8_t *code_ptr, int type, 69f54b3f92Saurel32 tcg_target_long value, tcg_target_long addend); 70c896fe29Sbellard 718399ad59SRichard Henderson TCGOpDef tcg_op_defs[] = { 720e2029a0SAurelien Jarno #define DEF(s, oargs, iargs, cargs, flags) { #s, oargs, iargs, cargs, iargs + oargs + cargs, flags }, 73c896fe29Sbellard #include "tcg-opc.h" 74c896fe29Sbellard #undef DEF 75c896fe29Sbellard }; 76c896fe29Sbellard 77b1d8e52eSblueswir1 static TCGRegSet tcg_target_available_regs[2]; 78b1d8e52eSblueswir1 static TCGRegSet tcg_target_call_clobber_regs; 79c896fe29Sbellard 80c896fe29Sbellard /* XXX: move that inside the context */ 81c896fe29Sbellard uint16_t *gen_opc_ptr; 82c896fe29Sbellard TCGArg *gen_opparam_ptr; 83c896fe29Sbellard 84c896fe29Sbellard static inline void tcg_out8(TCGContext *s, uint8_t v) 85c896fe29Sbellard { 86c896fe29Sbellard *s->code_ptr++ = v; 87c896fe29Sbellard } 88c896fe29Sbellard 89c896fe29Sbellard static inline void tcg_out16(TCGContext *s, uint16_t v) 90c896fe29Sbellard { 91c896fe29Sbellard *(uint16_t *)s->code_ptr = v; 92c896fe29Sbellard s->code_ptr += 2; 93c896fe29Sbellard } 94c896fe29Sbellard 95c896fe29Sbellard static inline void tcg_out32(TCGContext *s, uint32_t v) 96c896fe29Sbellard { 97c896fe29Sbellard *(uint32_t *)s->code_ptr = v; 98c896fe29Sbellard s->code_ptr += 4; 99c896fe29Sbellard } 100c896fe29Sbellard 101c896fe29Sbellard /* label relocation processing */ 102c896fe29Sbellard 103a5ad5916SStefan Weil static void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type, 104c896fe29Sbellard int label_index, long addend) 105c896fe29Sbellard { 106c896fe29Sbellard TCGLabel *l; 107c896fe29Sbellard TCGRelocation *r; 108c896fe29Sbellard 109c896fe29Sbellard l = &s->labels[label_index]; 110c896fe29Sbellard if (l->has_value) { 111623e265cSpbrook /* FIXME: This may break relocations on RISC targets that 112623e265cSpbrook modify instruction fields in place. The caller may not have 113623e265cSpbrook written the initial value. */ 114f54b3f92Saurel32 patch_reloc(code_ptr, type, l->u.value, addend); 115c896fe29Sbellard } else { 116c896fe29Sbellard /* add a new relocation entry */ 117c896fe29Sbellard r = tcg_malloc(sizeof(TCGRelocation)); 118c896fe29Sbellard r->type = type; 119c896fe29Sbellard r->ptr = code_ptr; 120c896fe29Sbellard r->addend = addend; 121c896fe29Sbellard r->next = l->u.first_reloc; 122c896fe29Sbellard l->u.first_reloc = r; 123c896fe29Sbellard } 124c896fe29Sbellard } 125c896fe29Sbellard 126c896fe29Sbellard static void tcg_out_label(TCGContext *s, int label_index, 127c896fe29Sbellard tcg_target_long value) 128c896fe29Sbellard { 129c896fe29Sbellard TCGLabel *l; 130c896fe29Sbellard TCGRelocation *r; 131c896fe29Sbellard 132c896fe29Sbellard l = &s->labels[label_index]; 133c896fe29Sbellard if (l->has_value) 134c896fe29Sbellard tcg_abort(); 135c896fe29Sbellard r = l->u.first_reloc; 136c896fe29Sbellard while (r != NULL) { 137f54b3f92Saurel32 patch_reloc(r->ptr, r->type, value, r->addend); 138c896fe29Sbellard r = r->next; 139c896fe29Sbellard } 140c896fe29Sbellard l->has_value = 1; 141c896fe29Sbellard l->u.value = value; 142c896fe29Sbellard } 143c896fe29Sbellard 144c896fe29Sbellard int gen_new_label(void) 145c896fe29Sbellard { 146c896fe29Sbellard TCGContext *s = &tcg_ctx; 147c896fe29Sbellard int idx; 148c896fe29Sbellard TCGLabel *l; 149c896fe29Sbellard 150c896fe29Sbellard if (s->nb_labels >= TCG_MAX_LABELS) 151c896fe29Sbellard tcg_abort(); 152c896fe29Sbellard idx = s->nb_labels++; 153c896fe29Sbellard l = &s->labels[idx]; 154c896fe29Sbellard l->has_value = 0; 155c896fe29Sbellard l->u.first_reloc = NULL; 156c896fe29Sbellard return idx; 157c896fe29Sbellard } 158c896fe29Sbellard 159c896fe29Sbellard #include "tcg-target.c" 160c896fe29Sbellard 161c896fe29Sbellard /* pool based memory allocation */ 162c896fe29Sbellard void *tcg_malloc_internal(TCGContext *s, int size) 163c896fe29Sbellard { 164c896fe29Sbellard TCGPool *p; 165c896fe29Sbellard int pool_size; 166c896fe29Sbellard 167c896fe29Sbellard if (size > TCG_POOL_CHUNK_SIZE) { 168c896fe29Sbellard /* big malloc: insert a new pool (XXX: could optimize) */ 1697267c094SAnthony Liguori p = g_malloc(sizeof(TCGPool) + size); 170c896fe29Sbellard p->size = size; 171c896fe29Sbellard if (s->pool_current) 172c896fe29Sbellard s->pool_current->next = p; 173c896fe29Sbellard else 174c896fe29Sbellard s->pool_first = p; 175c896fe29Sbellard p->next = s->pool_current; 176c896fe29Sbellard } else { 177c896fe29Sbellard p = s->pool_current; 178c896fe29Sbellard if (!p) { 179c896fe29Sbellard p = s->pool_first; 180c896fe29Sbellard if (!p) 181c896fe29Sbellard goto new_pool; 182c896fe29Sbellard } else { 183c896fe29Sbellard if (!p->next) { 184c896fe29Sbellard new_pool: 185c896fe29Sbellard pool_size = TCG_POOL_CHUNK_SIZE; 1867267c094SAnthony Liguori p = g_malloc(sizeof(TCGPool) + pool_size); 187c896fe29Sbellard p->size = pool_size; 188c896fe29Sbellard p->next = NULL; 189c896fe29Sbellard if (s->pool_current) 190c896fe29Sbellard s->pool_current->next = p; 191c896fe29Sbellard else 192c896fe29Sbellard s->pool_first = p; 193c896fe29Sbellard } else { 194c896fe29Sbellard p = p->next; 195c896fe29Sbellard } 196c896fe29Sbellard } 197c896fe29Sbellard } 198c896fe29Sbellard s->pool_current = p; 199c896fe29Sbellard s->pool_cur = p->data + size; 200c896fe29Sbellard s->pool_end = p->data + p->size; 201c896fe29Sbellard return p->data; 202c896fe29Sbellard } 203c896fe29Sbellard 204c896fe29Sbellard void tcg_pool_reset(TCGContext *s) 205c896fe29Sbellard { 206c896fe29Sbellard s->pool_cur = s->pool_end = NULL; 207c896fe29Sbellard s->pool_current = NULL; 208c896fe29Sbellard } 209c896fe29Sbellard 210c896fe29Sbellard void tcg_context_init(TCGContext *s) 211c896fe29Sbellard { 212c896fe29Sbellard int op, total_args, n; 213c896fe29Sbellard TCGOpDef *def; 214c896fe29Sbellard TCGArgConstraint *args_ct; 215c896fe29Sbellard int *sorted_args; 216c896fe29Sbellard 217c896fe29Sbellard memset(s, 0, sizeof(*s)); 218c896fe29Sbellard s->temps = s->static_temps; 219c896fe29Sbellard s->nb_globals = 0; 220c896fe29Sbellard 221c896fe29Sbellard /* Count total number of arguments and allocate the corresponding 222c896fe29Sbellard space */ 223c896fe29Sbellard total_args = 0; 224c896fe29Sbellard for(op = 0; op < NB_OPS; op++) { 225c896fe29Sbellard def = &tcg_op_defs[op]; 226c896fe29Sbellard n = def->nb_iargs + def->nb_oargs; 227c896fe29Sbellard total_args += n; 228c896fe29Sbellard } 229c896fe29Sbellard 2307267c094SAnthony Liguori args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args); 2317267c094SAnthony Liguori sorted_args = g_malloc(sizeof(int) * total_args); 232c896fe29Sbellard 233c896fe29Sbellard for(op = 0; op < NB_OPS; op++) { 234c896fe29Sbellard def = &tcg_op_defs[op]; 235c896fe29Sbellard def->args_ct = args_ct; 236c896fe29Sbellard def->sorted_args = sorted_args; 237c896fe29Sbellard n = def->nb_iargs + def->nb_oargs; 238c896fe29Sbellard sorted_args += n; 239c896fe29Sbellard args_ct += n; 240c896fe29Sbellard } 241c896fe29Sbellard 242c896fe29Sbellard tcg_target_init(s); 2439002ec79SRichard Henderson } 244b03cce8eSbellard 2459002ec79SRichard Henderson void tcg_prologue_init(TCGContext *s) 2469002ec79SRichard Henderson { 247b03cce8eSbellard /* init global prologue and epilogue */ 248b03cce8eSbellard s->code_buf = code_gen_prologue; 249b03cce8eSbellard s->code_ptr = s->code_buf; 250b03cce8eSbellard tcg_target_qemu_prologue(s); 251b03cce8eSbellard flush_icache_range((unsigned long)s->code_buf, 252b03cce8eSbellard (unsigned long)s->code_ptr); 253c896fe29Sbellard } 254c896fe29Sbellard 255c896fe29Sbellard void tcg_set_frame(TCGContext *s, int reg, 256c896fe29Sbellard tcg_target_long start, tcg_target_long size) 257c896fe29Sbellard { 258c896fe29Sbellard s->frame_start = start; 259c896fe29Sbellard s->frame_end = start + size; 260c896fe29Sbellard s->frame_reg = reg; 261c896fe29Sbellard } 262c896fe29Sbellard 263c896fe29Sbellard void tcg_func_start(TCGContext *s) 264c896fe29Sbellard { 265e8996ee0Sbellard int i; 266c896fe29Sbellard tcg_pool_reset(s); 267c896fe29Sbellard s->nb_temps = s->nb_globals; 268641d5fbeSbellard for(i = 0; i < (TCG_TYPE_COUNT * 2); i++) 269e8996ee0Sbellard s->first_free_temp[i] = -1; 270c896fe29Sbellard s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS); 271c896fe29Sbellard s->nb_labels = 0; 272c896fe29Sbellard s->current_frame_offset = s->frame_start; 273c896fe29Sbellard 274c896fe29Sbellard gen_opc_ptr = gen_opc_buf; 275c896fe29Sbellard gen_opparam_ptr = gen_opparam_buf; 276c896fe29Sbellard } 277c896fe29Sbellard 278c896fe29Sbellard static inline void tcg_temp_alloc(TCGContext *s, int n) 279c896fe29Sbellard { 280c896fe29Sbellard if (n > TCG_MAX_TEMPS) 281c896fe29Sbellard tcg_abort(); 282c896fe29Sbellard } 283c896fe29Sbellard 284a7812ae4Spbrook static inline int tcg_global_reg_new_internal(TCGType type, int reg, 285a7812ae4Spbrook const char *name) 286c896fe29Sbellard { 287c896fe29Sbellard TCGContext *s = &tcg_ctx; 288c896fe29Sbellard TCGTemp *ts; 289c896fe29Sbellard int idx; 290c896fe29Sbellard 291c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 292c896fe29Sbellard if (type != TCG_TYPE_I32) 293c896fe29Sbellard tcg_abort(); 294c896fe29Sbellard #endif 295c896fe29Sbellard if (tcg_regset_test_reg(s->reserved_regs, reg)) 296c896fe29Sbellard tcg_abort(); 297c896fe29Sbellard idx = s->nb_globals; 298c896fe29Sbellard tcg_temp_alloc(s, s->nb_globals + 1); 299c896fe29Sbellard ts = &s->temps[s->nb_globals]; 300c896fe29Sbellard ts->base_type = type; 301c896fe29Sbellard ts->type = type; 302c896fe29Sbellard ts->fixed_reg = 1; 303c896fe29Sbellard ts->reg = reg; 304c896fe29Sbellard ts->name = name; 305c896fe29Sbellard s->nb_globals++; 306c896fe29Sbellard tcg_regset_set_reg(s->reserved_regs, reg); 307a7812ae4Spbrook return idx; 308a7812ae4Spbrook } 309a7812ae4Spbrook 310a7812ae4Spbrook TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name) 311a7812ae4Spbrook { 312a7812ae4Spbrook int idx; 313a7812ae4Spbrook 314a7812ae4Spbrook idx = tcg_global_reg_new_internal(TCG_TYPE_I32, reg, name); 315a7812ae4Spbrook return MAKE_TCGV_I32(idx); 316a7812ae4Spbrook } 317a7812ae4Spbrook 318a7812ae4Spbrook TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name) 319a7812ae4Spbrook { 320a7812ae4Spbrook int idx; 321a7812ae4Spbrook 322a7812ae4Spbrook idx = tcg_global_reg_new_internal(TCG_TYPE_I64, reg, name); 323a7812ae4Spbrook return MAKE_TCGV_I64(idx); 324c896fe29Sbellard } 325c896fe29Sbellard 326a7812ae4Spbrook static inline int tcg_global_mem_new_internal(TCGType type, int reg, 327a7812ae4Spbrook tcg_target_long offset, 328c896fe29Sbellard const char *name) 329c896fe29Sbellard { 330c896fe29Sbellard TCGContext *s = &tcg_ctx; 331c896fe29Sbellard TCGTemp *ts; 332c896fe29Sbellard int idx; 333c896fe29Sbellard 334c896fe29Sbellard idx = s->nb_globals; 335c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 336c896fe29Sbellard if (type == TCG_TYPE_I64) { 337c896fe29Sbellard char buf[64]; 338c588979bSths tcg_temp_alloc(s, s->nb_globals + 2); 339c896fe29Sbellard ts = &s->temps[s->nb_globals]; 340c896fe29Sbellard ts->base_type = type; 341c896fe29Sbellard ts->type = TCG_TYPE_I32; 342c896fe29Sbellard ts->fixed_reg = 0; 343c896fe29Sbellard ts->mem_allocated = 1; 344c896fe29Sbellard ts->mem_reg = reg; 345c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN 346c896fe29Sbellard ts->mem_offset = offset + 4; 347c896fe29Sbellard #else 348c896fe29Sbellard ts->mem_offset = offset; 349c896fe29Sbellard #endif 350c896fe29Sbellard pstrcpy(buf, sizeof(buf), name); 351c896fe29Sbellard pstrcat(buf, sizeof(buf), "_0"); 352c896fe29Sbellard ts->name = strdup(buf); 353c896fe29Sbellard ts++; 354c896fe29Sbellard 355c896fe29Sbellard ts->base_type = type; 356c896fe29Sbellard ts->type = TCG_TYPE_I32; 357c896fe29Sbellard ts->fixed_reg = 0; 358c896fe29Sbellard ts->mem_allocated = 1; 359c896fe29Sbellard ts->mem_reg = reg; 360c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN 361c896fe29Sbellard ts->mem_offset = offset; 362c896fe29Sbellard #else 363c896fe29Sbellard ts->mem_offset = offset + 4; 364c896fe29Sbellard #endif 365c896fe29Sbellard pstrcpy(buf, sizeof(buf), name); 366c896fe29Sbellard pstrcat(buf, sizeof(buf), "_1"); 367c896fe29Sbellard ts->name = strdup(buf); 368c896fe29Sbellard 369c896fe29Sbellard s->nb_globals += 2; 370c896fe29Sbellard } else 371c896fe29Sbellard #endif 372c896fe29Sbellard { 373c896fe29Sbellard tcg_temp_alloc(s, s->nb_globals + 1); 374c896fe29Sbellard ts = &s->temps[s->nb_globals]; 375c896fe29Sbellard ts->base_type = type; 376c896fe29Sbellard ts->type = type; 377c896fe29Sbellard ts->fixed_reg = 0; 378c896fe29Sbellard ts->mem_allocated = 1; 379c896fe29Sbellard ts->mem_reg = reg; 380c896fe29Sbellard ts->mem_offset = offset; 381c896fe29Sbellard ts->name = name; 382c896fe29Sbellard s->nb_globals++; 383c896fe29Sbellard } 384a7812ae4Spbrook return idx; 385c896fe29Sbellard } 386c896fe29Sbellard 387a7812ae4Spbrook TCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset, 388a7812ae4Spbrook const char *name) 389a7812ae4Spbrook { 390a7812ae4Spbrook int idx; 391a7812ae4Spbrook 392a7812ae4Spbrook idx = tcg_global_mem_new_internal(TCG_TYPE_I32, reg, offset, name); 393a7812ae4Spbrook return MAKE_TCGV_I32(idx); 394a7812ae4Spbrook } 395a7812ae4Spbrook 396a7812ae4Spbrook TCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset, 397a7812ae4Spbrook const char *name) 398a7812ae4Spbrook { 399a7812ae4Spbrook int idx; 400a7812ae4Spbrook 401a7812ae4Spbrook idx = tcg_global_mem_new_internal(TCG_TYPE_I64, reg, offset, name); 402a7812ae4Spbrook return MAKE_TCGV_I64(idx); 403a7812ae4Spbrook } 404a7812ae4Spbrook 405a7812ae4Spbrook static inline int tcg_temp_new_internal(TCGType type, int temp_local) 406c896fe29Sbellard { 407c896fe29Sbellard TCGContext *s = &tcg_ctx; 408c896fe29Sbellard TCGTemp *ts; 409641d5fbeSbellard int idx, k; 410c896fe29Sbellard 411641d5fbeSbellard k = type; 412641d5fbeSbellard if (temp_local) 413641d5fbeSbellard k += TCG_TYPE_COUNT; 414641d5fbeSbellard idx = s->first_free_temp[k]; 415e8996ee0Sbellard if (idx != -1) { 416e8996ee0Sbellard /* There is already an available temp with the 417e8996ee0Sbellard right type */ 418e8996ee0Sbellard ts = &s->temps[idx]; 419641d5fbeSbellard s->first_free_temp[k] = ts->next_free_temp; 420e8996ee0Sbellard ts->temp_allocated = 1; 421641d5fbeSbellard assert(ts->temp_local == temp_local); 422e8996ee0Sbellard } else { 423c896fe29Sbellard idx = s->nb_temps; 424c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32 425c896fe29Sbellard if (type == TCG_TYPE_I64) { 4268df1ca4bSths tcg_temp_alloc(s, s->nb_temps + 2); 427c896fe29Sbellard ts = &s->temps[s->nb_temps]; 428c896fe29Sbellard ts->base_type = type; 429c896fe29Sbellard ts->type = TCG_TYPE_I32; 430e8996ee0Sbellard ts->temp_allocated = 1; 431641d5fbeSbellard ts->temp_local = temp_local; 432c896fe29Sbellard ts->name = NULL; 433c896fe29Sbellard ts++; 434c896fe29Sbellard ts->base_type = TCG_TYPE_I32; 435c896fe29Sbellard ts->type = TCG_TYPE_I32; 436e8996ee0Sbellard ts->temp_allocated = 1; 437641d5fbeSbellard ts->temp_local = temp_local; 438c896fe29Sbellard ts->name = NULL; 439c896fe29Sbellard s->nb_temps += 2; 440c896fe29Sbellard } else 441c896fe29Sbellard #endif 442c896fe29Sbellard { 443c896fe29Sbellard tcg_temp_alloc(s, s->nb_temps + 1); 444c896fe29Sbellard ts = &s->temps[s->nb_temps]; 445c896fe29Sbellard ts->base_type = type; 446c896fe29Sbellard ts->type = type; 447e8996ee0Sbellard ts->temp_allocated = 1; 448641d5fbeSbellard ts->temp_local = temp_local; 449c896fe29Sbellard ts->name = NULL; 450c896fe29Sbellard s->nb_temps++; 451c896fe29Sbellard } 452e8996ee0Sbellard } 45327bfd83cSPeter Maydell 45427bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG) 45527bfd83cSPeter Maydell s->temps_in_use++; 45627bfd83cSPeter Maydell #endif 457a7812ae4Spbrook return idx; 458c896fe29Sbellard } 459c896fe29Sbellard 460a7812ae4Spbrook TCGv_i32 tcg_temp_new_internal_i32(int temp_local) 461a7812ae4Spbrook { 462a7812ae4Spbrook int idx; 463a7812ae4Spbrook 464a7812ae4Spbrook idx = tcg_temp_new_internal(TCG_TYPE_I32, temp_local); 465a7812ae4Spbrook return MAKE_TCGV_I32(idx); 466a7812ae4Spbrook } 467a7812ae4Spbrook 468a7812ae4Spbrook TCGv_i64 tcg_temp_new_internal_i64(int temp_local) 469a7812ae4Spbrook { 470a7812ae4Spbrook int idx; 471a7812ae4Spbrook 472a7812ae4Spbrook idx = tcg_temp_new_internal(TCG_TYPE_I64, temp_local); 473a7812ae4Spbrook return MAKE_TCGV_I64(idx); 474a7812ae4Spbrook } 475a7812ae4Spbrook 476a7812ae4Spbrook static inline void tcg_temp_free_internal(int idx) 477c896fe29Sbellard { 478c896fe29Sbellard TCGContext *s = &tcg_ctx; 479c896fe29Sbellard TCGTemp *ts; 480641d5fbeSbellard int k; 481c896fe29Sbellard 48227bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG) 48327bfd83cSPeter Maydell s->temps_in_use--; 48427bfd83cSPeter Maydell if (s->temps_in_use < 0) { 48527bfd83cSPeter Maydell fprintf(stderr, "More temporaries freed than allocated!\n"); 48627bfd83cSPeter Maydell } 48727bfd83cSPeter Maydell #endif 48827bfd83cSPeter Maydell 489e8996ee0Sbellard assert(idx >= s->nb_globals && idx < s->nb_temps); 490c896fe29Sbellard ts = &s->temps[idx]; 491e8996ee0Sbellard assert(ts->temp_allocated != 0); 492e8996ee0Sbellard ts->temp_allocated = 0; 493641d5fbeSbellard k = ts->base_type; 494641d5fbeSbellard if (ts->temp_local) 495641d5fbeSbellard k += TCG_TYPE_COUNT; 496641d5fbeSbellard ts->next_free_temp = s->first_free_temp[k]; 497641d5fbeSbellard s->first_free_temp[k] = idx; 498e8996ee0Sbellard } 499e8996ee0Sbellard 500a7812ae4Spbrook void tcg_temp_free_i32(TCGv_i32 arg) 501e8996ee0Sbellard { 502a7812ae4Spbrook tcg_temp_free_internal(GET_TCGV_I32(arg)); 503a7812ae4Spbrook } 504a7812ae4Spbrook 505a7812ae4Spbrook void tcg_temp_free_i64(TCGv_i64 arg) 506a7812ae4Spbrook { 507a7812ae4Spbrook tcg_temp_free_internal(GET_TCGV_I64(arg)); 508a7812ae4Spbrook } 509a7812ae4Spbrook 510a7812ae4Spbrook TCGv_i32 tcg_const_i32(int32_t val) 511a7812ae4Spbrook { 512a7812ae4Spbrook TCGv_i32 t0; 513a7812ae4Spbrook t0 = tcg_temp_new_i32(); 514e8996ee0Sbellard tcg_gen_movi_i32(t0, val); 515e8996ee0Sbellard return t0; 516c896fe29Sbellard } 517c896fe29Sbellard 518a7812ae4Spbrook TCGv_i64 tcg_const_i64(int64_t val) 519c896fe29Sbellard { 520a7812ae4Spbrook TCGv_i64 t0; 521a7812ae4Spbrook t0 = tcg_temp_new_i64(); 522e8996ee0Sbellard tcg_gen_movi_i64(t0, val); 523e8996ee0Sbellard return t0; 524c896fe29Sbellard } 525c896fe29Sbellard 526a7812ae4Spbrook TCGv_i32 tcg_const_local_i32(int32_t val) 527bdffd4a9Saurel32 { 528a7812ae4Spbrook TCGv_i32 t0; 529a7812ae4Spbrook t0 = tcg_temp_local_new_i32(); 530bdffd4a9Saurel32 tcg_gen_movi_i32(t0, val); 531bdffd4a9Saurel32 return t0; 532bdffd4a9Saurel32 } 533bdffd4a9Saurel32 534a7812ae4Spbrook TCGv_i64 tcg_const_local_i64(int64_t val) 535bdffd4a9Saurel32 { 536a7812ae4Spbrook TCGv_i64 t0; 537a7812ae4Spbrook t0 = tcg_temp_local_new_i64(); 538bdffd4a9Saurel32 tcg_gen_movi_i64(t0, val); 539bdffd4a9Saurel32 return t0; 540bdffd4a9Saurel32 } 541bdffd4a9Saurel32 54227bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG) 54327bfd83cSPeter Maydell void tcg_clear_temp_count(void) 54427bfd83cSPeter Maydell { 54527bfd83cSPeter Maydell TCGContext *s = &tcg_ctx; 54627bfd83cSPeter Maydell s->temps_in_use = 0; 54727bfd83cSPeter Maydell } 54827bfd83cSPeter Maydell 54927bfd83cSPeter Maydell int tcg_check_temp_count(void) 55027bfd83cSPeter Maydell { 55127bfd83cSPeter Maydell TCGContext *s = &tcg_ctx; 55227bfd83cSPeter Maydell if (s->temps_in_use) { 55327bfd83cSPeter Maydell /* Clear the count so that we don't give another 55427bfd83cSPeter Maydell * warning immediately next time around. 55527bfd83cSPeter Maydell */ 55627bfd83cSPeter Maydell s->temps_in_use = 0; 55727bfd83cSPeter Maydell return 1; 55827bfd83cSPeter Maydell } 55927bfd83cSPeter Maydell return 0; 56027bfd83cSPeter Maydell } 56127bfd83cSPeter Maydell #endif 56227bfd83cSPeter Maydell 563c896fe29Sbellard void tcg_register_helper(void *func, const char *name) 564c896fe29Sbellard { 565c896fe29Sbellard TCGContext *s = &tcg_ctx; 566c896fe29Sbellard int n; 567c896fe29Sbellard if ((s->nb_helpers + 1) > s->allocated_helpers) { 568c896fe29Sbellard n = s->allocated_helpers; 569c896fe29Sbellard if (n == 0) { 570c896fe29Sbellard n = 4; 571c896fe29Sbellard } else { 572c896fe29Sbellard n *= 2; 573c896fe29Sbellard } 574c896fe29Sbellard s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo)); 575c896fe29Sbellard s->allocated_helpers = n; 576c896fe29Sbellard } 5774dc81f28Sbellard s->helpers[s->nb_helpers].func = (tcg_target_ulong)func; 578c896fe29Sbellard s->helpers[s->nb_helpers].name = name; 579c896fe29Sbellard s->nb_helpers++; 580c896fe29Sbellard } 581c896fe29Sbellard 58239cf05d3Sbellard /* Note: we convert the 64 bit args to 32 bit and do some alignment 58339cf05d3Sbellard and endian swap. Maybe it would be better to do the alignment 58439cf05d3Sbellard and endian swap in tcg_reg_alloc_call(). */ 585a7812ae4Spbrook void tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags, 586a7812ae4Spbrook int sizemask, TCGArg ret, int nargs, TCGArg *args) 587c896fe29Sbellard { 588568fffe3SChristophe Fergeau #if defined(TCG_TARGET_I386) && TCG_TARGET_REG_BITS < 64 589a7812ae4Spbrook int call_type; 5909678d950SBlue Swirl #endif 591a7812ae4Spbrook int i; 592a7812ae4Spbrook int real_args; 593a7812ae4Spbrook int nb_rets; 594a7812ae4Spbrook TCGArg *nparam; 5952bece2c8SRichard Henderson 5962bece2c8SRichard Henderson #if defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64 5972bece2c8SRichard Henderson for (i = 0; i < nargs; ++i) { 5982bece2c8SRichard Henderson int is_64bit = sizemask & (1 << (i+1)*2); 5992bece2c8SRichard Henderson int is_signed = sizemask & (2 << (i+1)*2); 6002bece2c8SRichard Henderson if (!is_64bit) { 6012bece2c8SRichard Henderson TCGv_i64 temp = tcg_temp_new_i64(); 6022bece2c8SRichard Henderson TCGv_i64 orig = MAKE_TCGV_I64(args[i]); 6032bece2c8SRichard Henderson if (is_signed) { 6042bece2c8SRichard Henderson tcg_gen_ext32s_i64(temp, orig); 6052bece2c8SRichard Henderson } else { 6062bece2c8SRichard Henderson tcg_gen_ext32u_i64(temp, orig); 6072bece2c8SRichard Henderson } 6082bece2c8SRichard Henderson args[i] = GET_TCGV_I64(temp); 6092bece2c8SRichard Henderson } 6102bece2c8SRichard Henderson } 6112bece2c8SRichard Henderson #endif /* TCG_TARGET_EXTEND_ARGS */ 6122bece2c8SRichard Henderson 613a7812ae4Spbrook *gen_opc_ptr++ = INDEX_op_call; 614a7812ae4Spbrook nparam = gen_opparam_ptr++; 615568fffe3SChristophe Fergeau #if defined(TCG_TARGET_I386) && TCG_TARGET_REG_BITS < 64 616c896fe29Sbellard call_type = (flags & TCG_CALL_TYPE_MASK); 6179678d950SBlue Swirl #endif 618a7812ae4Spbrook if (ret != TCG_CALL_DUMMY_ARG) { 619a7812ae4Spbrook #if TCG_TARGET_REG_BITS < 64 620a7812ae4Spbrook if (sizemask & 1) { 621a7812ae4Spbrook #ifdef TCG_TARGET_WORDS_BIGENDIAN 622a7812ae4Spbrook *gen_opparam_ptr++ = ret + 1; 623a7812ae4Spbrook *gen_opparam_ptr++ = ret; 624a7812ae4Spbrook #else 625a7812ae4Spbrook *gen_opparam_ptr++ = ret; 626a7812ae4Spbrook *gen_opparam_ptr++ = ret + 1; 627a7812ae4Spbrook #endif 628a7812ae4Spbrook nb_rets = 2; 629a7812ae4Spbrook } else 630a7812ae4Spbrook #endif 631a7812ae4Spbrook { 632a7812ae4Spbrook *gen_opparam_ptr++ = ret; 633a7812ae4Spbrook nb_rets = 1; 634a7812ae4Spbrook } 635a7812ae4Spbrook } else { 636a7812ae4Spbrook nb_rets = 0; 637a7812ae4Spbrook } 638a7812ae4Spbrook real_args = 0; 639a7812ae4Spbrook for (i = 0; i < nargs; i++) { 640a7812ae4Spbrook #if TCG_TARGET_REG_BITS < 64 6412bece2c8SRichard Henderson int is_64bit = sizemask & (1 << (i+1)*2); 6422bece2c8SRichard Henderson if (is_64bit) { 643c896fe29Sbellard #ifdef TCG_TARGET_I386 644c896fe29Sbellard /* REGPARM case: if the third parameter is 64 bit, it is 645c896fe29Sbellard allocated on the stack */ 646a7812ae4Spbrook if (i == 2 && call_type == TCG_CALL_TYPE_REGPARM) { 647c896fe29Sbellard call_type = TCG_CALL_TYPE_REGPARM_2; 648c896fe29Sbellard flags = (flags & ~TCG_CALL_TYPE_MASK) | call_type; 649c896fe29Sbellard } 650a7812ae4Spbrook #endif 65139cf05d3Sbellard #ifdef TCG_TARGET_CALL_ALIGN_ARGS 65239cf05d3Sbellard /* some targets want aligned 64 bit args */ 653ebd486d5Smalc if (real_args & 1) { 654a7812ae4Spbrook *gen_opparam_ptr++ = TCG_CALL_DUMMY_ARG; 655ebd486d5Smalc real_args++; 65639cf05d3Sbellard } 65739cf05d3Sbellard #endif 6583f90f252SRichard Henderson /* If stack grows up, then we will be placing successive 6593f90f252SRichard Henderson arguments at lower addresses, which means we need to 6603f90f252SRichard Henderson reverse the order compared to how we would normally 6613f90f252SRichard Henderson treat either big or little-endian. For those arguments 6623f90f252SRichard Henderson that will wind up in registers, this still works for 6633f90f252SRichard Henderson HPPA (the only current STACK_GROWSUP target) since the 6643f90f252SRichard Henderson argument registers are *also* allocated in decreasing 6653f90f252SRichard Henderson order. If another such target is added, this logic may 6663f90f252SRichard Henderson have to get more complicated to differentiate between 6673f90f252SRichard Henderson stack arguments and register arguments. */ 6683f90f252SRichard Henderson #if defined(TCG_TARGET_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP) 669a7812ae4Spbrook *gen_opparam_ptr++ = args[i] + 1; 670a7812ae4Spbrook *gen_opparam_ptr++ = args[i]; 671c896fe29Sbellard #else 672a7812ae4Spbrook *gen_opparam_ptr++ = args[i]; 673a7812ae4Spbrook *gen_opparam_ptr++ = args[i] + 1; 674c896fe29Sbellard #endif 675a7812ae4Spbrook real_args += 2; 6762bece2c8SRichard Henderson continue; 6772bece2c8SRichard Henderson } 6782bece2c8SRichard Henderson #endif /* TCG_TARGET_REG_BITS < 64 */ 6792bece2c8SRichard Henderson 680a7812ae4Spbrook *gen_opparam_ptr++ = args[i]; 681a7812ae4Spbrook real_args++; 682c896fe29Sbellard } 683a7812ae4Spbrook *gen_opparam_ptr++ = GET_TCGV_PTR(func); 684a7812ae4Spbrook 685a7812ae4Spbrook *gen_opparam_ptr++ = flags; 686a7812ae4Spbrook 687a7812ae4Spbrook *nparam = (nb_rets << 16) | (real_args + 1); 688a7812ae4Spbrook 689a7812ae4Spbrook /* total parameters, needed to go backward in the instruction stream */ 690a7812ae4Spbrook *gen_opparam_ptr++ = 1 + nb_rets + real_args + 3; 6912bece2c8SRichard Henderson 6922bece2c8SRichard Henderson #if defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64 6932bece2c8SRichard Henderson for (i = 0; i < nargs; ++i) { 6942bece2c8SRichard Henderson int is_64bit = sizemask & (1 << (i+1)*2); 6952bece2c8SRichard Henderson if (!is_64bit) { 6962bece2c8SRichard Henderson TCGv_i64 temp = MAKE_TCGV_I64(args[i]); 6972bece2c8SRichard Henderson tcg_temp_free_i64(temp); 6982bece2c8SRichard Henderson } 6992bece2c8SRichard Henderson } 7002bece2c8SRichard Henderson #endif /* TCG_TARGET_EXTEND_ARGS */ 701a7812ae4Spbrook } 702c896fe29Sbellard 703ac56dd48Spbrook #if TCG_TARGET_REG_BITS == 32 704a7812ae4Spbrook void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1, 705c896fe29Sbellard int c, int right, int arith) 706c896fe29Sbellard { 707cf60bce4Sbellard if (c == 0) { 708a7812ae4Spbrook tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg1)); 709cf60bce4Sbellard tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1)); 710cf60bce4Sbellard } else if (c >= 32) { 711c896fe29Sbellard c -= 32; 712c896fe29Sbellard if (right) { 713c896fe29Sbellard if (arith) { 714a7812ae4Spbrook tcg_gen_sari_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c); 715ac56dd48Spbrook tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31); 716c896fe29Sbellard } else { 717a7812ae4Spbrook tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c); 718ac56dd48Spbrook tcg_gen_movi_i32(TCGV_HIGH(ret), 0); 719c896fe29Sbellard } 720c896fe29Sbellard } else { 721a7812ae4Spbrook tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_LOW(arg1), c); 722a7812ae4Spbrook tcg_gen_movi_i32(TCGV_LOW(ret), 0); 723c896fe29Sbellard } 724c896fe29Sbellard } else { 725a7812ae4Spbrook TCGv_i32 t0, t1; 726c896fe29Sbellard 727a7812ae4Spbrook t0 = tcg_temp_new_i32(); 728a7812ae4Spbrook t1 = tcg_temp_new_i32(); 729c896fe29Sbellard if (right) { 730ac56dd48Spbrook tcg_gen_shli_i32(t0, TCGV_HIGH(arg1), 32 - c); 731c896fe29Sbellard if (arith) 732ac56dd48Spbrook tcg_gen_sari_i32(t1, TCGV_HIGH(arg1), c); 733c896fe29Sbellard else 734ac56dd48Spbrook tcg_gen_shri_i32(t1, TCGV_HIGH(arg1), c); 735a7812ae4Spbrook tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_LOW(arg1), c); 736a7812ae4Spbrook tcg_gen_or_i32(TCGV_LOW(ret), TCGV_LOW(ret), t0); 737ac56dd48Spbrook tcg_gen_mov_i32(TCGV_HIGH(ret), t1); 738c896fe29Sbellard } else { 739a7812ae4Spbrook tcg_gen_shri_i32(t0, TCGV_LOW(arg1), 32 - c); 740c896fe29Sbellard /* Note: ret can be the same as arg1, so we use t1 */ 741a7812ae4Spbrook tcg_gen_shli_i32(t1, TCGV_LOW(arg1), c); 742ac56dd48Spbrook tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c); 743ac56dd48Spbrook tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t0); 744a7812ae4Spbrook tcg_gen_mov_i32(TCGV_LOW(ret), t1); 745c896fe29Sbellard } 746a7812ae4Spbrook tcg_temp_free_i32(t0); 747a7812ae4Spbrook tcg_temp_free_i32(t1); 748c896fe29Sbellard } 749c896fe29Sbellard } 750ac56dd48Spbrook #endif 751c896fe29Sbellard 752be210acbSRichard Henderson 7538fcd3692Sblueswir1 static void tcg_reg_alloc_start(TCGContext *s) 754c896fe29Sbellard { 755c896fe29Sbellard int i; 756c896fe29Sbellard TCGTemp *ts; 757c896fe29Sbellard for(i = 0; i < s->nb_globals; i++) { 758c896fe29Sbellard ts = &s->temps[i]; 759c896fe29Sbellard if (ts->fixed_reg) { 760c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 761c896fe29Sbellard } else { 762c896fe29Sbellard ts->val_type = TEMP_VAL_MEM; 763c896fe29Sbellard } 764c896fe29Sbellard } 765e8996ee0Sbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 766e8996ee0Sbellard ts = &s->temps[i]; 767e8996ee0Sbellard ts->val_type = TEMP_VAL_DEAD; 768e8996ee0Sbellard ts->mem_allocated = 0; 769e8996ee0Sbellard ts->fixed_reg = 0; 770e8996ee0Sbellard } 771c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 772c896fe29Sbellard s->reg_to_temp[i] = -1; 773c896fe29Sbellard } 774c896fe29Sbellard } 775c896fe29Sbellard 776ac56dd48Spbrook static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size, 777ac56dd48Spbrook int idx) 778c896fe29Sbellard { 779c896fe29Sbellard TCGTemp *ts; 780ac56dd48Spbrook 781ac56dd48Spbrook ts = &s->temps[idx]; 782ac56dd48Spbrook if (idx < s->nb_globals) { 783ac56dd48Spbrook pstrcpy(buf, buf_size, ts->name); 784c896fe29Sbellard } else { 785641d5fbeSbellard if (ts->temp_local) 786641d5fbeSbellard snprintf(buf, buf_size, "loc%d", idx - s->nb_globals); 787641d5fbeSbellard else 788ac56dd48Spbrook snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals); 789c896fe29Sbellard } 790c896fe29Sbellard return buf; 791c896fe29Sbellard } 792c896fe29Sbellard 793a7812ae4Spbrook char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg) 794ac56dd48Spbrook { 795a7812ae4Spbrook return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I32(arg)); 796a7812ae4Spbrook } 797a7812ae4Spbrook 798a7812ae4Spbrook char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg) 799a7812ae4Spbrook { 800a810a2deSblueswir1 return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I64(arg)); 801ac56dd48Spbrook } 802ac56dd48Spbrook 803e8996ee0Sbellard static int helper_cmp(const void *p1, const void *p2) 804e8996ee0Sbellard { 805e8996ee0Sbellard const TCGHelperInfo *th1 = p1; 806e8996ee0Sbellard const TCGHelperInfo *th2 = p2; 807e8996ee0Sbellard if (th1->func < th2->func) 808e8996ee0Sbellard return -1; 809e8996ee0Sbellard else if (th1->func == th2->func) 810e8996ee0Sbellard return 0; 811e8996ee0Sbellard else 812e8996ee0Sbellard return 1; 813e8996ee0Sbellard } 814e8996ee0Sbellard 815e8996ee0Sbellard /* find helper definition (Note: A hash table would be better) */ 8164dc81f28Sbellard static TCGHelperInfo *tcg_find_helper(TCGContext *s, tcg_target_ulong val) 8174dc81f28Sbellard { 818e8996ee0Sbellard int m, m_min, m_max; 819e8996ee0Sbellard TCGHelperInfo *th; 820e8996ee0Sbellard tcg_target_ulong v; 821e8996ee0Sbellard 822e8996ee0Sbellard if (unlikely(!s->helpers_sorted)) { 823e8996ee0Sbellard qsort(s->helpers, s->nb_helpers, sizeof(TCGHelperInfo), 824e8996ee0Sbellard helper_cmp); 825e8996ee0Sbellard s->helpers_sorted = 1; 826e8996ee0Sbellard } 827e8996ee0Sbellard 828e8996ee0Sbellard /* binary search */ 829e8996ee0Sbellard m_min = 0; 830e8996ee0Sbellard m_max = s->nb_helpers - 1; 831e8996ee0Sbellard while (m_min <= m_max) { 832e8996ee0Sbellard m = (m_min + m_max) >> 1; 833e8996ee0Sbellard th = &s->helpers[m]; 834e8996ee0Sbellard v = th->func; 835e8996ee0Sbellard if (v == val) 836e8996ee0Sbellard return th; 837e8996ee0Sbellard else if (val < v) { 838e8996ee0Sbellard m_max = m - 1; 839e8996ee0Sbellard } else { 840e8996ee0Sbellard m_min = m + 1; 841e8996ee0Sbellard } 8424dc81f28Sbellard } 8434dc81f28Sbellard return NULL; 8444dc81f28Sbellard } 8454dc81f28Sbellard 846f48f3edeSblueswir1 static const char * const cond_name[] = 847f48f3edeSblueswir1 { 848f48f3edeSblueswir1 [TCG_COND_EQ] = "eq", 849f48f3edeSblueswir1 [TCG_COND_NE] = "ne", 850f48f3edeSblueswir1 [TCG_COND_LT] = "lt", 851f48f3edeSblueswir1 [TCG_COND_GE] = "ge", 852f48f3edeSblueswir1 [TCG_COND_LE] = "le", 853f48f3edeSblueswir1 [TCG_COND_GT] = "gt", 854f48f3edeSblueswir1 [TCG_COND_LTU] = "ltu", 855f48f3edeSblueswir1 [TCG_COND_GEU] = "geu", 856f48f3edeSblueswir1 [TCG_COND_LEU] = "leu", 857f48f3edeSblueswir1 [TCG_COND_GTU] = "gtu" 858f48f3edeSblueswir1 }; 859f48f3edeSblueswir1 860c896fe29Sbellard void tcg_dump_ops(TCGContext *s, FILE *outfile) 861c896fe29Sbellard { 862c896fe29Sbellard const uint16_t *opc_ptr; 863c896fe29Sbellard const TCGArg *args; 864c896fe29Sbellard TCGArg arg; 865a9751609SRichard Henderson TCGOpcode c; 866a9751609SRichard Henderson int i, k, nb_oargs, nb_iargs, nb_cargs, first_insn; 867c896fe29Sbellard const TCGOpDef *def; 868c896fe29Sbellard char buf[128]; 869c896fe29Sbellard 8707e4597d7Sbellard first_insn = 1; 871c896fe29Sbellard opc_ptr = gen_opc_buf; 872c896fe29Sbellard args = gen_opparam_buf; 873c896fe29Sbellard while (opc_ptr < gen_opc_ptr) { 874c896fe29Sbellard c = *opc_ptr++; 875c896fe29Sbellard def = &tcg_op_defs[c]; 8767e4597d7Sbellard if (c == INDEX_op_debug_insn_start) { 8777e4597d7Sbellard uint64_t pc; 8787e4597d7Sbellard #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS 8797e4597d7Sbellard pc = ((uint64_t)args[1] << 32) | args[0]; 8807e4597d7Sbellard #else 8817e4597d7Sbellard pc = args[0]; 8827e4597d7Sbellard #endif 8837e4597d7Sbellard if (!first_insn) 8847e4597d7Sbellard fprintf(outfile, "\n"); 8857e4597d7Sbellard fprintf(outfile, " ---- 0x%" PRIx64, pc); 8867e4597d7Sbellard first_insn = 0; 8877e4597d7Sbellard nb_oargs = def->nb_oargs; 8887e4597d7Sbellard nb_iargs = def->nb_iargs; 8897e4597d7Sbellard nb_cargs = def->nb_cargs; 8907e4597d7Sbellard } else if (c == INDEX_op_call) { 891c896fe29Sbellard TCGArg arg; 8924dc81f28Sbellard 893c896fe29Sbellard /* variable number of arguments */ 894c896fe29Sbellard arg = *args++; 895c896fe29Sbellard nb_oargs = arg >> 16; 896c896fe29Sbellard nb_iargs = arg & 0xffff; 897c896fe29Sbellard nb_cargs = def->nb_cargs; 898b03cce8eSbellard 8997e4597d7Sbellard fprintf(outfile, " %s ", def->name); 9007e4597d7Sbellard 901b03cce8eSbellard /* function name */ 902b03cce8eSbellard fprintf(outfile, "%s", 903e8996ee0Sbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + nb_iargs - 1])); 904b03cce8eSbellard /* flags */ 905b03cce8eSbellard fprintf(outfile, ",$0x%" TCG_PRIlx, 906b03cce8eSbellard args[nb_oargs + nb_iargs]); 907b03cce8eSbellard /* nb out args */ 908b03cce8eSbellard fprintf(outfile, ",$%d", nb_oargs); 909b03cce8eSbellard for(i = 0; i < nb_oargs; i++) { 910b03cce8eSbellard fprintf(outfile, ","); 911b03cce8eSbellard fprintf(outfile, "%s", 912b03cce8eSbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[i])); 913b03cce8eSbellard } 914b03cce8eSbellard for(i = 0; i < (nb_iargs - 1); i++) { 915b03cce8eSbellard fprintf(outfile, ","); 91639cf05d3Sbellard if (args[nb_oargs + i] == TCG_CALL_DUMMY_ARG) { 91739cf05d3Sbellard fprintf(outfile, "<dummy>"); 91839cf05d3Sbellard } else { 919b03cce8eSbellard fprintf(outfile, "%s", 920b03cce8eSbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + i])); 921b03cce8eSbellard } 92239cf05d3Sbellard } 923e8996ee0Sbellard } else if (c == INDEX_op_movi_i32 924e8996ee0Sbellard #if TCG_TARGET_REG_BITS == 64 925e8996ee0Sbellard || c == INDEX_op_movi_i64 926e8996ee0Sbellard #endif 927e8996ee0Sbellard ) { 928e8996ee0Sbellard tcg_target_ulong val; 929e8996ee0Sbellard TCGHelperInfo *th; 930e8996ee0Sbellard 931e8996ee0Sbellard nb_oargs = def->nb_oargs; 932e8996ee0Sbellard nb_iargs = def->nb_iargs; 933e8996ee0Sbellard nb_cargs = def->nb_cargs; 934e8996ee0Sbellard fprintf(outfile, " %s %s,$", def->name, 935e8996ee0Sbellard tcg_get_arg_str_idx(s, buf, sizeof(buf), args[0])); 936e8996ee0Sbellard val = args[1]; 937e8996ee0Sbellard th = tcg_find_helper(s, val); 938e8996ee0Sbellard if (th) { 9393e9a474eSaurel32 fprintf(outfile, "%s", th->name); 940e8996ee0Sbellard } else { 941e8996ee0Sbellard if (c == INDEX_op_movi_i32) 942e8996ee0Sbellard fprintf(outfile, "0x%x", (uint32_t)val); 943e8996ee0Sbellard else 944e8996ee0Sbellard fprintf(outfile, "0x%" PRIx64 , (uint64_t)val); 945e8996ee0Sbellard } 946b03cce8eSbellard } else { 9477e4597d7Sbellard fprintf(outfile, " %s ", def->name); 948b03cce8eSbellard if (c == INDEX_op_nopn) { 949c896fe29Sbellard /* variable number of arguments */ 950c896fe29Sbellard nb_cargs = *args; 951c896fe29Sbellard nb_oargs = 0; 952c896fe29Sbellard nb_iargs = 0; 953c896fe29Sbellard } else { 954c896fe29Sbellard nb_oargs = def->nb_oargs; 955c896fe29Sbellard nb_iargs = def->nb_iargs; 956c896fe29Sbellard nb_cargs = def->nb_cargs; 957c896fe29Sbellard } 958c896fe29Sbellard 959c896fe29Sbellard k = 0; 960c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 961c896fe29Sbellard if (k != 0) 962c896fe29Sbellard fprintf(outfile, ","); 963ac56dd48Spbrook fprintf(outfile, "%s", 964ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++])); 965c896fe29Sbellard } 966c896fe29Sbellard for(i = 0; i < nb_iargs; i++) { 967c896fe29Sbellard if (k != 0) 968c896fe29Sbellard fprintf(outfile, ","); 969ac56dd48Spbrook fprintf(outfile, "%s", 970ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++])); 971c896fe29Sbellard } 972be210acbSRichard Henderson switch (c) { 973be210acbSRichard Henderson case INDEX_op_brcond_i32: 974f48f3edeSblueswir1 #if TCG_TARGET_REG_BITS == 32 975be210acbSRichard Henderson case INDEX_op_brcond2_i32: 976f48f3edeSblueswir1 #elif TCG_TARGET_REG_BITS == 64 977be210acbSRichard Henderson case INDEX_op_brcond_i64: 978f48f3edeSblueswir1 #endif 979be210acbSRichard Henderson case INDEX_op_setcond_i32: 980be210acbSRichard Henderson #if TCG_TARGET_REG_BITS == 32 981be210acbSRichard Henderson case INDEX_op_setcond2_i32: 982be210acbSRichard Henderson #elif TCG_TARGET_REG_BITS == 64 983be210acbSRichard Henderson case INDEX_op_setcond_i64: 984be210acbSRichard Henderson #endif 985f48f3edeSblueswir1 if (args[k] < ARRAY_SIZE(cond_name) && cond_name[args[k]]) 986f48f3edeSblueswir1 fprintf(outfile, ",%s", cond_name[args[k++]]); 987f48f3edeSblueswir1 else 988f48f3edeSblueswir1 fprintf(outfile, ",$0x%" TCG_PRIlx, args[k++]); 989f48f3edeSblueswir1 i = 1; 990be210acbSRichard Henderson break; 991be210acbSRichard Henderson default: 992f48f3edeSblueswir1 i = 0; 993be210acbSRichard Henderson break; 994be210acbSRichard Henderson } 995f48f3edeSblueswir1 for(; i < nb_cargs; i++) { 996c896fe29Sbellard if (k != 0) 997c896fe29Sbellard fprintf(outfile, ","); 998c896fe29Sbellard arg = args[k++]; 999c896fe29Sbellard fprintf(outfile, "$0x%" TCG_PRIlx, arg); 1000c896fe29Sbellard } 1001b03cce8eSbellard } 1002c896fe29Sbellard fprintf(outfile, "\n"); 1003c896fe29Sbellard args += nb_iargs + nb_oargs + nb_cargs; 1004c896fe29Sbellard } 1005c896fe29Sbellard } 1006c896fe29Sbellard 1007c896fe29Sbellard /* we give more priority to constraints with less registers */ 1008c896fe29Sbellard static int get_constraint_priority(const TCGOpDef *def, int k) 1009c896fe29Sbellard { 1010c896fe29Sbellard const TCGArgConstraint *arg_ct; 1011c896fe29Sbellard 1012c896fe29Sbellard int i, n; 1013c896fe29Sbellard arg_ct = &def->args_ct[k]; 1014c896fe29Sbellard if (arg_ct->ct & TCG_CT_ALIAS) { 1015c896fe29Sbellard /* an alias is equivalent to a single register */ 1016c896fe29Sbellard n = 1; 1017c896fe29Sbellard } else { 1018c896fe29Sbellard if (!(arg_ct->ct & TCG_CT_REG)) 1019c896fe29Sbellard return 0; 1020c896fe29Sbellard n = 0; 1021c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 1022c896fe29Sbellard if (tcg_regset_test_reg(arg_ct->u.regs, i)) 1023c896fe29Sbellard n++; 1024c896fe29Sbellard } 1025c896fe29Sbellard } 1026c896fe29Sbellard return TCG_TARGET_NB_REGS - n + 1; 1027c896fe29Sbellard } 1028c896fe29Sbellard 1029c896fe29Sbellard /* sort from highest priority to lowest */ 1030c896fe29Sbellard static void sort_constraints(TCGOpDef *def, int start, int n) 1031c896fe29Sbellard { 1032c896fe29Sbellard int i, j, p1, p2, tmp; 1033c896fe29Sbellard 1034c896fe29Sbellard for(i = 0; i < n; i++) 1035c896fe29Sbellard def->sorted_args[start + i] = start + i; 1036c896fe29Sbellard if (n <= 1) 1037c896fe29Sbellard return; 1038c896fe29Sbellard for(i = 0; i < n - 1; i++) { 1039c896fe29Sbellard for(j = i + 1; j < n; j++) { 1040c896fe29Sbellard p1 = get_constraint_priority(def, def->sorted_args[start + i]); 1041c896fe29Sbellard p2 = get_constraint_priority(def, def->sorted_args[start + j]); 1042c896fe29Sbellard if (p1 < p2) { 1043c896fe29Sbellard tmp = def->sorted_args[start + i]; 1044c896fe29Sbellard def->sorted_args[start + i] = def->sorted_args[start + j]; 1045c896fe29Sbellard def->sorted_args[start + j] = tmp; 1046c896fe29Sbellard } 1047c896fe29Sbellard } 1048c896fe29Sbellard } 1049c896fe29Sbellard } 1050c896fe29Sbellard 1051c896fe29Sbellard void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs) 1052c896fe29Sbellard { 1053a9751609SRichard Henderson TCGOpcode op; 1054c896fe29Sbellard TCGOpDef *def; 1055c896fe29Sbellard const char *ct_str; 1056c896fe29Sbellard int i, nb_args; 1057c896fe29Sbellard 1058c896fe29Sbellard for(;;) { 1059a9751609SRichard Henderson if (tdefs->op == (TCGOpcode)-1) 1060c896fe29Sbellard break; 1061c896fe29Sbellard op = tdefs->op; 1062c3b08d0eSStefan Weil assert((unsigned)op < NB_OPS); 1063c896fe29Sbellard def = &tcg_op_defs[op]; 1064c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG) 1065c68aaa18SStefan Weil /* Duplicate entry in op definitions? */ 1066c68aaa18SStefan Weil assert(!def->used); 1067c68aaa18SStefan Weil def->used = 1; 1068c68aaa18SStefan Weil #endif 1069c896fe29Sbellard nb_args = def->nb_iargs + def->nb_oargs; 1070c896fe29Sbellard for(i = 0; i < nb_args; i++) { 1071c896fe29Sbellard ct_str = tdefs->args_ct_str[i]; 1072c68aaa18SStefan Weil /* Incomplete TCGTargetOpDef entry? */ 1073c68aaa18SStefan Weil assert(ct_str != NULL); 1074c896fe29Sbellard tcg_regset_clear(def->args_ct[i].u.regs); 1075c896fe29Sbellard def->args_ct[i].ct = 0; 1076c896fe29Sbellard if (ct_str[0] >= '0' && ct_str[0] <= '9') { 1077c896fe29Sbellard int oarg; 1078c896fe29Sbellard oarg = ct_str[0] - '0'; 1079c896fe29Sbellard assert(oarg < def->nb_oargs); 1080c896fe29Sbellard assert(def->args_ct[oarg].ct & TCG_CT_REG); 1081c896fe29Sbellard /* TCG_CT_ALIAS is for the output arguments. The input 10825ff9d6a4Sbellard argument is tagged with TCG_CT_IALIAS. */ 1083c896fe29Sbellard def->args_ct[i] = def->args_ct[oarg]; 10845ff9d6a4Sbellard def->args_ct[oarg].ct = TCG_CT_ALIAS; 10855ff9d6a4Sbellard def->args_ct[oarg].alias_index = i; 1086c896fe29Sbellard def->args_ct[i].ct |= TCG_CT_IALIAS; 10875ff9d6a4Sbellard def->args_ct[i].alias_index = oarg; 1088c896fe29Sbellard } else { 1089c896fe29Sbellard for(;;) { 1090c896fe29Sbellard if (*ct_str == '\0') 1091c896fe29Sbellard break; 1092c896fe29Sbellard switch(*ct_str) { 1093c896fe29Sbellard case 'i': 1094c896fe29Sbellard def->args_ct[i].ct |= TCG_CT_CONST; 1095c896fe29Sbellard ct_str++; 1096c896fe29Sbellard break; 1097c896fe29Sbellard default: 1098c896fe29Sbellard if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) { 1099c896fe29Sbellard fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n", 1100c896fe29Sbellard ct_str, i, def->name); 1101c896fe29Sbellard exit(1); 1102c896fe29Sbellard } 1103c896fe29Sbellard } 1104c896fe29Sbellard } 1105c896fe29Sbellard } 1106c896fe29Sbellard } 1107c896fe29Sbellard 1108c68aaa18SStefan Weil /* TCGTargetOpDef entry with too much information? */ 1109c68aaa18SStefan Weil assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL); 1110c68aaa18SStefan Weil 1111c896fe29Sbellard /* sort the constraints (XXX: this is just an heuristic) */ 1112c896fe29Sbellard sort_constraints(def, 0, def->nb_oargs); 1113c896fe29Sbellard sort_constraints(def, def->nb_oargs, def->nb_iargs); 1114c896fe29Sbellard 1115c896fe29Sbellard #if 0 1116c896fe29Sbellard { 1117c896fe29Sbellard int i; 1118c896fe29Sbellard 1119c896fe29Sbellard printf("%s: sorted=", def->name); 1120c896fe29Sbellard for(i = 0; i < def->nb_oargs + def->nb_iargs; i++) 1121c896fe29Sbellard printf(" %d", def->sorted_args[i]); 1122c896fe29Sbellard printf("\n"); 1123c896fe29Sbellard } 1124c896fe29Sbellard #endif 1125c896fe29Sbellard tdefs++; 1126c896fe29Sbellard } 1127c896fe29Sbellard 1128c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG) 1129a9751609SRichard Henderson i = 0; 1130c68aaa18SStefan Weil for (op = 0; op < ARRAY_SIZE(tcg_op_defs); op++) { 1131*f412c762SRichard Henderson const TCGOpDef *def = &tcg_op_defs[op]; 1132*f412c762SRichard Henderson if (op < INDEX_op_call 1133*f412c762SRichard Henderson || op == INDEX_op_debug_insn_start 1134*f412c762SRichard Henderson || (def->flags & TCG_OPF_NOT_PRESENT)) { 1135c68aaa18SStefan Weil /* Wrong entry in op definitions? */ 1136*f412c762SRichard Henderson if (def->used) { 1137*f412c762SRichard Henderson fprintf(stderr, "Invalid op definition for %s\n", def->name); 1138a9751609SRichard Henderson i = 1; 1139a9751609SRichard Henderson } 1140c68aaa18SStefan Weil } else { 1141c68aaa18SStefan Weil /* Missing entry in op definitions? */ 1142*f412c762SRichard Henderson if (!def->used) { 1143*f412c762SRichard Henderson fprintf(stderr, "Missing op definition for %s\n", def->name); 1144a9751609SRichard Henderson i = 1; 1145c68aaa18SStefan Weil } 1146c68aaa18SStefan Weil } 1147a9751609SRichard Henderson } 1148a9751609SRichard Henderson if (i == 1) { 1149a9751609SRichard Henderson tcg_abort(); 1150a9751609SRichard Henderson } 1151c68aaa18SStefan Weil #endif 1152c896fe29Sbellard } 1153c896fe29Sbellard 1154c896fe29Sbellard #ifdef USE_LIVENESS_ANALYSIS 1155c896fe29Sbellard 1156c896fe29Sbellard /* set a nop for an operation using 'nb_args' */ 1157c896fe29Sbellard static inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr, 1158c896fe29Sbellard TCGArg *args, int nb_args) 1159c896fe29Sbellard { 1160c896fe29Sbellard if (nb_args == 0) { 1161c896fe29Sbellard *opc_ptr = INDEX_op_nop; 1162c896fe29Sbellard } else { 1163c896fe29Sbellard *opc_ptr = INDEX_op_nopn; 1164c896fe29Sbellard args[0] = nb_args; 1165c896fe29Sbellard args[nb_args - 1] = nb_args; 1166c896fe29Sbellard } 1167c896fe29Sbellard } 1168c896fe29Sbellard 1169641d5fbeSbellard /* liveness analysis: end of function: globals are live, temps are 1170641d5fbeSbellard dead. */ 1171641d5fbeSbellard /* XXX: at this stage, not used as there would be little gains because 1172641d5fbeSbellard most TBs end with a conditional jump. */ 1173641d5fbeSbellard static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps) 1174c896fe29Sbellard { 1175c896fe29Sbellard memset(dead_temps, 0, s->nb_globals); 1176c896fe29Sbellard memset(dead_temps + s->nb_globals, 1, s->nb_temps - s->nb_globals); 1177c896fe29Sbellard } 1178c896fe29Sbellard 1179641d5fbeSbellard /* liveness analysis: end of basic block: globals are live, temps are 1180641d5fbeSbellard dead, local temps are live. */ 1181641d5fbeSbellard static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps) 1182641d5fbeSbellard { 1183641d5fbeSbellard int i; 1184641d5fbeSbellard TCGTemp *ts; 1185641d5fbeSbellard 1186641d5fbeSbellard memset(dead_temps, 0, s->nb_globals); 1187641d5fbeSbellard ts = &s->temps[s->nb_globals]; 1188641d5fbeSbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 1189641d5fbeSbellard if (ts->temp_local) 1190641d5fbeSbellard dead_temps[i] = 0; 1191641d5fbeSbellard else 1192641d5fbeSbellard dead_temps[i] = 1; 1193641d5fbeSbellard ts++; 1194641d5fbeSbellard } 1195641d5fbeSbellard } 1196641d5fbeSbellard 1197866cb6cbSAurelien Jarno /* Liveness analysis : update the opc_dead_args array to tell if a 1198c896fe29Sbellard given input arguments is dead. Instructions updating dead 1199c896fe29Sbellard temporaries are removed. */ 12008fcd3692Sblueswir1 static void tcg_liveness_analysis(TCGContext *s) 1201c896fe29Sbellard { 1202a9751609SRichard Henderson int i, op_index, nb_args, nb_iargs, nb_oargs, arg, nb_ops; 1203a9751609SRichard Henderson TCGOpcode op; 1204c896fe29Sbellard TCGArg *args; 1205c896fe29Sbellard const TCGOpDef *def; 1206c896fe29Sbellard uint8_t *dead_temps; 1207866cb6cbSAurelien Jarno unsigned int dead_args; 1208c896fe29Sbellard 1209c896fe29Sbellard gen_opc_ptr++; /* skip end */ 1210c896fe29Sbellard 1211c896fe29Sbellard nb_ops = gen_opc_ptr - gen_opc_buf; 1212c896fe29Sbellard 1213866cb6cbSAurelien Jarno s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t)); 1214c896fe29Sbellard 1215c896fe29Sbellard dead_temps = tcg_malloc(s->nb_temps); 1216c896fe29Sbellard memset(dead_temps, 1, s->nb_temps); 1217c896fe29Sbellard 1218c896fe29Sbellard args = gen_opparam_ptr; 1219c896fe29Sbellard op_index = nb_ops - 1; 1220c896fe29Sbellard while (op_index >= 0) { 1221c896fe29Sbellard op = gen_opc_buf[op_index]; 1222c896fe29Sbellard def = &tcg_op_defs[op]; 1223c896fe29Sbellard switch(op) { 1224c896fe29Sbellard case INDEX_op_call: 1225c6e113f5Sbellard { 1226c6e113f5Sbellard int call_flags; 1227c6e113f5Sbellard 1228c896fe29Sbellard nb_args = args[-1]; 1229c896fe29Sbellard args -= nb_args; 1230c896fe29Sbellard nb_iargs = args[0] & 0xffff; 1231c896fe29Sbellard nb_oargs = args[0] >> 16; 1232c896fe29Sbellard args++; 1233c6e113f5Sbellard call_flags = args[nb_oargs + nb_iargs]; 1234c6e113f5Sbellard 1235c6e113f5Sbellard /* pure functions can be removed if their result is not 1236c6e113f5Sbellard used */ 1237c6e113f5Sbellard if (call_flags & TCG_CALL_PURE) { 1238c6e113f5Sbellard for(i = 0; i < nb_oargs; i++) { 1239c6e113f5Sbellard arg = args[i]; 1240c6e113f5Sbellard if (!dead_temps[arg]) 1241c6e113f5Sbellard goto do_not_remove_call; 1242c6e113f5Sbellard } 1243c6e113f5Sbellard tcg_set_nop(s, gen_opc_buf + op_index, 1244c6e113f5Sbellard args - 1, nb_args); 1245c6e113f5Sbellard } else { 1246c6e113f5Sbellard do_not_remove_call: 1247c896fe29Sbellard 1248c896fe29Sbellard /* output args are dead */ 12496b64b624SAurelien Jarno dead_args = 0; 1250c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1251c896fe29Sbellard arg = args[i]; 12526b64b624SAurelien Jarno if (dead_temps[arg]) { 12536b64b624SAurelien Jarno dead_args |= (1 << i); 12546b64b624SAurelien Jarno } 1255c896fe29Sbellard dead_temps[arg] = 1; 1256c896fe29Sbellard } 1257c896fe29Sbellard 1258b9c18f56Saurel32 if (!(call_flags & TCG_CALL_CONST)) { 1259c896fe29Sbellard /* globals are live (they may be used by the call) */ 1260c896fe29Sbellard memset(dead_temps, 0, s->nb_globals); 1261b9c18f56Saurel32 } 1262c896fe29Sbellard 1263c896fe29Sbellard /* input args are live */ 1264866cb6cbSAurelien Jarno for(i = nb_oargs; i < nb_iargs + nb_oargs; i++) { 1265866cb6cbSAurelien Jarno arg = args[i]; 126639cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1267c896fe29Sbellard if (dead_temps[arg]) { 1268866cb6cbSAurelien Jarno dead_args |= (1 << i); 1269c896fe29Sbellard } 1270c896fe29Sbellard dead_temps[arg] = 0; 1271c896fe29Sbellard } 127239cf05d3Sbellard } 1273866cb6cbSAurelien Jarno s->op_dead_args[op_index] = dead_args; 1274c6e113f5Sbellard } 1275c896fe29Sbellard args--; 1276c6e113f5Sbellard } 1277c896fe29Sbellard break; 1278c896fe29Sbellard case INDEX_op_set_label: 1279c896fe29Sbellard args--; 1280c896fe29Sbellard /* mark end of basic block */ 1281c896fe29Sbellard tcg_la_bb_end(s, dead_temps); 1282c896fe29Sbellard break; 12837e4597d7Sbellard case INDEX_op_debug_insn_start: 12847e4597d7Sbellard args -= def->nb_args; 12857e4597d7Sbellard break; 1286c896fe29Sbellard case INDEX_op_nopn: 1287c896fe29Sbellard nb_args = args[-1]; 1288c896fe29Sbellard args -= nb_args; 1289c896fe29Sbellard break; 12905ff9d6a4Sbellard case INDEX_op_discard: 12915ff9d6a4Sbellard args--; 12925ff9d6a4Sbellard /* mark the temporary as dead */ 12935ff9d6a4Sbellard dead_temps[args[0]] = 1; 12945ff9d6a4Sbellard break; 1295c896fe29Sbellard case INDEX_op_end: 1296c896fe29Sbellard break; 1297c896fe29Sbellard /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */ 1298c896fe29Sbellard default: 1299c896fe29Sbellard args -= def->nb_args; 1300c896fe29Sbellard nb_iargs = def->nb_iargs; 1301c896fe29Sbellard nb_oargs = def->nb_oargs; 1302c896fe29Sbellard 1303c896fe29Sbellard /* Test if the operation can be removed because all 13045ff9d6a4Sbellard its outputs are dead. We assume that nb_oargs == 0 13055ff9d6a4Sbellard implies side effects */ 13065ff9d6a4Sbellard if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) { 1307c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1308c896fe29Sbellard arg = args[i]; 1309c896fe29Sbellard if (!dead_temps[arg]) 1310c896fe29Sbellard goto do_not_remove; 1311c896fe29Sbellard } 1312c896fe29Sbellard tcg_set_nop(s, gen_opc_buf + op_index, args, def->nb_args); 1313c896fe29Sbellard #ifdef CONFIG_PROFILER 1314a23a9ec6Sbellard s->del_op_count++; 1315c896fe29Sbellard #endif 1316c896fe29Sbellard } else { 1317c896fe29Sbellard do_not_remove: 1318c896fe29Sbellard 1319c896fe29Sbellard /* output args are dead */ 13206b64b624SAurelien Jarno dead_args = 0; 1321c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1322c896fe29Sbellard arg = args[i]; 13236b64b624SAurelien Jarno if (dead_temps[arg]) { 13246b64b624SAurelien Jarno dead_args |= (1 << i); 13256b64b624SAurelien Jarno } 1326c896fe29Sbellard dead_temps[arg] = 1; 1327c896fe29Sbellard } 1328c896fe29Sbellard 1329c896fe29Sbellard /* if end of basic block, update */ 1330c896fe29Sbellard if (def->flags & TCG_OPF_BB_END) { 1331c896fe29Sbellard tcg_la_bb_end(s, dead_temps); 1332b03cce8eSbellard } else if (def->flags & TCG_OPF_CALL_CLOBBER) { 1333b03cce8eSbellard /* globals are live */ 1334b03cce8eSbellard memset(dead_temps, 0, s->nb_globals); 1335c896fe29Sbellard } 1336c896fe29Sbellard 1337c896fe29Sbellard /* input args are live */ 1338866cb6cbSAurelien Jarno for(i = nb_oargs; i < nb_oargs + nb_iargs; i++) { 1339866cb6cbSAurelien Jarno arg = args[i]; 1340c896fe29Sbellard if (dead_temps[arg]) { 1341866cb6cbSAurelien Jarno dead_args |= (1 << i); 1342c896fe29Sbellard } 1343c896fe29Sbellard dead_temps[arg] = 0; 1344c896fe29Sbellard } 1345866cb6cbSAurelien Jarno s->op_dead_args[op_index] = dead_args; 1346c896fe29Sbellard } 1347c896fe29Sbellard break; 1348c896fe29Sbellard } 1349c896fe29Sbellard op_index--; 1350c896fe29Sbellard } 1351c896fe29Sbellard 1352c896fe29Sbellard if (args != gen_opparam_buf) 1353c896fe29Sbellard tcg_abort(); 1354c896fe29Sbellard } 1355c896fe29Sbellard #else 1356c896fe29Sbellard /* dummy liveness analysis */ 1357655feed5Smalc static void tcg_liveness_analysis(TCGContext *s) 1358c896fe29Sbellard { 1359c896fe29Sbellard int nb_ops; 1360c896fe29Sbellard nb_ops = gen_opc_ptr - gen_opc_buf; 1361c896fe29Sbellard 1362866cb6cbSAurelien Jarno s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t)); 1363866cb6cbSAurelien Jarno memset(s->op_dead_args, 0, nb_ops * sizeof(uint16_t)); 1364c896fe29Sbellard } 1365c896fe29Sbellard #endif 1366c896fe29Sbellard 1367c896fe29Sbellard #ifndef NDEBUG 1368c896fe29Sbellard static void dump_regs(TCGContext *s) 1369c896fe29Sbellard { 1370c896fe29Sbellard TCGTemp *ts; 1371c896fe29Sbellard int i; 1372c896fe29Sbellard char buf[64]; 1373c896fe29Sbellard 1374c896fe29Sbellard for(i = 0; i < s->nb_temps; i++) { 1375c896fe29Sbellard ts = &s->temps[i]; 1376ac56dd48Spbrook printf(" %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i)); 1377c896fe29Sbellard switch(ts->val_type) { 1378c896fe29Sbellard case TEMP_VAL_REG: 1379c896fe29Sbellard printf("%s", tcg_target_reg_names[ts->reg]); 1380c896fe29Sbellard break; 1381c896fe29Sbellard case TEMP_VAL_MEM: 1382c896fe29Sbellard printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]); 1383c896fe29Sbellard break; 1384c896fe29Sbellard case TEMP_VAL_CONST: 1385c896fe29Sbellard printf("$0x%" TCG_PRIlx, ts->val); 1386c896fe29Sbellard break; 1387c896fe29Sbellard case TEMP_VAL_DEAD: 1388c896fe29Sbellard printf("D"); 1389c896fe29Sbellard break; 1390c896fe29Sbellard default: 1391c896fe29Sbellard printf("???"); 1392c896fe29Sbellard break; 1393c896fe29Sbellard } 1394c896fe29Sbellard printf("\n"); 1395c896fe29Sbellard } 1396c896fe29Sbellard 1397c896fe29Sbellard for(i = 0; i < TCG_TARGET_NB_REGS; i++) { 1398c896fe29Sbellard if (s->reg_to_temp[i] >= 0) { 1399c896fe29Sbellard printf("%s: %s\n", 1400c896fe29Sbellard tcg_target_reg_names[i], 1401ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i])); 1402c896fe29Sbellard } 1403c896fe29Sbellard } 1404c896fe29Sbellard } 1405c896fe29Sbellard 1406c896fe29Sbellard static void check_regs(TCGContext *s) 1407c896fe29Sbellard { 1408c896fe29Sbellard int reg, k; 1409c896fe29Sbellard TCGTemp *ts; 1410c896fe29Sbellard char buf[64]; 1411c896fe29Sbellard 1412c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1413c896fe29Sbellard k = s->reg_to_temp[reg]; 1414c896fe29Sbellard if (k >= 0) { 1415c896fe29Sbellard ts = &s->temps[k]; 1416c896fe29Sbellard if (ts->val_type != TEMP_VAL_REG || 1417c896fe29Sbellard ts->reg != reg) { 1418c896fe29Sbellard printf("Inconsistency for register %s:\n", 1419c896fe29Sbellard tcg_target_reg_names[reg]); 1420b03cce8eSbellard goto fail; 1421c896fe29Sbellard } 1422c896fe29Sbellard } 1423c896fe29Sbellard } 1424c896fe29Sbellard for(k = 0; k < s->nb_temps; k++) { 1425c896fe29Sbellard ts = &s->temps[k]; 1426c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG && 1427c896fe29Sbellard !ts->fixed_reg && 1428c896fe29Sbellard s->reg_to_temp[ts->reg] != k) { 1429c896fe29Sbellard printf("Inconsistency for temp %s:\n", 1430ac56dd48Spbrook tcg_get_arg_str_idx(s, buf, sizeof(buf), k)); 1431b03cce8eSbellard fail: 1432c896fe29Sbellard printf("reg state:\n"); 1433c896fe29Sbellard dump_regs(s); 1434c896fe29Sbellard tcg_abort(); 1435c896fe29Sbellard } 1436c896fe29Sbellard } 1437c896fe29Sbellard } 1438c896fe29Sbellard #endif 1439c896fe29Sbellard 1440c896fe29Sbellard static void temp_allocate_frame(TCGContext *s, int temp) 1441c896fe29Sbellard { 1442c896fe29Sbellard TCGTemp *ts; 1443c896fe29Sbellard ts = &s->temps[temp]; 1444f44c9960SBlue Swirl #ifndef __sparc_v9__ /* Sparc64 stack is accessed with offset of 2047 */ 1445b591dc59SBlue Swirl s->current_frame_offset = (s->current_frame_offset + 1446b591dc59SBlue Swirl (tcg_target_long)sizeof(tcg_target_long) - 1) & 1447b591dc59SBlue Swirl ~(sizeof(tcg_target_long) - 1); 1448f44c9960SBlue Swirl #endif 1449b591dc59SBlue Swirl if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) > 1450b591dc59SBlue Swirl s->frame_end) { 14515ff9d6a4Sbellard tcg_abort(); 1452b591dc59SBlue Swirl } 1453c896fe29Sbellard ts->mem_offset = s->current_frame_offset; 1454c896fe29Sbellard ts->mem_reg = s->frame_reg; 1455c896fe29Sbellard ts->mem_allocated = 1; 1456b591dc59SBlue Swirl s->current_frame_offset += (tcg_target_long)sizeof(tcg_target_long); 1457c896fe29Sbellard } 1458c896fe29Sbellard 1459c896fe29Sbellard /* free register 'reg' by spilling the corresponding temporary if necessary */ 1460c896fe29Sbellard static void tcg_reg_free(TCGContext *s, int reg) 1461c896fe29Sbellard { 1462c896fe29Sbellard TCGTemp *ts; 1463c896fe29Sbellard int temp; 1464c896fe29Sbellard 1465c896fe29Sbellard temp = s->reg_to_temp[reg]; 1466c896fe29Sbellard if (temp != -1) { 1467c896fe29Sbellard ts = &s->temps[temp]; 1468c896fe29Sbellard assert(ts->val_type == TEMP_VAL_REG); 1469c896fe29Sbellard if (!ts->mem_coherent) { 1470c896fe29Sbellard if (!ts->mem_allocated) 1471c896fe29Sbellard temp_allocate_frame(s, temp); 1472e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1473c896fe29Sbellard } 1474c896fe29Sbellard ts->val_type = TEMP_VAL_MEM; 1475c896fe29Sbellard s->reg_to_temp[reg] = -1; 1476c896fe29Sbellard } 1477c896fe29Sbellard } 1478c896fe29Sbellard 1479c896fe29Sbellard /* Allocate a register belonging to reg1 & ~reg2 */ 1480c896fe29Sbellard static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2) 1481c896fe29Sbellard { 1482c896fe29Sbellard int i, reg; 1483c896fe29Sbellard TCGRegSet reg_ct; 1484c896fe29Sbellard 1485c896fe29Sbellard tcg_regset_andnot(reg_ct, reg1, reg2); 1486c896fe29Sbellard 1487c896fe29Sbellard /* first try free registers */ 14880954d0d9Sblueswir1 for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { 1489c896fe29Sbellard reg = tcg_target_reg_alloc_order[i]; 1490c896fe29Sbellard if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1) 1491c896fe29Sbellard return reg; 1492c896fe29Sbellard } 1493c896fe29Sbellard 1494c896fe29Sbellard /* XXX: do better spill choice */ 14950954d0d9Sblueswir1 for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { 1496c896fe29Sbellard reg = tcg_target_reg_alloc_order[i]; 1497c896fe29Sbellard if (tcg_regset_test_reg(reg_ct, reg)) { 1498c896fe29Sbellard tcg_reg_free(s, reg); 1499c896fe29Sbellard return reg; 1500c896fe29Sbellard } 1501c896fe29Sbellard } 1502c896fe29Sbellard 1503c896fe29Sbellard tcg_abort(); 1504c896fe29Sbellard } 1505c896fe29Sbellard 1506641d5fbeSbellard /* save a temporary to memory. 'allocated_regs' is used in case a 1507e8996ee0Sbellard temporary registers needs to be allocated to store a constant. */ 1508641d5fbeSbellard static void temp_save(TCGContext *s, int temp, TCGRegSet allocated_regs) 1509c896fe29Sbellard { 1510c896fe29Sbellard TCGTemp *ts; 1511641d5fbeSbellard int reg; 1512c896fe29Sbellard 1513641d5fbeSbellard ts = &s->temps[temp]; 1514c896fe29Sbellard if (!ts->fixed_reg) { 1515e8996ee0Sbellard switch(ts->val_type) { 1516e8996ee0Sbellard case TEMP_VAL_REG: 1517c896fe29Sbellard tcg_reg_free(s, ts->reg); 1518e8996ee0Sbellard break; 1519e8996ee0Sbellard case TEMP_VAL_DEAD: 1520e5097dc8Sbellard ts->val_type = TEMP_VAL_MEM; 1521e8996ee0Sbellard break; 1522e8996ee0Sbellard case TEMP_VAL_CONST: 15236692b043SAurelien Jarno reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 15246692b043SAurelien Jarno allocated_regs); 1525d6859202SAurelien Jarno if (!ts->mem_allocated) 1526d6859202SAurelien Jarno temp_allocate_frame(s, temp); 1527e8996ee0Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1528e8996ee0Sbellard tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1529e8996ee0Sbellard ts->val_type = TEMP_VAL_MEM; 1530e8996ee0Sbellard break; 1531e8996ee0Sbellard case TEMP_VAL_MEM: 1532e8996ee0Sbellard break; 1533e8996ee0Sbellard default: 1534e8996ee0Sbellard tcg_abort(); 1535c896fe29Sbellard } 1536c896fe29Sbellard } 1537c896fe29Sbellard } 1538641d5fbeSbellard 1539641d5fbeSbellard /* save globals to their cannonical location and assume they can be 1540641d5fbeSbellard modified be the following code. 'allocated_regs' is used in case a 1541641d5fbeSbellard temporary registers needs to be allocated to store a constant. */ 1542641d5fbeSbellard static void save_globals(TCGContext *s, TCGRegSet allocated_regs) 1543641d5fbeSbellard { 1544641d5fbeSbellard int i; 1545641d5fbeSbellard 1546641d5fbeSbellard for(i = 0; i < s->nb_globals; i++) { 1547641d5fbeSbellard temp_save(s, i, allocated_regs); 1548641d5fbeSbellard } 1549e5097dc8Sbellard } 1550e5097dc8Sbellard 1551e5097dc8Sbellard /* at the end of a basic block, we assume all temporaries are dead and 1552e8996ee0Sbellard all globals are stored at their canonical location. */ 1553e8996ee0Sbellard static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs) 1554e5097dc8Sbellard { 1555e5097dc8Sbellard TCGTemp *ts; 1556e5097dc8Sbellard int i; 1557e5097dc8Sbellard 1558c896fe29Sbellard for(i = s->nb_globals; i < s->nb_temps; i++) { 1559c896fe29Sbellard ts = &s->temps[i]; 1560641d5fbeSbellard if (ts->temp_local) { 1561641d5fbeSbellard temp_save(s, i, allocated_regs); 1562641d5fbeSbellard } else { 1563c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1564c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1565c896fe29Sbellard } 1566c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1567c896fe29Sbellard } 1568641d5fbeSbellard } 1569e8996ee0Sbellard 1570e8996ee0Sbellard save_globals(s, allocated_regs); 1571c896fe29Sbellard } 1572c896fe29Sbellard 1573866cb6cbSAurelien Jarno #define IS_DEAD_ARG(n) ((dead_args >> (n)) & 1) 1574c896fe29Sbellard 1575e8996ee0Sbellard static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args) 1576e8996ee0Sbellard { 1577e8996ee0Sbellard TCGTemp *ots; 1578e8996ee0Sbellard tcg_target_ulong val; 1579e8996ee0Sbellard 1580e8996ee0Sbellard ots = &s->temps[args[0]]; 1581e8996ee0Sbellard val = args[1]; 1582e8996ee0Sbellard 1583e8996ee0Sbellard if (ots->fixed_reg) { 1584e8996ee0Sbellard /* for fixed registers, we do not do any constant 1585e8996ee0Sbellard propagation */ 1586e8996ee0Sbellard tcg_out_movi(s, ots->type, ots->reg, val); 1587e8996ee0Sbellard } else { 15881235fc06Sths /* The movi is not explicitly generated here */ 1589e8996ee0Sbellard if (ots->val_type == TEMP_VAL_REG) 1590e8996ee0Sbellard s->reg_to_temp[ots->reg] = -1; 1591e8996ee0Sbellard ots->val_type = TEMP_VAL_CONST; 1592e8996ee0Sbellard ots->val = val; 1593e8996ee0Sbellard } 1594e8996ee0Sbellard } 1595e8996ee0Sbellard 1596c896fe29Sbellard static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def, 1597c896fe29Sbellard const TCGArg *args, 1598866cb6cbSAurelien Jarno unsigned int dead_args) 1599c896fe29Sbellard { 1600c896fe29Sbellard TCGTemp *ts, *ots; 1601c896fe29Sbellard int reg; 1602c896fe29Sbellard const TCGArgConstraint *arg_ct; 1603c896fe29Sbellard 1604c896fe29Sbellard ots = &s->temps[args[0]]; 1605c896fe29Sbellard ts = &s->temps[args[1]]; 1606c896fe29Sbellard arg_ct = &def->args_ct[0]; 1607c896fe29Sbellard 1608866cb6cbSAurelien Jarno /* XXX: always mark arg dead if IS_DEAD_ARG(1) */ 1609c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1610866cb6cbSAurelien Jarno if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) { 1611c896fe29Sbellard /* the mov can be suppressed */ 1612c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) 1613c896fe29Sbellard s->reg_to_temp[ots->reg] = -1; 1614c896fe29Sbellard reg = ts->reg; 1615c896fe29Sbellard s->reg_to_temp[reg] = -1; 1616c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1617c896fe29Sbellard } else { 1618c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) { 1619c896fe29Sbellard reg = ots->reg; 1620c896fe29Sbellard } else { 1621c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); 1622c896fe29Sbellard } 1623c896fe29Sbellard if (ts->reg != reg) { 16243b6dac34SRichard Henderson tcg_out_mov(s, ots->type, reg, ts->reg); 1625c896fe29Sbellard } 1626c896fe29Sbellard } 1627c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1628c896fe29Sbellard if (ots->val_type == TEMP_VAL_REG) { 1629c896fe29Sbellard reg = ots->reg; 1630c896fe29Sbellard } else { 1631c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs); 1632c896fe29Sbellard } 1633e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1634c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1635e8996ee0Sbellard if (ots->fixed_reg) { 1636c896fe29Sbellard reg = ots->reg; 1637c896fe29Sbellard tcg_out_movi(s, ots->type, reg, ts->val); 1638c896fe29Sbellard } else { 1639e8996ee0Sbellard /* propagate constant */ 1640e8996ee0Sbellard if (ots->val_type == TEMP_VAL_REG) 1641e8996ee0Sbellard s->reg_to_temp[ots->reg] = -1; 1642e8996ee0Sbellard ots->val_type = TEMP_VAL_CONST; 1643e8996ee0Sbellard ots->val = ts->val; 1644e8996ee0Sbellard return; 1645e8996ee0Sbellard } 1646e8996ee0Sbellard } else { 1647c896fe29Sbellard tcg_abort(); 1648c896fe29Sbellard } 1649c896fe29Sbellard s->reg_to_temp[reg] = args[0]; 1650c896fe29Sbellard ots->reg = reg; 1651c896fe29Sbellard ots->val_type = TEMP_VAL_REG; 1652c896fe29Sbellard ots->mem_coherent = 0; 1653c896fe29Sbellard } 1654c896fe29Sbellard 1655c896fe29Sbellard static void tcg_reg_alloc_op(TCGContext *s, 1656a9751609SRichard Henderson const TCGOpDef *def, TCGOpcode opc, 1657c896fe29Sbellard const TCGArg *args, 1658866cb6cbSAurelien Jarno unsigned int dead_args) 1659c896fe29Sbellard { 1660c896fe29Sbellard TCGRegSet allocated_regs; 1661c896fe29Sbellard int i, k, nb_iargs, nb_oargs, reg; 1662c896fe29Sbellard TCGArg arg; 1663c896fe29Sbellard const TCGArgConstraint *arg_ct; 1664c896fe29Sbellard TCGTemp *ts; 1665c896fe29Sbellard TCGArg new_args[TCG_MAX_OP_ARGS]; 1666c896fe29Sbellard int const_args[TCG_MAX_OP_ARGS]; 1667c896fe29Sbellard 1668c896fe29Sbellard nb_oargs = def->nb_oargs; 1669c896fe29Sbellard nb_iargs = def->nb_iargs; 1670c896fe29Sbellard 1671c896fe29Sbellard /* copy constants */ 1672c896fe29Sbellard memcpy(new_args + nb_oargs + nb_iargs, 1673c896fe29Sbellard args + nb_oargs + nb_iargs, 1674c896fe29Sbellard sizeof(TCGArg) * def->nb_cargs); 1675c896fe29Sbellard 1676c896fe29Sbellard /* satisfy input constraints */ 1677c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1678c896fe29Sbellard for(k = 0; k < nb_iargs; k++) { 1679c896fe29Sbellard i = def->sorted_args[nb_oargs + k]; 1680c896fe29Sbellard arg = args[i]; 1681c896fe29Sbellard arg_ct = &def->args_ct[i]; 1682c896fe29Sbellard ts = &s->temps[arg]; 1683c896fe29Sbellard if (ts->val_type == TEMP_VAL_MEM) { 1684c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1685e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1686c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1687c896fe29Sbellard ts->reg = reg; 1688c896fe29Sbellard ts->mem_coherent = 1; 1689c896fe29Sbellard s->reg_to_temp[reg] = arg; 1690c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1691c896fe29Sbellard if (tcg_target_const_match(ts->val, arg_ct)) { 1692c896fe29Sbellard /* constant is OK for instruction */ 1693c896fe29Sbellard const_args[i] = 1; 1694c896fe29Sbellard new_args[i] = ts->val; 1695c896fe29Sbellard goto iarg_end; 1696c896fe29Sbellard } else { 1697c896fe29Sbellard /* need to move to a register */ 1698c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1699c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1700e8996ee0Sbellard ts->val_type = TEMP_VAL_REG; 1701e8996ee0Sbellard ts->reg = reg; 1702e8996ee0Sbellard ts->mem_coherent = 0; 1703e8996ee0Sbellard s->reg_to_temp[reg] = arg; 1704c896fe29Sbellard } 1705c896fe29Sbellard } 1706c896fe29Sbellard assert(ts->val_type == TEMP_VAL_REG); 17075ff9d6a4Sbellard if (arg_ct->ct & TCG_CT_IALIAS) { 17085ff9d6a4Sbellard if (ts->fixed_reg) { 17095ff9d6a4Sbellard /* if fixed register, we must allocate a new register 17105ff9d6a4Sbellard if the alias is not the same register */ 17115ff9d6a4Sbellard if (arg != args[arg_ct->alias_index]) 17125ff9d6a4Sbellard goto allocate_in_reg; 17135ff9d6a4Sbellard } else { 1714c896fe29Sbellard /* if the input is aliased to an output and if it is 1715c896fe29Sbellard not dead after the instruction, we must allocate 1716c896fe29Sbellard a new register and move it */ 1717866cb6cbSAurelien Jarno if (!IS_DEAD_ARG(i)) { 1718c896fe29Sbellard goto allocate_in_reg; 1719c896fe29Sbellard } 17205ff9d6a4Sbellard } 1721866cb6cbSAurelien Jarno } 1722c896fe29Sbellard reg = ts->reg; 1723c896fe29Sbellard if (tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1724c896fe29Sbellard /* nothing to do : the constraint is satisfied */ 1725c896fe29Sbellard } else { 1726c896fe29Sbellard allocate_in_reg: 1727c896fe29Sbellard /* allocate a new register matching the constraint 1728c896fe29Sbellard and move the temporary register into it */ 1729c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 17303b6dac34SRichard Henderson tcg_out_mov(s, ts->type, reg, ts->reg); 1731c896fe29Sbellard } 1732c896fe29Sbellard new_args[i] = reg; 1733c896fe29Sbellard const_args[i] = 0; 1734c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1735c896fe29Sbellard iarg_end: ; 1736c896fe29Sbellard } 1737c896fe29Sbellard 1738e8996ee0Sbellard if (def->flags & TCG_OPF_BB_END) { 1739e8996ee0Sbellard tcg_reg_alloc_bb_end(s, allocated_regs); 1740e8996ee0Sbellard } else { 1741c896fe29Sbellard /* mark dead temporaries and free the associated registers */ 1742866cb6cbSAurelien Jarno for(i = nb_oargs; i < nb_oargs + nb_iargs; i++) { 1743866cb6cbSAurelien Jarno arg = args[i]; 1744866cb6cbSAurelien Jarno if (IS_DEAD_ARG(i)) { 1745c896fe29Sbellard ts = &s->temps[arg]; 1746e8996ee0Sbellard if (!ts->fixed_reg) { 1747c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1748c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1749c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1750c896fe29Sbellard } 1751c896fe29Sbellard } 1752c896fe29Sbellard } 1753c896fe29Sbellard 1754c896fe29Sbellard if (def->flags & TCG_OPF_CALL_CLOBBER) { 1755b03cce8eSbellard /* XXX: permit generic clobber register list ? */ 1756c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1757c896fe29Sbellard if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) { 1758c896fe29Sbellard tcg_reg_free(s, reg); 1759c896fe29Sbellard } 1760c896fe29Sbellard } 1761b03cce8eSbellard /* XXX: for load/store we could do that only for the slow path 1762b03cce8eSbellard (i.e. when a memory callback is called) */ 1763b03cce8eSbellard 1764b03cce8eSbellard /* store globals and free associated registers (we assume the insn 1765b03cce8eSbellard can modify any global. */ 1766e8996ee0Sbellard save_globals(s, allocated_regs); 1767c896fe29Sbellard } 1768c896fe29Sbellard 1769c896fe29Sbellard /* satisfy the output constraints */ 1770c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1771c896fe29Sbellard for(k = 0; k < nb_oargs; k++) { 1772c896fe29Sbellard i = def->sorted_args[k]; 1773c896fe29Sbellard arg = args[i]; 1774c896fe29Sbellard arg_ct = &def->args_ct[i]; 1775c896fe29Sbellard ts = &s->temps[arg]; 1776c896fe29Sbellard if (arg_ct->ct & TCG_CT_ALIAS) { 17775ff9d6a4Sbellard reg = new_args[arg_ct->alias_index]; 1778c896fe29Sbellard } else { 1779c896fe29Sbellard /* if fixed register, we try to use it */ 1780c896fe29Sbellard reg = ts->reg; 1781c896fe29Sbellard if (ts->fixed_reg && 1782c896fe29Sbellard tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1783c896fe29Sbellard goto oarg_end; 1784c896fe29Sbellard } 1785c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1786c896fe29Sbellard } 1787c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1788c896fe29Sbellard /* if a fixed register is used, then a move will be done afterwards */ 1789c896fe29Sbellard if (!ts->fixed_reg) { 1790c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1791c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 17928c11ad25SAurelien Jarno if (IS_DEAD_ARG(i)) { 17938c11ad25SAurelien Jarno ts->val_type = TEMP_VAL_DEAD; 17948c11ad25SAurelien Jarno } else { 1795c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1796c896fe29Sbellard ts->reg = reg; 1797c896fe29Sbellard /* temp value is modified, so the value kept in memory is 1798c896fe29Sbellard potentially not the same */ 1799c896fe29Sbellard ts->mem_coherent = 0; 1800c896fe29Sbellard s->reg_to_temp[reg] = arg; 1801c896fe29Sbellard } 18028c11ad25SAurelien Jarno } 1803c896fe29Sbellard oarg_end: 1804c896fe29Sbellard new_args[i] = reg; 1805c896fe29Sbellard } 1806e8996ee0Sbellard } 1807c896fe29Sbellard 1808c896fe29Sbellard /* emit instruction */ 1809c896fe29Sbellard tcg_out_op(s, opc, new_args, const_args); 1810c896fe29Sbellard 1811c896fe29Sbellard /* move the outputs in the correct register if needed */ 1812c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1813c896fe29Sbellard ts = &s->temps[args[i]]; 1814c896fe29Sbellard reg = new_args[i]; 1815c896fe29Sbellard if (ts->fixed_reg && ts->reg != reg) { 18163b6dac34SRichard Henderson tcg_out_mov(s, ts->type, ts->reg, reg); 1817c896fe29Sbellard } 1818c896fe29Sbellard } 1819c896fe29Sbellard } 1820c896fe29Sbellard 1821b03cce8eSbellard #ifdef TCG_TARGET_STACK_GROWSUP 1822b03cce8eSbellard #define STACK_DIR(x) (-(x)) 1823b03cce8eSbellard #else 1824b03cce8eSbellard #define STACK_DIR(x) (x) 1825b03cce8eSbellard #endif 1826b03cce8eSbellard 1827c896fe29Sbellard static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def, 1828a9751609SRichard Henderson TCGOpcode opc, const TCGArg *args, 1829866cb6cbSAurelien Jarno unsigned int dead_args) 1830c896fe29Sbellard { 1831c896fe29Sbellard int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params; 1832c896fe29Sbellard TCGArg arg, func_arg; 1833c896fe29Sbellard TCGTemp *ts; 1834f54b3f92Saurel32 tcg_target_long stack_offset, call_stack_size, func_addr; 1835b03cce8eSbellard int const_func_arg, allocate_args; 1836c896fe29Sbellard TCGRegSet allocated_regs; 1837c896fe29Sbellard const TCGArgConstraint *arg_ct; 1838c896fe29Sbellard 1839c896fe29Sbellard arg = *args++; 1840c896fe29Sbellard 1841c896fe29Sbellard nb_oargs = arg >> 16; 1842c896fe29Sbellard nb_iargs = arg & 0xffff; 1843c896fe29Sbellard nb_params = nb_iargs - 1; 1844c896fe29Sbellard 1845c896fe29Sbellard flags = args[nb_oargs + nb_iargs]; 1846c896fe29Sbellard 1847c896fe29Sbellard nb_regs = tcg_target_get_call_iarg_regs_count(flags); 1848c896fe29Sbellard if (nb_regs > nb_params) 1849c896fe29Sbellard nb_regs = nb_params; 1850c896fe29Sbellard 1851c896fe29Sbellard /* assign stack slots first */ 1852c896fe29Sbellard call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long); 1853c896fe29Sbellard call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) & 1854c896fe29Sbellard ~(TCG_TARGET_STACK_ALIGN - 1); 1855b03cce8eSbellard allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE); 1856b03cce8eSbellard if (allocate_args) { 1857345649c0SBlue Swirl /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed, 1858345649c0SBlue Swirl preallocate call stack */ 1859345649c0SBlue Swirl tcg_abort(); 1860b03cce8eSbellard } 186139cf05d3Sbellard 186239cf05d3Sbellard stack_offset = TCG_TARGET_CALL_STACK_OFFSET; 1863c896fe29Sbellard for(i = nb_regs; i < nb_params; i++) { 1864c896fe29Sbellard arg = args[nb_oargs + i]; 186539cf05d3Sbellard #ifdef TCG_TARGET_STACK_GROWSUP 186639cf05d3Sbellard stack_offset -= sizeof(tcg_target_long); 186739cf05d3Sbellard #endif 186839cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1869c896fe29Sbellard ts = &s->temps[arg]; 1870c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1871e4d5434cSblueswir1 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset); 1872c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1873c896fe29Sbellard reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 1874c896fe29Sbellard s->reserved_regs); 1875c896fe29Sbellard /* XXX: not correct if reading values from the stack */ 1876e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1877e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset); 1878c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1879c896fe29Sbellard reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 1880c896fe29Sbellard s->reserved_regs); 1881c896fe29Sbellard /* XXX: sign extend may be needed on some targets */ 1882c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1883e4d5434cSblueswir1 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset); 1884c896fe29Sbellard } else { 1885c896fe29Sbellard tcg_abort(); 1886c896fe29Sbellard } 188739cf05d3Sbellard } 188839cf05d3Sbellard #ifndef TCG_TARGET_STACK_GROWSUP 188939cf05d3Sbellard stack_offset += sizeof(tcg_target_long); 189039cf05d3Sbellard #endif 1891c896fe29Sbellard } 1892c896fe29Sbellard 1893c896fe29Sbellard /* assign input registers */ 1894c896fe29Sbellard tcg_regset_set(allocated_regs, s->reserved_regs); 1895c896fe29Sbellard for(i = 0; i < nb_regs; i++) { 1896c896fe29Sbellard arg = args[nb_oargs + i]; 189739cf05d3Sbellard if (arg != TCG_CALL_DUMMY_ARG) { 1898c896fe29Sbellard ts = &s->temps[arg]; 1899c896fe29Sbellard reg = tcg_target_call_iarg_regs[i]; 1900c896fe29Sbellard tcg_reg_free(s, reg); 1901c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) { 1902c896fe29Sbellard if (ts->reg != reg) { 19033b6dac34SRichard Henderson tcg_out_mov(s, ts->type, reg, ts->reg); 1904c896fe29Sbellard } 1905c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_MEM) { 1906e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1907c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1908c896fe29Sbellard /* XXX: sign extend ? */ 1909c896fe29Sbellard tcg_out_movi(s, ts->type, reg, ts->val); 1910c896fe29Sbellard } else { 1911c896fe29Sbellard tcg_abort(); 1912c896fe29Sbellard } 1913c896fe29Sbellard tcg_regset_set_reg(allocated_regs, reg); 1914c896fe29Sbellard } 191539cf05d3Sbellard } 1916c896fe29Sbellard 1917c896fe29Sbellard /* assign function address */ 1918c896fe29Sbellard func_arg = args[nb_oargs + nb_iargs - 1]; 1919c896fe29Sbellard arg_ct = &def->args_ct[0]; 1920c896fe29Sbellard ts = &s->temps[func_arg]; 1921f54b3f92Saurel32 func_addr = ts->val; 1922c896fe29Sbellard const_func_arg = 0; 1923c896fe29Sbellard if (ts->val_type == TEMP_VAL_MEM) { 1924c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1925e4d5434cSblueswir1 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset); 1926c896fe29Sbellard func_arg = reg; 1927e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1928c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_REG) { 1929c896fe29Sbellard reg = ts->reg; 1930c896fe29Sbellard if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) { 1931c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 19323b6dac34SRichard Henderson tcg_out_mov(s, ts->type, reg, ts->reg); 1933c896fe29Sbellard } 1934c896fe29Sbellard func_arg = reg; 1935e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1936c896fe29Sbellard } else if (ts->val_type == TEMP_VAL_CONST) { 1937f54b3f92Saurel32 if (tcg_target_const_match(func_addr, arg_ct)) { 1938c896fe29Sbellard const_func_arg = 1; 1939f54b3f92Saurel32 func_arg = func_addr; 1940c896fe29Sbellard } else { 1941c896fe29Sbellard reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs); 1942f54b3f92Saurel32 tcg_out_movi(s, ts->type, reg, func_addr); 1943c896fe29Sbellard func_arg = reg; 1944e8996ee0Sbellard tcg_regset_set_reg(allocated_regs, reg); 1945c896fe29Sbellard } 1946c896fe29Sbellard } else { 1947c896fe29Sbellard tcg_abort(); 1948c896fe29Sbellard } 1949c896fe29Sbellard 1950e8996ee0Sbellard 1951c896fe29Sbellard /* mark dead temporaries and free the associated registers */ 1952866cb6cbSAurelien Jarno for(i = nb_oargs; i < nb_iargs + nb_oargs; i++) { 1953866cb6cbSAurelien Jarno arg = args[i]; 1954866cb6cbSAurelien Jarno if (IS_DEAD_ARG(i)) { 1955c896fe29Sbellard ts = &s->temps[arg]; 1956e8996ee0Sbellard if (!ts->fixed_reg) { 1957c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1958c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 1959c896fe29Sbellard ts->val_type = TEMP_VAL_DEAD; 1960c896fe29Sbellard } 1961c896fe29Sbellard } 1962c896fe29Sbellard } 1963c896fe29Sbellard 1964c896fe29Sbellard /* clobber call registers */ 1965c896fe29Sbellard for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { 1966c896fe29Sbellard if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) { 1967c896fe29Sbellard tcg_reg_free(s, reg); 1968c896fe29Sbellard } 1969c896fe29Sbellard } 1970c896fe29Sbellard 1971c896fe29Sbellard /* store globals and free associated registers (we assume the call 1972c896fe29Sbellard can modify any global. */ 1973b9c18f56Saurel32 if (!(flags & TCG_CALL_CONST)) { 1974e8996ee0Sbellard save_globals(s, allocated_regs); 1975b9c18f56Saurel32 } 1976c896fe29Sbellard 1977c896fe29Sbellard tcg_out_op(s, opc, &func_arg, &const_func_arg); 1978c896fe29Sbellard 1979c896fe29Sbellard /* assign output registers and emit moves if needed */ 1980c896fe29Sbellard for(i = 0; i < nb_oargs; i++) { 1981c896fe29Sbellard arg = args[i]; 1982c896fe29Sbellard ts = &s->temps[arg]; 1983c896fe29Sbellard reg = tcg_target_call_oarg_regs[i]; 1984e8996ee0Sbellard assert(s->reg_to_temp[reg] == -1); 1985c896fe29Sbellard if (ts->fixed_reg) { 1986c896fe29Sbellard if (ts->reg != reg) { 19873b6dac34SRichard Henderson tcg_out_mov(s, ts->type, ts->reg, reg); 1988c896fe29Sbellard } 1989c896fe29Sbellard } else { 1990c896fe29Sbellard if (ts->val_type == TEMP_VAL_REG) 1991c896fe29Sbellard s->reg_to_temp[ts->reg] = -1; 19928c11ad25SAurelien Jarno if (IS_DEAD_ARG(i)) { 19938c11ad25SAurelien Jarno ts->val_type = TEMP_VAL_DEAD; 19948c11ad25SAurelien Jarno } else { 1995c896fe29Sbellard ts->val_type = TEMP_VAL_REG; 1996c896fe29Sbellard ts->reg = reg; 1997c896fe29Sbellard ts->mem_coherent = 0; 1998c896fe29Sbellard s->reg_to_temp[reg] = arg; 1999c896fe29Sbellard } 2000c896fe29Sbellard } 20018c11ad25SAurelien Jarno } 2002c896fe29Sbellard 2003c896fe29Sbellard return nb_iargs + nb_oargs + def->nb_cargs + 1; 2004c896fe29Sbellard } 2005c896fe29Sbellard 2006c896fe29Sbellard #ifdef CONFIG_PROFILER 2007c896fe29Sbellard 200854604f74Saurel32 static int64_t tcg_table_op_count[NB_OPS]; 2009c896fe29Sbellard 2010871e6c35SBlue Swirl static void dump_op_count(void) 2011c896fe29Sbellard { 2012c896fe29Sbellard int i; 2013c896fe29Sbellard FILE *f; 201454604f74Saurel32 f = fopen("/tmp/op.log", "w"); 2015c896fe29Sbellard for(i = INDEX_op_end; i < NB_OPS; i++) { 201654604f74Saurel32 fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, tcg_table_op_count[i]); 2017c896fe29Sbellard } 2018c896fe29Sbellard fclose(f); 2019c896fe29Sbellard } 2020c896fe29Sbellard #endif 2021c896fe29Sbellard 2022c896fe29Sbellard 2023c896fe29Sbellard static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf, 20242ba1eeb6Spbrook long search_pc) 2025c896fe29Sbellard { 2026a9751609SRichard Henderson TCGOpcode opc; 2027a9751609SRichard Henderson int op_index; 2028c896fe29Sbellard const TCGOpDef *def; 2029866cb6cbSAurelien Jarno unsigned int dead_args; 2030c896fe29Sbellard const TCGArg *args; 2031c896fe29Sbellard 2032c896fe29Sbellard #ifdef DEBUG_DISAS 20338fec2b8cSaliguori if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) { 203493fcfe39Saliguori qemu_log("OP:\n"); 2035c896fe29Sbellard tcg_dump_ops(s, logfile); 203693fcfe39Saliguori qemu_log("\n"); 2037c896fe29Sbellard } 2038c896fe29Sbellard #endif 2039c896fe29Sbellard 20408f2e8c07SKirill Batuzov #ifdef USE_TCG_OPTIMIZATIONS 20418f2e8c07SKirill Batuzov gen_opparam_ptr = 20428f2e8c07SKirill Batuzov tcg_optimize(s, gen_opc_ptr, gen_opparam_buf, tcg_op_defs); 20438f2e8c07SKirill Batuzov #endif 20448f2e8c07SKirill Batuzov 2045a23a9ec6Sbellard #ifdef CONFIG_PROFILER 2046a23a9ec6Sbellard s->la_time -= profile_getclock(); 2047a23a9ec6Sbellard #endif 2048c896fe29Sbellard tcg_liveness_analysis(s); 2049a23a9ec6Sbellard #ifdef CONFIG_PROFILER 2050a23a9ec6Sbellard s->la_time += profile_getclock(); 2051a23a9ec6Sbellard #endif 2052c896fe29Sbellard 2053c896fe29Sbellard #ifdef DEBUG_DISAS 20548fec2b8cSaliguori if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT))) { 20556a957025SAurelien Jarno qemu_log("OP after liveness analysis:\n"); 2056c896fe29Sbellard tcg_dump_ops(s, logfile); 205793fcfe39Saliguori qemu_log("\n"); 2058c896fe29Sbellard } 2059c896fe29Sbellard #endif 2060c896fe29Sbellard 2061c896fe29Sbellard tcg_reg_alloc_start(s); 2062c896fe29Sbellard 2063c896fe29Sbellard s->code_buf = gen_code_buf; 2064c896fe29Sbellard s->code_ptr = gen_code_buf; 2065c896fe29Sbellard 2066c896fe29Sbellard args = gen_opparam_buf; 2067c896fe29Sbellard op_index = 0; 2068b3db8758Sblueswir1 2069c896fe29Sbellard for(;;) { 2070c896fe29Sbellard opc = gen_opc_buf[op_index]; 2071c896fe29Sbellard #ifdef CONFIG_PROFILER 207254604f74Saurel32 tcg_table_op_count[opc]++; 2073c896fe29Sbellard #endif 2074c896fe29Sbellard def = &tcg_op_defs[opc]; 2075c896fe29Sbellard #if 0 2076c896fe29Sbellard printf("%s: %d %d %d\n", def->name, 2077c896fe29Sbellard def->nb_oargs, def->nb_iargs, def->nb_cargs); 2078c896fe29Sbellard // dump_regs(s); 2079c896fe29Sbellard #endif 2080c896fe29Sbellard switch(opc) { 2081c896fe29Sbellard case INDEX_op_mov_i32: 2082c896fe29Sbellard #if TCG_TARGET_REG_BITS == 64 2083c896fe29Sbellard case INDEX_op_mov_i64: 2084c896fe29Sbellard #endif 2085866cb6cbSAurelien Jarno dead_args = s->op_dead_args[op_index]; 2086866cb6cbSAurelien Jarno tcg_reg_alloc_mov(s, def, args, dead_args); 2087c896fe29Sbellard break; 2088e8996ee0Sbellard case INDEX_op_movi_i32: 2089e8996ee0Sbellard #if TCG_TARGET_REG_BITS == 64 2090e8996ee0Sbellard case INDEX_op_movi_i64: 2091e8996ee0Sbellard #endif 2092e8996ee0Sbellard tcg_reg_alloc_movi(s, args); 2093e8996ee0Sbellard break; 20947e4597d7Sbellard case INDEX_op_debug_insn_start: 20957e4597d7Sbellard /* debug instruction */ 20967e4597d7Sbellard break; 2097c896fe29Sbellard case INDEX_op_nop: 2098c896fe29Sbellard case INDEX_op_nop1: 2099c896fe29Sbellard case INDEX_op_nop2: 2100c896fe29Sbellard case INDEX_op_nop3: 2101c896fe29Sbellard break; 2102c896fe29Sbellard case INDEX_op_nopn: 2103c896fe29Sbellard args += args[0]; 2104c896fe29Sbellard goto next; 21055ff9d6a4Sbellard case INDEX_op_discard: 21065ff9d6a4Sbellard { 21075ff9d6a4Sbellard TCGTemp *ts; 21085ff9d6a4Sbellard ts = &s->temps[args[0]]; 21095ff9d6a4Sbellard /* mark the temporary as dead */ 2110e8996ee0Sbellard if (!ts->fixed_reg) { 21115ff9d6a4Sbellard if (ts->val_type == TEMP_VAL_REG) 21125ff9d6a4Sbellard s->reg_to_temp[ts->reg] = -1; 21135ff9d6a4Sbellard ts->val_type = TEMP_VAL_DEAD; 21145ff9d6a4Sbellard } 21155ff9d6a4Sbellard } 21165ff9d6a4Sbellard break; 2117c896fe29Sbellard case INDEX_op_set_label: 2118e8996ee0Sbellard tcg_reg_alloc_bb_end(s, s->reserved_regs); 2119c896fe29Sbellard tcg_out_label(s, args[0], (long)s->code_ptr); 2120c896fe29Sbellard break; 2121c896fe29Sbellard case INDEX_op_call: 2122866cb6cbSAurelien Jarno dead_args = s->op_dead_args[op_index]; 2123866cb6cbSAurelien Jarno args += tcg_reg_alloc_call(s, def, opc, args, dead_args); 2124c896fe29Sbellard goto next; 2125c896fe29Sbellard case INDEX_op_end: 2126c896fe29Sbellard goto the_end; 2127c896fe29Sbellard default: 212825c4d9ccSRichard Henderson /* Sanity check that we've not introduced any unhandled opcodes. */ 212925c4d9ccSRichard Henderson if (def->flags & TCG_OPF_NOT_PRESENT) { 213025c4d9ccSRichard Henderson tcg_abort(); 213125c4d9ccSRichard Henderson } 2132c896fe29Sbellard /* Note: in order to speed up the code, it would be much 2133c896fe29Sbellard faster to have specialized register allocator functions for 2134c896fe29Sbellard some common argument patterns */ 2135866cb6cbSAurelien Jarno dead_args = s->op_dead_args[op_index]; 2136866cb6cbSAurelien Jarno tcg_reg_alloc_op(s, def, opc, args, dead_args); 2137c896fe29Sbellard break; 2138c896fe29Sbellard } 2139c896fe29Sbellard args += def->nb_args; 21408df1ca4bSths next: 21412ba1eeb6Spbrook if (search_pc >= 0 && search_pc < s->code_ptr - gen_code_buf) { 2142c896fe29Sbellard return op_index; 2143c896fe29Sbellard } 2144c896fe29Sbellard op_index++; 2145c896fe29Sbellard #ifndef NDEBUG 2146c896fe29Sbellard check_regs(s); 2147c896fe29Sbellard #endif 2148c896fe29Sbellard } 2149c896fe29Sbellard the_end: 2150c896fe29Sbellard return -1; 2151c896fe29Sbellard } 2152c896fe29Sbellard 215354604f74Saurel32 int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf) 2154c896fe29Sbellard { 2155c896fe29Sbellard #ifdef CONFIG_PROFILER 2156c896fe29Sbellard { 2157c896fe29Sbellard int n; 2158c896fe29Sbellard n = (gen_opc_ptr - gen_opc_buf); 2159a23a9ec6Sbellard s->op_count += n; 2160a23a9ec6Sbellard if (n > s->op_count_max) 2161a23a9ec6Sbellard s->op_count_max = n; 2162a23a9ec6Sbellard 2163a23a9ec6Sbellard s->temp_count += s->nb_temps; 2164a23a9ec6Sbellard if (s->nb_temps > s->temp_count_max) 2165a23a9ec6Sbellard s->temp_count_max = s->nb_temps; 2166c896fe29Sbellard } 2167c896fe29Sbellard #endif 2168c896fe29Sbellard 21692ba1eeb6Spbrook tcg_gen_code_common(s, gen_code_buf, -1); 2170c896fe29Sbellard 2171c896fe29Sbellard /* flush instruction cache */ 2172c896fe29Sbellard flush_icache_range((unsigned long)gen_code_buf, 2173c896fe29Sbellard (unsigned long)s->code_ptr); 2174c896fe29Sbellard return s->code_ptr - gen_code_buf; 2175c896fe29Sbellard } 2176c896fe29Sbellard 21772ba1eeb6Spbrook /* Return the index of the micro operation such as the pc after is < 2178623e265cSpbrook offset bytes from the start of the TB. The contents of gen_code_buf must 2179623e265cSpbrook not be changed, though writing the same values is ok. 2180623e265cSpbrook Return -1 if not found. */ 218154604f74Saurel32 int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset) 2182c896fe29Sbellard { 2183623e265cSpbrook return tcg_gen_code_common(s, gen_code_buf, offset); 2184c896fe29Sbellard } 2185a23a9ec6Sbellard 2186a23a9ec6Sbellard #ifdef CONFIG_PROFILER 2187405cf9ffSStefan Weil void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf) 2188a23a9ec6Sbellard { 2189a23a9ec6Sbellard TCGContext *s = &tcg_ctx; 2190a23a9ec6Sbellard int64_t tot; 2191a23a9ec6Sbellard 2192a23a9ec6Sbellard tot = s->interm_time + s->code_time; 2193a23a9ec6Sbellard cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n", 2194a23a9ec6Sbellard tot, tot / 2.4e9); 2195a23a9ec6Sbellard cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n", 2196a23a9ec6Sbellard s->tb_count, 2197a23a9ec6Sbellard s->tb_count1 - s->tb_count, 2198a23a9ec6Sbellard s->tb_count1 ? (double)(s->tb_count1 - s->tb_count) / s->tb_count1 * 100.0 : 0); 2199a23a9ec6Sbellard cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n", 2200a23a9ec6Sbellard s->tb_count ? (double)s->op_count / s->tb_count : 0, s->op_count_max); 2201a23a9ec6Sbellard cpu_fprintf(f, "deleted ops/TB %0.2f\n", 2202a23a9ec6Sbellard s->tb_count ? 2203a23a9ec6Sbellard (double)s->del_op_count / s->tb_count : 0); 2204a23a9ec6Sbellard cpu_fprintf(f, "avg temps/TB %0.2f max=%d\n", 2205a23a9ec6Sbellard s->tb_count ? 2206a23a9ec6Sbellard (double)s->temp_count / s->tb_count : 0, 2207a23a9ec6Sbellard s->temp_count_max); 2208a23a9ec6Sbellard 2209a23a9ec6Sbellard cpu_fprintf(f, "cycles/op %0.1f\n", 2210a23a9ec6Sbellard s->op_count ? (double)tot / s->op_count : 0); 2211a23a9ec6Sbellard cpu_fprintf(f, "cycles/in byte %0.1f\n", 2212a23a9ec6Sbellard s->code_in_len ? (double)tot / s->code_in_len : 0); 2213a23a9ec6Sbellard cpu_fprintf(f, "cycles/out byte %0.1f\n", 2214a23a9ec6Sbellard s->code_out_len ? (double)tot / s->code_out_len : 0); 2215a23a9ec6Sbellard if (tot == 0) 2216a23a9ec6Sbellard tot = 1; 2217a23a9ec6Sbellard cpu_fprintf(f, " gen_interm time %0.1f%%\n", 2218a23a9ec6Sbellard (double)s->interm_time / tot * 100.0); 2219a23a9ec6Sbellard cpu_fprintf(f, " gen_code time %0.1f%%\n", 2220a23a9ec6Sbellard (double)s->code_time / tot * 100.0); 2221a23a9ec6Sbellard cpu_fprintf(f, "liveness/code time %0.1f%%\n", 2222a23a9ec6Sbellard (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0); 2223a23a9ec6Sbellard cpu_fprintf(f, "cpu_restore count %" PRId64 "\n", 2224a23a9ec6Sbellard s->restore_count); 2225a23a9ec6Sbellard cpu_fprintf(f, " avg cycles %0.1f\n", 2226a23a9ec6Sbellard s->restore_count ? (double)s->restore_time / s->restore_count : 0); 2227871e6c35SBlue Swirl 2228a23a9ec6Sbellard dump_op_count(); 2229a23a9ec6Sbellard } 2230a23a9ec6Sbellard #else 2231405cf9ffSStefan Weil void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf) 2232a23a9ec6Sbellard { 223324bf7b3aSbellard cpu_fprintf(f, "[TCG profiler not compiled]\n"); 2234a23a9ec6Sbellard } 2235a23a9ec6Sbellard #endif 2236