14571 Commits

Author SHA1 Message Date
Matt Knight
fb16633ecb C backend: add/sub/mul wrapping for the C backend 2021-07-08 09:56:40 -07:00
Andrew Kelley
62d27fcfb6
Merge pull request #9325 from ziglang/stage2-inferred-error-sets
Stage2 inferred error sets and `@panic`
2021-07-08 02:04:53 -04:00
yetanothercheer
7935e83b1d Fix indentation in langref.html.in 2021-07-08 00:05:29 -04:00
Andrew Kelley
c2e66d9bab stage2: basic inferred error set support
* Inferred error sets are stored in the return Type of the function,
   owned by the Module.Fn. So it cleans up that memory in deinit().
 * Sema: update the inferred error set in zirRetErrValue
   - Update relevant code in wrapErrorUnion
 * C backend: improve some some instructions to take advantage of
   liveness analysis to avoid being emitted when unused.
 * C backend: when an error union has a payload type with no runtime
   bits, emit the error union as the same type as the error set.
2021-07-07 20:47:21 -07:00
Andrew Kelley
5c8bd443d9 stage2: fix if expressions on error unions
AstGen had the then-else logic backwards for if expressions
on error unions. This commit fixes it.

Turns out AstGen only really needs `is_non_null` and `is_non_err`,
and does not need the `is_null` or `is_err` variants. So I removed the
`is_null{,_ptr}` and `is_err{,_ptr}` ZIR instructions (-4) and
added `is_non_err`, `is_non_err_ptr` ZIR instructions (+2) for
a total of (-2) ZIR instructions, giving us a tiny bit more headroom
within the 256 tag limit. This required swapping the order of
then/else blocks in a handful of cases, but ultimately means the
ZIR will be in the same as source order, which is convenient
when debugging.

AIR code on the other hand, gains the `is_non_err` and `is_non_err_ptr`
instructions.

Sema: fix logic in zirErrUnionCode and zirErrUnionCodePtr returning the
wrong result type.
2021-07-07 19:50:56 -07:00
Andrew Kelley
ec63411905 Revert "Skip over CRs at the end of multiline literals"
This reverts commit 9de452f9a69d5590743a194bc2d0817d26d66a0b.

No CRs allowed in multiline string literals - this is intentional.
2021-07-07 18:00:04 -07:00
Andrew Kelley
5816997ae7 stage2: get tests passing
* implement enough of ret_err_value to pass wasm tests
 * only do the proper `@panic` implementation for the backends which
   support it, which is currently only the C backend. The other backends
   will see `@breakpoint(); unreachable;` same as before.
   - I plan to do AIR memory layout reworking as a prerequisite to
     fixing other backends, because that will help me put all the
     constants up front, which will allow the codegen to lower to memory
     without jumps.
 * `@panic` is implemented using anon decls for the message. Makes it
   easier on the backends. Might want to look into re-using decls for
   this in the future.
 * implement DWARF .debug_info for pointer-like optionals.
2021-07-07 14:17:04 -07:00
Daniele Cocca
9de452f9a6 Skip over CRs at the end of multiline literals
Fixes #9257.
This is needed when tokenizing input containing DOS line endings, i.e.
the CRLF sequence.
2021-07-07 20:03:19 +03:00
Andrew Kelley
13f04e3012 stage2: implement @panic and beginnigs of inferred error sets
* ZIR: add two instructions:
   - ret_err_value_code
   - ret_err_value
 * AstGen: add countDefers and utilize it to emit more efficient ZIR for
   return expressions in the presence of defers.
 * AstGen: implement |err| payloads for `errdefer` syntax.
   - There is not an "unused capture" error for it yet.
 * AstGen: `return error.Foo` syntax gets a hot path in return
   expressions, using the new ZIR instructions. This also is part of
   implementing inferred error sets, since we need to tell Sema to add
   an error value to the inferred error set before it gets coerced.
 * Sema: implement `@setCold`.
   - Implement `@setCold` support for C backend.
 * `@panic` and regular safety panics such as `unreachable` now properly
   invoke `std.builtin.panic`.
 * C backend: improve pointer and function value rendering.
 * C linker: fix redundant typedefs.
 * Add Type.error_set_inferred.
 * Fix Value.format for enum_literal, enum_field_index, bytes.
 * Remove the C backend test that checks for identical text

