235 Commits

Author SHA1 Message Date
Veikka Tuominen
89cef9f5f7 stage2: make line field of @src runtime known 2022-07-07 10:50:06 +03:00
Andrew Kelley
c8531faaf5 stage2: hash/eql of fixed-size floats use bit pattern
Zig guarantees the memory layout of f16, f32, f64, f80, and f128 which
means for generic function purposes, values of these types need to be
compared on the basis of their bits in memory. This means nan-packing
can be used with generic functions, for example.

For comptime_float, the sign is observable, whether it is nan is
observable, but not any more kinds of bit patterns are observable.

This fixes the std.fmt tests that check printing "-nan".
2022-06-30 00:54:03 -07:00
Veikka Tuominen
8f9b31af92 value: handle slices in canMutateComptimeVarState 2022-06-18 20:04:49 +03:00
Andrew Kelley
33cf6ef621
Merge pull request #11881 from Vexu/stage2
Stage2: fixes for bugs found while looking for miscompilations
2022-06-17 19:23:08 -04:00
Veikka Tuominen
28986a0590 stage2: check that struct is a tuple when value tags differ in eql 2022-06-17 21:04:21 +03:00
Omar Alhammadi
69e2cac0d3
stage2: comptime @bitCast packed struct bug fix 2022-06-17 19:04:51 +03:00
Andrew Kelley
ffa700ee58
Merge pull request #11837 from Vexu/stage2
Fix (nearly) all stage2 crashes when testing stdlib
2022-06-12 17:45:57 -04:00
Veikka Tuominen
488e1e5f51 stage2: small fixes + adjustments to std tests 2022-06-11 23:49:33 +03:00
Andrew Kelley
9360cfebc7 Sema: type safety for "runtime_index" field
This commit does not change any behavior, but changes the type of
the runtime_index field from u32 to a non-exhaustive enum. This allows
us to put `std.math.maxInt(u32)` only in the enum type definition and
give it an official meaning.
2022-06-11 12:18:04 -07:00
Veikka Tuominen
14685e59b2 stage2: use correct type (u29) for alignment 2022-06-06 13:11:54 -07:00
Veikka Tuominen
cb5d2b691a Sema: validate equality on store to comptime field 2022-06-06 13:11:50 -07:00
Veikka Tuominen
5c65b086d6 Value: implement {read,write}Value for more types 2022-06-03 14:29:24 +03:00
Veikka Tuominen
d214b6bdf0 stage2: packed struct fields do not have a byte offset 2022-05-25 19:57:38 +03:00
Andrew Kelley
818fbd9c56 stage2: string literal interning
This is a temporary addition to stage2 in order to match stage1 behavior,
however the end-game once the lang spec is settled will be to use a global
InternPool for comptime memoized objects, making this behavior consistent
across all types, not only string literals. Or, we might decide to not
guarantee string literals to have equal comptime pointers, in which case
this commit can be reverted.
2022-05-24 01:01:24 -07:00
Andrew Kelley
fcd4efd8ec Sema: introduce laziness to @sizeOf
Motivation: the behavior test that is now passing.

The main change in this commit is introducing `Type.abiSizeAdvanced`,
`Value.Tag.lazy_size`, and adjusting `Sema.zirSizeOf` to take advantage
of these.

However, the bulk of lines changed in this commit ended up being moving
logic from value.zig and type.zig into Sema.zig. This logic had no
business being in Type/Value as it was only called from a Sema context,
and we need access to the Sema context for error reporting when a lazy
Value is resolved.

