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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_36/] [or1ksim/] [cuc/] [load.c] - Blame information for rev 883

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

powered by: WebSVN 2.1.0

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