From 71fdfd58d1cd11e543d4d7a33457edc46adee036 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:32:26 +0100 Subject: [PATCH] return f(x) passes on f's several results Report 2026-10.3, 7.3 and 8. The first factor after return in a procedure with several results may be a call with several results; if it is, its types must be the procedure's, in order, and ReturnAll copies them behind the procedure's own hidden pointer. TypeName names several results as (I64, BOOL). test/run/PassOn: one, two and three results through two levels; test/err/PassOnTypes: a mismatch. --- diff --git a/PLAN.md b/PLAN.md index 1caf7db..e8bab83 100644 --- a/PLAN.md +++ b/PLAN.md @@ -425,7 +425,7 @@ - [x] 42. The report, version 2026-10.3: sections 2, 3, 6, 6.5, 7.3, 8 and 11, and the list of versions. Approved before any code. -- [ ] 43. `return f(x);`: the call writes its results through the +- [x] 43. `return f(x);`: the call's results are copied behind the procedure's own hidden pointer, the one its caller gave it. The results must be the procedure's, type for type; `test/err` for a mismatch, `test/run` for one, two and three results, through diff --git a/doc/compiler.md b/doc/compiler.md index aa9bfe6..192cb16 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -10,10 +10,10 @@ |---|---|---|---| | `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 705 | 567 | Symbols, literals, comments, positions | -| `src/sym.pas` | 583 | 483 | Types, objects, scopes, layout | +| `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout | | `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder | -| `src/gen.pas` | 1,538 | 1,332 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,282 | 2,062 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/gen.pas` | 1,547 | 1,339 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,293 | 2,072 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 227 | 178 | The command line, the entry points | @@ -289,7 +289,9 @@ only in `EndCall` are they loaded into the registers System V wants, and the stack aligned for the call. A record passed by value goes as the address of a copy; one returned comes back through a hidden pointer -in `rdi`, as C's structs do, and so do several results. +in `rdi`, as C's structs do, and so do several results. `return +f(x);` passing on f's several results copies them, with `ReturnAll`, +from the call's temporary to the place its own hidden pointer names. Frames: `BeginProc` gives the parameters their slots; `EnterBody` writes the prologue, which zeroes the locals (with a few stores, or a diff --git a/src/gen.pas b/src/gen.pas index ebe2aea..5c8de08 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -118,6 +118,7 @@ procedure EnterBody(c, line: longint); procedure ReturnValue(var x: TItem); procedure ReturnField(f: PObj; var x: TItem); +procedure ReturnAll(var x: TItem); procedure ReturnJump; procedure ReturnNone; procedure Pin(var x: TItem); @@ -1434,6 +1435,14 @@ Lea(RDI, Mem(RDI, f^.val)); Copy(f^.typ^.size); end; +end; + +{ Several results at once, the call's that return f(x) passes on. } +procedure ReturnAll(var x: TItem); +begin + Address(x, RSI); + Load(RDI, Mem(RBP, retSlot), 8, false); + Copy(result^.size); end; { With the result in rax, as ReturnValue left it. } diff --git a/src/parse.pas b/src/parse.pas index 0f2e68b..34ad4ef 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -55,6 +55,7 @@ fwdPos: array[0..255] of TPos; { where each names its type } numFwd: longint; lastCalled: PObj; { the procedure the last call was of, if it had a name } + passOn: boolean; { the next factor may be a call with several results, for return } curProc: PObj; { the procedure being compiled, or nil } declExported: boolean; { the export mark of the declaration being read } curLoop: PLoop; { the innermost loop, or nil } @@ -1013,8 +1014,10 @@ end; procedure Factor(var x: TItem; want: PType); -var called, vol: boolean; t: PType; start: TPos; s: shortstring; +var called, vol, several: boolean; t: PType; start: TPos; s: shortstring; begin + several := passOn; + passOn := false; start := TokPos; case tok of sInt, sChar: begin @@ -1030,7 +1033,7 @@ if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure'; MarkAt(x.pos, s + ' returns nothing'); end; - if x.typ^.tuple then begin + if x.typ^.tuple and not several then begin if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure'; MarkHelp(x.pos, s + ' returns several results', 'they go to variables: a, b := ' + s + '(...)'); end; @@ -1362,12 +1365,20 @@ end; end; +{ return a, b; or return f(x); with f's several results passed on } procedure ReturnResults; var f: PObj; x: TItem; begin f := curResult^.fields; + passOn := true; while f <> nil do begin Expression(x, f^.typ); + if x.typ^.tuple then begin + if not Same(x.typ, curResult) then + MarkAt(x.pos, 'expected the results ' + TypeName(curResult) + ', found ' + TypeName(x.typ)); + ReturnAll(x); + exit; + end; ReturnField(f, x); f := f^.next; if f <> nil then Expect(sComma); diff --git a/src/sym.pas b/src/sym.pas index cc0ae56..d790247 100644 --- a/src/sym.pas +++ b/src/sym.pas @@ -436,7 +436,7 @@ end; function TypeName(t: PType): shortstring; -var s: shortstring; +var s: shortstring; f: PObj; begin if t = nil then exit('nothing'); if t^.obj <> nil then begin @@ -453,7 +453,17 @@ else TypeName := '^' + TypeName(t^.base); fArray: begin Str(t^.len, s); TypeName := '[' + s + ']' + TypeName(t^.base) end; fSlice: TypeName := '[]' + TypeName(t^.base); - fRecord: TypeName := 'record'; + fRecord: if not t^.tuple then TypeName := 'record' + else begin + s := '('; + f := t^.fields; + while f <> nil do begin + s := s + TypeName(f^.typ); + f := f^.next; + if f <> nil then s := s + ', '; + end; + TypeName := s + ')'; + end; fVariant: TypeName := 'variant'; fProc: TypeName := 'proc'; else TypeName := '?'; diff --git a/test/err/PassOnTypes.err b/test/err/PassOnTypes.err new file mode 100644 index 0000000..a4b51f3 --- /dev/null +++ b/test/err/PassOnTypes.err @@ -0,0 +1,4 @@ +test/err/PassOnTypes.raz:10:12: error: expected the results (BOOL, I64), found (I64, BOOL) + 10 | return two(n); + | ^^^^^^ +raz: 1 error diff --git a/test/err/PassOnTypes.raz b/test/err/PassOnTypes.raz new file mode 100644 index 0000000..3133598 --- /dev/null +++ b/test/err/PassOnTypes.raz @@ -0,0 +1,19 @@ +module PassOnTypes; + +proc two(n: I64): (I64, BOOL); +begin + return n, n = 0; +end; + +proc swapped(n: I64): (BOOL, I64); +begin + return two(n); +end; + +proc main*; +var b: BOOL; i: I64; +begin + b, i := swapped(1); +end; + +end PassOnTypes. diff --git a/test/run/PassOn.expect b/test/run/PassOn.expect new file mode 100644 index 0000000..e9604eb --- /dev/null +++ b/test/run/PassOn.expect @@ -0,0 +1,5 @@ +42 +2 whole +2 part +3 part +65 66 67 -65 diff --git a/test/run/PassOn.raz b/test/run/PassOn.raz new file mode 100644 index 0000000..00f4e7d --- /dev/null +++ b/test/run/PassOn.raz @@ -0,0 +1,42 @@ +module PassOn; +import Out; + +type Pair = record a, b: I64; end; + +proc one(n: I64): I64; +begin + return n * 2; +end; + +proc two(n: I64): (I64, BOOL); +begin + return n div 7, n mod 7 = 0; +end; + +proc three(n: I64): (U8, Pair, I64); +begin + return U8(n), {a: n + 1, b: n + 2}, -n; +end; + +(* each passes on what the one below returns *) +proc one1(n: I64): I64; begin return one(n); end; +proc one2(n: I64): I64; begin return one1(n); end; +proc two1(n: I64): (I64, BOOL); begin return two(n + 1); end; +proc two2(n: I64): (I64, BOOL); begin return two1(n + 1); end; +proc three1(n: I64): (U8, Pair, I64); begin return three(n); end; +proc three2(n: I64): (U8, Pair, I64); begin return three1(n); end; + +proc main*; +var i, q: I64; z: BOOL; c: U8; p: Pair; +begin + Out.int(one2(21)); Out.ln; + for i := 0 to 2 do + q, z := two2(i * 5 + 12); + Out.int(q); Out.str(" "); if z then Out.str("whole"); else Out.str("part"); end; Out.ln; + end; + c, p, i := three2(65); + Out.int(I64(c)); Out.str(" "); Out.int(p.a); Out.str(" "); Out.int(p.b); + Out.str(" "); Out.int(i); Out.ln; +end; + +end PassOn.