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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc2/] [or1ksim/] [cuc/] [load.c] - Blame information for rev 897

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

Line No. Rev Author Line
1 879 markom
/* load.c -- OpenRISC Custom Unit Compiler, instruction loading and converting
2
 *    Copyright (C) 2002 Marko Mlinar, markom@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 <stdio.h>
21
#include <stdlib.h>
22
#include <stdarg.h>
23
#include <assert.h>
24 897 markom
#include "sim-config.h"
25 879 markom
#include "cuc.h"
26
#include "opcode/or32.h"
27
#include "insn.h"
28
 
29
static const cuc_conv conv[] = {
30
{"l.add", II_ADD}, {"l.addi", II_ADD},
31
{"l.sub", II_SUB}, {"l.subi", II_SUB},
32
{"l.and", II_AND}, {"l.andi", II_AND},
33
{"l.xor", II_XOR}, {"l.xori", II_XOR},
34
{"l.or",  II_OR }, {"l.ori",  II_OR},
35
{"l.mul", II_MUL}, {"l.muli", II_MUL},
36
 
37
{"l.sra", II_SRA}, {"l.srai", II_SRA},
38
{"l.srl", II_SRL}, {"l.srli", II_SRL},
39
{"l.sll", II_SLL}, {"l.slli", II_SLL},
40
 
41
{"l.lbz",II_LB | II_MEM}, {"l.lbs", II_LB | II_MEM | II_SIGNED},
42
{"l.lhz",II_LH | II_MEM}, {"l.lhs", II_LH | II_MEM | II_SIGNED},
43
{"l.lwz",II_LW | II_MEM}, {"l.lws", II_LW | II_MEM | II_SIGNED},
44
{"l.sb", II_SB | II_MEM}, {"l.sh",  II_SH | II_MEM}, {"l.sw", II_SW | II_MEM},
45
{"l.sfeq",  II_SFEQ }, {"l.sfeqi", II_SFEQ},
46
{"l.sfne",  II_SFNE }, {"l.sfnei", II_SFNE},
47
{"l.sflts", II_SFLT | II_SIGNED}, {"l.sfltis", II_SFLT | II_SIGNED},
48
{"l.sfltu", II_SFLT}, {"l.sfltiu", II_SFLT},
49
{"l.sfgts", II_SFGT | II_SIGNED}, {"l.sfgtis", II_SFGT | II_SIGNED},
50
{"l.sfgtu", II_SFGT}, {"l.sfgtiu", II_SFGT},
51
{"l.sfges", II_SFGE | II_SIGNED}, {"l.sfgeis", II_SFGE | II_SIGNED},
52
{"l.sfgeu", II_SFGE}, {"l.sfgeiu", II_SFGE},
53
{"l.sfles", II_SFLE | II_SIGNED}, {"l.sfleis", II_SFLE | II_SIGNED},
54
{"l.sfleu", II_SFLE}, {"l.sfleiu", II_SFLE},
55
{"l.j",     II_BF   },
56
{"l.bf",    II_BF   },
57
{"l.nop",   II_NOP  }
58
};
59
 
60
/* Instructions from function */
61
cuc_insn insn[MAX_INSNS];
62
int num_insn;
63
int reloc[MAX_INSNS];
64
 
65
/* Prints out instructions */
66
void print_cuc_insns (char *s, int verbose)
67
{
68
  int i, j;
69
  printf ("****************** %s ******************\n", s);
70
  print_insns (insn, num_insn,verbose);
71
  printf ("\n\n");
72
}
73
 
74
void xchg_insn (int i, int j)
75
{
76
  cuc_insn t;
77
  t = insn[i];
78
  insn[i] = insn[j];
79
  insn[j] = t;
80
}
81
 
