Commit Graph

2798 Commits

Author SHA1 Message Date
Andrew Kelley
c4f53d1ef6 fix deadlock with build-exe on an object for windows
The steps to repro this issue are:

zig build-obj hello.zig -target x86_64-windows-msvc
zig build-exe hello.obj -target x86_64-windows-msvc --subsystem console
-lkernel32 -lntdll

What was happening is that the main Compilation added a work item to
produce kernel32.lib. Then it added a sub-Compilation to build zig's
libc, which ended up calling a function with extern "kernel32", which
caused the sub-Compilation to also try to produce kernel32.lib. The main
Compilation and sub-Compilation do not coordinate about the set of
import libraries that they will be trying to build, so this caused a
deadlock.

This commit solves the problem by disabling the extern "foo" feature
from working when building compiler_rt or libc. Zig's linker code is now
responsible for putting the appropriate import libs on the linker line,
if any for compiler_rt and libc.

Related: #5825
2020-12-11 18:34:34 -05:00
Evan Haas
55cac65f95 Support casting enums to all int types.
In C, enums are represented as signed integers, so casting from an enum to an integer
should use the "cast integer to integer" translation code path. Previously it used the
"cast enum to generic non-enum" code path, because enums were not being treated as integers.
Ultimately this can produce zig code that fails to compile if the destination type does not
support the full range of enum values (e.g. translated C code that casts an enum value to an
unsigned integer would fail to compile since enums are signed integers, and unsigned integers
cannot represent the full range of values that signed ones can).

One interesting thing that came up during testing is that the implicit enum-to-int cast that
occurs when an enum is used in a boolean expression was parsed as an (int) by some versions of
the zig compiler, and an (unsigned int) cast by others. Specifically, the following code:

```c
	enum Foo {Bar, Baz};
	// ...
	enum Foo foo = Bar;
	if (0 || foo) {
		// do something
	}
```

When tested on MacOS, Linux, and Windows using a compiler built from the Windows Zig Compiler
Dev Kit, the above code would emit a cast to c_uint:

`if (false or (@bitCast(c_uint, @enumToInt(foo)) != 0)) {}`

However when tested on Windows with a Zig compiler built using MSVC, it produces:

`if (false or (@bitCast(c_int, @enumToInt(foo)) != 0)) {}`

In this particular case I don't think it matters, since a c_int and c_uint will have the same
representation for zero, but I'm not sure if this is ultimately the result of
implementation-defined behavior or something else.

