v0.19.0 builds src/; the Pascal seed goes BOOT names the release that builds the compiler, v0.19.0, and its tarball's SHA-256; src/boot.sh fetches it into build/boot/ (curl, or gh's REST API for a private repository), checks the sum and puts its raz there with today's lib/ beside it. The fixpoint starts from it, and make prev-bench, which replaces seed-bench, fails if this compiler is a tenth slower than it. seed/ and the Pascal tests of the units below the parser (make tests) go; CI needs no fpc, only a token for boot.sh.
| @@ -24,8 +24,12 @@ | ||
| 24 | 24 | ref: ${{ inputs.tag || github.ref }} |
| 25 | 25 | fetch-depth: 0 # git describe needs the tags |
| 26 | 26 | # musl and musl-tools: test/libc over musl, and cabi with musl-gcc |
| 27 | - - run: sudo apt-get update && sudo apt-get install -y fp-compiler musl musl-tools | |
| 27 | + - run: sudo apt-get update && sudo apt-get install -y musl musl-tools | |
| 28 | + # src/boot.sh fetches the release BOOT names, by gh where the | |
| 29 | + # repository is private | |
| 28 | 30 | - run: make check |
| 31 | + env: | |
| 32 | + GH_TOKEN: ${{ github.token }} | |
| 29 | 33 | - run: make alpine |
| 30 | 34 | - name: release |
| 31 | 35 | if: env.TAG != '' |
| @@ -0,0 +1 @@ | ||
| 1 | +v0.19.0 217448cb6521c76a28d71499d70a5dfdebc2dbcbc81d6d41ed9d9590005772ed |
| @@ -1,19 +1,15 @@ | ||
| 1 | 1 | # Builds the compiler, raz, from src/ into the repository's root; the |
| 2 | -# library it needs is lib/, beside it. The seed, the Pascal compiler in | |
| 3 | -# seed/, builds it: fpc makes the seed in build/seed/ (with lib/ beside | |
| 4 | -# it, as every raz has), and the seed compiles src/. The one module | |
| 5 | -# the build makes, Version, holds what git says the version is. | |
| 6 | - | |
| 7 | -FPC = fpc | |
| 8 | -FPCFLAGS = -O2 -Mobjfpc -Sh- -vew -FUbuild | |
| 2 | +# library it needs is lib/, beside it. The previous release builds it: | |
| 3 | +# BOOT names that release and its tarball's SHA-256, and src/boot.sh | |
| 4 | +# puts its raz in build/boot/, with this lib/ beside it (as every raz | |
| 5 | +# has its library), so that src/ is built against today's library. The | |
| 6 | +# one module the build makes, Version, holds what git says the version is. | |
| 9 | 7 | |
| 10 | -raz: build/seed/raz src/*.raz build/src/Version.raz | |
| 11 | - build/seed/raz -Werror -I src -I build/src -o raz src/Raz.raz | |
| 8 | +raz: build/boot/raz src/*.raz build/src/Version.raz | |
| 9 | + build/boot/raz -Werror -I src -I build/src -o raz src/Raz.raz | |
| 12 | 10 | |
| 13 | -build/seed/raz: seed/*.pas build/version.inc | build | |
| 14 | - mkdir -p build/seed | |
| 15 | - $(FPC) $(FPCFLAGS) -Fibuild -FEbuild/seed -obuild/seed/raz seed/raz.pas | |
| 16 | - ln -sfn ../../lib build/seed/lib | |
| 11 | +build/boot/raz: BOOT src/boot.sh | build | |
| 12 | + src/boot.sh | |
| 17 | 13 | |
| 18 | 14 | build/src/Version.raz: build/version.inc | build |
| 19 | 15 | mkdir -p build/src |
| @@ -27,16 +23,6 @@ | ||
| 27 | 23 | |
| 28 | 24 | FORCE: |
| 29 | 25 | |
| 30 | -units: seed/*.pas | build | |
| 31 | - for u in diag scan sym chunk x64 link elf; do $(FPC) $(FPCFLAGS) seed/$$u.pas || exit 1; done | |
| 32 | - | |
| 33 | -# tests of the parts below the parser, built from test/ | |
| 34 | -tests: units | |
| 35 | - $(FPC) $(FPCFLAGS) -Fuseed -obuild/scantest test/scantest.pas | |
| 36 | - $(FPC) $(FPCFLAGS) -Fuseed -obuild/enctest test/enctest.pas | |
| 37 | - $(FPC) $(FPCFLAGS) -Fuseed -obuild/handtest test/handtest.pas | |
| 38 | - build/handtest build/hand && build/hand; test $$? = 42 | |
| 39 | - | |
| 40 | 26 | build: |
| 41 | 27 | mkdir -p build |
| 42 | 28 | |
| @@ -47,12 +33,13 @@ | ||
| 47 | 33 | check: raz build/raz-lib.3 |
| 48 | 34 | test/check.sh |
| 49 | 35 | |
| 50 | -# the scanner's and the compiler's speed against the seed's | |
| 51 | -seed-bench: raz | build | |
| 36 | +# the compiler's speed against the previous release's, BOOT's, on the | |
| 37 | +# same program: fails if this one is more than a tenth slower | |
| 38 | +prev-bench: raz | build | |
| 52 | 39 | src/bench.sh |
| 53 | 40 | |
| 54 | -# the seed builds src/, that builds src/, that builds it again: the | |
| 55 | -# last two must be the same file; check and bench then run on it | |
| 41 | +# the previous release builds src/, that builds src/, that builds it | |
| 42 | +# again: the last two must be the same file; check and bench then run on it | |
| 56 | 43 | fixpoint: raz | build |
| 57 | 44 | src/fixpoint.sh |
| 58 | 45 | |
| @@ -185,4 +172,4 @@ | ||
| 185 | 172 | site: raz pdf |
| 186 | 173 | python3 site/build.py |
| 187 | 174 | |
| 188 | -.PHONY: units tests seed-bench fixpoint check gdb bench kernels stress alpine fmt loc man pdf dist site clean install uninstall | |
| 175 | +.PHONY: prev-bench fixpoint check gdb bench kernels stress alpine fmt loc man pdf dist site clean install uninstall |
| @@ -1,318 +0,0 @@ | ||
| 1 | -{ chunk.pas: 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.pas). | |
| 13 | - | |
| 14 | - Depends on: scan, for errors. | |
| 15 | - Leaves out: the instructions; that is x64.pas. } | |
| 16 | - | |
| 17 | -unit chunk; | |
| 18 | - | |
| 19 | -{$mode objfpc}{$H-} | |
| 20 | - | |
| 21 | -interface | |
| 22 | - | |
| 23 | -uses scan, diag; | |
| 24 | - | |
| 25 | -type | |
| 26 | - TSection = (secText, secRodata, secBss); | |
| 27 | - | |
| 28 | - TChunk = record | |
| 29 | - sec: TSection; | |
| 30 | - start, size, align: longint; { in its section's buffer } | |
| 31 | - firstFix, numFix: longint; { its fixups: text's in fixups, rodata's in dataFixups } | |
| 32 | - module, name: longint; { for the symbol table; -1 if none } | |
| 33 | - adr: int64; { its address, set by the linker } | |
| 34 | - live: boolean; { reached from the entry point } | |
| 35 | - ext: boolean; { bss: the address of a C symbol, its name } | |
| 36 | - reals: boolean; { code: computes with reals, first at realsAt } | |
| 37 | - realsAt: TPos; | |
| 38 | - saveMask: longint; { code: the registers of variables it saves, as bits of gen's VarRegs } | |
| 39 | - saveAt: longint; { ... at this offset from the frame, one quadword each } | |
| 40 | - end; | |
| 41 | - | |
| 42 | - { A place in the source whose code starts at a place in text: for | |
| 43 | - gdb, and for Rt to say where a failed check or a signal was } | |
| 44 | - TLine = record | |
| 45 | - at, chunk: longint; { the place in text, and its chunk } | |
| 46 | - path, line, col: longint; { the file's path, interned, the line and column } | |
| 47 | - end; | |
| 48 | - | |
| 49 | - { A variable of a procedure, for the trap report: Rt shows it, by | |
| 50 | - kind, from its place in the frame } | |
| 51 | - TVarNote = record | |
| 52 | - chunk, name: longint; | |
| 53 | - off: longint; { from the frame pointer } | |
| 54 | - kind: longint; { how Rt shows it: see link.pas } | |
| 55 | - end; | |
| 56 | - | |
| 57 | - TFixup = record | |
| 58 | - at: longint; { the 32-bit field, as an offset in text; in | |
| 59 | - dataFixups, the 64-bit one, from its chunk's start } | |
| 60 | - chunk: longint; { the chunk it refers to } | |
| 61 | - addend: longint; { added to the chunk's address } | |
| 62 | - end; | |
| 63 | - | |
| 64 | - PChunks = ^TChunkArray; | |
| 65 | - TChunkArray = array[0..MaxInt div SizeOf(TChunk) - 1] of TChunk; | |
| 66 | - PLines = ^TLineArray; | |
| 67 | - TLineArray = array[0..MaxInt div SizeOf(TLine) - 1] of TLine; | |
| 68 | - PFixups = ^TFixupArray; | |
| 69 | - TFixupArray = array[0..MaxInt div SizeOf(TFixup) - 1] of TFixup; | |
| 70 | - | |
| 71 | -var | |
| 72 | - code: PBytes; { text } | |
| 73 | - pc: longint; { its length } | |
| 74 | - rodata: PBytes; | |
| 75 | - rolen: longint; | |
| 76 | - chunks: PChunks; | |
| 77 | - numChunks: longint; | |
| 78 | - fixups: PFixups; | |
| 79 | - numFixups: longint; | |
| 80 | - { Addresses in read-only data, as in an array constant of strings: | |
| 81 | - each 8 bytes, the chunk's absolute address, since the executable | |
| 82 | - is loaded where the linker placed it } | |
| 83 | - dataFixups: PFixups; | |
| 84 | - numDataFixups: longint; | |
| 85 | - lines: PLines; | |
| 86 | - numLines: longint; | |
| 87 | - curFile: longint; { the file being compiled, for NoteLine } | |
| 88 | - curChunk: longint; { the chunk of code being written } | |
| 89 | - varNotes: ^TVarNote; | |
| 90 | - numVarNotes: longint; | |
| 91 | - | |
| 92 | -function NewChunk(sec: TSection; module, name: longint): longint; | |
| 93 | -procedure BeginChunk(c: longint); | |
| 94 | -procedure EndChunk(c: longint); | |
| 95 | -procedure SetData(c: longint; p: PBytes; n, align: longint); | |
| 96 | -procedure SetBss(c: longint; size, align: longint); | |
| 97 | -function ExternChunk(name: longint): longint; | |
| 98 | -procedure NoteLine(line, col: longint); | |
| 99 | -procedure AddFixup(at, chunk, addend: longint); | |
| 100 | -procedure AddDataFixup(c, at, chunk, addend: longint); | |
| 101 | -procedure NoteVar(chunk, name, off, kind: longint); | |
| 102 | -function ChunkName(c: longint): shortstring; | |
| 103 | - | |
| 104 | -procedure Put1(b: longint); | |
| 105 | -procedure Put4(v: longint); | |
| 106 | -procedure Put8(v: int64); | |
| 107 | -procedure Put4At(at, v: longint); | |
| 108 | -function Get4At(at: longint): longint; | |
| 109 | -procedure TakeBack(n: longint); | |
| 110 | - | |
| 111 | -implementation | |
| 112 | - | |
| 113 | -var | |
| 114 | - codeCap, roCap, chunkCap, fixCap, lineCap: longint; | |
| 115 | - | |
| 116 | -procedure Room(n: longint); | |
| 117 | -begin | |
| 118 | - if pc + n > codeCap then begin | |
| 119 | - codeCap := codeCap * 2 + n; | |
| 120 | - code := ReAllocMem(code, codeCap); | |
| 121 | - end; | |
| 122 | -end; | |
| 123 | - | |
| 124 | -procedure Put1(b: longint); | |
| 125 | -begin | |
| 126 | - if pc + 16 > codeCap then Room(16); | |
| 127 | - code^[pc] := byte(b); | |
| 128 | - inc(pc); | |
| 129 | -end; | |
| 130 | - | |
| 131 | -procedure Put4(v: longint); | |
| 132 | -begin | |
| 133 | - if pc + 16 > codeCap then Room(16); | |
| 134 | - PLongint(@code^[pc])^ := v; | |
| 135 | - inc(pc, 4); | |
| 136 | -end; | |
| 137 | - | |
| 138 | -procedure Put4At(at, v: longint); | |
| 139 | -begin | |
| 140 | - PLongint(@code^[at])^ := v; | |
| 141 | -end; | |
| 142 | - | |
| 143 | -function Get4At(at: longint): longint; | |
| 144 | -begin | |
| 145 | - Get4At := PLongint(@code^[at])^; | |
| 146 | -end; | |
| 147 | - | |
| 148 | -{ The last n bytes of code taken back. A line noted past the new end | |
| 149 | - now starts there, in place of one noted there before, as NoteLine | |
| 150 | - would have it. } | |
| 151 | -procedure TakeBack(n: longint); | |
| 152 | -begin | |
| 153 | - dec(pc, n); | |
| 154 | - if (numLines > 0) and (lines^[numLines - 1].at > pc) then begin | |
| 155 | - lines^[numLines - 1].at := pc; | |
| 156 | - if (numLines > 1) and (lines^[numLines - 2].at = pc) then begin | |
| 157 | - lines^[numLines - 2] := lines^[numLines - 1]; | |
| 158 | - dec(numLines); | |
| 159 | - end; | |
| 160 | - end; | |
| 161 | -end; | |
| 162 | - | |
| 163 | -procedure Put8(v: int64); | |
| 164 | -begin | |
| 165 | - Put4(longint(v and $FFFFFFFF)); | |
| 166 | - Put4(longint(v shr 32)); | |
| 167 | -end; | |
| 168 | - | |
| 169 | -function NewChunk(sec: TSection; module, name: longint): longint; | |
| 170 | -begin | |
| 171 | - if numChunks = chunkCap then begin | |
| 172 | - chunkCap := chunkCap * 2; | |
| 173 | - chunks := ReAllocMem(chunks, chunkCap * SizeOf(TChunk)); | |
| 174 | - end; | |
| 175 | - FillChar(chunks^[numChunks], SizeOf(TChunk), 0); | |
| 176 | - chunks^[numChunks].sec := sec; | |
| 177 | - chunks^[numChunks].module := module; | |
| 178 | - chunks^[numChunks].name := name; | |
| 179 | - chunks^[numChunks].align := 1; | |
| 180 | - chunks^[numChunks].size := -1; { not yet defined } | |
| 181 | - NewChunk := numChunks; | |
| 182 | - inc(numChunks); | |
| 183 | -end; | |
| 184 | - | |
| 185 | -procedure BeginChunk(c: longint); | |
| 186 | -begin | |
| 187 | - { code is aligned to 32 bytes, the windows in which x86 fetches and | |
| 188 | - caches it, so that a procedure's loops sit in them the same way | |
| 189 | - whatever comes before: rat ran 5% slower at 16, by where its loops | |
| 190 | - fell. Padded with CC, x86's int3: a trap if it were ever run } | |
| 191 | - while pc and 31 <> 0 do Put1($CC); | |
| 192 | - curChunk := c; | |
| 193 | - chunks^[c].start := pc; | |
| 194 | - chunks^[c].align := 32; | |
| 195 | - chunks^[c].firstFix := numFixups; | |
| 196 | -end; | |
| 197 | - | |
| 198 | -procedure EndChunk(c: longint); | |
| 199 | -begin | |
| 200 | - chunks^[c].size := pc - chunks^[c].start; | |
| 201 | - chunks^[c].numFix := numFixups - chunks^[c].firstFix; | |
| 202 | -end; | |
| 203 | - | |
| 204 | -procedure SetData(c: longint; p: PBytes; n, align: longint); | |
| 205 | -begin | |
| 206 | - if rolen + n > roCap then begin | |
| 207 | - roCap := roCap * 2 + n; | |
| 208 | - rodata := ReAllocMem(rodata, roCap); | |
| 209 | - end; | |
| 210 | - Move(p^, rodata^[rolen], n); | |
| 211 | - chunks^[c].firstFix := numDataFixups; | |
| 212 | - chunks^[c].numFix := 0; | |
| 213 | - chunks^[c].start := rolen; | |
| 214 | - chunks^[c].size := n; | |
| 215 | - chunks^[c].align := align; | |
| 216 | - inc(rolen, n); | |
| 217 | -end; | |
| 218 | - | |
| 219 | -procedure SetBss(c: longint; size, align: longint); | |
| 220 | -begin | |
| 221 | - chunks^[c].size := size; | |
| 222 | - chunks^[c].align := align; | |
| 223 | -end; | |
| 224 | - | |
| 225 | -{ The code from here on is for line and column of the current file. A | |
| 226 | - place with no code of its own gives way to the next. } | |
| 227 | -procedure NoteLine(line, col: longint); | |
| 228 | -begin | |
| 229 | - if (numLines > 0) and (lines^[numLines - 1].at = pc) then dec(numLines); | |
| 230 | - if (numLines > 0) and (lines^[numLines - 1].chunk = curChunk) and (lines^[numLines - 1].line = line) | |
| 231 | - and (lines^[numLines - 1].col = col) and (lines^[numLines - 1].path = curFile) then exit; | |
| 232 | - if numLines = lineCap then begin | |
| 233 | - lineCap := lineCap * 2; | |
| 234 | - lines := ReAllocMem(lines, lineCap * SizeOf(TLine)); | |
| 235 | - end; | |
| 236 | - lines^[numLines].at := pc; | |
| 237 | - lines^[numLines].chunk := curChunk; | |
| 238 | - lines^[numLines].path := curFile; | |
| 239 | - lines^[numLines].line := line; | |
| 240 | - lines^[numLines].col := col; | |
| 241 | - inc(numLines); | |
| 242 | -end; | |
| 243 | - | |
| 244 | -{ The quadword that holds the address of C's symbol name, filled in | |
| 245 | - when the program starts; one for each name. } | |
| 246 | -function ExternChunk(name: longint): longint; | |
| 247 | -var c: longint; | |
| 248 | -begin | |
| 249 | - for c := 0 to numChunks - 1 do | |
| 250 | - if chunks^[c].ext and (chunks^[c].name = name) then exit(c); | |
| 251 | - c := NewChunk(secBss, -1, name); | |
| 252 | - SetBss(c, 8, 8); | |
| 253 | - chunks^[c].ext := true; | |
| 254 | - ExternChunk := c; | |
| 255 | -end; | |
| 256 | - | |
| 257 | -procedure AddFixup(at, chunk, addend: longint); | |
| 258 | -begin | |
| 259 | - if numFixups = fixCap then begin | |
| 260 | - fixCap := fixCap * 2; | |
| 261 | - fixups := ReAllocMem(fixups, fixCap * SizeOf(TFixup)); | |
| 262 | - end; | |
| 263 | - fixups^[numFixups].at := at; | |
| 264 | - fixups^[numFixups].chunk := chunk; | |
| 265 | - fixups^[numFixups].addend := addend; | |
| 266 | - inc(numFixups); | |
| 267 | -end; | |
| 268 | - | |
| 269 | -{ The address of chunk, plus addend, at offset at of rodata chunk c, | |
| 270 | - whose data is set: its fixups follow one another. } | |
| 271 | -procedure AddDataFixup(c, at, chunk, addend: longint); | |
| 272 | -begin | |
| 273 | - if numDataFixups and 255 = 0 then | |
| 274 | - dataFixups := ReAllocMem(dataFixups, (numDataFixups + 256) * SizeOf(TFixup)); | |
| 275 | - dataFixups^[numDataFixups].at := at; | |
| 276 | - dataFixups^[numDataFixups].chunk := chunk; | |
| 277 | - dataFixups^[numDataFixups].addend := addend; | |
| 278 | - inc(numDataFixups); | |
| 279 | - inc(chunks^[c].numFix); | |
| 280 | -end; | |
| 281 | - | |
| 282 | -procedure NoteVar(chunk, name, off, kind: longint); | |
| 283 | -begin | |
| 284 | - if numVarNotes and 255 = 0 then varNotes := ReAllocMem(varNotes, (numVarNotes + 256) * SizeOf(TVarNote)); | |
| 285 | - varNotes[numVarNotes].chunk := chunk; | |
| 286 | - varNotes[numVarNotes].name := name; | |
| 287 | - varNotes[numVarNotes].off := off; | |
| 288 | - varNotes[numVarNotes].kind := kind; | |
| 289 | - inc(numVarNotes); | |
| 290 | -end; | |
| 291 | - | |
| 292 | -{ The name of chunk c: its module's and its own, or literal and its | |
| 293 | - number for a literal } | |
| 294 | -function ChunkName(c: longint): shortstring; | |
| 295 | -begin | |
| 296 | - with chunks^[c] do | |
| 297 | - if name < 0 then ChunkName := 'literal' + IntStr(c) | |
| 298 | - else if module < 0 then ChunkName := NameOf(name) | |
| 299 | - else ChunkName := NameOf(module) + '.' + NameOf(name); | |
| 300 | -end; | |
| 301 | - | |
| 302 | -initialization | |
| 303 | - codeCap := 1 shl 20; | |
| 304 | - code := GetMem(codeCap); | |
| 305 | - pc := 0; | |
| 306 | - roCap := 1 shl 16; | |
| 307 | - rodata := GetMem(roCap); | |
| 308 | - rolen := 0; | |
| 309 | - chunkCap := 1 shl 10; | |
| 310 | - chunks := GetMem(chunkCap * SizeOf(TChunk)); | |
| 311 | - numChunks := 0; | |
| 312 | - fixCap := 1 shl 12; | |
| 313 | - fixups := GetMem(fixCap * SizeOf(TFixup)); | |
| 314 | - numFixups := 0; | |
| 315 | - lineCap := 1 shl 12; | |
| 316 | - lines := GetMem(lineCap * SizeOf(TLine)); | |
| 317 | - numLines := 0; | |
| 318 | -end. |
| @@ -1,316 +0,0 @@ | ||
| 1 | -{ diag.pas: messages. | |
| 2 | - | |
| 3 | - The one place the compiler's messages are made. A message is an | |
| 4 | - error or a warning at a position, which it shows as the file, line | |
| 5 | - and column, then the line of source with the position underlined; | |
| 6 | - notes, each at a position of its own, and help, a line of advice, | |
| 7 | - may follow it. Colour is used when standard error is a terminal and | |
| 8 | - NO_COLOR isn't set. diag keeps every source file it is given, so | |
| 9 | - that a note can show a line of a module compiled earlier. | |
| 10 | - | |
| 11 | - Depends on: BaseUnix and termio, to write and to ask about the | |
| 12 | - terminal. | |
| 13 | - Leaves out: wording. The callers say what is wrong; a message is at | |
| 14 | - most 255 characters, which every message so far keeps well within. } | |
| 15 | - | |
| 16 | -unit diag; | |
| 17 | - | |
| 18 | -{$mode objfpc}{$H-} | |
| 19 | - | |
| 20 | -interface | |
| 21 | - | |
| 22 | -type | |
| 23 | - { A stretch of a source file: bytes a up to but not including b. | |
| 24 | - a = b marks the place between two characters. } | |
| 25 | - TPos = record | |
| 26 | - f, a, b: longint; | |
| 27 | - end; | |
| 28 | - | |
| 29 | -const | |
| 30 | - MaxErrors = 20; | |
| 31 | - | |
| 32 | -var | |
| 33 | - errors, warnings: longint; | |
| 34 | - werror: boolean; { -Werror: a warning counts as an error } | |
| 35 | - { The construct being read uses a name already reported as not | |
| 36 | - declared: its errors follow from that one, and aren't shown. } | |
| 37 | - quiet: boolean; | |
| 38 | - | |
| 39 | -function AddFile(const path: shortstring; buf: PChar; len: longint): longint; | |
| 40 | -function ShownPath(const path: shortstring): shortstring; | |
| 41 | -function PathOf(f: longint): shortstring; | |
| 42 | -function LineOf(const p: TPos): longint; | |
| 43 | -function Join(const p, q: TPos): TPos; | |
| 44 | -function Source(const p: TPos): shortstring; | |
| 45 | -function LineText(const path: shortstring; line: longint): shortstring; | |
| 46 | -procedure Error(const p: TPos; const msg: shortstring); | |
| 47 | -procedure Warning(const p: TPos; const msg: shortstring); | |
| 48 | -procedure Note(const p: TPos; const msg: shortstring); | |
| 49 | -function IntStr(n: int64): shortstring; | |
| 50 | -procedure Help(const msg: shortstring); | |
| 51 | -procedure Fatal(const msg: shortstring); | |
| 52 | -procedure Finish; | |
| 53 | - | |
| 54 | -implementation | |
| 55 | - | |
| 56 | -uses BaseUnix, termio; | |
| 57 | - | |
| 58 | -type | |
| 59 | - TFile = record | |
| 60 | - path: shortstring; | |
| 61 | - buf: PChar; | |
| 62 | - len: longint; | |
| 63 | - end; | |
| 64 | - | |
| 65 | -var | |
| 66 | - files: ^TFile; { numFiles of them, in room for cap } | |
| 67 | - numFiles, cap: longint; | |
| 68 | - colour, colourKnown, cwdKnown: boolean; | |
| 69 | - cwd: shortstring; { the working directory and a /, or '' if unknown } | |
| 70 | - lastError: TPos; { where the last error was } | |
| 71 | - shown: boolean; { whether it was shown: its notes and help are only then } | |
| 72 | - | |
| 73 | -{ Paths are shown from the working directory where they are under it, | |
| 74 | - so messages read the same wherever the compiler or its library is. } | |
| 75 | -function ShownPath(const path: shortstring): shortstring; | |
| 76 | -var d: array[0..255] of char; | |
| 77 | -begin | |
| 78 | - if not cwdKnown then begin | |
| 79 | - if FpGetcwd(@d[0], SizeOf(d)) <> nil then cwd := StrPas(@d[0]) + '/'; | |
| 80 | - cwdKnown := true; | |
| 81 | - end; | |
| 82 | - if (cwd <> '') and (copy(path, 1, length(cwd)) = cwd) then ShownPath := copy(path, length(cwd) + 1, 255) | |
| 83 | - else ShownPath := path; | |
| 84 | -end; | |
| 85 | - | |
| 86 | -function AddFile(const path: shortstring; buf: PChar; len: longint): longint; | |
| 87 | -begin | |
| 88 | - if numFiles = cap then begin | |
| 89 | - cap := 2 * cap + 8; | |
| 90 | - ReallocMem(files, cap * SizeOf(TFile)); | |
| 91 | - end; | |
| 92 | - files[numFiles].path := ShownPath(path); | |
| 93 | - files[numFiles].buf := buf; | |
| 94 | - files[numFiles].len := len; | |
| 95 | - AddFile := numFiles; | |
| 96 | - inc(numFiles); | |
| 97 | -end; | |
| 98 | - | |
| 99 | -function PathOf(f: longint): shortstring; | |
| 100 | -begin | |
| 101 | - PathOf := files[f].path; | |
| 102 | -end; | |
| 103 | - | |
| 104 | -{ Writing } | |
| 105 | - | |
| 106 | -const | |
| 107 | - Bold = #27'[1m'; Red = #27'[1;31m'; Yellow = #27'[1;33m'; | |
| 108 | - Cyan = #27'[1;36m'; Green = #27'[1;32m'; Blue = #27'[1;34m'; | |
| 109 | - Plain = #27'[0m'; | |
| 110 | - | |
| 111 | -{ Whether NO_COLOR is set, to anything } | |
| 112 | -function NoColour: boolean; | |
| 113 | -var e: PPChar; | |
| 114 | -begin | |
| 115 | - e := envp; | |
| 116 | - while (e^ <> nil) and (CompareByte(e^^, PChar('NO_COLOR=')^, 9) <> 0) do inc(e); | |
| 117 | - NoColour := e^ <> nil; | |
| 118 | -end; | |
| 119 | - | |
| 120 | -{ s in colour c, when colour is on } | |
| 121 | -function Paint(const c, s: shortstring): shortstring; | |
| 122 | -begin | |
| 123 | - if not colourKnown then begin | |
| 124 | - colour := (IsATTY(2) = 1) and not NoColour; | |
| 125 | - colourKnown := true; | |
| 126 | - end; | |
| 127 | - if colour then Paint := c + s + Plain else Paint := s; | |
| 128 | -end; | |
| 129 | - | |
| 130 | -function IntStr(n: int64): shortstring; | |
| 131 | -var s: shortstring; | |
| 132 | -begin | |
| 133 | - Str(n, s); | |
| 134 | - IntStr := s; | |
| 135 | -end; | |
| 136 | - | |
| 137 | -{ Positions } | |
| 138 | - | |
| 139 | -{ The line of p, and the byte where it starts. Lines are counted from | |
| 140 | - the start of the file, which is slow only next to compiling it. } | |
| 141 | -procedure Locate(const p: TPos; out line, start: longint); | |
| 142 | -var i: longint; buf: PChar; | |
| 143 | -begin | |
| 144 | - buf := files[p.f].buf; | |
| 145 | - line := 1; start := 0; | |
| 146 | - for i := 0 to p.a - 1 do | |
| 147 | - if buf[i] = #10 then begin inc(line); start := i + 1 end; | |
| 148 | -end; | |
| 149 | - | |
| 150 | -function LineOf(const p: TPos): longint; | |
| 151 | -var line, start: longint; | |
| 152 | -begin | |
| 153 | - Locate(p, line, start); | |
| 154 | - LineOf := line; | |
| 155 | -end; | |
| 156 | - | |
| 157 | -{ Characters from byte i to byte j of buf: every byte but a UTF-8 | |
| 158 | - continuation byte. } | |
| 159 | -function Chars(buf: PChar; i, j: longint): longint; | |
| 160 | -var n: longint; | |
| 161 | -begin | |
| 162 | - n := 0; | |
| 163 | - for i := i to j - 1 do | |
| 164 | - if (byte(buf[i]) and $C0) <> $80 then inc(n); | |
| 165 | - Chars := n; | |
| 166 | -end; | |
| 167 | - | |
| 168 | -{ "file:line:col: kind: msg", then the line with p underlined. A tab | |
| 169 | - before p is copied into the underline, so that the two line up | |
| 170 | - whatever the terminal makes of tabs. } | |
| 171 | -{ A long line is shown cut to this many bytes around the place, its | |
| 172 | - cut ends marked ... } | |
| 173 | -const ShownBytes = 100; | |
| 174 | - | |
| 175 | -procedure Show(const p: TPos; const kind, kindColour, msg: shortstring); | |
| 176 | -var line, start, stop, lo, hi, i, n: longint; buf: PChar; num, gutter: shortstring; | |
| 177 | -begin | |
| 178 | - if p.f >= numFiles then begin | |
| 179 | - { about no place in a file, as for Rt, which the compiler imports | |
| 180 | - before it reads one } | |
| 181 | - writeln(StdErr, Paint(Bold, 'raz:'), ' ', Paint(kindColour, kind + ':'), ' ', Paint(Bold, msg)); | |
| 182 | - exit; | |
| 183 | - end; | |
| 184 | - Locate(p, line, start); | |
| 185 | - buf := files[p.f].buf; | |
| 186 | - writeln(StdErr, Paint(Bold, files[p.f].path + ':' + IntStr(line) + ':' | |
| 187 | - + IntStr(Chars(buf, start, p.a) + 1) + ':'), ' ', Paint(kindColour, kind + ':'), | |
| 188 | - ' ', Paint(Bold, msg)); | |
| 189 | - stop := start; | |
| 190 | - while (stop < files[p.f].len) and (buf[stop] <> #10) do inc(stop); | |
| 191 | - num := IntStr(line); | |
| 192 | - while length(num) < 5 do num := ' ' + num; | |
| 193 | - gutter := Paint(Blue, num + ' |'); | |
| 194 | - lo := start; hi := stop; | |
| 195 | - if hi - lo > ShownBytes then begin | |
| 196 | - if p.a - lo > ShownBytes div 2 then lo := p.a - ShownBytes div 2; | |
| 197 | - while (byte(buf[lo]) and $C0) = $80 do inc(lo); | |
| 198 | - if hi - lo > ShownBytes then hi := lo + ShownBytes; | |
| 199 | - while (hi < stop) and ((byte(buf[hi]) and $C0) = $80) do inc(hi); | |
| 200 | - end; | |
| 201 | - write(StdErr, gutter, ' '); | |
| 202 | - if lo > start then write(StdErr, '...'); | |
| 203 | - for i := lo to hi - 1 do write(StdErr, buf[i]); | |
| 204 | - if hi < stop then write(StdErr, '...'); | |
| 205 | - writeln(StdErr); | |
| 206 | - write(StdErr, Paint(Blue, ' |'), ' '); | |
| 207 | - if lo > start then write(StdErr, ' '); | |
| 208 | - for i := lo to p.a - 1 do | |
| 209 | - if buf[i] = #9 then write(StdErr, #9) | |
| 210 | - else if (byte(buf[i]) and $C0) <> $80 then write(StdErr, ' '); | |
| 211 | - if p.b < hi then n := Chars(buf, p.a, p.b) else n := Chars(buf, p.a, hi); | |
| 212 | - if n < 1 then n := 1; | |
| 213 | - writeln(StdErr, Paint(kindColour, StringOfChar('^', n))); | |
| 214 | -end; | |
| 215 | - | |
| 216 | -{ From the start of p to the end of q } | |
| 217 | -function Join(const p, q: TPos): TPos; | |
| 218 | -begin | |
| 219 | - Join := p; | |
| 220 | - Join.b := q.b; | |
| 221 | -end; | |
| 222 | - | |
| 223 | -{ The text at p, at most the 255 bytes a shortstring holds } | |
| 224 | -function Source(const p: TPos): shortstring; | |
| 225 | -var n: longint; | |
| 226 | -begin | |
| 227 | - n := p.b - p.a; | |
| 228 | - if n > 255 then n := 255; | |
| 229 | - SetLength(Source, n); | |
| 230 | - Move(files[p.f].buf[p.a], Source[1], n); | |
| 231 | -end; | |
| 232 | - | |
| 233 | -{ Messages } | |
| 234 | - | |
| 235 | -{ Line line of the file at path, as written } | |
| 236 | -function LineText(const path: shortstring; line: longint): shortstring; | |
| 237 | -var i, k, n: longint; s: shortstring; | |
| 238 | -begin | |
| 239 | - s := ''; | |
| 240 | - for i := 0 to numFiles - 1 do | |
| 241 | - if files[i].path = ShownPath(path) then begin | |
| 242 | - k := 0; n := 1; | |
| 243 | - while (n < line) and (k < files[i].len) do begin | |
| 244 | - if files[i].buf[k] = #10 then inc(n); | |
| 245 | - inc(k); | |
| 246 | - end; | |
| 247 | - while (k < files[i].len) and (files[i].buf[k] <> #10) and (length(s) < 255) do begin | |
| 248 | - s := s + files[i].buf[k]; | |
| 249 | - inc(k); | |
| 250 | - end; | |
| 251 | - break; | |
| 252 | - end; | |
| 253 | - LineText := s; | |
| 254 | -end; | |
| 255 | - | |
| 256 | -procedure Error(const p: TPos; const msg: shortstring); | |
| 257 | -begin | |
| 258 | - { a second error at one place follows from the first: when a symbol | |
| 259 | - ends a construct with an error, the one around it may stop there too } | |
| 260 | - shown := not quiet and not ((errors > 0) and (p.f = lastError.f) and (p.a = lastError.a)); | |
| 261 | - if not shown then exit; | |
| 262 | - lastError := p; | |
| 263 | - Show(p, 'error', Red, msg); | |
| 264 | - inc(errors); | |
| 265 | - if errors >= MaxErrors then begin | |
| 266 | - writeln(StdErr, 'raz: stopping after ', MaxErrors, ' errors'); | |
| 267 | - Halt(1); | |
| 268 | - end; | |
| 269 | -end; | |
| 270 | - | |
| 271 | -procedure Warning(const p: TPos; const msg: shortstring); | |
| 272 | -begin | |
| 273 | - if werror then Error(p, msg) | |
| 274 | - else begin | |
| 275 | - Show(p, 'warning', Yellow, msg); | |
| 276 | - shown := true; | |
| 277 | - inc(warnings); | |
| 278 | - end; | |
| 279 | -end; | |
| 280 | - | |
| 281 | -{ Notes and help follow an error or a warning, and are shown with it } | |
| 282 | -procedure Note(const p: TPos; const msg: shortstring); | |
| 283 | -begin | |
| 284 | - if shown then Show(p, 'note', Cyan, msg); | |
| 285 | -end; | |
| 286 | - | |
| 287 | -procedure Help(const msg: shortstring); | |
| 288 | -begin | |
| 289 | - if shown then writeln(StdErr, Paint(Green, 'help:'), ' ', msg); | |
| 290 | -end; | |
| 291 | - | |
| 292 | -{ An error with no place in the source, such as a file that can't be | |
| 293 | - read: it stops the compiler at once. } | |
| 294 | -procedure Fatal(const msg: shortstring); | |
| 295 | -begin | |
| 296 | - writeln(StdErr, 'raz: ', msg); | |
| 297 | - Halt(1); | |
| 298 | -end; | |
| 299 | - | |
| 300 | -function Count(n: longint; const what: shortstring): shortstring; | |
| 301 | -begin | |
| 302 | - if n = 1 then Count := '1 ' + what else Count := IntStr(n) + ' ' + what + 's'; | |
| 303 | -end; | |
| 304 | - | |
| 305 | -{ Says how many errors and warnings there were, and stops the compiler | |
| 306 | - if there were errors. } | |
| 307 | -procedure Finish; | |
| 308 | -begin | |
| 309 | - if (errors > 0) and (warnings > 0) then | |
| 310 | - writeln(StdErr, 'raz: ', Count(errors, 'error'), ', ', Count(warnings, 'warning')) | |
| 311 | - else if errors > 0 then writeln(StdErr, 'raz: ', Count(errors, 'error')) | |
| 312 | - else if warnings > 0 then writeln(StdErr, 'raz: ', Count(warnings, 'warning')); | |
| 313 | - if errors > 0 then Halt(1); | |
| 314 | -end; | |
| 315 | - | |
| 316 | -end. |
| @@ -1,373 +0,0 @@ | ||
| 1 | -{ elf.pas: the executable file. | |
| 2 | - | |
| 3 | - Writes an ELF64 executable for x86-64 Linux: the ELF header, the | |
| 4 | - program headers, the linked text and rodata, and section headers | |
| 5 | - with a symbol table, so that objdump and gdb can name each procedure | |
| 6 | - and variable. For gdb's line numbers there is DWARF 4: a line table | |
| 7 | - (.debug_line), and the one compile unit that points to it | |
| 8 | - (.debug_info, .debug_abbrev). There are no types or variables. | |
| 9 | - | |
| 10 | - A program that uses C is linked dynamically, in the least way the | |
| 11 | - loader accepts: it names its loader and libraries, and each C symbol | |
| 12 | - it uses is a quadword the loader fills in with the symbol's address | |
| 13 | - (a GLOB_DAT relocation), which the code calls or reads through. | |
| 14 | - There is no PLT and no lazy binding, and the program exports nothing. | |
| 15 | - | |
| 16 | - Depends on: link for the images and addresses, chunk for the chunks, | |
| 17 | - scan for names and errors, BaseUnix to write the file. | |
| 18 | - Leaves out: position independent executables, symbol versions, the | |
| 19 | - rest of DWARF. } | |
| 20 | - | |
| 21 | -unit elf; | |
| 22 | - | |
| 23 | -{$mode objfpc}{$H-} | |
| 24 | - | |
| 25 | -interface | |
| 26 | - | |
| 27 | -uses scan, diag, chunk, link; | |
| 28 | - | |
| 29 | -var | |
| 30 | - needed: array[0..31] of longint; { the libraries to load, names interned } | |
| 31 | - numNeeded: longint; { none: a static program } | |
| 32 | - mainPath: shortstring; { the main module's file, for gdb } | |
| 33 | - stripped: boolean; { -s: no symbols and no line numbers; the sections are there, empty } | |
| 34 | - | |
| 35 | -procedure WriteElf(const path: shortstring; entry: longint); | |
| 36 | - | |
| 37 | -implementation | |
| 38 | - | |
| 39 | -uses BaseUnix; | |
| 40 | - | |
| 41 | -var | |
| 42 | - buf: PBytes; | |
| 43 | - len, cap: int64; | |
| 44 | - | |
| 45 | -procedure Room(n: int64); | |
| 46 | -begin | |
| 47 | - if len + n > cap then begin | |
| 48 | - cap := cap * 2 + n; | |
| 49 | - buf := ReAllocMem(buf, cap); | |
| 50 | - end; | |
| 51 | -end; | |
| 52 | - | |
| 53 | -procedure PutN(v: qword; n: longint); | |
| 54 | -var i: longint; | |
| 55 | -begin | |
| 56 | - Room(n); | |
| 57 | - for i := 0 to n - 1 do begin | |
| 58 | - buf^[len] := byte(v); | |
| 59 | - v := v shr 8; | |
| 60 | - inc(len); | |
| 61 | - end; | |
| 62 | -end; | |
| 63 | - | |
| 64 | -procedure PutBytes(p: PBytes; n: int64); | |
| 65 | -begin | |
| 66 | - Room(n); | |
| 67 | - if n > 0 then Move(p^, buf^[len], n); | |
| 68 | - len := len + n; | |
| 69 | -end; | |
| 70 | - | |
| 71 | -procedure PadTo(off: int64); | |
| 72 | -begin | |
| 73 | - while len < off do PutN(0, 1); | |
| 74 | -end; | |
| 75 | - | |
| 76 | -procedure Patch(at: int64; v: qword; n: longint); | |
| 77 | -var i: longint; | |
| 78 | -begin | |
| 79 | - for i := 0 to n - 1 do begin | |
| 80 | - buf^[at + i] := byte(v); | |
| 81 | - v := v shr 8; | |
| 82 | - end; | |
| 83 | -end; | |
| 84 | - | |
| 85 | -procedure Phdr(typ, flags: longint; off, adr, filesz, memsz, align: int64); | |
| 86 | -begin | |
| 87 | - PutN(typ, 4); | |
| 88 | - PutN(flags, 4); | |
| 89 | - PutN(off, 8); | |
| 90 | - PutN(adr, 8); | |
| 91 | - PutN(adr, 8); | |
| 92 | - PutN(filesz, 8); | |
| 93 | - PutN(memsz, 8); | |
| 94 | - PutN(align, 8); | |
| 95 | -end; | |
| 96 | - | |
| 97 | -const | |
| 98 | - { the section names, in .shstrtab; a section header names its own } | |
| 99 | - ShNames = #0'.text'#0'.rodata'#0'.bss'#0'.symtab'#0'.strtab'#0'.shstrtab'#0 + | |
| 100 | - '.dynamic'#0'.dynstr'#0'.dynsym'#0'.debug_line'#0'.debug_info'#0'.debug_abbrev'#0; | |
| 101 | - | |
| 102 | -{ A section header. Its name is given as a string and stored as its | |
| 103 | - offset in ShNames: the #0 before it, counted from 1, is that offset | |
| 104 | - counted from 0; the empty name, found nowhere, is 0. } | |
| 105 | -procedure Shdr(const name: shortstring; typ: longint; flags, adr, off, size: int64; lnk, info: longint; align, entsize: int64); | |
| 106 | -begin | |
| 107 | - PutN(Pos(#0 + name + #0, ShNames), 4); PutN(typ, 4); PutN(flags, 8); PutN(adr, 8); | |
| 108 | - PutN(off, 8); PutN(size, 8); PutN(lnk, 4); PutN(info, 4); | |
| 109 | - PutN(align, 8); PutN(entsize, 8); | |
| 110 | -end; | |
| 111 | - | |
| 112 | -procedure PutStr(const s: shortstring); | |
| 113 | -begin | |
| 114 | - PutBytes(@s[1], length(s)); | |
| 115 | - PutN(0, 1); | |
| 116 | -end; | |
| 117 | - | |
| 118 | -{ LEB128, unsigned } | |
| 119 | -procedure PutU(v: qword); | |
| 120 | -begin | |
| 121 | - while v >= $80 do begin | |
| 122 | - PutN(v and $7F or $80, 1); | |
| 123 | - v := v shr 7; | |
| 124 | - end; | |
| 125 | - PutN(v, 1); | |
| 126 | -end; | |
| 127 | - | |
| 128 | -{ The compile unit, which names the line table, and its abbreviation; | |
| 129 | - the table itself is in the places, in rodata (link.pas). Section | |
| 130 | - offsets go in off, sizes in size: line, info, abbrev. } | |
| 131 | -procedure PutDebug(var off, size: array of int64); | |
| 132 | -var cwd: array[0..255] of char; | |
| 133 | -begin | |
| 134 | - off[0] := chunks^[placesChunk].adr - roAdr + roOff + linesOff; | |
| 135 | - size[0] := linesSize; | |
| 136 | - off[1] := len; | |
| 137 | - PutN(0, 4); PutN(4, 2); PutN(0, 4); PutN(8, 1); { length, version, abbrev, address size } | |
| 138 | - PutU(1); | |
| 139 | - PutStr(mainPath); | |
| 140 | - if FpGetcwd(@cwd, SizeOf(cwd)) = nil then cwd[0] := #0; | |
| 141 | - PutStr(PChar(@cwd)); | |
| 142 | - PutStr('raz'); | |
| 143 | - PutN(0, 4); { the line table, at 0 } | |
| 144 | - PutN(Base + textOff, 8); PutN(textSize, 8); { the code, all of it } | |
| 145 | - Patch(off[1], len - off[1] - 4, 4); | |
| 146 | - size[1] := len - off[1]; | |
| 147 | - | |
| 148 | - off[2] := len; | |
| 149 | - PutU(1); PutU($11); PutN(0, 1); { 1: a compile unit, no children } | |
| 150 | - PutU($03); PutU($08); { name, a string } | |
| 151 | - PutU($1B); PutU($08); { comp_dir, a string } | |
| 152 | - PutU($25); PutU($08); { producer, a string } | |
| 153 | - PutU($10); PutU($17); { stmt_list, a section offset } | |
| 154 | - PutU($11); PutU($01); { low_pc, an address } | |
| 155 | - PutU($12); PutU($07); { high_pc, a length in 8 bytes } | |
| 156 | - PutN(0, 2); PutN(0, 1); | |
| 157 | - size[2] := len - off[2]; | |
| 158 | -end; | |
| 159 | - | |
| 160 | -const | |
| 161 | - Interp = '/lib64/ld-linux-x86-64.so.2'; | |
| 162 | - { program header types } | |
| 163 | - PtLoad = 1; PtDynamic = 2; PtInterp = 3; PtGnuStack = $6474E551; | |
| 164 | - | |
| 165 | -var | |
| 166 | - { in the room at the end of rodata: where each of the loader's | |
| 167 | - tables starts, from extraAdr } | |
| 168 | - symAt, relaAt, hashAt, strAt: int64; | |
| 169 | - nsym, strLen: longint; | |
| 170 | - | |
| 171 | -{ The sizes of the loader's tables: what LinkAll must leave room for. } | |
| 172 | -procedure SizeTables(var roExtra, dynSize: int64); | |
| 173 | -var c, i: longint; | |
| 174 | -begin | |
| 175 | - nsym := 1; | |
| 176 | - strLen := 1; | |
| 177 | - for i := 0 to numNeeded - 1 do strLen := strLen + length(NameOf(needed[i])) + 1; | |
| 178 | - for c := 0 to numChunks - 1 do | |
| 179 | - if chunks^[c].live and chunks^[c].ext then begin | |
| 180 | - inc(nsym); | |
| 181 | - strLen := strLen + length(NameOf(chunks^[c].name)) + 1; | |
| 182 | - end; | |
| 183 | - symAt := (length(Interp) + 1 + 7) div 8 * 8; | |
| 184 | - relaAt := symAt + nsym * 24; | |
| 185 | - hashAt := relaAt + (nsym - 1) * 24; | |
| 186 | - strAt := hashAt + 4 * (3 + nsym); | |
| 187 | - roExtra := strAt + strLen; | |
| 188 | - dynSize := (numNeeded + 10) * 16; | |
| 189 | -end; | |
| 190 | - | |
| 191 | -{ The loader's tables in the room at the end of rodata, which the | |
| 192 | - buffer already holds as zeros: the loader's name; the symbols, one | |
| 193 | - for each C symbol used; a GLOB_DAT relocation for each, at its | |
| 194 | - quadword; a hash table of one bucket; and their names. } | |
| 195 | -procedure PutTables; | |
| 196 | -var c, k, i, at, name: longint; | |
| 197 | -begin | |
| 198 | - len := extraAdr - roAdr + roOff; | |
| 199 | - PutStr(Interp); | |
| 200 | - PadTo(extraAdr - roAdr + roOff + symAt); | |
| 201 | - PutN(0, 24); | |
| 202 | - name := strLen; | |
| 203 | - for c := numChunks - 1 downto 0 do { names go last in the string table } | |
| 204 | - if chunks^[c].live and chunks^[c].ext then name := name - length(NameOf(chunks^[c].name)) - 1; | |
| 205 | - for c := 0 to numChunks - 1 do | |
| 206 | - if chunks^[c].live and chunks^[c].ext then begin | |
| 207 | - PutN(name, 4); PutN($10, 1); PutN(0, 1); PutN(0, 2); { global, undefined } | |
| 208 | - PutN(0, 8); PutN(0, 8); | |
| 209 | - name := name + length(NameOf(chunks^[c].name)) + 1; | |
| 210 | - end; | |
| 211 | - k := 1; | |
| 212 | - for c := 0 to numChunks - 1 do | |
| 213 | - if chunks^[c].live and chunks^[c].ext then begin | |
| 214 | - PutN(chunks^[c].adr, 8); | |
| 215 | - PutN(qword(k) shl 32 or 6, 8); { R_X86_64_GLOB_DAT } | |
| 216 | - PutN(0, 8); | |
| 217 | - inc(k); | |
| 218 | - end; | |
| 219 | - PutN(1, 4); PutN(nsym, 4); PutN(nsym - 1, 4); { the bucket chains every symbol } | |
| 220 | - PutN(0, 4); | |
| 221 | - for i := 1 to nsym - 1 do PutN(i - 1, 4); | |
| 222 | - PutN(0, 1); | |
| 223 | - for i := 0 to numNeeded - 1 do PutStr(NameOf(needed[i])); | |
| 224 | - for c := 0 to numChunks - 1 do | |
| 225 | - if chunks^[c].live and chunks^[c].ext then PutStr(NameOf(chunks^[c].name)); | |
| 226 | - len := roOff + roSize; | |
| 227 | - | |
| 228 | - { the dynamic table, at the start of the writable segment } | |
| 229 | - PadTo(bssOff); | |
| 230 | - at := 1; | |
| 231 | - for i := 0 to numNeeded - 1 do begin | |
| 232 | - PutN(1, 8); PutN(at, 8); { DT_NEEDED } | |
| 233 | - at := at + length(NameOf(needed[i])) + 1; | |
| 234 | - end; | |
| 235 | - PutN(4, 8); PutN(extraAdr + hashAt, 8); { DT_HASH } | |
| 236 | - PutN(5, 8); PutN(extraAdr + strAt, 8); { DT_STRTAB } | |
| 237 | - PutN(6, 8); PutN(extraAdr + symAt, 8); { DT_SYMTAB } | |
| 238 | - PutN(10, 8); PutN(strLen, 8); { DT_STRSZ } | |
| 239 | - PutN(11, 8); PutN(24, 8); { DT_SYMENT } | |
| 240 | - PutN(7, 8); PutN(extraAdr + relaAt, 8); { DT_RELA } | |
| 241 | - PutN(8, 8); PutN((nsym - 1) * 24, 8); { DT_RELASZ } | |
| 242 | - PutN(9, 8); PutN(24, 8); { DT_RELAENT } | |
| 243 | - PutN(21, 8); PutN(0, 8); { DT_DEBUG, for gdb } | |
| 244 | - PutN(0, 16); { DT_NULL } | |
| 245 | -end; | |
| 246 | - | |
| 247 | -procedure WriteElf(const path: shortstring; entry: longint); | |
| 248 | -var | |
| 249 | - nph, nsh, c, nsyms, fd: longint; | |
| 250 | - roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: int64; | |
| 251 | - dbgOff, dbgSize: array[0..2] of int64; | |
| 252 | - strs: PBytes; | |
| 253 | - s, p: shortstring; | |
| 254 | - sec: longint; | |
| 255 | - dynamic: boolean; | |
| 256 | -begin | |
| 257 | - cap := 1 shl 16; | |
| 258 | - buf := GetMem(cap); | |
| 259 | - len := 0; | |
| 260 | - dynamic := numNeeded > 0; | |
| 261 | - roExtra := 0; dynSize := 0; | |
| 262 | - if dynamic then SizeTables(roExtra, dynSize); | |
| 263 | - { room for the most program headers either kind of program has } | |
| 264 | - LinkAll(64 + 6 * 56, roExtra, dynSize); | |
| 265 | - if dynamic then nph := 6 | |
| 266 | - else begin | |
| 267 | - nph := 2; | |
| 268 | - if roSize > 0 then inc(nph); | |
| 269 | - if bssSize > 0 then inc(nph); | |
| 270 | - end; | |
| 271 | - | |
| 272 | - { ELF header } | |
| 273 | - PutN($464C457F, 4); { #127 'ELF' } | |
| 274 | - PutN(2, 1); PutN(1, 1); PutN(1, 1); PutN(0, 9); { 64-bit, little-endian, version 1 } | |
| 275 | - PutN(2, 2); { ET_EXEC } | |
| 276 | - PutN(62, 2); { EM_X86_64 } | |
| 277 | - PutN(1, 4); | |
| 278 | - PutN(chunks^[entry].adr, 8); | |
| 279 | - PutN(64, 8); { program headers follow } | |
| 280 | - PutN(0, 8); { section headers: patched below } | |
| 281 | - PutN(0, 4); | |
| 282 | - nsh := 10; | |
| 283 | - if dynamic then nsh := 13; | |
| 284 | - PutN(64, 2); PutN(56, 2); PutN(nph, 2); | |
| 285 | - PutN(64, 2); PutN(nsh, 2); PutN(6, 2); { .shstrtab is section 6 } | |
| 286 | - | |
| 287 | - { flags: 4 read, 5 read and run, 6 read and write } | |
| 288 | - if dynamic then Phdr(PtInterp, 4, extraAdr - roAdr + roOff, extraAdr, length(Interp) + 1, length(Interp) + 1, 1); | |
| 289 | - { the first segment maps the headers too, so text can follow them } | |
| 290 | - Phdr(PtLoad, 5, 0, Base, textOff + textSize, textOff + textSize, Page); | |
| 291 | - if roSize > 0 then Phdr(PtLoad, 4, roOff, roAdr, roSize, roSize, Page); | |
| 292 | - if dynamic then begin | |
| 293 | - Phdr(PtLoad, 6, bssOff, bssAdr, dynSize, bssSize, Page); | |
| 294 | - Phdr(PtDynamic, 6, bssOff, bssAdr, dynSize, dynSize, 8); | |
| 295 | - end else if bssSize > 0 then Phdr(PtLoad, 6, 0, bssAdr, 0, bssSize, Page); | |
| 296 | - Phdr(PtGnuStack, 6, 0, 0, 0, 0, 16); { a stack that can't run code } | |
| 297 | - | |
| 298 | - PadTo(textOff); | |
| 299 | - PutBytes(textImg, textSize); | |
| 300 | - PadTo(roOff); { even when empty, so that every section lies in the file } | |
| 301 | - PutBytes(roImg, roSize); | |
| 302 | - if dynamic then PutTables; | |
| 303 | - | |
| 304 | - { the symbol table: one local symbol for each named live chunk } | |
| 305 | - PadTo((len + 7) div 8 * 8); | |
| 306 | - symOff := len; | |
| 307 | - strs := GetMem(1 shl 16); | |
| 308 | - strsLen := 1; | |
| 309 | - strs^[0] := 0; | |
| 310 | - PutN(0, 24); | |
| 311 | - nsyms := 1; | |
| 312 | - for c := 0 to numChunks - 1 do | |
| 313 | - if not stripped and chunks^[c].live and (chunks^[c].name >= 0) and not chunks^[c].ext then begin | |
| 314 | - s := ChunkName(c); | |
| 315 | - if strsLen + length(s) + 1 > MemSize(strs) then strs := ReAllocMem(strs, strsLen * 2 + 256); | |
| 316 | - Move(s[1], strs^[strsLen], length(s)); | |
| 317 | - strs^[strsLen + length(s)] := 0; | |
| 318 | - case chunks^[c].sec of { the section's index in the headers below } | |
| 319 | - secText: sec := 1; | |
| 320 | - secRodata: sec := 2; | |
| 321 | - else sec := 3; | |
| 322 | - end; | |
| 323 | - PutN(strsLen, 4); | |
| 324 | - if sec = 1 then PutN(2, 1) else PutN(1, 1); { STT_FUNC, STT_OBJECT } | |
| 325 | - PutN(0, 1); | |
| 326 | - PutN(sec, 2); | |
| 327 | - PutN(chunks^[c].adr, 8); | |
| 328 | - PutN(chunks^[c].size, 8); | |
| 329 | - strsLen := strsLen + length(s) + 1; | |
| 330 | - inc(nsyms); | |
| 331 | - end; | |
| 332 | - strOff := len; | |
| 333 | - PutBytes(strs, strsLen); | |
| 334 | - shstrOff := len; | |
| 335 | - s := ShNames; | |
| 336 | - PutBytes(@s[1], length(s)); | |
| 337 | - if stripped or (placesChunk < 0) then begin | |
| 338 | - for c := 0 to 2 do begin dbgOff[c] := len; dbgSize[c] := 0 end; | |
| 339 | - end else PutDebug(dbgOff, dbgSize); | |
| 340 | - | |
| 341 | - PadTo((len + 7) div 8 * 8); | |
| 342 | - shOff := len; | |
| 343 | - Patch(40, shOff, 8); | |
| 344 | - { types: 1 PROGBITS, 2 SYMTAB, 3 STRTAB, 6 DYNAMIC, 8 NOBITS, 11 DYNSYM; | |
| 345 | - flags: 2 ALLOC, 3 WRITE+ALLOC, 6 ALLOC+EXECINSTR. A symbol table's | |
| 346 | - link is its string table's index: 5 .strtab, 8 .dynstr. The order | |
| 347 | - here is the index the code above relies on. } | |
| 348 | - Shdr('', 0, 0, 0, 0, 0, 0, 0, 0, 0); | |
| 349 | - Shdr('.text', 1, 6, Base + textOff, textOff, textSize, 0, 0, 16, 0); | |
| 350 | - Shdr('.rodata', 1, 2, roAdr, roOff, roSize, 0, 0, 16, 0); | |
| 351 | - Shdr('.bss', 8, 3, bssAdr + dynSize, roOff + roSize, bssSize - dynSize, 0, 0, 16, 0); | |
| 352 | - Shdr('.symtab', 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24); | |
| 353 | - Shdr('.strtab', 3, 0, 0, strOff, strsLen, 0, 0, 1, 0); | |
| 354 | - Shdr('.shstrtab', 3, 0, 0, shstrOff, length(ShNames), 0, 0, 1, 0); | |
| 355 | - if dynamic then begin { the loader's tables, for readelf and objdump } | |
| 356 | - Shdr('.dynamic', 6, 3, bssAdr, bssOff, dynSize, 8, 0, 8, 16); | |
| 357 | - Shdr('.dynstr', 3, 2, extraAdr + strAt, extraAdr + strAt - roAdr + roOff, strLen, 0, 0, 1, 0); | |
| 358 | - Shdr('.dynsym', 11, 2, extraAdr + symAt, extraAdr + symAt - roAdr + roOff, nsym * 24, 8, 1, 8, 24); | |
| 359 | - end; | |
| 360 | - Shdr('.debug_line', 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0); | |
| 361 | - Shdr('.debug_info', 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0); | |
| 362 | - Shdr('.debug_abbrev', 1, 0, 0, dbgOff[2], dbgSize[2], 0, 0, 1, 0); | |
| 363 | - | |
| 364 | - p := path + #0; | |
| 365 | - fd := FpOpen(@p[1], O_WRONLY or O_CREAT or O_TRUNC, &755); | |
| 366 | - if fd < 0 then Fatal('can''t write ' + path); | |
| 367 | - if FpWrite(fd, buf^, len) <> len then Fatal('can''t write ' + path); | |
| 368 | - FpClose(fd); | |
| 369 | - FreeMem(strs); | |
| 370 | - FreeMem(buf); | |
| 371 | -end; | |
| 372 | - | |
| 373 | -end. |
| @@ -1,2322 +0,0 @@ | ||
| 1 | -{ gen.pas: the code generator, for x86-64. | |
| 2 | - | |
| 3 | - item.pas and parse.pas drive a stack machine: they push constants, | |
| 4 | - addresses and the values of variables, operate on the values on top, | |
| 5 | - store them and jump on them. Every value has a kind: a word, which | |
| 6 | - is an integer as big as an address, a pointer, a BOOL, a set or a | |
| 7 | - narrower integer; a long, a 64-bit integer on a machine whose word is | |
| 8 | - smaller (never here); an F32; an F64. This unit makes the machine's | |
| 9 | - code for those calls, and nothing above it knows the machine: a port | |
| 10 | - is a new gen.pas, with an encoder of its own. | |
| 11 | - | |
| 12 | - The stack is kept in registers, as in Wirth's compilers (Niklaus | |
| 13 | - Wirth's Pascal and Oberon compilers): the value at depth d, depth 0 | |
| 14 | - being the bottom, is in r8 to r11, register d mod 4, or in xmm8 to | |
| 15 | - xmm11 if it is a real. A value that needs a register a lower one | |
| 16 | - holds spills that one: stores it in a frame slot, to free the | |
| 17 | - register. A constant, or an address in the | |
| 18 | - frame or of a chunk, is only noted until something needs it in a | |
| 19 | - register, so that it can be an instruction's operand. | |
| 20 | - | |
| 21 | - A slice is two words, a pointer and then a length. | |
| 22 | - | |
| 23 | - Calls use the System V convention, which x86-64 Linux has for C: | |
| 24 | - arguments in rdi, rsi, rdx, rcx, r8, r9 and xmm0 to xmm7, the rest on | |
| 25 | - the machine's stack; rbx, rbp and r12 to r15 are callee-saved, which | |
| 26 | - is to say a called procedure gives them back as it found them. The | |
| 27 | - arguments are computed left to right onto the stack of values, and | |
| 28 | - moved into their registers just before the call: a call in a later | |
| 29 | - argument spills the earlier ones, as any call spills the values under | |
| 30 | - it, so that it can't disturb them. A slice goes as its two words, as | |
| 31 | - System V passes a 16-byte struct of two integers; a record, array or | |
| 32 | - variant passed by value goes as the address of a copy the caller | |
| 33 | - makes. A slice, or two results that are single values, come back in | |
| 34 | - rax and rdx (InTwoRegs); any other record, or several results, comes | |
| 35 | - back in memory the caller provides, through a hidden pointer | |
| 36 | - (BehindPointer), where C would send a small struct back in registers. | |
| 37 | - | |
| 38 | - A procedure's frame holds its parameters, its variables, and the | |
| 39 | - temporaries of the statement running. The variables are zeroed on | |
| 40 | - entry. | |
| 41 | - | |
| 42 | - Depends on: scan and diag for errors, sym for types, chunk for code | |
| 43 | - and data, x64 for instructions. | |
| 44 | - Leaves out: what the program means, its types and their checks; that | |
| 45 | - is item.pas. } | |
| 46 | - | |
| 47 | -unit gen; | |
| 48 | - | |
| 49 | -{$mode objfpc}{$H-} | |
| 50 | - | |
| 51 | -interface | |
| 52 | - | |
| 53 | -uses scan, diag, sym, chunk, x64; | |
| 54 | - | |
| 55 | -const | |
| 56 | - NoLabel = -1; | |
| 57 | - WordSize = 8; { the size of an address, and of INT } | |
| 58 | - { What the machine catches by itself, which the code needn't test for | |
| 59 | - (report section 10): on x86-64 Linux, a division by zero, a stack | |
| 60 | - overflow (at the guard page under the stack), a call to nil, and | |
| 61 | - a load or store within NilPage bytes of address 0, which is never | |
| 62 | - mapped. } | |
| 63 | - NilPage = 4096; | |
| 64 | - | |
| 65 | -type | |
| 66 | - TKind = (kWord, kLong, kF32, kF64); | |
| 67 | - | |
| 68 | - { Where a variable is: its address is relative to the frame, to a | |
| 69 | - chunk, to an address on the stack, or to one held in a frame slot } | |
| 70 | - TBase = (bFrame, bGlobal, bStack, bSlot, bReg); | |
| 71 | - TAdr = record | |
| 72 | - base: TBase; | |
| 73 | - chunk: longint; { bGlobal: the chunk; bSlot: the frame slot; bReg: the register } | |
| 74 | - off: int64; { added to the address; for bReg, which word, 0 or 8 } | |
| 75 | - hi: longint; { bReg, a slice: the register of its length, the | |
| 76 | - second word; chunk is that of its pointer } | |
| 77 | - end; | |
| 78 | - | |
| 79 | - { The code made so far, to go back to for a second try at a body } | |
| 80 | - TCodeMark = record pc, fixups, lines, said, fails: longint; end; | |
| 81 | - | |
| 82 | - { For EntryPoint: the address of chunk, put in the variable whose | |
| 83 | - chunk is into before the program starts } | |
| 84 | - TLink = record chunk, into: longint; end; | |
| 85 | - | |
| 86 | - TCall = record | |
| 87 | - sig: PType; | |
| 88 | - param: PObj; { the parameter the next argument is for } | |
| 89 | - obj: PObj; { the procedure called, if it has a name } | |
| 90 | - n: longint; { the arguments } | |
| 91 | - isF: array[0..63] of boolean; { ... goes in an xmm register } | |
| 92 | - pair: array[0..63] of boolean; { ... is a slice's first word, the next its second } | |
| 93 | - target: int64; { the frame slot of a procedure value, if chunk < 0 } | |
| 94 | - chunk: longint; { else the procedure's chunk } | |
| 95 | - ext: boolean; { C's: called through the chunk holding its address } | |
| 96 | - ret: int64; { set by Call: the temporary a slice or a record result is in } | |
| 97 | - end; | |
| 98 | - | |
| 99 | -const | |
| 100 | - { the registers a procedure keeps its busiest variables in: System V's | |
| 101 | - callee-saved ones, so they live through calls (a register variable); a procedure saves | |
| 102 | - those it uses, and Rt finds them there for the trap report } | |
| 103 | - VarRegs: array[0..4] of longint = (RBX, R12, R13, R14, R15); | |
| 104 | - { the registers the trap report can show a variable in: VarRegs, then | |
| 105 | - rdi and rsi, where a procedure that calls nothing keeps the | |
| 106 | - parameters that arrive in them (KeptIn) } | |
| 107 | - ShownRegs: array[0..6] of longint = (RBX, R12, R13, R14, R15, RDI, RSI); | |
| 108 | - { the registers a procedure keeps its busiest reals in, xmm12 to | |
| 109 | - xmm15, numbered from XmmVar up so that they aren't taken for r12 to | |
| 110 | - r15 (RegAt). Each has a slot too, written as it is, where the trap | |
| 111 | - report finds it, and from which it is loaded again after a call, as | |
| 112 | - System V has no callee-saved xmm register (XHome). } | |
| 113 | - XmmVar = 16; | |
| 114 | - XVarRegs: array[0..3] of longint = (XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15); | |
| 115 | - { a procedure's registers of variables, as bits: bit i is VarRegs[i], | |
| 116 | - bit XBit + i XVarRegs[i] } | |
| 117 | - XBit = 5; | |
| 118 | - | |
| 119 | -var | |
| 120 | - rtFail, argcChunk, argvChunk: longint; | |
| 121 | - { the stubs that Check's code calls, one for each kind of failure, | |
| 122 | - made by EntryPoint for those used } | |
| 123 | - failStubs: array[0..5] of longint = (-1, -1, -1, -1, -1, -1); | |
| 124 | - lost: boolean; { a parameter kept where it arrived was wanted | |
| 125 | - for other work: the procedure is compiled again, | |
| 126 | - the parameter in memory } | |
| 127 | - inPlace: longint = -1; { the register of a variable that the statement | |
| 128 | - being compiled assigns, x := x + e, where the | |
| 129 | - value can be worked out in the variable's own | |
| 130 | - register, from its first push on (lInto) } | |
| 131 | - depth: longint; { the values on the stack } | |
| 132 | - pushed: longint; { quadwords pushed on the machine's stack } | |
| 133 | - codeChunk: longint = -1; { the procedure or body being compiled, or -1 } | |
| 134 | - | |
| 135 | -function KindOf(t: PType): TKind; | |
| 136 | - | |
| 137 | -procedure PushConst(k: TKind; v: int64); | |
| 138 | -procedure PushAdr(const a: TAdr); | |
| 139 | -procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind); | |
| 140 | -procedure PushProc(c: longint; ext: boolean); | |
| 141 | -procedure PopVar(const a: TAdr; size: longint); | |
| 142 | -procedure Drop; | |
| 143 | -procedure Swap; | |
| 144 | -procedure ResetStack(d, p: longint); | |
| 145 | - | |
| 146 | -procedure CopyBlock(size, off: int64); | |
| 147 | -procedure CopyTo(const a: TAdr; size: int64); | |
| 148 | -procedure ZeroVar(const a: TAdr; size: int64); | |
| 149 | - | |
| 150 | -procedure Op(op: TSym; k: TKind; size: longint; signed: boolean); | |
| 151 | -procedure Compare(op: TSym; k: TKind; signed: boolean); | |
| 152 | -procedure Neg(k: TKind; size: longint; signed: boolean); | |
| 153 | -procedure Complement(size: longint; signed: boolean); | |
| 154 | -procedure NotBool; | |
| 155 | -procedure Narrow(size: longint; signed: boolean); | |
| 156 | -procedure IntToReal(k: TKind; big: boolean); | |
| 157 | -procedure RealToInt(k: TKind); | |
| 158 | -procedure RealToReal(k: TKind); | |
| 159 | -procedure CompareSeq(a, b: int64; shift: longint; eq: boolean); | |
| 160 | -procedure ErrorText(tmp: int64); | |
| 161 | -procedure IncDec(size: longint; signed, dec: boolean); | |
| 162 | -procedure IncDecReg(r, size: longint; signed, dec: boolean); | |
| 163 | -procedure ZeroNew(size: int64); | |
| 164 | -procedure Atomic(which: longint); | |
| 165 | -procedure SysCall(n: longint); | |
| 166 | -procedure Fence; | |
| 167 | - | |
| 168 | -procedure Jump(var L: longint); | |
| 169 | -procedure JumpIf(var L: longint; when: boolean); | |
| 170 | -procedure JumpCmp(op: TSym; signed: boolean; var L: longint); | |
| 171 | -procedure JumpOut(op: TSym; signed: boolean; var L: longint); | |
| 172 | -procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint); | |
| 173 | -procedure JumpBack(top: longint); | |
| 174 | -function JumpTable(slot, lo: int64; n, elseAt: longint; var endL: longint): longint; | |
| 175 | -procedure TableEntry(table, target: longint); | |
| 176 | -procedure AlignLoop; | |
| 177 | -procedure Fix(L: longint); | |
| 178 | -procedure FixTo(L, top: longint); | |
| 179 | -function JoinJumps(L1, L2: longint): longint; | |
| 180 | - | |
| 181 | -procedure CheckIndex(n: int64; const p: TPos); | |
| 182 | -procedure CheckEnum(n: int64; name: longint; const p: TPos); | |
| 183 | -procedure CheckNil(reach: int64; const p: TPos); | |
| 184 | -procedure IndexArray(const a: TAdr; size: int64); | |
| 185 | -procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); | |
| 186 | -procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); | |
| 187 | -procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); | |
| 188 | -procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); | |
| 189 | - | |
| 190 | -procedure Call(var c: TCall); | |
| 191 | - | |
| 192 | -function AllocTemp(size, align: int64): int64; | |
| 193 | -function AllocPerm(size, align: int64): int64; | |
| 194 | -function TempMark: int64; | |
| 195 | -procedure ReleaseTemps(m: int64); | |
| 196 | -procedure KeepTemps; | |
| 197 | - | |
| 198 | -procedure BeginProc(sig: PType; params: PObj); | |
| 199 | -procedure ResetLocals; | |
| 200 | -procedure KeepArrived(o: PObj); | |
| 201 | -function KeptIn(o: PObj): boolean; | |
| 202 | -procedure UseRegs(mask: longint); | |
| 203 | -procedure XHome(r: longint; slot: int64); | |
| 204 | -procedure MarkCode(var m: TCodeMark); | |
| 205 | -procedure Redo(const m: TCodeMark); | |
| 206 | -procedure Rewind(const m: TCodeMark); | |
| 207 | -procedure EnterBody(c, line: longint); | |
| 208 | -procedure ResultTo(off: int64; size: longint); | |
| 209 | -procedure CopyResult(off, size: int64); | |
| 210 | -procedure ReturnJump; | |
| 211 | -procedure TryAlign; | |
| 212 | -procedure TryReturn(slot: int64); | |
| 213 | -procedure EndProc(c: longint; open: boolean); | |
| 214 | -function DeferBegin(var L, saved: longint): longint; | |
| 215 | -procedure DeferEnd(L, saved: longint); | |
| 216 | -procedure RunDefer(at: longint); | |
| 217 | - | |
| 218 | -function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, exitProc: longint; const links: array of TLink; numLinks: longint): longint; | |
| 219 | -procedure CodeProc(c: longint; constExpr: TConstFn); | |
| 220 | - | |
| 221 | -implementation | |
| 222 | - | |
| 223 | -type | |
| 224 | - { where a value on the stack is } | |
| 225 | - TLoc = (lConst, { not loaded: val is the value } | |
| 226 | - lHeld, { not loaded: in register val, a register | |
| 227 | - variable's own (see VarRegs), read in place, which | |
| 228 | - nothing in an expression can change } | |
| 229 | - lAdr, { not loaded: the address of chunk, or of the frame | |
| 230 | - if chunk is -1, plus val } | |
| 231 | - lIdx, { not loaded: the address m, an element of an array | |
| 232 | - or of a slice in registers: the array's or the | |
| 233 | - pointer's register, or rbp, as the base, and the | |
| 234 | - index in a register variable's or the value's | |
| 235 | - own register (IndexSlice, IndexArray) } | |
| 236 | - lMem, { not loaded: the value of size bytes at m, in the | |
| 237 | - frame, a chunk, or at an address lAdr or lIdx | |
| 238 | - noted, read where it is used (PushVar) } | |
| 239 | - lInto, { in register val, a register variable's own, which | |
| 240 | - is what the value goes to (inPlace): so it is | |
| 241 | - changed in place } | |
| 242 | - lReg, { in its register } | |
| 243 | - lFrame); { spilled: in the frame slot val } | |
| 244 | - | |
| 245 | - TEntry = record | |
| 246 | - loc: TLoc; | |
| 247 | - val: int64; { lConst: the value; lHeld: the register; lAdr: the | |
| 248 | - offset; lFrame: the frame slot } | |
| 249 | - chunk: longint; { lAdr: the chunk, or -1 for the frame } | |
| 250 | - m: TMem; { lIdx, lMem } | |
| 251 | - size: longint; { lMem } | |
| 252 | - signed: boolean; { lMem } | |
| 253 | - k: TKind; | |
| 254 | - off: int64; { lHeld, lReg: a word, its register plus off, the | |
| 255 | - constant of x + 1 not yet added (Op), so that an | |
| 256 | - operand that uses it can take it as its | |
| 257 | - displacement: [r13 + r9 * 8 - 8] } | |
| 258 | - end; | |
| 259 | - | |
| 260 | -const | |
| 261 | - StubNames: array[0..5] of string[6] = ('check0', 'check1', 'check2', 'check3', 'check4', 'check5'); | |
| 262 | - { the registers of the stack: the value at depth d is in Regs[d mod | |
| 263 | - NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi | |
| 264 | - and rdi, which copying needs, aren't among them. } | |
| 265 | - NumRegs = 4; | |
| 266 | - Regs: array[0..NumRegs - 1] of longint = (R8, R9, R10, R11); | |
| 267 | - XRegs: array[0..NumRegs - 1] of longint = (8, 9, 10, 11); { xmm8 to xmm11 } | |
| 268 | - IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9); | |
| 269 | - | |
| 270 | -var | |
| 271 | - stack: array[0..255] of TEntry; | |
| 272 | - owner: array[0..NumRegs - 1] of longint; { the value in each register, or -1 } | |
| 273 | - frameTop, frameMax, permTop: int64; | |
| 274 | - frameFix: longint; { where the frame size is patched in } | |
| 275 | - paramTop: int64; { the parameters' slots end here } | |
| 276 | - zeroTop: int64; { the locals the prologue zeroes end here } | |
| 277 | - retSlot: int64; { holds the hidden result pointer } | |
| 278 | - resType: PType; { the procedure's result, or nil } | |
| 279 | - exitL: longint; | |
| 280 | - { parameters arriving in registers: where each is stored on entry } | |
| 281 | - inReg: array[0..13] of longint; | |
| 282 | - inSlot: array[0..13] of int64; | |
| 283 | - inSize: array[0..13] of longint; | |
| 284 | - inObj: array[0..13] of PObj; | |
| 285 | - numIn: longint; | |
| 286 | - procParams: PObj; { the parameters, for EnterBody } | |
| 287 | - regMask: longint; { the registers of variables, as bits (XBit) } | |
| 288 | - kept: longint; { bit r: register r holds a parameter, as it arrived } | |
| 289 | - saveAt: int64; { where they are saved, a quadword each } | |
| 290 | - homes: array[0..3] of int64; { the slots of the variables in XVarRegs } | |
| 291 | - frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations } | |
| 292 | - { the checks of the procedure, whose failures EndFails makes } | |
| 293 | - fails: array[0..4095] of record | |
| 294 | - L, what, ra, name, at: longint; | |
| 295 | - m: TMem; | |
| 296 | - n: int64; | |
| 297 | - end; | |
| 298 | - numFails: longint; | |
| 299 | - | |
| 300 | -function KindOf(t: PType): TKind; | |
| 301 | -begin | |
| 302 | - if t^.form = fReal then begin | |
| 303 | - if t^.size = 4 then KindOf := kF32 else KindOf := kF64 | |
| 304 | - end else if t^.size > WordSize then KindOf := kLong | |
| 305 | - else KindOf := kWord; | |
| 306 | -end; | |
| 307 | - | |
| 308 | -{ The stack, as in Wirth's compilers: the value at depth d is held in | |
| 309 | - register d mod NumRegs. A value, when a higher one wants its register, | |
| 310 | - and every value in a register when a call would clobber them, is | |
| 311 | - spilled to a frame slot, and loaded again where it is used. A constant | |
| 312 | - is only noted. | |
| 313 | - | |
| 314 | - Drop emits no code, and a dropped value's register still holds it | |
| 315 | - until the next push claims it: Place (for bStack), Operands, | |
| 316 | - IndexSlice and JumpCmp read a register after dropping its value. | |
| 317 | - | |
| 318 | - Registers outside the stack, free within one operation: rax, rcx and | |
| 319 | - rdx for arithmetic; rsi and rdi for copying; and, where the stack holds | |
| 320 | - nothing in them, r10 for the prologue's zeroing loop and r11 for a call | |
| 321 | - through a procedure value; xmm0 and xmm1 for reals. | |
| 322 | - | |
| 323 | - A real, an F32 or an F64, is held in xmm8 to xmm11 (XRegs), where the | |
| 324 | - machine computes with reals, the value at depth d in XRegs[d mod | |
| 325 | - NumRegs]: so each depth has two registers, and holds its value in the | |
| 326 | - one of its kind, which owner doesn't tell apart. The xmm registers | |
| 327 | - from xmm8 up carry no arguments, so that Call can move a real into | |
| 328 | - xmm0 to xmm7 without overwriting another still to go. } | |
| 329 | - | |
| 330 | -function Fits32(v: int64): boolean; | |
| 331 | -begin | |
| 332 | - Fits32 := (v >= low(longint)) and (v <= high(longint)); | |
| 333 | -end; | |
| 334 | - | |
| 335 | -{ The register of kind k for the value at depth i } | |
| 336 | -function RegIn(k: TKind; i: longint): longint; | |
| 337 | -begin | |
| 338 | - if k in [kF32, kF64] then RegIn := XRegs[i mod NumRegs] else RegIn := Regs[i mod NumRegs]; | |
| 339 | -end; | |
| 340 | - | |
| 341 | -function RegOf(i: longint): longint; | |
| 342 | -begin | |
| 343 | - RegOf := RegIn(stack[i].k, i); | |
| 344 | -end; | |
| 345 | - | |
| 346 | -function IsReal(i: longint): boolean; | |
| 347 | -begin | |
| 348 | - IsReal := stack[i].k in [kF32, kF64]; | |
| 349 | -end; | |
| 350 | - | |
| 351 | -{ A real constant, its bits v, as an operand: 8 bytes in read-only | |
| 352 | - data, as the machine has no instruction that holds one } | |
| 353 | -function RealConst(v: int64): TMem; | |
| 354 | -var c: longint; | |
| 355 | -begin | |
| 356 | - c := NewChunk(secRodata, -1, -1); | |
| 357 | - SetData(c, @v, 8, 8); | |
| 358 | - RealConst := MemRip(c, 0); | |
| 359 | -end; | |
| 360 | - | |
| 361 | -{ The real i into xmm register x, from wherever it is, as ValueTo does | |
| 362 | - for an integer register } | |
| 363 | -procedure XValueTo(i, x: longint); | |
| 364 | -var single: boolean; | |
| 365 | -begin | |
| 366 | - single := stack[i].k = kF32; | |
| 367 | - case stack[i].loc of | |
| 368 | - lConst: MovsXM(x, RealConst(stack[i].val), single); | |
| 369 | - lMem: MovsXM(x, stack[i].m, stack[i].size = 4); | |
| 370 | - lHeld, lInto: if stack[i].val <> x then MovX(x, stack[i].val); | |
| 371 | - lReg: if RegOf(i) <> x then MovX(x, RegOf(i)); | |
| 372 | - else MovsXM(x, Mem(RBP, stack[i].val), false); { spilled, as 8 bytes } | |
| 373 | - end; | |
| 374 | -end; | |
| 375 | - | |
| 376 | -function Loaded(i: longint): longint; forward; | |
| 377 | - | |
| 378 | -{ The value i, in a register, goes to a frame slot; one noted that | |
| 379 | - holds its register, as an index, is worked out in it first } | |
| 380 | -procedure Spill(i: longint); | |
| 381 | -var s: int64; | |
| 382 | -begin | |
| 383 | - if (stack[i].loc <> lReg) or (stack[i].off <> 0) then Loaded(i); | |
| 384 | - s := AllocTemp(8, 8); | |
| 385 | - if IsReal(i) then MovsMX(Mem(RBP, s), RegOf(i), false) | |
| 386 | - else Store(Mem(RBP, s), RegOf(i), 8); | |
| 387 | - stack[i].loc := lFrame; | |
| 388 | - stack[i].val := s; | |
| 389 | - owner[i mod NumRegs] := -1; | |
| 390 | -end; | |
| 391 | - | |
| 392 | -{ Each value below depth d read from memory now, as what comes next | |
| 393 | - may change it } | |
| 394 | -procedure ReadMems(d: longint); | |
| 395 | -var i: longint; | |
| 396 | -begin | |
| 397 | - for i := 0 to d - 1 do | |
| 398 | - if stack[i].loc = lMem then Loaded(i); | |
| 399 | -end; | |
| 400 | - | |
| 401 | -{ Every value below depth d in a register, spilled; one noted in memory | |
| 402 | - is read first, as a call may change it } | |
| 403 | -procedure SpillBelow(d: longint); | |
| 404 | -var r: longint; | |
| 405 | -begin | |
| 406 | - ReadMems(d); | |
| 407 | - for r := 0 to NumRegs - 1 do | |
| 408 | - if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]); | |
| 409 | -end; | |
| 410 | - | |
| 411 | -{ The register of kind k of value i, for it to hold: a lower value at | |
| 412 | - its depth's registers is spilled. } | |
| 413 | -function Claim(i: longint; k: TKind): longint; | |
| 414 | -var r: longint; | |
| 415 | -begin | |
| 416 | - r := i mod NumRegs; | |
| 417 | - if (owner[r] >= 0) and (owner[r] <> i) then Spill(owner[r]); | |
| 418 | - owner[r] := i; | |
| 419 | - Claim := RegIn(k, i); | |
| 420 | -end; | |
| 421 | - | |
| 422 | -{ The memory at the noted address i, plus off } | |
| 423 | -function AdrMem(i: longint; off: int64): TMem; | |
| 424 | -begin | |
| 425 | - if stack[i].chunk < 0 then AdrMem := Mem(RBP, stack[i].val + off) | |
| 426 | - else AdrMem := MemRip(stack[i].chunk, stack[i].val + off); | |
| 427 | -end; | |
| 428 | - | |
| 429 | -{ The value i, in its register } | |
| 430 | -function Loaded(i: longint): longint; | |
| 431 | -var r: longint; | |
| 432 | -begin | |
| 433 | - if stack[i].loc = lInto then exit(stack[i].val); | |
| 434 | - if (stack[i].loc <> lReg) and IsReal(i) then begin | |
| 435 | - XValueTo(i, Claim(i, stack[i].k)); | |
| 436 | - stack[i].loc := lReg; | |
| 437 | - end else if stack[i].loc <> lReg then begin | |
| 438 | - r := Claim(i, kWord); | |
| 439 | - case stack[i].loc of | |
| 440 | - lConst: MovRI(r, stack[i].val); | |
| 441 | - lHeld: MovRR(r, stack[i].val); | |
| 442 | - lAdr: Lea(r, AdrMem(i, 0)); | |
| 443 | - lIdx: Lea(r, stack[i].m); | |
| 444 | - lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed); | |
| 445 | - else Load(r, Mem(RBP, stack[i].val), 8, false); | |
| 446 | - end; | |
| 447 | - stack[i].loc := lReg; | |
| 448 | - end; | |
| 449 | - if stack[i].off <> 0 then begin | |
| 450 | - Lea(RegOf(i), Mem(RegOf(i), stack[i].off)); | |
| 451 | - stack[i].off := 0; | |
| 452 | - end; | |
| 453 | - Loaded := RegOf(i); | |
| 454 | -end; | |
| 455 | - | |
| 456 | -{ The register value i is in, only to be read: a variable's own, or | |
| 457 | - its register } | |
| 458 | -function Peek(i: longint): longint; | |
| 459 | -begin | |
| 460 | - if (stack[i].loc = lHeld) and (stack[i].off = 0) then Peek := stack[i].val else Peek := Loaded(i); | |
| 461 | -end; | |
| 462 | - | |
| 463 | -{ The offset of value i, taken from it, for the caller to add or to | |
| 464 | - make a displacement of } | |
| 465 | -function TakeOff(i: longint): int64; | |
| 466 | -begin | |
| 467 | - TakeOff := stack[i].off; | |
| 468 | - stack[i].off := 0; | |
| 469 | -end; | |
| 470 | - | |
| 471 | -{ The word on top, in a register, has o added: noted, if o can be a | |
| 472 | - displacement, else added now } | |
| 473 | -procedure PutOff(o: int64); | |
| 474 | -begin | |
| 475 | - if Fits32(stack[depth - 1].off + o) then stack[depth - 1].off := stack[depth - 1].off + o | |
| 476 | - else begin | |
| 477 | - MovRI(RCX, o); | |
| 478 | - Alu(aluAdd, Loaded(depth - 1), RCX); | |
| 479 | - end; | |
| 480 | -end; | |
| 481 | - | |
| 482 | -function Top: longint; | |
| 483 | -begin | |
| 484 | - if depth = 0 then Fatal('internal error: the stack is empty'); | |
| 485 | - Top := Loaded(depth - 1); | |
| 486 | -end; | |
| 487 | - | |
| 488 | -procedure NewEntry(k: TKind; loc: TLoc; v: int64); | |
| 489 | -begin | |
| 490 | - if depth > High(stack) then Mark('the expression is too deep'); | |
| 491 | - stack[depth].k := k; | |
| 492 | - stack[depth].loc := loc; | |
| 493 | - stack[depth].val := v; | |
| 494 | - stack[depth].chunk := -1; | |
| 495 | - stack[depth].off := 0; | |
| 496 | - inc(depth); | |
| 497 | -end; | |
| 498 | - | |
| 499 | -{ An address, lIdx, or a value at it, lMem, noted as the operand m. Its | |
| 500 | - index may be in the register of the value just dropped, at this | |
| 501 | - depth: it is the new value's own, then. } | |
| 502 | -procedure NewMem(loc: TLoc; const m: TMem; k: TKind; size: longint; signed: boolean); | |
| 503 | -begin | |
| 504 | - NewEntry(k, loc, 0); | |
| 505 | - stack[depth - 1].m := m; | |
| 506 | - stack[depth - 1].size := size; | |
| 507 | - stack[depth - 1].signed := signed; | |
| 508 | - if m.index = Regs[(depth - 1) mod NumRegs] then owner[(depth - 1) mod NumRegs] := depth - 1; | |
| 509 | -end; | |
| 510 | - | |
| 511 | -{ The register a new value on top is to be computed in } | |
| 512 | -function Target: longint; | |
| 513 | -begin | |
| 514 | - Target := Claim(depth, kWord); | |
| 515 | -end; | |
| 516 | - | |
| 517 | -{ The xmm register a new real on top is to be computed in } | |
| 518 | -function XTarget: longint; | |
| 519 | -begin | |
| 520 | - XTarget := Claim(depth, kF64); | |
| 521 | -end; | |
| 522 | - | |
| 523 | -{ The value just computed in Target goes on top } | |
| 524 | -procedure Computed(k: TKind); | |
| 525 | -begin | |
| 526 | - NewEntry(k, lReg, 0); | |
| 527 | -end; | |
| 528 | - | |
| 529 | -procedure Drop; | |
| 530 | -begin | |
| 531 | - dec(depth); | |
| 532 | - if owner[depth mod NumRegs] = depth then owner[depth mod NumRegs] := -1; | |
| 533 | -end; | |
| 534 | - | |
| 535 | -{ rsi and rdi are about to be used for other work, as in copying: a | |
| 536 | - parameter kept in either must go to memory (lost). } | |
| 537 | -procedure ClobberArgs; | |
| 538 | -begin | |
| 539 | - if kept <> 0 then lost := true; | |
| 540 | -end; | |
| 541 | - | |
| 542 | -{ The value i into register r, from wherever it is: a constant or an | |
| 543 | - address is made there, a variable's register or the value's own | |
| 544 | - register copied, a spilled value loaded. Unlike Loaded, it leaves the | |
| 545 | - value where it was, so r is its only new copy. } | |
| 546 | -procedure ValueTo(i, r: longint); | |
| 547 | -begin | |
| 548 | - if IsReal(i) and (stack[i].loc in [lHeld, lInto]) then MovqRX(r, stack[i].val) | |
| 549 | - else case stack[i].loc of | |
| 550 | - lConst: MovRI(r, stack[i].val); | |
| 551 | - lHeld, lInto: MovRR(r, stack[i].val); | |
| 552 | - lAdr: Lea(r, AdrMem(i, 0)); | |
| 553 | - lIdx: Lea(r, stack[i].m); | |
| 554 | - lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed); | |
| 555 | - lReg: | |
| 556 | - if IsReal(i) then MovqRX(r, RegOf(i)) | |
| 557 | - else if RegOf(i) <> r then MovRR(r, RegOf(i)); | |
| 558 | - else Load(r, Mem(RBP, stack[i].val), 8, false); | |
| 559 | - end; | |
| 560 | - if stack[i].off <> 0 then Lea(r, Mem(r, stack[i].off)); | |
| 561 | -end; | |
| 562 | - | |
| 563 | -{ The value on top, popped into register r } | |
| 564 | -procedure PopTo(r: longint); | |
| 565 | -begin | |
| 566 | - ValueTo(depth - 1, r); | |
| 567 | - Drop; | |
| 568 | -end; | |
| 569 | - | |
| 570 | -{ Whether the value on top is a constant, and if so, which } | |
| 571 | -function TopConst(var v: int64): boolean; | |
| 572 | -begin | |
| 573 | - TopConst := stack[depth - 1].loc = lConst; | |
| 574 | - v := stack[depth - 1].val; | |
| 575 | -end; | |
| 576 | - | |
| 577 | -procedure ResetStack(d, p: longint); | |
| 578 | -var r: longint; | |
| 579 | -begin | |
| 580 | - depth := d; | |
| 581 | - pushed := p; | |
| 582 | - for r := 0 to NumRegs - 1 do owner[r] := -1; | |
| 583 | -end; | |
| 584 | - | |
| 585 | -procedure PushConst(k: TKind; v: int64); | |
| 586 | -begin | |
| 587 | - NewEntry(k, lConst, v); | |
| 588 | -end; | |
| 589 | - | |
| 590 | -{ The register of the word at a, a variable in registers: the variable's | |
| 591 | - own, or for a slice its pointer's (off 0) or its length's (off 8) } | |
| 592 | -function RegAt(const a: TAdr): longint; | |
| 593 | -begin | |
| 594 | - if a.off = 0 then RegAt := a.chunk else RegAt := a.hi; | |
| 595 | - if RegAt >= XmmVar then RegAt := RegAt - XmmVar; { an xmm register, XVarRegs } | |
| 596 | -end; | |
| 597 | - | |
| 598 | -{ The memory a names. An address on the stack is popped, and is in its | |
| 599 | - register still; one in a slot is loaded into r. } | |
| 600 | -function Place(const a: TAdr; r: longint): TMem; | |
| 601 | -var ra: longint; o: int64; | |
| 602 | -begin | |
| 603 | - if a.base = bReg then Fatal('internal error: a register has no address'); | |
| 604 | - case a.base of | |
| 605 | - bFrame: Place := Mem(RBP, a.off); | |
| 606 | - bGlobal: Place := MemRip(a.chunk, a.off); | |
| 607 | - bStack: | |
| 608 | - if stack[depth - 1].loc = lAdr then begin | |
| 609 | - Place := AdrMem(depth - 1, a.off); | |
| 610 | - Drop; | |
| 611 | - end else if stack[depth - 1].loc = lIdx then begin | |
| 612 | - Place := stack[depth - 1].m; | |
| 613 | - Place.disp := Place.disp + a.off; | |
| 614 | - Drop; | |
| 615 | - end else begin | |
| 616 | - { an offset noted on the address is the operand's, as | |
| 617 | - p^.f + 8: [r8 + 8] } | |
| 618 | - if Fits32(a.off + stack[depth - 1].off) then o := TakeOff(depth - 1) else o := 0; | |
| 619 | - ra := Peek(depth - 1); | |
| 620 | - Drop; | |
| 621 | - Place := Mem(ra, a.off + o) | |
| 622 | - end; | |
| 623 | - else { bSlot: the address is in the frame slot a.chunk } | |
| 624 | - Load(r, Mem(RBP, a.chunk), 8, false); | |
| 625 | - Place := Mem(r, a.off); | |
| 626 | - end; | |
| 627 | -end; | |
| 628 | - | |
| 629 | -{ The address a names, on top of the stack } | |
| 630 | -procedure PushAdr(const a: TAdr); | |
| 631 | -var m: TMem; r: longint; | |
| 632 | -begin | |
| 633 | - if (a.base = bStack) and (a.off = 0) then exit; { it is on top already } | |
| 634 | - if a.base in [bFrame, bGlobal] then begin | |
| 635 | - NewEntry(kWord, lAdr, a.off); | |
| 636 | - if a.base = bGlobal then stack[depth - 1].chunk := a.chunk; | |
| 637 | - exit; | |
| 638 | - end; | |
| 639 | - { The order matters. With the address on the stack, Place pops it and | |
| 640 | - must come first, so that Target may reuse its register; otherwise | |
| 641 | - Target comes first, since Place may need r to load a slot's pointer. } | |
| 642 | - if a.base = bStack then begin m := Place(a, -1); r := Target end | |
| 643 | - else begin r := Target; m := Place(a, r) end; | |
| 644 | - Lea(r, m); | |
| 645 | - Computed(kWord); | |
| 646 | -end; | |
| 647 | - | |
| 648 | -procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind); | |
| 649 | -var m: TMem; r: longint; | |
| 650 | -begin | |
| 651 | - r := inPlace; | |
| 652 | - inPlace := -1; | |
| 653 | - if r >= XmmVar then r := r - XmmVar; | |
| 654 | - if (a.base = bReg) and (RegAt(a) = r) then begin | |
| 655 | - NewEntry(k, lInto, r); | |
| 656 | - exit; | |
| 657 | - end; | |
| 658 | - if a.base = bReg then begin | |
| 659 | - NewEntry(k, lHeld, RegAt(a)); | |
| 660 | - exit; | |
| 661 | - end; | |
| 662 | - { a variable in the frame or a chunk, or at an address only noted, is | |
| 663 | - only noted too: read where it is used, as an operand if it can be } | |
| 664 | - if (a.base in [bFrame, bGlobal]) or (a.base = bStack) and (stack[depth - 1].loc in [lAdr, lIdx]) then begin | |
| 665 | - m := Place(a, -1); | |
| 666 | - NewMem(lMem, m, k, size, signed); | |
| 667 | - exit; | |
| 668 | - end; | |
| 669 | - { in the order PushAdr explains } | |
| 670 | - if a.base = bStack then begin m := Place(a, -1); r := Target end | |
| 671 | - else begin r := Target; m := Place(a, r) end; | |
| 672 | - if k in [kF32, kF64] then MovsXM(XTarget, m, size = 4) | |
| 673 | - else Load(r, m, size, signed); | |
| 674 | - Computed(k); | |
| 675 | -end; | |
| 676 | - | |
| 677 | -procedure PushProc(c: longint; ext: boolean); | |
| 678 | -var r: longint; | |
| 679 | -begin | |
| 680 | - r := Target; | |
| 681 | - if ext then Load(r, MemRip(c, 0), 8, false) | |
| 682 | - else Lea(r, MemRip(c, 0)); | |
| 683 | - Computed(kWord); | |
| 684 | -end; | |
| 685 | - | |
| 686 | -{ The slot of the variable in xmm register x } | |
| 687 | -function HomeOf(x: longint): TMem; | |
| 688 | -begin | |
| 689 | - HomeOf := Mem(RBP, homes[x + XmmVar - XVarRegs[0]]); | |
| 690 | -end; | |
| 691 | - | |
| 692 | -{ Pops a value into the variable at a; an address on the stack is under | |
| 693 | - the value. } | |
| 694 | -procedure PopVar(const a: TAdr; size: longint); | |
| 695 | -var r: longint; v: int64; real: boolean; | |
| 696 | -begin | |
| 697 | - if (a.base = bReg) and IsReal(depth - 1) then begin | |
| 698 | - XValueTo(depth - 1, RegAt(a)); | |
| 699 | - Drop; | |
| 700 | - MovsMX(HomeOf(RegAt(a)), RegAt(a), false); { written through, as XHome says } | |
| 701 | - exit; | |
| 702 | - end; | |
| 703 | - if a.base = bReg then begin | |
| 704 | - if TopConst(v) then MovRI(RegAt(a), v) | |
| 705 | - else if Peek(depth - 1) <> RegAt(a) then MovRR(RegAt(a), Peek(depth - 1)); { else it is there, lInto } | |
| 706 | - Drop; | |
| 707 | - exit; | |
| 708 | - end; | |
| 709 | - if TopConst(v) and Fits32(v) then begin | |
| 710 | - Drop; | |
| 711 | - StoreI(Place(a, RCX), v, size); | |
| 712 | - exit; | |
| 713 | - end; | |
| 714 | - if TopConst(v) then begin r := RAX; MovRI(RAX, v) end | |
| 715 | - else r := Peek(depth - 1); | |
| 716 | - real := (stack[depth - 1].loc <> lConst) and IsReal(depth - 1); | |
| 717 | - Drop; | |
| 718 | - if real then MovsMX(Place(a, RCX), r, size = 4) | |
| 719 | - else Store(Place(a, RCX), r, size); | |
| 720 | -end; | |
| 721 | - | |
| 722 | -{ Register src to dst, an xmm register's if real } | |
| 723 | -procedure MovReg(real: boolean; dst, src: longint); | |
| 724 | -begin | |
| 725 | - if real then MovX(dst, src) else MovRR(dst, src); | |
| 726 | -end; | |
| 727 | - | |
| 728 | -{ The two values on top, of one kind, change places. } | |
| 729 | -procedure Swap; | |
| 730 | -var e: TEntry; x, y, t: longint; rx, ry, real: boolean; | |
| 731 | -begin | |
| 732 | - x := depth - 2; y := depth - 1; | |
| 733 | - { a value noted at an address that uses its register is worked out, | |
| 734 | - so that its register moves with it } | |
| 735 | - if stack[x].loc in [lIdx, lMem] then Loaded(x); | |
| 736 | - if stack[y].loc in [lIdx, lMem] then Loaded(y); | |
| 737 | - rx := stack[x].loc = lReg; | |
| 738 | - ry := stack[y].loc = lReg; | |
| 739 | - real := IsReal(y); | |
| 740 | - if real then t := 0 else t := RAX; { xmm0, or rax } | |
| 741 | - if rx and ry then begin | |
| 742 | - MovReg(real, t, RegOf(x)); | |
| 743 | - MovReg(real, RegOf(x), RegOf(y)); | |
| 744 | - MovReg(real, RegOf(y), t); | |
| 745 | - end else if rx then begin | |
| 746 | - owner[x mod NumRegs] := -1; | |
| 747 | - MovReg(real, Claim(y, stack[y].k), RegOf(x)); | |
| 748 | - end else if ry then begin | |
| 749 | - owner[y mod NumRegs] := -1; | |
| 750 | - MovReg(real, Claim(x, stack[x].k), RegOf(y)); | |
| 751 | - end; | |
| 752 | - e := stack[x]; stack[x] := stack[y]; stack[y] := e; | |
| 753 | -end; | |
| 754 | - | |
| 755 | -{ Copying } | |
| 756 | - | |
| 757 | -{ size bytes from [rsi] to [rdi], through rcx; rsi and rdi are left | |
| 758 | - as they fall. Up to 64 bytes, as a record or a small array mostly | |
| 759 | - is, they go as moves of 8, 4, 2 and 1 bytes: rep movsb takes some | |
| 760 | - twenty cycles to start, longer than the few moves take. } | |
| 761 | -procedure CopyBytes(size: int64); | |
| 762 | -var o, n: int64; | |
| 763 | -begin | |
| 764 | - if size > 64 then begin | |
| 765 | - MovRI(RCX, size); | |
| 766 | - RepMovsb; | |
| 767 | - exit; | |
| 768 | - end; | |
| 769 | - o := 0; | |
| 770 | - while o < size do begin | |
| 771 | - n := 8; | |
| 772 | - while n > size - o do n := n div 2; | |
| 773 | - Load(RCX, Mem(RSI, o), n, false); | |
| 774 | - Store(Mem(RDI, o), RCX, n); | |
| 775 | - Inc(o, n); | |
| 776 | - end; | |
| 777 | -end; | |
| 778 | - | |
| 779 | -{ Pops a source's address, and a destination's under it, and copies | |
| 780 | - size bytes to off bytes past the destination. } | |
| 781 | -procedure CopyBlock(size, off: int64); | |
| 782 | -begin | |
| 783 | - ClobberArgs; | |
| 784 | - PopTo(RSI); | |
| 785 | - PopTo(RDI); | |
| 786 | - if off <> 0 then Lea(RDI, Mem(RDI, off)); | |
| 787 | - CopyBytes(size); | |
| 788 | -end; | |
| 789 | - | |
| 790 | -{ Pops a source's address, and copies size bytes to a: for a slice in | |
| 791 | - registers, its two words into them. } | |
| 792 | -procedure CopyTo(const a: TAdr; size: int64); | |
| 793 | -begin | |
| 794 | - ClobberArgs; | |
| 795 | - PopTo(RSI); | |
| 796 | - if a.base = bReg then begin | |
| 797 | - Load(a.chunk, Mem(RSI, 0), 8, false); | |
| 798 | - Load(a.hi, Mem(RSI, 8), 8, false); | |
| 799 | - exit; | |
| 800 | - end; | |
| 801 | - Lea(RDI, Place(a, RDI)); | |
| 802 | - CopyBytes(size); | |
| 803 | -end; | |
| 804 | - | |
| 805 | -procedure ZeroVar(const a: TAdr; size: int64); | |
| 806 | -begin | |
| 807 | - ClobberArgs; | |
| 808 | - Lea(RDI, Place(a, RDI)); | |
| 809 | - MovRI(RCX, size); | |
| 810 | - MovRI(RAX, 0); | |
| 811 | - RepStosb; | |
| 812 | -end; | |
| 813 | - | |
| 814 | -{ Operations } | |
| 815 | - | |
| 816 | -{ n if v is 2^n, which is 0 for 1; -1 if v isn't a power of two } | |
| 817 | -function ExactLog2(v: int64): longint; | |
| 818 | -var n: longint; | |
| 819 | -begin | |
| 820 | - n := 0; | |
| 821 | - while (v > 1) and (v and 1 = 0) do begin v := v shr 1; inc(n) end; | |
| 822 | - if v <> 1 then n := -1; | |
| 823 | - ExactLog2 := n; | |
| 824 | -end; | |
| 825 | - | |
| 826 | -{ r times v, a size: by a shift where v is a power of two, as it | |
| 827 | - mostly is, since a shift takes one cycle and imul three } | |
| 828 | -procedure MulI(r: longint; v: int64); | |
| 829 | -begin | |
| 830 | - if ExactLog2(v) > 0 then ShiftI(shShl, r, ExactLog2(v)) | |
| 831 | - else if v <> 1 then ImulI(r, v); | |
| 832 | -end; | |
| 833 | - | |
| 834 | -{ r compared with v, through rdx if v needs more than 32 bits; with | |
| 835 | - zero by test, which sets the flags as cmp would and is shorter } | |
| 836 | -procedure CmpI(r: longint; v: int64); | |
| 837 | -begin | |
| 838 | - if v = 0 then TestRR(r, r) | |
| 839 | - else if Fits32(v) then AluI(aluCmp, r, v) | |
| 840 | - else begin MovRI(RDX, v); Alu(aluCmp, r, RDX) end; | |
| 841 | -end; | |
| 842 | - | |
| 843 | -{ r div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards | |
| 844 | - zero, so a negative dividend first gets 2^k - 1 added (the bias). } | |
| 845 | -procedure DivPow2(op: TSym; r, k: longint; unsign: boolean); | |
| 846 | -begin | |
| 847 | - { unsigned: div shifts right; mod keeps the low k bits, by shifting | |
| 848 | - them up to the top and back down } | |
| 849 | - if unsign then begin | |
| 850 | - if op = sDiv then ShiftI(shShr, r, k) | |
| 851 | - else begin ShiftI(shShl, r, 64 - k); ShiftI(shShr, r, 64 - k) end; | |
| 852 | - exit; | |
| 853 | - end; | |
| 854 | - { signed: sar 63 makes the sign into all ones or all zeros, and shr | |
| 855 | - 64 - k keeps k of them, the bias 2^k - 1 or 0; with it added, sar k | |
| 856 | - rounds towards zero. The remainder is r less the quotient times 2^k. } | |
| 857 | - MovRR(RCX, r); | |
| 858 | - ShiftI(shSar, RCX, 63); | |
| 859 | - ShiftI(shShr, RCX, 64 - k); | |
| 860 | - Alu(aluAdd, RCX, r); | |
| 861 | - ShiftI(shSar, RCX, k); | |
| 862 | - if op = sDiv then MovRR(r, RCX) | |
| 863 | - else begin ShiftI(shShl, RCX, k); Alu(aluSub, r, RCX) end; | |
| 864 | -end; | |
| 865 | - | |
| 866 | -{ Before rax div y, of a signed type of bits < 64: its smallest value | |
| 867 | - divided by -1 overflows it, which must stop the program as it does for | |
| 868 | - an I64 (report section 6.1). idiv on the 64-bit values wouldn't fault, | |
| 869 | - so then rax is made the smallest I64, and it does. } | |
| 870 | -procedure DivTrap(bits, y: longint); | |
| 871 | -var L: longint; | |
| 872 | -begin | |
| 873 | - L := NoLabel; | |
| 874 | - CmpI(y, -1); | |
| 875 | - Jcc(ccNE, L); | |
| 876 | - CmpI(RAX, int64(-1) shl (bits - 1)); | |
| 877 | - Jcc(ccNE, L); | |
| 878 | - ShiftI(shShl, RAX, 64 - bits); | |
| 879 | - FixLink(L); | |
| 880 | -end; | |
| 881 | - | |
| 882 | -{ Whether the value on top is a word in memory that can be the right | |
| 883 | - operand of an instruction as it is: add r, [m] } | |
| 884 | -function TopInMem: boolean; | |
| 885 | -begin | |
| 886 | - TopInMem := (stack[depth - 1].loc = lMem) and (stack[depth - 1].size = 8); | |
| 887 | -end; | |
| 888 | - | |
| 889 | -{ The right operand of the two values on top, popped, in register y: | |
| 890 | - its own, or rcx for a constant; the left one stays, in its register, | |
| 891 | - which is returned. } | |
| 892 | -function Operands(var y: longint): longint; | |
| 893 | -var v: int64; | |
| 894 | -begin | |
| 895 | - if TopConst(v) then begin MovRI(RCX, v); y := RCX end else y := Peek(depth - 1); | |
| 896 | - Drop; | |
| 897 | - Operands := Top; | |
| 898 | -end; | |
| 899 | - | |
| 900 | -{ The two reals on top, the left op the right, worked out in the left's | |
| 901 | - xmm register. The right is taken from memory where it is, as a | |
| 902 | - constant, a variable or a spilled value is: mulsd xmm8, [rbp - 16]. } | |
| 903 | -procedure RealOp(op: TSym; k: TKind); | |
| 904 | -var f: boolean; c, x, y: longint; m: TMem; | |
| 905 | -begin | |
| 906 | - f := k = kF32; | |
| 907 | - case op of | |
| 908 | - sPlus: c := $58; { addsd, or addss for an F32 } | |
| 909 | - sMinus: c := $5C; { subsd, subss } | |
| 910 | - sTimes: c := $59; { mulsd, mulss } | |
| 911 | - else c := $5E; { divsd, divss } | |
| 912 | - end; | |
| 913 | - if stack[depth - 1].loc in [lConst, lMem, lFrame] then begin | |
| 914 | - case stack[depth - 1].loc of | |
| 915 | - lConst: m := RealConst(stack[depth - 1].val); | |
| 916 | - lMem: m := stack[depth - 1].m; | |
| 917 | - else m := Mem(RBP, stack[depth - 1].val); | |
| 918 | - end; | |
| 919 | - Drop; | |
| 920 | - SseM(c, Top, m, f); | |
| 921 | - end else begin | |
| 922 | - y := Peek(depth - 1); | |
| 923 | - Drop; | |
| 924 | - x := Top; | |
| 925 | - Sse(c, x, y, f); | |
| 926 | - end; | |
| 927 | -end; | |
| 928 | - | |
| 929 | -{ The group 1 operation of +, -, &, | and xor } | |
| 930 | -function AluOf(op: TSym): longint; | |
| 931 | -begin | |
| 932 | - case op of | |
| 933 | - sPlus: AluOf := aluAdd; | |
| 934 | - sMinus: AluOf := aluSub; | |
| 935 | - sAmp: AluOf := aluAnd; | |
| 936 | - sBar: AluOf := aluOr; | |
| 937 | - else AluOf := aluXor; | |
| 938 | - end; | |
| 939 | -end; | |
| 940 | - | |
| 941 | -{ The two values on top, the left op the right, an integer of size | |
| 942 | - bytes, signed or not, brought back to its width. } | |
| 943 | -procedure Op(op: TSym; k: TKind; size: longint; signed: boolean); | |
| 944 | -var v, o: int64; r, y, sh, bits: longint; c: boolean; m: TMem; | |
| 945 | -begin | |
| 946 | - if k in [kF32, kF64] then begin RealOp(op, k); exit end; | |
| 947 | - bits := size * 8; | |
| 948 | - { a constant on the left of an operation whose order doesn't matter | |
| 949 | - goes right, where it can be an immediate or a shift } | |
| 950 | - if (op in [sPlus, sTimes, sAmp, sBar, sXor]) and (stack[depth - 2].loc = lConst) | |
| 951 | - and (stack[depth - 1].loc <> lConst) then Swap; | |
| 952 | - c := TopConst(v); | |
| 953 | - { A word's offset (see TEntry) is carried through + and -, and | |
| 954 | - through * by a constant, as (x + a) * k is x * k + a * k: the | |
| 955 | - operation is done on the registers, the offsets worked out here, | |
| 956 | - and put on the result. x + 1 is only noted. A value changed in | |
| 957 | - place, lInto, has no offset. } | |
| 958 | - o := 0; | |
| 959 | - if (bits = 64) and (stack[depth - 2].loc <> lInto) | |
| 960 | - and ((op in [sPlus, sMinus]) or (op = sTimes) and c and Fits32(v)) then begin | |
| 961 | - o := TakeOff(depth - 2); | |
| 962 | - if op = sTimes then o := o * v | |
| 963 | - else if c and Fits32(v) then begin | |
| 964 | - if op = sPlus then o := o + v else o := o - v; | |
| 965 | - Drop; | |
| 966 | - if not (stack[depth - 1].loc in [lReg, lHeld]) then Top; | |
| 967 | - PutOff(o); | |
| 968 | - exit; | |
| 969 | - end | |
| 970 | - else if op = sPlus then o := o + TakeOff(depth - 1) | |
| 971 | - else o := o - TakeOff(depth - 1); | |
| 972 | - end; | |
| 973 | - sh := -1; | |
| 974 | - if c and (op in [sTimes, sDiv, sMod]) then sh := ExactLog2(v); | |
| 975 | - if c and (sh >= 1) then begin | |
| 976 | - Drop; | |
| 977 | - r := Top; | |
| 978 | - if op = sTimes then ShiftI(shShl, r, sh) else DivPow2(op, r, sh, not signed); | |
| 979 | - end else if c and Fits32(v) and (op in [sPlus, sMinus, sBar, sXor, sAmp, sTimes, sShl, sShr]) then begin | |
| 980 | - Drop; | |
| 981 | - r := Top; | |
| 982 | - case op of | |
| 983 | - sTimes: ImulI(r, v); | |
| 984 | - sShl: ShiftI(shShl, r, v and (bits - 1)); | |
| 985 | - sShr: if signed then ShiftI(shSar, r, v and (bits - 1)) else ShiftI(shShr, r, v and (bits - 1)); | |
| 986 | - else AluI(AluOf(op), r, v); | |
| 987 | - end; | |
| 988 | - end else if TopInMem and (op in [sPlus, sMinus, sAmp, sBar, sXor]) then begin | |
| 989 | - m := stack[depth - 1].m; | |
| 990 | - Drop; | |
| 991 | - r := Top; | |
| 992 | - AluM(AluOf(op), r, m); | |
| 993 | - end else begin | |
| 994 | - r := Operands(y); | |
| 995 | - case op of | |
| 996 | - sPlus, sMinus, sAmp, sBar, sXor: Alu(AluOf(op), r, y); | |
| 997 | - sTimes: Imul(r, y); | |
| 998 | - sDiv, sMod: begin | |
| 999 | - MovRR(RAX, r); | |
| 1000 | - if not signed then begin MovRI(RDX, 0); Unary(6, y) end { div } | |
| 1001 | - else begin | |
| 1002 | - { only -1 can make the division overflow, so a constant | |
| 1003 | - divisor other than -1 needs no check } | |
| 1004 | - if (bits < 64) and (not c or (v = -1)) then DivTrap(bits, y); | |
| 1005 | - Cqo; Unary(7, y) { idiv, of rdx:rax, which cqo made from rax } | |
| 1006 | - end; | |
| 1007 | - if op = sDiv then MovRR(r, RAX) else MovRR(r, RDX); | |
| 1008 | - end; | |
| 1009 | - sShl, sShr: begin | |
| 1010 | - { the count modulo the width: the machine takes it modulo 64 } | |
| 1011 | - if y <> RCX then MovRR(RCX, y); | |
| 1012 | - if bits < 64 then AluI(aluAnd, RCX, bits - 1); | |
| 1013 | - if op = sShl then Shift(shShl, r) | |
| 1014 | - else if signed then Shift(shSar, r) else Shift(shShr, r); | |
| 1015 | - end; | |
| 1016 | - end; | |
| 1017 | - end; | |
| 1018 | - if bits < 64 then Extend(r, size, signed); | |
| 1019 | - stack[depth - 1].k := k; | |
| 1020 | - if o <> 0 then PutOff(o); | |
| 1021 | -end; | |
| 1022 | - | |
| 1023 | -{ The left op the right of the two values on top, as a BOOL } | |
| 1024 | -procedure Compare(op: TSym; k: TKind; signed: boolean); | |
| 1025 | -const | |
| 1026 | - CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); | |
| 1027 | - CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); | |
| 1028 | -var f: boolean; r, x, y: longint; v: int64; m: TMem; | |
| 1029 | -begin | |
| 1030 | - if k in [kF32, kF64] then begin | |
| 1031 | - f := k = kF32; | |
| 1032 | - y := Peek(depth - 1); | |
| 1033 | - Drop; | |
| 1034 | - x := Peek(depth - 1); | |
| 1035 | - Drop; | |
| 1036 | - r := Target; { the BOOL, in the left's integer register } | |
| 1037 | - { ucomis sets the flags as an unsigned comparison would, but for an | |
| 1038 | - unordered pair (a NaN) it sets zero, parity and carry all. So only | |
| 1039 | - "above" can be tested straight: it is false for NaN, as every | |
| 1040 | - comparison but <> must be. a < b is made b > a. = also asks for | |
| 1041 | - no parity, as NaN = NaN sets the zero flag, and <> asks for | |
| 1042 | - parity as well. } | |
| 1043 | - case op of | |
| 1044 | - sEql: begin Ucomis(x, y, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end; | |
| 1045 | - sNeq: begin Ucomis(x, y, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end; | |
| 1046 | - sLss: begin Ucomis(y, x, f); SetCC(ccA, r) end; | |
| 1047 | - sLeq: begin Ucomis(y, x, f); SetCC(ccAE, r) end; | |
| 1048 | - sGtr: begin Ucomis(x, y, f); SetCC(ccA, r) end; | |
| 1049 | - sGeq: begin Ucomis(x, y, f); SetCC(ccAE, r) end; | |
| 1050 | - end; | |
| 1051 | - Computed(kWord); | |
| 1052 | - end else begin | |
| 1053 | - if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end | |
| 1054 | - else if TopInMem then begin m := stack[depth - 1].m; Drop; r := Top; AluM(aluCmp, r, m) end | |
| 1055 | - else begin r := Operands(y); Alu(aluCmp, r, y) end; | |
| 1056 | - if signed then SetCC(CcSigned[op], r) else SetCC(CcUnsigned[op], r); | |
| 1057 | - end; | |
| 1058 | - stack[depth - 1].k := kWord; | |
| 1059 | -end; | |
| 1060 | - | |
| 1061 | -{ A real is negated by flipping its sign bit, in rax } | |
| 1062 | -procedure Neg(k: TKind; size: longint; signed: boolean); | |
| 1063 | -var r: longint; | |
| 1064 | -begin | |
| 1065 | - r := Top; | |
| 1066 | - case k of | |
| 1067 | - kF32, kF64: begin | |
| 1068 | - MovqRX(RAX, r); | |
| 1069 | - if k = kF32 then Btc(RAX, 31) else Btc(RAX, 63); | |
| 1070 | - MovqXR(r, RAX); | |
| 1071 | - end; | |
| 1072 | - else | |
| 1073 | - Unary(3, r); { neg } | |
| 1074 | - if size < 8 then Extend(r, size, signed); | |
| 1075 | - end; | |
| 1076 | -end; | |
| 1077 | - | |
| 1078 | -procedure Complement(size: longint; signed: boolean); | |
| 1079 | -var r: longint; | |
| 1080 | -begin | |
| 1081 | - r := Top; | |
| 1082 | - Unary(2, r); { not } | |
| 1083 | - if size < 8 then Extend(r, size, signed); | |
| 1084 | -end; | |
| 1085 | - | |
| 1086 | -procedure NotBool; | |
| 1087 | -begin | |
| 1088 | - AluI(aluXor, Top, 1); | |
| 1089 | -end; | |
| 1090 | - | |
| 1091 | -{ The integer on top, brought to size bytes } | |
| 1092 | -procedure Narrow(size: longint; signed: boolean); | |
| 1093 | -var r: longint; | |
| 1094 | -begin | |
| 1095 | - r := Top; | |
| 1096 | - if size < 8 then Extend(r, size, signed); | |
| 1097 | - stack[depth - 1].k := kWord; | |
| 1098 | -end; | |
| 1099 | - | |
| 1100 | -{ The integer on top, as a real of kind k; big if it may be a U64 above | |
| 1101 | - high(I64), which the machine would take as negative } | |
| 1102 | -procedure IntToReal(k: TKind; big: boolean); | |
| 1103 | -var L1, L2, r, x: longint; f: boolean; | |
| 1104 | -begin | |
| 1105 | - f := k = kF32; | |
| 1106 | - r := Top; | |
| 1107 | - x := RegIn(k, depth - 1); { its xmm register } | |
| 1108 | - if big then begin | |
| 1109 | - { above high(I64): halve, keeping the low bit, and double } | |
| 1110 | - L1 := NoLabel; L2 := NoLabel; | |
| 1111 | - TestRR(r, r); | |
| 1112 | - Jcc(ccS, L1); | |
| 1113 | - Cvtsi2s(x, r, f); | |
| 1114 | - Jmp(L2); | |
| 1115 | - FixLink(L1); | |
| 1116 | - MovRR(RCX, r); | |
| 1117 | - ShiftI(shShr, RCX, 1); | |
| 1118 | - AluI(aluAnd, r, 1); | |
| 1119 | - Alu(aluOr, RCX, r); | |
| 1120 | - Cvtsi2s(x, RCX, f); | |
| 1121 | - Sse($58, x, x, f); { adds it to itself: doubles } | |
| 1122 | - FixLink(L2); | |
| 1123 | - end else Cvtsi2s(x, r, f); | |
| 1124 | - stack[depth - 1].k := k; | |
| 1125 | -end; | |
| 1126 | - | |
| 1127 | -{ The real of kind k on top, truncated to an integer } | |
| 1128 | -procedure RealToInt(k: TKind); | |
| 1129 | -var x: longint; | |
| 1130 | -begin | |
| 1131 | - x := Top; | |
| 1132 | - Cvtts2si(RegIn(kWord, depth - 1), x, k = kF32); | |
| 1133 | - stack[depth - 1].k := kWord; | |
| 1134 | -end; | |
| 1135 | - | |
| 1136 | -{ The real of kind k on top, as the other kind } | |
| 1137 | -procedure RealToReal(k: TKind); | |
| 1138 | -var x: longint; | |
| 1139 | -begin | |
| 1140 | - x := Top; | |
| 1141 | - Sse($5A, x, x, k = kF32); { cvtss2sd, or cvtsd2ss } | |
| 1142 | - if k = kF32 then stack[depth - 1].k := kF64 else stack[depth - 1].k := kF32; | |
| 1143 | -end; | |
| 1144 | - | |
| 1145 | -{ Whether the slices in the frame at a and b (the offsets of their | |
| 1146 | - pointers, the lengths 8 bytes on) are equal (or not, if not eq): the | |
| 1147 | - lengths, then the bytes, len shl shift of them, compared where the | |
| 1148 | - comparison stands. } | |
| 1149 | -procedure CompareSeq(a, b: int64; shift: longint; eq: boolean); | |
| 1150 | -var differ, r: longint; | |
| 1151 | -begin | |
| 1152 | - ClobberArgs; | |
| 1153 | - r := Target; | |
| 1154 | - Load(RCX, Mem(RBP, b + 8), 8, false); | |
| 1155 | - AluM(aluCmp, RCX, Mem(RBP, a + 8)); | |
| 1156 | - differ := NoLabel; | |
| 1157 | - Jcc(ccNE, differ); | |
| 1158 | - Load(RSI, Mem(RBP, b), 8, false); | |
| 1159 | - Load(RDI, Mem(RBP, a), 8, false); | |
| 1160 | - if shift > 0 then ShiftI(shShl, RCX, shift); | |
| 1161 | - { repe cmpsb with rcx = 0 leaves the flags alone, so make them say | |
| 1162 | - equal first, which is right if there are no bytes } | |
| 1163 | - Alu(aluCmp, RCX, RCX); | |
| 1164 | - RepeCmpsb; | |
| 1165 | - FixLink(differ); | |
| 1166 | - if eq then SetCC(ccE, r) else SetCC(ccNE, r); | |
| 1167 | - Computed(kWord); | |
| 1168 | -end; | |
| 1169 | - | |
| 1170 | -{ An error points at its message's length, an 8-byte word, with the | |
| 1171 | - text after it; nil is no error. Pops one, and makes the slice of its | |
| 1172 | - message at tmp, empty for nil. } | |
| 1173 | -procedure ErrorText(tmp: int64); | |
| 1174 | -var L: longint; | |
| 1175 | -begin | |
| 1176 | - PopTo(RAX); | |
| 1177 | - MovRI(RCX, 0); | |
| 1178 | - L := NoLabel; | |
| 1179 | - TestRR(RAX, RAX); | |
| 1180 | - Jcc(ccE, L); | |
| 1181 | - Load(RCX, Mem(RAX, 0), 8, false); | |
| 1182 | - Lea(RAX, Mem(RAX, 8)); | |
| 1183 | - FixLink(L); | |
| 1184 | - Store(Mem(RBP, tmp), RAX, 8); | |
| 1185 | - Store(Mem(RBP, tmp + 8), RCX, 8); | |
| 1186 | -end; | |
| 1187 | - | |
| 1188 | -{ Pops a step and an integer variable's address, and adds the step to | |
| 1189 | - it, or takes it away. } | |
| 1190 | -procedure IncDec(size: longint; signed, dec: boolean); | |
| 1191 | -var r, y, op: longint; v: int64; a: TAdr; | |
| 1192 | -begin | |
| 1193 | - if TopConst(v) and Fits32(v) then begin | |
| 1194 | - Drop; | |
| 1195 | - a.base := bStack; a.off := 0; | |
| 1196 | - if dec then op := aluSub else op := aluAdd; | |
| 1197 | - AluMI(op, Place(a, RCX), v, size); | |
| 1198 | - exit; | |
| 1199 | - end; | |
| 1200 | - r := Operands(y); | |
| 1201 | - Drop; | |
| 1202 | - Load(RAX, Mem(r, 0), size, signed); | |
| 1203 | - if dec then Alu(aluSub, RAX, y) else Alu(aluAdd, RAX, y); | |
| 1204 | - Store(Mem(r, 0), RAX, size); | |
| 1205 | -end; | |
| 1206 | - | |
| 1207 | -{ INC or DEC of the variable in register r, by the value on top; a | |
| 1208 | - narrow one wraps at its width } | |
| 1209 | -procedure IncDecReg(r, size: longint; signed, dec: boolean); | |
| 1210 | -var y, op: longint; v: int64; | |
| 1211 | -begin | |
| 1212 | - if dec then op := aluSub else op := aluAdd; | |
| 1213 | - if TopConst(v) and Fits32(v) then AluI(op, r, v) | |
| 1214 | - else begin y := Top; Alu(op, r, y) end; | |
| 1215 | - Drop; | |
| 1216 | - if size < 8 then Extend(r, size, signed); | |
| 1217 | -end; | |
| 1218 | - | |
| 1219 | -{ The pointer on top, which NEW got, to size zero bytes unless nil } | |
| 1220 | -procedure ZeroNew(size: int64); | |
| 1221 | -var L, r: longint; | |
| 1222 | -begin | |
| 1223 | - ClobberArgs; | |
| 1224 | - r := Top; | |
| 1225 | - L := NoLabel; | |
| 1226 | - TestRR(r, r); | |
| 1227 | - Jcc(ccE, L); | |
| 1228 | - MovRR(RDI, r); | |
| 1229 | - MovRI(RCX, size); | |
| 1230 | - MovRI(RAX, 0); | |
| 1231 | - RepStosb; | |
| 1232 | - FixLink(L); | |
| 1233 | -end; | |
| 1234 | - | |
| 1235 | -{ CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack: the | |
| 1236 | - result where p was } | |
| 1237 | -procedure Atomic(which: longint); | |
| 1238 | -var r, n: longint; | |
| 1239 | -begin | |
| 1240 | - ReadMems(depth - 2 - ord(which = bCas)); { what it writes may be one } | |
| 1241 | - if which = bCas then begin n := Top; Drop end; | |
| 1242 | - PopTo(RAX); { cmpxchg compares with rax, so CAS's old value goes there } | |
| 1243 | - r := Top; | |
| 1244 | - case which of | |
| 1245 | - bCas: begin LockCmpxchg(Mem(r, 0), n); SetCC(ccE, r) end; | |
| 1246 | - bXchg: begin Xchg(Mem(r, 0), RAX); MovRR(r, RAX) end; | |
| 1247 | - bXadd: begin LockXadd(Mem(r, 0), RAX); MovRR(r, RAX) end; | |
| 1248 | - end; | |
| 1249 | - stack[depth - 1].k := kWord; | |
| 1250 | -end; | |
| 1251 | - | |
| 1252 | -{ The system call and its arguments, n values on the stack. Linux takes | |
| 1253 | - the number in rax and the arguments in rdi, rsi, rdx, r10, r8 and r9, | |
| 1254 | - and the syscall instruction clobbers rcx and r11. Every value in a | |
| 1255 | - register is spilled first, as r8 to r11 are the stack's registers, | |
| 1256 | - and the arguments are loaded from their slots. } | |
| 1257 | -procedure SysCall(n: longint); | |
| 1258 | -const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); | |
| 1259 | -var i: longint; | |
| 1260 | -begin | |
| 1261 | - ClobberArgs; | |
| 1262 | - SpillBelow(depth); | |
| 1263 | - for i := 0 to n - 1 do ValueTo(depth - n + i, SysRegs[i]); | |
| 1264 | - for i := 1 to n do Drop; | |
| 1265 | - x64.Syscall; | |
| 1266 | - MovRR(Target, RAX); | |
| 1267 | - Computed(kWord); | |
| 1268 | -end; | |
| 1269 | - | |
| 1270 | -procedure Fence; | |
| 1271 | -begin | |
| 1272 | - Mfence; | |
| 1273 | -end; | |
| 1274 | - | |
| 1275 | -{ Jumps. A label is the chain of the jumps to it, until it is placed. } | |
| 1276 | - | |
| 1277 | -procedure Jump(var L: longint); | |
| 1278 | -begin | |
| 1279 | - Jmp(L); | |
| 1280 | -end; | |
| 1281 | - | |
| 1282 | -{ Pops a BOOL, and jumps if it is when } | |
| 1283 | -procedure JumpIf(var L: longint; when: boolean); | |
| 1284 | -var r: longint; | |
| 1285 | -begin | |
| 1286 | - r := Peek(depth - 1); | |
| 1287 | - Drop; | |
| 1288 | - TestRR(r, r); | |
| 1289 | - if when then Jcc(ccNE, L) else Jcc(ccE, L); | |
| 1290 | -end; | |
| 1291 | - | |
| 1292 | -{ Pops two integers, and jumps if the left op the right } | |
| 1293 | -procedure JumpCmp(op: TSym; signed: boolean; var L: longint); | |
| 1294 | -const | |
| 1295 | - CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); | |
| 1296 | - CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); | |
| 1297 | -var r, y: longint; v: int64; m: TMem; | |
| 1298 | -begin | |
| 1299 | - { a comparison changes neither: a variable's register will do } | |
| 1300 | - if TopConst(v) then begin Drop; r := Peek(depth - 1); CmpI(r, v) end | |
| 1301 | - else if TopInMem then begin m := stack[depth - 1].m; Drop; r := Peek(depth - 1); AluM(aluCmp, r, m) end | |
| 1302 | - else begin y := Peek(depth - 1); Drop; r := Peek(depth - 1); Alu(aluCmp, r, y) end; | |
| 1303 | - Drop; | |
| 1304 | - if signed then Jcc(CcSigned[op], L) else Jcc(CcUnsigned[op], L); | |
| 1305 | -end; | |
| 1306 | - | |
| 1307 | -{ JumpCmp out of an expression, past code that may spill: the values | |
| 1308 | - under the two compared are spilled first, so that they are where the | |
| 1309 | - code after the landing place expects them, whichever way it came. In | |
| 1310 | - v + (a < b and f(x) = 0), a call to f spills v on the way through, so | |
| 1311 | - v must be in its slot too where a < b jumps past the call. } | |
| 1312 | -procedure JumpOut(op: TSym; signed: boolean; var L: longint); | |
| 1313 | -begin | |
| 1314 | - SpillBelow(depth - 2); | |
| 1315 | - JumpCmp(op, signed, L); | |
| 1316 | -end; | |
| 1317 | - | |
| 1318 | -{ The two values on top, compared, as a BOOL: true unless notOp, the | |
| 1319 | - opposite comparison, holds. fj and tj are the chains of jumps, made | |
| 1320 | - earlier by and, or and not, that are taken when the whole condition is | |
| 1321 | - false and when it is true; they land here, on the 0 and the 1. } | |
| 1322 | -procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint); | |
| 1323 | -var r, e: longint; | |
| 1324 | -begin | |
| 1325 | - JumpOut(notOp, signed, fj); | |
| 1326 | - FixLink(tj); | |
| 1327 | - r := Target; | |
| 1328 | - MovRI(r, 1); | |
| 1329 | - e := NoLabel; | |
| 1330 | - Jmp(e); | |
| 1331 | - FixLink(fj); | |
| 1332 | - MovRI(r, 0); | |
| 1333 | - FixLink(e); | |
| 1334 | - Computed(kWord); | |
| 1335 | -end; | |
| 1336 | - | |
| 1337 | -{ JumpBack, Fix, FixTo and JoinJumps are gen's names for the encoder's, | |
| 1338 | - so that a port can place jumps its own way. Fix(L) places label L here; | |
| 1339 | - FixTo(L, top) makes every jump on chain L go to the earlier place top; | |
| 1340 | - JoinJumps makes one chain of two. } | |
| 1341 | -procedure JumpBack(top: longint); | |
| 1342 | -begin | |
| 1343 | - JmpBack(top); | |
| 1344 | -end; | |
| 1345 | - | |
| 1346 | -procedure Fix(L: longint); | |
| 1347 | -begin | |
| 1348 | - FixLink(L); | |
| 1349 | -end; | |
| 1350 | - | |
| 1351 | -{ A jump table, for a match on the integer in frame slot slot, whose | |
| 1352 | - labels run from lo up for n values. The selector less lo, compared | |
| 1353 | - unsigned (so below lo is above n too), is out of the table if it | |
| 1354 | - isn't below n, and goes to the else arm at elseAt, or, if there is | |
| 1355 | - none, to endL, the end of the match. Else it indexes a table of | |
| 1356 | - 4-byte offsets, each from the start of the lea, that follows the | |
| 1357 | - jump, and the code jumps to its start plus the entry: | |
| 1358 | - | |
| 1359 | - mov rax, [slot] | |
| 1360 | - sub rax, lo | |
| 1361 | - cmp rax, n - 1 | |
| 1362 | - ja else | |
| 1363 | - lea rcx, [rip + start] start: this very lea | |
| 1364 | - movsxd rax, [rcx + rax*4 + 17] | |
| 1365 | - add rax, rcx | |
| 1366 | - jmp rax | |
| 1367 | - table: .long arm0 - start, arm1 - start, ... | |
| 1368 | - | |
| 1369 | - The four instructions after the compare are 7, 5, 3 and 2 bytes, so | |
| 1370 | - the table is 17 bytes on, which JumpTable checks. It returns start; | |
| 1371 | - the caller then makes the n entries with TableEntry. } | |
| 1372 | -function JumpTable(slot, lo: int64; n, elseAt: longint; var endL: longint): longint; | |
| 1373 | -const TableAt = 17; | |
| 1374 | -var start: longint; | |
| 1375 | -begin | |
| 1376 | - Load(RAX, Mem(RBP, slot), 8, false); | |
| 1377 | - if lo <> 0 then AluI(aluSub, RAX, lo); | |
| 1378 | - AluI(aluCmp, RAX, n - 1); | |
| 1379 | - if elseAt <> NoLabel then JccBack(ccA, elseAt) else Jcc(ccA, endL); | |
| 1380 | - start := pc; | |
| 1381 | - Lea(RCX, Here); | |
| 1382 | - Load(RAX, MemIdx(RCX, RAX, 4, TableAt), 4, true); | |
| 1383 | - Alu(aluAdd, RAX, RCX); | |
| 1384 | - JmpR(RAX); | |
| 1385 | - if pc <> start + TableAt then Fatal('internal error: the jump table is not where it was said'); | |
| 1386 | - JumpTable := start; | |
| 1387 | -end; | |
| 1388 | - | |
| 1389 | -{ The next entry of the table that JumpTable started at table: the | |
| 1390 | - code at target } | |
| 1391 | -procedure TableEntry(table, target: longint); | |
| 1392 | -begin | |
| 1393 | - Put4(target - table); | |
| 1394 | -end; | |
| 1395 | - | |
| 1396 | -procedure AlignLoop; | |
| 1397 | -begin | |
| 1398 | - Align(32); | |
| 1399 | -end; | |
| 1400 | - | |
| 1401 | -function JoinJumps(L1, L2: longint): longint; | |
| 1402 | -begin | |
| 1403 | - JoinJumps := JoinLinks(L1, L2); | |
| 1404 | -end; | |
| 1405 | - | |
| 1406 | -procedure FixTo(L, top: longint); | |
| 1407 | -begin | |
| 1408 | - FixLinkTo(L, top); | |
| 1409 | -end; | |
| 1410 | - | |
| 1411 | -{ Checks } | |
| 1412 | - | |
| 1413 | -const | |
| 1414 | - NoBase = -2; { a TMem that is no memory } | |
| 1415 | - ARegister = -3; { a TMem that is no memory but a register, its index } | |
| 1416 | - | |
| 1417 | -{ The program goes on if the condition cc holds, and otherwise stops | |
| 1418 | - in Rt.fail(what, a, b, len, at, name), which says what failed at p, | |
| 1419 | - found by at, which a stub works out from where it was called; what is | |
| 1420 | - an index a (0), a slice a..b (1), each for a length len, a position a | |
| 1421 | - of the enumeration called name (2), a slice of a pointer a..b that | |
| 1422 | - runs backwards (3), or a nil pointer (4). Rt.fail knows 5 and 6, a | |
| 1423 | - division fault and a stack overflow, which this machine catches | |
| 1424 | - itself, so no check here has them. The arguments go in rdi, rsi, rdx, | |
| 1425 | - rcx, r8 and r9: a is in register ra, b in RDX; len is in memory at m, | |
| 1426 | - or n if m.base is NoBase, or in register m.index if it is ARegister. | |
| 1427 | - rdi and rsi are passed on as they were, since a parameter may be kept | |
| 1428 | - in them, for the trap report to show: so a goes to the stub in rax. | |
| 1429 | - The failing code is out of the way, at the | |
| 1430 | - procedure's end (EndFails), so that a check that holds costs a jump | |
| 1431 | - not taken; it has p for its place (at is p's offset in the source), | |
| 1432 | - never returns, and may use any register once it has read what it | |
| 1433 | - needs. } | |
| 1434 | -procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint); | |
| 1435 | -begin | |
| 1436 | - if numFails > High(fails) then Mark('more than ' + IntStr(High(fails) + 1) + ' checks in one procedure'); | |
| 1437 | - fails[numFails].L := NoLabel; | |
| 1438 | - Jcc(cc xor 1, fails[numFails].L); { x86's conditions come in pairs, the other one its last bit flipped } | |
| 1439 | - fails[numFails].what := what; | |
| 1440 | - fails[numFails].ra := ra; | |
| 1441 | - fails[numFails].m := m; | |
| 1442 | - fails[numFails].n := n; | |
| 1443 | - fails[numFails].name := name; | |
| 1444 | - fails[numFails].at := p.a; | |
| 1445 | - inc(numFails); | |
| 1446 | -end; | |
| 1447 | - | |
| 1448 | -{ The failures of the procedure's checks: each gives Rt.fail its values | |
| 1449 | - and the place of the offending expression, through a stub for its | |
| 1450 | - kind, which takes the place from where it was called } | |
| 1451 | -procedure EndFails; | |
| 1452 | -var i, line, col: longint; | |
| 1453 | -begin | |
| 1454 | - for i := 0 to numFails - 1 do | |
| 1455 | - with fails[i] do begin | |
| 1456 | - FixLink(L); | |
| 1457 | - LineCol(at, line, col); | |
| 1458 | - NoteLine(line, col); | |
| 1459 | - if ra <> RAX then MovRR(RAX, ra); | |
| 1460 | - if m.base = NoBase then MovRI(RCX, n) | |
| 1461 | - else if m.base = ARegister then MovRR(RCX, m.index) | |
| 1462 | - else Load(RCX, m, 8, false); | |
| 1463 | - if name >= 0 then Lea(R9, MemRip(name, 0)); | |
| 1464 | - if failStubs[what] < 0 then failStubs[what] := NewChunk(secText, Intern('Rt', 2), Intern(@StubNames[what][1], 6)); | |
| 1465 | - CallChunk(failStubs[what]); | |
| 1466 | - end; | |
| 1467 | - numFails := 0; | |
| 1468 | -end; | |
| 1469 | - | |
| 1470 | -{ For Check: a length known here, not in memory } | |
| 1471 | -function NoMem: TMem; | |
| 1472 | -begin | |
| 1473 | - NoMem := Mem(NoBase, 0); | |
| 1474 | -end; | |
| 1475 | - | |
| 1476 | -{ For Check: a length in register r, a slice's in registers } | |
| 1477 | -function InRegister(r: longint): TMem; | |
| 1478 | -begin | |
| 1479 | - InRegister := Mem(ARegister, 0); | |
| 1480 | - InRegister.index := r; | |
| 1481 | -end; | |
| 1482 | - | |
| 1483 | -{ The index on top is below n, unsigned } | |
| 1484 | -procedure CheckIndex(n: int64; const p: TPos); | |
| 1485 | -var r: longint; | |
| 1486 | -begin | |
| 1487 | - r := Peek(depth - 1); | |
| 1488 | - CmpI(r, n); | |
| 1489 | - Check(ccB, 0, p, r, NoMem, n, -1); | |
| 1490 | -end; | |
| 1491 | - | |
| 1492 | -{ The integer on top is a position of an enumeration of n names } | |
| 1493 | -procedure CheckEnum(n: int64; name: longint; const p: TPos); | |
| 1494 | -var r: longint; | |
| 1495 | -begin | |
| 1496 | - r := Top; | |
| 1497 | - CmpI(r, n); | |
| 1498 | - Check(ccB, 2, p, r, NoMem, n, name); | |
| 1499 | -end; | |
| 1500 | - | |
| 1501 | -{ The address on top, from a pointer, which is to be used reach bytes | |
| 1502 | - past: the program stops if it is nil, unless the machine would stop | |
| 1503 | - it, as for a reach within NilPage. } | |
| 1504 | -procedure CheckNil(reach: int64; const p: TPos); | |
| 1505 | -var r: longint; | |
| 1506 | -begin | |
| 1507 | - if reach <= NilPage then exit; | |
| 1508 | - r := Top; | |
| 1509 | - TestRR(r, r); | |
| 1510 | - Check(ccNE, 4, p, r, NoMem, 0, -1); | |
| 1511 | -end; | |
| 1512 | - | |
| 1513 | -{ The displacement disp of an element of size bytes, with the offset of | |
| 1514 | - the index on top taken into it if it fits: s[k - 1] is [r13 + r9 * 8 | |
| 1515 | - - 8] } | |
| 1516 | -function IndexOff(size, disp: int64): int64; | |
| 1517 | -begin | |
| 1518 | - IndexOff := disp; | |
| 1519 | - if Fits32(disp + stack[depth - 1].off * size) then IndexOff := disp + TakeOff(depth - 1) * size; | |
| 1520 | -end; | |
| 1521 | - | |
| 1522 | -{ Pops an index, and the address of the slice at a if it is on the | |
| 1523 | - stack, and pushes the address of its element of size bytes: the | |
| 1524 | - index, checked against the length, times size, plus the pointer. | |
| 1525 | - It is worked out in the index's own register, which then holds the | |
| 1526 | - address; but if the slice's address is on the stack, under the | |
| 1527 | - index, the element's register is the one that holds that, still | |
| 1528 | - needed, so the work is done in rax. A slice in registers is | |
| 1529 | - compared with and added to them; for an element of 1, 2, 4 or 8 | |
| 1530 | - bytes, its address is only noted, as the operand [pointer + index * | |
| 1531 | - size], to be read or written where it is used (lIdx). } | |
| 1532 | -procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); | |
| 1533 | -var m, ml: TMem; r: longint; inRax: boolean; o: int64; | |
| 1534 | -begin | |
| 1535 | - if (a.base = bReg) and (size in [1, 2, 4, 8]) then begin | |
| 1536 | - o := 0; | |
| 1537 | - if not checked then o := IndexOff(size, 0); | |
| 1538 | - r := Peek(depth - 1); | |
| 1539 | - if checked then begin | |
| 1540 | - Alu(aluCmp, r, a.hi); | |
| 1541 | - Check(ccB, 0, p, r, InRegister(a.hi), 0, -1); | |
| 1542 | - end; | |
| 1543 | - Drop; | |
| 1544 | - NewMem(lIdx, MemIdx(a.chunk, r, size, o), kWord, 8, false); | |
| 1545 | - exit; | |
| 1546 | - end; | |
| 1547 | - inRax := a.base = bStack; | |
| 1548 | - if inRax then begin r := RAX; PopTo(RAX) end | |
| 1549 | - else begin r := Top; Drop end; | |
| 1550 | - if a.base = bReg then begin | |
| 1551 | - if checked then begin | |
| 1552 | - Alu(aluCmp, r, a.hi); | |
| 1553 | - Check(ccB, 0, p, r, InRegister(a.hi), 0, -1); | |
| 1554 | - end; | |
| 1555 | - MulI(r, size); | |
| 1556 | - Alu(aluAdd, r, a.chunk); | |
| 1557 | - end else begin | |
| 1558 | - m := Place(a, RCX); | |
| 1559 | - ml := m; ml.disp := ml.disp + 8; | |
| 1560 | - if checked then begin | |
| 1561 | - AluM(aluCmp, r, ml); | |
| 1562 | - Check(ccB, 0, p, r, ml, 0, -1); | |
| 1563 | - end; | |
| 1564 | - MulI(r, size); | |
| 1565 | - AluM(aluAdd, r, m); | |
| 1566 | - end; | |
| 1567 | - if inRax then MovRR(Target, RAX) | |
| 1568 | - else Claim(depth, kWord); { the index's register again, now for the address } | |
| 1569 | - Computed(kWord); | |
| 1570 | -end; | |
| 1571 | - | |
| 1572 | -{ Pops an index, checked, and makes the address of the element it | |
| 1573 | - picks of the array at a. An element of 1, 2, | |
| 1574 | - 4 or 8 bytes is only noted, as the operand [base + index * size] | |
| 1575 | - (lIdx): an array in the frame has rbp as its base, and one in a | |
| 1576 | - chunk, if the index is a variable's register, its address made in | |
| 1577 | - the value's own register. Any other is added up. } | |
| 1578 | -procedure IndexArray(const a: TAdr; size: int64); | |
| 1579 | -var r, b: longint; o: int64; | |
| 1580 | -begin | |
| 1581 | - if (size in [1, 2, 4, 8]) and (a.base = bFrame) then begin | |
| 1582 | - o := IndexOff(size, a.off); | |
| 1583 | - r := Peek(depth - 1); | |
| 1584 | - Drop; | |
| 1585 | - NewMem(lIdx, MemIdx(RBP, r, size, o), kWord, 8, false); | |
| 1586 | - end else if (size in [1, 2, 4, 8]) and (a.base = bGlobal) and (stack[depth - 1].loc = lHeld) then begin | |
| 1587 | - o := IndexOff(size, 0); | |
| 1588 | - r := Peek(depth - 1); | |
| 1589 | - Drop; | |
| 1590 | - b := Target; | |
| 1591 | - Lea(b, MemRip(a.chunk, a.off)); | |
| 1592 | - NewMem(lIdx, MemIdx(b, r, size, o), kWord, 8, false); | |
| 1593 | - end else begin | |
| 1594 | - MulI(Top, size); | |
| 1595 | - PushAdr(a); | |
| 1596 | - Op(sPlus, kWord, 8, false); | |
| 1597 | - end; | |
| 1598 | -end; | |
| 1599 | - | |
| 1600 | -{ Pops lo and hi, and the address of the array or slice at a if it is | |
| 1601 | - on the stack, and makes the slice lo..hi of it, with elements of size | |
| 1602 | - bytes, in the frame at tmp; the array has n elements. } | |
| 1603 | -procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); | |
| 1604 | -var m, ml: TMem; | |
| 1605 | -begin | |
| 1606 | - PopTo(RDX); | |
| 1607 | - PopTo(RCX); | |
| 1608 | - if a.base = bSlot then ClobberArgs; { Place loads its slot into rdi } | |
| 1609 | - if a.base = bReg then ml := InRegister(a.hi) { a slice in registers } | |
| 1610 | - else begin | |
| 1611 | - m := Place(a, RDI); | |
| 1612 | - ml := m; ml.disp := ml.disp + 8; | |
| 1613 | - end; | |
| 1614 | - if checked then begin | |
| 1615 | - { two checks, both unsigned, so that a negative bound fails too: | |
| 1616 | - hi against the length, then lo against hi } | |
| 1617 | - if isArray then begin CmpI(RDX, n); ml := NoMem end | |
| 1618 | - else if a.base = bReg then Alu(aluCmp, RDX, a.hi) | |
| 1619 | - else AluM(aluCmp, RDX, ml); | |
| 1620 | - Check(ccBE, 1, p, RCX, ml, n, -1); | |
| 1621 | - Alu(aluCmp, RCX, RDX); | |
| 1622 | - Check(ccBE, 1, p, RCX, ml, n, -1); | |
| 1623 | - end; | |
| 1624 | - MovRR(RAX, RDX); | |
| 1625 | - Alu(aluSub, RAX, RCX); | |
| 1626 | - Store(Mem(RBP, tmp + 8), RAX, 8); | |
| 1627 | - MulI(RCX, size); | |
| 1628 | - if isArray then Lea(RAX, m) | |
| 1629 | - else if a.base = bReg then MovRR(RAX, a.chunk) | |
| 1630 | - else Load(RAX, m, 8, false); | |
| 1631 | - Alu(aluAdd, RAX, RCX); | |
| 1632 | - Store(Mem(RBP, tmp), RAX, 8); | |
| 1633 | -end; | |
| 1634 | - | |
| 1635 | -{ COPY: the elements of the slice in the frame at sOff, of size bytes | |
| 1636 | - each, to the start of the one at dOff, which must be at least as long | |
| 1637 | - (the source may be shorter), as one block; backwards when the | |
| 1638 | - destination is above the source, so that an overlap comes out right. } | |
| 1639 | -procedure CopySlice(dOff, sOff, size: int64; checked: boolean; const p: TPos); | |
| 1640 | -var ml: TMem; L, E: longint; | |
| 1641 | -begin | |
| 1642 | - ClobberArgs; | |
| 1643 | - Load(RDX, Mem(RBP, sOff + 8), 8, false); | |
| 1644 | - if checked then begin | |
| 1645 | - ml := Mem(RBP, dOff + 8); | |
| 1646 | - MovRI(RAX, 0); | |
| 1647 | - AluM(aluCmp, RDX, ml); | |
| 1648 | - Check(ccBE, 1, p, RAX, ml, 0, -1); | |
| 1649 | - end; | |
| 1650 | - MovRR(RCX, RDX); | |
| 1651 | - MulI(RCX, size); | |
| 1652 | - Load(RSI, Mem(RBP, sOff), 8, false); | |
| 1653 | - Load(RDI, Mem(RBP, dOff), 8, false); | |
| 1654 | - Alu(aluCmp, RDI, RSI); | |
| 1655 | - L := NoLabel; | |
| 1656 | - Jcc(ccBE, L); { the destination is not above: forwards, at L } | |
| 1657 | - { backwards: std makes rep movsb run downward, so it starts at the | |
| 1658 | - last byte of each } | |
| 1659 | - Lea(RSI, MemIdx(RSI, RCX, 1, -1)); | |
| 1660 | - Lea(RDI, MemIdx(RDI, RCX, 1, -1)); | |
| 1661 | - Std; | |
| 1662 | - RepMovsb; | |
| 1663 | - Cld; | |
| 1664 | - E := NoLabel; | |
| 1665 | - Jmp(E); | |
| 1666 | - FixLink(L); | |
| 1667 | - RepMovsb; | |
| 1668 | - FixLink(E); | |
| 1669 | -end; | |
| 1670 | - | |
| 1671 | -{ Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its | |
| 1672 | - bounds can't be known, but it can't run backwards. } | |
| 1673 | -procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); | |
| 1674 | -begin | |
| 1675 | - ClobberArgs; | |
| 1676 | - PopTo(RDX); | |
| 1677 | - PopTo(RCX); | |
| 1678 | - PopTo(RDI); | |
| 1679 | - if checked then begin | |
| 1680 | - Alu(aluCmp, RCX, RDX); | |
| 1681 | - Check(ccLE, 3, p, RCX, NoMem, 0, -1); | |
| 1682 | - end; | |
| 1683 | - MovRR(RAX, RDX); | |
| 1684 | - Alu(aluSub, RAX, RCX); | |
| 1685 | - Store(Mem(RBP, tmp + 8), RAX, 8); | |
| 1686 | - MulI(RCX, size); | |
| 1687 | - Alu(aluAdd, RCX, RDI); | |
| 1688 | - Store(Mem(RBP, tmp), RCX, 8); | |
| 1689 | -end; | |
| 1690 | - | |
| 1691 | -{ Calls } | |
| 1692 | - | |
| 1693 | -{ The call c, its c.n arguments the values on top of the stack, in | |
| 1694 | - registers, noted as constants or spilled. They go to registers or the | |
| 1695 | - machine's stack, the procedure is called, and its result, if any, | |
| 1696 | - comes back: one value is pushed, two results are pushed as two values, | |
| 1697 | - a slice or a record is stored in the temporary c.ret. | |
| 1698 | - | |
| 1699 | - ni and nf count the integer and the real registers used so far, k the | |
| 1700 | - arguments on the machine's stack, and pad is 1 when a padding quadword | |
| 1701 | - is needed. } | |
| 1702 | - | |
| 1703 | -procedure Call(var c: TCall); | |
| 1704 | -var | |
| 1705 | - res: PType; i, ni, nf, k, pad, r, first: longint; | |
| 1706 | - onStack: array[0..63] of boolean; late: array[4..5] of longint; | |
| 1707 | -begin | |
| 1708 | - { the arguments are the c.n values on top, where computing them left | |
| 1709 | - them; the registers in use under them are saved around the call } | |
| 1710 | - ClobberArgs; | |
| 1711 | - first := depth - c.n; | |
| 1712 | - SpillBelow(first); | |
| 1713 | - res := c.sig^.base; | |
| 1714 | - c.ret := 0; | |
| 1715 | - ni := 0; nf := 0; | |
| 1716 | - if (res <> nil) and (BehindPointer(res) or (res^.form = fSlice)) then begin | |
| 1717 | - c.ret := AllocTemp(res^.size, res^.align); | |
| 1718 | - if BehindPointer(res) then ni := 1; | |
| 1719 | - end; | |
| 1720 | - { the System V classes: each argument in the next free register of | |
| 1721 | - its kind, or else on the stack } | |
| 1722 | - k := 0; | |
| 1723 | - for i := 0 to c.n - 1 do begin | |
| 1724 | - if c.isF[i] then begin onStack[i] := nf >= 8; if nf < 8 then inc(nf) end | |
| 1725 | - else begin | |
| 1726 | - { a slice's words both go in registers, or both on the stack } | |
| 1727 | - if (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1] | |
| 1728 | - else if c.pair[i] then onStack[i] := ni > 4 | |
| 1729 | - else onStack[i] := ni >= 6; | |
| 1730 | - if not onStack[i] then inc(ni); | |
| 1731 | - end; | |
| 1732 | - if onStack[i] then inc(k); | |
| 1733 | - end; | |
| 1734 | - { System V wants rsp a multiple of 16 at the call. A procedure's body | |
| 1735 | - has it so (push rbp, and a frame size that is a multiple of 16), so | |
| 1736 | - a quadword of padding makes the count pushed since even. The stack | |
| 1737 | - arguments go right to left, so that the first is at the lowest | |
| 1738 | - address. } | |
| 1739 | - pad := ord(odd(pushed + k)); | |
| 1740 | - if pad <> 0 then AluI(aluSub, RSP, 8); | |
| 1741 | - for i := c.n - 1 downto 0 do | |
| 1742 | - if onStack[i] then begin | |
| 1743 | - ValueTo(first + i, RAX); | |
| 1744 | - Push(RAX); | |
| 1745 | - end; | |
| 1746 | - ni := 0; nf := 0; | |
| 1747 | - late[4] := -1; late[5] := -1; | |
| 1748 | - if (c.ret <> 0) and BehindPointer(res) then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; | |
| 1749 | - { rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as | |
| 1750 | - they are stack registers and may hold arguments not yet moved: late | |
| 1751 | - is the value for each. The fifth goes through rax, so that writing r8 | |
| 1752 | - doesn't destroy a value still wanted for r9. } | |
| 1753 | - for i := 0 to c.n - 1 do | |
| 1754 | - if not onStack[i] then | |
| 1755 | - if c.isF[i] then begin | |
| 1756 | - XValueTo(first + i, nf); | |
| 1757 | - inc(nf); | |
| 1758 | - end else begin | |
| 1759 | - if ni < 4 then ValueTo(first + i, IntRegs[ni]) else late[ni] := first + i; | |
| 1760 | - inc(ni); | |
| 1761 | - end; | |
| 1762 | - if late[4] >= 0 then ValueTo(late[4], RAX); | |
| 1763 | - if late[5] >= 0 then ValueTo(late[5], R9); | |
| 1764 | - if late[4] >= 0 then MovRR(R8, RAX); | |
| 1765 | - for i := 1 to c.n do Drop; | |
| 1766 | - { a procedure taking ... is told in al how many xmm registers it got } | |
| 1767 | - if c.sig^.variadic then MovRI(RAX, nf); | |
| 1768 | - if c.ext then CallM(MemRip(c.chunk, 0)) | |
| 1769 | - else if c.chunk >= 0 then CallChunk(c.chunk) | |
| 1770 | - else begin | |
| 1771 | - { r11 is scratch: caller-saved, carrying no argument, and free, | |
| 1772 | - as the value stack was spilled above } | |
| 1773 | - Load(R11, Mem(RBP, c.target), 8, false); | |
| 1774 | - CallR(R11); | |
| 1775 | - end; | |
| 1776 | - if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); | |
| 1777 | - { the variables in xmm registers, which the call may have changed, | |
| 1778 | - from their slots } | |
| 1779 | - for i := 0 to 3 do | |
| 1780 | - if regMask and (1 shl (XBit + i)) <> 0 then MovsXM(XVarRegs[i] - XmmVar, Mem(RBP, homes[i]), false); | |
| 1781 | - { the result: a slice in rax and rdx goes to the caller's temporary, | |
| 1782 | - two results on the stack, the first under the second, and a single | |
| 1783 | - value on top } | |
| 1784 | - if res = nil then | |
| 1785 | - { nothing comes back } | |
| 1786 | - else if res^.form = fSlice then begin | |
| 1787 | - Store(Mem(RBP, c.ret), RAX, 8); | |
| 1788 | - Store(Mem(RBP, c.ret + 8), RDX, 8); | |
| 1789 | - end else if InTwoRegs(res) then begin | |
| 1790 | - MovRR(Target, RAX); Computed(kWord); | |
| 1791 | - MovRR(Target, RDX); Computed(kWord); | |
| 1792 | - end else if (c.ret = 0) and (res^.form = fReal) then begin | |
| 1793 | - MovX(XTarget, 0); | |
| 1794 | - Computed(KindOf(res)); | |
| 1795 | - end else if c.ret = 0 then begin | |
| 1796 | - r := Target; | |
| 1797 | - MovRR(r, RAX); | |
| 1798 | - if res^.form = fBool then Extend(r, 1, false) | |
| 1799 | - else if (res^.form = fInt) and (res^.size < 8) then Extend(r, res^.size, res^.signed); | |
| 1800 | - Computed(KindOf(res)); | |
| 1801 | - end; | |
| 1802 | -end; | |
| 1803 | - | |
| 1804 | -{ Frames } | |
| 1805 | - | |
| 1806 | -{ A slot of size bytes in the frame: its offset from rbp, negative, as | |
| 1807 | - the frame grows down. A temporary is released at the end of its | |
| 1808 | - statement (TempMark, ReleaseTemps); AllocPerm's lasts the procedure. } | |
| 1809 | -function AllocTemp(size, align: int64): int64; | |
| 1810 | -begin | |
| 1811 | - if align < 1 then align := 1; | |
| 1812 | - frameTop := AlignUp(frameTop + size, align); | |
| 1813 | - if frameTop > frameMax then frameMax := frameTop; | |
| 1814 | - AllocTemp := -frameTop; | |
| 1815 | -end; | |
| 1816 | - | |
| 1817 | -function AllocPerm(size, align: int64): int64; | |
| 1818 | -begin | |
| 1819 | - AllocPerm := AllocTemp(size, align); | |
| 1820 | - permTop := frameTop; | |
| 1821 | -end; | |
| 1822 | - | |
| 1823 | -function TempMark: int64; | |
| 1824 | -begin | |
| 1825 | - TempMark := frameTop; | |
| 1826 | -end; | |
| 1827 | - | |
| 1828 | -{ The slots allocated since the mark m are free again, but those | |
| 1829 | - AllocPerm made stay } | |
| 1830 | -procedure ReleaseTemps(m: int64); | |
| 1831 | -begin | |
| 1832 | - if m < permTop then m := permTop; | |
| 1833 | - frameTop := m; | |
| 1834 | -end; | |
| 1835 | - | |
| 1836 | -{ Every slot used so far stays reserved for the rest of the procedure: | |
| 1837 | - a deferred statement's temporaries live until it runs. } | |
| 1838 | -procedure KeepTemps; | |
| 1839 | -begin | |
| 1840 | - frameTop := frameMax; | |
| 1841 | - permTop := frameMax; | |
| 1842 | -end; | |
| 1843 | - | |
| 1844 | -{ The parameters' places: each in a frame slot of its own, stored there | |
| 1845 | - from its register on entry, or above the frame where the caller | |
| 1846 | - pushed it. A var parameter, and a record or array passed by value, | |
| 1847 | - holds an address (ind). } | |
| 1848 | -procedure BeginProc(sig: PType; params: PObj); | |
| 1849 | -var p: PObj; ni, nf, k: longint; stackOff: int64; f: boolean; | |
| 1850 | -begin | |
| 1851 | - frameTop := 0; frameMax := 0; permTop := 0; | |
| 1852 | - ResetStack(0, 0); | |
| 1853 | - exitL := NoLabel; | |
| 1854 | - numIn := 0; | |
| 1855 | - lost := false; | |
| 1856 | - kept := 0; | |
| 1857 | - resType := sig^.base; | |
| 1858 | - procParams := params; | |
| 1859 | - regMask := 0; | |
| 1860 | - ni := 0; nf := 0; stackOff := 16; { stackOff: above the saved rbp and the return address } | |
| 1861 | - if (resType <> nil) and BehindPointer(resType) then begin | |
| 1862 | - retSlot := AllocPerm(8, 8); | |
| 1863 | - inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; | |
| 1864 | - numIn := 1; ni := 1; | |
| 1865 | - end; | |
| 1866 | - p := params; | |
| 1867 | - while p <> nil do begin | |
| 1868 | - p^.ind := p^.isVar or (IsAggregate(p^.typ) and (p^.typ^.form <> fSlice)); | |
| 1869 | - f := (p^.typ^.form = fReal) and not p^.isVar; | |
| 1870 | - if (p^.typ^.form = fSlice) and not p^.isVar then begin | |
| 1871 | - { its two words, in two registers or both on the stack } | |
| 1872 | - if ni <= 4 then begin | |
| 1873 | - p^.val := AllocPerm(16, 8); | |
| 1874 | - for k := 0 to 1 do begin | |
| 1875 | - inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + 8 * k; | |
| 1876 | - inSize[numIn] := 8; inObj[numIn] := p; | |
| 1877 | - inc(numIn); inc(ni); | |
| 1878 | - end; | |
| 1879 | - end else begin | |
| 1880 | - p^.val := stackOff; | |
| 1881 | - inc(stackOff, 16); | |
| 1882 | - end; | |
| 1883 | - end else if (f and (nf < 8)) or (not f and (ni < 6)) then begin | |
| 1884 | - p^.val := AllocPerm(8, 8); | |
| 1885 | - { a real's register, xmm nf, is told from an integer register by | |
| 1886 | - a negative number (EnterBody decodes it) } | |
| 1887 | - if f then begin inReg[numIn] := -1 - nf; inc(nf) end | |
| 1888 | - else begin inReg[numIn] := IntRegs[ni]; inc(ni) end; | |
| 1889 | - inSlot[numIn] := p^.val; | |
| 1890 | - inObj[numIn] := p; | |
| 1891 | - if p^.ind then inSize[numIn] := 8 | |
| 1892 | - else if p^.typ^.form in [fInt, fBool, fEnum, fSet, fReal] then inSize[numIn] := p^.typ^.size | |
| 1893 | - else inSize[numIn] := 8; | |
| 1894 | - inc(numIn); | |
| 1895 | - end else begin | |
| 1896 | - p^.val := stackOff; | |
| 1897 | - inc(stackOff, 8); | |
| 1898 | - end; | |
| 1899 | - p := p^.next; | |
| 1900 | - end; | |
| 1901 | - paramTop := frameTop; | |
| 1902 | -end; | |
| 1903 | - | |
| 1904 | -{ The frame as BeginProc left it, with the parameters' slots only: the | |
| 1905 | - locals are given theirs again once their registers are chosen. } | |
| 1906 | -procedure ResetLocals; | |
| 1907 | -begin | |
| 1908 | - frameTop := paramTop; frameMax := paramTop; permTop := paramTop; | |
| 1909 | -end; | |
| 1910 | - | |
| 1911 | -{ The register the word (0, or 1 for a slice's second) of parameter o | |
| 1912 | - arrives in, or -1 if it comes on the stack or in an xmm register } | |
| 1913 | -function ArrivesIn(o: PObj; word: longint): longint; | |
| 1914 | -var i: longint; | |
| 1915 | -begin | |
| 1916 | - ArrivesIn := -1; | |
| 1917 | - for i := 0 to numIn - 1 do | |
| 1918 | - if (inObj[i] = o) and (inSlot[i] = o^.val + 8 * word) and (inReg[i] >= 0) then ArrivesIn := inReg[i]; | |
| 1919 | -end; | |
| 1920 | - | |
| 1921 | -{ Parameter o, of a procedure that calls nothing, stays in the register | |
| 1922 | - it arrives in if that is rdi or rsi, which only a call or a copy needs: | |
| 1923 | - no register is saved for it, and nothing is moved. A slice stays only | |
| 1924 | - if it has both. If the procedure turns out to copy after all, lost is | |
| 1925 | - set, and it is compiled again with o in memory. } | |
| 1926 | -procedure KeepArrived(o: PObj); | |
| 1927 | -var r: longint; | |
| 1928 | -begin | |
| 1929 | - r := ArrivesIn(o, 0); | |
| 1930 | - if o^.typ^.form = fSlice then begin | |
| 1931 | - if (r = RDI) and (ArrivesIn(o, 1) = RSI) then begin o^.reg := RDI; o^.reg2 := RSI end; | |
| 1932 | - end else if (r = RDI) or (r = RSI) then o^.reg := r; | |
| 1933 | -end; | |
| 1934 | - | |
| 1935 | -{ Whether parameter o is kept where it arrived } | |
| 1936 | -function KeptIn(o: PObj): boolean; | |
| 1937 | -begin | |
| 1938 | - KeptIn := (o^.reg = RDI) or (o^.reg = RSI); | |
| 1939 | -end; | |
| 1940 | - | |
| 1941 | -{ The locals in memory have their slots: the prologue zeroes those and | |
| 1942 | - nothing below. The procedure keeps variables in the registers of | |
| 1943 | - mask, which are saved next, a quadword each, in slots that need no | |
| 1944 | - zeroing; the frame as it then stands is where a second try at the | |
| 1945 | - body starts. } | |
| 1946 | -procedure UseRegs(mask: longint); | |
| 1947 | -var i, k: longint; p: PObj; | |
| 1948 | -begin | |
| 1949 | - zeroTop := permTop; | |
| 1950 | - regMask := mask; | |
| 1951 | - { the parameters left in rdi and rsi, as they arrived } | |
| 1952 | - kept := 0; | |
| 1953 | - p := procParams; | |
| 1954 | - while p <> nil do begin | |
| 1955 | - if p^.reg in [RDI, RSI] then kept := kept or (1 shl p^.reg); | |
| 1956 | - if p^.reg2 in [RDI, RSI] then kept := kept or (1 shl p^.reg2); | |
| 1957 | - p := p^.next; | |
| 1958 | - end; | |
| 1959 | - k := 0; | |
| 1960 | - for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k); | |
| 1961 | - if k > 0 then saveAt := AllocPerm(8 * k, 8); | |
| 1962 | - frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop; | |
| 1963 | -end; | |
| 1964 | - | |
| 1965 | -{ The variable in xmm register r of XVarRegs has a slot of 8 bytes at | |
| 1966 | - slot, kept as it changes } | |
| 1967 | -procedure XHome(r: longint; slot: int64); | |
| 1968 | -begin | |
| 1969 | - homes[r - XVarRegs[0]] := slot; | |
| 1970 | -end; | |
| 1971 | - | |
| 1972 | -procedure MarkCode(var m: TCodeMark); | |
| 1973 | -begin | |
| 1974 | - m.pc := pc; m.fixups := numFixups; m.lines := numLines; m.said := SaidCount; | |
| 1975 | - m.fails := numFails; | |
| 1976 | -end; | |
| 1977 | - | |
| 1978 | -{ The code made since m taken back, as if it had never been made; the | |
| 1979 | - frame is left as it is } | |
| 1980 | -procedure Rewind(const m: TCodeMark); | |
| 1981 | -begin | |
| 1982 | - pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said); | |
| 1983 | - numFails := m.fails; | |
| 1984 | -end; | |
| 1985 | - | |
| 1986 | -{ For a second try at the body: the code as it was at m, and the frame | |
| 1987 | - as UseRegs found it } | |
| 1988 | -procedure Redo(const m: TCodeMark); | |
| 1989 | -begin | |
| 1990 | - pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said); | |
| 1991 | - numFails := 0; | |
| 1992 | - lost := false; | |
| 1993 | - frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2]; | |
| 1994 | - exitL := NoLabel; | |
| 1995 | -end; | |
| 1996 | - | |
| 1997 | -{ The prologue, which is for the procedure's first line. Only the | |
| 1998 | - locals in memory are zeroed, from the parameters' slots down to | |
| 1999 | - zeroTop: the parameters are stored over their slots next, the | |
| 2000 | - registers of variables are zeroed in place, their save slots are | |
| 2001 | - written before they are read, and so is every temporary. } | |
| 2002 | -procedure EnterBody(c, line: longint); | |
| 2003 | -var i, top, r: longint; n: int64; p: PObj; | |
| 2004 | -begin | |
| 2005 | - BeginChunk(c); | |
| 2006 | - codeChunk := c; | |
| 2007 | - NoteLine(line, 1); { a declaration starts a line } | |
| 2008 | - Push(RBP); | |
| 2009 | - MovRR(RBP, RSP); | |
| 2010 | - Say('sub rsp, frame'); | |
| 2011 | - { sub rsp, imm32: always the form with a 32-bit field, so that EndProc | |
| 2012 | - can patch the frame's size in } | |
| 2013 | - Put1($48); Put1($81); Put1($EC); | |
| 2014 | - frameFix := pc; Put4(0); | |
| 2015 | - n := (AlignUp(zeroTop, 8) - paramTop) div 8; | |
| 2016 | - if n > 0 then MovRI(RAX, 0); | |
| 2017 | - if n <= 4 then | |
| 2018 | - for i := 1 to n do Store(Mem(RBP, -paramTop - 8 * i), RAX, 8) | |
| 2019 | - else begin | |
| 2020 | - { a quadword at a time: r10 runs from -n up to 0, and the store's | |
| 2021 | - address is scaled by it } | |
| 2022 | - MovRI(R10, -n); | |
| 2023 | - top := pc; | |
| 2024 | - Store(MemIdx(RBP, R10, 8, -paramTop), RAX, 8); | |
| 2025 | - AluI(aluAdd, R10, 1); | |
| 2026 | - JccBack(ccNE, top); | |
| 2027 | - end; | |
| 2028 | - { each register that holds a variable is saved, for the caller, and | |
| 2029 | - zeroed, as a variable starts at zero } | |
| 2030 | - chunks^[c].saveMask := regMask and (1 shl XBit - 1); | |
| 2031 | - chunks^[c].saveAt := saveAt; | |
| 2032 | - n := 0; | |
| 2033 | - for i := 0 to 4 do | |
| 2034 | - if regMask and (1 shl i) <> 0 then begin | |
| 2035 | - Store(Mem(RBP, saveAt + 8 * n), VarRegs[i], 8); | |
| 2036 | - MovRI(VarRegs[i], 0); | |
| 2037 | - inc(n); | |
| 2038 | - end; | |
| 2039 | - { and each xmm register that holds a variable, zeroed, as its slot | |
| 2040 | - is: the caller keeps its own } | |
| 2041 | - for i := 0 to 3 do | |
| 2042 | - if regMask and (1 shl (XBit + i)) <> 0 then begin | |
| 2043 | - MovRI(RAX, 0); | |
| 2044 | - MovqXR(XVarRegs[i] - XmmVar, RAX); | |
| 2045 | - end; | |
| 2046 | - { the parameters that came in registers, to their slots or, for a | |
| 2047 | - register variable, to its register: a slice's second word, the one | |
| 2048 | - for the slot 8 bytes into its own, to its length's } | |
| 2049 | - for i := 0 to numIn - 1 do | |
| 2050 | - if inReg[i] < 0 then begin | |
| 2051 | - MovsMX(Mem(RBP, inSlot[i]), -1 - inReg[i], false); | |
| 2052 | - if inObj[i]^.reg <> 0 then MovX(inObj[i]^.reg - XmmVar, -1 - inReg[i]); | |
| 2053 | - end | |
| 2054 | - else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin | |
| 2055 | - r := inObj[i]^.reg; | |
| 2056 | - if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2; | |
| 2057 | - if r <> inReg[i] then MovRR(r, inReg[i]); { else it is kept where it is } | |
| 2058 | - if inSize[i] < 8 then Extend(r, inSize[i], inObj[i]^.typ^.signed); | |
| 2059 | - end else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]); | |
| 2060 | - { a register variable's parameter that came on the stack, above the | |
| 2061 | - frame (val > 0), is loaded from there } | |
| 2062 | - p := procParams; | |
| 2063 | - while p <> nil do begin | |
| 2064 | - if (p^.reg <> 0) and (p^.val > 0) then | |
| 2065 | - if p^.typ^.form = fReal then MovsXM(p^.reg - XmmVar, Mem(RBP, p^.val), p^.typ^.size = 4) | |
| 2066 | - else if p^.typ^.form in [fInt, fBool, fEnum, fSet] then | |
| 2067 | - Load(p^.reg, Mem(RBP, p^.val), p^.typ^.size, p^.typ^.signed) | |
| 2068 | - else begin | |
| 2069 | - Load(p^.reg, Mem(RBP, p^.val), 8, false); | |
| 2070 | - if p^.typ^.form = fSlice then Load(p^.reg2, Mem(RBP, p^.val + 8), 8, false); | |
| 2071 | - end; | |
| 2072 | - p := p^.next; | |
| 2073 | - end; | |
| 2074 | -end; | |
| 2075 | - | |
| 2076 | -{ Pops one of several results, a value of size bytes, into its place | |
| 2077 | - off bytes into the caller's, behind the hidden pointer. } | |
| 2078 | -procedure ResultTo(off: int64; size: longint); | |
| 2079 | -var r: longint; real: boolean; | |
| 2080 | -begin | |
| 2081 | - r := Top; | |
| 2082 | - real := IsReal(depth - 1); | |
| 2083 | - Drop; | |
| 2084 | - Load(RCX, Mem(RBP, retSlot), 8, false); | |
| 2085 | - if real then MovsMX(Mem(RCX, off), r, size = 4) | |
| 2086 | - else Store(Mem(RCX, off), r, size); | |
| 2087 | -end; | |
| 2088 | - | |
| 2089 | -{ Pops an address, and copies size bytes from it to the result, off | |
| 2090 | - bytes into it. } | |
| 2091 | -procedure CopyResult(off, size: int64); | |
| 2092 | -begin | |
| 2093 | - ClobberArgs; | |
| 2094 | - PopTo(RSI); | |
| 2095 | - Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 2096 | - if off <> 0 then Lea(RDI, Mem(RDI, off)); | |
| 2097 | - CopyBytes(size); | |
| 2098 | -end; | |
| 2099 | - | |
| 2100 | -{ return: the result, a value on top, goes in rax or xmm0; two words, | |
| 2101 | - the two values on top, in rax and rdx; one behind the hidden pointer | |
| 2102 | - goes back with that pointer in rax. } | |
| 2103 | -procedure ReturnJump; | |
| 2104 | -begin | |
| 2105 | - if resType <> nil then | |
| 2106 | - if BehindPointer(resType) then Load(RAX, Mem(RBP, retSlot), 8, false) | |
| 2107 | - else if InTwoRegs(resType) then begin | |
| 2108 | - PopTo(RDX); | |
| 2109 | - PopTo(RAX); | |
| 2110 | - end else begin | |
| 2111 | - if resType^.form = fReal then begin XValueTo(depth - 1, 0); Drop end | |
| 2112 | - else PopTo(RAX); | |
| 2113 | - end; | |
| 2114 | - Jmp(exitL); | |
| 2115 | -end; | |
| 2116 | - | |
| 2117 | -{ The first of what try does where the error isn't nil (the report, 7.8): | |
| 2118 | - the machine's stack made even, as the deferred statements about to be | |
| 2119 | - run expect to find it. try can stand among the arguments of another | |
| 2120 | - call, where quadwords have been pushed. Nothing is lost: the frame is | |
| 2121 | - above rsp, and the code returns through the frame pointer. } | |
| 2122 | -procedure TryAlign; | |
| 2123 | -begin | |
| 2124 | - AluI(aluAnd, RSP, -16); | |
| 2125 | -end; | |
| 2126 | - | |
| 2127 | -{ The rest of it: the procedure returns the error in the frame slot, and | |
| 2128 | - a zero for each of its other results. Only code is made: no entry of | |
| 2129 | - the stack of values is touched, since the code that follows is the | |
| 2130 | - code for an error that is nil, with the stack as it was. } | |
| 2131 | -procedure TryReturn(slot: int64); | |
| 2132 | -var f: PObj; | |
| 2133 | -begin | |
| 2134 | - if BehindPointer(resType) then begin | |
| 2135 | - Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 2136 | - MovRI(RCX, resType^.size); | |
| 2137 | - MovRI(RAX, 0); | |
| 2138 | - RepStosb; | |
| 2139 | - f := resType^.fields; | |
| 2140 | - while f^.next <> nil do f := f^.next; | |
| 2141 | - Load(RCX, Mem(RBP, slot), 8, false); | |
| 2142 | - Load(RDX, Mem(RBP, retSlot), 8, false); | |
| 2143 | - Store(Mem(RDX, f^.val), RCX, 8); | |
| 2144 | - Load(RAX, Mem(RBP, retSlot), 8, false); | |
| 2145 | - end else if InTwoRegs(resType) then begin | |
| 2146 | - Load(RDX, Mem(RBP, slot), 8, false); | |
| 2147 | - MovRI(RAX, 0); | |
| 2148 | - end else Load(RAX, Mem(RBP, slot), 8, false); | |
| 2149 | - Jmp(exitL); | |
| 2150 | -end; | |
| 2151 | - | |
| 2152 | -{ The end of the procedure in chunk c: the code that returns, and the | |
| 2153 | - frame's size, known only now, patched into its prologue. If it is | |
| 2154 | - open, running off its end, it returns zeros. } | |
| 2155 | -procedure EndProc(c: longint; open: boolean); | |
| 2156 | -var n: int64; i: longint; | |
| 2157 | -begin | |
| 2158 | - codeChunk := -1; | |
| 2159 | - if (depth <> 0) or (pushed <> 0) then Fatal('internal error: stack depth'); | |
| 2160 | - if open then begin | |
| 2161 | - if BehindPointer(resType) then begin | |
| 2162 | - Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 2163 | - MovRI(RCX, resType^.size); | |
| 2164 | - MovRI(RAX, 0); | |
| 2165 | - RepStosb; | |
| 2166 | - Load(RAX, Mem(RBP, retSlot), 8, false); | |
| 2167 | - end else begin | |
| 2168 | - MovRI(RAX, 0); | |
| 2169 | - if InTwoRegs(resType) then MovRI(RDX, 0); | |
| 2170 | - MovqXR(0, RAX); | |
| 2171 | - end; | |
| 2172 | - end else DropJumpTo(exitL); | |
| 2173 | - FixLink(exitL); | |
| 2174 | - n := 0; | |
| 2175 | - for i := 0 to 4 do | |
| 2176 | - if regMask and (1 shl i) <> 0 then begin | |
| 2177 | - Load(VarRegs[i], Mem(RBP, saveAt + 8 * n), 8, false); | |
| 2178 | - inc(n); | |
| 2179 | - end; | |
| 2180 | - Leave; | |
| 2181 | - Ret; | |
| 2182 | - EndFails; | |
| 2183 | - n := AlignUp(frameMax, 16); | |
| 2184 | - if n < 16 then n := 16; | |
| 2185 | - if n > high(longint) then Mark('frame too big'); | |
| 2186 | - Put4At(frameFix, n); | |
| 2187 | - { the listing's text for the sub, which starts 3 bytes (REX.W, opcode, | |
| 2188 | - ModRM) before its immediate } | |
| 2189 | - Restate(frameFix - 3, 'sub rsp, ' + IntStr(n)); | |
| 2190 | - EndChunk(c); | |
| 2191 | -end; | |
| 2192 | - | |
| 2193 | -{ A deferred statement is compiled where it stands, as a subroutine of | |
| 2194 | - its procedure that the code jumps over, in the procedure's own frame | |
| 2195 | - (the same rbp), so that it sees all its variables; DeferBegin gives its address | |
| 2196 | - and starts it, with the return address pushed. The stack is empty | |
| 2197 | - where it is called, and where it begins. } | |
| 2198 | -function DeferBegin(var L, saved: longint): longint; | |
| 2199 | -begin | |
| 2200 | - Jmp(L); | |
| 2201 | - saved := pushed; | |
| 2202 | - pushed := 1; | |
| 2203 | - DeferBegin := pc; | |
| 2204 | -end; | |
| 2205 | - | |
| 2206 | -procedure DeferEnd(L, saved: longint); | |
| 2207 | -begin | |
| 2208 | - KeepTemps; | |
| 2209 | - Ret; | |
| 2210 | - FixLink(L); | |
| 2211 | - pushed := saved; | |
| 2212 | -end; | |
| 2213 | - | |
| 2214 | -procedure RunDefer(at: longint); | |
| 2215 | -begin | |
| 2216 | - CallBack(at); | |
| 2217 | -end; | |
| 2218 | - | |
| 2219 | -{ The entry point, _start, and its chunk: it saves argc and argv, and | |
| 2220 | - the address of the places for Rt; runs every used module's body, then | |
| 2221 | - main; and ends with Rt.exit, its status main's or 0. A dynamic program | |
| 2222 | - starts as C's crt1 does, through __libc_start_main(main, argc, argv, | |
| 2223 | - nil, nil, rtld_fini, stack_end), with rsp aligned; its main first | |
| 2224 | - gives Rt C's exit, in cExit. | |
| 2225 | - | |
| 2226 | - dynamic: the program uses C. first: the first module, whose chain | |
| 2227 | - gives the order of the bodies. main: the program's main procedure, or | |
| 2228 | - nil. cExit and exitProc: the chunks of Rt's cExit and exit. The | |
| 2229 | - first numLinks of links: the addresses Rt is given, of what the program has: the places, and | |
| 2230 | - the profiler and calls; what it isn't given, nothing reaches, and the | |
| 2231 | - linker leaves out. } | |
| 2232 | -function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, exitProc: longint; const links: array of TLink; numLinks: longint): longint; | |
| 2233 | -var entry, start, i: longint; m: PModule; | |
| 2234 | -begin | |
| 2235 | - entry := NewChunk(secText, -1, Intern('_start', 6)); | |
| 2236 | - if dynamic then begin | |
| 2237 | - start := NewChunk(secText, -1, Intern('main', 4)); | |
| 2238 | - BeginChunk(entry); | |
| 2239 | - { at the start rdx holds rtld_fini, from the dynamic loader, and the | |
| 2240 | - stack argc, then argv } | |
| 2241 | - MovRI(RBP, 0); { no frame above, where a trace stops } | |
| 2242 | - MovRR(R9, RDX); { rtld_fini, the sixth argument } | |
| 2243 | - Pop(RSI); { argc, the second } | |
| 2244 | - MovRR(RDX, RSP); { argv, the third } | |
| 2245 | - AluI(aluAnd, RSP, -16); | |
| 2246 | - Push(RAX); { padding, so that the next push, the seventh } | |
| 2247 | - Push(RSP); { argument, stack_end, leaves rsp aligned } | |
| 2248 | - MovRI(R8, 0); { fini and init: none } | |
| 2249 | - MovRI(RCX, 0); | |
| 2250 | - Lea(RDI, MemRip(start, 0)); { main, the first argument } | |
| 2251 | - CallM(MemRip(ExternChunk(Intern('__libc_start_main', 17)), 0)); | |
| 2252 | - Ud2; { it never returns } | |
| 2253 | - EndChunk(entry); | |
| 2254 | - BeginChunk(start); | |
| 2255 | - Push(RBP); { aligns the stack again } | |
| 2256 | - Store(MemRip(argcChunk, 0), RDI, 8); | |
| 2257 | - Store(MemRip(argvChunk, 0), RSI, 8); | |
| 2258 | - Load(RAX, MemRip(ExternChunk(Intern('exit', 4)), 0), 8, false); | |
| 2259 | - Store(MemRip(cExit, 0), RAX, 8); | |
| 2260 | - end else begin | |
| 2261 | - start := entry; | |
| 2262 | - BeginChunk(entry); | |
| 2263 | - Load(RAX, Mem(RSP, 0), 8, false); | |
| 2264 | - Store(MemRip(argcChunk, 0), RAX, 8); | |
| 2265 | - Lea(RAX, Mem(RSP, 8)); | |
| 2266 | - Store(MemRip(argvChunk, 0), RAX, 8); | |
| 2267 | - end; | |
| 2268 | - for i := 0 to numLinks - 1 do begin | |
| 2269 | - Lea(RAX, MemRip(links[i].chunk, 0)); | |
| 2270 | - Store(MemRip(links[i].into, 0), RAX, 8); | |
| 2271 | - end; | |
| 2272 | - m := first; | |
| 2273 | - while m <> nil do begin | |
| 2274 | - if m^.used and (m^.body >= 0) then CallChunk(m^.body); | |
| 2275 | - m := m^.next; | |
| 2276 | - end; | |
| 2277 | - if main <> nil then CallChunk(main^.chunk); | |
| 2278 | - if (main <> nil) and (main^.typ^.base <> nil) then MovRR(RDI, RAX) else MovRI(RDI, 0); | |
| 2279 | - CallChunk(exitProc); | |
| 2280 | - Ud2; { exit never returns: trap if it does } | |
| 2281 | - EndChunk(start); | |
| 2282 | - { Rt.fail, for the failures of each kind: the place is where the | |
| 2283 | - check's call is. The return address is the next instruction's, which | |
| 2284 | - may belong to another line; one byte back is inside the call, so the | |
| 2285 | - lookup finds the call's line. rsp is aligned for the call, as the | |
| 2286 | - check's code may have pushed an odd number of quadwords. rdi and rsi | |
| 2287 | - as the failing code had them go on the stack, as Rt.fail's last two | |
| 2288 | - arguments, and a comes in rax (EndFails). } | |
| 2289 | - for i := 0 to High(failStubs) do | |
| 2290 | - if failStubs[i] >= 0 then begin | |
| 2291 | - BeginChunk(failStubs[i]); | |
| 2292 | - Load(R8, Mem(RSP, 0), 8, false); | |
| 2293 | - AluI(aluSub, R8, 1); | |
| 2294 | - AluI(aluAnd, RSP, -16); | |
| 2295 | - Push(RSI); | |
| 2296 | - Push(RDI); | |
| 2297 | - MovRR(RSI, RAX); | |
| 2298 | - MovRI(RDI, i); | |
| 2299 | - CallChunk(rtFail); | |
| 2300 | - Ud2; | |
| 2301 | - EndChunk(failStubs[i]); | |
| 2302 | - end; | |
| 2303 | - EntryPoint := entry; | |
| 2304 | -end; | |
| 2305 | - | |
| 2306 | -{ A code procedure: its instructions, read by the assembler (x64.pas), | |
| 2307 | - are the whole of chunk c } | |
| 2308 | -procedure CodeProc(c: longint; constExpr: TConstFn); | |
| 2309 | -begin | |
| 2310 | - BeginChunk(c); | |
| 2311 | - Assemble(constExpr); | |
| 2312 | - EndChunk(c); | |
| 2313 | -end; | |
| 2314 | - | |
| 2315 | -initialization | |
| 2316 | - ResetStack(0, 0); | |
| 2317 | - rtFail := NewChunk(secText, Intern('Rt', 2), Intern('fail', 4)); | |
| 2318 | - argcChunk := NewChunk(secBss, -1, Intern('argc', 4)); | |
| 2319 | - SetBss(argcChunk, 8, 8); | |
| 2320 | - argvChunk := NewChunk(secBss, -1, Intern('argv', 4)); | |
| 2321 | - SetBss(argvChunk, 8, 8); | |
| 2322 | -end. |
| @@ -1,1575 +0,0 @@ | ||
| 1 | -{ item.pas: operands, their types, and what they mean. | |
| 2 | - | |
| 3 | - The parser describes each operand as an item (TItem), as in Wirth's | |
| 4 | - compilers: a constant, a variable at an address it can name, a value | |
| 5 | - on gen's stack, a procedure, a string. The routines here check the | |
| 6 | - items' types, fold constants, and have gen.pas make the code, as calls | |
| 7 | - of its stack machine; nothing here knows the machine. | |
| 8 | - | |
| 9 | - Depends on: scan for errors, sym for types, chunk for data, gen for | |
| 10 | - code. | |
| 11 | - Leaves out: the language's syntax, which is parse.pas; the machine, | |
| 12 | - which is gen.pas. } | |
| 13 | - | |
| 14 | -unit item; | |
| 15 | - | |
| 16 | -{$mode objfpc}{$H-} | |
| 17 | - | |
| 18 | -interface | |
| 19 | - | |
| 20 | -uses scan, diag, sym, chunk, gen; | |
| 21 | - | |
| 22 | -type | |
| 23 | - TMode = (mConst, mVar, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone, mCond, | |
| 24 | - mWords); { two results of a call, on the stack, the first under the second } | |
| 25 | - | |
| 26 | - TItem = record | |
| 27 | - mode: TMode; | |
| 28 | - typ: PType; | |
| 29 | - rel: TSym; { mCond: the comparison of the two integers on | |
| 30 | - the stack, not yet made, as Wirth's compilers | |
| 31 | - keep a condition } | |
| 32 | - relSigned: boolean; | |
| 33 | - fj, tj: longint; { mCond: the chains of jumps already made that are | |
| 34 | - taken when the whole condition is false, and when | |
| 35 | - it is true, not yet given a place (and, or and | |
| 36 | - not make them); see gen's JumpOut } | |
| 37 | - val: int64; { mConst: the value, an F64's bits; mStr: its length } | |
| 38 | - base: TBase; { mVar: what the address is relative to } | |
| 39 | - chunk: longint; { mVar with bGlobal, mStr: the chunk; bSlot: the slot } | |
| 40 | - off: int64; { mVar: added to the base } | |
| 41 | - vol: boolean; { mVar: reached through ^VOLATILE } | |
| 42 | - isVar: boolean; { mVar: a variable of the program, not a temporary } | |
| 43 | - odd: boolean; { mVar: in a packed record, so maybe misaligned } | |
| 44 | - ro: boolean; { mVar: another module's exported variable, or part | |
| 45 | - of one, which only that module changes } | |
| 46 | - deref: boolean; { mVar: at an address from a pointer, which may be | |
| 47 | - nil and isn't tested yet (NilTest) } | |
| 48 | - obj: PObj; { mProc, mBuiltin, mCase; mVar: the variable named } | |
| 49 | - pos: TPos; { where it is in the source, for messages } | |
| 50 | - end; | |
| 51 | - | |
| 52 | -var | |
| 53 | - demoted: boolean; { Address wanted a register variable's address: its | |
| 54 | - procedure is compiled again, the variable in memory } | |
| 55 | - checks: boolean; { the module's index and slice checks are made: it | |
| 56 | - doesn't say check off } | |
| 57 | - | |
| 58 | -procedure MakeConst(var x: TItem; t: PType; v: int64); | |
| 59 | -function What(const x: TItem): shortstring; | |
| 60 | -procedure MakeReal(var x: TItem; r: double); | |
| 61 | -procedure MakeStr(var x: TItem); | |
| 62 | -procedure MakeItem(var x: TItem; o: PObj); | |
| 63 | -function RealOf(const x: TItem): double; | |
| 64 | -function FrameAdr(off: int64): TAdr; | |
| 65 | -function GlobalAdr(c: longint): TAdr; | |
| 66 | -function AdrOf(const x: TItem): TAdr; | |
| 67 | - | |
| 68 | -procedure UsesReals(const p: TPos); | |
| 69 | -procedure Value(var x: TItem); | |
| 70 | -procedure Address(var x: TItem); | |
| 71 | -procedure Discard(const x: TItem); | |
| 72 | -procedure NilTest(var x: TItem); | |
| 73 | -procedure Coerce(var x: TItem; t: PType); | |
| 74 | -procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); | |
| 75 | -procedure NotReadOnly(const x: TItem); | |
| 76 | -procedure Assign(var dst, src: TItem); | |
| 77 | -procedure CopyToTemp(var x: TItem); | |
| 78 | -function ToSlot(var x: TItem): int64; | |
| 79 | - | |
| 80 | -procedure Deref(var x: TItem); | |
| 81 | -procedure Field(var x: TItem; f: PObj); | |
| 82 | -procedure IndexPrep(var x: TItem); | |
| 83 | -procedure Index(var x, y: TItem); | |
| 84 | -procedure SliceLo(var lo: TItem); | |
| 85 | -procedure Slice(var x, lo, hi: TItem); | |
| 86 | - | |
| 87 | -procedure Neg(var x: TItem); | |
| 88 | -procedure NotOp(var x: TItem); | |
| 89 | -procedure Complement(var x: TItem); | |
| 90 | -procedure Op1(op: TSym; var x: TItem; var L: longint); | |
| 91 | -procedure Op2(op: TSym; var x, y: TItem; L: longint); | |
| 92 | -function IsWord(t: PType): boolean; | |
| 93 | -procedure Convert(var x: TItem; t: PType); | |
| 94 | - | |
| 95 | -procedure BeginCall(var c: TCall; var x: TItem); | |
| 96 | -procedure PassArg(var c: TCall; var a: TItem); | |
| 97 | -procedure EndCall(var c: TCall; var x: TItem); | |
| 98 | - | |
| 99 | -procedure ZeroTemp(var x: TItem; t: PType); | |
| 100 | -procedure Construct(var x: TItem; c: PObj); | |
| 101 | -procedure SetField(const x: TItem; f: PObj; var a: TItem); | |
| 102 | -function MatchBegin(var x: TItem): int64; | |
| 103 | -procedure MatchTag(p: int64; tag: int64; var L: longint); | |
| 104 | -procedure MatchRange(p, lo, hi: int64; var body: longint); | |
| 105 | -procedure CondJump(var x: TItem; var L: longint); | |
| 106 | -procedure CondJumpTrue(var x: TItem; var L: longint); | |
| 107 | - | |
| 108 | -procedure SliceOf(var x: TItem; t: PType; len: int64); | |
| 109 | -procedure TempItem(var x: TItem; t: PType; off: int64); | |
| 110 | -function ScalarSize(t: PType): longint; | |
| 111 | -procedure ForStep(o: PObj; step: int64); | |
| 112 | -procedure ForCmp(o: PObj; const lim: TItem; op: TSym; var L: longint); | |
| 113 | -procedure ForNext(o: PObj; const lim: TItem; step: int64; var exitL: longint; top, body: longint); | |
| 114 | - | |
| 115 | -procedure BeginProc(sig: PType; params: PObj); | |
| 116 | -procedure ReturnValue(var x: TItem); | |
| 117 | -procedure ReturnField(f: PObj; var x: TItem); | |
| 118 | -procedure ReturnAll(var x: TItem); | |
| 119 | -procedure ReturnNone; | |
| 120 | -procedure Pin(var x: TItem); | |
| 121 | -procedure IncDec(var v, k: TItem; dec: boolean); | |
| 122 | -procedure CopyDest(var d: TItem); | |
| 123 | -procedure CopyFrom(var d, s: TItem); | |
| 124 | -procedure Msg(var x: TItem); | |
| 125 | -function ErrorChunk(var x: TItem; module, name: longint): longint; | |
| 126 | - | |
| 127 | -implementation | |
| 128 | - | |
| 129 | -const | |
| 130 | - { the comparison that holds when op doesn't, for integers } | |
| 131 | - Negated: array[sEql..sGeq] of TSym = (sNeq, sEql, sGeq, sGtr, sLeq, sLss); | |
| 132 | - | |
| 133 | -var | |
| 134 | - result: PType; { the result of the procedure being compiled, or nil } | |
| 135 | - | |
| 136 | -{ Items } | |
| 137 | - | |
| 138 | -procedure MakeConst(var x: TItem; t: PType; v: int64); | |
| 139 | -var p: TPos; | |
| 140 | -begin | |
| 141 | - p := x.pos; | |
| 142 | - FillChar(x, SizeOf(x), 0); | |
| 143 | - x.pos := p; | |
| 144 | - x.mode := mConst; | |
| 145 | - x.typ := t; | |
| 146 | - x.val := v; | |
| 147 | -end; | |
| 148 | - | |
| 149 | -{ A real constant: untyped, unless typed later. Its value is an F64, | |
| 150 | - which for an F32 is one an F32 can hold. } | |
| 151 | -procedure MakeReal(var x: TItem; r: double); | |
| 152 | -begin | |
| 153 | - MakeConst(x, tRConst, 0); | |
| 154 | - Move(r, x.val, 8); | |
| 155 | -end; | |
| 156 | - | |
| 157 | -function IsF32(t: PType): boolean; | |
| 158 | -begin | |
| 159 | - IsF32 := (t^.form = fReal) and (t^.size = 4); | |
| 160 | -end; | |
| 161 | - | |
| 162 | -{ Gives a real constant the real type t, rounded to it. } | |
| 163 | -procedure RealTo(var x: TItem; t: PType); | |
| 164 | -var r: double; | |
| 165 | -begin | |
| 166 | - r := RealOf(x); | |
| 167 | - if IsF32(t) then MakeReal(x, single(r)) else MakeReal(x, r); | |
| 168 | - x.typ := t; | |
| 169 | -end; | |
| 170 | - | |
| 171 | -function RealOf(const x: TItem): double; | |
| 172 | -var r: double; | |
| 173 | -begin | |
| 174 | - Move(x.val, r, 8); | |
| 175 | - RealOf := r; | |
| 176 | -end; | |
| 177 | - | |
| 178 | -{ The current string literal, as an item of its own chunk. } | |
| 179 | -procedure MakeStr(var x: TItem); | |
| 180 | -begin | |
| 181 | - FillChar(x, SizeOf(x), 0); | |
| 182 | - x.mode := mStr; | |
| 183 | - x.typ := tStr; | |
| 184 | - x.val := slen; | |
| 185 | - x.chunk := NewChunk(secRodata, -1, -1); | |
| 186 | - SetData(x.chunk, @sbuf, slen + 1, 1); | |
| 187 | -end; | |
| 188 | - | |
| 189 | -function FrameAdr(off: int64): TAdr; | |
| 190 | -begin | |
| 191 | - FrameAdr.base := bFrame; | |
| 192 | - FrameAdr.chunk := -1; | |
| 193 | - FrameAdr.off := off; | |
| 194 | - FrameAdr.hi := 0; | |
| 195 | -end; | |
| 196 | - | |
| 197 | -function GlobalAdr(c: longint): TAdr; | |
| 198 | -begin | |
| 199 | - GlobalAdr.base := bGlobal; | |
| 200 | - GlobalAdr.chunk := c; | |
| 201 | - GlobalAdr.off := 0; | |
| 202 | - GlobalAdr.hi := 0; | |
| 203 | -end; | |
| 204 | - | |
| 205 | -{ Where the variable x is, for gen; a slice in registers has two } | |
| 206 | -function AdrOf(const x: TItem): TAdr; | |
| 207 | -begin | |
| 208 | - AdrOf.base := x.base; | |
| 209 | - AdrOf.chunk := x.chunk; | |
| 210 | - AdrOf.off := x.off; | |
| 211 | - AdrOf.hi := 0; | |
| 212 | - if x.base = bReg then AdrOf.hi := x.obj^.reg2; | |
| 213 | -end; | |
| 214 | - | |
| 215 | -{ An item for "found ...": a constant without a type of its own as it | |
| 216 | - is written, so that "found 1.5" says what is wrong; else its type. } | |
| 217 | -function What(const x: TItem): shortstring; | |
| 218 | -begin | |
| 219 | - if (x.mode = mConst) and ((x.typ = tConst) or (x.typ = tRConst)) then What := Source(x.pos) | |
| 220 | - else What := TypeName(x.typ); | |
| 221 | -end; | |
| 222 | - | |
| 223 | -{ Two operands of different types. Numbers convert, so say how. } | |
| 224 | -procedure Mismatch(const x, y: TItem; xt, yt: PType); | |
| 225 | -var msg: shortstring; | |
| 226 | -begin | |
| 227 | - msg := TypeName(xt) + ' and ' + TypeName(yt) + ' are different types'; | |
| 228 | - if (IsInt(xt) or (xt^.form = fReal)) and (IsInt(yt) or (yt^.form = fReal)) | |
| 229 | - and (xt <> tConst) and (xt <> tRConst) then | |
| 230 | - MarkHelp(Join(x.pos, y.pos), msg, 'convert one to the other''s type, as in ' + TypeName(xt) + '(' + Source(y.pos) + ')') | |
| 231 | - else MarkAt(Join(x.pos, y.pos), msg); | |
| 232 | -end; | |
| 233 | - | |
| 234 | -{ An item that is not a value, by what it is. } | |
| 235 | -procedure NotValue(const x: TItem); | |
| 236 | -begin | |
| 237 | - case x.mode of | |
| 238 | - mType: MarkAt(x.pos, TypeName(x.typ) + ' is a type, not a value'); | |
| 239 | - mBuiltin: MarkAt(x.pos, NameOf(x.obj^.name) + ' is a built-in procedure, not a value'); | |
| 240 | - mCase: MarkAt(x.pos, NameOf(x.obj^.name) + ' is a case, not a value'); | |
| 241 | - mNone: | |
| 242 | - if x.obj <> nil then MarkAt(x.pos, NameOf(x.obj^.name) + ' returns nothing') | |
| 243 | - else MarkAt(x.pos, 'the procedure returns nothing'); | |
| 244 | - else MarkAt(x.pos, 'a string can''t be an operand'); | |
| 245 | - end; | |
| 246 | -end; | |
| 247 | - | |
| 248 | -procedure MakeItem(var x: TItem; o: PObj); | |
| 249 | -begin | |
| 250 | - FillChar(x, SizeOf(x), 0); | |
| 251 | - x.typ := o^.typ; | |
| 252 | - x.obj := o; | |
| 253 | - case o^.cls of | |
| 254 | - oConst: | |
| 255 | - if o^.typ = tStr then begin | |
| 256 | - x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk | |
| 257 | - end else if o^.typ^.form in [fArray, fRecord] then begin | |
| 258 | - { an array or record constant: a variable in read-only data, not one of | |
| 259 | - the program's, so nothing can change it } | |
| 260 | - x.mode := mVar; x.base := bGlobal; x.chunk := o^.chunk | |
| 261 | - end else begin | |
| 262 | - x.mode := mConst; x.val := o^.val | |
| 263 | - end; | |
| 264 | - oVar: begin | |
| 265 | - x.mode := mVar; | |
| 266 | - x.ro := (o^.lev = 0) and (o^.module <> curModule); | |
| 267 | - x.isVar := not x.ro; | |
| 268 | - if o^.ext then begin | |
| 269 | - PushVar(GlobalAdr(o^.chunk), 8, false, kWord); | |
| 270 | - x.base := bStack; | |
| 271 | - end else if o^.lev = 0 then begin | |
| 272 | - x.base := bGlobal; x.chunk := o^.chunk | |
| 273 | - end else if o^.ind then begin | |
| 274 | - x.base := bSlot; x.chunk := o^.val; x.off := o^.off; | |
| 275 | - end else if o^.reg <> 0 then begin | |
| 276 | - x.base := bReg; x.chunk := o^.reg | |
| 277 | - end else begin | |
| 278 | - x.base := bFrame; x.off := o^.val | |
| 279 | - end; | |
| 280 | - end; | |
| 281 | - oProc: x.mode := mProc; | |
| 282 | - oType: x.mode := mType; | |
| 283 | - oBuiltin: x.mode := mBuiltin; | |
| 284 | - oCase: x.mode := mCase; | |
| 285 | - else Mark(NameOf(o^.name) + ' is not a value'); | |
| 286 | - end; | |
| 287 | -end; | |
| 288 | - | |
| 289 | -function ScalarSize(t: PType): longint; | |
| 290 | -begin | |
| 291 | - if t^.form in [fInt, fBool, fEnum, fSet, fReal] then ScalarSize := t^.size | |
| 292 | - else ScalarSize := 8; | |
| 293 | -end; | |
| 294 | - | |
| 295 | -{ The width an operation on t is brought back to, and whether signed } | |
| 296 | -function IntSize(t: PType): longint; | |
| 297 | -begin | |
| 298 | - if t^.form = fInt then IntSize := t^.size else IntSize := 8; | |
| 299 | -end; | |
| 300 | - | |
| 301 | -function Signed(t: PType): boolean; | |
| 302 | -begin | |
| 303 | - Signed := ((t^.form = fInt) and t^.signed) or (t^.form = fConst); | |
| 304 | -end; | |
| 305 | - | |
| 306 | -function Cut(v: int64; t: PType): int64; | |
| 307 | -begin | |
| 308 | - Cut := v; | |
| 309 | - if t^.form <> fInt then exit; | |
| 310 | - case t^.size of | |
| 311 | - 1: if t^.signed then Cut := shortint(v) else Cut := byte(v); | |
| 312 | - 2: if t^.signed then Cut := smallint(v) else Cut := word(v); | |
| 313 | - 4: if t^.signed then Cut := longint(v) else Cut := longword(v); | |
| 314 | - end; | |
| 315 | -end; | |
| 316 | - | |
| 317 | -{ Code computing with a real is noted on its chunk, at the first place, | |
| 318 | - for a target without reals (report 5.1) to refuse if the chunk turns | |
| 319 | - out to be used. A real moved whole, as a record's bytes are copied, | |
| 320 | - needs nothing the target lacks, and isn't noted. } | |
| 321 | -procedure UsesReals(const p: TPos); | |
| 322 | -begin | |
| 323 | - if (codeChunk >= 0) and not chunks^[codeChunk].reals then begin | |
| 324 | - chunks^[codeChunk].reals := true; | |
| 325 | - chunks^[codeChunk].realsAt := p; | |
| 326 | - end; | |
| 327 | -end; | |
| 328 | - | |
| 329 | -{ x, at an address from a pointer, is about to be used: gen tests the | |
| 330 | - address for nil where the machine wouldn't stop at it (report section | |
| 331 | - 10), from its offset to its end. } | |
| 332 | -procedure NilTest(var x: TItem); | |
| 333 | -begin | |
| 334 | - if (x.mode = mVar) and x.deref then CheckNil(x.off + x.typ^.size, x.pos); | |
| 335 | - x.deref := false; | |
| 336 | -end; | |
| 337 | - | |
| 338 | -{ x's value, on top of the stack } | |
| 339 | -procedure Value(var x: TItem); | |
| 340 | -var f: single; | |
| 341 | -begin | |
| 342 | - if x.mode = mCond then begin | |
| 343 | - if (x.fj = NoLabel) and (x.tj = NoLabel) then Compare(x.rel, kWord, x.relSigned) | |
| 344 | - else CondValue(Negated[x.rel], x.relSigned, x.fj, x.tj); | |
| 345 | - x.mode := mStack; | |
| 346 | - exit; | |
| 347 | - end; | |
| 348 | - if x.typ^.form = fReal then UsesReals(x.pos); | |
| 349 | - NilTest(x); | |
| 350 | - case x.mode of | |
| 351 | - mConst: | |
| 352 | - if (x.typ^.form = fError) and (x.val > 0) then PushAdr(GlobalAdr(x.val - 1)) | |
| 353 | - else if IsF32(x.typ) then begin | |
| 354 | - f := RealOf(x); | |
| 355 | - PushConst(kF32, PLongword(@f)^); | |
| 356 | - end else PushConst(KindOf(x.typ), x.val); | |
| 357 | - mVar: begin | |
| 358 | - if not IsScalar(x.typ) then MarkAt(x.pos, 'expected a single value, found ' + What(x)); | |
| 359 | - PushVar(AdrOf(x), ScalarSize(x.typ), (x.typ^.form = fInt) and x.typ^.signed, KindOf(x.typ)); | |
| 360 | - end; | |
| 361 | - mStack: ; | |
| 362 | - mProc: begin | |
| 363 | - if x.obj^.isCode and (x.obj^.typ^.base <> nil) and x.obj^.typ^.base^.tuple then | |
| 364 | - MarkAt(x.pos, 'a code procedure with two results can only be called'); | |
| 365 | - PushProc(x.obj^.chunk, x.obj^.ext); | |
| 366 | - end; | |
| 367 | - else NotValue(x); | |
| 368 | - end; | |
| 369 | - x.mode := mStack; | |
| 370 | -end; | |
| 371 | - | |
| 372 | -{ The address of the variable or string x, on top of the stack } | |
| 373 | -procedure Address(var x: TItem); | |
| 374 | -begin | |
| 375 | - NilTest(x); | |
| 376 | - if (x.mode = mVar) and (x.base = bReg) then begin | |
| 377 | - { its address is wanted after all: the procedure is compiled again | |
| 378 | - with the variable in memory; till then, this code is thrown away } | |
| 379 | - x.obj^.reg := 0; | |
| 380 | - x.obj^.reg2 := 0; | |
| 381 | - demoted := true; | |
| 382 | - x.base := bFrame; | |
| 383 | - x.off := x.obj^.val; | |
| 384 | - end; | |
| 385 | - if x.mode = mStr then PushAdr(GlobalAdr(x.chunk)) | |
| 386 | - else if x.mode = mVar then begin | |
| 387 | - PushAdr(AdrOf(x)); | |
| 388 | - x.base := bStack; x.off := 0; | |
| 389 | - end else MarkAt(x.pos, 'expected a variable'); | |
| 390 | -end; | |
| 391 | - | |
| 392 | -{ x isn't wanted: what it has on the stack is dropped } | |
| 393 | -procedure Discard(const x: TItem); | |
| 394 | -var y: TItem; | |
| 395 | -begin | |
| 396 | - if x.mode = mCond then begin y := x; Value(y); Drop; exit end; | |
| 397 | - if (x.mode = mStack) or ((x.mode = mVar) and (x.base = bStack)) then Drop; | |
| 398 | - if x.mode = mWords then begin Drop; Drop end; | |
| 399 | -end; | |
| 400 | - | |
| 401 | -{ Frames } | |
| 402 | - | |
| 403 | -procedure TempItem(var x: TItem; t: PType; off: int64); | |
| 404 | -var p: TPos; | |
| 405 | -begin | |
| 406 | - p := x.pos; | |
| 407 | - FillChar(x, SizeOf(x), 0); | |
| 408 | - x.pos := p; | |
| 409 | - x.mode := mVar; x.typ := t; x.base := bFrame; x.off := off; | |
| 410 | -end; | |
| 411 | - | |
| 412 | -function ToSlot(var x: TItem): int64; | |
| 413 | -var s: int64; | |
| 414 | -begin | |
| 415 | - Value(x); | |
| 416 | - s := AllocTemp(8, 8); | |
| 417 | - PopVar(FrameAdr(s), 8); | |
| 418 | - ToSlot := s; | |
| 419 | -end; | |
| 420 | - | |
| 421 | -{ Copying } | |
| 422 | - | |
| 423 | -{ x, copied to a temporary } | |
| 424 | -procedure CopyToTemp(var x: TItem); | |
| 425 | -var t: int64; f, g: PObj; a: TAdr; | |
| 426 | -begin | |
| 427 | - if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 428 | - t := AllocTemp(x.typ^.size, x.typ^.align); | |
| 429 | - if (x.mode = mVar) and (x.base = bReg) then begin | |
| 430 | - { a slice in registers, which has no address: its words stored } | |
| 431 | - a := AdrOf(x); | |
| 432 | - PushVar(a, 8, false, kWord); | |
| 433 | - PopVar(FrameAdr(t), 8); | |
| 434 | - a.off := 8; | |
| 435 | - PushVar(a, 8, false, kWord); | |
| 436 | - PopVar(FrameAdr(t + 8), 8); | |
| 437 | - TempItem(x, x.typ, t); | |
| 438 | - exit; | |
| 439 | - end; | |
| 440 | - if x.mode = mWords then begin | |
| 441 | - { the second result is on top, so it is popped first } | |
| 442 | - f := x.typ^.fields; g := f^.next; | |
| 443 | - PopVar(FrameAdr(t + g^.val), ScalarSize(g^.typ)); | |
| 444 | - PopVar(FrameAdr(t + f^.val), ScalarSize(f^.typ)); | |
| 445 | - TempItem(x, x.typ, t); | |
| 446 | - exit; | |
| 447 | - end; | |
| 448 | - Address(x); | |
| 449 | - CopyTo(FrameAdr(t), x.typ^.size); | |
| 450 | - TempItem(x, x.typ, t); | |
| 451 | -end; | |
| 452 | - | |
| 453 | -{ A slice made from an address on the stack and a length. } | |
| 454 | -procedure SliceOf(var x: TItem; t: PType; len: int64); | |
| 455 | -var s: int64; | |
| 456 | -begin | |
| 457 | - s := AllocTemp(16, 8); | |
| 458 | - PopVar(FrameAdr(s), 8); | |
| 459 | - PushConst(kWord, len); | |
| 460 | - PopVar(FrameAdr(s + 8), 8); | |
| 461 | - TempItem(x, t, s); | |
| 462 | -end; | |
| 463 | - | |
| 464 | -{ An INT goes silently into an I64, and a UINT into a U64, losing | |
| 465 | - nothing on any target (report section 5.10). } | |
| 466 | -function Widens(s, t: PType): boolean; | |
| 467 | -begin | |
| 468 | - Widens := ((s = tInt) and Same(t, tI64)) or ((s = tUInt) and Same(t, tU64)); | |
| 469 | -end; | |
| 470 | - | |
| 471 | -{ Another module's exported variable can be read, not changed, nor | |
| 472 | - handed to anything that could change it (report section 3). } | |
| 473 | -procedure NotReadOnly(const x: TItem); | |
| 474 | -begin | |
| 475 | - if x.ro then MarkAt(x.pos, Source(x.pos) + ' is ' + NameOf(x.obj^.module^.name) + '''s, and can''t be changed outside it'); | |
| 476 | -end; | |
| 477 | - | |
| 478 | -var | |
| 479 | - passing: PObj; { the parameter the argument at passingAt goes to } | |
| 480 | - passingAt: TPos; | |
| 481 | - | |
| 482 | -{ An error in x's type; if x is an argument, a note at the parameter | |
| 483 | - it goes to. } | |
| 484 | -procedure Mismatch(const x: TItem; const msg: shortstring); | |
| 485 | -begin | |
| 486 | - Error(x.pos, msg); | |
| 487 | - if (passing <> nil) and (x.pos.f = passingAt.f) and (x.pos.a = passingAt.a) and (x.pos.b = passingAt.b) then | |
| 488 | - Note(passing^.pos, 'it goes to ' + NameOf(passing^.name) + ', declared here'); | |
| 489 | - Fail; | |
| 490 | -end; | |
| 491 | - | |
| 492 | -procedure Coerce(var x: TItem; t: PType); | |
| 493 | -var s: PType; tmp: int64; | |
| 494 | -begin | |
| 495 | - if x.mode = mCond then Value(x); | |
| 496 | - s := x.typ; | |
| 497 | - if x.mode = mStr then begin | |
| 498 | - if (t^.form = fSlice) and (t^.base = tU8) then begin | |
| 499 | - Address(x); SliceOf(x, t, x.val) | |
| 500 | - end else if (t^.form = fPtr) and (t^.base = tU8) then begin | |
| 501 | - Address(x); x.mode := mStack; x.typ := t | |
| 502 | - end else if (t^.form = fArray) and (t^.base = tU8) and (t^.len >= x.val) then begin | |
| 503 | - tmp := AllocTemp(t^.size, 1); | |
| 504 | - Address(x); | |
| 505 | - CopyTo(FrameAdr(tmp), x.val); | |
| 506 | - if t^.len > x.val then ZeroVar(FrameAdr(tmp + x.val), t^.len - x.val); | |
| 507 | - TempItem(x, t, tmp); | |
| 508 | - end else Mismatch(x, 'can''t use a string as ' + TypeName(t)); | |
| 509 | - exit; | |
| 510 | - end; | |
| 511 | - if x.mode in [mType, mBuiltin, mNone, mCase] then NotValue(x); | |
| 512 | - if Same(s, t) then exit; | |
| 513 | - if Widens(s, t) then x.typ := t { the same bits, on a 64-bit target } | |
| 514 | - else if (s = tRConst) and (t^.form = fReal) then RealTo(x, t) | |
| 515 | - else if (s^.form = fConst) and (t^.form = fInt) then begin | |
| 516 | - if (x.mode = mConst) and not Fits(x.val, t) then MarkAt(x.pos, IntStr(x.val) + ' doesn''t fit ' + TypeName(t)); | |
| 517 | - x.typ := t; | |
| 518 | - end else if (s^.form = fNil) and (t^.form in [fPtr, fProc, fError]) then | |
| 519 | - x.typ := t | |
| 520 | - else if (s^.form = fPtr) and (t^.form = fPtr) and t^.vol and not s^.vol | |
| 521 | - and Same(s^.base, t^.base) then | |
| 522 | - x.typ := t | |
| 523 | - else if (s^.form = fArray) and (t^.form = fSlice) and Same(s^.base, t^.base) then begin | |
| 524 | - NotReadOnly(x); | |
| 525 | - if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); | |
| 526 | - if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); | |
| 527 | - Address(x); | |
| 528 | - SliceOf(x, t, s^.len); | |
| 529 | - end else if (x.mode = mProc) and (t^.form <> fProc) and (s^.base = nil) then | |
| 530 | - MarkAt(x.pos, Source(x.pos) + ' is a procedure, and returns nothing') | |
| 531 | - else if (x.mode = mProc) and (t^.form <> fProc) then | |
| 532 | - MarkHelp(x.pos, 'can''t use ' + TypeName(s) + ' as ' + TypeName(t), 'to call it, write ' + Source(x.pos) + '()') | |
| 533 | - else | |
| 534 | - Mismatch(x, 'can''t use ' + TypeName(s) + ' as ' + TypeName(t)); | |
| 535 | -end; | |
| 536 | - | |
| 537 | -{ Coerce where the types must be the same, as in a for loop, INC and | |
| 538 | - several results, which don't widen an INT to an I64 (report sections | |
| 539 | - 5.10, 6.1 and 7.2); help, if not empty, says why. } | |
| 540 | -procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); | |
| 541 | -var h: shortstring; | |
| 542 | -begin | |
| 543 | - if Widens(x.typ, t) then begin | |
| 544 | - h := help; | |
| 545 | - if h = '' then h := 'they don''t mix here: write ' + TypeName(t) + '(' + Source(x.pos) + ')'; | |
| 546 | - MarkHelp(x.pos, 'expected ' + TypeName(t) + ', found ' + What(x), h); | |
| 547 | - end; | |
| 548 | - Coerce(x, t); | |
| 549 | -end; | |
| 550 | - | |
| 551 | -{ dst := src. The address of dst, if it has to be computed, is on the | |
| 552 | - stack already, under src's. } | |
| 553 | -procedure Assign(var dst, src: TItem); | |
| 554 | -begin | |
| 555 | - Coerce(src, dst.typ); | |
| 556 | - if IsScalar(dst.typ) then begin | |
| 557 | - Value(src); | |
| 558 | - PopVar(AdrOf(dst), ScalarSize(dst.typ)); | |
| 559 | - end else begin | |
| 560 | - if dst.vol or src.vol then MarkAt(src.pos, 'can''t copy through a volatile pointer'); | |
| 561 | - if (src.mode = mVar) and (src.base = bReg) then CopyToTemp(src); { a slice in registers } | |
| 562 | - Address(src); | |
| 563 | - if dst.base = bStack then CopyBlock(dst.typ^.size, dst.off) | |
| 564 | - else CopyTo(AdrOf(dst), dst.typ^.size); | |
| 565 | - end; | |
| 566 | -end; | |
| 567 | - | |
| 568 | -{ t's name, ending in a zero byte, for Rt.fail } | |
| 569 | -function NameChunk(t: PType): longint; | |
| 570 | -var s: shortstring; c: longint; | |
| 571 | -begin | |
| 572 | - s := TypeName(t) + #0; | |
| 573 | - c := NewChunk(secRodata, -1, -1); | |
| 574 | - SetData(c, @s[1], length(s), 1); | |
| 575 | - NameChunk := c; | |
| 576 | -end; | |
| 577 | - | |
| 578 | -{ Selectors } | |
| 579 | - | |
| 580 | -procedure Deref(var x: TItem); | |
| 581 | -var t: PType; | |
| 582 | -begin | |
| 583 | - t := x.typ; | |
| 584 | - if t^.form <> fPtr then MarkAt(x.pos, 'expected a pointer, found ' + TypeName(t)); | |
| 585 | - if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared'); | |
| 586 | - Value(x); | |
| 587 | - x.mode := mVar; x.base := bStack; x.off := 0; x.deref := true; | |
| 588 | - x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; | |
| 589 | -end; | |
| 590 | - | |
| 591 | -{ A field of a packed record, of more than one byte's alignment, may | |
| 592 | - sit anywhere; it is read and written, but has no address and no | |
| 593 | - volatile access, which a machine that can't reach a misaligned word | |
| 594 | - couldn't make one access (report section 5.5). } | |
| 595 | -procedure Field(var x: TItem; f: PObj); | |
| 596 | -begin | |
| 597 | - if x.typ^.packd and (f^.typ^.align > 1) then begin | |
| 598 | - if x.vol then MarkAt(x.pos, 'a field of a packed record can''t be volatile'); | |
| 599 | - x.odd := true; | |
| 600 | - end; | |
| 601 | - x.off := x.off + f^.val; | |
| 602 | - x.typ := f^.typ; | |
| 603 | -end; | |
| 604 | - | |
| 605 | -procedure IndexPrep(var x: TItem); | |
| 606 | -begin | |
| 607 | - NilTest(x); | |
| 608 | - if x.mode = mStr then Coerce(x, tBytes); | |
| 609 | - if (x.typ^.form = fPtr) and (x.typ^.base^.form = fArray) then | |
| 610 | - MarkAt(x.pos, 'a pointer to an array is indexed as p^[i]'); | |
| 611 | - if x.typ^.form = fPtr then Value(x) | |
| 612 | - else if not ((x.mode = mVar) and (x.typ^.form in [fArray, fSlice])) then | |
| 613 | - MarkAt(x.pos, 'expected an array, a slice or a pointer, found ' + What(x)); | |
| 614 | -end; | |
| 615 | - | |
| 616 | -{ The index on top, times the size of an element } | |
| 617 | -procedure ScaleBy(s: int64); | |
| 618 | -begin | |
| 619 | - if s <> 1 then begin | |
| 620 | - PushConst(kWord, s); | |
| 621 | - gen.Op(sTimes, kWord, 8, true); | |
| 622 | - end; | |
| 623 | -end; | |
| 624 | - | |
| 625 | -procedure Index(var x, y: TItem); | |
| 626 | -var t: PType; s: int64; | |
| 627 | -begin | |
| 628 | - t := x.typ; | |
| 629 | - s := t^.base^.size; | |
| 630 | - if not IsInt(y.typ) then MarkAt(y.pos, 'an index is an integer, found ' + What(y)); | |
| 631 | - if t^.form = fPtr then begin | |
| 632 | - Value(y); | |
| 633 | - ScaleBy(s); | |
| 634 | - gen.Op(sPlus, kWord, 8, false); | |
| 635 | - x.mode := mVar; x.base := bStack; x.off := 0; | |
| 636 | - x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; | |
| 637 | - end else if (t^.form = fArray) and (y.mode = mConst) then begin | |
| 638 | - if (y.val < 0) or (y.val >= t^.len) then MarkAt(y.pos, 'index ' + IntStr(y.val) + ' is out of range for a length of ' + IntStr(t^.len)); | |
| 639 | - x.off := x.off + y.val * s; | |
| 640 | - end else if t^.form = fArray then begin | |
| 641 | - Value(y); | |
| 642 | - if checks then CheckIndex(t^.len, y.pos); | |
| 643 | - if x.base = bStack then begin ScaleBy(s); gen.Op(sPlus, kWord, 8, false) end | |
| 644 | - else begin IndexArray(AdrOf(x), s); x.off := 0 end; | |
| 645 | - x.base := bStack; | |
| 646 | - end else begin | |
| 647 | - Value(y); | |
| 648 | - IndexSlice(AdrOf(x), s, checks, y.pos); | |
| 649 | - x.base := bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; | |
| 650 | - end; | |
| 651 | - x.typ := t^.base; | |
| 652 | -end; | |
| 653 | - | |
| 654 | -procedure SliceLo(var lo: TItem); | |
| 655 | -begin | |
| 656 | - if not IsInt(lo.typ) then MarkAt(lo.pos, 'an index is an integer, found ' + What(lo)); | |
| 657 | - Value(lo); | |
| 658 | -end; | |
| 659 | - | |
| 660 | -procedure Slice(var x, lo, hi: TItem); | |
| 661 | -var t, st: PType; tmp: int64; | |
| 662 | -begin | |
| 663 | - t := x.typ; | |
| 664 | - if not IsInt(hi.typ) then MarkAt(hi.pos, 'an index is an integer, found ' + What(hi)); | |
| 665 | - if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); | |
| 666 | - if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); | |
| 667 | - if t^.form = fArray then NotReadOnly(x); | |
| 668 | - Value(hi); | |
| 669 | - tmp := AllocTemp(16, 8); | |
| 670 | - if t^.form = fPtr then SlicePtr(t^.base^.size, tmp, checks, lo.pos) | |
| 671 | - else gen.Slice(AdrOf(x), t^.form = fArray, t^.len, t^.base^.size, tmp, checks, lo.pos); | |
| 672 | - st := NewType(fSlice, 16, 8); | |
| 673 | - st^.base := t^.base; | |
| 674 | - TempItem(x, st, tmp); | |
| 675 | -end; | |
| 676 | - | |
| 677 | -{ Operators } | |
| 678 | - | |
| 679 | -procedure Neg(var x: TItem); | |
| 680 | -begin | |
| 681 | - if x.typ^.form = fReal then begin | |
| 682 | - if x.mode = mConst then x.val := x.val xor low(int64) | |
| 683 | - else begin Value(x); gen.Neg(KindOf(x.typ), 8, false) end; | |
| 684 | - end else if IsInt(x.typ) then begin | |
| 685 | - if (x.mode = mConst) and (x.typ^.form = fConst) and (x.val = low(int64)) then | |
| 686 | - MarkAt(x.pos, 'the constant is too big for I64') | |
| 687 | - else if x.mode = mConst then x.val := Cut(-x.val, x.typ) | |
| 688 | - else begin Value(x); gen.Neg(kWord, IntSize(x.typ), Signed(x.typ)) end; | |
| 689 | - end else MarkAt(x.pos, 'can''t negate ' + TypeName(x.typ)); | |
| 690 | -end; | |
| 691 | - | |
| 692 | -procedure NotOp(var x: TItem); | |
| 693 | -var L: longint; | |
| 694 | -begin | |
| 695 | - if IsInt(x.typ) then MarkHelp(x.pos, '"not" needs a BOOL, found ' + What(x), 'the bits of an integer flip with ~'); | |
| 696 | - if x.typ^.form <> fBool then MarkAt(x.pos, '"not" needs a BOOL, found ' + What(x)); | |
| 697 | - if x.mode = mConst then x.val := 1 - x.val | |
| 698 | - else if x.mode = mCond then begin | |
| 699 | - x.rel := Negated[x.rel]; | |
| 700 | - L := x.fj; x.fj := x.tj; x.tj := L; | |
| 701 | - end | |
| 702 | - else begin Value(x); NotBool end; | |
| 703 | -end; | |
| 704 | - | |
| 705 | -procedure Complement(var x: TItem); | |
| 706 | -begin | |
| 707 | - if x.typ^.form = fBool then MarkHelp(x.pos, '"~" needs an integer, found ' + What(x), 'a BOOL is negated with not'); | |
| 708 | - if not IsInt(x.typ) then MarkAt(x.pos, '"~" needs an integer, found ' + What(x)); | |
| 709 | - if x.mode = mConst then x.val := Cut(not x.val, x.typ) | |
| 710 | - else begin Value(x); gen.Complement(IntSize(x.typ), Signed(x.typ)) end; | |
| 711 | -end; | |
| 712 | - | |
| 713 | -{ Arrays, slices and strings, which = and # compare element by element } | |
| 714 | -function IsSeq(t: PType): boolean; | |
| 715 | -begin | |
| 716 | - IsSeq := t^.form in [fStr, fArray, fSlice]; | |
| 717 | -end; | |
| 718 | - | |
| 719 | -function ElemOf(t: PType): PType; | |
| 720 | -begin | |
| 721 | - if t^.form = fStr then ElemOf := tU8 else ElemOf := t^.base; | |
| 722 | -end; | |
| 723 | - | |
| 724 | -{ An array, slice or string as a slice in a temporary, for comparing. } | |
| 725 | -procedure SeqSlice(var x: TItem); | |
| 726 | -var t: PType; | |
| 727 | -begin | |
| 728 | - t := NewType(fSlice, 16, 8); | |
| 729 | - t^.base := ElemOf(x.typ); | |
| 730 | - if x.typ^.form = fSlice then begin | |
| 731 | - x.typ := t; CopyToTemp(x) | |
| 732 | - end else begin | |
| 733 | - if x.mode = mStr then begin Address(x); SliceOf(x, t, x.val) end | |
| 734 | - else begin | |
| 735 | - if x.vol then MarkAt(x.pos, 'can''t compare through a volatile pointer'); | |
| 736 | - Address(x); SliceOf(x, t, x.typ^.len) | |
| 737 | - end; | |
| 738 | - end; | |
| 739 | -end; | |
| 740 | - | |
| 741 | -{ x, a BOOL, as a condition: a value compared with 0 if it isn't one } | |
| 742 | -procedure MakeCond(var x: TItem); | |
| 743 | -begin | |
| 744 | - if x.mode = mCond then exit; | |
| 745 | - Value(x); | |
| 746 | - PushConst(kWord, 0); | |
| 747 | - x.mode := mCond; | |
| 748 | - x.rel := sNeq; | |
| 749 | - x.relSigned := false; | |
| 750 | - x.fj := NoLabel; | |
| 751 | - x.tj := NoLabel; | |
| 752 | -end; | |
| 753 | - | |
| 754 | -procedure Op1(op: TSym; var x: TItem; var L: longint); | |
| 755 | -begin | |
| 756 | - L := NoLabel; | |
| 757 | - if (op in [sAnd, sOr]) and (x.typ^.form = fBool) then begin | |
| 758 | - if x.mode = mConst then exit; { folded by Op2 } | |
| 759 | - MakeCond(x); | |
| 760 | - if op = sAnd then begin | |
| 761 | - JumpOut(Negated[x.rel], x.relSigned, x.fj); | |
| 762 | - Fix(x.tj); | |
| 763 | - x.tj := NoLabel; | |
| 764 | - end else begin | |
| 765 | - JumpOut(x.rel, x.relSigned, x.tj); | |
| 766 | - Fix(x.fj); | |
| 767 | - x.fj := NoLabel; | |
| 768 | - end; | |
| 769 | - end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then | |
| 770 | - SeqSlice(x) | |
| 771 | - else if (op = sPlus) and (x.mode = mStr) then | |
| 772 | - { two string constants to be joined: nothing to compute } | |
| 773 | - else if (op = sPlus) and IsSeq(x.typ) then | |
| 774 | - MarkHelp(x.pos, '+ doesn''t join ' + What(x), 'strings are joined in a Buf: Buf.str(b, s)') | |
| 775 | - else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then | |
| 776 | - NotValue(x) | |
| 777 | - else if x.mode <> mConst then | |
| 778 | - Value(x); | |
| 779 | -end; | |
| 780 | - | |
| 781 | -function Log2(v: int64): longint; | |
| 782 | -var n: longint; | |
| 783 | -begin | |
| 784 | - n := 0; | |
| 785 | - while (v > 1) and (v and 1 = 0) do begin v := v shr 1; inc(n) end; | |
| 786 | - if v <> 1 then n := -1; | |
| 787 | - Log2 := n; | |
| 788 | -end; | |
| 789 | - | |
| 790 | -{ Whether a op b, for + - *, overflows an I64: an untyped constant | |
| 791 | - doesn't wrap, it is an error (report section 4). } | |
| 792 | -function Overflows(op: TSym; a, b: int64): boolean; | |
| 793 | -begin | |
| 794 | - case op of | |
| 795 | - sPlus: Overflows := ((b > 0) and (a > high(int64) - b)) or ((b < 0) and (a < low(int64) - b)); | |
| 796 | - sMinus: Overflows := ((b < 0) and (a > high(int64) + b)) or ((b > 0) and (a < low(int64) + b)); | |
| 797 | - sTimes: | |
| 798 | - if (a = 0) or (b = 0) then Overflows := false | |
| 799 | - else if a = -1 then Overflows := b = low(int64) | |
| 800 | - else if b = -1 then Overflows := a = low(int64) | |
| 801 | - else Overflows := (a * b) div b <> a; | |
| 802 | - else Overflows := false; | |
| 803 | - end; | |
| 804 | -end; | |
| 805 | - | |
| 806 | -{ A type's width in bits; an untyped constant's is an I64's } | |
| 807 | -function Bits(t: PType): longint; | |
| 808 | -begin | |
| 809 | - Bits := t^.size * 8; | |
| 810 | -end; | |
| 811 | - | |
| 812 | -function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64; | |
| 813 | -var u: boolean; | |
| 814 | -begin | |
| 815 | - u := (t^.form = fInt) and not t^.signed; | |
| 816 | - if (t^.form = fConst) and Overflows(op, a, b) then MarkAt(p, 'the constant is too big for I64'); | |
| 817 | - case op of | |
| 818 | - sPlus: FoldInt := a + b; | |
| 819 | - sMinus: FoldInt := a - b; | |
| 820 | - sTimes: FoldInt := a * b; | |
| 821 | - sDiv, sMod: begin | |
| 822 | - if b = 0 then MarkAt(p, 'division by zero'); | |
| 823 | - if u then begin | |
| 824 | - if op = sDiv then FoldInt := int64(qword(a) div qword(b)) | |
| 825 | - else FoldInt := int64(qword(a) mod qword(b)); | |
| 826 | - end else if (b = -1) and (a = int64(-1) shl (Bits(t) - 1)) then MarkAt(p, 'division overflows') | |
| 827 | - else if op = sDiv then FoldInt := a div b | |
| 828 | - else FoldInt := a mod b; | |
| 829 | - end; | |
| 830 | - sAmp: FoldInt := a and b; | |
| 831 | - sBar: FoldInt := a or b; | |
| 832 | - sXor: FoldInt := a xor b; | |
| 833 | - sShl: FoldInt := a shl (b and (Bits(t) - 1)); | |
| 834 | - sShr: if u then FoldInt := int64(qword(a) shr (b and (Bits(t) - 1))) | |
| 835 | - else FoldInt := SarInt64(a, b and (Bits(t) - 1)); | |
| 836 | - sEql: FoldInt := ord(a = b); | |
| 837 | - sNeq: FoldInt := ord(a <> b); | |
| 838 | - sLss: if u then FoldInt := ord(qword(a) < qword(b)) else FoldInt := ord(a < b); | |
| 839 | - sLeq: if u then FoldInt := ord(qword(a) <= qword(b)) else FoldInt := ord(a <= b); | |
| 840 | - sGtr: if u then FoldInt := ord(qword(a) > qword(b)) else FoldInt := ord(a > b); | |
| 841 | - sGeq: if u then FoldInt := ord(qword(a) >= qword(b)) else FoldInt := ord(a >= b); | |
| 842 | - else MarkAt(p, 'bad operator'); FoldInt := 0; | |
| 843 | - end; | |
| 844 | -end; | |
| 845 | - | |
| 846 | -{ Folds x op y, of the real type t: an F32 rounded at each step, as | |
| 847 | - the machine would. } | |
| 848 | -function FoldReal(op: TSym; const x, y: TItem; t: PType; var r: TItem): boolean; | |
| 849 | -var a, b: double; | |
| 850 | -begin | |
| 851 | - a := RealOf(x); b := RealOf(y); | |
| 852 | - FoldReal := true; | |
| 853 | - case op of | |
| 854 | - sPlus: MakeReal(r, a + b); | |
| 855 | - sMinus: MakeReal(r, a - b); | |
| 856 | - sTimes: MakeReal(r, a * b); | |
| 857 | - sSlash: MakeReal(r, a / b); | |
| 858 | - sEql: MakeConst(r, tBool, ord(a = b)); | |
| 859 | - sNeq: MakeConst(r, tBool, ord(a <> b)); | |
| 860 | - sLss: MakeConst(r, tBool, ord(a < b)); | |
| 861 | - sLeq: MakeConst(r, tBool, ord(a <= b)); | |
| 862 | - sGtr: MakeConst(r, tBool, ord(a > b)); | |
| 863 | - sGeq: MakeConst(r, tBool, ord(a >= b)); | |
| 864 | - else FoldReal := false; | |
| 865 | - end; | |
| 866 | - if r.typ^.form = fReal then RealTo(r, t); | |
| 867 | -end; | |
| 868 | - | |
| 869 | -{ and, or and not are for BOOLs; &, |, xor and ~ for the bits of | |
| 870 | - integers: x is the wrong kind for op, and the message names the | |
| 871 | - operator it may have meant } | |
| 872 | -procedure WrongLogic(op: TSym; var x: TItem); | |
| 873 | -const Bits = 'the bits of integers take '; | |
| 874 | -begin | |
| 875 | - case op of | |
| 876 | - sAnd: if IsInt(x.typ) then MarkHelp(x.pos, '"and" needs BOOLs, found ' + What(x), Bits + '&'); | |
| 877 | - sOr: if IsInt(x.typ) then MarkHelp(x.pos, '"or" needs BOOLs, found ' + What(x), Bits + '|'); | |
| 878 | - sAmp: MarkHelp(x.pos, '"&" needs integers, found ' + What(x), 'BOOLs take and'); | |
| 879 | - sBar: MarkHelp(x.pos, '"|" needs integers, found ' + What(x), 'BOOLs take or'); | |
| 880 | - sXor: MarkHelp(x.pos, '"xor" needs integers, found ' + What(x), 'BOOLs that differ are #'); | |
| 881 | - end; | |
| 882 | - MarkAt(x.pos, SymText(op) + ' needs BOOLs, found ' + What(x)); | |
| 883 | -end; | |
| 884 | - | |
| 885 | -{ x + y, of two string constants: one string, in a chunk of its own | |
| 886 | - (report section 4) } | |
| 887 | -procedure JoinStr(var x, y: TItem); | |
| 888 | -var p: PBytes; c: longint; | |
| 889 | -begin | |
| 890 | - if y.mode <> mStr then MarkAt(y.pos, '"+" joins two strings, found ' + What(y)); | |
| 891 | - p := GetMem(x.val + y.val + 1); | |
| 892 | - Move(rodata^[chunks^[x.chunk].start], p^[0], x.val); | |
| 893 | - Move(rodata^[chunks^[y.chunk].start], p^[x.val], y.val + 1); | |
| 894 | - c := NewChunk(secRodata, -1, -1); | |
| 895 | - SetData(c, p, x.val + y.val + 1, 1); | |
| 896 | - FreeMem(p); | |
| 897 | - x.chunk := c; | |
| 898 | - x.val := x.val + y.val; | |
| 899 | -end; | |
| 900 | - | |
| 901 | -{ A set's operations, as operations on its bits: the two values on top } | |
| 902 | -procedure SetOp(op: TSym); | |
| 903 | -begin | |
| 904 | - case op of | |
| 905 | - sPlus: gen.Op(sBar, kWord, 8, false); | |
| 906 | - sTimes: gen.Op(sAmp, kWord, 8, false); | |
| 907 | - sMinus: begin gen.Complement(8, false); gen.Op(sAmp, kWord, 8, false) end; | |
| 908 | - sLeq, sGeq: begin | |
| 909 | - { x <= y has no bit of x outside y } | |
| 910 | - if op = sGeq then Swap; | |
| 911 | - gen.Complement(8, false); | |
| 912 | - gen.Op(sAmp, kWord, 8, false); | |
| 913 | - PushConst(kWord, 0); | |
| 914 | - Compare(sEql, kWord, false); | |
| 915 | - end; | |
| 916 | - sIn: begin | |
| 917 | - Swap; | |
| 918 | - gen.Op(sShr, kWord, 8, false); | |
| 919 | - PushConst(kWord, 1); | |
| 920 | - gen.Op(sAmp, kWord, 8, false); | |
| 921 | - end; | |
| 922 | - else Compare(op, kWord, false); | |
| 923 | - end; | |
| 924 | -end; | |
| 925 | - | |
| 926 | -procedure Op2(op: TSym; var x, y: TItem; L: longint); | |
| 927 | -const | |
| 928 | - Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq]; | |
| 929 | -var | |
| 930 | - xt, yt, t, e: PType; r: TItem; scale: longint; isPtr: boolean; a, b: int64; | |
| 931 | -begin | |
| 932 | - xt := x.typ; yt := y.typ; | |
| 933 | - if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y); | |
| 934 | - if (op = sPlus) and (x.mode = mStr) then begin JoinStr(x, y); exit end; | |
| 935 | - | |
| 936 | - if op in [sAnd, sOr] then begin | |
| 937 | - if xt^.form <> fBool then WrongLogic(op, x); | |
| 938 | - if yt^.form <> fBool then WrongLogic(op, y); | |
| 939 | - if (x.mode = mConst) and (y.mode = mConst) then begin | |
| 940 | - if op = sAnd then x.val := x.val and y.val else x.val := x.val or y.val; | |
| 941 | - end else if x.mode = mConst then begin | |
| 942 | - { true and y, false or y: y; else x, y computed and dropped } | |
| 943 | - if (x.val = 1) = (op = sAnd) then x := y | |
| 944 | - else Discard(y); | |
| 945 | - end else begin | |
| 946 | - MakeCond(y); | |
| 947 | - if op = sAnd then begin x.fj := JoinJumps(x.fj, y.fj); x.tj := y.tj end | |
| 948 | - else begin x.tj := JoinJumps(x.tj, y.tj); x.fj := y.fj end; | |
| 949 | - x.rel := y.rel; | |
| 950 | - x.relSigned := y.relSigned; | |
| 951 | - end; | |
| 952 | - exit; | |
| 953 | - end; | |
| 954 | - | |
| 955 | - if (op in [sAmp, sBar, sXor]) and (xt^.form = fBool) then WrongLogic(op, x); | |
| 956 | - if (op in [sAmp, sBar, sXor]) and (yt^.form = fBool) then WrongLogic(op, y); | |
| 957 | - | |
| 958 | - { the lengths, then the bytes, LEN * SIZE(T) of them: the elements | |
| 959 | - are of types whose bytes say all there is to their values } | |
| 960 | - if (op in [sEql, sNeq]) and IsSeq(xt) then begin | |
| 961 | - e := ElemOf(xt); | |
| 962 | - if not IsSeq(yt) then | |
| 963 | - MarkAt(Join(x.pos, y.pos), 'can''t compare an array with ' + TypeName(yt)) | |
| 964 | - else if not Same(e, ElemOf(yt)) then | |
| 965 | - MarkAt(Join(x.pos, y.pos), 'can''t compare an array of ' + TypeName(e) + ' with one of ' + TypeName(ElemOf(yt))); | |
| 966 | - if e^.form = fReal then | |
| 967 | - MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of reals with ' + SymText(op), | |
| 968 | - '-0.0 = 0.0 and a NaN equals nothing, though their bytes say otherwise: write a loop') | |
| 969 | - else if not (e^.form in [fInt, fBool, fEnum, fSet, fPtr]) then | |
| 970 | - MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of ' + TypeName(e) + ' with ' + SymText(op), 'write a loop'); | |
| 971 | - SeqSlice(y); | |
| 972 | - CompareSeq(x.off, y.off, Log2(e^.size), op = sEql); | |
| 973 | - MakeConst(x, tBool, 0); | |
| 974 | - x.mode := mStack; | |
| 975 | - exit; | |
| 976 | - end; | |
| 977 | - | |
| 978 | - { the type of the result, and the checks } | |
| 979 | - t := nil; scale := 1; isPtr := false; | |
| 980 | - if IsInt(xt) and IsInt(yt) then begin | |
| 981 | - if op = sSlash then MarkHelp(Join(x.pos, y.pos), '"/" divides reals, not integers', 'to divide integers, write ' + Source(x.pos) + ' div ' + Source(y.pos)); | |
| 982 | - if op in [sShl, sShr] then begin | |
| 983 | - if xt^.form = fConst then t := tConst else t := xt | |
| 984 | - end else if (xt^.form = fConst) and (yt^.form = fConst) then t := tConst | |
| 985 | - else if xt^.form = fConst then begin | |
| 986 | - if (x.mode = mConst) and not Fits(x.val, yt) then MarkAt(x.pos, IntStr(x.val) + ' doesn''t fit ' + TypeName(yt)); | |
| 987 | - t := yt | |
| 988 | - end else if yt^.form = fConst then begin | |
| 989 | - if (y.mode = mConst) and not Fits(y.val, xt) then MarkAt(y.pos, IntStr(y.val) + ' doesn''t fit ' + TypeName(xt)); | |
| 990 | - t := xt | |
| 991 | - end else if Same(xt, yt) then t := xt | |
| 992 | - else Mismatch(x, y, xt, yt); | |
| 993 | - if not (op in Rel + [sPlus, sMinus, sTimes, sDiv, sMod, sAmp, sBar, sXor, sShl, sShr]) then | |
| 994 | - MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(t)); | |
| 995 | - end else if (xt^.form = fReal) and (yt^.form = fReal) then begin | |
| 996 | - if not (op in Rel + [sPlus, sMinus, sTimes, sSlash]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 997 | - if xt = tRConst then t := yt | |
| 998 | - else if (yt = tRConst) or Same(xt, yt) then t := xt | |
| 999 | - else Mismatch(x, y, xt, yt); | |
| 1000 | - if (x.mode = mConst) and (x.typ <> t) then RealTo(x, t); | |
| 1001 | - if (y.mode = mConst) and (y.typ <> t) then RealTo(y, t); | |
| 1002 | - end else if (xt^.form = fBool) and (yt^.form = fBool) then begin | |
| 1003 | - if not (op in [sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1004 | - t := tBool; | |
| 1005 | - end else if (xt^.form = fPtr) and IsInt(yt) and (op in [sPlus, sMinus]) then begin | |
| 1006 | - t := xt; scale := xt^.base^.size; isPtr := true; | |
| 1007 | - if y.mode = mConst then y.typ := tInt; | |
| 1008 | - end else if (xt^.form = fPtr) and (yt^.form = fPtr) and (op = sMinus) then begin | |
| 1009 | - if not Same(xt, yt) then Mismatch(x, y, xt, yt); | |
| 1010 | - t := tInt; scale := xt^.base^.size; isPtr := true; | |
| 1011 | - end else if (xt^.form in [fPtr, fProc, fNil, fError]) and (yt^.form in [fPtr, fProc, fNil, fError]) then begin | |
| 1012 | - if not (op in Rel) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1013 | - if (xt^.form in [fProc, fError]) or (yt^.form in [fProc, fError]) then | |
| 1014 | - if not (op in [sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1015 | - if (xt^.form <> fNil) and (yt^.form <> fNil) and not Same(xt, yt) then | |
| 1016 | - if not ((xt^.form = fPtr) and (yt^.form = fPtr) and Same(xt^.base, yt^.base)) then | |
| 1017 | - Mismatch(x, y, xt, yt); | |
| 1018 | - t := tU64; | |
| 1019 | - end else if (xt^.form = fEnum) and Same(xt, yt) then begin | |
| 1020 | - if not (op in Rel) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1021 | - t := xt; | |
| 1022 | - end else if (xt^.form = fSet) and Same(xt, yt) then begin | |
| 1023 | - if not (op in [sPlus, sTimes, sMinus, sEql, sNeq, sLeq, sGeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1024 | - t := xt; | |
| 1025 | - end else if (op = sIn) and (yt^.form = fSet) and Same(xt, yt^.base) then | |
| 1026 | - t := yt | |
| 1027 | - else | |
| 1028 | - Mismatch(x, y, xt, yt); | |
| 1029 | - | |
| 1030 | - { constants } | |
| 1031 | - if (x.mode = mConst) and (y.mode = mConst) and not isPtr then begin | |
| 1032 | - if t^.form = fReal then begin | |
| 1033 | - if not FoldReal(op, x, y, t, r) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1034 | - x := r; | |
| 1035 | - end else if t^.form = fSet then begin | |
| 1036 | - a := x.val; b := y.val; | |
| 1037 | - case op of | |
| 1038 | - sPlus: MakeConst(x, t, a or b); | |
| 1039 | - sTimes: MakeConst(x, t, a and b); | |
| 1040 | - sMinus: MakeConst(x, t, a and not b); | |
| 1041 | - sIn: MakeConst(x, tBool, (b shr a) and 1); | |
| 1042 | - sLeq: MakeConst(x, tBool, ord(a and not b = 0)); | |
| 1043 | - sGeq: MakeConst(x, tBool, ord(b and not a = 0)); | |
| 1044 | - else MakeConst(x, tBool, ord((a = b) = (op = sEql))); | |
| 1045 | - end; | |
| 1046 | - end else if op in Rel then | |
| 1047 | - MakeConst(x, tBool, FoldInt(op, x.val, y.val, t, Join(x.pos, y.pos))) | |
| 1048 | - else | |
| 1049 | - MakeConst(x, t, Cut(FoldInt(op, x.val, y.val, t, Join(x.pos, y.pos)), t)); | |
| 1050 | - exit; | |
| 1051 | - end; | |
| 1052 | - | |
| 1053 | - { x and y on the stack, x under y; a constant x goes on after y, and | |
| 1054 | - is left on top where the order doesn't matter, as gen takes a | |
| 1055 | - constant best on the right: 0.25 * e is mulsd xmm8, [0.25] } | |
| 1056 | - Value(y); | |
| 1057 | - if x.mode = mConst then begin | |
| 1058 | - Value(x); | |
| 1059 | - if not (op in [sPlus, sTimes, sAmp, sBar, sXor]) or isPtr then Swap; | |
| 1060 | - end; | |
| 1061 | - | |
| 1062 | - if t^.form = fReal then begin | |
| 1063 | - if op in Rel then Compare(op, KindOf(t), false) else gen.Op(op, KindOf(t), 8, false) | |
| 1064 | - end else if t^.form = fSet then | |
| 1065 | - SetOp(op) | |
| 1066 | - else if op in Rel then begin | |
| 1067 | - x.mode := mCond; | |
| 1068 | - x.rel := op; | |
| 1069 | - x.relSigned := Signed(t); | |
| 1070 | - x.fj := NoLabel; | |
| 1071 | - x.tj := NoLabel; | |
| 1072 | - x.typ := tBool; | |
| 1073 | - exit; | |
| 1074 | - end | |
| 1075 | - else if isPtr then begin | |
| 1076 | - if t <> tInt then ScaleBy(scale); | |
| 1077 | - gen.Op(op, kWord, 8, false); | |
| 1078 | - if (t = tInt) and (scale <> 1) then begin | |
| 1079 | - PushConst(kWord, scale); | |
| 1080 | - gen.Op(sDiv, kWord, 8, true); | |
| 1081 | - end; | |
| 1082 | - end else | |
| 1083 | - gen.Op(op, KindOf(t), IntSize(t), Signed(t)); | |
| 1084 | - x.mode := mStack; | |
| 1085 | - if op in Rel + [sIn] then x.typ := tBool else x.typ := t; | |
| 1086 | -end; | |
| 1087 | - | |
| 1088 | -{ INT and UINT, the integers as big as an address, through which | |
| 1089 | - integers and pointers convert (report section 6.7) } | |
| 1090 | -function IsWord(t: PType): boolean; | |
| 1091 | -begin | |
| 1092 | - IsWord := Same(t, tInt) or Same(t, tUInt); | |
| 1093 | -end; | |
| 1094 | - | |
| 1095 | -procedure Convert(var x: TItem; t: PType); | |
| 1096 | -var s: PType; r: double; | |
| 1097 | -begin | |
| 1098 | - s := x.typ; | |
| 1099 | - if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then begin | |
| 1100 | - if x.mode = mStr then MarkAt(x.pos, 'can''t convert a string'); | |
| 1101 | - NotValue(x); | |
| 1102 | - end; | |
| 1103 | - if Same(s, t) then exit; | |
| 1104 | - if (IsInt(s) or (s^.form in [fEnum, fSet])) and (t^.form = fInt) then begin | |
| 1105 | - if x.mode = mConst then x.val := Cut(x.val, t) | |
| 1106 | - else begin Value(x); Narrow(t^.size, t^.signed) end; | |
| 1107 | - end else if IsInt(s) and (t^.form = fEnum) then begin | |
| 1108 | - if x.mode = mConst then begin | |
| 1109 | - if (x.val < 0) or (x.val >= t^.len) then MarkAt(x.pos, IntStr(x.val) + ' isn''t a position of ' + TypeName(t)) | |
| 1110 | - end else begin Value(x); CheckEnum(t^.len, NameChunk(t), x.pos) end; | |
| 1111 | - end else if IsInt(s) and (t^.form = fSet) then begin | |
| 1112 | - if x.mode = mConst then begin | |
| 1113 | - if t^.len < 64 then x.val := x.val and ((int64(1) shl t^.len) - 1) | |
| 1114 | - end else begin | |
| 1115 | - Value(x); | |
| 1116 | - if t^.len < 64 then begin | |
| 1117 | - PushConst(kWord, (int64(1) shl t^.len) - 1); | |
| 1118 | - gen.Op(sAmp, kWord, 8, false); | |
| 1119 | - end; | |
| 1120 | - end; | |
| 1121 | - end else if IsInt(s) and (t^.form = fReal) then begin | |
| 1122 | - if x.mode = mConst then begin | |
| 1123 | - if (s^.form = fInt) and not s^.signed then r := qword(x.val) else r := x.val; | |
| 1124 | - MakeReal(x, r); | |
| 1125 | - RealTo(x, t); | |
| 1126 | - end else begin | |
| 1127 | - Value(x); | |
| 1128 | - IntToReal(KindOf(t), (s^.form = fInt) and not s^.signed and (s^.size = 8)); | |
| 1129 | - end; | |
| 1130 | - end else if (s^.form = fReal) and (t^.form = fReal) then begin | |
| 1131 | - { F32 to F64, F64 to F32, or a real constant to either } | |
| 1132 | - if x.mode = mConst then RealTo(x, t) | |
| 1133 | - else begin Value(x); RealToReal(KindOf(s)) end; | |
| 1134 | - end else if (s^.form = fReal) and (t^.form = fInt) then begin | |
| 1135 | - if x.mode = mConst then begin | |
| 1136 | - r := RealOf(x); | |
| 1137 | - { NaN or out of range folds to low(int64), the "integer | |
| 1138 | - indefinite" cvttsd2si gives at run time } | |
| 1139 | - if (r <> r) or (r >= 9.2233720368547758e18) or (r < -9.2233720368547758e18) then | |
| 1140 | - x.val := Cut(low(int64), t) | |
| 1141 | - else x.val := Cut(trunc(r), t); | |
| 1142 | - end else begin | |
| 1143 | - Value(x); RealToInt(KindOf(s)); Narrow(t^.size, t^.signed) | |
| 1144 | - end; | |
| 1145 | - end else if (IsWord(s) or (s^.form in [fConst, fPtr, fProc, fNil])) and (t^.form in [fPtr, fProc]) then begin | |
| 1146 | - if x.mode <> mConst then Value(x); | |
| 1147 | - end else if (s^.form in [fPtr, fProc]) and IsWord(t) then | |
| 1148 | - Value(x) | |
| 1149 | - else | |
| 1150 | - MarkAt(x.pos, 'can''t convert ' + TypeName(s) + ' to ' + TypeName(t)); | |
| 1151 | - x.typ := t; | |
| 1152 | -end; | |
| 1153 | - | |
| 1154 | -{ Calls } | |
| 1155 | - | |
| 1156 | -procedure BeginCall(var c: TCall; var x: TItem); | |
| 1157 | -begin | |
| 1158 | - if x.mode = mProc then begin | |
| 1159 | - c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0; | |
| 1160 | - c.obj := x.obj; c.ext := x.obj^.ext; | |
| 1161 | - end else begin | |
| 1162 | - c.obj := nil; c.ext := false; | |
| 1163 | - if x.typ^.form <> fProc then MarkAt(x.pos, 'expected a procedure, found ' + What(x)); | |
| 1164 | - c.sig := x.typ; c.chunk := -1; | |
| 1165 | - c.target := ToSlot(x); | |
| 1166 | - end; | |
| 1167 | - c.param := c.sig^.fields; | |
| 1168 | - c.n := 0; | |
| 1169 | -end; | |
| 1170 | - | |
| 1171 | -{ Too many arguments or too few, at p } | |
| 1172 | -procedure WrongCount(var c: TCall; const p: TPos; const what: shortstring); | |
| 1173 | -begin | |
| 1174 | - if c.obj = nil then MarkAt(p, what); | |
| 1175 | - Error(p, what + ': ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); | |
| 1176 | - if c.obj^.module <> nil then Note(c.obj^.pos, 'the signature is here'); | |
| 1177 | - Fail; | |
| 1178 | -end; | |
| 1179 | - | |
| 1180 | -{ Pushes the two words of x, a slice or two results (InTwoRegs), read | |
| 1181 | - from memory now, so that what comes after can't change them. } | |
| 1182 | -procedure PushWords(var x: TItem); | |
| 1183 | -var adr: TAdr; f: PObj; | |
| 1184 | -begin | |
| 1185 | - if x.mode = mWords then exit; { there already } | |
| 1186 | - { copied to a temporary unless its two words are in memory at a place | |
| 1187 | - that a later argument can't change } | |
| 1188 | - if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot, bReg]) then CopyToTemp(x); | |
| 1189 | - adr := AdrOf(x); | |
| 1190 | - if x.typ^.form = fSlice then begin | |
| 1191 | - PushVar(adr, 8, false, kWord); | |
| 1192 | - inc(adr.off, 8); | |
| 1193 | - PushVar(adr, 8, false, kWord); | |
| 1194 | - end else begin | |
| 1195 | - f := x.typ^.fields; | |
| 1196 | - while f <> nil do begin | |
| 1197 | - adr.off := AdrOf(x).off + f^.val; | |
| 1198 | - PushVar(adr, ScalarSize(f^.typ), Signed(f^.typ), KindOf(f^.typ)); | |
| 1199 | - f := f^.next; | |
| 1200 | - end; | |
| 1201 | - end; | |
| 1202 | -end; | |
| 1203 | - | |
| 1204 | -procedure PassArg(var c: TCall; var a: TItem); | |
| 1205 | -var p: PObj; | |
| 1206 | -begin | |
| 1207 | - p := c.param; | |
| 1208 | - if c.n >= High(c.isF) then MarkAt(a.pos, 'too many arguments'); | |
| 1209 | - if p = nil then begin | |
| 1210 | - { past the parameters of a C procedure that ends in ...: a string | |
| 1211 | - goes as a C string, an integer constant as an INT, a real | |
| 1212 | - constant as an F64; C takes no F32 there } | |
| 1213 | - if not c.sig^.variadic then WrongCount(c, a.pos, 'too many arguments'); | |
| 1214 | - if a.mode = mStr then Coerce(a, tPtrU8) | |
| 1215 | - else if a.typ^.form = fConst then Coerce(a, tInt) | |
| 1216 | - else if a.typ = tRConst then Coerce(a, tReal) | |
| 1217 | - else if IsF32(a.typ) then MarkAt(a.pos, 'an F32 goes to ... as F64(x)') | |
| 1218 | - else if not IsScalar(a.typ) then MarkAt(a.pos, 'only a single value goes to ...'); | |
| 1219 | - c.isF[c.n] := a.typ^.form = fReal; | |
| 1220 | - c.pair[c.n] := false; | |
| 1221 | - Value(a); | |
| 1222 | - inc(c.n); | |
| 1223 | - exit; | |
| 1224 | - end; | |
| 1225 | - c.isF[c.n] := (p^.typ^.form = fReal) and not p^.isVar; | |
| 1226 | - passing := p; | |
| 1227 | - passingAt := a.pos; | |
| 1228 | - if p^.isVar then begin | |
| 1229 | - NotReadOnly(a); | |
| 1230 | - if (a.mode <> mVar) or not a.isVar then MarkAt(a.pos, 'a var parameter needs a variable'); | |
| 1231 | - if a.odd then MarkAt(a.pos, 'a field of a packed record can''t be a var parameter'); | |
| 1232 | - if not Same(a.typ, p^.typ) then Mismatch(a, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a)); | |
| 1233 | - if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); | |
| 1234 | - Address(a); | |
| 1235 | - end else if p^.typ^.form = fSlice then begin | |
| 1236 | - Coerce(a, p^.typ); | |
| 1237 | - PushWords(a); | |
| 1238 | - c.pair[c.n] := true; | |
| 1239 | - inc(c.n); | |
| 1240 | - c.isF[c.n] := false; | |
| 1241 | - end else if IsAggregate(p^.typ) then begin | |
| 1242 | - Coerce(a, p^.typ); | |
| 1243 | - if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a); | |
| 1244 | - Address(a); | |
| 1245 | - end else begin | |
| 1246 | - Coerce(a, p^.typ); | |
| 1247 | - Value(a); | |
| 1248 | - end; | |
| 1249 | - c.pair[c.n] := false; | |
| 1250 | - inc(c.n); | |
| 1251 | - c.param := p^.next; | |
| 1252 | -end; | |
| 1253 | - | |
| 1254 | -{ The call c made, with x its result as an item: none; a temporary, for a | |
| 1255 | - slice or a record; two results on gen's stack (mWords); else one value | |
| 1256 | - on the stack. } | |
| 1257 | -procedure EndCall(var c: TCall; var x: TItem); | |
| 1258 | -var res: PType; | |
| 1259 | -begin | |
| 1260 | - if c.param <> nil then WrongCount(c, TokPos, 'too few arguments'); | |
| 1261 | - Call(c); | |
| 1262 | - res := c.sig^.base; | |
| 1263 | - FillChar(x, SizeOf(x), 0); | |
| 1264 | - if res = nil then begin | |
| 1265 | - x.mode := mNone; x.typ := tNone; x.obj := c.obj | |
| 1266 | - end else if c.ret <> 0 then | |
| 1267 | - TempItem(x, res, c.ret) | |
| 1268 | - else begin | |
| 1269 | - x.mode := mStack; x.typ := res; | |
| 1270 | - if res^.tuple then x.mode := mWords; | |
| 1271 | - end; | |
| 1272 | -end; | |
| 1273 | - | |
| 1274 | -{ Variants } | |
| 1275 | - | |
| 1276 | -{ A temporary of type t, all zero: for variants and record literals. } | |
| 1277 | -procedure ZeroTemp(var x: TItem; t: PType); | |
| 1278 | -var tmp: int64; | |
| 1279 | -begin | |
| 1280 | - tmp := AllocTemp(t^.size, t^.align); | |
| 1281 | - ZeroVar(FrameAdr(tmp), t^.size); | |
| 1282 | - TempItem(x, t, tmp); | |
| 1283 | -end; | |
| 1284 | - | |
| 1285 | -procedure Construct(var x: TItem; c: PObj); | |
| 1286 | -begin | |
| 1287 | - ZeroTemp(x, c^.typ); | |
| 1288 | - PushConst(kWord, c^.val); | |
| 1289 | - PopVar(FrameAdr(x.off), 8); | |
| 1290 | -end; | |
| 1291 | - | |
| 1292 | -procedure SetField(const x: TItem; f: PObj; var a: TItem); | |
| 1293 | -begin | |
| 1294 | - Coerce(a, f^.typ); | |
| 1295 | - if IsScalar(f^.typ) then begin | |
| 1296 | - Value(a); | |
| 1297 | - PopVar(FrameAdr(x.off + f^.val), ScalarSize(f^.typ)); | |
| 1298 | - end else begin | |
| 1299 | - if a.vol then MarkAt(a.pos, 'can''t copy through a volatile pointer'); | |
| 1300 | - Address(a); | |
| 1301 | - CopyTo(FrameAdr(x.off + f^.val), f^.typ^.size); | |
| 1302 | - end; | |
| 1303 | -end; | |
| 1304 | - | |
| 1305 | -{ The slot match tests: a variant's address, or an enumeration's or an | |
| 1306 | - integer's value. } | |
| 1307 | -function MatchBegin(var x: TItem): int64; | |
| 1308 | -var p: int64; | |
| 1309 | -begin | |
| 1310 | - if x.typ^.form in [fEnum, fInt] then exit(ToSlot(x)); | |
| 1311 | - if (x.mode <> mVar) or (x.typ^.form <> fVariant) then MarkAt(x.pos, 'match needs a variant, an enumeration or an integer, found ' + What(x)); | |
| 1312 | - if x.ro then CopyToTemp(x); { its arms' names stand for a copy's fields } | |
| 1313 | - Address(x); | |
| 1314 | - p := AllocTemp(8, 8); | |
| 1315 | - PopVar(FrameAdr(p), 8); | |
| 1316 | - MatchBegin := p; | |
| 1317 | -end; | |
| 1318 | - | |
| 1319 | -{ To L unless the tag of the variant whose address is in slot p is tag } | |
| 1320 | -procedure MatchTag(p: int64; tag: int64; var L: longint); | |
| 1321 | -var a: TAdr; | |
| 1322 | -begin | |
| 1323 | - PushVar(FrameAdr(p), 8, false, kWord); | |
| 1324 | - a.base := bStack; a.chunk := -1; a.off := 0; | |
| 1325 | - PushVar(a, 8, false, kWord); | |
| 1326 | - PushConst(kWord, tag); | |
| 1327 | - JumpCmp(sNeq, false, L); | |
| 1328 | -end; | |
| 1329 | - | |
| 1330 | -{ To body if the integer in slot p is from lo to hi: less its lo, it is | |
| 1331 | - then at most hi - lo, compared unsigned, which holds for a signed type | |
| 1332 | - or not, with no second jump. } | |
| 1333 | -procedure MatchRange(p, lo, hi: int64; var body: longint); | |
| 1334 | -begin | |
| 1335 | - PushVar(FrameAdr(p), 8, false, kWord); | |
| 1336 | - if lo = hi then begin | |
| 1337 | - PushConst(kWord, lo); | |
| 1338 | - JumpCmp(sEql, false, body); | |
| 1339 | - exit; | |
| 1340 | - end; | |
| 1341 | - if lo <> 0 then begin | |
| 1342 | - PushConst(kWord, lo); | |
| 1343 | - gen.Op(sMinus, kWord, 8, false); | |
| 1344 | - end; | |
| 1345 | - PushConst(kWord, hi - lo); | |
| 1346 | - JumpCmp(sLeq, false, body); | |
| 1347 | -end; | |
| 1348 | - | |
| 1349 | -{ A for loop's variable o, stepped by step, wrapping as its type does } | |
| 1350 | -procedure ForStep(o: PObj; step: int64); | |
| 1351 | -var v, w: TItem; | |
| 1352 | -begin | |
| 1353 | - MakeItem(v, o); | |
| 1354 | - if v.base = bReg then begin | |
| 1355 | - { a variable in a register is stepped in it } | |
| 1356 | - PushConst(kWord, step); | |
| 1357 | - IncDecReg(v.chunk, IntSize(o^.typ), Signed(o^.typ), false); | |
| 1358 | - exit; | |
| 1359 | - end; | |
| 1360 | - MakeItem(w, o); | |
| 1361 | - Value(w); | |
| 1362 | - PushConst(kWord, step); | |
| 1363 | - gen.Op(sPlus, kWord, IntSize(o^.typ), Signed(o^.typ)); | |
| 1364 | - Assign(v, w); | |
| 1365 | -end; | |
| 1366 | - | |
| 1367 | -{ For a for loop: pushes the variable o and the limit lim, a constant or | |
| 1368 | - the slot it was put in, and jumps to L if the one is op the other, as | |
| 1369 | - the type of o compares } | |
| 1370 | -procedure ForCmp(o: PObj; const lim: TItem; op: TSym; var L: longint); | |
| 1371 | -var v, b: TItem; | |
| 1372 | -begin | |
| 1373 | - MakeItem(v, o); | |
| 1374 | - Value(v); | |
| 1375 | - b := lim; | |
| 1376 | - Value(b); | |
| 1377 | - JumpCmp(op, o^.typ^.signed, L); | |
| 1378 | -end; | |
| 1379 | - | |
| 1380 | -{ The end of a round of a for loop, for a positive step s, as the report | |
| 1381 | - has it: if v <= b and b - v < s, the loop ends; otherwise v becomes | |
| 1382 | - v + s and the loop goes on while v <= b. Counting the distance left, | |
| 1383 | - rather than comparing after the step, can't be fooled by v wrapping | |
| 1384 | - around. A negative step is the same the other way round: Before, Past | |
| 1385 | - and Within are indexed by up, whether the step is positive. | |
| 1386 | - For a step of 1: v < b goes on to top, where v is stepped; v = b | |
| 1387 | - ends; otherwise the body moved v past b. | |
| 1388 | - For another step: v past b goes to past; else b - v (unsigned, as | |
| 1389 | - v <= b) >= |step| goes on to top, and else the loop ends. | |
| 1390 | - Past: v is stepped here and tested, going to body if within b. } | |
| 1391 | -procedure ForNext(o: PObj; const lim: TItem; step: int64; var exitL: longint; top, body: longint); | |
| 1392 | -const | |
| 1393 | - Before: array[boolean] of TSym = (sGtr, sLss); { by a negative step, by a positive } | |
| 1394 | - Past: array[boolean] of TSym = (sLss, sGtr); | |
| 1395 | - Within: array[boolean] of TSym = (sGeq, sLeq); | |
| 1396 | -var v, b: TItem; L, pastL: longint; up: boolean; | |
| 1397 | -begin | |
| 1398 | - up := step > 0; | |
| 1399 | - L := NoLabel; | |
| 1400 | - pastL := NoLabel; | |
| 1401 | - if abs(step) = 1 then begin | |
| 1402 | - ForCmp(o, lim, Before[up], L); | |
| 1403 | - FixTo(L, top); | |
| 1404 | - ForCmp(o, lim, sEql, exitL); | |
| 1405 | - end else begin | |
| 1406 | - ForCmp(o, lim, Past[up], pastL); | |
| 1407 | - MakeItem(v, o); | |
| 1408 | - b := lim; | |
| 1409 | - if up then begin Value(b); Value(v) end | |
| 1410 | - else begin Value(v); Value(b) end; | |
| 1411 | - gen.Op(sMinus, kWord, 8, false); | |
| 1412 | - PushConst(kWord, abs(step)); | |
| 1413 | - JumpCmp(sGeq, false, L); | |
| 1414 | - FixTo(L, top); | |
| 1415 | - Jump(exitL); | |
| 1416 | - end; | |
| 1417 | - Fix(pastL); | |
| 1418 | - ForStep(o, step); | |
| 1419 | - L := NoLabel; | |
| 1420 | - ForCmp(o, lim, Within[up], L); | |
| 1421 | - FixTo(L, body); | |
| 1422 | -end; | |
| 1423 | - | |
| 1424 | -{ Jumps to L if the condition x is false. The jumps waiting in x.fj join | |
| 1425 | - L; those in x.tj are placed here, as x is true past this. } | |
| 1426 | -procedure CondJump(var x: TItem; var L: longint); | |
| 1427 | -begin | |
| 1428 | - if x.typ^.form <> fBool then MarkAt(x.pos, 'a condition is a BOOL, found ' + What(x)); | |
| 1429 | - if x.mode = mCond then begin | |
| 1430 | - JumpCmp(Negated[x.rel], x.relSigned, L); | |
| 1431 | - L := JoinJumps(L, x.fj); | |
| 1432 | - Fix(x.tj); | |
| 1433 | - exit; | |
| 1434 | - end; | |
| 1435 | - Value(x); | |
| 1436 | - JumpIf(L, false); | |
| 1437 | -end; | |
| 1438 | - | |
| 1439 | -{ As CondJump, but the jump to L is taken where the condition is true; | |
| 1440 | - x.tj joins L and x.fj is placed here } | |
| 1441 | -procedure CondJumpTrue(var x: TItem; var L: longint); | |
| 1442 | -begin | |
| 1443 | - if x.typ^.form <> fBool then MarkAt(x.pos, 'a condition is a BOOL, found ' + What(x)); | |
| 1444 | - if x.mode = mCond then begin | |
| 1445 | - JumpCmp(x.rel, x.relSigned, L); | |
| 1446 | - L := JoinJumps(L, x.tj); | |
| 1447 | - Fix(x.fj); | |
| 1448 | - exit; | |
| 1449 | - end; | |
| 1450 | - Value(x); | |
| 1451 | - JumpIf(L, true); | |
| 1452 | -end; | |
| 1453 | - | |
| 1454 | -{ Procedures } | |
| 1455 | - | |
| 1456 | -procedure BeginProc(sig: PType; params: PObj); | |
| 1457 | -begin | |
| 1458 | - result := sig^.base; | |
| 1459 | - gen.BeginProc(sig, params); | |
| 1460 | -end; | |
| 1461 | - | |
| 1462 | -{ The result of return x: a value on top, a slice's two words on top, | |
| 1463 | - or one copied behind the hidden pointer } | |
| 1464 | -procedure ReturnValue(var x: TItem); | |
| 1465 | -begin | |
| 1466 | - if result = nil then MarkAt(x.pos, 'this procedure returns nothing'); | |
| 1467 | - Coerce(x, result); | |
| 1468 | - if InTwoRegs(result) then PushWords(x) | |
| 1469 | - else if IsAggregate(result) then begin | |
| 1470 | - if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 1471 | - Address(x); | |
| 1472 | - CopyResult(0, result^.size); | |
| 1473 | - end else Value(x); | |
| 1474 | -end; | |
| 1475 | - | |
| 1476 | -{ One of several results: of two words, a value left on top for | |
| 1477 | - ReturnJump to put in rax or rdx; else into its place behind the | |
| 1478 | - hidden pointer. } | |
| 1479 | -procedure ReturnField(f: PObj; var x: TItem); | |
| 1480 | -begin | |
| 1481 | - Coerce(x, f^.typ); | |
| 1482 | - if InTwoRegs(result) then Value(x) | |
| 1483 | - else if IsScalar(f^.typ) then begin | |
| 1484 | - Value(x); | |
| 1485 | - ResultTo(f^.val, ScalarSize(f^.typ)); | |
| 1486 | - end else begin | |
| 1487 | - if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 1488 | - Address(x); | |
| 1489 | - CopyResult(f^.val, f^.typ^.size); | |
| 1490 | - end; | |
| 1491 | -end; | |
| 1492 | - | |
| 1493 | -{ Several results at once, the call's that return f(x) passes on. } | |
| 1494 | -procedure ReturnAll(var x: TItem); | |
| 1495 | -begin | |
| 1496 | - if InTwoRegs(result) then PushWords(x) | |
| 1497 | - else begin | |
| 1498 | - Address(x); | |
| 1499 | - CopyResult(0, result^.size); | |
| 1500 | - end; | |
| 1501 | -end; | |
| 1502 | - | |
| 1503 | -procedure ReturnNone; | |
| 1504 | -begin | |
| 1505 | - if result <> nil then Mark('return needs a result of ' + TypeName(result)); | |
| 1506 | - ReturnJump; | |
| 1507 | -end; | |
| 1508 | - | |
| 1509 | -{ Keeps a variable's address in a frame slot, rather than on the stack, | |
| 1510 | - while other code runs. } | |
| 1511 | -procedure Pin(var x: TItem); | |
| 1512 | -var s: int64; | |
| 1513 | -begin | |
| 1514 | - if (x.mode = mVar) and (x.base = bStack) then begin | |
| 1515 | - Address(x); | |
| 1516 | - s := AllocTemp(8, 8); | |
| 1517 | - PopVar(FrameAdr(s), 8); | |
| 1518 | - x.base := bSlot; | |
| 1519 | - x.chunk := s; | |
| 1520 | - end; | |
| 1521 | -end; | |
| 1522 | - | |
| 1523 | -{ INC(v, k) or DEC: v's address is on the stack, from Address. } | |
| 1524 | -procedure IncDec(var v, k: TItem; dec: boolean); | |
| 1525 | -begin | |
| 1526 | - CoerceExact(k, v.typ, ''); | |
| 1527 | - Value(k); | |
| 1528 | - if v.base = bReg then IncDecReg(v.chunk, ScalarSize(v.typ), v.typ^.signed, dec) | |
| 1529 | - else gen.IncDec(ScalarSize(v.typ), v.typ^.signed, dec); | |
| 1530 | -end; | |
| 1531 | - | |
| 1532 | -{ COPY(d, s): d, an array variable or a slice, as a slice in the frame, | |
| 1533 | - before s is read } | |
| 1534 | -procedure CopyDest(var d: TItem); | |
| 1535 | -begin | |
| 1536 | - if not IsSeq(d.typ) or (d.mode = mStr) or (d.typ^.form = fArray) and ((d.mode <> mVar) or not d.isVar) then | |
| 1537 | - MarkAt(d.pos, 'COPY copies into an array variable or a slice, found ' + What(d)); | |
| 1538 | - if d.typ^.form = fArray then NotReadOnly(d); | |
| 1539 | - SeqSlice(d); | |
| 1540 | -end; | |
| 1541 | - | |
| 1542 | -{ ... and the elements of s copied to its start } | |
| 1543 | -procedure CopyFrom(var d, s: TItem); | |
| 1544 | -begin | |
| 1545 | - if not IsSeq(s.typ) or not Same(ElemOf(s.typ), d.typ^.base) then | |
| 1546 | - MarkAt(s.pos, 'COPY copies elements of ' + TypeName(d.typ^.base) + ', found ' + What(s)); | |
| 1547 | - SeqSlice(s); | |
| 1548 | - CopySlice(d.off, s.off, d.typ^.base^.size, checks, s.pos); | |
| 1549 | -end; | |
| 1550 | - | |
| 1551 | -{ An error is the address of its length, followed by its message. } | |
| 1552 | -function ErrorChunk(var x: TItem; module, name: longint): longint; | |
| 1553 | -var c: longint; p: PBytes; | |
| 1554 | -begin | |
| 1555 | - if x.mode <> mStr then MarkAt(x.pos, 'an error is made from a string'); | |
| 1556 | - p := GetMem(x.val + 8); | |
| 1557 | - PInt64(p)^ := x.val; | |
| 1558 | - Move(rodata^[chunks^[x.chunk].start], p^[8], x.val); | |
| 1559 | - c := NewChunk(secRodata, module, name); | |
| 1560 | - SetData(c, p, x.val + 8, 8); | |
| 1561 | - FreeMem(p); | |
| 1562 | - ErrorChunk := c; | |
| 1563 | -end; | |
| 1564 | - | |
| 1565 | -procedure Msg(var x: TItem); | |
| 1566 | -var s: int64; | |
| 1567 | -begin | |
| 1568 | - Coerce(x, tError); | |
| 1569 | - Value(x); | |
| 1570 | - s := AllocTemp(16, 8); | |
| 1571 | - ErrorText(s); | |
| 1572 | - TempItem(x, tBytes, s); | |
| 1573 | -end; | |
| 1574 | - | |
| 1575 | -end. |
| @@ -1,334 +0,0 @@ | ||
| 1 | -{ link.pas: tree shaking, layout and patching. | |
| 2 | - | |
| 3 | - Starting from the entry chunk, marks every chunk the program can | |
| 4 | - reach through fixups; the rest is left out. Then gives each live | |
| 5 | - chunk its address and builds the images of text and rodata, with | |
| 6 | - every fixup patched. | |
| 7 | - | |
| 8 | - The file is laid out so that each section can be mapped as it lies: | |
| 9 | - text right after the headers, rodata on the next page, then a | |
| 10 | - writable page that starts with the table the dynamic loader reads | |
| 11 | - (none in a static program) and goes on as bss, in memory only. The | |
| 12 | - loader's other tables come at the end of rodata; elf.pas writes them | |
| 13 | - in the room left there. | |
| 14 | - | |
| 15 | - Depends on: chunk for chunks and fixups, scan for names and errors. | |
| 16 | - Leaves out: the file format; that is elf.pas. } | |
| 17 | - | |
| 18 | -unit link; | |
| 19 | - | |
| 20 | -{$mode objfpc}{$H-} | |
| 21 | - | |
| 22 | -interface | |
| 23 | - | |
| 24 | -uses scan, diag, chunk; | |
| 25 | - | |
| 26 | -const | |
| 27 | - Base = $400000; | |
| 28 | - Page = 4096; | |
| 29 | - | |
| 30 | -var | |
| 31 | - textOff, textSize: int64; { file offset and size of text, at Base + textOff } | |
| 32 | - roOff, roSize: int64; { rodata, the room for the loader's tables included } | |
| 33 | - roAdr: int64; { where rodata is loaded } | |
| 34 | - extraAdr: int64; { that room } | |
| 35 | - bssAdr, bssSize: int64; { the writable segment, the loader's table first } | |
| 36 | - bssOff: int64; { the file offset of that table } | |
| 37 | - textImg, roImg: PBytes; | |
| 38 | - placesChunk: longint = -1; { the rodata chunk of the places, made here } | |
| 39 | - linesOff, linesSize: int64; { the line table, in the places: .debug_line } | |
| 40 | - | |
| 41 | -procedure MarkLive(entry: longint); | |
| 42 | -procedure LinkAll(headerSize, roExtra, dynSize: int64); | |
| 43 | - | |
| 44 | -implementation | |
| 45 | - | |
| 46 | -{ Marks live each chunk reached from entry through the fixups. } | |
| 47 | -procedure MarkLive(entry: longint); | |
| 48 | -var stack: PLongs; sp, c, f, t: longint; fx: PFixups; | |
| 49 | -begin | |
| 50 | - stack := GetMem(numChunks * 4); | |
| 51 | - sp := 0; | |
| 52 | - chunks^[entry].live := true; | |
| 53 | - stack^[0] := entry; | |
| 54 | - sp := 1; | |
| 55 | - while sp > 0 do begin | |
| 56 | - dec(sp); | |
| 57 | - c := stack^[sp]; | |
| 58 | - if chunks^[c].size < 0 then Fatal(ChunkName(c) + ' is declared but never defined'); | |
| 59 | - { code and constants have fixups; variables start at zero, holding | |
| 60 | - no addresses } | |
| 61 | - case chunks^[c].sec of | |
| 62 | - secText: fx := fixups; | |
| 63 | - secRodata: fx := dataFixups; | |
| 64 | - else continue; | |
| 65 | - end; | |
| 66 | - for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do begin | |
| 67 | - t := fx^[f].chunk; | |
| 68 | - if not chunks^[t].live then begin | |
| 69 | - chunks^[t].live := true; | |
| 70 | - stack^[sp] := t; | |
| 71 | - inc(sp); | |
| 72 | - end; | |
| 73 | - end; | |
| 74 | - end; | |
| 75 | - FreeMem(stack); | |
| 76 | -end; | |
| 77 | - | |
| 78 | -{ Gives the live chunks of one section their places, from start; | |
| 79 | - returns the end. } | |
| 80 | -function Place(sec: TSection; start: int64): int64; | |
| 81 | -var c: longint; a: int64; | |
| 82 | -begin | |
| 83 | - a := start; | |
| 84 | - for c := 0 to numChunks - 1 do | |
| 85 | - if chunks^[c].live and (chunks^[c].sec = sec) then begin | |
| 86 | - a := (a + chunks^[c].align - 1) div chunks^[c].align * chunks^[c].align; | |
| 87 | - chunks^[c].adr := a; | |
| 88 | - a := a + chunks^[c].size; | |
| 89 | - end; | |
| 90 | - Place := a; | |
| 91 | -end; | |
| 92 | - | |
| 93 | -{ The line table, DWARF 4's line program, which gdb reads as | |
| 94 | - .debug_line and Rt to say where code comes from: one sequence for | |
| 95 | - each live procedure with lines, a row for each place noted, its | |
| 96 | - address, file, line and column. A row whose address and line move a | |
| 97 | - little is one byte, a special opcode; the others are made of the | |
| 98 | - standard ones. Made in lp, lpLen bytes long. } | |
| 99 | -const | |
| 100 | - LineBase = -5; LineRange = 14; OpcodeBase = 13; { the special opcodes' } | |
| 101 | - | |
| 102 | -var | |
| 103 | - lp: PBytes; | |
| 104 | - lpLen, lpCap: longint; | |
| 105 | - | |
| 106 | -procedure PutB(v: int64); | |
| 107 | -begin | |
| 108 | - if lpLen = lpCap then begin | |
| 109 | - lpCap := lpCap * 2 + 256; | |
| 110 | - lp := ReAllocMem(lp, lpCap); | |
| 111 | - end; | |
| 112 | - lp^[lpLen] := byte(v); | |
| 113 | - inc(lpLen); | |
| 114 | -end; | |
| 115 | - | |
| 116 | -procedure PutN(v: int64; n: longint); | |
| 117 | -var i: longint; | |
| 118 | -begin | |
| 119 | - for i := 1 to n do begin PutB(v); v := v shr 8 end; | |
| 120 | -end; | |
| 121 | - | |
| 122 | -{ LEB128, unsigned and signed } | |
| 123 | -procedure PutU(v: int64); | |
| 124 | -begin | |
| 125 | - while v >= $80 do begin PutB(v and $7F or $80); v := v shr 7 end; | |
| 126 | - PutB(v); | |
| 127 | -end; | |
| 128 | - | |
| 129 | -procedure PutS(v: int64); | |
| 130 | -begin | |
| 131 | - while (v < -64) or (v >= 64) do begin PutB(v and $7F or $80); v := SarInt64(v, 7) end; | |
| 132 | - PutB(v and $7F); | |
| 133 | -end; | |
| 134 | - | |
| 135 | -{ The line program of the files paths, nfiles of them, numbered from | |
| 136 | - 1, as they are written in the table } | |
| 137 | -procedure MakeLines(paths: PLongs; nfiles: longint); | |
| 138 | -var i, f, c, cf, cl, cc, op: longint; at, da, dl: int64; s: shortstring; | |
| 139 | -begin | |
| 140 | - lpLen := 0; | |
| 141 | - PutN(0, 4); { unit_length, patched below } | |
| 142 | - PutN(4, 2); { version } | |
| 143 | - PutN(0, 4); { header_length, patched below } | |
| 144 | - PutB(1); PutB(1); { minimum_instruction_length, maximum_operations } | |
| 145 | - PutB(1); { default_is_stmt } | |
| 146 | - PutB(LineBase); PutB(LineRange); PutB(OpcodeBase); | |
| 147 | - for i := 1 to OpcodeBase - 1 do PutB(ord(i in [2, 3, 4, 5, 9, 12])); { their operands } | |
| 148 | - PutB(0); { no include directories } | |
| 149 | - for f := 0 to nfiles - 1 do begin | |
| 150 | - s := ShownPath(NameOf(paths^[f])); | |
| 151 | - for i := 1 to length(s) do PutB(ord(s[i])); | |
| 152 | - PutN(0, 4); { its end, directory, time, length } | |
| 153 | - end; | |
| 154 | - PutB(0); | |
| 155 | - PLongint(@lp^[6])^ := lpLen - 10; | |
| 156 | - i := 0; | |
| 157 | - while i < numLines do begin | |
| 158 | - c := lines^[i].chunk; | |
| 159 | - if not chunks^[c].live then begin inc(i); continue end; | |
| 160 | - PutB(0); PutU(9); PutB(2); { DW_LNE_set_address } | |
| 161 | - PutN(chunks^[c].adr + lines^[i].at - chunks^[c].start, 8); | |
| 162 | - at := lines^[i].at; cf := 1; cl := 1; cc := 0; | |
| 163 | - while (i < numLines) and (lines^[i].chunk = c) do begin | |
| 164 | - f := 1; | |
| 165 | - while paths^[f - 1] <> lines^[i].path do inc(f); | |
| 166 | - if f <> cf then begin PutB(4); PutU(f); cf := f end; { DW_LNS_set_file } | |
| 167 | - if lines^[i].col <> cc then begin PutB(5); PutU(lines^[i].col); cc := lines^[i].col end; { DW_LNS_set_column } | |
| 168 | - da := lines^[i].at - at; | |
| 169 | - dl := lines^[i].line - cl; | |
| 170 | - op := (dl - LineBase) + LineRange * da + OpcodeBase; | |
| 171 | - if (dl >= LineBase) and (dl < LineBase + LineRange) and (op <= 255) then PutB(op) { a special opcode: a row } | |
| 172 | - else begin | |
| 173 | - if da > 0 then begin PutB(2); PutU(da) end; { DW_LNS_advance_pc } | |
| 174 | - if dl <> 0 then begin PutB(3); PutS(dl) end; { DW_LNS_advance_line } | |
| 175 | - PutB(1); { DW_LNS_copy: a row } | |
| 176 | - end; | |
| 177 | - at := lines^[i].at; cl := lines^[i].line; | |
| 178 | - inc(i); | |
| 179 | - end; | |
| 180 | - PutB(2); PutU(chunks^[c].start + chunks^[c].size - at); | |
| 181 | - PutB(0); PutU(1); PutB(1); { DW_LNE_end_sequence } | |
| 182 | - end; | |
| 183 | - PLongint(@lp^[0])^ := lpLen - 4; | |
| 184 | -end; | |
| 185 | - | |
| 186 | -{ The places that Rt reads to say where a failed check or a signal | |
| 187 | - was, and what calls were under way: where the line table is in them, | |
| 188 | - where text starts and stops, the numbers of procedures and of their | |
| 189 | - variables (I64s); for each live procedure where it starts and stops, | |
| 190 | - the offset of its name, its first variable and how many it has, and | |
| 191 | - the registers of variables it saves, as bits of gen's VarRegs, and | |
| 192 | - the frame offset they are saved at (U32s); for each variable the | |
| 193 | - offset of its name, its place in the frame and its kind, as | |
| 194 | - parse.pas's VarKind (U32s); then the names, each ending in a zero | |
| 195 | - byte; then the line table (MakeLines), at linesOff. They are in no | |
| 196 | - order, since Rt looks at them only as the program stops. } | |
| 197 | -procedure MakePlaces(start, stop: int64); | |
| 198 | -var | |
| 199 | - files, first, count: PLongs; buf: PBytes; | |
| 200 | - i, f, nfiles, k, np, nv, c, v, at, names: longint; | |
| 201 | - | |
| 202 | - procedure Name(const s: shortstring); | |
| 203 | - begin | |
| 204 | - PLongint(@buf^[at])^ := names - k; | |
| 205 | - Move(s[1], buf^[names], length(s)); | |
| 206 | - buf^[names + length(s)] := 0; | |
| 207 | - names := names + length(s) + 1; | |
| 208 | - at := at + 4; | |
| 209 | - end; | |
| 210 | - | |
| 211 | -begin | |
| 212 | - files := GetMem(numLines * 4 + 4); | |
| 213 | - first := GetMem(numChunks * 4); | |
| 214 | - count := GetMem(numChunks * 4); | |
| 215 | - FillChar(count^, numChunks * 4, 0); | |
| 216 | - for v := numVarNotes - 1 downto 0 do begin | |
| 217 | - first^[varNotes[v].chunk] := v; | |
| 218 | - inc(count^[varNotes[v].chunk]); | |
| 219 | - end; | |
| 220 | - nfiles := 0; k := 0; np := 0; nv := 0; | |
| 221 | - for i := 0 to numLines - 1 do | |
| 222 | - if chunks^[lines^[i].chunk].live then begin | |
| 223 | - f := 0; | |
| 224 | - while (f < nfiles) and (files^[f] <> lines^[i].path) do inc(f); | |
| 225 | - if f = nfiles then begin | |
| 226 | - files^[f] := lines^[i].path; | |
| 227 | - inc(nfiles); | |
| 228 | - end; | |
| 229 | - end; | |
| 230 | - MakeLines(files, nfiles); | |
| 231 | - for c := 0 to numChunks - 1 do | |
| 232 | - if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin | |
| 233 | - inc(np); | |
| 234 | - k := k + length(ChunkName(c)) + 1; | |
| 235 | - for v := first^[c] to first^[c] + count^[c] - 1 do k := k + length(NameOf(varNotes[v].name)) + 1; | |
| 236 | - nv := nv + count^[c]; | |
| 237 | - end; | |
| 238 | - buf := GetMem(40 + 28 * np + 12 * nv + k + lpLen); | |
| 239 | - { the pool of names starts at k, the line table after it } | |
| 240 | - k := 40 + 28 * np + 12 * nv; | |
| 241 | - names := k; | |
| 242 | - at := 40; | |
| 243 | - v := 0; | |
| 244 | - for c := 0 to numChunks - 1 do | |
| 245 | - if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin | |
| 246 | - PLongint(@buf^[at])^ := chunks^[c].adr; | |
| 247 | - PLongint(@buf^[at + 4])^ := chunks^[c].adr + chunks^[c].size; | |
| 248 | - at := at + 8; | |
| 249 | - Name(ChunkName(c)); | |
| 250 | - PLongint(@buf^[at])^ := v; | |
| 251 | - PLongint(@buf^[at + 4])^ := count^[c]; | |
| 252 | - PLongint(@buf^[at + 8])^ := chunks^[c].saveMask; | |
| 253 | - PLongint(@buf^[at + 12])^ := chunks^[c].saveAt; | |
| 254 | - at := at + 16; | |
| 255 | - v := v + count^[c]; | |
| 256 | - end; | |
| 257 | - for c := 0 to numChunks - 1 do | |
| 258 | - if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then | |
| 259 | - for v := first^[c] to first^[c] + count^[c] - 1 do begin | |
| 260 | - Name(NameOf(varNotes[v].name)); | |
| 261 | - PLongint(@buf^[at])^ := varNotes[v].off; | |
| 262 | - PLongint(@buf^[at + 4])^ := varNotes[v].kind; | |
| 263 | - at := at + 8; | |
| 264 | - end; | |
| 265 | - linesOff := names; | |
| 266 | - linesSize := lpLen; | |
| 267 | - Move(lp^, buf^[names], lpLen); | |
| 268 | - PInt64(@buf^[0])^ := linesOff; | |
| 269 | - PInt64(@buf^[8])^ := start; | |
| 270 | - PInt64(@buf^[16])^ := stop; | |
| 271 | - PInt64(@buf^[24])^ := np; | |
| 272 | - PInt64(@buf^[32])^ := nv; | |
| 273 | - SetData(placesChunk, buf, names + lpLen, 8); | |
| 274 | - FreeMem(buf); FreeMem(files); FreeMem(first); FreeMem(count); | |
| 275 | -end; | |
| 276 | - | |
| 277 | -{ Places the live chunks, leaving roExtra bytes at the end of rodata | |
| 278 | - and dynSize at the start of the writable segment. The segments follow | |
| 279 | - each other in the file with no padding, and each is loaded a page | |
| 280 | - further from its file offset than the one before: rodata at Base + | |
| 281 | - Page + roOff, the writable segment at Base + 2 * Page + bssOff. So an | |
| 282 | - address and its offset agree within a page, as the loader needs, and | |
| 283 | - no two segments, with their different permissions, share a page. | |
| 284 | - The bss of a program without the loader's table has no bytes in the | |
| 285 | - file, and starts on a page of its own. } | |
| 286 | -procedure LinkAll(headerSize, roExtra, dynSize: int64); | |
| 287 | -var c, f, at: longint; e, p, v: int64; | |
| 288 | -begin | |
| 289 | - textOff := (headerSize + 31) div 32 * 32; { as each code chunk is } | |
| 290 | - e := Place(secText, Base + textOff); | |
| 291 | - textSize := e - (Base + textOff); | |
| 292 | - if placesChunk >= 0 then MakePlaces(Base + textOff, e); | |
| 293 | - roOff := (textOff + textSize + 31) div 32 * 32; | |
| 294 | - roAdr := Base + Page + roOff; | |
| 295 | - e := Place(secRodata, roAdr); | |
| 296 | - extraAdr := (e + 7) div 8 * 8; | |
| 297 | - if roExtra > 0 then e := extraAdr + roExtra; | |
| 298 | - roSize := e - roAdr; | |
| 299 | - bssOff := (roOff + roSize + 31) div 32 * 32; | |
| 300 | - if dynSize > 0 then bssAdr := Base + 2 * Page + bssOff | |
| 301 | - else bssAdr := (roAdr + roSize + Page - 1) div Page * Page; | |
| 302 | - e := Place(secBss, bssAdr + dynSize); | |
| 303 | - bssSize := e - bssAdr; | |
| 304 | - | |
| 305 | - textImg := GetMem(textSize + 1); | |
| 306 | - { the gaps between code chunks are int3, a trap if ever run } | |
| 307 | - FillChar(textImg^, textSize + 1, $CC); | |
| 308 | - roImg := GetMem(roSize + 1); | |
| 309 | - FillChar(roImg^, roSize + 1, 0); | |
| 310 | - for c := 0 to numChunks - 1 do | |
| 311 | - if chunks^[c].live then | |
| 312 | - case chunks^[c].sec of | |
| 313 | - secText: begin | |
| 314 | - p := chunks^[c].adr - (Base + textOff); | |
| 315 | - Move(code^[chunks^[c].start], textImg^[p], chunks^[c].size); | |
| 316 | - for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do begin | |
| 317 | - at := fixups^[f].at - chunks^[c].start; | |
| 318 | - { rel32: from the end of the field to the target } | |
| 319 | - v := chunks^[fixups^[f].chunk].adr + fixups^[f].addend | |
| 320 | - - (chunks^[c].adr + at + 4); | |
| 321 | - PLongint(@textImg^[p + at])^ := longint(v); | |
| 322 | - end; | |
| 323 | - end; | |
| 324 | - secRodata: begin | |
| 325 | - p := chunks^[c].adr - roAdr; | |
| 326 | - Move(rodata^[chunks^[c].start], roImg^[p], chunks^[c].size); | |
| 327 | - for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do | |
| 328 | - PInt64(@roImg^[p + dataFixups^[f].at])^ := | |
| 329 | - chunks^[dataFixups^[f].chunk].adr + dataFixups^[f].addend; | |
| 330 | - end; | |
| 331 | - end; | |
| 332 | -end; | |
| 333 | - | |
| 334 | -end. |
| @@ -1,3285 +0,0 @@ | ||
| 1 | -{ parse.pas: the parser. | |
| 2 | - | |
| 3 | - Reads modules by recursive descent and has gen.pas emit code as it | |
| 4 | - goes: one pass, no tree. Importing a module that isn't compiled yet | |
| 5 | - compiles it there and then, so modules are finished in an order where | |
| 6 | - each comes after the modules it imports; that is the order in which | |
| 7 | - their bodies run. | |
| 8 | - | |
| 9 | - A module may come in one file per target (Name.linux-x86_64.raz and | |
| 10 | - so on). The file for this target is compiled; the others beside it | |
| 11 | - are read for their declarations only, and must export the same | |
| 12 | - interface. | |
| 13 | - | |
| 14 | - Depends on: scan, sym, chunk for data, item and gen for code. | |
| 15 | - Leaves out: linking; a module only records the libraries it names, | |
| 16 | - and whether it uses C or says link static. } | |
| 17 | - | |
| 18 | -unit parse; | |
| 19 | - | |
| 20 | -{$mode objfpc}{$H-} | |
| 21 | - | |
| 22 | -interface | |
| 23 | - | |
| 24 | -uses scan, diag, sym, chunk, gen, item; | |
| 25 | - | |
| 26 | -const | |
| 27 | - Target = 'linux-x86_64'; | |
| 28 | - TargetOS = 'linux'; | |
| 29 | - TargetArch = 'x86_64'; | |
| 30 | - { Whether the target has reals (report, appendix A): x86-64 always has | |
| 31 | - SSE2. Without them, used code that computes with one is an error, | |
| 32 | - which raz.pas reports once it knows what the program uses. } | |
| 33 | - HasReals: boolean = true; | |
| 34 | - | |
| 35 | -var | |
| 36 | - libDir, mainDir: shortstring; | |
| 37 | - incDirs: array[0..15] of shortstring; | |
| 38 | - numInc: longint; | |
| 39 | - firstModule, lastModule: PModule; { in the order they were finished } | |
| 40 | - | |
| 41 | -function CompileMain(const path: shortstring): PModule; | |
| 42 | -function ImportModule(name: longint; const args: TArgs): PModule; | |
| 43 | - | |
| 44 | -implementation | |
| 45 | - | |
| 46 | -uses BaseUnix; | |
| 47 | - | |
| 48 | -type | |
| 49 | - PLoop = ^TLoop; | |
| 50 | - TLoop = record | |
| 51 | - brk: longint; { the chain of its breaks } | |
| 52 | - base: longint; { the deferred statements in force outside it } | |
| 53 | - end; | |
| 54 | - | |
| 55 | - { A generic module's import whose instance isn't made yet (deferred, | |
| 56 | - numDeferred); not to be confused with a defer statement (defers) } | |
| 57 | - TDeferred = record | |
| 58 | - o: PObj; { the name it imports as } | |
| 59 | - m: PModule; { the module that imports it } | |
| 60 | - mname: longint; { the generic module } | |
| 61 | - args: TScanState; { at the "(" of its arguments } | |
| 62 | - at: TPos; { the import } | |
| 63 | - end; | |
| 64 | - | |
| 65 | -var | |
| 66 | - checkMode: boolean; { reading a file only for its interface } | |
| 67 | - inTypes: boolean; { in a type section: ^T may name a later type } | |
| 68 | - fwdTypes: array[0..255] of PType; | |
| 69 | - fwdPos: array[0..255] of TPos; { where each names its type } | |
| 70 | - numFwd: longint; | |
| 71 | - lastCalled: PObj; { the procedure the last call was of, if it had a name } | |
| 72 | - passOn: boolean; { the next factor may be a call with several results, for return } | |
| 73 | - curProc: PObj; { the procedure being compiled, or nil } | |
| 74 | - declExported: boolean; { the export mark of the declaration being read } | |
| 75 | - curLoop: PLoop; { the innermost loop, or nil } | |
| 76 | - defers: array[0..63] of longint; { the defer statements in force, their code's addresses } | |
| 77 | - numDefers: longint; | |
| 78 | - deferring: boolean; { compiling a defer statement's statement } | |
| 79 | - directDefer: boolean; { ... as the statement itself, not in a list } | |
| 80 | - deferSlot: int64; { keeps a result while deferred statements run } | |
| 81 | - curResult: PType; { the result type of the procedure being compiled } | |
| 82 | - inExtern: boolean; { reading an extern's signature: ... allowed } | |
| 83 | - tArgv, tBodySig: PType; | |
| 84 | - nameRt, nameFail, nameMain, nameBody, nameStatic, nameCheck, nameOff, nameAdr: longint; | |
| 85 | - declaring: array[0..63] of longint; { the names the declaration being read declares } | |
| 86 | - numDeclaring: longint; | |
| 87 | - skipped: boolean; { the last statement had an error and was skipped } | |
| 88 | - deferred: array[0..63] of TDeferred; { generic imports not yet made, not defer statements } | |
| 89 | - numDeferred: longint; | |
| 90 | - making: boolean; { reading an instance's arguments, at its first use } | |
| 91 | - stray: boolean; { an "end" in this procedure stood left of its statement's line } | |
| 92 | - strayAt: TPos; { ... the first such statement } | |
| 93 | - elseIf: boolean; { an "else if" in this procedure } | |
| 94 | - drops: array[0..4095] of record | |
| 95 | - at: TPos; { a call standing on its own that drops results } | |
| 96 | - n: longint; { ... how many } | |
| 97 | - end; | |
| 98 | - numDrops: longint; { the module being compiled has those from its | |
| 99 | - first one, saved by CompileFile, to here } | |
| 100 | - ends: boolean; { the statements just read never go on past | |
| 101 | - their end: each way through them returns } | |
| 102 | - useAt: TPos; { ... which is here } | |
| 103 | - | |
| 104 | -procedure Expression(var x: TItem; want: PType); forward; | |
| 105 | -function ParseType: PType; forward; | |
| 106 | -procedure Statements; forward; | |
| 107 | -procedure Statement; forward; | |
| 108 | - | |
| 109 | -{ Little helpers } | |
| 110 | - | |
| 111 | -{ A missing symbol is reported just after the one before it. } | |
| 112 | -procedure Expected(const what: shortstring); | |
| 113 | -begin | |
| 114 | - if tok = sBlank then Mark('_ isn''t a name: it is the blank, which stands only left of :='); | |
| 115 | - MarkAt(MissingPos, 'expected ' + what + ', found ' + Found); | |
| 116 | -end; | |
| 117 | - | |
| 118 | -procedure Expect(s: TSym); | |
| 119 | -begin | |
| 120 | - if (tok = sLbrace) and (s in [sThen, sDo]) then | |
| 121 | - MarkHelp(MissingPos, 'expected ' + SymText(s) + ', found ' + Found, 'a block is written ' + SymText(s) + ' ... end'); | |
| 122 | - if tok <> s then Expected(SymText(s)); | |
| 123 | - Next; | |
| 124 | -end; | |
| 125 | - | |
| 126 | -{ The end of a block statement at start, whose line is indented to | |
| 127 | - ind. An "end" left of that was most likely meant for a block around | |
| 128 | - the statement, which leaves the statement's own "end" missing: the | |
| 129 | - first such is kept for EndMissing. } | |
| 130 | -procedure CloseEnd(const start: TPos; ind: longint); | |
| 131 | -begin | |
| 132 | - if (tok = sEnd) and not stray and (Indent < ind) then begin | |
| 133 | - stray := true; | |
| 134 | - strayAt := start; | |
| 135 | - end; | |
| 136 | - Expect(sEnd); | |
| 137 | -end; | |
| 138 | - | |
| 139 | -{ The error of a procedure or module whose own "end" isn't where it | |
| 140 | - should be, with what CloseEnd saw } | |
| 141 | -procedure EndMissing(const p: TPos; const msg: shortstring); | |
| 142 | -begin | |
| 143 | - Error(p, msg); | |
| 144 | - if stray then Note(strayAt, 'is this statement''s "end" missing? one further left closes it'); | |
| 145 | - if elseIf then Help('raz writes elsif: "else if" begins an if that needs its own "end"'); | |
| 146 | - Fail; | |
| 147 | -end; | |
| 148 | - | |
| 149 | -function Name(const s: shortstring): longint; | |
| 150 | -begin | |
| 151 | - Name := Intern(@s[1], length(s)); | |
| 152 | -end; | |
| 153 | - | |
| 154 | -{ A name being declared, and whether it is exported; at is the name } | |
| 155 | -procedure IdentDef(var n: longint; var exp: boolean; var at: TPos); | |
| 156 | -begin | |
| 157 | - if tok <> sIdent then Expected('a name'); | |
| 158 | - n := id; | |
| 159 | - at := TokPos; | |
| 160 | - Next; | |
| 161 | - exp := false; | |
| 162 | - if tok = sTimes then begin | |
| 163 | - exp := true; | |
| 164 | - Next; | |
| 165 | - end; | |
| 166 | -end; | |
| 167 | - | |
| 168 | -{ Declares o in the innermost scope. Its module keeps its declarations | |
| 169 | - in order: its own, for the interface check, and its procedures', to | |
| 170 | - warn of those never used. } | |
| 171 | -procedure Declare(o: PObj); | |
| 172 | -begin | |
| 173 | - Insert(o, TopScope); | |
| 174 | - if TopScope = curModule^.scope then begin | |
| 175 | - if curModule^.last = nil then curModule^.first := o | |
| 176 | - else curModule^.last^.link := o; | |
| 177 | - curModule^.last := o; | |
| 178 | - end else begin | |
| 179 | - if curModule^.lastLocal = nil then curModule^.locals := o | |
| 180 | - else curModule^.lastLocal^.link := o; | |
| 181 | - curModule^.lastLocal := o; | |
| 182 | - end; | |
| 183 | -end; | |
| 184 | - | |
| 185 | -{ o is named: a procedure naming itself doesn't count } | |
| 186 | -procedure Named(o: PObj); | |
| 187 | -begin | |
| 188 | - if o <> curProc then begin | |
| 189 | - o^.used := true; | |
| 190 | - o^.read := true; | |
| 191 | - end; | |
| 192 | -end; | |
| 193 | - | |
| 194 | -{ Recovery | |
| 195 | - | |
| 196 | - After an error the parser goes on with the next statement or | |
| 197 | - declaration, so that one run reports every error. Each statement and | |
| 198 | - declaration is a recovery point: it notes the state of the scanner | |
| 199 | - and the parser where it starts, and an error (MarkAt) jumps back to | |
| 200 | - it. The state is put back, the scanner reads the construct again | |
| 201 | - from its start, skipping to its end, and the parser carries on after | |
| 202 | - it. Nothing is generated after an error, so the code half made for | |
| 203 | - the construct does no harm. A name that isn't declared is declared | |
| 204 | - where it is used, with type tPoison, and what follows from it is not | |
| 205 | - reported again; so are the names of a declaration that had an error. | |
| 206 | - | |
| 207 | - Skipping counts the words that open and close blocks. A symbol at | |
| 208 | - the start of a line is taken to start a declaration of the module, | |
| 209 | - as layout has it: a statement skipped up to one gives up its | |
| 210 | - procedure, and the procedure is skipped up to its end. } | |
| 211 | - | |
| 212 | -type | |
| 213 | - TRecover = record | |
| 214 | - jb: jmp_buf; | |
| 215 | - outer: ^jmp_buf; | |
| 216 | - scan: TScanState; | |
| 217 | - scopes, depth, pushed, numDefers: longint; | |
| 218 | - loop: PLoop; | |
| 219 | - result: PType; | |
| 220 | - deferring, directDefer, quiet: boolean; | |
| 221 | - end; | |
| 222 | - | |
| 223 | -procedure Guard(var r: TRecover); | |
| 224 | -begin | |
| 225 | - r.outer := recovery; | |
| 226 | - SaveScan(r.scan); | |
| 227 | - r.scopes := ScopeDepth; r.depth := depth; r.pushed := pushed; r.numDefers := numDefers; | |
| 228 | - r.loop := curLoop; r.result := curResult; | |
| 229 | - r.deferring := deferring; r.directDefer := directDefer; r.quiet := quiet; | |
| 230 | - recovery := @r.jb; | |
| 231 | - quiet := false; | |
| 232 | -end; | |
| 233 | - | |
| 234 | -procedure Unguard(var r: TRecover); | |
| 235 | -begin | |
| 236 | - recovery := r.outer; | |
| 237 | - quiet := r.quiet; | |
| 238 | - skipping := false; | |
| 239 | -end; | |
| 240 | - | |
| 241 | -{ After an error: the state as it was at the start of r's construct, | |
| 242 | - whose first symbol is again the current one } | |
| 243 | -procedure Restore(var r: TRecover); | |
| 244 | -begin | |
| 245 | - RestoreScan(r.scan); | |
| 246 | - skipping := true; | |
| 247 | - CutScopes(r.scopes); ResetStack(r.depth, r.pushed); numDefers := r.numDefers; | |
| 248 | - curLoop := r.loop; curResult := r.result; | |
| 249 | - deferring := r.deferring; directDefer := r.directDefer; | |
| 250 | - inTypes := false; declExported := false; inExtern := false; making := false; | |
| 251 | -end; | |
| 252 | - | |
| 253 | -{ Gives up the construct r is for, and the one around it } | |
| 254 | -procedure GiveUp(var r: TRecover); | |
| 255 | -begin | |
| 256 | - Unguard(r); | |
| 257 | - if recovery = nil then Finish; | |
| 258 | - longjmp(recovery^, 1); | |
| 259 | -end; | |
| 260 | - | |
| 261 | -{ Reads to the end of a statement or declaration, from its first | |
| 262 | - symbol: up to the ";" that ends it, not read, or to the "end", | |
| 263 | - "else", "elsif", "until" or "|" of the statement around it (an arm's | |
| 264 | - "|" comes after a ";", the operator's after an operand). False if it | |
| 265 | - stopped at a symbol that starts a line, or the end of the file. } | |
| 266 | -function SkipConstruct: boolean; | |
| 267 | -var d, p: longint; prev: TSym; | |
| 268 | -begin | |
| 269 | - d := 0; p := 0; prev := sSemicolon; | |
| 270 | - repeat | |
| 271 | - case tok of | |
| 272 | - sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: inc(d); | |
| 273 | - sEnd, sUntil: if d = 0 then exit(true) else dec(d); | |
| 274 | - sElse, sElsif: if (d = 0) and (p = 0) then exit(true); | |
| 275 | - sBar: if (d = 0) and (p = 0) and (prev = sSemicolon) then exit(true); | |
| 276 | - sLparen, sLbrak, sLbrace: inc(p); | |
| 277 | - sRparen, sRbrak, sRbrace: if p > 0 then dec(p); | |
| 278 | - sSemicolon: if (d = 0) and (p = 0) then exit(true); | |
| 279 | - sEof: exit(false); | |
| 280 | - end; | |
| 281 | - prev := tok; | |
| 282 | - Next; | |
| 283 | - until AtLineStart; | |
| 284 | - SkipConstruct := false; | |
| 285 | -end; | |
| 286 | - | |
| 287 | -{ Declares n where it is used, after it was reported as not declared, | |
| 288 | - or after the declaration of it had an error } | |
| 289 | -function Poison(n: longint): PObj; | |
| 290 | -var o: PObj; | |
| 291 | -begin | |
| 292 | - o := NewObj(oConst, n, tPoison); | |
| 293 | - Insert(o, TopScope); | |
| 294 | - Poison := o; | |
| 295 | -end; | |
| 296 | - | |
| 297 | -procedure WillDeclare(n: longint); | |
| 298 | -begin | |
| 299 | - if numDeclaring <= High(declaring) then declaring[numDeclaring] := n; | |
| 300 | - inc(numDeclaring); | |
| 301 | -end; | |
| 302 | - | |
| 303 | -{ After a declaration had an error: its names are poison, and reading | |
| 304 | - goes on after it } | |
| 305 | -procedure SkipDeclaration; | |
| 306 | -var i: longint; | |
| 307 | -begin | |
| 308 | - for i := 0 to numDeclaring - 1 do | |
| 309 | - if (i <= High(declaring)) and (Find(TopScope, declaring[i]) = nil) then | |
| 310 | - Poison(declaring[i]); | |
| 311 | - if SkipConstruct and (tok = sSemicolon) then Next; | |
| 312 | -end; | |
| 313 | - | |
| 314 | -function ConstValue(var x: TItem): int64; | |
| 315 | -begin | |
| 316 | - if x.mode <> mConst then MarkAt(x.pos, 'expected a constant'); | |
| 317 | - ConstValue := x.val; | |
| 318 | -end; | |
| 319 | - | |
| 320 | -function ConstExpr: int64; | |
| 321 | -var x: TItem; | |
| 322 | -begin | |
| 323 | - Expression(x, nil); | |
| 324 | - if not IsInt(x.typ) then MarkAt(x.pos, 'expected an integer constant, found ' + What(x)); | |
| 325 | - ConstExpr := ConstValue(x); | |
| 326 | -end; | |
| 327 | - | |
| 328 | -{ Help after a name that isn't found: o, the closest name in reach } | |
| 329 | -procedure DidYouMean(o: PObj); | |
| 330 | -begin | |
| 331 | - if o <> nil then Help('did you mean ' + NameOf(o^.name) + '?'); | |
| 332 | -end; | |
| 333 | - | |
| 334 | -{ Where module n is found, if it is: dir and the file in it } | |
| 335 | -function ModuleFile(n: longint; var dir, f: shortstring): boolean; forward; | |
| 336 | - | |
| 337 | -{ Reports name n at p as not declared, with what it may have meant } | |
| 338 | -procedure NotDeclared(const p: TPos; n: longint); | |
| 339 | -var dir, f: shortstring; | |
| 340 | -begin | |
| 341 | - Error(p, NameOf(n) + ' isn''t declared'); | |
| 342 | - if ModuleFile(n, dir, f) then Help(NameOf(n) + ' is a module: import ' + NameOf(n)) | |
| 343 | - else if (NameOf(n) = 'elif') or (NameOf(n) = 'elseif') then Help('raz writes elsif') | |
| 344 | - else if Reserved(Name(LowerCase(NameOf(n)))) then Help('a reserved word is written in lower case: ' + LowerCase(NameOf(n))) | |
| 345 | - else DidYouMean(Closest(n, -1)); | |
| 346 | -end; | |
| 347 | - | |
| 348 | -procedure FirstUse(o: PObj; const at: TPos); forward; | |
| 349 | -procedure InstancesFor(wholeRest: boolean); forward; | |
| 350 | -procedure AddImport(m, im: PModule); forward; | |
| 351 | - | |
| 352 | -function QualIdent: PObj; | |
| 353 | -var o: PObj; m: PModule; at: TPos; | |
| 354 | -begin | |
| 355 | - if tok <> sIdent then Expected('a name'); | |
| 356 | - at := TokPos; | |
| 357 | - o := Lookup(id); | |
| 358 | - if o = nil then begin | |
| 359 | - if not making then Poison(id); { it may yet be declared } | |
| 360 | - quiet := false; | |
| 361 | - NotDeclared(at, id); | |
| 362 | - if making then Note(useAt, 'the instance is made here, where it is first used'); | |
| 363 | - Fail; | |
| 364 | - end; | |
| 365 | - if o^.typ = tPoison then quiet := true; | |
| 366 | - Named(o); | |
| 367 | - Next; | |
| 368 | - if o^.cls = oModule then begin | |
| 369 | - if o^.module = nil then FirstUse(o, at); | |
| 370 | - Expect(sPeriod); | |
| 371 | - if tok <> sIdent then Expected('a name'); | |
| 372 | - m := o^.module; | |
| 373 | - o := Find(m^.scope, id); | |
| 374 | - if (o = nil) or not o^.exported then begin | |
| 375 | - Error(TokPos, NameOf(id) + ' isn''t exported by ' + NameOf(m^.name)); | |
| 376 | - if o <> nil then Note(o^.pos, NameOf(id) + ' is declared here, without a *') | |
| 377 | - else DidYouMean(Closest(id, m^.scope)); | |
| 378 | - Fail; | |
| 379 | - end; | |
| 380 | - Next; | |
| 381 | - end; | |
| 382 | - QualIdent := o; | |
| 383 | -end; | |
| 384 | - | |
| 385 | -{ The field of record type t named by the current symbol } | |
| 386 | -function FieldOf(t: PType): PObj; | |
| 387 | -var f: PObj; | |
| 388 | -begin | |
| 389 | - f := Find(t^.scope, id); | |
| 390 | - if f = nil then begin | |
| 391 | - Error(TokPos, TypeName(t) + ' has no field ' + NameOf(id)); | |
| 392 | - DidYouMean(Closest(id, t^.scope)); | |
| 393 | - Fail; | |
| 394 | - end; | |
| 395 | - if not f^.exported and (t^.module <> curModule) then begin | |
| 396 | - Error(TokPos, 'field ' + NameOf(id) + ' of ' + TypeName(t) + ' isn''t exported'); | |
| 397 | - Note(f^.pos, NameOf(id) + ' is declared here, without a *'); | |
| 398 | - Fail; | |
| 399 | - end; | |
| 400 | - FieldOf := f; | |
| 401 | -end; | |
| 402 | - | |
| 403 | -function NewPtr(base: PType; vol: boolean): PType; | |
| 404 | -var t: PType; | |
| 405 | -begin | |
| 406 | - t := NewType(fPtr, 8, 8); | |
| 407 | - t^.base := base; | |
| 408 | - t^.vol := vol; | |
| 409 | - NewPtr := t; | |
| 410 | -end; | |
| 411 | - | |
| 412 | -{ Types } | |
| 413 | - | |
| 414 | -function RecordType: PType; | |
| 415 | -var | |
| 416 | - t, ft: PType; f, last: PObj; names: array[0..63] of longint; | |
| 417 | - exps: array[0..63] of boolean; poss: array[0..63] of TPos; | |
| 418 | - n, i: longint; off, a, maxA: int64; | |
| 419 | -begin | |
| 420 | - t := NewType(fRecord, 0, 1); | |
| 421 | - if tok = sPacked then begin t^.packd := true; Next end; | |
| 422 | - Expect(sRecord); | |
| 423 | - t^.scope := NewScope; | |
| 424 | - off := 0; maxA := 1; last := nil; | |
| 425 | - while tok = sIdent do begin | |
| 426 | - n := 0; | |
| 427 | - repeat | |
| 428 | - if n > High(names) then Mark('more than ' + IntStr(High(names) + 1) + ' names in one declaration'); | |
| 429 | - IdentDef(names[n], exps[n], poss[n]); | |
| 430 | - inc(n); | |
| 431 | - if tok <> sComma then break; | |
| 432 | - Next; | |
| 433 | - until false; | |
| 434 | - Expect(sColon); | |
| 435 | - ft := ParseType; | |
| 436 | - if t^.packd then a := 1 else a := ft^.align; | |
| 437 | - for i := 0 to n - 1 do begin | |
| 438 | - off := AlignUp(off, a); | |
| 439 | - f := NewObj(oField, names[i], ft); | |
| 440 | - f^.exported := exps[i]; | |
| 441 | - f^.pos := poss[i]; | |
| 442 | - f^.val := off; | |
| 443 | - Insert(f, t^.scope); | |
| 444 | - if last = nil then t^.fields := f else last^.next := f; | |
| 445 | - last := f; | |
| 446 | - off := off + ft^.size; | |
| 447 | - end; | |
| 448 | - if a > maxA then maxA := a; | |
| 449 | - Expect(sSemicolon); | |
| 450 | - end; | |
| 451 | - Expect(sEnd); | |
| 452 | - t^.align := maxA; | |
| 453 | - t^.size := AlignUp(off, maxA); | |
| 454 | - RecordType := t; | |
| 455 | -end; | |
| 456 | - | |
| 457 | -function VariantType: PType; | |
| 458 | -var t, ft: PType; c, lastC, f, lastF: PObj; tag, off, maxSize: int64; n: longint; | |
| 459 | -begin | |
| 460 | - Expect(sVariant); | |
| 461 | - t := NewType(fVariant, 8, 8); | |
| 462 | - tag := 0; maxSize := 8; lastC := nil; | |
| 463 | - while tok = sIdent do begin | |
| 464 | - c := NewObj(oCase, id, t); | |
| 465 | - c^.val := tag; | |
| 466 | - c^.exported := declExported; | |
| 467 | - Declare(c); | |
| 468 | - if lastC = nil then t^.fields := c else lastC^.next := c; | |
| 469 | - lastC := c; | |
| 470 | - Next; | |
| 471 | - off := 8; | |
| 472 | - if tok = sLparen then begin | |
| 473 | - Next; | |
| 474 | - lastF := nil; | |
| 475 | - repeat | |
| 476 | - n := 0; | |
| 477 | - repeat | |
| 478 | - if tok <> sIdent then Expected('a name'); | |
| 479 | - f := NewObj(oField, id, nil); | |
| 480 | - f^.exported := true; | |
| 481 | - if lastF = nil then c^.fields := f else lastF^.next := f; | |
| 482 | - lastF := f; | |
| 483 | - inc(n); | |
| 484 | - Next; | |
| 485 | - if tok <> sComma then break; | |
| 486 | - Next; | |
| 487 | - until false; | |
| 488 | - Expect(sColon); | |
| 489 | - ft := ParseType; | |
| 490 | - f := c^.fields; | |
| 491 | - while f^.typ <> nil do f := f^.next; | |
| 492 | - while f <> nil do begin | |
| 493 | - f^.typ := ft; | |
| 494 | - off := AlignUp(off, ft^.align); | |
| 495 | - f^.val := off; | |
| 496 | - off := off + ft^.size; | |
| 497 | - f := f^.next; | |
| 498 | - end; | |
| 499 | - if ft^.align > t^.align then t^.align := ft^.align; | |
| 500 | - if tok <> sSemicolon then break; | |
| 501 | - Next; | |
| 502 | - until false; | |
| 503 | - Expect(sRparen); | |
| 504 | - end; | |
| 505 | - if off > maxSize then maxSize := off; | |
| 506 | - Expect(sSemicolon); | |
| 507 | - inc(tag); | |
| 508 | - end; | |
| 509 | - Expect(sEnd); | |
| 510 | - t^.len := tag; | |
| 511 | - t^.size := AlignUp(maxSize, t^.align); | |
| 512 | - VariantType := t; | |
| 513 | -end; | |
| 514 | - | |
| 515 | -{ Several results: a record of them, laid out as C would, and | |
| 516 | - returned in rax and rdx if they are two words (InTwoRegs), or else | |
| 517 | - through a hidden pointer. } | |
| 518 | -function Results: PType; | |
| 519 | -var t, ft: PType; f, last: PObj; off: int64; | |
| 520 | -begin | |
| 521 | - Next; | |
| 522 | - t := NewType(fRecord, 0, 1); | |
| 523 | - t^.tuple := true; | |
| 524 | - off := 0; last := nil; | |
| 525 | - repeat | |
| 526 | - ft := ParseType; | |
| 527 | - off := AlignUp(off, ft^.align); | |
| 528 | - f := NewObj(oField, -1, ft); | |
| 529 | - f^.val := off; | |
| 530 | - off := off + ft^.size; | |
| 531 | - if ft^.align > t^.align then t^.align := ft^.align; | |
| 532 | - if last = nil then t^.fields := f else last^.next := f; | |
| 533 | - last := f; | |
| 534 | - inc(t^.len); | |
| 535 | - if tok <> sComma then break; | |
| 536 | - Next; | |
| 537 | - until false; | |
| 538 | - Expect(sRparen); | |
| 539 | - t^.size := AlignUp(off, t^.align); | |
| 540 | - if t^.len = 1 then Results := t^.fields^.typ else Results := t; | |
| 541 | -end; | |
| 542 | - | |
| 543 | -function Signature: PType; | |
| 544 | -var | |
| 545 | - t, pt: PType; p, first, last: PObj; isVar: boolean; n: longint; | |
| 546 | -begin | |
| 547 | - t := NewType(fProc, 8, 8); | |
| 548 | - last := nil; n := 0; | |
| 549 | - if tok = sLparen then begin | |
| 550 | - Next; | |
| 551 | - if tok <> sRparen then | |
| 552 | - repeat | |
| 553 | - if tok = sEllipsis then begin | |
| 554 | - if not inExtern then Mark('... is only for extern procedures'); | |
| 555 | - Next; t^.variadic := true; break | |
| 556 | - end; | |
| 557 | - isVar := tok = sVar; | |
| 558 | - if isVar then Next; | |
| 559 | - first := nil; | |
| 560 | - repeat | |
| 561 | - if tok <> sIdent then Expected('a name'); | |
| 562 | - p := NewObj(oVar, id, nil); | |
| 563 | - p^.isVar := isVar; | |
| 564 | - p^.lev := 1; | |
| 565 | - if last = nil then t^.fields := p else last^.next := p; | |
| 566 | - last := p; | |
| 567 | - if first = nil then first := p; | |
| 568 | - inc(n); | |
| 569 | - Next; | |
| 570 | - if tok <> sComma then break; | |
| 571 | - Next; | |
| 572 | - until false; | |
| 573 | - Expect(sColon); | |
| 574 | - pt := ParseType; | |
| 575 | - while first <> nil do begin first^.typ := pt; first := first^.next end; | |
| 576 | - if tok <> sSemicolon then break; | |
| 577 | - Next; | |
| 578 | - until false; | |
| 579 | - Expect(sRparen); | |
| 580 | - end; | |
| 581 | - t^.len := n; | |
| 582 | - if tok = sColon then begin | |
| 583 | - Next; | |
| 584 | - if tok = sLparen then t^.base := Results else t^.base := ParseType; | |
| 585 | - end; | |
| 586 | - Signature := t; | |
| 587 | -end; | |
| 588 | - | |
| 589 | -function EnumType: PType; | |
| 590 | -var t: PType; o, last: PObj; start: TPos; | |
| 591 | -begin | |
| 592 | - start := TokPos; | |
| 593 | - t := NewType(fEnum, 1, 1); | |
| 594 | - last := nil; | |
| 595 | - repeat | |
| 596 | - Next; | |
| 597 | - if tok <> sIdent then Expected('a name'); | |
| 598 | - o := NewObj(oConst, id, t); | |
| 599 | - o^.val := t^.len; | |
| 600 | - o^.exported := declExported; | |
| 601 | - o^.used := true; { an enumeration's names are its type's, warned of with it } | |
| 602 | - Declare(o); | |
| 603 | - if last = nil then t^.fields := o else last^.next := o; | |
| 604 | - last := o; | |
| 605 | - inc(t^.len); | |
| 606 | - Next; | |
| 607 | - until tok <> sComma; | |
| 608 | - Expect(sRparen); | |
| 609 | - if t^.len > 256 then MarkAt(SpanFrom(start), 'an enumeration has at most 256 names'); | |
| 610 | - EnumType := t; | |
| 611 | -end; | |
| 612 | - | |
| 613 | -function SetOf(e: PType; const at: TPos): PType; | |
| 614 | -var t: PType; | |
| 615 | -begin | |
| 616 | - if (e^.form <> fEnum) or (e^.len > 64) then MarkAt(at, 'a set needs an enumeration of at most 64 names, found ' + TypeName(e)); | |
| 617 | - t := NewType(fSet, 8, 8); | |
| 618 | - if e^.len <= 8 then t^.size := 1 | |
| 619 | - else if e^.len <= 16 then t^.size := 2 | |
| 620 | - else if e^.len <= 32 then t^.size := 4; | |
| 621 | - t^.align := t^.size; | |
| 622 | - t^.base := e; | |
| 623 | - t^.len := e^.len; | |
| 624 | - SetOf := t; | |
| 625 | -end; | |
| 626 | - | |
| 627 | -function ParseType: PType; | |
| 628 | -var t: PType; o: PObj; n: int64; vol: boolean; start: TPos; | |
| 629 | -begin | |
| 630 | - start := TokPos; | |
| 631 | - case tok of | |
| 632 | - sIdent: begin | |
| 633 | - o := QualIdent; | |
| 634 | - if o^.cls <> oType then MarkAt(SpanFrom(start), NameOf(o^.name) + ' isn''t a type'); | |
| 635 | - t := o^.typ; | |
| 636 | - end; | |
| 637 | - sLbrak: begin | |
| 638 | - Next; | |
| 639 | - if tok = sRbrak then begin | |
| 640 | - Next; | |
| 641 | - t := NewType(fSlice, 16, 8); | |
| 642 | - t^.base := ParseType(); | |
| 643 | - end else begin | |
| 644 | - start := TokPos; | |
| 645 | - n := ConstExpr; | |
| 646 | - if (n <= 0) or (n > (int64(1) shl 40)) then MarkAt(SpanFrom(start), 'an array''s length is from 1 to 2^40, found ' + IntStr(n)); | |
| 647 | - Expect(sRbrak); | |
| 648 | - t := NewType(fArray, 0, 1); | |
| 649 | - t^.len := n; | |
| 650 | - t^.base := ParseType(); | |
| 651 | - t^.size := n * t^.base^.size; | |
| 652 | - t^.align := t^.base^.align; | |
| 653 | - end; | |
| 654 | - end; | |
| 655 | - sArrow: begin | |
| 656 | - Next; | |
| 657 | - vol := tok = sVolatile; | |
| 658 | - if vol then Next; | |
| 659 | - if inTypes and (tok = sIdent) and (Lookup(id) = nil) then begin | |
| 660 | - { a type declared later in this section } | |
| 661 | - t := NewPtr(nil, vol); | |
| 662 | - t^.fwd := id; | |
| 663 | - if numFwd > High(fwdTypes) then Mark('more than ' + IntStr(High(fwdTypes) + 1) + ' pointers to types not yet declared'); | |
| 664 | - fwdTypes[numFwd] := t; | |
| 665 | - fwdPos[numFwd] := TokPos; | |
| 666 | - inc(numFwd); | |
| 667 | - Next; | |
| 668 | - end else | |
| 669 | - t := NewPtr(ParseType(), vol); | |
| 670 | - end; | |
| 671 | - sPacked, sRecord: t := RecordType; | |
| 672 | - sVariant: t := VariantType; | |
| 673 | - sProc: begin Next; t := Signature end; | |
| 674 | - sLparen: t := EnumType; | |
| 675 | - sSet: begin | |
| 676 | - Next; | |
| 677 | - Expect(sOf); | |
| 678 | - start := TokPos; | |
| 679 | - t := SetOf(ParseType(), start); | |
| 680 | - end; | |
| 681 | - else Expected('a type'); | |
| 682 | - end; | |
| 683 | - ParseType := t; | |
| 684 | -end; | |
| 685 | - | |
| 686 | -{ Expressions } | |
| 687 | - | |
| 688 | -procedure Call(var x: TItem); | |
| 689 | -var c: TCall; a: TItem; want: PType; | |
| 690 | -begin | |
| 691 | - BeginCall(c, x); | |
| 692 | - Expect(sLparen); | |
| 693 | - if tok <> sRparen then | |
| 694 | - repeat | |
| 695 | - if c.param <> nil then want := c.param^.typ else want := nil; | |
| 696 | - Expression(a, want); | |
| 697 | - PassArg(c, a); | |
| 698 | - if tok <> sComma then break; | |
| 699 | - Next; | |
| 700 | - until false; | |
| 701 | - Expect(sRparen); | |
| 702 | - EndCall(c, x); | |
| 703 | -end; | |
| 704 | - | |
| 705 | -procedure Atomic(var x: TItem; which: longint); | |
| 706 | -var p, a: TItem; t: PType; i, n: longint; | |
| 707 | -begin | |
| 708 | - Expression(p, nil); | |
| 709 | - if p.typ^.form <> fPtr then MarkAt(p.pos, 'expected a pointer, found ' + What(p)); | |
| 710 | - t := p.typ^.base; | |
| 711 | - if not (IsWord(t) or ((t^.form = fPtr) and (which <> bXadd))) then | |
| 712 | - if which = bXadd then MarkAt(p.pos, 'expected a pointer to an INT or a UINT, found ' + TypeName(p.typ)) | |
| 713 | - else MarkAt(p.pos, 'expected a pointer to an INT, a UINT or a pointer, found ' + TypeName(p.typ)); | |
| 714 | - Value(p); | |
| 715 | - if which = bCas then n := 2 else n := 1; | |
| 716 | - for i := 1 to n do begin | |
| 717 | - Expect(sComma); | |
| 718 | - Expression(a, t); | |
| 719 | - Coerce(a, t); | |
| 720 | - Value(a); | |
| 721 | - end; | |
| 722 | - gen.Atomic(which); | |
| 723 | - MakeConst(x, t, 0); | |
| 724 | - if which = bCas then x.typ := tBool; | |
| 725 | - x.mode := mStack; | |
| 726 | -end; | |
| 727 | - | |
| 728 | -{ NEW(p, a): p := a^.alloc(a, SIZE(T), alignment), zeroed if not nil. | |
| 729 | - a is a ^Mem.Allocator, the one interface the compiler knows. } | |
| 730 | -procedure New_(var x: TItem); | |
| 731 | -var y, z: TItem; t: PType; f: PObj; s: int64; c: TCall; | |
| 732 | -begin | |
| 733 | - Expression(x, nil); | |
| 734 | - NotReadOnly(x); | |
| 735 | - if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fPtr) then MarkAt(x.pos, 'NEW needs a pointer variable'); | |
| 736 | - t := x.typ^.base; | |
| 737 | - Expect(sComma); | |
| 738 | - Expression(y, nil); | |
| 739 | - if (y.typ^.form <> fPtr) or (y.typ^.base^.obj = nil) or (y.typ^.base^.obj^.module = nil) | |
| 740 | - or (NameOf(y.typ^.base^.obj^.name) <> 'Allocator') | |
| 741 | - or (NameOf(y.typ^.base^.obj^.module^.name) <> 'Mem') then MarkAt(y.pos, 'NEW needs a ^Mem.Allocator, found ' + What(y)); | |
| 742 | - f := Find(y.typ^.base^.scope, Name('alloc')); | |
| 743 | - s := ToSlot(y); | |
| 744 | - PushVar(FrameAdr(s), 8, false, kWord); | |
| 745 | - MakeConst(z, f^.typ, 0); | |
| 746 | - z.mode := mVar; z.base := bStack; z.off := f^.val; | |
| 747 | - BeginCall(c, z); | |
| 748 | - PushVar(FrameAdr(s), 8, false, kWord); | |
| 749 | - MakeConst(z, y.typ, 0); | |
| 750 | - z.mode := mStack; | |
| 751 | - PassArg(c, z); | |
| 752 | - MakeConst(z, tInt, t^.size); | |
| 753 | - PassArg(c, z); | |
| 754 | - MakeConst(z, tInt, t^.align); | |
| 755 | - PassArg(c, z); | |
| 756 | - EndCall(c, z); | |
| 757 | - ZeroNew(t^.size); | |
| 758 | - z.typ := x.typ; | |
| 759 | - Assign(x, z); | |
| 760 | - MakeConst(x, tNone, 0); | |
| 761 | - x.mode := mNone; | |
| 762 | -end; | |
| 763 | - | |
| 764 | -function IncOrDec(which: longint): shortstring; | |
| 765 | -begin | |
| 766 | - if which = bDec then IncOrDec := 'DEC' else IncOrDec := 'INC'; | |
| 767 | -end; | |
| 768 | - | |
| 769 | -procedure Builtin(var x: TItem); | |
| 770 | -var which, n: longint; y: TItem; t: PType; | |
| 771 | -begin | |
| 772 | - which := x.obj^.val; | |
| 773 | - if which in [bFence, bArgc, bArgv] then begin | |
| 774 | - if tok = sLparen then begin Next; Expect(sRparen) end; | |
| 775 | - MakeConst(x, tNone, 0); | |
| 776 | - case which of | |
| 777 | - bFence: begin Fence; x.mode := mNone end; | |
| 778 | - bArgc: begin PushVar(GlobalAdr(argcChunk), 8, false, kWord); x.typ := tInt; x.mode := mStack end; | |
| 779 | - bArgv: begin PushVar(GlobalAdr(argvChunk), 8, false, kWord); x.typ := tArgv; x.mode := mStack end; | |
| 780 | - end; | |
| 781 | - exit; | |
| 782 | - end; | |
| 783 | - Expect(sLparen); | |
| 784 | - case which of | |
| 785 | - bSize: begin | |
| 786 | - t := ParseType; | |
| 787 | - MakeConst(x, tConst, t^.size); | |
| 788 | - end; | |
| 789 | - bAdr: begin | |
| 790 | - Expression(y, nil); | |
| 791 | - NotReadOnly(y); | |
| 792 | - if (y.mode <> mVar) or not y.isVar then MarkAt(y.pos, 'ADR needs a variable'); | |
| 793 | - if y.odd then MarkAt(y.pos, 'a field of a packed record has no address'); | |
| 794 | - t := NewPtr(y.typ, y.vol); | |
| 795 | - Address(y); | |
| 796 | - MakeConst(x, t, 0); | |
| 797 | - x.mode := mStack; | |
| 798 | - end; | |
| 799 | - bLen: begin | |
| 800 | - Expression(y, nil); | |
| 801 | - if y.mode = mStr then MakeConst(x, tConst, y.val) | |
| 802 | - else if y.typ^.form = fArray then begin | |
| 803 | - Discard(y); | |
| 804 | - MakeConst(x, tConst, y.typ^.len) | |
| 805 | - end else if (y.typ^.form = fSlice) and (y.mode = mVar) then begin | |
| 806 | - { its length, the word after its pointer } | |
| 807 | - y.off := y.off + 8; | |
| 808 | - y.typ := tInt; | |
| 809 | - Value(y); | |
| 810 | - x := y; | |
| 811 | - end else MarkAt(y.pos, 'LEN needs an array or a slice, found ' + What(y)); | |
| 812 | - end; | |
| 813 | - bSyscall: begin | |
| 814 | - n := 0; | |
| 815 | - repeat | |
| 816 | - if n > 0 then Next; | |
| 817 | - Expression(y, nil); | |
| 818 | - if not (IsScalar(y.typ) and (y.typ^.form <> fReal)) then MarkAt(y.pos, 'expected an integer or a pointer, found ' + What(y)); | |
| 819 | - if y.typ^.form = fConst then Coerce(y, tInt); | |
| 820 | - Value(y); | |
| 821 | - inc(n); | |
| 822 | - until (tok <> sComma) or (n = 7); | |
| 823 | - SysCall(n); | |
| 824 | - MakeConst(x, tInt, 0); | |
| 825 | - x.mode := mStack; | |
| 826 | - end; | |
| 827 | - bCas, bXchg, bXadd: Atomic(x, which); | |
| 828 | - bMsg: begin Expression(x, tError); Msg(x) end; | |
| 829 | - bInc, bDec: begin | |
| 830 | - Expression(x, nil); | |
| 831 | - NotReadOnly(x); | |
| 832 | - if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fInt) then | |
| 833 | - MarkAt(x.pos, IncOrDec(which) + ' needs an integer variable'); | |
| 834 | - if x.base <> bReg then Address(x); | |
| 835 | - if tok = sComma then begin | |
| 836 | - Next; | |
| 837 | - Expression(y, x.typ); | |
| 838 | - end else MakeConst(y, x.typ, 1); | |
| 839 | - IncDec(x, y, which = bDec); | |
| 840 | - MakeConst(x, tNone, 0); | |
| 841 | - x.mode := mNone; | |
| 842 | - end; | |
| 843 | - bNew: New_(x); | |
| 844 | - bCopy: begin | |
| 845 | - Expression(x, nil); | |
| 846 | - CopyDest(x); | |
| 847 | - Expect(sComma); | |
| 848 | - Expression(y, nil); | |
| 849 | - CopyFrom(x, y); | |
| 850 | - MakeConst(x, tNone, 0); | |
| 851 | - x.mode := mNone; | |
| 852 | - end; | |
| 853 | - end; | |
| 854 | - Expect(sRparen); | |
| 855 | -end; | |
| 856 | - | |
| 857 | -procedure ConstructCase(var x: TItem; c: PObj; const at: TPos); | |
| 858 | -var f: PObj; a: TItem; | |
| 859 | -begin | |
| 860 | - Construct(x, c); | |
| 861 | - f := c^.fields; | |
| 862 | - if tok = sLparen then begin | |
| 863 | - Next; | |
| 864 | - while f <> nil do begin | |
| 865 | - Expression(a, f^.typ); | |
| 866 | - SetField(x, f, a); | |
| 867 | - f := f^.next; | |
| 868 | - if f <> nil then Expect(sComma); | |
| 869 | - end; | |
| 870 | - Expect(sRparen); | |
| 871 | - end else if f <> nil then MarkAt(at, NameOf(c^.name) + ' needs its fields'); | |
| 872 | -end; | |
| 873 | - | |
| 874 | -{ x, before ".", has no fields; tok is what follows. Other languages | |
| 875 | - write p.f through a pointer and s.len for a length. } | |
| 876 | -procedure FieldOfWhat(const x: TItem); | |
| 877 | -var msg, f: shortstring; | |
| 878 | -begin | |
| 879 | - msg := 'expected a record, found ' + What(x); | |
| 880 | - if tok = sIdent then f := NameOf(id) else f := ''; | |
| 881 | - if (x.typ^.form = fPtr) and (x.typ^.base^.form = fRecord) then | |
| 882 | - MarkHelp(x.pos, msg, 'through a pointer, write ' + Source(x.pos) + '^.' + f) | |
| 883 | - else if (x.typ^.form in [fArray, fSlice]) and ((f = 'len') or (f = 'length') or (f = 'size') or (f = 'count')) then | |
| 884 | - MarkHelp(x.pos, msg, 'its length is LEN(' + Source(x.pos) + ')'); | |
| 885 | - MarkAt(x.pos, msg); | |
| 886 | -end; | |
| 887 | - | |
| 888 | -{ A designator, with any selectors and calls after it. called tells | |
| 889 | - whether the last thing done was a call. } | |
| 890 | -procedure Designator(var x: TItem; var called: boolean); | |
| 891 | -var o, f: PObj; y, z: TItem; t: PType; start: TPos; | |
| 892 | -begin | |
| 893 | - start := TokPos; | |
| 894 | - o := QualIdent; | |
| 895 | - called := false; | |
| 896 | - if o^.cls = oModule then MarkAt(SpanFrom(start), NameOf(o^.name) + ' is a module, not a value'); | |
| 897 | - MakeItem(x, o); | |
| 898 | - if x.mode = mBuiltin then begin | |
| 899 | - Builtin(x); | |
| 900 | - called := true; | |
| 901 | - end else if x.mode = mCase then begin | |
| 902 | - ConstructCase(x, o, SpanFrom(start)); | |
| 903 | - end; | |
| 904 | - repeat | |
| 905 | - x.pos := SpanFrom(start); | |
| 906 | - case tok of | |
| 907 | - sPeriod: begin | |
| 908 | - Next; | |
| 909 | - if (x.mode <> mVar) or (x.typ^.form <> fRecord) then FieldOfWhat(x); | |
| 910 | - if tok <> sIdent then Expected('a field'); | |
| 911 | - f := FieldOf(x.typ); | |
| 912 | - Next; | |
| 913 | - Field(x, f); | |
| 914 | - called := false; | |
| 915 | - end; | |
| 916 | - sArrow: begin Next; Deref(x); called := false end; | |
| 917 | - sLbrak: begin | |
| 918 | - Next; | |
| 919 | - IndexPrep(x); | |
| 920 | - Expression(y, nil); | |
| 921 | - if tok = sUpto then begin | |
| 922 | - Next; | |
| 923 | - SliceLo(y); | |
| 924 | - Expression(z, nil); | |
| 925 | - Slice(x, y, z); | |
| 926 | - end else Index(x, y); | |
| 927 | - Expect(sRbrak); | |
| 928 | - called := false; | |
| 929 | - end; | |
| 930 | - sLparen: begin | |
| 931 | - if x.mode = mType then begin | |
| 932 | - t := x.typ; | |
| 933 | - Next; | |
| 934 | - Expression(x, nil); | |
| 935 | - Expect(sRparen); | |
| 936 | - Convert(x, t); | |
| 937 | - end else begin | |
| 938 | - if x.mode = mProc then lastCalled := x.obj else lastCalled := nil; | |
| 939 | - Call(x); | |
| 940 | - called := true; | |
| 941 | - end; | |
| 942 | - end; | |
| 943 | - else break; | |
| 944 | - end; | |
| 945 | - until false; | |
| 946 | - x.pos := SpanFrom(start); | |
| 947 | - NilTest(x); | |
| 948 | -end; | |
| 949 | - | |
| 950 | -procedure ArrayLiteral(var x: TItem; want: PType); | |
| 951 | -var | |
| 952 | - et, at: PType; e: TItem; slots: array[0..1023] of int64; n, i: longint; | |
| 953 | - off, es: int64; start: TPos; | |
| 954 | -begin | |
| 955 | - start := TokPos; | |
| 956 | - if (want = nil) or not (want^.form in [fArray, fSlice]) then | |
| 957 | - Mark('an array literal needs a known array or slice type'); | |
| 958 | - et := want^.base; | |
| 959 | - es := et^.size; | |
| 960 | - Next; | |
| 961 | - n := 0; | |
| 962 | - if tok <> sRbrak then | |
| 963 | - repeat | |
| 964 | - if n > High(slots) then Mark('more than ' + IntStr(High(slots) + 1) + ' elements'); | |
| 965 | - Expression(e, et); | |
| 966 | - Coerce(e, et); | |
| 967 | - if IsScalar(et) then slots[n] := ToSlot(e) | |
| 968 | - else begin | |
| 969 | - if e.isVar or e.vol or (e.mode <> mVar) or (e.base <> bFrame) then CopyToTemp(e); | |
| 970 | - slots[n] := e.off; | |
| 971 | - end; | |
| 972 | - inc(n); | |
| 973 | - if tok <> sComma then break; | |
| 974 | - Next; | |
| 975 | - until false; | |
| 976 | - Expect(sRbrak); | |
| 977 | - if (want^.form = fArray) and (n <> want^.len) then MarkAt(SpanFrom(start), 'expected ' + IntStr(want^.len) + ' elements, found ' + IntStr(n)); | |
| 978 | - if n = 0 then begin | |
| 979 | - PushConst(kWord, 0); | |
| 980 | - SliceOf(x, want, 0); | |
| 981 | - exit; | |
| 982 | - end; | |
| 983 | - if want^.form = fArray then begin | |
| 984 | - at := want; | |
| 985 | - off := AllocTemp(at^.size, at^.align); | |
| 986 | - end else begin | |
| 987 | - at := NewType(fArray, n * es, et^.align); | |
| 988 | - at^.len := n; | |
| 989 | - at^.base := et; | |
| 990 | - off := AllocPerm(at^.size, at^.align); { lives as long as the call } | |
| 991 | - end; | |
| 992 | - for i := 0 to n - 1 do | |
| 993 | - if IsScalar(et) then begin | |
| 994 | - PushVar(FrameAdr(slots[i]), 8, false, KindOf(et)); | |
| 995 | - PopVar(FrameAdr(off + i * es), ScalarSize(et)); | |
| 996 | - end else begin | |
| 997 | - PushAdr(FrameAdr(slots[i])); | |
| 998 | - CopyTo(FrameAdr(off + i * es), es); | |
| 999 | - end; | |
| 1000 | - TempItem(x, at, off); | |
| 1001 | -end; | |
| 1002 | - | |
| 1003 | -{ A record literal: a zeroed temporary, with the fields named filled in. } | |
| 1004 | -procedure RecordLiteral(var x: TItem; t: PType); | |
| 1005 | -var f: PObj; a: TItem; seen: array[0..63] of longint; n, i: longint; | |
| 1006 | -begin | |
| 1007 | - Next; | |
| 1008 | - ZeroTemp(x, t); | |
| 1009 | - FillChar(seen, SizeOf(seen), 0); | |
| 1010 | - n := 0; | |
| 1011 | - if tok <> sRbrace then | |
| 1012 | - repeat | |
| 1013 | - if tok <> sIdent then Expected('a field'); | |
| 1014 | - f := FieldOf(t); | |
| 1015 | - for i := 0 to n - 1 do if seen[i] = id then Mark('field ' + NameOf(id) + ' is given twice'); | |
| 1016 | - if n <= High(seen) then begin seen[n] := id; inc(n) end; | |
| 1017 | - Next; | |
| 1018 | - Expect(sColon); | |
| 1019 | - Expression(a, f^.typ); | |
| 1020 | - SetField(x, f, a); | |
| 1021 | - if tok <> sComma then break; | |
| 1022 | - Next; | |
| 1023 | - until false; | |
| 1024 | - Expect(sRbrace); | |
| 1025 | -end; | |
| 1026 | - | |
| 1027 | -{ A set literal: the bits of the constant names, or'd at run time with | |
| 1028 | - those of the others. } | |
| 1029 | -procedure SetLiteral(var x: TItem; t: PType); | |
| 1030 | -var e: TItem; bits, slot: int64; start: TPos; s: shortstring; | |
| 1031 | -begin | |
| 1032 | - start := TokPos; | |
| 1033 | - Next; | |
| 1034 | - if (t <> nil) and (t^.form <> fSet) then MarkAt(SpanFrom(start), 'expected a set type, found ' + TypeName(t)); | |
| 1035 | - bits := 0; | |
| 1036 | - slot := 0; | |
| 1037 | - if tok <> sRbrace then | |
| 1038 | - repeat | |
| 1039 | - if t = nil then Expression(e, nil) else Expression(e, t^.base); | |
| 1040 | - if t = nil then begin | |
| 1041 | - if e.typ^.form <> fEnum then MarkAt(e.pos, 'a set holds names of an enumeration, found ' + What(e)); | |
| 1042 | - t := SetOf(e.typ, e.pos); | |
| 1043 | - end; | |
| 1044 | - if not Same(e.typ, t^.base) then begin | |
| 1045 | - if e.obj <> nil then s := NameOf(e.obj^.name) else s := What(e); | |
| 1046 | - MarkAt(e.pos, s + ' isn''t a name of ' + TypeName(t^.base)); | |
| 1047 | - end; | |
| 1048 | - if e.mode = mConst then bits := bits or (int64(1) shl e.val) | |
| 1049 | - else begin | |
| 1050 | - if slot = 0 then begin | |
| 1051 | - slot := AllocTemp(8, 8); | |
| 1052 | - PushConst(kWord, 0); | |
| 1053 | - PopVar(FrameAdr(slot), 8); | |
| 1054 | - end; | |
| 1055 | - Value(e); | |
| 1056 | - PushConst(kWord, 1); | |
| 1057 | - Swap; | |
| 1058 | - Op(sShl, kWord, 8, false); | |
| 1059 | - PushVar(FrameAdr(slot), 8, false, kWord); | |
| 1060 | - Op(sBar, kWord, 8, false); | |
| 1061 | - PopVar(FrameAdr(slot), 8); | |
| 1062 | - end; | |
| 1063 | - if tok <> sComma then break; | |
| 1064 | - Next; | |
| 1065 | - until false; | |
| 1066 | - Expect(sRbrace); | |
| 1067 | - if t = nil then MarkAt(SpanFrom(start), '{} needs a known set type'); | |
| 1068 | - MakeConst(x, t, bits); | |
| 1069 | - if slot <> 0 then begin | |
| 1070 | - PushVar(FrameAdr(slot), 8, false, kWord); | |
| 1071 | - PushConst(kWord, bits); | |
| 1072 | - Op(sBar, kWord, 8, false); | |
| 1073 | - x.mode := mStack; | |
| 1074 | - end; | |
| 1075 | -end; | |
| 1076 | - | |
| 1077 | -procedure RunDefers(base: longint); forward; | |
| 1078 | - | |
| 1079 | -{ The type of the last result of t, the results of a procedure: t if | |
| 1080 | - it has one, else the last of its several. } | |
| 1081 | -function LastResult(t: PType): PType; | |
| 1082 | -var f: PObj; | |
| 1083 | -begin | |
| 1084 | - if not t^.tuple then exit(t); | |
| 1085 | - f := t^.fields; | |
| 1086 | - while f^.next <> nil do f := f^.next; | |
| 1087 | - LastResult := f^.typ; | |
| 1088 | -end; | |
| 1089 | - | |
| 1090 | -{ The results of the tuple t but the last: the same fields, one fewer } | |
| 1091 | -function WithoutLast(t: PType): PType; | |
| 1092 | -var r: PType; | |
| 1093 | -begin | |
| 1094 | - r := NewType(fRecord, t^.size, t^.align); | |
| 1095 | - r^.tuple := true; | |
| 1096 | - r^.fields := t^.fields; | |
| 1097 | - r^.len := t^.len - 1; | |
| 1098 | - WithoutLast := r; | |
| 1099 | -end; | |
| 1100 | - | |
| 1101 | -{ try c (the report, 7.8): the call c, then the error it returned, its | |
| 1102 | - last result: if that isn't nil, the procedure returns it, and a zero | |
| 1103 | - for each of its other results, after running the deferred statements | |
| 1104 | - in force. x is left c's other results: none (mNone), one (a value) or | |
| 1105 | - several (a tuple, for a multiple assignment). The error is kept in a | |
| 1106 | - slot of the frame, and tested there; the code for it, out of line, | |
| 1107 | - is made with the stack of values as it stands, and leaves it so, | |
| 1108 | - since what follows is the code for an error that is nil. } | |
| 1109 | -procedure TryCall(var x: TItem); | |
| 1110 | -var | |
| 1111 | - called: boolean; c: TCall; start, at: TPos; res: PType; last: PObj; slot: int64; ok: longint; | |
| 1112 | -begin | |
| 1113 | - start := TokPos; | |
| 1114 | - Next; | |
| 1115 | - if deferring then MarkAt(SpanFrom(start), 'try can''t leave a deferred statement'); | |
| 1116 | - if curResult = nil then | |
| 1117 | - MarkAt(SpanFrom(start), 'try needs a procedure that ends in an ERROR result, and this returns nothing') | |
| 1118 | - else if LastResult(curResult) <> tError then | |
| 1119 | - MarkAt(SpanFrom(start), 'try needs a procedure that ends in an ERROR result, and this returns ' + TypeName(curResult)); | |
| 1120 | - Designator(x, called); | |
| 1121 | - if not called and ((x.mode = mProc) or ((x.mode in [mVar, mStack]) and (x.typ^.form = fProc))) then begin | |
| 1122 | - { a procedure without parameters, written without parentheses } | |
| 1123 | - at := x.pos; | |
| 1124 | - BeginCall(c, x); | |
| 1125 | - EndCall(c, x); | |
| 1126 | - x.pos := at; | |
| 1127 | - called := true; | |
| 1128 | - end; | |
| 1129 | - if not called then MarkAt(x.pos, 'try needs a call, found ' + What(x)); | |
| 1130 | - if x.mode = mNone then | |
| 1131 | - MarkAt(x.pos, 'try needs a call that returns an ERROR last, and this returns nothing'); | |
| 1132 | - if LastResult(x.typ) <> tError then | |
| 1133 | - MarkAt(x.pos, 'try needs a call that returns an ERROR last, and this returns ' + TypeName(x.typ)); | |
| 1134 | - res := x.typ; | |
| 1135 | - if x.mode = mStack then begin | |
| 1136 | - { an ERROR alone } | |
| 1137 | - slot := AllocTemp(8, 8); | |
| 1138 | - PopVar(FrameAdr(slot), 8); | |
| 1139 | - x.mode := mNone; x.typ := tNone; | |
| 1140 | - end else if x.mode = mWords then begin | |
| 1141 | - { two words, the error on top } | |
| 1142 | - slot := AllocTemp(8, 8); | |
| 1143 | - PopVar(FrameAdr(slot), 8); | |
| 1144 | - x.mode := mStack; x.typ := res^.fields^.typ; | |
| 1145 | - end else begin | |
| 1146 | - { several, in a temporary: the error is where it is } | |
| 1147 | - last := res^.fields; | |
| 1148 | - while last^.next <> nil do last := last^.next; | |
| 1149 | - slot := x.off + last^.val; | |
| 1150 | - if res^.len = 2 then Field(x, res^.fields) else x.typ := WithoutLast(res); | |
| 1151 | - end; | |
| 1152 | - ok := NoLabel; | |
| 1153 | - PushVar(FrameAdr(slot), 8, false, kWord); | |
| 1154 | - JumpIf(ok, false); | |
| 1155 | - if numDefers > 0 then TryAlign; | |
| 1156 | - RunDefers(0); | |
| 1157 | - TryReturn(slot); | |
| 1158 | - Fix(ok); | |
| 1159 | - x.pos := SpanFrom(start); | |
| 1160 | -end; | |
| 1161 | - | |
| 1162 | -procedure Factor(var x: TItem; want: PType); | |
| 1163 | -var called, vol, several: boolean; t: PType; start: TPos; s: shortstring; | |
| 1164 | -begin | |
| 1165 | - several := passOn; | |
| 1166 | - passOn := false; | |
| 1167 | - start := TokPos; | |
| 1168 | - case tok of | |
| 1169 | - sInt, sChar: begin | |
| 1170 | - { only a hex number can be above high(I64), and it is a U64 } | |
| 1171 | - if ival < 0 then MakeConst(x, tU64, ival) else MakeConst(x, tConst, ival); | |
| 1172 | - Next; | |
| 1173 | - end; | |
| 1174 | - sReal: begin MakeReal(x, rval); UsesReals(TokPos); Next end; | |
| 1175 | - sString: begin MakeStr(x); Next end; | |
| 1176 | - sIdent: begin | |
| 1177 | - Designator(x, called); | |
| 1178 | - if x.mode = mNone then begin | |
| 1179 | - if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure'; | |
| 1180 | - MarkAt(x.pos, s + ' returns nothing'); | |
| 1181 | - end; | |
| 1182 | - if x.typ^.tuple and not several then begin | |
| 1183 | - if lastCalled <> nil then s := NameOf(lastCalled^.name) else s := 'this procedure'; | |
| 1184 | - MarkHelp(x.pos, s + ' returns several results', 'they go to variables: a, b := ' + s + '(...)'); | |
| 1185 | - if x.mode = mWords then CopyToTemp(x); | |
| 1186 | - end; | |
| 1187 | - end; | |
| 1188 | - sLparen: begin Next; Expression(x, want); Expect(sRparen) end; | |
| 1189 | - sTry: begin | |
| 1190 | - TryCall(x); | |
| 1191 | - if x.mode = mNone then MarkAt(x.pos, 'this try has no value'); | |
| 1192 | - if x.typ^.tuple then | |
| 1193 | - MarkHelp(x.pos, 'this try has several values', 'they go to variables: a, b := try f(...)'); | |
| 1194 | - end; | |
| 1195 | - sNot: begin Next; Factor(x, nil); NotOp(x) end; | |
| 1196 | - sTilde: begin Next; Factor(x, nil); Complement(x) end; | |
| 1197 | - sLbrak: ArrayLiteral(x, want); | |
| 1198 | - sLbrace: if (want <> nil) and (want^.form = fRecord) then RecordLiteral(x, want) | |
| 1199 | - else SetLiteral(x, want); | |
| 1200 | - sArrow: begin | |
| 1201 | - Next; | |
| 1202 | - vol := tok = sVolatile; | |
| 1203 | - if vol then Next; | |
| 1204 | - t := NewPtr(ParseType, vol); | |
| 1205 | - Expect(sLparen); | |
| 1206 | - Expression(x, nil); | |
| 1207 | - Expect(sRparen); | |
| 1208 | - Convert(x, t); | |
| 1209 | - { a conversion is a factor, not a designator: nothing selects from it } | |
| 1210 | - if tok = sArrow then | |
| 1211 | - MarkHelp(TokPos, 'a conversion can''t be dereferenced', 'convert into a pointer variable, then dereference that'); | |
| 1212 | - end; | |
| 1213 | - else Expected('an expression'); | |
| 1214 | - end; | |
| 1215 | - x.pos := SpanFrom(start); | |
| 1216 | -end; | |
| 1217 | - | |
| 1218 | -procedure Term(var x: TItem; want: PType); | |
| 1219 | -var op: TSym; y: TItem; L: longint; start: TPos; | |
| 1220 | -begin | |
| 1221 | - start := TokPos; | |
| 1222 | - Factor(x, want); | |
| 1223 | - while tok in [sTimes, sSlash, sDiv, sMod, sAmp, sShl, sShr] do begin | |
| 1224 | - op := tok; | |
| 1225 | - Next; | |
| 1226 | - Op1(op, x, L); | |
| 1227 | - Factor(y, nil); | |
| 1228 | - Op2(op, x, y, L); | |
| 1229 | - x.pos := SpanFrom(start); | |
| 1230 | - end; | |
| 1231 | -end; | |
| 1232 | - | |
| 1233 | -procedure SimpleExpr(var x: TItem; want: PType); | |
| 1234 | -var op: TSym; y: TItem; L: longint; start: TPos; | |
| 1235 | -begin | |
| 1236 | - start := TokPos; | |
| 1237 | - if tok = sMinus then begin | |
| 1238 | - Next; | |
| 1239 | - Term(x, nil); | |
| 1240 | - x.pos := SpanFrom(start); | |
| 1241 | - Neg(x); | |
| 1242 | - end else Term(x, want); | |
| 1243 | - while tok in [sPlus, sMinus, sBar, sXor] do begin | |
| 1244 | - op := tok; | |
| 1245 | - Next; | |
| 1246 | - Op1(op, x, L); | |
| 1247 | - Term(y, nil); | |
| 1248 | - Op2(op, x, y, L); | |
| 1249 | - x.pos := SpanFrom(start); | |
| 1250 | - end; | |
| 1251 | -end; | |
| 1252 | - | |
| 1253 | -procedure Relation(var x: TItem; want: PType); | |
| 1254 | -var op: TSym; y: TItem; L: longint; | |
| 1255 | -begin | |
| 1256 | - SimpleExpr(x, want); | |
| 1257 | - if tok in [sEql, sNeq, sLss, sLeq, sGtr, sGeq, sIn] then begin | |
| 1258 | - op := tok; | |
| 1259 | - Next; | |
| 1260 | - Op1(op, x, L); | |
| 1261 | - if op = sIn then SimpleExpr(y, nil) else SimpleExpr(y, x.typ); | |
| 1262 | - Op2(op, x, y, L); | |
| 1263 | - x.pos := Join(x.pos, y.pos); | |
| 1264 | - end; | |
| 1265 | -end; | |
| 1266 | - | |
| 1267 | -{ and binds tighter than or, and both below the relations } | |
| 1268 | -procedure Conjunct(var x: TItem; want: PType); | |
| 1269 | -var y: TItem; L: longint; start: TPos; | |
| 1270 | -begin | |
| 1271 | - start := TokPos; | |
| 1272 | - Relation(x, want); | |
| 1273 | - while tok = sAnd do begin | |
| 1274 | - Next; | |
| 1275 | - Op1(sAnd, x, L); | |
| 1276 | - Relation(y, nil); | |
| 1277 | - Op2(sAnd, x, y, L); | |
| 1278 | - x.pos := SpanFrom(start); | |
| 1279 | - end; | |
| 1280 | -end; | |
| 1281 | - | |
| 1282 | -procedure Expression(var x: TItem; want: PType); | |
| 1283 | -var y: TItem; L: longint; start: TPos; | |
| 1284 | -begin | |
| 1285 | - start := TokPos; | |
| 1286 | - Conjunct(x, want); | |
| 1287 | - while tok = sOr do begin | |
| 1288 | - Next; | |
| 1289 | - Op1(sOr, x, L); | |
| 1290 | - Conjunct(y, nil); | |
| 1291 | - Op2(sOr, x, y, L); | |
| 1292 | - x.pos := SpanFrom(start); | |
| 1293 | - end; | |
| 1294 | -end; | |
| 1295 | - | |
| 1296 | -{ Statements } | |
| 1297 | - | |
| 1298 | -procedure Loop(var lp: TLoop; var old: PLoop); | |
| 1299 | -begin | |
| 1300 | - lp.brk := NoLabel; | |
| 1301 | - lp.base := numDefers; | |
| 1302 | - old := curLoop; | |
| 1303 | - curLoop := @lp; | |
| 1304 | -end; | |
| 1305 | - | |
| 1306 | -{ Runs the deferred statements in force above base, newest first. } | |
| 1307 | -procedure RunDefers(base: longint); | |
| 1308 | -var i: longint; | |
| 1309 | -begin | |
| 1310 | - for i := numDefers - 1 downto base do RunDefer(defers[i]); | |
| 1311 | -end; | |
| 1312 | - | |
| 1313 | -{ Whether what follows "then" is a lone "break;", with no deferred | |
| 1314 | - statement to run on the way out of the loop: the break must be the | |
| 1315 | - branch's last statement, so ";" and then end, elsif or else follow. } | |
| 1316 | -function LoneBreak: boolean; | |
| 1317 | -var st: TScanState; | |
| 1318 | -begin | |
| 1319 | - LoneBreak := false; | |
| 1320 | - if (tok <> sBreak) or (curLoop = nil) or (numDefers <> curLoop^.base) then exit; | |
| 1321 | - SaveScan(st); | |
| 1322 | - Next; | |
| 1323 | - if tok = sSemicolon then begin | |
| 1324 | - Next; | |
| 1325 | - LoneBreak := tok in [sEnd, sElsif, sElse]; | |
| 1326 | - end; | |
| 1327 | - RestoreScan(st); | |
| 1328 | -end; | |
| 1329 | - | |
| 1330 | -{ The condition x of if or elsif, and the statements it guards, which | |
| 1331 | - L skips when it is false. "if c then break; end" is one jump out of | |
| 1332 | - the loop when c holds, not one past a jump out. } | |
| 1333 | -procedure ThenPart(var x: TItem; var L: longint); | |
| 1334 | -begin | |
| 1335 | - Expect(sThen); | |
| 1336 | - L := NoLabel; { stays so with a lone break: there is nothing to skip } | |
| 1337 | - if LoneBreak then begin | |
| 1338 | - NoteLine(CurLine, CurCol); { the jump gets the break's line } | |
| 1339 | - CondJumpTrue(x, curLoop^.brk); | |
| 1340 | - Next; Next; { skip "break" and ";" } | |
| 1341 | - ends := true; | |
| 1342 | - end else begin | |
| 1343 | - CondJump(x, L); | |
| 1344 | - Statements; | |
| 1345 | - end; | |
| 1346 | -end; | |
| 1347 | - | |
| 1348 | -{ if, elsif and else: each condition's jump skips its branch to the | |
| 1349 | - next; each branch that doesn't end jumps to the end. A branch that | |
| 1350 | - ends, in return or break, needs no jump past the others, and all of | |
| 1351 | - them ending is told to the caller in the global ends. } | |
| 1352 | -procedure IfStatement(const start: TPos; ind: longint); | |
| 1353 | -var x: TItem; L, endL: longint; all: boolean; | |
| 1354 | -begin | |
| 1355 | - Next; | |
| 1356 | - Expression(x, nil); | |
| 1357 | - ThenPart(x, L); | |
| 1358 | - all := ends; | |
| 1359 | - endL := NoLabel; | |
| 1360 | - while tok = sElsif do begin | |
| 1361 | - if not ends then Jump(endL); | |
| 1362 | - Fix(L); | |
| 1363 | - Next; | |
| 1364 | - Expression(x, nil); | |
| 1365 | - ThenPart(x, L); | |
| 1366 | - all := all and ends; | |
| 1367 | - end; | |
| 1368 | - if tok = sElse then begin | |
| 1369 | - if not ends then Jump(endL); | |
| 1370 | - Fix(L); | |
| 1371 | - Next; | |
| 1372 | - if tok = sIf then elseIf := true; | |
| 1373 | - Statements; | |
| 1374 | - all := all and ends; | |
| 1375 | - end else begin | |
| 1376 | - Fix(L); | |
| 1377 | - all := false; | |
| 1378 | - end; | |
| 1379 | - CloseEnd(start, ind); | |
| 1380 | - Fix(endL); | |
| 1381 | - ends := all; | |
| 1382 | -end; | |
| 1383 | - | |
| 1384 | -{ while c do s end: the test comes after the body, and the loop jumps to | |
| 1385 | - it first, so that a round takes one jump, back to the top if c holds: | |
| 1386 | - | |
| 1387 | - jmp test rather than top: c, jump to out if false | |
| 1388 | - top: s s | |
| 1389 | - test: c, jump to top if true jmp top | |
| 1390 | - out: | |
| 1391 | - | |
| 1392 | - One pass reads c before s: so c is compiled where it is written, for | |
| 1393 | - its messages, and its code taken back (Rewind); then read again, and | |
| 1394 | - compiled, after s (SaveScan, RestoreScan). } | |
| 1395 | -procedure WhileStatement(const start: TPos; ind: longint); | |
| 1396 | -var x: TItem; top, L, test: longint; lp: TLoop; old: PLoop; cond, after: TScanState; m: TCodeMark; | |
| 1397 | -begin | |
| 1398 | - Next; | |
| 1399 | - SaveScan(cond); | |
| 1400 | - MarkCode(m); | |
| 1401 | - Expression(x, nil); | |
| 1402 | - Discard(x); | |
| 1403 | - Rewind(m); | |
| 1404 | - Expect(sDo); | |
| 1405 | - test := NoLabel; | |
| 1406 | - Jump(test); | |
| 1407 | - AlignLoop; | |
| 1408 | - top := pc; | |
| 1409 | - Loop(lp, old); | |
| 1410 | - Statements; | |
| 1411 | - curLoop := old; | |
| 1412 | - SaveScan(after); | |
| 1413 | - RestoreScan(cond); | |
| 1414 | - Fix(test); | |
| 1415 | - NoteLine(CurLine, CurCol); | |
| 1416 | - Expression(x, nil); | |
| 1417 | - ends := (x.mode = mConst) and (x.val <> 0) and (lp.brk = NoLabel); { while true } | |
| 1418 | - if (x.mode = mConst) and (x.typ^.form = fBool) then begin | |
| 1419 | - if x.val <> 0 then JumpBack(top); | |
| 1420 | - end else begin | |
| 1421 | - L := NoLabel; | |
| 1422 | - CondJumpTrue(x, L); | |
| 1423 | - FixTo(L, top); | |
| 1424 | - end; | |
| 1425 | - RestoreScan(after); | |
| 1426 | - CloseEnd(start, ind); | |
| 1427 | - Fix(lp.brk); | |
| 1428 | -end; | |
| 1429 | - | |
| 1430 | -procedure RepeatStatement; | |
| 1431 | -var x: TItem; top, L: longint; lp: TLoop; old: PLoop; | |
| 1432 | -begin | |
| 1433 | - Next; | |
| 1434 | - top := pc; | |
| 1435 | - Loop(lp, old); | |
| 1436 | - Statements; | |
| 1437 | - curLoop := old; | |
| 1438 | - Expect(sUntil); | |
| 1439 | - Expression(x, nil); | |
| 1440 | - ends := (x.mode = mConst) and (x.val = 0) and (lp.brk = NoLabel); { until false } | |
| 1441 | - L := NoLabel; | |
| 1442 | - CondJump(x, L); | |
| 1443 | - FixTo(L, top); | |
| 1444 | - Fix(lp.brk); | |
| 1445 | -end; | |
| 1446 | - | |
| 1447 | -{ for v := a to b by s do ... end, laid out as | |
| 1448 | - v := a; the limit b, in a slot unless it is a constant | |
| 1449 | - if v is past b then goto exit | |
| 1450 | - goto body | |
| 1451 | - top: v := v + s | |
| 1452 | - body: the statements | |
| 1453 | - the end of a round: goto top, or exit (ForNext) | |
| 1454 | - exit: | |
| 1455 | - so that a round of a loop by 1 takes one compare and one jump. } | |
| 1456 | -procedure ForStatement(const start: TPos; ind: longint); | |
| 1457 | -var | |
| 1458 | - o: PObj; v, a, b, s: TItem; step: int64; top, body, bodyL, exitL: longint; | |
| 1459 | - lp: TLoop; old: PLoop; | |
| 1460 | -begin | |
| 1461 | - Next; | |
| 1462 | - if tok <> sIdent then Expected('a name'); | |
| 1463 | - o := Lookup(id); | |
| 1464 | - if o = nil then begin NotDeclared(TokPos, id); Fail end; | |
| 1465 | - Named(o); | |
| 1466 | - if (o^.cls <> oVar) or not (o^.typ^.form in [fInt, fEnum]) then | |
| 1467 | - if o^.cls = oVar then | |
| 1468 | - Mark('a for loop needs an integer or enumeration variable, found ' + TypeName(o^.typ)) | |
| 1469 | - else Mark(NameOf(id) + ' isn''t a variable'); | |
| 1470 | - Next; | |
| 1471 | - Expect(sBecomes); | |
| 1472 | - MakeItem(v, o); | |
| 1473 | - Expression(a, o^.typ); | |
| 1474 | - CoerceExact(a, o^.typ, ''); | |
| 1475 | - Assign(v, a); | |
| 1476 | - Expect(sTo); | |
| 1477 | - Expression(b, o^.typ); | |
| 1478 | - CoerceExact(b, o^.typ, ''); | |
| 1479 | - { the limit, evaluated once: a constant as it is, else in a slot } | |
| 1480 | - if b.mode <> mConst then TempItem(b, b.typ, ToSlot(b)); | |
| 1481 | - step := 1; | |
| 1482 | - if tok = sBy then begin | |
| 1483 | - Next; | |
| 1484 | - Expression(s, nil); | |
| 1485 | - step := ConstValue(s); | |
| 1486 | - if step = 0 then MarkAt(s.pos, 'the step can''t be zero'); | |
| 1487 | - end; | |
| 1488 | - Expect(sDo); | |
| 1489 | - exitL := NoLabel; | |
| 1490 | - if step > 0 then ForCmp(o, b, sGtr, exitL) else ForCmp(o, b, sLss, exitL); | |
| 1491 | - bodyL := NoLabel; | |
| 1492 | - Jump(bodyL); | |
| 1493 | - AlignLoop; | |
| 1494 | - top := pc; | |
| 1495 | - ForStep(o, step); | |
| 1496 | - Fix(bodyL); | |
| 1497 | - body := pc; | |
| 1498 | - Loop(lp, old); | |
| 1499 | - Statements; | |
| 1500 | - curLoop := old; | |
| 1501 | - CloseEnd(start, ind); | |
| 1502 | - ForNext(o, b, step, exitL, top, body); | |
| 1503 | - Fix(exitL); | |
| 1504 | - Fix(lp.brk); | |
| 1505 | -end; | |
| 1506 | - | |
| 1507 | -type | |
| 1508 | - TLabels = record | |
| 1509 | - lo, hi: array[0..1023] of int64; { the ranges of the labels so far } | |
| 1510 | - at: array[0..1023] of longint; { where the arm of each starts } | |
| 1511 | - n: longint; | |
| 1512 | - end; | |
| 1513 | - | |
| 1514 | -{ a < b, for values of an integer type, unsigned or not } | |
| 1515 | -function Below(a, b: int64; unsign: boolean): boolean; | |
| 1516 | -begin | |
| 1517 | - if unsign then Below := qword(a) < qword(b) else Below := a < b; | |
| 1518 | -end; | |
| 1519 | - | |
| 1520 | -{ An arm of a match on an integer of type t: its labels, each a | |
| 1521 | - constant or a range, none overlapping another (report section 7.4), | |
| 1522 | - then its statements. No code tests the labels here; Dispatch does, | |
| 1523 | - after the last arm, for which each label notes where its arm starts. } | |
| 1524 | -procedure IntArm(t: PType; var ls: TLabels; var endL: longint); | |
| 1525 | -var lo, hi: TItem; first, i: longint; u: boolean; start: TPos; | |
| 1526 | -begin | |
| 1527 | - u := not t^.signed; | |
| 1528 | - first := ls.n; | |
| 1529 | - repeat | |
| 1530 | - start := TokPos; | |
| 1531 | - Expression(lo, t); | |
| 1532 | - Coerce(lo, t); | |
| 1533 | - ConstValue(lo); | |
| 1534 | - hi := lo; | |
| 1535 | - if tok = sUpto then begin | |
| 1536 | - Next; | |
| 1537 | - Expression(hi, t); | |
| 1538 | - Coerce(hi, t); | |
| 1539 | - ConstValue(hi); | |
| 1540 | - if Below(hi.val, lo.val, u) then MarkAt(SpanFrom(start), 'the range is empty'); | |
| 1541 | - end; | |
| 1542 | - for i := 0 to ls.n - 1 do | |
| 1543 | - if not Below(hi.val, ls.lo[i], u) and not Below(ls.hi[i], lo.val, u) then | |
| 1544 | - if lo.val = hi.val then MarkAt(SpanFrom(start), Source(SpanFrom(start)) + ' is in another label too') | |
| 1545 | - else MarkAt(SpanFrom(start), Source(SpanFrom(start)) + ' overlaps another label'); | |
| 1546 | - if ls.n > High(ls.lo) then MarkAt(SpanFrom(start), 'more than ' + IntStr(High(ls.lo) + 1) + ' labels in one match'); | |
| 1547 | - ls.lo[ls.n] := lo.val; ls.hi[ls.n] := hi.val; | |
| 1548 | - inc(ls.n); | |
| 1549 | - if tok <> sComma then break; | |
| 1550 | - Next; | |
| 1551 | - until false; | |
| 1552 | - Expect(sColon); | |
| 1553 | - for i := first to ls.n - 1 do ls.at[i] := pc; | |
| 1554 | - Statements; | |
| 1555 | - Jump(endL); | |
| 1556 | -end; | |
| 1557 | - | |
| 1558 | -const | |
| 1559 | - MinTable = 4; { fewer labels are tested one by one } | |
| 1560 | - TableDensity = 8; { a table has at most this many entries to a label } | |
| 1561 | - | |
| 1562 | -{ The code that picks the arm, after the last one, which the match's | |
| 1563 | - start jumped to: label by label, as a chain of compares each to its | |
| 1564 | - arm; or, for four or more labels not far apart, a jump table (JumpTable), | |
| 1565 | - which takes the same few instructions for any of them. It is here and | |
| 1566 | - not before the arms, since a table can only be made once every arm's | |
| 1567 | - place is known; an arm is entered by a jump back to it. elseAt is where | |
| 1568 | - the else arm starts, or NoLabel. } | |
| 1569 | -procedure Dispatch(t: PType; p: int64; const ls: TLabels; elseAt: longint; var endL: longint); | |
| 1570 | -var | |
| 1571 | - lo, hi: int64; i, start: longint; v: int64; u: boolean; body: longint; | |
| 1572 | - entry: array[0..1023] of longint; | |
| 1573 | -begin | |
| 1574 | - u := (t^.form = fInt) and not t^.signed; | |
| 1575 | - lo := 0; hi := 0; | |
| 1576 | - if ls.n > 0 then begin | |
| 1577 | - lo := ls.lo[0]; hi := ls.hi[0]; | |
| 1578 | - for i := 1 to ls.n - 1 do begin | |
| 1579 | - if Below(ls.lo[i], lo, u) then lo := ls.lo[i]; | |
| 1580 | - if Below(hi, ls.hi[i], u) then hi := ls.hi[i]; | |
| 1581 | - end; | |
| 1582 | - end; | |
| 1583 | - if (ls.n >= MinTable) and (lo >= low(longint)) and (lo <= high(longint)) | |
| 1584 | - and (qword(hi - lo) < qword(TableDensity) * ls.n) and (hi - lo <= High(entry)) then begin | |
| 1585 | - start := JumpTable(p, lo, hi - lo + 1, elseAt, endL); | |
| 1586 | - for v := 0 to hi - lo do | |
| 1587 | - if elseAt <> NoLabel then entry[v] := elseAt else entry[v] := start; | |
| 1588 | - for i := 0 to ls.n - 1 do | |
| 1589 | - for v := ls.lo[i] - lo to ls.hi[i] - lo do entry[v] := ls.at[i]; | |
| 1590 | - for v := 0 to hi - lo do TableEntry(start, entry[v]); | |
| 1591 | - exit; | |
| 1592 | - end; | |
| 1593 | - for i := 0 to ls.n - 1 do begin | |
| 1594 | - body := NoLabel; | |
| 1595 | - MatchRange(p, ls.lo[i], ls.hi[i], body); | |
| 1596 | - FixTo(body, ls.at[i]); | |
| 1597 | - end; | |
| 1598 | - if elseAt <> NoLabel then JumpBack(elseAt); | |
| 1599 | -end; | |
| 1600 | - | |
| 1601 | -procedure MatchStatement(const start: TPos; ind: longint); | |
| 1602 | -var | |
| 1603 | - x: TItem; t: PType; c, f, o: PObj; p: int64; L, endL, n, first, disp, elseAt: longint; | |
| 1604 | - seen: array[0..255] of boolean; hasElse, all, byValue: boolean; endAt: TPos; ls: TLabels; | |
| 1605 | -begin | |
| 1606 | - Next; | |
| 1607 | - Expression(x, nil); | |
| 1608 | - if x.typ^.form = fConst then Coerce(x, tI64); | |
| 1609 | - p := MatchBegin(x); | |
| 1610 | - t := x.typ; | |
| 1611 | - Expect(sOf); | |
| 1612 | - FillChar(seen, SizeOf(seen), 0); | |
| 1613 | - hasElse := false; | |
| 1614 | - all := true; | |
| 1615 | - endL := NoLabel; | |
| 1616 | - ls.n := 0; | |
| 1617 | - { an integer or an enumeration is matched by its value, and the arms | |
| 1618 | - are picked by Dispatch, after them; a variant by its tag, arm by arm } | |
| 1619 | - byValue := t^.form in [fInt, fEnum]; | |
| 1620 | - disp := NoLabel; | |
| 1621 | - elseAt := NoLabel; | |
| 1622 | - if byValue then Jump(disp); | |
| 1623 | - while tok = sBar do begin | |
| 1624 | - Next; | |
| 1625 | - if hasElse then Mark('else must be the last arm'); | |
| 1626 | - if tok = sElse then begin | |
| 1627 | - Next; | |
| 1628 | - Expect(sColon); | |
| 1629 | - elseAt := pc; | |
| 1630 | - Statements; | |
| 1631 | - if byValue then Jump(endL); | |
| 1632 | - hasElse := true; | |
| 1633 | - end else if t^.form = fInt then | |
| 1634 | - IntArm(t, ls, endL) | |
| 1635 | - else begin | |
| 1636 | - first := ls.n; | |
| 1637 | - L := NoLabel; | |
| 1638 | - repeat | |
| 1639 | - if tok <> sIdent then Expected('a case'); | |
| 1640 | - c := t^.fields; | |
| 1641 | - while (c <> nil) and (c^.name <> id) do c := c^.next; | |
| 1642 | - if c = nil then Mark(NameOf(id) + ' isn''t a case of ' + TypeName(t)); | |
| 1643 | - if (c^.val <= High(seen)) and seen[c^.val] then Mark(NameOf(c^.name) + ' has two arms'); | |
| 1644 | - if c^.val <= High(seen) then seen[c^.val] := true; | |
| 1645 | - Next; | |
| 1646 | - if t^.form <> fEnum then begin MatchTag(p, c^.val, L); break end; | |
| 1647 | - { an enumeration's arm may name several, which share its place } | |
| 1648 | - ls.lo[ls.n] := c^.val; ls.hi[ls.n] := c^.val; | |
| 1649 | - inc(ls.n); | |
| 1650 | - if tok <> sComma then break; | |
| 1651 | - Next; | |
| 1652 | - until false; | |
| 1653 | - OpenScope(NewScope); | |
| 1654 | - if tok = sLparen then begin | |
| 1655 | - Next; | |
| 1656 | - f := c^.fields; | |
| 1657 | - n := 0; | |
| 1658 | - repeat | |
| 1659 | - if tok <> sIdent then Expected('a name'); | |
| 1660 | - if f = nil then Mark('more names than ' + NameOf(c^.name) + ' has fields'); | |
| 1661 | - o := NewObj(oVar, id, f^.typ); | |
| 1662 | - o^.lev := 1; o^.ind := true; o^.isVar := true; | |
| 1663 | - o^.val := p; o^.off := f^.val; | |
| 1664 | - Declare(o); | |
| 1665 | - f := f^.next; | |
| 1666 | - inc(n); | |
| 1667 | - Next; | |
| 1668 | - if tok <> sComma then break; | |
| 1669 | - Next; | |
| 1670 | - until false; | |
| 1671 | - if f <> nil then Mark('name all the fields of ' + NameOf(c^.name) + ', or none'); | |
| 1672 | - Expect(sRparen); | |
| 1673 | - end; | |
| 1674 | - Expect(sColon); | |
| 1675 | - for n := first to ls.n - 1 do ls.at[n] := pc; | |
| 1676 | - Statements; | |
| 1677 | - CloseScope; | |
| 1678 | - Jump(endL); | |
| 1679 | - if t^.form <> fEnum then Fix(L); | |
| 1680 | - end; | |
| 1681 | - all := all and ends; | |
| 1682 | - end; | |
| 1683 | - endAt := TokPos; | |
| 1684 | - CloseEnd(start, ind); | |
| 1685 | - ends := all; | |
| 1686 | - if (t^.form = fInt) and not hasElse then | |
| 1687 | - MarkHelp(endAt, 'a match on an integer needs an else arm', 'it may be empty: | else:'); | |
| 1688 | - if not hasElse then begin | |
| 1689 | - c := t^.fields; | |
| 1690 | - while c <> nil do begin | |
| 1691 | - if (c^.val > High(seen)) or not seen[c^.val] then MarkAt(endAt, 'no arm for ' + NameOf(c^.name)); | |
| 1692 | - c := c^.next; | |
| 1693 | - end; | |
| 1694 | - end; | |
| 1695 | - if byValue then begin | |
| 1696 | - Fix(disp); | |
| 1697 | - Dispatch(t, p, ls, elseAt, endL); | |
| 1698 | - end; | |
| 1699 | - Fix(endL); | |
| 1700 | -end; | |
| 1701 | - | |
| 1702 | -{ What a message calls x: its name n when x is just that name, else what | |
| 1703 | - it is. } | |
| 1704 | -function Subject(const x: TItem; n: longint): shortstring; | |
| 1705 | -begin | |
| 1706 | - if x.pos.b - x.pos.a = length(NameOf(n)) then Subject := NameOf(n) | |
| 1707 | - else if x.mode = mConst then Subject := 'a constant' | |
| 1708 | - else Subject := 'a value'; | |
| 1709 | -end; | |
| 1710 | - | |
| 1711 | -{ x, named first n, is a variable the program may assign: not an | |
| 1712 | - array constant, nor any part of one. } | |
| 1713 | -procedure Assignable(const x: TItem; n: longint); | |
| 1714 | -begin | |
| 1715 | - if (x.mode = mConst) and (Subject(x, n) = NameOf(n)) then | |
| 1716 | - MarkAt(x.pos, NameOf(n) + ' is a constant, and can''t be changed'); | |
| 1717 | - if (x.mode = mVar) and (x.obj <> nil) and (x.obj^.cls = oConst) then | |
| 1718 | - MarkAt(x.pos, NameOf(x.obj^.name) + ' is a constant, and can''t be changed'); | |
| 1719 | - if (x.mode <> mVar) or not x.isVar then MarkAt(x.pos, 'can''t assign to ' + Subject(x, n)); | |
| 1720 | -end; | |
| 1721 | - | |
| 1722 | -{ The designator a statement starts with. A variable named whole | |
| 1723 | - before := is assigned, not read: naming it there leaves it as unread | |
| 1724 | - as it was. } | |
| 1725 | -procedure LeftSide(var x: TItem; var called: boolean); | |
| 1726 | -var o: PObj; wasRead: boolean; n: longint; | |
| 1727 | -begin | |
| 1728 | - n := id; | |
| 1729 | - o := Lookup(n); | |
| 1730 | - wasRead := (o <> nil) and o^.read; | |
| 1731 | - Designator(x, called); | |
| 1732 | - if (tok in [sBecomes, sComma]) and (x.pos.b - x.pos.a = length(NameOf(n))) then | |
| 1733 | - o^.read := wasRead; | |
| 1734 | -end; | |
| 1735 | - | |
| 1736 | -{ a, b := f(x): the variables first, left to right, then the call; | |
| 1737 | - then each result to its variable, in order. x is the first variable, | |
| 1738 | - first its name, blank whether it is _. } | |
| 1739 | -procedure MultiAssign(var x: TItem; first: longint; blank: boolean); | |
| 1740 | -var | |
| 1741 | - d: array[0..15] of TItem; dn: array[0..15] of longint; blanks: array[0..15] of boolean; | |
| 1742 | - y, z: TItem; n, i: longint; called: boolean; f: PObj; | |
| 1743 | -begin | |
| 1744 | - n := 0; | |
| 1745 | - d[0] := x; | |
| 1746 | - dn[0] := first; | |
| 1747 | - blanks[0] := blank; | |
| 1748 | - repeat | |
| 1749 | - if not blanks[n] then begin | |
| 1750 | - NotReadOnly(d[n]); | |
| 1751 | - Assignable(d[n], dn[n]); | |
| 1752 | - Pin(d[n]); { works out the address now, so that the call can't change it } | |
| 1753 | - end; | |
| 1754 | - inc(n); | |
| 1755 | - if tok <> sComma then break; | |
| 1756 | - if n > High(d) then Mark('more than ' + IntStr(High(d) + 1) + ' variables in one assignment'); | |
| 1757 | - Next; | |
| 1758 | - blanks[n] := tok = sBlank; | |
| 1759 | - if blanks[n] then Next | |
| 1760 | - else begin | |
| 1761 | - dn[n] := id; | |
| 1762 | - LeftSide(d[n], called); | |
| 1763 | - end; | |
| 1764 | - until false; | |
| 1765 | - Expect(sBecomes); | |
| 1766 | - if tok = sTry then begin | |
| 1767 | - TryCall(y); | |
| 1768 | - called := true; | |
| 1769 | - end else Designator(y, called); | |
| 1770 | - if not called or not y.typ^.tuple or (y.typ^.len <> n) then begin | |
| 1771 | - MarkAt(y.pos, 'expected a call with ' + IntStr(n) + ' results'); | |
| 1772 | - if y.mode = mWords then CopyToTemp(y); | |
| 1773 | - end; | |
| 1774 | - { two results on the stack: the first is brought to the top, and each | |
| 1775 | - is popped into its variable in turn, as one value; else each is | |
| 1776 | - read from the temporary they came back in } | |
| 1777 | - if y.mode = mWords then Swap; | |
| 1778 | - f := y.typ^.fields; | |
| 1779 | - for i := 0 to n - 1 do begin | |
| 1780 | - z := y; | |
| 1781 | - if y.mode = mWords then begin z.mode := mStack; z.typ := f^.typ end | |
| 1782 | - else Field(z, f); | |
| 1783 | - if blanks[i] then Discard(z) | |
| 1784 | - else begin | |
| 1785 | - CoerceExact(z, d[i].typ, 'several results go to variables of just their types'); | |
| 1786 | - Assign(d[i], z); | |
| 1787 | - end; | |
| 1788 | - f := f^.next; | |
| 1789 | - end; | |
| 1790 | -end; | |
| 1791 | - | |
| 1792 | -{ return a, b; or return f(x); with f's several results passed on } | |
| 1793 | -procedure ReturnResults; | |
| 1794 | -var f: PObj; x: TItem; | |
| 1795 | -begin | |
| 1796 | - f := curResult^.fields; | |
| 1797 | - passOn := true; | |
| 1798 | - while f <> nil do begin | |
| 1799 | - Expression(x, f^.typ); | |
| 1800 | - if x.typ^.tuple then begin | |
| 1801 | - if not Same(x.typ, curResult) then | |
| 1802 | - MarkAt(x.pos, 'expected the results ' + TypeName(curResult) + ', found ' + TypeName(x.typ)); | |
| 1803 | - ReturnAll(x); | |
| 1804 | - exit; | |
| 1805 | - end; | |
| 1806 | - ReturnField(f, x); | |
| 1807 | - f := f^.next; | |
| 1808 | - if f <> nil then Expect(sComma); | |
| 1809 | - end; | |
| 1810 | -end; | |
| 1811 | - | |
| 1812 | -{ defer s: s is compiled where it stands, as a little subroutine of the | |
| 1813 | - procedure, jumped over; each way out of the statement list calls it. } | |
| 1814 | -procedure Defer; | |
| 1815 | -var L, pos, saved: longint; oldLoop: PLoop; oldDeferring: boolean; start: TPos; | |
| 1816 | -begin | |
| 1817 | - start := TokPos; | |
| 1818 | - Next; | |
| 1819 | - if directDefer then MarkAt(SpanFrom(start), 'defer can''t be deferred'); | |
| 1820 | - if numDefers > High(defers) then MarkAt(SpanFrom(start), 'more than ' + IntStr(High(defers) + 1) + ' deferred statements in one procedure'); | |
| 1821 | - L := NoLabel; | |
| 1822 | - pos := DeferBegin(L, saved); | |
| 1823 | - oldLoop := curLoop; oldDeferring := deferring; | |
| 1824 | - curLoop := nil; | |
| 1825 | - deferring := true; | |
| 1826 | - directDefer := true; | |
| 1827 | - Statement; | |
| 1828 | - directDefer := false; | |
| 1829 | - curLoop := oldLoop; deferring := oldDeferring; | |
| 1830 | - DeferEnd(L, saved); | |
| 1831 | - defers[numDefers] := pos; | |
| 1832 | - inc(numDefers); | |
| 1833 | -end; | |
| 1834 | - | |
| 1835 | -{ Whether the expression of x := ..., which starts at the current | |
| 1836 | - symbol, is x changed by adding to it, taking from it or its bits: | |
| 1837 | - x + e, x - e, x | e or x xor e, with x nowhere in e, so that it can be | |
| 1838 | - worked out in x's own register, if x is in one (gen's inPlace). } | |
| 1839 | -function ChangesItself(const x: TItem): boolean; | |
| 1840 | -var st: TScanState; wasSkipping: boolean; d: longint; | |
| 1841 | -begin | |
| 1842 | - ChangesItself := false; | |
| 1843 | - if (x.mode <> mVar) or (x.base <> bReg) or (x.typ^.form = fSlice) or (tok <> sIdent) or (id <> x.obj^.name) then exit; | |
| 1844 | - SaveScan(st); | |
| 1845 | - wasSkipping := skipping; | |
| 1846 | - skipping := true; { its slips are told when it is compiled } | |
| 1847 | - Next; | |
| 1848 | - if tok in [sPlus, sMinus, sBar, sXor] then begin | |
| 1849 | - ChangesItself := true; | |
| 1850 | - d := 0; | |
| 1851 | - while not ((d = 0) and (tok in [sSemicolon, sEnd])) and (tok <> sEof) do begin | |
| 1852 | - if (tok = sIdent) and (id = x.obj^.name) then ChangesItself := false; | |
| 1853 | - if tok in [sLparen, sLbrak, sLbrace] then inc(d) | |
| 1854 | - else if tok in [sRparen, sRbrak, sRbrace] then dec(d); | |
| 1855 | - Next; | |
| 1856 | - end; | |
| 1857 | - end; | |
| 1858 | - RestoreScan(st); | |
| 1859 | - skipping := wasSkipping; | |
| 1860 | -end; | |
| 1861 | - | |
| 1862 | -procedure OneStatement; | |
| 1863 | -var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; first, n, i: longint; k: TKind; | |
| 1864 | -begin | |
| 1865 | - start := TokPos; | |
| 1866 | - first := id; | |
| 1867 | - NoteLine(CurLine, CurCol); | |
| 1868 | - m := TempMark; | |
| 1869 | - ends := false; | |
| 1870 | - case tok of | |
| 1871 | - sIdent: begin | |
| 1872 | - LeftSide(x, called); | |
| 1873 | - if tok = sBecomes then begin | |
| 1874 | - NotReadOnly(x); | |
| 1875 | - Assignable(x, first); | |
| 1876 | - Next; | |
| 1877 | - if ChangesItself(x) then inPlace := x.chunk; | |
| 1878 | - Expression(y, x.typ); | |
| 1879 | - inPlace := -1; | |
| 1880 | - Assign(x, y); | |
| 1881 | - end else if tok = sComma then | |
| 1882 | - MultiAssign(x, first, false) | |
| 1883 | - else if not called then begin | |
| 1884 | - if (x.mode = mProc) or ((x.mode in [mVar, mStack]) and (x.typ^.form = fProc)) then begin | |
| 1885 | - BeginCall(c, x); | |
| 1886 | - EndCall(c, x); | |
| 1887 | - Discard(x); | |
| 1888 | - end else if tok = sEql then | |
| 1889 | - MarkHelp(x.pos, Subject(x, first) + ' isn''t a statement', 'assignment is written :=') | |
| 1890 | - else MarkAt(x.pos, Subject(x, first) + ' isn''t a statement'); | |
| 1891 | - end else begin | |
| 1892 | - if (x.mode <> mNone) and (numDrops <= High(drops)) then begin | |
| 1893 | - drops[numDrops].at := x.pos; | |
| 1894 | - if x.typ^.tuple then drops[numDrops].n := x.typ^.len else drops[numDrops].n := 1; | |
| 1895 | - inc(numDrops); | |
| 1896 | - end; | |
| 1897 | - Discard(x); | |
| 1898 | - end; | |
| 1899 | - end; | |
| 1900 | - sBlank: begin | |
| 1901 | - Next; | |
| 1902 | - if tok = sComma then MultiAssign(x, 0, true) | |
| 1903 | - else begin | |
| 1904 | - Expect(sBecomes); | |
| 1905 | - Expression(x, nil); | |
| 1906 | - { evaluated and dropped: a single value is loaded, so that a | |
| 1907 | - volatile one is read } | |
| 1908 | - if (x.mode in [mType, mBuiltin, mNone, mCase]) or (x.mode <> mStr) and not IsAggregate(x.typ) then | |
| 1909 | - Value(x); | |
| 1910 | - Discard(x); | |
| 1911 | - end; | |
| 1912 | - end; | |
| 1913 | - sIf: IfStatement(start, Indent); | |
| 1914 | - sWhile: WhileStatement(start, Indent); | |
| 1915 | - sRepeat: RepeatStatement; | |
| 1916 | - sFor: ForStatement(start, Indent); | |
| 1917 | - sMatch: MatchStatement(start, Indent); | |
| 1918 | - sReturn: begin | |
| 1919 | - Next; | |
| 1920 | - if deferring then MarkAt(SpanFrom(start), 'return can''t leave a deferred statement'); | |
| 1921 | - if tok = sSemicolon then begin | |
| 1922 | - RunDefers(0); | |
| 1923 | - ReturnNone; | |
| 1924 | - end else begin | |
| 1925 | - if (curResult <> nil) and curResult^.tuple then ReturnResults | |
| 1926 | - else begin | |
| 1927 | - Expression(x, curResult); | |
| 1928 | - ReturnValue(x); | |
| 1929 | - end; | |
| 1930 | - { the result, one value or two words on top, waits in the frame | |
| 1931 | - while the deferred statements run } | |
| 1932 | - if (numDefers > 0) and (curResult <> nil) and not BehindPointer(curResult) then begin | |
| 1933 | - n := 1; k := KindOf(curResult); | |
| 1934 | - if InTwoRegs(curResult) then begin n := 2; k := kWord end; | |
| 1935 | - if deferSlot = 0 then deferSlot := AllocPerm(16, 8); | |
| 1936 | - for i := n - 1 downto 0 do PopVar(FrameAdr(deferSlot + 8 * i), 8); | |
| 1937 | - RunDefers(0); | |
| 1938 | - for i := 0 to n - 1 do PushVar(FrameAdr(deferSlot + 8 * i), 8, false, k); | |
| 1939 | - end else RunDefers(0); | |
| 1940 | - ReturnJump; | |
| 1941 | - ends := true; | |
| 1942 | - end; | |
| 1943 | - end; | |
| 1944 | - sBreak: begin | |
| 1945 | - Next; | |
| 1946 | - if curLoop = nil then MarkAt(SpanFrom(start), 'break outside a loop'); | |
| 1947 | - RunDefers(curLoop^.base); | |
| 1948 | - Jump(curLoop^.brk); | |
| 1949 | - end; | |
| 1950 | - sDefer: Defer; | |
| 1951 | - sTry: begin | |
| 1952 | - TryCall(x); | |
| 1953 | - if (x.mode <> mNone) and (numDrops <= High(drops)) then begin | |
| 1954 | - drops[numDrops].at := x.pos; | |
| 1955 | - if x.typ^.tuple then drops[numDrops].n := x.typ^.len else drops[numDrops].n := 1; | |
| 1956 | - inc(numDrops); | |
| 1957 | - end; | |
| 1958 | - Discard(x); | |
| 1959 | - end; | |
| 1960 | - sArrow: MarkHelp(TokPos, 'a statement starts with a name', 'convert into a pointer variable, then assign through that'); | |
| 1961 | - else if AtLineStart then EndMissing(MissingPos, 'expected "end", found ' + Found) | |
| 1962 | - else Expected('a statement'); | |
| 1963 | - end; | |
| 1964 | - ReleaseTemps(m); | |
| 1965 | -end; | |
| 1966 | - | |
| 1967 | -procedure Statement; | |
| 1968 | -var r: TRecover; m: int64; | |
| 1969 | -begin | |
| 1970 | - m := TempMark; | |
| 1971 | - Guard(r); | |
| 1972 | - if setjmp(r.jb) = 0 then begin | |
| 1973 | - OneStatement; | |
| 1974 | - skipped := false; | |
| 1975 | - end else begin | |
| 1976 | - Restore(r); | |
| 1977 | - { a statement never starts a line: this is the next declaration } | |
| 1978 | - if AtLineStart or not SkipConstruct then GiveUp(r); | |
| 1979 | - ReleaseTemps(m); | |
| 1980 | - skipped := true; | |
| 1981 | - end; | |
| 1982 | - Unguard(r); | |
| 1983 | -end; | |
| 1984 | - | |
| 1985 | -procedure Statements; | |
| 1986 | -var base: longint; old: boolean; | |
| 1987 | -begin | |
| 1988 | - base := numDefers; | |
| 1989 | - old := directDefer; | |
| 1990 | - directDefer := false; | |
| 1991 | - ends := false; | |
| 1992 | - while not (tok in [sEnd, sElse, sElsif, sUntil, sBar, sEof]) do begin | |
| 1993 | - Statement; | |
| 1994 | - if not skipped or (tok = sSemicolon) then Expect(sSemicolon); | |
| 1995 | - end; | |
| 1996 | - RunDefers(base); | |
| 1997 | - numDefers := base; | |
| 1998 | - directDefer := old; | |
| 1999 | -end; | |
| 2000 | - | |
| 2001 | -{ Declarations } | |
| 2002 | - | |
| 2003 | -{ In a file read for its interface only: skip a procedure's body, from | |
| 2004 | - its local declarations to the end that closes it. } | |
| 2005 | -procedure SkipBody; | |
| 2006 | -var d: longint; sawBegin: boolean; | |
| 2007 | -begin | |
| 2008 | - d := 0; | |
| 2009 | - sawBegin := false; | |
| 2010 | - repeat | |
| 2011 | - case tok of | |
| 2012 | - sBegin: begin if d = 0 then sawBegin := true; inc(d) end; | |
| 2013 | - sIf, sWhile, sFor, sMatch, sRecord, sVariant: inc(d); | |
| 2014 | - sEnd: begin | |
| 2015 | - dec(d); | |
| 2016 | - if (d = 0) and sawBegin then begin Next; exit end; | |
| 2017 | - end; | |
| 2018 | - sEof: Expected('"end"'); | |
| 2019 | - end; | |
| 2020 | - Next; | |
| 2021 | - until false; | |
| 2022 | -end; | |
| 2023 | - | |
| 2024 | -procedure Declarations(top: boolean); forward; | |
| 2025 | - | |
| 2026 | -{ How Rt shows a variable of o's type in the trap report: 1 a signed | |
| 2027 | - integer, 2 an unsigned one, 3 a BOOL, 4 an address, 5 a real; plus | |
| 2028 | - its size times 16, and 256 if the frame holds its address, or 512 | |
| 2029 | - times 1 + i if it is in register VarRegs[i]; 0 if it isn't shown. } | |
| 2030 | -function VarKind(o: PObj): longint; | |
| 2031 | -var k, i: longint; | |
| 2032 | -begin | |
| 2033 | - case o^.typ^.form of | |
| 2034 | - fInt: k := 2 - ord(o^.typ^.signed); | |
| 2035 | - fEnum, fSet: k := 2; | |
| 2036 | - fBool: k := 3; | |
| 2037 | - fPtr, fProc, fError: k := 4; | |
| 2038 | - fReal: k := 5; | |
| 2039 | - else exit(0); | |
| 2040 | - end; | |
| 2041 | - VarKind := k + o^.typ^.size * 16 + ord(o^.ind) * 256; | |
| 2042 | - for i := 0 to High(ShownRegs) do | |
| 2043 | - if o^.reg = ShownRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512; | |
| 2044 | -end; | |
| 2045 | - | |
| 2046 | -{ Whether variable o could live in registers: a single value, a real | |
| 2047 | - in an xmm register, or a slice, in two, that isn't C's } | |
| 2048 | -function RegFit(o: PObj): boolean; | |
| 2049 | -begin | |
| 2050 | - RegFit := (o^.cls = oVar) and not o^.ind and not o^.ext | |
| 2051 | - and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError, fSlice, fReal]) | |
| 2052 | - or (o^.typ^.form = fSet) and (o^.typ^.size <= 8)); | |
| 2053 | -end; | |
| 2054 | - | |
| 2055 | -{ The first local of the procedure being compiled. Locals are chained | |
| 2056 | - by link in the order declared, the module's own first; before is the | |
| 2057 | - last one declared before the procedure's, or nil if there was none. } | |
| 2058 | -function FirstLocal(before: PObj): PObj; | |
| 2059 | -begin | |
| 2060 | - if before = nil then FirstLocal := curModule^.locals else FirstLocal := before^.link; | |
| 2061 | -end; | |
| 2062 | - | |
| 2063 | -{ Puts the busiest variables of the procedure, whose parameters are | |
| 2064 | - params and whose locals follow before, in registers: read ahead from | |
| 2065 | - its "begin" to its end, a use counts 1, in a loop 4, in two 16, in | |
| 2066 | - three or more 64, and a variable ADR is taken of can't have one. A register costs a save | |
| 2067 | - and a restore, so a variable used less than three times keeps its | |
| 2068 | - place in memory. A slice takes two, its pointer's and its length's; | |
| 2069 | - a real one of the xmm registers, stored and loaded again around each | |
| 2070 | - call, as System V keeps none through one. In a procedure that names no procedure, so calls nothing, the | |
| 2071 | - parameters that arrive in the registers no call needs stay there | |
| 2072 | - (KeepArrived), at no cost. } | |
| 2073 | -procedure ChooseRegs(params, before: PObj); | |
| 2074 | -var | |
| 2075 | - cand: array[0..63] of PObj; weight: array[0..63] of longint; | |
| 2076 | - isLoop: array[0..255] of boolean; | |
| 2077 | - n, i, k, kx, d, loops, best, need: longint; o, q, qual: PObj; st: TScanState; | |
| 2078 | - afterDot, wasSkipping, leaf: boolean; | |
| 2079 | - | |
| 2080 | - function Candidate(name: longint): longint; | |
| 2081 | - var j: longint; | |
| 2082 | - begin | |
| 2083 | - for j := 0 to n - 1 do if cand[j]^.name = name then exit(j); | |
| 2084 | - Candidate := -1; | |
| 2085 | - end; | |
| 2086 | - | |
| 2087 | - procedure Add(o: PObj); | |
| 2088 | - begin | |
| 2089 | - if RegFit(o) and (n <= High(cand)) then begin cand[n] := o; weight[n] := 0; inc(n) end; | |
| 2090 | - end; | |
| 2091 | - | |
| 2092 | - { Whether naming o may make a call: a procedure, a procedure variable, | |
| 2093 | - or NEW, SYSCALL or COPY, which use the registers a call would } | |
| 2094 | - function Calls(o: PObj): boolean; | |
| 2095 | - begin | |
| 2096 | - Calls := (o <> nil) and ((o^.cls = oProc) or (o^.cls = oVar) and (o^.typ^.form = fProc) | |
| 2097 | - or (o^.cls = oBuiltin) and ((o^.val = bNew) or (o^.val = bSyscall) or (o^.val = bCopy))); | |
| 2098 | - end; | |
| 2099 | - | |
| 2100 | -begin | |
| 2101 | - n := 0; | |
| 2102 | - o := params; | |
| 2103 | - while o <> nil do begin Add(o); o := o^.next end; | |
| 2104 | - o := FirstLocal(before); | |
| 2105 | - while o <> nil do begin Add(o); o := o^.link end; | |
| 2106 | - if n > 0 then begin | |
| 2107 | - SaveScan(st); | |
| 2108 | - wasSkipping := skipping; | |
| 2109 | - skipping := true; { its slips are told when it is compiled } | |
| 2110 | - d := 0; loops := 0; afterDot := false; leaf := true; qual := nil; | |
| 2111 | - repeat | |
| 2112 | - { a name, or a module's name and one of its exports, that calls } | |
| 2113 | - if (tok = sIdent) and not afterDot then begin | |
| 2114 | - q := Lookup(id); | |
| 2115 | - if Calls(q) then leaf := false; | |
| 2116 | - qual := nil; | |
| 2117 | - if (q <> nil) and (q^.cls = oModule) then qual := q; | |
| 2118 | - end else if (tok = sIdent) and (qual <> nil) then begin | |
| 2119 | - if (qual^.module = nil) or Calls(Find(qual^.module^.scope, id)) then leaf := false; | |
| 2120 | - qual := nil; | |
| 2121 | - end; | |
| 2122 | - case tok of | |
| 2123 | - sBegin, sIf, sMatch, sWhile, sFor, sRepeat: | |
| 2124 | - if d < High(isLoop) then begin | |
| 2125 | - inc(d); | |
| 2126 | - isLoop[d] := tok in [sWhile, sFor, sRepeat]; | |
| 2127 | - if isLoop[d] then inc(loops); | |
| 2128 | - end; | |
| 2129 | - sEnd, sUntil: begin | |
| 2130 | - if isLoop[d] then dec(loops); | |
| 2131 | - dec(d); | |
| 2132 | - end; | |
| 2133 | - sIdent: | |
| 2134 | - if id = nameAdr then begin | |
| 2135 | - Next; | |
| 2136 | - if tok = sLparen then Next; | |
| 2137 | - { a hugely negative weight bars it from a register, whatever | |
| 2138 | - its uses add } | |
| 2139 | - if (tok = sIdent) and (Candidate(id) >= 0) then weight[Candidate(id)] := low(longint) div 2; | |
| 2140 | - end else if not afterDot and (Candidate(id) >= 0) then begin | |
| 2141 | - k := Candidate(id); | |
| 2142 | - if loops > 2 then inc(weight[k], 64) else inc(weight[k], 1 shl (2 * loops)); | |
| 2143 | - end; | |
| 2144 | - end; | |
| 2145 | - afterDot := tok = sPeriod; { a name after a dot is a field, not a variable } | |
| 2146 | - if (d > 0) and (tok <> sEof) then Next; | |
| 2147 | - until (d = 0) or (tok = sEof); | |
| 2148 | - RestoreScan(st); | |
| 2149 | - skipping := wasSkipping; | |
| 2150 | - if leaf then begin | |
| 2151 | - o := params; | |
| 2152 | - while o <> nil do begin | |
| 2153 | - k := Candidate(o^.name); | |
| 2154 | - if (k >= 0) and (weight[k] >= 0) then KeepArrived(o); { a candidate whose ADR isn't taken } | |
| 2155 | - o := o^.next; | |
| 2156 | - end; | |
| 2157 | - end; | |
| 2158 | - { a slice's weight is shared by its two registers } | |
| 2159 | - for i := 0 to n - 1 do | |
| 2160 | - if cand[i]^.typ^.form = fSlice then weight[i] := weight[i] div 2; | |
| 2161 | - { the heaviest, each used at least three times a register, take the | |
| 2162 | - five registers in turn, while there are enough for each, and the | |
| 2163 | - reals the four xmm registers of XVarRegs } | |
| 2164 | - k := 0; kx := 0; | |
| 2165 | - repeat | |
| 2166 | - best := -1; | |
| 2167 | - for i := 0 to n - 1 do | |
| 2168 | - if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i; | |
| 2169 | - if best < 0 then break; | |
| 2170 | - need := 1 + ord(cand[best]^.typ^.form = fSlice); | |
| 2171 | - if cand[best]^.typ^.form = fReal then begin | |
| 2172 | - if kx < 4 then begin cand[best]^.reg := XVarRegs[kx]; inc(kx) end | |
| 2173 | - else weight[best] := 0; | |
| 2174 | - end else if k + need <= 5 then begin | |
| 2175 | - cand[best]^.reg := VarRegs[k]; | |
| 2176 | - if need = 2 then cand[best]^.reg2 := VarRegs[k + 1]; | |
| 2177 | - inc(k, need); | |
| 2178 | - end else weight[best] := 0; { too big for what is left } | |
| 2179 | - until false; | |
| 2180 | - end; | |
| 2181 | -end; | |
| 2182 | - | |
| 2183 | -{ The registers the variables of the procedure are in, as bits: bit i | |
| 2184 | - is VarRegs[i], bit XBit + i XVarRegs[i] } | |
| 2185 | -function RegsOf(params, before: PObj): longint; | |
| 2186 | -var o: PObj; m, k: longint; | |
| 2187 | -begin | |
| 2188 | - m := 0; | |
| 2189 | - o := params; | |
| 2190 | - while o <> nil do begin | |
| 2191 | - for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k); | |
| 2192 | - for k := 0 to 3 do if o^.reg = XVarRegs[k] then m := m or (1 shl (XBit + k)); | |
| 2193 | - o := o^.next; | |
| 2194 | - end; | |
| 2195 | - o := FirstLocal(before); | |
| 2196 | - while o <> nil do begin | |
| 2197 | - if o^.cls = oVar then begin | |
| 2198 | - for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k); | |
| 2199 | - for k := 0 to 3 do if o^.reg = XVarRegs[k] then m := m or (1 shl (XBit + k)); | |
| 2200 | - end; | |
| 2201 | - o := o^.link; | |
| 2202 | - end; | |
| 2203 | - RegsOf := m; | |
| 2204 | -end; | |
| 2205 | - | |
| 2206 | -{ Gives each local not in a register its slot in the frame, the slots | |
| 2207 | - the prologue zeroes, and then tells gen which registers the | |
| 2208 | - variables are in. A variable in a register has no slot: it starts at | |
| 2209 | - zero in its register, and the trap report finds it there. } | |
| 2210 | -procedure PlaceLocals(params, before: PObj); | |
| 2211 | -var o: PObj; | |
| 2212 | -begin | |
| 2213 | - ResetLocals; | |
| 2214 | - o := FirstLocal(before); | |
| 2215 | - while o <> nil do begin | |
| 2216 | - if (o^.cls = oVar) and (o^.reg = 0) then o^.val := AllocPerm(o^.typ^.size, o^.typ^.align) | |
| 2217 | - else if (o^.cls = oVar) and (o^.reg >= XmmVar) then begin | |
| 2218 | - o^.val := AllocPerm(8, 8); | |
| 2219 | - XHome(o^.reg, o^.val); | |
| 2220 | - end; | |
| 2221 | - o := o^.link; | |
| 2222 | - end; | |
| 2223 | - o := params; | |
| 2224 | - while o <> nil do begin | |
| 2225 | - if o^.reg >= XmmVar then XHome(o^.reg, o^.val); | |
| 2226 | - o := o^.next; | |
| 2227 | - end; | |
| 2228 | - UseRegs(RegsOf(params, before)); | |
| 2229 | -end; | |
| 2230 | - | |
| 2231 | -procedure Body(p: PObj; sig: PType; line: longint); | |
| 2232 | -var | |
| 2233 | - q, params, last, o, before: PObj; old: PType; st: TScanState; | |
| 2234 | - mark: TCodeMark; drops0, errs: longint; | |
| 2235 | -begin | |
| 2236 | - OpenScope(NewScope); | |
| 2237 | - params := nil; last := nil; | |
| 2238 | - q := sig^.fields; | |
| 2239 | - while q <> nil do begin | |
| 2240 | - o := NewObj(oVar, q^.name, q^.typ); | |
| 2241 | - o^.pos := q^.pos; | |
| 2242 | - o^.lev := 1; | |
| 2243 | - o^.isVar := q^.isVar; | |
| 2244 | - o^.param := true; | |
| 2245 | - if last = nil then params := o else last^.next := o; | |
| 2246 | - last := o; | |
| 2247 | - q := q^.next; | |
| 2248 | - end; | |
| 2249 | - BeginProc(sig, params); | |
| 2250 | - deferSlot := 0; | |
| 2251 | - o := params; | |
| 2252 | - while o <> nil do begin | |
| 2253 | - q := o^.next; | |
| 2254 | - Declare(o); | |
| 2255 | - o := q; | |
| 2256 | - end; | |
| 2257 | - curProc := p; | |
| 2258 | - before := curModule^.lastLocal; | |
| 2259 | - Declarations(false); | |
| 2260 | - ChooseRegs(params, before); | |
| 2261 | - PlaceLocals(params, before); | |
| 2262 | - { the body, made again if a variable in a register must be in memory } | |
| 2263 | - SaveScan(st); | |
| 2264 | - MarkCode(mark); drops0 := numDrops; errs := errors; | |
| 2265 | - old := curResult; | |
| 2266 | - repeat | |
| 2267 | - demoted := false; | |
| 2268 | - Expect(sBegin); | |
| 2269 | - curResult := sig^.base; | |
| 2270 | - EnterBody(p^.chunk, line); | |
| 2271 | - stray := false; elseIf := false; deferSlot := 0; | |
| 2272 | - Statements; | |
| 2273 | - p^.openEnd := (curResult <> nil) and not ends; | |
| 2274 | - NoteLine(CurLine, CurCol); | |
| 2275 | - Expect(sEnd); | |
| 2276 | - if not (demoted or lost) or (errors <> errs) then break; | |
| 2277 | - { a parameter kept where it arrived goes to memory: a call or a copy | |
| 2278 | - wanted its register } | |
| 2279 | - if lost then begin | |
| 2280 | - o := params; | |
| 2281 | - while o <> nil do begin | |
| 2282 | - if KeptIn(o) then begin o^.reg := 0; o^.reg2 := 0 end; | |
| 2283 | - o := o^.next; | |
| 2284 | - end; | |
| 2285 | - end; | |
| 2286 | - numDrops := drops0; | |
| 2287 | - Redo(mark); | |
| 2288 | - PlaceLocals(params, before); | |
| 2289 | - RestoreScan(st); | |
| 2290 | - until false; | |
| 2291 | - EndProc(p^.chunk, p^.openEnd); | |
| 2292 | - { its parameters and variables, for the trap report } | |
| 2293 | - o := FirstLocal(before); | |
| 2294 | - q := params; | |
| 2295 | - while q <> nil do begin | |
| 2296 | - if VarKind(q) <> 0 then NoteVar(p^.chunk, q^.name, q^.val, VarKind(q)); | |
| 2297 | - q := q^.next; | |
| 2298 | - end; | |
| 2299 | - while o <> nil do begin | |
| 2300 | - if (o^.cls = oVar) and (VarKind(o) <> 0) then NoteVar(p^.chunk, o^.name, o^.val, VarKind(o)); | |
| 2301 | - o := o^.link; | |
| 2302 | - end; | |
| 2303 | - curResult := old; | |
| 2304 | - curProc := nil; | |
| 2305 | - CloseScope; | |
| 2306 | -end; | |
| 2307 | - | |
| 2308 | -{ An integer constant in a code procedure, for the assembler: a whole | |
| 2309 | - expression, or a single factor where an expression would read on | |
| 2310 | - too far } | |
| 2311 | -function AsmConst(whole: boolean): int64; | |
| 2312 | -var x: TItem; | |
| 2313 | -begin | |
| 2314 | - if whole then exit(ConstExpr); | |
| 2315 | - Factor(x, nil); | |
| 2316 | - if not IsInt(x.typ) then MarkAt(x.pos, 'expected an integer constant, found ' + What(x)); | |
| 2317 | - AsmConst := ConstValue(x); | |
| 2318 | -end; | |
| 2319 | - | |
| 2320 | -procedure CodeBody(p: PObj; sig: PType; const at: TPos); | |
| 2321 | -var q: PObj; ni, nf: longint; | |
| 2322 | -begin | |
| 2323 | - ni := 0; nf := 0; | |
| 2324 | - q := sig^.fields; | |
| 2325 | - while q <> nil do begin | |
| 2326 | - if not q^.isVar and (q^.typ^.form = fSlice) then inc(ni, 2) { address, then length } | |
| 2327 | - else begin | |
| 2328 | - if not q^.isVar and not IsScalar(q^.typ) then | |
| 2329 | - MarkAt(at, 'a code procedure takes integers, BOOL, reals, pointers, procedures and slices'); | |
| 2330 | - if (q^.typ^.form = fReal) and not q^.isVar then inc(nf) else inc(ni); | |
| 2331 | - end; | |
| 2332 | - q := q^.next; | |
| 2333 | - end; | |
| 2334 | - if (ni > 6) or (nf > 8) or sig^.variadic then MarkAt(at, 'too many parameters for a code procedure'); | |
| 2335 | - if sig^.base <> nil then | |
| 2336 | - if sig^.base^.tuple then begin | |
| 2337 | - q := sig^.base^.fields; | |
| 2338 | - if (sig^.base^.len <> 2) or (q^.typ^.size <> 8) or (q^.next^.typ^.size <> 8) | |
| 2339 | - or not IsScalar(q^.typ) or not IsScalar(q^.next^.typ) | |
| 2340 | - or (q^.typ^.form = fReal) or (q^.next^.typ^.form = fReal) then | |
| 2341 | - MarkAt(at, 'a code procedure returns two results only in rax and rdx'); | |
| 2342 | - end else if not IsScalar(sig^.base) and (sig^.base^.form <> fSlice) then | |
| 2343 | - MarkAt(at, 'a code procedure returns a single value'); | |
| 2344 | - CodeProc(p^.chunk, @AsmConst); | |
| 2345 | - Expect(sEnd); | |
| 2346 | -end; | |
| 2347 | - | |
| 2348 | -procedure ProcDecl; | |
| 2349 | -var n, line: longint; exp: boolean; sig: PType; p: PObj; at: TPos; | |
| 2350 | -begin | |
| 2351 | - InstancesFor(false); | |
| 2352 | - line := CurLine; | |
| 2353 | - Next; | |
| 2354 | - IdentDef(n, exp, at); | |
| 2355 | - sig := Signature; | |
| 2356 | - Expect(sSemicolon); | |
| 2357 | - p := Find(TopScope, n); | |
| 2358 | - if (p <> nil) and (p^.cls = oProc) and p^.fwd then begin | |
| 2359 | - if not Same(p^.typ, sig) then begin | |
| 2360 | - Error(at, NameOf(n) + ' doesn''t have the signature it was declared forward with'); | |
| 2361 | - Note(p^.pos, 'the signature is here'); | |
| 2362 | - Fail; | |
| 2363 | - end; | |
| 2364 | - if tok = sForward then Mark(NameOf(n) + ' is declared forward twice'); | |
| 2365 | - p^.exported := p^.exported or exp; | |
| 2366 | - end else begin | |
| 2367 | - p := NewObj(oProc, n, sig); | |
| 2368 | - p^.pos := at; | |
| 2369 | - p^.exported := exp; | |
| 2370 | - if (curModule^.name = nameRt) and not checkMode and (n = nameFail) then p^.chunk := rtFail | |
| 2371 | - else p^.chunk := NewChunk(secText, curModule^.sname, n); | |
| 2372 | - Declare(p); | |
| 2373 | - end; | |
| 2374 | - if tok = sForward then begin | |
| 2375 | - Next; | |
| 2376 | - p^.fwd := true; | |
| 2377 | - exit; | |
| 2378 | - end; | |
| 2379 | - p^.fwd := false; | |
| 2380 | - if tok = sCode then begin | |
| 2381 | - Next; | |
| 2382 | - p^.isCode := true; | |
| 2383 | - CodeBody(p, sig, at); | |
| 2384 | - end else if checkMode then SkipBody else Body(p, sig, line); | |
| 2385 | - if tok = sIdent then begin | |
| 2386 | - if id = curModule^.name then EndMissing(TokPos, NameOf(n) + ' has no "end": this one closes the module') | |
| 2387 | - else if id <> n then EndMissing(TokPos, 'expected ' + NameOf(n) + ', found ' + NameOf(id)); | |
| 2388 | - Next; | |
| 2389 | - end; | |
| 2390 | -end; | |
| 2391 | - | |
| 2392 | -{ An extern procedure or variable: C's, reached through the quadword | |
| 2393 | - that holds its address. } | |
| 2394 | -procedure ExternDecl; | |
| 2395 | -var n: longint; exp: boolean; o, q: PObj; t: PType; at: TPos; | |
| 2396 | -begin | |
| 2397 | - Next; | |
| 2398 | - curModule^.usesC := true; | |
| 2399 | - if tok = sProc then begin | |
| 2400 | - Next; | |
| 2401 | - IdentDef(n, exp, at); | |
| 2402 | - WillDeclare(n); | |
| 2403 | - inExtern := true; | |
| 2404 | - t := Signature; | |
| 2405 | - inExtern := false; | |
| 2406 | - q := t^.fields; | |
| 2407 | - while q <> nil do begin | |
| 2408 | - if q^.isVar or not IsScalar(q^.typ) then | |
| 2409 | - MarkAt(at, 'C takes integers, BOOL, reals, pointers and procedures'); | |
| 2410 | - q := q^.next; | |
| 2411 | - end; | |
| 2412 | - if (t^.base <> nil) and not IsScalar(t^.base) then MarkAt(at, 'C returns a single value'); | |
| 2413 | - o := NewObj(oProc, n, t); | |
| 2414 | - end else begin | |
| 2415 | - if tok <> sVar then Expected('"proc" or "var"'); | |
| 2416 | - Next; | |
| 2417 | - IdentDef(n, exp, at); | |
| 2418 | - WillDeclare(n); | |
| 2419 | - Expect(sColon); | |
| 2420 | - declExported := exp; | |
| 2421 | - t := ParseType; | |
| 2422 | - declExported := false; | |
| 2423 | - o := NewObj(oVar, n, t); | |
| 2424 | - end; | |
| 2425 | - o^.pos := at; | |
| 2426 | - o^.exported := exp; | |
| 2427 | - o^.ext := true; | |
| 2428 | - o^.chunk := ExternChunk(n); | |
| 2429 | - Declare(o); | |
| 2430 | -end; | |
| 2431 | - | |
| 2432 | -{ An array or record constant: a literal of constants, which become | |
| 2433 | - the bytes of a read-only chunk, as a string constant's do (report | |
| 2434 | - section 4). An address in it, of a string, an array constant, a | |
| 2435 | - procedure or an ERROR, waits in fixes until the chunk is made, then | |
| 2436 | - is a fixup the linker patches. The bytes are the compiler's own | |
| 2437 | - machine's, little-endian, as the target's are. } | |
| 2438 | -var | |
| 2439 | - constBytes: PBytes; | |
| 2440 | - fixes: array[0..4095] of record off: int64; chunk: longint; end; | |
| 2441 | - numFixes: longint; | |
| 2442 | - | |
| 2443 | -procedure ConstFix(off: int64; chunk: longint); | |
| 2444 | -begin | |
| 2445 | - if numFixes > High(fixes) then MarkAt(TokPos, 'more than ' + IntStr(High(fixes) + 1) + ' addresses in one constant'); | |
| 2446 | - fixes[numFixes].off := off; | |
| 2447 | - fixes[numFixes].chunk := chunk; | |
| 2448 | - inc(numFixes); | |
| 2449 | -end; | |
| 2450 | - | |
| 2451 | -{ The part of type t at offset off of the constant } | |
| 2452 | -procedure ConstPart(t: PType; off: int64); | |
| 2453 | -var e: TItem; f: PObj; k: int64; r: single; start: TPos; | |
| 2454 | -begin | |
| 2455 | - start := TokPos; | |
| 2456 | - if (t^.form = fArray) and (tok = sLbrak) then begin | |
| 2457 | - Next; | |
| 2458 | - k := 0; | |
| 2459 | - if tok <> sRbrak then | |
| 2460 | - repeat | |
| 2461 | - if k < t^.len then ConstPart(t^.base, off + k * t^.base^.size) | |
| 2462 | - else ConstPart(t^.base, off); | |
| 2463 | - inc(k); | |
| 2464 | - if tok <> sComma then break; | |
| 2465 | - Next; | |
| 2466 | - until false; | |
| 2467 | - Expect(sRbrak); | |
| 2468 | - if k <> t^.len then MarkAt(SpanFrom(start), 'expected ' + IntStr(t^.len) + ' elements, found ' + IntStr(k)); | |
| 2469 | - exit; | |
| 2470 | - end; | |
| 2471 | - if (t^.form = fRecord) and not t^.tuple and (tok = sLbrace) then begin | |
| 2472 | - Next; | |
| 2473 | - if tok <> sRbrace then | |
| 2474 | - repeat | |
| 2475 | - if tok <> sIdent then Expected('a field'); | |
| 2476 | - f := FieldOf(t); | |
| 2477 | - Next; | |
| 2478 | - Expect(sColon); | |
| 2479 | - ConstPart(f^.typ, off + f^.val); | |
| 2480 | - if tok <> sComma then break; | |
| 2481 | - Next; | |
| 2482 | - until false; | |
| 2483 | - Expect(sRbrace); | |
| 2484 | - exit; | |
| 2485 | - end; | |
| 2486 | - Expression(e, t); | |
| 2487 | - if (e.mode = mStr) and ((t^.form = fSlice) or (t^.form = fArray)) and (t^.base = tU8) then begin | |
| 2488 | - if t^.form = fSlice then begin | |
| 2489 | - ConstFix(off, e.chunk); | |
| 2490 | - Move(e.val, constBytes^[off + 8], 8); | |
| 2491 | - end else if e.val > t^.len then MarkAt(e.pos, 'the string is longer than ' + TypeName(t)) | |
| 2492 | - else Move(rodata^[chunks^[e.chunk].start], constBytes^[off], e.val); | |
| 2493 | - end else if t^.form = fSlice then begin | |
| 2494 | - if (e.mode <> mVar) or (e.obj = nil) or (e.obj^.cls <> oConst) or (e.typ^.form <> fArray) | |
| 2495 | - or (e.pos.b - e.pos.a <> length(NameOf(e.obj^.name))) or not Same(e.typ^.base, t^.base) then | |
| 2496 | - MarkAt(e.pos, 'a constant slice is a string or an array constant of its elements, found ' + What(e)); | |
| 2497 | - ConstFix(off, e.chunk); | |
| 2498 | - Move(e.typ^.len, constBytes^[off + 8], 8); | |
| 2499 | - end else if t^.form = fVariant then MarkAt(e.pos, 'a constant holds no variant') | |
| 2500 | - else if t^.form in [fArray, fRecord] then MarkAt(e.pos, 'expected a literal of ' + TypeName(t)) | |
| 2501 | - else begin | |
| 2502 | - Coerce(e, t); | |
| 2503 | - if (e.mode = mProc) and not e.obj^.ext then ConstFix(off, e.obj^.chunk) | |
| 2504 | - else if e.mode <> mConst then MarkAt(e.pos, 'expected a constant, found ' + What(e)) | |
| 2505 | - else if (t^.form = fError) and (e.val > 0) then ConstFix(off, e.val - 1) | |
| 2506 | - else if (t^.form = fPtr) and (e.val <> 0) then MarkAt(e.pos, 'a constant pointer is nil') | |
| 2507 | - else if (t^.form = fReal) and (t^.size = 4) then begin | |
| 2508 | - r := RealOf(e); | |
| 2509 | - Move(r, constBytes^[off], 4); | |
| 2510 | - end else Move(e.val, constBytes^[off], t^.size); | |
| 2511 | - end; | |
| 2512 | -end; | |
| 2513 | - | |
| 2514 | -{ The chunk of n, a constant of type t whose literal is next } | |
| 2515 | -function AggregateConst(t: PType; n: longint): longint; | |
| 2516 | -var c, i: longint; | |
| 2517 | -begin | |
| 2518 | - constBytes := AllocMem(t^.size + 8); | |
| 2519 | - numFixes := 0; | |
| 2520 | - ConstPart(t, 0); | |
| 2521 | - c := NewChunk(secRodata, curModule^.sname, n); | |
| 2522 | - SetData(c, constBytes, t^.size, t^.align); | |
| 2523 | - for i := 0 to numFixes - 1 do AddDataFixup(c, fixes[i].off, fixes[i].chunk, 0); | |
| 2524 | - FreeMem(constBytes); | |
| 2525 | - AggregateConst := c; | |
| 2526 | -end; | |
| 2527 | - | |
| 2528 | -procedure ConstDecl(top: boolean); | |
| 2529 | -var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start, at: TPos; | |
| 2530 | -begin | |
| 2531 | - IdentDef(n, exp, at); | |
| 2532 | - WillDeclare(n); | |
| 2533 | - t := nil; | |
| 2534 | - if tok = sColon then begin | |
| 2535 | - Next; | |
| 2536 | - start := TokPos; | |
| 2537 | - t := ParseType; | |
| 2538 | - if (t <> tError) and not ((t^.form = fArray) or (t^.form = fRecord) and not t^.tuple) then | |
| 2539 | - MarkAt(SpanFrom(start), 'only an ERROR, an array or a record constant has a type'); | |
| 2540 | - end; | |
| 2541 | - Expect(sEql); | |
| 2542 | - if (t <> nil) and (t^.form in [fArray, fRecord]) then begin | |
| 2543 | - o := NewObj(oConst, n, t); | |
| 2544 | - o^.chunk := AggregateConst(t, n); | |
| 2545 | - o^.pos := at; | |
| 2546 | - o^.exported := exp and top; | |
| 2547 | - Declare(o); | |
| 2548 | - Expect(sSemicolon); | |
| 2549 | - exit; | |
| 2550 | - end; | |
| 2551 | - Expression(x, nil); | |
| 2552 | - o := NewObj(oConst, n, x.typ); | |
| 2553 | - o^.pos := at; | |
| 2554 | - o^.exported := exp and top; | |
| 2555 | - if t = tError then begin | |
| 2556 | - o^.typ := tError; | |
| 2557 | - o^.val := ErrorChunk(x, curModule^.sname, n) + 1; | |
| 2558 | - end else if x.mode = mStr then begin o^.val := x.val; o^.chunk := x.chunk end | |
| 2559 | - else o^.val := ConstValue(x); | |
| 2560 | - Declare(o); | |
| 2561 | - Expect(sSemicolon); | |
| 2562 | -end; | |
| 2563 | - | |
| 2564 | -procedure TypeDecl(top: boolean); | |
| 2565 | -var n: longint; exp: boolean; o: PObj; t: PType; at: TPos; | |
| 2566 | -begin | |
| 2567 | - IdentDef(n, exp, at); | |
| 2568 | - WillDeclare(n); | |
| 2569 | - Expect(sEql); | |
| 2570 | - declExported := exp and top; | |
| 2571 | - inTypes := true; | |
| 2572 | - t := ParseType; | |
| 2573 | - o := NewObj(oType, n, t); | |
| 2574 | - o^.pos := at; | |
| 2575 | - o^.exported := exp and top; | |
| 2576 | - if t^.obj = nil then t^.obj := o; | |
| 2577 | - Declare(o); | |
| 2578 | - Expect(sSemicolon); | |
| 2579 | -end; | |
| 2580 | - | |
| 2581 | -procedure VarDecl(top: boolean); | |
| 2582 | -var | |
| 2583 | - i, k: longint; o: PObj; t: PType; | |
| 2584 | - names: array[0..63] of longint; exps: array[0..63] of boolean; | |
| 2585 | - poss: array[0..63] of TPos; | |
| 2586 | -begin | |
| 2587 | - k := 0; | |
| 2588 | - repeat | |
| 2589 | - if k > High(names) then Mark('more than ' + IntStr(High(names) + 1) + ' names in one declaration'); | |
| 2590 | - IdentDef(names[k], exps[k], poss[k]); | |
| 2591 | - WillDeclare(names[k]); | |
| 2592 | - inc(k); | |
| 2593 | - if tok <> sComma then break; | |
| 2594 | - Next; | |
| 2595 | - until false; | |
| 2596 | - Expect(sColon); | |
| 2597 | - declExported := top and exps[0]; | |
| 2598 | - t := ParseType; | |
| 2599 | - declExported := false; | |
| 2600 | - for i := 0 to k - 1 do begin | |
| 2601 | - o := NewObj(oVar, names[i], t); | |
| 2602 | - o^.pos := poss[i]; | |
| 2603 | - o^.exported := exps[i] and top; | |
| 2604 | - o^.isVar := false; | |
| 2605 | - if top then begin | |
| 2606 | - o^.lev := 0; | |
| 2607 | - o^.chunk := NewChunk(secBss, curModule^.sname, names[i]); | |
| 2608 | - SetBss(o^.chunk, t^.size, t^.align); | |
| 2609 | - end else | |
| 2610 | - o^.lev := 1; { its slot comes from PlaceLocals, if it needs one } | |
| 2611 | - Declare(o); | |
| 2612 | - end; | |
| 2613 | - Expect(sSemicolon); | |
| 2614 | -end; | |
| 2615 | - | |
| 2616 | -{ After a procedure had an error: its name is poison if it wasn't | |
| 2617 | - declared, and reading goes on after the "end Name;" that closes it, | |
| 2618 | - or at the next procedure if that is missing } | |
| 2619 | -procedure SkipProc; | |
| 2620 | -var st: TScanState; | |
| 2621 | -begin | |
| 2622 | - Next; | |
| 2623 | - if (tok = sIdent) and (Find(TopScope, id) = nil) then Poison(id); | |
| 2624 | - repeat | |
| 2625 | - repeat Next until AtLineStart or (tok = sEof); | |
| 2626 | - if tok in [sProc, sExtern, sEof] then exit; | |
| 2627 | - if tok = sEnd then begin | |
| 2628 | - SaveScan(st); | |
| 2629 | - Next; | |
| 2630 | - if tok = sIdent then Next; | |
| 2631 | - if tok = sSemicolon then begin Next; exit end; | |
| 2632 | - RestoreScan(st); | |
| 2633 | - exit; | |
| 2634 | - end; | |
| 2635 | - until false; | |
| 2636 | -end; | |
| 2637 | - | |
| 2638 | -{ One declaration, of the kind k, as a recovery point } | |
| 2639 | -procedure Declaration(k: TSym; top: boolean); | |
| 2640 | -var r: TRecover; | |
| 2641 | -begin | |
| 2642 | - Guard(r); | |
| 2643 | - numDeclaring := 0; | |
| 2644 | - if setjmp(r.jb) = 0 then | |
| 2645 | - case k of | |
| 2646 | - sConst: ConstDecl(top); | |
| 2647 | - sType: TypeDecl(top); | |
| 2648 | - sVar: VarDecl(top); | |
| 2649 | - sProc: begin | |
| 2650 | - if not top then Mark('procedures don''t nest'); | |
| 2651 | - ProcDecl; | |
| 2652 | - Expect(sSemicolon); | |
| 2653 | - end; | |
| 2654 | - sExtern: begin | |
| 2655 | - if not top then Mark('extern is only for the top of a module'); | |
| 2656 | - ExternDecl; | |
| 2657 | - Expect(sSemicolon); | |
| 2658 | - end; | |
| 2659 | - end | |
| 2660 | - else begin | |
| 2661 | - Restore(r); | |
| 2662 | - if k = sProc then SkipProc else SkipDeclaration; | |
| 2663 | - end; | |
| 2664 | - Unguard(r); | |
| 2665 | -end; | |
| 2666 | - | |
| 2667 | -procedure Declarations(top: boolean); | |
| 2668 | -var i: longint; o: PObj; k: TSym; | |
| 2669 | -begin | |
| 2670 | - repeat | |
| 2671 | - k := tok; | |
| 2672 | - case k of | |
| 2673 | - sConst, sVar: begin | |
| 2674 | - Next; | |
| 2675 | - while tok = sIdent do Declaration(k, top); | |
| 2676 | - end; | |
| 2677 | - sType: begin | |
| 2678 | - Next; | |
| 2679 | - numFwd := 0; | |
| 2680 | - while tok = sIdent do Declaration(sType, top); | |
| 2681 | - inTypes := false; | |
| 2682 | - for i := 0 to numFwd - 1 do begin | |
| 2683 | - o := Lookup(fwdTypes[i]^.fwd); | |
| 2684 | - if (o = nil) or (o^.cls <> oType) then begin | |
| 2685 | - NotDeclared(fwdPos[i], fwdTypes[i]^.fwd); | |
| 2686 | - fwdTypes[i]^.base := tPoison; | |
| 2687 | - end else begin | |
| 2688 | - fwdTypes[i]^.base := o^.typ; | |
| 2689 | - Named(o); | |
| 2690 | - end; | |
| 2691 | - fwdTypes[i]^.fwd := -1; | |
| 2692 | - end; | |
| 2693 | - end; | |
| 2694 | - sProc, sExtern: Declaration(k, top); | |
| 2695 | - else exit; | |
| 2696 | - end; | |
| 2697 | - until false; | |
| 2698 | -end; | |
| 2699 | - | |
| 2700 | -{ Modules } | |
| 2701 | - | |
| 2702 | -{ Whether m is module n with these arguments. } | |
| 2703 | -function IsModule(m: PModule; n: longint; const args: TArgs): boolean; | |
| 2704 | -var i: longint; | |
| 2705 | -begin | |
| 2706 | - IsModule := false; | |
| 2707 | - if (m^.name <> n) or (m^.args.n <> args.n) then exit; | |
| 2708 | - for i := 0 to args.n - 1 do | |
| 2709 | - if (m^.args.o[i] <> args.o[i]) or not Same(m^.args.t[i], args.t[i]) then exit; | |
| 2710 | - IsModule := true; | |
| 2711 | -end; | |
| 2712 | - | |
| 2713 | -function FindModule(n: longint; const args: TArgs): PModule; | |
| 2714 | -var m: PModule; | |
| 2715 | -begin | |
| 2716 | - m := firstModule; | |
| 2717 | - while (m <> nil) and not IsModule(m, n, args) do m := m^.next; | |
| 2718 | - FindModule := m; | |
| 2719 | -end; | |
| 2720 | - | |
| 2721 | -{ The arguments of an instance: types, or procedures named. } | |
| 2722 | -procedure ParseArgs(var args: TArgs; const mname: shortstring); | |
| 2723 | -var o: PObj; start: TPos; | |
| 2724 | -begin | |
| 2725 | - Next; | |
| 2726 | - repeat | |
| 2727 | - if args.n > High(args.t) then Mark('too many type arguments for ' + mname); | |
| 2728 | - args.o[args.n] := nil; | |
| 2729 | - start := TokPos; | |
| 2730 | - if tok = sIdent then begin | |
| 2731 | - o := QualIdent; | |
| 2732 | - if o^.cls = oProc then args.o[args.n] := o | |
| 2733 | - else if o^.cls <> oType then MarkAt(SpanFrom(start), NameOf(o^.name) + ' isn''t a type or a procedure'); | |
| 2734 | - args.t[args.n] := o^.typ; | |
| 2735 | - end else args.t[args.n] := ParseType; | |
| 2736 | - args.pos[args.n] := SpanFrom(start); | |
| 2737 | - inc(args.n); | |
| 2738 | - if tok <> sComma then break; | |
| 2739 | - Next; | |
| 2740 | - until false; | |
| 2741 | - Expect(sRparen); | |
| 2742 | -end; | |
| 2743 | - | |
| 2744 | -{ Past an instance's arguments, to be read when it is made } | |
| 2745 | -procedure SkipArgs; | |
| 2746 | -var p: longint; | |
| 2747 | -begin | |
| 2748 | - p := 0; | |
| 2749 | - repeat | |
| 2750 | - if tok = sLparen then inc(p) | |
| 2751 | - else if tok = sRparen then dec(p) | |
| 2752 | - else if tok in [sSemicolon, sEof] then Expected('")"'); | |
| 2753 | - Next; | |
| 2754 | - until p = 0; | |
| 2755 | -end; | |
| 2756 | - | |
| 2757 | -{ A generic module's parameters, bound to the instance's arguments: a | |
| 2758 | - type parameter names its type, a procedure parameter its procedure. } | |
| 2759 | -procedure Params(m: PModule); | |
| 2760 | -var i: longint; o, a: PObj; sig: PType; s: shortstring; at: TPos; | |
| 2761 | -begin | |
| 2762 | - i := 0; | |
| 2763 | - s := NameOf(m^.name) + '('; | |
| 2764 | - Next; | |
| 2765 | - repeat | |
| 2766 | - if tok <> sIdent then Expected('a name'); | |
| 2767 | - if i >= m^.args.n then MarkAt(m^.args.at, NameOf(m^.name) + ' needs a type for ' + NameOf(id)); | |
| 2768 | - if m^.args.o[i] <> nil then MarkAt(m^.args.pos[i], NameOf(m^.name) + ' needs a type for ' + NameOf(id)); | |
| 2769 | - o := NewObj(oType, id, m^.args.t[i]); | |
| 2770 | - o^.used := true; { a parameter of the module, not a declaration } | |
| 2771 | - Declare(o); | |
| 2772 | - s := s + TypeName(m^.args.t[i]) + ','; | |
| 2773 | - inc(i); | |
| 2774 | - Next; | |
| 2775 | - if tok <> sComma then break; | |
| 2776 | - Next; | |
| 2777 | - until false; | |
| 2778 | - while tok = sSemicolon do begin | |
| 2779 | - Next; | |
| 2780 | - if tok <> sIdent then Expected('a name'); | |
| 2781 | - at := TokPos; | |
| 2782 | - o := NewObj(oProc, id, nil); | |
| 2783 | - Next; | |
| 2784 | - Expect(sColon); | |
| 2785 | - Expect(sProc); | |
| 2786 | - sig := Signature; | |
| 2787 | - if i >= m^.args.n then MarkAt(m^.args.at, NameOf(m^.name) + ' needs a procedure for ' + NameOf(o^.name)); | |
| 2788 | - a := m^.args.o[i]; | |
| 2789 | - if a = nil then MarkAt(m^.args.pos[i], NameOf(m^.name) + ' needs a procedure for ' + NameOf(o^.name)); | |
| 2790 | - if not Same(a^.typ, sig) then begin | |
| 2791 | - Error(m^.args.pos[i], NameOf(a^.name) + ' doesn''t have the signature of ' + NameOf(o^.name)); | |
| 2792 | - Note(at, 'the signature is here'); | |
| 2793 | - Finish; | |
| 2794 | - end; | |
| 2795 | - o^.typ := a^.typ; o^.chunk := a^.chunk; o^.isCode := a^.isCode; o^.ext := a^.ext; | |
| 2796 | - o^.used := true; | |
| 2797 | - Declare(o); | |
| 2798 | - s := s + NameOf(a^.module^.name) + '.' + NameOf(a^.name) + ','; | |
| 2799 | - inc(i); | |
| 2800 | - end; | |
| 2801 | - Expect(sRparen); | |
| 2802 | - if i <> m^.args.n then MarkAt(m^.args.at, 'too many type arguments for ' + NameOf(m^.name)); | |
| 2803 | - s[length(s)] := ')'; | |
| 2804 | - m^.sname := Name(s); | |
| 2805 | -end; | |
| 2806 | - | |
| 2807 | -{ Warns of o if it is never used, or a variable only assigned. What | |
| 2808 | - is exported may be used elsewhere, and a name that starts with _ says | |
| 2809 | - it is meant to go unused. Writing a var parameter, a match's name for | |
| 2810 | - a field or C's variable is what it is for. } | |
| 2811 | -procedure Warn(o: PObj); | |
| 2812 | -var s: shortstring; | |
| 2813 | -begin | |
| 2814 | - s := NameOf(o^.name); | |
| 2815 | - if (o^.cls = oProc) and o^.openEnd then | |
| 2816 | - Warning(o^.pos, s + ' may reach its end without return, and then returns zero'); | |
| 2817 | - if o^.exported or (s[1] = '_') then exit; | |
| 2818 | - if o^.used and (o^.read or (o^.cls <> oVar) or o^.isVar or o^.ext) then exit; | |
| 2819 | - case o^.cls of | |
| 2820 | - oConst: s := 'constant ' + s; | |
| 2821 | - oType: s := 'type ' + s; | |
| 2822 | - oVar: if o^.param then s := 'parameter ' + s else s := 'variable ' + s; | |
| 2823 | - oProc: s := 'procedure ' + s; | |
| 2824 | - oModule: s := 'module ' + s; | |
| 2825 | - else exit; | |
| 2826 | - end; | |
| 2827 | - if o^.used then Warning(o^.pos, s + ' is assigned but never read') | |
| 2828 | - else if o^.cls = oModule then Warning(o^.pos, s + ' is imported but never used') | |
| 2829 | - else Warning(o^.pos, s + ' is never used'); | |
| 2830 | - if not o^.used and (o^.param or o^.ind) then | |
| 2831 | - Help('if it must be there, call it _' + NameOf(o^.name)); | |
| 2832 | -end; | |
| 2833 | - | |
| 2834 | -{ Warns of the call at p, which drops its n results } | |
| 2835 | -procedure Dropped(const p: TPos; n: longint); | |
| 2836 | -var s, blanks: shortstring; i: longint; | |
| 2837 | -begin | |
| 2838 | - s := Source(p); | |
| 2839 | - i := Pos('(', s); | |
| 2840 | - if i > 0 then s := copy(s, 1, i - 1); | |
| 2841 | - blanks := '_'; | |
| 2842 | - for i := 2 to n do blanks := blanks + ', _'; | |
| 2843 | - if n = 1 then Warning(p, 'the result of ' + s + ' is dropped') | |
| 2844 | - else Warning(p, 'the results of ' + s + ' are dropped'); | |
| 2845 | - Help('if that is meant, write ' + blanks + ' := ' + s + '(...)'); | |
| 2846 | -end; | |
| 2847 | - | |
| 2848 | -{ The warnings of module m, by where each declaration is: its own | |
| 2849 | - declarations and its procedures' are two lists, each in order, and | |
| 2850 | - firstDrop on are the calls that drop results. The | |
| 2851 | - instances of a generic module share its text, and so its warnings: | |
| 2852 | - only the first is warned of. } | |
| 2853 | -procedure Warnings(m: PModule; firstDrop: longint); | |
| 2854 | -var a, b: PObj; n: PModule; i: longint; | |
| 2855 | -begin | |
| 2856 | - n := firstModule; | |
| 2857 | - while (n <> nil) and (n^.name <> m^.name) do n := n^.next; | |
| 2858 | - if n <> nil then exit; | |
| 2859 | - a := m^.first; b := m^.locals; | |
| 2860 | - i := firstDrop; | |
| 2861 | - while (a <> nil) or (b <> nil) or (i < numDrops) do | |
| 2862 | - if (i < numDrops) and ((a = nil) or (drops[i].at.a < a^.pos.a)) and ((b = nil) or (drops[i].at.a < b^.pos.a)) then begin | |
| 2863 | - Dropped(drops[i].at, drops[i].n); | |
| 2864 | - inc(i); | |
| 2865 | - end else if (b = nil) or (a <> nil) and (a^.pos.a < b^.pos.a) then begin | |
| 2866 | - Warn(a); | |
| 2867 | - a := a^.link; | |
| 2868 | - end else begin | |
| 2869 | - Warn(b); | |
| 2870 | - b := b^.link; | |
| 2871 | - end; | |
| 2872 | -end; | |
| 2873 | - | |
| 2874 | -{ Modules being compiled, not yet finished: for finding a cycle. } | |
| 2875 | -var | |
| 2876 | - pending: array[0..63] of PModule; | |
| 2877 | - numPending: longint; | |
| 2878 | - | |
| 2879 | -function Exists(const path: shortstring): boolean; | |
| 2880 | -var st: Stat; p: shortstring; | |
| 2881 | -begin | |
| 2882 | - p := path + #0; | |
| 2883 | - Exists := (FpStat(@p[1], st) = 0) and ((st.st_mode and S_IFMT) = S_IFREG); | |
| 2884 | -end; | |
| 2885 | - | |
| 2886 | -{ Compiles module n from path, an instance if args are given, or only | |
| 2887 | - reads its interface if checkOnly; returns it. An import calls this in | |
| 2888 | - the middle of reading another file, so it is re-entrant: it saves the | |
| 2889 | - scanner's state, the open scopes, the current module and file and | |
| 2890 | - whether checks are on, and puts them back at the end. } | |
| 2891 | -function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule; | |
| 2892 | -var | |
| 2893 | - st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint; | |
| 2894 | - oldMod: PModule; oldCheck, oldChecks: boolean; m: PModule; | |
| 2895 | - lib: PLib; start: TPos; alias, mname, line, oldPath: longint; o: PObj; body: longint; iargs: TArgs; | |
| 2896 | - generic: boolean; argsAt: TScanState; why: shortstring; firstDrop: longint; | |
| 2897 | -begin | |
| 2898 | - firstDrop := numDrops; | |
| 2899 | - SaveScan(st); | |
| 2900 | - SaveScopes(d, saved); | |
| 2901 | - oldMod := curModule; oldCheck := checkMode; oldPath := curFile; oldChecks := checks; | |
| 2902 | - checks := true; | |
| 2903 | - curFile := Name(path); | |
| 2904 | - why := Open(path); | |
| 2905 | - if why <> '' then Fatal('can''t read ' + path + ': ' + why); | |
| 2906 | - New(m); | |
| 2907 | - FillChar(m^, SizeOf(TModule), 0); | |
| 2908 | - m^.name := n; | |
| 2909 | - m^.sname := n; | |
| 2910 | - m^.args := args; | |
| 2911 | - m^.scope := NewScope; | |
| 2912 | - m^.path := path; | |
| 2913 | - m^.body := -1; | |
| 2914 | - m^.checkOnly := checkOnly; | |
| 2915 | - curModule := m; | |
| 2916 | - checkMode := checkOnly; | |
| 2917 | - if not checkOnly then begin | |
| 2918 | - if numPending > High(pending) then Mark('imports nested more than ' + IntStr(High(pending) + 1) + ' deep'); | |
| 2919 | - pending[numPending] := m; | |
| 2920 | - inc(numPending); | |
| 2921 | - end; | |
| 2922 | - OpenScope(m^.scope); | |
| 2923 | - Next; | |
| 2924 | - Expect(sModule); | |
| 2925 | - if (tok <> sIdent) or (id <> n) then Mark('expected module ' + NameOf(n) + ', found ' + Found); | |
| 2926 | - Next; | |
| 2927 | - { the module first pending is the one named on the command line, | |
| 2928 | - which nothing imports, so no arguments come with it } | |
| 2929 | - if (tok = sLparen) and not checkOnly and (numPending = 1) then | |
| 2930 | - Mark(NameOf(n) + ' is generic: it is compiled where a module imports it') | |
| 2931 | - else if tok = sLparen then Params(m) | |
| 2932 | - else if args.n > 0 then MarkAt(args.at, NameOf(n) + ' isn''t generic'); | |
| 2933 | - Expect(sSemicolon); | |
| 2934 | - while tok = sImport do begin | |
| 2935 | - Next; | |
| 2936 | - repeat | |
| 2937 | - if tok <> sIdent then Expected('a module name'); | |
| 2938 | - start := TokPos; | |
| 2939 | - alias := id; | |
| 2940 | - mname := id; | |
| 2941 | - iargs.n := 0; | |
| 2942 | - generic := false; | |
| 2943 | - Next; | |
| 2944 | - if tok = sBecomes then begin | |
| 2945 | - Next; | |
| 2946 | - if tok <> sIdent then Expected('a module name'); | |
| 2947 | - mname := id; | |
| 2948 | - Next; | |
| 2949 | - if tok = sLparen then begin | |
| 2950 | - generic := true; | |
| 2951 | - SaveScan(argsAt); | |
| 2952 | - SkipArgs; | |
| 2953 | - end; | |
| 2954 | - end; | |
| 2955 | - iargs.at := SpanFrom(start); | |
| 2956 | - o := NewObj(oModule, alias, nil); | |
| 2957 | - o^.pos := start; | |
| 2958 | - Declare(o); | |
| 2959 | - if not generic then begin | |
| 2960 | - o^.module := ImportModule(mname, iargs); | |
| 2961 | - AddImport(m, o^.module); | |
| 2962 | - end else begin | |
| 2963 | - o^.module := nil; { until the instance is made, at its first use } | |
| 2964 | - if numDeferred > High(deferred) then Mark('more than ' + IntStr(High(deferred) + 1) + ' generic imports'); | |
| 2965 | - deferred[numDeferred].o := o; | |
| 2966 | - deferred[numDeferred].m := m; | |
| 2967 | - deferred[numDeferred].mname := mname; | |
| 2968 | - deferred[numDeferred].args := argsAt; | |
| 2969 | - deferred[numDeferred].at := iargs.at; | |
| 2970 | - inc(numDeferred); | |
| 2971 | - end; | |
| 2972 | - if tok <> sComma then break; | |
| 2973 | - Next; | |
| 2974 | - until false; | |
| 2975 | - Expect(sSemicolon); | |
| 2976 | - end; | |
| 2977 | - while tok = sLink do begin | |
| 2978 | - Next; | |
| 2979 | - if (tok = sIdent) and (id = nameStatic) then m^.linkStatic := true | |
| 2980 | - else if tok = sString then begin | |
| 2981 | - New(lib); | |
| 2982 | - lib^.name := Intern(@sbuf, slen); | |
| 2983 | - lib^.next := m^.libs; | |
| 2984 | - m^.libs := lib; | |
| 2985 | - m^.usesC := true; | |
| 2986 | - end else Expected('a library or "static"'); | |
| 2987 | - Next; | |
| 2988 | - Expect(sSemicolon); | |
| 2989 | - end; | |
| 2990 | - { check off;: the module's index and slice checks left out; neither | |
| 2991 | - word is reserved } | |
| 2992 | - if (tok = sIdent) and (id = nameCheck) then begin | |
| 2993 | - Next; | |
| 2994 | - if (tok <> sIdent) or (id <> nameOff) then Expected('"off"'); | |
| 2995 | - Next; | |
| 2996 | - Expect(sSemicolon); | |
| 2997 | - checks := false; | |
| 2998 | - end; | |
| 2999 | - Declarations(true); | |
| 3000 | - if tok = sBegin then begin | |
| 3001 | - if checkMode then SkipBody | |
| 3002 | - else begin | |
| 3003 | - InstancesFor(true); | |
| 3004 | - line := CurLine; | |
| 3005 | - Next; | |
| 3006 | - body := NewChunk(secText, m^.sname, nameBody); | |
| 3007 | - m^.body := body; | |
| 3008 | - BeginProc(tBodySig, nil); | |
| 3009 | - deferSlot := 0; | |
| 3010 | - curResult := nil; | |
| 3011 | - EnterBody(body, line); | |
| 3012 | - stray := false; elseIf := false; | |
| 3013 | - Statements; | |
| 3014 | - NoteLine(CurLine, CurCol); | |
| 3015 | - Expect(sEnd); | |
| 3016 | - EndProc(body, false); | |
| 3017 | - end; | |
| 3018 | - end else Expect(sEnd); | |
| 3019 | - if (tok <> sIdent) or (id <> n) then EndMissing(MissingPos, 'expected ' + NameOf(n) + ', found ' + Found); | |
| 3020 | - Next; | |
| 3021 | - if tok <> sPeriod then Expected('"."'); | |
| 3022 | - o := m^.first; | |
| 3023 | - while o <> nil do begin | |
| 3024 | - if (o^.cls = oProc) and o^.fwd then Error(TokPos, NameOf(o^.name) + ' is declared forward but never defined'); | |
| 3025 | - o := o^.link; | |
| 3026 | - end; | |
| 3027 | - if (errors = 0) and not checkOnly then Warnings(m, firstDrop); | |
| 3028 | - numDrops := firstDrop; | |
| 3029 | - if errors > 0 then Finish; { a module with errors stops the compiler after it } | |
| 3030 | - CloseScope; | |
| 3031 | - m^.done := true; | |
| 3032 | - if not checkOnly then begin | |
| 3033 | - dec(numPending); | |
| 3034 | - if lastModule = nil then firstModule := m else lastModule^.next := m; | |
| 3035 | - lastModule := m; | |
| 3036 | - end; | |
| 3037 | - curModule := oldMod; checkMode := oldCheck; curFile := oldPath; checks := oldChecks; | |
| 3038 | - RestoreScopes(d, saved); | |
| 3039 | - RestoreScan(st); | |
| 3040 | - CompileFile := m; | |
| 3041 | -end; | |
| 3042 | - | |
| 3043 | -{ The interface check: b must export just what a does. } | |
| 3044 | -procedure CompareIface(a, b: PModule); | |
| 3045 | -var o, q: PObj; | |
| 3046 | -begin | |
| 3047 | - o := a^.first; | |
| 3048 | - while o <> nil do begin | |
| 3049 | - if o^.exported then begin | |
| 3050 | - q := Find(b^.scope, o^.name); | |
| 3051 | - if (q = nil) or not q^.exported or (Fingerprint(o) <> Fingerprint(q)) then | |
| 3052 | - Fatal(b^.path + ': ' + NameOf(o^.name) + ' isn''t exported as in ' + a^.path); | |
| 3053 | - end; | |
| 3054 | - o := o^.link; | |
| 3055 | - end; | |
| 3056 | - q := b^.first; | |
| 3057 | - while q <> nil do begin | |
| 3058 | - o := Find(a^.scope, q^.name); | |
| 3059 | - if q^.exported and ((o = nil) or not o^.exported) then | |
| 3060 | - Fatal(b^.path + ': ' + NameOf(q^.name) + ' isn''t exported by ' + a^.path); | |
| 3061 | - q := q^.link; | |
| 3062 | - end; | |
| 3063 | -end; | |
| 3064 | - | |
| 3065 | -{ Reads every other file of module n in dir, and checks its interface | |
| 3066 | - against m, compiled from the file named chosen. } | |
| 3067 | -procedure CheckSiblings(const dir, chosen: shortstring; n: longint; m: PModule; const args: TArgs); | |
| 3068 | -var | |
| 3069 | - dp: pDir; e: pDirent; f, prefix: shortstring; i: longint; | |
| 3070 | - others: array[0..31] of shortstring; numOthers: longint; | |
| 3071 | -begin | |
| 3072 | - prefix := NameOf(n) + '.'; | |
| 3073 | - numOthers := 0; | |
| 3074 | - dp := FpOpenDir(dir); | |
| 3075 | - if dp = nil then exit; | |
| 3076 | - repeat | |
| 3077 | - e := FpReadDir(dp^); | |
| 3078 | - if e = nil then break; | |
| 3079 | - f := ''; | |
| 3080 | - i := 0; | |
| 3081 | - while (e^.d_name[i] <> #0) and (i < 255) do begin | |
| 3082 | - f := f + e^.d_name[i]; | |
| 3083 | - inc(i); | |
| 3084 | - end; | |
| 3085 | - if (copy(f, 1, length(prefix)) = prefix) and (length(f) > 4) | |
| 3086 | - and (copy(f, length(f) - 3, 4) = '.raz') and (f <> chosen) | |
| 3087 | - and (numOthers <= High(others)) then begin | |
| 3088 | - others[numOthers] := f; | |
| 3089 | - inc(numOthers); | |
| 3090 | - end; | |
| 3091 | - until false; | |
| 3092 | - FpCloseDir(dp^); | |
| 3093 | - for i := 0 to numOthers - 1 do | |
| 3094 | - CompareIface(m, CompileFile(dir + '/' + others[i], n, args, true)); | |
| 3095 | -end; | |
| 3096 | - | |
| 3097 | -const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, ''); | |
| 3098 | - | |
| 3099 | -{ The directories searched for modules, in order, from -1 to numInc } | |
| 3100 | -function SearchDir(i: longint): shortstring; | |
| 3101 | -begin | |
| 3102 | - if i = -1 then SearchDir := mainDir | |
| 3103 | - else if i = numInc then SearchDir := libDir | |
| 3104 | - else SearchDir := incDirs[i]; | |
| 3105 | -end; | |
| 3106 | - | |
| 3107 | -function ModuleFile(n: longint; var dir, f: shortstring): boolean; | |
| 3108 | -var i, j: longint; | |
| 3109 | -begin | |
| 3110 | - for i := -1 to numInc do begin | |
| 3111 | - dir := SearchDir(i); | |
| 3112 | - for j := 0 to 3 do begin | |
| 3113 | - f := NameOf(n) + Suffixes[j] + '.raz'; | |
| 3114 | - if Exists(dir + '/' + f) then exit(true); | |
| 3115 | - end; | |
| 3116 | - end; | |
| 3117 | - ModuleFile := false; | |
| 3118 | -end; | |
| 3119 | - | |
| 3120 | -{ A program has one module of each name: module n, found as dir/f, | |
| 3121 | - must have no files in a later directory, unless dir/f is a link to | |
| 3122 | - one of them, which chooses it (report, section 3) } | |
| 3123 | -procedure CheckOneModule(n: longint; const dir, f: shortstring; const at: TPos); | |
| 3124 | -var i, j: longint; past, chosen: boolean; g, files, others: shortstring; | |
| 3125 | - st, mine: Stat; | |
| 3126 | -begin | |
| 3127 | - g := dir + '/' + f + #0; | |
| 3128 | - FpStat(@g[1], mine); | |
| 3129 | - others := ''; | |
| 3130 | - past := false; | |
| 3131 | - for i := -1 to numInc do begin | |
| 3132 | - if past then begin | |
| 3133 | - files := ''; | |
| 3134 | - chosen := false; | |
| 3135 | - for j := 0 to 3 do begin | |
| 3136 | - g := SearchDir(i) + '/' + NameOf(n) + Suffixes[j] + '.raz'; | |
| 3137 | - if Exists(g) then begin | |
| 3138 | - files := files + ' and ' + ShownPath(g); | |
| 3139 | - g := g + #0; | |
| 3140 | - FpStat(@g[1], st); | |
| 3141 | - chosen := chosen or ((st.st_dev = mine.st_dev) and (st.st_ino = mine.st_ino)); | |
| 3142 | - end; | |
| 3143 | - end; | |
| 3144 | - if not chosen then others := others + files; | |
| 3145 | - end; | |
| 3146 | - past := past or (SearchDir(i) = dir); | |
| 3147 | - end; | |
| 3148 | - if others = '' then exit; | |
| 3149 | - Error(at, 'module ' + NameOf(n) + ' is in ' + ShownPath(dir + '/' + f) + others); | |
| 3150 | - Help('a program has one module of each name; a symbolic link to one of these files chooses it'); | |
| 3151 | - Fail; | |
| 3152 | -end; | |
| 3153 | - | |
| 3154 | -function ImportModule(name: longint; const args: TArgs): PModule; | |
| 3155 | -var m: PModule; i: longint; dir, f: shortstring; | |
| 3156 | -begin | |
| 3157 | - m := FindModule(name, args); | |
| 3158 | - if m <> nil then exit(m); | |
| 3159 | - for i := 0 to numPending - 1 do | |
| 3160 | - if IsModule(pending[i], name, args) then MarkAt(args.at, NameOf(name) + ' imports itself'); | |
| 3161 | - if not ModuleFile(name, dir, f) then begin | |
| 3162 | - Error(args.at, 'can''t find module ' + NameOf(name)); | |
| 3163 | - dir := ShownPath(SearchDir(-1)); | |
| 3164 | - for i := 0 to numInc do dir := dir + ', ' + ShownPath(SearchDir(i)); | |
| 3165 | - Help('looked for ' + NameOf(name) + '.raz in ' + dir); | |
| 3166 | - Fail; | |
| 3167 | - end; | |
| 3168 | - CheckOneModule(name, dir, f, args.at); | |
| 3169 | - m := CompileFile(dir + '/' + f, name, args, false); | |
| 3170 | - CheckSiblings(dir, f, name, m, args); | |
| 3171 | - ImportModule := m; | |
| 3172 | -end; | |
| 3173 | - | |
| 3174 | -procedure AddImport(m, im: PModule); | |
| 3175 | -var imp: PImport; | |
| 3176 | -begin | |
| 3177 | - New(imp); | |
| 3178 | - imp^.module := im; | |
| 3179 | - imp^.next := m^.imports; | |
| 3180 | - m^.imports := imp; | |
| 3181 | -end; | |
| 3182 | - | |
| 3183 | -{ Makes the instance that deferred[i] imports: its arguments are read | |
| 3184 | - where the import wrote them, and must be declared by now. Compiling | |
| 3185 | - the instance comes in the middle of a declaration, so what that | |
| 3186 | - declaration has under way is kept aside meanwhile. } | |
| 3187 | -procedure MakeInstance(i: longint; const at: TPos); | |
| 3188 | -var | |
| 3189 | - d: TDeferred; st: TScanState; args: TArgs; types, exported, ext, wasQuiet: boolean; nf, nd: longint; | |
| 3190 | - ft: array[0..255] of PType; fp: array[0..255] of TPos; decl: array[0..63] of longint; | |
| 3191 | -begin | |
| 3192 | - d := deferred[i]; | |
| 3193 | - dec(numDeferred); | |
| 3194 | - deferred[i] := deferred[numDeferred]; | |
| 3195 | - SaveScan(st); | |
| 3196 | - RestoreScan(d.args); | |
| 3197 | - args.n := 0; | |
| 3198 | - making := true; useAt := at; | |
| 3199 | - ParseArgs(args, NameOf(d.mname)); | |
| 3200 | - making := false; | |
| 3201 | - RestoreScan(st); | |
| 3202 | - args.at := d.at; | |
| 3203 | - types := inTypes; exported := declExported; ext := inExtern; wasQuiet := quiet; | |
| 3204 | - nf := numFwd; ft := fwdTypes; fp := fwdPos; nd := numDeclaring; decl := declaring; | |
| 3205 | - d.o^.module := ImportModule(d.mname, args); | |
| 3206 | - inTypes := types; declExported := exported; inExtern := ext; quiet := wasQuiet; | |
| 3207 | - numFwd := nf; fwdTypes := ft; fwdPos := fp; numDeclaring := nd; declaring := decl; | |
| 3208 | - AddImport(curModule, d.o^.module); | |
| 3209 | -end; | |
| 3210 | - | |
| 3211 | -procedure FirstUse(o: PObj; const at: TPos); | |
| 3212 | -var i: longint; | |
| 3213 | -begin | |
| 3214 | - for i := 0 to numDeferred - 1 do | |
| 3215 | - if deferred[i].o = o then begin MakeInstance(i, at); exit end; | |
| 3216 | - Fail; { its arguments had an error, already reported } | |
| 3217 | -end; | |
| 3218 | - | |
| 3219 | -{ An instance can't be compiled in the middle of another procedure's | |
| 3220 | - code, so before a procedure's code begins, from its "proc" on, or | |
| 3221 | - the module body's, from its "begin" to the end of the file, this | |
| 3222 | - reads ahead for the names of imports not yet made, and makes those. } | |
| 3223 | -procedure InstancesFor(wholeRest: boolean); | |
| 3224 | -var st: TScanState; d, i: longint; begun: boolean; | |
| 3225 | -begin | |
| 3226 | - if checkMode then exit; | |
| 3227 | - i := numDeferred - 1; | |
| 3228 | - while (i >= 0) and (deferred[i].m <> curModule) do dec(i); | |
| 3229 | - if i < 0 then exit; | |
| 3230 | - { The procedure ends at the "end" that closes its first "begin": d | |
| 3231 | - counts the words still open that "end" or "until" closes, and begun | |
| 3232 | - says that begin was met, so that a record's end in its declarations | |
| 3233 | - doesn't stop it. A forward or code procedure has no raz body to | |
| 3234 | - read, so it stops at that word. The body reads to the end of the | |
| 3235 | - file, as wholeRest says. } | |
| 3236 | - SaveScan(st); | |
| 3237 | - d := 0; begun := false; | |
| 3238 | - repeat | |
| 3239 | - case tok of | |
| 3240 | - sIdent: for i := numDeferred - 1 downto 0 do | |
| 3241 | - if (deferred[i].m = curModule) and (deferred[i].o^.name = id) then MakeInstance(i, TokPos); | |
| 3242 | - sBegin: begin begun := begun or (d = 0); inc(d) end; | |
| 3243 | - sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: inc(d); | |
| 3244 | - sEnd, sUntil: dec(d); | |
| 3245 | - end; | |
| 3246 | - if not wholeRest and (d = 0) and (begun or (tok in [sForward, sCode])) then break; | |
| 3247 | - Next; | |
| 3248 | - until tok = sEof; | |
| 3249 | - RestoreScan(st); | |
| 3250 | -end; | |
| 3251 | - | |
| 3252 | -function CompileMain(const path: shortstring): PModule; | |
| 3253 | -var base: shortstring; i, n: longint; none: TArgs; | |
| 3254 | -begin | |
| 3255 | - none.n := 0; | |
| 3256 | - i := length(path); | |
| 3257 | - while (i > 0) and (path[i] <> '/') do dec(i); | |
| 3258 | - if i = 0 then mainDir := '.' else mainDir := copy(path, 1, i - 1); | |
| 3259 | - base := copy(path, i + 1, 255); | |
| 3260 | - i := pos('.', base); | |
| 3261 | - if i > 0 then base := copy(base, 1, i - 1); | |
| 3262 | - if base = '' then Fatal('no module in ' + path); | |
| 3263 | - n := Name(base); | |
| 3264 | - none.at := TokPos; | |
| 3265 | - if n <> nameRt then ImportModule(nameRt, none); | |
| 3266 | - if FindModule(n, none) <> nil then CompileMain := FindModule(n, none) | |
| 3267 | - else CompileMain := CompileFile(path, n, none, false); | |
| 3268 | -end; | |
| 3269 | - | |
| 3270 | -initialization | |
| 3271 | - nameRt := Name('Rt'); | |
| 3272 | - nameFail := Name('fail'); | |
| 3273 | - nameMain := Name('main'); | |
| 3274 | - nameAdr := Name('ADR'); | |
| 3275 | - nameBody := Name('body'); | |
| 3276 | - nameStatic := Name('static'); | |
| 3277 | - nameCheck := Name('check'); | |
| 3278 | - nameOff := Name('off'); | |
| 3279 | - tArgv := NewPtr(NewPtr(tU8, false), false); | |
| 3280 | - tBodySig := NewType(fProc, 8, 8); | |
| 3281 | - curLoop := nil; | |
| 3282 | - numPending := 0; | |
| 3283 | - firstModule := nil; | |
| 3284 | - lastModule := nil; | |
| 3285 | -end. |
| @@ -1,245 +0,0 @@ | ||
| 1 | -{ raz.pas: the command line. | |
| 2 | - | |
| 3 | - raz [-I dir]... [-o file] [-Werror] [-S] Main.raz | |
| 4 | - raz run [-I dir]... Main.raz [arg]... | |
| 5 | - raz --version | |
| 6 | - | |
| 7 | - Compiles the main module and every module it imports, writes the | |
| 8 | - entry point, links what it reaches, and writes the executable: by | |
| 9 | - default the main module's file without its .raz; with -S, it lists | |
| 10 | - the main module's code instead. raz run writes it to a memfd | |
| 11 | - instead, a file that lives only in memory, and executes it in raz's | |
| 12 | - place with the arguments after Main.raz, so its status is the | |
| 13 | - program's; its argv[0] is Main.raz. | |
| 14 | - | |
| 15 | - The program starts by saving argc and argv, runs every module's body | |
| 16 | - (those of the modules the program imports, imported modules first), | |
| 17 | - then the main module's main, and ends with Rt.exit and main's | |
| 18 | - status. A static program does this from its entry point. One that | |
| 19 | - uses C starts through the C library, __libc_start_main, which sets | |
| 20 | - C up and calls this as C's main; first it gives Rt C's exit, which | |
| 21 | - Rt.exit then ends the program with. | |
| 22 | - | |
| 23 | - Depends on: everything else. | |
| 24 | - Leaves out: the work itself; the other units compile, link and | |
| 25 | - write, and this only reads the command line and runs them in order. } | |
| 26 | - | |
| 27 | -program raz; | |
| 28 | - | |
| 29 | -{$mode objfpc}{$H-} | |
| 30 | - | |
| 31 | -uses math, BaseUnix, syscall, scan, diag, sym, chunk, x64, gen, parse, link, elf; | |
| 32 | - | |
| 33 | -const | |
| 34 | - { what git describe says, made by make: 0.3.0 at the tag v0.3.0, | |
| 35 | - 0.3.0-5-g38d29a8 five commits past it } | |
| 36 | - Version = {$I version.inc}; | |
| 37 | - Report = '2026-10.21'; { the version of doc/raz.md it follows } | |
| 38 | - MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } | |
| 39 | - MfdCloexec = 1; { an ELF still executes: the kernel opens it first } | |
| 40 | - | |
| 41 | -{ The modules the program uses: the main module, Rt, and every module | |
| 42 | - they import. A module read only for the interface check is not. } | |
| 43 | -procedure Use(m: PModule); | |
| 44 | -var i: PImport; | |
| 45 | -begin | |
| 46 | - if m^.used then exit; | |
| 47 | - m^.used := true; | |
| 48 | - i := m^.imports; | |
| 49 | - while i <> nil do begin | |
| 50 | - Use(i^.module); | |
| 51 | - i := i^.next; | |
| 52 | - end; | |
| 53 | -end; | |
| 54 | - | |
| 55 | -{ Whether the program uses C: if so, the libraries it needs go to | |
| 56 | - elf.pas, the C library first. } | |
| 57 | -function UsesC: boolean; | |
| 58 | -var m, st, c: PModule; l: PLib; i: longint; seen: boolean; | |
| 59 | -begin | |
| 60 | - st := nil; c := nil; | |
| 61 | - numNeeded := 1; | |
| 62 | - needed[0] := Intern(PChar('libc.so.6'), 9); | |
| 63 | - m := firstModule; | |
| 64 | - while m <> nil do begin | |
| 65 | - if m^.used and m^.linkStatic then st := m; | |
| 66 | - if m^.used and m^.usesC then c := m; | |
| 67 | - l := m^.libs; | |
| 68 | - while m^.used and (l <> nil) do begin | |
| 69 | - seen := false; | |
| 70 | - for i := 0 to numNeeded - 1 do seen := seen or (needed[i] = l^.name); | |
| 71 | - if not seen then begin | |
| 72 | - if numNeeded > High(needed) then Fatal('more than ' + IntStr(High(needed) + 1) + ' libraries'); | |
| 73 | - needed[numNeeded] := l^.name; | |
| 74 | - inc(numNeeded); | |
| 75 | - end; | |
| 76 | - l := l^.next; | |
| 77 | - end; | |
| 78 | - m := m^.next; | |
| 79 | - end; | |
| 80 | - if (st <> nil) and (c <> nil) then | |
| 81 | - Fatal(NameOf(st^.name) + ' says link static, but ' + NameOf(c^.name) + ' uses C'); | |
| 82 | - if c = nil then numNeeded := 0; | |
| 83 | - UsesC := c <> nil; | |
| 84 | -end; | |
| 85 | - | |
| 86 | -{ What raz -h says } | |
| 87 | -procedure Usage; | |
| 88 | -begin | |
| 89 | - writeln('usage: raz [option]... Main.raz compiles Main.raz and the modules it imports'); | |
| 90 | - writeln(' raz run [option]... Main.raz [arg]... compiles it and runs it, with the args'); | |
| 91 | - writeln('options:'); | |
| 92 | - writeln(' -o file the program''s file, Main without .raz if left out'); | |
| 93 | - writeln(' -I dir looks for imported modules in dir too, after Main.raz''s own'); | |
| 94 | - writeln(' -Werror a warning counts as an error'); | |
| 95 | - writeln(' -S lists the main module''s code, under each line of source'); | |
| 96 | - writeln(' -s strips symbols, line numbers, the places a failure is told'); | |
| 97 | - writeln(' by and the profiler: a failure says only its address'); | |
| 98 | - writeln(' -noprofile leaves out the profiler, which RAZ_PROFILE turns on'); | |
| 99 | - writeln(' -h, --help says this'); | |
| 100 | - writeln(' --version says the version, and the report it follows'); | |
| 101 | - writeln('see raz(1), and the report, doc/raz.md'); | |
| 102 | -end; | |
| 103 | - | |
| 104 | -function RtObj(rt: PModule; const n: shortstring): PObj; | |
| 105 | -var o: PObj; | |
| 106 | -begin | |
| 107 | - o := Find(rt^.scope, Intern(@n[1], length(n))); | |
| 108 | - if o = nil then Fatal('Rt has no ' + n); | |
| 109 | - RtObj := o; | |
| 110 | -end; | |
| 111 | - | |
| 112 | -var | |
| 113 | - path, outPath, dir, s: shortstring; | |
| 114 | - i, first, entry, fd: longint; | |
| 115 | - m, rt: PModule; | |
| 116 | - main: PObj; | |
| 117 | - run, noProfile: boolean; | |
| 118 | - links: array[0..3] of TLink; | |
| 119 | - numLinks: longint; | |
| 120 | - | |
| 121 | - | |
| 122 | -{ Rt is given the address of chunk, in its variable v } | |
| 123 | -procedure AddLink(chunk: longint; v: PObj); | |
| 124 | -begin | |
| 125 | - links[numLinks].chunk := chunk; | |
| 126 | - links[numLinks].into := v^.chunk; | |
| 127 | - inc(numLinks); | |
| 128 | -end; | |
| 129 | - | |
| 130 | -begin | |
| 131 | - { constants fold as the machine computes: NaN and infinities, no traps } | |
| 132 | - SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); | |
| 133 | - path := ''; | |
| 134 | - outPath := ''; | |
| 135 | - numInc := 0; | |
| 136 | - noProfile := false; | |
| 137 | - run := (ParamCount >= 1) and (ParamStr(1) = 'run'); | |
| 138 | - i := 1 + ord(run); | |
| 139 | - while i <= ParamCount do begin | |
| 140 | - s := ParamStr(i); | |
| 141 | - if s = '-I' then begin | |
| 142 | - inc(i); | |
| 143 | - if numInc > High(incDirs) then Fatal('more than ' + IntStr(High(incDirs) + 1) + ' -I directories'); | |
| 144 | - incDirs[numInc] := ParamStr(i); | |
| 145 | - inc(numInc); | |
| 146 | - end else if s = '-o' then begin | |
| 147 | - inc(i); | |
| 148 | - outPath := ParamStr(i); | |
| 149 | - end else if s = '-Werror' then | |
| 150 | - werror := true | |
| 151 | - else if s = '-S' then | |
| 152 | - listing := true | |
| 153 | - else if s = '-s' then | |
| 154 | - stripped := true | |
| 155 | - else if s = '-noprofile' then | |
| 156 | - noProfile := true | |
| 157 | - else if (s = '-h') or (s = '--help') then begin | |
| 158 | - Usage; | |
| 159 | - Halt(0); | |
| 160 | - end else if s = '--version' then begin | |
| 161 | - writeln('raz ', Version, ', report ', Report); | |
| 162 | - Halt(0); | |
| 163 | - end else if (s <> '') and (s[1] = '-') then | |
| 164 | - Fatal('unknown option ' + s) | |
| 165 | - else begin | |
| 166 | - path := s; | |
| 167 | - first := i; | |
| 168 | - if run then break; { the rest are the program's } | |
| 169 | - end; | |
| 170 | - inc(i); | |
| 171 | - end; | |
| 172 | - if path = '' then begin | |
| 173 | - Usage; | |
| 174 | - Halt(1); | |
| 175 | - end; | |
| 176 | - if run and (outPath <> '') then Fatal('raz run takes no -o'); | |
| 177 | - dir := ParamStr(0); | |
| 178 | - i := length(dir); | |
| 179 | - while (i > 0) and (dir[i] <> '/') do dec(i); | |
| 180 | - if i = 0 then libDir := 'lib' else libDir := copy(dir, 1, i) + 'lib'; | |
| 181 | - if outPath = '' then begin | |
| 182 | - outPath := path; | |
| 183 | - if copy(outPath, length(outPath) - 3, 4) = '.raz' then | |
| 184 | - outPath := copy(outPath, 1, length(outPath) - 4) | |
| 185 | - else outPath := outPath + '.out'; | |
| 186 | - end; | |
| 187 | - | |
| 188 | - m := CompileMain(path); | |
| 189 | - if errors > 0 then Finish; | |
| 190 | - | |
| 191 | - Use(m); | |
| 192 | - rt := firstModule; | |
| 193 | - while (rt <> nil) and (NameOf(rt^.name) <> 'Rt') do rt := rt^.next; | |
| 194 | - if rt <> nil then Use(rt); | |
| 195 | - | |
| 196 | - main := Find(m^.scope, Intern('main', 4)); | |
| 197 | - if main <> nil then begin | |
| 198 | - if (main^.cls <> oProc) or not main^.exported then Error(main^.pos, 'main must be an exported procedure') | |
| 199 | - else if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tInt)) then | |
| 200 | - Error(main^.pos, 'main must be proc main* or proc main*: INT'); | |
| 201 | - if errors > 0 then Finish; | |
| 202 | - end; | |
| 203 | - Finish; | |
| 204 | - | |
| 205 | - if rt = nil then Fatal('can''t find module Rt'); | |
| 206 | - { what Rt is given: the places, which the linker makes, for Rt to say | |
| 207 | - where a failed check or a signal was (place), and the calls under | |
| 208 | - way then (calls); and the profiler, which needs them too. raz -s | |
| 209 | - gives none, -noprofile no profiler. } | |
| 210 | - numLinks := 0; | |
| 211 | - if not stripped then begin | |
| 212 | - placesChunk := NewChunk(secRodata, -1, -1); | |
| 213 | - SetData(placesChunk, nil, 0, 8); | |
| 214 | - AddLink(placesChunk, RtObj(rt, 'places')); | |
| 215 | - AddLink(RtObj(rt, 'calls')^.chunk, RtObj(rt, 'teller')); | |
| 216 | - AddLink(RtObj(rt, 'place')^.chunk, RtObj(rt, 'placer')); | |
| 217 | - if not noProfile then AddLink(RtObj(rt, 'startProfile')^.chunk, RtObj(rt, 'profiler')); | |
| 218 | - end; | |
| 219 | - entry := EntryPoint(UsesC, firstModule, main, RtObj(rt, 'cExit')^.chunk, | |
| 220 | - RtObj(rt, 'exit')^.chunk, links, numLinks); | |
| 221 | - | |
| 222 | - MarkLive(entry); | |
| 223 | - if not HasReals then | |
| 224 | - for i := 0 to numChunks - 1 do | |
| 225 | - if chunks^[i].live and chunks^[i].reals then | |
| 226 | - Error(chunks^[i].realsAt, 'this target has no reals'); | |
| 227 | - if errors > 0 then Finish; | |
| 228 | - if listing then begin | |
| 229 | - List(m^.sname); | |
| 230 | - exit; | |
| 231 | - end; | |
| 232 | - | |
| 233 | - mainPath := path; | |
| 234 | - if run then begin | |
| 235 | - { argv from Main.raz on is the program's, ending in nil as it must } | |
| 236 | - fd := Do_SysCall(MemfdCreate, TSysParam(PChar('raz')), MfdCloexec); | |
| 237 | - if fd < 0 then Fatal('can''t create a memfd'); | |
| 238 | - Str(fd, s); | |
| 239 | - outPath := '/proc/self/fd/' + s + #0; | |
| 240 | - WriteElf(outPath, entry); | |
| 241 | - FpExecve(@outPath[1], @argv[first], envp); | |
| 242 | - Fatal('can''t run ' + path); | |
| 243 | - end; | |
| 244 | - WriteElf(outPath, entry); | |
| 245 | -end. |
| @@ -1,886 +0,0 @@ | ||
| 1 | -{ scan.pas: the scanner. | |
| 2 | - | |
| 3 | - Reads a raz source file and hands the parser one symbol at a time: | |
| 4 | - reserved words, identifiers, numbers, characters, strings and | |
| 5 | - operators. Identifiers are interned, so that the rest of the compiler | |
| 6 | - compares names as small integers. The scanner also says where the | |
| 7 | - current symbol is, for messages. | |
| 8 | - | |
| 9 | - Depends on: BaseUnix, to read files; diag, to report errors. | |
| 10 | - Leaves out: recovery from errors in the text itself, such as a string | |
| 11 | - not closed: the first such error stops the compiler. The parser goes | |
| 12 | - on after its own errors, through recovery below. } | |
| 13 | - | |
| 14 | -unit scan; | |
| 15 | - | |
| 16 | -{$mode objfpc}{$H-} | |
| 17 | - | |
| 18 | -interface | |
| 19 | - | |
| 20 | -uses diag; | |
| 21 | - | |
| 22 | -type | |
| 23 | - TSym = (sNull, sIdent, sInt, sReal, sChar, sString, | |
| 24 | - sPlus, sMinus, sTimes, sSlash, sEql, sNeq, sLss, sLeq, sGtr, sGeq, | |
| 25 | - sBecomes, sColon, sSemicolon, sComma, sPeriod, sUpto, sEllipsis, | |
| 26 | - sLparen, sRparen, sLbrak, sRbrak, sLbrace, sRbrace, sArrow, sAmp, sTilde, sBar, | |
| 27 | - sBlank, | |
| 28 | - { the reserved words, in the order of Words below } | |
| 29 | - sAnd, sBegin, sBreak, sBy, sCode, sConst, sDefer, sDiv, sDo, sElse, | |
| 30 | - sElsif, sEnd, sExtern, sFor, sForward, sIf, sImport, sIn, sLink, | |
| 31 | - sMatch, sMod, sModule, sNot, sOf, sOr, sPacked, sProc, sRecord, | |
| 32 | - sRepeat, sReturn, sSet, sShl, sShr, sThen, sTo, sTry, sType, sUntil, sVar, | |
| 33 | - sVariant, sVolatile, sWhile, sXor, | |
| 34 | - sEof); | |
| 35 | - | |
| 36 | - PBytes = ^TBytes; | |
| 37 | - TBytes = array[0..MaxInt - 1] of byte; | |
| 38 | - PLongs = ^TLongs; | |
| 39 | - TLongs = array[0..MaxInt div 4 - 1] of longint; | |
| 40 | - | |
| 41 | - { Where the scanner is. Importing a module compiles it in the middle | |
| 42 | - of another, so the parser saves this and restores it afterwards. } | |
| 43 | - TScanState = record | |
| 44 | - buf: PChar; | |
| 45 | - len, pos, line, lineStart: longint; | |
| 46 | - tokPos, tokLine, tokLineStart: longint; | |
| 47 | - prevEnd: longint; { where the symbol before this one ends } | |
| 48 | - tok: TSym; | |
| 49 | - ival: int64; | |
| 50 | - rval: double; | |
| 51 | - id: longint; | |
| 52 | - f: longint; { the file, as diag knows it } | |
| 53 | - end; | |
| 54 | - | |
| 55 | -const | |
| 56 | - MaxIdent = 63; | |
| 57 | - MaxStr = 65536; | |
| 58 | - | |
| 59 | -var | |
| 60 | - tok: TSym; { the current symbol } | |
| 61 | - ival: int64; { sInt, sChar: its value } | |
| 62 | - rval: double; { sReal: its value } | |
| 63 | - id: longint; { sIdent: its name } | |
| 64 | - sbuf: array[0..MaxStr] of byte; { sString: its bytes, then a zero } | |
| 65 | - slen: longint; { sString: how many bytes } | |
| 66 | - | |
| 67 | -function Intern(p: PChar; n: longint): longint; | |
| 68 | -function NameOf(n: longint): shortstring; | |
| 69 | -function Reserved(n: longint): boolean; | |
| 70 | -function Open(const path: shortstring): shortstring; | |
| 71 | -procedure Next; | |
| 72 | -procedure Mark(const msg: shortstring); | |
| 73 | -procedure MarkAt(const p: TPos; const msg: shortstring); | |
| 74 | -procedure MarkHelp(const p: TPos; const msg, hint: shortstring); | |
| 75 | -procedure Fail; | |
| 76 | -function AtLineStart: boolean; | |
| 77 | -function Indent: longint; | |
| 78 | -function SymText(s: TSym): shortstring; | |
| 79 | -function Found: shortstring; | |
| 80 | - | |
| 81 | -var | |
| 82 | - { Where the parser goes on after an error: the innermost statement or | |
| 83 | - declaration being read. nil stops the compiler at the first error. } | |
| 84 | - recovery: ^jmp_buf; | |
| 85 | - { The parser is skipping what it has read once already, after an | |
| 86 | - error, so slips in it aren't reported again. } | |
| 87 | - skipping: boolean; | |
| 88 | -procedure SaveScan(var st: TScanState); | |
| 89 | -procedure RestoreScan(const st: TScanState); | |
| 90 | -function TokPos: TPos; | |
| 91 | -function AfterPrev: TPos; | |
| 92 | -function MissingPos: TPos; | |
| 93 | -function SpanFrom(const p: TPos): TPos; | |
| 94 | -function CurLine: longint; | |
| 95 | -function CurCol: longint; | |
| 96 | -procedure LineCol(a: longint; out line, col: longint); | |
| 97 | - | |
| 98 | -implementation | |
| 99 | - | |
| 100 | -uses BaseUnix; | |
| 101 | - | |
| 102 | -const | |
| 103 | - NumWords = ord(sXor) - ord(sAnd) + 1; | |
| 104 | - Words: array[0..NumWords - 1] of string[8] = ( | |
| 105 | - 'and', 'begin', 'break', 'by', 'code', 'const', 'defer', 'div', 'do', | |
| 106 | - 'else', 'elsif', 'end', 'extern', 'for', 'forward', 'if', 'import', | |
| 107 | - 'in', 'link', 'match', 'mod', 'module', 'not', 'of', 'or', 'packed', | |
| 108 | - 'proc', 'record', 'repeat', 'return', 'set', 'shl', 'shr', 'then', | |
| 109 | - 'to', 'try', 'type', 'until', 'var', 'variant', 'VOLATILE', 'while', 'xor'); | |
| 110 | - Ops: array[sPlus..sBlank] of string[3] = ( | |
| 111 | - '+', '-', '*', '/', '=', '#', '<', '<=', '>', '>=', | |
| 112 | - ':=', ':', ';', ',', '.', '..', '...', | |
| 113 | - '(', ')', '[', ']', '{', '}', '^', '&', '~', '|', '_'); | |
| 114 | - | |
| 115 | -var | |
| 116 | - cur: TScanState; { buf .. path; tok, ival, rval and id live above } | |
| 117 | - | |
| 118 | - { The name table: every distinct identifier once, in a pool of | |
| 119 | - characters, found through an open-addressed hash table. } | |
| 120 | - pool: PChar; | |
| 121 | - poolLen, poolCap: longint; | |
| 122 | - nameOff, nameLen, nameHash: PLongs; | |
| 123 | - numNames, nameCap: longint; | |
| 124 | - table: PLongs; { name + 1, or 0 for an empty slot } | |
| 125 | - tableMask: longint; | |
| 126 | - | |
| 127 | -{ Names } | |
| 128 | - | |
| 129 | -function Hash(p: PChar; n: longint): longint; | |
| 130 | -var h: longword; i: longint; | |
| 131 | -begin | |
| 132 | - h := 2166136261; | |
| 133 | - for i := 0 to n - 1 do h := (h xor byte(p[i])) * 16777619; | |
| 134 | - Hash := longint(h and $7FFFFFFF); | |
| 135 | -end; | |
| 136 | - | |
| 137 | -procedure Grow; | |
| 138 | -var i, j, n: longint; | |
| 139 | -begin | |
| 140 | - FreeMem(table); | |
| 141 | - tableMask := tableMask * 2 + 1; | |
| 142 | - table := GetMem((tableMask + 1) * 4); | |
| 143 | - FillChar(table^, (tableMask + 1) * 4, 0); | |
| 144 | - for n := 0 to numNames - 1 do begin | |
| 145 | - i := nameHash^[n] and tableMask; | |
| 146 | - while table^[i] <> 0 do i := (i + 1) and tableMask; | |
| 147 | - table^[i] := n + 1; | |
| 148 | - end; | |
| 149 | - j := nameCap * 2; | |
| 150 | - nameOff := ReAllocMem(nameOff, j * 4); | |
| 151 | - nameLen := ReAllocMem(nameLen, j * 4); | |
| 152 | - nameHash := ReAllocMem(nameHash, j * 4); | |
| 153 | - nameCap := j; | |
| 154 | -end; | |
| 155 | - | |
| 156 | -function Intern(p: PChar; n: longint): longint; | |
| 157 | -var h, i, k: longint; | |
| 158 | -begin | |
| 159 | - h := Hash(p, n); | |
| 160 | - i := h and tableMask; | |
| 161 | - while table^[i] <> 0 do begin | |
| 162 | - k := table^[i] - 1; | |
| 163 | - if (nameHash^[k] = h) and (nameLen^[k] = n) | |
| 164 | - and (CompareByte(pool[nameOff^[k]], p^, n) = 0) then exit(k); | |
| 165 | - i := (i + 1) and tableMask; | |
| 166 | - end; | |
| 167 | - if poolLen + n > poolCap then begin | |
| 168 | - poolCap := poolCap * 2 + n; | |
| 169 | - pool := ReAllocMem(pool, poolCap); | |
| 170 | - end; | |
| 171 | - Move(p^, pool[poolLen], n); | |
| 172 | - k := numNames; | |
| 173 | - nameOff^[k] := poolLen; | |
| 174 | - nameLen^[k] := n; | |
| 175 | - nameHash^[k] := h; | |
| 176 | - inc(poolLen, n); | |
| 177 | - inc(numNames); | |
| 178 | - table^[i] := k + 1; | |
| 179 | - if numNames * 2 > tableMask then Grow; | |
| 180 | - Intern := k; | |
| 181 | -end; | |
| 182 | - | |
| 183 | -function NameOf(n: longint): shortstring; | |
| 184 | -var s: shortstring; | |
| 185 | -begin | |
| 186 | - SetLength(s, nameLen^[n]); | |
| 187 | - Move(pool[nameOff^[n]], s[1], nameLen^[n]); | |
| 188 | - NameOf := s; | |
| 189 | -end; | |
| 190 | - | |
| 191 | -{ Errors } | |
| 192 | - | |
| 193 | -{ The current symbol } | |
| 194 | -function TokPos: TPos; | |
| 195 | -begin | |
| 196 | - TokPos.f := cur.f; | |
| 197 | - TokPos.a := cur.tokPos; | |
| 198 | - TokPos.b := cur.pos; | |
| 199 | -end; | |
| 200 | - | |
| 201 | -{ The place just after the symbol before the current one, where a | |
| 202 | - missing symbol belongs } | |
| 203 | -function AfterPrev: TPos; | |
| 204 | -begin | |
| 205 | - AfterPrev.f := cur.f; | |
| 206 | - AfterPrev.a := cur.prevEnd; | |
| 207 | - AfterPrev.b := cur.prevEnd; | |
| 208 | -end; | |
| 209 | - | |
| 210 | -{ Where to say a symbol is missing: at the current symbol, unless it | |
| 211 | - starts a later line than the symbol before it, which is more likely | |
| 212 | - where the missing one belongs } | |
| 213 | -function MissingPos: TPos; | |
| 214 | -var i: longint; | |
| 215 | -begin | |
| 216 | - MissingPos := TokPos; | |
| 217 | - for i := cur.prevEnd to cur.tokPos - 1 do | |
| 218 | - if cur.buf[i] = #10 then MissingPos := AfterPrev; | |
| 219 | -end; | |
| 220 | - | |
| 221 | -{ From the start of p to the end of the symbol before the current one: | |
| 222 | - the whole of a construct that has just been read } | |
| 223 | -function SpanFrom(const p: TPos): TPos; | |
| 224 | -begin | |
| 225 | - SpanFrom := p; | |
| 226 | - SpanFrom.b := cur.prevEnd; | |
| 227 | -end; | |
| 228 | - | |
| 229 | -function CurLine: longint; | |
| 230 | -begin | |
| 231 | - CurLine := cur.tokLine; | |
| 232 | -end; | |
| 233 | - | |
| 234 | -{ The line and column of byte a of the file, at or before the current | |
| 235 | - symbol: counted back from the symbol's line, so that it costs only | |
| 236 | - the distance. A column counts characters, as diag's do. } | |
| 237 | -procedure LineCol(a: longint; out line, col: longint); | |
| 238 | -var i, start: longint; | |
| 239 | -begin | |
| 240 | - line := cur.tokLine; | |
| 241 | - start := cur.tokLineStart; | |
| 242 | - while a < start do begin | |
| 243 | - dec(line); | |
| 244 | - dec(start); | |
| 245 | - while (start > 0) and (cur.buf[start - 1] <> #10) do dec(start); | |
| 246 | - end; | |
| 247 | - col := 1; | |
| 248 | - for i := start to a - 1 do | |
| 249 | - if (byte(cur.buf[i]) and $C0) <> $80 then inc(col); | |
| 250 | -end; | |
| 251 | - | |
| 252 | -function CurCol: longint; | |
| 253 | -var line: longint; | |
| 254 | -begin | |
| 255 | - LineCol(cur.tokPos, line, CurCol); | |
| 256 | -end; | |
| 257 | - | |
| 258 | -procedure Mark(const msg: shortstring); | |
| 259 | -begin | |
| 260 | - MarkAt(TokPos, msg); | |
| 261 | -end; | |
| 262 | - | |
| 263 | -{ After an error in the parser's construct, reported with its notes | |
| 264 | - and help: the parser goes on from the recovery point. } | |
| 265 | -procedure Fail; | |
| 266 | -begin | |
| 267 | - if recovery = nil then Finish; | |
| 268 | - longjmp(recovery^, 1); | |
| 269 | -end; | |
| 270 | - | |
| 271 | -procedure MarkAt(const p: TPos; const msg: shortstring); | |
| 272 | -begin | |
| 273 | - Error(p, msg); | |
| 274 | - Fail; | |
| 275 | -end; | |
| 276 | - | |
| 277 | -{ MarkAt, with a line of help after the message. } | |
| 278 | -procedure MarkHelp(const p: TPos; const msg, hint: shortstring); | |
| 279 | -begin | |
| 280 | - Error(p, msg); | |
| 281 | - Help(hint); | |
| 282 | - Fail; | |
| 283 | -end; | |
| 284 | - | |
| 285 | -{ An error in the text at bytes a to b of the file being read, which | |
| 286 | - stops the compiler: past it, the symbols can't be trusted. } | |
| 287 | -procedure MarkBytes(a, b: longint; const msg: shortstring); | |
| 288 | -var p: TPos; | |
| 289 | -begin | |
| 290 | - p.f := cur.f; p.a := a; p.b := b; | |
| 291 | - quiet := false; | |
| 292 | - Error(p, msg); | |
| 293 | - Finish; | |
| 294 | -end; | |
| 295 | - | |
| 296 | -procedure LexError(const msg: shortstring); | |
| 297 | -begin | |
| 298 | - MarkBytes(cur.tokPos, cur.pos, msg); | |
| 299 | -end; | |
| 300 | - | |
| 301 | -{ The n characters at the scanner are how another language writes t: | |
| 302 | - an error, but reading goes on as if t were written. } | |
| 303 | -procedure Slip(n: longint; t: TSym; const msg: shortstring); | |
| 304 | -var p: TPos; | |
| 305 | -begin | |
| 306 | - p.f := cur.f; p.a := cur.pos; p.b := cur.pos + n; | |
| 307 | - if not skipping then Error(p, msg); | |
| 308 | - inc(cur.pos, n); | |
| 309 | - tok := t; | |
| 310 | -end; | |
| 311 | - | |
| 312 | -{ How an operator or reserved word is written, quoted, for messages. } | |
| 313 | -function SymText(s: TSym): shortstring; | |
| 314 | -begin | |
| 315 | - if s in [sPlus..sBlank] then SymText := '"' + Ops[s] + '"' | |
| 316 | - else SymText := '"' + Words[ord(s) - ord(sAnd)] + '"'; | |
| 317 | -end; | |
| 318 | - | |
| 319 | -{ The current symbol, for "expected ..., found ...": a name as it is, | |
| 320 | - a literal by its kind, an operator or reserved word quoted. } | |
| 321 | -function Found: shortstring; | |
| 322 | -begin | |
| 323 | - case tok of | |
| 324 | - sIdent: Found := NameOf(id); | |
| 325 | - sInt, sReal: Found := 'a number'; | |
| 326 | - sChar: Found := 'a character'; | |
| 327 | - sString: Found := 'a string'; | |
| 328 | - sEof: Found := 'the end of the file'; | |
| 329 | - else Found := SymText(tok); | |
| 330 | - end; | |
| 331 | -end; | |
| 332 | - | |
| 333 | -{ The current symbol is the first on its line: at the top level of a | |
| 334 | - module, by the usual layout, so where reading can start again. } | |
| 335 | -function AtLineStart: boolean; | |
| 336 | -begin | |
| 337 | - AtLineStart := cur.tokPos = cur.tokLineStart; | |
| 338 | -end; | |
| 339 | - | |
| 340 | -{ The column the current symbol's line is indented to } | |
| 341 | -function Indent: longint; | |
| 342 | -var i: longint; | |
| 343 | -begin | |
| 344 | - i := cur.tokLineStart; | |
| 345 | - while cur.buf[i] in [' ', #9] do inc(i); | |
| 346 | - Indent := i - cur.tokLineStart + 1; | |
| 347 | -end; | |
| 348 | - | |
| 349 | -{ Files } | |
| 350 | - | |
| 351 | -{ Name n is a reserved word } | |
| 352 | -function Reserved(n: longint): boolean; | |
| 353 | -begin | |
| 354 | - Reserved := n < NumWords; | |
| 355 | -end; | |
| 356 | - | |
| 357 | -{ Reads the file at path to scan it; '' if it can, else why not } | |
| 358 | -function Open(const path: shortstring): shortstring; | |
| 359 | -var fd: cint; st: Stat; n, got, r: longint; p: shortstring; | |
| 360 | -begin | |
| 361 | - p := path + #0; | |
| 362 | - fd := FpOpen(@p[1], O_RDONLY); | |
| 363 | - if fd < 0 then | |
| 364 | - case FpGetErrno of | |
| 365 | - ESysENOENT: exit('there is no such file'); | |
| 366 | - ESysEACCES: exit('permission denied'); | |
| 367 | - else exit('it can''t be opened'); | |
| 368 | - end; | |
| 369 | - if FpFStat(fd, st) <> 0 then begin FpClose(fd); exit('it can''t be opened') end; | |
| 370 | - if (st.st_mode and S_IFMT) <> S_IFREG then begin FpClose(fd); exit('it isn''t a file') end; | |
| 371 | - n := st.st_size; | |
| 372 | - cur.buf := GetMem(n + 1); | |
| 373 | - got := 0; | |
| 374 | - while got < n do begin | |
| 375 | - r := FpRead(fd, cur.buf[got], n - got); | |
| 376 | - if r <= 0 then Fatal('can''t read ' + path); | |
| 377 | - got := got + r; | |
| 378 | - end; | |
| 379 | - FpClose(fd); | |
| 380 | - cur.buf[n] := #0; { the sentinel that ends every scan } | |
| 381 | - cur.len := n; | |
| 382 | - cur.pos := 0; | |
| 383 | - cur.line := 1; | |
| 384 | - cur.lineStart := 0; | |
| 385 | - cur.tokPos := 0; | |
| 386 | - cur.tokLine := 1; | |
| 387 | - cur.tokLineStart := 0; | |
| 388 | - cur.f := AddFile(path, cur.buf, n); | |
| 389 | - Open := ''; | |
| 390 | -end; | |
| 391 | - | |
| 392 | -procedure SaveScan(var st: TScanState); | |
| 393 | -begin | |
| 394 | - st := cur; | |
| 395 | - st.tok := tok; st.ival := ival; st.rval := rval; st.id := id; | |
| 396 | -end; | |
| 397 | - | |
| 398 | -procedure RestoreScan(const st: TScanState); | |
| 399 | -begin | |
| 400 | - cur := st; | |
| 401 | - tok := st.tok; ival := st.ival; rval := st.rval; id := st.id; | |
| 402 | -end; | |
| 403 | - | |
| 404 | -{ UTF-8 } | |
| 405 | - | |
| 406 | -{ Decodes the character at buf[pos], which is not ASCII, checking that | |
| 407 | - it is well formed; returns its code point and moves pos past it. } | |
| 408 | -function Utf8(var pos: longint): longint; | |
| 409 | -var b0, b1, n, i, cp: longint; lo, hi: byte; | |
| 410 | -begin | |
| 411 | - b0 := byte(cur.buf[pos]); | |
| 412 | - lo := $80; hi := $BF; | |
| 413 | - case b0 of | |
| 414 | - $C2..$DF: begin n := 1; cp := b0 and $1F end; | |
| 415 | - $E0: begin n := 2; cp := 0; lo := $A0 end; | |
| 416 | - $E1..$EC, $EE, $EF: begin n := 2; cp := b0 and $0F end; | |
| 417 | - $ED: begin n := 2; cp := $0D; hi := $9F end; { no surrogates } | |
| 418 | - $F0: begin n := 3; cp := 0; lo := $90 end; | |
| 419 | - $F1..$F3: begin n := 3; cp := b0 and $07 end; | |
| 420 | - $F4: begin n := 3; cp := 4; hi := $8F end; { up to 10FFFFH } | |
| 421 | - else | |
| 422 | - cur.tokPos := pos; | |
| 423 | - LexError('invalid UTF-8'); | |
| 424 | - end; | |
| 425 | - for i := 1 to n do begin | |
| 426 | - b1 := byte(cur.buf[pos + i]); | |
| 427 | - if (b1 < lo) or (b1 > hi) or (pos + i >= cur.len) then begin | |
| 428 | - cur.tokPos := pos; | |
| 429 | - LexError('bad UTF-8'); | |
| 430 | - end; | |
| 431 | - cp := cp shl 6 or (b1 and $3F); | |
| 432 | - lo := $80; hi := $BF; | |
| 433 | - end; | |
| 434 | - pos := pos + n + 1; | |
| 435 | - Utf8 := cp; | |
| 436 | -end; | |
| 437 | - | |
| 438 | -procedure Put(b: longint); | |
| 439 | -begin | |
| 440 | - if slen >= MaxStr then LexError('a string holds at most ' + IntStr(MaxStr) + ' bytes'); | |
| 441 | - sbuf[slen] := b; | |
| 442 | - inc(slen); | |
| 443 | -end; | |
| 444 | - | |
| 445 | -procedure PutUtf8(cp: longint); | |
| 446 | -begin | |
| 447 | - if cp < $80 then Put(cp) | |
| 448 | - else if cp < $800 then begin | |
| 449 | - Put($C0 or cp shr 6); Put($80 or cp and $3F) | |
| 450 | - end else if cp < $10000 then begin | |
| 451 | - Put($E0 or cp shr 12); Put($80 or cp shr 6 and $3F); Put($80 or cp and $3F) | |
| 452 | - end else begin | |
| 453 | - Put($F0 or cp shr 18); Put($80 or cp shr 12 and $3F); | |
| 454 | - Put($80 or cp shr 6 and $3F); Put($80 or cp and $3F) | |
| 455 | - end; | |
| 456 | -end; | |
| 457 | - | |
| 458 | -function HexVal(c: char): longint; | |
| 459 | -begin | |
| 460 | - case c of | |
| 461 | - '0'..'9': HexVal := ord(c) - ord('0'); | |
| 462 | - 'A'..'F': HexVal := ord(c) - ord('A') + 10; | |
| 463 | - 'a'..'f': HexVal := ord(c) - ord('a') + 10; | |
| 464 | - else HexVal := -1; | |
| 465 | - end; | |
| 466 | -end; | |
| 467 | - | |
| 468 | -{ The text of the file from byte a up to but not including b } | |
| 469 | -function Text(a, b: longint): shortstring; | |
| 470 | -var p: TPos; | |
| 471 | -begin | |
| 472 | - p.f := cur.f; p.a := a; p.b := b; | |
| 473 | - Text := Source(p); | |
| 474 | -end; | |
| 475 | - | |
| 476 | -{ One character of a character or string literal, at buf[pos]: its code | |
| 477 | - point, or for \xHH, minus one minus the byte. } | |
| 478 | -function LitChar(inString: boolean): longint; | |
| 479 | -var c: char; v, d, n, esc, digits: longint; | |
| 480 | -begin | |
| 481 | - c := cur.buf[cur.pos]; | |
| 482 | - esc := cur.pos; | |
| 483 | - if c = '\' then begin | |
| 484 | - inc(cur.pos); | |
| 485 | - c := cur.buf[cur.pos]; | |
| 486 | - inc(cur.pos); | |
| 487 | - case c of | |
| 488 | - 'n': v := 10; | |
| 489 | - 't': v := 9; | |
| 490 | - 'r': v := 13; | |
| 491 | - '0': v := 0; | |
| 492 | - '\', '''', '"': v := ord(c); | |
| 493 | - 'x': begin | |
| 494 | - if not inString then MarkBytes(esc, cur.pos, '\x is only for strings'); | |
| 495 | - d := HexVal(cur.buf[cur.pos]); | |
| 496 | - n := HexVal(cur.buf[cur.pos + 1]); | |
| 497 | - if (d < 0) or (n < 0) then MarkBytes(esc, cur.pos + 2, '\x needs two hex digits'); | |
| 498 | - inc(cur.pos, 2); | |
| 499 | - v := -1 - (d * 16 + n); | |
| 500 | - end; | |
| 501 | - 'u': begin | |
| 502 | - if cur.buf[cur.pos] <> '{' then MarkBytes(esc, cur.pos, 'expected "{" after \u'); | |
| 503 | - inc(cur.pos); | |
| 504 | - digits := cur.pos; | |
| 505 | - v := 0; n := 0; | |
| 506 | - while HexVal(cur.buf[cur.pos]) >= 0 do begin | |
| 507 | - v := v * 16 + HexVal(cur.buf[cur.pos]); | |
| 508 | - inc(n); inc(cur.pos); | |
| 509 | - if n > 6 then MarkBytes(esc, cur.pos, 'a code point has at most 6 hex digits'); | |
| 510 | - end; | |
| 511 | - if (n = 0) or (cur.buf[cur.pos] <> '}') then MarkBytes(esc, cur.pos, 'expected hex digits and "}" after \u{'); | |
| 512 | - inc(cur.pos); | |
| 513 | - if (v > $10FFFF) or ((v >= $D800) and (v <= $DFFF)) then | |
| 514 | - MarkBytes(esc, cur.pos, Text(digits, cur.pos - 1) + ' isn''t a code point'); | |
| 515 | - end; | |
| 516 | - else MarkBytes(esc, cur.pos, 'unknown escape \' + c); | |
| 517 | - end; | |
| 518 | - end else if byte(c) >= $80 then | |
| 519 | - v := Utf8(cur.pos) | |
| 520 | - else if (c < ' ') or (c = #127) then | |
| 521 | - if inString then LexError('string not closed') | |
| 522 | - else LexError('character not closed') | |
| 523 | - else begin | |
| 524 | - v := ord(c); | |
| 525 | - inc(cur.pos); | |
| 526 | - end; | |
| 527 | - LitChar := v; | |
| 528 | -end; | |
| 529 | - | |
| 530 | -{ Symbols } | |
| 531 | - | |
| 532 | -procedure Comment; | |
| 533 | -var depth, open: longint; c: char; | |
| 534 | -begin | |
| 535 | - depth := 1; | |
| 536 | - open := cur.pos; | |
| 537 | - inc(cur.pos, 2); | |
| 538 | - repeat | |
| 539 | - c := cur.buf[cur.pos]; | |
| 540 | - if c = #0 then begin | |
| 541 | - if cur.pos >= cur.len then MarkBytes(open, open + 2, 'comment not closed'); | |
| 542 | - inc(cur.pos); | |
| 543 | - end else if (c = '(') and (cur.buf[cur.pos + 1] = '*') then begin | |
| 544 | - inc(depth); inc(cur.pos, 2) | |
| 545 | - end else if (c = '*') and (cur.buf[cur.pos + 1] = ')') then begin | |
| 546 | - dec(depth); inc(cur.pos, 2) | |
| 547 | - end else if c = #10 then begin | |
| 548 | - inc(cur.pos); inc(cur.line); cur.lineStart := cur.pos | |
| 549 | - end else if byte(c) >= $80 then | |
| 550 | - Utf8(cur.pos) | |
| 551 | - else | |
| 552 | - inc(cur.pos); | |
| 553 | - until depth = 0; | |
| 554 | -end; | |
| 555 | - | |
| 556 | -{ Reading a decimal real exactly: the F64 nearest to digits times 10 to | |
| 557 | - an exponent, ties to the even one, which a library's Val doesn't | |
| 558 | - promise. The number is a fraction of two big integers, num / den, | |
| 559 | - scaled by 2^e until its whole part has 53 or 54 bits; that part and | |
| 560 | - the remainder give the bits and the rounding. } | |
| 561 | -type | |
| 562 | - TBig = record | |
| 563 | - n: longint; { words in use; 0 is zero } | |
| 564 | - w: array[0..95] of longword; { least significant first } | |
| 565 | - end; | |
| 566 | - { 96 words, 3072 bits, are plenty: a shortstring holds at most 255 | |
| 567 | - digits, so ReadReal's den is at most 10^(330 + 255), about 2^1944, | |
| 568 | - and num below 10^310; the shift that follows makes the larger one at | |
| 569 | - most 54 bits longer than den, about 2000 bits in all. } | |
| 570 | - | |
| 571 | -{ a := a * m + add } | |
| 572 | -procedure BigMul(var a: TBig; m, add: longword); | |
| 573 | -var i: longint; c: qword; | |
| 574 | -begin | |
| 575 | - c := add; | |
| 576 | - for i := 0 to a.n - 1 do begin | |
| 577 | - c := qword(a.w[i]) * m + c; | |
| 578 | - a.w[i] := longword(c); | |
| 579 | - c := c shr 32; | |
| 580 | - end; | |
| 581 | - if c <> 0 then begin a.w[a.n] := longword(c); inc(a.n) end; | |
| 582 | -end; | |
| 583 | - | |
| 584 | -procedure BigShl(var a: TBig; k: longint); | |
| 585 | -var i, words, bits: longint; | |
| 586 | -begin | |
| 587 | - if a.n = 0 then exit; | |
| 588 | - words := k div 32; bits := k mod 32; | |
| 589 | - a.w[a.n + words] := 0; | |
| 590 | - for i := a.n - 1 downto 0 do begin | |
| 591 | - if bits > 0 then a.w[i + words + 1] := a.w[i + words + 1] or (a.w[i] shr (32 - bits)); | |
| 592 | - a.w[i + words] := a.w[i] shl bits; | |
| 593 | - end; | |
| 594 | - for i := 0 to words - 1 do a.w[i] := 0; | |
| 595 | - inc(a.n, words + 1); | |
| 596 | - while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); | |
| 597 | -end; | |
| 598 | - | |
| 599 | -procedure BigShr1(var a: TBig); | |
| 600 | -var i: longint; | |
| 601 | -begin | |
| 602 | - for i := 0 to a.n - 1 do begin | |
| 603 | - a.w[i] := a.w[i] shr 1; | |
| 604 | - if i + 1 < a.n then a.w[i] := a.w[i] or (a.w[i + 1] shl 31); | |
| 605 | - end; | |
| 606 | - while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); | |
| 607 | -end; | |
| 608 | - | |
| 609 | -function BigCmp(const a, b: TBig): longint; | |
| 610 | -var i: longint; | |
| 611 | -begin | |
| 612 | - if a.n <> b.n then exit(ord(a.n > b.n) * 2 - 1); | |
| 613 | - for i := a.n - 1 downto 0 do | |
| 614 | - if a.w[i] <> b.w[i] then exit(ord(a.w[i] > b.w[i]) * 2 - 1); | |
| 615 | - BigCmp := 0; | |
| 616 | -end; | |
| 617 | - | |
| 618 | -{ a := a - b, b not above a } | |
| 619 | -procedure BigSub(var a: TBig; const b: TBig); | |
| 620 | -var i: longint; d: int64; | |
| 621 | -begin | |
| 622 | - d := 0; | |
| 623 | - for i := 0 to a.n - 1 do begin | |
| 624 | - d := d + a.w[i]; | |
| 625 | - if i < b.n then d := d - b.w[i]; | |
| 626 | - a.w[i] := longword(d); | |
| 627 | - d := -ord(d < 0); { a borrow } | |
| 628 | - end; | |
| 629 | - while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); | |
| 630 | -end; | |
| 631 | - | |
| 632 | -function BigBits(const a: TBig): longint; | |
| 633 | -var t: longword; | |
| 634 | -begin | |
| 635 | - if a.n = 0 then exit(0); | |
| 636 | - BigBits := (a.n - 1) * 32; | |
| 637 | - t := a.w[a.n - 1]; | |
| 638 | - while t <> 0 do begin inc(BigBits); t := t shr 1 end; | |
| 639 | -end; | |
| 640 | - | |
| 641 | -{ s is digits, a point, digits, and maybe E, a sign and digits. False if | |
| 642 | - the value is past the largest F64; one below the smallest is 0. } | |
| 643 | -function ReadReal(const s: shortstring; out x: double): boolean; | |
| 644 | -var | |
| 645 | - num, den, d: TBig; i, digits, e, ex: longint; neg, half, sticky: boolean; | |
| 646 | - q, b: qword; | |
| 647 | -begin | |
| 648 | - num.n := 0; den.n := 1; den.w[0] := 1; | |
| 649 | - digits := 0; e := 0; i := 1; | |
| 650 | - while (i <= length(s)) and (s[i] <> 'E') do begin | |
| 651 | - if s[i] = '.' then e := 1 | |
| 652 | - else begin | |
| 653 | - BigMul(num, 10, ord(s[i]) - ord('0')); | |
| 654 | - if num.n > 0 then inc(digits); { counted from the first that isn't 0 } | |
| 655 | - if e > 0 then inc(e); | |
| 656 | - end; | |
| 657 | - inc(i); | |
| 658 | - end; | |
| 659 | - if e > 0 then e := 1 - e; { minus the digits after the point } | |
| 660 | - ex := 0; neg := false; | |
| 661 | - if i <= length(s) then begin | |
| 662 | - inc(i); | |
| 663 | - neg := s[i] = '-'; | |
| 664 | - if s[i] in ['+', '-'] then inc(i); | |
| 665 | - for i := i to length(s) do | |
| 666 | - if ex < 100000 then ex := ex * 10 + ord(s[i]) - ord('0'); | |
| 667 | - end; | |
| 668 | - if neg then ex := -ex; | |
| 669 | - inc(e, ex); | |
| 670 | - x := 0.0; | |
| 671 | - if num.n = 0 then exit(true); | |
| 672 | - if digits + e > 310 then exit(false); { 10^310 is past every F64 } | |
| 673 | - if digits + e < -330 then exit(true); { and 10^-330 below half the least } | |
| 674 | - for i := 1 to e do BigMul(num, 10, 0); | |
| 675 | - for i := 1 to -e do BigMul(den, 10, 0); | |
| 676 | - { num / den 2^-e from 2^52 to 2^54, but not past the least exponent } | |
| 677 | - e := BigBits(num) - BigBits(den) - 53; | |
| 678 | - if e < -1074 then e := -1074; | |
| 679 | - if e < 0 then BigShl(num, -e) else BigShl(den, e); | |
| 680 | - { q, its whole part, by long division; num is left the remainder } | |
| 681 | - d := den; BigShl(d, 53); | |
| 682 | - q := 0; | |
| 683 | - for i := 53 downto 0 do begin | |
| 684 | - if BigCmp(num, d) >= 0 then begin BigSub(num, d); q := q or (qword(1) shl i) end; | |
| 685 | - BigShr1(d); | |
| 686 | - end; | |
| 687 | - if q >= qword(1) shl 53 then begin { a bit too many: it rounds } | |
| 688 | - half := odd(q); sticky := num.n > 0; | |
| 689 | - q := q shr 1; inc(e); | |
| 690 | - end else begin { the remainder against half of den } | |
| 691 | - BigShl(num, 1); | |
| 692 | - i := BigCmp(num, den); | |
| 693 | - half := i >= 0; sticky := i > 0; | |
| 694 | - end; | |
| 695 | - if half and (sticky or odd(q)) then begin | |
| 696 | - inc(q); | |
| 697 | - if q = qword(1) shl 53 then begin q := q shr 1; inc(e) end; | |
| 698 | - end; | |
| 699 | - if e > 971 then exit(false); | |
| 700 | - if q >= qword(1) shl 52 then b := qword(e + 1075) shl 52 or (q and (qword(1) shl 52 - 1)) | |
| 701 | - else b := q; { below the least normal, e is -1074 } | |
| 702 | - Move(b, x, 8); | |
| 703 | - ReadReal := true; | |
| 704 | -end; | |
| 705 | - | |
| 706 | -procedure Number; | |
| 707 | -var start, i, d: longint; allDec: boolean; v: qword; s: shortstring; | |
| 708 | -begin | |
| 709 | - start := cur.pos; | |
| 710 | - allDec := true; | |
| 711 | - while cur.buf[cur.pos] in ['0'..'9', 'A'..'F'] do begin | |
| 712 | - if cur.buf[cur.pos] > '9' then allDec := false; | |
| 713 | - inc(cur.pos); | |
| 714 | - end; | |
| 715 | - if cur.buf[cur.pos] = 'H' then begin | |
| 716 | - v := 0; | |
| 717 | - for i := start to cur.pos - 1 do begin | |
| 718 | - if v shr 60 <> 0 then LexError(Text(start, cur.pos) + ' is too big for U64'); | |
| 719 | - v := v * 16 + qword(HexVal(cur.buf[i])); | |
| 720 | - end; | |
| 721 | - inc(cur.pos); | |
| 722 | - tok := sInt; | |
| 723 | - ival := int64(v); { 16 digits are the bits of a 64-bit word } | |
| 724 | - end else if (cur.buf[cur.pos] = '.') and (cur.buf[cur.pos + 1] in ['0'..'9']) then begin | |
| 725 | - if not allDec then LexError('a decimal number has only the digits 0 to 9'); | |
| 726 | - inc(cur.pos); | |
| 727 | - while cur.buf[cur.pos] in ['0'..'9'] do inc(cur.pos); | |
| 728 | - if cur.buf[cur.pos] = 'E' then begin | |
| 729 | - inc(cur.pos); | |
| 730 | - if cur.buf[cur.pos] in ['+', '-'] then inc(cur.pos); | |
| 731 | - if not (cur.buf[cur.pos] in ['0'..'9']) then LexError('expected a digit in the exponent'); | |
| 732 | - while cur.buf[cur.pos] in ['0'..'9'] do inc(cur.pos); | |
| 733 | - end; | |
| 734 | - if cur.pos - start > 255 then LexError('a number has at most 255 characters'); | |
| 735 | - SetLength(s, cur.pos - start); | |
| 736 | - Move(cur.buf[start], s[1], cur.pos - start); | |
| 737 | - if not ReadReal(s, rval) then LexError(Text(start, cur.pos) + ' is out of the range of F64'); | |
| 738 | - tok := sReal; | |
| 739 | - end else begin | |
| 740 | - if not allDec then LexError('a hex number ends with H'); | |
| 741 | - v := 0; | |
| 742 | - for i := start to cur.pos - 1 do begin | |
| 743 | - d := ord(cur.buf[i]) - ord('0'); | |
| 744 | - if v > (qword(high(int64)) - qword(d)) div 10 then LexError(Text(start, cur.pos) + ' is too big for I64'); | |
| 745 | - v := v * 10 + qword(d); | |
| 746 | - end; | |
| 747 | - tok := sInt; | |
| 748 | - ival := int64(v); | |
| 749 | - end; | |
| 750 | - if cur.buf[cur.pos] in ['A'..'Z', 'a'..'z', '_', '0'..'9'] then | |
| 751 | - LexError('a name can''t start with a digit'); | |
| 752 | -end; | |
| 753 | - | |
| 754 | -procedure Next; | |
| 755 | -var c: char; start, h, v, i: longint; | |
| 756 | -begin | |
| 757 | - cur.prevEnd := cur.pos; | |
| 758 | - repeat | |
| 759 | - c := cur.buf[cur.pos]; | |
| 760 | - while c in [' ', #9, #13, #10] do begin | |
| 761 | - if c = #10 then begin | |
| 762 | - inc(cur.line); cur.lineStart := cur.pos + 1 | |
| 763 | - end; | |
| 764 | - inc(cur.pos); | |
| 765 | - c := cur.buf[cur.pos]; | |
| 766 | - end; | |
| 767 | - if (c = '(') and (cur.buf[cur.pos + 1] = '*') then begin | |
| 768 | - Comment; c := ' ' | |
| 769 | - end; | |
| 770 | - until c <> ' '; | |
| 771 | - cur.tokPos := cur.pos; | |
| 772 | - cur.tokLine := cur.line; | |
| 773 | - cur.tokLineStart := cur.lineStart; | |
| 774 | - case c of | |
| 775 | - 'A'..'Z', 'a'..'z', '_': begin | |
| 776 | - start := cur.pos; | |
| 777 | - repeat inc(cur.pos) | |
| 778 | - until not (cur.buf[cur.pos] in ['A'..'Z', 'a'..'z', '0'..'9', '_']); | |
| 779 | - if cur.pos - start > MaxIdent then LexError('a name has at most ' + IntStr(MaxIdent) + ' characters'); | |
| 780 | - h := Intern(@cur.buf[start], cur.pos - start); | |
| 781 | - if (cur.pos - start = 1) and (c = '_') then tok := sBlank | |
| 782 | - else if h < NumWords then tok := TSym(ord(sAnd) + h) | |
| 783 | - else begin tok := sIdent; id := h end; | |
| 784 | - end; | |
| 785 | - '0'..'9': Number; | |
| 786 | - '''': begin | |
| 787 | - inc(cur.pos); | |
| 788 | - if cur.buf[cur.pos] = '''' then LexError('empty character'); | |
| 789 | - v := LitChar(false); | |
| 790 | - if cur.buf[cur.pos] <> '''' then begin | |
| 791 | - { 'ab': several characters, as some languages write a string } | |
| 792 | - i := cur.pos; | |
| 793 | - while not (cur.buf[i] in ['''', #10, #0]) do inc(i); | |
| 794 | - if cur.buf[i] = '''' then MarkBytes(cur.tokPos, i + 1, 'a character is one character: the string is "' + Text(cur.tokPos + 1, i) + '"') | |
| 795 | - else LexError('character not closed'); | |
| 796 | - end; | |
| 797 | - inc(cur.pos); | |
| 798 | - tok := sChar; | |
| 799 | - ival := v; | |
| 800 | - end; | |
| 801 | - '"': begin | |
| 802 | - inc(cur.pos); | |
| 803 | - slen := 0; | |
| 804 | - while cur.buf[cur.pos] <> '"' do begin | |
| 805 | - v := LitChar(true); | |
| 806 | - if v < 0 then Put(-1 - v) else PutUtf8(v); | |
| 807 | - end; | |
| 808 | - inc(cur.pos); | |
| 809 | - sbuf[slen] := 0; | |
| 810 | - tok := sString; | |
| 811 | - end; | |
| 812 | - '+': begin inc(cur.pos); tok := sPlus end; | |
| 813 | - '-': begin inc(cur.pos); tok := sMinus end; | |
| 814 | - '*': begin inc(cur.pos); tok := sTimes end; | |
| 815 | - '/': begin inc(cur.pos); tok := sSlash end; | |
| 816 | - '=': | |
| 817 | - if cur.buf[cur.pos + 1] = '=' then Slip(2, sEql, 'comparing is written =, not ==') | |
| 818 | - else begin inc(cur.pos); tok := sEql end; | |
| 819 | - '&': | |
| 820 | - if cur.buf[cur.pos + 1] = '&' then Slip(2, sAnd, '"and" is written and, not &&') | |
| 821 | - else begin inc(cur.pos); tok := sAmp end; | |
| 822 | - '#': begin inc(cur.pos); tok := sNeq end; | |
| 823 | - '<': begin | |
| 824 | - inc(cur.pos); | |
| 825 | - if cur.buf[cur.pos] = '=' then begin inc(cur.pos); tok := sLeq end | |
| 826 | - else tok := sLss; | |
| 827 | - end; | |
| 828 | - '>': begin | |
| 829 | - inc(cur.pos); | |
| 830 | - if cur.buf[cur.pos] = '=' then begin inc(cur.pos); tok := sGeq end | |
| 831 | - else tok := sGtr; | |
| 832 | - end; | |
| 833 | - ':': begin | |
| 834 | - inc(cur.pos); | |
| 835 | - if cur.buf[cur.pos] = '=' then begin inc(cur.pos); tok := sBecomes end | |
| 836 | - else tok := sColon; | |
| 837 | - end; | |
| 838 | - ';': begin inc(cur.pos); tok := sSemicolon end; | |
| 839 | - ',': begin inc(cur.pos); tok := sComma end; | |
| 840 | - '.': begin | |
| 841 | - inc(cur.pos); | |
| 842 | - if cur.buf[cur.pos] = '.' then begin | |
| 843 | - inc(cur.pos); | |
| 844 | - if cur.buf[cur.pos] = '.' then begin inc(cur.pos); tok := sEllipsis end | |
| 845 | - else tok := sUpto; | |
| 846 | - end else tok := sPeriod; | |
| 847 | - end; | |
| 848 | - '(': begin inc(cur.pos); tok := sLparen end; | |
| 849 | - ')': begin inc(cur.pos); tok := sRparen end; | |
| 850 | - '[': begin inc(cur.pos); tok := sLbrak end; | |
| 851 | - ']': begin inc(cur.pos); tok := sRbrak end; | |
| 852 | - '{': begin inc(cur.pos); tok := sLbrace end; | |
| 853 | - '}': begin inc(cur.pos); tok := sRbrace end; | |
| 854 | - '^': begin inc(cur.pos); tok := sArrow end; | |
| 855 | - '|': | |
| 856 | - if cur.buf[cur.pos + 1] = '|' then Slip(2, sOr, '"or" is written or, not ||') | |
| 857 | - else begin inc(cur.pos); tok := sBar end; | |
| 858 | - '~': begin inc(cur.pos); tok := sTilde end; | |
| 859 | - '!': | |
| 860 | - if cur.buf[cur.pos + 1] = '=' then Slip(2, sNeq, '"not equal" is written #, not !=') | |
| 861 | - else Slip(1, sNot, '"not" is written not, not !'); | |
| 862 | - #0: begin | |
| 863 | - if cur.pos < cur.len then MarkBytes(cur.pos, cur.pos + 1, 'NUL byte in the text'); | |
| 864 | - tok := sEof; | |
| 865 | - end; | |
| 866 | - else | |
| 867 | - if byte(c) >= $80 then | |
| 868 | - LexError('only ASCII outside strings, characters and comments'); | |
| 869 | - MarkBytes(cur.pos, cur.pos + 1, 'unexpected character "' + c + '"'); | |
| 870 | - end; | |
| 871 | -end; | |
| 872 | - | |
| 873 | -var i: longint; | |
| 874 | - | |
| 875 | -initialization | |
| 876 | - poolCap := 1 shl 16; | |
| 877 | - pool := GetMem(poolCap); | |
| 878 | - nameCap := 1 shl 12; | |
| 879 | - nameOff := GetMem(nameCap * 4); | |
| 880 | - nameLen := GetMem(nameCap * 4); | |
| 881 | - nameHash := GetMem(nameCap * 4); | |
| 882 | - tableMask := 2 * nameCap - 1; | |
| 883 | - table := GetMem((tableMask + 1) * 4); | |
| 884 | - FillChar(table^, (tableMask + 1) * 4, 0); | |
| 885 | - for i := 0 to NumWords - 1 do Intern(@Words[i][1], length(Words[i])); | |
| 886 | -end. |
| @@ -1,646 +0,0 @@ | ||
| 1 | -{ sym.pas: types, objects and scopes. | |
| 2 | - | |
| 3 | - Every name the program declares becomes an object (TObj): a constant, | |
| 4 | - type, variable, procedure, module, record field, variant case or | |
| 5 | - built-in. Every type is a TType. 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. | |
| 11 | - Leaves out: code. Where a variable lives is decided here (frame | |
| 12 | - offsets, chunk numbers), but nothing is emitted. } | |
| 13 | - | |
| 14 | -unit sym; | |
| 15 | - | |
| 16 | -{$mode objfpc}{$H-} | |
| 17 | - | |
| 18 | -interface | |
| 19 | - | |
| 20 | -uses scan, diag; | |
| 21 | - | |
| 22 | -type | |
| 23 | - TForm = (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 | - TClass = (oConst, oType, oVar, oProc, oModule, oField, oCase, oBuiltin); | |
| 30 | - | |
| 31 | - PType = ^TType; | |
| 32 | - PObj = ^TObj; | |
| 33 | - PModule = ^TModule; | |
| 34 | - PImport = ^TImport; | |
| 35 | - PLib = ^TLib; | |
| 36 | - | |
| 37 | - TType = record | |
| 38 | - form: TForm; | |
| 39 | - size, align: int64; | |
| 40 | - signed: boolean; { fInt } | |
| 41 | - vol: boolean; { fPtr: ^VOLATILE } | |
| 42 | - packd: boolean; { fRecord } | |
| 43 | - variadic: boolean; { fProc: ends in ... } | |
| 44 | - tuple: boolean; { fRecord: the results of a procedure that has several } | |
| 45 | - base: PType; { fPtr, fArray, fSlice: what it holds; fProc: result } | |
| 46 | - len: int64; { fArray: elements; fProc: parameters; fVariant: cases; a tuple: results } | |
| 47 | - fields: PObj; { fRecord: fields; fVariant: cases; fProc: parameters } | |
| 48 | - scope: longint; { fRecord: the scope of its fields } | |
| 49 | - fwd: longint; { fPtr whose base isn't declared yet: its name, else -1 } | |
| 50 | - obj: PObj; { the declaration that names it, or nil } | |
| 51 | - module: PModule; { the module that built it } | |
| 52 | - end; | |
| 53 | - | |
| 54 | - TObj = record | |
| 55 | - cls: TClass; | |
| 56 | - name: longint; | |
| 57 | - exported: boolean; | |
| 58 | - typ: PType; | |
| 59 | - next: PObj; { the next in its list: scope, fields, parameters } | |
| 60 | - scope: longint; { where it is declared } | |
| 61 | - pos: TPos; { where its name is declared, for messages } | |
| 62 | - val: int64; { oConst: value; oBuiltin: which; oCase: tag; | |
| 63 | - oField: offset; oVar: frame offset } | |
| 64 | - chunk: longint; { global oVar, oProc, string oConst: its chunk } | |
| 65 | - lev: integer; { oVar: 0 for a module variable, 1 for a local } | |
| 66 | - ind: boolean; { oVar: the frame slot holds the variable's address } | |
| 67 | - off: int64; { oVar with ind: added to that address } | |
| 68 | - isVar: boolean; { a var parameter } | |
| 69 | - param: boolean; { oVar: a parameter } | |
| 70 | - used: boolean; { named somewhere but in its own body } | |
| 71 | - read: boolean; { oVar: named other than as what := assigns } | |
| 72 | - fwd: boolean; { oProc: declared forward, no body yet } | |
| 73 | - isCode: boolean; { oProc: a code procedure } | |
| 74 | - openEnd: boolean; { oProc: has results, and may reach its end } | |
| 75 | - reg: longint; { oVar: the register it lives in, or 0 if in memory; | |
| 76 | - for a slice, the one its pointer is in } | |
| 77 | - reg2: longint; { oVar: a slice in registers: the one its length is in } | |
| 78 | - ext: boolean; { oProc, oVar: C's, reached through its chunk, which | |
| 79 | - holds its address } | |
| 80 | - fields: PObj; { oCase: its fields } | |
| 81 | - module: PModule; { oModule: the module; others: where declared } | |
| 82 | - link: PObj; { the next declaration of its module, top-level or | |
| 83 | - local } | |
| 84 | - end; | |
| 85 | - | |
| 86 | - { The arguments of a generic module's instance: a type, or a | |
| 87 | - procedure (o) and its type. } | |
| 88 | - TArgs = record | |
| 89 | - n: longint; | |
| 90 | - t: array[0..7] of PType; | |
| 91 | - o: array[0..7] of PObj; | |
| 92 | - pos: array[0..7] of TPos; { where each argument is, for messages } | |
| 93 | - at: TPos; { where the import is } | |
| 94 | - end; | |
| 95 | - | |
| 96 | - TImport = record | |
| 97 | - module: PModule; | |
| 98 | - next: PImport; | |
| 99 | - end; | |
| 100 | - | |
| 101 | - { A library a module links, by its name, as interned } | |
| 102 | - TLib = record | |
| 103 | - name: longint; | |
| 104 | - next: PLib; | |
| 105 | - end; | |
| 106 | - | |
| 107 | - TModule = record | |
| 108 | - name: longint; | |
| 109 | - sname: longint; { the name of its symbols: List(I64) for an instance } | |
| 110 | - args: TArgs; | |
| 111 | - scope: longint; | |
| 112 | - path: shortstring; | |
| 113 | - first, last: PObj; { its top-level declarations, in order } | |
| 114 | - locals, lastLocal: PObj; { its procedures' declarations, in order } | |
| 115 | - imports: PImport; | |
| 116 | - body: longint; { the chunk of its body, or -1 } | |
| 117 | - done: boolean; { compiled to the end } | |
| 118 | - checkOnly: boolean; { read only to check another file's interface } | |
| 119 | - used: boolean; { imported, through others, by the main module } | |
| 120 | - usesC: boolean; { has an extern, or links a library } | |
| 121 | - linkStatic: boolean; { says link static } | |
| 122 | - libs: PLib; { the libraries it links } | |
| 123 | - next: PModule; { modules in the order they were finished } | |
| 124 | - end; | |
| 125 | - | |
| 126 | -const | |
| 127 | - { built-in procedures } | |
| 128 | - bSize = 0; bAdr = 1; bLen = 2; bNew = 3; bMsg = 4; bSyscall = 5; | |
| 129 | - bFence = 6; bCas = 7; bXchg = 8; bXadd = 9; bArgc = 10; bArgv = 11; | |
| 130 | - bInc = 12; bDec = 13; bCopy = 14; | |
| 131 | - | |
| 132 | - MaxDepth = 64; { scopes open at once } | |
| 133 | - | |
| 134 | -var | |
| 135 | - tI8, tI16, tI32, tI64, tU8, tU16, tU32, tU64: PType; | |
| 136 | - tInt, tUInt: PType; { INT and UINT, as big as an address: 8 bytes here } | |
| 137 | - tBool, tF32, tReal, tRConst, tError, tConst, tNil, tStr, tNone, tPtrU8, tBytes: PType; | |
| 138 | - tPoison: PType; { of a name reported as not declared } | |
| 139 | - curModule: PModule; | |
| 140 | - | |
| 141 | -function NewType(form: TForm; size, align: int64): PType; | |
| 142 | -function NewObj(cls: TClass; name: longint; typ: PType): PObj; | |
| 143 | -function NewScope: longint; | |
| 144 | -procedure OpenScope(s: longint); | |
| 145 | -procedure CloseScope; | |
| 146 | -function ScopeDepth: longint; | |
| 147 | -procedure CutScopes(d: longint); | |
| 148 | -function TopScope: longint; | |
| 149 | -procedure SaveScopes(var d: longint; var saved: array of longint); | |
| 150 | -procedure RestoreScopes(d: longint; const saved: array of longint); | |
| 151 | -procedure Insert(obj: PObj; s: longint); | |
| 152 | -function Find(s, name: longint): PObj; | |
| 153 | -function Lookup(name: longint): PObj; | |
| 154 | -function Closest(name, s: longint): PObj; | |
| 155 | - | |
| 156 | -function AlignUp(x, a: int64): int64; | |
| 157 | -function Same(a, b: PType): boolean; | |
| 158 | -function IsInt(t: PType): boolean; | |
| 159 | -function IsScalar(t: PType): boolean; | |
| 160 | -function IsAggregate(t: PType): boolean; | |
| 161 | -function InTwoRegs(t: PType): boolean; | |
| 162 | -function BehindPointer(t: PType): boolean; | |
| 163 | -function Fits(v: int64; t: PType): boolean; | |
| 164 | -function TypeName(t: PType): shortstring; | |
| 165 | -function Fingerprint(o: PObj): qword; | |
| 166 | - | |
| 167 | -implementation | |
| 168 | - | |
| 169 | -uses math; | |
| 170 | - | |
| 171 | -type | |
| 172 | - PObjTab = ^TObjTab; | |
| 173 | - TObjTab = array[0..MaxInt div 16] of PObj; | |
| 174 | - | |
| 175 | -var | |
| 176 | - scopes: array[0..MaxDepth - 1] of longint; | |
| 177 | - depth: longint; | |
| 178 | - nextScope: longint; | |
| 179 | - hashTab: PObjTab; | |
| 180 | - hashMask, hashCount: longint; | |
| 181 | - | |
| 182 | -function NewType(form: TForm; size, align: int64): PType; | |
| 183 | -var t: PType; | |
| 184 | -begin | |
| 185 | - New(t); | |
| 186 | - FillChar(t^, SizeOf(TType), 0); | |
| 187 | - t^.form := form; | |
| 188 | - t^.size := size; | |
| 189 | - t^.align := align; | |
| 190 | - t^.fwd := -1; | |
| 191 | - t^.module := curModule; | |
| 192 | - NewType := t; | |
| 193 | -end; | |
| 194 | - | |
| 195 | -function NewObj(cls: TClass; name: longint; typ: PType): PObj; | |
| 196 | -var o: PObj; | |
| 197 | -begin | |
| 198 | - New(o); | |
| 199 | - FillChar(o^, SizeOf(TObj), 0); | |
| 200 | - o^.cls := cls; | |
| 201 | - o^.name := name; | |
| 202 | - o^.typ := typ; | |
| 203 | - o^.chunk := -1; | |
| 204 | - o^.module := curModule; | |
| 205 | - o^.pos := TokPos; | |
| 206 | - NewObj := o; | |
| 207 | -end; | |
| 208 | - | |
| 209 | -{ Scopes } | |
| 210 | - | |
| 211 | -function NewScope: longint; | |
| 212 | -begin | |
| 213 | - inc(nextScope); | |
| 214 | - NewScope := nextScope; | |
| 215 | -end; | |
| 216 | - | |
| 217 | -procedure OpenScope(s: longint); | |
| 218 | -begin | |
| 219 | - if depth = MaxDepth then Mark('scopes nested more than ' + IntStr(MaxDepth) + ' deep'); | |
| 220 | - scopes[depth] := s; | |
| 221 | - inc(depth); | |
| 222 | -end; | |
| 223 | - | |
| 224 | -procedure CloseScope; | |
| 225 | -begin | |
| 226 | - dec(depth); | |
| 227 | -end; | |
| 228 | - | |
| 229 | -{ An imported module is compiled in the middle of another, and must | |
| 230 | - see only the universe and itself. } | |
| 231 | -procedure SaveScopes(var d: longint; var saved: array of longint); | |
| 232 | -var i: longint; | |
| 233 | -begin | |
| 234 | - d := depth; | |
| 235 | - for i := 0 to depth - 1 do saved[i] := scopes[i]; | |
| 236 | - depth := 1; | |
| 237 | -end; | |
| 238 | - | |
| 239 | -procedure RestoreScopes(d: longint; const saved: array of longint); | |
| 240 | -var i: longint; | |
| 241 | -begin | |
| 242 | - depth := d; | |
| 243 | - for i := 0 to depth - 1 do scopes[i] := saved[i]; | |
| 244 | -end; | |
| 245 | - | |
| 246 | -function ScopeDepth: longint; | |
| 247 | -begin | |
| 248 | - ScopeDepth := depth; | |
| 249 | -end; | |
| 250 | - | |
| 251 | -{ Closes the scopes opened since there were d, after an error } | |
| 252 | -procedure CutScopes(d: longint); | |
| 253 | -begin | |
| 254 | - depth := d; | |
| 255 | -end; | |
| 256 | - | |
| 257 | -function TopScope: longint; | |
| 258 | -begin | |
| 259 | - TopScope := 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 | -function Slot(s, name: longint): longint; | |
| 267 | -begin | |
| 268 | - Slot := longint((longword(s) * 2654435761 xor longword(name) * 40503) and longword(hashMask)); | |
| 269 | -end; | |
| 270 | - | |
| 271 | -{ Doubles the table, which Insert keeps at most half full so that the | |
| 272 | - linear probing stays short, and puts every entry in its new slot. } | |
| 273 | -procedure Rehash; | |
| 274 | -var old: PObjTab; n, i, j: longint; | |
| 275 | -begin | |
| 276 | - old := hashTab; | |
| 277 | - n := hashMask + 1; | |
| 278 | - hashMask := n * 2 - 1; | |
| 279 | - hashTab := GetMem(n * 2 * SizeOf(PObj)); | |
| 280 | - FillChar(hashTab^, n * 2 * SizeOf(PObj), 0); | |
| 281 | - for i := 0 to n - 1 do | |
| 282 | - if old^[i] <> nil then begin | |
| 283 | - j := Slot(old^[i]^.scope, old^[i]^.name); | |
| 284 | - while hashTab^[j] <> nil do j := (j + 1) and hashMask; | |
| 285 | - hashTab^[j] := old^[i]; | |
| 286 | - end; | |
| 287 | - FreeMem(old); | |
| 288 | -end; | |
| 289 | - | |
| 290 | -procedure Insert(obj: PObj; s: longint); | |
| 291 | -var i: longint; o: PObj; | |
| 292 | -begin | |
| 293 | - i := Slot(s, obj^.name); | |
| 294 | - o := hashTab^[i]; | |
| 295 | - while o <> nil do begin | |
| 296 | - if (o^.scope = s) and (o^.name = obj^.name) then begin | |
| 297 | - Error(obj^.pos, NameOf(obj^.name) + ' is declared twice'); | |
| 298 | - Note(o^.pos, 'the first one is here'); | |
| 299 | - Fail; | |
| 300 | - end; | |
| 301 | - i := (i + 1) and hashMask; | |
| 302 | - o := hashTab^[i]; | |
| 303 | - end; | |
| 304 | - obj^.scope := s; | |
| 305 | - hashTab^[i] := obj; | |
| 306 | - inc(hashCount); | |
| 307 | - if hashCount * 2 > hashMask then Rehash; | |
| 308 | -end; | |
| 309 | - | |
| 310 | -function Find(s, name: longint): PObj; | |
| 311 | -var i: longint; o: PObj; | |
| 312 | -begin | |
| 313 | - i := Slot(s, name); | |
| 314 | - o := hashTab^[i]; | |
| 315 | - while o <> nil do begin | |
| 316 | - if (o^.scope = s) and (o^.name = name) then exit(o); | |
| 317 | - i := (i + 1) and hashMask; | |
| 318 | - o := hashTab^[i]; | |
| 319 | - end; | |
| 320 | - Find := nil; | |
| 321 | -end; | |
| 322 | - | |
| 323 | -function Lookup(name: longint): PObj; | |
| 324 | -var i: longint; o: PObj; | |
| 325 | -begin | |
| 326 | - for i := depth - 1 downto 0 do begin | |
| 327 | - o := Find(scopes[i], name); | |
| 328 | - if o <> nil then exit(o); | |
| 329 | - end; | |
| 330 | - Lookup := nil; | |
| 331 | -end; | |
| 332 | - | |
| 333 | -{ How far apart two names are: the edits, a character put in, taken out, | |
| 334 | - changed or swapped with the next, that make one the other, ignoring | |
| 335 | - case. } | |
| 336 | -function Distance(const a, b: shortstring): longint; | |
| 337 | -var d: array[0..MaxIdent + 1, 0..MaxIdent + 1] of longint; i, j, c: longint; | |
| 338 | -begin | |
| 339 | - for i := 0 to length(a) do d[i, 0] := i; | |
| 340 | - for j := 0 to length(b) do d[0, j] := j; | |
| 341 | - for i := 1 to length(a) do | |
| 342 | - for j := 1 to length(b) do begin | |
| 343 | - c := ord(UpCase(a[i]) <> UpCase(b[j])); | |
| 344 | - d[i, j] := Min(Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + c); | |
| 345 | - if (i > 1) and (j > 1) and (UpCase(a[i]) = UpCase(b[j - 1])) | |
| 346 | - and (UpCase(a[i - 1]) = UpCase(b[j])) then | |
| 347 | - d[i, j] := Min(d[i, j], d[i - 2, j - 2] + 1); | |
| 348 | - end; | |
| 349 | - Distance := d[length(a), length(b)]; | |
| 350 | -end; | |
| 351 | - | |
| 352 | -{ The name spelled most like name, for "did you mean": in scope s, if | |
| 353 | - it can be reached from here, or in the scopes in reach if s < 0; nil | |
| 354 | - if none is within an edit for every three characters. Only after an | |
| 355 | - error, so it looks at every object. } | |
| 356 | -function Closest(name, s: longint): PObj; | |
| 357 | -var i, j, d, best: longint; o: PObj; n: shortstring; reach: boolean; | |
| 358 | -begin | |
| 359 | - n := NameOf(name); | |
| 360 | - best := length(n) div 3 + 1; | |
| 361 | - Closest := nil; | |
| 362 | - for i := 0 to hashMask do begin | |
| 363 | - o := hashTab^[i]; | |
| 364 | - if (o = nil) or (o^.name = name) or (o^.name < 0) or (o^.typ = tPoison) then continue; | |
| 365 | - if s >= 0 then reach := (o^.scope = s) and (o^.exported or (o^.module = curModule)) | |
| 366 | - else begin | |
| 367 | - reach := false; | |
| 368 | - for j := 0 to depth - 1 do reach := reach or (o^.scope = scopes[j]); | |
| 369 | - end; | |
| 370 | - if reach then begin | |
| 371 | - d := Distance(n, NameOf(o^.name)); | |
| 372 | - if d < best then begin best := d; Closest := o end; | |
| 373 | - end; | |
| 374 | - end; | |
| 375 | -end; | |
| 376 | - | |
| 377 | -{ Types } | |
| 378 | - | |
| 379 | -function AlignUp(x, a: int64): int64; | |
| 380 | -begin | |
| 381 | - AlignUp := (x + a - 1) div a * a; | |
| 382 | -end; | |
| 383 | - | |
| 384 | -function IsInt(t: PType): boolean; | |
| 385 | -begin | |
| 386 | - IsInt := t^.form in [fInt, fConst]; | |
| 387 | -end; | |
| 388 | - | |
| 389 | -function IsScalar(t: PType): boolean; | |
| 390 | -begin | |
| 391 | - IsScalar := t^.form in [fBool, fInt, fReal, fConst, fNil, fPtr, fProc, | |
| 392 | - fEnum, fSet, fError]; | |
| 393 | -end; | |
| 394 | - | |
| 395 | -function IsAggregate(t: PType): boolean; | |
| 396 | -begin | |
| 397 | - IsAggregate := t^.form in [fArray, fSlice, fRecord, fVariant]; | |
| 398 | -end; | |
| 399 | - | |
| 400 | -{ Whether a procedure's result t is two words, which come back in rax | |
| 401 | - and rdx, as System V returns a struct of two integers: a slice (its | |
| 402 | - pointer, then its length), or two results that are each a single | |
| 403 | - value of 8 bytes at most and not a real. } | |
| 404 | -function InTwoRegs(t: PType): boolean; | |
| 405 | - | |
| 406 | - function OneWord(f: PObj): boolean; | |
| 407 | - begin | |
| 408 | - OneWord := IsScalar(f^.typ) and (f^.typ^.form <> fReal) and (f^.typ^.size <= 8); | |
| 409 | - end; | |
| 410 | - | |
| 411 | -begin | |
| 412 | - if t^.form = fSlice then exit(true); | |
| 413 | - InTwoRegs := t^.tuple and (t^.len = 2) and OneWord(t^.fields) and OneWord(t^.fields^.next); | |
| 414 | -end; | |
| 415 | - | |
| 416 | -{ Whether a procedure's result t comes back in memory the caller | |
| 417 | - provides, whose address the caller passes as a hidden first argument, | |
| 418 | - in rdi: any result that is neither one value, in rax or xmm0, nor two | |
| 419 | - words, in rax and rdx. } | |
| 420 | -function BehindPointer(t: PType): boolean; | |
| 421 | -begin | |
| 422 | - BehindPointer := IsAggregate(t) and not InTwoRegs(t); | |
| 423 | -end; | |
| 424 | - | |
| 425 | -function Fits(v: int64; t: PType): boolean; | |
| 426 | -begin | |
| 427 | - if t^.form <> fInt then exit(t^.form = fConst); | |
| 428 | - case t^.size of | |
| 429 | - 1: if t^.signed then Fits := (v >= -128) and (v <= 127) | |
| 430 | - else Fits := (v >= 0) and (v <= 255); | |
| 431 | - 2: if t^.signed then Fits := (v >= -32768) and (v <= 32767) | |
| 432 | - else Fits := (v >= 0) and (v <= 65535); | |
| 433 | - 4: if t^.signed then Fits := (v >= -2147483648) and (v <= 2147483647) | |
| 434 | - else Fits := (v >= 0) and (v <= 4294967295); | |
| 435 | - else Fits := t^.signed or (v >= 0); | |
| 436 | - end; | |
| 437 | -end; | |
| 438 | - | |
| 439 | -{ Structural sameness of two types. Named types are the same only if | |
| 440 | - they are one declaration; a type with no name is the same as another | |
| 441 | - built alike. } | |
| 442 | -function Alike(a, b: PType): boolean; | |
| 443 | -var p, q: PObj; | |
| 444 | -begin | |
| 445 | - if a = b then exit(true); | |
| 446 | - if a^.form <> b^.form then exit(false); | |
| 447 | - case a^.form of | |
| 448 | - fPtr: Alike := (a^.vol = b^.vol) and Same(a^.base, b^.base); | |
| 449 | - fArray: Alike := (a^.len = b^.len) and Same(a^.base, b^.base); | |
| 450 | - fSlice, fSet: Alike := Same(a^.base, b^.base); | |
| 451 | - fProc, fRecord: begin | |
| 452 | - { a record is the same only as itself, but for several results } | |
| 453 | - if (a^.form = fRecord) and not (a^.tuple and b^.tuple) then exit(false); | |
| 454 | - if (a^.len <> b^.len) or (a^.variadic <> b^.variadic) then exit(false); | |
| 455 | - if (a^.base = nil) <> (b^.base = nil) then exit(false); | |
| 456 | - if (a^.base <> nil) and not Same(a^.base, b^.base) then exit(false); | |
| 457 | - p := a^.fields; q := b^.fields; | |
| 458 | - while p <> nil do begin | |
| 459 | - if (p^.isVar <> q^.isVar) or not Same(p^.typ, q^.typ) then exit(false); | |
| 460 | - p := p^.next; q := q^.next; | |
| 461 | - end; | |
| 462 | - Alike := true; | |
| 463 | - end; | |
| 464 | - else Alike := false; | |
| 465 | - end; | |
| 466 | -end; | |
| 467 | - | |
| 468 | -function Same(a, b: PType): boolean; | |
| 469 | -begin | |
| 470 | - if a = b then exit(true); | |
| 471 | - if (a = nil) or (b = nil) then exit(false); | |
| 472 | - if (a^.obj <> nil) and (b^.obj <> nil) then exit(false); | |
| 473 | - Same := Alike(a, b); | |
| 474 | -end; | |
| 475 | - | |
| 476 | -function TypeName(t: PType): shortstring; | |
| 477 | -var s: shortstring; f: PObj; | |
| 478 | -begin | |
| 479 | - if t = nil then exit('nothing'); | |
| 480 | - if t^.obj <> nil then begin | |
| 481 | - if (t^.obj^.module <> nil) and (t^.obj^.module <> curModule) then | |
| 482 | - exit(NameOf(t^.obj^.module^.sname) + '.' + NameOf(t^.obj^.name)); | |
| 483 | - exit(NameOf(t^.obj^.name)); | |
| 484 | - end; | |
| 485 | - case t^.form of | |
| 486 | - fConst: TypeName := 'an integer constant'; | |
| 487 | - fReal: TypeName := 'a real constant'; | |
| 488 | - fNil: TypeName := 'nil'; | |
| 489 | - fStr: TypeName := 'a string'; | |
| 490 | - fPtr: if t^.vol then TypeName := '^VOLATILE ' + TypeName(t^.base) | |
| 491 | - else TypeName := '^' + TypeName(t^.base); | |
| 492 | - fArray: begin Str(t^.len, s); TypeName := '[' + s + ']' + TypeName(t^.base) end; | |
| 493 | - fSlice: TypeName := '[]' + TypeName(t^.base); | |
| 494 | - fRecord: if not t^.tuple then TypeName := 'record' | |
| 495 | - else begin | |
| 496 | - s := '('; | |
| 497 | - f := t^.fields; | |
| 498 | - while f <> nil do begin | |
| 499 | - s := s + TypeName(f^.typ); | |
| 500 | - f := f^.next; | |
| 501 | - if f <> nil then s := s + ', '; | |
| 502 | - end; | |
| 503 | - TypeName := s + ')'; | |
| 504 | - end; | |
| 505 | - fVariant: TypeName := 'variant'; | |
| 506 | - fProc: begin | |
| 507 | - s := 'proc('; | |
| 508 | - f := t^.fields; | |
| 509 | - while f <> nil do begin | |
| 510 | - if f^.isVar then s := s + 'var '; | |
| 511 | - s := s + TypeName(f^.typ); | |
| 512 | - f := f^.next; | |
| 513 | - if f <> nil then s := s + ', '; | |
| 514 | - end; | |
| 515 | - s := s + ')'; | |
| 516 | - if t^.base <> nil then s := s + ': ' + TypeName(t^.base); | |
| 517 | - TypeName := s; | |
| 518 | - end; | |
| 519 | - else TypeName := '?'; | |
| 520 | - end; | |
| 521 | -end; | |
| 522 | - | |
| 523 | -{ A fingerprint of what a declaration exports, for the interface | |
| 524 | - check: two files of one module export the same thing when their | |
| 525 | - fingerprints agree. A named type counts by its name, so that a type | |
| 526 | - declared in each file stands for the other's. } | |
| 527 | -procedure Mix(var h: qword; v: int64); | |
| 528 | -begin | |
| 529 | - h := (h xor qword(v)) * 1099511628211; { FNV-1a's 64-bit prime } | |
| 530 | -end; | |
| 531 | - | |
| 532 | -procedure Shape(var h: qword; t: PType; top: boolean); | |
| 533 | -var f, g: PObj; | |
| 534 | -begin | |
| 535 | - if t = nil then begin Mix(h, -1); exit end; | |
| 536 | - if (t^.obj <> nil) and not top then begin | |
| 537 | - Mix(h, t^.obj^.name); | |
| 538 | - if t^.obj^.module <> nil then Mix(h, t^.obj^.module^.sname); | |
| 539 | - exit; | |
| 540 | - end; | |
| 541 | - Mix(h, ord(t^.form)); | |
| 542 | - Mix(h, ord(t^.vol) + 2 * ord(t^.variadic)); | |
| 543 | - Mix(h, t^.len); | |
| 544 | - if t^.form in [fPtr, fArray, fSlice, fProc, fSet] then Shape(h, t^.base, false); | |
| 545 | - f := t^.fields; | |
| 546 | - while f <> nil do begin | |
| 547 | - if (t^.form <> fRecord) or f^.exported then begin | |
| 548 | - Mix(h, f^.name); | |
| 549 | - Mix(h, ord(f^.isVar)); | |
| 550 | - Shape(h, f^.typ, false); | |
| 551 | - g := f^.fields; | |
| 552 | - while g <> nil do begin Shape(h, g^.typ, false); g := g^.next end; | |
| 553 | - end; | |
| 554 | - f := f^.next; | |
| 555 | - end; | |
| 556 | -end; | |
| 557 | - | |
| 558 | -function Fingerprint(o: PObj): qword; | |
| 559 | -var h: qword; f: PObj; | |
| 560 | -begin | |
| 561 | - h := 14695981039346656037; { FNV-1a's 64-bit offset basis } | |
| 562 | - Mix(h, ord(o^.cls)); | |
| 563 | - if o^.cls = oCase then begin | |
| 564 | - Mix(h, o^.val); | |
| 565 | - f := o^.fields; | |
| 566 | - while f <> nil do begin Shape(h, f^.typ, false); f := f^.next end; | |
| 567 | - end else Shape(h, o^.typ, o^.cls = oType); | |
| 568 | - Fingerprint := h; | |
| 569 | -end; | |
| 570 | - | |
| 571 | -{ The universe } | |
| 572 | - | |
| 573 | -function Basic(const name: shortstring; form: TForm; size: int64; signed: boolean): PType; | |
| 574 | -var t: PType; o: PObj; | |
| 575 | -begin | |
| 576 | - t := NewType(form, size, size); | |
| 577 | - t^.signed := signed; | |
| 578 | - o := NewObj(oType, Intern(@name[1], length(name)), t); | |
| 579 | - o^.exported := true; | |
| 580 | - t^.obj := o; | |
| 581 | - Insert(o, 0); | |
| 582 | - Basic := t; | |
| 583 | -end; | |
| 584 | - | |
| 585 | -procedure Enter(const name: shortstring; cls: TClass; t: PType; v: int64); | |
| 586 | -var o: PObj; | |
| 587 | -begin | |
| 588 | - o := NewObj(cls, Intern(@name[1], length(name)), t); | |
| 589 | - o^.val := v; | |
| 590 | - o^.exported := true; | |
| 591 | - Insert(o, 0); | |
| 592 | -end; | |
| 593 | - | |
| 594 | -initialization | |
| 595 | - hashMask := (1 shl 14) - 1; | |
| 596 | - hashTab := GetMem((hashMask + 1) * SizeOf(PObj)); | |
| 597 | - FillChar(hashTab^, (hashMask + 1) * SizeOf(PObj), 0); | |
| 598 | - depth := 1; | |
| 599 | - scopes[0] := 0; | |
| 600 | - nextScope := 0; | |
| 601 | - curModule := nil; | |
| 602 | - tI8 := Basic('I8', fInt, 1, true); | |
| 603 | - tI16 := Basic('I16', fInt, 2, true); | |
| 604 | - tI32 := Basic('I32', fInt, 4, true); | |
| 605 | - tI64 := Basic('I64', fInt, 8, true); | |
| 606 | - tU8 := Basic('U8', fInt, 1, false); | |
| 607 | - tU16 := Basic('U16', fInt, 2, false); | |
| 608 | - tU32 := Basic('U32', fInt, 4, false); | |
| 609 | - tU64 := Basic('U64', fInt, 8, false); | |
| 610 | - tInt := Basic('INT', fInt, 8, true); | |
| 611 | - tUInt := Basic('UINT', fInt, 8, false); | |
| 612 | - tBool := Basic('BOOL', fBool, 1, false); | |
| 613 | - tF32 := Basic('F32', fReal, 4, false); | |
| 614 | - tReal := Basic('F64', fReal, 8, false); | |
| 615 | - tRConst := NewType(fReal, 8, 8); | |
| 616 | - tError := Basic('ERROR', fError, 8, false); | |
| 617 | - tConst := NewType(fConst, 8, 8); | |
| 618 | - tConst^.signed := true; | |
| 619 | - tNil := NewType(fNil, 8, 8); | |
| 620 | - tStr := NewType(fStr, 0, 1); | |
| 621 | - tNone := NewType(fNone, 0, 1); | |
| 622 | - tPtrU8 := NewType(fPtr, 8, 8); | |
| 623 | - tPtrU8^.base := tU8; | |
| 624 | - tBytes := NewType(fSlice, 16, 8); | |
| 625 | - tBytes^.base := tU8; | |
| 626 | - tPoison := NewType(fInt, 8, 8); | |
| 627 | - tPoison^.signed := true; | |
| 628 | - Enter('true', oConst, tBool, 1); | |
| 629 | - Enter('false', oConst, tBool, 0); | |
| 630 | - Enter('nil', oConst, tNil, 0); | |
| 631 | - Enter('SIZE', oBuiltin, nil, bSize); | |
| 632 | - Enter('ADR', oBuiltin, nil, bAdr); | |
| 633 | - Enter('LEN', oBuiltin, nil, bLen); | |
| 634 | - Enter('NEW', oBuiltin, nil, bNew); | |
| 635 | - Enter('MSG', oBuiltin, nil, bMsg); | |
| 636 | - Enter('SYSCALL', oBuiltin, nil, bSyscall); | |
| 637 | - Enter('FENCE', oBuiltin, nil, bFence); | |
| 638 | - Enter('CAS', oBuiltin, nil, bCas); | |
| 639 | - Enter('XCHG', oBuiltin, nil, bXchg); | |
| 640 | - Enter('XADD', oBuiltin, nil, bXadd); | |
| 641 | - Enter('ARGC', oBuiltin, nil, bArgc); | |
| 642 | - Enter('ARGV', oBuiltin, nil, bArgv); | |
| 643 | - Enter('INC', oBuiltin, nil, bInc); | |
| 644 | - Enter('DEC', oBuiltin, nil, bDec); | |
| 645 | - Enter('COPY', oBuiltin, nil, bCopy); | |
| 646 | -end. |
| @@ -1,1208 +0,0 @@ | ||
| 1 | -{ x64.pas: machine code for x86-64. | |
| 2 | - | |
| 3 | - The encoder writes the instructions the code generator needs, and | |
| 4 | - no others, into the text of chunk.pas. Registers are numbered as the | |
| 5 | - hardware numbers them. Jumps within a procedure are patched here, as | |
| 6 | - soon as their target is known. | |
| 7 | - | |
| 8 | - Depends on: scan, for errors; chunk, for the code buffer and fixups. | |
| 9 | - Leaves out: choosing instructions; that is gen.pas. } | |
| 10 | - | |
| 11 | -unit x64; | |
| 12 | - | |
| 13 | -{$mode objfpc}{$H-} | |
| 14 | - | |
| 15 | -interface | |
| 16 | - | |
| 17 | -uses scan, diag, chunk; | |
| 18 | - | |
| 19 | -const | |
| 20 | - RAX = 0; RCX = 1; RDX = 2; RBX = 3; RSP = 4; RBP = 5; RSI = 6; RDI = 7; | |
| 21 | - R8 = 8; R9 = 9; R10 = 10; R11 = 11; R12 = 12; R13 = 13; R14 = 14; R15 = 15; | |
| 22 | - RIP = -1; { a base meaning rip-relative, to a chunk } | |
| 23 | - NoIndex = -1; | |
| 24 | - | |
| 25 | - { condition codes } | |
| 26 | - ccB = 2; ccAE = 3; ccE = 4; ccNE = 5; ccBE = 6; ccA = 7; ccS = 8; | |
| 27 | - ccP = 10; ccNP = 11; ccL = 12; ccGE = 13; ccLE = 14; ccG = 15; | |
| 28 | - | |
| 29 | - { group 1 operations, the /digit of 81 and 83 } | |
| 30 | - aluAdd = 0; aluOr = 1; aluAnd = 4; aluSub = 5; aluXor = 6; aluCmp = 7; | |
| 31 | - | |
| 32 | - { the /digit of D3 and C1 } | |
| 33 | - shShl = 4; shShr = 5; shSar = 7; | |
| 34 | - | |
| 35 | - NoLabel = -1; | |
| 36 | - | |
| 37 | -type | |
| 38 | - TMem = record | |
| 39 | - base: longint; { a register, or RIP } | |
| 40 | - index: longint; { a register, or NoIndex } | |
| 41 | - scale: longint; { 1, 2, 4 or 8 } | |
| 42 | - disp: longint; | |
| 43 | - chunk: longint; { with RIP: the chunk addressed } | |
| 44 | - end; | |
| 45 | - | |
| 46 | -var | |
| 47 | - listing: boolean; { -S: each instruction's text is kept, for List } | |
| 48 | - | |
| 49 | -function Here: TMem; | |
| 50 | -function Mem(base: longint; disp: int64): TMem; | |
| 51 | -function MemRip(chunk: longint; disp: longint): TMem; | |
| 52 | -function MemIdx(base, index, scale: longint; disp: int64): TMem; | |
| 53 | - | |
| 54 | -procedure MovRR(dst, src: longint); | |
| 55 | -procedure MovRI(r: longint; v: int64); | |
| 56 | -procedure Load(r: longint; const m: TMem; size: longint; signed: boolean); | |
| 57 | -procedure Store(const m: TMem; r: longint; size: longint); | |
| 58 | -procedure Lea(r: longint; const m: TMem); | |
| 59 | -procedure Alu(op, dst, src: longint); | |
| 60 | -procedure AluI(op, r: longint; v: longint); | |
| 61 | -procedure AluM(op, r: longint; const m: TMem); | |
| 62 | -procedure AluMI(op: longint; m: TMem; v, size: longint); | |
| 63 | -procedure StoreI(m: TMem; v, size: longint); | |
| 64 | -procedure Imul(dst, src: longint); | |
| 65 | -procedure ImulI(r: longint; v: longint); | |
| 66 | -procedure Shift(op, r: longint); | |
| 67 | -procedure ShiftI(op, r, n: longint); | |
| 68 | -procedure Unary(ext, r: longint); { F7 /ext: 2 not, 3 neg, 6 div, 7 idiv } | |
| 69 | -procedure Cqo; | |
| 70 | -procedure Extend(r, size: longint; signed: boolean); | |
| 71 | -procedure SetCC(cc, r: longint); | |
| 72 | -procedure TestRR(a, b: longint); | |
| 73 | -procedure Push(r: longint); | |
| 74 | -procedure Pop(r: longint); | |
| 75 | -procedure CallChunk(c: longint); | |
| 76 | -procedure CallBack(target: longint); | |
| 77 | -procedure CallR(r: longint); | |
| 78 | -procedure JmpR(r: longint); | |
| 79 | -procedure CallM(const m: TMem); | |
| 80 | -procedure Ret; | |
| 81 | -procedure Leave; | |
| 82 | -procedure Syscall; | |
| 83 | -procedure Mfence; | |
| 84 | -procedure Ud2; | |
| 85 | -procedure Align(n: longint); | |
| 86 | -procedure RepMovsb; | |
| 87 | -procedure Std; | |
| 88 | -procedure Cld; | |
| 89 | -procedure RepStosb; | |
| 90 | -procedure RepeCmpsb; | |
| 91 | -procedure LockCmpxchg(const m: TMem; r: longint); | |
| 92 | -procedure Xchg(const m: TMem; r: longint); | |
| 93 | -procedure LockXadd(const m: TMem; r: longint); | |
| 94 | -procedure MovqXR(x, r: longint); | |
| 95 | -procedure MovqRX(r, x: longint); | |
| 96 | -procedure MovX(x, y: longint); | |
| 97 | -{ The real instructions take single: F32 (ss) rather than F64 (sd) } | |
| 98 | -procedure MovsXM(x: longint; const m: TMem; single: boolean); | |
| 99 | -procedure MovsMX(const m: TMem; x: longint; single: boolean); | |
| 100 | -procedure Sse(op, x, y: longint; single: boolean); { 58 add, 59 mul, 5C sub, 5E div, 5A convert } | |
| 101 | -procedure SseM(op, x: longint; const m: TMem; single: boolean); | |
| 102 | -procedure Ucomis(x, y: longint; single: boolean); | |
| 103 | -procedure Cvtsi2s(x, r: longint; single: boolean); | |
| 104 | -procedure Cvtts2si(r, x: longint; single: boolean); | |
| 105 | -procedure Btc(r, bit: longint); | |
| 106 | - | |
| 107 | -procedure Jmp(var L: longint); | |
| 108 | -procedure Jcc(cc: longint; var L: longint); | |
| 109 | -procedure JmpBack(target: longint); | |
| 110 | -procedure JccBack(cc, target: longint); | |
| 111 | -procedure FixLink(L: longint); | |
| 112 | -procedure DropJumpTo(var L: longint); | |
| 113 | -procedure FixLinkTo(L, target: longint); | |
| 114 | -function JoinLinks(L1, L2: longint): longint; | |
| 115 | - | |
| 116 | -type | |
| 117 | - { The parser's reader of an integer constant in a code procedure: a | |
| 118 | - whole expression, or, when whole is false, a single factor } | |
| 119 | - TConstFn = function(whole: boolean): int64; | |
| 120 | - | |
| 121 | -const | |
| 122 | - RegNames: array[0..3, 0..15] of string[5] = ( | |
| 123 | - ('rax', 'rcx', 'rdx', 'rbx', 'rsp', 'rbp', 'rsi', 'rdi', | |
| 124 | - 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15'), | |
| 125 | - ('eax', 'ecx', 'edx', 'ebx', 'esp', 'ebp', 'esi', 'edi', | |
| 126 | - 'r8d', 'r9d', 'r10d', 'r11d', 'r12d', 'r13d', 'r14d', 'r15d'), | |
| 127 | - ('ax', 'cx', 'dx', 'bx', 'sp', 'bp', 'si', 'di', | |
| 128 | - 'r8w', 'r9w', 'r10w', 'r11w', 'r12w', 'r13w', 'r14w', 'r15w'), | |
| 129 | - ('al', 'cl', 'dl', 'bl', 'spl', 'bpl', 'sil', 'dil', | |
| 130 | - 'r8b', 'r9b', 'r10b', 'r11b', 'r12b', 'r13b', 'r14b', 'r15b')); | |
| 131 | - CondNames: array[0..15] of string[3] = ('o', 'no', 'b', 'ae', 'e', 'ne', | |
| 132 | - 'be', 'a', 's', 'ns', 'p', 'np', 'l', 'ge', 'le', 'g'); | |
| 133 | - | |
| 134 | -procedure Assemble(constExpr: TConstFn); | |
| 135 | -procedure Say(const s: shortstring); | |
| 136 | -function SaidCount: longint; | |
| 137 | -procedure Unsay(n: longint); | |
| 138 | -procedure Restate(at: longint; const s: shortstring); | |
| 139 | -procedure List(module: longint); | |
| 140 | - | |
| 141 | -implementation | |
| 142 | - | |
| 143 | -{ Operands } | |
| 144 | - | |
| 145 | -function Mem(base: longint; disp: int64): TMem; | |
| 146 | -begin | |
| 147 | - if (disp < low(longint)) or (disp > high(longint)) then Mark('an offset beyond 32 bits'); | |
| 148 | - Mem.base := base; Mem.index := NoIndex; Mem.scale := 1; | |
| 149 | - Mem.disp := disp; Mem.chunk := -1; | |
| 150 | -end; | |
| 151 | - | |
| 152 | -function MemRip(chunk: longint; disp: longint): TMem; | |
| 153 | -begin | |
| 154 | - MemRip.base := RIP; MemRip.index := NoIndex; MemRip.scale := 1; | |
| 155 | - MemRip.disp := disp; MemRip.chunk := chunk; | |
| 156 | -end; | |
| 157 | - | |
| 158 | -function MemIdx(base, index, scale: longint; disp: int64): TMem; | |
| 159 | -begin | |
| 160 | - MemIdx := Mem(base, disp); | |
| 161 | - MemIdx.index := index; | |
| 162 | - MemIdx.scale := scale; | |
| 163 | -end; | |
| 164 | - | |
| 165 | -{ The address of the code about to be written, as an operand } | |
| 166 | -function Here: TMem; | |
| 167 | -begin | |
| 168 | - Here := MemRip(curChunk, pc - chunks^[curChunk].start); | |
| 169 | -end; | |
| 170 | - | |
| 171 | -{ Encoding. An instruction is: a legacy prefix, REX, the opcode, ModRM, | |
| 172 | - SIB and displacement. Opcodes of more than one byte are given with | |
| 173 | - their first byte in the low eight bits. } | |
| 174 | - | |
| 175 | -procedure Opcode(op: longint); | |
| 176 | -begin | |
| 177 | - repeat | |
| 178 | - Put1(op and $FF); | |
| 179 | - op := op shr 8; | |
| 180 | - until op = 0; | |
| 181 | -end; | |
| 182 | - | |
| 183 | -{ The legacy prefix pfx, if any, and REX: wide is REX.W; byte4 says | |
| 184 | - a byte register of 4 to 7 is used, spl to dil, which exist only with | |
| 185 | - a REX prefix. } | |
| 186 | -procedure Prefix(pfx: longint; w: boolean; reg, index, base: longint; byte4: boolean); | |
| 187 | -var rex: longint; | |
| 188 | -begin | |
| 189 | - if pfx <> 0 then Put1(pfx); | |
| 190 | - rex := 0; | |
| 191 | - if w then rex := rex or 8; | |
| 192 | - if reg >= 8 then rex := rex or 4; | |
| 193 | - if index >= 8 then rex := rex or 2; | |
| 194 | - if base >= 8 then rex := rex or 1; | |
| 195 | - if (rex <> 0) or byte4 then Put1($40 or rex); | |
| 196 | -end; | |
| 197 | - | |
| 198 | -{ op reg, [mem] } | |
| 199 | -procedure OpM(pfx: longint; w: boolean; op, reg: longint; const m: TMem; byteReg: boolean = false); | |
| 200 | -var md, b, sc: longint; | |
| 201 | -begin | |
| 202 | - byteReg := byteReg and (reg >= 4) and (reg <= 7); | |
| 203 | - if m.base = RIP then Prefix(pfx, w, reg, NoIndex, 0, byteReg) | |
| 204 | - else Prefix(pfx, w, reg, m.index, m.base, byteReg); | |
| 205 | - Opcode(op); | |
| 206 | - reg := reg and 7; | |
| 207 | - if m.base = RIP then begin | |
| 208 | - Put1(reg shl 3 or 5); | |
| 209 | - { mod 00 with r/m 101 is rip + disp32. The displacement counts | |
| 210 | - from the end of the instruction, and the fixup takes it to end | |
| 211 | - with these four bytes: an instruction with an immediate after | |
| 212 | - them, AluMI's or StoreI's, takes the immediate's size off disp | |
| 213 | - first (the assembler can't name a chunk). } | |
| 214 | - AddFixup(pc, m.chunk, m.disp); | |
| 215 | - Put4(0); | |
| 216 | - exit; | |
| 217 | - end; | |
| 218 | - b := m.base and 7; | |
| 219 | - { mod 00 takes no displacement, but with r/m 101 (RBP or R13) it means | |
| 220 | - rip-relative, or disp32 under a SIB byte, so those bases take a | |
| 221 | - zero disp8 } | |
| 222 | - if (m.disp = 0) and (b <> RBP) then md := 0 | |
| 223 | - else if (m.disp >= -128) and (m.disp <= 127) then md := 1 | |
| 224 | - else md := 2; | |
| 225 | - { r/m 100 (RSP or R12) means a SIB byte follows, so those bases need | |
| 226 | - one, with index 100 meaning none } | |
| 227 | - if (m.index <> NoIndex) or (b = RSP) then begin | |
| 228 | - Put1(md shl 6 or reg shl 3 or 4); | |
| 229 | - case m.scale of 1: sc := 0; 2: sc := 1; 4: sc := 2; else sc := 3; end; | |
| 230 | - if m.index = NoIndex then Put1(sc shl 6 or 4 shl 3 or b) | |
| 231 | - else Put1(sc shl 6 or (m.index and 7) shl 3 or b); | |
| 232 | - end else | |
| 233 | - Put1(md shl 6 or reg shl 3 or b); | |
| 234 | - if md = 1 then Put1(m.disp) | |
| 235 | - else if md = 2 then Put4(m.disp); | |
| 236 | -end; | |
| 237 | - | |
| 238 | -{ op reg, rm (both registers) } | |
| 239 | -procedure OpR(pfx: longint; w: boolean; op, reg, rm: longint; byteReg: boolean = false); | |
| 240 | -begin | |
| 241 | - Prefix(pfx, w, reg, NoIndex, rm, byteReg and ((reg in [4..7]) or (rm in [4..7]))); | |
| 242 | - Opcode(op); | |
| 243 | - Put1($C0 or (reg and 7) shl 3 or (rm and 7)); | |
| 244 | -end; | |
| 245 | - | |
| 246 | -{ The listing: the text of each instruction, as it is made } | |
| 247 | - | |
| 248 | -type | |
| 249 | - TSaid = record | |
| 250 | - at: longint; { where the instruction starts in code } | |
| 251 | - text: string[63]; | |
| 252 | - end; | |
| 253 | - | |
| 254 | -var | |
| 255 | - said: ^TSaid; | |
| 256 | - numSaid, saidCap: longint; | |
| 257 | - | |
| 258 | -{ How many instructions the listing has, and back to n of them, for | |
| 259 | - code made again } | |
| 260 | -function SaidCount: longint; | |
| 261 | -begin | |
| 262 | - SaidCount := numSaid; | |
| 263 | -end; | |
| 264 | - | |
| 265 | -procedure Unsay(n: longint); | |
| 266 | -begin | |
| 267 | - numSaid := n; | |
| 268 | -end; | |
| 269 | - | |
| 270 | -procedure SayAt(at: longint; const s: shortstring); | |
| 271 | -begin | |
| 272 | - if not listing then exit; | |
| 273 | - if numSaid = saidCap then begin | |
| 274 | - saidCap := saidCap * 2 + 1024; | |
| 275 | - said := ReAllocMem(said, saidCap * SizeOf(TSaid)); | |
| 276 | - end; | |
| 277 | - said[numSaid].at := at; | |
| 278 | - said[numSaid].text := s; | |
| 279 | - inc(numSaid); | |
| 280 | -end; | |
| 281 | - | |
| 282 | -{ The text of the instruction about to be written } | |
| 283 | -procedure Say(const s: shortstring); | |
| 284 | -begin | |
| 285 | - SayAt(pc, s); | |
| 286 | -end; | |
| 287 | - | |
| 288 | -{ The text of the instruction at at, said again, as when its operand is | |
| 289 | - patched } | |
| 290 | -procedure Restate(at: longint; const s: shortstring); | |
| 291 | -var i: longint; | |
| 292 | -begin | |
| 293 | - i := numSaid - 1; | |
| 294 | - while (i >= 0) and (said[i].at <> at) do dec(i); | |
| 295 | - if i >= 0 then said[i].text := s; | |
| 296 | -end; | |
| 297 | - | |
| 298 | -function RN(r: longint; size: longint = 8): shortstring; | |
| 299 | -begin | |
| 300 | - case size of | |
| 301 | - 1: RN := RegNames[3, r]; | |
| 302 | - 2: RN := RegNames[2, r]; | |
| 303 | - 4: RN := RegNames[1, r]; | |
| 304 | - else RN := RegNames[0, r]; | |
| 305 | - end; | |
| 306 | -end; | |
| 307 | - | |
| 308 | -function XN(x: longint): shortstring; | |
| 309 | -begin | |
| 310 | - XN := 'xmm' + IntStr(x); | |
| 311 | -end; | |
| 312 | - | |
| 313 | -{ A memory operand, with the size of what it holds, as the assembler | |
| 314 | - reads it } | |
| 315 | -function MN(const m: TMem; size: longint = 0): shortstring; | |
| 316 | -var s: shortstring; | |
| 317 | -begin | |
| 318 | - case size of | |
| 319 | - 1: s := 'byte '; | |
| 320 | - 2: s := 'word '; | |
| 321 | - 4: s := 'dword '; | |
| 322 | - 8: s := 'qword '; | |
| 323 | - else s := ''; | |
| 324 | - end; | |
| 325 | - if m.base = RIP then s := s + '[' + ChunkName(m.chunk) | |
| 326 | - else s := s + '[' + RN(m.base); | |
| 327 | - if m.index <> NoIndex then s := s + ' + ' + RN(m.index) + ' * ' + IntStr(m.scale); | |
| 328 | - if m.disp > 0 then s := s + ' + ' + IntStr(m.disp) | |
| 329 | - else if m.disp < 0 then s := s + ' - ' + IntStr(-m.disp); | |
| 330 | - MN := s + ']'; | |
| 331 | -end; | |
| 332 | - | |
| 333 | -const | |
| 334 | - AluNames: array[0..7] of string[3] = ('add', 'or', 'adc', 'sbb', 'and', 'sub', 'xor', 'cmp'); | |
| 335 | - ShiftNames: array[0..7] of string[3] = ('rol', 'ror', 'rcl', 'rcr', 'shl', 'shr', 'sal', 'sar'); | |
| 336 | - UnaryNames: array[2..7] of string[4] = ('not', 'neg', 'mul', 'imul', 'div', 'idiv'); | |
| 337 | - | |
| 338 | -procedure MovRR(dst, src: longint); | |
| 339 | -begin | |
| 340 | - if listing then Say('mov ' + RN(dst) + ', ' + RN(src)); | |
| 341 | - OpR(0, true, $89, src, dst); | |
| 342 | -end; | |
| 343 | - | |
| 344 | -{ The immediate of n bytes that ends an instruction } | |
| 345 | -procedure PutImm(v, n: longint); | |
| 346 | -begin | |
| 347 | - if n = 4 then Put4(v) | |
| 348 | - else begin | |
| 349 | - Put1(v and $FF); | |
| 350 | - if n = 2 then Put1((v shr 8) and $FF); | |
| 351 | - end; | |
| 352 | -end; | |
| 353 | - | |
| 354 | -{ op [m], v, on size bytes; v fits in them, or in 32 bits for 8 } | |
| 355 | -procedure AluMI(op: longint; m: TMem; v, size: longint); | |
| 356 | -var n: longint; | |
| 357 | -begin | |
| 358 | - if listing then Say(AluNames[op] + ' ' + MN(m, size) + ', ' + IntStr(v)); | |
| 359 | - if (size = 1) or (v >= -128) and (v <= 127) then n := 1 | |
| 360 | - else if size = 2 then n := 2 | |
| 361 | - else n := 4; | |
| 362 | - if m.base = RIP then dec(m.disp, n); | |
| 363 | - if size = 1 then OpM(0, false, $80, op, m) | |
| 364 | - else if n = 1 then OpM(ord(size = 2) * $66, size = 8, $83, op, m) | |
| 365 | - else OpM(ord(size = 2) * $66, size = 8, $81, op, m); | |
| 366 | - PutImm(v, n); | |
| 367 | -end; | |
| 368 | - | |
| 369 | -{ mov [m], v, of size bytes; v fits in them, or in 32 bits for 8 } | |
| 370 | -procedure StoreI(m: TMem; v, size: longint); | |
| 371 | -var n: longint; | |
| 372 | -begin | |
| 373 | - if listing then Say('mov ' + MN(m, size) + ', ' + IntStr(v)); | |
| 374 | - n := size; | |
| 375 | - if n > 4 then n := 4; | |
| 376 | - if m.base = RIP then dec(m.disp, n); | |
| 377 | - if size = 1 then OpM(0, false, $C6, 0, m) | |
| 378 | - else OpM(ord(size = 2) * $66, size = 8, $C7, 0, m); | |
| 379 | - PutImm(v, n); | |
| 380 | -end; | |
| 381 | - | |
| 382 | -procedure MovRI(r: longint; v: int64); | |
| 383 | -begin | |
| 384 | - if not listing then | |
| 385 | - else if v = 0 then Say('xor ' + RN(r, 4) + ', ' + RN(r, 4)) | |
| 386 | - else Say('mov ' + RN(r) + ', ' + IntStr(v)); | |
| 387 | - if (v >= low(longint)) and (v <= high(longint)) then begin | |
| 388 | - if v = 0 then OpR(0, false, $31, r, r) { xor r32, r32 } | |
| 389 | - else if v > 0 then begin { mov r32, imm32 } | |
| 390 | - Prefix(0, false, 0, NoIndex, r, false); Put1($B8 + r and 7); Put4(v) | |
| 391 | - end else begin | |
| 392 | - OpR(0, true, $C7, 0, r); Put4(v) { sign-extended imm32 } | |
| 393 | - end; | |
| 394 | - end else if (v > 0) and (v <= $FFFFFFFF) then begin | |
| 395 | - Prefix(0, false, 0, NoIndex, r, false); Put1($B8 + r and 7); Put4(longint(v)) | |
| 396 | - end else begin | |
| 397 | - Prefix(0, true, 0, NoIndex, r, false); Put1($B8 + r and 7); Put8(v) | |
| 398 | - end; | |
| 399 | -end; | |
| 400 | - | |
| 401 | -procedure Load(r: longint; const m: TMem; size: longint; signed: boolean); | |
| 402 | -begin | |
| 403 | - if not listing then | |
| 404 | - else if size = 8 then Say('mov ' + RN(r) + ', ' + MN(m)) | |
| 405 | - else if (size = 4) and not signed then Say('mov ' + RN(r, 4) + ', ' + MN(m)) | |
| 406 | - else if size = 4 then Say('movsxd ' + RN(r) + ', ' + MN(m, 4)) | |
| 407 | - else if signed then Say('movsx ' + RN(r) + ', ' + MN(m, size)) | |
| 408 | - else Say('movzx ' + RN(r, 4) + ', ' + MN(m, size)); | |
| 409 | - case size of | |
| 410 | - 1: if signed then OpM(0, true, $BE0F, r, m) else OpM(0, false, $B60F, r, m); | |
| 411 | - 2: if signed then OpM(0, true, $BF0F, r, m) else OpM(0, false, $B70F, r, m); | |
| 412 | - 4: if signed then OpM(0, true, $63, r, m) else OpM(0, false, $8B, r, m); | |
| 413 | - else OpM(0, true, $8B, r, m); | |
| 414 | - end; | |
| 415 | -end; | |
| 416 | - | |
| 417 | -procedure Store(const m: TMem; r: longint; size: longint); | |
| 418 | -begin | |
| 419 | - if listing then Say('mov ' + MN(m) + ', ' + RN(r, size)); | |
| 420 | - case size of | |
| 421 | - 1: OpM(0, false, $88, r, m, true); | |
| 422 | - 2: OpM($66, false, $89, r, m); | |
| 423 | - 4: OpM(0, false, $89, r, m); | |
| 424 | - else OpM(0, true, $89, r, m); | |
| 425 | - end; | |
| 426 | -end; | |
| 427 | - | |
| 428 | -procedure Lea(r: longint; const m: TMem); | |
| 429 | -begin | |
| 430 | - if listing then Say('lea ' + RN(r) + ', ' + MN(m)); | |
| 431 | - OpM(0, true, $8D, r, m); | |
| 432 | -end; | |
| 433 | - | |
| 434 | -procedure Alu(op, dst, src: longint); | |
| 435 | -begin | |
| 436 | - if listing then Say(AluNames[op] + ' ' + RN(dst) + ', ' + RN(src)); | |
| 437 | - OpR(0, true, op shl 3 or 1, src, dst); | |
| 438 | -end; | |
| 439 | - | |
| 440 | -procedure AluI(op, r: longint; v: longint); | |
| 441 | -begin | |
| 442 | - if listing then Say(AluNames[op] + ' ' + RN(r) + ', ' + IntStr(v)); | |
| 443 | - if (v >= -128) and (v <= 127) then begin | |
| 444 | - OpR(0, true, $83, op, r); Put1(v) | |
| 445 | - end else begin | |
| 446 | - OpR(0, true, $81, op, r); Put4(v) | |
| 447 | - end; | |
| 448 | -end; | |
| 449 | - | |
| 450 | -{ op r, [m] } | |
| 451 | -procedure AluM(op, r: longint; const m: TMem); | |
| 452 | -begin | |
| 453 | - if listing then Say(AluNames[op] + ' ' + RN(r) + ', ' + MN(m)); | |
| 454 | - OpM(0, true, op shl 3 or 3, r, m); | |
| 455 | -end; | |
| 456 | - | |
| 457 | -procedure Imul(dst, src: longint); | |
| 458 | -begin | |
| 459 | - if listing then Say('imul ' + RN(dst) + ', ' + RN(src)); | |
| 460 | - OpR(0, true, $AF0F, dst, src); | |
| 461 | -end; | |
| 462 | - | |
| 463 | -procedure ImulI(r: longint; v: longint); | |
| 464 | -begin | |
| 465 | - if listing then Say('imul ' + RN(r) + ', ' + RN(r) + ', ' + IntStr(v)); | |
| 466 | - OpR(0, true, $69, r, r); | |
| 467 | - Put4(v); | |
| 468 | -end; | |
| 469 | - | |
| 470 | -procedure Shift(op, r: longint); | |
| 471 | -begin | |
| 472 | - if listing then Say(ShiftNames[op] + ' ' + RN(r) + ', cl'); | |
| 473 | - OpR(0, true, $D3, op, r); | |
| 474 | -end; | |
| 475 | - | |
| 476 | -procedure ShiftI(op, r, n: longint); | |
| 477 | -begin | |
| 478 | - if listing then Say(ShiftNames[op] + ' ' + RN(r) + ', ' + IntStr(n)); | |
| 479 | - OpR(0, true, $C1, op, r); | |
| 480 | - Put1(n); | |
| 481 | -end; | |
| 482 | - | |
| 483 | -procedure Unary(ext, r: longint); | |
| 484 | -begin | |
| 485 | - if listing then Say(UnaryNames[ext] + ' ' + RN(r)); | |
| 486 | - OpR(0, true, $F7, ext, r); | |
| 487 | -end; | |
| 488 | - | |
| 489 | -procedure Cqo; | |
| 490 | -begin | |
| 491 | - if listing then Say('cqo'); | |
| 492 | - Put1($48); Put1($99); | |
| 493 | -end; | |
| 494 | - | |
| 495 | -{ Brings a value of size bytes in r to 64 bits, by its sign or zeros. } | |
| 496 | -procedure Extend(r, size: longint; signed: boolean); | |
| 497 | -begin | |
| 498 | - if not listing then | |
| 499 | - else if size = 4 then begin | |
| 500 | - if signed then Say('movsxd ' + RN(r) + ', ' + RN(r, 4)) else Say('mov ' + RN(r, 4) + ', ' + RN(r, 4)) | |
| 501 | - end else if signed then Say('movsx ' + RN(r) + ', ' + RN(r, size)) | |
| 502 | - else Say('movzx ' + RN(r, 4) + ', ' + RN(r, size)); | |
| 503 | - case size of | |
| 504 | - 1: if signed then OpR(0, true, $BE0F, r, r, true) else OpR(0, false, $B60F, r, r, true); | |
| 505 | - 2: if signed then OpR(0, true, $BF0F, r, r) else OpR(0, false, $B70F, r, r); | |
| 506 | - 4: if signed then OpR(0, true, $63, r, r) else OpR(0, false, $89, r, r); | |
| 507 | - end; | |
| 508 | -end; | |
| 509 | - | |
| 510 | -{ r := 1 if the condition holds, else 0 } | |
| 511 | -procedure SetCC(cc, r: longint); | |
| 512 | -begin | |
| 513 | - if listing then Say('set' + CondNames[cc] + ' ' + RN(r, 1)); | |
| 514 | - OpR(0, false, $900F + cc shl 8, 0, r, true); | |
| 515 | - if listing then Say('movzx ' + RN(r, 4) + ', ' + RN(r, 1)); | |
| 516 | - OpR(0, false, $B60F, r, r, true); | |
| 517 | -end; | |
| 518 | - | |
| 519 | -procedure TestRR(a, b: longint); | |
| 520 | -begin | |
| 521 | - if listing then Say('test ' + RN(a) + ', ' + RN(b)); | |
| 522 | - OpR(0, true, $85, b, a); | |
| 523 | -end; | |
| 524 | - | |
| 525 | -procedure Push(r: longint); | |
| 526 | -begin | |
| 527 | - if listing then Say('push ' + RN(r)); | |
| 528 | - if r >= 8 then Put1($41); | |
| 529 | - Put1($50 + r and 7); | |
| 530 | -end; | |
| 531 | - | |
| 532 | -procedure Pop(r: longint); | |
| 533 | -begin | |
| 534 | - if listing then Say('pop ' + RN(r)); | |
| 535 | - if r >= 8 then Put1($41); | |
| 536 | - Put1($58 + r and 7); | |
| 537 | -end; | |
| 538 | - | |
| 539 | -procedure CallChunk(c: longint); | |
| 540 | -begin | |
| 541 | - if listing then Say('call ' + ChunkName(c)); | |
| 542 | - Put1($E8); | |
| 543 | - AddFixup(pc, c, 0); | |
| 544 | - Put4(0); | |
| 545 | -end; | |
| 546 | - | |
| 547 | -procedure CallBack(target: longint); | |
| 548 | -begin | |
| 549 | - if listing then Say('call'); | |
| 550 | - Put1($E8); | |
| 551 | - Put4(target - (pc + 4)); | |
| 552 | -end; | |
| 553 | - | |
| 554 | -procedure CallR(r: longint); | |
| 555 | -begin | |
| 556 | - if listing then Say('call ' + RN(r)); | |
| 557 | - OpR(0, false, $FF, 2, r); | |
| 558 | -end; | |
| 559 | - | |
| 560 | -procedure JmpR(r: longint); | |
| 561 | -begin | |
| 562 | - if listing then Say('jmp ' + RN(r)); | |
| 563 | - OpR(0, false, $FF, 4, r); | |
| 564 | -end; | |
| 565 | - | |
| 566 | -procedure CallM(const m: TMem); | |
| 567 | -begin | |
| 568 | - if listing then Say('call ' + MN(m)); | |
| 569 | - OpM(0, false, $FF, 2, m); | |
| 570 | -end; | |
| 571 | - | |
| 572 | -procedure Ret; begin if listing then Say('ret'); Put1($C3) end; | |
| 573 | -procedure Leave; begin Say('leave'); Put1($C9) end; | |
| 574 | -procedure Syscall; begin Say('syscall'); Put1($0F); Put1($05) end; | |
| 575 | -procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end; | |
| 576 | -procedure Ud2; begin Say('ud2'); Put1($0F); Put1($0B) end; | |
| 577 | - | |
| 578 | -{ Code made up to a multiple of n bytes with no-ops, the fewest: the | |
| 579 | - long forms of nop, of up to 8 bytes, that x86 decodes as one } | |
| 580 | -procedure Align(n: longint); | |
| 581 | -const | |
| 582 | - Nops: array[1..8] of string[8] = (#$90, #$66#$90, #$0F#$1F#$00, #$0F#$1F#$40#$00, | |
| 583 | - #$0F#$1F#$44#$00#$00, #$66#$0F#$1F#$44#$00#$00, #$0F#$1F#$80#$00#$00#$00#$00, | |
| 584 | - #$0F#$1F#$84#$00#$00#$00#$00#$00); | |
| 585 | -var k, i: longint; | |
| 586 | -begin | |
| 587 | - while pc mod n <> 0 do begin | |
| 588 | - k := n - pc mod n; | |
| 589 | - if k > 8 then k := 8; | |
| 590 | - Say('nop'); | |
| 591 | - for i := 1 to k do Put1(ord(Nops[k][i])); | |
| 592 | - end; | |
| 593 | -end; | |
| 594 | -procedure RepMovsb; begin Say('rep movsb'); Put1($F3); Put1($A4) end; | |
| 595 | -{ the direction flag, which has rep movsb go down, and back up } | |
| 596 | -procedure Std; begin Say('std'); Put1($FD) end; | |
| 597 | -procedure Cld; begin Say('cld'); Put1($FC) end; | |
| 598 | -procedure RepStosb; begin Say('rep stosb'); Put1($F3); Put1($AA) end; | |
| 599 | -procedure RepeCmpsb; begin Say('repe cmpsb'); Put1($F3); Put1($A6) end; | |
| 600 | - | |
| 601 | -procedure LockCmpxchg(const m: TMem; r: longint); | |
| 602 | -begin | |
| 603 | - if listing then Say('lock cmpxchg ' + MN(m) + ', ' + RN(r)); | |
| 604 | - Put1($F0); | |
| 605 | - OpM(0, true, $B10F, r, m); | |
| 606 | -end; | |
| 607 | - | |
| 608 | -procedure Xchg(const m: TMem; r: longint); | |
| 609 | -begin | |
| 610 | - if listing then Say('xchg ' + MN(m) + ', ' + RN(r)); | |
| 611 | - OpM(0, true, $87, r, m); | |
| 612 | -end; | |
| 613 | - | |
| 614 | -procedure LockXadd(const m: TMem; r: longint); | |
| 615 | -begin | |
| 616 | - if listing then Say('lock xadd ' + MN(m) + ', ' + RN(r)); | |
| 617 | - Put1($F0); | |
| 618 | - OpM(0, true, $C10F, r, m); | |
| 619 | -end; | |
| 620 | - | |
| 621 | -procedure MovqXR(x, r: longint); | |
| 622 | -begin | |
| 623 | - if listing then Say('movq ' + XN(x) + ', ' + RN(r)); | |
| 624 | - OpR($66, true, $6E0F, x, r); | |
| 625 | -end; | |
| 626 | - | |
| 627 | -procedure MovqRX(r, x: longint); | |
| 628 | -begin | |
| 629 | - if listing then Say('movq ' + RN(r) + ', ' + XN(x)); | |
| 630 | - OpR($66, true, $7E0F, x, r); | |
| 631 | -end; | |
| 632 | - | |
| 633 | -{ movaps: all of xmm y to xmm x, the shortest move between two } | |
| 634 | -procedure MovX(x, y: longint); | |
| 635 | -begin | |
| 636 | - if listing then Say('movaps ' + XN(x) + ', ' + XN(y)); | |
| 637 | - OpR(0, false, $280F, x, y); | |
| 638 | -end; | |
| 639 | - | |
| 640 | -const | |
| 641 | - RealPfx: array[boolean] of longint = ($F2, $F3); | |
| 642 | - RealEnd: array[boolean] of string[2] = ('sd', 'ss'); | |
| 643 | - | |
| 644 | -procedure MovsXM(x: longint; const m: TMem; single: boolean); | |
| 645 | -begin | |
| 646 | - if listing then Say('movs' + RealEnd[single][2] + ' ' + XN(x) + ', ' + MN(m)); | |
| 647 | - OpM(RealPfx[single], false, $100F, x, m); | |
| 648 | -end; | |
| 649 | - | |
| 650 | -procedure MovsMX(const m: TMem; x: longint; single: boolean); | |
| 651 | -begin | |
| 652 | - if listing then Say('movs' + RealEnd[single][2] + ' ' + MN(m) + ', ' + XN(x)); | |
| 653 | - OpM(RealPfx[single], false, $110F, x, m); | |
| 654 | -end; | |
| 655 | - | |
| 656 | -function SseName(op: longint; single: boolean): shortstring; | |
| 657 | -begin | |
| 658 | - case op of | |
| 659 | - $58: SseName := 'add' + RealEnd[single]; | |
| 660 | - $59: SseName := 'mul' + RealEnd[single]; | |
| 661 | - $5C: SseName := 'sub' + RealEnd[single]; | |
| 662 | - $5E: SseName := 'div' + RealEnd[single]; | |
| 663 | - else SseName := 'cvt' + RealEnd[single] + '2' + RealEnd[not single]; | |
| 664 | - end; | |
| 665 | -end; | |
| 666 | - | |
| 667 | -procedure Sse(op, x, y: longint; single: boolean); | |
| 668 | -begin | |
| 669 | - if listing then Say(SseName(op, single) + ' ' + XN(x) + ', ' + XN(y)); | |
| 670 | - OpR(RealPfx[single], false, $0F or op shl 8, x, y); | |
| 671 | -end; | |
| 672 | - | |
| 673 | -{ The same, its right operand in memory: addsd x, [m] } | |
| 674 | -procedure SseM(op, x: longint; const m: TMem; single: boolean); | |
| 675 | -begin | |
| 676 | - if listing then Say(SseName(op, single) + ' ' + XN(x) + ', ' + MN(m)); | |
| 677 | - OpM(RealPfx[single], false, $0F or op shl 8, x, m); | |
| 678 | -end; | |
| 679 | - | |
| 680 | -procedure Ucomis(x, y: longint; single: boolean); | |
| 681 | -begin | |
| 682 | - if listing then Say('ucomi' + RealEnd[single] + ' ' + XN(x) + ', ' + XN(y)); | |
| 683 | - if single then OpR(0, false, $2E0F, x, y) else OpR($66, false, $2E0F, x, y); | |
| 684 | -end; | |
| 685 | - | |
| 686 | -procedure Cvtsi2s(x, r: longint; single: boolean); | |
| 687 | -begin | |
| 688 | - if listing then Say('cvtsi2' + RealEnd[single] + ' ' + XN(x) + ', ' + RN(r)); | |
| 689 | - OpR(RealPfx[single], true, $2A0F, x, r); | |
| 690 | -end; | |
| 691 | - | |
| 692 | -procedure Cvtts2si(r, x: longint; single: boolean); | |
| 693 | -begin | |
| 694 | - if listing then Say('cvtt' + RealEnd[single] + '2si ' + RN(r) + ', ' + XN(x)); | |
| 695 | - OpR(RealPfx[single], true, $2C0F, r, x); | |
| 696 | -end; | |
| 697 | - | |
| 698 | -procedure Btc(r, bit: longint); | |
| 699 | -begin | |
| 700 | - if listing then Say('btc ' + RN(r) + ', ' + IntStr(bit)); | |
| 701 | - OpR(0, true, $BA0F, 7, r); | |
| 702 | - Put1(bit); | |
| 703 | -end; | |
| 704 | - | |
| 705 | -{ Jumps. A label not yet placed is the chain of the jumps to it, each | |
| 706 | - rel32 field holding the position of the one before, NoLabel ending | |
| 707 | - it. Placing the label walks the chain and patches each. } | |
| 708 | - | |
| 709 | -procedure Jmp(var L: longint); | |
| 710 | -begin | |
| 711 | - if listing then Say('jmp'); | |
| 712 | - Put1($E9); | |
| 713 | - Put4(L); | |
| 714 | - L := pc - 4; | |
| 715 | -end; | |
| 716 | - | |
| 717 | -procedure Jcc(cc: longint; var L: longint); | |
| 718 | -begin | |
| 719 | - if listing then Say('j' + CondNames[cc]); | |
| 720 | - Put1($0F); Put1($80 + cc); | |
| 721 | - Put4(L); | |
| 722 | - L := pc - 4; | |
| 723 | -end; | |
| 724 | - | |
| 725 | -procedure JmpBack(target: longint); | |
| 726 | -begin | |
| 727 | - if listing then Say('jmp'); | |
| 728 | - Put1($E9); | |
| 729 | - Put4(target - (pc + 4)); | |
| 730 | -end; | |
| 731 | - | |
| 732 | -procedure JccBack(cc, target: longint); | |
| 733 | -begin | |
| 734 | - if listing then Say('j' + CondNames[cc]); | |
| 735 | - Put1($0F); Put1($80 + cc); | |
| 736 | - Put4(target - (pc + 4)); | |
| 737 | -end; | |
| 738 | - | |
| 739 | -procedure FixLinkTo(L, target: longint); | |
| 740 | -var next: longint; | |
| 741 | -begin | |
| 742 | - while L <> NoLabel do begin | |
| 743 | - next := Get4At(L); | |
| 744 | - Put4At(L, target - (L + 4)); | |
| 745 | - L := next; | |
| 746 | - end; | |
| 747 | -end; | |
| 748 | - | |
| 749 | -procedure FixLink(L: longint); | |
| 750 | -begin | |
| 751 | - FixLinkTo(L, pc); | |
| 752 | -end; | |
| 753 | - | |
| 754 | -{ If the last instruction made is a jmp to L, it is taken back, since L | |
| 755 | - is to be placed just after it: a return at the end of a procedure | |
| 756 | - needn't jump to the code that returns. L is the place of the latest | |
| 757 | - jump's 4-byte field, so the last instruction is that jump when L is | |
| 758 | - 4 bytes back and an E9 byte (jmp) is before it; a jcc's field follows | |
| 759 | - the bytes 0F 8x, never E9. L then becomes the next jump on the chain, | |
| 760 | - which the field held. The caller must place L next, before anything | |
| 761 | - can have noted where the jmp ended. } | |
| 762 | -procedure DropJumpTo(var L: longint); | |
| 763 | -begin | |
| 764 | - if (L = NoLabel) or (L <> pc - 4) or (code^[L - 1] <> $E9) then exit; | |
| 765 | - L := Get4At(L); | |
| 766 | - TakeBack(5); | |
| 767 | - if listing and (numSaid > 0) and (said[numSaid - 1].at = pc) then dec(numSaid); | |
| 768 | -end; | |
| 769 | - | |
| 770 | -{ The jumps of chains L1 and L2, as one chain: each jump's field holds | |
| 771 | - the next one's place until it is fixed, the last NoLabel } | |
| 772 | -function JoinLinks(L1, L2: longint): longint; | |
| 773 | -var L: longint; | |
| 774 | -begin | |
| 775 | - if L2 = NoLabel then exit(L1); | |
| 776 | - if L1 <> NoLabel then begin | |
| 777 | - L := L2; | |
| 778 | - while Get4At(L) <> NoLabel do L := Get4At(L); | |
| 779 | - Put4At(L, L1); | |
| 780 | - end; | |
| 781 | - JoinLinks := L2; | |
| 782 | -end; | |
| 783 | - | |
| 784 | - | |
| 785 | -{ The assembler, for code procedures: x86-64 in Intel's syntax as NASM | |
| 786 | - writes it (report, appendix A). It reads instructions with the | |
| 787 | - scanner up to end, and encodes them with OpM and OpR; constants are | |
| 788 | - read by the parser's constExpr. } | |
| 789 | - | |
| 790 | -const | |
| 791 | - NoBase = -3; { a memory operand's base, not yet read } | |
| 792 | - | |
| 793 | -type | |
| 794 | - TOpKind = (oReg, oXmm, oMem, oImm); | |
| 795 | - TOpd = record | |
| 796 | - kind: TOpKind; | |
| 797 | - r, size: longint; { oReg, oXmm: the register and its size; oMem: its size, or 0 } | |
| 798 | - m: TMem; | |
| 799 | - v: int64; { oImm } | |
| 800 | - end; | |
| 801 | - | |
| 802 | -{ The current symbol as a word: an identifier, or a reserved word that | |
| 803 | - is also an instruction } | |
| 804 | -function AsmWord: shortstring; | |
| 805 | -begin | |
| 806 | - case tok of | |
| 807 | - sIdent: AsmWord := NameOf(id); | |
| 808 | - sAnd: AsmWord := 'and'; | |
| 809 | - sOr: AsmWord := 'or'; | |
| 810 | - sXor: AsmWord := 'xor'; | |
| 811 | - sNot: AsmWord := 'not'; | |
| 812 | - sDiv: AsmWord := 'div'; | |
| 813 | - sShl: AsmWord := 'shl'; | |
| 814 | - sShr: AsmWord := 'shr'; | |
| 815 | - else AsmWord := ''; | |
| 816 | - end; | |
| 817 | -end; | |
| 818 | - | |
| 819 | -{ Whether w names a register: rax, eax, ax, al and so on, or xmm0 to | |
| 820 | - xmm15 (size 16) } | |
| 821 | -function IsReg(const w: shortstring; var r, size: longint): boolean; | |
| 822 | -const Sizes: array[0..3] of longint = (8, 4, 2, 1); | |
| 823 | -var i, j, e: longint; | |
| 824 | -begin | |
| 825 | - IsReg := true; | |
| 826 | - for i := 0 to 3 do | |
| 827 | - for j := 0 to 15 do | |
| 828 | - if RegNames[i, j] = w then begin r := j; size := Sizes[i]; exit end; | |
| 829 | - size := 16; | |
| 830 | - Val(copy(w, 4, 2), r, e); | |
| 831 | - IsReg := (copy(w, 1, 3) = 'xmm') and (length(w) > 3) and (e = 0) and (r in [0..15]); | |
| 832 | -end; | |
| 833 | - | |
| 834 | -{ Which word of list, a list of words each with a space either side, | |
| 835 | - w is, from 0; -1 if none } | |
| 836 | -function WordIndex(const w, list: shortstring): longint; | |
| 837 | -var i, k: longint; | |
| 838 | -begin | |
| 839 | - k := pos(' ' + w + ' ', list); | |
| 840 | - WordIndex := -1; | |
| 841 | - if k = 0 then exit; | |
| 842 | - WordIndex := 0; | |
| 843 | - for i := 2 to k do if list[i] = ' ' then inc(WordIndex); | |
| 844 | -end; | |
| 845 | - | |
| 846 | -{ The condition a name ends in: e of je, nz of jnz } | |
| 847 | -function Cond(const w: shortstring): longint; | |
| 848 | -const | |
| 849 | - Alias: array[0..11] of string[3] = ('c', 'nae', 'nc', 'nb', 'z', 'nz', 'na', 'nbe', 'nge', 'nl', 'ng', 'nle'); | |
| 850 | - AliasCc: array[0..11] of longint = (2, 2, 3, 3, 4, 5, 6, 7, 12, 13, 14, 15); | |
| 851 | -var i: longint; | |
| 852 | -begin | |
| 853 | - for i := 0 to 15 do if CondNames[i] = w then exit(i); | |
| 854 | - for i := 0 to 11 do if Alias[i] = w then exit(AliasCc[i]); | |
| 855 | - Cond := -1; | |
| 856 | -end; | |
| 857 | - | |
| 858 | -procedure Operand(var o: TOpd; constExpr: TConstFn); | |
| 859 | -var w: shortstring; r, size, sz: longint; st: TScanState; | |
| 860 | -begin | |
| 861 | - FillChar(o, SizeOf(o), 0); | |
| 862 | - w := AsmWord; | |
| 863 | - sz := 0; | |
| 864 | - if w = 'byte' then sz := 1 else if w = 'word' then sz := 2 | |
| 865 | - else if w = 'dword' then sz := 4 else if w = 'qword' then sz := 8; | |
| 866 | - if sz <> 0 then begin Next; w := AsmWord end; | |
| 867 | - if tok = sLbrak then begin | |
| 868 | - { [base + index * scale + disp], the displacement last } | |
| 869 | - Next; | |
| 870 | - o.kind := oMem; o.size := sz; | |
| 871 | - o.m := Mem(NoBase, 0); | |
| 872 | - repeat | |
| 873 | - if IsReg(AsmWord, r, size) and (size = 8) then begin | |
| 874 | - Next; | |
| 875 | - { the scale is a single number or constant: a whole expression | |
| 876 | - would take in the displacement after it, as 4 + 16 in | |
| 877 | - [rdi + rsi * 4 + 16] } | |
| 878 | - if tok = sTimes then begin | |
| 879 | - Next; | |
| 880 | - o.m.index := r; | |
| 881 | - o.m.scale := constExpr(false); | |
| 882 | - end | |
| 883 | - else if o.m.base = NoBase then o.m.base := r | |
| 884 | - else begin o.m.index := r; o.m.scale := 1 end; | |
| 885 | - end else o.m.disp := o.m.disp + constExpr(true); | |
| 886 | - if not (tok in [sPlus, sMinus]) then break; | |
| 887 | - SaveScan(st); | |
| 888 | - Next; | |
| 889 | - if not IsReg(AsmWord, r, size) and (st.tok = sMinus) then RestoreScan(st); | |
| 890 | - until false; | |
| 891 | - if (tok <> sRbrak) or (o.m.base = NoBase) or not (o.m.scale in [1, 2, 4, 8]) then | |
| 892 | - Mark('expected a memory operand, [base + index * scale + disp]'); | |
| 893 | - Next; | |
| 894 | - end else if IsReg(w, o.r, o.size) then begin | |
| 895 | - if o.size = 16 then o.kind := oXmm else o.kind := oReg; | |
| 896 | - Next; | |
| 897 | - end else begin | |
| 898 | - o.kind := oImm; | |
| 899 | - o.v := constExpr(true); | |
| 900 | - end; | |
| 901 | -end; | |
| 902 | - | |
| 903 | -{ op with register reg and the register or memory operand o } | |
| 904 | -procedure RM(pfx: longint; w: boolean; op, reg: longint; const o: TOpd; byteReg: boolean = false); | |
| 905 | -begin | |
| 906 | - if o.kind = oMem then OpM(pfx, w, op, reg, o.m, byteReg) | |
| 907 | - else OpR(pfx, w, op, reg, o.r, byteReg); | |
| 908 | -end; | |
| 909 | - | |
| 910 | -procedure PutImm(v: int64; size: longint); | |
| 911 | -begin | |
| 912 | - case size of | |
| 913 | - 1: Put1(v); | |
| 914 | - 2: begin Put1(v); Put1(v shr 8) end; | |
| 915 | - else Put4(v); | |
| 916 | - end; | |
| 917 | -end; | |
| 918 | - | |
| 919 | -procedure Assemble(constExpr: TConstFn); | |
| 920 | -const | |
| 921 | - Alus = ' add or adc sbb and sub xor cmp '; | |
| 922 | - Unaries = ' not neg mul imul div idiv '; | |
| 923 | - Shifts = ' rol ror rcl rcr shl shr sal sar '; | |
| 924 | - SseOps = ' sqrt add mul sub min div max '; | |
| 925 | - SseCodes: array[0..6] of longint = ($51, $58, $59, $5C, $5D, $5E, $5F); | |
| 926 | - { SSE2 integer instructions that work on 16 bytes at a time: 66 0F code, | |
| 927 | - xmm and xmm or memory (pshufd also takes a byte) } | |
| 928 | - PackedOps = ' punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb '; | |
| 929 | - PackedCodes: array[0..9] of longint = ($60, $61, $74, $70, $DB, $DF, $EB, $EF, $FC, $F8); | |
| 930 | - { bit scans and counts: reg, reg or memory, with the prefix and code that tell them } | |
| 931 | - BitOps = ' bsf bsr tzcnt lzcnt popcnt '; | |
| 932 | - BitPfx: array[0..4] of longint = (0, 0, $F3, $F3, $F3); | |
| 933 | - BitCodes: array[0..4] of longint = ($BC0F, $BD0F, $BC0F, $BD0F, $B80F); | |
| 934 | - Plain: array[0..14] of string[7] = ('ret', 'leave', 'syscall', 'cpuid', 'rdtsc', | |
| 935 | - 'pause', 'nop', 'ud2', 'int3', 'hlt', 'cqo', 'cdq', 'mfence', 'lfence', 'sfence'); | |
| 936 | - PlainOp: array[0..14] of longint = ($C3, $C9, $050F, $A20F, $310F, | |
| 937 | - $90F3, $90, $0B0F, $CC, $F4, $9948, $99, $F0AE0F, $E8AE0F, $F8AE0F); | |
| 938 | -var | |
| 939 | - w: shortstring; o: array[0..2] of TOpd; n, i, k, size, pfx, cc: longint; | |
| 940 | - wide, byteOp, wasListing: boolean; at, start: TPos; v: int64; first: longint; | |
| 941 | - names, places, chains: array[0..63] of longint; numLabels: longint; | |
| 942 | - usedAt: array[0..63] of TPos; | |
| 943 | - | |
| 944 | - { the label called n, entered if new } | |
| 945 | - function Lab(n: longint): longint; | |
| 946 | - var i: longint; | |
| 947 | - begin | |
| 948 | - for i := 0 to numLabels - 1 do if names[i] = n then exit(i); | |
| 949 | - if numLabels > High(names) then Mark('more than 64 labels'); | |
| 950 | - names[numLabels] := n; places[numLabels] := -1; chains[numLabels] := NoLabel; | |
| 951 | - usedAt[numLabels] := TokPos; | |
| 952 | - Lab := numLabels; | |
| 953 | - inc(numLabels); | |
| 954 | - end; | |
| 955 | - | |
| 956 | - procedure Bad; | |
| 957 | - begin | |
| 958 | - MarkAt(at, 'can''t assemble ' + w + ' with these operands'); | |
| 959 | - end; | |
| 960 | - | |
| 961 | - { an instruction of two operands: a register or memory, and a | |
| 962 | - register (opcode toRM), the other way (fromRM), or an immediate | |
| 963 | - (opImm /digit, or none if opImm < 0) } | |
| 964 | - procedure Two(toRM, fromRM, opImm, digit: longint); | |
| 965 | - begin | |
| 966 | - if n <> 2 then Bad; | |
| 967 | - if o[1].kind = oImm then begin | |
| 968 | - if (o[0].kind = oImm) or (opImm < 0) then Bad; | |
| 969 | - if (opImm = $81) and not byteOp and (o[1].v >= -128) and (o[1].v <= 127) then begin | |
| 970 | - RM(pfx, wide, $83, digit, o[0]); { a byte, sign-extended } | |
| 971 | - Put1(o[1].v); | |
| 972 | - end else begin | |
| 973 | - RM(pfx, wide, opImm - ord(byteOp), digit, o[0]); | |
| 974 | - PutImm(o[1].v, size); | |
| 975 | - end; | |
| 976 | - end else if (o[1].kind = oReg) and (o[0].kind in [oReg, oMem]) then | |
| 977 | - RM(pfx, wide, toRM - ord(byteOp), o[1].r, o[0], byteOp) | |
| 978 | - else if (o[0].kind = oReg) and (o[1].kind = oMem) then | |
| 979 | - RM(pfx, wide, fromRM - ord(byteOp), o[0].r, o[1], byteOp) | |
| 980 | - else Bad; | |
| 981 | - end; | |
| 982 | - | |
| 983 | -begin | |
| 984 | - numLabels := 0; | |
| 985 | - while not (tok in [sEnd, sEof]) do begin | |
| 986 | - at := TokPos; | |
| 987 | - w := AsmWord; | |
| 988 | - if tok = sSemicolon then begin Next; continue end; | |
| 989 | - if w = '' then Mark('expected an instruction'); | |
| 990 | - Next; | |
| 991 | - if tok = sColon then begin | |
| 992 | - SayAt(pc, w + ':'); | |
| 993 | - k := Lab(Intern(@w[1], length(w))); | |
| 994 | - if places[k] >= 0 then MarkAt(at, 'label ' + w + ' is defined twice'); | |
| 995 | - places[k] := pc; | |
| 996 | - FixLink(chains[k]); | |
| 997 | - Next; | |
| 998 | - continue; | |
| 999 | - end; | |
| 1000 | - first := pc; | |
| 1001 | - wasListing := listing; | |
| 1002 | - listing := false; { the instruction is said as it is written } | |
| 1003 | - if w = 'lock' then begin | |
| 1004 | - Put1($F0); | |
| 1005 | - w := AsmWord; | |
| 1006 | - Next; | |
| 1007 | - end; | |
| 1008 | - cc := -1; | |
| 1009 | - if w = 'db' then | |
| 1010 | - repeat | |
| 1011 | - start := TokPos; | |
| 1012 | - v := constExpr(true); | |
| 1013 | - if (v < -128) or (v > 255) then MarkAt(SpanFrom(start), 'expected a byte from 0 to 255, found ' + IntStr(v)); | |
| 1014 | - Put1(v); | |
| 1015 | - if tok <> sComma then break; | |
| 1016 | - Next; | |
| 1017 | - until false | |
| 1018 | - else if (w[1] = 'j') and ((w = 'jmp') or (Cond(copy(w, 2, 3)) >= 0)) and (tok = sIdent) then begin | |
| 1019 | - { a jump to a label } | |
| 1020 | - k := Lab(id); | |
| 1021 | - Next; | |
| 1022 | - if w <> 'jmp' then cc := Cond(copy(w, 2, 3)); | |
| 1023 | - if places[k] >= 0 then begin | |
| 1024 | - if cc < 0 then JmpBack(places[k]) else JccBack(cc, places[k]) | |
| 1025 | - end else if cc < 0 then Jmp(chains[k]) else Jcc(cc, chains[k]); | |
| 1026 | - end else begin | |
| 1027 | - n := 0; | |
| 1028 | - if tok <> sSemicolon then | |
| 1029 | - repeat | |
| 1030 | - if n > High(o) then Mark('too many operands'); | |
| 1031 | - Operand(o[n], constExpr); | |
| 1032 | - inc(n); | |
| 1033 | - if tok <> sComma then break; | |
| 1034 | - Next; | |
| 1035 | - until false; | |
| 1036 | - { the size: a register's, or what the memory operand says } | |
| 1037 | - size := 8; | |
| 1038 | - for i := n - 1 downto 0 do | |
| 1039 | - if (o[i].kind = oReg) or (o[i].kind = oMem) and (o[i].size <> 0) then size := o[i].size; | |
| 1040 | - if n > 0 then | |
| 1041 | - if (o[0].kind = oReg) or (o[0].kind = oMem) and (o[0].size <> 0) then size := o[0].size; | |
| 1042 | - wide := size = 8; | |
| 1043 | - byteOp := size = 1; | |
| 1044 | - if size = 2 then pfx := $66 else pfx := 0; | |
| 1045 | - if WordIndex(w, Alus) >= 0 then begin | |
| 1046 | - k := WordIndex(w, Alus); | |
| 1047 | - Two(k * 8 + 1, k * 8 + 3, $81, k) | |
| 1048 | - end | |
| 1049 | - else if w = 'mov' then begin | |
| 1050 | - if (n = 2) and (o[0].kind = oReg) and (o[1].kind = oImm) and (size >= 4) then begin | |
| 1051 | - if wide then MovRI(o[0].r, o[1].v) | |
| 1052 | - else begin Prefix(0, false, 0, NoIndex, o[0].r, false); Put1($B8 + o[0].r and 7); Put4(o[1].v) end; | |
| 1053 | - end else Two($89, $8B, $C7, 0); | |
| 1054 | - end else if w = 'test' then Two($85, $85, $F7, 0) | |
| 1055 | - else if w = 'xchg' then Two($87, $87, -1, 0) | |
| 1056 | - else if (w = 'cmpxchg') or (w = 'xadd') then begin | |
| 1057 | - if (n <> 2) or (o[1].kind <> oReg) then Bad; | |
| 1058 | - if w = 'xadd' then k := $C10F else k := $B10F; | |
| 1059 | - RM(pfx, wide, k - ord(byteOp) shl 8, o[1].r, o[0], byteOp); | |
| 1060 | - end else if w = 'lea' then begin | |
| 1061 | - if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind <> oMem) then Bad; | |
| 1062 | - RM(pfx, wide, $8D, o[0].r, o[1]); | |
| 1063 | - end else if (w = 'movzx') or (w = 'movsx') or (w = 'movsxd') then begin | |
| 1064 | - if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) then Bad; | |
| 1065 | - if w = 'movsxd' then k := $63 | |
| 1066 | - else if o[1].size = 1 then k := $B60F | |
| 1067 | - else if o[1].size = 2 then k := $B70F | |
| 1068 | - else Bad; | |
| 1069 | - if w = 'movsx' then k := k + $0800; | |
| 1070 | - RM(pfx, wide, k, o[0].r, o[1], o[1].size = 1); | |
| 1071 | - end else if ((WordIndex(w, Unaries) >= 0) and (n = 1)) or (w = 'inc') or (w = 'dec') then begin | |
| 1072 | - if o[0].kind = oImm then Bad; | |
| 1073 | - if w = 'inc' then RM(pfx, wide, $FF - ord(byteOp), 0, o[0], byteOp) | |
| 1074 | - else if w = 'dec' then RM(pfx, wide, $FF - ord(byteOp), 1, o[0], byteOp) | |
| 1075 | - else RM(pfx, wide, $F7 - ord(byteOp), WordIndex(w, Unaries) + 2, o[0], byteOp); | |
| 1076 | - end else if w = 'imul' then begin | |
| 1077 | - if (n < 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) then Bad; | |
| 1078 | - if n = 2 then RM(pfx, wide, $AF0F, o[0].r, o[1]) | |
| 1079 | - else begin RM(pfx, wide, $69, o[0].r, o[1]); PutImm(o[2].v, size) end; | |
| 1080 | - end else if WordIndex(w, Shifts) >= 0 then begin | |
| 1081 | - k := WordIndex(w, Shifts); | |
| 1082 | - if k = 6 then k := 4; { sal is shl } | |
| 1083 | - if (n <> 2) or (o[0].kind = oImm) then Bad; | |
| 1084 | - if o[1].kind = oImm then begin RM(pfx, wide, $C1 - ord(byteOp), k, o[0], byteOp); Put1(o[1].v) end | |
| 1085 | - else if (o[1].kind = oReg) and (o[1].r = RCX) and (o[1].size = 1) then RM(pfx, wide, $D3 - ord(byteOp), k, o[0], byteOp) | |
| 1086 | - else Bad; | |
| 1087 | - end else if (w = 'push') or (w = 'pop') then begin | |
| 1088 | - if (n <> 1) or (o[0].kind <> oReg) or not wide then Bad; | |
| 1089 | - if w = 'push' then Push(o[0].r) else Pop(o[0].r); | |
| 1090 | - end else if (w = 'call') or (w = 'jmp') then begin | |
| 1091 | - if (n <> 1) or (o[0].kind = oImm) then Bad; | |
| 1092 | - if w = 'call' then RM(0, false, $FF, 2, o[0]) else RM(0, false, $FF, 4, o[0]); | |
| 1093 | - end else if (copy(w, 1, 3) = 'set') and (Cond(copy(w, 4, 3)) >= 0) then begin | |
| 1094 | - if (n <> 1) or (o[0].kind = oImm) or (size <> 1) then Bad; | |
| 1095 | - RM(0, false, $900F + Cond(copy(w, 4, 3)) shl 8, 0, o[0], true); | |
| 1096 | - end else if (copy(w, 1, 4) = 'cmov') and (Cond(copy(w, 5, 3)) >= 0) then begin | |
| 1097 | - if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) then Bad; | |
| 1098 | - RM(pfx, wide, $400F + Cond(copy(w, 5, 3)) shl 8, o[0].r, o[1]); | |
| 1099 | - end else if (length(w) > 2) and (WordIndex(copy(w, 1, length(w) - 2), SseOps) >= 0) | |
| 1100 | - and ((copy(w, length(w) - 1, 2) = 'sd') or (copy(w, length(w) - 1, 2) = 'ss')) then begin | |
| 1101 | - { sqrtsd, addss and the like: xmm, xmm or memory } | |
| 1102 | - if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad; | |
| 1103 | - k := $0F or SseCodes[WordIndex(copy(w, 1, length(w) - 2), SseOps)] shl 8; | |
| 1104 | - RM(RealPfx[w[length(w)] = 's'], false, k, o[0].r, o[1]); | |
| 1105 | - end else if WordIndex(w, PackedOps) >= 0 then begin | |
| 1106 | - if (n < 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad; | |
| 1107 | - k := WordIndex(w, PackedOps); | |
| 1108 | - RM($66, false, $0F or PackedCodes[k] shl 8, o[0].r, o[1]); | |
| 1109 | - if w = 'pshufd' then begin | |
| 1110 | - if (n <> 3) or (o[2].kind <> oImm) then Bad; | |
| 1111 | - Put1(o[2].v); | |
| 1112 | - end else if n <> 2 then Bad; | |
| 1113 | - end else if (w = 'movdqu') or (w = 'movdqa') then begin | |
| 1114 | - { the load is 6F and the store 7F; movdqu is F3, movdqa 66 } | |
| 1115 | - if w = 'movdqu' then k := $F3 else k := $66; | |
| 1116 | - if (n = 2) and (o[0].kind = oXmm) and (o[1].kind in [oXmm, oMem]) then RM(k, false, $6F0F, o[0].r, o[1]) | |
| 1117 | - else if (n = 2) and (o[0].kind = oMem) and (o[1].kind = oXmm) then RM(k, false, $7F0F, o[1].r, o[0]) | |
| 1118 | - else Bad; | |
| 1119 | - end else if w = 'pmovmskb' then begin | |
| 1120 | - if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind <> oXmm) then Bad; | |
| 1121 | - RM($66, false, $D70F, o[0].r, o[1]); | |
| 1122 | - end else if WordIndex(w, BitOps) >= 0 then begin | |
| 1123 | - if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) or (size < 4) then Bad; | |
| 1124 | - k := WordIndex(w, BitOps); | |
| 1125 | - RM(BitPfx[k], wide, BitCodes[k], o[0].r, o[1]); | |
| 1126 | - end else if (w = 'movsd') or (w = 'movss') then begin | |
| 1127 | - if w = 'movsd' then k := $F2 else k := $F3; | |
| 1128 | - if (n = 2) and (o[0].kind = oXmm) and (o[1].kind in [oXmm, oMem]) then RM(k, false, $100F, o[0].r, o[1]) | |
| 1129 | - else if (n = 2) and (o[0].kind = oMem) and (o[1].kind = oXmm) then RM(k, false, $110F, o[1].r, o[0]) | |
| 1130 | - else Bad; | |
| 1131 | - end else if (w = 'movq') or (w = 'movd') then begin | |
| 1132 | - if (n = 2) and (o[0].kind = oXmm) and (o[1].kind in [oReg, oMem]) then RM($66, w = 'movq', $6E0F, o[0].r, o[1]) | |
| 1133 | - else if (n = 2) and (o[1].kind = oXmm) and (o[0].kind in [oReg, oMem]) then RM($66, w = 'movq', $7E0F, o[1].r, o[0]) | |
| 1134 | - else Bad; | |
| 1135 | - end else if (w = 'ucomisd') or (w = 'ucomiss') then begin | |
| 1136 | - if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad; | |
| 1137 | - if w = 'ucomisd' then RM($66, false, $2E0F, o[0].r, o[1]) else RM(0, false, $2E0F, o[0].r, o[1]); | |
| 1138 | - end else if (w = 'cvtsi2sd') or (w = 'cvtsi2ss') then begin | |
| 1139 | - if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oReg, oMem]) then Bad; | |
| 1140 | - RM(RealPfx[w = 'cvtsi2ss'], o[1].size <> 4, $2A0F, o[0].r, o[1]); | |
| 1141 | - end else if (w = 'cvttsd2si') or (w = 'cvttss2si') then begin | |
| 1142 | - if (n <> 2) or (o[0].kind <> oReg) or not (o[1].kind in [oXmm, oMem]) then Bad; | |
| 1143 | - RM(RealPfx[w = 'cvttss2si'], wide, $2C0F, o[0].r, o[1]); | |
| 1144 | - end else if (w = 'cvtsd2ss') or (w = 'cvtss2sd') then begin | |
| 1145 | - if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad; | |
| 1146 | - RM(RealPfx[w = 'cvtss2sd'], false, $5A0F, o[0].r, o[1]); | |
| 1147 | - end else begin | |
| 1148 | - k := 0; | |
| 1149 | - while (k <= High(Plain)) and (Plain[k] <> w) do inc(k); | |
| 1150 | - if (k > High(Plain)) or (n <> 0) then MarkAt(at, 'unknown instruction ' + w); | |
| 1151 | - Opcode(PlainOp[k]); | |
| 1152 | - end; | |
| 1153 | - end; | |
| 1154 | - if tok <> sSemicolon then Mark('expected ";"'); | |
| 1155 | - listing := wasListing; | |
| 1156 | - SayAt(first, Source(SpanFrom(at))); | |
| 1157 | - Next; | |
| 1158 | - end; | |
| 1159 | - for k := 0 to numLabels - 1 do | |
| 1160 | - if places[k] < 0 then MarkAt(usedAt[k], 'label ' + NameOf(names[k]) + ' isn''t defined'); | |
| 1161 | -end; | |
| 1162 | - | |
| 1163 | -{ The listing, raz -S: each live procedure of the module in the order | |
| 1164 | - it was made, each line of source it comes from, and under it the | |
| 1165 | - instructions made for it, at their offsets in the procedure; a jump | |
| 1166 | - within it says where to by its offset too. } | |
| 1167 | -procedure List(module: longint); | |
| 1168 | -var order: ^longint; n, i, j, k, c, t, line: longint; text: shortstring; | |
| 1169 | -begin | |
| 1170 | - order := GetMem(numChunks * SizeOf(longint)); | |
| 1171 | - n := 0; | |
| 1172 | - for c := 0 to numChunks - 1 do | |
| 1173 | - if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module = module) then begin | |
| 1174 | - i := n; | |
| 1175 | - while (i > 0) and (chunks^[order[i - 1]].start > chunks^[c].start) do begin | |
| 1176 | - order[i] := order[i - 1]; | |
| 1177 | - dec(i); | |
| 1178 | - end; | |
| 1179 | - order[i] := c; | |
| 1180 | - inc(n); | |
| 1181 | - end; | |
| 1182 | - i := 0; j := 0; | |
| 1183 | - for k := 0 to n - 1 do begin | |
| 1184 | - c := order[k]; | |
| 1185 | - writeln; | |
| 1186 | - writeln(ChunkName(c), ':'); | |
| 1187 | - line := -1; | |
| 1188 | - while (i < numSaid) and (said[i].at < chunks^[c].start) do inc(i); | |
| 1189 | - while (i < numSaid) and (said[i].at < chunks^[c].start + chunks^[c].size) do begin | |
| 1190 | - while (j < numLines) and (lines^[j].at <= said[i].at) do begin | |
| 1191 | - if (lines^[j].chunk = c) and (lines^[j].line <> line) then begin | |
| 1192 | - line := lines^[j].line; | |
| 1193 | - writeln(line: 6, ' ', LineText(NameOf(lines^[j].path), line)); | |
| 1194 | - end; | |
| 1195 | - inc(j); | |
| 1196 | - end; | |
| 1197 | - text := said[i].text; | |
| 1198 | - t := said[i].at; | |
| 1199 | - if (text = 'jmp') or (text = 'call') then text := text + ' ' + IntStr(t + 5 + Get4At(t + 1) - chunks^[c].start) | |
| 1200 | - else if (text[1] = 'j') and (pos(' ', text) = 0) then text := text + ' ' + IntStr(t + 6 + Get4At(t + 2) - chunks^[c].start); | |
| 1201 | - writeln(t - chunks^[c].start: 14, ' ', text); | |
| 1202 | - inc(i); | |
| 1203 | - end; | |
| 1204 | - end; | |
| 1205 | - FreeMem(order); | |
| 1206 | -end; | |
| 1207 | - | |
| 1208 | -end. |
| @@ -1,35 +1,25 @@ | ||
| 1 | 1 | #!/bin/sh |
| 2 | -# The scanner's speed against the seed's, on the compiler's own | |
| 3 | -# benchmark program: the best of seven runs of each. Run from the | |
| 4 | -# repository root, after make raz. | |
| 2 | +# The compiler's speed against the previous release's, the one BOOT | |
| 3 | +# names (build/boot/raz), on the same generated program: the best of | |
| 4 | +# five runs of each, as build/cmp/raz, which finds the library beside | |
| 5 | +# it. A regression test: it fails if this compiler takes more than a | |
| 6 | +# tenth longer. Run from the repository root, after make raz. | |
| 5 | 7 | set -e |
| 6 | -b=build/src | |
| 7 | -mkdir -p $b | |
| 8 | -python3 test/genbench.py 12000 >$b/Bench.raz | |
| 9 | -fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/scanbench.seed src/test/ScanBench.pas >/dev/null | |
| 10 | -./raz -Werror -I src -o $b/scanbench.src src/test/ScanBench.raz | |
| 11 | -echo "$(wc -l <$b/Bench.raz) lines" | |
| 12 | -for t in seed src; do | |
| 13 | - best=0 | |
| 14 | - for i in 1 2 3 4 5 6 7; do | |
| 15 | - s=$(date +%s%N); $b/scanbench.$t $b/Bench.raz >/dev/null; e=$(date +%s%N) | |
| 16 | - d=$(( (e - s) / 1000000 )); if [ $best = 0 ] || [ $d -lt $best ]; then best=$d; fi | |
| 17 | - done | |
| 18 | - echo "$t: $best ms" | |
| 19 | -done | |
| 20 | - | |
| 21 | -# The whole compiler's speed, the seed's and this one's, on the same | |
| 22 | -# program: the best of five runs of each, as build/cmp/raz, which finds | |
| 23 | -# the library beside it. | |
| 24 | 8 | mkdir -p build/cmp/bench |
| 25 | 9 | ln -sfn ../../lib build/cmp/lib |
| 26 | -cp $b/Bench.raz build/cmp/bench/Bench.raz | |
| 27 | -for t in seed src; do | |
| 28 | - if [ $t = seed ]; then cp build/seed/raz build/cmp/raz; else cp $b/raz.src build/cmp/raz; fi | |
| 10 | +python3 test/genbench.py 12000 >build/cmp/bench/Bench.raz | |
| 11 | +echo "$(wc -l <build/cmp/bench/Bench.raz) lines" | |
| 12 | +for t in prev this; do | |
| 13 | + if [ $t = prev ]; then cp build/boot/raz build/cmp/raz; else cp raz build/cmp/raz; fi | |
| 29 | 14 | best=0 |
| 30 | 15 | for i in 1 2 3 4 5; do |
| 31 | 16 | s=$(date +%s%N); build/cmp/raz -o build/cmp/bench/Bench.$t build/cmp/bench/Bench.raz; e=$(date +%s%N) |
| 32 | 17 | d=$(( (e - s) / 1000000 )); if [ $best = 0 ] || [ $d -lt $best ]; then best=$d; fi |
| 33 | 18 | done |
| 34 | - echo "compile, $t: $best ms" | |
| 19 | + echo "$t: $best ms" | |
| 20 | + eval "ms_$t=$best" | |
| 35 | 21 | done |
| 22 | +if [ $((ms_this * 10)) -gt $((ms_prev * 11)) ]; then | |
| 23 | + echo "slower than the previous release, $(cut -d" " -f1 BOOT), by more than a tenth" | |
| 24 | + exit 1 | |
| 25 | +fi |
| @@ -0,0 +1,37 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +# The compiler that builds src/: the release BOOT names, by its tag and | |
| 3 | +# its tarball's SHA-256. A release's tarball is the same, byte for byte, | |
| 4 | +# wherever it is made (the Makefile's dist), so the sum pins the very | |
| 5 | +# compiler; and since that release was built by the one before it, back | |
| 6 | +# to v0.19.0, which the Pascal seed built (git has it at that tag), | |
| 7 | +# raz can always be built again from source alone. | |
| 8 | +# | |
| 9 | +# The tarball is taken from build/boot/ if it is there; else from the | |
| 10 | +# GitHub release, by curl, or by gh's REST API where the repository is | |
| 11 | +# private and gh has a token; a machine with no network has it put | |
| 12 | +# there by hand. Its raz goes to build/boot/raz, with this repository's | |
| 13 | +# lib/ beside it, since src/ is built against today's library. Run from | |
| 14 | +# the repository root, by make. | |
| 15 | +set -e | |
| 16 | +read tag sum < BOOT | |
| 17 | +v=${tag#v} | |
| 18 | +t=raz-$v-linux-x86_64.tar.gz | |
| 19 | +d=build/boot | |
| 20 | +repo=ldiaz94/raz | |
| 21 | +mkdir -p $d | |
| 22 | +if [ ! -f $d/$t ]; then | |
| 23 | + if ! curl -fsSL -o $d/$t https://github.com/$repo/releases/download/$tag/$t 2>/dev/null; then | |
| 24 | + id=$(gh api repos/$repo/releases/tags/$tag --jq ".assets[] | select(.name == \"$t\") | .id" 2>/dev/null) && | |
| 25 | + [ -n "$id" ] && | |
| 26 | + gh api -H 'Accept: application/octet-stream' repos/$repo/releases/assets/$id >$d/$t 2>/dev/null || | |
| 27 | + { rm -f $d/$t; echo "boot: can't fetch $t; put it in $d" >&2; exit 1; } | |
| 28 | + fi | |
| 29 | +fi | |
| 30 | +if ! echo "$sum $d/$t" | sha256sum -c --quiet - >/dev/null 2>&1; then | |
| 31 | + echo "boot: $d/$t isn't $tag's tarball: its SHA-256 isn't BOOT's" >&2 | |
| 32 | + exit 1 | |
| 33 | +fi | |
| 34 | +rm -rf $d/raz-$v-linux-x86_64 | |
| 35 | +tar -xzf $d/$t -C $d | |
| 36 | +cp $d/raz-$v-linux-x86_64/raz $d/raz | |
| 37 | +ln -sfn ../../lib $d/lib |
| @@ -1,27 +1,28 @@ | ||
| 1 | 1 | #!/bin/sh |
| 2 | -# The fixpoint: the seed builds src/ (stage 1), that builds src/ | |
| 3 | -# (stage 2), that builds it again (stage 3), and stages 2 and 3 must | |
| 4 | -# be the same file. A program holds the paths it was made from, so each | |
| 2 | +# The fixpoint: the previous release, BOOT's, builds src/ (stage 1), | |
| 3 | +# that builds src/ (stage 2), that builds it again (stage 3), and | |
| 4 | +# stages 2 and 3 must be the same file. A program holds the paths it was made from, so each | |
| 5 | 5 | # stage is built by the same command in the same place, from the |
| 6 | 6 | # compiler before it copied to build/fix/raz. Run from the repository |
| 7 | 7 | # root, after `make raz`. Then stage 3 stands in for ./raz (messages name |
| 8 | 8 | # the library by its path from the compiler, so it must be at the root) |
| 9 | -# while `make check` and `make bench` run on it; the seed's comes back. | |
| 9 | +# while `make check` and `make bench` run on it; the one make built | |
| 10 | +# comes back. | |
| 10 | 11 | set -e |
| 11 | 12 | d=build/fix |
| 12 | 13 | rm -rf $d |
| 13 | 14 | mkdir -p $d/v |
| 14 | 15 | ln -s ../../lib $d/lib |
| 15 | 16 | printf 'module Version;\nconst Text* = "%s";\nend Version.\n' "$(tr -d "'" < build/version.inc)" >$d/v/Version.raz |
| 16 | -cp build/seed/raz $d/stage0 | |
| 17 | +cp build/boot/raz $d/stage0 | |
| 17 | 18 | for n in 1 2 3; do |
| 18 | 19 | cp $d/stage$((n - 1)) $d/raz |
| 19 | 20 | $d/raz -Werror -I src -I $d/v -o $d/stage$n src/Raz.raz |
| 20 | 21 | done |
| 21 | 22 | cmp $d/stage2 $d/stage3 |
| 22 | 23 | echo "fixpoint: stages 2 and 3 are the same, $(wc -c <$d/stage3) bytes" |
| 23 | -cp raz $d/seed | |
| 24 | -trap 'cp $d/seed raz' EXIT | |
| 24 | +cp raz $d/made | |
| 25 | +trap 'cp $d/made raz' EXIT | |
| 25 | 26 | cp $d/stage3 raz |
| 26 | 27 | make check |
| 27 | 28 | make bench |
| @@ -1,12 +0,0 @@ | ||
| 1 | -{ Counts the symbols of a file and nothing else, for timing scan.pas | |
| 2 | - against src/Scan.raz (ScanBench.raz). } | |
| 3 | -program scanbench; | |
| 4 | -{$mode objfpc}{$H-} | |
| 5 | -uses diag, scan; | |
| 6 | -var n: longint; | |
| 7 | -begin | |
| 8 | - if Open(ParamStr(1)) <> '' then Fatal('no file'); | |
| 9 | - n := 0; | |
| 10 | - repeat Next; inc(n) until tok = sEof; | |
| 11 | - writeln(n, ' symbols'); | |
| 12 | -end. |
| @@ -1,15 +0,0 @@ | ||
| 1 | -(* Counts the symbols of a file and nothing else, for timing Scan.raz | |
| 2 | - against the seed's scan.pas (ScanBench.pas). *) | |
| 3 | -module ScanBench; | |
| 4 | -import Out, Files, Diag, Scan; | |
| 5 | - | |
| 6 | -proc main*; | |
| 7 | -var n: INT; | |
| 8 | -begin | |
| 9 | - if LEN(Scan.open(Files.arg(1))) # 0 then Diag.fatal("no file"); end; | |
| 10 | - n := 0; | |
| 11 | - repeat Scan.next; INC(n); until Scan.tok = Scan.sEof; | |
| 12 | - Out.int(n); Out.str(" symbols\n"); | |
| 13 | -end; | |
| 14 | - | |
| 15 | -end ScanBench. |
| @@ -1,37 +0,0 @@ | ||
| 1 | -{ Emits one of each instruction form into a chunk, for objdump: a test | |
| 2 | - of the encoder in x64.pas. } | |
| 3 | -program enctest; | |
| 4 | -{$mode objfpc}{$H-} | |
| 5 | -uses scan, chunk, x64, link, elf; | |
| 6 | -var e, d: longint; L: longint; | |
| 7 | -begin | |
| 8 | - d := NewChunk(secBss, -1, Intern('data', 4)); SetBss(d, 8, 8); | |
| 9 | - e := NewChunk(secText, -1, Intern('start', 5)); | |
| 10 | - BeginChunk(e); | |
| 11 | - MovRR(RAX, RCX); MovRR(R11, RDI); MovRR(RSI, R9); | |
| 12 | - MovRI(RAX, 0); MovRI(RCX, 5); MovRI(RDX, -5); MovRI(R10, $FFFFFFFF); MovRI(RAX, $123456789A); | |
| 13 | - Load(RAX, Mem(RBP, -8), 8, true); Load(RAX, Mem(RBP, -200), 4, true); Load(RCX, Mem(RAX, 0), 4, false); | |
| 14 | - Load(RAX, Mem(RCX, 16), 2, true); Load(RAX, Mem(RAX, 3), 2, false); Load(RAX, Mem(RSP, 8), 1, true); | |
| 15 | - Load(RAX, Mem(RBP, 0), 1, false); Load(R10, Mem(12, 0), 8, true); Load(RAX, Mem(13, 0), 8, true); | |
| 16 | - Store(Mem(RBP, -16), RAX, 8); Store(Mem(RBP, -16), RSI, 1); Store(Mem(RCX, 0), RAX, 2); Store(Mem(RCX, 4), RDX, 4); | |
| 17 | - Store(MemIdx(RBP, R10, 8, 0), RAX, 8); Store(MemRip(d, 0), RAX, 8); Lea(RAX, MemRip(d, 4)); | |
| 18 | - Alu(aluAdd, RAX, RCX); Alu(aluSub, RCX, RAX); Alu(aluCmp, RAX, RCX); Alu(aluXor, R8, R9); | |
| 19 | - AluI(aluAdd, RAX, 1); AluI(aluAnd, RAX, 1000); AluI(aluCmp, RSP, 8); AluM(aluCmp, RAX, Mem(RCX, 8)); AluM(aluAdd, RAX, Mem(RCX, 0)); | |
| 20 | - Imul(RAX, RCX); ImulI(RAX, 24); Shift(shShl, RAX); Shift(shSar, RAX); ShiftI(shShr, RCX, 3); | |
| 21 | - Unary(2, RAX); Unary(3, RAX); Unary(6, RCX); Unary(7, RCX); Cqo; | |
| 22 | - Extend(RAX, 1, true); Extend(RAX, 1, false); Extend(RDI, 1, false); Extend(RSI, 1, true); Extend(RAX, 2, true); Extend(RAX, 2, false); Extend(RAX, 4, true); Extend(RAX, 4, false); | |
| 23 | - SetCC(ccL, RAX); SetCC(ccA, RCX); SetCC(ccE, RDI); TestRR(RAX, RAX); | |
| 24 | - Push(RAX); Push(12); Pop(RCX); Pop(R11); CallR(R11); CallM(Mem(RCX, 8)); Ret; Leave; Syscall; Mfence; Ud2; RepMovsb; RepStosb; | |
| 25 | - LockCmpxchg(Mem(RCX, 0), RDX); Xchg(Mem(RCX, 0), RAX); LockXadd(Mem(RCX, 0), RAX); | |
| 26 | - MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsXM(2, Mem(RBP, -8), false); MovsMX(Mem(RBP, -8), 7, false); | |
| 27 | - MovX(9, 12); MovX(0, 8); MovsXM(8, MemIdx(R13, R9, 8, -8), true); MovsMX(Mem(RBP, -8), 11, true); | |
| 28 | - SseM($58, 9, MemIdx(R13, R10, 8, 4096), false); SseM($59, 0, Mem(RBP, -16), true); Sse($5C, 8, 11, false); | |
| 29 | - Sse($58, 0, 1, false); Sse($5E, 0, 1, true); Sse($5A, 0, 0, false); Sse($5A, 0, 0, true); | |
| 30 | - Ucomis(0, 1, false); Ucomis(0, 1, true); Cvtsi2s(0, RAX, false); Cvtsi2s(0, RAX, true); | |
| 31 | - Cvtts2si(RAX, 0, false); Cvtts2si(RAX, 0, true); Btc(RAX, 63); | |
| 32 | - L := NoLabel; Jcc(ccE, L); Jmp(L); FixLink(L); JmpBack(chunks^[e].start); JccBack(ccNE, chunks^[e].start); | |
| 33 | - Ret; Align(32); | |
| 34 | - EndChunk(e); | |
| 35 | - MarkLive(e); | |
| 36 | - WriteElf(ParamStr(1), e); | |
| 37 | -end. |
| @@ -1,46 +0,0 @@ | ||
| 1 | -{ Builds a program by hand, chunk by chunk, links it and writes it: | |
| 2 | - a test of x64.pas, link.pas and elf.pas. The program writes "hand" | |
| 3 | - from rodata, adds one to a bss variable through a call, and exits | |
| 4 | - with that variable plus 41: status 42. A chunk nothing reaches is | |
| 5 | - left out. } | |
| 6 | -program handtest; | |
| 7 | -{$mode objfpc}{$H-} | |
| 8 | -uses scan, chunk, x64, link, elf; | |
| 9 | -var entry, msg, count, bump, unused: longint; s: shortstring; | |
| 10 | -begin | |
| 11 | - s := 'hand'#10; | |
| 12 | - msg := NewChunk(secRodata, -1, Intern('msg', 3)); | |
| 13 | - SetData(msg, @s[1], length(s), 1); | |
| 14 | - count := NewChunk(secBss, -1, Intern('count', 5)); | |
| 15 | - SetBss(count, 8, 8); | |
| 16 | - bump := NewChunk(secText, -1, Intern('bump', 4)); | |
| 17 | - unused := NewChunk(secText, -1, Intern('unused', 6)); | |
| 18 | - entry := NewChunk(secText, -1, Intern('start', 5)); | |
| 19 | - | |
| 20 | - BeginChunk(bump); { count := count + 1 } | |
| 21 | - Load(RAX, MemRip(count, 0), 8, true); | |
| 22 | - AluI(aluAdd, RAX, 1); | |
| 23 | - Store(MemRip(count, 0), RAX, 8); | |
| 24 | - Ret; | |
| 25 | - EndChunk(bump); | |
| 26 | - | |
| 27 | - BeginChunk(unused); | |
| 28 | - Ud2; | |
| 29 | - EndChunk(unused); | |
| 30 | - | |
| 31 | - BeginChunk(entry); | |
| 32 | - MovRI(RAX, 1); { write(1, msg, 5) } | |
| 33 | - MovRI(RDI, 1); | |
| 34 | - Lea(RSI, MemRip(msg, 0)); | |
| 35 | - MovRI(RDX, length(s)); | |
| 36 | - Syscall; | |
| 37 | - CallChunk(bump); | |
| 38 | - Load(RDI, MemRip(count, 0), 8, true); | |
| 39 | - AluI(aluAdd, RDI, 41); | |
| 40 | - MovRI(RAX, 60); { exit(count + 41) } | |
| 41 | - Syscall; | |
| 42 | - EndChunk(entry); | |
| 43 | - | |
| 44 | - MarkLive(entry); | |
| 45 | - WriteElf(ParamStr(1), entry); | |
| 46 | -end. |
| @@ -1,19 +0,0 @@ | ||
| 1 | -{ Prints the symbols of a file, one per line: a test of scan.pas. } | |
| 2 | -program scantest; | |
| 3 | -{$mode objfpc}{$H-} | |
| 4 | -uses diag, scan; | |
| 5 | -var i: longint; | |
| 6 | -begin | |
| 7 | - if Open(ParamStr(1)) <> '' then Fatal('no file'); | |
| 8 | - repeat | |
| 9 | - Next; | |
| 10 | - write(LineOf(TokPos), ':', TokPos.a, ' ', ord(tok)); | |
| 11 | - case tok of | |
| 12 | - sIdent: write(' ', NameOf(id)); | |
| 13 | - sInt, sChar: write(' ', ival); | |
| 14 | - sReal: write(' ', rval:0:6); | |
| 15 | - sString: begin write(' "'); for i := 0 to slen - 1 do write(sbuf[i], ','); write('"') end; | |
| 16 | - end; | |
| 17 | - writeln; | |
| 18 | - until tok = sEof; | |
| 19 | -end. |