Inlining a procedure that only returns: a call of one whose body is return e, with no locals and a single value its result, is made in place (Parse.inlined): the arguments as for a call into temporaries, each once and in order, then e read again in its own module's scope with its checks; imported and generic procedures too, a recursion ending in a call; a failed check in e told at e's line in its file (Gen's check works out the place as it is made); KeptCheck and KeptNil call through a procedure value, the tutorial's Fail through a procedure with no result; word count 887 ms to 638, Grep 268 to 199, the compiler 3% fewer instructions; conform/run/Inline, test/run/InlineCheck
| @@ -399,3 +399,15 @@ | ||
| 399 | 399 | or another module's array, as `d`, says why it can't be changed, for |
| 400 | 400 | `COPY` too. `seqSlice` copies no slice that is a temporary already |
| 401 | 401 | (a slice just cut): nothing `s` or `x` does can change it. |
| 402 | +- A call of a procedure whose body is only `return e`, with no locals, | |
| 403 | + a single value its result and no `try` in `e`, is made in place | |
| 404 | + (`Parse.inlined`): the arguments read and converted as for a call, | |
| 405 | + each once and in order, into temporaries the parameters are named | |
| 406 | + as (a `var` or `const` one, a record or an array, by the address the | |
| 407 | + call would pass), then `e` read again in its own module's scope, | |
| 408 | + with that module's checks. Another module's procedure and a generic | |
| 409 | + instance's too; not one already being made in place, nor a call | |
| 410 | + through a procedure value. The procedure has no frame: it is in | |
| 411 | + neither the trap report's calls nor gdb's backtrace; a failed check | |
| 412 | + in `e` is told at `e`'s line, a signal at the caller's statement. | |
| 413 | + A test that wants its frame calls it through a procedure value. |
| @@ -0,0 +1,19 @@ | ||
| 1 | +sub 7 | |
| 2 | +order 103 | |
| 3 | +count 2 | |
| 4 | +nested 9 | |
| 5 | +count 4 | |
| 6 | +lower 3 | |
| 7 | +wide 400 | |
| 8 | +low 255 | |
| 9 | +add8 44 | |
| 10 | +half 1 | |
| 11 | +const 7 | |
| 12 | +literal 3 | |
| 13 | +var 8 | |
| 14 | +var after 8 | |
| 15 | +slice 98 | |
| 16 | +even 1 | |
| 17 | +value 7 | |
| 18 | +imported 202 | |
| 19 | +generic 42 |
| @@ -0,0 +1,130 @@ | ||
| 1 | +(* Calls of procedures whose body is only return e mean what any call | |
| 2 | + means: each argument evaluated once, left to right, and converted as | |
| 3 | + for a call; a var or const parameter the variable itself; the result | |
| 4 | + of the procedure's type. *) | |
| 5 | +module Inline; | |
| 6 | +import Out, Inl; | |
| 7 | + | |
| 8 | +type Pair = record a, b: INT; end; | |
| 9 | + | |
| 10 | +var trace: [16]INT; numTrace: INT; | |
| 11 | + | |
| 12 | +(* v, noted in trace: an argument with a side effect *) | |
| 13 | +proc note(v: INT): INT; | |
| 14 | +begin | |
| 15 | + trace[numTrace] := v; | |
| 16 | + INC(numTrace); | |
| 17 | + return v; | |
| 18 | +end; | |
| 19 | + | |
| 20 | +proc sub(a, b: INT): INT; | |
| 21 | +begin | |
| 22 | + return a - b; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +proc square(a: INT): INT; | |
| 26 | +begin | |
| 27 | + return a * a; | |
| 28 | +end; | |
| 29 | + | |
| 30 | +proc subSquares(a, b: INT): INT; | |
| 31 | +begin | |
| 32 | + return sub(square(a), square(b)); | |
| 33 | +end; | |
| 34 | + | |
| 35 | +proc isLower(c: U8): BOOL; | |
| 36 | +begin | |
| 37 | + return U8(c - 97) < 26; | |
| 38 | +end; | |
| 39 | + | |
| 40 | +proc wide(x: U8): INT; | |
| 41 | +begin | |
| 42 | + return x; | |
| 43 | +end; | |
| 44 | + | |
| 45 | +proc low(x: INT): U8; | |
| 46 | +begin | |
| 47 | + return U8(x & 255); | |
| 48 | +end; | |
| 49 | + | |
| 50 | +proc add8(a, b: U8): U8; | |
| 51 | +begin | |
| 52 | + return a + b; | |
| 53 | +end; | |
| 54 | + | |
| 55 | +proc half(r: F64): F64; | |
| 56 | +begin | |
| 57 | + return r / 2.0; | |
| 58 | +end; | |
| 59 | + | |
| 60 | +proc sumOf(const p: Pair): INT; | |
| 61 | +begin | |
| 62 | + return p.a + p.b; | |
| 63 | +end; | |
| 64 | + | |
| 65 | +proc incr(var n: INT): INT; | |
| 66 | +begin | |
| 67 | + INC(n); | |
| 68 | + return n; | |
| 69 | +end; | |
| 70 | + | |
| 71 | +proc bumped(var n: INT): INT; | |
| 72 | +begin | |
| 73 | + return incr(n); | |
| 74 | +end; | |
| 75 | + | |
| 76 | +proc second(s: []U8): U8; | |
| 77 | +begin | |
| 78 | + return s[1]; | |
| 79 | +end; | |
| 80 | + | |
| 81 | +proc odd(n: INT): BOOL; forward; | |
| 82 | + | |
| 83 | +(* recursive through odd, which is made in place here and there *) | |
| 84 | +proc even(n: INT): BOOL; | |
| 85 | +begin | |
| 86 | + return (n = 0) or odd(n - 1); | |
| 87 | +end; | |
| 88 | + | |
| 89 | +proc odd(n: INT): BOOL; | |
| 90 | +begin | |
| 91 | + return (n # 0) and even(n - 1); | |
| 92 | +end; | |
| 93 | + | |
| 94 | +proc show(s: []U8; n: INT); | |
| 95 | +begin | |
| 96 | + Out.str(s); Out.str(" "); Out.int(n); Out.ln; | |
| 97 | +end; | |
| 98 | + | |
| 99 | +proc main*; | |
| 100 | +var i, n: INT; s: []U8; p: Pair; f: proc(a, b: INT): INT; c: U8; | |
| 101 | +begin | |
| 102 | + show("sub", sub(note(10), note(3))); | |
| 103 | + show("order", trace[0] * 10 + trace[1]); | |
| 104 | + show("count", numTrace); | |
| 105 | + show("nested", subSquares(note(5), note(4))); | |
| 106 | + show("count", numTrace); | |
| 107 | + s := "abcD"; | |
| 108 | + i := 0; | |
| 109 | + while (i < LEN(s)) and isLower(s[i]) do INC(i); end; | |
| 110 | + show("lower", i); | |
| 111 | + c := 200; | |
| 112 | + show("wide", wide(c) + wide(c)); | |
| 113 | + show("low", INT(low(-1))); | |
| 114 | + n := add8(200, 100); | |
| 115 | + show("add8", n); | |
| 116 | + if half(3.0) = 1.5 then show("half", 1); end; | |
| 117 | + p := {a: 3, b: 4}; | |
| 118 | + show("const", sumOf(p)); | |
| 119 | + show("literal", sumOf({a: 1, b: 2})); | |
| 120 | + n := 7; | |
| 121 | + show("var", bumped(n)); | |
| 122 | + show("var after", n); | |
| 123 | + show("slice", INT(second(s))); | |
| 124 | + if even(10) and odd(7) and not even(3) then show("even", 1); end; | |
| 125 | + f := sub; | |
| 126 | + show("value", f(9, 2)); | |
| 127 | + show("imported", Inl.twice(Inl.plusHidden(1))); | |
| 128 | + show("generic", Inl.boxed(41)); | |
| 129 | +end; | |
| 130 | +end Inline. |
| @@ -0,0 +1,9 @@ | ||
| 1 | +(* A generic module whose procedure only returns, for Inline *) | |
| 2 | +module Cell(T); | |
| 3 | + | |
| 4 | +proc add*(a, b: T): T; | |
| 5 | +begin | |
| 6 | + return a + b; | |
| 7 | +end; | |
| 8 | + | |
| 9 | +end Cell. |
| @@ -0,0 +1,25 @@ | ||
| 1 | +(* Procedures that only return, for Inline: imported, one reading a | |
| 2 | + variable this module hides, one calling a generic module's. *) | |
| 3 | +module Inl; | |
| 4 | +import Box := Cell(INT); | |
| 5 | + | |
| 6 | +var hidden: INT; | |
| 7 | + | |
| 8 | +proc twice*(x: INT): INT; | |
| 9 | +begin | |
| 10 | + return 2 * x; | |
| 11 | +end; | |
| 12 | + | |
| 13 | +proc plusHidden*(x: INT): INT; | |
| 14 | +begin | |
| 15 | + return x + hidden; | |
| 16 | +end; | |
| 17 | + | |
| 18 | +proc boxed*(x: INT): INT; | |
| 19 | +begin | |
| 20 | + return Box.add(x, 1); | |
| 21 | +end; | |
| 22 | + | |
| 23 | +begin | |
| 24 | + hidden := 100; | |
| 25 | +end Inl. |
| @@ -18,12 +18,12 @@ | ||
| 18 | 18 | |---|---|---|---| |
| 19 | 19 | | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | |
| 20 | 20 | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | |
| 21 | -| `src/Sym.raz` | 724 | 582 | Types, objects, scopes, layout | | |
| 21 | +| `src/Sym.raz` | 737 | 590 | Types, objects, scopes, layout | | |
| 22 | 22 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 23 | 23 | | `src/X64.raz` | 1,620 | 1,329 | The instruction encoder, jumps within a procedure, the assembler | |
| 24 | -| `src/Gen.raz` | 2,828 | 1,978 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 24 | +| `src/Gen.raz` | 2,833 | 1,982 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 25 | 25 | | `src/Item.raz` | 2,442 | 1,959 | Items: types, constants, variables, operators, conversions, calls | |
| 26 | -| `src/Parse.raz` | 4,614 | 3,959 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 26 | +| `src/Parse.raz` | 4,748 | 4,046 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Raz.raz` | 418 | 333 | The command line | |
| @@ -937,7 +937,79 @@ | ||
| 937 | 937 | before. The prologue saves those registers and moves the parameter |
| 938 | 938 | there, as it does for a procedure that calls. ChaCha's block with |
| 939 | 939 | the copy 1,132 ms to 684, 35% fewer instructions. |
| 940 | + | |
| 941 | +## Inlining a procedure that only returns | |
| 942 | + | |
| 943 | +A call of a procedure whose body is only `return e`, with no locals, | |
| 944 | +is made in place of the call, as PROPOSALS.md's "Inlining a procedure | |
| 945 | +that only returns" has it. One pass keeps no tree to copy, but it can | |
| 946 | +read source again, as `while` reads its condition again: `body` notes | |
| 947 | +such a procedure (`onlyReturns`), keeping in its `Obj` an `Inline` | |
| 948 | +(`Sym.raz`): the scanner's state at the `return`, its file, whether | |
| 949 | +its module makes checks, and whether `e` calls anything. `call` hands | |
| 950 | +a call of it to `inlined`, which: | |
| 951 | + | |
| 952 | +1. reads and converts the arguments as for a call (`Item.passArg`), | |
| 953 | + which leaves them on Gen's stack where `Gen.call` would take them; | |
| 954 | +2. pops them, the last on top first, into temporaries of the | |
| 955 | + statement, so each is evaluated once and in order: a parameter by | |
| 956 | + address (`var`, `const`, a record or an array) becomes a slot | |
| 957 | + holding the address the call would pass (`ind`, as a parameter | |
| 958 | + passed so is), a slice its two words, any other its value; | |
| 959 | +3. opens, in place of the caller's scopes, the procedure's module's | |
| 960 | + and one of its own that names the parameters as those temporaries, | |
| 961 | + puts the scanner back at the `return`, with the module, its file | |
| 962 | + and its checks, and reads `e`; | |
| 963 | +4. loads the result, as a call's comes back and widening as one does, | |
| 964 | + unless it is a condition, which stays one for `if` and `while` to | |
| 965 | + jump on; and puts back what it changed. What `e` notes for the | |
| 966 | + warnings was noted when the procedure was compiled, so its doubts | |
| 967 | + are dropped, and no warning comes or goes. | |
| 968 | + | |
| 969 | +Kept narrow: the result a single value (no slice, record or several), | |
| 970 | +no `try` in `e`, and not a procedure already being made in place, so | |
| 971 | +that a recursion ends in a call. A procedure of another module is made | |
| 972 | +in place as one of this module is, its source kept as every file's is, | |
| 973 | +for messages; so is one of a generic module's instance. A procedure | |
| 974 | +taken as a value, or called before its body is read, is called; its | |
| 975 | +body is compiled all the same, and tree shaking drops it if nothing | |
| 976 | +calls it. `chooseRegs` doesn't count a call of one whose `e` calls | |
| 977 | +nothing (`calls`), so the caller can stay a leaf. | |
| 940 | 978 | |
| 979 | +``` | |
| 980 | +proc isLower(c: U8): BOOL; begin return U8(c - 97) < 26; end; | |
| 981 | +... | |
| 982 | +while (i < LEN(s)) and isLower(s[i]) do INC(i); end; | |
| 983 | + | |
| 984 | +before: movzx edi, byte [r12 + rbx] after: movzx r8d, byte [r12 + rbx] | |
| 985 | + call T.isLower mov [rbp - 50], r8b | |
| 986 | + mov r8, rax movzx r8d, byte [rbp - 50] | |
| 987 | + movzx r8d, r8b sub r8, 97 | |
| 988 | + test r8, r8 movzx r8d, r8b | |
| 989 | + jne loop cmp r8, 26 | |
| 990 | + (isLower: push rbp ... cmp r8, 26; jb loop | |
| 991 | + setb r8b; movzx r8d, r8b; mov rax, r8; | |
| 992 | + leave; ret) | |
| 993 | +``` | |
| 994 | + | |
| 995 | +A word count of 16 MiB sixteen times with that test, 887 ms to 638 | |
| 996 | +(586 with `check off`; 516 with the test written in place by hand); | |
| 997 | +test/bench's Grep, whose `zeros` and `lowest` only return, 268 ms to | |
| 998 | +199 (194 by hand). What is left is the store and load of each | |
| 999 | +temporary. The compiler built by itself runs 3% fewer instructions, | |
| 1000 | +the same time, and is 17 KB larger. | |
| 1001 | + | |
| 1002 | +What the trace and a debugger see: the procedure has no frame, so it | |
| 1003 | +isn't in the trap report's list of calls, nor in gdb's backtrace; its | |
| 1004 | +caller is, with the caller's variables. A failed check in `e` is told | |
| 1005 | +at `e`'s line, in its file (Gen's `check` works out the place while | |
| 1006 | +`e` is read, and `endFails` notes it in that file), and a gdb | |
| 1007 | +breakpoint at that line stops on the failing code, and in the | |
| 1008 | +procedure's own body if it is kept. A signal in `e` is told at the | |
| 1009 | +caller's statement, as the line notes inside a statement are its own. | |
| 1010 | +A test that wants the procedure's own frame calls it through a | |
| 1011 | +procedure value (`test/run/KeptCheck.raz`). | |
| 1012 | + | |
| 941 | 1013 | ## Diag.raz: messages |
| 942 | 1014 | |
| 943 | 1015 | Every message the compiler prints is made here. A position, `Pos`, is a |
| @@ -1376,7 +1448,10 @@ | ||
| 1376 | 1448 | a stub for its kind of failure (`failStubs`, made by `entryPoint`), |
| 1377 | 1449 | which says the kind and passes the address it was called from, by which |
| 1378 | 1450 | `Rt` finds the place: the failing code has the place of the offending |
| 1379 | -expression (`lineCol` in `Scan.raz`). It leaves `rdi` and `rsi` as they | |
| 1451 | +expression (`lineCol` in `Scan.raz`), worked out when the check is | |
| 1452 | +made, while the source read is the expression's, and noted in its | |
| 1453 | +file, so that a check in a procedure made in place of its call is told | |
| 1454 | +at that procedure's line. It leaves `rdi` and `rsi` as they | |
| 1380 | 1455 | were, so that the stub can pass them on, for the trap report to show a |
| 1381 | 1456 | parameter kept in them. A check costs a compare and a jump where it |
| 1382 | 1457 | stands, and a dozen bytes out of the way. Code is aligned to 32 bytes, |
| @@ -1698,7 +1773,9 @@ | ||
| 1698 | 1773 | calls the matching routine in `Item.raz` on the item it has. The |
| 1699 | 1774 | `want` parameter carries the type an expression must have, where it is |
| 1700 | 1775 | known, so that `[1, 2, 3]`, `{x: 1}` and `{red}` know what to build |
| 1701 | -(`arrayLiteral`, `recordLiteral`, `setLiteral`). | |
| 1776 | +(`arrayLiteral`, `recordLiteral`, `setLiteral`). A call of a | |
| 1777 | +procedure that only returns is made in place by `inlined` (see | |
| 1778 | +"Inlining a procedure that only returns"). | |
| 1702 | 1779 | |
| 1703 | 1780 | Then statements: `statement` makes each one a recovery point (below) |
| 1704 | 1781 | and `oneStatement` dispatches; `ifStatement`, |
| @@ -1008,17 +1008,17 @@ | ||
| 1008 | 1008 | module Fail; |
| 1009 | 1009 | import Out; |
| 1010 | 1010 | |
| 1011 | -proc get(a: []INT; i: INT): INT; | |
| 1011 | +proc show(a: []INT; i: INT); | |
| 1012 | 1012 | begin |
| 1013 | - return a[i]; | |
| 1013 | + Out.int(a[i]); | |
| 1014 | + Out.ln; | |
| 1014 | 1015 | end; |
| 1015 | 1016 | |
| 1016 | 1017 | proc main*; |
| 1017 | 1018 | var a: [3]INT; i: INT; |
| 1018 | 1019 | begin |
| 1019 | 1020 | for i := 0 to 3 do |
| 1020 | - Out.int(get(a, i)); | |
| 1021 | - Out.ln; | |
| 1021 | + show(a, i); | |
| 1022 | 1022 | end; |
| 1023 | 1023 | end; |
| 1024 | 1024 | |
| @@ -1028,9 +1028,9 @@ | ||
| 1028 | 1028 | 0 |
| 1029 | 1029 | 0 |
| 1030 | 1030 | 0 |
| 1031 | -Fail.raz:6:14: index 3 is out of range for a length of 3 | |
| 1032 | - in Fail.get at Fail.raz:6:14: i = 3 | |
| 1033 | - in Fail.main at Fail.raz:13:9: i = 3 | |
| 1031 | +Fail.raz:6:15: index 3 is out of range for a length of 3 | |
| 1032 | + in Fail.show at Fail.raz:6:15: i = 3 | |
| 1033 | + in Fail.main at Fail.raz:14:9: i = 3 | |
| 1034 | 1034 | .Ed |
| 1035 | 1035 | .Pp |
| 1036 | 1036 | Every index and slice is checked. |
| @@ -185,7 +185,9 @@ | ||
| 185 | 185 | |
| 186 | 186 | (* a check of the procedure, whose failure endFails makes *) |
| 187 | 187 | Fail = record |
| 188 | - L, what, ra, name, at: INT; | |
| 188 | + L, what, ra, name: INT; | |
| 189 | + path, line, col: INT; (* the place, told now: an inlined procedure's | |
| 190 | + is in its own file (Parse's inlined) *) | |
| 189 | 191 | m: X.Mem; |
| 190 | 192 | n: I64; |
| 191 | 193 | end; |
| @@ -1589,9 +1591,9 @@ | ||
| 1589 | 1591 | in them, for the trap report to show: so a goes to the stub in rax. |
| 1590 | 1592 | The failing code is out of the way, at the |
| 1591 | 1593 | procedure's end (endFails), so that a check that holds costs a jump |
| 1592 | - not taken; it has p for its place (at is p's offset in the source), | |
| 1593 | - never returns, and may use any register once it has read what it | |
| 1594 | - needs. *) | |
| 1594 | + not taken; it has p for its place (its line and column, worked out | |
| 1595 | + now, while the source read is p's), never returns, and may use any | |
| 1596 | + register once it has read what it needs. *) | |
| 1595 | 1597 | proc check(cc, what: INT; p: Diag.Pos; ra: INT; m: X.Mem; n: I64; name: INT); |
| 1596 | 1598 | begin |
| 1597 | 1599 | if numFails > LEN(fails) - 1 then |
| @@ -1605,7 +1607,8 @@ | ||
| 1605 | 1607 | fails[numFails].m := m; |
| 1606 | 1608 | fails[numFails].n := n; |
| 1607 | 1609 | fails[numFails].name := name; |
| 1608 | - fails[numFails].at := p.a; | |
| 1610 | + fails[numFails].path := Chunk.curFilePath(); | |
| 1611 | + fails[numFails].line, fails[numFails].col := Scan.lineCol(p.a); | |
| 1609 | 1612 | INC(numFails); |
| 1610 | 1613 | end; |
| 1611 | 1614 | |
| @@ -1613,13 +1616,15 @@ | ||
| 1613 | 1616 | and the place of the offending expression, through a stub for its |
| 1614 | 1617 | kind, which takes the place from where it was called *) |
| 1615 | 1618 | proc endFails; |
| 1616 | -var i, line, col: INT; f: Fail; | |
| 1619 | +var i, path: INT; f: Fail; | |
| 1617 | 1620 | begin |
| 1621 | + path := Chunk.curFilePath(); | |
| 1618 | 1622 | for i := 0 to numFails - 1 do |
| 1619 | 1623 | f := fails[i]; |
| 1620 | 1624 | X.fixLink(f.L); |
| 1621 | - line, col := Scan.lineCol(f.at); | |
| 1622 | - Chunk.noteLine(line, col); | |
| 1625 | + Chunk.setCurFile(f.path); | |
| 1626 | + Chunk.noteLine(f.line, f.col); | |
| 1627 | + Chunk.setCurFile(path); | |
| 1623 | 1628 | if f.ra # RAX then X.movRR(RAX, f.ra); end; |
| 1624 | 1629 | if f.m.base = NoBase then X.movRI(RCX, f.n); |
| 1625 | 1630 | elsif f.m.base = ARegister then X.movRR(RCX, f.m.index); |
| @@ -138,6 +138,12 @@ | ||
| 138 | 138 | in the procedure (chooseRegs); numFors counts them as compiled. *) |
| 139 | 139 | limitK, numFors: INT; |
| 140 | 140 | limitIn: [MaxFors]BOOL; |
| 141 | + (* For a body that is only return e (inlined): where the body's first | |
| 142 | + return statement ends, its ";" with it, or -1; whether a try was | |
| 143 | + read; and the calls made, which an inlined one doesn't count *) | |
| 144 | + returnEnd: INT; | |
| 145 | + sawTry: BOOL; | |
| 146 | + numCalls: INT; | |
| 141 | 147 | |
| 142 | 148 | proc expression(var x: TItem; want: PType): ERROR; forward; |
| 143 | 149 | proc parseType: (PType, ERROR); forward; |
| @@ -958,6 +964,104 @@ | ||
| 958 | 964 | |
| 959 | 965 | (* Expressions *) |
| 960 | 966 | |
| 967 | +(* Inlining a procedure that only returns | |
| 968 | + | |
| 969 | + A call of a procedure whose body is only return e, with no locals, is | |
| 970 | + made in place: e is read again from where it begins, the scanner put | |
| 971 | + back there, as a while loop reads its condition again. The arguments, | |
| 972 | + read and converted as for a call and left on Gen's stack as the call | |
| 973 | + would take them, go to temporaries, the last on top first, so each is | |
| 974 | + evaluated once and in order; a scope names the parameters as them. | |
| 975 | + e is read in the procedure's own module's scope, with its module's | |
| 976 | + checks, so it means what it means there. For | |
| 977 | + | |
| 978 | + proc isLower(c: U8): BOOL; begin return U8(c - 97) < 26; end; | |
| 979 | + ... if isLower(s[i]) then ... | |
| 980 | + | |
| 981 | + the code was, before, | |
| 982 | + | |
| 983 | + movzx edi, byte [s + i]; call isLower | |
| 984 | + mov r8, rax; movzx r8d, r8b; test r8, r8; je else | |
| 985 | + (isLower: push rbp ... cmp r8, 26; setb r8b; movzx r8d, r8b; | |
| 986 | + mov rax, r8; leave; ret) | |
| 987 | + | |
| 988 | + and is now, after, | |
| 989 | + | |
| 990 | + movzx r8d, byte [s + i]; mov [rbp - t], r8b | |
| 991 | + movzx r8d, byte [rbp - t]; sub r8, 97; movzx r8d, r8b | |
| 992 | + cmp r8, 26; jae else | |
| 993 | + | |
| 994 | + A parameter by address (var, const, a record or an array) is a slot | |
| 995 | + holding the address the call would have passed (ind), a slice its two | |
| 996 | + words, any other its value. A failed check in e is told at e's own | |
| 997 | + line, in its own file (Gen's check notes the place while e is read); | |
| 998 | + the procedure isn't in the trap report's list, nor in a debugger's | |
| 999 | + backtrace, as it has no frame. The procedure itself is still | |
| 1000 | + compiled, for a call through a procedure value or before its body | |
| 1001 | + is read. *) | |
| 1002 | +proc inlined(var c: Gen.CallInfo; var x: TItem): ERROR; | |
| 1003 | +var | |
| 1004 | + p: PObj; inl: ^Sym.Inline; q, o: PObj; params: [64]PObj; n, i: INT; t: I64; | |
| 1005 | + st: Scan.State; saved: [Sym.MaxDepth]INT; d, path, doubts0: INT; home: PModule; | |
| 1006 | + checks: BOOL; res: PType; err: ERROR; | |
| 1007 | +begin | |
| 1008 | + p := c.obj; inl := p^.ret; res := c.sig^.base; | |
| 1009 | + n := 0; | |
| 1010 | + q := c.sig^.fields; | |
| 1011 | + while q # nil do | |
| 1012 | + o := Sym.newObj(Sym.oVar, q^.name, q^.typ); | |
| 1013 | + o^.pos := q^.pos; o^.lev := 1; o^.param := true; | |
| 1014 | + o^.isVar := q^.isVar; o^.isConst := q^.isConst; | |
| 1015 | + params[n] := o; | |
| 1016 | + INC(n); | |
| 1017 | + q := q^.next; | |
| 1018 | + end; | |
| 1019 | + for i := n - 1 to 0 by -1 do | |
| 1020 | + o := params[i]; | |
| 1021 | + if o^.isVar or (Sym.isAggregate(o^.typ) and (o^.typ^.form # Sym.fSlice)) then | |
| 1022 | + t := Gen.allocTemp(8, 8); | |
| 1023 | + Gen.popVar(Item.frameAdr(t), 8); | |
| 1024 | + o^.ind := true; | |
| 1025 | + elsif o^.typ^.form = Sym.fSlice then | |
| 1026 | + t := Gen.allocTemp(16, 8); | |
| 1027 | + Gen.popVar(Item.frameAdr(t + 8), 8); (* the length, on top *) | |
| 1028 | + Gen.popVar(Item.frameAdr(t), 8); | |
| 1029 | + else | |
| 1030 | + t := Gen.allocTemp(o^.typ^.size, o^.typ^.align); | |
| 1031 | + Gen.popVar(Item.frameAdr(t), Item.scalarSize(o^.typ)); | |
| 1032 | + end; | |
| 1033 | + o^.val := t; | |
| 1034 | + end; | |
| 1035 | + (* e in the procedure's module, and back; what it notes for warnings | |
| 1036 | + was noted when the procedure was compiled *) | |
| 1037 | + Scan.saveScan(st); | |
| 1038 | + d := Sym.saveScopes(saved[0..Sym.MaxDepth]); | |
| 1039 | + home := Sym.curModule; path := Chunk.curFilePath(); checks := Item.checks; doubts0 := numDoubts; | |
| 1040 | + Sym.setCurModule(p^.home); Chunk.setCurFile(inl^.path); Item.setChecks(inl^.checks); | |
| 1041 | + err := Sym.openScope(p^.home^.scope); | |
| 1042 | + if err = nil then err := Sym.openScope(Sym.newScope()); end; | |
| 1043 | + for i := 0 to n - 1 do | |
| 1044 | + if err = nil then err := Sym.insert(params[i], Sym.topScope()); end; | |
| 1045 | + end; | |
| 1046 | + Scan.restoreScan(inl^.at); | |
| 1047 | + Scan.next; (* past the return *) | |
| 1048 | + inl^.busy := true; | |
| 1049 | + if err = nil then err := expression(x, res); end; | |
| 1050 | + (* a condition stays one, for if and while to jump on; anything else | |
| 1051 | + is loaded, as a call's result is, and widens as one does (made) *) | |
| 1052 | + if (err = nil) and (x.mode # Item.mCond) then | |
| 1053 | + err := Item.coerce(x, res); | |
| 1054 | + if err = nil then err := Item.value(x); end; | |
| 1055 | + x.made := false; | |
| 1056 | + end; | |
| 1057 | + inl^.busy := false; | |
| 1058 | + numDoubts := doubts0; | |
| 1059 | + Sym.setCurModule(home); Chunk.setCurFile(path); Item.setChecks(checks); | |
| 1060 | + Sym.restoreScopes(d, saved[0..Sym.MaxDepth]); | |
| 1061 | + Scan.restoreScan(st); | |
| 1062 | + return err; | |
| 1063 | +end; | |
| 1064 | + | |
| 961 | 1065 | proc call(var x: TItem): ERROR; |
| 962 | 1066 | var c: Gen.CallInfo; a: TItem; want: PType; |
| 963 | 1067 | begin |
| @@ -973,6 +1077,11 @@ | ||
| 973 | 1077 | until false; |
| 974 | 1078 | end; |
| 975 | 1079 | try expect(Scan.sRparen); |
| 1080 | + if (c.param = nil) and (c.obj # nil) and (c.obj^.ret # nil) and not c.obj^.ret^.busy | |
| 1081 | + and (Gen.codeChunk >= 0) then | |
| 1082 | + return inlined(c, x); | |
| 1083 | + end; | |
| 1084 | + INC(numCalls); | |
| 976 | 1085 | return Item.endCall(c, x); |
| 977 | 1086 | end; |
| 978 | 1087 | |
| @@ -1634,6 +1743,7 @@ | ||
| 1634 | 1743 | begin |
| 1635 | 1744 | start := Scan.tokPos(); |
| 1636 | 1745 | Scan.next; |
| 1746 | + sawTry := true; | |
| 1637 | 1747 | if deferring then return Scan.markAt(Scan.spanFrom(start), "try can't leave a deferred statement"); end; |
| 1638 | 1748 | if curResult = nil then |
| 1639 | 1749 | return Scan.markAt(Scan.spanFrom(start), "try needs a procedure that ends in an ERROR result, and this returns nothing"); |
| @@ -2782,6 +2892,10 @@ | ||
| 2782 | 2892 | end; |
| 2783 | 2893 | Gen.returnJump(); |
| 2784 | 2894 | ends := true; |
| 2895 | + if returnEnd < 0 then | |
| 2896 | + returnEnd := Scan.afterPrev().a; | |
| 2897 | + if Scan.tok = Scan.sSemicolon then returnEnd := Scan.tokStart() + 1; end; | |
| 2898 | + end; | |
| 2785 | 2899 | end; |
| 2786 | 2900 | | sBreak: |
| 2787 | 2901 | Scan.next; |
| @@ -2958,12 +3072,13 @@ | ||
| 2958 | 3072 | end; |
| 2959 | 3073 | end; |
| 2960 | 3074 | |
| 2961 | -(* Whether naming o may make a call: a procedure, a procedure variable, | |
| 3075 | +(* Whether naming o may make a call: a procedure (not one made in place | |
| 3076 | + whose expression calls none), a procedure variable, | |
| 2962 | 3077 | or NEW, SYSCALL, COPY or FILL, which use the registers a call would *) |
| 2963 | 3078 | proc calls(o: PObj): BOOL; |
| 2964 | 3079 | begin |
| 2965 | 3080 | if o = nil then return false; end; |
| 2966 | - return ((o^.cls = Sym.oProc) and (o^.inl = 0)) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc)) | |
| 3081 | + return ((o^.cls = Sym.oProc) and (o^.inl = 0) and ((o^.ret = nil) or o^.ret^.calls)) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc)) | |
| 2967 | 3082 | or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy) |
| 2968 | 3083 | or (o^.val = Sym.bFill))); |
| 2969 | 3084 | end; |
| @@ -3196,10 +3311,23 @@ | ||
| 3196 | 3311 | Gen.useRegs(regsOf(params, before)); |
| 3197 | 3312 | end; |
| 3198 | 3313 | |
| 3314 | +(* p, whose body is only return e, e read from at, is made in place of | |
| 3315 | + a call (inlined) if it returns a single value. *) | |
| 3316 | +proc onlyReturns(p: PObj; sig: PType; const at: Scan.State; calls: BOOL); | |
| 3317 | +begin | |
| 3318 | + if (sig^.base = nil) or not Sym.isScalar(sig^.base) then return; end; | |
| 3319 | + NEW(p^.ret, Heap.heap); | |
| 3320 | + if p^.ret = nil then Diag.fatal("out of memory"); end; | |
| 3321 | + p^.ret^.at := at; | |
| 3322 | + p^.ret^.path := Chunk.curFilePath(); | |
| 3323 | + p^.ret^.checks := Item.checks; | |
| 3324 | + p^.ret^.calls := calls; | |
| 3325 | +end; | |
| 3326 | + | |
| 3199 | 3327 | proc body(p: PObj; sig: PType; line: INT): ERROR; |
| 3200 | 3328 | var |
| 3201 | - q, params, last, o, before: PObj; old: PType; st: Scan.State; | |
| 3202 | - mark: Gen.CodeMark; doubts0, errs: INT; | |
| 3329 | + q, params, last, o, before: PObj; old: PType; st, at: Scan.State; | |
| 3330 | + mark: Gen.CodeMark; doubts0, errs, calls0: INT; first, only: BOOL; | |
| 3203 | 3331 | begin |
| 3204 | 3332 | try Sym.openScope(Sym.newScope()); |
| 3205 | 3333 | params := nil; last := nil; |
| @@ -3238,7 +3366,10 @@ | ||
| 3238 | 3366 | curResult := sig^.base; |
| 3239 | 3367 | Gen.enterBody(p^.chunk, line); |
| 3240 | 3368 | stray := false; elseIf := false; deferSlot := 0; numFors := 0; |
| 3369 | + first := Scan.tok = Scan.sReturn; | |
| 3370 | + Scan.saveScan(at); returnEnd := -1; sawTry := false; calls0 := numCalls; | |
| 3241 | 3371 | try statements(); |
| 3372 | + only := first and (Scan.tok = Scan.sEnd) and (Scan.afterPrev().a = returnEnd) and not sawTry; | |
| 3242 | 3373 | p^.openEnd := (curResult # nil) and not ends; |
| 3243 | 3374 | Chunk.noteLine(Scan.curLine(), Scan.curCol()); |
| 3244 | 3375 | try expect(Scan.sEnd); |
| @@ -3258,6 +3389,9 @@ | ||
| 3258 | 3389 | Scan.restoreScan(st); |
| 3259 | 3390 | until false; |
| 3260 | 3391 | try Gen.endProc(p^.chunk, p^.openEnd); |
| 3392 | + if only and (Sym.curModule^.lastLocal = before) and (Diag.errors = errs) then | |
| 3393 | + onlyReturns(p, sig, at, numCalls # calls0); | |
| 3394 | + end; | |
| 3261 | 3395 | (* its parameters and variables, for the trap report *) |
| 3262 | 3396 | o := firstLocal(before); |
| 3263 | 3397 | q := params; |
| @@ -43,6 +43,17 @@ | ||
| 43 | 43 | home*: ^Module; (* the module that built it *) |
| 44 | 44 | end; |
| 45 | 45 | |
| 46 | + (* A procedure whose body is only return e, made in place of a call: | |
| 47 | + e is read again from where it begins (Parse's inlined) *) | |
| 48 | + Inline* = record | |
| 49 | + at*: Scan.State; (* at the return, before e *) | |
| 50 | + path*: INT; (* its file, as Chunk knows it *) | |
| 51 | + checks*: BOOL; (* its module's checks are made *) | |
| 52 | + calls*: BOOL; (* e calls a procedure *) | |
| 53 | + busy*: BOOL; (* being read in place now: not again inside, so | |
| 54 | + that a recursion ends *) | |
| 55 | + end; | |
| 56 | + | |
| 46 | 57 | Obj* = record |
| 47 | 58 | cls*: Class; |
| 48 | 59 | name*: INT; |
| @@ -69,6 +80,8 @@ | ||
| 69 | 80 | xmm0, made in place of a call (Gen.oneRealOp); |
| 70 | 81 | 0 if not *) |
| 71 | 82 | openEnd*: BOOL; (* oProc: has results, and may reach its end *) |
| 83 | + ret*: ^Inline; (* oProc: its body is only return e, made in place | |
| 84 | + of a call; nil if not *) | |
| 72 | 85 | reg*: INT; (* oVar: the register it lives in, or 0 if in memory; |
| 73 | 86 | for a slice, the one its pointer is in *) |
| 74 | 87 | reg2*: INT; (* oVar: a slice in registers: the one its length is in *) |
| @@ -3,32 +3,17 @@ | ||
| 3 | 3 | 0 lea rax, [rdi + rdi] |
| 4 | 4 | 4 ret |
| 5 | 5 | |
| 6 | -Listing.add: | |
| 7 | - 12 proc add(a, b: I64): I64; | |
| 8 | - 0 push rbp | |
| 9 | - 1 mov rbp, rsp | |
| 10 | - 4 sub rsp, 16 | |
| 11 | - 14 return a + b * 2; | |
| 12 | - 11 mov r9, rsi | |
| 13 | - 14 shl r9, 1 | |
| 14 | - 18 mov r8, rdi | |
| 15 | - 21 add r8, r9 | |
| 16 | - 24 mov rax, r8 | |
| 17 | - 15 end add; | |
| 18 | - 27 leave | |
| 19 | - 28 ret | |
| 20 | - | |
| 21 | 6 | Listing.main: |
| 22 | 7 | 17 proc main*; |
| 23 | 8 | 0 push rbp |
| 24 | 9 | 1 mov rbp, rsp |
| 25 | - 4 sub rsp, 16 | |
| 10 | + 4 sub rsp, 32 | |
| 26 | 11 | 11 mov [rbp - 8], rbx |
| 27 | 12 | 15 xor ebx, ebx |
| 28 | 13 | 20 for i := 1 to 3 do |
| 29 | 14 | 17 mov rbx, 1 |
| 30 | 15 | 22 cmp rbx, 3 |
| 31 | - 26 jg 156 | |
| 16 | + 26 jg 168 | |
| 32 | 17 | 32 jmp 68 |
| 33 | 18 | 37 nop |
| 34 | 19 | 45 nop |
| @@ -37,25 +22,28 @@ | ||
| 37 | 22 | 64 add rbx, 1 |
| 38 | 23 | 21 if i # 2 then total := add(total, twice(i)); end; |
| 39 | 24 | 68 cmp rbx, 2 |
| 40 | - 72 je 122 | |
| 25 | + 72 je 134 | |
| 41 | 26 | 78 mov r8, [Listing.total] |
| 42 | 27 | 85 mov [rbp - 16], r8 |
| 43 | 28 | 89 mov rdi, rbx |
| 44 | 29 | 92 call Listing.twice |
| 45 | 30 | 97 mov r9, rax |
| 46 | - 100 mov rdi, [rbp - 16] | |
| 47 | - 104 mov rsi, r9 | |
| 48 | - 107 call Listing.add | |
| 49 | - 112 mov r8, rax | |
| 50 | - 115 mov [Listing.total], r8 | |
| 51 | - 122 cmp rbx, 3 | |
| 52 | - 126 jl 64 | |
| 53 | - 132 cmp rbx, 3 | |
| 54 | - 136 je 156 | |
| 55 | - 142 add rbx, 1 | |
| 56 | - 146 cmp rbx, 3 | |
| 57 | - 150 jle 68 | |
| 31 | + 100 mov [rbp - 24], r9 | |
| 32 | + 104 mov r8, [rbp - 16] | |
| 33 | + 108 mov [rbp - 32], r8 | |
| 34 | + 112 mov r9, [rbp - 24] | |
| 35 | + 116 shl r9, 1 | |
| 36 | + 120 mov r8, [rbp - 32] | |
| 37 | + 124 add r8, r9 | |
| 38 | + 127 mov [Listing.total], r8 | |
| 39 | + 134 cmp rbx, 3 | |
| 40 | + 138 jl 64 | |
| 41 | + 144 cmp rbx, 3 | |
| 42 | + 148 je 168 | |
| 43 | + 154 add rbx, 1 | |
| 44 | + 158 cmp rbx, 3 | |
| 45 | + 162 jle 68 | |
| 58 | 46 | 23 end main; |
| 59 | - 156 mov rbx, [rbp - 8] | |
| 60 | - 160 leave | |
| 61 | - 161 ret | |
| 47 | + 168 mov rbx, [rbp - 8] | |
| 48 | + 172 leave | |
| 49 | + 173 ret |
| @@ -1,3 +1,2 @@ | ||
| 1 | 1 | conform/run/ViewNegative.raz:9:16: a view at index -1 runs past a length of 16 |
| 2 | - in ViewNegative.at at conform/run/ViewNegative.raz:9:16: i = -1 | |
| 3 | - in ViewNegative.main at conform/run/ViewNegative.raz:15:5 | |
| 2 | + in ViewNegative.main at conform/run/ViewNegative.raz:9:16 |
| @@ -1,4 +1,3 @@ | ||
| 1 | 1 | Heap: out of memory |
| 2 | 2 | in Heap.allocSure at lib/Heap.raz:122:9: a = 0x?, size = 9223372036854775803, align = 8, p = nil |
| 3 | - in Mem.alloc at lib/Mem.raz:33:5: a = 0x?, size = 9223372036854775803, align = 8 | |
| 4 | 3 | in HeapSure.main at test/run/HeapSure.raz:8:5 |
| @@ -1,3 +1,2 @@ | ||
| 1 | 1 | test/run/IndexParam.raz:106:14: index 8 is out of range for a length of 8 |
| 2 | - in IndexParam.at at test/run/IndexParam.raz:106:14: k = 7 | |
| 3 | - in IndexParam.main at test/run/IndexParam.raz:151:5: i = 7 | |
| 2 | + in IndexParam.main at test/run/IndexParam.raz:106:14: i = 7 |
| @@ -0,0 +1,3 @@ | ||
| 1 | +1 | |
| 2 | +2 | |
| 3 | +3 |
| @@ -0,0 +1,15 @@ | ||
| 1 | +(* A check that fails in a procedure made in place of its call is told | |
| 2 | + at the procedure's line, in its file; the trap report names the | |
| 3 | + procedure that called it, which the code is part of. *) | |
| 4 | +module InlineCheck; | |
| 5 | +import Out, InlAt; | |
| 6 | + | |
| 7 | +proc main*; | |
| 8 | +var a: [3]INT; i: INT; | |
| 9 | +begin | |
| 10 | + a := [1, 2, 3]; | |
| 11 | + for i := 0 to 3 do | |
| 12 | + Out.int(InlAt.at(a, i)); Out.ln; | |
| 13 | + end; | |
| 14 | +end; | |
| 15 | +end InlineCheck. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1,2 @@ | ||
| 1 | +test/run/mods/InlAt.raz:7:14: index 3 is out of range for a length of 3 | |
| 2 | + in InlineCheck.main at test/run/mods/InlAt.raz:7:14: i = 3 |
| @@ -10,10 +10,13 @@ | ||
| 10 | 10 | return a[i] + I64(b); |
| 11 | 11 | end; |
| 12 | 12 | |
| 13 | +(* called through a procedure value, so that at isn't made in place *) | |
| 13 | 14 | proc main*; |
| 15 | +var f: proc(i: INT; b: U8): I64; | |
| 14 | 16 | begin |
| 15 | - Out.int(at(3, 1)); Out.ln; | |
| 16 | - Out.int(at(4, 200)); Out.ln; | |
| 17 | + f := at; | |
| 18 | + Out.int(f(3, 1)); Out.ln; | |
| 19 | + Out.int(f(4, 200)); Out.ln; | |
| 17 | 20 | end; |
| 18 | 21 | |
| 19 | 22 | end KeptCheck. |
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | test/run/KeptCheck.raz:10:14: index 4 is out of range for a length of 4 |
| 2 | 2 | in KeptCheck.at at test/run/KeptCheck.raz:10:14: i = 4, b = 200 |
| 3 | - in KeptCheck.main at test/run/KeptCheck.raz:16:5 | |
| 3 | + in KeptCheck.main at test/run/KeptCheck.raz:19:5: f = 0x? |
| @@ -10,12 +10,14 @@ | ||
| 10 | 10 | return p^.x + k; |
| 11 | 11 | end; |
| 12 | 12 | |
| 13 | +(* called through a procedure value, so that get isn't made in place *) | |
| 13 | 14 | proc main*; |
| 14 | -var r: Rec; | |
| 15 | +var r: Rec; f: proc(p: ^Rec; k: INT): INT; | |
| 15 | 16 | begin |
| 16 | 17 | r.x := 1; |
| 17 | - Out.int(get(ADR(r), 2)); Out.ln; | |
| 18 | - Out.int(get(nil, 5)); Out.ln; | |
| 18 | + f := get; | |
| 19 | + Out.int(f(ADR(r), 2)); Out.ln; | |
| 20 | + Out.int(f(nil, 5)); Out.ln; | |
| 19 | 21 | end; |
| 20 | 22 | |
| 21 | 23 | end KeptNil. |
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | test/run/KeptNil.raz:10:5: stopped by a nil pointer |
| 2 | 2 | in KeptNil.get at test/run/KeptNil.raz:10:5: p = nil, k = 5 |
| 3 | - in KeptNil.main at test/run/KeptNil.raz:18:5 | |
| 3 | + in KeptNil.main at test/run/KeptNil.raz:20:5: f = 0x? |
| @@ -0,0 +1,10 @@ | ||
| 1 | +(* A procedure that only returns, made in place in InlineCheck: its | |
| 2 | + check is told here, at its own line. *) | |
| 3 | +module InlAt; | |
| 4 | + | |
| 5 | +proc at*(s: []INT; i: INT): INT; | |
| 6 | +begin | |
| 7 | + return s[i]; | |
| 8 | +end; | |
| 9 | + | |
| 10 | +end InlAt. |