Self step 2b: Link, Elf and X64 in raz, tested against the seed's self/Link.raz, self/Elf.raz and self/X64.raz (encoder, assembler, listing). Link and Elf write the same executable from the same chunks, static, dynamic and stripped; X64's encoders give the same bytes and listing on a script, and its assembler the same bytes, listing and messages on a long run of every instruction and a file for each of its errors (test/genasm.py). Scan.regrab is shared by them. README lists what the port found. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | 1. `diag` and `scan` (done): the trial. Measure `scan`'s speed against the |
| 32 | 32 | seed's and list what `Sys` and the library lacked. Stop for review: |
| 33 | 33 | if the cost is too high, mend the code generator first. |
| 34 | -2. `chunk`, `sym` (2a, done); `link`, `elf`, `x64` (2b). | |
| 34 | +2. `chunk`, `sym` (2a, done); `link`, `elf`, `x64` (2b, done). | |
| 35 | 35 | 3. `gen`, `item`. |
| 36 | 36 | 4. `parse`, `raz`: the first compiler that builds. |
| 37 | 37 | 5. Fixpoint: the seed builds `self`, that builds `self`, that builds |
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | Exported variables can't be set from outside, so the setters at the |
| 18 | 18 | end do what the seed's other units do to these. *) |
| 19 | 19 | module Chunk; |
| 20 | -import Mem, Heap, Fmt, Diag, Scan; | |
| 20 | +import Fmt, Diag, Scan; | |
| 21 | 21 | |
| 22 | 22 | type |
| 23 | 23 | Section* = (secText, secRodata, secBss); |
| @@ -27,7 +27,7 @@ | ||
| 27 | 27 | start*, size*, align*: INT; (* in its section's buffer *) |
| 28 | 28 | firstFix*, numFix*: INT; (* its fixups: text's in fixups, rodata's in dataFixups *) |
| 29 | 29 | home*, name*: INT; (* its module and its name, for the symbol table; -1 if none *) |
| 30 | - adr*: I64; (* its address, set by the linker *) | |
| 30 | + adr*: INT; (* its address, set by the linker *) | |
| 31 | 31 | live*: BOOL; (* reached from the entry point *) |
| 32 | 32 | ext*: BOOL; (* bss: the address of a C symbol, its name *) |
| 33 | 33 | reals*: BOOL; (* code: computes with reals, first at realsAt *) |
| @@ -81,21 +81,11 @@ | ||
| 81 | 81 | |
| 82 | 82 | codeCap, roCap, chunkCap, fixCap, dataFixCap, lineCap, noteCap: INT; |
| 83 | 83 | |
| 84 | -(* p, which holds used bytes, moved to a block of size bytes *) | |
| 85 | -proc grown(p: ^U8; used, size: INT): ^U8; | |
| 86 | -var q: ^U8; | |
| 87 | -begin | |
| 88 | - q := Scan.grab(size); | |
| 89 | - COPY(q[0..used], p[0..used]); | |
| 90 | - Mem.free(Heap.heap, p); | |
| 91 | - return q; | |
| 92 | -end; | |
| 93 | - | |
| 94 | 84 | (* Room for n more bytes of code *) |
| 95 | 85 | proc room(n: INT); |
| 96 | 86 | begin |
| 97 | 87 | if pc + n > codeCap then |
| 98 | - text := grown(text, pc, codeCap * 2 + n); | |
| 88 | + text := Scan.regrab(text, pc, codeCap * 2 + n); | |
| 99 | 89 | codeCap := codeCap * 2 + n; |
| 100 | 90 | end; |
| 101 | 91 | end; |
| @@ -157,7 +147,7 @@ | ||
| 157 | 147 | var c: ^Chunk; |
| 158 | 148 | begin |
| 159 | 149 | if numChunks = chunkCap then |
| 160 | - chunks := ^Chunk(grown(^U8(chunks), numChunks * SIZE(Chunk), chunkCap * 2 * SIZE(Chunk))); | |
| 150 | + chunks := ^Chunk(Scan.regrab(^U8(chunks), numChunks * SIZE(Chunk), chunkCap * 2 * SIZE(Chunk))); | |
| 161 | 151 | chunkCap := chunkCap * 2; |
| 162 | 152 | end; |
| 163 | 153 | c := ADR(chunks[numChunks]); |
| @@ -194,7 +184,7 @@ | ||
| 194 | 184 | proc setData*(c: INT; p: ^U8; n, align: INT); |
| 195 | 185 | begin |
| 196 | 186 | if rolen + n > roCap then |
| 197 | - rodata := grown(rodata, rolen, roCap * 2 + n); | |
| 187 | + rodata := Scan.regrab(rodata, rolen, roCap * 2 + n); | |
| 198 | 188 | roCap := roCap * 2 + n; |
| 199 | 189 | end; |
| 200 | 190 | COPY(rodata[rolen..rolen + n], p[0..n]); |
| @@ -222,7 +212,7 @@ | ||
| 222 | 212 | return; |
| 223 | 213 | end; |
| 224 | 214 | if numLines = lineCap then |
| 225 | - lines := ^Line(grown(^U8(lines), numLines * SIZE(Line), lineCap * 2 * SIZE(Line))); | |
| 215 | + lines := ^Line(Scan.regrab(^U8(lines), numLines * SIZE(Line), lineCap * 2 * SIZE(Line))); | |
| 226 | 216 | lineCap := lineCap * 2; |
| 227 | 217 | end; |
| 228 | 218 | lines[numLines].at := pc; |
| @@ -250,7 +240,7 @@ | ||
| 250 | 240 | proc addFixup*(at, chunk, addend: INT); |
| 251 | 241 | begin |
| 252 | 242 | if numFixups = fixCap then |
| 253 | - fixups := ^Fixup(grown(^U8(fixups), numFixups * SIZE(Fixup), fixCap * 2 * SIZE(Fixup))); | |
| 243 | + fixups := ^Fixup(Scan.regrab(^U8(fixups), numFixups * SIZE(Fixup), fixCap * 2 * SIZE(Fixup))); | |
| 254 | 244 | fixCap := fixCap * 2; |
| 255 | 245 | end; |
| 256 | 246 | fixups[numFixups].at := at; |
| @@ -264,7 +254,7 @@ | ||
| 264 | 254 | proc addDataFixup*(c, at, chunk, addend: INT); |
| 265 | 255 | begin |
| 266 | 256 | if numDataFixups = dataFixCap then |
| 267 | - dataFixups := ^Fixup(grown(^U8(dataFixups), numDataFixups * SIZE(Fixup), dataFixCap * 2 * SIZE(Fixup))); | |
| 257 | + dataFixups := ^Fixup(Scan.regrab(^U8(dataFixups), numDataFixups * SIZE(Fixup), dataFixCap * 2 * SIZE(Fixup))); | |
| 268 | 258 | dataFixCap := dataFixCap * 2; |
| 269 | 259 | end; |
| 270 | 260 | dataFixups[numDataFixups].at := at; |
| @@ -277,7 +267,7 @@ | ||
| 277 | 267 | proc noteVar*(chunk, name, off, kind: INT); |
| 278 | 268 | begin |
| 279 | 269 | if numVarNotes = noteCap then |
| 280 | - varNotes := ^VarNote(grown(^U8(varNotes), numVarNotes * SIZE(VarNote), noteCap * 2 * SIZE(VarNote))); | |
| 270 | + varNotes := ^VarNote(Scan.regrab(^U8(varNotes), numVarNotes * SIZE(VarNote), noteCap * 2 * SIZE(VarNote))); | |
| 281 | 271 | noteCap := noteCap * 2; |
| 282 | 272 | end; |
| 283 | 273 | varNotes[numVarNotes].chunk := chunk; |
| @@ -321,7 +311,7 @@ | ||
| 321 | 311 | chunks[c].live := true; |
| 322 | 312 | end; |
| 323 | 313 | |
| 324 | -proc setAdr*(c: INT; a: I64); | |
| 314 | +proc setAdr*(c: INT; a: INT); | |
| 325 | 315 | begin |
| 326 | 316 | chunks[c].adr := a; |
| 327 | 317 | end; |
| @@ -0,0 +1,431 @@ | ||
| 1 | +(* Elf: 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 memory, Diag for errors, Sys and Files to write | |
| 18 | + the file. | |
| 19 | + Leaves out: position independent executables, symbol versions, the | |
| 20 | + rest of DWARF. | |
| 21 | + | |
| 22 | + Exported variables can't be set from outside, so needLib, setMainPath | |
| 23 | + and setStripped do it. *) | |
| 24 | +module Elf; | |
| 25 | +import Sys, Files, Fmt, Diag, Scan, Chunk, Link; | |
| 26 | + | |
| 27 | +var | |
| 28 | + needed: [32]INT; (* the libraries to load, names interned *) | |
| 29 | + numNeeded*: INT; (* none: a static program *) | |
| 30 | + mainPath: []U8; (* the main module's file, for gdb *) | |
| 31 | + stripped*: BOOL; (* -s: no symbols and no line numbers; the sections are there, empty *) | |
| 32 | + | |
| 33 | + buf: ^U8; | |
| 34 | + len, cap: INT; | |
| 35 | + | |
| 36 | +(* The library name, to be loaded, unless it is already: false if there | |
| 37 | + are too many *) | |
| 38 | +proc needLib*(name: INT): BOOL; | |
| 39 | +var i: INT; | |
| 40 | +begin | |
| 41 | + for i := 0 to numNeeded - 1 do | |
| 42 | + if needed[i] = name then return true; end; | |
| 43 | + end; | |
| 44 | + if numNeeded = 32 then return false; end; | |
| 45 | + needed[numNeeded] := name; | |
| 46 | + INC(numNeeded); | |
| 47 | + return true; | |
| 48 | +end; | |
| 49 | + | |
| 50 | +(* A static program again *) | |
| 51 | +proc noLibs*; | |
| 52 | +begin | |
| 53 | + numNeeded := 0; | |
| 54 | +end; | |
| 55 | + | |
| 56 | +proc setMainPath*(path: []U8); | |
| 57 | +begin | |
| 58 | + mainPath := path; | |
| 59 | +end; | |
| 60 | + | |
| 61 | +proc setStripped*(yes: BOOL); | |
| 62 | +begin | |
| 63 | + stripped := yes; | |
| 64 | +end; | |
| 65 | + | |
| 66 | +proc room(n: INT); | |
| 67 | +begin | |
| 68 | + if len + n > cap then | |
| 69 | + buf := Scan.regrab(buf, len, cap * 2 + n); | |
| 70 | + cap := cap * 2 + n; | |
| 71 | + end; | |
| 72 | +end; | |
| 73 | + | |
| 74 | +(* The n low bytes of v, least significant first *) | |
| 75 | +proc putN(v: INT; n: INT); | |
| 76 | +var i: INT; | |
| 77 | +begin | |
| 78 | + room(n); | |
| 79 | + for i := 0 to n - 1 do | |
| 80 | + buf[len] := U8(v & 0FFH); | |
| 81 | + v := v shr 8; | |
| 82 | + INC(len); | |
| 83 | + end; | |
| 84 | +end; | |
| 85 | + | |
| 86 | +proc putBytes(p: ^U8; n: INT); | |
| 87 | +begin | |
| 88 | + room(n); | |
| 89 | + if n > 0 then COPY(buf[len..len + n], p[0..n]); end; | |
| 90 | + len := len + n; | |
| 91 | +end; | |
| 92 | + | |
| 93 | +proc padTo(off: INT); | |
| 94 | +begin | |
| 95 | + while len < off do putN(0, 1); end; | |
| 96 | +end; | |
| 97 | + | |
| 98 | +proc patch(at: INT; v: INT; n: INT); | |
| 99 | +var i: INT; | |
| 100 | +begin | |
| 101 | + for i := 0 to n - 1 do | |
| 102 | + buf[at + i] := U8(v & 0FFH); | |
| 103 | + v := v shr 8; | |
| 104 | + end; | |
| 105 | +end; | |
| 106 | + | |
| 107 | +proc phdr(typ, flags: INT; off, adr, filesz, memsz, align: INT); | |
| 108 | +begin | |
| 109 | + putN(typ, 4); | |
| 110 | + putN(flags, 4); | |
| 111 | + putN(off, 8); | |
| 112 | + putN(adr, 8); | |
| 113 | + putN(adr, 8); | |
| 114 | + putN(filesz, 8); | |
| 115 | + putN(memsz, 8); | |
| 116 | + putN(align, 8); | |
| 117 | +end; | |
| 118 | + | |
| 119 | +const | |
| 120 | + (* the section names, in .shstrtab; a section header names its own *) | |
| 121 | + ShNames = "\0.text\0.rodata\0.bss\0.symtab\0.strtab\0.shstrtab\0.dynamic\0.dynstr\0.dynsym\0.debug_line\0.debug_info\0.debug_abbrev\0"; | |
| 122 | + | |
| 123 | +(* Where in ShNames the name is: the byte after a zero that starts it and | |
| 124 | + is followed by one; the empty name, found nowhere, is 0 *) | |
| 125 | +proc nameAt(name: []U8): INT; | |
| 126 | +var i, k: INT; same: BOOL; | |
| 127 | +begin | |
| 128 | + for i := 1 to LEN(ShNames) - LEN(name) - 1 do | |
| 129 | + if ShNames[i - 1] = 0 then | |
| 130 | + same := ShNames[i + LEN(name)] = 0; | |
| 131 | + for k := 0 to LEN(name) - 1 do | |
| 132 | + if ShNames[i + k] # name[k] then same := false; end; | |
| 133 | + end; | |
| 134 | + if same and (LEN(name) > 0) then return i; end; | |
| 135 | + end; | |
| 136 | + end; | |
| 137 | + return 0; | |
| 138 | +end; | |
| 139 | + | |
| 140 | +(* A section header. Its name is stored as its offset in ShNames. *) | |
| 141 | +proc shdr(name: []U8; typ: INT; flags, adr, off, size: INT; lnk, info: INT; align, entsize: INT); | |
| 142 | +begin | |
| 143 | + putN(nameAt(name), 4); putN(typ, 4); putN(flags, 8); putN(adr, 8); | |
| 144 | + putN(off, 8); putN(size, 8); putN(lnk, 4); putN(info, 4); | |
| 145 | + putN(align, 8); putN(entsize, 8); | |
| 146 | +end; | |
| 147 | + | |
| 148 | +proc putStr(s: []U8); | |
| 149 | +begin | |
| 150 | + putBytes(ADR(s[0]), LEN(s)); | |
| 151 | + putN(0, 1); | |
| 152 | +end; | |
| 153 | + | |
| 154 | +(* LEB128, unsigned *) | |
| 155 | +proc putU(v: INT); | |
| 156 | +begin | |
| 157 | + while v >= 80H do | |
| 158 | + putN(v & 7FH | 80H, 1); | |
| 159 | + v := v shr 7; | |
| 160 | + end; | |
| 161 | + putN(v, 1); | |
| 162 | +end; | |
| 163 | + | |
| 164 | +(* The compile unit, which names the line table, and its abbreviation; | |
| 165 | + the table itself is in the places, in rodata (Link). Section | |
| 166 | + offsets go in off, sizes in size: line, info, abbrev. *) | |
| 167 | +proc putDebug(off, size: []INT); | |
| 168 | +var cwd: []U8; err: INT; | |
| 169 | +begin | |
| 170 | + off[0] := Chunk.chunks[Link.placesChunk].adr - Link.roAdr + Link.roOff + Link.linesOff; | |
| 171 | + size[0] := Link.linesSize; | |
| 172 | + off[1] := len; | |
| 173 | + putN(0, 4); putN(4, 2); putN(0, 4); putN(8, 1); (* length, version, abbrev, address size *) | |
| 174 | + putU(1); | |
| 175 | + putStr(mainPath); | |
| 176 | + cwd, err := Sys.workDir(); | |
| 177 | + if err # 0 then cwd := ""; end; | |
| 178 | + putStr(cwd); | |
| 179 | + putStr("raz"); | |
| 180 | + putN(0, 4); (* the line table, at 0 *) | |
| 181 | + putN(Link.Base + Link.textOff, 8); putN(Link.textSize, 8); (* the code, all of it *) | |
| 182 | + patch(off[1], len - off[1] - 4, 4); | |
| 183 | + size[1] := len - off[1]; | |
| 184 | + | |
| 185 | + off[2] := len; | |
| 186 | + putU(1); putU(11H); putN(0, 1); (* 1: a compile unit, no children *) | |
| 187 | + putU(03H); putU(08H); (* name, a string *) | |
| 188 | + putU(1BH); putU(08H); (* comp_dir, a string *) | |
| 189 | + putU(25H); putU(08H); (* producer, a string *) | |
| 190 | + putU(10H); putU(17H); (* stmt_list, a section offset *) | |
| 191 | + putU(11H); putU(01H); (* low_pc, an address *) | |
| 192 | + putU(12H); putU(07H); (* high_pc, a length in 8 bytes *) | |
| 193 | + putN(0, 2); putN(0, 1); | |
| 194 | + size[2] := len - off[2]; | |
| 195 | +end; | |
| 196 | + | |
| 197 | +const | |
| 198 | + Interp = "/lib64/ld-linux-x86-64.so.2"; | |
| 199 | + (* program header types *) | |
| 200 | + PtLoad = 1; PtDynamic = 2; PtInterp = 3; PtGnuStack = 6474E551H; | |
| 201 | + | |
| 202 | +var | |
| 203 | + (* in the room at the end of rodata: where each of the loader's | |
| 204 | + tables starts, from extraAdr *) | |
| 205 | + symAt, relaAt, hashAt, strAt: INT; | |
| 206 | + nsym, strLen: INT; | |
| 207 | + | |
| 208 | +(* Whether chunk c is a C symbol the program uses *) | |
| 209 | +proc isExt(c: INT): BOOL; | |
| 210 | +begin | |
| 211 | + return Chunk.chunks[c].live and Chunk.chunks[c].ext; | |
| 212 | +end; | |
| 213 | + | |
| 214 | +(* The sizes of the loader's tables: what linkAll must leave room for. *) | |
| 215 | +proc sizeTables(roExtra, dynSize: ^INT); | |
| 216 | +var c, i: INT; | |
| 217 | +begin | |
| 218 | + nsym := 1; | |
| 219 | + strLen := 1; | |
| 220 | + for i := 0 to numNeeded - 1 do strLen := strLen + LEN(Scan.nameOf(needed[i])) + 1; end; | |
| 221 | + for c := 0 to Chunk.numChunks - 1 do | |
| 222 | + if isExt(c) then | |
| 223 | + INC(nsym); | |
| 224 | + strLen := strLen + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; | |
| 225 | + end; | |
| 226 | + end; | |
| 227 | + symAt := (LEN(Interp) + 1 + 7) div 8 * 8; | |
| 228 | + relaAt := symAt + nsym * 24; | |
| 229 | + hashAt := relaAt + (nsym - 1) * 24; | |
| 230 | + strAt := hashAt + 4 * (3 + nsym); | |
| 231 | + roExtra^ := strAt + strLen; | |
| 232 | + dynSize^ := (numNeeded + 10) * 16; | |
| 233 | +end; | |
| 234 | + | |
| 235 | +(* The loader's tables in the room at the end of rodata, which the | |
| 236 | + buffer already holds as zeros: the loader's name; the symbols, one | |
| 237 | + for each C symbol used; a GLOB_DAT relocation for each, at its | |
| 238 | + quadword; a hash table of one bucket; and their names. *) | |
| 239 | +proc putTables; | |
| 240 | +var c, k, i, at, name: INT; | |
| 241 | +begin | |
| 242 | + len := Link.extraAdr - Link.roAdr + Link.roOff; | |
| 243 | + putStr(Interp); | |
| 244 | + padTo(Link.extraAdr - Link.roAdr + Link.roOff + symAt); | |
| 245 | + putN(0, 24); | |
| 246 | + name := strLen; | |
| 247 | + for c := Chunk.numChunks - 1 to 0 by -1 do (* names go last in the string table *) | |
| 248 | + if isExt(c) then name := name - LEN(Scan.nameOf(Chunk.chunks[c].name)) - 1; end; | |
| 249 | + end; | |
| 250 | + for c := 0 to Chunk.numChunks - 1 do | |
| 251 | + if isExt(c) then | |
| 252 | + putN(name, 4); putN(10H, 1); putN(0, 1); putN(0, 2); (* global, undefined *) | |
| 253 | + putN(0, 8); putN(0, 8); | |
| 254 | + name := name + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; | |
| 255 | + end; | |
| 256 | + end; | |
| 257 | + k := 1; | |
| 258 | + for c := 0 to Chunk.numChunks - 1 do | |
| 259 | + if isExt(c) then | |
| 260 | + putN(Chunk.chunks[c].adr, 8); | |
| 261 | + putN((k shl 32) | 6, 8); (* R_X86_64_GLOB_DAT *) | |
| 262 | + putN(0, 8); | |
| 263 | + INC(k); | |
| 264 | + end; | |
| 265 | + end; | |
| 266 | + putN(1, 4); putN(nsym, 4); putN(nsym - 1, 4); (* the bucket chains every symbol *) | |
| 267 | + putN(0, 4); | |
| 268 | + for i := 1 to nsym - 1 do putN(i - 1, 4); end; | |
| 269 | + putN(0, 1); | |
| 270 | + for i := 0 to numNeeded - 1 do putStr(Scan.nameOf(needed[i])); end; | |
| 271 | + for c := 0 to Chunk.numChunks - 1 do | |
| 272 | + if isExt(c) then putStr(Scan.nameOf(Chunk.chunks[c].name)); end; | |
| 273 | + end; | |
| 274 | + len := Link.roOff + Link.roSize; | |
| 275 | + | |
| 276 | + (* the dynamic table, at the start of the writable segment *) | |
| 277 | + padTo(Link.bssOff); | |
| 278 | + at := 1; | |
| 279 | + for i := 0 to numNeeded - 1 do | |
| 280 | + putN(1, 8); putN(at, 8); (* DT_NEEDED *) | |
| 281 | + at := at + LEN(Scan.nameOf(needed[i])) + 1; | |
| 282 | + end; | |
| 283 | + putN(4, 8); putN(Link.extraAdr + hashAt, 8); (* DT_HASH *) | |
| 284 | + putN(5, 8); putN(Link.extraAdr + strAt, 8); (* DT_STRTAB *) | |
| 285 | + putN(6, 8); putN(Link.extraAdr + symAt, 8); (* DT_SYMTAB *) | |
| 286 | + putN(10, 8); putN(strLen, 8); (* DT_STRSZ *) | |
| 287 | + putN(11, 8); putN(24, 8); (* DT_SYMENT *) | |
| 288 | + putN(7, 8); putN(Link.extraAdr + relaAt, 8); (* DT_RELA *) | |
| 289 | + putN(8, 8); putN((nsym - 1) * 24, 8); (* DT_RELASZ *) | |
| 290 | + putN(9, 8); putN(24, 8); (* DT_RELAENT *) | |
| 291 | + putN(21, 8); putN(0, 8); (* DT_DEBUG, for gdb *) | |
| 292 | + putN(0, 16); (* DT_NULL *) | |
| 293 | +end; | |
| 294 | + | |
| 295 | +proc writeElf*(path: []U8; entry: INT); | |
| 296 | +var | |
| 297 | + nph, nsh, c, nsyms, sec, strsCap: INT; | |
| 298 | + roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: INT; | |
| 299 | + dbgOff, dbgSize: [3]INT; | |
| 300 | + strs: ^U8; | |
| 301 | + s: []U8; | |
| 302 | + dyn: BOOL; | |
| 303 | +begin | |
| 304 | + cap := 1 shl 16; | |
| 305 | + buf := Scan.grab(cap); | |
| 306 | + len := 0; | |
| 307 | + dyn := numNeeded > 0; | |
| 308 | + roExtra := 0; dynSize := 0; | |
| 309 | + if dyn then sizeTables(ADR(roExtra), ADR(dynSize)); end; | |
| 310 | + (* room for the most program headers either kind of program has *) | |
| 311 | + Link.linkAll(64 + 6 * 56, roExtra, dynSize); | |
| 312 | + if dyn then | |
| 313 | + nph := 6; | |
| 314 | + else | |
| 315 | + nph := 2; | |
| 316 | + if Link.roSize > 0 then INC(nph); end; | |
| 317 | + if Link.bssSize > 0 then INC(nph); end; | |
| 318 | + end; | |
| 319 | + | |
| 320 | + (* ELF header *) | |
| 321 | + putN(464C457FH, 4); (* #127 'ELF' *) | |
| 322 | + putN(2, 1); putN(1, 1); putN(1, 1); putN(0, 9); (* 64-bit, little-endian, version 1 *) | |
| 323 | + putN(2, 2); (* ET_EXEC *) | |
| 324 | + putN(62, 2); (* EM_X86_64 *) | |
| 325 | + putN(1, 4); | |
| 326 | + putN(Chunk.chunks[entry].adr, 8); | |
| 327 | + putN(64, 8); (* program headers follow *) | |
| 328 | + putN(0, 8); (* section headers: patched below *) | |
| 329 | + putN(0, 4); | |
| 330 | + nsh := 10; | |
| 331 | + if dyn then nsh := 13; end; | |
| 332 | + putN(64, 2); putN(56, 2); putN(nph, 2); | |
| 333 | + putN(64, 2); putN(nsh, 2); putN(6, 2); (* .shstrtab is section 6 *) | |
| 334 | + | |
| 335 | + (* flags: 4 read, 5 read and run, 6 read and write *) | |
| 336 | + if dyn then | |
| 337 | + phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(Interp) + 1, LEN(Interp) + 1, 1); | |
| 338 | + end; | |
| 339 | + (* the first segment maps the headers too, so text can follow them *) | |
| 340 | + phdr(PtLoad, 5, 0, Link.Base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page); | |
| 341 | + if Link.roSize > 0 then phdr(PtLoad, 4, Link.roOff, Link.roAdr, Link.roSize, Link.roSize, Link.Page); end; | |
| 342 | + if dyn then | |
| 343 | + phdr(PtLoad, 6, Link.bssOff, Link.bssAdr, dynSize, Link.bssSize, Link.Page); | |
| 344 | + phdr(PtDynamic, 6, Link.bssOff, Link.bssAdr, dynSize, dynSize, 8); | |
| 345 | + elsif Link.bssSize > 0 then | |
| 346 | + phdr(PtLoad, 6, 0, Link.bssAdr, 0, Link.bssSize, Link.Page); | |
| 347 | + end; | |
| 348 | + phdr(PtGnuStack, 6, 0, 0, 0, 0, 16); (* a stack that can't run code *) | |
| 349 | + | |
| 350 | + padTo(Link.textOff); | |
| 351 | + putBytes(Link.textImg, Link.textSize); | |
| 352 | + padTo(Link.roOff); (* even when empty, so that every section lies in the file *) | |
| 353 | + putBytes(Link.roImg, Link.roSize); | |
| 354 | + if dyn then putTables; end; | |
| 355 | + | |
| 356 | + (* the symbol table: one local symbol for each named live chunk *) | |
| 357 | + padTo((len + 7) div 8 * 8); | |
| 358 | + symOff := len; | |
| 359 | + strsCap := 1 shl 16; | |
| 360 | + strs := Scan.grab(strsCap); | |
| 361 | + strsLen := 1; | |
| 362 | + strs[0] := 0; | |
| 363 | + putN(0, 24); | |
| 364 | + nsyms := 1; | |
| 365 | + for c := 0 to Chunk.numChunks - 1 do | |
| 366 | + if not stripped and Chunk.chunks[c].live and (Chunk.chunks[c].name >= 0) and not Chunk.chunks[c].ext then | |
| 367 | + s := Chunk.chunkName(c); | |
| 368 | + if strsLen + LEN(s) + 1 > strsCap then | |
| 369 | + strs := Scan.regrab(strs, strsLen, strsLen * 2 + 256); | |
| 370 | + strsCap := strsLen * 2 + 256; | |
| 371 | + end; | |
| 372 | + COPY(strs[strsLen..strsLen + LEN(s)], s); | |
| 373 | + strs[strsLen + LEN(s)] := 0; | |
| 374 | + if Chunk.chunks[c].sec = Chunk.secText then (* the section's index in the headers below *) | |
| 375 | + sec := 1; | |
| 376 | + elsif Chunk.chunks[c].sec = Chunk.secRodata then | |
| 377 | + sec := 2; | |
| 378 | + else | |
| 379 | + sec := 3; | |
| 380 | + end; | |
| 381 | + putN(strsLen, 4); | |
| 382 | + if sec = 1 then putN(2, 1); else putN(1, 1); end; (* STT_FUNC, STT_OBJECT *) | |
| 383 | + putN(0, 1); | |
| 384 | + putN(sec, 2); | |
| 385 | + putN(Chunk.chunks[c].adr, 8); | |
| 386 | + putN(Chunk.chunks[c].size, 8); | |
| 387 | + strsLen := strsLen + LEN(s) + 1; | |
| 388 | + INC(nsyms); | |
| 389 | + end; | |
| 390 | + end; | |
| 391 | + strOff := len; | |
| 392 | + putBytes(strs, strsLen); | |
| 393 | + shstrOff := len; | |
| 394 | + putBytes(ADR(ShNames[0]), LEN(ShNames)); | |
| 395 | + if stripped or (Link.placesChunk < 0) then | |
| 396 | + for c := 0 to 2 do dbgOff[c] := len; dbgSize[c] := 0; end; | |
| 397 | + else | |
| 398 | + putDebug(dbgOff, dbgSize); | |
| 399 | + end; | |
| 400 | + | |
| 401 | + padTo((len + 7) div 8 * 8); | |
| 402 | + shOff := len; | |
| 403 | + patch(40, shOff, 8); | |
| 404 | + (* types: 1 PROGBITS, 2 SYMTAB, 3 STRTAB, 6 DYNAMIC, 8 NOBITS, 11 DYNSYM; | |
| 405 | + flags: 2 ALLOC, 3 WRITE+ALLOC, 6 ALLOC+EXECINSTR. A symbol table's | |
| 406 | + link is its string table's index: 5 .strtab, 8 .dynstr. The order | |
| 407 | + here is the index the code above relies on. *) | |
| 408 | + shdr("", 0, 0, 0, 0, 0, 0, 0, 0, 0); | |
| 409 | + shdr(".text", 1, 6, Link.Base + Link.textOff, Link.textOff, Link.textSize, 0, 0, 16, 0); | |
| 410 | + shdr(".rodata", 1, 2, Link.roAdr, Link.roOff, Link.roSize, 0, 0, 16, 0); | |
| 411 | + shdr(".bss", 8, 3, Link.bssAdr + dynSize, Link.roOff + Link.roSize, Link.bssSize - dynSize, 0, 0, 16, 0); | |
| 412 | + shdr(".symtab", 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24); | |
| 413 | + shdr(".strtab", 3, 0, 0, strOff, strsLen, 0, 0, 1, 0); | |
| 414 | + shdr(".shstrtab", 3, 0, 0, shstrOff, LEN(ShNames), 0, 0, 1, 0); | |
| 415 | + if dyn then (* the loader's tables, for readelf and objdump *) | |
| 416 | + shdr(".dynamic", 6, 3, Link.bssAdr, Link.bssOff, dynSize, 8, 0, 8, 16); | |
| 417 | + shdr(".dynstr", 3, 2, Link.extraAdr + strAt, Link.extraAdr + strAt - Link.roAdr + Link.roOff, strLen, 0, 0, 1, 0); | |
| 418 | + shdr(".dynsym", 11, 2, Link.extraAdr + symAt, Link.extraAdr + symAt - Link.roAdr + Link.roOff, nsym * 24, 8, 1, 8, 24); | |
| 419 | + end; | |
| 420 | + shdr(".debug_line", 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0); | |
| 421 | + shdr(".debug_info", 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0); | |
| 422 | + shdr(".debug_abbrev", 1, 0, 0, dbgOff[2], dbgSize[2], 0, 0, 1, 0); | |
| 423 | + | |
| 424 | + if not Files.write(path, buf[0..len]) or (Sys.chmod(path, 1EDH) < 0) then | |
| 425 | + Diag.fatal(Diag.say("can't write {}", [Fmt.str(path)])); | |
| 426 | + end; | |
| 427 | +end; | |
| 428 | + | |
| 429 | +begin | |
| 430 | + mainPath := ""; | |
| 431 | +end Elf. |
| @@ -0,0 +1,381 @@ | ||
| 1 | +(* Link: 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 writes them | |
| 13 | + in the room left there. | |
| 14 | + | |
| 15 | + Depends on: Chunk for chunks and fixups, Scan for names and memory, | |
| 16 | + Diag for errors and paths. | |
| 17 | + Leaves out: the file format; that is Elf. *) | |
| 18 | +module Link; | |
| 19 | +import Fmt, Diag, Scan, Chunk; | |
| 20 | + | |
| 21 | +const | |
| 22 | + Base* = 400000H; | |
| 23 | + Page* = 4096; | |
| 24 | + | |
| 25 | +var | |
| 26 | + textOff*, textSize*: INT; (* file offset and size of text, at Base + textOff *) | |
| 27 | + roOff*, roSize*: INT; (* rodata, the room for the loader's tables included *) | |
| 28 | + roAdr*: INT; (* where rodata is loaded *) | |
| 29 | + extraAdr*: INT; (* that room *) | |
| 30 | + bssAdr*, bssSize*: INT; (* the writable segment, the loader's table first *) | |
| 31 | + bssOff*: INT; (* the file offset of that table *) | |
| 32 | + textImg*, roImg*: ^U8; | |
| 33 | + placesChunk*: INT; (* the rodata chunk of the places, made by the caller *) | |
| 34 | + linesOff*, linesSize*: INT; (* the line table, in the places: .debug_line *) | |
| 35 | + | |
| 36 | +proc setPlaces*(c: INT); | |
| 37 | +begin | |
| 38 | + placesChunk := c; | |
| 39 | +end; | |
| 40 | + | |
| 41 | +(* Marks live each chunk reached from entry through the fixups. *) | |
| 42 | +proc markLive*(entry: INT); | |
| 43 | +var stack: ^INT; sp, c, f, t: INT; fx: ^Chunk.Fixup; | |
| 44 | +begin | |
| 45 | + stack := ^INT(Scan.grab(Chunk.numChunks * SIZE(INT))); | |
| 46 | + Chunk.setLive(entry); | |
| 47 | + stack[0] := entry; | |
| 48 | + sp := 1; | |
| 49 | + while sp > 0 do | |
| 50 | + DEC(sp); | |
| 51 | + c := stack[sp]; | |
| 52 | + if Chunk.chunks[c].size < 0 then | |
| 53 | + Diag.fatal(Diag.say("{} is declared but never defined", [Fmt.str(Chunk.chunkName(c))])); | |
| 54 | + end; | |
| 55 | + (* code and constants have fixups; variables start at zero, holding | |
| 56 | + no addresses *) | |
| 57 | + fx := nil; | |
| 58 | + if Chunk.chunks[c].sec = Chunk.secText then | |
| 59 | + fx := Chunk.fixups; | |
| 60 | + elsif Chunk.chunks[c].sec = Chunk.secRodata then | |
| 61 | + fx := Chunk.dataFixups; | |
| 62 | + end; | |
| 63 | + if fx # nil then | |
| 64 | + for f := Chunk.chunks[c].firstFix to Chunk.chunks[c].firstFix + Chunk.chunks[c].numFix - 1 do | |
| 65 | + t := fx[f].chunk; | |
| 66 | + if not Chunk.chunks[t].live then | |
| 67 | + Chunk.setLive(t); | |
| 68 | + stack[sp] := t; | |
| 69 | + INC(sp); | |
| 70 | + end; | |
| 71 | + end; | |
| 72 | + end; | |
| 73 | + end; | |
| 74 | +end; | |
| 75 | + | |
| 76 | +(* Gives the live chunks of one section their places, from start; | |
| 77 | + returns the end. *) | |
| 78 | +proc place(sec: Chunk.Section; start: INT): INT; | |
| 79 | +var c, a: INT; | |
| 80 | +begin | |
| 81 | + a := start; | |
| 82 | + for c := 0 to Chunk.numChunks - 1 do | |
| 83 | + if Chunk.chunks[c].live and (Chunk.chunks[c].sec = sec) then | |
| 84 | + a := (a + Chunk.chunks[c].align - 1) div Chunk.chunks[c].align * Chunk.chunks[c].align; | |
| 85 | + Chunk.setAdr(c, a); | |
| 86 | + a := a + Chunk.chunks[c].size; | |
| 87 | + end; | |
| 88 | + end; | |
| 89 | + return a; | |
| 90 | +end; | |
| 91 | + | |
| 92 | +(* The line table, DWARF 4's line program, which gdb reads as | |
| 93 | + .debug_line and Rt to say where code comes from: one sequence for | |
| 94 | + each live procedure with lines, a row for each place noted, its | |
| 95 | + address, file, line and column. A row whose address and line move a | |
| 96 | + little is one byte, a special opcode; the others are made of the | |
| 97 | + standard ones. Made in lp, lpLen bytes long. *) | |
| 98 | +const | |
| 99 | + LineBase = -5; LineRange = 14; OpcodeBase = 13; (* the special opcodes' *) | |
| 100 | + | |
| 101 | +var | |
| 102 | + lp: ^U8; | |
| 103 | + lpLen, lpCap: INT; | |
| 104 | + | |
| 105 | +proc putB(v: INT); | |
| 106 | +begin | |
| 107 | + if lpLen = lpCap then | |
| 108 | + lp := Scan.regrab(lp, lpLen, lpCap * 2 + 256); | |
| 109 | + lpCap := lpCap * 2 + 256; | |
| 110 | + end; | |
| 111 | + lp[lpLen] := U8(v & 0FFH); | |
| 112 | + INC(lpLen); | |
| 113 | +end; | |
| 114 | + | |
| 115 | +(* The n low bytes of v, least significant first *) | |
| 116 | +proc putN(v: INT; n: INT); | |
| 117 | +var i: INT; | |
| 118 | +begin | |
| 119 | + for i := 1 to n do putB(v); v := v shr 8; end; | |
| 120 | +end; | |
| 121 | + | |
| 122 | +(* LEB128, unsigned and signed *) | |
| 123 | +proc putU(v: INT); | |
| 124 | +begin | |
| 125 | + while v >= 80H do putB(v & 7FH | 80H); v := v shr 7; end; | |
| 126 | + putB(v); | |
| 127 | +end; | |
| 128 | + | |
| 129 | +proc putS(v: INT); | |
| 130 | +begin | |
| 131 | + while (v < -64) or (v >= 64) do putB(v & 7FH | 80H); v := v shr 7; end; | |
| 132 | + putB(v & 7FH); | |
| 133 | +end; | |
| 134 | + | |
| 135 | +(* v as the four bytes at at in p *) | |
| 136 | +proc patch4(p: ^U8; at, v: INT); | |
| 137 | +var i: INT; | |
| 138 | +begin | |
| 139 | + for i := 0 to 3 do p[at + i] := U8((v shr (8 * i)) & 0FFH); end; | |
| 140 | +end; | |
| 141 | + | |
| 142 | +(* v as the eight bytes at at in p *) | |
| 143 | +proc patch8(p: ^U8; at, v: INT); | |
| 144 | +var i: INT; | |
| 145 | +begin | |
| 146 | + for i := 0 to 7 do p[at + i] := U8((v shr (8 * i)) & 0FFH); end; | |
| 147 | +end; | |
| 148 | + | |
| 149 | +(* Whether the standard opcode i takes an operand *) | |
| 150 | +proc takesOperand(i: INT): BOOL; | |
| 151 | +begin | |
| 152 | + return (i = 2) or (i = 3) or (i = 4) or (i = 5) or (i = 9) or (i = 12); | |
| 153 | +end; | |
| 154 | + | |
| 155 | +(* The line program of the files paths, nfiles of them, numbered from | |
| 156 | + 1, as they are written in the table *) | |
| 157 | +proc makeLines(paths: ^INT; nfiles: INT); | |
| 158 | +var i, f, c, cf, cl, cc, op, at, da, dl: INT; s: []U8; | |
| 159 | +begin | |
| 160 | + lpLen := 0; | |
| 161 | + putN(0, 4); (* unit_length, patched below *) | |
| 162 | + putN(4, 2); (* version *) | |
| 163 | + putN(0, 4); (* header_length, patched below *) | |
| 164 | + putB(1); putB(1); (* minimum_instruction_length, maximum_operations *) | |
| 165 | + putB(1); (* default_is_stmt *) | |
| 166 | + putB(LineBase); putB(LineRange); putB(OpcodeBase); | |
| 167 | + for i := 1 to OpcodeBase - 1 do | |
| 168 | + if takesOperand(i) then putB(1); else putB(0); end; (* their operands *) | |
| 169 | + end; | |
| 170 | + putB(0); (* no include directories *) | |
| 171 | + for f := 0 to nfiles - 1 do | |
| 172 | + s := Diag.shownPath(Scan.nameOf(paths[f])); | |
| 173 | + for i := 0 to LEN(s) - 1 do putB(INT(s[i])); end; | |
| 174 | + putN(0, 4); (* its end, directory, time, length *) | |
| 175 | + end; | |
| 176 | + putB(0); | |
| 177 | + patch4(lp, 6, lpLen - 10); | |
| 178 | + i := 0; | |
| 179 | + while i < Chunk.numLines do | |
| 180 | + c := Chunk.lines[i].chunk; | |
| 181 | + if not Chunk.chunks[c].live then | |
| 182 | + INC(i); | |
| 183 | + else | |
| 184 | + putB(0); putU(9); putB(2); (* DW_LNE_set_address *) | |
| 185 | + putN(Chunk.chunks[c].adr + Chunk.lines[i].at - Chunk.chunks[c].start, 8); | |
| 186 | + at := Chunk.lines[i].at; cf := 1; cl := 1; cc := 0; | |
| 187 | + while (i < Chunk.numLines) and (Chunk.lines[i].chunk = c) do | |
| 188 | + f := 1; | |
| 189 | + while paths[f - 1] # Chunk.lines[i].path do INC(f); end; | |
| 190 | + if f # cf then putB(4); putU(f); cf := f; end; (* DW_LNS_set_file *) | |
| 191 | + if Chunk.lines[i].col # cc then putB(5); putU(Chunk.lines[i].col); cc := Chunk.lines[i].col; end; (* DW_LNS_set_column *) | |
| 192 | + da := Chunk.lines[i].at - at; | |
| 193 | + dl := Chunk.lines[i].line - cl; | |
| 194 | + op := (dl - LineBase) + LineRange * da + OpcodeBase; | |
| 195 | + if (dl >= LineBase) and (dl < LineBase + LineRange) and (op <= 255) then | |
| 196 | + putB(op); (* a special opcode: a row *) | |
| 197 | + else | |
| 198 | + if da > 0 then putB(2); putU(da); end; (* DW_LNS_advance_pc *) | |
| 199 | + if dl # 0 then putB(3); putS(dl); end; (* DW_LNS_advance_line *) | |
| 200 | + putB(1); (* DW_LNS_copy: a row *) | |
| 201 | + end; | |
| 202 | + at := Chunk.lines[i].at; cl := Chunk.lines[i].line; | |
| 203 | + INC(i); | |
| 204 | + end; | |
| 205 | + putB(2); putU(Chunk.chunks[c].start + Chunk.chunks[c].size - at); | |
| 206 | + putB(0); putU(1); putB(1); (* DW_LNE_end_sequence *) | |
| 207 | + end; | |
| 208 | + end; | |
| 209 | + patch4(lp, 0, lpLen - 4); | |
| 210 | +end; | |
| 211 | + | |
| 212 | +var | |
| 213 | + buf: ^U8; (* the places, being made *) | |
| 214 | + at, names, poolAt: INT; (* where the next number goes, the next name, the pool's start *) | |
| 215 | + | |
| 216 | +(* The name s, in the pool, and its offset from the pool's start, at at *) | |
| 217 | +proc putName(s: []U8); | |
| 218 | +begin | |
| 219 | + patch4(buf, at, names - poolAt); | |
| 220 | + COPY(buf[names..names + LEN(s)], s); | |
| 221 | + buf[names + LEN(s)] := 0; | |
| 222 | + names := names + LEN(s) + 1; | |
| 223 | + at := at + 4; | |
| 224 | +end; | |
| 225 | + | |
| 226 | +(* Whether chunk c is a live procedure, whose places Rt knows *) | |
| 227 | +proc isProc(c: INT): BOOL; | |
| 228 | +begin | |
| 229 | + return Chunk.chunks[c].live and (Chunk.chunks[c].sec = Chunk.secText) and (Chunk.chunks[c].home >= 0); | |
| 230 | +end; | |
| 231 | + | |
| 232 | +(* The places that Rt reads to say where a failed check or a signal | |
| 233 | + was, and what calls were under way: where the line table is in them, | |
| 234 | + where text starts and stops, the numbers of procedures and of their | |
| 235 | + variables (I64s); for each live procedure where it starts and stops, | |
| 236 | + the offset of its name, its first variable and how many it has, and | |
| 237 | + the registers of variables it saves, as bits of Gen's VarRegs, and | |
| 238 | + the frame offset they are saved at (U32s); for each variable the | |
| 239 | + offset of its name, its place in the frame and its kind, as | |
| 240 | + Parse's VarKind (U32s); then the names, each ending in a zero | |
| 241 | + byte; then the line table (makeLines), at linesOff. They are in no | |
| 242 | + order, since Rt looks at them only as the program stops. *) | |
| 243 | +proc makePlaces(start, stop: INT); | |
| 244 | +var | |
| 245 | + files, first, count: ^INT; | |
| 246 | + i, f, nfiles, np, nv, c, v: INT; | |
| 247 | +begin | |
| 248 | + files := ^INT(Scan.grab(Chunk.numLines * SIZE(INT) + SIZE(INT))); | |
| 249 | + first := ^INT(Scan.grab(Chunk.numChunks * SIZE(INT))); | |
| 250 | + count := ^INT(Scan.grab(Chunk.numChunks * SIZE(INT))); | |
| 251 | + for c := 0 to Chunk.numChunks - 1 do first[c] := 0; count[c] := 0; end; | |
| 252 | + for v := Chunk.numVarNotes - 1 to 0 by -1 do | |
| 253 | + first[Chunk.varNotes[v].chunk] := v; | |
| 254 | + INC(count[Chunk.varNotes[v].chunk]); | |
| 255 | + end; | |
| 256 | + nfiles := 0; poolAt := 0; np := 0; nv := 0; | |
| 257 | + for i := 0 to Chunk.numLines - 1 do | |
| 258 | + if Chunk.chunks[Chunk.lines[i].chunk].live then | |
| 259 | + f := 0; | |
| 260 | + while (f < nfiles) and (files[f] # Chunk.lines[i].path) do INC(f); end; | |
| 261 | + if f = nfiles then | |
| 262 | + files[f] := Chunk.lines[i].path; | |
| 263 | + INC(nfiles); | |
| 264 | + end; | |
| 265 | + end; | |
| 266 | + end; | |
| 267 | + makeLines(files, nfiles); | |
| 268 | + for c := 0 to Chunk.numChunks - 1 do | |
| 269 | + if isProc(c) then | |
| 270 | + INC(np); | |
| 271 | + poolAt := poolAt + LEN(Chunk.chunkName(c)) + 1; | |
| 272 | + for v := first[c] to first[c] + count[c] - 1 do | |
| 273 | + poolAt := poolAt + LEN(Scan.nameOf(Chunk.varNotes[v].name)) + 1; | |
| 274 | + end; | |
| 275 | + nv := nv + count[c]; | |
| 276 | + end; | |
| 277 | + end; | |
| 278 | + buf := Scan.grab(40 + 28 * np + 12 * nv + poolAt + lpLen); | |
| 279 | + (* the pool of names starts at poolAt, the line table after it *) | |
| 280 | + poolAt := 40 + 28 * np + 12 * nv; | |
| 281 | + names := poolAt; | |
| 282 | + at := 40; | |
| 283 | + v := 0; | |
| 284 | + for c := 0 to Chunk.numChunks - 1 do | |
| 285 | + if isProc(c) then | |
| 286 | + patch4(buf, at, Chunk.chunks[c].adr); | |
| 287 | + patch4(buf, at + 4, Chunk.chunks[c].adr + Chunk.chunks[c].size); | |
| 288 | + at := at + 8; | |
| 289 | + putName(Chunk.chunkName(c)); | |
| 290 | + patch4(buf, at, v); | |
| 291 | + patch4(buf, at + 4, count[c]); | |
| 292 | + patch4(buf, at + 8, Chunk.chunks[c].saveMask); | |
| 293 | + patch4(buf, at + 12, Chunk.chunks[c].saveAt); | |
| 294 | + at := at + 16; | |
| 295 | + v := v + count[c]; | |
| 296 | + end; | |
| 297 | + end; | |
| 298 | + for c := 0 to Chunk.numChunks - 1 do | |
| 299 | + if isProc(c) then | |
| 300 | + for v := first[c] to first[c] + count[c] - 1 do | |
| 301 | + putName(Scan.nameOf(Chunk.varNotes[v].name)); | |
| 302 | + patch4(buf, at, Chunk.varNotes[v].off); | |
| 303 | + patch4(buf, at + 4, Chunk.varNotes[v].kind); | |
| 304 | + at := at + 8; | |
| 305 | + end; | |
| 306 | + end; | |
| 307 | + end; | |
| 308 | + linesOff := names; | |
| 309 | + linesSize := lpLen; | |
| 310 | + COPY(buf[names..names + lpLen], lp[0..lpLen]); | |
| 311 | + patch8(buf, 0, linesOff); | |
| 312 | + patch8(buf, 8, start); | |
| 313 | + patch8(buf, 16, stop); | |
| 314 | + patch8(buf, 24, np); | |
| 315 | + patch8(buf, 32, nv); | |
| 316 | + Chunk.setData(placesChunk, buf, names + lpLen, 8); | |
| 317 | +end; | |
| 318 | + | |
| 319 | +(* Places the live chunks, leaving roExtra bytes at the end of rodata | |
| 320 | + and dynSize at the start of the writable segment. The segments follow | |
| 321 | + each other in the file with no padding, and each is loaded a page | |
| 322 | + further from its file offset than the one before: rodata at Base + | |
| 323 | + Page + roOff, the writable segment at Base + 2 * Page + bssOff. So an | |
| 324 | + address and its offset agree within a page, as the loader needs, and | |
| 325 | + no two segments, with their different permissions, share a page. | |
| 326 | + The bss of a program without the loader's table has no bytes in the | |
| 327 | + file, and starts on a page of its own. *) | |
| 328 | +proc linkAll*(headerSize, roExtra, dynSize: INT); | |
| 329 | +var c, f, at, p, e, v: INT; | |
| 330 | +begin | |
| 331 | + textOff := (headerSize + 31) div 32 * 32; (* as each code chunk is *) | |
| 332 | + e := place(Chunk.secText, Base + textOff); | |
| 333 | + textSize := e - (Base + textOff); | |
| 334 | + if placesChunk >= 0 then makePlaces(Base + textOff, e); end; | |
| 335 | + roOff := (textOff + textSize + 31) div 32 * 32; | |
| 336 | + roAdr := Base + Page + roOff; | |
| 337 | + e := place(Chunk.secRodata, roAdr); | |
| 338 | + extraAdr := (e + 7) div 8 * 8; | |
| 339 | + if roExtra > 0 then e := extraAdr + roExtra; end; | |
| 340 | + roSize := e - roAdr; | |
| 341 | + bssOff := (roOff + roSize + 31) div 32 * 32; | |
| 342 | + if dynSize > 0 then | |
| 343 | + bssAdr := Base + 2 * Page + bssOff; | |
| 344 | + else | |
| 345 | + bssAdr := (roAdr + roSize + Page - 1) div Page * Page; | |
| 346 | + end; | |
| 347 | + e := place(Chunk.secBss, bssAdr + dynSize); | |
| 348 | + bssSize := e - bssAdr; | |
| 349 | + | |
| 350 | + textImg := Scan.grab(textSize + 1); | |
| 351 | + (* the gaps between code chunks are int3, a trap if ever run *) | |
| 352 | + for p := 0 to textSize do textImg[p] := 0CCH; end; | |
| 353 | + roImg := Scan.grab(roSize + 1); | |
| 354 | + for p := 0 to roSize do roImg[p] := 0; end; | |
| 355 | + for c := 0 to Chunk.numChunks - 1 do | |
| 356 | + if Chunk.chunks[c].live then | |
| 357 | + if Chunk.chunks[c].sec = Chunk.secText then | |
| 358 | + p := Chunk.chunks[c].adr - (Base + textOff); | |
| 359 | + COPY(textImg[p..p + Chunk.chunks[c].size], Chunk.text[Chunk.chunks[c].start..Chunk.chunks[c].start + Chunk.chunks[c].size]); | |
| 360 | + for f := Chunk.chunks[c].firstFix to Chunk.chunks[c].firstFix + Chunk.chunks[c].numFix - 1 do | |
| 361 | + at := Chunk.fixups[f].at - Chunk.chunks[c].start; | |
| 362 | + (* rel32: from the end of the field to the target *) | |
| 363 | + v := Chunk.chunks[Chunk.fixups[f].chunk].adr + Chunk.fixups[f].addend | |
| 364 | + - (Chunk.chunks[c].adr + at + 4); | |
| 365 | + patch4(textImg, p + at, v); | |
| 366 | + end; | |
| 367 | + elsif Chunk.chunks[c].sec = Chunk.secRodata then | |
| 368 | + p := Chunk.chunks[c].adr - roAdr; | |
| 369 | + COPY(roImg[p..p + Chunk.chunks[c].size], Chunk.rodata[Chunk.chunks[c].start..Chunk.chunks[c].start + Chunk.chunks[c].size]); | |
| 370 | + for f := Chunk.chunks[c].firstFix to Chunk.chunks[c].firstFix + Chunk.chunks[c].numFix - 1 do | |
| 371 | + patch8(roImg, p + Chunk.dataFixups[f].at, | |
| 372 | + Chunk.chunks[Chunk.dataFixups[f].chunk].adr + Chunk.dataFixups[f].addend); | |
| 373 | + end; | |
| 374 | + end; | |
| 375 | + end; | |
| 376 | + end; | |
| 377 | +end; | |
| 378 | + | |
| 379 | +begin | |
| 380 | + placesChunk := -1; | |
| 381 | +end Link. |
| @@ -15,7 +15,13 @@ | ||
| 15 | 15 | pseudo-random script of calls on its side and prints all it made or |
| 16 | 16 | answered (`Sym` also ends, as an argument says, in the error of a name |
| 17 | 17 | declared twice or of too many scopes): the output, the messages and |
| 18 | -the status must be the same. | |
| 18 | +the status must be the same. `Link` and `Elf` are tested by | |
| 19 | +`test/LinkDump`, which makes a program of chunks by a script and writes | |
| 20 | +it as a static, a dynamic and a stripped executable, which must be the | |
| 21 | +same file; `X64` by `test/X64Dump`, every encoder with operands from a | |
| 22 | +script, bytes, fixups and listing, and by `test/AsmDump` on the files | |
| 23 | +`test/genasm.py` makes: a long run of every instruction the assembler | |
| 24 | +knows, and a file for each of its errors. | |
| 19 | 25 | |
| 20 | 26 | `check.sh` builds a dumper for each side, `test/ScanDump.pas` over the |
| 21 | 27 | seed's units and `test/ScanDump.raz` over these, and has both read the |
| @@ -59,6 +65,21 @@ | ||
| 59 | 65 | `setAdr`, `setReals`, `setSaves`, `setCurFile`, and `rewindTo` for |
| 60 | 66 | gen's `Redo` and `Rewind`), and `Sym` has `setCurModule`. `Scan.grab` |
| 61 | 67 | is exported for the memory they all take. |
| 68 | +- **No `continue`.** The seed's loops that skip to the next round | |
| 69 | + (`MarkLive`, the line program, the assembler's statements) have the | |
| 70 | + rest of the body in an `else`, or in a procedure of its own. | |
| 71 | +- **`X64`.** `code` of `Chunk` being reserved, its text is `Chunk.text`; | |
| 72 | + `Say` and the rest of the encoder keep their names, in lower case. | |
| 73 | + `ConstFn`, the parser's reader of a constant, returns an `ERROR` too, | |
| 74 | + and `assemble` returns what it passes up. `mem` marks an offset beyond | |
| 75 | + 32 bits and goes on, since an operand has nowhere to return an error | |
| 76 | + to; the compile ends with the message shown. The listing's text is | |
| 77 | + made by `Diag.say`, which keeps it, and only when `-S` asks for it. | |
| 78 | +- **`Link` and `Elf`.** What `raz.pas` set there has a setter each: | |
| 79 | + `Link.setPlaces`, `Elf.needLib`, `noLibs`, `setMainPath`, | |
| 80 | + `setStripped` and `X64.setListing`. The file is written by | |
| 81 | + `Files.write` and made executable by `Sys.chmod`, in place of one | |
| 82 | + `open` with mode 755 (which the umask would trim). | |
| 62 | 83 | - **No bit cast.** A real's bits and back go through a pointer. |
| 63 | 84 | - **Speed.** On the 348,025 lines of `test/genbench.py`, 7.6 MB, the |
| 64 | 85 | seed's scanner takes 75 ms and this one 74. It took 121 at first. |
| @@ -120,6 +120,16 @@ | ||
| 120 | 120 | return p; |
| 121 | 121 | end; |
| 122 | 122 | |
| 123 | +(* p, which holds used bytes, moved to a block of size bytes *) | |
| 124 | +proc regrab*(p: ^U8; used, size: INT): ^U8; | |
| 125 | +var q: ^U8; | |
| 126 | +begin | |
| 127 | + q := grab(size); | |
| 128 | + COPY(q[0..used], p[0..used]); | |
| 129 | + Mem.free(Heap.heap, p); | |
| 130 | + return q; | |
| 131 | +end; | |
| 132 | + | |
| 123 | 133 | (* n zeroed words *) |
| 124 | 134 | proc words64(n: INT): ^INT; |
| 125 | 135 | var p: ^INT; i: INT; |
| @@ -0,0 +1,1472 @@ | ||
| 1 | +(* X64: 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. 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 and the assembler's symbols; Diag, for | |
| 9 | + positions; Chunk for the code buffer and fixups. | |
| 10 | + Leaves out: choosing instructions; that is Gen. | |
| 11 | + | |
| 12 | + The seed's `Mem` that is too large an offset is marked and the code | |
| 13 | + goes on, since a function that makes an operand has no place to give | |
| 14 | + an error back from; the compile ends with the error shown. *) | |
| 15 | +module X64; | |
| 16 | +import Out, Fmt, Heap, Buf, Str, Diag, Scan, Chunk; | |
| 17 | + | |
| 18 | +const | |
| 19 | + RAX* = 0; RCX* = 1; RDX* = 2; RBX* = 3; RSP* = 4; RBP* = 5; RSI* = 6; RDI* = 7; | |
| 20 | + R8* = 8; R9* = 9; R10* = 10; R11* = 11; R12* = 12; R13* = 13; R14* = 14; R15* = 15; | |
| 21 | + RIP* = -1; (* a base meaning rip-relative, to a chunk *) | |
| 22 | + NoIndex* = -1; | |
| 23 | + | |
| 24 | + (* condition codes *) | |
| 25 | + ccB* = 2; ccAE* = 3; ccE* = 4; ccNE* = 5; ccBE* = 6; ccA* = 7; ccS* = 8; | |
| 26 | + ccP* = 10; ccNP* = 11; ccL* = 12; ccGE* = 13; ccLE* = 14; ccG* = 15; | |
| 27 | + | |
| 28 | + (* group 1 operations, the /digit of 81 and 83 *) | |
| 29 | + aluAdd* = 0; aluOr* = 1; aluAnd* = 4; aluSub* = 5; aluXor* = 6; aluCmp* = 7; | |
| 30 | + | |
| 31 | + (* the /digit of D3 and C1 *) | |
| 32 | + shShl* = 4; shShr* = 5; shSar* = 7; | |
| 33 | + | |
| 34 | + NoLabel* = -1; | |
| 35 | + | |
| 36 | +type | |
| 37 | + Mem* = record | |
| 38 | + base*: INT; (* a register, or RIP *) | |
| 39 | + index*: INT; (* a register, or NoIndex *) | |
| 40 | + scale*: INT; (* 1, 2, 4 or 8 *) | |
| 41 | + disp*: INT; | |
| 42 | + chunk*: INT; (* with RIP: the chunk addressed *) | |
| 43 | + end; | |
| 44 | + | |
| 45 | + (* The parser's reader of an integer constant in a code procedure: a | |
| 46 | + whole expression, or, when whole is false, a single factor *) | |
| 47 | + ConstFn* = proc(whole: BOOL): (I64, ERROR); | |
| 48 | + | |
| 49 | + (* The text of the instruction that starts at at, for the listing *) | |
| 50 | + Said = record | |
| 51 | + at: INT; | |
| 52 | + text: []U8; | |
| 53 | + end; | |
| 54 | + | |
| 55 | +var | |
| 56 | + listing*: BOOL; (* -S: each instruction's text is kept, for list *) | |
| 57 | + said: ^Said; | |
| 58 | + numSaid, saidCap: INT; | |
| 59 | + | |
| 60 | +proc setListing*(yes: BOOL); | |
| 61 | +begin | |
| 62 | + listing := yes; | |
| 63 | +end; | |
| 64 | + | |
| 65 | +(* Operands *) | |
| 66 | + | |
| 67 | +proc mem*(base: INT; disp: I64): Mem; | |
| 68 | +var m: Mem; | |
| 69 | +begin | |
| 70 | + if (disp < -2147483648) or (disp > 2147483647) then | |
| 71 | + _ := Scan.mark("an offset beyond 32 bits"); | |
| 72 | + end; | |
| 73 | + m.base := base; m.index := NoIndex; m.scale := 1; | |
| 74 | + m.disp := INT(disp); m.chunk := -1; | |
| 75 | + return m; | |
| 76 | +end; | |
| 77 | + | |
| 78 | +proc memRip*(chunk: INT; disp: INT): Mem; | |
| 79 | +var m: Mem; | |
| 80 | +begin | |
| 81 | + m.base := RIP; m.index := NoIndex; m.scale := 1; | |
| 82 | + m.disp := disp; m.chunk := chunk; | |
| 83 | + return m; | |
| 84 | +end; | |
| 85 | + | |
| 86 | +proc memIdx*(base, index, scale: INT; disp: I64): Mem; | |
| 87 | +var m: Mem; | |
| 88 | +begin | |
| 89 | + m := mem(base, disp); | |
| 90 | + m.index := index; | |
| 91 | + m.scale := scale; | |
| 92 | + return m; | |
| 93 | +end; | |
| 94 | + | |
| 95 | +(* The address of the code about to be written, as an operand *) | |
| 96 | +proc here*: Mem; | |
| 97 | +begin | |
| 98 | + return memRip(Chunk.curChunk, Chunk.pc - Chunk.chunks[Chunk.curChunk].start); | |
| 99 | +end; | |
| 100 | + | |
| 101 | +(* Encoding. An instruction is: a legacy prefix, REX, the opcode, ModRM, | |
| 102 | + SIB and displacement. Opcodes of more than one byte are given with | |
| 103 | + their first byte in the low eight bits. *) | |
| 104 | + | |
| 105 | +proc opcode(op: INT); | |
| 106 | +begin | |
| 107 | + repeat | |
| 108 | + Chunk.put1(op & 0FFH); | |
| 109 | + op := op shr 8; | |
| 110 | + until op = 0; | |
| 111 | +end; | |
| 112 | + | |
| 113 | +(* The legacy prefix pfx, if any, and REX: wide is REX.W; byte4 says | |
| 114 | + a byte register of 4 to 7 is used, spl to dil, which exist only with | |
| 115 | + a REX prefix. *) | |
| 116 | +proc prefix(pfx: INT; w: BOOL; reg, index, base: INT; byte4: BOOL); | |
| 117 | +var rex: INT; | |
| 118 | +begin | |
| 119 | + if pfx # 0 then Chunk.put1(pfx); end; | |
| 120 | + rex := 0; | |
| 121 | + if w then rex := rex | 8; end; | |
| 122 | + if reg >= 8 then rex := rex | 4; end; | |
| 123 | + if index >= 8 then rex := rex | 2; end; | |
| 124 | + if base >= 8 then rex := rex | 1; end; | |
| 125 | + if (rex # 0) or byte4 then Chunk.put1(40H | rex); end; | |
| 126 | +end; | |
| 127 | + | |
| 128 | +(* op reg, [mem] *) | |
| 129 | +proc opM(pfx: INT; w: BOOL; op, reg: INT; m: Mem; byteReg: BOOL); | |
| 130 | +var md, b, sc: INT; | |
| 131 | +begin | |
| 132 | + byteReg := byteReg and (reg >= 4) and (reg <= 7); | |
| 133 | + if m.base = RIP then | |
| 134 | + prefix(pfx, w, reg, NoIndex, 0, byteReg); | |
| 135 | + else | |
| 136 | + prefix(pfx, w, reg, m.index, m.base, byteReg); | |
| 137 | + end; | |
| 138 | + opcode(op); | |
| 139 | + reg := reg & 7; | |
| 140 | + if m.base = RIP then | |
| 141 | + Chunk.put1((reg shl 3) | 5); | |
| 142 | + (* mod 00 with r/m 101 is rip + disp32. The displacement counts | |
| 143 | + from the end of the instruction, and the fixup takes it to end | |
| 144 | + with these four bytes: an instruction with an immediate after | |
| 145 | + them, aluMI's or storeI's, takes the immediate's size off disp | |
| 146 | + first (the assembler can't name a chunk). *) | |
| 147 | + Chunk.addFixup(Chunk.pc, m.chunk, m.disp); | |
| 148 | + Chunk.put4(0); | |
| 149 | + return; | |
| 150 | + end; | |
| 151 | + b := m.base & 7; | |
| 152 | + (* mod 00 takes no displacement, but with r/m 101 (RBP or R13) it means | |
| 153 | + rip-relative, or disp32 under a SIB byte, so those bases take a | |
| 154 | + zero disp8 *) | |
| 155 | + if (m.disp = 0) and (b # RBP) then | |
| 156 | + md := 0; | |
| 157 | + elsif (m.disp >= -128) and (m.disp <= 127) then | |
| 158 | + md := 1; | |
| 159 | + else | |
| 160 | + md := 2; | |
| 161 | + end; | |
| 162 | + (* r/m 100 (RSP or R12) means a SIB byte follows, so those bases need | |
| 163 | + one, with index 100 meaning none *) | |
| 164 | + if (m.index # NoIndex) or (b = RSP) then | |
| 165 | + Chunk.put1((md shl 6) | (reg shl 3) | 4); | |
| 166 | + if m.scale = 1 then sc := 0; elsif m.scale = 2 then sc := 1; elsif m.scale = 4 then sc := 2; else sc := 3; end; | |
| 167 | + if m.index = NoIndex then | |
| 168 | + Chunk.put1((sc shl 6) | (4 shl 3) | b); | |
| 169 | + else | |
| 170 | + Chunk.put1((sc shl 6) | ((m.index & 7) shl 3) | b); | |
| 171 | + end; | |
| 172 | + else | |
| 173 | + Chunk.put1((md shl 6) | (reg shl 3) | b); | |
| 174 | + end; | |
| 175 | + if md = 1 then | |
| 176 | + Chunk.put1(m.disp); | |
| 177 | + elsif md = 2 then | |
| 178 | + Chunk.put4(m.disp); | |
| 179 | + end; | |
| 180 | +end; | |
| 181 | + | |
| 182 | +(* op reg, rm (both registers) *) | |
| 183 | +proc opR(pfx: INT; w: BOOL; op, reg, rm: INT; byteReg: BOOL); | |
| 184 | +begin | |
| 185 | + prefix(pfx, w, reg, NoIndex, rm, byteReg and (((reg >= 4) and (reg <= 7)) or ((rm >= 4) and (rm <= 7)))); | |
| 186 | + opcode(op); | |
| 187 | + Chunk.put1(0C0H | ((reg & 7) shl 3) | (rm & 7)); | |
| 188 | +end; | |
| 189 | + | |
| 190 | +(* The listing: the text of each instruction, as it is made *) | |
| 191 | + | |
| 192 | +(* How many instructions the listing has, and back to n of them, for | |
| 193 | + code made again *) | |
| 194 | +proc saidCount*: INT; | |
| 195 | +begin | |
| 196 | + return numSaid; | |
| 197 | +end; | |
| 198 | + | |
| 199 | +proc unsay*(n: INT); | |
| 200 | +begin | |
| 201 | + numSaid := n; | |
| 202 | +end; | |
| 203 | + | |
| 204 | +proc sayAt*(at: INT; s: []U8); | |
| 205 | +begin | |
| 206 | + if not listing then return; end; | |
| 207 | + if numSaid = saidCap then | |
| 208 | + said := ^Said(Scan.regrab(^U8(said), numSaid * SIZE(Said), (saidCap * 2 + 1024) * SIZE(Said))); | |
| 209 | + saidCap := saidCap * 2 + 1024; | |
| 210 | + end; | |
| 211 | + said[numSaid].at := at; | |
| 212 | + said[numSaid].text := s; | |
| 213 | + INC(numSaid); | |
| 214 | +end; | |
| 215 | + | |
| 216 | +(* The text of the instruction about to be written *) | |
| 217 | +proc say*(s: []U8); | |
| 218 | +begin | |
| 219 | + sayAt(Chunk.pc, s); | |
| 220 | +end; | |
| 221 | + | |
| 222 | +(* The text of the instruction at at, said again, as when its operand is | |
| 223 | + patched *) | |
| 224 | +proc restate*(at: INT; s: []U8); | |
| 225 | +var i: INT; | |
| 226 | +begin | |
| 227 | + i := numSaid - 1; | |
| 228 | + while (i >= 0) and (said[i].at # at) do DEC(i); end; | |
| 229 | + if i >= 0 then said[i].text := s; end; | |
| 230 | +end; | |
| 231 | + | |
| 232 | +const | |
| 233 | + RegNames: [4][16][]U8 = [ | |
| 234 | + ["rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", | |
| 235 | + "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"], | |
| 236 | + ["eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", | |
| 237 | + "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"], | |
| 238 | + ["ax", "cx", "dx", "bx", "sp", "bp", "si", "di", | |
| 239 | + "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"], | |
| 240 | + ["al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", | |
| 241 | + "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b"]]; | |
| 242 | + CondNames*: [16][]U8 = ["o", "no", "b", "ae", "e", "ne", "be", "a", "s", "ns", "p", "np", "l", "ge", "le", "g"]; | |
| 243 | + AluNames: [8][]U8 = ["add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"]; | |
| 244 | + ShiftNames: [8][]U8 = ["rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar"]; | |
| 245 | + UnaryNames: [8][]U8 = ["", "", "not", "neg", "mul", "imul", "div", "idiv"]; (* from 2 *) | |
| 246 | + | |
| 247 | +(* The name of register r, of size bytes *) | |
| 248 | +proc rnSized(r, size: INT): []U8; | |
| 249 | +begin | |
| 250 | + if size = 1 then return RegNames[3][r]; end; | |
| 251 | + if size = 2 then return RegNames[2][r]; end; | |
| 252 | + if size = 4 then return RegNames[1][r]; end; | |
| 253 | + return RegNames[0][r]; | |
| 254 | +end; | |
| 255 | + | |
| 256 | +proc rn(r: INT): []U8; | |
| 257 | +begin | |
| 258 | + return RegNames[0][r]; | |
| 259 | +end; | |
| 260 | + | |
| 261 | +proc xn(x: INT): []U8; | |
| 262 | +begin | |
| 263 | + return Diag.say("xmm{}", [Fmt.int(x)]); | |
| 264 | +end; | |
| 265 | + | |
| 266 | +(* A memory operand, with the size of what it holds, as the assembler | |
| 267 | + reads it *) | |
| 268 | +proc mnSized(m: Mem; size: INT): []U8; | |
| 269 | +var b: ^Buf.Buf; | |
| 270 | +begin | |
| 271 | + b := ^Buf.Buf(Scan.grab(SIZE(Buf.Buf))); | |
| 272 | + Buf.init(b, Heap.heap); | |
| 273 | + if size = 1 then Buf.str(b, "byte "); | |
| 274 | + elsif size = 2 then Buf.str(b, "word "); | |
| 275 | + elsif size = 4 then Buf.str(b, "dword "); | |
| 276 | + elsif size = 8 then Buf.str(b, "qword "); | |
| 277 | + end; | |
| 278 | + Buf.str(b, "["); | |
| 279 | + if m.base = RIP then Buf.str(b, Chunk.chunkName(m.chunk)); else Buf.str(b, rn(m.base)); end; | |
| 280 | + if m.index # NoIndex then Fmt.write(b, " + {} * {}", [Fmt.str(rn(m.index)), Fmt.int(m.scale)]); end; | |
| 281 | + if m.disp > 0 then | |
| 282 | + Fmt.write(b, " + {}", [Fmt.int(m.disp)]); | |
| 283 | + elsif m.disp < 0 then | |
| 284 | + Fmt.write(b, " - {}", [Fmt.int(-m.disp)]); | |
| 285 | + end; | |
| 286 | + Buf.str(b, "]"); | |
| 287 | + return Buf.bytes(b); | |
| 288 | +end; | |
| 289 | + | |
| 290 | +proc mn(m: Mem): []U8; | |
| 291 | +begin | |
| 292 | + return mnSized(m, 0); | |
| 293 | +end; | |
| 294 | + | |
| 295 | +(* "op a, b" and "op a" *) | |
| 296 | +proc text2(op, a, b: []U8): []U8; | |
| 297 | +begin | |
| 298 | + return Diag.say("{} {}, {}", [Fmt.str(op), Fmt.str(a), Fmt.str(b)]); | |
| 299 | +end; | |
| 300 | + | |
| 301 | +proc text1(op, a: []U8): []U8; | |
| 302 | +begin | |
| 303 | + return Diag.say("{} {}", [Fmt.str(op), Fmt.str(a)]); | |
| 304 | +end; | |
| 305 | + | |
| 306 | +(* v in decimal *) | |
| 307 | +proc num(v: INT): []U8; | |
| 308 | +begin | |
| 309 | + return Diag.say("{}", [Fmt.int(v)]); | |
| 310 | +end; | |
| 311 | + | |
| 312 | +proc movRR*(dst, src: INT); | |
| 313 | +begin | |
| 314 | + if listing then say(text2("mov", rn(dst), rn(src))); end; | |
| 315 | + opR(0, true, 89H, src, dst, false); | |
| 316 | +end; | |
| 317 | + | |
| 318 | +(* The operand-size prefix a size of 2 bytes takes *) | |
| 319 | +proc pfx16(size: INT): INT; | |
| 320 | +begin | |
| 321 | + if size = 2 then return 66H; end; | |
| 322 | + return 0; | |
| 323 | +end; | |
| 324 | + | |
| 325 | +(* The immediate of n bytes that ends an instruction *) | |
| 326 | +proc putImm(v, n: INT); | |
| 327 | +begin | |
| 328 | + if n = 4 then | |
| 329 | + Chunk.put4(v); | |
| 330 | + else | |
| 331 | + Chunk.put1(v & 0FFH); | |
| 332 | + if n = 2 then Chunk.put1((v shr 8) & 0FFH); end; | |
| 333 | + end; | |
| 334 | +end; | |
| 335 | + | |
| 336 | +(* op [m], v, on size bytes; v fits in them, or in 32 bits for 8 *) | |
| 337 | +proc aluMI*(op: INT; m: Mem; v, size: INT); | |
| 338 | +var n: INT; | |
| 339 | +begin | |
| 340 | + if listing then say(text2(AluNames[op], mnSized(m, size), num(v))); end; | |
| 341 | + if (size = 1) or ((v >= -128) and (v <= 127)) then | |
| 342 | + n := 1; | |
| 343 | + elsif size = 2 then | |
| 344 | + n := 2; | |
| 345 | + else | |
| 346 | + n := 4; | |
| 347 | + end; | |
| 348 | + if m.base = RIP then m.disp := m.disp - n; end; | |
| 349 | + if size = 1 then | |
| 350 | + opM(0, false, 80H, op, m, false); | |
| 351 | + elsif n = 1 then | |
| 352 | + opM(pfx16(size), size = 8, 83H, op, m, false); | |
| 353 | + else | |
| 354 | + opM(pfx16(size), size = 8, 81H, op, m, false); | |
| 355 | + end; | |
| 356 | + putImm(v, n); | |
| 357 | +end; | |
| 358 | + | |
| 359 | +(* mov [m], v, of size bytes; v fits in them, or in 32 bits for 8 *) | |
| 360 | +proc storeI*(m: Mem; v, size: INT); | |
| 361 | +var n: INT; | |
| 362 | +begin | |
| 363 | + if listing then say(text2("mov", mnSized(m, size), num(v))); end; | |
| 364 | + n := size; | |
| 365 | + if n > 4 then n := 4; end; | |
| 366 | + if m.base = RIP then m.disp := m.disp - n; end; | |
| 367 | + if size = 1 then | |
| 368 | + opM(0, false, 0C6H, 0, m, false); | |
| 369 | + else | |
| 370 | + opM(pfx16(size), size = 8, 0C7H, 0, m, false); | |
| 371 | + end; | |
| 372 | + putImm(v, n); | |
| 373 | +end; | |
| 374 | + | |
| 375 | +proc movRI*(r: INT; v: I64); | |
| 376 | +begin | |
| 377 | + if listing then | |
| 378 | + if v = 0 then | |
| 379 | + say(text2("xor", rnSized(r, 4), rnSized(r, 4))); | |
| 380 | + else | |
| 381 | + say(text2("mov", rn(r), Diag.say("{}", [Fmt.int(v)]))); | |
| 382 | + end; | |
| 383 | + end; | |
| 384 | + if (v >= -2147483648) and (v <= 2147483647) then | |
| 385 | + if v = 0 then | |
| 386 | + opR(0, false, 31H, r, r, false); (* xor r32, r32 *) | |
| 387 | + elsif v > 0 then (* mov r32, imm32 *) | |
| 388 | + prefix(0, false, 0, NoIndex, r, false); Chunk.put1(0B8H + (r & 7)); Chunk.put4(INT(v)); | |
| 389 | + else | |
| 390 | + opR(0, true, 0C7H, 0, r, false); Chunk.put4(INT(v)); (* sign-extended imm32 *) | |
| 391 | + end; | |
| 392 | + elsif (v > 0) and (v <= 0FFFFFFFFH) then | |
| 393 | + prefix(0, false, 0, NoIndex, r, false); Chunk.put1(0B8H + (r & 7)); Chunk.put4(INT(v)); | |
| 394 | + else | |
| 395 | + prefix(0, true, 0, NoIndex, r, false); Chunk.put1(0B8H + (r & 7)); Chunk.put8(v); | |
| 396 | + end; | |
| 397 | +end; | |
| 398 | + | |
| 399 | +proc load*(r: INT; m: Mem; size: INT; signed: BOOL); | |
| 400 | +begin | |
| 401 | + if listing then | |
| 402 | + if size = 8 then | |
| 403 | + say(text2("mov", rn(r), mn(m))); | |
| 404 | + elsif (size = 4) and not signed then | |
| 405 | + say(text2("mov", rnSized(r, 4), mn(m))); | |
| 406 | + elsif size = 4 then | |
| 407 | + say(text2("movsxd", rn(r), mnSized(m, 4))); | |
| 408 | + elsif signed then | |
| 409 | + say(text2("movsx", rn(r), mnSized(m, size))); | |
| 410 | + else | |
| 411 | + say(text2("movzx", rnSized(r, 4), mnSized(m, size))); | |
| 412 | + end; | |
| 413 | + end; | |
| 414 | + if size = 1 then | |
| 415 | + if signed then opM(0, true, 0BE0FH, r, m, false); else opM(0, false, 0B60FH, r, m, false); end; | |
| 416 | + elsif size = 2 then | |
| 417 | + if signed then opM(0, true, 0BF0FH, r, m, false); else opM(0, false, 0B70FH, r, m, false); end; | |
| 418 | + elsif size = 4 then | |
| 419 | + if signed then opM(0, true, 63H, r, m, false); else opM(0, false, 8BH, r, m, false); end; | |
| 420 | + else | |
| 421 | + opM(0, true, 8BH, r, m, false); | |
| 422 | + end; | |
| 423 | +end; | |
| 424 | + | |
| 425 | +proc store*(m: Mem; r: INT; size: INT); | |
| 426 | +begin | |
| 427 | + if listing then say(text2("mov", mn(m), rnSized(r, size))); end; | |
| 428 | + if size = 1 then | |
| 429 | + opM(0, false, 88H, r, m, true); | |
| 430 | + elsif size = 2 then | |
| 431 | + opM(66H, false, 89H, r, m, false); | |
| 432 | + elsif size = 4 then | |
| 433 | + opM(0, false, 89H, r, m, false); | |
| 434 | + else | |
| 435 | + opM(0, true, 89H, r, m, false); | |
| 436 | + end; | |
| 437 | +end; | |
| 438 | + | |
| 439 | +proc lea*(r: INT; m: Mem); | |
| 440 | +begin | |
| 441 | + if listing then say(text2("lea", rn(r), mn(m))); end; | |
| 442 | + opM(0, true, 8DH, r, m, false); | |
| 443 | +end; | |
| 444 | + | |
| 445 | +proc alu*(op, dst, src: INT); | |
| 446 | +begin | |
| 447 | + if listing then say(text2(AluNames[op], rn(dst), rn(src))); end; | |
| 448 | + opR(0, true, (op shl 3) | 1, src, dst, false); | |
| 449 | +end; | |
| 450 | + | |
| 451 | +proc aluI*(op, r: INT; v: INT); | |
| 452 | +begin | |
| 453 | + if listing then say(text2(AluNames[op], rn(r), num(v))); end; | |
| 454 | + if (v >= -128) and (v <= 127) then | |
| 455 | + opR(0, true, 83H, op, r, false); Chunk.put1(v); | |
| 456 | + else | |
| 457 | + opR(0, true, 81H, op, r, false); Chunk.put4(v); | |
| 458 | + end; | |
| 459 | +end; | |
| 460 | + | |
| 461 | +(* op r, [m] *) | |
| 462 | +proc aluM*(op, r: INT; m: Mem); | |
| 463 | +begin | |
| 464 | + if listing then say(text2(AluNames[op], rn(r), mn(m))); end; | |
| 465 | + opM(0, true, (op shl 3) | 3, r, m, false); | |
| 466 | +end; | |
| 467 | + | |
| 468 | +proc imul*(dst, src: INT); | |
| 469 | +begin | |
| 470 | + if listing then say(text2("imul", rn(dst), rn(src))); end; | |
| 471 | + opR(0, true, 0AF0FH, dst, src, false); | |
| 472 | +end; | |
| 473 | + | |
| 474 | +proc imulI*(r: INT; v: INT); | |
| 475 | +begin | |
| 476 | + if listing then say(Diag.say("imul {}, {}, {}", [Fmt.str(rn(r)), Fmt.str(rn(r)), Fmt.int(v)])); end; | |
| 477 | + opR(0, true, 69H, r, r, false); | |
| 478 | + Chunk.put4(v); | |
| 479 | +end; | |
| 480 | + | |
| 481 | +proc shift*(op, r: INT); | |
| 482 | +begin | |
| 483 | + if listing then say(text2(ShiftNames[op], rn(r), "cl")); end; | |
| 484 | + opR(0, true, 0D3H, op, r, false); | |
| 485 | +end; | |
| 486 | + | |
| 487 | +proc shiftI*(op, r, n: INT); | |
| 488 | +begin | |
| 489 | + if listing then say(text2(ShiftNames[op], rn(r), num(n))); end; | |
| 490 | + opR(0, true, 0C1H, op, r, false); | |
| 491 | + Chunk.put1(n); | |
| 492 | +end; | |
| 493 | + | |
| 494 | +(* F7 /ext: 2 not, 3 neg, 6 div, 7 idiv *) | |
| 495 | +proc unary*(ext, r: INT); | |
| 496 | +begin | |
| 497 | + if listing then say(text1(UnaryNames[ext], rn(r))); end; | |
| 498 | + opR(0, true, 0F7H, ext, r, false); | |
| 499 | +end; | |
| 500 | + | |
| 501 | +proc cqo*; | |
| 502 | +begin | |
| 503 | + if listing then say("cqo"); end; | |
| 504 | + Chunk.put1(48H); Chunk.put1(99H); | |
| 505 | +end; | |
| 506 | + | |
| 507 | +(* Brings a value of size bytes in r to 64 bits, by its sign or zeros. *) | |
| 508 | +proc extend*(r, size: INT; signed: BOOL); | |
| 509 | +begin | |
| 510 | + if listing then | |
| 511 | + if size = 4 then | |
| 512 | + if signed then | |
| 513 | + say(text2("movsxd", rn(r), rnSized(r, 4))); | |
| 514 | + else | |
| 515 | + say(text2("mov", rnSized(r, 4), rnSized(r, 4))); | |
| 516 | + end; | |
| 517 | + elsif signed then | |
| 518 | + say(text2("movsx", rn(r), rnSized(r, size))); | |
| 519 | + else | |
| 520 | + say(text2("movzx", rnSized(r, 4), rnSized(r, size))); | |
| 521 | + end; | |
| 522 | + end; | |
| 523 | + if size = 1 then | |
| 524 | + if signed then opR(0, true, 0BE0FH, r, r, true); else opR(0, false, 0B60FH, r, r, true); end; | |
| 525 | + elsif size = 2 then | |
| 526 | + if signed then opR(0, true, 0BF0FH, r, r, false); else opR(0, false, 0B70FH, r, r, false); end; | |
| 527 | + elsif size = 4 then | |
| 528 | + if signed then opR(0, true, 63H, r, r, false); else opR(0, false, 89H, r, r, false); end; | |
| 529 | + end; | |
| 530 | +end; | |
| 531 | + | |
| 532 | +(* r := 1 if the condition holds, else 0 *) | |
| 533 | +proc setCC*(cc, r: INT); | |
| 534 | +begin | |
| 535 | + if listing then say(text1(Diag.say("set{}", [Fmt.str(CondNames[cc])]), rnSized(r, 1))); end; | |
| 536 | + opR(0, false, 900FH + (cc shl 8), 0, r, true); | |
| 537 | + if listing then say(text2("movzx", rnSized(r, 4), rnSized(r, 1))); end; | |
| 538 | + opR(0, false, 0B60FH, r, r, true); | |
| 539 | +end; | |
| 540 | + | |
| 541 | +proc testRR*(a, b: INT); | |
| 542 | +begin | |
| 543 | + if listing then say(text2("test", rn(a), rn(b))); end; | |
| 544 | + opR(0, true, 85H, b, a, false); | |
| 545 | +end; | |
| 546 | + | |
| 547 | +proc push*(r: INT); | |
| 548 | +begin | |
| 549 | + if listing then say(text1("push", rn(r))); end; | |
| 550 | + if r >= 8 then Chunk.put1(41H); end; | |
| 551 | + Chunk.put1(50H + (r & 7)); | |
| 552 | +end; | |
| 553 | + | |
| 554 | +proc pop*(r: INT); | |
| 555 | +begin | |
| 556 | + if listing then say(text1("pop", rn(r))); end; | |
| 557 | + if r >= 8 then Chunk.put1(41H); end; | |
| 558 | + Chunk.put1(58H + (r & 7)); | |
| 559 | +end; | |
| 560 | + | |
| 561 | +proc callChunk*(c: INT); | |
| 562 | +begin | |
| 563 | + if listing then say(text1("call", Chunk.chunkName(c))); end; | |
| 564 | + Chunk.put1(0E8H); | |
| 565 | + Chunk.addFixup(Chunk.pc, c, 0); | |
| 566 | + Chunk.put4(0); | |
| 567 | +end; | |
| 568 | + | |
| 569 | +proc callBack*(target: INT); | |
| 570 | +begin | |
| 571 | + if listing then say("call"); end; | |
| 572 | + Chunk.put1(0E8H); | |
| 573 | + Chunk.put4(target - (Chunk.pc + 4)); | |
| 574 | +end; | |
| 575 | + | |
| 576 | +proc callR*(r: INT); | |
| 577 | +begin | |
| 578 | + if listing then say(text1("call", rn(r))); end; | |
| 579 | + opR(0, false, 0FFH, 2, r, false); | |
| 580 | +end; | |
| 581 | + | |
| 582 | +proc jmpR*(r: INT); | |
| 583 | +begin | |
| 584 | + if listing then say(text1("jmp", rn(r))); end; | |
| 585 | + opR(0, false, 0FFH, 4, r, false); | |
| 586 | +end; | |
| 587 | + | |
| 588 | +proc callM*(m: Mem); | |
| 589 | +begin | |
| 590 | + if listing then say(text1("call", mn(m))); end; | |
| 591 | + opM(0, false, 0FFH, 2, m, false); | |
| 592 | +end; | |
| 593 | + | |
| 594 | +proc ret*; | |
| 595 | +begin | |
| 596 | + if listing then say("ret"); end; | |
| 597 | + Chunk.put1(0C3H); | |
| 598 | +end; | |
| 599 | + | |
| 600 | +proc leave*; | |
| 601 | +begin | |
| 602 | + say("leave"); Chunk.put1(0C9H); | |
| 603 | +end; | |
| 604 | + | |
| 605 | +proc syscall*; | |
| 606 | +begin | |
| 607 | + say("syscall"); Chunk.put1(0FH); Chunk.put1(05H); | |
| 608 | +end; | |
| 609 | + | |
| 610 | +proc mfence*; | |
| 611 | +begin | |
| 612 | + say("mfence"); Chunk.put1(0FH); Chunk.put1(0AEH); Chunk.put1(0F0H); | |
| 613 | +end; | |
| 614 | + | |
| 615 | +proc ud2*; | |
| 616 | +begin | |
| 617 | + say("ud2"); Chunk.put1(0FH); Chunk.put1(0BH); | |
| 618 | +end; | |
| 619 | + | |
| 620 | +(* Code made up to a multiple of n bytes with no-ops, the fewest: the | |
| 621 | + long forms of nop, of up to 8 bytes, that x86 decodes as one *) | |
| 622 | +proc align*(n: INT); | |
| 623 | +const | |
| 624 | + Nops: [8][]U8 = ["\x90", "\x66\x90", "\x0F\x1F\x00", "\x0F\x1F\x40\x00", | |
| 625 | + "\x0F\x1F\x44\x00\x00", "\x66\x0F\x1F\x44\x00\x00", "\x0F\x1F\x80\x00\x00\x00\x00", | |
| 626 | + "\x0F\x1F\x84\x00\x00\x00\x00\x00"]; | |
| 627 | +var k, i: INT; | |
| 628 | +begin | |
| 629 | + while Chunk.pc mod n # 0 do | |
| 630 | + k := n - Chunk.pc mod n; | |
| 631 | + if k > 8 then k := 8; end; | |
| 632 | + say("nop"); | |
| 633 | + for i := 0 to k - 1 do Chunk.put1(INT(Nops[k - 1][i])); end; | |
| 634 | + end; | |
| 635 | +end; | |
| 636 | + | |
| 637 | +proc repMovsb*; | |
| 638 | +begin | |
| 639 | + say("rep movsb"); Chunk.put1(0F3H); Chunk.put1(0A4H); | |
| 640 | +end; | |
| 641 | + | |
| 642 | +(* the direction flag, which has rep movsb go down, and back up *) | |
| 643 | +proc std*; | |
| 644 | +begin | |
| 645 | + say("std"); Chunk.put1(0FDH); | |
| 646 | +end; | |
| 647 | + | |
| 648 | +proc cld*; | |
| 649 | +begin | |
| 650 | + say("cld"); Chunk.put1(0FCH); | |
| 651 | +end; | |
| 652 | + | |
| 653 | +proc repStosb*; | |
| 654 | +begin | |
| 655 | + say("rep stosb"); Chunk.put1(0F3H); Chunk.put1(0AAH); | |
| 656 | +end; | |
| 657 | + | |
| 658 | +proc repeCmpsb*; | |
| 659 | +begin | |
| 660 | + say("repe cmpsb"); Chunk.put1(0F3H); Chunk.put1(0A6H); | |
| 661 | +end; | |
| 662 | + | |
| 663 | +proc lockCmpxchg*(m: Mem; r: INT); | |
| 664 | +begin | |
| 665 | + if listing then say(text2("lock cmpxchg", mn(m), rn(r))); end; | |
| 666 | + Chunk.put1(0F0H); | |
| 667 | + opM(0, true, 0B10FH, r, m, false); | |
| 668 | +end; | |
| 669 | + | |
| 670 | +proc xchg*(m: Mem; r: INT); | |
| 671 | +begin | |
| 672 | + if listing then say(text2("xchg", mn(m), rn(r))); end; | |
| 673 | + opM(0, true, 87H, r, m, false); | |
| 674 | +end; | |
| 675 | + | |
| 676 | +proc lockXadd*(m: Mem; r: INT); | |
| 677 | +begin | |
| 678 | + if listing then say(text2("lock xadd", mn(m), rn(r))); end; | |
| 679 | + Chunk.put1(0F0H); | |
| 680 | + opM(0, true, 0C10FH, r, m, false); | |
| 681 | +end; | |
| 682 | + | |
| 683 | +proc movqXR*(x, r: INT); | |
| 684 | +begin | |
| 685 | + if listing then say(text2("movq", xn(x), rn(r))); end; | |
| 686 | + opR(66H, true, 6E0FH, x, r, false); | |
| 687 | +end; | |
| 688 | + | |
| 689 | +proc movqRX*(r, x: INT); | |
| 690 | +begin | |
| 691 | + if listing then say(text2("movq", rn(r), xn(x))); end; | |
| 692 | + opR(66H, true, 7E0FH, x, r, false); | |
| 693 | +end; | |
| 694 | + | |
| 695 | +(* movaps: all of xmm y to xmm x, the shortest move between two *) | |
| 696 | +proc movX*(x, y: INT); | |
| 697 | +begin | |
| 698 | + if listing then say(text2("movaps", xn(x), xn(y))); end; | |
| 699 | + opR(0, false, 280FH, x, y, false); | |
| 700 | +end; | |
| 701 | + | |
| 702 | +(* The prefix of a real instruction, and the end of its name: sd, or ss for single *) | |
| 703 | +proc realPfx(single: BOOL): INT; | |
| 704 | +begin | |
| 705 | + if single then return 0F3H; end; | |
| 706 | + return 0F2H; | |
| 707 | +end; | |
| 708 | + | |
| 709 | +proc realEnd(single: BOOL): []U8; | |
| 710 | +begin | |
| 711 | + if single then return "ss"; end; | |
| 712 | + return "sd"; | |
| 713 | +end; | |
| 714 | + | |
| 715 | +(* The real instructions take single: F32 (ss) rather than F64 (sd) *) | |
| 716 | +proc movsXM*(x: INT; m: Mem; single: BOOL); | |
| 717 | +begin | |
| 718 | + if listing then say(text2(Diag.say("movs{}", [Fmt.str(realEnd(single)[1..2])]), xn(x), mn(m))); end; | |
| 719 | + opM(realPfx(single), false, 100FH, x, m, false); | |
| 720 | +end; | |
| 721 | + | |
| 722 | +proc movsMX*(m: Mem; x: INT; single: BOOL); | |
| 723 | +begin | |
| 724 | + if listing then say(text2(Diag.say("movs{}", [Fmt.str(realEnd(single)[1..2])]), mn(m), xn(x))); end; | |
| 725 | + opM(realPfx(single), false, 110FH, x, m, false); | |
| 726 | +end; | |
| 727 | + | |
| 728 | +proc sseName(op: INT; single: BOOL): []U8; | |
| 729 | +begin | |
| 730 | + if op = 58H then return Diag.say("add{}", [Fmt.str(realEnd(single))]); end; | |
| 731 | + if op = 59H then return Diag.say("mul{}", [Fmt.str(realEnd(single))]); end; | |
| 732 | + if op = 5CH then return Diag.say("sub{}", [Fmt.str(realEnd(single))]); end; | |
| 733 | + if op = 5EH then return Diag.say("div{}", [Fmt.str(realEnd(single))]); end; | |
| 734 | + return Diag.say("cvt{}2{}", [Fmt.str(realEnd(single)), Fmt.str(realEnd(not single))]); | |
| 735 | +end; | |
| 736 | + | |
| 737 | +(* 58 add, 59 mul, 5C sub, 5E div, 5A convert *) | |
| 738 | +proc sse*(op, x, y: INT; single: BOOL); | |
| 739 | +begin | |
| 740 | + if listing then say(text2(sseName(op, single), xn(x), xn(y))); end; | |
| 741 | + opR(realPfx(single), false, 0FH | (op shl 8), x, y, false); | |
| 742 | +end; | |
| 743 | + | |
| 744 | +(* The same, its right operand in memory: addsd x, [m] *) | |
| 745 | +proc sseM*(op, x: INT; m: Mem; single: BOOL); | |
| 746 | +begin | |
| 747 | + if listing then say(text2(sseName(op, single), xn(x), mn(m))); end; | |
| 748 | + opM(realPfx(single), false, 0FH | (op shl 8), x, m, false); | |
| 749 | +end; | |
| 750 | + | |
| 751 | +proc ucomis*(x, y: INT; single: BOOL); | |
| 752 | +begin | |
| 753 | + if listing then say(text2(Diag.say("ucomi{}", [Fmt.str(realEnd(single))]), xn(x), xn(y))); end; | |
| 754 | + if single then opR(0, false, 2E0FH, x, y, false); else opR(66H, false, 2E0FH, x, y, false); end; | |
| 755 | +end; | |
| 756 | + | |
| 757 | +proc cvtsi2s*(x, r: INT; single: BOOL); | |
| 758 | +begin | |
| 759 | + if listing then say(text2(Diag.say("cvtsi2{}", [Fmt.str(realEnd(single))]), xn(x), rn(r))); end; | |
| 760 | + opR(realPfx(single), true, 2A0FH, x, r, false); | |
| 761 | +end; | |
| 762 | + | |
| 763 | +proc cvtts2si*(r, x: INT; single: BOOL); | |
| 764 | +begin | |
| 765 | + if listing then say(text2(Diag.say("cvtt{}2si", [Fmt.str(realEnd(single))]), rn(r), xn(x))); end; | |
| 766 | + opR(realPfx(single), true, 2C0FH, r, x, false); | |
| 767 | +end; | |
| 768 | + | |
| 769 | +proc btc*(r, bit: INT); | |
| 770 | +begin | |
| 771 | + if listing then say(text2("btc", rn(r), num(bit))); end; | |
| 772 | + opR(0, true, 0BA0FH, 7, r, false); | |
| 773 | + Chunk.put1(bit); | |
| 774 | +end; | |
| 775 | + | |
| 776 | +(* Jumps. A label not yet placed is the chain of the jumps to it, each | |
| 777 | + rel32 field holding the position of the one before, NoLabel ending | |
| 778 | + it. Placing the label walks the chain and patches each. *) | |
| 779 | + | |
| 780 | +proc jmp*(var L: INT); | |
| 781 | +begin | |
| 782 | + if listing then say("jmp"); end; | |
| 783 | + Chunk.put1(0E9H); | |
| 784 | + Chunk.put4(L); | |
| 785 | + L := Chunk.pc - 4; | |
| 786 | +end; | |
| 787 | + | |
| 788 | +proc jcc*(cc: INT; var L: INT); | |
| 789 | +begin | |
| 790 | + if listing then say(Diag.say("j{}", [Fmt.str(CondNames[cc])])); end; | |
| 791 | + Chunk.put1(0FH); Chunk.put1(80H + cc); | |
| 792 | + Chunk.put4(L); | |
| 793 | + L := Chunk.pc - 4; | |
| 794 | +end; | |
| 795 | + | |
| 796 | +proc jmpBack*(target: INT); | |
| 797 | +begin | |
| 798 | + if listing then say("jmp"); end; | |
| 799 | + Chunk.put1(0E9H); | |
| 800 | + Chunk.put4(target - (Chunk.pc + 4)); | |
| 801 | +end; | |
| 802 | + | |
| 803 | +proc jccBack*(cc, target: INT); | |
| 804 | +begin | |
| 805 | + if listing then say(Diag.say("j{}", [Fmt.str(CondNames[cc])])); end; | |
| 806 | + Chunk.put1(0FH); Chunk.put1(80H + cc); | |
| 807 | + Chunk.put4(target - (Chunk.pc + 4)); | |
| 808 | +end; | |
| 809 | + | |
| 810 | +proc fixLinkTo*(L, target: INT); | |
| 811 | +var next: INT; | |
| 812 | +begin | |
| 813 | + while L # NoLabel do | |
| 814 | + next := Chunk.get4At(L); | |
| 815 | + Chunk.put4At(L, target - (L + 4)); | |
| 816 | + L := next; | |
| 817 | + end; | |
| 818 | +end; | |
| 819 | + | |
| 820 | +proc fixLink*(L: INT); | |
| 821 | +begin | |
| 822 | + fixLinkTo(L, Chunk.pc); | |
| 823 | +end; | |
| 824 | + | |
| 825 | +(* If the last instruction made is a jmp to L, it is taken back, since L | |
| 826 | + is to be placed just after it: a return at the end of a procedure | |
| 827 | + needn't jump to the code that returns. L is the place of the latest | |
| 828 | + jump's 4-byte field, so the last instruction is that jump when L is | |
| 829 | + 4 bytes back and an E9 byte (jmp) is before it; a jcc's field follows | |
| 830 | + the bytes 0F 8x, never E9. L then becomes the next jump on the chain, | |
| 831 | + which the field held. The caller must place L next, before anything | |
| 832 | + can have noted where the jmp ended. *) | |
| 833 | +proc dropJumpTo*(var L: INT); | |
| 834 | +begin | |
| 835 | + if (L = NoLabel) or (L # Chunk.pc - 4) or (Chunk.text[L - 1] # 0E9H) then return; end; | |
| 836 | + L := Chunk.get4At(L); | |
| 837 | + Chunk.takeBack(5); | |
| 838 | + if listing and (numSaid > 0) and (said[numSaid - 1].at = Chunk.pc) then DEC(numSaid); end; | |
| 839 | +end; | |
| 840 | + | |
| 841 | +(* The jumps of chains L1 and L2, as one chain: each jump's field holds | |
| 842 | + the next one's place until it is fixed, the last NoLabel *) | |
| 843 | +proc joinLinks*(L1, L2: INT): INT; | |
| 844 | +var L: INT; | |
| 845 | +begin | |
| 846 | + if L2 = NoLabel then return L1; end; | |
| 847 | + if L1 # NoLabel then | |
| 848 | + L := L2; | |
| 849 | + while Chunk.get4At(L) # NoLabel do L := Chunk.get4At(L); end; | |
| 850 | + Chunk.put4At(L, L1); | |
| 851 | + end; | |
| 852 | + return L2; | |
| 853 | +end; | |
| 854 | + | |
| 855 | +(* The assembler, for code procedures: x86-64 in Intel's syntax as NASM | |
| 856 | + writes it (report, appendix A). It reads instructions with the | |
| 857 | + scanner up to end, and encodes them with opM and opR; constants are | |
| 858 | + read by the parser's constExpr. *) | |
| 859 | + | |
| 860 | +const | |
| 861 | + NoBase = -3; (* a memory operand's base, not yet read *) | |
| 862 | + | |
| 863 | +type | |
| 864 | + OpKind = (oReg, oXmm, oMem, oImm); | |
| 865 | + Operand = record | |
| 866 | + kind: OpKind; | |
| 867 | + r, size: INT; (* oReg, oXmm: the register and its size; oMem: its size, or 0 *) | |
| 868 | + m: Mem; | |
| 869 | + v: I64; (* oImm *) | |
| 870 | + end; | |
| 871 | + | |
| 872 | +(* The current symbol as a word: an identifier, or a reserved word that | |
| 873 | + is also an instruction *) | |
| 874 | +proc asmWord: []U8; | |
| 875 | +begin | |
| 876 | + match Scan.tok of | |
| 877 | + | sIdent: return Scan.nameOf(Scan.id); | |
| 878 | + | sAnd: return "and"; | |
| 879 | + | sOr: return "or"; | |
| 880 | + | sXor: return "xor"; | |
| 881 | + | sNot: return "not"; | |
| 882 | + | sDiv: return "div"; | |
| 883 | + | sShl: return "shl"; | |
| 884 | + | sShr: return "shr"; | |
| 885 | + | else: return ""; | |
| 886 | + end; | |
| 887 | +end; | |
| 888 | + | |
| 889 | +(* Whether w names a register: rax, eax, ax, al and so on, or xmm0 to | |
| 890 | + xmm15 (size 16); r and size are set if so *) | |
| 891 | +proc isReg(w: []U8; r, size: ^INT): BOOL; | |
| 892 | +const Sizes: [4]INT = [8, 4, 2, 1]; | |
| 893 | +var i, j, n, d: INT; | |
| 894 | +begin | |
| 895 | + for i := 0 to 3 do | |
| 896 | + for j := 0 to 15 do | |
| 897 | + if RegNames[i][j] = w then r^ := j; size^ := Sizes[i]; return true; end; | |
| 898 | + end; | |
| 899 | + end; | |
| 900 | + size^ := 16; | |
| 901 | + if not Str.hasPrefix(w, "xmm") or (LEN(w) <= 3) then return false; end; | |
| 902 | + (* the seed read the two bytes after xmm as a number, whatever follows *) | |
| 903 | + n := LEN(w) - 3; | |
| 904 | + if n > 2 then n := 2; end; | |
| 905 | + r^ := 0; | |
| 906 | + for i := 0 to n - 1 do | |
| 907 | + d := INT(w[3 + i]) - 48; | |
| 908 | + if (d < 0) or (d > 9) then return false; end; | |
| 909 | + r^ := r^ * 10 + d; | |
| 910 | + end; | |
| 911 | + return r^ <= 15; | |
| 912 | +end; | |
| 913 | + | |
| 914 | +(* Which word of list, a list of words each with a space either side, | |
| 915 | + w is, from 0; -1 if none *) | |
| 916 | +proc wordIndex(w: []U8; list: []U8): INT; | |
| 917 | +var i, n, start: INT; | |
| 918 | +begin | |
| 919 | + n := 0; | |
| 920 | + start := 1; | |
| 921 | + for i := 1 to LEN(list) - 1 do | |
| 922 | + if list[i] = ' ' then | |
| 923 | + if (i - start = LEN(w)) and (list[start..i] = w) then return n; end; | |
| 924 | + INC(n); | |
| 925 | + start := i + 1; | |
| 926 | + end; | |
| 927 | + end; | |
| 928 | + return -1; | |
| 929 | +end; | |
| 930 | + | |
| 931 | +(* w from byte from on, at most n bytes of it, as copy does *) | |
| 932 | +proc part(w: []U8; from, n: INT): []U8; | |
| 933 | +var e: INT; | |
| 934 | +begin | |
| 935 | + e := from + n; | |
| 936 | + if e > LEN(w) then e := LEN(w); end; | |
| 937 | + if from >= e then return ""; end; | |
| 938 | + return w[from..e]; | |
| 939 | +end; | |
| 940 | + | |
| 941 | +(* The condition a name ends in: e of je, nz of jnz *) | |
| 942 | +proc cond(w: []U8): INT; | |
| 943 | +const | |
| 944 | + Alias: [12][]U8 = ["c", "nae", "nc", "nb", "z", "nz", "na", "nbe", "nge", "nl", "ng", "nle"]; | |
| 945 | + AliasCc: [12]INT = [2, 2, 3, 3, 4, 5, 6, 7, 12, 13, 14, 15]; | |
| 946 | +var i: INT; | |
| 947 | +begin | |
| 948 | + for i := 0 to 15 do | |
| 949 | + if CondNames[i] = w then return i; end; | |
| 950 | + end; | |
| 951 | + for i := 0 to 11 do | |
| 952 | + if Alias[i] = w then return AliasCc[i]; end; | |
| 953 | + end; | |
| 954 | + return -1; | |
| 955 | +end; | |
| 956 | + | |
| 957 | +proc operand(o: ^Operand; constExpr: ConstFn): ERROR; | |
| 958 | +var w: []U8; r, size, sz: INT; st: Scan.State; v: I64; isR, wasMinus: BOOL; | |
| 959 | +begin | |
| 960 | + o^ := {}; | |
| 961 | + w := asmWord(); | |
| 962 | + sz := 0; | |
| 963 | + if w = "byte" then sz := 1; | |
| 964 | + elsif w = "word" then sz := 2; | |
| 965 | + elsif w = "dword" then sz := 4; | |
| 966 | + elsif w = "qword" then sz := 8; | |
| 967 | + end; | |
| 968 | + if sz # 0 then Scan.next; w := asmWord(); end; | |
| 969 | + if Scan.tok = Scan.sLbrak then | |
| 970 | + (* [base + index * scale + disp], the displacement last *) | |
| 971 | + Scan.next; | |
| 972 | + o^.kind := oMem; o^.size := sz; | |
| 973 | + o^.m := mem(NoBase, 0); | |
| 974 | + repeat | |
| 975 | + if isReg(asmWord(), ADR(r), ADR(size)) and (size = 8) then | |
| 976 | + Scan.next; | |
| 977 | + (* the scale is a single number or constant: a whole expression | |
| 978 | + would take in the displacement after it, as 4 + 16 in | |
| 979 | + [rdi + rsi * 4 + 16] *) | |
| 980 | + if Scan.tok = Scan.sTimes then | |
| 981 | + Scan.next; | |
| 982 | + o^.m.index := r; | |
| 983 | + v := try constExpr(false); | |
| 984 | + o^.m.scale := INT(v); | |
| 985 | + elsif o^.m.base = NoBase then | |
| 986 | + o^.m.base := r; | |
| 987 | + else | |
| 988 | + o^.m.index := r; o^.m.scale := 1; | |
| 989 | + end; | |
| 990 | + else | |
| 991 | + v := try constExpr(true); | |
| 992 | + o^.m.disp := o^.m.disp + INT(v); | |
| 993 | + end; | |
| 994 | + if (Scan.tok # Scan.sPlus) and (Scan.tok # Scan.sMinus) then break; end; | |
| 995 | + wasMinus := Scan.tok = Scan.sMinus; | |
| 996 | + Scan.saveScan(st); | |
| 997 | + Scan.next; | |
| 998 | + isR := isReg(asmWord(), ADR(r), ADR(size)); | |
| 999 | + if not isR and wasMinus then Scan.restoreScan(st); end; | |
| 1000 | + until false; | |
| 1001 | + if (Scan.tok # Scan.sRbrak) or (o^.m.base = NoBase) | |
| 1002 | + or not ((o^.m.scale = 1) or (o^.m.scale = 2) or (o^.m.scale = 4) or (o^.m.scale = 8)) then | |
| 1003 | + return Scan.mark("expected a memory operand, [base + index * scale + disp]"); | |
| 1004 | + end; | |
| 1005 | + Scan.next; | |
| 1006 | + elsif isReg(w, ADR(o^.r), ADR(o^.size)) then | |
| 1007 | + if o^.size = 16 then o^.kind := oXmm; else o^.kind := oReg; end; | |
| 1008 | + Scan.next; | |
| 1009 | + else | |
| 1010 | + o^.kind := oImm; | |
| 1011 | + o^.v := try constExpr(true); | |
| 1012 | + end; | |
| 1013 | + return nil; | |
| 1014 | +end; | |
| 1015 | + | |
| 1016 | +(* op with register reg and the register or memory operand o *) | |
| 1017 | +proc rm(pfx: INT; w: BOOL; op, reg: INT; o: Operand; byteReg: BOOL); | |
| 1018 | +begin | |
| 1019 | + if o.kind = oMem then opM(pfx, w, op, reg, o.m, byteReg); else opR(pfx, w, op, reg, o.r, byteReg); end; | |
| 1020 | +end; | |
| 1021 | + | |
| 1022 | +proc putImmV(v: I64; size: INT); | |
| 1023 | +begin | |
| 1024 | + if size = 1 then | |
| 1025 | + Chunk.put1(INT(v & 0FFH)); | |
| 1026 | + elsif size = 2 then | |
| 1027 | + Chunk.put1(INT(v & 0FFH)); Chunk.put1(INT((v shr 8) & 0FFH)); | |
| 1028 | + else | |
| 1029 | + Chunk.put4(INT(v & 0FFFFFFFFH)); | |
| 1030 | + end; | |
| 1031 | +end; | |
| 1032 | + | |
| 1033 | +const | |
| 1034 | + Alus = " add or adc sbb and sub xor cmp "; | |
| 1035 | + Unaries = " not neg mul imul div idiv "; | |
| 1036 | + Shifts = " rol ror rcl rcr shl shr sal sar "; | |
| 1037 | + SseOps = " sqrt add mul sub min div max "; | |
| 1038 | + SseCodes: [7]INT = [51H, 58H, 59H, 5CH, 5DH, 5EH, 5FH]; | |
| 1039 | + (* SSE2 integer instructions that work on 16 bytes at a time: 66 0F code, | |
| 1040 | + xmm and xmm or memory (pshufd also takes a byte) *) | |
| 1041 | + PackedOps = " punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb "; | |
| 1042 | + PackedCodes: [10]INT = [60H, 61H, 74H, 70H, 0DBH, 0DFH, 0EBH, 0EFH, 0FCH, 0F8H]; | |
| 1043 | + (* bit scans and counts: reg, reg or memory, with the prefix and code that tell them *) | |
| 1044 | + BitOps = " bsf bsr tzcnt lzcnt popcnt "; | |
| 1045 | + BitPfx: [5]INT = [0, 0, 0F3H, 0F3H, 0F3H]; | |
| 1046 | + BitCodes: [5]INT = [0BC0FH, 0BD0FH, 0BC0FH, 0BD0FH, 0B80FH]; | |
| 1047 | + Plain: [15][]U8 = ["ret", "leave", "syscall", "cpuid", "rdtsc", | |
| 1048 | + "pause", "nop", "ud2", "int3", "hlt", "cqo", "cdq", "mfence", "lfence", "sfence"]; | |
| 1049 | + PlainOp: [15]INT = [0C3H, 0C9H, 050FH, 0A20FH, 310FH, | |
| 1050 | + 90F3H, 90H, 0B0FH, 0CCH, 0F4H, 9948H, 99H, 0F0AE0FH, 0E8AE0FH, 0F8AE0FH]; | |
| 1051 | + | |
| 1052 | +var | |
| 1053 | + (* the instruction being assembled *) | |
| 1054 | + aw: []U8; (* its word *) | |
| 1055 | + ao: [3]Operand; (* its operands *) | |
| 1056 | + an: INT; (* how many *) | |
| 1057 | + asize, apfx: INT; | |
| 1058 | + awide, abyte: BOOL; | |
| 1059 | + aat: Diag.Pos; (* where it starts *) | |
| 1060 | + (* the labels of the procedure *) | |
| 1061 | + lnames, lplaces, lchains: [64]INT; | |
| 1062 | + lusedAt: [64]Diag.Pos; | |
| 1063 | + numLabels: INT; | |
| 1064 | + | |
| 1065 | +(* The label called n, entered if new *) | |
| 1066 | +proc lab(n: INT): (INT, ERROR); | |
| 1067 | +var i: INT; | |
| 1068 | +begin | |
| 1069 | + for i := 0 to numLabels - 1 do | |
| 1070 | + if lnames[i] = n then return i, nil; end; | |
| 1071 | + end; | |
| 1072 | + if numLabels > 63 then return 0, Scan.mark("more than 64 labels"); end; | |
| 1073 | + lnames[numLabels] := n; lplaces[numLabels] := -1; lchains[numLabels] := NoLabel; | |
| 1074 | + lusedAt[numLabels] := Scan.tokPos(); | |
| 1075 | + numLabels := numLabels + 1; | |
| 1076 | + return numLabels - 1, nil; | |
| 1077 | +end; | |
| 1078 | + | |
| 1079 | +proc bad: ERROR; | |
| 1080 | +begin | |
| 1081 | + return Scan.markAt(aat, Diag.say("can't assemble {} with these operands", [Fmt.str(aw)])); | |
| 1082 | +end; | |
| 1083 | + | |
| 1084 | +(* an instruction of two operands: a register or memory, and a | |
| 1085 | + register (opcode toRM), the other way (fromRM), or an immediate | |
| 1086 | + (opImm /digit, or none if opImm < 0) *) | |
| 1087 | +proc two(toRM, fromRM, opImm, digit: INT): ERROR; | |
| 1088 | +var b: INT; | |
| 1089 | +begin | |
| 1090 | + b := 0; | |
| 1091 | + if abyte then b := 1; end; | |
| 1092 | + if an # 2 then return bad(); end; | |
| 1093 | + if ao[1].kind = oImm then | |
| 1094 | + if (ao[0].kind = oImm) or (opImm < 0) then return bad(); end; | |
| 1095 | + if (opImm = 81H) and not abyte and (ao[1].v >= -128) and (ao[1].v <= 127) then | |
| 1096 | + rm(apfx, awide, 83H, digit, ao[0], false); (* a byte, sign-extended *) | |
| 1097 | + Chunk.put1(INT(ao[1].v & 0FFH)); | |
| 1098 | + else | |
| 1099 | + rm(apfx, awide, opImm - b, digit, ao[0], false); | |
| 1100 | + putImmV(ao[1].v, asize); | |
| 1101 | + end; | |
| 1102 | + elsif (ao[1].kind = oReg) and ((ao[0].kind = oReg) or (ao[0].kind = oMem)) then | |
| 1103 | + rm(apfx, awide, toRM - b, ao[1].r, ao[0], abyte); | |
| 1104 | + elsif (ao[0].kind = oReg) and (ao[1].kind = oMem) then | |
| 1105 | + rm(apfx, awide, fromRM - b, ao[0].r, ao[1], abyte); | |
| 1106 | + else | |
| 1107 | + return bad(); | |
| 1108 | + end; | |
| 1109 | + return nil; | |
| 1110 | +end; | |
| 1111 | + | |
| 1112 | +(* whether operand i is an xmm register or a memory operand *) | |
| 1113 | +proc isXM(i: INT): BOOL; | |
| 1114 | +begin | |
| 1115 | + return (ao[i].kind = oXmm) or (ao[i].kind = oMem); | |
| 1116 | +end; | |
| 1117 | + | |
| 1118 | +(* whether operand i is a general register or a memory operand, for an | |
| 1119 | + xmm register's partner *) | |
| 1120 | +proc isRMem(i: INT): BOOL; | |
| 1121 | +begin | |
| 1122 | + return (ao[i].kind = oReg) or (ao[i].kind = oMem); | |
| 1123 | +end; | |
| 1124 | + | |
| 1125 | +(* b as 1 for true *) | |
| 1126 | +proc bit(f: BOOL): INT; | |
| 1127 | +begin | |
| 1128 | + if f then return 1; end; | |
| 1129 | + return 0; | |
| 1130 | +end; | |
| 1131 | + | |
| 1132 | +(* The instruction w with its operands, in ao and an, apart from the | |
| 1133 | + jumps to labels and db, which assemble does *) | |
| 1134 | +proc instruction(): ERROR; | |
| 1135 | +var k: INT; | |
| 1136 | +begin | |
| 1137 | + if wordIndex(aw, Alus) >= 0 then | |
| 1138 | + k := wordIndex(aw, Alus); | |
| 1139 | + return two(k * 8 + 1, k * 8 + 3, 81H, k); | |
| 1140 | + elsif aw = "mov" then | |
| 1141 | + if (an = 2) and (ao[0].kind = oReg) and (ao[1].kind = oImm) and (asize >= 4) then | |
| 1142 | + if awide then | |
| 1143 | + movRI(ao[0].r, ao[1].v); | |
| 1144 | + else | |
| 1145 | + prefix(0, false, 0, NoIndex, ao[0].r, false); Chunk.put1(0B8H + (ao[0].r & 7)); Chunk.put4(INT(ao[1].v & 0FFFFFFFFH)); | |
| 1146 | + end; | |
| 1147 | + return nil; | |
| 1148 | + end; | |
| 1149 | + return two(89H, 8BH, 0C7H, 0); | |
| 1150 | + elsif aw = "test" then | |
| 1151 | + return two(85H, 85H, 0F7H, 0); | |
| 1152 | + elsif aw = "xchg" then | |
| 1153 | + return two(87H, 87H, -1, 0); | |
| 1154 | + elsif (aw = "cmpxchg") or (aw = "xadd") then | |
| 1155 | + if (an # 2) or (ao[1].kind # oReg) then return bad(); end; | |
| 1156 | + if aw = "xadd" then k := 0C10FH; else k := 0B10FH; end; | |
| 1157 | + rm(apfx, awide, k - (bit(abyte) shl 8), ao[1].r, ao[0], abyte); | |
| 1158 | + elsif aw = "lea" then | |
| 1159 | + if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind # oMem) then return bad(); end; | |
| 1160 | + rm(apfx, awide, 8DH, ao[0].r, ao[1], false); | |
| 1161 | + elsif (aw = "movzx") or (aw = "movsx") or (aw = "movsxd") then | |
| 1162 | + if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) then return bad(); end; | |
| 1163 | + if aw = "movsxd" then | |
| 1164 | + k := 63H; | |
| 1165 | + elsif ao[1].size = 1 then | |
| 1166 | + k := 0B60FH; | |
| 1167 | + elsif ao[1].size = 2 then | |
| 1168 | + k := 0B70FH; | |
| 1169 | + else | |
| 1170 | + return bad(); | |
| 1171 | + end; | |
| 1172 | + if aw = "movsx" then k := k + 0800H; end; | |
| 1173 | + rm(apfx, awide, k, ao[0].r, ao[1], ao[1].size = 1); | |
| 1174 | + elsif ((wordIndex(aw, Unaries) >= 0) and (an = 1)) or (aw = "inc") or (aw = "dec") then | |
| 1175 | + if ao[0].kind = oImm then return bad(); end; | |
| 1176 | + if aw = "inc" then | |
| 1177 | + rm(apfx, awide, 0FFH - bit(abyte), 0, ao[0], abyte); | |
| 1178 | + elsif aw = "dec" then | |
| 1179 | + rm(apfx, awide, 0FFH - bit(abyte), 1, ao[0], abyte); | |
| 1180 | + else | |
| 1181 | + rm(apfx, awide, 0F7H - bit(abyte), wordIndex(aw, Unaries) + 2, ao[0], abyte); | |
| 1182 | + end; | |
| 1183 | + elsif aw = "imul" then | |
| 1184 | + if (an < 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) then return bad(); end; | |
| 1185 | + if an = 2 then | |
| 1186 | + rm(apfx, awide, 0AF0FH, ao[0].r, ao[1], false); | |
| 1187 | + else | |
| 1188 | + rm(apfx, awide, 69H, ao[0].r, ao[1], false); | |
| 1189 | + putImmV(ao[2].v, asize); | |
| 1190 | + end; | |
| 1191 | + elsif wordIndex(aw, Shifts) >= 0 then | |
| 1192 | + k := wordIndex(aw, Shifts); | |
| 1193 | + if k = 6 then k := 4; end; (* sal is shl *) | |
| 1194 | + if (an # 2) or (ao[0].kind = oImm) then return bad(); end; | |
| 1195 | + if ao[1].kind = oImm then | |
| 1196 | + rm(apfx, awide, 0C1H - bit(abyte), k, ao[0], abyte); | |
| 1197 | + Chunk.put1(INT(ao[1].v & 0FFH)); | |
| 1198 | + elsif (ao[1].kind = oReg) and (ao[1].r = RCX) and (ao[1].size = 1) then | |
| 1199 | + rm(apfx, awide, 0D3H - bit(abyte), k, ao[0], abyte); | |
| 1200 | + else | |
| 1201 | + return bad(); | |
| 1202 | + end; | |
| 1203 | + elsif (aw = "push") or (aw = "pop") then | |
| 1204 | + if (an # 1) or (ao[0].kind # oReg) or not awide then return bad(); end; | |
| 1205 | + if aw = "push" then push(ao[0].r); else pop(ao[0].r); end; | |
| 1206 | + elsif (aw = "call") or (aw = "jmp") then | |
| 1207 | + if (an # 1) or (ao[0].kind = oImm) then return bad(); end; | |
| 1208 | + if aw = "call" then rm(0, false, 0FFH, 2, ao[0], false); else rm(0, false, 0FFH, 4, ao[0], false); end; | |
| 1209 | + elsif Str.hasPrefix(aw, "set") and (cond(part(aw, 3, 3)) >= 0) then | |
| 1210 | + if (an # 1) or (ao[0].kind = oImm) or (asize # 1) then return bad(); end; | |
| 1211 | + rm(0, false, 900FH + (cond(part(aw, 3, 3)) shl 8), 0, ao[0], true); | |
| 1212 | + elsif Str.hasPrefix(aw, "cmov") and (cond(part(aw, 4, 3)) >= 0) then | |
| 1213 | + if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) then return bad(); end; | |
| 1214 | + rm(apfx, awide, 400FH + (cond(part(aw, 4, 3)) shl 8), ao[0].r, ao[1], false); | |
| 1215 | + elsif (LEN(aw) > 2) and (wordIndex(aw[0..LEN(aw) - 2], SseOps) >= 0) | |
| 1216 | + and ((aw[LEN(aw) - 2..LEN(aw)] = "sd") or (aw[LEN(aw) - 2..LEN(aw)] = "ss")) then | |
| 1217 | + (* sqrtsd, addss and the like: xmm, xmm or memory *) | |
| 1218 | + if (an # 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; | |
| 1219 | + k := 0FH | (SseCodes[wordIndex(aw[0..LEN(aw) - 2], SseOps)] shl 8); | |
| 1220 | + rm(realPfx(aw[LEN(aw) - 1] = 's'), false, k, ao[0].r, ao[1], false); | |
| 1221 | + elsif wordIndex(aw, PackedOps) >= 0 then | |
| 1222 | + if (an < 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; | |
| 1223 | + k := wordIndex(aw, PackedOps); | |
| 1224 | + rm(66H, false, 0FH | (PackedCodes[k] shl 8), ao[0].r, ao[1], false); | |
| 1225 | + if aw = "pshufd" then | |
| 1226 | + if (an # 3) or (ao[2].kind # oImm) then return bad(); end; | |
| 1227 | + Chunk.put1(INT(ao[2].v & 0FFH)); | |
| 1228 | + elsif an # 2 then | |
| 1229 | + return bad(); | |
| 1230 | + end; | |
| 1231 | + elsif (aw = "movdqu") or (aw = "movdqa") then | |
| 1232 | + (* the load is 6F and the store 7F; movdqu is F3, movdqa 66 *) | |
| 1233 | + if aw = "movdqu" then k := 0F3H; else k := 66H; end; | |
| 1234 | + if (an = 2) and (ao[0].kind = oXmm) and isXM(1) then | |
| 1235 | + rm(k, false, 6F0FH, ao[0].r, ao[1], false); | |
| 1236 | + elsif (an = 2) and (ao[0].kind = oMem) and (ao[1].kind = oXmm) then | |
| 1237 | + rm(k, false, 7F0FH, ao[1].r, ao[0], false); | |
| 1238 | + else | |
| 1239 | + return bad(); | |
| 1240 | + end; | |
| 1241 | + elsif aw = "pmovmskb" then | |
| 1242 | + if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind # oXmm) then return bad(); end; | |
| 1243 | + rm(66H, false, 0D70FH, ao[0].r, ao[1], false); | |
| 1244 | + elsif wordIndex(aw, BitOps) >= 0 then | |
| 1245 | + if (an # 2) or (ao[0].kind # oReg) or (ao[1].kind = oImm) or (asize < 4) then return bad(); end; | |
| 1246 | + k := wordIndex(aw, BitOps); | |
| 1247 | + rm(BitPfx[k], awide, BitCodes[k], ao[0].r, ao[1], false); | |
| 1248 | + elsif (aw = "movsd") or (aw = "movss") then | |
| 1249 | + if aw = "movsd" then k := 0F2H; else k := 0F3H; end; | |
| 1250 | + if (an = 2) and (ao[0].kind = oXmm) and isXM(1) then | |
| 1251 | + rm(k, false, 100FH, ao[0].r, ao[1], false); | |
| 1252 | + elsif (an = 2) and (ao[0].kind = oMem) and (ao[1].kind = oXmm) then | |
| 1253 | + rm(k, false, 110FH, ao[1].r, ao[0], false); | |
| 1254 | + else | |
| 1255 | + return bad(); | |
| 1256 | + end; | |
| 1257 | + elsif (aw = "movq") or (aw = "movd") then | |
| 1258 | + if (an = 2) and (ao[0].kind = oXmm) and isRMem(1) then | |
| 1259 | + rm(66H, aw = "movq", 6E0FH, ao[0].r, ao[1], false); | |
| 1260 | + elsif (an = 2) and (ao[1].kind = oXmm) and isRMem(0) then | |
| 1261 | + rm(66H, aw = "movq", 7E0FH, ao[1].r, ao[0], false); | |
| 1262 | + else | |
| 1263 | + return bad(); | |
| 1264 | + end; | |
| 1265 | + elsif (aw = "ucomisd") or (aw = "ucomiss") then | |
| 1266 | + if (an # 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; | |
| 1267 | + if aw = "ucomisd" then | |
| 1268 | + rm(66H, false, 2E0FH, ao[0].r, ao[1], false); | |
| 1269 | + else | |
| 1270 | + rm(0, false, 2E0FH, ao[0].r, ao[1], false); | |
| 1271 | + end; | |
| 1272 | + elsif (aw = "cvtsi2sd") or (aw = "cvtsi2ss") then | |
| 1273 | + if (an # 2) or (ao[0].kind # oXmm) or not isRMem(1) then return bad(); end; | |
| 1274 | + rm(realPfx(aw = "cvtsi2ss"), ao[1].size # 4, 2A0FH, ao[0].r, ao[1], false); | |
| 1275 | + elsif (aw = "cvttsd2si") or (aw = "cvttss2si") then | |
| 1276 | + if (an # 2) or (ao[0].kind # oReg) or not isXM(1) then return bad(); end; | |
| 1277 | + rm(realPfx(aw = "cvttss2si"), awide, 2C0FH, ao[0].r, ao[1], false); | |
| 1278 | + elsif (aw = "cvtsd2ss") or (aw = "cvtss2sd") then | |
| 1279 | + if (an # 2) or (ao[0].kind # oXmm) or not isXM(1) then return bad(); end; | |
| 1280 | + rm(realPfx(aw = "cvtss2sd"), false, 5A0FH, ao[0].r, ao[1], false); | |
| 1281 | + else | |
| 1282 | + k := 0; | |
| 1283 | + while (k <= 14) and (Plain[k] # aw) do INC(k); end; | |
| 1284 | + if (k > 14) or (an # 0) then | |
| 1285 | + return Scan.markAt(aat, Diag.say("unknown instruction {}", [Fmt.str(aw)])); | |
| 1286 | + end; | |
| 1287 | + opcode(PlainOp[k]); | |
| 1288 | + end; | |
| 1289 | + return nil; | |
| 1290 | +end; | |
| 1291 | + | |
| 1292 | +(* One instruction or label, as assemble reads it *) | |
| 1293 | +proc statement(constExpr: ConstFn): ERROR; | |
| 1294 | +var | |
| 1295 | + i, k, cc, first: INT; wasListing: BOOL; start: Diag.Pos; v: I64; | |
| 1296 | +begin | |
| 1297 | + aat := Scan.tokPos(); | |
| 1298 | + aw := asmWord(); | |
| 1299 | + if LEN(aw) = 0 then return Scan.mark("expected an instruction"); end; | |
| 1300 | + Scan.next; | |
| 1301 | + if Scan.tok = Scan.sColon then | |
| 1302 | + sayAt(Chunk.pc, Diag.say("{}:", [Fmt.str(aw)])); | |
| 1303 | + k := try lab(Scan.intern(ADR(aw[0]), LEN(aw))); | |
| 1304 | + if lplaces[k] >= 0 then | |
| 1305 | + return Scan.markAt(aat, Diag.say("label {} is defined twice", [Fmt.str(aw)])); | |
| 1306 | + end; | |
| 1307 | + lplaces[k] := Chunk.pc; | |
| 1308 | + fixLink(lchains[k]); | |
| 1309 | + Scan.next; | |
| 1310 | + return nil; | |
| 1311 | + end; | |
| 1312 | + first := Chunk.pc; | |
| 1313 | + wasListing := listing; | |
| 1314 | + listing := false; (* the instruction is said as it is written *) | |
| 1315 | + if aw = "lock" then | |
| 1316 | + Chunk.put1(0F0H); | |
| 1317 | + aw := asmWord(); | |
| 1318 | + Scan.next; | |
| 1319 | + end; | |
| 1320 | + cc := -1; | |
| 1321 | + if aw = "db" then | |
| 1322 | + repeat | |
| 1323 | + start := Scan.tokPos(); | |
| 1324 | + v := try constExpr(true); | |
| 1325 | + if (v < -128) or (v > 255) then | |
| 1326 | + return Scan.markAt(Scan.spanFrom(start), Diag.say("expected a byte from 0 to 255, found {}", [Fmt.int(v)])); | |
| 1327 | + end; | |
| 1328 | + Chunk.put1(INT(v & 0FFH)); | |
| 1329 | + if Scan.tok # Scan.sComma then break; end; | |
| 1330 | + Scan.next; | |
| 1331 | + until false; | |
| 1332 | + elsif (aw[0] = 'j') and ((aw = "jmp") or (cond(part(aw, 1, 3)) >= 0)) and (Scan.tok = Scan.sIdent) then | |
| 1333 | + (* a jump to a label *) | |
| 1334 | + k := try lab(Scan.id); | |
| 1335 | + Scan.next; | |
| 1336 | + if aw # "jmp" then cc := cond(part(aw, 1, 3)); end; | |
| 1337 | + if lplaces[k] >= 0 then | |
| 1338 | + if cc < 0 then jmpBack(lplaces[k]); else jccBack(cc, lplaces[k]); end; | |
| 1339 | + elsif cc < 0 then | |
| 1340 | + jmp(lchains[k]); | |
| 1341 | + else | |
| 1342 | + jcc(cc, lchains[k]); | |
| 1343 | + end; | |
| 1344 | + else | |
| 1345 | + an := 0; | |
| 1346 | + if Scan.tok # Scan.sSemicolon then | |
| 1347 | + repeat | |
| 1348 | + if an > 2 then return Scan.mark("too many operands"); end; | |
| 1349 | + try operand(ADR(ao[an]), constExpr); | |
| 1350 | + INC(an); | |
| 1351 | + if Scan.tok # Scan.sComma then break; end; | |
| 1352 | + Scan.next; | |
| 1353 | + until false; | |
| 1354 | + end; | |
| 1355 | + (* the size: a register's, or what the memory operand says *) | |
| 1356 | + asize := 8; | |
| 1357 | + for i := an - 1 to 0 by -1 do | |
| 1358 | + if (ao[i].kind = oReg) or ((ao[i].kind = oMem) and (ao[i].size # 0)) then asize := ao[i].size; end; | |
| 1359 | + end; | |
| 1360 | + if an > 0 then | |
| 1361 | + if (ao[0].kind = oReg) or ((ao[0].kind = oMem) and (ao[0].size # 0)) then asize := ao[0].size; end; | |
| 1362 | + end; | |
| 1363 | + awide := asize = 8; | |
| 1364 | + abyte := asize = 1; | |
| 1365 | + if asize = 2 then apfx := 66H; else apfx := 0; end; | |
| 1366 | + try instruction(); | |
| 1367 | + end; | |
| 1368 | + if Scan.tok # Scan.sSemicolon then return Scan.mark("expected \";\""); end; | |
| 1369 | + listing := wasListing; | |
| 1370 | + sayAt(first, Diag.source(Scan.spanFrom(aat))); | |
| 1371 | + Scan.next; | |
| 1372 | + return nil; | |
| 1373 | +end; | |
| 1374 | + | |
| 1375 | +proc assemble*(constExpr: ConstFn): ERROR; | |
| 1376 | +var k: INT; | |
| 1377 | +begin | |
| 1378 | + numLabels := 0; | |
| 1379 | + while (Scan.tok # Scan.sEnd) and (Scan.tok # Scan.sEof) do | |
| 1380 | + if Scan.tok = Scan.sSemicolon then | |
| 1381 | + Scan.next; | |
| 1382 | + else | |
| 1383 | + try statement(constExpr); | |
| 1384 | + end; | |
| 1385 | + end; | |
| 1386 | + for k := 0 to numLabels - 1 do | |
| 1387 | + if lplaces[k] < 0 then | |
| 1388 | + return Scan.markAt(lusedAt[k], Diag.say("label {} isn't defined", [Fmt.str(Scan.nameOf(lnames[k]))])); | |
| 1389 | + end; | |
| 1390 | + end; | |
| 1391 | + return nil; | |
| 1392 | +end; | |
| 1393 | + | |
| 1394 | +(* Whether s holds a space *) | |
| 1395 | +proc hasSpace(s: []U8): BOOL; | |
| 1396 | +var i: INT; | |
| 1397 | +begin | |
| 1398 | + for i := 0 to LEN(s) - 1 do | |
| 1399 | + if s[i] = ' ' then return true; end; | |
| 1400 | + end; | |
| 1401 | + return false; | |
| 1402 | +end; | |
| 1403 | + | |
| 1404 | +(* n, in decimal, at least w wide, as write(n: w) *) | |
| 1405 | +proc padded(n, w: INT); | |
| 1406 | +var s: []U8; i: INT; | |
| 1407 | +begin | |
| 1408 | + s := Diag.say("{}", [Fmt.int(n)]); | |
| 1409 | + for i := LEN(s) to w - 1 do Out.str(" "); end; | |
| 1410 | + Out.str(s); | |
| 1411 | +end; | |
| 1412 | + | |
| 1413 | +(* The listing, raz -S: each live procedure of the module in the order | |
| 1414 | + it was made, each line of source it comes from, and under it the | |
| 1415 | + instructions made for it, at their offsets in the procedure; a jump | |
| 1416 | + within it says where to by its offset too. *) | |
| 1417 | +proc list*(home: INT); | |
| 1418 | +var order: ^INT; n, i, j, k, c, t, line: INT; text: []U8; | |
| 1419 | +begin | |
| 1420 | + order := ^INT(Scan.grab(Chunk.numChunks * SIZE(INT))); | |
| 1421 | + n := 0; | |
| 1422 | + for c := 0 to Chunk.numChunks - 1 do | |
| 1423 | + if Chunk.chunks[c].live and (Chunk.chunks[c].sec = Chunk.secText) and (Chunk.chunks[c].home = home) then | |
| 1424 | + i := n; | |
| 1425 | + while (i > 0) and (Chunk.chunks[order[i - 1]].start > Chunk.chunks[c].start) do | |
| 1426 | + order[i] := order[i - 1]; | |
| 1427 | + DEC(i); | |
| 1428 | + end; | |
| 1429 | + order[i] := c; | |
| 1430 | + INC(n); | |
| 1431 | + end; | |
| 1432 | + end; | |
| 1433 | + i := 0; j := 0; | |
| 1434 | + for k := 0 to n - 1 do | |
| 1435 | + c := order[k]; | |
| 1436 | + Out.ln; | |
| 1437 | + Out.str(Chunk.chunkName(c)); Out.str(":\n"); | |
| 1438 | + line := -1; | |
| 1439 | + while (i < numSaid) and (said[i].at < Chunk.chunks[c].start) do INC(i); end; | |
| 1440 | + while (i < numSaid) and (said[i].at < Chunk.chunks[c].start + Chunk.chunks[c].size) do | |
| 1441 | + while (j < Chunk.numLines) and (Chunk.lines[j].at <= said[i].at) do | |
| 1442 | + if (Chunk.lines[j].chunk = c) and (Chunk.lines[j].line # line) then | |
| 1443 | + line := Chunk.lines[j].line; | |
| 1444 | + padded(line, 6); | |
| 1445 | + Out.str(" "); | |
| 1446 | + Out.str(Diag.lineText(Scan.nameOf(Chunk.lines[j].path), line)); | |
| 1447 | + Out.ln; | |
| 1448 | + end; | |
| 1449 | + INC(j); | |
| 1450 | + end; | |
| 1451 | + text := said[i].text; | |
| 1452 | + t := said[i].at; | |
| 1453 | + if (text = "jmp") or (text = "call") then | |
| 1454 | + text := Diag.say("{} {}", [Fmt.str(text), Fmt.int(t + 5 + Chunk.get4At(t + 1) - Chunk.chunks[c].start)]); | |
| 1455 | + elsif (text[0] = 'j') and not hasSpace(text) then | |
| 1456 | + text := Diag.say("{} {}", [Fmt.str(text), Fmt.int(t + 6 + Chunk.get4At(t + 2) - Chunk.chunks[c].start)]); | |
| 1457 | + end; | |
| 1458 | + padded(t - Chunk.chunks[c].start, 14); | |
| 1459 | + Out.str(" "); | |
| 1460 | + Out.str(text); | |
| 1461 | + Out.ln; | |
| 1462 | + INC(i); | |
| 1463 | + end; | |
| 1464 | + end; | |
| 1465 | +end; | |
| 1466 | + | |
| 1467 | +begin | |
| 1468 | + listing := false; | |
| 1469 | + numSaid := 0; | |
| 1470 | + saidCap := 0; | |
| 1471 | + said := nil; | |
| 1472 | +end X64. |
| @@ -54,3 +54,40 @@ | ||
| 54 | 54 | cmp $b/sym.seed.err $b/sym.self.err |
| 55 | 55 | done |
| 56 | 56 | echo "chunk and sym ok" |
| 57 | + | |
| 58 | +# Link and Elf: a program of chunks made by a script, linked and written | |
| 59 | +# as a static, a dynamic and a stripped executable, which must be the | |
| 60 | +# same file from both sides | |
| 61 | +fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fusrc -o$b/linkdump.seed self/test/LinkDump.pas >/dev/null | |
| 62 | +./raz -Werror -I self -o $b/linkdump.self self/test/LinkDump.raz | |
| 63 | +for m in 0 1 2; do | |
| 64 | + $b/linkdump.seed $m $b/link.seed | |
| 65 | + $b/linkdump.self $m $b/link.self | |
| 66 | + cmp $b/link.seed $b/link.self | |
| 67 | +done | |
| 68 | +echo "link and elf ok" | |
| 69 | + | |
| 70 | +# X64: every encoder, then the assembler on a long run of instructions | |
| 71 | +# of every kind and a file for each error it makes; the bytes and the | |
| 72 | +# listing, or the message and status, must be the same | |
| 73 | +fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fusrc -o$b/x64dump.seed self/test/X64Dump.pas >/dev/null | |
| 74 | +./raz -Werror -I self -o $b/x64dump.self self/test/X64Dump.raz | |
| 75 | +$b/x64dump.seed >$b/x64.seed | |
| 76 | +$b/x64dump.self >$b/x64.self | |
| 77 | +cmp $b/x64.seed $b/x64.self | |
| 78 | +fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fusrc -o$b/asmdump.seed self/test/AsmDump.pas >/dev/null | |
| 79 | +./raz -Werror -I self -o $b/asmdump.self self/test/AsmDump.raz | |
| 80 | +mkdir -p $b/asm | |
| 81 | +python3 self/test/genasm.py $b/asm | |
| 82 | +n=0; bad=0 | |
| 83 | +for f in $b/asm/*.raz; do | |
| 84 | + s1=0; s2=0 | |
| 85 | + $b/asmdump.seed $f >$b/seed.out 2>$b/seed.err || s1=$? | |
| 86 | + $b/asmdump.self $f >$b/self.out 2>$b/self.err || s2=$? | |
| 87 | + n=$((n + 1)) | |
| 88 | + if [ $s1 != $s2 ] || ! cmp -s $b/seed.out $b/self.out || ! cmp -s $b/seed.err $b/self.err; then | |
| 89 | + echo "differs: $f (status $s1 and $s2)"; bad=$((bad + 1)) | |
| 90 | + fi | |
| 91 | +done | |
| 92 | +echo "x64: $n assembler files, $bad differ" | |
| 93 | +test $bad = 0 |
| @@ -0,0 +1,46 @@ | ||
| 1 | +{ Assembles the instructions in the file of the argument, as the body of | |
| 2 | + a code procedure that ends in end, and prints the bytes and the | |
| 3 | + listing: the seed's side of a test of the assembler in self/X64.raz, | |
| 4 | + whose AsmDump.raz does the same and prints the same. Constants are | |
| 5 | + numbers, with a minus sign, and sums of them. } | |
| 6 | +program asmdump; | |
| 7 | +{$mode objfpc}{$H-} | |
| 8 | +uses diag, scan, chunk, x64; | |
| 9 | +var i: longint; path: string; | |
| 10 | + | |
| 11 | +function ConstVal(whole: boolean): int64; | |
| 12 | +var neg: boolean; v: int64; | |
| 13 | +begin | |
| 14 | + neg := tok = sMinus; | |
| 15 | + if neg then Next; | |
| 16 | + if tok <> sInt then Mark('expected a number'); | |
| 17 | + v := ival; | |
| 18 | + Next; | |
| 19 | + if neg then v := -v; | |
| 20 | + while whole and (tok in [sPlus, sMinus]) do begin | |
| 21 | + neg := tok = sMinus; | |
| 22 | + Next; | |
| 23 | + if tok <> sInt then Mark('expected a number'); | |
| 24 | + if neg then v := v - ival else v := v + ival; | |
| 25 | + Next; | |
| 26 | + end; | |
| 27 | + ConstVal := v; | |
| 28 | +end; | |
| 29 | + | |
| 30 | +begin | |
| 31 | + if Open(ParamStr(1)) <> '' then Fatal('no file'); | |
| 32 | + path := 'lib/Rt.raz'; | |
| 33 | + curFile := Intern(@path[1], length(path)); | |
| 34 | + listing := true; | |
| 35 | + NewChunk(secText, 0, 0); | |
| 36 | + BeginChunk(0); | |
| 37 | + NoteLine(1, 1); | |
| 38 | + Next; | |
| 39 | + Assemble(@ConstVal); | |
| 40 | + EndChunk(0); | |
| 41 | + chunks^[0].live := true; | |
| 42 | + for i := 0 to pc - 1 do write(HexStr(code^[i], 2)); | |
| 43 | + writeln; | |
| 44 | + List(0); | |
| 45 | + Finish; | |
| 46 | +end. |
| @@ -0,0 +1,55 @@ | ||
| 1 | +(* Assembles the instructions in the file of the argument, as the body of | |
| 2 | + a code procedure that ends in end, and prints the bytes and the | |
| 3 | + listing: self/X64.raz's assembler's side of a test against the seed's, | |
| 4 | + AsmDump.pas, which prints the same. Constants are numbers, with a | |
| 5 | + minus sign, and sums of them. *) | |
| 6 | +module AsmDump; | |
| 7 | +import Out, Files, Diag, Scan, Chunk, X64; | |
| 8 | + | |
| 9 | +proc number(neg: BOOL): (I64, ERROR); | |
| 10 | +var v: I64; | |
| 11 | +begin | |
| 12 | + if Scan.tok # Scan.sInt then return 0, Scan.mark("expected a number"); end; | |
| 13 | + v := Scan.ival; | |
| 14 | + Scan.next; | |
| 15 | + if neg then v := -v; end; | |
| 16 | + return v, nil; | |
| 17 | +end; | |
| 18 | + | |
| 19 | +proc constant(whole: BOOL): (I64, ERROR); | |
| 20 | +var neg: BOOL; v, w: I64; | |
| 21 | +begin | |
| 22 | + neg := Scan.tok = Scan.sMinus; | |
| 23 | + if neg then Scan.next; end; | |
| 24 | + v := try number(neg); | |
| 25 | + while whole and ((Scan.tok = Scan.sPlus) or (Scan.tok = Scan.sMinus)) do | |
| 26 | + neg := Scan.tok = Scan.sMinus; | |
| 27 | + Scan.next; | |
| 28 | + w := try number(neg); | |
| 29 | + v := v + w; | |
| 30 | + end; | |
| 31 | + return v, nil; | |
| 32 | +end; | |
| 33 | + | |
| 34 | +proc main*; | |
| 35 | +var i: INT; path: []U8; err: ERROR; | |
| 36 | +begin | |
| 37 | + if LEN(Scan.open(Files.arg(1))) # 0 then Diag.fatal("no file"); end; | |
| 38 | + path := "lib/Rt.raz"; | |
| 39 | + Chunk.setCurFile(Scan.intern(ADR(path[0]), LEN(path))); | |
| 40 | + X64.setListing(true); | |
| 41 | + _ := Chunk.newChunk(Chunk.secText, 0, 0); | |
| 42 | + Chunk.beginChunk(0); | |
| 43 | + Chunk.noteLine(1, 1); | |
| 44 | + Scan.next; | |
| 45 | + err := X64.assemble(constant); | |
| 46 | + if err # nil then Diag.finish; end; | |
| 47 | + Chunk.endChunk(0); | |
| 48 | + Chunk.setLive(0); | |
| 49 | + for i := 0 to Chunk.pc - 1 do Out.hex(U64(Chunk.text[i]), 2); end; | |
| 50 | + Out.ln; | |
| 51 | + X64.list(0); | |
| 52 | + Diag.finish; | |
| 53 | +end; | |
| 54 | + | |
| 55 | +end AsmDump. |
| @@ -0,0 +1,91 @@ | ||
| 1 | +{ Makes a program of chunks by a fixed pseudo-random script, links it and | |
| 2 | + writes it as an executable to the file of the second argument, in the | |
| 3 | + way the first says (0 static, 1 dynamic, 2 stripped): the seed's side | |
| 4 | + of a test of self/Link.raz and self/Elf.raz, whose LinkDump.raz does | |
| 5 | + the same and writes the same file. } | |
| 6 | +program linkdump; | |
| 7 | +{$mode objfpc}{$H-} | |
| 8 | +uses diag, scan, chunk, link, elf; | |
| 9 | +const NumC = 200; | |
| 10 | +var | |
| 11 | + seed: int64; | |
| 12 | + names: array[0..7] of longint; | |
| 13 | + i, j, k, kind, n, x, y, z, mode: longint; | |
| 14 | + b: array[0..63] of byte; | |
| 15 | + | |
| 16 | +function Rnd(n: longint): longint; | |
| 17 | +begin | |
| 18 | + seed := (seed * 1103515245 + 12345) and $7FFFFFFF; | |
| 19 | + Rnd := (seed shr 8) mod n; | |
| 20 | +end; | |
| 21 | + | |
| 22 | +var c, e, line, col: longint; | |
| 23 | +begin | |
| 24 | + seed := 5; | |
| 25 | + mode := ord(ParamStr(1)[1]) - 48; | |
| 26 | + for i := 0 to 7 do begin b[0] := 97 + i; names[i] := Intern(@b[0], 1) end; | |
| 27 | + placesChunk := NewChunk(secRodata, -1, -1); | |
| 28 | + SetData(placesChunk, @b[0], 0, 8); | |
| 29 | + for i := 0 to NumC - 1 do begin | |
| 30 | + if i = 0 then kind := 0 else kind := Rnd(10); | |
| 31 | + x := Rnd(4); y := Rnd(8); | |
| 32 | + case kind of | |
| 33 | + 0..5: begin | |
| 34 | + c := NewChunk(secText, x - 1, y); | |
| 35 | + BeginChunk(c); | |
| 36 | + z := Rnd(3); curFile := names[z]; | |
| 37 | + line := Rnd(300); col := Rnd(60); | |
| 38 | + NoteLine(line, col); | |
| 39 | + if i = 0 then begin AddFixup(pc, placesChunk, 0); Put4(0) end; | |
| 40 | + n := 2 + Rnd(10); | |
| 41 | + for j := 1 to n do begin | |
| 42 | + z := Rnd(5); | |
| 43 | + if z <= 1 then begin | |
| 44 | + e := Rnd(NumC); y := Rnd(8); | |
| 45 | + AddFixup(pc, e, y); | |
| 46 | + Put4(0); | |
| 47 | + end else if z = 2 then begin | |
| 48 | + z := Rnd(3); curFile := names[z]; | |
| 49 | + line := Rnd(300); col := Rnd(60); | |
| 50 | + NoteLine(line, col); | |
| 51 | + end else begin x := Rnd(1 shl 30); Put4(x); x := Rnd(256); Put1(x) end; | |
| 52 | + end; | |
| 53 | + EndChunk(c); | |
| 54 | + n := Rnd(3); | |
| 55 | + for j := 1 to n do begin | |
| 56 | + x := Rnd(8); y := Rnd(200); z := Rnd(6); | |
| 57 | + NoteVar(c, names[x], y - 100, z); | |
| 58 | + end; | |
| 59 | + end; | |
| 60 | + 6, 7: begin | |
| 61 | + c := NewChunk(secRodata, x - 1, y); | |
| 62 | + n := 8 * (1 + Rnd(4)); | |
| 63 | + for j := 0 to n - 1 do b[j] := Rnd(256); | |
| 64 | + SetData(c, @b[0], n, 8); | |
| 65 | + for j := 0 to n div 8 - 1 do begin | |
| 66 | + z := Rnd(2); | |
| 67 | + if z = 0 then begin e := Rnd(NumC); y := Rnd(8); AddDataFixup(c, j * 8, e, y) end; | |
| 68 | + end; | |
| 69 | + end; | |
| 70 | + 8: begin | |
| 71 | + c := NewChunk(secBss, x - 1, y); | |
| 72 | + z := Rnd(200); e := Rnd(4); | |
| 73 | + SetBss(c, 1 + z, 1 shl e); | |
| 74 | + end; | |
| 75 | + 9: begin z := Rnd(8); c := ExternChunk(names[z]) end; | |
| 76 | + end; | |
| 77 | + end; | |
| 78 | + { every chunk a fixup names must exist, and be defined if it is reached } | |
| 79 | + while numChunks < NumC do begin c := NewChunk(secBss, -1, -1); SetBss(c, 8, 8) end; | |
| 80 | + for i := 0 to numChunks - 1 do | |
| 81 | + if chunks^[i].size < 0 then SetBss(i, 8, 8); | |
| 82 | + mainPath := 'some/main.raz'; | |
| 83 | + if mode = 1 then begin | |
| 84 | + numNeeded := 2; | |
| 85 | + needed[0] := names[0]; needed[1] := names[1]; | |
| 86 | + end; | |
| 87 | + stripped := mode = 2; | |
| 88 | + if mode = 2 then placesChunk := -1; | |
| 89 | + MarkLive(1); | |
| 90 | + WriteElf(ParamStr(2), 1); | |
| 91 | +end. |
| @@ -0,0 +1,103 @@ | ||
| 1 | +(* Makes a program of chunks by a fixed pseudo-random script, links it | |
| 2 | + and writes it as an executable to the file of the second argument, | |
| 3 | + in the way the first says (0 static, 1 dynamic, 2 stripped): | |
| 4 | + self/Link.raz's and self/Elf.raz's side of a test against the seed's, | |
| 5 | + LinkDump.pas, which writes the same file. *) | |
| 6 | +module LinkDump; | |
| 7 | +import Files, Diag, Scan, Chunk, Link, Elf; | |
| 8 | + | |
| 9 | +const NumC = 200; | |
| 10 | + | |
| 11 | +var seed: I64; | |
| 12 | + | |
| 13 | +proc rnd(n: INT): INT; | |
| 14 | +begin | |
| 15 | + seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; | |
| 16 | + return INT((seed shr 8) mod I64(n)); | |
| 17 | +end; | |
| 18 | + | |
| 19 | +(* a library that must be taken *) | |
| 20 | +proc ok(taken: BOOL); | |
| 21 | +begin | |
| 22 | + if not taken then Diag.fatal("too many libraries"); end; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +proc main*; | |
| 26 | +var | |
| 27 | + names: [8]INT; b: [64]U8; arg: []U8; | |
| 28 | + i, j, kind, n, x, y, z, c, e, line, col, mode: INT; | |
| 29 | +begin | |
| 30 | + seed := 5; | |
| 31 | + arg := Files.arg(1); | |
| 32 | + mode := INT(arg[0]) - 48; | |
| 33 | + for i := 0 to 7 do b[0] := U8(97 + i); names[i] := Scan.intern(ADR(b[0]), 1); end; | |
| 34 | + Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); | |
| 35 | + Chunk.setData(Link.placesChunk, ADR(b[0]), 0, 8); | |
| 36 | + for i := 0 to NumC - 1 do | |
| 37 | + if i = 0 then kind := 0; else kind := rnd(10); end; | |
| 38 | + x := rnd(4); y := rnd(8); | |
| 39 | + match kind of | |
| 40 | + | 0..5: | |
| 41 | + c := Chunk.newChunk(Chunk.secText, x - 1, y); | |
| 42 | + Chunk.beginChunk(c); | |
| 43 | + z := rnd(3); Chunk.setCurFile(names[z]); | |
| 44 | + line := rnd(300); col := rnd(60); | |
| 45 | + Chunk.noteLine(line, col); | |
| 46 | + if i = 0 then Chunk.addFixup(Chunk.pc, Link.placesChunk, 0); Chunk.put4(0); end; | |
| 47 | + n := 2 + rnd(10); | |
| 48 | + for j := 1 to n do | |
| 49 | + z := rnd(5); | |
| 50 | + if z <= 1 then | |
| 51 | + e := rnd(NumC); y := rnd(8); | |
| 52 | + Chunk.addFixup(Chunk.pc, e, y); | |
| 53 | + Chunk.put4(0); | |
| 54 | + elsif z = 2 then | |
| 55 | + z := rnd(3); Chunk.setCurFile(names[z]); | |
| 56 | + line := rnd(300); col := rnd(60); | |
| 57 | + Chunk.noteLine(line, col); | |
| 58 | + else | |
| 59 | + x := rnd(1 shl 30); Chunk.put4(x); x := rnd(256); Chunk.put1(x); | |
| 60 | + end; | |
| 61 | + end; | |
| 62 | + Chunk.endChunk(c); | |
| 63 | + n := rnd(3); | |
| 64 | + for j := 1 to n do | |
| 65 | + x := rnd(8); y := rnd(200); z := rnd(6); | |
| 66 | + Chunk.noteVar(c, names[x], y - 100, z); | |
| 67 | + end; | |
| 68 | + | 6, 7: | |
| 69 | + c := Chunk.newChunk(Chunk.secRodata, x - 1, y); | |
| 70 | + n := 8 * (1 + rnd(4)); | |
| 71 | + for j := 0 to n - 1 do b[j] := U8(rnd(256)); end; | |
| 72 | + Chunk.setData(c, ADR(b[0]), n, 8); | |
| 73 | + for j := 0 to n div 8 - 1 do | |
| 74 | + z := rnd(2); | |
| 75 | + if z = 0 then e := rnd(NumC); y := rnd(8); Chunk.addDataFixup(c, j * 8, e, y); end; | |
| 76 | + end; | |
| 77 | + | 8: | |
| 78 | + c := Chunk.newChunk(Chunk.secBss, x - 1, y); | |
| 79 | + z := rnd(200); e := rnd(4); | |
| 80 | + Chunk.setBss(c, 1 + z, 1 shl e); | |
| 81 | + | else: z := rnd(8); c := Chunk.externChunk(names[z]); | |
| 82 | + end; | |
| 83 | + end; | |
| 84 | + (* every chunk a fixup names must exist, and be defined if it is reached *) | |
| 85 | + while Chunk.numChunks < NumC do | |
| 86 | + c := Chunk.newChunk(Chunk.secBss, -1, -1); | |
| 87 | + Chunk.setBss(c, 8, 8); | |
| 88 | + end; | |
| 89 | + for i := 0 to Chunk.numChunks - 1 do | |
| 90 | + if Chunk.chunks[i].size < 0 then Chunk.setBss(i, 8, 8); end; | |
| 91 | + end; | |
| 92 | + Elf.setMainPath("some/main.raz"); | |
| 93 | + if mode = 1 then | |
| 94 | + ok(Elf.needLib(names[0])); | |
| 95 | + ok(Elf.needLib(names[1])); | |
| 96 | + end; | |
| 97 | + Elf.setStripped(mode = 2); | |
| 98 | + if mode = 2 then Link.setPlaces(-1); end; | |
| 99 | + Link.markLive(1); | |
| 100 | + Elf.writeElf(Files.arg(2), 1); | |
| 101 | +end; | |
| 102 | + | |
| 103 | +end LinkDump. |
| @@ -0,0 +1,160 @@ | ||
| 1 | +{ Writes every instruction the encoder makes, with operands drawn by a | |
| 2 | + fixed pseudo-random script, then prints the bytes, the fixups and the | |
| 3 | + listing (raz -S) of what it made: the seed's side of a test of | |
| 4 | + self/X64.raz, whose X64Dump.raz does the same and prints the same. } | |
| 5 | +program x64dump; | |
| 6 | +{$mode objfpc}{$H-} | |
| 7 | +uses diag, scan, chunk, x64; | |
| 8 | +var | |
| 9 | + seed: int64; | |
| 10 | + i, k, op, a, b, c, d, lab1, lab2: longint; | |
| 11 | + m: TMem; | |
| 12 | + path: string; | |
| 13 | + | |
| 14 | +function Rnd(n: longint): longint; | |
| 15 | +begin | |
| 16 | + seed := (seed * 1103515245 + 12345) and $7FFFFFFF; | |
| 17 | + Rnd := (seed shr 8) mod n; | |
| 18 | +end; | |
| 19 | + | |
| 20 | +{ a register, with the stack and base pointers and the high ones often enough } | |
| 21 | +function Reg: longint; | |
| 22 | +begin | |
| 23 | + Reg := Rnd(16); | |
| 24 | +end; | |
| 25 | + | |
| 26 | +{ a size of 1, 2, 4 or 8 bytes } | |
| 27 | +function Size: longint; | |
| 28 | +begin | |
| 29 | + Size := 1 shl Rnd(4); | |
| 30 | +end; | |
| 31 | + | |
| 32 | +{ a value that fits size bytes, of 8 as 32 bits } | |
| 33 | +function Imm(sz: longint): longint; | |
| 34 | +var x: longint; | |
| 35 | +begin | |
| 36 | + x := Rnd(1 shl 30); | |
| 37 | + case Rnd(3) of | |
| 38 | + 0: Imm := x mod 100 - 50; | |
| 39 | + 1: Imm := x - (1 shl 29); | |
| 40 | + else Imm := x mod 20000 - 10000; | |
| 41 | + end; | |
| 42 | + if sz = 1 then Imm := Imm mod 128 | |
| 43 | + else if sz = 2 then Imm := Imm mod 32768; | |
| 44 | +end; | |
| 45 | + | |
| 46 | +function RandMem: TMem; | |
| 47 | +var kind, base, idx, sc, disp, x: longint; | |
| 48 | +begin | |
| 49 | + kind := Rnd(6); | |
| 50 | + base := Reg; idx := Reg; sc := 1 shl Rnd(4); x := Rnd(5); | |
| 51 | + case x of | |
| 52 | + 0: disp := 0; | |
| 53 | + 1: disp := Rnd(250) - 125; | |
| 54 | + 2: disp := Rnd(1 shl 20) - (1 shl 19); | |
| 55 | + 3: disp := 127 + Rnd(3) - 1; | |
| 56 | + else disp := -128 + Rnd(3) - 1; | |
| 57 | + end; | |
| 58 | + case kind of | |
| 59 | + 0, 1: RandMem := Mem(base, disp); | |
| 60 | + 2: RandMem := MemRip(Rnd(5), disp); | |
| 61 | + 3, 4: RandMem := MemIdx(base, idx, sc, disp); | |
| 62 | + else RandMem := Mem(base, 0); | |
| 63 | + end; | |
| 64 | +end; | |
| 65 | + | |
| 66 | +begin | |
| 67 | + seed := 3; | |
| 68 | + path := 'lib/Rt.raz'; | |
| 69 | + if Open(path) <> '' then Fatal('no file'); | |
| 70 | + listing := true; | |
| 71 | + for i := 0 to 4 do NewChunk(secText, i - 1, i); | |
| 72 | + curFile := Intern(@path[1], length(path)); | |
| 73 | + BeginChunk(0); | |
| 74 | + lab1 := NoLabel; lab2 := NoLabel; | |
| 75 | + for k := 1 to 3000 do begin | |
| 76 | + if k mod 7 = 0 then begin a := Rnd(300); b := Rnd(60); NoteLine(1 + a, b) end; | |
| 77 | + op := Rnd(50); | |
| 78 | + case op of | |
| 79 | + 0: begin a := Reg; b := Reg; MovRR(a, b) end; | |
| 80 | + 1: begin | |
| 81 | + a := Reg; b := Rnd(6); c := Rnd(1 shl 30); | |
| 82 | + case b of | |
| 83 | + 0: MovRI(a, 0); | |
| 84 | + 1: MovRI(a, c); | |
| 85 | + 2: MovRI(a, -c); | |
| 86 | + 3: MovRI(a, int64(c) * 8 + 5); | |
| 87 | + 4: MovRI(a, int64(c) * 4096 * 4096 * 9 - 7); | |
| 88 | + else MovRI(a, 0); | |
| 89 | + end; | |
| 90 | + end; | |
| 91 | + 2: begin a := Rnd(8); m := RandMem; b := Size; c := Imm(b); AluMI(a, m, c, b) end; | |
| 92 | + 3: begin m := RandMem; b := Size; c := Imm(b); StoreI(m, c, b) end; | |
| 93 | + 4: begin a := Reg; m := RandMem; b := Size; c := Rnd(2); Load(a, m, b, c = 0) end; | |
| 94 | + 5: begin m := RandMem; a := Reg; b := Size; Store(m, a, b) end; | |
| 95 | + 6: begin a := Reg; m := RandMem; Lea(a, m) end; | |
| 96 | + 7: begin a := Rnd(8); b := Reg; c := Reg; Alu(a, b, c) end; | |
| 97 | + 8: begin a := Rnd(8); b := Reg; c := Imm(8); AluI(a, b, c) end; | |
| 98 | + 9: begin a := Rnd(8); b := Reg; m := RandMem; AluM(a, b, m) end; | |
| 99 | + 10: begin a := Reg; b := Reg; Imul(a, b) end; | |
| 100 | + 11: begin a := Reg; c := Imm(8); ImulI(a, c) end; | |
| 101 | + 12: begin a := Rnd(3); b := Reg; if a = 0 then a := shShl else if a = 1 then a := shShr else a := shSar; Shift(a, b) end; | |
| 102 | + 13: begin a := Rnd(3); b := Reg; c := Rnd(64); if a = 0 then a := shShl else if a = 1 then a := shShr else a := shSar; ShiftI(a, b, c) end; | |
| 103 | + 14: begin a := Rnd(4); b := Reg; if a < 2 then a := a + 2 else a := a + 4; Unary(a, b) end; | |
| 104 | + 15: Cqo; | |
| 105 | + 16: begin a := Reg; b := 1 shl Rnd(3); c := Rnd(2); Extend(a, b, c = 0) end; | |
| 106 | + 17: begin a := Rnd(16); b := Reg; SetCC(a, b) end; | |
| 107 | + 18: begin a := Reg; b := Reg; TestRR(a, b) end; | |
| 108 | + 19: begin a := Reg; Push(a) end; | |
| 109 | + 20: begin a := Reg; Pop(a) end; | |
| 110 | + 21: begin a := Rnd(5); CallChunk(a) end; | |
| 111 | + 22: begin a := Rnd(1000); CallBack(a) end; | |
| 112 | + 23: begin a := Reg; CallR(a) end; | |
| 113 | + 24: begin a := Reg; JmpR(a) end; | |
| 114 | + 25: begin m := RandMem; CallM(m) end; | |
| 115 | + 26: begin a := Rnd(5); case a of 0: Ret; 1: Leave; 2: Syscall; 3: Mfence; else Ud2 end end; | |
| 116 | + 27: begin a := 8 shl Rnd(3); Align(a) end; | |
| 117 | + 28: begin a := Rnd(5); case a of 0: RepMovsb; 1: Std; 2: Cld; 3: RepStosb; else RepeCmpsb end end; | |
| 118 | + 29: begin m := RandMem; a := Reg; LockCmpxchg(m, a) end; | |
| 119 | + 30: begin m := RandMem; a := Reg; Xchg(m, a) end; | |
| 120 | + 31: begin m := RandMem; a := Reg; LockXadd(m, a) end; | |
| 121 | + 32: begin a := Reg; b := Reg; MovqXR(a, b) end; | |
| 122 | + 33: begin a := Reg; b := Reg; MovqRX(a, b) end; | |
| 123 | + 34: begin a := Reg; b := Reg; MovX(a, b) end; | |
| 124 | + 35: begin a := Reg; m := RandMem; b := Rnd(2); MovsXM(a, m, b = 0) end; | |
| 125 | + 36: begin m := RandMem; a := Reg; b := Rnd(2); MovsMX(m, a, b = 0) end; | |
| 126 | + 37: begin | |
| 127 | + d := Rnd(5); a := Reg; b := Reg; c := Rnd(2); | |
| 128 | + case d of 0: d := $58; 1: d := $59; 2: d := $5C; 3: d := $5E; else d := $5A end; | |
| 129 | + Sse(d, a, b, c = 0); | |
| 130 | + end; | |
| 131 | + 38: begin | |
| 132 | + d := Rnd(5); a := Reg; m := RandMem; c := Rnd(2); | |
| 133 | + case d of 0: d := $58; 1: d := $59; 2: d := $5C; 3: d := $5E; else d := $5A end; | |
| 134 | + SseM(d, a, m, c = 0); | |
| 135 | + end; | |
| 136 | + 39: begin a := Reg; b := Reg; c := Rnd(2); Ucomis(a, b, c = 0) end; | |
| 137 | + 40: begin a := Reg; b := Reg; c := Rnd(2); Cvtsi2s(a, b, c = 0) end; | |
| 138 | + 41: begin a := Reg; b := Reg; c := Rnd(2); Cvtts2si(a, b, c = 0) end; | |
| 139 | + 42: begin a := Reg; b := Rnd(64); Btc(a, b) end; | |
| 140 | + 43: begin | |
| 141 | + a := Rnd(16); b := Rnd(2); | |
| 142 | + if b = 0 then Jmp(lab1) else Jcc(a, lab2); | |
| 143 | + end; | |
| 144 | + 44: begin a := Rnd(16); b := Rnd(2); c := pc - Rnd(200); if b = 0 then JmpBack(c) else JccBack(a, c) end; | |
| 145 | + 45: if Rnd(3) = 0 then begin FixLink(lab1); lab1 := NoLabel end | |
| 146 | + else begin FixLinkTo(lab2, pc - 20); lab2 := NoLabel end; | |
| 147 | + 46: begin Jmp(lab1); DropJumpTo(lab1) end; | |
| 148 | + 47: begin a := JoinLinks(lab1, lab2); lab1 := a; lab2 := NoLabel end; | |
| 149 | + else begin a := Rnd(256); Put1(a) end; | |
| 150 | + end; | |
| 151 | + end; | |
| 152 | + FixLink(lab1); FixLink(lab2); | |
| 153 | + EndChunk(0); | |
| 154 | + chunks^[0].live := true; | |
| 155 | + writeln('pc ', pc, ' fixups ', numFixups, ' lines ', numLines, ' said ', SaidCount); | |
| 156 | + for i := 0 to pc - 1 do write(HexStr(code^[i], 2)); | |
| 157 | + writeln; | |
| 158 | + for i := 0 to numFixups - 1 do writeln('F ', fixups^[i].at, ' ', fixups^[i].chunk, ' ', fixups^[i].addend); | |
| 159 | + List(-1); | |
| 160 | +end. |
| @@ -0,0 +1,208 @@ | ||
| 1 | +(* Writes every instruction the encoder makes, with operands drawn by a | |
| 2 | + fixed pseudo-random script, then prints the bytes, the fixups and the | |
| 3 | + listing (raz -S) of what it made: self/X64.raz's side of a test | |
| 4 | + against the seed's, X64Dump.pas, which prints the same. *) | |
| 5 | +module X64Dump; | |
| 6 | +import Out, Fmt, Diag, Scan, Chunk, X64; | |
| 7 | + | |
| 8 | +var seed: I64; | |
| 9 | + | |
| 10 | +proc rnd(n: INT): INT; | |
| 11 | +begin | |
| 12 | + seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; | |
| 13 | + return INT((seed shr 8) mod I64(n)); | |
| 14 | +end; | |
| 15 | + | |
| 16 | +(* a register *) | |
| 17 | +proc reg: INT; | |
| 18 | +begin | |
| 19 | + return rnd(16); | |
| 20 | +end; | |
| 21 | + | |
| 22 | +(* a size of 1, 2, 4 or 8 bytes *) | |
| 23 | +proc size: INT; | |
| 24 | +begin | |
| 25 | + return 1 shl rnd(4); | |
| 26 | +end; | |
| 27 | + | |
| 28 | +(* a value that fits sz bytes, of 8 as 32 bits *) | |
| 29 | +proc imm(sz: INT): INT; | |
| 30 | +var x, r: INT; | |
| 31 | +begin | |
| 32 | + x := rnd(1 shl 30); | |
| 33 | + match rnd(3) of | |
| 34 | + | 0: r := x mod 100 - 50; | |
| 35 | + | 1: r := x - (1 shl 29); | |
| 36 | + | else: r := x mod 20000 - 10000; | |
| 37 | + end; | |
| 38 | + if sz = 1 then | |
| 39 | + r := r mod 128; | |
| 40 | + elsif sz = 2 then | |
| 41 | + r := r mod 32768; | |
| 42 | + end; | |
| 43 | + return r; | |
| 44 | +end; | |
| 45 | + | |
| 46 | +proc randMem: X64.Mem; | |
| 47 | +var kind, base, idx, sc, disp, x: INT; | |
| 48 | +begin | |
| 49 | + kind := rnd(6); | |
| 50 | + base := reg(); idx := reg(); sc := 1 shl rnd(4); x := rnd(5); | |
| 51 | + match x of | |
| 52 | + | 0: disp := 0; | |
| 53 | + | 1: disp := rnd(250) - 125; | |
| 54 | + | 2: disp := rnd(1 shl 20) - (1 shl 19); | |
| 55 | + | 3: disp := 127 + rnd(3) - 1; | |
| 56 | + | else: disp := -128 + rnd(3) - 1; | |
| 57 | + end; | |
| 58 | + match kind of | |
| 59 | + | 0, 1: return X64.mem(base, disp); | |
| 60 | + | 2: return X64.memRip(rnd(5), disp); | |
| 61 | + | 3, 4: return X64.memIdx(base, idx, sc, disp); | |
| 62 | + | else: return X64.mem(base, 0); | |
| 63 | + end; | |
| 64 | +end; | |
| 65 | + | |
| 66 | +proc main*; | |
| 67 | +var | |
| 68 | + i, k, op, a, b, c, d, lab1, lab2: INT; m: X64.Mem; path: []U8; | |
| 69 | +begin | |
| 70 | + seed := 3; | |
| 71 | + path := "lib/Rt.raz"; | |
| 72 | + if LEN(Scan.open(path)) # 0 then Diag.fatal("no file"); end; | |
| 73 | + X64.setListing(true); | |
| 74 | + for i := 0 to 4 do c := Chunk.newChunk(Chunk.secText, i - 1, i); end; | |
| 75 | + Chunk.setCurFile(Scan.intern(ADR(path[0]), LEN(path))); | |
| 76 | + Chunk.beginChunk(0); | |
| 77 | + lab1 := X64.NoLabel; lab2 := X64.NoLabel; | |
| 78 | + for k := 1 to 3000 do | |
| 79 | + if k mod 7 = 0 then a := rnd(300); b := rnd(60); Chunk.noteLine(1 + a, b); end; | |
| 80 | + op := rnd(50); | |
| 81 | + match op of | |
| 82 | + | 0: a := reg(); b := reg(); X64.movRR(a, b); | |
| 83 | + | 1: | |
| 84 | + a := reg(); b := rnd(6); c := rnd(1 shl 30); | |
| 85 | + match b of | |
| 86 | + | 0: X64.movRI(a, 0); | |
| 87 | + | 1: X64.movRI(a, c); | |
| 88 | + | 2: X64.movRI(a, -c); | |
| 89 | + | 3: X64.movRI(a, I64(c) * 8 + 5); | |
| 90 | + | 4: X64.movRI(a, I64(c) * 4096 * 4096 * 9 - 7); | |
| 91 | + | else: X64.movRI(a, 0); | |
| 92 | + end; | |
| 93 | + | 2: a := rnd(8); m := randMem(); b := size(); c := imm(b); X64.aluMI(a, m, c, b); | |
| 94 | + | 3: m := randMem(); b := size(); c := imm(b); X64.storeI(m, c, b); | |
| 95 | + | 4: a := reg(); m := randMem(); b := size(); c := rnd(2); X64.load(a, m, b, c = 0); | |
| 96 | + | 5: m := randMem(); a := reg(); b := size(); X64.store(m, a, b); | |
| 97 | + | 6: a := reg(); m := randMem(); X64.lea(a, m); | |
| 98 | + | 7: a := rnd(8); b := reg(); c := reg(); X64.alu(a, b, c); | |
| 99 | + | 8: a := rnd(8); b := reg(); c := imm(8); X64.aluI(a, b, c); | |
| 100 | + | 9: a := rnd(8); b := reg(); m := randMem(); X64.aluM(a, b, m); | |
| 101 | + | 10: a := reg(); b := reg(); X64.imul(a, b); | |
| 102 | + | 11: a := reg(); c := imm(8); X64.imulI(a, c); | |
| 103 | + | 12: | |
| 104 | + a := rnd(3); b := reg(); | |
| 105 | + if a = 0 then a := X64.shShl; elsif a = 1 then a := X64.shShr; else a := X64.shSar; end; | |
| 106 | + X64.shift(a, b); | |
| 107 | + | 13: | |
| 108 | + a := rnd(3); b := reg(); c := rnd(64); | |
| 109 | + if a = 0 then a := X64.shShl; elsif a = 1 then a := X64.shShr; else a := X64.shSar; end; | |
| 110 | + X64.shiftI(a, b, c); | |
| 111 | + | 14: | |
| 112 | + a := rnd(4); b := reg(); | |
| 113 | + if a < 2 then a := a + 2; else a := a + 4; end; | |
| 114 | + X64.unary(a, b); | |
| 115 | + | 15: X64.cqo; | |
| 116 | + | 16: a := reg(); b := 1 shl rnd(3); c := rnd(2); X64.extend(a, b, c = 0); | |
| 117 | + | 17: a := rnd(16); b := reg(); X64.setCC(a, b); | |
| 118 | + | 18: a := reg(); b := reg(); X64.testRR(a, b); | |
| 119 | + | 19: a := reg(); X64.push(a); | |
| 120 | + | 20: a := reg(); X64.pop(a); | |
| 121 | + | 21: a := rnd(5); X64.callChunk(a); | |
| 122 | + | 22: a := rnd(1000); X64.callBack(a); | |
| 123 | + | 23: a := reg(); X64.callR(a); | |
| 124 | + | 24: a := reg(); X64.jmpR(a); | |
| 125 | + | 25: m := randMem(); X64.callM(m); | |
| 126 | + | 26: | |
| 127 | + a := rnd(5); | |
| 128 | + match a of | |
| 129 | + | 0: X64.ret; | |
| 130 | + | 1: X64.leave; | |
| 131 | + | 2: X64.syscall; | |
| 132 | + | 3: X64.mfence; | |
| 133 | + | else: X64.ud2; | |
| 134 | + end; | |
| 135 | + | 27: a := 8 shl rnd(3); X64.align(a); | |
| 136 | + | 28: | |
| 137 | + a := rnd(5); | |
| 138 | + match a of | |
| 139 | + | 0: X64.repMovsb; | |
| 140 | + | 1: X64.std; | |
| 141 | + | 2: X64.cld; | |
| 142 | + | 3: X64.repStosb; | |
| 143 | + | else: X64.repeCmpsb; | |
| 144 | + end; | |
| 145 | + | 29: m := randMem(); a := reg(); X64.lockCmpxchg(m, a); | |
| 146 | + | 30: m := randMem(); a := reg(); X64.xchg(m, a); | |
| 147 | + | 31: m := randMem(); a := reg(); X64.lockXadd(m, a); | |
| 148 | + | 32: a := reg(); b := reg(); X64.movqXR(a, b); | |
| 149 | + | 33: a := reg(); b := reg(); X64.movqRX(a, b); | |
| 150 | + | 34: a := reg(); b := reg(); X64.movX(a, b); | |
| 151 | + | 35: a := reg(); m := randMem(); b := rnd(2); X64.movsXM(a, m, b = 0); | |
| 152 | + | 36: m := randMem(); a := reg(); b := rnd(2); X64.movsMX(m, a, b = 0); | |
| 153 | + | 37: | |
| 154 | + d := rnd(5); a := reg(); b := reg(); c := rnd(2); | |
| 155 | + match d of | |
| 156 | + | 0: d := 58H; | |
| 157 | + | 1: d := 59H; | |
| 158 | + | 2: d := 5CH; | |
| 159 | + | 3: d := 5EH; | |
| 160 | + | else: d := 5AH; | |
| 161 | + end; | |
| 162 | + X64.sse(d, a, b, c = 0); | |
| 163 | + | 38: | |
| 164 | + d := rnd(5); a := reg(); m := randMem(); c := rnd(2); | |
| 165 | + match d of | |
| 166 | + | 0: d := 58H; | |
| 167 | + | 1: d := 59H; | |
| 168 | + | 2: d := 5CH; | |
| 169 | + | 3: d := 5EH; | |
| 170 | + | else: d := 5AH; | |
| 171 | + end; | |
| 172 | + X64.sseM(d, a, m, c = 0); | |
| 173 | + | 39: a := reg(); b := reg(); c := rnd(2); X64.ucomis(a, b, c = 0); | |
| 174 | + | 40: a := reg(); b := reg(); c := rnd(2); X64.cvtsi2s(a, b, c = 0); | |
| 175 | + | 41: a := reg(); b := reg(); c := rnd(2); X64.cvtts2si(a, b, c = 0); | |
| 176 | + | 42: a := reg(); b := rnd(64); X64.btc(a, b); | |
| 177 | + | 43: | |
| 178 | + a := rnd(16); b := rnd(2); | |
| 179 | + if b = 0 then X64.jmp(lab1); else X64.jcc(a, lab2); end; | |
| 180 | + | 44: | |
| 181 | + a := rnd(16); b := rnd(2); c := Chunk.pc - rnd(200); | |
| 182 | + if b = 0 then X64.jmpBack(c); else X64.jccBack(a, c); end; | |
| 183 | + | 45: | |
| 184 | + a := rnd(3); | |
| 185 | + if a = 0 then | |
| 186 | + X64.fixLink(lab1); lab1 := X64.NoLabel; | |
| 187 | + else | |
| 188 | + X64.fixLinkTo(lab2, Chunk.pc - 20); lab2 := X64.NoLabel; | |
| 189 | + end; | |
| 190 | + | 46: X64.jmp(lab1); X64.dropJumpTo(lab1); | |
| 191 | + | 47: a := X64.joinLinks(lab1, lab2); lab1 := a; lab2 := X64.NoLabel; | |
| 192 | + | else: a := rnd(256); Chunk.put1(a); | |
| 193 | + end; | |
| 194 | + end; | |
| 195 | + X64.fixLink(lab1); X64.fixLink(lab2); | |
| 196 | + Chunk.endChunk(0); | |
| 197 | + Chunk.setLive(0); | |
| 198 | + Fmt.print("pc {} fixups {} lines {} said {}\n", [Fmt.int(Chunk.pc), Fmt.int(Chunk.numFixups), | |
| 199 | + Fmt.int(Chunk.numLines), Fmt.int(X64.saidCount())]); | |
| 200 | + for i := 0 to Chunk.pc - 1 do Out.hex(U64(Chunk.text[i]), 2); end; | |
| 201 | + Out.ln; | |
| 202 | + for i := 0 to Chunk.numFixups - 1 do | |
| 203 | + Fmt.print("F {} {} {}\n", [Fmt.int(Chunk.fixups[i].at), Fmt.int(Chunk.fixups[i].chunk), Fmt.int(Chunk.fixups[i].addend)]); | |
| 204 | + end; | |
| 205 | + X64.list(-1); | |
| 206 | +end; | |
| 207 | + | |
| 208 | +end X64Dump. |
| @@ -0,0 +1,244 @@ | ||
| 1 | +#!/usr/bin/env python3 | |
| 2 | +"""Writes the files the assembler of X64 is tested on, into the directory | |
| 3 | +given: valid.raz, a long run of instructions of every kind the assembler | |
| 4 | +knows, and one small file for each error it reports. Each ends in `end`, | |
| 5 | +as a code procedure's body does. Fixed seed: the same files each run.""" | |
| 6 | +import random, sys, os | |
| 7 | + | |
| 8 | +random.seed(7) | |
| 9 | +out = sys.argv[1] | |
| 10 | +os.makedirs(out, exist_ok=True) | |
| 11 | + | |
| 12 | +R64 = "rax rcx rdx rbx rsp rbp rsi rdi r8 r9 r10 r11 r12 r13 r14 r15".split() | |
| 13 | +R32 = "eax ecx edx ebx esp ebp esi edi r8d r9d r10d r11d r12d r13d r14d r15d".split() | |
| 14 | +R16 = "ax cx dx bx sp bp si di r8w r9w r10w r11w r12w r13w r14w r15w".split() | |
| 15 | +R8 = "al cl dl bl spl bpl sil dil r8b r9b r10b r11b r12b r13b r14b r15b".split() | |
| 16 | +BY = {8: R64, 4: R32, 2: R16, 1: R8} | |
| 17 | +PRE = {8: "qword ", 4: "dword ", 2: "word ", 1: "byte "} | |
| 18 | +XM = ["xmm%d" % i for i in range(16)] | |
| 19 | +CC = "o no b ae e ne be a s ns p np l ge le g c nae nc nb z nz na nbe nge nl ng nle".split() | |
| 20 | + | |
| 21 | + | |
| 22 | +def reg(size=8): | |
| 23 | + return random.choice(BY[size]) | |
| 24 | + | |
| 25 | + | |
| 26 | +def mem(size=0): | |
| 27 | + k = random.randrange(8) | |
| 28 | + b = random.choice(R64) | |
| 29 | + i = random.choice([r for r in R64 if r != "rsp"]) | |
| 30 | + d = random.choice([0, 8, -8, 127, -128, 128, 4096, -4096, 100000]) | |
| 31 | + s = random.choice([1, 2, 4, 8]) | |
| 32 | + p = PRE[size] if size else "" | |
| 33 | + if k == 0: | |
| 34 | + t = "[%s]" % b | |
| 35 | + elif k == 1: | |
| 36 | + t = "[%s + %d]" % (b, abs(d)) if d >= 0 else "[%s - %d]" % (b, -d) | |
| 37 | + elif k == 2: | |
| 38 | + t = "[%s + %s * %d]" % (b, i, s) | |
| 39 | + elif k == 3: | |
| 40 | + t = "[%s + %s * %d + %d]" % (b, i, s, abs(d)) if d >= 0 else "[%s + %s * %d - %d]" % (b, i, s, -d) | |
| 41 | + elif k == 4: | |
| 42 | + t = "[%s + %s]" % (b, i) | |
| 43 | + elif k == 5: | |
| 44 | + t = "[%s + 0]" % b | |
| 45 | + elif k == 6: | |
| 46 | + t = "[rsp + %d]" % abs(d) | |
| 47 | + else: | |
| 48 | + t = "[%s - %d]" % (b, abs(d) + 1) | |
| 49 | + return p + t | |
| 50 | + | |
| 51 | + | |
| 52 | +def imm(size): | |
| 53 | + if size == 1: | |
| 54 | + return str(random.choice([0, 1, -1, 127, 255, -128, 77])) | |
| 55 | + if size == 2: | |
| 56 | + return str(random.choice([0, 1, -1, 300, 32767, -32768, 255, 65535])) | |
| 57 | + return str(random.choice([0, 1, -1, 127, 128, -128, -129, 100000, -100000, 2147483647, -2147483648, 4294967295])) | |
| 58 | + | |
| 59 | + | |
| 60 | +def size(): | |
| 61 | + return random.choice([1, 2, 4, 8]) | |
| 62 | + | |
| 63 | + | |
| 64 | +lines = [] | |
| 65 | + | |
| 66 | + | |
| 67 | +def emit(s): | |
| 68 | + lines.append(" " + s + ";") | |
| 69 | + | |
| 70 | + | |
| 71 | +for n in range(900): | |
| 72 | + k = random.randrange(40) | |
| 73 | + sz = size() | |
| 74 | + if k < 6: | |
| 75 | + op = random.choice("add or adc sbb and sub xor cmp".split()) | |
| 76 | + f = random.randrange(5) | |
| 77 | + if f == 0: | |
| 78 | + emit("%s %s, %s" % (op, reg(sz), reg(sz))) | |
| 79 | + elif f == 1: | |
| 80 | + emit("%s %s, %s" % (op, reg(sz), mem(sz if random.random() < .5 else 0))) | |
| 81 | + elif f == 2: | |
| 82 | + emit("%s %s, %s" % (op, mem(sz if random.random() < .5 else 0), reg(sz))) | |
| 83 | + elif f == 3: | |
| 84 | + emit("%s %s, %s" % (op, reg(sz), imm(sz))) | |
| 85 | + else: | |
| 86 | + emit("%s %s, %s" % (op, mem(sz), imm(sz))) | |
| 87 | + elif k < 10: | |
| 88 | + f = random.randrange(5) | |
| 89 | + if f == 0: | |
| 90 | + emit("mov %s, %s" % (reg(sz), reg(sz))) | |
| 91 | + elif f == 1: | |
| 92 | + emit("mov %s, %s" % (reg(sz), mem())) | |
| 93 | + elif f == 2: | |
| 94 | + emit("mov %s, %s" % (mem(), reg(sz))) | |
| 95 | + elif f == 3: | |
| 96 | + v = random.choice([0, 5, -5, 2147483647, 4294967295, 4294967296, -2147483649, 123456789012345]) | |
| 97 | + emit("mov %s, %d" % (reg(8 if sz >= 4 else sz) if sz >= 4 else reg(sz), v if sz == 8 else (v % 65536 if sz == 2 else v % 256 if sz == 1 else v % 4294967296))) | |
| 98 | + else: | |
| 99 | + emit("mov %s, %s" % (mem(sz), imm(sz))) | |
| 100 | + elif k == 10: | |
| 101 | + emit("test %s, %s" % (reg(sz), random.choice([reg(sz), imm(sz) if sz != 1 else "1"]))) | |
| 102 | + elif k == 11: | |
| 103 | + emit("xchg %s, %s" % (random.choice([reg(sz), mem()]), reg(sz))) | |
| 104 | + elif k == 12: | |
| 105 | + emit("%slock %s %s, %s" % ("", random.choice(["cmpxchg", "xadd"]), mem(), reg(sz))) | |
| 106 | + elif k == 13: | |
| 107 | + emit("lea %s, %s" % (reg(random.choice([2, 4, 8])), mem())) | |
| 108 | + elif k == 14: | |
| 109 | + op = random.choice(["movzx", "movsx"]) | |
| 110 | + emit("%s %s, %s" % (op, reg(random.choice([2, 4, 8])), random.choice([mem(random.choice([1, 2])), reg(random.choice([1, 2]))]))) | |
| 111 | + elif k == 15: | |
| 112 | + emit("movsxd %s, %s" % (reg(8), random.choice([mem(4), reg(4)]))) | |
| 113 | + elif k == 16: | |
| 114 | + emit("%s %s" % (random.choice("not neg mul imul div idiv inc dec".split()), random.choice([reg(sz), mem(sz)]))) | |
| 115 | + elif k == 17: | |
| 116 | + emit("imul %s" % ", ".join([reg(sz if sz > 1 else 4), random.choice([reg(sz if sz > 1 else 4)] * 1 + [mem()])] + ([imm(sz if sz > 1 else 4)] if random.random() < .4 else [])).replace("%", "%")) | |
| 117 | + elif k == 18: | |
| 118 | + op = random.choice("rol ror rcl rcr shl shr sal sar".split()) | |
| 119 | + emit("%s %s, %s" % (op, random.choice([reg(sz), mem(sz)]), random.choice([str(random.randrange(64)), "cl"]))) | |
| 120 | + elif k == 19: | |
| 121 | + emit("%s %s" % (random.choice(["push", "pop"]), reg(8))) | |
| 122 | + elif k == 20: | |
| 123 | + op = random.choice(["call", "jmp"]) | |
| 124 | + emit("%s %s" % (op, random.choice([reg(8), mem()]) if op == "call" else mem())) | |
| 125 | + elif k == 21: | |
| 126 | + emit("set%s %s" % (random.choice(CC), random.choice([reg(1), mem(1)]))) | |
| 127 | + elif k == 22: | |
| 128 | + emit("cmov%s %s, %s" % (random.choice(CC), reg(random.choice([2, 4, 8])), random.choice([reg(8), mem()]))) | |
| 129 | + elif k == 23: | |
| 130 | + op = random.choice("sqrt add mul sub min div max".split()) + random.choice(["sd", "ss"]) | |
| 131 | + emit("%s %s, %s" % (op, random.choice(XM), random.choice([random.choice(XM), mem()]))) | |
| 132 | + elif k == 24: | |
| 133 | + op = random.choice("punpcklbw punpcklwd pcmpeqb pand pandn por pxor paddb psubb".split()) | |
| 134 | + emit("%s %s, %s" % (op, random.choice(XM), random.choice([random.choice(XM), mem()]))) | |
| 135 | + elif k == 25: | |
| 136 | + emit("pshufd %s, %s, %d" % (random.choice(XM), random.choice([random.choice(XM), mem()]), random.randrange(256))) | |
| 137 | + elif k == 26: | |
| 138 | + op = random.choice(["movdqu", "movdqa"]) | |
| 139 | + if random.random() < .5: | |
| 140 | + emit("%s %s, %s" % (op, random.choice(XM), random.choice([random.choice(XM), mem()]))) | |
| 141 | + else: | |
| 142 | + emit("%s %s, %s" % (op, mem(), random.choice(XM))) | |
| 143 | + elif k == 27: | |
| 144 | + emit("pmovmskb %s, %s" % (reg(4), random.choice(XM))) | |
| 145 | + elif k == 28: | |
| 146 | + emit("%s %s, %s" % (random.choice("bsf bsr tzcnt lzcnt popcnt".split()), reg(random.choice([4, 8])), random.choice([reg(8), mem()]))) | |
| 147 | + elif k == 29: | |
| 148 | + op = random.choice(["movsd", "movss"]) | |
| 149 | + if random.random() < .5: | |
| 150 | + emit("%s %s, %s" % (op, random.choice(XM), random.choice([random.choice(XM), mem()]))) | |
| 151 | + else: | |
| 152 | + emit("%s %s, %s" % (op, mem(), random.choice(XM))) | |
| 153 | + elif k == 30: | |
| 154 | + op = random.choice(["movq", "movd"]) | |
| 155 | + if random.random() < .5: | |
| 156 | + emit("%s %s, %s" % (op, random.choice(XM), random.choice([reg(8), mem()]))) | |
| 157 | + else: | |
| 158 | + emit("%s %s, %s" % (op, random.choice([reg(8), mem()]), random.choice(XM))) | |
| 159 | + elif k == 31: | |
| 160 | + emit("%s %s, %s" % (random.choice(["ucomisd", "ucomiss"]), random.choice(XM), random.choice([random.choice(XM), mem()]))) | |
| 161 | + elif k == 32: | |
| 162 | + emit("%s %s, %s" % (random.choice(["cvtsi2sd", "cvtsi2ss"]), random.choice(XM), random.choice([reg(random.choice([4, 8])), mem(random.choice([4, 8]))]))) | |
| 163 | + elif k == 33: | |
| 164 | + emit("%s %s, %s" % (random.choice(["cvttsd2si", "cvttss2si"]), reg(random.choice([4, 8])), random.choice([random.choice(XM), mem()]))) | |
| 165 | + elif k == 34: | |
| 166 | + emit("%s %s, %s" % (random.choice(["cvtsd2ss", "cvtss2sd"]), random.choice(XM), random.choice([random.choice(XM), mem()]))) | |
| 167 | + elif k == 35: | |
| 168 | + emit(random.choice("ret leave syscall cpuid rdtsc pause nop ud2 int3 hlt cqo cdq mfence lfence sfence".split())) | |
| 169 | + elif k == 36: | |
| 170 | + emit("db " + ", ".join(str(random.choice([0, 1, 255, 128, 77, -1, -128])) for _ in range(random.randrange(1, 6)))) | |
| 171 | + elif k == 37: | |
| 172 | + lines.append(" L%d:" % n) | |
| 173 | + lines.append(" add rax, 1;") | |
| 174 | + lines.append(" j%s L%d;" % (random.choice(CC), n)) | |
| 175 | + lines.append(" jmp L%d;" % n) | |
| 176 | + elif k == 38: | |
| 177 | + lines.append(" jmp F%d;" % n) | |
| 178 | + lines.append(" nop;") | |
| 179 | + lines.append(" j%s F%d;" % (random.choice(CC), n)) | |
| 180 | + lines.append(" F%d:" % n) | |
| 181 | + else: | |
| 182 | + emit("lock add %s, %s" % (mem(sz), reg(sz))) | |
| 183 | + | |
| 184 | +open(os.path.join(out, "valid.raz"), "w").write("\n".join(lines) + "\n end\n") | |
| 185 | + | |
| 186 | +errors = { | |
| 187 | + "empty": "", | |
| 188 | + "badop": "foo rax;", | |
| 189 | + "noinsn": "42;", | |
| 190 | + "nosemi": "nop", | |
| 191 | + "undefined": "jmp nowhere;", | |
| 192 | + "twice": "a: nop; a: nop;", | |
| 193 | + "toomany": "add rax, rbx, rcx, rdx;", | |
| 194 | + "movimm": "mov 1, 2;", | |
| 195 | + "movmm": "mov [rax], [rbx];", | |
| 196 | + "addimm": "add 1, rax;", | |
| 197 | + "pushmem": "push [rax];", | |
| 198 | + "pushsmall": "push eax;", | |
| 199 | + "badmem": "mov rax, [rsi * 4];", | |
| 200 | + "badscale": "mov rax, [rsi + rdi * 3];", | |
| 201 | + "nomem": "mov rax, [];", | |
| 202 | + "unclosed": "mov rax, [rbx;", | |
| 203 | + "shiftcl": "shl rax, dl;", | |
| 204 | + "shiftimm": "shl 3, 1;", | |
| 205 | + "lea": "lea rax, rbx;", | |
| 206 | + "movzx": "movzx rax, rbx;", | |
| 207 | + "movzx4": "movzx rax, dword [rbx];", | |
| 208 | + "setsize": "sete rax;", | |
| 209 | + "cmovimm": "cmove rax, 5;", | |
| 210 | + "sse": "addsd rax, xmm1;", | |
| 211 | + "sse1": "addsd xmm1;", | |
| 212 | + "pshufd": "pshufd xmm1, xmm2;", | |
| 213 | + "pand3": "pand xmm1, xmm2, 3;", | |
| 214 | + "movdq": "movdqu rax, xmm1;", | |
| 215 | + "pmov": "pmovmskb xmm1, rax;", | |
| 216 | + "bits": "popcnt ax, bx;", | |
| 217 | + "movsd": "movsd rax, xmm1;", | |
| 218 | + "movq": "movq rax, rbx;", | |
| 219 | + "ucomi": "ucomisd rax, xmm1;", | |
| 220 | + "cvtsi": "cvtsi2sd rax, xmm1;", | |
| 221 | + "cvtt": "cvttsd2si xmm1, xmm2;", | |
| 222 | + "cvtss": "cvtss2sd rax, xmm1;", | |
| 223 | + "plainops": "ret rax;", | |
| 224 | + "dbrange": "db 256;", | |
| 225 | + "dbneg": "db -129;", | |
| 226 | + "imul1": "imul rax, 5;", | |
| 227 | + "imulmem": "imul [rax], rbx;", | |
| 228 | + "unary2": "not rax, rbx;", | |
| 229 | + "unaryimm": "neg 5;", | |
| 230 | + "inc": "inc 5;", | |
| 231 | + "xchgimm": "xchg rax, 5;", | |
| 232 | + "cmpxchg": "cmpxchg rax, 5;", | |
| 233 | + "calli": "call 5;", | |
| 234 | + "jmpimm": "jmp 5;", | |
| 235 | + "labels65": "".join("l%d: nop; " % i for i in range(65)), | |
| 236 | + "xmm16": "addsd xmm16, xmm1;", | |
| 237 | + "noend": None, | |
| 238 | +} | |
| 239 | +for name, body in errors.items(): | |
| 240 | + if body is None: | |
| 241 | + text = " nop;\n" | |
| 242 | + else: | |
| 243 | + text = " " + body + "\n end\n" | |
| 244 | + open(os.path.join(out, "err_%s.raz" % name), "w").write(text) |