raz / commit 39a17b57

commit
39a17b5764c4533717325011054cc3f55df83f2a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
f4ab7cf6
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.

patch · log

12 files changed, +357 -27

PLAN.md (history) modified +1 -1

@@ -92,7 +92,7 @@
9292
9393 Stage 2: the rest of the language
9494
95-- [ ] 7. Multiple results and errors.
95+- [x] 7. Multiple results and errors; `INC` and `DEC`.
9696 - [ ] 8. Enumerations and sets, record literals, `defer`; examples.
9797 - [ ] 9. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`.
9898 - [ ] 10. Generic modules, with type and procedure arguments;

doc/jed.md modified +6 -0

@@ -507,6 +507,8 @@
507507 | `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 |
508508 | `MSG(e)` | the message of the error `e`, a `[]U8` |
509509 | `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` |
510512 | `FENCE` | a full memory barrier |
511513 | `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; atomically |
512514 | `XCHG(p, x)` | sets `p^ := x` and returns the value before, atomically |
@@ -517,6 +519,10 @@
517519 `SYSCALL` exists only on Linux targets. Other systems have no stable
518520 system call interface, and their modules call the C library instead
519521 (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.
520526
521527 For `CAS`, `XCHG` and `XADD`, `p` is a `^T` or `^VOLATILE T` where `T`
522528 is an 8-byte integer or pointer type (an integer type for `XADD`). They

src/gen.pas modified +123 -14

@@ -31,7 +31,7 @@
3131
3232 type
3333 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 }
3535
3636 TItem = record
3737 mode: TMode;
@@ -53,6 +53,7 @@
5353 n: longint;
5454 target: int64; { the frame slot of a procedure value, if any }
5555 chunk: longint; { else the procedure's chunk }
56+ code: boolean; { a code procedure: two results come in rax and rdx }
5657 end;
5758
5859 var
@@ -110,7 +111,13 @@
110111 procedure BeginProc(sig: PType; params: PObj);
111112 procedure EnterBody(c: longint);
112113 procedure ReturnValue(var x: TItem);
114+procedure ReturnField(f: PObj; var x: TItem);
115+procedure ReturnJump;
113116 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;
114121 procedure EndProc(c: longint);
115122 procedure CmpI(r: longint; v: int64);
116123 procedure PushRax;
@@ -217,6 +224,13 @@
217224 else begin MovRI(R11, v); Alu(aluCmp, r, R11) end;
218225 end;
219226
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+
220234 function MemOf(var x: TItem; r: longint): TMem;
221235 begin
222236 case x.base of
@@ -227,6 +241,11 @@
227241 x.base := bReg; x.reg := r;
228242 MemOf := Mem(r, x.off);
229243 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;
230249 else MemOf := Mem(x.reg, x.off);
231250 end;
232251 end;
@@ -258,7 +277,7 @@
258277 var m: TMem;
259278 begin
260279 case x.mode of
261- mConst: MovRI(RAX, x.val);
280+ mConst: ConstTo(RAX, x);
262281 mVar: begin
263282 if not IsScalar(x.typ) then Mark('not a single value');
264283 m := MemOf(x, RAX);
@@ -266,7 +285,11 @@
266285 end;
267286 mReg: ;
268287 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;
270293 else Mark('not a value');
271294 end;
272295 x.mode := mReg;
@@ -391,7 +414,7 @@
391414 if (s^.form = fConst) and (t^.form = fInt) then begin
392415 if (x.mode = mConst) and not Fits(x.val, t) then Mark('constant doesn''t fit ' + TypeName(t));
393416 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
395418 x.typ := t
396419 else if (s^.form = fPtr) and (t^.form = fPtr) and t^.vol and not s^.vol
397420 and Same(s^.base, t^.base) then
@@ -752,10 +775,10 @@
752775 end else if (xt^.form = fPtr) and (yt^.form = fPtr) and (op = sMinus) then begin
753776 if not Same(xt, yt) then Mark('different pointer types');
754777 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
756779 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 #');
759782 if (xt^.form <> fNil) and (yt^.form <> fNil) and not Same(xt, yt) then
760783 if not ((xt^.form = fPtr) and (yt^.form = fPtr) and Same(xt^.base, yt^.base)) then
761784 Mark('different pointer types');
@@ -779,11 +802,11 @@
779802 { x into rax, y into rcx }
780803 if y.mode = mConst then begin
781804 if x.mode = mStack then PopR(RAX) else Value(x);
782- MovRI(RCX, y.val);
805+ ConstTo(RCX, y);
783806 end else begin
784807 Value(y);
785808 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);
787810 end;
788811
789812 if t^.form = fReal then begin
@@ -893,8 +916,10 @@
893916 procedure BeginCall(var c: TCall; var x: TItem);
894917 begin
895918 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;
897921 end else begin
922+ c.code := false;
898923 if x.typ^.form <> fProc then Mark('not a procedure');
899924 c.sig := x.typ; c.chunk := -1;
900925 c.target := ToSlot(x);
@@ -942,7 +967,7 @@
942967 ni := 0; nf := 0;
943968 if (res <> nil) and IsAggregate(res) then begin
944969 ret := AllocTemp(res^.size, res^.align);
945- ni := 1;
970+ if not c.code then ni := 1;
946971 end;
947972 { the System V classes: each argument in the next free register of
948973 its kind, or else on the stack }
@@ -963,7 +988,7 @@
963988 Push(RAX);
964989 end;
965990 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;
967992 for i := 0 to c.n - 1 do
968993 if not onStack[i] then
969994 if isF[i] then begin MovsdXM(nf, Mem(RBP, c.slots[i])); inc(nf) end
@@ -974,6 +999,10 @@
974999 CallR(R11);
9751000 end;
9761001 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;
9771006 FillChar(x, SizeOf(x), 0);
9781007 if res = nil then begin
9791008 x.mode := mNone; x.typ := tNone
@@ -1136,11 +1165,91 @@
11361165 Load(RDI, Mem(RBP, retSlot), 8, false);
11371166 Copy(result^.size);
11381167 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));
11391179 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);
11421185 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);
11431193 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);
11441253 end;
11451254
11461255 procedure ReturnNone;

src/parse.pas modified +111 -10

@@ -11,7 +11,7 @@
1111 are read for their declarations only, and must export the same
1212 interface.
1313
14- Depends on: scan, tok, x64, gen.
14+ Depends on: scan, sym, x64, gen.
1515 Leaves out (for later stages): multiple results, errors, enumerations
1616 and sets, record literals, defer, NEW, generic modules, extern. }
1717
@@ -234,6 +234,33 @@
234234 VariantType := t;
235235 end;
236236
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+
237264 function Signature: PType;
238265 var
239266 t, pt: PType; p, first, last: PObj; isVar: boolean; n: longint;
@@ -274,8 +301,7 @@
274301 t^.len := n;
275302 if tok = sColon then begin
276303 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;
279305 end;
280306 Signature := t;
281307 end;
@@ -450,7 +476,21 @@
450476 x.mode := mReg;
451477 end;
452478 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');
454494 end;
455495 Expect(sRparen, ')');
456496 end;
@@ -600,6 +640,7 @@
600640 sIdent: begin
601641 Designator(x, called);
602642 if x.mode = mNone then Mark('this procedure returns nothing');
643+ if x.typ^.tuple then Mark('several results go only to a, b := ...');
603644 end;
604645 sLparen: begin Next; Expression(x, want); Expect(sRparen, ')') end;
605646 sNot: begin Next; Factor(x, nil); NotOp(x) end;
@@ -853,6 +894,47 @@
853894 FixLink(endL);
854895 end;
855896
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+
856938 procedure Statement;
857939 var x, y: TItem; called: boolean; m: int64; c: TCall;
858940 begin
@@ -867,7 +949,7 @@
867949 Expression(y, x.typ);
868950 Assign(x, y);
869951 end else if tok = sComma then
870- Mark('several results come in stage 2')
952+ MultiAssign(x)
871953 else if not called then begin
872954 if (x.mode = mProc) or ((x.mode in [mVar, mReg]) and (x.typ^.form = fProc)) then begin
873955 BeginCall(c, x);
@@ -884,8 +966,12 @@
884966 Next;
885967 if tok = sSemicolon then ReturnNone
886968 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;
889975 end;
890976 end;
891977 sBreak: begin
@@ -977,7 +1063,14 @@
9771063 q := q^.next;
9781064 end;
9791065 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');
9811074 BeginChunk(p^.chunk);
9821075 repeat
9831076 v := ConstExpr;
@@ -1039,12 +1132,20 @@
10391132 Next;
10401133 while tok = sIdent do begin
10411134 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;
10431141 Expect(sEql, '=');
10441142 Expression(x, nil);
10451143 o := NewObj(oConst, n, x.typ);
10461144 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
10481149 else o^.val := ConstValue(x);
10491150 Declare(o);
10501151 Expect(sSemicolon, ';');

src/sym.pas modified +11 -2

@@ -39,6 +39,7 @@
3939 vol: boolean; { fPtr: ^VOLATILE }
4040 packd: boolean; { fRecord }
4141 variadic: boolean; { fProc: ends in ... }
42+ tuple: boolean; { fRecord: the results of a procedure that has several }
4243 base: PType; { fPtr, fArray, fSlice: what it holds; fProc: result }
4344 len: int64; { fArray: elements; fProc: parameters; fVariant: cases }
4445 fields: PObj; { fRecord: fields; fVariant: cases; fProc: parameters }
@@ -91,12 +92,13 @@
9192 { built-in procedures }
9293 bSize = 0; bAdr = 1; bLen = 2; bNew = 3; bMsg = 4; bSyscall = 5;
9394 bFence = 6; bCas = 7; bXchg = 8; bXadd = 9; bArgc = 10; bArgv = 11;
95+ bInc = 12; bDec = 13;
9496
9597 MaxDepth = 64; { scopes open at once }
9698
9799 var
98100 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;
100102 curModule: PModule;
101103
102104 function NewType(form: TForm; size, align: int64): PType;
@@ -312,7 +314,9 @@
312314 fPtr: Alike := (a^.vol = b^.vol) and Same(a^.base, b^.base);
313315 fArray: Alike := (a^.len = b^.len) and Same(a^.base, b^.base);
314316 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);
316320 if (a^.len <> b^.len) or (a^.variadic <> b^.variadic) then exit(false);
317321 if (a^.base = nil) <> (b^.base = nil) then exit(false);
318322 if (a^.base <> nil) and not Same(a^.base, b^.base) then exit(false);
@@ -448,6 +452,7 @@
448452 tU64 := Basic('U64', fInt, 8, false);
449453 tBool := Basic('BOOL', fBool, 1, false);
450454 tReal := Basic('F64', fReal, 8, false);
455+ tError := Basic('ERROR', fError, 8, false);
451456 tConst := NewType(fConst, 8, 8);
452457 tConst^.signed := true;
453458 tNil := NewType(fNil, 8, 8);
@@ -455,6 +460,8 @@
455460 tNone := NewType(fNone, 0, 1);
456461 tPtrU8 := NewType(fPtr, 8, 8);
457462 tPtrU8^.base := tU8;
463+ tBytes := NewType(fSlice, 16, 8);
464+ tBytes^.base := tU8;
458465 Enter('true', oConst, tBool, 1);
459466 Enter('false', oConst, tBool, 0);
460467 Enter('nil', oConst, tNil, 0);
@@ -470,4 +477,6 @@
470477 Enter('XADD', oBuiltin, nil, bXadd);
471478 Enter('ARGC', oBuiltin, nil, bArgc);
472479 Enter('ARGV', oBuiltin, nil, bArgv);
480+ Enter('INC', oBuiltin, nil, bInc);
481+ Enter('DEC', oBuiltin, nil, bDec);
473482 end.

test/err/ErrOrder.err added +1 -0

@@ -0,0 +1 @@
1+compare only with = and #

test/err/ErrOrder.jed added +7 -0

@@ -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.

test/err/Tuple.err added +1 -0

@@ -0,0 +1 @@
1+several results go only to a, b := ...

test/err/Tuple.jed added +8 -0

@@ -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.

test/run/Results added (executable)

Binary file; the patch has it.

test/run/Results.expect (history) added +9 -0

@@ -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

test/run/Results.jed added +79 -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.