1013 Commits

Author SHA1 Message Date
Alex Rønne Petersen
3a2647b7d3
glibc: Don't build CRT objects that won't be used. 2024-11-03 09:29:33 +01:00
Alex Rønne Petersen
4c70aea460
Compilation: Use the regular module mechanism for setting PIC on CRT objects.
addCCArgs() will then pass the appropriate flag to Clang.
2024-11-03 09:29:33 +01:00
Alex Rønne Petersen
39abcc303c
Compilation: Pass -fno-PIC to clang if PIC is disabled.
Let's not implicitly rely on whatever Clang's default is.
2024-11-03 09:29:33 +01:00
Alex Rønne Petersen
8abbdb56a4
Merge pull request #21729 from alexrp/target-cpu-baseline
`std.Target.Cpu.Model`: Further refinements to `generic()` and `baseline()`
2024-11-02 10:27:03 +01:00
mlugg
d11bbde5f9
compiler: remove anonymous struct types, unify all tuples
This commit reworks how anonymous struct literals and tuples work.

Previously, an untyped anonymous struct literal
(e.g. `const x = .{ .a = 123 }`) was given an "anonymous struct type",
which is a special kind of struct which coerces using structural
equivalence. This mechanism was a holdover from before we used
RLS / result types as the primary mechanism of type inference. This
commit changes the language so that the type assigned here is a "normal"
struct type. It uses a form of equivalence based on the AST node and the
type's structure, much like a reified (`@Type`) type.

Additionally, tuples have been simplified. The distinction between
"simple" and "complex" tuple types is eliminated. All tuples, even those
explicitly declared using `struct { ... }` syntax, use structural
equivalence, and do not undergo staged type resolution. Tuples are very
restricted: they cannot have non-`auto` layouts, cannot have aligned
fields, and cannot have default values with the exception of `comptime`
fields. Tuples currently do not have optimized layout, but this can be
changed in the future.

This change simplifies the language, and fixes some problematic
coercions through pointers which led to unintuitive behavior.

Resolves: #16865
2024-10-31 20:42:53 +00:00
Alex Rønne Petersen
4b78dbe068
Compilation: Omit Clang CPU model flags for some targets. 2024-10-26 21:59:58 +02:00
David Rubin
0d83304228
remove leak from linker 2024-10-24 23:23:15 -07:00
Andrew Kelley
4bdc2d3871 avoid unnecessarily building Scrt1.o when cross-compiling glibc
which, in this branch causes a miscompilation because it would get sent
to the linker.
2024-10-23 16:27:39 -07:00
Andrew Kelley
ba71079837 combine codegen work queue and linker task queue
these tasks have some shared data dependencies so they cannot be done
simultaneously. Future work should untangle these data dependencies so
that more can be done in parallel.

for now this commit ensures correctness by making linker input parsing
and codegen tasks part of the same queue.
2024-10-23 16:27:39 -07:00
Andrew Kelley
9a511b4b27 work on compiler_rt and fuzzer libs earlier in the pipeline
don't wait for AstGen and C source files to complete before starting to
build compiler_rt and libfuzzer
2024-10-23 16:27:39 -07:00
Andrew Kelley
e0623df9f7 Compilation.create: skip libc_installation on darwin
If the "is darwin" check is moved below the libc_installation check
below, error.LibCInstallationMissingCrtDir is returned from
lci.resolveCrtPaths().

This should be revisited because it makes sense to check
libc_installation first even on darwin.

Anyway for now this more closely matches logic from master branch.
2024-10-23 16:27:39 -07:00
Andrew Kelley
f2efe05155 use deterministic order in relocatable-eh-frame tests
This test does not pass in master branch either if you flip the object
order around.
2024-10-23 16:27:39 -07:00
Andrew Kelley
5d75d8f6fc also find static libc files on the host
and don't look for glibc files on windows
2024-10-23 16:27:39 -07:00
Andrew Kelley
ab33d2e7a9 add missing check for output mode in tsan logic 2024-10-23 16:27:38 -07:00
Andrew Kelley
d1ecb742ec don't create unused musl crt objects 2024-10-23 16:27:38 -07:00
Andrew Kelley
c2898c436f branch fixes 2024-10-23 16:27:38 -07:00
Andrew Kelley
5ca54036ca move linker input file parsing to the compilation pipeline 2024-10-23 16:27:38 -07:00
Andrew Kelley
b510c5719f refactor Compilation.crtFilePath 2024-10-23 16:27:38 -07:00
Andrew Kelley
e567abb339 rework linker inputs
* Compilation.objects changes to Compilation.link_inputs which stores
  objects, archives, windows resources, shared objects, and strings
  intended to be put directly into the dynamic section. Order is now
  preserved between all of these kinds of linker inputs. If it is
  determined the order does not matter for a particular kind of linker
  input, that item should be moved to a different array.
