aboutsummaryrefslogtreecommitdiff
path: root/src/Module.zig
AgeCommit message (Collapse)Author
2023-10-25x86_64: pass more testsJacob Young
* 128-bit integer multiplication with overflow * more instruction encodings used by std inline asm * implement the `try_ptr` air instruction * follow correct stack frame abi * enable full panic handler * enable stack traces
2023-10-24InternPool: remove runtime_value representationmlugg
The main goal of this commit is to remove the `runtime_value` field from `InternPool.Key` (and its associated representation), but there are a few dominos. Specifically, this mostly eliminates the "maybe runtime" concept from value resolution in Sema: so some resolution functions like `resolveMaybeUndefValAllowVariablesMaybeRuntime` are gone. This required a small change to struct/union/array initializers, to no longer use `runtime_value` if a field was a `variable` - I'm not convinced this case was even reachable, as `variable` should only ever exist as the trivial value of a global runtime `var` decl. Now, the only case in which a `Sema.resolveMaybeUndefVal`-esque function can return the `variable` key is `resolveMaybeUndefValAllowVariables`, which is directly called from `Sema.resolveInstValueAllowVariables` (previously `Sema.resolveInstValue`), which is only used for resolving the value of a Decl from `Module.semaDecl`. While changing these functions, I also slightly reordered and restructured some of them, and updated their doc comments.
2023-10-23frontend: slightly simplify memory managementAndrew Kelley
no reason to pass `keep_resolved_path` to `newEmbedFile`
2023-10-23frontend: rework `@embedFile` for incremental compilationAndrew Kelley
This feature was made to work with the legacy incremental compilation mechanism which is being reworked. This commit regresses the ability to update files used with `@embedFile` while the compiler is running. In exchange, we get these benefits: * The embedded file contents are copied directly into InternPool rather than there being an extra allocation and memcpy. * The EmbedFile struct, which represents a long-lived object, is made more serialization friendly. * Eliminate the creation and use of a Decl as an anonymous decl. When implementing the new incremental compilation mechanism, functionality will need to be added back for handling `@embedFile`.
2023-10-22remove uses of non-configurable `err_int`Veikka Tuominen
2023-10-21make distinct error limit configurableVeikka Tuominen
Closes #786
2023-10-20Revert "make distinct error limit configurable"Andrew Kelley
This reverts commit 78855bd21866b515018259a2194e036e4b3120df. This commit did not replace uses of `Type.err_int` of which there are currently 60 uses. Re-opens #786
2023-10-16make distinct error limit configurableVeikka Tuominen
Closes #786
2023-10-08finish hooking up new dependency tree logicAndrew Kelley
* add Module instances for each package's build.zig and attach it to the dependencies.zig module with the hash digest hex string as the name. * fix incorrectly skipping the wrong packages for creating dependencies.zig * a couple more renaming of "package" to "module"
2023-10-08CLI: finish updating module API usageAndrew Kelley
Finish the work started in 4c4fb839972f66f55aa44fc0aca5f80b0608c731. Now the compiler compiles again. Wire up dependency tree fetching code in the CLI for `zig build`. Everything is hooked up except for `createDependenciesModule` is not yet implemented.
2023-10-08get `zig fetch` working with the new systemAndrew Kelley
* start renaming "package" to "module" (see #14307) - build system gains `main_mod_path` and `main_pkg_path` is still there but it is deprecated. * eliminate the object-oriented memory management style of what was previously `*Package`. Now it is `*Package.Module` and all pointers point to externally managed memory. * fixes to get the new Fetch.zig code working. The previous commit was work-in-progress. There are still two commented out code paths, the one that leads to `Compilation.create` and the one for `zig build` that fetches the entire dependency tree and creates the required modules for the build runner.
2023-10-03compiler: start handling anonymous decls differentlyAndrew Kelley
Instead of explicitly creating a `Module.Decl` object for each anonymous declaration, each `InternPool.Index` value is implicitly understood to be an anonymous declaration when encountered by backend codegen. The memory management strategy for these anonymous decls then becomes to garbage collect them along with standard InternPool garbage. In the interest of a smooth transition, this commit only implements this new scheme for string literals and leaves all the previous mechanisms in place.
2023-10-02sema: support reinterpreting extern/packed unions at comptime via field accesskcbanner
My previous change for reading / writing to unions at comptime did not handle union field read/writes correctly in all cases. Previously, if a field was written to a union, it would overwrite the entire value. This is problematic when a field of a larger size is subsequently read, because the value would not be long enough, causing a panic. Additionally, the writing behaviour itself was incorrect. Writing to a field of a packed or extern union should only overwrite the bits corresponding to that field, allowing for memory reintepretation via field writes / reads. I addressed these problems as follows: Add the concept of a "backing type" for extern / packed unions (`Type.unionBackingType`). For extern unions, this is a `u8` array, for packed unions it's an integer matching the `bitSize` of the union. Whenever union memory is read at comptime, it's read as this type. When union memory is written at comptime, the tag may still be known. If so, the memory is written using the tagged type. If the tag is unknown (because this union had previously been read from memory), it's simply written back out as the backing type. I added `write_packed` to the `reinterpret` field of `ComptimePtrMutationKit`. This causes writes of the operand to be packed - which is necessary when writing to a field of a packed union. Without this, writing a value to a `u1` field would overwrite the entire byte it occupied. The final case to address was reading a different (potentially larger) field from a union when it was written with a known tag. To handle this, a new kind of bitcast was introduced (`bitCastUnionFieldVal`) which supports reading a larger field by using a backing buffer that has the unwritten bits set to undefined. The reason to support this (vs always just writing the union as it's backing type), is that no reads to larger fields ever occur at comptime, it would be strictly worse to have spent time writing the full backing type.
2023-09-26Merge pull request #17215 from kcbanner/read_from_memory_unionVeikka Tuominen
sema: add support for unions in readFromMemory and writeToMemory
2023-09-25translate-c: convert clang errors messages into `std.zig.ErrorBundle`Techatrix
2023-09-24revert "compiler: packed structs cache bit offsets"Andrew Kelley
This is mostly a revert of a7088fd9a3edb037f0f51bb402a3c557334634f3. Measurement revealed the commit actually regressed performance.
2023-09-23compiler: packed structs cache bit offsetsAndrew Kelley
Instead of linear search every time a packed struct field's bit or byte offset is wanted, they are computed once during resolution of the packed struct's backing int type, and stored in InternPool for O(1) lookup. Closes #17178
2023-09-23sema: rework the comptime representation of comptime unionskcbanner
When the tag is not known, it's set to `.none`. In this case, the value is either an array of bytes (for extern unions) or an integer (for packed unions).
2023-09-23sema: add support for unions in readFromMemory and writeToMemorykcbanner
2023-09-21compiler: fix compilation for 32-bit targetsAndrew Kelley
2023-09-21compiler: make pointer type canonicalization always workAndrew Kelley
Previously it would canonicalize or not depending on some volatile internal state of the compiler, now it forces resolution of the element type to determine the alignment if it needs to.
2023-09-21sema: delete dead codeAndrew Kelley
Not sure what that code was supposed to be doing, it doesn't seem to be reachable.
2023-09-21compiler: move struct types into InternPool properAndrew Kelley
Structs were previously using `SegmentedList` to be given indexes, but were not actually backed by the InternPool arrays. After this, the only remaining uses of `SegmentedList` in the compiler are `Module.Decl` and `Module.Namespace`. Once those last two are migrated to become backed by InternPool arrays as well, we can introduce state serialization via writing these arrays to disk all at once. Unfortunately there are a lot of source code locations that touch the struct type API, so this commit is still work-in-progress. Once I get it compiling and passing the test suite, I can provide some interesting data points such as how it affected the InternPool memory size and performance comparison against master branch. I also couldn't resist migrating over a bunch of alignment API over to use the log2 Alignment type rather than a mismash of u32 and u64 byte units with 0 meaning something implicitly different and special at every location. Turns out you can do all the math you need directly on the log2 representation of alignments.
2023-09-15compiler: rework capture scopes in-memory layoutAndrew Kelley
* Use 32-bit integers instead of pointers for compactness and serialization friendliness. * Use a separate hash map for runtime and comptime capture scopes, avoiding the 1-bit union tag. * Use a compact array representation instead of a tree of hash maps. * Eliminate the only instance of ref-counting in the compiler, instead relying on garbage collection (not implemented yet but is the plan for almost all long-lived objects related to incremental compilation). Because a code modification may need to access capture scope data, this makes capture scope data long-lived state. My goal is to get incremental compilation state serialization down to a single pwritev syscall, by unifying the on-disk representation with the in-memory representation. This commit eliminates the last remaining pointer field of `Module.Decl`.
2023-08-30Sema: cleanup `coerceExtra`Jacob Young
* remove unreachable code * remove already handled cases * avoid `InternPool.getCoerced` * add some undef checks * error when converting undef int to float Closes #16987
2023-08-28Sema: revert reference trace changes that are no longer neededJacob Young
2023-08-28Sema: factor out `NeededComptimeReason` from comptime value resolutionJacob Young
This makes the call sites easier to read, reduces the number of `catch` expressions required, and prepares for comptime reasons to appear earlier in the list of notes.
2023-08-28Sema: implement reference trace with called from here notesJacob Young
Closes #15124
2023-08-22compiler: move unions into InternPoolAndrew Kelley
There are a couple concepts here worth understanding: Key.UnionType - This type is available *before* resolving the union's fields. The enum tag type, number of fields, and field names, field types, and field alignments are not available with this. InternPool.UnionType - This one can be obtained from the above type with `InternPool.loadUnionType` which asserts that the union's enum tag type has been resolved. This one has all the information available. Additionally: * ZIR: Turn an unused bit into `any_aligned_fields` flag to help semantic analysis know whether a union has explicit alignment on any fields (usually not). * Sema: delete `resolveTypeRequiresComptime` which had the same type signature and near-duplicate logic to `typeRequiresComptime`. - Make opaque types not report comptime-only (this was inconsistent between the two implementations of this function). * Implement accepted proposal #12556 which is a breaking change.
2023-08-17InternPool: safer enum APIAndrew Kelley
The key changes in this commit are: ```diff - names: []const NullTerminatedString, + names: NullTerminatedString.Slice, - values: []const Index, + values: Index.Slice, ``` Which eliminates the slices from `InternPool.Key.EnumType` and replaces them with structs that contain `start` and `len` indexes. This makes the lifetime of `EnumType` change from expiring with updates to InternPool, to expiring when the InternPool is garbage-collected, which is currently never. This is gearing up for a larger change I started working on locally which moves union types into InternPool. As a bonus, I fixed some unnecessary instances of `@as`.
2023-08-15InternPool: preserve indices of builtin types when resolvedmlugg
Some builtin types have a special InternPool index (e.g. `.type_info_type`) so that AstGen can refer to them before semantic analysis. Unfortunately, this previously led to a second index existing to refer to the type once it was resolved, complicating Sema by having the concept of an "unresolved" type index. This change makes Sema modify these InternPool indices in-place to contain the expanded representation when resolved. The analysis of the corresponding decls is caught in `Module.semaDecl`, and a field is set on Sema telling it which index to place struct/union/enum types at. This system could break if `std.builtin` contained complex decls which evaluate multiple struct types, but this will be caught by the assertions in `InternPool.resolveBuiltinType`. The AstGen result types which were disabled in 6917a8c have been re-enabled. Resolves: #16603
2023-08-11Sema: remove redundant addConstant functionsmlugg
After ff37ccd, interned values are trivial to convert to Air refs, using `Air.internedToRef`. This made functions like `Sema.addConstant` effectively redundant. This commit removes `Sema.addConstant` and `Sema.addType`, replacing them with direct usages of `Air.internedToRef`. Additionally, a new helper `Module.undefValue` is added, and the following functions are moved into Module: * `Sema.addConstUndef` -> `Module.undefRef` * `Sema.addUnsignedInt` -> `Module.intRef` (now also works for signed types) The general pattern here is that any `Module.xyzValue` helper may also have a corresponding `Module.xyzRef` helper, which just wraps the call in `Air.internedToRef`.
2023-08-10Sema: refactor generic calls to interleave argument analysis and parameter ↵mlugg
type resolution AstGen provides all function call arguments with a result location, referenced through the call instruction index. The idea is that this should be the parameter type, but for `anytype` parameters, we use generic poison, which is required to be handled correctly. Previously, generic instantiations and inline calls worked by evaluating all args in advance, before resolving generic parameter types. This means any generic parameter (not just `anytype` ones) had generic poison result types. This caused missing result locations in some cases. Additionally, the generic instantiation logic caused `zirParam` to analyze the argument types a second time before coercion. This meant that for nominal types (struct/enum/etc), a *new* type was created, distinct to the result type which was previously forwarded to the argument expression. This commit fixes both of these issues. Generic parameter type resolution is now interleaved with argument analysis, so that we don't have unnecessary generic poison types, and generic instantiation logic now handles parameters itself rather than falling through to the standard zirParam logic, so avoids duplicating the types. Resolves: #16566 Resolves: #16258 Resolves: #16753
2023-08-09change uses of std.builtin.Mode to OptimizeMode (#16745)Zachary Raineri
std.builtin.Mode is deprecated.
2023-08-09Module: implement `span` for `.call_arg` of a `@call`Jacob Young
Closes #16750
2023-07-31Sema: restrict what can appear in a naked functionJacob Young
* Disable runtime calls, since it is not possible to know the proper stack adjustment to follow the callee abi. * Disable runtime returns, since it is not possible to know where the return address is stored in general. * Allow implicit returns regardless of the return type, which allows naked functions with a non-void return type to be written.
2023-07-19llvm: finish converting `lowerValue`Jacob Young
2023-07-18Sema: fix fn_proto_param LazySrcLoc resolutionAndrew Kelley
to match source code span from merge-base.
2023-07-18Sema: fix source location crash for function prototypesAndrew Kelley
2023-07-18Sema: fix crash: array_in_c_exported_functionAndrew Kelley
Fuck it, we're storing decl indexes in LazySrcLoc now.
2023-07-18compiler: fix branch regressionsAndrew Kelley
* getOwnedFunctionIndex no longer checks if the value is actually a function. * The callsites to `intern` that I added want to avoid the `getCoerced` call, so I added `intern2`. * Adding to inferred error sets should not happen if the destination error set is not the inferred error set of the current Sema instance. * adhoc_inferred_error_set_type can be seen by the backend. Treat it like anyerror.
2023-07-18Sema: resolve inferred error set with function state in_progressAndrew Kelley
This way dependency loops are reported instead of the compiler crashing.
2023-07-18InternPool: implement indexToKey for func_instance and func_declAndrew Kelley
Also delete incorrect frees an arena-allocated parameters.
2023-07-18compiler: rework inferred error setsAndrew Kelley
* move inferred error sets into InternPool. - they are now represented by pointing directly at the corresponding function body value. * inferred error set working memory is now in Sema and expires after the Sema for the function corresponding to the inferred error set is finished having its body analyzed. * error sets use a InternPool.Index.Slice rather than an actual slice to avoid lifetime issues.
2023-07-18compiler: begin untangling anonymous decls from source declsAndrew Kelley
The idea here is to move towards a future where anonymous decls are represented entirely by an `InternPool.Index`. This was needed to start implementing `InternPool.getFuncDecl` which requires moving creation and deletion of Decl objects into InternPool. * remove `Namespace.anon_decls` * remove the concept of cleaning up resources from anonymous decls, relying on InternPool instead. * move namespace and decl object allocation into InternPool
2023-07-18rework generic function callsAndrew Kelley
Abridged summary: * Move `Module.Fn` into `InternPool`. * Delete a lot of confusing and problematic `Sema` logic related to generic function calls. This commit removes `Module.Fn` and replaces it with two new `InternPool.Tag` values: * `func_decl` - corresponding to a function declared in the source code. This one contains line/column numbers, zir_body_inst, etc. * `func_instance` - one for each monomorphization of a generic function. Contains a reference to the `func_decl` from whence the instantiation came, along with the `comptime` parameter values (or types in the case of `anytype`) Since `InternPool` provides deduplication on these values, these fields are now deleted from `Module`: * `monomorphed_func_keys` * `monomorphed_funcs` * `align_stack_fns` Instead of these, Sema logic for generic function instantiation now unconditionally evaluates the function prototype expression for every generic callsite. This is technically required in order for type coercions to work. The previous code had some dubious, probably wrong hacks to make things work, such as `hashUncoerced`. I'm not 100% sure how we were able to eliminate that function and still pass all the behavior tests, but I'm pretty sure things were still broken without doing type coercion for every generic function call argument. After the function prototype is evaluated, it produces a deduplicated `func_instance` `InternPool.Index` which can then be used for the generic function call. Some other nice things made by this simplification are the removal of `comptime_args_fn_inst` and `preallocated_new_func` from `Sema`, and the messy logic associated with them. I have not yet been able to measure the perf of this against master branch. On one hand, it reduces memory usage and pointer chasing of the most heavily used `InternPool` Tag - function bodies - but on the other hand, it does evaluate function prototype expressions more than before. We will soon find out.
2023-07-18cbe: fix pointers to aliases of extern valuesJacob Young
2023-07-03std.math.big.int: better name for equal functionr00ster91
All of the std except these few functions call it "eql" instead of "eq". This has previously tripped me up when I expected the equality check function to be called "eql" (just like all the rest of the std) instead of "eq". The motivation is consistency. If search "eq" on Autodoc, these functions stick out and it looks inconsistent. I just noticed there are also a few functions spelling it out as "equal" (such as std.mem.allEqual). Maybe those functions should also spell it "eql" but that can be done in a future PR.
2023-06-30Remerge pull request #15995 from mlugg/fix/union-field-ptr-alignJacob Young
Sema: copy pointer alignment to union field pointers This is an unrevert of 43c98dc11567eeb38be041c7dad179c53156f3df.
2023-06-30Sema: copy pointer alignment to struct field pointersmlugg