Because of this, I added explicit casts in the `translate_c.zig` tests, to ensure that the
emitted zig source exactly matches across platforms. I also added a behavior test in
`run_translated_c.zig` that uses the old implicit casts from `translate_c.zig` to ensure
that the emitted Zig code behaves the same as the C code regardless of what cast is used.
2020-12-10 15:47:56 -05:00
Vexu
73016212a3 translate-c: support referencing c containers in macros 2020-12-10 14:45:48 +02:00
Vexu
7e30e83900
small fixes and zig fmt 2020-12-09 13:54:26 +02:00
Andrew Kelley
4592fd26b9 add std.testing.expectStringEndsWith 2020-12-08 21:59:24 -07:00
LemonBoy
8ac711596d stage1: Validate the specified cc for lazy fn types
Closes #7337
2020-12-08 19:09:25 -05:00
Andrew Kelley
58c8ad8ea8 tests: run-translated-c now respects -Dtarget 2020-12-08 13:46:05 -07:00
g-w1
6294c1136c
stage2: variable shadowing detection (#6969) 2020-12-06 19:36:49 +02:00
Veikka Tuominen
0268f54fcf
Merge pull request #7313 from LemonBoy/booo
Fix a few unsound optimizations on single-element union/enum
2020-12-06 19:31:07 +02:00
LemonBoy
b45d2968e5 Add some test cases for the previous commits 2020-12-05 20:14:04 +01:00
Andrew Kelley
2ed1ed9b32 stage2: introduce Module.failed_root_source_file
Use case:

zig build-exe non_existent_file.zig

Previous behavior:

error.FileNotFound, followed by an error return trace

Behavior after this commit:

error: unable to read non_existent_file.zig: FileNotFound
(end of stderr, exit code 1)

This turns AllErrors.Message into a tagged union which now has the
capability to represent both "plain" errors as well as source-based
errors (with file, line, column, byte offset). The "no entry point found"
error has moved to be a plain error message.
2020-12-04 17:21:55 -07:00
Jakub Konka
b6b7a6401c
Merge pull request #7293 from kubkon/fix-7030
stage1: allow idx 0 err to be put into error_name_table
2020-12-04 16:29:38 +01:00
Jakub Konka
e3db2be899 Add minimal standalone test case 2020-12-04 12:43:58 +01:00
Andrew Kelley
afaef36194 stage1: compile error for pointer arithmetic on ptr-to-array
See #2018
2020-12-03 17:07:13 -07:00
Timon Kruiper
ac85e1f2c8 stage2: make sure to emit the ZIR instructions of exported functions
Previously the emitted ZIR code would not have the ZIR instructions
of the exported functions.
2020-12-03 11:46:35 -08:00
Andrew Kelley
429a219f42 stage2: fix not detecting all dynamic libraries
Positional shared library arguments were not being detected as causing
dynamic linking, resulting in invalid linker lines. LLD did not have an
error message for this when targeting x86_64-linux but it did emit an
error message when targeting aarch64-linux, which is how I noticed the
problem.

This surfaced an error having to do with fifo.pipe() in the cat example
which I did not diagnose but solved the issue by doing the revamp that
was already overdue for that example.

It appears that the zig-window project was exploiting the previous
behavior for it to function properly, so this prompts the question, is
there some kind of static/dynamic executable hybrid that the compiler
should recognize? Unclear - but we can discuss that in #7240.
2020-11-30 20:25:28 -07:00
Andrew Kelley
263f25fea6
Merge pull request #7116 from joachimschmidt557/stage2-arm
stage2 ARM: add basic arithmetic instructions
2020-11-29 10:43:29 -08:00
LemonBoy
df99cfdf1e stage1: Fix typeInfo generation for arrays w/o sentinel
ZigTypeIdOptional types have a different way of specifying their payload
value depending on whether the child type is a pointer or not (plus some
other special cases).

Fixes #7251
2020-11-29 10:39:10 -08:00
LemonBoy
c80d196094 stage1: Add missing bitcast when rendering var ptr
Some types require this extra bitcast, eg. structs or unions with extra
padding fields inserted by the compiler.

Fixes #7250
2020-11-29 10:37:06 -08:00
joachimschmidt557
ceebcb2b4d
stage2 ARM: add test case for addition 2020-11-28 23:26:17 +01:00
LemonBoy
f91df39ad2 stage1: Fix crash in *[N]T to []T conversion with zst
Prevent the crash by not making the codegen try to access the
non-existing ptr field in the slice.

Closes #6951
2020-11-27 14:33:26 -08:00
Jakub Konka
f125c4f5c7 stage2 macho: enable end-to-end incremental linking tests on aarch64 2020-11-26 11:50:09 +01:00
LemonBoy
58c2bec589 stage1: Fix ICE when generating struct fields with padding
Make gen_const_ptr_struct_recursive aware of the possible presence of
some trailing padding by always bitcasting the pointer to its expected
type.

Not an elegant solution but makes LLVM happy and is consistent with how
the other callsites are handling this case.

Fixes #5398
2020-11-25 15:36:33 -08:00
Andrew Kelley
500fbdad57 update stack trace test with new start.zig line number 2020-11-25 00:40:50 -07:00
LemonBoy
bfa7e5c743 Update stack_traces test 2020-11-23 12:36:03 +01:00
Andrew Kelley
bf0cc32aa6
Merge pull request #7165 from LemonBoy/ppc64final
Make the PPC64 port usable
2020-11-20 17:40:17 -08:00
LemonBoy
6029114f84 stage1: Resolve usingnamespace decls when calling @typeInfo
Closes #7176
2020-11-20 17:01:23 -08:00
LemonBoy
2193bbfd93 Skip f16 to f128 conversion test for ppc64
As for aarch64 we're waiting for LLVM to emit calls to the specific
builtins that implement this conversion.
2020-11-20 08:38:11 +01:00
LemonBoy
f2b4e6b2e7 Better coverage in @splat tests
Cover more common and uncommon cases.
2020-11-20 08:38:10 +01:00
Andrew Kelley
73be59433f
Merge pull request #6928 from data-man/reduce_tests
Add more tests for reduce
2020-11-19 17:48:18 -08:00
Veikka Tuominen
cf819b95fe
Merge pull request #6829 from tadeokondrak/error-unsupported-callconv
stage1: Compile error instead of falling back to C for unsupported cc
2020-11-19 19:03:08 +02:00
Tadeo Kondrak
25ec2dbc1e Add builtin.Signedness, use it instead of is_signed 2020-11-19 18:59:21 +02:00
Vexu
3a28b659bd
add compile-error tests for unsupported calling convention 2020-11-19 14:25:46 +02:00
Tadeo Kondrak
c002a5026a
Update code to not use unsupported calling conventions for target 2020-11-19 14:01:07 +02:00
LemonBoy
2b7781d82a stage1: Fix undefined assignment for bitfields
Prevents silent memory corruption.

Closes #7055
2020-11-18 21:49:39 -08:00
pfg
cf7de64f1a stage1: improve error for missing a number type on a runtime var 2020-11-18 21:45:51 +02:00
Veikka Tuominen
a6470088c6
Merge pull request #6434 from daurnimator/fifo.pump
std: add LinearFifo(...).pump(src_reader, dest_writer)
2020-11-18 16:35:13 +02:00
frmdstryr
a39d3155b4 Change error when runtime value passed to comptime arg 2020-11-18 13:33:45 +02:00
LemonBoy
129ccad434 stage1: Reject undefined values when taking union ptr
The code rightfully assumes the union_val object to be fully initialized.

Closes #7019
2020-11-18 13:21:36 +02:00
Veikka Tuominen
6d5b76a75d
Merge pull request #7005 from jshholland/deprecate-span
Remove ArrayList.span
2020-11-18 13:14:48 +02:00
LemonBoy
fa27420b72 stage1: Fix asyncCall with non-abi-aligned arguments
Make the code used to calculate the variable slot index into the frame
match what's done during the structure layout calculation.

Prevents a few nasty LLVM errors when such types are passed around.
2020-11-17 15:55:12 -08:00
data-man
86b86bef23 Added links to the relevant issue 2020-11-17 14:41:05 +05:00
Andrew Kelley
d9c36cb250
Merge pull request #6878 from frmdstryr/multiline-string-comments
Support comments in multiline string literals
2020-11-16 14:05:15 -08:00
Alexandros Naskos
6e2e747b0b
Merge pull request #7112 from LemonBoy/fix-7104
stage1: Fix generation of pass-by-value args in async fns
2020-11-16 09:12:42 +02:00
LemonBoy
7ebbc717c0 stage1: Fix generation of pass-by-value args in async fns
The mismatch between the argument slot type in the frame structure and
the one used in the store operation made the generated code write
garbage over the nearby fields.

Fixes #7104
2020-11-14 15:30:06 +01:00
LemonBoy
c4fd3fc270 stage1: Resolve ErrorUnion children types
Since the code is accessing the abi_size field compute the full type
size for both err_set_type and payload_type, not only for the latter.
2020-11-13 14:28:40 -07:00
Jakub Konka
51717314e4
Merge pull request #6900 from joachimschmidt557/stage2-aarch64
Add stage2 AArch64 backend
2020-11-12 20:41:15 +01:00
joachimschmidt557
b17859b568
stage2 AArch64: add Linux Hello World test 2020-11-11 23:29:18 +01:00
Jakub Konka
5b92d0ea45 stage2 aarch64: add macOS incremental test 2020-11-11 15:36:47 +01:00
Vexu
08270d72b4 ensure TypeInfo payload is not undefined 2020-11-11 16:04:46 +02:00