aboutsummaryrefslogtreecommitdiff
path: root/src/arch/wasm/CodeGen.zig
AgeCommit message (Collapse)Author
2022-02-03wasm: Implement vector_init for array & structsLuuk de Gram
Implements the instruction `vector_init` for structs and arrays. For arrays, it checks if the element must be passed by reference or not. When not, it can simply use the `offset` field of a store instruction to copy the values into the array. When it is byref, it will move the pointer by the element size, and then perform a store operation. This ensures types like structs will be moved into the right position. For structs we will always move the pointer, as we currently cannot verify if all fields are not by ref.
2022-02-03wasm: Implement elem_ptrLuuk de Gram
This implements lowering elem_ptr for decl's and constants. To generate the correct pointer, we perform a relocation by using the addend that represents the offset. The offset is calculated by taking the element's size and multiplying that by the index. For constants this generates a single immediate instruction, and for decl's this generates a single pointer address.
2022-02-02wasm32: fix struct paddingJakub Konka
2022-01-30stage2: implement shl_exact and shr_exactAndrew Kelley
These produce an undefined value when one bits are shifted out. New AIR instruction: shr_exact.
2022-01-25wasm: Implement genTypedValue for enumsLuuk de Gram
This makes all union test cases succeed. `rem` was also implemented as all we had to do is enable the instruction. Loading and storing values based on ABI-size was simplified to a direct abiSize() call. We also enabled all the newly passing test cases and disable them for all non-passing backends. All of those test cases were verified to see if they perhaps already pass for the c-backend.
2022-01-25wasm: Implement lowering unionsLuuk de Gram
2022-01-25wasm: Implement get_union_tagLuuk de Gram
2022-01-25wasm: Implement set_union_tagLuuk de Gram
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-19wasm: Re-use genTypedValue for constantsLuuk de Gram
When a constant will be passed by reference, such as a struct, we will call into genTypedValue to lower the constant to bytes and store them into the `rodata` section. We will then return the address of this constant as a `WValue`. This change means we will have all constants lowered during compilation time, and no longer have to sacrifice runtime to lower them onto the stack.
2022-01-19wasm: Split funcgen and declgenLuuk de Gram
This allows us to get rid of unused fields when generating code for non-function decls. We can now create seperate instances of `DeclGen` which in turn can then be used to generate the code for a constant. Besides those reasons, it will be much easier to switch to the generic purpose `codegen.zig` that any backend should use. Allowing us to deduplicate this code.
2022-01-19wasm: Refactor storing values.Luuk de Gram
Due to the new structure of lowerConstant, we can now simplify the logic in a lot of situations. - We no longer have to check the `WValue`'s tag to determine how to load/store a value. - We can now provide simple memcopy's for aggregate types. - Constants are now memoized, meaning we do no longer lower constants on each callsite.
2022-01-19wasm: Refactor emitConstant to lower to `WValue`Luuk de Gram
2022-01-18stage2: implement `@prefetch`Andrew Kelley
This reverts commit f423b5949b8722d4b290f57c3d06d015e39217b0, re-instating commit d48e4245b68bf25c7f41804a5012ac157a5ee546.
2022-01-18Revert "stage2: implement `@prefetch`"Andrew Kelley
This reverts commit d48e4245b68bf25c7f41804a5012ac157a5ee546. I have no idea why this is failing Drone CI, but in a branch, reverting this commit solved the problem.
2022-01-15stage2: implement `@prefetch`Andrew Kelley
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.
2022-01-13stage2: fix build on 32-bit ISAsAndrew Kelley
Fixes regression introduced in 93b854eb745ab3294054ae71150fe60f134f4d10.
2022-01-12stage2: implement `@ctz` and `@clz` including SIMDAndrew Kelley
AIR: * `array_elem_val` is now allowed to be used with a vector as the array type. * New instructions: splat, vector_init AstGen: * The splat ZIR instruction uses coerced_ty for the ResultLoc, avoiding an unnecessary `as` instruction, since the coercion will be performed in Sema. * Builtins that accept vectors now ignore the type parameter. Comment from this commit reproduced here: The accepted proposal #6835 tells us to remove the type parameter from these builtins. To stay source-compatible with stage1, we still observe the parameter here, but we do not encode it into the ZIR. To implement this proposal in stage2, only AstGen code will need to be changed. Sema: * `clz` and `ctz` ZIR instructions are now handled by the same function which accept AIR tag and comptime eval function pointer to differentiate. * `@typeInfo` for vectors is implemented. * `@splat` is implemented. It takes advantage of `Value.Tag.repeated` 😎 * `elemValue` is implemented for vectors, when the index is a scalar. Handling a vector index is still TODO. * Element-wise coercion is implemented for vectors. It could probably be optimized a bit, but it is at least complete & correct. * `Type.intInfo` supports vectors, returning int info for the element. * `Value.ctz` initial implementation. Needs work. * `Value.eql` is implemented for arrays and vectors. LLVM backend: * Implement vector support when lowering `array_elem_val`. * Implement vector support when lowering `ctz` and `clz`. * Implement `splat` and `vector_init`.
2022-01-10wasm: Basic 128bit integer supportLuuk de Gram
This implements storing, loading and comparing 128bit integers. TODO: Make all >64 bit integers make a call to compiler-rt for binary operations.
2022-01-10wasm: Implement optional compareLuuk de Gram
We now pass all optionals.zig behavior tests
2022-01-10wasm: Implement float_to_intLuuk de Gram
- This implements the float_to_int AIR instruction. - Lowering a decl_ref to a slice was previously assumed to contain a pointer to a slice, rather than an array. This is now fixed, making `@src()` work as well. - Some preliminary work on 128bit integers have been done to find out what needs to be done to implement 128bit arithmetic.
2022-01-08wasm: Implement arraysLuuk de Gram
2022-01-06Sema: const inferred alloc infers comptime-nessAndrew Kelley
const locals now detect if the value ends up being comptime known. In such case, it replaces the runtime AIR instructions with a decl_ref const. In the backends, some more sophisticated logic for marking decls as alive was needed to prevent Decls incorrectly being garbage collected that were indirectly referenced in such manner.
2022-01-04wasm: Implement memset, and sret arguments.Luuk de Gram
We now detect if the return type will be set by passing the first argument as a pointer to stack memory from the callee's frame. This way, we do not have to worry about stack memory being overwritten. Besides this, we implement memset by either using wasm's memory.fill instruction when available, or lower it manually. In the future we can lower this to a compiler_rt call.
2022-01-04wasm: Implement 'slice' instructionLuuk de Gram
Emitting undefined ptr's also has been implemented. This means we now pass the void.zig behavior tests.
2022-01-04wasm: Fix lowering constant struct values to the stackLuuk de Gram
We now get the null.zig, this.zig and member_func.zig behavior tests passing.
2022-01-04wasm: Implement (and fix) most optional instructionsLuuk de Gram
Previously we were performing the wrapping and unwrapping operations incorrectly. We now correctly create the type and set its values. Besides this, we also set the null-byte to the incorrect value, which meant we were doing the opposite action of a is_null check. This is now fixed as well. While implementing this, I found a small bug in the wrapErrUnionPayload where we would load a pointer value and save that, rather than store the pointer with the error. This is now fixed as well, by copying the entire operand into the payload of the error union.
2022-01-01wasm: Fix loading from pointers to support deferLuuk de Gram
Previously, the `load` instruction would just pass the pointer to the next instruction for types that comply to `isByRef`. However, this meant that a defer would directly write to the reference, rather than a copy. After this commit, we always copy the value.
2022-01-01wasm: Implement pointer arithmetic and refactoring:Luuk de Gram
- This implements all pointer arithmetic related instructions such as ptr_add, ptr_sub, ptr_elem_val - We refactored the code, to use `isByRef` to ensure consistancy. - Pointers will now be loaded correctly, rather then being passed around. - The behaviour test for pointers is now passing.
2022-01-01wasm: Implement memCpy and get if behavior tests passingLuuk de Gram
2022-01-01wasm: Implement @ptrToInt and fix indirect function callLuuk de Gram
- Previously the table index and function type index were switched. This commit swaps them. - This also emits the correct indirect function calls count when importing the function table
2022-01-01wasm: Implement `array_to_slice` and bug fixes:Luuk de Gram
- Add method to easily create local for virtual stack - Ensure function pointers are passed correctly - Correctly handle slices as return types and values - Fix wrapping error sets/payloads. - Handle ptr-like optionals correctly, by using address '0' as null. - Implement `array_to_slice` - linker: Always emit a table, so call_indirect inside bodies do not fail if there's no table. TODO: Only do this when we emit a call_indirect but the relocation cannot be resolved.
2022-01-01wasm: Pass 'bugs' behavior testsLuuk de Gram
2022-01-01wasm: Fix storing error. Pass bool.zig behavior testsLuuk de Gram
2021-12-05wasm: Initial behavior tests succeedingLuuk de Gram
- Correctly load slice value on stack - Implement WrapErrorUnionErr and payload - Implement trunc, fix sliceLen and write undefined - Implement slice as return type and argument Note: This also fixes a memory leak for inferred error sets, and for usingnamespace
2021-12-04Revert "Merge pull request #10270 from Luukdegram/behaviour-tests"Andrew Kelley
This reverts commit 725267f7c20f0ba588b472048a8c1fe1a328c714, reversing changes made to 2dae860de3494f97c9477af9282fe0131ff5c4cb. This test is failing: ```zig pub fn main() u8 { var e = foo(); const i = e catch 69; return i; } fn foo() anyerror!u8 { return 5; } ``` It's returning 69 instead of the expected value 5.
2021-12-04wasm: Initial behavior tests succeedingLuuk de Gram
Note: This also fixes a memory leak for inferred error sets, and for usingnamespace
2021-12-04wasm: Implement slice as return type and argumentLuuk de Gram
2021-12-04wasm: Implement trunc, fix sliceLen and write undefinedLuuk de Gram
2021-12-04wasm: Correctly load slice value on stackLuuk de Gram
2021-12-04wasm: Implement WrapErrorUnionErr(payload)Luuk de Gram
2021-11-30allocgate: std Allocator interface refactorLee Cannon
2021-11-29wasm: Update wasm stage2 test backend to use 'main'Luuk de Gram
2021-11-28wasm: Implement 'zig test'Luuk de Gram
- This implements the required codegen for decl types such as pointers, arrays, structs and more. - Wasm's start function can now use both a 'u8' and 'void' as return type. This will help us with writing tests using the stage2 testing backend. (Until all tests of behavioural tests pass). - Now correctly generates relocations for function pointers. - Also implements unwrapping error union error, as well as return pointers.
2021-11-28wasm-link: Implement indirect function tableLuuk de Gram
The function table contains all function pointers that are called by using call_indirect. During codegen, we create a relocation where the linker will resolve the correct index into the table and stores this value within the data section at the location of the pointer.
2021-11-27wasm: Implement slicesLuuk de Gram
2021-11-27wasm-linker: Resolve relocationsLuuk de Gram
We now resolve relocations for globals, memory addresses and function indexes. Besides above, we now also emit imported functions correctly and create a corresponding undefined symbol for it, where as we create a defined symbol for all other cases. TODO: Make incrememental compilation work again with new linker infrastructure
2021-11-27wasm-linker: Upstream zwld into stage2Luuk de Gram
- Converts previous `DeclBlock` into `Atom`'s to also make them compatible when the rest of zlwd gets upstreamed and we can link with other object files. - Resolves function signatures and removes any duplicates, saving us a lot of potential bytes for larger projects. - We now create symbols for each decl of the respective type - We can now (but not implemented yet) perform proper relocations. - Having symbols and segment_info allows us to create an object file for wasm.
2021-11-24stage2: fix unwrap function call with optional pointer return valueAndrew Kelley