Two words of results in rax and rdx v0.7.0 step 2, by the owner's word. A result of two words, a slice or two single values that aren't reals (InTwoRegs), comes back in rax and rdx as System V returns a struct of two integers, not through a hidden pointer: return leaves both on gen's stack for ReturnJump, and Call stores them in the caller's temporary. TCall.code goes, since a code procedure's two results now follow the same rule. PushWords pushes a slice's or a pair's two words, for arguments and results alike. rat: within noise of step 1 (547 ms both); a kernel of calls returning two results and a slice 9% faster (491 -> 447 ms); rat's text -0.1%. conform/run/TwoWords covers defers, passing on, procedure values and running off the end.
| @@ -129,8 +129,10 @@ | ||
| 129 | 129 | - `for`: the step is a constant; the loop never steps past the limit, so |
| 130 | 130 | it can't wrap; afterwards the variable holds the last value it took; |
| 131 | 131 | the body may assign it. Leave as is. |
| 132 | -- Several results come back through a hidden pointer; a code procedure | |
| 133 | - returns two in rax and rdx. | |
| 132 | +- Two results that are each a single value of a word at most, not a | |
| 133 | + real, come back in rax and rdx, and so does a slice, as from a code | |
| 134 | + procedure (owner's word, v0.7.0); other several results, and records, | |
| 135 | + through a hidden pointer. | |
| 134 | 136 | - `NEW(p, a)` requires `a: ^Mem.Allocator` and calls its `alloc` field: |
| 135 | 137 | the one library type the compiler knows by name. `Rt.fail` is |
| 136 | 138 | found by name too. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +7 -5 | |
| 2 | +F 4 | |
| 3 | +T 12 | |
| 4 | +5 22 | |
| 5 | +1 6 | |
| 6 | +llo | |
| 7 | +0 0 | |
| 8 | +14 111 |
| @@ -0,0 +1,61 @@ | ||
| 1 | +module TwoWords; | |
| 2 | + | |
| 3 | +(* Two results, and a slice, returned: with deferred statements, passed | |
| 4 | + on, through a procedure value, run off the end, and with a call in | |
| 5 | + the second. *) | |
| 6 | +import Out; | |
| 7 | + | |
| 8 | +var log: INT; | |
| 9 | + | |
| 10 | +proc note; begin log := log * 10 + 1; end; | |
| 11 | + | |
| 12 | +proc pair(a, b: I32): (I32, I32); | |
| 13 | +begin | |
| 14 | + defer note; | |
| 15 | + return b, a; | |
| 16 | +end; | |
| 17 | + | |
| 18 | +proc mixed(n: INT): (BOOL, I64); | |
| 19 | +begin | |
| 20 | + if n < 0 then return false, -n; end; | |
| 21 | + return true, n * 3; | |
| 22 | +end; | |
| 23 | + | |
| 24 | +proc twice(n: INT): INT; begin return 2 * n; end; | |
| 25 | + | |
| 26 | +proc nested(n: INT): (INT, INT); | |
| 27 | +begin | |
| 28 | + return n, twice(n) + twice(n + 1); | |
| 29 | +end; | |
| 30 | + | |
| 31 | +proc passOn(n: INT): (INT, INT); | |
| 32 | +begin | |
| 33 | + defer note; | |
| 34 | + return nested(n); | |
| 35 | +end; | |
| 36 | + | |
| 37 | +proc tail(s: []U8; i: INT): []U8; | |
| 38 | +begin | |
| 39 | + defer note; | |
| 40 | + return s[i..LEN(s)]; | |
| 41 | +end; | |
| 42 | + | |
| 43 | +proc fallOff(n: INT): (INT, INT); | |
| 44 | +begin | |
| 45 | + if n > 0 then return 1, 2; end; | |
| 46 | +end; | |
| 47 | + | |
| 48 | +proc main*; | |
| 49 | +var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, INT); | |
| 50 | +begin | |
| 51 | + x, y := pair(-5, 7); Out.int(I64(x)); Out.str(" "); Out.int(I64(y)); Out.ln; | |
| 52 | + b, w := mixed(-4); if b then Out.str("T"); else Out.str("F"); end; Out.str(" "); Out.int(w); Out.ln; | |
| 53 | + b, w := mixed(4); if b then Out.str("T"); else Out.str("F"); end; Out.str(" "); Out.int(w); Out.ln; | |
| 54 | + p, q := passOn(5); Out.int(p); Out.str(" "); Out.int(q); Out.ln; | |
| 55 | + h := nested; p, q := h(1); Out.int(p); Out.str(" "); Out.int(q); Out.ln; | |
| 56 | + Out.str(tail("hello", 2)); Out.ln; | |
| 57 | + p, q := fallOff(0); Out.int(p); Out.str(" "); Out.int(q); Out.ln; | |
| 58 | + _, v := nested(3); Out.int(v); Out.str(" "); Out.int(log); Out.ln; | |
| 59 | +end; | |
| 60 | + | |
| 61 | +end TwoWords. |
| @@ -10,12 +10,12 @@ | ||
| 10 | 10 | |---|---|---|---| |
| 11 | 11 | | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts | |
| 12 | 12 | | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | |
| 13 | -| `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout | | |
| 13 | +| `src/sym.pas` | 643 | 527 | Types, objects, scopes, layout | | |
| 14 | 14 | | `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes | |
| 15 | 15 | | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler | |
| 16 | -| `src/gen.pas` | 1,633 | 1,280 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 17 | -| `src/item.pas` | 1,444 | 1,204 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | -| `src/parse.pas` | 2,913 | 2,579 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 16 | +| `src/gen.pas` | 1,641 | 1,287 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 17 | +| `src/item.pas` | 1,467 | 1,223 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | +| `src/parse.pas` | 2,916 | 2,580 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 19 | 19 | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | |
| 20 | 20 | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | |
| 21 | 21 | | `src/raz.pas` | 197 | 147 | The command line | |
| @@ -353,9 +353,13 @@ | ||
| 353 | 353 | aligns the stack. A slice goes as its two words, in two registers or |
| 354 | 354 | both on the stack, as System V passes a struct of two integers, and |
| 355 | 355 | the procedure keeps them in its frame. A record |
| 356 | -passed by value goes as the address of a copy; one returned comes back | |
| 357 | -through a hidden pointer in `rdi`, as C's structs do, and so do several | |
| 358 | -results (`ResultTo`, `CopyResult`). | |
| 356 | +passed by value goes as the address of a copy. A result of two words, | |
| 357 | +a slice or two single values that aren't reals (`InTwoRegs`, in | |
| 358 | +`sym.pas`), comes back in rax and rdx, as System V returns a struct of | |
| 359 | +two integers: `return` leaves both on the stack for `ReturnJump`, and | |
| 360 | +`Call` stores them in a temporary of the caller's. Any other record or | |
| 361 | +several results come back through a hidden pointer in `rdi`, as C's | |
| 362 | +structs do (`BehindPointer`, `ResultTo`, `CopyResult`). | |
| 359 | 363 | |
| 360 | 364 | Frames: `BeginProc` gives the parameters their slots; `EnterBody` |
| 361 | 365 | writes the prologue, which zeroes the locals in memory (with a few |
| @@ -444,10 +448,11 @@ | ||
| 444 | 448 | conversion, `T(x)`. |
| 445 | 449 | |
| 446 | 450 | Calls: `BeginCall`, then `PassArg` for each argument, then `EndCall`, |
| 447 | -which has gen's `Call` make the call. Each argument is computed into a | |
| 448 | -frame slot of its own. `return f(x);` passing on f's several results | |
| 449 | -copies them, with `ReturnAll`, from the call's temporary to the place | |
| 450 | -its own hidden pointer names. | |
| 451 | +which has gen's `Call` make the call. Each argument is left on gen's | |
| 452 | +stack, a slice as its two words (`PushWords`). `return f(x);` passing | |
| 453 | +on f's several results, with `ReturnAll`, pushes them from the call's | |
| 454 | +temporary if they are two words, or else copies them to the place its | |
| 455 | +own hidden pointer names. | |
| 451 | 456 | |
| 452 | 457 | ## parse.pas: the parser |
| 453 | 458 | |
| @@ -678,8 +683,9 @@ | ||
| 678 | 683 | - **Registers for variables.** Up to five of a procedure's single |
| 679 | 684 | values, not reals, whose address is never taken, live in `rbx` and |
| 680 | 685 | `r12` to `r15`; the trap report shows them as it shows the others. |
| 681 | -- **Several results** come back through a hidden pointer to the | |
| 682 | - caller's slots; a code procedure returns two in rax and rdx. | |
| 686 | +- **Several results** that are two words, and a slice, come back in | |
| 687 | + rax and rdx, as from a code procedure; others through a hidden | |
| 688 | + pointer to the caller's slots. | |
| 683 | 689 | - **Reals.** A real constant is held as an F64. An `F32` in a register |
| 684 | 690 | is its low 32 bits; the upper ones are left as they fall. |
| 685 | 691 | - **How a failure is caught.** A division by zero, a stack overflow |
| @@ -74,9 +74,8 @@ | ||
| 74 | 74 | pair: array[0..63] of boolean; { ... is a slice's first word, the next its second } |
| 75 | 75 | target: int64; { the frame slot of a procedure value, if chunk < 0 } |
| 76 | 76 | chunk: longint; { else the procedure's chunk } |
| 77 | - code: boolean; { a code procedure: two results come in rax and rdx } | |
| 78 | 77 | ext: boolean; { C's: called through the chunk holding its address } |
| 79 | - ret: int64; { set by Call: the temporary an aggregate result is in } | |
| 78 | + ret: int64; { set by Call: the temporary a result of more than one word is in } | |
| 80 | 79 | end; |
| 81 | 80 | |
| 82 | 81 | const |
| @@ -1192,7 +1191,7 @@ | ||
| 1192 | 1191 | ni := 0; nf := 0; |
| 1193 | 1192 | if (res <> nil) and IsAggregate(res) then begin |
| 1194 | 1193 | c.ret := AllocTemp(res^.size, res^.align); |
| 1195 | - if not c.code then ni := 1; | |
| 1194 | + if BehindPointer(res) then ni := 1; | |
| 1196 | 1195 | end; |
| 1197 | 1196 | { the System V classes: each argument in the next free register of |
| 1198 | 1197 | its kind, or else on the stack } |
| @@ -1219,7 +1218,7 @@ | ||
| 1219 | 1218 | end; |
| 1220 | 1219 | ni := 0; nf := 0; |
| 1221 | 1220 | late[4] := -1; late[5] := -1; |
| 1222 | - if (c.ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; | |
| 1221 | + if (c.ret <> 0) and BehindPointer(res) then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; | |
| 1223 | 1222 | { rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as |
| 1224 | 1223 | they may hold arguments still } |
| 1225 | 1224 | for i := 0 to c.n - 1 do |
| @@ -1247,10 +1246,14 @@ | ||
| 1247 | 1246 | CallR(R11); |
| 1248 | 1247 | end; |
| 1249 | 1248 | if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); |
| 1250 | - if (c.ret <> 0) and c.code then begin | |
| 1251 | - Store(Mem(RBP, c.ret), RAX, 8); | |
| 1252 | - Store(Mem(RBP, c.ret + 8), RDX, 8); | |
| 1253 | - end; | |
| 1249 | + if (c.ret <> 0) and InTwoRegs(res) then | |
| 1250 | + if res^.form = fSlice then begin | |
| 1251 | + Store(Mem(RBP, c.ret), RAX, 8); | |
| 1252 | + Store(Mem(RBP, c.ret + 8), RDX, 8); | |
| 1253 | + end else begin | |
| 1254 | + Store(Mem(RBP, c.ret + res^.fields^.val), RAX, res^.fields^.typ^.size); | |
| 1255 | + Store(Mem(RBP, c.ret + res^.fields^.next^.val), RDX, res^.fields^.next^.typ^.size); | |
| 1256 | + end; | |
| 1254 | 1257 | if (res <> nil) and (c.ret = 0) then begin |
| 1255 | 1258 | r := Target; |
| 1256 | 1259 | if res^.form = fReal then MovqRX(r, 0) |
| @@ -1313,7 +1316,7 @@ | ||
| 1313 | 1316 | procParams := params; |
| 1314 | 1317 | regMask := 0; |
| 1315 | 1318 | ni := 0; nf := 0; stackOff := 16; |
| 1316 | - if (resType <> nil) and IsAggregate(resType) then begin | |
| 1319 | + if (resType <> nil) and BehindPointer(resType) then begin | |
| 1317 | 1320 | retSlot := AllocPerm(8, 8); |
| 1318 | 1321 | inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; |
| 1319 | 1322 | numIn := 1; ni := 1; |
| @@ -1469,13 +1472,17 @@ | ||
| 1469 | 1472 | CopyBytes(size); |
| 1470 | 1473 | end; |
| 1471 | 1474 | |
| 1472 | -{ return: the result, a value on top, goes in rax or xmm0; one behind | |
| 1473 | - the hidden pointer goes back with that pointer in rax. } | |
| 1475 | +{ return: the result, a value on top, goes in rax or xmm0; two words, | |
| 1476 | + the two values on top, in rax and rdx; one behind the hidden pointer | |
| 1477 | + goes back with that pointer in rax. } | |
| 1474 | 1478 | procedure ReturnJump; |
| 1475 | 1479 | begin |
| 1476 | 1480 | if resType <> nil then |
| 1477 | - if IsAggregate(resType) then Load(RAX, Mem(RBP, retSlot), 8, false) | |
| 1478 | - else begin | |
| 1481 | + if BehindPointer(resType) then Load(RAX, Mem(RBP, retSlot), 8, false) | |
| 1482 | + else if InTwoRegs(resType) then begin | |
| 1483 | + MovRR(RDX, Top); Drop; | |
| 1484 | + MovRR(RAX, Top); Drop; | |
| 1485 | + end else begin | |
| 1479 | 1486 | if resType^.form = fReal then MovqXR(0, Top) else MovRR(RAX, Top); |
| 1480 | 1487 | Drop; |
| 1481 | 1488 | end; |
| @@ -1491,7 +1498,7 @@ | ||
| 1491 | 1498 | if (depth <> 0) or (pushed <> 0) then Fatal('internal error: stack depth'); |
| 1492 | 1499 | { running off the end returns zeros } |
| 1493 | 1500 | if resType <> nil then |
| 1494 | - if IsAggregate(resType) then begin | |
| 1501 | + if BehindPointer(resType) then begin | |
| 1495 | 1502 | Load(RDI, Mem(RBP, retSlot), 8, false); |
| 1496 | 1503 | MovRI(RCX, resType^.size); |
| 1497 | 1504 | MovRI(RAX, 0); |
| @@ -1499,6 +1506,7 @@ | ||
| 1499 | 1506 | Load(RAX, Mem(RBP, retSlot), 8, false); |
| 1500 | 1507 | end else begin |
| 1501 | 1508 | MovRI(RAX, 0); |
| 1509 | + if InTwoRegs(resType) then MovRI(RDX, 0); | |
| 1502 | 1510 | MovqXR(0, RAX); |
| 1503 | 1511 | end; |
| 1504 | 1512 | FixLink(exitL); |
| @@ -1119,9 +1119,9 @@ | ||
| 1119 | 1119 | begin |
| 1120 | 1120 | if x.mode = mProc then begin |
| 1121 | 1121 | c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0; |
| 1122 | - c.obj := x.obj; c.code := x.obj^.isCode; c.ext := x.obj^.ext; | |
| 1122 | + c.obj := x.obj; c.ext := x.obj^.ext; | |
| 1123 | 1123 | end else begin |
| 1124 | - c.obj := nil; c.code := false; c.ext := false; | |
| 1124 | + c.obj := nil; c.ext := false; | |
| 1125 | 1125 | if x.typ^.form <> fProc then MarkAt(x.pos, 'expected a procedure, found ' + What(x)); |
| 1126 | 1126 | c.sig := x.typ; c.chunk := -1; |
| 1127 | 1127 | c.target := ToSlot(x); |
| @@ -1139,8 +1139,29 @@ | ||
| 1139 | 1139 | Fail; |
| 1140 | 1140 | end; |
| 1141 | 1141 | |
| 1142 | +{ Pushes the two words of x, a slice or two results (InTwoRegs), read | |
| 1143 | + from memory now, so that what comes after can't change them. } | |
| 1144 | +procedure PushWords(var x: TItem); | |
| 1145 | +var adr: TAdr; f: PObj; | |
| 1146 | +begin | |
| 1147 | + if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(x); | |
| 1148 | + adr := AdrOf(x); | |
| 1149 | + if x.typ^.form = fSlice then begin | |
| 1150 | + PushVar(adr, 8, false, kWord); | |
| 1151 | + inc(adr.off, 8); | |
| 1152 | + PushVar(adr, 8, false, kWord); | |
| 1153 | + end else begin | |
| 1154 | + f := x.typ^.fields; | |
| 1155 | + while f <> nil do begin | |
| 1156 | + adr.off := AdrOf(x).off + f^.val; | |
| 1157 | + PushVar(adr, ScalarSize(f^.typ), Signed(f^.typ), KindOf(f^.typ)); | |
| 1158 | + f := f^.next; | |
| 1159 | + end; | |
| 1160 | + end; | |
| 1161 | +end; | |
| 1162 | + | |
| 1142 | 1163 | procedure PassArg(var c: TCall; var a: TItem); |
| 1143 | -var p: PObj; adr: TAdr; s: int64; | |
| 1164 | +var p: PObj; | |
| 1144 | 1165 | begin |
| 1145 | 1166 | p := c.param; |
| 1146 | 1167 | if c.n >= High(c.isF) then MarkAt(a.pos, 'too many arguments'); |
| @@ -1171,16 +1192,11 @@ | ||
| 1171 | 1192 | if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); |
| 1172 | 1193 | Address(a); |
| 1173 | 1194 | end else if p^.typ^.form = fSlice then begin |
| 1174 | - { its two words, read now, so that a later argument can't change them } | |
| 1175 | 1195 | Coerce(a, p^.typ); |
| 1176 | - if (a.mode <> mVar) or a.vol or not (a.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(a); | |
| 1177 | - adr := AdrOf(a); | |
| 1178 | - PushVar(adr, 8, false, kWord); | |
| 1196 | + PushWords(a); | |
| 1179 | 1197 | c.pair[c.n] := true; |
| 1180 | 1198 | inc(c.n); |
| 1181 | 1199 | c.isF[c.n] := false; |
| 1182 | - inc(adr.off, 8); | |
| 1183 | - PushVar(adr, 8, false, kWord); | |
| 1184 | 1200 | end else if IsAggregate(p^.typ) then begin |
| 1185 | 1201 | Coerce(a, p^.typ); |
| 1186 | 1202 | if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a); |
| @@ -1335,24 +1351,28 @@ | ||
| 1335 | 1351 | gen.BeginProc(sig, params); |
| 1336 | 1352 | end; |
| 1337 | 1353 | |
| 1338 | -{ The result of return x: a value on top, or one copied behind the | |
| 1339 | - hidden pointer } | |
| 1354 | +{ The result of return x: a value on top, a slice's two words on top, | |
| 1355 | + or one copied behind the hidden pointer } | |
| 1340 | 1356 | procedure ReturnValue(var x: TItem); |
| 1341 | 1357 | begin |
| 1342 | 1358 | if result = nil then MarkAt(x.pos, 'this procedure returns nothing'); |
| 1343 | 1359 | Coerce(x, result); |
| 1344 | - if IsAggregate(result) then begin | |
| 1360 | + if InTwoRegs(result) then PushWords(x) | |
| 1361 | + else if IsAggregate(result) then begin | |
| 1345 | 1362 | if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); |
| 1346 | 1363 | Address(x); |
| 1347 | 1364 | CopyResult(0, result^.size); |
| 1348 | 1365 | end else Value(x); |
| 1349 | 1366 | end; |
| 1350 | 1367 | |
| 1351 | -{ One of several results, into its place behind the hidden pointer. } | |
| 1368 | +{ One of several results: of two words, a value left on top for | |
| 1369 | + ReturnJump to put in rax or rdx; else into its place behind the | |
| 1370 | + hidden pointer. } | |
| 1352 | 1371 | procedure ReturnField(f: PObj; var x: TItem); |
| 1353 | 1372 | begin |
| 1354 | 1373 | Coerce(x, f^.typ); |
| 1355 | - if IsScalar(f^.typ) then begin | |
| 1374 | + if InTwoRegs(result) then Value(x) | |
| 1375 | + else if IsScalar(f^.typ) then begin | |
| 1356 | 1376 | Value(x); |
| 1357 | 1377 | ResultTo(f^.val, ScalarSize(f^.typ)); |
| 1358 | 1378 | end else begin |
| @@ -1365,8 +1385,11 @@ | ||
| 1365 | 1385 | { Several results at once, the call's that return f(x) passes on. } |
| 1366 | 1386 | procedure ReturnAll(var x: TItem); |
| 1367 | 1387 | begin |
| 1368 | - Address(x); | |
| 1369 | - CopyResult(0, result^.size); | |
| 1388 | + if InTwoRegs(result) then PushWords(x) | |
| 1389 | + else begin | |
| 1390 | + Address(x); | |
| 1391 | + CopyResult(0, result^.size); | |
| 1392 | + end; | |
| 1370 | 1393 | end; |
| 1371 | 1394 | |
| 1372 | 1395 | procedure ReturnNone; |
| @@ -513,7 +513,8 @@ | ||
| 513 | 513 | end; |
| 514 | 514 | |
| 515 | 515 | { Several results: a record of them, laid out as C would, and |
| 516 | - returned through a hidden pointer. } | |
| 516 | + returned in rax and rdx if they are two words (InTwoRegs), or else | |
| 517 | + through a hidden pointer. } | |
| 517 | 518 | function Results: PType; |
| 518 | 519 | var t, ft: PType; f, last: PObj; off: int64; |
| 519 | 520 | begin |
| @@ -1599,7 +1600,7 @@ | ||
| 1599 | 1600 | end; |
| 1600 | 1601 | |
| 1601 | 1602 | procedure OneStatement; |
| 1602 | -var x, y: TItem; called, scalar: boolean; m: int64; c: TCall; start: TPos; first: longint; | |
| 1603 | +var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; first, n, i: longint; k: TKind; | |
| 1603 | 1604 | begin |
| 1604 | 1605 | start := TokPos; |
| 1605 | 1606 | first := id; |
| @@ -1664,13 +1665,15 @@ | ||
| 1664 | 1665 | Expression(x, curResult); |
| 1665 | 1666 | ReturnValue(x); |
| 1666 | 1667 | end; |
| 1667 | - { the result waits in the frame while the deferred statements run } | |
| 1668 | - scalar := (curResult <> nil) and not IsAggregate(curResult); | |
| 1669 | - if (numDefers > 0) and scalar then begin | |
| 1670 | - if deferSlot = 0 then deferSlot := AllocPerm(8, 8); | |
| 1671 | - PopVar(FrameAdr(deferSlot), 8); | |
| 1668 | + { the result, one value or two words on top, waits in the frame | |
| 1669 | + while the deferred statements run } | |
| 1670 | + if (numDefers > 0) and (curResult <> nil) and not BehindPointer(curResult) then begin | |
| 1671 | + n := 1; k := KindOf(curResult); | |
| 1672 | + if InTwoRegs(curResult) then begin n := 2; k := kWord end; | |
| 1673 | + if deferSlot = 0 then deferSlot := AllocPerm(16, 8); | |
| 1674 | + for i := n - 1 downto 0 do PopVar(FrameAdr(deferSlot + 8 * i), 8); | |
| 1672 | 1675 | RunDefers(0); |
| 1673 | - PushVar(FrameAdr(deferSlot), 8, false, KindOf(curResult)); | |
| 1676 | + for i := 0 to n - 1 do PushVar(FrameAdr(deferSlot + 8 * i), 8, false, k); | |
| 1674 | 1677 | end else RunDefers(0); |
| 1675 | 1678 | ReturnJump; |
| 1676 | 1679 | ends := true; |
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | variadic: boolean; { fProc: ends in ... } |
| 44 | 44 | tuple: boolean; { fRecord: the results of a procedure that has several } |
| 45 | 45 | base: PType; { fPtr, fArray, fSlice: what it holds; fProc: result } |
| 46 | - len: int64; { fArray: elements; fProc: parameters; fVariant: cases } | |
| 46 | + len: int64; { fArray: elements; fProc: parameters; fVariant: cases; a tuple: results } | |
| 47 | 47 | fields: PObj; { fRecord: fields; fVariant: cases; fProc: parameters } |
| 48 | 48 | scope: longint; { fRecord: the scope of its fields } |
| 49 | 49 | fwd: longint; { fPtr whose base isn't declared yet: its name, else -1 } |
| @@ -156,6 +156,8 @@ | ||
| 156 | 156 | function IsInt(t: PType): boolean; |
| 157 | 157 | function IsScalar(t: PType): boolean; |
| 158 | 158 | function IsAggregate(t: PType): boolean; |
| 159 | +function InTwoRegs(t: PType): boolean; | |
| 160 | +function BehindPointer(t: PType): boolean; | |
| 159 | 161 | function Fits(v: int64; t: PType): boolean; |
| 160 | 162 | function TypeName(t: PType): shortstring; |
| 161 | 163 | function Fingerprint(o: PObj): qword; |
| @@ -391,6 +393,30 @@ | ||
| 391 | 393 | function IsAggregate(t: PType): boolean; |
| 392 | 394 | begin |
| 393 | 395 | IsAggregate := t^.form in [fArray, fSlice, fRecord, fVariant]; |
| 396 | +end; | |
| 397 | + | |
| 398 | +{ Whether a procedure's result t is two words, which come back in rax | |
| 399 | + and rdx, as System V returns a struct of two integers: a slice (its | |
| 400 | + pointer, then its length), or two results that are each a single | |
| 401 | + value of 8 bytes at most and not a real. } | |
| 402 | +function InTwoRegs(t: PType): boolean; | |
| 403 | + | |
| 404 | + function OneWord(f: PObj): boolean; | |
| 405 | + begin | |
| 406 | + OneWord := IsScalar(f^.typ) and (f^.typ^.form <> fReal) and (f^.typ^.size <= 8); | |
| 407 | + end; | |
| 408 | + | |
| 409 | +begin | |
| 410 | + if t^.form = fSlice then exit(true); | |
| 411 | + InTwoRegs := t^.tuple and (t^.len = 2) and OneWord(t^.fields) and OneWord(t^.fields^.next); | |
| 412 | +end; | |
| 413 | + | |
| 414 | +{ Whether a procedure's result t comes back in the caller's memory, in | |
| 415 | + a place a hidden pointer, passed first, says: any result that is | |
| 416 | + neither one value, in rax or xmm0, nor two words, in rax and rdx. } | |
| 417 | +function BehindPointer(t: PType): boolean; | |
| 418 | +begin | |
| 419 | + BehindPointer := IsAggregate(t) and not InTwoRegs(t); | |
| 394 | 420 | end; |
| 395 | 421 | |
| 396 | 422 | function Fits(v: int64; t: PType): boolean; |