raz / commit 36efcd0f

commit
36efcd0faadbff4af4c41a38782949f5e4dc91c5
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
848f0c57
Step 8: Wirth's register stack in gen

The value at depth d is in Regs[d mod 4], r8 to r11; rax, rdx, rcx
(division, shifts) and rsi, rdi (copying) stay out. Each value is a
constant (only noted), in its register, or spilled to a frame slot;
Claim gives a value its register, spilling a deeper owner when an
expression runs deeper than the registers; Loaded brings one back
where it is used. Calls and system calls spill every register in use
first (SpillBelow); and/or spill what is under the left operand, so
both ways to their label find it in the same place. Ops work in place
on the left operand's register, with the right one's own register, an
immediate, shifts for a power of two, or rcx. Results come back to
the stack from rax/xmm0. CmpI's scratch is rdx now (r11 is a stack
register).
test/run/Deep: nine values deep, calls, and/or, syscalls and constants
on the left among held values. Bench 1.03M lines/s.

patch · log

5 files changed, +464 -306

PLAN.md (history) modified +1 -1

@@ -134,7 +134,7 @@
134134 integer, an `F32`, an `F64`) and the place in the source, and no
135135 x86-64 in the parser; a port is a new `gen`. Every test gives
136136 the same output as before.
137-- [ ] 8. Wirth's register stack in the x86-64 `gen`: the value at
137+- [x] 8. Wirth's register stack in the x86-64 `gen`: the value at
138138 depth `d` is held in register `d`, not pushed. The registers in
139139 use are saved around a call, and spilled to the frame when an
140140 expression is deeper than the registers; the stack is empty when

doc/compiler.md (history) modified +32 -25

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is eleven files of Free Pascal, under 7,000 lines of code.
3+The compiler is eleven files of Free Pascal, some 7,000 lines of code.
44 This guide walks through them in the order they are best read: from the
55 characters of a source file to the bytes of an executable. It assumes
66 you know the language from `doc/raz.md`, and enough x86-64 to read a
@@ -13,7 +13,7 @@
1313 | `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 244 | 192 | The compiled program: chunks, fixups, line notes |
1515 | `src/x64.pas` | 476 | 377 | The instruction encoder, and jumps within a procedure |
16-| `src/gen.pas` | 1,206 | 963 | The stack machine, for x86-64: values, operations, checks, calls, frames |
16+| `src/gen.pas` | 1,290 | 1,020 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1717 | `src/item.pas` | 1,303 | 1,090 | Items: types, constants, variables, operators, conversions, calls |
1818 | `src/parse.pas` | 2,538 | 2,276 | Declarations, statements, expressions, built-ins, recovery, warnings |
1919 | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places |
@@ -41,13 +41,13 @@
4141 another machine is a new `gen.pas`, with an encoder of its own in place
4242 of `x64.pas`.
4343
44-On x86-64 the stack machine's stack is the machine's: the value on
45-top is in `rax`, and the values under it are pushed when another is
46-computed; a constant is only noted, until something needs it in a
47-register, so that it can be an instruction's operand. No register is
48-ever allocated, so there is no register allocator, and every access
49-the program writes is one instruction, in the order written. That is
50-also why `VOLATILE` costs the compiler nothing: it never does anything
44+On x86-64 the stack lives in registers, as in Wirth's compilers: the
45+value at depth `d` is in register `d`, of `r8` to `r11`, and a constant
46+is only noted, until something needs it in a register, so that it can
47+be an instruction's operand. Which register a value goes in is settled
48+by its depth, so there is no register allocator, and every access the
49+program writes is one instruction, in the order written. That is also
50+why `VOLATILE` costs the compiler nothing: it never does anything
5151 clever to begin with.
5252
5353 The output is a set of *chunks*: one for each procedure, each module
@@ -100,13 +100,11 @@
100100 sub rsp,0x10 ; its size, patched in by EndProc
101101 mov QWORD PTR [rbp-0x8],rdi ; the parameters, into their slots
102102 mov QWORD PTR [rbp-0x10],rsi
103-mov rax,QWORD PTR [rbp-0x8] ; a
104-push rax ; kept while b is loaded
105-mov rax,QWORD PTR [rbp-0x10] ; b; the 2 is only noted
106-shl rax,0x1 ; times 2 is a shift
107-mov rcx,rax
108-pop rax ; a again
109-add rax,rcx
103+mov r8,QWORD PTR [rbp-0x8] ; a, at depth 0
104+mov r9,QWORD PTR [rbp-0x10] ; b, at depth 1; the 2 is only noted
105+shl r9,0x1 ; times 2 is a shift
106+add r8,r9
107+mov rax,r8 ; the result
110108 jmp <leave> ; return
111109 xor eax,eax ; running off the end returns zero
112110 movq xmm0,rax
@@ -114,9 +112,9 @@
114112 ret
115113 ```
116114
117-Every line of it can be traced to a routine in `gen.pas`; the push is
118-`Spill`'s, as `PushVar` loads `b`, the pop `PopTo`'s, as `Op` takes
119-its operands, and the frame is `EnterBody`'s and `EndProc`'s. It is
115+Every line of it can be traced to a routine in `gen.pas`: the loads
116+are `PushVar`'s, the shift and the add `Op`'s, the result
117+`ReturnJump`'s, and the frame is `EnterBody`'s and `EndProc`'s. It is
120118 not fast code. It is code whose every instruction you can account
121119 for, which is the point.
122120
@@ -271,12 +269,21 @@
271269 address first, so that a variable is read or written with one
272270 instruction.
273271
274-The stack: `stack` notes each value, a constant or one computed;
275-`inRax` is the one in `rax`, if any. `Computed` puts a value just
276-computed in `rax` on top; `Spill` pushes the one in `rax` first, when
277-`rax` is wanted for another; `PopTo` brings the value on top into a
278-register, from the constant, from `rax` or by a pop. `pushed` counts
279-what is on the machine's stack, which a call must align. `Op` takes a
272+The stack: `stack` notes where each value is, a constant (`lConst`),
273+in its register (`lReg`) or in a frame slot (`lFrame`); `owner` which
274+value each register holds. The value at depth `d` belongs in
275+`Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division
276+and shifts need, and `rsi` and `rdi`, which copying needs, aren't among
277+them. `Claim` gives a value its register, and `Spill`s a deeper one
278+that holds it, when an expression is deeper than the registers;
279+`Loaded` brings a value into its register, from its constant or its
280+slot; `Top` is `Loaded` for the value on top, and `Target` the
281+register a new one is computed in. Before a call every value in a
282+register is spilled (`SpillBelow`), since the call may change them, and
283+so are the values under `and`'s and `or`'s left operand, which both
284+ways to its label must find in the same place. `pushed` counts what
285+is on the machine's stack, which a call must align: the return
286+address, in a deferred statement. `Op` takes a
280287 constant on the right as it is, and a power of two on the right of
281288 `*`, `div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow.
282289 Narrow integers are brought back to their width after each operation,

