emit.c
10.2 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* 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.
*
*/
/* from common */
#include "simtypes.h"
#include "sim_error.h"
#include "tc.h"
#include "tc_coherence.h"
/* cpus-alpha/alpha_shared */
#include "alpha.h"
#include "alpha_regs.h"
#include "qc.h"
#include "ev5_ipr.h"
/* gamma */
#include "gamma.h"
/* delta */
#include "assym.h"
#include "emit.h"
#include "delta.h"
#define EMIT_U(_x) { (emit[currentCache].emitptr++)->word = _x;}
#define EMIT_INSTR(_x) { *emit[currentCache].emitptr++ = _x;}
#define EMITTED_LENGTH 200
static int currentCache;
static int emitCount;
static struct {
TCA emitptr;
void *emittedFunctions[MAX_CALLOUTS];
} emit[TC_MAX_CACHES];
static void Emit_CalloutQC(int callout, int saveCalloutRegs)
{
int jmpReg = REG_PV;
EMIT_LOAD(jmpReg,DELTAREG_CONSTANTS,callout*sizeof(Reg));
EMIT_STORE(REG_RA,REG_SP,0);
if (saveCalloutRegs) {
EMIT_STORE(DELTAREG_CALLOUT_PC,DELTAREG_BASE,PC_OFF);
EMIT_STORE(DELTAREG_CALLOUT_CCD,DELTAREG_BASE,COUNTDOWN_OFF);
} else {
EMIT_STORE(DELTAREG_PC,DELTAREG_BASE,PC_OFF);
EMIT_STORE(DELTAREG_COUNTDOWN,DELTAREG_BASE,COUNTDOWN_OFF);
}
EMIT_JMP(REG_RA,jmpReg,jsr_jsr,0);
EMIT_LOAD(REG_RA,REG_SP,0);
EMIT_RELOAD();
EMIT_JMP(REG_ZERO,REG_RA,jsr_ret,0);
}
static void Emit_Callout(int callout, int reg_ra, int jsr)
{
int jmpReg = REG_PV;
EMIT_LOAD(jmpReg,DELTAREG_CONSTANTS,callout*sizeof(Reg));
EMIT_JMP(reg_ra,jmpReg,jsr,0);
}
/* ******************************************************************
* QCLookup{Read,Write}: assembly version of QCLookup (qc.c)
*
* Arguments: qcTable and vAddr are not modified
*
* returns: REG_V0: mAddr (or 0)
*
* Algorithm:
* vpn0 = VPN(vAddr)
* tmp = HASH(vpn0)
* tmp = tmp << log2EntrySize + qcTable
* vpn1 = 0(tmp)
* mAddr= 8(tmp)
*
*/
static void Emit_QCLookup(int isWrite, int qcTable, int vAddr )
{
/* REG_A0, REG_A1 assumed to be used */
int vpn0 = REG_A2;
int tmp = REG_A3;
int vpn1 = REG_A4;
int offset =REG_A5;
int mAddr = REG_V0;
int mask = REG_AT;
int log2QCEntry = 4;
ASSERT( (1<<log2QCEntry) == sizeof(struct QCEntry));
ASSERT( qcTable!=vpn0 && qcTable!=tmp && qcTable!=vpn1 &&
qcTable!=offset &&qcTable!=mAddr && qcTable!=mask);
ASSERT( vAddr!=vpn0 && vAddr!=tmp && vAddr!=vpn1 &&
vAddr!=offset &&vAddr!=mAddr && vAddr!=mask);
/* compute hash function and offset */
EMIT_OPI(op_ints,ints_sra,vAddr,PAGE_BITS,vpn0);
EMIT_LDA(mask,REG_ZERO,(QC_INDEX-1));
EMIT_OP(op_intl,intl_and,vpn0,mask,tmp);
if ((QC_INDEX-1) != (1<<PAGE_BITS)-1) {
EMIT_LDA(mask,REG_ZERO,(1<<PAGE_BITS)-1);
}
EMIT_OP(op_intl,intl_and,vAddr,mask,offset);
/* scale to compute offset */
EMIT_OPI(op_ints,ints_sll,tmp,log2QCEntry,tmp);
EMIT_OP(op_inta,inta_addq,qcTable,tmp,tmp);
/* load vpn and mAddr */
EMIT_LOAD(vpn1,tmp,0);
EMIT_LOAD(mAddr,tmp,8);
if (isWrite) {
EMIT_MOVE(mAddr,mask);
}
/* clear low bit of mAddr */
EMIT_OPI(op_intl,intl_bic,mAddr,-1,mAddr);
/* tmp = vpn0==vpn1 */
EMIT_OP(op_intl,intl_xor,vpn0,vpn1,tmp);
/* mAddr = mAddr + page offset */
EMIT_OP(op_intl,intl_bis,mAddr,offset,mAddr);
if (isWrite) {
/* low bit of mAddr clear --> clear mAddr */
EMIT_OP(op_intl,intl_cmovlbc,mask,REG_ZERO,mAddr);
}
/* do the vpns match ?. Clear mAddr if not */
EMIT_OP(op_intl,intl_cmovne,tmp,REG_ZERO,mAddr);
}
static void Emit_QCLookupRead(void)
{
Emit_QCLookup(0,REG_A0,REG_A1);
EMIT_JMP(REG_ZERO,REG_RA,jsr_ret,0);
}
static void Emit_QCLookupWrite(void)
{
Emit_QCLookup(1,REG_A0,REG_A1);
EMIT_JMP(REG_ZERO,REG_RA,jsr_ret,0);
}
static void Emit_ITB(void)
{
union alpha_instruction inst;
if (delta.inlineIQC) {
Emit_QCLookup(0,DELTAREG_CURIQC,DELTAREG_PC);
inst.common.opcode = op_beq;
inst.b_format.ra = REG_V0;
inst.b_format.branch_displacement = 1;
EMIT_INSTR(inst);
EMIT_JMP(REG_ZERO,REG_RA,jsr_ret,0);
Emit_CalloutQC(CALLOUT_ITB,0);
} else {
Emit_CalloutQC(CALLOUT_ITB,0);
}
}
static void Emit_DTBLoad(void)
{
union alpha_instruction inst;
if (delta.inlineDQC) {
Emit_QCLookup(0,DELTAREG_CURDQC,REG_A0);
inst.common.opcode = op_beq;
inst.b_format.ra = REG_V0;
inst.b_format.branch_displacement = 1;
EMIT_INSTR(inst);
EMIT_JMP(REG_ZERO,REG_RA,jsr_ret,0);
Emit_CalloutQC(CALLOUT_DTB_LOAD,1);
} else {
Emit_CalloutQC(CALLOUT_DTB_LOAD,1);
}
}
static void Emit_DTBStore(void)
{
union alpha_instruction inst;
if (delta.inlineDQC) {
Emit_QCLookup(1,DELTAREG_CURDQC,REG_A0);
inst.common.opcode = op_beq;
inst.b_format.ra = REG_V0;
inst.b_format.branch_displacement = 1;
EMIT_INSTR(inst);
EMIT_JMP(REG_ZERO,REG_RA,jsr_ret,0);
Emit_CalloutQC(CALLOUT_DTB_STORE,1);
} else {
Emit_CalloutQC(CALLOUT_DTB_STORE,1);
}
}
static void Emit_CheckFPE(void)
{
#ifndef EV5_IPR_H
#error "Assumes EV5";
#endif
union alpha_instruction inst;
int tmp = REG_V0;
EMIT_LOAD(tmp,DELTAREG_BASE,IPR_OFF);
EMIT_LOAD(tmp,tmp,IPR_ICSR*8);
EMIT_OPI(op_ints,ints_srl,tmp,26,tmp);
EMIT_OPI(op_intl,intl_and,tmp,1,tmp);
inst.common.opcode = op_beq;
inst.b_format.ra = REG_V0;
inst.b_format.branch_displacement = 1;
EMIT_INSTR(inst);
EMIT_JMP(REG_ZERO,REG_RA,jsr_ret,0);
Emit_CalloutQC(CALLOUT_FEN,1);
}
/* **********************************************
* Emit_CTXT
*
* COUNTDOWN,PC already saved.
*
* **********************************************/
static void Emit_CTXT(void)
{
int cnstOff;
cnstOff = DeltaAllocateConstant(&curPE);
EMIT_STORE(DELTAREG_PC, DELTAREG_BASE,PC_OFF);
EMIT_STORE(DELTAREG_COUNTDOWN,DELTAREG_BASE,COUNTDOWN_OFF);
/* BASE = BASE->nextCPU */
EMIT_LOAD(DELTAREG_BASE,DELTAREG_BASE,NEXTCPU_OFF);
/* P = BASE */
EMIT_LOAD(REG_PV,DELTAREG_CONSTANTS,cnstOff);
EMIT_STORE(DELTAREG_BASE,REG_PV,0);
EMIT_LOAD(DELTAREG_PC,DELTAREG_BASE,PC_OFF);
EMIT_LOAD(DELTAREG_COUNTDOWN,DELTAREG_BASE,COUNTDOWN_OFF);
EMIT_STORE(DELTAREG_CONSTANTS,DELTAREG_BASE,CONSTANTS_OFF);
/* V0 = CalloutChain(0) (or never return) */
EMIT_MOVE(REG_ZERO,REG_A0);
Emit_Callout(CALLOUT_CHAIN_CTXT,REG_RA,jsr_jsr);
EMIT_MOVE(REG_V0,REG_PV);
EMIT_RELOAD();
EMIT_JMP(REG_ZERO,REG_V0,jsr_ret,0);
}
/* *******************************************************************
* EnterTC
* Arguments: REG_A0: P
* REG_A1: jumpAddress in TC
* REG_A2: stackptr
* REG_A3: contantsptr
*
* *******************************************************************/
static void Emit_EnterTC(void)
{
EMIT_MOVE(REG_A0,DELTAREG_BASE);
EMIT_MOVE(REG_A1,REG_PV);
EMIT_MOVE(REG_A2,REG_SP);
EMIT_STORE(REG_A3,DELTAREG_BASE,CONSTANTS_OFF);
EMIT_LOAD(DELTAREG_PC,DELTAREG_BASE,PC_OFF);
EMIT_LOAD(DELTAREG_COUNTDOWN,DELTAREG_BASE,COUNTDOWN_OFF);
EMIT_RELOAD();
EMIT_JMP(REG_ZERO,REG_PV,jsr_jmp,0);
}
/* *******************************************************************
* Chain.
* Arguments: REG_RA: TCA of UpdatePC position in previous BB
* DELTAREG_PREV_PREV_PC (virtual address)
*
* *******************************************************************/
static void Emit_Chain(void)
{
EMIT_STORE(DELTAREG_COUNTDOWN,DELTAREG_BASE,COUNTDOWN_OFF);
EMIT_LDA(REG_A0,REG_RA,-4);
EMIT_STORE(DELTAREG_PC,DELTAREG_BASE,PC_OFF);
if (DELTAREG_PREV_PC != REG_A1) {
EMIT_MOVE(DELTAREG_PREV_PC,REG_A1);
}
Emit_Callout(CALLOUT_CHAIN,REG_RA,jsr_jmp);
EMIT_MOVE(REG_V0,REG_PV);
EMIT_RELOAD();
EMIT_JMP(REG_ZERO,REG_V0,jsr_ret,0);
}
/* *******************************************************************
* Chain.
* Arguments: REG_RA: TCA of UpdatePC position in previous BB
*
*
* *******************************************************************/
static void Emit_ChainSpec(void)
{
EMIT_LDA(REG_A0,REG_RA,-4);
Emit_Callout(CALLOUT_CHAIN_SPEC,REG_RA,jsr_jmp);
EMIT_MOVE(REG_V0,REG_PV);
EMIT_RELOAD();
EMIT_JMP(REG_ZERO,REG_V0,jsr_ret,0);
}
static void Emit_InitSP(void)
{
EMIT_LDA(REG_V0,REG_SP, -256);
EMIT_JMP(REG_ZERO,REG_RA,jsr_ret,0);
}
void EmitOne(char *name, int idx, void (*f)())
{
extern disasm();
TCA start = emit[currentCache].emitptr;
char buf[200];
long piG1=0, piG2=0, piG3=0;
f();
while( (uint64)emit[currentCache].emitptr &0xf) {
(emit[currentCache].emitptr++)->word = 0;
}
emit[currentCache].emittedFunctions[idx] = start;
if (emitCount==1) {
while (start != emit[currentCache].emitptr) {
if (!start->word) {
buf[0] = '\0';
} else {
disasm(buf,(VA)start,start->word,
&piG1, &piG2, &piG3);
}
CPUPrint("%-10s : 0x%lx : %s\n",name,(VA)start,buf);
start++;
}
}
}
void EmitFunctions(int cache)
{
TCA start;
currentCache = cache;
bzero((char*)&emit[cache],sizeof(emit[cache]));
emitCount++;
#if 0
CPUWarning("Emitted functions (%d time generate) \n",emitCount);
#endif
emit[cache].emitptr = start = TC_GetTCPtr(cache);
ASSERT(TC_Is_Room(EMITTED_LENGTH*sizeof(Inst),cache));
EmitOne("ITB",CALLOUT_ITB,Emit_ITB);
EmitOne("DTBLoad",CALLOUT_DTB_LOAD,Emit_DTBLoad);
EmitOne("DTBStore",CALLOUT_DTB_STORE,Emit_DTBStore);
EmitOne("CTXT",CALLOUT_CTXT,Emit_CTXT);
EmitOne("EnterTC",CALLOUT_ENTERTC,Emit_EnterTC);
EmitOne("Chain",CALLOUT_CHAIN,Emit_Chain);
EmitOne("ChainSpec",CALLOUT_CHAIN_SPEC,Emit_ChainSpec);
EmitOne("InitSP",CALLOUT_INITSP,Emit_InitSP);
EmitOne("QCLookupRead",CALLOUT_QCLOOKUP_READ ,Emit_QCLookupRead);
EmitOne("QCLookupWrite",CALLOUT_QCLOOKUP_WRITE,Emit_QCLookupWrite);
EmitOne("CheckFPE",CALLOUT_FEN,Emit_CheckFPE);
ASSERT (emit[cache].emitptr - start < EMITTED_LENGTH);
TC_SetTCNext(cache,start,emit[cache].emitptr);
}
void * GetEmitFunction(int cache,int callout)
{
ASSERT( emit[cache].emittedFunctions[callout]);
return emit[cache].emittedFunctions[callout];
}