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 720

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 720 markom
  } else if (strncmp ("get_operand", str, 11) == 0) {
81
    replace = 10; str += 11;
82 712 markom
  }
83
  if (replace) {
84
    int width, oper;
85 720 markom
    if (replace < 10) {
86
      sscanf (str, "%i(%i", &width, &oper);
87
      while (*str && *str != '(') str++;
88
      while (*str && *str != ',') str++;
89
      str++;
90
    } else {
91
      sscanf (str, "(%i)", &oper);
92
      while (*str && *str != ')') str++;
93
    }
94 712 markom
    if (replace == 1) {
95
      if (op[oper] & OPTYPE_DIS) {
96
        fprintf (fo, "eval_mem%i (%c", width, 'a' + oper);
97
      } else {
98
        if (op[oper] & OPTYPE_REG) {
99 713 markom
          fprintf (fo, "(reg[%c]", 'a' + oper);
100 712 markom
        } else {
101
          fprintf (fo, "(%c", 'a' + oper);
102
        }
103
      }
104 720 markom
    } else if (replace == 2) {
105 712 markom
      op[oper] |= OPTYPE_DST;
106
      if (op[oper] & OPTYPE_DIS) {
107
        fprintf (fo, "set_mem%i(%c,", width, 'a' + oper);
108
      } else if (op[oper] & OPTYPE_REG) {
109 713 markom
        fprintf (fo, "reg[%c] = (", 'a' + oper);
110
        write_to_reg = 1;
111 712 markom
      } else {
112
        fprintf (stderr, "Invalid operand type.\n");
113
        exit (1);
114
      }
115
      while (*str != ',') str = replace_operands (fo, str) + 1;
116 720 markom
    } else {
117
      fprintf (fo, "%c", 'a' + oper);
118 712 markom
    }
119 720 markom
    if (replace < 10) {
120
      while (*str && *str != ')') str++;
121
      if (op[oper] & OPTYPE_DIS) fprintf (fo, ", &breakpoint)");
122
      else fprintf (fo, ")");
123
    }
124 712 markom
  } else {
125
    fputc (*str, fo);
126
  }
127
  return str;
128
}
129
 
130 713 markom
/* Generates a execute sequence for one instruction */
131 709 markom
int output_function (FILE *fo, const char *func_name, int level)
132
{
133
  FILE *fi;
134
  if ((fi = fopen (in_file, "rt")) == NULL) return 1;
135
  while (!feof (fi)) {
136
    char line[10000], *str = line;
137
    fgets (str, sizeof (line), fi);
138
    line[sizeof(line) - 1] = 0;
139
    if (strncmp (str, "INSTRUCTION (", 13) == 0) {
140
      char *s;
141
      str += 13;
142
      while (isspace (*str)) str++;
143
      s = str;
144
      while (*s && *s != ')') s++;
145
      *s = 0;
146
      while (isspace(*(s - 1))) s--;
147
      *s = 0;
148
      if (strcmp (str, func_name) == 0) {
149 712 markom
        olevel = 1;
150 709 markom
        str += strlen (str) + 1;
151
        while (isspace (*str)) str++;
152
        s = str;
153
        while (*s && *s != '\n' && *s != '\r') s++;
154
        *s = 0;
155
        while (isspace(*(s - 1))) s--;
156
        *s = 0;
157
        fprintf (fo, "%s", str);
158
        fprintf (fo, "   /* \"%s\" */\n", func_name);
159
        SHIFT;
160
        do {
161 712 markom
          fgets (line, sizeof (line), fi);
162
          line[sizeof(line) - 1] = 0;
163
          for (str = line; *str; str++) {
164
            str = replace_operands (fo, str);
165
          }
166
          SHIFT;
167 709 markom
        } while (olevel);
168
        return 0;
169
      }
170
    }
171
  }
172
  fprintf (fo, "{\n");
173
  level++;
174
  SHIFT; fprintf (fo, "%s ();\n", func_name);
175
  level--;
176
  SHIFT; fprintf (fo, "}");
177
  return 0;
178
}
179
 
180
/* Parses and puts operands into op[] structure.
181
   Replacement for eval_operands routine. */
182
 
