4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #define NO_CPU_IO_DEFS
34 /* code generation context */
37 uint16_t gen_opc_buf
[OPC_BUF_SIZE
];
38 TCGArg gen_opparam_buf
[OPPARAM_BUF_SIZE
];
40 target_ulong gen_opc_pc
[OPC_BUF_SIZE
];
41 uint8_t gen_opc_instr_start
[OPC_BUF_SIZE
];
42 #if defined(TARGET_I386)
43 uint8_t gen_opc_cc_op
[OPC_BUF_SIZE
];
44 #elif defined(TARGET_SPARC)
45 target_ulong gen_opc_npc
[OPC_BUF_SIZE
];
46 target_ulong gen_opc_jump_pc
[2];
47 #elif defined(TARGET_HPPA)
48 target_ulong gen_opc_npc
[OPC_BUF_SIZE
];
49 #elif defined(TARGET_MIPS) || defined(TARGET_SH4)
50 uint32_t gen_opc_hflags
[OPC_BUF_SIZE
];
53 int code_copy_enabled
= 1;
55 #ifdef CONFIG_PROFILER
56 int64_t dyngen_tb_count1
;
57 int64_t dyngen_tb_count
;
58 int64_t dyngen_op_count
;
59 int64_t dyngen_old_op_count
;
60 int64_t dyngen_tcg_del_op_count
;
61 int dyngen_op_count_max
;
62 int64_t dyngen_code_in_len
;
63 int64_t dyngen_code_out_len
;
64 int64_t dyngen_interm_time
;
65 int64_t dyngen_code_time
;
66 int64_t dyngen_restore_count
;
67 int64_t dyngen_restore_time
;
70 /* XXX: suppress that */
71 unsigned long code_gen_max_block_size(void)
73 static unsigned long max
;
76 max
= TCG_MAX_OP_SIZE
;
77 #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
86 void cpu_gen_init(void)
88 tcg_context_init(&tcg_ctx
);
89 tcg_set_frame(&tcg_ctx
, TCG_AREG0
, offsetof(CPUState
, temp_buf
),
90 CPU_TEMP_BUF_NLONGS
* sizeof(long));
93 /* return non zero if the very first instruction is invalid so that
94 the virtual CPU can trigger an exception.
96 '*gen_code_size_ptr' contains the size of the generated code (host
99 int cpu_gen_code(CPUState
*env
, TranslationBlock
*tb
, int *gen_code_size_ptr
)
101 TCGContext
*s
= &tcg_ctx
;
102 uint8_t *gen_code_buf
;
104 #ifdef CONFIG_PROFILER
108 #ifdef CONFIG_PROFILER
109 dyngen_tb_count1
++; /* includes aborted translations because of
111 ti
= profile_getclock();
115 if (gen_intermediate_code(env
, tb
) < 0)
118 /* generate machine code */
119 gen_code_buf
= tb
->tc_ptr
;
120 tb
->tb_next_offset
[0] = 0xffff;
121 tb
->tb_next_offset
[1] = 0xffff;
122 s
->tb_next_offset
= tb
->tb_next_offset
;
123 #ifdef USE_DIRECT_JUMP
124 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
126 /* the following two entries are optional (only used for string ops) */
127 /* XXX: not used ? */
128 tb
->tb_jmp_offset
[2] = 0xffff;
129 tb
->tb_jmp_offset
[3] = 0xffff;
131 s
->tb_jmp_offset
= NULL
;
132 s
->tb_next
= tb
->tb_next
;
135 #ifdef CONFIG_PROFILER
137 dyngen_interm_time
+= profile_getclock() - ti
;
138 dyngen_code_time
-= profile_getclock();
140 gen_code_size
= dyngen_code(s
, gen_code_buf
);
141 *gen_code_size_ptr
= gen_code_size
;
142 #ifdef CONFIG_PROFILER
143 dyngen_code_time
+= profile_getclock();
144 dyngen_code_in_len
+= tb
->size
;
145 dyngen_code_out_len
+= gen_code_size
;
149 if (loglevel
& CPU_LOG_TB_OUT_ASM
) {
150 fprintf(logfile
, "OUT: [size=%d]\n", *gen_code_size_ptr
);
151 disas(logfile
, tb
->tc_ptr
, *gen_code_size_ptr
);
152 fprintf(logfile
, "\n");
159 /* The cpu state corresponding to 'searched_pc' is restored.
161 int cpu_restore_state(TranslationBlock
*tb
,
162 CPUState
*env
, unsigned long searched_pc
,
165 TCGContext
*s
= &tcg_ctx
;
167 unsigned long tc_ptr
;
168 #ifdef CONFIG_PROFILER
172 #ifdef CONFIG_PROFILER
173 ti
= profile_getclock();
177 if (gen_intermediate_code_pc(env
, tb
) < 0)
180 /* find opc index corresponding to search_pc */
181 tc_ptr
= (unsigned long)tb
->tc_ptr
;
182 if (searched_pc
< tc_ptr
)
185 s
->tb_next_offset
= tb
->tb_next_offset
;
186 #ifdef USE_DIRECT_JUMP
187 s
->tb_jmp_offset
= tb
->tb_jmp_offset
;
190 s
->tb_jmp_offset
= NULL
;
191 s
->tb_next
= tb
->tb_next
;
193 j
= dyngen_code_search_pc(s
, (uint8_t *)tc_ptr
, searched_pc
- tc_ptr
);
196 /* now find start of instruction before */
197 while (gen_opc_instr_start
[j
] == 0)
200 gen_pc_load(env
, tb
, searched_pc
, j
, puc
);
202 #ifdef CONFIG_PROFILER
203 dyngen_restore_time
+= profile_getclock() - ti
;
204 dyngen_restore_count
++;