1105 Commits

Author SHA1 Message Date
Andrew Kelley
4f085b8d2c
result location semantics for error union wrapping a payload 2019-06-10 16:55:07 -04:00
Andrew Kelley
eaa9d8bdac
result location semantics for optional wrap 2019-06-10 16:20:13 -04:00
Andrew Kelley
c362895116
result location semantics for slices
```zig
export fn entry() void {
    var buf: [10]u8 = undefined;
    const slice1: []const u8 = &buf;
    const slice2 = buf[0..];
}
```

```llvm
define void @entry() #2 !dbg !35 {
Entry:
  %buf = alloca [10 x i8], align 1
  %slice1 = alloca %"[]u8", align 8
  %slice2 = alloca %"[]u8", align 8
  %0 = bitcast [10 x i8]* %buf to i8*, !dbg !46
  call void @llvm.memset.p0i8.i64(i8* align 1 %0, i8 -86, i64 10, i1 false), !dbg !46
  call void @llvm.dbg.declare(metadata [10 x i8]* %buf, metadata !39, metadata !DIExpression()), !dbg !46
  %1 = getelementptr inbounds %"[]u8", %"[]u8"* %slice1, i32 0, i32 0, !dbg !47
  %2 = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0, !dbg !47
  store i8* %2, i8** %1, align 8, !dbg !47
  %3 = getelementptr inbounds %"[]u8", %"[]u8"* %slice1, i32 0, i32 1, !dbg !47
  store i64 10, i64* %3, align 8, !dbg !47
  call void @llvm.dbg.declare(metadata %"[]u8"* %slice1, metadata !44, metadata !DIExpression()), !dbg !48
  %4 = getelementptr inbounds %"[]u8", %"[]u8"* %slice2, i32 0, i32 0, !dbg !49
  %5 = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0, !dbg !49
  store i8* %5, i8** %4, align 8, !dbg !49
  %6 = getelementptr inbounds %"[]u8", %"[]u8"* %slice2, i32 0, i32 1, !dbg !49
  store i64 10, i64* %6, align 8, !dbg !49
  call void @llvm.dbg.declare(metadata %"[]u8"* %slice2, metadata !45, metadata !DIExpression()), !dbg !50
  ret void, !dbg !51
}
```
2019-06-10 15:49:45 -04:00
Andrew Kelley
17b1ac5d03
result location semantics for @bitCast
```zig
export fn entry() void {
    var x = @bitCast(f32, foo());
}
```

```llvm
define void @entry() #2 !dbg !35 {
Entry:
  %x = alloca float, align 4
  %0 = bitcast float* %x to %Foo*, !dbg !42
  call fastcc void @foo(%Foo* sret %0), !dbg !42
  call void @llvm.dbg.declare(metadata float* %x, metadata !39, metadata !DIExpression()), !dbg !43
  ret void, !dbg !44
}
```
2019-06-10 12:24:19 -04:00
Andrew Kelley
1a51bf6304
hook up result locations for union initializations
```zig
export fn entry() void {
    var x = Foo{ .bar = bar() };
}
```

```llvm
define void @entry() #2 !dbg !44 {
Entry:
  %x = alloca %Foo, align 4
  %0 = getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 1, !dbg !68
  store i1 true, i1* %0, align 1, !dbg !68
  %1 = getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 0, !dbg !68
  %2 = bitcast { i32, [4 x i8] }* %1 to %Bar*, !dbg !68
  call fastcc void @bar(%Bar* sret %2), !dbg !68
  call void @llvm.dbg.declare(metadata %Foo* %x, metadata !48, metadata !DIExpression()), !dbg !69
  ret void, !dbg !70
}
```
2019-06-10 11:15:32 -04:00
Andrew Kelley
4e2b2822f1
inferred array size of array literals works 2019-06-09 19:55:15 -04:00
Andrew Kelley
3a4b749c8a
Merge remote-tracking branch 'origin/master' into copy-elision-3 2019-06-09 19:44:01 -04:00
Andrew Kelley
771e88951a
result location mechanism for struct initialization
```zig
export fn entry() void {
    const static = Foo{
        .x = 9,
        .bar = Bar{ .y = 10 },
    };
    const runtime = foo(true);
}

fn foo(c: bool) Foo {
    return Foo{
        .x = 12,
        .bar = if (c) bar1() else bar2(),
    };
}

fn bar1() Bar {
    return Bar{ .y = 34 };
}

fn bar2() Bar {
    return Bar{ .y = 56 };
}
```

