268 Commits

Author SHA1 Message Date
jacobly0
562ac8be48
codegen: add support for lowering .field_ptr on a slice
Closes #13068
2022-10-12 12:40:59 +03:00
Veikka Tuominen
d48af541c7 Sema: handle union and enum field order being different
Closes #12543
2022-08-21 12:51:40 +03:00
Veikka Tuominen
7c9979a02e stage2: generate a switch for @errSetCast safety 2022-08-12 11:40:37 +03:00
Veikka Tuominen
0daa77bd63 stage2 cbe: correct airIsNull ptr operand check 2022-08-05 22:13:58 +03:00
Veikka Tuominen
f46d7304b1 stage2: add runtime safety for invalid enum values 2022-08-05 22:13:58 +03:00
Veikka Tuominen
d75fa86d70 stage2: implement @setFloatMode 2022-07-23 15:40:12 +03:00
Veikka Tuominen
ff7ec4efb5 Sema: bad union field access safety 2022-07-23 15:40:11 +03:00
Veikka Tuominen
03b1fbe50d stage2: fix airIsErr when is_ptr == true 2022-07-22 21:07:55 +03:00
Andrew Kelley
6bc6e47b15 stage2: lower float negation explicitly
Rather than lowering float negation as `0.0 - x`.

 * Add AIR instruction for float negation.
 * Add compiler-rt functions for f128, f80 negation

closes #11853
2022-06-30 00:02:00 -07:00
Veikka Tuominen
38a1222c87 std.crypto: fix invalid pass by value 2022-06-20 15:11:22 +03:00
Veikka Tuominen
0333ff4476 stage2: make error{} the same size as anyerror
Having `error{}` be a zero bit type causes issues when it interracts
with empty inferred error sets which are the same size as `anyerror`.
2022-06-11 23:49:33 +03:00
Andrew Kelley
bac132bc8f introduce std.debug.Trace
And use it to debug a LazySrcLoc in stage2 that is set to a bogus value.

The actual fix in this commit is:

```diff
-        try sema.emitBackwardBranch(&child_block, call_src);
+        try sema.emitBackwardBranch(block, call_src);
```
2022-06-09 15:37:16 -07:00
Andrew Kelley
d2f31d315e C backend: implement try instruction 2022-06-05 10:37:08 +02:00
Andrew Kelley
ef885a78d6 stage2: implement the new "try" ZIR/AIR instruction
Implements semantic analysis for the new try/try_inline ZIR
instruction. Adds the new try/try_ptr AIR instructions and implements
them for the LLVM backend.

Fixes not calling rvalue() for tryExpr in AstGen.

This is part of an effort to implement #11772.
2022-06-05 10:37:08 +02:00
Andrew Kelley
c711c788f0 stage2: fixes for error unions, optionals, errors
* `?E` where E is an error set with only one field now lowers the same
   as `bool`.
 * Fix implementation of errUnionErrOffset and errUnionPayloadOffset to
   properly compute the offset of each field. Also name them the same
   as the corresponding LLVM functions and have the same function
   signature, to avoid confusion. This fixes a bug where wasm was
   passing the error union type instead of the payload type.
 * Fix C backend handling of optionals with zero-bit payload types.
 * C backend: separate out airOptionalPayload and airOptionalPayloadPtr
   which reduces branching and cleans up control flow.
 * Make Type.isNoReturn return true for error sets with no fields.
 * Make `?error{}` have only one possible value (null).
2022-05-24 15:34:52 -07:00
Andrew Kelley
02e9d9b43b stage2: make ?anyerror represented the same as anyerror
I was able to get the backend implementation working on LLVM and the C
backend, but I'm going to ask for some help on the other backends.
2022-05-24 15:34:52 -07:00
Andrew Kelley
c97c7f9e3b C backend: update to new error union semantics 2022-05-24 15:34:52 -07:00
Andrew Kelley
b6798c26ef stage2: fix pointer arithmetic result type
This makes it so the result of doing pointer arithmetic creates a new
pointer type that has adjusted alignment.
2022-05-17 23:50:38 -07:00
Veikka Tuominen
66c3988e5e stage2: disable error return tracing on unsupported targets 2022-05-16 17:42:51 -07:00
Veikka Tuominen
eee8fffec7 stage2: implement error return traces 2022-05-16 17:42:51 -07:00
Andrew Kelley
03ed0f0d28 C backend: implement overflow arithmetic
Most of the work here was additions to zig.h. The lowering code is
mainly responsible for calling the correct function name depending on
the operand type.

