Commit Graph

3927 Commits

Author SHA1 Message Date
Andrew Kelley
ed2364a148 stage2: introduce anonymous struct literals 2022-03-01 23:46:57 -07:00
Andrew Kelley
6f303c01f3 LLVM: add extra padding to structs and tuples sometimes
* Sema: resolve type fully when emitting an alloc AIR instruction to
   avoid tripping assertion for checking struct field alignment.
 * LLVM backend: keep a reference to the LLVM target data alive during
   lowering so that we can ask LLVM what it thinks the ABI alignment
   and size of LLVM types are. We need this in order to lower tuples and
   structs so that we can put in extra padding bytes when Zig disagrees
   with LLVM about the size or alignment of something.
 * LLVM backend: make the LLVM struct type packed that contains the most
   aligned union field and the padding. This prevents the struct from
   being too big according to LLVM. In the future, we may want to
   consider instead emitting unions in a "flat" manner; putting the tag,
   most aligned union field, and padding all in the same struct field
   space.
 * LLVM backend: make structs with 2 or fewer fields return isByRef=false.
   This results in more efficient codegen. This required lowering of
   bitcast to sometimes store the struct into an alloca, ptrcast, and
   then load because LLVM does not allow bitcasting structs.
 * enable more passing behavior tests.
2022-03-01 18:24:00 -07:00
Andrew Kelley
8878f085dc Sema: correct implementation of comptimeOnly for tuples
This makes formatted printing work when mixing comptime and runtime
fields.
2022-03-01 15:26:31 -07:00
Andrew Kelley
f6aaab9406 LLVM: fix tripping assertions
Packed structs were tripping an LLVM assertion due to calling
`LLVMConstZExt` from i16 to i16. Solved by using instead
`LLVMConstZExtOrBitCast`.

Unions were tripping an LLVM assertion due to a typo using the union
llvm type to construct an integer value rather than the tag type.
2022-03-01 14:58:37 -07:00
Andrew Kelley
1b194931b0 LLVM: fix when sret and isByRef ret_ty disagree
This can happen functions use the C ABI.
2022-03-01 14:58:37 -07:00
Jakub Konka
52205a3c16 x64: fix array to slice impl 2022-03-01 17:59:39 +01:00
Jakub Konka
caa4e30ef4 x64: impl airMemset using inline memset 2022-03-01 15:21:10 +01:00
joachimschmidt557
ca97caab8a stage2 ARM: implement return types with abi size > 4 2022-03-01 09:14:52 +01:00
Jakub Konka
ec54ceee6d
Merge pull request #11016 from ziglang/x64-more-codegen 2022-03-01 09:14:23 +01:00
Andrew Kelley
4763fd1a41 Sema: clean up peer resolution of errors
* Fix compile error for `zirErrorUnionType`.
 * Convert zirMergeErrorSets logic to call `Type.errorSetMerge`.
   It does not need to create a Decl as the TODO comment hinted.
 * Extract out a function called `resolveInferredErrorSetTy`.
 * Rework `resolvePeerTypes` with respect to error unions and
   error sets. This is a less complex implementation that passes all the
   same tests and uses many fewer lines of code by taking advantage of
   the function `coerceInMemoryAllowedErrorSets`.
   - Always merge error sets in the order that makes sense, even when
     that means `@typeInfo` incompatibility with stage1.
 * `Type.errorSetMerge` no longer overallocates.
 * Don't skip passing tests.
2022-02-28 23:15:58 -07:00
Mitchell Hashimoto
ea00e50045 stage2: skip error union test on native 2022-02-28 20:42:13 -07:00
Mitchell Hashimoto
0eb3cb9e8c stage2: order of error set merging should match stage1 2022-02-28 20:42:13 -07:00
Mitchell Hashimoto
6dae1de017 skip tests on native backends 2022-02-28 20:42:13 -07:00
Mitchell Hashimoto
b4ce855788 stage2: error union and non-error set/union peer cast resolution 2022-02-28 20:42:13 -07:00
Mitchell Hashimoto
38aae2cb7c stage2: peer resolve error sets and unions, add more tests 2022-02-28 20:42:13 -07:00
Mitchell Hashimoto
bfada7c514 stage2: implement peer type resolution between error unions 2022-02-28 20:42:13 -07:00
Andrew Kelley
157f66ec07 Sema: fix pointer type hash and equality functions
Several issues with pointer types are fixed:

