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 717

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 717 markom
  int no = 0;
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 709 markom
}
227
 
228 713 markom
/* Generates decode and execute for one instruction instance */
229 709 markom
int output_call (FILE *fo, int index, int level)
230
{
231 712 markom
  int i;
232
  printf ("%i:%s\n", index, insn_name (index));
233 709 markom
  fprintf (fo, "{\n");
234
  level++;
235
  if (index >= 0) {
236 712 markom
    SHIFT; fprintf (fo, "unsigned long data, tmp;\n");
237
    SHIFT; fprintf (fo, "unsigned long a, b, c; /* operands */\n");
238 709 markom
  }
239 713 markom
  write_to_reg = 0;
240 717 markom
  if (index >= 0)
241 709 markom
    gen_eval_operands (fo, index, level);
242 717 markom
  else
243
    num_op = 0;
244 709 markom
  SHIFT;
245
  if (index < 0) output_function (fo, "l_invalid", level);
246
  else output_function (fo, or32_opcodes[index].function_name, level);
247 713 markom
  fprintf (fo, "\n");
248 717 markom
 
249
  SHIFT; fprintf (fo, "if (do_stats) {\n");
250
  level++;
251
  SHIFT; fprintf (fo, "num_op = %i;\n", num_op);
252
  if (num_op) {SHIFT; fprintf (fo, "  op = &current->op[0];\n");}
253
  SHIFT; fprintf (fo, "current->insn_index = %i;   /* \"%s\" */\n", index, insn_name (index));
254
  for (i = 0; i < num_op; i++) {
255
    SHIFT; fprintf (fo, "op[%i] = %c;\n", i, 'a' + i);
256
    SHIFT; fprintf (fo, "op[%i + MAX_OPERANDS] = 0x%08x;\n", i, op[i]);
257
  }
258
  SHIFT; fprintf (fo, "analysis(current);\n");
259
  level--;
260
  SHIFT; fprintf (fo, "}\n");
261 713 markom
  if (write_to_reg) {
262
    SHIFT; fprintf (fo, "reg[0] = 0; /* Repair in case we changed it */\n", i);
263
  }
264 709 markom
  level--;
265
  SHIFT; fprintf (fo, "}");
266
  return 0;
267
}
268
 
269 713 markom
/* Generates .c file header */
270 717 markom
int generate_header (FILE *fo)
271 709 markom
{
272
  fprintf (fo, "/* This file was automatically generated by generate (see cpu/or32/generate.c) */\n\n");
273 712 markom
  fprintf (fo, "static inline void decode_execute (struct iqueue_entry *current)\n{\n");
274 709 markom
  fprintf (fo, "  unsigned long insn = current->insn;\n");
275
  return 0;
276
}
277
 
278 713 markom
/* Generates .c file footer */
279 709 markom
int generate_footer (FILE *fo)
280
{
281
  fprintf (fo, "}\n");
282
  return 0;
283
}
284
 
285
/* Decodes all instructions and generates code for that.  This function
286
   is similar to insn_decode, except it decodes all instructions. */
287
static int generate_body (FILE *fo, unsigned long *a, unsigned long cur_mask, int level)
288
{
289
  int i;
290
  if (!(*a & LEAF_FLAG)) {
291
    unsigned int shift = *a++;
292
    unsigned int mask  = *a++;
293
    int prev_invalid = 0;
294
    fprintf (fo, "\n");
295
    SHIFT; fprintf (fo, "/* (insn >> %i) & 0x%x */\n", shift, mask);
296
    SHIFT; fprintf (fo, "switch ((insn >> %i) & 0x%x) {\n", shift, mask);
297
    level++;
298
 
299
    /* Print each case recursively */
300
    for (i = 0; i <= mask; i++, a++) {
301
      /* Group invalid instruction decodes together */
302
      if (!*a) {
303
        if (prev_invalid) fprintf (fo, "\n");
304
        prev_invalid = 1;
305
        SHIFT; fprintf (fo, "case 0x%02x: ", i);
306
      } else {
307
        if (prev_invalid) {
308
          if (output_call (fo, -1, level)) return 1;
309
          fprintf (fo, "  break;\n");
310
        }
311
        SHIFT; fprintf (fo, "case 0x%02x: ", i);
312
        if (generate_body (fo, automata + *a, cur_mask | (mask << shift), level + 1)) return 1;
313
        prev_invalid = 0;
314
      }
315
    }
316
    if (prev_invalid) {
317
      if (output_call (fo, -1, level)) return 1;
318
      fprintf (fo, "  break;\n");
319
    }
320
    level--;
321
    if (level > 1)
322
      fprintf (fo, "}  break;\n");
323
    else
324
      fprintf (fo, "}\n");
325
  } else {
326
    i = *a & ~LEAF_FLAG;
327
    /* Final check - do we have direct match?
328
       (based on or32_opcodes this should be the only possibility,
329
       but in case of invalid/missing instruction we must perform a check)  */
330
 
331
    if (ti[i].insn_mask != cur_mask) {
332
      fprintf (fo, "\n");
333
      SHIFT; fprintf (fo, "/* Not unique: real mask %08x and current mask %08x differ - do final check */\n", ti[i].insn_mask, cur_mask);
334
      SHIFT; fprintf (fo, "if ((insn & 0x%08x) == 0x%08x) ", ti[i].insn_mask, ti[i].insn);
335
      output_call (fo, i, level);
336
      fprintf (fo, " else ");
337
      if (output_call (fo, -1, level)) return 1;
338
    } else {
339
      output_call (fo, i, level - 1);
340
    }
341
    fprintf (fo, "  break;\n");
342
  }
343
  return 0;
344
}
345
 
346
/* Main function; it takes two parameters:
347
   input_file(possibly insnset.c) output_file(possibly execgen.c)*/
348
int main (int argc, char *argv[])
349
{
350
  FILE *fo;
351
 
352
  if (argc != 3) {
353
    fprintf (stderr, "USAGE: generate input_file(possibly insnset.c) output_file(possibly execgen.c)\n");
354
    exit (-1);
355
  }
356
 
357
  in_file = argv[1];
358
  if (!(fo = fopen (argv[2], "wt+"))) {
359
    fprintf (stderr, "Cannot create '%s'.\n", argv[2]);
360
    exit (1);
361
  }
362
 
363
  build_automata ();
364
  if (generate_header (fo)) {fprintf (stderr, "generate_header\n"); return 1;}
365
  if (generate_body (fo, automata, 0, 1)) {fprintf (stderr, "generate_body\n"); return 1;}
366
  if (generate_footer (fo)) {fprintf (stderr, "generate_footer\n"); return 1;}
367
  fclose (fo);
368
  destruct_automata ();
369
  return 0;
370
}
371 713 markom
 

powered by: WebSVN 2.1.0

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