-
Notifications
You must be signed in to change notification settings - Fork 5
/
XDC_CODE.PAS
335 lines (302 loc) · 8.58 KB
/
XDC_CODE.PAS
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
unit xdc_codegeneration;
{
Contains all structures and procedures responsible for generating code.
}
interface
uses
xdc_deltas;
type
codeGenType=(size,speed);
{size = default, for 8088. Generate smallest deltas possible.
speed = enforces faster code for 8086 platforms at the expense of size}
const
codeGenMax=10; {in bytes}
optimizeCode:codeGenType=size;
(* DRAM refresh enabled: *)
REPMOVSWcycleCost=38.4; {one iteration of REP MOVSW}
REPSTOSWcycleCost=27.4; {one iteration of REP STOSW}
lastAL:word=0; {we can cache AX because our runs are sorted by value}
ALcached:boolean=false;
frameHeaderLen=14;
frameHeaderCode:array[0..frameHeaderLen-1] of byte=(
$1E, {PUSH DS }
$0E, {PUSH CS }
$1F, {POP DS }
$BE,$FF,$FF, {MOV SI,FFFF}
$B8,$00,$B8, {MOV AX,B800}
$8E,$C0, {MOV ES,AX }
$B5,$00, {MOV CH,00 }
$FC {CLD }
);
frameHeaderCycles=18*4;
frameFooterLen=2;
frameFooterCode:array[0..frameFooterLen-1] of byte=(
$1F, {POP DS }
$CB {RETF }
);
frameFooterCycles=(3*4)+34;
frameByteOverhead=frameHeaderLen+frameFooterLen;
frameCycleOverhead=frameHeaderCycles+frameFooterCycles;
type
opcodetype=record
ob:byte; {opcode byte}
oc:real; {total cycle time (higher value of (IOs*4) vs. execution time}
end;
opcodeList=(
pushDS,
pushCS,
popDS,
movSI,
movAX,
movESAX1,
movESAX2,
cld,
movAL,
movCL,
movCX,
movDI,
repz,
movsb,
movsw,
stosb,
stosw,
retf,
es,
movbpmem1,
movbpmem2,
movwpmem1,
movwpmem2
);
opcodesType=array[opcodeList] of opcodeType;
{Data structure to hold output of delta compilation step for later linking.
"Values" are stored on the stack and, as such, must be word-aligned.}
PEncodeTarget=^TEncodeTarget;
TEncodeTarget=record
codes:array[0..codeGenMax-1] of byte;
numOpcodeBytes:word;
numDataBytes:word;
totalBytes:word;
totalCycles:real; {in cycles, represents TOTAL execution time}
modifiedAL:boolean;
AL:word;
end;
procedure encodeDelta(d:pdelta; var enc:TEncodeTarget);
implementation
uses
xdc_globals,xdc_common;
const
{Order MUST match that of opcodeList!}
opcode:opcodesType=(
{pushDS} (ob:$1e; oc:4*3),
{pushCS} (ob:$0e; oc:4*3),
{popDS} (ob:$1f; oc:4*3),
{movSI} (ob:$be; oc:4*3),
{movAX} (ob:$b8; oc:4*3),
{movESAX1} (ob:$8e; oc:4),
{movESAX2} (ob:$c0; oc:4),
{cld} (ob:$fc; oc:4),
{movAL} (ob:$b0; oc:4*2),
{movCL} (ob:$b1; oc:4*2),
{movCX} (ob:$b9; oc:4*3),
{movDI} (ob:$bf; oc:4*3),
{repz} (ob:$f3; oc:4),
{movsb} (ob:$a4; oc:4+(REPMOVSWcycleCost / 2)),
{movsw} (ob:$a5; oc:4+REPMOVSWcycleCost),
{stosb} (ob:$aa; oc:4+(REPSTOSWcycleCost / 2)),
{stosw} (ob:$ab; oc:4+REPSTOSWcycleCost),
{retf} (ob:$cb; oc:4),
{es} (ob:$26; oc:4),
{movbpmem1} (ob:$c6; oc:0), {total cycles in next part}
{movbpmem2} (ob:$06; oc:4*6), {2 opcode bytes, 3 operand bytes, 1 store}
{movwpmem1} (ob:$c7; oc:0), {total cycles in next part}
{movwpmem2} (ob:$06; oc:4*8) {2 opcode bytes, 4 operand bytes, 2 store}
);
procedure encodeDelta(d:pdelta; var enc:TEncodeTarget);
{Encode to multiple targets; pick best target based on user preference.
We will use this not just
in the encode step, but in the delta optimization step that comes before
actual encoding.}
const
maxUnroll=6;
var
encodes:array[0..4] of TEncodeTarget;
b,numEnc:byte;
opcodepos:byte;
procedure injectOpcode(o:opcodeList);
begin
encodes[numEnc].codes[opcodepos]:=opcode[o].ob;
inc(opcodePos);
inc(encodes[numEnc].numOpcodeBytes);
encodes[numEnc].totalCycles:=encodes[numEnc].totalCycles+opcode[o].oc;
end;
procedure injectWordval(w:word);
begin
encodes[numEnc].codes[opcodepos ]:=lo(w);
encodes[numEnc].codes[opcodepos+1]:=hi(w);
inc(opcodePos,2);
inc(encodes[numEnc].numOpcodeBytes,2);
end;
procedure injectByteval(b:byte);
begin
encodes[numEnc].codes[opcodepos]:=b;
inc(opcodePos);
inc(encodes[numEnc].numOpcodeBytes);
end;
procedure encodeSTOS;
var
loop:byte;
begin
opcodepos:=0;
injectOpcode(movdi); injectWordVal(d^.startofs);
{if AL not cached, set new one}
if (lastAL<>d^.fillvalue) or not ALcached then begin
injectOpcode(moval); injectbyteVal(d^.fillvalue); ALcached:=true;
encodes[numEnc].AL:=d^.fillValue;
encodes[numEnc].modifiedAL:=true;
end;
{inject STOSBs}
for loop:=0 to d^.blength-1 do injectOpcode(stosb);
with encodes[numEnc] do totalbytes:=numOpcodeBytes+numDataBytes;
inc(numEnc);
end;
procedure encodeMOVS;
begin
opcodepos:=0;
injectOpcode(movdi); injectWordVal(d^.startofs);
case d^.blength of
1:begin
injectOpcode(movsb);
end;
2:begin
injectOpcode(movsw);
end;
3:begin
injectOpcode(movsw);
injectOpcode(movsb);
end;
4:begin
injectOpcode(movsw);
injectOpcode(movsw);
end;
5:begin
injectOpcode(movsw);
injectOpcode(movsw);
injectOpcode(movsb);
end;
6:begin
injectOpcode(movsw);
injectOpcode(movsw);
injectOpcode(movsw);
end;
else
fatalerr('cannot unroll MOVS beyond max unroll length');
end;
encodes[numEnc].numDataBytes:=d^.blength;
with encodes[numEnc] do totalbytes:=numOpcodeBytes+numDataBytes;
inc(numEnc);
end;
procedure encodeREPSTOS;
begin
opcodepos:=0;
injectOpcode(movdi); injectWordVal(d^.startofs);
if (d^.blength<=255) then begin
injectOpcode(movcl);
injectByteVal(d^.blength);
end else begin
injectOpcode(movcx);
injectWordVal(d^.blength);
end;
if (lastAL<>d^.fillvalue) or not ALcached then begin
injectOpcode(moval); injectbyteVal(d^.fillvalue); ALcached:=true;
encodes[numEnc].AL:=d^.fillValue;
encodes[numEnc].modifiedAL:=true;
end;
injectOpcode(repz);
injectOpcode(stosb);
encodes[numEnc].totalCycles:=
encodes[numEnc].totalCycles+(d^.blength*(REPSTOSWcycleCost/2));
with encodes[numEnc] do totalbytes:=numOpcodeBytes+numDataBytes;
inc(numEnc);
end;
procedure encodeREPMOVS;
begin
opcodepos:=0;
injectOpcode(movdi); injectWordVal(d^.startofs);
if (d^.blength<=255) then begin
injectOpcode(movcl);
injectByteVal(d^.blength);
end else begin
injectOpcode(movcx);
injectWordVal(d^.blength);
end;
injectOpcode(repz);
injectOpcode(movsb);
encodes[numEnc].totalCycles:=
encodes[numEnc].totalCycles+(d^.blength*(REPMOVSWcycleCost/2));
encodes[numEnc].numDataBytes:=d^.blength;
with encodes[numEnc] do totalbytes:=numOpcodeBytes+numDataBytes;
inc(numEnc);
end;
procedure encodeMOV;
var
loop:byte;
pb:^byte;
pw:^word;
begin
opcodepos:=0;
case d^.blength of
1:begin
injectOpcode(es);
injectOpcode(movbpmem1);
injectOpcode(movbpmem2);
injectWordVal(d^.startofs);
pb:=prevframe;
word(pb):=d^.startofs;
injectByteVal(pb^);
end;
2:begin
injectOpcode(es);
injectOpcode(movwpmem1);
injectOpcode(movwpmem2);
injectWordVal(d^.startofs);
pw:=prevframe;
word(pw):=d^.startofs;
injectWordVal(pw^);
end;
else
fatalerr('no dword pointer on this architecture');
end;
with encodes[numEnc] do totalbytes:=numOpcodeBytes+numDataBytes;
inc(numEnc);
end;
begin
fillchar(Enc,sizeof(Enc),$ff);
fillchar(encodes,sizeof(encodes),0);
for b:=0 to 4 do encodes[b].modifiedAL:=false;
enc.numOpcodeBytes:=maxVChunk;
enc.numDataBytes:=maxVChunk;
enc.totalBytes:=maxVChunk*2;
enc.totalCycles:=totalCyclesPerFrame;
enc.modifiedAL:=false;
numEnc:=0;
{encode in every way that makes sense}
if d^.dtype=run then begin
if d^.blength<=maxUnroll then encodeSTOS;
encodeREPSTOS;
end;
if d^.blength<=maxUnroll then encodeMOVS;
encodeREPMOVS;
if d^.blength < 3 then encodeMOV;
{pick the best encode for our user preferences: size or speed}
for b:=0 to numEnc-1 do begin
if optimizeCode=size then begin
if (encodes[b].totalBytes < Enc.totalBytes)
then Enc:=encodes[b];
end;
if optimizeCode=speed then begin
if (encodes[b].totalCycles < Enc.totalCycles)
then Enc:=encodes[b];
end;
end;
end;
end.