]>
Commit | Line | Data |
---|---|---|
d19893da FB |
1 | /* |
2 | * Host code generation | |
5fafdf24 | 3 | * |
d19893da FB |
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
d19893da FB |
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" | |
2054396a | 26 | |
af5ad107 | 27 | #define NO_CPU_IO_DEFS |
d3eead2e | 28 | #include "cpu.h" |
d19893da | 29 | #include "disas.h" |
57fec1fe | 30 | #include "tcg.h" |
29e922b6 | 31 | #include "qemu-timer.h" |
d19893da | 32 | |
57fec1fe FB |
33 | /* code generation context */ |
34 | TCGContext tcg_ctx; | |
d19893da | 35 | |
57fec1fe FB |
36 | void cpu_gen_init(void) |
37 | { | |
38 | tcg_context_init(&tcg_ctx); | |
57fec1fe FB |
39 | } |
40 | ||
d19893da | 41 | /* return non zero if the very first instruction is invalid so that |
5fafdf24 | 42 | the virtual CPU can trigger an exception. |
d19893da FB |
43 | |
44 | '*gen_code_size_ptr' contains the size of the generated code (host | |
45 | code). | |
46 | */ | |
9349b4f9 | 47 | int cpu_gen_code(CPUArchState *env, TranslationBlock *tb, int *gen_code_size_ptr) |
d19893da | 48 | { |
57fec1fe | 49 | TCGContext *s = &tcg_ctx; |
d19893da FB |
50 | uint8_t *gen_code_buf; |
51 | int gen_code_size; | |
57fec1fe FB |
52 | #ifdef CONFIG_PROFILER |
53 | int64_t ti; | |
54 | #endif | |
55 | ||
56 | #ifdef CONFIG_PROFILER | |
b67d9a52 FB |
57 | s->tb_count1++; /* includes aborted translations because of |
58 | exceptions */ | |
57fec1fe FB |
59 | ti = profile_getclock(); |
60 | #endif | |
61 | tcg_func_start(s); | |
d19893da | 62 | |
2cfc5f17 TS |
63 | gen_intermediate_code(env, tb); |
64 | ||
ec6338ba | 65 | /* generate machine code */ |
57fec1fe | 66 | gen_code_buf = tb->tc_ptr; |
ec6338ba FB |
67 | tb->tb_next_offset[0] = 0xffff; |
68 | tb->tb_next_offset[1] = 0xffff; | |
57fec1fe | 69 | s->tb_next_offset = tb->tb_next_offset; |
4cbb86e1 | 70 | #ifdef USE_DIRECT_JUMP |
57fec1fe FB |
71 | s->tb_jmp_offset = tb->tb_jmp_offset; |
72 | s->tb_next = NULL; | |
d19893da | 73 | #else |
57fec1fe FB |
74 | s->tb_jmp_offset = NULL; |
75 | s->tb_next = tb->tb_next; | |
d19893da | 76 | #endif |
57fec1fe FB |
77 | |
78 | #ifdef CONFIG_PROFILER | |
b67d9a52 FB |
79 | s->tb_count++; |
80 | s->interm_time += profile_getclock() - ti; | |
81 | s->code_time -= profile_getclock(); | |
57fec1fe | 82 | #endif |
54604f74 | 83 | gen_code_size = tcg_gen_code(s, gen_code_buf); |
d19893da | 84 | *gen_code_size_ptr = gen_code_size; |
57fec1fe | 85 | #ifdef CONFIG_PROFILER |
b67d9a52 FB |
86 | s->code_time += profile_getclock(); |
87 | s->code_in_len += tb->size; | |
88 | s->code_out_len += gen_code_size; | |
57fec1fe FB |
89 | #endif |
90 | ||
d19893da | 91 | #ifdef DEBUG_DISAS |
8fec2b8c | 92 | if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { |
93fcfe39 AL |
93 | qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr); |
94 | log_disas(tb->tc_ptr, *gen_code_size_ptr); | |
95 | qemu_log("\n"); | |
31b1a7b4 | 96 | qemu_log_flush(); |
d19893da FB |
97 | } |
98 | #endif | |
99 | return 0; | |
100 | } | |
101 | ||
5fafdf24 | 102 | /* The cpu state corresponding to 'searched_pc' is restored. |
d19893da | 103 | */ |
5fafdf24 | 104 | int cpu_restore_state(TranslationBlock *tb, |
6375e09e | 105 | CPUArchState *env, uintptr_t searched_pc) |
d19893da | 106 | { |
57fec1fe FB |
107 | TCGContext *s = &tcg_ctx; |
108 | int j; | |
6375e09e | 109 | uintptr_t tc_ptr; |
57fec1fe FB |
110 | #ifdef CONFIG_PROFILER |
111 | int64_t ti; | |
112 | #endif | |
113 | ||
114 | #ifdef CONFIG_PROFILER | |
115 | ti = profile_getclock(); | |
116 | #endif | |
117 | tcg_func_start(s); | |
d19893da | 118 | |
2cfc5f17 | 119 | gen_intermediate_code_pc(env, tb); |
3b46e624 | 120 | |
2e70f6ef PB |
121 | if (use_icount) { |
122 | /* Reset the cycle counter to the start of the block. */ | |
123 | env->icount_decr.u16.low += tb->icount; | |
124 | /* Clear the IO flag. */ | |
125 | env->can_do_io = 0; | |
126 | } | |
127 | ||
d19893da | 128 | /* find opc index corresponding to search_pc */ |
6375e09e | 129 | tc_ptr = (uintptr_t)tb->tc_ptr; |
d19893da FB |
130 | if (searched_pc < tc_ptr) |
131 | return -1; | |
57fec1fe FB |
132 | |
133 | s->tb_next_offset = tb->tb_next_offset; | |
134 | #ifdef USE_DIRECT_JUMP | |
135 | s->tb_jmp_offset = tb->tb_jmp_offset; | |
136 | s->tb_next = NULL; | |
137 | #else | |
138 | s->tb_jmp_offset = NULL; | |
139 | s->tb_next = tb->tb_next; | |
140 | #endif | |
54604f74 | 141 | j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr); |
57fec1fe FB |
142 | if (j < 0) |
143 | return -1; | |
d19893da | 144 | /* now find start of instruction before */ |
ab1103de | 145 | while (s->gen_opc_instr_start[j] == 0) { |
d19893da | 146 | j--; |
ab1103de | 147 | } |
c9c99c22 | 148 | env->icount_decr.u16.low -= s->gen_opc_icount[j]; |
3b46e624 | 149 | |
e87b7cb0 | 150 | restore_state_to_opc(env, tb, j); |
57fec1fe FB |
151 | |
152 | #ifdef CONFIG_PROFILER | |
b67d9a52 FB |
153 | s->restore_time += profile_getclock() - ti; |
154 | s->restore_count++; | |
57fec1fe | 155 | #endif |
d19893da FB |
156 | return 0; |
157 | } |