An element of an array as the machine has it x[i], of an array of the module with i in a register, was a copy of i, a shift, a lea and an add before its load. Now an element of 1, 2, 4 or 8 bytes of an array in a chunk, its index a variable's register, is noted as the operand [base + i * 8], the array's address made in the value's own register; one in the frame is [rbp + i * 8 + off], whatever its index (IndexArray). CheckIndex compares a variable's register as it is. NBody 225 -> 147 ms (FPC -O2 85); rat 464 -> 463, within noise; the other kernels unchanged. 9,036 lines of code.
| @@ -29,7 +29,13 @@ | ||
| 29 | 29 | `movsd`, a constant read from read-only data, and an operation takes |
| 30 | 30 | its right operand from memory as it is. Jacobi 251 ms to 129, NBody |
| 31 | 31 | 375 to 225; rat within noise. |
| 32 | -3. **Real variables in xmm registers**, where that pays: measured, and | |
| 32 | +3. Done: **An element of an array as the machine has it.** NBody's | |
| 33 | + time went mostly to its addresses, not its reals: `x[i]`, of an | |
| 34 | + array of the module, was a copy, a shift, a `lea` and an add before | |
| 35 | + its load. Now an element of an array in the frame or a chunk is the | |
| 36 | + operand `[base + i * 8]`, as one of a slice in registers is. NBody | |
| 37 | + 225 ms to 147; rat within noise. | |
| 38 | +4. **Real variables in xmm registers**, where that pays: measured, and | |
| 33 | 39 | kept only if it does. |
| 34 | 40 | |
| 35 | 41 | ## Next, when the owner says |
| @@ -15,8 +15,8 @@ | ||
| 15 | 15 | | `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout | |
| 16 | 16 | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | |
| 17 | 17 | | `src/x64.pas` | 1,201 | 992 | The instruction encoder, jumps within a procedure, the assembler | |
| 18 | -| `src/gen.pas` | 2,093 | 1,522 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 19 | -| `src/item.pas` | 1,579 | 1,302 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | +| `src/gen.pas` | 2,121 | 1,542 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 19 | +| `src/item.pas` | 1,578 | 1,301 | Items: types, constants, variables, operators, conversions, calls | | |
| 20 | 20 | | `src/parse.pas` | 3,103 | 2,707 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 21 | 21 | | `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places | |
| 22 | 22 | | `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -489,6 +489,36 @@ | ||
| 489 | 489 | under it as it does the integers. Jacobi 251 ms to 129, NBody 375 |
| 490 | 490 | to 225; rat within noise. |
| 491 | 491 | |
| 492 | +2. **An element of an array as the machine has it.** That left | |
| 493 | + NBody's reals waiting on their addresses: `x[i]`, of an array of | |
| 494 | + the module, was worked out as an address, and the index copied | |
| 495 | + from its register to be scaled: | |
| 496 | + | |
| 497 | + ``` | |
| 498 | + mov r8, rbx ; i | |
| 499 | + shl r8, 3 | |
| 500 | + lea r9, [NBody.x] | |
| 501 | + add r8, r9 | |
| 502 | + movsd xmm8, [r8] | |
| 503 | + ``` | |
| 504 | + | |
| 505 | + Now an element of 1, 2, 4 or 8 bytes of an array in a chunk, with | |
| 506 | + its index in a variable's register, is noted as the operand that | |
| 507 | + reads it, its base the array's address, made in the value's own | |
| 508 | + register; one in the frame has `rbp` as its base, whatever its | |
| 509 | + index (`IndexArray`, as `IndexSlice` does for a slice in | |
| 510 | + registers): | |
| 511 | + | |
| 512 | + ``` | |
| 513 | + lea r8, [NBody.x] | |
| 514 | + movsd xmm8, [r8 + rbx * 8] | |
| 515 | + ``` | |
| 516 | + | |
| 517 | + A computed index in a chunk's array is added up as before, as it | |
| 518 | + would need a third register. `CheckIndex` compares a variable's | |
| 519 | + register as it is (`Peek`), not a copy. NBody 225 ms to 147; rat | |
| 520 | + within noise. | |
| 521 | + | |
| 492 | 522 | ## diag.pas: messages |
| 493 | 523 | |
| 494 | 524 | Every message the compiler prints is made here. A position, `TPos`, is |
| @@ -656,7 +686,9 @@ | ||
| 656 | 686 | `JumpOut` (`JumpCmp` from within an expression) and `CondValue` for |
| 657 | 687 | `and` and `or`, `Fix` where the label is, and `JoinJumps`; |
| 658 | 688 | - checks: `CheckIndex`, `CheckEnum`, and `IndexSlice`, `Slice` and |
| 659 | - `SlicePtr`, which check as they make their address or slice; | |
| 689 | + `SlicePtr`, which check as they make their address or slice; and | |
| 690 | + `IndexArray`, an element's address in an array in the frame or a | |
| 691 | + chunk, the index checked before; | |
| 660 | 692 | - calls (`Call`), frames (`BeginProc`, `EnterBody`, `ReturnJump`, |
| 661 | 693 | `EndProc`, the temporaries of `AllocTemp`), deferred statements |
| 662 | 694 | (`DeferBegin`, `DeferEnd`, `RunDefer`), the built-ins that are |
| @@ -169,6 +169,7 @@ | ||
| 169 | 169 | procedure CheckIndex(n: int64; const p: TPos); |
| 170 | 170 | procedure CheckEnum(n: int64; name: longint; const p: TPos); |
| 171 | 171 | procedure CheckNil(reach: int64; const p: TPos); |
| 172 | +procedure IndexArray(const a: TAdr; size: int64); | |
| 172 | 173 | procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); |
| 173 | 174 | procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); |
| 174 | 175 | procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); |
| @@ -1352,7 +1353,7 @@ | ||
| 1352 | 1353 | procedure CheckIndex(n: int64; const p: TPos); |
| 1353 | 1354 | var r: longint; |
| 1354 | 1355 | begin |
| 1355 | - r := Top; | |
| 1356 | + r := Peek(depth - 1); | |
| 1356 | 1357 | CmpI(r, n); |
| 1357 | 1358 | Check(ccB, 0, p, r, NoMem, n, -1); |
| 1358 | 1359 | end; |
| @@ -1424,6 +1425,32 @@ | ||
| 1424 | 1425 | if inRax then MovRR(Target, RAX) |
| 1425 | 1426 | else Claim(depth, kWord); { the index's register again, now for the address } |
| 1426 | 1427 | Computed(kWord); |
| 1428 | +end; | |
| 1429 | + | |
| 1430 | +{ Pops an index, checked, and makes the address of the element it | |
| 1431 | + picks of the array at a. An element of 1, 2, | |
| 1432 | + 4 or 8 bytes is only noted, as the operand [base + index * size] | |
| 1433 | + (lIdx): an array in the frame has rbp as its base, and one in a | |
| 1434 | + chunk, if the index is a variable's register, its address made in | |
| 1435 | + the value's own register. Any other is added up. } | |
| 1436 | +procedure IndexArray(const a: TAdr; size: int64); | |
| 1437 | +var r, b: longint; | |
| 1438 | +begin | |
| 1439 | + if (size in [1, 2, 4, 8]) and (a.base = bFrame) then begin | |
| 1440 | + r := Peek(depth - 1); | |
| 1441 | + Drop; | |
| 1442 | + NewMem(lIdx, MemIdx(RBP, r, size, a.off), kWord, 8, false); | |
| 1443 | + end else if (size in [1, 2, 4, 8]) and (a.base = bGlobal) and (stack[depth - 1].loc = lHeld) then begin | |
| 1444 | + r := stack[depth - 1].val; | |
| 1445 | + Drop; | |
| 1446 | + b := Target; | |
| 1447 | + Lea(b, MemRip(a.chunk, a.off)); | |
| 1448 | + NewMem(lIdx, MemIdx(b, r, size, 0), kWord, 8, false); | |
| 1449 | + end else begin | |
| 1450 | + MulI(Top, size); | |
| 1451 | + PushAdr(a); | |
| 1452 | + Op(sPlus, kWord, 8, false); | |
| 1453 | + end; | |
| 1427 | 1454 | end; |
| 1428 | 1455 | |
| 1429 | 1456 | { Pops lo and hi, and the address of the array or slice at a if it is |
| @@ -640,9 +640,8 @@ | ||
| 640 | 640 | end else if t^.form = fArray then begin |
| 641 | 641 | Value(y); |
| 642 | 642 | if checks then CheckIndex(t^.len, y.pos); |
| 643 | - ScaleBy(s); | |
| 644 | - if x.base <> bStack then begin PushAdr(AdrOf(x)); x.off := 0 end; | |
| 645 | - gen.Op(sPlus, kWord, 8, false); | |
| 643 | + if x.base = bStack then begin ScaleBy(s); gen.Op(sPlus, kWord, 8, false) end | |
| 644 | + else begin IndexArray(AdrOf(x), s); x.off := 0 end; | |
| 646 | 645 | x.base := bStack; |
| 647 | 646 | end else begin |
| 648 | 647 | Value(y); |