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 1308

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 1308 phoenix
#if DEBUG
50 709 markom
  char *p;
51
  va_list ap;
52
 
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 1308 phoenix
          SHIFT;
203
          fprintf (fo, "tmp %s= ((insn  >> %li) & 0x%08x) << %i;\n",
204
                   first ? "" : "|", opd->type & OPTYPE_SHR,
205
                   (1 << opd->data) - 1, nbits);
206 709 markom
          nbits += opd->data;
207
          if (opd->type & OPTYPE_OP)
208
            break;
209
          opd++;
210
          first = 0;
211
        }
212
 
213
      /* Do we have to sign extend? */
214
      if (opd->type & OPTYPE_SIG)
215
        {
216
          int sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
217
          SHIFT; fprintf (fo, "if (tmp & (1 << %i)) tmp |= 0xFFFFFFFF << %i; /* Sign extend */\n", sbit, sbit);
218
        }
219
      if (opd->type & OPTYPE_DIS) {
220
        /* We have to read register later.  */
221
        SHIFT; fprintf (fo, "data %s= tmp;\n", firstd ? "" : "+");
222
        firstd = 0;
223
        dis = 1;
224
      } else
225
        {
226
          if (dis && (opd->type & OPTYPE_REG)) {
227 713 markom
            if (MAX_GPRS == (1 << nbits)) {
228 714 markom
              SHIFT; fprintf (fo, "%c = data + reg [tmp];\n", 'a' + no);
229
            } else {
230
              SHIFT; fprintf (fo, "%c = data + eval_reg32 (tmp);\n", 'a' + no);
231 713 markom
            }
232 709 markom
          } else {
233 714 markom
            SHIFT; fprintf (fo, "%c = tmp;\n", 'a' + no);
234 709 markom
          }
235 712 markom
          op[no] = opd->type | (dis ? OPTYPE_DIS : 0);
236 709 markom
          no++;
237
          firstd = 1;
238
          dis = 0;
239
        }
240 714 markom
      if(opd->type & OPTYPE_LAST) goto last;
241 709 markom
      opd++;
242
    }
243 714 markom
 
244
last:
245
  num_op = no;
246 709 markom
}
247
 
248 713 markom
/* Generates decode and execute for one instruction instance */
249 709 markom
int output_call (FILE *fo, int index, int level)
250
{
251 712 markom
  int i;
252
  printf ("%i:%s\n", index, insn_name (index));
253 709 markom
  fprintf (fo, "{\n");
254
  level++;
255
  if (index >= 0) {
256 712 markom
    SHIFT; fprintf (fo, "unsigned long data, tmp;\n");
257 1290 phoenix
    SHIFT; fprintf (fo, "unsigned long a, b, c, d, e; /* operands */\n");
258 709 markom
  }
259 713 markom
  write_to_reg = 0;
260 717 markom
  if (index >= 0)
261 709 markom
    gen_eval_operands (fo, index, level);
262 717 markom
  else
263
    num_op = 0;
264 709 markom
  SHIFT;
265
  if (index < 0) output_function (fo, "l_invalid", level);
266
  else output_function (fo, or32_opcodes[index].function_name, level);
267 713 markom
  fprintf (fo, "\n");
268 717 markom
 
269
  SHIFT; fprintf (fo, "if (do_stats) {\n");
270
  level++;
271
  SHIFT; fprintf (fo, "num_op = %i;\n", num_op);
272
  if (num_op) {SHIFT; fprintf (fo, "  op = &current->op[0];\n");}
273
  SHIFT; fprintf (fo, "current->insn_index = %i;   /* \"%s\" */\n", index, insn_name (index));
274
  for (i = 0; i < num_op; i++) {
275
    SHIFT; fprintf (fo, "op[%i] = %c;\n", i, 'a' + i);
276
    SHIFT; fprintf (fo, "op[%i + MAX_OPERANDS] = 0x%08x;\n", i, op[i]);
277
  }
278
  SHIFT; fprintf (fo, "analysis(current);\n");
279
  level--;
280
  SHIFT; fprintf (fo, "}\n");
281 713 markom
  if (write_to_reg) {
282 1244 hpanther
    SHIFT; fprintf (fo, "reg[0] = 0; /* Repair in case we changed it */\n");
283 713 markom
  }
284 709 markom
  level--;
285
  SHIFT; fprintf (fo, "}");
286
  return 0;
287
}
288
 
