15584 Commits

Author SHA1 Message Date
Andrew Kelley
dfb3231959 stage2: implement switching on unions
* AstGen: Move `refToIndex` and `indexToRef` to Zir
 * ZIR: the switch_block_*_* instruction tags are collapsed into one
   switch_block tag which uses 4 bits for flags, and reduces the
   scalar_cases_len field from 32 to 28 bits.
   This freed up more ZIR tags, 2 of which are now used for
   `switch_cond` and `switch_cond_ref` for producing the switch
   condition value. For example, for union values it returns the
   corresponding enum value.
 * switching with multiple cases and ranges is not yet supported because
   I want to change the ZIR encoding to store index pointers into the
   extra array rather than storing prong indexes. This will avoid O(N^2)
   iteration over prongs.
 * AstGen now adds a `switch_cond` on the operand and then passes the
   result of that to the `switch_block` instruction.
 * Sema: partially implement `switch_capture_*` instructions.
 * Sema: `unionToTag` notices if the enum type has only one possible value.
2021-10-19 20:22:47 -07:00
Andrew Kelley
4a76523b92
Merge pull request #9984 from Snektron/field-elem-access
Field elem access
2021-10-19 22:39:46 -04:00
Andrew Kelley
c1508c98f4 stage2 minor cleanups 2021-10-19 19:38:43 -07:00
Andrew Kelley
2192d404d5 stage2: wasm: implement struct_field_val 2021-10-19 19:20:31 -07:00
Andrew Kelley
e4c437f23b stage2: implement union member access as enum tag 2021-10-19 19:03:20 -07:00
Robin Voetter
6329f4e47a stage2: union field value 2021-10-20 03:44:02 +02:00
Robin Voetter
d6f048c456 stage2: make (typeHas)OnePossibleValue return the right value 2021-10-20 03:44:02 +02:00
Robin Voetter
7b97f6792f stage2: add Value.the_only_possible_value 2021-10-20 03:44:02 +02:00
Robin Voetter
b65582e834 stage2: remove AstGen none_or_ref
The remaining uses of this result location were causing a bunch of errors
problems where the pointers returned from rvalue and lvalue expressions
would be confused, allowing for extra pointers on rvalue expressions.
For example:
```zig
const X = struct {a: i32};
var x: X = .{.a = 1};
var ptr = &x;
_ = x.a;
```
In the last line, the lookup of x with result location .none_or_ref would
return a double pointer (**X). This would be dereferenced one, after which
a relative pointer to `a` would be fetched and derefenced to get the final
result.

However, this also allows us to manually construct a double pointer, and
fetch the field of the inner type of that:
```zig
_ = &(&(x)).a;
```

This problem also manifests itself with element access. There are two obvious
ways to fix the problem, both of which include replacing the usage of
.none_or_ref for field- and element accesses with something which
deterministically produce either a pointer or value: either result location
.ref or .none. In the former case, this would be paired with .elem_ptr, and
in the latter case with .elem_val.

Note that the stage 1 compiler does not have this problem, because there is
no equivalent of .elem_val and .field_val. In this way it is equivalent to
using the result location .ref for field- and element accesses.

In this case i have used .none, as this matches language behaviour more
closely.
2021-10-20 03:44:02 +02:00
Robin Voetter
bfedf40c92 stage2: zirIndexablePtrLen for non-pointer types 2021-10-20 03:44:02 +02:00
Robin Voetter
c507e0b763 stage2: Sema.fieldPtr for slice ptr and len 2021-10-20 03:44:02 +02:00
Robin Voetter
05c5c99a95 stage2: air ptr_slice_len_ptr and ptr_slice_ptr_ptr 2021-10-20 03:44:02 +02:00
Robin Voetter
e5d6fe18b9 stage2: restructure Sema.fieldVal and sema.fieldPtr
Dereferencing single pointers is now handled outside of the main switch,
which allows deduplication of some cases. This also implements the
relevant operations for pointers to types and pointers to slices.
2021-10-20 03:44:02 +02:00
Jakub Konka
372e9709ad macho: fix LLVM codepaths in self-hosted linker
* do not add linkage scope to aliased exported symbols - this is
  not respected on macOS
* special-case `MachO.openPath` in `link.File.openPath` as on macOS
  we always link with zld
* redirect to `MachO.flushObject` when linking relocatable objects
  in MachO linker whereas move the entire linking logic into
  `MachO.flushModule`
