]> git.proxmox.com Git - mirror_qemu.git/blame - translate.c
extracted generic code
[mirror_qemu.git] / translate.c
CommitLineData
d19893da
FB
1/*
2 * Host code generation
3 *
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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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"
27#define IN_OP_I386
28#include "cpu-" TARGET_ARCH ".h"
29#include "exec.h"
30#include "disas.h"
31
32enum {
33#define DEF(s, n, copy_size) INDEX_op_ ## s,
34#include "opc-" TARGET_ARCH ".h"
35#undef DEF
36 NB_OPS,
37};
38
39#include "dyngen.h"
40#include "op-" TARGET_ARCH ".h"
41
42uint16_t gen_opc_buf[OPC_BUF_SIZE];
43uint32_t gen_opparam_buf[OPPARAM_BUF_SIZE];
44uint32_t gen_opc_pc[OPC_BUF_SIZE];
45uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
46
47
48#ifdef DEBUG_DISAS
49static const char *op_str[] = {
50#define DEF(s, n, copy_size) #s,
51#include "opc-" TARGET_ARCH ".h"
52#undef DEF
53};
54
55static uint8_t op_nb_args[] = {
56#define DEF(s, n, copy_size) n,
57#include "opc-" TARGET_ARCH ".h"
58#undef DEF
59};
60
61void dump_ops(const uint16_t *opc_buf, const uint32_t *opparam_buf)
62{
63 const uint16_t *opc_ptr;
64 const uint32_t *opparam_ptr;
65 int c, n, i;
66
67 opc_ptr = opc_buf;
68 opparam_ptr = opparam_buf;
69 for(;;) {
70 c = *opc_ptr++;
71 n = op_nb_args[c];
72 fprintf(logfile, "0x%04x: %s",
73 (int)(opc_ptr - opc_buf - 1), op_str[c]);
74 for(i = 0; i < n; i++) {
75 fprintf(logfile, " 0x%x", opparam_ptr[i]);
76 }
77 fprintf(logfile, "\n");
78 if (c == INDEX_op_end)
79 break;
80 opparam_ptr += n;
81 }
82}
83
84#endif
85
86/* return non zero if the very first instruction is invalid so that
87 the virtual CPU can trigger an exception.
88
89 '*gen_code_size_ptr' contains the size of the generated code (host
90 code).
91*/
92int cpu_gen_code(TranslationBlock *tb,
93 int max_code_size, int *gen_code_size_ptr)
94{
95 uint8_t *gen_code_buf;
96 int gen_code_size;
97
98 if (gen_intermediate_code(tb, 0) < 0)
99 return -1;
100
101 /* generate machine code */
102 tb->tb_next_offset[0] = 0xffff;
103 tb->tb_next_offset[1] = 0xffff;
104 gen_code_buf = tb->tc_ptr;
105 gen_code_size = dyngen_code(gen_code_buf, tb->tb_next_offset,
106#ifdef USE_DIRECT_JUMP
107 tb->tb_jmp_offset,
108#else
109 NULL,
110#endif
111 gen_opc_buf, gen_opparam_buf);
112 *gen_code_size_ptr = gen_code_size;
113#ifdef DEBUG_DISAS
114 if (loglevel) {
115 fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
116 disas(logfile, gen_code_buf, *gen_code_size_ptr, 1, 0);
117 fprintf(logfile, "\n");
118 fflush(logfile);
119 }
120#endif
121 return 0;
122}
123
124static const unsigned short opc_copy_size[] = {
125#define DEF(s, n, copy_size) copy_size,
126#include "opc-i386.h"
127#undef DEF
128};
129
130/* The simulated PC corresponding to
131 'searched_pc' in the generated code is searched. 0 is returned if
132 found. *found_pc contains the found PC.
133 */
134int cpu_search_pc(TranslationBlock *tb,
135 uint32_t *found_pc, unsigned long searched_pc)
136{
137 int j, c;
138 unsigned long tc_ptr;
139 uint16_t *opc_ptr;
140
141 if (gen_intermediate_code(tb, 1) < 0)
142 return -1;
143
144 /* find opc index corresponding to search_pc */
145 tc_ptr = (unsigned long)tb->tc_ptr;
146 if (searched_pc < tc_ptr)
147 return -1;
148 j = 0;
149 opc_ptr = gen_opc_buf;
150 for(;;) {
151 c = *opc_ptr;
152 if (c == INDEX_op_end)
153 return -1;
154 tc_ptr += opc_copy_size[c];
155 if (searched_pc < tc_ptr)
156 break;
157 opc_ptr++;
158 }
159 j = opc_ptr - gen_opc_buf;
160 /* now find start of instruction before */
161 while (gen_opc_instr_start[j] == 0)
162 j--;
163 *found_pc = gen_opc_pc[j];
164 return 0;
165}
166
167