Also worth mentioning is that I bumped up the comptime `@floatToInt`
implementation from using f64 to f128.
2022-05-20 02:47:20 -07:00
Andrew Kelley
b6798c26ef stage2: fix pointer arithmetic result type
This makes it so the result of doing pointer arithmetic creates a new
pointer type that has adjusted alignment.
2022-05-17 23:50:38 -07:00
William Sengir
86a928ce61 stage2: perform comptime vectorization of *_with_overflow in Value 2022-05-16 13:55:26 -07:00
William Sengir
e8117bab6f stage2: clean up creation of boolean Values 2022-05-16 13:55:26 -07:00
William Sengir
6b5c87957b stage2: handle vectors in Value.intFitsInType 2022-05-16 13:55:26 -07:00
Andrew Kelley
b33c8b0b06 Sema: comptime float negation supports negative zero
When handling the `negate` ZIR instruction, Zig now checks for a
comptime operand and handles it as a special case rather than lowering
it as `0 - x` so that the expression `-x` where `x` is a floating point
value known at compile-time, will get the negative zero bitwise
representation.
2022-05-10 21:50:55 -07:00
Andrew Kelley
3ed9ef3e6b Sema: fix bigIntToFloat
The implementation had the `@mulAdd` parameters mixed up.
2022-05-03 21:50:00 -07:00
Veikka Tuominen
091fe78337
Merge pull request #11541 from Vexu/stage2-slice-field-ptr
Stage2: fix slice field modification at comptime
2022-04-28 22:27:50 +03:00
Veikka Tuominen
095d51164f Sema: fix slice field modification at comptime 2022-04-28 17:45:33 +03:00
Andrew Kelley
758ec9bdd4 enable newly passing behavior tests
closes #11030
2022-04-27 17:24:36 -07:00
Andrew Kelley
09f1d62bdf add new builtin function @tan
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-27 16:45:23 -07:00
Andrew Kelley
41dd2beaac compiler-rt: math functions reorg
* unify the logic for exporting math functions from compiler-rt,
   with the appropriate suffixes and prefixes.
   - add all missing f128 and f80 exports. Functions with missing
     implementations call other functions and have TODO comments.
   - also add f16 functions
 * move math functions from freestanding libc to compiler-rt (#7265)
 * enable all the f128 and f80 code in the stage2 compiler and behavior
   tests (#11161).
 * update std lib to use builtins rather than `std.math`.
2022-04-27 12:20:44 -07:00
Andrew Kelley
f7596ae942 stage2: use indexes for Decl objects
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-20 17:37:35 -07:00
Andrew Kelley
edb4a07d4d Sema: fix not reserving enough memory for comptime shl 2022-04-18 16:41:54 -07:00
Andrew Kelley
2a00df9c09 Sema: fix generic instantiation false negatives
The problem was that types of non-anytype parameters were being included
as part of the check to see if generic function instantiations were
equal. Now, Module.Fn additionally stores the information for whether each
parameter is anytype or not. `generic_poison` cannot be used to signal
this because the type is still needed for comptime arguments; in such
case the type will not be present in the newly generated function
prototype.

This presented one additional challenge: we need to compare equality of
two values where one of them is post-coercion and the other is not. So
we make some minor adjustments to `Type.eql` to support this. I think
this small complexity tradeoff is worth it because it means the compiler
does much less work on the hot path that a generic function is called
and there is already an existing matching instantiation.

closes #11146
2022-04-14 06:08:28 -07:00
Andrew Kelley
99657dca1f Sema: fix comptime equality of extern unions with same tag 2022-04-12 11:36:26 -07:00
Andrew Kelley
7e47f106cc Value.hashPtr: handle lazy_align as an integer
Although lazy_align is a different tag than integer values, it needs to
be hashed and equality-tested as if it were a simple integer. Otherwise
our basic data structure guarantees fall apart.
2022-03-28 12:05:08 -07:00
Veikka Tuominen
7217148edf Value: hash lazy_align 2022-03-27 22:38:37 +03:00
Andrew Kelley
af844931b2 stage2: resolve types more lazily
This avoids unwanted "foo depends on itself" compilation errors.
2022-03-26 00:33:14 -07:00
Andrew Kelley
bae35bdf2d stage2: result location types for function call arguments
* 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-26 00:27:12 -04:00
Andrew Kelley
aca42c6259 Sema: fix comptime elem_ptr compare fixed address 2022-03-23 19:58:13 -07:00
Andrew Kelley
74ccd0c40b Sema: Value.copy: we gotta copy the bytes
For Value.Tag.bytes, the value copy implementation did not copy the
bytes array. No good. This operation must do a deep copy. If we want
some other mechanism for not copying very large byte buffers then it has
to work differently than this one.
2022-03-23 19:20:38 -07:00
Andrew Kelley
60d8c4739d Sema: introduce a mechanism in Value to resolve types
This commit adds a new optional argument to several Value methods which
provides the ability to resolve types if it comes to it. This prevents
having duplicated logic inside both Sema and Value.

With this commit, the "struct contains slice of itself" test is passing
by exploiting the new lazy_align Value Tag.
2022-03-22 15:45:59 -07:00
Andrew Kelley
593130ce0a stage2: lazy @alignOf
Add a `target` parameter to every function that deals with Type and
Value.
2022-03-22 15:45:58 -07:00
William Sengir
c9598c4cd3 behavior tests: enable all vector tests for the stage2 LLVM backend 2022-03-21 16:54:19 -07:00
William Sengir
afdcfb005e Sema: make most instructions vector-agnostic
Made most `Value` functions require a `Type`. If the provided type is a
vector, then automatically vectorize the operation and return with
another vector. The Sema side can then automatically become vectorized
with minimal changes. There are already a few manually vectorized
instructions, but we can simplify those later.
2022-03-21 16:54:19 -07:00
William Sengir
3f4676901a stage2: return Value.zero when truncating int to 0 bits at comptime 2022-03-21 16:54:19 -07:00
Veikka Tuominen
a31fe0ff12 stage2: add way to print values with types 2022-03-21 15:03:42 +02:00
Andrew Kelley
0576086395 stage2: remove Value.Tag.abi_align_default
and make Decl alignment & linksection, and struct & union field alignment
be scalar values, not Value values.

YAGNI
2022-03-20 00:36:44 -07:00
Veikka Tuominen
12f3c461a4 Sema: implement zirSwitchCaptureElse for error sets 2022-03-19 15:49:27 +02:00
Andrew Kelley
7233a3324a stage2: implement @reduce
Notably, Value.eql and Value.hash are improved to treat NaN as equal to
itself, so that Type/Value can be hash map keys. Likewise float hashing
normalizes the float value before computing the hash.
2022-03-17 17:24:35 -07:00
Mitchell Hashimoto
79d3780fbd stage2: bit_not on u0 is always 0 2022-03-16 23:18:35 -07:00
Robin Voetter
0bd84e03b9 gdb: add printer for selfhosted Value 2022-03-16 17:57:31 +01:00
Andrew Kelley
2f92d1a026 stage2: fixups for topolarity-comptime-memory-reinterp branch
* don't store `has_well_defined_layout` in memory.
 * remove struct `hasWellDefinedLayout` logic. it's just
   `layout != .Auto`. This means we only need one implementation, in
   Type.
 * fix some of the cases being wrong in `hasWellDefinedLayout`, such as
   optional pointers.
 * move `tag_ty_inferred` field into a position that makes it more
   obvious how the struct layout will be done. Also we don't have a
   compiler that intelligently moves fields around so this layout is
   better.
 * Sema: don't `resolveTypeLayout` in `zirCoerceResultPtr` unless
   necessary.
 * Rename `ComptimePtrLoadKit` `target` field to `pointee` to avoid
   confusion with `target`.
2022-03-14 21:43:03 -07:00
Cody Tapscott
5fa057053c stage2 sema: Respect container_ty of parent ptrs
The core change here is that we no longer blindly trust that parent
pointers (.elem_ptr, .field_ptr, .eu_payload_ptr, .union_payload_ptr)
were derived from the "true" type of the underlying decl. When types
diverge, direct dereference fails and we are forced to bitcast, as
usual.

In order to maximize our chances to have a successful bitcast, this
includes several changes to the dereference procedure:
   - `root` is now `parent` and is the largest Value containing the
     dereference target, with the condition that its layout and the
     byte offset of the target within are both well-defined.
   - If the target cannot be dereferenced directly, because the
     pointers were not derived from the true type of the underlying
     decl, then it is returned as null.
   - `beginComptimePtrDeref` now accepts an optional array_ty param,
     which is used to directly dereference an array from an elem_ptr,
     if necessary. This allows us to dereference array types without
     well-defined layouts (e.g. `[N]?u8`) at an offset

The load_ty also allows us to correctly "over-read" an .elem_ptr to an
array of [N]T, if necessary. This makes direct dereference work for
array types even in the presence of an offset, which is necessary if
the array has no well-defined layout (e.g. loading from `[6]?u8`)
2022-03-14 21:42:43 -07:00
Cody Tapscott
bbd750ff05 stage2: Add container_ty/elem_ty to elem_ptr, field_ptr, *_payload_ptr Values 2022-03-14 21:42:42 -07:00