2021-10-19 20:39:42 +02:00
Matthew Borkowski
2d7b55aa0a translate_c: prevent a while under an if from stealing the else 2021-10-19 13:48:09 -04:00
Matthew Borkowski
135cb529de astgen.zig: fix emitting wrong error unwrapping instructions in tryExpr 2021-10-19 13:44:48 -04:00
Sizhe Zhao
ec3ed92f48 src/link/C/zig.h: Fix indent 2021-10-19 13:43:32 -04:00
Andrew Kelley
bea447a637 stage2: fix coercion of error set to error union
When returning an error set or an error union from a function which has
an inferred error set, it populates the error names in addition to the
set of functions. This can have false negatives, meaning that after
checking the map of an unresolved error set, one must do full error set
resolution before emitting a compile error.
2021-10-18 15:45:11 -07:00
Andrew Kelley
75c8c4442d coff linking: honor the link_libunwind flag 2021-10-18 10:49:39 -07:00
Matthew Borkowski
79a3dfcfd8 astgen.zig: fix false positive in breakExpr's checking for store_to_block_ptr 2021-10-18 13:18:47 -04:00
Andrew Kelley
cde3dd365e
Merge pull request #9966 from nektro/stage2-hasfield
stage2: implement `@hasField`
2021-10-18 00:57:10 -04:00
Andrew Kelley
7b00bef6bf Sema: resolveTypeFields before accessing type fields 2021-10-17 21:53:59 -07:00
Meghan Denny
0ef2e2520a stage2: implement @hasField
struct and union are kept in stage1 because struct/unionFieldCount are returning zero
2021-10-17 21:42:22 -07:00
Meghan Denny
7d0a74456b alphebetize behavior tests 2021-10-17 21:42:22 -07:00
Andrew Kelley
40cbf525f7 stage2: implement coercion from null to C pointer 2021-10-17 19:10:49 -07:00
Andrew Kelley
e5dac0a0b3 stage2: implement @embedFile 2021-10-17 18:59:11 -07:00
Andrew Kelley
ad17108bdd
Merge pull request #9960 from Snektron/bit-not
Some not and vector stuff
2021-10-17 21:59:10 -04:00
Andrew Kelley
e9d1e5e533 stage2: LLVM backend: lower constant field/elem ptrs 2021-10-17 17:02:20 -07:00
Andrew Kelley
07691db3ae stage2: fix handling of error unions as return type
* LLVM backend: fix phi instruction not respecting `isByRef`
   - Also fix `is_non_null` not respecting `isByRef`
 * Type: implement abiSize for error unions
2021-10-17 15:36:12 -07:00
Andrew Kelley
6534f2ef4f stage2: implement error wrapping
* Sema: fix returned operands not coercing to the function return type
   in some cases.
   - When returning an error or an error union from a function with an
     inferred error set, it will now populate the inferred error set.
   - Implement error set coercion for the common case of inferred error
     set to inferred error set, without forcing a full resolution.
 * LLVM backend: update instruction lowering that handles error unions
   to respect `isByRef`.
   - Also implement `wrap_err_union_err`.
2021-10-17 14:55:32 -07:00
Max Hollmann
53b87fa78a Move compareFn from init to type constructor in PriorityQueue and PriorityDequeue.
This change significantly improves performance for simple compare functions and modifies
the API to be more consistent with e.g. `HashMap`.
2021-10-17 17:47:43 -04:00
Robin Voetter
15a0b30d8e ci: disable macos stage 2 tests 2021-10-17 22:46:36 +02:00
Jonathan Marler
22beaf5afc actually fix child process deadlock on windows
Looks like I forgot to remove windows from this workaround condition when I finished implementing the child process output collection on windows.
2021-10-17 16:07:51 -04:00
LemonBoy
51be575745 std: Fix endless loop in fmt impl
Apparently there's a stage1 bug that may sometimes lead to an endless
loop being generated when unrolling the fmt impl.

Closes #9961
2021-10-17 16:06:40 -04:00
Robin Voetter
d004ef2e5d stage2: make zirBoolNot return undefined when argument is undefined 2021-10-17 20:33:04 +02:00
Robin Voetter
fd838584bf stage2: vector constants 2021-10-17 20:33:04 +02:00
Robin Voetter
d193ba9843 stage2: array->vector coercion 2021-10-17 20:33:04 +02:00
Robin Voetter
9336a87452 stage2: bitNot 2021-10-17 20:33:04 +02:00
Robin Voetter
6a3659c4e0 big.int: 2s-complement binary wrapping not 2021-10-17 20:33:04 +02:00
Stephen Gregoratto
98a37dfb23 Linux: Update syscall numbers for 5.14 2021-10-17 14:22:47 -04:00
Andrew Kelley
127cd06ba1 stage2: add haveFieldTypes() assertions
This will help with contributions such as #9966.
2021-10-17 10:59:40 -07:00
Andrew Kelley
fdd11f6cee Sema: coercion from error sets to anyerror 2021-10-16 12:41:03 -07:00
Andrew Kelley
4d6d6977b0 stage2: fixes to extern variables
* Relax compile error for "unable to export type foo" to allow
   integers, structs, arrays, and floats. This will need to be further
   improved to do the same checks as we do for C ABI struct field types.
 * LLVM backend: fix extern variables
 * LLVM backend: implement AIR instruction `wrap_err_union_payload`
2021-10-16 12:26:06 -07:00
Andrew Kelley
82ec56e47e
Merge pull request #9954 from Snektron/shifts
Big int saturating left shift
2021-10-16 15:06:13 -04:00
Meghan Denny
6f30c8c098 elevate more passing tests 2021-10-16 15:04:48 -04:00
Robin Voetter
1e09157b53 big ints: Fix set(signed int minimum) panic 2021-10-16 11:32:05 +02:00
Robin Voetter
f6bf24b2f3 stage2: comptime saturating shl 2021-10-16 11:32:05 +02:00
Robin Voetter
efa4f76c8b big ints: Saturating left shift + tests 2021-10-16 11:32:05 +02:00
Meghan
1df926b24e stage2: @hasDecl is passing 2021-10-16 04:09:16 -04:00
Andrew Kelley
682cdeceaa stage2: optional comparison and 0-bit payloads
* Sema: implement peer type resolution for optionals and null.
 * Rename `Module.optionalType` to `Type.optional`.
 * LLVM backend: re-use anonymous values. This is especially useful when
   isByRef()=true because it means re-using the same generated LLVM globals.
 * LLVM backend: rework the implementation of is_null and is_non_null
   AIR instructions. Generate slightly better LLVM code, and also fix
   the behavior for optionals whose payload type is 0-bit.
 * LLVM backend: improve `cmp` AIR instruction lowering to support
   pointer-like optionals.
 * `Value`: implement support for equality-checking optionals.
2021-10-15 18:37:09 -07:00