183
static void
184
gen_eval_operands (FILE *fo, int insn_index, int level)
185
{
186
  struct insn_op_struct *opd = op_start[insn_index];
187
  int dis = 0;
188 717 markom
  int no = 0;
189 709 markom
  int firstd = 1;
190 712 markom
 
191 709 markom
  while (1)
192
    {
193
      int nbits = 0, first = 1;
194
      while (1)
195
        {
196
          SHIFT; fprintf (fo, "tmp %s= ((insn  >> %i) & 0x%08x) << %i;\n", first ? "" : "|", opd->type & OPTYPE_SHR, (1 << opd->data) - 1, nbits);
197
          nbits += opd->data;
198
          if (opd->type & OPTYPE_OP)
199
            break;
200
          opd++;
201
          first = 0;
202
        }
203
 
204
      /* Do we have to sign extend? */
205
      if (opd->type & OPTYPE_SIG)
206
        {
207
          int sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
208
          SHIFT; fprintf (fo, "if (tmp & (1 << %i)) tmp |= 0xFFFFFFFF << %i; /* Sign extend */\n", sbit, sbit);
209
        }
210
      if (opd->type & OPTYPE_DIS) {
211
        /* We have to read register later.  */
212
        SHIFT; fprintf (fo, "data %s= tmp;\n", firstd ? "" : "+");
213
        firstd = 0;
214
        dis = 1;
215
      } else
216
        {
217
          if (dis && (opd->type & OPTYPE_REG)) {
218 713 markom
            if (MAX_GPRS == (1 << nbits)) {
219 714 markom
              SHIFT; fprintf (fo, "%c = data + reg [tmp];\n", 'a' + no);
220
            } else {
221
              SHIFT; fprintf (fo, "%c = data + eval_reg32 (tmp);\n", 'a' + no);
222 713 markom
            }
223 709 markom
          } else {
224 714 markom
            SHIFT; fprintf (fo, "%c = tmp;\n", 'a' + no);
225 709 markom
          }
226 712 markom
          op[no] = opd->type | (dis ? OPTYPE_DIS : 0);
227 709 markom
          no++;
228
          firstd = 1;
229
          dis = 0;
230
        }
231 714 markom
      if(opd->type & OPTYPE_LAST) goto last;
232 709 markom
      opd++;
233
    }
234 714 markom
 
235
last:
236
  num_op = no;
237 709 markom
}
238
 
239 713 markom
/* Generates decode and execute for one instruction instance */
240 709 markom
int output_call (FILE *fo, int index, int level)
241
{
242 712 markom
  int i;
243
  printf ("%i:%s\n", index, insn_name (index));
244 709 markom
  fprintf (fo, "{\n");
245
  level++;
246
  if (index >= 0) {
247 712 markom
    SHIFT; fprintf (fo, "unsigned long data, tmp;\n");
248
    SHIFT; fprintf (fo, "unsigned long a, b, c; /* operands */\n");
249 709 markom
  }
250 713 markom
  write_to_reg = 0;
251 717 markom
  if (index >= 0)
252 709 markom
    gen_eval_operands (fo, index, level);
253 717 markom
  else
254
    num_op = 0;
255 709 markom
  SHIFT;
256
  if (index < 0) output_function (fo, "l_invalid", level);
257
  else output_function (fo, or32_opcodes[index].function_name, level);
258 713 markom
  fprintf (fo, "\n");
259 717 markom
 
260
  SHIFT; fprintf (fo, "if (do_stats) {\n");
261
  level++;
262
  SHIFT; fprintf (fo, "num_op = %i;\n", num_op);
263
  if (num_op) {SHIFT; fprintf (fo, "  op = &current->op[0];\n");}
264
  SHIFT; fprintf (fo, "current->insn_index = %i;   /* \"%s\" */\n", index, insn_name (index));
265
  for (i = 0; i < num_op; i++) {
266
    SHIFT; fprintf (fo, "op[%i] = %c;\n", i, 'a' + i);
267
    SHIFT; fprintf (fo, "op[%i + MAX_OPERANDS] = 0x%08x;\n", i, op[i]);
268
  }
269
  SHIFT; fprintf (fo, "analysis(current);\n");
270
  level--;
271
  SHIFT; fprintf (fo, "}\n");
272 713 markom
  if (write_to_reg) {
273
    SHIFT; fprintf (fo, "reg[0] = 0; /* Repair in case we changed it */\n", i);
274
  }
275 709 markom
  level--;
276
  SHIFT; fprintf (fo, "}");
277
  return 0;
278
}
279
 
280 713 markom
/* Generates .c file header */
281 717 markom
int generate_header (FILE *fo)
282 709 markom
{
283
  fprintf (fo, "/* This file was automatically generated by generate (see cpu/or32/generate.c) */\n\n");
284 712 markom
  fprintf (fo, "static inline void decode_execute (struct iqueue_entry *current)\n{\n");
285 709 markom
  fprintf (fo, "  unsigned long insn = current->insn;\n");
286
  return 0;
287
}
288
 
289 713 markom
/* Generates .c file footer */
290 709 markom
int generate_footer (FILE *fo)
291
{
292
  fprintf (fo, "}\n");
293
  return 0;
294
}
295
 
296
/* Decodes all instructions and generates code for that.  This function
297
   is similar to insn_decode, except it decodes all instructions. */
298
static int generate_body (FILE *fo, unsigned long *a, unsigned long cur_mask, int level)
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.