From 03224cab4dd40daacaca3abd55dca46ecaeadbf2 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 10 Oct 2026 08:32:50 +0000 Subject: [PATCH] Step 8: enumerations and sets, record literals, defer An enumeration is a byte; its names are constants in the scope around it, and it works with relations, for and match. A set is the smallest word with a bit per name; + * - are or, and, and-not, <= and >= test subsets, in tests a bit. Record and set literals take their type from where they are used; a set literal of constants is a constant. A deferred statement is compiled where it stands as a little subroutine of the procedure, jumped over, and called at each way out of its statement list: the end, return (after the result is computed and kept) and break. Its temporaries stay reserved until it runs. --- diff --git a/PLAN.md b/PLAN.md index a80e167..ea57af7 100644 --- a/PLAN.md +++ b/PLAN.md @@ -93,7 +93,7 @@ Stage 2: the rest of the language - [x] 7. Multiple results and errors; `INC` and `DEC`. -- [ ] 8. Enumerations and sets, record literals, `defer`; examples. +- [x] 8. Enumerations and sets, record literals, `defer`; examples. - [ ] 9. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`. - [ ] 10. Generic modules, with type and procedure arguments; `lib/Str.jed`, `lib/Buf.jed`, `lib/Fmt.jed`; examples. diff --git a/examples/Flags b/examples/Flags new file mode 100755 index 0000000..538508f Binary files /dev/null and b/examples/Flags differ diff --git a/examples/Flags.expect b/examples/Flags.expect new file mode 100644 index 0000000..4bc7677 --- /dev/null +++ b/examples/Flags.expect @@ -0,0 +1,2 @@ +[rwxr-xr-- octal 754] +[rw-r-x---] diff --git a/examples/Flags.jed b/examples/Flags.jed new file mode 100644 index 0000000..e2dea05 --- /dev/null +++ b/examples/Flags.jed @@ -0,0 +1,44 @@ +(* Enumerations, sets, record literals and defer: Unix permissions as + sets, which are their bits, and a report that closes its brackets + however it leaves. *) +module Flags; +import Out; + +type + Perm = (x, w, r); (* bits 0, 1, 2, as chmod has them *) + Perms = set of Perm; + Mode = record user, group, other: Perms; end; + +proc digit(p: Perms): I64; +begin + return I64(U8(p)); +end; + +proc show(p: Perms); +begin + if r in p then Out.str("r"); else Out.str("-"); end; + if w in p then Out.str("w"); else Out.str("-"); end; + if x in p then Out.str("x"); else Out.str("-"); end; +end; + +proc report(m: Mode); +begin + Out.str("["); + defer Out.str("]\n"); + show(m.user); show(m.group); show(m.other); + if m.other = {} then return; end; + Out.str(" octal "); + Out.int(digit(m.user)); Out.int(digit(m.group)); Out.int(digit(m.other)); +end; + +proc main*; +var m: Mode; +begin + m := {user: {r, w, x}, group: {r, x}, other: {r}}; + report(m); + m.user := m.user - {x}; + m.other := {}; + report(m); +end; + +end Flags. diff --git a/src/gen.pas b/src/gen.pas index 521bd01..0ff16ca 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -92,10 +92,11 @@ procedure PassArg(var c: TCall; var a: TItem); procedure EndCall(var c: TCall; var x: TItem); +procedure ZeroTemp(var x: TItem; t: PType); procedure Construct(var x: TItem; c: PObj); procedure SetField(const x: TItem; f: PObj; var a: TItem); function MatchBegin(var x: TItem): int64; -procedure MatchTag(p: int64; tag: int64; var L: longint); +procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean); procedure CondJump(var x: TItem; var L: longint); procedure SliceOf(var x: TItem; t: PType; len: int64); @@ -107,6 +108,7 @@ function AllocPerm(size, align: int64): int64; function TempMark: int64; procedure ReleaseTemps(m: int64); +procedure KeepTemps; procedure BeginProc(sig: PType; params: PObj); procedure EnterBody(c: longint); @@ -341,6 +343,14 @@ begin if m < permTop then m := permTop; frameTop := m; +end; + +{ Every slot used so far stays reserved for the rest of the procedure: + a deferred statement's temporaries live until it runs. } +procedure KeepTemps; +begin + frameTop := frameMax; + permTop := frameMax; end; procedure TempItem(var x: TItem; t: PType; off: int64); @@ -715,7 +725,7 @@ CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); var - xt, yt, t: PType; r: TItem; scale, sh: longint; isPtr, unsign: boolean; + xt, yt, t: PType; r: TItem; scale, sh: longint; isPtr, unsign: boolean; a, b: int64; begin xt := x.typ; yt := y.typ; if y.mode in [mType, mBuiltin, mNone, mCase] then Mark('not a value'); @@ -783,7 +793,15 @@ if not ((xt^.form = fPtr) and (yt^.form = fPtr) and Same(xt^.base, yt^.base)) then Mark('different pointer types'); t := tU64; - end else + end else if (xt^.form = fEnum) and Same(xt, yt) then begin + if not (op in Rel) then Mark('enumerations only compare'); + t := xt; + end else if (xt^.form = fSet) and Same(xt, yt) then begin + if not (op in [sPlus, sTimes, sMinus, sEql, sNeq, sLeq, sGeq]) then Mark('not a set operator'); + t := xt; + end else if (op = sIn) and (yt^.form = fSet) and Same(xt, yt^.base) then + t := yt + else Mark('can''t combine ' + TypeName(xt) + ' and ' + TypeName(yt)); unsign := (t^.form = fInt) and not t^.signed; @@ -792,6 +810,17 @@ if t^.form = fReal then begin if not FoldReal(op, x, y, r) then Mark('not an F64 operator'); x := r; + end else if t^.form = fSet then begin + a := x.val; b := y.val; + case op of + sPlus: MakeConst(x, t, a or b); + sTimes: MakeConst(x, t, a and b); + sMinus: MakeConst(x, t, a and not b); + sIn: MakeConst(x, tBool, (b shr a) and 1); + sLeq: MakeConst(x, tBool, ord(a and not b = 0)); + sGeq: MakeConst(x, tBool, ord(b and not a = 0)); + else MakeConst(x, tBool, ord((a = b) = (op = sEql))); + end; end else if op in Rel then MakeConst(x, tBool, FoldInt(op, x.val, y.val, t)) else @@ -825,6 +854,24 @@ sGeq: begin Ucomisd(0, 1); SetCC(ccAE, RAX) end; end; if not (op in Rel) then MovqRX(RAX, 0); + end else if t^.form = fSet then begin + case op of + sPlus: Alu(aluOr, RAX, RCX); + sTimes: Alu(aluAnd, RAX, RCX); + sMinus: begin Unary(2, RCX); Alu(aluAnd, RAX, RCX) end; + sLeq, sGeq: begin + if op = sLeq then Unary(2, RCX) else Unary(2, RAX); + Alu(aluAnd, RAX, RCX); + TestRR(RAX, RAX); + SetCC(ccE, RAX); + end; + sIn: begin + MovRR(RDX, RCX); MovRR(RCX, RAX); MovRR(RAX, RDX); + Shift(shShr, RAX); + AluI(aluAnd, RAX, 1); + end; + else Alu(aluCmp, RAX, RCX); SetCC(CcUnsigned[op], RAX); + end; end else if op in Rel then begin Alu(aluCmp, RAX, RCX); if (t^.form = fInt) and t^.signed or (t^.form = fConst) then SetCC(CcSigned[op], RAX) @@ -857,7 +904,7 @@ Norm(t); end; x.mode := mReg; - if op in Rel then x.typ := tBool else x.typ := t; + if op in Rel + [sIn] then x.typ := tBool else x.typ := t; end; procedure Convert(var x: TItem; t: PType); @@ -866,9 +913,20 @@ s := x.typ; if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then Mark('can''t convert this'); if Same(s, t) then exit; - if IsInt(s) and (t^.form = fInt) then begin + if (IsInt(s) or (s^.form in [fEnum, fSet])) and (t^.form = fInt) then begin if x.mode = mConst then x.val := Cut(x.val, t) else begin Value(x); Norm(t) end; + end else if IsInt(s) and (t^.form = fEnum) then begin + if x.mode = mConst then begin + if (x.val < 0) or (x.val >= t^.len) then Mark('not a position of ' + TypeName(t)) + end else begin Value(x); CmpI(RAX, t^.len); Check(ccB) end; + end else if IsInt(s) and (t^.form = fSet) then begin + if x.mode = mConst then begin + if t^.len < 64 then x.val := x.val and ((int64(1) shl t^.len) - 1) + end else begin + Value(x); + if t^.len < 64 then begin MovRI(RCX, (int64(1) shl t^.len) - 1); Alu(aluAnd, RAX, RCX) end; + end; end else if IsInt(s) and (t^.form = fReal) then begin if x.mode = mConst then begin if (s^.form = fInt) and not s^.signed then r := qword(x.val) else r := x.val; @@ -1018,20 +1076,25 @@ { Variants } -procedure Construct(var x: TItem; c: PObj); -var t: PType; tmp: int64; +{ A temporary of type t, all zero: for variants and record literals. } +procedure ZeroTemp(var x: TItem; t: PType); +var tmp: int64; begin - t := c^.typ; tmp := AllocTemp(t^.size, t^.align); Lea(RDI, Mem(RBP, tmp)); MovRI(RCX, t^.size); MovRI(RAX, 0); RepStosb; - MovRI(RAX, c^.val); - Store(Mem(RBP, tmp), RAX, 8); TempItem(x, t, tmp); end; +procedure Construct(var x: TItem; c: PObj); +begin + ZeroTemp(x, c^.typ); + MovRI(RAX, c^.val); + Store(Mem(RBP, x.off), RAX, 8); +end; + procedure SetField(const x: TItem; f: PObj; var a: TItem); begin Coerce(a, f^.typ); @@ -1046,20 +1109,22 @@ end; end; +{ The slot match tests: a variant's address, or an enumeration's value. } function MatchBegin(var x: TItem): int64; var p: int64; begin - if (x.mode <> mVar) or (x.typ^.form <> fVariant) then Mark('match needs a variant'); + if x.typ^.form = fEnum then exit(ToSlot(x)); + if (x.mode <> mVar) or (x.typ^.form <> fVariant) then Mark('match needs a variant or an enumeration'); Address(x, RAX); p := AllocTemp(8, 8); Store(Mem(RBP, p), RAX, 8); MatchBegin := p; end; -procedure MatchTag(p: int64; tag: int64; var L: longint); +procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean); begin Load(RAX, Mem(RBP, p), 8, false); - Load(RAX, Mem(RAX, 0), 8, false); + if not isEnum then Load(RAX, Mem(RAX, 0), 8, false); CmpI(RAX, tag); Jcc(ccNE, L); end; diff --git a/src/parse.pas b/src/parse.pas index 19bb4ec..6383400 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -41,13 +41,25 @@ uses BaseUnix; +type + PLoop = ^TLoop; + TLoop = record + brk: longint; { the chain of its breaks } + base: longint; { the deferred statements in force outside it } + end; + var checkMode: boolean; { reading a file only for its interface } inTypes: boolean; { in a type section: ^T may name a later type } fwdTypes: array[0..255] of PType; numFwd: longint; declExported: boolean; { the export mark of the declaration being read } - curBreak: PLongint; { the break chain of the innermost loop, or nil } + curLoop: PLoop; { the innermost loop, or nil } + defers: array[0..63] of longint; { the deferred statements in force } + numDefers: longint; + deferring: boolean; { compiling a deferred statement } + directDefer: boolean; { ... as the statement itself, not in a list } + deferSlot: int64; { keeps a result while deferred statements run } curResult: PType; { the result type of the procedure being compiled } tArgv, tBodySig: PType; nameRt, nameFail, nameEq, nameMain, nameBody: longint; @@ -55,6 +67,7 @@ procedure Expression(var x: TItem; want: PType); forward; function ParseType: PType; forward; procedure Statements; forward; +procedure Statement; forward; { Little helpers } @@ -306,6 +319,42 @@ Signature := t; end; +function EnumType: PType; +var t: PType; o, last: PObj; +begin + t := NewType(fEnum, 1, 1); + last := nil; + repeat + Next; + if tok <> sIdent then Mark('name expected'); + o := NewObj(oConst, id, t); + o^.val := t^.len; + o^.exported := declExported; + Declare(o); + if last = nil then t^.fields := o else last^.next := o; + last := o; + inc(t^.len); + Next; + until tok <> sComma; + Expect(sRparen, ')'); + if t^.len > 256 then Mark('more than 256 names'); + EnumType := t; +end; + +function SetOf(e: PType): PType; +var t: PType; +begin + if (e^.form <> fEnum) or (e^.len > 64) then Mark('a set is of an enumeration of at most 64 names'); + t := NewType(fSet, 8, 8); + if e^.len <= 8 then t^.size := 1 + else if e^.len <= 16 then t^.size := 2 + else if e^.len <= 32 then t^.size := 4; + t^.align := t^.size; + t^.base := e; + t^.len := e^.len; + SetOf := t; +end; + function ParseType: PType; var t: PType; o: PObj; n: int64; vol: boolean; begin @@ -350,7 +399,12 @@ sPacked, sRecord: t := RecordType; sVariant: t := VariantType; sProc: begin Next; t := Signature end; - sLparen, sSet: Mark('enumerations and sets come in stage 2'); + sLparen: t := EnumType; + sSet: begin + Next; + Expect(sOf, 'of'); + t := SetOf(ParseType()); + end; else Mark('type expected'); end; ParseType := t; @@ -626,6 +680,76 @@ TempItem(x, at, off); end; +{ A record literal: a zeroed temporary, with the fields named filled in. } +procedure RecordLiteral(var x: TItem; t: PType); +var f: PObj; a: TItem; seen: array[0..63] of longint; n, i: longint; +begin + Next; + ZeroTemp(x, t); + n := 0; + if tok <> sRbrace then + repeat + if tok <> sIdent then Mark('field expected'); + f := Find(t^.scope, id); + if f = nil then Mark('no field ' + NameOf(id)); + if not f^.exported and (t^.module <> curModule) then Mark('field ' + NameOf(id) + ' isn''t exported'); + for i := 0 to n - 1 do if seen[i] = id then Mark(NameOf(id) + ' twice'); + if n <= High(seen) then begin seen[n] := id; inc(n) end; + Next; + Expect(sColon, ':'); + Expression(a, f^.typ); + SetField(x, f, a); + if tok <> sComma then break; + Next; + until false; + Expect(sRbrace, '}'); +end; + +{ A set literal: the bits of the constant names, or'd at run time with + those of the others. } +procedure SetLiteral(var x: TItem; t: PType); +var e: TItem; bits, slot: int64; +begin + Next; + if (t <> nil) and (t^.form <> fSet) then Mark('a set literal needs a set type'); + bits := 0; + slot := 0; + if tok <> sRbrace then + repeat + if t = nil then Expression(e, nil) else Expression(e, t^.base); + if t = nil then begin + if e.typ^.form <> fEnum then Mark('a set holds names of an enumeration'); + t := SetOf(e.typ); + end; + if not Same(e.typ, t^.base) then Mark('not a name of ' + TypeName(t^.base)); + if e.mode = mConst then bits := bits or (int64(1) shl e.val) + else begin + if slot = 0 then begin + slot := AllocTemp(8, 8); + MovRI(RAX, 0); + Store(Mem(RBP, slot), RAX, 8); + end; + Value(e); + MovRR(RCX, RAX); + MovRI(RAX, 1); + Shift(shShl, RAX); + AluM(aluOr, RAX, Mem(RBP, slot)); + Store(Mem(RBP, slot), RAX, 8); + end; + if tok <> sComma then break; + Next; + until false; + Expect(sRbrace, '}'); + if t = nil then Mark('{} needs a known set type'); + MakeConst(x, t, bits); + if slot <> 0 then begin + Load(RAX, Mem(RBP, slot), 8, false); + MovRI(RCX, bits); + Alu(aluOr, RAX, RCX); + x.mode := mReg; + end; +end; + procedure Factor(var x: TItem; want: PType); var called, vol: boolean; t: PType; begin @@ -645,7 +769,8 @@ sLparen: begin Next; Expression(x, want); Expect(sRparen, ')') end; sNot: begin Next; Factor(x, nil); NotOp(x) end; sLbrak: ArrayLiteral(x, want); - sLbrace: Mark('record and set literals come in stage 2'); + sLbrace: if (want <> nil) and (want^.form = fRecord) then RecordLiteral(x, want) + else SetLiteral(x, want); sArrow: begin Next; vol := tok = sVolatile; @@ -694,22 +819,30 @@ var op: TSym; y: TItem; L: longint; begin SimpleExpr(x, want); - if tok in [sEql, sNeq, sLss, sLeq, sGtr, sGeq] then begin + if tok in [sEql, sNeq, sLss, sLeq, sGtr, sGeq, sIn] then begin op := tok; Next; Op1(op, x, L); - SimpleExpr(y, nil); + if op = sIn then SimpleExpr(y, nil) else SimpleExpr(y, x.typ); Op2(op, x, y, L); - end else if tok = sIn then Mark('sets come in stage 2'); + end; end; { Statements } -procedure Loop(var brk: longint; var old: PLongint); +procedure Loop(var lp: TLoop; var old: PLoop); +begin + lp.brk := NoLabel; + lp.base := numDefers; + old := curLoop; + curLoop := @lp; +end; + +{ Runs the deferred statements in force above base, newest first. } +procedure RunDefers(base: longint); +var i: longint; begin - brk := NoLabel; - old := curBreak; - curBreak := @brk; + for i := numDefers - 1 downto base do CallBack(defers[i]); end; procedure IfStatement; @@ -743,7 +876,7 @@ end; procedure WhileStatement; -var x: TItem; top, L, brk: longint; old: PLongint; +var x: TItem; top, L: longint; lp: TLoop; old: PLoop; begin Next; top := pc; @@ -751,41 +884,41 @@ Expect(sDo, 'do'); L := NoLabel; CondJump(x, L); - Loop(brk, old); + Loop(lp, old); Statements; JmpBack(top); Expect(sEnd, 'end'); FixLink(L); - FixLink(brk); - curBreak := old; + FixLink(lp.brk); + curLoop := old; end; procedure RepeatStatement; -var x: TItem; top, L, brk: longint; old: PLongint; +var x: TItem; top, L: longint; lp: TLoop; old: PLoop; begin Next; top := pc; - Loop(brk, old); + Loop(lp, old); Statements; - curBreak := old; + curLoop := old; Expect(sUntil, 'until'); Expression(x, nil); L := NoLabel; CondJump(x, L); FixLinkTo(L, top); - FixLink(brk); + FixLink(lp.brk); end; procedure ForStatement; var - o: PObj; v, a, b, s: TItem; bslot, step: int64; top, exitL, brk: longint; - old: PLongint; + o: PObj; v, a, b, s: TItem; bslot, step: int64; top, exitL: longint; + lp: TLoop; old: PLoop; begin Next; if tok <> sIdent then Mark('name expected'); o := Lookup(id); - if (o = nil) or (o^.cls <> oVar) or (o^.typ^.form <> fInt) then - Mark('a for loop needs an integer variable'); + if (o = nil) or (o^.cls <> oVar) or not (o^.typ^.form in [fInt, fEnum]) then + Mark('a for loop needs an integer or enumeration variable'); Next; Expect(sBecomes, ':='); MakeItem(v, o); @@ -815,13 +948,13 @@ end else begin if step > 0 then Jcc(ccA, exitL) else Jcc(ccB, exitL) end; - Loop(brk, old); + Loop(lp, old); Statements; - curBreak := old; + curLoop := old; Expect(sEnd, 'end'); ForNext(o, bslot, step, exitL, top); FixLink(exitL); - FixLink(brk); + FixLink(lp.brk); end; procedure MatchStatement; @@ -854,7 +987,7 @@ if c^.val <= High(seen) then seen[c^.val] := true; Next; L := NoLabel; - MatchTag(p, c^.val, L); + MatchTag(p, c^.val, L, t^.form = fEnum); OpenScope(NewScope); if tok = sLparen then begin Next; @@ -935,6 +1068,32 @@ end; end; +{ defer s: s is compiled where it stands, as a little subroutine of the + procedure, jumped over; each way out of the statement list calls it. } +procedure Defer; +var L, pos, oldDepth: longint; oldLoop: PLoop; oldDeferring: boolean; +begin + Next; + if directDefer then Mark('defer can''t be deferred'); + if numDefers > High(defers) then Mark('too many deferred statements'); + L := NoLabel; + Jmp(L); + pos := pc; + oldDepth := depth; oldLoop := curLoop; oldDeferring := deferring; + depth := 1; { the return address } + curLoop := nil; + deferring := true; + directDefer := true; + Statement; + directDefer := false; + KeepTemps; + depth := oldDepth; curLoop := oldLoop; deferring := oldDeferring; + Ret; + FixLink(L); + defers[numDefers] := pos; + inc(numDefers); +end; + procedure Statement; var x, y: TItem; called: boolean; m: int64; c: TCall; begin @@ -964,33 +1123,50 @@ sMatch: MatchStatement; sReturn: begin Next; - if tok = sSemicolon then ReturnNone - else begin + if deferring then Mark('return can''t leave a deferred statement'); + if tok = sSemicolon then begin + RunDefers(0); + ReturnNone; + end else begin if (curResult <> nil) and curResult^.tuple then ReturnResults else begin Expression(x, curResult); ReturnValue(x); end; + if numDefers > 0 then begin + if deferSlot = 0 then deferSlot := AllocPerm(8, 8); + Store(Mem(RBP, deferSlot), RAX, 8); + RunDefers(0); + Load(RAX, Mem(RBP, deferSlot), 8, false); + end; ReturnJump; end; end; sBreak: begin Next; - if curBreak = nil then Mark('break outside a loop'); - Jmp(curBreak^); + if curLoop = nil then Mark('break outside a loop'); + RunDefers(curLoop^.base); + Jmp(curLoop^.brk); end; - sDefer: Mark('defer comes in stage 2'); + sDefer: Defer; else Mark('statement expected'); end; ReleaseTemps(m); end; procedure Statements; +var base: longint; old: boolean; begin + base := numDefers; + old := directDefer; + directDefer := false; while not (tok in [sEnd, sElse, sElsif, sUntil, sBar, sEof]) do begin Statement; Expect(sSemicolon, ';'); end; + RunDefers(base); + numDefers := base; + directDefer := old; end; { Declarations } @@ -1033,6 +1209,7 @@ q := q^.next; end; BeginProc(sig, params); + deferSlot := 0; o := params; while o <> nil do begin q := o^.next; @@ -1308,6 +1485,7 @@ body := NewChunk(secText, n, nameBody); m^.body := body; BeginProc(tBodySig, nil); + deferSlot := 0; curResult := nil; EnterBody(body); Statements; @@ -1445,7 +1623,7 @@ SetBss(argvChunk, 8, 8); tArgv := NewPtr(NewPtr(tU8, false), false); tBodySig := NewType(fProc, 8, 8); - curBreak := nil; + curLoop := nil; numPending := 0; firstModule := nil; lastModule := nil; diff --git a/src/sym.pas b/src/sym.pas index 473113e..41f8c8c 100644 --- a/src/sym.pas +++ b/src/sym.pas @@ -313,7 +313,7 @@ case a^.form of fPtr: Alike := (a^.vol = b^.vol) and Same(a^.base, b^.base); fArray: Alike := (a^.len = b^.len) and Same(a^.base, b^.base); - fSlice: Alike := Same(a^.base, b^.base); + fSlice, fSet: Alike := Same(a^.base, b^.base); fProc, fRecord: begin { a record is the same only as itself, but for several results } if (a^.form = fRecord) and not (a^.tuple and b^.tuple) then exit(false); @@ -384,7 +384,7 @@ Mix(h, ord(t^.form)); Mix(h, ord(t^.vol) + 2 * ord(t^.variadic)); Mix(h, t^.len); - if t^.form in [fPtr, fArray, fSlice, fProc] then Shape(h, t^.base, false); + if t^.form in [fPtr, fArray, fSlice, fProc, fSet] then Shape(h, t^.base, false); f := t^.fields; while f <> nil do begin if (t^.form <> fRecord) or f^.exported then begin diff --git a/src/x64.pas b/src/x64.pas index 882f325..ec64b76 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -116,6 +116,7 @@ procedure Push(r: longint); procedure Pop(r: longint); procedure CallChunk(c: longint); +procedure CallBack(target: longint); procedure CallR(r: longint); procedure Ret; procedure Leave; @@ -474,6 +475,12 @@ Put1($E8); AddFixup(pc, c, 0); Put4(0); +end; + +procedure CallBack(target: longint); +begin + Put1($E8); + Put4(target - (pc + 4)); end; procedure CallR(r: longint); diff --git a/test/err/DeferBreak.err b/test/err/DeferBreak.err new file mode 100644 index 0000000..fa5a823 --- /dev/null +++ b/test/err/DeferBreak.err @@ -0,0 +1 @@ +break outside a loop diff --git a/test/err/DeferBreak.jed b/test/err/DeferBreak.jed new file mode 100644 index 0000000..30abd59 --- /dev/null +++ b/test/err/DeferBreak.jed @@ -0,0 +1,8 @@ +module DeferBreak; +proc f; +begin + while true do + defer break; + end; +end; +end DeferBreak. diff --git a/test/err/DeferDefer.err b/test/err/DeferDefer.err new file mode 100644 index 0000000..a25f3db --- /dev/null +++ b/test/err/DeferDefer.err @@ -0,0 +1 @@ +defer can't be deferred diff --git a/test/err/DeferDefer.jed b/test/err/DeferDefer.jed new file mode 100644 index 0000000..acbb0f6 --- /dev/null +++ b/test/err/DeferDefer.jed @@ -0,0 +1,6 @@ +module DeferDefer; +proc f; +begin + defer defer f; +end; +end DeferDefer. diff --git a/test/err/DeferReturn.err b/test/err/DeferReturn.err new file mode 100644 index 0000000..dcaca3b --- /dev/null +++ b/test/err/DeferReturn.err @@ -0,0 +1 @@ +return can't leave a deferred statement diff --git a/test/err/DeferReturn.jed b/test/err/DeferReturn.jed new file mode 100644 index 0000000..2fdb85a --- /dev/null +++ b/test/err/DeferReturn.jed @@ -0,0 +1,7 @@ +module DeferReturn; +proc f: I64; +begin + defer return 1; + return 0; +end; +end DeferReturn. diff --git a/test/run/Defers b/test/run/Defers new file mode 100755 index 0000000..53b89d6 Binary files /dev/null and b/test/run/Defers differ diff --git a/test/run/Defers.expect b/test/run/Defers.expect new file mode 100644 index 0000000..0592968 --- /dev/null +++ b/test/run/Defers.expect @@ -0,0 +1,5 @@ +body body done +()30 +56 +[...*][...+**] +0 diff --git a/test/run/Defers.jed b/test/run/Defers.jed new file mode 100644 index 0000000..388406b --- /dev/null +++ b/test/run/Defers.jed @@ -0,0 +1,71 @@ +module Defers; +import Out; + +type Pair = record a, b: I64; end; + +var depth: I64; + +proc enter(s: []U8); +begin + Out.str(s); INC(depth); +end; + +proc leave(s: []U8); +begin + Out.str(s); DEC(depth); +end; + +proc real(x: F64): F64; +begin + enter("("); + defer leave(")"); + return x * 2.0; +end; + +proc pair(n: I64): Pair; +var p: Pair; +begin + defer p.a := 99; + p.a := n; p.b := n + 1; + return p; +end; + +proc begin_end(i: I64); +var k: I64; +begin + defer for k := 1 to i do Out.str("*"); end; + if i = 1 then return; end; + Out.str("+"); +end; + +proc nested; +var i, j: I64; +begin + for i := 1 to 2 do + enter("["); + defer leave("]"); + defer begin_end(i); + j := 0; + while true do + defer Out.str("."); + INC(j); + if j = 3 then break; end; + end; + end; +end; + +proc main*; +var p: Pair; +begin + Out.int(I64(real(1.5) * 10.0)); Out.ln; + p := pair(5); + Out.int(p.a); Out.int(p.b); Out.ln; + nested; + Out.ln; + Out.int(depth); Out.ln; +end; + +begin + defer Out.str("body done\n"); + Out.str("body "); +end Defers. diff --git a/test/run/Kinds b/test/run/Kinds new file mode 100755 index 0000000..9ac195a Binary files /dev/null and b/test/run/Kinds differ diff --git a/test/run/Kinds.expect b/test/run/Kinds.expect new file mode 100644 index 0000000..21c1243 --- /dev/null +++ b/test/run/Kinds.expect @@ -0,0 +1,8 @@ +023 2 0 in out sub sup ne eq +13 10 1 2 lt green other +120 0 2 +47mid +24 +50 LLCBA +-1 LLDBA +3 \ No newline at end of file diff --git a/test/run/Kinds.jed b/test/run/Kinds.jed new file mode 100644 index 0000000..c5c6d47 --- /dev/null +++ b/test/run/Kinds.jed @@ -0,0 +1,95 @@ +module Kinds; +import Out; + +type + Color* = (red, green, blue, black); + Colors* = set of Color; + Cell = record ch: U8; attr: U8; fg: Color; end; + Point = record x, y: I64; name: []U8; end; + Line = record a, b: Point; end; + +var log: [8]U8; nlog: I64; + +proc note(c: U8); +begin + log[nlog] := c; + INC(nlog); +end; + +proc show(s: Colors); +var c: Color; +begin + for c := red to black do + if c in s then Out.int(I64(c)); end; + end; + Out.str(" "); +end; + +proc name(c: Color): []U8; +begin + match c of + | red: return "red"; + | green: return "green"; + | else: return "other"; + end; +end; + +proc deferred(n: I64): I64; +var i: I64; +begin + defer note('A'); + defer note('B'); + for i := 1 to 3 do + defer note('L'); + if i = 2 then break; end; + end; + if n > 0 then + defer note('C'); + return n * 10; + end; + note('D'); + return -1; +end; + +proc mid(a, b: Point): Point; +begin + return {x: (a.x + b.x) div 2, y: (a.y + b.y) div 2, name: "mid"}; +end; + +proc main*; +var s, t: Colors; c: Color; cell: Cell; p: Point; l: Line; r: I64; one: Colors; +begin + s := {red, blue}; + t := {blue, black}; + show(s + t); show(s * t); show(s - t); show({}); + if red in s then Out.str("in "); end; + if not (green in s) then Out.str("out "); end; + if s * t <= t then Out.str("sub "); end; + if s + t >= s then Out.str("sup "); end; + if s # t then Out.str("ne "); end; + if s = {blue, red} then Out.str("eq"); end; + Out.ln; + c := green; + one := {c, black}; + show(one); + Out.int(I64(U8(one))); Out.str(" "); + Out.int(SIZE(Colors)); Out.str(" "); + Out.int(I64(Color(2))); Out.str(" "); + if c < blue then Out.str("lt "); end; + Out.str(name(c)); Out.str(" "); Out.str(name(black)); Out.ln; + cell := {ch: 'x', fg: blue}; + Out.int(I64(cell.ch)); Out.str(" "); Out.int(I64(cell.attr)); Out.str(" "); Out.int(I64(cell.fg)); Out.ln; + p := mid({x: 2, y: 4}, {y: 10, x: 6}); + Out.int(p.x); Out.int(p.y); Out.str(p.name); Out.ln; + l := {a: {x: 1, y: 2}, b: p}; + Out.int(l.a.y); Out.int(l.b.x); Out.ln; + r := deferred(5); + Out.int(r); Out.str(" "); Out.str(log[0..nlog]); Out.ln; + nlog := 0; + r := deferred(0); + Out.int(r); Out.str(" "); Out.str(log[0..nlog]); Out.ln; + Out.int(I64(Color(I64(c) + 2))); + c := Color(nlog); +end; + +end Kinds. diff --git a/test/run/Kinds.status b/test/run/Kinds.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/Kinds.status @@ -0,0 +1 @@ +2