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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0/] [or1ksim/] [cuc/] [load.c] - Blame information for rev 898

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

powered by: WebSVN 2.1.0

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