Some of the compiler-rt calls here are not implemented yet and are
non-standard symbols due to the C programming language not needing them.

After this commit, the behavior tests with -ofmt=c are passing again.
2022-05-16 13:55:26 -07:00
Andrew Kelley
65389dc280 stage2: improve inline asm stage1 compatibility
* outputs can have names and be referenced with template replacements
   the same as inputs.
 * fix print_air.zig not decoding correctly.
 * LLVM backend: use a table for template names for simplicity
2022-05-02 22:14:17 -07:00
Daniele Cocca
d127c1d59e CBE: handle returning undefined for ErrorUnion
Just like for Struct in 8238d4b33585a715c58ab559cd001dd3ea1db55b, in the
case of ErrorUnion struct we need to return a compound literal "(T){...}"
instead of just "{}", which is invalid code when used in e.g. a "return"
expression.
2022-04-30 13:29:21 -04:00
Daniele Cocca
fda143d5d8 CBE: fix renderValue() for struct fields with no runtime bits
These shouldn't count towards the total emitted, or the stray comma
separators would cause compilation errors.
2022-04-28 23:41:42 -04:00
Andrew Kelley
09f1d62bdf add new builtin function @tan
The reason for having `@tan` is that we already have `@sin` and `@cos`
because some targets have machine code instructions for them, but in the
case that the implementation needs to go into compiler-rt, sin, cos, and
tan all share a common dependency which includes a table of data. To
avoid duplicating this table of data, we promote tan to become a builtin
alongside sin and cos.

ZIR: The tag enum is at capacity so this commit moves
`field_call_bind_named` to be `extended`. I measured this as one of
the least used tags in the zig codebase.

Fix libc math suffix for `f32` being wrong in both stage1 and stage2.
stage1: add missing libc prefix for float functions.
2022-04-27 16:45:23 -07:00
Annika L
c992b25908 C backend: Fix array declarations 2022-04-22 07:42:47 -07:00
Andrew Kelley
f7596ae942 stage2: use indexes for Decl objects
Rather than allocating Decl objects with an Allocator, we instead allocate
them with a SegmentedList. This provides four advantages:
 * Stable memory so that one thread can access a Decl object while another
   thread allocates additional Decl objects from this list.
 * It allows us to use u32 indexes to reference Decl objects rather than
   pointers, saving memory in Type, Value, and dependency sets.
 * Using integers to reference Decl objects rather than pointers makes
   serialization trivial.
 * It provides a unique integer to be used for anonymous symbol names,
   avoiding multi-threaded contention on an atomic counter.
2022-04-20 17:37:35 -07:00
Andrew Kelley
2587474717 stage2: progress towards stage3
* The `@bitCast` workaround is removed in favor of `@ptrCast` properly
   doing element casting for slice element types. This required an
   enhancement both to stage1 and stage2.
 * stage1 incorrectly accepts `.{}` instead of `{}`. stage2 code that
   abused this is fixed.
 * Make some parameters comptime to support functions in switch
   expressions (as opposed to making them function pointers).
 * Avoid relying on local temporaries being mutable.
 * Workarounds for when stage1 and stage2 disagree on function pointer
   types.
 * Workaround recursive formatting bug with a `@panic("TODO")`.
 * Remove unreachable `else` prongs for some inferred error sets.

All in effort towards #89.
2022-04-14 10:12:45 -07:00
Andrew Kelley
b0edd8752a Liveness: modify encoding to support over 32 operands
Prior to this, Liveness encoded `asm`, `call`, and `aggregate_init` with
a single 32-bit integer, allowing up to 35 operands (3 are provided by
the regular tomb_bits). However, the Zig language allows function calls
with more than 35 arguments, inline assembly with more than 35 inputs,
and anonymous tuples with more than 35 elements.

The new encoding stores an index to the extra array instead of the bits
directly, and then as many extra elements as needed to encode all the
operands. The MSB is used as a flag to tell which element is the last
one, allowing for 31 bits per element.

Prior to this, print_air did not bother correctly printing tombstones
for these instructions; now it does.