```llvm
@0 = internal unnamed_addr constant %Foo { i32 9, %Bar { i32 10 } }, align 4
@1 = internal unnamed_addr constant %Bar { i32 34 }, align 4
@2 = internal unnamed_addr constant %Bar { i32 56 }, align 4

define void @entry() #2 !dbg !35 {
Entry:
  %runtime = alloca %Foo, align 4
  call void @llvm.dbg.declare(metadata %Foo* @0, metadata !39, metadata !DIExpression()), !dbg !50
  call fastcc void @foo(%Foo* sret %runtime, i1 true), !dbg !51
  call void @llvm.dbg.declare(metadata %Foo* %runtime, metadata !49, metadata !DIExpression()), !dbg !52
  ret void, !dbg !53
}

define internal fastcc void @foo(%Foo* nonnull sret, i1) unnamed_addr #2 !dbg !54 {
Entry:
  %c = alloca i1, align 1
  store i1 %1, i1* %c, align 1
  call void @llvm.dbg.declare(metadata i1* %c, metadata !60, metadata !DIExpression()), !dbg !61
  %2 = getelementptr inbounds %Foo, %Foo* %0, i32 0, i32 0, !dbg !62
  store i32 12, i32* %2, align 4, !dbg !62
  %3 = getelementptr inbounds %Foo, %Foo* %0, i32 0, i32 1, !dbg !64
  %4 = load i1, i1* %c, align 1, !dbg !65
  br i1 %4, label %Then, label %Else, !dbg !65

Then:                                             ; preds = %Entry
  call fastcc void @bar1(%Bar* sret %3), !dbg !66
  br label %EndIf, !dbg !64

Else:                                             ; preds = %Entry
  call fastcc void @bar2(%Bar* sret %3), !dbg !67
  br label %EndIf, !dbg !64

EndIf:                                            ; preds = %Else, %Then
  ret void, !dbg !68
}

define internal fastcc void @bar1(%Bar* nonnull sret) unnamed_addr #2 !dbg !69 {
Entry:
  %1 = bitcast %Bar* %0 to i8*, !dbg !73
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %1, i8* align 4 bitcast (%Bar* @1 to i8*), i64 4, i1 false), !dbg !73
  ret void, !dbg !73
}

define internal fastcc void @bar2(%Bar* nonnull sret) unnamed_addr #2 !dbg !75 {
Entry:
  %1 = bitcast %Bar* %0 to i8*, !dbg !76
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %1, i8* align 4 bitcast (%Bar* @2 to i8*), i64 4, i1 false), !dbg !76
  ret void, !dbg !76
}

!39 = !DILocalVariable(name: "static", scope: !40, file: !5, line: 2, type: !41)
!49 = !DILocalVariable(name: "runtime", scope: !40, file: !5, line: 6, type: !41)
```
2019-06-08 18:51:31 -04:00
Andrew Kelley
5784631fab
update the default macos version min to 10.14 2019-06-07 12:20:02 -04:00
LemonBoy
80cd142c96 Propagate DIFlags to LLVM 2019-06-04 09:05:33 +02:00
LemonBoy
ebe921e48f Make void a signed type
Follow the convention set by C so that lldb stops complaining about it.
2019-06-04 09:05:12 +02:00
Andrew Kelley
143d6ada8f
no-copy semantics for for loops
Note that only the index variable requires a stack allocation, and the
memcpy for the element is gone.

```zig
export fn entry() void {
    var buf: [10]i32 = undefined;
    for (buf) |x| {}
}
```

