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

Subversion Repositories or1k

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

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 1244 hpanther
        if ((fi = fopen (in_file, "rt")) == NULL) {
135
                printf("could not open file\n");
136
                return 1;
137
        };
138 709 markom
  while (!feof (fi)) {
139
    char line[10000], *str = line;
140
    fgets (str, sizeof (line), fi);
141
    line[sizeof(line) - 1] = 0;
142
    if (strncmp (str, "INSTRUCTION (", 13) == 0) {
143
      char *s;
144
      str += 13;
145
      while (isspace (*str)) str++;
146
      s = str;
147
      while (*s && *s != ')') s++;
148
      *s = 0;
149
      while (isspace(*(s - 1))) s--;
150
      *s = 0;
151
      if (strcmp (str, func_name) == 0) {
152 712 markom
        olevel = 1;
153 709 markom
        str += strlen (str) + 1;
154
        while (isspace (*str)) str++;
155
        s = str;
156
        while (*s && *s != '\n' && *s != '\r') s++;
157
        *s = 0;
158
        while (isspace(*(s - 1))) s--;
159
        *s = 0;
160
        fprintf (fo, "%s", str);
161
        fprintf (fo, "   /* \"%s\" */\n", func_name);
162
        SHIFT;
163
        do {
164 712 markom
          fgets (line, sizeof (line), fi);
165
          line[sizeof(line) - 1] = 0;
166
          for (str = line; *str; str++) {
167
            str = replace_operands (fo, str);
168
          }
169
          SHIFT;
170 709 markom
        } while (olevel);
171 1244 hpanther
                fclose(fi);
172 709 markom
        return 0;
173
      }
174
    }
175
  }
176
  fprintf (fo, "{\n");
177
  level++;
178
  SHIFT; fprintf (fo, "%s ();\n", func_name);
179
  level--;
180
  SHIFT; fprintf (fo, "}");
181 1244 hpanther
 
182
  fclose(fi);
183 709 markom
  return 0;
184
}
185
 
186
/* Parses and puts operands into op[] structure.
187
   Replacement for eval_operands routine. */
188
 
189
static void
190
gen_eval_operands (FILE *fo, int insn_index, int level)
191
{
192
  struct insn_op_struct *opd = op_start[insn_index];
193
  int dis = 0;
194 717 markom
  int no = 0;
195 709 markom
  int firstd = 1;
196 712 markom
 
197 709 markom
  while (1)
198
    {
199
      int nbits = 0, first = 1;
200
      while (1)
201
        {
202
          SHIFT; fprintf (fo, "tmp %s= ((insn  >> %i) & 0x%08x) << %i;\n", first ? "" : "|", opd->type & OPTYPE_SHR, (1 << opd->data) - 1, nbits);
203
          nbits += opd->data;
204
          if (opd->type & OPTYPE_OP)
205
            break;
206
          opd++;
207
          first = 0;
208
        }
209
 
210
      /* Do we have to sign extend? */
211
      if (opd->type & OPTYPE_SIG)
212
        {
213
          int sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
214
          SHIFT; fprintf (fo, "if (tmp & (1 << %i)) tmp |= 0xFFFFFFFF << %i; /* Sign extend */\n", sbit, sbit);
215
        }
216
      if (opd->type & OPTYPE_DIS) {
217
        /* We have to read register later.  */
218
        SHIFT; fprintf (fo, "data %s= tmp;\n", firstd ? "" : "+");
219
        firstd = 0;
220
        dis = 1;
221
      } else
222
        {
223
          if (dis && (opd->type & OPTYPE_REG)) {
224 713 markom
            if (MAX_GPRS == (1 << nbits)) {
225 714 markom
              SHIFT; fprintf (fo, "%c = data + reg [tmp];\n", 'a' + no);
226
            } else {
227
              SHIFT; fprintf (fo, "%c = data + eval_reg32 (tmp);\n", 'a' + no);
228 713 markom
            }
229 709 markom
          } else {
230 714 markom
            SHIFT; fprintf (fo, "%c = tmp;\n", 'a' + no);
231 709 markom
          }
232 712 markom
          op[no] = opd->type | (dis ? OPTYPE_DIS : 0);
233 709 markom
          no++;
234
          firstd = 1;
235
          dis = 0;
236
        }
237 714 markom
      if(opd->type & OPTYPE_LAST) goto last;
238 709 markom
      opd++;
239
    }
240 714 markom
 
241
last:
242
  num_op = no;
243 709 markom
}
244
 
245 713 markom
/* Generates decode and execute for one instruction instance */
246 709 markom
int output_call (FILE *fo, int index, int level)
247
{
248 712 markom
  int i;
249
  printf ("%i:%s\n", index, insn_name (index));
250 709 markom
  fprintf (fo, "{\n");
251
  level++;
252
  if (index >= 0) {
253 712 markom
    SHIFT; fprintf (fo, "unsigned long data, tmp;\n");
254 1290 phoenix
    SHIFT; fprintf (fo, "unsigned long a, b, c, d, e; /* operands */\n");
255 709 markom
  }
256 713 markom
  write_to_reg = 0;
257 717 markom
  if (index >= 0)
258 709 markom
    gen_eval_operands (fo, index, level);
259 717 markom
  else
260
    num_op = 0;
261 709 markom
  SHIFT;
262
  if (index < 0) output_function (fo, "l_invalid", level);
263
  else output_function (fo, or32_opcodes[index].function_name, level);
264 713 markom
  fprintf (fo, "\n");
265 717 markom
 
266
  SHIFT; fprintf (fo, "if (do_stats) {\n");
267
  level++;
268
  SHIFT; fprintf (fo, "num_op = %i;\n", num_op);
269
  if (num_op) {SHIFT; fprintf (fo, "  op = &current->op[0];\n");}
270
  SHIFT; fprintf (fo, "current->insn_index = %i;   /* \"%s\" */\n", index, insn_name (index));
271
  for (i = 0; i < num_op; i++) {
272
    SHIFT; fprintf (fo, "op[%i] = %c;\n", i, 'a' + i);
273
    SHIFT; fprintf (fo, "op[%i + MAX_OPERANDS] = 0x%08x;\n", i, op[i]);
274
  }
275
  SHIFT; fprintf (fo, "analysis(current);\n");
276
  level--;
277
  SHIFT; fprintf (fo, "}\n");
278 713 markom
  if (write_to_reg) {
279 1244 hpanther
    SHIFT; fprintf (fo, "reg[0] = 0; /* Repair in case we changed it */\n");
280 713 markom
  }
281 709 markom
  level--;
282
  SHIFT; fprintf (fo, "}");
283
  return 0;
284
}
285
 
