(* Parse: the parser. Reads modules by recursive descent and has Gen emit code as it goes: one pass, no tree. Importing a module that isn't compiled yet compiles it there and then, so modules are finished in an order where each comes after the modules it imports; that is the order in which their bodies run. A module may come in one file per target (Name.linux-x86_64.raz and so on). The file for this target is compiled; the others beside it are read for their declarations only, and must export the same interface. Depends on: Scan, Sym, Chunk for data, Item and Gen for code. Leaves out: linking; a module only records the libraries it names, and whether it uses C or says link static. The seed's errors ended in a jump back to the statement or declaration being read (Scan.mark, longjmp). Here every routine that can find an error returns an ERROR as its last result, and the ones that call it pass it up with try, as Item does; the statement or declaration that is a recovery point (guard) looks at what comes back and reads on. What has been made by then is what the seed had made when it jumped. A seed `var` that only gave a value back is a result where that is shorter; the others stay as they were. A set of Scan.Sym is a chain of tests, since it has more names than a set holds. *) module Parse; import Fmt, Diag, Scan, Sym, Chunk, Gen, Item, Sys, Str, Buf, Heap, Out, Rt; import SYSTEM; type PType = ^Sym.Type; PObj = ^Sym.Obj; PModule = ^Sym.Module; TItem = Item.Item; (* The innermost loop: the chain of its breaks, the chain of its continues, and the deferred statements in force outside it *) Loop = record brk: INT; cont: INT; base: INT; end; PLoop = ^Loop; (* A generic module's import whose instance isn't made yet (deferred, numDeferred); not to be confused with a defer statement (defers) *) Deferred = record o: PObj; (* the name it imports as *) home: PModule; (* the module that imports it *) mname: INT; (* the generic module *) args: Scan.State; (* at the "(" of its arguments *) at: Diag.Pos; (* the import *) end; (* A type a type section names before it declares it (report, 4): a pointer's base, or a procedure type's parameter or result. A placeholder stands for it in its owner until the section's end, when resolveLaters puts the type declared in its place. *) Later = record name: INT; (* the name of the type *) at: Diag.Pos; (* where it is named *) held: PType; (* the placeholder, a type of form fNone *) owner: PType; (* the pointer or procedure type that holds it *) end; const (* Whether the target has reals (report, appendix A): x86-64 always has SSE2. Without them, used code that computes with one is an error, which Raz reports once it knows what the program uses. *) HasReals* = true; MaxNames = 64; (* names in one declaration, and the like *) MaxLater = 256; (* types named before they are declared *) MaxDefers = 64; (* defer statements in force *) MaxDoubts = 4096; (* places to warn of, noted while a module is read *) MaxDeferred = 64; (* generic imports not yet made *) MaxLabels = 1024; (* labels of one match *) MaxElements = 1024; (* elements of an array literal *) MaxFixes = 4096; (* addresses in one constant *) MaxPending = 64; (* modules being compiled at once *) MaxFors = 256; (* for loops of a procedure whose limit may go in a register *) MaxNest = 256; (* statements, expressions and types nested; each level is a few recursive calls of the parser *) var libDir*, mainDir: []const U8; incDirs*: [16][]const U8; numInc*: INT; userDirs: [][]const U8; (* RAZ_PATH, or HOME/.raz/lib; set by the driver *) thisVersion: []const U8; (* the compiler's version; set by the driver *) targetOs, targetArch: []const U8; (* the target's names, Exe.Os and Gen.Arch; set by the driver *) originNote: []const U8; (* for the module about to be opened, see libraryNote *) firstModule*, lastModule*: PModule; (* in the order they were finished *) checkMode: BOOL; (* reading a file only for its interface *) define: BOOL; (* raz -q def: the cursor asks where its name is declared *) inTypes: BOOL; (* in a type section: ^T, and a procedure type, may name a later type *) laters: [MaxLater]Later; numLater: INT; lastCalled: PObj; (* the procedure the last call was of, if it had a name *) passOn: BOOL; (* the next factor may be a call with several results, for return *) curProc: PObj; (* the procedure being compiled, or nil *) declExported: BOOL; (* the export mark of the declaration being read *) curLoop: PLoop; (* the innermost loop, or nil *) defers: [MaxDefers]INT; (* the defer statements in force, their code's addresses *) nest: INT; (* statements, expressions and types being read, one inside the other *) numDefers: INT; deferring: BOOL; (* compiling a defer statement's statement *) directDefer: BOOL; (* ... as the statement itself, not in a list *) deferSlot: I64; (* keeps a result while deferred statements run *) curResult: PType; (* the result type of the procedure being compiled *) inExtern: BOOL; (* reading an extern's signature: ... allowed *) tArgv, tBodySig: PType; nameRt, nameFail, nameBody, nameStatic, nameCheck, nameOff, nameAdr, nameCas, nameXchg, nameXadd: INT; declaring: [MaxNames]INT; (* the names the declaration being read declares *) numDeclaring: INT; skipped: BOOL; (* the last statement had an error and was skipped *) deferred: [MaxDeferred]Deferred; (* generic imports not yet made, not defer statements *) numDeferred: INT; making: BOOL; (* reading an instance's arguments, at its first use *) stray: BOOL; (* an "end" in this procedure stood left of its statement's line *) strayAt: Diag.Pos; (* ... the first such statement *) elseIf: BOOL; (* an "else if" in this procedure *) doubts: [MaxDoubts]record at: Diag.Pos; (* a place the module's end warns of *) n: INT; (* a call standing on its own that drops this many results, or *) msg, hint: []const U8; (* any other doubt, in words (msg is empty for a drop) *) end; numDoubts: INT; (* the module being compiled has those from its first one, saved by compileFile, to here *) bareBoolRel: BOOL; (* the relation just read is an = or # of two BOOLs, not in parentheses *) ends: BOOL; (* the statements just read never go on past their end: each way through them returns *) useAt: Diag.Pos; (* ... which is here *) guards: INT; (* the recovery points open *) (* A variable register no variable took, for the limits of the procedure's innermost for loops that aren't constant: compared each round, they would be read from the frame. limitK is its place in VarRegs, or -1; limitIn says which for loops, by their order in the procedure (chooseRegs); numFors counts them as compiled. *) limitK, numFors: INT; limitIn: [MaxFors]BOOL; (* For a body that is only return e (inlined): where the body's first return statement ends, its ";" with it, or -1; whether a try was read; and the calls made, which an inlined one doesn't count *) returnEnd: INT; sawTry: BOOL; numCalls: INT; proc expression(var x: TItem; want: PType): ERROR; forward; proc parseType: (PType, ERROR); forward; proc typeBody: (PType, ERROR); forward; proc statements: ERROR; forward; proc statement: ERROR; forward; (* Little helpers *) proc setLibDir*(dir: []const U8); begin libDir := dir; end; proc addIncDir*(dir: []const U8); begin incDirs[numInc] := dir; INC(numInc); end; (* The driver's user library directories, kept until compilation ends *) proc setUserDirs*(dirs: [][]const U8); begin userDirs := dirs; end; (* The target's operating system and machine, as the suffixes of a module's files say them: Exe.Os and Gen.Arch, which the driver gives *) proc setTarget*(os, arch: []const U8); begin targetOs := os; targetArch := arch; end; (* The driver's version of the compiler, which a library's note names *) proc setVersion*(v: []const U8); begin thisVersion := v; end; (* raz -q def, not words: the cursor asks where its name is declared *) proc setDefine*(yes: BOOL); begin define := yes; end; proc nameOf(n: INT): []const U8; begin return Scan.nameOf(n); end; (* The name number of s, which isn't empty *) proc internName(s: []const U8): INT; begin return Scan.intern(SYSTEM.PTR(s), LEN(s)); end; proc say1(f, a: []const U8): []const U8; begin return Diag.say(f, [Fmt.str(a)]); end; proc say2(f, a, b: []const U8): []const U8; begin return Diag.say(f, [Fmt.str(a), Fmt.str(b)]); end; proc say3(f, a, b, c: []const U8): []const U8; begin return Diag.say(f, [Fmt.str(a), Fmt.str(b), Fmt.str(c)]); end; (* More than n of what, as a message *) proc tooMany(n: INT; what: []const U8): []const U8; begin return Diag.say("more than {} {}", [Fmt.int(n), Fmt.str(what)]); end; (* The error for a statement or an expression nested too deep: the parser calls itself for each level, so its depth is bounded to keep the stack well inside 8 MiB *) proc tooDeep: ERROR; begin return Scan.mark(tooMany(MaxNest, "statements, expressions and types nested")); end; (* After an error at p: if a comment closed just before it, it most likely quoted the characters that end one, and ended early; where it ended is shown. *) proc commentNote(p: Diag.Pos); var closed: Diag.Pos; ok: BOOL; begin if p.f # Scan.tokPos().f then return; end; closed, ok := Scan.commentClosed(p.a); if ok then Diag.note(closed, "a comment ended here"); Diag.help("a comment that quotes *) writes it apart: * )"); end; end; (* After an error at at about the symbol found: that symbol, if at is above it with a comment between, as a missing symbol's place may be (with only blanks between, the two are seen together); and the comment, if it ended just before the symbol. *) proc foundNotes(at: Diag.Pos); begin if (at.a # Scan.tokPos().a) and Scan.commentBetween() then Diag.note(Scan.tokPos(), say1("found here: {}", Scan.found())); end; commentNote(Scan.tokPos()); end; (* A missing symbol is reported just after the one before it. *) proc expected(what: []const U8): ERROR; var at: Diag.Pos; begin if Scan.tok = Scan.sBlank then return Scan.mark("_ isn't a name: it is the blank, which stands only left of :="); end; at := Scan.missingPos(); Diag.error(at, say2("expected {}, found {}", what, Scan.found())); foundNotes(at); return Scan.Failed; end; proc expect(s: Scan.Sym): ERROR; begin if (Scan.tok = Scan.sLbrace) and ((s = Scan.sThen) or (s = Scan.sDo)) then return Scan.markHelp(Scan.missingPos(), say2("expected {}, found {}", Scan.symText(s), Scan.found()), say1("a block is written {} ... end", Scan.symText(s))); end; if Scan.tok # s then return expected(Scan.symText(s)); end; Scan.next; return nil; end; (* The end of a block statement at start, whose line is indented to ind. An "end" left of that was most likely meant for a block around the statement, which leaves the statement's own "end" missing: the first such is kept for endMissing. *) proc closeEnd(start: Diag.Pos; ind: INT): ERROR; begin if (Scan.tok = Scan.sEnd) and not stray and (Scan.indent() < ind) then stray := true; strayAt := start; end; return expect(Scan.sEnd); end; (* The error of a procedure or module whose own "end" isn't where it should be, with what closeEnd saw *) proc endMissing(p: Diag.Pos; msg: []const U8): ERROR; begin Diag.error(p, msg); foundNotes(p); if stray then Diag.note(strayAt, "is this statement's \"end\" missing? one further left closes it"); end; if elseIf then Diag.help("raz writes elsif: \"else if\" begins an if that needs its own \"end\""); end; return Scan.Failed; end; (* A name being declared, and whether it is exported; at is the name *) proc identDef(var n: INT; var exp: BOOL; var at: Diag.Pos): ERROR; begin if Scan.tok # Scan.sIdent then return expected("a name"); end; n := Scan.id; at := Scan.tokPos(); Scan.next; exp := false; if Scan.tok = Scan.sTimes then exp := true; Scan.next; end; return nil; end; (* Declares o in the innermost scope. Its module keeps its declarations in order: its own, for the interface check, and its procedures', to warn of those never used. *) proc declare(o: PObj): ERROR; var m: PModule; begin m := Sym.curModule; try Sym.insert(o, Sym.topScope()); if Sym.topScope() = m^.scope then if m^.last = nil then m^.first := o; else m^.last^.chain := o; end; m^.last := o; else if m^.lastLocal = nil then m^.locals := o; else m^.lastLocal^.chain := o; end; m^.lastLocal := o; end; return nil; end; (* o is named: a procedure naming itself doesn't count *) proc named(o: PObj); begin if o # curProc then o^.used := true; o^.read := true; end; end; (* Recovery After an error the parser goes on with the next statement or declaration, so that one run reports every error. Each statement and declaration is a recovery point: it notes the state of the scanner and the parser where it starts, and when what it read comes back with an error, the state is put back, the scanner reads the construct again from its start, skipping to its end, and the parser carries on after it. Nothing is generated after an error, so the code half made for the construct does no harm. A name that isn't declared is declared where it is used, with type tPoison, and what follows from it is not reported again; so are the names of a declaration that had an error. Skipping counts the words that open and close blocks. A symbol at the start of a line is taken to start a declaration of the module, as layout has it: a statement skipped up to one gives up its procedure, and the procedure is skipped up to its end. *) type Recover = record scan: Scan.State; scopes, depth, pushed, numDefers, nest: INT; guards: INT; loop: PLoop; result: PType; deferring, directDefer, quiet: BOOL; end; proc guard(var r: Recover); begin Scan.saveScan(r.scan); r.guards := guards; r.scopes := Sym.scopeDepth(); r.depth := Gen.depth; r.pushed := Gen.pushed; r.numDefers := numDefers; r.nest := nest; r.loop := curLoop; r.result := curResult; r.deferring := deferring; r.directDefer := directDefer; r.quiet := Diag.quiet; INC(guards); Diag.setQuiet(false); end; proc unguard(var r: Recover); begin guards := r.guards; Diag.setQuiet(r.quiet); Scan.setSkipping(false); end; (* After an error: the state as it was at the start of r's construct, whose first symbol is again the current one *) proc restore(var r: Recover); begin Scan.restoreScan(r.scan); Scan.setSkipping(true); Sym.cutScopes(r.scopes); Gen.resetStack(r.depth, r.pushed); numDefers := r.numDefers; nest := r.nest; curLoop := r.loop; curResult := r.result; deferring := r.deferring; directDefer := r.directDefer; inTypes := false; declExported := false; inExtern := false; making := false; end; (* Gives up the construct r is for, and the one around it: the error goes on up to the next recovery point, or ends the compile if there is none *) proc giveUp(var r: Recover): ERROR; begin unguard(r); if guards = 0 then Diag.finish(); end; return Scan.Failed; end; (* Reads to the end of a statement or declaration, from its first symbol: up to the ";" that ends it, not read, or to the "end", "else", "elsif", "until" or "|" of the statement around it (an arm's "|" comes after a ";", the operator's after an operand). False if it stopped at a symbol that starts a line, or the end of the file. *) proc skipConstruct: BOOL; var d, p: INT; prev: Scan.Sym; begin d := 0; p := 0; prev := Scan.sSemicolon; repeat match Scan.tok of | sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: INC(d); | sEnd, sUntil: if d = 0 then return true; end; DEC(d); | sElse, sElsif: if (d = 0) and (p = 0) then return true; end; | sBar: if (d = 0) and (p = 0) and (prev = Scan.sSemicolon) then return true; end; | sLparen, sLbrak, sLbrace: INC(p); | sRparen, sRbrak, sRbrace: if p > 0 then DEC(p); end; | sSemicolon: if (d = 0) and (p = 0) then return true; end; | sEof: return false; | else: end; prev := Scan.tok; Scan.next; until Scan.atLineStart(); return false; end; (* Declares n where it is used, after it was reported as not declared, or after the declaration of it had an error *) proc poison(n: INT); var o: PObj; begin o := Sym.newObj(Sym.oConst, n, Sym.tPoison); _ := Sym.insert(o, Sym.topScope()); (* n isn't in this scope, so it can't be twice *) end; proc willDeclare(n: INT); begin if numDeclaring < MaxNames then declaring[numDeclaring] := n; end; INC(numDeclaring); end; (* After a declaration had an error: its names are poison, and reading goes on after it *) proc skipDeclaration; var i: INT; begin for i := 0 to numDeclaring - 1 do if (i < MaxNames) and (Sym.find(Sym.topScope(), declaring[i]) = nil) then poison(declaring[i]); end; end; if skipConstruct() and (Scan.tok = Scan.sSemicolon) then Scan.next; end; end; proc constValue(var x: TItem): (I64, ERROR); begin if x.mode # Item.mConst then return 0, Scan.markAt(x.pos, "expected a constant"); end; return x.val, nil; end; proc constExpr: (I64, ERROR); var x: TItem; begin try expression(x, nil); if not Sym.isInt(x.typ) then return 0, Scan.markAt(x.pos, say1("expected an integer constant, found {}", Item.what(x))); end; return constValue(x); end; (* Help after a name that isn't found: o, the closest name in reach *) proc didYouMean(o: PObj); begin if o # nil then Diag.help(say1("did you mean {}?", nameOf(o^.name))); end; end; (* Where module n is found, if it is: dir and the file in it *) proc moduleFile(n: INT): ([]const U8, []const U8, BOOL); forward; (* name in lower case, in memory that is never freed, as messages are *) proc lowerCase(s: []const U8): []const U8; var p: ^U8; i: INT; begin if LEN(s) = 0 then return ""; end; p := Scan.grab(LEN(s)); for i := 0 to LEN(s) - 1 do if (s[i] >= 'A') and (s[i] <= 'Z') then p[i] := s[i] + 32; else p[i] := s[i]; end; end; return p[0..LEN(s)]; end; (* Whether s is a hexadecimal number but for the digit it must start with, as FFFDH is: capitals A to F and digits, then H *) proc hexLike(s: []const U8): BOOL; var i: INT; begin if (LEN(s) < 2) or (s[LEN(s) - 1] # 'H') then return false; end; for i := 0 to LEN(s) - 2 do if (Str.hexVal(s[i]) < 0) or (s[i] >= 'a') then return false; end; end; return true; end; (* Reports name n at p as not declared, with what it may have meant *) proc notDeclared(p: Diag.Pos; n: INT); var found: BOOL; low: []const U8; begin Diag.error(p, say1("{} isn't declared", nameOf(n))); commentNote(p); _, _, found := moduleFile(n); low := lowerCase(nameOf(n)); if found then Diag.help(say2("{} is a module: import {}", nameOf(n), nameOf(n))); elsif (nameOf(n) = "elif") or (nameOf(n) = "elseif") then Diag.help("raz writes elsif"); elsif Scan.reserved(internName(low)) then Diag.help(say1("a reserved word is written in lower case: {}", low)); elsif hexLike(nameOf(n)) then Diag.help(say1("a hexadecimal number starts with a digit: 0{}", nameOf(n))); else didYouMean(Sym.closest(n, -1)); end; end; proc firstUse(o: PObj; at: Diag.Pos): ERROR; forward; proc instancesFor(wholeRest: BOOL): ERROR; forward; proc addImport(m, im: PModule); forward; (* The name of the enumeration t that the current symbol is, after "t.": Elem.bold (report section 5.7). *) proc enumName(t: PType): (PObj, ERROR); var o: PObj; begin if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; o := t^.fields; while (o # nil) and (o^.name # Scan.id) do o := o^.next; end; if o = nil then return nil, Scan.mark(say2("{} has no name {}", Sym.typeName(t), nameOf(Scan.id))); end; Scan.next; return o, nil; end; (* An error at at: o, a name of an enumeration, is a name of another of its scope too, so it is written with its type. *) proc twinError(o: PObj; at: Diag.Pos): ERROR; var a, b: []const U8; begin a := Sym.typeName(o^.typ); b := Sym.typeName(o^.twin^.typ); return Scan.markHelp(at, Diag.say("{} is a name of {} and of {}", [Fmt.str(nameOf(o^.name)), Fmt.str(a), Fmt.str(b)]), Diag.say("write it with its type: {}.{} or {}.{}", [Fmt.str(a), Fmt.str(nameOf(o^.name)), Fmt.str(b), Fmt.str(nameOf(o^.name))])); end; (* raz -q words or def: the current symbol is the cursor, where a name of scope s is written, or of any scope open if s < 0. words writes the names that start with what is typed there; def where the name written there is declared, its place and what it is: a module's place is its name in its own file, and the language's own names have none. Then the compile ends. *) proc answer(s: INT); var b: Buf.Buf; o: PObj; begin Buf.init(b, Heap.heap); if not define then Sym.offer(s, Scan.prefix, b); else if s < 0 then o := Sym.lookup(internName(Scan.word)); else o := Sym.find(s, internName(Scan.word)); end; if (o # nil) and (o^.typ # Sym.tPoison) then if (o^.cls = Sym.oModule) and (o^.home # nil) and (o^.home^.path # "") then Diag.writePlace(b, o^.home^.at); elsif (o^.scope = 0) or (o^.cls = Sym.oBuiltin) then Diag.writePlace(b, {f: MAX(INT), a: 0, b: 0}); else Diag.writePlace(b, o^.pos); end; Buf.add(b, '\t'); Sym.describe(o, b); Buf.add(b, '\n'); end; end; Out.str(Buf.bytes(b)); Out.flush(); Rt.exit(0); end; proc qualIdent: (PObj, ERROR); var o: PObj; m: PModule; at: Diag.Pos; begin if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; if Scan.id = Scan.atCursor then answer(-1); end; at := Scan.tokPos(); o := Sym.lookup(Scan.id); if o = nil then if not making then poison(Scan.id); end; (* it may yet be declared *) Diag.setQuiet(false); notDeclared(at, Scan.id); if making then Diag.note(useAt, "the instance is made here, where it is first used"); end; return nil, Scan.Failed; end; if o^.typ = Sym.tPoison then Diag.setQuiet(true); end; named(o); Scan.next; if o^.cls = Sym.oModule then if o^.home = nil then try firstUse(o, at); end; try expect(Scan.sPeriod); if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; m := o^.home; if Scan.id = Scan.atCursor then answer(m^.scope); end; o := Sym.find(m^.scope, Scan.id); if (o = nil) or not o^.exported then Diag.error(Scan.tokPos(), say2("{} isn't exported by {}", nameOf(Scan.id), nameOf(m^.name))); if o # nil then Diag.note(o^.pos, say1("{} is declared here, without a *", nameOf(Scan.id))); else didYouMean(Sym.closest(Scan.id, m^.scope)); end; return nil, Scan.Failed; end; Scan.next; end; if (o^.cls = Sym.oType) and (o^.typ^.form = Sym.fEnum) and (Scan.tok = Scan.sPeriod) then Scan.next; o := try enumName(o^.typ); elsif o^.twin # nil then return nil, twinError(o, at); end; return o, nil; end; (* The field of record type t named by the current symbol *) proc fieldOf(t: PType): (PObj, ERROR); var f: PObj; begin f := Sym.find(t^.scope, Scan.id); if f = nil then Diag.error(Scan.tokPos(), say2("{} has no field {}", Sym.typeName(t), nameOf(Scan.id))); didYouMean(Sym.closest(Scan.id, t^.scope)); return nil, Scan.Failed; end; if not f^.exported and (t^.home # Sym.curModule) then Diag.error(Scan.tokPos(), say2("field {} of {} isn't exported", nameOf(Scan.id), Sym.typeName(t))); Diag.note(f^.pos, say1("{} is declared here, without a *", nameOf(Scan.id))); return nil, Scan.Failed; end; return f, nil; end; proc newPtr(base: PType; vol: BOOL): PType; var t: PType; begin t := Sym.newType(Sym.fPtr, 8, 8); t^.base := base; t^.vol := vol; return t; end; (* Types *) (* Lays the results t holds out as C would, each field at its offset; again by resolveLaters, if one names a type declared later. *) proc layOut(t: PType); var f: PObj; off: I64; begin off := 0; t^.align := 1; f := t^.fields; while f # nil do off := Sym.alignUp(off, f^.typ^.align); f^.val := off; off := off + f^.typ^.size; if f^.typ^.align > t^.align then t^.align := f^.typ^.align; end; f := f^.next; end; t^.size := Sym.alignUp(off, t^.align); end; (* A type that owner, a pointer or a procedure type, holds. In a type section it may name a type not declared yet (report, 4): a placeholder of its own stands for it, noted in laters, and resolveLaters puts the type in its place at the section's end. A pointer is a word and a procedure value an address, so neither's size waits for it. *) proc laterType(owner: PType): (PType, ERROR); var held: PType; begin if not inTypes or (Scan.tok # Scan.sIdent) or (Sym.lookup(Scan.id) # nil) then return parseType(); end; if numLater >= MaxLater then return nil, Scan.mark(tooMany(MaxLater, "types named before they are declared")); end; held := Sym.newType(Sym.fNone, 0, 1); laters[numLater].name := Scan.id; laters[numLater].at := Scan.tokPos(); laters[numLater].held := held; laters[numLater].owner := owner; INC(numLater); Scan.next; return held, nil; end; (* Gives t to each of the list f whose type is the placeholder held *) proc putFor(f: PObj; held, t: PType); begin while f # nil do if f^.typ = held then f^.typ := t; end; f := f^.next; end; end; (* Puts in place of each placeholder of laters the type it names, now that the type section is read: in a pointer's base, a procedure type's parameters and result, and its results, laid out again as their sizes are now known. A name that isn't a type is poison. *) proc resolveLaters; var i: INT; o: PObj; held, t, owner: PType; begin for i := 0 to numLater - 1 do held := laters[i].held; owner := laters[i].owner; o := Sym.lookup(laters[i].name); if (o # nil) and (o^.typ = Sym.tPoison) then t := Sym.tPoison; (* its declaration had an error, told already *) elsif (o = nil) or (o^.cls # Sym.oType) then notDeclared(laters[i].at, laters[i].name); t := Sym.tPoison; else t := o^.typ; named(o); end; if owner^.base = held then owner^.base := t; end; if owner^.form = Sym.fProc then putFor(owner^.fields, held, t); if (owner^.base # nil) and owner^.base^.tuple then putFor(owner^.base^.fields, held, t); layOut(owner^.base); end; end; end; numLater := 0; end; proc recordType: (PType, ERROR); var t, ft: PType; f, last: PObj; names: [MaxNames]INT; exps: [MaxNames]BOOL; poss: [MaxNames]Diag.Pos; n, i: INT; off, a, maxA: I64; begin t := Sym.newType(Sym.fRecord, 0, 1); if Scan.tok = Scan.sPacked then t^.packd := true; Scan.next; end; try expect(Scan.sRecord); t^.scope := Sym.newScope(); off := 0; maxA := 1; last := nil; while Scan.tok = Scan.sIdent do n := 0; repeat if n >= MaxNames then return nil, Scan.mark(tooMany(MaxNames, "names in one declaration")); end; try identDef(names[n], exps[n], poss[n]); INC(n); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; try expect(Scan.sColon); ft := try parseType(); if t^.packd then a := 1; else a := ft^.align; end; for i := 0 to n - 1 do off := Sym.alignUp(off, a); f := Sym.newObj(Sym.oField, names[i], ft); f^.exported := exps[i]; f^.pos := poss[i]; f^.val := off; try Sym.insert(f, t^.scope); if last = nil then t^.fields := f; else last^.next := f; end; last := f; off := off + ft^.size; end; if a > maxA then maxA := a; end; try expect(Scan.sSemicolon); end; try expect(Scan.sEnd); t^.align := maxA; t^.size := Sym.alignUp(off, maxA); return t, nil; end; proc variantType: (PType, ERROR); var t, ft: PType; c, lastC, f, lastF: PObj; tag, off, maxSize: I64; begin try expect(Scan.sVariant); t := Sym.newType(Sym.fVariant, 8, 8); tag := 0; maxSize := 8; lastC := nil; while Scan.tok = Scan.sIdent do c := Sym.newObj(Sym.oCase, Scan.id, t); c^.val := tag; c^.exported := declExported; try declare(c); if lastC = nil then t^.fields := c; else lastC^.next := c; end; lastC := c; Scan.next; off := 8; if Scan.tok = Scan.sLparen then Scan.next; lastF := nil; repeat repeat if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; f := Sym.newObj(Sym.oField, Scan.id, nil); f^.exported := true; if lastF = nil then c^.fields := f; else lastF^.next := f; end; lastF := f; Scan.next; if Scan.tok # Scan.sComma then break; end; Scan.next; until false; try expect(Scan.sColon); ft := try parseType(); f := c^.fields; while f^.typ # nil do f := f^.next; end; while f # nil do f^.typ := ft; off := Sym.alignUp(off, ft^.align); f^.val := off; off := off + ft^.size; f := f^.next; end; if ft^.align > t^.align then t^.align := ft^.align; end; if Scan.tok # Scan.sSemicolon then break; end; Scan.next; until false; try expect(Scan.sRparen); end; if off > maxSize then maxSize := off; end; try expect(Scan.sSemicolon); INC(tag); end; try expect(Scan.sEnd); t^.len := tag; t^.size := Sym.alignUp(maxSize, t^.align); return t, nil; end; (* The results of the procedure type sig: a record of them, laid out as C would, and returned in rax and rdx if they are two words (inTwoRegs), or else through a hidden pointer. One alone in parentheses is the result itself. *) proc results(sig: PType): (PType, ERROR); var t: PType; f, last: PObj; begin Scan.next; t := Sym.newType(Sym.fRecord, 0, 1); t^.tuple := true; last := nil; repeat f := Sym.newObj(Sym.oField, -1, try laterType(sig)); if last = nil then t^.fields := f; else last^.next := f; end; last := f; INC(t^.len); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; try expect(Scan.sRparen); if t^.len = 1 then return t^.fields^.typ, nil; end; layOut(t); return t, nil; end; proc signature: (PType, ERROR); var t, pt: PType; p, first, last: PObj; isVar, isConst: BOOL; n: INT; begin t := Sym.newType(Sym.fProc, 8, 8); last := nil; n := 0; if Scan.tok = Scan.sLparen then Scan.next; if Scan.tok # Scan.sRparen then repeat if Scan.tok = Scan.sEllipsis then if not inExtern then return nil, Scan.mark("... is only for extern procedures"); end; Scan.next; t^.variadic := true; break; end; isVar := Scan.tok = Scan.sVar; isConst := Scan.tok = Scan.sConst; if isVar or isConst then Scan.next; end; first := nil; repeat if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; p := Sym.newObj(Sym.oVar, Scan.id, nil); (* a const parameter goes by address, as a var one *) p^.isVar := isVar or isConst; p^.isConst := isConst; p^.lev := 1; if last = nil then t^.fields := p; else last^.next := p; end; last := p; if first = nil then first := p; end; INC(n); Scan.next; if Scan.tok # Scan.sComma then break; end; Scan.next; until false; try expect(Scan.sColon); pt := try laterType(t); while first # nil do first^.typ := pt; first := first^.next; end; if Scan.tok # Scan.sSemicolon then break; end; Scan.next; until false; end; try expect(Scan.sRparen); end; t^.len := n; if Scan.tok = Scan.sColon then Scan.next; if Scan.tok = Scan.sLparen then t^.base := try results(t); else t^.base := try laterType(t); end; end; return t, nil; end; proc enumType: (PType, ERROR); var t: PType; o, last: PObj; start: Diag.Pos; begin start := Scan.tokPos(); t := Sym.newType(Sym.fEnum, 1, 1); last := nil; repeat Scan.next; if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; o := Sym.newObj(Sym.oConst, Scan.id, t); o^.val := t^.len; o^.ofEnum := true; o^.exported := declExported; o^.used := true; (* an enumeration's names are its type's, warned of with it *) try declare(o); if last = nil then t^.fields := o; else last^.next := o; end; last := o; INC(t^.len); Scan.next; until Scan.tok # Scan.sComma; try expect(Scan.sRparen); if t^.len > 256 then return nil, Scan.markAt(Scan.spanFrom(start), "an enumeration has at most 256 names"); end; return t, nil; end; proc setOf(e: PType; at: Diag.Pos): (PType, ERROR); var t: PType; begin if (e^.form # Sym.fEnum) or (e^.len > 64) then return nil, Scan.markAt(at, say1("a set needs an enumeration of at most 64 names, found {}", Sym.typeName(e))); end; t := Sym.newType(Sym.fSet, 8, 8); if e^.len <= 8 then t^.size := 1; elsif e^.len <= 16 then t^.size := 2; elsif e^.len <= 32 then t^.size := 4; end; t^.align := t^.size; t^.base := e; t^.len := e^.len; return t, nil; end; (* A type, read as typeBody does, counted as one more level of nesting, so that a type of types of types ... can't overflow the stack *) proc parseType: (PType, ERROR); var t: PType; err: ERROR; begin INC(nest); if nest > MaxNest then return nil, tooDeep(); end; t, err := typeBody(); DEC(nest); return t, err; end; proc typeBody: (PType, ERROR); var t: PType; o: PObj; n: I64; vol: BOOL; start: Diag.Pos; begin start := Scan.tokPos(); match Scan.tok of | sIdent: o := try qualIdent(); if o^.cls # Sym.oType then return nil, Scan.markAt(Scan.spanFrom(start), say1("{} isn't a type", nameOf(o^.name))); end; t := o^.typ; | sLbrak: Scan.next; if Scan.tok = Scan.sRbrak then Scan.next; t := Sym.newType(Sym.fSlice, 16, 8); if Scan.tok = Scan.sConst then Scan.next; t^.rdonly := true; end; (* []const T *) t^.base := try parseType(); else start := Scan.tokPos(); n := try constExpr(); if (n <= 0) or (n > (I64(1) shl 40)) then return nil, Scan.markAt(Scan.spanFrom(start), Diag.say("an array's length is from 1 to 2^40, found {}", [Fmt.int(n)])); end; try expect(Scan.sRbrak); t := Sym.newType(Sym.fArray, 0, 1); t^.len := n; t^.base := try parseType(); t^.size := n * t^.base^.size; t^.align := t^.base^.align; end; | sArrow: Scan.next; vol := Scan.tok = Scan.sVolatile; if vol then Scan.next; end; t := newPtr(nil, vol); t^.base := try laterType(t); | sPacked, sRecord: t := try recordType(); | sVariant: t := try variantType(); | sProc: Scan.next; t := try signature(); | sLparen: t := try enumType(); | sSet: Scan.next; try expect(Scan.sOf); start := Scan.tokPos(); t := try setOf(try parseType(), start); | else: return nil, expected("a type"); end; return t, nil; end; (* Expressions *) (* Inlining a procedure that only returns A call of a procedure whose body is only return e, with no locals, is made in place: e is read again from where it begins, the scanner put back there, as a while loop reads its condition again. The arguments, read and converted as for a call and left on Gen's stack as the call would take them, go to temporaries, the last on top first, so each is evaluated once and in order; a scope names the parameters as them. e is read in the procedure's own module's scope, with its module's checks, so it means what it means there. For proc isLower(c: U8): BOOL; begin return U8(c - 97) < 26; end; ... if isLower(s[i]) then ... the code was, before, movzx edi, byte [s + i]; call isLower mov r8, rax; movzx r8d, r8b; test r8, r8; je else (isLower: push rbp ... cmp r8, 26; setb r8b; movzx r8d, r8b; mov rax, r8; leave; ret) and is now, after, movzx r8d, byte [s + i]; mov [rbp - t], r8b movzx r8d, byte [rbp - t]; sub r8, 97; movzx r8d, r8b cmp r8, 26; jae else A parameter by address (var, const, a record or an array) is a slot holding the address the call would have passed (ind), a slice its two words, any other its value. A failed check in e is told at e's own line, in its own file (Gen's check notes the place while e is read); the procedure isn't in the trap report's list, nor in a debugger's backtrace, as it has no frame. The procedure itself is still compiled, for a call through a procedure value or before its body is read. *) proc inlined(var c: Gen.CallInfo; var x: TItem): ERROR; var p: PObj; inl: ^Sym.Inline; q, o: PObj; params: [64]PObj; n, i: INT; t: I64; st: Scan.State; saved: [Sym.MaxDepth]INT; d, path, doubts0: INT; home: PModule; checks: BOOL; res: PType; err: ERROR; begin p := c.obj; inl := p^.ret; res := c.sig^.base; n := 0; q := c.sig^.fields; while q # nil do o := Sym.newObj(Sym.oVar, q^.name, q^.typ); o^.pos := q^.pos; o^.lev := 1; o^.param := true; o^.isVar := q^.isVar; o^.isConst := q^.isConst; params[n] := o; INC(n); q := q^.next; end; for i := n - 1 to 0 by -1 do o := params[i]; if o^.isVar or (Sym.isAggregate(o^.typ) and (o^.typ^.form # Sym.fSlice)) then t := Gen.allocTemp(8, 8); Gen.popVar(Item.frameAdr(t), 8); o^.ind := true; elsif o^.typ^.form = Sym.fSlice then t := Gen.allocTemp(16, 8); Gen.popVar(Item.frameAdr(t + 8), 8); (* the length, on top *) Gen.popVar(Item.frameAdr(t), 8); else t := Gen.allocTemp(o^.typ^.size, o^.typ^.align); Gen.popVar(Item.frameAdr(t), Item.scalarSize(o^.typ)); end; o^.val := t; end; (* e in the procedure's module, and back; what it notes for warnings was noted when the procedure was compiled *) Scan.saveScan(st); d := Sym.saveScopes(saved[0..Sym.MaxDepth]); home := Sym.curModule; path := Chunk.curFilePath(); checks := Item.checks; doubts0 := numDoubts; Sym.setCurModule(p^.home); Chunk.setCurFile(inl^.path); Item.setChecks(inl^.checks); err := Sym.openScope(p^.home^.scope); if err = nil then err := Sym.openScope(Sym.newScope()); end; for i := 0 to n - 1 do if err = nil then err := Sym.insert(params[i], Sym.topScope()); end; end; Scan.restoreScan(inl^.at); Scan.next; (* past the return *) inl^.busy := true; if err = nil then err := expression(x, res); end; (* a condition stays one, for if and while to jump on; anything else is loaded, as a call's result is, and widens as one does (made) *) if (err = nil) and (x.mode # Item.mCond) then err := Item.coerce(x, res); if err = nil then err := Item.value(x); end; x.made := false; end; inl^.busy := false; numDoubts := doubts0; Sym.setCurModule(home); Chunk.setCurFile(path); Item.setChecks(checks); Sym.restoreScopes(d, saved[0..Sym.MaxDepth]); Scan.restoreScan(st); return err; end; proc call(var x: TItem): ERROR; var c: Gen.CallInfo; a: TItem; want: PType; begin try Item.beginCall(c, x); try expect(Scan.sLparen); if Scan.tok # Scan.sRparen then repeat if c.param # nil then want := c.param^.typ; else want := nil; end; try expression(a, want); try Item.passArg(c, a); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; end; try expect(Scan.sRparen); if (c.param = nil) and (c.obj # nil) and (c.obj^.ret # nil) and not c.obj^.ret^.busy and (Gen.codeChunk >= 0) then return inlined(c, x); end; INC(numCalls); return Item.endCall(c, x); end; (* Whether an atomic built-in, which, works on a variable of type t: an INT or a UINT, or for CAS and XCHG a pointer *) proc atomicType(t: PType; which: INT): BOOL; begin return Item.isWord(t) or ((t^.form = Sym.fPtr) and (which # Sym.bXadd)); end; (* The name of the atomic built-in which, for a message *) proc atomicName(which: INT): []const U8; begin if which = Sym.bCas then return "CAS"; end; if which = Sym.bXchg then return "XCHG"; end; return "XADD"; end; (* CAS(v, old, new), XCHG(v, x) or XADD(v, x) (report 6.8): v is a variable, whose address goes on the stack, as ADR's would. Before report 2026-10.37 they took a pointer to it, ADR(v): the error says so. *) proc atomic(var x: TItem; which: INT): ERROR; var p, a: TItem; t: PType; i, n: INT; begin try expression(p, nil); if (p.mode # Item.mVar) or not p.isVar then if p.typ^.form = Sym.fPtr then return Scan.markHelp(p.pos, say1("{} takes a variable, not a pointer to it", atomicName(which)), "give it the variable itself: XADD(n, 1), or XADD(p^, 1) through a pointer"); end; return Scan.markAt(p.pos, say1("expected a variable, found {}", Item.what(p))); end; t := p.typ; if not atomicType(t, which) then if (t^.form = Sym.fPtr) and atomicType(t^.base, which) then return Scan.markHelp(p.pos, say1("{} works on an INT or a UINT variable, not a pointer", atomicName(which)), say1("for what the pointer points at, write {}^", Diag.source(p.pos))); elsif which = Sym.bXadd then return Scan.markAt(p.pos, say1("expected an INT or a UINT variable, found {}", Item.what(p))); end; return Scan.markAt(p.pos, say1("expected an INT, a UINT or a pointer variable, found {}", Item.what(p))); end; try Item.notReadOnly(p); if p.odd then return Scan.markAt(p.pos, "a field of a packed record has no address"); end; if p.view then return Item.noAddress(p, "an atomic can't take a view"); end; try Item.address(p); if which = Sym.bCas then n := 2; else n := 1; end; for i := 1 to n do try expect(Scan.sComma); try expression(a, t); try Item.coerce(a, t); try Item.value(a); end; Gen.atomic(which); Item.makeConst(x, t, 0); if which = Sym.bCas then x.typ := Sym.tBool; end; x.mode := Item.mStack; return nil; end; (* NEW(p, a): p := a^.alloc(a, SIZE(T), alignment), zeroed if not nil. a is a ^Mem.Allocator, the one interface the compiler knows. *) proc new_(var x: TItem): ERROR; var y, z: TItem; t: PType; f: PObj; s: I64; c: Gen.CallInfo; begin try expression(x, nil); try Item.notReadOnly(x); if (x.mode # Item.mVar) or not x.isVar or (x.typ^.form # Sym.fPtr) then return Scan.markAt(x.pos, "NEW needs a pointer variable"); end; t := x.typ^.base; try expect(Scan.sComma); try expression(y, nil); if (y.typ^.form # Sym.fPtr) or (y.typ^.base^.obj = nil) or (y.typ^.base^.obj^.home = nil) or (nameOf(y.typ^.base^.obj^.name) # "Allocator") or (nameOf(y.typ^.base^.obj^.home^.name) # "Mem") then return Scan.markAt(y.pos, say1("NEW needs a ^Mem.Allocator, found {}", Item.what(y))); end; f := Sym.find(y.typ^.base^.scope, internName("alloc")); s := try Item.toSlot(y); Gen.pushVar(Item.frameAdr(s), 8, false, Gen.kWord); Item.makeConst(z, f^.typ, 0); z.mode := Item.mVar; z.base := Gen.bStack; z.off := f^.val; try Item.beginCall(c, z); Gen.pushVar(Item.frameAdr(s), 8, false, Gen.kWord); Item.makeConst(z, y.typ, 0); z.mode := Item.mStack; try Item.passArg(c, z); Item.makeConst(z, Sym.tInt, t^.size); try Item.passArg(c, z); Item.makeConst(z, Sym.tInt, t^.align); try Item.passArg(c, z); try Item.endCall(c, z); Gen.zeroNew(t^.size); z.typ := x.typ; try Item.assign(x, z); Item.makeConst(x, Sym.tNone, 0); x.mode := Item.mNone; return nil; end; proc incOrDec(which: INT): []const U8; begin if which = Sym.bDec then return "DEC"; end; return "INC"; end; (* PTR(x): the item x, a slice, an array or a string, made the address its elements start at by Item's startOf. Anything else is an error that says what PTR is for, and for a pointer what to write. *) proc ptrOf(var x: TItem): ERROR; var e: PType; s: []const U8; begin if x.mode = Item.mStr then e := Sym.tU8; elsif x.typ^.form in {Sym.fSlice, Sym.fArray} then e := x.typ^.base; else s := say1("PTR needs a slice or an array, found {}", Item.what(x)); if (x.typ^.form = Sym.fPtr) and (x.typ^.base # nil) and (x.typ^.base^.form = Sym.fArray) then return Scan.markHelp(x.pos, s, Diag.say("write PTR({}^) for the address of the array's first element, a ^{}", [Fmt.str(Diag.source(x.pos)), Fmt.str(Sym.typeName(x.typ^.base^.base))])); elsif x.typ^.form = Sym.fPtr then return Scan.markHelp(x.pos, s, say1("{} is a pointer already, which is what PTR gives of a slice or an array", Diag.source(x.pos))); end; return Scan.markAt(x.pos, s); end; return Item.startOf(x, newPtr(e, false)); end; (* The field f of SIZE(T.f) or OFFSET(T, f), at its name, of the type t written at at. T is a record type: a variant's cases share their place, so have none of their own. f is one name, not a path a.b, which would be a field of f's type, asked of that type. *) proc typeField(t: PType; at: Diag.Pos; isOffset: BOOL): (PObj, ERROR); var f: PObj; what, s, hint, g: []const U8; dot: Diag.Pos; begin if isOffset then what := "OFFSET"; else what := "SIZE(T.f)"; end; if t^.form # Sym.fRecord then s := say2("{} needs a record type, found {}", what, Sym.typeName(t)); if t^.form = Sym.fVariant then return nil, Scan.markHelp(at, s, "a variant's cases share one place, so have no offset or size of their own"); end; return nil, Scan.markAt(at, s); end; if Scan.tok # Scan.sIdent then return nil, expected("a field"); end; f := try fieldOf(t); Scan.next; if Scan.tok = Scan.sPeriod then dot := Scan.tokPos(); Scan.next; if Scan.tok = Scan.sIdent then g := nameOf(Scan.id); else g := "g"; end; if isOffset then hint := Diag.say("add the offset within {}'s type: OFFSET({}, {}) + OFFSET({}, {})", [Fmt.str(nameOf(f^.name)), Fmt.str(Sym.typeName(t)), Fmt.str(nameOf(f^.name)), Fmt.str(Sym.typeName(f^.typ)), Fmt.str(g)]); else hint := Diag.say("ask {}'s type: SIZE({}.{})", [Fmt.str(nameOf(f^.name)), Fmt.str(Sym.typeName(f^.typ)), Fmt.str(g)]); end; return nil, Scan.markHelp(dot, say1("{} takes one field name, not a path", what), hint); end; return f, nil; end; proc builtin(var x: TItem): ERROR; var which, n, start: INT; y: TItem; t: PType; f: PObj; at: Diag.Pos; begin which := INT(x.obj^.val); if (x.obj^.home # Sym.system) and ((which = Sym.bAdr) or (which = Sym.bPtr) or (which = Sym.bSyscall) or (which = Sym.bArgv)) then return Scan.markHelp(x.pos, say2("{} is SYSTEM's, and written SYSTEM.{}", nameOf(x.obj^.name), nameOf(x.obj^.name)), "say import SYSTEM after the module's imports, and write the name after SYSTEM."); end; if (which = Sym.bFence) or (which = Sym.bArgc) or (which = Sym.bArgv) then if Scan.tok = Scan.sLparen then Scan.next; try expect(Scan.sRparen); end; Item.makeConst(x, Sym.tNone, 0); match which of | Sym.bFence: Gen.fence(); x.mode := Item.mNone; | Sym.bArgc: Gen.pushVar(Item.globalAdr(Gen.argcChunk), 8, false, Gen.kWord); x.typ := Sym.tInt; x.mode := Item.mStack; | else: Gen.pushVar(Item.globalAdr(Gen.argvChunk), 8, false, Gen.kWord); x.typ := tArgv; x.mode := Item.mStack; end; return nil; end; try expect(Scan.sLparen); match which of | Sym.bSize: (* SIZE(T) or SIZE(T.f): parseType reads M.T whole, since the name before a "." there must be a module's; so a "." past the type is always a field's *) start := Scan.tokStart(); t := try parseType(); if Scan.tok = Scan.sPeriod then at := Scan.spanFromAt(start); Scan.next; f := try typeField(t, at, false); Item.makeConst(x, Sym.tConst, f^.typ^.size); else Item.makeConst(x, Sym.tConst, t^.size); end; | Sym.bOffset: start := Scan.tokStart(); t := try parseType(); at := Scan.spanFromAt(start); try expect(Scan.sComma); f := try typeField(t, at, true); Item.makeConst(x, Sym.tConst, f^.val); (* a field's val is its offset *) | Sym.bAdr: try expression(y, nil); try Item.notReadOnly(y); if (y.mode # Item.mVar) or not y.isVar then return Scan.markAt(y.pos, "ADR needs a variable"); end; if y.odd then return Scan.markAt(y.pos, "a field of a packed record has no address"); end; if y.view then return Item.noAddress(y, "ADR can't take a view"); end; t := newPtr(y.typ, y.vol); try Item.address(y); Item.makeConst(x, t, 0); x.mode := Item.mStack; | Sym.bLen: try expression(y, nil); if y.mode = Item.mStr then Item.makeConst(x, Sym.tConst, y.val); elsif y.typ^.form = Sym.fArray then try Item.discard(y); Item.makeConst(x, Sym.tConst, y.typ^.len); elsif (y.typ^.form = Sym.fSlice) and (y.mode = Item.mVar) then (* its length, the word after its pointer *) y.off := y.off + 8; y.typ := Sym.tInt; try Item.value(y); x := y; else return Scan.markAt(y.pos, say1("LEN needs an array or a slice, found {}", Item.what(y))); end; | Sym.bPtr: if Scan.tok = Scan.sLbrak then (* a literal takes its type from where it stands, and here nothing gives one *) return Scan.markHelp(Scan.tokPos(), "PTR can't take an array literal, which has no type here", "PTR([]) would be nil, the empty slice's pointer; give any other literal a variable first"); end; try expression(y, nil); try ptrOf(y); x := y; | Sym.bBytes: try expression(y, nil); try Item.bytesOf(y); x := y; | Sym.bSyscall: n := 0; repeat if n > 0 then Scan.next; end; try expression(y, nil); if not (Sym.isScalar(y.typ) and (y.typ^.form # Sym.fReal)) then return Scan.markAt(y.pos, say1("expected an integer or a pointer, found {}", Item.what(y))); end; if y.typ^.form = Sym.fConst then try Item.coerce(y, Sym.tInt); end; try Item.value(y); INC(n); until (Scan.tok # Scan.sComma) or (n = 7); Gen.sysCall(n); Item.makeConst(x, Sym.tInt, 0); x.mode := Item.mStack; | Sym.bCas, Sym.bXchg, Sym.bXadd: try atomic(x, which); | Sym.bMsg: try expression(x, Sym.tError); try Item.msg(x); | Sym.bInc, Sym.bDec: try expression(x, nil); try Item.notReadOnly(x); if (x.mode # Item.mVar) or not x.isVar or (x.typ^.form # Sym.fInt) then return Scan.markAt(x.pos, say1("{} needs an integer variable", incOrDec(which))); end; if x.base # Gen.bReg then try Item.address(x); end; if Scan.tok = Scan.sComma then Scan.next; try expression(y, x.typ); else Item.makeConst(y, x.typ, 1); end; try Item.incDec(x, y, which = Sym.bDec); Item.makeConst(x, Sym.tNone, 0); x.mode := Item.mNone; | Sym.bNew: try new_(x); | Sym.bRol, Sym.bRor: try expression(x, nil); try Item.rotand(x, which = Sym.bRol); try expect(Scan.sComma); try expression(y, nil); try Item.rotate(x, y, which = Sym.bRol); | Sym.bMin, Sym.bMax: at := Scan.tokPos(); t := try parseType(); try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(at)); | Sym.bAbs: try expression(x, nil); try Item.absolute(x); | Sym.bClz, Sym.bCtz, Sym.bPopcount: try expression(x, nil); try Item.countBits(x, which); | Sym.bCopy: try expression(x, nil); try Item.copyDest(x); try expect(Scan.sComma); try expression(y, nil); try Item.copyFrom(x, y); Item.makeConst(x, Sym.tNone, 0); x.mode := Item.mNone; | Sym.bFill: try expression(x, nil); t := x.typ; try Item.fillDest(x); try expect(Scan.sComma); try expression(y, x.typ^.base); try Item.fillWith(x, y, t); Item.makeConst(x, Sym.tNone, 0); x.mode := Item.mNone; | else: end; return expect(Scan.sRparen); end; proc constructCase(var x: TItem; c: PObj; at: Diag.Pos): ERROR; var f: PObj; a: TItem; begin Item.construct(x, c); f := c^.fields; if Scan.tok = Scan.sLparen then Scan.next; while f # nil do try expression(a, f^.typ); try Item.setField(x, f, a); f := f^.next; if f # nil then try expect(Scan.sComma); end; end; return expect(Scan.sRparen); elsif f # nil then return Scan.markAt(at, say1("{} needs its fields", nameOf(c^.name))); end; return nil; end; (* x, before ".", has no fields; tok is what follows. Other languages write p.f through a pointer and s.len for a length. *) proc fieldOfWhat(x: TItem): ERROR; var msg, f: []const U8; begin msg := say1("expected a record, found {}", Item.what(x)); if Scan.tok = Scan.sIdent then f := nameOf(Scan.id); else f := ""; end; if (x.typ^.form = Sym.fPtr) and (x.typ^.base^.form = Sym.fRecord) then return Scan.markHelp(x.pos, msg, say2("through a pointer, write {}^.{}", Diag.source(x.pos), f)); elsif ((x.typ^.form = Sym.fArray) or (x.typ^.form = Sym.fSlice)) and ((f = "len") or (f = "length") or (f = "size") or (f = "count")) then return Scan.markHelp(x.pos, msg, say1("its length is LEN({})", Diag.source(x.pos))); end; return Scan.markAt(x.pos, msg); end; (* A designator, with any selectors and calls after it. called tells whether the last thing done was a call. *) (* Whether a view, ".(", is next: a "." and, past it, a "(". *) proc viewNext: BOOL; var st: Scan.State; yes: BOOL; begin if Scan.tok # Scan.sPeriod then return false; end; Scan.saveScan(st); Scan.next; yes := Scan.tok = Scan.sLparen; Scan.restoreScan(st); return yes; end; (* The "(" T ")" of a view, at the "(": T. *) proc viewType: (PType, ERROR); var t: PType; begin Scan.next; t := try parseType(); try expect(Scan.sRparen); return t, nil; end; proc designator(var x: TItem; var called: BOOL): ERROR; var o, f: PObj; y, z: TItem; t: PType; start: INT; begin start := Scan.tokStart(); o := try qualIdent(); called := false; if o^.cls = Sym.oModule then return Scan.markAt(Scan.spanFromAt(start), say1("{} is a module, not a value", nameOf(o^.name))); end; try Item.makeItem(x, o); if x.mode = Item.mBuiltin then x.pos := Scan.spanFromAt(start); (* its name, for a message *) try builtin(x); called := true; elsif x.mode = Item.mCase then try constructCase(x, o, Scan.spanFromAt(start)); end; repeat x.pos := Scan.spanFromAt(start); match Scan.tok of | sPeriod: Scan.next; if Scan.tok = Scan.sLparen then t := try viewType(); x.pos := Scan.spanFromAt(start); try Item.view(x, t); else if (x.mode # Item.mVar) or (x.typ^.form # Sym.fRecord) then return fieldOfWhat(x); end; if Scan.tok # Scan.sIdent then return expected("a field"); end; f := try fieldOf(x.typ); Scan.next; try Item.field(x, f); end; called := false; | sArrow: Scan.next; try Item.deref(x); called := false; | sLbrak: Scan.next; try Item.indexPrep(x); try expression(y, nil); if Scan.tok = Scan.sUpto then Scan.next; try Item.sliceLo(y); try expression(z, nil); try Item.slice(x, y, z); try expect(Scan.sRbrak); else (* a[i].(T), a view of an element, is checked against the end of a, not of the element: so the parser looks past the "]" for ".(" before the index is made *) try expect(Scan.sRbrak); if viewNext() then Scan.next; t := try viewType(); x.pos := Scan.spanFromAt(start); try Item.indexView(x, y, t); else try Item.index(x, y); end; end; called := false; | sLparen: if x.mode = Item.mType then t := x.typ; Scan.next; try expression(x, nil); try expect(Scan.sRparen); try Item.convert(x, t); else if x.mode = Item.mProc then lastCalled := x.obj; else lastCalled := nil; end; try call(x); called := true; end; | else: break; end; until false; x.pos := Scan.spanFromAt(start); Item.nilTest(x); return nil; end; proc arrayLiteral(var x: TItem; want: PType): ERROR; var et, at: PType; e: TItem; slots: [MaxElements]I64; n, i: INT; off, es: I64; start: Diag.Pos; begin start := Scan.tokPos(); if (want = nil) or not ((want^.form = Sym.fArray) or (want^.form = Sym.fSlice)) then return Scan.mark("an array literal needs a known array or slice type"); end; et := want^.base; es := et^.size; Scan.next; n := 0; if Scan.tok # Scan.sRbrak then repeat if n >= MaxElements then return Scan.mark(tooMany(MaxElements, "elements")); end; try expression(e, et); try Item.coerce(e, et); if Sym.isScalar(et) then slots[n] := try Item.toSlot(e); else if e.isVar or e.vol or (e.mode # Item.mVar) or (e.base # Gen.bFrame) then try Item.copyToTemp(e); end; slots[n] := e.off; end; INC(n); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; end; try expect(Scan.sRbrak); if (want^.form = Sym.fArray) and (I64(n) # want^.len) then return Scan.markAt(Scan.spanFrom(start), Diag.say("expected {} elements, found {}", [Fmt.int(want^.len), Fmt.int(n)])); end; if n = 0 then Gen.pushConst(Gen.kWord, 0); Item.sliceOf(x, want, 0); return nil; end; if want^.form = Sym.fArray then at := want; off := Gen.allocTemp(at^.size, at^.align); else at := Sym.newType(Sym.fArray, I64(n) * es, et^.align); at^.len := n; at^.base := et; off := Gen.allocPerm(at^.size, at^.align); (* lives as long as the call *) end; for i := 0 to n - 1 do if Sym.isScalar(et) then Gen.pushVar(Item.frameAdr(slots[i]), 8, false, Gen.kindOf(et)); Gen.popVar(Item.frameAdr(off + I64(i) * es), Item.scalarSize(et)); else Gen.pushAdr(Item.frameAdr(slots[i])); Gen.copyTo(Item.frameAdr(off + I64(i) * es), es); end; end; Item.tempItem(x, at, off); return nil; end; (* The padding of the record type t, the bytes between its fields and after the last, cleared in x: a word at a time, so a few stores *) proc clearPadding(const x: TItem; t: PType); var f: PObj; at, lo, hi: I64; begin at := 0; f := t^.fields; while true do if f # nil then hi := f^.val; else hi := t^.size; end; if hi > at then lo := at - at mod 8; hi := Sym.alignUp(hi, 8); if hi > t^.size then hi := t^.size; end; Item.zeroRange(x, lo, hi - lo); end; if f = nil then break; end; at := f^.val + f^.typ^.size; f := f^.next; end; end; (* The fields of the record type t that a literal left out, of which seen are the n names it gave, cleared in x *) proc clearMissing(const x: TItem; t: PType; const seen: [MaxNames]INT; n: INT); var f: PObj; i: INT; given: BOOL; begin f := t^.fields; while f # nil do given := false; for i := 0 to n - 1 do if seen[i] = f^.name then given := true; end; end; if not given then Item.zeroRange(x, f^.val, f^.typ^.size); end; f := f^.next; end; end; (* A record literal: a temporary, or the result's place, with the fields named filled in and the rest zero. The temporary is cleared where the fields won't write: the padding first, then what the literal left out. *) proc recordLiteral(var x: TItem; t: PType): ERROR; var f: PObj; a: TItem; seen: [MaxNames]INT; n, i, fields: INT; begin Scan.next; Item.tempOf(x, t); n := 0; fields := 0; f := t^.fields; while f # nil do INC(fields); f := f^.next; end; if fields > MaxNames then Item.zeroRange(x, 0, t^.size); (* more than seen has room for *) else clearPadding(x, t); end; if Scan.tok # Scan.sRbrace then repeat if Scan.tok # Scan.sIdent then return expected("a field"); end; f := try fieldOf(t); for i := 0 to n - 1 do if seen[i] = Scan.id then return Scan.mark(say1("field {} is given twice", nameOf(Scan.id))); end; end; if n < MaxNames then seen[n] := Scan.id; INC(n); end; Scan.next; try expect(Scan.sColon); try expression(a, f^.typ); try Item.setField(x, f, a); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; end; if fields <= MaxNames then clearMissing(x, t, seen, n); end; return expect(Scan.sRbrace); end; (* A set literal: the bits of the constant names, or'd at run time with those of the others. *) proc setLiteral(var x: TItem; t: PType): ERROR; var e: TItem; bits, slot: I64; start: Diag.Pos; s: []const U8; begin start := Scan.tokPos(); Scan.next; if (t # nil) and (t^.form # Sym.fSet) then return Scan.markAt(Scan.spanFrom(start), say1("expected a set type, found {}", Sym.typeName(t))); end; bits := 0; slot := 0; if Scan.tok # Scan.sRbrace then repeat if t = nil then try expression(e, nil); else try expression(e, t^.base); end; if t = nil then if e.typ^.form # Sym.fEnum then return Scan.markAt(e.pos, say1("a set holds names of an enumeration, found {}", Item.what(e))); end; t := try setOf(e.typ, e.pos); end; if not Sym.same(e.typ, t^.base) then if e.obj # nil then s := nameOf(e.obj^.name); else s := Item.what(e); end; return Scan.markAt(e.pos, say2("{} isn't a name of {}", s, Sym.typeName(t^.base))); end; if e.mode = Item.mConst then bits := bits | (I64(1) shl e.val); else if slot = 0 then slot := Gen.allocTemp(8, 8); Gen.pushConst(Gen.kWord, 0); Gen.popVar(Item.frameAdr(slot), 8); end; try Item.value(e); Gen.pushConst(Gen.kWord, 1); Gen.swap(); Gen.op(Scan.sShl, Gen.kWord, 8, false); Gen.pushVar(Item.frameAdr(slot), 8, false, Gen.kWord); Gen.op(Scan.sBar, Gen.kWord, 8, false); Gen.popVar(Item.frameAdr(slot), 8); end; if Scan.tok # Scan.sComma then break; end; Scan.next; until false; end; try expect(Scan.sRbrace); if t = nil then return Scan.markAt(Scan.spanFrom(start), "{} needs a known set type"); end; Item.makeConst(x, t, bits); if slot # 0 then Gen.pushVar(Item.frameAdr(slot), 8, false, Gen.kWord); Gen.pushConst(Gen.kWord, bits); Gen.op(Scan.sBar, Gen.kWord, 8, false); x.mode := Item.mStack; end; return nil; end; proc runDefers(base: INT); forward; (* The type of the last result of t, the results of a procedure: t if it has one, else the last of its several. *) proc lastResult(t: PType): PType; var f: PObj; begin if not t^.tuple then return t; end; f := t^.fields; while f^.next # nil do f := f^.next; end; return f^.typ; end; (* The results of the tuple t but the last: the same fields, one fewer *) proc withoutLast(t: PType): PType; var r: PType; begin r := Sym.newType(Sym.fRecord, t^.size, t^.align); r^.tuple := true; r^.fields := t^.fields; r^.len := t^.len - 1; return r; end; (* try c (the report, 7.8): the call c, then the error it returned, its last result: if that isn't nil, the procedure returns it, and a zero for each of its other results, after running the deferred statements in force. x is left c's other results: none (mNone), one (a value) or several (a tuple, for a multiple assignment). The error is kept in a slot of the frame, and tested there; the code for it, out of line, is made with the stack of values as it stands, and leaves it so, since what follows is the code for an error that is nil. *) proc tryCall(var x: TItem): ERROR; var called: BOOL; c: Gen.CallInfo; start, at: Diag.Pos; res: PType; last: PObj; slot: I64; ok: INT; direct: BOOL; begin start := Scan.tokPos(); Scan.next; sawTry := true; if deferring then return Scan.markAt(Scan.spanFrom(start), "try can't leave a deferred statement"); end; if curResult = nil then return Scan.markAt(Scan.spanFrom(start), "try needs a procedure that ends in an ERROR result, and this returns nothing"); elsif lastResult(curResult) # Sym.tError then return Scan.markAt(Scan.spanFrom(start), say1("try needs a procedure that ends in an ERROR result, and this returns {}", Sym.typeName(curResult))); end; try designator(x, called); if not called and ((x.mode = Item.mProc) or (((x.mode = Item.mVar) or (x.mode = Item.mStack)) and (x.typ^.form = Sym.fProc))) then (* a procedure without parameters, written without parentheses *) at := x.pos; try Item.beginCall(c, x); try Item.endCall(c, x); x.pos := at; called := true; end; if not called then return Scan.markAt(x.pos, say1("try needs a call, found {}", Item.what(x))); end; if x.mode = Item.mNone then return Scan.markAt(x.pos, "try needs a call that returns an ERROR last, and this returns nothing"); end; if lastResult(x.typ) # Sym.tError then return Scan.markAt(x.pos, say1("try needs a call that returns an ERROR last, and this returns {}", Sym.typeName(x.typ))); end; res := x.typ; direct := false; if x.mode = Item.mStack then (* an ERROR alone *) direct := numDefers = 0; if not direct then slot := Gen.allocTemp(8, 8); Gen.popVar(Item.frameAdr(slot), 8); end; x.mode := Item.mNone; x.typ := Sym.tNone; elsif x.mode = Item.mWords then (* two words, the error on top *) direct := numDefers = 0; if not direct then slot := Gen.allocTemp(8, 8); Gen.popVar(Item.frameAdr(slot), 8); end; x.mode := Item.mStack; x.typ := res^.fields^.typ; else (* several, in a temporary: the error is where it is *) last := res^.fields; while last^.next # nil do last := last^.next; end; slot := x.off + last^.val; if res^.len = 2 then try Item.field(x, res^.fields); else x.typ := withoutLast(res); end; end; ok := Gen.NoLabel; if direct then (* nothing to defer: the error is tested and returned from the register it is in, with no slot *) Gen.tryReturnFrom(Gen.tryTest(ok)); else Gen.pushVar(Item.frameAdr(slot), 8, false, Gen.kWord); Gen.jumpIf(ok, false); if numDefers > 0 then Gen.tryAlign(); end; runDefers(0); Gen.tryReturn(slot); end; Gen.fix(ok); x.pos := Scan.spanFrom(start); return nil; end; (* A doubt other than a drop, to be warned of at p with msg and hint. Text read twice, as a while's condition is, is noted once. *) proc noteDoubt(p: Diag.Pos; msg, hint: []const U8); var i: INT; begin for i := 0 to numDoubts - 1 do if (doubts[i].at.f = p.f) and (doubts[i].at.a = p.a) then return; end; end; if numDoubts < MaxDoubts then doubts[numDoubts].at := p; doubts[numDoubts].msg := msg; doubts[numDoubts].hint := hint; INC(numDoubts); end; end; (* Notes the string at the scanner if it has a \0 before a digit *) proc noteZeroDigit; var at: Diag.Pos; yes: BOOL; begin at, yes := Scan.zeroDigit(); if yes then noteDoubt(at, "\\0 is a zero byte and the digit after it is itself", "write \\x00 to keep them apart"); end; end; proc factor(var x: TItem; want: PType): ERROR; var called, vol, several: BOOL; t: PType; start: INT; s: []const U8; begin INC(nest); if nest > MaxNest then return tooDeep(); end; several := passOn; passOn := false; start := Scan.tokStart(); match Scan.tok of | sInt, sChar: (* only a hex number can be above high(I64), and it is a U64 *) if Scan.ival < 0 then Item.makeConst(x, Sym.tU64, Scan.ival); else Item.makeConst(x, Sym.tConst, Scan.ival); end; Scan.next; | sReal: Item.makeReal(x, Scan.rval); Item.usesReals(Scan.tokPos()); Scan.next; | sString: Item.makeStr(x); noteZeroDigit(); Scan.next; | sIdent: try designator(x, called); if x.mode = Item.mNone then if lastCalled # nil then s := nameOf(lastCalled^.name); else s := "this procedure"; end; return Scan.markAt(x.pos, say1("{} returns nothing", s)); end; if x.typ^.tuple and not several then if lastCalled # nil then s := nameOf(lastCalled^.name); else s := "this procedure"; end; return Scan.markHelp(x.pos, say1("{} returns several results", s), say1("they go to variables: a, b := {}(...)", s)); end; | sLparen: Scan.next; try expression(x, want); try expect(Scan.sRparen); | sTry: try tryCall(x); if x.mode = Item.mNone then return Scan.markAt(x.pos, "this try has no value"); end; if x.typ^.tuple then return Scan.markHelp(x.pos, "this try has several values", "they go to variables: a, b := try f(...)"); end; | sNot: Scan.next; try factor(x, nil); try Item.notOp(x); | sTilde: Scan.next; try factor(x, nil); try Item.complement(x); | sLbrak: try arrayLiteral(x, want); | sLbrace: if (want # nil) and (want^.form = Sym.fRecord) then try recordLiteral(x, want); else try setLiteral(x, want); end; | sArrow: Scan.next; vol := Scan.tok = Scan.sVolatile; if vol then Scan.next; end; t := try parseType(); t := newPtr(t, vol); try expect(Scan.sLparen); try expression(x, nil); try expect(Scan.sRparen); try Item.convert(x, t); (* a conversion is a factor, not a designator: nothing selects from it *) if Scan.tok = Scan.sArrow then return Scan.markHelp(Scan.tokPos(), "a conversion can't be dereferenced", "convert into a pointer variable, then dereference that"); end; | else: return expected("an expression"); end; x.pos := Scan.spanFromAt(start); DEC(nest); return nil; end; proc isMulOp: BOOL; begin match Scan.tok of | sTimes, sSlash, sDiv, sMod, sAmp, sShl, sShr: return true; | else: return false; end; end; proc isAddOp: BOOL; begin match Scan.tok of | sPlus, sMinus, sBar, sXor: return true; | else: return false; end; end; proc isRelOp: BOOL; begin match Scan.tok of | sEql, sNeq, sLss, sLeq, sGtr, sGeq, sIn: return true; | else: return false; end; end; proc term(var x: TItem; want: PType): ERROR; var op: Scan.Sym; y: TItem; L: INT; start: INT; begin start := Scan.tokStart(); try factor(x, want); while isMulOp() do op := Scan.tok; Scan.next; try Item.op1(op, x, L); try factor(y, nil); try Item.op2(op, x, y, L); x.pos := Scan.spanFromAt(start); end; return nil; end; proc simpleExpr(var x: TItem; want: PType): ERROR; var op: Scan.Sym; y: TItem; L: INT; start: INT; begin start := Scan.tokStart(); if Scan.tok = Scan.sMinus then Scan.next; try term(x, nil); x.pos := Scan.spanFromAt(start); try Item.neg(x); else try term(x, want); end; while isAddOp() do op := Scan.tok; Scan.next; try Item.op1(op, x, L); try term(y, nil); try Item.op2(op, x, y, L); x.pos := Scan.spanFromAt(start); end; return nil; end; (* A relation. It sets bareBoolRel, for the and or or that may follow: a relation binds tighter than and and or, so the # in "a # b and c" is read first, as (a # b) and c, which a Pascal reader doesn't expect. *) proc relation(var x: TItem; want: PType): ERROR; var op: Scan.Sym; y: TItem; L: INT; bools: BOOL; begin try simpleExpr(x, want); bools := false; if isRelOp() then op := Scan.tok; Scan.next; try Item.op1(op, x, L); if op = Scan.sIn then try simpleExpr(y, nil); else try simpleExpr(y, x.typ); end; bools := ((op = Scan.sEql) or (op = Scan.sNeq)) and (x.typ = Sym.tBool) and (y.typ = Sym.tBool); try Item.op2(op, x, y, L); x.pos := Diag.join(x.pos, y.pos); end; bareBoolRel := bools; (* a relation in parentheses comes here as a factor, and sets it false *) return nil; end; (* Notes x, an operand of the and or or (word) just read, if it is an unparenthesised = or # of two BOOLs *) proc noteBoolRel(const x: TItem; word: []const U8); begin if bareBoolRel then noteDoubt(x.pos, say2("{} is read first: = and # bind tighter than {}", Diag.source(x.pos), word), "put the = or # of two BOOLs in parentheses"); end; end; (* and binds tighter than or, and both below the relations *) proc conjunct(var x: TItem; want: PType): ERROR; var y: TItem; L: INT; start: INT; begin start := Scan.tokStart(); try relation(x, want); if Scan.tok = Scan.sAnd then noteBoolRel(x, "and"); repeat Scan.next; try Item.op1(Scan.sAnd, x, L); try relation(y, nil); noteBoolRel(y, "and"); try Item.op2(Scan.sAnd, x, y, L); x.pos := Scan.spanFromAt(start); until Scan.tok # Scan.sAnd; bareBoolRel := false; (* x is a conjunction now *) end; return nil; end; proc expression(var x: TItem; want: PType): ERROR; var y: TItem; L: INT; start: INT; begin start := Scan.tokStart(); try conjunct(x, want); if Scan.tok = Scan.sOr then noteBoolRel(x, "or"); repeat Scan.next; try Item.op1(Scan.sOr, x, L); try conjunct(y, nil); noteBoolRel(y, "or"); try Item.op2(Scan.sOr, x, y, L); x.pos := Scan.spanFromAt(start); until Scan.tok # Scan.sOr; bareBoolRel := false; (* x is a disjunction now *) end; return nil; end; (* Statements *) proc enterLoop(var lp: Loop; var old: PLoop); begin lp.brk := Gen.NoLabel; lp.cont := Gen.NoLabel; lp.base := numDefers; old := curLoop; curLoop := SYSTEM.ADR(lp); end; (* Runs the deferred statements in force above base, newest first. *) proc runDefers(base: INT); var i: INT; begin for i := numDefers - 1 to base by -1 do Gen.runDefer(defers[i]); end; end; (* Whether what follows "then" is a lone "break;" or "continue;", with no deferred statement to run on the way out of the round: it must be the branch's last statement, so ";" and then end, elsif or else follow. *) proc loneJump: BOOL; var st: Scan.State; lone: BOOL; begin if ((Scan.tok # Scan.sBreak) and (Scan.tok # Scan.sContinue)) or (curLoop = nil) or (numDefers # curLoop^.base) then return false; end; Scan.saveScan(st); lone := false; Scan.next; if Scan.tok = Scan.sSemicolon then Scan.next; lone := (Scan.tok = Scan.sEnd) or (Scan.tok = Scan.sElsif) or (Scan.tok = Scan.sElse); end; Scan.restoreScan(st); return lone; end; (* The condition x of if or elsif, and the statements it guards, which L skips when it is false. "if c then break; end" is one jump out of the loop when c holds, not one past a jump out; "if c then continue; end" one jump to the end of the round. *) proc thenPart(var x: TItem; var L: INT): ERROR; begin try expect(Scan.sThen); L := Gen.NoLabel; (* stays so with a lone break or continue: there is nothing to skip *) if loneJump() then Chunk.noteLine(Scan.curLine(), Scan.curCol()); (* the jump gets the break's or continue's line *) if Scan.tok = Scan.sBreak then try Item.condJumpTrue(x, curLoop^.brk); else try Item.condJumpTrue(x, curLoop^.cont); end; Scan.next; Scan.next; (* skip "break" or "continue", and ";" *) ends := true; else try Item.condJump(x, L); try statements(); end; return nil; end; (* if, elsif and else: each condition's jump skips its branch to the next; each branch that doesn't end jumps to the end. A branch that ends, in return or break, needs no jump past the others, and all of them ending is told to the caller in the global ends. *) proc ifStatement(start: Diag.Pos; ind: INT): ERROR; var x: TItem; L, endL: INT; all: BOOL; begin Scan.next; try expression(x, nil); try thenPart(x, L); all := ends; endL := Gen.NoLabel; while Scan.tok = Scan.sElsif do if not ends then Gen.jump(endL); end; Gen.fix(L); Scan.next; try expression(x, nil); try thenPart(x, L); all := all and ends; end; if Scan.tok = Scan.sElse then if not ends then Gen.jump(endL); end; Gen.fix(L); Scan.next; if Scan.tok = Scan.sIf then elseIf := true; end; try statements(); all := all and ends; else Gen.fix(L); all := false; end; try closeEnd(start, ind); Gen.fix(endL); ends := all; return nil; end; (* while c do s end: the test comes after the body, and the loop jumps to it first, so that a round takes one jump, back to the top if c holds: jmp test rather than top: c, jump to out if false top: s s test: c, jump to top if true jmp top out: One pass reads c before s: so c is compiled where it is written, for its messages, and its code taken back (rewind); then read again, and compiled, after s (saveScan, restoreScan). *) proc whileStatement(start: Diag.Pos; ind: INT): ERROR; var x: TItem; top, L, test: INT; lp: Loop; old: PLoop; cond, after: Scan.State; m: Gen.CodeMark; begin Scan.next; Scan.saveScan(cond); Gen.markCode(m); try expression(x, nil); try Item.discard(x); Gen.rewind(m); try expect(Scan.sDo); test := Gen.NoLabel; Gen.jump(test); Gen.alignLoop(); top := Chunk.pc; enterLoop(lp, old); try statements(); curLoop := old; Scan.saveScan(after); Scan.restoreScan(cond); Gen.fix(test); Gen.fix(lp.cont); (* a continue goes to the test *) Chunk.noteLine(Scan.curLine(), Scan.curCol()); try expression(x, nil); ends := (x.mode = Item.mConst) and (x.val # 0) and (lp.brk = Gen.NoLabel); (* while true *) if (x.mode = Item.mConst) and (x.typ^.form = Sym.fBool) then if x.val # 0 then Gen.jumpBack(top); end; else L := Gen.NoLabel; try Item.condJumpTrue(x, L); Gen.fixTo(L, top); end; Scan.restoreScan(after); try closeEnd(start, ind); Gen.fix(lp.brk); return nil; end; proc repeatStatement: ERROR; var x: TItem; top, L: INT; lp: Loop; old: PLoop; begin Scan.next; top := Chunk.pc; enterLoop(lp, old); try statements(); curLoop := old; try expect(Scan.sUntil); Gen.fix(lp.cont); (* a continue goes to the test *) try expression(x, nil); ends := (x.mode = Item.mConst) and (x.val = 0) and (lp.brk = Gen.NoLabel); (* until false *) L := Gen.NoLabel; try Item.condJump(x, L); Gen.fixTo(L, top); Gen.fix(lp.brk); return nil; end; (* for v := a to b by s do ... end, laid out as v := a; the limit b, in a slot unless it is a constant if v is past b then goto exit goto body top: v := v + s body: the statements the end of a round: goto top, or exit (forNext) exit: so that a round of a loop by 1 takes one compare and one jump. *) proc forStatement(start: Diag.Pos; ind: INT): ERROR; var o, lim: PObj; v, a, b, s: TItem; step: I64; top, body, bodyL, exitL, n: INT; lp: Loop; old: PLoop; f: Sym.Form; slot: I64; begin n := numFors; INC(numFors); Scan.next; if Scan.tok # Scan.sIdent then return expected("a name"); end; o := Sym.lookup(Scan.id); if o = nil then notDeclared(Scan.tokPos(), Scan.id); return Scan.Failed; end; named(o); if o^.cls # Sym.oVar then return Scan.mark(say1("{} isn't a variable", nameOf(Scan.id))); end; f := o^.typ^.form; if (f # Sym.fInt) and (f # Sym.fEnum) then return Scan.mark(say1("a for loop needs an integer or enumeration variable, found {}", Sym.typeName(o^.typ))); end; Scan.next; try expect(Scan.sBecomes); try Item.makeItem(v, o); try expression(a, o^.typ); try Item.coerce(a, o^.typ); try Item.assign(v, a); try expect(Scan.sTo); try expression(b, o^.typ); try Item.coerce(b, o^.typ); (* the limit, evaluated once: a constant as it is; else, in an innermost loop, in the register chooseRegs kept for it, as a variable no one can name; else in a slot *) if (b.mode # Item.mConst) and (limitK >= 0) and (n < MaxFors) and limitIn[n] then lim := Sym.newObj(Sym.oVar, o^.name, b.typ); lim^.lev := 1; lim^.reg := Gen.VarRegs[limitK]; try Item.makeItem(v, lim); try Item.assign(v, b); b := v; elsif b.mode # Item.mConst then slot := try Item.toSlot(b); Item.tempItem(b, b.typ, slot); end; step := 1; if Scan.tok = Scan.sBy then Scan.next; try expression(s, nil); step := try constValue(s); if step = 0 then return Scan.markAt(s.pos, "the step can't be zero"); end; end; try expect(Scan.sDo); exitL := Gen.NoLabel; if step > 0 then try Item.forCmp(o, b, Scan.sGtr, exitL); else try Item.forCmp(o, b, Scan.sLss, exitL); end; bodyL := Gen.NoLabel; Gen.jump(bodyL); Gen.alignLoop(); top := Chunk.pc; try Item.forStep(o, step); Gen.fix(bodyL); body := Chunk.pc; enterLoop(lp, old); try statements(); curLoop := old; try closeEnd(start, ind); Gen.fix(lp.cont); (* a continue goes to the end of the round *) try Item.forNext(o, b, step, exitL, top, body); Gen.fix(exitL); Gen.fix(lp.brk); return nil; end; type Labels = record lo, hi: [MaxLabels]I64; (* the ranges of the labels so far *) at: [MaxLabels]INT; (* where the arm of each starts *) n: INT; end; (* a < b, for values of an integer type, unsigned or not *) proc below(a, b: I64; unsign: BOOL): BOOL; begin if unsign then return U64(a) < U64(b); end; return a < b; end; (* An arm of a match on an integer of type t: its labels, each a constant or a range, none overlapping another (report section 7.4), then its statements. No code tests the labels here; dispatch does, after the last arm, for which each label notes where its arm starts. *) proc intArm(t: PType; var ls: Labels; var endL: INT): ERROR; var lo, hi: TItem; first, i: INT; u: BOOL; start: Diag.Pos; begin u := not t^.signed; first := ls.n; repeat start := Scan.tokPos(); try expression(lo, t); try Item.coerce(lo, t); _ := try constValue(lo); hi := lo; if Scan.tok = Scan.sUpto then Scan.next; try expression(hi, t); try Item.coerce(hi, t); _ := try constValue(hi); if below(hi.val, lo.val, u) then return Scan.markAt(Scan.spanFrom(start), "the range is empty"); end; end; for i := 0 to ls.n - 1 do if not below(hi.val, ls.lo[i], u) and not below(ls.hi[i], lo.val, u) then if lo.val = hi.val then return Scan.markAt(Scan.spanFrom(start), say1("{} is in another label too", Diag.source(Scan.spanFrom(start)))); end; return Scan.markAt(Scan.spanFrom(start), say1("{} overlaps another label", Diag.source(Scan.spanFrom(start)))); end; end; if ls.n >= MaxLabels then return Scan.markAt(Scan.spanFrom(start), tooMany(MaxLabels, "labels in one match")); end; ls.lo[ls.n] := lo.val; ls.hi[ls.n] := hi.val; INC(ls.n); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; try expect(Scan.sColon); for i := first to ls.n - 1 do ls.at[i] := Chunk.pc; end; try statements(); Gen.jump(endL); return nil; end; (* An arm of a match on a string, whose slice is in slot p: its labels, string constants, none equal to another (report section 7.4), each compared in turn as its arm is reached, then its statements. ls keeps each label's chunk in lo and its length in hi, for the comparing of labels. *) proc strArm(p: I64; var ls: Labels; var endL: INT): ERROR; var x: TItem; i, L, body: INT; last: BOOL; s: []const U8; begin L := Gen.NoLabel; body := Gen.NoLabel; repeat try expression(x, nil); if x.mode # Item.mStr then return Scan.markAt(x.pos, say1("a label of a match on a string is a string constant, found {}", Item.what(x))); end; s := Chunk.rodata[Chunk.chunks[x.chunk].start..Chunk.chunks[x.chunk].start + INT(x.val)]; for i := 0 to ls.n - 1 do if s = Chunk.rodata[Chunk.chunks[ls.lo[i]].start..Chunk.chunks[ls.lo[i]].start + INT(ls.hi[i])] then return Scan.markAt(x.pos, say1("{} is in another label too", Diag.source(x.pos))); end; end; if ls.n >= MaxLabels then return Scan.markAt(x.pos, tooMany(MaxLabels, "labels in one match")); end; ls.lo[ls.n] := x.chunk; ls.hi[ls.n] := x.val; INC(ls.n); last := Scan.tok # Scan.sComma; try Item.matchStr(p, x, last, L, body); if last then break; end; Scan.next; until false; try expect(Scan.sColon); Gen.fix(body); try statements(); Gen.jump(endL); Gen.fix(L); return nil; end; const MinTable = 4; (* fewer labels are tested one by one *) TableDensity = 8; (* a table has at most this many entries to a label *) (* The code that picks the arm, after the last one, which the match's start jumped to: label by label, as a chain of compares each to its arm; or, for four or more labels not far apart, a jump table (jumpTable), which takes the same few instructions for any of them. It is here and not before the arms, since a table can only be made once every arm's place is known; an arm is entered by a jump back to it. elseAt is where the else arm starts, or NoLabel. *) proc dispatch(t: PType; p: I64; var ls: Labels; elseAt: INT; var endL: INT); var lo, hi: I64; i, start, v, body: INT; u: BOOL; entry: [MaxLabels]INT; begin u := (t^.form = Sym.fInt) and not t^.signed; lo := 0; hi := 0; if ls.n > 0 then lo := ls.lo[0]; hi := ls.hi[0]; for i := 1 to ls.n - 1 do if below(ls.lo[i], lo, u) then lo := ls.lo[i]; end; if below(hi, ls.hi[i], u) then hi := ls.hi[i]; end; end; end; if (ls.n >= MinTable) and (lo >= -2147483648) and (lo <= 2147483647) and (U64(hi - lo) < U64(TableDensity * ls.n)) and (hi - lo < MaxLabels) then start := Gen.jumpTable(p, lo, INT(hi - lo) + 1, elseAt, endL); for v := 0 to INT(hi - lo) do if elseAt # Gen.NoLabel then entry[v] := elseAt; else entry[v] := start; end; end; for i := 0 to ls.n - 1 do for v := INT(ls.lo[i] - lo) to INT(ls.hi[i] - lo) do entry[v] := ls.at[i]; end; end; for v := 0 to INT(hi - lo) do Gen.tableEntry(start, entry[v]); end; return; end; for i := 0 to ls.n - 1 do body := Gen.NoLabel; Item.matchRange(p, ls.lo[i], ls.hi[i], body); Gen.fixTo(body, ls.at[i]); end; if elseAt # Gen.NoLabel then Gen.jumpBack(elseAt); end; end; (* The case of t, an enumeration or a variant, named n, or nil *) proc caseNamed(t: PType; n: INT): PObj; var c: PObj; begin c := t^.fields; while (c # nil) and (c^.name # n) do c := c^.next; end; return c; end; (* The case of t, an enumeration or a variant, that an arm of a match names (report section 7.4). A name alone is looked for among t's own, whatever the scope holds. A qualified one, Kind.done, Xml.done or Xml.Kind.done, is read by qualIdent, as it is anywhere else, and must be one of t's. *) proc armCase(t: PType): (PObj, ERROR); var st: Scan.State; c: PObj; at: Diag.Pos; qualified: BOOL; msg: []const U8; begin at := Scan.tokPos(); Scan.saveScan(st); Scan.next; qualified := Scan.tok = Scan.sPeriod; Scan.restoreScan(st); if not qualified then c := caseNamed(t, Scan.id); if c = nil then return nil, Scan.mark(say2("{} isn't a case of {}", nameOf(Scan.id), Sym.typeName(t))); end; Scan.next; return c, nil; end; c := try qualIdent(); if (c^.cls # Sym.oCase) and not c^.ofEnum then return nil, Scan.markAt(Scan.spanFrom(at), say1("expected a case of {}", Sym.typeName(t))); end; if not Sym.same(c^.typ, t) then msg := say2("the match is on {}, not {}", Sym.typeName(t), Sym.typeName(c^.typ)); if caseNamed(t, c^.name) = nil then return nil, Scan.markAt(Scan.spanFrom(at), msg); end; return nil, Scan.markHelp(Scan.spanFrom(at), msg, say2("{} has a {} of its own: write it alone", Sym.typeName(t), nameOf(c^.name))); end; return c, nil; end; proc matchStatement(start: Diag.Pos; ind: INT): ERROR; var x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT; seen: [256]BOOL; hasElse, all, byValue, isStr, onlyRead: BOOL; endAt, at: Diag.Pos; ls: Labels; begin Scan.next; try expression(x, nil); if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end; (* a const parameter or a constant: then so are the names its arms give *) onlyRead := Item.onlyRead(x) or x.elemRo or ((x.mode = Item.mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst)); p := try Item.matchBegin(x); t := x.typ; isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *) try expect(Scan.sOf); hasElse := false; all := true; endL := Gen.NoLabel; ls.n := 0; (* an integer or an enumeration is matched by its value, and the arms are picked by dispatch, after them; a variant by its tag, arm by arm *) byValue := (t^.form = Sym.fInt) or (t^.form = Sym.fEnum); disp := Gen.NoLabel; elseAt := Gen.NoLabel; if byValue then Gen.jump(disp); end; while Scan.tok = Scan.sBar do Scan.next; if hasElse then return Scan.mark("else must be the last arm"); end; if Scan.tok = Scan.sBlank then return Scan.markHelp(Scan.tokPos(), "_ matches nothing: it is the blank, which stands only left of :=", "the arm for every other case is | else:"); end; if Scan.tok = Scan.sElse then Scan.next; try expect(Scan.sColon); elseAt := Chunk.pc; try statements(); if byValue then Gen.jump(endL); end; hasElse := true; elsif t^.form = Sym.fInt then try intArm(t, ls, endL); elsif isStr then try strArm(p, ls, endL); else first := ls.n; L := Gen.NoLabel; repeat if Scan.tok # Scan.sIdent then return expected("a case"); end; at := Scan.tokPos(); c := try armCase(t); if (c^.val <= 255) and seen[c^.val] then return Scan.markAt(Scan.spanFrom(at), say1("{} has two arms", nameOf(c^.name))); end; if c^.val <= 255 then seen[c^.val] := true; end; if t^.form # Sym.fEnum then Item.matchTag(p, c^.val, L); break; end; (* an enumeration's arm may name several, which share its place *) ls.lo[ls.n] := c^.val; ls.hi[ls.n] := c^.val; INC(ls.n); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; try Sym.openScope(Sym.newScope()); if Scan.tok = Scan.sLparen then Scan.next; f := c^.fields; n := 0; repeat if (Scan.tok = Scan.sBlank) and (f # nil) then return Scan.markHelp(Scan.tokPos(), "a field is named, not blanked", say1("call one the arm doesn't read _{}", nameOf(f^.name))); end; if Scan.tok # Scan.sIdent then return expected("a name"); end; if f = nil then return Scan.mark(say1("more names than {} has fields", nameOf(c^.name))); end; o := Sym.newObj(Sym.oVar, Scan.id, f^.typ); o^.lev := 1; o^.ind := true; o^.isVar := true; o^.isConst := onlyRead; o^.val := p; o^.off := f^.val; try declare(o); f := f^.next; INC(n); Scan.next; if Scan.tok # Scan.sComma then break; end; Scan.next; until false; if f # nil then return Scan.mark(say1("name all the fields of {}, or none", nameOf(c^.name))); end; try expect(Scan.sRparen); end; try expect(Scan.sColon); for n := first to ls.n - 1 do ls.at[n] := Chunk.pc; end; try statements(); Sym.closeScope(); Gen.jump(endL); if t^.form # Sym.fEnum then Gen.fix(L); end; end; all := all and ends; end; endAt := Scan.tokPos(); try closeEnd(start, ind); ends := all; if (t^.form = Sym.fInt) and not hasElse then return Scan.markHelp(endAt, "a match on an integer needs an else arm", "it may be empty: | else:"); elsif isStr and not hasElse then return Scan.markHelp(endAt, "a match on a string needs an else arm", "it may be empty: | else:"); end; if not hasElse then c := t^.fields; while c # nil do if (c^.val > 255) or not seen[c^.val] then return Scan.markAt(endAt, say1("no arm for {}", nameOf(c^.name))); end; c := c^.next; end; end; if byValue then Gen.fix(disp); dispatch(t, p, ls, elseAt, endL); end; Gen.fix(endL); return nil; end; (* What a message calls x: its name n when x is just that name, else what it is. *) proc subject(x: TItem; n: INT): []const U8; begin if x.pos.b - x.pos.a = LEN(nameOf(n)) then return nameOf(n); end; if x.mode = Item.mConst then return "a constant"; end; return "a value"; end; (* x, named first n, is a variable the program may assign: not an array constant, nor any part of one. *) proc assignable(const x: TItem; n: INT): ERROR; begin if (x.mode = Item.mConst) and (subject(x, n) = nameOf(n)) then return Scan.markAt(x.pos, say1("{} is a constant, and can't be changed", nameOf(n))); end; if (x.mode = Item.mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst) then return Scan.markAt(x.pos, say1("{} is a constant, and can't be changed", nameOf(x.obj^.name))); end; if (x.mode # Item.mVar) or not x.isVar then return Scan.markAt(x.pos, say1("can't assign to {}", subject(x, n))); end; return nil; end; (* The designator a statement starts with. A variable named whole before := is assigned, not read: naming it there leaves it as unread as it was. *) proc leftSide(var x: TItem; var called: BOOL): ERROR; var o: PObj; wasRead: BOOL; n: INT; begin n := Scan.id; o := Sym.lookup(n); wasRead := (o # nil) and o^.read; try designator(x, called); if ((Scan.tok = Scan.sBecomes) or (Scan.tok = Scan.sComma)) and (x.pos.b - x.pos.a = LEN(nameOf(n))) then o^.read := wasRead; end; return nil; end; (* a, b := f(x): the variables first, left to right, then the call; then each result to its variable, in order. x is the first variable, first its name, blank whether it is _. *) proc multiAssign(var x: TItem; first: INT; blank: BOOL): ERROR; var d: [16]TItem; dn: [16]INT; blanks: [16]BOOL; y, z: TItem; n, i: INT; called: BOOL; f: PObj; begin n := 0; d[0] := x; dn[0] := first; blanks[0] := blank; repeat if not blanks[n] then try Item.notReadOnly(d[n]); try assignable(d[n], dn[n]); try Item.pin(d[n]); (* works out the address now, so that the call can't change it *) end; INC(n); if Scan.tok # Scan.sComma then break; end; if n > 15 then return Scan.mark(tooMany(16, "variables in one assignment")); end; Scan.next; blanks[n] := Scan.tok = Scan.sBlank; if blanks[n] then Scan.next; else dn[n] := Scan.id; try leftSide(d[n], called); end; until false; try expect(Scan.sBecomes); if Scan.tok = Scan.sTry then try tryCall(y); called := true; else try designator(y, called); end; if not called or not y.typ^.tuple or (y.typ^.len # I64(n)) then return Scan.markAt(y.pos, Diag.say("expected a call with {} results", [Fmt.int(n)])); end; (* two results on the stack: the first is brought to the top, and each is popped into its variable in turn, as one value; else each is read from the temporary they came back in *) if y.mode = Item.mWords then Gen.swap(); end; f := y.typ^.fields; for i := 0 to n - 1 do z := y; if y.mode = Item.mWords then z.mode := Item.mStack; z.typ := f^.typ; else try Item.field(z, f); end; if blanks[i] then try Item.discard(z); else (* an INT result widens to an I64 variable; an I64 result into an INT is written, but not here, in the call *) if Item.widens(d[i].typ, z.typ) then return Scan.markAt(y.pos, Diag.say("result {} is {}, which can't go to {}", [Fmt.int(i + 1), Fmt.str(Sym.typeName(z.typ)), Fmt.str(Sym.typeName(d[i].typ))])); end; try Item.coerce(z, d[i].typ); try Item.assign(d[i], z); end; f := f^.next; end; return nil; end; (* return a, b; or return f(x); with f's several results passed on *) proc returnResults: ERROR; var f: PObj; x: TItem; begin f := curResult^.fields; passOn := true; while f # nil do try expression(x, f^.typ); if x.typ^.tuple then if not Sym.same(x.typ, curResult) and not Item.resultsFit(x.typ, curResult) then return Scan.markAt(x.pos, say2("expected the results {}, found {}", Sym.typeName(curResult), Sym.typeName(x.typ))); end; return Item.returnAll(x); end; try Item.returnField(f, x); f := f^.next; if f # nil then try expect(Scan.sComma); end; end; return nil; 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. *) proc deferStatement: ERROR; var L, pos, saved: INT; oldLoop: PLoop; oldDeferring: BOOL; start: Diag.Pos; begin start := Scan.tokPos(); Scan.next; if directDefer then return Scan.markAt(Scan.spanFrom(start), "defer can't be deferred"); end; if numDefers >= MaxDefers then return Scan.markAt(Scan.spanFrom(start), tooMany(MaxDefers, "deferred statements in one procedure")); end; L := Gen.NoLabel; pos, saved := Gen.deferBegin(L); oldLoop := curLoop; oldDeferring := deferring; curLoop := nil; deferring := true; directDefer := true; try statement(); directDefer := false; curLoop := oldLoop; deferring := oldDeferring; Gen.deferEnd(L, saved); defers[numDefers] := pos; INC(numDefers); return nil; end; (* Whether the expression of x := ..., which starts at the current symbol, is x changed by adding to it, taking from it or its bits: x + e, x - e, x | e or x xor e, with x nowhere in e, so that it can be worked out in x's own register, if x is in one (Gen's inPlace). *) proc changesItself(const x: TItem): BOOL; var st: Scan.State; wasSkipping, changes: BOOL; d: INT; begin if (x.mode # Item.mVar) or (x.base # Gen.bReg) or (x.typ^.form = Sym.fSlice) or (Scan.tok # Scan.sIdent) or (Scan.id # x.obj^.name) then return false; end; changes := false; Scan.saveScan(st); wasSkipping := Scan.skipping; Scan.setSkipping(true); (* its slips are told when it is compiled *) Scan.next; if isAddOp() then changes := true; d := 0; while not ((d = 0) and ((Scan.tok = Scan.sSemicolon) or (Scan.tok = Scan.sEnd))) and (Scan.tok # Scan.sEof) do if (Scan.tok = Scan.sIdent) and (Scan.id = x.obj^.name) then changes := false; end; match Scan.tok of | sLparen, sLbrak, sLbrace: INC(d); | sRparen, sRbrak, sRbrace: DEC(d); | else: end; Scan.next; end; end; Scan.restoreScan(st); Scan.setSkipping(wasSkipping); return changes; end; (* A call standing on its own, which drops the results x holds, if any, to be warned of *) proc noteDrop(const x: TItem); begin if (x.mode # Item.mNone) and (numDoubts < MaxDoubts) then doubts[numDoubts].at := x.pos; doubts[numDoubts].msg := ""; if x.typ^.tuple then doubts[numDoubts].n := INT(x.typ^.len); else doubts[numDoubts].n := 1; end; INC(numDoubts); end; end; proc oneStatement: ERROR; var x, y: TItem; called: BOOL; m: I64; c: Gen.CallInfo; start: Diag.Pos; first, n, i: INT; k: Gen.Kind; begin start := Scan.tokPos(); first := Scan.id; Chunk.noteLine(Scan.curLine(), Scan.curCol()); m := Gen.tempMark(); ends := false; match Scan.tok of | sIdent: try leftSide(x, called); if Scan.tok = Scan.sBecomes then try Item.notReadOnly(x); try assignable(x, first); Scan.next; if changesItself(x) then Gen.setInPlace(x.chunk); end; try expression(y, x.typ); Gen.setInPlace(-1); try Item.assign(x, y); elsif Scan.tok = Scan.sComma then try multiAssign(x, first, false); elsif not called then if (x.mode = Item.mProc) or (((x.mode = Item.mVar) or (x.mode = Item.mStack)) and (x.typ^.form = Sym.fProc)) then try Item.beginCall(c, x); try Item.endCall(c, x); try Item.discard(x); elsif Scan.tok = Scan.sEql then return Scan.markHelp(x.pos, say1("{} isn't a statement", subject(x, first)), "assignment is written :="); else return Scan.markAt(x.pos, say1("{} isn't a statement", subject(x, first))); end; else noteDrop(x); try Item.discard(x); end; | sBlank: Scan.next; if Scan.tok = Scan.sComma then try multiAssign(x, 0, true); else try expect(Scan.sBecomes); try expression(x, nil); (* evaluated and dropped: a single value is loaded, so that a volatile one is read *) if (x.mode = Item.mType) or (x.mode = Item.mBuiltin) or (x.mode = Item.mNone) or (x.mode = Item.mCase) or ((x.mode # Item.mStr) and not Sym.isAggregate(x.typ)) then try Item.value(x); end; try Item.discard(x); end; | sIf: try ifStatement(start, Scan.indent()); | sWhile: try whileStatement(start, Scan.indent()); | sRepeat: try repeatStatement(); | sFor: try forStatement(start, Scan.indent()); | sMatch: try matchStatement(start, Scan.indent()); | sReturn: Scan.next; if deferring then return Scan.markAt(Scan.spanFrom(start), "return can't leave a deferred statement"); end; if Scan.tok = Scan.sSemicolon then runDefers(0); try Item.returnNone(); else if (curResult # nil) and curResult^.tuple then try returnResults(); else Item.literalInResult((Scan.tok = Scan.sLbrace) and (curResult # nil) and (curResult^.form = Sym.fRecord) and Sym.behindPointer(curResult)); try expression(x, curResult); Item.literalInResult(false); try Item.returnValue(x); end; (* the result, one value or two words on top, waits in the frame while the deferred statements run *) if (numDefers > 0) and (curResult # nil) and not Sym.behindPointer(curResult) then n := 1; k := Gen.kindOf(curResult); if Sym.inTwoRegs(curResult) then n := 2; k := Gen.kWord; end; if deferSlot = 0 then deferSlot := Gen.allocPerm(16, 8); end; for i := n - 1 to 0 by -1 do Gen.popVar(Item.frameAdr(deferSlot + I64(8 * i)), 8); end; runDefers(0); for i := 0 to n - 1 do Gen.pushVar(Item.frameAdr(deferSlot + I64(8 * i)), 8, false, k); end; else runDefers(0); end; Gen.returnJump(); ends := true; if returnEnd < 0 then returnEnd := Scan.afterPrev().a; if Scan.tok = Scan.sSemicolon then returnEnd := Scan.tokStart() + 1; end; end; end; | sBreak: Scan.next; if curLoop = nil then return Scan.markAt(Scan.spanFrom(start), "break outside a loop"); end; runDefers(curLoop^.base); Gen.jump(curLoop^.brk); | sContinue: Scan.next; if curLoop = nil then return Scan.markAt(Scan.spanFrom(start), "continue outside a loop"); end; runDefers(curLoop^.base); Gen.jump(curLoop^.cont); | sDefer: try deferStatement(); | sTry: try tryCall(x); noteDrop(x); try Item.discard(x); | sArrow: return Scan.markHelp(Scan.tokPos(), "a statement starts with a name", "convert into a pointer variable, then assign through that"); | else: if Scan.atLineStart() then return endMissing(Scan.missingPos(), say1("expected \"end\", found {}", Scan.found())); end; return expected("a statement"); end; Gen.releaseTemps(m); return nil; end; proc statement: ERROR; var r: Recover; m: I64; err: ERROR; begin m := Gen.tempMark(); guard(r); INC(nest); if nest > MaxNest then err := tooDeep(); else err := oneStatement(); end; if err = nil then DEC(nest); skipped := false; else restore(r); (* a statement never starts a line: this is the next declaration *) if Scan.atLineStart() or not skipConstruct() then return giveUp(r); end; Gen.releaseTemps(m); skipped := true; end; unguard(r); return nil; end; proc endsStatements: BOOL; begin match Scan.tok of | sEnd, sElse, sElsif, sUntil, sBar, sEof: return true; | else: return false; end; end; proc statements: ERROR; var base: INT; old: BOOL; begin base := numDefers; old := directDefer; directDefer := false; ends := false; while not endsStatements() do try statement(); if not skipped or (Scan.tok = Scan.sSemicolon) then try expect(Scan.sSemicolon); end; end; runDefers(base); numDefers := base; directDefer := old; return nil; end; (* Declarations *) (* In a file read for its interface only: skip a procedure's body, from its local declarations to the end that closes it. *) proc skipBody: ERROR; var d: INT; sawBegin: BOOL; begin d := 0; sawBegin := false; repeat match Scan.tok of | sBegin: if d = 0 then sawBegin := true; end; INC(d); | sIf, sWhile, sFor, sMatch, sRecord, sVariant: INC(d); | sEnd: DEC(d); if (d = 0) and sawBegin then Scan.next; return nil; end; | sEof: return expected("\"end\""); | else: end; Scan.next; until false; return nil; end; proc declarations(top: BOOL); forward; (* How Rt shows a variable of o's type in the trap report: 1 a signed integer, 2 an unsigned one, 3 a BOOL, 4 an address, 5 a real; plus its size times 16, and 256 if the frame holds its address, or 512 times 1 + i if it is in register ShownRegs[i]; 0 if it isn't shown. *) proc varKind(o: PObj): INT; var k, i, v: INT; begin if o^.ind and (o^.reg # 0) then return 0; end; (* the address is in a register, not shown *) match o^.typ^.form of | fInt: if o^.typ^.signed then k := 1; else k := 2; end; | fEnum, fSet: k := 2; | fBool: k := 3; | fPtr, fProc, fError: k := 4; | fReal: k := 5; | else: return 0; end; v := k + INT(o^.typ^.size) * 16; if o^.ind then v := v + 256; end; for i := 0 to LEN(Gen.ShownRegs) - 1 do if o^.reg = Gen.ShownRegs[i] then v := k + INT(o^.typ^.size) * 16 + (i + 1) * 512; end; end; return v; end; (* Whether variable o could live in registers: a single value, a real in an xmm register, or a slice, in two, that isn't C's *) proc regFit(o: PObj): BOOL; var f: Sym.Form; begin if (o^.cls # Sym.oVar) or o^.ext then return false; end; (* a parameter passed by its address keeps the address in the register *) if o^.ind then return o^.param; end; f := o^.typ^.form; return (f = Sym.fInt) or (f = Sym.fBool) or (f = Sym.fEnum) or (f = Sym.fPtr) or (f = Sym.fProc) or (f = Sym.fError) or (f = Sym.fSlice) or (f = Sym.fReal) or ((f = Sym.fSet) and (o^.typ^.size <= 8)); end; (* The first local of the procedure being compiled. Locals are chained by chain in the order declared, the module's own first; before is the last one declared before the procedure's, or nil if there was none. *) proc firstLocal(before: PObj): PObj; begin if before = nil then return Sym.curModule^.locals; end; return before^.chain; end; const MaxCand = 64; (* variables of a procedure that may go in registers *) var cand: [MaxCand]PObj; (* chooseRegs's candidates and their weights *) weight: [MaxCand]INT; numCand: INT; proc candidate(n: INT): INT; var j: INT; begin for j := 0 to numCand - 1 do if cand[j]^.name = n then return j; end; end; return -1; end; proc addCandidate(o: PObj); begin if regFit(o) and (numCand < MaxCand) then cand[numCand] := o; weight[numCand] := 0; INC(numCand); end; end; (* Whether naming o may make a call: a procedure (not one made in place whose expression calls none), a procedure variable, or NEW, SYSCALL, COPY or FILL, which use the registers a call would *) proc calls(o: PObj): BOOL; begin if o = nil then return false; end; return ((o^.cls = Sym.oProc) and (o^.inl = 0) and ((o^.ret = nil) or o^.ret^.calls)) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc)) or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy) or (o^.val = Sym.bFill))); end; (* Puts the busiest variables of the procedure, whose parameters are params and whose locals follow before, in registers: read ahead from its "begin" to its end, a use counts 1, in a loop 4, in two 16, in three or more 64, and a variable ADR, CAS, XCHG or XADD is given can't have one, since they need its address. A register costs a save and a restore, so a variable used less than three times keeps its place in memory. A slice takes two, its pointer's and its length's; a real one of the xmm registers, stored and loaded again around each call, as System V keeps none through one. In a procedure that names no procedure, so calls nothing, the parameters that arrive in the registers no call needs stay there (keepArrived), at no cost. *) proc chooseRegs(params, before: PObj); var isLoop: [256]BOOL; forAt: [256]INT; (* the for loop opened at each depth, or -1 *) i, k, kx, xmax, d, loops, best, need, nf, inLim: INT; o, q, qual: PObj; st: Scan.State; afterDot, wasSkipping, leaf: BOOL; begin numCand := 0; limitK := -1; nf := 0; inLim := -1; o := params; while o # nil do addCandidate(o); o := o^.next; end; o := firstLocal(before); while o # nil do addCandidate(o); o := o^.chain; end; if numCand = 0 then return; end; Scan.saveScan(st); wasSkipping := Scan.skipping; Scan.setSkipping(true); (* its slips are told when it is compiled *) d := 0; loops := 0; afterDot := false; leaf := true; qual := nil; repeat (* a name, or a module's name and one of its exports, that calls; once one has, the rest needn't be looked up *) if leaf then if (Scan.tok = Scan.sIdent) and not afterDot then q := Sym.lookup(Scan.id); if calls(q) then leaf := false; end; qual := nil; if (q # nil) and (q^.cls = Sym.oModule) then qual := q; end; elsif (Scan.tok = Scan.sIdent) and (qual # nil) then if (qual^.home = nil) or calls(Sym.find(qual^.home^.scope, Scan.id)) then leaf := false; end; qual := nil; end; end; match Scan.tok of | sBegin, sIf, sMatch, sWhile, sFor, sRepeat: if d < 255 then INC(d); isLoop[d] := (Scan.tok = Scan.sWhile) or (Scan.tok = Scan.sFor) or (Scan.tok = Scan.sRepeat); if isLoop[d] then INC(loops); end; forAt[d] := -1; if (Scan.tok = Scan.sFor) and (nf < MaxFors) then (* innermost until a for is found inside it; its limit constant until a name in it isn't one *) forAt[d] := nf; limitIn[nf] := false; i := d - 1; while (i > 0) and (forAt[i] < 0) do DEC(i); end; if i > 0 then limitIn[forAt[i]] := false; end; INC(nf); end; end; | sTo: if (d > 0) and (forAt[d] >= 0) then inLim := forAt[d]; end; | sDo, sBy: inLim := -1; | sEnd, sUntil: (* an end with no begin before it, in a body the parser will report, leaves d at 0 *) if d > 0 then if isLoop[d] then DEC(loops); end; DEC(d); end; | sIdent: if (Scan.id = nameAdr) or (Scan.id = nameCas) or (Scan.id = nameXchg) or (Scan.id = nameXadd) then Scan.next; if Scan.tok = Scan.sLparen then Scan.next; end; (* a hugely negative weight bars it from a register, whatever its uses add *) if (Scan.tok = Scan.sIdent) and (candidate(Scan.id) >= 0) then weight[candidate(Scan.id)] := -1073741824; end; elsif not afterDot and (candidate(Scan.id) >= 0) then k := candidate(Scan.id); if loops > 2 then INC(weight[k], 64); else INC(weight[k], 1 shl (2 * loops)); end; end; if (inLim >= 0) and not afterDot then q := Sym.lookup(Scan.id); if (q = nil) or (q^.cls # Sym.oConst) then limitIn[inLim] := true; end; end; | else: end; afterDot := Scan.tok = Scan.sPeriod; (* a name after a dot is a field, not a variable *) if (d > 0) and (Scan.tok # Scan.sEof) then Scan.next; end; until (d = 0) or (Scan.tok = Scan.sEof); Scan.restoreScan(st); Scan.setSkipping(wasSkipping); if leaf then o := params; while o # nil do k := candidate(o^.name); if (k >= 0) and (weight[k] >= 0) then Gen.keepArrived(o); end; (* a candidate whose ADR isn't taken *) o := o^.next; end; end; (* xmm4 to xmm7 too, where no call or real parameter needs them *) xmax := 4; if leaf then xmax := 8; o := params; i := 0; while o # nil do if (o^.typ^.form = Sym.fReal) and not o^.isVar then INC(i); end; o := o^.next; end; if i > 4 then xmax := 4; end; end; (* a slice's weight is shared by its two registers; one passed by its address has one *) for i := 0 to numCand - 1 do if (cand[i]^.typ^.form = Sym.fSlice) and not cand[i]^.ind then weight[i] := weight[i] div 2; end; end; (* the heaviest, each used at least three times a register, take the five registers in turn, while there are enough for each, and the reals xmax of the xmm registers of XVarRegs *) k := 0; kx := 0; repeat best := -1; for i := 0 to numCand - 1 do if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i; end; end; if best < 0 then break; end; (* a parameter passed by its address (ind) holds the address, one integer register whatever its type: a var real's address in an xmm register would be moved, by the prologue, into the integer register of the same number, another variable's *) need := 1; if (cand[best]^.typ^.form = Sym.fSlice) and not cand[best]^.ind then need := 2; end; if (cand[best]^.typ^.form = Sym.fReal) and not cand[best]^.ind then if kx < xmax then cand[best]^.reg := Gen.XVarRegs[kx]; INC(kx); else weight[best] := 0; end; elsif k + need <= 5 then cand[best]^.reg := Gen.VarRegs[k]; if need = 2 then cand[best]^.reg2 := Gen.VarRegs[k + 1]; end; INC(k, need); else weight[best] := 0; (* too big for what is left *) end; until false; for i := 0 to nf - 1 do if limitIn[i] and (k < 5) then limitK := k; end; end; end; (* The registers the variables of the procedure are in, as bits: bit i is VarRegs[i], bit XBit + i XVarRegs[i] *) proc regBits(o: PObj): INT; var m, k: INT; begin m := 0; for k := 0 to 4 do if (o^.reg = Gen.VarRegs[k]) or (o^.reg2 = Gen.VarRegs[k]) then m := m | (1 shl k); end; end; for k := 0 to LEN(Gen.XVarRegs) - 1 do if o^.reg = Gen.XVarRegs[k] then m := m | (1 shl (Gen.XBit + k)); end; end; return m; end; proc regsOf(params, before: PObj): INT; var o: PObj; m: INT; begin m := 0; o := params; while o # nil do m := m | regBits(o); o := o^.next; end; o := firstLocal(before); while o # nil do if o^.cls = Sym.oVar then m := m | regBits(o); end; o := o^.chain; end; if limitK >= 0 then m := m | (1 shl limitK); end; return m; end; (* Parameter o, kept where it arrived until a call or a copy wanted rdi or rsi, takes the registers of VarRegs that m, the taken ones, leaves, if it is used enough for them (chooseRegs's weight of 3) and there are enough; else it goes to memory. *) proc unkeep(o: PObj; m: INT); var k: INT; two: BOOL; begin two := o^.reg2 # 0; o^.reg := 0; o^.reg2 := 0; if weight[candidate(o^.name)] < 3 then return; end; for k := 0 to 4 do if (m & (1 shl k)) = 0 then if o^.reg = 0 then o^.reg := Gen.VarRegs[k]; elsif two and (o^.reg2 = 0) then o^.reg2 := Gen.VarRegs[k]; end; end; end; if two and (o^.reg2 = 0) then o^.reg := 0; end; end; (* Gives each local not in a register its slot in the frame, the slots the prologue zeroes, and then tells Gen which registers the variables are in. A variable in a register has no slot: it starts at zero in its register, and the trap report finds it there. *) proc placeLocals(params, before: PObj); var o: PObj; begin Gen.resetLocals(); o := firstLocal(before); while o # nil do if (o^.cls = Sym.oVar) and (o^.reg = 0) then o^.val := Gen.allocPerm(o^.typ^.size, o^.typ^.align); elsif (o^.cls = Sym.oVar) and (o^.reg >= Gen.XmmVar) then o^.val := Gen.allocPerm(8, 8); Gen.xHome(o^.reg, o^.val); end; o := o^.chain; end; o := params; while o # nil do if o^.reg >= Gen.XmmVar then Gen.xHome(o^.reg, o^.val); end; o := o^.next; end; Gen.useRegs(regsOf(params, before)); end; (* p, whose body is only return e, e read from at, is made in place of a call (inlined) if it returns a single value. *) proc onlyReturns(p: PObj; sig: PType; const at: Scan.State; calls: BOOL); begin if (sig^.base = nil) or not Sym.isScalar(sig^.base) then return; end; NEW(p^.ret, Heap.heap); if p^.ret = nil then Diag.fatal("out of memory"); end; p^.ret^.at := at; p^.ret^.path := Chunk.curFilePath(); p^.ret^.checks := Item.checks; p^.ret^.calls := calls; end; proc body(p: PObj; sig: PType; line: INT): ERROR; var q, params, last, o, before: PObj; old: PType; st, at: Scan.State; mark: Gen.CodeMark; doubts0, errs, calls0: INT; first, only: BOOL; begin try Sym.openScope(Sym.newScope()); params := nil; last := nil; q := sig^.fields; while q # nil do o := Sym.newObj(Sym.oVar, q^.name, q^.typ); o^.pos := q^.pos; o^.lev := 1; o^.isVar := q^.isVar; o^.isConst := q^.isConst; o^.param := true; if last = nil then params := o; else last^.next := o; end; last := o; q := q^.next; end; Item.beginProc(sig, params); deferSlot := 0; o := params; while o # nil do q := o^.next; try declare(o); o := q; end; curProc := p; before := Sym.curModule^.lastLocal; declarations(false); chooseRegs(params, before); placeLocals(params, before); (* the body, made again if a variable in a register must be in memory *) Scan.saveScan(st); Gen.markCode(mark); doubts0 := numDoubts; errs := Diag.errors; old := curResult; repeat Item.setDemoted(false); try expect(Scan.sBegin); curResult := sig^.base; Gen.enterBody(p^.chunk, line); stray := false; elseIf := false; deferSlot := 0; numFors := 0; first := Scan.tok = Scan.sReturn; Scan.saveScan(at); returnEnd := -1; sawTry := false; calls0 := numCalls; try statements(); only := first and (Scan.tok = Scan.sEnd) and (Scan.afterPrev().a = returnEnd) and not sawTry; p^.openEnd := (curResult # nil) and not ends; Chunk.noteLine(Scan.curLine(), Scan.curCol()); try expect(Scan.sEnd); if not (Item.demoted or Gen.lost) or (Diag.errors # errs) then break; end; (* a parameter kept where it arrived goes to a free register of VarRegs, or to memory: a call or a copy wanted its register *) if Gen.lost then o := params; while o # nil do if Gen.keptIn(o) then unkeep(o, regsOf(params, before)); end; o := o^.next; end; end; numDoubts := doubts0; Gen.redo(mark); placeLocals(params, before); Scan.restoreScan(st); until false; try Gen.endProc(p^.chunk, p^.openEnd); if only and (Sym.curModule^.lastLocal = before) and (Diag.errors = errs) then onlyReturns(p, sig, at, numCalls # calls0); end; (* its parameters and variables, for the trap report *) o := firstLocal(before); q := params; while q # nil do if varKind(q) # 0 then Chunk.noteVar(p^.chunk, q^.name, INT(q^.val), varKind(q)); end; q := q^.next; end; while o # nil do if (o^.cls = Sym.oVar) and (varKind(o) # 0) then Chunk.noteVar(p^.chunk, o^.name, INT(o^.val), varKind(o)); end; o := o^.chain; end; curResult := old; curProc := nil; limitK := -1; Sym.closeScope(); return nil; end; (* An integer constant in a code procedure, for the assembler: a whole expression, or a single factor where an expression would read on too far *) proc asmConst(whole: BOOL): (I64, ERROR); var x: TItem; begin if whole then return constExpr(); end; try factor(x, nil); if not Sym.isInt(x.typ) then return 0, Scan.markAt(x.pos, say1("expected an integer constant, found {}", Item.what(x))); end; return constValue(x); end; (* A procedure or a module variable named in a code procedure, for the assembler: it stands for its address (report section 8). Its chunk, the name read, bare or qualified as any other; -1, with nothing read, if the name at the scan is anything else, for the assembler to read as a constant. Being named, it is used, and the linker keeps it, as it keeps what a call reaches: by the fixup to its chunk. *) proc asmName: (INT, ERROR); var o: PObj; st: Scan.State; at: Diag.Pos; begin o := Sym.lookup(Scan.id); if (o = nil) or ((o^.cls # Sym.oModule) and (o^.cls # Sym.oProc) and (o^.cls # Sym.oVar)) then return -1, nil; end; Scan.saveScan(st); at := Scan.tokPos(); o := try qualIdent(); if (o^.cls = Sym.oProc) or ((o^.cls = Sym.oVar) and (o^.lev = 0)) then if o^.ext then return -1, Scan.markAt(Scan.spanFrom(at), say1("{} is C's: a code procedure names only raz's procedures and variables", nameOf(o^.name))); end; return o^.chunk, nil; end; Scan.restoreScan(st); return -1, nil; end; proc codeBody(p: PObj; sig: PType; at: Diag.Pos): ERROR; var q: PObj; ni, nf: INT; err: ERROR; begin ni := 0; nf := 0; q := sig^.fields; while q # nil do if not q^.isVar and (q^.typ^.form = Sym.fSlice) then INC(ni, 2); (* address, then length *) else if not q^.isVar and not Sym.isScalar(q^.typ) then return Scan.markAt(at, "a code procedure takes integers, BOOL, reals, pointers, procedures and slices"); end; if (q^.typ^.form = Sym.fReal) and not q^.isVar then INC(nf); else INC(ni); end; end; q := q^.next; end; if (ni > 6) or (nf > 8) or sig^.variadic then return Scan.markAt(at, "too many parameters for a code procedure"); end; if sig^.base # nil then if sig^.base^.tuple then q := sig^.base^.fields; if (sig^.base^.len # 2) or (q^.typ^.size # 8) or (q^.next^.typ^.size # 8) or not Sym.isScalar(q^.typ) or not Sym.isScalar(q^.next^.typ) or (q^.typ^.form = Sym.fReal) or (q^.next^.typ^.form = Sym.fReal) then return Scan.markAt(at, "a code procedure returns two results only in rax and rdx"); end; elsif not Sym.isScalar(sig^.base) and (sig^.base^.form # Sym.fSlice) then return Scan.markAt(at, "a code procedure returns a single value"); end; end; (* in a scope of its own, so that a name it uses and isn't declared, declared then as poison, is gone at its end, as a procedure's locals are, and doesn't clash with a declaration after it *) try Sym.openScope(Sym.newScope()); err := Gen.codeProc(p^.chunk, asmConst, asmName); Sym.closeScope; if err # nil then return err; end; (* a square root of one real, of its own kind, is made in place *) q := sig^.fields; if (q # nil) and (q^.next = nil) and not q^.isVar and (q^.typ^.form = Sym.fReal) and (sig^.base # nil) and Sym.same(sig^.base, q^.typ) then p^.inl := Gen.oneRealOp(p^.chunk, q^.typ^.size = 4); end; return expect(Scan.sEnd); end; proc procDecl: ERROR; var n, line: INT; exp: BOOL; sig: PType; p: PObj; at: Diag.Pos; begin try instancesFor(false); line := Scan.curLine(); Scan.next; try identDef(n, exp, at); sig := try signature(); try expect(Scan.sSemicolon); p := Sym.find(Sym.topScope(), n); if (p # nil) and (p^.cls = Sym.oProc) and p^.fwd then if not Sym.same(p^.typ, sig) then Diag.error(at, say1("{} doesn't have the signature it was declared forward with", nameOf(n))); Diag.note(p^.pos, "the signature is here"); return Scan.Failed; end; if Scan.tok = Scan.sForward then return Scan.mark(say1("{} is declared forward twice", nameOf(n))); end; p^.exported := p^.exported or exp; else p := Sym.newObj(Sym.oProc, n, sig); p^.pos := at; p^.exported := exp; if (Sym.curModule^.name = nameRt) and not checkMode and (n = nameFail) then p^.chunk := Gen.rtFail; else p^.chunk := Chunk.newChunk(Chunk.secText, Sym.curModule^.sname, n); end; try declare(p); end; if Scan.tok = Scan.sForward then Scan.next; p^.fwd := true; return nil; end; p^.fwd := false; if Scan.tok = Scan.sCode then _ := Item.needSystem(Scan.tokPos(), "a code procedure"); (* said, and the declaration read on *) Scan.next; p^.isCode := true; if checkMode then (* a file of another target's, read for its interface: its code is in that machine's assembly, which this one's can't read; ! is a symbol there, as A64's assembler has it *) Scan.setBang(true); while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do Scan.next; end; Scan.setBang(false); try expect(Scan.sEnd); else try codeBody(p, sig, at); end; elsif checkMode then try skipBody(); else try body(p, sig, line); end; if Scan.tok = Scan.sIdent then if Scan.id = Sym.curModule^.name then return endMissing(Scan.tokPos(), say1("{} has no \"end\": this one closes the module", nameOf(n))); elsif Scan.id # n then return endMissing(Scan.tokPos(), say2("expected {}, found {}", nameOf(n), nameOf(Scan.id))); end; Scan.next; end; return nil; end; (* An extern procedure or variable: C's, reached through the quadword that holds its address. *) proc externDecl: ERROR; var n: INT; exp: BOOL; o, q: PObj; t: PType; at: Diag.Pos; begin _ := Item.needSystem(Scan.tokPos(), "extern"); (* said, and the declaration read on *) Scan.next; Sym.curModule^.usesC := true; if Scan.tok = Scan.sProc then Scan.next; try identDef(n, exp, at); willDeclare(n); inExtern := true; t := try signature(); inExtern := false; q := t^.fields; while q # nil do if q^.isVar or not Sym.isScalar(q^.typ) then return Scan.markAt(at, "C takes integers, BOOL, reals, pointers and procedures"); end; q := q^.next; end; if (t^.base # nil) and not Sym.isScalar(t^.base) then return Scan.markAt(at, "C returns a single value"); end; o := Sym.newObj(Sym.oProc, n, t); else if Scan.tok # Scan.sVar then return expected("\"proc\" or \"var\""); end; Scan.next; try identDef(n, exp, at); willDeclare(n); try expect(Scan.sColon); declExported := exp; t := try parseType(); declExported := false; o := Sym.newObj(Sym.oVar, n, t); end; o^.pos := at; o^.exported := exp; o^.ext := true; o^.chunk := Chunk.externChunk(n); return declare(o); end; (* An array or record constant: a literal of constants, which become the bytes of a read-only chunk, as a string constant's do (report section 4). An address in it, of a string, an array constant, a procedure or an ERROR, waits in fixes until the chunk is made, then is a fixup the linker patches. The bytes are the compiler's own machine's, little-endian, as the target's are. *) var constBytes: ^U8; fixes: [MaxFixes]record off, chunk: INT; end; numFixes: INT; proc constFix(off, chunk: INT): ERROR; begin if numFixes >= MaxFixes then return Scan.mark(tooMany(MaxFixes, "addresses in one constant")); end; fixes[numFixes].off := off; fixes[numFixes].chunk := chunk; INC(numFixes); return nil; end; (* The n bytes of v, at off in the constant: little-endian, as the machine is *) proc putBytes(off, n: INT; v: I64); var src: ^U8; begin src := ^U8(SYSTEM.ADR(v)); COPY(constBytes[off..off + n], src[0..n]); end; (* The number of elements of the array literal at the scanner, at its "[": read ahead, counting the commas outside any inner brackets, then read again from the "[" (saveScan, restoreScan). Its slips are told when it is read again. *) proc literalLength: INT; var st: Scan.State; depth, n: INT; wasSkipping: BOOL; begin Scan.saveScan(st); wasSkipping := Scan.skipping; Scan.setSkipping(true); Scan.next; n := 0; if Scan.tok # Scan.sRbrak then n := 1; end; depth := 0; while (Scan.tok # Scan.sEof) and ((depth > 0) or (Scan.tok # Scan.sRbrak)) do match Scan.tok of | sLparen, sLbrak, sLbrace: INC(depth); | sRparen, sRbrak, sRbrace: DEC(depth); | sComma: if depth = 0 then INC(n); end; | else: end; Scan.next; end; Scan.setSkipping(wasSkipping); Scan.restoreScan(st); return n; end; (* A constant typed []T, or an array literal for a slice part of one, is an array constant whose length is its literal's (report, 4): t, the slice type, becomes [n]T, as if n had been written, with n counted ahead (literalLength). With no element, [], it stays []T: the empty slice, kept as 16 zero bytes. *) proc literalArray(t: PType): (PType, ERROR); var a: PType; n: INT; begin if Scan.tok # Scan.sLbrak then return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t))); end; n := literalLength(); if n = 0 then return t, nil; end; a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align); a^.len := I64(n); a^.base := t^.base; return a, nil; end; proc constPart(t: PType; off: INT): ERROR; forward; proc constChunk(t: PType; n: INT): (INT, ERROR); forward; (* A variant's case built from constants, at offset off of the constant (report section 4): its number, then each field where the case lays it out, as constructCase builds one in a program; the rest stays zero. The case is read here, by its name, since expression would make the code that builds one. *) proc constCase(t: PType; off: INT): ERROR; var c, f: PObj; at: Diag.Pos; begin at := Scan.tokPos(); c := try qualIdent(); if (c^.cls # Sym.oCase) or (c^.typ # t) then return Scan.markAt(Scan.spanFrom(at), say1("expected a case of {}", Sym.typeName(t))); end; putBytes(off, 8, c^.val); f := c^.fields; if Scan.tok = Scan.sLparen then Scan.next; while f # nil do try constPart(f^.typ, off + INT(f^.val)); f := f^.next; if f # nil then try expect(Scan.sComma); end; end; return expect(Scan.sRparen); elsif f # nil then return Scan.markAt(Scan.spanFrom(at), say1("{} needs its fields", nameOf(c^.name))); end; return nil; end; (* The part of type t at offset off of the constant *) proc constPart(t: PType; off: INT): ERROR; var e: TItem; f: PObj; k, c: INT; r: F32; start: Diag.Pos; src: ^U8; st, fx: INT; a: PType; begin start := Scan.tokPos(); if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) then a := try literalArray(t); if a = t then (* [], the empty slice (report, 4): its bytes stay zero, ptr nil and len 0 *) Scan.next; return expect(Scan.sRbrak); end; (* an array literal (report, 4) is an array constant of its own, in a chunk of its own, made before this one is; the slice points at it, as it would at a named one: const Rows: [][]INT = [[1, 2], [3]]; is laid out as const R0: [2]INT = [1, 2]; R1: [1]INT = [3]; const Rows: [][]INT = [R0, R1]; *) c := try constChunk(a, -1); try constFix(off, c); putBytes(off + 8, 8, a^.len); return nil; end; if (t^.form = Sym.fArray) and (Scan.tok = Scan.sLbrak) then Scan.next; k := 0; if Scan.tok # Scan.sRbrak then repeat if I64(k) < t^.len then try constPart(t^.base, off + k * INT(t^.base^.size)); else try constPart(t^.base, off); end; INC(k); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; end; try expect(Scan.sRbrak); if I64(k) # t^.len then return Scan.markAt(Scan.spanFrom(start), Diag.say("expected {} elements, found {}", [Fmt.int(t^.len), Fmt.int(k)])); end; return nil; end; if (t^.form = Sym.fRecord) and not t^.tuple and (Scan.tok = Scan.sLbrace) then Scan.next; if Scan.tok # Scan.sRbrace then repeat if Scan.tok # Scan.sIdent then return expected("a field"); end; f := try fieldOf(t); Scan.next; try expect(Scan.sColon); try constPart(f^.typ, off + INT(f^.val)); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; end; return expect(Scan.sRbrace); end; if (t^.form = Sym.fVariant) and (Scan.tok = Scan.sIdent) then return constCase(t, off); end; if Scan.tok = Scan.sLbrak then (* an array literal where neither an array nor a slice is *) return Scan.mark(say1("expected {}, found an array literal", Sym.typeName(t))); end; try expression(e, t); if (t^.form = Sym.fSlice) and not t^.rdonly then return Scan.markHelp(e.pos, say1("a constant's slice is a []const, not {}", Sym.typeName(t)), "a constant can't be changed: declare the slice []const"); end; if (e.mode = Item.mStr) and ((t^.form = Sym.fSlice) or (t^.form = Sym.fArray)) and (t^.base = Sym.tU8) then if t^.form = Sym.fSlice then try constFix(off, e.chunk); putBytes(off + 8, 8, e.val); elsif e.val > t^.len then return Scan.markAt(e.pos, say1("the string is longer than {}", Sym.typeName(t))); else st := Chunk.chunks[e.chunk].start; COPY(constBytes[off..off + INT(e.val)], Chunk.rodata[st..st + INT(e.val)]); end; elsif t^.form = Sym.fSlice then (* the whole constant, here or another module's, not a part of it, which has a type of its own: the element's, or a slice's *) if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ # e.obj^.typ) or not (e.typ^.form in {Sym.fArray, Sym.fSlice}) or not Sym.same(e.typ^.base, t^.base) then return Scan.markAt(e.pos, say1("a constant slice is a string, an array constant or literal of its elements, or [], found {}", Item.what(e))); end; if e.typ^.form = Sym.fArray then (* else an empty slice constant, whose bytes stay zero *) try constFix(off, e.chunk); putBytes(off + 8, 8, e.typ^.len); end; elsif t^.form = Sym.fVariant then return Scan.markAt(e.pos, say1("expected a case of {}", Sym.typeName(t))); elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then (* a literal, or the whole of a constant of the type: its bytes and addresses are copied *) if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ # e.obj^.typ) or not Sym.same(e.typ, t) then return Scan.markAt(e.pos, say1("expected a literal of {}", Sym.typeName(t))); end; st := Chunk.chunks[e.chunk].start; COPY(constBytes[off..off + INT(t^.size)], Chunk.rodata[st..st + INT(t^.size)]); fx := Chunk.chunks[e.chunk].firstFix; for k := fx to fx + Chunk.chunks[e.chunk].numFix - 1 do try constFix(off + Chunk.dataFixups[k].at, Chunk.dataFixups[k].chunk); end; else try Item.coerce(e, t); if (e.mode = Item.mProc) and not e.obj^.ext then try constFix(off, e.obj^.chunk); elsif e.mode # Item.mConst then return Scan.markAt(e.pos, say1("expected a constant, found {}", Item.what(e))); elsif (t^.form = Sym.fError) and (e.val > 0) then try constFix(off, INT(e.val) - 1); elsif (t^.form = Sym.fPtr) and (e.val # 0) then return Scan.markAt(e.pos, "a constant pointer is nil"); elsif (t^.form = Sym.fReal) and (t^.size = 4) then r := F32(Item.realOf(e)); src := ^U8(SYSTEM.ADR(r)); COPY(constBytes[off..off + 4], src[0..4]); else putBytes(off, INT(t^.size), e.val); end; end; return nil; end; (* The chunk of n, a constant of type t whose literal is next, or of an array literal in a constant's slice part, with n -1. That one is made while the constant around it is still being read: its bytes are its own, and the constant's are put back after; its addresses go on in fixes after the constant's, from base, and are taken off again. *) proc constChunk(t: PType; n: INT): (INT, ERROR); var c, i, size, base: INT; outer: ^U8; begin outer := constBytes; base := numFixes; size := INT(t^.size); constBytes := Scan.grab(size + 8); for i := 0 to size + 7 do constBytes[i] := 0; end; try constPart(t, 0); if n < 0 then c := Chunk.newChunk(Chunk.secRodata, -1, -1); else c := Chunk.newChunk(Chunk.secRodata, Sym.curModule^.sname, n); end; Chunk.setData(c, constBytes, size, INT(t^.align)); for i := base to numFixes - 1 do Chunk.addDataFixup(c, fixes[i].off, fixes[i].chunk, 0); end; numFixes := base; constBytes := outer; return c, nil; end; (* The chunk of n, a constant of type t whose literal is next *) proc aggregateConst(t: PType; n: INT): (INT, ERROR); begin numFixes := 0; (* none left over from a constant an error broke off *) return constChunk(t, n); end; proc constDecl(top: BOOL): ERROR; var n, c: INT; exp: BOOL; x: TItem; o: PObj; t: PType; start, at: Diag.Pos; begin try identDef(n, exp, at); willDeclare(n); t := nil; if Scan.tok = Scan.sColon then Scan.next; start := Scan.tokPos(); t := try parseType(); if (t # Sym.tError) and not ((t^.form = Sym.fArray) or (t^.form = Sym.fSlice) or ((t^.form = Sym.fRecord) and not t^.tuple) or (t^.form = Sym.fVariant)) then if t^.form = Sym.fSet then return Scan.markAt(Scan.spanFrom(start), "a set constant takes its type from its names: write it without one"); end; return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array, a record or a variant constant has a type"); end; end; try expect(Scan.sEql); if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t); end; if (t # nil) and (t^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice, Sym.fVariant}) then o := Sym.newObj(Sym.oConst, n, t); o^.chunk := try aggregateConst(t, n); o^.pos := at; o^.exported := exp and top; try declare(o); return expect(Scan.sSemicolon); end; try expression(x, nil); if x.typ^.form = Sym.fVariant then return Scan.markHelp(x.pos, "a variant constant needs its type", say2("write const {}: {} = ...", nameOf(n), Sym.typeName(x.typ))); end; o := Sym.newObj(Sym.oConst, n, x.typ); o^.pos := at; o^.exported := exp and top; if (t = Sym.tError) and (x.mode = Item.mConst) and (x.typ = Sym.tError) and (x.val > 0) then (* another ERROR constant (report, 5.8): that same error, its chunk, so that the two compare equal; only a string makes one *) o^.typ := Sym.tError; o^.val := x.val; elsif t = Sym.tError then o^.typ := Sym.tError; c := try Item.errorChunk(x, Sym.curModule^.sname, n); o^.val := c + 1; elsif x.mode = Item.mStr then o^.val := x.val; o^.chunk := x.chunk; else o^.val := try constValue(x); end; try declare(o); return expect(Scan.sSemicolon); end; proc typeDecl(top: BOOL): ERROR; var n: INT; exp: BOOL; o: PObj; t: PType; at: Diag.Pos; begin try identDef(n, exp, at); willDeclare(n); try expect(Scan.sEql); declExported := exp and top; inTypes := true; t := try parseType(); o := Sym.newObj(Sym.oType, n, t); o^.pos := at; o^.exported := exp and top; if t^.obj = nil then t^.obj := o; end; try declare(o); return expect(Scan.sSemicolon); end; proc varDecl(top: BOOL): ERROR; var i, k: INT; o: PObj; t: PType; names: [MaxNames]INT; exps: [MaxNames]BOOL; poss: [MaxNames]Diag.Pos; begin k := 0; repeat if k >= MaxNames then return Scan.mark(tooMany(MaxNames, "names in one declaration")); end; try identDef(names[k], exps[k], poss[k]); willDeclare(names[k]); INC(k); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; try expect(Scan.sColon); declExported := top and exps[0]; t := try parseType(); declExported := false; for i := 0 to k - 1 do o := Sym.newObj(Sym.oVar, names[i], t); o^.pos := poss[i]; o^.exported := exps[i] and top; o^.isVar := false; if top then o^.lev := 0; o^.chunk := Chunk.newChunk(Chunk.secBss, Sym.curModule^.sname, names[i]); Chunk.setBss(o^.chunk, INT(t^.size), INT(t^.align)); else o^.lev := 1; (* its slot comes from placeLocals, if it needs one *) end; try declare(o); end; return expect(Scan.sSemicolon); end; (* After a procedure had an error: its name is poison if it wasn't declared, and reading goes on after the "end Name;" that closes it, or at the next procedure if that is missing *) proc skipProc; var st: Scan.State; begin Scan.next; if (Scan.tok = Scan.sIdent) and (Sym.find(Sym.topScope(), Scan.id) = nil) then poison(Scan.id); end; repeat repeat Scan.next; until Scan.atLineStart() or (Scan.tok = Scan.sEof); match Scan.tok of | sProc, sExtern, sEof: return; | sEnd: Scan.saveScan(st); Scan.next; if Scan.tok = Scan.sIdent then Scan.next; end; if Scan.tok = Scan.sSemicolon then Scan.next; return; end; Scan.restoreScan(st); return; | else: end; until false; end; (* What one declaration, of the kind k, reads *) proc declarationOf(k: Scan.Sym; top: BOOL): ERROR; begin match k of | sConst: return constDecl(top); | sType: return typeDecl(top); | sVar: return varDecl(top); | sProc: if not top then return Scan.mark("procedures don't nest"); end; try procDecl(); return expect(Scan.sSemicolon); | sExtern: if not top then return Scan.mark("extern is only for the top of a module"); end; try externDecl(); return expect(Scan.sSemicolon); | else: return nil; end; end; (* One declaration, as a recovery point: an error is reported, the declaration skipped, and reading goes on after it *) proc declaration(k: Scan.Sym; top: BOOL); var r: Recover; begin guard(r); numDeclaring := 0; if declarationOf(k, top) # nil then restore(r); if k = Scan.sProc then skipProc(); else skipDeclaration(); end; end; unguard(r); end; proc declarations(top: BOOL); var k: Scan.Sym; begin repeat k := Scan.tok; match k of | sConst, sVar: Scan.next; while Scan.tok = Scan.sIdent do declaration(k, top); end; | sType: Scan.next; numLater := 0; while Scan.tok = Scan.sIdent do declaration(Scan.sType, top); end; inTypes := false; resolveLaters(); | sProc, sExtern: declaration(k, top); | else: return; end; until false; end; (* Modules *) (* Whether m is module n with these arguments. *) proc isModule(m: PModule; n: INT; args: Sym.Args): BOOL; var i: INT; begin if (m^.name # n) or (m^.args.n # args.n) then return false; end; for i := 0 to args.n - 1 do if (m^.args.o[i] # args.o[i]) or not Sym.same(m^.args.t[i], args.t[i]) then return false; end; end; return true; end; proc findModule(n: INT; args: Sym.Args): PModule; var m: PModule; begin m := firstModule; while (m # nil) and not isModule(m, n, args) do m := m^.next; end; return m; end; (* The arguments of an instance: types, or procedures named. *) proc parseArgs(var args: Sym.Args; mname: []const U8): ERROR; var o: PObj; start: Diag.Pos; begin Scan.next; repeat if args.n >= LEN(args.o) then return Scan.mark(tooMany(LEN(args.o), say1("type arguments to {}", mname))); end; args.o[args.n] := nil; start := Scan.tokPos(); if Scan.tok = Scan.sIdent then o := try qualIdent(); if o^.cls = Sym.oProc then args.o[args.n] := o; elsif o^.cls # Sym.oType then return Scan.markAt(Scan.spanFrom(start), say1("{} isn't a type or a procedure", nameOf(o^.name))); end; args.t[args.n] := o^.typ; else args.t[args.n] := try parseType(); end; args.pos[args.n] := Scan.spanFrom(start); INC(args.n); if Scan.tok # Scan.sComma then break; end; Scan.next; until false; return expect(Scan.sRparen); end; (* Past an instance's arguments, to be read when it is made *) proc skipArgs: ERROR; var p: INT; begin p := 0; repeat if Scan.tok = Scan.sLparen then INC(p); elsif Scan.tok = Scan.sRparen then DEC(p); elsif (Scan.tok = Scan.sSemicolon) or (Scan.tok = Scan.sEof) then return expected("\")\""); end; Scan.next; until p = 0; return nil; end; (* A generic module's parameters, bound to the instance's arguments: a type parameter names its type, a procedure parameter its procedure. The instance's name is the module's with its arguments, List(I64). *) proc moduleParams(m: PModule): ERROR; var i: INT; o, a: PObj; sig: PType; s: Buf.Buf; at: Diag.Pos; text: []U8; begin Buf.init(s, Heap.heap); i := 0; Buf.str(s, nameOf(m^.name)); Buf.add(s, '('); Scan.next; repeat if Scan.tok # Scan.sIdent then return expected("a name"); end; if i >= m^.args.n then return Scan.markAt(m^.args.at, say2("{} needs a type for {}", nameOf(m^.name), nameOf(Scan.id))); end; if m^.args.o[i] # nil then return Scan.markAt(m^.args.pos[i], say2("{} needs a type for {}", nameOf(m^.name), nameOf(Scan.id))); end; o := Sym.newObj(Sym.oType, Scan.id, m^.args.t[i]); o^.used := true; (* a parameter of the module, not a declaration *) try declare(o); Buf.str(s, Sym.typeName(m^.args.t[i])); Buf.add(s, ','); INC(i); Scan.next; if Scan.tok # Scan.sComma then break; end; Scan.next; until false; while Scan.tok = Scan.sSemicolon do Scan.next; if Scan.tok # Scan.sIdent then return expected("a name"); end; at := Scan.tokPos(); o := Sym.newObj(Sym.oProc, Scan.id, nil); Scan.next; try expect(Scan.sColon); try expect(Scan.sProc); sig := try signature(); if i >= m^.args.n then return Scan.markAt(m^.args.at, say2("{} needs a procedure for {}", nameOf(m^.name), nameOf(o^.name))); end; a := m^.args.o[i]; if a = nil then return Scan.markAt(m^.args.pos[i], say2("{} needs a procedure for {}", nameOf(m^.name), nameOf(o^.name))); end; if not Sym.same(a^.typ, sig) then Diag.error(m^.args.pos[i], say2("{} doesn't have the signature of {}", nameOf(a^.name), nameOf(o^.name))); Diag.note(at, "the signature is here"); Diag.finish(); end; o^.typ := a^.typ; o^.chunk := a^.chunk; o^.isCode := a^.isCode; o^.ext := a^.ext; o^.used := true; try declare(o); Buf.str(s, nameOf(a^.home^.name)); Buf.add(s, '.'); Buf.str(s, nameOf(a^.name)); Buf.add(s, ','); INC(i); end; try expect(Scan.sRparen); if i # m^.args.n then return Scan.markAt(m^.args.at, say1("too many type arguments for {}", nameOf(m^.name))); end; text := Buf.bytes(s); text[LEN(text) - 1] := ')'; m^.sname := internName(text); Buf.done(s); return nil; end; (* Warns of o if it is never used, or a variable only assigned. What is exported may be used elsewhere, and a name that starts with _ says it is meant to go unused. Writing a var parameter, a match's name for a field or C's variable is what it is for. *) proc warn(o: PObj); var s, t: []const U8; begin s := nameOf(o^.name); if (o^.cls = Sym.oProc) and o^.openEnd then Diag.warning(o^.pos, say1("{} may reach its end without return, and then returns zero", s)); end; if o^.exported or (s[0] = '_') then return; end; if o^.used and (o^.read or (o^.cls # Sym.oVar) or (o^.isVar and not o^.isConst) or o^.ext) then return; end; match o^.cls of | oConst: t := say1("constant {}", s); | oType: t := say1("type {}", s); | oVar: if o^.param then t := say1("parameter {}", s); else t := say1("variable {}", s); end; | oProc: t := say1("procedure {}", s); | oModule: t := say1("module {}", s); | else: return; end; if o^.used then Diag.warning(o^.pos, say1("{} is assigned but never read", t)); elsif o^.cls = Sym.oModule then Diag.warning(o^.pos, say1("{} is imported but never used", t)); else Diag.warning(o^.pos, say1("{} is never used", t)); end; if not o^.used and o^.param then Diag.help(say1("if it must be there, call it _{}", s)); elsif not o^.used and o^.ind then (* not a parameter, so a name a match arm gives *) Diag.help(say1("an arm names all the fields of a case or none: call one it doesn't read _{}", s)); end; end; (* Warns of the call at p, which drops its n results *) proc dropped(p: Diag.Pos; n: INT); var s, blanks: []const U8; i: INT; begin s := Diag.source(p); i := 0; while (i < LEN(s)) and (s[i] # '(') do INC(i); end; if i < LEN(s) then s := s[0..i]; end; blanks := "_"; for i := 2 to n do blanks := say1("{}, _", blanks); end; if n = 1 then Diag.warning(p, say1("the result of {} is dropped", s)); else Diag.warning(p, say1("the results of {} are dropped", s)); end; Diag.help(say2("if that is meant, write {} := {}(...)", blanks, s)); end; (* The warnings of module m, by where each declaration is: its own declarations and its procedures' are two lists, each in order, and firstDoubt on are the calls that drop results and the other doubts. The instances of a generic module share its text, and so its warnings: only the first is warned of. *) proc warnings(m: PModule; firstDoubt: INT); var a, b: PObj; n: PModule; i: INT; begin n := firstModule; while (n # nil) and (n^.name # m^.name) do n := n^.next; end; if n # nil then return; end; a := m^.first; b := m^.locals; i := firstDoubt; while (a # nil) or (b # nil) or (i < numDoubts) do if (i < numDoubts) and ((a = nil) or (doubts[i].at.a < a^.pos.a)) and ((b = nil) or (doubts[i].at.a < b^.pos.a)) then if LEN(doubts[i].msg) = 0 then dropped(doubts[i].at, doubts[i].n); else Diag.warning(doubts[i].at, doubts[i].msg); Diag.help(doubts[i].hint); end; INC(i); elsif (b = nil) or ((a # nil) and (a^.pos.a < b^.pos.a)) then warn(a); a := a^.chain; else warn(b); b := b^.chain; end; end; end; (* Modules being compiled, not yet finished: for finding a cycle. *) var pending: [MaxPending]PModule; numPending: INT; proc exists(path: []const U8): BOOL; var info: Sys.Info; err: INT; begin info, err := Sys.info(path); return (err = 0) and info.file; end; (* Which file path is, as its device and inode: two names of one file have the same. Zero for a file that can't be asked about. *) proc identity(path: []const U8): (INT, INT); var i: Sys.Info; err: INT; (* Sys.info follows a symbolic link, and is the same on every target *) begin i, err := Sys.info(path); if err # 0 then return 0, 0; end; return INT(i.dev), INT(i.ino); end; (* s, in memory that is never freed, for what must outlast the buffer it is in *) proc keep(s: []const U8): []const U8; var p: ^U8; begin if LEN(s) = 0 then return ""; end; p := Scan.grab(LEN(s)); COPY(p[0..LEN(s)], s); return p[0..LEN(s)]; end; proc importModule(name: INT; args: Sym.Args): (PModule, ERROR); forward; (* Compiles module n from path, an instance if args are given, or only reads its interface if checkOnly; returns it. An import calls this in the middle of reading another file, so it is re-entrant: it saves the scanner's state, the open scopes, the current module and file and whether checks are on, and puts them back at the end. *) proc compileFile(path: []const U8; n: INT; args: Sym.Args; checkOnly: BOOL): (PModule, ERROR); var st: Scan.State; saved: [Sym.MaxDepth]INT; d: INT; oldMod: PModule; oldCheck, oldChecks: BOOL; m: PModule; lib: ^Sym.Lib; start: Diag.Pos; alias, mname, line, oldPath: INT; o: PObj; bodyChunk: INT; iargs: Sym.Args; generic, atName: BOOL; argsAt: Scan.State; why: []const U8; firstDoubt: INT; begin firstDoubt := numDoubts; Scan.saveScan(st); d := Sym.saveScopes(saved[0..Sym.MaxDepth]); oldMod := Sym.curModule; oldCheck := checkMode; oldPath := Chunk.curFilePath(); oldChecks := Item.checks; Item.setChecks(true); Chunk.setCurFile(internName(path)); why := Scan.open(path); if LEN(why) # 0 then Diag.fatal(say2("can't read {}: {}", path, why)); end; Diag.setOrigin(Scan.tokPos().f, originNote); originNote := ""; NEW(m, Heap.heap); m^.name := n; m^.sname := n; m^.args := args; m^.scope := Sym.newScope(); m^.path := path; m^.body := -1; m^.checkOnly := checkOnly; Sym.setCurModule(m); checkMode := checkOnly; if not checkOnly then if numPending >= MaxPending then return nil, Scan.mark(tooMany(MaxPending, "imports nested deep")); end; pending[numPending] := m; INC(numPending); end; try Sym.openScope(m^.scope); Scan.next; try expect(Scan.sModule); if (Scan.tok # Scan.sIdent) or (Scan.id # n) then return nil, Scan.mark(say2("expected module {}, found {}", nameOf(n), Scan.found())); end; m^.at := Scan.tokPos(); Scan.next; (* the module first pending is the one named on the command line, which nothing imports, so no arguments come with it *) if (Scan.tok = Scan.sLparen) and not checkOnly and (numPending = 1) then return nil, Scan.mark(say1("{} is generic: it is compiled where a module imports it", nameOf(n))); elsif Scan.tok = Scan.sLparen then try moduleParams(m); elsif args.n > 0 then return nil, Scan.markAt(args.at, say1("{} isn't generic", nameOf(n))); end; try expect(Scan.sSemicolon); while Scan.tok = Scan.sImport do Scan.next; repeat if Scan.tok # Scan.sIdent then return nil, expected("a module name"); end; start := Scan.tokPos(); alias := Scan.id; atName := define and (alias = Scan.atCursor); (* raz -q def on a module's name here *) if atName then alias := internName(Scan.word); end; mname := alias; iargs.n := 0; generic := false; Scan.next; if Scan.tok = Scan.sBecomes then Scan.next; if Scan.tok # Scan.sIdent then return nil, expected("a module name"); end; mname := Scan.id; Scan.next; if Scan.tok = Scan.sLparen then generic := true; Scan.saveScan(argsAt); try skipArgs(); end; end; iargs.at := Scan.spanFrom(start); o := Sym.newObj(Sym.oModule, alias, nil); o^.pos := start; try declare(o); if mname = Sym.system^.name then (* no file: the language's own, which a grep must find *) if (alias # mname) or generic then return nil, Scan.markHelp(iargs.at, "SYSTEM is imported only as itself", "every module that reaches memory through SYSTEM says so by that name: write import SYSTEM"); end; o^.home := Sym.system; m^.system := true; named(o); (* not warned of when only its features are used *) elsif not generic then o^.home := try importModule(mname, iargs); addImport(m, o^.home); if atName then answer(m^.scope); end; else o^.home := nil; (* until the instance is made, at its first use *) if numDeferred >= MaxDeferred then return nil, Scan.mark(tooMany(MaxDeferred, "generic imports")); end; deferred[numDeferred].o := o; deferred[numDeferred].home := m; deferred[numDeferred].mname := mname; deferred[numDeferred].args := argsAt; deferred[numDeferred].at := iargs.at; INC(numDeferred); end; if Scan.tok # Scan.sComma then break; end; Scan.next; until false; try expect(Scan.sSemicolon); end; while Scan.tok = Scan.sLink do Scan.next; if (Scan.tok = Scan.sIdent) and (Scan.id = nameStatic) then m^.linkStatic := true; elsif Scan.tok = Scan.sString then _ := Item.needSystem(Scan.tokPos(), "a link of a library"); (* said, and the declaration read on *) NEW(lib, Heap.heap); lib^.name := internName(Scan.strBytes()); lib^.next := m^.libs; m^.libs := lib; m^.usesC := true; else return nil, expected("a library or \"static\""); end; Scan.next; try expect(Scan.sSemicolon); end; (* check off;: the module's index and slice checks left out; neither word is reserved *) if (Scan.tok = Scan.sIdent) and (Scan.id = nameCheck) then _ := Item.needSystem(Scan.tokPos(), "check off"); (* said, and the declaration read on *) Scan.next; if (Scan.tok # Scan.sIdent) or (Scan.id # nameOff) then return nil, expected("\"off\""); end; Scan.next; try expect(Scan.sSemicolon); Item.setChecks(false); end; declarations(true); if Scan.tok = Scan.sBegin then if checkMode then try skipBody(); else try instancesFor(true); line := Scan.curLine(); Scan.next; bodyChunk := Chunk.newChunk(Chunk.secText, m^.sname, nameBody); m^.body := bodyChunk; Item.beginProc(tBodySig, nil); deferSlot := 0; curResult := nil; Gen.enterBody(bodyChunk, line); stray := false; elseIf := false; try statements(); Chunk.noteLine(Scan.curLine(), Scan.curCol()); try expect(Scan.sEnd); try Gen.endProc(bodyChunk, false); end; else try expect(Scan.sEnd); end; if (Scan.tok # Scan.sIdent) or (Scan.id # n) then return nil, endMissing(Scan.missingPos(), say2("expected {}, found {}", nameOf(n), Scan.found())); end; Scan.next; if Scan.tok # Scan.sPeriod then return nil, expected("\".\""); end; o := m^.first; while o # nil do if (o^.cls = Sym.oProc) and o^.fwd then Diag.error(Scan.tokPos(), say1("{} is declared forward but never defined", nameOf(o^.name))); end; o := o^.chain; end; if (Diag.errors = 0) and not checkOnly then warnings(m, firstDoubt); end; numDoubts := firstDoubt; if Diag.errors > 0 then Diag.finish(); end; (* a module with errors stops the compiler after it *) Sym.closeScope(); m^.done := true; if not checkOnly then DEC(numPending); if lastModule = nil then firstModule := m; else lastModule^.next := m; end; lastModule := m; end; Sym.setCurModule(oldMod); checkMode := oldCheck; Chunk.setCurFile(oldPath); Item.setChecks(oldChecks); Sym.restoreScopes(d, saved[0..Sym.MaxDepth]); Scan.restoreScan(st); return m, nil; end; (* The interface check: b must export just what a does. *) proc compareIface(a, b: PModule); var o, q: PObj; begin (* an enumeration's names are its type's, which the type's fingerprint has, and the name alone may be another's *) o := a^.first; while o # nil do if o^.exported and not o^.ofEnum then q := Sym.find(b^.scope, o^.name); if (q = nil) or not q^.exported or (Sym.fingerprint(o) # Sym.fingerprint(q)) then Diag.fatal(Diag.say("{}: {} isn't exported as in {}", [Fmt.str(b^.path), Fmt.str(nameOf(o^.name)), Fmt.str(a^.path)])); end; end; o := o^.chain; end; q := b^.first; while q # nil do o := Sym.find(a^.scope, q^.name); if q^.exported and not q^.ofEnum and ((o = nil) or not o^.exported) then Diag.fatal(Diag.say("{}: {} isn't exported by {}", [Fmt.str(b^.path), Fmt.str(nameOf(q^.name)), Fmt.str(a^.path)])); end; q := q^.chain; end; end; (* A directory and a module name, keeping the root's slash and not adding another when the directory already ends in one: "lib/" gives "lib/Sys.raz", not "lib//Sys.raz", and "/" gives "/Sys.raz". *) proc modulePath(dir, file: []const U8): []const U8; begin if (LEN(dir) > 0) and (dir[LEN(dir) - 1] = '/') then return say2("{}{}", dir, file); end; return say2("{}/{}", dir, file); end; (* Reads every other file of module n in dir, and checks its interface against m, compiled from the file named chosen. *) proc checkSiblings(dir, chosen: []const U8; n: INT; m: PModule; args: Sym.Args): ERROR; var dp: Sys.Dir; e, prefix: []const U8; i, numOthers: INT; more: BOOL; sib: PModule; others: [32][]const U8; begin prefix := say1("{}.", nameOf(n)); numOthers := 0; if Sys.openDir(dp, dir) # 0 then return nil; end; repeat e, more := Sys.nextEntry(dp); if not more then break; end; if (LEN(e) > 4) and Str.hasPrefix(e, prefix) and (e[LEN(e) - 4..LEN(e)] = ".raz") and (e # chosen) and (numOthers < 32) then others[numOthers] := keep(e); INC(numOthers); end; until false; Sys.closeDir(dp); for i := 0 to numOthers - 1 do sib := try compileFile(modulePath(dir, others[i]), n, args, true); compareIface(m, sib); end; return nil; end; (* The compiler's version as a library's RAZ-VERSION would say it: "0.21" for "0.21.0-5-g38d29a8"; a name with fewer dots stays whole *) proc majorMinor(v: []const U8): []const U8; var i, dots: INT; begin dots := 0; for i := 0 to LEN(v) - 1 do if v[i] = '.' then INC(dots); if dots = 2 then return v[0..i]; end; end; end; return v; end; (* The line told with the first error in a module found in dir, an -I or RAZ_PATH directory: if dir holds a file RAZ-VERSION, the raz its library was written for. The file is only read; a library with none, or a module of the program's own directory or of lib, has no line. *) proc libraryNote(dir: []const U8): []const U8; var fd, n, i: INT; b: [32]U8; begin if (dir = mainDir) or (dir = libDir) then return ""; end; fd := Sys.openRead(say1("{}/RAZ-VERSION", dir)); if fd < 0 then return ""; end; n := Sys.read(fd, b[0..32]); Sys.close(fd); i := 0; while (i < n) and (b[i] # 10) and (b[i] # 32) do INC(i); end; if i = 0 then return ""; end; return say2("a library written for raz {}; this is raz {}", b[0..i], majorMinor(thisVersion)); end; (* The directories searched for modules: main, -I, user libraries, lib *) proc searchDir(i: INT): []const U8; begin if i = -1 then return mainDir; end; if i < numInc then return incDirs[i]; end; i := i - numInc; if i < LEN(userDirs) then return userDirs[i]; end; return libDir; end; (* The file of module n in dir that suffix j says: Name.OS-ARCH.raz for a file of this target alone (Name.linux-x86_64.raz, say), then Name.OS.raz, Name.ARCH.raz, and Name.raz *) proc fileName(n, j: INT): []const U8; begin if j = 0 then return Diag.say("{}.{}-{}.raz", [Fmt.str(nameOf(n)), Fmt.str(targetOs), Fmt.str(targetArch)]); elsif j = 1 then return Diag.say("{}.{}.raz", [Fmt.str(nameOf(n)), Fmt.str(targetOs)]); elsif j = 2 then return Diag.say("{}.{}.raz", [Fmt.str(nameOf(n)), Fmt.str(targetArch)]); end; return Diag.say("{}.raz", [Fmt.str(nameOf(n))]); end; proc moduleFile(n: INT): ([]const U8, []const U8, BOOL); var i, j: INT; dir, f: []const U8; begin for i := -1 to numInc + LEN(userDirs) do dir := searchDir(i); for j := 0 to 3 do f := fileName(n, j); if exists(modulePath(dir, f)) then return dir, f, true; end; end; end; return "", "", false; end; (* A program has one module of each name: module n, found as dir/f, must have no files in a later directory, unless dir/f is a link to one of them, which chooses it (report, section 3) *) proc checkOneModule(n: INT; dir, f: []const U8; at: Diag.Pos): ERROR; var i, j, dev, ino, mineDev, mineIno: INT; past, chosen: BOOL; g, files, others: []const U8; begin mineDev, mineIno := identity(modulePath(dir, f)); others := ""; past := false; for i := -1 to numInc + LEN(userDirs) do if past then files := ""; chosen := false; for j := 0 to 3 do g := modulePath(searchDir(i), fileName(n, j)); if exists(g) then files := say2("{} and {}", files, Diag.shownPath(g)); dev, ino := identity(g); chosen := chosen or ((dev = mineDev) and (ino = mineIno)); end; end; if not chosen then others := say2("{}{}", others, files); end; end; past := past or (searchDir(i) = dir); end; if LEN(others) = 0 then return nil; end; Diag.error(at, say3("module {} is in {}{}", nameOf(n), Diag.shownPath(modulePath(dir, f)), others)); Diag.help("a program has one module of each name; a symbolic link to one of these files chooses it"); return Scan.Failed; end; proc importModule(name: INT; args: Sym.Args): (PModule, ERROR); var m: PModule; i: INT; dir, f, where: []const U8; found: BOOL; begin m := findModule(name, args); if m # nil then return m, nil; end; for i := 0 to numPending - 1 do if isModule(pending[i], name, args) then return nil, Scan.markAt(args.at, say1("{} imports itself", nameOf(name))); end; end; dir, f, found := moduleFile(name); if not found then Diag.error(args.at, say1("can't find module {}", nameOf(name))); where := Diag.shownPath(searchDir(-1)); for i := 0 to numInc + LEN(userDirs) do where := say2("{}, {}", where, Diag.shownPath(searchDir(i))); end; Diag.help(say2("looked for {}.raz in {}", nameOf(name), where)); return nil, Scan.Failed; end; try checkOneModule(name, dir, f, args.at); originNote := libraryNote(dir); m := try compileFile(modulePath(dir, f), name, args, false); try checkSiblings(dir, f, name, m, args); return m, nil; end; proc addImport(m, im: PModule); var imp: ^Sym.Import; begin NEW(imp, Heap.heap); imp^.home := im; imp^.next := m^.imports; m^.imports := imp; end; (* Makes the instance that deferred[i] imports: its arguments are read where the import wrote them, and must be declared by now. Compiling the instance comes in the middle of a declaration, so what that declaration has under way is kept aside meanwhile. *) proc makeInstance(i: INT; at: Diag.Pos): ERROR; var d: Deferred; st: Scan.State; args: Sym.Args; types, exported, ext, wasQuiet: BOOL; nf, nd: INT; lt: [MaxLater]Later; decl: [MaxNames]INT; begin d := deferred[i]; DEC(numDeferred); deferred[i] := deferred[numDeferred]; Scan.saveScan(st); Scan.restoreScan(d.args); args.n := 0; making := true; useAt := at; try parseArgs(args, nameOf(d.mname)); making := false; Scan.restoreScan(st); args.at := d.at; types := inTypes; exported := declExported; ext := inExtern; wasQuiet := Diag.quiet; nf := numLater; lt := laters; nd := numDeclaring; decl := declaring; d.o^.home := try importModule(d.mname, args); inTypes := types; declExported := exported; inExtern := ext; Diag.setQuiet(wasQuiet); numLater := nf; laters := lt; numDeclaring := nd; declaring := decl; addImport(Sym.curModule, d.o^.home); return nil; end; proc firstUse(o: PObj; at: Diag.Pos): ERROR; var i: INT; begin for i := 0 to numDeferred - 1 do if deferred[i].o = o then return makeInstance(i, at); end; end; return Scan.Failed; (* its arguments had an error, already reported *) end; (* An instance can't be compiled in the middle of another procedure's code, so before a procedure's code begins, from its "proc" on, or the module body's, from its "begin" to the end of the file, this reads ahead for the names of imports not yet made, and makes those. *) proc instancesFor(wholeRest: BOOL): ERROR; var st: Scan.State; d, i: INT; begun: BOOL; begin if checkMode then return nil; end; i := numDeferred - 1; while (i >= 0) and (deferred[i].home # Sym.curModule) do DEC(i); end; if i < 0 then return nil; end; (* The procedure ends at the "end" that closes its first "begin": d counts the words still open that "end" or "until" closes, and begun says that begin was met, so that a record's end in its declarations doesn't stop it. A forward or code procedure has no raz body to read, so it stops at that word. The body reads to the end of the file, as wholeRest says. *) Scan.saveScan(st); d := 0; begun := false; repeat match Scan.tok of | sIdent: for i := numDeferred - 1 to 0 by -1 do if (deferred[i].home = Sym.curModule) and (deferred[i].o^.name = Scan.id) then try makeInstance(i, Scan.tokPos()); end; end; | sBegin: begun := begun or (d = 0); INC(d); | sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: INC(d); | sEnd, sUntil: DEC(d); | else: end; if not wholeRest and (d = 0) and (begun or (Scan.tok = Scan.sForward) or (Scan.tok = Scan.sCode)) then break; end; Scan.next; until Scan.tok = Scan.sEof; Scan.restoreScan(st); return nil; end; (* The main module, from the file at path, and Rt, which every program needs, first *) proc compileMain*(path: []const U8): (PModule, ERROR); var base: []const U8; i, n: INT; none: Sym.Args; m: PModule; begin none.n := 0; i := LEN(path); while (i > 0) and (path[i - 1] # '/') do DEC(i); end; (* the directory is the path less its last "/"; for "/Main.raz" that would be "", so keep the root *) if i = 0 then mainDir := "."; elsif i = 1 then mainDir := "/"; else mainDir := path[0..i - 1]; end; base := path[i..LEN(path)]; i := 0; while (i < LEN(base)) and (base[i] # '.') do INC(i); end; base := base[0..i]; if LEN(base) = 0 then Diag.fatal(say1("no module in {}", path)); end; n := internName(base); none.at := Scan.tokPos(); if n # nameRt then _ := try importModule(nameRt, none); end; m := findModule(n, none); if m # nil then return m, nil; end; return compileFile(path, n, none, false); end; begin limitK := -1; nameRt := internName("Rt"); nameFail := internName("fail"); nameAdr := internName("ADR"); nameCas := internName("CAS"); nameXchg := internName("XCHG"); nameXadd := internName("XADD"); nameBody := internName("body"); nameStatic := internName("static"); nameCheck := internName("check"); nameOff := internName("off"); tArgv := newPtr(newPtr(Sym.tU8, false), false); tBodySig := Sym.newType(Sym.fProc, 8, 8); end Parse.