aboutsummaryrefslogtreecommitdiff
path: root/src/AstGen.zig
AgeCommit message (Collapse)Author
2022-02-08stage2: fix inferred comptime constant localsAndrew Kelley
`const` declarations inside comptime blocks were not getting properly evaluated at compile-time. To accomplish this there is a new ZIR instruction, `alloc_inferred_comptime`. Actually we already had one named that, but it got renamed to `alloc_inferred_comptime_mut` to match the naming convention with the other similar instructions.
2022-02-06AstGen: refactor redundant expressionsAndrew Kelley
This is a non-functional change.
2022-02-06stage2: add more float arithmetic and f80 supportAndrew Kelley
AstGen: Fixed bug where f80 types in source were triggering illegal behavior. Value: handle f80 in floating point arithmetic functions. Value: implement floatRem and floatMod This commit introduces dependencies on compiler-rt that are not implemented. Those are a prerequisite to merging this branch.
2022-02-06stage2: apply type coercion in if expressionsJohn Schmidt
When setting the break value in an if expression we must explicitly check if a result location type coercion that needs to happen. This was already done for switch expression, so let's just imitate that check and fix for if expressions. To make this possible, we now also propagate `rl_ty_inst` to sub scopes.
2022-02-01stage2: remove anytype fields from the languageAndrew Kelley
closes #10705
2022-01-31astgen: clean up source line calculation and managementJakub Konka
Clarify that `astgen.advanceSourceCursor` already increments absolute values of the line and columns numbers; i.e., `GenZir.calcLine` is thus not only obsolete but wrong by design. Incidentally, this clean up allows for specifying the `FnDecl` line numbers for DWARF use correctly as relative values with respect to the start of the parent `Decl`. This `Decl` in turn has its line number information specified relatively to its parent `Decl`, and so on, until we reach the global scope.
2022-01-28stage1: add f80 typeVeikka Tuominen
2022-01-26stage2: fix crash on file-level struct field source locationsAndrew Kelley
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-24stage2: type system treats fn ptr and body separatelyAndrew Kelley
This commit updates stage2 to enforce the property that the syntax `fn()void` is a function *body* not a *pointer*. To get a pointer, the syntax `*const fn()void` is required. ZIR puts function alignment into the func instruction rather than the decl because this way it makes it into function types. LLVM backend respects function alignments. Struct and Union have methods `fieldSrcLoc` to help look up source locations of their fields. These trigger full loading, tokenization, and parsing of source files, so should only be called once it is confirmed that an error message needs to be printed. There are some nice new error hints for explaining why a type is required to be comptime, particularly for structs that contain function body types. `Type.requiresComptime` is now moved into Sema because it can fail and might need to trigger field type resolution. Comptime pointer loading takes into account types that do not have a well-defined memory layout and does not try to compute a byte offset for them. `fn()void` syntax no longer secretly makes a pointer. You get a function body type, which requires comptime. However a pointer to a function body can be runtime known (obviously). Compile errors that report "expected pointer, found ..." are factored out into convenience functions `checkPtrOperand` and `checkPtrType` and have a note about function pointers. Implemented `Value.hash` for functions, enum literals, and undefined values. stage1 is not updated to this (yet?), so some workarounds and disabled tests are needed to keep everything working. Should we update stage1 to these new type semantics? Yes probably because I don't want to add too much conditional compilation logic in the std lib for the different backends.
2022-01-23AstGen: doc comment fixupsAndrew Kelley
* AstGen: use Ast.zig helper methods to avoid copy pasting token counting logic - take advantage of the `first_doc_comment` field we already have for param AST nodes * Add missing ZIR docs
2022-01-21add function param doc comment info in zirLoris Cro
2022-01-21add support for more decl attributes in doc comment zirLoris Cro
The previous commit that implemented doc comment zir support for decls did not properly account for all the possible attribute keyword combinations (threadlocal, extern, and such).
2022-01-21add field doc comments to zirLoris Cro
Doc comment information is stored in `extra` unconditionally for each field. This commmit covers Structs, Enums, Unions, and ErrSets.
2022-01-21astgen saves decl doc comments in zirLoris Cro
The field is saved in `extra` unconditionally for each decl.
2022-01-20stage2: implement tuplesAndrew Kelley
* AIR instruction vector_init gains the ability to init arrays and tuples in addition to vectors. This will probably also gain the ability to initialize structs and be renamed to `aggregate_init`. * AstGen prefers to use an `anon_array_init` ZIR instruction for local variables when the init expr is an array literal and there is no type.
2022-01-17Sema: implement else capture valueAndrew Kelley
The ZIR instructions `switch_capture_else` and `switch_capture_ref` are removed because they are not needed. Instead, the prong index is set to max int for the special prong. Else prong with error sets is not handled yet. Adds a new behavior test because there was not a prior on to cover only the capture value of else on a switch.
2022-01-17stage2: do not interpret identifier containing underscores (eg: u3_2) as int ↵riverbl
primitive type
2022-01-15AstGen: emit `as` instructions for branching expressionsAndrew Kelley
There is a mechanism to avoid redundant `as` ZIR instructions which is to pass `ResultLoc.coerced_ty` instead of `ResultLoc.ty` when it is known by AstGen that Sema will do the coercion. This commit downgrades `coerced_ty` to `ty` when a result location passes through an expression that branches, such as `if`, `switch`, `while`, and `for`, causing the `as` ZIR instruction to be emitted. This ensures that the type of a result location will be applied to, e.g. a `comptime_int` on either side of a branch on a runtime condition.
2022-01-13stage2: detection of comptime array literalsAndrew Kelley
Introduce `validate_array_init_comptime`, similar to `validate_struct_init_comptime` introduced in 713d2a9b3883942491b40738245232680877cc66. `zirValidateArrayInit` is improved to detect comptime array literals and emit AIR accordingly. This code is very similar to the changes introduced in that same commit for `zirValidateStructInit`. The C backend needed some improvements to continue passing the same set of tests: * `resolveInst` for arrays now will add a local `static const` with the array value and so then `elem_val` instructions reference that local. It memoizes accesses using `value_map`, which is changed to use `Air.Inst.Ref` as the key rather than `Air.Inst.Index`. * This required a mechanism for writing to a "header" which is lines that appear at the beginning of a function body, before everything else. * dbg_stmt output comments rather than `#line` directives. TODO comment reproduced here: We need to re-evaluate whether to emit these or not. If we naively emit these directives, the output file will report bogus line numbers because every newline after the #line directive adds one to the line. We also don't print the filename yet, so the output is strictly unhelpful. If we wanted to go this route, we would need to go all the way and not output newlines until the next dbg_stmt occurs. Perhaps an additional compilation option is in order? `Value.elemValue` is improved to support `elem_ptr` values.
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-08stage2: implement @srcRobin Voetter
2022-01-04Sema: better code generated for struct literalsAndrew Kelley
Add a variant of the `validate_struct_init` ZIR instruction: `validate_struct_init_comptime` which is the same thing except it indicates a comptime scope. Sema code for this instruction now handles default struct field values and detects when the struct initialization resulted in a comptime value, replacing the already-emitted AIR instructions to store each individual field with a single `store` instruction with a comptime struct value as the operand. In the case of a comptime scope, there is a simpler path that only evals the implicit store instructions for default field values, avoiding the mechanism for detecting comptime values. This regressed one test case for the wasm backend, but it's just hitting a different prong of `emitConstant` which currently has "TODO" in there, so I think it's fine.
2021-12-30For unused references & redundant keywords, append the compiler error but ↵Jarred Sumner
continue running AstGen
2021-12-27AstGen: fix loop result locationsAndrew Kelley
The main problem was that the loop body was treated as an expression that was one of the peer result values of a loop, when in reality the loop body is noreturn and only the `break` operands are the result values of loops. This was solved by introducing an override that prevents rvalue() from emitting a store to result location instruction for loop bodies. An orthogonal change also included in this commit is switching `elem_val` index expressions to using `coerced_ty` and doing the coercion to `usize` inside `Sema`, resulting in smaller ZIR (since the cast becomes implied). I also changed the break operand expression to use `reachableExpr`, introducing a new compile error for double break. This makes a few more behavior tests pass for `while` and `for` loops.
2021-12-21stage2: wrap function prototypes in an inline block.Robin Voetter
Previously, function parameter instructions for function prototypes would be generated in the parent block. This caused issues in blocks where multiple prototypes would be generated in, such as the block for struct fields for example. This change introduces an inline block around every prototype such that all parameters for a prototype are confined to a unique block.
2021-12-19stage1, stage2: rename c_void to anyopaque (#10316)Isaac Freund
zig fmt now replaces c_void with anyopaque to make updating code easy.
2021-12-10AstGen: implement @prefetch() builtinIsaac Freund
2021-12-10AstGen: increase zig fmt off/on granularityIsaac Freund
This enables automatic formatting for a significant amount of code that currently doesn't deviate from the standard zig fmt enforced style.
2021-12-02AstGen: expr-evaluate asm template expressionsAndrew Kelley
See previous commit 1912ec0323af9a9077a8706157beb8207f6e3eb9 for more context. closes #10262
2021-12-01AstGen: use null string to communicate non-string-literal asmAndrew Kelley
dd62a6d2e8de522187fd096354e7156cca1821c5 short-circuited the logic of `asmExpr` by emitting ZIR for `@compileError("...")`. This caused false positive "unreachable code" errors for stage1 when there was an expression in the asm template. This commit makes such cases instead go through logic of `asmExpr` like normal, however the asm template is set to 0. This is then picked up in Sema (part of stage2, not stage1) and reported as "assembly code must use string literal syntax".
2021-11-30Merge pull request #10055 from leecannon/allocator_refactorAndrew Kelley
Allocgate
2021-11-30AstGen: allow non-string-literal inline assembly for stage1Andrew Kelley
The end-game for inline assembly is that the syntax is more integrated with zig, and it will not allow string concatenation for the assembler code, for the same reasons that Zig does not have a preprocessor. However, inline assembly in zig right now is lacking for a variety of use cases (take a look at the open issues having to do with inline assembly for example), and being able to use comptime expressions to concatenate text is a workaround that real-world users are exploiting to get by in the short term. This commit keeps "assembly code must use string literal syntax" as a compile error when using stage2, but allows it through when using stage1. I expect to revert this commit after making enough improvements to inline assembly that our real world users' needs are satisfied.
2021-11-30allocgate: renamed getAllocator function to allocatorLee Cannon
2021-11-30allocgate: stage 1 and 2 buildingLee Cannon
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-29AstGen: require binary operations to have reachable operandsAndrew Kelley
2021-11-24AstGen: use reachableExpr for return operandAndrew Kelley
Related: #9630
2021-11-01astgen.zig: avoid temporary allocations by sharing the `instructions` ↵Matthew Borkowski
ArrayList between a GenZir and its sub-blocks wherever their use of it is strictly nested
2021-11-01astgen.zig: delay adding closure_capture instructions to preserve GenZir ↵Matthew Borkowski
nesting. Only containers create Namespaces, so the declaring_gz is always the GenZir passed to containerDecl, and containerDecl will always add exactly one instruction (an extended *_decl) to that GenZir. Thus, closure_capture instructions are always lined up immediately after a container decl instruction, so rather than adding them at the point of first mention, where we're nested arbitrarily deep, simply walk through the Namespace captures hash map at the end of each containerDecl branch and add them then.
2021-11-01astgen.zig: use scratch buffer for temporary allocations in switchExpr and ↵Matthew Borkowski
WipMembers
2021-11-01astgen.zig: simplify container functions by pulling out common processing of ↵Matthew Borkowski
members
2021-11-01astgen.zig: replace WipDecls with WipMembers, use one allocation to collect ↵Matthew Borkowski
container decls, fields, and bits, instead of up to four
2021-11-01astgen.zig: simplify switchExpr and collect payload in one ArrayList instead ↵Matthew Borkowski
of three
2021-11-01astgen.zig: avoid unnecessary allocation in identifier for @"" syntaxMatthew Borkowski
2021-11-01astgen.zig: avoid temporary allocations in arrayInit* and structInit*, ↵Matthew Borkowski
callExpr, errorSetDecl, typeOf, and builtinCall's compile_log branch
2021-10-31optimize AstGen.callExprLee Cannon
2021-10-26astgen.zig: don't add scopes for extern functions params as they may shadow ↵Matthew Borkowski
other names
2021-10-26astgen: generate correct switch prong indicesRobin Voetter
Switch prong values are fetched by index in semantic analysis by prong offset, but these were computed as capture offset. This means that a switch where the first prong does not capture and the second does, the switch_capture zir instruction would be assigned switch_prong 0 instead of 1.