aboutsummaryrefslogtreecommitdiff
path: root/src/value.zig
AgeCommit message (Collapse)Author
2022-07-07stage2: make line field of `@src` runtime knownVeikka Tuominen
2022-06-30stage2: hash/eql of fixed-size floats use bit patternAndrew Kelley
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-18value: handle slices in canMutateComptimeVarStateVeikka Tuominen
2022-06-17Merge pull request #11881 from Vexu/stage2Andrew Kelley
Stage2: fixes for bugs found while looking for miscompilations
2022-06-17stage2: check that struct is a tuple when value tags differ in eqlVeikka Tuominen
2022-06-17stage2: comptime @bitCast packed struct bug fixOmar Alhammadi
2022-06-12Merge pull request #11837 from Vexu/stage2Andrew Kelley
Fix (nearly) all stage2 crashes when testing stdlib
2022-06-11stage2: small fixes + adjustments to std testsVeikka Tuominen
2022-06-11Sema: type safety for "runtime_index" fieldAndrew Kelley
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-06stage2: use correct type (u29) for alignmentVeikka Tuominen
2022-06-06Sema: validate equality on store to comptime fieldVeikka Tuominen
2022-06-03Value: implement {read,write}Value for more typesVeikka Tuominen
2022-05-25stage2: packed struct fields do not have a byte offsetVeikka Tuominen
2022-05-24stage2: string literal interningAndrew Kelley
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-20Sema: introduce laziness to `@sizeOf`Andrew Kelley
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-17stage2: fix pointer arithmetic result typeAndrew Kelley
This makes it so the result of doing pointer arithmetic creates a new pointer type that has adjusted alignment.
2022-05-16stage2: perform comptime vectorization of `*_with_overflow` in `Value`William Sengir
2022-05-16stage2: clean up creation of boolean `Value`sWilliam Sengir
2022-05-16stage2: handle vectors in `Value.intFitsInType`William Sengir
2022-05-10Sema: comptime float negation supports negative zeroAndrew Kelley
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-03Sema: fix bigIntToFloatAndrew Kelley
The implementation had the `@mulAdd` parameters mixed up.
2022-04-28Merge pull request #11541 from Vexu/stage2-slice-field-ptrVeikka Tuominen
Stage2: fix slice field modification at comptime
2022-04-28Sema: fix slice field modification at comptimeVeikka Tuominen
2022-04-27enable newly passing behavior testsAndrew Kelley
closes #11030
2022-04-27add new builtin function `@tan`Andrew Kelley
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-27compiler-rt: math functions reorgAndrew Kelley
* 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-20stage2: use indexes for Decl objectsAndrew Kelley
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-18Sema: fix not reserving enough memory for comptime shlAndrew Kelley
2022-04-14Sema: fix generic instantiation false negativesAndrew Kelley
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-12Sema: fix comptime equality of extern unions with same tagAndrew Kelley
2022-03-28Value.hashPtr: handle lazy_align as an integerAndrew Kelley
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-27Value: hash lazy_alignVeikka Tuominen
2022-03-26stage2: resolve types more lazilyAndrew Kelley
This avoids unwanted "foo depends on itself" compilation errors.
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-23Sema: fix comptime elem_ptr compare fixed addressAndrew Kelley
2022-03-23Sema: Value.copy: we gotta copy the bytesAndrew Kelley
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-22Sema: introduce a mechanism in Value to resolve typesAndrew Kelley
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-22stage2: lazy `@alignOf`Andrew Kelley
Add a `target` parameter to every function that deals with Type and Value.
2022-03-21behavior tests: enable all vector tests for the stage2 LLVM backendWilliam Sengir
2022-03-21Sema: make most instructions vector-agnosticWilliam Sengir
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-21stage2: return `Value.zero` when truncating int to 0 bits at comptimeWilliam Sengir
2022-03-21stage2: add way to print values with typesVeikka Tuominen
2022-03-20stage2: remove Value.Tag.abi_align_defaultAndrew Kelley
and make Decl alignment & linksection, and struct & union field alignment be scalar values, not Value values. YAGNI
2022-03-19Sema: implement zirSwitchCaptureElse for error setsVeikka Tuominen
2022-03-17stage2: implement `@reduce`Andrew Kelley
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-16stage2: bit_not on u0 is always 0Mitchell Hashimoto
2022-03-16gdb: add printer for selfhosted ValueRobin Voetter
2022-03-14stage2: fixups for topolarity-comptime-memory-reinterp branchAndrew Kelley
* 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-14stage2 sema: Respect container_ty of parent ptrsCody Tapscott
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-14stage2: Add container_ty/elem_ty to elem_ptr, field_ptr, *_payload_ptr ValuesCody Tapscott