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.
| @@ -425,7 +425,7 @@ | ||
| 425 | 425 | |
| 426 | 426 | - [x] 42. The report, version 2026-10.3: sections 2, 3, 6, 6.5, 7.3, 8 |
| 427 | 427 | 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 | |
| 429 | 429 | procedure's own hidden pointer, the one its caller gave it. The |
| 430 | 430 | results must be the procedure's, type for type; `test/err` for |
| 431 | 431 | a mismatch, `test/run` for one, two and three results, through |
| @@ -10,10 +10,10 @@ | ||
| 10 | 10 | |---|---|---|---| |
| 11 | 11 | | `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts | |
| 12 | 12 | | `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 | | |
| 14 | 14 | | `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 | | |
| 17 | 17 | | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| 19 | 19 | | `src/raz.pas` | 227 | 178 | The command line, the entry points | |
| @@ -289,7 +289,9 @@ | ||
| 289 | 289 | only in `EndCall` are they loaded into the registers System V wants, |
| 290 | 290 | and the stack aligned for the call. A record passed by value goes as |
| 291 | 291 | 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. | |
| 293 | 295 | |
| 294 | 296 | Frames: `BeginProc` gives the parameters their slots; `EnterBody` |
| 295 | 297 | writes the prologue, which zeroes the locals (with a few stores, or a |
| @@ -118,6 +118,7 @@ | ||
| 118 | 118 | procedure EnterBody(c, line: longint); |
| 119 | 119 | procedure ReturnValue(var x: TItem); |
| 120 | 120 | procedure ReturnField(f: PObj; var x: TItem); |
| 121 | +procedure ReturnAll(var x: TItem); | |
| 121 | 122 | procedure ReturnJump; |
| 122 | 123 | procedure ReturnNone; |
| 123 | 124 | procedure Pin(var x: TItem); |
| @@ -1434,6 +1435,14 @@ | ||
| 1434 | 1435 | Lea(RDI, Mem(RDI, f^.val)); |
| 1435 | 1436 | Copy(f^.typ^.size); |
| 1436 | 1437 | 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); | |
| 1437 | 1446 | end; |
| 1438 | 1447 | |
| 1439 | 1448 | { With the result in rax, as ReturnValue left it. } |
| @@ -55,6 +55,7 @@ | ||
| 55 | 55 | fwdPos: array[0..255] of TPos; { where each names its type } |
| 56 | 56 | numFwd: longint; |
| 57 | 57 | 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 } | |
| 58 | 59 | curProc: PObj; { the procedure being compiled, or nil } |
| 59 | 60 | declExported: boolean; { the export mark of the declaration being read } |
| 60 | 61 | curLoop: PLoop; { the innermost loop, or nil } |
| @@ -1013,8 +1014,10 @@ | ||
| 1013 | 1014 | end; |
| 1014 | 1015 | |
| 1015 | 1016 | 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; | |
| 1017 | 1018 | begin |
| 1019 | + several := passOn; | |
| 1020 | + passOn := false; | |
| 1018 | 1021 | start := TokPos; |
| 1019 | 1022 | case tok of |
| 1020 | 1023 | sInt, sChar: begin |
| @@ -1030,7 +1033,7 @@ | ||
| 1030 | 1033 | if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure'; |
| 1031 | 1034 | MarkAt(x.pos, s + ' returns nothing'); |
| 1032 | 1035 | end; |
| 1033 | - if x.typ^.tuple then begin | |
| 1036 | + if x.typ^.tuple and not several then begin | |
| 1034 | 1037 | if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure'; |
| 1035 | 1038 | MarkHelp(x.pos, s + ' returns several results', 'they go to variables: a, b := ' + s + '(...)'); |
| 1036 | 1039 | end; |
| @@ -1362,12 +1365,20 @@ | ||
| 1362 | 1365 | end; |
| 1363 | 1366 | end; |
| 1364 | 1367 | |
| 1368 | +{ return a, b; or return f(x); with f's several results passed on } | |
| 1365 | 1369 | procedure ReturnResults; |
| 1366 | 1370 | var f: PObj; x: TItem; |
| 1367 | 1371 | begin |
| 1368 | 1372 | f := curResult^.fields; |
| 1373 | + passOn := true; | |
| 1369 | 1374 | while f <> nil do begin |
| 1370 | 1375 | 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; | |
| 1371 | 1382 | ReturnField(f, x); |
| 1372 | 1383 | f := f^.next; |
| 1373 | 1384 | if f <> nil then Expect(sComma); |
| @@ -436,7 +436,7 @@ | ||
| 436 | 436 | end; |
| 437 | 437 | |
| 438 | 438 | function TypeName(t: PType): shortstring; |
| 439 | -var s: shortstring; | |
| 439 | +var s: shortstring; f: PObj; | |
| 440 | 440 | begin |
| 441 | 441 | if t = nil then exit('nothing'); |
| 442 | 442 | if t^.obj <> nil then begin |
| @@ -453,7 +453,17 @@ | ||
| 453 | 453 | else TypeName := '^' + TypeName(t^.base); |
| 454 | 454 | fArray: begin Str(t^.len, s); TypeName := '[' + s + ']' + TypeName(t^.base) end; |
| 455 | 455 | 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; | |
| 457 | 467 | fVariant: TypeName := 'variant'; |
| 458 | 468 | fProc: TypeName := 'proc'; |
| 459 | 469 | else TypeName := '?'; |
| @@ -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 |
| @@ -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. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +42 | |
| 2 | +2 whole | |
| 3 | +2 part | |
| 4 | +3 part | |
| 5 | +65 66 67 -65 |
| @@ -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. |