82
/* Remove delay slots */
83
void remove_dslots ()
84
{
85
  int i;
86
  int in_delay = 0;
87
  for (i = 0; i < num_insn; i++) {
88
    if (in_delay) insn[i].type |= IT_INDELAY;
89
    in_delay = 0;
90
    if (insn[i].type & IT_BRANCH) in_delay = 1;
91
    if (insn[i].type & IT_INDELAY) {
92
      /* delay slot should not be a branch target! */
93
      assert ((insn[i].type & IT_BBSTART) == 0);
94
      assert ((insn[i - 1].type & IT_INDELAY) == 0);
95
      insn[i].type &= ~IT_INDELAY; /* no more in delay slot */
96
      xchg_insn (i, i - 1);
97
    }
98
  }
99
  assert (in_delay == 0);
100
}
101
 
102
/* Convert local variables (uses stack frame -- r1) to internal values */
103
void detect_locals ()
104
{
105
  int stack[MAX_STACK];
106
  int i, can_remove_stack = 1;
107
  int real_stack_size = 0;
108
 
109
  for (i = 0; i < MAX_STACK; i++) stack[i] = -1;
110
 
111
  for (i = 0; i < num_insn; i++) {
112
    /* sw off (r1),rx */
113
    if (insn[i].index == II_SW
114
      && (insn[i].opt[0] & OPT_CONST)
115
      && insn[i].op[1] == 1 && (insn[i].opt[1] & OPT_REGISTER)) {
116
 
117 883 markom
      if (insn[i].op[0] < MAX_STACK/* && insn[i].op[1] >= 4*/) { /* Convert to normal move */
118 879 markom
        stack[insn[i].op[0]] = i;
119
        insn[i].type &= IT_INDELAY | IT_BBSTART;
120
        change_insn_type (&insn[i], II_ADD);
121
        insn[i].op[0] = -1; insn[i].opt[0] = OPT_REGISTER | OPT_DEST;
122
        insn[i].op[1] = insn[i].op[2]; insn[i].opt[1] = insn[i].opt[2];
123
        insn[i].op[2] = 0; insn[i].opt[2] = OPT_CONST;
124
      } else can_remove_stack = 0;
125
    /* lw rx,off (r1) */
126
    } else if (insn[i].index == II_LW
127
      && (insn[i].opt[1] & OPT_CONST)
128
      && insn[i].op[2] == 1 && (insn[i].opt[2] & OPT_REGISTER)) {
129
 
130 883 markom
      if (insn[i].op[1] < MAX_STACK && stack[insn[i].op[1]] >= 0) { /* Convert to normal move */
131 879 markom
        insn[i].type &= IT_INDELAY | IT_BBSTART;
132
        change_insn_type (&insn[i], II_ADD);
133
        insn[i].op[1] = stack[insn[i].op[1]]; insn[i].opt[1] = OPT_REF;
134
        insn[i].op[2] = 0; insn[i].opt[2] = OPT_CONST;
135
      } else can_remove_stack = 0;
136
    /* Check for defined stack size */
137
    } else if (insn[i].index == II_ADD && !real_stack_size
138
            && (insn[i].opt[0] & OPT_REGISTER) && insn[i].op[0] == 1
139
            && (insn[i].opt[1] & OPT_REGISTER) && insn[i].op[1] == 1
140
            && (insn[i].opt[2] & OPT_CONST)) {
141
      real_stack_size = -insn[i].op[2];
142
    }
143
  }
144 883 markom
  //assert (can_remove_stack); /* TODO */  
145 879 markom
}
146
 
147
/* Disassemble one instruction from insn index and generate parameters */
148
const char *build_insn (unsigned long data, cuc_insn *insn)
149
{
150
  const char *name;
151
  char *s;
152
  extern char *disassembled;
153
  int index = insn_decode (data);
154
  struct or32_opcode const *opcode;
155
  int i, argc = 0;
156
 
157
  insn->insn = data;
158
  insn->index = -1;
159
  insn->type = 0;
160
  name = insn_name (index);
161
  insn->index = index;
162
  disassemble_index (data, index);
163
  strcpy (insn->disasm, disassembled);
164
  insn->dep = NULL;
165
  for (i = 0; i < MAX_OPERANDS; i++) insn->opt[i] = OPT_NONE;
166
 
167
  if (index < 0) {
168
    fprintf (stderr, "Invalid opcode 0x%08x!\n", data);
169
    exit (1);
170
  }
171
  opcode = &or32_opcodes[index];
172
 
173
  for (s = opcode->args; *s != '\0'; ++s) {
174
    switch (*s) {
175
    case '\0': return name;
176
    case 'r':
177
      insn->opt[argc] = OPT_REGISTER | (argc ? 0 : OPT_DEST);
178
      insn->op[argc++] = or32_extract(*++s, opcode->encoding, data);
179
      break;
180
 
181
    default:
182
      if (strchr (opcode->encoding, *s)) {
183
        unsigned long imm = or32_extract (*s, opcode->encoding, data);
184
        imm = extend_imm(imm, *s);
185
        insn->opt[argc] = OPT_CONST;
186
        insn->op[argc++] = imm;
187
      }
188
    }
189
  }
190
  return name;
191
}
192
 
