Statistics
| Branch: | Revision:

root / translate-all.c @ 8c0d577e

History | View | Annotate | Download (4.6 kB)

1 d19893da bellard
/*
2 d19893da bellard
 *  Host code generation
3 5fafdf24 ths
 *
4 d19893da bellard
 *  Copyright (c) 2003 Fabrice Bellard
5 d19893da bellard
 *
6 d19893da bellard
 * This library is free software; you can redistribute it and/or
7 d19893da bellard
 * modify it under the terms of the GNU Lesser General Public
8 d19893da bellard
 * License as published by the Free Software Foundation; either
9 d19893da bellard
 * version 2 of the License, or (at your option) any later version.
10 d19893da bellard
 *
11 d19893da bellard
 * This library is distributed in the hope that it will be useful,
12 d19893da bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 d19893da bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 d19893da bellard
 * Lesser General Public License for more details.
15 d19893da bellard
 *
16 d19893da bellard
 * You should have received a copy of the GNU Lesser General Public
17 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 d19893da bellard
 */
19 d19893da bellard
#include <stdarg.h>
20 d19893da bellard
#include <stdlib.h>
21 d19893da bellard
#include <stdio.h>
22 d19893da bellard
#include <string.h>
23 d19893da bellard
#include <inttypes.h>
24 d19893da bellard
25 d19893da bellard
#include "config.h"
26 2054396a bellard
27 af5ad107 bellard
#define NO_CPU_IO_DEFS
28 d3eead2e bellard
#include "cpu.h"
29 d3eead2e bellard
#include "exec-all.h"
30 d19893da bellard
#include "disas.h"
31 57fec1fe bellard
#include "tcg.h"
32 29e922b6 Blue Swirl
#include "qemu-timer.h"
33 d19893da bellard
34 57fec1fe bellard
/* code generation context */
35 57fec1fe bellard
TCGContext tcg_ctx;
36 d19893da bellard
37 d19893da bellard
uint16_t gen_opc_buf[OPC_BUF_SIZE];
38 57fec1fe bellard
TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
39 c4687878 bellard
40 c4687878 bellard
target_ulong gen_opc_pc[OPC_BUF_SIZE];
41 2e70f6ef pbrook
uint16_t gen_opc_icount[OPC_BUF_SIZE];
42 d19893da bellard
uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
43 d19893da bellard
44 57fec1fe bellard
/* XXX: suppress that */
45 d07bde88 blueswir1
unsigned long code_gen_max_block_size(void)
46 d07bde88 blueswir1
{
47 d07bde88 blueswir1
    static unsigned long max;
48 d07bde88 blueswir1
49 d07bde88 blueswir1
    if (max == 0) {
50 a208e54a pbrook
        max = TCG_MAX_OP_SIZE;
51 d07bde88 blueswir1
#define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
52 57fec1fe bellard
#include "tcg-opc.h"
53 d07bde88 blueswir1
#undef DEF
54 d07bde88 blueswir1
        max *= OPC_MAX_SIZE;
55 d07bde88 blueswir1
    }
56 d07bde88 blueswir1
57 d07bde88 blueswir1
    return max;
58 d07bde88 blueswir1
}
59 d07bde88 blueswir1
60 57fec1fe bellard
void cpu_gen_init(void)
61 57fec1fe bellard
{
62 57fec1fe bellard
    tcg_context_init(&tcg_ctx); 
63 57fec1fe bellard
    tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
64 a20e31dc blueswir1
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
65 57fec1fe bellard
}
66 57fec1fe bellard
67 d19893da bellard
/* return non zero if the very first instruction is invalid so that
68 5fafdf24 ths
   the virtual CPU can trigger an exception.
69 d19893da bellard

70 d19893da bellard
   '*gen_code_size_ptr' contains the size of the generated code (host
71 d19893da bellard
   code).
72 d19893da bellard
*/
73 d07bde88 blueswir1
int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
74 d19893da bellard
{
75 57fec1fe bellard
    TCGContext *s = &tcg_ctx;
76 d19893da bellard
    uint8_t *gen_code_buf;
77 d19893da bellard
    int gen_code_size;
78 57fec1fe bellard
#ifdef CONFIG_PROFILER
79 57fec1fe bellard
    int64_t ti;
80 57fec1fe bellard
#endif
81 57fec1fe bellard
82 57fec1fe bellard
#ifdef CONFIG_PROFILER
83 b67d9a52 bellard
    s->tb_count1++; /* includes aborted translations because of
84 b67d9a52 bellard
                       exceptions */
85 57fec1fe bellard
    ti = profile_getclock();
86 57fec1fe bellard
#endif
87 57fec1fe bellard
    tcg_func_start(s);
88 d19893da bellard
89 2cfc5f17 ths
    gen_intermediate_code(env, tb);
90 2cfc5f17 ths
91 ec6338ba bellard
    /* generate machine code */
92 57fec1fe bellard
    gen_code_buf = tb->tc_ptr;
93 ec6338ba bellard
    tb->tb_next_offset[0] = 0xffff;
94 ec6338ba bellard
    tb->tb_next_offset[1] = 0xffff;
95 57fec1fe bellard
    s->tb_next_offset = tb->tb_next_offset;
96 4cbb86e1 bellard
#ifdef USE_DIRECT_JUMP
97 57fec1fe bellard
    s->tb_jmp_offset = tb->tb_jmp_offset;
98 57fec1fe bellard
    s->tb_next = NULL;
99 d19893da bellard
#else
100 57fec1fe bellard
    s->tb_jmp_offset = NULL;
101 57fec1fe bellard
    s->tb_next = tb->tb_next;
102 d19893da bellard
#endif
103 57fec1fe bellard
104 57fec1fe bellard
#ifdef CONFIG_PROFILER
105 b67d9a52 bellard
    s->tb_count++;
106 b67d9a52 bellard
    s->interm_time += profile_getclock() - ti;
107 b67d9a52 bellard
    s->code_time -= profile_getclock();
108 57fec1fe bellard
#endif
109 54604f74 aurel32
    gen_code_size = tcg_gen_code(s, gen_code_buf);
110 d19893da bellard
    *gen_code_size_ptr = gen_code_size;
111 57fec1fe bellard
#ifdef CONFIG_PROFILER
112 b67d9a52 bellard
    s->code_time += profile_getclock();
113 b67d9a52 bellard
    s->code_in_len += tb->size;
114 b67d9a52 bellard
    s->code_out_len += gen_code_size;
115 57fec1fe bellard
#endif
116 57fec1fe bellard
117 d19893da bellard
#ifdef DEBUG_DISAS
118 8fec2b8c aliguori
    if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
119 93fcfe39 aliguori
        qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
120 93fcfe39 aliguori
        log_disas(tb->tc_ptr, *gen_code_size_ptr);
121 93fcfe39 aliguori
        qemu_log("\n");
122 31b1a7b4 aliguori
        qemu_log_flush();
123 d19893da bellard
    }