```llvm
define void @entry() #2 !dbg !35 {
Entry:
  %buf = alloca [10 x i32], align 4
  %i = alloca i64, align 8
  %0 = bitcast [10 x i32]* %buf to i8*, !dbg !47
  call void @llvm.memset.p0i8.i64(i8* align 4 %0, i8 -86, i64 40, i1 false), !dbg !47
  call void @llvm.dbg.declare(metadata [10 x i32]* %buf, metadata !39, metadata !DIExpression()), !dbg !47
  store i64 0, i64* %i, align 8, !dbg !48
  call void @llvm.dbg.declare(metadata i64* %i, metadata !45, metadata !DIExpression()), !dbg !48
  br label %ForCond, !dbg !48

ForCond:                                          ; preds = %ForBody, %Entry
  %1 = load i64, i64* %i, align 8, !dbg !48
  %2 = icmp ult i64 %1, 10, !dbg !48
  br i1 %2, label %ForBody, label %ForEnd, !dbg !48

ForBody:                                          ; preds = %ForCond
  %3 = getelementptr inbounds [10 x i32], [10 x i32]* %buf, i64 0, i64 %1, !dbg !48
  call void @llvm.dbg.declare(metadata i32* %3, metadata !46, metadata !DIExpression()), !dbg !49
  %4 = add nuw i64 %1, 1, !dbg !48
  store i64 %4, i64* %i, align 8, !dbg !48
  br label %ForCond, !dbg !48

ForEnd:                                           ; preds = %ForCond
  ret void, !dbg !50
}
```
2019-06-03 21:40:56 -04:00
Andrew Kelley
d4054e35fe
while loops
Note that neither the payload capture variable nor the error capture
variable require a stack allocation.

```zig
export fn entry() void {
    var c: anyerror!i32 = 1234;
    while (c) |hi| {} else |e| {}
}
```

```llvm
define void @entry() #2 !dbg !39 {
Entry:
  %c = alloca { i16, i32 }, align 4
  %0 = bitcast { i16, i32 }* %c to i8*, !dbg !52
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %0, i8* align 4 bitcast ({ i16, i32 }* @0 to i8*), i64 8, i1 false), !dbg !52
  call void @llvm.dbg.declare(metadata { i16, i32 }* %c, metadata !43, metadata !DIExpression()), !dbg !52
  br label %WhileCond, !dbg !53

WhileCond:                                        ; preds = %WhileBody, %Entry
  %1 = getelementptr inbounds { i16, i32 }, { i16, i32 }* %c, i32 0, i32 0, !dbg !54
  %2 = load i16, i16* %1, align 2, !dbg !54
  %3 = icmp ne i16 %2, 0, !dbg !54
  br i1 %3, label %WhileElse, label %WhileBody, !dbg !54

WhileBody:                                        ; preds = %WhileCond
  %4 = getelementptr inbounds { i16, i32 }, { i16, i32 }* %c, i32 0, i32 1, !dbg !53
  call void @llvm.dbg.declare(metadata i32* %4, metadata !50, metadata !DIExpression()), !dbg !53
  br label %WhileCond, !dbg !53

WhileElse:                                        ; preds = %WhileCond
  %5 = getelementptr inbounds { i16, i32 }, { i16, i32 }* %c, i32 0, i32 0, !dbg !55
  call void @llvm.dbg.declare(metadata i16* %5, metadata !51, metadata !DIExpression()), !dbg !55
  ret void, !dbg !56
}
```
2019-06-03 20:56:22 -04:00
Andrew Kelley
b6108eed52
fix alignment of consts 2019-06-03 19:11:41 -04:00
Andrew Kelley
eb8a132d23
var types, alignment, and comptime 2019-06-03 17:46:58 -04:00
Andrew Kelley
ccce3d8526
no-copy semantics for function forwarding
```zig
fn foo() Foo {
    return bar();
}
```

```llvm
define internal fastcc void @foo(%Foo* nonnull sret) unnamed_addr #2 !dbg !48 {
Entry:
  call fastcc void @bar(%Foo* sret %0), !dbg !52
  ret void, !dbg !54
}
```
2019-05-31 01:36:57 -04:00
Andrew Kelley
a4aca78722
no-copy semantics for if expr
```zig
export fn entry() void {
    var c = true;
    var x = if (c) u8(4) else u32(10);
}
```

