771 Commits

Author SHA1 Message Date
Andrew Kelley
f54471b54c compiler: miscellaneous branch progress
implement builtin.zig file population for all modules rather than
assuming there is only one global builtin.zig module.

move some fields from link.File to Compilation
move some fields from Module to Compilation

compute debug_format in global Compilation config resolution

wire up C compilation to the concept of owner modules

make whole cache mode call link.File.createEmpty() instead of
link.File.open()
2024-01-01 17:51:19 -07:00
Andrew Kelley
769dea6e37 Compilation: redo whole vs incremental logic in create and update 2024-01-01 17:51:19 -07:00
Andrew Kelley
33cdf33b95 compiler: update many references to bin_file.options 2024-01-01 17:51:19 -07:00
Andrew Kelley
bc4d2b646d compiler: update references to target 2024-01-01 17:51:19 -07:00
Andrew Kelley
1642c003b4 update libunwind references to bin_file.options 2024-01-01 17:51:19 -07:00
Andrew Kelley
f5ddef1e45 update references to module (to be renamed to zcu) 2024-01-01 17:51:19 -07:00
Andrew Kelley
41a8f5aec3 remove parent_compilation_link_libc 2024-01-01 17:51:19 -07:00
Andrew Kelley
0789e91eeb linkers: update references to "options" field 2024-01-01 17:51:19 -07:00
Andrew Kelley
3b6cb257df update image_base references 2024-01-01 17:51:19 -07:00
Andrew Kelley
9a48a5ab07 compiler: update references to single_threaded 2024-01-01 17:51:19 -07:00
Andrew Kelley
2bef0715c7 move a large chunk of linker logic away from "options"
These options are only supposed to be provided to the initialization
functions, resolved, and then computed values stored in the appropriate
place (base struct or the object-format-specific structs).

Many more to go...
2024-01-01 17:51:18 -07:00
Andrew Kelley
12de7e3472 WIP: move many global settings to become per-Module
Much of the logic from Compilation.create() is extracted into
Compilation.Config.resolve() which accepts many optional settings and
produces concrete settings. This separate step is needed by API users of
Compilation so that they can pass the resolved global settings to the
Module creation function, which itself needs to resolve per-Module
settings.

Since the target and other things are no longer global settings, I did
not want them stored in link.File (in the `options` field). That options
field was already a kludge; those options should be resolved into
concrete settings. This commit also starts to work on that, deleting
link.Options, moving the fields into Compilation and
ObjectFormat-specific structs instead. Some fields were ephemeral and
should not have been stored at all, such as symbol_size_hint.

The link.File object of Compilation is now a `?*link.File` and `null`
when -fno-emit-bin is passed. It is now arena-allocated along with
Compilation itself, avoiding some messy cleanup code that was there
before.

On the command line, it is now possible to configure the standard
library itself by using `--mod std` just like any other module. This
meant that the CLI needed to create the standard library module rather
than having Compilation create it.

There are a lot of changes in this commit and it's still not done. I
didn't realize how quickly this changeset was going to balloon out of
control, and there are still many lines that need to be changed before
it even compiles successfully.

* introduce std.Build.Cache.HashHelper.oneShot
* add error_tracing to std.Build.Module
* extract build.zig file generation into src/Builtin.zig
* each CSourceFile and RcSourceFile now has a Module owner, which
  determines some of the C compiler flags.
2024-01-01 17:51:18 -07:00
Andrew Kelley
77420af9d0 compiler: get the dynamic linker from the target
instead of passing it to Compilation separately and storing it
separately in the linker options.
2024-01-01 17:51:18 -07:00
Andrew Kelley
dbdb87502d std.Target: add DynamicLinker 2024-01-01 17:51:18 -07:00
Andrew Kelley
142471fcc4 zig build system: change target, compilation, and module APIs
Introduce the concept of "target query" and "resolved target". A target
query is what the user specifies, with some things left to default. A
resolved target has the default things discovered and populated.
In the future, std.zig.CrossTarget will be rename to std.Target.Query.
Introduces `std.Build.resolveTargetQuery` to get from one to the other.

The concept of `main_mod_path` is gone, no longer supported. You have to
put the root source file at the module root now.

* remove deprecated API
* update build.zig for the breaking API changes in this branch
* move std.Build.Step.Compile.BuildId to std.zig.BuildId
* add more options to std.Build.ExecutableOptions, std.Build.ObjectOptions,
  std.Build.SharedLibraryOptions, std.Build.StaticLibraryOptions, and
  std.Build.TestOptions.
* remove `std.Build.constructCMacro`. There is no use for this API.
* deprecate `std.Build.Step.Compile.defineCMacro`. Instead,
  `std.Build.Module.addCMacro` is provided.
  - remove `std.Build.Step.Compile.defineCMacroRaw`.
* deprecate `std.Build.Step.Compile.linkFrameworkNeeded`
  - use `std.Build.Module.linkFramework`
