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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include <stdarg.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <inttypes.h> |
| 24 | |
| 25 | #include "config.h" |
bellard | 2054396 | 2003-06-15 23:28:43 +0000 | [diff] [blame] | 26 | |
bellard | af5ad10 | 2004-01-04 23:28:12 +0000 | [diff] [blame] | 27 | #define NO_CPU_IO_DEFS |
bellard | d3eead2 | 2003-09-30 20:59:51 +0000 | [diff] [blame] | 28 | #include "cpu.h" |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 29 | #include "disas.h" |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 30 | #include "tcg.h" |
Blue Swirl | 29e922b | 2010-03-29 19:24:00 +0000 | [diff] [blame] | 31 | #include "qemu-timer.h" |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 32 | |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 33 | /* code generation context */ |
| 34 | TCGContext tcg_ctx; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 35 | |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 36 | uint16_t gen_opc_buf[OPC_BUF_SIZE]; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 37 | TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE]; |
bellard | c468787 | 2005-01-03 23:44:44 +0000 | [diff] [blame] | 38 | |
| 39 | target_ulong gen_opc_pc[OPC_BUF_SIZE]; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 40 | uint16_t gen_opc_icount[OPC_BUF_SIZE]; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 41 | uint8_t gen_opc_instr_start[OPC_BUF_SIZE]; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 42 | |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 43 | void cpu_gen_init(void) |
| 44 | { |
| 45 | tcg_context_init(&tcg_ctx); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 46 | } |
| 47 | |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 48 | /* return non zero if the very first instruction is invalid so that |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 49 | the virtual CPU can trigger an exception. |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 50 | |
| 51 | '*gen_code_size_ptr' contains the size of the generated code (host |
| 52 | code). |
| 53 | */ |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 54 | int cpu_gen_code(CPUArchState *env, TranslationBlock *tb, int *gen_code_size_ptr) |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 55 | { |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 56 | TCGContext *s = &tcg_ctx; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 57 | uint8_t *gen_code_buf; |
| 58 | int gen_code_size; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 59 | #ifdef CONFIG_PROFILER |
| 60 | int64_t ti; |
| 61 | #endif |
| 62 | |
| 63 | #ifdef CONFIG_PROFILER |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 64 | s->tb_count1++; /* includes aborted translations because of |
| 65 | exceptions */ |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 66 | ti = profile_getclock(); |
| 67 | #endif |
| 68 | tcg_func_start(s); |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 69 | |
ths | 2cfc5f1 | 2008-07-18 18:01:29 +0000 | [diff] [blame] | 70 | gen_intermediate_code(env, tb); |
| 71 | |
bellard | ec6338b | 2007-11-08 14:25:03 +0000 | [diff] [blame] | 72 | /* generate machine code */ |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 73 | gen_code_buf = tb->tc_ptr; |
bellard | ec6338b | 2007-11-08 14:25:03 +0000 | [diff] [blame] | 74 | tb->tb_next_offset[0] = 0xffff; |
| 75 | tb->tb_next_offset[1] = 0xffff; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 76 | s->tb_next_offset = tb->tb_next_offset; |
bellard | 4cbb86e | 2003-09-17 22:53:29 +0000 | [diff] [blame] | 77 | #ifdef USE_DIRECT_JUMP |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 78 | s->tb_jmp_offset = tb->tb_jmp_offset; |
| 79 | s->tb_next = NULL; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 80 | #else |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 81 | s->tb_jmp_offset = NULL; |
| 82 | s->tb_next = tb->tb_next; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 83 | #endif |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 84 | |
| 85 | #ifdef CONFIG_PROFILER |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 86 | s->tb_count++; |
| 87 | s->interm_time += profile_getclock() - ti; |
| 88 | s->code_time -= profile_getclock(); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 89 | #endif |
aurel32 | 54604f7 | 2008-12-07 20:35:00 +0000 | [diff] [blame] | 90 | gen_code_size = tcg_gen_code(s, gen_code_buf); |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 91 | *gen_code_size_ptr = gen_code_size; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 92 | #ifdef CONFIG_PROFILER |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 93 | s->code_time += profile_getclock(); |
| 94 | s->code_in_len += tb->size; |
| 95 | s->code_out_len += gen_code_size; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 98 | #ifdef DEBUG_DISAS |
aliguori | 8fec2b8 | 2009-01-15 22:36:53 +0000 | [diff] [blame] | 99 | if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 100 | qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr); |
| 101 | log_disas(tb->tc_ptr, *gen_code_size_ptr); |
| 102 | qemu_log("\n"); |
aliguori | 31b1a7b | 2009-01-15 22:35:09 +0000 | [diff] [blame] | 103 | qemu_log_flush(); |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 104 | } |
| 105 | #endif |
| 106 | return 0; |
| 107 | } |
| 108 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 109 | /* The cpu state corresponding to 'searched_pc' is restored. |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 110 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 111 | int cpu_restore_state(TranslationBlock *tb, |
Stefan Weil | 6375e09 | 2012-04-06 22:26:15 +0200 | [diff] [blame] | 112 | CPUArchState *env, uintptr_t searched_pc) |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 113 | { |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 114 | TCGContext *s = &tcg_ctx; |
| 115 | int j; |
Stefan Weil | 6375e09 | 2012-04-06 22:26:15 +0200 | [diff] [blame] | 116 | uintptr_t tc_ptr; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 117 | #ifdef CONFIG_PROFILER |
| 118 | int64_t ti; |
| 119 | #endif |
| 120 | |
| 121 | #ifdef CONFIG_PROFILER |
| 122 | ti = profile_getclock(); |
| 123 | #endif |
| 124 | tcg_func_start(s); |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 125 | |
ths | 2cfc5f1 | 2008-07-18 18:01:29 +0000 | [diff] [blame] | 126 | gen_intermediate_code_pc(env, tb); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 127 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 128 | if (use_icount) { |
| 129 | /* Reset the cycle counter to the start of the block. */ |
| 130 | env->icount_decr.u16.low += tb->icount; |
| 131 | /* Clear the IO flag. */ |
| 132 | env->can_do_io = 0; |
| 133 | } |
| 134 | |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 135 | /* find opc index corresponding to search_pc */ |
Stefan Weil | 6375e09 | 2012-04-06 22:26:15 +0200 | [diff] [blame] | 136 | tc_ptr = (uintptr_t)tb->tc_ptr; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 137 | if (searched_pc < tc_ptr) |
| 138 | return -1; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 139 | |
| 140 | s->tb_next_offset = tb->tb_next_offset; |
| 141 | #ifdef USE_DIRECT_JUMP |
| 142 | s->tb_jmp_offset = tb->tb_jmp_offset; |
| 143 | s->tb_next = NULL; |
| 144 | #else |
| 145 | s->tb_jmp_offset = NULL; |
| 146 | s->tb_next = tb->tb_next; |
| 147 | #endif |
aurel32 | 54604f7 | 2008-12-07 20:35:00 +0000 | [diff] [blame] | 148 | 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] | 149 | if (j < 0) |
| 150 | return -1; |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 151 | /* now find start of instruction before */ |
| 152 | while (gen_opc_instr_start[j] == 0) |
| 153 | j--; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 154 | env->icount_decr.u16.low -= gen_opc_icount[j]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 155 | |
Stefan Weil | e87b7cb | 2011-04-18 06:39:52 +0000 | [diff] [blame] | 156 | restore_state_to_opc(env, tb, j); |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 157 | |
| 158 | #ifdef CONFIG_PROFILER |
bellard | b67d9a5 | 2008-05-23 09:57:34 +0000 | [diff] [blame] | 159 | s->restore_time += profile_getclock() - ti; |
| 160 | s->restore_count++; |
bellard | 57fec1f | 2008-02-01 10:50:11 +0000 | [diff] [blame] | 161 | #endif |
bellard | d19893d | 2003-06-15 19:58:51 +0000 | [diff] [blame] | 162 | return 0; |
| 163 | } |