7042 Commits

Author SHA1 Message Date
Andrew Kelley
11330cbcc5
Merge pull request #10201 from Snektron/stage2-more-coercion
stage2: more in-memory coercion
2021-11-22 19:00:30 -05:00
Jakub Konka
691090f342 zld: parse ObjC ivars and eh_types in tapi v3 and v4 2021-11-22 18:01:15 +01:00
Jakub Konka
e17c4a497f zld: parse []TbdV3 before TbdV3 2021-11-22 18:01:15 +01:00
Jakub Konka
40e49fe2f0 zld: add missing Tbdv3 fields to the declarative struct 2021-11-22 18:01:15 +01:00
Stephen Gutekanst
9836f1b2f9
add support for compiling Objective-C++ code (#10096)
* add support for compiling Objective-C++ code

Prior to this change, calling `step.addCSourceFiles` with Obj-C++ file extensions
(`.mm`) would result in an error due to Zig not being aware of that extension.
Clang supports an `-ObjC++` compilation mode flag, but it was only possible to use
if you violated standards and renamed your `.mm` Obj-C++ files to `.m` (Obj-C) to
workaround Zig being unaware of the extension.

This change makes Zig aware of `.mm` files so they can be compiled, enabling compilation
of projects such as [Google's Dawn WebGPU](https://dawn.googlesource.com/dawn/) using
a `build.zig` file only.

Helps hexops/mach#21

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>

* test/standalone: add ObjC++ compilation/linking test

Based on the existing objc example, just tweaked for ObjC++.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2021-11-22 08:44:49 +01:00
Andrew Kelley
722c6b9567
Merge pull request #10188 from Luukdegram/stage2-wasm-stack
stage2: wasm - implement the stack
2021-11-21 22:09:47 -05:00
Andrew Kelley
6afcaf4a08 stage2: fix the build for 32-bit architectures
* Introduce a mechanism into Sema for emitting a compile error when an
   integer is too big and we need it to fit into a usize.
 * Add `@intCast` where necessary
 * link/MachO: fix an unnecessary allocation when all that was happening
   was appending zeroes to an ArrayList.
 * Add `error.Overflow` as a possible error to some codepaths, allowing
   usage of `math.intCast`.

closes #9710
2021-11-21 19:43:08 -07:00
Robin Voetter
cb248898ab sema: error union in-memory coercion 2021-11-22 03:21:31 +01:00
Robin Voetter
83a0329c92 sema: move error set coercion to coerceInMemoryAlloed 2021-11-22 03:04:55 +01:00
Robin Voetter
41b37712ea sema: function (pointer) in-memory coercion 2021-11-22 03:04:55 +01:00
Andrew Kelley
96e5f661bd zig cc: add -fcolor-diagnostics and -fcaret-diagnostics integration
Closes #6290
2021-11-21 17:07:18 -07:00
Andrew Kelley
ceeb230c23 zig cc: add -ffunction-sections integration
Also update to latest LLVM 13 command line options.

See #6290
2021-11-21 17:01:07 -07:00
Luuk de Gram
deb8d0765b
wasm: Fix text cases and add pointer test cases
Ensure all previous test cases are still passing, as well as add some basic tests for now
for testing pointers to the stack.

This means we can start implementing wasm's C ABI found at: https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md

We also simplified the block logic by always using 'void' block types and instead writing the value to a local,
which can then be referenced by continues instructions, as done currently by AIR.
Besides this, we also no longer need to insert blocks at an offset, as we simply write the saved temporary
after we create the block.
2021-11-21 21:07:55 +01:00
Luuk de Gram
ec5220405b
wasm: Implement optionals and ensure correct alignment
Rather than writing the alignment in its natural form, wasm binaries encode the alignment of types as the exponent of a power of 2.
So rather than performing this encoding during AIR->MIR, we do this while emitting MIR->binary encoding.
This allows us to keep alignment logic to its natural form while doing calculations (Which is what we need during linking as well).

We also implement optionals and pointers to an optional.
2021-11-21 21:07:55 +01:00
Luuk de Gram
460b3d39ea
wasm: Implement error unions as well as returning them
This implements basic calling convention resolving. This means that for
types such as an error union, we will now allocate space on the stack to store the result.
This result will then be saved in a temporary local at the callsite.
2021-11-21 21:07:55 +01:00
Luuk de Gram
b2221e5644
wasm: Implement structs stored on the stack
By calculating the abi size of the struct, we move the stack
pointer and store each field depending on its size (i.e. a 1-byte field will use i32.store8).
This commit adds all required opcodes to perform those stores and loads.

This also gets rid of `mir_offset` as we now save results of binary operations into
locals and emit its result onto the stack within condbr instead. This makes everything a lot
simpler but also more robust.
In the future, we could look into an algorithm to re-use such locals.

For struct fields we use the new `local_with_offset` tag. This stores the struct's
stack pointer as well as the field's offset from that stack pointer.

`allocLocal` will now always allocate a single local, using a given type.
2021-11-21 21:07:55 +01:00
Luuk de Gram
261f13414b
wasm: Implement emulated stack
All non-temporary locals will now use stack memory.
When `airAlloc` is called, we create a new local, move the stack pointer,
and write its offset into the local. Arguments act as a register and do not
use any stack space.

We no longer use offsets for binary operations, but instead write the result
into a local. In this case, the local is simply used as a register, and does not
require stack space. This allows us to ensure the order of instructions is correct,
and we no longer require any patching/inserting at a specific offset.

print_air was missing the logic to print the type of a `ty_str`.
2021-11-21 21:07:54 +01:00
Luuk de Gram
c18bc08e3c
wasm: Linker - emit stack pointer
The self-hosted wasm linker now emits a mutable global.
This entry represents the stack pointer, which has an initial value of offset table size + data size + stack size.
Stack size can either be set by the user, or has the default of a single wasm page (64KiB).
2021-11-21 21:07:51 +01:00
Jakub Konka
05330bbe0d stage2: ensure 16byte stack alignment on macOS x86_64 2021-11-21 20:43:10 +01:00
Jakub Konka
110c185886 stage2: add x86_64 NOP MIR inst and lowering
If we don't touch the stack, ellide `sub rsp, imm32` to `nop`.
2021-11-21 20:43:10 +01:00
Evan Haas
c6b4fe0066 translate-c: coerce boolean results to c_int when negated
Fixes #10175
2021-11-20 19:52:06 -05:00
Andrew Kelley
4e5a88b288 stage2: default dynamic libraries to be linked as needed
After this change, the default for dynamic libraries (`-l` or
`--library`) is to only link them if they end up being actually used.

With the Zig CLI, the new options `-needed-l` or `--needed-library` can
be used to force link against a dynamic library.

With `zig cc`, this behavior can be overridden with `-Wl,--no-as-needed`
(and restored with `-Wl,--as-needed`).

Closes #10164
2021-11-20 17:23:44 -07:00
Evan Haas
3fefdc1a0b translate-c: Allow negative denominator in remainder (%) operator
Fixes #10176
2021-11-20 13:52:07 -05:00
joachimschmidt557
e8112f7744 stage2 RISCV64: implement basic function prologue and epilogue 2021-11-20 19:25:59 +01:00
Jakub Konka
ad5533fdfb stage2,x86_64: revert fixing callee preserved regs
This will require further rework in the codegen so reverting for now.
2021-11-19 22:18:56 +01:00
Jakub Konka
bc59a630ab stage2,x86_64: fix genBinMathOp and clarify callee-saved regs
Previously, we have confused callee-saved with caller-saved registers
(the actual register sets were swapped). This commit fixes that
for both `.x86` and `.x86_64` native backends.

This commit also fixes the register allocation logic in `genBinMathOp`
for `.x86_64` native backend where in a situation such that we require
to spill a register, we would end up spilling the register that is
already involved in the instruction as the other operand. In such a
case, we make a note of this and spill a subsequent register instead.
2021-11-19 20:01:35 +01:00
Jacob G-W
149bc79486 add tests for previous commit 2021-11-19 09:38:36 +01:00
Jacob G-W
8fc3a707a4 x86_64/Emit: implement restoring callee_preserved_registers 2021-11-19 09:38:36 +01:00
Jacob G-W
f950d763a1 stage2 x86_64 codegen: don't count return registers as callee-preserved 2021-11-19 09:38:36 +01:00
Jakub Konka
6cf8a49bb0 macho: sync .variable decl handling with elf linker 2021-11-18 21:41:44 +01:00
Jakub Konka
7747bf07c7 stage2: use correct register alias for mem operands
When loading/storing on the stack, use `registerAlias` to correctly
workout the size of the register and thus correctly select target
instruction.
2021-11-18 19:46:56 +01:00
joachimschmidt557
d94b032e92 stage2 ARM: Introduce MIR 2021-11-16 19:52:21 -05:00
Andrew Kelley
09588c795c stage2: LLVM backend: memset to 0xaa for undefined stores
Also support `one` and `int_big_positive` tags for const pointers.
2021-11-16 17:46:39 -07:00
drew
a1d7604162 correct misnamed variables caused by copy-paste 2021-11-16 16:51:31 -07:00
drew
f33af8f071 fix array airStoreUndefined for arrays 2021-11-16 16:51:31 -07:00
drew
cf99afc525 add generics behavior test
-airLoad and airStore now properly report an error if they are used with an array, instead of having the C compiler emit a vague error
-airStoreUndefined now works with array types
-structFieldPtr now works with array types, allowing generics' tests to pass
2021-11-16 16:51:31 -07:00
drew
3896de3078 simplify things 2021-11-16 16:51:31 -07:00
drew
0300ec4ef7 fix assumption where all positive big ints are unsigned 2021-11-16 16:51:31 -07:00
drew
dffa6dcaf9 make it more clear we should do UB wrapping optimizations for ptr arithmetic 2021-11-16 16:51:31 -07:00
drew
ad4627ea3b small changes + align tests obviously shouldn't have passed 2021-11-16 16:51:31 -07:00
drew
34684725aa fmt 2021-11-16 16:51:31 -07:00
drew
9bf1681990 C backend: basic big ints, fix airPtrToInt, array references, pointer arithmetic UB with NULL, implement airPtrElemPtr/Val, fix redundant indirection/references with arrays
-add additional test cases that were found to be passing
-add basic int128 test cases which previously did not pass but weren't covered
-most test cases in cast.zig now pass
-i128/u128 or smaller int constants can now be rendered
-unsigned int constants are now always suffixed with 'u' to prevent random compile errors
-pointers with a val tag of 'zero' now just emit a 0 constant which coerces to the pointer type and fixes some warnings with ordered comparisons
-pointers with a val tag of 'one' are now casted back to the pointer type
-support pointers with a u64 val
-fix bug where rendering an array's type will emit more indirection than is needed
-render uint128_t/int128_t manually when needed
-implement ptr_add/sub AIR handlers manually so they manually cast to int types which avoids UB if the result or ptr operand is NULL
-implement airPtrElemVal/Ptr
-airAlloc for arrays will not allocate a ref as the local for the array is already a reference/pointer to the array itself
-fix airPtrToInt by casting to the int type
2021-11-16 16:51:31 -07:00
LemonBoy
952d865bd2 stage1: Fix caching of LLVM builtin fns
The cache entry must take into account the fact some functions operate on scalar types and some other on vectors of scalar types.

Fixes #10147
2021-11-16 16:35:56 -05:00
Andrew Kelley
85e427e4b2 stage2: equality compare optional with non-optional 2021-11-16 14:01:07 -07:00
mlarouche
4eaf3c665e Fix linking errors with floorl on MSVC by including math.h in a .cpp file. floorl is a force inline function defined in a header thus not visible for the linker. 2021-11-16 15:02:16 -05:00
Andrew Kelley
ed2e4a7f13 disable LTO by default on Windows when linking libc++
See #8531
2021-11-16 12:52:05 -07:00
Andrew Kelley
eecb669074 libcxx: disable redundant new/delete definitions
which are already provided by libcxxabi
2021-11-16 12:44:25 -07:00
Andrew Kelley
08c768ad82 pre-merge cleanups
* Annotate workarounds with their corresponding GitHub issue links.
 * Enable test coverage for LTO on Windows with the added c_compiler test.
2021-11-15 16:32:15 -07:00
xavier
564629f704 build: workaround link error with LTO and mingw
The symbol "_tls_index" gets lost when using LTO.
Disabling LTO on the object file that defines it allows the link to work.

fixes https://github.com/ziglang/zig/issues/8531
2021-11-15 16:15:39 -07:00
J.C. Moyer
33889e1974 mingw-w64: add missing file frexp.c to mingwex.lib
Fixes a frexp() segfault on Windows.
2021-11-15 17:28:49 -05:00