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

Subversion Repositories openrisc

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

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
        return  1;
85
      }
86
 
87 19 jeremybenn
    line[sizeof (line) - 1] = 0;
88
    line_num++;
89
    if (strncmp (str, "INSTRUCTION (", 13) == 0) {
90
      char *s;
91
      str += 13;
92
      while (isspace (*str)) str++;
93
      s = str;
94
      while (*s && *s != ')') s++;
95
      *s = 0;
96
      while (isspace(*(s - 1))) s--;
97
      *s = 0;
98
      if (strcmp (str, func_name) == 0) {
99
        olevel = 1;
100
        str += strlen (str) + 1;
101
        while (isspace (*str)) str++;
102
        s = str;
103
        while (*s && *s != '\n' && *s != '\r') s++;
104
        *s = 0;
105
        while (isspace(*(s - 1))) s--;
106
        *s = 0;
107
        /*shift_fprintf (level, fo, "#line %i \"%s\"\n", line_num, in_file);*/
108
        shift_fprintf (level, fo, "%s", str);
109
        shift_fprintf (level, fo, "   /* \"%s\" */\n", func_name);
110
        do {
111 82 jeremybenn
          res = fgets (line, sizeof (line), fi);
112
 
113
          if (NULL == res)
114
            {
115
              return  1;
116
            }
117
 
118 19 jeremybenn
          line[sizeof(line) - 1] = 0;
119
          for (str = line; *str; str++) {
120
            if (*str == '{') olevel++;
121
            else if (*str == '}') olevel--;
122
          }
123
          shift_fprintf (level, fo, "%s", line);
124
        } while (olevel);
125
        fclose(fi);
126
        /*shift_fprintf (level, fo, "#line %i \"%s\"\n", out_lines, out_file);*/
127
        return 0;
128
      }
129
    }
130
  }
131
  shift_fprintf (level, fo, "%s ();\n", func_name);
132
 
133
  fclose(fi);
134
  return 0;
135
}
136
 
137
/* Parses operands. */
138
 
139
static int
140
gen_eval_operands (FILE *fo, int insn_index, int level)
141
{
142
  struct insn_op_struct *opd = op_start[insn_index];
143
  int i;
144
  int num_ops;
145
  int nbits = 0;
146
  int set_param = 0;
147
  int dis = 0;
148
  int sbit;
149
  int dis_op = -1;
150
 
151
  write_to_reg = 0;
152
 
153
  shift_fprintf (level, fo, "uorreg_t ");
154
 
155
  /* Count number of operands */
156
  for (i = 0, num_ops = 0;; i++) {
157
    if (!(opd[i].type & OPTYPE_OP))
158
      continue;
159
    if (opd[i].type & OPTYPE_DIS)
160
      continue;
161
    if (num_ops)
162
      fprintf(fo, ", ");
163
    fprintf(fo, "%c", 'a' + num_ops);
164
    num_ops++;
165
    if (opd[i].type & OPTYPE_LAST)
166
      break;
167
  }
168
 
169
  fprintf (fo, ";\n");
170
 
171
  shift_fprintf (level, fo, "/* Number of operands: %i */\n", num_ops);
172
 
173
  i = 0;
174
  num_ops = 0;
175
  do {
176
/*
177
    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');
178
    printf("opd[%i].type<reg> = %c\n", i, opd->type & OPTYPE_REG ? '1' : '0');
179
    printf("opd[%i].type<sig> = %c\n", i, opd->type & OPTYPE_SIG ? '1' : '0');
180
    printf("opd[%i].type<dis> = %c\n", i, opd->type & OPTYPE_DIS ? '1' : '0');
181
    printf("opd[%i].type<shr> = %i\n", i, opd->type & OPTYPE_SHR);
182
    printf("opd[%i].type<sbit> = %i\n", i, (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR);
183
    printf("opd[%i].data = %i\n", i, opd->data);
184
*/
185
 
186
    if (!nbits)
187
      shift_fprintf (level, fo, "%c = (insn >> %i) & 0x%x;\n", 'a' + num_ops,
188
                     opd->type & OPTYPE_SHR, (1 << opd->data) - 1);
189
    else
190
      shift_fprintf (level, fo, "%c |= ((insn >> %i) & 0x%x) << %i;\n",
191
                     'a' + num_ops, opd->type & OPTYPE_SHR,
192
                     (1 << opd->data) - 1, nbits);
193
 
194
    nbits += opd->data;
195
 
196
    if ((opd->type & OPTYPE_DIS) && (opd->type & OPTYPE_OP)) {
197
      sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
198
      if (opd->type & OPTYPE_SIG)
199
        shift_fprintf (level, fo, "if(%c & 0x%08x) %c |= 0x%x;\n",
200
                       'a' + num_ops, 1 << sbit, 'a' + num_ops,
201
                       0xffffffff << sbit);
202
      opd++;
203
      shift_fprintf (level, fo, "*(orreg_t *)&%c += (orreg_t)cpu_state.reg[(insn >> %i) & 0x%x];\n",
204
                     'a' + num_ops, opd->type & OPTYPE_SHR,
205
                     (1 << opd->data) - 1);
206
      dis = 1;
207
      dis_op = num_ops;
208
    }
209
 
210
    if (opd->type & OPTYPE_OP) {
211
      sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
212
      if (opd->type & OPTYPE_SIG)
213
        shift_fprintf (level, fo, "if(%c & 0x%08x) %c |= 0x%x;\n",
214
                       'a' + num_ops, 1 << sbit, 'a' + num_ops,
215
                       0xffffffff << sbit);
216
      if ((opd->type & OPTYPE_REG) && !dis) {
217
        if(!i) {
218
          shift_fprintf (level, fo, "#define SET_PARAM0(val) cpu_state.reg[a] = val\n");
219
          set_param = 1;
220
        }
221
        shift_fprintf (level, fo, "#define PARAM%i cpu_state.reg[%c]\n", num_ops,
222
                      'a' + num_ops);
223
        if(opd->type & OPTYPE_DST)
224
          write_to_reg = 1;
225
      } else {
226
        shift_fprintf (level, fo, "#define PARAM%i %c\n", num_ops,
227
                       'a' + num_ops);
228
      }
229
      num_ops++;
230
      nbits = 0;
231
      dis = 0;
232
    }
233
 
234
    if ((opd->type & OPTYPE_LAST))
235
      break;
236
    opd++;
237
    i++;
238
  } while (1);
239
 
240
  output_function (fo, or32_opcodes[insn_index].function_name, level);
241
 
242
  if (set_param)
243
    shift_fprintf (level, fo, "#undef SET_PARAM\n");
244
 
245
  for (i = 0; i < num_ops; i++)
246
    shift_fprintf (level, fo, "#undef PARAM%i\n", i);
247
 
248
  return dis_op;
249
}
250
 
251
/* Generates decode and execute for one instruction instance */
252
static int output_call (FILE *fo, int index, int level)
253
{
254
  int dis_op = -1;
255
 
256
  /*printf ("%i:%s\n", index, insn_name (index));*/
257
 
258
  shift_fprintf (level++, fo, "{\n");
259
 
260
  if (index >= 0)
261
    dis_op = gen_eval_operands (fo, index, level);
262
 
263
  if (index < 0) output_function (fo, "l_invalid", level);
264
 
265
  fprintf (fo, "\n");
266
 
267
  shift_fprintf (level++, fo, "if (do_stats) {\n");
268
 
269
  if (dis_op >= 0)
270
    shift_fprintf (level, fo, "cpu_state.insn_ea = %c;\n", 'a' + dis_op);
271
 
272
  shift_fprintf (level, fo, "current->insn_index = %i;   /* \"%s\" */\n", index,
273
                 insn_name (index));
274
 
275
  shift_fprintf (level, fo, "analysis(current);\n");
276
  shift_fprintf (--level, fo, "}\n");
277
 
278
  if (write_to_reg)
279
    shift_fprintf (level, fo, "cpu_state.reg[0] = 0; /* Repair in case we changed it */\n");
280
  shift_fprintf (--level, fo, "}\n");
281
  return 0;
282
}
283
 
284
/* Generates .c file header */
285
static int generate_header (FILE *fo)
286
{
287
  fprintf (fo, "/* execgen.c -- Automatically generated decoder\n");
288
  fprintf (fo, "\n");
289
  fprintf (fo, "   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org\n");
290
  fprintf (fo, "   Copyright (C) 2008 Embecosm Limited\n");
291
  fprintf (fo, "\n");
292
  fprintf (fo, "   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>\n");
293
  fprintf (fo, "\n");
294
  fprintf (fo, "   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.\n");
295
  fprintf (fo, "\n");
296
  fprintf (fo, "   This program is free software; you can redistribute it and/or modify it\n");
297
  fprintf (fo, "   under the terms of the GNU General Public License as published by the Free\n");
298
  fprintf (fo, "   Software Foundation; either version 3 of the License, or (at your option)\n");
299
  fprintf (fo, "   any later version.\n");
300
  fprintf (fo, "\n");
301
  fprintf (fo, "   This program is distributed in the hope that it will be useful, but WITHOUT\n");
302
  fprintf (fo, "   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n");
303
  fprintf (fo, "   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\n");
304
  fprintf (fo, "   more details.\n");
305
  fprintf (fo, "\n");
306
  fprintf (fo, "   You should have received a copy of the GNU General Public License along\n");
307
  fprintf (fo, "   with this program.  If not, see <http://www.gnu.org/licenses/>.  */\n");
308
  fprintf (fo, "\n");
309
  fprintf (fo, "/* This program is commented throughout in a fashion suitable for processing\n");
310
  fprintf (fo, "   with Doxygen. */\n");
311
  fprintf (fo, "\n");
312
 
313
  fprintf (fo, "/* This file was automatically generated by generate (see\n");
314
  fprintf (fo, "   cpu/or32/generate.c) */\n\n");
315
  fprintf (fo, "static void decode_execute (struct iqueue_entry *current)\n{\n");
316
  fprintf (fo, "  uint32_t insn = current->insn;\n");
317
  out_lines = 5;
318
  return 0;
319
}
320
 
321
/* Generates .c file footer */
322
static int generate_footer (FILE *fo)
323
{
324
  fprintf (fo, "}\n");
325
  return 0;
326
}
327
 
328
/* Decodes all instructions and generates code for that.  This function
329 82 jeremybenn
   is similar to insn_decode, except it decodes all instructions.
330
 
331
   JPB: Added code to generate an illegal instruction exception for invalid
332
   instructions. */
333 19 jeremybenn
static int generate_body (FILE *fo, unsigned long *a, unsigned long cur_mask, int level)
334
{
335
  unsigned long shift = *a;
336
  unsigned long mask;
337
  int i;
338
  int prev_inv = 0;
339
 
340
  if (!(*a & LEAF_FLAG)) {
341
    shift = *a++;
342
    mask = *a++;
343
    shift_fprintf (level, fo, "switch((insn >> %i) & 0x%x) {\n", shift,
344
                   mask);
345
    for (i = 0; i <= mask; i++, a++) {
346
      if (!*a) {
347
        shift_fprintf (level, fo, "case 0x%x:\n", i);
348
        prev_inv = 1;
349
      } else {
350
        if(prev_inv) {
351
          shift_fprintf (++level, fo, "/* Invalid instruction(s) */\n");
352
          shift_fprintf (level--, fo, "break;\n");
353
        }
354
        shift_fprintf (level, fo, "case 0x%x:\n", i);
355
        generate_body (fo, automata + *a, cur_mask | (mask << shift), ++level);
356
        shift_fprintf (level--, fo, "break;\n");
357
        prev_inv = 0;
358
      }
359
    }
360
    if (prev_inv) {
361
      shift_fprintf (++level, fo, "/* Invalid instruction(s) */\n");
362 82 jeremybenn
      shift_fprintf (level, fo,
363
                     "except_handle (EXCEPT_ILLEGAL, cpu_state.pc);\n");
364 19 jeremybenn
      shift_fprintf (level--, fo, "break;\n");
365
    }
366
    shift_fprintf (level, fo, "}\n");
367
  } else {
368
    i = *a & ~LEAF_FLAG;
369
 
370
    /* Final check - do we have direct match?
371
       (based on or32_opcodes this should be the only possibility,
372
       but in case of invalid/missing instruction we must perform a check)  */
373
 
374
    if (ti[i].insn_mask != cur_mask) {
375
      shift_fprintf (level, fo, "/* Not unique: real mask %08lx and current mask %08lx differ - do final check */\n", ti[i].insn_mask, cur_mask);
376
      shift_fprintf (level++, fo, "if((insn & 0x%x) == 0x%x) {\n",
377
                     ti[i].insn_mask, ti[i].insn);
378
    }
379
    shift_fprintf (level, fo, "/* Instruction: %s */\n", or32_opcodes[i].name);
380
 
381
    output_call (fo, i, level);
382
 
383
    if (ti[i].insn_mask != cur_mask) {
384
      shift_fprintf (--level, fo, "} else {\n");
385
      shift_fprintf (++level, fo, "/* Invalid insn */\n");
386
      output_call (fo, -1, level);
387
      shift_fprintf (--level, fo, "}\n");
388
    }
389
  }
390
  return 0;
391
}
392
 
393
/* Main function; it takes two parameters:
394
   input_file(possibly insnset.c) output_file(possibly execgen.c)*/
395
int main (int argc, char *argv[])
396
{
397
  FILE *fo;
398
 
399
  if (argc != 3) {
400
    fprintf (stderr, "USAGE: generate input_file(possibly insnset.c) output_file(possibly execgen.c)\n");
401
    exit (-1);
402
  }
403
 
404
  in_file = argv[1];
405
  out_file = argv[2];
406
  if (!(fo = fopen (argv[2], "wt+"))) {
407
    fprintf (stderr, "Cannot create '%s'.\n", argv[2]);
408
    exit (1);
409
  }
410
 
411
  build_automata ();
412
  if (generate_header (fo)) {
413
    fprintf (stderr, "generate_header\n");
414
    return 1;
415
  }
416
 
417
  if (generate_body (fo, automata, 0, 1)) {
418
    fprintf (stderr, "generate_body\n");
419
    return 1;
420
  }
421
 
422
  if (generate_footer (fo)) {
423
    fprintf (stderr, "generate_footer\n");
424
    return 1;
425
  }
426
 
427
  fclose (fo);
428
  destruct_automata ();
429
  return 0;
430
}
431
 

powered by: WebSVN 2.1.0

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