From 88e6b860d768f019a8b7d5b43129fcbade500814 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:17:35 +0100 Subject: [PATCH] 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 --- diff --git a/CLAUDE.md b/CLAUDE.md index 0e8d84e..c70430d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -399,3 +399,15 @@ or another module's array, as `d`, says why it can't be changed, for `COPY` too. `seqSlice` copies no slice that is a temporary already (a slice just cut): nothing `s` or `x` does can change it. +- A call of a procedure whose body is only `return e`, with no locals, + a single value its result and no `try` in `e`, is made in place + (`Parse.inlined`): the arguments read and converted as for a call, + each once and in order, into temporaries the parameters are named + as (a `var` or `const` one, a record or an array, by the address the + call would pass), then `e` read again in its own module's scope, + with that module's checks. Another module's procedure and a generic + instance's too; not one already being made in place, nor a call + through a procedure value. The procedure has no frame: it is in + neither the trap report's calls nor gdb's backtrace; a failed check + in `e` is told at `e`'s line, a signal at the caller's statement. + A test that wants its frame calls it through a procedure value. diff --git a/conform/run/Inline.expect b/conform/run/Inline.expect new file mode 100644 index 0000000..67e8349 --- /dev/null +++ b/conform/run/Inline.expect @@ -0,0 +1,19 @@ +sub 7 +order 103 +count 2 +nested 9 +count 4 +lower 3 +wide 400 +low 255 +add8 44 +half 1 +const 7 +literal 3 +var 8 +var after 8 +slice 98 +even 1 +value 7 +imported 202 +generic 42 diff --git a/conform/run/Inline.raz b/conform/run/Inline.raz new file mode 100644 index 0000000..69996f7 --- /dev/null +++ b/conform/run/Inline.raz @@ -0,0 +1,130 @@ +(* Calls of procedures whose body is only return e mean what any call + means: each argument evaluated once, left to right, and converted as + for a call; a var or const parameter the variable itself; the result + of the procedure's type. *) +module Inline; +import Out, Inl; + +type Pair = record a, b: INT; end; + +var trace: [16]INT; numTrace: INT; + +(* v, noted in trace: an argument with a side effect *) +proc note(v: INT): INT; +begin + trace[numTrace] := v; + INC(numTrace); + return v; +end; + +proc sub(a, b: INT): INT; +begin + return a - b; +end; + +proc square(a: INT): INT; +begin + return a * a; +end; + +proc subSquares(a, b: INT): INT; +begin + return sub(square(a), square(b)); +end; + +proc isLower(c: U8): BOOL; +begin + return U8(c - 97) < 26; +end; + +proc wide(x: U8): INT; +begin + return x; +end; + +proc low(x: INT): U8; +begin + return U8(x & 255); +end; + +proc add8(a, b: U8): U8; +begin + return a + b; +end; + +proc half(r: F64): F64; +begin + return r / 2.0; +end; + +proc sumOf(const p: Pair): INT; +begin + return p.a + p.b; +end; + +proc incr(var n: INT): INT; +begin + INC(n); + return n; +end; + +proc bumped(var n: INT): INT; +begin + return incr(n); +end; + +proc second(s: []U8): U8; +begin + return s[1]; +end; + +proc odd(n: INT): BOOL; forward; + +(* recursive through odd, which is made in place here and there *) +proc even(n: INT): BOOL; +begin + return (n = 0) or odd(n - 1); +end; + +proc odd(n: INT): BOOL; +begin + return (n # 0) and even(n - 1); +end; + +proc show(s: []U8; n: INT); +begin + Out.str(s); Out.str(" "); Out.int(n); Out.ln; +end; + +proc main*; +var i, n: INT; s: []U8; p: Pair; f: proc(a, b: INT): INT; c: U8; +begin + show("sub", sub(note(10), note(3))); + show("order", trace[0] * 10 + trace[1]); + show("count", numTrace); + show("nested", subSquares(note(5), note(4))); + show("count", numTrace); + s := "abcD"; + i := 0; + while (i < LEN(s)) and isLower(s[i]) do INC(i); end; + show("lower", i); + c := 200; + show("wide", wide(c) + wide(c)); + show("low", INT(low(-1))); + n := add8(200, 100); + show("add8", n); + if half(3.0) = 1.5 then show("half", 1); end; + p := {a: 3, b: 4}; + show("const", sumOf(p)); + show("literal", sumOf({a: 1, b: 2})); + n := 7; + show("var", bumped(n)); + show("var after", n); + show("slice", INT(second(s))); + if even(10) and odd(7) and not even(3) then show("even", 1); end; + f := sub; + show("value", f(9, 2)); + show("imported", Inl.twice(Inl.plusHidden(1))); + show("generic", Inl.boxed(41)); +end; +end Inline. diff --git a/conform/run/mods/Cell.raz b/conform/run/mods/Cell.raz new file mode 100644 index 0000000..dbc214d --- /dev/null +++ b/conform/run/mods/Cell.raz @@ -0,0 +1,9 @@ +(* A generic module whose procedure only returns, for Inline *) +module Cell(T); + +proc add*(a, b: T): T; +begin + return a + b; +end; + +end Cell. diff --git a/conform/run/mods/Inl.raz b/conform/run/mods/Inl.raz new file mode 100644 index 0000000..f5b8b59 --- /dev/null +++ b/conform/run/mods/Inl.raz @@ -0,0 +1,25 @@ +(* Procedures that only return, for Inline: imported, one reading a + variable this module hides, one calling a generic module's. *) +module Inl; +import Box := Cell(INT); + +var hidden: INT; + +proc twice*(x: INT): INT; +begin + return 2 * x; +end; + +proc plusHidden*(x: INT): INT; +begin + return x + hidden; +end; + +proc boxed*(x: INT): INT; +begin + return Box.add(x, 1); +end; + +begin + hidden := 100; +end Inl. diff --git a/doc/compiler.md b/doc/compiler.md index 0ebb5a1..44fb9e0 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -18,12 +18,12 @@ |---|---|---|---| | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 724 | 582 | Types, objects, scopes, layout | +| `src/Sym.raz` | 737 | 590 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,620 | 1,329 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,828 | 1,978 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Gen.raz` | 2,833 | 1,982 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,442 | 1,959 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,614 | 3,959 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,748 | 4,046 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -937,7 +937,79 @@ before. The prologue saves those registers and moves the parameter there, as it does for a procedure that calls. ChaCha's block with the copy 1,132 ms to 684, 35% fewer instructions. + +## Inlining a procedure that only returns + +A call of a procedure whose body is only `return e`, with no locals, +is made in place of the call, as PROPOSALS.md's "Inlining a procedure +that only returns" has it. One pass keeps no tree to copy, but it can +read source again, as `while` reads its condition again: `body` notes +such a procedure (`onlyReturns`), keeping in its `Obj` an `Inline` +(`Sym.raz`): the scanner's state at the `return`, its file, whether +its module makes checks, and whether `e` calls anything. `call` hands +a call of it to `inlined`, which: + +1. reads and converts the arguments as for a call (`Item.passArg`), + which leaves them on Gen's stack where `Gen.call` would take them; +2. pops them, the last on top first, into temporaries of the + statement, so each is evaluated once and in order: a parameter by + address (`var`, `const`, a record or an array) becomes a slot + holding the address the call would pass (`ind`, as a parameter + passed so is), a slice its two words, any other its value; +3. opens, in place of the caller's scopes, the procedure's module's + and one of its own that names the parameters as those temporaries, + puts the scanner back at the `return`, with the module, its file + and its checks, and reads `e`; +4. loads the result, as a call's comes back and widening as one does, + unless it is a condition, which stays one for `if` and `while` to + jump on; and puts back what it changed. What `e` notes for the + warnings was noted when the procedure was compiled, so its doubts + are dropped, and no warning comes or goes. + +Kept narrow: the result a single value (no slice, record or several), +no `try` in `e`, and not a procedure already being made in place, so +that a recursion ends in a call. A procedure of another module is made +in place as one of this module is, its source kept as every file's is, +for messages; so is one of a generic module's instance. A procedure +taken as a value, or called before its body is read, is called; its +body is compiled all the same, and tree shaking drops it if nothing +calls it. `chooseRegs` doesn't count a call of one whose `e` calls +nothing (`calls`), so the caller can stay a leaf. +``` +proc isLower(c: U8): BOOL; begin return U8(c - 97) < 26; end; +... +while (i < LEN(s)) and isLower(s[i]) do INC(i); end; + +before: movzx edi, byte [r12 + rbx] after: movzx r8d, byte [r12 + rbx] + call T.isLower mov [rbp - 50], r8b + mov r8, rax movzx r8d, byte [rbp - 50] + movzx r8d, r8b sub r8, 97 + test r8, r8 movzx r8d, r8b + jne loop cmp r8, 26 + (isLower: push rbp ... cmp r8, 26; jb loop + setb r8b; movzx r8d, r8b; mov rax, r8; + leave; ret) +``` + +A word count of 16 MiB sixteen times with that test, 887 ms to 638 +(586 with `check off`; 516 with the test written in place by hand); +test/bench's Grep, whose `zeros` and `lowest` only return, 268 ms to +199 (194 by hand). What is left is the store and load of each +temporary. The compiler built by itself runs 3% fewer instructions, +the same time, and is 17 KB larger. + +What the trace and a debugger see: the procedure has no frame, so it +isn't in the trap report's list of calls, nor in gdb's backtrace; its +caller is, with the caller's variables. A failed check in `e` is told +at `e`'s line, in its file (Gen's `check` works out the place while +`e` is read, and `endFails` notes it in that file), and a gdb +breakpoint at that line stops on the failing code, and in the +procedure's own body if it is kept. A signal in `e` is told at the +caller's statement, as the line notes inside a statement are its own. +A test that wants the procedure's own frame calls it through a +procedure value (`test/run/KeptCheck.raz`). + ## Diag.raz: messages Every message the compiler prints is made here. A position, `Pos`, is a @@ -1376,7 +1448,10 @@ a stub for its kind of failure (`failStubs`, made by `entryPoint`), which says the kind and passes the address it was called from, by which `Rt` finds the place: the failing code has the place of the offending -expression (`lineCol` in `Scan.raz`). It leaves `rdi` and `rsi` as they +expression (`lineCol` in `Scan.raz`), worked out when the check is +made, while the source read is the expression's, and noted in its +file, so that a check in a procedure made in place of its call is told +at that procedure's line. It leaves `rdi` and `rsi` as they were, so that the stub can pass them on, for the trap report to show a parameter kept in them. A check costs a compare and a jump where it stands, and a dozen bytes out of the way. Code is aligned to 32 bytes, @@ -1698,7 +1773,9 @@ calls the matching routine in `Item.raz` on the item it has. The `want` parameter carries the type an expression must have, where it is known, so that `[1, 2, 3]`, `{x: 1}` and `{red}` know what to build -(`arrayLiteral`, `recordLiteral`, `setLiteral`). +(`arrayLiteral`, `recordLiteral`, `setLiteral`). A call of a +procedure that only returns is made in place by `inlined` (see +"Inlining a procedure that only returns"). Then statements: `statement` makes each one a recovery point (below) and `oneStatement` dispatches; `ifStatement`, diff --git a/doc/raz-tutorial.7 b/doc/raz-tutorial.7 index 1c38572..c91ebf5 100644 --- a/doc/raz-tutorial.7 +++ b/doc/raz-tutorial.7 @@ -1008,17 +1008,17 @@ module Fail; import Out; -proc get(a: []INT; i: INT): INT; +proc show(a: []INT; i: INT); begin - return a[i]; + Out.int(a[i]); + Out.ln; end; proc main*; var a: [3]INT; i: INT; begin for i := 0 to 3 do - Out.int(get(a, i)); - Out.ln; + show(a, i); end; end; @@ -1028,9 +1028,9 @@ 0 0 0 -Fail.raz:6:14: index 3 is out of range for a length of 3 - in Fail.get at Fail.raz:6:14: i = 3 - in Fail.main at Fail.raz:13:9: i = 3 +Fail.raz:6:15: index 3 is out of range for a length of 3 + in Fail.show at Fail.raz:6:15: i = 3 + in Fail.main at Fail.raz:14:9: i = 3 .Ed .Pp Every index and slice is checked. diff --git a/src/Gen.raz b/src/Gen.raz index a9a6f84..27707f4 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -185,7 +185,9 @@ (* a check of the procedure, whose failure endFails makes *) Fail = record - L, what, ra, name, at: INT; + L, what, ra, name: INT; + path, line, col: INT; (* the place, told now: an inlined procedure's + is in its own file (Parse's inlined) *) m: X.Mem; n: I64; end; @@ -1589,9 +1591,9 @@ in them, for the trap report to show: so a goes to the stub in rax. The failing code is out of the way, at the procedure's end (endFails), so that a check that holds costs a jump - not taken; it has p for its place (at is p's offset in the source), - never returns, and may use any register once it has read what it - needs. *) + not taken; it has p for its place (its line and column, worked out + now, while the source read is p's), never returns, and may use any + register once it has read what it needs. *) proc check(cc, what: INT; p: Diag.Pos; ra: INT; m: X.Mem; n: I64; name: INT); begin if numFails > LEN(fails) - 1 then @@ -1605,7 +1607,8 @@ fails[numFails].m := m; fails[numFails].n := n; fails[numFails].name := name; - fails[numFails].at := p.a; + fails[numFails].path := Chunk.curFilePath(); + fails[numFails].line, fails[numFails].col := Scan.lineCol(p.a); INC(numFails); end; @@ -1613,13 +1616,15 @@ and the place of the offending expression, through a stub for its kind, which takes the place from where it was called *) proc endFails; -var i, line, col: INT; f: Fail; +var i, path: INT; f: Fail; begin + path := Chunk.curFilePath(); for i := 0 to numFails - 1 do f := fails[i]; X.fixLink(f.L); - line, col := Scan.lineCol(f.at); - Chunk.noteLine(line, col); + Chunk.setCurFile(f.path); + Chunk.noteLine(f.line, f.col); + Chunk.setCurFile(path); if f.ra # RAX then X.movRR(RAX, f.ra); end; if f.m.base = NoBase then X.movRI(RCX, f.n); elsif f.m.base = ARegister then X.movRR(RCX, f.m.index); diff --git a/src/Parse.raz b/src/Parse.raz index bb40223..8cdfccf 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -138,6 +138,12 @@ in the procedure (chooseRegs); numFors counts them as compiled. *) limitK, numFors: INT; limitIn: [MaxFors]BOOL; + (* For a body that is only return e (inlined): where the body's first + return statement ends, its ";" with it, or -1; whether a try was + read; and the calls made, which an inlined one doesn't count *) + returnEnd: INT; + sawTry: BOOL; + numCalls: INT; proc expression(var x: TItem; want: PType): ERROR; forward; proc parseType: (PType, ERROR); forward; @@ -958,6 +964,104 @@ (* Expressions *) +(* Inlining a procedure that only returns + + A call of a procedure whose body is only return e, with no locals, is + made in place: e is read again from where it begins, the scanner put + back there, as a while loop reads its condition again. The arguments, + read and converted as for a call and left on Gen's stack as the call + would take them, go to temporaries, the last on top first, so each is + evaluated once and in order; a scope names the parameters as them. + e is read in the procedure's own module's scope, with its module's + checks, so it means what it means there. For + + proc isLower(c: U8): BOOL; begin return U8(c - 97) < 26; end; + ... if isLower(s[i]) then ... + + the code was, before, + + movzx edi, byte [s + i]; call isLower + mov r8, rax; movzx r8d, r8b; test r8, r8; je else + (isLower: push rbp ... cmp r8, 26; setb r8b; movzx r8d, r8b; + mov rax, r8; leave; ret) + + and is now, after, + + movzx r8d, byte [s + i]; mov [rbp - t], r8b + movzx r8d, byte [rbp - t]; sub r8, 97; movzx r8d, r8b + cmp r8, 26; jae else + + A parameter by address (var, const, a record or an array) is a slot + holding the address the call would have passed (ind), a slice its two + words, any other its value. A failed check in e is told at e's own + line, in its own file (Gen's check notes the place while e is read); + the procedure isn't in the trap report's list, nor in a debugger's + backtrace, as it has no frame. The procedure itself is still + compiled, for a call through a procedure value or before its body + is read. *) +proc inlined(var c: Gen.CallInfo; var x: TItem): ERROR; +var + p: PObj; inl: ^Sym.Inline; q, o: PObj; params: [64]PObj; n, i: INT; t: I64; + st: Scan.State; saved: [Sym.MaxDepth]INT; d, path, doubts0: INT; home: PModule; + checks: BOOL; res: PType; err: ERROR; +begin + p := c.obj; inl := p^.ret; res := c.sig^.base; + n := 0; + q := c.sig^.fields; + while q # nil do + o := Sym.newObj(Sym.oVar, q^.name, q^.typ); + o^.pos := q^.pos; o^.lev := 1; o^.param := true; + o^.isVar := q^.isVar; o^.isConst := q^.isConst; + params[n] := o; + INC(n); + q := q^.next; + end; + for i := n - 1 to 0 by -1 do + o := params[i]; + if o^.isVar or (Sym.isAggregate(o^.typ) and (o^.typ^.form # Sym.fSlice)) then + t := Gen.allocTemp(8, 8); + Gen.popVar(Item.frameAdr(t), 8); + o^.ind := true; + elsif o^.typ^.form = Sym.fSlice then + t := Gen.allocTemp(16, 8); + Gen.popVar(Item.frameAdr(t + 8), 8); (* the length, on top *) + Gen.popVar(Item.frameAdr(t), 8); + else + t := Gen.allocTemp(o^.typ^.size, o^.typ^.align); + Gen.popVar(Item.frameAdr(t), Item.scalarSize(o^.typ)); + end; + o^.val := t; + end; + (* e in the procedure's module, and back; what it notes for warnings + was noted when the procedure was compiled *) + Scan.saveScan(st); + d := Sym.saveScopes(saved[0..Sym.MaxDepth]); + home := Sym.curModule; path := Chunk.curFilePath(); checks := Item.checks; doubts0 := numDoubts; + Sym.setCurModule(p^.home); Chunk.setCurFile(inl^.path); Item.setChecks(inl^.checks); + err := Sym.openScope(p^.home^.scope); + if err = nil then err := Sym.openScope(Sym.newScope()); end; + for i := 0 to n - 1 do + if err = nil then err := Sym.insert(params[i], Sym.topScope()); end; + end; + Scan.restoreScan(inl^.at); + Scan.next; (* past the return *) + inl^.busy := true; + if err = nil then err := expression(x, res); end; + (* a condition stays one, for if and while to jump on; anything else + is loaded, as a call's result is, and widens as one does (made) *) + if (err = nil) and (x.mode # Item.mCond) then + err := Item.coerce(x, res); + if err = nil then err := Item.value(x); end; + x.made := false; + end; + inl^.busy := false; + numDoubts := doubts0; + Sym.setCurModule(home); Chunk.setCurFile(path); Item.setChecks(checks); + Sym.restoreScopes(d, saved[0..Sym.MaxDepth]); + Scan.restoreScan(st); + return err; +end; + proc call(var x: TItem): ERROR; var c: Gen.CallInfo; a: TItem; want: PType; begin @@ -973,6 +1077,11 @@ until false; end; try expect(Scan.sRparen); + if (c.param = nil) and (c.obj # nil) and (c.obj^.ret # nil) and not c.obj^.ret^.busy + and (Gen.codeChunk >= 0) then + return inlined(c, x); + end; + INC(numCalls); return Item.endCall(c, x); end; @@ -1634,6 +1743,7 @@ begin start := Scan.tokPos(); Scan.next; + sawTry := true; if deferring then return Scan.markAt(Scan.spanFrom(start), "try can't leave a deferred statement"); end; if curResult = nil then return Scan.markAt(Scan.spanFrom(start), "try needs a procedure that ends in an ERROR result, and this returns nothing"); @@ -2782,6 +2892,10 @@ end; Gen.returnJump(); ends := true; + if returnEnd < 0 then + returnEnd := Scan.afterPrev().a; + if Scan.tok = Scan.sSemicolon then returnEnd := Scan.tokStart() + 1; end; + end; end; | sBreak: Scan.next; @@ -2958,12 +3072,13 @@ end; end; -(* Whether naming o may make a call: a procedure, a procedure variable, +(* Whether naming o may make a call: a procedure (not one made in place + whose expression calls none), a procedure variable, or NEW, SYSCALL, COPY or FILL, which use the registers a call would *) proc calls(o: PObj): BOOL; begin if o = nil then return false; end; - return ((o^.cls = Sym.oProc) and (o^.inl = 0)) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc)) + 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)) or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy) or (o^.val = Sym.bFill))); end; @@ -3196,10 +3311,23 @@ Gen.useRegs(regsOf(params, before)); end; +(* p, whose body is only return e, e read from at, is made in place of + a call (inlined) if it returns a single value. *) +proc onlyReturns(p: PObj; sig: PType; const at: Scan.State; calls: BOOL); +begin + if (sig^.base = nil) or not Sym.isScalar(sig^.base) then return; end; + NEW(p^.ret, Heap.heap); + if p^.ret = nil then Diag.fatal("out of memory"); end; + p^.ret^.at := at; + p^.ret^.path := Chunk.curFilePath(); + p^.ret^.checks := Item.checks; + p^.ret^.calls := calls; +end; + proc body(p: PObj; sig: PType; line: INT): ERROR; var - q, params, last, o, before: PObj; old: PType; st: Scan.State; - mark: Gen.CodeMark; doubts0, errs: INT; + q, params, last, o, before: PObj; old: PType; st, at: Scan.State; + mark: Gen.CodeMark; doubts0, errs, calls0: INT; first, only: BOOL; begin try Sym.openScope(Sym.newScope()); params := nil; last := nil; @@ -3238,7 +3366,10 @@ curResult := sig^.base; Gen.enterBody(p^.chunk, line); stray := false; elseIf := false; deferSlot := 0; numFors := 0; + first := Scan.tok = Scan.sReturn; + Scan.saveScan(at); returnEnd := -1; sawTry := false; calls0 := numCalls; try statements(); + only := first and (Scan.tok = Scan.sEnd) and (Scan.afterPrev().a = returnEnd) and not sawTry; p^.openEnd := (curResult # nil) and not ends; Chunk.noteLine(Scan.curLine(), Scan.curCol()); try expect(Scan.sEnd); @@ -3258,6 +3389,9 @@ Scan.restoreScan(st); until false; try Gen.endProc(p^.chunk, p^.openEnd); + if only and (Sym.curModule^.lastLocal = before) and (Diag.errors = errs) then + onlyReturns(p, sig, at, numCalls # calls0); + end; (* its parameters and variables, for the trap report *) o := firstLocal(before); q := params; diff --git a/src/Sym.raz b/src/Sym.raz index 3bf254e..e53dd25 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -43,6 +43,17 @@ home*: ^Module; (* the module that built it *) end; + (* A procedure whose body is only return e, made in place of a call: + e is read again from where it begins (Parse's inlined) *) + Inline* = record + at*: Scan.State; (* at the return, before e *) + path*: INT; (* its file, as Chunk knows it *) + checks*: BOOL; (* its module's checks are made *) + calls*: BOOL; (* e calls a procedure *) + busy*: BOOL; (* being read in place now: not again inside, so + that a recursion ends *) + end; + Obj* = record cls*: Class; name*: INT; @@ -69,6 +80,8 @@ xmm0, made in place of a call (Gen.oneRealOp); 0 if not *) openEnd*: BOOL; (* oProc: has results, and may reach its end *) + ret*: ^Inline; (* oProc: its body is only return e, made in place + of a call; nil if not *) reg*: INT; (* oVar: the register it lives in, or 0 if in memory; for a slice, the one its pointer is in *) reg2*: INT; (* oVar: a slice in registers: the one its length is in *) diff --git a/test/list/Listing.expect b/test/list/Listing.expect index 45a3618..84572bc 100644 --- a/test/list/Listing.expect +++ b/test/list/Listing.expect @@ -3,32 +3,17 @@ 0 lea rax, [rdi + rdi] 4 ret -Listing.add: - 12 proc add(a, b: I64): I64; - 0 push rbp - 1 mov rbp, rsp - 4 sub rsp, 16 - 14 return a + b * 2; - 11 mov r9, rsi - 14 shl r9, 1 - 18 mov r8, rdi - 21 add r8, r9 - 24 mov rax, r8 - 15 end add; - 27 leave - 28 ret - Listing.main: 17 proc main*; 0 push rbp 1 mov rbp, rsp - 4 sub rsp, 16 + 4 sub rsp, 32 11 mov [rbp - 8], rbx 15 xor ebx, ebx 20 for i := 1 to 3 do 17 mov rbx, 1 22 cmp rbx, 3 - 26 jg 156 + 26 jg 168 32 jmp 68 37 nop 45 nop @@ -37,25 +22,28 @@ 64 add rbx, 1 21 if i # 2 then total := add(total, twice(i)); end; 68 cmp rbx, 2 - 72 je 122 + 72 je 134 78 mov r8, [Listing.total] 85 mov [rbp - 16], r8 89 mov rdi, rbx 92 call Listing.twice 97 mov r9, rax - 100 mov rdi, [rbp - 16] - 104 mov rsi, r9 - 107 call Listing.add - 112 mov r8, rax - 115 mov [Listing.total], r8 - 122 cmp rbx, 3 - 126 jl 64 - 132 cmp rbx, 3 - 136 je 156 - 142 add rbx, 1 - 146 cmp rbx, 3 - 150 jle 68 + 100 mov [rbp - 24], r9 + 104 mov r8, [rbp - 16] + 108 mov [rbp - 32], r8 + 112 mov r9, [rbp - 24] + 116 shl r9, 1 + 120 mov r8, [rbp - 32] + 124 add r8, r9 + 127 mov [Listing.total], r8 + 134 cmp rbx, 3 + 138 jl 64 + 144 cmp rbx, 3 + 148 je 168 + 154 add rbx, 1 + 158 cmp rbx, 3 + 162 jle 68 23 end main; - 156 mov rbx, [rbp - 8] - 160 leave - 161 ret + 168 mov rbx, [rbp - 8] + 172 leave + 173 ret diff --git a/test/msg/ViewNegative.stderr b/test/msg/ViewNegative.stderr index e01e07e..9007305 100644 --- a/test/msg/ViewNegative.stderr +++ b/test/msg/ViewNegative.stderr @@ -1,3 +1,2 @@ conform/run/ViewNegative.raz:9:16: a view at index -1 runs past a length of 16 - in ViewNegative.at at conform/run/ViewNegative.raz:9:16: i = -1 - in ViewNegative.main at conform/run/ViewNegative.raz:15:5 + in ViewNegative.main at conform/run/ViewNegative.raz:9:16 diff --git a/test/run/HeapSure.stderr b/test/run/HeapSure.stderr index 344bd75..f1007f8 100644 --- a/test/run/HeapSure.stderr +++ b/test/run/HeapSure.stderr @@ -1,4 +1,3 @@ Heap: out of memory in Heap.allocSure at lib/Heap.raz:122:9: a = 0x?, size = 9223372036854775803, align = 8, p = nil - in Mem.alloc at lib/Mem.raz:33:5: a = 0x?, size = 9223372036854775803, align = 8 in HeapSure.main at test/run/HeapSure.raz:8:5 diff --git a/test/run/IndexParam.stderr b/test/run/IndexParam.stderr index 710fea2..f9243fe 100644 --- a/test/run/IndexParam.stderr +++ b/test/run/IndexParam.stderr @@ -1,3 +1,2 @@ test/run/IndexParam.raz:106:14: index 8 is out of range for a length of 8 - in IndexParam.at at test/run/IndexParam.raz:106:14: k = 7 - in IndexParam.main at test/run/IndexParam.raz:151:5: i = 7 + in IndexParam.main at test/run/IndexParam.raz:106:14: i = 7 diff --git a/test/run/InlineCheck.expect b/test/run/InlineCheck.expect new file mode 100644 index 0000000..01e79c3 --- /dev/null +++ b/test/run/InlineCheck.expect @@ -0,0 +1,3 @@ +1 +2 +3 diff --git a/test/run/InlineCheck.raz b/test/run/InlineCheck.raz new file mode 100644 index 0000000..9b11895 --- /dev/null +++ b/test/run/InlineCheck.raz @@ -0,0 +1,15 @@ +(* A check that fails in a procedure made in place of its call is told + at the procedure's line, in its file; the trap report names the + procedure that called it, which the code is part of. *) +module InlineCheck; +import Out, InlAt; + +proc main*; +var a: [3]INT; i: INT; +begin + a := [1, 2, 3]; + for i := 0 to 3 do + Out.int(InlAt.at(a, i)); Out.ln; + end; +end; +end InlineCheck. diff --git a/test/run/InlineCheck.status b/test/run/InlineCheck.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/InlineCheck.status @@ -0,0 +1 @@ +2 diff --git a/test/run/InlineCheck.stderr b/test/run/InlineCheck.stderr new file mode 100644 index 0000000..a98d365 --- /dev/null +++ b/test/run/InlineCheck.stderr @@ -0,0 +1,2 @@ +test/run/mods/InlAt.raz:7:14: index 3 is out of range for a length of 3 + in InlineCheck.main at test/run/mods/InlAt.raz:7:14: i = 3 diff --git a/test/run/KeptCheck.raz b/test/run/KeptCheck.raz index 78e99ab..f2e870d 100644 --- a/test/run/KeptCheck.raz +++ b/test/run/KeptCheck.raz @@ -10,10 +10,13 @@ return a[i] + I64(b); end; +(* called through a procedure value, so that at isn't made in place *) proc main*; +var f: proc(i: INT; b: U8): I64; begin - Out.int(at(3, 1)); Out.ln; - Out.int(at(4, 200)); Out.ln; + f := at; + Out.int(f(3, 1)); Out.ln; + Out.int(f(4, 200)); Out.ln; end; end KeptCheck. diff --git a/test/run/KeptCheck.stderr b/test/run/KeptCheck.stderr index 7b7733d..a6b19e1 100644 --- a/test/run/KeptCheck.stderr +++ b/test/run/KeptCheck.stderr @@ -1,3 +1,3 @@ test/run/KeptCheck.raz:10:14: index 4 is out of range for a length of 4 in KeptCheck.at at test/run/KeptCheck.raz:10:14: i = 4, b = 200 - in KeptCheck.main at test/run/KeptCheck.raz:16:5 + in KeptCheck.main at test/run/KeptCheck.raz:19:5: f = 0x? diff --git a/test/run/KeptNil.raz b/test/run/KeptNil.raz index 75b2970..570631e 100644 --- a/test/run/KeptNil.raz +++ b/test/run/KeptNil.raz @@ -10,12 +10,14 @@ return p^.x + k; end; +(* called through a procedure value, so that get isn't made in place *) proc main*; -var r: Rec; +var r: Rec; f: proc(p: ^Rec; k: INT): INT; begin r.x := 1; - Out.int(get(ADR(r), 2)); Out.ln; - Out.int(get(nil, 5)); Out.ln; + f := get; + Out.int(f(ADR(r), 2)); Out.ln; + Out.int(f(nil, 5)); Out.ln; end; end KeptNil. diff --git a/test/run/KeptNil.stderr b/test/run/KeptNil.stderr index 3ea206f..28c861b 100644 --- a/test/run/KeptNil.stderr +++ b/test/run/KeptNil.stderr @@ -1,3 +1,3 @@ test/run/KeptNil.raz:10:5: stopped by a nil pointer in KeptNil.get at test/run/KeptNil.raz:10:5: p = nil, k = 5 - in KeptNil.main at test/run/KeptNil.raz:18:5 + in KeptNil.main at test/run/KeptNil.raz:20:5: f = 0x? diff --git a/test/run/mods/InlAt.raz b/test/run/mods/InlAt.raz new file mode 100644 index 0000000..2521e73 --- /dev/null +++ b/test/run/mods/InlAt.raz @@ -0,0 +1,10 @@ +(* A procedure that only returns, made in place in InlineCheck: its + check is told here, at its own line. *) +module InlAt; + +proc at*(s: []INT; i: INT): INT; +begin + return s[i]; +end; + +end InlAt.