raz / src / Gen.x86_64.raz

2863 lines · 101.5 KiB · raw · history · last change e99cab9b

1(* Gen: the code generator, for x86-64 (Gen.x86_64.raz; another machine has
2 a Gen of its own, chosen by the suffix of its file, doc/raz.md, 3).
3
4 Item and Parse drive a stack machine: they push constants, addresses
5 and the values of variables, operate on the values on top, store
6 them and jump on them. Every value has a kind: a word, which is an
7 integer as big as an address, a pointer, a BOOL, a set or a narrower
8 integer; a long, a 64-bit integer on a machine whose word is smaller
9 (never here); an F32; an F64. This unit makes the machine's code for
10 those calls, and nothing above it knows the machine: a port is a new
11 Gen, with an encoder of its own.
12
13 The stack is kept in registers, as in Wirth's compilers (Niklaus
14 Wirth's Pascal and Oberon compilers): the value at depth d, depth 0
15 being the bottom, is in r8 to r11, register d mod 4, or in xmm8 to
16 xmm11 if it is a real. A value that needs a register a lower one
17 holds spills that one: stores it in a frame slot, to free the
18 register. A constant, or an address in the frame or of a chunk, is
19 only noted until something needs it in a register, so that it can be
20 an instruction's operand.
21
22 A slice is two words, a pointer and then a length.
23
24 Calls use the System V convention, which x86-64 Linux has for C:
25 arguments in rdi, rsi, rdx, rcx, r8, r9 and xmm0 to xmm7, the rest on
26 the machine's stack; rbx, rbp and r12 to r15 are callee-saved, which
27 is to say a called procedure gives them back as it found them. The
28 arguments are computed left to right onto the stack of values, and
29 moved into their registers just before the call: a call in a later
30 argument spills the earlier ones, as any call spills the values under
31 it, so that it can't disturb them. A slice goes as its two words, as
32 System V passes a 16-byte struct of two integers; a record, array or
33 variant passed by value goes as the address of a copy the caller
34 makes. A slice, or two results that are single values, come back in
35 rax and rdx (inTwoRegs); any other record, or several results, comes
36 back in memory the caller provides, through a hidden pointer
37 (behindPointer), where C would send a small struct back in registers.
38
39 A procedure's frame holds its parameters, its variables, and the
40 temporaries of the statement running, and the results of its calls
41 that hold an array (report 8). The variables are zeroed on
42 entry.
43
44 Depends on: Scan and Diag for errors, Sym for types, Chunk for code
45 and data, X64 for instructions.
46 Leaves out: what the program means, its types and their checks; that
47 is Item.
48
49 The seed's `Mark` in the middle of a push or a check can't give an
50 error back, so it is shown and the call goes on without doing what
51 it could not (see newEntry and check): the compile ends with the
52 message, and the parser's recovery makes the stack right again with
53 resetStack. A full stack (newEntry) ends the compile at once: the pops
54 after it would go below the stack. endProc returns its error, for the
55 parser to pass up.
56
57 What the parser sets, `inPlace`, has a setter, as the exported
58 variables can't be set from outside. The seed's types lose their T
59 (`TAdr` is `Adr`), and its `Call` record, which the procedure `call`
60 takes, is `CallInfo`. *)
61module Gen;
62import Fmt, Diag, Scan, Sym, Chunk, X := X64;
63import SYSTEM;
64
65const
66 Arch* = "x86_64"; (* the target's machine, as a file's suffix says it *)
67 NoLabel* = -1;
68 WordSize* = 8; (* the size of an address, and of INT *)
69 (* What the machine catches by itself, which the code needn't test for
70 (report section 10): on x86-64 Linux, a division by zero, a stack
71 overflow (at the guard page under the stack), a call to nil, and
72 a load or store within NilPage bytes of address 0, which is never
73 mapped. *)
74 NilPage* = 4096;
75
76 RAX = X.RAX; RCX = X.RCX; RDX = X.RDX; RSP = X.RSP; RBP = X.RBP;
77 RSI = X.RSI; RDI = X.RDI; R8 = X.R8; R9 = X.R9; R10 = X.R10; R11 = X.R11;
78
79type
80 Kind* = (kWord, kLong, kF32, kF64);
81
82 (* Where a variable is: its address is relative to the frame, to a
83 chunk, to an address on the stack, or to one held in a frame slot *)
84 Base* = (bFrame, bGlobal, bStack, bSlot, bReg, bPtr);
85 Adr* = record
86 base*: Base;
87 chunk*: INT; (* bGlobal: the chunk; bSlot: the frame slot; bReg: the register;
88 bPtr: the register that holds the address *)
89 off*: I64; (* added to the address; for bReg, which word, 0 or 8 *)
90 hi*: INT; (* bReg, a slice: the register of its length, the
91 second word; chunk is that of its pointer *)
92 end;
93
94 (* The code made so far, to go back to for a second try at a body *)
95 CodeMark* = record pc*, fixups*, lines*, said*, fails*, exit*: INT; end;
96
97 (* For entryPoint: the address of chunk, put in the variable whose
98 chunk is into before the program starts; or, for C's symbols, the
99 address in chunk, the quadword the loader fills in *)
100 Link* = record chunk*, into*: INT; end;
101
102 CallInfo* = record
103 sig*: ^Sym.Type;
104 param*: ^Sym.Obj; (* the parameter the next argument is for *)
105 obj*: ^Sym.Obj; (* the procedure called, if it has a name *)
106 n*: INT; (* the arguments *)
107 isF*: [64]BOOL; (* ... goes in an xmm register *)
108 pair*: [64]BOOL; (* ... is a slice's first word, the next its second *)
109 target*: I64; (* the frame slot of a procedure value, if chunk < 0 *)
110 chunk*: INT; (* else the procedure's chunk *)
111 ext*: BOOL; (* C's: called through the chunk holding its address *)
112 ret*: I64; (* set by call: the temporary a slice or a record result is in *)
113 end;
114
115const
116 (* the registers a procedure keeps its busiest variables in: System V's
117 callee-saved ones, so they live through calls (a register variable);
118 a procedure saves those it uses, and Rt finds them there for the
119 trap report *)
120 VarRegs*: [5]INT = [X.RBX, X.R12, X.R13, X.R14, X.R15];
121 (* the registers the trap report can show a variable in: VarRegs, then
122 rdi and rsi, where a procedure that calls nothing keeps the
123 parameters that arrive in them (keptIn) *)
124 ShownRegs*: [7]INT = [X.RBX, X.R12, X.R13, X.R14, X.R15, X.RDI, X.RSI];
125 (* the registers a procedure keeps its busiest reals in, xmm12 to
126 xmm15, numbered from XmmVar up so that they aren't taken for r12 to
127 r15 (regAt); and in a procedure that calls nothing, which has no
128 real parameter in them, xmm4 to xmm7 too, as nothing else uses
129 them there. Each has a slot too, written as it is, where the trap
130 report finds it, and from which it is loaded again after a call, as
131 System V has no callee-saved xmm register (xHome). *)
132 XmmVar* = 16;
133 XVarRegs*: [8]INT = [XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15,
134 XmmVar + 4, XmmVar + 5, XmmVar + 6, XmmVar + 7];
135 (* a procedure's registers of variables, as bits: bit i is VarRegs[i],
136 bit XBit + i XVarRegs[i] *)
137 XBit* = 5;
138
139var
140 rtFail*, argcChunk*, argvChunk*: INT;
141 (* the stubs that check's code calls, one for each kind of failure,
142 made by entryPoint for those used *)
143 failStubs: [8]INT;
144 lost*: BOOL; (* a parameter kept where it arrived was wanted
145 for other work: the procedure is compiled again,
146 the parameter in memory *)
147 inPlace: INT; (* the register of a variable that the statement
148 being compiled assigns, x := x + e, where the
149 value can be worked out in the variable's own
150 register, from its first push on (lInto) *)
151 depth*: INT; (* the values on the stack *)
152 pushed*: INT; (* quadwords pushed on the machine's stack *)
153 codeChunk*: INT; (* the procedure or body being compiled, or -1 *)
154
155type
156 (* where a value on the stack is *)
157 Loc = (lConst, (* not loaded: val is the value *)
158 lHeld, (* not loaded: in register val, a register
159 variable's own (see VarRegs), read in place, which
160 nothing in an expression can change *)
161 lAdr, (* not loaded: the address of chunk, or of the frame
162 if chunk is -1, plus val *)
163 lIdx, (* not loaded: the address m, an element of an array
164 or of a slice in registers: the array's or the
165 pointer's register, or rbp, as the base, and the
166 index in a register variable's or the value's
167 own register (indexSlice, indexArray) *)
168 lMem, (* not loaded: the value of size bytes at m, in the
169 frame, a chunk, or at an address lAdr or lIdx
170 noted, read where it is used (pushVar) *)
171 lInto, (* in register val, a register variable's own, which
172 is what the value goes to (inPlace): so it is
173 changed in place *)
174 lReg, (* in its register *)
175 lFrame); (* spilled: in the frame slot val *)
176
177 Entry = record
178 loc: Loc;
179 val: I64; (* lConst: the value; lHeld: the register; lAdr: the
180 offset; lFrame: the frame slot *)
181 chunk: INT; (* lAdr: the chunk, or -1 for the frame *)
182 m: X.Mem; (* lIdx, lMem *)
183 size: INT; (* lMem *)
184 signed: BOOL; (* lMem *)
185 k: Kind;
186 off: I64; (* lHeld, lReg: a word, its register plus off, the
187 constant of x + 1 not yet added (op), so that an
188 operand that uses it can take it as its
189 displacement: [r13 + r9 * 8 - 8] *)
190 end;
191
192 (* a check of the procedure, whose failure endFails makes *)
193 Fail = record
194 L, what, ra, name: INT;
195 path, line, col: INT; (* the place, told now: an inlined procedure's
196 is in its own file (Parse's inlined) *)
197 m: X.Mem;
198 n: I64;
199 end;
200
201const
202 MaxStack = 1024; (* values waiting in one expression: 63 nested calls of up to 15 arguments each *)
203 StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"];
204 (* the registers of the stack: the value at depth d is in Regs[d mod
205 NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi
206 and rdi, which copying needs, aren't among them. *)
207 NumRegs = 4;
208 Regs: [NumRegs]INT = [R8, R9, R10, R11];
209 XRegs: [NumRegs]INT = [8, 9, 10, 11]; (* xmm8 to xmm11 *)
210 IntRegs: [6]INT = [RDI, RSI, RDX, RCX, R8, R9];
211
212var
213 stack: [MaxStack]Entry;
214 owner: [NumRegs]INT; (* the value in each register, or -1 *)
215 frameTop, frameMax, permTop: I64;
216 frameFix: INT; (* where the frame size is patched in *)
217 paramTop: I64; (* the parameters' slots end here *)
218 zeroTop: I64; (* the locals the prologue zeroes end here *)
219 retSlot: I64; (* holds the hidden result pointer *)
220 resType: ^Sym.Type; (* the procedure's result, or nil *)
221 exitL: INT;
222 (* parameters arriving in registers: where each is stored on entry *)
223 inReg: [14]INT;
224 inSlot: [14]I64;
225 inSize: [14]INT;
226 inObj: [14]^Sym.Obj;
227 numIn: INT;
228 procParams: ^Sym.Obj; (* the parameters, for enterBody *)
229 regMask: INT; (* the registers of variables, as bits (XBit) *)
230 kept: INT; (* bit r: register r holds a parameter, as it arrived *)
231 saveAt: I64; (* where they are saved, a quadword each *)
232 homes: [8]I64; (* the slots of the variables in XVarRegs *)
233 frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *)
234 fails: [4096]Fail;
235 numFails: INT;
236
237(* -S: the text of each instruction is kept, and list prints it. Raz asks
238 Gen, not the encoder, so that no unit but Gen names the machine. *)
239proc setListing*(yes: BOOL);
240begin
241 X.setListing(yes);
242end;
243
244proc list*(home: INT);
245begin
246 X.list(home);
247end;
248
249proc setInPlace*(r: INT);
250begin
251 inPlace := r;
252end;
253
254proc kindOf*(t: ^Sym.Type): Kind;
255begin
256 if t^.form = Sym.fReal then
257 if t^.size = 4 then return kF32; end;
258 return kF64;
259 end;
260 if t^.size > WordSize then return kLong; end;
261 return kWord;
262end;
263
264(* The stack, as in Wirth's compilers: the value at depth d is held in
265 register d mod NumRegs. A value, when a higher one wants its register,
266 and every value in a register when a call would clobber them, is
267 spilled to a frame slot, and loaded again where it is used. A constant
268 is only noted.
269
270 drop emits no code, and a dropped value's register still holds it
271 until the next push claims it: place (for bStack), operands,
272 indexSlice and jumpCmp read a register after dropping its value.
273
274 Registers outside the stack, free within one operation: rax, rcx and
275 rdx for arithmetic; rsi and rdi for copying; and, where the stack holds
276 nothing in them, r10 for the prologue's zeroing loop and r11 for a call
277 through a procedure value; xmm0 and xmm1 for reals.
278
279 A real, an F32 or an F64, is held in xmm8 to xmm11 (XRegs), where the
280 machine computes with reals, the value at depth d in XRegs[d mod
281 NumRegs]: so each depth has two registers, and holds its value in the
282 one of its kind, which owner doesn't tell apart. The xmm registers
283 from xmm8 up carry no arguments, so that call can move a real into
284 xmm0 to xmm7 without overwriting another still to go. *)
285
286proc fits32(v: I64): BOOL;
287begin
288 return (v >= -2147483648) and (v <= 2147483647);
289end;
290
291(* The register of kind k for the value at depth i *)
292proc regIn(k: Kind; i: INT): INT;
293begin
294 if k in {kF32, kF64} then return XRegs[i mod NumRegs]; end;
295 return Regs[i mod NumRegs];
296end;
297
298proc regOf(i: INT): INT;
299begin
300 return regIn(stack[i].k, i);
301end;
302
303proc isReal(i: INT): BOOL;
304begin
305 return stack[i].k in {kF32, kF64};
306end;
307
308(* A real constant, its bits v, as an operand: 8 bytes in read-only
309 data, as the machine has no instruction that holds one *)
310proc realConst(v: I64): X.Mem;
311var c: INT;
312begin
313 c := Chunk.newChunk(Chunk.secRodata, -1, -1);
314 Chunk.setData(c, ^U8(SYSTEM.ADR(v)), 8, 8);
315 return X.memRip(c, 0);
316end;
317
318(* The real i into xmm register x, from wherever it is, as valueTo does
319 for an integer register *)
320proc xValueTo(i, x: INT);
321begin
322 match stack[i].loc of
323 | lConst: X.movsXM(x, realConst(stack[i].val), stack[i].k = kF32);
324 | lMem: X.movsXM(x, stack[i].m, stack[i].size = 4);
325 | lHeld: if INT(stack[i].val) # x then X.movX(x, INT(stack[i].val)); end;
326 | lInto: if INT(stack[i].val) # x then X.movX(x, INT(stack[i].val)); end;
327 | lReg: if regOf(i) # x then X.movX(x, regOf(i)); end;
328 | else: X.movsXM(x, X.mem(RBP, stack[i].val), false); (* spilled, as 8 bytes *)
329 end;
330end;
331
332proc loaded(i: INT): INT; forward;
333proc allocPerm*(size, align: I64): I64; forward;
334proc allocTemp*(size, align: I64): I64; forward;
335
336(* The value i, in a register, goes to a frame slot; one noted that
337 holds its register, as an index, is worked out in it first *)
338proc spill(i: INT);
339var s: I64;
340begin
341 if (stack[i].loc # lReg) or (stack[i].off # 0) then _ := loaded(i); end;
342 s := allocTemp(8, 8);
343 if isReal(i) then X.movsMX(X.mem(RBP, s), regOf(i), false);
344 else X.store(X.mem(RBP, s), regOf(i), 8); end;
345 stack[i].loc := lFrame;
346 stack[i].val := s;
347 owner[i mod NumRegs] := -1;
348end;
349
350(* Each value below depth d read from memory now, as what comes next
351 may change it *)
352proc readMems(d: INT);
353var i: INT;
354begin
355 for i := 0 to d - 1 do
356 if stack[i].loc = lMem then _ := loaded(i); end;
357 end;
358end;
359
360(* Every value below depth d in a register, spilled; one noted in memory
361 is read first, as a call may change it *)
362proc spillBelow(d: INT);
363var r: INT;
364begin
365 readMems(d);
366 for r := 0 to NumRegs - 1 do
367 if (owner[r] >= 0) and (owner[r] < d) then spill(owner[r]); end;
368 end;
369end;
370
371(* The register of kind k of value i, for it to hold: a lower value at
372 its depth's registers is spilled. *)
373proc claim(i: INT; k: Kind): INT;
374var r: INT;
375begin
376 r := i mod NumRegs;
377 if (owner[r] >= 0) and (owner[r] # i) then spill(owner[r]); end;
378 owner[r] := i;
379 return regIn(k, i);
380end;
381
382(* The memory at the noted address i, plus off *)
383proc adrMem(i: INT; off: I64): X.Mem;
384begin
385 if stack[i].chunk < 0 then return X.mem(RBP, stack[i].val + off); end;
386 return X.memRip(stack[i].chunk, INT(stack[i].val + off));
387end;
388
389(* The value i, in its register *)
390proc loaded(i: INT): INT;
391var r: INT;
392begin
393 if stack[i].loc = lInto then return INT(stack[i].val); end;
394 if (stack[i].loc # lReg) and isReal(i) then
395 xValueTo(i, claim(i, stack[i].k));
396 stack[i].loc := lReg;
397 elsif stack[i].loc # lReg then
398 r := claim(i, kWord);
399 match stack[i].loc of
400 | lConst: X.movRI(r, stack[i].val);
401 | lHeld: X.movRR(r, INT(stack[i].val));
402 | lAdr: X.lea(r, adrMem(i, 0));
403 | lIdx: X.lea(r, stack[i].m);
404 | lMem: X.load(r, stack[i].m, stack[i].size, stack[i].signed);
405 | else: X.load(r, X.mem(RBP, stack[i].val), 8, false);
406 end;
407 stack[i].loc := lReg;
408 end;
409 if stack[i].off # 0 then
410 X.lea(regOf(i), X.mem(regOf(i), stack[i].off));
411 stack[i].off := 0;
412 end;
413 return regOf(i);
414end;
415
416(* The register value i is in, only to be read: a variable's own, or
417 its register *)
418proc peek(i: INT): INT;
419begin
420 if (stack[i].loc = lHeld) and (stack[i].off = 0) then return INT(stack[i].val); end;
421 return loaded(i);
422end;
423
424(* The offset of value i, taken from it, for the caller to add or to
425 make a displacement of *)
426proc takeOff(i: INT): I64;
427var o: I64;
428begin
429 o := stack[i].off;
430 stack[i].off := 0;
431 return o;
432end;
433
434(* The word on top, in a register, has o added: noted, if o can be a
435 displacement, else added now *)
436proc putOff(o: I64);
437begin
438 if fits32(stack[depth - 1].off + o) then
439 stack[depth - 1].off := stack[depth - 1].off + o;
440 else
441 X.movRI(RCX, o);
442 X.alu(X.aluAdd, loaded(depth - 1), RCX);
443 end;
444end;
445
446proc top: INT;
447begin
448 if depth = 0 then Diag.fatal("internal error: the stack is empty"); end;
449 return loaded(depth - 1);
450end;
451
452(* A value on the stack's top. At the stack's last place the error is shown
453 and the compile ends there: with nothing pushed, the pops after it would
454 go below the stack. See the head of this unit. *)
455proc newEntry(k: Kind; loc: Loc; v: I64);
456begin
457 if depth > LEN(stack) - 1 then
458 _ := Scan.mark(Diag.say("more than {} values waiting in one expression", [Fmt.int(MaxStack)]));
459 Diag.finish(); (* ends the compile: an error was counted *)
460 return;
461 end;
462 stack[depth].k := k;
463 stack[depth].loc := loc;
464 stack[depth].val := v;
465 stack[depth].chunk := -1;
466 stack[depth].off := 0;
467 INC(depth);
468end;
469
470(* An address, lIdx, or a value at it, lMem, noted as the operand m. Its
471 index may be in the register of the value just dropped, at this
472 depth: it is the new value's own, then. *)
473proc newMem(loc: Loc; m: X.Mem; k: Kind; size: INT; signed: BOOL);
474begin
475 newEntry(k, loc, 0);
476 stack[depth - 1].m := m;
477 stack[depth - 1].size := size;
478 stack[depth - 1].signed := signed;
479 if m.index = Regs[(depth - 1) mod NumRegs] then owner[(depth - 1) mod NumRegs] := depth - 1; end;
480end;
481
482(* The register a new value on top is to be computed in *)
483proc target: INT;
484begin
485 return claim(depth, kWord);
486end;
487
488(* The xmm register a new real on top is to be computed in *)
489proc xTarget: INT;
490begin
491 return claim(depth, kF64);
492end;
493
494(* The value just computed in target goes on top *)
495proc computed(k: Kind);
496begin
497 newEntry(k, lReg, 0);
498end;
499
500proc drop*;
501begin
502 DEC(depth);
503 if owner[depth mod NumRegs] = depth then owner[depth mod NumRegs] := -1; end;
504end;
505
506(* rsi and rdi are about to be used for other work, as in copying: a
507 parameter kept in either must go to memory (lost). *)
508proc clobberArgs;
509begin
510 if kept # 0 then lost := true; end;
511end;
512
513(* The value i into register r, from wherever it is: a constant or an
514 address is made there, a variable's register or the value's own
515 register copied, a spilled value loaded. Unlike loaded, it leaves the
516 value where it was, so r is its only new copy. *)
517proc valueTo(i, r: INT);
518begin
519 if isReal(i) and (stack[i].loc in {lHeld, lInto}) then
520 X.movqRX(r, INT(stack[i].val));
521 else
522 match stack[i].loc of
523 | lConst: X.movRI(r, stack[i].val);
524 | lHeld: X.movRR(r, INT(stack[i].val));
525 | lInto: X.movRR(r, INT(stack[i].val));
526 | lAdr: X.lea(r, adrMem(i, 0));
527 | lIdx: X.lea(r, stack[i].m);
528 | lMem: X.load(r, stack[i].m, stack[i].size, stack[i].signed);
529 | lReg:
530 if isReal(i) then X.movqRX(r, regOf(i));
531 elsif regOf(i) # r then X.movRR(r, regOf(i)); end;
532 | else: X.load(r, X.mem(RBP, stack[i].val), 8, false);
533 end;
534 end;
535 if stack[i].off # 0 then X.lea(r, X.mem(r, stack[i].off)); end;
536end;
537
538(* The value on top, popped into register r *)
539proc popTo(r: INT);
540begin
541 valueTo(depth - 1, r);
542 drop();
543end;
544
545(* Whether the value on top is a constant, and if so, which *)
546proc topConst: (BOOL, I64);
547begin
548 return stack[depth - 1].loc = lConst, stack[depth - 1].val;
549end;
550
551proc resetStack*(d, p: INT);
552var r: INT;
553begin
554 depth := d;
555 pushed := p;
556 for r := 0 to NumRegs - 1 do owner[r] := -1; end;
557end;
558
559proc pushConst*(k: Kind; v: I64);
560begin
561 newEntry(k, lConst, v);
562end;
563
564(* The register of the word at a, a variable in registers: the variable's
565 own, or for a slice its pointer's (off 0) or its length's (off 8) *)
566proc regAt(a: Adr): INT;
567var r: INT;
568begin
569 if a.off = 0 then r := a.chunk; else r := a.hi; end;
570 if r >= XmmVar then r := r - XmmVar; end; (* an xmm register, XVarRegs *)
571 return r;
572end;
573
574(* The memory a names. An address on the stack is popped, and is in its
575 register still; one in a slot is loaded into r. *)
576proc place(a: Adr; r: INT): X.Mem;
577var m: X.Mem; ra: INT; o: I64;
578begin
579 if a.base = bReg then Diag.fatal("internal error: a register has no address"); end;
580 match a.base of
581 | bFrame: return X.mem(RBP, a.off);
582 | bGlobal: return X.memRip(a.chunk, INT(a.off));
583 | bStack:
584 if stack[depth - 1].loc = lAdr then
585 m := adrMem(depth - 1, a.off);
586 drop();
587 return m;
588 elsif stack[depth - 1].loc = lIdx then
589 m := stack[depth - 1].m;
590 m.disp := m.disp + INT(a.off);
591 drop();
592 return m;
593 end;
594 (* an offset noted on the address is the operand's, as
595 p^.f + 8: [r8 + 8] *)
596 if fits32(a.off + stack[depth - 1].off) then o := takeOff(depth - 1); else o := 0; end;
597 ra := peek(depth - 1);
598 drop();
599 return X.mem(ra, a.off + o);
600 | bPtr: return X.mem(a.chunk, a.off);
601 | else: (* bSlot: the address is in the frame slot a.chunk *)
602 X.load(r, X.mem(RBP, a.chunk), 8, false);
603 return X.mem(r, a.off);
604 end;
605end;
606
607(* The address a names, on top of the stack *)
608proc pushAdr*(a: Adr);
609var m: X.Mem; r: INT;
610begin
611 if (a.base = bStack) and (a.off = 0) then return; end; (* it is on top already *)
612 if a.base in {bFrame, bGlobal} then
613 newEntry(kWord, lAdr, a.off);
614 if a.base = bGlobal then stack[depth - 1].chunk := a.chunk; end;
615 return;
616 end;
617 (* The order matters. With the address on the stack, place pops it and
618 must come first, so that target may reuse its register; otherwise
619 target comes first, since place may need r to load a slot's pointer. *)
620 if a.base = bStack then
621 m := place(a, -1); r := target();
622 else
623 r := target(); m := place(a, r);
624 end;
625 (* an address that is already in r, [r], needs no instruction *)
626 if (m.base # r) or (m.index # X.NoIndex) or (m.disp # 0) then X.lea(r, m); end;
627 computed(kWord);
628end;
629
630proc pushVar*(a: Adr; size: INT; signed: BOOL; k: Kind);
631var m: X.Mem; r: INT;
632begin
633 r := inPlace;
634 inPlace := -1;
635 if r >= XmmVar then r := r - XmmVar; end;
636 if (a.base = bReg) and (regAt(a) = r) then
637 newEntry(k, lInto, r);
638 return;
639 end;
640 if a.base = bReg then
641 newEntry(k, lHeld, regAt(a));
642 return;
643 end;
644 (* a variable in the frame or a chunk, or at an address only noted, is
645 only noted too: read where it is used, as an operand if it can be *)
646 if (a.base in {bFrame, bGlobal, bPtr}) or (a.base = bStack) and (stack[depth - 1].loc in {lAdr, lIdx}) then
647 m := place(a, -1);
648 newMem(lMem, m, k, size, signed);
649 return;
650 end;
651 (* in the order pushAdr explains *)
652 if a.base = bStack then
653 m := place(a, -1); r := target();
654 else
655 r := target(); m := place(a, r);
656 end;
657 if k in {kF32, kF64} then X.movsXM(xTarget(), m, size = 4);
658 else X.load(r, m, size, signed); end;
659 computed(k);
660end;
661
662proc pushProc*(c: INT; ext: BOOL);
663var r: INT;
664begin
665 r := target();
666 if ext then X.load(r, X.memRip(c, 0), 8, false);
667 else X.lea(r, X.memRip(c, 0)); end;
668 computed(kWord);
669end;
670
671(* Where xmm register r is in XVarRegs *)
672proc xIndex(r: INT): INT;
673begin
674 if r >= XVarRegs[0] then return r - XVarRegs[0]; end;
675 return r - XVarRegs[4] + 4;
676end;
677
678(* The slot of the variable in xmm register x *)
679proc homeOf(x: INT): X.Mem;
680begin
681 return X.mem(RBP, homes[xIndex(x + XmmVar)]);
682end;
683
684(* Pops a value into the variable at a; an address on the stack is under
685 the value. *)
686proc popVar*(a: Adr; size: INT);
687var r: INT; v: I64; c, fl: BOOL;
688begin
689 if (a.base = bReg) and isReal(depth - 1) then
690 xValueTo(depth - 1, regAt(a));
691 drop();
692 X.movsMX(homeOf(regAt(a)), regAt(a), false); (* written through, as xHome says *)
693 return;
694 end;
695 if a.base = bReg then
696 c, v := topConst();
697 if c then X.movRI(regAt(a), v);
698 elsif peek(depth - 1) # regAt(a) then X.movRR(regAt(a), peek(depth - 1)); end; (* else it is there, lInto *)
699 drop();
700 return;
701 end;
702 c, v := topConst();
703 if c and fits32(v) then
704 drop();
705 X.storeI(place(a, RCX), INT(v), size);
706 return;
707 end;
708 if c then r := RAX; X.movRI(RAX, v);
709 else r := peek(depth - 1); end;
710 fl := (stack[depth - 1].loc # lConst) and isReal(depth - 1);
711 drop();
712 if fl then X.movsMX(place(a, RCX), r, size = 4);
713 else X.store(place(a, RCX), r, size); end;
714end;
715
716(* Register src to dst, an xmm register's if fl *)
717proc movReg(fl: BOOL; dst, src: INT);
718begin
719 if fl then X.movX(dst, src); else X.movRR(dst, src); end;
720end;
721
722(* The two values on top, of one kind, change places. *)
723proc swap*;
724var e: Entry; x, y, t: INT; rx, ry, fl: BOOL;
725begin
726 x := depth - 2; y := depth - 1;
727 (* a value noted at an address that uses its register is worked out,
728 so that its register moves with it *)
729 if stack[x].loc in {lIdx, lMem} then _ := loaded(x); end;
730 if stack[y].loc in {lIdx, lMem} then _ := loaded(y); end;
731 rx := stack[x].loc = lReg;
732 ry := stack[y].loc = lReg;
733 fl := isReal(y);
734 if fl then t := 0; else t := RAX; end; (* xmm0, or rax *)
735 if rx and ry then
736 movReg(fl, t, regOf(x));
737 movReg(fl, regOf(x), regOf(y));
738 movReg(fl, regOf(y), t);
739 elsif rx then
740 owner[x mod NumRegs] := -1;
741 movReg(fl, claim(y, stack[y].k), regOf(x));
742 elsif ry then
743 owner[y mod NumRegs] := -1;
744 movReg(fl, claim(x, stack[x].k), regOf(y));
745 end;
746 e := stack[x]; stack[x] := stack[y]; stack[y] := e;
747end;
748
749(* Copying *)
750
751(* The most that copyBytes and zeroBytes do with moves, in place of rep
752 movsb and rep stosb, which take some twenty cycles to start, longer
753 than the few moves take. *)
754const MaxMoves = 256;
755
756(* Up to this size a copy goes as words: a record just made field by
757 field is read back by loads as wide as its stores, and a 16-byte
758 load across two 8-byte stores waits for them to reach the cache. *)
759const MaxWordMoves = 64;
760
761(* size bytes from [rsi] to [rdi], through xmm0 and rcx; rsi and rdi are
762 left as they fall. Up to MaxMoves bytes, as a record or a small array
763 mostly is, they go as moves: of 16 bytes from MaxWordMoves up, then
764 of 8, 4, 2 and 1. *)
765proc copyBytes(size: I64);
766var o, n: I64;
767begin
768 if size > MaxMoves then
769 X.movRI(RCX, size);
770 X.repMovsb();
771 return;
772 end;
773 o := 0;
774 while (size > MaxWordMoves) and (o + 16 <= size) do
775 X.movdquXM(0, X.mem(RSI, o));
776 X.movdquMX(X.mem(RDI, o), 0);
777 INC(o, 16);
778 end;
779 while o < size do
780 n := 8;
781 while n > size - o do n := n div 2; end;
782 X.load(RCX, X.mem(RSI, o), INT(n), false);
783 X.store(X.mem(RDI, o), RCX, INT(n));
784 INC(o, n);
785 end;
786end;
787
788(* Pops a source's address, and a destination's under it, and copies
789 size bytes to off bytes past the destination. *)
790proc copyBlock*(size, off: I64);
791begin
792 clobberArgs();
793 popTo(RSI);
794 popTo(RDI);
795 if off # 0 then X.lea(RDI, X.mem(RDI, off)); end;
796 copyBytes(size);
797end;
798
799(* Pops a source's address, and copies size bytes to a: for a slice in
800 registers, its two words into them. *)
801proc copyTo*(a: Adr; size: I64);
802begin
803 clobberArgs();
804 popTo(RSI);
805 if a.base = bReg then
806 X.load(a.chunk, X.mem(RSI, 0), 8, false);
807 X.load(a.hi, X.mem(RSI, 8), 8, false);
808 return;
809 end;
810 X.lea(RDI, place(a, RDI));
811 copyBytes(size);
812end;
813
814(* size zero bytes at [rdi], through xmm0 and rax; rdi is left as it
815 falls. Up to MaxMoves bytes they go as stores of 16, then of 8, 4, 2
816 and 1 bytes, as copyBytes does. *)
817proc zeroBytes(size: I64);
818var o, n: I64;
819begin
820 if size > MaxMoves then
821 X.movRI(RAX, 0);
822 X.movRI(RCX, size);
823 X.repStos(1);
824 return;
825 end;
826 o := 0;
827 if size >= 16 then X.pxorXX(0); end;
828 while o + 16 <= size do
829 X.movdquMX(X.mem(RDI, o), 0);
830 INC(o, 16);
831 end;
832 if o < size then X.movRI(RAX, 0); end;
833 while o < size do
834 n := 8;
835 while n > size - o do n := n div 2; end;
836 X.store(X.mem(RDI, o), RAX, INT(n));
837 INC(o, n);
838 end;
839end;
840
841proc zeroVar*(a: Adr; size: I64);
842begin
843 clobberArgs();
844 X.lea(RDI, place(a, RDI));
845 zeroBytes(size);
846end;
847
848(* Operations *)
849
850(* n if v is 2^n, which is 0 for 1; -1 if v isn't a power of two *)
851proc exactLog2(v: I64): INT;
852var n: INT;
853begin
854 n := 0;
855 while (v > 1) and ((v & 1) = 0) do v := v shr 1; INC(n); end;
856 if v # 1 then n := -1; end;
857 return n;
858end;
859
860(* r times v, a size: by a shift where v is a power of two, as it
861 mostly is, since a shift takes one cycle and imul three *)
862proc mulI(r: INT; v: I64);
863begin
864 if exactLog2(v) > 0 then X.shiftI(X.shShl, r, exactLog2(v));
865 elsif v # 1 then X.imulI(r, INT(v)); end;
866end;
867
868(* r compared with v, through rdx if v needs more than 32 bits; with
869 zero by test, which sets the flags as cmp would and is shorter *)
870proc cmpI(r: INT; v: I64);
871begin
872 if v = 0 then X.testRR(r, r);
873 elsif fits32(v) then X.aluI(X.aluCmp, r, INT(v));
874 else X.movRI(RDX, v); X.alu(X.aluCmp, r, RDX); end;
875end;
876
877(* r div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards
878 zero, so a negative dividend first gets 2^k - 1 added (the bias). *)
879proc divPow2(sy: Scan.Sym; r, k: INT; unsign: BOOL);
880begin
881 (* unsigned: div shifts right; mod keeps the low k bits, by shifting
882 them up to the top and back down *)
883 if unsign then
884 if sy = Scan.sDiv then X.shiftI(X.shShr, r, k);
885 else X.shiftI(X.shShl, r, 64 - k); X.shiftI(X.shShr, r, 64 - k); end;
886 return;
887 end;
888 (* signed: sar 63 makes the sign into all ones or all zeros, and shr
889 64 - k keeps k of them, the bias 2^k - 1 or 0; with it added, sar k
890 rounds towards zero. The remainder is r less the quotient times 2^k. *)
891 X.movRR(RCX, r);
892 X.shiftI(X.shSar, RCX, 63);
893 X.shiftI(X.shShr, RCX, 64 - k);
894 X.alu(X.aluAdd, RCX, r);
895 X.shiftI(X.shSar, RCX, k);
896 if sy = Scan.sDiv then X.movRR(r, RCX);
897 else X.shiftI(X.shShl, RCX, k); X.alu(X.aluSub, r, RCX); end;
898end;
899
900(* Before rax div y, of a signed type of bits < 64: its smallest value
901 divided by -1 overflows it, which must stop the program as it does for
902 an I64 (report section 6.1). idiv on the 64-bit values wouldn't fault,
903 so then rax is made the smallest I64, and it does. *)
904proc divTrap(bits, y: INT);
905var L: INT;
906begin
907 L := NoLabel;
908 cmpI(y, -1);
909 X.jcc(X.ccNE, L);
910 cmpI(RAX, I64(-1) shl (bits - 1));
911 X.jcc(X.ccNE, L);
912 X.shiftI(X.shShl, RAX, 64 - bits);
913 X.fixLink(L);
914end;
915
916(* Whether the value on top is a word in memory that can be the right
917 operand of an instruction as it is: add r, [m] *)
918proc topInMem: BOOL;
919begin
920 return (stack[depth - 1].loc = lMem) and (stack[depth - 1].size = 8);
921end;
922
923(* The right operand of the two values on top, popped, in register y:
924 its own, or rcx for a constant; the left one stays, in its register,
925 which is returned with y. *)
926proc operands: (INT, INT);
927var v: I64; c: BOOL; y: INT;
928begin
929 c, v := topConst();
930 if c then X.movRI(RCX, v); y := RCX; else y := peek(depth - 1); end;
931 drop();
932 return top(), y;
933end;
934
935(* The two reals on top, the left op the right, worked out in the left's
936 xmm register. The right is taken from memory where it is, as a
937 constant, a variable or a spilled value is: mulsd xmm8, [rbp - 16]. *)
938proc realOp(sy: Scan.Sym; k: Kind);
939var f: BOOL; c, x, y: INT; m: X.Mem;
940begin
941 f := k = kF32;
942 match sy of
943 | sPlus: c := 58H; (* addsd, or addss for an F32 *)
944 | sMinus: c := 5CH; (* subsd, subss *)
945 | sTimes: c := 59H; (* mulsd, mulss *)
946 | else: c := 5EH; (* divsd, divss *)
947 end;
948 if stack[depth - 1].loc in {lConst, lMem, lFrame} then
949 match stack[depth - 1].loc of
950 | lConst: m := realConst(stack[depth - 1].val);
951 | lMem: m := stack[depth - 1].m;
952 | else: m := X.mem(RBP, stack[depth - 1].val);
953 end;
954 drop();
955 X.sseM(c, top(), m, f);
956 else
957 y := peek(depth - 1);
958 drop();
959 x := top();
960 X.sse(c, x, y, f);
961 end;
962end;
963
964(* The group 1 operation of +, -, &, | and xor *)
965proc aluOf(sy: Scan.Sym): INT;
966begin
967 match sy of
968 | sPlus: return X.aluAdd;
969 | sMinus: return X.aluSub;
970 | sAmp: return X.aluAnd;
971 | sBar: return X.aluOr;
972 | else: return X.aluXor;
973 end;
974end;
975
976const
977 (* the condition codes of the comparisons, from sEql: = # < <= > >= *)
978 CcSigned: [6]INT = [X.ccE, X.ccNE, X.ccL, X.ccLE, X.ccG, X.ccGE];
979 CcUnsigned: [6]INT = [X.ccE, X.ccNE, X.ccB, X.ccBE, X.ccA, X.ccAE];
980
981(* The two values on top, the left op the right, an integer of size
982 bytes, signed or not, brought back to its width. *)
983proc op*(sy: Scan.Sym; k: Kind; size: INT; signed: BOOL);
984var v, o: I64; r, y, sh, bits: INT; c: BOOL; m: X.Mem;
985begin
986 if k in {kF32, kF64} then realOp(sy, k); return; end;
987 bits := size * 8;
988 (* a constant on the left of an operation whose order doesn't matter
989 goes right, where it can be an immediate or a shift *)
990 if (((sy = Scan.sPlus) or (sy = Scan.sTimes) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor))) and (stack[depth - 2].loc = lConst)
991 and (stack[depth - 1].loc # lConst) then swap(); end;
992 c, v := topConst();
993 (* A word's offset (see Entry) is carried through + and -, and
994 through * by a constant, as (x + a) * k is x * k + a * k: the
995 operation is done on the registers, the offsets worked out here,
996 and put on the result. x + 1 is only noted. A value changed in
997 place, lInto, has no offset. *)
998 o := 0;
999 if (bits = 64) and (stack[depth - 2].loc # lInto)
1000 and ((((sy = Scan.sPlus) or (sy = Scan.sMinus))) or ((sy = Scan.sTimes) and c and fits32(v))) then
1001 o := takeOff(depth - 2);
1002 if sy = Scan.sTimes then o := o * v;
1003 elsif c and fits32(v) then
1004 if sy = Scan.sPlus then o := o + v; else o := o - v; end;
1005 drop();
1006 if not (stack[depth - 1].loc in {lReg, lHeld}) then _ := top(); end;
1007 putOff(o);
1008 return;
1009 elsif sy = Scan.sPlus then o := o + takeOff(depth - 1);
1010 else o := o - takeOff(depth - 1); end;
1011 end;
1012 sh := -1;
1013 if c and (((sy = Scan.sTimes) or (sy = Scan.sDiv) or (sy = Scan.sMod))) then sh := exactLog2(v); end;
1014 if c and (sh >= 1) then
1015 drop();
1016 r := top();
1017 if sy = Scan.sTimes then X.shiftI(X.shShl, r, sh); else divPow2(sy, r, sh, not signed); end;
1018 elsif c and fits32(v) and (((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sBar) or (sy = Scan.sXor) or (sy = Scan.sAmp) or (sy = Scan.sTimes) or (sy = Scan.sShl) or (sy = Scan.sShr))) then
1019 drop();
1020 r := top();
1021 match sy of
1022 | sTimes: X.imulI(r, INT(v));
1023 | sShl: X.shiftI(X.shShl, r, INT(v & I64(bits - 1)));
1024 | sShr:
1025 if signed then X.shiftI(X.shSar, r, INT(v & I64(bits - 1)));
1026 else X.shiftI(X.shShr, r, INT(v & I64(bits - 1))); end;
1027 | else: X.aluI(aluOf(sy), r, INT(v));
1028 end;
1029 elsif topInMem() and (((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor))) then
1030 m := stack[depth - 1].m;
1031 drop();
1032 r := top();
1033 X.aluM(aluOf(sy), r, m);
1034 else
1035 r, y := operands();
1036 if ((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor)) then
1037 X.alu(aluOf(sy), r, y);
1038 elsif sy = Scan.sTimes then
1039 X.imul(r, y);
1040 elsif ((sy = Scan.sDiv) or (sy = Scan.sMod)) then
1041 X.movRR(RAX, r);
1042 if not signed then
1043 X.movRI(RDX, 0); X.unary(6, y); (* div *)
1044 else
1045 (* only -1 can make the division overflow, so a constant
1046 divisor other than -1 needs no check *)
1047 if (bits < 64) and (not c or (v = -1)) then divTrap(bits, y); end;
1048 X.cqo(); X.unary(7, y); (* idiv, of rdx:rax, which cqo made from rax *)
1049 end;
1050 if sy = Scan.sDiv then X.movRR(r, RAX); else X.movRR(r, RDX); end;
1051 else (* sShl, sShr *)
1052 (* the count modulo the width: the machine takes it modulo 64 *)
1053 if y # RCX then X.movRR(RCX, y); end;
1054 if bits < 64 then X.aluI(X.aluAnd, RCX, bits - 1); end;
1055 if sy = Scan.sShl then X.shift(X.shShl, r);
1056 elsif signed then X.shift(X.shSar, r);
1057 else X.shift(X.shShr, r); end;
1058 end;
1059 end;
1060 if bits < 64 then X.extend(r, size, signed); end;
1061 stack[depth - 1].k := k;
1062 if o # 0 then putOff(o); end;
1063end;
1064
1065(* ROL or ROR, by left, of the two values on top: an unsigned integer of
1066 size bytes, under the count (report section 6.1). x86 takes the count
1067 of rol and ror modulo 32, or 64 for a quadword, and a byte's or a
1068 word's rotation then goes round modulo 8 or 16 itself: as 8 and 16
1069 divide 32, that is the count modulo the width, as the report has it,
1070 with no mask of the count's own, which a shift needs. A constant
1071 count is cut to the width here, an imm8; 0 rotates nothing. The bits
1072 above the width are zero and stay so: rol al and rol ax leave them,
1073 rol eax clears them. *)
1074proc rotate*(left: BOOL; size: INT);
1075var c: BOOL; v: I64; r, y, op: INT;
1076begin
1077 if left then op := X.shRol; else op := X.shRor; end;
1078 c, v := topConst();
1079 if c then
1080 drop();
1081 r := top();
1082 v := v & I64(size * 8 - 1);
1083 if v # 0 then X.rotate(op, r, size, INT(v)); end;
1084 else
1085 r, y := operands();
1086 if y # RCX then X.movRR(RCX, y); end;
1087 X.rotate(op, r, size, -1);
1088 end;
1089 stack[depth - 1].k := kWord;
1090end;
1091
1092(* CLZ, if leading, or CTZ of the unsigned integer of size bytes on top
1093 (report section 6.1). bsr finds the place h of its highest one bit,
1094 and the zeros above it are the width less 1 less h; bsf finds the
1095 place of its lowest, which is the zeros below it. Neither finds a
1096 place in 0, the one value with no one bit, and both say so by ZF:
1097 then the count is the whole width, as if h were -1, or the lowest
1098 one at the width. The bits above size bytes are zero, so a scan of
1099 all 64 sees the value's own. The baseline x86-64 lacks lzcnt and
1100 tzcnt, which would count 0 by themselves. For a U32:
1101
1102 bsr r, r bsf r, r
1103 jnz L jnz L
1104 mov r, -1 mov r, 32
1105 L: neg r L:
1106 add r, 31 *)
1107proc countZeros*(leading: BOOL; size: INT);
1108var r, w, L: INT;
1109begin
1110 r := top();
1111 w := size * 8;
1112 L := NoLabel;
1113 X.bitScan(leading, r);
1114 X.jcc(X.ccNE, L);
1115 if leading then X.movRI(r, -1); else X.movRI(r, w); end;
1116 X.fixLink(L);
1117 if leading then
1118 X.unary(3, r); (* neg *)
1119 X.aluI(X.aluAdd, r, w - 1);
1120 end;
1121 stack[depth - 1].k := kWord;
1122end;
1123
1124(* POPCOUNT of the integer or set on top (report section 6.1), counted
1125 as lib/Bytes.raz counts bytes, without a loop: each two bits are
1126 made the sum of their ones, then each four, then each byte, and a
1127 multiplication by 0101...H adds the eight bytes' sums into the top
1128 byte. The baseline x86-64 lacks popcnt. rax and rdx are
1129 free between the stack machine's operations.
1130
1131 mov rax, r; shr rax, 1; mov rdx, 5555...H; and rax, rdx; sub r, rax
1132 mov rax, r; shr rax, 2; mov rdx, 3333...H; and rax, rdx; and r, rdx; add r, rax
1133 mov rax, r; shr rax, 4; add r, rax; mov rdx, 0F0F...H; and r, rdx
1134 mov rdx, 0101...H; imul r, rdx; shr r, 56 *)
1135proc popCount*;
1136var r: INT;
1137begin
1138 r := top();
1139 (* each two bits: their value less its upper bit, which is its ones *)
1140 X.movRR(RAX, r);
1141 X.shiftI(X.shShr, RAX, 1);
1142 X.movRI(RDX, 5555555555555555H);
1143 X.alu(X.aluAnd, RAX, RDX);
1144 X.alu(X.aluSub, r, RAX);
1145 (* each four bits: the sums of its two pairs *)
1146 X.movRR(RAX, r);
1147 X.shiftI(X.shShr, RAX, 2);
1148 X.movRI(RDX, 3333333333333333H);
1149 X.alu(X.aluAnd, RAX, RDX);
1150 X.alu(X.aluAnd, r, RDX);
1151 X.alu(X.aluAdd, r, RAX);
1152 (* each byte: the sums of its two halves, at most 8, which fits four bits *)
1153 X.movRR(RAX, r);
1154 X.shiftI(X.shShr, RAX, 4);
1155 X.alu(X.aluAdd, r, RAX);
1156 X.movRI(RDX, 0F0F0F0F0F0F0F0FH);
1157 X.alu(X.aluAnd, r, RDX);
1158 (* the top byte of r times 0101...H is the sum of all eight *)
1159 X.movRI(RDX, 0101010101010101H);
1160 X.imul(r, RDX);
1161 X.shiftI(X.shShr, r, 56);
1162 stack[depth - 1].k := kWord;
1163end;
1164
1165(* ABS of the real, or the signed integer of size bytes, on top (report
1166 section 6.1); an unsigned one needs no code. A real's sign bit is
1167 cleared, in rax, as neg flips it. An integer is negated, and its copy
1168 in rax taken back if the negation is negative (SF), as it is for a
1169 positive one. The smallest value negates to itself, negative, and so
1170 gives itself, as -x does, with no branch and no check. A narrow
1171 value is negated on all 64 bits, where its smallest is not the
1172 smallest, so it is cut back to its width after, as neg does:
1173 -128 in an I8 negates to 128, which is -128 again in 8 bits.
1174
1175 movq rax, x; btr rax, 63; movq x, rax (F64; 31 for an F32)
1176 mov rax, r; neg r; cmovs r, rax (I64; then movsx r, r for a narrow one) *)
1177proc abs*(k: Kind; size: INT);
1178var r: INT;
1179begin
1180 r := top();
1181 if k in {kF32, kF64} then
1182 X.movqRX(RAX, r);
1183 if k = kF32 then X.btr(RAX, 31); else X.btr(RAX, 63); end;
1184 X.movqXR(r, RAX);
1185 else
1186 X.movRR(RAX, r);
1187 X.unary(3, r); (* neg *)
1188 X.cmov(X.ccS, r, RAX);
1189 if size < 8 then X.extend(r, size, true); end;
1190 end;
1191end;
1192
1193(* The left op the right of the two values on top, as a BOOL *)
1194proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL);
1195var f: BOOL; r, x, y, i: INT; v: I64; c: BOOL; m: X.Mem;
1196begin
1197 i := INT(sy) - INT(Scan.sEql);
1198 if k in {kF32, kF64} then
1199 f := k = kF32;
1200 y := peek(depth - 1);
1201 drop();
1202 x := peek(depth - 1);
1203 drop();
1204 r := target(); (* the BOOL, in the left's integer register *)
1205 (* ucomis sets the flags as an unsigned comparison would, but for an
1206 unordered pair (a NaN) it sets zero, parity and carry all. So only
1207 "above" can be tested straight: it is false for NaN, as every
1208 comparison but # must be. a < b is made b > a. = also asks for
1209 no parity, as NaN = NaN sets the zero flag, and # asks for
1210 parity as well. *)
1211 match sy of
1212 | sEql: X.ucomis(x, y, f); X.setCC(X.ccE, r); X.setCC(X.ccNP, RCX); X.alu(X.aluAnd, r, RCX);
1213 | sNeq: X.ucomis(x, y, f); X.setCC(X.ccNE, r); X.setCC(X.ccP, RCX); X.alu(X.aluOr, r, RCX);
1214 | sLss: X.ucomis(y, x, f); X.setCC(X.ccA, r);
1215 | sLeq: X.ucomis(y, x, f); X.setCC(X.ccAE, r);
1216 | sGtr: X.ucomis(x, y, f); X.setCC(X.ccA, r);
1217 | else: X.ucomis(x, y, f); X.setCC(X.ccAE, r);
1218 end;
1219 computed(kWord);
1220 else
1221 c, v := topConst();
1222 if c then
1223 drop(); r := top(); cmpI(r, v);
1224 elsif topInMem() then
1225 m := stack[depth - 1].m; drop(); r := top(); X.aluM(X.aluCmp, r, m);
1226 else
1227 r, y := operands(); X.alu(X.aluCmp, r, y);
1228 end;
1229 if signed then X.setCC(CcSigned[i], r); else X.setCC(CcUnsigned[i], r); end;
1230 end;
1231 stack[depth - 1].k := kWord;
1232end;
1233
1234(* A real is negated by flipping its sign bit, in rax *)
1235proc neg*(k: Kind; size: INT; signed: BOOL);
1236var r: INT;
1237begin
1238 r := top();
1239 if k in {kF32, kF64} then
1240 X.movqRX(RAX, r);
1241 if k = kF32 then X.btc(RAX, 31); else X.btc(RAX, 63); end;
1242 X.movqXR(r, RAX);
1243 else
1244 X.unary(3, r); (* neg *)
1245 if size < 8 then X.extend(r, size, signed); end;
1246 end;
1247end;
1248
1249proc complement*(size: INT; signed: BOOL);
1250var r: INT;
1251begin
1252 r := top();
1253 X.unary(2, r); (* not *)
1254 if size < 8 then X.extend(r, size, signed); end;
1255end;
1256
1257proc notBool*;
1258begin
1259 X.aluI(X.aluXor, top(), 1);
1260end;
1261
1262(* The integer on top, brought to size bytes *)
1263proc narrow*(size: INT; signed: BOOL);
1264var r: INT;
1265begin
1266 r := top();
1267 if size < 8 then X.extend(r, size, signed); end;
1268 stack[depth - 1].k := kWord;
1269end;
1270
1271(* The integer on top, as a real of kind k; big if it may be a U64 above
1272 high(I64), which the machine would take as negative *)
1273proc intToReal*(k: Kind; big: BOOL);
1274var L1, L2, r, x: INT; f: BOOL;
1275begin
1276 f := k = kF32;
1277 r := top();
1278 x := regIn(k, depth - 1); (* its xmm register *)
1279 if big then
1280 (* above high(I64): halve, keeping the low bit, and double *)
1281 L1 := NoLabel; L2 := NoLabel;
1282 X.testRR(r, r);
1283 X.jcc(X.ccS, L1);
1284 X.cvtsi2s(x, r, f);
1285 X.jmp(L2);
1286 X.fixLink(L1);
1287 X.movRR(RCX, r);
1288 X.shiftI(X.shShr, RCX, 1);
1289 X.aluI(X.aluAnd, r, 1);
1290 X.alu(X.aluOr, RCX, r);
1291 X.cvtsi2s(x, RCX, f);
1292 X.sse(58H, x, x, f); (* adds it to itself: doubles *)
1293 X.fixLink(L2);
1294 else
1295 X.cvtsi2s(x, r, f);
1296 end;
1297 stack[depth - 1].k := k;
1298end;
1299
1300(* The real of kind k on top, truncated to an integer *)
1301proc realToInt*(k: Kind);
1302var x: INT;
1303begin
1304 x := top();
1305 X.cvtts2si(regIn(kWord, depth - 1), x, k = kF32);
1306 stack[depth - 1].k := kWord;
1307end;
1308
1309(* The real of kind k on top, as the other kind *)
1310proc realToReal*(k: Kind);
1311var x: INT;
1312begin
1313 x := top();
1314 X.sse(5AH, x, x, k = kF32); (* cvtss2sd, or cvtsd2ss *)
1315 if k = kF32 then stack[depth - 1].k := kF64; else stack[depth - 1].k := kF32; end;
1316end;
1317
1318(* Whether the slices in the frame at a and b (the offsets of their
1319 pointers, the lengths 8 bytes on) are equal (or not, if not eq): the
1320 lengths, then the bytes, len shl shift of them, compared where the
1321 comparison stands. *)
1322proc compareSeq*(a, b: I64; shift: INT; eq: BOOL);
1323var differ, r: INT;
1324begin
1325 clobberArgs();
1326 r := target();
1327 X.load(RCX, X.mem(RBP, b + 8), 8, false);
1328 X.aluM(X.aluCmp, RCX, X.mem(RBP, a + 8));
1329 differ := NoLabel;
1330 X.jcc(X.ccNE, differ);
1331 X.load(RSI, X.mem(RBP, b), 8, false);
1332 X.load(RDI, X.mem(RBP, a), 8, false);
1333 if shift > 0 then X.shiftI(X.shShl, RCX, shift); end;
1334 (* repe cmpsb with rcx = 0 leaves the flags alone, so make them say
1335 equal first, which is right if there are no bytes *)
1336 X.alu(X.aluCmp, RCX, RCX);
1337 X.repeCmpsb();
1338 X.fixLink(differ);
1339 if eq then X.setCC(X.ccE, r); else X.setCC(X.ccNE, r); end;
1340 computed(kWord);
1341end;
1342
1343(* An error points at its message's length, an 8-byte word, with the
1344 text after it; nil is no error. Pops one, and makes the slice of its
1345 message at tmp, empty for nil. *)
1346proc errorText*(tmp: I64);
1347var L: INT;
1348begin
1349 popTo(RAX);
1350 X.movRI(RCX, 0);
1351 L := NoLabel;
1352 X.testRR(RAX, RAX);
1353 X.jcc(X.ccE, L);
1354 X.load(RCX, X.mem(RAX, 0), 8, false);
1355 X.lea(RAX, X.mem(RAX, 8));
1356 X.fixLink(L);
1357 X.store(X.mem(RBP, tmp), RAX, 8);
1358 X.store(X.mem(RBP, tmp + 8), RCX, 8);
1359end;
1360
1361(* Pops a step and an integer variable's address, and adds the step to
1362 it, or takes it away. *)
1363proc incDec*(size: INT; signed, dec: BOOL);
1364var r, y, o: INT; v: I64; a: Adr; c: BOOL;
1365begin
1366 c, v := topConst();
1367 if dec then o := X.aluSub; else o := X.aluAdd; end;
1368 if c and fits32(v) then
1369 drop();
1370 a.base := bStack; a.off := 0;
1371 X.aluMI(o, place(a, RCX), INT(v), size);
1372 return;
1373 end;
1374 r, y := operands();
1375 drop();
1376 X.load(RAX, X.mem(r, 0), size, signed);
1377 X.alu(o, RAX, y);
1378 X.store(X.mem(r, 0), RAX, size);
1379end;
1380
1381(* INC or DEC of the variable in register r, by the value on top; a
1382 narrow one wraps at its width *)
1383proc incDecReg*(r, size: INT; signed, dec: BOOL);
1384var y, o: INT; v: I64; c: BOOL;
1385begin
1386 if dec then o := X.aluSub; else o := X.aluAdd; end;
1387 c, v := topConst();
1388 if c and fits32(v) then X.aluI(o, r, INT(v));
1389 else y := top(); X.alu(o, r, y); end;
1390 drop();
1391 if size < 8 then X.extend(r, size, signed); end;
1392end;
1393
1394(* The pointer on top, which NEW got, to size zero bytes unless nil *)
1395proc zeroNew*(size: I64);
1396var L, r: INT;
1397begin
1398 clobberArgs();
1399 r := top();
1400 L := NoLabel;
1401 X.testRR(r, r);
1402 X.jcc(X.ccE, L);
1403 X.movRR(RDI, r);
1404 X.movRI(RCX, size);
1405 X.movRI(RAX, 0);
1406 X.repStos(1);
1407 X.fixLink(L);
1408end;
1409
1410(* CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack: the
1411 result where p was *)
1412proc atomic*(which: INT);
1413var r, n: INT;
1414begin
1415 if which = Sym.bCas then readMems(depth - 3); else readMems(depth - 2); end; (* what it writes may be one *)
1416 if which = Sym.bCas then n := top(); drop(); end;
1417 popTo(RAX); (* cmpxchg compares with rax, so CAS's old value goes there *)
1418 r := top();
1419 match which of
1420 | Sym.bCas: X.lockCmpxchg(X.mem(r, 0), n); X.setCC(X.ccE, r);
1421 | Sym.bXchg: X.xchg(X.mem(r, 0), RAX); X.movRR(r, RAX);
1422 | else: X.lockXadd(X.mem(r, 0), RAX); X.movRR(r, RAX);
1423 end;
1424 stack[depth - 1].k := kWord;
1425end;
1426
1427(* The system call and its arguments, n values on the stack. Linux takes
1428 the number in rax and the arguments in rdi, rsi, rdx, r10, r8 and r9,
1429 and the syscall instruction clobbers rcx and r11. Every value in a
1430 register is spilled first, as r8 to r11 are the stack's registers,
1431 and the arguments are loaded from their slots. *)
1432proc sysCall*(n: INT);
1433const SysRegs: [7]INT = [RAX, RDI, RSI, RDX, R10, R8, R9];
1434var i: INT;
1435begin
1436 clobberArgs();
1437 spillBelow(depth);
1438 for i := 0 to n - 1 do valueTo(depth - n + i, SysRegs[i]); end;
1439 for i := 1 to n do drop(); end;
1440 X.syscall();
1441 X.movRR(target(), RAX);
1442 computed(kWord);
1443end;
1444
1445proc fence*;
1446begin
1447 X.mfence();
1448end;
1449
1450(* Jumps. A label is the chain of the jumps to it, until it is placed. *)
1451
1452proc jump*(var L: INT);
1453begin
1454 X.jmp(L);
1455end;
1456
1457(* Pops a BOOL, and jumps if it is when *)
1458proc jumpIf*(var L: INT; when: BOOL);
1459var r: INT;
1460begin
1461 r := peek(depth - 1);
1462 drop();
1463 X.testRR(r, r);
1464 if when then X.jcc(X.ccNE, L); else X.jcc(X.ccE, L); end;
1465end;
1466
1467(* Pops two integers, and jumps if the left op the right *)
1468proc jumpCmp*(sy: Scan.Sym; signed: BOOL; var L: INT);
1469var r, y, i: INT; v: I64; c: BOOL; m: X.Mem;
1470begin
1471 i := INT(sy) - INT(Scan.sEql);
1472 (* a comparison changes neither: a variable's register will do *)
1473 c, v := topConst();
1474 if c then
1475 drop(); r := peek(depth - 1); cmpI(r, v);
1476 elsif topInMem() then
1477 m := stack[depth - 1].m; drop(); r := peek(depth - 1); X.aluM(X.aluCmp, r, m);
1478 else
1479 y := peek(depth - 1); drop(); r := peek(depth - 1); X.alu(X.aluCmp, r, y);
1480 end;
1481 drop();
1482 if signed then X.jcc(CcSigned[i], L); else X.jcc(CcUnsigned[i], L); end;
1483end;
1484
1485(* jumpCmp out of an expression, past code that may spill: the values
1486 under the two compared are spilled first, so that they are where the
1487 code after the landing place expects them, whichever way it came. In
1488 v + (a < b and f(x) = 0), a call to f spills v on the way through, so
1489 v must be in its slot too where a < b jumps past the call. *)
1490proc jumpOut*(sy: Scan.Sym; signed: BOOL; var L: INT);
1491begin
1492 spillBelow(depth - 2);
1493 jumpCmp(sy, signed, L);
1494end;
1495
1496(* The two values on top, compared, as a BOOL: true unless notOp, the
1497 opposite comparison, holds. fj and tj are the chains of jumps, made
1498 earlier by and, or and not, that are taken when the whole condition is
1499 false and when it is true; they land here, on the 0 and the 1. *)
1500proc condValue*(notOp: Scan.Sym; signed: BOOL; fj, tj: INT);
1501var r, e: INT;
1502begin
1503 jumpOut(notOp, signed, fj);
1504 X.fixLink(tj);
1505 r := target();
1506 X.movRI(r, 1);
1507 e := NoLabel;
1508 X.jmp(e);
1509 X.fixLink(fj);
1510 X.movRI(r, 0);
1511 X.fixLink(e);
1512 computed(kWord);
1513end;
1514
1515(* jumpBack, fix, fixTo and joinJumps are Gen's names for the encoder's,
1516 so that a port can place jumps its own way. fix(L) places label L here;
1517 fixTo(L, top) makes every jump on chain L go to the earlier place top;
1518 joinJumps makes one chain of two. *)
1519proc jumpBack*(top: INT);
1520begin
1521 X.jmpBack(top);
1522end;
1523
1524proc fix*(L: INT);
1525begin
1526 X.fixLink(L);
1527end;
1528
1529(* A jump table, for a match on the integer in frame slot slot, whose
1530 labels run from lo up for n values. The selector less lo, compared
1531 unsigned (so below lo is above n too), is out of the table if it
1532 isn't below n, and goes to the else arm at elseAt, or, if there is
1533 none, to endL, the end of the match. Else it indexes a table of
1534 4-byte offsets, each from the start of the lea, that follows the
1535 jump, and the code jumps to its start plus the entry:
1536
1537 mov rax, [slot]
1538 sub rax, lo
1539 cmp rax, n - 1
1540 ja else
1541 lea rcx, [rip + start] start: this very lea
1542 movsxd rax, [rcx + rax*4 + 17]
1543 add rax, rcx
1544 jmp rax
1545 table: .long arm0 - start, arm1 - start, ...
1546
1547 The four instructions after the compare are 7, 5, 3 and 2 bytes, so
1548 the table is 17 bytes on, which jumpTable checks. It returns start;
1549 the caller then makes the n entries with tableEntry. *)
1550proc jumpTable*(slot, lo: I64; n, elseAt: INT; var endL: INT): INT;
1551const TableAt = 17;
1552var start: INT;
1553begin
1554 X.load(RAX, X.mem(RBP, slot), 8, false);
1555 if lo # 0 then X.aluI(X.aluSub, RAX, INT(lo)); end;
1556 X.aluI(X.aluCmp, RAX, n - 1);
1557 if elseAt # NoLabel then X.jccBack(X.ccA, elseAt); else X.jcc(X.ccA, endL); end;
1558 start := Chunk.pc;
1559 X.lea(RCX, X.here());
1560 X.load(RAX, X.memIdx(RCX, RAX, 4, TableAt), 4, true);
1561 X.alu(X.aluAdd, RAX, RCX);
1562 X.jmpR(RAX);
1563 if Chunk.pc # start + TableAt then Diag.fatal("internal error: the jump table is not where it was said"); end;
1564 return start;
1565end;
1566
1567(* The next entry of the table that jumpTable started at table: the
1568 code at target *)
1569proc tableEntry*(table, target: INT);
1570begin
1571 Chunk.put4(target - table);
1572end;
1573
1574proc alignLoop*;
1575begin
1576 X.align(32);
1577end;
1578
1579proc joinJumps*(L1, L2: INT): INT;
1580begin
1581 return X.joinLinks(L1, L2);
1582end;
1583
1584proc fixTo*(L, top: INT);
1585begin
1586 X.fixLinkTo(L, top);
1587end;
1588
1589(* Checks *)
1590
1591const
1592 NoBase = -2; (* a Mem that is no memory *)
1593 ARegister = -3; (* a Mem that is no memory but a register, its index *)
1594
1595(* The name s, interned *)
1596proc internStr(s: []const U8): INT;
1597begin
1598 return Scan.intern(SYSTEM.PTR(s), LEN(s));
1599end;
1600
1601(* The program goes on if the condition cc holds, and otherwise stops
1602 in Rt.fail(what, a, b, len, at, name), which says what failed at p,
1603 found by at, which a stub works out from where it was called; what is
1604 an index a (0), a slice a..b (1), each for a length len, a position a
1605 of the enumeration called name (2), a slice of a pointer a..b that
1606 runs backwards (3), a nil pointer (4), or a view at index a that runs
1607 past a length len (7). Rt.fail knows 5 and 6, a
1608 division fault and a stack overflow, which this machine catches
1609 itself, so no check here has them. The arguments go in rdi, rsi, rdx,
1610 rcx, r8 and r9: a is in register ra, b in RDX; len is in memory at m,
1611 or n if m.base is NoBase, or in register m.index if it is ARegister.
1612 rdi and rsi are passed on as they were, since a parameter may be kept
1613 in them, for the trap report to show: so a goes to the stub in rax.
1614 The failing code is out of the way, at the
1615 procedure's end (endFails), so that a check that holds costs a jump
1616 not taken; it has p for its place (its line and column, worked out
1617 now, while the source read is p's), never returns, and may use any
1618 register once it has read what it needs. *)
1619proc check(cc, what: INT; p: Diag.Pos; ra: INT; m: X.Mem; n: I64; name: INT);
1620begin
1621 if numFails > LEN(fails) - 1 then
1622 _ := Scan.mark(Diag.say("more than {} checks in one procedure", [Fmt.int(LEN(fails))]));
1623 return;
1624 end;
1625 fails[numFails].L := NoLabel;
1626 X.jcc(cc xor 1, fails[numFails].L); (* x86's conditions come in pairs, the other one its last bit flipped *)
1627 fails[numFails].what := what;
1628 fails[numFails].ra := ra;
1629 fails[numFails].m := m;
1630 fails[numFails].n := n;
1631 fails[numFails].name := name;
1632 fails[numFails].path := Chunk.curFilePath();
1633 fails[numFails].line, fails[numFails].col := Scan.lineCol(p.a);
1634 INC(numFails);
1635end;
1636
1637(* The failures of the procedure's checks: each gives Rt.fail its values
1638 and the place of the offending expression, through a stub for its
1639 kind, which takes the place from where it was called *)
1640proc endFails;
1641var i, path: INT; f: Fail;
1642begin
1643 path := Chunk.curFilePath();
1644 for i := 0 to numFails - 1 do
1645 f := fails[i];
1646 X.fixLink(f.L);
1647 Chunk.setCurFile(f.path);
1648 Chunk.noteLine(f.line, f.col);
1649 Chunk.setCurFile(path);
1650 if f.ra # RAX then X.movRR(RAX, f.ra); end;
1651 if f.m.base = NoBase then X.movRI(RCX, f.n);
1652 elsif f.m.base = ARegister then X.movRR(RCX, f.m.index);
1653 else X.load(RCX, f.m, 8, false); end;
1654 if f.name >= 0 then X.lea(R9, X.memRip(f.name, 0)); end;
1655 if failStubs[f.what] < 0 then
1656 failStubs[f.what] := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr(StubNames[f.what]));
1657 end;
1658 X.callChunk(failStubs[f.what]);
1659 end;
1660 numFails := 0;
1661end;
1662
1663(* For check: a length known here, not in memory *)
1664proc noMem: X.Mem;
1665begin
1666 return X.mem(NoBase, 0);
1667end;
1668
1669(* For check: a length in register r, a slice's in registers *)
1670proc inRegister(r: INT): X.Mem;
1671var m: X.Mem;
1672begin
1673 m := X.mem(ARegister, 0);
1674 m.index := r;
1675 return m;
1676end;
1677
1678(* The index on top is below n, unsigned *)
1679proc checkIndex*(n: I64; p: Diag.Pos);
1680var r: INT;
1681begin
1682 r := peek(depth - 1);
1683 cmpI(r, n);
1684 check(X.ccB, 0, p, r, noMem(), n, -1);
1685end;
1686
1687(* The index on top is the element a view starts at, in an array of n
1688 elements: the view needs k elements from there, so the index must be
1689 below n - k + 1, which is below. A negative index is a huge one,
1690 unsigned, so one comparison does (report section 6.9). *)
1691proc checkView*(n, below: I64; p: Diag.Pos);
1692var r: INT;
1693begin
1694 r := peek(depth - 1);
1695 cmpI(r, below);
1696 check(X.ccB, 7, p, r, noMem(), n, -1);
1697end;
1698
1699(* What a failed index check is: an index past the length (0), or a
1700 view that runs past it (7), when the view needs room elements more
1701 than the one indexed. *)
1702proc kindOfIndex(room: I64): INT;
1703begin
1704 if room > 0 then return 7; end;
1705 return 0;
1706end;
1707
1708(* A view of a slice's element, at the index in r, needs room elements
1709 after it: rdx holds the slice's length, which r is below already,
1710 so length - r, at least 1, can't wrap, and must be more than room.
1711 The length is at m for the message. *)
1712proc checkRoom(r: INT; room: I64; p: Diag.Pos; m: X.Mem);
1713begin
1714 X.alu(X.aluSub, RDX, r);
1715 X.aluI(X.aluCmp, RDX, INT(room));
1716 check(X.ccA, 7, p, r, m, 0, -1);
1717end;
1718
1719(* The integer on top is a position of an enumeration of n names *)
1720proc checkEnum*(n: I64; name: INT; p: Diag.Pos);
1721var r: INT;
1722begin
1723 r := top();
1724 cmpI(r, n);
1725 check(X.ccB, 2, p, r, noMem(), n, name);
1726end;
1727
1728(* The address on top, from a pointer, which is to be used reach bytes
1729 past: the program stops if it is nil, unless the machine would stop
1730 it, as for a reach within NilPage. *)
1731proc checkNil*(reach: I64; p: Diag.Pos);
1732var r: INT;
1733begin
1734 if reach <= NilPage then return; end;
1735 r := top();
1736 X.testRR(r, r);
1737 check(X.ccNE, 4, p, r, noMem(), 0, -1);
1738end;
1739
1740(* The displacement disp of an element of size bytes, with the offset of
1741 the index on top taken into it if it fits: s[k - 1] is [r13 + r9 * 8
1742 - 8] *)
1743proc indexOff(size, disp: I64): I64;
1744begin
1745 if fits32(disp + stack[depth - 1].off * size) then return disp + takeOff(depth - 1) * size; end;
1746 return disp;
1747end;
1748
1749(* Pops an index, and the address of the slice at a if it is on the
1750 stack, and pushes the address of its element of size bytes: the
1751 index, checked against the length, times size, plus the pointer.
1752 It is worked out in the index's own register, which then holds the
1753 address; but if the slice's address is on the stack, under the
1754 index, the element's register is the one that holds that, still
1755 needed, so the work is done in rax. A slice in registers is
1756 compared with and added to them; for an element of 1, 2, 4 or 8
1757 bytes, its address is only noted, as the operand [pointer + index *
1758 size], to be read or written where it is used (lIdx). *)
1759proc indexSlice*(a: Adr; size, room: I64; checked: BOOL; p: Diag.Pos);
1760var m, ml: X.Mem; r: INT; inRax: BOOL; o: I64;
1761begin
1762 if (a.base = bReg) and (((size = 1) or (size = 2) or (size = 4) or (size = 8))) then
1763 o := 0;
1764 if not checked then o := indexOff(size, 0); end;
1765 r := peek(depth - 1);
1766 if checked then
1767 X.alu(X.aluCmp, r, a.hi);
1768 check(X.ccB, kindOfIndex(room), p, r, inRegister(a.hi), 0, -1);
1769 if room > 0 then X.movRR(RDX, a.hi); checkRoom(r, room, p, inRegister(a.hi)); end;
1770 end;
1771 drop();
1772 newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false);
1773 return;
1774 end;
1775 inRax := a.base = bStack;
1776 if inRax then r := RAX; popTo(RAX);
1777 else r := top(); drop(); end;
1778 if a.base = bReg then
1779 if checked then
1780 X.alu(X.aluCmp, r, a.hi);
1781 check(X.ccB, kindOfIndex(room), p, r, inRegister(a.hi), 0, -1);
1782 if room > 0 then X.movRR(RDX, a.hi); checkRoom(r, room, p, inRegister(a.hi)); end;
1783 end;
1784 mulI(r, size);
1785 X.alu(X.aluAdd, r, a.chunk);
1786 else
1787 m := place(a, RCX);
1788 ml := m; ml.disp := ml.disp + 8;
1789 if checked then
1790 X.aluM(X.aluCmp, r, ml);
1791 check(X.ccB, kindOfIndex(room), p, r, ml, 0, -1);
1792 if room > 0 then X.load(RDX, ml, 8, false); checkRoom(r, room, p, ml); end;
1793 end;
1794 mulI(r, size);
1795 X.aluM(X.aluAdd, r, m);
1796 end;
1797 if inRax then X.movRR(target(), RAX);
1798 else _ := claim(depth, kWord); end; (* the index's register again, now for the address *)
1799 computed(kWord);
1800end;
1801
1802(* Whether r is one of the stack's registers, not a variable's *)
1803proc isStackReg(r: INT): BOOL;
1804var i: INT;
1805begin
1806 for i := 0 to NumRegs - 1 do
1807 if Regs[i] = r then return true; end;
1808 end;
1809 return false;
1810end;
1811
1812(* Pops an index and the pointer under it, and pushes the address of
1813 the element of size bytes: p + i * size. For an element of 1, 2, 4
1814 or 8 bytes it is only noted, as the operand [p + i * size] (lIdx), to
1815 be read or written where it is used, when at most one of the two is
1816 in a stack register, the register the new value owns. Otherwise lea
1817 adds them up, in one instruction in place of a multiplication and an
1818 addition. *)
1819proc indexPtr*(size: I64);
1820var b, x, own: INT; o: I64;
1821begin
1822 o := 0;
1823 if fits32(stack[depth - 2].off + stack[depth - 1].off * size) then
1824 o := takeOff(depth - 2) + takeOff(depth - 1) * size;
1825 end;
1826 b := peek(depth - 2);
1827 x := peek(depth - 1);
1828 own := Regs[(depth - 2) mod NumRegs];
1829 drop();
1830 drop();
1831 if (not isStackReg(b) or (b = own)) and (not isStackReg(x) or (x = own)) then
1832 newMem(lIdx, X.memIdx(b, x, INT(size), o), kWord, 8, false);
1833 if b = own then owner[(depth - 1) mod NumRegs] := depth - 1; end;
1834 else
1835 X.lea(target(), X.memIdx(b, x, INT(size), o));
1836 computed(kWord);
1837 end;
1838end;
1839
1840(* Pops an index, checked, and makes the address of the element it
1841 picks of the array at a. An element of 1, 2,
1842 4 or 8 bytes is only noted, as the operand [base + index * size]
1843 (lIdx): an array in the frame has rbp as its base, one reached
1844 through a pointer in a register (a var or const parameter) that
1845 register, and one in a chunk its address as the displacement
1846 (X.memAbs), with no lea. Any other is added up. *)
1847proc indexArray*(a: Adr; size: I64);
1848var r: INT; o: I64;
1849begin
1850 if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base in {bFrame, bGlobal, bPtr}) then
1851 o := indexOff(size, a.off);
1852 r := peek(depth - 1);
1853 drop();
1854 if a.base = bFrame then
1855 newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false);
1856 elsif a.base = bPtr then
1857 newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false);
1858 else
1859 newMem(lIdx, X.memAbs(a.chunk, r, INT(size), INT(o)), kWord, 8, false);
1860 end;
1861 else
1862 mulI(top(), size);
1863 pushAdr(a);
1864 op(Scan.sPlus, kWord, 8, false);
1865 end;
1866end;
1867
1868(* Pops lo and hi, and the address of the array or slice at a if it is
1869 on the stack, and makes the slice lo..hi of it, with elements of size
1870 bytes, in the frame at tmp; the array has n elements. *)
1871proc slice*(a: Adr; isArray: BOOL; n, size, tmp: I64; checked: BOOL; p: Diag.Pos);
1872var m, ml: X.Mem;
1873begin
1874 popTo(RDX);
1875 popTo(RCX);
1876 if a.base = bSlot then clobberArgs(); end; (* place loads its slot into rdi *)
1877 if a.base = bReg then
1878 ml := inRegister(a.hi); (* a slice in registers *)
1879 else
1880 m := place(a, RDI);
1881 ml := m; ml.disp := ml.disp + 8;
1882 end;
1883 if checked then
1884 (* two checks, both unsigned, so that a negative bound fails too:
1885 hi against the length, then lo against hi *)
1886 if isArray then cmpI(RDX, n); ml := noMem();
1887 elsif a.base = bReg then X.alu(X.aluCmp, RDX, a.hi);
1888 else X.aluM(X.aluCmp, RDX, ml); end;
1889 check(X.ccBE, 1, p, RCX, ml, n, -1);
1890 X.alu(X.aluCmp, RCX, RDX);
1891 check(X.ccBE, 1, p, RCX, ml, n, -1);
1892 end;
1893 X.movRR(RAX, RDX);
1894 X.alu(X.aluSub, RAX, RCX);
1895 X.store(X.mem(RBP, tmp + 8), RAX, 8);
1896 mulI(RCX, size);
1897 if isArray then X.lea(RAX, m);
1898 elsif a.base = bReg then X.movRR(RAX, a.chunk);
1899 else X.load(RAX, m, 8, false); end;
1900 X.alu(X.aluAdd, RAX, RCX);
1901 X.store(X.mem(RBP, tmp), RAX, 8);
1902end;
1903
1904(* The rcx bytes below rsi to those below rdi, n at a time while rcx
1905 holds n or more, through rax, moving rsi, rdi and rcx down: COPY's
1906 backward copy, as a loop, since rep movsb going down (std) moves a
1907 byte at a time. *)
1908proc copyDown(n: INT);
1909var top, out: INT;
1910begin
1911 top := Chunk.pc;
1912 X.aluI(X.aluCmp, RCX, n);
1913 out := NoLabel;
1914 X.jcc(X.ccB, out);
1915 X.aluI(X.aluSub, RSI, n);
1916 X.aluI(X.aluSub, RDI, n);
1917 X.aluI(X.aluSub, RCX, n);
1918 X.load(RAX, X.mem(RSI, 0), n, false);
1919 X.store(X.mem(RDI, 0), RAX, n);
1920 X.jmpBack(top);
1921 X.fixLink(out);
1922end;
1923
1924(* COPY: the elements of the slice in the frame at sOff, of size bytes
1925 each, to the start of the one at dOff, which must be at least as long
1926 (the source may be shorter), as one block. It goes forwards, by rep
1927 movsb, but backwards when the destination starts inside the source,
1928 so that an overlap comes out right: by words from the ends, then the
1929 last few bytes one by one. *)
1930proc copySlice*(dOff, sOff, size: I64; checked: BOOL; p: Diag.Pos);
1931var ml: X.Mem; L, E: INT;
1932begin
1933 clobberArgs();
1934 X.load(RDX, X.mem(RBP, sOff + 8), 8, false);
1935 if checked then
1936 ml := X.mem(RBP, dOff + 8);
1937 X.movRI(RAX, 0);
1938 X.aluM(X.aluCmp, RDX, ml);
1939 check(X.ccBE, 1, p, RAX, ml, 0, -1);
1940 end;
1941 X.movRR(RCX, RDX);
1942 mulI(RCX, size);
1943 X.load(RSI, X.mem(RBP, sOff), 8, false);
1944 X.load(RDI, X.mem(RBP, dOff), 8, false);
1945 L := NoLabel;
1946 X.alu(X.aluCmp, RDI, RSI);
1947 X.jcc(X.ccBE, L); (* the destination is not above: forwards, at L *)
1948 X.lea(RAX, X.memIdx(RSI, RCX, 1, 0));
1949 X.alu(X.aluCmp, RDI, RAX);
1950 X.jcc(X.ccAE, L); (* nor does it start inside the source *)
1951 X.alu(X.aluAdd, RSI, RCX); (* backwards, from the ends *)
1952 X.alu(X.aluAdd, RDI, RCX);
1953 copyDown(8);
1954 copyDown(1);
1955 E := NoLabel;
1956 X.jmp(E);
1957 X.fixLink(L);
1958 X.repMovsb();
1959 X.fixLink(E);
1960end;
1961
1962(* The most elements FILL stores one by one: as for MaxMoves, a store
1963 each costs less than rep stos takes to start, which at 10 elements
1964 is twice the time. *)
1965const FewFills = 16;
1966
1967(* rax, an element of size bytes, 1, 2, 4 or 8, into each of the
1968 elements of the slice in the frame at dOff, of which there are n if
1969 n isn't -1. Few, as of a short array, are a store each; else, at run
1970 time, a loop of stores for fewer than FewFills, or rep stos of the
1971 element's width:
1972
1973 mov rdi, [rbp + dOff]
1974 mov rcx, [rbp + dOff + 8]
1975 cmp rcx, 16
1976 jae many
1977 test rcx, rcx
1978 jz out
1979 top: mov [rdi], eax ; for a U32
1980 add rdi, 4
1981 sub rcx, 1
1982 jne top
1983 jmp out
1984 many: rep stosd
1985 out: *)
1986proc fillScalar(dOff, size, n: I64);
1987var i: I64; many, out, top: INT;
1988begin
1989 X.load(RDI, X.mem(RBP, dOff), 8, false);
1990 if (n >= 0) and (n <= FewFills) then
1991 for i := 0 to n - 1 do X.store(X.mem(RDI, i * size), RAX, INT(size)); end;
1992 return;
1993 end;
1994 X.load(RCX, X.mem(RBP, dOff + 8), 8, false);
1995 many := NoLabel;
1996 out := NoLabel;
1997 X.aluI(X.aluCmp, RCX, FewFills);
1998 X.jcc(X.ccAE, many);
1999 X.testRR(RCX, RCX);
2000 X.jcc(X.ccE, out);
2001 top := Chunk.pc;
2002 X.store(X.mem(RDI, 0), RAX, INT(size));
2003 X.aluI(X.aluAdd, RDI, INT(size));
2004 X.aluI(X.aluSub, RCX, 1);
2005 X.jccBack(X.ccNE, top);
2006 X.jmp(out);
2007 X.fixLink(many);
2008 X.repStos(INT(size));
2009 X.fixLink(out);
2010end;
2011
2012(* The size bytes at rsi into each element of the slice in the frame at
2013 dOff: into the first by copyBytes, and from there forward by rep
2014 movsb, from d[0] on to d[1] on. A forward copy reads each byte after it has written it, size
2015 bytes before, so the first element repeats through the rest:
2016
2017 mov rdi, [rbp + dOff]
2018 mov rdx, [rbp + dOff + 8]
2019 test rdx, rdx
2020 jz out ; nothing to fill
2021 ... ; copyBytes(size): d[0] := x
2022 mov rsi, [rbp + dOff]
2023 lea rdi, [rsi + size]
2024 lea rcx, [rdx - 1]
2025 imul rcx, size ; the bytes of d[1..n]
2026 rep movsb
2027 out: *)
2028proc fillCopy(dOff, size: I64);
2029var out: INT;
2030begin
2031 X.load(RDI, X.mem(RBP, dOff), 8, false);
2032 X.load(RDX, X.mem(RBP, dOff + 8), 8, false);
2033 X.testRR(RDX, RDX);
2034 out := NoLabel;
2035 X.jcc(X.ccE, out);
2036 copyBytes(size);
2037 X.load(RSI, X.mem(RBP, dOff), 8, false);
2038 X.lea(RDI, X.mem(RSI, size));
2039 X.lea(RCX, X.mem(RDX, -1));
2040 mulI(RCX, size);
2041 X.repMovsb();
2042 X.fixLink(out);
2043end;
2044
2045(* FILL: every element of the slice in the frame at dOff, of size bytes
2046 each, set to the value on top of the stack if the element is a
2047 scalar, or else to the size bytes at the address on top. n is how
2048 many elements the slice has if that is known, as for an array, else
2049 -1. A scalar goes into rax and is stored as it is, a real's bits as
2050 an integer's (fillScalar); but a constant zero into an array of n
2051 elements is n * size zero bytes, as NEW's zeroing is: by moves, or
2052 rep stosb (zeroBytes). Any other element is copied (fillCopy). *)
2053proc fillSlice*(dOff, size, n: I64; scalar: BOOL);
2054var isConst: BOOL; v: I64;
2055begin
2056 clobberArgs();
2057 isConst, v := topConst();
2058 if scalar and isConst and (v = 0) and (n >= 0) then
2059 drop();
2060 X.load(RDI, X.mem(RBP, dOff), 8, false);
2061 zeroBytes(n * size);
2062 elsif scalar then
2063 popTo(RAX);
2064 fillScalar(dOff, size, n);
2065 else
2066 popTo(RSI);
2067 fillCopy(dOff, size);
2068 end;
2069end;
2070
2071(* Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its
2072 bounds can't be known, but it can't run backwards. *)
2073proc slicePtr*(size, tmp: I64; checked: BOOL; p: Diag.Pos);
2074begin
2075 clobberArgs();
2076 popTo(RDX);
2077 popTo(RCX);
2078 popTo(RDI);
2079 if checked then
2080 X.alu(X.aluCmp, RCX, RDX);
2081 check(X.ccLE, 3, p, RCX, noMem(), 0, -1);
2082 end;
2083 X.movRR(RAX, RDX);
2084 X.alu(X.aluSub, RAX, RCX);
2085 X.store(X.mem(RBP, tmp + 8), RAX, 8);
2086 mulI(RCX, size);
2087 X.alu(X.aluAdd, RCX, RDI);
2088 X.store(X.mem(RBP, tmp), RCX, 8);
2089end;
2090
2091(* Calls *)
2092
2093(* The call c, its c.n arguments the values on top of the stack, in
2094 registers, noted as constants or spilled. They go to registers or the
2095 machine's stack, the procedure is called, and its result, if any,
2096 comes back: one value is pushed, two results are pushed as two values,
2097 a slice or a record is stored in the temporary c.ret.
2098
2099 ni and nf count the integer and the real registers used so far, k the
2100 arguments on the machine's stack, and pad is 1 when a padding quadword
2101 is needed. *)
2102
2103proc call*(var c: CallInfo);
2104var
2105 res: ^Sym.Type; i, ni, nf, k, pad, r, first: INT;
2106 onStack: [64]BOOL; late: [2]INT; (* late: the values for r8 and r9 *)
2107begin
2108 (* the arguments are the c.n values on top, where computing them left
2109 them; the registers in use under them are saved around the call *)
2110 clobberArgs();
2111 first := depth - c.n;
2112 spillBelow(first);
2113 res := c.sig^.base;
2114 c.ret := 0;
2115 ni := 0; nf := 0;
2116 if (res # nil) and Sym.holdsArray(res) then
2117 (* a slice of it may be kept for the rest of the procedure
2118 (report 8), so its slot lasts as long *)
2119 c.ret := allocPerm(res^.size, res^.align);
2120 ni := 1;
2121 elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then
2122 c.ret := allocTemp(res^.size, res^.align);
2123 if Sym.behindPointer(res) then ni := 1; end;
2124 end;
2125 (* the System V classes: each argument in the next free register of
2126 its kind, or else on the stack *)
2127 k := 0;
2128 for i := 0 to c.n - 1 do
2129 if c.isF[i] then
2130 onStack[i] := nf >= 8;
2131 if nf < 8 then INC(nf); end;
2132 else
2133 (* a slice's words both go in registers, or both on the stack *)
2134 if (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1];
2135 elsif c.pair[i] then onStack[i] := ni > 4;
2136 else onStack[i] := ni >= 6; end;
2137 if not onStack[i] then INC(ni); end;
2138 end;
2139 if onStack[i] then INC(k); end;
2140 end;
2141 (* System V wants rsp a multiple of 16 at the call. A procedure's body
2142 has it so (push rbp, and a frame size that is a multiple of 16), so
2143 a quadword of padding makes the count pushed since even. The stack
2144 arguments go right to left, so that the first is at the lowest
2145 address. *)
2146 pad := 0;
2147 if ((pushed + k) & 1) # 0 then pad := 1; end;
2148 if pad # 0 then X.aluI(X.aluSub, RSP, 8); end;
2149 for i := c.n - 1 to 0 by -1 do
2150 if onStack[i] then
2151 valueTo(first + i, RAX);
2152 X.push(RAX);
2153 end;
2154 end;
2155 ni := 0; nf := 0;
2156 late[0] := -1; late[1] := -1;
2157 if (c.ret # 0) and Sym.behindPointer(res) then X.lea(RDI, X.mem(RBP, c.ret)); ni := 1; end;
2158 (* rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as
2159 they are stack registers and may hold arguments not yet moved: late
2160 is the value for each. The fifth goes through rax, so that writing r8
2161 doesn't destroy a value still wanted for r9. *)
2162 for i := 0 to c.n - 1 do
2163 if not onStack[i] then
2164 if c.isF[i] then
2165 xValueTo(first + i, nf);
2166 INC(nf);
2167 else
2168 if ni < 4 then valueTo(first + i, IntRegs[ni]); else late[ni - 4] := first + i; end;
2169 INC(ni);
2170 end;
2171 end;
2172 end;
2173 if late[0] >= 0 then valueTo(late[0], RAX); end;
2174 if late[1] >= 0 then valueTo(late[1], R9); end;
2175 if late[0] >= 0 then X.movRR(R8, RAX); end;
2176 for i := 1 to c.n do drop(); end;
2177 (* a procedure taking ... is told in al how many xmm registers it got *)
2178 if c.sig^.variadic then X.movRI(RAX, nf); end;
2179 if c.ext then X.callM(X.memRip(c.chunk, 0));
2180 elsif c.chunk >= 0 then X.callChunk(c.chunk);
2181 else
2182 (* r11 is scratch: caller-saved, carrying no argument, and free,
2183 as the value stack was spilled above *)
2184 X.load(R11, X.mem(RBP, c.target), 8, false);
2185 X.callR(R11);
2186 end;
2187 if k + pad > 0 then X.aluI(X.aluAdd, RSP, 8 * (k + pad)); end;
2188 (* the variables in xmm registers, which the call may have changed,
2189 from their slots *)
2190 for i := 0 to LEN(XVarRegs) - 1 do
2191 if (regMask & (1 shl (XBit + i))) # 0 then
2192 X.movsXM(XVarRegs[i] - XmmVar, X.mem(RBP, homes[i]), false);
2193 end;
2194 end;
2195 (* the result: a slice in rax and rdx goes to the caller's temporary,
2196 two results on the stack, the first under the second, and a single
2197 value on top *)
2198 if res # nil then
2199 if res^.form = Sym.fSlice then
2200 X.store(X.mem(RBP, c.ret), RAX, 8);
2201 X.store(X.mem(RBP, c.ret + 8), RDX, 8);
2202 elsif Sym.inTwoRegs(res) then
2203 X.movRR(target(), RAX); computed(kWord);
2204 X.movRR(target(), RDX); computed(kWord);
2205 elsif (c.ret = 0) and (res^.form = Sym.fReal) then
2206 X.movX(xTarget(), 0);
2207 computed(kindOf(res));
2208 elsif c.ret = 0 then
2209 r := target();
2210 X.movRR(r, RAX);
2211 if res^.form = Sym.fBool then X.extend(r, 1, false);
2212 elsif (res^.form = Sym.fInt) and (res^.size < 8) then X.extend(r, INT(res^.size), res^.signed); end;
2213 computed(kindOf(res));
2214 end;
2215 end;
2216end;
2217
2218(* Frames *)
2219
2220(* A slot of size bytes in the frame: its offset from rbp, negative, as
2221 the frame grows down. A temporary is released at the end of its
2222 statement (tempMark, releaseTemps); allocPerm's lasts the procedure. *)
2223proc allocTemp*(size, align: I64): I64;
2224begin
2225 if align < 1 then align := 1; end;
2226 frameTop := Sym.alignUp(frameTop + size, align);
2227 if frameTop > frameMax then frameMax := frameTop; end;
2228 return -frameTop;
2229end;
2230
2231proc allocPerm*(size, align: I64): I64;
2232var s: I64;
2233begin
2234 s := allocTemp(size, align);
2235 permTop := frameTop;
2236 return s;
2237end;
2238
2239proc tempMark*: I64;
2240begin
2241 return frameTop;
2242end;
2243
2244(* The slots allocated since the mark m are free again, but those
2245 allocPerm made stay *)
2246proc releaseTemps*(m: I64);
2247begin
2248 if m < permTop then m := permTop; end;
2249 frameTop := m;
2250end;
2251
2252(* Every slot used so far stays reserved for the rest of the procedure:
2253 a deferred statement's temporaries live until it runs. *)
2254proc keepTemps*;
2255begin
2256 frameTop := frameMax;
2257 permTop := frameMax;
2258end;
2259
2260(* The parameters' places: each in a frame slot of its own, stored there
2261 from its register on entry, or above the frame where the caller
2262 pushed it. A var parameter, and a record or array passed by value,
2263 holds an address (ind). *)
2264proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj);
2265var p: ^Sym.Obj; ni, nf, k: INT; stackOff: I64; f: BOOL;
2266begin
2267 frameTop := 0; frameMax := 0; permTop := 0;
2268 resetStack(0, 0);
2269 exitL := NoLabel;
2270 numIn := 0;
2271 lost := false;
2272 kept := 0;
2273 resType := sig^.base;
2274 procParams := params;
2275 regMask := 0;
2276 ni := 0; nf := 0; stackOff := 16; (* stackOff: above the saved rbp and the return address *)
2277 if (resType # nil) and Sym.behindPointer(resType) then
2278 retSlot := allocPerm(8, 8);
2279 inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil;
2280 numIn := 1; ni := 1;
2281 end;
2282 p := params;
2283 while p # nil do
2284 p^.ind := p^.isVar or (Sym.isAggregate(p^.typ) and (p^.typ^.form # Sym.fSlice));
2285 f := (p^.typ^.form = Sym.fReal) and not p^.isVar;
2286 if (p^.typ^.form = Sym.fSlice) and not p^.isVar then
2287 (* its two words, in two registers or both on the stack *)
2288 if ni <= 4 then
2289 p^.val := allocPerm(16, 8);
2290 for k := 0 to 1 do
2291 inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + I64(8 * k);
2292 inSize[numIn] := 8; inObj[numIn] := p;
2293 INC(numIn); INC(ni);
2294 end;
2295 else
2296 p^.val := stackOff;
2297 INC(stackOff, 16);
2298 end;
2299 elsif (f and (nf < 8)) or (not f and (ni < 6)) then
2300 p^.val := allocPerm(8, 8);
2301 (* a real's register, xmm nf, is told from an integer register by
2302 a negative number (enterBody decodes it) *)
2303 if f then
2304 inReg[numIn] := -1 - nf; INC(nf);
2305 else
2306 inReg[numIn] := IntRegs[ni]; INC(ni);
2307 end;
2308 inSlot[numIn] := p^.val;
2309 inObj[numIn] := p;
2310 if p^.ind then inSize[numIn] := 8;
2311 elsif p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet, Sym.fReal} then inSize[numIn] := INT(p^.typ^.size);
2312 else inSize[numIn] := 8; end;
2313 INC(numIn);
2314 else
2315 p^.val := stackOff;
2316 INC(stackOff, 8);
2317 end;
2318 p := p^.next;
2319 end;
2320 paramTop := frameTop;
2321end;
2322
2323(* The frame as beginProc left it, with the parameters' slots only: the
2324 locals are given theirs again once their registers are chosen. *)
2325proc resetLocals*;
2326begin
2327 frameTop := paramTop; frameMax := paramTop; permTop := paramTop;
2328end;
2329
2330(* The register the word (0, or 1 for a slice's second) of parameter o
2331 arrives in, or -1 if it comes on the stack or in an xmm register *)
2332proc arrivesIn(o: ^Sym.Obj; word: INT): INT;
2333var i, r: INT;
2334begin
2335 r := -1;
2336 for i := 0 to numIn - 1 do
2337 if (inObj[i] = o) and (inSlot[i] = o^.val + I64(8 * word)) and (inReg[i] >= 0) then r := inReg[i]; end;
2338 end;
2339 return r;
2340end;
2341
2342(* Parameter o, of a procedure that calls nothing, stays in the register
2343 it arrives in if that is rdi or rsi, which only a call or a copy needs:
2344 no register is saved for it, and nothing is moved. A slice stays only
2345 if it has both. If the procedure turns out to copy after all, lost is
2346 set, and it is compiled again with o in memory. *)
2347proc keepArrived*(o: ^Sym.Obj);
2348var r: INT;
2349begin
2350 r := arrivesIn(o, 0);
2351 if o^.typ^.form = Sym.fSlice then
2352 if (r = RDI) and (arrivesIn(o, 1) = RSI) then o^.reg := RDI; o^.reg2 := RSI; end;
2353 elsif (r = RDI) or (r = RSI) then o^.reg := r; end;
2354end;
2355
2356(* Whether parameter o is kept where it arrived *)
2357proc keptIn*(o: ^Sym.Obj): BOOL;
2358begin
2359 return (o^.reg = RDI) or (o^.reg = RSI);
2360end;
2361
2362(* The locals in memory have their slots: the prologue zeroes those and
2363 nothing below. The procedure keeps variables in the registers of
2364 mask, which are saved next, a quadword each, in slots that need no
2365 zeroing; the frame as it then stands is where a second try at the
2366 body starts. *)
2367proc useRegs*(mask: INT);
2368var i, k: INT; p: ^Sym.Obj;
2369begin
2370 zeroTop := permTop;
2371 regMask := mask;
2372 (* the parameters left in rdi and rsi, as they arrived *)
2373 kept := 0;
2374 p := procParams;
2375 while p # nil do
2376 if ((p^.reg = RDI) or (p^.reg = RSI)) then kept := kept | (1 shl p^.reg); end;
2377 if ((p^.reg2 = RDI) or (p^.reg2 = RSI)) then kept := kept | (1 shl p^.reg2); end;
2378 p := p^.next;
2379 end;
2380 k := 0;
2381 for i := 0 to 4 do
2382 if (mask & (1 shl i)) # 0 then INC(k); end;
2383 end;
2384 if k > 0 then saveAt := allocPerm(8 * k, 8); end;
2385 frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop;
2386end;
2387
2388(* The variable in xmm register r of XVarRegs has a slot of 8 bytes at
2389 slot, kept as it changes *)
2390proc xHome*(r: INT; slot: I64);
2391begin
2392 homes[xIndex(r)] := slot;
2393end;
2394
2395proc markCode*(var m: CodeMark);
2396begin
2397 m.pc := Chunk.pc; m.fixups := Chunk.numFixups; m.lines := Chunk.numLines; m.said := X.saidCount();
2398 m.fails := numFails; m.exit := exitL;
2399end;
2400
2401(* The code made since m taken back, as if it had never been made; the
2402 frame is left as it is *)
2403proc rewind*(m: CodeMark);
2404begin
2405 Chunk.rewindTo(m.pc, m.fixups, m.lines);
2406 X.unsay(m.said);
2407 numFails := m.fails;
2408 exitL := m.exit; (* a try or return in it made jumps to the exit *)
2409end;
2410
2411(* For a second try at the body: the code as it was at m, and the frame
2412 as useRegs found it *)
2413proc redo*(m: CodeMark);
2414begin
2415 Chunk.rewindTo(m.pc, m.fixups, m.lines);
2416 X.unsay(m.said);
2417 numFails := 0;
2418 lost := false;
2419 frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2];
2420 exitL := NoLabel;
2421end;
2422
2423(* The prologue, which is for the procedure's first line. Only the
2424 locals in memory are zeroed, from the parameters' slots down to
2425 zeroTop: the parameters are stored over their slots next, the
2426 registers of variables are zeroed in place, their save slots are
2427 written before they are read, and so is every temporary. *)
2428proc enterBody*(c, line: INT);
2429var i, loop, r: INT; n: I64; p: ^Sym.Obj;
2430begin
2431 Chunk.beginChunk(c);
2432 codeChunk := c;
2433 Chunk.noteLine(line, 1); (* a declaration starts a line *)
2434 X.push(RBP);
2435 X.movRR(RBP, RSP);
2436 X.say("sub rsp, frame");
2437 (* sub rsp, imm32: always the form with a 32-bit field, so that endProc
2438 can patch the frame's size in *)
2439 Chunk.put1(48H); Chunk.put1(81H); Chunk.put1(0ECH);
2440 frameFix := Chunk.pc; Chunk.put4(0);
2441 n := (Sym.alignUp(zeroTop, 8) - paramTop) div 8;
2442 if n = 1 then
2443 X.movRI(RAX, 0);
2444 X.store(X.mem(RBP, -paramTop - 8), RAX, 8);
2445 elsif n > 1 then
2446 (* the locals, 16 bytes at a time, from xmm8, which holds nothing
2447 yet (xmm0 to xmm7 may hold a parameter): up to 16 stores in a
2448 row, else a loop with r10 running from minus the bytes up to 0,
2449 the store's address scaled by 1. An odd quadword is the
2450 lowest. *)
2451 X.pxorXX(8);
2452 if n <= 32 then
2453 for i := 0 to INT(n div 2) - 1 do
2454 X.movdquMX(X.mem(RBP, -paramTop - 16 * I64(i + 1)), 8);
2455 end;
2456 else
2457 X.movRI(R10, -16 * (n div 2));
2458 loop := Chunk.pc;
2459 X.movdquMX(X.memIdx(RBP, R10, 1, -paramTop), 8);
2460 X.aluI(X.aluAdd, R10, 16);
2461 X.jccBack(X.ccNE, loop);
2462 end;
2463 if n mod 2 = 1 then
2464 X.movRI(RAX, 0);
2465 X.store(X.mem(RBP, -paramTop - 8 * n), RAX, 8);
2466 end;
2467 end;
2468 (* each register that holds a variable is saved, for the caller, and
2469 zeroed, as a variable starts at zero *)
2470 Chunk.setSaves(c, regMask & ((1 shl XBit) - 1), INT(saveAt));
2471 n := 0;
2472 for i := 0 to 4 do
2473 if (regMask & (1 shl i)) # 0 then
2474 X.store(X.mem(RBP, saveAt + 8 * n), VarRegs[i], 8);
2475 X.movRI(VarRegs[i], 0);
2476 INC(n);
2477 end;
2478 end;
2479 (* and each xmm register that holds a variable, zeroed, as its slot
2480 is: the caller keeps its own *)
2481 for i := 0 to LEN(XVarRegs) - 1 do
2482 if (regMask & (1 shl (XBit + i))) # 0 then
2483 X.movRI(RAX, 0);
2484 X.movqXR(XVarRegs[i] - XmmVar, RAX);
2485 end;
2486 end;
2487 (* the parameters that came in registers, to their slots or, for a
2488 register variable, to its register: a slice's second word, the one
2489 for the slot 8 bytes into its own, to its length's *)
2490 for i := 0 to numIn - 1 do
2491 if inReg[i] < 0 then
2492 X.movsMX(X.mem(RBP, inSlot[i]), -1 - inReg[i], false);
2493 if inObj[i]^.reg # 0 then X.movX(inObj[i]^.reg - XmmVar, -1 - inReg[i]); end;
2494 elsif (inObj[i] # nil) and (inObj[i]^.reg # 0) then
2495 r := inObj[i]^.reg;
2496 if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end;
2497 if r # inReg[i] then X.movRR(r, inReg[i]); end; (* else it is kept where it is *)
2498 if inSize[i] < 8 then X.extend(r, inSize[i], inObj[i]^.typ^.signed); end;
2499 else
2500 X.store(X.mem(RBP, inSlot[i]), inReg[i], inSize[i]);
2501 end;
2502 end;
2503 (* a register variable's parameter that came on the stack, above the
2504 frame (val > 0), is loaded from there; one passed by its address
2505 (ind: var, or const) holds the address, 8 bytes, whatever its type:
2506 a var BOOL loaded as a byte was a bad pointer *)
2507 p := procParams;
2508 while p # nil do
2509 if (p^.reg # 0) and (p^.val > 0) then
2510 if p^.ind then
2511 X.load(p^.reg, X.mem(RBP, p^.val), 8, false);
2512 elsif p^.typ^.form = Sym.fReal then
2513 X.movsXM(p^.reg - XmmVar, X.mem(RBP, p^.val), p^.typ^.size = 4);
2514 elsif p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet} then
2515 X.load(p^.reg, X.mem(RBP, p^.val), INT(p^.typ^.size), p^.typ^.signed);
2516 else
2517 X.load(p^.reg, X.mem(RBP, p^.val), 8, false);
2518 if p^.typ^.form = Sym.fSlice then X.load(p^.reg2, X.mem(RBP, p^.val + 8), 8, false); end;
2519 end;
2520 end;
2521 p := p^.next;
2522 end;
2523end;
2524
2525(* Pops one of several results, a value of size bytes, into its place
2526 off bytes into the caller's, behind the hidden pointer. *)
2527(* The frame slot that holds the hidden result pointer *)
2528proc resultSlot*: I64;
2529begin
2530 return retSlot;
2531end;
2532
2533proc resultTo*(off: I64; size: INT);
2534var r: INT; fl: BOOL;
2535begin
2536 r := top();
2537 fl := isReal(depth - 1);
2538 drop();
2539 X.load(RCX, X.mem(RBP, retSlot), 8, false);
2540 if fl then X.movsMX(X.mem(RCX, off), r, size = 4);
2541 else X.store(X.mem(RCX, off), r, size); end;
2542end;
2543
2544(* Pops an address, and copies size bytes from it to the result, off
2545 bytes into it. *)
2546proc copyResult*(off, size: I64);
2547begin
2548 clobberArgs();
2549 popTo(RSI);
2550 X.load(RDI, X.mem(RBP, retSlot), 8, false);
2551 if off # 0 then X.lea(RDI, X.mem(RDI, off)); end;
2552 copyBytes(size);
2553end;
2554
2555(* return: the result, a value on top, goes in rax or xmm0; two words,
2556 the two values on top, in rax and rdx; one behind the hidden pointer
2557 goes back with that pointer in rax. *)
2558proc returnJump*;
2559begin
2560 if resType # nil then
2561 if Sym.behindPointer(resType) then
2562 X.load(RAX, X.mem(RBP, retSlot), 8, false);
2563 elsif Sym.inTwoRegs(resType) then
2564 popTo(RDX);
2565 popTo(RAX);
2566 elsif resType^.form = Sym.fReal then
2567 xValueTo(depth - 1, 0);
2568 drop();
2569 else
2570 popTo(RAX);
2571 end;
2572 end;
2573 X.jmp(exitL);
2574end;
2575
2576(* The first of what try does where the error isn't nil (the report, 7.8):
2577 the machine's stack made even, as the deferred statements about to be
2578 run expect to find it. try can stand among the arguments of another
2579 call, where quadwords have been pushed. Nothing is lost: the frame is
2580 above rsp, and the code returns through the frame pointer. *)
2581proc tryAlign*;
2582begin
2583 X.aluI(X.aluAnd, RSP, -16);
2584end;
2585
2586(* The procedure returns the error in register r, and a zero for each of
2587 its other results; it isn't rax, rcx, rdx or rdi, which this uses. *)
2588proc returnError(r: INT);
2589var f: ^Sym.Obj;
2590begin
2591 if Sym.behindPointer(resType) then
2592 X.load(RDI, X.mem(RBP, retSlot), 8, false);
2593 zeroBytes(resType^.size);
2594 f := resType^.fields;
2595 while f^.next # nil do f := f^.next; end;
2596 X.load(RDX, X.mem(RBP, retSlot), 8, false);
2597 X.store(X.mem(RDX, f^.val), r, 8);
2598 X.load(RAX, X.mem(RBP, retSlot), 8, false);
2599 elsif Sym.inTwoRegs(resType) then
2600 X.movRR(RDX, r);
2601 X.movRI(RAX, 0);
2602 else
2603 X.movRR(RAX, r);
2604 end;
2605 X.jmp(exitL);
2606end;
2607
2608(* The rest of it: the procedure returns the error in the frame slot, and
2609 a zero for each of its other results. Only code is made: no entry of
2610 the stack of values is touched, since the code that follows is the
2611 code for an error that is nil, with the stack as it was. *)
2612proc tryReturn*(slot: I64);
2613begin
2614 X.load(RSI, X.mem(RBP, slot), 8, false);
2615 returnError(RSI);
2616end;
2617
2618(* try, with no deferred statement to run: the error on top is tested in
2619 its register r, which is returned, and, if it isn't nil, returned from
2620 the procedure by tryReturnFrom, so it needs no slot in the frame. *)
2621proc tryTest*(var ok: INT): INT;
2622var r: INT;
2623begin
2624 r := loaded(depth - 1);
2625 jumpIf(ok, false);
2626 return r;
2627end;
2628
2629proc tryReturnFrom*(r: INT);
2630begin
2631 returnError(r);
2632end;
2633
2634(* The end of the procedure in chunk c: the code that returns, and the
2635 frame's size, known only now, patched into its prologue. If it is
2636 open, running off its end, it returns zeros. *)
2637proc endProc*(c: INT; open: BOOL): ERROR;
2638var n: I64; i: INT;
2639begin
2640 codeChunk := -1;
2641 if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end;
2642 if open then
2643 if Sym.behindPointer(resType) then
2644 X.load(RDI, X.mem(RBP, retSlot), 8, false);
2645 X.movRI(RCX, resType^.size);
2646 X.movRI(RAX, 0);
2647 X.repStos(1);
2648 X.load(RAX, X.mem(RBP, retSlot), 8, false);
2649 else
2650 X.movRI(RAX, 0);
2651 if Sym.inTwoRegs(resType) then X.movRI(RDX, 0); end;
2652 X.movqXR(0, RAX);
2653 end;
2654 else
2655 X.dropJumpTo(exitL);
2656 end;
2657 X.fixLink(exitL);
2658 n := 0;
2659 for i := 0 to 4 do
2660 if (regMask & (1 shl i)) # 0 then
2661 X.load(VarRegs[i], X.mem(RBP, saveAt + 8 * n), 8, false);
2662 INC(n);
2663 end;
2664 end;
2665 X.leave();
2666 X.ret();
2667 endFails();
2668 n := Sym.alignUp(frameMax, 16);
2669 if n < 16 then n := 16; end;
2670 if n > 2147483647 then return Scan.mark("frame too big"); end;
2671 Chunk.put4At(frameFix, INT(n));
2672 (* the listing's text for the sub, which starts 3 bytes (REX.W, opcode,
2673 ModRM) before its immediate *)
2674 if X.listing then X.restate(frameFix - 3, Diag.say("sub rsp, {}", [Fmt.int(INT(n))])); end;
2675 Chunk.endChunk(c);
2676 return nil;
2677end;
2678
2679(* A deferred statement is compiled where it stands, as a subroutine of
2680 its procedure that the code jumps over, in the procedure's own frame
2681 (the same rbp), so that it sees all its variables; deferBegin gives its
2682 address and what pushed was, and starts it, with the return address
2683 pushed. The stack is empty where it is called, and where it begins. *)
2684proc deferBegin*(var L: INT): (INT, INT);
2685var saved: INT;
2686begin
2687 X.jmp(L);
2688 saved := pushed;
2689 pushed := 1;
2690 return Chunk.pc, saved;
2691end;
2692
2693proc deferEnd*(L, saved: INT);
2694begin
2695 keepTemps();
2696 X.ret();
2697 X.fixLink(L);
2698 pushed := saved;
2699end;
2700
2701proc runDefer*(at: INT);
2702begin
2703 X.callBack(at);
2704end;
2705
2706(* The entry point, _start, and its chunk: it saves argc and argv, and
2707 the address of the places for Rt; runs every used module's body, then
2708 main; and ends with Rt.exit, its status main's or 0. A dynamic program
2709 starts as C's crt1 does, through __libc_start_main(main, argc, argv,
2710 nil, nil, rtld_fini, stack_end), with rsp aligned; its main first
2711 gives the library what it needs of C: Rt C's exit, in cExit, and
2712 Thread pthread_create and pthread_join.
2713
2714 dynamic: the program uses C. first: the first module, whose chain
2715 gives the order of the bodies. mainObj: the program's main procedure, or
2716 nil. The first numCLinks of cLinks: C's symbols, each loaded from the
2717 quadword the loader fills in and stored in a variable of the
2718 library's, in a program that uses C. exitProc: the chunk of Rt's exit. The
2719 first numLinks of links: the addresses Rt is given, of what the program
2720 has: the places, and the profiler and calls; what it isn't given,
2721 nothing reaches, and the linker leaves out.
2722
2723 bare: raz -bare, a program for the bare machine, a kernel's, with
2724 no Linux under it. Its entry point reads no argc and argv, which
2725 only Linux leaves on the stack, so that ARGC() is 0; and runs no body
2726 of Rt, whose system calls, to set the signals' handlers, would fault
2727 there. A kernel's own module sets up what Rt's body would. *)
2728proc entryPoint*(dynamic, bare: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT;
2729var entry, start, i: INT; m: ^Sym.Module;
2730begin
2731 entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start"));
2732 if dynamic then
2733 start := Chunk.newChunk(Chunk.secText, -1, internStr("main"));
2734 Chunk.beginChunk(entry);
2735 (* at the start rdx holds rtld_fini, from the dynamic loader, and the
2736 stack argc, then argv *)
2737 X.movRI(X.RBP, 0); (* no frame above, where a trace stops *)
2738 X.movRR(R9, RDX); (* rtld_fini, the sixth argument *)
2739 X.pop(RSI); (* argc, the second *)
2740 X.movRR(RDX, RSP); (* argv, the third *)
2741 X.aluI(X.aluAnd, RSP, -16);
2742 X.push(RAX); (* padding, so that the next push, the seventh *)
2743 X.push(RSP); (* argument, stack_end, leaves rsp aligned *)
2744 X.movRI(R8, 0); (* fini and init: none *)
2745 X.movRI(RCX, 0);
2746 X.lea(RDI, X.memRip(start, 0)); (* main, the first argument *)
2747 X.callM(X.memRip(Chunk.externChunk(internStr("__libc_start_main")), 0));
2748 X.ud2(); (* it never returns *)
2749 Chunk.endChunk(entry);
2750 Chunk.beginChunk(start);
2751 X.push(RBP); (* aligns the stack again *)
2752 X.store(X.memRip(argcChunk, 0), RDI, 8);
2753 X.store(X.memRip(argvChunk, 0), RSI, 8);
2754 for i := 0 to numCLinks - 1 do
2755 X.load(RAX, X.memRip(cLinks[i].chunk, 0), 8, false);
2756 X.store(X.memRip(cLinks[i].into, 0), RAX, 8);
2757 end;
2758 elsif bare then
2759 start := entry;
2760 Chunk.beginChunk(entry);
2761 else
2762 start := entry;
2763 Chunk.beginChunk(entry);
2764 X.load(RAX, X.mem(RSP, 0), 8, false);
2765 X.store(X.memRip(argcChunk, 0), RAX, 8);
2766 X.lea(RAX, X.mem(RSP, 8));
2767 X.store(X.memRip(argvChunk, 0), RAX, 8);
2768 end;
2769 for i := 0 to numLinks - 1 do
2770 X.lea(RAX, X.memRip(links[i].chunk, 0));
2771 X.store(X.memRip(links[i].into, 0), RAX, 8);
2772 end;
2773 m := first;
2774 while m # nil do
2775 if m^.used and (m^.body >= 0) and not (bare and (m^.name = internStr("Rt"))) then X.callChunk(m^.body); end;
2776 m := m^.next;
2777 end;
2778 if mainObj # nil then X.callChunk(mainObj^.chunk); end;
2779 if (mainObj # nil) and (mainObj^.typ^.base # nil) then X.movRR(RDI, RAX); else X.movRI(RDI, 0); end;
2780 X.callChunk(exitProc);
2781 X.ud2(); (* exit never returns: trap if it does *)
2782 Chunk.endChunk(start);
2783 (* Rt.fail, for the failures of each kind: the place is where the
2784 check's call is. The return address is the next instruction's, which
2785 may belong to another line; one byte back is inside the call, so the
2786 lookup finds the call's line. rsp is aligned for the call, as the
2787 check's code may have pushed an odd number of quadwords. rdi and rsi
2788 as the failing code had them go on the stack, as Rt.fail's last two
2789 arguments, and a comes in rax (endFails). *)
2790 for i := 0 to LEN(failStubs) - 1 do
2791 if failStubs[i] >= 0 then
2792 Chunk.beginChunk(failStubs[i]);
2793 X.load(R8, X.mem(RSP, 0), 8, false);
2794 X.aluI(X.aluSub, R8, 1);
2795 X.aluI(X.aluAnd, RSP, -16);
2796 X.push(RSI);
2797 X.push(RDI);
2798 X.movRR(RSI, RAX);
2799 X.movRI(RDI, i);
2800 X.callChunk(rtFail);
2801 X.ud2();
2802 Chunk.endChunk(failStubs[i]);
2803 end;
2804 end;
2805 return entry;
2806end;
2807
2808(* A code procedure: its instructions, read by the assembler (X64),
2809 are the whole of chunk c *)
2810proc codeProc*(c: INT; constExpr: X.ConstFn; nameFn: X.NameFn): ERROR;
2811begin
2812 Chunk.beginChunk(c);
2813 try X.assemble(constExpr, nameFn);
2814 Chunk.endChunk(c);
2815 return nil;
2816end;
2817
2818(* Whether the code procedure in chunk c is a square root of xmm0 in
2819 itself, an F32's if single, then ret, as Math.sqrt is:
2820
2821 sqrtsd xmm0, xmm0 F2 0F 51 C0
2822 ret C3
2823
2824 If so, its prefix and opcode, F251H (F351H for sqrtss), which a call of it does in place
2825 on the value's own register (realInPlace); else 0. A call would spill
2826 the stack's reals and reload the variables' xmm registers around it,
2827 as System V keeps none through one, for an instruction of four bytes. *)
2828proc oneRealOp*(c: INT; single: BOOL): INT;
2829var s, pfx: INT;
2830begin
2831 s := Chunk.chunks[c].start;
2832 if (Chunk.chunks[c].size # 5) or (Chunk.chunks[c].numFix # 0) then return 0; end;
2833 pfx := 0F2H;
2834 if single then pfx := 0F3H; end; (* sqrtss *)
2835 if (INT(Chunk.text[s]) # pfx) or (Chunk.text[s + 1] # 0FH)
2836 or (Chunk.text[s + 2] # 51H) or (Chunk.text[s + 3] # 0C0H) or (Chunk.text[s + 4] # 0C3H) then
2837 return 0;
2838 end;
2839 return INT(Chunk.text[s]) * 256 + INT(Chunk.text[s + 2]);
2840end;
2841
2842(* The operation op, of oneRealOp, on the real on top, in its register:
2843 Math.sqrt(d2) is sqrtsd xmm8, xmm8, with no call. *)
2844proc realInPlace*(op: INT);
2845var x: INT;
2846begin
2847 x := top();
2848 X.sse(op & 0FFH, x, x, op shr 8 = 0F3H);
2849end;
2850
2851var i: INT;
2852begin
2853 for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end;
2854 Chunk.setPad(0CCCCCCCCH); (* int3, a trap, between procedures *)
2855 inPlace := -1;
2856 codeChunk := -1;
2857 resetStack(0, 0);
2858 rtFail := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr("fail"));
2859 argcChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argc"));
2860 Chunk.setBss(argcChunk, 8, 8);
2861 argvChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argv"));
2862 Chunk.setBss(argvChunk, 8, 8);
2863end Gen.