aboutsummaryrefslogtreecommitdiff
path: root/src/arch/x86_64/CodeGen.zig
AgeCommit message (Collapse)Author
2021-12-29stage2: update PrintMir with latest instructions and Isel changesJakub Konka
2021-12-29stage2: lower 1-byte and 2-byte values saved to stackJakub Konka
* fix handling of `ah`, `bh`, `ch`, and `dh` registers (which are actually used as aliases to `dil`, etc. registers). Currenly, we treat them as aliases only meaning when we encounter `ah` we make sure to set the REX.W to promote the instruction to 64bits and use `dil` register instead - otherwise we might have mismatch between registers used in different parts of the codegen. In the future, we can and should use `ah`, etc. as upper 8bit halves of 16bit registers `ax`, etc. * fix bug in `airCmp` where `.cmp` MIR instruction shouldn't force type `Bool` but let the type of the original type propagate downwards - we need this to make an informed choice of the target register size and hence choose the right encoding down the line. * implement lowering of 1-byte and 2-byte values to stack and add matching stage2 tests for x86_64 codegen
2021-12-29stage2: add separate tag for MI encodingJakub Konka
To request memory-immediate encoding at the MIR side, we should now use a new tag such as `mov_mem_imm` where the size of the memory pointer is encoded as the flags: ``` 0b00 => .byte_ptr, 0b01 => .word_ptr, 0b10 => .dword_ptr, 0b11 => .qword_ptr, ```
2021-12-27stage2: LLVM backend: implement `@tagName` for enumsAndrew Kelley
Introduced a new AIR instruction: `tag_name`. Reasons to do this instead of lowering it in Sema to a switch, function call, array lookup, or if-else tower: * Sema is a bottleneck; do less work in Sema whenever possible. * If any optimization passes run, and the operand to becomes comptime-known, then it could change to have a comptime result value instead of lowering to a function or array or something which would then have to be garbage-collected. * Backends may want to choose to use a function and a switch branch, or they may want to use a different strategy. Codegen for `@tagName` is implemented for the LLVM backend but not any others yet. Introduced some new `Type` tags: * `const_slice_u8_sentinel_0` * `manyptr_const_u8_sentinel_0` The motivation for this was to make typeof() on the tag_name AIR instruction non-allocating. A bunch more enum tests are passing now.
2021-12-23Merge pull request #10394 from ziglang/stage2-x86_64-mir-intel-syntaxAndrew Kelley
stage2: rewrite MIR -> Isel layer for x86_64
2021-12-23stage2: use lowerToRmEnc to lower two-operand imulJakub Konka
Fix mismatched register sizes in codegen.
2021-12-23stage2: lower jcc and setcc conditional jump/set instructionsJakub Konka
2021-12-21stage2: @shlWithOverflowRobin Voetter
2021-12-21stage2: @subWithOverflowRobin Voetter
2021-12-21stage2: @mulWithOverflowRobin Voetter
2021-12-21stage2: @returnAddress()Robin Voetter
2021-12-21stage2: @addWithOverflowRobin Voetter
2021-12-20stage2: merge MOV back with arith instructionsJakub Konka
* turns out MOV and other arithmetic instructions such as ADD can naturally share the same lowering codepath (for the same variants) * there are variants that are specific to ADD, or MOV which will be implemented as standalone MIR tags * tweak Isel tests to generate corresponding test cases for all arithmetic instructions in comptime
2021-12-20stage2: remove obsolete MOV variant 0b11Jakub Konka
* variant `0b11` when both `reg1 != .none` and `reg2 != .none` is identical to `0b00` therefore it can safely be removed * fix proper destination register size calculation when setting register from another source register
2021-12-20stage2: fix MOV MIR -> Isel lowering logicJakub Konka
* ensure that every callsite of basic MOV MIR instruction follows the Intel syntax (dst <- src) * add extensive unit tests for MOV MIR -> Isel lowering * leave TODOs for cases that are currently not handled and/or missing * fix any ABI size mismatch between operands
2021-12-15stage2: fix register size selectionJakub Konka
This actually needs proper rework, and I'll get to that when refactoring MIR.
2021-12-15stage2: clean up testsJakub Konka
* move darwin tests into respective architecture test files: `x86_64` and `aarch64` * run majority of `x86_64` tests on macOS
2021-11-30allocgate: std Allocator interface refactorLee Cannon
2021-11-30std lib API deprecations for the upcoming 0.9.0 releaseAndrew Kelley
See #3811
2021-11-27interleave Air instructions and tags in printing Mir instructionsJacob G-W
2021-11-27initial implementation of print_mirJacob G-W
2021-11-21stage2: ensure 16byte stack alignment on macOS x86_64Jakub Konka
2021-11-21stage2: add x86_64 NOP MIR inst and loweringJakub Konka
If we don't touch the stack, ellide `sub rsp, imm32` to `nop`.
2021-11-19stage2,x86_64: fix genBinMathOp and clarify callee-saved regsJakub Konka
Previously, we have confused callee-saved with caller-saved registers (the actual register sets were swapped). This commit fixes that for both `.x86` and `.x86_64` native backends. This commit also fixes the register allocation logic in `genBinMathOp` for `.x86_64` native backend where in a situation such that we require to spill a register, we would end up spilling the register that is already involved in the instruction as the other operand. In such a case, we make a note of this and spill a subsequent register instead.
2021-11-19x86_64/Emit: implement restoring callee_preserved_registersJacob G-W
2021-11-19stage2 x86_64 codegen: don't count return registers as callee-preservedJacob G-W
2021-11-18stage2: use correct register alias for mem operandsJakub Konka
When loading/storing on the stack, use `registerAlias` to correctly workout the size of the register and thus correctly select target instruction.
2021-11-11Merge pull request #9935 from g-w1/plan9-stdJakub Konka
add plan9 support to std
2021-11-10macho: use start.zig for macOS entrypointJakub Konka
This effectively allows us to compile ```zig pub fn main() void {} ``` which then calls into `std.start`. Changes required to make this happen: * handle signed int to immediate in x86_64 and aarch64 codegen * ensure that on arm64 macOS, `.x19` is a caller-preserved register - I'm not sure about that one at all and would like to brainstorm it with anyone interested and especially Joachim. * finally, fix a bug in the linker - mark new got entry as dirty upon atom growth.
2021-11-09Sema: implement coerce_result_ptr for optionalsAndrew Kelley
New AIR instruction: `optional_payload_ptr_set` It's like `optional_payload_ptr` except it sets the non-null bit. When storing to the payload via a result location that is an optional, `optional_payload_ptr_set` is now emitted. There is a new algorithm in `zirCoerceResultPtr` which stores a dummy value through the result pointer into a temporary block, and then pops off the AIR instructions from the temporary block in order to determine how to transform the result location pointer in case any in-between coercions need to happen. Fixes a couple of behavior tests regarding optionals.
2021-11-09add initial plan9 support to stdJacob G-W
2021-11-08stage2 x86_64: add MIR->Isel lowering step for x86_64Jakub Konka
* incorporate Andrew's MIR draft as Mir.zig * add skeleton for Emit.zig module - Emit will lower MIR into machine code or textual ASM. * implement push * implement ret * implement mov r/m, r * implement sub r/m imm and sub r/m, r * put encoding common ops together - some ops share impl such as MOV and cmp so put them together and vary the actual opcode with modRM ext only. * implement pop * implement movabs - movabs being a special-case of mov not handled by general mov MIR instruction due to requirement to handle 64bit immediates. * store imm64 as a struct `Imm64{ msb: u32, lsb: u32 }` in extra data for use with for instance movabs inst * implement more mov variations * implement adc * implement add * implement sub * implement xor * implement and * implement or * implement sbb * implement cmp * implement lea - lea doesn't follow the scheme as other inst above. Similarly, I think bit shifts and rotates should be put in a separate basket too. * implement adc_scale_src * implement add_scale_src * implement sub_scale_src * implement xor_scale_src * implement and_scale_src * implement or_scale_src * implement sbb_scale_src * implement cmp_scale_src * implement adc_scale_dst * implement add_scale_dst * implement sub_scale_dst * implement xor_scale_dst * implement and_scale_dst * implement or_scale_dst * implement sbb_scale_dst * implement cmp_scale_dst * implement mov_scale_src * implement mov_scale_dst * implement adc_scale_imm * implement add_scale_imm * implement sub_scale_imm * implement xor_scale_imm * implement and_scale_imm * implement or_scale_imm * implement sbb_scale_imm * implement cmp_scale_imm * port bin math to MIR * backpatch stack size into prev MIR inst * implement Function.gen() (minus dbg info) * implement jmp/call [imm] - we can now call functions using indirect absolute addressing, or via registers. * port airRet to use MIR * port airLoop to use MIR * patch up performReloc to use inst indices * implement conditional jumps (without relocs) * implement set byte on condition * implement basic lea r64, [rip + imm] * implement calling externs * implement callq in PIE * implement lea RIP in PIE context * remove all refs to Encoder from CodeGen * implement basic imul ops * pass all Linux tests! * enable most of dbg info gen * generate arg dbg info in Emit
2021-10-31stage2: move x86_64 codegen to arch/x86_64/CodeGen.zigJakub Konka
This mimics steps taken for aarch64 and preps stage2 x86_64 for a rewrite introducing MIR for this arch.