bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Host code generation |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
aurel32 | fad6cb1 | 2009-01-04 22:05:52 +0000 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include <stdarg.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | #include "config.h" |
bellard | 2054396 | 2003-06-15 23:28:43 +0000 | [diff] [blame] | 27 | |
bellard | af5ad10 | 2004-01-04 23:28:12 +0000 | [diff] [blame] | 28 | #define NO_CPU_IO_DEFS |
bellard | d3eead2 | 2003-09-30 20:59:51 +0000 | [diff] [blame] | 29 | #include "cpu.h" |
| 30 | #include "exec-all.h" |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 31 | #include "disas.h" |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 32 | #include "tcg.h" |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 33 | |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 34 | /* code generation context */ |
| 35 | TCGContext tcg_ctx; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 36 | |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 37 | uint16_t gen_opc_buf[OPC_BUF_SIZE]; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 38 | TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE]; |
bellard | c468787 | 2005-01-03 23:44:44 +0000 | [diff] [blame] | 39 | |
| 40 | target_ulong gen_opc_pc[OPC_BUF_SIZE]; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 41 | uint16_t gen_opc_icount[OPC_BUF_SIZE]; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 42 | uint8_t gen_opc_instr_start[OPC_BUF_SIZE]; |
bellard | f76af4b | 2003-06-24 13:21:23 +0000 | [diff] [blame] | 43 | #if defined(TARGET_I386) |
| 44 | uint8_t gen_opc_cc_op[OPC_BUF_SIZE]; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 45 | #elif defined(TARGET_SPARC) |
bellard | c468787 | 2005-01-03 23:44:44 +0000 | [diff] [blame] | 46 | target_ulong gen_opc_npc[OPC_BUF_SIZE]; |
bellard | c3278b7 | 2005-03-20 12:43:29 +0000 | [diff] [blame] | 47 | target_ulong gen_opc_jump_pc[2]; |
ths | 823029f | 2007-12-02 06:10:04 +0000 | [diff] [blame] | 48 | #elif defined(TARGET_MIPS) || defined(TARGET_SH4) |
bellard | 30d6cb8 | 2005-12-05 19:56:07 +0000 | [diff] [blame] | 49 | uint32_t gen_opc_hflags[OPC_BUF_SIZE]; |
bellard | f76af4b | 2003-06-24 13:21:23 +0000 | [diff] [blame] | 50 | #endif |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 51 | |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 52 | /* XXX: suppress that */ |
blueswir1 | d07bde8 | 2007-12-11 19:35:45 +0000 | [diff] [blame] | 53 | unsigned long code_gen_max_block_size(void) |
| 54 | { |
| 55 | static unsigned long max; |
| 56 | |
| 57 | if (max == 0) { |
pbrook | a208e54 | 2008-03-31 17:07:36 +0000 | [diff] [blame] | 58 | max = TCG_MAX_OP_SIZE; |
blueswir1 | d07bde8 | 2007-12-11 19:35:45 +0000 | [diff] [blame] | 59 | #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 60 | #include "tcg-opc.h" |
blueswir1 | d07bde8 | 2007-12-11 19:35:45 +0000 | [diff] [blame] | 61 | #undef DEF |
| 62 | max *= OPC_MAX_SIZE; |
| 63 | } |
| 64 | |
| 65 | return max; |
| 66 | } |
| 67 | |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 68 | void cpu_gen_init(void) |
| 69 | { |
| 70 | tcg_context_init(&tcg_ctx); |
| 71 | tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf), |
blueswir1 | a20e31d | 2008-04-08 19:29:54 +0000 | [diff] [blame] | 72 | CPU_TEMP_BUF_NLONGS * sizeof(long)); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 73 | } |
| 74 | |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 75 | /* return non zero if the very first instruction is invalid so that |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 76 | the virtual CPU can trigger an exception. |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 77 | |
| 78 | '*gen_code_size_ptr' contains the size of the generated code (host |
| 79 | code). |
| 80 | */ |
blueswir1 | d07bde8 | 2007-12-11 19:35:45 +0000 | [diff] [blame] | 81 | int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr) |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 82 | { |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 83 | TCGContext *s = &tcg_ctx; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 84 | uint8_t *gen_code_buf; |
| 85 | int gen_code_size; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 86 | #ifdef CONFIG_PROFILER |
| 87 | int64_t ti; |
| 88 | #endif |
| 89 | |
| 90 | #ifdef CONFIG_PROFILER |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 91 | s->tb_count1++; /* includes aborted translations because of |
| 92 | exceptions */ |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 93 | ti = profile_getclock(); |
| 94 | #endif |
| 95 | tcg_func_start(s); |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 96 | |
ths | 2cfc5f1 | 2008-07-18 18:01:29 +0000 | [diff] [blame] | 97 | gen_intermediate_code(env, tb); |
| 98 | |
bellard | ec6338b | 2007-11-08 14:25:03 +0000 | [diff] [blame] | 99 | /* generate machine code */ |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 100 | gen_code_buf = tb->tc_ptr; |
bellard | ec6338b | 2007-11-08 14:25:03 +0000 | [diff] [blame] | 101 | tb->tb_next_offset[0] = 0xffff; |
| 102 | tb->tb_next_offset[1] = 0xffff; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 103 | s->tb_next_offset = tb->tb_next_offset; |
bellard | 4cbb86e | 2003-09-17 22:53:29 +0000 | [diff] [blame] | 104 | #ifdef USE_DIRECT_JUMP |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 105 | s->tb_jmp_offset = tb->tb_jmp_offset; |
| 106 | s->tb_next = NULL; |
bellard | ec6338b | 2007-11-08 14:25:03 +0000 | [diff] [blame] | 107 | /* the following two entries are optional (only used for string ops) */ |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 108 | /* XXX: not used ? */ |
bellard | ec6338b | 2007-11-08 14:25:03 +0000 | [diff] [blame] | 109 | tb->tb_jmp_offset[2] = 0xffff; |
| 110 | tb->tb_jmp_offset[3] = 0xffff; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 111 | #else |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 112 | s->tb_jmp_offset = NULL; |
| 113 | s->tb_next = tb->tb_next; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 114 | #endif |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 115 | |
| 116 | #ifdef CONFIG_PROFILER |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 117 | s->tb_count++; |
| 118 | s->interm_time += profile_getclock() - ti; |
| 119 | s->code_time -= profile_getclock(); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 120 | #endif |
aurel32 | 54604f7 | 2008-12-07 20:35:00 +0000 | [diff] [blame] | 121 | gen_code_size = tcg_gen_code(s, gen_code_buf); |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 122 | *gen_code_size_ptr = gen_code_size; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 123 | #ifdef CONFIG_PROFILER |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 124 | s->code_time += profile_getclock(); |
| 125 | s->code_in_len += tb->size; |
| 126 | s->code_out_len += gen_code_size; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 129 | #ifdef DEBUG_DISAS |
aliguori | 8fec2b8 | 2009-01-15 22:36:53 +0000 | [diff] [blame] | 130 | if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 131 | qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr); |
| 132 | log_disas(tb->tc_ptr, *gen_code_size_ptr); |
| 133 | qemu_log("\n"); |
aliguori | 31b1a7b | 2009-01-15 22:35:09 +0000 | [diff] [blame] | 134 | qemu_log_flush(); |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 135 | } |
| 136 | #endif |
| 137 | return 0; |
| 138 | } |
| 139 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 140 | /* The cpu state corresponding to 'searched_pc' is restored. |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 141 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 142 | int cpu_restore_state(TranslationBlock *tb, |
bellard | 58fe2f1 | 2004-02-16 22:11:32 +0000 | [diff] [blame] | 143 | CPUState *env, unsigned long searched_pc, |
| 144 | void *puc) |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 145 | { |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 146 | TCGContext *s = &tcg_ctx; |
| 147 | int j; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 148 | unsigned long tc_ptr; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 149 | #ifdef CONFIG_PROFILER |
| 150 | int64_t ti; |
| 151 | #endif |
| 152 | |
| 153 | #ifdef CONFIG_PROFILER |
| 154 | ti = profile_getclock(); |
| 155 | #endif |
| 156 | tcg_func_start(s); |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 157 | |
ths | 2cfc5f1 | 2008-07-18 18:01:29 +0000 | [diff] [blame] | 158 | gen_intermediate_code_pc(env, tb); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 159 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 160 | if (use_icount) { |
| 161 | /* Reset the cycle counter to the start of the block. */ |
| 162 | env->icount_decr.u16.low += tb->icount; |
| 163 | /* Clear the IO flag. */ |
| 164 | env->can_do_io = 0; |
| 165 | } |
| 166 | |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 167 | /* find opc index corresponding to search_pc */ |
| 168 | tc_ptr = (unsigned long)tb->tc_ptr; |
| 169 | if (searched_pc < tc_ptr) |
| 170 | return -1; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 171 | |
| 172 | s->tb_next_offset = tb->tb_next_offset; |
| 173 | #ifdef USE_DIRECT_JUMP |
| 174 | s->tb_jmp_offset = tb->tb_jmp_offset; |
| 175 | s->tb_next = NULL; |
| 176 | #else |
| 177 | s->tb_jmp_offset = NULL; |
| 178 | s->tb_next = tb->tb_next; |
| 179 | #endif |
aurel32 | 54604f7 | 2008-12-07 20:35:00 +0000 | [diff] [blame] | 180 | j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 181 | if (j < 0) |
| 182 | return -1; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 183 | /* now find start of instruction before */ |
| 184 | while (gen_opc_instr_start[j] == 0) |
| 185 | j--; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 186 | env->icount_decr.u16.low -= gen_opc_icount[j]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 187 | |
aurel32 | d2856f1 | 2008-04-28 00:32:32 +0000 | [diff] [blame] | 188 | gen_pc_load(env, tb, searched_pc, j, puc); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 189 | |
| 190 | #ifdef CONFIG_PROFILER |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 191 | s->restore_time += profile_getclock() - ti; |
| 192 | s->restore_count++; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 193 | #endif |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |