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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [cpu/] [or32/] [generate.c] - Blame information for rev 714

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

Line No. Rev Author Line
1 709 markom
/* generate.c -- generates file execgen.c from instruction set
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.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
#include <stdlib.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include <stdarg.h>
24
#include <ctype.h>
25
 
26
#include "config.h"
27
#include "opcode/or32.h"
28
#include "abstract.h"
29
#include "labels.h"
30
#include "parse.h"
31
#include "execute.h"
32
 
33
#define LEAF_FLAG         (0x80000000)
34
#define SHIFT {int i; for (i = 0; i < level; i++) fprintf (fo, "  ");}
35
 
36
extern unsigned long *automata;
37
extern struct temp_insn_struct {
38
  unsigned long insn;
39
  unsigned long insn_mask;
40
  int in_pass;
41
} *ti;
42
 
43
static char *in_file;
44 712 markom
unsigned long op[MAX_OPERANDS];
45
int num_op;
46 709 markom
 
47
inline void debug(int level, const char *format, ...)
48
{
49
  char *p;
50
  va_list ap;
51
 
52
#if DEBUG
53
  if ((p = malloc(1000)) == NULL)
54
    return;
55
  va_start(ap, format);
56
  (void) vsnprintf(p, 1000, format, ap);
57
  va_end(ap);
58
  printf("%s\n", p);
59
  fflush(stdout);
60
  free(p);
61
#endif
62
}
63
 
64 713 markom
/* Whether this instruction stores something in register */
65
static int write_to_reg = 0;
66
 
67 712 markom
static int olevel;
68
 
69 713 markom
/* Following functions recursivelly searches for substrings eval_operand and
70
   set_operand (see functions with the same name in execute.c) and replaces
71
   them with optimized code. */
72 712 markom
char *replace_operands (FILE *fo, char *str) {
73
  int replace = 0;
74
  if (*str == '}') {olevel--;}
75
  else if (*str == '{') {olevel++;}
76
  else if (strncmp ("eval_operand", str, 12) == 0) {
77
    replace = 1; str += 12;
78
  } else if (strncmp ("set_operand", str, 11) == 0) {
79
    replace = 2; str += 11;
80
  }
81
  if (replace) {
82
    int width, oper;
83
    sscanf (str, "%i(%i", &width, &oper);
84
    while (*str && *str != '(') str++;
85
    while (*str && *str != ',') str++;
86
    str++;
87
    if (replace == 1) {
88
      if (op[oper] & OPTYPE_DIS) {
89
        fprintf (fo, "eval_mem%i (%c", width, 'a' + oper);
90
      } else {
91
        if (op[oper] & OPTYPE_REG) {
92 713 markom
          fprintf (fo, "(reg[%c]", 'a' + oper);
93 712 markom
        } else {
94
          fprintf (fo, "(%c", 'a' + oper);
95
        }
96
      }
97
    } else {
98
      op[oper] |= OPTYPE_DST;
99
      if (op[oper] & OPTYPE_DIS) {
100
        fprintf (fo, "set_mem%i(%c,", width, 'a' + oper);
101
      } else if (op[oper] & OPTYPE_REG) {
102 713 markom
        fprintf (fo, "reg[%c] = (", 'a' + oper);
103
        write_to_reg = 1;
104 712 markom
      } else {
105
        fprintf (stderr, "Invalid operand type.\n");
106
        exit (1);
107
      }
108
      while (*str != ',') str = replace_operands (fo, str) + 1;
109
    }
110
    while (*str && *str != ')') str++;
111
    if (op[oper] & OPTYPE_DIS) fprintf (fo, ", &breakpoint)");
112
    else fprintf (fo, ")");
113
  } else {
114
    fputc (*str, fo);
115
  }
116
  return str;
117
}
118
 
119 713 markom
/* Generates a execute sequence for one instruction */
120 709 markom
int output_function (FILE *fo, const char *func_name, int level)
121
{
122
  FILE *fi;
123
  if ((fi = fopen (in_file, "rt")) == NULL) return 1;
124
  while (!feof (fi)) {
125
    char line[10000], *str = line;
126
    fgets (str, sizeof (line), fi);
127
    line[sizeof(line) - 1] = 0;
128
    if (strncmp (str, "INSTRUCTION (", 13) == 0) {
129
      char *s;
130
      str += 13;
131
      while (isspace (*str)) str++;
132
      s = str;
133
      while (*s && *s != ')') s++;
134
      *s = 0;
135
      while (isspace(*(s - 1))) s--;
136
      *s = 0;
137
      if (strcmp (str, func_name) == 0) {
138 712 markom
        olevel = 1;
139 709 markom
        str += strlen (str) + 1;
140
        while (isspace (*str)) str++;
141
        s = str;
142
        while (*s && *s != '\n' && *s != '\r') s++;
143
        *s = 0;
144
        while (isspace(*(s - 1))) s--;
145
        *s = 0;
146
        fprintf (fo, "%s", str);
147
        fprintf (fo, "   /* \"%s\" */\n", func_name);
148
        SHIFT;
149
        do {
150 712 markom
          fgets (line, sizeof (line), fi);
151
          line[sizeof(line) - 1] = 0;
152
          for (str = line; *str; str++) {
153
            str = replace_operands (fo, str);
154
          }
155
          SHIFT;
156 709 markom
        } while (olevel);
157
        return 0;
158
      }
159
    }
160
  }
161
  fprintf (fo, "{\n");
162
  level++;
163
  SHIFT; fprintf (fo, "%s ();\n", func_name);
164
  level--;
165
  SHIFT; fprintf (fo, "}");
166
  return 0;
167
}
168
 
169
/* Parses and puts operands into op[] structure.
170
   Replacement for eval_operands routine. */
171
 
172
static void
173
gen_eval_operands (FILE *fo, int insn_index, int level)
174
{
175
  struct insn_op_struct *opd = op_start[insn_index];
176
  int dis = 0;
177 714 markom
  int no = 0, num_op;
178 709 markom
  int firstd = 1;
179 712 markom
 
180 709 markom
  while (1)
181
    {
182
      int nbits = 0, first = 1;
183
      while (1)
184
        {
185
          SHIFT; fprintf (fo, "tmp %s= ((insn  >> %i) & 0x%08x) << %i;\n", first ? "" : "|", opd->type & OPTYPE_SHR, (1 << opd->data) - 1, nbits);
186
          nbits += opd->data;
187
          if (opd->type & OPTYPE_OP)
188
            break;
189
          opd++;
190
          first = 0;
191
        }
192
 
193
      /* Do we have to sign extend? */
194
      if (opd->type & OPTYPE_SIG)
195
        {
196
          int sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
197
          SHIFT; fprintf (fo, "if (tmp & (1 << %i)) tmp |= 0xFFFFFFFF << %i; /* Sign extend */\n", sbit, sbit);
198
        }
199
      if (opd->type & OPTYPE_DIS) {
200
        /* We have to read register later.  */
201
        SHIFT; fprintf (fo, "data %s= tmp;\n", firstd ? "" : "+");
202
        firstd = 0;
203
        dis = 1;
204
      } else
205
        {
206
          if (dis && (opd->type & OPTYPE_REG)) {
207 713 markom
            if (MAX_GPRS == (1 << nbits)) {
208 714 markom
              SHIFT; fprintf (fo, "%c = data + reg [tmp];\n", 'a' + no);
209
            } else {
210
              SHIFT; fprintf (fo, "%c = data + eval_reg32 (tmp);\n", 'a' + no);
211 713 markom
            }
212 709 markom
          } else {
213 714 markom
            SHIFT; fprintf (fo, "%c = tmp;\n", 'a' + no);
214 709 markom
          }
215 712 markom
          op[no] = opd->type | (dis ? OPTYPE_DIS : 0);
216 709 markom
          no++;
217
          firstd = 1;
218
          dis = 0;
219
        }
220 714 markom
      if(opd->type & OPTYPE_LAST) goto last;
221 709 markom
      opd++;
222
    }
223 714 markom
 
224
last:
225
  num_op = no;
226
  SHIFT; fprintf (fo, "if (do_stats) {\n");
227
  level++;
228 709 markom
  SHIFT; fprintf (fo, "num_op = %i;\n", no);
229 714 markom
  SHIFT; fprintf (fo, "insn_index = %i;   /* \"%s\" */\n", insn_index, insn_name (insn_index));
230
  for (no = 0; no < num_op; no++) {
231
    SHIFT; fprintf (fo, "op[%i] = %c;\n", no, 'a' + no);
232
    SHIFT; fprintf (fo, "op[%i + MAX_OPERANDS] = 0x%08x;\n", no, op[no]);
233
  }
234
  level--;
235
  SHIFT; fprintf (fo, "}\n");
236 709 markom
}
237
 
238 713 markom
/* Generates decode and execute for one instruction instance */
239 709 markom
int output_call (FILE *fo, int index, int level)
240
{
241 712 markom
  int i;
242
  printf ("%i:%s\n", index, insn_name (index));
243 709 markom
  fprintf (fo, "{\n");
244
  level++;
245
  if (index >= 0) {
246 712 markom
    SHIFT; fprintf (fo, "unsigned long data, tmp;\n");
247
    SHIFT; fprintf (fo, "unsigned long a, b, c; /* operands */\n");
248 709 markom
  }
249 713 markom
  write_to_reg = 0;
250 709 markom
  if (index >= 0) {
251
    gen_eval_operands (fo, index, level);
252 712 markom
  } else {
253 714 markom
    SHIFT; fprintf (fo, "if (do_stats) {\n");
254
    level++;
255
    SHIFT; fprintf (fo, "num_op = 0;\n");
256 713 markom
    SHIFT; fprintf (fo, "insn_index = -1;\n");
257 714 markom
    level--;
258
    SHIFT; fprintf (fo, "}\n");
259 709 markom
  }
260
  SHIFT;
261
  if (index < 0) output_function (fo, "l_invalid", level);
262
  else output_function (fo, or32_opcodes[index].function_name, level);
263 713 markom
  fprintf (fo, "\n");
264 712 markom
  for (i = 0; i < num_op; i++)
265
    if (op[i] & OPTYPE_DST) {
266
      SHIFT; fprintf (fo, "IFF (config.cpu.dependstats) op[%i + MAX_OPERANDS] |= OPTYPE_DST;\n", i);
267
    }
268 713 markom
  if (write_to_reg) {
269
    SHIFT; fprintf (fo, "reg[0] = 0; /* Repair in case we changed it */\n", i);
270
  }
271 709 markom
  level--;
272
  SHIFT; fprintf (fo, "}");
273
  return 0;
274
}
275
 
276 713 markom
/* Generates .c file header */
277 709 markom
static int generate_header (FILE *fo)
278
{
279
  fprintf (fo, "/* This file was automatically generated by generate (see cpu/or32/generate.c) */\n\n");
280 712 markom
  fprintf (fo, "static inline void decode_execute (struct iqueue_entry *current)\n{\n");
281 709 markom
  fprintf (fo, "  unsigned long insn = current->insn;\n");
282 713 markom
  fprintf (fo, "  int insn_index;\n");
283
  fprintf (fo, "  op = &current->op[0];\n");
284 709 markom
  return 0;
285
}
286
 
287 713 markom
/* Generates .c file footer */
288 709 markom
int generate_footer (FILE *fo)
289
{
290
  fprintf (fo, "  current->insn_index = insn_index;\n");
291
  fprintf (fo, "}\n");
292
  return 0;
293
}
294
 
295
/* Decodes all instructions and generates code for that.  This function
296
   is similar to insn_decode, except it decodes all instructions. */
297
static int generate_body (FILE *fo, unsigned long *a, unsigned long cur_mask, int level)
298
{
299
 
300
  int i;
301
  if (!(*a & LEAF_FLAG)) {
302
    unsigned int shift = *a++;
303
    unsigned int mask  = *a++;
304
    int prev_invalid = 0;
305
    fprintf (fo, "\n");
306
    SHIFT; fprintf (fo, "/* (insn >> %i) & 0x%x */\n", shift, mask);
307
    SHIFT; fprintf (fo, "switch ((insn >> %i) & 0x%x) {\n", shift, mask);
308
    level++;
309
 
310
    /* Print each case recursively */
311
    for (i = 0; i <= mask; i++, a++) {
312
      /* Group invalid instruction decodes together */
313
      if (!*a) {
314
        if (prev_invalid) fprintf (fo, "\n");
315
        prev_invalid = 1;
316
        SHIFT; fprintf (fo, "case 0x%02x: ", i);
317
      } else {
318
        if (prev_invalid) {
319
          if (output_call (fo, -1, level)) return 1;
320
          fprintf (fo, "  break;\n");
321
        }
322
        SHIFT; fprintf (fo, "case 0x%02x: ", i);
323
        if (generate_body (fo, automata + *a, cur_mask | (mask << shift), level + 1)) return 1;
324
        prev_invalid = 0;
325
      }
326
    }
327
    if (prev_invalid) {
328
      if (output_call (fo, -1, level)) return 1;
329
      fprintf (fo, "  break;\n");
330
    }
331
    level--;
332
    if (level > 1)
333
      fprintf (fo, "}  break;\n");
334
    else
335
      fprintf (fo, "}\n");
336
  } else {
337
    i = *a & ~LEAF_FLAG;
338
    /* Final check - do we have direct match?
339
       (based on or32_opcodes this should be the only possibility,
340
       but in case of invalid/missing instruction we must perform a check)  */
341
 
342
    if (ti[i].insn_mask != cur_mask) {
343
      fprintf (fo, "\n");
344
      SHIFT; fprintf (fo, "/* Not unique: real mask %08x and current mask %08x differ - do final check */\n", ti[i].insn_mask, cur_mask);
345
      SHIFT; fprintf (fo, "if ((insn & 0x%08x) == 0x%08x) ", ti[i].insn_mask, ti[i].insn);
346
      output_call (fo, i, level);
347
      fprintf (fo, " else ");
348
      if (output_call (fo, -1, level)) return 1;
349
    } else {
350
      output_call (fo, i, level - 1);
351
    }
352
    fprintf (fo, "  break;\n");
353
  }
354
  return 0;
355
}
356
 
357
/* Main function; it takes two parameters:
358
   input_file(possibly insnset.c) output_file(possibly execgen.c)*/
359
int main (int argc, char *argv[])
360
{
361
  FILE *fo;
362
 
363
  if (argc != 3) {
364
    fprintf (stderr, "USAGE: generate input_file(possibly insnset.c) output_file(possibly execgen.c)\n");
365
    exit (-1);
366
  }
367
 
368
  in_file = argv[1];
369
  if (!(fo = fopen (argv[2], "wt+"))) {
370
    fprintf (stderr, "Cannot create '%s'.\n", argv[2]);
371
    exit (1);
372
  }
373
 
374
  build_automata ();
375
  if (generate_header (fo)) {fprintf (stderr, "generate_header\n"); return 1;}
376
  if (generate_body (fo, automata, 0, 1)) {fprintf (stderr, "generate_body\n"); return 1;}
377
  if (generate_footer (fo)) {fprintf (stderr, "generate_footer\n"); return 1;}
378
  fclose (fo);
379
  destruct_automata ();
380
  return 0;
381
}
382 713 markom
 

powered by: WebSVN 2.1.0

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