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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1ksim/] [cuc/] [load.c] - Blame information for rev 905

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

powered by: WebSVN 2.1.0

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