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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [cpu/] [or32/] [generate.c] - Blame information for rev 755

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/* generate.c -- generates file execgen.c from instruction set
2
 
3
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
4
   Copyright (C) 2005 György `nog' Jeney, nog@sdf.lonestar.org
5
   Copyright (C) 2008 Embecosm Limited
6
 
7
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
10
 
11
   This program is free software; you can redistribute it and/or modify it
12
   under the terms of the GNU General Public License as published by the Free
13
   Software Foundation; either version 3 of the License, or (at your option)
14
   any later version.
15
 
16
   This program is distributed in the hope that it will be useful, but WITHOUT
17
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19
   more details.
20
 
21
   You should have received a copy of the GNU General Public License along
22
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
 
24
/* This program is commented throughout in a fashion suitable for processing
25
   with Doxygen. */
26
 
27
 
28
/* Autoconf and/or portability configuration */
29
#include "config.h"
30
#include "port.h"
31
 
32
/* System includes */
33
#include <stdlib.h>
34
#include <stdio.h>
35
#include <stdarg.h>
36
 
37
/* Package includes */
38
#include "opcode/or32.h"
39
 
40
static char *in_file;
41
static char *out_file;
42
 
43
/* Whether this instruction stores something in register */
44
static int write_to_reg;
45
 
46
static int out_lines = 0;
47
 
48
static int shift_fprintf(int level, FILE *f, const char *fmt, ...)
49
{
50
  va_list ap;
51
  int i;
52
 
53
  va_start(ap, fmt);
54
  for(i = 0; i < level; i++)
55
    fprintf(f, "  ");
56
 
57
  i = vfprintf(f, fmt, ap);
58
  va_end(ap);
59
 
60
  out_lines++;
61
  return i + (level * 2);
62
}
63
 
64
/* Generates a execute sequence for one instruction */
65
int output_function (FILE *fo, const char *func_name, int level)
66
{
67
  FILE *fi;
68
  int olevel;
69
  int line_num = 0;
70
 
71
  if ((fi = fopen (in_file, "rt")) == NULL) {
72
    printf("could not open file\n");
73
    return 1;
74
  }
75
 
76
  while (!feof (fi)) {
77
    char line[10000], *str = line;
78 82 jeremybenn
    char *res;
79
 
80
    res = fgets (str, sizeof (line), fi);
81
 
82
    if (NULL == res)
83
      {
84 96 jeremybenn
        fclose (fi);                    /* Mark Jarvin patch */
85 82 jeremybenn
        return  1;
86
      }
87
 
88 19 jeremybenn
    line[sizeof (line) - 1] = 0;
89
    line_num++;
90
    if (strncmp (str, "INSTRUCTION (", 13) == 0) {
91
      char *s;
92
      str += 13;
93 240 julius
      while (isspace ((int)*str)) str++;
94 19 jeremybenn
      s = str;
95
      while (*s && *s != ')') s++;
96
      *s = 0;
97 240 julius
      while (isspace((int)*(s - 1))) s--;
98 19 jeremybenn
      *s = 0;
99
      if (strcmp (str, func_name) == 0) {
100
        olevel = 1;
101
        str += strlen (str) + 1;
102 240 julius
        while (isspace ((int)*str)) str++;
103 19 jeremybenn
        s = str;
104
        while (*s && *s != '\n' && *s != '\r') s++;
105
        *s = 0;
106 240 julius
        while (isspace((int)*(s - 1))) s--;
107 19 jeremybenn
        *s = 0;
108
        /*shift_fprintf (level, fo, "#line %i \"%s\"\n", line_num, in_file);*/
109
        shift_fprintf (level, fo, "%s", str);
110
        shift_fprintf (level, fo, "   /* \"%s\" */\n", func_name);
111
        do {
112 82 jeremybenn
          res = fgets (line, sizeof (line), fi);
113
 
114
          if (NULL == res)
115
            {
116 96 jeremybenn
              fclose (fi);
117 82 jeremybenn
              return  1;
118
            }
119
 
120 19 jeremybenn
          line[sizeof(line) - 1] = 0;
121
          for (str = line; *str; str++) {
122
            if (*str == '{') olevel++;
123
            else if (*str == '}') olevel--;
124
          }
125
          shift_fprintf (level, fo, "%s", line);
126
        } while (olevel);
127
        fclose(fi);
128
        /*shift_fprintf (level, fo, "#line %i \"%s\"\n", out_lines, out_file);*/
129
        return 0;
130
      }
131
    }
132
  }
133
  shift_fprintf (level, fo, "%s ();\n", func_name);
134
 
135
  fclose(fi);
136
  return 0;
137
}
138
 
