Self step 2a: Chunk and Sym in raz, tested against the seeds
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | 1. `diag` and `scan` (done): the trial. Measure `scan`'s speed against the |
| 32 | 32 | seed's and list what `Sys` and the library lacked. Stop for review: |
| 33 | 33 | if the cost is too high, mend the code generator first. |
| 34 | -2. `chunk`, `sym`, `link`, `elf`, `x64`. | |
| 34 | +2. `chunk`, `sym` (2a, done); `link`, `elf`, `x64` (2b). | |
| 35 | 35 | 3. `gen`, `item`. |
| 36 | 36 | 4. `parse`, `raz`: the first compiler that builds. |
| 37 | 37 | 5. Fixpoint: the seed builds `self`, that builds `self`, that builds |
| @@ -0,0 +1,364 @@ | ||
| 1 | +(* Chunk: the compiled program, before it is linked. | |
| 2 | + | |
| 3 | + The code of a program is a set of chunks: one per procedure, module | |
| 4 | + variable and string. A chunk's bytes go in one of three sections: | |
| 5 | + text (code), rodata (strings and other constants), bss (variables, | |
| 6 | + which are all zero). A reference from code to another chunk (a call, | |
| 7 | + or an address) is a fixup, patched once the linker has placed every | |
| 8 | + chunk. Line notes say which place in the source the code from each | |
| 9 | + point on is for. | |
| 10 | + | |
| 11 | + Nothing here depends on the machine: text is bytes, written by the | |
| 12 | + encoder for the target (X64). | |
| 13 | + | |
| 14 | + Depends on: Scan, for names and memory; Diag, for positions. | |
| 15 | + Leaves out: the instructions; that is X64. | |
| 16 | + | |
| 17 | + Exported variables can't be set from outside, so the setters at the | |
| 18 | + end do what the seed's other units do to these. *) | |
| 19 | +module Chunk; | |
| 20 | +import Mem, Heap, Fmt, Diag, Scan; | |
| 21 | + | |
| 22 | +type | |
| 23 | + Section* = (secText, secRodata, secBss); | |
| 24 | + | |
| 25 | + Chunk* = record | |
| 26 | + sec*: Section; | |
| 27 | + start*, size*, align*: INT; (* in its section's buffer *) | |
| 28 | + firstFix*, numFix*: INT; (* its fixups: text's in fixups, rodata's in dataFixups *) | |
| 29 | + home*, name*: INT; (* its module and its name, for the symbol table; -1 if none *) | |
| 30 | + adr*: I64; (* its address, set by the linker *) | |
| 31 | + live*: BOOL; (* reached from the entry point *) | |
| 32 | + ext*: BOOL; (* bss: the address of a C symbol, its name *) | |
| 33 | + reals*: BOOL; (* code: computes with reals, first at realsAt *) | |
| 34 | + realsAt*: Diag.Pos; | |
| 35 | + saveMask*: INT; (* code: the registers of variables it saves, as bits of Gen's VarRegs *) | |
| 36 | + saveAt*: INT; (* ... at this offset from the frame, one quadword each *) | |
| 37 | + end; | |
| 38 | + | |
| 39 | + (* A place in the source whose code starts at a place in text: for | |
| 40 | + gdb, and for Rt to say where a failed check or a signal was *) | |
| 41 | + Line* = record | |
| 42 | + at*, chunk*: INT; (* the place in text, and its chunk *) | |
| 43 | + path*, line*, col*: INT; (* the file's path, interned, the line and column *) | |
| 44 | + end; | |
| 45 | + | |
| 46 | + (* A variable of a procedure, for the trap report: Rt shows it, by | |
| 47 | + kind, from its place in the frame *) | |
| 48 | + VarNote* = record | |
| 49 | + chunk*, name*: INT; | |
| 50 | + off*: INT; (* from the frame pointer *) | |
| 51 | + kind*: INT; (* how Rt shows it: see Link *) | |
| 52 | + end; | |
| 53 | + | |
| 54 | + Fixup* = record | |
| 55 | + at*: INT; (* the 32-bit field, as an offset in text; in | |
| 56 | + dataFixups, the 64-bit one, from its chunk's start *) | |
| 57 | + chunk*: INT; (* the chunk it refers to *) | |
| 58 | + addend*: INT; (* added to the chunk's address *) | |
| 59 | + end; | |
| 60 | + | |
| 61 | +var | |
| 62 | + text*: ^U8; (* text *) | |
| 63 | + pc*: INT; (* its length *) | |
| 64 | + rodata*: ^U8; | |
| 65 | + rolen*: INT; | |
| 66 | + chunks*: ^Chunk; | |
| 67 | + numChunks*: INT; | |
| 68 | + fixups*: ^Fixup; | |
| 69 | + numFixups*: INT; | |
| 70 | + (* Addresses in read-only data, as in an array constant of strings: | |
| 71 | + each 8 bytes, the chunk's absolute address, since the executable | |
| 72 | + is loaded where the linker placed it *) | |
| 73 | + dataFixups*: ^Fixup; | |
| 74 | + numDataFixups*: INT; | |
| 75 | + lines*: ^Line; | |
| 76 | + numLines*: INT; | |
| 77 | + curFile: INT; (* the file being compiled, for noteLine *) | |
| 78 | + curChunk*: INT; (* the chunk of code being written *) | |
| 79 | + varNotes*: ^VarNote; | |
| 80 | + numVarNotes*: INT; | |
| 81 | + | |
| 82 | + codeCap, roCap, chunkCap, fixCap, dataFixCap, lineCap, noteCap: INT; | |
| 83 | + | |
| 84 | +(* p, which holds used bytes, moved to a block of size bytes *) | |
| 85 | +proc grown(p: ^U8; used, size: INT): ^U8; | |
| 86 | +var q: ^U8; | |
| 87 | +begin | |
| 88 | + q := Scan.grab(size); | |
| 89 | + COPY(q[0..used], p[0..used]); | |
| 90 | + Mem.free(Heap.heap, p); | |
| 91 | + return q; | |
| 92 | +end; | |
| 93 | + | |
| 94 | +(* Room for n more bytes of code *) | |
| 95 | +proc room(n: INT); | |
| 96 | +begin | |
| 97 | + if pc + n > codeCap then | |
| 98 | + text := grown(text, pc, codeCap * 2 + n); | |
| 99 | + codeCap := codeCap * 2 + n; | |
| 100 | + end; | |
| 101 | +end; | |
| 102 | + | |
| 103 | +proc put1*(b: INT); | |
| 104 | +begin | |
| 105 | + if pc + 16 > codeCap then room(16); end; | |
| 106 | + text[pc] := U8(b & 0FFH); | |
| 107 | + INC(pc); | |
| 108 | +end; | |
| 109 | + | |
| 110 | +(* The low 32 bits of v, least significant byte first *) | |
| 111 | +proc put4*(v: INT); | |
| 112 | +var i: INT; | |
| 113 | +begin | |
| 114 | + if pc + 16 > codeCap then room(16); end; | |
| 115 | + for i := 0 to 3 do text[pc + i] := U8((v shr (8 * i)) & 0FFH); end; | |
| 116 | + INC(pc, 4); | |
| 117 | +end; | |
| 118 | + | |
| 119 | +proc put4At*(at, v: INT); | |
| 120 | +var i: INT; | |
| 121 | +begin | |
| 122 | + for i := 0 to 3 do text[at + i] := U8((v shr (8 * i)) & 0FFH); end; | |
| 123 | +end; | |
| 124 | + | |
| 125 | +(* The 32 bits at at, as a signed number *) | |
| 126 | +proc get4At*(at: INT): INT; | |
| 127 | +var u, i: INT; | |
| 128 | +begin | |
| 129 | + u := 0; | |
| 130 | + for i := 0 to 3 do u := u * 256 + INT(text[at + 3 - i]); end; | |
| 131 | + if u >= 80000000H then u := u - 100000000H; end; | |
| 132 | + return u; | |
| 133 | +end; | |
| 134 | + | |
| 135 | +(* The last n bytes of code taken back. A line noted past the new end | |
| 136 | + now starts there, in place of one noted there before, as noteLine | |
| 137 | + would have it. *) | |
| 138 | +proc takeBack*(n: INT); | |
| 139 | +begin | |
| 140 | + DEC(pc, n); | |
| 141 | + if (numLines > 0) and (lines[numLines - 1].at > pc) then | |
| 142 | + lines[numLines - 1].at := pc; | |
| 143 | + if (numLines > 1) and (lines[numLines - 2].at = pc) then | |
| 144 | + lines[numLines - 2] := lines[numLines - 1]; | |
| 145 | + DEC(numLines); | |
| 146 | + end; | |
| 147 | + end; | |
| 148 | +end; | |
| 149 | + | |
| 150 | +proc put8*(v: I64); | |
| 151 | +begin | |
| 152 | + put4(INT(v & 0FFFFFFFFH)); | |
| 153 | + put4(INT((v shr 32) & 0FFFFFFFFH)); | |
| 154 | +end; | |
| 155 | + | |
| 156 | +proc newChunk*(sec: Section; home, name: INT): INT; | |
| 157 | +var c: ^Chunk; | |
| 158 | +begin | |
| 159 | + if numChunks = chunkCap then | |
| 160 | + chunks := ^Chunk(grown(^U8(chunks), numChunks * SIZE(Chunk), chunkCap * 2 * SIZE(Chunk))); | |
| 161 | + chunkCap := chunkCap * 2; | |
| 162 | + end; | |
| 163 | + c := ADR(chunks[numChunks]); | |
| 164 | + c^ := {}; | |
| 165 | + c^.sec := sec; | |
| 166 | + c^.home := home; | |
| 167 | + c^.name := name; | |
| 168 | + c^.align := 1; | |
| 169 | + c^.size := -1; (* not yet defined *) | |
| 170 | + INC(numChunks); | |
| 171 | + return numChunks - 1; | |
| 172 | +end; | |
| 173 | + | |
| 174 | +proc beginChunk*(c: INT); | |
| 175 | +begin | |
| 176 | + (* code is aligned to 32 bytes, the windows in which x86 fetches and | |
| 177 | + caches it, so that a procedure's loops sit in them the same way | |
| 178 | + whatever comes before: rat ran 5% slower at 16, by where its loops | |
| 179 | + fell. Padded with CC, x86's int3: a trap if it were ever run *) | |
| 180 | + while pc & 31 # 0 do put1(0CCH); end; | |
| 181 | + curChunk := c; | |
| 182 | + chunks[c].start := pc; | |
| 183 | + chunks[c].align := 32; | |
| 184 | + chunks[c].firstFix := numFixups; | |
| 185 | +end; | |
| 186 | + | |
| 187 | +proc endChunk*(c: INT); | |
| 188 | +begin | |
| 189 | + chunks[c].size := pc - chunks[c].start; | |
| 190 | + chunks[c].numFix := numFixups - chunks[c].firstFix; | |
| 191 | +end; | |
| 192 | + | |
| 193 | +(* The n bytes at p are the data of rodata chunk c *) | |
| 194 | +proc setData*(c: INT; p: ^U8; n, align: INT); | |
| 195 | +begin | |
| 196 | + if rolen + n > roCap then | |
| 197 | + rodata := grown(rodata, rolen, roCap * 2 + n); | |
| 198 | + roCap := roCap * 2 + n; | |
| 199 | + end; | |
| 200 | + COPY(rodata[rolen..rolen + n], p[0..n]); | |
| 201 | + chunks[c].firstFix := numDataFixups; | |
| 202 | + chunks[c].numFix := 0; | |
| 203 | + chunks[c].start := rolen; | |
| 204 | + chunks[c].size := n; | |
| 205 | + chunks[c].align := align; | |
| 206 | + INC(rolen, n); | |
| 207 | +end; | |
| 208 | + | |
| 209 | +proc setBss*(c: INT; size, align: INT); | |
| 210 | +begin | |
| 211 | + chunks[c].size := size; | |
| 212 | + chunks[c].align := align; | |
| 213 | +end; | |
| 214 | + | |
| 215 | +(* The code from here on is for line and column of the current file. A | |
| 216 | + place with no code of its own gives way to the next. *) | |
| 217 | +proc noteLine*(line, col: INT); | |
| 218 | +begin | |
| 219 | + if (numLines > 0) and (lines[numLines - 1].at = pc) then DEC(numLines); end; | |
| 220 | + if (numLines > 0) and (lines[numLines - 1].chunk = curChunk) and (lines[numLines - 1].line = line) | |
| 221 | + and (lines[numLines - 1].col = col) and (lines[numLines - 1].path = curFile) then | |
| 222 | + return; | |
| 223 | + end; | |
| 224 | + if numLines = lineCap then | |
| 225 | + lines := ^Line(grown(^U8(lines), numLines * SIZE(Line), lineCap * 2 * SIZE(Line))); | |
| 226 | + lineCap := lineCap * 2; | |
| 227 | + end; | |
| 228 | + lines[numLines].at := pc; | |
| 229 | + lines[numLines].chunk := curChunk; | |
| 230 | + lines[numLines].path := curFile; | |
| 231 | + lines[numLines].line := line; | |
| 232 | + lines[numLines].col := col; | |
| 233 | + INC(numLines); | |
| 234 | +end; | |
| 235 | + | |
| 236 | +(* The quadword that holds the address of C's symbol name, filled in | |
| 237 | + when the program starts; one for each name. *) | |
| 238 | +proc externChunk*(name: INT): INT; | |
| 239 | +var c: INT; | |
| 240 | +begin | |
| 241 | + for c := 0 to numChunks - 1 do | |
| 242 | + if chunks[c].ext and (chunks[c].name = name) then return c; end; | |
| 243 | + end; | |
| 244 | + c := newChunk(secBss, -1, name); | |
| 245 | + setBss(c, 8, 8); | |
| 246 | + chunks[c].ext := true; | |
| 247 | + return c; | |
| 248 | +end; | |
| 249 | + | |
| 250 | +proc addFixup*(at, chunk, addend: INT); | |
| 251 | +begin | |
| 252 | + if numFixups = fixCap then | |
| 253 | + fixups := ^Fixup(grown(^U8(fixups), numFixups * SIZE(Fixup), fixCap * 2 * SIZE(Fixup))); | |
| 254 | + fixCap := fixCap * 2; | |
| 255 | + end; | |
| 256 | + fixups[numFixups].at := at; | |
| 257 | + fixups[numFixups].chunk := chunk; | |
| 258 | + fixups[numFixups].addend := addend; | |
| 259 | + INC(numFixups); | |
| 260 | +end; | |
| 261 | + | |
| 262 | +(* The address of chunk, plus addend, at offset at of rodata chunk c, | |
| 263 | + whose data is set: its fixups follow one another. *) | |
| 264 | +proc addDataFixup*(c, at, chunk, addend: INT); | |
| 265 | +begin | |
| 266 | + if numDataFixups = dataFixCap then | |
| 267 | + dataFixups := ^Fixup(grown(^U8(dataFixups), numDataFixups * SIZE(Fixup), dataFixCap * 2 * SIZE(Fixup))); | |
| 268 | + dataFixCap := dataFixCap * 2; | |
| 269 | + end; | |
| 270 | + dataFixups[numDataFixups].at := at; | |
| 271 | + dataFixups[numDataFixups].chunk := chunk; | |
| 272 | + dataFixups[numDataFixups].addend := addend; | |
| 273 | + INC(numDataFixups); | |
| 274 | + INC(chunks[c].numFix); | |
| 275 | +end; | |
| 276 | + | |
| 277 | +proc noteVar*(chunk, name, off, kind: INT); | |
| 278 | +begin | |
| 279 | + if numVarNotes = noteCap then | |
| 280 | + varNotes := ^VarNote(grown(^U8(varNotes), numVarNotes * SIZE(VarNote), noteCap * 2 * SIZE(VarNote))); | |
| 281 | + noteCap := noteCap * 2; | |
| 282 | + end; | |
| 283 | + varNotes[numVarNotes].chunk := chunk; | |
| 284 | + varNotes[numVarNotes].name := name; | |
| 285 | + varNotes[numVarNotes].off := off; | |
| 286 | + varNotes[numVarNotes].kind := kind; | |
| 287 | + INC(numVarNotes); | |
| 288 | +end; | |
| 289 | + | |
| 290 | +(* The name of chunk c: its module's and its own, or literal and its | |
| 291 | + number for a literal *) | |
| 292 | +proc chunkName*(c: INT): []U8; | |
| 293 | +begin | |
| 294 | + if chunks[c].name < 0 then return Diag.say("literal{}", [Fmt.int(c)]); end; | |
| 295 | + if chunks[c].home < 0 then return Scan.nameOf(chunks[c].name); end; | |
| 296 | + return Diag.say("{}.{}", [Fmt.str(Scan.nameOf(chunks[c].home)), Fmt.str(Scan.nameOf(chunks[c].name))]); | |
| 297 | +end; | |
| 298 | + | |
| 299 | +(* What Gen's Redo and Rewind put back: the code, fixups and lines made | |
| 300 | + since are forgotten *) | |
| 301 | +proc rewindTo*(atPc, atFixups, atLines: INT); | |
| 302 | +begin | |
| 303 | + pc := atPc; | |
| 304 | + numFixups := atFixups; | |
| 305 | + numLines := atLines; | |
| 306 | +end; | |
| 307 | + | |
| 308 | +(* The file the code is being made from, as its path interned *) | |
| 309 | +proc setCurFile*(path: INT); | |
| 310 | +begin | |
| 311 | + curFile := path; | |
| 312 | +end; | |
| 313 | + | |
| 314 | +proc curFilePath*: INT; | |
| 315 | +begin | |
| 316 | + return curFile; | |
| 317 | +end; | |
| 318 | + | |
| 319 | +proc setLive*(c: INT); | |
| 320 | +begin | |
| 321 | + chunks[c].live := true; | |
| 322 | +end; | |
| 323 | + | |
| 324 | +proc setAdr*(c: INT; a: I64); | |
| 325 | +begin | |
| 326 | + chunks[c].adr := a; | |
| 327 | +end; | |
| 328 | + | |
| 329 | +(* Chunk c computes with reals, the first of them at p *) | |
| 330 | +proc setReals*(c: INT; p: Diag.Pos); | |
| 331 | +begin | |
| 332 | + chunks[c].reals := true; | |
| 333 | + chunks[c].realsAt := p; | |
| 334 | +end; | |
| 335 | + | |
| 336 | +proc setSaves*(c, mask, at: INT); | |
| 337 | +begin | |
| 338 | + chunks[c].saveMask := mask; | |
| 339 | + chunks[c].saveAt := at; | |
| 340 | +end; | |
| 341 | + | |
| 342 | +begin | |
| 343 | + codeCap := 1 shl 20; | |
| 344 | + text := Scan.grab(codeCap); | |
| 345 | + pc := 0; | |
| 346 | + roCap := 1 shl 16; | |
| 347 | + rodata := Scan.grab(roCap); | |
| 348 | + rolen := 0; | |
| 349 | + chunkCap := 1 shl 10; | |
| 350 | + chunks := ^Chunk(Scan.grab(chunkCap * SIZE(Chunk))); | |
| 351 | + numChunks := 0; | |
| 352 | + fixCap := 1 shl 12; | |
| 353 | + fixups := ^Fixup(Scan.grab(fixCap * SIZE(Fixup))); | |
| 354 | + numFixups := 0; | |
| 355 | + dataFixCap := 1 shl 8; | |
| 356 | + dataFixups := ^Fixup(Scan.grab(dataFixCap * SIZE(Fixup))); | |
| 357 | + numDataFixups := 0; | |
| 358 | + lineCap := 1 shl 12; | |
| 359 | + lines := ^Line(Scan.grab(lineCap * SIZE(Line))); | |
| 360 | + numLines := 0; | |
| 361 | + noteCap := 1 shl 8; | |
| 362 | + varNotes := ^VarNote(Scan.grab(noteCap * SIZE(VarNote))); | |
| 363 | + numVarNotes := 0; | |
| 364 | +end Chunk. |
| @@ -10,6 +10,13 @@ | ||
| 10 | 10 | make self # each unit against the seed's, on the same files |
| 11 | 11 | make self-bench # the scanner's speed against the seed's |
| 12 | 12 | |
| 13 | +`Chunk` and `Sym` have no input files to read, so each has a dumper | |
| 14 | +pair, `test/ChunkDump` and `test/SymDump`, that runs one fixed | |
| 15 | +pseudo-random script of calls on its side and prints all it made or | |
| 16 | +answered (`Sym` also ends, as an argument says, in the error of a name | |
| 17 | +declared twice or of too many scopes): the output, the messages and | |
| 18 | +the status must be the same. | |
| 19 | + | |
| 13 | 20 | `check.sh` builds a dumper for each side, `test/ScanDump.pas` over the |
| 14 | 21 | seed's units and `test/ScanDump.raz` over these, and has both read the |
| 15 | 22 | library, the examples, every test of the compiler and a file for each |
| @@ -40,6 +47,18 @@ | ||
| 40 | 47 | which keeps what it makes: messages are made only to be shown. A name |
| 41 | 48 | from `nameOf` stays valid, since names are kept in chunks that are |
| 42 | 49 | never moved; the seed's was a copy. |
| 50 | +- **Reserved words as names.** The seed's `code` (the text buffer of | |
| 51 | + `Chunk`) is `text`; `module` (a record's, an import's) is `home`; an | |
| 52 | + object's `link` is `chain`. A seed type `TName` is `Name`, and the | |
| 53 | + procedures begin in lower case, as `Scan`'s do. | |
| 54 | +- **Errors in `Sym`.** `insert` (a name declared twice) and `openScope` | |
| 55 | + (too many scopes) show the error and return `Scan.Failed`, as `mark` | |
| 56 | + does, for the parser to pass up with `try`. `saveScopes` returns what | |
| 57 | + the seed's `var d` gave. | |
| 58 | +- **Setters.** What other units set in `Chunk` has one each (`setLive`, | |
| 59 | + `setAdr`, `setReals`, `setSaves`, `setCurFile`, and `rewindTo` for | |
| 60 | + gen's `Redo` and `Rewind`), and `Sym` has `setCurModule`. `Scan.grab` | |
| 61 | + is exported for the memory they all take. | |
| 43 | 62 | - **No bit cast.** A real's bits and back go through a pointer. |
| 44 | 63 | - **Speed.** On the 348,025 lines of `test/genbench.py`, 7.6 MB, the |
| 45 | 64 | seed's scanner takes 75 ms and this one 74. It took 121 at first. |
| @@ -112,7 +112,7 @@ | ||
| 112 | 112 | end; |
| 113 | 113 | |
| 114 | 114 | (* Memory, or the end of the compile when there is none *) |
| 115 | -proc grab(size: INT): ^U8; | |
| 115 | +proc grab*(size: INT): ^U8; | |
| 116 | 116 | var p: ^U8; |
| 117 | 117 | begin |
| 118 | 118 | p := Mem.alloc(Heap.heap, size, 8); |
| @@ -0,0 +1,684 @@ | ||
| 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 | + | |
| 21 | +type | |
| 22 | + Form* = (fNone, fBool, fInt, fReal, fConst, fNil, fStr, fPtr, fArray, | |
| 23 | + fSlice, fRecord, fVariant, fProc, fEnum, fSet, fError); | |
| 24 | + (* fConst is the type of an untyped integer constant; fStr of a string | |
| 25 | + literal, whose length the literal carries. F32 and F64 are fReal, | |
| 26 | + told apart by size, and so is tRConst, an untyped real constant. *) | |
| 27 | + | |
| 28 | + Class* = (oConst, oType, oVar, oProc, oModule, oField, oCase, oBuiltin); | |
| 29 | + | |
| 30 | + Type* = record | |
| 31 | + form*: Form; | |
| 32 | + size*, align*: I64; | |
| 33 | + signed*: BOOL; (* fInt *) | |
| 34 | + vol*: BOOL; (* fPtr: ^VOLATILE *) | |
| 35 | + packd*: BOOL; (* fRecord *) | |
| 36 | + variadic*: BOOL; (* fProc: ends in ... *) | |
| 37 | + tuple*: BOOL; (* fRecord: the results of a procedure that has several *) | |
| 38 | + base*: ^Type; (* fPtr, fArray, fSlice: what it holds; fProc: result *) | |
| 39 | + len*: I64; (* fArray: elements; fProc: parameters; fVariant: cases; a tuple: results *) | |
| 40 | + fields*: ^Obj; (* fRecord: fields; fVariant: cases; fProc: parameters *) | |
| 41 | + scope*: INT; (* fRecord: the scope of its fields *) | |
| 42 | + fwd*: INT; (* fPtr whose base isn't declared yet: its name, else -1 *) | |
| 43 | + obj*: ^Obj; (* the declaration that names it, or nil *) | |
| 44 | + home*: ^Module; (* the module that built it *) | |
| 45 | + end; | |
| 46 | + | |
| 47 | + Obj* = record | |
| 48 | + cls*: Class; | |
| 49 | + name*: INT; | |
| 50 | + exported*: BOOL; | |
| 51 | + typ*: ^Type; | |
| 52 | + next*: ^Obj; (* the next in its list: scope, fields, parameters *) | |
| 53 | + scope*: INT; (* where it is declared *) | |
| 54 | + pos*: Diag.Pos; (* where its name is declared, for messages *) | |
| 55 | + val*: I64; (* oConst: value; oBuiltin: which; oCase: tag; | |
| 56 | + oField: offset; oVar: frame offset *) | |
| 57 | + chunk*: INT; (* global oVar, oProc, string oConst: its chunk *) | |
| 58 | + lev*: INT; (* oVar: 0 for a module variable, 1 for a local *) | |
| 59 | + ind*: BOOL; (* oVar: the frame slot holds the variable's address *) | |
| 60 | + off*: I64; (* oVar with ind: added to that address *) | |
| 61 | + isVar*: BOOL; (* a var parameter *) | |
| 62 | + param*: BOOL; (* oVar: a parameter *) | |
| 63 | + used*: BOOL; (* named somewhere but in its own body *) | |
| 64 | + read*: BOOL; (* oVar: named other than as what := assigns *) | |
| 65 | + fwd*: BOOL; (* oProc: declared forward, no body yet *) | |
| 66 | + isCode*: BOOL; (* oProc: a code procedure *) | |
| 67 | + openEnd*: BOOL; (* oProc: has results, and may reach its end *) | |
| 68 | + reg*: INT; (* oVar: the register it lives in, or 0 if in memory; | |
| 69 | + for a slice, the one its pointer is in *) | |
| 70 | + reg2*: INT; (* oVar: a slice in registers: the one its length is in *) | |
| 71 | + ext*: BOOL; (* oProc, oVar: C's, reached through its chunk, which | |
| 72 | + holds its address *) | |
| 73 | + fields*: ^Obj; (* oCase: its fields *) | |
| 74 | + home*: ^Module; (* oModule: the module; others: where declared *) | |
| 75 | + chain*: ^Obj; (* the next declaration of its module, top-level or | |
| 76 | + local *) | |
| 77 | + end; | |
| 78 | + | |
| 79 | + (* The arguments of a generic module's instance: a type, or a | |
| 80 | + procedure (o) and its type. *) | |
| 81 | + Args* = record | |
| 82 | + n*: INT; | |
| 83 | + t*: [8]^Type; | |
| 84 | + o*: [8]^Obj; | |
| 85 | + pos*: [8]Diag.Pos; (* where each argument is, for messages *) | |
| 86 | + at*: Diag.Pos; (* where the import is *) | |
| 87 | + end; | |
| 88 | + | |
| 89 | + Import* = record | |
| 90 | + home*: ^Module; | |
| 91 | + next*: ^Import; | |
| 92 | + end; | |
| 93 | + | |
| 94 | + (* A library a module links, by its name, as interned *) | |
| 95 | + Lib* = record | |
| 96 | + name*: INT; | |
| 97 | + next*: ^Lib; | |
| 98 | + end; | |
| 99 | + | |
| 100 | + Module* = record | |
| 101 | + name*: INT; | |
| 102 | + sname*: INT; (* the name of its symbols: List(I64) for an instance *) | |
| 103 | + args*: Args; | |
| 104 | + scope*: INT; | |
| 105 | + path*: []U8; | |
| 106 | + first*, last*: ^Obj; (* its top-level declarations, in order *) | |
| 107 | + locals*, lastLocal*: ^Obj; (* its procedures' declarations, in order *) | |
| 108 | + imports*: ^Import; | |
| 109 | + body*: INT; (* the chunk of its body, or -1 *) | |
| 110 | + done*: BOOL; (* compiled to the end *) | |
| 111 | + checkOnly*: BOOL; (* read only to check another file's interface *) | |
| 112 | + used*: BOOL; (* imported, through others, by the main module *) | |
| 113 | + usesC*: BOOL; (* has an extern, or links a library *) | |
| 114 | + linkStatic*: BOOL; (* says link static *) | |
| 115 | + libs*: ^Lib; (* the libraries it links *) | |
| 116 | + next*: ^Module; (* modules in the order they were finished *) | |
| 117 | + end; | |
| 118 | + | |
| 119 | +const | |
| 120 | + (* built-in procedures *) | |
| 121 | + bSize* = 0; bAdr* = 1; bLen* = 2; bNew* = 3; bMsg* = 4; bSyscall* = 5; | |
| 122 | + bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; | |
| 123 | + bInc* = 12; bDec* = 13; bCopy* = 14; | |
| 124 | + | |
| 125 | + MaxDepth* = 64; (* scopes open at once *) | |
| 126 | + | |
| 127 | +var | |
| 128 | + tI8*, tI16*, tI32*, tI64*, tU8*, tU16*, tU32*, tU64*: ^Type; | |
| 129 | + tInt*, tUInt*: ^Type; (* INT and UINT, as big as an address: 8 bytes here *) | |
| 130 | + tBool*, tF32*, tReal*, tRConst*, tError*, tConst*, tNil*, tStr*, tNone*, tPtrU8*, tBytes*: ^Type; | |
| 131 | + tPoison*: ^Type; (* of a name reported as not declared *) | |
| 132 | + curModule*: ^Module; | |
| 133 | + | |
| 134 | + scopes: [MaxDepth]INT; | |
| 135 | + depth: INT; | |
| 136 | + nextScope: INT; | |
| 137 | + hashTab: ^^Obj; | |
| 138 | + hashMask, hashCount: INT; | |
| 139 | + | |
| 140 | +proc setCurModule*(m: ^Module); | |
| 141 | +begin | |
| 142 | + curModule := m; | |
| 143 | +end; | |
| 144 | + | |
| 145 | +proc newType*(form: Form; size, align: I64): ^Type; | |
| 146 | +var t: ^Type; | |
| 147 | +begin | |
| 148 | + NEW(t, Heap.heap); | |
| 149 | + if t = nil then Diag.fatal("out of memory"); end; | |
| 150 | + t^.form := form; | |
| 151 | + t^.size := size; | |
| 152 | + t^.align := align; | |
| 153 | + t^.fwd := -1; | |
| 154 | + t^.home := curModule; | |
| 155 | + return t; | |
| 156 | +end; | |
| 157 | + | |
| 158 | +proc newObj*(cls: Class; name: INT; typ: ^Type): ^Obj; | |
| 159 | +var o: ^Obj; | |
| 160 | +begin | |
| 161 | + NEW(o, Heap.heap); | |
| 162 | + if o = nil then Diag.fatal("out of memory"); end; | |
| 163 | + o^.cls := cls; | |
| 164 | + o^.name := name; | |
| 165 | + o^.typ := typ; | |
| 166 | + o^.chunk := -1; | |
| 167 | + o^.home := curModule; | |
| 168 | + o^.pos := Scan.tokPos(); | |
| 169 | + return o; | |
| 170 | +end; | |
| 171 | + | |
| 172 | +(* Scopes *) | |
| 173 | + | |
| 174 | +proc newScope*: INT; | |
| 175 | +begin | |
| 176 | + INC(nextScope); | |
| 177 | + return nextScope; | |
| 178 | +end; | |
| 179 | + | |
| 180 | +proc openScope*(s: INT): ERROR; | |
| 181 | +begin | |
| 182 | + if depth = MaxDepth then | |
| 183 | + return Scan.mark(Diag.say("scopes nested more than {} deep", [Fmt.int(MaxDepth)])); | |
| 184 | + end; | |
| 185 | + scopes[depth] := s; | |
| 186 | + INC(depth); | |
| 187 | + return nil; | |
| 188 | +end; | |
| 189 | + | |
| 190 | +proc closeScope*; | |
| 191 | +begin | |
| 192 | + DEC(depth); | |
| 193 | +end; | |
| 194 | + | |
| 195 | +(* An imported module is compiled in the middle of another, and must | |
| 196 | + see only the universe and itself. The scopes open are kept in saved, | |
| 197 | + and their number is the result. *) | |
| 198 | +proc saveScopes*(saved: []INT): INT; | |
| 199 | +var i, d: INT; | |
| 200 | +begin | |
| 201 | + d := depth; | |
| 202 | + for i := 0 to depth - 1 do saved[i] := scopes[i]; end; | |
| 203 | + depth := 1; | |
| 204 | + return d; | |
| 205 | +end; | |
| 206 | + | |
| 207 | +proc restoreScopes*(d: INT; saved: []INT); | |
| 208 | +var i: INT; | |
| 209 | +begin | |
| 210 | + depth := d; | |
| 211 | + for i := 0 to depth - 1 do scopes[i] := saved[i]; end; | |
| 212 | +end; | |
| 213 | + | |
| 214 | +proc scopeDepth*: INT; | |
| 215 | +begin | |
| 216 | + return depth; | |
| 217 | +end; | |
| 218 | + | |
| 219 | +(* Closes the scopes opened since there were d, after an error *) | |
| 220 | +proc cutScopes*(d: INT); | |
| 221 | +begin | |
| 222 | + depth := d; | |
| 223 | +end; | |
| 224 | + | |
| 225 | +proc topScope*: INT; | |
| 226 | +begin | |
| 227 | + return scopes[depth - 1]; | |
| 228 | +end; | |
| 229 | + | |
| 230 | +(* Where the search for name in scope s starts in the hash table: each | |
| 231 | + number is multiplied by an odd constant near 2^32 or 2^16 over the | |
| 232 | + golden ratio (Knuth's multiplicative hashing), which spreads the low | |
| 233 | + bits the mask keeps. *) | |
| 234 | +proc slot(s, name: INT): INT; | |
| 235 | +begin | |
| 236 | + return INT(((U32(s & 0FFFFFFFFH) * 2654435761) xor (U32(name & 0FFFFFFFFH) * 40503)) & U32(hashMask)); | |
| 237 | +end; | |
| 238 | + | |
| 239 | +(* A table of n slots, all empty *) | |
| 240 | +proc emptyTable(n: INT): ^^Obj; | |
| 241 | +var t: ^^Obj; i: INT; | |
| 242 | +begin | |
| 243 | + t := ^^Obj(Scan.grab(n * SIZE(^Obj))); | |
| 244 | + for i := 0 to n - 1 do t[i] := nil; end; | |
| 245 | + return t; | |
| 246 | +end; | |
| 247 | + | |
| 248 | +(* Doubles the table, which insert keeps at most half full so that the | |
| 249 | + linear probing stays short, and puts every entry in its new slot. *) | |
| 250 | +proc rehash; | |
| 251 | +var old: ^^Obj; n, i, j: INT; | |
| 252 | +begin | |
| 253 | + old := hashTab; | |
| 254 | + n := hashMask + 1; | |
| 255 | + hashMask := n * 2 - 1; | |
| 256 | + hashTab := emptyTable(n * 2); | |
| 257 | + for i := 0 to n - 1 do | |
| 258 | + if old[i] # nil then | |
| 259 | + j := slot(old[i]^.scope, old[i]^.name); | |
| 260 | + while hashTab[j] # nil do j := (j + 1) & hashMask; end; | |
| 261 | + hashTab[j] := old[i]; | |
| 262 | + end; | |
| 263 | + end; | |
| 264 | + Mem.free(Heap.heap, ^U8(old)); | |
| 265 | +end; | |
| 266 | + | |
| 267 | +proc insert*(obj: ^Obj; s: INT): ERROR; | |
| 268 | +var i: INT; o: ^Obj; | |
| 269 | +begin | |
| 270 | + i := slot(s, obj^.name); | |
| 271 | + o := hashTab[i]; | |
| 272 | + while o # nil do | |
| 273 | + if (o^.scope = s) and (o^.name = obj^.name) then | |
| 274 | + Diag.error(obj^.pos, Diag.say("{} is declared twice", [Fmt.str(Scan.nameOf(obj^.name))])); | |
| 275 | + Diag.note(o^.pos, "the first one is here"); | |
| 276 | + return Scan.Failed; | |
| 277 | + end; | |
| 278 | + i := (i + 1) & hashMask; | |
| 279 | + o := hashTab[i]; | |
| 280 | + end; | |
| 281 | + obj^.scope := s; | |
| 282 | + hashTab[i] := obj; | |
| 283 | + INC(hashCount); | |
| 284 | + if hashCount * 2 > hashMask then rehash; end; | |
| 285 | + return nil; | |
| 286 | +end; | |
| 287 | + | |
| 288 | +proc find*(s, name: INT): ^Obj; | |
| 289 | +var i: INT; o: ^Obj; | |
| 290 | +begin | |
| 291 | + i := slot(s, name); | |
| 292 | + o := hashTab[i]; | |
| 293 | + while o # nil do | |
| 294 | + if (o^.scope = s) and (o^.name = name) then return o; end; | |
| 295 | + i := (i + 1) & hashMask; | |
| 296 | + o := hashTab[i]; | |
| 297 | + end; | |
| 298 | + return nil; | |
| 299 | +end; | |
| 300 | + | |
| 301 | +proc lookup*(name: INT): ^Obj; | |
| 302 | +var i: INT; o: ^Obj; | |
| 303 | +begin | |
| 304 | + for i := depth - 1 to 0 by -1 do | |
| 305 | + o := find(scopes[i], name); | |
| 306 | + if o # nil then return o; end; | |
| 307 | + end; | |
| 308 | + return nil; | |
| 309 | +end; | |
| 310 | + | |
| 311 | +proc min(a, b: INT): INT; | |
| 312 | +begin | |
| 313 | + if a < b then return a; end; | |
| 314 | + return b; | |
| 315 | +end; | |
| 316 | + | |
| 317 | +(* c, in capitals if it is a letter *) | |
| 318 | +proc up(c: U8): U8; | |
| 319 | +begin | |
| 320 | + if (c >= 97) and (c <= 122) then return c - 32; end; | |
| 321 | + return c; | |
| 322 | +end; | |
| 323 | + | |
| 324 | +(* How far apart two names are: the edits, a character put in, taken out, | |
| 325 | + changed or swapped with the next, that make one the other, ignoring | |
| 326 | + case. *) | |
| 327 | +proc distance(a, b: []U8): INT; | |
| 328 | +var d: [Scan.MaxIdent + 2][Scan.MaxIdent + 2]INT; i, j, c: INT; | |
| 329 | +begin | |
| 330 | + for i := 0 to LEN(a) do d[i][0] := i; end; | |
| 331 | + for j := 0 to LEN(b) do d[0][j] := j; end; | |
| 332 | + for i := 1 to LEN(a) do | |
| 333 | + for j := 1 to LEN(b) do | |
| 334 | + c := 1; | |
| 335 | + if up(a[i - 1]) = up(b[j - 1]) then c := 0; end; | |
| 336 | + d[i][j] := min(min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + c); | |
| 337 | + if (i > 1) and (j > 1) and (up(a[i - 1]) = up(b[j - 2])) | |
| 338 | + and (up(a[i - 2]) = up(b[j - 1])) then | |
| 339 | + d[i][j] := min(d[i][j], d[i - 2][j - 2] + 1); | |
| 340 | + end; | |
| 341 | + end; | |
| 342 | + end; | |
| 343 | + return d[LEN(a)][LEN(b)]; | |
| 344 | +end; | |
| 345 | + | |
| 346 | +(* The name spelled most like name, for "did you mean": in scope s, if | |
| 347 | + it can be reached from here, or in the scopes in reach if s < 0; nil | |
| 348 | + if none is within an edit for every three characters. Only after an | |
| 349 | + error, so it looks at every object. *) | |
| 350 | +proc closest*(name, s: INT): ^Obj; | |
| 351 | +var i, j, d, best: INT; o, found: ^Obj; n: []U8; reach: BOOL; | |
| 352 | +begin | |
| 353 | + n := Scan.nameOf(name); | |
| 354 | + best := LEN(n) div 3 + 1; | |
| 355 | + found := nil; | |
| 356 | + for i := 0 to hashMask do | |
| 357 | + o := hashTab[i]; | |
| 358 | + if (o = nil) or (o^.name = name) or (o^.name < 0) or (o^.typ = tPoison) then | |
| 359 | + reach := false; | |
| 360 | + elsif s >= 0 then | |
| 361 | + reach := (o^.scope = s) and (o^.exported or (o^.home = curModule)); | |
| 362 | + else | |
| 363 | + reach := false; | |
| 364 | + for j := 0 to depth - 1 do reach := reach or (o^.scope = scopes[j]); end; | |
| 365 | + end; | |
| 366 | + if reach then | |
| 367 | + d := distance(n, Scan.nameOf(o^.name)); | |
| 368 | + if d < best then best := d; found := o; end; | |
| 369 | + end; | |
| 370 | + end; | |
| 371 | + return found; | |
| 372 | +end; | |
| 373 | + | |
| 374 | +(* Types *) | |
| 375 | + | |
| 376 | +proc alignUp*(x, a: I64): I64; | |
| 377 | +begin | |
| 378 | + return (x + a - 1) div a * a; | |
| 379 | +end; | |
| 380 | + | |
| 381 | +proc isInt*(t: ^Type): BOOL; | |
| 382 | +begin | |
| 383 | + return t^.form in {fInt, fConst}; | |
| 384 | +end; | |
| 385 | + | |
| 386 | +proc isScalar*(t: ^Type): BOOL; | |
| 387 | +begin | |
| 388 | + return t^.form in {fBool, fInt, fReal, fConst, fNil, fPtr, fProc, fEnum, fSet, fError}; | |
| 389 | +end; | |
| 390 | + | |
| 391 | +proc isAggregate*(t: ^Type): BOOL; | |
| 392 | +begin | |
| 393 | + return t^.form in {fArray, fSlice, fRecord, fVariant}; | |
| 394 | +end; | |
| 395 | + | |
| 396 | +(* Whether a result field f is a single value of at most a word, not a real *) | |
| 397 | +proc oneWord(f: ^Obj): BOOL; | |
| 398 | +begin | |
| 399 | + return isScalar(f^.typ) and (f^.typ^.form # fReal) and (f^.typ^.size <= 8); | |
| 400 | +end; | |
| 401 | + | |
| 402 | +(* Whether a procedure's result t is two words, which come back in rax | |
| 403 | + and rdx, as System V returns a struct of two integers: a slice (its | |
| 404 | + pointer, then its length), or two results that are each a single | |
| 405 | + value of 8 bytes at most and not a real. *) | |
| 406 | +proc inTwoRegs*(t: ^Type): BOOL; | |
| 407 | +begin | |
| 408 | + if t^.form = fSlice then return true; end; | |
| 409 | + return t^.tuple and (t^.len = 2) and oneWord(t^.fields) and oneWord(t^.fields^.next); | |
| 410 | +end; | |
| 411 | + | |
| 412 | +(* Whether a procedure's result t comes back in memory the caller | |
| 413 | + provides, whose address the caller passes as a hidden first argument, | |
| 414 | + in rdi: any result that is neither one value, in rax or xmm0, nor two | |
| 415 | + words, in rax and rdx. *) | |
| 416 | +proc behindPointer*(t: ^Type): BOOL; | |
| 417 | +begin | |
| 418 | + return isAggregate(t) and not inTwoRegs(t); | |
| 419 | +end; | |
| 420 | + | |
| 421 | +proc fits*(v: I64; t: ^Type): BOOL; | |
| 422 | +begin | |
| 423 | + if t^.form # fInt then return t^.form = fConst; end; | |
| 424 | + match t^.size of | |
| 425 | + | 1: | |
| 426 | + if t^.signed then return (v >= -128) and (v <= 127); end; | |
| 427 | + return (v >= 0) and (v <= 255); | |
| 428 | + | 2: | |
| 429 | + if t^.signed then return (v >= -32768) and (v <= 32767); end; | |
| 430 | + return (v >= 0) and (v <= 65535); | |
| 431 | + | 4: | |
| 432 | + if t^.signed then return (v >= -2147483648) and (v <= 2147483647); end; | |
| 433 | + return (v >= 0) and (v <= 4294967295); | |
| 434 | + | else: return t^.signed or (v >= 0); | |
| 435 | + end; | |
| 436 | +end; | |
| 437 | + | |
| 438 | +proc same*(a, b: ^Type): BOOL; forward; | |
| 439 | + | |
| 440 | +(* Whether two procedure types, or two tuples, are alike: the same | |
| 441 | + results and parameters, in the same modes *) | |
| 442 | +proc alikeShape(a, b: ^Type): BOOL; | |
| 443 | +var p, q: ^Obj; | |
| 444 | +begin | |
| 445 | + (* a record is the same only as itself, but for several results *) | |
| 446 | + if (a^.form = fRecord) and not (a^.tuple and b^.tuple) then return false; end; | |
| 447 | + if (a^.len # b^.len) or (a^.variadic # b^.variadic) then return false; end; | |
| 448 | + if (a^.base = nil) # (b^.base = nil) then return false; end; | |
| 449 | + if (a^.base # nil) and not same(a^.base, b^.base) then return false; end; | |
| 450 | + p := a^.fields; q := b^.fields; | |
| 451 | + while p # nil do | |
| 452 | + if (p^.isVar # q^.isVar) or not same(p^.typ, q^.typ) then return false; end; | |
| 453 | + p := p^.next; q := q^.next; | |
| 454 | + end; | |
| 455 | + return true; | |
| 456 | +end; | |
| 457 | + | |
| 458 | +(* Structural sameness of two types. Named types are the same only if | |
| 459 | + they are one declaration; a type with no name is the same as another | |
| 460 | + built alike. *) | |
| 461 | +proc alike(a, b: ^Type): BOOL; | |
| 462 | +begin | |
| 463 | + if a = b then return true; end; | |
| 464 | + if a^.form # b^.form then return false; end; | |
| 465 | + match a^.form of | |
| 466 | + | fPtr: return (a^.vol = b^.vol) and same(a^.base, b^.base); | |
| 467 | + | fArray: return (a^.len = b^.len) and same(a^.base, b^.base); | |
| 468 | + | fSlice: return same(a^.base, b^.base); | |
| 469 | + | fSet: return same(a^.base, b^.base); | |
| 470 | + | fProc: return alikeShape(a, b); | |
| 471 | + | fRecord: return alikeShape(a, b); | |
| 472 | + | else: return false; | |
| 473 | + end; | |
| 474 | +end; | |
| 475 | + | |
| 476 | +proc same*(a, b: ^Type): BOOL; | |
| 477 | +begin | |
| 478 | + if a = b then return true; end; | |
| 479 | + if (a = nil) or (b = nil) then return false; end; | |
| 480 | + if (a^.obj # nil) and (b^.obj # nil) then return false; end; | |
| 481 | + return alike(a, b); | |
| 482 | +end; | |
| 483 | + | |
| 484 | +(* What a type is called, added to b: the names a program gives it, else | |
| 485 | + what it is made of *) | |
| 486 | +proc typeText(b: ^Buf.Buf; t: ^Type); | |
| 487 | +var f: ^Obj; | |
| 488 | +begin | |
| 489 | + if t = nil then Buf.str(b, "nothing"); return; end; | |
| 490 | + if t^.obj # nil then | |
| 491 | + if (t^.obj^.home # nil) and (t^.obj^.home # curModule) then | |
| 492 | + Buf.str(b, Scan.nameOf(t^.obj^.home^.sname)); | |
| 493 | + Buf.str(b, "."); | |
| 494 | + end; | |
| 495 | + Buf.str(b, Scan.nameOf(t^.obj^.name)); | |
| 496 | + return; | |
| 497 | + end; | |
| 498 | + match t^.form of | |
| 499 | + | fConst: Buf.str(b, "an integer constant"); | |
| 500 | + | fReal: Buf.str(b, "a real constant"); | |
| 501 | + | fNil: Buf.str(b, "nil"); | |
| 502 | + | fStr: Buf.str(b, "a string"); | |
| 503 | + | fPtr: | |
| 504 | + if t^.vol then Buf.str(b, "^VOLATILE "); else Buf.str(b, "^"); end; | |
| 505 | + typeText(b, t^.base); | |
| 506 | + | fArray: | |
| 507 | + Fmt.write(b, "[{}]", [Fmt.int(t^.len)]); | |
| 508 | + typeText(b, t^.base); | |
| 509 | + | fSlice: | |
| 510 | + Buf.str(b, "[]"); | |
| 511 | + typeText(b, t^.base); | |
| 512 | + | fRecord: | |
| 513 | + if not t^.tuple then | |
| 514 | + Buf.str(b, "record"); | |
| 515 | + else | |
| 516 | + Buf.str(b, "("); | |
| 517 | + f := t^.fields; | |
| 518 | + while f # nil do | |
| 519 | + typeText(b, f^.typ); | |
| 520 | + f := f^.next; | |
| 521 | + if f # nil then Buf.str(b, ", "); end; | |
| 522 | + end; | |
| 523 | + Buf.str(b, ")"); | |
| 524 | + end; | |
| 525 | + | fVariant: Buf.str(b, "variant"); | |
| 526 | + | fProc: | |
| 527 | + Buf.str(b, "proc("); | |
| 528 | + f := t^.fields; | |
| 529 | + while f # nil do | |
| 530 | + if f^.isVar then Buf.str(b, "var "); end; | |
| 531 | + typeText(b, f^.typ); | |
| 532 | + f := f^.next; | |
| 533 | + if f # nil then Buf.str(b, ", "); end; | |
| 534 | + end; | |
| 535 | + Buf.str(b, ")"); | |
| 536 | + if t^.base # nil then | |
| 537 | + Buf.str(b, ": "); | |
| 538 | + typeText(b, t^.base); | |
| 539 | + end; | |
| 540 | + | else: Buf.str(b, "?"); | |
| 541 | + end; | |
| 542 | +end; | |
| 543 | + | |
| 544 | +(* A name for t, kept for good, as every message is *) | |
| 545 | +proc typeName*(t: ^Type): []U8; | |
| 546 | +var b: ^Buf.Buf; | |
| 547 | +begin | |
| 548 | + b := ^Buf.Buf(Scan.grab(SIZE(Buf.Buf))); | |
| 549 | + Buf.init(b, Heap.heap); | |
| 550 | + typeText(b, t); | |
| 551 | + return Buf.bytes(b); | |
| 552 | +end; | |
| 553 | + | |
| 554 | +(* A fingerprint of what a declaration exports, for the interface | |
| 555 | + check: two files of one module export the same thing when their | |
| 556 | + fingerprints agree. A named type counts by its name, so that a type | |
| 557 | + declared in each file stands for the other's. *) | |
| 558 | +proc mix(var h: U64; v: I64); | |
| 559 | +begin | |
| 560 | + h := (h xor U64(v)) * 1099511628211; (* FNV-1a's 64-bit prime *) | |
| 561 | +end; | |
| 562 | + | |
| 563 | +proc bit(f: BOOL): INT; | |
| 564 | +begin | |
| 565 | + if f then return 1; end; | |
| 566 | + return 0; | |
| 567 | +end; | |
| 568 | + | |
| 569 | +proc shape(var h: U64; t: ^Type; top: BOOL); | |
| 570 | +var f, g: ^Obj; | |
| 571 | +begin | |
| 572 | + if t = nil then mix(h, -1); return; end; | |
| 573 | + if (t^.obj # nil) and not top then | |
| 574 | + mix(h, t^.obj^.name); | |
| 575 | + if t^.obj^.home # nil then mix(h, t^.obj^.home^.sname); end; | |
| 576 | + return; | |
| 577 | + end; | |
| 578 | + mix(h, INT(t^.form)); | |
| 579 | + mix(h, bit(t^.vol) + 2 * bit(t^.variadic)); | |
| 580 | + mix(h, t^.len); | |
| 581 | + if t^.form in {fPtr, fArray, fSlice, fProc, fSet} then shape(h, t^.base, false); end; | |
| 582 | + f := t^.fields; | |
| 583 | + while f # nil do | |
| 584 | + if (t^.form # fRecord) or f^.exported then | |
| 585 | + mix(h, f^.name); | |
| 586 | + mix(h, bit(f^.isVar)); | |
| 587 | + shape(h, f^.typ, false); | |
| 588 | + g := f^.fields; | |
| 589 | + while g # nil do shape(h, g^.typ, false); g := g^.next; end; | |
| 590 | + end; | |
| 591 | + f := f^.next; | |
| 592 | + end; | |
| 593 | +end; | |
| 594 | + | |
| 595 | +proc fingerprint*(o: ^Obj): U64; | |
| 596 | +var h: U64; f: ^Obj; | |
| 597 | +begin | |
| 598 | + h := 0CBF29CE484222325H; (* FNV-1a's 64-bit offset basis *) | |
| 599 | + mix(h, INT(o^.cls)); | |
| 600 | + if o^.cls = oCase then | |
| 601 | + mix(h, o^.val); | |
| 602 | + f := o^.fields; | |
| 603 | + while f # nil do shape(h, f^.typ, false); f := f^.next; end; | |
| 604 | + else | |
| 605 | + shape(h, o^.typ, o^.cls = oType); | |
| 606 | + end; | |
| 607 | + return h; | |
| 608 | +end; | |
| 609 | + | |
| 610 | +(* The universe *) | |
| 611 | + | |
| 612 | +proc basic(name: []U8; form: Form; size: I64; signed: BOOL): ^Type; | |
| 613 | +var t: ^Type; o: ^Obj; | |
| 614 | +begin | |
| 615 | + t := newType(form, size, size); | |
| 616 | + t^.signed := signed; | |
| 617 | + o := newObj(oType, Scan.intern(ADR(name[0]), LEN(name)), t); | |
| 618 | + o^.exported := true; | |
| 619 | + t^.obj := o; | |
| 620 | + _ := insert(o, 0); | |
| 621 | + return t; | |
| 622 | +end; | |
| 623 | + | |
| 624 | +proc enter(name: []U8; cls: Class; t: ^Type; v: I64); | |
| 625 | +var o: ^Obj; | |
| 626 | +begin | |
| 627 | + o := newObj(cls, Scan.intern(ADR(name[0]), LEN(name)), t); | |
| 628 | + o^.val := v; | |
| 629 | + o^.exported := true; | |
| 630 | + _ := insert(o, 0); | |
| 631 | +end; | |
| 632 | + | |
| 633 | +begin | |
| 634 | + hashMask := (1 shl 14) - 1; | |
| 635 | + hashTab := emptyTable(hashMask + 1); | |
| 636 | + depth := 1; | |
| 637 | + scopes[0] := 0; | |
| 638 | + nextScope := 0; | |
| 639 | + curModule := nil; | |
| 640 | + tI8 := basic("I8", fInt, 1, true); | |
| 641 | + tI16 := basic("I16", fInt, 2, true); | |
| 642 | + tI32 := basic("I32", fInt, 4, true); | |
| 643 | + tI64 := basic("I64", fInt, 8, true); | |
| 644 | + tU8 := basic("U8", fInt, 1, false); | |
| 645 | + tU16 := basic("U16", fInt, 2, false); | |
| 646 | + tU32 := basic("U32", fInt, 4, false); | |
| 647 | + tU64 := basic("U64", fInt, 8, false); | |
| 648 | + tInt := basic("INT", fInt, 8, true); | |
| 649 | + tUInt := basic("UINT", fInt, 8, false); | |
| 650 | + tBool := basic("BOOL", fBool, 1, false); | |
| 651 | + tF32 := basic("F32", fReal, 4, false); | |
| 652 | + tReal := basic("F64", fReal, 8, false); | |
| 653 | + tRConst := newType(fReal, 8, 8); | |
| 654 | + tError := basic("ERROR", fError, 8, false); | |
| 655 | + tConst := newType(fConst, 8, 8); | |
| 656 | + tConst^.signed := true; | |
| 657 | + tNil := newType(fNil, 8, 8); | |
| 658 | + tStr := newType(fStr, 0, 1); | |
| 659 | + tNone := newType(fNone, 0, 1); | |
| 660 | + tPtrU8 := newType(fPtr, 8, 8); | |
| 661 | + tPtrU8^.base := tU8; | |
| 662 | + tBytes := newType(fSlice, 16, 8); | |
| 663 | + tBytes^.base := tU8; | |
| 664 | + tPoison := newType(fInt, 8, 8); | |
| 665 | + tPoison^.signed := true; | |
| 666 | + enter("true", oConst, tBool, 1); | |
| 667 | + enter("false", oConst, tBool, 0); | |
| 668 | + enter("nil", oConst, tNil, 0); | |
| 669 | + enter("SIZE", oBuiltin, nil, bSize); | |
| 670 | + enter("ADR", oBuiltin, nil, bAdr); | |
| 671 | + enter("LEN", oBuiltin, nil, bLen); | |
| 672 | + enter("NEW", oBuiltin, nil, bNew); | |
| 673 | + enter("MSG", oBuiltin, nil, bMsg); | |
| 674 | + enter("SYSCALL", oBuiltin, nil, bSyscall); | |
| 675 | + enter("FENCE", oBuiltin, nil, bFence); | |
| 676 | + enter("CAS", oBuiltin, nil, bCas); | |
| 677 | + enter("XCHG", oBuiltin, nil, bXchg); | |
| 678 | + enter("XADD", oBuiltin, nil, bXadd); | |
| 679 | + enter("ARGC", oBuiltin, nil, bArgc); | |
| 680 | + enter("ARGV", oBuiltin, nil, bArgv); | |
| 681 | + enter("INC", oBuiltin, nil, bInc); | |
| 682 | + enter("DEC", oBuiltin, nil, bDec); | |
| 683 | + enter("COPY", oBuiltin, nil, bCopy); | |
| 684 | +end Sym. |
| @@ -33,3 +33,24 @@ | ||
| 33 | 33 | printf 'a bad b bad bad c\nd bad\n' >$b/rec.raz |
| 34 | 34 | r=$($b/recover $b/rec.raz 2>/dev/null) || test $? = 1 |
| 35 | 35 | test "$r" = "8 names, 4 failed, 4 errors" && echo "recover ok" |
| 36 | + | |
| 37 | +# Chunk and Sym: a fixed pseudo-random script each, run by both sides, | |
| 38 | +# which print all they have made or answered; Sym also ends, as the | |
| 39 | +# argument says, in an error, whose message and status must agree | |
| 40 | +for u in Chunk Sym; do | |
| 41 | + l=$(echo $u | tr A-Z a-z) | |
| 42 | + fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fusrc -o$b/${l}dump.seed self/test/${u}Dump.pas >/dev/null | |
| 43 | + ./raz -Werror -I self -o $b/${l}dump.self self/test/${u}Dump.raz | |
| 44 | +done | |
| 45 | +for a in "" dup deep; do | |
| 46 | + for s in seed self; do | |
| 47 | + t=0; $b/chunkdump.$s >$b/chunk.$s 2>/dev/null || t=$? | |
| 48 | + t2=0; $b/symdump.$s $a >$b/sym.$s.out 2>$b/sym.$s.err || t2=$? | |
| 49 | + echo "$t $t2" >$b/status.$s | |
| 50 | + done | |
| 51 | + cmp $b/status.seed $b/status.self | |
| 52 | + cmp $b/chunk.seed $b/chunk.self | |
| 53 | + cmp $b/sym.seed.out $b/sym.self.out | |
| 54 | + cmp $b/sym.seed.err $b/sym.self.err | |
| 55 | +done | |
| 56 | +echo "chunk and sym ok" |
| @@ -0,0 +1,89 @@ | ||
| 1 | +{ Makes a program of chunks by a fixed pseudo-random script and prints | |
| 2 | + all of it: the seed's side of a test of self/Chunk.raz, whose | |
| 3 | + ChunkDump.raz does the same and prints the same. } | |
| 4 | +program chunkdump; | |
| 5 | +{$mode objfpc}{$H-} | |
| 6 | +uses diag, scan, chunk; | |
| 7 | +var | |
| 8 | + seed: int64; | |
| 9 | + names: array[0..7] of longint; | |
| 10 | + i, k, n, op, c, sum, open, x, y, z: longint; | |
| 11 | + b: array[0..63] of byte; | |
| 12 | + | |
| 13 | +function Rnd(n: longint): longint; | |
| 14 | +begin | |
| 15 | + seed := (seed * 1103515245 + 12345) and $7FFFFFFF; | |
| 16 | + Rnd := (seed shr 8) mod n; | |
| 17 | +end; | |
| 18 | + | |
| 19 | +begin | |
| 20 | + seed := 7; | |
| 21 | + for i := 0 to 7 do begin b[0] := 97 + i; names[i] := Intern(@b[0], 1) end; | |
| 22 | + open := -1; | |
| 23 | + sum := 0; | |
| 24 | + for k := 1 to 30000 do begin | |
| 25 | + op := Rnd(16); | |
| 26 | + case op of | |
| 27 | + 0..3: Put1(Rnd(256)); | |
| 28 | + 4: begin x := Rnd(1 shl 30); y := Rnd(1 shl 30); Put4(x - y) end; | |
| 29 | + 5: begin x := Rnd(1 shl 30); y := Rnd(1 shl 30); Put8(int64(x) * 4096 - y) end; | |
| 30 | + 6: begin | |
| 31 | + if open >= 0 then EndChunk(open); | |
| 32 | + x := Rnd(5); y := Rnd(9); | |
| 33 | + open := NewChunk(secText, x - 1, y - 1); | |
| 34 | + BeginChunk(open); | |
| 35 | + end; | |
| 36 | + 7: begin x := Rnd(8); curFile := names[x]; x := Rnd(300); y := Rnd(80); NoteLine(x, y) end; | |
| 37 | + 8: if numChunks > 0 then begin x := Rnd(numChunks); y := Rnd(100); AddFixup(pc, x, y - 50) end; | |
| 38 | + 9: if (open >= 0) and (pc - chunks^[open].start > 8) then begin x := Rnd(6); TakeBack(x) end; | |
| 39 | + 10: begin | |
| 40 | + x := Rnd(5); y := Rnd(9); | |
| 41 | + c := NewChunk(secRodata, x - 1, y - 1); | |
| 42 | + n := Rnd(64); | |
| 43 | + for i := 0 to n - 1 do b[i] := Rnd(256); | |
| 44 | + x := Rnd(4); | |
| 45 | + SetData(c, @b[0], n, 1 shl x); | |
| 46 | + n := Rnd(4); | |
| 47 | + for i := 1 to n do begin x := Rnd(8); y := Rnd(numChunks); z := Rnd(10); AddDataFixup(c, x * 8, y, z) end; | |
| 48 | + end; | |
| 49 | + 11: begin | |
| 50 | + x := Rnd(5); y := Rnd(9); | |
| 51 | + c := NewChunk(secBss, x - 1, y - 1); | |
| 52 | + x := Rnd(100); y := Rnd(4); | |
| 53 | + SetBss(c, x, 1 shl y); | |
| 54 | + end; | |
| 55 | + 12: begin x := Rnd(8); c := ExternChunk(names[x]) end; | |
| 56 | + 13: if numChunks > 0 then begin | |
| 57 | + x := Rnd(numChunks); y := Rnd(8); z := Rnd(200); n := Rnd(5); | |
| 58 | + NoteVar(x, names[y], z - 100, n); | |
| 59 | + end; | |
| 60 | + 14: if pc >= 8 then begin | |
| 61 | + x := Rnd(1 shl 30); y := Rnd(1 shl 30); | |
| 62 | + Put4At(pc - 8, x - y); | |
| 63 | + sum := (sum + Get4At(pc - 8)) and $FFFFFF; | |
| 64 | + end; | |
| 65 | + 15: if numChunks > 0 then begin | |
| 66 | + x := Rnd(numChunks); chunks^[x].live := true; | |
| 67 | + x := Rnd(numChunks); y := Rnd(1 shl 30); chunks^[x].adr := y; | |
| 68 | + end; | |
| 69 | + end; | |
| 70 | + end; | |
| 71 | + if open >= 0 then EndChunk(open); | |
| 72 | + writeln('sum ', sum, ' pc ', pc, ' rolen ', rolen); | |
| 73 | + for i := 0 to numChunks - 1 do | |
| 74 | + with chunks^[i] do | |
| 75 | + writeln('C ', i, ' ', ord(sec), ' ', start, ' ', size, ' ', align, ' ', firstFix, ' ', numFix, ' ', | |
| 76 | + module, ' ', name, ' ', adr, ' ', ord(live), ' ', ord(ext), ' ', ChunkName(i)); | |
| 77 | + for i := 0 to numFixups - 1 do | |
| 78 | + writeln('F ', fixups^[i].at, ' ', fixups^[i].chunk, ' ', fixups^[i].addend); | |
| 79 | + for i := 0 to numDataFixups - 1 do | |
| 80 | + writeln('D ', dataFixups^[i].at, ' ', dataFixups^[i].chunk, ' ', dataFixups^[i].addend); | |
| 81 | + for i := 0 to numLines - 1 do | |
| 82 | + writeln('L ', lines^[i].at, ' ', lines^[i].chunk, ' ', lines^[i].path, ' ', lines^[i].line, ' ', lines^[i].col); | |
| 83 | + for i := 0 to numVarNotes - 1 do | |
| 84 | + writeln('V ', varNotes[i].chunk, ' ', varNotes[i].name, ' ', varNotes[i].off, ' ', varNotes[i].kind); | |
| 85 | + for i := 0 to pc - 1 do write(HexStr(code^[i], 2)); | |
| 86 | + writeln; | |
| 87 | + for i := 0 to rolen - 1 do write(HexStr(rodata^[i], 2)); | |
| 88 | + writeln; | |
| 89 | +end. |
| @@ -0,0 +1,118 @@ | ||
| 1 | +(* Makes a program of chunks by a fixed pseudo-random script and prints | |
| 2 | + all of it: self/Chunk.raz's side of a test against the seed's, | |
| 3 | + ChunkDump.pas, which prints the same. *) | |
| 4 | +module ChunkDump; | |
| 5 | +import Out, Fmt, Scan, Chunk; | |
| 6 | + | |
| 7 | +var seed: I64; | |
| 8 | + | |
| 9 | +proc rnd(n: INT): INT; | |
| 10 | +begin | |
| 11 | + seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; | |
| 12 | + return INT((seed shr 8) mod I64(n)); | |
| 13 | +end; | |
| 14 | + | |
| 15 | +(* a flag, as the number the seed's side prints for it *) | |
| 16 | +proc bit(f: BOOL): INT; | |
| 17 | +begin | |
| 18 | + if f then return 1; end; | |
| 19 | + return 0; | |
| 20 | +end; | |
| 21 | + | |
| 22 | +proc main*; | |
| 23 | +var | |
| 24 | + names: [8]INT; b: [64]U8; | |
| 25 | + i, k, n, op, c, sum, open, x, y, z: INT; | |
| 26 | +begin | |
| 27 | + seed := 7; | |
| 28 | + for i := 0 to 7 do b[0] := U8(97 + i); names[i] := Scan.intern(ADR(b[0]), 1); end; | |
| 29 | + open := -1; | |
| 30 | + sum := 0; | |
| 31 | + for k := 1 to 30000 do | |
| 32 | + op := rnd(16); | |
| 33 | + match op of | |
| 34 | + | 0..3: Chunk.put1(rnd(256)); | |
| 35 | + | 4: x := rnd(1 shl 30); y := rnd(1 shl 30); Chunk.put4(x - y); | |
| 36 | + | 5: x := rnd(1 shl 30); y := rnd(1 shl 30); Chunk.put8(I64(x) * 4096 - I64(y)); | |
| 37 | + | 6: | |
| 38 | + if open >= 0 then Chunk.endChunk(open); end; | |
| 39 | + x := rnd(5); y := rnd(9); | |
| 40 | + open := Chunk.newChunk(Chunk.secText, x - 1, y - 1); | |
| 41 | + Chunk.beginChunk(open); | |
| 42 | + | 7: | |
| 43 | + x := rnd(8); Chunk.setCurFile(names[x]); | |
| 44 | + x := rnd(300); y := rnd(80); Chunk.noteLine(x, y); | |
| 45 | + | 8: | |
| 46 | + if Chunk.numChunks > 0 then | |
| 47 | + x := rnd(Chunk.numChunks); y := rnd(100); Chunk.addFixup(Chunk.pc, x, y - 50); | |
| 48 | + end; | |
| 49 | + | 9: | |
| 50 | + if (open >= 0) and (Chunk.pc - Chunk.chunks[open].start > 8) then | |
| 51 | + x := rnd(6); Chunk.takeBack(x); | |
| 52 | + end; | |
| 53 | + | 10: | |
| 54 | + x := rnd(5); y := rnd(9); | |
| 55 | + c := Chunk.newChunk(Chunk.secRodata, x - 1, y - 1); | |
| 56 | + n := rnd(64); | |
| 57 | + for i := 0 to n - 1 do b[i] := U8(rnd(256)); end; | |
| 58 | + x := rnd(4); | |
| 59 | + Chunk.setData(c, ADR(b[0]), n, 1 shl x); | |
| 60 | + n := rnd(4); | |
| 61 | + for i := 1 to n do | |
| 62 | + x := rnd(8); y := rnd(Chunk.numChunks); z := rnd(10); | |
| 63 | + Chunk.addDataFixup(c, x * 8, y, z); | |
| 64 | + end; | |
| 65 | + | 11: | |
| 66 | + x := rnd(5); y := rnd(9); | |
| 67 | + c := Chunk.newChunk(Chunk.secBss, x - 1, y - 1); | |
| 68 | + x := rnd(100); y := rnd(4); | |
| 69 | + Chunk.setBss(c, x, 1 shl y); | |
| 70 | + | 12: x := rnd(8); c := Chunk.externChunk(names[x]); | |
| 71 | + | 13: | |
| 72 | + if Chunk.numChunks > 0 then | |
| 73 | + x := rnd(Chunk.numChunks); y := rnd(8); z := rnd(200); n := rnd(5); | |
| 74 | + Chunk.noteVar(x, names[y], z - 100, n); | |
| 75 | + end; | |
| 76 | + | 14: | |
| 77 | + if Chunk.pc >= 8 then | |
| 78 | + x := rnd(1 shl 30); y := rnd(1 shl 30); | |
| 79 | + Chunk.put4At(Chunk.pc - 8, x - y); | |
| 80 | + sum := (sum + Chunk.get4At(Chunk.pc - 8)) & 0FFFFFFH; | |
| 81 | + end; | |
| 82 | + | else: | |
| 83 | + if Chunk.numChunks > 0 then | |
| 84 | + x := rnd(Chunk.numChunks); Chunk.setLive(x); | |
| 85 | + x := rnd(Chunk.numChunks); y := rnd(1 shl 30); Chunk.setAdr(x, y); | |
| 86 | + end; | |
| 87 | + end; | |
| 88 | + end; | |
| 89 | + if open >= 0 then Chunk.endChunk(open); end; | |
| 90 | + Fmt.print("sum {} pc {} rolen {}\n", [Fmt.int(sum), Fmt.int(Chunk.pc), Fmt.int(Chunk.rolen)]); | |
| 91 | + for i := 0 to Chunk.numChunks - 1 do | |
| 92 | + Fmt.print("C {} {} {} {} {} {} {} {} {} {} {} {} {}\n", [Fmt.int(i), Fmt.int(INT(Chunk.chunks[i].sec)), | |
| 93 | + Fmt.int(Chunk.chunks[i].start), Fmt.int(Chunk.chunks[i].size), Fmt.int(Chunk.chunks[i].align), | |
| 94 | + Fmt.int(Chunk.chunks[i].firstFix), Fmt.int(Chunk.chunks[i].numFix), Fmt.int(Chunk.chunks[i].home), | |
| 95 | + Fmt.int(Chunk.chunks[i].name), Fmt.int(Chunk.chunks[i].adr), | |
| 96 | + Fmt.int(bit(Chunk.chunks[i].live)), Fmt.int(bit(Chunk.chunks[i].ext)), Fmt.str(Chunk.chunkName(i))]); | |
| 97 | + end; | |
| 98 | + for i := 0 to Chunk.numFixups - 1 do | |
| 99 | + Fmt.print("F {} {} {}\n", [Fmt.int(Chunk.fixups[i].at), Fmt.int(Chunk.fixups[i].chunk), Fmt.int(Chunk.fixups[i].addend)]); | |
| 100 | + end; | |
| 101 | + for i := 0 to Chunk.numDataFixups - 1 do | |
| 102 | + Fmt.print("D {} {} {}\n", [Fmt.int(Chunk.dataFixups[i].at), Fmt.int(Chunk.dataFixups[i].chunk), Fmt.int(Chunk.dataFixups[i].addend)]); | |
| 103 | + end; | |
| 104 | + for i := 0 to Chunk.numLines - 1 do | |
| 105 | + Fmt.print("L {} {} {} {} {}\n", [Fmt.int(Chunk.lines[i].at), Fmt.int(Chunk.lines[i].chunk), | |
| 106 | + Fmt.int(Chunk.lines[i].path), Fmt.int(Chunk.lines[i].line), Fmt.int(Chunk.lines[i].col)]); | |
| 107 | + end; | |
| 108 | + for i := 0 to Chunk.numVarNotes - 1 do | |
| 109 | + Fmt.print("V {} {} {} {}\n", [Fmt.int(Chunk.varNotes[i].chunk), Fmt.int(Chunk.varNotes[i].name), | |
| 110 | + Fmt.int(Chunk.varNotes[i].off), Fmt.int(Chunk.varNotes[i].kind)]); | |
| 111 | + end; | |
| 112 | + for i := 0 to Chunk.pc - 1 do Out.hex(U64(Chunk.text[i]), 2); end; | |
| 113 | + Out.ln; | |
| 114 | + for i := 0 to Chunk.rolen - 1 do Out.hex(U64(Chunk.rodata[i]), 2); end; | |
| 115 | + Out.ln; | |
| 116 | +end; | |
| 117 | + | |
| 118 | +end ChunkDump. |
| @@ -0,0 +1,185 @@ | ||
| 1 | +{ Builds types and objects by a fixed pseudo-random script, asks of them | |
| 2 | + what the parser asks of sym, and prints the answers: the seed's side | |
| 3 | + of a test of self/Sym.raz, whose SymDump.raz does the same and prints | |
| 4 | + the same. With the argument dup or deep it ends in the error of a name | |
| 5 | + declared twice, or of too many scopes. } | |
| 6 | +program symdump; | |
| 7 | +{$mode objfpc}{$H-} | |
| 8 | +uses diag, scan, sym; | |
| 9 | +const | |
| 10 | + Words: array[0..19] of string = ('alpha', 'alpho', 'beta', 'bet', 'gamma', 'gama', 'delta', 'Delta', | |
| 11 | + 'eps', 'zeta', 'eta', 'theta', 'iota', 'kappa', 'lambda', 'mu', 'nu', 'xi', 'omicron', 'pi'); | |
| 12 | +var | |
| 13 | + seed: int64; | |
| 14 | + names: array[0..19] of longint; | |
| 15 | + mods: array[0..2] of PModule; | |
| 16 | + base: array[0..11] of PType; | |
| 17 | + pool: array[0..299] of PType; | |
| 18 | + objs: array[0..299] of PObj; | |
| 19 | + numPool, numObjs, i, k, n, op, x, y, z: longint; | |
| 20 | + t, o2: PType; | |
| 21 | + o: PObj; | |
| 22 | + saved: array[0..MaxDepth - 1] of longint; | |
| 23 | + d: longint; | |
| 24 | + v: int64; | |
| 25 | + | |
| 26 | +function Rnd(n: longint): longint; | |
| 27 | +begin | |
| 28 | + seed := (seed * 1103515245 + 12345) and $7FFFFFFF; | |
| 29 | + Rnd := (seed shr 8) mod n; | |
| 30 | +end; | |
| 31 | + | |
| 32 | +function Fields(m: longint; allowVar: boolean): PObj; | |
| 33 | +var f, first, last: PObj; j, x, y, z: longint; | |
| 34 | +begin | |
| 35 | + first := nil; last := nil; | |
| 36 | + for j := 1 to m do begin | |
| 37 | + x := Rnd(20); y := Rnd(numPool); | |
| 38 | + f := NewObj(oField, names[x], pool[y]); | |
| 39 | + z := Rnd(2); f^.exported := z = 0; | |
| 40 | + z := Rnd(3); f^.isVar := allowVar and (z = 0); | |
| 41 | + f^.val := j * 8; | |
| 42 | + if first = nil then first := f else last^.next := f; | |
| 43 | + last := f; | |
| 44 | + end; | |
| 45 | + Fields := first; | |
| 46 | +end; | |
| 47 | + | |
| 48 | +procedure Add(t: PType); | |
| 49 | +begin | |
| 50 | + if numPool <= High(pool) then begin pool[numPool] := t; inc(numPool) end; | |
| 51 | +end; | |
| 52 | + | |
| 53 | +procedure MakeType; | |
| 54 | +var t: PType; r, m, x: longint; f: PObj; | |
| 55 | +begin | |
| 56 | + r := Rnd(11); | |
| 57 | + case r of | |
| 58 | + 0: t := NewType(fPtr, 8, 8); | |
| 59 | + 1: t := NewType(fArray, 0, 1); | |
| 60 | + 2: t := NewType(fSlice, 16, 8); | |
| 61 | + 3: t := NewType(fRecord, 0, 8); | |
| 62 | + 4: t := NewType(fProc, 8, 8); | |
| 63 | + 5: t := NewType(fSet, 8, 8); | |
| 64 | + 6: t := NewType(fVariant, 16, 8); | |
| 65 | + 7: t := NewType(fEnum, 1, 1); | |
| 66 | + 8: t := NewType(fRecord, 16, 8); | |
| 67 | + 9: t := NewType(fProc, 8, 8); | |
| 68 | + else t := NewType(fPtr, 8, 8); | |
| 69 | + end; | |
| 70 | + x := Rnd(numPool); t^.base := pool[x]; | |
| 71 | + t^.len := Rnd(5); | |
| 72 | + x := Rnd(4); t^.vol := x = 0; | |
| 73 | + x := Rnd(4); t^.variadic := (t^.form = fProc) and (x = 0); | |
| 74 | + x := Rnd(2); t^.tuple := (t^.form = fRecord) and (x = 0); | |
| 75 | + if t^.form in [fRecord, fProc, fVariant] then begin | |
| 76 | + m := Rnd(4); | |
| 77 | + t^.fields := Fields(m, t^.form = fProc); | |
| 78 | + t^.len := m; | |
| 79 | + if t^.form = fRecord then t^.base := nil; | |
| 80 | + x := Rnd(2); | |
| 81 | + if (t^.form = fProc) and (x = 0) then t^.base := nil; | |
| 82 | + if t^.form = fVariant then begin | |
| 83 | + f := t^.fields; | |
| 84 | + while f <> nil do begin f^.cls := oCase; x := Rnd(3); f^.fields := Fields(x, false); f := f^.next end; | |
| 85 | + end; | |
| 86 | + end; | |
| 87 | + x := Rnd(3); | |
| 88 | + if x = 0 then begin | |
| 89 | + x := Rnd(20); t^.obj := NewObj(oType, names[x], t); | |
| 90 | + x := Rnd(3); t^.obj^.module := mods[x]; | |
| 91 | + end; | |
| 92 | + Add(t); | |
| 93 | +end; | |
| 94 | + | |
| 95 | +begin | |
| 96 | + seed := 11; | |
| 97 | + for i := 0 to 19 do names[i] := Intern(@Words[i][1], length(Words[i])); | |
| 98 | + for i := 0 to 2 do begin | |
| 99 | + New(mods[i]); FillChar(mods[i]^, SizeOf(TModule), 0); | |
| 100 | + mods[i]^.sname := names[i * 5]; | |
| 101 | + end; | |
| 102 | + curModule := mods[0]; | |
| 103 | + base[0] := tI8; base[1] := tI16; base[2] := tI32; base[3] := tI64; base[4] := tU8; | |
| 104 | + base[5] := tU16; base[6] := tU32; base[7] := tU64; base[8] := tBool; base[9] := tF32; | |
| 105 | + base[10] := tReal; base[11] := tInt; | |
| 106 | + for i := 0 to 11 do Add(base[i]); | |
| 107 | + if ParamCount >= 1 then begin if Open('lib/Rt.raz') <> '' then Fatal('no file'); Next; Next end; | |
| 108 | + numObjs := 0; | |
| 109 | + for k := 1 to 4000 do begin | |
| 110 | + op := Rnd(14); | |
| 111 | + case op of | |
| 112 | + 0, 1, 2: MakeType; | |
| 113 | + 3, 4: if numObjs <= High(objs) then begin | |
| 114 | + x := Rnd(8); y := Rnd(20); | |
| 115 | + if Find(x, names[y]) = nil then begin | |
| 116 | + z := Rnd(8); n := Rnd(numPool); | |
| 117 | + o := NewObj(TClass(z), names[y], pool[n]); | |
| 118 | + z := Rnd(2); o^.exported := z = 0; | |
| 119 | + z := Rnd(3); o^.module := mods[z]; | |
| 120 | + Insert(o, x); | |
| 121 | + objs[numObjs] := o; inc(numObjs); | |
| 122 | + writeln('I ', x, ' ', NameOf(names[y]), ' ', o^.scope); | |
| 123 | + end; | |
| 124 | + end; | |
| 125 | + 5: begin x := Rnd(8); y := Rnd(20); o := Find(x, names[y]); | |
| 126 | + write('F ', x, ' ', NameOf(names[y]), ' '); | |
| 127 | + if o = nil then writeln('-') else writeln(ord(o^.cls), ' ', o^.scope); | |
| 128 | + end; | |
| 129 | + 6: begin | |
| 130 | + x := Rnd(8); y := Rnd(20); | |
| 131 | + if ScopeDepth < 6 then OpenScope(x); | |
| 132 | + o := Lookup(names[y]); | |
| 133 | + write('L ', ScopeDepth, ' ', NameOf(names[y]), ' '); | |
| 134 | + if o = nil then writeln('-') else writeln(ord(o^.cls), ' ', o^.scope); | |
| 135 | + z := Rnd(2); | |
| 136 | + if (ScopeDepth > 1) and (z = 0) then CloseScope; | |
| 137 | + end; | |
| 138 | + 7: begin | |
| 139 | + y := Rnd(20); x := Rnd(9) - 1; z := Rnd(3); curModule := mods[z]; | |
| 140 | + o := Closest(names[y], x); | |
| 141 | + write('C ', NameOf(names[y]), ' ', x, ' '); | |
| 142 | + if o = nil then writeln('-') else writeln(NameOf(o^.name), ' ', o^.scope); | |
| 143 | + end; | |
| 144 | + 8: begin | |
| 145 | + x := Rnd(numPool); z := Rnd(3); curModule := mods[z]; | |
| 146 | + writeln('T ', TypeName(pool[x])); | |
| 147 | + end; | |
| 148 | + 9: begin | |
| 149 | + x := Rnd(numPool); y := Rnd(numPool); | |
| 150 | + writeln('S ', ord(Same(pool[x], pool[y])), ' ', ord(Same(pool[x], pool[x]))); | |
| 151 | + t := NewType(fPtr, 8, 8); t^.base := pool[x]; | |
| 152 | + o2 := t; t := NewType(fPtr, 8, 8); t^.base := pool[x]; | |
| 153 | + writeln('S ', ord(Same(o2, t))); | |
| 154 | + end; | |
| 155 | + 10: if numObjs > 0 then begin x := Rnd(numObjs); writeln('P ', HexStr(Fingerprint(objs[x]), 16)) end; | |
| 156 | + 11: begin | |
| 157 | + x := Rnd(numPool); y := Rnd(1 shl 20); z := Rnd(2); n := Rnd(2); | |
| 158 | + v := int64(y) * 4096 * z - y * n; | |
| 159 | + t := pool[x]; | |
| 160 | + writeln('B ', ord(Fits(v, t)), ' ', ord(IsInt(t)), ' ', ord(IsScalar(t)), ' ', ord(IsAggregate(t)), ' ', | |
| 161 | + ord(InTwoRegs(t)), ' ', ord(BehindPointer(t)), ' ', AlignUp(y, 1 shl z)); | |
| 162 | + end; | |
| 163 | + 12: if (ScopeDepth > 1) and (ScopeDepth < 6) then begin | |
| 164 | + d := ScopeDepth; SaveScopes(d, saved); | |
| 165 | + writeln('V ', ScopeDepth, ' ', TopScope); | |
| 166 | + RestoreScopes(d, saved); | |
| 167 | + writeln('V ', ScopeDepth, ' ', TopScope); | |
| 168 | + x := NewScope; writeln('N ', x); | |
| 169 | + end; | |
| 170 | + 13: begin x := ScopeDepth; if x > 3 then CutScopes(2); writeln('X ', x, ' ', ScopeDepth) end; | |
| 171 | + end; | |
| 172 | + end; | |
| 173 | + { the universe, and the sizes it made } | |
| 174 | + for i := 0 to 11 do writeln('U ', ord(base[i]^.form), ' ', base[i]^.size, ' ', ord(base[i]^.signed), ' ', NameOf(base[i]^.obj^.name)); | |
| 175 | + if ParamCount >= 1 then begin | |
| 176 | + if ParamStr(1) = 'dup' then begin | |
| 177 | + o := NewObj(oVar, names[3], tI8); | |
| 178 | + Insert(o, 100); Insert(o, 100); | |
| 179 | + end; | |
| 180 | + if ParamStr(1) = 'deep' then begin | |
| 181 | + CutScopes(1); | |
| 182 | + for i := 1 to MaxDepth + 1 do OpenScope(i); | |
| 183 | + end; | |
| 184 | + end; | |
| 185 | +end. |
| @@ -0,0 +1,225 @@ | ||
| 1 | +(* Builds types and objects by a fixed pseudo-random script, asks of them | |
| 2 | + what the parser asks of Sym, and prints the answers: self/Sym.raz's | |
| 3 | + side of a test against the seed's, SymDump.pas, which prints the same. | |
| 4 | + With the argument dup or deep it ends in the error of a name declared | |
| 5 | + twice, or of too many scopes. *) | |
| 6 | +module SymDump; | |
| 7 | +import Out, Fmt, Files, Heap, Diag, Scan, Sym; | |
| 8 | + | |
| 9 | +const | |
| 10 | + Words: [20][]U8 = ["alpha", "alpho", "beta", "bet", "gamma", "gama", "delta", "Delta", | |
| 11 | + "eps", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "omicron", "pi"]; | |
| 12 | + | |
| 13 | +var | |
| 14 | + seed: I64; | |
| 15 | + names: [20]INT; | |
| 16 | + mods: [3]^Sym.Module; | |
| 17 | + base: [12]^Sym.Type; | |
| 18 | + pool: [300]^Sym.Type; | |
| 19 | + objs: [300]^Sym.Obj; | |
| 20 | + classes: [8]Sym.Class; | |
| 21 | + numPool, numObjs: INT; | |
| 22 | + | |
| 23 | +proc rnd(n: INT): INT; | |
| 24 | +begin | |
| 25 | + seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; | |
| 26 | + return INT((seed shr 8) mod I64(n)); | |
| 27 | +end; | |
| 28 | + | |
| 29 | +proc bit(f: BOOL): INT; | |
| 30 | +begin | |
| 31 | + if f then return 1; end; | |
| 32 | + return 0; | |
| 33 | +end; | |
| 34 | + | |
| 35 | +proc nm(i: INT): []U8; | |
| 36 | +begin | |
| 37 | + return Scan.nameOf(names[i]); | |
| 38 | +end; | |
| 39 | + | |
| 40 | +(* The result of a call that must succeed *) | |
| 41 | +proc ok(e: ERROR); | |
| 42 | +begin | |
| 43 | + if e # nil then Diag.finish; end; | |
| 44 | +end; | |
| 45 | + | |
| 46 | +proc fields(m: INT; allowVar: BOOL): ^Sym.Obj; | |
| 47 | +var f, first, last: ^Sym.Obj; j, x, y, z: INT; | |
| 48 | +begin | |
| 49 | + first := nil; last := nil; | |
| 50 | + for j := 1 to m do | |
| 51 | + x := rnd(20); y := rnd(numPool); | |
| 52 | + f := Sym.newObj(Sym.oField, names[x], pool[y]); | |
| 53 | + z := rnd(2); f^.exported := z = 0; | |
| 54 | + z := rnd(3); f^.isVar := allowVar and (z = 0); | |
| 55 | + f^.val := j * 8; | |
| 56 | + if first = nil then first := f; else last^.next := f; end; | |
| 57 | + last := f; | |
| 58 | + end; | |
| 59 | + return first; | |
| 60 | +end; | |
| 61 | + | |
| 62 | +proc add(t: ^Sym.Type); | |
| 63 | +begin | |
| 64 | + if numPool < 300 then pool[numPool] := t; INC(numPool); end; | |
| 65 | +end; | |
| 66 | + | |
| 67 | +proc makeType; | |
| 68 | +var t: ^Sym.Type; r, m, x: INT; f: ^Sym.Obj; | |
| 69 | +begin | |
| 70 | + r := rnd(11); | |
| 71 | + match r of | |
| 72 | + | 0: t := Sym.newType(Sym.fPtr, 8, 8); | |
| 73 | + | 1: t := Sym.newType(Sym.fArray, 0, 1); | |
| 74 | + | 2: t := Sym.newType(Sym.fSlice, 16, 8); | |
| 75 | + | 3: t := Sym.newType(Sym.fRecord, 0, 8); | |
| 76 | + | 4: t := Sym.newType(Sym.fProc, 8, 8); | |
| 77 | + | 5: t := Sym.newType(Sym.fSet, 8, 8); | |
| 78 | + | 6: t := Sym.newType(Sym.fVariant, 16, 8); | |
| 79 | + | 7: t := Sym.newType(Sym.fEnum, 1, 1); | |
| 80 | + | 8: t := Sym.newType(Sym.fRecord, 16, 8); | |
| 81 | + | 9: t := Sym.newType(Sym.fProc, 8, 8); | |
| 82 | + | else: t := Sym.newType(Sym.fPtr, 8, 8); | |
| 83 | + end; | |
| 84 | + x := rnd(numPool); t^.base := pool[x]; | |
| 85 | + t^.len := rnd(5); | |
| 86 | + x := rnd(4); t^.vol := x = 0; | |
| 87 | + x := rnd(4); t^.variadic := (t^.form = Sym.fProc) and (x = 0); | |
| 88 | + x := rnd(2); t^.tuple := (t^.form = Sym.fRecord) and (x = 0); | |
| 89 | + if (t^.form = Sym.fRecord) or (t^.form = Sym.fProc) or (t^.form = Sym.fVariant) then | |
| 90 | + m := rnd(4); | |
| 91 | + t^.fields := fields(m, t^.form = Sym.fProc); | |
| 92 | + t^.len := m; | |
| 93 | + if t^.form = Sym.fRecord then t^.base := nil; end; | |
| 94 | + x := rnd(2); | |
| 95 | + if (t^.form = Sym.fProc) and (x = 0) then t^.base := nil; end; | |
| 96 | + if t^.form = Sym.fVariant then | |
| 97 | + f := t^.fields; | |
| 98 | + while f # nil do | |
| 99 | + f^.cls := Sym.oCase; | |
| 100 | + x := rnd(3); f^.fields := fields(x, false); | |
| 101 | + f := f^.next; | |
| 102 | + end; | |
| 103 | + end; | |
| 104 | + end; | |
| 105 | + x := rnd(3); | |
| 106 | + if x = 0 then | |
| 107 | + x := rnd(20); t^.obj := Sym.newObj(Sym.oType, names[x], t); | |
| 108 | + x := rnd(3); t^.obj^.home := mods[x]; | |
| 109 | + end; | |
| 110 | + add(t); | |
| 111 | +end; | |
| 112 | + | |
| 113 | +proc main*; | |
| 114 | +var | |
| 115 | + i, k, op, x, y, z, n, d: INT; t, o2: ^Sym.Type; o: ^Sym.Obj; v: I64; | |
| 116 | + saved: [Sym.MaxDepth]INT; arg: []U8; | |
| 117 | +begin | |
| 118 | + seed := 11; | |
| 119 | + classes := [Sym.oConst, Sym.oType, Sym.oVar, Sym.oProc, Sym.oModule, Sym.oField, Sym.oCase, Sym.oBuiltin]; | |
| 120 | + for i := 0 to 19 do names[i] := Scan.intern(ADR(Words[i][0]), LEN(Words[i])); end; | |
| 121 | + for i := 0 to 2 do | |
| 122 | + NEW(mods[i], Heap.heap); | |
| 123 | + mods[i]^.sname := names[i * 5]; | |
| 124 | + end; | |
| 125 | + Sym.setCurModule(mods[0]); | |
| 126 | + base[0] := Sym.tI8; base[1] := Sym.tI16; base[2] := Sym.tI32; base[3] := Sym.tI64; base[4] := Sym.tU8; | |
| 127 | + base[5] := Sym.tU16; base[6] := Sym.tU32; base[7] := Sym.tU64; base[8] := Sym.tBool; base[9] := Sym.tF32; | |
| 128 | + base[10] := Sym.tReal; base[11] := Sym.tInt; | |
| 129 | + for i := 0 to 11 do add(base[i]); end; | |
| 130 | + arg := ""; | |
| 131 | + if ARGC > 1 then arg := Files.arg(1); end; | |
| 132 | + if LEN(arg) > 0 then | |
| 133 | + if LEN(Scan.open("lib/Rt.raz")) # 0 then Diag.fatal("no file"); end; | |
| 134 | + Scan.next; Scan.next; | |
| 135 | + end; | |
| 136 | + numObjs := 0; | |
| 137 | + for k := 1 to 4000 do | |
| 138 | + op := rnd(14); | |
| 139 | + match op of | |
| 140 | + | 0, 1, 2: makeType; | |
| 141 | + | 3, 4: | |
| 142 | + if numObjs < 300 then | |
| 143 | + x := rnd(8); y := rnd(20); | |
| 144 | + if Sym.find(x, names[y]) = nil then | |
| 145 | + z := rnd(8); n := rnd(numPool); | |
| 146 | + o := Sym.newObj(classes[z], names[y], pool[n]); | |
| 147 | + z := rnd(2); o^.exported := z = 0; | |
| 148 | + z := rnd(3); o^.home := mods[z]; | |
| 149 | + ok(Sym.insert(o, x)); | |
| 150 | + objs[numObjs] := o; INC(numObjs); | |
| 151 | + Fmt.print("I {} {} {}\n", [Fmt.int(x), Fmt.str(nm(y)), Fmt.int(o^.scope)]); | |
| 152 | + end; | |
| 153 | + end; | |
| 154 | + | 5: | |
| 155 | + x := rnd(8); y := rnd(20); o := Sym.find(x, names[y]); | |
| 156 | + Fmt.print("F {} {} ", [Fmt.int(x), Fmt.str(nm(y))]); | |
| 157 | + if o = nil then Out.str("-\n"); | |
| 158 | + else Fmt.print("{} {}\n", [Fmt.int(INT(o^.cls)), Fmt.int(o^.scope)]); end; | |
| 159 | + | 6: | |
| 160 | + x := rnd(8); y := rnd(20); | |
| 161 | + if Sym.scopeDepth() < 6 then ok(Sym.openScope(x)); end; | |
| 162 | + o := Sym.lookup(names[y]); | |
| 163 | + Fmt.print("L {} {} ", [Fmt.int(Sym.scopeDepth()), Fmt.str(nm(y))]); | |
| 164 | + if o = nil then Out.str("-\n"); | |
| 165 | + else Fmt.print("{} {}\n", [Fmt.int(INT(o^.cls)), Fmt.int(o^.scope)]); end; | |
| 166 | + z := rnd(2); | |
| 167 | + if (Sym.scopeDepth() > 1) and (z = 0) then Sym.closeScope; end; | |
| 168 | + | 7: | |
| 169 | + y := rnd(20); x := rnd(9) - 1; z := rnd(3); Sym.setCurModule(mods[z]); | |
| 170 | + o := Sym.closest(names[y], x); | |
| 171 | + Fmt.print("C {} {} ", [Fmt.str(nm(y)), Fmt.int(x)]); | |
| 172 | + if o = nil then Out.str("-\n"); | |
| 173 | + else Fmt.print("{} {}\n", [Fmt.str(Scan.nameOf(o^.name)), Fmt.int(o^.scope)]); end; | |
| 174 | + | 8: | |
| 175 | + x := rnd(numPool); z := rnd(3); Sym.setCurModule(mods[z]); | |
| 176 | + Fmt.print("T {}\n", [Fmt.str(Sym.typeName(pool[x]))]); | |
| 177 | + | 9: | |
| 178 | + x := rnd(numPool); y := rnd(numPool); | |
| 179 | + Fmt.print("S {} {}\n", [Fmt.int(bit(Sym.same(pool[x], pool[y]))), Fmt.int(bit(Sym.same(pool[x], pool[x])))]); | |
| 180 | + t := Sym.newType(Sym.fPtr, 8, 8); t^.base := pool[x]; | |
| 181 | + o2 := t; t := Sym.newType(Sym.fPtr, 8, 8); t^.base := pool[x]; | |
| 182 | + Fmt.print("S {}\n", [Fmt.int(bit(Sym.same(o2, t)))]); | |
| 183 | + | 10: | |
| 184 | + if numObjs > 0 then | |
| 185 | + x := rnd(numObjs); | |
| 186 | + Out.str("P "); Out.hex(Sym.fingerprint(objs[x]), 16); Out.ln; | |
| 187 | + end; | |
| 188 | + | 11: | |
| 189 | + x := rnd(numPool); y := rnd(1 shl 20); z := rnd(2); n := rnd(2); | |
| 190 | + v := I64(y) * 4096 * I64(z) - I64(y) * I64(n); | |
| 191 | + t := pool[x]; | |
| 192 | + Fmt.print("B {} {} {} {} {} {} {}\n", [Fmt.int(bit(Sym.fits(v, t))), Fmt.int(bit(Sym.isInt(t))), | |
| 193 | + Fmt.int(bit(Sym.isScalar(t))), Fmt.int(bit(Sym.isAggregate(t))), Fmt.int(bit(Sym.inTwoRegs(t))), | |
| 194 | + Fmt.int(bit(Sym.behindPointer(t))), Fmt.int(Sym.alignUp(y, 1 shl z))]); | |
| 195 | + | 12: | |
| 196 | + if (Sym.scopeDepth() > 1) and (Sym.scopeDepth() < 6) then | |
| 197 | + d := Sym.saveScopes(saved); | |
| 198 | + Fmt.print("V {} {}\n", [Fmt.int(Sym.scopeDepth()), Fmt.int(Sym.topScope())]); | |
| 199 | + Sym.restoreScopes(d, saved); | |
| 200 | + Fmt.print("V {} {}\n", [Fmt.int(Sym.scopeDepth()), Fmt.int(Sym.topScope())]); | |
| 201 | + x := Sym.newScope(); | |
| 202 | + Fmt.print("N {}\n", [Fmt.int(x)]); | |
| 203 | + end; | |
| 204 | + | else: | |
| 205 | + x := Sym.scopeDepth(); | |
| 206 | + if x > 3 then Sym.cutScopes(2); end; | |
| 207 | + Fmt.print("X {} {}\n", [Fmt.int(x), Fmt.int(Sym.scopeDepth())]); | |
| 208 | + end; | |
| 209 | + end; | |
| 210 | + for i := 0 to 11 do | |
| 211 | + Fmt.print("U {} {} {} {}\n", [Fmt.int(INT(base[i]^.form)), Fmt.int(base[i]^.size), | |
| 212 | + Fmt.int(bit(base[i]^.signed)), Fmt.str(Scan.nameOf(base[i]^.obj^.name))]); | |
| 213 | + end; | |
| 214 | + if arg = "dup" then | |
| 215 | + o := Sym.newObj(Sym.oVar, names[3], Sym.tI8); | |
| 216 | + ok(Sym.insert(o, 100)); | |
| 217 | + ok(Sym.insert(o, 100)); | |
| 218 | + end; | |
| 219 | + if arg = "deep" then | |
| 220 | + Sym.cutScopes(1); | |
| 221 | + for i := 1 to Sym.MaxDepth + 1 do ok(Sym.openScope(i)); end; | |
| 222 | + end; | |
| 223 | +end; | |
| 224 | + | |
| 225 | +end SymDump. |