| 1 | (* Chunk: the compiled program, before it is linked. |
| 2 | |
| 3 | The code of a program is a set of chunks: one per procedure, module |
| 4 | variable and string. A chunk's bytes go in one of three sections: |
| 5 | text (code), rodata (strings and other constants), bss (variables, |
| 6 | which are all zero). A reference from code to another chunk (a call, |
| 7 | or an address) is a fixup, patched once the linker has placed every |
| 8 | chunk. Line notes say which place in the source the code from each |
| 9 | point on is for. |
| 10 | |
| 11 | Nothing here depends on the machine: text is bytes, written by the |
| 12 | encoder for the target (X64, or A64 for arm64). |
| 13 | |
| 14 | Depends on: Scan, for names and memory; Diag, for positions. |
| 15 | Leaves out: the instructions; that is X64 or A64. |
| 16 | |
| 17 | Exported variables can't be set from outside, so the setters at the |
| 18 | end do what the seed's other units do to these. *) |
| 19 | module Chunk; |
| 20 | import Fmt, Diag, Scan; |
| 21 | import SYSTEM; |
| 22 | |
| 23 | type |
| 24 | Section* = (secText, secRodata, secBss); |
| 25 | |
| 26 | Chunk* = record |
| 27 | sec*: Section; |
| 28 | start*, size*, align*: INT; (* in its section's buffer *) |
| 29 | firstFix*, numFix*: INT; (* its fixups: text's in fixups, rodata's in dataFixups *) |
| 30 | home*, name*: INT; (* its module and its name, for the symbol table; -1 if none *) |
| 31 | adr*: INT; (* its address, set by the linker *) |
| 32 | live*: BOOL; (* reached from the entry point *) |
| 33 | ext*: BOOL; (* bss: the address of a C symbol, its name *) |
| 34 | reals*: BOOL; (* code: computes with reals, first at realsAt *) |
| 35 | realsAt*: Diag.Pos; |
| 36 | saveMask*: INT; (* code: the registers of variables it saves, as bits of Gen's VarRegs *) |
| 37 | saveAt*: INT; (* ... at this offset from the frame, one quadword each *) |
| 38 | missing*: []const U8; (* code: what the target's Gen can't make yet, first at missingAt; "" if none *) |
| 39 | missingAt*: Diag.Pos; |
| 40 | end; |
| 41 | |
| 42 | (* A place in the source whose code starts at a place in text: for |
| 43 | gdb, and for Rt to say where a failed check or a signal was *) |
| 44 | Line* = record |
| 45 | at*, chunk*: INT; (* the place in text, and its chunk *) |
| 46 | path*, line*, col*: INT; (* the file's path, interned, the line and column *) |
| 47 | end; |
| 48 | |
| 49 | (* A variable of a procedure, for the trap report: Rt shows it, by |
| 50 | kind, from its place in the frame *) |
| 51 | VarNote* = record |
| 52 | chunk*, name*: INT; |
| 53 | off*: INT; (* from the frame pointer *) |
| 54 | kind*: INT; (* how Rt shows it: see Link *) |
| 55 | end; |
| 56 | |
| 57 | (* How a fixup's field is patched. fxRel32 is x86-64's: 32 bits, the |
| 58 | distance from the field's end, or with abs the address itself. The |
| 59 | others are arm64's (A64), each a field of the instruction at at, |
| 60 | as the Mach-O relocations of the same names: |
| 61 | fxBranch26 b or bl: the distance in words, in imm26 |
| 62 | fxPage21 adrp: the distance in 4 KB pages from the |
| 63 | instruction's page, in immhi:immlo |
| 64 | fxPageOff12 add, or a load or store's unsigned offset: the |
| 65 | address's place in its page, in imm12, divided |
| 66 | by the size a load or store reads from its own |
| 67 | bits (31 and 30, or 16 for a q register), as |
| 68 | ARM64_RELOC_PAGEOFF12 is |
| 69 | Link patches them all. *) |
| 70 | FixKind* = (fxRel32, fxBranch26, fxPage21, fxPageOff12); |
| 71 | |
| 72 | Fixup* = record |
| 73 | at*: INT; (* the 32-bit field, as an offset in text; in |
| 74 | dataFixups, the 64-bit one, from its chunk's start *) |
| 75 | chunk*: INT; (* the chunk it refers to *) |
| 76 | addend*: INT; (* added to the chunk's address *) |
| 77 | abs*: BOOL; (* text: the address itself, not from the field's end *) |
| 78 | kind*: FixKind; (* text: how the field is patched *) |
| 79 | end; |
| 80 | |
| 81 | var |
| 82 | text*: ^U8; (* text *) |
| 83 | pc*: INT; (* its length *) |
| 84 | rodata*: ^U8; |
| 85 | rolen*: INT; |
| 86 | chunks*: ^Chunk; |
| 87 | numChunks*: INT; |
| 88 | fixups*: ^Fixup; |
| 89 | numFixups*: INT; |
| 90 | (* Addresses in read-only data, as in an array constant of strings: |
| 91 | each 8 bytes, the chunk's absolute address, since the executable |
| 92 | is loaded where the linker placed it *) |
| 93 | dataFixups*: ^Fixup; |
| 94 | numDataFixups*: INT; |
| 95 | lines*: ^Line; |
| 96 | numLines*: INT; |
| 97 | curFile: INT; (* the file being compiled, for noteLine *) |
| 98 | curChunk*: INT; (* the chunk of code being written *) |
| 99 | varNotes*: ^VarNote; |
| 100 | numVarNotes*: INT; |
| 101 | (* What fills the gaps between procedures in text, Gen's (setPad): a |
| 102 | word of four bytes, the byte at an offset that is i more than a |
| 103 | multiple of 4 its byte i, a trap if it were ever run: int3, CC, |
| 104 | on x86, brk on arm64 *) |
| 105 | pad*: INT; |
| 106 | |
| 107 | codeCap, roCap, chunkCap, fixCap, dataFixCap, lineCap, noteCap: INT; |
| 108 | |
| 109 | (* Room for n more bytes of code *) |
| 110 | proc room(n: INT); |
| 111 | begin |
| 112 | if pc + n > codeCap then |
| 113 | text := Scan.regrab(text, pc, codeCap * 2 + n); |
| 114 | codeCap := codeCap * 2 + n; |
| 115 | end; |
| 116 | end; |
| 117 | |
| 118 | proc put1*(b: INT); |
| 119 | begin |
| 120 | if pc + 16 > codeCap then room(16); end; |
| 121 | text[pc] := U8(b & 0FFH); |
| 122 | INC(pc); |
| 123 | end; |
| 124 | |
| 125 | (* The low 32 bits of v, least significant byte first *) |
| 126 | proc put4*(v: INT); |
| 127 | var i: INT; |
| 128 | begin |
| 129 | if pc + 16 > codeCap then room(16); end; |
| 130 | for i := 0 to 3 do text[pc + i] := U8((v shr (8 * i)) & 0FFH); end; |
| 131 | INC(pc, 4); |
| 132 | end; |
| 133 | |
| 134 | proc put4At*(at, v: INT); |
| 135 | var i: INT; |
| 136 | begin |
| 137 | for i := 0 to 3 do text[at + i] := U8((v shr (8 * i)) & 0FFH); end; |
| 138 | end; |
| 139 | |
| 140 | (* The 32 bits at at, as a signed number *) |
| 141 | proc get4At*(at: INT): INT; |
| 142 | var u, i: INT; |
| 143 | begin |
| 144 | u := 0; |
| 145 | for i := 0 to 3 do u := u * 256 + INT(text[at + 3 - i]); end; |
| 146 | if u >= 80000000H then u := u - 100000000H; end; |
| 147 | return u; |
| 148 | end; |
| 149 | |
| 150 | (* The last n bytes of code taken back. A line noted past the new end |
| 151 | now starts there, in place of one noted there before, as noteLine |
| 152 | would have it. *) |
| 153 | proc takeBack*(n: INT); |
| 154 | begin |
| 155 | DEC(pc, n); |
| 156 | if (numLines > 0) and (lines[numLines - 1].at > pc) then |
| 157 | lines[numLines - 1].at := pc; |
| 158 | if (numLines > 1) and (lines[numLines - 2].at = pc) then |
| 159 | lines[numLines - 2] := lines[numLines - 1]; |
| 160 | DEC(numLines); |
| 161 | end; |
| 162 | end; |
| 163 | end; |
| 164 | |
| 165 | proc put8*(v: I64); |
| 166 | begin |
| 167 | put4(INT(v & 0FFFFFFFFH)); |
| 168 | put4(INT((v shr 32) & 0FFFFFFFFH)); |
| 169 | end; |
| 170 | |
| 171 | proc newChunk*(sec: Section; home, name: INT): INT; |
| 172 | var c: ^Chunk; |
| 173 | begin |
| 174 | if numChunks = chunkCap then |
| 175 | chunks := ^Chunk(Scan.regrab(^U8(chunks), numChunks * SIZE(Chunk), chunkCap * 2 * SIZE(Chunk))); |
| 176 | chunkCap := chunkCap * 2; |
| 177 | end; |
| 178 | c := SYSTEM.ADR(chunks[numChunks]); |
| 179 | c^ := {}; |
| 180 | c^.sec := sec; |
| 181 | c^.home := home; |
| 182 | c^.name := name; |
| 183 | c^.align := 1; |
| 184 | c^.size := -1; (* not yet defined *) |
| 185 | INC(numChunks); |
| 186 | return numChunks - 1; |
| 187 | end; |
| 188 | |
| 189 | proc beginChunk*(c: INT); |
| 190 | begin |
| 191 | (* code is aligned to 32 bytes, the windows in which x86 fetches and |
| 192 | caches it, so that a procedure's loops sit in them the same way |
| 193 | whatever comes before: rat ran 5% slower at 16, by where its loops |
| 194 | fell. Padded with Gen's trap (pad) *) |
| 195 | while pc & 31 # 0 do put1(pad shr (8 * (pc & 3))); end; |
| 196 | curChunk := c; |
| 197 | chunks[c].start := pc; |
| 198 | chunks[c].align := 32; |
| 199 | chunks[c].firstFix := numFixups; |
| 200 | end; |
| 201 | |
| 202 | proc endChunk*(c: INT); |
| 203 | begin |
| 204 | chunks[c].size := pc - chunks[c].start; |
| 205 | chunks[c].numFix := numFixups - chunks[c].firstFix; |
| 206 | end; |
| 207 | |
| 208 | (* The n bytes at p are the data of rodata chunk c *) |
| 209 | proc setData*(c: INT; p: ^U8; n, align: INT); |
| 210 | begin |
| 211 | if rolen + n > roCap then |
| 212 | rodata := Scan.regrab(rodata, rolen, roCap * 2 + n); |
| 213 | roCap := roCap * 2 + n; |
| 214 | end; |
| 215 | COPY(rodata[rolen..rolen + n], p[0..n]); |
| 216 | chunks[c].firstFix := numDataFixups; |
| 217 | chunks[c].numFix := 0; |
| 218 | chunks[c].start := rolen; |
| 219 | chunks[c].size := n; |
| 220 | chunks[c].align := align; |
| 221 | INC(rolen, n); |
| 222 | end; |
| 223 | |
| 224 | proc setBss*(c: INT; size, align: INT); |
| 225 | begin |
| 226 | chunks[c].size := size; |
| 227 | chunks[c].align := align; |
| 228 | end; |
| 229 | |
| 230 | (* The code from here on is for line and column of the current file. A |
| 231 | place with no code of its own gives way to the next. *) |
| 232 | proc noteLine*(line, col: INT); |
| 233 | begin |
| 234 | if (numLines > 0) and (lines[numLines - 1].at = pc) then DEC(numLines); end; |
| 235 | if (numLines > 0) and (lines[numLines - 1].chunk = curChunk) and (lines[numLines - 1].line = line) |
| 236 | and (lines[numLines - 1].col = col) and (lines[numLines - 1].path = curFile) then |
| 237 | return; |
| 238 | end; |
| 239 | if numLines = lineCap then |
| 240 | lines := ^Line(Scan.regrab(^U8(lines), numLines * SIZE(Line), lineCap * 2 * SIZE(Line))); |
| 241 | lineCap := lineCap * 2; |
| 242 | end; |
| 243 | lines[numLines].at := pc; |
| 244 | lines[numLines].chunk := curChunk; |
| 245 | lines[numLines].path := curFile; |
| 246 | lines[numLines].line := line; |
| 247 | lines[numLines].col := col; |
| 248 | INC(numLines); |
| 249 | end; |
| 250 | |
| 251 | (* The quadword that holds the address of C's symbol name, filled in |
| 252 | when the program starts; one for each name. *) |
| 253 | proc externChunk*(name: INT): INT; |
| 254 | var c: INT; |
| 255 | begin |
| 256 | for c := 0 to numChunks - 1 do |
| 257 | if chunks[c].ext and (chunks[c].name = name) then return c; end; |
| 258 | end; |
| 259 | c := newChunk(secBss, -1, name); |
| 260 | setBss(c, 8, 8); |
| 261 | chunks[c].ext := true; |
| 262 | return c; |
| 263 | end; |
| 264 | |
| 265 | proc addFixup*(at, chunk, addend: INT); |
| 266 | begin |
| 267 | if numFixups = fixCap then |
| 268 | fixups := ^Fixup(Scan.regrab(^U8(fixups), numFixups * SIZE(Fixup), fixCap * 2 * SIZE(Fixup))); |
| 269 | fixCap := fixCap * 2; |
| 270 | end; |
| 271 | fixups[numFixups].at := at; |
| 272 | fixups[numFixups].chunk := chunk; |
| 273 | fixups[numFixups].addend := addend; |
| 274 | fixups[numFixups].abs := false; |
| 275 | fixups[numFixups].kind := fxRel32; |
| 276 | INC(numFixups); |
| 277 | end; |
| 278 | |
| 279 | (* The same, of an arm64 kind: the instruction at at *) |
| 280 | proc addKindFixup*(at, chunk, addend: INT; kind: FixKind); |
| 281 | begin |
| 282 | addFixup(at, chunk, addend); |
| 283 | fixups[numFixups - 1].kind := kind; |
| 284 | end; |
| 285 | |
| 286 | (* The same, for X64.memAbs: the field holds the address itself *) |
| 287 | proc addAbsFixup*(at, chunk, addend: INT); |
| 288 | begin |
| 289 | addFixup(at, chunk, addend); |
| 290 | fixups[numFixups - 1].abs := true; |
| 291 | end; |
| 292 | |
| 293 | (* The address of chunk, plus addend, at offset at of rodata chunk c, |
| 294 | whose data is set: its fixups follow one another. *) |
| 295 | proc addDataFixup*(c, at, chunk, addend: INT); |
| 296 | begin |
| 297 | if numDataFixups = dataFixCap then |
| 298 | dataFixups := ^Fixup(Scan.regrab(^U8(dataFixups), numDataFixups * SIZE(Fixup), dataFixCap * 2 * SIZE(Fixup))); |
| 299 | dataFixCap := dataFixCap * 2; |
| 300 | end; |
| 301 | dataFixups[numDataFixups].at := at; |
| 302 | dataFixups[numDataFixups].chunk := chunk; |
| 303 | dataFixups[numDataFixups].addend := addend; |
| 304 | INC(numDataFixups); |
| 305 | INC(chunks[c].numFix); |
| 306 | end; |
| 307 | |
| 308 | proc noteVar*(chunk, name, off, kind: INT); |
| 309 | begin |
| 310 | if numVarNotes = noteCap then |
| 311 | varNotes := ^VarNote(Scan.regrab(^U8(varNotes), numVarNotes * SIZE(VarNote), noteCap * 2 * SIZE(VarNote))); |
| 312 | noteCap := noteCap * 2; |
| 313 | end; |
| 314 | varNotes[numVarNotes].chunk := chunk; |
| 315 | varNotes[numVarNotes].name := name; |
| 316 | varNotes[numVarNotes].off := off; |
| 317 | varNotes[numVarNotes].kind := kind; |
| 318 | INC(numVarNotes); |
| 319 | end; |
| 320 | |
| 321 | (* The name of chunk c: its module's and its own, or literal and its |
| 322 | number for a literal *) |
| 323 | proc chunkName*(c: INT): []const U8; |
| 324 | begin |
| 325 | if chunks[c].name < 0 then return Diag.say("literal{}", [Fmt.int(c)]); end; |
| 326 | if chunks[c].home < 0 then return Scan.nameOf(chunks[c].name); end; |
| 327 | return Diag.say("{}.{}", [Fmt.str(Scan.nameOf(chunks[c].home)), Fmt.str(Scan.nameOf(chunks[c].name))]); |
| 328 | end; |
| 329 | |
| 330 | (* What Gen's Redo and Rewind put back: the code, fixups and lines made |
| 331 | since are forgotten *) |
| 332 | proc rewindTo*(atPc, atFixups, atLines: INT); |
| 333 | begin |
| 334 | pc := atPc; |
| 335 | numFixups := atFixups; |
| 336 | numLines := atLines; |
| 337 | end; |
| 338 | |
| 339 | (* The file the code is being made from, as its path interned *) |
| 340 | proc setCurFile*(path: INT); |
| 341 | begin |
| 342 | curFile := path; |
| 343 | end; |
| 344 | |
| 345 | proc curFilePath*: INT; |
| 346 | begin |
| 347 | return curFile; |
| 348 | end; |
| 349 | |
| 350 | proc setLive*(c: INT); |
| 351 | begin |
| 352 | chunks[c].live := true; |
| 353 | end; |
| 354 | |
| 355 | proc setAdr*(c: INT; a: INT); |
| 356 | begin |
| 357 | chunks[c].adr := a; |
| 358 | end; |
| 359 | |
| 360 | (* Chunk c computes with reals, the first of them at p *) |
| 361 | proc setReals*(c: INT; p: Diag.Pos); |
| 362 | begin |
| 363 | chunks[c].reals := true; |
| 364 | chunks[c].realsAt := p; |
| 365 | end; |
| 366 | |
| 367 | (* Chunk c holds code its Gen can't make yet, what, the first at p: an |
| 368 | error if c is live (Raz), as a program can't use what isn't made *) |
| 369 | proc setMissing*(c: INT; what: []const U8; p: Diag.Pos); |
| 370 | begin |
| 371 | if LEN(chunks[c].missing) = 0 then |
| 372 | chunks[c].missing := what; |
| 373 | chunks[c].missingAt := p; |
| 374 | end; |
| 375 | end; |
| 376 | |
| 377 | proc setPad*(word: INT); |
| 378 | begin |
| 379 | pad := word; |
| 380 | end; |
| 381 | |
| 382 | proc setSaves*(c, mask, at: INT); |
| 383 | begin |
| 384 | chunks[c].saveMask := mask; |
| 385 | chunks[c].saveAt := at; |
| 386 | end; |
| 387 | |
| 388 | begin |
| 389 | codeCap := 1 shl 20; |
| 390 | text := Scan.grab(codeCap); |
| 391 | pc := 0; |
| 392 | roCap := 1 shl 16; |
| 393 | rodata := Scan.grab(roCap); |
| 394 | rolen := 0; |
| 395 | chunkCap := 1 shl 10; |
| 396 | chunks := ^Chunk(Scan.grab(chunkCap * SIZE(Chunk))); |
| 397 | numChunks := 0; |
| 398 | fixCap := 1 shl 12; |
| 399 | fixups := ^Fixup(Scan.grab(fixCap * SIZE(Fixup))); |
| 400 | numFixups := 0; |
| 401 | dataFixCap := 1 shl 8; |
| 402 | dataFixups := ^Fixup(Scan.grab(dataFixCap * SIZE(Fixup))); |
| 403 | numDataFixups := 0; |
| 404 | lineCap := 1 shl 12; |
| 405 | lines := ^Line(Scan.grab(lineCap * SIZE(Line))); |
| 406 | numLines := 0; |
| 407 | noteCap := 1 shl 8; |
| 408 | varNotes := ^VarNote(Scan.grab(noteCap * SIZE(VarNote))); |
| 409 | numVarNotes := 0; |
| 410 | end Chunk. |