Step 7: multiple results, errors, INC and DEC Several results are a record, returned through the hidden pointer; a code procedure returns two in rax and rdx. a, b := f(x) keeps each variable's address in a frame slot while the call runs, then assigns in order. An ERROR constant is the address of its length and message; errors compare by that address, and MSG reads the message. INC and DEC evaluate their variable once.
| @@ -92,7 +92,7 @@ | ||
| 92 | 92 | |
| 93 | 93 | Stage 2: the rest of the language |
| 94 | 94 | |
| 95 | -- [ ] 7. Multiple results and errors. | |
| 95 | +- [x] 7. Multiple results and errors; `INC` and `DEC`. | |
| 96 | 96 | - [ ] 8. Enumerations and sets, record literals, `defer`; examples. |
| 97 | 97 | - [ ] 9. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`. |
| 98 | 98 | - [ ] 10. Generic modules, with type and procedure arguments; |
| @@ -507,6 +507,8 @@ | ||
| 507 | 507 | | `NEW(p, a)` | sets `p: ^T` to a fresh, zeroed `T` from the allocator `a: ^Mem.Allocator`, or to `nil` if `a` has no room | |
| 508 | 508 | | `MSG(e)` | the message of the error `e`, a `[]U8` | |
| 509 | 509 | | `SYSCALL(n, x1, ..., x6)` | Linux system call `n` with up to six integer or pointer arguments; the raw result, an `I64` (a negative number is `-errno`) | |
| 510 | +| `INC(v)`, `INC(v, k)` | adds 1, or `k`, to the integer variable `v`; it wraps, like `+` | | |
| 511 | +| `DEC(v)`, `DEC(v, k)` | subtracts 1, or `k`, from `v` | | |
| 510 | 512 | | `FENCE` | a full memory barrier | |
| 511 | 513 | | `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; atomically | |
| 512 | 514 | | `XCHG(p, x)` | sets `p^ := x` and returns the value before, atomically | |
| @@ -517,6 +519,10 @@ | ||
| 517 | 519 | `SYSCALL` exists only on Linux targets. Other systems have no stable |
| 518 | 520 | system call interface, and their modules call the C library instead |
| 519 | 521 | (section 9). |
| 522 | + | |
| 523 | +`INC` and `DEC` evaluate `v` once, so `INC(a[f(i)])` calls `f` once. They | |
| 524 | +read `v` and then write it: two accesses, even through a `^VOLATILE` | |
| 525 | +pointer, and not atomic; `XADD` is the atomic one. | |
| 520 | 526 | |
| 521 | 527 | For `CAS`, `XCHG` and `XADD`, `p` is a `^T` or `^VOLATILE T` where `T` |
| 522 | 528 | is an 8-byte integer or pointer type (an integer type for `XADD`). They |
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | |
| 32 | 32 | type |
| 33 | 33 | TMode = (mConst, mVar, mReg, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone); |
| 34 | - TBase = (bFrame, bGlobal, bReg, bStack); | |
| 34 | + TBase = (bFrame, bGlobal, bReg, bStack, bSlot); { bSlot: address in a frame slot, chunk } | |
| 35 | 35 | |
| 36 | 36 | TItem = record |
| 37 | 37 | mode: TMode; |
| @@ -53,6 +53,7 @@ | ||
| 53 | 53 | n: longint; |
| 54 | 54 | target: int64; { the frame slot of a procedure value, if any } |
| 55 | 55 | chunk: longint; { else the procedure's chunk } |
| 56 | + code: boolean; { a code procedure: two results come in rax and rdx } | |
| 56 | 57 | end; |
| 57 | 58 | |
| 58 | 59 | var |
| @@ -110,7 +111,13 @@ | ||
| 110 | 111 | procedure BeginProc(sig: PType; params: PObj); |
| 111 | 112 | procedure EnterBody(c: longint); |
| 112 | 113 | procedure ReturnValue(var x: TItem); |
| 114 | +procedure ReturnField(f: PObj; var x: TItem); | |
| 115 | +procedure ReturnJump; | |
| 113 | 116 | procedure ReturnNone; |
| 117 | +procedure Pin(var x: TItem); | |
| 118 | +procedure IncDec(var v, k: TItem; dec: boolean); | |
| 119 | +procedure Msg(var x: TItem); | |
| 120 | +function ErrorChunk(var x: TItem; module, name: longint): longint; | |
| 114 | 121 | procedure EndProc(c: longint); |
| 115 | 122 | procedure CmpI(r: longint; v: int64); |
| 116 | 123 | procedure PushRax; |
| @@ -217,6 +224,13 @@ | ||
| 217 | 224 | else begin MovRI(R11, v); Alu(aluCmp, r, R11) end; |
| 218 | 225 | end; |
| 219 | 226 | |
| 227 | +{ A constant into r: an error constant is the address of its chunk. } | |
| 228 | +procedure ConstTo(r: longint; const x: TItem); | |
| 229 | +begin | |
| 230 | + if (x.typ^.form = fError) and (x.val > 0) then Lea(r, MemRip(x.val - 1, 0)) | |
| 231 | + else MovRI(r, x.val); | |
| 232 | +end; | |
| 233 | + | |
| 220 | 234 | function MemOf(var x: TItem; r: longint): TMem; |
| 221 | 235 | begin |
| 222 | 236 | case x.base of |
| @@ -227,6 +241,11 @@ | ||
| 227 | 241 | x.base := bReg; x.reg := r; |
| 228 | 242 | MemOf := Mem(r, x.off); |
| 229 | 243 | end; |
| 244 | + bSlot: begin | |
| 245 | + Load(r, Mem(RBP, x.chunk), 8, false); | |
| 246 | + x.base := bReg; x.reg := r; | |
| 247 | + MemOf := Mem(r, x.off); | |
| 248 | + end; | |
| 230 | 249 | else MemOf := Mem(x.reg, x.off); |
| 231 | 250 | end; |
| 232 | 251 | end; |
| @@ -258,7 +277,7 @@ | ||
| 258 | 277 | var m: TMem; |
| 259 | 278 | begin |
| 260 | 279 | case x.mode of |
| 261 | - mConst: MovRI(RAX, x.val); | |
| 280 | + mConst: ConstTo(RAX, x); | |
| 262 | 281 | mVar: begin |
| 263 | 282 | if not IsScalar(x.typ) then Mark('not a single value'); |
| 264 | 283 | m := MemOf(x, RAX); |
| @@ -266,7 +285,11 @@ | ||
| 266 | 285 | end; |
| 267 | 286 | mReg: ; |
| 268 | 287 | mStack: PopR(RAX); |
| 269 | - mProc: Lea(RAX, MemRip(x.obj^.chunk, 0)); | |
| 288 | + mProc: begin | |
| 289 | + if x.obj^.isCode and (x.obj^.typ^.base <> nil) and x.obj^.typ^.base^.tuple then | |
| 290 | + Mark('a code procedure with two results can only be called'); | |
| 291 | + Lea(RAX, MemRip(x.obj^.chunk, 0)); | |
| 292 | + end; | |
| 270 | 293 | else Mark('not a value'); |
| 271 | 294 | end; |
| 272 | 295 | x.mode := mReg; |
| @@ -391,7 +414,7 @@ | ||
| 391 | 414 | if (s^.form = fConst) and (t^.form = fInt) then begin |
| 392 | 415 | if (x.mode = mConst) and not Fits(x.val, t) then Mark('constant doesn''t fit ' + TypeName(t)); |
| 393 | 416 | x.typ := t; |
| 394 | - end else if (s^.form = fNil) and (t^.form in [fPtr, fProc]) then | |
| 417 | + end else if (s^.form = fNil) and (t^.form in [fPtr, fProc, fError]) then | |
| 395 | 418 | x.typ := t |
| 396 | 419 | else if (s^.form = fPtr) and (t^.form = fPtr) and t^.vol and not s^.vol |
| 397 | 420 | and Same(s^.base, t^.base) then |
| @@ -752,10 +775,10 @@ | ||
| 752 | 775 | end else if (xt^.form = fPtr) and (yt^.form = fPtr) and (op = sMinus) then begin |
| 753 | 776 | if not Same(xt, yt) then Mark('different pointer types'); |
| 754 | 777 | t := tI64; scale := xt^.base^.size; isPtr := true; |
| 755 | - end else if (xt^.form in [fPtr, fProc, fNil]) and (yt^.form in [fPtr, fProc, fNil]) then begin | |
| 778 | + end else if (xt^.form in [fPtr, fProc, fNil, fError]) and (yt^.form in [fPtr, fProc, fNil, fError]) then begin | |
| 756 | 779 | if not (op in Rel) then Mark('not a pointer operator'); |
| 757 | - if (xt^.form = fProc) or (yt^.form = fProc) then | |
| 758 | - if not (op in [sEql, sNeq]) then Mark('procedures compare only with = and #'); | |
| 780 | + if (xt^.form in [fProc, fError]) or (yt^.form in [fProc, fError]) then | |
| 781 | + if not (op in [sEql, sNeq]) then Mark('procedures and errors compare only with = and #'); | |
| 759 | 782 | if (xt^.form <> fNil) and (yt^.form <> fNil) and not Same(xt, yt) then |
| 760 | 783 | if not ((xt^.form = fPtr) and (yt^.form = fPtr) and Same(xt^.base, yt^.base)) then |
| 761 | 784 | Mark('different pointer types'); |
| @@ -779,11 +802,11 @@ | ||
| 779 | 802 | { x into rax, y into rcx } |
| 780 | 803 | if y.mode = mConst then begin |
| 781 | 804 | if x.mode = mStack then PopR(RAX) else Value(x); |
| 782 | - MovRI(RCX, y.val); | |
| 805 | + ConstTo(RCX, y); | |
| 783 | 806 | end else begin |
| 784 | 807 | Value(y); |
| 785 | 808 | MovRR(RCX, RAX); |
| 786 | - if x.mode = mConst then MovRI(RAX, x.val) else PopR(RAX); | |
| 809 | + if x.mode = mConst then ConstTo(RAX, x) else PopR(RAX); | |
| 787 | 810 | end; |
| 788 | 811 | |
| 789 | 812 | if t^.form = fReal then begin |
| @@ -893,8 +916,10 @@ | ||
| 893 | 916 | procedure BeginCall(var c: TCall; var x: TItem); |
| 894 | 917 | begin |
| 895 | 918 | if x.mode = mProc then begin |
| 896 | - c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0 | |
| 919 | + c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0; | |
| 920 | + c.code := x.obj^.isCode; | |
| 897 | 921 | end else begin |
| 922 | + c.code := false; | |
| 898 | 923 | if x.typ^.form <> fProc then Mark('not a procedure'); |
| 899 | 924 | c.sig := x.typ; c.chunk := -1; |
| 900 | 925 | c.target := ToSlot(x); |
| @@ -942,7 +967,7 @@ | ||
| 942 | 967 | ni := 0; nf := 0; |
| 943 | 968 | if (res <> nil) and IsAggregate(res) then begin |
| 944 | 969 | ret := AllocTemp(res^.size, res^.align); |
| 945 | - ni := 1; | |
| 970 | + if not c.code then ni := 1; | |
| 946 | 971 | end; |
| 947 | 972 | { the System V classes: each argument in the next free register of |
| 948 | 973 | its kind, or else on the stack } |
| @@ -963,7 +988,7 @@ | ||
| 963 | 988 | Push(RAX); |
| 964 | 989 | end; |
| 965 | 990 | ni := 0; nf := 0; |
| 966 | - if ret <> 0 then begin Lea(RDI, Mem(RBP, ret)); ni := 1 end; | |
| 991 | + if (ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, ret)); ni := 1 end; | |
| 967 | 992 | for i := 0 to c.n - 1 do |
| 968 | 993 | if not onStack[i] then |
| 969 | 994 | if isF[i] then begin MovsdXM(nf, Mem(RBP, c.slots[i])); inc(nf) end |
| @@ -974,6 +999,10 @@ | ||
| 974 | 999 | CallR(R11); |
| 975 | 1000 | end; |
| 976 | 1001 | if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); |
| 1002 | + if (ret <> 0) and c.code then begin | |
| 1003 | + Store(Mem(RBP, ret), RAX, 8); | |
| 1004 | + Store(Mem(RBP, ret + 8), RDX, 8); | |
| 1005 | + end; | |
| 977 | 1006 | FillChar(x, SizeOf(x), 0); |
| 978 | 1007 | if res = nil then begin |
| 979 | 1008 | x.mode := mNone; x.typ := tNone |
| @@ -1136,11 +1165,91 @@ | ||
| 1136 | 1165 | Load(RDI, Mem(RBP, retSlot), 8, false); |
| 1137 | 1166 | Copy(result^.size); |
| 1138 | 1167 | Load(RAX, Mem(RBP, retSlot), 8, false); |
| 1168 | + end else Value(x); | |
| 1169 | +end; | |
| 1170 | + | |
| 1171 | +{ One of several results, into its place behind the hidden pointer. } | |
| 1172 | +procedure ReturnField(f: PObj; var x: TItem); | |
| 1173 | +begin | |
| 1174 | + Coerce(x, f^.typ); | |
| 1175 | + if IsScalar(f^.typ) then begin | |
| 1176 | + Value(x); | |
| 1177 | + Load(RCX, Mem(RBP, retSlot), 8, false); | |
| 1178 | + Store(Mem(RCX, f^.val), RAX, ScalarSize(f^.typ)); | |
| 1139 | 1179 | end else begin |
| 1140 | - Value(x); | |
| 1141 | - if result^.form = fReal then MovqXR(0, RAX); | |
| 1180 | + if x.vol then Mark('can''t copy through a volatile pointer'); | |
| 1181 | + Address(x, RSI); | |
| 1182 | + Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 1183 | + Lea(RDI, Mem(RDI, f^.val)); | |
| 1184 | + Copy(f^.typ^.size); | |
| 1142 | 1185 | end; |
| 1186 | +end; | |
| 1187 | + | |
| 1188 | +{ With the result in rax, as ReturnValue left it. } | |
| 1189 | +procedure ReturnJump; | |
| 1190 | +begin | |
| 1191 | + if IsAggregate(result) then Load(RAX, Mem(RBP, retSlot), 8, false) | |
| 1192 | + else if result^.form = fReal then MovqXR(0, RAX); | |
| 1143 | 1193 | Jmp(exitL); |
| 1194 | +end; | |
| 1195 | + | |
| 1196 | +{ Keeps a variable's address in a frame slot, rather than in a register | |
| 1197 | + or on the stack, while other code runs. } | |
| 1198 | +procedure Pin(var x: TItem); | |
| 1199 | +var s: int64; | |
| 1200 | +begin | |
| 1201 | + if (x.mode = mVar) and (x.base in [bReg, bStack]) then begin | |
| 1202 | + Address(x, RAX); | |
| 1203 | + s := AllocTemp(8, 8); | |
| 1204 | + Store(Mem(RBP, s), RAX, 8); | |
| 1205 | + x.base := bSlot; | |
| 1206 | + x.chunk := s; | |
| 1207 | + end; | |
| 1208 | +end; | |
| 1209 | + | |
| 1210 | +procedure IncDec(var v, k: TItem; dec: boolean); | |
| 1211 | +var n: longint; | |
| 1212 | +begin | |
| 1213 | + Coerce(k, v.typ); | |
| 1214 | + Value(k); | |
| 1215 | + MovRR(RCX, RAX); | |
| 1216 | + Address(v, RDX); | |
| 1217 | + n := ScalarSize(v.typ); | |
| 1218 | + Load(RAX, Mem(RDX, 0), n, v.typ^.signed); | |
| 1219 | + if dec then Alu(aluSub, RAX, RCX) else Alu(aluAdd, RAX, RCX); | |
| 1220 | + Store(Mem(RDX, 0), RAX, n); | |
| 1221 | +end; | |
| 1222 | + | |
| 1223 | +{ An error is the address of its length, followed by its message. } | |
| 1224 | +function ErrorChunk(var x: TItem; module, name: longint): longint; | |
| 1225 | +var c: longint; p: PBytes; | |
| 1226 | +begin | |
| 1227 | + if x.mode <> mStr then Mark('an error is made from a string'); | |
| 1228 | + p := GetMem(x.val + 8); | |
| 1229 | + PInt64(p)^ := x.val; | |
| 1230 | + Move(rodata^[chunks^[x.chunk].start], p^[8], x.val); | |
| 1231 | + c := NewChunk(secRodata, module, name); | |
| 1232 | + SetData(c, p, x.val + 8, 8); | |
| 1233 | + FreeMem(p); | |
| 1234 | + ErrorChunk := c; | |
| 1235 | +end; | |
| 1236 | + | |
| 1237 | +procedure Msg(var x: TItem); | |
| 1238 | +var L: longint; s: int64; | |
| 1239 | +begin | |
| 1240 | + Coerce(x, tError); | |
| 1241 | + Value(x); | |
| 1242 | + s := AllocTemp(16, 8); | |
| 1243 | + MovRI(RCX, 0); | |
| 1244 | + L := NoLabel; | |
| 1245 | + TestRR(RAX, RAX); | |
| 1246 | + Jcc(ccE, L); | |
| 1247 | + Load(RCX, Mem(RAX, 0), 8, false); | |
| 1248 | + Lea(RAX, Mem(RAX, 8)); | |
| 1249 | + FixLink(L); | |
| 1250 | + Store(Mem(RBP, s), RAX, 8); | |
| 1251 | + Store(Mem(RBP, s + 8), RCX, 8); | |
| 1252 | + TempItem(x, tBytes, s); | |
| 1144 | 1253 | end; |
| 1145 | 1254 | |
| 1146 | 1255 | procedure ReturnNone; |
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | are read for their declarations only, and must export the same |
| 12 | 12 | interface. |
| 13 | 13 | |
| 14 | - Depends on: scan, tok, x64, gen. | |
| 14 | + Depends on: scan, sym, x64, gen. | |
| 15 | 15 | Leaves out (for later stages): multiple results, errors, enumerations |
| 16 | 16 | and sets, record literals, defer, NEW, generic modules, extern. } |
| 17 | 17 | |
| @@ -234,6 +234,33 @@ | ||
| 234 | 234 | VariantType := t; |
| 235 | 235 | end; |
| 236 | 236 | |
| 237 | +{ Several results: a record of them, laid out as C would, and | |
| 238 | + returned through a hidden pointer. } | |
| 239 | +function Results: PType; | |
| 240 | +var t, ft: PType; f, last: PObj; off: int64; | |
| 241 | +begin | |
| 242 | + Next; | |
| 243 | + t := NewType(fRecord, 0, 1); | |
| 244 | + t^.tuple := true; | |
| 245 | + off := 0; last := nil; | |
| 246 | + repeat | |
| 247 | + ft := ParseType; | |
| 248 | + off := AlignUp(off, ft^.align); | |
| 249 | + f := NewObj(oField, -1, ft); | |
| 250 | + f^.val := off; | |
| 251 | + off := off + ft^.size; | |
| 252 | + if ft^.align > t^.align then t^.align := ft^.align; | |
| 253 | + if last = nil then t^.fields := f else last^.next := f; | |
| 254 | + last := f; | |
| 255 | + inc(t^.len); | |
| 256 | + if tok <> sComma then break; | |
| 257 | + Next; | |
| 258 | + until false; | |
| 259 | + Expect(sRparen, ')'); | |
| 260 | + t^.size := AlignUp(off, t^.align); | |
| 261 | + if t^.len = 1 then Results := t^.fields^.typ else Results := t; | |
| 262 | +end; | |
| 263 | + | |
| 237 | 264 | function Signature: PType; |
| 238 | 265 | var |
| 239 | 266 | t, pt: PType; p, first, last: PObj; isVar: boolean; n: longint; |
| @@ -274,8 +301,7 @@ | ||
| 274 | 301 | t^.len := n; |
| 275 | 302 | if tok = sColon then begin |
| 276 | 303 | Next; |
| 277 | - if tok = sLparen then Mark('several results come in stage 2'); | |
| 278 | - t^.base := ParseType; | |
| 304 | + if tok = sLparen then t^.base := Results else t^.base := ParseType; | |
| 279 | 305 | end; |
| 280 | 306 | Signature := t; |
| 281 | 307 | end; |
| @@ -450,7 +476,21 @@ | ||
| 450 | 476 | x.mode := mReg; |
| 451 | 477 | end; |
| 452 | 478 | bCas, bXchg, bXadd: Atomic(x, which); |
| 453 | - bNew, bMsg: Mark('NEW and MSG come in stage 2'); | |
| 479 | + bMsg: begin Expression(x, tError); Msg(x) end; | |
| 480 | + bInc, bDec: begin | |
| 481 | + Expression(x, nil); | |
| 482 | + if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fInt) then | |
| 483 | + Mark('INC and DEC need an integer variable'); | |
| 484 | + Save(x); | |
| 485 | + if tok = sComma then begin | |
| 486 | + Next; | |
| 487 | + Expression(y, x.typ); | |
| 488 | + end else MakeConst(y, x.typ, 1); | |
| 489 | + IncDec(x, y, which = bDec); | |
| 490 | + MakeConst(x, tNone, 0); | |
| 491 | + x.mode := mNone; | |
| 492 | + end; | |
| 493 | + bNew: Mark('NEW comes in step 9'); | |
| 454 | 494 | end; |
| 455 | 495 | Expect(sRparen, ')'); |
| 456 | 496 | end; |
| @@ -600,6 +640,7 @@ | ||
| 600 | 640 | sIdent: begin |
| 601 | 641 | Designator(x, called); |
| 602 | 642 | if x.mode = mNone then Mark('this procedure returns nothing'); |
| 643 | + if x.typ^.tuple then Mark('several results go only to a, b := ...'); | |
| 603 | 644 | end; |
| 604 | 645 | sLparen: begin Next; Expression(x, want); Expect(sRparen, ')') end; |
| 605 | 646 | sNot: begin Next; Factor(x, nil); NotOp(x) end; |
| @@ -853,6 +894,47 @@ | ||
| 853 | 894 | FixLink(endL); |
| 854 | 895 | end; |
| 855 | 896 | |
| 897 | +{ a, b := f(x): the variables first, left to right, then the call; | |
| 898 | + then each result to its variable, in order. } | |
| 899 | +procedure MultiAssign(var x: TItem); | |
| 900 | +var d: array[0..15] of TItem; y, z: TItem; n, i: longint; called: boolean; f: PObj; | |
| 901 | +begin | |
| 902 | + n := 0; | |
| 903 | + d[0] := x; | |
| 904 | + repeat | |
| 905 | + if (d[n].mode <> mVar) or not d[n].isVar then Mark('can''t assign to this'); | |
| 906 | + Pin(d[n]); | |
| 907 | + inc(n); | |
| 908 | + if tok <> sComma then break; | |
| 909 | + if n > High(d) then Mark('too many variables'); | |
| 910 | + Next; | |
| 911 | + Designator(d[n], called); | |
| 912 | + until false; | |
| 913 | + Expect(sBecomes, ':='); | |
| 914 | + Designator(y, called); | |
| 915 | + if not called or not y.typ^.tuple or (y.typ^.len <> n) then | |
| 916 | + Mark('needs a call with as many results as variables'); | |
| 917 | + f := y.typ^.fields; | |
| 918 | + for i := 0 to n - 1 do begin | |
| 919 | + z := y; | |
| 920 | + Field(z, f); | |
| 921 | + Assign(d[i], z); | |
| 922 | + f := f^.next; | |
| 923 | + end; | |
| 924 | +end; | |
| 925 | + | |
| 926 | +procedure ReturnResults; | |
| 927 | +var f: PObj; x: TItem; | |
| 928 | +begin | |
| 929 | + f := curResult^.fields; | |
| 930 | + while f <> nil do begin | |
| 931 | + Expression(x, f^.typ); | |
| 932 | + ReturnField(f, x); | |
| 933 | + f := f^.next; | |
| 934 | + if f <> nil then Expect(sComma, ','); | |
| 935 | + end; | |
| 936 | +end; | |
| 937 | + | |
| 856 | 938 | procedure Statement; |
| 857 | 939 | var x, y: TItem; called: boolean; m: int64; c: TCall; |
| 858 | 940 | begin |
| @@ -867,7 +949,7 @@ | ||
| 867 | 949 | Expression(y, x.typ); |
| 868 | 950 | Assign(x, y); |
| 869 | 951 | end else if tok = sComma then |
| 870 | - Mark('several results come in stage 2') | |
| 952 | + MultiAssign(x) | |
| 871 | 953 | else if not called then begin |
| 872 | 954 | if (x.mode = mProc) or ((x.mode in [mVar, mReg]) and (x.typ^.form = fProc)) then begin |
| 873 | 955 | BeginCall(c, x); |
| @@ -884,8 +966,12 @@ | ||
| 884 | 966 | Next; |
| 885 | 967 | if tok = sSemicolon then ReturnNone |
| 886 | 968 | else begin |
| 887 | - Expression(x, curResult); | |
| 888 | - ReturnValue(x); | |
| 969 | + if (curResult <> nil) and curResult^.tuple then ReturnResults | |
| 970 | + else begin | |
| 971 | + Expression(x, curResult); | |
| 972 | + ReturnValue(x); | |
| 973 | + end; | |
| 974 | + ReturnJump; | |
| 889 | 975 | end; |
| 890 | 976 | end; |
| 891 | 977 | sBreak: begin |
| @@ -977,7 +1063,14 @@ | ||
| 977 | 1063 | q := q^.next; |
| 978 | 1064 | end; |
| 979 | 1065 | if (ni > 6) or (nf > 8) or sig^.variadic then Mark('too many parameters for a code procedure'); |
| 980 | - if (sig^.base <> nil) and not IsScalar(sig^.base) then Mark('a code procedure returns a single value'); | |
| 1066 | + if sig^.base <> nil then | |
| 1067 | + if sig^.base^.tuple then begin | |
| 1068 | + q := sig^.base^.fields; | |
| 1069 | + if (sig^.base^.len <> 2) or (q^.typ^.size <> 8) or (q^.next^.typ^.size <> 8) | |
| 1070 | + or not IsScalar(q^.typ) or not IsScalar(q^.next^.typ) | |
| 1071 | + or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then | |
| 1072 | + Mark('a code procedure returns two results only in rax and rdx'); | |
| 1073 | + end else if not IsScalar(sig^.base) then Mark('a code procedure returns a single value'); | |
| 981 | 1074 | BeginChunk(p^.chunk); |
| 982 | 1075 | repeat |
| 983 | 1076 | v := ConstExpr; |
| @@ -1039,12 +1132,20 @@ | ||
| 1039 | 1132 | Next; |
| 1040 | 1133 | while tok = sIdent do begin |
| 1041 | 1134 | IdentDef(n, exp); |
| 1042 | - if tok = sColon then Mark('errors come in stage 2'); | |
| 1135 | + t := nil; | |
| 1136 | + if tok = sColon then begin | |
| 1137 | + Next; | |
| 1138 | + t := ParseType; | |
| 1139 | + if t <> tError then Mark('only an ERROR constant has a type'); | |
| 1140 | + end; | |
| 1043 | 1141 | Expect(sEql, '='); |
| 1044 | 1142 | Expression(x, nil); |
| 1045 | 1143 | o := NewObj(oConst, n, x.typ); |
| 1046 | 1144 | o^.exported := exp and top; |
| 1047 | - if x.mode = mStr then begin o^.val := x.val; o^.chunk := x.chunk end | |
| 1145 | + if t = tError then begin | |
| 1146 | + o^.typ := tError; | |
| 1147 | + o^.val := ErrorChunk(x, curModule^.name, n) + 1; | |
| 1148 | + end else if x.mode = mStr then begin o^.val := x.val; o^.chunk := x.chunk end | |
| 1048 | 1149 | else o^.val := ConstValue(x); |
| 1049 | 1150 | Declare(o); |
| 1050 | 1151 | Expect(sSemicolon, ';'); |
| @@ -39,6 +39,7 @@ | ||
| 39 | 39 | vol: boolean; { fPtr: ^VOLATILE } |
| 40 | 40 | packd: boolean; { fRecord } |
| 41 | 41 | variadic: boolean; { fProc: ends in ... } |
| 42 | + tuple: boolean; { fRecord: the results of a procedure that has several } | |
| 42 | 43 | base: PType; { fPtr, fArray, fSlice: what it holds; fProc: result } |
| 43 | 44 | len: int64; { fArray: elements; fProc: parameters; fVariant: cases } |
| 44 | 45 | fields: PObj; { fRecord: fields; fVariant: cases; fProc: parameters } |
| @@ -91,12 +92,13 @@ | ||
| 91 | 92 | { built-in procedures } |
| 92 | 93 | bSize = 0; bAdr = 1; bLen = 2; bNew = 3; bMsg = 4; bSyscall = 5; |
| 93 | 94 | bFence = 6; bCas = 7; bXchg = 8; bXadd = 9; bArgc = 10; bArgv = 11; |
| 95 | + bInc = 12; bDec = 13; | |
| 94 | 96 | |
| 95 | 97 | MaxDepth = 64; { scopes open at once } |
| 96 | 98 | |
| 97 | 99 | var |
| 98 | 100 | tI8, tI16, tI32, tI64, tU8, tU16, tU32, tU64: PType; |
| 99 | - tBool, tReal, tConst, tNil, tStr, tNone, tPtrU8: PType; | |
| 101 | + tBool, tReal, tError, tConst, tNil, tStr, tNone, tPtrU8, tBytes: PType; | |
| 100 | 102 | curModule: PModule; |
| 101 | 103 | |
| 102 | 104 | function NewType(form: TForm; size, align: int64): PType; |
| @@ -312,7 +314,9 @@ | ||
| 312 | 314 | fPtr: Alike := (a^.vol = b^.vol) and Same(a^.base, b^.base); |
| 313 | 315 | fArray: Alike := (a^.len = b^.len) and Same(a^.base, b^.base); |
| 314 | 316 | fSlice: Alike := Same(a^.base, b^.base); |
| 315 | - fProc: begin | |
| 317 | + fProc, fRecord: begin | |
| 318 | + { a record is the same only as itself, but for several results } | |
| 319 | + if (a^.form = fRecord) and not (a^.tuple and b^.tuple) then exit(false); | |
| 316 | 320 | if (a^.len <> b^.len) or (a^.variadic <> b^.variadic) then exit(false); |
| 317 | 321 | if (a^.base = nil) <> (b^.base = nil) then exit(false); |
| 318 | 322 | if (a^.base <> nil) and not Same(a^.base, b^.base) then exit(false); |
| @@ -448,6 +452,7 @@ | ||
| 448 | 452 | tU64 := Basic('U64', fInt, 8, false); |
| 449 | 453 | tBool := Basic('BOOL', fBool, 1, false); |
| 450 | 454 | tReal := Basic('F64', fReal, 8, false); |
| 455 | + tError := Basic('ERROR', fError, 8, false); | |
| 451 | 456 | tConst := NewType(fConst, 8, 8); |
| 452 | 457 | tConst^.signed := true; |
| 453 | 458 | tNil := NewType(fNil, 8, 8); |
| @@ -455,6 +460,8 @@ | ||
| 455 | 460 | tNone := NewType(fNone, 0, 1); |
| 456 | 461 | tPtrU8 := NewType(fPtr, 8, 8); |
| 457 | 462 | tPtrU8^.base := tU8; |
| 463 | + tBytes := NewType(fSlice, 16, 8); | |
| 464 | + tBytes^.base := tU8; | |
| 458 | 465 | Enter('true', oConst, tBool, 1); |
| 459 | 466 | Enter('false', oConst, tBool, 0); |
| 460 | 467 | Enter('nil', oConst, tNil, 0); |
| @@ -470,4 +477,6 @@ | ||
| 470 | 477 | Enter('XADD', oBuiltin, nil, bXadd); |
| 471 | 478 | Enter('ARGC', oBuiltin, nil, bArgc); |
| 472 | 479 | Enter('ARGV', oBuiltin, nil, bArgv); |
| 480 | + Enter('INC', oBuiltin, nil, bInc); | |
| 481 | + Enter('DEC', oBuiltin, nil, bDec); | |
| 473 | 482 | end. |
| @@ -0,0 +1 @@ | ||
| 1 | +compare only with = and # |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ErrOrder; | |
| 2 | +const A: ERROR = "a"; B: ERROR = "b"; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + if A < B then end; | |
| 6 | +end; | |
| 7 | +end ErrOrder. |
| @@ -0,0 +1 @@ | ||
| 1 | +several results go only to a, b := ... |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module Tuple; | |
| 2 | +proc two: (I64, I64); begin return 1, 2; end; | |
| 3 | +proc main*; | |
| 4 | +var a: I64; | |
| 5 | +begin | |
| 6 | + a := two() + 1; | |
| 7 | +end; | |
| 8 | +end Tuple. |
Binary file; the patch has it.
| @@ -0,0 +1,9 @@ | ||
| 1 | +3 2 | |
| 2 | +2 nil | |
| 3 | +-1 not found is NotFound, not Other, not Empty | |
| 4 | +[] | |
| 5 | +30 two 2 ok | |
| 6 | +2 1 | |
| 7 | +39 | |
| 8 | +0 zero | |
| 9 | +14 6 0 |
| @@ -0,0 +1,79 @@ | ||
| 1 | +module Results; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +const | |
| 5 | + NotFound*: ERROR = "not found"; | |
| 6 | + Empty: ERROR = "empty"; | |
| 7 | + Other: ERROR = "not found"; | |
| 8 | + | |
| 9 | +type Pair = record a, b: I64; end; | |
| 10 | + | |
| 11 | +var table: [4]I64; g: Pair; | |
| 12 | + | |
| 13 | +proc divmod(a, b: I64): (I64, I64); | |
| 14 | +begin | |
| 15 | + return a div b, a mod b; | |
| 16 | +end; | |
| 17 | + | |
| 18 | +proc find(key: I64): (I64, ERROR); | |
| 19 | +var i: I64; | |
| 20 | +begin | |
| 21 | + for i := 0 to 3 do | |
| 22 | + if table[i] = key then return i, nil; end; | |
| 23 | + end; | |
| 24 | + return -1, NotFound; | |
| 25 | +end; | |
| 26 | + | |
| 27 | +proc mixed(x: F64): (F64, []U8, Pair, BOOL); | |
| 28 | +var p: Pair; | |
| 29 | +begin | |
| 30 | + p.a := 1; p.b := 2; | |
| 31 | + return x * 2.0, "two", p, true; | |
| 32 | +end; | |
| 33 | + | |
| 34 | +proc minmax(a, b: I64): (I64, I64); | |
| 35 | +code 48H, 89H, 0F8H, (* mov rax, rdi *) | |
| 36 | + 48H, 89H, 0F2H, (* mov rdx, rsi *) | |
| 37 | + 48H, 39H, 0F7H, (* cmp rdi, rsi *) | |
| 38 | + 7EH, 06H, (* jle +6 *) | |
| 39 | + 48H, 89H, 0F0H, (* mov rax, rsi *) | |
| 40 | + 48H, 89H, 0FAH, (* mov rdx, rdi *) | |
| 41 | + 0C3H; | |
| 42 | + | |
| 43 | +proc nothing: (I64, ERROR); | |
| 44 | +begin | |
| 45 | +end; | |
| 46 | + | |
| 47 | +proc main*; | |
| 48 | +var q, r, i: I64; e: ERROR; x: F64; s: []U8; p: Pair; ok: BOOL; a: [3]I64; u: U8; | |
| 49 | +begin | |
| 50 | + q, r := divmod(17, 5); | |
| 51 | + Out.int(q); Out.str(" "); Out.int(r); Out.ln; | |
| 52 | + table := [10, 20, 30, 40]; | |
| 53 | + i, e := find(30); | |
| 54 | + Out.int(i); if e = nil then Out.str(" nil"); end; Out.ln; | |
| 55 | + i, e := find(7); | |
| 56 | + Out.int(i); Out.str(" "); Out.str(MSG(e)); | |
| 57 | + if e = NotFound then Out.str(" is NotFound"); end; | |
| 58 | + if e # Other then Out.str(", not Other"); end; | |
| 59 | + if e # Empty then Out.str(", not Empty"); end; | |
| 60 | + Out.ln; | |
| 61 | + Out.str("["); Out.str(MSG(nil)); Out.str("]"); Out.ln; | |
| 62 | + x, s, p, ok := mixed(1.5); | |
| 63 | + Out.int(I64(x * 10.0)); Out.str(" "); Out.str(s); Out.str(" "); Out.int(p.b); | |
| 64 | + if ok then Out.str(" ok"); end; Out.ln; | |
| 65 | + a[1], g.b := divmod(9, 4); | |
| 66 | + Out.int(a[1]); Out.str(" "); Out.int(g.b); Out.ln; | |
| 67 | + q, r := minmax(9, 3); | |
| 68 | + Out.int(q); Out.int(r); Out.ln; | |
| 69 | + divmod(1, 1); | |
| 70 | + i, e := nothing(); | |
| 71 | + Out.int(i); if e = nil then Out.str(" zero"); end; Out.ln; | |
| 72 | + i := 5; | |
| 73 | + INC(i); INC(i, 10); DEC(i, 2); | |
| 74 | + a[2] := 7; DEC(a[2]); | |
| 75 | + u := 255; INC(u); | |
| 76 | + Out.int(i); Out.str(" "); Out.int(a[2]); Out.str(" "); Out.int(I64(u)); Out.ln; | |
| 77 | +end; | |
| 78 | + | |
| 79 | +end Results. |