193
/* expands immediate memory instructions to two */
194
void expand_memory ()
195
{
196
  int i, j, num_mem = 0, d;
197
  for (i = 0; i < num_insn; i++) if (insn[i].type & IT_MEMORY) num_mem++;
198
 
199
  d = num_insn + num_mem;
200
  assert (d < MAX_INSNS);
201
 
202
  /* Split memory commands */
203
  for (i = num_insn - 1; i >= 0; i--) if (insn[i].type & IT_MEMORY) {
204
    insn[--d] = insn[i];
205
    insn[--d] = insn[i];
206
    reloc[i] = d;
207
    switch (insn[d].index) {
208
    case II_SW:
209
    case II_SH:
210
    case II_SB:
211
              insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST; /* add rd, ra, rb */
212
              insn[d].op[2] = insn[i].op[0]; insn[d].opt[2] = insn[i].opt[0];
213
              insn[d].opt[3] = OPT_NONE;
214
              insn[d].type &= IT_INDELAY | IT_BBSTART;
215
              insn[d].type |= IT_MEMADD;
216
              change_insn_type (&insn[d], II_ADD);
217
              insn[d + 1].op[1] = d; insn[d + 1].opt[1] = OPT_REF; /* sw (t($-1)),rx */
218
              insn[d + 1].op[0] = insn[i].op[2]; insn[d + 1].opt[0] = insn[i].opt[2];
219
              insn[d + 1].opt[2] = OPT_NONE;
220
              insn[d + 1].type &= ~IT_BBSTART;
221
              break;
222
    case II_LW:
223
    case II_LH:
224
    case II_LB:
225
              insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST; /* add rd, ra, rb */
226
              insn[d].type &= IT_INDELAY | IT_BBSTART;
227
              insn[d].type |= IT_MEMADD;
228
              change_insn_type (&insn[d], II_ADD);
229
              insn[d + 1].op[1] = d; insn[d + 1].opt[1] = OPT_REF; /* lw (t($-1)),rx */
230
              insn[d + 1].opt[2] = OPT_NONE;
231
              insn[d + 1].opt[3] = OPT_NONE;
232
              insn[d + 1].type &= ~IT_BBSTART;
233
              break;
234
    default:  fprintf (stderr, "%4i, %4i: %s\n", i, d, cuc_insn_name (&insn[d]));
235
              assert (0);
236
    }
237
  } else {
238
    insn[--d] = insn[i];
239
    reloc[i] = d;
240
  }
241
  num_insn += num_mem;
242
  for (i = 0; i < num_insn; i++) if (!(insn[i].type & IT_MEMORY))
243
    for (j = 0; j < MAX_OPERANDS; j++)
244
      if (insn[i].opt[j] & OPT_REF || insn[i].opt[j] & OPT_JUMP)
245
        insn[i].op[j] = reloc[insn[i].op[j]];
246
}
247
 
