276 Commits

Author SHA1 Message Date
Jacob G-W
72bb6bb143 plan9 linker: produce an object file that can actually work!!! 2021-07-08 14:10:49 -07:00
Takeshi Yoneda
5c34c01179 WASI: include exec-model in cache state.
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2021-07-03 11:57:44 +03:00
Andrew Kelley
d979dd9b58 stage2: improve AstGen FileNotFound error message
Partially addresses #9203. It fixes the first case, but not the second
one mentioned in the issue.
2021-07-02 15:27:00 -07:00
Andrew Kelley
3f680abbe2 stage2: tokenizer: require null terminated source
By requiring the source file to be null-terminated, we avoid extra
branching while simplifying the logic at the same time.

Running ast-check on a large zig source file (udivmodti4_test.zig),
master branch compared to this commit:
 * 4% faster wall clock
 * 7% fewer cache misses
 * 1% fewer branches
2021-07-02 13:27:35 -07:00
Andrew Kelley
8ce880ca75 avoid calling into stage1 backend when AstGen fails
The motivation for this commit is that there exists source files which
produce ast-check errors, but crash stage1 or otherwise trigger stage1
bugs. Previously to this commit, Zig would run AstGen, collect the
compile errors, run stage1, report stage1 compile errors and exit if
any, and then report AstGen compile errors.

The main change in this commit is to report AstGen errors prior to
invoking stage1, and in fact if any AstGen errors occur, do not invoke
stage1 at all.

This caused most of the compile error tests to fail due to things such
as unused local variables and mismatched stage1/stage2 error messages.
It was taking a long time to update the test cases one-by-one, so I
took this opportunity to unify the stage1 and stage2 testing harness,
specifically with regards to compile errors. In this way we can start
keeping track of which tests pass for 1, 2, or both.
`zig build test-compile-errors` no longer works; it is now integrated
into `zig build test-stage2`.

This is one step closer to executing compile error tests in parallel; in
fact the ThreadPool object is already in scope.

There are some cases where the stage1 compile errors were actually
better; those are left failing in this commit, to be addressed in a
follow-up commit.

Other changes in this commit:

 * build.zig: improve support for -Dstage1 used with the test step.
 * AstGen: minor cosmetic changes to error messages.
 * stage2: add -fstage1 and -fno-stage1 flags. This now allows one to
   download a binary of the zig compiler and use the llvm backend of
   self-hosted. This was also needed for hooking up the test harness.
   However, I realized that stage1 calls exit() and also has memory
   leaks, so had to complicate the test harness by not using this flag
   after all and instead invoking as a child process.
   - These CLI flags will disappear once we start shipping the
     self-hosted compiler as the main compiler. Until then, they can be
     used to try out the work-in-progress stage2.
 * stage2: select the LLVM backend by default for release modes, as long
   as the target architecture is supported by LLVM.
 * test harness: support setting the optimize mode
