Commit Graph

1719 Commits

Author SHA1 Message Date
mlugg
6df78c3bc1 Sema: mark pointers to inline functions as comptime-only
This is supposed to be the case, similar to how pointers to generic
functions are comptime-only (several pieces of logic already assumed
this). These types being considered runtime was causing `dbg_var_val`
AIR instructions to be wrongly emitted for such values, causing codegen
backends to create a runtime reference to the inline function, which (at
least on the LLVM backend) triggers an error.

Resolves: #38
2023-09-15 21:46:38 -07:00
mlugg
88f5315ddf compiler: implement destructuring syntax
This change implements the following syntax into the compiler:

```zig
const x: u32, var y, foo.bar = .{ 1, 2, 3 };
```

A destructure expression may only appear within a block (i.e. not at
comtainer scope). The LHS consists of a sequence of comma-separated var
decls and/or lvalue expressions. The RHS is a normal expression.

A new result location type, `destructure`, is used, which contains
result pointers for each component of the destructure. This means that
when the RHS is a more complicated expression, peer type resolution is
not used: each result value is individually destructured and written to
the result pointers. RLS is always used for destructure expressions,
meaning every `const` on the LHS of such an expression creates a true
stack allocation.

Aside from anonymous array literals, Sema is capable of destructuring
the following types:
* Tuples
* Arrays
* Vectors

A destructure may be prefixed with the `comptime` keyword, in which case
the entire destructure is evaluated at comptime: this means all `var`s
in the LHS are `comptime var`s, every lvalue expression is evaluated at
comptime, and the RHS is evaluated at comptime. If every LHS is a
`const`, this is not allowed: as with single declarations, the user
should instead mark the RHS as `comptime`.

There are a few subtleties in the grammar changes here. For one thing,
if every LHS is an lvalue expression (rather than a var decl), a
destructure is considered an expression. This makes, for instance,
`if (cond) x, y = .{ 1, 2 };` valid Zig code. A destructure is allowed
in almost every context where a standard assignment expression is
permitted. The exception is `switch` prongs, which cannot be
destructures as the comma is ambiguous with the end of the prong.

A follow-up commit will begin utilizing this syntax in the Zig compiler.

Resolves: #498
2023-09-15 11:33:53 -07:00
mlugg
cba7e8a4e9 AstGen: do not forward result pointers through @as
The `coerce_result_ptr` instruction is highly problematic and leads to
unintentional memory reinterpretation in some cases. It is more correct
to simply not forward result pointers through this builtin.

`coerce_result_ptr` is still used for struct and array initializations,
where it can still cause issues. Eliminating this usage will be a future
change.

Resolves: #16991
2023-09-15 01:05:02 -07:00
Techatrix
ab970094ab wasm: enable successful behavior tests 2023-09-10 15:59:02 +02:00
Jacob Young
e2ff486de5 Sema: cleanup coerceExtra
* remove unreachable code
 * remove already handled cases
 * avoid `InternPool.getCoerced`
 * add some undef checks
 * error when converting undef int to float

Closes #16987
2023-08-30 16:50:30 -04:00
Jacob Young
c429bb5d2f llvm/cbe: support slice in @prefetch
Closes #16967
2023-08-28 19:04:53 -07:00
mlugg
b8e6c42688 compiler: provide result type for @memset value
Resolves: #16986
2023-08-28 12:33:36 -07:00
mlugg
8d036d1d78 Sema: allow cast builtins on vectors
The following cast builtins did not previously work on vectors, and have
been made to:

* `@floatCast`
* `@ptrFromInt`
* `@intFromPtr`
* `@floatFromInt`
* `@intFromFloat`
* `@intFromBool`

Resolves: #16267
2023-08-28 12:32:02 -07:00
Jay Petacat
ff61c42879 std: Rename TailQueue to DoublyLinkedList
`TailQueue` was implemented as a doubly-linked list, but named after an
abstract data type. This was inconsistent with `SinglyLinkedList`, which
can be used to implement an abstract data type, but is still named after
the implementation. Renaming `TailQueue` to `DoublyLinkedList` improves
consistency between the two type names, and should help discoverability.

`TailQueue` is now a deprecated alias of `DoublyLinkedList`.

