OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_66/] [or1ksim/] [cpu/] [or32/] [dyngen.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1452 nogj
/* 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
"  struct x_ref *next_x_ref = dp->xrefs;\n"
75
"\n"
76
"  while(opq) {\n"
77
"    /* For now, only store offsets in the x-ref table */\n"
78
"    if(next_x_ref && (next_x_ref->or_addr == pc)) {\n"
79
"      next_x_ref->dyn_addr = (void *)(host_cur - dp->host_page);\n"
80
"      next_x_ref = next_x_ref->next;\n"
81
"    }\n"
82
"\n"
83
"    /* Patch the dyn_addr of the xrefs for infinite loops */\n"
84
"    if(opq->jump_local == 2)\n"
85
"      opq->xref->dyn_addr = (void *)(host_cur - dp->host_page);\n"
86
"\n"
87
"    for(i = 0, ops = opq->ops; i < opq->num_ops; i++, ops++) {\n"
88
"      switch(*ops) {\n";
89
 
90
static const char *c_sw_file_tail =
91
"      }\n"
92
"    }\n"
93
"    opq = opq->next;\n"
94
"    pc += 4;\n"
95
"  }\n"
96
"\n"
97
"  dp->host_len = host_cur - dp->host_page;\n"
98
"  dp->host_page = realloc(dp->host_page, dp->host_len);\n"
99
"}\n";
100
 
101
static const char *c_rel_file_head =
102
"#include <stdio.h> /* To get printf... */\n"
103
"#include <stdlib.h>\n"
104
"\n"
105
"#include \"config.h\"\n"
106
"\n"
107
"#include <inttypes.h>\n"
108
"\n"
109
"#include \"arch.h\"\n"
110
"#include \"spr_defs.h\"\n"
111
"#include \"i386_regs.h\"\n"
112
"#include \"opcode/or32.h\"\n"
113
"#include \"abstract.h\"\n"
114
"#include \"tick.h\"\n"
115
"#include \"execute.h\"\n"
116
"#include \"sprs.h\"\n"
117
"#include \"dyn_rec.h\"\n"
118
"#include \"op_support.h\"\n"
119
"#include \"%s\"\n"
120
"\n"
121
"void do_scheduler(void); /* FIXME: Remove */\n"
122
"void analysis(struct iqueue_entry *current); /* FIXME: Remove */\n"
123
"void do_sched_wrap(void); /* FIXME: Remove */\n"
124
"void simprintf(oraddr_t stackaddr, unsigned long regparam); /* FIXME: Remove */\n"
125
"\n"
126
"void patch_relocs(struct op_queue *opq, void *host_page)\n"
127
"{\n"
128
"  unsigned int *ops, *ops_param, i;\n"
129
"\n"
130
"  while(opq) {\n"
131
"    for(i = 0, ops = opq->ops, ops_param = opq->ops_param; i < opq->num_ops; i++, ops++) {\n"
132
"      switch(*ops) {\n";
133
 
134
static const char *c_rel_file_tail =
135
"      }\n"
136
"    }\n"
137
"    opq = opq->next;\n"
138
"  }\n"
139
"}\n";
140
 
141
static void gen_func_proto(FILE *f, const char *name, int *params)
142
{
143
  int i;
144
 
145
  fprintf(f, "void gen_%s(struct op_queue *opq, int end", name);
146
  for(i = 0; (i < MAX_PARAMS) && params[i]; i++) {
147
    fprintf(f, ", uorreg_t param%i", i + 1);
148
  }
149
  fprintf(f, ")");
150
}
151
 
152
int main(int argc, char **argv)
153
{
154
  void *obj;
155
  int i, j;
156
  unsigned int len;
157
  char *name;
158
  int params[MAX_PARAMS];
159
  struct reloc reloc;
160
 
161
  FILE *c_file;
162
  FILE *h_file;
163
  FILE *c_sw_file;
164
  FILE *c_rel_file;
165
 
166
  if(argc != 6) {
167
    fprintf(stderr, "Usage: %s <object file> <c file> <c file with gen_code()> <c file with patch_reloc()> <h file>\n", argv[0]);
168
    return 1;
169
  }
170
 
171
  obj = bffs.open_obj(argv[1]);
172
  if(!obj) {
173
    fprintf(stderr, "Unable to open object file %s\n", argv[1]);
174
    return 1;
175
  }
176
 
177
  if(!(c_file = fopen(argv[2], "w"))) {
178
    fprintf(stderr, "Unable to open %s for writting\n", argv[2]);
179
    bffs.close_obj(obj);
180
    return 1;
181
  }
182
 
183
  if(!(c_sw_file = fopen(argv[3], "w"))) {
184
    fprintf(stderr, "Unable to open %s for writting\n", argv[2]);
185
    fclose(c_file);
186
    bffs.close_obj(obj);
187
  }
188
 
189
  if(!(c_rel_file = fopen(argv[4], "w"))) {
190
    fprintf(stderr, "Unable to open %s for writting\n", argv[3]);
191
    fclose(c_file);
192
    fclose(c_sw_file);
193
    bffs.close_obj(obj);
194
    return 1;
195
  }
196
 
197
  if(!(h_file = fopen(argv[5], "w"))) {
198
    fprintf(stderr, "Unable to open %s for writting\n", argv[3]);
199
    fclose(c_file);
200
    fclose(c_sw_file);
201
    fclose(c_rel_file);
202
    bffs.close_obj(obj);
203
    return 1;
204
  }
205
 
206
  fprintf(c_file, c_file_head, argv[5]);
207
  fprintf(c_sw_file, c_sw_file_head, argv[5]);
208
  fprintf(c_rel_file, c_rel_file_head, argv[5]);
209
  fprintf(h_file, "%s", gen_code_proto);
210
 
211
  /* Itterate through the functions in the object file */
212
  for(i = 0; (name = bffs.get_func_name(obj, i)); i++) {
213
    if(strncmp(name, OP_FUNC_PREFIX, strlen(OP_FUNC_PREFIX)))
214
      continue;
215
 
216
    /* Find all the relocations associated with this function */
217
    memset(params, 0, sizeof(params));
218
    for(j = 0; bffs.get_func_reloc(obj, i, j, &reloc); j++) {
219
      //fprintf(stderr, "%s %i %i\n", reloc.name, reloc.func_offset, reloc.addend);
220
      if(strncmp(reloc.name, OP_FUNC_PARAM_PREFIX, strlen(OP_FUNC_PARAM_PREFIX))) {
221
        continue;
222
      }
223
      params[atoi(reloc.name + strlen(OP_FUNC_PARAM_PREFIX)) - 1] = 1;
224
    }
225
 
226
    len = archfs.get_real_func_len(bffs.get_func_start(obj, i),
227
                                   bffs.get_func_len(obj, i), name);
228
 
229
    gen_func_proto(h_file, name, params);
230
    fprintf(h_file, ";\n");
231
 
232
    if(len) {
233
      /* Prototype the operation */
234
      fprintf(h_file, "void %s(void);\n", name);
235
      fprintf(h_file, "#define %s_indx %i\n", name, i);
236
    }
237
 
238
    gen_func_proto(c_file, name, params);
239
    fprintf(c_file, "\n{\n");
240
    if(len) {
241
      fprintf(c_file, "  add_to_opq(opq, end, %s_indx);\n", name);
242
 
243
      for(j = 0; params[j]; j++)
244
        fprintf(c_file, "  add_to_op_params(opq, end, param%i);\n", j + 1);
245
    }
246
    fprintf(c_file, "}\n\n");
247
 
248
    if(!len) {
249
      /* If the operation is of length 0, then just ignore it */
250
      fprintf(stderr, "WARNING: Useless operation (size == 0): %s\n", name);
251
      continue;
252
    }
253
 
254
    fprintf(c_sw_file, "      case %s_indx:\n", name);
255
    fprintf(c_sw_file, "        host_cur = enough_host_page(dp, host_cur, &host_len, %i);\n", len);
256
    fprintf(c_sw_file, "        memcpy(host_cur, %s, %i);\n", name, len);
257
 
258
    fprintf(c_rel_file, "      case %s_indx:\n", name);
259
    for(j = 0; bffs.get_func_reloc(obj, i, j, &reloc); j++) {
260
      /* Ignore nameless relocations, they appear to be absolute */
261
      if(!*reloc.name)
262
        continue;
263
      if(strncmp(reloc.name, OP_FUNC_PARAM_PREFIX, strlen(OP_FUNC_PARAM_PREFIX))) {
264
        /* We have to manually do the relocations when the relocation is
265
         * relative to the PC as the code gets copied to a different location
266
         * during recompilation, where the relative addresses shall be waay out.
267
         */
268
        archfs.gen_func_reloc(c_rel_file, &reloc);
269
      } else {
270
        archfs.gen_reloc(c_rel_file, &reloc,
271
                         atoi(reloc.name + strlen(OP_FUNC_PARAM_PREFIX)));
272
      }
273
    }
274
 
275
    fprintf(c_sw_file, "        host_cur += %i;\n        break;\n\n", len);
276
    fprintf(c_rel_file, "        host_page += %i;\n", len);
277
    /* Count how many parameters where used */
278
    for(j = 0; params[j]; j++);
279
    fprintf(c_rel_file, "        ops_param += %i;\n        break;\n\n", j);
280
  }
281
 
282
  fprintf(c_sw_file, "%s", c_sw_file_tail);
283
  fprintf(c_rel_file, "%s", c_rel_file_tail);
284
 
285
  fclose(h_file);
286
  fclose(c_file);
287
  fclose(c_sw_file);
288
  fclose(c_rel_file);
289
  bffs.close_obj(obj);
290
  return 0;
291
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.