decoder.c
6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (C) 1998 by the Board of Trustees
* of Leland Stanford Junior University.
* Copyright (C) 1998 Digital Equipment Corporation
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
#include "simtypes.h"
#include "sim_error.h"
#include "tc.h"
#include "arch_specifics.h"
#include "alpha.h"
#include "alpha_regs.h"
#include "gamma.h"
#include "delta.h"
#include "decoder.h"
static inline void PreloadRegister(struct RegAlloc *regAlloc, int reg)
{
uint32 bit = 1<<reg;
if (reg==REG_ZERO) return;
if (!(regAlloc->accessed & bit)) {
regAlloc->accessed |= bit;
if (regAlloc->numRegsAlloc < NUM_SHADOW_REGS) {
regAlloc->reg[reg] = regAlloc->numRegsAlloc;
regAlloc->preload[regAlloc->numRegsAlloc++] = reg;
}
}
}
static inline void KillRegister(struct RegAlloc *regAlloc,int reg)
{
uint32 bit = 1<<reg;
if (!(regAlloc->accessed & bit)) {
regAlloc->accessed |= bit;
}
}
static void DecodeBlock( VA vPC, MA mPC, BasicBlock *bb)
{
int offset=0;
register union alpha_instruction inst;
BasicBlockInstr *bbInst = bb->instr;
int stop=0;
int opcode;
bb->usesFP = 0;
while(!stop && PAGE_NUMBER(vPC+offset) == PAGE_NUMBER(vPC) &&
bb->numInstructions <DECODER_MAX_INSTR) {
inst = *(union alpha_instruction *) (mPC+offset);
bbInst->instr = inst;
bbInst->offset = offset;
bbInst->cycles = bb->numCycles++;
bbInst->saveReg = REG_ZERO;
bbInst->isBranch = 0;
opcode = inst.common.opcode;
switch(opcode) {
case op_call_pal:
case op_misc:
callout_misc:
bbInst->saveReg |= SAVE_REG_CALLOUT;
stop = 1;
break;
case op_br:
case op_bsr:
KillRegister(&bb->regAlloc,inst.b_format.ra);
bbInst->mergedBranch = 0;
bbInst->isBranch = 1;
stop = 1;
if (delta.mergeBranches) {
int dist = inst.b_format.branch_displacement<<2;
VA targetPC = vPC + offset + dist + 4;
if (VADDR2VPN(vPC)==VADDR2VPN(targetPC)) {
#if 0
CPUPrint("Merged branch at pc=0x%lx mPC=0x%lx target=0x%lx \n",
vPC,mPC,targetPC);
#endif
stop = 0;
bbInst->mergedBranch = 1;
offset+=dist;
}
}
break;
case op_blbc:
case op_beq:
case op_blt:
case op_ble:
case op_blbs:
case op_bne:
case op_bge:
case op_bgt:
PreloadRegister(&bb->regAlloc,inst.b_format.ra);
stop = 1;
bbInst->isBranch = 1;
break;
case op_fbeq:
case op_fblt:
case op_fble:
case op_fbne:
case op_fbge:
case op_fbgt:
bb->usesFP = 1;
bbInst->isBranch = 1;
stop = 1;
break;
case op_opc14:
/* @@@@ */
NOTREACHED();
goto callout_misc;
bb->usesFP = 1;
goto callout_misc;
case op_ldf:
case op_ldg:
case op_lds:
case op_ldt:
case op_stf:
case op_stg:
case op_sts:
case op_stt:
case op_fltv:
case op_flti:
case op_fltl:
bb->usesFP = 1;
break;
case op_jsr:
PreloadRegister(&bb->regAlloc,inst.j_format.ra);
stop = 1;
bbInst->isBranch = 1;
break;
case op_intl:
if (inst.o_format.function == intl_amask
|| inst.o_format.function == intl_implver) {
goto callout_misc;
break;
} else {
goto int_ops;
}
int_ops:
case op_inta:
case op_ints:
case op_intm:
if (inst.o_format.rc != REG_ZERO) {
int flags = gammaOperateFlags[opcode*(1<<7)+inst.o_format.function];
PreloadRegister(&bb->regAlloc,inst.o_format.ra);
if (!inst.o_format.form) {
PreloadRegister(&bb->regAlloc,inst.o_format.rb);
}
bbInst->saveReg = inst.o_format.rc;
if (flags&AXPTYPE_INT_CONDITIONAL) {
PreloadRegister(&bb->regAlloc,inst.o_format.rc);
} else {
KillRegister(&bb->regAlloc,inst.o_format.rc);
}
if (flags & AXPTYPE_CANTRAP) {
bbInst->saveReg |= SAVE_REG_CALLOUT;
}
}
break;
case op_lda:
case op_ldah:
bbInst->saveReg = inst.m_format.ra;
PreloadRegister(&bb->regAlloc,inst.m_format.rb);
KillRegister(&bb->regAlloc,inst.m_format.ra);
break;
case op_ldq_u:
case op_ldl:
case op_ldq:
case op_ldl_l:
case op_ldq_l:
bbInst->saveReg = (inst.m_format.ra|SAVE_REG_CALLOUT);
PreloadRegister(&bb->regAlloc,inst.m_format.rb);
KillRegister(&bb->regAlloc,inst.m_format.ra);
break;
case op_stq_u:
case op_stl:
case op_stq:
bbInst->saveReg = SAVE_REG_CALLOUT;
PreloadRegister(&bb->regAlloc,inst.m_format.ra);
PreloadRegister(&bb->regAlloc,inst.m_format.rb);
break;
case op_stl_c:
case op_stq_c:
bbInst->saveReg = (SAVE_REG_CALLOUT | inst.m_format.ra);
PreloadRegister(&bb->regAlloc,inst.m_format.ra);
PreloadRegister(&bb->regAlloc,inst.m_format.rb);
bb->hasSC = 1;
break;
default:
/* nothing */
} /* switch(opcode) */
offset +=4;
bb->numInstructions++;
bbInst++;
}
bb->nextPC = vPC+offset;
}
static void OptimizeSaveReg(BasicBlock *bb)
{
uint bv=0; /* bitvector of saved registers */
int i;
for (i=bb->numInstructions-1;i>=0;i--) {
int saveReg = bb->instr[i].saveReg;
int reg = saveReg & REG_ZERO;
uint mask = (1<<reg);
if ((reg != REG_ZERO) && (bv&mask)) {
bb->instr[i].saveReg |= SAVE_REG_OPT;
} else {
bv |= mask;
}
if (saveReg & SAVE_REG_CALLOUT) {
bv = 0;
}
}
}
void Decode( VA vPC, MA mPC, BasicBlock *bb)
{
int i;
bb->vPC = vPC;
bb->mPC = mPC;
bb->numInstructions = 0;
bb->numCycles = 0;
bb->hasPCAnn =0;
bb->hasSC = 0;
bzero((char *)&bb->regAlloc,sizeof(bb->regAlloc));
bb->regAlloc.numRegsAlloc= 1; /* 0 fakes out REG_ZERO */
DecodeBlock(vPC,mPC,bb);
for (i=0;i<bb->numInstructions;i++) {
BasicBlockInstr *bbInst = &bb->instr[i];
bbInst->prePCAnn = (AnnFMLookup(vPC+bbInst->offset,ANNFM_PRE_PC_TYPE)!=0);
bbInst->postPCAnn = (AnnFMLookup(vPC+bbInst->offset,ANNFM_PC_TYPE)!=0);
bb->hasPCAnn |= (bbInst->prePCAnn | bbInst->postPCAnn) ;
}
if (delta.saveRegOpt && !bb->hasPCAnn && !bb->hasSC && !bb->usesFP) {
OptimizeSaveReg(bb);
}
}