Step 7: the parser reaches the machine only through gen Three layers, none of the upper two knowing x86-64: - parse.pas: syntax; jumps and labels through gen. - item.pas (was gen.pas): Wirth's items, type checks, folding, where variables are (TAdr: frame, chunk, address on the stack, address in a slot); drives gen. - gen.pas (new): a stack machine for x86-64. PushConst/PushAdr/PushVar/ PushProc/PopVar, Op/Compare/Neg/Narrow/conversions with the kind of each value (word, long, F32, F64), JumpIf/JumpCmp/JumpKeep, checks, IndexSlice/Slice, Call, frames, defers, Atomic/SysCall/Fence, and the entry point (EntryPoint, from raz.pas). Top of stack in rax, the rest pushed; constants noted, not loaded, until needed. chunk.pas splits out of x64.pas: buffers, chunks, fixups, line notes. A port is a new gen.pas and encoder. All tests give the same output; bench 957k lines/s (was 1.25M). handtest and enctest call MarkLive, which WriteElf stopped doing in step 6: make tests had been failing since.
| @@ -16,7 +16,7 @@ | ||
| 16 | 16 | FORCE: |
| 17 | 17 | |
| 18 | 18 | units: src/*.pas | build |
| 19 | - for u in diag scan sym x64 link elf; do $(FPC) $(FPCFLAGS) src/$$u.pas || exit 1; done | |
| 19 | + for u in diag scan sym chunk x64 link elf; do $(FPC) $(FPCFLAGS) src/$$u.pas || exit 1; done | |
| 20 | 20 | |
| 21 | 21 | # tests of the parts below the parser, built from test/ |
| 22 | 22 | tests: units |
| @@ -55,7 +55,7 @@ | ||
| 55 | 55 | - [x] 6. `examples/Sh.raz`: pipelines, redirection, `cd`, `&`, `wait`, |
| 56 | 56 | and Ctrl-C to the job in the foreground, in a few hundred lines. |
| 57 | 57 | Its test runs scripts given with `-c`. |
| 58 | -- [ ] 7. rat, rlX11, rmacs and ravite on this raz, in their own | |
| 58 | +- [x] 7. rat, rlX11, rmacs and ravite on this raz, in their own | |
| 59 | 59 | repositories: the four copies of UTF-8 encoding go to |
| 60 | 60 | `Str.encode`. |
| 61 | 61 | - [x] 8. The freeze, in CLAUDE.md and the report's first section: the |
| @@ -129,7 +129,7 @@ | ||
| 129 | 129 | labels, no value in two, `else` required (7.4); compares and |
| 130 | 130 | branches, no jump table. `examples/Sh.raz`'s chains of `elsif` |
| 131 | 131 | become `match`. |
| 132 | -- [ ] 7. The parser reaches the machine only through `gen`: calls of | |
| 132 | +- [x] 7. The parser reaches the machine only through `gen`: calls of | |
| 133 | 133 | a stack machine, with the kind of each value (a word, a 64-bit |
| 134 | 134 | integer, an `F32`, an `F64`) and the place in the source, and no |
| 135 | 135 | x86-64 in the parser; a port is a new `gen`. Every test gives |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | -The compiler is nine files of Free Pascal, under 6,500 lines of code. | |
| 3 | +The compiler is eleven files of Free Pascal, under 7,000 lines of code. | |
| 4 | 4 | This guide walks through them in the order they are best read: from the |
| 5 | 5 | characters of a source file to the bytes of an executable. It assumes |
| 6 | 6 | you know the language from `doc/raz.md`, and enough x86-64 to read a |
| @@ -11,12 +11,14 @@ | ||
| 11 | 11 | | `src/diag.pas` | 274 | 198 | Messages: positions, source excerpts, colour, counts | |
| 12 | 12 | | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | |
| 13 | 13 | | `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout | |
| 14 | -| `src/x64.pas` | 695 | 559 | Code buffer, chunks, fixups, places, instruction encoder | | |
| 15 | -| `src/gen.pas` | 1,750 | 1,493 | Items: loading, storing, copying, frames, calls | | |
| 16 | -| `src/parse.pas` | 2,468 | 2,222 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 14 | +| `src/chunk.pas` | 244 | 192 | The compiled program: chunks, fixups, line notes | | |
| 15 | +| `src/x64.pas` | 476 | 377 | The instruction encoder, and jumps within a procedure | | |
| 16 | +| `src/gen.pas` | 1,206 | 963 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 17 | +| `src/item.pas` | 1,303 | 1,090 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | +| `src/parse.pas` | 2,538 | 2,276 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 17 | 19 | | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | |
| 18 | 20 | | `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| 19 | -| `src/raz.pas` | 234 | 185 | The command line, the entry points | | |
| 21 | +| `src/raz.pas` | 188 | 141 | The command line | | |
| 20 | 22 | |
| 21 | 23 | Every file opens with a comment that says what it is, what it depends |
| 22 | 24 | on and what it leaves out. Read those first; they are the map. |
| @@ -28,16 +30,26 @@ | ||
| 28 | 30 | is kept to be looked at again, except the declarations, which live in |
| 29 | 31 | the symbol table for as long as the compiler runs. |
| 30 | 32 | |
| 31 | -The code generator is a stack machine through `rax`. An expression's | |
| 32 | -value is computed into `rax`; when a second operand has to be computed, | |
| 33 | -the first is pushed, and popped into `rax` when both are ready, the | |
| 34 | -second going to `rcx`; if the second needed no code of its own (a | |
| 35 | -constant, or a plain variable), the push is taken back instead. No | |
| 36 | -register is ever allocated, so there is no register allocator, and | |
| 37 | -every access the program writes is one instruction, in the order | |
| 38 | -written. That is also why `VOLATILE` costs the compiler nothing: it | |
| 39 | -never does anything clever to begin with. | |
| 33 | +Three layers do it. `parse.pas` knows the language's syntax. `item.pas` | |
| 34 | +knows what the program means: each operand is an *item*, as in Wirth's | |
| 35 | +compilers, and the routines there check the items' types, fold | |
| 36 | +constants and work out where variables are. Neither knows the machine: | |
| 37 | +they drive a stack machine, `gen.pas`, by its calls (push a value, | |
| 38 | +add the two on top, store the top in a variable, jump if it is false), | |
| 39 | +each with the kind of the value (a word, a 64-bit integer, an `F32`, | |
| 40 | +an `F64`). `gen.pas` is the only one that knows x86-64; a port to | |
| 41 | +another machine is a new `gen.pas`, with an encoder of its own in place | |
| 42 | +of `x64.pas`. | |
| 40 | 43 | |
| 44 | +On x86-64 the stack machine's stack is the machine's: the value on | |
| 45 | +top is in `rax`, and the values under it are pushed when another is | |
| 46 | +computed; a constant is only noted, until something needs it in a | |
| 47 | +register, so that it can be an instruction's operand. No register is | |
| 48 | +ever allocated, so there is no register allocator, and every access | |
| 49 | +the program writes is one instruction, in the order written. That is | |
| 50 | +also why `VOLATILE` costs the compiler nothing: it never does anything | |
| 51 | +clever to begin with. | |
| 52 | + | |
| 41 | 53 | The output is a set of *chunks*: one for each procedure, each module |
| 42 | 54 | variable, each string. Code refers to other chunks through *fixups*, |
| 43 | 55 | which aren't resolved until the end. When everything is compiled, the |
| @@ -64,9 +76,9 @@ | ||
| 64 | 76 | 4. Each procedure becomes a chunk of code; `Out.str("hello, world\n")` |
| 65 | 77 | becomes a string chunk, a call, and a fixup to `Out.str`'s chunk. |
| 66 | 78 | 5. Back in `raz.pas`, `Use` marks the modules the program uses; the |
| 67 | - entry point, `_start`, is written as one more chunk: save `argc` and | |
| 68 | - `argv` and the address of the places (below), call every used | |
| 69 | - module's body, call `main`, call `Rt.exit`. | |
| 79 | + entry point, `_start`, is written by `gen.pas`'s `EntryPoint` as one | |
| 80 | + more chunk: save `argc` and `argv` and the address of the places | |
| 81 | + (below), call every used module's body, call `main`, call `Rt.exit`. | |
| 70 | 82 | 6. `MarkLive` (`link.pas`) marks what `_start` reaches; then |
| 71 | 83 | `WriteElf` (`elf.pas`) has `link.pas` place it and patch it, and |
| 72 | 84 | writes headers, code, data, symbols and line numbers. |
| @@ -89,8 +101,8 @@ | ||
| 89 | 101 | mov QWORD PTR [rbp-0x8],rdi ; the parameters, into their slots |
| 90 | 102 | mov QWORD PTR [rbp-0x10],rsi |
| 91 | 103 | mov rax,QWORD PTR [rbp-0x8] ; a |
| 92 | -push rax ; kept while b * 2 is computed | |
| 93 | -mov rax,QWORD PTR [rbp-0x10] ; b, its push taken back for the 2 | |
| 104 | +push rax ; kept while b is loaded | |
| 105 | +mov rax,QWORD PTR [rbp-0x10] ; b; the 2 is only noted | |
| 94 | 106 | shl rax,0x1 ; times 2 is a shift |
| 95 | 107 | mov rcx,rax |
| 96 | 108 | pop rax ; a again |
| @@ -102,9 +114,10 @@ | ||
| 102 | 114 | ret |
| 103 | 115 | ``` |
| 104 | 116 | |
| 105 | -Every line of it can be traced to a routine in `gen.pas`; the pushes | |
| 106 | -and pops are `Op1` and `Op2`, the frame `EnterBody` and `EndProc`. It | |
| 107 | -is not fast code. It is code whose every instruction you can account | |
| 117 | +Every line of it can be traced to a routine in `gen.pas`; the push is | |
| 118 | +`Spill`'s, as `PushVar` loads `b`, the pop `PopTo`'s, as `Op` takes | |
| 119 | +its operands, and the frame is `EnterBody`'s and `EndProc`'s. It is | |
| 120 | +not fast code. It is code whose every instruction you can account | |
| 108 | 121 | for, which is the point. |
| 109 | 122 | |
| 110 | 123 | ## diag.pas: messages |
| @@ -197,50 +210,110 @@ | ||
| 197 | 210 | for different targets agree. The universe, the basic types and built-in |
| 198 | 211 | procedures, is set up in the unit's `initialization`. |
| 199 | 212 | |
| 200 | -## x64.pas: machine code | |
| 213 | +## chunk.pas: the compiled program | |
| 201 | 214 | |
| 202 | 215 | Code is written into one growing buffer, `code`, with `pc` its length; |
| 203 | -strings go to `rodata`. A chunk (`TChunk`) records its section, where | |
| 204 | -its bytes start, how many, and the fixups it makes. `BeginChunk` and | |
| 205 | -`EndChunk` bracket a procedure; `SetData` and `SetBss` make data | |
| 206 | -chunks. | |
| 216 | +strings and other constants go to `rodata`. A chunk (`TChunk`) records | |
| 217 | +its section, where its bytes start, how many, and the fixups it makes. | |
| 218 | +`BeginChunk` and `EndChunk` bracket a procedure; `SetData` and `SetBss` | |
| 219 | +make data chunks. | |
| 207 | 220 | |
| 208 | 221 | A *fixup* (`TFixup`) is a 32-bit field in the code that will hold the |
| 209 | 222 | distance to another chunk: a call's target, or a rip-relative address. |
| 210 | -`AddFixup` records one; `link.pas` fills them in. | |
| 223 | +`AddFixup` records one; `link.pas` fills them in. `NoteLine` records | |
| 224 | +which line and column of the source the code from `pc` on belongs to, | |
| 225 | +for gdb and for the places. `ExternChunk` makes, once per C symbol, | |
| 226 | +the quadword the dynamic loader will fill with that symbol's address. | |
| 227 | +None of this depends on the machine. | |
| 211 | 228 | |
| 212 | -Jumps within a procedure don't wait for the linker. A label not yet | |
| 213 | -placed is a chain of the jumps to it, each jump's 32-bit field holding | |
| 214 | -the position of the one before (`Jmp`, `Jcc`); `FixLink` walks the | |
| 215 | -chain and patches each once the label's position is known. This is the | |
| 216 | -trick from Wirth's compilers, and it means a label is one integer. | |
| 229 | +## x64.pas: machine code | |
| 217 | 230 | |
| 218 | 231 | The encoder is `OpM` and `OpR`: a prefix, the REX byte, the opcode, the |
| 219 | 232 | ModRM and SIB bytes and the displacement, for a register and a memory |
| 220 | 233 | operand (`TMem`) or two registers. Everything else (`MovRR`, `Load`, |
| 221 | 234 | `Store`, `Alu`, `Sse`, ...) is a line or two that calls them with the |
| 222 | 235 | right opcode. Only the instructions the code generator uses are here. |
| 223 | -`test/enctest.pas` emits one of each for `objdump` to check. | |
| 236 | +`test/enctest.pas` emits one of each for `objdump` to check. `Here` is | |
| 237 | +the address of the code being written, as an operand. | |
| 224 | 238 | |
| 225 | -Two more things live here because they are tied to code positions: | |
| 226 | -`NoteLine` records which line and column of the source the code from | |
| 227 | -`pc` on belongs to, for gdb and for the places; `Here` is the address | |
| 228 | -of the code being written, as an operand. `ExternChunk` makes, once per C symbol, the quadword the | |
| 229 | -dynamic loader will fill with that symbol's address. | |
| 239 | +Jumps within a procedure don't wait for the linker. A label not yet | |
| 240 | +placed is a chain of the jumps to it, each jump's 32-bit field holding | |
| 241 | +the position of the one before (`Jmp`, `Jcc`); `FixLink` walks the | |
| 242 | +chain and patches each once the label's position is known. This is the | |
| 243 | +trick from Wirth's compilers, and it means a label is one integer. | |
| 244 | + | |
| 245 | +## gen.pas: the stack machine | |
| 246 | + | |
| 247 | +This is the only unit that knows the machine. The calls it answers: | |
| 248 | + | |
| 249 | +- values: `PushConst`, `PushAdr` (the address of a variable), | |
| 250 | + `PushVar` (its value), `PushProc`, and `PopVar`, which stores the | |
| 251 | + value on top; `Drop` and `Swap`; | |
| 252 | +- operations on the values on top: `Op`, `Compare`, `Neg`, | |
| 253 | + `Complement`, `NotBool`, `Narrow` (to a narrow integer's width), | |
| 254 | + the conversions between integers and reals, `CompareSeq` (of two | |
| 255 | + slices), and copying: `CopyBlock`, `CopyTo`, `ZeroVar`; | |
| 256 | +- jumps: `Jump`, `JumpIf` (on a `BOOL`), `JumpCmp` (on two integers), | |
| 257 | + `JumpKeep` and `FixKeep` for `and` and `or`, `Fix` where the label | |
| 258 | + is; | |
| 259 | +- checks: `CheckIndex`, `CheckEnum`, and `IndexSlice`, `Slice` and | |
| 260 | + `SlicePtr`, which check as they make their address or slice; | |
| 261 | +- calls (`Call`), frames (`BeginProc`, `EnterBody`, `ReturnJump`, | |
| 262 | + `EndProc`, the temporaries of `AllocTemp`), deferred statements | |
| 263 | + (`DeferBegin`, `DeferEnd`, `RunDefer`), the built-ins that are | |
| 264 | + instructions (`Atomic`, `SysCall`, `Fence`), and the program's entry | |
| 265 | + point (`EntryPoint`). | |
| 230 | 266 | |
| 231 | -## gen.pas: the code generator | |
| 267 | +A variable's place is a `TAdr`: relative to the frame (`bFrame`), to a | |
| 268 | +chunk (`bGlobal`), to an address on the stack (`bStack`), or to one | |
| 269 | +held in a frame slot (`bSlot`, as a `var` parameter's is); plus `off`. | |
| 270 | +`Place` turns it into an x64 memory operand, popping or loading the | |
| 271 | +address first, so that a variable is read or written with one | |
| 272 | +instruction. | |
| 232 | 273 | |
| 233 | -This is the heart, and the file to read slowly. | |
| 274 | +The stack: `stack` notes each value, a constant or one computed; | |
| 275 | +`inRax` is the one in `rax`, if any. `Computed` puts a value just | |
| 276 | +computed in `rax` on top; `Spill` pushes the one in `rax` first, when | |
| 277 | +`rax` is wanted for another; `PopTo` brings the value on top into a | |
| 278 | +register, from the constant, from `rax` or by a pop. `pushed` counts | |
| 279 | +what is on the machine's stack, which a call must align. `Op` takes a | |
| 280 | +constant on the right as it is, and a power of two on the right of | |
| 281 | +`*`, `div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow. | |
| 282 | +Narrow integers are brought back to their width after each operation, | |
| 283 | +which is how `U8` arithmetic wraps; since they are computed in 64 bits, | |
| 284 | +a shift's count is cut to their width first, and `DivTrap` makes the | |
| 285 | +smallest value of one divided by -1 fault as an `I64`'s does. | |
| 234 | 286 | |
| 287 | +Checks: `Check` jumps over a call to `Rt.fail` when the check holds. | |
| 288 | +The call passes what failed, the values involved and its own address, | |
| 289 | +by which `Rt` finds the place; that code has the place of the | |
| 290 | +offending expression (`LineCol` in `scan.pas`), and the code after it | |
| 291 | +the statement's again. | |
| 292 | + | |
| 293 | +Calls: the arguments are in frame slots by the time `Call` runs, one | |
| 294 | +each, computed left to right; only then are they loaded into the | |
| 295 | +registers System V wants, and the stack aligned for the call. A record | |
| 296 | +passed by value goes as the address of a copy; one returned comes back | |
| 297 | +through a hidden pointer in `rdi`, as C's structs do, and so do several | |
| 298 | +results (`ResultTo`, `CopyResult`). | |
| 299 | + | |
| 300 | +Frames: `BeginProc` gives the parameters their slots; `EnterBody` | |
| 301 | +writes the prologue, which zeroes the locals (with a few stores, or a | |
| 302 | +loop for more; there are none to zero in `scale`) but not the | |
| 303 | +temporaries, since each is written before it is read; `EndProc` writes | |
| 304 | +the epilogue and patches the frame's size in. Within a statement, | |
| 305 | +scratch space comes from `AllocTemp` and is given back by | |
| 306 | +`ReleaseTemps`; `AllocPerm` reserves space for the whole procedure. | |
| 307 | + | |
| 308 | +## item.pas: items | |
| 309 | + | |
| 235 | 310 | An operand is an *item* (`TItem`), as in Wirth's compilers. Its `mode` |
| 236 | 311 | says what it is now: |
| 237 | 312 | |
| 238 | 313 | - `mConst`: a value known to the compiler, in `val`; |
| 239 | -- `mVar`: a variable at an address the code can name: relative to | |
| 240 | - `rbp` (`bFrame`), to a chunk (`bGlobal`), to a register (`bReg`), to | |
| 241 | - an address pushed on the stack (`bStack`), or kept in a frame slot | |
| 242 | - (`bSlot`); plus `off`; | |
| 243 | -- `mReg`: a value in `rax`; `mStack`: a value pushed on the stack; | |
| 314 | +- `mVar`: a variable, at `base`, `chunk` and `off`, which `AdrOf` | |
| 315 | + makes a `TAdr` for gen; | |
| 316 | +- `mStack`: a value on gen's stack; | |
| 244 | 317 | - `mProc`, `mType`, `mStr`, and a few more the parser needs. |
| 245 | 318 | |
| 246 | 319 | An item also carries `pos`, the stretch of source it came from, which |
| @@ -250,99 +323,74 @@ | ||
| 250 | 323 | message: a constant without a type of its own as it is written |
| 251 | 324 | (`found 1.5`), anything else by its type. |
| 252 | 325 | |
| 253 | -Three routines move items towards the machine. `Value` brings a value | |
| 254 | -into `rax`, loading it with the one instruction its size needs. | |
| 255 | -`Address` brings a variable's address into a register. `Save` pushes | |
| 256 | -whatever an item has in a register, so that another expression can be | |
| 257 | -computed; the item remembers that it's on the stack. Read these three, | |
| 258 | -then `MemOf`, which turns an `mVar` into an x64 memory operand, popping | |
| 259 | -its address first if it was pushed. | |
| 326 | +Two routines move items towards the machine: `Value` pushes an item's | |
| 327 | +value, and `Address` a variable's address. Both leave the item saying | |
| 328 | +it is on the stack. An item nothing more is done with is given back to | |
| 329 | +`Discard`. | |
| 260 | 330 | |
| 261 | 331 | Expressions: `Op1` is called when the parser has seen an operator and |
| 262 | -its left operand, `Op2` when it has the right one. `Op1` saves the left | |
| 263 | -operand (or, for `and` and `or`, emits the test that skips the right | |
| 264 | -one). `Op2` checks the types, folds constants (`FoldInt`, `FoldReal`), | |
| 265 | -and otherwise puts the left operand in `rax`, the right in `rcx`, and | |
| 266 | -emits the operation. When the left was the last thing pushed and the | |
| 267 | -right needs no code (a constant, or a variable on the frame or in a | |
| 268 | -chunk), `Unpush` takes the push back, and the right goes straight to | |
| 269 | -`rcx`; not if a jump or a line starts after the push. A power of two | |
| 270 | -on the right of `*`, `div` or `mod` becomes shifts (`DivPow2`), since | |
| 271 | -`idiv` is slow. Narrow integers are brought back to their width | |
| 272 | -after each operation (`Norm`), which is how `U8` arithmetic wraps. | |
| 273 | -Since they are computed in 64 bits, a shift's count is cut to their | |
| 274 | -width first, and `DivTrap` makes the smallest value of one divided by | |
| 275 | --1 fault as an `I64`'s does. | |
| 276 | -`=` and `#` on arrays, slices and strings make a slice of each side | |
| 277 | -(`SeqSlice`), compare the lengths, and then the bytes with `repe | |
| 278 | -cmpsb`, right where the comparison stands: no call. `+` on two string | |
| 279 | -constants is `JoinStr`, which makes one string of them, in a chunk of | |
| 280 | -its own. | |
| 332 | +its left operand, `Op2` when it has the right one. `Op1` pushes the | |
| 333 | +left operand, unless it is a constant (or, for `and` and `or`, has gen | |
| 334 | +jump past the right one). `Op2` checks the types, folds constants | |
| 335 | +(`FoldInt`, `FoldReal`), and otherwise pushes the right operand and has | |
| 336 | +gen do the operation; a constant on the left goes on after the right, | |
| 337 | +and the two `Swap`. A set's operations are operations on its bits | |
| 338 | +(`SetOp`). `=` and `#` on arrays, slices and strings make a slice of | |
| 339 | +each side (`SeqSlice`) and have gen compare them right where the | |
| 340 | +comparison stands: no call. `+` on two string constants is `JoinStr`, | |
| 341 | +which makes one string of them, in a chunk of its own. | |
| 281 | 342 | |
| 282 | 343 | Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a |
| 283 | 344 | variable into an item for part of it, mostly by adding to `off`. An |
| 284 | 345 | item for another module's exported variable, or part of it, is `ro`: |
| 285 | 346 | it isn't a variable (`isVar`), and `NotReadOnly` says why where one is |
| 286 | -wanted; what it points at is. An array constant is a variable too, | |
| 287 | -in read-only data: `ArrayConst` (`parse.pas`) reads its literal into the | |
| 347 | +wanted; what it points at is. An array constant is a variable too, in | |
| 348 | +read-only data: `ArrayConst` (`parse.pas`) reads its literal into the | |
| 288 | 349 | bytes of a chunk, as a string constant's are, and `MakeItem` makes it |
| 289 | 350 | an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and |
| 290 | -passed, never changed. | |
| 291 | -`Index` and `Slice` emit the bounds checks, unless the module says | |
| 292 | -`check off;` (`checks`, which `CompileFile` sets for each module), and | |
| 293 | -`Convert` the check of an enumeration's position; `Check` jumps over a call to `Rt.fail` when | |
| 294 | -the check holds. The call passes what failed, the values involved and | |
| 295 | -its own address, by which `Rt` finds the place; that code has the | |
| 296 | -place of the offending expression (`LineCol` in `scan.pas`), and the | |
| 297 | -code after it the statement's again. | |
| 351 | +passed, never changed. `Index` and `Slice` have gen make the bounds | |
| 352 | +checks, unless the module says `check off;` (`checks`, which | |
| 353 | +`CompileFile` sets for each module), and `Convert` the check of an | |
| 354 | +enumeration's position. | |
| 298 | 355 | |
| 299 | 356 | Assignment and conversion: `Coerce` makes a value fit the type it's |
| 300 | 357 | going to, or reports that it can't; it's where a string literal becomes |
| 301 | 358 | a slice, an array a slice, an untyped constant a typed one, and an |
| 302 | 359 | `INT` an `I64` (`Widens`). `CoerceExact` is for where the types must |
| 303 | 360 | be the same, as in a `for` loop, `INC` and several results. `Assign` |
| 304 | -stores, copying records and arrays with `rep movsb`. `Convert` is the | |
| 305 | -written conversion, `T(x)`. | |
| 361 | +stores, or copies records and arrays. `Convert` is the written | |
| 362 | +conversion, `T(x)`. | |
| 306 | 363 | |
| 307 | -Calls: `BeginCall`, then `PassArg` for each argument, then `EndCall`. | |
| 308 | -Each argument is computed into a frame slot of its own, left to right; | |
| 309 | -only in `EndCall` are they loaded into the registers System V wants, | |
| 310 | -and the stack aligned for the call. A record passed by value goes as | |
| 311 | -the address of a copy; one returned comes back through a hidden pointer | |
| 312 | -in `rdi`, as C's structs do, and so do several results. `return | |
| 313 | -f(x);` passing on f's several results copies them, with `ReturnAll`, | |
| 314 | -from the call's temporary to the place its own hidden pointer names. | |
| 315 | - | |
| 316 | -Frames: `BeginProc` gives the parameters their slots; `EnterBody` | |
| 317 | -writes the prologue, which zeroes the locals (with a few stores, or a | |
| 318 | -loop for more; there are none to zero in `scale`) but not the | |
| 319 | -temporaries, since each is written before it is read; | |
| 320 | -`EndProc` writes the epilogue and patches the frame's size in. Within | |
| 321 | -a statement, scratch space comes from `AllocTemp` and is given back by | |
| 322 | -`ReleaseTemps`; `AllocPerm` reserves space for the whole procedure. | |
| 364 | +Calls: `BeginCall`, then `PassArg` for each argument, then `EndCall`, | |
| 365 | +which has gen's `Call` make the call. Each argument is computed into a | |
| 366 | +frame slot of its own. `return f(x);` passing on f's several results | |
| 367 | +copies them, with `ReturnAll`, from the call's temporary to the place | |
| 368 | +its own hidden pointer names. | |
| 323 | 369 | |
| 324 | 370 | ## parse.pas: the parser |
| 325 | 371 | |
| 326 | 372 | A recursive descent parser, one routine for each rule of the grammar in |
| 327 | -the report, calling `gen.pas` as it goes. | |
| 373 | +the report, calling `item.pas`, and for jumps `gen.pas`, as it goes. | |
| 328 | 374 | |
| 329 | 375 | Start with expressions, bottom up: `Factor`, `Term`, `SimpleExpr`, |
| 330 | 376 | `Relation`, `Conjunct` (the `and`s), `Expression` (the `or`s). Then `Designator`, which reads a name and what follows |
| 331 | 377 | it: `.field`, `^`, `[i]`, `[lo..hi]`, a call, a conversion. Each step |
| 332 | -calls the matching routine in `gen.pas` on the item it has. The | |
| 378 | +calls the matching routine in `item.pas` on the item it has. The | |
| 333 | 379 | `want` parameter carries the type an expression must have, where it is |
| 334 | 380 | known, so that `[1, 2, 3]`, `{x: 1}` and `{red}` know what to build. |
| 335 | 381 | |
| 336 | 382 | Then statements: `Statement` makes each one a recovery point (below) |
| 337 | 383 | and `OneStatement` dispatches; `IfStatement`, |
| 338 | 384 | `WhileStatement`, `ForStatement` and `MatchStatement` emit their jumps |
| 339 | -with `x64.pas`'s labels. A `match` on an integer reads each arm's | |
| 385 | +with gen's labels. A `match` on an integer reads each arm's | |
| 340 | 386 | labels in `IntArm`, which refuses a value in two of them, and tests them |
| 341 | 387 | one after another with `MatchRange`: compares and branches, no jump |
| 342 | 388 | table. `Defer` is worth reading: a deferred statement |
| 343 | 389 | is compiled where it stands, as a little subroutine of the procedure |
| 344 | -that the code jumps over, and `RunDefers` emits a call to it at every | |
| 345 | -way out of its statement list: the end, `return` and `break`. | |
| 390 | +that the code jumps over (gen's `DeferBegin` and `DeferEnd`), and | |
| 391 | +`RunDefers` emits a call to it at every way out of its statement list: | |
| 392 | +the end, `return` and `break`. A `return`'s result waits in the frame | |
| 393 | +meanwhile, so that gen's stack is empty when they run. | |
| 346 | 394 | |
| 347 | 395 | Then declarations: `Declarations` reads `const`, `type`, `var`, `proc` |
| 348 | 396 | and `extern`, each one through `Declaration`, a recovery point, which |
| @@ -451,11 +499,12 @@ | ||
| 451 | 499 | `raz.pas` parses the arguments, calls `CompileMain`, and works out what |
| 452 | 500 | the program uses: `Use` marks the modules imported from the main module |
| 453 | 501 | and `Rt`; `UsesC` decides whether the program is dynamic, refusing one |
| 454 | -that both uses C and has a module saying `link static`. Then it writes | |
| 455 | -the entry point, and has `MarkLive` mark what it reaches. On a target | |
| 502 | +that both uses C and has a module saying `link static`. Then it has | |
| 503 | +gen's `EntryPoint` write the entry point, and `MarkLive` mark what it | |
| 504 | +reaches. On a target | |
| 456 | 505 | without reals (`HasReals`, beside `Target` in `parse.pas`; x86-64 has |
| 457 | 506 | them), a live chunk that computes with one is an error at the first |
| 458 | -place it does: `UsesReals` in `gen.pas` notes it, from `Value` and a | |
| 507 | +place it does: `UsesReals` in `item.pas` notes it, from `Value` and a | |
| 459 | 508 | real literal, on the chunk of the procedure being compiled. A real |
| 460 | 509 | moved whole, as a record's bytes are, isn't noted. A static program starts at `_start` itself. A dynamic |
| 461 | 510 | one starts at a `_start` that calls `__libc_start_main` the way C's own |
| @@ -470,9 +519,12 @@ | ||
| 470 | 519 | ## Where to start changing it |
| 471 | 520 | |
| 472 | 521 | - **A built-in procedure:** an entry in `sym.pas`'s universe, a case in |
| 473 | - `Builtin` in `parse.pas`, and the code, in `gen.pas` or right there. | |
| 522 | + `Builtin` in `parse.pas`, and the code, in `gen.pas` if it is an | |
| 523 | + instruction, or of calls to gen right there. | |
| 474 | 524 | - **An instruction:** a routine in `x64.pas` built on `OpM` or `OpR`, |
| 475 | 525 | and a line in `test/enctest.pas` to check its encoding. |
| 526 | +- **A machine:** a new `gen.pas`, answering the same calls, and an | |
| 527 | + encoder for it in place of `x64.pas`. | |
| 476 | 528 | - **A statement:** a reserved word in `scan.pas` (in `TSym` and `Words`, |
| 477 | 529 | in the same order), a case in `Statement`, and a routine beside the |
| 478 | 530 | others. |
| @@ -0,0 +1,244 @@ | ||
| 1 | +{ chunk.pas: the compiled program, before it is linked. | |
| 2 | + | |
| 3 | + The code of a program is a set of chunks: one per procedure, module | |
| 4 | + variable and string. A chunk's bytes go in one of three sections: | |
| 5 | + text (code), rodata (strings and other constants), bss (variables, | |
| 6 | + which are all zero). A reference from code to another chunk (a call, | |
| 7 | + or an address) is a fixup, patched once the linker has placed every | |
| 8 | + chunk. Line notes say which place in the source the code from each | |
| 9 | + point on is for. | |
| 10 | + | |
| 11 | + Nothing here depends on the machine: text is bytes, written by the | |
| 12 | + encoder for the target (x64.pas). | |
| 13 | + | |
| 14 | + Depends on: scan, for errors. | |
| 15 | + Leaves out: the instructions; that is x64.pas. } | |
| 16 | + | |
| 17 | +unit chunk; | |
| 18 | + | |
| 19 | +{$mode objfpc}{$H-} | |
| 20 | + | |
| 21 | +interface | |
| 22 | + | |
| 23 | +uses scan, diag; | |
| 24 | + | |
| 25 | +type | |
| 26 | + TSection = (secText, secRodata, secBss); | |
| 27 | + | |
| 28 | + TChunk = record | |
| 29 | + sec: TSection; | |
| 30 | + start, size, align: longint; { in its section's buffer } | |
| 31 | + firstFix, numFix: longint; { its fixups, text chunks only } | |
| 32 | + module, name: longint; { for the symbol table; -1 if none } | |
| 33 | + adr: int64; { its address, set by the linker } | |
| 34 | + live: boolean; { reached from the entry point } | |
| 35 | + ext: boolean; { bss: the address of a C symbol, its name } | |
| 36 | + reals: boolean; { code: computes with reals, first at realsAt } | |
| 37 | + realsAt: TPos; | |
| 38 | + end; | |
| 39 | + | |
| 40 | + { A place in the source whose code starts at a place in text: for | |
| 41 | + gdb, and for Rt to say where a failed check or a signal was } | |
| 42 | + TLine = record | |
| 43 | + at, chunk: longint; { the place in text, and its chunk } | |
| 44 | + path, line, col: longint; { the file's path, interned, the line and column } | |
| 45 | + end; | |
| 46 | + | |
| 47 | + TFixup = record | |
| 48 | + at: longint; { the 32-bit field, as an offset in text } | |
| 49 | + chunk: longint; { the chunk it refers to } | |
| 50 | + addend: longint; { added to the chunk's address } | |
| 51 | + end; | |
| 52 | + | |
| 53 | + PChunks = ^TChunkArray; | |
| 54 | + TChunkArray = array[0..MaxInt div SizeOf(TChunk) - 1] of TChunk; | |
| 55 | + PLines = ^TLineArray; | |
| 56 | + TLineArray = array[0..MaxInt div SizeOf(TLine) - 1] of TLine; | |
| 57 | + PFixups = ^TFixupArray; | |
| 58 | + TFixupArray = array[0..MaxInt div SizeOf(TFixup) - 1] of TFixup; | |
| 59 | + | |
| 60 | +var | |
| 61 | + code: PBytes; { text } | |
| 62 | + pc: longint; { its length } | |
| 63 | + rodata: PBytes; | |
| 64 | + rolen: longint; | |
| 65 | + chunks: PChunks; | |
| 66 | + numChunks: longint; | |
| 67 | + fixups: PFixups; | |
| 68 | + numFixups: longint; | |
| 69 | + lines: PLines; | |
| 70 | + numLines: longint; | |
| 71 | + curFile: longint; { the file being compiled, for NoteLine } | |
| 72 | + curChunk: longint; { the chunk of code being written } | |
| 73 | + | |
| 74 | +function NewChunk(sec: TSection; module, name: longint): longint; | |
| 75 | +procedure BeginChunk(c: longint); | |
| 76 | +procedure EndChunk(c: longint); | |
| 77 | +procedure SetData(c: longint; p: PBytes; n, align: longint); | |
| 78 | +procedure SetBss(c: longint; size, align: longint); | |
| 79 | +function ExternChunk(name: longint): longint; | |
| 80 | +procedure NoteLine(line, col: longint); | |
| 81 | +procedure AddFixup(at, chunk, addend: longint); | |
| 82 | + | |
| 83 | +procedure Put1(b: longint); | |
| 84 | +procedure Put4(v: longint); | |
| 85 | +procedure Put8(v: int64); | |
| 86 | +procedure Put4At(at, v: longint); | |
| 87 | +function Get4At(at: longint): longint; | |
| 88 | + | |
| 89 | +implementation | |
| 90 | + | |
| 91 | +var | |
| 92 | + codeCap, roCap, chunkCap, fixCap, lineCap: longint; | |
| 93 | + | |
| 94 | +procedure Room(n: longint); | |
| 95 | +begin | |
| 96 | + if pc + n > codeCap then begin | |
| 97 | + codeCap := codeCap * 2 + n; | |
| 98 | + code := ReAllocMem(code, codeCap); | |
| 99 | + end; | |
| 100 | +end; | |
| 101 | + | |
| 102 | +procedure Put1(b: longint); | |
| 103 | +begin | |
| 104 | + if pc + 16 > codeCap then Room(16); | |
| 105 | + code^[pc] := byte(b); | |
| 106 | + inc(pc); | |
| 107 | +end; | |
| 108 | + | |
| 109 | +procedure Put4(v: longint); | |
| 110 | +begin | |
| 111 | + if pc + 16 > codeCap then Room(16); | |
| 112 | + PLongint(@code^[pc])^ := v; | |
| 113 | + inc(pc, 4); | |
| 114 | +end; | |
| 115 | + | |
| 116 | +procedure Put4At(at, v: longint); | |
| 117 | +begin | |
| 118 | + PLongint(@code^[at])^ := v; | |
| 119 | +end; | |
| 120 | + | |
| 121 | +function Get4At(at: longint): longint; | |
| 122 | +begin | |
| 123 | + Get4At := PLongint(@code^[at])^; | |
| 124 | +end; | |
| 125 | + | |
| 126 | +procedure Put8(v: int64); | |
| 127 | +begin | |
| 128 | + Put4(longint(v and $FFFFFFFF)); | |
| 129 | + Put4(longint(v shr 32)); | |
| 130 | +end; | |
| 131 | + | |
| 132 | +function NewChunk(sec: TSection; module, name: longint): longint; | |
| 133 | +begin | |
| 134 | + if numChunks = chunkCap then begin | |
| 135 | + chunkCap := chunkCap * 2; | |
| 136 | + chunks := ReAllocMem(chunks, chunkCap * SizeOf(TChunk)); | |
| 137 | + end; | |
| 138 | + FillChar(chunks^[numChunks], SizeOf(TChunk), 0); | |
| 139 | + chunks^[numChunks].sec := sec; | |
| 140 | + chunks^[numChunks].module := module; | |
| 141 | + chunks^[numChunks].name := name; | |
| 142 | + chunks^[numChunks].align := 1; | |
| 143 | + chunks^[numChunks].size := -1; { not yet defined } | |
| 144 | + NewChunk := numChunks; | |
| 145 | + inc(numChunks); | |
| 146 | +end; | |
| 147 | + | |
| 148 | +procedure BeginChunk(c: longint); | |
| 149 | +begin | |
| 150 | + { code is aligned to 16 bytes, padded with CC, x86's int3: a trap if | |
| 151 | + it were ever run } | |
| 152 | + while pc and 15 <> 0 do Put1($CC); | |
| 153 | + curChunk := c; | |
| 154 | + chunks^[c].start := pc; | |
| 155 | + chunks^[c].align := 16; | |
| 156 | + chunks^[c].firstFix := numFixups; | |
| 157 | +end; | |
| 158 | + | |
| 159 | +procedure EndChunk(c: longint); | |
| 160 | +begin | |
| 161 | + chunks^[c].size := pc - chunks^[c].start; | |
| 162 | + chunks^[c].numFix := numFixups - chunks^[c].firstFix; | |
| 163 | +end; | |
| 164 | + | |
| 165 | +procedure SetData(c: longint; p: PBytes; n, align: longint); | |
| 166 | +begin | |
| 167 | + if rolen + n > roCap then begin | |
| 168 | + roCap := roCap * 2 + n; | |
| 169 | + rodata := ReAllocMem(rodata, roCap); | |
| 170 | + end; | |
| 171 | + Move(p^, rodata^[rolen], n); | |
| 172 | + chunks^[c].start := rolen; | |
| 173 | + chunks^[c].size := n; | |
| 174 | + chunks^[c].align := align; | |
| 175 | + inc(rolen, n); | |
| 176 | +end; | |
| 177 | + | |
| 178 | +procedure SetBss(c: longint; size, align: longint); | |
| 179 | +begin | |
| 180 | + chunks^[c].size := size; | |
| 181 | + chunks^[c].align := align; | |
| 182 | +end; | |
| 183 | + | |
| 184 | +{ The code from here on is for line and column of the current file. A | |
| 185 | + place with no code of its own gives way to the next. } | |
| 186 | +procedure NoteLine(line, col: longint); | |
| 187 | +begin | |
| 188 | + if (numLines > 0) and (lines^[numLines - 1].at = pc) then dec(numLines); | |
| 189 | + if (numLines > 0) and (lines^[numLines - 1].chunk = curChunk) and (lines^[numLines - 1].line = line) | |
| 190 | + and (lines^[numLines - 1].col = col) and (lines^[numLines - 1].path = curFile) then exit; | |
| 191 | + if numLines = lineCap then begin | |
| 192 | + lineCap := lineCap * 2; | |
| 193 | + lines := ReAllocMem(lines, lineCap * SizeOf(TLine)); | |
| 194 | + end; | |
| 195 | + lines^[numLines].at := pc; | |
| 196 | + lines^[numLines].chunk := curChunk; | |
| 197 | + lines^[numLines].path := curFile; | |
| 198 | + lines^[numLines].line := line; | |
| 199 | + lines^[numLines].col := col; | |
| 200 | + inc(numLines); | |
| 201 | +end; | |
| 202 | + | |
| 203 | +{ The quadword that holds the address of C's symbol name, filled in | |
| 204 | + when the program starts; one for each name. } | |
| 205 | +function ExternChunk(name: longint): longint; | |
| 206 | +var c: longint; | |
| 207 | +begin | |
| 208 | + for c := 0 to numChunks - 1 do | |
| 209 | + if chunks^[c].ext and (chunks^[c].name = name) then exit(c); | |
| 210 | + c := NewChunk(secBss, -1, name); | |
| 211 | + SetBss(c, 8, 8); | |
| 212 | + chunks^[c].ext := true; | |
| 213 | + ExternChunk := c; | |
| 214 | +end; | |
| 215 | + | |
| 216 | +procedure AddFixup(at, chunk, addend: longint); | |
| 217 | +begin | |
| 218 | + if numFixups = fixCap then begin | |
| 219 | + fixCap := fixCap * 2; | |
| 220 | + fixups := ReAllocMem(fixups, fixCap * SizeOf(TFixup)); | |
| 221 | + end; | |
| 222 | + fixups^[numFixups].at := at; | |
| 223 | + fixups^[numFixups].chunk := chunk; | |
| 224 | + fixups^[numFixups].addend := addend; | |
| 225 | + inc(numFixups); | |
| 226 | +end; | |
| 227 | + | |
| 228 | +initialization | |
| 229 | + codeCap := 1 shl 20; | |
| 230 | + code := GetMem(codeCap); | |
| 231 | + pc := 0; | |
| 232 | + roCap := 1 shl 16; | |
| 233 | + rodata := GetMem(roCap); | |
| 234 | + rolen := 0; | |
| 235 | + chunkCap := 1 shl 10; | |
| 236 | + chunks := GetMem(chunkCap * SizeOf(TChunk)); | |
| 237 | + numChunks := 0; | |
| 238 | + fixCap := 1 shl 12; | |
| 239 | + fixups := GetMem(fixCap * SizeOf(TFixup)); | |
| 240 | + numFixups := 0; | |
| 241 | + lineCap := 1 shl 12; | |
| 242 | + lines := GetMem(lineCap * SizeOf(TLine)); | |
| 243 | + numLines := 0; | |
| 244 | +end. |
| @@ -13,7 +13,7 @@ | ||
| 13 | 13 | (a GLOB_DAT relocation), which the code calls or reads through. |
| 14 | 14 | There is no PLT and no lazy binding, and the program exports nothing. |
| 15 | 15 | |
| 16 | - Depends on: link for the images and addresses, x64 for the chunks, | |
| 16 | + Depends on: link for the images and addresses, chunk for the chunks, | |
| 17 | 17 | scan for names and errors, BaseUnix to write the file. |
| 18 | 18 | Leaves out: position independent executables, symbol versions, the |
| 19 | 19 | rest of DWARF. } |
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | |
| 25 | 25 | interface |
| 26 | 26 | |
| 27 | -uses scan, diag, x64, link; | |
| 27 | +uses scan, diag, chunk, link; | |
| 28 | 28 | |
| 29 | 29 | var |
| 30 | 30 | needed: array[0..31] of longint; { the libraries to load, names interned } |
| @@ -1,25 +1,34 @@ | ||
| 1 | -{ gen.pas: the code generator. | |
| 1 | +{ gen.pas: the code generator, for x86-64. | |
| 2 | 2 | |
| 3 | - The parser describes each operand as an item (TItem), as in Wirth's | |
| 4 | - compilers: a constant, a variable at an address the code can name, a | |
| 5 | - value in rax, a value pushed on the stack, a procedure. The routines | |
| 6 | - here turn items into code. They are a stack machine through rax: a | |
| 7 | - value is computed into rax, and pushed while another is computed, so | |
| 8 | - no register is ever allocated. Every access the program writes is one | |
| 9 | - instruction, in the order written. | |
| 3 | + item.pas and parse.pas drive a stack machine: they push constants, | |
| 4 | + addresses and the values of variables, operate on the values on top, | |
| 5 | + store them and jump on them. Every value has a kind: a word, which | |
| 6 | + is an integer as big as an address, a pointer, a BOOL, a set or a | |
| 7 | + narrower integer; a long, a 64-bit integer on a machine whose word is | |
| 8 | + smaller (never here); an F32; an F64. This unit makes the machine's | |
| 9 | + code for those calls, and nothing above it knows the machine: a port | |
| 10 | + is a new gen.pas, with an encoder of its own. | |
| 10 | 11 | |
| 12 | + Here the stack is the machine's: the value on top, once computed, is | |
| 13 | + in rax, and the values under it are pushed when another is computed. | |
| 14 | + A constant is only noted until something needs it in a register, so | |
| 15 | + that it can be an instruction's operand. | |
| 16 | + | |
| 11 | 17 | Calls use the System V convention. Arguments are computed left to |
| 12 | 18 | right into frame slots, then loaded into registers just before the |
| 13 | 19 | call, so that computing one argument can't disturb another. A record, |
| 14 | 20 | array, slice or variant passed by value goes as the address of a copy |
| 15 | 21 | the caller makes; one returned goes through a hidden pointer, as C's |
| 16 | - structs do. | |
| 22 | + structs do, and so do several results. | |
| 17 | 23 | |
| 18 | 24 | A procedure's frame holds its parameters, its variables, and the |
| 19 | - temporaries of the statement running. All of it is zeroed on entry. | |
| 25 | + temporaries of the statement running. The variables are zeroed on | |
| 26 | + entry. | |
| 20 | 27 | |
| 21 | - Depends on: scan for errors, sym for types, x64 for instructions. | |
| 22 | - Leaves out: the language's syntax; that is parse.pas. } | |
| 28 | + Depends on: scan and diag for errors, sym for types, chunk for code | |
| 29 | + and data, x64 for instructions. | |
| 30 | + Leaves out: what the program means, its types and their checks; that | |
| 31 | + is item.pas. } | |
| 23 | 32 | |
| 24 | 33 | unit gen; |
| 25 | 34 | |
| @@ -27,98 +36,93 @@ | ||
| 27 | 36 | |
| 28 | 37 | interface |
| 29 | 38 | |
| 30 | -uses scan, diag, sym, x64; | |
| 39 | +uses scan, diag, sym, chunk, x64; | |
| 40 | + | |
| 41 | +const | |
| 42 | + NoLabel = -1; | |
| 43 | + WordSize = 8; { the size of an address, and of INT } | |
| 31 | 44 | |
| 32 | 45 | type |
| 33 | - TMode = (mConst, mVar, mReg, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone); | |
| 34 | - TBase = (bFrame, bGlobal, bReg, bStack, bSlot); { bSlot: address in a frame slot, chunk } | |
| 46 | + TKind = (kWord, kLong, kF32, kF64); | |
| 35 | 47 | |
| 36 | - TItem = record | |
| 37 | - mode: TMode; | |
| 38 | - typ: PType; | |
| 39 | - val: int64; { mConst: the value, an F64's bits; mStr: its length } | |
| 40 | - base: TBase; { mVar: what the address is relative to } | |
| 41 | - reg: longint; { mVar with bReg: the register } | |
| 42 | - off: int64; { mVar: added to the base } | |
| 43 | - chunk: longint; { mVar with bGlobal, mStr: the chunk } | |
| 44 | - vol: boolean; { mVar: reached through ^VOLATILE } | |
| 45 | - isVar: boolean; { mVar: a variable of the program, not a temporary } | |
| 46 | - odd: boolean; { mVar: in a packed record, so maybe misaligned } | |
| 47 | - ro: boolean; { mVar: another module's exported variable, or part | |
| 48 | - of one, which only that module changes } | |
| 49 | - obj: PObj; { mProc, mBuiltin, mCase } | |
| 50 | - pos: TPos; { where it is in the source, for messages } | |
| 48 | + { Where a variable is: its address is relative to the frame, to a | |
| 49 | + chunk, to an address on the stack, or to one held in a frame slot } | |
| 50 | + TBase = (bFrame, bGlobal, bStack, bSlot); | |
| 51 | + TAdr = record | |
| 52 | + base: TBase; | |
| 53 | + chunk: longint; { bGlobal: the chunk; bSlot: the frame slot } | |
| 54 | + off: int64; { added to the address } | |
| 51 | 55 | end; |
| 52 | 56 | |
| 53 | 57 | TCall = record |
| 54 | 58 | sig: PType; |
| 55 | 59 | param: PObj; { the parameter the next argument is for } |
| 56 | - slots: array[0..63] of int64; | |
| 57 | - n: longint; | |
| 58 | - target: int64; { the frame slot of a procedure value, if any } | |
| 60 | + obj: PObj; { the procedure called, if it has a name } | |
| 61 | + n: longint; { the arguments } | |
| 62 | + slots: array[0..63] of int64; { each one's frame slot } | |
| 63 | + isF: array[0..63] of boolean; { ... goes in an xmm register } | |
| 64 | + target: int64; { the frame slot of a procedure value, if chunk < 0 } | |
| 59 | 65 | chunk: longint; { else the procedure's chunk } |
| 60 | - obj: PObj; { the procedure called, if it has a name } | |
| 61 | 66 | code: boolean; { a code procedure: two results come in rax and rdx } |
| 62 | 67 | ext: boolean; { C's: called through the chunk holding its address } |
| 63 | - isF: array[0..63] of boolean; { each argument: goes in an xmm register } | |
| 68 | + ret: int64; { set by Call: the temporary an aggregate result is in } | |
| 64 | 69 | end; |
| 65 | 70 | |
| 66 | 71 | var |
| 67 | 72 | rtFail, argcChunk, argvChunk: longint; |
| 68 | - depth: longint; { values pushed and not yet popped } | |
| 69 | - checks: boolean; { the module's index and slice checks are made: it | |
| 70 | - doesn't say check off } | |
| 73 | + depth: longint; { the values on the stack } | |
| 74 | + pushed: longint; { quadwords pushed on the machine's stack } | |
| 75 | + codeChunk: longint = -1; { the procedure or body being compiled, or -1 } | |
| 71 | 76 | |
| 72 | -procedure MakeConst(var x: TItem; t: PType; v: int64); | |
| 73 | -function What(const x: TItem): shortstring; | |
| 74 | -procedure MakeReal(var x: TItem; r: double); | |
| 75 | -procedure MakeStr(var x: TItem); | |
| 76 | -procedure MakeItem(var x: TItem; o: PObj); | |
| 77 | -function RealOf(const x: TItem): double; | |
| 77 | +function KindOf(t: PType): TKind; | |
| 78 | 78 | |
| 79 | -procedure UsesReals(const p: TPos); | |
| 80 | -procedure Value(var x: TItem); | |
| 81 | -procedure Address(var x: TItem; r: longint); | |
| 82 | -procedure Save(var x: TItem); | |
| 83 | -procedure Coerce(var x: TItem; t: PType); | |
| 84 | -procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); | |
| 85 | -procedure NotReadOnly(const x: TItem); | |
| 86 | -procedure Assign(var dst, src: TItem); | |
| 87 | -procedure CopyToTemp(var x: TItem); | |
| 88 | -function ToSlot(var x: TItem): int64; | |
| 79 | +procedure PushConst(k: TKind; v: int64); | |
| 80 | +procedure PushAdr(const a: TAdr); | |
| 81 | +procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind); | |
| 82 | +procedure PushProc(c: longint; ext: boolean); | |
| 83 | +procedure PopVar(const a: TAdr; size: longint); | |
| 84 | +procedure Drop; | |
| 85 | +procedure Swap; | |
| 86 | +procedure ResetStack(d, p: longint); | |
| 89 | 87 | |
| 90 | -procedure Deref(var x: TItem); | |
| 91 | -procedure Field(var x: TItem; f: PObj); | |
| 92 | -procedure IndexPrep(var x: TItem); | |
| 93 | -procedure Index(var x, y: TItem); | |
| 94 | -procedure SliceLo(var lo: TItem); | |
| 95 | -procedure Slice(var x, lo, hi: TItem); | |
| 88 | +procedure CopyBlock(size, off: int64); | |
| 89 | +procedure CopyTo(const a: TAdr; size: int64); | |
| 90 | +procedure ZeroVar(const a: TAdr; size: int64); | |
| 96 | 91 | |
| 97 | -procedure Neg(var x: TItem); | |
| 98 | -procedure NotOp(var x: TItem); | |
| 99 | -procedure Complement(var x: TItem); | |
| 100 | -procedure Op1(op: TSym; var x: TItem; var L: longint); | |
| 101 | -procedure Op2(op: TSym; var x, y: TItem; L: longint); | |
| 102 | -function IsWord(t: PType): boolean; | |
| 103 | -procedure Convert(var x: TItem; t: PType); | |
| 92 | +procedure Op(op: TSym; k: TKind; size: longint; signed: boolean); | |
| 93 | +procedure Compare(op: TSym; k: TKind; signed: boolean); | |
| 94 | +procedure Neg(k: TKind; size: longint; signed: boolean); | |
| 95 | +procedure Complement(size: longint; signed: boolean); | |
| 96 | +procedure NotBool; | |
| 97 | +procedure Narrow(size: longint; signed: boolean); | |
| 98 | +procedure IntToReal(k: TKind; big: boolean); | |
| 99 | +procedure RealToInt(k: TKind); | |
| 100 | +procedure RealToReal(k: TKind); | |
| 101 | +procedure CompareSeq(a, b: int64; shift: longint; eq: boolean); | |
| 102 | +procedure ErrorText(tmp: int64); | |
| 103 | +procedure IncDec(size: longint; signed, dec: boolean); | |
| 104 | +procedure ZeroNew(size: int64); | |
| 105 | +procedure Atomic(which: longint); | |
| 106 | +procedure SysCall(n: longint); | |
| 107 | +procedure Fence; | |
| 104 | 108 | |
| 105 | -procedure BeginCall(var c: TCall; var x: TItem); | |
| 106 | -procedure PassArg(var c: TCall; var a: TItem); | |
| 107 | -procedure EndCall(var c: TCall; var x: TItem); | |
| 109 | +procedure Jump(var L: longint); | |
| 110 | +procedure JumpIf(var L: longint; when: boolean); | |
| 111 | +procedure JumpKeep(var L: longint; when: boolean); | |
| 112 | +procedure FixKeep(L: longint); | |
| 113 | +procedure JumpCmp(op: TSym; signed: boolean; var L: longint); | |
| 114 | +procedure JumpBack(top: longint); | |
| 115 | +procedure Fix(L: longint); | |
| 116 | +procedure FixTo(L, top: longint); | |
| 108 | 117 | |
| 109 | -procedure ZeroTemp(var x: TItem; t: PType); | |
| 110 | -procedure Construct(var x: TItem; c: PObj); | |
| 111 | -procedure SetField(const x: TItem; f: PObj; var a: TItem); | |
| 112 | -function MatchBegin(var x: TItem): int64; | |
| 113 | -procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean); | |
| 114 | -procedure MatchRange(p, lo, hi: int64; var body: longint); | |
| 115 | -procedure CondJump(var x: TItem; var L: longint); | |
| 118 | +procedure CheckIndex(n: int64; const p: TPos); | |
| 119 | +procedure CheckEnum(n: int64; name: longint; const p: TPos); | |
| 120 | +procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); | |
| 121 | +procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); | |
| 122 | +procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); | |
| 123 | + | |
| 124 | +procedure Call(var c: TCall); | |
| 116 | 125 | |
| 117 | -procedure SliceOf(var x: TItem; t: PType; len: int64); | |
| 118 | -procedure TempItem(var x: TItem; t: PType; off: int64); | |
| 119 | -function ScalarSize(t: PType): longint; | |
| 120 | -procedure Norm(t: PType); | |
| 121 | -procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); | |
| 122 | 126 | function AllocTemp(size, align: int64): int64; |
| 123 | 127 | function AllocPerm(size, align: int64): int64; |
| 124 | 128 | function TempMark: int64; |
| @@ -127,31 +131,34 @@ | ||
| 127 | 131 | |
| 128 | 132 | procedure BeginProc(sig: PType; params: PObj); |
| 129 | 133 | procedure EnterBody(c, line: longint); |
| 130 | -procedure ReturnValue(var x: TItem); | |
| 131 | -procedure ReturnField(f: PObj; var x: TItem); | |
| 132 | -procedure ReturnAll(var x: TItem); | |
| 134 | +procedure ResultTo(off: int64; size: longint); | |
| 135 | +procedure CopyResult(off, size: int64); | |
| 133 | 136 | procedure ReturnJump; |
| 134 | -procedure ReturnNone; | |
| 135 | -procedure Pin(var x: TItem); | |
| 136 | -procedure IncDec(var v, k: TItem; dec: boolean); | |
| 137 | -procedure Msg(var x: TItem); | |
| 138 | -function ErrorChunk(var x: TItem; module, name: longint): longint; | |
| 139 | 137 | procedure EndProc(c: longint); |
| 140 | -procedure CmpI(r: longint; v: int64); | |
| 141 | -procedure PushRax; | |
| 142 | -procedure PopR(r: longint); | |
| 138 | +function DeferBegin(var L, saved: longint): longint; | |
| 139 | +procedure DeferEnd(L, saved: longint); | |
| 140 | +procedure RunDefer(at: longint); | |
| 141 | + | |
| 142 | +function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint; | |
| 143 | 143 | |
| 144 | 144 | implementation |
| 145 | 145 | |
| 146 | +type | |
| 147 | + TEntry = record | |
| 148 | + isConst: boolean; { not loaded: val is the value } | |
| 149 | + val: int64; | |
| 150 | + k: TKind; | |
| 151 | + end; | |
| 152 | + | |
| 146 | 153 | var |
| 154 | + stack: array[0..255] of TEntry; | |
| 155 | + inRax: longint; { the entry whose value is in rax, or -1 } | |
| 147 | 156 | frameTop, frameMax, permTop: int64; |
| 148 | 157 | frameFix: longint; { where the frame size is patched in } |
| 149 | 158 | paramTop: int64; { the parameters' slots end here } |
| 150 | 159 | retSlot: int64; { holds the hidden result pointer } |
| 151 | - pushEnd: longint; { the code just after the last push of rax } | |
| 152 | - result: PType; { the procedure's result, or nil } | |
| 160 | + resType: PType; { the procedure's result, or nil } | |
| 153 | 161 | exitL: longint; |
| 154 | - codeChunk: longint = -1; { the procedure or body being compiled, or -1 } | |
| 155 | 162 | { parameters arriving in registers: where each is stored on entry } |
| 156 | 163 | inReg: array[0..13] of longint; |
| 157 | 164 | inSlot: array[0..13] of int64; |
| @@ -161,322 +168,166 @@ | ||
| 161 | 168 | const |
| 162 | 169 | IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9); |
| 163 | 170 | |
| 164 | -{ Items } | |
| 165 | - | |
| 166 | -procedure MakeConst(var x: TItem; t: PType; v: int64); | |
| 167 | -var p: TPos; | |
| 171 | +function KindOf(t: PType): TKind; | |
| 168 | 172 | begin |
| 169 | - p := x.pos; | |
| 170 | - FillChar(x, SizeOf(x), 0); | |
| 171 | - x.pos := p; | |
| 172 | - x.mode := mConst; | |
| 173 | - x.typ := t; | |
| 174 | - x.val := v; | |
| 175 | -end; | |
| 176 | - | |
| 177 | -{ A real constant: untyped, unless typed later. Its value is an F64, | |
| 178 | - which for an F32 is one an F32 can hold. } | |
| 179 | -procedure MakeReal(var x: TItem; r: double); | |
| 180 | -begin | |
| 181 | - MakeConst(x, tRConst, 0); | |
| 182 | - Move(r, x.val, 8); | |
| 173 | + if t^.form = fReal then begin | |
| 174 | + if t^.size = 4 then KindOf := kF32 else KindOf := kF64 | |
| 175 | + end else if t^.size > WordSize then KindOf := kLong | |
| 176 | + else KindOf := kWord; | |
| 183 | 177 | end; |
| 184 | 178 | |
| 185 | -function IsF32(t: PType): boolean; | |
| 186 | -begin | |
| 187 | - IsF32 := (t^.form = fReal) and (t^.size = 4); | |
| 188 | -end; | |
| 179 | +{ The stack } | |
| 189 | 180 | |
| 190 | -{ Gives a real constant the real type t, rounded to it. } | |
| 191 | -procedure RealTo(var x: TItem; t: PType); | |
| 192 | -var r: double; | |
| 181 | +{ rax is wanted for a new value: the one in it is pushed } | |
| 182 | +procedure Spill; | |
| 193 | 183 | begin |
| 194 | - r := RealOf(x); | |
| 195 | - if IsF32(t) then MakeReal(x, single(r)) else MakeReal(x, r); | |
| 196 | - x.typ := t; | |
| 197 | -end; | |
| 198 | - | |
| 199 | -function RealOf(const x: TItem): double; | |
| 200 | -var r: double; | |
| 201 | -begin | |
| 202 | - Move(x.val, r, 8); | |
| 203 | - RealOf := r; | |
| 204 | -end; | |
| 205 | - | |
| 206 | -{ The current string literal, as an item of its own chunk. } | |
| 207 | -procedure MakeStr(var x: TItem); | |
| 208 | -begin | |
| 209 | - FillChar(x, SizeOf(x), 0); | |
| 210 | - x.mode := mStr; | |
| 211 | - x.typ := tStr; | |
| 212 | - x.val := slen; | |
| 213 | - x.chunk := NewChunk(secRodata, -1, -1); | |
| 214 | - SetData(x.chunk, @sbuf, slen + 1, 1); | |
| 215 | -end; | |
| 216 | - | |
| 217 | -{ An item for "found ...": a constant without a type of its own as it | |
| 218 | - is written, so that "found 1.5" says what is wrong; else its type. } | |
| 219 | -function What(const x: TItem): shortstring; | |
| 220 | -begin | |
| 221 | - if (x.mode = mConst) and ((x.typ = tConst) or (x.typ = tRConst)) then What := Source(x.pos) | |
| 222 | - else What := TypeName(x.typ); | |
| 223 | -end; | |
| 224 | - | |
| 225 | -{ Two operands of different types. Numbers convert, so say how. } | |
| 226 | -procedure Mismatch(const x, y: TItem; xt, yt: PType); | |
| 227 | -var msg: shortstring; | |
| 228 | -begin | |
| 229 | - msg := TypeName(xt) + ' and ' + TypeName(yt) + ' are different types'; | |
| 230 | - if (IsInt(xt) or (xt^.form = fReal)) and (IsInt(yt) or (yt^.form = fReal)) | |
| 231 | - and (xt <> tConst) and (xt <> tRConst) then | |
| 232 | - MarkHelp(Join(x.pos, y.pos), msg, 'convert one to the other''s type, as in ' + TypeName(xt) + '(' + Source(y.pos) + ')') | |
| 233 | - else MarkAt(Join(x.pos, y.pos), msg); | |
| 234 | -end; | |
| 235 | - | |
| 236 | -{ An item that is not a value, by what it is. } | |
| 237 | -procedure NotValue(const x: TItem); | |
| 238 | -begin | |
| 239 | - case x.mode of | |
| 240 | - mType: MarkAt(x.pos, TypeName(x.typ) + ' is a type, not a value'); | |
| 241 | - mBuiltin: MarkAt(x.pos, NameOf(x.obj^.name) + ' is a built-in procedure, not a value'); | |
| 242 | - mCase: MarkAt(x.pos, NameOf(x.obj^.name) + ' is a case, not a value'); | |
| 243 | - mNone: | |
| 244 | - if x.obj <> nil then MarkAt(x.pos, NameOf(x.obj^.name) + ' returns nothing') | |
| 245 | - else MarkAt(x.pos, 'the procedure returns nothing'); | |
| 246 | - else MarkAt(x.pos, 'a string can''t be an operand'); | |
| 184 | + if inRax >= 0 then begin | |
| 185 | + Push(RAX); | |
| 186 | + inc(pushed); | |
| 187 | + inRax := -1; | |
| 247 | 188 | end; |
| 248 | 189 | end; |
| 249 | 190 | |
| 250 | -procedure MakeItem(var x: TItem; o: PObj); | |
| 191 | +procedure NewEntry(k: TKind; isConst: boolean; v: int64); | |
| 251 | 192 | begin |
| 252 | - FillChar(x, SizeOf(x), 0); | |
| 253 | - x.typ := o^.typ; | |
| 254 | - x.obj := o; | |
| 255 | - case o^.cls of | |
| 256 | - oConst: | |
| 257 | - if o^.typ = tStr then begin | |
| 258 | - x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk | |
| 259 | - end else if o^.typ^.form = fArray then begin | |
| 260 | - { an array constant: a variable in read-only data, not one of | |
| 261 | - the program's, so nothing can change it } | |
| 262 | - x.mode := mVar; x.base := bGlobal; x.chunk := o^.chunk | |
| 263 | - end else begin | |
| 264 | - x.mode := mConst; x.val := o^.val | |
| 265 | - end; | |
| 266 | - oVar: begin | |
| 267 | - x.mode := mVar; | |
| 268 | - x.ro := (o^.lev = 0) and (o^.module <> curModule); | |
| 269 | - x.isVar := not x.ro; | |
| 270 | - if o^.ext then begin | |
| 271 | - Load(RAX, MemRip(o^.chunk, 0), 8, false); | |
| 272 | - x.base := bReg; x.reg := RAX; | |
| 273 | - end else if o^.lev = 0 then begin | |
| 274 | - x.base := bGlobal; x.chunk := o^.chunk | |
| 275 | - end else if o^.ind then begin | |
| 276 | - Load(RAX, Mem(RBP, o^.val), 8, false); | |
| 277 | - x.base := bReg; x.reg := RAX; x.off := o^.off; | |
| 278 | - end else begin | |
| 279 | - x.base := bFrame; x.off := o^.val | |
| 280 | - end; | |
| 281 | - end; | |
| 282 | - oProc: x.mode := mProc; | |
| 283 | - oType: x.mode := mType; | |
| 284 | - oBuiltin: x.mode := mBuiltin; | |
| 285 | - oCase: x.mode := mCase; | |
| 286 | - else Mark(NameOf(o^.name) + ' is not a value'); | |
| 287 | - end; | |
| 193 | + if depth > High(stack) then Mark('the expression is too deep'); | |
| 194 | + stack[depth].k := k; | |
| 195 | + stack[depth].isConst := isConst; | |
| 196 | + stack[depth].val := v; | |
| 197 | + inc(depth); | |
| 288 | 198 | end; |
| 289 | 199 | |
| 290 | -procedure PushRax; | |
| 200 | +{ The value just computed in rax goes on top } | |
| 201 | +procedure Computed(k: TKind); | |
| 291 | 202 | begin |
| 292 | - Push(RAX); | |
| 293 | - inc(depth); | |
| 294 | - pushEnd := pc; | |
| 203 | + NewEntry(k, false, 0); | |
| 204 | + inRax := depth - 1; | |
| 295 | 205 | end; |
| 296 | 206 | |
| 297 | -procedure PopR(r: longint); | |
| 207 | +{ The value on top, popped into register r } | |
| 208 | +procedure PopTo(r: longint); | |
| 209 | +var e: TEntry; | |
| 298 | 210 | begin |
| 299 | - Pop(r); | |
| 211 | + if depth = 0 then Fatal('internal error: the stack is empty'); | |
| 300 | 212 | dec(depth); |
| 301 | -end; | |
| 302 | - | |
| 303 | -procedure CmpI(r: longint; v: int64); | |
| 304 | -begin | |
| 305 | - if (v >= low(longint)) and (v <= high(longint)) then AluI(aluCmp, r, v) | |
| 306 | - else begin MovRI(R11, v); Alu(aluCmp, r, R11) end; | |
| 307 | -end; | |
| 308 | - | |
| 309 | -{ A constant into r: an error constant is the address of its chunk. } | |
| 310 | -procedure ConstTo(r: longint; const x: TItem); | |
| 311 | -var f: single; | |
| 312 | -begin | |
| 313 | - if (x.typ^.form = fError) and (x.val > 0) then Lea(r, MemRip(x.val - 1, 0)) | |
| 314 | - else if IsF32(x.typ) then begin | |
| 315 | - f := RealOf(x); | |
| 316 | - MovRI(r, PLongword(@f)^); | |
| 317 | - end else MovRI(r, x.val); | |
| 318 | -end; | |
| 319 | - | |
| 320 | -function MemOf(var x: TItem; r: longint): TMem; | |
| 321 | -begin | |
| 322 | - case x.base of | |
| 323 | - bFrame: MemOf := Mem(RBP, x.off); | |
| 324 | - bGlobal: MemOf := MemRip(x.chunk, x.off); | |
| 325 | - bStack: begin | |
| 326 | - PopR(r); | |
| 327 | - x.base := bReg; x.reg := r; | |
| 328 | - MemOf := Mem(r, x.off); | |
| 329 | - end; | |
| 330 | - bSlot: begin | |
| 331 | - Load(r, Mem(RBP, x.chunk), 8, false); | |
| 332 | - x.base := bReg; x.reg := r; | |
| 333 | - MemOf := Mem(r, x.off); | |
| 334 | - end; | |
| 335 | - else MemOf := Mem(x.reg, x.off); | |
| 213 | + e := stack[depth]; | |
| 214 | + if e.isConst then begin | |
| 215 | + if r = RAX then Spill; | |
| 216 | + MovRI(r, e.val); | |
| 217 | + end else if inRax = depth then begin | |
| 218 | + if r <> RAX then MovRR(r, RAX); | |
| 219 | + inRax := -1; | |
| 220 | + end else begin | |
| 221 | + Pop(r); | |
| 222 | + dec(pushed); | |
| 336 | 223 | end; |
| 337 | 224 | end; |
| 338 | 225 | |
| 339 | -function ScalarSize(t: PType): longint; | |
| 226 | +{ The value on top, into rax, and on top still } | |
| 227 | +procedure TopToRax; | |
| 228 | +var k: TKind; | |
| 340 | 229 | begin |
| 341 | - if t^.form in [fInt, fBool, fEnum, fSet, fReal] then ScalarSize := t^.size | |
| 342 | - else ScalarSize := 8; | |
| 230 | + k := stack[depth - 1].k; | |
| 231 | + PopTo(RAX); | |
| 232 | + Computed(k); | |
| 343 | 233 | end; |
| 344 | 234 | |
| 345 | -{ Brings a value just computed in rax to its type's width. } | |
| 346 | -procedure Norm(t: PType); | |
| 235 | +procedure ResetStack(d, p: longint); | |
| 347 | 236 | begin |
| 348 | - if (t^.form = fInt) and (t^.size < 8) then Extend(RAX, t^.size, t^.signed); | |
| 237 | + depth := d; | |
| 238 | + pushed := p; | |
| 239 | + inRax := -1; | |
| 349 | 240 | end; |
| 350 | 241 | |
| 351 | -function Cut(v: int64; t: PType): int64; | |
| 242 | +procedure PushConst(k: TKind; v: int64); | |
| 352 | 243 | begin |
| 353 | - Cut := v; | |
| 354 | - if t^.form <> fInt then exit; | |
| 355 | - case t^.size of | |
| 356 | - 1: if t^.signed then Cut := shortint(v) else Cut := byte(v); | |
| 357 | - 2: if t^.signed then Cut := smallint(v) else Cut := word(v); | |
| 358 | - 4: if t^.signed then Cut := longint(v) else Cut := longword(v); | |
| 359 | - end; | |
| 360 | -end; | |
| 361 | - | |
| 362 | -procedure LoadVar(var x: TItem; r: longint); | |
| 363 | -begin | |
| 364 | - if not IsScalar(x.typ) then MarkAt(x.pos, 'expected a single value, found ' + What(x)); | |
| 365 | - Load(r, MemOf(x, r), ScalarSize(x.typ), (x.typ^.form = fInt) and x.typ^.signed); | |
| 244 | + NewEntry(k, true, v); | |
| 366 | 245 | end; |
| 367 | 246 | |
| 368 | -{ Takes back the push of rax just emitted, if it is the last code, and | |
| 369 | - neither a jump nor a line starts after it: the value is still in rax. } | |
| 370 | -function Unpush: boolean; | |
| 247 | +{ The memory a names: an address on the stack is popped into r, one in | |
| 248 | + a slot loaded into r } | |
| 249 | +function Place(const a: TAdr; r: longint): TMem; | |
| 371 | 250 | begin |
| 372 | - Unpush := (pc = pushEnd) and (labelAt <> pc) | |
| 373 | - and ((numLines = 0) or (lines^[numLines - 1].at < pc)); | |
| 374 | - if Unpush then begin dec(pc); dec(depth); pushEnd := -1 end; | |
| 375 | -end; | |
| 376 | - | |
| 377 | -{ Code computing with a real is noted on its chunk, at the first place, | |
| 378 | - for a target without reals (report 5.1) to refuse if the chunk turns | |
| 379 | - out to be used. A real moved whole, as a record's bytes are copied, | |
| 380 | - needs nothing the target lacks, and isn't noted. } | |
| 381 | -procedure UsesReals(const p: TPos); | |
| 382 | -begin | |
| 383 | - if (codeChunk >= 0) and not chunks^[codeChunk].reals then begin | |
| 384 | - chunks^[codeChunk].reals := true; | |
| 385 | - chunks^[codeChunk].realsAt := p; | |
| 251 | + case a.base of | |
| 252 | + bFrame: Place := Mem(RBP, a.off); | |
| 253 | + bGlobal: Place := MemRip(a.chunk, a.off); | |
| 254 | + bStack: begin PopTo(r); Place := Mem(r, a.off) end; | |
| 255 | + else | |
| 256 | + if r = RAX then Spill; | |
| 257 | + Load(r, Mem(RBP, a.chunk), 8, false); | |
| 258 | + Place := Mem(r, a.off); | |
| 386 | 259 | end; |
| 387 | 260 | end; |
| 388 | 261 | |
| 389 | -procedure Value(var x: TItem); | |
| 390 | -begin | |
| 391 | - if x.typ^.form = fReal then UsesReals(x.pos); | |
| 392 | - case x.mode of | |
| 393 | - mConst: ConstTo(RAX, x); | |
| 394 | - mVar: LoadVar(x, RAX); | |
| 395 | - mReg: ; | |
| 396 | - mStack: PopR(RAX); | |
| 397 | - mProc: begin | |
| 398 | - if x.obj^.isCode and (x.obj^.typ^.base <> nil) and x.obj^.typ^.base^.tuple then | |
| 399 | - MarkAt(x.pos, 'a code procedure with two results can only be called'); | |
| 400 | - if x.obj^.ext then Load(RAX, MemRip(x.obj^.chunk, 0), 8, false) | |
| 401 | - else Lea(RAX, MemRip(x.obj^.chunk, 0)); | |
| 402 | - end; | |
| 403 | - else NotValue(x); | |
| 404 | - end; | |
| 405 | - x.mode := mReg; | |
| 406 | -end; | |
| 407 | - | |
| 408 | -procedure Address(var x: TItem; r: longint); | |
| 262 | +procedure PushAdr(const a: TAdr); | |
| 409 | 263 | var m: TMem; |
| 410 | 264 | begin |
| 411 | - if x.mode = mStr then Lea(r, MemRip(x.chunk, 0)) | |
| 412 | - else if x.mode = mVar then begin | |
| 413 | - m := MemOf(x, r); | |
| 414 | - if (x.base <> bReg) or (x.reg <> r) or (x.off <> 0) then Lea(r, m); | |
| 415 | - x.base := bReg; x.reg := r; x.off := 0; | |
| 416 | - end else MarkAt(x.pos, 'expected a variable'); | |
| 265 | + if (a.base = bStack) and (a.off = 0) then exit; | |
| 266 | + m := Place(a, RAX); | |
| 267 | + Spill; | |
| 268 | + Lea(RAX, m); | |
| 269 | + Computed(kWord); | |
| 417 | 270 | end; |
| 418 | 271 | |
| 419 | -procedure Save(var x: TItem); | |
| 272 | +procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind); | |
| 273 | +var m: TMem; | |
| 420 | 274 | begin |
| 421 | - if x.mode = mReg then begin | |
| 422 | - PushRax; x.mode := mStack | |
| 423 | - end else if (x.mode = mVar) and (x.base = bReg) then begin | |
| 424 | - if x.reg <> RAX then MovRR(RAX, x.reg); | |
| 425 | - PushRax; x.base := bStack; | |
| 426 | - end; | |
| 275 | + m := Place(a, RAX); | |
| 276 | + Spill; | |
| 277 | + Load(RAX, m, size, signed); | |
| 278 | + Computed(k); | |
| 427 | 279 | end; |
| 428 | 280 | |
| 429 | -{ Frames } | |
| 430 | - | |
| 431 | -function AllocTemp(size, align: int64): int64; | |
| 281 | +procedure PushProc(c: longint; ext: boolean); | |
| 432 | 282 | begin |
| 433 | - if align < 1 then align := 1; | |
| 434 | - frameTop := AlignUp(frameTop + size, align); | |
| 435 | - if frameTop > frameMax then frameMax := frameTop; | |
| 436 | - AllocTemp := -frameTop; | |
| 283 | + Spill; | |
| 284 | + if ext then Load(RAX, MemRip(c, 0), 8, false) | |
| 285 | + else Lea(RAX, MemRip(c, 0)); | |
| 286 | + Computed(kWord); | |
| 437 | 287 | end; |
| 438 | 288 | |
| 439 | -function AllocPerm(size, align: int64): int64; | |
| 289 | +{ Pops a value into the variable at a; an address on the stack is under | |
| 290 | + the value. } | |
| 291 | +procedure PopVar(const a: TAdr; size: longint); | |
| 292 | +var m: TMem; | |
| 440 | 293 | begin |
| 441 | - AllocPerm := AllocTemp(size, align); | |
| 442 | - permTop := frameTop; | |
| 443 | -end; | |
| 444 | - | |
| 445 | -function TempMark: int64; | |
| 446 | -begin | |
| 447 | - TempMark := frameTop; | |
| 448 | -end; | |
| 449 | - | |
| 450 | -procedure ReleaseTemps(m: int64); | |
| 451 | -begin | |
| 452 | - if m < permTop then m := permTop; | |
| 453 | - frameTop := m; | |
| 454 | -end; | |
| 455 | - | |
| 456 | -{ Every slot used so far stays reserved for the rest of the procedure: | |
| 457 | - a deferred statement's temporaries live until it runs. } | |
| 458 | -procedure KeepTemps; | |
| 459 | -begin | |
| 460 | - frameTop := frameMax; | |
| 461 | - permTop := frameMax; | |
| 294 | + if a.base in [bFrame, bGlobal] then begin | |
| 295 | + PopTo(RAX); | |
| 296 | + Store(Place(a, RAX), RAX, size); | |
| 297 | + end else begin | |
| 298 | + PopTo(RCX); | |
| 299 | + m := Place(a, RAX); | |
| 300 | + Store(m, RCX, size); | |
| 301 | + end; | |
| 462 | 302 | end; |
| 463 | 303 | |
| 464 | -procedure TempItem(var x: TItem; t: PType; off: int64); | |
| 465 | -var p: TPos; | |
| 304 | +procedure Drop; | |
| 466 | 305 | begin |
| 467 | - p := x.pos; | |
| 468 | - FillChar(x, SizeOf(x), 0); | |
| 469 | - x.pos := p; | |
| 470 | - x.mode := mVar; x.typ := t; x.base := bFrame; x.off := off; | |
| 306 | + dec(depth); | |
| 307 | + if stack[depth].isConst then exit; | |
| 308 | + if inRax = depth then inRax := -1 | |
| 309 | + else begin | |
| 310 | + AluI(aluAdd, RSP, 8); | |
| 311 | + dec(pushed); | |
| 312 | + end; | |
| 471 | 313 | end; |
| 472 | 314 | |
| 473 | -function ToSlot(var x: TItem): int64; | |
| 474 | -var s: int64; | |
| 315 | +{ The two values on top change places. } | |
| 316 | +procedure Swap; | |
| 317 | +var e: TEntry; a, b: boolean; | |
| 475 | 318 | begin |
| 476 | - Value(x); | |
| 477 | - s := AllocTemp(8, 8); | |
| 478 | - Store(Mem(RBP, s), RAX, 8); | |
| 479 | - ToSlot := s; | |
| 319 | + a := stack[depth - 2].isConst; | |
| 320 | + b := stack[depth - 1].isConst; | |
| 321 | + if not a and not b then begin | |
| 322 | + { the one on top is in rax, or pushed over the other } | |
| 323 | + if inRax <> depth - 1 then begin Pop(RAX); dec(pushed) end; | |
| 324 | + Xchg(Mem(RSP, 0), RAX); | |
| 325 | + inRax := depth - 1; | |
| 326 | + end else if inRax = depth - 1 then inRax := depth - 2 | |
| 327 | + else if inRax = depth - 2 then inRax := depth - 1; | |
| 328 | + e := stack[depth - 1]; | |
| 329 | + stack[depth - 1] := stack[depth - 2]; | |
| 330 | + stack[depth - 2] := e; | |
| 480 | 331 | end; |
| 481 | 332 | |
| 482 | 333 | { Copying } |
| @@ -485,7 +336,7 @@ | ||
| 485 | 336 | as they fall. Up to 64 bytes, as a record or a small array mostly |
| 486 | 337 | is, they go as moves of 8, 4, 2 and 1 bytes: rep movsb takes some |
| 487 | 338 | twenty cycles to start, longer than the few moves take. } |
| 488 | -procedure Copy(size: int64); | |
| 339 | +procedure CopyBytes(size: int64); | |
| 489 | 340 | var o, n: int64; |
| 490 | 341 | begin |
| 491 | 342 | if size > 64 then begin |
| @@ -503,929 +354,559 @@ | ||
| 503 | 354 | end; |
| 504 | 355 | end; |
| 505 | 356 | |
| 506 | -procedure CopyToTemp(var x: TItem); | |
| 507 | -var t: int64; | |
| 357 | +{ Pops a source's address, and a destination's under it, and copies | |
| 358 | + size bytes to off bytes past the destination. } | |
| 359 | +procedure CopyBlock(size, off: int64); | |
| 508 | 360 | begin |
| 509 | - if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 510 | - t := AllocTemp(x.typ^.size, x.typ^.align); | |
| 511 | - Address(x, RSI); | |
| 512 | - Lea(RDI, Mem(RBP, t)); | |
| 513 | - Copy(x.typ^.size); | |
| 514 | - TempItem(x, x.typ, t); | |
| 361 | + PopTo(RSI); | |
| 362 | + PopTo(RDI); | |
| 363 | + if off <> 0 then Lea(RDI, Mem(RDI, off)); | |
| 364 | + CopyBytes(size); | |
| 515 | 365 | end; |
| 516 | 366 | |
| 517 | -{ A slice made from an address in rax and a length. } | |
| 518 | -procedure SliceOf(var x: TItem; t: PType; len: int64); | |
| 519 | -var s: int64; | |
| 367 | +{ Pops a source's address, and copies size bytes to a. } | |
| 368 | +procedure CopyTo(const a: TAdr; size: int64); | |
| 520 | 369 | begin |
| 521 | - s := AllocTemp(16, 8); | |
| 522 | - Store(Mem(RBP, s), RAX, 8); | |
| 523 | - MovRI(RAX, len); | |
| 524 | - Store(Mem(RBP, s + 8), RAX, 8); | |
| 525 | - TempItem(x, t, s); | |
| 370 | + PopTo(RSI); | |
| 371 | + Lea(RDI, Place(a, RDI)); | |
| 372 | + CopyBytes(size); | |
| 526 | 373 | end; |
| 527 | 374 | |
| 528 | -{ An INT goes silently into an I64, and a UINT into a U64, losing | |
| 529 | - nothing on any target (report section 5.10). } | |
| 530 | -function Widens(s, t: PType): boolean; | |
| 375 | +procedure ZeroVar(const a: TAdr; size: int64); | |
| 531 | 376 | begin |
| 532 | - Widens := ((s = tInt) and Same(t, tI64)) or ((s = tUInt) and Same(t, tU64)); | |
| 377 | + Lea(RDI, Place(a, RDI)); | |
| 378 | + Spill; | |
| 379 | + MovRI(RCX, size); | |
| 380 | + MovRI(RAX, 0); | |
| 381 | + RepStosb; | |
| 533 | 382 | end; |
| 534 | 383 | |
| 535 | -{ Another module's exported variable can be read, not changed, nor | |
| 536 | - handed to anything that could change it (report section 3). } | |
| 537 | -procedure NotReadOnly(const x: TItem); | |
| 538 | -begin | |
| 539 | - if x.ro then MarkAt(x.pos, Source(x.pos) + ' is ' + NameOf(x.obj^.module^.name) + '''s, and can''t be changed outside it'); | |
| 540 | -end; | |
| 384 | +{ Operations } | |
| 541 | 385 | |
| 542 | -procedure Coerce(var x: TItem; t: PType); | |
| 543 | -var s: PType; tmp: int64; | |
| 386 | +function Log2(v: int64): longint; | |
| 387 | +var n: longint; | |
| 544 | 388 | begin |
| 545 | - s := x.typ; | |
| 546 | - if x.mode = mStr then begin | |
| 547 | - if (t^.form = fSlice) and (t^.base = tU8) then begin | |
| 548 | - Address(x, RAX); SliceOf(x, t, x.val) | |
| 549 | - end else if (t^.form = fPtr) and (t^.base = tU8) then begin | |
| 550 | - Address(x, RAX); x.mode := mReg; x.typ := t | |
| 551 | - end else if (t^.form = fArray) and (t^.base = tU8) and (t^.len >= x.val) then begin | |
| 552 | - tmp := AllocTemp(t^.size, 1); | |
| 553 | - Lea(RDI, Mem(RBP, tmp)); | |
| 554 | - Lea(RSI, MemRip(x.chunk, 0)); | |
| 555 | - Copy(x.val); | |
| 556 | - Lea(RDI, Mem(RBP, tmp + x.val)); | |
| 557 | - MovRI(RCX, t^.len - x.val); | |
| 558 | - MovRI(RAX, 0); | |
| 559 | - RepStosb; | |
| 560 | - TempItem(x, t, tmp); | |
| 561 | - end else MarkAt(x.pos, 'can''t use a string as ' + TypeName(t)); | |
| 562 | - exit; | |
| 563 | - end; | |
| 564 | - if x.mode in [mType, mBuiltin, mNone, mCase] then NotValue(x); | |
| 565 | - if Same(s, t) then exit; | |
| 566 | - if Widens(s, t) then x.typ := t { the same bits, on a 64-bit target } | |
| 567 | - else if (s = tRConst) and (t^.form = fReal) then RealTo(x, t) | |
| 568 | - else if (s^.form = fConst) and (t^.form = fInt) then begin | |
| 569 | - if (x.mode = mConst) and not Fits(x.val, t) then MarkAt(x.pos, IntStr(x.val) + ' doesn''t fit ' + TypeName(t)); | |
| 570 | - x.typ := t; | |
| 571 | - end else if (s^.form = fNil) and (t^.form in [fPtr, fProc, fError]) then | |
| 572 | - x.typ := t | |
| 573 | - else if (s^.form = fPtr) and (t^.form = fPtr) and t^.vol and not s^.vol | |
| 574 | - and Same(s^.base, t^.base) then | |
| 575 | - x.typ := t | |
| 576 | - else if (s^.form = fArray) and (t^.form = fSlice) and Same(s^.base, t^.base) then begin | |
| 577 | - NotReadOnly(x); | |
| 578 | - if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); | |
| 579 | - if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); | |
| 580 | - Address(x, RAX); | |
| 581 | - SliceOf(x, t, s^.len); | |
| 582 | - end else | |
| 583 | - MarkAt(x.pos, 'can''t use ' + TypeName(s) + ' as ' + TypeName(t)); | |
| 389 | + n := 0; | |
| 390 | + while (v > 1) and (v and 1 = 0) do begin v := v shr 1; inc(n) end; | |
| 391 | + if v <> 1 then n := -1; | |
| 392 | + Log2 := n; | |
| 584 | 393 | end; |
| 585 | 394 | |
| 586 | -{ Coerce where the types must be the same, as in a for loop, INC and | |
| 587 | - several results, which don't widen an INT to an I64 (report sections | |
| 588 | - 5.10, 6.1 and 7.2); help, if not empty, says why. } | |
| 589 | -procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); | |
| 590 | -var h: shortstring; | |
| 395 | +{ rax div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards | |
| 396 | + zero, so a negative dividend first gets 2^k - 1 added (the bias). } | |
| 397 | +procedure DivPow2(op: TSym; k: longint; unsign: boolean); | |
| 591 | 398 | begin |
| 592 | - if Widens(x.typ, t) then begin | |
| 593 | - h := help; | |
| 594 | - if h = '' then h := 'they don''t mix here: write ' + TypeName(t) + '(' + Source(x.pos) + ')'; | |
| 595 | - MarkHelp(x.pos, 'expected ' + TypeName(t) + ', found ' + What(x), h); | |
| 399 | + if unsign then begin | |
| 400 | + if op = sDiv then ShiftI(shShr, RAX, k) | |
| 401 | + else begin ShiftI(shShl, RAX, 64 - k); ShiftI(shShr, RAX, 64 - k) end; | |
| 402 | + exit; | |
| 596 | 403 | end; |
| 597 | - Coerce(x, t); | |
| 404 | + MovRR(RCX, RAX); | |
| 405 | + ShiftI(shSar, RCX, 63); | |
| 406 | + ShiftI(shShr, RCX, 64 - k); | |
| 407 | + Alu(aluAdd, RCX, RAX); | |
| 408 | + ShiftI(shSar, RCX, k); | |
| 409 | + if op = sDiv then MovRR(RAX, RCX) | |
| 410 | + else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end; | |
| 598 | 411 | end; |
| 599 | 412 | |
| 600 | -procedure Assign(var dst, src: TItem); | |
| 601 | -var m: TMem; | |
| 413 | +procedure CmpI(r: longint; v: int64); | |
| 602 | 414 | begin |
| 603 | - Coerce(src, dst.typ); | |
| 604 | - if IsScalar(dst.typ) then begin | |
| 605 | - Value(src); | |
| 606 | - m := MemOf(dst, RCX); | |
| 607 | - Store(m, RAX, ScalarSize(dst.typ)); | |
| 608 | - end else begin | |
| 609 | - if dst.vol or src.vol then MarkAt(src.pos, 'can''t copy through a volatile pointer'); | |
| 610 | - Address(src, RSI); | |
| 611 | - Address(dst, RDI); | |
| 612 | - Copy(dst.typ^.size); | |
| 613 | - end; | |
| 415 | + if (v >= low(longint)) and (v <= high(longint)) then AluI(aluCmp, r, v) | |
| 416 | + else begin MovRI(R11, v); Alu(aluCmp, r, R11) end; | |
| 614 | 417 | end; |
| 615 | - | |
| 616 | -{ Checks } | |
| 617 | 418 | |
| 618 | -const | |
| 619 | - NoBase = -2; { a TMem that is no memory } | |
| 620 | - | |
| 621 | -{ The program goes on if the condition cc holds, and otherwise stops | |
| 622 | - in Rt.fail(what, a, b, len, at, name), which says what failed at p, | |
| 623 | - found by at, the address of the call: an index a (what = 0), a slice | |
| 624 | - a..b (1), each for a length len, or a position a of the enumeration | |
| 625 | - called name (2). a is in register ra, b in RDX; len is in memory at | |
| 626 | - m, or n if m.base is NoBase. The failing code has p for its place, | |
| 627 | - and the code after it the place before it again. } | |
| 628 | -procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint); | |
| 629 | -var L, line, col: longint; before: TLine; | |
| 419 | +{ Before rax div rcx, of a signed type of bits < 64: its smallest value | |
| 420 | + divided by -1 overflows it, which must stop the program as it does for | |
| 421 | + an I64 (report section 6.1). idiv on the 64-bit values wouldn't fault, | |
| 422 | + so then rax is made the smallest I64, and it does. } | |
| 423 | +procedure DivTrap(bits: longint); | |
| 424 | +var L: longint; | |
| 630 | 425 | begin |
| 631 | 426 | L := NoLabel; |
| 632 | - Jcc(cc, L); | |
| 633 | - before := lines^[numLines - 1]; | |
| 634 | - LineCol(p.a, line, col); | |
| 635 | - NoteLine(line, col); | |
| 636 | - MovRR(RSI, ra); | |
| 637 | - if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false); | |
| 638 | - Lea(R8, Here); | |
| 639 | - MovRI(RDI, what); | |
| 640 | - if name >= 0 then Lea(R9, MemRip(name, 0)); | |
| 641 | - AluI(aluAnd, RSP, -16); | |
| 642 | - CallChunk(rtFail); | |
| 643 | - Ud2; | |
| 427 | + CmpI(RCX, -1); | |
| 428 | + Jcc(ccNE, L); | |
| 429 | + CmpI(RAX, int64(-1) shl (bits - 1)); | |
| 430 | + Jcc(ccNE, L); | |
| 431 | + ShiftI(shShl, RAX, 64 - bits); | |
| 644 | 432 | FixLink(L); |
| 645 | - NoteLine(before.line, before.col); | |
| 646 | 433 | end; |
| 647 | 434 | |
| 648 | -{ For Check: a length known here, not in memory } | |
| 649 | -function NoMem: TMem; | |
| 435 | +{ The two values on top, the left one into rax and the right into rcx } | |
| 436 | +procedure Operands; | |
| 650 | 437 | begin |
| 651 | - NoMem := Mem(NoBase, 0); | |
| 438 | + PopTo(RCX); | |
| 439 | + PopTo(RAX); | |
| 652 | 440 | end; |
| 653 | 441 | |
| 654 | -{ t's name, ending in a zero byte, for Rt.fail } | |
| 655 | -function NameChunk(t: PType): longint; | |
| 656 | -var s: shortstring; c: longint; | |
| 442 | +procedure RealOp(op: TSym; k: TKind); | |
| 443 | +var f: boolean; | |
| 657 | 444 | begin |
| 658 | - s := TypeName(t) + #0; | |
| 659 | - c := NewChunk(secRodata, -1, -1); | |
| 660 | - SetData(c, @s[1], length(s), 1); | |
| 661 | - NameChunk := c; | |
| 445 | + f := k = kF32; | |
| 446 | + Operands; | |
| 447 | + MovqXR(0, RAX); | |
| 448 | + MovqXR(1, RCX); | |
| 449 | + case op of | |
| 450 | + sPlus: Sse($58, 0, 1, f); | |
| 451 | + sMinus: Sse($5C, 0, 1, f); | |
| 452 | + sTimes: Sse($59, 0, 1, f); | |
| 453 | + sSlash: Sse($5E, 0, 1, f); | |
| 454 | + end; | |
| 455 | + MovqRX(RAX, 0); | |
| 456 | + Computed(k); | |
| 662 | 457 | end; |
| 663 | 458 | |
| 664 | -{ Selectors } | |
| 665 | - | |
| 666 | -procedure Deref(var x: TItem); | |
| 667 | -var t: PType; | |
| 459 | +{ The two values on top, the left op the right, an integer of size | |
| 460 | + bytes, signed or not, brought back to its width. } | |
| 461 | +procedure Op(op: TSym; k: TKind; size: longint; signed: boolean); | |
| 462 | +var y: TEntry; sh, bits: longint; | |
| 668 | 463 | begin |
| 669 | - t := x.typ; | |
| 670 | - if t^.form <> fPtr then MarkAt(x.pos, 'expected a pointer, found ' + TypeName(t)); | |
| 671 | - if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared'); | |
| 672 | - Value(x); | |
| 673 | - x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; | |
| 674 | - x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; | |
| 675 | -end; | |
| 676 | - | |
| 677 | -{ A field of a packed record, of more than one byte's alignment, may | |
| 678 | - sit anywhere; it is read and written, but has no address and no | |
| 679 | - volatile access, which a machine that can't reach a misaligned word | |
| 680 | - couldn't make one access (report section 5.5). } | |
| 681 | -procedure Field(var x: TItem; f: PObj); | |
| 682 | -begin | |
| 683 | - if x.typ^.packd and (f^.typ^.align > 1) then begin | |
| 684 | - if x.vol then MarkAt(x.pos, 'a field of a packed record can''t be volatile'); | |
| 685 | - x.odd := true; | |
| 464 | + if k in [kF32, kF64] then begin RealOp(op, k); exit end; | |
| 465 | + bits := size * 8; | |
| 466 | + y := stack[depth - 1]; | |
| 467 | + sh := -1; | |
| 468 | + if y.isConst and (op in [sTimes, sDiv, sMod]) then sh := Log2(y.val); | |
| 469 | + if y.isConst then begin | |
| 470 | + dec(depth); | |
| 471 | + PopTo(RAX); | |
| 472 | + if sh < 1 then MovRI(RCX, y.val); | |
| 473 | + end else Operands; | |
| 474 | + case op of | |
| 475 | + sPlus: Alu(aluAdd, RAX, RCX); | |
| 476 | + sMinus: Alu(aluSub, RAX, RCX); | |
| 477 | + sTimes: if sh >= 1 then ShiftI(shShl, RAX, sh) else Imul(RAX, RCX); | |
| 478 | + sDiv, sMod: if sh >= 1 then DivPow2(op, sh, not signed) else begin | |
| 479 | + if not signed then begin MovRI(RDX, 0); Unary(6, RCX) end | |
| 480 | + else begin | |
| 481 | + if (bits < 64) and (not y.isConst or (y.val = -1)) then DivTrap(bits); | |
| 482 | + Cqo; Unary(7, RCX) | |
| 483 | + end; | |
| 484 | + if op = sMod then MovRR(RAX, RDX); | |
| 485 | + end; | |
| 486 | + sAmp: Alu(aluAnd, RAX, RCX); | |
| 487 | + sBar: Alu(aluOr, RAX, RCX); | |
| 488 | + sXor: Alu(aluXor, RAX, RCX); | |
| 489 | + sShl, sShr: begin | |
| 490 | + { the count modulo the width: the machine takes it modulo 64 } | |
| 491 | + if bits < 64 then AluI(aluAnd, RCX, bits - 1); | |
| 492 | + if op = sShl then Shift(shShl, RAX) | |
| 493 | + else if signed then Shift(shSar, RAX) else Shift(shShr, RAX); | |
| 494 | + end; | |
| 686 | 495 | end; |
| 687 | - x.off := x.off + f^.val; | |
| 688 | - x.typ := f^.typ; | |
| 689 | -end; | |
| 690 | - | |
| 691 | -procedure IndexPrep(var x: TItem); | |
| 692 | -begin | |
| 693 | - if x.mode = mStr then Coerce(x, tBytes); | |
| 694 | - if (x.typ^.form = fPtr) and (x.typ^.base^.form = fArray) then | |
| 695 | - MarkAt(x.pos, 'a pointer to an array is indexed as p^[i]'); | |
| 696 | - if x.typ^.form = fPtr then begin | |
| 697 | - Value(x); PushRax; x.mode := mStack | |
| 698 | - end else if (x.mode = mVar) and (x.typ^.form in [fArray, fSlice]) then | |
| 699 | - Save(x) | |
| 700 | - else MarkAt(x.pos, 'expected an array, a slice or a pointer, found ' + What(x)); | |
| 496 | + if bits < 64 then Extend(RAX, size, signed); | |
| 497 | + Computed(k); | |
| 701 | 498 | end; |
| 702 | 499 | |
| 703 | -procedure Index(var x, y: TItem); | |
| 704 | -var t: PType; s: int64; m, ml: TMem; | |
| 500 | +{ The left op the right of the two values on top, as a BOOL } | |
| 501 | +procedure Compare(op: TSym; k: TKind; signed: boolean); | |
| 502 | +const | |
| 503 | + CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); | |
| 504 | + CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); | |
| 505 | +var y: TEntry; f: boolean; | |
| 705 | 506 | begin |
| 706 | - t := x.typ; | |
| 707 | - s := t^.base^.size; | |
| 708 | - if not IsInt(y.typ) then MarkAt(y.pos, 'an index is an integer, found ' + What(y)); | |
| 709 | - if t^.form = fPtr then begin | |
| 710 | - Value(y); | |
| 711 | - if s <> 1 then ImulI(RAX, s); | |
| 712 | - PopR(RCX); | |
| 713 | - Alu(aluAdd, RAX, RCX); | |
| 714 | - x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; | |
| 715 | - x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; | |
| 716 | - end else if (t^.form = fArray) and (y.mode = mConst) then begin | |
| 717 | - if (y.val < 0) or (y.val >= t^.len) then MarkAt(y.pos, 'index ' + IntStr(y.val) + ' is out of range for a length of ' + IntStr(t^.len)); | |
| 718 | - x.off := x.off + y.val * s; | |
| 719 | - if x.base = bStack then begin | |
| 720 | - PopR(RAX); x.base := bReg; x.reg := RAX | |
| 507 | + if k in [kF32, kF64] then begin | |
| 508 | + f := k = kF32; | |
| 509 | + Operands; | |
| 510 | + MovqXR(0, RAX); | |
| 511 | + MovqXR(1, RCX); | |
| 512 | + case op of | |
| 513 | + sEql: begin Ucomis(0, 1, f); SetCC(ccE, RAX); SetCC(ccNP, RCX); Alu(aluAnd, RAX, RCX) end; | |
| 514 | + sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, RAX); SetCC(ccP, RCX); Alu(aluOr, RAX, RCX) end; | |
| 515 | + sLss: begin Ucomis(1, 0, f); SetCC(ccA, RAX) end; | |
| 516 | + sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, RAX) end; | |
| 517 | + sGtr: begin Ucomis(0, 1, f); SetCC(ccA, RAX) end; | |
| 518 | + sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, RAX) end; | |
| 721 | 519 | end; |
| 722 | - end else if t^.form = fArray then begin | |
| 723 | - Value(y); | |
| 724 | - if checks then begin | |
| 725 | - CmpI(RAX, t^.len); | |
| 726 | - Check(ccB, 0, y.pos, RAX, NoMem, t^.len, -1); | |
| 727 | - end; | |
| 728 | - if s <> 1 then ImulI(RAX, s); | |
| 729 | - if x.base = bStack then PopR(RCX) else Lea(RCX, MemOf(x, RCX)); | |
| 730 | - Alu(aluAdd, RAX, RCX); | |
| 731 | - if x.base <> bStack then x.off := 0; | |
| 732 | - x.base := bReg; x.reg := RAX; | |
| 733 | 520 | end else begin |
| 734 | - Value(y); | |
| 735 | - m := MemOf(x, RCX); | |
| 736 | - ml := m; ml.disp := ml.disp + 8; | |
| 737 | - if checks then begin | |
| 738 | - AluM(aluCmp, RAX, ml); | |
| 739 | - Check(ccB, 0, y.pos, RAX, ml, 0, -1); | |
| 521 | + y := stack[depth - 1]; | |
| 522 | + if y.isConst then begin | |
| 523 | + dec(depth); | |
| 524 | + PopTo(RAX); | |
| 525 | + CmpI(RAX, y.val); | |
| 526 | + end else begin | |
| 527 | + Operands; | |
| 528 | + Alu(aluCmp, RAX, RCX); | |
| 740 | 529 | end; |
| 741 | - if s <> 1 then ImulI(RAX, s); | |
| 742 | - AluM(aluAdd, RAX, m); | |
| 743 | - x.base := bReg; x.reg := RAX; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; | |
| 530 | + if signed then SetCC(CcSigned[op], RAX) else SetCC(CcUnsigned[op], RAX); | |
| 744 | 531 | end; |
| 745 | - x.typ := t^.base; | |
| 532 | + Computed(kWord); | |
| 746 | 533 | end; |
| 747 | 534 | |
| 748 | -procedure SliceLo(var lo: TItem); | |
| 535 | +procedure Neg(k: TKind; size: longint; signed: boolean); | |
| 749 | 536 | begin |
| 750 | - if not IsInt(lo.typ) then MarkAt(lo.pos, 'an index is an integer, found ' + What(lo)); | |
| 751 | - Value(lo); | |
| 752 | - PushRax; | |
| 537 | + PopTo(RAX); | |
| 538 | + case k of | |
| 539 | + kF32: Btc(RAX, 31); | |
| 540 | + kF64: Btc(RAX, 63); | |
| 541 | + else | |
| 542 | + Unary(3, RAX); | |
| 543 | + if size < 8 then Extend(RAX, size, signed); | |
| 544 | + end; | |
| 545 | + Computed(k); | |
| 753 | 546 | end; |
| 754 | 547 | |
| 755 | -procedure Slice(var x, lo, hi: TItem); | |
| 756 | -var t, st: PType; s, tmp: int64; m, ml: TMem; | |
| 548 | +procedure Complement(size: longint; signed: boolean); | |
| 757 | 549 | begin |
| 758 | - t := x.typ; | |
| 759 | - s := t^.base^.size; | |
| 760 | - if not IsInt(hi.typ) then MarkAt(hi.pos, 'an index is an integer, found ' + What(hi)); | |
| 761 | - if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); | |
| 762 | - if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); | |
| 763 | - if t^.form = fArray then NotReadOnly(x); | |
| 764 | - Value(hi); | |
| 765 | - MovRR(RDX, RAX); | |
| 766 | - PopR(RCX); | |
| 767 | - if t^.form = fPtr then begin | |
| 768 | - { the bounds can't be known, but a slice can't run backwards } | |
| 769 | - PopR(R8); | |
| 770 | - if checks then begin | |
| 771 | - Alu(aluCmp, RCX, RDX); | |
| 772 | - Check(ccLE, 3, lo.pos, RCX, NoMem, 0, -1); | |
| 773 | - end; | |
| 774 | - end else begin | |
| 775 | - m := MemOf(x, R8); | |
| 776 | - ml := m; ml.disp := ml.disp + 8; | |
| 777 | - if checks then begin | |
| 778 | - if t^.form = fArray then begin CmpI(RDX, t^.len); ml := NoMem end | |
| 779 | - else AluM(aluCmp, RDX, ml); | |
| 780 | - Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); | |
| 781 | - Alu(aluCmp, RCX, RDX); | |
| 782 | - Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); | |
| 783 | - end; | |
| 784 | - end; | |
| 785 | - tmp := AllocTemp(16, 8); | |
| 786 | - MovRR(RAX, RDX); | |
| 787 | - Alu(aluSub, RAX, RCX); | |
| 788 | - Store(Mem(RBP, tmp + 8), RAX, 8); | |
| 789 | - if s <> 1 then ImulI(RCX, s); | |
| 790 | - case t^.form of | |
| 791 | - fPtr: MovRR(RAX, R8); | |
| 792 | - fArray: Lea(RAX, m); | |
| 793 | - else Load(RAX, m, 8, false); | |
| 794 | - end; | |
| 795 | - Alu(aluAdd, RAX, RCX); | |
| 796 | - Store(Mem(RBP, tmp), RAX, 8); | |
| 797 | - st := NewType(fSlice, 16, 8); | |
| 798 | - st^.base := t^.base; | |
| 799 | - TempItem(x, st, tmp); | |
| 550 | + PopTo(RAX); | |
| 551 | + Unary(2, RAX); | |
| 552 | + if size < 8 then Extend(RAX, size, signed); | |
| 553 | + Computed(kWord); | |
| 800 | 554 | end; |
| 801 | 555 | |
| 802 | -{ Operators } | |
| 803 | - | |
| 804 | -procedure Neg(var x: TItem); | |
| 556 | +procedure NotBool; | |
| 805 | 557 | begin |
| 806 | - if x.typ^.form = fReal then begin | |
| 807 | - if x.mode = mConst then x.val := x.val xor low(int64) | |
| 808 | - else begin Value(x); if IsF32(x.typ) then Btc(RAX, 31) else Btc(RAX, 63) end; | |
| 809 | - end else if IsInt(x.typ) then begin | |
| 810 | - if (x.mode = mConst) and (x.typ^.form = fConst) and (x.val = low(int64)) then | |
| 811 | - MarkAt(x.pos, 'the constant is too big for I64') | |
| 812 | - else if x.mode = mConst then x.val := Cut(-x.val, x.typ) | |
| 813 | - else begin Value(x); Unary(3, RAX); Norm(x.typ) end; | |
| 814 | - end else MarkAt(x.pos, 'can''t negate ' + TypeName(x.typ)); | |
| 558 | + PopTo(RAX); | |
| 559 | + AluI(aluXor, RAX, 1); | |
| 560 | + Computed(kWord); | |
| 815 | 561 | end; |
| 816 | 562 | |
| 817 | -procedure NotOp(var x: TItem); | |
| 563 | +{ The integer on top, brought to size bytes } | |
| 564 | +procedure Narrow(size: longint; signed: boolean); | |
| 818 | 565 | begin |
| 819 | - if IsInt(x.typ) then MarkHelp(x.pos, '"not" needs a BOOL, found ' + What(x), 'the bits of an integer flip with ~'); | |
| 820 | - if x.typ^.form <> fBool then MarkAt(x.pos, '"not" needs a BOOL, found ' + What(x)); | |
| 821 | - if x.mode = mConst then x.val := 1 - x.val | |
| 822 | - else begin Value(x); AluI(aluXor, RAX, 1) end; | |
| 566 | + PopTo(RAX); | |
| 567 | + if size < 8 then Extend(RAX, size, signed); | |
| 568 | + Computed(kWord); | |
| 823 | 569 | end; |
| 824 | 570 | |
| 825 | -procedure Complement(var x: TItem); | |
| 571 | +{ The integer on top, as a real of kind k; big if it may be a U64 above | |
| 572 | + high(I64), which the machine would take as negative } | |
| 573 | +procedure IntToReal(k: TKind; big: boolean); | |
| 574 | +var L1, L2: longint; f: boolean; | |
| 826 | 575 | begin |
| 827 | - if x.typ^.form = fBool then MarkHelp(x.pos, '"~" needs an integer, found ' + What(x), 'a BOOL is negated with not'); | |
| 828 | - if not IsInt(x.typ) then MarkAt(x.pos, '"~" needs an integer, found ' + What(x)); | |
| 829 | - if x.mode = mConst then x.val := Cut(not x.val, x.typ) | |
| 830 | - else begin Value(x); Unary(2, RAX); Norm(x.typ) end; | |
| 576 | + f := k = kF32; | |
| 577 | + PopTo(RAX); | |
| 578 | + if big then begin | |
| 579 | + { above high(I64): halve, keeping the low bit, and double } | |
| 580 | + L1 := NoLabel; L2 := NoLabel; | |
| 581 | + TestRR(RAX, RAX); | |
| 582 | + Jcc(ccS, L1); | |
| 583 | + Cvtsi2s(0, RAX, f); | |
| 584 | + Jmp(L2); | |
| 585 | + FixLink(L1); | |
| 586 | + MovRR(RCX, RAX); | |
| 587 | + ShiftI(shShr, RCX, 1); | |
| 588 | + AluI(aluAnd, RAX, 1); | |
| 589 | + Alu(aluOr, RCX, RAX); | |
| 590 | + Cvtsi2s(0, RCX, f); | |
| 591 | + Sse($58, 0, 0, f); | |
| 592 | + FixLink(L2); | |
| 593 | + end else Cvtsi2s(0, RAX, f); | |
| 594 | + MovqRX(RAX, 0); | |
| 595 | + Computed(k); | |
| 831 | 596 | end; |
| 832 | 597 | |
| 833 | -{ Arrays, slices and strings, which = and # compare element by element } | |
| 834 | -function IsSeq(t: PType): boolean; | |
| 598 | +{ The real of kind k on top, truncated to an integer } | |
| 599 | +procedure RealToInt(k: TKind); | |
| 835 | 600 | begin |
| 836 | - IsSeq := t^.form in [fStr, fArray, fSlice]; | |
| 601 | + PopTo(RAX); | |
| 602 | + MovqXR(0, RAX); | |
| 603 | + Cvtts2si(RAX, 0, k = kF32); | |
| 604 | + Computed(kWord); | |
| 837 | 605 | end; |
| 838 | 606 | |
| 839 | -function ElemOf(t: PType): PType; | |
| 607 | +{ The real of kind k on top, as the other kind } | |
| 608 | +procedure RealToReal(k: TKind); | |
| 840 | 609 | begin |
| 841 | - if t^.form = fStr then ElemOf := tU8 else ElemOf := t^.base; | |
| 610 | + PopTo(RAX); | |
| 611 | + MovqXR(0, RAX); | |
| 612 | + Sse($5A, 0, 0, k = kF32); | |
| 613 | + MovqRX(RAX, 0); | |
| 614 | + if k = kF32 then Computed(kF64) else Computed(kF32); | |
| 842 | 615 | end; |
| 843 | 616 | |
| 844 | -{ An array, slice or string as a slice in a temporary, for comparing. } | |
| 845 | -procedure SeqSlice(var x: TItem); | |
| 846 | -var t: PType; | |
| 617 | +{ Whether the slices in the frame at a and b are equal (or not, if not | |
| 618 | + eq): the lengths, then the bytes, len shl shift of them, compared | |
| 619 | + right where the comparison stands. } | |
| 620 | +procedure CompareSeq(a, b: int64; shift: longint; eq: boolean); | |
| 621 | +var differ: longint; | |
| 847 | 622 | begin |
| 848 | - t := NewType(fSlice, 16, 8); | |
| 849 | - t^.base := ElemOf(x.typ); | |
| 850 | - if x.typ^.form = fSlice then begin | |
| 851 | - x.typ := t; CopyToTemp(x) | |
| 852 | - end else begin | |
| 853 | - if x.mode = mStr then begin Address(x, RAX); SliceOf(x, t, x.val) end | |
| 854 | - else begin | |
| 855 | - if x.vol then MarkAt(x.pos, 'can''t compare through a volatile pointer'); | |
| 856 | - Address(x, RAX); SliceOf(x, t, x.typ^.len) | |
| 857 | - end; | |
| 858 | - end; | |
| 623 | + Spill; | |
| 624 | + Load(RCX, Mem(RBP, b + 8), 8, false); | |
| 625 | + AluM(aluCmp, RCX, Mem(RBP, a + 8)); | |
| 626 | + differ := NoLabel; | |
| 627 | + Jcc(ccNE, differ); | |
| 628 | + Load(RSI, Mem(RBP, b), 8, false); | |
| 629 | + Load(RDI, Mem(RBP, a), 8, false); | |
| 630 | + if shift > 0 then ShiftI(shShl, RCX, shift); | |
| 631 | + Alu(aluCmp, RCX, RCX); { equal, if there are no bytes } | |
| 632 | + RepeCmpsb; | |
| 633 | + FixLink(differ); | |
| 634 | + if eq then SetCC(ccE, RAX) else SetCC(ccNE, RAX); | |
| 635 | + Computed(kWord); | |
| 859 | 636 | end; |
| 860 | 637 | |
| 861 | -procedure Op1(op: TSym; var x: TItem; var L: longint); | |
| 862 | -var tmp: TItem; | |
| 638 | +{ An error is the address of its length, followed by its message: pops | |
| 639 | + one, and makes the slice of its message at tmp, empty for nil. } | |
| 640 | +procedure ErrorText(tmp: int64); | |
| 641 | +var L: longint; | |
| 863 | 642 | begin |
| 643 | + PopTo(RAX); | |
| 644 | + MovRI(RCX, 0); | |
| 864 | 645 | L := NoLabel; |
| 865 | - if (op in [sAnd, sOr]) and (x.typ^.form = fBool) then begin | |
| 866 | - tmp := x; | |
| 867 | - Value(tmp); | |
| 868 | - if x.mode <> mConst then x := tmp; | |
| 869 | - TestRR(RAX, RAX); | |
| 870 | - if op = sAnd then Jcc(ccE, L) else Jcc(ccNE, L); | |
| 871 | - end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then | |
| 872 | - SeqSlice(x) | |
| 873 | - else if (op = sPlus) and (x.mode = mStr) then | |
| 874 | - { two string constants to be joined: nothing to compute } | |
| 875 | - else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then | |
| 876 | - NotValue(x) | |
| 877 | - else if x.mode <> mConst then begin | |
| 878 | - Value(x); PushRax; x.mode := mStack | |
| 879 | - end; | |
| 646 | + TestRR(RAX, RAX); | |
| 647 | + Jcc(ccE, L); | |
| 648 | + Load(RCX, Mem(RAX, 0), 8, false); | |
| 649 | + Lea(RAX, Mem(RAX, 8)); | |
| 650 | + FixLink(L); | |
| 651 | + Store(Mem(RBP, tmp), RAX, 8); | |
| 652 | + Store(Mem(RBP, tmp + 8), RCX, 8); | |
| 880 | 653 | end; |
| 881 | 654 | |
| 882 | -function Log2(v: int64): longint; | |
| 883 | -var n: longint; | |
| 655 | +{ Pops a step and an integer variable's address, and adds the step to | |
| 656 | + it, or takes it away. } | |
| 657 | +procedure IncDec(size: longint; signed, dec: boolean); | |
| 884 | 658 | begin |
| 885 | - n := 0; | |
| 886 | - while (v > 1) and (v and 1 = 0) do begin v := v shr 1; inc(n) end; | |
| 887 | - if v <> 1 then n := -1; | |
| 888 | - Log2 := n; | |
| 659 | + PopTo(RCX); | |
| 660 | + PopTo(RDX); | |
| 661 | + Spill; | |
| 662 | + Load(RAX, Mem(RDX, 0), size, signed); | |
| 663 | + if dec then Alu(aluSub, RAX, RCX) else Alu(aluAdd, RAX, RCX); | |
| 664 | + Store(Mem(RDX, 0), RAX, size); | |
| 889 | 665 | end; |
| 890 | 666 | |
| 891 | -{ rax div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards | |
| 892 | - zero, so a negative dividend first gets 2^k - 1 added (the bias). } | |
| 893 | -procedure DivPow2(op: TSym; k: longint; unsign: boolean); | |
| 667 | +{ The pointer on top, which NEW got, to size zero bytes unless nil } | |
| 668 | +procedure ZeroNew(size: int64); | |
| 669 | +var L: longint; | |
| 894 | 670 | begin |
| 895 | - if unsign then begin | |
| 896 | - if op = sDiv then ShiftI(shShr, RAX, k) | |
| 897 | - else begin ShiftI(shShl, RAX, 64 - k); ShiftI(shShr, RAX, 64 - k) end; | |
| 898 | - exit; | |
| 899 | - end; | |
| 900 | - MovRR(RCX, RAX); | |
| 901 | - ShiftI(shSar, RCX, 63); | |
| 902 | - ShiftI(shShr, RCX, 64 - k); | |
| 903 | - Alu(aluAdd, RCX, RAX); | |
| 904 | - ShiftI(shSar, RCX, k); | |
| 905 | - if op = sDiv then MovRR(RAX, RCX) | |
| 906 | - else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end; | |
| 671 | + PopTo(RAX); | |
| 672 | + L := NoLabel; | |
| 673 | + TestRR(RAX, RAX); | |
| 674 | + Jcc(ccE, L); | |
| 675 | + MovRR(RDX, RAX); | |
| 676 | + MovRR(RDI, RAX); | |
| 677 | + MovRI(RCX, size); | |
| 678 | + MovRI(RAX, 0); | |
| 679 | + RepStosb; | |
| 680 | + MovRR(RAX, RDX); | |
| 681 | + FixLink(L); | |
| 682 | + Computed(kWord); | |
| 907 | 683 | end; |
| 908 | 684 | |
| 909 | -{ Whether a op b, for + - *, overflows an I64: an untyped constant | |
| 910 | - doesn't wrap, it is an error (report section 4). } | |
| 911 | -function Overflows(op: TSym; a, b: int64): boolean; | |
| 685 | +{ CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack } | |
| 686 | +procedure Atomic(which: longint); | |
| 912 | 687 | begin |
| 913 | - case op of | |
| 914 | - sPlus: Overflows := ((b > 0) and (a > high(int64) - b)) or ((b < 0) and (a < low(int64) - b)); | |
| 915 | - sMinus: Overflows := ((b < 0) and (a > high(int64) + b)) or ((b > 0) and (a < low(int64) + b)); | |
| 916 | - sTimes: | |
| 917 | - if (a = 0) or (b = 0) then Overflows := false | |
| 918 | - else if a = -1 then Overflows := b = low(int64) | |
| 919 | - else if b = -1 then Overflows := a = low(int64) | |
| 920 | - else Overflows := (a * b) div b <> a; | |
| 921 | - else Overflows := false; | |
| 688 | + if which = bCas then PopTo(RDX); | |
| 689 | + PopTo(RAX); | |
| 690 | + PopTo(RCX); | |
| 691 | + case which of | |
| 692 | + bCas: begin LockCmpxchg(Mem(RCX, 0), RDX); SetCC(ccE, RAX) end; | |
| 693 | + bXchg: Xchg(Mem(RCX, 0), RAX); | |
| 694 | + bXadd: LockXadd(Mem(RCX, 0), RAX); | |
| 922 | 695 | end; |
| 696 | + Computed(kWord); | |
| 923 | 697 | end; |
| 924 | 698 | |
| 925 | -{ A type's width in bits; an untyped constant's is an I64's } | |
| 926 | -function Bits(t: PType): longint; | |
| 699 | +{ The system call and its arguments, n values on the stack } | |
| 700 | +procedure SysCall(n: longint); | |
| 701 | +const Regs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); | |
| 702 | +var i: longint; | |
| 927 | 703 | begin |
| 928 | - Bits := t^.size * 8; | |
| 704 | + for i := n - 1 downto 0 do PopTo(Regs[i]); | |
| 705 | + x64.Syscall; | |
| 706 | + Computed(kWord); | |
| 929 | 707 | end; |
| 930 | 708 | |
| 931 | -function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64; | |
| 932 | -var u: boolean; | |
| 709 | +procedure Fence; | |
| 933 | 710 | begin |
| 934 | - u := (t^.form = fInt) and not t^.signed; | |
| 935 | - if (t^.form = fConst) and Overflows(op, a, b) then MarkAt(p, 'the constant is too big for I64'); | |
| 936 | - case op of | |
| 937 | - sPlus: FoldInt := a + b; | |
| 938 | - sMinus: FoldInt := a - b; | |
| 939 | - sTimes: FoldInt := a * b; | |
| 940 | - sDiv, sMod: begin | |
| 941 | - if b = 0 then MarkAt(p, 'division by zero'); | |
| 942 | - if u then begin | |
| 943 | - if op = sDiv then FoldInt := int64(qword(a) div qword(b)) | |
| 944 | - else FoldInt := int64(qword(a) mod qword(b)); | |
| 945 | - end else if (b = -1) and (a = int64(-1) shl (Bits(t) - 1)) then MarkAt(p, 'division overflows') | |
| 946 | - else if op = sDiv then FoldInt := a div b | |
| 947 | - else FoldInt := a mod b; | |
| 948 | - end; | |
| 949 | - sAmp: FoldInt := a and b; | |
| 950 | - sBar: FoldInt := a or b; | |
| 951 | - sXor: FoldInt := a xor b; | |
| 952 | - sShl: FoldInt := a shl (b and (Bits(t) - 1)); | |
| 953 | - sShr: if u then FoldInt := int64(qword(a) shr (b and (Bits(t) - 1))) | |
| 954 | - else FoldInt := SarInt64(a, b and (Bits(t) - 1)); | |
| 955 | - sEql: FoldInt := ord(a = b); | |
| 956 | - sNeq: FoldInt := ord(a <> b); | |
| 957 | - sLss: if u then FoldInt := ord(qword(a) < qword(b)) else FoldInt := ord(a < b); | |
| 958 | - sLeq: if u then FoldInt := ord(qword(a) <= qword(b)) else FoldInt := ord(a <= b); | |
| 959 | - sGtr: if u then FoldInt := ord(qword(a) > qword(b)) else FoldInt := ord(a > b); | |
| 960 | - sGeq: if u then FoldInt := ord(qword(a) >= qword(b)) else FoldInt := ord(a >= b); | |
| 961 | - else MarkAt(p, 'bad operator'); FoldInt := 0; | |
| 962 | - end; | |
| 711 | + Mfence; | |
| 963 | 712 | end; |
| 964 | 713 | |
| 965 | -{ Folds x op y, of the real type t: an F32 rounded at each step, as | |
| 966 | - the machine would. } | |
| 967 | -function FoldReal(op: TSym; const x, y: TItem; t: PType; var r: TItem): boolean; | |
| 968 | -var a, b: double; | |
| 714 | +{ Jumps. A label is the chain of the jumps to it, until it is placed. } | |
| 715 | + | |
| 716 | +procedure Jump(var L: longint); | |
| 969 | 717 | begin |
| 970 | - a := RealOf(x); b := RealOf(y); | |
| 971 | - FoldReal := true; | |
| 972 | - case op of | |
| 973 | - sPlus: MakeReal(r, a + b); | |
| 974 | - sMinus: MakeReal(r, a - b); | |
| 975 | - sTimes: MakeReal(r, a * b); | |
| 976 | - sSlash: MakeReal(r, a / b); | |
| 977 | - sEql: MakeConst(r, tBool, ord(a = b)); | |
| 978 | - sNeq: MakeConst(r, tBool, ord(a <> b)); | |
| 979 | - sLss: MakeConst(r, tBool, ord(a < b)); | |
| 980 | - sLeq: MakeConst(r, tBool, ord(a <= b)); | |
| 981 | - sGtr: MakeConst(r, tBool, ord(a > b)); | |
| 982 | - sGeq: MakeConst(r, tBool, ord(a >= b)); | |
| 983 | - else FoldReal := false; | |
| 984 | - end; | |
| 985 | - if r.typ^.form = fReal then RealTo(r, t); | |
| 718 | + Jmp(L); | |
| 986 | 719 | end; |
| 987 | 720 | |
| 988 | -{ and, or and not are for BOOLs; &, |, xor and ~ for the bits of | |
| 989 | - integers: x is the wrong kind for op, and the message names the | |
| 990 | - operator it may have meant } | |
| 991 | -{ Before rax div rcx, of a signed type narrower than 64 bits: its | |
| 992 | - smallest value divided by -1 overflows it, which must stop the program | |
| 993 | - as it does for an I64 (report section 6.1). idiv on the 64-bit values | |
| 994 | - wouldn't fault, so then rax is made the smallest I64, and it does. } | |
| 995 | -procedure DivTrap(t: PType); | |
| 996 | -var L: longint; | |
| 721 | +{ Pops a BOOL, and jumps if it is when } | |
| 722 | +procedure JumpIf(var L: longint; when: boolean); | |
| 997 | 723 | begin |
| 998 | - L := NoLabel; | |
| 999 | - CmpI(RCX, -1); | |
| 1000 | - Jcc(ccNE, L); | |
| 1001 | - CmpI(RAX, int64(-1) shl (Bits(t) - 1)); | |
| 1002 | - Jcc(ccNE, L); | |
| 1003 | - ShiftI(shShl, RAX, 64 - Bits(t)); | |
| 1004 | - FixLink(L); | |
| 724 | + PopTo(RAX); | |
| 725 | + TestRR(RAX, RAX); | |
| 726 | + if when then Jcc(ccNE, L) else Jcc(ccE, L); | |
| 1005 | 727 | end; |
| 1006 | 728 | |
| 1007 | -procedure WrongLogic(op: TSym; var x: TItem); | |
| 1008 | -const Bits = 'the bits of integers take '; | |
| 729 | +{ For and and or: jumps if the BOOL on top is when, keeping it for | |
| 730 | + where the jump goes, and drops it if not. } | |
| 731 | +procedure JumpKeep(var L: longint; when: boolean); | |
| 1009 | 732 | begin |
| 1010 | - case op of | |
| 1011 | - sAnd: if IsInt(x.typ) then MarkHelp(x.pos, '"and" needs BOOLs, found ' + What(x), Bits + '&'); | |
| 1012 | - sOr: if IsInt(x.typ) then MarkHelp(x.pos, '"or" needs BOOLs, found ' + What(x), Bits + '|'); | |
| 1013 | - sAmp: MarkHelp(x.pos, '"&" needs integers, found ' + What(x), 'BOOLs take and'); | |
| 1014 | - sBar: MarkHelp(x.pos, '"|" needs integers, found ' + What(x), 'BOOLs take or'); | |
| 1015 | - sXor: MarkHelp(x.pos, '"xor" needs integers, found ' + What(x), 'BOOLs that differ are #'); | |
| 1016 | - end; | |
| 1017 | - MarkAt(x.pos, SymText(op) + ' needs BOOLs, found ' + What(x)); | |
| 733 | + JumpIf(L, when); | |
| 1018 | 734 | end; |
| 1019 | 735 | |
| 1020 | -{ x + y, of two string constants: one string, in a chunk of its own | |
| 1021 | - (report section 4) } | |
| 1022 | -procedure JoinStr(var x, y: TItem); | |
| 1023 | -var p: PBytes; c: longint; | |
| 736 | +{ Where JumpKeep went: the BOOL on top is there, as it was there. } | |
| 737 | +procedure FixKeep(L: longint); | |
| 1024 | 738 | begin |
| 1025 | - if y.mode <> mStr then MarkAt(y.pos, '"+" joins two strings, found ' + What(y)); | |
| 1026 | - p := GetMem(x.val + y.val + 1); | |
| 1027 | - Move(rodata^[chunks^[x.chunk].start], p^[0], x.val); | |
| 1028 | - Move(rodata^[chunks^[y.chunk].start], p^[x.val], y.val + 1); | |
| 1029 | - c := NewChunk(secRodata, -1, -1); | |
| 1030 | - SetData(c, p, x.val + y.val + 1, 1); | |
| 1031 | - FreeMem(p); | |
| 1032 | - x.chunk := c; | |
| 1033 | - x.val := x.val + y.val; | |
| 739 | + TopToRax; | |
| 740 | + FixLink(L); | |
| 1034 | 741 | end; |
| 1035 | 742 | |
| 1036 | -procedure Op2(op: TSym; var x, y: TItem; L: longint); | |
| 743 | +{ Pops two integers, and jumps if the left op the right } | |
| 744 | +procedure JumpCmp(op: TSym; signed: boolean; var L: longint); | |
| 1037 | 745 | const |
| 1038 | - Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq]; | |
| 1039 | 746 | CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); |
| 1040 | 747 | CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); |
| 1041 | -var | |
| 1042 | - xt, yt, t, e: PType; r: TItem; scale, sh, k, differ: longint; isPtr, unsign, f: boolean; a, b: int64; | |
| 748 | +var y: TEntry; | |
| 1043 | 749 | begin |
| 1044 | - xt := x.typ; yt := y.typ; | |
| 1045 | - if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y); | |
| 1046 | - if (op = sPlus) and (x.mode = mStr) then begin JoinStr(x, y); exit end; | |
| 1047 | - | |
| 1048 | - if op in [sAnd, sOr] then begin | |
| 1049 | - if xt^.form <> fBool then WrongLogic(op, x); | |
| 1050 | - if yt^.form <> fBool then WrongLogic(op, y); | |
| 1051 | - if (x.mode = mConst) and (y.mode = mConst) then begin | |
| 1052 | - FixLink(L); | |
| 1053 | - if op = sAnd then x.val := x.val and y.val else x.val := x.val or y.val; | |
| 1054 | - exit; | |
| 1055 | - end; | |
| 1056 | - Value(y); | |
| 1057 | - FixLink(L); | |
| 1058 | - x.mode := mReg; x.typ := tBool; | |
| 1059 | - exit; | |
| 750 | + y := stack[depth - 1]; | |
| 751 | + if y.isConst then begin | |
| 752 | + dec(depth); | |
| 753 | + PopTo(RAX); | |
| 754 | + CmpI(RAX, y.val); | |
| 755 | + end else begin | |
| 756 | + Operands; | |
| 757 | + Alu(aluCmp, RAX, RCX); | |
| 1060 | 758 | end; |
| 759 | + if signed then Jcc(CcSigned[op], L) else Jcc(CcUnsigned[op], L); | |
| 760 | +end; | |
| 1061 | 761 | |
| 1062 | - if (op in [sAmp, sBar, sXor]) and (xt^.form = fBool) then WrongLogic(op, x); | |
| 1063 | - if (op in [sAmp, sBar, sXor]) and (yt^.form = fBool) then WrongLogic(op, y); | |
| 762 | +procedure JumpBack(top: longint); | |
| 763 | +begin | |
| 764 | + JmpBack(top); | |
| 765 | +end; | |
| 1064 | 766 | |
| 1065 | - { the lengths, then the bytes, LEN * SIZE(T) of them: the elements | |
| 1066 | - are of types whose bytes say all there is to their values } | |
| 1067 | - if (op in [sEql, sNeq]) and IsSeq(xt) then begin | |
| 1068 | - e := ElemOf(xt); | |
| 1069 | - if not IsSeq(yt) then | |
| 1070 | - MarkAt(Join(x.pos, y.pos), 'can''t compare an array with ' + TypeName(yt)) | |
| 1071 | - else if not Same(e, ElemOf(yt)) then | |
| 1072 | - MarkAt(Join(x.pos, y.pos), 'can''t compare an array of ' + TypeName(e) + ' with one of ' + TypeName(ElemOf(yt))); | |
| 1073 | - if e^.form = fReal then | |
| 1074 | - MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of reals with ' + SymText(op), | |
| 1075 | - '-0.0 = 0.0 and a NaN equals nothing, though their bytes say otherwise: write a loop') | |
| 1076 | - else if not (e^.form in [fInt, fBool, fEnum, fSet, fPtr]) then | |
| 1077 | - MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of ' + TypeName(e) + ' with ' + SymText(op), 'write a loop'); | |
| 1078 | - SeqSlice(y); | |
| 1079 | - Address(y, RSI); | |
| 1080 | - Address(x, RDI); | |
| 1081 | - Load(RCX, Mem(RSI, 8), 8, false); | |
| 1082 | - AluM(aluCmp, RCX, Mem(RDI, 8)); | |
| 1083 | - differ := NoLabel; | |
| 1084 | - Jcc(ccNE, differ); | |
| 1085 | - Load(RSI, Mem(RSI, 0), 8, false); | |
| 1086 | - Load(RDI, Mem(RDI, 0), 8, false); | |
| 1087 | - if e^.size > 1 then ShiftI(shShl, RCX, Log2(e^.size)); | |
| 1088 | - Alu(aluCmp, RCX, RCX); { equal, if there are no bytes } | |
| 1089 | - RepeCmpsb; | |
| 1090 | - FixLink(differ); | |
| 1091 | - if op = sEql then SetCC(ccE, RAX) else SetCC(ccNE, RAX); | |
| 1092 | - MakeConst(x, tBool, 0); | |
| 1093 | - x.mode := mReg; | |
| 1094 | - exit; | |
| 1095 | - end; | |
| 767 | +procedure Fix(L: longint); | |
| 768 | +begin | |
| 769 | + FixLink(L); | |
| 770 | +end; | |
| 1096 | 771 | |
| 1097 | - { the type of the result, and the checks } | |
| 1098 | - t := nil; scale := 1; isPtr := false; | |
| 1099 | - if IsInt(xt) and IsInt(yt) then begin | |
| 1100 | - if op = sSlash then MarkHelp(Join(x.pos, y.pos), '"/" divides reals, not integers', 'to divide integers, write ' + Source(x.pos) + ' div ' + Source(y.pos)); | |
| 1101 | - if op in [sShl, sShr] then begin | |
| 1102 | - if xt^.form = fConst then t := tConst else t := xt | |
| 1103 | - end else if (xt^.form = fConst) and (yt^.form = fConst) then t := tConst | |
| 1104 | - else if xt^.form = fConst then begin | |
| 1105 | - if (x.mode = mConst) and not Fits(x.val, yt) then MarkAt(x.pos, IntStr(x.val) + ' doesn''t fit ' + TypeName(yt)); | |
| 1106 | - t := yt | |
| 1107 | - end else if yt^.form = fConst then begin | |
| 1108 | - if (y.mode = mConst) and not Fits(y.val, xt) then MarkAt(y.pos, IntStr(y.val) + ' doesn''t fit ' + TypeName(xt)); | |
| 1109 | - t := xt | |
| 1110 | - end else if Same(xt, yt) then t := xt | |
| 1111 | - else Mismatch(x, y, xt, yt); | |
| 1112 | - end else if (xt^.form = fReal) and (yt^.form = fReal) then begin | |
| 1113 | - if not (op in Rel + [sPlus, sMinus, sTimes, sSlash]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1114 | - if xt = tRConst then t := yt | |
| 1115 | - else if (yt = tRConst) or Same(xt, yt) then t := xt | |
| 1116 | - else Mismatch(x, y, xt, yt); | |
| 1117 | - if (x.mode = mConst) and (x.typ <> t) then RealTo(x, t); | |
| 1118 | - if (y.mode = mConst) and (y.typ <> t) then RealTo(y, t); | |
| 1119 | - end else if (xt^.form = fBool) and (yt^.form = fBool) then begin | |
| 1120 | - if not (op in [sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1121 | - t := tBool; | |
| 1122 | - end else if (xt^.form = fPtr) and IsInt(yt) and (op in [sPlus, sMinus]) then begin | |
| 1123 | - t := xt; scale := xt^.base^.size; isPtr := true; | |
| 1124 | - if y.mode = mConst then y.typ := tInt; | |
| 1125 | - end else if (xt^.form = fPtr) and (yt^.form = fPtr) and (op = sMinus) then begin | |
| 1126 | - if not Same(xt, yt) then Mismatch(x, y, xt, yt); | |
| 1127 | - t := tInt; scale := xt^.base^.size; isPtr := true; | |
| 1128 | - end else if (xt^.form in [fPtr, fProc, fNil, fError]) and (yt^.form in [fPtr, fProc, fNil, fError]) then begin | |
| 1129 | - if not (op in Rel) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1130 | - if (xt^.form in [fProc, fError]) or (yt^.form in [fProc, fError]) then | |
| 1131 | - if not (op in [sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1132 | - if (xt^.form <> fNil) and (yt^.form <> fNil) and not Same(xt, yt) then | |
| 1133 | - if not ((xt^.form = fPtr) and (yt^.form = fPtr) and Same(xt^.base, yt^.base)) then | |
| 1134 | - Mismatch(x, y, xt, yt); | |
| 1135 | - t := tU64; | |
| 1136 | - end else if (xt^.form = fEnum) and Same(xt, yt) then begin | |
| 1137 | - if not (op in Rel) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1138 | - t := xt; | |
| 1139 | - end else if (xt^.form = fSet) and Same(xt, yt) then begin | |
| 1140 | - if not (op in [sPlus, sTimes, sMinus, sEql, sNeq, sLeq, sGeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1141 | - t := xt; | |
| 1142 | - end else if (op = sIn) and (yt^.form = fSet) and Same(xt, yt^.base) then | |
| 1143 | - t := yt | |
| 1144 | - else | |
| 1145 | - Mismatch(x, y, xt, yt); | |
| 1146 | - unsign := (t^.form = fInt) and not t^.signed; | |
| 772 | +procedure FixTo(L, top: longint); | |
| 773 | +begin | |
| 774 | + FixLinkTo(L, top); | |
| 775 | +end; | |
| 1147 | 776 | |
| 1148 | - { constants } | |
| 1149 | - if (x.mode = mConst) and (y.mode = mConst) and not isPtr then begin | |
| 1150 | - if t^.form = fReal then begin | |
| 1151 | - if not FoldReal(op, x, y, t, r) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 1152 | - x := r; | |
| 1153 | - end else if t^.form = fSet then begin | |
| 1154 | - a := x.val; b := y.val; | |
| 1155 | - case op of | |
| 1156 | - sPlus: MakeConst(x, t, a or b); | |
| 1157 | - sTimes: MakeConst(x, t, a and b); | |
| 1158 | - sMinus: MakeConst(x, t, a and not b); | |
| 1159 | - sIn: MakeConst(x, tBool, (b shr a) and 1); | |
| 1160 | - sLeq: MakeConst(x, tBool, ord(a and not b = 0)); | |
| 1161 | - sGeq: MakeConst(x, tBool, ord(b and not a = 0)); | |
| 1162 | - else MakeConst(x, tBool, ord((a = b) = (op = sEql))); | |
| 1163 | - end; | |
| 1164 | - end else if op in Rel then | |
| 1165 | - MakeConst(x, tBool, FoldInt(op, x.val, y.val, t, Join(x.pos, y.pos))) | |
| 1166 | - else | |
| 1167 | - MakeConst(x, t, Cut(FoldInt(op, x.val, y.val, t, Join(x.pos, y.pos)), t)); | |
| 1168 | - exit; | |
| 1169 | - end; | |
| 777 | +{ Checks } | |
| 1170 | 778 | |
| 1171 | - { a power of two on the right: shifts instead of imul and idiv } | |
| 1172 | - k := -1; | |
| 1173 | - if (y.mode = mConst) and (op in [sTimes, sDiv, sMod]) and (t^.form = fInt) then k := Log2(y.val); | |
| 779 | +const | |
| 780 | + NoBase = -2; { a TMem that is no memory } | |
| 1174 | 781 | |
| 1175 | - { x into rax, y into rcx; a left operand just pushed stays in rax when | |
| 1176 | - the right one needs no code of its own } | |
| 1177 | - if y.mode = mConst then begin | |
| 1178 | - if x.mode <> mStack then Value(x) | |
| 1179 | - else if not Unpush then PopR(RAX); | |
| 1180 | - if k < 1 then ConstTo(RCX, y); | |
| 1181 | - end else if (x.mode = mStack) and (y.mode = mVar) and (y.base in [bFrame, bGlobal]) | |
| 1182 | - and Unpush then | |
| 1183 | - LoadVar(y, RCX) | |
| 1184 | - else begin | |
| 1185 | - Value(y); | |
| 1186 | - MovRR(RCX, RAX); | |
| 1187 | - if x.mode = mConst then ConstTo(RAX, x) else PopR(RAX); | |
| 1188 | - end; | |
| 1189 | - | |
| 1190 | - if t^.form = fReal then begin | |
| 1191 | - MovqXR(0, RAX); | |
| 1192 | - MovqXR(1, RCX); | |
| 1193 | - f := IsF32(t); | |
| 1194 | - case op of | |
| 1195 | - sPlus: Sse($58, 0, 1, f); | |
| 1196 | - sMinus: Sse($5C, 0, 1, f); | |
| 1197 | - sTimes: Sse($59, 0, 1, f); | |
| 1198 | - sSlash: Sse($5E, 0, 1, f); | |
| 1199 | - sEql: begin Ucomis(0, 1, f); SetCC(ccE, RAX); SetCC(ccNP, RCX); Alu(aluAnd, RAX, RCX) end; | |
| 1200 | - sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, RAX); SetCC(ccP, RCX); Alu(aluOr, RAX, RCX) end; | |
| 1201 | - sLss: begin Ucomis(1, 0, f); SetCC(ccA, RAX) end; | |
| 1202 | - sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, RAX) end; | |
| 1203 | - sGtr: begin Ucomis(0, 1, f); SetCC(ccA, RAX) end; | |
| 1204 | - sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, RAX) end; | |
| 1205 | - end; | |
| 1206 | - if not (op in Rel) then MovqRX(RAX, 0); | |
| 1207 | - end else if t^.form = fSet then begin | |
| 1208 | - case op of | |
| 1209 | - sPlus: Alu(aluOr, RAX, RCX); | |
| 1210 | - sTimes: Alu(aluAnd, RAX, RCX); | |
| 1211 | - sMinus: begin Unary(2, RCX); Alu(aluAnd, RAX, RCX) end; | |
| 1212 | - sLeq, sGeq: begin | |
| 1213 | - if op = sLeq then Unary(2, RCX) else Unary(2, RAX); | |
| 1214 | - Alu(aluAnd, RAX, RCX); | |
| 1215 | - TestRR(RAX, RAX); | |
| 1216 | - SetCC(ccE, RAX); | |
| 1217 | - end; | |
| 1218 | - sIn: begin | |
| 1219 | - MovRR(RDX, RCX); MovRR(RCX, RAX); MovRR(RAX, RDX); | |
| 1220 | - Shift(shShr, RAX); | |
| 1221 | - AluI(aluAnd, RAX, 1); | |
| 1222 | - end; | |
| 1223 | - else Alu(aluCmp, RAX, RCX); SetCC(CcUnsigned[op], RAX); | |
| 1224 | - end; | |
| 1225 | - end else if op in Rel then begin | |
| 1226 | - Alu(aluCmp, RAX, RCX); | |
| 1227 | - if (t^.form = fInt) and t^.signed or (t^.form = fConst) then SetCC(CcSigned[op], RAX) | |
| 1228 | - else SetCC(CcUnsigned[op], RAX); | |
| 1229 | - end else begin | |
| 1230 | - case op of | |
| 1231 | - sPlus: begin if scale <> 1 then ImulI(RCX, scale); Alu(aluAdd, RAX, RCX) end; | |
| 1232 | - sMinus: begin | |
| 1233 | - if (scale <> 1) and (t <> tInt) then ImulI(RCX, scale); | |
| 1234 | - Alu(aluSub, RAX, RCX); | |
| 1235 | - if (t = tInt) and isPtr and (scale <> 1) then begin | |
| 1236 | - sh := Log2(scale); | |
| 1237 | - if sh >= 0 then ShiftI(shSar, RAX, sh) | |
| 1238 | - else begin MovRI(RCX, scale); Cqo; Unary(7, RCX) end; | |
| 1239 | - end; | |
| 1240 | - end; | |
| 1241 | - sTimes: if k >= 1 then ShiftI(shShl, RAX, k) else Imul(RAX, RCX); | |
| 1242 | - sDiv, sMod: if k >= 1 then DivPow2(op, k, unsign) else begin | |
| 1243 | - if unsign then begin MovRI(RDX, 0); Unary(6, RCX) end | |
| 1244 | - else begin | |
| 1245 | - if (t^.size < 8) and ((y.mode <> mConst) or (y.val = -1)) then DivTrap(t); | |
| 1246 | - Cqo; Unary(7, RCX) | |
| 1247 | - end; | |
| 1248 | - if op = sMod then MovRR(RAX, RDX); | |
| 1249 | - end; | |
| 1250 | - sAmp: Alu(aluAnd, RAX, RCX); | |
| 1251 | - sBar: Alu(aluOr, RAX, RCX); | |
| 1252 | - sXor: Alu(aluXor, RAX, RCX); | |
| 1253 | - sShl, sShr: begin | |
| 1254 | - { the count modulo the width: the machine takes it modulo 64 } | |
| 1255 | - if Bits(t) < 64 then AluI(aluAnd, RCX, Bits(t) - 1); | |
| 1256 | - if op = sShl then Shift(shShl, RAX) | |
| 1257 | - else if unsign then Shift(shShr, RAX) else Shift(shSar, RAX); | |
| 1258 | - end; | |
| 1259 | - else MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(t)); | |
| 1260 | - end; | |
| 1261 | - Norm(t); | |
| 1262 | - end; | |
| 1263 | - x.mode := mReg; | |
| 1264 | - if op in Rel + [sIn] then x.typ := tBool else x.typ := t; | |
| 782 | +{ The program goes on if the condition cc holds, and otherwise stops | |
| 783 | + in Rt.fail(what, a, b, len, at, name), which says what failed at p, | |
| 784 | + found by at, the address of the call: an index a (what = 0), a slice | |
| 785 | + a..b (1), each for a length len, a position a of the enumeration | |
| 786 | + called name (2), or a slice of a pointer a..b that runs backwards | |
| 787 | + (3). a is in register ra, b in RDX; len is in memory at m, or n if | |
| 788 | + m.base is NoBase. The failing code has p for its place, and the code | |
| 789 | + after it the place before it again. } | |
| 790 | +procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint); | |
| 791 | +var L, line, col: longint; before: TLine; | |
| 792 | +begin | |
| 793 | + L := NoLabel; | |
| 794 | + Jcc(cc, L); | |
| 795 | + before := lines^[numLines - 1]; | |
| 796 | + LineCol(p.a, line, col); | |
| 797 | + NoteLine(line, col); | |
| 798 | + MovRR(RSI, ra); | |
| 799 | + if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false); | |
| 800 | + Lea(R8, Here); | |
| 801 | + MovRI(RDI, what); | |
| 802 | + if name >= 0 then Lea(R9, MemRip(name, 0)); | |
| 803 | + AluI(aluAnd, RSP, -16); | |
| 804 | + CallChunk(rtFail); | |
| 805 | + Ud2; | |
| 806 | + FixLink(L); | |
| 807 | + NoteLine(before.line, before.col); | |
| 1265 | 808 | end; |
| 1266 | 809 | |
| 1267 | -{ INT and UINT, the integers as big as an address, through which | |
| 1268 | - integers and pointers convert (report section 6.7) } | |
| 1269 | -function IsWord(t: PType): boolean; | |
| 810 | +{ For Check: a length known here, not in memory } | |
| 811 | +function NoMem: TMem; | |
| 1270 | 812 | begin |
| 1271 | - IsWord := Same(t, tInt) or Same(t, tUInt); | |
| 813 | + NoMem := Mem(NoBase, 0); | |
| 1272 | 814 | end; |
| 1273 | 815 | |
| 1274 | -procedure Convert(var x: TItem; t: PType); | |
| 1275 | -var s: PType; r: double; L1, L2: longint; f: boolean; | |
| 816 | +{ The index on top is below n, unsigned } | |
| 817 | +procedure CheckIndex(n: int64; const p: TPos); | |
| 1276 | 818 | begin |
| 1277 | - s := x.typ; | |
| 1278 | - if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then begin | |
| 1279 | - if x.mode = mStr then MarkAt(x.pos, 'can''t convert a string'); | |
| 1280 | - NotValue(x); | |
| 1281 | - end; | |
| 1282 | - if Same(s, t) then exit; | |
| 1283 | - if (IsInt(s) or (s^.form in [fEnum, fSet])) and (t^.form = fInt) then begin | |
| 1284 | - if x.mode = mConst then x.val := Cut(x.val, t) | |
| 1285 | - else begin Value(x); Norm(t) end; | |
| 1286 | - end else if IsInt(s) and (t^.form = fEnum) then begin | |
| 1287 | - if x.mode = mConst then begin | |
| 1288 | - if (x.val < 0) or (x.val >= t^.len) then MarkAt(x.pos, IntStr(x.val) + ' isn''t a position of ' + TypeName(t)) | |
| 1289 | - end else begin Value(x); CmpI(RAX, t^.len); Check(ccB, 2, x.pos, RAX, NoMem, t^.len, NameChunk(t)) end; | |
| 1290 | - end else if IsInt(s) and (t^.form = fSet) then begin | |
| 1291 | - if x.mode = mConst then begin | |
| 1292 | - if t^.len < 64 then x.val := x.val and ((int64(1) shl t^.len) - 1) | |
| 1293 | - end else begin | |
| 1294 | - Value(x); | |
| 1295 | - if t^.len < 64 then begin MovRI(RCX, (int64(1) shl t^.len) - 1); Alu(aluAnd, RAX, RCX) end; | |
| 1296 | - end; | |
| 1297 | - end else if IsInt(s) and (t^.form = fReal) then begin | |
| 1298 | - f := IsF32(t); | |
| 1299 | - if x.mode = mConst then begin | |
| 1300 | - if (s^.form = fInt) and not s^.signed then r := qword(x.val) else r := x.val; | |
| 1301 | - MakeReal(x, r); | |
| 1302 | - RealTo(x, t); | |
| 1303 | - end else begin | |
| 1304 | - Value(x); | |
| 1305 | - if (s^.form = fInt) and not s^.signed and (s^.size = 8) then begin | |
| 1306 | - { above high(I64): halve, keeping the low bit, and double } | |
| 1307 | - L1 := NoLabel; L2 := NoLabel; | |
| 1308 | - TestRR(RAX, RAX); | |
| 1309 | - Jcc(ccS, L1); | |
| 1310 | - Cvtsi2s(0, RAX, f); | |
| 1311 | - Jmp(L2); | |
| 1312 | - FixLink(L1); | |
| 1313 | - MovRR(RCX, RAX); | |
| 1314 | - ShiftI(shShr, RCX, 1); | |
| 1315 | - AluI(aluAnd, RAX, 1); | |
| 1316 | - Alu(aluOr, RCX, RAX); | |
| 1317 | - Cvtsi2s(0, RCX, f); | |
| 1318 | - Sse($58, 0, 0, f); | |
| 1319 | - FixLink(L2); | |
| 1320 | - end else Cvtsi2s(0, RAX, f); | |
| 1321 | - MovqRX(RAX, 0); | |
| 1322 | - end; | |
| 1323 | - end else if (s^.form = fReal) and (t^.form = fReal) then begin | |
| 1324 | - { F32 to F64, F64 to F32, or a real constant to either } | |
| 1325 | - if x.mode = mConst then RealTo(x, t) | |
| 1326 | - else begin | |
| 1327 | - Value(x); MovqXR(0, RAX); Sse($5A, 0, 0, IsF32(s)); MovqRX(RAX, 0) | |
| 1328 | - end; | |
| 1329 | - end else if (s^.form = fReal) and (t^.form = fInt) then begin | |
| 1330 | - if x.mode = mConst then begin | |
| 1331 | - r := RealOf(x); | |
| 1332 | - if (r <> r) or (r >= 9.2233720368547758e18) or (r < -9.2233720368547758e18) then | |
| 1333 | - x.val := Cut(low(int64), t) | |
| 1334 | - else x.val := Cut(trunc(r), t); | |
| 1335 | - end else begin | |
| 1336 | - Value(x); MovqXR(0, RAX); Cvtts2si(RAX, 0, IsF32(s)); Norm(t) | |
| 1337 | - end; | |
| 1338 | - end else if (IsWord(s) or (s^.form in [fConst, fPtr, fProc, fNil])) and (t^.form in [fPtr, fProc]) then begin | |
| 1339 | - if x.mode <> mConst then Value(x); | |
| 1340 | - end else if (s^.form in [fPtr, fProc]) and IsWord(t) then | |
| 1341 | - Value(x) | |
| 1342 | - else | |
| 1343 | - MarkAt(x.pos, 'can''t convert ' + TypeName(s) + ' to ' + TypeName(t)); | |
| 1344 | - x.typ := t; | |
| 819 | + TopToRax; | |
| 820 | + CmpI(RAX, n); | |
| 821 | + Check(ccB, 0, p, RAX, NoMem, n, -1); | |
| 1345 | 822 | end; |
| 1346 | 823 | |
| 1347 | -{ Calls } | |
| 824 | +{ The integer on top is a position of an enumeration of n names } | |
| 825 | +procedure CheckEnum(n: int64; name: longint; const p: TPos); | |
| 826 | +begin | |
| 827 | + TopToRax; | |
| 828 | + CmpI(RAX, n); | |
| 829 | + Check(ccB, 2, p, RAX, NoMem, n, name); | |
| 830 | +end; | |
| 1348 | 831 | |
| 1349 | -procedure BeginCall(var c: TCall; var x: TItem); | |
| 832 | +{ Pops an index, and the address of the slice at a if it is on the | |
| 833 | + stack, and pushes the address of its element of size bytes. } | |
| 834 | +procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); | |
| 835 | +var m, ml: TMem; | |
| 1350 | 836 | begin |
| 1351 | - if x.mode = mProc then begin | |
| 1352 | - c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0; | |
| 1353 | - c.obj := x.obj; c.code := x.obj^.isCode; c.ext := x.obj^.ext; | |
| 1354 | - end else begin | |
| 1355 | - c.obj := nil; c.code := false; c.ext := false; | |
| 1356 | - if x.typ^.form <> fProc then MarkAt(x.pos, 'expected a procedure, found ' + What(x)); | |
| 1357 | - c.sig := x.typ; c.chunk := -1; | |
| 1358 | - c.target := ToSlot(x); | |
| 837 | + PopTo(RAX); | |
| 838 | + m := Place(a, RCX); | |
| 839 | + ml := m; ml.disp := ml.disp + 8; | |
| 840 | + if checked then begin | |
| 841 | + AluM(aluCmp, RAX, ml); | |
| 842 | + Check(ccB, 0, p, RAX, ml, 0, -1); | |
| 1359 | 843 | end; |
| 1360 | - c.param := c.sig^.fields; | |
| 1361 | - c.n := 0; | |
| 844 | + if size <> 1 then ImulI(RAX, size); | |
| 845 | + AluM(aluAdd, RAX, m); | |
| 846 | + Computed(kWord); | |
| 1362 | 847 | end; |
| 1363 | 848 | |
| 1364 | -{ Too many arguments or too few, at p } | |
| 1365 | -procedure WrongCount(var c: TCall; const p: TPos; const what: shortstring); | |
| 849 | +{ Pops lo and hi, and the address of the array or slice at a if it is | |
| 850 | + on the stack, and makes the slice lo..hi of it, with elements of size | |
| 851 | + bytes, in the frame at tmp; the array has n elements. } | |
| 852 | +procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); | |
| 853 | +var m, ml: TMem; | |
| 1366 | 854 | begin |
| 1367 | - if c.obj = nil then MarkAt(p, what); | |
| 1368 | - Error(p, what + ': ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); | |
| 1369 | - if c.obj^.module <> nil then Note(c.obj^.pos, 'the signature is here'); | |
| 1370 | - Fail; | |
| 855 | + PopTo(RDX); | |
| 856 | + PopTo(RCX); | |
| 857 | + m := Place(a, R8); | |
| 858 | + ml := m; ml.disp := ml.disp + 8; | |
| 859 | + if checked then begin | |
| 860 | + if isArray then begin CmpI(RDX, n); ml := NoMem end | |
| 861 | + else AluM(aluCmp, RDX, ml); | |
| 862 | + Check(ccBE, 1, p, RCX, ml, n, -1); | |
| 863 | + Alu(aluCmp, RCX, RDX); | |
| 864 | + Check(ccBE, 1, p, RCX, ml, n, -1); | |
| 865 | + end; | |
| 866 | + Spill; | |
| 867 | + MovRR(RAX, RDX); | |
| 868 | + Alu(aluSub, RAX, RCX); | |
| 869 | + Store(Mem(RBP, tmp + 8), RAX, 8); | |
| 870 | + if size <> 1 then ImulI(RCX, size); | |
| 871 | + if isArray then Lea(RAX, m) else Load(RAX, m, 8, false); | |
| 872 | + Alu(aluAdd, RAX, RCX); | |
| 873 | + Store(Mem(RBP, tmp), RAX, 8); | |
| 1371 | 874 | end; |
| 1372 | 875 | |
| 1373 | -procedure PassArg(var c: TCall; var a: TItem); | |
| 1374 | -var p: PObj; s: int64; | |
| 876 | +{ Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its | |
| 877 | + bounds can't be known, but it can't run backwards. } | |
| 878 | +procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); | |
| 1375 | 879 | begin |
| 1376 | - p := c.param; | |
| 1377 | - if c.n > High(c.slots) then MarkAt(a.pos, 'too many arguments'); | |
| 1378 | - if p = nil then begin | |
| 1379 | - { past the parameters of a C procedure that ends in ...: a string | |
| 1380 | - goes as a C string, an integer constant as an INT, a real | |
| 1381 | - constant as an F64; C takes no F32 there } | |
| 1382 | - if not c.sig^.variadic then WrongCount(c, a.pos, 'too many arguments'); | |
| 1383 | - if a.mode = mStr then Coerce(a, tPtrU8) | |
| 1384 | - else if a.typ^.form = fConst then Coerce(a, tInt) | |
| 1385 | - else if a.typ = tRConst then Coerce(a, tReal) | |
| 1386 | - else if IsF32(a.typ) then MarkAt(a.pos, 'an F32 goes to ... as F64(x)') | |
| 1387 | - else if not IsScalar(a.typ) then MarkAt(a.pos, 'only a single value goes to ...'); | |
| 1388 | - c.isF[c.n] := a.typ^.form = fReal; | |
| 1389 | - c.slots[c.n] := ToSlot(a); | |
| 1390 | - inc(c.n); | |
| 1391 | - exit; | |
| 1392 | - end; | |
| 1393 | - c.isF[c.n] := (p^.typ^.form = fReal) and not p^.isVar; | |
| 1394 | - if p^.isVar then begin | |
| 1395 | - NotReadOnly(a); | |
| 1396 | - if (a.mode <> mVar) or not a.isVar then MarkAt(a.pos, 'a var parameter needs a variable'); | |
| 1397 | - if a.odd then MarkAt(a.pos, 'a field of a packed record can''t be a var parameter'); | |
| 1398 | - if not Same(a.typ, p^.typ) then MarkAt(a.pos, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a)); | |
| 1399 | - if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); | |
| 1400 | - Address(a, RAX); | |
| 1401 | - s := AllocTemp(8, 8); | |
| 1402 | - Store(Mem(RBP, s), RAX, 8); | |
| 1403 | - end else if IsAggregate(p^.typ) then begin | |
| 1404 | - Coerce(a, p^.typ); | |
| 1405 | - if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a); | |
| 1406 | - Address(a, RAX); | |
| 1407 | - s := AllocTemp(8, 8); | |
| 1408 | - Store(Mem(RBP, s), RAX, 8); | |
| 1409 | - end else begin | |
| 1410 | - Coerce(a, p^.typ); | |
| 1411 | - s := ToSlot(a); | |
| 880 | + PopTo(RDX); | |
| 881 | + PopTo(RCX); | |
| 882 | + PopTo(R8); | |
| 883 | + if checked then begin | |
| 884 | + Alu(aluCmp, RCX, RDX); | |
| 885 | + Check(ccLE, 3, p, RCX, NoMem, 0, -1); | |
| 1412 | 886 | end; |
| 1413 | - c.slots[c.n] := s; | |
| 1414 | - inc(c.n); | |
| 1415 | - c.param := p^.next; | |
| 887 | + Spill; | |
| 888 | + MovRR(RAX, RDX); | |
| 889 | + Alu(aluSub, RAX, RCX); | |
| 890 | + Store(Mem(RBP, tmp + 8), RAX, 8); | |
| 891 | + if size <> 1 then ImulI(RCX, size); | |
| 892 | + MovRR(RAX, R8); | |
| 893 | + Alu(aluAdd, RAX, RCX); | |
| 894 | + Store(Mem(RBP, tmp), RAX, 8); | |
| 1416 | 895 | end; |
| 1417 | 896 | |
| 1418 | -procedure EndCall(var c: TCall; var x: TItem); | |
| 897 | +{ Calls } | |
| 898 | + | |
| 899 | +procedure Call(var c: TCall); | |
| 1419 | 900 | var |
| 1420 | - res: PType; i, ni, nf, k, pad: longint; ret: int64; | |
| 901 | + res: PType; i, ni, nf, k, pad: longint; | |
| 1421 | 902 | onStack: array[0..63] of boolean; |
| 1422 | 903 | begin |
| 1423 | - if c.param <> nil then WrongCount(c, TokPos, 'too few arguments'); | |
| 904 | + Spill; { the value in rax waits on the stack, past the call } | |
| 1424 | 905 | res := c.sig^.base; |
| 1425 | - ret := 0; | |
| 906 | + c.ret := 0; | |
| 1426 | 907 | ni := 0; nf := 0; |
| 1427 | 908 | if (res <> nil) and IsAggregate(res) then begin |
| 1428 | - ret := AllocTemp(res^.size, res^.align); | |
| 909 | + c.ret := AllocTemp(res^.size, res^.align); | |
| 1429 | 910 | if not c.code then ni := 1; |
| 1430 | 911 | end; |
| 1431 | 912 | { the System V classes: each argument in the next free register of |
| @@ -1436,7 +917,7 @@ | ||
| 1436 | 917 | else begin onStack[i] := ni >= 6; if ni < 6 then inc(ni) end; |
| 1437 | 918 | if onStack[i] then inc(k); |
| 1438 | 919 | end; |
| 1439 | - pad := ord(odd(depth + k)); | |
| 920 | + pad := ord(odd(pushed + k)); | |
| 1440 | 921 | if pad <> 0 then AluI(aluSub, RSP, 8); |
| 1441 | 922 | for i := c.n - 1 downto 0 do |
| 1442 | 923 | if onStack[i] then begin |
| @@ -1444,7 +925,7 @@ | ||
| 1444 | 925 | Push(RAX); |
| 1445 | 926 | end; |
| 1446 | 927 | ni := 0; nf := 0; |
| 1447 | - if (ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, ret)); ni := 1 end; | |
| 928 | + if (c.ret <> 0) and not c.code then begin Lea(RDI, Mem(RBP, c.ret)); ni := 1 end; | |
| 1448 | 929 | for i := 0 to c.n - 1 do |
| 1449 | 930 | if not onStack[i] then |
| 1450 | 931 | if c.isF[i] then begin MovsdXM(nf, Mem(RBP, c.slots[i])); inc(nf) end |
| @@ -1458,142 +939,67 @@ | ||
| 1458 | 939 | CallR(R11); |
| 1459 | 940 | end; |
| 1460 | 941 | if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); |
| 1461 | - if (ret <> 0) and c.code then begin | |
| 1462 | - Store(Mem(RBP, ret), RAX, 8); | |
| 1463 | - Store(Mem(RBP, ret + 8), RDX, 8); | |
| 942 | + if (c.ret <> 0) and c.code then begin | |
| 943 | + Store(Mem(RBP, c.ret), RAX, 8); | |
| 944 | + Store(Mem(RBP, c.ret + 8), RDX, 8); | |
| 1464 | 945 | end; |
| 1465 | - FillChar(x, SizeOf(x), 0); | |
| 1466 | - if res = nil then begin | |
| 1467 | - x.mode := mNone; x.typ := tNone; x.obj := c.obj | |
| 1468 | - end else if ret <> 0 then | |
| 1469 | - TempItem(x, res, ret) | |
| 1470 | - else begin | |
| 1471 | - x.mode := mReg; x.typ := res; | |
| 946 | + if (res <> nil) and (c.ret = 0) then begin | |
| 1472 | 947 | if res^.form = fReal then MovqRX(RAX, 0) |
| 1473 | 948 | else if res^.form = fBool then Extend(RAX, 1, false) |
| 1474 | - else Norm(res); | |
| 949 | + else if (res^.form = fInt) and (res^.size < 8) then Extend(RAX, res^.size, res^.signed); | |
| 950 | + Computed(KindOf(res)); | |
| 1475 | 951 | end; |
| 1476 | 952 | end; |
| 1477 | 953 | |
| 1478 | -{ Variants } | |
| 954 | +{ Frames } | |
| 1479 | 955 | |
| 1480 | -{ A temporary of type t, all zero: for variants and record literals. } | |
| 1481 | -procedure ZeroTemp(var x: TItem; t: PType); | |
| 1482 | -var tmp: int64; | |
| 956 | +function AllocTemp(size, align: int64): int64; | |
| 1483 | 957 | begin |
| 1484 | - tmp := AllocTemp(t^.size, t^.align); | |
| 1485 | - Lea(RDI, Mem(RBP, tmp)); | |
| 1486 | - MovRI(RCX, t^.size); | |
| 1487 | - MovRI(RAX, 0); | |
| 1488 | - RepStosb; | |
| 1489 | - TempItem(x, t, tmp); | |
| 958 | + if align < 1 then align := 1; | |
| 959 | + frameTop := AlignUp(frameTop + size, align); | |
| 960 | + if frameTop > frameMax then frameMax := frameTop; | |
| 961 | + AllocTemp := -frameTop; | |
| 1490 | 962 | end; |
| 1491 | 963 | |
| 1492 | -procedure Construct(var x: TItem; c: PObj); | |
| 964 | +function AllocPerm(size, align: int64): int64; | |
| 1493 | 965 | begin |
| 1494 | - ZeroTemp(x, c^.typ); | |
| 1495 | - MovRI(RAX, c^.val); | |
| 1496 | - Store(Mem(RBP, x.off), RAX, 8); | |
| 966 | + AllocPerm := AllocTemp(size, align); | |
| 967 | + permTop := frameTop; | |
| 1497 | 968 | end; |
| 1498 | 969 | |
| 1499 | -procedure SetField(const x: TItem; f: PObj; var a: TItem); | |
| 970 | +function TempMark: int64; | |
| 1500 | 971 | begin |
| 1501 | - Coerce(a, f^.typ); | |
| 1502 | - if IsScalar(f^.typ) then begin | |
| 1503 | - Value(a); | |
| 1504 | - Store(Mem(RBP, x.off + f^.val), RAX, ScalarSize(f^.typ)); | |
| 1505 | - end else begin | |
| 1506 | - if a.vol then MarkAt(a.pos, 'can''t copy through a volatile pointer'); | |
| 1507 | - Address(a, RSI); | |
| 1508 | - Lea(RDI, Mem(RBP, x.off + f^.val)); | |
| 1509 | - Copy(f^.typ^.size); | |
| 1510 | - end; | |
| 972 | + TempMark := frameTop; | |
| 1511 | 973 | end; |
| 1512 | 974 | |
| 1513 | -{ The slot match tests: a variant's address, or an enumeration's value. } | |
| 1514 | -function MatchBegin(var x: TItem): int64; | |
| 1515 | -var p: int64; | |
| 975 | +procedure ReleaseTemps(m: int64); | |
| 1516 | 976 | begin |
| 1517 | - if x.typ^.form in [fEnum, fInt] then exit(ToSlot(x)); | |
| 1518 | - if (x.mode <> mVar) or (x.typ^.form <> fVariant) then MarkAt(x.pos, 'match needs a variant, an enumeration or an integer, found ' + What(x)); | |
| 1519 | - if x.ro then CopyToTemp(x); { its arms' names stand for a copy's fields } | |
| 1520 | - Address(x, RAX); | |
| 1521 | - p := AllocTemp(8, 8); | |
| 1522 | - Store(Mem(RBP, p), RAX, 8); | |
| 1523 | - MatchBegin := p; | |
| 977 | + if m < permTop then m := permTop; | |
| 978 | + frameTop := m; | |
| 1524 | 979 | end; |
| 1525 | 980 | |
| 1526 | -procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean); | |
| 981 | +{ Every slot used so far stays reserved for the rest of the procedure: | |
| 982 | + a deferred statement's temporaries live until it runs. } | |
| 983 | +procedure KeepTemps; | |
| 1527 | 984 | begin |
| 1528 | - Load(RAX, Mem(RBP, p), 8, false); | |
| 1529 | - if not isEnum then Load(RAX, Mem(RAX, 0), 8, false); | |
| 1530 | - CmpI(RAX, tag); | |
| 1531 | - Jcc(ccNE, L); | |
| 985 | + frameTop := frameMax; | |
| 986 | + permTop := frameMax; | |
| 1532 | 987 | end; |
| 1533 | 988 | |
| 1534 | -{ To body if the integer in slot p is from lo to hi: less its lo, it is | |
| 1535 | - then at most hi - lo, compared unsigned, which holds for a signed type | |
| 1536 | - or not, with no second jump. } | |
| 1537 | -procedure MatchRange(p, lo, hi: int64; var body: longint); | |
| 1538 | -begin | |
| 1539 | - Load(RAX, Mem(RBP, p), 8, false); | |
| 1540 | - if lo = hi then begin | |
| 1541 | - CmpI(RAX, lo); | |
| 1542 | - Jcc(ccE, body); | |
| 1543 | - exit; | |
| 1544 | - end; | |
| 1545 | - if lo <> 0 then begin MovRI(RCX, lo); Alu(aluSub, RAX, RCX) end; | |
| 1546 | - CmpI(RAX, hi - lo); | |
| 1547 | - Jcc(ccBE, body); | |
| 1548 | -end; | |
| 1549 | - | |
| 1550 | -{ The end of a round of a for loop: stop if the step would pass the | |
| 1551 | - limit, else step. Counting the distance left, rather than comparing | |
| 1552 | - after the step, can't be fooled by the variable wrapping around. } | |
| 1553 | -procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); | |
| 1554 | -var v, w: TItem; | |
| 1555 | -begin | |
| 1556 | - MakeItem(w, o); | |
| 1557 | - Value(w); | |
| 1558 | - Load(RCX, Mem(RBP, bslot), 8, false); | |
| 1559 | - if step > 0 then begin | |
| 1560 | - Alu(aluSub, RCX, RAX); | |
| 1561 | - CmpI(RCX, step); | |
| 1562 | - end else begin | |
| 1563 | - Alu(aluSub, RAX, RCX); | |
| 1564 | - CmpI(RAX, -step); | |
| 1565 | - end; | |
| 1566 | - Jcc(ccB, exitL); | |
| 1567 | - MakeItem(v, o); | |
| 1568 | - Save(v); | |
| 1569 | - MakeItem(w, o); | |
| 1570 | - Value(w); | |
| 1571 | - MovRI(RCX, step); | |
| 1572 | - Alu(aluAdd, RAX, RCX); | |
| 1573 | - Norm(o^.typ); | |
| 1574 | - Assign(v, w); | |
| 1575 | - JmpBack(top); | |
| 1576 | -end; | |
| 1577 | - | |
| 1578 | -procedure CondJump(var x: TItem; var L: longint); | |
| 1579 | -begin | |
| 1580 | - if x.typ^.form <> fBool then MarkAt(x.pos, 'a condition is a BOOL, found ' + What(x)); | |
| 1581 | - Value(x); | |
| 1582 | - TestRR(RAX, RAX); | |
| 1583 | - Jcc(ccE, L); | |
| 1584 | -end; | |
| 1585 | - | |
| 1586 | -{ Procedures } | |
| 1587 | - | |
| 989 | +{ The parameters' places: each in a frame slot of its own, stored there | |
| 990 | + from its register on entry, or above the frame where the caller | |
| 991 | + pushed it. A var parameter, and a record or array passed by value, | |
| 992 | + holds an address (ind). } | |
| 1588 | 993 | procedure BeginProc(sig: PType; params: PObj); |
| 1589 | 994 | var p: PObj; ni, nf: longint; stackOff: int64; f: boolean; |
| 1590 | 995 | begin |
| 1591 | - frameTop := 0; frameMax := 0; permTop := 0; depth := 0; | |
| 996 | + frameTop := 0; frameMax := 0; permTop := 0; | |
| 997 | + ResetStack(0, 0); | |
| 1592 | 998 | exitL := NoLabel; |
| 1593 | 999 | numIn := 0; |
| 1594 | - result := sig^.base; | |
| 1000 | + resType := sig^.base; | |
| 1595 | 1001 | ni := 0; nf := 0; stackOff := 16; |
| 1596 | - if (result <> nil) and IsAggregate(result) then begin | |
| 1002 | + if (resType <> nil) and IsAggregate(resType) then begin | |
| 1597 | 1003 | retSlot := AllocPerm(8, 8); |
| 1598 | 1004 | inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; |
| 1599 | 1005 | numIn := 1; ni := 1; |
| @@ -1607,7 +1013,9 @@ | ||
| 1607 | 1013 | if f then begin inReg[numIn] := -1 - nf; inc(nf) end |
| 1608 | 1014 | else begin inReg[numIn] := IntRegs[ni]; inc(ni) end; |
| 1609 | 1015 | inSlot[numIn] := p^.val; |
| 1610 | - if p^.ind then inSize[numIn] := 8 else inSize[numIn] := ScalarSize(p^.typ); | |
| 1016 | + if p^.ind then inSize[numIn] := 8 | |
| 1017 | + else if p^.typ^.form in [fInt, fBool, fEnum, fSet, fReal] then inSize[numIn] := p^.typ^.size | |
| 1018 | + else inSize[numIn] := 8; | |
| 1611 | 1019 | inc(numIn); |
| 1612 | 1020 | end else begin |
| 1613 | 1021 | p^.val := stackOff; |
| @@ -1618,10 +1026,10 @@ | ||
| 1618 | 1026 | paramTop := frameTop; |
| 1619 | 1027 | end; |
| 1620 | 1028 | |
| 1621 | -{ The prologue, which is for the procedure's first line. } | |
| 1622 | -{ Only the locals are zeroed: the parameters are stored over their | |
| 1623 | - slots next, and every temporary is written before it is read. The | |
| 1624 | - locals are all declared by now, below the parameters' slots. } | |
| 1029 | +{ The prologue, which is for the procedure's first line. Only the | |
| 1030 | + locals are zeroed: the parameters are stored over their slots next, | |
| 1031 | + and every temporary is written before it is read. The locals are all | |
| 1032 | + declared by now, below the parameters' slots. } | |
| 1625 | 1033 | procedure EnterBody(c, line: longint); |
| 1626 | 1034 | var i, top: longint; n: int64; |
| 1627 | 1035 | begin |
| @@ -1649,127 +1057,48 @@ | ||
| 1649 | 1057 | else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]); |
| 1650 | 1058 | end; |
| 1651 | 1059 | |
| 1652 | -procedure ReturnValue(var x: TItem); | |
| 1060 | +{ Pops one of several results, a value of size bytes, into its place | |
| 1061 | + off bytes into the caller's, behind the hidden pointer. } | |
| 1062 | +procedure ResultTo(off: int64; size: longint); | |
| 1653 | 1063 | begin |
| 1654 | - if result = nil then MarkAt(x.pos, 'this procedure returns nothing'); | |
| 1655 | - Coerce(x, result); | |
| 1656 | - if IsAggregate(result) then begin | |
| 1657 | - if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 1658 | - Address(x, RSI); | |
| 1659 | - Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 1660 | - Copy(result^.size); | |
| 1661 | - Load(RAX, Mem(RBP, retSlot), 8, false); | |
| 1662 | - end else Value(x); | |
| 1064 | + PopTo(RAX); | |
| 1065 | + Load(RCX, Mem(RBP, retSlot), 8, false); | |
| 1066 | + Store(Mem(RCX, off), RAX, size); | |
| 1663 | 1067 | end; |
| 1664 | 1068 | |
| 1665 | -{ One of several results, into its place behind the hidden pointer. } | |
| 1666 | -procedure ReturnField(f: PObj; var x: TItem); | |
| 1667 | -begin | |
| 1668 | - Coerce(x, f^.typ); | |
| 1669 | - if IsScalar(f^.typ) then begin | |
| 1670 | - Value(x); | |
| 1671 | - Load(RCX, Mem(RBP, retSlot), 8, false); | |
| 1672 | - Store(Mem(RCX, f^.val), RAX, ScalarSize(f^.typ)); | |
| 1673 | - end else begin | |
| 1674 | - if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 1675 | - Address(x, RSI); | |
| 1676 | - Load(RDI, Mem(RBP, retSlot), 8, false); | |
| 1677 | - Lea(RDI, Mem(RDI, f^.val)); | |
| 1678 | - Copy(f^.typ^.size); | |
| 1679 | - end; | |
| 1680 | -end; | |
| 1681 | - | |
| 1682 | -{ Several results at once, the call's that return f(x) passes on. } | |
| 1683 | -procedure ReturnAll(var x: TItem); | |
| 1069 | +{ Pops an address, and copies size bytes from it to the result, off | |
| 1070 | + bytes into it. } | |
| 1071 | +procedure CopyResult(off, size: int64); | |
| 1684 | 1072 | begin |
| 1685 | - Address(x, RSI); | |
| 1073 | + PopTo(RSI); | |
| 1686 | 1074 | Load(RDI, Mem(RBP, retSlot), 8, false); |
| 1687 | - Copy(result^.size); | |
| 1075 | + if off <> 0 then Lea(RDI, Mem(RDI, off)); | |
| 1076 | + CopyBytes(size); | |
| 1688 | 1077 | end; |
| 1689 | 1078 | |
| 1690 | -{ With the result in rax, as ReturnValue left it. } | |
| 1079 | +{ return: the result, a value on top, goes in rax or xmm0; one behind | |
| 1080 | + the hidden pointer goes back with that pointer in rax. } | |
| 1691 | 1081 | procedure ReturnJump; |
| 1692 | 1082 | begin |
| 1693 | - if IsAggregate(result) then Load(RAX, Mem(RBP, retSlot), 8, false) | |
| 1694 | - else if result^.form = fReal then MovqXR(0, RAX); | |
| 1083 | + if resType <> nil then | |
| 1084 | + if IsAggregate(resType) then Load(RAX, Mem(RBP, retSlot), 8, false) | |
| 1085 | + else begin | |
| 1086 | + PopTo(RAX); | |
| 1087 | + if resType^.form = fReal then MovqXR(0, RAX); | |
| 1088 | + end; | |
| 1695 | 1089 | Jmp(exitL); |
| 1696 | 1090 | end; |
| 1697 | 1091 | |
| 1698 | -{ Keeps a variable's address in a frame slot, rather than in a register | |
| 1699 | - or on the stack, while other code runs. } | |
| 1700 | -procedure Pin(var x: TItem); | |
| 1701 | -var s: int64; | |
| 1702 | -begin | |
| 1703 | - if (x.mode = mVar) and (x.base in [bReg, bStack]) then begin | |
| 1704 | - Address(x, RAX); | |
| 1705 | - s := AllocTemp(8, 8); | |
| 1706 | - Store(Mem(RBP, s), RAX, 8); | |
| 1707 | - x.base := bSlot; | |
| 1708 | - x.chunk := s; | |
| 1709 | - end; | |
| 1710 | -end; | |
| 1711 | - | |
| 1712 | -procedure IncDec(var v, k: TItem; dec: boolean); | |
| 1713 | -var n: longint; | |
| 1714 | -begin | |
| 1715 | - CoerceExact(k, v.typ, ''); | |
| 1716 | - Value(k); | |
| 1717 | - MovRR(RCX, RAX); | |
| 1718 | - Address(v, RDX); | |
| 1719 | - n := ScalarSize(v.typ); | |
| 1720 | - Load(RAX, Mem(RDX, 0), n, v.typ^.signed); | |
| 1721 | - if dec then Alu(aluSub, RAX, RCX) else Alu(aluAdd, RAX, RCX); | |
| 1722 | - Store(Mem(RDX, 0), RAX, n); | |
| 1723 | -end; | |
| 1724 | - | |
| 1725 | -{ An error is the address of its length, followed by its message. } | |
| 1726 | -function ErrorChunk(var x: TItem; module, name: longint): longint; | |
| 1727 | -var c: longint; p: PBytes; | |
| 1728 | -begin | |
| 1729 | - if x.mode <> mStr then MarkAt(x.pos, 'an error is made from a string'); | |
| 1730 | - p := GetMem(x.val + 8); | |
| 1731 | - PInt64(p)^ := x.val; | |
| 1732 | - Move(rodata^[chunks^[x.chunk].start], p^[8], x.val); | |
| 1733 | - c := NewChunk(secRodata, module, name); | |
| 1734 | - SetData(c, p, x.val + 8, 8); | |
| 1735 | - FreeMem(p); | |
| 1736 | - ErrorChunk := c; | |
| 1737 | -end; | |
| 1738 | - | |
| 1739 | -procedure Msg(var x: TItem); | |
| 1740 | -var L: longint; s: int64; | |
| 1741 | -begin | |
| 1742 | - Coerce(x, tError); | |
| 1743 | - Value(x); | |
| 1744 | - s := AllocTemp(16, 8); | |
| 1745 | - MovRI(RCX, 0); | |
| 1746 | - L := NoLabel; | |
| 1747 | - TestRR(RAX, RAX); | |
| 1748 | - Jcc(ccE, L); | |
| 1749 | - Load(RCX, Mem(RAX, 0), 8, false); | |
| 1750 | - Lea(RAX, Mem(RAX, 8)); | |
| 1751 | - FixLink(L); | |
| 1752 | - Store(Mem(RBP, s), RAX, 8); | |
| 1753 | - Store(Mem(RBP, s + 8), RCX, 8); | |
| 1754 | - TempItem(x, tBytes, s); | |
| 1755 | -end; | |
| 1756 | - | |
| 1757 | -procedure ReturnNone; | |
| 1758 | -begin | |
| 1759 | - if result <> nil then Mark('return needs a result of ' + TypeName(result)); | |
| 1760 | - Jmp(exitL); | |
| 1761 | -end; | |
| 1762 | - | |
| 1763 | 1092 | procedure EndProc(c: longint); |
| 1764 | 1093 | var n: int64; |
| 1765 | 1094 | begin |
| 1766 | 1095 | codeChunk := -1; |
| 1767 | - if depth <> 0 then Fatal('internal error: stack depth'); | |
| 1096 | + if (depth <> 0) or (pushed <> 0) then Fatal('internal error: stack depth'); | |
| 1768 | 1097 | { running off the end returns zeros } |
| 1769 | - if result <> nil then | |
| 1770 | - if IsAggregate(result) then begin | |
| 1098 | + if resType <> nil then | |
| 1099 | + if IsAggregate(resType) then begin | |
| 1771 | 1100 | Load(RDI, Mem(RBP, retSlot), 8, false); |
| 1772 | - MovRI(RCX, result^.size); | |
| 1101 | + MovRI(RCX, resType^.size); | |
| 1773 | 1102 | MovRI(RAX, 0); |
| 1774 | 1103 | RepStosb; |
| 1775 | 1104 | Load(RAX, Mem(RBP, retSlot), 8, false); |
| @@ -1785,6 +1114,93 @@ | ||
| 1785 | 1114 | if n > high(longint) then Mark('frame too big'); |
| 1786 | 1115 | Put4At(frameFix, n); |
| 1787 | 1116 | EndChunk(c); |
| 1117 | +end; | |
| 1118 | + | |
| 1119 | +{ A deferred statement is compiled where it stands, as a subroutine of | |
| 1120 | + its procedure that the code jumps over; DeferBegin gives its address | |
| 1121 | + and starts it, with the return address pushed. The stack is empty | |
| 1122 | + where it is called, and where it begins. } | |
| 1123 | +function DeferBegin(var L, saved: longint): longint; | |
| 1124 | +begin | |
| 1125 | + Jmp(L); | |
| 1126 | + saved := pushed; | |
| 1127 | + pushed := 1; | |
| 1128 | + DeferBegin := pc; | |
| 1129 | +end; | |
| 1130 | + | |
| 1131 | +procedure DeferEnd(L, saved: longint); | |
| 1132 | +begin | |
| 1133 | + KeepTemps; | |
| 1134 | + Ret; | |
| 1135 | + FixLink(L); | |
| 1136 | + pushed := saved; | |
| 1788 | 1137 | end; |
| 1789 | 1138 | |
| 1139 | +procedure RunDefer(at: longint); | |
| 1140 | +begin | |
| 1141 | + CallBack(at); | |
| 1142 | +end; | |
| 1143 | + | |
| 1144 | +{ The entry point, _start, and its chunk: it saves argc and argv, and | |
| 1145 | + the address of the places for Rt; runs every used module's body, then | |
| 1146 | + main; and ends with Rt.exit, its status main's or 0. A dynamic program | |
| 1147 | + starts as C's crt1 does, through __libc_start_main(main, argc, argv, | |
| 1148 | + nil, nil, rtld_fini, stack_end), with rsp aligned; its main first | |
| 1149 | + gives Rt C's exit, in cExit. } | |
| 1150 | +function EntryPoint(dynamic: boolean; first: PModule; main: PObj; cExit, places, placesVar, exitProc: longint): longint; | |
| 1151 | +var entry, start: longint; m: PModule; | |
| 1152 | +begin | |
| 1153 | + entry := NewChunk(secText, -1, Intern('_start', 6)); | |
| 1154 | + if dynamic then begin | |
| 1155 | + start := NewChunk(secText, -1, Intern('main', 4)); | |
| 1156 | + BeginChunk(entry); | |
| 1157 | + MovRI(RBP, 0); | |
| 1158 | + MovRR(R9, RDX); | |
| 1159 | + Pop(RSI); | |
| 1160 | + MovRR(RDX, RSP); | |
| 1161 | + AluI(aluAnd, RSP, -16); | |
| 1162 | + Push(RAX); | |
| 1163 | + Push(RSP); | |
| 1164 | + MovRI(R8, 0); | |
| 1165 | + MovRI(RCX, 0); | |
| 1166 | + Lea(RDI, MemRip(start, 0)); | |
| 1167 | + CallM(MemRip(ExternChunk(Intern('__libc_start_main', 17)), 0)); | |
| 1168 | + Ud2; | |
| 1169 | + EndChunk(entry); | |
| 1170 | + BeginChunk(start); | |
| 1171 | + Push(RBP); { aligns the stack again } | |
| 1172 | + Store(MemRip(argcChunk, 0), RDI, 8); | |
| 1173 | + Store(MemRip(argvChunk, 0), RSI, 8); | |
| 1174 | + Load(RAX, MemRip(ExternChunk(Intern('exit', 4)), 0), 8, false); | |
| 1175 | + Store(MemRip(cExit, 0), RAX, 8); | |
| 1176 | + end else begin | |
| 1177 | + start := entry; | |
| 1178 | + BeginChunk(entry); | |
| 1179 | + Load(RAX, Mem(RSP, 0), 8, false); | |
| 1180 | + Store(MemRip(argcChunk, 0), RAX, 8); | |
| 1181 | + Lea(RAX, Mem(RSP, 8)); | |
| 1182 | + Store(MemRip(argvChunk, 0), RAX, 8); | |
| 1183 | + end; | |
| 1184 | + Lea(RAX, MemRip(places, 0)); | |
| 1185 | + Store(MemRip(placesVar, 0), RAX, 8); | |
| 1186 | + m := first; | |
| 1187 | + while m <> nil do begin | |
| 1188 | + if m^.used and (m^.body >= 0) then CallChunk(m^.body); | |
| 1189 | + m := m^.next; | |
| 1190 | + end; | |
| 1191 | + if main <> nil then CallChunk(main^.chunk); | |
| 1192 | + if (main <> nil) and (main^.typ^.base <> nil) then MovRR(RDI, RAX) else MovRI(RDI, 0); | |
| 1193 | + CallChunk(exitProc); | |
| 1194 | + Ud2; | |
| 1195 | + EndChunk(start); | |
| 1196 | + EntryPoint := entry; | |
| 1197 | +end; | |
| 1198 | + | |
| 1199 | +initialization | |
| 1200 | + inRax := -1; | |
| 1201 | + rtFail := NewChunk(secText, Intern('Rt', 2), Intern('fail', 4)); | |
| 1202 | + argcChunk := NewChunk(secBss, -1, Intern('argc', 4)); | |
| 1203 | + SetBss(argcChunk, 8, 8); | |
| 1204 | + argvChunk := NewChunk(secBss, -1, Intern('argv', 4)); | |
| 1205 | + SetBss(argvChunk, 8, 8); | |
| 1790 | 1206 | end. |
| @@ -0,0 +1,1303 @@ | ||
| 1 | +{ item.pas: operands, their types, and what they mean. | |
| 2 | + | |
| 3 | + The parser describes each operand as an item (TItem), as in Wirth's | |
| 4 | + compilers: a constant, a variable at an address it can name, a value | |
| 5 | + on gen's stack, a procedure, a string. The routines here check the | |
| 6 | + items' types, fold constants, and have gen.pas make the code, as calls | |
| 7 | + of its stack machine; nothing here knows the machine. | |
| 8 | + | |
| 9 | + Depends on: scan for errors, sym for types, chunk for data, gen for | |
| 10 | + code. | |
| 11 | + Leaves out: the language's syntax, which is parse.pas; the machine, | |
| 12 | + which is gen.pas. } | |
| 13 | + | |
| 14 | +unit item; | |
| 15 | + | |
| 16 | +{$mode objfpc}{$H-} | |
| 17 | + | |
| 18 | +interface | |
| 19 | + | |
| 20 | +uses scan, diag, sym, chunk, gen; | |
| 21 | + | |
| 22 | +type | |
| 23 | + TMode = (mConst, mVar, mStack, mProc, mType, mStr, mBuiltin, mCase, mNone); | |
| 24 | + | |
| 25 | + TItem = record | |
| 26 | + mode: TMode; | |
| 27 | + typ: PType; | |
| 28 | + val: int64; { mConst: the value, an F64's bits; mStr: its length } | |
| 29 | + base: TBase; { mVar: what the address is relative to } | |
| 30 | + chunk: longint; { mVar with bGlobal, mStr: the chunk; bSlot: the slot } | |
| 31 | + off: int64; { mVar: added to the base } | |
| 32 | + vol: boolean; { mVar: reached through ^VOLATILE } | |
| 33 | + isVar: boolean; { mVar: a variable of the program, not a temporary } | |
| 34 | + odd: boolean; { mVar: in a packed record, so maybe misaligned } | |
| 35 | + ro: boolean; { mVar: another module's exported variable, or part | |
| 36 | + of one, which only that module changes } | |
| 37 | + obj: PObj; { mProc, mBuiltin, mCase; mVar: the variable named } | |
| 38 | + pos: TPos; { where it is in the source, for messages } | |
| 39 | + end; | |
| 40 | + | |
| 41 | +var | |
| 42 | + checks: boolean; { the module's index and slice checks are made: it | |
| 43 | + doesn't say check off } | |
| 44 | + | |
| 45 | +procedure MakeConst(var x: TItem; t: PType; v: int64); | |
| 46 | +function What(const x: TItem): shortstring; | |
| 47 | +procedure MakeReal(var x: TItem; r: double); | |
| 48 | +procedure MakeStr(var x: TItem); | |
| 49 | +procedure MakeItem(var x: TItem; o: PObj); | |
| 50 | +function RealOf(const x: TItem): double; | |
| 51 | +function FrameAdr(off: int64): TAdr; | |
| 52 | +function GlobalAdr(c: longint): TAdr; | |
| 53 | +function AdrOf(const x: TItem): TAdr; | |
| 54 | + | |
| 55 | +procedure UsesReals(const p: TPos); | |
| 56 | +procedure Value(var x: TItem); | |
| 57 | +procedure Address(var x: TItem); | |
| 58 | +procedure Discard(const x: TItem); | |
| 59 | +procedure Coerce(var x: TItem; t: PType); | |
| 60 | +procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); | |
| 61 | +procedure NotReadOnly(const x: TItem); | |
| 62 | +procedure Assign(var dst, src: TItem); | |
| 63 | +procedure CopyToTemp(var x: TItem); | |
| 64 | +function ToSlot(var x: TItem): int64; | |
| 65 | + | |
| 66 | +procedure Deref(var x: TItem); | |
| 67 | +procedure Field(var x: TItem; f: PObj); | |
| 68 | +procedure IndexPrep(var x: TItem); | |
| 69 | +procedure Index(var x, y: TItem); | |
| 70 | +procedure SliceLo(var lo: TItem); | |
| 71 | +procedure Slice(var x, lo, hi: TItem); | |
| 72 | + | |
| 73 | +procedure Neg(var x: TItem); | |
| 74 | +procedure NotOp(var x: TItem); | |
| 75 | +procedure Complement(var x: TItem); | |
| 76 | +procedure Op1(op: TSym; var x: TItem; var L: longint); | |
| 77 | +procedure Op2(op: TSym; var x, y: TItem; L: longint); | |
| 78 | +function IsWord(t: PType): boolean; | |
| 79 | +procedure Convert(var x: TItem; t: PType); | |
| 80 | + | |
| 81 | +procedure BeginCall(var c: TCall; var x: TItem); | |
| 82 | +procedure PassArg(var c: TCall; var a: TItem); | |
| 83 | +procedure EndCall(var c: TCall; var x: TItem); | |
| 84 | + | |
| 85 | +procedure ZeroTemp(var x: TItem; t: PType); | |
| 86 | +procedure Construct(var x: TItem; c: PObj); | |
| 87 | +procedure SetField(const x: TItem; f: PObj; var a: TItem); | |
| 88 | +function MatchBegin(var x: TItem): int64; | |
| 89 | +procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean); | |
| 90 | +procedure MatchRange(p, lo, hi: int64; var body: longint); | |
| 91 | +procedure CondJump(var x: TItem; var L: longint); | |
| 92 | + | |
| 93 | +procedure SliceOf(var x: TItem; t: PType; len: int64); | |
| 94 | +procedure TempItem(var x: TItem; t: PType; off: int64); | |
| 95 | +function ScalarSize(t: PType): longint; | |
| 96 | +procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); | |
| 97 | + | |
| 98 | +procedure BeginProc(sig: PType; params: PObj); | |
| 99 | +procedure ReturnValue(var x: TItem); | |
| 100 | +procedure ReturnField(f: PObj; var x: TItem); | |
| 101 | +procedure ReturnAll(var x: TItem); | |
| 102 | +procedure ReturnNone; | |
| 103 | +procedure Pin(var x: TItem); | |
| 104 | +procedure IncDec(var v, k: TItem; dec: boolean); | |
| 105 | +procedure Msg(var x: TItem); | |
| 106 | +function ErrorChunk(var x: TItem; module, name: longint): longint; | |
| 107 | + | |
| 108 | +implementation | |
| 109 | + | |
| 110 | +var | |
| 111 | + result: PType; { the result of the procedure being compiled, or nil } | |
| 112 | + | |
| 113 | +{ Items } | |
| 114 | + | |
| 115 | +procedure MakeConst(var x: TItem; t: PType; v: int64); | |
| 116 | +var p: TPos; | |
| 117 | +begin | |
| 118 | + p := x.pos; | |
| 119 | + FillChar(x, SizeOf(x), 0); | |
| 120 | + x.pos := p; | |
| 121 | + x.mode := mConst; | |
| 122 | + x.typ := t; | |
| 123 | + x.val := v; | |
| 124 | +end; | |
| 125 | + | |
| 126 | +{ A real constant: untyped, unless typed later. Its value is an F64, | |
| 127 | + which for an F32 is one an F32 can hold. } | |
| 128 | +procedure MakeReal(var x: TItem; r: double); | |
| 129 | +begin | |
| 130 | + MakeConst(x, tRConst, 0); | |
| 131 | + Move(r, x.val, 8); | |
| 132 | +end; | |
| 133 | + | |
| 134 | +function IsF32(t: PType): boolean; | |
| 135 | +begin | |
| 136 | + IsF32 := (t^.form = fReal) and (t^.size = 4); | |
| 137 | +end; | |
| 138 | + | |
| 139 | +{ Gives a real constant the real type t, rounded to it. } | |
| 140 | +procedure RealTo(var x: TItem; t: PType); | |
| 141 | +var r: double; | |
| 142 | +begin | |
| 143 | + r := RealOf(x); | |
| 144 | + if IsF32(t) then MakeReal(x, single(r)) else MakeReal(x, r); | |
| 145 | + x.typ := t; | |
| 146 | +end; | |
| 147 | + | |
| 148 | +function RealOf(const x: TItem): double; | |
| 149 | +var r: double; | |
| 150 | +begin | |
| 151 | + Move(x.val, r, 8); | |
| 152 | + RealOf := r; | |
| 153 | +end; | |
| 154 | + | |
| 155 | +{ The current string literal, as an item of its own chunk. } | |
| 156 | +procedure MakeStr(var x: TItem); | |
| 157 | +begin | |
| 158 | + FillChar(x, SizeOf(x), 0); | |
| 159 | + x.mode := mStr; | |
| 160 | + x.typ := tStr; | |
| 161 | + x.val := slen; | |
| 162 | + x.chunk := NewChunk(secRodata, -1, -1); | |
| 163 | + SetData(x.chunk, @sbuf, slen + 1, 1); | |
| 164 | +end; | |
| 165 | + | |
| 166 | +function FrameAdr(off: int64): TAdr; | |
| 167 | +begin | |
| 168 | + FrameAdr.base := bFrame; | |
| 169 | + FrameAdr.chunk := -1; | |
| 170 | + FrameAdr.off := off; | |
| 171 | +end; | |
| 172 | + | |
| 173 | +function GlobalAdr(c: longint): TAdr; | |
| 174 | +begin | |
| 175 | + GlobalAdr.base := bGlobal; | |
| 176 | + GlobalAdr.chunk := c; | |
| 177 | + GlobalAdr.off := 0; | |
| 178 | +end; | |
| 179 | + | |
| 180 | +{ Where the variable x is, for gen } | |
| 181 | +function AdrOf(const x: TItem): TAdr; | |
| 182 | +begin | |
| 183 | + AdrOf.base := x.base; | |
| 184 | + AdrOf.chunk := x.chunk; | |
| 185 | + AdrOf.off := x.off; | |
| 186 | +end; | |
| 187 | + | |
| 188 | +{ An item for "found ...": a constant without a type of its own as it | |
| 189 | + is written, so that "found 1.5" says what is wrong; else its type. } | |
| 190 | +function What(const x: TItem): shortstring; | |
| 191 | +begin | |
| 192 | + if (x.mode = mConst) and ((x.typ = tConst) or (x.typ = tRConst)) then What := Source(x.pos) | |
| 193 | + else What := TypeName(x.typ); | |
| 194 | +end; | |
| 195 | + | |
| 196 | +{ Two operands of different types. Numbers convert, so say how. } | |
| 197 | +procedure Mismatch(const x, y: TItem; xt, yt: PType); | |
| 198 | +var msg: shortstring; | |
| 199 | +begin | |
| 200 | + msg := TypeName(xt) + ' and ' + TypeName(yt) + ' are different types'; | |
| 201 | + if (IsInt(xt) or (xt^.form = fReal)) and (IsInt(yt) or (yt^.form = fReal)) | |
| 202 | + and (xt <> tConst) and (xt <> tRConst) then | |
| 203 | + MarkHelp(Join(x.pos, y.pos), msg, 'convert one to the other''s type, as in ' + TypeName(xt) + '(' + Source(y.pos) + ')') | |
| 204 | + else MarkAt(Join(x.pos, y.pos), msg); | |
| 205 | +end; | |
| 206 | + | |
| 207 | +{ An item that is not a value, by what it is. } | |
| 208 | +procedure NotValue(const x: TItem); | |
| 209 | +begin | |
| 210 | + case x.mode of | |
| 211 | + mType: MarkAt(x.pos, TypeName(x.typ) + ' is a type, not a value'); | |
| 212 | + mBuiltin: MarkAt(x.pos, NameOf(x.obj^.name) + ' is a built-in procedure, not a value'); | |
| 213 | + mCase: MarkAt(x.pos, NameOf(x.obj^.name) + ' is a case, not a value'); | |
| 214 | + mNone: | |
| 215 | + if x.obj <> nil then MarkAt(x.pos, NameOf(x.obj^.name) + ' returns nothing') | |
| 216 | + else MarkAt(x.pos, 'the procedure returns nothing'); | |
| 217 | + else MarkAt(x.pos, 'a string can''t be an operand'); | |
| 218 | + end; | |
| 219 | +end; | |
| 220 | + | |
| 221 | +procedure MakeItem(var x: TItem; o: PObj); | |
| 222 | +begin | |
| 223 | + FillChar(x, SizeOf(x), 0); | |
| 224 | + x.typ := o^.typ; | |
| 225 | + x.obj := o; | |
| 226 | + case o^.cls of | |
| 227 | + oConst: | |
| 228 | + if o^.typ = tStr then begin | |
| 229 | + x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk | |
| 230 | + end else if o^.typ^.form = fArray then begin | |
| 231 | + { an array constant: a variable in read-only data, not one of | |
| 232 | + the program's, so nothing can change it } | |
| 233 | + x.mode := mVar; x.base := bGlobal; x.chunk := o^.chunk | |
| 234 | + end else begin | |
| 235 | + x.mode := mConst; x.val := o^.val | |
| 236 | + end; | |
| 237 | + oVar: begin | |
| 238 | + x.mode := mVar; | |
| 239 | + x.ro := (o^.lev = 0) and (o^.module <> curModule); | |
| 240 | + x.isVar := not x.ro; | |
| 241 | + if o^.ext then begin | |
| 242 | + PushVar(GlobalAdr(o^.chunk), 8, false, kWord); | |
| 243 | + x.base := bStack; | |
| 244 | + end else if o^.lev = 0 then begin | |
| 245 | + x.base := bGlobal; x.chunk := o^.chunk | |
| 246 | + end else if o^.ind then begin | |
| 247 | + x.base := bSlot; x.chunk := o^.val; x.off := o^.off; | |
| 248 | + end else begin | |
| 249 | + x.base := bFrame; x.off := o^.val | |
| 250 | + end; | |
| 251 | + end; | |
| 252 | + oProc: x.mode := mProc; | |
| 253 | + oType: x.mode := mType; | |
| 254 | + oBuiltin: x.mode := mBuiltin; | |
| 255 | + oCase: x.mode := mCase; | |
| 256 | + else Mark(NameOf(o^.name) + ' is not a value'); | |
| 257 | + end; | |
| 258 | +end; | |
| 259 | + | |
| 260 | +function ScalarSize(t: PType): longint; | |
| 261 | +begin | |
| 262 | + if t^.form in [fInt, fBool, fEnum, fSet, fReal] then ScalarSize := t^.size | |
| 263 | + else ScalarSize := 8; | |
| 264 | +end; | |
| 265 | + | |
| 266 | +{ The width an operation on t is brought back to, and whether signed } | |
| 267 | +function IntSize(t: PType): longint; | |
| 268 | +begin | |
| 269 | + if t^.form = fInt then IntSize := t^.size else IntSize := 8; | |
| 270 | +end; | |
| 271 | + | |
| 272 | +function Signed(t: PType): boolean; | |
| 273 | +begin | |
| 274 | + Signed := ((t^.form = fInt) and t^.signed) or (t^.form = fConst); | |
| 275 | +end; | |
| 276 | + | |
| 277 | +function Cut(v: int64; t: PType): int64; | |
| 278 | +begin | |
| 279 | + Cut := v; | |
| 280 | + if t^.form <> fInt then exit; | |
| 281 | + case t^.size of | |
| 282 | + 1: if t^.signed then Cut := shortint(v) else Cut := byte(v); | |
| 283 | + 2: if t^.signed then Cut := smallint(v) else Cut := word(v); | |
| 284 | + 4: if t^.signed then Cut := longint(v) else Cut := longword(v); | |
| 285 | + end; | |
| 286 | +end; | |
| 287 | + | |
| 288 | +{ Code computing with a real is noted on its chunk, at the first place, | |
| 289 | + for a target without reals (report 5.1) to refuse if the chunk turns | |
| 290 | + out to be used. A real moved whole, as a record's bytes are copied, | |
| 291 | + needs nothing the target lacks, and isn't noted. } | |
| 292 | +procedure UsesReals(const p: TPos); | |
| 293 | +begin | |
| 294 | + if (codeChunk >= 0) and not chunks^[codeChunk].reals then begin | |
| 295 | + chunks^[codeChunk].reals := true; | |
| 296 | + chunks^[codeChunk].realsAt := p; | |
| 297 | + end; | |
| 298 | +end; | |
| 299 | + | |
| 300 | +{ x's value, on top of the stack } | |
| 301 | +procedure Value(var x: TItem); | |
| 302 | +var f: single; | |
| 303 | +begin | |
| 304 | + if x.typ^.form = fReal then UsesReals(x.pos); | |
| 305 | + case x.mode of | |
| 306 | + mConst: | |
| 307 | + if (x.typ^.form = fError) and (x.val > 0) then PushAdr(GlobalAdr(x.val - 1)) | |
| 308 | + else if IsF32(x.typ) then begin | |
| 309 | + f := RealOf(x); | |
| 310 | + PushConst(kF32, PLongword(@f)^); | |
| 311 | + end else PushConst(KindOf(x.typ), x.val); | |
| 312 | + mVar: begin | |
| 313 | + if not IsScalar(x.typ) then MarkAt(x.pos, 'expected a single value, found ' + What(x)); | |
| 314 | + PushVar(AdrOf(x), ScalarSize(x.typ), (x.typ^.form = fInt) and x.typ^.signed, KindOf(x.typ)); | |
| 315 | + end; | |
| 316 | + mStack: ; | |
| 317 | + mProc: begin | |
| 318 | + if x.obj^.isCode and (x.obj^.typ^.base <> nil) and x.obj^.typ^.base^.tuple then | |
| 319 | + MarkAt(x.pos, 'a code procedure with two results can only be called'); | |
| 320 | + PushProc(x.obj^.chunk, x.obj^.ext); | |
| 321 | + end; | |
| 322 | + else NotValue(x); | |
| 323 | + end; | |
| 324 | + x.mode := mStack; | |
| 325 | +end; | |
| 326 | + | |
| 327 | +{ The address of the variable or string x, on top of the stack } | |
| 328 | +procedure Address(var x: TItem); | |
| 329 | +begin | |
| 330 | + if x.mode = mStr then PushAdr(GlobalAdr(x.chunk)) | |
| 331 | + else if x.mode = mVar then begin | |
| 332 | + PushAdr(AdrOf(x)); | |
| 333 | + x.base := bStack; x.off := 0; | |
| 334 | + end else MarkAt(x.pos, 'expected a variable'); | |
| 335 | +end; | |
| 336 | + | |
| 337 | +{ x isn't wanted: what it has on the stack is dropped } | |
| 338 | +procedure Discard(const x: TItem); | |
| 339 | +begin | |
| 340 | + if (x.mode = mStack) or ((x.mode = mVar) and (x.base = bStack)) then Drop; | |
| 341 | +end; | |
| 342 | + | |
| 343 | +{ Frames } | |
| 344 | + | |
| 345 | +procedure TempItem(var x: TItem; t: PType; off: int64); | |
| 346 | +var p: TPos; | |
| 347 | +begin | |
| 348 | + p := x.pos; | |
| 349 | + FillChar(x, SizeOf(x), 0); | |
| 350 | + x.pos := p; | |
| 351 | + x.mode := mVar; x.typ := t; x.base := bFrame; x.off := off; | |
| 352 | +end; | |
| 353 | + | |
| 354 | +function ToSlot(var x: TItem): int64; | |
| 355 | +var s: int64; | |
| 356 | +begin | |
| 357 | + Value(x); | |
| 358 | + s := AllocTemp(8, 8); | |
| 359 | + PopVar(FrameAdr(s), 8); | |
| 360 | + ToSlot := s; | |
| 361 | +end; | |
| 362 | + | |
| 363 | +{ Copying } | |
| 364 | + | |
| 365 | +procedure CopyToTemp(var x: TItem); | |
| 366 | +var t: int64; | |
| 367 | +begin | |
| 368 | + if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 369 | + t := AllocTemp(x.typ^.size, x.typ^.align); | |
| 370 | + Address(x); | |
| 371 | + CopyTo(FrameAdr(t), x.typ^.size); | |
| 372 | + TempItem(x, x.typ, t); | |
| 373 | +end; | |
| 374 | + | |
| 375 | +{ A slice made from an address on the stack and a length. } | |
| 376 | +procedure SliceOf(var x: TItem; t: PType; len: int64); | |
| 377 | +var s: int64; | |
| 378 | +begin | |
| 379 | + s := AllocTemp(16, 8); | |
| 380 | + PopVar(FrameAdr(s), 8); | |
| 381 | + PushConst(kWord, len); | |
| 382 | + PopVar(FrameAdr(s + 8), 8); | |
| 383 | + TempItem(x, t, s); | |
| 384 | +end; | |
| 385 | + | |
| 386 | +{ An INT goes silently into an I64, and a UINT into a U64, losing | |
| 387 | + nothing on any target (report section 5.10). } | |
| 388 | +function Widens(s, t: PType): boolean; | |
| 389 | +begin | |
| 390 | + Widens := ((s = tInt) and Same(t, tI64)) or ((s = tUInt) and Same(t, tU64)); | |
| 391 | +end; | |
| 392 | + | |
| 393 | +{ Another module's exported variable can be read, not changed, nor | |
| 394 | + handed to anything that could change it (report section 3). } | |
| 395 | +procedure NotReadOnly(const x: TItem); | |
| 396 | +begin | |
| 397 | + if x.ro then MarkAt(x.pos, Source(x.pos) + ' is ' + NameOf(x.obj^.module^.name) + '''s, and can''t be changed outside it'); | |
| 398 | +end; | |
| 399 | + | |
| 400 | +procedure Coerce(var x: TItem; t: PType); | |
| 401 | +var s: PType; tmp: int64; | |
| 402 | +begin | |
| 403 | + s := x.typ; | |
| 404 | + if x.mode = mStr then begin | |
| 405 | + if (t^.form = fSlice) and (t^.base = tU8) then begin | |
| 406 | + Address(x); SliceOf(x, t, x.val) | |
| 407 | + end else if (t^.form = fPtr) and (t^.base = tU8) then begin | |
| 408 | + Address(x); x.mode := mStack; x.typ := t | |
| 409 | + end else if (t^.form = fArray) and (t^.base = tU8) and (t^.len >= x.val) then begin | |
| 410 | + tmp := AllocTemp(t^.size, 1); | |
| 411 | + Address(x); | |
| 412 | + CopyTo(FrameAdr(tmp), x.val); | |
| 413 | + if t^.len > x.val then ZeroVar(FrameAdr(tmp + x.val), t^.len - x.val); | |
| 414 | + TempItem(x, t, tmp); | |
| 415 | + end else MarkAt(x.pos, 'can''t use a string as ' + TypeName(t)); | |
| 416 | + exit; | |
| 417 | + end; | |
| 418 | + if x.mode in [mType, mBuiltin, mNone, mCase] then NotValue(x); | |
| 419 | + if Same(s, t) then exit; | |
| 420 | + if Widens(s, t) then x.typ := t { the same bits, on a 64-bit target } | |
| 421 | + else if (s = tRConst) and (t^.form = fReal) then RealTo(x, t) | |
| 422 | + else if (s^.form = fConst) and (t^.form = fInt) then begin | |
| 423 | + if (x.mode = mConst) and not Fits(x.val, t) then MarkAt(x.pos, IntStr(x.val) + ' doesn''t fit ' + TypeName(t)); | |
| 424 | + x.typ := t; | |
| 425 | + end else if (s^.form = fNil) and (t^.form in [fPtr, fProc, fError]) then | |
| 426 | + x.typ := t | |
| 427 | + else if (s^.form = fPtr) and (t^.form = fPtr) and t^.vol and not s^.vol | |
| 428 | + and Same(s^.base, t^.base) then | |
| 429 | + x.typ := t | |
| 430 | + else if (s^.form = fArray) and (t^.form = fSlice) and Same(s^.base, t^.base) then begin | |
| 431 | + NotReadOnly(x); | |
| 432 | + if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); | |
| 433 | + if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); | |
| 434 | + Address(x); | |
| 435 | + SliceOf(x, t, s^.len); | |
| 436 | + end else | |
| 437 | + MarkAt(x.pos, 'can''t use ' + TypeName(s) + ' as ' + TypeName(t)); | |
| 438 | +end; | |
| 439 | + | |
| 440 | +{ Coerce where the types must be the same, as in a for loop, INC and | |
| 441 | + several results, which don't widen an INT to an I64 (report sections | |
| 442 | + 5.10, 6.1 and 7.2); help, if not empty, says why. } | |
| 443 | +procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); | |
| 444 | +var h: shortstring; | |
| 445 | +begin | |
| 446 | + if Widens(x.typ, t) then begin | |
| 447 | + h := help; | |
| 448 | + if h = '' then h := 'they don''t mix here: write ' + TypeName(t) + '(' + Source(x.pos) + ')'; | |
| 449 | + MarkHelp(x.pos, 'expected ' + TypeName(t) + ', found ' + What(x), h); | |
| 450 | + end; | |
| 451 | + Coerce(x, t); | |
| 452 | +end; | |
| 453 | + | |
| 454 | +{ dst := src. The address of dst, if it has to be computed, is on the | |
| 455 | + stack already, under src's. } | |
| 456 | +procedure Assign(var dst, src: TItem); | |
| 457 | +begin | |
| 458 | + Coerce(src, dst.typ); | |
| 459 | + if IsScalar(dst.typ) then begin | |
| 460 | + Value(src); | |
| 461 | + PopVar(AdrOf(dst), ScalarSize(dst.typ)); | |
| 462 | + end else begin | |
| 463 | + if dst.vol or src.vol then MarkAt(src.pos, 'can''t copy through a volatile pointer'); | |
| 464 | + Address(src); | |
| 465 | + if dst.base = bStack then CopyBlock(dst.typ^.size, dst.off) | |
| 466 | + else CopyTo(AdrOf(dst), dst.typ^.size); | |
| 467 | + end; | |
| 468 | +end; | |
| 469 | + | |
| 470 | +{ t's name, ending in a zero byte, for Rt.fail } | |
| 471 | +function NameChunk(t: PType): longint; | |
| 472 | +var s: shortstring; c: longint; | |
| 473 | +begin | |
| 474 | + s := TypeName(t) + #0; | |
| 475 | + c := NewChunk(secRodata, -1, -1); | |
| 476 | + SetData(c, @s[1], length(s), 1); | |
| 477 | + NameChunk := c; | |
| 478 | +end; | |
| 479 | + | |
| 480 | +{ Selectors } | |
| 481 | + | |
| 482 | +procedure Deref(var x: TItem); | |
| 483 | +var t: PType; | |
| 484 | +begin | |
| 485 | + t := x.typ; | |
| 486 | + if t^.form <> fPtr then MarkAt(x.pos, 'expected a pointer, found ' + TypeName(t)); | |
| 487 | + if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared'); | |
| 488 | + Value(x); | |
| 489 | + x.mode := mVar; x.base := bStack; x.off := 0; | |
| 490 | + x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; | |
| 491 | +end; | |
| 492 | + | |
| 493 | +{ A field of a packed record, of more than one byte's alignment, may | |
| 494 | + sit anywhere; it is read and written, but has no address and no | |
| 495 | + volatile access, which a machine that can't reach a misaligned word | |
| 496 | + couldn't make one access (report section 5.5). } | |
| 497 | +procedure Field(var x: TItem; f: PObj); | |
| 498 | +begin | |
| 499 | + if x.typ^.packd and (f^.typ^.align > 1) then begin | |
| 500 | + if x.vol then MarkAt(x.pos, 'a field of a packed record can''t be volatile'); | |
| 501 | + x.odd := true; | |
| 502 | + end; | |
| 503 | + x.off := x.off + f^.val; | |
| 504 | + x.typ := f^.typ; | |
| 505 | +end; | |
| 506 | + | |
| 507 | +procedure IndexPrep(var x: TItem); | |
| 508 | +begin | |
| 509 | + if x.mode = mStr then Coerce(x, tBytes); | |
| 510 | + if (x.typ^.form = fPtr) and (x.typ^.base^.form = fArray) then | |
| 511 | + MarkAt(x.pos, 'a pointer to an array is indexed as p^[i]'); | |
| 512 | + if x.typ^.form = fPtr then Value(x) | |
| 513 | + else if not ((x.mode = mVar) and (x.typ^.form in [fArray, fSlice])) then | |
| 514 | + MarkAt(x.pos, 'expected an array, a slice or a pointer, found ' + What(x)); | |
| 515 | +end; | |
| 516 | + | |
| 517 | +{ The index on top, times the size of an element } | |
| 518 | +procedure ScaleBy(s: int64); | |
| 519 | +begin | |
| 520 | + if s <> 1 then begin | |
| 521 | + PushConst(kWord, s); | |
| 522 | + gen.Op(sTimes, kWord, 8, true); | |
| 523 | + end; | |
| 524 | +end; | |
| 525 | + | |
| 526 | +procedure Index(var x, y: TItem); | |
| 527 | +var t: PType; s: int64; | |
| 528 | +begin | |
| 529 | + t := x.typ; | |
| 530 | + s := t^.base^.size; | |
| 531 | + if not IsInt(y.typ) then MarkAt(y.pos, 'an index is an integer, found ' + What(y)); | |
| 532 | + if t^.form = fPtr then begin | |
| 533 | + Value(y); | |
| 534 | + ScaleBy(s); | |
| 535 | + gen.Op(sPlus, kWord, 8, false); | |
| 536 | + x.mode := mVar; x.base := bStack; x.off := 0; | |
| 537 | + x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; | |
| 538 | + end else if (t^.form = fArray) and (y.mode = mConst) then begin | |
| 539 | + if (y.val < 0) or (y.val >= t^.len) then MarkAt(y.pos, 'index ' + IntStr(y.val) + ' is out of range for a length of ' + IntStr(t^.len)); | |
| 540 | + x.off := x.off + y.val * s; | |
| 541 | + end else if t^.form = fArray then begin | |
| 542 | + Value(y); | |
| 543 | + if checks then CheckIndex(t^.len, y.pos); | |
| 544 | + ScaleBy(s); | |
| 545 | + if x.base <> bStack then begin PushAdr(AdrOf(x)); x.off := 0 end; | |
| 546 | + gen.Op(sPlus, kWord, 8, false); | |
| 547 | + x.base := bStack; | |
| 548 | + end else begin | |
| 549 | + Value(y); | |
| 550 | + IndexSlice(AdrOf(x), s, checks, y.pos); | |
| 551 | + x.base := bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; | |
| 552 | + end; | |
| 553 | + x.typ := t^.base; | |
| 554 | +end; | |
| 555 | + | |
| 556 | +procedure SliceLo(var lo: TItem); | |
| 557 | +begin | |
| 558 | + if not IsInt(lo.typ) then MarkAt(lo.pos, 'an index is an integer, found ' + What(lo)); | |
| 559 | + Value(lo); | |
| 560 | +end; | |
| 561 | + | |
| 562 | +procedure Slice(var x, lo, hi: TItem); | |
| 563 | +var t, st: PType; tmp: int64; | |
| 564 | +begin | |
| 565 | + t := x.typ; | |
| 566 | + if not IsInt(hi.typ) then MarkAt(hi.pos, 'an index is an integer, found ' + What(hi)); | |
| 567 | + if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); | |
| 568 | + if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); | |
| 569 | + if t^.form = fArray then NotReadOnly(x); | |
| 570 | + Value(hi); | |
| 571 | + tmp := AllocTemp(16, 8); | |
| 572 | + if t^.form = fPtr then SlicePtr(t^.base^.size, tmp, checks, lo.pos) | |
| 573 | + else gen.Slice(AdrOf(x), t^.form = fArray, t^.len, t^.base^.size, tmp, checks, lo.pos); | |
| 574 | + st := NewType(fSlice, 16, 8); | |
| 575 | + st^.base := t^.base; | |
| 576 | + TempItem(x, st, tmp); | |
| 577 | +end; | |
| 578 | + | |
| 579 | +{ Operators } | |
| 580 | + | |
| 581 | +procedure Neg(var x: TItem); | |
| 582 | +begin | |
| 583 | + if x.typ^.form = fReal then begin | |
| 584 | + if x.mode = mConst then x.val := x.val xor low(int64) | |
| 585 | + else begin Value(x); gen.Neg(KindOf(x.typ), 8, false) end; | |
| 586 | + end else if IsInt(x.typ) then begin | |
| 587 | + if (x.mode = mConst) and (x.typ^.form = fConst) and (x.val = low(int64)) then | |
| 588 | + MarkAt(x.pos, 'the constant is too big for I64') | |
| 589 | + else if x.mode = mConst then x.val := Cut(-x.val, x.typ) | |
| 590 | + else begin Value(x); gen.Neg(kWord, IntSize(x.typ), Signed(x.typ)) end; | |
| 591 | + end else MarkAt(x.pos, 'can''t negate ' + TypeName(x.typ)); | |
| 592 | +end; | |
| 593 | + | |
| 594 | +procedure NotOp(var x: TItem); | |
| 595 | +begin | |
| 596 | + if IsInt(x.typ) then MarkHelp(x.pos, '"not" needs a BOOL, found ' + What(x), 'the bits of an integer flip with ~'); | |
| 597 | + if x.typ^.form <> fBool then MarkAt(x.pos, '"not" needs a BOOL, found ' + What(x)); | |
| 598 | + if x.mode = mConst then x.val := 1 - x.val | |
| 599 | + else begin Value(x); NotBool end; | |
| 600 | +end; | |
| 601 | + | |
| 602 | +procedure Complement(var x: TItem); | |
| 603 | +begin | |
| 604 | + if x.typ^.form = fBool then MarkHelp(x.pos, '"~" needs an integer, found ' + What(x), 'a BOOL is negated with not'); | |
| 605 | + if not IsInt(x.typ) then MarkAt(x.pos, '"~" needs an integer, found ' + What(x)); | |
| 606 | + if x.mode = mConst then x.val := Cut(not x.val, x.typ) | |
| 607 | + else begin Value(x); gen.Complement(IntSize(x.typ), Signed(x.typ)) end; | |
| 608 | +end; | |
| 609 | + | |
| 610 | +{ Arrays, slices and strings, which = and # compare element by element } | |
| 611 | +function IsSeq(t: PType): boolean; | |
| 612 | +begin | |
| 613 | + IsSeq := t^.form in [fStr, fArray, fSlice]; | |
| 614 | +end; | |
| 615 | + | |
| 616 | +function ElemOf(t: PType): PType; | |
| 617 | +begin | |
| 618 | + if t^.form = fStr then ElemOf := tU8 else ElemOf := t^.base; | |
| 619 | +end; | |
| 620 | + | |
| 621 | +{ An array, slice or string as a slice in a temporary, for comparing. } | |
| 622 | +procedure SeqSlice(var x: TItem); | |
| 623 | +var t: PType; | |
| 624 | +begin | |
| 625 | + t := NewType(fSlice, 16, 8); | |
| 626 | + t^.base := ElemOf(x.typ); | |
| 627 | + if x.typ^.form = fSlice then begin | |
| 628 | + x.typ := t; CopyToTemp(x) | |
| 629 | + end else begin | |
| 630 | + if x.mode = mStr then begin Address(x); SliceOf(x, t, x.val) end | |
| 631 | + else begin | |
| 632 | + if x.vol then MarkAt(x.pos, 'can''t compare through a volatile pointer'); | |
| 633 | + Address(x); SliceOf(x, t, x.typ^.len) | |
| 634 | + end; | |
| 635 | + end; | |
| 636 | +end; | |
| 637 | + | |
| 638 | +procedure Op1(op: TSym; var x: TItem; var L: longint); | |
| 639 | +var tmp: TItem; | |
| 640 | +begin | |
| 641 | + L := NoLabel; | |
| 642 | + if (op in [sAnd, sOr]) and (x.typ^.form = fBool) then begin | |
| 643 | + tmp := x; | |
| 644 | + Value(tmp); | |
| 645 | + if x.mode <> mConst then x := tmp; | |
| 646 | + JumpKeep(L, op = sOr); | |
| 647 | + end else if (op in [sEql, sNeq]) and IsSeq(x.typ) then | |
| 648 | + SeqSlice(x) | |
| 649 | + else if (op = sPlus) and (x.mode = mStr) then | |
| 650 | + { two string constants to be joined: nothing to compute } | |
| 651 | + else if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then | |
| 652 | + NotValue(x) | |
| 653 | + else if x.mode <> mConst then | |
| 654 | + Value(x); | |
| 655 | +end; | |
| 656 | + | |
| 657 | +function Log2(v: int64): longint; | |
| 658 | +var n: longint; | |
| 659 | +begin | |
| 660 | + n := 0; | |
| 661 | + while (v > 1) and (v and 1 = 0) do begin v := v shr 1; inc(n) end; | |
| 662 | + if v <> 1 then n := -1; | |
| 663 | + Log2 := n; | |
| 664 | +end; | |
| 665 | + | |
| 666 | +{ Whether a op b, for + - *, overflows an I64: an untyped constant | |
| 667 | + doesn't wrap, it is an error (report section 4). } | |
| 668 | +function Overflows(op: TSym; a, b: int64): boolean; | |
| 669 | +begin | |
| 670 | + case op of | |
| 671 | + sPlus: Overflows := ((b > 0) and (a > high(int64) - b)) or ((b < 0) and (a < low(int64) - b)); | |
| 672 | + sMinus: Overflows := ((b < 0) and (a > high(int64) + b)) or ((b > 0) and (a < low(int64) + b)); | |
| 673 | + sTimes: | |
| 674 | + if (a = 0) or (b = 0) then Overflows := false | |
| 675 | + else if a = -1 then Overflows := b = low(int64) | |
| 676 | + else if b = -1 then Overflows := a = low(int64) | |
| 677 | + else Overflows := (a * b) div b <> a; | |
| 678 | + else Overflows := false; | |
| 679 | + end; | |
| 680 | +end; | |
| 681 | + | |
| 682 | +{ A type's width in bits; an untyped constant's is an I64's } | |
| 683 | +function Bits(t: PType): longint; | |
| 684 | +begin | |
| 685 | + Bits := t^.size * 8; | |
| 686 | +end; | |
| 687 | + | |
| 688 | +function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64; | |
| 689 | +var u: boolean; | |
| 690 | +begin | |
| 691 | + u := (t^.form = fInt) and not t^.signed; | |
| 692 | + if (t^.form = fConst) and Overflows(op, a, b) then MarkAt(p, 'the constant is too big for I64'); | |
| 693 | + case op of | |
| 694 | + sPlus: FoldInt := a + b; | |
| 695 | + sMinus: FoldInt := a - b; | |
| 696 | + sTimes: FoldInt := a * b; | |
| 697 | + sDiv, sMod: begin | |
| 698 | + if b = 0 then MarkAt(p, 'division by zero'); | |
| 699 | + if u then begin | |
| 700 | + if op = sDiv then FoldInt := int64(qword(a) div qword(b)) | |
| 701 | + else FoldInt := int64(qword(a) mod qword(b)); | |
| 702 | + end else if (b = -1) and (a = int64(-1) shl (Bits(t) - 1)) then MarkAt(p, 'division overflows') | |
| 703 | + else if op = sDiv then FoldInt := a div b | |
| 704 | + else FoldInt := a mod b; | |
| 705 | + end; | |
| 706 | + sAmp: FoldInt := a and b; | |
| 707 | + sBar: FoldInt := a or b; | |
| 708 | + sXor: FoldInt := a xor b; | |
| 709 | + sShl: FoldInt := a shl (b and (Bits(t) - 1)); | |
| 710 | + sShr: if u then FoldInt := int64(qword(a) shr (b and (Bits(t) - 1))) | |
| 711 | + else FoldInt := SarInt64(a, b and (Bits(t) - 1)); | |
| 712 | + sEql: FoldInt := ord(a = b); | |
| 713 | + sNeq: FoldInt := ord(a <> b); | |
| 714 | + sLss: if u then FoldInt := ord(qword(a) < qword(b)) else FoldInt := ord(a < b); | |
| 715 | + sLeq: if u then FoldInt := ord(qword(a) <= qword(b)) else FoldInt := ord(a <= b); | |
| 716 | + sGtr: if u then FoldInt := ord(qword(a) > qword(b)) else FoldInt := ord(a > b); | |
| 717 | + sGeq: if u then FoldInt := ord(qword(a) >= qword(b)) else FoldInt := ord(a >= b); | |
| 718 | + else MarkAt(p, 'bad operator'); FoldInt := 0; | |
| 719 | + end; | |
| 720 | +end; | |
| 721 | + | |
| 722 | +{ Folds x op y, of the real type t: an F32 rounded at each step, as | |
| 723 | + the machine would. } | |
| 724 | +function FoldReal(op: TSym; const x, y: TItem; t: PType; var r: TItem): boolean; | |
| 725 | +var a, b: double; | |
| 726 | +begin | |
| 727 | + a := RealOf(x); b := RealOf(y); | |
| 728 | + FoldReal := true; | |
| 729 | + case op of | |
| 730 | + sPlus: MakeReal(r, a + b); | |
| 731 | + sMinus: MakeReal(r, a - b); | |
| 732 | + sTimes: MakeReal(r, a * b); | |
| 733 | + sSlash: MakeReal(r, a / b); | |
| 734 | + sEql: MakeConst(r, tBool, ord(a = b)); | |
| 735 | + sNeq: MakeConst(r, tBool, ord(a <> b)); | |
| 736 | + sLss: MakeConst(r, tBool, ord(a < b)); | |
| 737 | + sLeq: MakeConst(r, tBool, ord(a <= b)); | |
| 738 | + sGtr: MakeConst(r, tBool, ord(a > b)); | |
| 739 | + sGeq: MakeConst(r, tBool, ord(a >= b)); | |
| 740 | + else FoldReal := false; | |
| 741 | + end; | |
| 742 | + if r.typ^.form = fReal then RealTo(r, t); | |
| 743 | +end; | |
| 744 | + | |
| 745 | +{ and, or and not are for BOOLs; &, |, xor and ~ for the bits of | |
| 746 | + integers: x is the wrong kind for op, and the message names the | |
| 747 | + operator it may have meant } | |
| 748 | +procedure WrongLogic(op: TSym; var x: TItem); | |
| 749 | +const Bits = 'the bits of integers take '; | |
| 750 | +begin | |
| 751 | + case op of | |
| 752 | + sAnd: if IsInt(x.typ) then MarkHelp(x.pos, '"and" needs BOOLs, found ' + What(x), Bits + '&'); | |
| 753 | + sOr: if IsInt(x.typ) then MarkHelp(x.pos, '"or" needs BOOLs, found ' + What(x), Bits + '|'); | |
| 754 | + sAmp: MarkHelp(x.pos, '"&" needs integers, found ' + What(x), 'BOOLs take and'); | |
| 755 | + sBar: MarkHelp(x.pos, '"|" needs integers, found ' + What(x), 'BOOLs take or'); | |
| 756 | + sXor: MarkHelp(x.pos, '"xor" needs integers, found ' + What(x), 'BOOLs that differ are #'); | |
| 757 | + end; | |
| 758 | + MarkAt(x.pos, SymText(op) + ' needs BOOLs, found ' + What(x)); | |
| 759 | +end; | |
| 760 | + | |
| 761 | +{ x + y, of two string constants: one string, in a chunk of its own | |
| 762 | + (report section 4) } | |
| 763 | +procedure JoinStr(var x, y: TItem); | |
| 764 | +var p: PBytes; c: longint; | |
| 765 | +begin | |
| 766 | + if y.mode <> mStr then MarkAt(y.pos, '"+" joins two strings, found ' + What(y)); | |
| 767 | + p := GetMem(x.val + y.val + 1); | |
| 768 | + Move(rodata^[chunks^[x.chunk].start], p^[0], x.val); | |
| 769 | + Move(rodata^[chunks^[y.chunk].start], p^[x.val], y.val + 1); | |
| 770 | + c := NewChunk(secRodata, -1, -1); | |
| 771 | + SetData(c, p, x.val + y.val + 1, 1); | |
| 772 | + FreeMem(p); | |
| 773 | + x.chunk := c; | |
| 774 | + x.val := x.val + y.val; | |
| 775 | +end; | |
| 776 | + | |
| 777 | +{ A set's operations, as operations on its bits: the two values on top } | |
| 778 | +procedure SetOp(op: TSym); | |
| 779 | +begin | |
| 780 | + case op of | |
| 781 | + sPlus: gen.Op(sBar, kWord, 8, false); | |
| 782 | + sTimes: gen.Op(sAmp, kWord, 8, false); | |
| 783 | + sMinus: begin gen.Complement(8, false); gen.Op(sAmp, kWord, 8, false) end; | |
| 784 | + sLeq, sGeq: begin | |
| 785 | + { x <= y has no bit of x outside y } | |
| 786 | + if op = sGeq then Swap; | |
| 787 | + gen.Complement(8, false); | |
| 788 | + gen.Op(sAmp, kWord, 8, false); | |
| 789 | + PushConst(kWord, 0); | |
| 790 | + Compare(sEql, kWord, false); | |
| 791 | + end; | |
| 792 | + sIn: begin | |
| 793 | + Swap; | |
| 794 | + gen.Op(sShr, kWord, 8, false); | |
| 795 | + PushConst(kWord, 1); | |
| 796 | + gen.Op(sAmp, kWord, 8, false); | |
| 797 | + end; | |
| 798 | + else Compare(op, kWord, false); | |
| 799 | + end; | |
| 800 | +end; | |
| 801 | + | |
| 802 | +procedure Op2(op: TSym; var x, y: TItem; L: longint); | |
| 803 | +const | |
| 804 | + Rel = [sEql, sNeq, sLss, sLeq, sGtr, sGeq]; | |
| 805 | +var | |
| 806 | + xt, yt, t, e: PType; r: TItem; scale: longint; isPtr: boolean; a, b: int64; | |
| 807 | +begin | |
| 808 | + xt := x.typ; yt := y.typ; | |
| 809 | + if y.mode in [mType, mBuiltin, mNone, mCase] then NotValue(y); | |
| 810 | + if (op = sPlus) and (x.mode = mStr) then begin JoinStr(x, y); exit end; | |
| 811 | + | |
| 812 | + if op in [sAnd, sOr] then begin | |
| 813 | + if xt^.form <> fBool then WrongLogic(op, x); | |
| 814 | + if yt^.form <> fBool then WrongLogic(op, y); | |
| 815 | + if (x.mode = mConst) and (y.mode = mConst) then begin | |
| 816 | + Fix(L); | |
| 817 | + if op = sAnd then x.val := x.val and y.val else x.val := x.val or y.val; | |
| 818 | + exit; | |
| 819 | + end; | |
| 820 | + Value(y); | |
| 821 | + FixKeep(L); | |
| 822 | + x.mode := mStack; x.typ := tBool; | |
| 823 | + exit; | |
| 824 | + end; | |
| 825 | + | |
| 826 | + if (op in [sAmp, sBar, sXor]) and (xt^.form = fBool) then WrongLogic(op, x); | |
| 827 | + if (op in [sAmp, sBar, sXor]) and (yt^.form = fBool) then WrongLogic(op, y); | |
| 828 | + | |
| 829 | + { the lengths, then the bytes, LEN * SIZE(T) of them: the elements | |
| 830 | + are of types whose bytes say all there is to their values } | |
| 831 | + if (op in [sEql, sNeq]) and IsSeq(xt) then begin | |
| 832 | + e := ElemOf(xt); | |
| 833 | + if not IsSeq(yt) then | |
| 834 | + MarkAt(Join(x.pos, y.pos), 'can''t compare an array with ' + TypeName(yt)) | |
| 835 | + else if not Same(e, ElemOf(yt)) then | |
| 836 | + MarkAt(Join(x.pos, y.pos), 'can''t compare an array of ' + TypeName(e) + ' with one of ' + TypeName(ElemOf(yt))); | |
| 837 | + if e^.form = fReal then | |
| 838 | + MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of reals with ' + SymText(op), | |
| 839 | + '-0.0 = 0.0 and a NaN equals nothing, though their bytes say otherwise: write a loop') | |
| 840 | + else if not (e^.form in [fInt, fBool, fEnum, fSet, fPtr]) then | |
| 841 | + MarkHelp(Join(x.pos, y.pos), 'can''t compare arrays of ' + TypeName(e) + ' with ' + SymText(op), 'write a loop'); | |
| 842 | + SeqSlice(y); | |
| 843 | + CompareSeq(x.off, y.off, Log2(e^.size), op = sEql); | |
| 844 | + MakeConst(x, tBool, 0); | |
| 845 | + x.mode := mStack; | |
| 846 | + exit; | |
| 847 | + end; | |
| 848 | + | |
| 849 | + { the type of the result, and the checks } | |
| 850 | + t := nil; scale := 1; isPtr := false; | |
| 851 | + if IsInt(xt) and IsInt(yt) then begin | |
| 852 | + if op = sSlash then MarkHelp(Join(x.pos, y.pos), '"/" divides reals, not integers', 'to divide integers, write ' + Source(x.pos) + ' div ' + Source(y.pos)); | |
| 853 | + if op in [sShl, sShr] then begin | |
| 854 | + if xt^.form = fConst then t := tConst else t := xt | |
| 855 | + end else if (xt^.form = fConst) and (yt^.form = fConst) then t := tConst | |
| 856 | + else if xt^.form = fConst then begin | |
| 857 | + if (x.mode = mConst) and not Fits(x.val, yt) then MarkAt(x.pos, IntStr(x.val) + ' doesn''t fit ' + TypeName(yt)); | |
| 858 | + t := yt | |
| 859 | + end else if yt^.form = fConst then begin | |
| 860 | + if (y.mode = mConst) and not Fits(y.val, xt) then MarkAt(y.pos, IntStr(y.val) + ' doesn''t fit ' + TypeName(xt)); | |
| 861 | + t := xt | |
| 862 | + end else if Same(xt, yt) then t := xt | |
| 863 | + else Mismatch(x, y, xt, yt); | |
| 864 | + if not (op in Rel + [sPlus, sMinus, sTimes, sDiv, sMod, sAmp, sBar, sXor, sShl, sShr]) then | |
| 865 | + MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(t)); | |
| 866 | + end else if (xt^.form = fReal) and (yt^.form = fReal) then begin | |
| 867 | + if not (op in Rel + [sPlus, sMinus, sTimes, sSlash]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 868 | + if xt = tRConst then t := yt | |
| 869 | + else if (yt = tRConst) or Same(xt, yt) then t := xt | |
| 870 | + else Mismatch(x, y, xt, yt); | |
| 871 | + if (x.mode = mConst) and (x.typ <> t) then RealTo(x, t); | |
| 872 | + if (y.mode = mConst) and (y.typ <> t) then RealTo(y, t); | |
| 873 | + end else if (xt^.form = fBool) and (yt^.form = fBool) then begin | |
| 874 | + if not (op in [sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 875 | + t := tBool; | |
| 876 | + end else if (xt^.form = fPtr) and IsInt(yt) and (op in [sPlus, sMinus]) then begin | |
| 877 | + t := xt; scale := xt^.base^.size; isPtr := true; | |
| 878 | + if y.mode = mConst then y.typ := tInt; | |
| 879 | + end else if (xt^.form = fPtr) and (yt^.form = fPtr) and (op = sMinus) then begin | |
| 880 | + if not Same(xt, yt) then Mismatch(x, y, xt, yt); | |
| 881 | + t := tInt; scale := xt^.base^.size; isPtr := true; | |
| 882 | + end else if (xt^.form in [fPtr, fProc, fNil, fError]) and (yt^.form in [fPtr, fProc, fNil, fError]) then begin | |
| 883 | + if not (op in Rel) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 884 | + if (xt^.form in [fProc, fError]) or (yt^.form in [fProc, fError]) then | |
| 885 | + if not (op in [sEql, sNeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 886 | + if (xt^.form <> fNil) and (yt^.form <> fNil) and not Same(xt, yt) then | |
| 887 | + if not ((xt^.form = fPtr) and (yt^.form = fPtr) and Same(xt^.base, yt^.base)) then | |
| 888 | + Mismatch(x, y, xt, yt); | |
| 889 | + t := tU64; | |
| 890 | + end else if (xt^.form = fEnum) and Same(xt, yt) then begin | |
| 891 | + if not (op in Rel) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 892 | + t := xt; | |
| 893 | + end else if (xt^.form = fSet) and Same(xt, yt) then begin | |
| 894 | + if not (op in [sPlus, sTimes, sMinus, sEql, sNeq, sLeq, sGeq]) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 895 | + t := xt; | |
| 896 | + end else if (op = sIn) and (yt^.form = fSet) and Same(xt, yt^.base) then | |
| 897 | + t := yt | |
| 898 | + else | |
| 899 | + Mismatch(x, y, xt, yt); | |
| 900 | + | |
| 901 | + { constants } | |
| 902 | + if (x.mode = mConst) and (y.mode = mConst) and not isPtr then begin | |
| 903 | + if t^.form = fReal then begin | |
| 904 | + if not FoldReal(op, x, y, t, r) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); | |
| 905 | + x := r; | |
| 906 | + end else if t^.form = fSet then begin | |
| 907 | + a := x.val; b := y.val; | |
| 908 | + case op of | |
| 909 | + sPlus: MakeConst(x, t, a or b); | |
| 910 | + sTimes: MakeConst(x, t, a and b); | |
| 911 | + sMinus: MakeConst(x, t, a and not b); | |
| 912 | + sIn: MakeConst(x, tBool, (b shr a) and 1); | |
| 913 | + sLeq: MakeConst(x, tBool, ord(a and not b = 0)); | |
| 914 | + sGeq: MakeConst(x, tBool, ord(b and not a = 0)); | |
| 915 | + else MakeConst(x, tBool, ord((a = b) = (op = sEql))); | |
| 916 | + end; | |
| 917 | + end else if op in Rel then | |
| 918 | + MakeConst(x, tBool, FoldInt(op, x.val, y.val, t, Join(x.pos, y.pos))) | |
| 919 | + else | |
| 920 | + MakeConst(x, t, Cut(FoldInt(op, x.val, y.val, t, Join(x.pos, y.pos)), t)); | |
| 921 | + exit; | |
| 922 | + end; | |
| 923 | + | |
| 924 | + { x and y on the stack, x under y; a constant x goes on after y } | |
| 925 | + Value(y); | |
| 926 | + if x.mode = mConst then begin Value(x); Swap end; | |
| 927 | + | |
| 928 | + if t^.form = fReal then begin | |
| 929 | + if op in Rel then Compare(op, KindOf(t), false) else gen.Op(op, KindOf(t), 8, false) | |
| 930 | + end else if t^.form = fSet then | |
| 931 | + SetOp(op) | |
| 932 | + else if op in Rel then | |
| 933 | + Compare(op, kWord, Signed(t)) | |
| 934 | + else if isPtr then begin | |
| 935 | + if t <> tInt then ScaleBy(scale); | |
| 936 | + gen.Op(op, kWord, 8, false); | |
| 937 | + if (t = tInt) and (scale <> 1) then begin | |
| 938 | + PushConst(kWord, scale); | |
| 939 | + gen.Op(sDiv, kWord, 8, true); | |
| 940 | + end; | |
| 941 | + end else | |
| 942 | + gen.Op(op, KindOf(t), IntSize(t), Signed(t)); | |
| 943 | + x.mode := mStack; | |
| 944 | + if op in Rel + [sIn] then x.typ := tBool else x.typ := t; | |
| 945 | +end; | |
| 946 | + | |
| 947 | +{ INT and UINT, the integers as big as an address, through which | |
| 948 | + integers and pointers convert (report section 6.7) } | |
| 949 | +function IsWord(t: PType): boolean; | |
| 950 | +begin | |
| 951 | + IsWord := Same(t, tInt) or Same(t, tUInt); | |
| 952 | +end; | |
| 953 | + | |
| 954 | +procedure Convert(var x: TItem; t: PType); | |
| 955 | +var s: PType; r: double; | |
| 956 | +begin | |
| 957 | + s := x.typ; | |
| 958 | + if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then begin | |
| 959 | + if x.mode = mStr then MarkAt(x.pos, 'can''t convert a string'); | |
| 960 | + NotValue(x); | |
| 961 | + end; | |
| 962 | + if Same(s, t) then exit; | |
| 963 | + if (IsInt(s) or (s^.form in [fEnum, fSet])) and (t^.form = fInt) then begin | |
| 964 | + if x.mode = mConst then x.val := Cut(x.val, t) | |
| 965 | + else begin Value(x); Narrow(t^.size, t^.signed) end; | |
| 966 | + end else if IsInt(s) and (t^.form = fEnum) then begin | |
| 967 | + if x.mode = mConst then begin | |
| 968 | + if (x.val < 0) or (x.val >= t^.len) then MarkAt(x.pos, IntStr(x.val) + ' isn''t a position of ' + TypeName(t)) | |
| 969 | + end else begin Value(x); CheckEnum(t^.len, NameChunk(t), x.pos) end; | |
| 970 | + end else if IsInt(s) and (t^.form = fSet) then begin | |
| 971 | + if x.mode = mConst then begin | |
| 972 | + if t^.len < 64 then x.val := x.val and ((int64(1) shl t^.len) - 1) | |
| 973 | + end else begin | |
| 974 | + Value(x); | |
| 975 | + if t^.len < 64 then begin | |
| 976 | + PushConst(kWord, (int64(1) shl t^.len) - 1); | |
| 977 | + gen.Op(sAmp, kWord, 8, false); | |
| 978 | + end; | |
| 979 | + end; | |
| 980 | + end else if IsInt(s) and (t^.form = fReal) then begin | |
| 981 | + if x.mode = mConst then begin | |
| 982 | + if (s^.form = fInt) and not s^.signed then r := qword(x.val) else r := x.val; | |
| 983 | + MakeReal(x, r); | |
| 984 | + RealTo(x, t); | |
| 985 | + end else begin | |
| 986 | + Value(x); | |
| 987 | + IntToReal(KindOf(t), (s^.form = fInt) and not s^.signed and (s^.size = 8)); | |
| 988 | + end; | |
| 989 | + end else if (s^.form = fReal) and (t^.form = fReal) then begin | |
| 990 | + { F32 to F64, F64 to F32, or a real constant to either } | |
| 991 | + if x.mode = mConst then RealTo(x, t) | |
| 992 | + else begin Value(x); RealToReal(KindOf(s)) end; | |
| 993 | + end else if (s^.form = fReal) and (t^.form = fInt) then begin | |
| 994 | + if x.mode = mConst then begin | |
| 995 | + r := RealOf(x); | |
| 996 | + if (r <> r) or (r >= 9.2233720368547758e18) or (r < -9.2233720368547758e18) then | |
| 997 | + x.val := Cut(low(int64), t) | |
| 998 | + else x.val := Cut(trunc(r), t); | |
| 999 | + end else begin | |
| 1000 | + Value(x); RealToInt(KindOf(s)); Narrow(t^.size, t^.signed) | |
| 1001 | + end; | |
| 1002 | + end else if (IsWord(s) or (s^.form in [fConst, fPtr, fProc, fNil])) and (t^.form in [fPtr, fProc]) then begin | |
| 1003 | + if x.mode <> mConst then Value(x); | |
| 1004 | + end else if (s^.form in [fPtr, fProc]) and IsWord(t) then | |
| 1005 | + Value(x) | |
| 1006 | + else | |
| 1007 | + MarkAt(x.pos, 'can''t convert ' + TypeName(s) + ' to ' + TypeName(t)); | |
| 1008 | + x.typ := t; | |
| 1009 | +end; | |
| 1010 | + | |
| 1011 | +{ Calls } | |
| 1012 | + | |
| 1013 | +procedure BeginCall(var c: TCall; var x: TItem); | |
| 1014 | +begin | |
| 1015 | + if x.mode = mProc then begin | |
| 1016 | + c.sig := x.obj^.typ; c.chunk := x.obj^.chunk; c.target := 0; | |
| 1017 | + c.obj := x.obj; c.code := x.obj^.isCode; c.ext := x.obj^.ext; | |
| 1018 | + end else begin | |
| 1019 | + c.obj := nil; c.code := false; c.ext := false; | |
| 1020 | + if x.typ^.form <> fProc then MarkAt(x.pos, 'expected a procedure, found ' + What(x)); | |
| 1021 | + c.sig := x.typ; c.chunk := -1; | |
| 1022 | + c.target := ToSlot(x); | |
| 1023 | + end; | |
| 1024 | + c.param := c.sig^.fields; | |
| 1025 | + c.n := 0; | |
| 1026 | +end; | |
| 1027 | + | |
| 1028 | +{ Too many arguments or too few, at p } | |
| 1029 | +procedure WrongCount(var c: TCall; const p: TPos; const what: shortstring); | |
| 1030 | +begin | |
| 1031 | + if c.obj = nil then MarkAt(p, what); | |
| 1032 | + Error(p, what + ': ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); | |
| 1033 | + if c.obj^.module <> nil then Note(c.obj^.pos, 'the signature is here'); | |
| 1034 | + Fail; | |
| 1035 | +end; | |
| 1036 | + | |
| 1037 | +procedure PassArg(var c: TCall; var a: TItem); | |
| 1038 | +var p: PObj; s: int64; | |
| 1039 | +begin | |
| 1040 | + p := c.param; | |
| 1041 | + if c.n > High(c.slots) then MarkAt(a.pos, 'too many arguments'); | |
| 1042 | + if p = nil then begin | |
| 1043 | + { past the parameters of a C procedure that ends in ...: a string | |
| 1044 | + goes as a C string, an integer constant as an INT, a real | |
| 1045 | + constant as an F64; C takes no F32 there } | |
| 1046 | + if not c.sig^.variadic then WrongCount(c, a.pos, 'too many arguments'); | |
| 1047 | + if a.mode = mStr then Coerce(a, tPtrU8) | |
| 1048 | + else if a.typ^.form = fConst then Coerce(a, tInt) | |
| 1049 | + else if a.typ = tRConst then Coerce(a, tReal) | |
| 1050 | + else if IsF32(a.typ) then MarkAt(a.pos, 'an F32 goes to ... as F64(x)') | |
| 1051 | + else if not IsScalar(a.typ) then MarkAt(a.pos, 'only a single value goes to ...'); | |
| 1052 | + c.isF[c.n] := a.typ^.form = fReal; | |
| 1053 | + c.slots[c.n] := ToSlot(a); | |
| 1054 | + inc(c.n); | |
| 1055 | + exit; | |
| 1056 | + end; | |
| 1057 | + c.isF[c.n] := (p^.typ^.form = fReal) and not p^.isVar; | |
| 1058 | + if p^.isVar then begin | |
| 1059 | + NotReadOnly(a); | |
| 1060 | + if (a.mode <> mVar) or not a.isVar then MarkAt(a.pos, 'a var parameter needs a variable'); | |
| 1061 | + if a.odd then MarkAt(a.pos, 'a field of a packed record can''t be a var parameter'); | |
| 1062 | + if not Same(a.typ, p^.typ) then MarkAt(a.pos, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a)); | |
| 1063 | + if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); | |
| 1064 | + Address(a); | |
| 1065 | + s := AllocTemp(8, 8); | |
| 1066 | + PopVar(FrameAdr(s), 8); | |
| 1067 | + end else if IsAggregate(p^.typ) then begin | |
| 1068 | + Coerce(a, p^.typ); | |
| 1069 | + if a.isVar or a.vol or (a.base <> bFrame) then CopyToTemp(a); | |
| 1070 | + Address(a); | |
| 1071 | + s := AllocTemp(8, 8); | |
| 1072 | + PopVar(FrameAdr(s), 8); | |
| 1073 | + end else begin | |
| 1074 | + Coerce(a, p^.typ); | |
| 1075 | + s := ToSlot(a); | |
| 1076 | + end; | |
| 1077 | + c.slots[c.n] := s; | |
| 1078 | + inc(c.n); | |
| 1079 | + c.param := p^.next; | |
| 1080 | +end; | |
| 1081 | + | |
| 1082 | +procedure EndCall(var c: TCall; var x: TItem); | |
| 1083 | +var res: PType; | |
| 1084 | +begin | |
| 1085 | + if c.param <> nil then WrongCount(c, TokPos, 'too few arguments'); | |
| 1086 | + Call(c); | |
| 1087 | + res := c.sig^.base; | |
| 1088 | + FillChar(x, SizeOf(x), 0); | |
| 1089 | + if res = nil then begin | |
| 1090 | + x.mode := mNone; x.typ := tNone; x.obj := c.obj | |
| 1091 | + end else if c.ret <> 0 then | |
| 1092 | + TempItem(x, res, c.ret) | |
| 1093 | + else begin | |
| 1094 | + x.mode := mStack; x.typ := res; | |
| 1095 | + end; | |
| 1096 | +end; | |
| 1097 | + | |
| 1098 | +{ Variants } | |
| 1099 | + | |
| 1100 | +{ A temporary of type t, all zero: for variants and record literals. } | |
| 1101 | +procedure ZeroTemp(var x: TItem; t: PType); | |
| 1102 | +var tmp: int64; | |
| 1103 | +begin | |
| 1104 | + tmp := AllocTemp(t^.size, t^.align); | |
| 1105 | + ZeroVar(FrameAdr(tmp), t^.size); | |
| 1106 | + TempItem(x, t, tmp); | |
| 1107 | +end; | |
| 1108 | + | |
| 1109 | +procedure Construct(var x: TItem; c: PObj); | |
| 1110 | +begin | |
| 1111 | + ZeroTemp(x, c^.typ); | |
| 1112 | + PushConst(kWord, c^.val); | |
| 1113 | + PopVar(FrameAdr(x.off), 8); | |
| 1114 | +end; | |
| 1115 | + | |
| 1116 | +procedure SetField(const x: TItem; f: PObj; var a: TItem); | |
| 1117 | +begin | |
| 1118 | + Coerce(a, f^.typ); | |
| 1119 | + if IsScalar(f^.typ) then begin | |
| 1120 | + Value(a); | |
| 1121 | + PopVar(FrameAdr(x.off + f^.val), ScalarSize(f^.typ)); | |
| 1122 | + end else begin | |
| 1123 | + if a.vol then MarkAt(a.pos, 'can''t copy through a volatile pointer'); | |
| 1124 | + Address(a); | |
| 1125 | + CopyTo(FrameAdr(x.off + f^.val), f^.typ^.size); | |
| 1126 | + end; | |
| 1127 | +end; | |
| 1128 | + | |
| 1129 | +{ The slot match tests: a variant's address, or an enumeration's or an | |
| 1130 | + integer's value. } | |
| 1131 | +function MatchBegin(var x: TItem): int64; | |
| 1132 | +var p: int64; | |
| 1133 | +begin | |
| 1134 | + if x.typ^.form in [fEnum, fInt] then exit(ToSlot(x)); | |
| 1135 | + if (x.mode <> mVar) or (x.typ^.form <> fVariant) then MarkAt(x.pos, 'match needs a variant, an enumeration or an integer, found ' + What(x)); | |
| 1136 | + if x.ro then CopyToTemp(x); { its arms' names stand for a copy's fields } | |
| 1137 | + Address(x); | |
| 1138 | + p := AllocTemp(8, 8); | |
| 1139 | + PopVar(FrameAdr(p), 8); | |
| 1140 | + MatchBegin := p; | |
| 1141 | +end; | |
| 1142 | + | |
| 1143 | +{ To L unless the tag of the variant whose address is in slot p, or the | |
| 1144 | + enumeration in it, is tag } | |
| 1145 | +procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean); | |
| 1146 | +var a: TAdr; | |
| 1147 | +begin | |
| 1148 | + PushVar(FrameAdr(p), 8, false, kWord); | |
| 1149 | + if not isEnum then begin | |
| 1150 | + a.base := bStack; a.chunk := -1; a.off := 0; | |
| 1151 | + PushVar(a, 8, false, kWord); | |
| 1152 | + end; | |
| 1153 | + PushConst(kWord, tag); | |
| 1154 | + JumpCmp(sNeq, false, L); | |
| 1155 | +end; | |
| 1156 | + | |
| 1157 | +{ To body if the integer in slot p is from lo to hi: less its lo, it is | |
| 1158 | + then at most hi - lo, compared unsigned, which holds for a signed type | |
| 1159 | + or not, with no second jump. } | |
| 1160 | +procedure MatchRange(p, lo, hi: int64; var body: longint); | |
| 1161 | +begin | |
| 1162 | + PushVar(FrameAdr(p), 8, false, kWord); | |
| 1163 | + if lo = hi then begin | |
| 1164 | + PushConst(kWord, lo); | |
| 1165 | + JumpCmp(sEql, false, body); | |
| 1166 | + exit; | |
| 1167 | + end; | |
| 1168 | + if lo <> 0 then begin | |
| 1169 | + PushConst(kWord, lo); | |
| 1170 | + gen.Op(sMinus, kWord, 8, false); | |
| 1171 | + end; | |
| 1172 | + PushConst(kWord, hi - lo); | |
| 1173 | + JumpCmp(sLeq, false, body); | |
| 1174 | +end; | |
| 1175 | + | |
| 1176 | +{ The end of a round of a for loop: stop if the step would pass the | |
| 1177 | + limit, else step. Counting the distance left, rather than comparing | |
| 1178 | + after the step, can't be fooled by the variable wrapping around. } | |
| 1179 | +procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); | |
| 1180 | +var v, w: TItem; | |
| 1181 | +begin | |
| 1182 | + MakeItem(w, o); | |
| 1183 | + if step > 0 then begin | |
| 1184 | + PushVar(FrameAdr(bslot), 8, false, kWord); | |
| 1185 | + Value(w); | |
| 1186 | + end else begin | |
| 1187 | + Value(w); | |
| 1188 | + PushVar(FrameAdr(bslot), 8, false, kWord); | |
| 1189 | + end; | |
| 1190 | + gen.Op(sMinus, kWord, 8, false); | |
| 1191 | + PushConst(kWord, abs(step)); | |
| 1192 | + JumpCmp(sLss, false, exitL); | |
| 1193 | + MakeItem(v, o); | |
| 1194 | + MakeItem(w, o); | |
| 1195 | + Value(w); | |
| 1196 | + PushConst(kWord, step); | |
| 1197 | + gen.Op(sPlus, kWord, IntSize(o^.typ), Signed(o^.typ)); | |
| 1198 | + Assign(v, w); | |
| 1199 | + JumpBack(top); | |
| 1200 | +end; | |
| 1201 | + | |
| 1202 | +procedure CondJump(var x: TItem; var L: longint); | |
| 1203 | +begin | |
| 1204 | + if x.typ^.form <> fBool then MarkAt(x.pos, 'a condition is a BOOL, found ' + What(x)); | |
| 1205 | + Value(x); | |
| 1206 | + JumpIf(L, false); | |
| 1207 | +end; | |
| 1208 | + | |
| 1209 | +{ Procedures } | |
| 1210 | + | |
| 1211 | +procedure BeginProc(sig: PType; params: PObj); | |
| 1212 | +begin | |
| 1213 | + result := sig^.base; | |
| 1214 | + gen.BeginProc(sig, params); | |
| 1215 | +end; | |
| 1216 | + | |
| 1217 | +{ The result of return x: a value on top, or one copied behind the | |
| 1218 | + hidden pointer } | |
| 1219 | +procedure ReturnValue(var x: TItem); | |
| 1220 | +begin | |
| 1221 | + if result = nil then MarkAt(x.pos, 'this procedure returns nothing'); | |
| 1222 | + Coerce(x, result); | |
| 1223 | + if IsAggregate(result) then begin | |
| 1224 | + if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 1225 | + Address(x); | |
| 1226 | + CopyResult(0, result^.size); | |
| 1227 | + end else Value(x); | |
| 1228 | +end; | |
| 1229 | + | |
| 1230 | +{ One of several results, into its place behind the hidden pointer. } | |
| 1231 | +procedure ReturnField(f: PObj; var x: TItem); | |
| 1232 | +begin | |
| 1233 | + Coerce(x, f^.typ); | |
| 1234 | + if IsScalar(f^.typ) then begin | |
| 1235 | + Value(x); | |
| 1236 | + ResultTo(f^.val, ScalarSize(f^.typ)); | |
| 1237 | + end else begin | |
| 1238 | + if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); | |
| 1239 | + Address(x); | |
| 1240 | + CopyResult(f^.val, f^.typ^.size); | |
| 1241 | + end; | |
| 1242 | +end; | |
| 1243 | + | |
| 1244 | +{ Several results at once, the call's that return f(x) passes on. } | |
| 1245 | +procedure ReturnAll(var x: TItem); | |
| 1246 | +begin | |
| 1247 | + Address(x); | |
| 1248 | + CopyResult(0, result^.size); | |
| 1249 | +end; | |
| 1250 | + | |
| 1251 | +procedure ReturnNone; | |
| 1252 | +begin | |
| 1253 | + if result <> nil then Mark('return needs a result of ' + TypeName(result)); | |
| 1254 | + ReturnJump; | |
| 1255 | +end; | |
| 1256 | + | |
| 1257 | +{ Keeps a variable's address in a frame slot, rather than on the stack, | |
| 1258 | + while other code runs. } | |
| 1259 | +procedure Pin(var x: TItem); | |
| 1260 | +var s: int64; | |
| 1261 | +begin | |
| 1262 | + if (x.mode = mVar) and (x.base = bStack) then begin | |
| 1263 | + Address(x); | |
| 1264 | + s := AllocTemp(8, 8); | |
| 1265 | + PopVar(FrameAdr(s), 8); | |
| 1266 | + x.base := bSlot; | |
| 1267 | + x.chunk := s; | |
| 1268 | + end; | |
| 1269 | +end; | |
| 1270 | + | |
| 1271 | +{ INC(v, k) or DEC: v's address is on the stack, from Address. } | |
| 1272 | +procedure IncDec(var v, k: TItem; dec: boolean); | |
| 1273 | +begin | |
| 1274 | + CoerceExact(k, v.typ, ''); | |
| 1275 | + Value(k); | |
| 1276 | + gen.IncDec(ScalarSize(v.typ), v.typ^.signed, dec); | |
| 1277 | +end; | |
| 1278 | + | |
| 1279 | +{ An error is the address of its length, followed by its message. } | |
| 1280 | +function ErrorChunk(var x: TItem; module, name: longint): longint; | |
| 1281 | +var c: longint; p: PBytes; | |
| 1282 | +begin | |
| 1283 | + if x.mode <> mStr then MarkAt(x.pos, 'an error is made from a string'); | |
| 1284 | + p := GetMem(x.val + 8); | |
| 1285 | + PInt64(p)^ := x.val; | |
| 1286 | + Move(rodata^[chunks^[x.chunk].start], p^[8], x.val); | |
| 1287 | + c := NewChunk(secRodata, module, name); | |
| 1288 | + SetData(c, p, x.val + 8, 8); | |
| 1289 | + FreeMem(p); | |
| 1290 | + ErrorChunk := c; | |
| 1291 | +end; | |
| 1292 | + | |
| 1293 | +procedure Msg(var x: TItem); | |
| 1294 | +var s: int64; | |
| 1295 | +begin | |
| 1296 | + Coerce(x, tError); | |
| 1297 | + Value(x); | |
| 1298 | + s := AllocTemp(16, 8); | |
| 1299 | + ErrorText(s); | |
| 1300 | + TempItem(x, tBytes, s); | |
| 1301 | +end; | |
| 1302 | + | |
| 1303 | +end. |
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | loader's other tables come at the end of rodata; elf.pas writes them |
| 13 | 13 | in the room left there. |
| 14 | 14 | |
| 15 | - Depends on: x64 for chunks and fixups, scan for names and errors. | |
| 15 | + Depends on: chunk for chunks and fixups, scan for names and errors. | |
| 16 | 16 | Leaves out: the file format; that is elf.pas. } |
| 17 | 17 | |
| 18 | 18 | unit link; |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | |
| 22 | 22 | interface |
| 23 | 23 | |
| 24 | -uses scan, diag, x64; | |
| 24 | +uses scan, diag, chunk; | |
| 25 | 25 | |
| 26 | 26 | const |
| 27 | 27 | Base = $400000; |
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | are read for their declarations only, and must export the same |
| 12 | 12 | interface. |
| 13 | 13 | |
| 14 | - Depends on: scan, sym, x64, gen. | |
| 14 | + Depends on: scan, sym, chunk for data, item and gen for code. | |
| 15 | 15 | Leaves out: linking; a module only records the libraries it names, |
| 16 | 16 | and whether it uses C or says link static. } |
| 17 | 17 | |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | |
| 22 | 22 | interface |
| 23 | 23 | |
| 24 | -uses scan, diag, sym, x64, gen; | |
| 24 | +uses scan, diag, sym, chunk, gen, item; | |
| 25 | 25 | |
| 26 | 26 | const |
| 27 | 27 | Target = 'linux-x86_64'; |
| @@ -178,7 +178,7 @@ | ||
| 178 | 178 | jb: jmp_buf; |
| 179 | 179 | outer: ^jmp_buf; |
| 180 | 180 | scan: TScanState; |
| 181 | - scopes, depth, numDefers: longint; | |
| 181 | + scopes, depth, pushed, numDefers: longint; | |
| 182 | 182 | loop: PLoop; |
| 183 | 183 | result: PType; |
| 184 | 184 | deferring, directDefer, quiet: boolean; |
| @@ -188,7 +188,7 @@ | ||
| 188 | 188 | begin |
| 189 | 189 | r.outer := recovery; |
| 190 | 190 | SaveScan(r.scan); |
| 191 | - r.scopes := ScopeDepth; r.depth := depth; r.numDefers := numDefers; | |
| 191 | + r.scopes := ScopeDepth; r.depth := depth; r.pushed := pushed; r.numDefers := numDefers; | |
| 192 | 192 | r.loop := curLoop; r.result := curResult; |
| 193 | 193 | r.deferring := deferring; r.directDefer := directDefer; r.quiet := quiet; |
| 194 | 194 | recovery := @r.jb; |
| @@ -208,7 +208,7 @@ | ||
| 208 | 208 | begin |
| 209 | 209 | RestoreScan(r.scan); |
| 210 | 210 | skipping := true; |
| 211 | - CutScopes(r.scopes); depth := r.depth; numDefers := r.numDefers; | |
| 211 | + CutScopes(r.scopes); ResetStack(r.depth, r.pushed); numDefers := r.numDefers; | |
| 212 | 212 | curLoop := r.loop; curResult := r.result; |
| 213 | 213 | deferring := r.deferring; directDefer := r.directDefer; |
| 214 | 214 | inTypes := false; declExported := false; inExtern := false; making := false; |
| @@ -664,20 +664,8 @@ | ||
| 664 | 664 | EndCall(c, x); |
| 665 | 665 | end; |
| 666 | 666 | |
| 667 | -procedure Args(n: longint; var slots: array of int64); | |
| 668 | -var i: longint; a: TItem; | |
| 669 | -begin | |
| 670 | - for i := 0 to n - 1 do begin | |
| 671 | - if i > 0 then Expect(sComma); | |
| 672 | - Expression(a, nil); | |
| 673 | - if not (IsScalar(a.typ) and (a.typ^.form <> fReal)) then MarkAt(a.pos, 'expected an integer or a pointer, found ' + What(a)); | |
| 674 | - if a.typ^.form = fConst then Coerce(a, tInt); | |
| 675 | - slots[i] := ToSlot(a); | |
| 676 | - end; | |
| 677 | -end; | |
| 678 | - | |
| 679 | 667 | procedure Atomic(var x: TItem; which: longint); |
| 680 | -var p, a: TItem; t: PType; s: array[0..2] of int64; i, n: longint; | |
| 668 | +var p, a: TItem; t: PType; i, n: longint; | |
| 681 | 669 | begin |
| 682 | 670 | Expression(p, nil); |
| 683 | 671 | if p.typ^.form <> fPtr then MarkAt(p.pos, 'expected a pointer, found ' + What(p)); |
| @@ -685,40 +673,29 @@ | ||
| 685 | 673 | if not (IsWord(t) or ((t^.form = fPtr) and (which <> bXadd))) then |
| 686 | 674 | if which = bXadd then MarkAt(p.pos, 'expected a pointer to an INT or a UINT, found ' + TypeName(p.typ)) |
| 687 | 675 | else MarkAt(p.pos, 'expected a pointer to an INT, a UINT or a pointer, found ' + TypeName(p.typ)); |
| 688 | - s[0] := ToSlot(p); | |
| 676 | + Value(p); | |
| 689 | 677 | if which = bCas then n := 2 else n := 1; |
| 690 | 678 | for i := 1 to n do begin |
| 691 | 679 | Expect(sComma); |
| 692 | 680 | Expression(a, t); |
| 693 | 681 | Coerce(a, t); |
| 694 | - s[i] := ToSlot(a); | |
| 682 | + Value(a); | |
| 695 | 683 | end; |
| 696 | - Load(RCX, Mem(RBP, s[0]), 8, false); | |
| 697 | - Load(RAX, Mem(RBP, s[1]), 8, false); | |
| 684 | + gen.Atomic(which); | |
| 698 | 685 | MakeConst(x, t, 0); |
| 699 | - case which of | |
| 700 | - bCas: begin | |
| 701 | - Load(RDX, Mem(RBP, s[2]), 8, false); | |
| 702 | - LockCmpxchg(Mem(RCX, 0), RDX); | |
| 703 | - SetCC(ccE, RAX); | |
| 704 | - x.typ := tBool; | |
| 705 | - end; | |
| 706 | - bXchg: Xchg(Mem(RCX, 0), RAX); | |
| 707 | - bXadd: LockXadd(Mem(RCX, 0), RAX); | |
| 708 | - end; | |
| 709 | - x.mode := mReg; | |
| 686 | + if which = bCas then x.typ := tBool; | |
| 687 | + x.mode := mStack; | |
| 710 | 688 | end; |
| 711 | 689 | |
| 712 | 690 | { NEW(p, a): p := a^.alloc(a, SIZE(T), alignment), zeroed if not nil. |
| 713 | 691 | a is a ^Mem.Allocator, the one interface the compiler knows. } |
| 714 | 692 | procedure New_(var x: TItem); |
| 715 | -var y, z: TItem; t: PType; f: PObj; s: int64; c: TCall; L: longint; | |
| 693 | +var y, z: TItem; t: PType; f: PObj; s: int64; c: TCall; | |
| 716 | 694 | begin |
| 717 | 695 | Expression(x, nil); |
| 718 | 696 | NotReadOnly(x); |
| 719 | 697 | if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fPtr) then MarkAt(x.pos, 'NEW needs a pointer variable'); |
| 720 | 698 | t := x.typ^.base; |
| 721 | - Save(x); | |
| 722 | 699 | Expect(sComma); |
| 723 | 700 | Expression(y, nil); |
| 724 | 701 | if (y.typ^.form <> fPtr) or (y.typ^.base^.obj = nil) or (y.typ^.base^.obj^.module = nil) |
| @@ -726,29 +703,20 @@ | ||
| 726 | 703 | or (NameOf(y.typ^.base^.obj^.module^.name) <> 'Mem') then MarkAt(y.pos, 'NEW needs a ^Mem.Allocator, found ' + What(y)); |
| 727 | 704 | f := Find(y.typ^.base^.scope, Name('alloc')); |
| 728 | 705 | s := ToSlot(y); |
| 729 | - Load(RAX, Mem(RBP, s), 8, false); | |
| 706 | + PushVar(FrameAdr(s), 8, false, kWord); | |
| 730 | 707 | MakeConst(z, f^.typ, 0); |
| 731 | - z.mode := mVar; z.base := bReg; z.reg := RAX; z.off := f^.val; | |
| 708 | + z.mode := mVar; z.base := bStack; z.off := f^.val; | |
| 732 | 709 | BeginCall(c, z); |
| 733 | - Load(RAX, Mem(RBP, s), 8, false); | |
| 710 | + PushVar(FrameAdr(s), 8, false, kWord); | |
| 734 | 711 | MakeConst(z, y.typ, 0); |
| 735 | - z.mode := mReg; | |
| 712 | + z.mode := mStack; | |
| 736 | 713 | PassArg(c, z); |
| 737 | 714 | MakeConst(z, tInt, t^.size); |
| 738 | 715 | PassArg(c, z); |
| 739 | 716 | MakeConst(z, tInt, t^.align); |
| 740 | 717 | PassArg(c, z); |
| 741 | 718 | EndCall(c, z); |
| 742 | - L := NoLabel; | |
| 743 | - TestRR(RAX, RAX); | |
| 744 | - Jcc(ccE, L); | |
| 745 | - MovRR(RDX, RAX); | |
| 746 | - MovRR(RDI, RAX); | |
| 747 | - MovRI(RCX, t^.size); | |
| 748 | - MovRI(RAX, 0); | |
| 749 | - RepStosb; | |
| 750 | - MovRR(RAX, RDX); | |
| 751 | - FixLink(L); | |
| 719 | + ZeroNew(t^.size); | |
| 752 | 720 | z.typ := x.typ; |
| 753 | 721 | Assign(x, z); |
| 754 | 722 | MakeConst(x, tNone, 0); |
| @@ -761,17 +729,16 @@ | ||
| 761 | 729 | end; |
| 762 | 730 | |
| 763 | 731 | procedure Builtin(var x: TItem); |
| 764 | -const SysRegs: array[0..6] of longint = (RAX, RDI, RSI, RDX, R10, R8, R9); | |
| 765 | -var which, n, i: longint; y: TItem; t: PType; slots: array[0..6] of int64; | |
| 732 | +var which, n: longint; y: TItem; t: PType; | |
| 766 | 733 | begin |
| 767 | 734 | which := x.obj^.val; |
| 768 | 735 | if which in [bFence, bArgc, bArgv] then begin |
| 769 | 736 | if tok = sLparen then begin Next; Expect(sRparen) end; |
| 770 | 737 | MakeConst(x, tNone, 0); |
| 771 | 738 | case which of |
| 772 | - bFence: begin Mfence; x.mode := mNone end; | |
| 773 | - bArgc: begin Load(RAX, MemRip(argcChunk, 0), 8, false); x.typ := tInt; x.mode := mReg end; | |
| 774 | - bArgv: begin Load(RAX, MemRip(argvChunk, 0), 8, false); x.typ := tArgv; x.mode := mReg end; | |
| 739 | + bFence: begin Fence; x.mode := mNone end; | |
| 740 | + bArgc: begin PushVar(GlobalAdr(argcChunk), 8, false, kWord); x.typ := tInt; x.mode := mStack end; | |
| 741 | + bArgv: begin PushVar(GlobalAdr(argvChunk), 8, false, kWord); x.typ := tArgv; x.mode := mStack end; | |
| 775 | 742 | end; |
| 776 | 743 | exit; |
| 777 | 744 | end; |
| @@ -787,36 +754,37 @@ | ||
| 787 | 754 | if (y.mode <> mVar) or not y.isVar then MarkAt(y.pos, 'ADR needs a variable'); |
| 788 | 755 | if y.odd then MarkAt(y.pos, 'a field of a packed record has no address'); |
| 789 | 756 | t := NewPtr(y.typ, y.vol); |
| 790 | - Address(y, RAX); | |
| 757 | + Address(y); | |
| 791 | 758 | MakeConst(x, t, 0); |
| 792 | - x.mode := mReg; | |
| 759 | + x.mode := mStack; | |
| 793 | 760 | end; |
| 794 | 761 | bLen: begin |
| 795 | 762 | Expression(y, nil); |
| 796 | 763 | if y.mode = mStr then MakeConst(x, tConst, y.val) |
| 797 | - else if y.typ^.form = fArray then MakeConst(x, tConst, y.typ^.len) | |
| 798 | - else if (y.typ^.form = fSlice) and (y.mode = mVar) then begin | |
| 799 | - Address(y, RAX); | |
| 800 | - Load(RAX, Mem(RAX, 8), 8, false); | |
| 801 | - MakeConst(x, tInt, 0); | |
| 802 | - x.mode := mReg; | |
| 764 | + else if y.typ^.form = fArray then begin | |
| 765 | + Discard(y); | |
| 766 | + MakeConst(x, tConst, y.typ^.len) | |
| 767 | + end else if (y.typ^.form = fSlice) and (y.mode = mVar) then begin | |
| 768 | + { its length, the word after its pointer } | |
| 769 | + y.off := y.off + 8; | |
| 770 | + y.typ := tInt; | |
| 771 | + Value(y); | |
| 772 | + x := y; | |
| 803 | 773 | end else MarkAt(y.pos, 'LEN needs an array or a slice, found ' + What(y)); |
| 804 | 774 | end; |
| 805 | 775 | bSyscall: begin |
| 806 | - n := 1; | |
| 807 | - Args(1, slots); | |
| 808 | - while (tok = sComma) and (n < 7) do begin | |
| 809 | - Next; | |
| 776 | + n := 0; | |
| 777 | + repeat | |
| 778 | + if n > 0 then Next; | |
| 810 | 779 | Expression(y, nil); |
| 811 | 780 | if not (IsScalar(y.typ) and (y.typ^.form <> fReal)) then MarkAt(y.pos, 'expected an integer or a pointer, found ' + What(y)); |
| 812 | 781 | if y.typ^.form = fConst then Coerce(y, tInt); |
| 813 | - slots[n] := ToSlot(y); | |
| 782 | + Value(y); | |
| 814 | 783 | inc(n); |
| 815 | - end; | |
| 816 | - for i := 0 to n - 1 do Load(SysRegs[i], Mem(RBP, slots[i]), 8, false); | |
| 817 | - Syscall; | |
| 784 | + until (tok <> sComma) or (n = 7); | |
| 785 | + SysCall(n); | |
| 818 | 786 | MakeConst(x, tInt, 0); |
| 819 | - x.mode := mReg; | |
| 787 | + x.mode := mStack; | |
| 820 | 788 | end; |
| 821 | 789 | bCas, bXchg, bXadd: Atomic(x, which); |
| 822 | 790 | bMsg: begin Expression(x, tError); Msg(x) end; |
| @@ -825,7 +793,7 @@ | ||
| 825 | 793 | NotReadOnly(x); |
| 826 | 794 | if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fInt) then |
| 827 | 795 | MarkAt(x.pos, IncOrDec(which) + ' needs an integer variable'); |
| 828 | - Save(x); | |
| 796 | + Address(x); | |
| 829 | 797 | if tok = sComma then begin |
| 830 | 798 | Next; |
| 831 | 799 | Expression(y, x.typ); |
| @@ -946,7 +914,7 @@ | ||
| 946 | 914 | Expect(sRbrak); |
| 947 | 915 | if (want^.form = fArray) and (n <> want^.len) then MarkAt(SpanFrom(start), 'expected ' + IntStr(want^.len) + ' elements, found ' + IntStr(n)); |
| 948 | 916 | if n = 0 then begin |
| 949 | - MovRI(RAX, 0); | |
| 917 | + PushConst(kWord, 0); | |
| 950 | 918 | SliceOf(x, want, 0); |
| 951 | 919 | exit; |
| 952 | 920 | end; |
| @@ -961,13 +929,11 @@ | ||
| 961 | 929 | end; |
| 962 | 930 | for i := 0 to n - 1 do |
| 963 | 931 | if IsScalar(et) then begin |
| 964 | - Load(RAX, Mem(RBP, slots[i]), 8, false); | |
| 965 | - Store(Mem(RBP, off + i * es), RAX, ScalarSize(et)); | |
| 932 | + PushVar(FrameAdr(slots[i]), 8, false, KindOf(et)); | |
| 933 | + PopVar(FrameAdr(off + i * es), ScalarSize(et)); | |
| 966 | 934 | end else begin |
| 967 | - Lea(RSI, Mem(RBP, slots[i])); | |
| 968 | - Lea(RDI, Mem(RBP, off + i * es)); | |
| 969 | - MovRI(RCX, es); | |
| 970 | - RepMovsb; | |
| 935 | + PushAdr(FrameAdr(slots[i])); | |
| 936 | + CopyTo(FrameAdr(off + i * es), es); | |
| 971 | 937 | end; |
| 972 | 938 | TempItem(x, at, off); |
| 973 | 939 | end; |
| @@ -1021,15 +987,16 @@ | ||
| 1021 | 987 | else begin |
| 1022 | 988 | if slot = 0 then begin |
| 1023 | 989 | slot := AllocTemp(8, 8); |
| 1024 | - MovRI(RAX, 0); | |
| 1025 | - Store(Mem(RBP, slot), RAX, 8); | |
| 990 | + PushConst(kWord, 0); | |
| 991 | + PopVar(FrameAdr(slot), 8); | |
| 1026 | 992 | end; |
| 1027 | 993 | Value(e); |
| 1028 | - MovRR(RCX, RAX); | |
| 1029 | - MovRI(RAX, 1); | |
| 1030 | - Shift(shShl, RAX); | |
| 1031 | - AluM(aluOr, RAX, Mem(RBP, slot)); | |
| 1032 | - Store(Mem(RBP, slot), RAX, 8); | |
| 994 | + PushConst(kWord, 1); | |
| 995 | + Swap; | |
| 996 | + Op(sShl, kWord, 8, false); | |
| 997 | + PushVar(FrameAdr(slot), 8, false, kWord); | |
| 998 | + Op(sBar, kWord, 8, false); | |
| 999 | + PopVar(FrameAdr(slot), 8); | |
| 1033 | 1000 | end; |
| 1034 | 1001 | if tok <> sComma then break; |
| 1035 | 1002 | Next; |
| @@ -1038,10 +1005,10 @@ | ||
| 1038 | 1005 | if t = nil then MarkAt(SpanFrom(start), '{} needs a known set type'); |
| 1039 | 1006 | MakeConst(x, t, bits); |
| 1040 | 1007 | if slot <> 0 then begin |
| 1041 | - Load(RAX, Mem(RBP, slot), 8, false); | |
| 1042 | - MovRI(RCX, bits); | |
| 1043 | - Alu(aluOr, RAX, RCX); | |
| 1044 | - x.mode := mReg; | |
| 1008 | + PushVar(FrameAdr(slot), 8, false, kWord); | |
| 1009 | + PushConst(kWord, bits); | |
| 1010 | + Op(sBar, kWord, 8, false); | |
| 1011 | + x.mode := mStack; | |
| 1045 | 1012 | end; |
| 1046 | 1013 | end; |
| 1047 | 1014 | |
| @@ -1183,7 +1150,7 @@ | ||
| 1183 | 1150 | procedure RunDefers(base: longint); |
| 1184 | 1151 | var i: longint; |
| 1185 | 1152 | begin |
| 1186 | - for i := numDefers - 1 downto base do CallBack(defers[i]); | |
| 1153 | + for i := numDefers - 1 downto base do RunDefer(defers[i]); | |
| 1187 | 1154 | end; |
| 1188 | 1155 | |
| 1189 | 1156 | procedure IfStatement; |
| @@ -1197,8 +1164,8 @@ | ||
| 1197 | 1164 | Statements; |
| 1198 | 1165 | endL := NoLabel; |
| 1199 | 1166 | while tok = sElsif do begin |
| 1200 | - Jmp(endL); | |
| 1201 | - FixLink(L); | |
| 1167 | + Jump(endL); | |
| 1168 | + Fix(L); | |
| 1202 | 1169 | Next; |
| 1203 | 1170 | Expression(x, nil); |
| 1204 | 1171 | Expect(sThen); |
| @@ -1207,13 +1174,13 @@ | ||
| 1207 | 1174 | Statements; |
| 1208 | 1175 | end; |
| 1209 | 1176 | if tok = sElse then begin |
| 1210 | - Jmp(endL); | |
| 1211 | - FixLink(L); | |
| 1177 | + Jump(endL); | |
| 1178 | + Fix(L); | |
| 1212 | 1179 | Next; |
| 1213 | 1180 | Statements; |
| 1214 | - end else FixLink(L); | |
| 1181 | + end else Fix(L); | |
| 1215 | 1182 | Expect(sEnd); |
| 1216 | - FixLink(endL); | |
| 1183 | + Fix(endL); | |
| 1217 | 1184 | end; |
| 1218 | 1185 | |
| 1219 | 1186 | procedure WhileStatement; |
| @@ -1227,10 +1194,10 @@ | ||
| 1227 | 1194 | CondJump(x, L); |
| 1228 | 1195 | Loop(lp, old); |
| 1229 | 1196 | Statements; |
| 1230 | - JmpBack(top); | |
| 1197 | + JumpBack(top); | |
| 1231 | 1198 | Expect(sEnd); |
| 1232 | - FixLink(L); | |
| 1233 | - FixLink(lp.brk); | |
| 1199 | + Fix(L); | |
| 1200 | + Fix(lp.brk); | |
| 1234 | 1201 | curLoop := old; |
| 1235 | 1202 | end; |
| 1236 | 1203 | |
| @@ -1246,8 +1213,8 @@ | ||
| 1246 | 1213 | Expression(x, nil); |
| 1247 | 1214 | L := NoLabel; |
| 1248 | 1215 | CondJump(x, L); |
| 1249 | - FixLinkTo(L, top); | |
| 1250 | - FixLink(lp.brk); | |
| 1216 | + FixTo(L, top); | |
| 1217 | + Fix(lp.brk); | |
| 1251 | 1218 | end; |
| 1252 | 1219 | |
| 1253 | 1220 | procedure ForStatement; |
| @@ -1267,7 +1234,6 @@ | ||
| 1267 | 1234 | Next; |
| 1268 | 1235 | Expect(sBecomes); |
| 1269 | 1236 | MakeItem(v, o); |
| 1270 | - Save(v); | |
| 1271 | 1237 | Expression(a, o^.typ); |
| 1272 | 1238 | CoerceExact(a, o^.typ, ''); |
| 1273 | 1239 | Assign(v, a); |
| @@ -1287,20 +1253,16 @@ | ||
| 1287 | 1253 | top := pc; |
| 1288 | 1254 | MakeItem(v, o); |
| 1289 | 1255 | Value(v); |
| 1290 | - Load(RCX, Mem(RBP, bslot), 8, false); | |
| 1291 | - Alu(aluCmp, RAX, RCX); | |
| 1292 | - if o^.typ^.signed then begin | |
| 1293 | - if step > 0 then Jcc(ccG, exitL) else Jcc(ccL, exitL) | |
| 1294 | - end else begin | |
| 1295 | - if step > 0 then Jcc(ccA, exitL) else Jcc(ccB, exitL) | |
| 1296 | - end; | |
| 1256 | + PushVar(FrameAdr(bslot), 8, false, kWord); | |
| 1257 | + if step > 0 then JumpCmp(sGtr, o^.typ^.signed, exitL) | |
| 1258 | + else JumpCmp(sLss, o^.typ^.signed, exitL); | |
| 1297 | 1259 | Loop(lp, old); |
| 1298 | 1260 | Statements; |
| 1299 | 1261 | curLoop := old; |
| 1300 | 1262 | Expect(sEnd); |
| 1301 | 1263 | ForNext(o, bslot, step, exitL, top); |
| 1302 | - FixLink(exitL); | |
| 1303 | - FixLink(lp.brk); | |
| 1264 | + Fix(exitL); | |
| 1265 | + Fix(lp.brk); | |
| 1304 | 1266 | end; |
| 1305 | 1267 | |
| 1306 | 1268 | type |
| @@ -1348,12 +1310,12 @@ | ||
| 1348 | 1310 | Next; |
| 1349 | 1311 | until false; |
| 1350 | 1312 | L := NoLabel; |
| 1351 | - Jmp(L); | |
| 1352 | - FixLink(body); | |
| 1313 | + Jump(L); | |
| 1314 | + Fix(body); | |
| 1353 | 1315 | Expect(sColon); |
| 1354 | 1316 | Statements; |
| 1355 | - Jmp(endL); | |
| 1356 | - FixLink(L); | |
| 1317 | + Jump(endL); | |
| 1318 | + Fix(L); | |
| 1357 | 1319 | end; |
| 1358 | 1320 | |
| 1359 | 1321 | procedure MatchStatement; |
| @@ -1415,8 +1377,8 @@ | ||
| 1415 | 1377 | Expect(sColon); |
| 1416 | 1378 | Statements; |
| 1417 | 1379 | CloseScope; |
| 1418 | - Jmp(endL); | |
| 1419 | - FixLink(L); | |
| 1380 | + Jump(endL); | |
| 1381 | + Fix(L); | |
| 1420 | 1382 | end; |
| 1421 | 1383 | end; |
| 1422 | 1384 | endAt := TokPos; |
| @@ -1430,7 +1392,7 @@ | ||
| 1430 | 1392 | c := c^.next; |
| 1431 | 1393 | end; |
| 1432 | 1394 | end; |
| 1433 | - FixLink(endL); | |
| 1395 | + Fix(endL); | |
| 1434 | 1396 | end; |
| 1435 | 1397 | |
| 1436 | 1398 | { What a message calls x: its name n when x is just that name, else what |
| @@ -1521,32 +1483,28 @@ | ||
| 1521 | 1483 | { defer s: s is compiled where it stands, as a little subroutine of the |
| 1522 | 1484 | procedure, jumped over; each way out of the statement list calls it. } |
| 1523 | 1485 | procedure Defer; |
| 1524 | -var L, pos, oldDepth: longint; oldLoop: PLoop; oldDeferring: boolean; start: TPos; | |
| 1486 | +var L, pos, saved: longint; oldLoop: PLoop; oldDeferring: boolean; start: TPos; | |
| 1525 | 1487 | begin |
| 1526 | 1488 | start := TokPos; |
| 1527 | 1489 | Next; |
| 1528 | 1490 | if directDefer then MarkAt(SpanFrom(start), 'defer can''t be deferred'); |
| 1529 | 1491 | if numDefers > High(defers) then MarkAt(SpanFrom(start), 'more than ' + IntStr(High(defers) + 1) + ' deferred statements in one procedure'); |
| 1530 | 1492 | L := NoLabel; |
| 1531 | - Jmp(L); | |
| 1532 | - pos := pc; | |
| 1533 | - oldDepth := depth; oldLoop := curLoop; oldDeferring := deferring; | |
| 1534 | - depth := 1; { the return address } | |
| 1493 | + pos := DeferBegin(L, saved); | |
| 1494 | + oldLoop := curLoop; oldDeferring := deferring; | |
| 1535 | 1495 | curLoop := nil; |
| 1536 | 1496 | deferring := true; |
| 1537 | 1497 | directDefer := true; |
| 1538 | 1498 | Statement; |
| 1539 | 1499 | directDefer := false; |
| 1540 | - KeepTemps; | |
| 1541 | - depth := oldDepth; curLoop := oldLoop; deferring := oldDeferring; | |
| 1542 | - Ret; | |
| 1543 | - FixLink(L); | |
| 1500 | + curLoop := oldLoop; deferring := oldDeferring; | |
| 1501 | + DeferEnd(L, saved); | |
| 1544 | 1502 | defers[numDefers] := pos; |
| 1545 | 1503 | inc(numDefers); |
| 1546 | 1504 | end; |
| 1547 | 1505 | |
| 1548 | 1506 | procedure OneStatement; |
| 1549 | -var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; first: longint; | |
| 1507 | +var x, y: TItem; called, scalar: boolean; m: int64; c: TCall; start: TPos; first: longint; | |
| 1550 | 1508 | begin |
| 1551 | 1509 | start := TokPos; |
| 1552 | 1510 | first := id; |
| @@ -1558,20 +1516,20 @@ | ||
| 1558 | 1516 | if tok = sBecomes then begin |
| 1559 | 1517 | NotReadOnly(x); |
| 1560 | 1518 | Assignable(x, first); |
| 1561 | - Save(x); | |
| 1562 | 1519 | Next; |
| 1563 | 1520 | Expression(y, x.typ); |
| 1564 | 1521 | Assign(x, y); |
| 1565 | 1522 | end else if tok = sComma then |
| 1566 | 1523 | MultiAssign(x, first) |
| 1567 | 1524 | else if not called then begin |
| 1568 | - if (x.mode = mProc) or ((x.mode in [mVar, mReg]) and (x.typ^.form = fProc)) then begin | |
| 1525 | + if (x.mode = mProc) or ((x.mode in [mVar, mStack]) and (x.typ^.form = fProc)) then begin | |
| 1569 | 1526 | BeginCall(c, x); |
| 1570 | 1527 | EndCall(c, x); |
| 1528 | + Discard(x); | |
| 1571 | 1529 | end else if tok = sEql then |
| 1572 | 1530 | MarkHelp(x.pos, Subject(x, first) + ' isn''t a statement', 'assignment is written :=') |
| 1573 | 1531 | else MarkAt(x.pos, Subject(x, first) + ' isn''t a statement'); |
| 1574 | - end; | |
| 1532 | + end else Discard(x); | |
| 1575 | 1533 | end; |
| 1576 | 1534 | sIf: IfStatement; |
| 1577 | 1535 | sWhile: WhileStatement; |
| @@ -1590,12 +1548,14 @@ | ||
| 1590 | 1548 | Expression(x, curResult); |
| 1591 | 1549 | ReturnValue(x); |
| 1592 | 1550 | end; |
| 1593 | - if numDefers > 0 then begin | |
| 1551 | + { the result waits in the frame while the deferred statements run } | |
| 1552 | + scalar := (curResult <> nil) and not IsAggregate(curResult); | |
| 1553 | + if (numDefers > 0) and scalar then begin | |
| 1594 | 1554 | if deferSlot = 0 then deferSlot := AllocPerm(8, 8); |
| 1595 | - Store(Mem(RBP, deferSlot), RAX, 8); | |
| 1555 | + PopVar(FrameAdr(deferSlot), 8); | |
| 1596 | 1556 | RunDefers(0); |
| 1597 | - Load(RAX, Mem(RBP, deferSlot), 8, false); | |
| 1598 | - end; | |
| 1557 | + PushVar(FrameAdr(deferSlot), 8, false, KindOf(curResult)); | |
| 1558 | + end else RunDefers(0); | |
| 1599 | 1559 | ReturnJump; |
| 1600 | 1560 | end; |
| 1601 | 1561 | end; |
| @@ -1603,7 +1563,7 @@ | ||
| 1603 | 1563 | Next; |
| 1604 | 1564 | if curLoop = nil then MarkAt(SpanFrom(start), 'break outside a loop'); |
| 1605 | 1565 | RunDefers(curLoop^.base); |
| 1606 | - Jmp(curLoop^.brk); | |
| 1566 | + Jump(curLoop^.brk); | |
| 1607 | 1567 | end; |
| 1608 | 1568 | sDefer: Defer; |
| 1609 | 1569 | else if AtLineStart then Expected('"end"') |
| @@ -2569,11 +2529,6 @@ | ||
| 2569 | 2529 | nameStatic := Name('static'); |
| 2570 | 2530 | nameCheck := Name('check'); |
| 2571 | 2531 | nameOff := Name('off'); |
| 2572 | - rtFail := NewChunk(secText, nameRt, nameFail); | |
| 2573 | - argcChunk := NewChunk(secBss, -1, Name('argc')); | |
| 2574 | - SetBss(argcChunk, 8, 8); | |
| 2575 | - argvChunk := NewChunk(secBss, -1, Name('argv')); | |
| 2576 | - SetBss(argvChunk, 8, 8); | |
| 2577 | 2532 | tArgv := NewPtr(NewPtr(tU8, false), false); |
| 2578 | 2533 | tBodySig := NewType(fProc, 8, 8); |
| 2579 | 2534 | curLoop := nil; |
| @@ -25,7 +25,7 @@ | ||
| 25 | 25 | |
| 26 | 26 | {$mode objfpc}{$H-} |
| 27 | 27 | |
| 28 | -uses math, BaseUnix, syscall, scan, diag, sym, x64, gen, parse, link, elf; | |
| 28 | +uses math, BaseUnix, syscall, scan, diag, sym, chunk, gen, parse, link, elf; | |
| 29 | 29 | |
| 30 | 30 | const |
| 31 | 31 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| @@ -90,10 +90,10 @@ | ||
| 90 | 90 | |
| 91 | 91 | var |
| 92 | 92 | path, outPath, dir, s: shortstring; |
| 93 | - i, first, entry, start, fd: longint; | |
| 93 | + i, first, entry, fd: longint; | |
| 94 | 94 | m, rt: PModule; |
| 95 | 95 | main: PObj; |
| 96 | - hasResult, dynamic, run: boolean; | |
| 96 | + run: boolean; | |
| 97 | 97 | |
| 98 | 98 | begin |
| 99 | 99 | { constants fold as the machine computes: NaN and infinities, no traps } |
| @@ -150,67 +150,21 @@ | ||
| 150 | 150 | if rt <> nil then Use(rt); |
| 151 | 151 | |
| 152 | 152 | main := Find(m^.scope, Intern('main', 4)); |
| 153 | - hasResult := false; | |
| 154 | 153 | if main <> nil then begin |
| 155 | 154 | if (main^.cls <> oProc) or not main^.exported then Error(main^.pos, 'main must be an exported procedure') |
| 156 | 155 | else if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tInt)) then |
| 157 | 156 | Error(main^.pos, 'main must be proc main* or proc main*: INT'); |
| 158 | 157 | if errors > 0 then Finish; |
| 159 | - hasResult := main^.typ^.base <> nil; | |
| 160 | 158 | end; |
| 161 | 159 | Finish; |
| 162 | 160 | |
| 163 | 161 | if rt = nil then Fatal('can''t find module Rt'); |
| 164 | - dynamic := UsesC; | |
| 165 | - entry := NewChunk(secText, -1, Intern('_start', 6)); | |
| 166 | - if dynamic then begin | |
| 167 | - { _start as C's crt1 does it: __libc_start_main(main, argc, argv, | |
| 168 | - nil, nil, rtld_fini, stack_end), with rsp aligned } | |
| 169 | - start := NewChunk(secText, -1, Intern('main', 4)); | |
| 170 | - BeginChunk(entry); | |
| 171 | - MovRI(RBP, 0); | |
| 172 | - MovRR(R9, RDX); | |
| 173 | - Pop(RSI); | |
| 174 | - MovRR(RDX, RSP); | |
| 175 | - AluI(aluAnd, RSP, -16); | |
| 176 | - Push(RAX); | |
| 177 | - Push(RSP); | |
| 178 | - MovRI(R8, 0); | |
| 179 | - MovRI(RCX, 0); | |
| 180 | - Lea(RDI, MemRip(start, 0)); | |
| 181 | - CallM(MemRip(ExternChunk(Intern('__libc_start_main', 17)), 0)); | |
| 182 | - Ud2; | |
| 183 | - EndChunk(entry); | |
| 184 | - BeginChunk(start); | |
| 185 | - Push(RBP); { aligns the stack again } | |
| 186 | - Store(MemRip(argcChunk, 0), RDI, 8); | |
| 187 | - Store(MemRip(argvChunk, 0), RSI, 8); | |
| 188 | - Load(RAX, MemRip(ExternChunk(Intern('exit', 4)), 0), 8, false); | |
| 189 | - Store(MemRip(RtObj(rt, 'cExit')^.chunk, 0), RAX, 8); | |
| 190 | - end else begin | |
| 191 | - start := entry; | |
| 192 | - BeginChunk(entry); | |
| 193 | - Load(RAX, Mem(RSP, 0), 8, false); | |
| 194 | - Store(MemRip(argcChunk, 0), RAX, 8); | |
| 195 | - Lea(RAX, Mem(RSP, 8)); | |
| 196 | - Store(MemRip(argvChunk, 0), RAX, 8); | |
| 197 | - end; | |
| 198 | 162 | { the places, which the linker makes, for Rt to say where a failed |
| 199 | 163 | check or a signal was } |
| 200 | 164 | placesChunk := NewChunk(secRodata, -1, -1); |
| 201 | 165 | SetData(placesChunk, nil, 0, 8); |
| 202 | - Lea(RAX, MemRip(placesChunk, 0)); | |
| 203 | - Store(MemRip(RtObj(rt, 'places')^.chunk, 0), RAX, 8); | |
| 204 | - m := firstModule; | |
| 205 | - while m <> nil do begin | |
| 206 | - if m^.used and (m^.body >= 0) then CallChunk(m^.body); | |
| 207 | - m := m^.next; | |
| 208 | - end; | |
| 209 | - if main <> nil then CallChunk(main^.chunk); | |
| 210 | - if hasResult then MovRR(RDI, RAX) else MovRI(RDI, 0); | |
| 211 | - CallChunk(RtObj(rt, 'exit')^.chunk); | |
| 212 | - Ud2; | |
| 213 | - EndChunk(start); | |
| 166 | + entry := EntryPoint(UsesC, firstModule, main, RtObj(rt, 'cExit')^.chunk, placesChunk, | |
| 167 | + RtObj(rt, 'places')^.chunk, RtObj(rt, 'exit')^.chunk); | |
| 214 | 168 | |
| 215 | 169 | MarkLive(entry); |
| 216 | 170 | if not HasReals then |
| @@ -1,17 +1,11 @@ | ||
| 1 | -{ x64.pas: machine code. | |
| 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), bss (variables, which are all zero). | |
| 6 | - A reference from code to another chunk (a call, or a rip-relative | |
| 7 | - address) is a fixup, patched once the linker has placed every chunk. | |
| 8 | - Jumps within a procedure are patched here, as soon as their target | |
| 9 | - is known. | |
| 1 | +{ x64.pas: machine code for x86-64. | |
| 10 | 2 | |
| 11 | 3 | The encoder writes the instructions the code generator needs, and |
| 12 | - no others. Registers are numbered as the hardware numbers them. | |
| 4 | + no others, into the text of chunk.pas. Registers are numbered as the | |
| 5 | + hardware numbers them. Jumps within a procedure are patched here, as | |
| 6 | + soon as their target is known. | |
| 13 | 7 | |
| 14 | - Depends on: scan, for errors. | |
| 8 | + Depends on: scan, for errors; chunk, for the code buffer and fixups. | |
| 15 | 9 | Leaves out: choosing instructions; that is gen.pas. } |
| 16 | 10 | |
| 17 | 11 | unit x64; |
| @@ -20,7 +14,7 @@ | ||
| 20 | 14 | |
| 21 | 15 | interface |
| 22 | 16 | |
| 23 | -uses scan, diag; | |
| 17 | +uses scan, diag, chunk; | |
| 24 | 18 | |
| 25 | 19 | const |
| 26 | 20 | RAX = 0; RCX = 1; RDX = 2; RBX = 3; RSP = 4; RBP = 5; RSI = 6; RDI = 7; |
| @@ -41,8 +35,6 @@ | ||
| 41 | 35 | NoLabel = -1; |
| 42 | 36 | |
| 43 | 37 | type |
| 44 | - TSection = (secText, secRodata, secBss); | |
| 45 | - | |
| 46 | 38 | TMem = record |
| 47 | 39 | base: longint; { a register, or RIP } |
| 48 | 40 | index: longint; { a register, or NoIndex } |
| @@ -51,66 +43,10 @@ | ||
| 51 | 43 | chunk: longint; { with RIP: the chunk addressed } |
| 52 | 44 | end; |
| 53 | 45 | |
| 54 | - TChunk = record | |
| 55 | - sec: TSection; | |
| 56 | - start, size, align: longint; { in its section's buffer } | |
| 57 | - firstFix, numFix: longint; { its fixups, text chunks only } | |
| 58 | - module, name: longint; { for the symbol table; -1 if none } | |
| 59 | - adr: int64; { its address, set by the linker } | |
| 60 | - live: boolean; { reached from the entry point } | |
| 61 | - ext: boolean; { bss: the address of a C symbol, its name } | |
| 62 | - reals: boolean; { code: computes with reals, first at realsAt } | |
| 63 | - realsAt: TPos; | |
| 64 | - end; | |
| 65 | - | |
| 66 | - { A place in the source whose code starts at a place in text: for | |
| 67 | - gdb, and for Rt to say where a failed check or a signal was } | |
| 68 | - TLine = record | |
| 69 | - at, chunk: longint; { the place in text, and its chunk } | |
| 70 | - path, line, col: longint; { the file's path, interned, the line and column } | |
| 71 | - end; | |
| 72 | - | |
| 73 | - TFixup = record | |
| 74 | - at: longint; { the rel32 field, as an offset in text } | |
| 75 | - chunk: longint; { the chunk it refers to } | |
| 76 | - addend: longint; { added to the chunk's address } | |
| 77 | - end; | |
| 78 | - | |
| 79 | - PChunks = ^TChunkArray; | |
| 80 | - TChunkArray = array[0..MaxInt div SizeOf(TChunk) - 1] of TChunk; | |
| 81 | - PLines = ^TLineArray; | |
| 82 | - TLineArray = array[0..MaxInt div SizeOf(TLine) - 1] of TLine; | |
| 83 | - PFixups = ^TFixupArray; | |
| 84 | - TFixupArray = array[0..MaxInt div SizeOf(TFixup) - 1] of TFixup; | |
| 85 | - | |
| 86 | 46 | var |
| 87 | - code: PBytes; { text } | |
| 88 | - pc: longint; { its length } | |
| 89 | - rodata: PBytes; | |
| 90 | - rolen: longint; | |
| 91 | - chunks: PChunks; | |
| 92 | - numChunks: longint; | |
| 93 | - fixups: PFixups; | |
| 94 | - numFixups: longint; | |
| 95 | - lines: PLines; | |
| 96 | - numLines: longint; | |
| 97 | - curFile: longint; { the file being compiled, for NoteLine } | |
| 98 | 47 | labelAt: longint; { the last place a jump was made to land } |
| 99 | 48 | |
| 100 | -function NewChunk(sec: TSection; module, name: longint): longint; | |
| 101 | -procedure BeginChunk(c: longint); | |
| 102 | -procedure EndChunk(c: longint); | |
| 103 | -procedure SetData(c: longint; p: PBytes; n, align: longint); | |
| 104 | -procedure SetBss(c: longint; size, align: longint); | |
| 105 | -function ExternChunk(name: longint): longint; | |
| 106 | -procedure NoteLine(line, col: longint); | |
| 107 | 49 | function Here: TMem; |
| 108 | - | |
| 109 | -procedure Put1(b: longint); | |
| 110 | -procedure Put4(v: longint); | |
| 111 | -procedure Put4At(at, v: longint); | |
| 112 | -function Get4At(at: longint): longint; | |
| 113 | - | |
| 114 | 50 | function Mem(base: longint; disp: int64): TMem; |
| 115 | 51 | function MemRip(chunk: longint; disp: longint): TMem; |
| 116 | 52 | function MemIdx(base, index, scale: longint; disp: int64): TMem; |
| @@ -169,151 +105,6 @@ | ||
| 169 | 105 | |
| 170 | 106 | implementation |
| 171 | 107 | |
| 172 | -var | |
| 173 | - codeCap, roCap, chunkCap, fixCap, lineCap: longint; | |
| 174 | - curChunk: longint; | |
| 175 | - | |
| 176 | -{ Buffers and chunks } | |
| 177 | - | |
| 178 | -procedure Room(n: longint); | |
| 179 | -begin | |
| 180 | - if pc + n > codeCap then begin | |
| 181 | - codeCap := codeCap * 2 + n; | |
| 182 | - code := ReAllocMem(code, codeCap); | |
| 183 | - end; | |
| 184 | -end; | |
| 185 | - | |
| 186 | -procedure Put1(b: longint); | |
| 187 | -begin | |
| 188 | - if pc + 16 > codeCap then Room(16); | |
| 189 | - code^[pc] := byte(b); | |
| 190 | - inc(pc); | |
| 191 | -end; | |
| 192 | - | |
| 193 | -procedure Put4(v: longint); | |
| 194 | -begin | |
| 195 | - if pc + 16 > codeCap then Room(16); | |
| 196 | - PLongint(@code^[pc])^ := v; | |
| 197 | - inc(pc, 4); | |
| 198 | -end; | |
| 199 | - | |
| 200 | -procedure Put4At(at, v: longint); | |
| 201 | -begin | |
| 202 | - PLongint(@code^[at])^ := v; | |
| 203 | -end; | |
| 204 | - | |
| 205 | -function Get4At(at: longint): longint; | |
| 206 | -begin | |
| 207 | - Get4At := PLongint(@code^[at])^; | |
| 208 | -end; | |
| 209 | - | |
| 210 | -procedure Put8(v: int64); | |
| 211 | -begin | |
| 212 | - Put4(longint(v and $FFFFFFFF)); | |
| 213 | - Put4(longint(v shr 32)); | |
| 214 | -end; | |
| 215 | - | |
| 216 | -function NewChunk(sec: TSection; module, name: longint): longint; | |
| 217 | -begin | |
| 218 | - if numChunks = chunkCap then begin | |
| 219 | - chunkCap := chunkCap * 2; | |
| 220 | - chunks := ReAllocMem(chunks, chunkCap * SizeOf(TChunk)); | |
| 221 | - end; | |
| 222 | - FillChar(chunks^[numChunks], SizeOf(TChunk), 0); | |
| 223 | - chunks^[numChunks].sec := sec; | |
| 224 | - chunks^[numChunks].module := module; | |
| 225 | - chunks^[numChunks].name := name; | |
| 226 | - chunks^[numChunks].align := 1; | |
| 227 | - chunks^[numChunks].size := -1; { not yet defined } | |
| 228 | - NewChunk := numChunks; | |
| 229 | - inc(numChunks); | |
| 230 | -end; | |
| 231 | - | |
| 232 | -procedure BeginChunk(c: longint); | |
| 233 | -begin | |
| 234 | - { code is aligned to 16 bytes, padded with int3 } | |
| 235 | - while pc and 15 <> 0 do Put1($CC); | |
| 236 | - curChunk := c; | |
| 237 | - chunks^[c].start := pc; | |
| 238 | - chunks^[c].align := 16; | |
| 239 | - chunks^[c].firstFix := numFixups; | |
| 240 | -end; | |
| 241 | - | |
| 242 | -procedure EndChunk(c: longint); | |
| 243 | -begin | |
| 244 | - chunks^[c].size := pc - chunks^[c].start; | |
| 245 | - chunks^[c].numFix := numFixups - chunks^[c].firstFix; | |
| 246 | -end; | |
| 247 | - | |
| 248 | -procedure SetData(c: longint; p: PBytes; n, align: longint); | |
| 249 | -begin | |
| 250 | - if rolen + n > roCap then begin | |
| 251 | - roCap := roCap * 2 + n; | |
| 252 | - rodata := ReAllocMem(rodata, roCap); | |
| 253 | - end; | |
| 254 | - Move(p^, rodata^[rolen], n); | |
| 255 | - chunks^[c].start := rolen; | |
| 256 | - chunks^[c].size := n; | |
| 257 | - chunks^[c].align := align; | |
| 258 | - inc(rolen, n); | |
| 259 | -end; | |
| 260 | - | |
| 261 | -procedure SetBss(c: longint; size, align: longint); | |
| 262 | -begin | |
| 263 | - chunks^[c].size := size; | |
| 264 | - chunks^[c].align := align; | |
| 265 | -end; | |
| 266 | - | |
| 267 | -{ The code from here on is for line and column of the current file. A | |
| 268 | - place with no code of its own gives way to the next. } | |
| 269 | -procedure NoteLine(line, col: longint); | |
| 270 | -begin | |
| 271 | - if (numLines > 0) and (lines^[numLines - 1].at = pc) then dec(numLines); | |
| 272 | - if (numLines > 0) and (lines^[numLines - 1].chunk = curChunk) and (lines^[numLines - 1].line = line) | |
| 273 | - and (lines^[numLines - 1].col = col) and (lines^[numLines - 1].path = curFile) then exit; | |
| 274 | - if numLines = lineCap then begin | |
| 275 | - lineCap := lineCap * 2; | |
| 276 | - lines := ReAllocMem(lines, lineCap * SizeOf(TLine)); | |
| 277 | - end; | |
| 278 | - lines^[numLines].at := pc; | |
| 279 | - lines^[numLines].chunk := curChunk; | |
| 280 | - lines^[numLines].path := curFile; | |
| 281 | - lines^[numLines].line := line; | |
| 282 | - lines^[numLines].col := col; | |
| 283 | - inc(numLines); | |
| 284 | -end; | |
| 285 | - | |
| 286 | -{ The address of the code about to be written, as an operand } | |
| 287 | -function Here: TMem; | |
| 288 | -begin | |
| 289 | - Here := MemRip(curChunk, pc - chunks^[curChunk].start); | |
| 290 | -end; | |
| 291 | - | |
| 292 | -{ The quadword that holds the address of C's symbol name, filled in | |
| 293 | - when the program starts; one for each name. } | |
| 294 | -function ExternChunk(name: longint): longint; | |
| 295 | -var c: longint; | |
| 296 | -begin | |
| 297 | - for c := 0 to numChunks - 1 do | |
| 298 | - if chunks^[c].ext and (chunks^[c].name = name) then exit(c); | |
| 299 | - c := NewChunk(secBss, -1, name); | |
| 300 | - SetBss(c, 8, 8); | |
| 301 | - chunks^[c].ext := true; | |
| 302 | - ExternChunk := c; | |
| 303 | -end; | |
| 304 | - | |
| 305 | -procedure AddFixup(at, chunk, addend: longint); | |
| 306 | -begin | |
| 307 | - if numFixups = fixCap then begin | |
| 308 | - fixCap := fixCap * 2; | |
| 309 | - fixups := ReAllocMem(fixups, fixCap * SizeOf(TFixup)); | |
| 310 | - end; | |
| 311 | - fixups^[numFixups].at := at; | |
| 312 | - fixups^[numFixups].chunk := chunk; | |
| 313 | - fixups^[numFixups].addend := addend; | |
| 314 | - inc(numFixups); | |
| 315 | -end; | |
| 316 | - | |
| 317 | 108 | { Operands } |
| 318 | 109 | |
| 319 | 110 | function Mem(base: longint; disp: int64): TMem; |
| @@ -336,6 +127,12 @@ | ||
| 336 | 127 | MemIdx.scale := scale; |
| 337 | 128 | end; |
| 338 | 129 | |
| 130 | +{ The address of the code about to be written, as an operand } | |
| 131 | +function Here: TMem; | |
| 132 | +begin | |
| 133 | + Here := MemRip(curChunk, pc - chunks^[curChunk].start); | |
| 134 | +end; | |
| 135 | + | |
| 339 | 136 | { Encoding. An instruction is: a legacy prefix, REX, the opcode, ModRM, |
| 340 | 137 | SIB and displacement. Opcodes of more than one byte are given with |
| 341 | 138 | their first byte in the low eight bits. } |
| @@ -676,20 +473,4 @@ | ||
| 676 | 473 | FixLinkTo(L, pc); |
| 677 | 474 | end; |
| 678 | 475 | |
| 679 | -initialization | |
| 680 | - codeCap := 1 shl 20; | |
| 681 | - code := GetMem(codeCap); | |
| 682 | - pc := 0; | |
| 683 | - roCap := 1 shl 16; | |
| 684 | - rodata := GetMem(roCap); | |
| 685 | - rolen := 0; | |
| 686 | - chunkCap := 1 shl 10; | |
| 687 | - chunks := GetMem(chunkCap * SizeOf(TChunk)); | |
| 688 | - numChunks := 0; | |
| 689 | - fixCap := 1 shl 12; | |
| 690 | - fixups := GetMem(fixCap * SizeOf(TFixup)); | |
| 691 | - numFixups := 0; | |
| 692 | - lineCap := 1 shl 12; | |
| 693 | - lines := GetMem(lineCap * SizeOf(TLine)); | |
| 694 | - numLines := 0; | |
| 695 | 476 | end. |
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | of the encoder in x64.pas. } |
| 3 | 3 | program enctest; |
| 4 | 4 | {$mode objfpc}{$H-} |
| 5 | -uses scan, x64, link, elf; | |
| 5 | +uses scan, chunk, x64, link, elf; | |
| 6 | 6 | var e, d: longint; L: longint; |
| 7 | 7 | begin |
| 8 | 8 | d := NewChunk(secBss, -1, Intern('data', 4)); SetBss(d, 8, 8); |
| @@ -29,5 +29,6 @@ | ||
| 29 | 29 | Cvtts2si(RAX, 0, false); Cvtts2si(RAX, 0, true); Btc(RAX, 63); |
| 30 | 30 | L := NoLabel; Jcc(ccE, L); Jmp(L); FixLink(L); JmpBack(chunks^[e].start); JccBack(ccNE, chunks^[e].start); |
| 31 | 31 | EndChunk(e); |
| 32 | + MarkLive(e); | |
| 32 | 33 | WriteElf(ParamStr(1), e); |
| 33 | 34 | end. |
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | left out. } |
| 6 | 6 | program handtest; |
| 7 | 7 | {$mode objfpc}{$H-} |
| 8 | -uses scan, x64, link, elf; | |
| 8 | +uses scan, chunk, x64, link, elf; | |
| 9 | 9 | var entry, msg, count, bump, unused: longint; s: shortstring; |
| 10 | 10 | begin |
| 11 | 11 | s := 'hand'#10; |
| @@ -41,5 +41,6 @@ | ||
| 41 | 41 | Syscall; |
| 42 | 42 | EndChunk(entry); |
| 43 | 43 | |
| 44 | + MarkLive(entry); | |
| 44 | 45 | WriteElf(ParamStr(1), entry); |
| 45 | 46 | end. |