aboutsummaryrefslogtreecommitdiff
path: root/src/stage1.zig
AgeCommit message (Collapse)Author
2022-08-19make self-hosted the default compilerAndrew Kelley
stage1 is available behind the -fstage1 flag. closes #89
2022-08-19Sema: avoid false positive error for linking libcAndrew Kelley
when extern c functions are called.
2022-07-23stage1: don't lowercaseify lib namesviri
2022-07-19delete the stage1 implementation of autodocAndrew Kelley
2022-04-22Revert "Fix C include files not being in `whole` cache (#11365)"Andrew Kelley
This reverts commit a430630002bf02162ccbf8d3eb10fd73e490cefd. Wait a minute, I'm sorry, I need to revert this. The whole premise of this change is broken because the point of the hash is that it tells whether the same compilation has been done before. This requires items to be added to the hash in the same sequence every time. This means that introducing a lock is fundamentally broken because the order needs to be the same in future runs of the compiler, and not decided by threads racing against each other. The proper solution to this is to, in whole cache mode, append the hash inputs to some data structure, and then after the compilation is complete, do some kind of sorting on the hash inputs so that they will be the same order every time, then apply them in sequence. No lock on the Cache object is needed for this scheme.
2022-04-22Fix C include files not being in `whole` cache (#11365)Tom Read Cutting
2022-02-08std.Progress: make the API infallibleAndrew Kelley
by handling `error.TimerUnsupported`. In this case, only explicit calls to refresh() will cause the progress line to be printed.
2022-01-11dupe sym name from stage1 before passing to stage2Jakub Konka
2022-01-07Pass `--allow-unknown-exports` to wasmtimeLuuk de Gram
Also skip exporting non-function symbols when we're building a WASI command using the stage2 llvm backend.
2022-01-06wasm-linker: Only export symbols notated as suchLuuk de Gram
This exposes a function from stage2 to stage1 to append symbols to automatically export them. This happends under the following conditions: - Target is wasm - User has not provided --export/--rdynamic flags themselves.
2022-01-02stage2: fix CLI not populating output binary filesAndrew Kelley
This fixes a regression in this branch that can be reproduced with the following steps: 1. `zig build-exe hello.zig` 2. delete the "hello" binary 3. `zig build-exe hello.zig` 4. observe that the "hello" binary is missing This happened because it was a cache hit, but nothing got copied to the output directory. This commit sets CacheMode to incremental - even for stage1 - when the CLI requests `disable_lld_caching` (this option should be renamed), resulting in the main Compilation to be repeated (uncached) for stage1, populating the binary into the cwd as expected. For stage2 the result is even better: the incremental compilation system will look for build artifacts to incrementally compile, and start fresh if not found.
2022-01-02stage2: introduce CacheModeAndrew Kelley
The two CacheMode values are `whole` and `incremental`. `incremental` is what we had before; `whole` is new. Whole cache mode uses everything as inputs to the cache hash; and when a hit occurs it skips everything including linking. This is ideal for when source files change rarely and for backends that do not have good incremental compilation support, for example compiler-rt or libc compiled with LLVM with optimizations on. This is the main motivation for the additional mode, so that we can have LLVM-optimized compiler-rt/libc builds, without waiting for the LLVM backend every single time Zig is invoked. Incremental cache mode hashes only the input file path and a few target options, intentionally relying on collisions to locate already-existing build artifacts which can then be incrementally updated. The bespoke logic for caching stage1 backend build artifacts is removed since we now have a global caching mechanism for when we want to cache the entire compilation, *including* linking. Previously we had to get "creative" with libs.txt and a special byte in the hash id to communicate flags, so that when the cached artifacts were re-linked, we had this information from stage1 even though we didn't actually run it. Now that `CacheMode.whole` includes linking, this extra information does not need to be preserved for cache hits. So although this changeset introduces complexity, it also removes complexity. The main trickiness here comes from the inherent differences between the two modes: `incremental` wants a directory immediately to operate on, while `whole` doesn't know the output directory until the compilation is complete. This commit deals with this problem mostly inside `update()`, where, on a cache miss, it replaces `zig_cache_artifact_directory` with a temporary directory, and then renames it into place once the compilation is complete. Items remaining before this branch can be merged: * [ ] make sure these things make it into the cache manifest: - @import files - @embedFile files - we already add dep files from c but make sure the main .c files make it in there too, not just the included files * [ ] double check that the emit paths of other things besides the binary are working correctly. * [ ] test `-fno-emit-bin` + `-fstage1` * [ ] test `-femit-bin=foo` + `-fstage1` * [ ] implib emit directory copies bin_file_emit directory in create() and needs to be adjusted to be overridden as well. * [ ] make sure emit-h is handled correctly in the cache hash * [ ] Cache: detect duplicate files added to the manifest Some preliminary performance measurements of wall clock time and peak RSS used: stage1 behavior (1077 tests), llvm backend, release build: * cold global cache: 4.6s, 1.1 GiB * warm global cache: 3.4s, 980 MiB stage2 master branch behavior (575 tests), llvm backend, release build: * cold global cache: 0.62s, 191 MiB * warm global cache: 0.40s, 128 MiB stage2 this branch behavior (575 tests), llvm backend, release build: * cold global cache: 0.62s, 179 MiB * warm global cache: 0.27s, 90 MiB
2021-12-03default mabi based on RISC-V extensions and -mabi build optionvole-dev
The target abi can also be set in build.zig via LibExeObjStep.target_abi The value passed in is checked that it is a valid value in std.Target.TargetAbi The target abi is also validated against the target cpu
2021-11-30allocgate: renamed getAllocator function to allocatorLee Cannon
2021-11-30allocgate: stage 1 and 2 buildingLee Cannon
2021-11-30std lib API deprecations for the upcoming 0.9.0 releaseAndrew Kelley
See #3811
2021-10-16Add build.zig and command line flagsLee Cannon
2021-07-23stage2: improvements towards `zig test`Andrew Kelley
* There is now a main_pkg in addition to root_pkg. They are usually the same. When using `zig test`, main_pkg is the user's source file and root_pkg has the test runner. * scanDecl no longer looks for test decls outside the package being tested. honoring `--test-filter` is still TODO. * test runner main function has a void return value rather than `anyerror!void` * Sema is improved to generate better AIR for for loops on slices. * Sema: fix incorrect capacity calculation in zirBoolBr * Sema: add compile errors for trying to use slice fields as an lvalue. * Sema: fix type coercion for error unions * Sema: fix analyzeVarRef generating garbage AIR * C codegen: fix renderValue for error unions with 0 bit payload * C codegen: implement function pointer calls * CLI: fix usage text Adds 4 new AIR instructions: * slice_len, slice_ptr: to get the ptr and len fields of a slice. * slice_elem_val, ptr_slice_elem_val: to get the element value of a slice, and a pointer to a slice. AstGen gains a new functionality: * One of the unused flags of struct decls is now used to indicate structs that are known to have non-zero size based on the AST alone.
2021-07-22support -fcompiler-rt in conjunction with build-objAndrew Kelley
When using `build-exe` or `build-lib -dynamic`, `-fcompiler-rt` means building compiler-rt into a static library and then linking it into the executable. When using `build-lib`, `-fcompiler-rt` means building compiler-rt into an object file and then adding it into the static archive. Before this commit, when using `build-obj`, zig would build compiler-rt into an object file, and then on ELF, use `lld -r` to merge it into the main object file. Other linker backends of LLD do not support `-r` to merge objects, so this failed with error messages for those targets. Now, `-fcompiler-rt` when used with `build-obj` acts as if the user puts `_ = @import("compiler_rt");` inside their root source file. The symbols of compiler-rt go into the same compilation unit as the root source file. This is hooked up for stage1 only for now. Once stage2 is capable of building compiler-rt, it should be hooked up there as well.
2021-07-22add -femit-llvm-bc CLI option and implement itAndrew Kelley
* Added doc comments for `std.Target.ObjectFormat` enum * `std.Target.oFileExt` is removed because it is incorrect for Plan-9 targets. Instead, use `std.Target.ObjectFormat.fileExt` and pass a CPU architecture. * Added `Compilation.Directory.joinZ` for when a null byte is desired. * Improvements to `Compilation.create` logic for computing `use_llvm` and reporting errors in contradictory flags. `-femit-llvm-ir` and `-femit-llvm-bc` will now imply `-fLLVM`. * Fix compilation when passing `.bc` files on the command line. * Improvements to the stage2 LLVM backend: - cleaned up error messages and error reporting. Properly bubble up some errors rather than dumping to stderr; others turn into panics. - properly call ZigLLVMCreateTargetMachine and ZigLLVMTargetMachineEmitToFile and implement calculation of the respective parameters (cpu features, code model, abi name, lto, tsan, etc). - LLVM module verification only runs in debug builds of the compiler - use LLVMDumpModule rather than printToString because in the case that we incorrectly pass a null pointer to LLVM it may crash during dumping the module and having it partially printed is helpful in this case. - support -femit-asm, -fno-emit-bin, -femit-llvm-ir, -femit-llvm-bc - Support LLVM backend when used with Mach-O and WASM linkers.
2021-07-02avoid calling into stage1 backend when AstGen failsAndrew Kelley
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-06-21fix code broken from previous commitJacob G-W
2021-06-14add ast-check flag to zig fmt, fix found bugsVeikka Tuominen
2021-06-11better awareness of unwind tablesAndrew Kelley
* 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-09CLI: remove --verbose-ast and --verbose-tokenizeAndrew Kelley
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-05-28stage1: rework tokenizer to match stage2Andrew Kelley
* Extracts AstGen logic from ir.cpp into astgen.cpp. Reduces the largest file of stage1 from 33,551 lines to 25,510. * tokenizer: rework it completely to match the stage2 tokenizer logic. They can now be maintained together; when one is changed, the other can be changed in the same way. - Each token now takes up 13 bytes instead of 64 bytes. The tokenizer does not parse char literals, string literals, integer literals, etc into meaningful data. Instead, that happens during parsing or astgen. - no longer store line offsets. Error messages scan source files to find the line/column as needed (same as stage2). - main loop: instead of checking the loop, handle a null byte explicitly in the switch statements. This is a nice improvement that we may want to backport to stage2. - delete some dead tokens, artifacts of past syntax that no longer exists. * Parser: fix a TODO by parsing builtin functions as tokens rather than `@` as a separate token. This is how stage2 does it. * Remove some debugging infrastructure. These will need to be redone, if at all, as the code migrates to match stage2. - remove the ast_render code. - remove the IR debugging stuff - remove teh token printing code
2021-02-21std.Progress: improve support for "dumb" terminalsjacob gw
2021-01-23add LTO supportAndrew Kelley
The CLI gains -flto and -fno-lto options to override the default. However, the cool thing about this is that the defaults are great! In general when you use build-exe in release mode, Zig will enable LTO if it would work and it would help. zig cc supports detecting and honoring the -flto and -fno-lto flags as well. The linkWithLld functions are improved to all be the same with regards to copying the artifact instead of trying to pass single objects through LLD with -r. There is possibly a future improvement here as well; see the respective TODOs. stage1 is updated to support outputting LLVM bitcode instead of machine code when lto is enabled. This allows LLVM to optimize across the Zig and C/C++ code boundary. closes #2845
2021-01-11stage2: cleanups regarding red zone CLI flagsAndrew Kelley
* CLI: change to -mred-zone and -mno-red-zone to match gcc/clang. * build.zig: remove the double negative and make it an optional bool. This follows precedent from other flags, allowing the compiler CLI to be the decider of what is default instead of duplicating the default value into the build system code. * Compilation: make it an optional `want_red_zone` instead of a `no_red_zone` bool. The default is decided by a call to `target_util.hasRedZone`. * When creating a Clang command line, put -mred-zone on the command line if we are forcing it to be enabled. * Update update_clang_options.zig with respect to the recent {s}/{} format changes. * `zig cc` integration with red zone preference.
2021-01-11Added support for no red zoneLee Cannon
2021-01-02stage2: Use {s} instead of {} when formatting stringsLemonBoy
2020-12-24WIP start adding support for TSANAndrew Kelley
2020-12-20std.Progress: make the API thread-safeAndrew Kelley
We generally get away with atomic primitives, however a lock is required around the refresh function since it traverses the Node graph, and we need to be sure no references to Nodes remain after end() is called.
2020-12-07ability to build stage1 using only a zig tarballAndrew Kelley
The main idea here is that there are now 2 ways to get a stage1 zig binary: * The cmake path. Requirements: cmake, system C++ compiler, system LLVM, LLD, Clang libraries, compiled by the system C++ compiler. * The zig path. Requirements: a zig installation, system LLVM, LLD, Clang libraries, compiled by the zig installation. Note that the former can be used to now take the latter path. Removed config.h.in and config.zig.in. The build.zig script no longer is coupled to the cmake script. cmake no longer tries to determine the zig version. A build with cmake will yield a stage1 zig binary that reports 0.0.0+zig0. This is going to get reverted. `zig build` now accepts `-Dstage1` which will build the stage1 compiler, and put the stage2 backend behind a feature flag. build.zig is simplified to only support the use case of enabling LLVM support when the LLVM, LLD, and Clang libraries were built by zig. This part is probably sadly going to have to get reverted to make package maintainers happy. Zig build system addBuildOption supports a couple new types. The biggest reason to make this change is that the zig path is an attractive option for doing compiler development work on Windows. It allows people to work on the compiler without having MSVC installed, using only a .zip file that contains Zig + LLVM/LLD/Clang libraries.
2020-11-22modernize the PIE patch for the latest master branchAndrew Kelley
This is the part of #3960 that has to be rewritten to apply to latest master branch code.
2020-10-26non-hacky workaround for the empty file bugAndrew Kelley
See #3328
2020-10-17code cleanupsAndrew Kelley
* in selfExePath, return errors instead of defaulting to bogus data * less invasive edits to the logic of link/Elf.zig * less indentation
2020-10-11initialize std.os.argv in stage1 compiler. it is needed for selfExePath ↵Sébastien Marie
under OpenBSD
2020-10-03stage1 main(): catch unreachable in debug builds for easier troubleshootingAndrew Kelley
Fixup to 2a3393eff853fe0a1dee766c859a345186e91fa3
2020-10-01Adds error return trace printing to stage1.Felix (xq) Queißner
2020-09-28the stage1 backend cache stores inferred link libsAndrew Kelley
So that we properly learn about extern "foo" functions called even when we get a stage1 cache hit.
2020-09-28stage2: building mingw-w64 and COFF LDD linkingAndrew Kelley
still TODO is the task of creating import .lib files for DLLs on the fly both for -lfoo and for e.g. `extern "kernel32"`
2020-09-26fix another round of regressions in this branchAndrew Kelley
* std.log: still print error messages in ReleaseSmall builds. - when start code gets an error code from main, it uses std.log.err to report the error. this resulted in a test failure because ReleaseSmall wasn't printing `error: TheErrorCode` when an error was returned from main. But that seems like it should keep working. So I changed the std.log defaults. I plan to follow this up with a proposal to change the names of and reduce the quantity of the log levels. * warning emitted when using -femit-h when using stage1 backend; fatal log message when using -femit-h with self-hosted backend (because the feature is not yet available) * fix double `test-cli` build steps in zig's build.zig * update docgen to use new CLI * translate-c uses `-x c` and generates a temporary basename with a `.h` extension. Otherwise clang reports an error. * --show-builtin implies -fno-emit-bin * restore the compile error for using an extern "c" function without putting -lc on the build line. we have to know about the libc dependency up front. * Fix ReleaseFast and ReleaseSmall getting swapped when passing the value to the stage1 backend. * correct the zig0 CLI usage text. * update test harness code to the new CLI.
2020-09-26implement -femit-asm, -femit-docs, -femit-llvm-ir, etcAndrew Kelley
These CLI options are now forwarded to the stage1 backend. We're not going to support the -mllvm CLI option any longer. As a compromise, we unconditionally tell LLVM to output intel x86 syntax when using -femit-asm. Simplify stage1 logic; it no longer has the concept of an output directory. --output-dir is no longer a valid CLI option. cmake uses the `-femit-bin=[path]` option. Note the changes to test/cli.zig. This breaks the CLI API that Godbolt is using so we're going to want to open a PR to help them upgrade to the new CLI for the upcoming Zig 0.7.0 release.
2020-09-24don't delete C import .c source from zig-cacheAndrew Kelley
preventing CacheUnavailable error when doing `@cImport`
2020-09-24stage2: implement @cImportAndrew Kelley
Also rename Cache.CacheHash to Cache.Manifest
2020-09-22stage2: implement zig buildAndrew Kelley
As part of this: * add std.process.cleanExit. closes #6395 - use it in several places * adjust the alignment of text in `zig build --help` menu * Cache: support the concept of "unhit" so that we properly keep track of the cache when we find out using the secondary hash that the cache "hit" was actually a miss. Use this to fix false negatives of caching of stage1 build artifacts. * fix not deleting the symlink hash for stage1 build artifacts causing false positives. * implement support for Package arguments in stage1 build artifacts * update and add missing usage text * add --override-lib-dir and --enable-cache CLI options - `--enable-cache` takes the place of `--cache on` * CLI supports -femit-bin=foo combined with --enable-cache to do an "update file" operation. --enable-cache without that argument will build the output into a cache directory and then print the path to stdout (matching master branch behavior). * errors surfacing from main() now print "error: Foo" instead of "error: error.Foo".
2020-09-21rename src-self-hosted/ to src/Andrew Kelley