raz / commit 3e451d8e

commit
3e451d8e02ebc39e4b1e5aced5f4c0885ce3138e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
6c50d7c8
Slices in two registers, arguments on the stack

A slice goes as its two words, in two registers or both on the stack,
as System V passes a struct of two integers, not as the address of a
copy: the procedure keeps them in its frame, and s[i] is one load the
fewer. Arguments no longer wait in frame slots: they stay on the value
stack where computing them left them, constants, addresses and
register variables only noted, and Call moves each into its register
(ArgTo), r8 and r9 last, through rax, as they may hold others. A call
in a later argument spills the earlier ones, as it spills any value
under it, which keeps the order of evaluation. PopArg, LoadArg and the
argument slots go. rat on 26 MB: 668 ms to 634, its text -0.8%.
8,390 lines of code.

patch · log

5 files changed, +107 -88

doc/compiler.md (history) modified +11 -7

@@ -13,8 +13,8 @@
1313 | `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes |
1515 | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,611 | 1,263 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17-| `src/item.pas` | 1,433 | 1,194 | Items: types, constants, variables, operators, conversions, calls |
16+| `src/gen.pas` | 1,616 | 1,270 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17+| `src/item.pas` | 1,444 | 1,204 | Items: types, constants, variables, operators, conversions, calls |
1818 | `src/parse.pas` | 2,904 | 2,575 | Declarations, statements, expressions, built-ins, recovery, warnings |
1919 | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers |
@@ -344,11 +344,15 @@
344344 own `gen.pas`: `Rt.fail` stops for a nil pointer, a division by zero
345345 and a stack overflow as the signals would.
346346
347-Calls: the arguments wait for `Call`, computed left to right: a
348-constant or a noted address as itself, anything else in a frame slot
349-(`PopArg`), so that computing a later one can't disturb it; only then
350-are they loaded into the registers System V wants (`LoadArg`), and the
351-stack aligned for the call. A record
347+Calls: the arguments are computed left to right onto the stack, where
348+they wait for `Call`, as any values do: a constant, an address or a
349+variable in a register only noted, the rest in registers, or spilled
350+by a call in a later argument, so that it can't disturb them. `Call`
351+spills what is under them, moves each into the register System V
352+wants (`ArgTo`), `r8` and `r9` last since they may hold others, and
353+aligns the stack. A slice goes as its two words, in two registers or
354+both on the stack, as System V passes a struct of two integers, and
355+the procedure keeps them in its frame. A record
352356 passed by value goes as the address of a copy; one returned comes back
353357 through a hidden pointer in `rdi`, as C's structs do, and so do several
354358 results (`ResultTo`, `CopyResult`).

src/gen.pas modified +59 -54

@@ -16,12 +16,11 @@
1616 needs it in a register, so that it can be an instruction's operand.
1717
1818 Calls use the System V convention. Arguments are computed left to
19- right into frame slots, then loaded into registers just before the
20- call, so that computing one argument can't disturb another; a
21- constant or an address, which nothing can disturb, goes straight to
22- its register. A record,
23- array, slice or variant passed by value goes as the address of a copy
24- the caller makes; one returned goes through a hidden pointer, as C's
19+ right onto the stack, and moved into their registers just before the
20+ call: a call in a later argument spills the earlier ones, as any call
21+ spills the values under it, so that it can't disturb them. A slice goes as its two words, as System V passes a
22+ 16-byte struct of two integers; a record, array or variant passed by
23+ value goes as the address of a copy the caller makes; one returned goes through a hidden pointer, as C's
2524 structs do, and so do several results.
2625
2726 A procedure's frame holds its parameters, its variables, and the
@@ -62,10 +61,6 @@
6261 chunk: longint; { bGlobal: the chunk; bSlot: the frame slot; bReg: the register }
6362 off: int64; { added to the address }
6463 end;
65-
66- { How an argument waits for its call: in a frame slot, or noted, as a
67- constant or as the address of the frame or a chunk plus an offset }
68- TArgHow = (argSlot, argConst, argAdr, argHeld);
6964
7065 { The code made so far, to go back to for a second try at a body }
7166 TCodeMark = record pc, fixups, lines, said: longint; end;
@@ -75,10 +70,8 @@
7570 param: PObj; { the parameter the next argument is for }
7671 obj: PObj; { the procedure called, if it has a name }
7772 n: longint; { the arguments }
78- slots: array[0..63] of int64; { each one's frame slot, constant or offset }
79- how: array[0..63] of TArgHow;
80- adrChunk: array[0..63] of longint; { argAdr: the chunk, or -1 for the frame }
8173 isF: array[0..63] of boolean; { ... goes in an xmm register }
74+ pair: array[0..63] of boolean; { ... is a slice's first word, the next its second }
8275 target: int64; { the frame slot of a procedure value, if chunk < 0 }
8376 chunk: longint; { else the procedure's chunk }
8477 code: boolean; { a code procedure: two results come in rax and rdx }
@@ -152,7 +145,6 @@
152145 procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos);
153146 procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos);
154147
155-procedure PopArg(var c: TCall);
156148 procedure Call(var c: TCall);
157149
158150 function AllocTemp(size, align: int64): int64;
@@ -1172,46 +1164,27 @@
11721164 { The call c, its arguments already in their frame slots: they go to
11731165 registers or the stack, the procedure is called, and its result, if
11741166 any, is pushed on the value stack. }
1175-{ The value on top is argument c.n of c, which waits for the call: a
1176- constant or an address is noted, a real or anything else goes to a
1177- frame slot. }
1178-procedure PopArg(var c: TCall);
1179-var i: longint;
1167+{ Argument value i, as it is on the stack, into register r }
1168+procedure ArgTo(i, r: longint);
11801169 begin
1181- i := depth - 1;
1182- c.how[c.n] := argSlot;
1183- if not c.isF[c.n] and (stack[i].loc = lConst) then c.how[c.n] := argConst
1184- else if stack[i].loc = lAdr then c.how[c.n] := argAdr
1185- else if stack[i].loc = lHeld then c.how[c.n] := argHeld;
1186- if c.how[c.n] = argSlot then begin
1187- c.slots[c.n] := AllocTemp(8, 8);
1188- Store(Mem(RBP, c.slots[c.n]), Top, 8);
1189- end else begin
1190- c.slots[c.n] := stack[i].val;
1191- c.adrChunk[c.n] := stack[i].chunk;
1170+ case stack[i].loc of
1171+ lConst: MovRI(r, stack[i].val);
1172+ lHeld: MovRR(r, stack[i].val);
1173+ lAdr: Lea(r, AdrMem(i, 0));
1174+ lReg: if RegOf(i) <> r then MovRR(r, RegOf(i));
1175+ else Load(r, Mem(RBP, stack[i].val), 8, false);
11921176 end;
1193- Drop;
11941177 end;
11951178
1196-{ Argument i of c into register r }
1197-procedure LoadArg(const c: TCall; i, r: longint);
1198-begin
1199- case c.how[i] of
1200- argConst: MovRI(r, c.slots[i]);
1201- argHeld: MovRR(r, c.slots[i]);
1202- argAdr:
1203- if c.adrChunk[i] < 0 then Lea(r, Mem(RBP, c.slots[i]))
1204- else Lea(r, MemRip(c.adrChunk[i], c.slots[i]));
1205- else Load(r, Mem(RBP, c.slots[i]), 8, false);
1206- end;
1207-end;
1208-
12091179 procedure Call(var c: TCall);
12101180 var
1211- res: PType; i, ni, nf, k, pad, r: longint;
1212- onStack: array[0..63] of boolean;
1181+ res: PType; i, ni, nf, k, pad, r, first: longint;
1182+ onStack: array[0..63] of boolean; late: array[4..5] of longint;
12131183 begin
1214- SpillBelow(depth); { the registers in use, saved around the call }
1184+ { the arguments are the c.n values on top, where computing them left
1185+ them; the registers in use under them are saved around the call }
1186+ first := depth - c.n;
1187+ SpillBelow(first);
12151188 res := c.sig^.base;
12161189 c.ret := 0;
12171190 ni := 0; nf := 0;
@@ -1224,7 +1197,13 @@
12241197 k := 0;
12251198 for i := 0 to c.n - 1 do begin
12261199 if c.isF[i] then begin onStack[i] := nf >= 8; if nf < 8 then inc(nf) end
1227- else begin onStack[i] := ni >= 6; if ni < 6 then inc(ni) end;
1200+ else begin
1201+ { a slice's words both go in registers, or both on the stack }
1202+ if (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1]
1203+ else if c.pair[i] then onStack[i] := ni > 4
1204+ else onStack[i] := ni >= 6;
1205+ if not onStack[i] then inc(ni);
1206+ end;
12281207 if onStack[i] then inc(k);
12291208 end;
12301209 { System V wants rsp a multiple of 16 at the call: a quadword of
@@ -1233,15 +1212,28 @@
12331212 if pad <> 0 then AluI(aluSub, RSP, 8);
12341213 for i := c.n - 1 downto 0 do
12351214 if onStack[i] then begin
1236- LoadArg(c, i, RAX);
1215+ ArgTo(first + i, RAX);
12371216 Push(RAX);
12381217 end;
12391218 ni := 0; nf := 0;
1219+ late[4] := -1; late[5] := -1;
12401220 if (c.ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end;
1221+ { rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as
1222+ they may hold arguments still }
12411223 for i := 0 to c.n - 1 do
12421224 if not onStack[i] then
1243- if c.isF[i] then begin MovsdXM(nf, Mem(RBP, c.slots[i])); inc(nf) end
1244- else begin LoadArg(c, i, IntRegs[ni]); inc(ni) end;
1225+ if c.isF[i] then begin
1226+ if stack[first + i].loc = lFrame then MovsdXM(nf, Mem(RBP, stack[first + i].val))
1227+ else begin ArgTo(first + i, RAX); MovqXR(nf, RAX) end;
1228+ inc(nf);
1229+ end else begin
1230+ if ni < 4 then ArgTo(first + i, IntRegs[ni]) else late[ni] := first + i;
1231+ inc(ni);
1232+ end;
1233+ if late[4] >= 0 then ArgTo(late[4], RAX);
1234+ if late[5] >= 0 then ArgTo(late[5], R9);
1235+ if late[4] >= 0 then MovRR(R8, RAX);
1236+ for i := 1 to c.n do Drop;
12451237 { a procedure taking ... is told in al how many xmm registers it got }
12461238 if c.sig^.variadic then MovRI(RAX, nf);
12471239 if c.ext then CallM(MemRip(c.chunk, 0))
@@ -1309,7 +1301,7 @@
13091301 pushed it. A var parameter, and a record or array passed by value,
13101302 holds an address (ind). }
13111303 procedure BeginProc(sig: PType; params: PObj);
1312-var p: PObj; ni, nf: longint; stackOff: int64; f: boolean;
1304+var p: PObj; ni, nf, k: longint; stackOff: int64; f: boolean;
13131305 begin
13141306 frameTop := 0; frameMax := 0; permTop := 0;
13151307 ResetStack(0, 0);
@@ -1326,9 +1318,22 @@
13261318 end;
13271319 p := params;
13281320 while p <> nil do begin
1329- p^.ind := p^.isVar or IsAggregate(p^.typ);
1321+ p^.ind := p^.isVar or IsAggregate(p^.typ) and (p^.typ^.form <> fSlice);
13301322 f := (p^.typ^.form = fReal) and not p^.isVar;
1331- if (f and (nf < 8)) or (not f and (ni < 6)) then begin
1323+ if (p^.typ^.form = fSlice) and not p^.isVar then begin
1324+ { its two words, in two registers or both on the stack }
1325+ if ni <= 4 then begin
1326+ p^.val := AllocPerm(16, 8);
1327+ for k := 0 to 1 do begin
1328+ inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + 8 * k;
1329+ inSize[numIn] := 8; inObj[numIn] := nil;
1330+ inc(numIn); inc(ni);
1331+ end;
1332+ end else begin
1333+ p^.val := stackOff;
1334+ inc(stackOff, 16);
1335+ end;
1336+ end else if (f and (nf < 8)) or (not f and (ni < 6)) then begin
13321337 p^.val := AllocPerm(8, 8);
13331338 if f then begin inReg[numIn] := -1 - nf; inc(nf) end
13341339 else begin inReg[numIn] := IntRegs[ni]; inc(ni) end;

src/item.pas modified +15 -4

@@ -1140,10 +1140,10 @@
11401140 end;
11411141
11421142 procedure PassArg(var c: TCall; var a: TItem);
1143-var p: PObj; s: int64;
1143+var p: PObj; adr: TAdr; s: int64;
11441144 begin
11451145 p := c.param;
1146- if c.n > High(c.slots) then MarkAt(a.pos, 'too many arguments');
1146+ if c.n >= High(c.isF) then MarkAt(a.pos, 'too many arguments');
11471147 if p = nil then begin
11481148 { past the parameters of a C procedure that ends in ...: a string
11491149 goes as a C string, an integer constant as an INT, a real
@@ -1155,8 +1155,8 @@
11551155 else if IsF32(a.typ) then MarkAt(a.pos, 'an F32 goes to ... as F64(x)')
11561156 else if not IsScalar(a.typ) then MarkAt(a.pos, 'only a single value goes to ...');
11571157 c.isF[c.n] := a.typ^.form = fReal;
1158+ c.pair[c.n] := false;
11581159 Value(a);
1159- PopArg(c);
11601160 inc(c.n);
11611161 exit;
11621162 end;
@@ -1170,6 +1170,17 @@
11701170 if not Same(a.typ, p^.typ) then Mismatch(a, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a));
11711171 if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter');
11721172 Address(a);
1173+ end else if p^.typ^.form = fSlice then begin
1174+ { its two words, read now, so that a later argument can't change them }
1175+ Coerce(a, p^.typ);
1176+ if (a.mode <> mVar) or a.vol or not (a.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(a);
1177+ adr := AdrOf(a);
1178+ PushVar(adr, 8, false, kWord);
1179+ c.pair[c.n] := true;
1180+ inc(c.n);
1181+ c.isF[c.n] := false;
1182+ inc(adr.off, 8);
1183+ PushVar(adr, 8, false, kWord);
11731184 end else if IsAggregate(p^.typ) then begin
11741185 Coerce(a, p^.typ);
11751186 if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a);
@@ -1178,7 +1189,7 @@
11781189 Coerce(a, p^.typ);
11791190 Value(a);
11801191 end;
1181- PopArg(c);
1192+ c.pair[c.n] := false;
11821193 inc(c.n);
11831194 c.param := p^.next;
11841195 end;

test/list/Listing.expect (history) modified +20 -21

@@ -27,7 +27,7 @@
2727 17 proc main*;
2828 0 push rbp
2929 1 mov rbp, rsp
30- 4 sub rsp, 48
30+ 4 sub rsp, 32
3131 11 xor eax, eax
3232 13 mov [rbp - 8], rax
3333 17 mov [rbp - 16], rax
@@ -38,30 +38,29 @@
3838 32 mov qword [rbp - 24], 3
3939 40 mov r9, [rbp - 24]
4040 44 cmp rbx, r9
41- 47 jg 144
41+ 47 jg 139
4242 21 if i # 2 then total := add(total, twice(i)); end;
4343 53 cmp rbx, 2
44- 57 je 112
44+ 57 je 107
4545 63 mov r8, [Listing.total]
4646 70 mov [rbp - 32], r8
4747 74 mov rdi, rbx
4848 77 call Listing.twice
49- 82 mov r8, rax
50- 85 mov [rbp - 40], r8
51- 89 mov rdi, [rbp - 32]
52- 93 mov rsi, [rbp - 40]
53- 97 call Listing.add
54- 102 mov r8, rax
55- 105 mov [Listing.total], r8
56- 112 mov r8, [rbp - 24]
57- 116 sub r8, rbx
58- 119 cmp r8, 1
59- 123 jb 144
60- 129 mov r8, rbx
61- 132 add r8, 1
62- 136 mov rbx, r8
63- 139 jmp 40
49+ 82 mov r9, rax
50+ 85 mov rdi, [rbp - 32]
51+ 89 mov rsi, r9
52+ 92 call Listing.add
53+ 97 mov r8, rax
54+ 100 mov [Listing.total], r8
55+ 107 mov r8, [rbp - 24]
56+ 111 sub r8, rbx
57+ 114 cmp r8, 1
58+ 118 jb 139
59+ 124 mov r8, rbx
60+ 127 add r8, 1
61+ 131 mov rbx, r8
62+ 134 jmp 40
6463 23 end main;
65- 144 mov rbx, [rbp - 16]
66- 148 leave
67- 149 ret
64+ 139 mov rbx, [rbp - 16]
65+ 143 leave
66+ 144 ret

test/run/ThreadOverflow.stderr (history) modified +2 -2

@@ -1,6 +1,6 @@
11 test/run/ThreadOverflow.raz:6:1: stopped by a stack overflow
22 in ThreadOverflow.down at test/run/ThreadOverflow.raz:6:1
3- in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1871
4- and 1871 more the same
3+ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926
4+ and 1926 more the same
55 in ThreadOverflow.work at test/run/ThreadOverflow.raz:15:5: _arg = nil
66 in Thread.run at lib/Thread.linux-x86_64.raz:80:5: arg = 0x?, t = 0x?