Prior to this commit, Zig would not canonicalize a pointer type with
an explicit alignment to alignment=0 if it matched the pointee ABI
alignment. In order to fix this, `Type.ptr` now takes a Target
parameter. I also moved the host_size canonicalization to `Type.ptr`
since target is now available. Similarly, is_allowzero in the case of
C pointers is now treated as a canonicalization done by the function
rather than a precondition.

in-memory coercion for pointers now properly checks ABI alignment
of pointee types instead of incorrectly treating the 0 value as an
alignment.

Type equality is completely reworked based on the tag() rather than the
zigTypeTag(). It's still semantically based on zigTypeTag() but that
knowledge is implied rather than dictating the control flow of the
logic. Importantly, this fixes cases for opaques, structs, tuples,
enums, and unions, where type equality was incorrectly returning based
on whether the tag() values were equal.

Additionally, pointer type equality now takes into account alignment.
Because we canonicalize non-zero alignment which equals pointee type ABI
alignment to alignment=0, this now can be a simple integer comparison.

Type hashing is implemented for pointers and floats. Array types now
additionally hash their sentinels.

This regressed some behavior tests that were passing but only because
of bugs regarding type equality.

The C backend has a noticeable problem with lowering differently-aligned
pointers (particularly slices) as the same type, causing C compilation
errors due to duplicate declarations.
2022-02-28 19:22:16 -07:00
Jakub Konka
cfbc3537ef x64: pass more behavior tests 2022-02-28 23:20:05 +01:00
Jakub Konka
66d28d0f20 x64: implement get_union_tag for register 2022-02-28 23:20:05 +01:00
Jakub Konka
decc90e0e7 x64: clean up loadMemPtrIntoRegister abstraction 2022-02-28 23:20:05 +01:00
Jakub Konka
12cdb36c5b codegen: fix padding calculation for error unions when lowering
* do not track `rdi` register before `call` inst, but instead freeze
  it from further use, until `call` has been realised
* pass more error union tests
2022-02-28 23:20:05 +01:00
Jakub Konka
05431d7c4a x64: impl unwrap_errunion_payload and unwrap_errunion_err for register 2022-02-28 23:20:05 +01:00
Mitchell Hashimoto
a7ca40b281 stage2: sentinel comp during peer type resolution should use elem type
We were using the array type, not the element type. Also, we should do
the sentinel comparison after we verify that the element types of both
are compatible.
2022-02-28 16:27:16 -05:00
Veikka Tuominen
dfeffcfbf8 stage2: tuple mul/cat 2022-02-28 13:09:14 -07:00
Jakub Konka
331cc810de
Merge pull request #11012 from ziglang/x64-union-tag
stage2,x64: basic (un)tagged unions
2022-02-28 17:42:59 +01:00
Jakub Konka
16f9774d2d x64: fix switch condition mir; pass more union tests 2022-02-28 15:25:40 +01:00
Jakub Konka
a61ac9ecbf x64: fix store with ABI size > 8 on stack; pass union tests 2022-02-28 12:14:41 +01:00
Jakub Konka
90059a12e0
Merge pull request #11008 from joachimschmidt557/stage2-arm
stage2 ARM: pass more behavior tests
2022-02-28 09:59:44 +01:00
Veikka Tuominen
87dc60e8de stage2: implement builtin_call 2022-02-27 18:59:44 -05:00
Cody Tapscott
71aa5084ed stage2: Resolve alignment for union field in @TypeInfo
This also includes two other small fixes:
 - Instantiate void TypeInfo fields as void
 - Return error in `type.comptimeOnly` on unresolved comptime requirements