* rename system_libs to windows_libs
* untangle library lookup from CLI types
* when doing library lookup, instead of using access syscalls, go ahead
  and open the files and keep the handles around for passing to the
  cache system and the linker.
* during library lookup and cache file hashing, use positioned reads to
  avoid affecting the file seek position.
* library directories are opened in the CLI and converted to Directory
  objects, warnings emitted for those that cannot be opened.
2024-10-23 16:27:38 -07:00
Andrew Kelley
5b016e290a move ld script processing to the frontend
along with the relevant logic, making the libraries within subject to
the same search criteria as all the other libraries.

this unfortunately means doing file system access on all .so files when
targeting ELF to determine if they are linker scripts, however, I have a
plan to address this.
2024-10-23 16:27:38 -07:00
Alex Rønne Petersen
549a7eba40
std.Target: Rename OS version range functions to drop the "get" prefix. 2024-10-16 22:25:29 +02:00
mlugg
22539783ad
incremental: introduce file dependencies to handle AstGen failures
The re-analysis here is a little coarse; it'd be nice in the future to
have a way for an AstGen failure to preserve *all* analysis which
depends on the last success, and just hide the compile errors which
depend on it somehow. But I'm not sure how we'd achieve that, so this
works fine for now.

Resolves: #21223
2024-10-16 16:30:36 +01:00
mlugg
c6842b58d4
Zcu: cache output of resolveReferences between calls
This not only simplifies the error bundling logic, but also improves
efficiency by allowing the result to be cached between, for instance,
multiple calls to `totalErrorCount`.
2024-10-16 16:30:36 +01:00
Andrew Kelley
1ba3fc90be link.Elf: eliminate an O(N^2) algorithm in flush()
Make shared_objects a StringArrayHashMap so that deduping does not
need to happen in flush. That deduping code also was using an O(N^2)
algorithm, which is not allowed in this codebase. There is another
violation of this rule in resolveSymbols but this commit does not
address it.

This required reworking shared object parsing, breaking it into
independent components so that we could access soname earlier.

Shared object parsing had a few problems that I noticed and fixed in
this commit:
* Many instances of incorrect use of align(1).
* `shnum * @sizeOf(elf.Elf64_Shdr)` can overflow based on user data.
* `@divExact` can cause illegal behavior based on user data.
* Strange versyms logic that wasn't present in mold nor lld. The logic
  was not commented and there is no git blame information in ziglang/zig
  nor kubkon/zld. I changed it to match mold and lld instead.
* Use of ArrayList for slices of memory that are never resized.
* finding DT_VERDEFNUM in a different loop than finding DT_SONAME.
  Ultimately I think we should follow mold's lead and ignore this
  integer, relying on null termination instead.
* Doing logic based on VER_FLG_BASE rather than ignoring it like mold
  and LLD do. No comment explaining why the behavior is different.
* Mutating the original ELF symbols rather than only storing the mangled
  name on the new Symbol struct.

I noticed something that I didn't try to address in this commit: Symbol
stores a lot of redundant information that is already present in the ELF
symbols. I suspect that the codebase could benefit from reworking Symbol
to not store redundant information.

Additionally:
* Add some type safety to std.elf.
* Eliminate 1-3 file system reads for determining the kind of input
  files, by taking advantage of file name extension and handling error
  codes properly.
* Move more error handling methods to link.Diags and make them
  infallible and thread-safe
* Make the data dependencies obvious in the parameters of
  parseSharedObject. It's now clear that the first two steps (Header and
  Parsed) can be done during the main Compilation pipeline, rather than
  waiting for flush().
