7982 Commits

Author SHA1 Message Date
Jakub Konka
e4039cecc7 x64: fix (un)wrapping error unions + refactor 2022-03-06 19:02:02 +01:00
Luuk de Gram
23e2368ac3
stage2: Fix codegen for unions and error unions
When an union had a zero-sized payload type, we would lower the tag twice. This is fixed
by exiting early when `payload_size` is 0.

With regards to error unions, we were only accounting for padding for the payload field.
However, the errorset value can have a smaller alignment than the payload as well, i.e. error!usize.
We fix this by also accounting for padding/alignment of the error set tag of an error union.
2022-03-06 16:04:15 +01:00
Mitchell Hashimoto
bf972e44d5 stage2: coerce [*:0]u8 to other valid pointer types
This makes the following work properly (as it does in stage1, too):

  var zero_ptr: [*:0]const u8 = undefined;
  var no_zero_ptr: [*]const u8 = zero_ptr;

Prior to this this would fail with an "expected type" error since
coercion failed.
2022-03-06 10:38:53 +02:00
Jakub Konka
1252bdd4d6 elf: add debug info for non-ptr optionals 2022-03-05 20:18:18 +01:00
Jakub Konka
9a027d9ee3 macho: fix incorrect line and pc advancement 2022-03-05 17:55:01 +01:00
Jakub Konka
0a9088bd06 macho: remove anon_struct_type which is now redundant 2022-03-05 17:55:01 +01:00
Jakub Konka
2be003a3b3 macho: write NOPs as padding for machine code section 2022-03-05 17:55:01 +01:00
Jakub Konka
3318400ea3 macho: migrate to named struct for slices 2022-03-05 17:55:01 +01:00
Jakub Konka
eaf08bfb84 macho: handle optional non-ptr types in DWARF 2022-03-05 17:55:01 +01:00
joachimschmidt557
a06e9eca45
stage2 AArch64: add more slice support
* airSlice
* airArrayToSlice
* and initial support for airSlicePtr and co
2022-03-05 11:31:51 +01:00
joachimschmidt557
ed7e2938ff
stage2 AArch64: generate less no-op branches
mirroring d486a7b81188ea1e027334c81589157efde2ad23 for aarch64
2022-03-05 08:56:01 +01:00
joachimschmidt557
691ec964ef
stage2 AArch64: implement bitwise binary operations
for integers with bit sizes <= 64
2022-03-05 08:56:01 +01:00
joachimschmidt557
e3121accac
stage2 AArch64: introduce logical immediate instructions 2022-03-05 08:56:01 +01:00
Andrew Kelley
f2a5d0bf94 stage2: fix tuple assigned to variable
Before this we would see ZIR code like this:
```
%69 = alloc_inferred_mut()
%70 = array_base_ptr(%69)
%71 = elem_ptr_imm(%70, 0)
```
This would crash the compiler because it expects to see a
`coerce_result_ptr` instruction after `alloc_inferred_mut`, but that
does not happen in this case because there is no type to coerce the
result pointer to.

In this commit I modified AstGen so that it has similar codegen as when
using a const instead of a var:
```
%69 = alloc_inferred_mut()
%76 = array_init_anon(.{%71, %73, %75})
%77 = store_to_inferred_ptr(%69, %76)
```

This does not obey result locations, meaning if you call a function
inside the initializer, it will end up doing a copy into the LHS.
Solving this problem, or changing the language to make this legal,
will be left for my future self to deal with. Hi future self!
I see you reading this commit log. Hope you're doing OK buddy.

Sema for `store_ptr` of a tuple where the pointer is in fact the same
element type as the operand had an issue where the comptime fields would
get incorrectly lowered to runtime stores to bogus addresses. This is
solved with an exception to the optimization in Sema for storing
pointers that handles tuples element-wise. In the case that we are
storing a tuple to itself, it skips the optimization. This results in
better code and avoids the problem. However this caused a regression in
GeneralPurposeAllocator from the standard library.