139
/* Parses operands. */
140
 
141
static int
142
gen_eval_operands (FILE *fo, int insn_index, int level)
143
{
144 230 jeremybenn
  struct insn_op_struct *opd = or1ksim_op_start[insn_index];
145 19 jeremybenn
  int i;
146
  int num_ops;
147
  int nbits = 0;
148
  int set_param = 0;
149
  int dis = 0;
150
  int sbit;
151
  int dis_op = -1;
152
 
153
  write_to_reg = 0;
154
 
155
  shift_fprintf (level, fo, "uorreg_t ");
156
 
157
  /* Count number of operands */
158
  for (i = 0, num_ops = 0;; i++) {
159
    if (!(opd[i].type & OPTYPE_OP))
160
      continue;
161
    if (opd[i].type & OPTYPE_DIS)
162
      continue;
163
    if (num_ops)
164
      fprintf(fo, ", ");
165
    fprintf(fo, "%c", 'a' + num_ops);
166
    num_ops++;
167
    if (opd[i].type & OPTYPE_LAST)
168
      break;
169
  }
170
 
171
  fprintf (fo, ";\n");
172
 
173
  shift_fprintf (level, fo, "/* Number of operands: %i */\n", num_ops);
174
 
175
  i = 0;
176
  num_ops = 0;
177
  do {
178
/*
179
    printf("opd[%i].type<last> = %c\n", i, opd->type & OPTYPE_LAST ? '1' : '0');    printf("opd[%i].type<op> = %c\n", i, opd->type & OPTYPE_OP ? '1' : '0');
180
    printf("opd[%i].type<reg> = %c\n", i, opd->type & OPTYPE_REG ? '1' : '0');
181
    printf("opd[%i].type<sig> = %c\n", i, opd->type & OPTYPE_SIG ? '1' : '0');
182
    printf("opd[%i].type<dis> = %c\n", i, opd->type & OPTYPE_DIS ? '1' : '0');
183
    printf("opd[%i].type<shr> = %i\n", i, opd->type & OPTYPE_SHR);
184
    printf("opd[%i].type<sbit> = %i\n", i, (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR);
185
    printf("opd[%i].data = %i\n", i, opd->data);
186
*/
187
 
188
    if (!nbits)
189
      shift_fprintf (level, fo, "%c = (insn >> %i) & 0x%x;\n", 'a' + num_ops,
190
                     opd->type & OPTYPE_SHR, (1 << opd->data) - 1);
191
    else
192
      shift_fprintf (level, fo, "%c |= ((insn >> %i) & 0x%x) << %i;\n",
193
                     'a' + num_ops, opd->type & OPTYPE_SHR,
194
                     (1 << opd->data) - 1, nbits);
195
 
196
    nbits += opd->data;
197
 
198
    if ((opd->type & OPTYPE_DIS) && (opd->type & OPTYPE_OP)) {
199
      sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
200
      if (opd->type & OPTYPE_SIG)
201
        shift_fprintf (level, fo, "if(%c & 0x%08x) %c |= 0x%x;\n",
202
                       'a' + num_ops, 1 << sbit, 'a' + num_ops,
203
                       0xffffffff << sbit);
204
      opd++;
205
      shift_fprintf (level, fo, "*(orreg_t *)&%c += (orreg_t)cpu_state.reg[(insn >> %i) & 0x%x];\n",
206
                     'a' + num_ops, opd->type & OPTYPE_SHR,
207
                     (1 << opd->data) - 1);
208
      dis = 1;
209
      dis_op = num_ops;
210
    }
211
 
212
    if (opd->type & OPTYPE_OP) {
213
      sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
214
      if (opd->type & OPTYPE_SIG)
215
        shift_fprintf (level, fo, "if(%c & 0x%08x) %c |= 0x%x;\n",
216
                       'a' + num_ops, 1 << sbit, 'a' + num_ops,
217
                       0xffffffff << sbit);
218
      if ((opd->type & OPTYPE_REG) && !dis) {
219
        if(!i) {
220
          shift_fprintf (level, fo, "#define SET_PARAM0(val) cpu_state.reg[a] = val\n");
221 121 jeremybenn
          shift_fprintf (level, fo, "#define REG_PARAM0  a\n");
222 19 jeremybenn
          set_param = 1;
223
        }
224
        shift_fprintf (level, fo, "#define PARAM%i cpu_state.reg[%c]\n", num_ops,
225
                      'a' + num_ops);
226
        if(opd->type & OPTYPE_DST)
227
          write_to_reg = 1;
228
      } else {
229
        shift_fprintf (level, fo, "#define PARAM%i %c\n", num_ops,
230
                       'a' + num_ops);
231
      }
232
      num_ops++;
233
      nbits = 0;
234
      dis = 0;
235
    }
236
 
237
    if ((opd->type & OPTYPE_LAST))
238
      break;
239
    opd++;
240
    i++;
241
  } while (1);
242
 
243 230 jeremybenn
  output_function (fo, or1ksim_or32_opcodes[insn_index].function_name, level);
244 19 jeremybenn
 
245
  if (set_param)
246 121 jeremybenn
    {
247
      shift_fprintf (level, fo, "#undef SET_PARAM0\n");
248
      shift_fprintf (level, fo, "#undef REG_PARAM0\n");
249
    }
250 19 jeremybenn
 
251
  for (i = 0; i < num_ops; i++)
252
    shift_fprintf (level, fo, "#undef PARAM%i\n", i);
253
 
254
  return dis_op;
255
}
256
 