2024-10-12 10:44:17 -07:00
Andrew Kelley
13fb68c064 link: consolidate diagnostics
By organizing linker diagnostics into this struct, it becomes possible
to share more code between linker backends, and more importantly it
becomes possible to pass only the Diag struct to some functions, rather
than passing the entire linker state object in. This makes data
dependencies more obvious, making it easier to rearrange code and to
multithread.

Also fix MachO code abusing an atomic variable. Not only was it using
the wrong atomic operation, it is unnecessary additional state since
the state is already being protected by a mutex.
2024-10-11 10:36:19 -07:00
Andrew Kelley
4000685557 Compilation: don't write cache manifest on failure
When errors occurred during flush(), incremental cache mode was still
writing a successful cache manifest, making subsequent compilations fail
because they would get a cache hit only to find invalid data.
2024-10-10 14:21:52 -07:00
Andrew Kelley
14c8e270bb link: fix false positive crtbegin/crtend detection
Embrace the Path abstraction, doing more operations based on directory
handles rather than absolute file paths. Most of the diff noise here
comes from this one.

Fix sorting of crtbegin/crtend atoms. Previously it would look at all
path components for those strings.

Make the C runtime path detection partially a pure function, and move
some logic to glibc.zig where it belongs.
2024-10-10 14:21:52 -07:00
Andrew Kelley
31d70cb1e1 link.Elf: avoid needless file system reads in flush()
flush() must not do anything more than necessary. Determining the type
of input files must be done only once, before flush. Fortunately, we
don't even need any file system accesses to do this since that
information is statically known in most cases, and in the rest of the
cases can be determined by file extension alone.

This commit also updates the nearby code to conform to the convention
for error handling where there is exactly one error code to represent
the fact that error messages have already been emitted. This had the
side effect of improving the error message for a linker script parse
error.

"positionals" is not a linker concept; it is a command line interface
concept. Zig's linker implementation should not mention "positionals".
This commit deletes that array list in favor of directly making function
calls, eliminating that heap allocation during flush().
2024-10-08 18:02:59 -07:00
Alex Rønne Petersen
9b273f6b9a
Merge pull request #21570 from alexrp/windows-itanium
Initial port work for `*-windows-itanium` support.
2024-10-03 21:53:22 +02:00
Alex Rønne Petersen
537a873b17
Initial port work for *-windows-itanium support.
https://llvm.org/docs/HowToBuildWindowsItaniumPrograms.html

This is a weird middle ground between `*-windows-gnu` and `*-windows-msvc`. It
uses the C++ ABI of the former while using the system libraries of the latter.
2024-09-28 21:43:52 +02:00
Andrew Kelley
4f8d244e7e remove formatted panics
implements #17969
2024-09-26 12:35:14 -07:00
Alex Rønne Petersen
0082475ddb Compilation: Synchronize some libcxx CXXFLAGS with upstream. 2024-09-19 18:20:21 -07:00
Linus Groh
8588964972 Replace deprecated default initializations with decl literals 2024-09-12 16:01:23 +01:00
Alex Rønne Petersen
f9455511fa
Compilation: Work around llvm/llvm-project#105972 by defining the macros.
https://github.com/llvm/llvm-project/issues/105972
2024-09-10 08:53:30 +02:00
Alex Rønne Petersen
77c8f4b671
Compilation: Pass hard/soft float flags to Clang as appropriate. 2024-09-10 08:53:30 +02:00
Alex Rønne Petersen
2de7296262
Compilation: Pass -mthumb for assembly files too, not just C files. 2024-08-31 03:38:07 +02:00
Alex Rønne Petersen
f10b226c77
Compilation: Pass -fPIC for assembly files too, not just C files.
There are targets (e.g. MIPS) where PIC actually affects assembler behavior.
2024-08-31 03:38:06 +02:00
mlugg
0fe3fd01dd
std: update std.builtin.Type fields to follow naming conventions
The compiler actually doesn't need any functional changes for this: Sema
does reification based on the tag indices of `std.builtin.Type` already!
So, no zig1.wasm update is necessary.