```llvm
define void @entry() #2 !dbg !35 {
Entry:
  %c = alloca i1, align 1
  %x = alloca i32, align 4
  store i1 true, i1* %c, align 1, !dbg !44
  call void @llvm.dbg.declare(metadata i1* %c, metadata !39, metadata !DIExpression()), !dbg !45
  %0 = load i1, i1* %c, align 1, !dbg !46
  br i1 %0, label %Then, label %Else, !dbg !46

Then:                                             ; preds = %Entry
  br label %EndIf, !dbg !47

Else:                                             ; preds = %Entry
  br label %EndIf, !dbg !47

EndIf:                                            ; preds = %Else, %Then
  %1 = phi i32 [ 4, %Then ], [ 10, %Else ], !dbg !47
  store i32 %1, i32* %x, align 4, !dbg !47
  call void @llvm.dbg.declare(metadata i32* %x, metadata !42, metadata !DIExpression()), !dbg !48
  ret void, !dbg !49
}
```
2019-05-30 23:16:11 -04:00
Andrew Kelley
5e1003bc81
no-copy semantics for basic runtime function call variable init
```zig
export fn entry() void {
    var x: Foo = foo();
}
```

```llvm
define void @entry() #2 !dbg !37 {
Entry:
  %x = alloca %Foo, align 4
  call fastcc void @foo(%Foo* sret %x), !dbg !48
  call void @llvm.dbg.declare(metadata %Foo* %x, metadata !41, metadata !DIExpression()), !dbg !49
  ret void, !dbg !50
}
```
2019-05-30 17:11:14 -04:00
tgschultz
f9e7bd2682 std.meta/trait: def/definition => decl/declaration
TypeInfo: defs/Definition => decls/Declarations
2019-05-29 20:43:07 -04:00
Andrew Kelley
3819654c39
codegen: initialize subsystem 2019-05-29 16:55:02 -04:00
Andrew Kelley
1ab0ac3ea2
cleanups for windows subsystem in builtin.zig 2019-05-29 16:55:02 -04:00
emekoi
b461e600e2
set subsystem to null if not on windows or uefi 2019-05-29 16:55:01 -04:00
emekoi
6bc5b07e3e
try to resolve TargetSubSystemAuto to actual subsystem 2019-05-29 16:55:01 -04:00
emekoi
fb5dc28921
added subsystem to builtin.zig 2019-05-29 16:54:59 -04:00
Andrew Kelley
b66438eb80
no "use of undeclared identifer" in dead comptime branches 2019-05-28 18:19:27 -04:00
Andrew Kelley
9d16839420
fix invalid LLVM IR generated for ?*void const casts
closes #2578
2019-05-28 16:11:36 -04:00
LemonBoy
e2c2263434 Move __zig_fail_unwrap locals on stack 2019-05-28 13:02:45 -04:00
emekoi
3f302594b8 respect subsystem flag in all cases 2019-05-27 22:15:33 -04:00
Andrew Kelley
f924fbddcf
Merge pull request #2552 from Sahnvour/issue-2543
gen-h: do not output visibility macros when the build is static
2019-05-27 21:51:34 -04:00
Andrew Kelley
2c0280ba08
improve the stack check CLI options
See #2526
2019-05-27 20:59:19 -04:00
Andrew Kelley
269a53b6af
introduce @hasDecl builtin function
closes #1439
2019-05-26 16:21:03 -04:00
Sahnvour
99ee0608f7 gen-h: do not output visibility macros when the build is static 2019-05-25 14:17:59 +02:00
LemonBoy
0a3aec020a Fix load/store of non-integer fields in packed struct 2019-05-18 19:59:47 -04:00
Andrew Kelley
80983ca1ca
fixups to the previous commit 2019-05-16 16:37:58 -04:00
Shawn Landden
1fdb24827f
breaking changes to all bit manipulation intrinsics
* `@clz`, `@ctz`, `@popCount`, `@bswap`, `@bitreverse` now
   have a type parameter
 * rename @bitreverse to @bitReverse
 * rename @bswap to @byteSwap

Closes #2119
Closes #2120
2019-05-16 16:37:58 -04:00
Andrew Kelley
81e960eb74
improvements to build-lib use case of WebAssembly
* build-exe does include the startup code that supplies _start for the
   wasm32-freestanding target. Previously this did not occur because
   of logic excluding "freestanding".
 * build-lib for wasm32-freestanding target gets linked by LLD. To avoid
   infinite recursion, compiler_rt and zig libc are built as objects
   rather than libraries.
   - no "lib" prefix and ".wasm" extension instead of ".a". Rather than
   build-lib foo.zig producing "libfoo.a", now it produces "foo.wasm".
 * go back to using `.o` extension for webassembly objects
 * zig libc only provides _start symbol for wasm when linking libc.
