aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.zig
AgeCommit message (Collapse)Author
2022-06-11stage2: make `error{}` the same size as `anyerror`Veikka Tuominen
Having `error{}` be a zero bit type causes issues when it interracts with empty inferred error sets which are the same size as `anyerror`.
2022-05-27math: make `cast` return optional instead of an errorAli Chraghi
2022-05-24stage2: fixes for error unions, optionals, errorsAndrew Kelley
* `?E` where E is an error set with only one field now lowers the same as `bool`. * Fix implementation of errUnionErrOffset and errUnionPayloadOffset to properly compute the offset of each field. Also name them the same as the corresponding LLVM functions and have the same function signature, to avoid confusion. This fixes a bug where wasm was passing the error union type instead of the payload type. * Fix C backend handling of optionals with zero-bit payload types. * C backend: separate out airOptionalPayload and airOptionalPayloadPtr which reduces branching and cleans up control flow. * Make Type.isNoReturn return true for error sets with no fields. * Make `?error{}` have only one possible value (null).
2022-05-24aarch64: update for new error union layoutJakub Konka
2022-05-24x64: update for new error union layoutJakub Konka
2022-05-24stage2: make `?anyerror` represented the same as `anyerror`Andrew Kelley
I was able to get the backend implementation working on LLVM and the C backend, but I'm going to ask for some help on the other backends.
2022-05-24wasm: Fixes for error union semanticsLuuk de Gram
2022-05-24codegen: Order error union fields per alignmentLuuk de Gram
Based on the size of the payload the native backends will lower the error union with its fields (errorset & payload) in the correct order. e.g. ErrorA!u8 will first lower the error set's value and then the payload. In the event of ErrorA!u32 will lower the payload first.
2022-05-24stage2: string literal interningAndrew Kelley
This is a temporary addition to stage2 in order to match stage1 behavior, however the end-game once the lang spec is settled will be to use a global InternPool for comptime memoized objects, making this behavior consistent across all types, not only string literals. Or, we might decide to not guarantee string literals to have equal comptime pointers, in which case this commit can be reverted.
2022-05-13target: Rename sparcv9 -> sparc64Koakuma
Rename all references of sparcv9 to sparc64, to make Zig align more with other projects. Also, added new function to convert glibc arch name to Zig arch name, since it refers to the architecture as sparcv9. This is based on the suggestion by @kubkon in PR 11847. (https://github.com/ziglang/zig/pull/11487#pullrequestreview-963761757)
2022-04-20stage2: use indexes for Decl objectsAndrew Kelley
Rather than allocating Decl objects with an Allocator, we instead allocate them with a SegmentedList. This provides four advantages: * Stable memory so that one thread can access a Decl object while another thread allocates additional Decl objects from this list. * It allows us to use u32 indexes to reference Decl objects rather than pointers, saving memory in Type, Value, and dependency sets. * Using integers to reference Decl objects rather than pointers makes serialization trivial. * It provides a unique integer to be used for anonymous symbol names, avoiding multi-threaded contention on an atomic counter.
2022-04-15stage2: lower u128, and refactor some bits in x64Jakub Konka
2022-04-13dwarf: gen debug info for arraysJakub Konka
2022-03-30dwarf: pass DeclState around instead of storing a temp global in DwarfJakub Konka
Avoids many pitfalls connected with premature/early return in case there are errors with Decl, etc. This is effectively bringing back the old design however in a much nicer packaging, where every mechanism related to tracking Decl's debug info is now nicely wrapped in a single struct (aka the `DeclState`). This includes relocation table, type arena, etc. It is now the caller's responsibility to deinit the state (so that no memory is leaked) after `Decl` has been analysed (or errored out). The caller here is typically a linker such as `Elf` or `MachO`.
2022-03-27dwarf: track type relocation state in Dwarf moduleJakub Konka
2022-03-27dwarf: move DbgInfoTypeRelocsTable into Dwarf moduleJakub Konka
2022-03-27dwarf: add debug info for error unionsJakub Konka
2022-03-27dwarf: add debug info for error setsJakub Konka
2022-03-22dwarf: lower enumsJakub Konka
2022-03-22stage2: lazy `@alignOf`Andrew Kelley
Add a `target` parameter to every function that deals with Type and Value.
2022-03-21Merge pull request #11224 from koachan/sparc64-codegenAndrew Kelley
stage2 sparcv9: Add instruction encoder and placeholder codegen impl
2022-03-21stage2: add way to print values with typesVeikka Tuominen
2022-03-19wasm: All union/tuple/array tests passingLuuk de Gram
This implements improvements/fixes to get all the union, tuple, and array behavior tests passing. Previously, we lowered parent pointers for field_ptr and element_ptr incompletely. This has now been improved to recursively lower such pointer. Also a fix was done to `generateSymbol` when checking a container's layout. Previously it was assumed to always be a struct. However, the type can also be a tuple, and therefore panicking. Updating to ask a type's container layout instead allows us to keep a singular branch for both cases.
2022-03-19stage2 sparcv9: Add placeholder files and generate() functionKoakuma
Add placeholder files for Codegen, Emit, and Mir stages, complete with a placeholder implementation of generate() to make it able to be plugged in to the frontend. At the moment the implementation just panics, it'll be worked on incrementally later. Also, this registers the sparcv9 backend files into CMakeLists.txt.
2022-03-14stage2: apply fix for #11165 to codegen.zig for native backendsMitchell Hashimoto
Co-authored-by: Cody Tapscott <topolarity@tapscott.me>
2022-03-14stage2: rework Value storage of structs and arraysAndrew Kelley
Now they both use `Value.Tag.aggregate`. Additionally the LLVM backend now has implemented lowering of tuple values.
2022-03-10stage2: implement integer pointer constantsAndrew Kelley
2022-03-06wasm: Unify function generationLuuk de Gram
Like decl code generation, also unify the wasm backend and the wasm linker to call into the general purpose `codegen.zig` to generate the code for a function.
2022-03-06stage2: Fix codegen for unions and error unionsLuuk de Gram
When an union had a zero-sized payload type, we would lower the tag twice. This is fixed by exiting early when `payload_size` is 0. With regards to error unions, we were only accounting for padding for the payload field. However, the errorset value can have a smaller alignment than the payload as well, i.e. error!usize. We fix this by also accounting for padding/alignment of the error set tag of an error union.
2022-03-05elf: add debug info for non-ptr optionalsJakub Konka
2022-03-02codegen: handle elem_ptr when lowering to memoryJakub Konka
* x64: handle storing from-to non-stack memory
2022-03-02codegen: leave f80 explicitly unhandled for nowJakub Konka
2022-03-02codegen: lower optionals and floats across linking backendsJakub Konka
2022-03-01codegen: ensure we descend on nested field_ptrs when loweringJakub Konka
2022-03-01codegen: lower field_ptr to memory across linking backendsJakub Konka
This requires generating an addend for the target relocation as the field pointer might point at a field inner to the container.
2022-03-01Merge pull request #11016 from ziglang/x64-more-codegenJakub Konka
2022-02-28Sema: fix pointer type hash and equality functionsAndrew Kelley
Several issues with pointer types are fixed: Prior to this commit, Zig would not canonicalize a pointer type with an explicit alignment to alignment=0 if it matched the pointee ABI alignment. In order to fix this, `Type.ptr` now takes a Target parameter. I also moved the host_size canonicalization to `Type.ptr` since target is now available. Similarly, is_allowzero in the case of C pointers is now treated as a canonicalization done by the function rather than a precondition. in-memory coercion for pointers now properly checks ABI alignment of pointee types instead of incorrectly treating the 0 value as an alignment. Type equality is completely reworked based on the tag() rather than the zigTypeTag(). It's still semantically based on zigTypeTag() but that knowledge is implied rather than dictating the control flow of the logic. Importantly, this fixes cases for opaques, structs, tuples, enums, and unions, where type equality was incorrectly returning based on whether the tag() values were equal. Additionally, pointer type equality now takes into account alignment. Because we canonicalize non-zero alignment which equals pointee type ABI alignment to alignment=0, this now can be a simple integer comparison. Type hashing is implemented for pointers and floats. Array types now additionally hash their sentinels. This regressed some behavior tests that were passing but only because of bugs regarding type equality. The C backend has a noticeable problem with lowering differently-aligned pointers (particularly slices) as the same type, causing C compilation errors due to duplicate declarations.
2022-02-28codegen: fix padding calculation for error unions when loweringJakub Konka
* do not track `rdi` register before `call` inst, but instead freeze it from further use, until `call` has been realised * pass more error union tests
2022-02-28codegen: impl lowering of union type to memoryJakub Konka
2022-02-22x64: fix lowering of error unions (we didn't pad to alignment)Jakub Konka
* fix returning large values on stack from procedure calls - we need to explicitly specify source and dest base registers for `genSetStack` as well
2022-02-22codegen: lower repeated and empty_with_sentinel array typeJakub Konka
2022-02-22codegen: lower error_set and error_unionJakub Konka
2022-02-11elf: store pointer relocations indexed by containing atomJakub Konka
In `getDeclVAddr`, it may happen that the target `Decl` has not been allocated space in virtual memory. In this case, we store a relocation in the linker-global table which we will iterate over when flushing the module, and fill in any missing address in the final binary. Note that for optimisation, if the address was resolved at the time of a call to `getDeclVAddr`, we skip relocating this atom. This commit also adds the glue code for lowering const slices in the ARM backend.
2022-02-11codegen: handle lowering of const slice pointersJakub Konka
2022-02-09stage2: handle decl ref to void typesJakub Konka
Fixes behavior test 1914
2022-02-08stage2: tiny improvements all over the placeJakub Konka
* pass more x64 behavior tests * return with a TODO error when lowering a decl with no runtime bits * insert some debug logs for tracing recursive descent down the type-value tree when lowering types * print `Decl`'s name when print debugging `decl_ref` value
2022-02-07stage2,x64: impl lowering of shift ops in EmitJakub Konka
2022-02-02stage2: pad out (non-packed) struct fields when lowering to bytesJakub Konka
* pad out (non-packed) struct fields when lowering to bytes to be saved in the binary - prior to this change, fields would be saved at non-aligned addresses leading to wrong accesses * add a matching test case to `behavior/struct.zig` tests * fix offset to field calculation in `struct_field_ptr` on `x86_64`
2022-01-24stage2: rework a lot of stuffAndrew Kelley
AstGen: * rename the known_has_bits flag to known_non_opv to make it better reflect what it actually means. * add a known_comptime_only flag. * make the flags take advantage of identifiers of primitives and the fact that zig has no shadowing. * correct the known_non_opv flag for function bodies. Sema: * Rename `hasCodeGenBits` to `hasRuntimeBits` to better reflect what it does. - This function got a bit more complicated in this commit because of the duality of function bodies: on one hand they have runtime bits, but on the other hand they require being comptime known. * WipAnonDecl now takes a LazySrcDecl parameter and performs the type resolutions that it needs during finish(). * Implement comptime `@ptrToInt`. Codegen: * Improved handling of lowering decl_ref; make it work for comptime-known ptr-to-int values. - This same change had to be made many different times; perhaps we should look into merging the implementations of `genTypedValue` across x86, arm, aarch64, and riscv.
2022-01-15stage2: fix Decl garbage collection not marking enoughAndrew Kelley
It is the job of codegen backends to mark Decls that are referenced as alive so that the frontend does not sweep them with the garbage. This commit unifies the code between the backends with an added method on Decl. The implementation is more complete than before, switching on the Decl val tag and recursing into sub-values. As a result, two more array tests are passing.