This change is necessary to disallow name clashes between fields and
decls on a type, which is a prerequisite of #9938.
2024-08-28 08:39:59 +01:00
mlugg
6808ce27bd
compiler,lib,test,langref: migrate @setCold to @branchHint 2024-08-27 00:44:35 +01:00
David Rubin
f777b29832
fix up merge conflicts with master 2024-08-25 22:43:57 -07:00
David Rubin
1c1feba08e
remove mod aliases for Zcus 2024-08-25 15:17:40 -07:00
David Rubin
889a324e77
comp: remove an, unencouraged... pattern 2024-08-25 15:17:23 -07:00
David Rubin
863f74dcd2
comp: rename module to zcu 2024-08-25 15:17:21 -07:00
Jacob Young
78dec0a3da Compilation: fix crash saving incremental state 2024-08-20 08:09:33 -04:00
Robin Voetter
b4343074d2
replace Compilation.Emit with std.Build.Cache.Path
This type is exactly the same as std.Build.Cache.Path, except for
one function which is not used anymore. Therefore we can replace
it without consequences.
2024-08-19 19:09:12 +02:00
Robin Voetter
43f73af359
fix various issues related to Path handling in the compiler and std
A compilation build step for which the binary is not required could not
be compiled previously. There were 2 issues that caused this:

- The compiler communicated only the results of the emitted binary and
  did not properly communicate the result if the binary was not emitted.

  This is fixed by communicating the final hash of the artifact path (the
  hash of the corresponding /o/<hash> directory) and communicating this
  instead of the entire path. This changes the zig build --listen protocol
  to communicate hashes instead of paths, and emit_bin_path is accordingly
  renamed to emit_digest.

- There was an error related to the default llvm object path when
  CacheUse.Whole was selected. I'm not really sure why this didn't manifest
  when the binary is also emitted.

  This was fixed by improving the path handling related to flush() and
  emitLlvmObject().

In general, this commit also improves some of the path handling throughout
the compiler and standard library.
2024-08-19 19:09:11 +02:00
mlugg
f0374fe3f0 Compilation: simplify totalErrorCount
This function now has to allocate anyway to resolve references, so we
may as well just build the error bundle and check its length.

Also remove some unnecessary calls of this function for efficiency.
2024-08-17 18:50:10 -04:00
mlugg
7f2466e65f std.BoundedArray: add clear() 2024-08-17 18:50:10 -04:00
mlugg
90116d92b0 Compilation: don't call resolveReferences unnecessarily
This function is slow and should only be called in compile error cases.
2024-08-17 18:50:10 -04:00
mlugg
84c2ebd6c6 frontend: incremental compilation progress
Another big commit, sorry! This commit makes all fixes necessary for
incremental updates of the compiler itself (specifically, adding a
breakpoint to `zirCompileLog`) to succeed, at least on the frontend.

The biggest change here is a reform to how types are handled. It works
like this:
* When a type is first created in `zirStructDecl` etc, its namespace is
  scanned. If the type requires resolution, an `interned` dependency is
  declared for the containing `AnalUnit`.
* `zirThis` also declared an `interned` dependency for its `AnalUnit` on
  the namespace's owner type.
* If the type's namespace changes, the surrounding source declaration
  changes hash, so `zirStructDecl` etc will be hit again. We check
  whether the namespace has been scanned this generation, and re-scan it
  if not.
* Namespace lookups also check whether the namespace in question
  requires a re-scan based on the generation. This is because there's no
  guarantee that the `zirStructDecl` is re-analyzed before the namespace
  lookup is re-analyzed.
* If a type's structure (essentially its fields) change, then the type's
  `Cau` is considered outdated. When the type is re-analyzed due to
  being outdated, or the `zirStructDecl` is re-analyzed by being
  transitively outdated, or a corresponding `zirThis` is re-analyzed by
  being transitively outdated, the struct type is recreated at a new
  `InternPool` index. The namespace's owner is updated (but not
  re-scanned, since that is handled by the mechanisms above), and the
  old type, while remaining a valid `Index`, is removed from the map
  metadata so it will never be found by lookups. `zirStructDecl` and
  `zirThis` store an `interned` dependency on the *new* type.
2024-08-17 18:50:10 -04:00