| 1 | (* Gen: the code generator, for arm64 (Gen.arm64.raz; x86-64's is |
| 2 | Gen.x86_64.raz, the suffix of the file choosing, doc/raz.md, 3). |
| 3 | |
| 4 | The same stack machine as x86-64's, with the same calls, which Item |
| 5 | and Parse make: they push constants, addresses and the values of |
| 6 | variables, operate on the values on top, store them and jump on |
| 7 | them. It makes all that x86-64's does, in arm64's instructions, but |
| 8 | SYSCALL, which is Linux's only: a program that uses it fails to |
| 9 | compile (missing). |
| 10 | |
| 11 | The stack is kept in registers as on x86-64: the value at depth d, |
| 12 | depth 0 being the bottom, is in x9 to x14, register d mod 6, all of |
| 13 | them registers a call may change and that carry no argument, so |
| 14 | that a call moves its arguments into x0 to x7 in any order. A value |
| 15 | that needs a register a lower one holds spills that one to a frame |
| 16 | slot. A constant, or an address in the frame or of a chunk, is only |
| 17 | noted until something needs it in a register. A variable's value is |
| 18 | loaded when it is pushed: no instruction of arm64 but a load or a |
| 19 | store takes memory. |
| 20 | |
| 21 | A real, an F32 or an F64, is held as x86-64 holds it, in the |
| 22 | registers that compute with reals: the value at depth d in v16 to |
| 23 | v21, XRegs[d mod 6], d or s, so each depth has two registers, and |
| 24 | owner doesn't tell them apart. They are a call's to change and carry |
| 25 | no argument, as x9 to x14 are; v23 is Gen's scratch. A real constant |
| 26 | is made in x15 and moved across (fmov), as arm64 has no instruction |
| 27 | that holds any real. |
| 28 | |
| 29 | Registers outside the stack: x15 and x17, Gen's scratch, as rax and |
| 30 | rcx are on x86-64; x16, the encoder's (A64); x0 to x3 for copying |
| 31 | and slices, between calls, where they hold nothing; x8, which Apple's convention |
| 32 | keeps for the address of a result in memory; x18, the system's, |
| 33 | never touched; x19 to x23, a procedure's busiest variables (VarRegs), |
| 34 | and d8 to d15, its busiest reals (XVarRegs), which a called procedure |
| 35 | gives back as it found them; x29, the frame pointer, and x30, the |
| 36 | return address. |
| 37 | |
| 38 | Calls use Apple's arm64 convention: the first eight integer arguments |
| 39 | in x0 to x7, the first eight reals in v0 to v7, the rest on the |
| 40 | machine's stack, results in x0 and x1, or d0, or in memory at the |
| 41 | address in x8, sp a multiple of 16 always, a |
| 42 | frame record of x29 and x30 that x29 points at, so that the chain of |
| 43 | frames can be followed. A frame is made whole by the prologue, so |
| 44 | nothing is pushed in a body but a call's stack arguments, for the |
| 45 | call, and a deferred statement's return address: pushed is always 0. |
| 46 | |
| 47 | What x86-64 does by itself, and arm64 doesn't, the code does: it |
| 48 | checks a division by zero, and the smallest value by -1 (divide), |
| 49 | and gives the smallest I64 for a real out of range (realToInt). |
| 50 | |
| 51 | Depends on: Scan and Diag for errors, Sym for types, Chunk for code |
| 52 | and data, A64 for instructions. |
| 53 | Leaves out SYSCALL, which is Linux's only. *) |
| 54 | module Gen; |
| 55 | import Fmt, Diag, Scan, Sym, Chunk, A := A64; |
| 56 | import SYSTEM; |
| 57 | |
| 58 | const |
| 59 | Arch* = "arm64"; (* the target's machine, as a file's suffix says it *) |
| 60 | NoLabel* = -1; |
| 61 | WordSize* = 8; (* the size of an address, and of INT *) |
| 62 | (* What the machine catches by itself: a load or store within |
| 63 | NilPage bytes of address 0, in the Mac's page zero of 4 GB, which |
| 64 | Rt's handler tells as a nil pointer; and a call to nil, and a |
| 65 | stack overflow at the guard page under the stack. A division by |
| 66 | zero it doesn't catch: op checks for it. *) |
| 67 | NilPage* = 100000000H; |
| 68 | |
| 69 | FP = A.FP; LR = A.LR; SP = A.SP; ZR = A.ZR; |
| 70 | T = 15; U = 17; (* Gen's scratch *) |
| 71 | Dst = 0; Src = 1; Tmp = 2; (* copying's *) |
| 72 | XT = 23; (* Gen's scratch for a real *) |
| 73 | |
| 74 | type |
| 75 | Kind* = (kWord, kLong, kF32, kF64); |
| 76 | |
| 77 | (* Where a variable is: its address is relative to the frame, to a |
| 78 | chunk, to an address on the stack, or to one held in a frame slot *) |
| 79 | Base* = (bFrame, bGlobal, bStack, bSlot, bReg, bPtr); |
| 80 | Adr* = record |
| 81 | base*: Base; |
| 82 | chunk*: INT; (* bGlobal: the chunk; bSlot: the frame slot; bReg: the register; |
| 83 | bPtr: the register that holds the address *) |
| 84 | off*: I64; (* added to the address; for bReg, which word, 0 or 8 *) |
| 85 | hi*: INT; (* bReg, a slice: the register of its length, the |
| 86 | second word; chunk is that of its pointer *) |
| 87 | end; |
| 88 | |
| 89 | (* The code made so far, to go back to for a second try at a body *) |
| 90 | CodeMark* = record pc*, fixups*, lines*, said*, fails*, exit*: INT; end; |
| 91 | |
| 92 | (* For entryPoint: the address of chunk, put in the variable whose |
| 93 | chunk is into before the program starts; or, for C's symbols, the |
| 94 | address in chunk, the quadword the loader fills in *) |
| 95 | Link* = record chunk*, into*: INT; end; |
| 96 | |
| 97 | CallInfo* = record |
| 98 | sig*: ^Sym.Type; |
| 99 | param*: ^Sym.Obj; (* the parameter the next argument is for *) |
| 100 | obj*: ^Sym.Obj; (* the procedure called, if it has a name *) |
| 101 | n*: INT; (* the arguments *) |
| 102 | isF*: [64]BOOL; (* ... goes in a real's register *) |
| 103 | pair*: [64]BOOL; (* ... is a slice's first word, the next its second *) |
| 104 | target*: I64; (* the frame slot of a procedure value, if chunk < 0 *) |
| 105 | chunk*: INT; (* else the procedure's chunk *) |
| 106 | ext*: BOOL; (* C's: called through the chunk holding its address *) |
| 107 | ret*: I64; (* set by call: the temporary a slice result is in *) |
| 108 | end; |
| 109 | |
| 110 | const |
| 111 | (* the registers a procedure keeps its busiest variables in, which a |
| 112 | called procedure saves and gives back (Apple's x19 to x28); the |
| 113 | trap report finds them where the prologue saved them *) |
| 114 | VarRegs*: [5]INT = [19, 20, 21, 22, 23]; |
| 115 | (* the registers the trap report can show a variable in: VarRegs; no |
| 116 | parameter is kept where it arrives, so the last two are none *) |
| 117 | ShownRegs*: [7]INT = [19, 20, 21, 22, 23, -1, -1]; |
| 118 | (* the registers of real variables, d8 to d15, which a call keeps, |
| 119 | numbered from XmmVar up so that they aren't taken for x8 to x15 *) |
| 120 | XmmVar* = 32; |
| 121 | XVarRegs*: [8]INT = [XmmVar + 8, XmmVar + 9, XmmVar + 10, XmmVar + 11, |
| 122 | XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15]; |
| 123 | (* a procedure's registers of variables, as bits: bit i is VarRegs[i], |
| 124 | bit XBit + i XVarRegs[i] *) |
| 125 | XBit* = 5; |
| 126 | |
| 127 | var |
| 128 | rtFail*, argcChunk*, argvChunk*: INT; |
| 129 | lost*: BOOL; (* never set here: no parameter is kept where it arrived *) |
| 130 | depth*: INT; (* the values on the stack *) |
| 131 | pushed*: INT; (* quadwords pushed on the machine's stack: always 0 *) |
| 132 | codeChunk*: INT; (* the procedure or body being compiled, or -1 *) |
| 133 | |
| 134 | type |
| 135 | (* where a value on the stack is *) |
| 136 | Loc = (lConst, (* not loaded: val is the value *) |
| 137 | lAdr, (* not loaded: the address of chunk, or of the frame |
| 138 | if chunk is -1, plus val *) |
| 139 | lReg, (* in its register *) |
| 140 | lFrame); (* spilled: in the frame slot val *) |
| 141 | |
| 142 | Entry = record |
| 143 | loc: Loc; |
| 144 | val: I64; |
| 145 | chunk: INT; (* lAdr: the chunk, or -1 for the frame *) |
| 146 | k: Kind; |
| 147 | end; |
| 148 | |
| 149 | (* A place in memory: off from register base, or, if chunk >= 0, off |
| 150 | into that chunk, reached by adrp (A64.loadChunk) *) |
| 151 | Mem = record base, chunk: INT; off: I64; end; |
| 152 | |
| 153 | (* a check of the procedure, whose failure endFails makes: what |
| 154 | failed, a's register, the length's (or -1 for the constant n), |
| 155 | the enumeration's name, and the place *) |
| 156 | Fail = record |
| 157 | L, what, ra, lenReg, name: INT; |
| 158 | n: I64; |
| 159 | path, line, col: INT; |
| 160 | end; |
| 161 | |
| 162 | const |
| 163 | MaxStack = 1024; (* values waiting in one expression *) |
| 164 | StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"]; |
| 165 | NumRegs = 6; |
| 166 | Regs: [NumRegs]INT = [9, 10, 11, 12, 13, 14]; |
| 167 | XRegs: [NumRegs]INT = [16, 17, 18, 19, 20, 21]; (* v16 to v21 *) |
| 168 | (* the condition codes of the comparisons, from sEql: = # < <= > >= *) |
| 169 | CcSigned: [6]INT = [A.ccEQ, A.ccNE, A.ccLT, A.ccLE, A.ccGT, A.ccGE]; |
| 170 | CcUnsigned: [6]INT = [A.ccEQ, A.ccNE, A.ccLO, A.ccLS, A.ccHI, A.ccHS]; |
| 171 | (* and of reals, after fcmp: an unordered pair, a NaN, sets C and V |
| 172 | and not N or Z, so that only # holds, as IEEE wants; lt and le |
| 173 | would hold, as they test N # V, so < is mi and <= ls *) |
| 174 | CcReal: [6]INT = [A.ccEQ, A.ccNE, A.ccMI, A.ccLS, A.ccGT, A.ccGE]; |
| 175 | (* sub sp, sp, #imm12 and the same shifted by 12: the prologue's two, |
| 176 | whose immediates endProc patches *) |
| 177 | SubSp = 0D10003FFH; SubSp12 = 0D14003FFH; |
| 178 | |
| 179 | var |
| 180 | stack: [MaxStack]Entry; |
| 181 | owner: [NumRegs]INT; (* the value in each register, or -1 *) |
| 182 | frameTop, frameMax, permTop: I64; |
| 183 | frameFix: INT; (* where the frame's two subs are *) |
| 184 | paramTop: I64; (* the parameters' slots end here *) |
| 185 | zeroTop: I64; (* the locals the prologue zeroes end here *) |
| 186 | retSlot: I64; |
| 187 | resType: ^Sym.Type; (* the procedure's result, or nil *) |
| 188 | exitL: INT; |
| 189 | (* parameters arriving in registers, x0 to x7 or, told by a negative |
| 190 | number, -1 - n, v0 to v7, and a result's address in x8: where each |
| 191 | is stored on entry *) |
| 192 | inReg: [17]INT; |
| 193 | inSlot: [17]I64; |
| 194 | inSize: [17]INT; |
| 195 | inObj: [17]^Sym.Obj; |
| 196 | numIn: INT; |
| 197 | procParams: ^Sym.Obj; (* the parameters, for enterBody *) |
| 198 | regMask: INT; (* the registers of variables, as bits (XBit) *) |
| 199 | saveAt: I64; (* where they are saved, a quadword each *) |
| 200 | homes: [8]I64; (* the slots of the variables in XVarRegs *) |
| 201 | frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *) |
| 202 | (* the stubs that a failed check calls, one for each kind of failure, |
| 203 | made by entryPoint for those used; and the procedure's checks *) |
| 204 | failStubs: [8]INT; |
| 205 | fails: [4096]Fail; |
| 206 | numFails: INT; |
| 207 | |
| 208 | (* What this Gen won't make, SYSCALL, marks the procedure being compiled |
| 209 | (Chunk.setMissing), which is an error if the program uses it, and the |
| 210 | compile reads on: a module the program imports may hold what it never |
| 211 | calls. The routine keeps the stack as the real one would. *) |
| 212 | proc missing(msg: []const U8); |
| 213 | begin |
| 214 | if codeChunk >= 0 then Chunk.setMissing(codeChunk, msg, Scan.tokPos()); end; |
| 215 | end; |
| 216 | |
| 217 | (* -S: the text of each instruction is kept, and list prints it *) |
| 218 | proc setListing*(yes: BOOL); |
| 219 | begin |
| 220 | A.setListing(yes); |
| 221 | end; |
| 222 | |
| 223 | proc list*(home: INT); |
| 224 | begin |
| 225 | A.list(home); |
| 226 | end; |
| 227 | |
| 228 | (* A variable changed in place, x := x + e, is x86-64's; here it is |
| 229 | loaded and stored as any other *) |
| 230 | proc setInPlace*(r: INT); |
| 231 | begin |
| 232 | _ := r; |
| 233 | end; |
| 234 | |
| 235 | proc kindOf*(t: ^Sym.Type): Kind; |
| 236 | begin |
| 237 | if t^.form = Sym.fReal then |
| 238 | if t^.size = 4 then return kF32; end; |
| 239 | return kF64; |
| 240 | end; |
| 241 | if t^.size > WordSize then return kLong; end; |
| 242 | return kWord; |
| 243 | end; |
| 244 | |
| 245 | (* The stack *) |
| 246 | |
| 247 | proc isReal(i: INT): BOOL; |
| 248 | begin |
| 249 | return stack[i].k in {kF32, kF64}; |
| 250 | end; |
| 251 | |
| 252 | proc allocTemp*(size, align: I64): I64; forward; |
| 253 | proc allocPerm*(size, align: I64): I64; forward; |
| 254 | proc loaded(i: INT): INT; forward; |
| 255 | |
| 256 | (* The value i, in its register, goes to a frame slot: a real as 8 |
| 257 | bytes, an F32 with what lies above it *) |
| 258 | proc spill(i: INT); |
| 259 | var s: I64; r: INT; |
| 260 | begin |
| 261 | r := loaded(i); |
| 262 | s := allocTemp(8, 8); |
| 263 | if isReal(i) then A.storeF(r, FP, INT(s), 8); else A.store(r, FP, INT(s), 8); end; |
| 264 | stack[i].loc := lFrame; |
| 265 | stack[i].val := s; |
| 266 | owner[i mod NumRegs] := -1; |
| 267 | end; |
| 268 | |
| 269 | (* Every value below depth d in a register, spilled, as a call may |
| 270 | change them *) |
| 271 | proc spillBelow(d: INT); |
| 272 | var r: INT; |
| 273 | begin |
| 274 | for r := 0 to NumRegs - 1 do |
| 275 | if (owner[r] >= 0) and (owner[r] < d) then spill(owner[r]); end; |
| 276 | end; |
| 277 | end; |
| 278 | |
| 279 | (* The register of value i, for it to hold: a lower value there is |
| 280 | spilled *) |
| 281 | proc claim(i: INT): INT; |
| 282 | var r: INT; |
| 283 | begin |
| 284 | r := i mod NumRegs; |
| 285 | if (owner[r] >= 0) and (owner[r] # i) then spill(owner[r]); end; |
| 286 | owner[r] := i; |
| 287 | return Regs[r]; |
| 288 | end; |
| 289 | |
| 290 | (* The value i into register r, from wherever it is; it stays where it |
| 291 | was, so r is a new copy. A real goes as its bits. *) |
| 292 | proc valueTo(i, r: INT); |
| 293 | begin |
| 294 | match stack[i].loc of |
| 295 | | lConst: A.movRI(r, stack[i].val); |
| 296 | | lAdr: |
| 297 | if stack[i].chunk >= 0 then A.addrChunk(r, stack[i].chunk, INT(stack[i].val)); |
| 298 | else A.aluI(A.opAdd, r, FP, stack[i].val); end; |
| 299 | | lReg: |
| 300 | if isReal(i) then A.fmovRF(r, XRegs[i mod NumRegs], false); |
| 301 | elsif Regs[i mod NumRegs] # r then A.movRR(r, Regs[i mod NumRegs]); end; |
| 302 | | lFrame: A.load(r, FP, INT(stack[i].val), 8, false); |
| 303 | end; |
| 304 | end; |
| 305 | |
| 306 | (* The real i into register v, as valueTo does for an integer: a |
| 307 | constant by its bits, made in x15, or the zero register's *) |
| 308 | proc xValueTo(i, v: INT); |
| 309 | begin |
| 310 | match stack[i].loc of |
| 311 | | lConst: |
| 312 | if stack[i].val = 0 then A.fmovFR(v, ZR, false); |
| 313 | else A.movRI(T, stack[i].val); A.fmovFR(v, T, false); end; |
| 314 | | lReg: if XRegs[i mod NumRegs] # v then A.fop1(A.fMov, v, XRegs[i mod NumRegs], false); end; |
| 315 | | else: A.loadF(v, FP, INT(stack[i].val), 8); (* lFrame *) |
| 316 | end; |
| 317 | end; |
| 318 | |
| 319 | (* The value i, in its register, of its kind *) |
| 320 | proc loaded(i: INT): INT; |
| 321 | var r: INT; |
| 322 | begin |
| 323 | if stack[i].loc # lReg then |
| 324 | r := claim(i); |
| 325 | if isReal(i) then xValueTo(i, XRegs[i mod NumRegs]); else valueTo(i, r); end; |
| 326 | stack[i].loc := lReg; |
| 327 | end; |
| 328 | if isReal(i) then return XRegs[i mod NumRegs]; end; |
| 329 | return Regs[i mod NumRegs]; |
| 330 | end; |
| 331 | |
| 332 | proc top: INT; |
| 333 | begin |
| 334 | if depth = 0 then Diag.fatal("internal error: the stack is empty"); end; |
| 335 | return loaded(depth - 1); |
| 336 | end; |
| 337 | |
| 338 | (* A value on the stack's top; at its last place the compile ends, as on |
| 339 | x86-64 *) |
| 340 | proc newEntry(k: Kind; loc: Loc; v: I64); |
| 341 | begin |
| 342 | if depth > LEN(stack) - 1 then |
| 343 | _ := Scan.mark(Diag.say("more than {} values waiting in one expression", [Fmt.int(MaxStack)])); |
| 344 | Diag.finish(); |
| 345 | return; |
| 346 | end; |
| 347 | stack[depth].k := k; |
| 348 | stack[depth].loc := loc; |
| 349 | stack[depth].val := v; |
| 350 | stack[depth].chunk := -1; |
| 351 | INC(depth); |
| 352 | end; |
| 353 | |
| 354 | (* The register a new value on top is computed in, and a real's *) |
| 355 | proc target: INT; |
| 356 | begin |
| 357 | return claim(depth); |
| 358 | end; |
| 359 | |
| 360 | proc xTarget: INT; |
| 361 | begin |
| 362 | _ := claim(depth); |
| 363 | return XRegs[depth mod NumRegs]; |
| 364 | end; |
| 365 | |
| 366 | (* The value just computed in target goes on top *) |
| 367 | proc computed(k: Kind); |
| 368 | begin |
| 369 | newEntry(k, lReg, 0); |
| 370 | end; |
| 371 | |
| 372 | (* drop emits no code, and the dropped value's register holds it until |
| 373 | the next push claims it, which place and operands rely on *) |
| 374 | proc drop*; |
| 375 | begin |
| 376 | DEC(depth); |
| 377 | if owner[depth mod NumRegs] = depth then owner[depth mod NumRegs] := -1; end; |
| 378 | end; |
| 379 | |
| 380 | proc popTo(r: INT); |
| 381 | begin |
| 382 | valueTo(depth - 1, r); |
| 383 | drop(); |
| 384 | end; |
| 385 | |
| 386 | proc topConst: (BOOL, I64); |
| 387 | begin |
| 388 | return stack[depth - 1].loc = lConst, stack[depth - 1].val; |
| 389 | end; |
| 390 | |
| 391 | proc resetStack*(d, p: INT); |
| 392 | var r: INT; |
| 393 | begin |
| 394 | depth := d; |
| 395 | pushed := p; |
| 396 | for r := 0 to NumRegs - 1 do owner[r] := -1; end; |
| 397 | end; |
| 398 | |
| 399 | (* A constant; a real's v is its bits, an F64's or, in the low 32, an |
| 400 | F32's *) |
| 401 | proc pushConst*(k: Kind; v: I64); |
| 402 | begin |
| 403 | newEntry(k, lConst, v); |
| 404 | end; |
| 405 | |
| 406 | (* The register of the word at a, a variable in registers: the |
| 407 | pointer's (off 0) or, for a slice, the length's (off 8) *) |
| 408 | proc regAt(a: Adr): INT; |
| 409 | begin |
| 410 | if a.off = 0 then return a.chunk; end; |
| 411 | return a.hi; |
| 412 | end; |
| 413 | |
| 414 | (* The memory a names. An address on the stack is popped, and is in its |
| 415 | register still; one in a slot is loaded into r. *) |
| 416 | proc place(a: Adr; r: INT): Mem; |
| 417 | var m: Mem; |
| 418 | begin |
| 419 | m.chunk := -1; |
| 420 | m.off := a.off; |
| 421 | match a.base of |
| 422 | | bFrame: m.base := FP; |
| 423 | | bGlobal: m.chunk := a.chunk; |
| 424 | | bStack: |
| 425 | if (stack[depth - 1].loc = lAdr) and (stack[depth - 1].chunk >= 0) then |
| 426 | m.chunk := stack[depth - 1].chunk; |
| 427 | m.off := m.off + stack[depth - 1].val; |
| 428 | elsif stack[depth - 1].loc = lAdr then |
| 429 | m.base := FP; |
| 430 | m.off := m.off + stack[depth - 1].val; |
| 431 | else |
| 432 | m.base := loaded(depth - 1); |
| 433 | end; |
| 434 | drop(); |
| 435 | | bPtr: m.base := a.chunk; |
| 436 | | bSlot: |
| 437 | A.load(r, FP, a.chunk, 8, false); |
| 438 | m.base := r; |
| 439 | | bReg: Diag.fatal("internal error: a register has no address"); |
| 440 | end; |
| 441 | return m; |
| 442 | end; |
| 443 | |
| 444 | (* Register r loaded from m, or stored there, size bytes *) |
| 445 | proc loadMem(r: INT; m: Mem; size: INT; signed: BOOL); |
| 446 | begin |
| 447 | if m.chunk >= 0 then A.loadChunk(r, m.chunk, INT(m.off), size, signed); |
| 448 | else A.load(r, m.base, INT(m.off), size, signed); end; |
| 449 | end; |
| 450 | |
| 451 | proc storeMem(r: INT; m: Mem; size: INT); |
| 452 | begin |
| 453 | if m.chunk >= 0 then A.storeChunk(r, m.chunk, INT(m.off), size); |
| 454 | else A.store(r, m.base, INT(m.off), size); end; |
| 455 | end; |
| 456 | |
| 457 | (* A real's register v loaded from m, or stored there if not load *) |
| 458 | proc realMem(v: INT; m: Mem; size: INT; load: BOOL); |
| 459 | begin |
| 460 | if (m.chunk >= 0) and load then A.loadChunkF(v, m.chunk, INT(m.off), size); |
| 461 | elsif m.chunk >= 0 then A.storeChunkF(v, m.chunk, INT(m.off), size); |
| 462 | elsif load then A.loadF(v, m.base, INT(m.off), size); |
| 463 | else A.storeF(v, m.base, INT(m.off), size); end; |
| 464 | end; |
| 465 | |
| 466 | (* Register r := the address m *) |
| 467 | proc adrTo(r: INT; m: Mem); |
| 468 | begin |
| 469 | if m.chunk >= 0 then A.addrChunk(r, m.chunk, INT(m.off)); |
| 470 | elsif (m.off # 0) or (m.base # r) then A.aluI(A.opAdd, r, m.base, m.off); end; |
| 471 | end; |
| 472 | |
| 473 | (* The address a names, on top of the stack *) |
| 474 | proc pushAdr*(a: Adr); |
| 475 | var m: Mem; r: INT; |
| 476 | begin |
| 477 | if (a.base = bStack) and (a.off = 0) then return; end; (* it is on top already *) |
| 478 | if a.base in {bFrame, bGlobal} then |
| 479 | newEntry(kWord, lAdr, a.off); |
| 480 | if a.base = bGlobal then stack[depth - 1].chunk := a.chunk; end; |
| 481 | return; |
| 482 | end; |
| 483 | (* With the address on the stack, place pops it first, so that target |
| 484 | may take its register; else target comes first, for place to load |
| 485 | a slot's pointer into *) |
| 486 | if a.base = bStack then |
| 487 | m := place(a, U); r := target(); |
| 488 | else |
| 489 | r := target(); m := place(a, r); |
| 490 | end; |
| 491 | adrTo(r, m); |
| 492 | computed(kWord); |
| 493 | end; |
| 494 | |
| 495 | proc pushVar*(a: Adr; size: INT; signed: BOOL; k: Kind); |
| 496 | var m: Mem; r: INT; fl: BOOL; |
| 497 | begin |
| 498 | fl := k in {kF32, kF64}; |
| 499 | if (a.base = bReg) and fl then |
| 500 | A.fop1(A.fMov, xTarget(), regAt(a) - XmmVar, false); |
| 501 | elsif a.base = bReg then |
| 502 | A.movRR(target(), regAt(a)); |
| 503 | else |
| 504 | if a.base = bStack then |
| 505 | m := place(a, U); r := target(); |
| 506 | else |
| 507 | r := target(); m := place(a, r); |
| 508 | end; |
| 509 | if fl then realMem(xTarget(), m, size, true); else loadMem(r, m, size, signed); end; |
| 510 | end; |
| 511 | computed(k); |
| 512 | end; |
| 513 | |
| 514 | proc pushProc*(c: INT; ext: BOOL); |
| 515 | var r: INT; |
| 516 | begin |
| 517 | r := target(); |
| 518 | if ext then A.loadChunk(r, c, 0, 8, false); else A.addrChunk(r, c, 0); end; |
| 519 | computed(kWord); |
| 520 | end; |
| 521 | |
| 522 | (* Pops a value into the variable at a; an address on the stack is under |
| 523 | the value. A zero is stored from the zero register, and a real |
| 524 | constant as its bits. A real variable in a register is written |
| 525 | through to its slot, as on x86-64, where the trap report finds it. *) |
| 526 | proc popVar*(a: Adr; size: INT); |
| 527 | var r: INT; v: I64; c, fl: BOOL; |
| 528 | begin |
| 529 | if (a.base = bReg) and isReal(depth - 1) then |
| 530 | r := regAt(a) - XmmVar; |
| 531 | xValueTo(depth - 1, r); |
| 532 | drop(); |
| 533 | A.storeF(r, FP, INT(homes[r - 8]), 8); |
| 534 | return; |
| 535 | end; |
| 536 | if a.base = bReg then |
| 537 | popTo(regAt(a)); |
| 538 | return; |
| 539 | end; |
| 540 | c, v := topConst(); |
| 541 | fl := not c and isReal(depth - 1); |
| 542 | if c and (v = 0) then r := ZR; |
| 543 | elsif c then r := T; A.movRI(T, v); |
| 544 | else r := loaded(depth - 1); end; |
| 545 | drop(); |
| 546 | if fl then realMem(r, place(a, U), size, false); else storeMem(r, place(a, U), size); end; |
| 547 | end; |
| 548 | |
| 549 | (* The two values on top, of one kind, change places, each loaded in |
| 550 | its register *) |
| 551 | proc swap*; |
| 552 | var e: Entry; x, y: INT; |
| 553 | begin |
| 554 | x := loaded(depth - 2); |
| 555 | y := loaded(depth - 1); |
| 556 | if isReal(depth - 1) then |
| 557 | A.fop1(A.fMov, XT, x, false); A.fop1(A.fMov, x, y, false); A.fop1(A.fMov, y, XT, false); |
| 558 | else |
| 559 | A.movRR(T, x); A.movRR(x, y); A.movRR(y, T); |
| 560 | end; |
| 561 | e := stack[depth - 2]; stack[depth - 2] := stack[depth - 1]; stack[depth - 1] := e; |
| 562 | end; |
| 563 | |
| 564 | (* Copying, by loads and stores of 8, 4, 2 and 1 bytes, from [x1] to |
| 565 | [x0], through x2. Past MaxMoves bytes the words go by a loop, which |
| 566 | moves x0 and x1 on, counting down in x3, and the last few bytes |
| 567 | after it as before: |
| 568 | |
| 569 | mov x3, #words |
| 570 | top: ldr x2, [x1], #8 |
| 571 | str x2, [x0], #8 |
| 572 | subs x3, x3, #1 |
| 573 | b.ne top |
| 574 | ... ; the size mod 8 bytes left, from [x1] to [x0] |
| 575 | |
| 576 | zeroBytes is the same, storing the zero register. *) |
| 577 | |
| 578 | const MaxMoves = 256; |
| 579 | |
| 580 | (* The loop's head and tail, of the words of size bytes, by store and |
| 581 | load if load; returns what is left after the words *) |
| 582 | proc wordLoop(size: I64; load: BOOL): I64; |
| 583 | var top: INT; |
| 584 | begin |
| 585 | if size <= MaxMoves then return size; end; |
| 586 | A.movRI(3, size div 8); |
| 587 | top := Chunk.pc; |
| 588 | if load then A.loadIdx(Tmp, Src, 8, 8, A.mPost); A.storeIdx(Tmp, Dst, 8, 8, A.mPost); |
| 589 | else A.storeIdx(ZR, Dst, 8, 8, A.mPost); end; |
| 590 | A.aluI(A.opSubs, 3, 3, 1); |
| 591 | A.jccBack(A.ccNE, top); |
| 592 | return size mod 8; |
| 593 | end; |
| 594 | |
| 595 | proc copyBytes(size: I64); |
| 596 | var o, n: I64; |
| 597 | begin |
| 598 | size := wordLoop(size, true); |
| 599 | o := 0; |
| 600 | while o < size do |
| 601 | n := 8; |
| 602 | while n > size - o do n := n div 2; end; |
| 603 | A.load(Tmp, Src, INT(o), INT(n), false); |
| 604 | A.store(Tmp, Dst, INT(o), INT(n)); |
| 605 | INC(o, n); |
| 606 | end; |
| 607 | end; |
| 608 | |
| 609 | proc zeroBytes(size: I64); |
| 610 | var o, n: I64; |
| 611 | begin |
| 612 | size := wordLoop(size, false); |
| 613 | o := 0; |
| 614 | while o < size do |
| 615 | n := 8; |
| 616 | while n > size - o do n := n div 2; end; |
| 617 | A.store(ZR, Dst, INT(o), INT(n)); |
| 618 | INC(o, n); |
| 619 | end; |
| 620 | end; |
| 621 | |
| 622 | (* Pops a source's address, and a destination's under it, and copies |
| 623 | size bytes to off bytes past the destination *) |
| 624 | proc copyBlock*(size, off: I64); |
| 625 | begin |
| 626 | popTo(Src); |
| 627 | popTo(Dst); |
| 628 | if off # 0 then A.aluI(A.opAdd, Dst, Dst, off); end; |
| 629 | copyBytes(size); |
| 630 | end; |
| 631 | |
| 632 | (* Pops a source's address, and copies size bytes to a: for a slice in |
| 633 | registers, its two words into them *) |
| 634 | proc copyTo*(a: Adr; size: I64); |
| 635 | begin |
| 636 | popTo(Src); |
| 637 | if a.base = bReg then |
| 638 | A.load(a.chunk, Src, 0, 8, false); |
| 639 | A.load(a.hi, Src, 8, 8, false); |
| 640 | return; |
| 641 | end; |
| 642 | adrTo(Dst, place(a, Dst)); |
| 643 | copyBytes(size); |
| 644 | end; |
| 645 | |
| 646 | proc zeroVar*(a: Adr; size: I64); |
| 647 | begin |
| 648 | adrTo(Dst, place(a, Dst)); |
| 649 | zeroBytes(size); |
| 650 | end; |
| 651 | |
| 652 | (* Checks *) |
| 653 | |
| 654 | (* The name s, interned *) |
| 655 | proc internStr(s: []const U8): INT; |
| 656 | begin |
| 657 | return Scan.intern(SYSTEM.PTR(s), LEN(s)); |
| 658 | end; |
| 659 | |
| 660 | (* The program goes on if the condition cc holds, and otherwise stops |
| 661 | in Rt.fail(what, a, b, len, at, name), as on x86-64, whose check says |
| 662 | what each is (Gen.x86_64.raz). Here a is in register ra and b, where |
| 663 | there is one, in x2; len is in register lenReg, or the constant n if |
| 664 | lenReg is -1. The failing code is out of the way, at the procedure's |
| 665 | end (endFails), so that a check that holds is a branch not taken; it |
| 666 | has p for its place, worked out now. *) |
| 667 | proc check(cc, what: INT; p: Diag.Pos; ra, lenReg: INT; n: I64; name: INT); |
| 668 | var f: INT; |
| 669 | begin |
| 670 | if numFails > LEN(fails) - 1 then |
| 671 | _ := Scan.mark(Diag.say("more than {} checks in one procedure", [Fmt.int(LEN(fails))])); |
| 672 | return; |
| 673 | end; |
| 674 | f := numFails; |
| 675 | fails[f].L := NoLabel; |
| 676 | A.jcc(A.invert(cc), fails[f].L); |
| 677 | fails[f].what := what; |
| 678 | fails[f].ra := ra; |
| 679 | fails[f].lenReg := lenReg; |
| 680 | fails[f].n := n; |
| 681 | fails[f].name := name; |
| 682 | fails[f].path := Chunk.curFilePath(); |
| 683 | fails[f].line, fails[f].col := Scan.lineCol(p.a); |
| 684 | INC(numFails); |
| 685 | end; |
| 686 | |
| 687 | (* The failures of the procedure's checks: each puts Rt.fail's values in |
| 688 | the registers it takes them in, x3 first, since a is never there, |
| 689 | then x1 and x5, and calls the stub for its kind, which adds what |
| 690 | failed and where (entryPoint): |
| 691 | |
| 692 | mov x3, x10 ; the length, or mov x3, #n |
| 693 | mov x1, x15 ; a |
| 694 | adrp x5, name; add x5 ... ; an enumeration's name |
| 695 | bl check0 *) |
| 696 | proc endFails; |
| 697 | var i, path: INT; f: Fail; |
| 698 | begin |
| 699 | path := Chunk.curFilePath(); |
| 700 | for i := 0 to numFails - 1 do |
| 701 | f := fails[i]; |
| 702 | A.fixLink(f.L); |
| 703 | Chunk.setCurFile(f.path); |
| 704 | Chunk.noteLine(f.line, f.col); |
| 705 | Chunk.setCurFile(path); |
| 706 | if f.lenReg < 0 then A.movRI(3, f.n); elsif f.lenReg # 3 then A.movRR(3, f.lenReg); end; |
| 707 | if f.ra # 1 then A.movRR(1, f.ra); end; |
| 708 | if f.name >= 0 then A.addrChunk(5, f.name, 0); end; |
| 709 | if failStubs[f.what] < 0 then |
| 710 | failStubs[f.what] := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr(StubNames[f.what])); |
| 711 | end; |
| 712 | A.callChunk(failStubs[f.what]); |
| 713 | end; |
| 714 | numFails := 0; |
| 715 | end; |
| 716 | |
| 717 | (* Operations *) |
| 718 | |
| 719 | (* The right operand of the two values on top, popped, in register y: |
| 720 | its own, or U for a constant; the left one stays, in its register, |
| 721 | which is returned with y *) |
| 722 | proc operands: (INT, INT); |
| 723 | var v: I64; c: BOOL; y: INT; |
| 724 | begin |
| 725 | c, v := topConst(); |
| 726 | if c then A.movRI(U, v); y := U; else y := loaded(depth - 1); end; |
| 727 | drop(); |
| 728 | return top(), y; |
| 729 | end; |
| 730 | |
| 731 | (* The operation of two registers of +, -, &, | and xor *) |
| 732 | proc aluOf(sy: Scan.Sym): INT; |
| 733 | begin |
| 734 | match sy of |
| 735 | | sPlus: return A.opAdd; |
| 736 | | sMinus: return A.opSub; |
| 737 | | sAmp: return A.opAnd; |
| 738 | | sBar: return A.opOrr; |
| 739 | | else: return A.opEor; |
| 740 | end; |
| 741 | end; |
| 742 | |
| 743 | (* r div y, or r mod y, of bits bits, signed or not, y being the |
| 744 | constant v if c. x86's idiv stops the program for a division by |
| 745 | zero, and for the smallest value divided by -1, which overflows; the |
| 746 | report binds both (6.1), and arm64's sdiv and udiv give a value for |
| 747 | each, so the code checks first, as Rt.fail's kind 5, a division |
| 748 | fault. The smallest value is that of the type's width, for a narrow |
| 749 | one, an I32's -2^31, sign-extended in 64 bits. Of an I64: |
| 750 | |
| 751 | cmp x17, #0 |
| 752 | b.eq fail ; by zero |
| 753 | cmn x17, #1 |
| 754 | b.ne L |
| 755 | mov x15, #8000000000000000H |
| 756 | cmp x9, x15 |
| 757 | b.eq fail ; the smallest by -1 |
| 758 | L: sdiv x15, x9, x17 |
| 759 | mov x9, x15 ; div; for mod, msub x9, x15, x17, x9 |
| 760 | |
| 761 | msub is x9 - x15 * x17, what is left. A constant divisor other than |
| 762 | 0 needs no first check, and other than -1 no second. *) |
| 763 | proc divide(sy: Scan.Sym; r, y, bits: INT; signed, c: BOOL; v: I64); |
| 764 | var L: INT; |
| 765 | begin |
| 766 | if not c or (v = 0) then |
| 767 | A.cmpI(y, 0); |
| 768 | check(A.ccNE, 5, Scan.tokPos(), r, -1, 0, -1); |
| 769 | end; |
| 770 | if signed and (not c or (v = -1)) then |
| 771 | L := NoLabel; |
| 772 | A.cmpI(y, -1); |
| 773 | A.jcc(A.ccNE, L); |
| 774 | A.movRI(T, I64(-1) shl (bits - 1)); |
| 775 | A.cmp(r, T); |
| 776 | check(A.ccNE, 5, Scan.tokPos(), r, -1, 0, -1); |
| 777 | A.fixLink(L); |
| 778 | end; |
| 779 | A.divide(signed, T, r, y); |
| 780 | if sy = Scan.sDiv then A.movRR(r, T); else A.madd(r, T, y, r, true); end; |
| 781 | end; |
| 782 | |
| 783 | (* The two reals on top, the left op the right, in the left's |
| 784 | register: fadd d16, d16, d17 *) |
| 785 | proc realOp(sy: Scan.Sym; k: Kind); |
| 786 | var x, y, o: INT; |
| 787 | begin |
| 788 | match sy of |
| 789 | | sPlus: o := A.fAdd; |
| 790 | | sMinus: o := A.fSub; |
| 791 | | sTimes: o := A.fMul; |
| 792 | | else: o := A.fDiv; |
| 793 | end; |
| 794 | y := loaded(depth - 1); |
| 795 | drop(); |
| 796 | x := top(); |
| 797 | A.fop(o, x, x, y, k = kF32); |
| 798 | end; |
| 799 | |
| 800 | (* The two values on top, the left op the right, an integer of size |
| 801 | bytes, signed or not, computed in 64 bits and brought back to its |
| 802 | width, which is how a narrow integer wraps. A shift's count is taken |
| 803 | modulo the width, as x86-64's code takes it. *) |
| 804 | proc op*(sy: Scan.Sym; k: Kind; size: INT; signed: BOOL); |
| 805 | var v: I64; r, y, bits, sh: INT; c, alu: BOOL; |
| 806 | begin |
| 807 | if k in {kF32, kF64} then realOp(sy, k); return; end; |
| 808 | bits := size * 8; |
| 809 | alu := (sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor); |
| 810 | (* a constant goes right, where it can be an immediate *) |
| 811 | if (alu and (sy # Scan.sMinus) or (sy = Scan.sTimes)) and (stack[depth - 2].loc = lConst) |
| 812 | and (stack[depth - 1].loc # lConst) then swap(); end; |
| 813 | if sy = Scan.sShl then sh := A.shLsl; elsif signed then sh := A.shAsr; else sh := A.shLsr; end; |
| 814 | c, v := topConst(); |
| 815 | if c and alu then |
| 816 | drop(); |
| 817 | r := top(); |
| 818 | A.aluI(aluOf(sy), r, r, v); |
| 819 | elsif c and ((sy = Scan.sShl) or (sy = Scan.sShr)) then |
| 820 | drop(); |
| 821 | r := top(); |
| 822 | A.shiftI(sh, r, r, INT(v & I64(bits - 1)), 8); |
| 823 | else |
| 824 | r, y := operands(); |
| 825 | if alu then |
| 826 | A.alu(aluOf(sy), r, r, y); |
| 827 | elsif sy = Scan.sTimes then |
| 828 | A.mul(r, r, y); |
| 829 | elsif (sy = Scan.sDiv) or (sy = Scan.sMod) then |
| 830 | divide(sy, r, y, bits, signed, c, v); |
| 831 | else (* sShl, sShr: the machine takes the count modulo 64 *) |
| 832 | if bits < 64 then A.aluI(A.opAnd, U, y, bits - 1); y := U; end; |
| 833 | A.shift(sh, r, r, y, 8); |
| 834 | end; |
| 835 | end; |
| 836 | if bits < 64 then A.extend(r, r, size, signed); end; |
| 837 | stack[depth - 1].k := k; |
| 838 | end; |
| 839 | |
| 840 | (* ROL, if left, or ROR of the integer of size bytes under the count on |
| 841 | top, at its own width, the count taken modulo it, a negative one as |
| 842 | a shift's is (Gen.x86_64.raz says more). arm64 rotates only right, |
| 843 | by ror, and only 32 or 64 bits: a left rotation is a right one by |
| 844 | minus the count, and a U8 or a U16 is first repeated through 32 |
| 845 | bits, so that what leaves its low end comes back at its top, as a |
| 846 | rotation of 32 bits by any count rotates each of the copies alike. |
| 847 | ROL(x, n) of a U8: |
| 848 | |
| 849 | and x9, x9, #0FFH ; repeated: 000000AB to ABABABAB |
| 850 | orr x9, x9, x9, lsl #8 |
| 851 | orr x9, x9, x9, lsl #16 |
| 852 | neg x17, x10 ; left by n is right by -n |
| 853 | ror w9, w9, w17 ; the count modulo 32, so modulo 8 too |
| 854 | and x9, x9, #0FFH |
| 855 | |
| 856 | A constant count is rotated by as ror x9, x9, #k, none if 0. As on |
| 857 | x86-64, a rotation of 32 bits leaves the upper ones zero; a narrow |
| 858 | one here leaves them zero too, where x86-64 leaves them as they |
| 859 | were, which differs only for a signed integer. *) |
| 860 | proc rotate*(left: BOOL; size: INT); |
| 861 | var c: BOOL; v: I64; r, y, w, sz: INT; |
| 862 | begin |
| 863 | w := size * 8; |
| 864 | sz := 4; |
| 865 | if size = 8 then sz := 8; end; |
| 866 | c, v := topConst(); |
| 867 | v := v & I64(w - 1); |
| 868 | if left then v := (I64(w) - v) & I64(w - 1); end; |
| 869 | if c then |
| 870 | drop(); |
| 871 | r := top(); |
| 872 | if v = 0 then stack[depth - 1].k := kWord; return; end; |
| 873 | else |
| 874 | r, y := operands(); |
| 875 | if left then A.neg(U, y); y := U; end; |
| 876 | end; |
| 877 | if w < 32 then |
| 878 | A.aluI(A.opAnd, r, r, (I64(1) shl w) - 1); |
| 879 | A.aluShift(A.opOrr, r, r, r, A.shLsl, w); |
| 880 | if w = 8 then A.aluShift(A.opOrr, r, r, r, A.shLsl, 16); end; |
| 881 | end; |
| 882 | if c then A.shiftI(A.shRor, r, r, INT(v), sz); else A.shift(A.shRor, r, r, y, sz); end; |
| 883 | if w < 32 then A.aluI(A.opAnd, r, r, (I64(1) shl w) - 1); end; |
| 884 | stack[depth - 1].k := kWord; |
| 885 | end; |
| 886 | |
| 887 | (* CLZ, if leading, or CTZ of the unsigned integer of size bytes on top |
| 888 | (report section 6.1), whose bits above its width are zero. clz |
| 889 | counts the leading zeros of 32 or 64 bits, the whole width for 0; |
| 890 | of a U8 or a U16, those of 32 less the 24 or 16 above it. CTZ is |
| 891 | clz of the bits reversed (rbit), and of a U8 or a U16 a one bit set |
| 892 | just above its width first, so that 0 counts the width: |
| 893 | |
| 894 | CLZ, a U8 CTZ, a U8 |
| 895 | clz w9, w9 orr x9, x9, #100H |
| 896 | sub x9, x9, #24 rbit w9, w9 |
| 897 | clz w9, w9 *) |
| 898 | proc countZeros*(leading: BOOL; size: INT); |
| 899 | var r, w, sz: INT; |
| 900 | begin |
| 901 | r := top(); |
| 902 | w := size * 8; |
| 903 | sz := 4; |
| 904 | if size = 8 then sz := 8; end; |
| 905 | if not leading and (w < 32) then A.aluI(A.opOrr, r, r, I64(1) shl w); end; |
| 906 | if not leading then A.rbit(r, r, sz); end; |
| 907 | A.clz(r, r, sz); |
| 908 | if leading and (w < 32) then A.aluI(A.opSub, r, r, 32 - w); end; |
| 909 | stack[depth - 1].k := kWord; |
| 910 | end; |
| 911 | |
| 912 | (* POPCOUNT of the integer or set on top (report section 6.1), by the |
| 913 | vector instructions every arm64 has, as C's compilers make it: cnt |
| 914 | counts the ones in each of the eight bytes, and addv adds them: |
| 915 | |
| 916 | fmov d23, x9 |
| 917 | cnt v23.8b, v23.8b |
| 918 | addv b23, v23.8b |
| 919 | fmov x9, d23 *) |
| 920 | proc popCount*; |
| 921 | var r: INT; |
| 922 | begin |
| 923 | r := top(); |
| 924 | A.fmovFR(XT, r, false); |
| 925 | A.cnt(XT, XT); |
| 926 | A.addv(XT, XT); |
| 927 | A.fmovRF(r, XT, false); |
| 928 | stack[depth - 1].k := kWord; |
| 929 | end; |
| 930 | |
| 931 | (* ABS of the real, or the signed integer, on top. A real's sign bit is |
| 932 | cleared, fabs, a NaN's too, as x86-64 clears it. An integer is |
| 933 | negated if negative, and cut back to its width, so that the smallest |
| 934 | value gives itself, as -x does (Gen.x86_64.raz says more): |
| 935 | |
| 936 | cmp x9, #0 |
| 937 | cneg x9, x9, lt ; csneg x9, x9, x9, ge |
| 938 | sxtw x9, w9 ; an I32 *) |
| 939 | proc abs*(k: Kind; size: INT); |
| 940 | var r: INT; |
| 941 | begin |
| 942 | r := top(); |
| 943 | if k in {kF32, kF64} then A.fop1(A.fAbs, r, r, k = kF32); return; end; |
| 944 | A.cmpI(r, 0); |
| 945 | A.csel(A.csNeg, A.ccGE, r, r, r); |
| 946 | if size < 8 then A.extend(r, r, size, true); end; |
| 947 | end; |
| 948 | |
| 949 | (* The two values on top, compared: a BOOL, 1 or 0. Two reals are |
| 950 | compared by fcmp, and the BOOL is made in the left's integer |
| 951 | register: fcmp d16, d17; cset x9, mi for < (CcReal) *) |
| 952 | proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL); |
| 953 | var r, x, y, i: INT; |
| 954 | begin |
| 955 | i := INT(sy) - INT(Scan.sEql); |
| 956 | if k in {kF32, kF64} then |
| 957 | y := loaded(depth - 1); |
| 958 | drop(); |
| 959 | x := top(); |
| 960 | drop(); |
| 961 | r := target(); |
| 962 | A.fcmp(x, y, k = kF32); |
| 963 | A.cset(CcReal[i], r); |
| 964 | computed(kWord); |
| 965 | return; |
| 966 | end; |
| 967 | r, y := operands(); |
| 968 | A.cmp(r, y); |
| 969 | if signed then A.cset(CcSigned[i], r); else A.cset(CcUnsigned[i], r); end; |
| 970 | stack[depth - 1].k := kWord; |
| 971 | end; |
| 972 | |
| 973 | (* -x; of a real, its sign bit flipped, fneg, as x86-64 flips it *) |
| 974 | proc neg*(k: Kind; size: INT; signed: BOOL); |
| 975 | var r: INT; |
| 976 | begin |
| 977 | r := top(); |
| 978 | if k in {kF32, kF64} then A.fop1(A.fNeg, r, r, k = kF32); return; end; |
| 979 | A.neg(r, r); |
| 980 | if size < 8 then A.extend(r, r, size, signed); end; |
| 981 | end; |
| 982 | |
| 983 | proc complement*(size: INT; signed: BOOL); |
| 984 | var r: INT; |
| 985 | begin |
| 986 | r := top(); |
| 987 | A.mvn(r, r); |
| 988 | if size < 8 then A.extend(r, r, size, signed); end; |
| 989 | end; |
| 990 | |
| 991 | proc notBool*; |
| 992 | var r: INT; |
| 993 | begin |
| 994 | r := top(); |
| 995 | A.aluI(A.opEor, r, r, 1); |
| 996 | end; |
| 997 | |
| 998 | (* The integer on top, brought to size bytes *) |
| 999 | proc narrow*(size: INT; signed: BOOL); |
| 1000 | var r: INT; |
| 1001 | begin |
| 1002 | r := top(); |
| 1003 | if size < 8 then A.extend(r, r, size, signed); end; |
| 1004 | stack[depth - 1].k := kWord; |
| 1005 | end; |
| 1006 | |
| 1007 | (* The integer on top, as a real of kind k, in its real register: scvtf |
| 1008 | of the 64 bits, or ucvtf if big, a U64 that may be above high(I64), |
| 1009 | which x86-64 halves and doubles to the same value *) |
| 1010 | proc intToReal*(k: Kind; big: BOOL); |
| 1011 | var r: INT; |
| 1012 | begin |
| 1013 | r := top(); |
| 1014 | A.scvtf(XRegs[(depth - 1) mod NumRegs], r, k = kF32, not big); |
| 1015 | stack[depth - 1].k := k; |
| 1016 | end; |
| 1017 | |
| 1018 | (* The real of kind k on top, toward zero, as an I64 (report 6.1, the |
| 1019 | table of conversions); out of an I64's range, or a NaN, it is the |
| 1020 | smallest I64, as x86-64's cvttsd2si gives. fcvtzs gives the nearest |
| 1021 | I64 instead, and 0 for a NaN, so the result is mended: a NaN is |
| 1022 | unordered with itself, which sets V, and only the largest I64, which |
| 1023 | no real converts to exactly, overflows when 1 is added, setting V: |
| 1024 | |
| 1025 | fcvtzs x9, d16 |
| 1026 | mov x15, #8000000000000000H |
| 1027 | fcmp d16, d16 |
| 1028 | csel x9, x15, x9, vs ; a NaN |
| 1029 | cmn x9, #1 |
| 1030 | csel x9, x15, x9, vs ; too big |
| 1031 | Too small is the smallest already. *) |
| 1032 | proc realToInt*(k: Kind); |
| 1033 | var x, r: INT; |
| 1034 | begin |
| 1035 | x := top(); |
| 1036 | r := Regs[(depth - 1) mod NumRegs]; |
| 1037 | A.fcvtz(r, x, k = kF32, true); |
| 1038 | A.movRI(T, MIN(I64)); |
| 1039 | A.fcmp(x, x, k = kF32); |
| 1040 | A.csel(A.csSel, A.ccVS, r, T, r); |
| 1041 | A.cmpI(r, -1); |
| 1042 | A.csel(A.csSel, A.ccVS, r, T, r); |
| 1043 | stack[depth - 1].k := kWord; |
| 1044 | end; |
| 1045 | |
| 1046 | (* The real of kind k on top, as the other kind: fcvt, which rounds to |
| 1047 | the nearest as x86-64's cvtsd2ss does *) |
| 1048 | proc realToReal*(k: Kind); |
| 1049 | var x: INT; |
| 1050 | begin |
| 1051 | x := top(); |
| 1052 | A.fcvt(x, x, k = kF64); |
| 1053 | if k = kF32 then stack[depth - 1].k := kF64; else stack[depth - 1].k := kF32; end; |
| 1054 | end; |
| 1055 | |
| 1056 | (* Whether the slices in the frame at a and b (the offsets of their |
| 1057 | pointers, the lengths 8 bytes on) are equal (or not, if not eq): the |
| 1058 | lengths, then the bytes, len shl shift of them, a byte at a time. The |
| 1059 | flags say equal or not, where the loop leaves it: |
| 1060 | |
| 1061 | ldr x2, [x29, #b + 8] |
| 1062 | ldr x15, [x29, #a + 8] |
| 1063 | cmp x2, x15 |
| 1064 | b.ne out ; the lengths differ |
| 1065 | ldr x1, [x29, #b] |
| 1066 | ldr x0, [x29, #a] |
| 1067 | lsl x2, x2, #shift |
| 1068 | top: cmp x2, #0 |
| 1069 | b.eq out ; no bytes left: equal |
| 1070 | ldrb w15, [x0], #1 |
| 1071 | ldrb w17, [x1], #1 |
| 1072 | sub x2, x2, #1 |
| 1073 | cmp x15, x17 |
| 1074 | b.eq top |
| 1075 | out: cset x9, eq ; or ne *) |
| 1076 | proc compareSeq*(a, b: I64; shift: INT; eq: BOOL); |
| 1077 | var r, out, loop: INT; |
| 1078 | begin |
| 1079 | r := target(); |
| 1080 | out := NoLabel; |
| 1081 | A.load(Tmp, FP, INT(b) + 8, 8, false); |
| 1082 | A.load(T, FP, INT(a) + 8, 8, false); |
| 1083 | A.cmp(Tmp, T); |
| 1084 | A.jcc(A.ccNE, out); |
| 1085 | A.load(Src, FP, INT(b), 8, false); |
| 1086 | A.load(Dst, FP, INT(a), 8, false); |
| 1087 | if shift > 0 then A.shiftI(A.shLsl, Tmp, Tmp, shift, 8); end; |
| 1088 | loop := Chunk.pc; |
| 1089 | A.cmpI(Tmp, 0); |
| 1090 | A.jcc(A.ccEQ, out); |
| 1091 | A.loadIdx(T, Dst, 1, 1, A.mPost); |
| 1092 | A.loadIdx(U, Src, 1, 1, A.mPost); |
| 1093 | A.aluI(A.opSub, Tmp, Tmp, 1); |
| 1094 | A.cmp(T, U); |
| 1095 | A.jccBack(A.ccEQ, loop); |
| 1096 | A.fixLink(out); |
| 1097 | if eq then A.cset(A.ccEQ, r); else A.cset(A.ccNE, r); end; |
| 1098 | computed(kWord); |
| 1099 | end; |
| 1100 | |
| 1101 | (* An error points at its message's length, an 8-byte word, with the |
| 1102 | text after it; nil is no error. Pops one, and makes the slice of its |
| 1103 | message at tmp, empty for nil. *) |
| 1104 | proc errorText*(tmp: I64); |
| 1105 | var L: INT; |
| 1106 | begin |
| 1107 | popTo(T); |
| 1108 | A.movRI(U, 0); |
| 1109 | L := NoLabel; |
| 1110 | A.cbz(false, T, L); |
| 1111 | A.load(U, T, 0, 8, false); |
| 1112 | A.aluI(A.opAdd, T, T, 8); |
| 1113 | A.fixLink(L); |
| 1114 | A.store(T, FP, INT(tmp), 8); |
| 1115 | A.store(U, FP, INT(tmp) + 8, 8); |
| 1116 | end; |
| 1117 | |
| 1118 | (* INC or DEC of the variable whose address is under the step on top; |
| 1119 | its store cuts a narrow one to its width *) |
| 1120 | proc incDec*(size: INT; signed, dec: BOOL); |
| 1121 | var r, y, o: INT; |
| 1122 | begin |
| 1123 | if dec then o := A.opSub; else o := A.opAdd; end; |
| 1124 | r, y := operands(); |
| 1125 | drop(); |
| 1126 | A.load(T, r, 0, size, signed); |
| 1127 | A.alu(o, T, T, y); |
| 1128 | A.store(T, r, 0, size); |
| 1129 | end; |
| 1130 | |
| 1131 | (* INC or DEC of the variable in register r, by the value on top *) |
| 1132 | proc incDecReg*(r, size: INT; signed, dec: BOOL); |
| 1133 | var c: BOOL; v: I64; o: INT; |
| 1134 | begin |
| 1135 | if dec then o := A.opSub; else o := A.opAdd; end; |
| 1136 | c, v := topConst(); |
| 1137 | if c then A.aluI(o, r, r, v); else A.alu(o, r, r, loaded(depth - 1)); end; |
| 1138 | drop(); |
| 1139 | if size < 8 then A.extend(r, r, size, signed); end; |
| 1140 | end; |
| 1141 | |
| 1142 | (* NEW's memory, at the address on top, size bytes zeroed unless the |
| 1143 | address is nil (the allocator had no room): cbz x9, L; then |
| 1144 | zeroBytes from x0 *) |
| 1145 | proc zeroNew*(size: I64); |
| 1146 | var L, r: INT; |
| 1147 | begin |
| 1148 | r := top(); |
| 1149 | L := NoLabel; |
| 1150 | A.cbz(false, r, L); |
| 1151 | A.movRR(Dst, r); |
| 1152 | zeroBytes(size); |
| 1153 | A.fixLink(L); |
| 1154 | end; |
| 1155 | |
| 1156 | (* CAS(v, old, new), XCHG(v, x) or XADD(v, x), from the stack, v's |
| 1157 | address under the rest: the result where the address was. By the |
| 1158 | LSE instructions every Apple processor has, each with acquire and |
| 1159 | release, as x86's lock does: casal stores new if [v] holds old, |
| 1160 | leaving what [v] held where old was, which says whether it did: |
| 1161 | |
| 1162 | mov x17, x15 ; old |
| 1163 | casal x17, x10, [x9] ; x10, new |
| 1164 | cmp x17, x15 |
| 1165 | cset x9, eq |
| 1166 | |
| 1167 | swpal and ldaddal leave what [v] held in x17, the result. *) |
| 1168 | proc atomic*(which: INT); |
| 1169 | var r, n: INT; |
| 1170 | begin |
| 1171 | if which = Sym.bCas then n := top(); drop(); end; |
| 1172 | popTo(T); |
| 1173 | r := top(); |
| 1174 | match which of |
| 1175 | | Sym.bCas: A.movRR(U, T); A.casal(U, n, r, 8); A.cmp(U, T); A.cset(A.ccEQ, r); |
| 1176 | | Sym.bXchg: A.swpal(T, U, r, 8); A.movRR(r, U); |
| 1177 | | else: A.ldaddal(T, U, r, 8); A.movRR(r, U); |
| 1178 | end; |
| 1179 | stack[depth - 1].k := kWord; |
| 1180 | end; |
| 1181 | |
| 1182 | (* SYSTEM.SYSCALL is Linux's only (report 6.8): macOS has no stable |
| 1183 | system calls, and a program calls the C library there *) |
| 1184 | proc sysCall*(n: INT); |
| 1185 | var i: INT; |
| 1186 | begin |
| 1187 | missing("SYSTEM.SYSCALL is Linux's only: call the C library"); |
| 1188 | for i := 1 to n do drop(); end; |
| 1189 | _ := target(); |
| 1190 | computed(kWord); |
| 1191 | end; |
| 1192 | |
| 1193 | (* A barrier for every processor, dmb ish *) |
| 1194 | proc fence*; |
| 1195 | begin |
| 1196 | A.dmb(); |
| 1197 | end; |
| 1198 | |
| 1199 | (* Jumps. A label is the chain of the jumps to it, until it is placed |
| 1200 | (A64). *) |
| 1201 | |
| 1202 | proc jump*(var L: INT); |
| 1203 | begin |
| 1204 | A.jmp(L); |
| 1205 | end; |
| 1206 | |
| 1207 | (* Pops a BOOL, and jumps if it is when *) |
| 1208 | proc jumpIf*(var L: INT; when: BOOL); |
| 1209 | var r: INT; |
| 1210 | begin |
| 1211 | r := loaded(depth - 1); |
| 1212 | drop(); |
| 1213 | A.cbz(when, r, L); |
| 1214 | end; |
| 1215 | |
| 1216 | (* Pops two integers, and jumps if the left op the right *) |
| 1217 | proc jumpCmp*(sy: Scan.Sym; signed: BOOL; var L: INT); |
| 1218 | var r, y, i: INT; |
| 1219 | begin |
| 1220 | i := INT(sy) - INT(Scan.sEql); |
| 1221 | r, y := operands(); |
| 1222 | drop(); |
| 1223 | A.cmp(r, y); |
| 1224 | if signed then A.jcc(CcSigned[i], L); else A.jcc(CcUnsigned[i], L); end; |
| 1225 | end; |
| 1226 | |
| 1227 | (* jumpCmp out of an expression: the values under the two compared are |
| 1228 | spilled first, so that they are where the code after the landing |
| 1229 | place expects them, whichever way it came (Gen.x86_64.raz says more) *) |
| 1230 | proc jumpOut*(sy: Scan.Sym; signed: BOOL; var L: INT); |
| 1231 | begin |
| 1232 | spillBelow(depth - 2); |
| 1233 | jumpCmp(sy, signed, L); |
| 1234 | end; |
| 1235 | |
| 1236 | (* The two values on top, compared, as a BOOL: true unless notOp holds; |
| 1237 | fj and tj, the chains of and, or and not, land on the 0 and the 1 *) |
| 1238 | proc condValue*(notOp: Scan.Sym; signed: BOOL; fj, tj: INT); |
| 1239 | var r, e: INT; |
| 1240 | begin |
| 1241 | jumpOut(notOp, signed, fj); |
| 1242 | A.fixLink(tj); |
| 1243 | r := target(); |
| 1244 | A.movRI(r, 1); |
| 1245 | e := NoLabel; |
| 1246 | A.jmp(e); |
| 1247 | A.fixLink(fj); |
| 1248 | A.movRI(r, 0); |
| 1249 | A.fixLink(e); |
| 1250 | computed(kWord); |
| 1251 | end; |
| 1252 | |
| 1253 | proc jumpBack*(top: INT); |
| 1254 | begin |
| 1255 | A.jmpBack(top); |
| 1256 | end; |
| 1257 | |
| 1258 | proc fix*(L: INT); |
| 1259 | begin |
| 1260 | A.fixLink(L); |
| 1261 | end; |
| 1262 | |
| 1263 | (* A jump table, for a match on the integer in frame slot slot, whose |
| 1264 | labels run from lo up for n values. The selector less lo, compared |
| 1265 | unsigned (so below lo is above n too), is out of the table if it |
| 1266 | isn't below n, and goes to the else arm at elseAt, or, if there is |
| 1267 | none, to endL, the end of the match. Else it picks a word of the |
| 1268 | table that follows the jump, an offset from the table's start, which |
| 1269 | adr finds four instructions on: |
| 1270 | |
| 1271 | ldr x15, [x29, #slot] |
| 1272 | sub x15, x15, #lo |
| 1273 | cmp x15, #n - 1 |
| 1274 | b.hi else |
| 1275 | adr x17, table |
| 1276 | ldrsw x15, [x17, x15, lsl #2] |
| 1277 | add x15, x17, x15 |
| 1278 | br x15 |
| 1279 | table: .word arm0 - table, arm1 - table, ... |
| 1280 | |
| 1281 | It returns the table's start; the caller then makes the n entries |
| 1282 | with tableEntry. *) |
| 1283 | proc jumpTable*(slot, lo: I64; n, elseAt: INT; var endL: INT): INT; |
| 1284 | const TableAt = 16; |
| 1285 | var start: INT; |
| 1286 | begin |
| 1287 | A.load(T, FP, INT(slot), 8, false); |
| 1288 | if lo # 0 then A.aluI(A.opSub, T, T, lo); end; |
| 1289 | A.cmpI(T, n - 1); |
| 1290 | if elseAt # NoLabel then A.jccBack(A.ccHI, elseAt); else A.jcc(A.ccHI, endL); end; |
| 1291 | start := Chunk.pc + TableAt; |
| 1292 | A.adr(U, TableAt); |
| 1293 | A.loadR(T, U, T, 4, true, true); |
| 1294 | A.alu(A.opAdd, T, U, T); |
| 1295 | A.jmpR(T); |
| 1296 | if Chunk.pc # start then Diag.fatal("internal error: the jump table is not where it was said"); end; |
| 1297 | return start; |
| 1298 | end; |
| 1299 | |
| 1300 | (* The next entry of the table that jumpTable started at table: the |
| 1301 | code at target *) |
| 1302 | proc tableEntry*(table, target: INT); |
| 1303 | begin |
| 1304 | Chunk.put4(target - table); |
| 1305 | end; |
| 1306 | |
| 1307 | (* A loop's start is aligned on x86-64, for its fetches; here it isn't *) |
| 1308 | proc alignLoop*; |
| 1309 | begin |
| 1310 | end; |
| 1311 | |
| 1312 | proc joinJumps*(L1, L2: INT): INT; |
| 1313 | begin |
| 1314 | return A.joinLinks(L1, L2); |
| 1315 | end; |
| 1316 | |
| 1317 | proc fixTo*(L, top: INT); |
| 1318 | begin |
| 1319 | A.fixLinkTo(L, top); |
| 1320 | end; |
| 1321 | |
| 1322 | (* Checks, and what checks as it goes, as on x86-64, whose comments |
| 1323 | say more; a comparison is unsigned, so that a negative index is a |
| 1324 | huge one *) |
| 1325 | |
| 1326 | (* The index on top is below n *) |
| 1327 | proc checkIndex*(n: I64; p: Diag.Pos); |
| 1328 | var r: INT; |
| 1329 | begin |
| 1330 | r := top(); |
| 1331 | A.cmpI(r, n); |
| 1332 | check(A.ccLO, 0, p, r, -1, n, -1); |
| 1333 | end; |
| 1334 | |
| 1335 | (* The index on top is where a view starts in an array of n elements, |
| 1336 | below below *) |
| 1337 | proc checkView*(n, below: I64; p: Diag.Pos); |
| 1338 | var r: INT; |
| 1339 | begin |
| 1340 | r := top(); |
| 1341 | A.cmpI(r, below); |
| 1342 | check(A.ccLO, 7, p, r, -1, n, -1); |
| 1343 | end; |
| 1344 | |
| 1345 | (* The integer on top is a position of an enumeration of n names *) |
| 1346 | proc checkEnum*(n: I64; name: INT; p: Diag.Pos); |
| 1347 | var r: INT; |
| 1348 | begin |
| 1349 | r := top(); |
| 1350 | A.cmpI(r, n); |
| 1351 | check(A.ccLO, 2, p, r, -1, n, name); |
| 1352 | end; |
| 1353 | |
| 1354 | (* The address on top, to be used reach bytes past, isn't nil, unless |
| 1355 | the machine would stop it, within page zero *) |
| 1356 | proc checkNil*(reach: I64; p: Diag.Pos); |
| 1357 | var r: INT; |
| 1358 | begin |
| 1359 | if reach <= NilPage then return; end; |
| 1360 | r := top(); |
| 1361 | A.cmpI(r, 0); |
| 1362 | check(A.ccNE, 4, p, r, -1, 0, -1); |
| 1363 | end; |
| 1364 | |
| 1365 | (* What a failed index check is: an index past the length (0), or a |
| 1366 | view that runs past it (7) when it needs room elements more *) |
| 1367 | proc kindOfIndex(room: I64): INT; |
| 1368 | begin |
| 1369 | if room > 0 then return 7; end; |
| 1370 | return 0; |
| 1371 | end; |
| 1372 | |
| 1373 | (* r times v, an element's size: by a shift where v is a power of two, |
| 1374 | else through x3, free between calls as x0 to x2 are *) |
| 1375 | proc mulI(r: INT; v: I64); |
| 1376 | var n: INT; |
| 1377 | begin |
| 1378 | n := 0; |
| 1379 | while (I64(1) shl n) < v do INC(n); end; |
| 1380 | if (I64(1) shl n) = v then |
| 1381 | if n > 0 then A.shiftI(A.shLsl, r, r, n, 8); end; |
| 1382 | else |
| 1383 | A.movRI(3, v); |
| 1384 | A.mul(r, r, 3); |
| 1385 | end; |
| 1386 | end; |
| 1387 | |
| 1388 | (* Pops an index, and the address of the slice at a if it is on the |
| 1389 | stack, and makes the address of the element it picks: the slice's |
| 1390 | pointer plus the index times size. The index is checked against the |
| 1391 | length, in its register or loaded into x3; a view, which needs room |
| 1392 | elements after the one indexed, against the length less the index, |
| 1393 | which can't wrap once the index is below it: |
| 1394 | |
| 1395 | ldr x3, [x29, #-24] ; the length |
| 1396 | cmp x15, x3 |
| 1397 | b.hs fail |
| 1398 | sub x2, x3, x15 |
| 1399 | cmp x2, #room |
| 1400 | b.ls fail *) |
| 1401 | proc indexSlice*(a: Adr; size, room: I64; checked: BOOL; p: Diag.Pos); |
| 1402 | var m, ml: Mem; r, len: INT; |
| 1403 | begin |
| 1404 | popTo(T); |
| 1405 | if a.base = bReg then |
| 1406 | len := a.hi; |
| 1407 | else |
| 1408 | m := place(a, U); |
| 1409 | ml := m; ml.off := ml.off + 8; |
| 1410 | len := 3; |
| 1411 | if checked then loadMem(len, ml, 8, false); end; |
| 1412 | end; |
| 1413 | if checked then |
| 1414 | A.cmp(T, len); |
| 1415 | check(A.ccLO, kindOfIndex(room), p, T, len, 0, -1); |
| 1416 | if room > 0 then |
| 1417 | A.alu(A.opSub, Tmp, len, T); |
| 1418 | A.cmpI(Tmp, room); |
| 1419 | check(A.ccHI, 7, p, T, len, 0, -1); |
| 1420 | end; |
| 1421 | end; |
| 1422 | mulI(T, size); |
| 1423 | if a.base = bReg then |
| 1424 | A.alu(A.opAdd, T, T, a.chunk); |
| 1425 | else |
| 1426 | loadMem(U, m, 8, false); |
| 1427 | A.alu(A.opAdd, T, T, U); |
| 1428 | end; |
| 1429 | r := target(); |
| 1430 | A.movRR(r, T); |
| 1431 | computed(kWord); |
| 1432 | end; |
| 1433 | |
| 1434 | (* Pops an index, and a pointer under it, and makes the address of the |
| 1435 | element it picks *) |
| 1436 | proc indexPtr*(size: I64); |
| 1437 | begin |
| 1438 | mulI(top(), size); |
| 1439 | op(Scan.sPlus, kWord, 8, false); |
| 1440 | end; |
| 1441 | |
| 1442 | (* Pops an index, checked before, and makes the address of the element |
| 1443 | it picks of the array at a *) |
| 1444 | proc indexArray*(a: Adr; size: I64); |
| 1445 | begin |
| 1446 | mulI(top(), size); |
| 1447 | pushAdr(a); |
| 1448 | op(Scan.sPlus, kWord, 8, false); |
| 1449 | end; |
| 1450 | |
| 1451 | (* Pops hi and lo, and the address of the array or slice at a if it is |
| 1452 | on the stack, and makes the slice lo..hi of it, of elements of size |
| 1453 | bytes, in the frame at tmp: its pointer, the start plus lo times size, |
| 1454 | and its length, hi - lo. The array has n elements. Two checks, both |
| 1455 | unsigned, so that a negative bound fails too: hi against the length, |
| 1456 | then lo against hi; lo is in x1 and hi in x2, as Rt.fail takes a and |
| 1457 | b. *) |
| 1458 | proc slice*(a: Adr; isArray: BOOL; n, size, tmp: I64; checked: BOOL; p: Diag.Pos); |
| 1459 | var m, ml: Mem; len: INT; |
| 1460 | begin |
| 1461 | popTo(Tmp); (* hi *) |
| 1462 | popTo(Src); (* lo *) |
| 1463 | if a.base # bReg then m := place(a, U); end; |
| 1464 | if checked then |
| 1465 | if isArray then |
| 1466 | len := -1; |
| 1467 | A.cmpI(Tmp, n); |
| 1468 | else |
| 1469 | len := a.hi; |
| 1470 | if a.base # bReg then len := 3; ml := m; ml.off := ml.off + 8; loadMem(len, ml, 8, false); end; |
| 1471 | A.cmp(Tmp, len); |
| 1472 | end; |
| 1473 | check(A.ccLS, 1, p, Src, len, n, -1); |
| 1474 | A.cmp(Src, Tmp); |
| 1475 | check(A.ccLS, 1, p, Src, len, n, -1); |
| 1476 | end; |
| 1477 | if a.base = bReg then A.movRR(U, a.chunk); |
| 1478 | elsif isArray then adrTo(U, m); |
| 1479 | else loadMem(U, m, 8, false); end; |
| 1480 | A.alu(A.opSub, Tmp, Tmp, Src); |
| 1481 | A.store(Tmp, FP, INT(tmp) + 8, 8); |
| 1482 | mulI(Src, size); |
| 1483 | A.alu(A.opAdd, U, U, Src); |
| 1484 | A.store(U, FP, INT(tmp), 8); |
| 1485 | end; |
| 1486 | |
| 1487 | (* The x2 bytes from [x1] to [x0], n at a time while x2 holds n or more, |
| 1488 | through x15: forward, moving x1 and x0 on after each, or back, from |
| 1489 | the ends, moving them down first *) |
| 1490 | proc copyRun(n: INT; back: BOOL); |
| 1491 | var top, out: INT; |
| 1492 | begin |
| 1493 | top := Chunk.pc; |
| 1494 | out := NoLabel; |
| 1495 | A.cmpI(Tmp, n); |
| 1496 | A.jcc(A.ccLO, out); |
| 1497 | if back then |
| 1498 | A.loadIdx(T, Src, -n, n, A.mPre); A.storeIdx(T, Dst, -n, n, A.mPre); |
| 1499 | else |
| 1500 | A.loadIdx(T, Src, n, n, A.mPost); A.storeIdx(T, Dst, n, n, A.mPost); |
| 1501 | end; |
| 1502 | A.aluI(A.opSub, Tmp, Tmp, n); |
| 1503 | A.jmpBack(top); |
| 1504 | A.fixLink(out); |
| 1505 | end; |
| 1506 | |
| 1507 | (* COPY: the elements of the slice in the frame at sOff, of size bytes |
| 1508 | each, to the start of the one at dOff, which must be at least as long |
| 1509 | (the source may be shorter): slice 0..LEN(s) of d, checked. They go |
| 1510 | by words, then the last few bytes one by one, forward; but back, from |
| 1511 | the ends, when the destination starts inside the source, so that an |
| 1512 | overlap comes out right (report 6.8): |
| 1513 | |
| 1514 | ldr x2, [x29, #sOff + 8] |
| 1515 | ldr x3, [x29, #dOff + 8] |
| 1516 | cmp x2, x3 |
| 1517 | b.hi fail ; the source longer than the destination |
| 1518 | ldr x1, [x29, #sOff] |
| 1519 | ldr x0, [x29, #dOff] |
| 1520 | cmp x0, x1 |
| 1521 | b.ls fwd ; the destination not above: forward |
| 1522 | add x15, x1, x2 |
| 1523 | cmp x0, x15 |
| 1524 | b.hs fwd ; nor inside the source |
| 1525 | add x1, x1, x2 ; back, from the ends |
| 1526 | add x0, x0, x2 |
| 1527 | ... ; copyRun(8, back), copyRun(1, back) |
| 1528 | b out |
| 1529 | fwd: ... ; copyRun(8), copyRun(1) |
| 1530 | out: *) |
| 1531 | proc copySlice*(dOff, sOff, size: I64; checked: BOOL; p: Diag.Pos); |
| 1532 | var fwd, out: INT; |
| 1533 | begin |
| 1534 | A.load(Tmp, FP, INT(sOff) + 8, 8, false); |
| 1535 | if checked then |
| 1536 | A.load(3, FP, INT(dOff) + 8, 8, false); |
| 1537 | A.movRI(Src, 0); (* a, the slice's start *) |
| 1538 | A.cmp(Tmp, 3); |
| 1539 | check(A.ccLS, 1, p, Src, 3, 0, -1); |
| 1540 | end; |
| 1541 | mulI(Tmp, size); |
| 1542 | A.load(Src, FP, INT(sOff), 8, false); |
| 1543 | A.load(Dst, FP, INT(dOff), 8, false); |
| 1544 | fwd := NoLabel; |
| 1545 | A.cmp(Dst, Src); |
| 1546 | A.jcc(A.ccLS, fwd); |
| 1547 | A.alu(A.opAdd, T, Src, Tmp); |
| 1548 | A.cmp(Dst, T); |
| 1549 | A.jcc(A.ccHS, fwd); |
| 1550 | A.alu(A.opAdd, Src, Src, Tmp); |
| 1551 | A.alu(A.opAdd, Dst, Dst, Tmp); |
| 1552 | copyRun(8, true); |
| 1553 | copyRun(1, true); |
| 1554 | out := NoLabel; |
| 1555 | A.jmp(out); |
| 1556 | A.fixLink(fwd); |
| 1557 | copyRun(8, false); |
| 1558 | copyRun(1, false); |
| 1559 | A.fixLink(out); |
| 1560 | end; |
| 1561 | |
| 1562 | (* FILL: every element of the slice in the frame at dOff, of size bytes |
| 1563 | each, set to the value on top if the element is a scalar, or else to |
| 1564 | the size bytes at the address on top; n is how many elements there |
| 1565 | are if that is known, as of an array, else -1 (Gen.x86_64.raz says |
| 1566 | more). A constant zero into an array is its bytes zeroed |
| 1567 | (zeroBytes). A scalar, a real's bits as an integer's, goes into x15, |
| 1568 | and is stored into each element, by a store each into an array of |
| 1569 | FewFills elements or fewer, else by a loop: |
| 1570 | |
| 1571 | ldr x0, [x29, #dOff] |
| 1572 | ldr x2, [x29, #dOff + 8] |
| 1573 | cbz x2, out |
| 1574 | top: str w15, [x0], #4 ; a U32 |
| 1575 | subs x2, x2, #1 |
| 1576 | b.ne top |
| 1577 | out: |
| 1578 | |
| 1579 | Any other element is copied into d[0], and from there forward, from |
| 1580 | d[0] on to d[1] on, as copySlice copies, by words if it is 8 bytes |
| 1581 | or more, else by bytes: each is read after it is written, size |
| 1582 | bytes before, so d[0] repeats through the rest. |
| 1583 | |
| 1584 | ... ; d[0] := x, as for a copy, from x1 |
| 1585 | ldr x1, [x29, #dOff] |
| 1586 | add x0, x1, #size |
| 1587 | ldr x2, [x29, #dOff + 8] |
| 1588 | sub x2, x2, #1 |
| 1589 | ... ; times size: the bytes of d[1..n] |
| 1590 | ... ; copyRun(8), copyRun(1) *) |
| 1591 | const FewFills = 16; |
| 1592 | |
| 1593 | proc fillSlice*(dOff, size, n: I64; scalar: BOOL); |
| 1594 | var c: BOOL; v, i: I64; out, top: INT; |
| 1595 | begin |
| 1596 | c, v := topConst(); |
| 1597 | out := NoLabel; |
| 1598 | if scalar and c and (v = 0) and (n >= 0) then |
| 1599 | drop(); |
| 1600 | A.load(Dst, FP, INT(dOff), 8, false); |
| 1601 | zeroBytes(n * size); |
| 1602 | elsif scalar and (n >= 0) and (n <= FewFills) then |
| 1603 | popTo(T); |
| 1604 | A.load(Dst, FP, INT(dOff), 8, false); |
| 1605 | for i := 0 to n - 1 do A.store(T, Dst, INT(i * size), INT(size)); end; |
| 1606 | elsif scalar then |
| 1607 | popTo(T); |
| 1608 | A.load(Dst, FP, INT(dOff), 8, false); |
| 1609 | A.load(Tmp, FP, INT(dOff) + 8, 8, false); |
| 1610 | A.cbz(false, Tmp, out); |
| 1611 | top := Chunk.pc; |
| 1612 | A.storeIdx(T, Dst, INT(size), INT(size), A.mPost); |
| 1613 | A.aluI(A.opSubs, Tmp, Tmp, 1); |
| 1614 | A.jccBack(A.ccNE, top); |
| 1615 | else |
| 1616 | popTo(Src); |
| 1617 | A.load(Dst, FP, INT(dOff), 8, false); |
| 1618 | A.load(Tmp, FP, INT(dOff) + 8, 8, false); |
| 1619 | A.cbz(false, Tmp, out); |
| 1620 | copyBytes(size); |
| 1621 | A.load(Src, FP, INT(dOff), 8, false); |
| 1622 | A.aluI(A.opAdd, Dst, Src, size); |
| 1623 | A.load(Tmp, FP, INT(dOff) + 8, 8, false); |
| 1624 | A.aluI(A.opSub, Tmp, Tmp, 1); |
| 1625 | mulI(Tmp, size); |
| 1626 | if size >= 8 then copyRun(8, false); end; |
| 1627 | copyRun(1, false); |
| 1628 | end; |
| 1629 | A.fixLink(out); |
| 1630 | end; |
| 1631 | |
| 1632 | (* Pops hi, lo and a pointer, and makes the slice p[lo..hi] at tmp. Its |
| 1633 | bounds can't be known, but it can't run backwards. *) |
| 1634 | proc slicePtr*(size, tmp: I64; checked: BOOL; p: Diag.Pos); |
| 1635 | begin |
| 1636 | popTo(Tmp); (* hi *) |
| 1637 | popTo(Src); (* lo *) |
| 1638 | popTo(U); (* the pointer *) |
| 1639 | if checked then |
| 1640 | A.cmp(Src, Tmp); |
| 1641 | check(A.ccLE, 3, p, Src, -1, 0, -1); |
| 1642 | end; |
| 1643 | A.alu(A.opSub, Tmp, Tmp, Src); |
| 1644 | A.store(Tmp, FP, INT(tmp) + 8, 8); |
| 1645 | mulI(Src, size); |
| 1646 | A.alu(A.opAdd, U, U, Src); |
| 1647 | A.store(U, FP, INT(tmp), 8); |
| 1648 | end; |
| 1649 | |
| 1650 | (* The words the parameters of procedure type sig take: two for a |
| 1651 | slice passed by value, one for anything else *) |
| 1652 | proc paramWords(sig: ^Sym.Type): INT; |
| 1653 | var q: ^Sym.Obj; n: INT; |
| 1654 | begin |
| 1655 | n := 0; |
| 1656 | q := sig^.fields; |
| 1657 | while q # nil do |
| 1658 | if (q^.typ^.form = Sym.fSlice) and not q^.isVar then INC(n, 2); else INC(n); end; |
| 1659 | q := q^.next; |
| 1660 | end; |
| 1661 | return n; |
| 1662 | end; |
| 1663 | |
| 1664 | (* Calls. The arguments are the c.n values on top, each moved into the |
| 1665 | next of x0 to x7, or a real into the next of v0 to v7, just before |
| 1666 | the call; the values under them are spilled first, as the call may |
| 1667 | change their registers. As the stack's registers carry no argument, |
| 1668 | the moves may go in any order. A slice goes as its two words, both |
| 1669 | in registers or both on the stack, a record by value as the address |
| 1670 | of a copy (Item makes it). The arguments past the registers go on |
| 1671 | the machine's stack, the first at sp, in an area sp is lowered by |
| 1672 | for the call, a multiple of 16; and so do those past a C procedure's |
| 1673 | own parameters where it ends in ..., in registers or not, as Apple's |
| 1674 | convention has it. Each takes 8 bytes, a real as its bits, but a C |
| 1675 | procedure's own parameter of an integer, BOOL, enumeration, set or |
| 1676 | real, which takes its size, at a multiple of it, as Apple packs |
| 1677 | them: an I32 then an I64 go at sp and sp + 8, an I32 then an I16 at |
| 1678 | sp and sp + 4. A result in memory, a record, an array or several |
| 1679 | results, goes to the caller's temporary c.ret, its address in x8. A |
| 1680 | single result comes back in x0, or d0, two words in x0 and x1: a |
| 1681 | slice is stored in c.ret, two results pushed as two values. *) |
| 1682 | proc call*(var c: CallInfo); |
| 1683 | var |
| 1684 | res: ^Sym.Type; q: ^Sym.Obj; i, ni, nf, first, own, r: INT; area: I64; |
| 1685 | onStack: [64]BOOL; at, size: [64]INT; (* where on the stack, and how wide *) |
| 1686 | begin |
| 1687 | first := depth - c.n; |
| 1688 | spillBelow(first); |
| 1689 | res := c.sig^.base; |
| 1690 | c.ret := 0; |
| 1691 | if (res # nil) and Sym.holdsArray(res) then |
| 1692 | c.ret := allocPerm(res^.size, res^.align); (* kept, as on x86-64 (report 8) *) |
| 1693 | elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then |
| 1694 | c.ret := allocTemp(res^.size, res^.align); |
| 1695 | end; |
| 1696 | own := c.n; |
| 1697 | if c.sig^.variadic then own := paramWords(c.sig); end; |
| 1698 | ni := 0; nf := 0; area := 0; |
| 1699 | q := c.sig^.fields; (* the parameter of argument i, while there is one *) |
| 1700 | for i := 0 to c.n - 1 do |
| 1701 | size[i] := 8; |
| 1702 | if c.ext and (q # nil) and not q^.isVar |
| 1703 | and (q^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet, Sym.fReal}) then |
| 1704 | size[i] := INT(q^.typ^.size); |
| 1705 | end; |
| 1706 | if (q # nil) and not c.pair[i] then q := q^.next; end; |
| 1707 | if i >= own then onStack[i] := true; |
| 1708 | elsif c.isF[i] then onStack[i] := nf = 8; |
| 1709 | elsif (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1]; |
| 1710 | else onStack[i] := ni + 1 + INT(c.pair[i]) > 8; end; |
| 1711 | if onStack[i] then |
| 1712 | area := Sym.alignUp(area, size[i]); |
| 1713 | at[i] := INT(area); |
| 1714 | INC(area, size[i]); |
| 1715 | elsif c.isF[i] then INC(nf); |
| 1716 | else INC(ni); end; |
| 1717 | end; |
| 1718 | area := Sym.alignUp(area, 16); |
| 1719 | if area > 0 then A.aluI(A.opSub, SP, SP, area); end; |
| 1720 | ni := 0; nf := 0; |
| 1721 | for i := 0 to c.n - 1 do |
| 1722 | if onStack[i] then |
| 1723 | valueTo(first + i, T); |
| 1724 | A.store(T, SP, at[i], size[i]); |
| 1725 | elsif c.isF[i] then |
| 1726 | xValueTo(first + i, nf); |
| 1727 | INC(nf); |
| 1728 | else |
| 1729 | valueTo(first + i, ni); |
| 1730 | INC(ni); |
| 1731 | end; |
| 1732 | end; |
| 1733 | if (c.ret # 0) and Sym.behindPointer(res) then A.aluI(A.opAdd, 8, FP, c.ret); end; |
| 1734 | for i := 1 to c.n do drop(); end; |
| 1735 | if c.ext then |
| 1736 | A.loadChunk(U, c.chunk, 0, 8, false); |
| 1737 | A.callR(U); |
| 1738 | elsif c.chunk >= 0 then |
| 1739 | A.callChunk(c.chunk); |
| 1740 | else |
| 1741 | A.load(U, FP, INT(c.target), 8, false); |
| 1742 | A.callR(U); |
| 1743 | end; |
| 1744 | if area > 0 then A.aluI(A.opAdd, SP, SP, area); end; |
| 1745 | if res # nil then |
| 1746 | if res^.form = Sym.fSlice then |
| 1747 | A.store(0, FP, INT(c.ret), 8); |
| 1748 | A.store(1, FP, INT(c.ret) + 8, 8); |
| 1749 | elsif Sym.inTwoRegs(res) then |
| 1750 | A.movRR(target(), 0); computed(kWord); |
| 1751 | A.movRR(target(), 1); computed(kWord); |
| 1752 | elsif (c.ret = 0) and (res^.form = Sym.fReal) then |
| 1753 | A.fop1(A.fMov, xTarget(), 0, false); |
| 1754 | computed(kindOf(res)); |
| 1755 | elsif c.ret = 0 then |
| 1756 | (* C leaves the bits above a narrow result as they fall *) |
| 1757 | r := target(); |
| 1758 | A.movRR(r, 0); |
| 1759 | if res^.form = Sym.fBool then A.extend(r, r, 1, false); |
| 1760 | elsif (res^.form = Sym.fInt) and (res^.size < 8) then A.extend(r, r, INT(res^.size), res^.signed); end; |
| 1761 | computed(kindOf(res)); |
| 1762 | end; |
| 1763 | end; |
| 1764 | end; |
| 1765 | |
| 1766 | (* Frames: a slot of size bytes, its offset from x29, negative. A |
| 1767 | temporary is released at the end of its statement; allocPerm's lasts |
| 1768 | the procedure. *) |
| 1769 | proc allocTemp*(size, align: I64): I64; |
| 1770 | begin |
| 1771 | if align < 1 then align := 1; end; |
| 1772 | frameTop := Sym.alignUp(frameTop + size, align); |
| 1773 | if frameTop > frameMax then frameMax := frameTop; end; |
| 1774 | return -frameTop; |
| 1775 | end; |
| 1776 | |
| 1777 | proc allocPerm*(size, align: I64): I64; |
| 1778 | var s: I64; |
| 1779 | begin |
| 1780 | s := allocTemp(size, align); |
| 1781 | permTop := frameTop; |
| 1782 | return s; |
| 1783 | end; |
| 1784 | |
| 1785 | proc tempMark*: I64; |
| 1786 | begin |
| 1787 | return frameTop; |
| 1788 | end; |
| 1789 | |
| 1790 | proc releaseTemps*(m: I64); |
| 1791 | begin |
| 1792 | if m < permTop then m := permTop; end; |
| 1793 | frameTop := m; |
| 1794 | end; |
| 1795 | |
| 1796 | proc keepTemps*; |
| 1797 | begin |
| 1798 | frameTop := frameMax; |
| 1799 | permTop := frameMax; |
| 1800 | end; |
| 1801 | |
| 1802 | (* The parameters' places: each in a frame slot of its own, stored there |
| 1803 | from its register on entry, or above the frame where the caller put |
| 1804 | it, past the frame record (call). A var parameter, and a record or |
| 1805 | array passed by value, holds an address (ind). A real comes in the |
| 1806 | next of v0 to v7, and the ninth on, as call puts them, on the stack. |
| 1807 | The address of a result in memory arrives in x8, and is kept in |
| 1808 | retSlot. *) |
| 1809 | proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj); |
| 1810 | var p: ^Sym.Obj; ni, nf, k, w: INT; stackOff: I64; f: BOOL; |
| 1811 | begin |
| 1812 | frameTop := 0; frameMax := 0; permTop := 0; |
| 1813 | resetStack(0, 0); |
| 1814 | exitL := NoLabel; |
| 1815 | numIn := 0; |
| 1816 | lost := false; |
| 1817 | resType := sig^.base; |
| 1818 | procParams := params; |
| 1819 | regMask := 0; |
| 1820 | if (resType # nil) and Sym.behindPointer(resType) then |
| 1821 | retSlot := allocPerm(8, 8); |
| 1822 | inReg[0] := 8; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; |
| 1823 | numIn := 1; |
| 1824 | end; |
| 1825 | ni := 0; nf := 0; |
| 1826 | stackOff := 16; |
| 1827 | p := params; |
| 1828 | while p # nil do |
| 1829 | p^.ind := p^.isVar or (Sym.isAggregate(p^.typ) and (p^.typ^.form # Sym.fSlice)); |
| 1830 | f := (p^.typ^.form = Sym.fReal) and not p^.isVar; |
| 1831 | k := 1; |
| 1832 | if (p^.typ^.form = Sym.fSlice) and not p^.isVar then k := 2; end; |
| 1833 | if f and (nf < 8) then |
| 1834 | p^.val := allocPerm(8, 8); |
| 1835 | inReg[numIn] := -1 - nf; inSlot[numIn] := p^.val; inSize[numIn] := 8; inObj[numIn] := p; |
| 1836 | INC(numIn); INC(nf); |
| 1837 | k := 0; |
| 1838 | elsif f or (ni + k > 8) then (* on the stack, as call puts it *) |
| 1839 | p^.val := stackOff; |
| 1840 | INC(stackOff, 8 * k); |
| 1841 | k := 0; |
| 1842 | else |
| 1843 | p^.val := allocPerm(8 * k, 8); |
| 1844 | end; |
| 1845 | for w := 0 to k - 1 do (* a slice's two words, in two slots *) |
| 1846 | inReg[numIn] := ni; inSlot[numIn] := p^.val + I64(8 * w); inObj[numIn] := p; |
| 1847 | inSize[numIn] := 8; |
| 1848 | if not p^.ind and (p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet}) then inSize[numIn] := INT(p^.typ^.size); end; |
| 1849 | INC(numIn); INC(ni); |
| 1850 | end; |
| 1851 | p := p^.next; |
| 1852 | end; |
| 1853 | paramTop := frameTop; |
| 1854 | end; |
| 1855 | |
| 1856 | (* The frame as beginProc left it, with the parameters' slots only *) |
| 1857 | proc resetLocals*; |
| 1858 | begin |
| 1859 | frameTop := paramTop; frameMax := paramTop; permTop := paramTop; |
| 1860 | end; |
| 1861 | |
| 1862 | (* No parameter is kept where it arrived, as x86-64 keeps one in rdi or |
| 1863 | rsi: each is stored on entry, or moved to its register variable *) |
| 1864 | proc keepArrived*(o: ^Sym.Obj); |
| 1865 | begin |
| 1866 | _ := o; |
| 1867 | end; |
| 1868 | |
| 1869 | proc keptIn*(o: ^Sym.Obj): BOOL; |
| 1870 | begin |
| 1871 | _ := o; |
| 1872 | return false; |
| 1873 | end; |
| 1874 | |
| 1875 | (* The locals in memory have their slots: the prologue zeroes those. The |
| 1876 | registers of variables in mask are saved next, a quadword each, in |
| 1877 | slots that need no zeroing; the frame as it then stands is where a |
| 1878 | second try at the body starts. *) |
| 1879 | proc useRegs*(mask: INT); |
| 1880 | var i, k: INT; |
| 1881 | begin |
| 1882 | zeroTop := permTop; |
| 1883 | regMask := mask; |
| 1884 | k := 0; |
| 1885 | for i := 0 to XBit + 7 do |
| 1886 | if (mask & (1 shl i)) # 0 then INC(k); end; |
| 1887 | end; |
| 1888 | if k > 0 then saveAt := allocPerm(8 * k, 8); end; |
| 1889 | frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop; |
| 1890 | end; |
| 1891 | |
| 1892 | (* The registers of variables in regMask, saved from saveAt on, a |
| 1893 | quadword each, x19 to x23 first, then d8 to d15, of which a called |
| 1894 | procedure keeps the low 64 bits, and so must give them back; and |
| 1895 | each zeroed, as a variable starts at zero. Or, if back, restored. *) |
| 1896 | proc saveRegs(back: BOOL); |
| 1897 | var i, n, r: INT; |
| 1898 | begin |
| 1899 | n := 0; |
| 1900 | for i := 0 to XBit + 7 do |
| 1901 | if (regMask & (1 shl i)) # 0 then |
| 1902 | if i < XBit then |
| 1903 | r := VarRegs[i]; |
| 1904 | if back then A.load(r, FP, INT(saveAt) + 8 * n, 8, false); |
| 1905 | else A.store(r, FP, INT(saveAt) + 8 * n, 8); A.movRI(r, 0); end; |
| 1906 | else |
| 1907 | r := XVarRegs[i - XBit] - XmmVar; |
| 1908 | if back then A.loadF(r, FP, INT(saveAt) + 8 * n, 8); |
| 1909 | else A.storeF(r, FP, INT(saveAt) + 8 * n, 8); A.fmovFR(r, ZR, false); end; |
| 1910 | end; |
| 1911 | INC(n); |
| 1912 | end; |
| 1913 | end; |
| 1914 | end; |
| 1915 | |
| 1916 | (* The variable in register r of XVarRegs has a slot of 8 bytes at |
| 1917 | slot, which popVar writes as it writes r *) |
| 1918 | proc xHome*(r: INT; slot: I64); |
| 1919 | begin |
| 1920 | homes[r - XVarRegs[0]] := slot; |
| 1921 | end; |
| 1922 | |
| 1923 | proc markCode*(var m: CodeMark); |
| 1924 | begin |
| 1925 | m.pc := Chunk.pc; m.fixups := Chunk.numFixups; m.lines := Chunk.numLines; m.said := A.saidCount(); |
| 1926 | m.fails := numFails; m.exit := exitL; |
| 1927 | end; |
| 1928 | |
| 1929 | (* The code made since m taken back; the frame is left as it is *) |
| 1930 | proc rewind*(m: CodeMark); |
| 1931 | begin |
| 1932 | Chunk.rewindTo(m.pc, m.fixups, m.lines); |
| 1933 | A.unsay(m.said); |
| 1934 | numFails := m.fails; |
| 1935 | exitL := m.exit; |
| 1936 | end; |
| 1937 | |
| 1938 | (* For a second try at the body: the code as it was at m, and the frame |
| 1939 | as useRegs found it *) |
| 1940 | proc redo*(m: CodeMark); |
| 1941 | begin |
| 1942 | Chunk.rewindTo(m.pc, m.fixups, m.lines); |
| 1943 | A.unsay(m.said); |
| 1944 | numFails := 0; |
| 1945 | frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2]; |
| 1946 | exitL := NoLabel; |
| 1947 | end; |
| 1948 | |
| 1949 | (* The prologue, for the procedure's first line: the frame record, x29 |
| 1950 | and x30, pushed, and x29 made to point at it; sp lowered by the |
| 1951 | frame's size, known only at endProc, which patches it into the two |
| 1952 | subs, of its bits from 12 up and of its low 12 (so a frame is less |
| 1953 | than 16 MB); the locals in memory zeroed, from the parameters' slots |
| 1954 | down to zeroTop, by a store each of the zero register up to 16, else |
| 1955 | by a loop; the registers of variables saved and zeroed; and the |
| 1956 | parameters stored from their registers to their slots, or moved to |
| 1957 | their registers. *) |
| 1958 | proc enterBody*(c, line: INT); |
| 1959 | var i, loop, r: INT; n: I64; p: ^Sym.Obj; |
| 1960 | begin |
| 1961 | Chunk.beginChunk(c); |
| 1962 | codeChunk := c; |
| 1963 | Chunk.noteLine(line, 1); (* a declaration starts a line *) |
| 1964 | A.stp(FP, LR, SP, -16, A.mPre); |
| 1965 | A.movRR(FP, SP); |
| 1966 | frameFix := Chunk.pc; |
| 1967 | A.say("sub sp, sp, frame, lsl #12"); Chunk.put4(SubSp12); |
| 1968 | A.say("sub sp, sp, frame"); Chunk.put4(SubSp); |
| 1969 | n := (Sym.alignUp(zeroTop, 8) - paramTop) div 8; |
| 1970 | if n <= 16 then |
| 1971 | for i := 1 to INT(n) do A.store(ZR, FP, INT(-paramTop) - 8 * i, 8); end; |
| 1972 | elsif n > 0 then |
| 1973 | A.aluI(A.opSub, T, FP, paramTop + 8 * n); |
| 1974 | A.movRI(U, n); |
| 1975 | loop := Chunk.pc; |
| 1976 | A.storeIdx(ZR, T, 8, 8, A.mPost); |
| 1977 | A.aluI(A.opSubs, U, U, 1); |
| 1978 | A.jccBack(A.ccNE, loop); |
| 1979 | end; |
| 1980 | Chunk.setSaves(c, regMask & ((1 shl XBit) - 1), INT(saveAt)); |
| 1981 | saveRegs(false); |
| 1982 | for i := 0 to numIn - 1 do |
| 1983 | if inReg[i] < 0 then (* a real, from v0 to v7 *) |
| 1984 | A.storeF(-1 - inReg[i], FP, INT(inSlot[i]), 8); |
| 1985 | if inObj[i]^.reg # 0 then A.fop1(A.fMov, inObj[i]^.reg - XmmVar, -1 - inReg[i], false); end; |
| 1986 | elsif (inObj[i] # nil) and (inObj[i]^.reg # 0) then |
| 1987 | r := inObj[i]^.reg; |
| 1988 | if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end; |
| 1989 | A.movRR(r, inReg[i]); |
| 1990 | if inSize[i] < 8 then A.extend(r, r, inSize[i], inObj[i]^.typ^.signed); end; |
| 1991 | else |
| 1992 | A.store(inReg[i], FP, INT(inSlot[i]), inSize[i]); |
| 1993 | end; |
| 1994 | end; |
| 1995 | (* a register variable's parameter that came on the stack, above the |
| 1996 | frame (val > 0), is loaded from there: one passed by its address |
| 1997 | (ind) is the address, 8 bytes, whatever its type *) |
| 1998 | p := procParams; |
| 1999 | while p # nil do |
| 2000 | if (p^.reg # 0) and (p^.val > 0) then |
| 2001 | if not p^.ind and (p^.typ^.form = Sym.fReal) then |
| 2002 | A.loadF(p^.reg - XmmVar, FP, INT(p^.val), INT(p^.typ^.size)); |
| 2003 | elsif not p^.ind and (p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet}) then |
| 2004 | A.load(p^.reg, FP, INT(p^.val), INT(p^.typ^.size), p^.typ^.signed); |
| 2005 | else |
| 2006 | A.load(p^.reg, FP, INT(p^.val), 8, false); |
| 2007 | if not p^.ind and (p^.typ^.form = Sym.fSlice) then A.load(p^.reg2, FP, INT(p^.val) + 8, 8, false); end; |
| 2008 | end; |
| 2009 | end; |
| 2010 | p := p^.next; |
| 2011 | end; |
| 2012 | end; |
| 2013 | |
| 2014 | proc resultSlot*: I64; |
| 2015 | begin |
| 2016 | return retSlot; |
| 2017 | end; |
| 2018 | |
| 2019 | (* Pops one of several results, a value of size bytes, into its place |
| 2020 | off bytes into the caller's, at the address that came in x8 *) |
| 2021 | proc resultTo*(off: I64; size: INT); |
| 2022 | var r: INT; fl: BOOL; |
| 2023 | begin |
| 2024 | r := top(); |
| 2025 | fl := isReal(depth - 1); |
| 2026 | drop(); |
| 2027 | A.load(U, FP, INT(retSlot), 8, false); |
| 2028 | if fl then A.storeF(r, U, INT(off), size); else A.store(r, U, INT(off), size); end; |
| 2029 | end; |
| 2030 | |
| 2031 | (* Pops an address, and copies size bytes from it to the result, off |
| 2032 | bytes into it *) |
| 2033 | proc copyResult*(off, size: I64); |
| 2034 | begin |
| 2035 | popTo(Src); |
| 2036 | A.load(Dst, FP, INT(retSlot), 8, false); |
| 2037 | if off # 0 then A.aluI(A.opAdd, Dst, Dst, off); end; |
| 2038 | copyBytes(size); |
| 2039 | end; |
| 2040 | |
| 2041 | (* return: the result, a value on top, goes in x0, or d0; two words, |
| 2042 | the two values on top, in x0 and x1; one in memory is there already *) |
| 2043 | proc returnJump*; |
| 2044 | begin |
| 2045 | if (resType # nil) and (resType^.form = Sym.fReal) then |
| 2046 | xValueTo(depth - 1, 0); |
| 2047 | drop(); |
| 2048 | elsif (resType # nil) and not Sym.behindPointer(resType) then |
| 2049 | if Sym.inTwoRegs(resType) then popTo(1); end; |
| 2050 | popTo(0); |
| 2051 | end; |
| 2052 | A.jmp(exitL); |
| 2053 | end; |
| 2054 | |
| 2055 | (* try, where the error isn't nil, first makes the machine's stack even |
| 2056 | on x86-64; here nothing is pushed, so sp is as the prologue left it *) |
| 2057 | proc tryAlign*; |
| 2058 | begin |
| 2059 | end; |
| 2060 | |
| 2061 | (* The procedure returns the error in register r, and a zero for each |
| 2062 | of its other results; r isn't x0, x1 or x2, which this uses. *) |
| 2063 | proc returnError(r: INT); |
| 2064 | var f: ^Sym.Obj; |
| 2065 | begin |
| 2066 | if Sym.behindPointer(resType) then |
| 2067 | A.load(Dst, FP, INT(retSlot), 8, false); |
| 2068 | zeroBytes(resType^.size); |
| 2069 | f := resType^.fields; |
| 2070 | while f^.next # nil do f := f^.next; end; |
| 2071 | A.load(Dst, FP, INT(retSlot), 8, false); |
| 2072 | A.store(r, Dst, INT(f^.val), 8); |
| 2073 | elsif Sym.inTwoRegs(resType) then |
| 2074 | A.movRR(1, r); |
| 2075 | A.movRI(0, 0); |
| 2076 | else |
| 2077 | A.movRR(0, r); |
| 2078 | end; |
| 2079 | A.jmp(exitL); |
| 2080 | end; |
| 2081 | |
| 2082 | (* The rest of what try does where the error isn't nil: the procedure |
| 2083 | returns the error in the frame slot. Only code is made: the stack of |
| 2084 | values is left as it is, for the code that follows, where the error |
| 2085 | is nil. *) |
| 2086 | proc tryReturn*(slot: I64); |
| 2087 | begin |
| 2088 | A.load(U, FP, INT(slot), 8, false); |
| 2089 | returnError(U); |
| 2090 | end; |
| 2091 | |
| 2092 | (* try, with no deferred statement to run: the error on top is tested in |
| 2093 | its register r, which is returned, and, if it isn't nil, returned |
| 2094 | from the procedure by tryReturnFrom *) |
| 2095 | proc tryTest*(var ok: INT): INT; |
| 2096 | var r: INT; |
| 2097 | begin |
| 2098 | r := loaded(depth - 1); |
| 2099 | jumpIf(ok, false); |
| 2100 | return r; |
| 2101 | end; |
| 2102 | |
| 2103 | proc tryReturnFrom*(r: INT); |
| 2104 | begin |
| 2105 | returnError(r); |
| 2106 | end; |
| 2107 | |
| 2108 | (* The end of the procedure in chunk c: the code that returns, and the |
| 2109 | frame's size, known only now, patched into its prologue. If it is |
| 2110 | open, running off its end, it returns zeros. *) |
| 2111 | proc endProc*(c: INT; open: BOOL): ERROR; |
| 2112 | var n: I64; |
| 2113 | begin |
| 2114 | codeChunk := -1; |
| 2115 | if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end; |
| 2116 | if open and Sym.behindPointer(resType) then |
| 2117 | A.load(Dst, FP, INT(retSlot), 8, false); |
| 2118 | zeroBytes(resType^.size); |
| 2119 | elsif open then |
| 2120 | A.movRI(0, 0); |
| 2121 | if Sym.inTwoRegs(resType) then A.movRI(1, 0); end; |
| 2122 | A.fmovFR(0, ZR, false); |
| 2123 | else |
| 2124 | A.dropJumpTo(exitL); |
| 2125 | end; |
| 2126 | A.fixLink(exitL); |
| 2127 | saveRegs(true); |
| 2128 | A.movRR(SP, FP); |
| 2129 | A.ldp(FP, LR, SP, 16, A.mPost); |
| 2130 | A.ret(); |
| 2131 | endFails(); |
| 2132 | n := Sym.alignUp(frameMax, 16); |
| 2133 | if n >= 1000000H then return Scan.mark("frame too big: 16 MB or more"); end; |
| 2134 | Chunk.put4At(frameFix, SubSp12 | INT((n shr 12) shl 10)); |
| 2135 | Chunk.put4At(frameFix + 4, SubSp | INT((n & 0FFFH) shl 10)); |
| 2136 | if A.listing then |
| 2137 | A.restate(frameFix, Diag.say("sub sp, sp, #{}, lsl #12", [Fmt.int(INT(n shr 12))])); |
| 2138 | A.restate(frameFix + 4, Diag.say("sub sp, sp, #{}", [Fmt.int(INT(n & 0FFFH))])); |
| 2139 | end; |
| 2140 | Chunk.endChunk(c); |
| 2141 | return nil; |
| 2142 | end; |
| 2143 | |
| 2144 | (* A deferred statement is compiled where it stands, as a subroutine of |
| 2145 | its procedure that the code jumps over, in the procedure's own frame |
| 2146 | (the same x29), so that it sees all its variables; deferBegin gives |
| 2147 | its address and what pushed was. bl, which runs it, leaves its return |
| 2148 | address in x30, which its own calls would change: so it keeps x30 on |
| 2149 | the machine's stack, 16 bytes, so that sp stays a multiple of 16: |
| 2150 | |
| 2151 | b over |
| 2152 | at: str x30, [sp, #-16]! |
| 2153 | ... ; the statement |
| 2154 | ldr x30, [sp], #16 |
| 2155 | ret |
| 2156 | over: *) |
| 2157 | proc deferBegin*(var L: INT): (INT, INT); |
| 2158 | var at: INT; |
| 2159 | begin |
| 2160 | A.jmp(L); |
| 2161 | at := Chunk.pc; |
| 2162 | A.storeIdx(LR, SP, -16, 8, A.mPre); |
| 2163 | return at, pushed; |
| 2164 | end; |
| 2165 | |
| 2166 | proc deferEnd*(L, saved: INT); |
| 2167 | begin |
| 2168 | keepTemps(); |
| 2169 | A.loadIdx(LR, SP, 16, 8, A.mPost); |
| 2170 | A.ret(); |
| 2171 | A.fixLink(L); |
| 2172 | pushed := saved; |
| 2173 | end; |
| 2174 | |
| 2175 | proc runDefer*(at: INT); |
| 2176 | begin |
| 2177 | A.callBack(at); |
| 2178 | end; |
| 2179 | |
| 2180 | (* The entry point, main, and its chunk. macOS starts every program in |
| 2181 | dyld, the dynamic loader, which binds its C symbols and calls the |
| 2182 | procedure LC_MAIN names as C's main(argc, argv): this one. It saves |
| 2183 | argc, an int (so its upper half is undefined until extended), and |
| 2184 | argv; gives the library C's symbols it needs (cLinks: Rt's cExit) and |
| 2185 | Rt the addresses it is given (links); runs every used module's body, |
| 2186 | then main; and ends with Rt.exit, its status main's or 0. The frame |
| 2187 | record it pushes links to dyld's, where a trace ends. Every program |
| 2188 | reaches the kernel through libSystem, so dynamic is always so; bare, |
| 2189 | for a machine with no system, isn't a Mac's. *) |
| 2190 | proc entryPoint*(dynamic, bare: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT; |
| 2191 | var entry, i: INT; m: ^Sym.Module; |
| 2192 | begin |
| 2193 | if bare or not dynamic then Diag.fatal("a program for macOS runs over libSystem: Rt must use C, and -bare is not for it"); end; |
| 2194 | entry := Chunk.newChunk(Chunk.secText, -1, internStr("main")); |
| 2195 | Chunk.beginChunk(entry); |
| 2196 | A.stp(FP, LR, SP, -16, A.mPre); |
| 2197 | A.movRR(FP, SP); |
| 2198 | A.extend(0, 0, 4, true); |
| 2199 | A.storeChunk(0, argcChunk, 0, 8); |
| 2200 | A.storeChunk(1, argvChunk, 0, 8); |
| 2201 | for i := 0 to numCLinks - 1 do |
| 2202 | A.loadChunk(0, cLinks[i].chunk, 0, 8, false); |
| 2203 | A.storeChunk(0, cLinks[i].into, 0, 8); |
| 2204 | end; |
| 2205 | for i := 0 to numLinks - 1 do |
| 2206 | A.addrChunk(0, links[i].chunk, 0); |
| 2207 | A.storeChunk(0, links[i].into, 0, 8); |
| 2208 | end; |
| 2209 | m := first; |
| 2210 | while m # nil do |
| 2211 | if m^.used and (m^.body >= 0) then A.callChunk(m^.body); end; |
| 2212 | m := m^.next; |
| 2213 | end; |
| 2214 | if mainObj # nil then A.callChunk(mainObj^.chunk); end; |
| 2215 | if (mainObj = nil) or (mainObj^.typ^.base = nil) then A.movRI(0, 0); end; |
| 2216 | A.callChunk(exitProc); |
| 2217 | A.brk(1); (* exit never returns: trap if it does *) |
| 2218 | Chunk.endChunk(entry); |
| 2219 | (* Rt.fail, for the failures of each kind, given a, b, len and name |
| 2220 | by the check's code (endFails), which called the stub by bl: the |
| 2221 | place is that bl's, 4 bytes before the return address. Rt.fail's |
| 2222 | last two arguments, the registers x86-64's trap report shows, are |
| 2223 | none here. |
| 2224 | |
| 2225 | sub x4, x30, #4 |
| 2226 | mov x0, #kind |
| 2227 | mov x6, #0 |
| 2228 | mov x7, #0 |
| 2229 | bl Rt.fail *) |
| 2230 | for i := 0 to LEN(failStubs) - 1 do |
| 2231 | if failStubs[i] >= 0 then |
| 2232 | Chunk.beginChunk(failStubs[i]); |
| 2233 | A.aluI(A.opSub, 4, LR, 4); |
| 2234 | A.movRI(0, i); |
| 2235 | A.movRI(6, 0); |
| 2236 | A.movRI(7, 0); |
| 2237 | A.callChunk(rtFail); |
| 2238 | A.brk(1); |
| 2239 | Chunk.endChunk(failStubs[i]); |
| 2240 | end; |
| 2241 | end; |
| 2242 | return entry; |
| 2243 | end; |
| 2244 | |
| 2245 | (* A code procedure: its instructions, read by the assembler (A64), are |
| 2246 | the whole of chunk c *) |
| 2247 | proc codeProc*(c: INT; constExpr: A.ConstFn; nameFn: A.NameFn): ERROR; |
| 2248 | begin |
| 2249 | Chunk.beginChunk(c); |
| 2250 | try A.assemble(constExpr, nameFn); |
| 2251 | Chunk.endChunk(c); |
| 2252 | return nil; |
| 2253 | end; |
| 2254 | |
| 2255 | (* Whether the code procedure in chunk c is a square root of d0, or of |
| 2256 | s0 if single, in itself, then ret, as Math.sqrt is, which a call of |
| 2257 | it does in place (realInPlace), as on x86-64: its first word, or 0. *) |
| 2258 | const FsqrtD = 1E61C000H; FsqrtS = 1E21C000H; Ret = 0D65F03C0H; |
| 2259 | |
| 2260 | proc oneRealOp*(c: INT; single: BOOL): INT; |
| 2261 | var s, i, w, want: INT; words: [2]INT; |
| 2262 | begin |
| 2263 | s := Chunk.chunks[c].start; |
| 2264 | if (Chunk.chunks[c].size # 8) or (Chunk.chunks[c].numFix # 0) then return 0; end; |
| 2265 | for w := 0 to 1 do |
| 2266 | words[w] := 0; |
| 2267 | for i := 3 to 0 by -1 do words[w] := words[w] shl 8 | INT(Chunk.text[s + 4 * w + i]); end; |
| 2268 | end; |
| 2269 | want := FsqrtD; |
| 2270 | if single then want := FsqrtS; end; |
| 2271 | if (words[0] = want) and (words[1] = Ret) then return want; end; |
| 2272 | return 0; |
| 2273 | end; |
| 2274 | |
| 2275 | (* The square root of oneRealOp on the real on top, in its register: |
| 2276 | Math.sqrt(d2) is fsqrt d16, d16, with no call *) |
| 2277 | proc realInPlace*(op: INT); |
| 2278 | var x: INT; |
| 2279 | begin |
| 2280 | x := top(); |
| 2281 | A.fop1(A.fSqrt, x, x, op = FsqrtS); |
| 2282 | end; |
| 2283 | |
| 2284 | var i: INT; |
| 2285 | begin |
| 2286 | for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end; |
| 2287 | Chunk.setPad(0D4200020H); (* brk #1, a trap, between procedures *) |
| 2288 | codeChunk := -1; |
| 2289 | resetStack(0, 0); |
| 2290 | rtFail := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr("fail")); |
| 2291 | argcChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argc")); |
| 2292 | Chunk.setBss(argcChunk, 8, 8); |
| 2293 | argvChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argv")); |
| 2294 | Chunk.setBss(argvChunk, 8, 8); |
| 2295 | end Gen. |