Related to issues #1629 and #8233.
2023-08-27 20:57:46 -07:00
riverbl
87557b37c6 Replace @panic with unreachable, add test
Replace `@panic` with `unreachable` in stage2 wasm `@divFloor` implementation

Add test for division and remainder operations for stage2 wasm
2023-08-23 20:34:59 +01:00
Andrew Kelley
ada0010471 compiler: move unions into InternPool
There are a couple concepts here worth understanding:

Key.UnionType - This type is available *before* resolving the union's
fields. The enum tag type, number of fields, and field names, field
types, and field alignments are not available with this.

InternPool.UnionType - This one can be obtained from the above type with
`InternPool.loadUnionType` which asserts that the union's enum tag type
has been resolved. This one has all the information available.

Additionally:

* ZIR: Turn an unused bit into `any_aligned_fields` flag to help
  semantic analysis know whether a union has explicit alignment on any
  fields (usually not).
* Sema: delete `resolveTypeRequiresComptime` which had the same type
  signature and near-duplicate logic to `typeRequiresComptime`.
  - Make opaque types not report comptime-only (this was inconsistent
    between the two implementations of this function).
* Implement accepted proposal #12556 which is a breaking change.
2023-08-22 13:54:14 -07:00
Carl Åstholm
60fc18bd1c compiler_rt: fix f80 comparisons
This corrects comparisons between negative numbers.
2023-08-21 11:26:25 -07:00
mlugg
283afb50b5 AstGen: disallow '-0' integer literal
The intent here is ambiguous: this resolves to the comptime_int '0', but
it's likely the user meant to use a floating-point literal.