248
/* expands signed comparisons to three instructions */
249
void expand_signed ()
250
{
251
  int i, j, num_sig = 0, d;
252 897 markom
  for (i = 0; i < num_insn; i++)
253
    if (insn[i].type & IT_SIGNED && !(insn[i].type & IT_MEMORY)) num_sig++;
254 879 markom
 
255
  d = num_insn + num_sig * 2;
256
  assert (d < MAX_INSNS);
257
 
258
  /* Split signed instructions */
259 897 markom
  for (i = num_insn - 1; i >= 0; i--)
260 879 markom
    /* We will expand signed memory later */
261 897 markom
    if (insn[i].type & IT_SIGNED && !(insn[i].type & IT_MEMORY)) {
262
      insn[--d] = insn[i];
263
      insn[d].op[1] = d - 2; insn[d].opt[1] = OPT_REF;
264
      insn[d].op[2] = d - 1; insn[d].opt[2] = OPT_REF;
265 879 markom
 
266 897 markom
      insn[--d] = insn[i];
267
      change_insn_type (&insn[d], II_ADD);
268
      insn[d].type = 0;
269
      insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST;
270
      insn[d].op[1] = insn[d].op[2]; insn[d].opt[1] = insn[d].opt[2];
271
      insn[d].op[2] = 0x20000000; insn[d].opt[2] = OPT_CONST;
272
      insn[d].opt[3] = OPT_NONE;
273 879 markom
 
274 897 markom
      insn[--d] = insn[i];
275
      change_insn_type (&insn[d], II_ADD);
276
      insn[d].type = 0;
277
      insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST;
278
      insn[d].op[1] = insn[d].op[1]; insn[d].opt[1] = insn[d].opt[1];
279
      insn[d].op[2] = 0x20000000; insn[d].opt[2] = OPT_CONST;
280
      insn[d].opt[3] = OPT_NONE;
281 879 markom
 
282 897 markom
      reloc[i] = d;
283
    } else {
284
      insn[--d] = insn[i];
285
      reloc[i] = d;
286
    }
287 879 markom
  num_insn += num_sig * 2;
288
  for (i = 0; i < num_insn; i++) if (insn[i].type & IT_MEMORY || !(insn[i].type & IT_SIGNED)) {
289
    for (j = 0; j < MAX_OPERANDS; j++)
290
      if (insn[i].opt[j] & OPT_REF || insn[i].opt[j] & OPT_JUMP)
291
        insn[i].op[j] = reloc[insn[i].op[j]];
292
  } else insn[i].type &= ~IT_SIGNED;
293
}
294
 
295 897 markom
/* Loads function from file into global array insn.
296
   Function returns nonzero if function cannot be converted. */
