4939 Commits

Author SHA1 Message Date
Jonathan Marler
31802c6c68 remove z/Z format specifiers
Zig's format system is flexible enough to add custom formatters.  This PR removes the new z/Z format specifiers that were added for printing Zig identifiers and replaces them with custom formatters.
2021-01-07 23:49:22 -08:00
Jay Petacat
a9b505fa77 Reduce use of deprecated IO types
Related: #4917
2021-01-07 23:48:58 -08:00
Jakub Konka
70771283c5 Want native headers when linking with frameworks
This PR ensures we use system libc headers and system search paths for
framework headers when linking against frameworks and compiling natively
on macOS.
2021-01-07 23:47:37 -08:00
Andrew Kelley
5ee0431527 stage2: update to new ArrayListHashMap API 2021-01-06 17:40:25 -07:00
Andrew Kelley
d7d905696c
Merge pull request #7622 from tetsuo-cpp/array-hash-map-improvements
std: Support equivalent ArrayList operations in ArrayHashMap
2021-01-06 16:32:23 -08:00
Andrew Kelley
76870a2265
Merge pull request #7700 from FireFox317/more-stage2-stuff-llvm
stage2: improvements to LLVM backend
2021-01-06 16:06:32 -08:00
joachimschmidt557
be6ac82ee1 stage2 ARM: fix stack offsets for genSetReg and genSetStack 2021-01-06 15:53:10 -08:00
joachimschmidt557
480d6182ad stage2 ARM: fix offsets in exitlude jump relocations 2021-01-06 15:53:10 -08:00
Andrew Kelley
efe94a9a12 stage2: C backend: support unused Decls 2021-01-06 16:47:09 -07:00
Timon Kruiper
b1cfa923be stage2: rename and move files related to LLVM backend 2021-01-06 10:52:20 +01:00
Timon Kruiper
31d1ec4c2f stage2: make use of llvm.Context in LLVM backend
This for example allows for multiple LLVM instances to run in parallel.

Also rename some functions in llvm_bindings.zig.

Fixes #7688
2021-01-06 10:52:20 +01:00
Timon Kruiper
5d5db833f2 stage2: hoist alloca instructions to top of function in LLVM backend
This way the generated code only has to setup the stack size at the
beginning of a function and this improves codegen.

Fixes #7689

```
fn foo() void {}

export fn hello(z: i8) void {
    var x: i16 = undefined;
    foo();
    var y: i32 = 1;
    y += z;
}
```
llvm-ir:
```
define void @hello(i8 %0) {
Entry:
  %1 = alloca i8, align 1
  %2 = alloca i16, align 2
  %3 = alloca i32, align 4
  store i8 %0, i8* %1, align 1
  %4 = load i8, i8* %1, align 1
  store i16 undef, i16* %2, align 2
  call void @foo()
  store i32 1, i32* %3, align 4
  %5 = load i32, i32* %3, align 4
  %6 = sext i8 %4 to i32
  %7 = add nsw i32 %5, %6
  store i32 %7, i32* %3, align 4
  ret void
}
```
2021-01-06 10:52:20 +01:00
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
Andrew Kelley
91e3431d4a stage2 test harness: don't try to run non-native C backend tests 2021-01-05 20:44:19 -07:00
g-w1
ab5f7b5156 stage2: add compile log statement 2021-01-05 18:43:41 -07:00
Andrew Kelley
3e39d0c44f minor fixups from moving identifiers and files around 2021-01-05 17:41:22 -07:00
Andrew Kelley
1a2dd85570 stage2: C backend: re-implement emit-h
and also mark functions as `extern "C"` as appropriate to support c++
compilers.
2021-01-05 17:41:14 -07:00
Andrew Kelley
cd95444e47 stage2: C backend: remove format() hackery
All C backend tests passing now, except for emit-h tests. Next task in
the branch is to restore emit-h.
2021-01-05 17:41:14 -07:00
Andrew Kelley
58cfaa5982 stage2: C backend: adjust spaces around functions 2021-01-05 17:41:14 -07:00
Andrew Kelley
e1811f72eb stage2: link.C: use pwritev 2021-01-05 17:41:14 -07:00
Andrew Kelley
7b8cede61f stage2: rework the C backend
* std.ArrayList gains `moveToUnmanaged` and dead code
   `ArrayListUnmanaged.appendWrite` is deleted.
 * emit_h state is attached to Module rather than Compilation.
 * remove the implementation of emit-h because it did not properly
   integrate with incremental compilation. I will re-implement it
   in a follow-up commit.
 * Compilation: use the .codegen_failure tag rather than
   .dependency_failure tag for when `bin_file.updateDecl` fails.

C backend:
 * Use a CValue tagged union instead of strings for C values.
 * Cleanly separate state into Object and DeclGen:
   - Object is present only when generating a .c file
   - DeclGen is present for both generating a .c and .h
 * Move some functions into their respective Object/DeclGen namespace.
 * Forward decls are managed by the incremental compilation frontend; C
   backend no longer renders function signatures based on callsites.
   For simplicity, all functions always get forward decls.
 * Constants are managed by the incremental compilation frontend. C
   backend no longer has a "constants" section.
 * Participate in incremental compilation. Each Decl gets an ArrayList
   for its generated C code and it is updated when the Decl is updated.
   During flush(), all these are joined together in the output file.
 * The new CValue tagged union is used to clean up using of assigning to
   locals without an additional pointer local.
 * Fix bug with bitcast of non-pointers making the memcpy destination
   immutable.
2021-01-05 17:41:14 -07:00
Noam Preil
9360e5887c integrate CBE with Compilation.update pipeline (closes #7589)
* CBE buffers are only valid during a flush()
* the file is reopened and truncated during each flush()
* CBE now explicitly ignores updateDecl and deleteDecl
* CBE updateDecl is gone
* test case is enabled
2021-01-05 17:41:14 -07:00
Alex Cameron
d92ea56884 std: Support equivalent ArrayList operations in ArrayHashMap 2021-01-06 00:55:51 +11:00
Alex Cameron
89286376c6 std: Rename ArrayList shrink => shrinkAndFree 2021-01-06 00:55:51 +11: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
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
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
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
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
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