257
/* Generates decode and execute for one instruction instance */
258
static int output_call (FILE *fo, int index, int level)
259
{
260
  int dis_op = -1;
261
 
262
  /*printf ("%i:%s\n", index, insn_name (index));*/
263
 
264
  shift_fprintf (level++, fo, "{\n");
265
 
266
  if (index >= 0)
267
    dis_op = gen_eval_operands (fo, index, level);
268
 
269
  if (index < 0) output_function (fo, "l_invalid", level);
270
 
271
  fprintf (fo, "\n");
272
 
273
  shift_fprintf (level++, fo, "if (do_stats) {\n");
274
 
275
  if (dis_op >= 0)
276
    shift_fprintf (level, fo, "cpu_state.insn_ea = %c;\n", 'a' + dis_op);
277
 
278
  shift_fprintf (level, fo, "current->insn_index = %i;   /* \"%s\" */\n", index,
279 230 jeremybenn
                 or1ksim_insn_name (index));
280 19 jeremybenn
 
281
  shift_fprintf (level, fo, "analysis(current);\n");
282
  shift_fprintf (--level, fo, "}\n");
283
 
284
  if (write_to_reg)
285
    shift_fprintf (level, fo, "cpu_state.reg[0] = 0; /* Repair in case we changed it */\n");
286
  shift_fprintf (--level, fo, "}\n");
287
  return 0;
288
}
289
 
290 226 julius
 