I measured a 14% reduction in Total ZIR Bytes from master branch
for std/os.zig.
2021-07-07 00:39:23 -07:00
Andrew Kelley
d481acc7db std.builtin.panic: simpler default panic for stage2
until it catches up to stage1 in terms of supported language features
2021-07-07 00:38:46 -07:00
Andrew Kelley
298a65ff4b std.HashMap: add ensureUnusedCapacity and ensureTotalCapacity
and deprecated ensureCapacity. This matches the pattern set by ArrayList
and ArrayHashMap already.
2021-07-07 00:38:10 -07:00
Carlos Zúñiga
b936bbd2f1 Fixed builtin.Target -> std.Target 2021-07-07 07:54:38 +03:00
Jacob G-W
23f414b927 stage1 parser: update comments to match impl 2021-07-07 07:38:26 +03:00
Andrew Kelley
6ba843ee0f
Merge pull request #9310 from ziglang/stage1-better-hashing
Speed up stage 1 by improving hash functions
2021-07-06 13:20:15 -04:00
leesongun
cff7ecee07
Fix unexpected truncation behavior with comptime_int larger than u64 range (#9303)
Closes #9299
2021-07-06 12:42:18 +03:00
Martin Wickham
f02ee7a9f5 Avoid some large copies for another second of time saved 2021-07-06 00:57:46 -05:00
Jacob G-W
6c11e9bb07 stage2: add error note for comparing booleans with '||' 2021-07-06 07:51:30 +03:00
Jacob G-W
d089b3df7a remove really weird debugging statement from stage1 2021-07-05 21:13:10 -04:00
Martin Wickham
149ecdfe1b Remove debug checks, audit field accesses 2021-07-05 19:41:48 -05:00
Andrew Kelley
b3225a755a stage1: avoid incorrectly reading ZigValue data
Hashing, equality checking, and expanding lazy values were not
inspecting the is_comptime field of structs, causing incorrect behavior
for tuples. When looking at a comptime value of a struct, if the
is_comptime field is true, the value must be learned from the type
rather than the value.
2021-07-05 16:22:40 -07:00
Evan Haas
b5659e0233 translate-c: Don't discard variables unless necessary
Closes #9205
2021-07-05 17:51:53 -04:00
Andrew Kelley
0ff9a4d21c stage1: resolve lazy values before comptime fn call 2021-07-05 14:07:36 -07:00
Martin Wickham
0e5fa87ac9 Better hashing, new asserts failing 2021-07-05 15:28:13 -05:00
Andrew Kelley
9b08687766 stage1: recursively resolve lazy values before hashing
When putting ZigValues into a hash map. The hash of a lazy value
and a fully resolved value must equal, and so we must resolve
the lazy values prior. The hash function asserts that none of
the values are lazy.
2021-07-05 13:05:03 -07:00
Andrew Kelley
b7da1b2d45
Merge pull request #9175 from kprotty/thread
std.Thread enhancements
2021-07-04 22:31:02 -04:00
Nulo
5d0dad9acd Link to the GPA now that it's on upstream 2021-07-04 15:44:29 -04:00
leesongun
da2ca447c8
implement xoshiro256++ (#9298)
Implement xoshiro256++
2021-07-04 16:15:23 +02:00
Nulo
8a863381d1 CODE_OF_CONDUCT: change Freenode to Libera.chat 2021-07-03 20:28:17 -04:00
Andrew Kelley
254a35fd88 stage2: add the zig version to AstGen cache hash
This solves the problem of different versions of Zig having different
binary representations of ZIR code.

closes #9290
2021-07-03 11:47:58 -07:00
kprotty
c8f90a7e7e zig fmt 2021-07-03 11:49:07 -05: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
af20fdbce7 stage1: eliminate the IrInst base struct
This commit intentions to have no functional changes. The only purpose
is to delete the struct IrInst, which is the common base struct that
both IrInstSrc (ZIR) and IrInstGen (AIR) instructions embed.

This untangles stage1 ZIR and AIR memory layout, paving the way for a
following commit to reduce memory usage.
2021-07-03 03:14:54 -04:00
Andrew Kelley
bb98620c10
Merge pull request #9219 from ziglang/unreachable-code
move "unreachable code" error from stage1 to stage2
2021-07-02 21:11:45 -04:00
Andrew Kelley
a6bf68ccf9 langref: fix test cases now that AST Lowering has priority 2021-07-02 16:22:09 -07: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
5103053977 compile errors test harness: support unknown file/line/column
This gets us 2 more passing compile error test cases.
2021-07-02 13:28:31 -07:00
Andrew Kelley
c5c23db627 tokenizer: clean up invalid token error
It now displays the byte with proper printability handling. This makes
the relevant compile error test case no longer a regression in quality
from stage1 to stage2.
2021-07-02 13:28:31 -07:00
Andrew Kelley
7a2e0d9810 AstGen: cleanups to pass more compile error test cases 2021-07-02 13:28:29 -07:00
Andrew Kelley
41336acb0b AstGen: detect redeclaration of function parameters
Also improve redeclaration error message to include the category of
variable.
2021-07-02 13:27:35 -07:00
Andrew Kelley
abfee12735 AstGen: pass more compile error tests
* Implement "initializing array with struct syntax"
 * Implement "'_' used as an identifier without @\"_\" syntax"
 * Fix source location of "missing parameter name"
 * Update test cases where appropriate
2021-07-02 13:27:35 -07:00
Andrew Kelley
24c432608f stage2: improve compile errors from tokenizer
In order to not regress the quality of compile errors, some improvements
had to be made.

 * std.zig.parseCharLiteral is improved to return more detailed parse
   failure information.
 * tokenizer is improved to handle null bytes in the middle of strings,
   character literals, and line comments.
 * validating how many unicode escape digits in string literals is moved
   to std.zig.parseStringLiteral rather than handled in the tokenizer.
 * when a tokenizer error occurs, if the reported token is the 'invalid'
   tag, an error note is added to point to the invalid byte location.
   Further improvements would be:
   - Mention the expected set of allowed bytes at this location.
   - Display the invalid byte (if printable, print it, otherwise
     escape-print it).
2021-07-02 13:27:35 -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
Andrew Kelley
22b20f20b6 AstGen: fix missing compile error for unreachable @TypeOf argument 2021-07-02 13:26:50 -07:00
Andrew Kelley
7f5560689a AstGen: introduce 'reachableExpr' function
This function can be swapped out for calls to expr() to report a compile
error when the expression results in control flow that does not return.
2021-07-02 13:26:50 -07:00
Andrew Kelley
cffa22a658 AstGen: implement compile error for useless locals
When a local variable has an initialization expression of type
'noreturn', emit a compile error. This brings this branch closer
to parity with master branch.
2021-07-02 13:26:50 -07:00
Andrew Kelley
527c55aa56 stage1: get rid of the is_noreturn flag on IrInstSrc
One more step towards lowering the memory footprint of stage1. This flag
was hiding in padding but now that it is gone we can re-arrange the
memory layout more easily.
2021-07-02 13:26:50 -07:00
Andrew Kelley
125b85d737 move "unreachable code" error from stage1 to stage2
* AstGen: implement "unreachable code" error for blocks. This works at
   the statement level.
 * stage1: remove the "unreachable code" error implementation, which
   means removing the `is_gen` field from IrInstSrc. This is one small
   step towards a smaller memory footprint for stage1. The benefits
   won't be realized until a future commit because this flag took
   advantage of padding.

There may be a regression here with "union has no associated enum"
error, and there is a regression with the following code:

```zig
const a = noreturn;
```

A future commit will address these regressions.
2021-07-02 13:26:50 -07:00
J.C. Moyer
d84b386f60 stage2: print valid filename in error messages 2021-07-02 14:08:52 -04:00
kprotty
483eb8e057 std.Thread: move linux detach code to inline asm 2021-07-01 17:34:23 -05:00