raz / commit 942acfeb

commit
942acfebc8b357020574b36f38fcfc7981143c7e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
626e09b7
Step 3: machine code

x64.pas holds the code and rodata buffers, the chunks (one per
procedure, variable and string) and the fixups between them, labels
for jumps within a procedure, and an encoder for the instructions the
code generator needs.

patch · log

1 file changed, +613 -0

src/x64.pas added +613 -0

@@ -0,0 +1,613 @@
1+{ x64.pas: machine code.
2+
3+ The code of a program is a set of chunks: one per procedure, module
4+ variable and string. A chunk's bytes go in one of three sections:
5+ text (code), rodata (strings), bss (variables, which are all zero).
6+ A reference from code to another chunk (a call, or a rip-relative
7+ address) is a fixup, patched once the linker has placed every chunk.
8+ Jumps within a procedure are patched here, as soon as their target
9+ is known.
10+
11+ The encoder writes the instructions the code generator needs, and
12+ no others. Registers are numbered as the hardware numbers them.
13+
14+ Depends on: scan, for errors.
15+ Leaves out: choosing instructions; that is gen.pas. }
16+
17+unit x64;
18+
19+{$mode objfpc}{$H-}
20+
21+interface
22+
23+uses scan;
24+
25+const
26+ RAX = 0; RCX = 1; RDX = 2; RBX = 3; RSP = 4; RBP = 5; RSI = 6; RDI = 7;
27+ R8 = 8; R9 = 9; R10 = 10; R11 = 11;
28+ RIP = -1; { a base meaning rip-relative, to a chunk }
29+ NoIndex = -1;
30+
31+ { condition codes }
32+ ccB = 2; ccAE = 3; ccE = 4; ccNE = 5; ccBE = 6; ccA = 7; ccS = 8;
33+ ccP = 10; ccNP = 11; ccL = 12; ccGE = 13; ccLE = 14; ccG = 15;
34+
35+ { group 1 operations, the /digit of 81 and 83 }
36+ aluAdd = 0; aluOr = 1; aluAnd = 4; aluSub = 5; aluXor = 6; aluCmp = 7;
37+
38+ { the /digit of D3 and C1 }
39+ shShl = 4; shShr = 5; shSar = 7;
40+
41+ NoLabel = -1;
42+
43+type
44+ TSection = (secText, secRodata, secBss);
45+
46+ TMem = record
47+ base: longint; { a register, or RIP }
48+ index: longint; { a register, or NoIndex }
49+ scale: longint; { 1, 2, 4 or 8 }
50+ disp: longint;
51+ chunk: longint; { with RIP: the chunk addressed }
52+ end;
53+
54+ TChunk = record
55+ sec: TSection;
56+ start, size, align: longint; { in its section's buffer }
57+ firstFix, numFix: longint; { its fixups, text chunks only }
58+ module, name: longint; { for the symbol table; -1 if none }
59+ adr: int64; { its address, set by the linker }
60+ live: boolean; { reached from the entry point }
61+ end;
62+
63+ TFixup = record
64+ at: longint; { the rel32 field, as an offset in text }
65+ chunk: longint; { the chunk it refers to }
66+ addend: longint; { added to the chunk's address }
67+ end;
68+
69+ PChunks = ^TChunkArray;
70+ TChunkArray = array[0..MaxInt div SizeOf(TChunk) - 1] of TChunk;
71+ PFixups = ^TFixupArray;
72+ TFixupArray = array[0..MaxInt div SizeOf(TFixup) - 1] of TFixup;
73+
74+var
75+ code: PBytes; { text }
76+ pc: longint; { its length }
77+ rodata: PBytes;
78+ rolen: longint;
79+ chunks: PChunks;
80+ numChunks: longint;
81+ fixups: PFixups;
82+ numFixups: longint;
83+
84+function NewChunk(sec: TSection; module, name: longint): longint;
85+procedure BeginChunk(c: longint);
86+procedure EndChunk(c: longint);
87+procedure SetData(c: longint; p: PBytes; n, align: longint);
88+procedure SetBss(c: longint; size, align: longint);
89+
90+procedure Put1(b: longint);
91+procedure Put4(v: longint);
92+procedure Put4At(at, v: longint);
93+function Get4At(at: longint): longint;
94+
95+function Mem(base: longint; disp: int64): TMem;
96+function MemRip(chunk: longint; disp: longint): TMem;
97+function MemIdx(base, index, scale: longint; disp: int64): TMem;
98+
99+procedure MovRR(dst, src: longint);
100+procedure MovRI(r: longint; v: int64);
101+procedure Load(r: longint; const m: TMem; size: longint; signed: boolean);
102+procedure Store(const m: TMem; r: longint; size: longint);
103+procedure Lea(r: longint; const m: TMem);
104+procedure Alu(op, dst, src: longint);
105+procedure AluI(op, r: longint; v: longint);
106+procedure AluM(op, r: longint; const m: TMem);
107+procedure Imul(dst, src: longint);
108+procedure ImulI(r: longint; v: longint);
109+procedure Shift(op, r: longint);
110+procedure ShiftI(op, r, n: longint);
111+procedure Unary(ext, r: longint); { F7 /ext: 2 not, 3 neg, 6 div, 7 idiv }
112+procedure Cqo;
113+procedure Extend(r, size: longint; signed: boolean);
114+procedure SetCC(cc, r: longint);
115+procedure TestRR(a, b: longint);
116+procedure Push(r: longint);
117+procedure Pop(r: longint);
118+procedure CallChunk(c: longint);
119+procedure CallR(r: longint);
120+procedure Ret;
121+procedure Leave;
122+procedure Syscall;
123+procedure Mfence;
124+procedure Ud2;
125+procedure RepMovsb;
126+procedure RepStosb;
127+procedure LockCmpxchg(const m: TMem; r: longint);
128+procedure Xchg(const m: TMem; r: longint);
129+procedure LockXadd(const m: TMem; r: longint);
130+procedure MovqXR(x, r: longint);
131+procedure MovqRX(r, x: longint);
132+procedure MovsdXM(x: longint; const m: TMem);
133+procedure MovsdMX(const m: TMem; x: longint);
134+procedure Sse(op, x, y: longint); { F2 0F op: 58 add, 59 mul, 5C sub, 5E div }
135+procedure Ucomisd(x, y: longint);
136+procedure Cvtsi2sd(x, r: longint);
137+procedure Cvttsd2si(r, x: longint);
138+procedure Btc(r, bit: longint);
139+
140+procedure Jmp(var L: longint);
141+procedure Jcc(cc: longint; var L: longint);
142+procedure JmpBack(target: longint);
143+procedure JccBack(cc, target: longint);
144+procedure FixLink(L: longint);
145+procedure FixLinkTo(L, target: longint);
146+
147+implementation
148+
149+var
150+ codeCap, roCap, chunkCap, fixCap: longint;
151+
152+{ Buffers and chunks }
153+
154+procedure Room(n: longint);
155+begin
156+ if pc + n > codeCap then begin
157+ codeCap := codeCap * 2 + n;
158+ code := ReAllocMem(code, codeCap);
159+ end;
160+end;
161+
162+procedure Put1(b: longint);
163+begin
164+ if pc + 16 > codeCap then Room(16);
165+ code^[pc] := byte(b);
166+ inc(pc);
167+end;
168+
169+procedure Put4(v: longint);
170+begin
171+ if pc + 16 > codeCap then Room(16);
172+ PLongint(@code^[pc])^ := v;
173+ inc(pc, 4);
174+end;
175+
176+procedure Put4At(at, v: longint);
177+begin
178+ PLongint(@code^[at])^ := v;
179+end;
180+
181+function Get4At(at: longint): longint;
182+begin
183+ Get4At := PLongint(@code^[at])^;
184+end;
185+
186+procedure Put8(v: int64);
187+begin
188+ Put4(longint(v and $FFFFFFFF));
189+ Put4(longint(v shr 32));
190+end;
191+
192+function NewChunk(sec: TSection; module, name: longint): longint;
193+begin
194+ if numChunks = chunkCap then begin
195+ chunkCap := chunkCap * 2;
196+ chunks := ReAllocMem(chunks, chunkCap * SizeOf(TChunk));
197+ end;
198+ FillChar(chunks^[numChunks], SizeOf(TChunk), 0);
199+ chunks^[numChunks].sec := sec;
200+ chunks^[numChunks].module := module;
201+ chunks^[numChunks].name := name;
202+ chunks^[numChunks].align := 1;
203+ chunks^[numChunks].size := -1; { not yet defined }
204+ NewChunk := numChunks;
205+ inc(numChunks);
206+end;
207+
208+procedure BeginChunk(c: longint);
209+begin
210+ { code is aligned to 16 bytes, padded with int3 }
211+ while pc and 15 <> 0 do Put1($CC);
212+ chunks^[c].start := pc;
213+ chunks^[c].align := 16;
214+ chunks^[c].firstFix := numFixups;
215+end;
216+
217+procedure EndChunk(c: longint);
218+begin
219+ chunks^[c].size := pc - chunks^[c].start;
220+ chunks^[c].numFix := numFixups - chunks^[c].firstFix;
221+end;
222+
223+procedure SetData(c: longint; p: PBytes; n, align: longint);
224+begin
225+ if rolen + n > roCap then begin
226+ roCap := roCap * 2 + n;
227+ rodata := ReAllocMem(rodata, roCap);
228+ end;
229+ Move(p^, rodata^[rolen], n);
230+ chunks^[c].start := rolen;
231+ chunks^[c].size := n;
232+ chunks^[c].align := align;
233+ inc(rolen, n);
234+end;
235+
236+procedure SetBss(c: longint; size, align: longint);
237+begin
238+ chunks^[c].size := size;
239+ chunks^[c].align := align;
240+end;
241+
242+procedure AddFixup(at, chunk, addend: longint);
243+begin
244+ if numFixups = fixCap then begin
245+ fixCap := fixCap * 2;
246+ fixups := ReAllocMem(fixups, fixCap * SizeOf(TFixup));
247+ end;
248+ fixups^[numFixups].at := at;
249+ fixups^[numFixups].chunk := chunk;
250+ fixups^[numFixups].addend := addend;
251+ inc(numFixups);
252+end;
253+
254+{ Operands }
255+
256+function Mem(base: longint; disp: int64): TMem;
257+begin
258+ if (disp < low(longint)) or (disp > high(longint)) then Mark('offset too big');
259+ Mem.base := base; Mem.index := NoIndex; Mem.scale := 1;
260+ Mem.disp := disp; Mem.chunk := -1;
261+end;
262+
263+function MemRip(chunk: longint; disp: longint): TMem;
264+begin
265+ MemRip.base := RIP; MemRip.index := NoIndex; MemRip.scale := 1;
266+ MemRip.disp := disp; MemRip.chunk := chunk;
267+end;
268+
269+function MemIdx(base, index, scale: longint; disp: int64): TMem;
270+begin
271+ MemIdx := Mem(base, disp);
272+ MemIdx.index := index;
273+ MemIdx.scale := scale;
274+end;
275+
276+{ Encoding. An instruction is: a legacy prefix, REX, the opcode, ModRM,
277+ SIB and displacement. Opcodes of more than one byte are given with
278+ their first byte in the low eight bits. }
279+
280+procedure Opcode(op: longint);
281+begin
282+ repeat
283+ Put1(op and $FF);
284+ op := op shr 8;
285+ until op = 0;
286+end;
287+
288+procedure Prefix(pfx: longint; w: boolean; reg, index, base: longint; byteReg: boolean);
289+var rex: longint;
290+begin
291+ if pfx <> 0 then Put1(pfx);
292+ rex := 0;
293+ if w then rex := rex or 8;
294+ if reg >= 8 then rex := rex or 4;
295+ if index >= 8 then rex := rex or 2;
296+ if base >= 8 then rex := rex or 1;
297+ { sil and dil exist only with a REX prefix }
298+ if (rex <> 0) or (byteReg and (reg >= 4) and (reg <= 7)) then Put1($40 or rex);
299+end;
300+
301+{ op reg, [mem] }
302+procedure OpM(pfx: longint; w: boolean; op, reg: longint; const m: TMem; byteReg: boolean = false);
303+var md, b, sc: longint;
304+begin
305+ if m.base = RIP then Prefix(pfx, w, reg, NoIndex, 0, byteReg)
306+ else Prefix(pfx, w, reg, m.index, m.base, byteReg);
307+ Opcode(op);
308+ reg := reg and 7;
309+ if m.base = RIP then begin
310+ Put1(reg shl 3 or 5);
311+ { the rip-relative displacement counts from the end of the
312+ instruction, which these instructions end with }
313+ AddFixup(pc, m.chunk, m.disp);
314+ Put4(0);
315+ exit;
316+ end;
317+ b := m.base and 7;
318+ if (m.disp = 0) and (b <> RBP) then md := 0
319+ else if (m.disp >= -128) and (m.disp <= 127) then md := 1
320+ else md := 2;
321+ if (m.index <> NoIndex) or (b = RSP) then begin
322+ Put1(md shl 6 or reg shl 3 or 4);
323+ case m.scale of 1: sc := 0; 2: sc := 1; 4: sc := 2; else sc := 3; end;
324+ if m.index = NoIndex then Put1(sc shl 6 or 4 shl 3 or b)
325+ else Put1(sc shl 6 or (m.index and 7) shl 3 or b);
326+ end else
327+ Put1(md shl 6 or reg shl 3 or b);
328+ if md = 1 then Put1(m.disp)
329+ else if md = 2 then Put4(m.disp);
330+end;
331+
332+{ op reg, rm (both registers) }
333+procedure OpR(pfx: longint; w: boolean; op, reg, rm: longint);
334+begin
335+ Prefix(pfx, w, reg, NoIndex, rm, false);
336+ Opcode(op);
337+ Put1($C0 or (reg and 7) shl 3 or (rm and 7));
338+end;
339+
340+procedure MovRR(dst, src: longint);
341+begin
342+ OpR(0, true, $89, src, dst);
343+end;
344+
345+procedure MovRI(r: longint; v: int64);
346+begin
347+ if (v >= low(longint)) and (v <= high(longint)) then begin
348+ if v = 0 then OpR(0, false, $31, r, r) { xor r32, r32 }
349+ else if v > 0 then begin { mov r32, imm32 }
350+ Prefix(0, false, 0, NoIndex, r, false); Put1($B8 + r and 7); Put4(v)
351+ end else begin
352+ OpR(0, true, $C7, 0, r); Put4(v) { sign-extended imm32 }
353+ end;
354+ end else if (v > 0) and (v <= $FFFFFFFF) then begin
355+ Prefix(0, false, 0, NoIndex, r, false); Put1($B8 + r and 7); Put4(longint(v))
356+ end else begin
357+ Prefix(0, true, 0, NoIndex, r, false); Put1($B8 + r and 7); Put8(v)
358+ end;
359+end;
360+
361+procedure Load(r: longint; const m: TMem; size: longint; signed: boolean);
362+begin
363+ case size of
364+ 1: if signed then OpM(0, true, $BE0F, r, m) else OpM(0, false, $B60F, r, m);
365+ 2: if signed then OpM(0, true, $BF0F, r, m) else OpM(0, false, $B70F, r, m);
366+ 4: if signed then OpM(0, true, $63, r, m) else OpM(0, false, $8B, r, m);
367+ else OpM(0, true, $8B, r, m);
368+ end;
369+end;
370+
371+procedure Store(const m: TMem; r: longint; size: longint);
372+begin
373+ case size of
374+ 1: OpM(0, false, $88, r, m, true);
375+ 2: OpM($66, false, $89, r, m);
376+ 4: OpM(0, false, $89, r, m);
377+ else OpM(0, true, $89, r, m);
378+ end;
379+end;
380+
381+procedure Lea(r: longint; const m: TMem);
382+begin
383+ OpM(0, true, $8D, r, m);
384+end;
385+
386+procedure Alu(op, dst, src: longint);
387+begin
388+ OpR(0, true, op shl 3 or 1, src, dst);
389+end;
390+
391+procedure AluI(op, r: longint; v: longint);
392+begin
393+ if (v >= -128) and (v <= 127) then begin
394+ OpR(0, true, $83, op, r); Put1(v)
395+ end else begin
396+ OpR(0, true, $81, op, r); Put4(v)
397+ end;
398+end;
399+
400+{ op r, [m] }
401+procedure AluM(op, r: longint; const m: TMem);
402+begin
403+ OpM(0, true, op shl 3 or 3, r, m);
404+end;
405+
406+procedure Imul(dst, src: longint);
407+begin
408+ OpR(0, true, $AF0F, dst, src);
409+end;
410+
411+procedure ImulI(r: longint; v: longint);
412+begin
413+ OpR(0, true, $69, r, r);
414+ Put4(v);
415+end;
416+
417+procedure Shift(op, r: longint);
418+begin
419+ OpR(0, true, $D3, op, r);
420+end;
421+
422+procedure ShiftI(op, r, n: longint);
423+begin
424+ OpR(0, true, $C1, op, r);
425+ Put1(n);
426+end;
427+
428+procedure Unary(ext, r: longint);
429+begin
430+ OpR(0, true, $F7, ext, r);
431+end;
432+
433+procedure Cqo;
434+begin
435+ Put1($48); Put1($99);
436+end;
437+
438+{ Brings a value of size bytes in r to 64 bits, by its sign or zeros. }
439+procedure Extend(r, size: longint; signed: boolean);
440+begin
441+ case size of
442+ 1: if signed then OpR(0, true, $BE0F, r, r) else OpR(0, false, $B60F, r, r);
443+ 2: if signed then OpR(0, true, $BF0F, r, r) else OpR(0, false, $B70F, r, r);
444+ 4: if signed then OpR(0, true, $63, r, r) else OpR(0, false, $89, r, r);
445+ end;
446+end;
447+
448+{ r := 1 if the condition holds, else 0 }
449+procedure SetCC(cc, r: longint);
450+begin
451+ OpR(0, false, $900F + cc shl 8, 0, r);
452+ OpR(0, false, $B60F, r, r);
453+end;
454+
455+procedure TestRR(a, b: longint);
456+begin
457+ OpR(0, true, $85, b, a);
458+end;
459+
460+procedure Push(r: longint);
461+begin
462+ if r >= 8 then Put1($41);
463+ Put1($50 + r and 7);
464+end;
465+
466+procedure Pop(r: longint);
467+begin
468+ if r >= 8 then Put1($41);
469+ Put1($58 + r and 7);
470+end;
471+
472+procedure CallChunk(c: longint);
473+begin
474+ Put1($E8);
475+ AddFixup(pc, c, 0);
476+ Put4(0);
477+end;
478+
479+procedure CallR(r: longint);
480+begin
481+ OpR(0, false, $FF, 2, r);
482+end;
483+
484+procedure Ret; begin Put1($C3) end;
485+procedure Leave; begin Put1($C9) end;
486+procedure Syscall; begin Put1($0F); Put1($05) end;
487+procedure Mfence; begin Put1($0F); Put1($AE); Put1($F0) end;
488+procedure Ud2; begin Put1($0F); Put1($0B) end;
489+procedure RepMovsb; begin Put1($F3); Put1($A4) end;
490+procedure RepStosb; begin Put1($F3); Put1($AA) end;
491+
492+procedure LockCmpxchg(const m: TMem; r: longint);
493+begin
494+ Put1($F0);
495+ OpM(0, true, $B10F, r, m);
496+end;
497+
498+procedure Xchg(const m: TMem; r: longint);
499+begin
500+ OpM(0, true, $87, r, m);
501+end;
502+
503+procedure LockXadd(const m: TMem; r: longint);
504+begin
505+ Put1($F0);
506+ OpM(0, true, $C10F, r, m);
507+end;
508+
509+procedure MovqXR(x, r: longint);
510+begin
511+ OpR($66, true, $6E0F, x, r);
512+end;
513+
514+procedure MovqRX(r, x: longint);
515+begin
516+ OpR($66, true, $7E0F, x, r);
517+end;
518+
519+procedure MovsdXM(x: longint; const m: TMem);
520+begin
521+ OpM($F2, false, $100F, x, m);
522+end;
523+
524+procedure MovsdMX(const m: TMem; x: longint);
525+begin
526+ OpM($F2, false, $110F, x, m);
527+end;
528+
529+procedure Sse(op, x, y: longint);
530+begin
531+ OpR($F2, false, $0F or op shl 8, x, y);
532+end;
533+
534+procedure Ucomisd(x, y: longint);
535+begin
536+ OpR($66, false, $2E0F, x, y);
537+end;
538+
539+procedure Cvtsi2sd(x, r: longint);
540+begin
541+ OpR($F2, true, $2A0F, x, r);
542+end;
543+
544+procedure Cvttsd2si(r, x: longint);
545+begin
546+ OpR($F2, true, $2C0F, r, x);
547+end;
548+
549+procedure Btc(r, bit: longint);
550+begin
551+ OpR(0, true, $BA0F, 7, r);
552+ Put1(bit);
553+end;
554+
555+{ Jumps. A label not yet placed is the chain of the jumps to it, each
556+ rel32 field holding the position of the one before, NoLabel ending
557+ it. Placing the label walks the chain and patches each. }
558+
559+procedure Jmp(var L: longint);
560+begin
561+ Put1($E9);
562+ Put4(L);
563+ L := pc - 4;
564+end;
565+
566+procedure Jcc(cc: longint; var L: longint);
567+begin
568+ Put1($0F); Put1($80 + cc);
569+ Put4(L);
570+ L := pc - 4;
571+end;
572+
573+procedure JmpBack(target: longint);
574+begin
575+ Put1($E9);
576+ Put4(target - (pc + 4));
577+end;
578+
579+procedure JccBack(cc, target: longint);
580+begin
581+ Put1($0F); Put1($80 + cc);
582+ Put4(target - (pc + 4));
583+end;
584+
585+procedure FixLinkTo(L, target: longint);
586+var next: longint;
587+begin
588+ while L <> NoLabel do begin
589+ next := Get4At(L);
590+ Put4At(L, target - (L + 4));
591+ L := next;
592+ end;
593+end;
594+
595+procedure FixLink(L: longint);
596+begin
597+ FixLinkTo(L, pc);
598+end;
599+
600+initialization
601+ codeCap := 1 shl 20;
602+ code := GetMem(codeCap);
603+ pc := 0;
604+ roCap := 1 shl 16;
605+ rodata := GetMem(roCap);
606+ rolen := 0;
607+ chunkCap := 1 shl 10;
608+ chunks := GetMem(chunkCap * SizeOf(TChunk));
609+ numChunks := 0;
610+ fixCap := 1 shl 12;
611+ fixups := GetMem(fixCap * SizeOf(TFixup));
612+ numFixups := 0;
613+end.