* deprecate `std.Build.Step.Compile.linkFrameworkWeak`
  - use `std.Build.Module.linkFramework`
* move more logic into `std.Build.Module`
* allow `target` and `optimize` to be `null` when creating a Module.
  Along with other fields, those unspecified options will be inherited
  from parent `Module` when inserted into an import table.
* the `target` field of `addExecutable` is now required. pass `b.host`
  to get the host target.
2024-01-01 17:51:18 -07:00
Jacob Young
3f2a65594e Compilation: cleanup hashmap usage 2024-01-01 13:38:30 -08:00
Andrew Kelley
225fe6ddbf Compilation: remove parent_compilation_link_libc
This option is not needed since the link_libc flag can be set directly
when creating compiler_rt.

This fixes a problem where an immutable flag was being mutated in Sema.
2023-12-13 11:28:42 -08:00
Meghan Denny
2549de80b2 move Module.Decl.Index and Module.Namespace.Index to InternPool 2023-11-26 02:24:40 -05:00
Andrew Kelley
7103088e4a
Merge pull request #18105 from Vexu/translate-c
Use Aro's tokenizer in `translate-c`
2023-11-26 02:22:51 -05:00
Veikka Tuominen
74010fecc7 translate-c: use Aro's tokenizer 2023-11-25 12:28:19 +02:00
Robin Voetter
b4b1c4df64
spirv: add -fstructured-cfg option
This enables the compiler to generate a structured cfg even in opencl,
even if it is not strictly required by the SPIR-V Kernel specification.
2023-11-24 11:40:18 +01:00
mlugg
b355893438
compiler: correct unnecessary uses of 'var' 2023-11-19 11:11:49 +00:00
Michael Dusan
569182dbb2 compilation: forbid PIE for dynamic libraries
but allow for { .exe, .o, .a }.

closes #17928
2023-11-13 20:30:57 +01:00
Andrew Kelley
91b897ef58 rework memory management of Module.Namespace hash maps
The motivating problem here was a memory leak in the hash maps of
Module.Namespace.

The commit deletes more of the legacy incremental compilation
implementation. It had things like use of orderedRemove and trying to do
too much OOP-style creation and deletion of objects.

Instead, this commit iterates over all the namespaces on Module deinit
and calls deinit on the hash map fields. This logic is much simpler to
reason about.

Similarly, change global inline assembly to an array hash map since
iterating over the values is a primary use of it, and clean up the
remaining values on Module deinit, solving another memory leak.

After this there are no more memory leaks remaining when using the
x86 backend in a libc-less compiler.
2023-11-12 23:21:21 -05:00
Andrew Kelley
53f74d6a04 move libssp into libcompiler_rt
closes #7265
2023-11-10 13:12:10 -07:00
Andrew Kelley
24b020d9f6 Compilation: fix logic regarding needs_c_symbols 2023-11-10 13:12:00 -07:00
Jacob Young
10f4486c0b Compilation: forward clang diagnostics to error bundles 2023-11-09 16:43:12 -05:00
Andrew Kelley
77bc8e7b67
Merge pull request #17771 from ehaas/mingw-aro
mingw: Use aro instead of clang for preprocessing import libs
2023-11-07 14:47:05 -05:00
Jakub Konka
bf0387b6bb
Merge pull request #17873 from ziglang/elf-archive
elf: implement archiving input object files
2023-11-07 03:22:14 +01:00
Jakub Konka
281dabaa88 Compilation: unconditionally close open file handles for writable dance 2023-11-06 13:47:39 +01:00
Andrew Kelley
e74ced21b7 frontend: fix -fsingle-threaded default detection logic
The logic in 509be7cf1f10c5d329d2b0524f2af6bfcabd52de assumed that
`use_llvm` meant that the LLVM backend would be used, however, use_llvm
is false when there are no zig files to compile, which is the case for
zig cc. This logic resulted in `-fsingle-threaded` which made libc++
fail to compile for C++ code that includes the threading abstractions
(such as LLVM).
2023-11-06 01:16:09 -05:00
Jakub Konka
f24ceec35a
Merge pull request #17844 from ziglang/elf-object
elf: handle emitting relocatables and static libraries - humble beginnings
2023-11-04 20:58:15 +01:00
Jakub Konka
7a186d9eb6 Compilation: take into account if LLVM is available in lib-building logic 2023-11-04 16:09:52 +01:00
Andrew Kelley
a333ac846d Compilation: refactor logic for library-building capabilities
And mark the stage2_x86_64 backend as being capable of building
compiler-rt and zig libc.
2023-11-04 09:12:54 +01:00
Jacob Young
509be7cf1f x86_64: fix std test failures 2023-11-03 23:18:21 -04:00
Andrew Kelley
1ccc68f307 frontend: rip out Decl dependencies
This incremental compilation logic will need to be reworked so that it
does not depend on buried pointers - that is, long-lived pointers that
are owned by non-top-level objects such as Decl.