289 713 markom
/* Generates .c file header */
290 717 markom
int generate_header (FILE *fo)
291 709 markom
{
292
  fprintf (fo, "/* This file was automatically generated by generate (see cpu/or32/generate.c) */\n\n");
293 712 markom
  fprintf (fo, "static inline void decode_execute (struct iqueue_entry *current)\n{\n");
294 709 markom
  fprintf (fo, "  unsigned long insn = current->insn;\n");
295
  return 0;
296
}
297
 
298 713 markom
/* Generates .c file footer */
299 709 markom
int generate_footer (FILE *fo)
300
{
301
  fprintf (fo, "}\n");
302
  return 0;
303
}
304
 
305
/* Decodes all instructions and generates code for that.  This function
306
   is similar to insn_decode, except it decodes all instructions. */
307
static int generate_body (FILE *fo, unsigned long *a, unsigned long cur_mask, int level)
308
{
309
  int i;
310
  if (!(*a & LEAF_FLAG)) {
311
    unsigned int shift = *a++;
312
    unsigned int mask  = *a++;
313
    int prev_invalid = 0;
314
    fprintf (fo, "\n");
315
    SHIFT; fprintf (fo, "/* (insn >> %i) & 0x%x */\n", shift, mask);
316
    SHIFT; fprintf (fo, "switch ((insn >> %i) & 0x%x) {\n", shift, mask);
317
    level++;
318
 
319
    /* Print each case recursively */
320
    for (i = 0; i <= mask; i++, a++) {
321
      /* Group invalid instruction decodes together */
322
      if (!*a) {
323
        if (prev_invalid) fprintf (fo, "\n");
324
        prev_invalid = 1;
325
        SHIFT; fprintf (fo, "case 0x%02x: ", i);
326
      } else {
327
        if (prev_invalid) {
328
          if (output_call (fo, -1, level)) return 1;
329
          fprintf (fo, "  break;\n");
330
        }
331
        SHIFT; fprintf (fo, "case 0x%02x: ", i);
332
        if (generate_body (fo, automata + *a, cur_mask | (mask << shift), level + 1)) return 1;
333
        prev_invalid = 0;
334
      }
335
    }
336
    if (prev_invalid) {
337
      if (output_call (fo, -1, level)) return 1;
338
      fprintf (fo, "  break;\n");
339
    }
340
    level--;
341
    if (level > 1)
342
      fprintf (fo, "}  break;\n");
343
    else
344
      fprintf (fo, "}\n");
345
  } else {
346
    i = *a & ~LEAF_FLAG;
347
    /* Final check - do we have direct match?
348
       (based on or32_opcodes this should be the only possibility,
349
       but in case of invalid/missing instruction we must perform a check)  */
350
 
351
    if (ti[i].insn_mask != cur_mask) {
352
      fprintf (fo, "\n");
353 1308 phoenix
      SHIFT;
354
      fprintf (fo, "/* Not unique: real mask %08lx and current mask %08lx differ - do final check */\n", ti[i].insn_mask, cur_mask);
355
      SHIFT; fprintf (fo, "if ((insn & 0x%08lx) == 0x%08lx) ", ti[i].insn_mask, ti[i].insn);
356 1244 hpanther
      if (output_call (fo, i, level)) return 1;         // Fail
357 709 markom
      fprintf (fo, " else ");
358 1244 hpanther
      if (output_call (fo, -1, level)) return 1;                // Fail
359 709 markom
    } else {
360 1244 hpanther
      if (output_call (fo, i, level - 1)) return 1;             // Fail
361 709 markom
    }
362
    fprintf (fo, "  break;\n");
363
  }
364
  return 0;
365
}
366
 
367
/* Main function; it takes two parameters:
368
   input_file(possibly insnset.c) output_file(possibly execgen.c)*/
369
int main (int argc, char *argv[])
370
{
371
  FILE *fo;
372
 
373
  if (argc != 3) {
374
    fprintf (stderr, "USAGE: generate input_file(possibly insnset.c) output_file(possibly execgen.c)\n");
375
    exit (-1);
376
  }
377
 
378
  in_file = argv[1];
379
  if (!(fo = fopen (argv[2], "wt+"))) {
380
    fprintf (stderr, "Cannot create '%s'.\n", argv[2]);
381
    exit (1);
382
  }
383
 
384
  build_automata ();
385
  if (generate_header (fo)) {fprintf (stderr, "generate_header\n"); return 1;}
386
  if (generate_body (fo, automata, 0, 1)) {fprintf (stderr, "generate_body\n"); return 1;}
387
  if (generate_footer (fo)) {fprintf (stderr, "generate_footer\n"); return 1;}
388
  fclose (fo);
389
  destruct_automata ();
390
  return 0;
391
}
392 713 markom
 

powered by: WebSVN 2.1.0

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