291 19 jeremybenn
/* Generates .c file header */
292
static int generate_header (FILE *fo)
293
{
294
  fprintf (fo, "/* execgen.c -- Automatically generated decoder\n");
295
  fprintf (fo, "\n");
296
  fprintf (fo, "   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org\n");
297
  fprintf (fo, "   Copyright (C) 2008 Embecosm Limited\n");
298
  fprintf (fo, "\n");
299
  fprintf (fo, "   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>\n");
300
  fprintf (fo, "\n");
301
  fprintf (fo, "   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.\n");
302
  fprintf (fo, "\n");
303
  fprintf (fo, "   This program is free software; you can redistribute it and/or modify it\n");
304
  fprintf (fo, "   under the terms of the GNU General Public License as published by the Free\n");
305
  fprintf (fo, "   Software Foundation; either version 3 of the License, or (at your option)\n");
306
  fprintf (fo, "   any later version.\n");
307
  fprintf (fo, "\n");
308
  fprintf (fo, "   This program is distributed in the hope that it will be useful, but WITHOUT\n");
309
  fprintf (fo, "   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n");
310
  fprintf (fo, "   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\n");
311
  fprintf (fo, "   more details.\n");
312
  fprintf (fo, "\n");
313
  fprintf (fo, "   You should have received a copy of the GNU General Public License along\n");
314
  fprintf (fo, "   with this program.  If not, see <http://www.gnu.org/licenses/>.  */\n");
315
  fprintf (fo, "\n");
316
  fprintf (fo, "/* This program is commented throughout in a fashion suitable for processing\n");
317
  fprintf (fo, "   with Doxygen. */\n");
318
  fprintf (fo, "\n");
319
 
320
  fprintf (fo, "/* This file was automatically generated by generate (see\n");
321
  fprintf (fo, "   cpu/or32/generate.c) */\n\n");
322 226 julius
 
323 230 jeremybenn
 
324 226 julius
  return 0;
325
}
326
 
327
/* Generates .c file decode function hedaer */
328
static int generate_decode_function_header (FILE *fo)
329
{
330 100 julius
  fprintf (fo, "typedef union {\n\tfloat fval;\n\tuint32_t hval;\n} FLOAT;\n\n");
331 19 jeremybenn
  fprintf (fo, "static void decode_execute (struct iqueue_entry *current)\n{\n");
332
  fprintf (fo, "  uint32_t insn = current->insn;\n");
333 226 julius
  out_lines +=3;
334 19 jeremybenn
  return 0;
335
}
336
 
337 226 julius
// List of strings which will be printed on a line after "#include "
338
char *include_strings[] = { "<math.h>",
339
                            "<stdint.h>",
340
                            ""}; // Last one must be empty
341
 
342
 
343
/* Generates .c file includes */
344
static int generate_includes (FILE *fo)
345
{
346
  int i;
347
  for (i=0;(strcmp(include_strings[i], "")!=0);i++)
348
    fprintf (fo, "#include %s\n\n", include_strings[i]);
349
  out_lines +=i;
350
  return 0;
351
 
352
}
353
 
354 19 jeremybenn
/* Generates .c file footer */
355 429 julius
static int
356
generate_footer (FILE *fo)
357 19 jeremybenn
{
358
  fprintf (fo, "}\n");
359
  return 0;
360
}
361
 
362
/* Decodes all instructions and generates code for that.  This function
363 230 jeremybenn
   is similar to or1ksim_insn_decode, except it decodes all instructions.
364 82 jeremybenn
 
365
   JPB: Added code to generate an illegal instruction exception for invalid
366
   instructions. */