2021-07-02 13:27:28 -07:00
J.C. Moyer
d84b386f60 stage2: print valid filename in error messages 2021-07-02 14:08:52 -04:00
Takeshi Yoneda
bc7761d8e0
Add support for WASI reactor in pure Zig-exe. (#9178)
* Add command line help for "-mexec-model"
* Define WasmExecModel enum in std.builtin.
* Drop the support for the old crt1.o in favor of crt1-command.o

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2021-06-30 20:02:48 -04:00
Jakub Konka
81bf05bf6c
Merge pull request #9266 from ziglang/zld-dylibs
zig ld can create dylibs now; remove system linker hack and any mention of ld64.lld from the codebase
2021-06-30 00:03:55 +02:00
Andrew Kelley
3d7ae63c6f stage2: remove c_object_cache_digest_set
This logic was a workaround to prevent cache deadlocks which happened
from always using exclusive file locks. Now that the Cache system
supports sharing cached artifacts, this workaround is no longer needed.

Closes #7596
2021-06-29 14:25:04 -07:00
Jakub Konka
75a13a456b zld: remove system linker hack and lld hooks 2021-06-29 07:59:46 +02:00
Andrew Kelley
6d47b4f39e Revert "Include package root dir in stage2 error messages"
This reverts commit 15a030ef3d2d68992835568f2fb9d5deab18f39f.

ast-check started crashing after this commit. Needs more QA before
merging.
2021-06-27 17:30:29 -07:00
J.C. Moyer
15a030ef3d Include package root dir in stage2 error messages 2021-06-27 14:53:46 +03:00
Isaac Freund
260c5aed86
stage2: add --sysroot link option
This feature is necessary for cross-compiling code that dynamically
links system libraries, at least with the current feature set of lld.
2021-06-25 19:13:43 +02:00
Jakub Konka
3f57468c8b Classify .m as ObjC, compile using clang and link with zld 2021-06-24 18:56:56 +02:00
Andrew Kelley
2beb21c4e4 stage2: fix crash when using stage1 backend
Calling processOutdatedAndDeletedDecls() should not happen when using
the stage1 backend. Problem solved with checking a simple flag.
2021-06-23 10:44:46 -07:00
Andrew Kelley
af40e3f54c stage2: glue code to AstGen root source file when using stage1
Normally we rely on importing std to in turn import the root
in the start code, but when using the stage1 won't happen,
so in order to run AstGen on the root we put it into the
import_table here.
2021-06-23 10:44:46 -07:00
Andrew Kelley
150515f44d stage2: slightly improve error reporting for missing imports
There is now a distinction between `@import` with a .zig extension and
without. Without a .zig extension it assumes it is a package name, and
returns error.PackageNotFound if not mapped into the package table.
2021-06-23 10:44:46 -07:00
Andrew Kelley
6fb45807ab progress bar: call it "AST Lowering" instead of "AstGen" 2021-06-23 10:44:46 -07:00
Andrew Kelley
cf65ab8601 run AstGen even when using the stage1 backend
This change reduces the amount of divergence in the compiler's main
pipeline logic enough to run AstGen for all files in the compilation,
regardless of whether the stage1 or stage2 backend is being used.

Practically, this means that all Zig code is subject to new compile
errors, such as unused local variables.

Additionally:
 * remove leftover unsound asserts from recent hash map changes
 * fix sub-Compilation errors not indenting correctly
2021-06-23 10:44:46 -07:00
Andrew Kelley
fee5aad699 stage2: remove unused parameter from importPkg 2021-06-21 17:03:04 -07:00
Andrew Kelley
f203334878 stage2: wire up AstGen to the progress bar 2021-06-21 17:03:03 -07:00
Jacob G-W
9fffffb07b fix code broken from previous commit 2021-06-21 17:03:03 -07:00
Jacob G-W
641ecc260f std, src, doc, test: remove unused variables 2021-06-21 17:03:03 -07:00
Michael Dusan
9ff15e24f8 fix oob during riscv64 feature processing 2021-06-17 23:15:47 -04:00
Andrew Kelley
90c73d8768 fix RISC-V assembly CPU features
Previously, Zig did not properly communicate the target CPU features for
RISC-V to clang assembler, because Clang has a different way to pass CPU
features for C code and for assembly code. This commit makes Zig pass a
RISC-V -march flag in order to communicate CPU features to Clang when
compiling assembly files.
2021-06-15 17:12:22 -04:00
Andrew Kelley
b6277a4b1c better awareness of unwind tables
* stage1 backend allows configuring the uwtables function attr
   via a flag rather than its own logic.
 * stage2 defaults to enabling uwtable attr when
   linking libunwind, or always on windows
 * stage2 makes link_eh_frame_hdr true automatically if uwtable
   attr is set to be on for zig functions
 * CLI: add -funwind-tables and -fno-unwind-tables to allow the user to
   override the defaults.
 * hook it up to `zig cc`

closes #9046
2021-06-11 13:23:00 -04:00
daurnimator
916b645fc1 Have std.fmt functions take case as an enum 2021-06-10 08:33:42 +03:00
Andrew Kelley
5e63baae8d CLI: remove --verbose-ast and --verbose-tokenize
closes #9034

These options were listed under the
"Debug Options (Zig Compiler Development)" heading. Anything in this
section should be considered unstable and can be modified at any time
at any developer's discretion.
2021-06-09 14:38:27 -07:00
Jakub Konka
9f8de83d93 cc,wasi: use wasi_libc.CRTFile directly instead of WasiExecModel 2021-06-09 11:11:55 +02:00
Takeshi Yoneda
bf568ec62a cc,wasi: support WASI reactors via -mexec-model flag.
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2021-06-09 17:07:06 +09:00
Jakub Konka
2ee1f7898b cc,wasi: store CRTFile enum in wasi_emulated_libs
* then, in `link/Wasm.zig` map `CRTFile` to full emulated libs name
* move logic for removing any mention of WASI snapshot
  `wasi_snapshot_preview1` from `Compilation.zig` into `link/Wasm.zig`
2021-06-09 01:25:38 +02:00
Jakub Konka
95745f77da wasi: skip adding wasi_snapshot_preview1 as lib dep in stage1 2021-06-09 01:25:38 +02:00
Jakub Konka
4e4722a65e cc,wasi: build referenced-only emulated components
Move parsing of system libs into `main.zig` next to where we decide
if we should link libC, and, if targeting WASI, if the specified
libname equals one of the emulated components, save it on the side
and remove it from the system libs. Then, build *only* those parts
of WASI libc that were preserved in the previous step.

This also fixes building of different crt1 bits needed to support
reactors and commands.
2021-06-09 01:25:38 +02:00
Martin Wickham
fc9430f567 Breaking hash map changes for 0.8.0
- hash/eql functions moved into a Context object
- *Context functions pass an explicit context
- *Adapted functions pass specialized keys and contexts
- new getPtr() function returns a pointer to value
- remove functions renamed to fetchRemove
- new remove functions return bool
- removeAssertDiscard deleted, use assert(remove(...)) instead
- Keys and values are stored in separate arrays
- Entry is now {*K, *V}, the new KV is {K, V}
- BufSet/BufMap functions renamed to match other set/map types
- fixed iterating-while-modifying bug in src/link/C.zig
2021-06-03 17:02:16 -05:00
xavier
8275c771c8 zig cc: expose header files when linking libunwind 2021-05-24 13:48:49 -04:00
Andrew Kelley
55811d8dac stage2: introduce clangAssemblerSupportsMcpuArg
Clang has a completely inconsistent CLI for its integrated assembler for
each target architecture. For x86_64, for example, it does not accept
an -mcpu parameter, and emits "warning: unused parameter". However, for
ARM, -mcpu is needed in order to properly lower assembly to machine code
instructions (see new standalone test case provided thanks to @g-w1).

This is a compromise between
b8f85a805bf61ae11d6ee2bd6d8356fbc98ee3ba and
afb9f695b1bdbf81185e7d55d5783bcbab880989.
2021-05-23 21:51:10 -07:00
Andrew Kelley
609207801a make "gnu" (mingw-w64) the default C ABI on Windows
Closes #6565
2021-05-24 00:13:54 -04:00
Andrew Kelley
b8f85a805b Revert "stage2: add support for zig cc assembler -mcpu option"
This reverts commit afb9f695b1bdbf81185e7d55d5783bcbab880989.

I don't think this was ever actually verified to fix the thing it
purported to, and it started causing warnings for unused command line
parameters.
2021-05-23 16:32:22 -07:00
Andrew Kelley
ae01c65a58 stage2: fix reference to musl arch name
Also rename musl.archMuslName to musl.archName.

Fixes a merge conflict from #8730 and #8837
2021-05-23 13:32:52 -07:00
Michael Dusan
0f26120377 overhaul elf csu (c-runtime startup) logic
- more support for linux, android, freebsd, netbsd, openbsd, dragonfly
- centralize musl utils; musl logic is no longer intertwined with csu
- fix musl compilation to build crti/crtn for full archs list
- fix openbsd to support `zig build-lib -dynamic`
- initial dragonfly linking success (with a warning)

ancillary:

- fix emutls (openbsd) tests to use `try`
2021-05-23 15:38:57 -04:00
Andrew Kelley
51701fb2da stage2: omit printing source lines in compile errors
when configured to not use tty awareness
2021-05-22 17:17:32 -07:00
Jakub Konka
4b69bd61e4
Merge pull request #8837 from ziglang/cc-wasm32-wasi
cc,wasi: ship WASI libc and autobuild it when needed
2021-05-21 09:04:16 +02:00
Andrew Kelley
3845c264a6 stage2: fix compile error rendering for hard tabs
render them as 1 space instead. This fixes the column caret.
2021-05-20 15:20:27 -07:00
Jakub Konka
b63c92f0b9 cc,wasi: do not add stack protector 2021-05-20 16:54:00 +02:00
Jakub Konka
3a5d0f7700 wasm: link dynamically by default when targeting wasm
This matches the behaviour of other languages and leaves us
the ability to create actual static Wasm archives with

```
zig build-lib -static some.zig
```

which can then be combined with other Wasm object files and linked
into either a Wasm lib or executable using `wasm-ld`.

Update langref to reflect the fact we now ship WASI libc.
2021-05-20 16:54:00 +02:00
Jakub Konka
6d5002028a cc,wasi: link compiled WASI libc with wasm-ld 2021-05-20 16:54:00 +02:00
Jakub Konka
7b74de7d71 wasi,cc: fix naming and add stubs for building
Rename include dir to match the convention:
  from `wasm32-wasi` to `wasm-wasi-musl`

Add building stubs which will be used to build and cache WASI
libc sysroot.
2021-05-20 16:54:00 +02:00
Isaac Freund
1d6c804b29 stage2: only default to linking system libc if linking system libs
We do need to link the system libc if linking system libraries as
they may potentially be compiled against e.g. a newer glibc version
than zig can provide. However if not linking system libraries, using
the zig provided libc is more reliable as it does not depend on any
quirks of the host system or being able to invoke the system cc to
find include dirs.
2021-05-19 01:22:36 -04:00
Andrew Kelley
ab8f8465a3 stage2: fix deletion of Decls that get re-referenced
When scanDecls happens, we create stub Decl objects that
have not been semantically analyzed. When they get referenced,
they get semantically analyzed.

Before this commit, when they got unreferenced, they were completely
deleted, including deleted from the containing Namespace.

However, if the update did not cause the containing Namespace to get
deleted, for example, if `std.builtin.ExportOptions` is no longer
referenced, but `std.builtin` is still referenced, and then `ExportOptions`
gets referenced again, the Namespace would be incorrectly missing the
Decl, so we get an incorrect "no such member" error.

The solution is to, when dealing with a no longer referenced Decl
objects during an update, clear them to the state they would be in
on a fresh scanDecl, rather than completely deleting them.
2021-05-18 12:35:36 -07:00
Andrew Kelley
731c35f15a stage2: get rid of NameHash
Previously, stage2 used a global decl_table for all Decl objects, keyed
by a 16-byte name hash that was hopefully unique. Now, there is a tree
of Namespace objects that own their named Decl objects.
2021-05-17 16:09:20 -07:00