4962 Commits

Author SHA1 Message Date
LemonBoy
65b7d2b4e1 stage1: Resolve alignment earlier in @alignCast
Fixes #7744
2021-01-12 16:36:51 -08:00
Andrew Kelley
5b2a79848c stage2: cleanups regarding red zone CLI flags
* 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-11 22:07:21 -07:00
Lee Cannon
8932c2d745 Added support for no red zone 2021-01-11 22:07:14 -07:00
Andrew Kelley
025f1559a0
Merge pull request #7200 from Vexu/arr
Type coercion for pointers to anon literals
2021-01-11 16:09:28 -08:00
Andrew Kelley
29c9d5896c Merge branch 'Stage2 begin implementing container types' 2021-01-11 16:25:21 -07:00
Timon Kruiper
e1d8073d2f stage2: add support for loops in LLVM backend
A simple `while(true) {}` loop generates the following LLVMIR:
```
define i32 @main() {
Entry:
  br label %Loop

Loop:                                 ; preds = %Loop, %Entry
  br label %Loop
}
```

Also implement TZIR printing for loops and add a corresponding test.
2021-01-10 17:47:34 -08:00
joachimschmidt557
a7da90071e stage2: fix bug in genArg
When an argument is unused in the function body, still increment
arg_index so we still select the correct arguments in the args slice.
2021-01-10 00:41:02 -08:00
Andrew Kelley
29928af600
Merge pull request #7729 from jayschwa/remove-deprecated-stream
Remove deprecated stream aliases
2021-01-09 13:04:08 -08:00
Jay Petacat
a0ad2dee6a builtin: Add zig_version
This will enable code to perform version checks and make it easier to
support multiple versions of Zig.

Within the SemVer implementation, an intermediate value needed to be
coerced to a slice to workaround a comptime bug.

Closes #6466
2021-01-09 12:50:39 -08:00
Andrew Kelley
5c49a137d5
Merge pull request #7725 from FireFox317/even-more-llvm
stage2: initial implementation of control flow in LLVM backend + TZIR printing
2021-01-09 12:32:10 -08:00
xackus
e4b8148e9c Fix system library path detection on linux
Uses the NativeTargetInfo results instead of std.Target.current.
2021-01-08 20:07:59 -07:00
LemonBoy
eb696e453f stage2: Implicitly enable --eh_frame_hdr when compiling c/c++ files
Matches what Clang/GCC driver do.

Closes #7711
2021-01-08 19:01:05 -08:00
Jay Petacat
1595ce273e Remove deprecated stream aliases 2021-01-08 16:54:56 -05:00
Timon Kruiper
56c059077c stage2: add initial impl of control flow in LLVM backend
The following TZIR instrutions have been implemented in the backend:
- all cmp operators (lt, lte, gt, gte, eq, neq)
- block
- br
- condbr

The following LLVMIR is generated for a simple assert function:
```
define void @assert(i1 %0) {
Entry:
  %1 = alloca i1, align 1
  store i1 %0, i1* %1, align 1
  %2 = load i1, i1* %1, align 1
  %3 = xor i1 %2, true
  br i1 %3, label %Then, label %Else

Then:                                             ; preds = %Entry
  call void @llvm.debugtrap()
  unreachable

Else:                                             ; preds = %Entry
  br label %Block

Block:                                            ; preds = %Else
  ret void
}
```

See tests for more examples.
2021-01-08 19:30:52 +01:00
Timon Kruiper
3715ed7b54 stage2: implement TZIR printing for block and condbr
Zig code:
```
fn assert(ok: bool) void {
    if (!ok) unreachable;
}
```

TZIR:
```
Module.Function(name=assert):
  @0: void = {};
  %0: bool = arg(ok)
  %1: void = dbg_stmt()
  %2: void = block(
    %3: bool = not(%0)
    %4: noreturn = condbr(%3,
    then:
      %5: void = breakpoint()
      %6: noreturn = unreach()
    else:
      %7: noreturn = br(%2, @0)
    )
  )
  %8: noreturn = retvoid()
```
2021-01-08 12:13:27 +01:00
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
Timon Kruiper
36950815a4 stage2 test: make sure to pass the dynamic linker to Compilation
Because this was not set, the `-dynamic-path` argument was not passed
to LLD when linking an ELF binary. This would still generate a valid glibc
binary, however when executing would result in a segfault.
2021-01-07 11:37:52 +01: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