32172 Commits

Author SHA1 Message Date
Andrew Kelley
4f8a6b0888 wasm linker: implement data fixups
one hash table lookup per fixup
2025-01-15 15:11:36 -08:00
Andrew Kelley
5fac6f380e wasm linker: fix eliding empty data segments 2025-01-15 15:11:36 -08:00
Andrew Kelley
568d9936ab wasm codegen: fix call_indirect 2025-01-15 15:11:36 -08:00
Andrew Kelley
070b973c4a wasm linker: allow undefined imports when lib name is provided
and expose object_host_name as an option for setting the lib name for
object files, since the wasm linking standards don't specify a way to do
it.
2025-01-15 15:11:36 -08:00
Andrew Kelley
23d0882b54 wasm linker: handle extern functions in updateNav 2025-01-15 15:11:36 -08:00
Andrew Kelley
766284fec8 wasm linker: fix data segments memory flow
Recognize three distinct phases:
* before prelink ("object phase")
* after prelink, before flush ("zcu phase")
* during flush ("flush phase")

With this setup, we create data structures during the object phase, then
mutate them during the zcu phase, and then further mutate them during
the flush phase. In order to make the flush phase repeatable, the data
structures are copied just before starting the flush phase.

Further Zcu updates occur against the non-copied data structures.

What's not implemented is frontend garbage collection, in which case
some more changes will be needed in this linker logic to achieve a valid
state with data invariants intact.
2025-01-15 15:11:36 -08:00
Andrew Kelley
3cb00c5bcd std.ArrayHashMap: allow passing empty values array
in which case the values array is set to undefined
2025-01-15 15:11:35 -08:00
Andrew Kelley
91efc5c98b wasm linker: fix calling imported functions
and more disciplined type safety for output function indexes
2025-01-15 15:11:35 -08:00
Andrew Kelley
1a58ae2ed6 wasm codegen: fix extra index not relative 2025-01-15 15:11:35 -08:00
Andrew Kelley
3c70392210 wasm linker: add __zig_error_name_table data when needed 2025-01-15 15:11:35 -08:00
Andrew Kelley
85b53730fe add safety for calling functions that get virtual addrs 2025-01-15 15:11:35 -08:00
Andrew Kelley
2d899e9a9f wasm codegen: fix wrong union field for locals 2025-01-15 15:11:35 -08:00
Andrew Kelley
0630344894 fix calculation of nav alignment 2025-01-15 15:11:35 -08:00
Andrew Kelley
416fc2df94 complete wasm.Emit implementation 2025-01-15 15:11:35 -08:00
Andrew Kelley
458f658b42 wasm linker: implement missing logic
fix some compilation errors for reworked Emit now that it's actually
referenced

introduce DataSegment.Id for sorting data both from object files and
from the Zcu.

introduce optimization: data segment sorting includes a descending sort
on reference count so that references to data can be smaller integers
leading to better LEB encodings. this optimization is skipped for object
files.

