12751 Commits

Author SHA1 Message Date
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
50ef10eb49
Sema: add missing compile error for runtime-known const with comptime-only type
When RLS is used to initialize a value with a comptime-only type, the
usual "value with comptime-only type depends on runtime control flow"
error message isn't hit, because we don't use results from a block. When
we reach `make_ptr_const`, we must validate that the value is
comptime-known.
2023-09-15 14:29:57 +01: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
Andrew Kelley
8592c5cdac compiler: rework capture scopes in-memory layout
* Use 32-bit integers instead of pointers for compactness and
  serialization friendliness.
* Use a separate hash map for runtime and comptime capture scopes,
  avoiding the 1-bit union tag.
* Use a compact array representation instead of a tree of hash maps.
* Eliminate the only instance of ref-counting in the compiler, instead
  relying on garbage collection (not implemented yet but is the plan for
  almost all long-lived objects related to incremental compilation).

Because a code modification may need to access capture scope data, this
makes capture scope data long-lived state. My goal is to get incremental
compilation state serialization down to a single pwritev syscall, by
unifying the on-disk representation with the in-memory representation.
This commit eliminates the last remaining pointer field of
`Module.Decl`.
2023-09-15 00:55:07 -07:00
Ryan Liptak
30e1883834 Add -includes option to zig libc
Prints the detected libc include paths for the target and exits
2023-09-14 11:04:34 -07:00
Jakub Konka
fc86b80b3b elf: correctly handle overflows on non-64bit hosts 2023-09-13 22:38:44 +02:00
Jakub Konka
d4c1e85a13 elf: skip writing non-alloc and zerofill atoms 2023-09-13 21:51:43 +02:00
Jakub Konka
a9f1b994bd elf: allocate locals and globals in objects 2023-09-13 21:51:43 +02:00
Jakub Konka
d37cb60621 elf: re-enable linking compiler_rt 2023-09-13 21:51:43 +02:00
Jakub Konka
ce88df497c elf: do not store Symbol's index in Symbol 2023-09-13 21:51:43 +02:00
Jakub Konka
dbde746f9d elf: parse archives 2023-09-13 21:51:43 +02:00
Jakub Konka
5eef7577d1 elf: handle more relocs with GOT relaxation 2023-09-13 21:51:43 +02:00
Jakub Konka
7a16a97671 x86_64: add simple disassembler interface to the encoder 2023-09-13 21:51:43 +02:00
Jakub Konka
9de0df76a8 elf: allocate .bss section and matching PHDR 2023-09-13 21:51:43 +02:00
Jakub Konka
0d924d2da6 elf: look for entry point globally if not set by incremental compiler 2023-09-13 12:33:51 +02:00
Jakub Konka
31f363d51f elf: enable linker for non-incremental code paths 2023-09-13 12:17:02 +02:00
Jakub Konka
4d29b39678
Merge pull request #17113 from ziglang/elf-linker
elf: upstream zld/ELF functionality, part 1
2023-09-13 10:07:07 +02:00
Andrew Kelley
cb6201715a InternPool: prevent anon struct UAF bugs with type safety
Instead of using actual slices for InternPool.Key.AnonStructType, this
commit changes to use Slice types instead, which store a
long-lived index rather than a pointer.

This is a follow-up to 7ef1eb1c27754cb0349fdc10db1f02ff2dddd99b.
2023-09-12 20:08:56 -04:00
Jakub Konka
6910a50ae5 elf: add u64 to usize casts where required 2023-09-13 00:31:41 +02:00
Jakub Konka
1a6d12ea92 elf: clean up and unify symbol ref handling in relocs
Also, this lets us re-enable proper undefined symbols tracking.
2023-09-12 23:27:14 +02:00
Jakub Konka
9719fa7412 elf: include C compilation artifacts on the linker line 2023-09-12 19:26:51 +02:00
Jakub Konka
ae74a36af0 elf: resolve and write objects to file 2023-09-12 19:17:57 +02:00
Jakub Konka
652ebf3b6a elf: allocate objects, currently atom-by-atom 2023-09-12 18:07:10 +02:00
Jakub Konka
9db472cff6 elf: set output section index of a global when resolving 2023-09-12 17:52:55 +02:00
Jakub Konka
472d326a8c elf: set output section index when parsing objects 2023-09-12 17:35:56 +02:00
Jakub Konka
44e84af874 elf: add simplistic reloc scanning mechanism 2023-09-12 16:32:55 +02:00
Jakub Konka
c654f3b0ee elf: claim unresolved dangling symbols that can be claimed 2023-09-12 15:44:16 +02:00
Jakub Konka
b478a0dd1a elf: mark imports-exports; populate symtab with objects 2023-09-12 15:14:38 +02:00
Jakub Konka
962b46148d elf: add simplistic symbol resolution 2023-09-12 14:36:55 +02:00
Jakub Konka
53c3757c00 elf: start fixing symbol resolution 2023-09-12 14:02:15 +02:00
Jakub Konka
00787885f4 elf: report undefined symbols in objects 2023-09-12 00:10:54 +02:00
Jakub Konka
67d458370d elf: add prelim impl of Object parsing 2023-09-11 22:49:42 +02:00
Jakub Konka
3df58a9583 elf: add basic error reporting for positional parsing 2023-09-11 16:32:58 +02:00
Jakub Konka
65b9597c07 elf: report undefined symbols as errors 2023-09-11 16:02:21 +02:00
Jakub Konka
7a9eba2f85 elf: emit relocation to an extern function 2023-09-11 10:52:30 +02:00
Jakub Konka
d07edfabd6 elf: simplify handling of relocs for atoms 2023-09-11 07:59:54 +02:00
Jakub Konka
975cb235cc
Merge pull request #17081 from Techatrix/wasm-float-op
wasm: implement more float operations on f80 and f128
2023-09-11 07:59:30 +02:00
Jakub Konka
4b082d89c9 elf: decouple notion of atom free list from shdrs
Not every section will have the need for such a free list.
2023-09-10 21:58:45 +02:00
Techatrix
ec7f88945b wasm: implement more math operations on 128 bit integers
these operations are required to be able to print floats
2023-09-10 15:59:02 +02:00
Techatrix
9d6b6bddb6 wasm: implement common conversions between integers/floats with bitsize larger than 64 bits 2023-09-10 15:59:02 +02:00
Techatrix
55694c2a4d wasm: implement comparison on f80 and f128 2023-09-10 15:59:02 +02:00
Techatrix
673ebfabd1 wasm: implement negation on f80 and f128 2023-09-10 15:40:52 +02:00
Jakub Konka
a3ce011408 elf: allocate linker defined symbols 2023-09-10 11:10:16 +02:00
Jakub Konka
1c3fd16c37 elf: write linker-defined symbols to symtab 2023-09-10 09:10:00 +02:00
Jakub Konka
a455b5692a elf: create required linker-defined symbols 2023-09-10 08:41:13 +02:00
Jakub Konka
a6e9163284 elf: clean up naming and remove unused functions 2023-09-09 16:03:39 +02:00
Jakub Konka
a3372050b1 elf: rename program_headers to phdrs 2023-09-08 23:21:44 +02:00
Jakub Konka
465431807b elf: write $got symbols into the symtab 2023-09-08 22:53:53 +02:00
Jakub Konka
69738a07c2 elf: store Index rather than ?Index in Atom; gen ABS STT_FILE for zig source 2023-09-08 21:09:45 +02:00
Jakub Konka
6ad5db030c elf: store GOT index in symbol extra array; use GotSection for GOT 2023-09-08 18:12:53 +02:00