src/gen.pas modified +364 -280

@@ -9,10 +9,10 @@
99 code for those calls, and nothing above it knows the machine: a port
1010 is a new gen.pas, with an encoder of its own.
1111
12- Here the stack is the machine's: the value on top, once computed, is
13- in rax, and the values under it are pushed when another is computed.
14- A constant is only noted until something needs it in a register, so
15- that it can be an instruction's operand.
12+ The stack is kept in registers, as in Wirth's compilers: the value at
13+ depth d in register d, of r8 to r11, a deeper one spilled to the frame
14+ while its register is wanted. A constant is only noted until something
15+ needs it in a register, so that it can be an instruction's operand.
1616
1717 Calls use the System V convention. Arguments are computed left to
1818 right into frame slots, then loaded into registers just before the
@@ -144,15 +144,28 @@
144144 implementation
145145
146146 type
147+ { where a value on the stack is }
148+ TLoc = (lConst, { not loaded: val is the value }
149+ lReg, { in its register }
150+ lFrame); { spilled: in the frame slot val }
151+
147152 TEntry = record
148- isConst: boolean; { not loaded: val is the value }
153+ loc: TLoc;
149154 val: int64;
150155 k: TKind;
151156 end;
152157
158+const
159+ { the registers of the stack: the value at depth d is in Regs[d mod
160+ NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi
161+ and rdi, which copying needs, aren't among them. }
162+ NumRegs = 4;
163+ Regs: array[0..NumRegs - 1] of longint = (R8, R9, R10, R11);
164+ IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9);
165+
153166 var
154167 stack: array[0..255] of TEntry;
155- inRax: longint; { the entry whose value is in rax, or -1 }
168+ owner: array[0..NumRegs - 1] of longint; { the value in each register, or -1 }
156169 frameTop, frameMax, permTop: int64;
157170 frameFix: longint; { where the frame size is patched in }
158171 paramTop: int64; { the parameters' slots end here }
@@ -165,9 +178,6 @@
165178 inSize: array[0..13] of longint;
166179 numIn: longint;
167180
168-const
169- IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9);
170-
171181 function KindOf(t: PType): TKind;
172182 begin
173183 if t^.form = fReal then begin
@@ -176,158 +186,186 @@
176186 else KindOf := kWord;
177187 end;
178188
179-{ The stack }
189+{ The stack, as in Wirth's compilers: the value at depth d is held in
190+ register d, for as many registers as there are. A value deeper than
191+ that, when its register is wanted, and every value in a register
192+ when a call would clobber them, is spilled to a frame slot, and loaded
193+ again where it is used. A constant is only noted. }
194+
195+function RegOf(i: longint): longint;
196+begin
197+ RegOf := Regs[i mod NumRegs];
198+end;
199+
200+{ The value i, in a register, goes to a frame slot }
201+procedure Spill(i: longint);
202+var s: int64;
203+begin
204+ s := AllocTemp(8, 8);
205+ Store(Mem(RBP, s), RegOf(i), 8);
206+ stack[i].loc := lFrame;
207+ stack[i].val := s;
208+ owner[i mod NumRegs] := -1;
209+end;
180210
181-{ rax is wanted for a new value: the one in it is pushed }
182-procedure Spill;
211+{ Every value below depth d in a register, spilled }
212+procedure SpillBelow(d: longint);
213+var r: longint;
183214 begin
184- if inRax >= 0 then begin
185- Push(RAX);
186- inc(pushed);
187- inRax := -1;
215+ for r := 0 to NumRegs - 1 do
216+ if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]);
217+end;
218+
219+{ The register of value i, for it to hold: a deeper value in it is
220+ spilled. }
221+function Claim(i: longint): longint;
222+var r: longint;
223+begin
224+ r := i mod NumRegs;
225+ if (owner[r] >= 0) and (owner[r] <> i) then Spill(owner[r]);
226+ owner[r] := i;
227+ Claim := Regs[r];
228+end;
229+
230+{ The value i, in its register }
231+function Loaded(i: longint): longint;
232+var r: longint;
233+begin
234+ if stack[i].loc <> lReg then begin
235+ r := Claim(i);
236+ if stack[i].loc = lConst then MovRI(r, stack[i].val)
237+ else Load(r, Mem(RBP, stack[i].val), 8, false);
238+ stack[i].loc := lReg;
188239 end;
240+ Loaded := RegOf(i);
189241 end;
190242
191-procedure NewEntry(k: TKind; isConst: boolean; v: int64);
243+function Top: longint;
192244 begin
245+ if depth = 0 then Fatal('internal error: the stack is empty');
246+ Top := Loaded(depth - 1);
247+end;
248+
249+procedure NewEntry(k: TKind; loc: TLoc; v: int64);
250+begin
193251 if depth > High(stack) then Mark('the expression is too deep');
194252 stack[depth].k := k;
195- stack[depth].isConst := isConst;
253+ stack[depth].loc := loc;
196254 stack[depth].val := v;
197255 inc(depth);
198256 end;
199257
200-{ The value just computed in rax goes on top }
258+{ The register a new value on top is to be computed in }
259+function Target: longint;
260+begin
261+ Target := Claim(depth);
262+end;
263+
264+{ The value just computed in Target goes on top }
201265 procedure Computed(k: TKind);
202266 begin
203- NewEntry(k, false, 0);
204- inRax := depth - 1;
267+ NewEntry(k, lReg, 0);
205268 end;
206269
207-{ The value on top, popped into register r }
208-procedure PopTo(r: longint);
209-var e: TEntry;
270+procedure Drop;
210271 begin
211- if depth = 0 then Fatal('internal error: the stack is empty');
212272 dec(depth);
213- e := stack[depth];
214- if e.isConst then begin
215- if r = RAX then Spill;
216- MovRI(r, e.val);
217- end else if inRax = depth then begin
218- if r <> RAX then MovRR(r, RAX);
219- inRax := -1;
220- end else begin
221- Pop(r);
222- dec(pushed);
223- end;
273+ if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1;
224274 end;
225275
226-{ The value on top, into rax, and on top still }
227-procedure TopToRax;
228-var k: TKind;
276+{ Whether the value on top is a constant, and if so, which }
277+function TopConst(var v: int64): boolean;
229278 begin
230- k := stack[depth - 1].k;
231- PopTo(RAX);
232- Computed(k);
279+ TopConst := stack[depth - 1].loc = lConst;
280+ v := stack[depth - 1].val;
233281 end;
234282
235283 procedure ResetStack(d, p: longint);
284+var r: longint;
236285 begin
237286 depth := d;
238287 pushed := p;
239- inRax := -1;
288+ for r := 0 to NumRegs - 1 do owner[r] := -1;
240289 end;
241290
242291 procedure PushConst(k: TKind; v: int64);
243292 begin
244- NewEntry(k, true, v);
293+ NewEntry(k, lConst, v);
245294 end;
246295
247-{ The memory a names: an address on the stack is popped into r, one in
248- a slot loaded into r }
296+{ The memory a names. An address on the stack is popped, and is in its
297+ register still; one in a slot is loaded into r. }
249298 function Place(const a: TAdr; r: longint): TMem;
299+var ra: longint;
250300 begin
251301 case a.base of
252302 bFrame: Place := Mem(RBP, a.off);
253303 bGlobal: Place := MemRip(a.chunk, a.off);
254- bStack: begin PopTo(r); Place := Mem(r, a.off) end;
304+ bStack: begin ra := Top; Drop; Place := Mem(ra, a.off) end;
255305 else
256- if r = RAX then Spill;
257306 Load(r, Mem(RBP, a.chunk), 8, false);
258307 Place := Mem(r, a.off);
259308 end;
260309 end;
261310
262311 procedure PushAdr(const a: TAdr);
263-var m: TMem;
312+var m: TMem; r: longint;
264313 begin
265314 if (a.base = bStack) and (a.off = 0) then exit;
266- m := Place(a, RAX);
267- Spill;
268- Lea(RAX, m);
315+ if a.base = bStack then begin m := Place(a, -1); r := Target end
316+ else begin r := Target; m := Place(a, r) end;
317+ Lea(r, m);
269318 Computed(kWord);
270319 end;
271320
272321 procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind);
273-var m: TMem;
322+var m: TMem; r: longint;
274323 begin
275- m := Place(a, RAX);
276- Spill;
277- Load(RAX, m, size, signed);
324+ if a.base = bStack then begin m := Place(a, -1); r := Target end
325+ else begin r := Target; m := Place(a, r) end;
326+ Load(r, m, size, signed);
278327 Computed(k);
279328 end;
280329
281330 procedure PushProc(c: longint; ext: boolean);
331+var r: longint;
282332 begin
283- Spill;
284- if ext then Load(RAX, MemRip(c, 0), 8, false)
285- else Lea(RAX, MemRip(c, 0));
333+ r := Target;
334+ if ext then Load(r, MemRip(c, 0), 8, false)
335+ else Lea(r, MemRip(c, 0));
286336 Computed(kWord);
287337 end;
288338
289339 { Pops a value into the variable at a; an address on the stack is under
290340 the value. }
291341 procedure PopVar(const a: TAdr; size: longint);
292-var m: TMem;
342+var r: longint; v: int64;
293343 begin
294- if a.base in [bFrame, bGlobal] then begin
295- PopTo(RAX);
296- Store(Place(a, RAX), RAX, size);
297- end else begin
298- PopTo(RCX);
299- m := Place(a, RAX);
300- Store(m, RCX, size);
301- end;
344+ if TopConst(v) then begin r := RAX; MovRI(RAX, v) end
345+ else r := Top;
346+ Drop;
347+ Store(Place(a, RCX), r, size);
302348 end;
303349
304-procedure Drop;
305-begin
306- dec(depth);
307- if stack[depth].isConst then exit;
308- if inRax = depth then inRax := -1
309- else begin
310- AluI(aluAdd, RSP, 8);
311- dec(pushed);
312- end;
313-end;
314-
315350 { The two values on top change places. }
316351 procedure Swap;
317-var e: TEntry; a, b: boolean;
352+var e: TEntry; x, y: longint; rx, ry: boolean;
318353 begin
319- a := stack[depth - 2].isConst;
320- b := stack[depth - 1].isConst;
321- if not a and not b then begin
322- { the one on top is in rax, or pushed over the other }
323- if inRax <> depth - 1 then begin Pop(RAX); dec(pushed) end;
324- Xchg(Mem(RSP, 0), RAX);
325- inRax := depth - 1;
326- end else if inRax = depth - 1 then inRax := depth - 2
327- else if inRax = depth - 2 then inRax := depth - 1;
328- e := stack[depth - 1];
329- stack[depth - 1] := stack[depth - 2];
330- stack[depth - 2] := e;
354+ x := depth - 2; y := depth - 1;
355+ rx := stack[x].loc = lReg;
356+ ry := stack[y].loc = lReg;
357+ if rx and ry then begin
358+ MovRR(RAX, RegOf(x));
359+ MovRR(RegOf(x), RegOf(y));
360+ MovRR(RegOf(y), RAX);
361+ end else if rx then begin
362+ owner[x mod NumRegs] := -1;
363+ MovRR(Claim(y), RegOf(x));
364+ end else if ry then begin
365+ owner[y mod NumRegs] := -1;
366+ MovRR(Claim(x), RegOf(y));
367+ end;
368+ e := stack[x]; stack[x] := stack[y]; stack[y] := e;
331369 end;
332370
333371 { Copying }
@@ -358,8 +396,10 @@
358396 size bytes to off bytes past the destination. }
359397 procedure CopyBlock(size, off: int64);
360398 begin
361- PopTo(RSI);
362- PopTo(RDI);
399+ MovRR(RSI, Top);
400+ Drop;
401+ MovRR(RDI, Top);
402+ Drop;
363403 if off <> 0 then Lea(RDI, Mem(RDI, off));
364404 CopyBytes(size);
365405 end;
@@ -367,7 +407,8 @@
367407 { Pops a source's address, and copies size bytes to a. }
368408 procedure CopyTo(const a: TAdr; size: int64);
369409 begin
370- PopTo(RSI);
410+ MovRR(RSI, Top);
411+ Drop;
371412 Lea(RDI, Place(a, RDI));
372413 CopyBytes(size);
373414 end;
@@ -375,7 +416,6 @@
375416 procedure ZeroVar(const a: TAdr; size: int64);
376417 begin
377418 Lea(RDI, Place(a, RDI));
378- Spill;
379419 MovRI(RCX, size);
380420 MovRI(RAX, 0);
381421 RepStosb;
@@ -392,39 +432,45 @@
392432 Log2 := n;
393433 end;
394434
395-{ rax div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards
435+function Fits32(v: int64): boolean;
436+begin
437+ Fits32 := (v >= low(longint)) and (v <= high(longint));
438+end;
439+
440+{ r compared with v, through rdx if v needs more than 32 bits }
441+procedure CmpI(r: longint; v: int64);
442+begin
443+ if Fits32(v) then AluI(aluCmp, r, v)
444+ else begin MovRI(RDX, v); Alu(aluCmp, r, RDX) end;
445+end;
446+
447+{ r div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards
396448 zero, so a negative dividend first gets 2^k - 1 added (the bias). }
397-procedure DivPow2(op: TSym; k: longint; unsign: boolean);
449+procedure DivPow2(op: TSym; r, k: longint; unsign: boolean);
398450 begin
399451 if unsign then begin
400- if op = sDiv then ShiftI(shShr, RAX, k)
401- else begin ShiftI(shShl, RAX, 64 - k); ShiftI(shShr, RAX, 64 - k) end;
452+ if op = sDiv then ShiftI(shShr, r, k)
453+ else begin ShiftI(shShl, r, 64 - k); ShiftI(shShr, r, 64 - k) end;
402454 exit;
403455 end;
404- MovRR(RCX, RAX);
456+ MovRR(RCX, r);
405457 ShiftI(shSar, RCX, 63);
406458 ShiftI(shShr, RCX, 64 - k);
407- Alu(aluAdd, RCX, RAX);
459+ Alu(aluAdd, RCX, r);
408460 ShiftI(shSar, RCX, k);
409- if op = sDiv then MovRR(RAX, RCX)
410- else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end;
411-end;
412-
413-procedure CmpI(r: longint; v: int64);
414-begin
415- if (v >= low(longint)) and (v <= high(longint)) then AluI(aluCmp, r, v)
416- else begin MovRI(R11, v); Alu(aluCmp, r, R11) end;
461+ if op = sDiv then MovRR(r, RCX)
462+ else begin ShiftI(shShl, RCX, k); Alu(aluSub, r, RCX) end;
417463 end;
418464
419-{ Before rax div rcx, of a signed type of bits < 64: its smallest value
465+{ Before rax div y, of a signed type of bits < 64: its smallest value
420466 divided by -1 overflows it, which must stop the program as it does for
421467 an I64 (report section 6.1). idiv on the 64-bit values wouldn't fault,
422468 so then rax is made the smallest I64, and it does. }
423-procedure DivTrap(bits: longint);
469+procedure DivTrap(bits, y: longint);
424470 var L: longint;
425471 begin
426472 L := NoLabel;
427- CmpI(RCX, -1);
473+ CmpI(y, -1);
428474 Jcc(ccNE, L);
429475 CmpI(RAX, int64(-1) shl (bits - 1));
430476 Jcc(ccNE, L);
@@ -432,69 +478,93 @@
432478 FixLink(L);
433479 end;
434480
435-{ The two values on top, the left one into rax and the right into rcx }
436-procedure Operands;
481+{ The right operand of the two values on top, popped, in register y:
482+ its own, or rcx for a constant; the left one stays, in its register,
483+ which is returned. }
484+function Operands(var y: longint): longint;
485+var v: int64;
437486 begin
438- PopTo(RCX);
439- PopTo(RAX);
487+ if TopConst(v) then begin MovRI(RCX, v); y := RCX end else y := Top;
488+ Drop;
489+ Operands := Top;
440490 end;
441491
442492 procedure RealOp(op: TSym; k: TKind);
443-var f: boolean;
493+var f: boolean; r, y: longint;
444494 begin
445495 f := k = kF32;
446- Operands;
447- MovqXR(0, RAX);
448- MovqXR(1, RCX);
496+ r := Operands(y);
497+ MovqXR(0, r);
498+ MovqXR(1, y);
449499 case op of
450500 sPlus: Sse($58, 0, 1, f);
451501 sMinus: Sse($5C, 0, 1, f);
452502 sTimes: Sse($59, 0, 1, f);
453503 sSlash: Sse($5E, 0, 1, f);
454504 end;
455- MovqRX(RAX, 0);
456- Computed(k);
505+ MovqRX(r, 0);
457506 end;
458507
508+{ The group 1 operation of +, -, &, | and xor }
509+function AluOf(op: TSym): longint;
510+begin
511+ case op of
512+ sPlus: AluOf := aluAdd;
513+ sMinus: AluOf := aluSub;
514+ sAmp: AluOf := aluAnd;
515+ sBar: AluOf := aluOr;
516+ else AluOf := aluXor;
517+ end;
518+end;
519+
459520 { The two values on top, the left op the right, an integer of size
460521 bytes, signed or not, brought back to its width. }
461522 procedure Op(op: TSym; k: TKind; size: longint; signed: boolean);
462-var y: TEntry; sh, bits: longint;
523+var v: int64; r, y, sh, bits: longint; c: boolean;
463524 begin
464525 if k in [kF32, kF64] then begin RealOp(op, k); exit end;
465526 bits := size * 8;
466- y := stack[depth - 1];
527+ c := TopConst(v);
467528 sh := -1;
468- if y.isConst and (op in [sTimes, sDiv, sMod]) then sh := Log2(y.val);
469- if y.isConst then begin
470- dec(depth);
471- PopTo(RAX);
472- if sh < 1 then MovRI(RCX, y.val);
473- end else Operands;
474- case op of
475- sPlus: Alu(aluAdd, RAX, RCX);
476- sMinus: Alu(aluSub, RAX, RCX);
477- sTimes: if sh >= 1 then ShiftI(shShl, RAX, sh) else Imul(RAX, RCX);
478- sDiv, sMod: if sh >= 1 then DivPow2(op, sh, not signed) else begin
479- if not signed then begin MovRI(RDX, 0); Unary(6, RCX) end
480- else begin
481- if (bits < 64) and (not y.isConst or (y.val = -1)) then DivTrap(bits);
482- Cqo; Unary(7, RCX)
529+ if c and (op in [sTimes, sDiv, sMod]) then sh := Log2(v);
530+ if c and (sh >= 1) then begin
531+ Drop;
532+ r := Top;
533+ if op = sTimes then ShiftI(shShl, r, sh) else DivPow2(op, r, sh, not signed);
534+ end else if c and Fits32(v) and (op in [sPlus, sMinus, sBar, sXor, sAmp, sTimes, sShl, sShr]) then begin
535+ Drop;
536+ r := Top;
537+ case op of
538+ sTimes: ImulI(r, v);
539+ sShl: ShiftI(shShl, r, v and (bits - 1));
540+ sShr: if signed then ShiftI(shSar, r, v and (bits - 1)) else ShiftI(shShr, r, v and (bits - 1));
541+ else AluI(AluOf(op), r, v);
542+ end;
543+ end else begin
544+ r := Operands(y);
545+ case op of
546+ sPlus, sMinus, sAmp, sBar, sXor: Alu(AluOf(op), r, y);
547+ sTimes: Imul(r, y);
548+ sDiv, sMod: begin
549+ MovRR(RAX, r);
550+ if not signed then begin MovRI(RDX, 0); Unary(6, y) end
551+ else begin
552+ if (bits < 64) and (not c or (v = -1)) then DivTrap(bits, y);
553+ Cqo; Unary(7, y)
554+ end;
555+ if op = sDiv then MovRR(r, RAX) else MovRR(r, RDX);
483556 end;
484- if op = sMod then MovRR(RAX, RDX);
557+ sShl, sShr: begin
558+ { the count modulo the width: the machine takes it modulo 64 }
559+ if y <> RCX then MovRR(RCX, y);
560+ if bits < 64 then AluI(aluAnd, RCX, bits - 1);
561+ if op = sShl then Shift(shShl, r)
562+ else if signed then Shift(shSar, r) else Shift(shShr, r);
563+ end;
485564 end;
486- sAmp: Alu(aluAnd, RAX, RCX);
487- sBar: Alu(aluOr, RAX, RCX);
488- sXor: Alu(aluXor, RAX, RCX);
489- sShl, sShr: begin
490- { the count modulo the width: the machine takes it modulo 64 }
491- if bits < 64 then AluI(aluAnd, RCX, bits - 1);
492- if op = sShl then Shift(shShl, RAX)
493- else if signed then Shift(shSar, RAX) else Shift(shShr, RAX);
494- end;
495565 end;
496- if bits < 64 then Extend(RAX, size, signed);
497- Computed(k);
566+ if bits < 64 then Extend(r, size, signed);
567+ stack[depth - 1].k := k;
498568 end;
499569
500570 { The left op the right of the two values on top, as a BOOL }
@@ -502,125 +572,119 @@
502572 const
503573 CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE);
504574 CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE);
505-var y: TEntry; f: boolean;
575+var f: boolean; r, y: longint; v: int64;
506576 begin
507577 if k in [kF32, kF64] then begin
508578 f := k = kF32;
509- Operands;
510- MovqXR(0, RAX);
511- MovqXR(1, RCX);
579+ r := Operands(y);
580+ MovqXR(0, r);
581+ MovqXR(1, y);
512582 case op of
513- sEql: begin Ucomis(0, 1, f); SetCC(ccE, RAX); SetCC(ccNP, RCX); Alu(aluAnd, RAX, RCX) end;
514- sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, RAX); SetCC(ccP, RCX); Alu(aluOr, RAX, RCX) end;
515- sLss: begin Ucomis(1, 0, f); SetCC(ccA, RAX) end;
516- sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, RAX) end;
517- sGtr: begin Ucomis(0, 1, f); SetCC(ccA, RAX) end;
518- sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, RAX) end;
583+ sEql: begin Ucomis(0, 1, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end;
584+ sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end;
585+ sLss: begin Ucomis(1, 0, f); SetCC(ccA, r) end;
586+ sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, r) end;
587+ sGtr: begin Ucomis(0, 1, f); SetCC(ccA, r) end;
588+ sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, r) end;
519589 end;
520590 end else begin
521- y := stack[depth - 1];
522- if y.isConst then begin
523- dec(depth);
524- PopTo(RAX);
525- CmpI(RAX, y.val);
526- end else begin
527- Operands;
528- Alu(aluCmp, RAX, RCX);
529- end;
530- if signed then SetCC(CcSigned[op], RAX) else SetCC(CcUnsigned[op], RAX);
591+ if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end
592+ else begin r := Operands(y); Alu(aluCmp, r, y) end;
593+ if signed then SetCC(CcSigned[op], r) else SetCC(CcUnsigned[op], r);
531594 end;
532- Computed(kWord);
595+ stack[depth - 1].k := kWord;
533596 end;
534597
535598 procedure Neg(k: TKind; size: longint; signed: boolean);
599+var r: longint;
536600 begin
537- PopTo(RAX);
601+ r := Top;
538602 case k of
539- kF32: Btc(RAX, 31);
540- kF64: Btc(RAX, 63);
603+ kF32: Btc(r, 31);
604+ kF64: Btc(r, 63);
541605 else
542- Unary(3, RAX);
543- if size < 8 then Extend(RAX, size, signed);
606+ Unary(3, r);
607+ if size < 8 then Extend(r, size, signed);
544608 end;
545- Computed(k);
546609 end;
547610
548611 procedure Complement(size: longint; signed: boolean);
612+var r: longint;
549613 begin
550- PopTo(RAX);
551- Unary(2, RAX);
552- if size < 8 then Extend(RAX, size, signed);
553- Computed(kWord);
614+ r := Top;
615+ Unary(2, r);
616+ if size < 8 then Extend(r, size, signed);
554617 end;
555618
556619 procedure NotBool;
557620 begin
558- PopTo(RAX);
559- AluI(aluXor, RAX, 1);
560- Computed(kWord);
621+ AluI(aluXor, Top, 1);
561622 end;
562623
563624 { The integer on top, brought to size bytes }
564625 procedure Narrow(size: longint; signed: boolean);
626+var r: longint;
565627 begin
566- PopTo(RAX);
567- if size < 8 then Extend(RAX, size, signed);
568- Computed(kWord);
628+ r := Top;
629+ if size < 8 then Extend(r, size, signed);
630+ stack[depth - 1].k := kWord;
569631 end;
570632
571633 { The integer on top, as a real of kind k; big if it may be a U64 above
572634 high(I64), which the machine would take as negative }
573635 procedure IntToReal(k: TKind; big: boolean);
574-var L1, L2: longint; f: boolean;
636+var L1, L2, r: longint; f: boolean;
575637 begin
576638 f := k = kF32;
577- PopTo(RAX);
639+ r := Top;
578640 if big then begin
579641 { above high(I64): halve, keeping the low bit, and double }
580642 L1 := NoLabel; L2 := NoLabel;
581- TestRR(RAX, RAX);
643+ TestRR(r, r);
582644 Jcc(ccS, L1);
583- Cvtsi2s(0, RAX, f);
645+ Cvtsi2s(0, r, f);
584646 Jmp(L2);
585647 FixLink(L1);
586- MovRR(RCX, RAX);
648+ MovRR(RCX, r);
587649 ShiftI(shShr, RCX, 1);
588- AluI(aluAnd, RAX, 1);
589- Alu(aluOr, RCX, RAX);
650+ AluI(aluAnd, r, 1);
651+ Alu(aluOr, RCX, r);
590652 Cvtsi2s(0, RCX, f);
591653 Sse($58, 0, 0, f);
592654 FixLink(L2);
593- end else Cvtsi2s(0, RAX, f);
594- MovqRX(RAX, 0);
595- Computed(k);
655+ end else Cvtsi2s(0, r, f);
656+ MovqRX(r, 0);
657+ stack[depth - 1].k := k;
596658 end;
597659
598660 { The real of kind k on top, truncated to an integer }
599661 procedure RealToInt(k: TKind);
662+var r: longint;
600663 begin
601- PopTo(RAX);
602- MovqXR(0, RAX);
603- Cvtts2si(RAX, 0, k = kF32);
604- Computed(kWord);
664+ r := Top;
665+ MovqXR(0, r);
666+ Cvtts2si(r, 0, k = kF32);
667+ stack[depth - 1].k := kWord;
605668 end;
606669
607670 { The real of kind k on top, as the other kind }
608671 procedure RealToReal(k: TKind);
672+var r: longint;
609673 begin
610- PopTo(RAX);
611- MovqXR(0, RAX);
674+ r := Top;
675+ MovqXR(0, r);
612676 Sse($5A, 0, 0, k = kF32);
613- MovqRX(RAX, 0);
614- if k = kF32 then Computed(kF64) else Computed(kF32);
677+ MovqRX(r, 0);
678+ if k = kF32 then stack[depth - 1].k := kF64 else stack[depth - 1].k := kF32;
615679 end;
616680
617681 { Whether the slices in the frame at a and b are equal (or not, if not
618682 eq): the lengths, then the bytes, len shl shift of them, compared
619683 right where the comparison stands. }
620684 procedure CompareSeq(a, b: int64; shift: longint; eq: boolean);
621-var differ: longint;
685+var differ, r: longint;
622686 begin
623- Spill;
687+ r := Target;
624688 Load(RCX, Mem(RBP, b + 8), 8, false);
625689 AluM(aluCmp, RCX, Mem(RBP, a + 8));
626690 differ := NoLabel;
@@ -631,7 +695,7 @@
631695 Alu(aluCmp, RCX, RCX); { equal, if there are no bytes }
632696 RepeCmpsb;
633697 FixLink(differ);
634- if eq then SetCC(ccE, RAX) else SetCC(ccNE, RAX);
698+ if eq then SetCC(ccE, r) else SetCC(ccNE, r);
635699 Computed(kWord);
636700 end;
637701
@@ -640,7 +704,8 @@
640704 procedure ErrorText(tmp: int64);
641705 var L: longint;
642706 begin
643- PopTo(RAX);
707+ MovRR(RAX, Top);
708+ Drop;
644709 MovRI(RCX, 0);
645710 L := NoLabel;
646711 TestRR(RAX, RAX);
@@ -655,54 +720,61 @@
655720 { Pops a step and an integer variable's address, and adds the step to
656721 it, or takes it away. }
657722 procedure IncDec(size: longint; signed, dec: boolean);
723+var r, y: longint;
658724 begin
659- PopTo(RCX);
660- PopTo(RDX);
661- Spill;
662- Load(RAX, Mem(RDX, 0), size, signed);
663- if dec then Alu(aluSub, RAX, RCX) else Alu(aluAdd, RAX, RCX);
664- Store(Mem(RDX, 0), RAX, size);
725+ r := Operands(y);
726+ Drop;
727+ Load(RAX, Mem(r, 0), size, signed);
728+ if dec then Alu(aluSub, RAX, y) else Alu(aluAdd, RAX, y);
729+ Store(Mem(r, 0), RAX, size);
665730 end;
666731
667732 { The pointer on top, which NEW got, to size zero bytes unless nil }
668733 procedure ZeroNew(size: int64);
669-var L: longint;
734+var L, r: longint;
670735 begin
671- PopTo(RAX);
736+ r := Top;
672737 L := NoLabel;
673- TestRR(RAX, RAX);
738+ TestRR(r, r);
674739 Jcc(ccE, L);
675- MovRR(RDX, RAX);
676- MovRR(RDI, RAX);
740+ MovRR(RDI, r);
677741 MovRI(RCX, size);
678742 MovRI(RAX, 0);
679743 RepStosb;
680- MovRR(RAX, RDX);
681744 FixLink(L);
682- Computed(kWord);
683745 end;
684746
685-{ CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack }
747+{ CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack: the
748+ result where p was }
686749 procedure Atomic(which: longint);
750+var r, n: longint;
687751 begin
688- if which = bCas then PopTo(RDX);
689- PopTo(RAX);
690- PopTo(RCX);
752+ if which = bCas then begin n := Top; Drop end;
753+ MovRR(RAX, Top);
754+ Drop;
755+ r := Top;
691756 case which of
692- bCas: begin LockCmpxchg(Mem(RCX, 0), RDX); SetCC(ccE, RAX) end;
693- bXchg: Xchg(Mem(RCX, 0), RAX);
694- bXadd: LockXadd(Mem(RCX, 0), RAX);
757+ bCas: begin LockCmpxchg(Mem(r, 0), n); SetCC(ccE, r) end;
758+ bXchg: begin Xchg(Mem(r, 0), RAX); MovRR(r, RAX) end;
759+ bXadd: begin LockXadd(Mem(r, 0), RAX); MovRR(r, RAX) end;
695760 end;
696- Computed(kWord);
761+ stack[depth - 1].k := kWord;
697762 end;
698763
699-{ The system call and its arguments, n values on the stack }
764+{ The system call and its arguments, n values on the stack; every value
765+ in a register is spilled first, as the arguments go in its registers }
700766 procedure SysCall(n: longint);
701-const Regs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9);
767+const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9);
702768 var i: longint;
703769 begin
704- for i := n - 1 downto 0 do PopTo(Regs[i]);
770+ SpillBelow(depth);
771+ for i := 0 to n - 1 do
772+ with stack[depth - n + i] do
773+ if loc = lConst then MovRI(SysRegs[i], val)
774+ else Load(SysRegs[i], Mem(RBP, val), 8, false);
775+ for i := 1 to n do Drop;
705776 x64.Syscall;
777+ MovRR(Target, RAX);
706778 Computed(kWord);
707779 end;
708780
@@ -720,23 +792,27 @@
720792
721793 { Pops a BOOL, and jumps if it is when }
722794 procedure JumpIf(var L: longint; when: boolean);
795+var r: longint;
723796 begin
724- PopTo(RAX);
725- TestRR(RAX, RAX);
797+ r := Top;
798+ Drop;
799+ TestRR(r, r);
726800 if when then Jcc(ccNE, L) else Jcc(ccE, L);
727801 end;
728802
729-{ For and and or: jumps if the BOOL on top is when, keeping it for
730- where the jump goes, and drops it if not. }
803+{ For and and or: jumps if the BOOL on top is when, keeping it, in its
804+ register, for where the jump goes; drops it if not. The values under
805+ it are spilled first, so that both ways find them in the same place. }
731806 procedure JumpKeep(var L: longint; when: boolean);
732807 begin
808+ SpillBelow(depth - 1);
733809 JumpIf(L, when);
734810 end;
735811
736812 { Where JumpKeep went: the BOOL on top is there, as it was there. }
737813 procedure FixKeep(L: longint);
738814 begin
739- TopToRax;
815+ Top;
740816 FixLink(L);
741817 end;
742818
@@ -745,17 +821,11 @@
745821 const
746822 CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE);
747823 CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE);
748-var y: TEntry;
824+var r, y: longint; v: int64;
749825 begin
750- y := stack[depth - 1];
751- if y.isConst then begin
752- dec(depth);
753- PopTo(RAX);
754- CmpI(RAX, y.val);
755- end else begin
756- Operands;
757- Alu(aluCmp, RAX, RCX);
758- end;
826+ if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end
827+ else begin r := Operands(y); Alu(aluCmp, r, y) end;
828+ Drop;
759829 if signed then Jcc(CcSigned[op], L) else Jcc(CcUnsigned[op], L);
760830 end;
761831
@@ -786,7 +856,8 @@
786856 called name (2), or a slice of a pointer a..b that runs backwards
787857 (3). a is in register ra, b in RDX; len is in memory at m, or n if
788858 m.base is NoBase. The failing code has p for its place, and the code
789- after it the place before it again. }
859+ after it the place before it again; it never returns, and may use
860+ any register once it has read what it needs. }
790861 procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint);
791862 var L, line, col: longint; before: TLine;
792863 begin
@@ -815,18 +886,20 @@
815886
816887 { The index on top is below n, unsigned }
817888 procedure CheckIndex(n: int64; const p: TPos);
889+var r: longint;
818890 begin
819- TopToRax;
820- CmpI(RAX, n);
821- Check(ccB, 0, p, RAX, NoMem, n, -1);
891+ r := Top;
892+ CmpI(r, n);
893+ Check(ccB, 0, p, r, NoMem, n, -1);
822894 end;
823895
824896 { The integer on top is a position of an enumeration of n names }
825897 procedure CheckEnum(n: int64; name: longint; const p: TPos);
898+var r: longint;
826899 begin
827- TopToRax;
828- CmpI(RAX, n);
829- Check(ccB, 2, p, RAX, NoMem, n, name);
900+ r := Top;
901+ CmpI(r, n);
902+ Check(ccB, 2, p, r, NoMem, n, name);
830903 end;
831904
832905 { Pops an index, and the address of the slice at a if it is on the
@@ -834,7 +907,8 @@
834907 procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos);
835908 var m, ml: TMem;
836909 begin
837- PopTo(RAX);
910+ MovRR(RAX, Top);
911+ Drop;
838912 m := Place(a, RCX);
839913 ml := m; ml.disp := ml.disp + 8;
840914 if checked then begin
@@ -843,6 +917,7 @@
843917 end;
844918 if size <> 1 then ImulI(RAX, size);
845919 AluM(aluAdd, RAX, m);
920+ MovRR(Target, RAX);
846921 Computed(kWord);
847922 end;
848923
@@ -852,9 +927,11 @@
852927 procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos);
853928 var m, ml: TMem;
854929 begin
855- PopTo(RDX);
856- PopTo(RCX);
857- m := Place(a, R8);
930+ MovRR(RDX, Top);
931+ Drop;
932+ MovRR(RCX, Top);
933+ Drop;
934+ m := Place(a, RDI);
858935 ml := m; ml.disp := ml.disp + 8;
859936 if checked then begin
860937 if isArray then begin CmpI(RDX, n); ml := NoMem end
@@ -863,7 +940,6 @@
863940 Alu(aluCmp, RCX, RDX);
864941 Check(ccBE, 1, p, RCX, ml, n, -1);
865942 end;
866- Spill;
867943 MovRR(RAX, RDX);
868944 Alu(aluSub, RAX, RCX);
869945 Store(Mem(RBP, tmp + 8), RAX, 8);
@@ -877,31 +953,32 @@
877953 bounds can't be known, but it can't run backwards. }
878954 procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos);
879955 begin
880- PopTo(RDX);
881- PopTo(RCX);
882- PopTo(R8);
956+ MovRR(RDX, Top);
957+ Drop;
958+ MovRR(RCX, Top);
959+ Drop;
960+ MovRR(RDI, Top);
961+ Drop;
883962 if checked then begin
884963 Alu(aluCmp, RCX, RDX);
885964 Check(ccLE, 3, p, RCX, NoMem, 0, -1);
886965 end;
887- Spill;
888966 MovRR(RAX, RDX);
889967 Alu(aluSub, RAX, RCX);
890968 Store(Mem(RBP, tmp + 8), RAX, 8);
891969 if size <> 1 then ImulI(RCX, size);
892- MovRR(RAX, R8);
893- Alu(aluAdd, RAX, RCX);
894- Store(Mem(RBP, tmp), RAX, 8);
970+ Alu(aluAdd, RCX, RDI);
971+ Store(Mem(RBP, tmp), RCX, 8);
895972 end;
896973
897974 { Calls }
898975
899976 procedure Call(var c: TCall);
900977 var
901- res: PType; i, ni, nf, k, pad: longint;
978+ res: PType; i, ni, nf, k, pad, r: longint;
902979 onStack: array[0..63] of boolean;
903980 begin
904- Spill; { the value in rax waits on the stack, past the call }
981+ SpillBelow(depth); { the registers in use, saved around the call }
905982 res := c.sig^.base;
906983 c.ret := 0;
907984 ni := 0; nf := 0;
@@ -944,9 +1021,13 @@
9441021 Store(Mem(RBP, c.ret + 8), RDX, 8);
9451022 end;
9461023 if (res <> nil) and (c.ret = 0) then begin
947- if res^.form = fReal then MovqRX(RAX, 0)
948- else if res^.form = fBool then Extend(RAX, 1, false)
949- else if (res^.form = fInt) and (res^.size < 8) then Extend(RAX, res^.size, res^.signed);
1024+ r := Target;
1025+ if res^.form = fReal then MovqRX(r, 0)
1026+ else begin
1027+ MovRR(r, RAX);
1028+ if res^.form = fBool then Extend(r, 1, false)
1029+ else if (res^.form = fInt) and (res^.size < 8) then Extend(r, res^.size, res^.signed);
1030+ end;
9501031 Computed(KindOf(res));
9511032 end;
9521033 end;
@@ -1060,17 +1141,20 @@
10601141 { Pops one of several results, a value of size bytes, into its place
10611142 off bytes into the caller's, behind the hidden pointer. }
10621143 procedure ResultTo(off: int64; size: longint);
1144+var r: longint;
10631145 begin
1064- PopTo(RAX);
1146+ r := Top;
1147+ Drop;
10651148 Load(RCX, Mem(RBP, retSlot), 8, false);
1066- Store(Mem(RCX, off), RAX, size);
1149+ Store(Mem(RCX, off), r, size);
10671150 end;
10681151
10691152 { Pops an address, and copies size bytes from it to the result, off
10701153 bytes into it. }
10711154 procedure CopyResult(off, size: int64);
10721155 begin
1073- PopTo(RSI);
1156+ MovRR(RSI, Top);
1157+ Drop;
10741158 Load(RDI, Mem(RBP, retSlot), 8, false);
10751159 if off <> 0 then Lea(RDI, Mem(RDI, off));
10761160 CopyBytes(size);
@@ -1083,8 +1167,8 @@
10831167 if resType <> nil then
10841168 if IsAggregate(resType) then Load(RAX, Mem(RBP, retSlot), 8, false)
10851169 else begin
1086- PopTo(RAX);
1087- if resType^.form = fReal then MovqXR(0, RAX);
1170+ if resType^.form = fReal then MovqXR(0, Top) else MovRR(RAX, Top);
1171+ Drop;
10881172 end;
10891173 Jmp(exitL);
10901174 end;
@@ -1197,7 +1281,7 @@
11971281 end;
11981282
11991283 initialization
1200- inRax := -1;
1284+ ResetStack(0, 0);
12011285 rtFail := NewChunk(secText, Intern('Rt', 2), Intern('fail', 4));
12021286 argcChunk := NewChunk(secBss, -1, Intern('argc', 4));
12031287 SetBss(argcChunk, 8, 8);

test/run/Deep.expect (history) added +10 -0

@@ -0,0 +1,10 @@
1+-10
2+-95
3+3
4+99 50 2 16 1
5+9
6+23
7+237037034815
8+-2147483648 7
9+-803
10+10

test/run/Deep.raz (history) added +57 -0

@@ -0,0 +1,57 @@
1+module Deep;
2+import Out;
3+
4+(* Expressions deeper than the registers of the stack, with calls,
5+ and, or, constants on the left and system calls among them. *)
6+
7+var g: I64;
8+
9+proc f(x: I64): I64;
10+begin
11+ g := g + 1;
12+ return x * 3;
13+end;
14+
15+proc b(x: BOOL): BOOL;
16+begin
17+ g := g + 10;
18+ return x;
19+end;
20+
21+proc n(x: BOOL): I64;
22+begin
23+ if x then return 1; end;
24+ return 0;
25+end;
26+
27+proc main*;
28+var a, c, d, e: I64; s: [8]I64; t: BOOL; k: I32; u: U8; p: ^I64;
29+begin
30+ a := 1; c := 2; d := 3; e := 4;
31+ (* nine values deep, right-nested *)
32+ Out.int(a - (c - (d - (e - (a - (c - (d - (e - (a - 7))))))))); Out.ln;
33+ (* calls under values held in registers *)
34+ Out.int(a + (c * (d + f(e + f(a) * (c - f(d)))))); Out.ln;
35+ Out.int(g); Out.ln;
36+ (* constants on the left of operators that don't commute *)
37+ Out.int(100 - a); Out.str(" "); Out.int(100 div c); Out.str(" ");
38+ Out.int(100 mod (d + e)); Out.str(" "); Out.int(1 shl e); Out.str(" ");
39+ Out.int(n(-1000000000000 < a)); Out.ln;
40+ (* and, or over values held below them *)
41+ t := (a + c > d) and b(c < d) or b(false);
42+ Out.int(a + c * (d + n(t or b(true)))); Out.ln;
43+ Out.int(g); Out.ln;
44+ (* a constant too big for an instruction's operand *)
45+ Out.int(a + 123456789012 * c - 9876543210); Out.ln;
46+ (* narrow integers, division of their smallest by -1 aside *)
47+ k := 2147483647; k := k + I32(a); Out.int(I64(k)); Out.str(" ");
48+ u := 250; u := u + U8(c * 5) + U8(d); Out.int(I64(u)); Out.ln;
49+ (* array elements and pointers among deep values *)
50+ for d := 0 to 7 do s[d] := d * d; end;
51+ p := ADR(s[3]);
52+ Out.int(s[a] + (s[c] * (s[e] - (p^ + s[s[2]] * (s[7] - s[6]))))); Out.ln;
53+ (* a system call under values *)
54+ Out.int(a + (c * n(SYSCALL(39) > 0) + d)); Out.ln;
55+end;
56+
57+end Deep.