12230 Commits

Author SHA1 Message Date
Timon Kruiper
1149cd593e stage2: rename *const llvm.ValueRef to *const llvm.Value in LLVM backend
The same has been done for all the other LLVM types.
2021-01-06 10:52:19 +01:00
Timon Kruiper
70f6d16ae2 stage2: add initial impl for generating global decls in LLVM backend
Also adds support for extern functions, simple pointer and simple array
types and values.

A simple hello world now compiles:
`zig build-exe example.zig -fLLVM -lc`
```
extern fn puts(s: [*:0]const u8) c_int;
export fn main() c_int {
    _ = puts("hello world!");
    return 0;
}
```
2021-01-06 10:52:07 +01:00
xackus
6c4924408b std.c add syslog 2021-01-05 13:09:40 -08:00
xackus
1640c357f4 freebsd, netbsd, dragonfly: add struct timeval 2021-01-05 13:07:25 -08:00
Andrew Kelley
3e8aaee829 std: skip more tests on Windows to save CI memory
I've enabled only the tests that check things specific to Windows that
are not tested by other systems.
2021-01-04 22:25:04 -07:00
Andrew Kelley
16896a9d8b ci: skip crypto tests on windows
Trying to buy us more time on the Windows CI.
2021-01-04 15:57:54 -07:00
Andrew Kelley
66e5e92a3e
Merge pull request #7592 from LemonBoy/fix-7188
Allow variable captures on multi-prong switch arms
2021-01-04 14:23:01 -08:00
Evan Haas
d95724454c Allow dollar sign $ in identifiers in translate-c
In strictly conforming C, identifiers cannot container dollar signs.
However GCC and Clang allow them by default, so translate-c should
handle them. See http://gcc.gnu.org/onlinedocs/cpp/Tokenization.html
I encountered this in the wild in windows.h

Fixes #7585
2021-01-04 14:14:04 -08:00
Felix (xq) Queißner
819f2a01a1 Fixes missing error prong in std.os.send. 2021-01-04 14:03:39 -08:00
J.C. Moyer
fc3508b7e8 Fix off-by-one error in SinglyLinkedList.len() and add associated tests 2021-01-04 14:03:21 -08:00
xackus
a93c123f83 std.c: add some noalias 2021-01-04 14:02:42 -08:00
Andrew Kelley
2fe8a48215 ci: omit stage2 backend from stage1 on Windows
to avoid out-of-memory on the CI runs.
2021-01-04 14:59:18 -07:00
Andrew Kelley
462c1d8c74 stage2: add more perf tracing points 2021-01-04 14:33:32 -07:00
Andrew Kelley
fc38b42521 Revert "Fix #7296:"
This broke build scripts that wanted to refer to `exe_dir` or
`install_path`.

There has also been some pushback and discussion on this breaking
change. I think it should be re-evaluated.

This reverts commit a1a1929cf4ff979bdaba17f858d4ce8647e87a65.
2021-01-04 13:49:17 -07:00
Andrew Kelley
ef2fa67ef0 Merge branch 'g-w1-stage2-evalbranch'
closes #7682
2021-01-04 13:40:51 -07:00
Andrew Kelley
7e64dc4221 stage2: improvements to @setEvalBranchQuota
* extract magic number into a constant
 * properly use result location casting for the operand
 * naming convention for ZIR instructions
2021-01-04 13:40:01 -07:00
g-w1
638f93ebdc stage2: implementation of @setEvalBranchQuota:
`@setEvalBranchQuota` can be called before the comptime/inline call
stack is created.

For example:

```zig
@setEvalBranchQuota(100);
comptime {
    while (true) {}
}
```

Here we need to set the branch_quota before the comptime block creates a
scope for the branch_count.
2021-01-04 12:42:52 -07:00
joachimschmidt557
aa0906e9aa stage2 x86_64: fix bug in Function.gen
Previously, the x86_64 backend would remove code for exitlude relocs
if the jump amount were 0. This causes issues as earlier jumps rely on
the jump being present at the same address.
2021-01-03 19:54:12 -08:00
Frank Denis
4400d2d7ab std/crypto: add BLAKE2-160 types and tests 2021-01-03 19:53:30 -08:00
daurnimator
e4c4a0a5f6 Improve uring definitions 2021-01-03 19:52:20 -08:00
Andrew Kelley
53a0b7997d
Merge pull request #7681 from kubkon/stage2-aarch64-fn-args
stage2: basic fn args for aarch64
2021-01-03 19:51:38 -08:00
Andrew Kelley
c8e44d82bd stage2: remove the Cache deadlock detection code
It's more trouble than it's worth; it didn't even catch the most recent
incident because it was across process boundaries anyway.
2021-01-03 20:34:51 -07:00
Andrew Kelley
404dc9692e stage2: fix Cache debug deadlock code memory leak 2021-01-03 20:34:51 -07:00
Andrew Kelley
5c92e24a29 drone ci: skip compile error tests to save time
These are covered by other CI scripts and we're up against Drone CI time
limits.
2021-01-03 20:10:07 -07:00
Andrew Kelley
f6644255f5
Merge pull request #7598 from FireFox317/more-llvm-stage2
stage2: More improvements to self-hosted LLVM backend
2021-01-03 16:09:14 -08:00
Evan Haas
5cc131030c Static function declarations with no prototype should not be variadic
If a static function is defined with no argument list and no prototype
is given, it should be treated as a function that takes no arguments
rather than as a variadic function.

