| 1 | (* Sym: types, objects and scopes. |
| 2 | |
| 3 | Every name the program declares becomes an object (Obj): a constant, |
| 4 | type, variable, procedure, module, record field, variant case or |
| 5 | built-in. Every type is a Type. Objects are found through one hash |
| 6 | table keyed by scope and name, so a lookup costs the same however |
| 7 | big a module grows. A scope is just a number: the universe is 0, |
| 8 | and each module, procedure, record and match arm gets a fresh one. |
| 9 | |
| 10 | Depends on: Scan, for names and errors; Diag, for positions. |
| 11 | Leaves out: code. Where a variable lives is decided here (frame |
| 12 | offsets, chunk numbers), but nothing is emitted. |
| 13 | |
| 14 | Exported variables can't be set from outside, so setCurModule is |
| 15 | how the parser says which module it is in. A name that holds a |
| 16 | reserved word of raz is spelled otherwise: a record's `module` is |
| 17 | its `home`, an object's `link` its `chain`. *) |
| 18 | module Sym; |
| 19 | import Mem, Heap, Buf, Fmt, Diag, Scan; |
| 20 | import SYSTEM; |
| 21 | |
| 22 | type |
| 23 | Form* = (fNone, fBool, fInt, fReal, fConst, fNil, fStr, fPtr, fArray, |
| 24 | fSlice, fRecord, fVariant, fProc, fEnum, fSet, fError); |
| 25 | (* fConst is the type of an untyped integer constant; fStr of a string |
| 26 | literal, whose length the literal carries. F32 and F64 are fReal, |
| 27 | told apart by size, and so is tRConst, an untyped real constant. *) |
| 28 | |
| 29 | Class* = (oConst, oType, oVar, oProc, oModule, oField, oCase, oBuiltin); |
| 30 | |
| 31 | Type* = record |
| 32 | form*: Form; |
| 33 | size*, align*: I64; |
| 34 | signed*: BOOL; (* fInt *) |
| 35 | vol*: BOOL; (* fPtr: ^VOLATILE *) |
| 36 | rdonly*: BOOL; (* fSlice: []const, its elements not written through it *) |
| 37 | packd*: BOOL; (* fRecord *) |
| 38 | variadic*: BOOL; (* fProc: ends in ... *) |
| 39 | tuple*: BOOL; (* fRecord: the results of a procedure that has several *) |
| 40 | base*: ^Type; (* fPtr, fArray, fSlice: what it holds; fProc: result *) |
| 41 | len*: I64; (* fArray: elements; fProc: parameters; fVariant: cases; a tuple: results *) |
| 42 | fields*: ^Obj; (* fRecord: fields; fVariant: cases; fProc: parameters *) |
| 43 | scope*: INT; (* fRecord: the scope of its fields *) |
| 44 | obj*: ^Obj; (* the declaration that names it, or nil *) |
| 45 | home*: ^Module; (* the module that built it *) |
| 46 | end; |
| 47 | |
| 48 | (* A procedure whose body is only return e, made in place of a call: |
| 49 | e is read again from where it begins (Parse's inlined) *) |
| 50 | Inline* = record |
| 51 | at*: Scan.State; (* at the return, before e *) |
| 52 | path*: INT; (* its file, as Chunk knows it *) |
| 53 | checks*: BOOL; (* its module's checks are made *) |
| 54 | calls*: BOOL; (* e calls a procedure *) |
| 55 | busy*: BOOL; (* being read in place now: not again inside, so |
| 56 | that a recursion ends *) |
| 57 | end; |
| 58 | |
| 59 | Obj* = record |
| 60 | cls*: Class; |
| 61 | name*: INT; |
| 62 | exported*: BOOL; |
| 63 | typ*: ^Type; |
| 64 | next*: ^Obj; (* the next in its list: scope, fields, parameters *) |
| 65 | scope*: INT; (* where it is declared *) |
| 66 | pos*: Diag.Pos; (* where its name is declared, for messages *) |
| 67 | val*: I64; (* oConst: value; oBuiltin: which; oCase: tag; |
| 68 | oField: offset; oVar: frame offset *) |
| 69 | chunk*: INT; (* global oVar, oProc, string oConst: its chunk *) |
| 70 | lev*: INT; (* oVar: 0 for a module variable, 1 for a local *) |
| 71 | ind*: BOOL; (* oVar: the frame slot holds the variable's address *) |
| 72 | off*: I64; (* oVar with ind: added to that address *) |
| 73 | isVar*: BOOL; (* a var parameter, or a const one: passed by address *) |
| 74 | isConst*: BOOL; (* a const parameter, or a name a match gives a field |
| 75 | of something only read: never changed *) |
| 76 | param*: BOOL; (* oVar: a parameter *) |
| 77 | used*: BOOL; (* named somewhere but in its own body *) |
| 78 | read*: BOOL; (* oVar: named other than as what := assigns *) |
| 79 | fwd*: BOOL; (* oProc: declared forward, no body yet *) |
| 80 | isCode*: BOOL; (* oProc: a code procedure *) |
| 81 | inl*: INT; (* oProc: a code procedure that is a square root of |
| 82 | xmm0, made in place of a call (Gen.oneRealOp); |
| 83 | 0 if not *) |
| 84 | openEnd*: BOOL; (* oProc: has results, and may reach its end *) |
| 85 | ret*: ^Inline; (* oProc: its body is only return e, made in place |
| 86 | of a call; nil if not *) |
| 87 | reg*: INT; (* oVar: the register it lives in, or 0 if in memory; |
| 88 | for a slice, the one its pointer is in *) |
| 89 | reg2*: INT; (* oVar: a slice in registers: the one its length is in *) |
| 90 | ext*: BOOL; (* oProc, oVar: C's, reached through its chunk, which |
| 91 | holds its address *) |
| 92 | fields*: ^Obj; (* oCase: its fields *) |
| 93 | home*: ^Module; (* oModule: the module; others: where declared *) |
| 94 | chain*: ^Obj; (* the next declaration of its module, top-level or |
| 95 | local *) |
| 96 | ofEnum*: BOOL; (* oConst: a name of its enumeration, which may share |
| 97 | its scope with another declaration of the name |
| 98 | (report section 5.7) *) |
| 99 | twin*: ^Obj; (* an enumeration's name in its scope: another |
| 100 | enumeration's of the same name, which makes the |
| 101 | name alone an error; nil if none *) |
| 102 | end; |
| 103 | |
| 104 | (* The arguments of a generic module's instance: a type, or a |
| 105 | procedure (o) and its type. *) |
| 106 | Args* = record |
| 107 | n*: INT; |
| 108 | t*: [8]^Type; |
| 109 | o*: [8]^Obj; |
| 110 | pos*: [8]Diag.Pos; (* where each argument is, for messages *) |
| 111 | at*: Diag.Pos; (* where the import is *) |
| 112 | end; |
| 113 | |
| 114 | Import* = record |
| 115 | home*: ^Module; |
| 116 | next*: ^Import; |
| 117 | end; |
| 118 | |
| 119 | (* A library a module links, by its name, as interned *) |
| 120 | Lib* = record |
| 121 | name*: INT; |
| 122 | next*: ^Lib; |
| 123 | end; |
| 124 | |
| 125 | Module* = record |
| 126 | name*: INT; |
| 127 | sname*: INT; (* the name of its symbols: List(I64) for an instance *) |
| 128 | args*: Args; |
| 129 | scope*: INT; |
| 130 | path*: []const U8; |
| 131 | at*: Diag.Pos; (* its name in its "module" line, for raz -q def *) |
| 132 | first*, last*: ^Obj; (* its top-level declarations, in order *) |
| 133 | locals*, lastLocal*: ^Obj; (* its procedures' declarations, in order *) |
| 134 | imports*: ^Import; |
| 135 | body*: INT; (* the chunk of its body, or -1 *) |
| 136 | done*: BOOL; (* compiled to the end *) |
| 137 | checkOnly*: BOOL; (* read only to check another file's interface *) |
| 138 | used*: BOOL; (* imported, through others, by the main module *) |
| 139 | usesC*: BOOL; (* has an extern, or links a library *) |
| 140 | linkStatic*: BOOL; (* says link static *) |
| 141 | system*: BOOL; (* says import SYSTEM (report 3) *) |
| 142 | libs*: ^Lib; (* the libraries it links *) |
| 143 | next*: ^Module; (* modules in the order they were finished *) |
| 144 | end; |
| 145 | |
| 146 | const |
| 147 | (* built-in procedures *) |
| 148 | bSize* = 0; bAdr* = 1; bLen* = 2; bNew* = 3; bMsg* = 4; bSyscall* = 5; |
| 149 | bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; |
| 150 | bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17; |
| 151 | bMin* = 18; bMax* = 19; bClz* = 20; bCtz* = 21; bPopcount* = 22; |
| 152 | bOffset* = 23; bBytes* = 24; bFill* = 25; bAbs* = 26; |
| 153 | |
| 154 | MaxDepth* = 64; (* scopes open at once *) |
| 155 | |
| 156 | var |
| 157 | tI8*, tI16*, tI32*, tI64*, tU8*, tU16*, tU32*, tU64*: ^Type; |
| 158 | tInt*, tUInt*: ^Type; (* INT and UINT, as big as an address: 8 bytes here *) |
| 159 | tBool*, tF32*, tReal*, tRConst*, tError*, tConst*, tNil*, tStr*, tNone*, tPtrU8*, tBytes*, tConstBytes*: ^Type; |
| 160 | tPoison*: ^Type; (* of a name reported as not declared *) |
| 161 | curModule*: ^Module; |
| 162 | (* SYSTEM, the module the language gives, of what reaches memory |
| 163 | other than through a variable (report 3): no file, its scope the |
| 164 | built-ins that may be written SYSTEM.ADR and the like *) |
| 165 | system*: ^Module; |
| 166 | |
| 167 | scopes: [MaxDepth]INT; |
| 168 | depth: INT; |
| 169 | nextScope: INT; |
| 170 | hashTab: ^^Obj; |
| 171 | hashMask, hashCount: INT; |
| 172 | |
| 173 | proc setCurModule*(m: ^Module); |
| 174 | begin |
| 175 | curModule := m; |
| 176 | end; |
| 177 | |
| 178 | proc newType*(form: Form; size, align: I64): ^Type; |
| 179 | var t: ^Type; |
| 180 | begin |
| 181 | NEW(t, Heap.heap); |
| 182 | if t = nil then Diag.fatal("out of memory"); end; |
| 183 | t^.form := form; |
| 184 | t^.size := size; |
| 185 | t^.align := align; |
| 186 | t^.home := curModule; |
| 187 | return t; |
| 188 | end; |
| 189 | |
| 190 | proc newObj*(cls: Class; name: INT; typ: ^Type): ^Obj; |
| 191 | var o: ^Obj; |
| 192 | begin |
| 193 | NEW(o, Heap.heap); |
| 194 | if o = nil then Diag.fatal("out of memory"); end; |
| 195 | o^.cls := cls; |
| 196 | o^.name := name; |
| 197 | o^.typ := typ; |
| 198 | o^.chunk := -1; |
| 199 | o^.home := curModule; |
| 200 | o^.pos := Scan.tokPos(); |
| 201 | return o; |
| 202 | end; |
| 203 | |
| 204 | (* Scopes *) |
| 205 | |
| 206 | proc newScope*: INT; |
| 207 | begin |
| 208 | INC(nextScope); |
| 209 | return nextScope; |
| 210 | end; |
| 211 | |
| 212 | proc openScope*(s: INT): ERROR; |
| 213 | begin |
| 214 | if depth = MaxDepth then |
| 215 | return Scan.mark(Diag.say("scopes nested more than {} deep", [Fmt.int(MaxDepth)])); |
| 216 | end; |
| 217 | scopes[depth] := s; |
| 218 | INC(depth); |
| 219 | return nil; |
| 220 | end; |
| 221 | |
| 222 | proc closeScope*; |
| 223 | begin |
| 224 | DEC(depth); |
| 225 | end; |
| 226 | |
| 227 | (* An imported module is compiled in the middle of another, and must |
| 228 | see only the universe and itself. The scopes open are kept in saved, |
| 229 | and their number is the result. *) |
| 230 | proc saveScopes*(saved: []INT): INT; |
| 231 | var i, d: INT; |
| 232 | begin |
| 233 | d := depth; |
| 234 | for i := 0 to depth - 1 do saved[i] := scopes[i]; end; |
| 235 | depth := 1; |
| 236 | return d; |
| 237 | end; |
| 238 | |
| 239 | proc restoreScopes*(d: INT; saved: []INT); |
| 240 | var i: INT; |
| 241 | begin |
| 242 | depth := d; |
| 243 | for i := 0 to depth - 1 do scopes[i] := saved[i]; end; |
| 244 | end; |
| 245 | |
| 246 | proc scopeDepth*: INT; |
| 247 | begin |
| 248 | return depth; |
| 249 | end; |
| 250 | |
| 251 | (* Closes the scopes opened since there were d, after an error *) |
| 252 | proc cutScopes*(d: INT); |
| 253 | begin |
| 254 | depth := d; |
| 255 | end; |
| 256 | |
| 257 | proc topScope*: INT; |
| 258 | begin |
| 259 | return scopes[depth - 1]; |
| 260 | end; |
| 261 | |
| 262 | (* Where the search for name in scope s starts in the hash table: each |
| 263 | number is multiplied by an odd constant near 2^32 or 2^16 over the |
| 264 | golden ratio (Knuth's multiplicative hashing), which spreads the low |
| 265 | bits the mask keeps. *) |
| 266 | proc slot(s, name: INT): INT; |
| 267 | begin |
| 268 | return INT(((U32(s & 0FFFFFFFFH) * 2654435761) xor (U32(name & 0FFFFFFFFH) * 40503)) & U32(hashMask)); |
| 269 | end; |
| 270 | |
| 271 | (* A table of n slots, all empty *) |
| 272 | proc emptyTable(n: INT): ^^Obj; |
| 273 | var t: ^^Obj; i: INT; |
| 274 | begin |
| 275 | t := ^^Obj(Scan.grab(n * SIZE(^Obj))); |
| 276 | for i := 0 to n - 1 do t[i] := nil; end; |
| 277 | return t; |
| 278 | end; |
| 279 | |
| 280 | (* Doubles the table, which insert keeps at most half full so that the |
| 281 | linear probing stays short, and puts every entry in its new slot. *) |
| 282 | proc rehash; |
| 283 | var old: ^^Obj; n, i, j: INT; |
| 284 | begin |
| 285 | old := hashTab; |
| 286 | n := hashMask + 1; |
| 287 | hashMask := n * 2 - 1; |
| 288 | hashTab := emptyTable(n * 2); |
| 289 | for i := 0 to n - 1 do |
| 290 | if old[i] # nil then |
| 291 | j := slot(old[i]^.scope, old[i]^.name); |
| 292 | while hashTab[j] # nil do j := (j + 1) & hashMask; end; |
| 293 | hashTab[j] := old[i]; |
| 294 | end; |
| 295 | end; |
| 296 | Mem.free(Heap.heap, ^U8(old)); |
| 297 | end; |
| 298 | |
| 299 | (* Declares obj in the scope s, or reports a name declared twice there. |
| 300 | A name of an enumeration may share its scope with another declaration |
| 301 | of the name (report section 5.7): it is always reached through its |
| 302 | type, Elem.bold, so the scope keeps the other declaration, which the |
| 303 | name alone then means; of two enumerations' names, it keeps the first, |
| 304 | with the second as its twin, and the name alone is an error. *) |
| 305 | proc insert*(obj: ^Obj; s: INT): ERROR; |
| 306 | var i: INT; o: ^Obj; |
| 307 | begin |
| 308 | i := slot(s, obj^.name); |
| 309 | o := hashTab[i]; |
| 310 | while o # nil do |
| 311 | if (o^.scope = s) and (o^.name = obj^.name) then |
| 312 | obj^.scope := s; |
| 313 | if obj^.ofEnum then |
| 314 | if o^.ofEnum and (o^.twin = nil) then o^.twin := obj; end; |
| 315 | return nil; |
| 316 | elsif o^.ofEnum then |
| 317 | hashTab[i] := obj; (* the name alone is obj *) |
| 318 | return nil; |
| 319 | end; |
| 320 | Diag.error(obj^.pos, Diag.say("{} is declared twice", [Fmt.str(Scan.nameOf(obj^.name))])); |
| 321 | Diag.note(o^.pos, "the first one is here"); |
| 322 | return Scan.Failed; |
| 323 | end; |
| 324 | i := (i + 1) & hashMask; |
| 325 | o := hashTab[i]; |
| 326 | end; |
| 327 | obj^.scope := s; |
| 328 | hashTab[i] := obj; |
| 329 | INC(hashCount); |
| 330 | if hashCount * 2 > hashMask then rehash; end; |
| 331 | return nil; |
| 332 | end; |
| 333 | |
| 334 | proc find*(s, name: INT): ^Obj; |
| 335 | var i: INT; o: ^Obj; |
| 336 | begin |
| 337 | i := slot(s, name); |
| 338 | o := hashTab[i]; |
| 339 | while o # nil do |
| 340 | if (o^.scope = s) and (o^.name = name) then return o; end; |
| 341 | i := (i + 1) & hashMask; |
| 342 | o := hashTab[i]; |
| 343 | end; |
| 344 | return nil; |
| 345 | end; |
| 346 | |
| 347 | proc lookup*(name: INT): ^Obj; |
| 348 | var i: INT; o: ^Obj; |
| 349 | begin |
| 350 | for i := depth - 1 to 0 by -1 do |
| 351 | o := find(scopes[i], name); |
| 352 | if o # nil then return o; end; |
| 353 | end; |
| 354 | return nil; |
| 355 | end; |
| 356 | |
| 357 | proc min(a, b: INT): INT; |
| 358 | begin |
| 359 | if a < b then return a; end; |
| 360 | return b; |
| 361 | end; |
| 362 | |
| 363 | (* c, in capitals if it is a letter *) |
| 364 | proc up(c: U8): U8; |
| 365 | begin |
| 366 | if (c >= 97) and (c <= 122) then return c - 32; end; |
| 367 | return c; |
| 368 | end; |
| 369 | |
| 370 | (* How far apart two names are: the edits, a character put in, taken out, |
| 371 | changed or swapped with the next, that make one the other, ignoring |
| 372 | case. *) |
| 373 | proc distance(a, b: []const U8): INT; |
| 374 | var d: [Scan.MaxIdent + 2][Scan.MaxIdent + 2]INT; i, j, c: INT; |
| 375 | begin |
| 376 | for i := 0 to LEN(a) do d[i][0] := i; end; |
| 377 | for j := 0 to LEN(b) do d[0][j] := j; end; |
| 378 | for i := 1 to LEN(a) do |
| 379 | for j := 1 to LEN(b) do |
| 380 | c := 1; |
| 381 | if up(a[i - 1]) = up(b[j - 1]) then c := 0; end; |
| 382 | d[i][j] := min(min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + c); |
| 383 | if (i > 1) and (j > 1) and (up(a[i - 1]) = up(b[j - 2])) |
| 384 | and (up(a[i - 2]) = up(b[j - 1])) then |
| 385 | d[i][j] := min(d[i][j], d[i - 2][j - 2] + 1); |
| 386 | end; |
| 387 | end; |
| 388 | end; |
| 389 | return d[LEN(a)][LEN(b)]; |
| 390 | end; |
| 391 | |
| 392 | (* Whether o is a name that can be reached from here: in scope s, if |
| 393 | it is exported or of the module being compiled; or, if s < 0, in a |
| 394 | scope open *) |
| 395 | proc reaches(o: ^Obj; s: INT): BOOL; |
| 396 | var j: INT; |
| 397 | begin |
| 398 | if (o = nil) or (o^.name < 0) or (o^.typ = tPoison) then return false; end; |
| 399 | if s >= 0 then return (o^.scope = s) and (o^.exported or (o^.home = curModule)); end; |
| 400 | for j := 0 to depth - 1 do |
| 401 | if o^.scope = scopes[j] then return true; end; |
| 402 | end; |
| 403 | return false; |
| 404 | end; |
| 405 | |
| 406 | (* The name spelled most like name, for "did you mean": in scope s, if |
| 407 | it can be reached from here, or in the scopes in reach if s < 0; nil |
| 408 | if none is within an edit for every three characters. Only after an |
| 409 | error, so it looks at every object. *) |
| 410 | proc closest*(name, s: INT): ^Obj; |
| 411 | var i, d, best: INT; o, found: ^Obj; n: []const U8; |
| 412 | begin |
| 413 | n := Scan.nameOf(name); |
| 414 | best := LEN(n) div 3 + 1; |
| 415 | found := nil; |
| 416 | for i := 0 to hashMask do |
| 417 | o := hashTab[i]; |
| 418 | if reaches(o, s) and (o^.name # name) then |
| 419 | d := distance(n, Scan.nameOf(o^.name)); |
| 420 | if d < best then best := d; found := o; end; |
| 421 | end; |
| 422 | end; |
| 423 | return found; |
| 424 | end; |
| 425 | |
| 426 | proc typeName*(t: ^Type): []const U8; forward; |
| 427 | |
| 428 | (* What kind of declaration o is, as raz -q words says it *) |
| 429 | proc kindOf(o: ^Obj): []const U8; |
| 430 | begin |
| 431 | match o^.cls of |
| 432 | | oConst, oCase: return "const"; |
| 433 | | oType: return "type"; |
| 434 | | oVar: return "var"; |
| 435 | | oProc: return "proc"; |
| 436 | | oModule: return "module"; |
| 437 | | oField: return "field"; |
| 438 | | oBuiltin: return "builtin"; |
| 439 | end; |
| 440 | end; |
| 441 | |
| 442 | (* What o is, as raz -q says it: its kind, its name and its type, |
| 443 | split by tabs, written to out *) |
| 444 | proc describe*(o: ^Obj; var out: Buf.Buf); |
| 445 | var t: []const U8; |
| 446 | begin |
| 447 | t := ""; |
| 448 | if (o^.typ # nil) and (o^.cls # oModule) and (o^.cls # oBuiltin) then t := typeName(o^.typ); end; |
| 449 | Fmt.write(out, "{}\t{}\t{}", [Fmt.str(kindOf(o)), Fmt.str(Scan.nameOf(o^.name)), Fmt.str(t)]); |
| 450 | end; |
| 451 | |
| 452 | (* raz -q words: a row in out for each name in reach that starts with |
| 453 | prefix, in scope s as closest has it: its kind, the name and its |
| 454 | type, split by tabs. A name another declaration hides is left out. *) |
| 455 | proc offer*(s: INT; prefix: []const U8; var out: Buf.Buf); |
| 456 | var i: INT; o: ^Obj; n: []const U8; |
| 457 | begin |
| 458 | for i := 0 to hashMask do |
| 459 | o := hashTab[i]; |
| 460 | if reaches(o, s) and ((s >= 0) or (lookup(o^.name) = o)) then |
| 461 | n := Scan.nameOf(o^.name); |
| 462 | if (LEN(n) >= LEN(prefix)) and (n[0..LEN(prefix)] = prefix) then |
| 463 | describe(o, out); |
| 464 | Buf.add(out, '\n'); |
| 465 | end; |
| 466 | end; |
| 467 | end; |
| 468 | end; |
| 469 | |
| 470 | (* Types *) |
| 471 | |
| 472 | proc alignUp*(x, a: I64): I64; |
| 473 | begin |
| 474 | return (x + a - 1) div a * a; |
| 475 | end; |
| 476 | |
| 477 | proc isInt*(t: ^Type): BOOL; |
| 478 | begin |
| 479 | return t^.form in {fInt, fConst}; |
| 480 | end; |
| 481 | |
| 482 | proc isScalar*(t: ^Type): BOOL; |
| 483 | begin |
| 484 | return t^.form in {fBool, fInt, fReal, fConst, fNil, fPtr, fProc, fEnum, fSet, fError}; |
| 485 | end; |
| 486 | |
| 487 | proc isAggregate*(t: ^Type): BOOL; |
| 488 | begin |
| 489 | return t^.form in {fArray, fSlice, fRecord, fVariant}; |
| 490 | end; |
| 491 | |
| 492 | (* Whether t is an array, or a record or variant with one among its |
| 493 | fields, at any depth: a value a slice may be cut from (report 8, an |
| 494 | array a call returns) *) |
| 495 | proc holdsArray*(t: ^Type): BOOL; |
| 496 | var f, c: ^Obj; |
| 497 | begin |
| 498 | if t^.form = fArray then return true; end; |
| 499 | if t^.form = fRecord then |
| 500 | f := t^.fields; |
| 501 | while f # nil do |
| 502 | if holdsArray(f^.typ) then return true; end; |
| 503 | f := f^.next; |
| 504 | end; |
| 505 | elsif t^.form = fVariant then |
| 506 | c := t^.fields; |
| 507 | while c # nil do |
| 508 | f := c^.fields; |
| 509 | while f # nil do |
| 510 | if holdsArray(f^.typ) then return true; end; |
| 511 | f := f^.next; |
| 512 | end; |
| 513 | c := c^.next; |
| 514 | end; |
| 515 | end; |
| 516 | return false; |
| 517 | end; |
| 518 | |
| 519 | (* Whether any bytes at all are a value of t that holds no address: an |
| 520 | integer or a real, or a record or array of these alone (report 3 and |
| 521 | 6.9: what a view may write over outside a module that imports SYSTEM) *) |
| 522 | proc isPlain*(t: ^Type): BOOL; |
| 523 | var f: ^Obj; |
| 524 | begin |
| 525 | if t^.form in {fInt, fReal} then return true; end; |
| 526 | if t^.form = fArray then return isPlain(t^.base); end; |
| 527 | if t^.form # fRecord then return false; end; |
| 528 | f := t^.fields; |
| 529 | while f # nil do |
| 530 | if not isPlain(f^.typ) then return false; end; |
| 531 | f := f^.next; |
| 532 | end; |
| 533 | return true; |
| 534 | end; |
| 535 | |
| 536 | (* Whether a result field f is a single value of at most a word, not a real *) |
| 537 | proc oneWord(f: ^Obj): BOOL; |
| 538 | begin |
| 539 | return isScalar(f^.typ) and (f^.typ^.form # fReal) and (f^.typ^.size <= 8); |
| 540 | end; |
| 541 | |
| 542 | (* Whether a procedure's result t is two words, which come back in rax |
| 543 | and rdx, as System V returns a struct of two integers: a slice (its |
| 544 | pointer, then its length), or two results that are each a single |
| 545 | value of 8 bytes at most and not a real. *) |
| 546 | proc inTwoRegs*(t: ^Type): BOOL; |
| 547 | begin |
| 548 | if t^.form = fSlice then return true; end; |
| 549 | return t^.tuple and (t^.len = 2) and oneWord(t^.fields) and oneWord(t^.fields^.next); |
| 550 | end; |
| 551 | |
| 552 | (* Whether a procedure's result t comes back in memory the caller |
| 553 | provides, whose address the caller passes as a hidden first argument, |
| 554 | in rdi: any result that is neither one value, in rax or xmm0, nor two |
| 555 | words, in rax and rdx. *) |
| 556 | proc behindPointer*(t: ^Type): BOOL; |
| 557 | begin |
| 558 | return isAggregate(t) and not inTwoRegs(t); |
| 559 | end; |
| 560 | |
| 561 | proc fits*(v: I64; t: ^Type): BOOL; |
| 562 | begin |
| 563 | if t^.form # fInt then return t^.form = fConst; end; |
| 564 | match t^.size of |
| 565 | | 1: |
| 566 | if t^.signed then return (v >= -128) and (v <= 127); end; |
| 567 | return (v >= 0) and (v <= 255); |
| 568 | | 2: |
| 569 | if t^.signed then return (v >= -32768) and (v <= 32767); end; |
| 570 | return (v >= 0) and (v <= 65535); |
| 571 | | 4: |
| 572 | if t^.signed then return (v >= -2147483648) and (v <= 2147483647); end; |
| 573 | return (v >= 0) and (v <= 4294967295); |
| 574 | | else: return t^.signed or (v >= 0); |
| 575 | end; |
| 576 | end; |
| 577 | |
| 578 | proc same*(a, b: ^Type): BOOL; forward; |
| 579 | |
| 580 | (* Whether two procedure types, or two tuples, are alike: the same |
| 581 | results and parameters, in the same modes *) |
| 582 | proc alikeShape(a, b: ^Type): BOOL; |
| 583 | var p, q: ^Obj; |
| 584 | begin |
| 585 | (* a record is the same only as itself, but for several results *) |
| 586 | if (a^.form = fRecord) and not (a^.tuple and b^.tuple) then return false; end; |
| 587 | if (a^.len # b^.len) or (a^.variadic # b^.variadic) then return false; end; |
| 588 | if (a^.base = nil) # (b^.base = nil) then return false; end; |
| 589 | if (a^.base # nil) and not same(a^.base, b^.base) then return false; end; |
| 590 | p := a^.fields; q := b^.fields; |
| 591 | while p # nil do |
| 592 | if (p^.isVar # q^.isVar) or (p^.isConst # q^.isConst) or not same(p^.typ, q^.typ) then |
| 593 | return false; |
| 594 | end; |
| 595 | p := p^.next; q := q^.next; |
| 596 | end; |
| 597 | return true; |
| 598 | end; |
| 599 | |
| 600 | (* Structural sameness of two types. Named types are the same only if |
| 601 | they are one declaration; a type with no name is the same as another |
| 602 | built alike. *) |
| 603 | proc alike(a, b: ^Type): BOOL; |
| 604 | begin |
| 605 | if a = b then return true; end; |
| 606 | if a^.form # b^.form then return false; end; |
| 607 | match a^.form of |
| 608 | | fPtr: return (a^.vol = b^.vol) and same(a^.base, b^.base); |
| 609 | | fArray: return (a^.len = b^.len) and same(a^.base, b^.base); |
| 610 | | fSlice: return (a^.rdonly = b^.rdonly) and same(a^.base, b^.base); |
| 611 | | fSet: return same(a^.base, b^.base); |
| 612 | | fProc: return alikeShape(a, b); |
| 613 | | fRecord: return alikeShape(a, b); |
| 614 | | else: return false; |
| 615 | end; |
| 616 | end; |
| 617 | |
| 618 | proc same*(a, b: ^Type): BOOL; |
| 619 | begin |
| 620 | if a = b then return true; end; |
| 621 | if (a = nil) or (b = nil) then return false; end; |
| 622 | if (a^.obj # nil) and (b^.obj # nil) then return false; end; |
| 623 | return alike(a, b); |
| 624 | end; |
| 625 | |
| 626 | (* What a type is called, added to b: the names a program gives it, else |
| 627 | what it is made of *) |
| 628 | proc typeText(var b: Buf.Buf; t: ^Type); |
| 629 | var f: ^Obj; |
| 630 | begin |
| 631 | if t = nil then Buf.str(b, "nothing"); return; end; |
| 632 | if t^.obj # nil then |
| 633 | if (t^.obj^.home # nil) and (t^.obj^.home # curModule) then |
| 634 | Buf.str(b, Scan.nameOf(t^.obj^.home^.sname)); |
| 635 | Buf.str(b, "."); |
| 636 | end; |
| 637 | Buf.str(b, Scan.nameOf(t^.obj^.name)); |
| 638 | return; |
| 639 | end; |
| 640 | match t^.form of |
| 641 | | fConst: Buf.str(b, "an integer constant"); |
| 642 | | fReal: Buf.str(b, "a real constant"); |
| 643 | | fNil: Buf.str(b, "nil"); |
| 644 | | fStr: Buf.str(b, "a string"); |
| 645 | | fPtr: |
| 646 | if t^.vol then Buf.str(b, "^VOLATILE "); else Buf.str(b, "^"); end; |
| 647 | typeText(b, t^.base); |
| 648 | | fArray: |
| 649 | Fmt.write(b, "[{}]", [Fmt.int(t^.len)]); |
| 650 | typeText(b, t^.base); |
| 651 | | fSlice: |
| 652 | if t^.rdonly then Buf.str(b, "[]const "); else Buf.str(b, "[]"); end; |
| 653 | typeText(b, t^.base); |
| 654 | | fRecord: |
| 655 | if not t^.tuple then |
| 656 | Buf.str(b, "record"); |
| 657 | else |
| 658 | Buf.str(b, "("); |
| 659 | f := t^.fields; |
| 660 | while f # nil do |
| 661 | typeText(b, f^.typ); |
| 662 | f := f^.next; |
| 663 | if f # nil then Buf.str(b, ", "); end; |
| 664 | end; |
| 665 | Buf.str(b, ")"); |
| 666 | end; |
| 667 | | fVariant: Buf.str(b, "variant"); |
| 668 | | fProc: |
| 669 | Buf.str(b, "proc("); |
| 670 | f := t^.fields; |
| 671 | while f # nil do |
| 672 | if f^.isConst then Buf.str(b, "const "); |
| 673 | elsif f^.isVar then Buf.str(b, "var "); end; |
| 674 | typeText(b, f^.typ); |
| 675 | f := f^.next; |
| 676 | if f # nil then Buf.str(b, ", "); end; |
| 677 | end; |
| 678 | Buf.str(b, ")"); |
| 679 | if t^.base # nil then |
| 680 | Buf.str(b, ": "); |
| 681 | typeText(b, t^.base); |
| 682 | end; |
| 683 | | else: Buf.str(b, "?"); |
| 684 | end; |
| 685 | end; |
| 686 | |
| 687 | (* A name for t, kept for good, as every message is *) |
| 688 | proc typeName*(t: ^Type): []const U8; |
| 689 | var b: ^Buf.Buf; |
| 690 | begin |
| 691 | b := ^Buf.Buf(Scan.grab(SIZE(Buf.Buf))); |
| 692 | Buf.init(b^, Heap.heap); |
| 693 | typeText(b^, t); |
| 694 | return Buf.bytes(b^); |
| 695 | end; |
| 696 | |
| 697 | (* A fingerprint of what a declaration exports, for the interface |
| 698 | check: two files of one module export the same thing when their |
| 699 | fingerprints agree. A named type counts by its name, so that a type |
| 700 | declared in each file stands for the other's. *) |
| 701 | proc mix(var h: U64; v: I64); |
| 702 | begin |
| 703 | h := (h xor U64(v)) * 1099511628211; (* FNV-1a's 64-bit prime *) |
| 704 | end; |
| 705 | |
| 706 | proc bit(f: BOOL): INT; |
| 707 | begin |
| 708 | if f then return 1; end; |
| 709 | return 0; |
| 710 | end; |
| 711 | |
| 712 | proc shape(var h: U64; t: ^Type; top: BOOL); |
| 713 | var f, g: ^Obj; |
| 714 | begin |
| 715 | if t = nil then mix(h, -1); return; end; |
| 716 | if (t^.obj # nil) and not top then |
| 717 | mix(h, t^.obj^.name); |
| 718 | if t^.obj^.home # nil then mix(h, t^.obj^.home^.sname); end; |
| 719 | return; |
| 720 | end; |
| 721 | mix(h, INT(t^.form)); |
| 722 | mix(h, bit(t^.vol) + 2 * bit(t^.variadic) + 4 * bit(t^.rdonly)); |
| 723 | mix(h, t^.len); |
| 724 | if t^.form in {fPtr, fArray, fSlice, fProc, fSet} then shape(h, t^.base, false); end; |
| 725 | f := t^.fields; |
| 726 | while f # nil do |
| 727 | if (t^.form # fRecord) or f^.exported then |
| 728 | mix(h, f^.name); |
| 729 | mix(h, bit(f^.isVar) + 2 * bit(f^.isConst)); |
| 730 | shape(h, f^.typ, false); |
| 731 | g := f^.fields; |
| 732 | while g # nil do shape(h, g^.typ, false); g := g^.next; end; |
| 733 | end; |
| 734 | f := f^.next; |
| 735 | end; |
| 736 | end; |
| 737 | |
| 738 | proc fingerprint*(o: ^Obj): U64; |
| 739 | var h: U64; f: ^Obj; |
| 740 | begin |
| 741 | h := 0CBF29CE484222325H; (* FNV-1a's 64-bit offset basis *) |
| 742 | mix(h, INT(o^.cls)); |
| 743 | if o^.cls = oCase then |
| 744 | mix(h, o^.val); |
| 745 | f := o^.fields; |
| 746 | while f # nil do shape(h, f^.typ, false); f := f^.next; end; |
| 747 | else |
| 748 | shape(h, o^.typ, o^.cls = oType); |
| 749 | end; |
| 750 | return h; |
| 751 | end; |
| 752 | |
| 753 | (* The universe *) |
| 754 | |
| 755 | proc basic(name: []const U8; form: Form; size: I64; signed: BOOL): ^Type; |
| 756 | var t: ^Type; o: ^Obj; |
| 757 | begin |
| 758 | t := newType(form, size, size); |
| 759 | t^.signed := signed; |
| 760 | o := newObj(oType, Scan.intern(SYSTEM.PTR(name), LEN(name)), t); |
| 761 | o^.exported := true; |
| 762 | t^.obj := o; |
| 763 | _ := insert(o, 0); |
| 764 | return t; |
| 765 | end; |
| 766 | |
| 767 | (* The name s, interned *) |
| 768 | proc internOf(s: []const U8): INT; |
| 769 | begin |
| 770 | return Scan.intern(SYSTEM.PTR(s), LEN(s)); |
| 771 | end; |
| 772 | |
| 773 | (* Declares name in the scope s, exported, as a built-in is *) |
| 774 | proc enterIn(s: INT; name: []const U8; cls: Class; t: ^Type; v: I64); |
| 775 | var o: ^Obj; |
| 776 | begin |
| 777 | o := newObj(cls, internOf(name), t); |
| 778 | o^.val := v; |
| 779 | o^.exported := true; |
| 780 | if s # 0 then o^.home := system; end; (* one of SYSTEM's, told from the universe's *) |
| 781 | _ := insert(o, s); |
| 782 | end; |
| 783 | |
| 784 | (* Declares name in the universe *) |
| 785 | proc enter(name: []const U8; cls: Class; t: ^Type; v: I64); |
| 786 | begin |
| 787 | enterIn(0, name, cls, t, v); |
| 788 | end; |
| 789 | |
| 790 | begin |
| 791 | hashMask := (1 shl 14) - 1; |
| 792 | hashTab := emptyTable(hashMask + 1); |
| 793 | depth := 1; |
| 794 | scopes[0] := 0; |
| 795 | nextScope := 0; |
| 796 | curModule := nil; |
| 797 | tI8 := basic("I8", fInt, 1, true); |
| 798 | tI16 := basic("I16", fInt, 2, true); |
| 799 | tI32 := basic("I32", fInt, 4, true); |
| 800 | tI64 := basic("I64", fInt, 8, true); |
| 801 | tU8 := basic("U8", fInt, 1, false); |
| 802 | tU16 := basic("U16", fInt, 2, false); |
| 803 | tU32 := basic("U32", fInt, 4, false); |
| 804 | tU64 := basic("U64", fInt, 8, false); |
| 805 | tInt := basic("INT", fInt, 8, true); |
| 806 | tUInt := basic("UINT", fInt, 8, false); |
| 807 | tBool := basic("BOOL", fBool, 1, false); |
| 808 | tF32 := basic("F32", fReal, 4, false); |
| 809 | tReal := basic("F64", fReal, 8, false); |
| 810 | tRConst := newType(fReal, 8, 8); |
| 811 | tError := basic("ERROR", fError, 8, false); |
| 812 | tConst := newType(fConst, 8, 8); |
| 813 | tConst^.signed := true; |
| 814 | tNil := newType(fNil, 8, 8); |
| 815 | tStr := newType(fStr, 0, 1); |
| 816 | tNone := newType(fNone, 0, 1); |
| 817 | tPtrU8 := newType(fPtr, 8, 8); |
| 818 | tPtrU8^.base := tU8; |
| 819 | tBytes := newType(fSlice, 16, 8); |
| 820 | tBytes^.base := tU8; |
| 821 | tConstBytes := newType(fSlice, 16, 8); (* []const U8 *) |
| 822 | tConstBytes^.base := tU8; |
| 823 | tConstBytes^.rdonly := true; |
| 824 | tPoison := newType(fInt, 8, 8); |
| 825 | tPoison^.signed := true; |
| 826 | enter("true", oConst, tBool, 1); |
| 827 | enter("false", oConst, tBool, 0); |
| 828 | enter("nil", oConst, tNil, 0); |
| 829 | enter("SIZE", oBuiltin, nil, bSize); |
| 830 | enter("ADR", oBuiltin, nil, bAdr); |
| 831 | enter("LEN", oBuiltin, nil, bLen); |
| 832 | enter("PTR", oBuiltin, nil, bPtr); |
| 833 | enter("OFFSET", oBuiltin, nil, bOffset); |
| 834 | enter("BYTES", oBuiltin, nil, bBytes); |
| 835 | enter("NEW", oBuiltin, nil, bNew); |
| 836 | enter("MSG", oBuiltin, nil, bMsg); |
| 837 | enter("SYSCALL", oBuiltin, nil, bSyscall); |
| 838 | enter("FENCE", oBuiltin, nil, bFence); |
| 839 | enter("CAS", oBuiltin, nil, bCas); |
| 840 | enter("XCHG", oBuiltin, nil, bXchg); |
| 841 | enter("XADD", oBuiltin, nil, bXadd); |
| 842 | enter("ARGC", oBuiltin, nil, bArgc); |
| 843 | enter("ARGV", oBuiltin, nil, bArgv); |
| 844 | enter("INC", oBuiltin, nil, bInc); |
| 845 | enter("DEC", oBuiltin, nil, bDec); |
| 846 | enter("COPY", oBuiltin, nil, bCopy); |
| 847 | enter("FILL", oBuiltin, nil, bFill); |
| 848 | enter("ROL", oBuiltin, nil, bRol); |
| 849 | enter("ROR", oBuiltin, nil, bRor); |
| 850 | enter("MIN", oBuiltin, nil, bMin); |
| 851 | enter("MAX", oBuiltin, nil, bMax); |
| 852 | enter("ABS", oBuiltin, nil, bAbs); |
| 853 | enter("CLZ", oBuiltin, nil, bClz); |
| 854 | enter("CTZ", oBuiltin, nil, bCtz); |
| 855 | enter("POPCOUNT", oBuiltin, nil, bPopcount); |
| 856 | NEW(system, Heap.heap); |
| 857 | system^.name := internOf("SYSTEM"); |
| 858 | system^.sname := system^.name; |
| 859 | system^.scope := newScope(); |
| 860 | system^.body := -1; |
| 861 | system^.done := true; |
| 862 | enterIn(system^.scope, "ADR", oBuiltin, nil, bAdr); |
| 863 | enterIn(system^.scope, "PTR", oBuiltin, nil, bPtr); |
| 864 | enterIn(system^.scope, "SYSCALL", oBuiltin, nil, bSyscall); |
| 865 | enterIn(system^.scope, "ARGV", oBuiltin, nil, bArgv); |
| 866 | end Sym. |