I regressed the test runner code back to the simpler path. It's too
hard to debug standard library code in the LLVM backend right now since
we don't have debug info hooked up. Also, we didn't have any behavior
test coverage of whatever was regressed, so let's try to get that
coverage added as a stepping stone to getting the standard library
working.
2022-03-04 18:27:46 -07:00
Andrew Kelley
d3648cc030 zig cc: integrate with -fstack-check, -fno-stack-check 2022-03-04 18:10:37 -07:00
joachimschmidt557
d486a7b811 stage2 ARM: generate less no-op branches
The checks detecting such no-op branches (essentially instructions
that branch to the instruction immediately following the branch) were
tightened to catch more of these occurrences.
2022-03-04 23:28:14 +01:00
Andrew Kelley
63c5c510b1 Sema: rework peer type logic for pointers
Now it's centered around a switch on the chosen type tag which gives us
easy access to pointer data.

The logic is simplied and in some cases logic is removed when it is
sufficient to choose the type that is a better coercion target without
knowing whether such coercion will succeed ahead of time.

A bug is fixed at the bottom of the function; we were doing the opposite
of what we were supposed to with `seen_const`.

Also the bottom of the function has a more complete handling of the
possible combinations of `any_are_null`, `convert_to_slice`, and
`err_set_ty`.

In the behavior tests, not as many backends needed to be skipped.
2022-03-03 23:12:18 -07:00
Mitchell Hashimoto
26be5bb8b1 stage2: peer resolve *T to [*c]T 2022-03-03 21:33:18 -07:00
Andrew Kelley
e532b0c0b5 stage2: cleanups to wasm memory intrinsics
* AIR: use pl_op instead of ty_pl for wasm_memory_size. No need to
   store the type because the type is always `u32`.
 * AstGen: use coerced_ty for `@wasmMemorySize` and `@wasmMemoryGrow`
   and do the coercions in Sema.
 * Sema: use more accurate source locations for errors.
 * Provide more information in the compiler error message.
 * Codegen: use liveness data to avoid lowering unused
   `@wasmMemorySize`.
 * LLVM backend: add implementation
   - I wasn't able to test it because we are hitting a linker error for
     `-target wasm32-wasi -fLLVM`.
 * C backend: use `zig_unimplemented()` instead of silently doing wrong
   behavior for these builtins.
 * behavior tests: branch only on stage2_arch for inclusion of the
   wasm.zig file. We would change it to `builtin.cpu.arch` but that is
   causing a compiler crash on some backends.
2022-03-03 18:31:55 -07:00
Luuk de Gram
7fd32de018 cbe: Implement wasm builtins
This implements the wasm builtins by lowering to builtins that are supported by c-compilers.
In this case: Clang.

This also simplifies the `AIR` instruction as it now uses the payload field of `ty_pl` and `pl_op`
directly to store the index argument rather than storing it inside Extra. This saves us 4 bytes
per builtin call.
2022-03-03 16:33:46 -07:00
Luuk de Gram
21f0503c01 Update behavior tests 2022-03-03 16:33:46 -07:00
Luuk de Gram
43cb19ea4d wasm: Implement @wasmMemoryGrow builtin
Similarly to the other wasm builtin, this implements the grow variation where the memory
index is a comptime known value. The operand as well as the result are runtime values.
This also verifies during semantic analysis the target we're building for is wasm, or else
emits a compilation error. This means that other backends do not have to handle this AIR instruction,
other than the wasm and LLVM backends.
2022-03-03 16:33:46 -07:00
Luuk de Gram
ec4c30ae48 wasm: Implement @wasmMemorySize() builtin
This implements the `wasmMemorySize` builtin, in Sema and the Wasm backend.
The Stage2 implementation differs from stage1 in the way that `index` must be a comptime value.
The stage1 variant is incorrect, as the index is part of the instruction encoding, and therefore,
cannot be a runtime value.
2022-03-03 16:33:46 -07:00
Andrew Kelley
0ea51f7f49
Merge pull request #11049 from mitchellh/peer-arrays
stage2: fix a couple issues with peer resolution and const casting arrays
2022-03-03 17:56:37 -05:00
Veikka Tuominen
aa7cbca7d3 stage2: make analyzePtrArithmetic no-op with offset=0 2022-03-03 22:42:34 +02:00
Veikka Tuominen
69bd2c243e stage2: remove opaque type workaround from Type.ptrAlignment 2022-03-03 22:16:49 +02:00
Veikka Tuominen
7691013d10 stage2: add 'called from here' error note 2022-03-03 22:14:43 +02:00
Andrew Kelley
365aca0e90 Sema: allow pointers to opaques to be aligned 2022-03-03 13:11:06 -07:00
Cody Tapscott
85610a9aff stage2 llvm: Lower 0-bit field-ptr as ptr-to-void 2022-03-03 13:08:14 -07:00
Cody Tapscott
b6a6f05c0d stage2: support @ptrCast for slices with an offset 2022-03-03 13:08:14 -07:00
Cody Tapscott
b6f1a8612b stage2: Preserve larger alignment in @ptrCast 2022-03-03 13:08:14 -07:00
Mitchell Hashimoto
7deadf4301 stage2: reify error sets 2022-03-03 15:07:23 -05:00
Mitchell Hashimoto
b96d5fd71f
stage2: peer resolve const array pointer to unknown ptr 2022-03-03 11:08:14 -08:00
Mitchell Hashimoto
0924f17a23
stage2: peer resolve const u8 slices correctly 2022-03-03 11:04:04 -08:00
Andrew Kelley
b33f3b23c9 LLVM: fix lowering of unions and switches
`Module.Union.getLayout` now additionally returns a `padding` field
which tells how many bytes are between the final field end offset and
the ending offset of the union. This is used by the LLVM backend to
explicitly insert padding.

