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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc1/] [or1ksim/] [cuc/] [load.c] - Blame information for rev 997

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

powered by: WebSVN 2.1.0

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