124 d19893da bellard
#endif
125 d19893da bellard
    return 0;
126 d19893da bellard
}
127 d19893da bellard
128 5fafdf24 ths
/* The cpu state corresponding to 'searched_pc' is restored.
129 d19893da bellard
 */
130 5fafdf24 ths
int cpu_restore_state(TranslationBlock *tb,
131 58fe2f10 bellard
                      CPUState *env, unsigned long searched_pc,
132 58fe2f10 bellard
                      void *puc)
133 d19893da bellard
{
134 57fec1fe bellard
    TCGContext *s = &tcg_ctx;
135 57fec1fe bellard
    int j;
136 d19893da bellard
    unsigned long tc_ptr;
137 57fec1fe bellard
#ifdef CONFIG_PROFILER
138 57fec1fe bellard
    int64_t ti;
139 57fec1fe bellard
#endif
140 57fec1fe bellard
141 57fec1fe bellard
#ifdef CONFIG_PROFILER
142 57fec1fe bellard
    ti = profile_getclock();
143 57fec1fe bellard
#endif
144 57fec1fe bellard
    tcg_func_start(s);
145 d19893da bellard
146 2cfc5f17 ths
    gen_intermediate_code_pc(env, tb);
147 3b46e624 ths
148 2e70f6ef pbrook
    if (use_icount) {
149 2e70f6ef pbrook
        /* Reset the cycle counter to the start of the block.  */
150 2e70f6ef pbrook
        env->icount_decr.u16.low += tb->icount;
151 2e70f6ef pbrook
        /* Clear the IO flag.  */
152 2e70f6ef pbrook
        env->can_do_io = 0;
153 2e70f6ef pbrook
    }
154 2e70f6ef pbrook
155 d19893da bellard
    /* find opc index corresponding to search_pc */
156 d19893da bellard
    tc_ptr = (unsigned long)tb->tc_ptr;
157 d19893da bellard
    if (searched_pc < tc_ptr)
158 d19893da bellard
        return -1;
159 57fec1fe bellard
160 57fec1fe bellard
    s->tb_next_offset = tb->tb_next_offset;
161 57fec1fe bellard
#ifdef USE_DIRECT_JUMP
162 57fec1fe bellard
    s->tb_jmp_offset = tb->tb_jmp_offset;
163 57fec1fe bellard
    s->tb_next = NULL;
164 57fec1fe bellard
#else
165 57fec1fe bellard
    s->tb_jmp_offset = NULL;
166 57fec1fe bellard
    s->tb_next = tb->tb_next;
167 57fec1fe bellard
#endif
168 54604f74 aurel32
    j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
169 57fec1fe bellard
    if (j < 0)
170 57fec1fe bellard
        return -1;
171 d19893da bellard
    /* now find start of instruction before */
172 d19893da bellard
    while (gen_opc_instr_start[j] == 0)
173 d19893da bellard
        j--;
174 2e70f6ef pbrook
    env->icount_decr.u16.low -= gen_opc_icount[j];
175 3b46e624 ths
176 d2856f1a aurel32
    gen_pc_load(env, tb, searched_pc, j, puc);
177 57fec1fe bellard
178 57fec1fe bellard
#ifdef CONFIG_PROFILER
179 b67d9a52 bellard
    s->restore_time += profile_getclock() - ti;
180 b67d9a52 bellard
    s->restore_count++;
181 57fec1fe bellard
#endif
182 d19893da bellard
    return 0;
183 d19893da bellard
}