1 | unit UCompilerPascal;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UInstructionReader, UOpcode, UCpu, strutils;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TCompilerPascal }
|
---|
13 |
|
---|
14 | TCompilerPascal = class
|
---|
15 | private
|
---|
16 | OpcodeDefs: TOpcodeDefs;
|
---|
17 | Instructions: array[TOpcode] of TInstructionEvent;
|
---|
18 | Indent: Integer;
|
---|
19 | Labels: TStringList;
|
---|
20 | procedure InstConvert;
|
---|
21 | procedure InstNop;
|
---|
22 | procedure InstHalt;
|
---|
23 | procedure InstLoad;
|
---|
24 | procedure InstLoadImmediate;
|
---|
25 | procedure InstJump;
|
---|
26 | procedure InstJumpZero;
|
---|
27 | procedure InstJumpNotZero;
|
---|
28 | procedure InstJumpRel;
|
---|
29 | procedure InstJumpRelZero;
|
---|
30 | procedure InstJumpRelNotZero;
|
---|
31 | procedure InstTest;
|
---|
32 | procedure InstNeg;
|
---|
33 | procedure InstClear;
|
---|
34 | procedure InstLoadMem;
|
---|
35 | procedure InstStoreMem;
|
---|
36 | procedure InstExchg;
|
---|
37 | procedure InstPush;
|
---|
38 | procedure InstPop;
|
---|
39 | procedure InstCall;
|
---|
40 | procedure InstRet;
|
---|
41 | procedure InstAdd;
|
---|
42 | procedure InstAddi;
|
---|
43 | procedure InstSub;
|
---|
44 | procedure InstSubi;
|
---|
45 | procedure InstMul;
|
---|
46 | procedure InstDiv;
|
---|
47 | procedure InstMod;
|
---|
48 | procedure InstInc;
|
---|
49 | procedure InstDec;
|
---|
50 | procedure InstIn;
|
---|
51 | procedure InstOut;
|
---|
52 | procedure InstShr;
|
---|
53 | procedure InstShl;
|
---|
54 | procedure InstAnd;
|
---|
55 | procedure InstOr;
|
---|
56 | procedure InstXor;
|
---|
57 | procedure InstLddr;
|
---|
58 | procedure InstLdir;
|
---|
59 | procedure InstDataPrefix8;
|
---|
60 | procedure InstDataPrefix16;
|
---|
61 | procedure InstDataPrefix32;
|
---|
62 | procedure InstDataPrefix64;
|
---|
63 | procedure InstDataSize;
|
---|
64 | procedure InstAddrPrefix8;
|
---|
65 | procedure InstAddrPrefix16;
|
---|
66 | procedure InstAddrPrefix32;
|
---|
67 | procedure InstAddrPrefix64;
|
---|
68 | procedure InstAddrSize;
|
---|
69 | procedure InitInstructions;
|
---|
70 | procedure AddLine(Text: string);
|
---|
71 | function GetRegister(Index: Byte; BitWidth: TBitWidth): string;
|
---|
72 | function GetMemory(Offset: string; BitWidth: TBitWidth): string;
|
---|
73 | function GetJump(LabelName: string): string;
|
---|
74 | public
|
---|
75 | Reader: TInstructionReader;
|
---|
76 | Output: string;
|
---|
77 | procedure Compile;
|
---|
78 | constructor Create;
|
---|
79 | destructor Destroy; override;
|
---|
80 | end;
|
---|
81 |
|
---|
82 |
|
---|
83 | implementation
|
---|
84 |
|
---|
85 | const
|
---|
86 | BitWidthSuffix: array[TBitWidth] of string = ('', '.B', '.W', '.D', '.Q');
|
---|
87 | BitWidthPtrType: array[TBitWidth] of string = ('', 'PByte', 'PWord', 'PDWord', 'PQWord');
|
---|
88 |
|
---|
89 | { TCompilerPascal }
|
---|
90 |
|
---|
91 | procedure TCompilerPascal.InstConvert;
|
---|
92 | var
|
---|
93 | R: Byte;
|
---|
94 | begin
|
---|
95 | R := Reader.Read8;
|
---|
96 | AddLine(GetRegister(R, Reader.AddrSize) + ' := ' + GetRegister(R, Reader.DataSize) + ';');
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TCompilerPascal.InstNop;
|
---|
100 | begin
|
---|
101 | end;
|
---|
102 |
|
---|
103 | procedure TCompilerPascal.InstHalt;
|
---|
104 | begin
|
---|
105 | AddLine('Exit;');
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TCompilerPascal.InstLoad;
|
---|
109 | var
|
---|
110 | R1, R2: Byte;
|
---|
111 | begin
|
---|
112 | R1 := Reader.Read8;
|
---|
113 | R2 := Reader.Read8;
|
---|
114 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R2, Reader.DataSize) + ';');
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TCompilerPascal.InstLoadImmediate;
|
---|
118 | var
|
---|
119 | R: Byte;
|
---|
120 | begin
|
---|
121 | R := Reader.Read8;
|
---|
122 | AddLine(GetRegister(R, Reader.DataSize) + ' := ' + IntToHexEx(Reader.ReadData, -1, '$') + ';');
|
---|
123 | end;
|
---|
124 |
|
---|
125 | procedure TCompilerPascal.InstJump;
|
---|
126 | begin
|
---|
127 | AddLine(GetJump('L' + IntToStr(Reader.ReadAddress)));
|
---|
128 | end;
|
---|
129 |
|
---|
130 | procedure TCompilerPascal.InstJumpZero;
|
---|
131 | begin
|
---|
132 | AddLine('if Zero then ' + GetJump('L' + IntToStr(Reader.ReadAddress)));
|
---|
133 | end;
|
---|
134 |
|
---|
135 | procedure TCompilerPascal.InstJumpNotZero;
|
---|
136 | begin
|
---|
137 | AddLine('if not Zero then ' + GetJump('L' + IntToStr(Reader.ReadAddress)));
|
---|
138 | end;
|
---|
139 |
|
---|
140 | procedure TCompilerPascal.InstJumpRel;
|
---|
141 | var
|
---|
142 | Addr: TAddressSigned;
|
---|
143 | begin
|
---|
144 | Addr := Reader.ReadAddressSigned;
|
---|
145 | AddLine(GetJump('L' + IntToStr(Reader.IP + Addr)));
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TCompilerPascal.InstJumpRelZero;
|
---|
149 | var
|
---|
150 | Addr: TAddressSigned;
|
---|
151 | begin
|
---|
152 | Addr := Reader.ReadAddressSigned;
|
---|
153 | AddLine('if Zero then ' + GetJump('L' + IntToStr(Reader.IP + Addr)));
|
---|
154 | end;
|
---|
155 |
|
---|
156 | procedure TCompilerPascal.InstJumpRelNotZero;
|
---|
157 | var
|
---|
158 | Addr: TAddressSigned;
|
---|
159 | begin
|
---|
160 | Addr := Reader.ReadAddressSigned;
|
---|
161 | AddLine('if not Zero then ' + GetJump('L' + IntToStr(Reader.IP + Addr)));
|
---|
162 | end;
|
---|
163 |
|
---|
164 | procedure TCompilerPascal.InstTest;
|
---|
165 | var
|
---|
166 | R: TRegIndex;
|
---|
167 | begin
|
---|
168 | R := Reader.Read8;
|
---|
169 | AddLine('Zero := ' + GetRegister(R, Reader.DataSize) + ' = 0;');
|
---|
170 | end;
|
---|
171 |
|
---|
172 | procedure TCompilerPascal.InstNeg;
|
---|
173 | var
|
---|
174 | R: TRegIndex;
|
---|
175 | begin
|
---|
176 | R := Reader.Read8;
|
---|
177 | AddLine(GetRegister(R, Reader.DataSize) + ' := -' + GetRegister(R, Reader.DataSize) + ';');
|
---|
178 | end;
|
---|
179 |
|
---|
180 | procedure TCompilerPascal.InstClear;
|
---|
181 | var
|
---|
182 | R: TRegIndex;
|
---|
183 | begin
|
---|
184 | R := Reader.Read8;
|
---|
185 | AddLine(GetRegister(R, Reader.DataSize) + ' := 0;');
|
---|
186 | end;
|
---|
187 |
|
---|
188 | procedure TCompilerPascal.InstLoadMem;
|
---|
189 | var
|
---|
190 | R1, R2: Byte;
|
---|
191 | begin
|
---|
192 | R1 := Reader.Read8;
|
---|
193 | R2 := Reader.Read8;
|
---|
194 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' +
|
---|
195 | GetMemory(GetRegister(R2, Reader.AddrSize), Reader.AddrSize) + ';');
|
---|
196 | end;
|
---|
197 |
|
---|
198 | procedure TCompilerPascal.InstStoreMem;
|
---|
199 | var
|
---|
200 | R1, R2: Byte;
|
---|
201 | begin
|
---|
202 | R1 := Reader.Read8;
|
---|
203 | R2 := Reader.Read8;
|
---|
204 | AddLine(GetMemory(GetRegister(R1, Reader.AddrSize), Reader.AddrSize) + ' := ' +
|
---|
205 | GetRegister(R2, Reader.DataSize) + ';');
|
---|
206 | end;
|
---|
207 |
|
---|
208 | procedure TCompilerPascal.InstExchg;
|
---|
209 | var
|
---|
210 | R1, R2: Byte;
|
---|
211 | begin
|
---|
212 | R1 := Reader.Read8;
|
---|
213 | R2 := Reader.Read8;
|
---|
214 | AddLine('Temp := ' + GetRegister(R1, Reader.DataSize) + ';');
|
---|
215 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R2, Reader.DataSize) + ';');
|
---|
216 | AddLine(GetRegister(R2, Reader.DataSize) + ' := Temp;');
|
---|
217 | end;
|
---|
218 |
|
---|
219 | procedure TCompilerPascal.InstPush;
|
---|
220 | var
|
---|
221 | R: Byte;
|
---|
222 | begin
|
---|
223 | R := Reader.Read8;
|
---|
224 | AddLine('Dec(SP, ' + IntToStr(BitWidthBytes[Reader.DataSize]) + ');');
|
---|
225 | AddLine(GetMemory('SP', Reader.AddrSize) + ' := ' + GetRegister(R, Reader.DataSize) + ';');
|
---|
226 | end;
|
---|
227 |
|
---|
228 | procedure TCompilerPascal.InstPop;
|
---|
229 | var
|
---|
230 | R: Byte;
|
---|
231 | begin
|
---|
232 | R := Reader.Read8;
|
---|
233 | AddLine(GetRegister(R, Reader.DataSize) + ' := ' + GetMemory('SP', Reader.AddrSize) + ';');
|
---|
234 | AddLine('Inc(SP, ' + IntToStr(BitWidthBytes[Reader.DataSize]) + ');');
|
---|
235 | end;
|
---|
236 |
|
---|
237 | procedure TCompilerPascal.InstCall;
|
---|
238 | var
|
---|
239 | Addr: QWord;
|
---|
240 | begin
|
---|
241 | Addr := Reader.ReadAddress;
|
---|
242 | AddLine('Dec(SP, ' + IntToStr(BitWidthBytes[Reader.AddrSize]) + ');');
|
---|
243 | AddLine(GetMemory('SP', Reader.AddrSize) + ' := LongWord(@L' + IntToStr(Reader.IP) + ');');
|
---|
244 | AddLine(GetJump('L' + IntToStr(Addr)));
|
---|
245 | end;
|
---|
246 |
|
---|
247 | procedure TCompilerPascal.InstRet;
|
---|
248 | begin
|
---|
249 | AddLine('TempAddr := Pointer(' + GetMemory('SP', Reader.AddrSize) + ');');
|
---|
250 | AddLine('Inc(SP, ' + IntToStr(BitWidthBytes[Reader.AddrSize]) + ');');
|
---|
251 | AddLine(GetJump('TempAddr'));
|
---|
252 | end;
|
---|
253 |
|
---|
254 | procedure TCompilerPascal.InstAdd;
|
---|
255 | var
|
---|
256 | R1, R2: Byte;
|
---|
257 | begin
|
---|
258 | R1 := Reader.Read8;
|
---|
259 | R2 := Reader.Read8;
|
---|
260 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R1, Reader.DataSize) +
|
---|
261 | ' + ' + GetRegister(R2, Reader.DataSize));
|
---|
262 | end;
|
---|
263 |
|
---|
264 | procedure TCompilerPascal.InstAddi;
|
---|
265 | var
|
---|
266 | R: Byte;
|
---|
267 | begin
|
---|
268 | R := Reader.Read8;
|
---|
269 | AddLine(GetRegister(R, Reader.DataSize) + ' := ' + GetRegister(R, Reader.DataSize) +
|
---|
270 | ' + ' + IntToStr(Reader.ReadData));
|
---|
271 | end;
|
---|
272 |
|
---|
273 | procedure TCompilerPascal.InstSub;
|
---|
274 | var
|
---|
275 | R1, R2: Byte;
|
---|
276 | begin
|
---|
277 | R1 := Reader.Read8;
|
---|
278 | R2 := Reader.Read8;
|
---|
279 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R1, Reader.DataSize) +
|
---|
280 | ' - ' + GetRegister(R2, Reader.DataSize));
|
---|
281 | end;
|
---|
282 |
|
---|
283 | procedure TCompilerPascal.InstSubi;
|
---|
284 | var
|
---|
285 | R: Byte;
|
---|
286 | begin
|
---|
287 | R := Reader.Read8;
|
---|
288 | AddLine(GetRegister(R, Reader.DataSize) + ' := ' + GetRegister(R, Reader.DataSize) +
|
---|
289 | ' - ' + IntToStr(Reader.ReadData));
|
---|
290 | end;
|
---|
291 |
|
---|
292 | procedure TCompilerPascal.InstMul;
|
---|
293 | var
|
---|
294 | R1, R2: Byte;
|
---|
295 | begin
|
---|
296 | R1 := Reader.Read8;
|
---|
297 | R2 := Reader.Read8;
|
---|
298 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R1, Reader.DataSize) +
|
---|
299 | ' * ' + GetRegister(R2, Reader.DataSize));
|
---|
300 | end;
|
---|
301 |
|
---|
302 | procedure TCompilerPascal.InstDiv;
|
---|
303 | var
|
---|
304 | R1, R2: Byte;
|
---|
305 | begin
|
---|
306 | R1 := Reader.Read8;
|
---|
307 | R2 := Reader.Read8;
|
---|
308 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R1, Reader.DataSize) +
|
---|
309 | ' div ' + GetRegister(R2, Reader.DataSize));
|
---|
310 | end;
|
---|
311 |
|
---|
312 | procedure TCompilerPascal.InstMod;
|
---|
313 | var
|
---|
314 | R1, R2: Byte;
|
---|
315 | begin
|
---|
316 | R1 := Reader.Read8;
|
---|
317 | R2 := Reader.Read8;
|
---|
318 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R1, Reader.DataSize) +
|
---|
319 | ' mod ' + GetRegister(R2, Reader.DataSize));
|
---|
320 | end;
|
---|
321 |
|
---|
322 | procedure TCompilerPascal.InstInc;
|
---|
323 | var
|
---|
324 | R: Byte;
|
---|
325 | begin
|
---|
326 | R := Reader.Read8;
|
---|
327 | AddLine('Inc(' + GetRegister(R, Reader.DataSize) + ');');
|
---|
328 | end;
|
---|
329 |
|
---|
330 | procedure TCompilerPascal.InstDec;
|
---|
331 | var
|
---|
332 | R: Byte;
|
---|
333 | begin
|
---|
334 | R := Reader.Read8;
|
---|
335 | AddLine('Dec(' + GetRegister(R, Reader.DataSize) + ');');
|
---|
336 | end;
|
---|
337 |
|
---|
338 | procedure TCompilerPascal.InstIn;
|
---|
339 | var
|
---|
340 | R: TRegIndex;
|
---|
341 | Port: TAddress;
|
---|
342 | begin
|
---|
343 | R := Reader.Read8;
|
---|
344 | Port := Reader.ReadAddress;
|
---|
345 | // TODO
|
---|
346 | end;
|
---|
347 |
|
---|
348 | procedure TCompilerPascal.InstOut;
|
---|
349 | var
|
---|
350 | R: TRegIndex;
|
---|
351 | Port: TAddress;
|
---|
352 | begin
|
---|
353 | Port := Reader.ReadAddress;
|
---|
354 | R := Reader.Read8;
|
---|
355 | // TODO
|
---|
356 | end;
|
---|
357 |
|
---|
358 | procedure TCompilerPascal.InstShr;
|
---|
359 | var
|
---|
360 | R: Byte;
|
---|
361 | begin
|
---|
362 | R := Reader.Read8;
|
---|
363 | AddLine(GetRegister(R, Reader.DataSize) + ' := ' + GetRegister(R, Reader.DataSize) +
|
---|
364 | ' shr ' + IntToStr(Reader.ReadData));
|
---|
365 | end;
|
---|
366 |
|
---|
367 | procedure TCompilerPascal.InstShl;
|
---|
368 | var
|
---|
369 | R: Byte;
|
---|
370 | begin
|
---|
371 | R := Reader.Read8;
|
---|
372 | AddLine(GetRegister(R, Reader.DataSize) + ' := ' + GetRegister(R, Reader.DataSize) +
|
---|
373 | ' shl ' + IntToStr(Reader.ReadData));
|
---|
374 | end;
|
---|
375 |
|
---|
376 | procedure TCompilerPascal.InstAnd;
|
---|
377 | var
|
---|
378 | R1, R2: Byte;
|
---|
379 | begin
|
---|
380 | R1 := Reader.Read8;
|
---|
381 | R2 := Reader.Read8;
|
---|
382 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R1, Reader.DataSize) +
|
---|
383 | ' and ' + GetRegister(R2, Reader.DataSize));
|
---|
384 | end;
|
---|
385 |
|
---|
386 | procedure TCompilerPascal.InstOr;
|
---|
387 | var
|
---|
388 | R1, R2: Byte;
|
---|
389 | begin
|
---|
390 | R1 := Reader.Read8;
|
---|
391 | R2 := Reader.Read8;
|
---|
392 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R1, Reader.DataSize) +
|
---|
393 | ' or ' + GetRegister(R2, Reader.DataSize));
|
---|
394 | end;
|
---|
395 |
|
---|
396 | procedure TCompilerPascal.InstXor;
|
---|
397 | var
|
---|
398 | R1, R2: Byte;
|
---|
399 | begin
|
---|
400 | R1 := Reader.Read8;
|
---|
401 | R2 := Reader.Read8;
|
---|
402 | AddLine(GetRegister(R1, Reader.DataSize) + ' := ' + GetRegister(R1, Reader.DataSize) +
|
---|
403 | ' xor ' + GetRegister(R2, Reader.DataSize));
|
---|
404 | end;
|
---|
405 |
|
---|
406 | procedure TCompilerPascal.InstLddr;
|
---|
407 | begin
|
---|
408 | // TODO
|
---|
409 | end;
|
---|
410 |
|
---|
411 | procedure TCompilerPascal.InstLdir;
|
---|
412 | begin
|
---|
413 | // TODO
|
---|
414 | end;
|
---|
415 |
|
---|
416 | procedure TCompilerPascal.InstDataPrefix8;
|
---|
417 | begin
|
---|
418 | Reader.Prefix := True;
|
---|
419 | Reader.DataSize := bw8;
|
---|
420 | end;
|
---|
421 |
|
---|
422 | procedure TCompilerPascal.InstDataPrefix16;
|
---|
423 | begin
|
---|
424 | Reader.Prefix := True;
|
---|
425 | Reader.DataSize := bw16;
|
---|
426 | end;
|
---|
427 |
|
---|
428 | procedure TCompilerPascal.InstDataPrefix32;
|
---|
429 | begin
|
---|
430 | Reader.Prefix := True;
|
---|
431 | Reader.DataSize := bw32;
|
---|
432 | end;
|
---|
433 |
|
---|
434 | procedure TCompilerPascal.InstDataPrefix64;
|
---|
435 | begin
|
---|
436 | Reader.Prefix := True;
|
---|
437 | Reader.DataSize := bw64;
|
---|
438 | end;
|
---|
439 |
|
---|
440 | procedure TCompilerPascal.InstDataSize;
|
---|
441 | var
|
---|
442 | BitWidth: Byte;
|
---|
443 | begin
|
---|
444 | BitWidth := Reader.Read8;
|
---|
445 | if (BitWidth <> 0) and (BitWidth <= Byte(High(TBitWidth))) then begin
|
---|
446 | Reader.DataSizeBase := TBitWidth(BitWidth);
|
---|
447 | Reader.DataSize := Reader.DataSizeBase;
|
---|
448 | end;
|
---|
449 | end;
|
---|
450 |
|
---|
451 | procedure TCompilerPascal.InstAddrPrefix8;
|
---|
452 | begin
|
---|
453 | Reader.Prefix := True;
|
---|
454 | Reader.AddrSize := bw8;
|
---|
455 | end;
|
---|
456 |
|
---|
457 | procedure TCompilerPascal.InstAddrPrefix16;
|
---|
458 | begin
|
---|
459 | Reader.Prefix := True;
|
---|
460 | Reader.AddrSize := bw16;
|
---|
461 | end;
|
---|
462 |
|
---|
463 | procedure TCompilerPascal.InstAddrPrefix32;
|
---|
464 | begin
|
---|
465 | Reader.Prefix := True;
|
---|
466 | Reader.AddrSize := bw32;
|
---|
467 | end;
|
---|
468 |
|
---|
469 | procedure TCompilerPascal.InstAddrPrefix64;
|
---|
470 | begin
|
---|
471 | Reader.Prefix := True;
|
---|
472 | Reader.AddrSize := bw64;
|
---|
473 | end;
|
---|
474 |
|
---|
475 | procedure TCompilerPascal.InstAddrSize;
|
---|
476 | var
|
---|
477 | BitWidth: Byte;
|
---|
478 | begin
|
---|
479 | BitWidth := Reader.Read8;
|
---|
480 | if (BitWidth <> 0) and (BitWidth <= Byte(High(TBitWidth))) then begin
|
---|
481 | Reader.AddrSizeBase := TBitWidth(BitWidth);
|
---|
482 | Reader.AddrSize := Reader.AddrSizeBase;
|
---|
483 | end; // TODO error reporting
|
---|
484 | end;
|
---|
485 |
|
---|
486 | procedure TCompilerPascal.InitInstructions;
|
---|
487 | begin
|
---|
488 | Instructions[opNop] := InstNop;
|
---|
489 | Instructions[opHalt] := InstHalt;
|
---|
490 | Instructions[opLoad] := InstLoad;
|
---|
491 | Instructions[opLoadi] := InstLoadImmediate;
|
---|
492 | Instructions[opJump] := InstJump;
|
---|
493 | Instructions[opJumpNotZero] := InstJumpNotZero;
|
---|
494 | Instructions[opJumpZero] := InstJumpZero;
|
---|
495 | Instructions[opJumpRel] := InstJumpRel;
|
---|
496 | Instructions[opJumpRelNotZero] := InstJumpRelNotZero;
|
---|
497 | Instructions[opJumpRelZero] := InstJumpRelZero;
|
---|
498 | Instructions[opLoadMem] := InstLoadMem;
|
---|
499 | Instructions[opStoreMem] := InstStoreMem;
|
---|
500 | Instructions[opClear] := InstClear;
|
---|
501 | Instructions[opNeg] := InstNeg;
|
---|
502 | Instructions[opExchg] := InstExchg;
|
---|
503 | Instructions[opPop] := InstPop;
|
---|
504 | Instructions[opPush] := InstPush;
|
---|
505 | Instructions[opCall] := InstCall;
|
---|
506 | Instructions[opRet] := InstRet;
|
---|
507 | Instructions[opAdd] := InstAdd;
|
---|
508 | Instructions[opAddi] := InstAddi;
|
---|
509 | Instructions[opSub] := InstSub;
|
---|
510 | Instructions[opSubi] := InstSubi;
|
---|
511 | Instructions[opInc] := InstInc;
|
---|
512 | Instructions[opDec] := InstDec;
|
---|
513 | Instructions[opIn] := InstIn;
|
---|
514 | Instructions[opOut] := InstOut;
|
---|
515 | Instructions[opShr] := InstShr;
|
---|
516 | Instructions[opShl] := InstShl;
|
---|
517 | Instructions[opDataSize] := InstDataSize;
|
---|
518 | Instructions[opDataPrefix8] := InstDataPrefix8;
|
---|
519 | Instructions[opDataPrefix16] := InstDataPrefix16;
|
---|
520 | Instructions[opDataPrefix32] := InstDataPrefix32;
|
---|
521 | Instructions[opDataPrefix64] := InstDataPrefix64;
|
---|
522 | Instructions[opAddrSize] := InstAddrSize;
|
---|
523 | Instructions[opAddrPrefix8] := InstAddrPrefix8;
|
---|
524 | Instructions[opAddrPrefix16] := InstAddrPrefix16;
|
---|
525 | Instructions[opAddrPrefix32] := InstAddrPrefix32;
|
---|
526 | Instructions[opAddrPrefix64] := InstAddrPrefix64;
|
---|
527 | Instructions[opTest] := InstTest;
|
---|
528 | Instructions[opAnd] := InstAnd;
|
---|
529 | Instructions[opOr] := InstOr;
|
---|
530 | Instructions[opXor] := InstXor;
|
---|
531 | Instructions[opLdir] := InstLdir;
|
---|
532 | Instructions[opLddr] := InstLddr;
|
---|
533 | Instructions[opMul] := InstMul;
|
---|
534 | Instructions[opDiv] := InstDiv;
|
---|
535 | Instructions[opMod] := InstMod;
|
---|
536 | Instructions[opConvert] := InstConvert;
|
---|
537 | end;
|
---|
538 |
|
---|
539 | procedure TCompilerPascal.AddLine(Text: string);
|
---|
540 | begin
|
---|
541 | Output := Output + DupeString(' ', Indent) + Text + LineEnding;
|
---|
542 | end;
|
---|
543 |
|
---|
544 | function TCompilerPascal.GetRegister(Index: Byte; BitWidth: TBitWidth): string;
|
---|
545 | begin
|
---|
546 | Result := 'R' + IntToStr(Index) + BitWidthSuffix[BitWidth];
|
---|
547 | end;
|
---|
548 |
|
---|
549 | function TCompilerPascal.GetMemory(Offset: string; BitWidth: TBitWidth): string;
|
---|
550 | begin
|
---|
551 | Result := BitWidthPtrType[BitWidth] + '(Memory + ' + Offset + ')^';
|
---|
552 | end;
|
---|
553 |
|
---|
554 | function TCompilerPascal.GetJump(LabelName: string): string;
|
---|
555 | begin
|
---|
556 | Result := 'asm jmp ' + LabelName + ' end;';
|
---|
557 | end;
|
---|
558 |
|
---|
559 | procedure TCompilerPascal.Compile;
|
---|
560 | var
|
---|
561 | Opcode: Byte;
|
---|
562 | MemorySize: QWord;
|
---|
563 | OutputBody: string;
|
---|
564 | LabelsText: string;
|
---|
565 | LabelAddr: QWord;
|
---|
566 | I: Integer;
|
---|
567 | begin
|
---|
568 | Reader.Init;
|
---|
569 | MemorySize := MemSize(Reader.Cpu.Memory);
|
---|
570 | AddLine('begin');
|
---|
571 | AddLine(' SP := ' + IntToStr(MemorySize) + ';');
|
---|
572 | AddLine(' Memory := GetMem(' + IntToStr(MemorySize) + ');');
|
---|
573 | Inc(Indent);
|
---|
574 | while Reader.IP < MemorySize do begin
|
---|
575 | Reader.Prefix := False;
|
---|
576 | LabelAddr := Reader.IP;
|
---|
577 | Opcode := Reader.Read8;
|
---|
578 | if Opcode <= Integer(High(TOpcode)) then begin
|
---|
579 | AddLine('L' + IntToStr(LabelAddr) + ':');
|
---|
580 | Labels.Add('L' + IntToStr(LabelAddr));
|
---|
581 | if Opcode < Length(Instructions) then begin
|
---|
582 | if Assigned(Instructions[TOpcode(Opcode)]) then Instructions[TOpcode(Opcode)]
|
---|
583 | else raise Exception.Create('Missing instruction handler for opcode '+ IntToStr(Opcode));
|
---|
584 | end else raise Exception.Create('Unsupported opcode ' + IntToStr(Opcode) + ' at address ' + IntToHex(Reader.IP - 1, 8) + '.');
|
---|
585 | if not Reader.Prefix then begin
|
---|
586 | Reader.DataSize := Reader.DataSizeBase;
|
---|
587 | Reader.AddrSize := Reader.AddrSizeBase;
|
---|
588 | end;
|
---|
589 | end else begin
|
---|
590 | end;
|
---|
591 | end;
|
---|
592 | Dec(Indent);
|
---|
593 | AddLine('end.');
|
---|
594 | OutputBody := Output;
|
---|
595 |
|
---|
596 | Output := '';
|
---|
597 | AddLine('type');
|
---|
598 | AddLine(' TBitWidth = (bwNone, bw8, bw16, bw32, bw64);');
|
---|
599 | AddLine(' TRegister = record');
|
---|
600 | AddLine(' case TBitWidth of');
|
---|
601 | AddLine(' bw8: (B: Byte);');
|
---|
602 | AddLine(' bw16: (W: Word);');
|
---|
603 | AddLine(' bw32: (D: DWord);');
|
---|
604 | AddLine(' bw64: (Q: QWord);');
|
---|
605 | AddLine(' end;');
|
---|
606 | AddLine('var');
|
---|
607 | AddLine(' Memory: Pointer;');
|
---|
608 | for I := 0 to High(Reader.Cpu.Registers) do
|
---|
609 | AddLine(' R' + IntToStr(I) + ': TRegister;');
|
---|
610 | AddLine(' SP: QWord;');
|
---|
611 | AddLine(' Zero: Boolean;');
|
---|
612 | AddLine(' Temp: QWord;');
|
---|
613 | AddLine(' TempAddr: Pointer;');
|
---|
614 | AddLine('');
|
---|
615 | LabelsText := '';
|
---|
616 | for I := 0 to Labels.Count - 1 do begin
|
---|
617 | if I > 0 then LabelsText := LabelsText + ', ';
|
---|
618 | LabelsText := LabelsText + Labels[I];
|
---|
619 | end;
|
---|
620 | AddLine('label ' + LabelsText + ';');
|
---|
621 | Output := Output + OutputBody;
|
---|
622 | end;
|
---|
623 |
|
---|
624 | constructor TCompilerPascal.Create;
|
---|
625 | begin
|
---|
626 | OpcodeDefs := TOpcodeDefs.Create;
|
---|
627 | Reader := TInstructionReader.Create;
|
---|
628 | Labels := TStringList.Create;
|
---|
629 | InitInstructions;
|
---|
630 | end;
|
---|
631 |
|
---|
632 | destructor TCompilerPascal.Destroy;
|
---|
633 | begin
|
---|
634 | FreeAndNil(Reader);
|
---|
635 | FreeAndNil(OpcodeDefs);
|
---|
636 | FreeAndNil(Labels);
|
---|
637 | inherited Destroy;
|
---|
638 | end;
|
---|
639 |
|
---|
640 | end.
|
---|
641 |
|
---|