2022-02-27 14:24:47 -07:00
joachimschmidt557
1bf8da19e1
stage2 ARM: implement slice and array_to_slice 2022-02-27 21:38:56 +01:00
joachimschmidt557
91fbcf7093
stage2 ARM: enable more behavior tests 2022-02-27 21:38:56 +01:00
Andrew Kelley
104a8840db
Merge pull request #11002 from topolarity/comptime-int-comparison
stage2: Add comptime result for certain unsigned/comptime comparisons
2022-02-27 15:32:46 -05:00
Cody Tapscott
0bdc3d8f4e stage2: Implement @Type for Array, Optional, Float, and ErrorUnion 2022-02-27 15:29:38 -05:00
Veikka Tuominen
9f59189c95 stage2: do not memoize calls that can mutate comptime state 2022-02-27 16:43:53 +02:00
Veikka Tuominen
7a92b89a9d stage2: forward discard result loc to more expressions 2022-02-27 13:32:55 +02:00
Cody Tapscott
a7a508fcd9 stage2 sema: Implement comptime result for comparison of uint to comptime value
This adds a comptime result when comparing a comptime value to an
unsigned integer. For example:
   ( 0 <= (unsigned runtime value)) => true
   (-1 <  (unsigned runtime value)) => true
   ((unsigned runtime value) < -15) => false
2022-02-27 02:24:28 -07:00
Andrew Kelley
2687b8f7f4 stage2: implement @unionInit
The ZIR instruction `union_init_ptr` is renamed to `union_init`.
I made it always use by-value semantics for now, not taking the time to
invest in result location semantics, in case we decide to change the
rules for unions. This way is much simpler.

There is a new AIR instruction: union_init. This is for a comptime known
tag, runtime-known field value.
vector_init is renamed to aggregate_init, which solves a TODO comment.
2022-02-26 20:59:36 -07:00
Andrew Kelley
32e89a98d8 Sema: implement union value equality at comptime
Still TODO is extern unions.
2022-02-26 20:59:23 -07:00
Mitchell Hashimoto
e999a925fa
stage2: @TypeInfo for error sets (#10998) 2022-02-26 22:59:06 -05:00
Andrew Kelley
aefe4046de Sema: implement @enumToInt for unions 2022-02-26 16:53:23 -07:00
Andrew Kelley
d62229e3ad Sema: Module.Union.abiAlignment can return 0
When the union is a 0-bit type.
2022-02-26 16:53:23 -07:00
Andrew Kelley
822d29286b Sema: make align(a) T same as align(a:0:N) T
where `@sizeOf(T) == N`.
2022-02-26 16:50:35 -07:00
Andrew Kelley
e81b21a0ea
Merge pull request #10992 from mitchellh/peer-slices
stage2: peer resolve *[N]T to []T and E![]T and [*]T, handle in-memory coercion
2022-02-26 18:49:44 -05:00
Veikka Tuominen
bf3c88b68d stage2: various fixes to get one test passing
* resolve error sets before merging them
* implement tupleFieldPtr
* make ret_ptr behave like alloc with zero sized types in llvm backend
2022-02-26 18:44:23 -05:00
Mitchell Hashimoto
156316bc7c
stage2: skip more tests for native backends 2022-02-26 13:56:50 -08:00
Joachim Schmidt
058e482247
Merge pull request #10996 from joachimschmidt557/stage2-arm
stage2 ARM: implement truncate to integers with <= 32 bits
2022-02-26 22:50:31 +01:00
Veikka Tuominen
bff7714a7c stage2: fix toAllocatedBytes on slices 2022-02-26 12:52:06 -07:00
Veikka Tuominen
ee149aaa03 stage2: actually coerce in coerce_result_ptr at comptime 2022-02-26 12:51:23 -07:00
Veikka Tuominen
315d4e8442 stage2: do not require function when evaluating typeOf
We only care about the instructions type; it will never actually be codegen'd.
2022-02-26 18:08:31 +02:00