Resolves: #16890
2023-08-21 11:47:31 +03:00
Lewis Gaul
387b0ac4f1
Make NaNs quiet by default and other NaN tidy-up (#16826)
* Generalise NaN handling and make std.math.nan() give quiet NaNs

* Address uses of std.math.qnan_* and std.math.nan_* consts

* Comment out failing test due to issues with signalling NaN

* Fix issue in c_builtins.zig where we need qnan_u32
2023-08-18 02:07:49 -04:00
Jacob Young
8b9161179d Sema: avoid deleting runtime side-effects in comptime initializers
Closes #16744
2023-08-11 11:01:47 -07:00
Xavier Bouchoux
77dd64b5f4 Sema: fix coerceArrayLike() for vectors with padding
as explainded at https://llvm.org/docs/LangRef.html#vector-type :

"In general vector elements are laid out in memory in the same way as array types.
Such an analogy works fine as long as the vector elements are byte sized.
However, when the elements of the vector aren’t byte sized it gets a bit more complicated.
One way to describe the layout is by describing what happens when a vector such
as <N x iM> is bitcasted to an integer type with N*M bits, and then following the
rules for storing such an integer to memory."

"When <N*M> isn’t evenly divisible by the byte size the exact memory layout
is unspecified (just like it is for an integral type of the same size)."
2023-08-10 16:10:59 -07:00
Andrew Kelley
b820d5df79
Merge pull request #16747 from jacobly0/llvm-wo-libllvm
llvm: enable the backend even when not linked to llvm
2023-08-10 12:02:57 -07:00
mlugg
f2c8fa769a
Sema: refactor generic calls to interleave argument analysis and parameter type resolution
AstGen provides all function call arguments with a result location,
referenced through the call instruction index. The idea is that this
should be the parameter type, but for `anytype` parameters, we use
generic poison, which is required to be handled correctly.

Previously, generic instantiations and inline calls worked by evaluating
all args in advance, before resolving generic parameter types. This
means any generic parameter (not just `anytype` ones) had generic poison
result types. This caused missing result locations in some cases.

Additionally, the generic instantiation logic caused `zirParam` to
analyze the argument types a second time before coercion. This meant
that for nominal types (struct/enum/etc), a *new* type was created,
distinct to the result type which was previously forwarded to the
argument expression.

This commit fixes both of these issues. Generic parameter type
resolution is now interleaved with argument analysis, so that we don't
have unnecessary generic poison types, and generic instantiation logic
now handles parameters itself rather than falling through to the
standard zirParam logic, so avoids duplicating the types.

Resolves: #16566
Resolves: #16258
Resolves: #16753
2023-08-10 10:00:26 +01:00
mlugg
6917a8c258
AstGen: handle ty result location for struct and array init correctly
Well, this was a journey!

The original issue I was trying to fix is covered by the new behavior
test in array.zig: in essence, `ty` and `coerced_ty` result locations
were not correctly propagated.

While fixing this, I noticed a similar bug in struct inits: the type was
propagated to *fields* fine, but the actual struct init was
unnecessarily anonymous, which could lead to unnecessary copies. Note
that the behavior test added in struct.zig was already passing - the bug
here didn't change any easy-to-test behavior - but I figured I'd add it
anyway.

This is a little harder than it seems, because the result type may not
itself be an array/struct type: it could be an optional / error union
wrapper. A new ZIR instruction is introduced to unwrap these.

This is also made a little tricky by the fact that it's possible for
result types to be unknown at the time of semantic analysis (due to
`anytype` parameters), leading to generic poison. In these cases, we
must essentially downgrade to an anonymous initialization.

Fixing these issues exposed *another* bug, related to type resolution in
Sema. That issue is now tracked by #16603. As a temporary workaround for
this bug, a few result locations for builtin function operands have been
disabled in AstGen. This is technically a breaking change, but it's very
minor: I doubt it'll cause any breakage in the wild.
2023-08-09 19:46:55 +01:00
Jacob Young
66084b6c3f Sema: remove validateRunTimeType
This function does not seem to differ in any interesting way from
`!typeRequiresComptime`, other than the `is_extern` param which is only
used in one place, and some differences did not seem correct anyway.

My reasoning for changing opaque types to be comptime-only is that
`explainWhyTypeIsComptime` is quite happy to explain why they are. :D
2023-08-09 05:46:44 -04:00
Stevie Hryciw
2ad6ca581d Add behavior test for copying self-referential struct
Closes #6312. In the C++ implementation this caused a stack overflow
from infinite recursion during analysis.
2023-08-06 18:23:28 -07:00
Stevie Hryciw
6bba5a39e4 Add behavior test for ptrCast on function pointer
Closes #6280. In the C++ implementation this triggered a
compiler assertion.
2023-08-06 18:09:41 -07:00
Andrew Kelley
4f6013bf50 add behavior test for sub-aligned field access
The workaround in std.zig.Server remains because the C backend is not
passing the new test.

see #14904
2023-08-01 00:39:30 -07:00
Jacob Young
228c956377 std: finish cleanup up asm
This also required implementing the necessary syntax in the x86_64 backend.
2023-07-31 03:49:21 -04:00
Jacob Young
9831f27238 cbe: get behavior tests running on arm
Specifically without linking libc.
2023-07-31 01:58:10 -04:00
Jacob Young
817fa3af86 std: cleanup asm usage
After fixing some issues with inline assembly in the C backend, the std
cleanups have the side effect of making these functions compatible with
the backend, allowing it to be used on linux without linking libc.
2023-07-31 01:58:10 -04:00
Jacob Young
6f0a613b6f
Merge pull request #16611 from xxxbxxx/packed-struct
codegen: fix various packed struct issues

Closes #16609
Closes #15337
2023-07-29 23:49:14 -04:00
Evan Haas
9b5586ab7c add behavior test for vector bitcast at comptime
closes #8184
2023-07-29 18:08:47 -07:00
Jacob Young
dc8a80a191 llvm: support read-write output constraints in assembly
Closes #15227
2023-07-29 09:49:04 -07:00
AdamGoertz
796927b900
Allow zero-sized fields in extern structs (#16404)
This change allows the following types to appear in extern structs:
* Zero-bit integers
* void
* zero-sized structs and packed structs
* enums with zero-bit backing integers
* arrays of any length with zero-size elements
2023-07-29 12:45:01 -04:00
Xavier Bouchoux
8c367ef99a codegen: fix access to byte-aligned nested packed struct elems
When acessing a packed struct member via a byte aligned ptr (from the optimisation in Sema.structFieldPtrByIndex())
the codegen must apply the parent ptr packed_offset in addition to the field offset itself.

resolves https://github.com/ziglang/zig/issues/16609
2023-07-29 18:16:13 +02:00
Jacob Young
8d1805f81c behavior: add coverage for no longer reproducing issue
Closes #14305
2023-07-29 09:00:23 -07:00
Xavier Bouchoux
46abf20454 llvm: partial fix of store undefined to packed result location
prefer marking too few undefined bits, rather than too many that may overwrite nearby values.
partially resolves https://github.com/ziglang/zig/issues/15337
2023-07-29 11:56:27 +02:00
Jacob Young
125b453c58 llvm: fix SysV C abi for structs smaller than two eightbytes
Closes #16038
Closes #16288
2023-07-28 19:27:08 -04:00
Jacob Young
c80609dfec Sema: don't reorder tuple fields
This conflicts with anon structs which can be in-memory coercible but
are never reordered.

Closes #16242
2023-07-28 19:27:08 -04:00
Andrew Kelley
e66190025f frontend: make fn calls byval; fix false positive isNonErr
This commit does two things which seem unrelated at first, but,
together, solve a miscompilation, and potentially slightly speed up
compiler perf, at the expense of making #2765 trickier to implement in
the future.

Sema: avoid returning a false positive for whether an inferred error set
is comptime-known to be empty.

AstGen: mark function calls as not being interested in a result
location. This prevents the test case "ret_ptr doesn't cause own
inferred error set to be resolved" from being regressed. If we want to
accept and implement #2765 in the future, it will require solving this
problem a different way, but the principle of YAGNI tells us to go ahead
with this change.

Old ZIR looks like this:

  %97 = ret_ptr()
  %101 = store_node(%97, %100)
  %102 = load(%97)
  %103 = ret_is_non_err(%102)

New ZIR looks like this:

  %97 = ret_type()
  %101 = as_node(%97, %100)
  %102 = ret_is_non_err(%101)

closes #15669
2023-07-27 10:12:08 -07:00
Andrew Kelley
c3364b372f
Merge pull request #16572 from ziglang/stage1-test-coverage
add behavior test coverage for stage1 issues
2023-07-26 23:38:28 -07:00
Jacob Young
7201e69454 AstGen: fix missing deferred ref
Closes #16524
2023-07-26 22:19:30 -07:00
Andrew Kelley
d28f24d1d1 add behavior test for defer assign
closes #10591
2023-07-26 19:02:02 -07:00
Andrew Kelley
407d91f7a7 add behavior test for switch nested break
closes #10196
2023-07-26 19:02:02 -07:00
Andrew Kelley
9d3363fee9 add behavior test for bitcast packed struct twice
closes #9914
2023-07-26 19:02:02 -07:00
Andrew Kelley
508294e9be add behavior test for comptime ptrcast packed struct
closes #9912
2023-07-26 19:02:02 -07:00
Andrew Kelley
2936602566 add behavior test for union with 128 bit integer
closes #9871
2023-07-26 19:02:02 -07:00
Andrew Kelley
87961237cf add behavior test for tail calls
closes #9703
2023-07-26 19:02:02 -07:00
Andrew Kelley
00d6a4da49 add behavior test for comptime array load
closes #8487
2023-07-26 17:21:20 -07:00
Andrew Kelley
6cee98eb30 frontend: forbid packed and extern tuples 2023-07-25 21:45:33 -07:00
Xavier Bouchoux
5ab1854602 codegen/llvm: fix memset with vectors smaller than one byte
The special case to take advantage of llvm's intrinsic
was generating invalid llvm ir:

```
Invalid bitcast
  %60 = bitcast <2 x i2> %59 to i8, !dbg !3122
thread 145453 panic: LLVM module verification failed
```
2023-07-25 18:14:10 -07:00
Jacob G-W
3c08fe931a make @typeInfo not return private decls
fixes #10731
Thanks @nektro for previous work in #14878

This change creates a small breaking change:
It removes the `is_pub` field of a decl in `@typeInfo`
2023-07-25 16:19:08 -07:00
Andrew Kelley
cc6964c5dc InternPool: add func_coerced handling to funcIesResolved 2023-07-23 17:47:18 -07:00
Luuk de Gram
619140c0d2 wasm: correctly intcast signed integers
When a signed integer's bitsize is not 32 or 64, but the given
bitsize and wanted bitsize are either both represented by Wasm's i32
or i64, we must either sign extend or wrap the integer.
2023-07-22 02:12:07 +02:00