Fixes #7594
2021-01-03 15:08:32 -08:00
Jakub Konka
807dc56fd6 stage2: add aarch64 stage2 tests
Fix missing string format specifier in Mach-O used to generate
path to debug symbols bundle.
2021-01-03 23:20:09 +01:00
Jakub Konka
2a410baa2b stage2: implement basic function params aarch64
Implement missing `.register` prong for `aarch64` `genSetReg`.
2021-01-03 23:03:20 +01:00
Timon Kruiper
0151f3b76a stage2: Add support for testing LLVM enabled builds in test-stage2
To make sure that we don't have to rebuild libc for every case, we now
have a seperate cache directory for the global cache, which remains
the same between test runs.

Also make sure to destory the Compilation before executing a child process,
otherwise the compiler deadlocks. (#7596)
2021-01-03 21:13:38 +01:00
Timon Kruiper
a926c91814 stage2: enable building test-stage2 with LLVM backend enabled
We can now run `zig build test-stage2 -Denable-llvm`.
2021-01-03 17:39:43 +01:00
Timon Kruiper
7e5aacab69 stage2: add some missing deallocations in Compilation.zig 2021-01-03 17:39:43 +01:00
Timon Kruiper
3c05c60acc stage2: Output the LLVM object files in the cache directory
Also make sure to properly free everything.
2021-01-03 17:39:43 +01:00
Timon Kruiper
0008bef1e6 stage2: add support for integers in LLVM backend
Also adds support for simple operators, like add and subtract.
The intcast and bitcast instruction also have been implemented.

Linking with libc also works, so we can now generate working executables!

`zig build-exe example.zig -fLLVM -lc`:
```
fn add(a: i32, b: i32) i32 {
    return a + b;
}

export fn main() c_int {
    var a: i32 = -5;
    const x = add(a, 7);
    var y = add(2, 0);
    y -= x;
    return y;
}
```
2021-01-03 17:39:30 +01:00
Timon Kruiper
e095ebf312 stage2: make use of proper LLVM intrinsic APIs in LLVM backend 2021-01-03 17:23:30 +01:00
Timon Kruiper
da545d6a31 stage2: implement argument passing and returning in LLVM backend
Furthermore add the Not instruction.

The following now works:
```
export fn _start() noreturn {
    var x: bool = true;
    var other: bool = foo(x);
    exit();
}

fn foo(cond: bool) bool {
    return !cond;
}

fn exit() noreturn {
    unreachable;
}
```
2021-01-03 17:23:30 +01:00
Timon Kruiper
47a4d43e41 stage2: Add code generation for Load instruction in LLVM backend
The following now works:
```
export fn _start() noreturn {
    var x: bool = true;
    var y: bool = x;
    exit();
}

fn exit() noreturn {
    unreachable;
}
```
2021-01-03 17:23:30 +01:00
Timon Kruiper
19cfd310b0 stage2: implement register allocation in LLVM self-hosted backend
A HashMap has been added which store the LLVM values used in a function.
Together with the alloc and store instructions the following now works:
```
export fn _start() noreturn {
    var x: bool = true;
    exit();
}

fn exit() noreturn {
    unreachable;
}
```
2021-01-03 17:23:30 +01:00
Timon Kruiper
a5dab15ede stage2: clear err_msg after it has been added to module.failed_decls 2021-01-03 17:23:30 +01:00
Timon Kruiper
0ed04aac8b stage2: fix building self-hosted compiler with -Dstatic-llvm
This supersedes c81ae52ee0b2e952f8ed9c5c6517af8182bb09c1
2021-01-03 17:23:30 +01:00
Frank Denis
5aac2fc281 std/crypto: properly support arbitrary output sizes
Fixes #7657
2021-01-02 22:32:57 -08:00
Andrew Kelley
683814190b
Merge pull request #7612 from g-w1/do-7296
Implement #7296
2021-01-02 22:05:31 -08:00
Andrew Kelley
d8f3f14532
Merge pull request #7647 from ziglang/stage2-comptime-fn-call
stage2: comptime function calls and inline function calls
2021-01-02 22:01:51 -08:00
Andrew Kelley
654832253a stage2: support recursive inline/comptime functions
zir.Inst no longer has an `analyzed_inst` field. This is previously how
we mapped ZIR to their TZIR counterparts, however with the way inline
and comptime function calls work, we can potentially have the same ZIR
structure being analyzed by multiple different analyses, such as during
a recursive inline function call. This would cause the `analyzed_inst`
field to become clobbered. So instead, we use a table to map the
instructions to their semantically analyzed counterparts. This will help
with multi-threaded compilation as well.

Scope.Block.Inlining is split into 2 different layers of "sharedness".
The first layer is shared by the whole inline/comptime function call
stack. It contains the callsite where something is being inlined and the
branch count/quota. The second layer is different per function call but
shared by all the blocks within the function being inlined.

Add support for debug dumping br and brvoid TZIR instructions.

Remove the "unreachable code" error. It was happening even for this case:

```zig
if (comptime_condition) return;
bar(); // error: unreachable code
```

We will need smarter logic for when it is legal to emit this compile
error.

Remove the ZIR test cases. These are redundant with other higher level
Zig source tests we have, and maintaining support for ZIRModule as a
first-class top level abstraction is getting in the way of clean
compiler design for the main use case. We will have ZIR/TZIR based test
cases someday to help with testing optimization passes and ZIR to TZIR
analysis, but as is, these test cases are not accomplishing that, and
they are getting in the way.
2021-01-02 22:42:07 -07:00
g-w1
3d151fbfc8 fix 7665:
only add self exe path when testing
2021-01-02 20:35:44 -08:00
Andrew Kelley
50a530196c stage2: fix handling compile error in inline fn call
* scopes properly inherit inlining information
 * compile errors of inline function calls are properly attached to the
   caller rather than the callee.
   - added a test case for this
 * --watch still opens a repl if compile errors happen.
2021-01-02 19:11:56 -07:00
Andrew Kelley
006e7f6805 stage2: re-use ZIR for comptime and inline calls
Instead of freeing ZIR after semantic analysis, we keep it around so
that it can be used for comptime calls, inline calls, and generic
function calls. ZIR memory is now managed by the Decl arena.

Debug dump() functions are conditionally compiled; only available in
Debug builds of the compiler.

Add a test for an inline function call.
2021-01-02 19:11:55 -07:00
Andrew Kelley
9362f382ab stage2: implement function call inlining in the frontend
* remove the -Ddump-zir thing. that's handled through --verbose-ir
 * rework Fn to have an is_inline flag without requiring any more memory
   on the heap per function.
 * implement a rough first version of dumping typed zir (tzir) which is
   a lot more helpful for debugging than what we had before. We don't
   have a way to parse it though.
 * keep track of whether the inline-ness of a function changes because
   if it does we have to go update callsites.
 * add compile error for inline and export used together.

inline function calls and comptime function calls are implemented the
same way. A block instruction is set up to capture the result, and then
a scope is set up that has a flag for is_comptime and some state if the
scope is being inlined.

when analyzing `ret` instructions, zig looks for inlining state in the
scope, and if found, treats `ret` as a `break` instruction instead, with
the target block being the one set up at the inline callsite.

Follow-up items:
 * Complete out the debug TZIR dumping code.
 * Don't redundantly generate ZIR for each inline/comptime function
   call. Instead we should add a new state enum tag to Fn.
 * comptime and inlining branch quotas.
 * Add more test cases.
2021-01-02 19:11:19 -07:00
Andrew Kelley
fea8659b82 stage2: comptime function calls
* Function calls that happen in a comptime scope get called at
   compile-time. We do this by putting the parameters in place as
   constant values and then running regular function analysis on the
   body.
 * Added `Scope.Block.dump()` for debugging purposes.
 * Fixed some code to call `identifierTokenString` rather than
   `tokenSlice`, making it work for `@""` syntax.
 * Implemented `Value.copy` for big integers.

Follow-up issues to tackle:
 * Adding compile errors to the callsite instead of the callee Decl.
 * Proper error notes for "called from here".
   - Related: #7555
 * Branch quotas.
 * ZIR support?
2021-01-02 19:10:11 -07:00
Andrew Kelley
fb37c1b091 Merge branch 'LemonBoy-revive-6680'
closes #6870
2021-01-02 19:03:37 -07:00
Andrew Kelley
974c008a0e convert more {} to {d} and {s} 2021-01-02 19:03:14 -07:00