aboutsummaryrefslogtreecommitdiff
path: root/src/arch/wasm/CodeGen.zig
AgeCommit message (Collapse)Author
2022-06-09introduce std.debug.TraceAndrew Kelley
And use it to debug a LazySrcLoc in stage2 that is set to a bogus value. The actual fix in this commit is: ```diff - try sema.emitBackwardBranch(&child_block, call_src); + try sema.emitBackwardBranch(block, call_src); ```
2022-06-06Merge pull request #11783 from ziglang/stage2-tryAndrew Kelley
introduce a "try" ZIR and AIR instruction
2022-06-06dwarf: fix incorrect type reloc for unionsJakub Konka
Split type relocs into two kinds: local and global. Global relocs use a global type resolver and calculate offset to the existing definition of a type abbreviation. Local relocs use offset in the abbrev section of the containing atom plus addend to generate a local relocation.
2022-06-05wasm: Implement `try` instructionLuuk de Gram
2022-06-05stage2: implement the new "try" ZIR/AIR instructionAndrew Kelley
Implements semantic analysis for the new try/try_inline ZIR instruction. Adds the new try/try_ptr AIR instructions and implements them for the LLVM backend. Fixes not calling rvalue() for tryExpr in AstGen. This is part of an effort to implement #11772.
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-24wasm: use errUnionPayloadOffset and errUnionErrOffset from codegen.zigJakub 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-17stage2: fix pointer arithmetic result typeAndrew Kelley
This makes it so the result of doing pointer arithmetic creates a new pointer type that has adjusted alignment.
2022-05-18wasm: Implement {add/sub}WithOverflow for 128bitLuuk de Gram
2022-05-18wasm: Support `not` instruction for 128 bit integersLuuk de Gram
This also fixes the instruction for all other integer bitsizes, as it was previously assuming to always be a bool. 128 bit substraction was also fixed as it contained a bug where it swapped lhs with rhs.
2022-05-18wasm: Implement trunc/wrap for 128 bit integersLuuk de Gram
This also implments wrapping for arbitrary integer widths between 64 and 128. `@truncate` was fixed where the wasm types between operand and result differentiated. We solved this by first casting and then wrapping.
2022-05-18wasm: Support 128bit integers for max/min/ctz/clzLuuk de Gram
`airMaxMin` was slightly updated to automatically support 128 bit integers, by using the `cmp` function, instead of doing it manually. This makes the function more maintanable as well. `ctz` and `clz` now support 128 bit integers, while updating the previous implementation also.
2022-05-18wasm: Fix C-ABI for 128 bit integersLuuk de Gram
We now pass the correct wasm type when the return type is a 128-bit integer. When a function accepts a 128-bit integer, we now allocate space on the virtual stack and store both arguments within that space as currently all following instructions assume the 128 bit integer doesn't live in a local, but the stack.
2022-05-18wasm: 128 bit intcast and binary operationsLuuk de Gram
Also fixes some bugs in 128-bit binary comparisons where we checked if the lsb were equal, rather than msb.
2022-05-18wasm: Support 128bit add/sub wrapping operandsLuuk de Gram
2022-05-18wasm: 128bit integer cmp supportLuuk de Gram
This implements support for all compare operations on a 128bit integer, for both signed and unsigned integers. The new implementation is almost more efficient as it requires no control-flow, unlike the old implementation which used a block with breaks.
2022-05-18wasm: Support 128bit integer coercionLuuk de Gram
The Wasm backend now correctly supports coercing a smaller integer into a 128bit integer. Regardless of signedness.
2022-05-16stage2: implement error return tracesVeikka Tuominen
2022-05-16wasm: Improve shl_with_overflowLuuk de Gram
This re-implements the shl_with_overflow operation from scratch, making it a lot more robust and outputs the equal code to the LLVM backend.
2022-05-16wasm: Improve overflow add/sub for ints <= 64bitsLuuk de Gram
The implementation for add_with_overflow and sub_with_overflow is now a lot more robust and takes account for signed integers and arbitrary integer bitsizes. The final output is equal to that of the LLVM backend.
2022-05-09wasm: Write nops for padding debug infoLuuk de Gram
2022-05-09wasm: Debug info for lines + pro/epilogueLuuk de Gram
Maps lines and columns between wasm bytecode and Zig source code. While this supports prologue and epilogue information, we need to add support for performing relocations as the offsets are relative to the code section, which means we must relocate it according to the atom offset's offset while keeping function count in mind as well (due to leb128 encoding).
2022-05-09wasm: Debug information for localsLuuk de Gram
Implements very basic debug information for locals. For now it only implements debug info when the variable is stored within a Wasm local. The goal is to support those that live in the data section (virtual stack).
2022-05-09wasm: Implement debug info for parametersLuuk de Gram
2022-05-07wasm: Fix `@floatToInt` and split overflow opsLuuk de Gram
As we now store negative signed integers as two's complement, we must also ensure that when truncating a float, its value is wrapped around the integer's size. This also splits `@mulWithOverflow` into its own function to make the code more maintainable and reduce branching.
2022-05-07wasm: `@addWithOverflow` for bitsize 32Luuk de Gram
2022-05-06wasm: Store signed ints as two's complementLuuk de Gram
When a signed integer is negative, the integer will be stored as a two's complement, rather than its signed value. Instead, we verify the signed bits during arithmetic operations. This fixes signed cases of `@mulWithOverflow`.
2022-04-27add new builtin function `@tan`Andrew Kelley
The reason for having `@tan` is that we already have `@sin` and `@cos` because some targets have machine code instructions for them, but in the case that the implementation needs to go into compiler-rt, sin, cos, and tan all share a common dependency which includes a table of data. To avoid duplicating this table of data, we promote tan to become a builtin alongside sin and cos. ZIR: The tag enum is at capacity so this commit moves `field_call_bind_named` to be `extended`. I measured this as one of the least used tags in the zig codebase. Fix libc math suffix for `f32` being wrong in both stage1 and stage2. stage1: add missing libc prefix for float functions.
2022-04-26wasm: Implement codegen for C-ABILuuk de Gram
This implements passing arguments and storing return values correctly for the C-ABI as specified by the tool-convention: https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md There's definitely room for better codegen in follow-up commits.
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-19Merge pull request #11461 from Luukdegram/wasm-debug-infoAndrew Kelley
Wasm: add support for debug information
2022-04-19wasm: Fix unreachable pathsLuuk de Gram
When the last instruction is a debug instruction, the type of it is void. Similarly for 'noreturn' emit an 'unreachable' instruction to tell the wasm-validator the path cannot be reached. Also respect the '--strip' flag in the self-hosted wasm linker and not emit a 'name' section when the flag is set to `true`.
2022-04-19wasm: fix lowerDeclRefValue using wrong DeclAndrew Kelley
2022-04-14stage2: progress towards stage3Andrew Kelley
* The `@bitCast` workaround is removed in favor of `@ptrCast` properly doing element casting for slice element types. This required an enhancement both to stage1 and stage2. * stage1 incorrectly accepts `.{}` instead of `{}`. stage2 code that abused this is fixed. * Make some parameters comptime to support functions in switch expressions (as opposed to making them function pointers). * Avoid relying on local temporaries being mutable. * Workarounds for when stage1 and stage2 disagree on function pointer types. * Workaround recursive formatting bug with a `@panic("TODO")`. * Remove unreachable `else` prongs for some inferred error sets. All in effort towards #89.
2022-04-05wasm: Use 'select' instruction for max/minLuuk de Gram
Rather than using blocks and control flow to check which operand is the maximum or minimum, we use wasm's `select` instruction which returns us the operand based on a result from a comparison. This saves us the need of control flow, as well as reduce the instruction count from 13 to 7.
2022-04-02wasm: Implement `@ctz` for bitsize <= 64Luuk de Gram
Implements the `ctz` AIR instruction for integers with bitsize <= 64. When the bitsize of the integer does not match the bitsize of a wasm type, we first XOR the value with the value of (1<<bitsize) to set the right bits and ensure we will only count the trailing zeroes of the integer with the correct bitsize.
2022-04-02wasm: Implement `@clz`Luuk de Gram
Implements the `clz` AIR instruction for integers with bitsize <= 64. When the bitsize of the integer is not the same as wasm's bitsize, we substract the difference in bits as those will always be 0 for the integer, but should not be counted towards the end result. We also wrap the result to ensure it fits in the result type as documented in the language reference.
2022-04-02wasm: Implement `@mulAdd` for f32, f64Luuk de Gram
This implements the `mul_add` AIR instruction for floats of bitsize 32 and 64. f16's will require us being able to extend and truncate f16's to correctly store and load them without losing the accuracy.
2022-04-02wasm: Implement `@maximum` & `@minimum`Luuk de Gram
This implements the `max` and `min` AIR instructions by checking whether LHS is great/lesser than RHS. If that's the case, we assign LHS to the result, otherwise assign RHS to it instead.
2022-03-30Sema: enhance is_non_err to be comptime more oftenAndrew Kelley
* Sema: store the precomputed monomorphed_funcs hash inside Module.Fn. This is important because it may be accessed when resizing monomorphed_funcs while this Fn has already been added to the set, but does not have the owner_decl, comptime_args, or other fields populated yet. * Sema: in `analyzeIsNonErr`, take advantage of the AIR tag being `wrap_errunion_payload` to infer that `is_non_err` is comptime true without performing any error set resolution. - Also add some code to check for empty inferred error sets in this function. If necessary we do resolve the inferred error set. * Sema: queue full type resolution of payload type when `wrap_errunion_payload` AIR instruction is emitted. This ensures the backend may check the alignment of it. * Sema: resolveTypeFully now additionally resolves comptime-only status. closes #11306
2022-03-29stage2: implement `@intToError` with safetyAndrew Kelley
This commit introduces a new AIR instruction `cmp_lt_errors_len`. It's specific to this use case for two reasons: * The total number of errors is not stable during semantic analysis; it can only be reliably checked when flush() is called. So the backend that is lowering the instruction must emit a relocation of some kind and then populate it during flush(). * The fewer AIR instructions in memory, the better for compiler performance, so we squish complex meanings into AIR tags without hesitation. The instruction is implemented only in the LLVM backend so far. It does this by creating a simple function which is gutted and re-populated with each flush(). AstGen now uses ResultLoc.coerced_ty for `@intToError` and Sema does the coercion.
2022-03-27wasm: Implement overflow arithmeticLuuk de Gram
This implements the overflow arithmetic for unsigned and signed integers. Meaning the following instructions: - @addWithOverflow - @subWithOverflow - @shlWithOverflow - @mulWithOverflow
2022-03-26wasm: Only generate import when referencedLuuk de Gram
Rather than creating an import for externs on updateDecl, we now generate them when they're referenced. This is required so using @TypeOf(extern_fn()) will not emit the import into the binary (causing an incorrect function type index as it won't be fully analyzed).
2022-03-25sema: use `pl_op` for `@select`John Schmidt
2022-03-25stage2: implement `@select`John Schmidt
2022-03-23Sema: introduce a type resolution queueAndrew Kelley
That happens after a function body is analyzed. This prevents circular dependency compile errors and yet a way to mark types that need to be fully resolved before a given function is sent to the codegen backend.
2022-03-23wasm: Implement more instructionsLuuk de Gram
Implements the following instructions: - int_to_float - ptr_slice_len_ptr - ptr_slice_ptr_ptr - unwrap_errunion_payload_ptr - unwrap_errunion_err_ptr