| | @@ -9,10 +9,10 @@ |
| 9 | 9 | code for those calls, and nothing above it knows the machine: a port |
| 10 | 10 | is a new gen.pas, with an encoder of its own. |
| 11 | 11 | |
| 12 | | - Here the stack is the machine's: the value on top, once computed, is |
| 13 | | - in rax, and the values under it are pushed when another is computed. |
| 14 | | - A constant is only noted until something needs it in a register, so |
| 15 | | - that it can be an instruction's operand. |
| 12 | + The stack is kept in registers, as in Wirth's compilers: the value at |
| 13 | + depth d in register d, of r8 to r11, a deeper one spilled to the frame |
| 14 | + while its register is wanted. A constant is only noted until something |
| 15 | + needs it in a register, so that it can be an instruction's operand. |
| 16 | 16 | |
| 17 | 17 | Calls use the System V convention. Arguments are computed left to |
| 18 | 18 | right into frame slots, then loaded into registers just before the |
| | @@ -144,15 +144,28 @@ |
| 144 | 144 | implementation |
| 145 | 145 | |
| 146 | 146 | type |
| 147 | + { where a value on the stack is } |
| 148 | + TLoc = (lConst, { not loaded: val is the value } |
| 149 | + lReg, { in its register } |
| 150 | + lFrame); { spilled: in the frame slot val } |
| 151 | + |
| 147 | 152 | TEntry = record |
| 148 | | - isConst: boolean; { not loaded: val is the value } |
| 153 | + loc: TLoc; |
| 149 | 154 | val: int64; |
| 150 | 155 | k: TKind; |
| 151 | 156 | end; |
| 152 | 157 | |
| 158 | +const |
| 159 | + { the registers of the stack: the value at depth d is in Regs[d mod |
| 160 | + NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi |
| 161 | + and rdi, which copying needs, aren't among them. } |
| 162 | + NumRegs = 4; |
| 163 | + Regs: array[0..NumRegs - 1] of longint = (R8, R9, R10, R11); |
| 164 | + IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9); |
| 165 | + |
| 153 | 166 | var |
| 154 | 167 | stack: array[0..255] of TEntry; |
| 155 | | - inRax: longint; { the entry whose value is in rax, or -1 } |
| 168 | + owner: array[0..NumRegs - 1] of longint; { the value in each register, or -1 } |
| 156 | 169 | frameTop, frameMax, permTop: int64; |
| 157 | 170 | frameFix: longint; { where the frame size is patched in } |
| 158 | 171 | paramTop: int64; { the parameters' slots end here } |
| | @@ -165,9 +178,6 @@ |
| 165 | 178 | inSize: array[0..13] of longint; |
| 166 | 179 | numIn: longint; |
| 167 | 180 | |
| 168 | | -const |
| 169 | | - IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9); |
| 170 | | - |
| 171 | 181 | function KindOf(t: PType): TKind; |
| 172 | 182 | begin |
| 173 | 183 | if t^.form = fReal then begin |
| | @@ -176,158 +186,186 @@ |
| 176 | 186 | else KindOf := kWord; |
| 177 | 187 | end; |
| 178 | 188 | |
| 179 | | -{ The stack } |
| 189 | +{ The stack, as in Wirth's compilers: the value at depth d is held in |
| 190 | + register d, for as many registers as there are. A value deeper than |
| 191 | + that, when its register is wanted, and every value in a register |
| 192 | + when a call would clobber them, is spilled to a frame slot, and loaded |
| 193 | + again where it is used. A constant is only noted. } |
| 194 | + |
| 195 | +function RegOf(i: longint): longint; |
| 196 | +begin |
| 197 | + RegOf := Regs[i mod NumRegs]; |
| 198 | +end; |
| 199 | + |
| 200 | +{ The value i, in a register, goes to a frame slot } |
| 201 | +procedure Spill(i: longint); |
| 202 | +var s: int64; |
| 203 | +begin |
| 204 | + s := AllocTemp(8, 8); |
| 205 | + Store(Mem(RBP, s), RegOf(i), 8); |
| 206 | + stack[i].loc := lFrame; |
| 207 | + stack[i].val := s; |
| 208 | + owner[i mod NumRegs] := -1; |
| 209 | +end; |
| 180 | 210 | |
| 181 | | -{ rax is wanted for a new value: the one in it is pushed } |
| 182 | | -procedure Spill; |
| 211 | +{ Every value below depth d in a register, spilled } |
| 212 | +procedure SpillBelow(d: longint); |
| 213 | +var r: longint; |
| 183 | 214 | begin |
| 184 | | - if inRax >= 0 then begin |
| 185 | | - Push(RAX); |
| 186 | | - inc(pushed); |
| 187 | | - inRax := -1; |
| 215 | + for r := 0 to NumRegs - 1 do |
| 216 | + if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]); |
| 217 | +end; |
| 218 | + |
| 219 | +{ The register of value i, for it to hold: a deeper value in it is |
| 220 | + spilled. } |
| 221 | +function Claim(i: longint): longint; |
| 222 | +var r: longint; |
| 223 | +begin |
| 224 | + r := i mod NumRegs; |
| 225 | + if (owner[r] >= 0) and (owner[r] <> i) then Spill(owner[r]); |
| 226 | + owner[r] := i; |
| 227 | + Claim := Regs[r]; |
| 228 | +end; |
| 229 | + |
| 230 | +{ The value i, in its register } |
| 231 | +function Loaded(i: longint): longint; |
| 232 | +var r: longint; |
| 233 | +begin |
| 234 | + if stack[i].loc <> lReg then begin |
| 235 | + r := Claim(i); |
| 236 | + if stack[i].loc = lConst then MovRI(r, stack[i].val) |
| 237 | + else Load(r, Mem(RBP, stack[i].val), 8, false); |
| 238 | + stack[i].loc := lReg; |
| 188 | 239 | end; |
| 240 | + Loaded := RegOf(i); |
| 189 | 241 | end; |
| 190 | 242 | |
| 191 | | -procedure NewEntry(k: TKind; isConst: boolean; v: int64); |
| 243 | +function Top: longint; |
| 192 | 244 | begin |
| 245 | + if depth = 0 then Fatal('internal error: the stack is empty'); |
| 246 | + Top := Loaded(depth - 1); |
| 247 | +end; |
| 248 | + |
| 249 | +procedure NewEntry(k: TKind; loc: TLoc; v: int64); |
| 250 | +begin |
| 193 | 251 | if depth > High(stack) then Mark('the expression is too deep'); |
| 194 | 252 | stack[depth].k := k; |
| 195 | | - stack[depth].isConst := isConst; |
| 253 | + stack[depth].loc := loc; |
| 196 | 254 | stack[depth].val := v; |
| 197 | 255 | inc(depth); |
| 198 | 256 | end; |
| 199 | 257 | |
| 200 | | -{ The value just computed in rax goes on top } |
| 258 | +{ The register a new value on top is to be computed in } |
| 259 | +function Target: longint; |
| 260 | +begin |
| 261 | + Target := Claim(depth); |
| 262 | +end; |
| 263 | + |
| 264 | +{ The value just computed in Target goes on top } |
| 201 | 265 | procedure Computed(k: TKind); |
| 202 | 266 | begin |
| 203 | | - NewEntry(k, false, 0); |
| 204 | | - inRax := depth - 1; |
| 267 | + NewEntry(k, lReg, 0); |
| 205 | 268 | end; |
| 206 | 269 | |
| 207 | | -{ The value on top, popped into register r } |
| 208 | | -procedure PopTo(r: longint); |
| 209 | | -var e: TEntry; |
| 270 | +procedure Drop; |
| 210 | 271 | begin |
| 211 | | - if depth = 0 then Fatal('internal error: the stack is empty'); |
| 212 | 272 | dec(depth); |
| 213 | | - e := stack[depth]; |
| 214 | | - if e.isConst then begin |
| 215 | | - if r = RAX then Spill; |
| 216 | | - MovRI(r, e.val); |
| 217 | | - end else if inRax = depth then begin |
| 218 | | - if r <> RAX then MovRR(r, RAX); |
| 219 | | - inRax := -1; |
| 220 | | - end else begin |
| 221 | | - Pop(r); |
| 222 | | - dec(pushed); |
| 223 | | - end; |
| 273 | + if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1; |
| 224 | 274 | end; |
| 225 | 275 | |
| 226 | | -{ The value on top, into rax, and on top still } |
| 227 | | -procedure TopToRax; |
| 228 | | -var k: TKind; |
| 276 | +{ Whether the value on top is a constant, and if so, which } |
| 277 | +function TopConst(var v: int64): boolean; |
| 229 | 278 | begin |
| 230 | | - k := stack[depth - 1].k; |
| 231 | | - PopTo(RAX); |
| 232 | | - Computed(k); |
| 279 | + TopConst := stack[depth - 1].loc = lConst; |
| 280 | + v := stack[depth - 1].val; |
| 233 | 281 | end; |
| 234 | 282 | |
| 235 | 283 | procedure ResetStack(d, p: longint); |
| 284 | +var r: longint; |
| 236 | 285 | begin |
| 237 | 286 | depth := d; |
| 238 | 287 | pushed := p; |
| 239 | | - inRax := -1; |
| 288 | + for r := 0 to NumRegs - 1 do owner[r] := -1; |
| 240 | 289 | end; |
| 241 | 290 | |
| 242 | 291 | procedure PushConst(k: TKind; v: int64); |
| 243 | 292 | begin |
| 244 | | - NewEntry(k, true, v); |
| 293 | + NewEntry(k, lConst, v); |
| 245 | 294 | end; |
| 246 | 295 | |
| 247 | | -{ The memory a names: an address on the stack is popped into r, one in |
| 248 | | - a slot loaded into r } |
| 296 | +{ The memory a names. An address on the stack is popped, and is in its |
| 297 | + register still; one in a slot is loaded into r. } |
| 249 | 298 | function Place(const a: TAdr; r: longint): TMem; |
| 299 | +var ra: longint; |
| 250 | 300 | begin |
| 251 | 301 | case a.base of |
| 252 | 302 | bFrame: Place := Mem(RBP, a.off); |
| 253 | 303 | bGlobal: Place := MemRip(a.chunk, a.off); |
| 254 | | - bStack: begin PopTo(r); Place := Mem(r, a.off) end; |
| 304 | + bStack: begin ra := Top; Drop; Place := Mem(ra, a.off) end; |
| 255 | 305 | else |
| 256 | | - if r = RAX then Spill; |
| 257 | 306 | Load(r, Mem(RBP, a.chunk), 8, false); |
| 258 | 307 | Place := Mem(r, a.off); |
| 259 | 308 | end; |
| 260 | 309 | end; |
| 261 | 310 | |
| 262 | 311 | procedure PushAdr(const a: TAdr); |
| 263 | | -var m: TMem; |
| 312 | +var m: TMem; r: longint; |
| 264 | 313 | begin |
| 265 | 314 | if (a.base = bStack) and (a.off = 0) then exit; |
| 266 | | - m := Place(a, RAX); |
| 267 | | - Spill; |
| 268 | | - Lea(RAX, m); |
| 315 | + if a.base = bStack then begin m := Place(a, -1); r := Target end |
| 316 | + else begin r := Target; m := Place(a, r) end; |
| 317 | + Lea(r, m); |
| 269 | 318 | Computed(kWord); |
| 270 | 319 | end; |
| 271 | 320 | |
| 272 | 321 | procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind); |
| 273 | | -var m: TMem; |
| 322 | +var m: TMem; r: longint; |
| 274 | 323 | begin |
| 275 | | - m := Place(a, RAX); |
| 276 | | - Spill; |
| 277 | | - Load(RAX, m, size, signed); |
| 324 | + if a.base = bStack then begin m := Place(a, -1); r := Target end |
| 325 | + else begin r := Target; m := Place(a, r) end; |
| 326 | + Load(r, m, size, signed); |
| 278 | 327 | Computed(k); |
| 279 | 328 | end; |
| 280 | 329 | |
| 281 | 330 | procedure PushProc(c: longint; ext: boolean); |
| 331 | +var r: longint; |
| 282 | 332 | begin |
| 283 | | - Spill; |
| 284 | | - if ext then Load(RAX, MemRip(c, 0), 8, false) |
| 285 | | - else Lea(RAX, MemRip(c, 0)); |
| 333 | + r := Target; |
| 334 | + if ext then Load(r, MemRip(c, 0), 8, false) |
| 335 | + else Lea(r, MemRip(c, 0)); |
| 286 | 336 | Computed(kWord); |
| 287 | 337 | end; |
| 288 | 338 | |
| 289 | 339 | { Pops a value into the variable at a; an address on the stack is under |
| 290 | 340 | the value. } |
| 291 | 341 | procedure PopVar(const a: TAdr; size: longint); |
| 292 | | -var m: TMem; |
| 342 | +var r: longint; v: int64; |
| 293 | 343 | begin |
| 294 | | - if a.base in [bFrame, bGlobal] then begin |
| 295 | | - PopTo(RAX); |
| 296 | | - Store(Place(a, RAX), RAX, size); |
| 297 | | - end else begin |
| 298 | | - PopTo(RCX); |
| 299 | | - m := Place(a, RAX); |
| 300 | | - Store(m, RCX, size); |
| 301 | | - end; |
| 344 | + if TopConst(v) then begin r := RAX; MovRI(RAX, v) end |
| 345 | + else r := Top; |
| 346 | + Drop; |
| 347 | + Store(Place(a, RCX), r, size); |
| 302 | 348 | end; |
| 303 | 349 | |
| 304 | | -procedure Drop; |
| 305 | | -begin |
| 306 | | - dec(depth); |
| 307 | | - if stack[depth].isConst then exit; |
| 308 | | - if inRax = depth then inRax := -1 |
| 309 | | - else begin |
| 310 | | - AluI(aluAdd, RSP, 8); |
| 311 | | - dec(pushed); |
| 312 | | - end; |
| 313 | | -end; |
| 314 | | - |
| 315 | 350 | { The two values on top change places. } |
| 316 | 351 | procedure Swap; |
| 317 | | -var e: TEntry; a, b: boolean; |
| 352 | +var e: TEntry; x, y: longint; rx, ry: boolean; |
| 318 | 353 | begin |
| 319 | | - a := stack[depth - 2].isConst; |
| 320 | | - b := stack[depth - 1].isConst; |
| 321 | | - if not a and not b then begin |
| 322 | | - { the one on top is in rax, or pushed over the other } |
| 323 | | - if inRax <> depth - 1 then begin Pop(RAX); dec(pushed) end; |
| 324 | | - Xchg(Mem(RSP, 0), RAX); |
| 325 | | - inRax := depth - 1; |
| 326 | | - end else if inRax = depth - 1 then inRax := depth - 2 |
| 327 | | - else if inRax = depth - 2 then inRax := depth - 1; |
| 328 | | - e := stack[depth - 1]; |
| 329 | | - stack[depth - 1] := stack[depth - 2]; |
| 330 | | - stack[depth - 2] := e; |
| 354 | + x := depth - 2; y := depth - 1; |
| 355 | + rx := stack[x].loc = lReg; |
| 356 | + ry := stack[y].loc = lReg; |
| 357 | + if rx and ry then begin |
| 358 | + MovRR(RAX, RegOf(x)); |
| 359 | + MovRR(RegOf(x), RegOf(y)); |
| 360 | + MovRR(RegOf(y), RAX); |
| 361 | + end else if rx then begin |
| 362 | + owner[x mod NumRegs] := -1; |
| 363 | + MovRR(Claim(y), RegOf(x)); |
| 364 | + end else if ry then begin |
| 365 | + owner[y mod NumRegs] := -1; |
| 366 | + MovRR(Claim(x), RegOf(y)); |
| 367 | + end; |
| 368 | + e := stack[x]; stack[x] := stack[y]; stack[y] := e; |
| 331 | 369 | end; |
| 332 | 370 | |
| 333 | 371 | { Copying } |
| | @@ -358,8 +396,10 @@ |
| 358 | 396 | size bytes to off bytes past the destination. } |
| 359 | 397 | procedure CopyBlock(size, off: int64); |
| 360 | 398 | begin |
| 361 | | - PopTo(RSI); |
| 362 | | - PopTo(RDI); |
| 399 | + MovRR(RSI, Top); |
| 400 | + Drop; |
| 401 | + MovRR(RDI, Top); |
| 402 | + Drop; |
| 363 | 403 | if off <> 0 then Lea(RDI, Mem(RDI, off)); |
| 364 | 404 | CopyBytes(size); |
| 365 | 405 | end; |
| | @@ -367,7 +407,8 @@ |
| 367 | 407 | { Pops a source's address, and copies size bytes to a. } |
| 368 | 408 | procedure CopyTo(const a: TAdr; size: int64); |
| 369 | 409 | begin |
| 370 | | - PopTo(RSI); |
| 410 | + MovRR(RSI, Top); |
| 411 | + Drop; |
| 371 | 412 | Lea(RDI, Place(a, RDI)); |
| 372 | 413 | CopyBytes(size); |
| 373 | 414 | end; |
| | @@ -375,7 +416,6 @@ |
| 375 | 416 | procedure ZeroVar(const a: TAdr; size: int64); |
| 376 | 417 | begin |
| 377 | 418 | Lea(RDI, Place(a, RDI)); |
| 378 | | - Spill; |
| 379 | 419 | MovRI(RCX, size); |
| 380 | 420 | MovRI(RAX, 0); |
| 381 | 421 | RepStosb; |
| | @@ -392,39 +432,45 @@ |
| 392 | 432 | Log2 := n; |
| 393 | 433 | end; |
| 394 | 434 | |
| 395 | | -{ rax div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards |
| 435 | +function Fits32(v: int64): boolean; |
| 436 | +begin |
| 437 | + Fits32 := (v >= low(longint)) and (v <= high(longint)); |
| 438 | +end; |
| 439 | + |
| 440 | +{ r compared with v, through rdx if v needs more than 32 bits } |
| 441 | +procedure CmpI(r: longint; v: int64); |
| 442 | +begin |
| 443 | + if Fits32(v) then AluI(aluCmp, r, v) |
| 444 | + else begin MovRI(RDX, v); Alu(aluCmp, r, RDX) end; |
| 445 | +end; |
| 446 | + |
| 447 | +{ r div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards |
| 396 | 448 | zero, so a negative dividend first gets 2^k - 1 added (the bias). } |
| 397 | | -procedure DivPow2(op: TSym; k: longint; unsign: boolean); |
| 449 | +procedure DivPow2(op: TSym; r, k: longint; unsign: boolean); |
| 398 | 450 | begin |
| 399 | 451 | if unsign then begin |
| 400 | | - if op = sDiv then ShiftI(shShr, RAX, k) |
| 401 | | - else begin ShiftI(shShl, RAX, 64 - k); ShiftI(shShr, RAX, 64 - k) end; |
| 452 | + if op = sDiv then ShiftI(shShr, r, k) |
| 453 | + else begin ShiftI(shShl, r, 64 - k); ShiftI(shShr, r, 64 - k) end; |
| 402 | 454 | exit; |
| 403 | 455 | end; |
| 404 | | - MovRR(RCX, RAX); |
| 456 | + MovRR(RCX, r); |
| 405 | 457 | ShiftI(shSar, RCX, 63); |
| 406 | 458 | ShiftI(shShr, RCX, 64 - k); |
| 407 | | - Alu(aluAdd, RCX, RAX); |
| 459 | + Alu(aluAdd, RCX, r); |
| 408 | 460 | ShiftI(shSar, RCX, k); |
| 409 | | - if op = sDiv then MovRR(RAX, RCX) |
| 410 | | - else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end; |
| 411 | | -end; |
| 412 | | - |
| 413 | | -procedure CmpI(r: longint; v: int64); |
| 414 | | -begin |
| 415 | | - if (v >= low(longint)) and (v <= high(longint)) then AluI(aluCmp, r, v) |
| 416 | | - else begin MovRI(R11, v); Alu(aluCmp, r, R11) end; |
| 461 | + if op = sDiv then MovRR(r, RCX) |
| 462 | + else begin ShiftI(shShl, RCX, k); Alu(aluSub, r, RCX) end; |
| 417 | 463 | end; |
| 418 | 464 | |
| 419 | | -{ Before rax div rcx, of a signed type of bits < 64: its smallest value |
| 465 | +{ Before rax div y, of a signed type of bits < 64: its smallest value |
| 420 | 466 | divided by -1 overflows it, which must stop the program as it does for |
| 421 | 467 | an I64 (report section 6.1). idiv on the 64-bit values wouldn't fault, |
| 422 | 468 | so then rax is made the smallest I64, and it does. } |
| 423 | | -procedure DivTrap(bits: longint); |
| 469 | +procedure DivTrap(bits, y: longint); |
| 424 | 470 | var L: longint; |
| 425 | 471 | begin |
| 426 | 472 | L := NoLabel; |
| 427 | | - CmpI(RCX, -1); |
| 473 | + CmpI(y, -1); |
| 428 | 474 | Jcc(ccNE, L); |
| 429 | 475 | CmpI(RAX, int64(-1) shl (bits - 1)); |
| 430 | 476 | Jcc(ccNE, L); |
| | @@ -432,69 +478,93 @@ |
| 432 | 478 | FixLink(L); |
| 433 | 479 | end; |
| 434 | 480 | |
| 435 | | -{ The two values on top, the left one into rax and the right into rcx } |
| 436 | | -procedure Operands; |
| 481 | +{ The right operand of the two values on top, popped, in register y: |
| 482 | + its own, or rcx for a constant; the left one stays, in its register, |
| 483 | + which is returned. } |
| 484 | +function Operands(var y: longint): longint; |
| 485 | +var v: int64; |
| 437 | 486 | begin |
| 438 | | - PopTo(RCX); |
| 439 | | - PopTo(RAX); |
| 487 | + if TopConst(v) then begin MovRI(RCX, v); y := RCX end else y := Top; |
| 488 | + Drop; |
| 489 | + Operands := Top; |
| 440 | 490 | end; |
| 441 | 491 | |
| 442 | 492 | procedure RealOp(op: TSym; k: TKind); |
| 443 | | -var f: boolean; |
| 493 | +var f: boolean; r, y: longint; |
| 444 | 494 | begin |
| 445 | 495 | f := k = kF32; |
| 446 | | - Operands; |
| 447 | | - MovqXR(0, RAX); |
| 448 | | - MovqXR(1, RCX); |
| 496 | + r := Operands(y); |
| 497 | + MovqXR(0, r); |
| 498 | + MovqXR(1, y); |
| 449 | 499 | case op of |
| 450 | 500 | sPlus: Sse($58, 0, 1, f); |
| 451 | 501 | sMinus: Sse($5C, 0, 1, f); |
| 452 | 502 | sTimes: Sse($59, 0, 1, f); |
| 453 | 503 | sSlash: Sse($5E, 0, 1, f); |
| 454 | 504 | end; |
| 455 | | - MovqRX(RAX, 0); |
| 456 | | - Computed(k); |
| 505 | + MovqRX(r, 0); |
| 457 | 506 | end; |
| 458 | 507 | |
| 508 | +{ The group 1 operation of +, -, &, | and xor } |
| 509 | +function AluOf(op: TSym): longint; |
| 510 | +begin |
| 511 | + case op of |
| 512 | + sPlus: AluOf := aluAdd; |
| 513 | + sMinus: AluOf := aluSub; |
| 514 | + sAmp: AluOf := aluAnd; |
| 515 | + sBar: AluOf := aluOr; |
| 516 | + else AluOf := aluXor; |
| 517 | + end; |
| 518 | +end; |
| 519 | + |
| 459 | 520 | { The two values on top, the left op the right, an integer of size |
| 460 | 521 | bytes, signed or not, brought back to its width. } |
| 461 | 522 | procedure Op(op: TSym; k: TKind; size: longint; signed: boolean); |
| 462 | | -var y: TEntry; sh, bits: longint; |
| 523 | +var v: int64; r, y, sh, bits: longint; c: boolean; |
| 463 | 524 | begin |
| 464 | 525 | if k in [kF32, kF64] then begin RealOp(op, k); exit end; |
| 465 | 526 | bits := size * 8; |
| 466 | | - y := stack[depth - 1]; |
| 527 | + c := TopConst(v); |
| 467 | 528 | sh := -1; |
| 468 | | - if y.isConst and (op in [sTimes, sDiv, sMod]) then sh := Log2(y.val); |
| 469 | | - if y.isConst then begin |
| 470 | | - dec(depth); |
| 471 | | - PopTo(RAX); |
| 472 | | - if sh < 1 then MovRI(RCX, y.val); |
| 473 | | - end else Operands; |
| 474 | | - case op of |
| 475 | | - sPlus: Alu(aluAdd, RAX, RCX); |
| 476 | | - sMinus: Alu(aluSub, RAX, RCX); |
| 477 | | - sTimes: if sh >= 1 then ShiftI(shShl, RAX, sh) else Imul(RAX, RCX); |
| 478 | | - sDiv, sMod: if sh >= 1 then DivPow2(op, sh, not signed) else begin |
| 479 | | - if not signed then begin MovRI(RDX, 0); Unary(6, RCX) end |
| 480 | | - else begin |
| 481 | | - if (bits < 64) and (not y.isConst or (y.val = -1)) then DivTrap(bits); |
| 482 | | - Cqo; Unary(7, RCX) |
| 529 | + if c and (op in [sTimes, sDiv, sMod]) then sh := Log2(v); |
| 530 | + if c and (sh >= 1) then begin |
| 531 | + Drop; |
| 532 | + r := Top; |
| 533 | + if op = sTimes then ShiftI(shShl, r, sh) else DivPow2(op, r, sh, not signed); |
| 534 | + end else if c and Fits32(v) and (op in [sPlus, sMinus, sBar, sXor, sAmp, sTimes, sShl, sShr]) then begin |
| 535 | + Drop; |
| 536 | + r := Top; |
| 537 | + case op of |
| 538 | + sTimes: ImulI(r, v); |
| 539 | + sShl: ShiftI(shShl, r, v and (bits - 1)); |
| 540 | + sShr: if signed then ShiftI(shSar, r, v and (bits - 1)) else ShiftI(shShr, r, v and (bits - 1)); |
| 541 | + else AluI(AluOf(op), r, v); |
| 542 | + end; |
| 543 | + end else begin |
| 544 | + r := Operands(y); |
| 545 | + case op of |
| 546 | + sPlus, sMinus, sAmp, sBar, sXor: Alu(AluOf(op), r, y); |
| 547 | + sTimes: Imul(r, y); |
| 548 | + sDiv, sMod: begin |
| 549 | + MovRR(RAX, r); |
| 550 | + if not signed then begin MovRI(RDX, 0); Unary(6, y) end |
| 551 | + else begin |
| 552 | + if (bits < 64) and (not c or (v = -1)) then DivTrap(bits, y); |
| 553 | + Cqo; Unary(7, y) |
| 554 | + end; |
| 555 | + if op = sDiv then MovRR(r, RAX) else MovRR(r, RDX); |
| 483 | 556 | end; |
| 484 | | - if op = sMod then MovRR(RAX, RDX); |
| 557 | + sShl, sShr: begin |
| 558 | + { the count modulo the width: the machine takes it modulo 64 } |
| 559 | + if y <> RCX then MovRR(RCX, y); |
| 560 | + if bits < 64 then AluI(aluAnd, RCX, bits - 1); |
| 561 | + if op = sShl then Shift(shShl, r) |
| 562 | + else if signed then Shift(shSar, r) else Shift(shShr, r); |
| 563 | + end; |
| 485 | 564 | end; |
| 486 | | - sAmp: Alu(aluAnd, RAX, RCX); |
| 487 | | - sBar: Alu(aluOr, RAX, RCX); |
| 488 | | - sXor: Alu(aluXor, RAX, RCX); |
| 489 | | - sShl, sShr: begin |
| 490 | | - { the count modulo the width: the machine takes it modulo 64 } |
| 491 | | - if bits < 64 then AluI(aluAnd, RCX, bits - 1); |
| 492 | | - if op = sShl then Shift(shShl, RAX) |
| 493 | | - else if signed then Shift(shSar, RAX) else Shift(shShr, RAX); |
| 494 | | - end; |
| 495 | 565 | end; |
| 496 | | - if bits < 64 then Extend(RAX, size, signed); |
| 497 | | - Computed(k); |
| 566 | + if bits < 64 then Extend(r, size, signed); |
| 567 | + stack[depth - 1].k := k; |
| 498 | 568 | end; |
| 499 | 569 | |
| 500 | 570 | { The left op the right of the two values on top, as a BOOL } |
| | @@ -502,125 +572,119 @@ |
| 502 | 572 | const |
| 503 | 573 | CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); |
| 504 | 574 | CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); |
| 505 | | -var y: TEntry; f: boolean; |
| 575 | +var f: boolean; r, y: longint; v: int64; |
| 506 | 576 | begin |
| 507 | 577 | if k in [kF32, kF64] then begin |
| 508 | 578 | f := k = kF32; |
| 509 | | - Operands; |
| 510 | | - MovqXR(0, RAX); |
| 511 | | - MovqXR(1, RCX); |
| 579 | + r := Operands(y); |
| 580 | + MovqXR(0, r); |
| 581 | + MovqXR(1, y); |
| 512 | 582 | case op of |
| 513 | | - sEql: begin Ucomis(0, 1, f); SetCC(ccE, RAX); SetCC(ccNP, RCX); Alu(aluAnd, RAX, RCX) end; |
| 514 | | - sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, RAX); SetCC(ccP, RCX); Alu(aluOr, RAX, RCX) end; |
| 515 | | - sLss: begin Ucomis(1, 0, f); SetCC(ccA, RAX) end; |
| 516 | | - sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, RAX) end; |
| 517 | | - sGtr: begin Ucomis(0, 1, f); SetCC(ccA, RAX) end; |
| 518 | | - sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, RAX) end; |
| 583 | + sEql: begin Ucomis(0, 1, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end; |
| 584 | + sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end; |
| 585 | + sLss: begin Ucomis(1, 0, f); SetCC(ccA, r) end; |
| 586 | + sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, r) end; |
| 587 | + sGtr: begin Ucomis(0, 1, f); SetCC(ccA, r) end; |
| 588 | + sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, r) end; |
| 519 | 589 | end; |
| 520 | 590 | end else begin |
| 521 | | - y := stack[depth - 1]; |
| 522 | | - if y.isConst then begin |
| 523 | | - dec(depth); |
| 524 | | - PopTo(RAX); |
| 525 | | - CmpI(RAX, y.val); |
| 526 | | - end else begin |
| 527 | | - Operands; |
| 528 | | - Alu(aluCmp, RAX, RCX); |
| 529 | | - end; |
| 530 | | - if signed then SetCC(CcSigned[op], RAX) else SetCC(CcUnsigned[op], RAX); |
| 591 | + if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end |
| 592 | + else begin r := Operands(y); Alu(aluCmp, r, y) end; |
| 593 | + if signed then SetCC(CcSigned[op], r) else SetCC(CcUnsigned[op], r); |
| 531 | 594 | end; |
| 532 | | - Computed(kWord); |
| 595 | + stack[depth - 1].k := kWord; |
| 533 | 596 | end; |
| 534 | 597 | |
| 535 | 598 | procedure Neg(k: TKind; size: longint; signed: boolean); |
| 599 | +var r: longint; |
| 536 | 600 | begin |
| 537 | | - PopTo(RAX); |
| 601 | + r := Top; |
| 538 | 602 | case k of |
| 539 | | - kF32: Btc(RAX, 31); |
| 540 | | - kF64: Btc(RAX, 63); |
| 603 | + kF32: Btc(r, 31); |
| 604 | + kF64: Btc(r, 63); |
| 541 | 605 | else |
| 542 | | - Unary(3, RAX); |
| 543 | | - if size < 8 then Extend(RAX, size, signed); |
| 606 | + Unary(3, r); |
| 607 | + if size < 8 then Extend(r, size, signed); |
| 544 | 608 | end; |
| 545 | | - Computed(k); |
| 546 | 609 | end; |
| 547 | 610 | |
| 548 | 611 | procedure Complement(size: longint; signed: boolean); |
| 612 | +var r: longint; |
| 549 | 613 | begin |
| 550 | | - PopTo(RAX); |
| 551 | | - Unary(2, RAX); |
| 552 | | - if size < 8 then Extend(RAX, size, signed); |
| 553 | | - Computed(kWord); |
| 614 | + r := Top; |
| 615 | + Unary(2, r); |
| 616 | + if size < 8 then Extend(r, size, signed); |
| 554 | 617 | end; |
| 555 | 618 | |
| 556 | 619 | procedure NotBool; |
| 557 | 620 | begin |
| 558 | | - PopTo(RAX); |
| 559 | | - AluI(aluXor, RAX, 1); |
| 560 | | - Computed(kWord); |
| 621 | + AluI(aluXor, Top, 1); |
| 561 | 622 | end; |
| 562 | 623 | |
| 563 | 624 | { The integer on top, brought to size bytes } |
| 564 | 625 | procedure Narrow(size: longint; signed: boolean); |
| 626 | +var r: longint; |
| 565 | 627 | begin |
| 566 | | - PopTo(RAX); |
| 567 | | - if size < 8 then Extend(RAX, size, signed); |
| 568 | | - Computed(kWord); |
| 628 | + r := Top; |
| 629 | + if size < 8 then Extend(r, size, signed); |
| 630 | + stack[depth - 1].k := kWord; |
| 569 | 631 | end; |
| 570 | 632 | |
| 571 | 633 | { The integer on top, as a real of kind k; big if it may be a U64 above |
| 572 | 634 | high(I64), which the machine would take as negative } |
| 573 | 635 | procedure IntToReal(k: TKind; big: boolean); |
| 574 | | -var L1, L2: longint; f: boolean; |
| 636 | +var L1, L2, r: longint; f: boolean; |
| 575 | 637 | begin |
| 576 | 638 | f := k = kF32; |
| 577 | | - PopTo(RAX); |
| 639 | + r := Top; |
| 578 | 640 | if big then begin |
| 579 | 641 | { above high(I64): halve, keeping the low bit, and double } |
| 580 | 642 | L1 := NoLabel; L2 := NoLabel; |
| 581 | | - TestRR(RAX, RAX); |
| 643 | + TestRR(r, r); |
| 582 | 644 | Jcc(ccS, L1); |
| 583 | | - Cvtsi2s(0, RAX, f); |
| 645 | + Cvtsi2s(0, r, f); |
| 584 | 646 | Jmp(L2); |
| 585 | 647 | FixLink(L1); |
| 586 | | - MovRR(RCX, RAX); |
| 648 | + MovRR(RCX, r); |
| 587 | 649 | ShiftI(shShr, RCX, 1); |
| 588 | | - AluI(aluAnd, RAX, 1); |
| 589 | | - Alu(aluOr, RCX, RAX); |
| 650 | + AluI(aluAnd, r, 1); |
| 651 | + Alu(aluOr, RCX, r); |
| 590 | 652 | Cvtsi2s(0, RCX, f); |
| 591 | 653 | Sse($58, 0, 0, f); |
| 592 | 654 | FixLink(L2); |
| 593 | | - end else Cvtsi2s(0, RAX, f); |
| 594 | | - MovqRX(RAX, 0); |
| 595 | | - Computed(k); |
| 655 | + end else Cvtsi2s(0, r, f); |
| 656 | + MovqRX(r, 0); |
| 657 | + stack[depth - 1].k := k; |
| 596 | 658 | end; |
| 597 | 659 | |
| 598 | 660 | { The real of kind k on top, truncated to an integer } |
| 599 | 661 | procedure RealToInt(k: TKind); |
| 662 | +var r: longint; |
| 600 | 663 | begin |
| 601 | | - PopTo(RAX); |
| 602 | | - MovqXR(0, RAX); |
| 603 | | - Cvtts2si(RAX, 0, k = kF32); |
| 604 | | - Computed(kWord); |
| 664 | + r := Top; |
| 665 | + MovqXR(0, r); |
| 666 | + Cvtts2si(r, 0, k = kF32); |
| 667 | + stack[depth - 1].k := kWord; |
| 605 | 668 | end; |
| 606 | 669 | |
| 607 | 670 | { The real of kind k on top, as the other kind } |
| 608 | 671 | procedure RealToReal(k: TKind); |
| 672 | +var r: longint; |
| 609 | 673 | begin |
| 610 | | - PopTo(RAX); |
| 611 | | - MovqXR(0, RAX); |
| 674 | + r := Top; |
| 675 | + MovqXR(0, r); |
| 612 | 676 | Sse($5A, 0, 0, k = kF32); |
| 613 | | - MovqRX(RAX, 0); |
| 614 | | - if k = kF32 then Computed(kF64) else Computed(kF32); |
| 677 | + MovqRX(r, 0); |
| 678 | + if k = kF32 then stack[depth - 1].k := kF64 else stack[depth - 1].k := kF32; |
| 615 | 679 | end; |
| 616 | 680 | |
| 617 | 681 | { Whether the slices in the frame at a and b are equal (or not, if not |
| 618 | 682 | eq): the lengths, then the bytes, len shl shift of them, compared |
| 619 | 683 | right where the comparison stands. } |
| 620 | 684 | procedure CompareSeq(a, b: int64; shift: longint; eq: boolean); |
| 621 | | -var differ: longint; |
| 685 | +var differ, r: longint; |
| 622 | 686 | begin |
| 623 | | - Spill; |
| 687 | + r := Target; |
| 624 | 688 | Load(RCX, Mem(RBP, b + 8), 8, false); |
| 625 | 689 | AluM(aluCmp, RCX, Mem(RBP, a + 8)); |
| 626 | 690 | differ := NoLabel; |
| | @@ -631,7 +695,7 @@ |
| 631 | 695 | Alu(aluCmp, RCX, RCX); { equal, if there are no bytes } |
| 632 | 696 | RepeCmpsb; |
| 633 | 697 | FixLink(differ); |
| 634 | | - if eq then SetCC(ccE, RAX) else SetCC(ccNE, RAX); |
| 698 | + if eq then SetCC(ccE, r) else SetCC(ccNE, r); |
| 635 | 699 | Computed(kWord); |
| 636 | 700 | end; |
| 637 | 701 | |
| | @@ -640,7 +704,8 @@ |
| 640 | 704 | procedure ErrorText(tmp: int64); |
| 641 | 705 | var L: longint; |
| 642 | 706 | begin |
| 643 | | - PopTo(RAX); |
| 707 | + MovRR(RAX, Top); |
| 708 | + Drop; |
| 644 | 709 | MovRI(RCX, 0); |
| 645 | 710 | L := NoLabel; |
| 646 | 711 | TestRR(RAX, RAX); |
| | @@ -655,54 +720,61 @@ |
| 655 | 720 | { Pops a step and an integer variable's address, and adds the step to |
| 656 | 721 | it, or takes it away. } |
| 657 | 722 | procedure IncDec(size: longint; signed, dec: boolean); |
| 723 | +var r, y: longint; |
| 658 | 724 | begin |
| 659 | | - PopTo(RCX); |
| 660 | | - PopTo(RDX); |
| 661 | | - Spill; |
| 662 | | - Load(RAX, Mem(RDX, 0), size, signed); |
| 663 | | - if dec then Alu(aluSub, RAX, RCX) else Alu(aluAdd, RAX, RCX); |
| 664 | | - Store(Mem(RDX, 0), RAX, size); |
| 725 | + r := Operands(y); |
| 726 | + Drop; |
| 727 | + Load(RAX, Mem(r, 0), size, signed); |
| 728 | + if dec then Alu(aluSub, RAX, y) else Alu(aluAdd, RAX, y); |
| 729 | + Store(Mem(r, 0), RAX, size); |
| 665 | 730 | end; |
| 666 | 731 | |
| 667 | 732 | { The pointer on top, which NEW got, to size zero bytes unless nil } |
| 668 | 733 | procedure ZeroNew(size: int64); |
| 669 | | -var L: longint; |
| 734 | +var L, r: longint; |
| 670 | 735 | begin |
| 671 | | - PopTo(RAX); |
| 736 | + r := Top; |
| 672 | 737 | L := NoLabel; |
| 673 | | - TestRR(RAX, RAX); |
| 738 | + TestRR(r, r); |
| 674 | 739 | Jcc(ccE, L); |
| 675 | | - MovRR(RDX, RAX); |
| 676 | | - MovRR(RDI, RAX); |
| 740 | + MovRR(RDI, r); |
| 677 | 741 | MovRI(RCX, size); |
| 678 | 742 | MovRI(RAX, 0); |
| 679 | 743 | RepStosb; |
| 680 | | - MovRR(RAX, RDX); |
| 681 | 744 | FixLink(L); |
| 682 | | - Computed(kWord); |
| 683 | 745 | end; |
| 684 | 746 | |
| 685 | | -{ CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack } |
| 747 | +{ CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack: the |
| 748 | + result where p was } |
| 686 | 749 | procedure Atomic(which: longint); |
| 750 | +var r, n: longint; |
| 687 | 751 | begin |
| 688 | | - if which = bCas then PopTo(RDX); |
| 689 | | - PopTo(RAX); |
| 690 | | - PopTo(RCX); |
| 752 | + if which = bCas then begin n := Top; Drop end; |
| 753 | + MovRR(RAX, Top); |
| 754 | + Drop; |
| 755 | + r := Top; |
| 691 | 756 | case which of |
| 692 | | - bCas: begin LockCmpxchg(Mem(RCX, 0), RDX); SetCC(ccE, RAX) end; |
| 693 | | - bXchg: Xchg(Mem(RCX, 0), RAX); |
| 694 | | - bXadd: LockXadd(Mem(RCX, 0), RAX); |
| 757 | + bCas: begin LockCmpxchg(Mem(r, 0), n); SetCC(ccE, r) end; |
| 758 | + bXchg: begin Xchg(Mem(r, 0), RAX); MovRR(r, RAX) end; |
| 759 | + bXadd: begin LockXadd(Mem(r, 0), RAX); MovRR(r, RAX) end; |
| 695 | 760 | end; |
| 696 | | - Computed(kWord); |
| 761 | + stack[depth - 1].k := kWord; |
| 697 | 762 | end; |
| 698 | 763 | |
| 699 | | -{ The system call and its arguments, n values on the stack } |
| 764 | +{ The system call and its arguments, n values on the stack; every value |
| 765 | + in a register is spilled first, as the arguments go in its registers } |
| 700 | 766 | procedure SysCall(n: longint); |
| 701 | | -const Regs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); |
| 767 | +const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); |
| 702 | 768 | var i: longint; |
| 703 | 769 | begin |
| 704 | | - for i := n - 1 downto 0 do PopTo(Regs[i]); |
| 770 | + SpillBelow(depth); |
| 771 | + for i := 0 to n - 1 do |
| 772 | + with stack[depth - n + i] do |
| 773 | + if loc = lConst then MovRI(SysRegs[i], val) |
| 774 | + else Load(SysRegs[i], Mem(RBP, val), 8, false); |
| 775 | + for i := 1 to n do Drop; |
| 705 | 776 | x64.Syscall; |
| 777 | + MovRR(Target, RAX); |
| 706 | 778 | Computed(kWord); |
| 707 | 779 | end; |
| 708 | 780 | |
| | @@ -720,23 +792,27 @@ |
| 720 | 792 | |
| 721 | 793 | { Pops a BOOL, and jumps if it is when } |
| 722 | 794 | procedure JumpIf(var L: longint; when: boolean); |
| 795 | +var r: longint; |
| 723 | 796 | begin |
| 724 | | - PopTo(RAX); |
| 725 | | - TestRR(RAX, RAX); |
| 797 | + r := Top; |
| 798 | + Drop; |
| 799 | + TestRR(r, r); |
| 726 | 800 | if when then Jcc(ccNE, L) else Jcc(ccE, L); |
| 727 | 801 | end; |
| 728 | 802 | |
| 729 | | -{ For and and or: jumps if the BOOL on top is when, keeping it for |
| 730 | | - where the jump goes, and drops it if not. } |
| 803 | +{ For and and or: jumps if the BOOL on top is when, keeping it, in its |
| 804 | + register, for where the jump goes; drops it if not. The values under |
| 805 | + it are spilled first, so that both ways find them in the same place. } |
| 731 | 806 | procedure JumpKeep(var L: longint; when: boolean); |
| 732 | 807 | begin |
| 808 | + SpillBelow(depth - 1); |
| 733 | 809 | JumpIf(L, when); |
| 734 | 810 | end; |
| 735 | 811 | |
| 736 | 812 | { Where JumpKeep went: the BOOL on top is there, as it was there. } |
| 737 | 813 | procedure FixKeep(L: longint); |
| 738 | 814 | begin |
| 739 | | - TopToRax; |
| 815 | + Top; |
| 740 | 816 | FixLink(L); |
| 741 | 817 | end; |
| 742 | 818 | |
| | @@ -745,17 +821,11 @@ |
| 745 | 821 | const |
| 746 | 822 | CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); |
| 747 | 823 | CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); |
| 748 | | -var y: TEntry; |
| 824 | +var r, y: longint; v: int64; |
| 749 | 825 | begin |
| 750 | | - y := stack[depth - 1]; |
| 751 | | - if y.isConst then begin |
| 752 | | - dec(depth); |
| 753 | | - PopTo(RAX); |
| 754 | | - CmpI(RAX, y.val); |
| 755 | | - end else begin |
| 756 | | - Operands; |
| 757 | | - Alu(aluCmp, RAX, RCX); |
| 758 | | - end; |
| 826 | + if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end |
| 827 | + else begin r := Operands(y); Alu(aluCmp, r, y) end; |
| 828 | + Drop; |
| 759 | 829 | if signed then Jcc(CcSigned[op], L) else Jcc(CcUnsigned[op], L); |
| 760 | 830 | end; |
| 761 | 831 | |
| | @@ -786,7 +856,8 @@ |
| 786 | 856 | called name (2), or a slice of a pointer a..b that runs backwards |
| 787 | 857 | (3). a is in register ra, b in RDX; len is in memory at m, or n if |
| 788 | 858 | m.base is NoBase. The failing code has p for its place, and the code |
| 789 | | - after it the place before it again. } |
| 859 | + after it the place before it again; it never returns, and may use |
| 860 | + any register once it has read what it needs. } |
| 790 | 861 | procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint); |
| 791 | 862 | var L, line, col: longint; before: TLine; |
| 792 | 863 | begin |
| | @@ -815,18 +886,20 @@ |
| 815 | 886 | |
| 816 | 887 | { The index on top is below n, unsigned } |
| 817 | 888 | procedure CheckIndex(n: int64; const p: TPos); |
| 889 | +var r: longint; |
| 818 | 890 | begin |
| 819 | | - TopToRax; |
| 820 | | - CmpI(RAX, n); |
| 821 | | - Check(ccB, 0, p, RAX, NoMem, n, -1); |
| 891 | + r := Top; |
| 892 | + CmpI(r, n); |
| 893 | + Check(ccB, 0, p, r, NoMem, n, -1); |
| 822 | 894 | end; |
| 823 | 895 | |
| 824 | 896 | { The integer on top is a position of an enumeration of n names } |
| 825 | 897 | procedure CheckEnum(n: int64; name: longint; const p: TPos); |
| 898 | +var r: longint; |
| 826 | 899 | begin |
| 827 | | - TopToRax; |
| 828 | | - CmpI(RAX, n); |
| 829 | | - Check(ccB, 2, p, RAX, NoMem, n, name); |
| 900 | + r := Top; |
| 901 | + CmpI(r, n); |
| 902 | + Check(ccB, 2, p, r, NoMem, n, name); |
| 830 | 903 | end; |
| 831 | 904 | |
| 832 | 905 | { Pops an index, and the address of the slice at a if it is on the |
| | @@ -834,7 +907,8 @@ |
| 834 | 907 | procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); |
| 835 | 908 | var m, ml: TMem; |
| 836 | 909 | begin |
| 837 | | - PopTo(RAX); |
| 910 | + MovRR(RAX, Top); |
| 911 | + Drop; |
| 838 | 912 | m := Place(a, RCX); |
| 839 | 913 | ml := m; ml.disp := ml.disp + 8; |
| 840 | 914 | if checked then begin |
| | @@ -843,6 +917,7 @@ |
| 843 | 917 | end; |
| 844 | 918 | if size <> 1 then ImulI(RAX, size); |
| 845 | 919 | AluM(aluAdd, RAX, m); |
| 920 | + MovRR(Target, RAX); |
| 846 | 921 | Computed(kWord); |
| 847 | 922 | end; |
| 848 | 923 | |
| | @@ -852,9 +927,11 @@ |
| 852 | 927 | procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); |
| 853 | 928 | var m, ml: TMem; |
| 854 | 929 | begin |
| 855 | | - PopTo(RDX); |
| 856 | | - PopTo(RCX); |
| 857 | | - m := Place(a, R8); |
| 930 | + MovRR(RDX, Top); |
| 931 | + Drop; |
| 932 | + MovRR(RCX, Top); |
| 933 | + Drop; |
| 934 | + m := Place(a, RDI); |
| 858 | 935 | ml := m; ml.disp := ml.disp + 8; |
| 859 | 936 | if checked then begin |
| 860 | 937 | if isArray then begin CmpI(RDX, n); ml := NoMem end |
| | @@ -863,7 +940,6 @@ |
| 863 | 940 | Alu(aluCmp, RCX, RDX); |
| 864 | 941 | Check(ccBE, 1, p, RCX, ml, n, -1); |
| 865 | 942 | end; |
| 866 | | - Spill; |
| 867 | 943 | MovRR(RAX, RDX); |
| 868 | 944 | Alu(aluSub, RAX, RCX); |
| 869 | 945 | Store(Mem(RBP, tmp + 8), RAX, 8); |
| | @@ -877,31 +953,32 @@ |
| 877 | 953 | bounds can't be known, but it can't run backwards. } |
| 878 | 954 | procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); |
| 879 | 955 | begin |
| 880 | | - PopTo(RDX); |
| 881 | | - PopTo(RCX); |
| 882 | | - PopTo(R8); |
| 956 | + MovRR(RDX, Top); |
| 957 | + Drop; |
| 958 | + MovRR(RCX, Top); |
| 959 | + Drop; |
| 960 | + MovRR(RDI, Top); |
| 961 | + Drop; |
| 883 | 962 | if checked then begin |
| 884 | 963 | Alu(aluCmp, RCX, RDX); |
| 885 | 964 | Check(ccLE, 3, p, RCX, NoMem, 0, -1); |
| 886 | 965 | end; |
| 887 | | - Spill; |
| 888 | 966 | MovRR(RAX, RDX); |
| 889 | 967 | Alu(aluSub, RAX, RCX); |
| 890 | 968 | Store(Mem(RBP, tmp + 8), RAX, 8); |
| 891 | 969 | if size <> 1 then ImulI(RCX, size); |
| 892 | | - MovRR(RAX, R8); |
| 893 | | - Alu(aluAdd, RAX, RCX); |
| 894 | | - Store(Mem(RBP, tmp), RAX, 8); |
| 970 | + Alu(aluAdd, RCX, RDI); |
| 971 | + Store(Mem(RBP, tmp), RCX, 8); |
| 895 | 972 | end; |
| 896 | 973 | |
| 897 | 974 | { Calls } |
| 898 | 975 | |
| 899 | 976 | procedure Call(var c: TCall); |
| 900 | 977 | var |
| 901 | | - res: PType; i, ni, nf, k, pad: longint; |
| 978 | + res: PType; i, ni, nf, k, pad, r: longint; |
| 902 | 979 | onStack: array[0..63] of boolean; |
| 903 | 980 | begin |
| 904 | | - Spill; { the value in rax waits on the stack, past the call } |
| 981 | + SpillBelow(depth); { the registers in use, saved around the call } |
| 905 | 982 | res := c.sig^.base; |
| 906 | 983 | c.ret := 0; |
| 907 | 984 | ni := 0; nf := 0; |
| | @@ -944,9 +1021,13 @@ |
| 944 | 1021 | Store(Mem(RBP, c.ret + 8), RDX, 8); |
| 945 | 1022 | end; |
| 946 | 1023 | if (res <> nil) and (c.ret = 0) then begin |
| 947 | | - if res^.form = fReal then MovqRX(RAX, 0) |
| 948 | | - else if res^.form = fBool then Extend(RAX, 1, false) |
| 949 | | - else if (res^.form = fInt) and (res^.size < 8) then Extend(RAX, res^.size, res^.signed); |
| 1024 | + r := Target; |
| 1025 | + if res^.form = fReal then MovqRX(r, 0) |
| 1026 | + else begin |
| 1027 | + MovRR(r, RAX); |
| 1028 | + if res^.form = fBool then Extend(r, 1, false) |
| 1029 | + else if (res^.form = fInt) and (res^.size < 8) then Extend(r, res^.size, res^.signed); |
| 1030 | + end; |
| 950 | 1031 | Computed(KindOf(res)); |
| 951 | 1032 | end; |
| 952 | 1033 | end; |
| | @@ -1060,17 +1141,20 @@ |
| 1060 | 1141 | { Pops one of several results, a value of size bytes, into its place |
| 1061 | 1142 | off bytes into the caller's, behind the hidden pointer. } |
| 1062 | 1143 | procedure ResultTo(off: int64; size: longint); |
| 1144 | +var r: longint; |
| 1063 | 1145 | begin |
| 1064 | | - PopTo(RAX); |
| 1146 | + r := Top; |
| 1147 | + Drop; |
| 1065 | 1148 | Load(RCX, Mem(RBP, retSlot), 8, false); |
| 1066 | | - Store(Mem(RCX, off), RAX, size); |
| 1149 | + Store(Mem(RCX, off), r, size); |
| 1067 | 1150 | end; |
| 1068 | 1151 | |
| 1069 | 1152 | { Pops an address, and copies size bytes from it to the result, off |
| 1070 | 1153 | bytes into it. } |
| 1071 | 1154 | procedure CopyResult(off, size: int64); |
| 1072 | 1155 | begin |
| 1073 | | - PopTo(RSI); |
| 1156 | + MovRR(RSI, Top); |
| 1157 | + Drop; |
| 1074 | 1158 | Load(RDI, Mem(RBP, retSlot), 8, false); |
| 1075 | 1159 | if off <> 0 then Lea(RDI, Mem(RDI, off)); |
| 1076 | 1160 | CopyBytes(size); |
| | @@ -1083,8 +1167,8 @@ |
| 1083 | 1167 | if resType <> nil then |
| 1084 | 1168 | if IsAggregate(resType) then Load(RAX, Mem(RBP, retSlot), 8, false) |
| 1085 | 1169 | else begin |
| 1086 | | - PopTo(RAX); |
| 1087 | | - if resType^.form = fReal then MovqXR(0, RAX); |
| 1170 | + if resType^.form = fReal then MovqXR(0, Top) else MovRR(RAX, Top); |
| 1171 | + Drop; |
| 1088 | 1172 | end; |
| 1089 | 1173 | Jmp(exitL); |
| 1090 | 1174 | end; |
| | @@ -1197,7 +1281,7 @@ |
| 1197 | 1281 | end; |
| 1198 | 1282 | |
| 1199 | 1283 | initialization |
| 1200 | | - inRax := -1; |
| 1284 | + ResetStack(0, 0); |
| 1201 | 1285 | rtFail := NewChunk(secText, Intern('Rt', 2), Intern('fail', 4)); |
| 1202 | 1286 | argcChunk := NewChunk(secBss, -1, Intern('argc', 4)); |
| 1203 | 1287 | SetBss(argcChunk, 8, 8); |