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 |
1308 |
phoenix |
#include <string.h>
|
25 |
|
|
|
26 |
1350 |
nogj |
#include "config.h"
|
27 |
|
|
|
28 |
|
|
#ifdef HAVE_INTTYPES_H
|
29 |
|
|
#include <inttypes.h>
|
30 |
|
|
#endif
|
31 |
|
|
|
32 |
|
|
#include "port.h"
|
33 |
|
|
#include "arch.h"
|
34 |
1308 |
phoenix |
#include "abstract.h"
|
35 |
897 |
markom |
#include "sim-config.h"
|
36 |
879 |
markom |
#include "cuc.h"
|
37 |
|
|
#include "opcode/or32.h"
|
38 |
|
|
#include "insn.h"
|
39 |
|
|
|
40 |
|
|
static const cuc_conv conv[] = {
|
41 |
|
|
{"l.add", II_ADD}, {"l.addi", II_ADD},
|
42 |
898 |
markom |
{"l.movhi", II_OR},
|
43 |
879 |
markom |
{"l.sub", II_SUB}, {"l.subi", II_SUB},
|
44 |
|
|
{"l.and", II_AND}, {"l.andi", II_AND},
|
45 |
|
|
{"l.xor", II_XOR}, {"l.xori", II_XOR},
|
46 |
|
|
{"l.or", II_OR }, {"l.ori", II_OR},
|
47 |
|
|
{"l.mul", II_MUL}, {"l.muli", II_MUL},
|
48 |
|
|
|
49 |
|
|
{"l.sra", II_SRA}, {"l.srai", II_SRA},
|
50 |
|
|
{"l.srl", II_SRL}, {"l.srli", II_SRL},
|
51 |
|
|
{"l.sll", II_SLL}, {"l.slli", II_SLL},
|
52 |
|
|
|
53 |
|
|
{"l.lbz",II_LB | II_MEM}, {"l.lbs", II_LB | II_MEM | II_SIGNED},
|
54 |
|
|
{"l.lhz",II_LH | II_MEM}, {"l.lhs", II_LH | II_MEM | II_SIGNED},
|
55 |
|
|
{"l.lwz",II_LW | II_MEM}, {"l.lws", II_LW | II_MEM | II_SIGNED},
|
56 |
|
|
{"l.sb", II_SB | II_MEM}, {"l.sh", II_SH | II_MEM}, {"l.sw", II_SW | II_MEM},
|
57 |
|
|
{"l.sfeq", II_SFEQ }, {"l.sfeqi", II_SFEQ},
|
58 |
|
|
{"l.sfne", II_SFNE }, {"l.sfnei", II_SFNE},
|
59 |
|
|
{"l.sflts", II_SFLT | II_SIGNED}, {"l.sfltis", II_SFLT | II_SIGNED},
|
60 |
|
|
{"l.sfltu", II_SFLT}, {"l.sfltiu", II_SFLT},
|
61 |
|
|
{"l.sfgts", II_SFGT | II_SIGNED}, {"l.sfgtis", II_SFGT | II_SIGNED},
|
62 |
|
|
{"l.sfgtu", II_SFGT}, {"l.sfgtiu", II_SFGT},
|
63 |
|
|
{"l.sfges", II_SFGE | II_SIGNED}, {"l.sfgeis", II_SFGE | II_SIGNED},
|
64 |
|
|
{"l.sfgeu", II_SFGE}, {"l.sfgeiu", II_SFGE},
|
65 |
|
|
{"l.sfles", II_SFLE | II_SIGNED}, {"l.sfleis", II_SFLE | II_SIGNED},
|
66 |
|
|
{"l.sfleu", II_SFLE}, {"l.sfleiu", II_SFLE},
|
67 |
|
|
{"l.j", II_BF },
|
68 |
|
|
{"l.bf", II_BF },
|
69 |
906 |
markom |
{"l.jal", II_CALL },
|
70 |
879 |
markom |
{"l.nop", II_NOP }
|
71 |
|
|
};
|
72 |
|
|
|
73 |
|
|
/* Instructions from function */
|
74 |
|
|
cuc_insn insn[MAX_INSNS];
|
75 |
|
|
int num_insn;
|
76 |
|
|
int reloc[MAX_INSNS];
|
77 |
|
|
|
78 |
|
|
/* Prints out instructions */
|
79 |
|
|
void print_cuc_insns (char *s, int verbose)
|
80 |
|
|
{
|
81 |
997 |
markom |
PRINTF ("****************** %s ******************\n", s);
|
82 |
1062 |
markom |
print_insns (0, insn, num_insn,verbose);
|
83 |
997 |
markom |
PRINTF ("\n\n");
|
84 |
879 |
markom |
}
|
85 |
|
|
|
86 |
|
|
void xchg_insn (int i, int j)
|
87 |
|
|
{
|
88 |
|
|
cuc_insn t;
|
89 |
|
|
t = insn[i];
|
90 |
|
|
insn[i] = insn[j];
|
91 |
|
|
insn[j] = t;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
905 |
markom |
/* Negates conditional instruction */
|
95 |
|
|
void negate_conditional (cuc_insn *ii)
|
96 |
|
|
{
|
97 |
|
|
assert (ii->type & IT_COND);
|
98 |
|
|
|
99 |
|
|
if (ii->index == II_SFEQ) change_insn_type (ii, II_SFNE);
|
100 |
|
|
else if (ii->index == II_SFNE) change_insn_type (ii, II_SFEQ);
|
101 |
|
|
else if (ii->index == II_SFLT) change_insn_type (ii, II_SFGE);
|
102 |
|
|
else if (ii->index == II_SFGT) change_insn_type (ii, II_SFLE);
|
103 |
|
|
else if (ii->index == II_SFLE) change_insn_type (ii, II_SFGT);
|
104 |
|
|
else if (ii->index == II_SFGE) change_insn_type (ii, II_SFLT);
|
105 |
|
|
else assert (0);
|
106 |
|
|
}
|
107 |
|
|
|
108 |
879 |
markom |
/* Remove delay slots */
|
109 |
|
|
void remove_dslots ()
|
110 |
|
|
{
|
111 |
|
|
int i;
|
112 |
|
|
int in_delay = 0;
|
113 |
|
|
for (i = 0; i < num_insn; i++) {
|
114 |
|
|
if (in_delay) insn[i].type |= IT_INDELAY;
|
115 |
|
|
in_delay = 0;
|
116 |
|
|
if (insn[i].type & IT_BRANCH) in_delay = 1;
|
117 |
|
|
if (insn[i].type & IT_INDELAY) {
|
118 |
1043 |
markom |
cuc_insn *ii;
|
119 |
|
|
cuc_insn *bi;
|
120 |
|
|
assert (i >= 2);
|
121 |
|
|
ii = &insn[i - 2];
|
122 |
|
|
bi = &insn[i - 1];
|
123 |
879 |
markom |
/* delay slot should not be a branch target! */
|
124 |
|
|
assert ((insn[i].type & IT_BBSTART) == 0);
|
125 |
1043 |
markom |
assert ((bi->type & IT_INDELAY) == 0);
|
126 |
879 |
markom |
insn[i].type &= ~IT_INDELAY; /* no more in delay slot */
|
127 |
1043 |
markom |
|
128 |
|
|
/* Get the value we need before the actual jump */
|
129 |
|
|
if (bi->opt[1] & OPT_REGISTER && bi->op[1] >= 0) {
|
130 |
|
|
int r = bi->op[1];
|
131 |
|
|
assert (ii->index == II_NOP);
|
132 |
|
|
change_insn_type (ii, II_ADD);
|
133 |
|
|
ii->type = IT_COND;
|
134 |
|
|
ii->dep = NULL;
|
135 |
|
|
ii->op[0] = r; ii->opt[0] = OPT_REGISTER | OPT_DEST;
|
136 |
|
|
ii->op[1] = r; ii->opt[1] = OPT_REGISTER;
|
137 |
|
|
ii->op[2] = 0; ii->opt[2] = OPT_CONST;
|
138 |
|
|
ii->opt[3] = OPT_NONE;
|
139 |
|
|
bi->op[1] = i - 2; bi->opt[1] = OPT_REF;
|
140 |
|
|
}
|
141 |
|
|
xchg_insn (i, i - 1);
|
142 |
879 |
markom |
}
|
143 |
|
|
}
|
144 |
|
|
assert (in_delay == 0);
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
/* Convert local variables (uses stack frame -- r1) to internal values */
|
148 |
|
|
void detect_locals ()
|
149 |
|
|
{
|
150 |
1308 |
phoenix |
int stack[CUC_MAX_STACK];
|
151 |
879 |
markom |
int i, can_remove_stack = 1;
|
152 |
|
|
int real_stack_size = 0;
|
153 |
|
|
|
154 |
1308 |
phoenix |
for (i = 0; i < CUC_MAX_STACK; i++) stack[i] = -1;
|
155 |
879 |
markom |
|
156 |
|
|
for (i = 0; i < num_insn; i++) {
|
157 |
|
|
/* sw off (r1),rx */
|
158 |
|
|
if (insn[i].index == II_SW
|
159 |
|
|
&& (insn[i].opt[0] & OPT_CONST)
|
160 |
|
|
&& insn[i].op[1] == 1 && (insn[i].opt[1] & OPT_REGISTER)) {
|
161 |
|
|
|
162 |
1308 |
phoenix |
if (insn[i].op[0] < CUC_MAX_STACK/* && insn[i].op[1] >= 4*/) { /* Convert to normal move */
|
163 |
879 |
markom |
stack[insn[i].op[0]] = i;
|
164 |
|
|
insn[i].type &= IT_INDELAY | IT_BBSTART;
|
165 |
|
|
change_insn_type (&insn[i], II_ADD);
|
166 |
|
|
insn[i].op[0] = -1; insn[i].opt[0] = OPT_REGISTER | OPT_DEST;
|
167 |
|
|
insn[i].op[1] = insn[i].op[2]; insn[i].opt[1] = insn[i].opt[2];
|
168 |
|
|
insn[i].op[2] = 0; insn[i].opt[2] = OPT_CONST;
|
169 |
|
|
} else can_remove_stack = 0;
|
170 |
|
|
/* lw rx,off (r1) */
|
171 |
|
|
} else if (insn[i].index == II_LW
|
172 |
|
|
&& (insn[i].opt[1] & OPT_CONST)
|
173 |
|
|
&& insn[i].op[2] == 1 && (insn[i].opt[2] & OPT_REGISTER)) {
|
174 |
|
|
|
175 |
1308 |
phoenix |
if (insn[i].op[1] < CUC_MAX_STACK && stack[insn[i].op[1]] >= 0) { /* Convert to normal move */
|
176 |
879 |
markom |
insn[i].type &= IT_INDELAY | IT_BBSTART;
|
177 |
|
|
change_insn_type (&insn[i], II_ADD);
|
178 |
|
|
insn[i].op[1] = stack[insn[i].op[1]]; insn[i].opt[1] = OPT_REF;
|
179 |
|
|
insn[i].op[2] = 0; insn[i].opt[2] = OPT_CONST;
|
180 |
|
|
} else can_remove_stack = 0;
|
181 |
|
|
/* Check for defined stack size */
|
182 |
|
|
} else if (insn[i].index == II_ADD && !real_stack_size
|
183 |
|
|
&& (insn[i].opt[0] & OPT_REGISTER) && insn[i].op[0] == 1
|
184 |
|
|
&& (insn[i].opt[1] & OPT_REGISTER) && insn[i].op[1] == 1
|
185 |
|
|
&& (insn[i].opt[2] & OPT_CONST)) {
|
186 |
|
|
real_stack_size = -insn[i].op[2];
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
883 |
markom |
//assert (can_remove_stack); /* TODO */
|
190 |
879 |
markom |
}
|
191 |
|
|
|
192 |
|
|
/* Disassemble one instruction from insn index and generate parameters */
|
193 |
|
|
const char *build_insn (unsigned long data, cuc_insn *insn)
|
194 |
|
|
{
|
195 |
|
|
const char *name;
|
196 |
|
|
char *s;
|
197 |
|
|
extern char *disassembled;
|
198 |
|
|
int index = insn_decode (data);
|
199 |
|
|
struct or32_opcode const *opcode;
|
200 |
|
|
int i, argc = 0;
|
201 |
|
|
|
202 |
|
|
insn->insn = data;
|
203 |
|
|
insn->index = -1;
|
204 |
|
|
insn->type = 0;
|
205 |
|
|
name = insn_name (index);
|
206 |
|
|
insn->index = index;
|
207 |
|
|
disassemble_index (data, index);
|
208 |
|
|
strcpy (insn->disasm, disassembled);
|
209 |
|
|
insn->dep = NULL;
|
210 |
|
|
for (i = 0; i < MAX_OPERANDS; i++) insn->opt[i] = OPT_NONE;
|
211 |
|
|
|
212 |
|
|
if (index < 0) {
|
213 |
1308 |
phoenix |
fprintf (stderr, "Invalid opcode 0x%08lx!\n", data);
|
214 |
879 |
markom |
exit (1);
|
215 |
|
|
}
|
216 |
|
|
opcode = &or32_opcodes[index];
|
217 |
|
|
|
218 |
|
|
for (s = opcode->args; *s != '\0'; ++s) {
|
219 |
|
|
switch (*s) {
|
220 |
|
|
case '\0': return name;
|
221 |
|
|
case 'r':
|
222 |
|
|
insn->opt[argc] = OPT_REGISTER | (argc ? 0 : OPT_DEST);
|
223 |
|
|
insn->op[argc++] = or32_extract(*++s, opcode->encoding, data);
|
224 |
|
|
break;
|
225 |
|
|
|
226 |
|
|
default:
|
227 |
|
|
if (strchr (opcode->encoding, *s)) {
|
228 |
|
|
unsigned long imm = or32_extract (*s, opcode->encoding, data);
|
229 |
|
|
imm = extend_imm(imm, *s);
|
230 |
|
|
insn->opt[argc] = OPT_CONST;
|
231 |
|
|
insn->op[argc++] = imm;
|
232 |
|
|
}
|
233 |
|
|
}
|
234 |
|
|
}
|
235 |
|
|
return name;
|
236 |
|
|
}
|
237 |
|
|
|
238 |
1043 |
markom |
/* inserts nop before branch */
|
239 |
|
|
void expand_branch ()
|
240 |
|
|
{
|
241 |
|
|
int i, j, num_bra = 0, d;
|
242 |
|
|
for (i = 0; i < num_insn; i++) if (insn[i].type & IT_BRANCH) num_bra++;
|
243 |
|
|
|
244 |
1062 |
markom |
d = num_insn + 2 * num_bra;
|
245 |
1043 |
markom |
assert (d < MAX_INSNS);
|
246 |
|
|
|
247 |
1061 |
markom |
/* Add nop before branch */
|
248 |
1043 |
markom |
for (i = num_insn - 1; i >= 0; i--) if (insn[i].type & IT_BRANCH) {
|
249 |
1062 |
markom |
insn[--d] = insn[i]; // for delay slot (later)
|
250 |
|
|
if (insn[d].opt[1] & OPT_REGISTER) {
|
251 |
|
|
assert (insn[d].op[1] == FLAG_REG);
|
252 |
|
|
insn[d].op[1] = i; insn[d].opt[1] = OPT_REF;
|
253 |
|
|
}
|
254 |
|
|
insn[--d] = insn[i]; // for branch
|
255 |
|
|
change_insn_type (&insn[d], II_NOP);
|
256 |
|
|
insn[--d] = insn[i]; // save flag & negation of conditional, if required
|
257 |
|
|
change_insn_type (&insn[d], II_CMOV);
|
258 |
|
|
insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST;
|
259 |
|
|
insn[d].op[1] = insn[d].type & IT_FLAG1 ? 0 : 1; insn[d].opt[1] = OPT_CONST;
|
260 |
|
|
insn[d].op[2] = insn[d].type & IT_FLAG1 ? 1 : 0; insn[d].opt[2] = OPT_CONST;
|
261 |
|
|
insn[d].op[3] = FLAG_REG; insn[d].opt[3] = OPT_REGISTER;
|
262 |
|
|
insn[d].type = IT_COND;
|
263 |
|
|
if (insn[d].type)
|
264 |
1043 |
markom |
reloc[i] = d;
|
265 |
|
|
} else {
|
266 |
|
|
insn[--d] = insn[i];
|
267 |
|
|
reloc[i] = d;
|
268 |
|
|
}
|
269 |
1062 |
markom |
num_insn += 2 * num_bra;
|
270 |
1043 |
markom |
for (i = 0; i < num_insn; i++)
|
271 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
272 |
|
|
if (insn[i].opt[j] & OPT_REF || insn[i].opt[j] & OPT_JUMP)
|
273 |
|
|
insn[i].op[j] = reloc[insn[i].op[j]];
|
274 |
|
|
}
|
275 |
|
|
|
276 |
879 |
markom |
/* expands immediate memory instructions to two */
|
277 |
|
|
void expand_memory ()
|
278 |
|
|
{
|
279 |
|
|
int i, j, num_mem = 0, d;
|
280 |
|
|
for (i = 0; i < num_insn; i++) if (insn[i].type & IT_MEMORY) num_mem++;
|
281 |
|
|
|
282 |
|
|
d = num_insn + num_mem;
|
283 |
|
|
assert (d < MAX_INSNS);
|
284 |
|
|
|
285 |
|
|
/* Split memory commands */
|
286 |
|
|
for (i = num_insn - 1; i >= 0; i--) if (insn[i].type & IT_MEMORY) {
|
287 |
|
|
insn[--d] = insn[i];
|
288 |
|
|
insn[--d] = insn[i];
|
289 |
|
|
reloc[i] = d;
|
290 |
|
|
switch (insn[d].index) {
|
291 |
|
|
case II_SW:
|
292 |
|
|
case II_SH:
|
293 |
|
|
case II_SB:
|
294 |
1060 |
markom |
insn[d + 1].op[1] = d; insn[d + 1].opt[1] = OPT_REF; /* sw rx,(t($-1)) */
|
295 |
|
|
insn[d + 1].op[0] = insn[i].op[2]; insn[d + 1].opt[0] = insn[d + 1].opt[2];
|
296 |
|
|
insn[d + 1].opt[2] = OPT_NONE;
|
297 |
|
|
insn[d + 1].type &= ~IT_BBSTART;
|
298 |
1061 |
markom |
insn[d].op[2] = insn[d].op[0]; insn[d].opt[2] = insn[d].opt[0];
|
299 |
879 |
markom |
insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST; /* add rd, ra, rb */
|
300 |
|
|
insn[d].opt[3] = OPT_NONE;
|
301 |
|
|
insn[d].type &= IT_INDELAY | IT_BBSTART;
|
302 |
|
|
insn[d].type |= IT_MEMADD;
|
303 |
|
|
change_insn_type (&insn[d], II_ADD);
|
304 |
|
|
break;
|
305 |
|
|
case II_LW:
|
306 |
|
|
case II_LH:
|
307 |
|
|
case II_LB:
|
308 |
|
|
insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST; /* add rd, ra, rb */
|
309 |
|
|
insn[d].type &= IT_INDELAY | IT_BBSTART;
|
310 |
|
|
insn[d].type |= IT_MEMADD;
|
311 |
|
|
change_insn_type (&insn[d], II_ADD);
|
312 |
|
|
insn[d + 1].op[1] = d; insn[d + 1].opt[1] = OPT_REF; /* lw (t($-1)),rx */
|
313 |
|
|
insn[d + 1].opt[2] = OPT_NONE;
|
314 |
|
|
insn[d + 1].opt[3] = OPT_NONE;
|
315 |
|
|
insn[d + 1].type &= ~IT_BBSTART;
|
316 |
|
|
break;
|
317 |
|
|
default: fprintf (stderr, "%4i, %4i: %s\n", i, d, cuc_insn_name (&insn[d]));
|
318 |
|
|
assert (0);
|
319 |
|
|
}
|
320 |
|
|
} else {
|
321 |
|
|
insn[--d] = insn[i];
|
322 |
|
|
reloc[i] = d;
|
323 |
|
|
}
|
324 |
|
|
num_insn += num_mem;
|
325 |
|
|
for (i = 0; i < num_insn; i++) if (!(insn[i].type & IT_MEMORY))
|
326 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
327 |
|
|
if (insn[i].opt[j] & OPT_REF || insn[i].opt[j] & OPT_JUMP)
|
328 |
|
|
insn[i].op[j] = reloc[insn[i].op[j]];
|
329 |
|
|
}
|
330 |
|
|
|
331 |
|
|
/* expands signed comparisons to three instructions */
|
332 |
|
|
void expand_signed ()
|
333 |
|
|
{
|
334 |
|
|
int i, j, num_sig = 0, d;
|
335 |
897 |
markom |
for (i = 0; i < num_insn; i++)
|
336 |
|
|
if (insn[i].type & IT_SIGNED && !(insn[i].type & IT_MEMORY)) num_sig++;
|
337 |
879 |
markom |
|
338 |
|
|
d = num_insn + num_sig * 2;
|
339 |
|
|
assert (d < MAX_INSNS);
|
340 |
|
|
|
341 |
|
|
/* Split signed instructions */
|
342 |
897 |
markom |
for (i = num_insn - 1; i >= 0; i--)
|
343 |
879 |
markom |
/* We will expand signed memory later */
|
344 |
897 |
markom |
if (insn[i].type & IT_SIGNED && !(insn[i].type & IT_MEMORY)) {
|
345 |
|
|
insn[--d] = insn[i];
|
346 |
|
|
insn[d].op[1] = d - 2; insn[d].opt[1] = OPT_REF;
|
347 |
|
|
insn[d].op[2] = d - 1; insn[d].opt[2] = OPT_REF;
|
348 |
879 |
markom |
|
349 |
897 |
markom |
insn[--d] = insn[i];
|
350 |
|
|
change_insn_type (&insn[d], II_ADD);
|
351 |
|
|
insn[d].type = 0;
|
352 |
|
|
insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST;
|
353 |
|
|
insn[d].op[1] = insn[d].op[2]; insn[d].opt[1] = insn[d].opt[2];
|
354 |
905 |
markom |
insn[d].op[2] = 0x80000000; insn[d].opt[2] = OPT_CONST;
|
355 |
897 |
markom |
insn[d].opt[3] = OPT_NONE;
|
356 |
879 |
markom |
|
357 |
897 |
markom |
insn[--d] = insn[i];
|
358 |
|
|
change_insn_type (&insn[d], II_ADD);
|
359 |
|
|
insn[d].type = 0;
|
360 |
|
|
insn[d].op[0] = -1; insn[d].opt[0] = OPT_REGISTER | OPT_DEST;
|
361 |
|
|
insn[d].op[1] = insn[d].op[1]; insn[d].opt[1] = insn[d].opt[1];
|
362 |
905 |
markom |
insn[d].op[2] = 0x80000000; insn[d].opt[2] = OPT_CONST;
|
363 |
897 |
markom |
insn[d].opt[3] = OPT_NONE;
|
364 |
879 |
markom |
|
365 |
897 |
markom |
reloc[i] = d;
|
366 |
|
|
} else {
|
367 |
|
|
insn[--d] = insn[i];
|
368 |
|
|
reloc[i] = d;
|
369 |
|
|
}
|
370 |
879 |
markom |
num_insn += num_sig * 2;
|
371 |
|
|
for (i = 0; i < num_insn; i++) if (insn[i].type & IT_MEMORY || !(insn[i].type & IT_SIGNED)) {
|
372 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
373 |
|
|
if (insn[i].opt[j] & OPT_REF || insn[i].opt[j] & OPT_JUMP)
|
374 |
|
|
insn[i].op[j] = reloc[insn[i].op[j]];
|
375 |
|
|
} else insn[i].type &= ~IT_SIGNED;
|
376 |
|
|
}
|
377 |
|
|
|
378 |
906 |
markom |
/* expands calls to 7 instructions */
|
379 |
|
|
void expand_calls ()
|
380 |
|
|
{
|
381 |
|
|
int i, j, num_call = 0, d;
|
382 |
|
|
for (i = 0; i < num_insn; i++)
|
383 |
|
|
if (insn[i].index == II_CALL) num_call++;
|
384 |
|
|
|
385 |
|
|
d = num_insn + num_call * 6; /* 6 parameters */
|
386 |
|
|
assert (d < MAX_INSNS);
|
387 |
|
|
|
388 |
|
|
/* Split call instructions */
|
389 |
|
|
for (i = num_insn - 1; i >= 0; i--)
|
390 |
|
|
/* We will expand signed memory later */
|
391 |
|
|
if (insn[i].index == II_CALL) {
|
392 |
|
|
insn[--d] = insn[i];
|
393 |
|
|
insn[d].op[0] = insn[d].op[1]; insn[d].opt[0] = OPT_CONST;
|
394 |
|
|
insn[d].opt[1] = OPT_NONE;
|
395 |
|
|
insn[d].type |= IT_VOLATILE;
|
396 |
|
|
|
397 |
|
|
for (j = 0; j < 6; j++) {
|
398 |
|
|
insn[--d] = insn[i];
|
399 |
|
|
change_insn_type (&insn[d], II_ADD);
|
400 |
|
|
insn[d].type = IT_VOLATILE;
|
401 |
|
|
insn[d].op[0] = 3 + j; insn[d].opt[0] = OPT_REGISTER | OPT_DEST;
|
402 |
|
|
insn[d].op[1] = 3 + j; insn[d].opt[1] = OPT_REGISTER;
|
403 |
|
|
insn[d].op[2] = 0x80000000; insn[d].opt[2] = OPT_CONST;
|
404 |
|
|
insn[d].opt[3] = OPT_NONE;
|
405 |
|
|
}
|
406 |
|
|
|
407 |
|
|
reloc[i] = d;
|
408 |
|
|
} else {
|
409 |
|
|
insn[--d] = insn[i];
|
410 |
|
|
reloc[i] = d;
|
411 |
|
|
}
|
412 |
|
|
num_insn += num_call * 6;
|
413 |
|
|
for (i = 0; i < num_insn; i++)
|
414 |
|
|
for (j = 0; j < MAX_OPERANDS; j++)
|
415 |
|
|
if (insn[i].opt[j] & OPT_REF || insn[i].opt[j] & OPT_JUMP)
|
416 |
|
|
insn[i].op[j] = reloc[insn[i].op[j]];
|
417 |
|
|
}
|
418 |
|
|
|
419 |
897 |
markom |
/* Loads function from file into global array insn.
|
420 |
|
|
Function returns nonzero if function cannot be converted. */
|
421 |
|
|
int cuc_load (char *in_fn)
|
422 |
879 |
markom |
{
|
423 |
1308 |
phoenix |
int i, j;
|
424 |
879 |
markom |
FILE *fi;
|
425 |
|
|
int func_return = 0;
|
426 |
|
|
num_insn = 0;
|
427 |
|
|
|
428 |
|
|
log ("Loading filename %s\n", in_fn);
|
429 |
|
|
if ((fi = fopen (in_fn, "rt")) == NULL) {
|
430 |
|
|
fprintf (stderr, "Cannot open '%s'\n", in_fn);
|
431 |
|
|
exit (1);
|
432 |
|
|
}
|
433 |
|
|
/* Read in the function and decode the instructions */
|
434 |
|
|
for (i = 0;; i++) {
|
435 |
|
|
unsigned long data;
|
436 |
|
|
const char *name;
|
437 |
|
|
|
438 |
1308 |
phoenix |
if (fscanf (fi, "%08lx\n", &data) != 1) break;
|
439 |
879 |
markom |
|
440 |
|
|
/* build params */
|
441 |
|
|
name = build_insn (data, &insn[i]);
|
442 |
|
|
if (func_return) func_return++;
|
443 |
997 |
markom |
//PRINTF ("%s\n", name);
|
444 |
879 |
markom |
|
445 |
|
|
if (or32_opcodes[insn[i].index].flags & OR32_IF_DELAY) {
|
446 |
|
|
int f;
|
447 |
|
|
if (strcmp (name, "l.bnf") == 0) f = 1;
|
448 |
|
|
else if (strcmp (name, "l.bf") == 0) f = 0;
|
449 |
|
|
else if (strcmp (name, "l.j") == 0) {
|
450 |
|
|
f = -1;
|
451 |
|
|
} else if (strcmp (name, "l.jr") == 0 && func_return == 0) {
|
452 |
|
|
func_return = 1;
|
453 |
|
|
change_insn_type (&insn[i], II_NOP);
|
454 |
|
|
continue;
|
455 |
|
|
} else {
|
456 |
897 |
markom |
cucdebug (1, "Instruction #%i: \"%s\" not supported.\n", i, name);
|
457 |
|
|
log ("Instruction #%i: \"%s\" not supported.\n", i, name);
|
458 |
|
|
return 1;
|
459 |
879 |
markom |
}
|
460 |
|
|
if (f < 0) { /* l.j */
|
461 |
|
|
/* repair params */
|
462 |
|
|
change_insn_type (&insn[i], II_BF);
|
463 |
|
|
insn[i].op[0] = i + insn[i].op[0]; insn[i].opt[0] = OPT_JUMP;
|
464 |
|
|
insn[i].op[1] = 1; insn[i].opt[1] = OPT_CONST;
|
465 |
|
|
insn[i].type |= IT_BRANCH | IT_VOLATILE;
|
466 |
|
|
} else {
|
467 |
|
|
change_insn_type (&insn[i], II_BF);
|
468 |
|
|
insn[i].op[0] = i + insn[i].op[0]; insn[i].opt[0] = OPT_JUMP;
|
469 |
|
|
insn[i].op[1] = FLAG_REG; insn[i].opt[1] = OPT_REGISTER;
|
470 |
|
|
insn[i].type |= IT_BRANCH | IT_VOLATILE;
|
471 |
1062 |
markom |
if (f) insn[i].type |= IT_FLAG1;
|
472 |
879 |
markom |
}
|
473 |
|
|
} else {
|
474 |
|
|
insn[i].index = -1;
|
475 |
|
|
for (j = 0; j < sizeof (conv) / sizeof (cuc_conv); j++)
|
476 |
|
|
if (strcmp (conv[j].from, name) == 0) {
|
477 |
|
|
if (conv[j].to & II_SIGNED) insn[i].type |= IT_SIGNED;
|
478 |
|
|
if (conv[j].to & II_MEM) insn[i].type |= IT_MEMORY | IT_VOLATILE;
|
479 |
|
|
change_insn_type (&insn[i], conv[j].to & II_MASK);
|
480 |
|
|
break;
|
481 |
|
|
}
|
482 |
898 |
markom |
if (strcmp (name, "l.movhi") == 0) {
|
483 |
|
|
insn[i].op[1] <<= 16;
|
484 |
|
|
insn[i].op[2] = 0;
|
485 |
|
|
insn[i].opt[2] = OPT_CONST;
|
486 |
|
|
}
|
487 |
1062 |
markom |
if (insn[i].index == II_SFEQ || insn[i].index == II_SFNE
|
488 |
|
|
|| insn[i].index == II_SFLE || insn[i].index == II_SFGT
|
489 |
|
|
|| insn[i].index == II_SFGE || insn[i].index == II_SFLT) {
|
490 |
|
|
/* repair params */
|
491 |
|
|
insn[i].op[2] = insn[i].op[1]; insn[i].opt[2] = insn[i].opt[1] & ~OPT_DEST;
|
492 |
|
|
insn[i].op[1] = insn[i].op[0]; insn[i].opt[1] = insn[i].opt[0] & ~OPT_DEST;
|
493 |
|
|
insn[i].op[0] = FLAG_REG; insn[i].opt[0] = OPT_DEST | OPT_REGISTER;
|
494 |
|
|
insn[i].opt[3] = OPT_NONE;
|
495 |
|
|
insn[i].type |= IT_COND;
|
496 |
|
|
}
|
497 |
897 |
markom |
if (insn[i].index < 0 || insn[i].index == II_NOP && insn[i].op[0] != 0) {
|
498 |
|
|
cucdebug (1, "Instruction #%i: \"%s\" not supported (2).\n", i, name);
|
499 |
|
|
log ("Instruction #%i: \"%s\" not supported (2).\n", i, name);
|
500 |
|
|
return 1;
|
501 |
879 |
markom |
}
|
502 |
|
|
}
|
503 |
|
|
}
|
504 |
|
|
num_insn = i;
|
505 |
|
|
fclose (fi);
|
506 |
|
|
if (func_return != 2) {
|
507 |
897 |
markom |
cucdebug (1, "Unsupported function structure.\n");
|
508 |
|
|
log ("Unsupported function structure.\n");
|
509 |
|
|
return 1;
|
510 |
879 |
markom |
}
|
511 |
|
|
|
512 |
|
|
log ("Number of instructions loaded = %i\n", num_insn);
|
513 |
883 |
markom |
if (cuc_debug >= 3) print_cuc_insns ("INITIAL", 1);
|
514 |
879 |
markom |
|
515 |
|
|
log ("Converting.\n");
|
516 |
1043 |
markom |
expand_branch ();
|
517 |
|
|
if (cuc_debug >= 6) print_cuc_insns ("AFTER_EXP_BRANCH", 0);
|
518 |
|
|
|
519 |
879 |
markom |
remove_dslots ();
|
520 |
883 |
markom |
if (cuc_debug >= 6) print_cuc_insns ("NO_DELAY_SLOTS", 0);
|
521 |
879 |
markom |
|
522 |
897 |
markom |
if (config.cuc.calling_convention) {
|
523 |
879 |
markom |
detect_locals ();
|
524 |
883 |
markom |
if (cuc_debug >= 7) print_cuc_insns ("AFTER_LOCALS", 0);
|
525 |
879 |
markom |
}
|
526 |
|
|
expand_memory ();
|
527 |
883 |
markom |
if (cuc_debug >= 3) print_cuc_insns ("AFTER_EXP_MEM", 0);
|
528 |
879 |
markom |
|
529 |
|
|
expand_signed ();
|
530 |
883 |
markom |
if (cuc_debug >= 3) print_cuc_insns ("AFTER_EXP_SIG", 0);
|
531 |
906 |
markom |
|
532 |
|
|
expand_calls ();
|
533 |
|
|
if (cuc_debug >= 3) print_cuc_insns ("AFTER_EXP_CALLS", 0);
|
534 |
1043 |
markom |
|
535 |
897 |
markom |
return 0;
|
536 |
879 |
markom |
}
|