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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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