raz / commit 88e6b860

commit
88e6b860d768f019a8b7d5b43129fcbade500814
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0af18683
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

patch · log

23 files changed, +514 -69

CLAUDE.md modified +12 -0

@@ -399,3 +399,15 @@
399399 or another module's array, as `d`, says why it can't be changed, for
400400 `COPY` too. `seqSlice` copies no slice that is a temporary already
401401 (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.

conform/run/Inline.expect (history) added +19 -0

@@ -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

conform/run/Inline.raz (history) added +130 -0

@@ -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.

conform/run/mods/Cell.raz (history) added +9 -0

@@ -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.

conform/run/mods/Inl.raz (history) added +25 -0

@@ -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.

doc/compiler.md (history) modified +82 -5

@@ -18,12 +18,12 @@
1818 |---|---|---|---|
1919 | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts |
2020 | `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 |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `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 |
2525 | `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 |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -937,7 +937,79 @@
937937 before. The prologue saves those registers and moves the parameter
938938 there, as it does for a procedure that calls. ChaCha's block with
939939 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.
940978
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+
9411013 ## Diag.raz: messages
9421014
9431015 Every message the compiler prints is made here. A position, `Pos`, is a
@@ -1376,7 +1448,10 @@
13761448 a stub for its kind of failure (`failStubs`, made by `entryPoint`),
13771449 which says the kind and passes the address it was called from, by which
13781450 `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
13801455 were, so that the stub can pass them on, for the trap report to show a
13811456 parameter kept in them. A check costs a compare and a jump where it
13821457 stands, and a dozen bytes out of the way. Code is aligned to 32 bytes,
@@ -1698,7 +1773,9 @@
16981773 calls the matching routine in `Item.raz` on the item it has. The
16991774 `want` parameter carries the type an expression must have, where it is
17001775 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").
17021779
17031780 Then statements: `statement` makes each one a recovery point (below)
17041781 and `oneStatement` dispatches; `ifStatement`,

doc/raz-tutorial.7 (history) modified +7 -7

@@ -1008,17 +1008,17 @@
10081008 module Fail;
10091009 import Out;
10101010
1011-proc get(a: []INT; i: INT): INT;
1011+proc show(a: []INT; i: INT);
10121012 begin
1013- return a[i];
1013+ Out.int(a[i]);
1014+ Out.ln;
10141015 end;
10151016
10161017 proc main*;
10171018 var a: [3]INT; i: INT;
10181019 begin
10191020 for i := 0 to 3 do
1020- Out.int(get(a, i));
1021- Out.ln;
1021+ show(a, i);
10221022 end;
10231023 end;
10241024
@@ -1028,9 +1028,9 @@
10281028 0
10291029 0
10301030 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
10341034 .Ed
10351035 .Pp
10361036 Every index and slice is checked.

src/Gen.raz modified +13 -8

@@ -185,7 +185,9 @@
185185
186186 (* a check of the procedure, whose failure endFails makes *)
187187 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) *)
189191 m: X.Mem;
190192 n: I64;
191193 end;
@@ -1589,9 +1591,9 @@
15891591 in them, for the trap report to show: so a goes to the stub in rax.
15901592 The failing code is out of the way, at the
15911593 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. *)
15951597 proc check(cc, what: INT; p: Diag.Pos; ra: INT; m: X.Mem; n: I64; name: INT);
15961598 begin
15971599 if numFails > LEN(fails) - 1 then
@@ -1605,7 +1607,8 @@
16051607 fails[numFails].m := m;
16061608 fails[numFails].n := n;
16071609 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);
16091612 INC(numFails);
16101613 end;
16111614
@@ -1613,13 +1616,15 @@
16131616 and the place of the offending expression, through a stub for its
16141617 kind, which takes the place from where it was called *)
16151618 proc endFails;
1616-var i, line, col: INT; f: Fail;
1619+var i, path: INT; f: Fail;
16171620 begin
1621+ path := Chunk.curFilePath();
16181622 for i := 0 to numFails - 1 do
16191623 f := fails[i];
16201624 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);
16231628 if f.ra # RAX then X.movRR(RAX, f.ra); end;
16241629 if f.m.base = NoBase then X.movRI(RCX, f.n);
16251630 elsif f.m.base = ARegister then X.movRR(RCX, f.m.index);

src/Parse.raz (history) modified +138 -4