2019-05-16 13:56:56 -04:00
Andrew Kelley
6b36b756eb
fix static builds of zig from requiring c compiler
to be installed when linking libc.

When zig links against libc, it requires a dynamic linker path.
Usually this can be determined based on the architecture and operating
system components of the target. However on some systems this is not
correct; because of this zig checks its own dynamic linker.

When zig is statically linked, this information is not available, and so
it resorts to using cc -print-filename=foo to find the dynamic linker
path.

Before this commit, Zig incorrectly exited with an error if there was
no c compiler installed. Now, Zig falls back to the dynamic linker
determined based on the arch and os when no C compiler can be found.
2019-05-15 21:50:56 -04:00
Andrew Kelley
df4f77024e
else value when switching on error set has
optional capture value which is subset.

see #769
2019-05-14 18:06:57 -04:00
LemonBoy
6536b409df Don't emit DW_TAG_lexical_block for VarDecls 2019-05-14 14:34:49 -04:00
Andrew Kelley
7330a6102f
cache_add_dep_file: handle ErrorFileNotFound specially 2019-05-13 12:15:55 -04:00
LemonBoy
a038ef3570 Assemble asm files using CC
Stuffing all the files together and compiling the resulting blob with
the main program is a terrible idea.

Some files, namely the .S ones, must be run trough the C preprocessor
before assembling them (#2437).

Beside that the aggregate may be mis-compiled due to the presence of
some flags that affect the following code.

For example let's consider two files, a.s and b.s

a.s
```
fn1:
    ret
.data
data1:
    .word 0
```

b.s
```
fn2:
    ret
```

Now, fn1 and fn2 will be both placed in the .text section as intended if
the two files are compiled separately. But if we merge them the `.data`
flag ends up placing fn2 in the wrong section!

This fixes a nasty crash where musl's memset ended up in the
non-executable data segment, leading to too many hours of
head-scratching.
2019-05-13 16:41:07 +02:00
Andrew Kelley
a7346ea49f fix build on macOS
Sadly due to a workaround for LLD linker limitations on macOS
we cannot put libuserland into an .a file; instead we have to use object
files. Again due to linker limitations, bundling compiler_rt.o into
another relocatable object also doesn't work. So we're left with
disabling stack probing on macOS for the stage1 self-hosted code.

These workarounds could all be removed if the macos support in the LLD
linker improved, or if Zig project had its own linker that did not have
these issues.
2019-05-08 22:45:49 -04:00
Andrew Kelley
9bbd71c9ab
add --bundle-compiler-rt function to link options
and use it when building libuserland.a

The self-hosted part of stage1 relies on zig's compiler-rt, and so we
include it in libuserland.a.

This should potentially be the default, but for now it's behind a linker
option.

self-hosted translate-c: small progress on translating functions.
2019-05-08 20:51:49 -04:00
Andrew Kelley
0099583bd3
C pointers support .? operator
see #1967
2019-05-08 17:39:00 -04:00
Andrew Kelley
50bbb34594
C pointers support null
See #1967
2019-05-08 16:06:34 -04:00
LemonBoy
be7cacfbbe Implement stack probes for x86/x86_64
Enabled on non-Windows systems only since it already requires stack
probes.
2019-05-08 12:36:54 -04:00
Andrew Kelley
bd9c629c4c
always respect threadlocal for variables with external linkage
Previously if you had, for example:

extern "c" threadlocal var errno: c_int;

This would turn errno into a normal variable for --single-threaded
builds. However for variables with external linkage, there is an ABI
to uphold.

This is needed to make errno work for DragonFly BSD. See #2381.
2019-05-03 14:39:54 -04:00
Andrew Kelley
3552180143
optimize @memset with undefined
When using `@memset` to set bytes to `undefined`, Zig notices this
case and does a single Valgrind client request rather than N.

Speeds up all allocators in safe modes.

Closes #2388
2019-05-03 01:25:03 -04:00
Andrew Kelley
e1bf74fca3
translate-c: put -x c back in there, it's necessary 2019-04-25 00:06:58 -04:00
Andrew Kelley
712274997e
translate-c: unify API for self-hosted and C++ translate-c
See #1964
2019-04-25 00:06:57 -04:00