526 Commits

Author SHA1 Message Date
Andrew Kelley
935d208ffb
Merge pull request #11090 from mitchellh/inferred-ty
stage2: correct comptime-known inferred alloc type, plus comptime array init sentinels
2022-03-08 20:56:00 -05:00
joachimschmidt557
95fc41b2b4 stage2 ARM: implement ret_load 2022-03-08 21:10:04 +01:00
Mitchell Hashimoto
ba62853d26
stage2: sentinel array init must add sentinel to array value
I didn't realize that the `array` value type has the sentinel on it.
2022-03-08 12:05:12 -08:00
Mitchell Hashimoto
55ccf4c7a8 stage2: elem vals of many pointers need not deref pointers
By the time zirElemVal is reached for a many pointer, a load has already
happened, making sure the operand is already dereferenced.

This makes `mem.sliceTo` now work.
2022-03-08 14:10:43 -05:00
Andrew Kelley
6ffa44554e
Merge pull request #11079 from Vexu/stage2
stage2: make references to const allocs const
2022-03-08 13:49:29 -05:00
Evan Haas
4b9fd57aa8
translate-c: use nested scope for comma operator in macros
Fixes #11040
2022-03-08 20:38:51 +02:00
Jonathan Marler
d805adddd6 deprecated TypeInfo in favor of Type
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-03-08 20:38:12 +02:00
Jakub Konka
404f5d6179 x64: pass more behavior/cast.zig tests 2022-03-08 10:56:54 +01:00
joachimschmidt557
3ea603c82a stage2 ARM: implement ptr_add, ptr_sub for all element sizes
Also reduces slice_elem_val to ptr_add, simplifying the implementation
2022-03-08 10:54:08 +01:00
Veikka Tuominen
8f037db885 stage2: correct constness of allocs 2022-03-08 11:23:39 +02:00
Veikka Tuominen
1f4a097117 stage2: fix mem{set,cpy} for non comptime mutable pointers 2022-03-08 11:23:38 +02:00
Andrew Kelley
38c161afab Sema: fix @hasDecl for simple enums 2022-03-07 13:54:25 -07:00
Andrew Kelley
c467f6693e stage2: fix union layout returning non-zero for zero-sized tag 2022-03-07 13:36:58 -07:00
Andrew Kelley
f59cbd89e3
Merge pull request #11077 from mitchellh/array-init-ty
stage2: sentinel-terminated array initialization
2022-03-07 13:54:09 -05:00
Mitchell Hashimoto
c9fac41368
stage2: resolve array type for typed array init expressions
Array types with sentinels were not being typed correctly in the
translation from ZIR to Sema (comptime). This modifies the `array_init`
ZIR to also retain the type of the init expression (note: untyped array
initialization is done via the `array_init_anon` ZIR and so is unchanged
in this commit).
2022-03-07 07:30:30 -08:00
Andrew Kelley
8c32d989c9
Merge pull request #11054 from schmee/mul-add
Implement `@mulAdd` for scalar floats
2022-03-07 04:00:45 -05:00
Andrew Kelley
6547da8f97 Sema: handle peer type resolution of optional slices 2022-03-06 21:29:07 -07:00
Andrew Kelley
488eb8ac29 disable failing @mulAdd test 2022-03-06 16:18:51 -07:00
Andrew Kelley
71b8760d3b stage2: rework @mulAdd
* mul_add AIR instruction: use `pl_op` instead of `ty_pl`. The type is
   always the same as the operand; no need to waste bytes redundantly
   storing the type.
 * AstGen: use coerced_ty for all the operands except for one which we
   use to communicate the type.
 * Sema: use the correct source location for requireRuntimeBlock in
   handling of `@mulAdd`.
 * native backends: handle liveness even for the functions that are
   TODO.
 * C backend: implement `@mulAdd`. It lowers to libc calls.
 * LLVM backend: make `@mulAdd` handle all float types.
   - improved fptrunc and fpext to handle f80 with compiler-rt calls.
 * Value.mulAdd: handle all float types and use the `@mulAdd` builtin.
 * behavior tests: revert the changes to testing `@mulAdd`. These
   changes broke the test coverage, making it only tested at
   compile-time.

Improved f80 support:
 * std.math.fma handles f80
 * move fma functions from freestanding libc to compiler-rt
   - add __fmax and fmal
   - make __fmax and fmaq only exported when they don't alias fmal.
   - make their linkage weak just like the rest of compiler-rt symbols.
 * removed `longDoubleIsF128` and replaced it with `longDoubleIs` which
   takes a type as a parameter. The implementation is now more accurate
   and handles more targets. Similarly, in stage2 the function
   CTypes.sizeInBits is more accurate for long double for more targets.
2022-03-06 16:11:39 -07:00
John Schmidt
6637335981 stage2: implement @mulAdd for scalar floats 2022-03-06 15:36:56 -07:00
Jakub Konka
27c084065a
Merge pull request #11070 from Luukdegram/wasm-unify
stage2: wasm - unify codegen with other backends
2022-03-06 20:44:51 +01:00
Luuk de Gram
6d84f22fa0 stage2: Fix wasm linker for llvm backend
This fixes 2 entrypoints within the self-hosted wasm linker that would be called
for the llvm backend, whereas we should simply call into the llvm backend to perform such action.
i.e. not allocate a decl index when we have an llvm object, and when flushing a module,
we should be calling it on llvm's object, rather than have the wasm linker perform the operation.

Also, this fixes the wasm intrinsics for wasm.memory.size and wasm.memory.grow.
Lastly, this commit ensures that when an extern function is being resolved, we tell LLVM how
to import such function.
2022-03-06 14:17:36 -05:00
Luuk de Gram
70fc6e3776
wasm: call into generateSymbol when lowering
This also unifies the wasm backend to use `generateSymbol` when lowering a constant
that cannot be lowered to an immediate value.
As both decls and constants are now refactored, the old `genTypedValue` is removed.
2022-03-06 19:38:53 +01:00
Luuk de Gram
5a45fe2dba
wasm: Call generateSymbol for updateDecl
To unify the wasm backend with the other backends, we will now call `generateSymbol` to
lower a Decl into bytes. This means we also have to change some function signatures
to comply with the linker interface.

Since the general purpose generateSymbol is less featureful than wasm's, some tests are
temporarily disabled.
2022-03-06 19:38:50 +01:00
Joachim Schmidt
ac936c0aba
Merge pull request #11059 from joachimschmidt557/stage2-aarch64
stage2 AArch64: various improvements
2022-03-05 14:30:02 +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
Mitchell Hashimoto
e297860158 stage2: test changed behavior of c pointer resolution from stage1
stage1 peer resolves the given test to `[*c]u8` but stage2 makes that a
const u8. I believe stage2 behavior is correct since the pointer itself
is const.
2022-03-05 02:56:24 -05: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
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
21f0503c01 Update behavior tests 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
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
0a4f58490f behavior tests cleanup: promote math.zig 2022-03-03 01:24:26 -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
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
c9d1db7e8e x64: fix incorrect calc of rdi spill stack loc for backpatching 2022-03-02 14:53:02 +01:00