709 Commits

Author SHA1 Message Date
viri
e46c612503
use math/float.zig everywhere 2022-04-07 05:04:38 -06:00
Andrew Kelley
62f54aa39c Sema: in-memory coercion of differently named int types
which have the same number of bits and the same signedness.
2022-04-06 02:39:55 -07:00
Andrew Kelley
95a87e88fa Sema: forward switch condition to captures 2022-04-04 22:46:05 -07:00
Andrew Kelley
51ef31a833 Sema: add empty tuple to mutable slice coercion 2022-04-04 14:29:08 -07:00
Andrew Kelley
3b32e0be31 behavior tests: disable failing stage1 test
I forgot to check that the new behavior tests also pass in stage1. One
of them does not.

Fixes regression from 4618c41fa6ca70f06c7e65762d2f38d57b00818c.
2022-04-02 19:02:29 -07:00
Andrew Kelley
4618c41fa6 Sema: mechanism for converting comptime breaks to runtime
closes #11369
2022-04-02 18:30:44 -07:00
Veikka Tuominen
83bb98e13b stage2 llvm: properly align error union payload 2022-04-02 19:31:32 -04:00
Luuk de Gram
a0a587ff85 wasm: Enable passing behavior tests
This shuffles some tests do ensure the new instructions are tested for the wasm backend,
by moving vectors into their own tests as well as move the f16 test cases as those require
special operating also.
2022-04-02 21:54:01 +02:00
joachimschmidt557
c4778fc029
stage2 ARM: implement mul_with_overflow for ints <= 16 bits 2022-04-01 22:02:56 +02:00
joachimschmidt557
77e70189f4
stage2 ARM: implement shl_with_overflow for ints <= 32 bits 2022-04-01 22:02:56 +02:00
joachimschmidt557
37a8c28802
stage2 ARM: implement add/sub_with_overflow for ints < 32 bits 2022-04-01 22:02:56 +02:00
Jakub Konka
fd29ddc06c x64: implement add/sub with wrapping and xor op 2022-04-01 11:37:18 +02:00
Andrew Kelley
cf4aad4858 AstGen: fix referencing unreferencable instructions
Sema avoids adding map entries for certain instructions such as
`set_eval_branch_quota` and `atomic_store`. This means that result
location semantics in AstGen must not emit any instructions that attempt
to use the result of any of these instructions.

This commit makes AstGen replace such instructions with
`Zir.Inst.Ref.void_value` if their result value ends up being
referenced.

