1 |
19 |
jeremybenn |
/* dyngen.c -- Generates micro operation generating functions
|
2 |
|
|
Copyright (C) 2005 György `nog' Jeney, nog@sdf.lonestar.org
|
3 |
|
|
|
4 |
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
9 |
|
|
(at your option) any later version.
|
10 |
|
|
|
11 |
|
|
This program 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
|
14 |
|
|
GNU General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with this program; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
#include <stdio.h>
|
22 |
|
|
#include <string.h>
|
23 |
|
|
#include <stdlib.h>
|
24 |
|
|
|
25 |
|
|
#include "dyngen.h"
|
26 |
|
|
|
27 |
|
|
#define OP_FUNC_PREFIX "op_"
|
28 |
|
|
#define OP_FUNC_PARAM_PREFIX "__op_param"
|
29 |
|
|
/* Have to add to to make sure that the param[] is 0 */
|
30 |
|
|
#define MAX_PARAMS (3 + 1)
|
31 |
|
|
|
32 |
|
|
static const char *c_file_head =
|
33 |
|
|
"#include \"config.h\"\n"
|
34 |
|
|
"\n"
|
35 |
|
|
"#include <inttypes.h>\n"
|
36 |
|
|
"\n"
|
37 |
|
|
"#include \"arch.h\"\n"
|
38 |
|
|
"#include \"opcode/or32.h\"\n"
|
39 |
|
|
"#include \"spr-defs.h\"\n"
|
40 |
|
|
"#include \"i386-regs.h\"\n"
|
41 |
|
|
"#include \"abstract.h\"\n"
|
42 |
|
|
"\n"
|
43 |
|
|
"#include \"dyn-rec.h\"\n"
|
44 |
|
|
"#include \"%s\"\n"
|
45 |
|
|
"\n";
|
46 |
|
|
|
47 |
|
|
static const char *gen_code_proto =
|
48 |
|
|
"void gen_code(struct op_queue *opq, struct dyn_page *dp);\n"
|
49 |
|
|
"void patch_relocs(struct op_queue *opq, void *host_page);\n"
|
50 |
|
|
"\n";
|
51 |
|
|
|
52 |
|
|
static const char *c_sw_file_head =
|
53 |
|
|
"#include <stdlib.h>\n"
|
54 |
|
|
"#include <string.h>\n"
|
55 |
|
|
"\n"
|
56 |
|
|
"#include \"config.h\"\n"
|
57 |
|
|
"\n"
|
58 |
|
|
"#include <inttypes.h>\n"
|
59 |
|
|
"\n"
|
60 |
|
|
"#include \"arch.h\"\n"
|
61 |
|
|
"#include \"opcode/or32.h\"\n"
|
62 |
|
|
"#include \"spr-defs.h\"\n"
|
63 |
|
|
"#include \"i386-regs.h\"\n"
|
64 |
|
|
"#include \"abstract.h\"\n"
|
65 |
|
|
"#include \"dyn-rec.h\"\n"
|
66 |
|
|
"#include \"%s\"\n"
|
67 |
|
|
"\n"
|
68 |
|
|
"void gen_code(struct op_queue *opq, struct dyn_page *dp)\n"
|
69 |
|
|
"{\n"
|
70 |
|
|
" unsigned int *ops, i;\n"
|
71 |
|
|
" unsigned int host_len = dp->host_len;\n"
|
72 |
|
|
" void *host_cur = dp->host_page;\n"
|
73 |
|
|
" oraddr_t pc = dp->or_page;\n"
|
74 |
|
|
" void **loc = dp->locs;\n"
|
75 |
|
|
"\n"
|
76 |
|
|
" while(opq) {\n"
|
77 |
|
|
" if(opq->next)\n"
|
78 |
|
|
" *loc++ = (void *)(host_cur - dp->host_page);"
|
79 |
|
|
"\n"
|
80 |
|
|
" for(i = 0, ops = opq->ops; i < opq->num_ops; i++, ops++) {\n"
|
81 |
|
|
" switch(*ops) {\n"
|
82 |
|
|
" case op_mark_loc_indx:\n"
|
83 |
|
|
" opq->ops_param[0] = host_cur - dp->host_page;\n"
|
84 |
|
|
" break;\n";
|
85 |
|
|
|
86 |
|
|
static const char *c_sw_file_tail =
|
87 |
|
|
" }\n"
|
88 |
|
|
" }\n"
|
89 |
|
|
" opq = opq->next;\n"
|
90 |
|
|
" pc += 4;\n"
|
91 |
|
|
" }\n"
|
92 |
|
|
"\n"
|
93 |
|
|
" dp->host_len = host_cur - dp->host_page;\n"
|
94 |
|
|
" dp->host_page = realloc(dp->host_page, dp->host_len);\n"
|
95 |
|
|
"}\n";
|
96 |
|
|
|
97 |
|
|
static const char *c_rel_file_head =
|
98 |
|
|
"#include <stdio.h> /* To get printf... */\n"
|
99 |
|
|
"#include <stdlib.h>\n"
|
100 |
|
|
"\n"
|
101 |
|
|
"#include \"config.h\"\n"
|
102 |
|
|
"\n"
|
103 |
|
|
"#include <inttypes.h>\n"
|
104 |
|
|
"\n"
|
105 |
|
|
"#include \"arch.h\"\n"
|
106 |
|
|
"#include \"spr-defs.h\"\n"
|
107 |
|
|
"#include \"i386-regs.h\"\n"
|
108 |
|
|
"#include \"opcode/or32.h\"\n"
|
109 |
|
|
"#include \"abstract.h\"\n"
|
110 |
|
|
"#include \"tick.h\"\n"
|
111 |
|
|
"#include \"execute.h\"\n"
|
112 |
|
|
"#include \"sprs.h\"\n"
|
113 |
|
|
"#include \"dyn-rec.h\"\n"
|
114 |
|
|
"#include \"op-support.h\"\n"
|
115 |
|
|
"#include \"%s\"\n"
|
116 |
|
|
"\n"
|
117 |
|
|
"void do_scheduler(void); /* FIXME: Remove */\n"
|
118 |
|
|
"void do_sched_wrap(void); /* FIXME: Remove */\n"
|
119 |
|
|
"void do_sched_wrap_delay(void); /* FIXME: Remove */\n"
|
120 |
|
|
"void simprintf(oraddr_t stackaddr, unsigned long regparam); /* FIXME: Remove */\n"
|
121 |
|
|
"\n"
|
122 |
|
|
"void patch_relocs(struct op_queue *opq, void *host_page)\n"
|
123 |
|
|
"{\n"
|
124 |
|
|
" unsigned int *ops, *ops_param, i;\n"
|
125 |
|
|
"\n"
|
126 |
|
|
" while(opq) {\n"
|
127 |
|
|
" for(i = 0, ops = opq->ops, ops_param = opq->ops_param; i < opq->num_ops; i++, ops++) {\n"
|
128 |
|
|
" switch(*ops) {\n";
|
129 |
|
|
|
130 |
|
|
static const char *c_rel_file_tail =
|
131 |
|
|
" }\n"
|
132 |
|
|
" }\n"
|
133 |
|
|
" opq = opq->next;\n"
|
134 |
|
|
" }\n"
|
135 |
|
|
"}\n";
|
136 |
|
|
|
137 |
|
|
static void gen_func_proto(FILE *f, const char *name, int *params)
|
138 |
|
|
{
|
139 |
|
|
int i;
|
140 |
|
|
|
141 |
|
|
fprintf(f, "void gen_%s(struct op_queue *opq, int end", name);
|
142 |
|
|
for(i = 0; (i < MAX_PARAMS) && params[i]; i++) {
|
143 |
|
|
fprintf(f, ", uorreg_t param%i", i + 1);
|
144 |
|
|
}
|
145 |
|
|
fprintf(f, ")");
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
int main(int argc, char **argv)
|
149 |
|
|
{
|
150 |
|
|
void *obj;
|
151 |
|
|
int i, j;
|
152 |
|
|
unsigned int len;
|
153 |
|
|
char *name;
|
154 |
|
|
int params[MAX_PARAMS];
|
155 |
|
|
struct reloc reloc;
|
156 |
|
|
|
157 |
|
|
FILE *c_file;
|
158 |
|
|
FILE *h_file;
|
159 |
|
|
FILE *c_sw_file;
|
160 |
|
|
FILE *c_rel_file;
|
161 |
|
|
|
162 |
|
|
if(argc != 6) {
|
163 |
|
|
fprintf(stderr, "Usage: %s <object file> <c file> <c file with gen_code()> <c file with patch_reloc()> <h file>\n", argv[0]);
|
164 |
|
|
return 1;
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
obj = bffs.open_obj(argv[1]);
|
168 |
|
|
if(!obj) {
|
169 |
|
|
fprintf(stderr, "Unable to open object file %s\n", argv[1]);
|
170 |
|
|
return 1;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
if(!(c_file = fopen(argv[2], "w"))) {
|
174 |
|
|
fprintf(stderr, "Unable to open %s for writting\n", argv[2]);
|
175 |
|
|
bffs.close_obj(obj);
|
176 |
|
|
return 1;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
if(!(c_sw_file = fopen(argv[3], "w"))) {
|
180 |
|
|
fprintf(stderr, "Unable to open %s for writting\n", argv[2]);
|
181 |
|
|
fclose(c_file);
|
182 |
|
|
bffs.close_obj(obj);
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
if(!(c_rel_file = fopen(argv[4], "w"))) {
|
186 |
|
|
fprintf(stderr, "Unable to open %s for writting\n", argv[3]);
|
187 |
|
|
fclose(c_file);
|
188 |
|
|
fclose(c_sw_file);
|
189 |
|
|
bffs.close_obj(obj);
|
190 |
|
|
return 1;
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
if(!(h_file = fopen(argv[5], "w"))) {
|
194 |
|
|
fprintf(stderr, "Unable to open %s for writting\n", argv[3]);
|
195 |
|
|
fclose(c_file);
|
196 |
|
|
fclose(c_sw_file);
|
197 |
|
|
fclose(c_rel_file);
|
198 |
|
|
bffs.close_obj(obj);
|
199 |
|
|
return 1;
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
fprintf(c_file, c_file_head, argv[5]);
|
203 |
|
|
fprintf(c_sw_file, c_sw_file_head, argv[5]);
|
204 |
|
|
fprintf(c_rel_file, c_rel_file_head, argv[5]);
|
205 |
|
|
fprintf(h_file, "%s", gen_code_proto);
|
206 |
|
|
|
207 |
|
|
/* Itterate through the functions in the object file */
|
208 |
|
|
for(i = 0; (name = bffs.get_func_name(obj, i)); i++) {
|
209 |
|
|
if(strncmp(name, OP_FUNC_PREFIX, strlen(OP_FUNC_PREFIX)))
|
210 |
|
|
continue;
|
211 |
|
|
|
212 |
|
|
/* Find all the relocations associated with this function */
|
213 |
|
|
memset(params, 0, sizeof(params));
|
214 |
|
|
for(j = 0; bffs.get_func_reloc(obj, i, j, &reloc); j++) {
|
215 |
|
|
//fprintf(stderr, "%s %i %i\n", reloc.name, reloc.func_offset, reloc.addend);
|
216 |
|
|
if(strncmp(reloc.name, OP_FUNC_PARAM_PREFIX, strlen(OP_FUNC_PARAM_PREFIX))) {
|
217 |
|
|
continue;
|
218 |
|
|
}
|
219 |
|
|
params[atoi(reloc.name + strlen(OP_FUNC_PARAM_PREFIX)) - 1] = 1;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
len = archfs.get_real_func_len(bffs.get_func_start(obj, i),
|
223 |
|
|
bffs.get_func_len(obj, i), name);
|
224 |
|
|
|
225 |
|
|
gen_func_proto(h_file, name, params);
|
226 |
|
|
fprintf(h_file, ";\n");
|
227 |
|
|
|
228 |
|
|
if(len) {
|
229 |
|
|
/* Prototype the operation */
|
230 |
|
|
fprintf(h_file, "void %s(void);\n", name);
|
231 |
|
|
fprintf(h_file, "#define %s_indx %i\n", name, i);
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
gen_func_proto(c_file, name, params);
|
235 |
|
|
fprintf(c_file, "\n{\n");
|
236 |
|
|
if(len) {
|
237 |
|
|
fprintf(c_file, " add_to_opq(opq, end, %s_indx);\n", name);
|
238 |
|
|
|
239 |
|
|
for(j = 0; params[j]; j++)
|
240 |
|
|
fprintf(c_file, " add_to_op_params(opq, end, param%i);\n", j + 1);
|
241 |
|
|
}
|
242 |
|
|
fprintf(c_file, "}\n\n");
|
243 |
|
|
|
244 |
|
|
if(!len) {
|
245 |
|
|
/* If the operation is of length 0, then just ignore it */
|
246 |
|
|
fprintf(stderr, "WARNING: Useless operation (size == 0): %s\n", name);
|
247 |
|
|
continue;
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
fprintf(c_sw_file, " case %s_indx:\n", name);
|
251 |
|
|
fprintf(c_sw_file, " host_cur = enough_host_page(dp, host_cur, &host_len, %i);\n", len);
|
252 |
|
|
fprintf(c_sw_file, " memcpy(host_cur, %s, %i);\n", name, len);
|
253 |
|
|
|
254 |
|
|
fprintf(c_rel_file, " case %s_indx:\n", name);
|
255 |
|
|
for(j = 0; bffs.get_func_reloc(obj, i, j, &reloc); j++) {
|
256 |
|
|
/* Ignore nameless relocations, they appear to be absolute */
|
257 |
|
|
if(!*reloc.name)
|
258 |
|
|
continue;
|
259 |
|
|
if(strncmp(reloc.name, OP_FUNC_PARAM_PREFIX, strlen(OP_FUNC_PARAM_PREFIX))) {
|
260 |
|
|
/* We have to manually do the relocations when the relocation is
|
261 |
|
|
* relative to the PC as the code gets copied to a different location
|
262 |
|
|
* during recompilation, where the relative addresses shall be waay out.
|
263 |
|
|
*/
|
264 |
|
|
archfs.gen_func_reloc(c_rel_file, &reloc);
|
265 |
|
|
} else {
|
266 |
|
|
archfs.gen_reloc(c_rel_file, &reloc,
|
267 |
|
|
atoi(reloc.name + strlen(OP_FUNC_PARAM_PREFIX)));
|
268 |
|
|
}
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
fprintf(c_sw_file, " host_cur += %i;\n break;\n\n", len);
|
272 |
|
|
fprintf(c_rel_file, " host_page += %i;\n", len);
|
273 |
|
|
/* Count how many parameters where used */
|
274 |
|
|
for(j = 0; params[j]; j++);
|
275 |
|
|
fprintf(c_rel_file, " ops_param += %i;\n break;\n\n", j);
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
fprintf(c_sw_file, "%s", c_sw_file_tail);
|
279 |
|
|
fprintf(c_rel_file, "%s", c_rel_file_tail);
|
280 |
|
|
|
281 |
|
|
fprintf(h_file, "\n#define op_mark_loc_indx %i\n", i);
|
282 |
|
|
|
283 |
|
|
fclose(h_file);
|
284 |
|
|
fclose(c_file);
|
285 |
|
|
fclose(c_sw_file);
|
286 |
|
|
fclose(c_rel_file);
|
287 |
|
|
bffs.close_obj(obj);
|
288 |
|
|
return 0;
|
289 |
|
|
}
|