@@ -138,6 +138,12 @@
138138 in the procedure (chooseRegs); numFors counts them as compiled. *)
139139 limitK, numFors: INT;
140140 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;
141147
142148 proc expression(var x: TItem; want: PType): ERROR; forward;
143149 proc parseType: (PType, ERROR); forward;
@@ -958,6 +964,104 @@
958964
959965 (* Expressions *)
960966
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+
9611065 proc call(var x: TItem): ERROR;
9621066 var c: Gen.CallInfo; a: TItem; want: PType;
9631067 begin
@@ -973,6 +1077,11 @@
9731077 until false;
9741078 end;
9751079 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);
9761085 return Item.endCall(c, x);
9771086 end;
9781087
@@ -1634,6 +1743,7 @@
16341743 begin
16351744 start := Scan.tokPos();
16361745 Scan.next;
1746+ sawTry := true;
16371747 if deferring then return Scan.markAt(Scan.spanFrom(start), "try can't leave a deferred statement"); end;
16381748 if curResult = nil then
16391749 return Scan.markAt(Scan.spanFrom(start), "try needs a procedure that ends in an ERROR result, and this returns nothing");
@@ -2782,6 +2892,10 @@
27822892 end;
27832893 Gen.returnJump();
27842894 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;
27852899 end;
27862900 | sBreak:
27872901 Scan.next;
@@ -2958,12 +3072,13 @@
29583072 end;
29593073 end;
29603074
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,
29623077 or NEW, SYSCALL, COPY or FILL, which use the registers a call would *)
29633078 proc calls(o: PObj): BOOL;
29643079 begin
29653080 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))
29673082 or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy)
29683083 or (o^.val = Sym.bFill)));
29693084 end;
@@ -3196,10 +3311,23 @@
31963311 Gen.useRegs(regsOf(params, before));
31973312 end;
31983313
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+
31993327 proc body(p: PObj; sig: PType; line: INT): ERROR;
32003328 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;
32033331 begin
32043332 try Sym.openScope(Sym.newScope());
32053333 params := nil; last := nil;
@@ -3238,7 +3366,10 @@
32383366 curResult := sig^.base;
32393367 Gen.enterBody(p^.chunk, line);
32403368 stray := false; elseIf := false; deferSlot := 0; numFors := 0;
3369+ first := Scan.tok = Scan.sReturn;
3370+ Scan.saveScan(at); returnEnd := -1; sawTry := false; calls0 := numCalls;
32413371 try statements();
3372+ only := first and (Scan.tok = Scan.sEnd) and (Scan.afterPrev().a = returnEnd) and not sawTry;
32423373 p^.openEnd := (curResult # nil) and not ends;
32433374 Chunk.noteLine(Scan.curLine(), Scan.curCol());
32443375 try expect(Scan.sEnd);
@@ -3258,6 +3389,9 @@
32583389 Scan.restoreScan(st);
32593390 until false;
32603391 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;
32613395 (* its parameters and variables, for the trap report *)
32623396 o := firstLocal(before);
32633397 q := params;

src/Sym.raz (history) modified +13 -0

@@ -43,6 +43,17 @@
4343 home*: ^Module; (* the module that built it *)
4444 end;
4545
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+
4657 Obj* = record
4758 cls*: Class;
4859 name*: INT;
@@ -69,6 +80,8 @@
6980 xmm0, made in place of a call (Gen.oneRealOp);
7081 0 if not *)
7182 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 *)
7285 reg*: INT; (* oVar: the register it lives in, or 0 if in memory;
7386 for a slice, the one its pointer is in *)
7487 reg2*: INT; (* oVar: a slice in registers: the one its length is in *)

test/list/Listing.expect (history) modified +21 -33

@@ -3,32 +3,17 @@
33 0 lea rax, [rdi + rdi]
44 4 ret
55
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-
216 Listing.main:
227 17 proc main*;
238 0 push rbp
249 1 mov rbp, rsp
25- 4 sub rsp, 16
10+ 4 sub rsp, 32
2611 11 mov [rbp - 8], rbx
2712 15 xor ebx, ebx
2813 20 for i := 1 to 3 do
2914 17 mov rbx, 1
3015 22 cmp rbx, 3
31- 26 jg 156
16+ 26 jg 168
3217 32 jmp 68
3318 37 nop
3419 45 nop
@@ -37,25 +22,28 @@
3722 64 add rbx, 1
3823 21 if i # 2 then total := add(total, twice(i)); end;
3924 68 cmp rbx, 2
40- 72 je 122
25+ 72 je 134
4126 78 mov r8, [Listing.total]
4227 85 mov [rbp - 16], r8
4328 89 mov rdi, rbx
4429 92 call Listing.twice
4530 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
5846 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

test/msg/ViewNegative.stderr (history) modified +1 -2

@@ -1,3 +1,2 @@
11 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

test/run/HeapSure.stderr (history) modified +0 -1

@@ -1,4 +1,3 @@
11 Heap: out of memory
22 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
43 in HeapSure.main at test/run/HeapSure.raz:8:5

test/run/IndexParam.stderr (history) modified +1 -2

@@ -1,3 +1,2 @@
11 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

test/run/InlineCheck.expect (history) added +3 -0

@@ -0,0 +1,3 @@
1+1
2+2
3+3

test/run/InlineCheck.raz (history) added +15 -0

@@ -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.

test/run/InlineCheck.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/InlineCheck.stderr (history) added +2 -0

@@ -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

test/run/KeptCheck.raz (history) modified +5 -2

@@ -10,10 +10,13 @@
1010 return a[i] + I64(b);
1111 end;
1212
13+(* called through a procedure value, so that at isn't made in place *)
1314 proc main*;
15+var f: proc(i: INT; b: U8): I64;
1416 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;
1720 end;
1821
1922 end KeptCheck.

test/run/KeptCheck.stderr (history) modified +1 -1

@@ -1,3 +1,3 @@
11 test/run/KeptCheck.raz:10:14: index 4 is out of range for a length of 4
22 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?

test/run/KeptNil.raz (history) modified +5 -3

@@ -10,12 +10,14 @@
1010 return p^.x + k;
1111 end;
1212
13+(* called through a procedure value, so that get isn't made in place *)
1314 proc main*;
14-var r: Rec;
15+var r: Rec; f: proc(p: ^Rec; k: INT): INT;
1516 begin
1617 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;
1921 end;
2022
2123 end KeptNil.

test/run/KeptNil.stderr (history) modified +1 -1

@@ -1,3 +1,3 @@
11 test/run/KeptNil.raz:10:5: stopped by a nil pointer
22 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?

test/run/mods/InlAt.raz (history) added +10 -0

@@ -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.