367 429 julius
static int
368
generate_body (FILE *fo, unsigned long *a, unsigned long cur_mask, int level)
369 19 jeremybenn
{
370
  unsigned long shift = *a;
371
  unsigned long mask;
372
  int i;
373
  int prev_inv = 0;
374
 
375
  if (!(*a & LEAF_FLAG)) {
376
    shift = *a++;
377
    mask = *a++;
378
    shift_fprintf (level, fo, "switch((insn >> %i) & 0x%x) {\n", shift,
379
                   mask);
380
    for (i = 0; i <= mask; i++, a++) {
381
      if (!*a) {
382
        shift_fprintf (level, fo, "case 0x%x:\n", i);
383
        prev_inv = 1;
384
      } else {
385
        if(prev_inv) {
386
          shift_fprintf (++level, fo, "/* Invalid instruction(s) */\n");
387 429 julius
          shift_fprintf (level, fo,
388
                         "except_handle (EXCEPT_ILLEGAL, cpu_state.pc);\n");
389 19 jeremybenn
          shift_fprintf (level--, fo, "break;\n");
390
        }
391
        shift_fprintf (level, fo, "case 0x%x:\n", i);
392 230 jeremybenn
        generate_body (fo, or1ksim_automata + *a, cur_mask | (mask << shift), ++level);
393 19 jeremybenn
        shift_fprintf (level--, fo, "break;\n");
394
        prev_inv = 0;
395
      }
396
    }
397
    if (prev_inv) {
398
      shift_fprintf (++level, fo, "/* Invalid instruction(s) */\n");
399 82 jeremybenn
      shift_fprintf (level, fo,
400
                     "except_handle (EXCEPT_ILLEGAL, cpu_state.pc);\n");
401 19 jeremybenn
      shift_fprintf (level--, fo, "break;\n");
402
    }
403
    shift_fprintf (level, fo, "}\n");
404
  } else {
405
    i = *a & ~LEAF_FLAG;
406
 
407
    /* Final check - do we have direct match?
408 230 jeremybenn
       (based on or1ksim_or32_opcodes this should be the only possibility,
409 19 jeremybenn
       but in case of invalid/missing instruction we must perform a check)  */
410
 
411 230 jeremybenn
    if (or1ksim_ti[i].insn_mask != cur_mask) {
412
      shift_fprintf (level, fo, "/* Not unique: real mask %08lx and current mask %08lx differ - do final check */\n", or1ksim_ti[i].insn_mask, cur_mask);
413 19 jeremybenn
      shift_fprintf (level++, fo, "if((insn & 0x%x) == 0x%x) {\n",
414 230 jeremybenn
                     or1ksim_ti[i].insn_mask, or1ksim_ti[i].insn);
415 19 jeremybenn
    }
416 230 jeremybenn
    shift_fprintf (level, fo, "/* Instruction: %s */\n", or1ksim_or32_opcodes[i].name);
417 19 jeremybenn
 
418
    output_call (fo, i, level);
419
 
420 230 jeremybenn
    if (or1ksim_ti[i].insn_mask != cur_mask) {
421 19 jeremybenn
      shift_fprintf (--level, fo, "} else {\n");
422
      shift_fprintf (++level, fo, "/* Invalid insn */\n");
423
      output_call (fo, -1, level);
424
      shift_fprintf (--level, fo, "}\n");
425
    }
426
  }
427
  return 0;
428
}
429
 
430
/* Main function; it takes two parameters:
431
   input_file(possibly insnset.c) output_file(possibly execgen.c)*/
432
int main (int argc, char *argv[])
433
{
434
  FILE *fo;
435
 
436
  if (argc != 3) {
437
    fprintf (stderr, "USAGE: generate input_file(possibly insnset.c) output_file(possibly execgen.c)\n");
438
    exit (-1);
439
  }
440
 
441
  in_file = argv[1];
442
  out_file = argv[2];
443
  if (!(fo = fopen (argv[2], "wt+"))) {
444
    fprintf (stderr, "Cannot create '%s'.\n", argv[2]);
445
    exit (1);
446
  }
447
 
448 230 jeremybenn
  or1ksim_build_automata (0);
449 19 jeremybenn
  if (generate_header (fo)) {
450
    fprintf (stderr, "generate_header\n");
451
    return 1;
452
  }
453
 
454 226 julius
  if (generate_includes (fo)) {
455
    fprintf (stderr, "generate_includes\n");
456
    return 1;
457
  }
458
 
459
  if (generate_decode_function_header (fo)) {
460
    fprintf (stderr, "generate_decode_function_header\n");
461
    return 1;
462
  }
463
 
464 230 jeremybenn
  if (generate_body (fo, or1ksim_automata, 0, 1)) {
465 19 jeremybenn
    fprintf (stderr, "generate_body\n");
466
    return 1;
467
  }
468
 
469
  if (generate_footer (fo)) {
470
    fprintf (stderr, "generate_footer\n");
471
    return 1;
472
  }
473
 
474
  fclose (fo);
475 230 jeremybenn
  or1ksim_destruct_automata ();
476 19 jeremybenn
  return 0;
477
}
478
 

powered by: WebSVN 2.1.0

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