raz / commit 71fdfd58

commit
71fdfd58d1cd11e543d4d7a33457edc46adee036
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
aaad64d2
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.

patch · log

9 files changed, +111 -9

PLAN.md (history) modified +1 -1

@@ -425,7 +425,7 @@
425425
426426 - [x] 42. The report, version 2026-10.3: sections 2, 3, 6, 6.5, 7.3, 8
427427 and 11, and the list of versions. Approved before any code.
428-- [ ] 43. `return f(x);`: the call writes its results through the
428+- [x] 43. `return f(x);`: the call's results are copied behind the
429429 procedure's own hidden pointer, the one its caller gave it. The
430430 results must be the procedure's, type for type; `test/err` for
431431 a mismatch, `test/run` for one, two and three results, through

doc/compiler.md (history) modified +6 -4

@@ -10,10 +10,10 @@
1010 |---|---|---|---|
1111 | `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 705 | 567 | Symbols, literals, comments, positions |
13-| `src/sym.pas` | 583 | 483 | Types, objects, scopes, layout |
13+| `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout |
1414 | `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder |
15-| `src/gen.pas` | 1,538 | 1,332 | Items: loading, storing, copying, frames, calls |
16-| `src/parse.pas` | 2,282 | 2,062 | Declarations, statements, expressions, built-ins, recovery, warnings |
15+| `src/gen.pas` | 1,547 | 1,339 | Items: loading, storing, copying, frames, calls |
16+| `src/parse.pas` | 2,293 | 2,072 | Declarations, statements, expressions, built-ins, recovery, warnings |
1717 | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places |
1818 | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
1919 | `src/raz.pas` | 227 | 178 | The command line, the entry points |
@@ -289,7 +289,9 @@
289289 only in `EndCall` are they loaded into the registers System V wants,
290290 and the stack aligned for the call. A record passed by value goes as
291291 the address of a copy; one returned comes back through a hidden pointer
292-in `rdi`, as C's structs do, and so do several results.
292+in `rdi`, as C's structs do, and so do several results. `return
293+f(x);` passing on f's several results copies them, with `ReturnAll`,
294+from the call's temporary to the place its own hidden pointer names.
293295
294296 Frames: `BeginProc` gives the parameters their slots; `EnterBody`
295297 writes the prologue, which zeroes the locals (with a few stores, or a

src/gen.pas modified +9 -0

@@ -118,6 +118,7 @@
118118 procedure EnterBody(c, line: longint);
119119 procedure ReturnValue(var x: TItem);
120120 procedure ReturnField(f: PObj; var x: TItem);
121+procedure ReturnAll(var x: TItem);
121122 procedure ReturnJump;
122123 procedure ReturnNone;
123124 procedure Pin(var x: TItem);
@@ -1434,6 +1435,14 @@
14341435 Lea(RDI, Mem(RDI, f^.val));
14351436 Copy(f^.typ^.size);
14361437 end;
1438+end;
1439+
1440+{ Several results at once, the call's that return f(x) passes on. }
1441+procedure ReturnAll(var x: TItem);
1442+begin
1443+ Address(x, RSI);
1444+ Load(RDI, Mem(RBP, retSlot), 8, false);
1445+ Copy(result^.size);
14371446 end;
14381447
14391448 { With the result in rax, as ReturnValue left it. }

src/parse.pas modified +13 -2

@@ -55,6 +55,7 @@
5555 fwdPos: array[0..255] of TPos; { where each names its type }
5656 numFwd: longint;
5757 lastCalled: PObj; { the procedure the last call was of, if it had a name }
58+ passOn: boolean; { the next factor may be a call with several results, for return }
5859 curProc: PObj; { the procedure being compiled, or nil }
5960 declExported: boolean; { the export mark of the declaration being read }
6061 curLoop: PLoop; { the innermost loop, or nil }
@@ -1013,8 +1014,10 @@
10131014 end;
10141015
10151016 procedure Factor(var x: TItem; want: PType);
1016-var called, vol: boolean; t: PType; start: TPos; s: shortstring;
1017+var called, vol, several: boolean; t: PType; start: TPos; s: shortstring;
10171018 begin
1019+ several := passOn;
1020+ passOn := false;
10181021 start := TokPos;
10191022 case tok of
10201023 sInt, sChar: begin
@@ -1030,7 +1033,7 @@
10301033 if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure';
10311034 MarkAt(x.pos, s + ' returns nothing');
10321035 end;
1033- if x.typ^.tuple then begin
1036+ if x.typ^.tuple and not several then begin
10341037 if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure';
10351038 MarkHelp(x.pos, s + ' returns several results', 'they go to variables: a, b := ' + s + '(...)');
10361039 end;
@@ -1362,12 +1365,20 @@
13621365 end;
13631366 end;
13641367
1368+{ return a, b; or return f(x); with f's several results passed on }
13651369 procedure ReturnResults;
13661370 var f: PObj; x: TItem;
13671371 begin
13681372 f := curResult^.fields;
1373+ passOn := true;
13691374 while f <> nil do begin
13701375 Expression(x, f^.typ);
1376+ if x.typ^.tuple then begin
1377+ if not Same(x.typ, curResult) then
1378+ MarkAt(x.pos, 'expected the results ' + TypeName(curResult) + ', found ' + TypeName(x.typ));
1379+ ReturnAll(x);
1380+ exit;
1381+ end;
13711382 ReturnField(f, x);
13721383 f := f^.next;
13731384 if f <> nil then Expect(sComma);

src/sym.pas modified +12 -2

@@ -436,7 +436,7 @@
436436 end;
437437
438438 function TypeName(t: PType): shortstring;
439-var s: shortstring;
439+var s: shortstring; f: PObj;
440440 begin
441441 if t = nil then exit('nothing');
442442 if t^.obj <> nil then begin
@@ -453,7 +453,17 @@
453453 else TypeName := '^' + TypeName(t^.base);
454454 fArray: begin Str(t^.len, s); TypeName := '[' + s + ']' + TypeName(t^.base) end;
455455 fSlice: TypeName := '[]' + TypeName(t^.base);
456- fRecord: TypeName := 'record';
456+ fRecord: if not t^.tuple then TypeName := 'record'
457+ else begin
458+ s := '(';
459+ f := t^.fields;
460+ while f <> nil do begin
461+ s := s + TypeName(f^.typ);
462+ f := f^.next;
463+ if f <> nil then s := s + ', ';
464+ end;
465+ TypeName := s + ')';
466+ end;
457467 fVariant: TypeName := 'variant';
458468 fProc: TypeName := 'proc';
459469 else TypeName := '?';

test/err/PassOnTypes.err added +4 -0

@@ -0,0 +1,4 @@
1+test/err/PassOnTypes.raz:10:12: error: expected the results (BOOL, I64), found (I64, BOOL)
2+ 10 | return two(n);
3+ | ^^^^^^
4+raz: 1 error

test/err/PassOnTypes.raz added +19 -0

@@ -0,0 +1,19 @@
1+module PassOnTypes;
2+
3+proc two(n: I64): (I64, BOOL);
4+begin
5+ return n, n = 0;
6+end;
7+
8+proc swapped(n: I64): (BOOL, I64);
9+begin
10+ return two(n);
11+end;
12+
13+proc main*;
14+var b: BOOL; i: I64;
15+begin
16+ b, i := swapped(1);
17+end;
18+
19+end PassOnTypes.

test/run/PassOn.expect added +5 -0

@@ -0,0 +1,5 @@
1+42
2+2 whole
3+2 part
4+3 part
5+65 66 67 -65

test/run/PassOn.raz added +42 -0

@@ -0,0 +1,42 @@
1+module PassOn;
2+import Out;
3+
4+type Pair = record a, b: I64; end;
5+
6+proc one(n: I64): I64;
7+begin
8+ return n * 2;
9+end;
10+
11+proc two(n: I64): (I64, BOOL);
12+begin
13+ return n div 7, n mod 7 = 0;
14+end;
15+
16+proc three(n: I64): (U8, Pair, I64);
17+begin
18+ return U8(n), {a: n + 1, b: n + 2}, -n;
19+end;
20+
21+(* each passes on what the one below returns *)
22+proc one1(n: I64): I64; begin return one(n); end;
23+proc one2(n: I64): I64; begin return one1(n); end;
24+proc two1(n: I64): (I64, BOOL); begin return two(n + 1); end;
25+proc two2(n: I64): (I64, BOOL); begin return two1(n + 1); end;
26+proc three1(n: I64): (U8, Pair, I64); begin return three(n); end;
27+proc three2(n: I64): (U8, Pair, I64); begin return three1(n); end;
28+
29+proc main*;
30+var i, q: I64; z: BOOL; c: U8; p: Pair;
31+begin
32+ Out.int(one2(21)); Out.ln;
33+ for i := 0 to 2 do
34+ q, z := two2(i * 5 + 12);
35+ Out.int(q); Out.str(" "); if z then Out.str("whole"); else Out.str("part"); end; Out.ln;
36+ end;
37+ c, p, i := three2(65);
38+ Out.int(I64(c)); Out.str(" "); Out.int(p.a); Out.str(" "); Out.int(p.b);
39+ Out.str(" "); Out.int(i); Out.ln;
40+end;
41+
42+end PassOn.