LLVM backend: lowering of unions now inserts additional padding so that
LLVM's internals will agree on the ABI size to match what ABI size zig
wants unions to be. This is an alternative to calling LLVMABISizeOfType
and LLVMABIAlignmentOfType which end up crashing when recursive struct
definitions come into play. We no longer ever call these two functions
and the bindings are deleted to avoid future footgun firings.

LLVM backend: lowering of unions now represents untagged unions
consistently. Before it was tripping an assertion.

LLVM backend: switch cases call inttoptr on the case items and condition
if necessary. Prevents tripping an LLVM assertion.

After this commit, we are no longer tripping over any LLVM assertions.
2022-03-03 01:19:49 -07:00
Andrew Kelley
06b1a88a15 Sema: implement cast from anon struct ptr to union ptr 2022-03-02 20:18:22 -07:00
Mitchell Hashimoto
ac7028f559
stage2: implement @errSetCast (#11039) 2022-03-02 22:01:55 -05:00
Andrew Kelley
f5e2e301e9 Sema: add coercion from anon structs to unions 2022-03-02 19:22:31 -07:00
Jakub Konka
3ec74a1cd8 codegen: handle elem_ptr when lowering to memory
* x64: handle storing from-to non-stack memory
2022-03-02 20:04:34 -05:00
Andrew Kelley
1c8a86f063 Sema: detect comptime-known union initializations
Follow a similar pattern as we already do for validate_array_init and
validate_struct_init.

I threw in a bit of behavior test cleanup on top of it.
2022-03-02 17:28:39 -07:00
Andrew Kelley
220708e7c3 LLVM: aggregate_init supports structs
in addition to tuples
2022-03-02 13:35:15 -07:00
Andrew Kelley
446324a1d8
Merge pull request #11025 from Vexu/stage2
stage2: implement `@extern`
2022-03-02 14:51:29 -05:00
Curtis Wilkinson
7fc8dd6642 Zir: rename the 'ret_coerce' tag to 'ret_tok' as per TODO 2022-03-02 14:46:09 -05:00
Cody Tapscott
5c8a507e7a stage2 parser: UTF-8 encode \u{NNNNNN} escape sequences
The core of this change is to re-use the escape sequence parsing logic
for parsing both string and character literals.

The actual fix is that UTF-8 encoding was missing for string literals
with \u{...} escape sequences.
2022-03-02 14:45:19 -05:00
Jakub Konka
77072d1a17 x64: fix bug in lowering optionals directly to immediate 2022-03-02 17:40:37 +01:00
Jakub Konka
b0bb1583cb codegen: leave f80 explicitly unhandled for now 2022-03-02 17:08:16 +01:00
Jakub Konka
c9d1db7e8e x64: fix incorrect calc of rdi spill stack loc for backpatching 2022-03-02 14:53:02 +01:00
Jakub Konka
350bf9db13 x64: fix intCast to properly clear out dest register 2022-03-02 14:34:43 +01:00
Jakub Konka
d35cae551e x64: rectify and add missing optionals bits
Includes changes/additions to:
* `wrap_optional`
* `optional_payload`
* `isNull` helper
2022-03-02 14:05:29 +01:00
Veikka Tuominen
403a1fe5d7 stage2: add cast from ?*T to ?*anyopaque 2022-03-02 12:26:04 +02:00