implement uav address access function which is based on only 1 hash
table lookup to find out the offset after sorting.
2025-01-15 15:11:35 -08:00
Andrew Kelley
761387dc55 wasm linker: implement type index method 2025-01-15 15:11:35 -08:00
Andrew Kelley
70414c1f43 std.Thread: don't export wasi_thread_start in single-threaded mode 2025-01-15 15:11:35 -08:00
Andrew Kelley
0555fe8d5b fix replaceVecSectionHeader 2025-01-15 15:11:35 -08:00
Andrew Kelley
d6b42e585b wasm linker: implement name subsection
unlike the previous implementation, we can simply iterate an array.
2025-01-15 15:11:35 -08:00
Andrew Kelley
e80a203768 wasm linker fixes
* function resolution now links to zcu_funcs, not navs_exe/navs_obj
* updateFunc now adds things to output functions
* updateNav now handles function aliases correctly
* only report start symbol missing when it is unresolved
2025-01-15 15:11:35 -08:00
Andrew Kelley
4ecc4addc4 wasm codegen: remove dependency on PerThread where possible 2025-01-15 15:11:35 -08:00
Andrew Kelley
098e0b1906 wasm codegen: fix lowering of 32/64 float rt calls 2025-01-15 15:11:35 -08:00
Andrew Kelley
55773aee3f remove bad deinit 2025-01-15 15:11:35 -08:00
Andrew Kelley
4a4fb16c27 add dev env for wasm
with this I get 5s compilations
2025-01-15 15:11:35 -08:00
Andrew Kelley
7879d2cf41 cmake: remove deleted file 2025-01-15 15:11:35 -08:00
Andrew Kelley
edd592d371 fix compilation when enabling llvm 2025-01-15 15:11:35 -08:00
Andrew Kelley
968941b535 wasm linker: finish the flush function
This branch is passing type checking now.
2025-01-15 15:11:35 -08:00
Andrew Kelley
264a63b000 wasm linker: flush export section 2025-01-15 15:11:35 -08:00
Andrew Kelley
bf88059591 wasm linker: flush implemented up to the export section 2025-01-15 15:11:35 -08:00
Andrew Kelley
e21a42723b wasm linker: implement name, module name, and type for function imports 2025-01-15 15:11:35 -08:00
Andrew Kelley
c96e23632f frontend: add const to more Zcu pointers 2025-01-15 15:11:35 -08:00
Andrew Kelley
26c38b2d42 wasm linker: support export section as implicit symbols 2025-01-15 15:11:35 -08:00
Andrew Kelley
031c84c8cb wasm: fix many compilation errors
Still, the branch is not yet passing semantic analysis.
2025-01-15 15:11:35 -08:00
Andrew Kelley
b3ecdb21ee switch to ArrayListUnmanaged for machine code 2025-01-15 15:11:35 -08:00
Andrew Kelley
bf20a4aa9e wasm: use call_intrinsic MIR instruction 2025-01-15 15:11:35 -08:00
Andrew Kelley
c443a7a57f wasm: move error_name lowering to Emit phase 2025-01-15 15:11:35 -08:00
Andrew Kelley
d45e5ac5eb wasm codegen: rename func: CodeGen to cg: CodeGen 2025-01-15 15:11:35 -08:00
Andrew Kelley
4a1447d1db wasm codegen: switch on bool instead of int 2025-01-15 15:11:35 -08:00
Andrew Kelley
e24f635c75 wasm: implement errors_len as a MIR opcode with no linker involvement 2025-01-15 15:11:35 -08:00
Andrew Kelley
bffa148600 wasm codegen: fix some compilation errors 2025-01-15 15:11:35 -08:00
Andrew Kelley
e521879e47 rewrite wasm/Emit.zig
mainly, rework how relocations works. This is the point at which symbol
indexes are known - not before. And don't emit unnecessary relocations!
They're only needed when emitting an object file.

Changes wasm linker to keep MIR around long-lived so that fixups can be
reapplied after linker garbage collection.

use labeled switch while we're at it
2025-01-15 15:11:35 -08:00
Andrew Kelley
b9355edfb1 std.array_list: tiny refactor for pleasure 2025-01-15 15:11:35 -08:00
Andrew Kelley
943dac3e85 compiler: add type safety for export indices 2025-01-15 15:11:35 -08:00
Andrew Kelley
9bf715de74 rework error handling in the backends 2025-01-15 15:11:35 -08:00
Andrew Kelley
77accf597d elf linker: conform to explicit error sets 2025-01-15 15:11:35 -08:00
Andrew Kelley
da25ed95fc macho linker conforms to explicit error sets, again 2025-01-15 15:11:35 -08:00
Andrew Kelley
6235cc3da4 remove "FIXME" from codebase
See #363. Please file issues rather than making TODO comments.
2025-01-15 15:11:35 -08:00
Andrew Kelley
16180f525a macho linker: conform to explicit error sets
Makes linker functions have small error sets, required to report
diagnostics properly rather than having a massive error set that has a
lot of codes.

Other linker implementations are not ported yet.

Also the branch is not passing semantic analysis yet.
2025-01-15 15:11:35 -08:00
Andrew Kelley
795e7c64d5 wasm linker: aggressive DODification
The goals of this branch are to:
* compile faster when using the wasm linker and backend
* enable saving compiler state by directly copying in-memory linker
  state to disk.
* more efficient compiler memory utilization
* introduce integer type safety to wasm linker code
* generate better WebAssembly code
* fully participate in incremental compilation
* do as much work as possible outside of flush(), while continuing to do
  linker garbage collection.
* avoid unnecessary heap allocations
* avoid unnecessary indirect function calls

In order to accomplish this goals, this removes the ZigObject
abstraction, as well as Symbol and Atom. These abstractions resulted
in overly generic code, doing unnecessary work, and needless
complications that simply go away by creating a better in-memory data
model and emitting more things lazily.

For example, this makes wasm codegen emit MIR which is then lowered to
wasm code during linking, with optimal function indexes etc, or
relocations are emitted if outputting an object. Previously, this would
always emit relocations, which are fully unnecessary when emitting an
executable, and required all function calls to use the maximum size LEB
encoding.

This branch introduces the concept of the "prelink" phase which occurs
after all object files have been parsed, but before any Zcu updates are
sent to the linker. This allows the linker to fully parse all objects
into a compact memory model, which is guaranteed to be complete when Zcu
code is generated.

This commit is not a complete implementation of all these goals; it is
not even passing semantic analysis.
2025-01-15 15:11:35 -08:00
mlugg
77273103a8 print_value: fix crash on undefined slice ptr
Resolves: #22418
2025-01-15 21:13:51 +00:00