In addition to updating the BigTomb iteration logic in the machine code
backends, this commit extracts the common logic into the Liveness namespace.
2022-04-12 11:22:12 -07: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
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
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
John Schmidt
12d5efcbe6 stage2: implement @select 2022-03-25 16:13:54 +01:00
Cody Tapscott
a9a91a5d49 stage2 CBE: Improve support for unions and error sets
This includes various fixes/improvements to the C backend to improve
error/union support. It also fixes up our handling of decls, where some
decls were not correctly marked alive.
2022-03-23 16:29:38 -04:00
Andrew Kelley
593130ce0a stage2: lazy @alignOf
Add a `target` parameter to every function that deals with Type and
Value.
2022-03-22 15:45:58 -07:00
William Sengir
0f48307041 stage2: add AIR instruction cmp_vector
The existing `cmp_*` instructions get their result type from `lhs`, but
vector comparison will always return a vector of bools with only the
length derived from its operands. This necessitates the creation of a
new AIR instruction.
2022-03-21 16:54:19 -07:00
Andrew Kelley
0576086395 stage2: remove Value.Tag.abi_align_default
and make Decl alignment & linksection, and struct & union field alignment
be scalar values, not Value values.

YAGNI
2022-03-20 00:36:44 -07:00
Daniele Cocca
b6203b89d6 CBE: implement mod, divFloor, divTrunc 2022-03-19 21:01:36 -04:00
Veikka Tuominen
a8520fbd0f stage2: add dbg_block_{begin,end} instruction 2022-03-19 11:20:38 +02:00
Daniele Cocca
6fdca525de CBE: add support for tuples
Also promote tests that are now passing.
2022-03-18 16:50:34 +00:00
Andrew Kelley
7233a3324a stage2: implement @reduce
Notably, Value.eql and Value.hash are improved to treat NaN as equal to
itself, so that Type/Value can be hash map keys. Likewise float hashing
normalizes the float value before computing the hash.
2022-03-17 17:24:35 -07:00
Daniele Cocca
312536540b CBE: better handling of sentineled slices/arrays
Adds the sentinel element to the type name to avoid ambiguous
declarations, and outputs the sentinel element (if needed) even in what
would otherwise be empty arrays.
2022-03-16 19:58:45 -07:00
Veikka Tuominen
d83a26f068 stage2 llvm: keep track of inlined functions 2022-03-16 10:53:41 +02:00
Veikka Tuominen
0343811836 Sema: emit dbg_func around inline calls 2022-03-16 09:34:26 +02:00
Daniele Cocca
79f74943b5 CBE: fix output of airMinMax()
This was trying to output a ternary operator, but the output was broken
by the lack of a '?'.
2022-03-15 19:00:16 +00:00
Andrew Kelley
84f96779c3
Merge pull request #11143 from jmc-88/cbe
CBE: Implement popCount, byteSwap, bitReverse for ints <= 128 bits
2022-03-14 18:23:00 -04:00
Daniele Cocca
8643591c9a CBE: split {clz,ctz,mod,popCount,byteSwap,bitReverse} by type
This also surfaces the fact that clz, ctz and popCount didn't actually
support 128 bit integers, despite what was claimed by
226fcd7c709ec664c5d883042cf7beb3026f66cb. This was partially hidden by
the fact that the test code for popCount only exercised 128 bit integers
in a comptime context. This commit duplicates that test case for runtime
ints too.
2022-03-14 19:43:31 +00:00
Andrew Kelley
5ea94e7715 stage2: rework Value storage of structs and arrays
Now they both use `Value.Tag.aggregate`.

Additionally the LLVM backend now has implemented lowering of
tuple values.
2022-03-14 12:28:52 -07:00
Daniele Cocca
d912699e08 Remove signed_type from zig_{clz,ctz,popcount}
This parameter is only currently needed by zig_byte_swap() and
zig_bit_reverse(). This commit adds an option to airBuiltinCall() to
allow emitting the signedness information only when needed, removing
this unused parameter from the other builtins.
2022-03-14 01:04:24 +00:00
Daniele Cocca
5a971bbeea Review suggestion: use hasRuntimeBitsIgnoreComptime()
This should cover not only integers, as done in
87744a7ea9a2449764a110da4210d7750e3938ee, but also void, enums with a
single field, etc...

Co-authored-by: Andrew Kelley <andrew@ziglang.org>
2022-03-14 00:52:20 +00:00