aboutsummaryrefslogtreecommitdiff
path: root/src/AstGen.zig
AgeCommit message (Collapse)Author
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-28AstGen: coerce break operands of labeled blocksAndrew Kelley
Similar code was already in place for conditional branches. This updates AstGen to do the same for labeled blocks. It takes advantage of the `store_to_block_ptr` instructions by mutating them in place to become `as` instructions, coercing the break operands before they are returned from the block.
2022-03-28AstGen: clear rl_ty_inst in setBreakResultLoc if one is not providedVeikka Tuominen
2022-03-26stage2: result location types for function call argumentsAndrew Kelley
* AstGen: restore the param_type ZIR instruction and pass it to the expression for function call arguments. This does not solve the problem for generic function parameters, but it catches stage2 up to stage1 which also does not solve the problem for generic function parameters. - Most of the enhancements in this commit will still be needed for a more sophisticated further improvement to handle generic function types. - In Sema, handling of `as` coercion recognizes the `var_args_param` Type Tag and passes the operand through doing no coercion. - That was the last ZIR tag and we are now using all 256 ZIR tags. * AstGen: array init and struct init expressions use the anon form even when the result location has a type. Prevents the type system incorrectly believing, for example, that a tuple is actually an array when the result location is a param_type of a function with `anytype` parameter. * Sema: add missing coercion in `unionInit` to coerce the init to the corresponding union field type. * `Value.fieldValue` now takes a type and does not take an allocator. closes #11293 After this commit, stage2 passes all the parser tests.
2022-03-24AstGen: emit break_inline from inline while loopAndrew Kelley
2022-03-24AstGen: fix const locals with comptime initializationsAndrew Kelley
`const foo = comptime ...` generated invalid ZIR when the initialization expression contained an array literal because the validate_array_init_comptime instruction assumed that the corresponding alloc instruction was comptime. The solution is to look slightly ahead and notice that the initialization expression would be comptime-known and affect the alloc instruction tag accordingly.
2022-03-20add error when binary ops don't have matching whitespace on both sidesDaniel Hooper
This change also moves the warning about "&&" from the AstGen into the parser so that the "&&" warning can supersede the whitespace warning.
2022-03-19AstGen: always add dbg_block_end before last instructionVeikka Tuominen
2022-03-19stage2: add debug info for payload capturesVeikka Tuominen
2022-03-19stage2: add dbg_block_{begin,end} instructionVeikka Tuominen
2022-03-18AstGen: remove unused parameterAndrew Kelley
This function took a parameter that was only ever used with one value, obscuring the fact that it was a regular `block` which should be used with `.break` and not `.break_inline`.
2022-03-18AstGen: labeled blocks should always complete with a normal breakMitchell Hashimoto
They aren't inline blocks by nature of being labeled. Fixes #11213
2022-03-18stage2: improve `@typeName`Andrew Kelley
* make it always return a fully qualified name. stage1 is inconsistent about this. * AstGen: fix anon_name_strategy to correctly be `func` when anon type creation happens in the operand of the return expression. * Sema: implement type names for the "function" naming strategy. * Put "enum", "union", "opaque", or "struct" in place of "anon" when creating respective anonymous Decl names. * std.testing: add `expectStringStartsWith`. Didn't end up using it after all. Also this enables the real test runner for stage2 LLVM backend (sans wasm32) since it works now.
2022-03-16AstGen: emit dbg_stmt before function calls and branch conditionsVeikka Tuominen
2022-03-16stage2: move duplicate error set check to AstGenMitchell Hashimoto
2022-03-15stage2: comptime fields should not affect opv/comptime-onlyCody Tapscott
2022-03-15AstGen: add missing coercion for const localsAndrew Kelley
A const local which had its init expression write to the result pointer, but then gets elided to directly initialize, was missing the coercion to the type annotation.
2022-03-13stage2: add debug info for locals in the LLVM backendAndrew Kelley
Adds 2 new AIR instructions: * dbg_var_ptr * dbg_var_val Sema no longer emits dbg_stmt AIR instructions when strip=true. LLVM backend: fixed lowerPtrToVoid when calling ptrAlignment on the element type is problematic. LLVM backend: fixed alloca instructions improperly getting debug location annotated, causing chaotic debug info behavior. zig_llvm.cpp: fixed incorrect bindings for a function that should use unsigned integers for line and column. A bunch of C test cases regressed because the new dbg_var AIR instructions caused their operands to be alive, exposing latent bugs. Mostly it's just a problem that the C backend lowers mutable and const slices to the same C type, so we need to represent that in the C backend instead of printing two duplicate typedefs.
2022-03-12AstGen: fix nosuspendExpr handling result location twiceVeikka Tuominen
2022-03-10AstGen: structInitExpr and arrayInitExpr avoid crashAndrew Kelley
when an inferred alloc is passed as the result pointer of a block.
2022-03-10AstGen: lower anon struct inits differentlyAndrew Kelley
This is a companion commit to f2a5d0bf94897554e25e889dc1c6c4c7fc6c1217. What that one did for tuples, this one does for anonymous structs.
2022-03-09AstGen: ensure lableld block implicitly ends in a breakVeikka Tuominen
2022-03-09Sema: handle noreturn result in condbr_inlineVeikka Tuominen
2022-03-08Merge pull request #11079 from Vexu/stage2Andrew Kelley
stage2: make references to const allocs const
2022-03-08deprecated TypeInfo in favor of TypeJonathan Marler
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-03-08stage2: correct constness of allocsVeikka Tuominen
2022-03-07Merge pull request #11077 from mitchellh/array-init-tyAndrew Kelley
stage2: sentinel-terminated array initialization
2022-03-07stage2: new zir array_init_sent for sentinel-terminated array initsMitchell Hashimoto
This uses a new ZIR inst `array_init_sent` (and a ref equivalent) to represent array init expressions that terminate in a a sentinel value. The sentienl value is the last value in the `MultiOp` payload. This makes it a bit more awkward to deal with (lots of "len - 1") but makes it so that the payload matches the fact that sentinels appear at the end of arrays. However, this is not a hill I want to die on so if we want to change it to index 0, I'm happy to do so. This makes the following work properly: try expect(@TypeOf([_:0]u8{}) == [0:0]u8);
2022-03-07stage2: resolve array type for typed array init expressionsMitchell Hashimoto
Array types with sentinels were not being typed correctly in the translation from ZIR to Sema (comptime). This modifies the `array_init` ZIR to also retain the type of the init expression (note: untyped array initialization is done via the `array_init_anon` ZIR and so is unchanged in this commit).
2022-03-06stage2: rework `@mulAdd`Andrew Kelley
* mul_add AIR instruction: use `pl_op` instead of `ty_pl`. The type is always the same as the operand; no need to waste bytes redundantly storing the type. * AstGen: use coerced_ty for all the operands except for one which we use to communicate the type. * Sema: use the correct source location for requireRuntimeBlock in handling of `@mulAdd`. * native backends: handle liveness even for the functions that are TODO. * C backend: implement `@mulAdd`. It lowers to libc calls. * LLVM backend: make `@mulAdd` handle all float types. - improved fptrunc and fpext to handle f80 with compiler-rt calls. * Value.mulAdd: handle all float types and use the `@mulAdd` builtin. * behavior tests: revert the changes to testing `@mulAdd`. These changes broke the test coverage, making it only tested at compile-time. Improved f80 support: * std.math.fma handles f80 * move fma functions from freestanding libc to compiler-rt - add __fmax and fmal - make __fmax and fmaq only exported when they don't alias fmal. - make their linkage weak just like the rest of compiler-rt symbols. * removed `longDoubleIsF128` and replaced it with `longDoubleIs` which takes a type as a parameter. The implementation is now more accurate and handles more targets. Similarly, in stage2 the function CTypes.sizeInBits is more accurate for long double for more targets.
2022-03-04stage2: fix tuple assigned to variableAndrew Kelley
Before this we would see ZIR code like this: ``` %69 = alloc_inferred_mut() %70 = array_base_ptr(%69) %71 = elem_ptr_imm(%70, 0) ``` This would crash the compiler because it expects to see a `coerce_result_ptr` instruction after `alloc_inferred_mut`, but that does not happen in this case because there is no type to coerce the result pointer to. In this commit I modified AstGen so that it has similar codegen as when using a const instead of a var: ``` %69 = alloc_inferred_mut() %76 = array_init_anon(.{%71, %73, %75}) %77 = store_to_inferred_ptr(%69, %76) ``` This does not obey result locations, meaning if you call a function inside the initializer, it will end up doing a copy into the LHS. Solving this problem, or changing the language to make this legal, will be left for my future self to deal with. Hi future self! I see you reading this commit log. Hope you're doing OK buddy. Sema for `store_ptr` of a tuple where the pointer is in fact the same element type as the operand had an issue where the comptime fields would get incorrectly lowered to runtime stores to bogus addresses. This is solved with an exception to the optimization in Sema for storing pointers that handles tuples element-wise. In the case that we are storing a tuple to itself, it skips the optimization. This results in better code and avoids the problem. However this caused a regression in GeneralPurposeAllocator from the standard library. I regressed the test runner code back to the simpler path. It's too hard to debug standard library code in the LLVM backend right now since we don't have debug info hooked up. Also, we didn't have any behavior test coverage of whatever was regressed, so let's try to get that coverage added as a stepping stone to getting the standard library working.
2022-03-03stage2: cleanups to wasm memory intrinsicsAndrew Kelley
* AIR: use pl_op instead of ty_pl for wasm_memory_size. No need to store the type because the type is always `u32`. * AstGen: use coerced_ty for `@wasmMemorySize` and `@wasmMemoryGrow` and do the coercions in Sema. * Sema: use more accurate source locations for errors. * Provide more information in the compiler error message. * Codegen: use liveness data to avoid lowering unused `@wasmMemorySize`. * LLVM backend: add implementation - I wasn't able to test it because we are hitting a linker error for `-target wasm32-wasi -fLLVM`. * C backend: use `zig_unimplemented()` instead of silently doing wrong behavior for these builtins. * behavior tests: branch only on stage2_arch for inclusion of the wasm.zig file. We would change it to `builtin.cpu.arch` but that is causing a compiler crash on some backends.
2022-03-03wasm: Implement `@wasmMemoryGrow` builtinLuuk de Gram
Similarly to the other wasm builtin, this implements the grow variation where the memory index is a comptime known value. The operand as well as the result are runtime values. This also verifies during semantic analysis the target we're building for is wasm, or else emits a compilation error. This means that other backends do not have to handle this AIR instruction, other than the wasm and LLVM backends.
2022-03-03wasm: Implement `@wasmMemorySize()` builtinLuuk de Gram
This implements the `wasmMemorySize` builtin, in Sema and the Wasm backend. The Stage2 implementation differs from stage1 in the way that `index` must be a comptime value. The stage1 variant is incorrect, as the index is part of the instruction encoding, and therefore, cannot be a runtime value.
2022-03-02Zir: rename the 'ret_coerce' tag to 'ret_tok' as per TODOCurtis Wilkinson
2022-03-02stage2 parser: UTF-8 encode \u{NNNNNN} escape sequencesCody Tapscott
The core of this change is to re-use the escape sequence parsing logic for parsing both string and character literals. The actual fix is that UTF-8 encoding was missing for string literals with \u{...} escape sequences.
2022-03-01Sema: eliminate use of resolveAlreadyCoercedIntAndrew Kelley
2022-02-27stage2: forward discard result loc to more expressionsVeikka Tuominen
2022-02-26stage2: implement `@unionInit`Andrew Kelley
The ZIR instruction `union_init_ptr` is renamed to `union_init`. I made it always use by-value semantics for now, not taking the time to invest in result location semantics, in case we decide to change the rules for unions. This way is much simpler. There is a new AIR instruction: union_init. This is for a comptime known tag, runtime-known field value. vector_init is renamed to aggregate_init, which solves a TODO comment.
2022-02-26stage2: evaluate TypeOf arguments in a separate scopeVeikka Tuominen
2022-02-24stage2: improved handling of store_to_block_ptrAndrew Kelley
* AstGen: remove the setBlockBodyEliding function. This is no longer needed after 63788b2a511eb87974065a052e2436b0c6202544. * Sema: store_to_block_ptr instruction is handled as store_to_inferred_ptr or store, as necessary.
2022-02-24stage2: change how stale `store_to_block_ptr`s are detectedVeikka Tuominen
Instead of explicitly setting lhs to .none, check if the lhs instruction was analyzed. This simpler approach also handles stores from nested blocks correctly.
2022-02-24stage2: implement fieldParentPtrVeikka Tuominen
2022-02-23stage2: integer-backed packed structsAndrew Kelley
This implements #10113 for the self-hosted compiler only. It removes the ability to override alignment of packed struct fields, and removes the ability to put pointers and arrays inside packed structs. After this commit, nearly all the behavior tests pass for the stage2 llvm backend that involve packed structs. I didn't implement the compile errors or compile error tests yet. I'm waiting until we have stage2 building itself and then I want to rework the compile error test harness with inspiration from Vexu's arocc test harness. At that point it should be a much nicer dev experience to work on compile errors.
2022-02-21Merge pull request #10925 from Vexu/stage2Andrew Kelley
stage2: support anon init through error unions and optionals
2022-02-19AstGen: emit break_inline for corresponding inline loopsAndrew Kelley
Prior to this commit there would be a `break` ZIR instruction to break from a `block_inline` which is a mismatch.
2022-02-20stage2: validate struct/array init tyVeikka Tuominen
2022-02-19AstGen: evaluate `comptime var` init expressions in a comptime contextAndrew Kelley
2022-02-19stage2: support anon init through error unions and optionalsVeikka Tuominen
2022-02-13stage2: add decltestsJacob G-W