In the meantime, this fixes memory leaks since the memory management of
these dependencies has bitrotted.
2023-11-03 21:24:13 -04:00
Evan Haas
3eb8a094c0 comp: remove CBE guards for aro 2023-11-03 22:18:40 +02:00
Andrew Kelley
256ab68a97 frontend: make Decl.zir_decl_index typed
This field had the wrong type. It's not a `Zir.Inst.Index`, it's
actually a `Zir.OptionalExtraIndex`. Also, the former is currently
aliased to `u32` while the latter is a nonexhaustive enum that gives us
more type checking.

This commit is preparation for making this field non-optional. Now it
can be changed to `Zir.ExtraIndex` and then the compiler will point out
all the places that the non-optional assumption is being violated.
2023-10-28 04:30:27 -04:00
Jacob Young
51f7e5412a cbe: update DeclGen.decl_index to support anon decls 2023-10-25 19:57:02 -07:00
Andrew Kelley
411fcd22ab frontend: rework @embedFile for incremental compilation
This feature was made to work with the legacy incremental compilation
mechanism which is being reworked.

This commit regresses the ability to update files used with `@embedFile`
while the compiler is running.

In exchange, we get these benefits:
* The embedded file contents are copied directly into InternPool rather
  than there being an extra allocation and memcpy.
* The EmbedFile struct, which represents a long-lived object, is made
  more serialization friendly.
* Eliminate the creation and use of a Decl as an anonymous decl.

When implementing the new incremental compilation mechanism,
functionality will need to be added back for handling `@embedFile`.
2023-10-23 17:24:58 -07:00
Andrew Kelley
94d61ce964
Merge pull request #17651 from Vexu/error-limit
Make distinct error limit configurable (attempt #2)
2023-10-23 03:19:03 -04:00
Andrew Kelley
7bab406c79 InternPool: store alignment of anon decls
Commit 5393e56500d499753dbc39704c0161b47d1e4d5c has a flaw pointed out
by @mlugg: the `ty` field of pointer values changes when comptime values
are pointer-casted. This commit introduces a new encoding which
additionally stores the "original pointer type" which is used to store
the alignment of the anonymous decl, and potentially other information
in the future such as section and pointer address space. However, this
new encoding is only used when the original pointer type differs from
the casted pointer type in a meaningful way.

I was able to make the LLVM backend and the C backend lower anonymous
decls with the appropriate alignment, however I will need some help
figuring out how to do this for the backends that lower anonymous decls
via src/codegen.zig and the wasm backend.
2023-10-21 21:38:41 -04:00
Veikka Tuominen
bf61c5c065 make distinct error limit configurable
Closes #786
2023-10-21 12:36:29 +03:00
Andrew Kelley
328ec15d9c Revert "make distinct error limit configurable"
This reverts commit 78855bd21866b515018259a2194e036e4b3120df.

This commit did not replace uses of `Type.err_int` of which there are
currently 60 uses.

Re-opens #786
2023-10-20 14:03:55 -07:00
Andrew Kelley
a361f37b1c
Merge pull request #17608 from squeek502/resinator-fixes
resinator: Fix `INCLUDE` var handling and sync with upstream
2023-10-20 03:49:14 -04:00
Michael Dusan
aa76ca2931 llvm: set PIE only for executables
closes #17575
2023-10-20 01:59:55 -04:00
Andrew Kelley
ae2cd5fe26 Revert "Never implicitly add rpaths for each lib dir, add NixOS libdir to rpath"
This reverts commit d7b73af8f65bb891c8700ed47777144bb6f35fe1.

I did not look at this closely enough. This is incorrect; it should not
implicitly add rpaths for every library, and it should not disable the
nice default of each_lib_path when compiling for the native OS.

See #16062 where we are working on a follow-up improvement to this.
2023-10-18 23:37:47 -07:00
Ryan Liptak
8ec04b567e Error if an .rc file uses the 'preprocess only' or 'no preprocess' flags 2023-10-18 18:30:32 -07:00
Ryan Liptak
81a61c8ecd Sync resinator with upstream and fix INCLUDE env var handling
The INCLUDE variable being used during `.rc` preprocessing was an accidental regression in https://github.com/ziglang/zig/pull/17412.
Closes #17585.

resinator changes:
source_mapping: Protect against NUL bytes in #line filenames
lex: Avoid recalculating column on every tab stop within string literals
Proper error handling for failing to open cwd instead of `catch unreachable`
Use platform-specific delimiter for INCLUDE env var parsing
2023-10-18 18:30:32 -07:00
Andrew Kelley
ec21da0d51 compiler: fix LTO availability logic
Before this commit, the logic would fail with a "LTO not available"
error if the user set `-fno-lto` which doesn't make sense. This commit
corrects the logic to understand when the user is explicitly requesting
to turn LTO off.
2023-10-18 17:47:57 -04:00