286 713 markom
/* Generates .c file header */
287 717 markom
int generate_header (FILE *fo)
288 709 markom
{
289
  fprintf (fo, "/* This file was automatically generated by generate (see cpu/or32/generate.c) */\n\n");
290 712 markom
  fprintf (fo, "static inline void decode_execute (struct iqueue_entry *current)\n{\n");
291 709 markom
  fprintf (fo, "  unsigned long insn = current->insn;\n");
292
  return 0;
293
}
294
 
295 713 markom
/* Generates .c file footer */
296 709 markom
int generate_footer (FILE *fo)
297
{
298
  fprintf (fo, "}\n");
299
  return 0;
300
}
301
 
302
/* Decodes all instructions and generates code for that.  This function
303
   is similar to insn_decode, except it decodes all instructions. */
304
static int generate_body (FILE *fo, unsigned long *a, unsigned long cur_mask, int level)
305
{
306
  int i;
307
  if (!(*a & LEAF_FLAG)) {
308
    unsigned int shift = *a++;
309
    unsigned int mask  = *a++;
310
    int prev_invalid = 0;
311
    fprintf (fo, "\n");
312
    SHIFT; fprintf (fo, "/* (insn >> %i) & 0x%x */\n", shift, mask);
313
    SHIFT; fprintf (fo, "switch ((insn >> %i) & 0x%x) {\n", shift, mask);
314
    level++;
315
 
316
    /* Print each case recursively */
317
    for (i = 0; i <= mask; i++, a++) {
318
      /* Group invalid instruction decodes together */
319
      if (!*a) {
320
        if (prev_invalid) fprintf (fo, "\n");
321
        prev_invalid = 1;
322
        SHIFT; fprintf (fo, "case 0x%02x: ", i);
323
      } else {
324
        if (prev_invalid) {
325
          if (output_call (fo, -1, level)) return 1;
326
          fprintf (fo, "  break;\n");
327
        }
328
        SHIFT; fprintf (fo, "case 0x%02x: ", i);
329
        if (generate_body (fo, automata + *a, cur_mask | (mask << shift), level + 1)) return 1;
330
        prev_invalid = 0;
331
      }
332
    }
333
    if (prev_invalid) {
334
      if (output_call (fo, -1, level)) return 1;
335
      fprintf (fo, "  break;\n");
336
    }
337
    level--;
338
    if (level > 1)
339
      fprintf (fo, "}  break;\n");
340
    else
341
      fprintf (fo, "}\n");
342
  } else {
343
    i = *a & ~LEAF_FLAG;
344
    /* Final check - do we have direct match?
345
       (based on or32_opcodes this should be the only possibility,
346
       but in case of invalid/missing instruction we must perform a check)  */
347
 
348
    if (ti[i].insn_mask != cur_mask) {
349
      fprintf (fo, "\n");
350
      SHIFT; fprintf (fo, "/* Not unique: real mask %08x and current mask %08x differ - do final check */\n", ti[i].insn_mask, cur_mask);
351
      SHIFT; fprintf (fo, "if ((insn & 0x%08x) == 0x%08x) ", ti[i].insn_mask, ti[i].insn);
352 1244 hpanther
      if (output_call (fo, i, level)) return 1;         // Fail
353 709 markom
      fprintf (fo, " else ");
354 1244 hpanther
      if (output_call (fo, -1, level)) return 1;                // Fail
355 709 markom
    } else {
356 1244 hpanther
      if (output_call (fo, i, level - 1)) return 1;             // Fail
357 709 markom
    }
358
    fprintf (fo, "  break;\n");
359
  }
360
  return 0;
361
}
362
 
363
/* Main function; it takes two parameters:
364
   input_file(possibly insnset.c) output_file(possibly execgen.c)*/
365
int main (int argc, char *argv[])
366
{
367
  FILE *fo;
368
 
369
  if (argc != 3) {
370
    fprintf (stderr, "USAGE: generate input_file(possibly insnset.c) output_file(possibly execgen.c)\n");
371
    exit (-1);
372
  }
373
 
374
  in_file = argv[1];
375
  if (!(fo = fopen (argv[2], "wt+"))) {
376
    fprintf (stderr, "Cannot create '%s'.\n", argv[2]);
377
    exit (1);
378
  }
379
 
380
  build_automata ();
381
  if (generate_header (fo)) {fprintf (stderr, "generate_header\n"); return 1;}
382
  if (generate_body (fo, automata, 0, 1)) {fprintf (stderr, "generate_body\n"); return 1;}
383
  if (generate_footer (fo)) {fprintf (stderr, "generate_footer\n"); return 1;}
384
  fclose (fo);
385
  destruct_automata ();
386
  return 0;
387
}
388 713 markom
 

powered by: WebSVN 2.1.0

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