297
int cuc_load (char *in_fn)
298 879 markom
{
299
  int i, j, in_delay;
300
  FILE *fi;
301
  int func_return = 0;
302
  num_insn = 0;
303
 
304
  log ("Loading filename %s\n", in_fn);
305
  if ((fi = fopen (in_fn, "rt")) == NULL) {
306
    fprintf (stderr, "Cannot open '%s'\n", in_fn);
307
    exit (1);
308
  }
309
  /* Read in the function and decode the instructions */
310
  for (i = 0;; i++) {
311
    unsigned long data;
312
    extern char *disassembled;
313
    const char *name;
314
 
315
    if (fscanf (fi, "%08x\n", &data) != 1) break;
316
 
317
    /* build params */
318
    name = build_insn (data, &insn[i]);
319
    if (func_return) func_return++;
320
    //printf ("%s\n", name);
321
 
322
    if (or32_opcodes[insn[i].index].flags & OR32_IF_DELAY) {
323
      int f;
324
      if (strcmp (name, "l.bnf") == 0) f = 1;
325
      else if (strcmp (name, "l.bf") == 0) f = 0;
326
      else if (strcmp (name, "l.j") == 0) {
327
        f = -1;
328
      } else if (strcmp (name, "l.jr") == 0 && func_return == 0) {
329
        func_return = 1;
330
        change_insn_type (&insn[i], II_NOP);
331
        continue;
332
      } else {
333 897 markom
        cucdebug (1, "Instruction #%i: \"%s\" not supported.\n", i, name);
334
        log ("Instruction #%i: \"%s\" not supported.\n", i, name);
335
        return 1;
336 879 markom
      }
337
      if (f < 0) { /* l.j */
338
        /* repair params */
339
        change_insn_type (&insn[i], II_BF);
340
        insn[i].op[0] = i + insn[i].op[0]; insn[i].opt[0] = OPT_JUMP;
341
        insn[i].op[1] = 1; insn[i].opt[1] = OPT_CONST;
342
        insn[i].type |= IT_BRANCH | IT_VOLATILE;
343
      } else {
344
        i--;
345
        if (f) {
346
          //printf ("%s\n", cuc_insn_name (&insn[i]));
347
          if (insn[i].index == II_SFEQ) change_insn_type (&insn[i], II_SFNE);
348
          else if (insn[i].index == II_SFNE) change_insn_type (&insn[i], II_SFEQ);
349
          else if (insn[i].index == II_SFLT) change_insn_type (&insn[i], II_SFGE);
350
          else if (insn[i].index == II_SFGT) change_insn_type (&insn[i], II_SFLE);
351
          else if (insn[i].index == II_SFLE) change_insn_type (&insn[i], II_SFGT);
352
          else if (insn[i].index == II_SFGE) change_insn_type (&insn[i], II_SFLT);
353
          else assert (0);
354
        }
355
        /* repair params */
356
        insn[i].op[2] = insn[i].op[1]; insn[i].opt[2] = insn[i].opt[1] & ~OPT_DEST;
357
        insn[i].op[1] = insn[i].op[0]; insn[i].opt[1] = insn[i].opt[0] & ~OPT_DEST;
358
        insn[i].op[0] = FLAG_REG; insn[i].opt[0] = OPT_DEST | OPT_REGISTER;
359
        insn[i].opt[3] = OPT_NONE;
360
        insn[i].type |= IT_COND;
361
        i++;
362
        change_insn_type (&insn[i], II_BF);
363
        insn[i].op[0] = i + insn[i].op[0]; insn[i].opt[0] = OPT_JUMP;
364
        insn[i].op[1] = FLAG_REG; insn[i].opt[1] = OPT_REGISTER;
365
        insn[i].type |= IT_BRANCH | IT_VOLATILE;
366
      }
367
    } else {
368
      insn[i].index = -1;
369
      for (j = 0; j < sizeof (conv) / sizeof (cuc_conv); j++)
370
        if (strcmp (conv[j].from, name) == 0) {
371
          const int x = conv[j].to;
372
          if (conv[j].to & II_SIGNED) insn[i].type |= IT_SIGNED;
373
          if (conv[j].to & II_MEM) insn[i].type |= IT_MEMORY | IT_VOLATILE;
374
          change_insn_type (&insn[i], conv[j].to & II_MASK);
375
          break;
376
        }
377 897 markom
      if (insn[i].index < 0 || insn[i].index == II_NOP && insn[i].op[0] != 0) {
378
        cucdebug (1, "Instruction #%i: \"%s\" not supported (2).\n", i, name);
379
        log ("Instruction #%i: \"%s\" not supported (2).\n", i, name);
380
        return 1;
381 879 markom
      }
382
    }
383
  }
384
  num_insn = i;
385
  fclose (fi);
386
  if (func_return != 2) {
387 897 markom
    cucdebug (1, "Unsupported function structure.\n");
388
    log ("Unsupported function structure.\n");
389
    return 1;
390 879 markom
  }
391
 
392
  log ("Number of instructions loaded = %i\n", num_insn);
393 883 markom
  if (cuc_debug >= 3) print_cuc_insns ("INITIAL", 1);
394 879 markom
 
395
  log ("Converting.\n");
396
  remove_dslots ();
397 883 markom
  if (cuc_debug >= 6) print_cuc_insns ("NO_DELAY_SLOTS", 0);
398 879 markom
 
399 897 markom
  if (config.cuc.calling_convention) {
400 879 markom
    detect_locals ();
401 883 markom
    if (cuc_debug >= 7) print_cuc_insns ("AFTER_LOCALS", 0);
402 879 markom
  }
403
  expand_memory ();
404 883 markom
  if (cuc_debug >= 3) print_cuc_insns ("AFTER_EXP_MEM", 0);
405 879 markom
 
406
  expand_signed ();
407 883 markom
  if (cuc_debug >= 3) print_cuc_insns ("AFTER_EXP_SIG", 0);
408 897 markom
  return 0;
409 879 markom
}

powered by: WebSVN 2.1.0

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