433 Commits

Author SHA1 Message Date
mlugg
9eda6ccefc InternPool: use separate key for slices
This change eliminates some problematic recursive logic in InternPool,
and provides a safer API.
2024-02-02 11:02:03 +00:00
SuperAuguste
a479fd3132 Fix some comptime packed struct issues
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2024-01-29 13:09:17 -08:00
Veikka Tuominen
281b2695c4 Value: expand canMutateComptimeVarState
Closes #17761
2024-01-29 13:48:45 +02:00
Veikka Tuominen
2e7d28dd0d Sema: replace uses of toUnsignedInt with toUnsignedIntAdvanced
During semantic analysis the value may be an unresolved lazy value
which makes using `toUnsignedInt` invalid.

Add assertions to detect similar issues in the future.

Closes #18624
2024-01-20 12:21:05 -08:00
Carl Åstholm
c8fa767f08 Work around stage1 not yet returning null-terminated @typeInfo strings
These changes can be reverted the next time stage1 is updated.
2024-01-07 16:21:08 +01:00
Bogdan Romanyuk
7fbbeae617
value: handle lazy_align and lazy_size in writeToPackedMemory 2023-11-27 12:35:29 +00:00
Meghan Denny
2549de80b2 move Module.Decl.Index and Module.Namespace.Index to InternPool 2023-11-26 02:24:40 -05:00
Techatrix
18608223ef convert toType and toValue to Type.fromInterned and Value.fromInterned 2023-11-25 04:09:53 -05:00
Bogdan Romanyuk
583afd6f0c
value: update isDeclRef for anonymous declarations 2023-11-21 13:21:32 +02:00
mlugg
b355893438
compiler: correct unnecessary uses of 'var' 2023-11-19 11:11:49 +00:00
Andrew Kelley
3fc6fc6812 std.builtin.Endian: make the tags lower case
Let's take this breaking change opportunity to fix the style of this
enum.
2023-10-31 21:37:35 -04:00
mlugg
7882bdcb7e
Value: remove unused function 2023-10-24 14:28:33 +01:00
mlugg
20bb81166f
InternPool: remove runtime_value representation
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-24 14:28:33 +01:00
Andrew Kelley
94d61ce964
Merge pull request #17651 from Vexu/error-limit
Make distinct error limit configurable (attempt #2)
2023-10-23 03:19:03 -04:00
Andrew Kelley
7bab406c79 InternPool: store alignment of anon decls
Commit 5393e56500d499753dbc39704c0161b47d1e4d5c has a flaw pointed out
by @mlugg: the `ty` field of pointer values changes when comptime values
are pointer-casted. This commit introduces a new encoding which
additionally stores the "original pointer type" which is used to store
the alignment of the anonymous decl, and potentially other information
in the future such as section and pointer address space. However, this
new encoding is only used when the original pointer type differs from
the casted pointer type in a meaningful way.

I was able to make the LLVM backend and the C backend lower anonymous
decls with the appropriate alignment, however I will need some help
figuring out how to do this for the backends that lower anonymous decls
via src/codegen.zig and the wasm backend.
2023-10-21 21:38:41 -04:00
Veikka Tuominen
bf61c5c065 make distinct error limit configurable
Closes #786
2023-10-21 12:36:29 +03:00
Andrew Kelley
328ec15d9c Revert "make distinct error limit configurable"
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-20 14:03:55 -07:00
Veikka Tuominen
78855bd218 make distinct error limit configurable
Closes #786
2023-10-16 04:08:45 -04:00
Andrew Kelley
c0b5512544 compiler: start handling anonymous decls differently
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-03 12:12:50 -07:00
kcbanner
d657b6c0e2 sema: support reinterpreting extern/packed unions at comptime via field access
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-10-02 13:15:28 -04:00
Andrew Kelley
937138cb90
Merge pull request #17248 from antlilja/abs
Replace @fabs builtin with new @abs builtin
2023-09-27 17:25:19 -07:00
Veikka Tuominen
ab3ac1e670 Value: fix assertion failure when mutating extern union
Closes #17292
2023-09-27 11:47:24 -07:00
Andrew Kelley
1c02e58fc0 Revert "compiler: don't use @abs builtin yet"
This reverts commit 21780899eb17a0cb795ff40e5fae6556c38ea13e.

After this commit, a version of the compiler which supports the new
`@abs` builtin is required.
2023-09-27 11:23:28 -07:00
Andrew Kelley
21780899eb compiler: don't use @abs builtin yet
This commit can be used to rebuild zig1.wasm
2023-09-27 11:21:59 -07:00
antlilja
6a29646a55 Rename @fabs to @abs and accept integers
Replaces the @fabs builtin with a new @abs builtins which accepts
floats, signed integers and vectors of said types.
2023-09-27 11:15:53 -07:00
kcbanner
9f4649b197 codegen/sema: handle unions with unknown tags in more places 2023-09-23 14:34:01 -04:00
kcbanner
f2a24b48e1 sema: rework the comptime representation of comptime unions
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-23 13:05:04 -04:00
kcbanner
2fddd767ba sema: add support for unions in readFromMemory and writeToMemory 2023-09-23 13:04:56 -04:00
mlugg
1b672e41c5 InternPool,Sema,type,llvm: alignment fixes
This changeset fixes the handling of alignment in several places. The
new rules are:
* `@alignOf(T)` where `T` is a runtime zero-bit type is at least 1,
  maybe greater.
* Zero-bit fields in `extern` structs *do* force alignment, potentially
  offsetting following fields.
* Zero-bit fields *do* have addresses within structs which can be
  observed and are consistent with `@offsetOf`.

These are not necessarily all implemented correctly yet (see disabled
test), but this commit fixes all regressions compared to master, and
makes one new test pass.
2023-09-21 14:48:41 -07:00
mlugg
0182b7242e TypedValue: do not crash when failing to dereference pointer
All of the logic in `Value.elemValue` is quite questionable, but
printing an error is definitely better than crashing. Notably, this
should stop us from hitting crashes when dumping AIR.
2023-09-21 14:48:40 -07:00
Andrew Kelley
accd5701c2 compiler: move struct types into InternPool proper
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-21 14:48:40 -07:00
Andrew Kelley
cb6201715a InternPool: prevent anon struct UAF bugs with type safety
Instead of using actual slices for InternPool.Key.AnonStructType, this
commit changes to use Slice types instead, which store a
long-lived index rather than a pointer.

This is a follow-up to 7ef1eb1c27754cb0349fdc10db1f02ff2dddd99b.
2023-09-12 20:08:56 -04:00
Jacob Young
e2ff486de5 Sema: cleanup coerceExtra
* 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-30 16:50:30 -04:00
Andrew Kelley
ada0010471 compiler: move unions into InternPool
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-22 13:54:14 -07:00
Andrew Kelley
7ef1eb1c27 InternPool: safer enum API
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-17 18:16:03 -07:00
Jacob Young
49cc1bff08 llvm: finish converting intrinsics 2023-08-08 21:32:50 -04:00
Zachary Raineri
49244dc0ca
std: remove some unused imports (#16710) 2023-08-06 15:18:50 -04:00
Andrew Kelley
076b54c8e7 frontend: fix wrong result of Value.getFunction
Fixes regression introduced in
db33ee45b7261c9ec62a1087cfc9377bc4e7aa8f.

Found from compiling bun. Unfortunately we do not have a behavior test
reduction for this bug.
2023-07-22 19:57:23 -07:00
Andrew Kelley
3f2a4720b1 compiler: fix branch regressions
* 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-18 19:02:06 -07:00
Andrew Kelley
db33ee45b7 rework generic function calls
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-18 19:02:05 -07:00
Andrew Kelley
17890f6b8a
Merge pull request #15879 from r00ster91/more_stage1_test_coverage
More stage1 test coverage
2023-07-03 15:59:54 -07:00
r00ster91
2b8687ba2d std.math.big.int: better name for equal function
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-07-03 11:00:13 -07:00
r00ster91
11c32c756f fix u65529 and above overflowing in more places
See also #15537
2023-06-27 19:08:24 -04:00
Jacob Young
c036f83fa0 Value: fix incorrect types returned from readFromMemory 2023-06-26 01:30:00 -07:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
Jacob Young
6aa88ecc54 Type/Value: garbage collect some methods 2023-06-22 11:45:33 -07:00
Jacob Young
c60896743d Value: handle more legacy tags when writing extern struct to memory
Closes #16130
2023-06-22 08:07:02 -07:00
Jacob Young
d7bd4f339c Sema: optimize value resolution 2023-06-20 14:02:09 -04:00
Jacob Young
6b56f4ff0b Value: optimize isPtrToThreadLocal 2023-06-20 14:02:09 -04:00
Jacob Young
d69e324eae Value: optimize isRuntimeValue 2023-06-20 14:02:08 -04:00