| | @@ -4,16 +4,9 @@ |
| 4 | 4 | The same stack machine as x86-64's, with the same calls, which Item |
| 5 | 5 | and Parse make: they push constants, addresses and the values of |
| 6 | 6 | variables, operate on the values on top, store them and jump on |
| 7 | | - them. What it makes so far is frames, constants, the values of |
| 8 | | - variables and their stores, the integer operations, comparisons and |
| 9 | | - jumps, jump tables, elements and slices and their checks, calls with |
| 10 | | - integer arguments in registers and on the stack, C's through the |
| 11 | | - quadword the loader fills in, results in memory, try, deferred |
| 12 | | - statements, the atomics, COPY, code procedures and the program's |
| 13 | | - entry point. What else |
| 14 | | - is asked is noted as "not yet on arm64" (notYet), and a program that |
| 15 | | - uses it fails to compile rather than run wrong; PLAN.md 5 says the |
| 16 | | - pieces still to come. |
| 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). |
| 17 | 10 | |
| 18 | 11 | The stack is kept in registers as on x86-64: the value at depth d, |
| 19 | 12 | depth 0 being the bottom, is in x9 to x14, register d mod 6, all of |
| | @@ -25,29 +18,39 @@ |
| 25 | 18 | loaded when it is pushed: no instruction of arm64 but a load or a |
| 26 | 19 | store takes memory. |
| 27 | 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 | + |
| 28 | 29 | Registers outside the stack: x15 and x17, Gen's scratch, as rax and |
| 29 | 30 | rcx are on x86-64; x16, the encoder's (A64); x0 to x3 for copying |
| 30 | 31 | and slices, between calls, where they hold nothing; x8, which Apple's convention |
| 31 | 32 | keeps for the address of a result in memory; x18, the system's, |
| 32 | 33 | never touched; x19 to x23, a procedure's busiest variables (VarRegs), |
| 33 | | - which a called procedure gives back as it found them; x29, the frame |
| 34 | | - pointer, and x30, the return address. |
| 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. |
| 35 | 37 | |
| 36 | 38 | Calls use Apple's arm64 convention: the first eight integer arguments |
| 37 | | - in x0 to x7, the rest on the machine's stack, results in x0 and x1, |
| 38 | | - or in memory at the address in x8, sp a multiple of 16 always, a |
| 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 |
| 39 | 42 | frame record of x29 and x30 that x29 points at, so that the chain of |
| 40 | 43 | frames can be followed. A frame is made whole by the prologue, so |
| 41 | 44 | nothing is pushed in a body but a call's stack arguments, for the |
| 42 | 45 | call, and a deferred statement's return address: pushed is always 0. |
| 43 | 46 | |
| 44 | | - What x86-64 catches by itself, and arm64 doesn't, the code checks: a |
| 45 | | - division by zero, and the smallest value by -1 (divide). |
| 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). |
| 46 | 50 | |
| 47 | 51 | Depends on: Scan and Diag for errors, Sym for types, Chunk for code |
| 48 | 52 | and data, A64 for instructions. |
| 49 | | - Leaves out, for now: reals, FILL, NEW's zeroing, the counts of bits |
| 50 | | - and the rotations; and SYSCALL, which is Linux's only. *) |
| 53 | + Leaves out SYSCALL, which is Linux's only. *) |
| 51 | 54 | module Gen; |
| 52 | 55 | import Fmt, Diag, Scan, Sym, Chunk, A := A64; |
| 53 | 56 | import SYSTEM; |
| | @@ -66,6 +69,7 @@ |
| 66 | 69 | FP = A.FP; LR = A.LR; SP = A.SP; ZR = A.ZR; |
| 67 | 70 | T = 15; U = 17; (* Gen's scratch *) |
| 68 | 71 | Dst = 0; Src = 1; Tmp = 2; (* copying's *) |
| 72 | + XT = 23; (* Gen's scratch for a real *) |
| 69 | 73 | |
| 70 | 74 | type |
| 71 | 75 | Kind* = (kWord, kLong, kF32, kF64); |
| | @@ -160,9 +164,14 @@ |
| 160 | 164 | StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"]; |
| 161 | 165 | NumRegs = 6; |
| 162 | 166 | Regs: [NumRegs]INT = [9, 10, 11, 12, 13, 14]; |
| 167 | + XRegs: [NumRegs]INT = [16, 17, 18, 19, 20, 21]; (* v16 to v21 *) |
| 163 | 168 | (* the condition codes of the comparisons, from sEql: = # < <= > >= *) |
| 164 | 169 | CcSigned: [6]INT = [A.ccEQ, A.ccNE, A.ccLT, A.ccLE, A.ccGT, A.ccGE]; |
| 165 | 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]; |
| 166 | 175 | (* sub sp, sp, #imm12 and the same shifted by 12: the prologue's two, |
| 167 | 176 | whose immediates endProc patches *) |
| 168 | 177 | SubSp = 0D10003FFH; SubSp12 = 0D14003FFH; |
| | @@ -177,48 +186,34 @@ |
| 177 | 186 | retSlot: I64; |
| 178 | 187 | resType: ^Sym.Type; (* the procedure's result, or nil *) |
| 179 | 188 | exitL: INT; |
| 180 | | - (* parameters arriving in registers, x0 to x7, and a result's address |
| 181 | | - in x8: where each is stored on entry *) |
| 182 | | - inReg: [9]INT; |
| 183 | | - inSlot: [9]I64; |
| 184 | | - inSize: [9]INT; |
| 185 | | - inObj: [9]^Sym.Obj; |
| 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; |
| 186 | 196 | numIn: INT; |
| 187 | 197 | procParams: ^Sym.Obj; (* the parameters, for enterBody *) |
| 188 | 198 | regMask: INT; (* the registers of variables, as bits (XBit) *) |
| 189 | 199 | saveAt: I64; (* where they are saved, a quadword each *) |
| 200 | + homes: [8]I64; (* the slots of the variables in XVarRegs *) |
| 190 | 201 | frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *) |
| 191 | | - pending: []const U8; (* what beginProc found not yet made, for enterBody's chunk *) |
| 192 | | - pendingAt: Diag.Pos; |
| 193 | 202 | (* the stubs that a failed check calls, one for each kind of failure, |
| 194 | 203 | made by entryPoint for those used; and the procedure's checks *) |
| 195 | 204 | failStubs: [8]INT; |
| 196 | 205 | fails: [4096]Fail; |
| 197 | 206 | numFails: INT; |
| 198 | 207 | |
| 199 | | -(* What this Gen can't make yet marks the procedure being compiled |
| 208 | +(* What this Gen won't make, SYSCALL, marks the procedure being compiled |
| 200 | 209 | (Chunk.setMissing), which is an error if the program uses it, and the |
| 201 | 210 | compile reads on: a module the program imports may hold what it never |
| 202 | | - calls, as Sys does. So each routine not yet made keeps the stack as |
| 203 | | - the real one would, and makes what code it likes, never run; it |
| 204 | | - reads into _ the parameters it doesn't use yet, so that none is |
| 205 | | - warned of. What this Gen will never make, SYSCALL, is marked so too, |
| 206 | | - with its own message. *) |
| 211 | + calls. The routine keeps the stack as the real one would. *) |
| 207 | 212 | proc missing(msg: []const U8); |
| 208 | 213 | begin |
| 209 | | - if codeChunk >= 0 then |
| 210 | | - Chunk.setMissing(codeChunk, msg, Scan.tokPos()); |
| 211 | | - elsif LEN(pending) = 0 then (* beginProc's, before enterBody names the chunk *) |
| 212 | | - pending := msg; |
| 213 | | - pendingAt := Scan.tokPos(); |
| 214 | | - end; |
| 214 | + if codeChunk >= 0 then Chunk.setMissing(codeChunk, msg, Scan.tokPos()); end; |
| 215 | 215 | end; |
| 216 | 216 | |
| 217 | | -proc notYet(what: []const U8); |
| 218 | | -begin |
| 219 | | - missing(Diag.say("not yet on arm64: {}", [Fmt.str(what)])); |
| 220 | | -end; |
| 221 | | - |
| 222 | 217 | (* -S: the text of each instruction is kept, and list prints it *) |
| 223 | 218 | proc setListing*(yes: BOOL); |
| 224 | 219 | begin |
| | @@ -258,13 +253,14 @@ |
| 258 | 253 | proc allocPerm*(size, align: I64): I64; forward; |
| 259 | 254 | proc loaded(i: INT): INT; forward; |
| 260 | 255 | |
| 261 | | -(* The value i, in its register, goes to a frame slot *) |
| 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 *) |
| 262 | 258 | proc spill(i: INT); |
| 263 | | -var s: I64; |
| 259 | +var s: I64; r: INT; |
| 264 | 260 | begin |
| 265 | | - _ := loaded(i); |
| 261 | + r := loaded(i); |
| 266 | 262 | s := allocTemp(8, 8); |
| 267 | | - A.store(Regs[i mod NumRegs], FP, INT(s), 8); |
| 263 | + if isReal(i) then A.storeF(r, FP, INT(s), 8); else A.store(r, FP, INT(s), 8); end; |
| 268 | 264 | stack[i].loc := lFrame; |
| 269 | 265 | stack[i].val := s; |
| 270 | 266 | owner[i mod NumRegs] := -1; |
| | @@ -292,7 +288,7 @@ |
| 292 | 288 | end; |
| 293 | 289 | |
| 294 | 290 | (* The value i into register r, from wherever it is; it stays where it |
| 295 | | - was, so r is a new copy *) |
| 291 | + was, so r is a new copy. A real goes as its bits. *) |
| 296 | 292 | proc valueTo(i, r: INT); |
| 297 | 293 | begin |
| 298 | 294 | match stack[i].loc of |
| | @@ -300,20 +296,36 @@ |
| 300 | 296 | | lAdr: |
| 301 | 297 | if stack[i].chunk >= 0 then A.addrChunk(r, stack[i].chunk, INT(stack[i].val)); |
| 302 | 298 | else A.aluI(A.opAdd, r, FP, stack[i].val); end; |
| 303 | | - | lReg: if Regs[i mod NumRegs] # r then A.movRR(r, Regs[i mod NumRegs]); 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; |
| 304 | 302 | | lFrame: A.load(r, FP, INT(stack[i].val), 8, false); |
| 305 | 303 | end; |
| 306 | 304 | end; |
| 307 | 305 | |
| 308 | | -(* The value i, in its register *) |
| 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 *) |
| 309 | 320 | proc loaded(i: INT): INT; |
| 310 | 321 | var r: INT; |
| 311 | 322 | begin |
| 312 | 323 | if stack[i].loc # lReg then |
| 313 | 324 | r := claim(i); |
| 314 | | - valueTo(i, r); |
| 325 | + if isReal(i) then xValueTo(i, XRegs[i mod NumRegs]); else valueTo(i, r); end; |
| 315 | 326 | stack[i].loc := lReg; |
| 316 | 327 | end; |
| 328 | + if isReal(i) then return XRegs[i mod NumRegs]; end; |
| 317 | 329 | return Regs[i mod NumRegs]; |
| 318 | 330 | end; |
| 319 | 331 | |
| | @@ -339,12 +351,18 @@ |
| 339 | 351 | INC(depth); |
| 340 | 352 | end; |
| 341 | 353 | |
| 342 | | -(* The register a new value on top is computed in *) |
| 354 | +(* The register a new value on top is computed in, and a real's *) |
| 343 | 355 | proc target: INT; |
| 344 | 356 | begin |
| 345 | 357 | return claim(depth); |
| 346 | 358 | end; |
| 347 | 359 | |
| 360 | +proc xTarget: INT; |
| 361 | +begin |
| 362 | + _ := claim(depth); |
| 363 | + return XRegs[depth mod NumRegs]; |
| 364 | +end; |
| 365 | + |
| 348 | 366 | (* The value just computed in target goes on top *) |
| 349 | 367 | proc computed(k: Kind); |
| 350 | 368 | begin |
| | @@ -378,9 +396,10 @@ |
| 378 | 396 | for r := 0 to NumRegs - 1 do owner[r] := -1; end; |
| 379 | 397 | end; |
| 380 | 398 | |
| 399 | +(* A constant; a real's v is its bits, an F64's or, in the low 32, an |
| 400 | + F32's *) |
| 381 | 401 | proc pushConst*(k: Kind; v: I64); |
| 382 | 402 | begin |
| 383 | | - if k in {kF32, kF64} then notYet("reals"); end; |
| 384 | 403 | newEntry(k, lConst, v); |
| 385 | 404 | end; |
| 386 | 405 | |
| | @@ -435,6 +454,15 @@ |
| 435 | 454 | else A.store(r, m.base, INT(m.off), size); end; |
| 436 | 455 | end; |
| 437 | 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 | + |
| 438 | 466 | (* Register r := the address m *) |
| 439 | 467 | proc adrTo(r: INT; m: Mem); |
| 440 | 468 | begin |
| | @@ -465,21 +493,21 @@ |
| 465 | 493 | end; |
| 466 | 494 | |
| 467 | 495 | proc pushVar*(a: Adr; size: INT; signed: BOOL; k: Kind); |
| 468 | | -var m: Mem; r: INT; |
| 496 | +var m: Mem; r: INT; fl: BOOL; |
| 469 | 497 | begin |
| 470 | | - if k in {kF32, kF64} then notYet("reals"); end; |
| 471 | | - if a.base = bReg then |
| 472 | | - r := target(); |
| 473 | | - A.movRR(r, regAt(a)); |
| 474 | | - computed(k); |
| 475 | | - return; |
| 476 | | - end; |
| 477 | | - if a.base = bStack then |
| 478 | | - m := place(a, U); r := target(); |
| 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)); |
| 479 | 503 | else |
| 480 | | - r := target(); m := place(a, r); |
| 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; |
| 481 | 510 | end; |
| 482 | | - loadMem(r, m, size, signed); |
| 483 | 511 | computed(k); |
| 484 | 512 | end; |
| 485 | 513 | |
| | @@ -492,32 +520,44 @@ |
| 492 | 520 | end; |
| 493 | 521 | |
| 494 | 522 | (* Pops a value into the variable at a; an address on the stack is under |
| 495 | | - the value. A zero is stored from the zero register. *) |
| 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. *) |
| 496 | 526 | proc popVar*(a: Adr; size: INT); |
| 497 | | -var r: INT; v: I64; c: BOOL; |
| 527 | +var r: INT; v: I64; c, fl: BOOL; |
| 498 | 528 | begin |
| 499 | | - if isReal(depth - 1) then notYet("reals"); end; |
| 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; |
| 500 | 536 | if a.base = bReg then |
| 501 | 537 | popTo(regAt(a)); |
| 502 | 538 | return; |
| 503 | 539 | end; |
| 504 | 540 | c, v := topConst(); |
| 541 | + fl := not c and isReal(depth - 1); |
| 505 | 542 | if c and (v = 0) then r := ZR; |
| 506 | 543 | elsif c then r := T; A.movRI(T, v); |
| 507 | 544 | else r := loaded(depth - 1); end; |
| 508 | 545 | drop(); |
| 509 | | - storeMem(r, place(a, U), size); |
| 546 | + if fl then realMem(r, place(a, U), size, false); else storeMem(r, place(a, U), size); end; |
| 510 | 547 | end; |
| 511 | 548 | |
| 512 | | -(* The two values on top change places, each loaded in its register *) |
| 549 | +(* The two values on top, of one kind, change places, each loaded in |
| 550 | + its register *) |
| 513 | 551 | proc swap*; |
| 514 | 552 | var e: Entry; x, y: INT; |
| 515 | 553 | begin |
| 516 | 554 | x := loaded(depth - 2); |
| 517 | 555 | y := loaded(depth - 1); |
| 518 | | - A.movRR(T, x); |
| 519 | | - A.movRR(x, y); |
| 520 | | - A.movRR(y, T); |
| 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; |
| 521 | 561 | e := stack[depth - 2]; stack[depth - 2] := stack[depth - 1]; stack[depth - 1] := e; |
| 522 | 562 | end; |
| 523 | 563 | |
| | @@ -740,6 +780,23 @@ |
| 740 | 780 | if sy = Scan.sDiv then A.movRR(r, T); else A.madd(r, T, y, r, true); end; |
| 741 | 781 | end; |
| 742 | 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 | + |
| 743 | 800 | (* The two values on top, the left op the right, an integer of size |
| 744 | 801 | bytes, signed or not, computed in 64 bits and brought back to its |
| 745 | 802 | width, which is how a narrow integer wraps. A shift's count is taken |
| | @@ -747,7 +804,7 @@ |
| 747 | 804 | proc op*(sy: Scan.Sym; k: Kind; size: INT; signed: BOOL); |
| 748 | 805 | var v: I64; r, y, bits, sh: INT; c, alu: BOOL; |
| 749 | 806 | begin |
| 750 | | - if k in {kF32, kF64} then notYet("reals"); end; |
| 807 | + if k in {kF32, kF64} then realOp(sy, k); return; end; |
| 751 | 808 | bits := size * 8; |
| 752 | 809 | alu := (sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor); |
| 753 | 810 | (* a constant goes right, where it can be an immediate *) |
| | @@ -780,27 +837,101 @@ |
| 780 | 837 | stack[depth - 1].k := k; |
| 781 | 838 | end; |
| 782 | 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. *) |
| 783 | 860 | proc rotate*(left: BOOL; size: INT); |
| 861 | +var c: BOOL; v: I64; r, y, w, sz: INT; |
| 784 | 862 | begin |
| 785 | | - _ := left; _ := size; |
| 786 | | - notYet("ROL and ROR"); |
| 787 | | - drop(); (* the count *) |
| 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; |
| 788 | 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: |
| 789 | 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 *) |
| 790 | 898 | proc countZeros*(leading: BOOL; size: INT); |
| 899 | +var r, w, sz: INT; |
| 791 | 900 | begin |
| 792 | | - _ := leading; _ := size; |
| 793 | | - notYet("CLZ and CTZ"); |
| 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; |
| 794 | 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: |
| 795 | 915 | |
| 916 | + fmov d23, x9 |
| 917 | + cnt v23.8b, v23.8b |
| 918 | + addv b23, v23.8b |
| 919 | + fmov x9, d23 *) |
| 796 | 920 | proc popCount*; |
| 921 | +var r: INT; |
| 797 | 922 | begin |
| 798 | | - notYet("POPCOUNT"); |
| 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; |
| 799 | 929 | end; |
| 800 | 930 | |
| 801 | | -(* ABS of the signed integer on top: negated if negative, and cut back |
| 802 | | - to its width, so that the smallest value gives itself, as -x does |
| 803 | | - (Gen.x86_64.raz says more): |
| 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): |
| 804 | 935 | |
| 805 | 936 | cmp x9, #0 |
| 806 | 937 | cneg x9, x9, lt ; csneg x9, x9, x9, ge |
| | @@ -808,30 +939,43 @@ |
| 808 | 939 | proc abs*(k: Kind; size: INT); |
| 809 | 940 | var r: INT; |
| 810 | 941 | begin |
| 811 | | - if k in {kF32, kF64} then notYet("reals"); end; |
| 812 | 942 | r := top(); |
| 943 | + if k in {kF32, kF64} then A.fop1(A.fAbs, r, r, k = kF32); return; end; |
| 813 | 944 | A.cmpI(r, 0); |
| 814 | 945 | A.csel(A.csNeg, A.ccGE, r, r, r); |
| 815 | 946 | if size < 8 then A.extend(r, r, size, true); end; |
| 816 | 947 | end; |
| 817 | 948 | |
| 818 | | -(* The two values on top, compared: a BOOL, 1 or 0 *) |
| 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) *) |
| 819 | 952 | proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL); |
| 820 | | -var r, y, i: INT; |
| 953 | +var r, x, y, i: INT; |
| 821 | 954 | begin |
| 822 | | - if k in {kF32, kF64} then notYet("reals"); end; |
| 823 | 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; |
| 824 | 967 | r, y := operands(); |
| 825 | 968 | A.cmp(r, y); |
| 826 | 969 | if signed then A.cset(CcSigned[i], r); else A.cset(CcUnsigned[i], r); end; |
| 827 | 970 | stack[depth - 1].k := kWord; |
| 828 | 971 | end; |
| 829 | 972 | |
| 973 | +(* -x; of a real, its sign bit flipped, fneg, as x86-64 flips it *) |
| 830 | 974 | proc neg*(k: Kind; size: INT; signed: BOOL); |
| 831 | 975 | var r: INT; |
| 832 | 976 | begin |
| 833 | | - if k in {kF32, kF64} then notYet("reals"); end; |
| 834 | 977 | r := top(); |
| 978 | + if k in {kF32, kF64} then A.fop1(A.fNeg, r, r, k = kF32); return; end; |
| 835 | 979 | A.neg(r, r); |
| 836 | 980 | if size < 8 then A.extend(r, r, size, signed); end; |
| 837 | 981 | end; |
| | @@ -860,24 +1004,53 @@ |
| 860 | 1004 | stack[depth - 1].k := kWord; |
| 861 | 1005 | end; |
| 862 | 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 *) |
| 863 | 1010 | proc intToReal*(k: Kind; big: BOOL); |
| 1011 | +var r: INT; |
| 864 | 1012 | begin |
| 865 | | - _ := big; |
| 866 | | - notYet("reals"); |
| 1013 | + r := top(); |
| 1014 | + A.scvtf(XRegs[(depth - 1) mod NumRegs], r, k = kF32, not big); |
| 867 | 1015 | stack[depth - 1].k := k; |
| 868 | 1016 | end; |
| 869 | 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. *) |
| 870 | 1032 | proc realToInt*(k: Kind); |
| 1033 | +var x, r: INT; |
| 871 | 1034 | begin |
| 872 | | - _ := k; |
| 873 | | - notYet("reals"); |
| 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); |
| 874 | 1043 | stack[depth - 1].k := kWord; |
| 875 | 1044 | end; |
| 876 | 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 *) |
| 877 | 1048 | proc realToReal*(k: Kind); |
| 1049 | +var x: INT; |
| 878 | 1050 | begin |
| 879 | | - notYet("reals"); |
| 880 | | - stack[depth - 1].k := k; |
| 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; |
| 881 | 1054 | end; |
| 882 | 1055 | |
| 883 | 1056 | (* Whether the slices in the frame at a and b (the offsets of their |
| | @@ -966,10 +1139,18 @@ |
| 966 | 1139 | if size < 8 then A.extend(r, r, size, signed); end; |
| 967 | 1140 | end; |
| 968 | 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 *) |
| 969 | 1145 | proc zeroNew*(size: I64); |
| 1146 | +var L, r: INT; |
| 970 | 1147 | begin |
| 971 | | - _ := size; |
| 972 | | - notYet("NEW"); |
| 1148 | + r := top(); |
| 1149 | + L := NoLabel; |
| 1150 | + A.cbz(false, r, L); |
| 1151 | + A.movRR(Dst, r); |
| 1152 | + zeroBytes(size); |
| 1153 | + A.fixLink(L); |
| 973 | 1154 | end; |
| 974 | 1155 | |
| 975 | 1156 | (* CAS(v, old, new), XCHG(v, x) or XADD(v, x), from the stack, v's |
| | @@ -1378,11 +1559,74 @@ |
| 1378 | 1559 | A.fixLink(out); |
| 1379 | 1560 | end; |
| 1380 | 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 | + |
| 1381 | 1593 | proc fillSlice*(dOff, size, n: I64; scalar: BOOL); |
| 1594 | +var c: BOOL; v, i: I64; out, top: INT; |
| 1382 | 1595 | begin |
| 1383 | | - _ := dOff; _ := size; _ := n; _ := scalar; |
| 1384 | | - notYet("FILL"); |
| 1385 | | - drop(); |
| 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); |
| 1386 | 1630 | end; |
| 1387 | 1631 | |
| 1388 | 1632 | (* Pops hi, lo and a pointer, and makes the slice p[lo..hi] at tmp. Its |
| | @@ -1418,23 +1662,27 @@ |
| 1418 | 1662 | end; |
| 1419 | 1663 | |
| 1420 | 1664 | (* Calls. The arguments are the c.n values on top, each moved into the |
| 1421 | | - next of x0 to x7 just before the call; the values under them are |
| 1422 | | - spilled first, as the call may change their registers. As the stack's |
| 1423 | | - registers carry no argument, the moves may go in any order. A slice |
| 1424 | | - goes as its two words, both in registers or both on the stack, a |
| 1425 | | - record by value as the address of a copy (Item makes it). The |
| 1426 | | - arguments past the registers go on the machine's stack, 8 bytes |
| 1427 | | - each, the first at sp, in an area sp is lowered by for the call, a |
| 1428 | | - multiple of 16; and so do those past a C procedure's own parameters |
| 1429 | | - where it ends in ..., in registers or not, as Apple's convention has |
| 1430 | | - it (where a C procedure's own parameters past x7 narrower than 8 |
| 1431 | | - bytes would be packed, which raz doesn't do). A result in memory, a |
| 1432 | | - record, an array or several results, goes to the caller's temporary |
| 1433 | | - c.ret, its address in x8. A single result comes back in x0, two words |
| 1434 | | - in x0 and x1: a slice is stored in c.ret, two results pushed as two |
| 1435 | | - values. *) |
| 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. *) |
| 1436 | 1682 | proc call*(var c: CallInfo); |
| 1437 | | -var res: ^Sym.Type; i, ni, k, first, own, r: INT; area: I64; onStack: [64]BOOL; |
| 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 *) |
| 1438 | 1686 | begin |
| 1439 | 1687 | first := depth - c.n; |
| 1440 | 1688 | spillBelow(first); |
| | @@ -1444,27 +1692,39 @@ |
| 1444 | 1692 | c.ret := allocPerm(res^.size, res^.align); (* kept, as on x86-64 (report 8) *) |
| 1445 | 1693 | elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then |
| 1446 | 1694 | c.ret := allocTemp(res^.size, res^.align); |
| 1447 | | - elsif (res # nil) and (res^.form = Sym.fReal) then |
| 1448 | | - notYet("reals"); |
| 1449 | 1695 | end; |
| 1450 | 1696 | own := c.n; |
| 1451 | 1697 | if c.sig^.variadic then own := paramWords(c.sig); end; |
| 1452 | | - ni := 0; k := 0; |
| 1698 | + ni := 0; nf := 0; area := 0; |
| 1699 | + q := c.sig^.fields; (* the parameter of argument i, while there is one *) |
| 1453 | 1700 | for i := 0 to c.n - 1 do |
| 1454 | | - if c.isF[i] then notYet("reals"); end; |
| 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; |
| 1455 | 1707 | if i >= own then onStack[i] := true; |
| 1708 | + elsif c.isF[i] then onStack[i] := nf = 8; |
| 1456 | 1709 | elsif (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1]; |
| 1457 | 1710 | else onStack[i] := ni + 1 + INT(c.pair[i]) > 8; end; |
| 1458 | | - if onStack[i] then INC(k); else INC(ni); 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; |
| 1459 | 1717 | end; |
| 1460 | | - area := Sym.alignUp(8 * k, 16); |
| 1718 | + area := Sym.alignUp(area, 16); |
| 1461 | 1719 | if area > 0 then A.aluI(A.opSub, SP, SP, area); end; |
| 1462 | | - k := 0; ni := 0; |
| 1720 | + ni := 0; nf := 0; |
| 1463 | 1721 | for i := 0 to c.n - 1 do |
| 1464 | 1722 | if onStack[i] then |
| 1465 | 1723 | valueTo(first + i, T); |
| 1466 | | - A.store(T, SP, 8 * k, 8); |
| 1467 | | - INC(k); |
| 1724 | + A.store(T, SP, at[i], size[i]); |
| 1725 | + elsif c.isF[i] then |
| 1726 | + xValueTo(first + i, nf); |
| 1727 | + INC(nf); |
| 1468 | 1728 | else |
| 1469 | 1729 | valueTo(first + i, ni); |
| 1470 | 1730 | INC(ni); |
| | @@ -1489,6 +1749,9 @@ |
| 1489 | 1749 | elsif Sym.inTwoRegs(res) then |
| 1490 | 1750 | A.movRR(target(), 0); computed(kWord); |
| 1491 | 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)); |
| 1492 | 1755 | elsif c.ret = 0 then |
| 1493 | 1756 | (* C leaves the bits above a narrow result as they fall *) |
| 1494 | 1757 | r := target(); |
| | @@ -1539,10 +1802,12 @@ |
| 1539 | 1802 | (* The parameters' places: each in a frame slot of its own, stored there |
| 1540 | 1803 | from its register on entry, or above the frame where the caller put |
| 1541 | 1804 | it, past the frame record (call). A var parameter, and a record or |
| 1542 | | - array passed by value, holds an address (ind). The address of a |
| 1543 | | - result in memory arrives in x8, and is kept in retSlot. *) |
| 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. *) |
| 1544 | 1809 | proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj); |
| 1545 | | -var p: ^Sym.Obj; ni, k, w: INT; stackOff: I64; |
| 1810 | +var p: ^Sym.Obj; ni, nf, k, w: INT; stackOff: I64; f: BOOL; |
| 1546 | 1811 | begin |
| 1547 | 1812 | frameTop := 0; frameMax := 0; permTop := 0; |
| 1548 | 1813 | resetStack(0, 0); |
| | @@ -1557,18 +1822,20 @@ |
| 1557 | 1822 | inReg[0] := 8; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; |
| 1558 | 1823 | numIn := 1; |
| 1559 | 1824 | end; |
| 1560 | | - ni := 0; |
| 1825 | + ni := 0; nf := 0; |
| 1561 | 1826 | stackOff := 16; |
| 1562 | 1827 | p := params; |
| 1563 | 1828 | while p # nil do |
| 1564 | 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; |
| 1565 | 1831 | k := 1; |
| 1566 | 1832 | if (p^.typ^.form = Sym.fSlice) and not p^.isVar then k := 2; end; |
| 1567 | | - if (p^.typ^.form = Sym.fReal) and not p^.isVar then |
| 1568 | | - notYet("a real parameter"); |
| 1833 | + if f and (nf < 8) then |
| 1569 | 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); |
| 1570 | 1837 | k := 0; |
| 1571 | | - elsif ni + k > 8 then (* on the stack, as call puts it *) |
| 1838 | + elsif f or (ni + k > 8) then (* on the stack, as call puts it *) |
| 1572 | 1839 | p^.val := stackOff; |
| 1573 | 1840 | INC(stackOff, 8 * k); |
| 1574 | 1841 | k := 0; |
| | @@ -1615,16 +1882,42 @@ |
| 1615 | 1882 | zeroTop := permTop; |
| 1616 | 1883 | regMask := mask; |
| 1617 | 1884 | k := 0; |
| 1618 | | - for i := 0 to 4 do |
| 1885 | + for i := 0 to XBit + 7 do |
| 1619 | 1886 | if (mask & (1 shl i)) # 0 then INC(k); end; |
| 1620 | 1887 | end; |
| 1621 | 1888 | if k > 0 then saveAt := allocPerm(8 * k, 8); end; |
| 1622 | 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; |
| 1623 | 1914 | end; |
| 1624 | 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 *) |
| 1625 | 1918 | proc xHome*(r: INT; slot: I64); |
| 1626 | 1919 | begin |
| 1627 | | - _ := r; _ := slot; |
| 1920 | + homes[r - XVarRegs[0]] := slot; |
| 1628 | 1921 | end; |
| 1629 | 1922 | |
| 1630 | 1923 | proc markCode*(var m: CodeMark); |
| | @@ -1667,7 +1960,6 @@ |
| 1667 | 1960 | begin |
| 1668 | 1961 | Chunk.beginChunk(c); |
| 1669 | 1962 | codeChunk := c; |
| 1670 | | - if LEN(pending) > 0 then Chunk.setMissing(c, pending, pendingAt); pending := ""; end; |
| 1671 | 1963 | Chunk.noteLine(line, 1); (* a declaration starts a line *) |
| 1672 | 1964 | A.stp(FP, LR, SP, -16, A.mPre); |
| 1673 | 1965 | A.movRR(FP, SP); |
| | @@ -1685,18 +1977,13 @@ |
| 1685 | 1977 | A.aluI(A.opSubs, U, U, 1); |
| 1686 | 1978 | A.jccBack(A.ccNE, loop); |
| 1687 | 1979 | end; |
| 1688 | | - if (regMask shr XBit) # 0 then notYet("reals in registers"); end; |
| 1689 | | - Chunk.setSaves(c, regMask, INT(saveAt)); |
| 1690 | | - n := 0; |
| 1691 | | - for i := 0 to 4 do |
| 1692 | | - if (regMask & (1 shl i)) # 0 then |
| 1693 | | - A.store(VarRegs[i], FP, INT(saveAt + 8 * n), 8); |
| 1694 | | - A.movRI(VarRegs[i], 0); |
| 1695 | | - INC(n); |
| 1696 | | - end; |
| 1697 | | - end; |
| 1980 | + Chunk.setSaves(c, regMask & ((1 shl XBit) - 1), INT(saveAt)); |
| 1981 | + saveRegs(false); |
| 1698 | 1982 | for i := 0 to numIn - 1 do |
| 1699 | | - if (inObj[i] # nil) and (inObj[i]^.reg # 0) then |
| 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 |
| 1700 | 1987 | r := inObj[i]^.reg; |
| 1701 | 1988 | if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end; |
| 1702 | 1989 | A.movRR(r, inReg[i]); |
| | @@ -1711,7 +1998,9 @@ |
| 1711 | 1998 | p := procParams; |
| 1712 | 1999 | while p # nil do |
| 1713 | 2000 | if (p^.reg # 0) and (p^.val > 0) then |
| 1714 | | - if not p^.ind and (p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet}) 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 |
| 1715 | 2004 | A.load(p^.reg, FP, INT(p^.val), INT(p^.typ^.size), p^.typ^.signed); |
| 1716 | 2005 | else |
| 1717 | 2006 | A.load(p^.reg, FP, INT(p^.val), 8, false); |
| | @@ -1730,12 +2019,13 @@ |
| 1730 | 2019 | (* Pops one of several results, a value of size bytes, into its place |
| 1731 | 2020 | off bytes into the caller's, at the address that came in x8 *) |
| 1732 | 2021 | proc resultTo*(off: I64; size: INT); |
| 1733 | | -var r: INT; |
| 2022 | +var r: INT; fl: BOOL; |
| 1734 | 2023 | begin |
| 1735 | 2024 | r := top(); |
| 2025 | + fl := isReal(depth - 1); |
| 1736 | 2026 | drop(); |
| 1737 | 2027 | A.load(U, FP, INT(retSlot), 8, false); |
| 1738 | | - A.store(r, U, INT(off), size); |
| 2028 | + if fl then A.storeF(r, U, INT(off), size); else A.store(r, U, INT(off), size); end; |
| 1739 | 2029 | end; |
| 1740 | 2030 | |
| 1741 | 2031 | (* Pops an address, and copies size bytes from it to the result, off |
| | @@ -1748,11 +2038,14 @@ |
| 1748 | 2038 | copyBytes(size); |
| 1749 | 2039 | end; |
| 1750 | 2040 | |
| 1751 | | -(* return: the result, a value on top, goes in x0; two words, the two |
| 1752 | | - values on top, in x0 and x1; one in memory is there already *) |
| 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 *) |
| 1753 | 2043 | proc returnJump*; |
| 1754 | 2044 | begin |
| 1755 | | - if (resType # nil) and not Sym.behindPointer(resType) then |
| 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 |
| 1756 | 2049 | if Sym.inTwoRegs(resType) then popTo(1); end; |
| 1757 | 2050 | popTo(0); |
| 1758 | 2051 | end; |
| | @@ -1816,7 +2109,7 @@ |
| 1816 | 2109 | frame's size, known only now, patched into its prologue. If it is |
| 1817 | 2110 | open, running off its end, it returns zeros. *) |
| 1818 | 2111 | proc endProc*(c: INT; open: BOOL): ERROR; |
| 1819 | | -var n: I64; i: INT; |
| 2112 | +var n: I64; |
| 1820 | 2113 | begin |
| 1821 | 2114 | codeChunk := -1; |
| 1822 | 2115 | if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end; |
| | @@ -1826,17 +2119,12 @@ |
| 1826 | 2119 | elsif open then |
| 1827 | 2120 | A.movRI(0, 0); |
| 1828 | 2121 | if Sym.inTwoRegs(resType) then A.movRI(1, 0); end; |
| 2122 | + A.fmovFR(0, ZR, false); |
| 1829 | 2123 | else |
| 1830 | 2124 | A.dropJumpTo(exitL); |
| 1831 | 2125 | end; |
| 1832 | 2126 | A.fixLink(exitL); |
| 1833 | | - n := 0; |
| 1834 | | - for i := 0 to 4 do |
| 1835 | | - if (regMask & (1 shl i)) # 0 then |
| 1836 | | - A.load(VarRegs[i], FP, INT(saveAt + 8 * n), 8, false); |
| 1837 | | - INC(n); |
| 1838 | | - end; |
| 1839 | | - end; |
| 2127 | + saveRegs(true); |
| 1840 | 2128 | A.movRR(SP, FP); |
| 1841 | 2129 | A.ldp(FP, LR, SP, 16, A.mPost); |
| 1842 | 2130 | A.ret(); |
| | @@ -1964,18 +2252,33 @@ |
| 1964 | 2252 | return nil; |
| 1965 | 2253 | end; |
| 1966 | 2254 | |
| 1967 | | -(* A code procedure that is one instruction on a real, as Math.sqrt on |
| 1968 | | - x86-64, is made in place of its call there; none is, yet, here *) |
| 1969 | | -proc oneRealOp*(c: INT): INT; |
| 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; |
| 1970 | 2262 | begin |
| 1971 | | - _ := c; |
| 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; |
| 1972 | 2272 | return 0; |
| 1973 | 2273 | end; |
| 1974 | 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 *) |
| 1975 | 2277 | proc realInPlace*(op: INT); |
| 2278 | +var x: INT; |
| 1976 | 2279 | begin |
| 1977 | | - _ := op; |
| 1978 | | - notYet("reals"); |
| 2280 | + x := top(); |
| 2281 | + A.fop1(A.fSqrt, x, x, op = FsqrtS); |
| 1979 | 2282 | end; |
| 1980 | 2283 | |
| 1981 | 2284 | var i: INT; |
| | @@ -1983,7 +2286,6 @@ |
| 1983 | 2286 | for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end; |
| 1984 | 2287 | Chunk.setPad(0D4200020H); (* brk #1, a trap, between procedures *) |
| 1985 | 2288 | codeChunk := -1; |
| 1986 | | - pending := ""; |
| 1987 | 2289 | resetStack(0, 0); |
| 1988 | 2290 | rtFail := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr("fail")); |
| 1989 | 2291 | argcChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argc")); |