This fixes a compiler crash when running std lib atomic tests.
2022-03-30 23:19:10 -07:00
Andrew Kelley
02d69f5009 Sema: fix usingnamespace decl Value in wrong arena
closes #11297
2022-03-30 17:24:01 -07:00
Veikka Tuominen
3c64c519e6
Merge pull request #11246 from jmc-88/cbe-asm
CBE: improve support for asm inputs
2022-03-31 00:04:04 +03:00
Meghan
b73cf97c93
replace other uses of std.meta.Vector with @Vector (#11346) 2022-03-30 14:12:14 -04:00
Jakub Konka
b153e156b1
Merge pull request #11342 from ziglang/stage2-x64-mul-div
x64: use all available registers (caller and callee), and fix how we multiply (fixes @mulWithOverflow for byte-size operands)
2022-03-30 11:22:35 +02:00
Daniele Cocca
907dc1e13f CBE: improve support for asm inputs
This is not complete support for asm expressions, but allows a few more
test cases from test/behavior/asm.zig to pass. Since the non-register
inputs are named `input_${n}` they can cause name collisions: I'm
wrapping the asm expressions in their own block to prevent that.

Contextually, this change also makes test/behavior/asm.zig run for
stage2, but skips individual tests for most backends (I only verified
the C and LLVM backends successfully run one new test case) and the
entire test file for aarch64, where it's running into preexisting
shortcomings.
2022-03-30 12:10:02 +03:00
Andrew Kelley
c21f046a8b Sema: enhance is_non_err to be comptime more often
* Sema: store the precomputed monomorphed_funcs hash inside Module.Fn.
   This is important because it may be accessed when resizing monomorphed_funcs
   while this Fn has already been added to the set, but does not have the
   owner_decl, comptime_args, or other fields populated yet.
 * Sema: in `analyzeIsNonErr`, take advantage of the AIR tag being
   `wrap_errunion_payload` to infer that `is_non_err` is comptime true
   without performing any error set resolution.
   - Also add some code to check for empty inferred error sets in this
     function. If necessary we do resolve the inferred error set.
 * Sema: queue full type resolution of payload type when
   `wrap_errunion_payload` AIR instruction is emitted. This ensures the
   backend may check the alignment of it.
 * Sema: resolveTypeFully now additionally resolves comptime-only
   status.

closes #11306
2022-03-30 00:47:55 -07:00
Andrew Kelley
05947ea870 stage2: implement @intToError with safety
This commit introduces a new AIR instruction `cmp_lt_errors_len`. It's
specific to this use case for two reasons:

 * The total number of errors is not stable during semantic analysis; it
   can only be reliably checked when flush() is called. So the backend
   that is lowering the instruction must emit a relocation of some kind
   and then populate it during flush().
 * The fewer AIR instructions in memory, the better for compiler
   performance, so we squish complex meanings into AIR tags without
   hesitation.

The instruction is implemented only in the LLVM backend so far. It does
this by creating a simple function which is gutted and re-populated
with each flush().

AstGen now uses ResultLoc.coerced_ty for `@intToError` and Sema does the
coercion.
2022-03-29 22:19:06 -07:00
Andrew Kelley
9821a0c6f0 Sema: fix generic instantiations of return types with nested captures
* In semaStructFields and semaUnionFields we return error.GenericPoison
   if one of the field types ends up being generic poison.
   - This requires handling function calls and function types taking
     this into account when calling `typeRequiresComptime` on the return
     type.
 * Unrelated: I noticed using Valgrind that struct reification did not
   populate the `known_opv` field. After fixing it, the behavior tests
   run Valgrind-clean.
 * ZIR: use `@ptrCast` to cast between slices instead of exploiting
   the fact that stage1 incorrectly allows `@bitCast` between slices.
   - A future enhancement will make Zig support `@ptrCast` to directly
     cast between slices.
2022-03-29 20:11:48 -07:00
Andrew Kelley
b59428e9f7 Sema: adjust coercion of undefined error union payload
To no longer set the error code to undefined. This fixes the problem
where an undefined single-item pointer coerced to an error union of a
slice set the whole thing to undefined even though the sub-coercion to
the slice would have produced a defined value.
2022-03-29 16:56:12 -07:00
Jakub Konka
ee6e3aef5d x64: redo @mulWithOverflow using rax/rdx based multiplication 2022-03-30 00:37:42 +02:00
Daniele Cocca
8238d4b335 CBE: fix C output after PR #11302, reenable tests
Commit 052079c99455d01312d377d72fa1b8b5c0b22aad surfaced two issues with
the generated C code:

  - renderInt128() contained a seemingly unnecessary assertion to verify
    that the high 64 bits of the number were nonzero, dating back to
    9bf1681990fe87a6b2e5fc644a89f1aece304579. I removed it.
  - renderValue() didn't have any special handling for undefined structs,
    falling back to printing "{}" which generated invalid expressions
    such as "return {}" for functions returning structs, whereas
    "return (S){}" is the correct form. I changed it accordingly.

At the same time I'm reenabling the relevant tests.
2022-03-29 02:28:20 -04:00
Andrew Kelley
4dd65316b7 AstGen: coerce break operands of labeled blocks
Similar code was already in place for conditional branches. This updates
AstGen to do the same for labeled blocks. It takes advantage of the
`store_to_block_ptr` instructions by mutating them in place to become
`as` instructions, coercing the break operands before they are returned
from the block.
2022-03-28 18:28:08 -07:00
Andrew Kelley
b6ccde47ad Sema: allow mixing array and vector operands
* Added peer type resolution for arrays and vectors: the vector type is
   selected.
 * Fixed passing the lhs type or rhs type instead of the peer resolved
   type when calling Value methods during analyzeArithmetic handling of
   comptime expressions.
 * `checkVectorizableBinaryOperands` now allows mixing vectors and
   arrays, as long as one of the operands is a vector.

This matches stage1's handling of `^=` but apparently stage1 is
inconsistent and does not handle e.g. `*=`. stage2 now will always allow
mixing vector and array operands for all operations.
2022-03-28 14:17:05 -07:00
Veikka Tuominen
c517e65d8f Sema: implement coerceInMemoryAllowed for optionals 2022-03-28 13:05:40 -07:00
Veikka Tuominen
a415fe0bc0 AstGen: clear rl_ty_inst in setBreakResultLoc if one is not provided 2022-03-28 13:05:08 -07:00
Veikka Tuominen
3c4ac47e00 stage2 llvm: fix union init of byRef values 2022-03-28 13:04:50 -07:00
Jakub Konka
e6729036e4 x64: partially fix genImul, enable overflow tests 2022-03-28 17:45:50 +02:00
Andrew Kelley
c8f8440271 stage1: disable failing test
The new behavior test introduced in the previous commit is not passing
for stage1 on mips.
2022-03-27 14:40:24 -07:00
Andrew Kelley
6d2ec7a4e3 LLVM: handle aggregate_init for packed structs 2022-03-27 14:22:47 -07:00
Andrew Kelley
052079c994
Merge pull request #11302 from mitchellh/slice-null-cptr
stage2: runtime safety checks for slicing a null C pointer and @intCast truncating bits
2022-03-27 15:37:09 -04:00
Luuk de Gram
e1bb09648f
wasm: Enable overflow behavior tests 2022-03-27 19:02:45 +02:00
Mitchell Hashimoto
8fbac2e86d
stage2: runtime safety check integer cast truncating bits 2022-03-27 09:20:37 -07:00
Mitchell Hashimoto
01698528d1
stage2: safety checks for slicing a null C pointer 2022-03-27 09:20:35 -07:00
leesongun
7ae22813ee
stage1: implement casting from u0 2022-03-27 11:49:54 +03:00
Luuk de Gram
16e88b75ba wasm: Enable passing tests 2022-03-26 21:20:29 +01:00
Andrew Kelley
88e98a0611
Merge pull request #11289 from schmee/stage2-select
stage2: implement `@select`
2022-03-26 00:33:22 -04:00
Andrew Kelley
bae35bdf2d stage2: result location types for function call arguments
* 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-26 00:27:12 -04:00
Andrew Kelley
bcd7eb012a
Merge pull request #11304 from joachimschmidt557/stage2-aarch64
stage2 AArch64: remove MCValue.embedded_in_code
2022-03-25 22:22:47 -04:00
Veikka Tuominen
2f326f24dd Sema: implement zirSwitchCapture multi for unions 2022-03-25 22:32:15 +02:00
Veikka Tuominen
17d214a249 Sema: implement zirStructInit for runtime-known union values 2022-03-25 22:32:15 +02:00
Veikka Tuominen
5ff518fbb9 Sema: implement zirSwitchCapture for error sets 2022-03-25 22:32:14 +02:00
Veikka Tuominen
f6bd534fc9 Sema: ensure error_set_merged is sorted 2022-03-25 22:32:14 +02:00
joachimschmidt557
061d6699c0
stage2 AArch64: lower cmp to binOp 2022-03-25 19:21:34 +01:00
John Schmidt
cd46daf7d0 sema: coerce inputs to vectors in zirSelect 2022-03-25 19:14:11 +01:00
John Schmidt
12d5efcbe6 stage2: implement @select 2022-03-25 16:13:54 +01:00
Andrew Kelley
7f91be9c80 AstGen: emit break_inline from inline while loop 2022-03-24 22:45:10 -07:00
Andrew Kelley
bcf2eb1a00 Sema: fix closure capture typeof runtime-known parameter
Closures are not necessarily constant values. For example, Zig
code might do something like this:

    fn foo(x: anytype) void {
        const S = struct {field: @TypeOf(x)};
    }

...in which case the closure_capture instruction has access to a
runtime value only. In such case we preserve the type and use a
dummy runtime value.

closes #11292
2022-03-24 21:47:18 -07:00