7505 Commits

Author SHA1 Message Date
Daniel Saier
2f41bd3be4 zig cc: Treat cu files as C++ source files
First step towards #10634.

Treating stub files as C++ allows to use zig c++ as a host
compiler for nvcc.

Treating cu files as C++ allow using zig c++ as a host compiler in
CMake. CMake calls the host compiler with -E on a cu file to identify
the compiler.

Using zig c++ to directly compile CUDA code is untested.
2022-01-27 19:23:55 +02:00
Andrew Kelley
82bd0ac572 Sema: implement struct init is_ref=true
Takes advantage of the pattern already established with
array_init_anon. Also upgrades array_init (non-anon) to the pattern.

Implements comptime struct value equality and pointer value hashing.
2022-01-26 20:02:01 -07:00
Andrew Kelley
50accb757f stage2: fix crash on file-level struct field source locations 2022-01-26 20:02:01 -07:00
Andrew Kelley
b463924da2
Merge pull request #10699 from motiejus/arm64
[linux headers] rename arm64 to aarch64
2022-01-26 20:51:59 -05:00
Andrew Kelley
40c9ce2caf zig cc: add --hash-style linker parameter
This is only relevant for ELF files.

I also fixed a bug where passing a zig source file to `zig cc` would
incorrectly punt to clang because it thought there were no positional
arguments.
2022-01-26 15:01:59 -07:00
joachimschmidt557
35503b3d3f stage2 regalloc: Add freezeRegs/unfreezeRegs API
The freeze/unfreeze API replaces the exceptions API for hopefully
preventing bugs in codegen code using the RegisterManager. The
exceptions API is still available for backwards compatibility and will
be removed once all backends transition to the new freeze/unfreeze
API.
2022-01-26 18:23:27 +01:00
Jakub Konka
4192be8403 elf: implement slice types in debug info
Implements slice types including `[]const u8` for passing as
formal parameters in DWARF. Breaking on a function accepting
a slice in `gdb` will now yield the same behavior as stage1 and/or
LLVM backend:

```zig
fn sumArrayLens(a: []const u32, b: []const u8) usize {
  return a.len + b.len;
}
```

Both `a` and `b` can now be inspected in the debugger:

```
Breakpoint 1, sumArrayLens (a=..., b=...) at arr.zig:59
(gdb) p a
$1 = {ptr = 0x7fffffff685c, len = 5}
(gdb) p b
$2 = {ptr = 0x7fffffff683d "\252\252\252\\h\377\377\377\177", len = 3}
(gdb)
```
2022-01-26 17:28:58 +01:00
Jakub Konka
9f224ebd9a stage2: add zero- and sign-extend moves to x86_64
* remove `LoweringError` error set from `Emit.zig` - it actually
  was less than helpful; it's better to either not throw an error
  since there can be instructions with mismatching operand sizes
  such as `movsx` or assert on a by instruction-basis. Currently,
  let's just pass through and see how we fare.
* when moving integers into registers, check for signedness and move
  with zero- or sign-extension if source operand is smaller than 8
  bytes. The destination operand is always assumed to be full-width,
  i.e., 8 bytes.
* clean up `airTrunc` a little to match the rest of CodeGen inst
  implementations.
2022-01-26 16:14:57 +01:00
Jakub Konka
075e2eaaaf Merge branch 'x64-trunc' of git://github.com/mparadinha/zig into mparadinha-x64-trunc 2022-01-26 11:15:57 +01:00
Motiejus Jakštys
26e54d8a32 fix aarch64 include path 2022-01-26 09:10:12 +02:00
Andrew Kelley
f0ddc7f7a2 translate-c: update for new function pointer semantics
After #10656, function pointers are represented with e.g.
`*const fn()void` rather than `fn()void`.

This commit adds code to translate-c to emit different code
depending on whether the output zig source code is intended
to be compiled with stage1 or stage2.

Ideally we will have stage1 and stage2 support the exact same
Zig language, but for now they diverge because I would rather
focus on finishing and shipping stage2 than implementing the
features in stage1.
2022-01-25 22:04:01 -07:00
Andrew Kelley
618055db50 c backend: use an explicit map of reserved idents
rather than unconditionally prepending double underscore to all
identifiers. Also, use the prefix `zig_e_` instead of `__`. Also, avoid
triggering this escaping when rendering an identifier and there has
already been a prefix printed.
2022-01-25 20:49:43 -07:00
mparadinha
575d430f92 stage2: x64: implement airTrunc 2022-01-25 23:53:26 +00:00
Lee Cannon
fbe5336f3b add option to force usage of GeneralPurposeAllocator 2022-01-25 18:21:58 -05:00
Jakub Konka
53c668d3a9 stage2: add naive impl of pointer type in ELF
Augment relocation tracking mechanism to de-duplicate potential
creation of base as well as composite types while unrolling
composite types in the linker - there is still potential for
further space optimisation by moving all type information into
a separate section `.debug_types` and providing references to
entries within that section whenever required (e.g., `ref4` form).
Currently, we duplicate type definitions on a per-decl basis.

Anyhow, with this patch, an example function signature of the following
type:

```zig
fn byPtrPtr(ptr_ptr_x: **u32, ptr_x: *u32) void {
    ptr_ptr_x.* = ptr_x;
}
```

will generate the following `.debug_info` for formal parameters:

```
 <1><1aa>: Abbrev Number: 3 (DW_TAG_subprogram)
    <1ab>   DW_AT_low_pc      : 0x8000197
    <1b3>   DW_AT_high_pc     : 0x2c
    <1b7>   DW_AT_name        : byPtrPtr
 <2><1c0>: Abbrev Number: 7 (DW_TAG_formal_parameter)
    <1c1>   DW_AT_location    : 1 byte block: 55        (DW_OP_reg5 (rdi))
    <1c3>   DW_AT_type        : <0x1df>
    <1c7>   DW_AT_name        : ptr_ptr_x
 <2><1d1>: Abbrev Number: 7 (DW_TAG_formal_parameter)
    <1d2>   DW_AT_location    : 1 byte block: 54        (DW_OP_reg4 (rsi))
    <1d4>   DW_AT_type        : <0x1e4>
    <1d8>   DW_AT_name        : ptr_x
 <2><1de>: Abbrev Number: 0
 <1><1df>: Abbrev Number: 5 (DW_TAG_pointer_type)
    <1e0>   DW_AT_type        : <0x1e4>
 <1><1e4>: Abbrev Number: 5 (DW_TAG_pointer_type)
    <1e5>   DW_AT_type        : <0x1e9>
 <1><1e9>: Abbrev Number: 4 (DW_TAG_base_type)
    <1ea>   DW_AT_encoding    : 7       (unsigned)
    <1eb>   DW_AT_byte_size   : 4
    <1ec>   DW_AT_name        : u32
```
2022-01-25 23:51:19 +01:00
Jakub Konka
05c5bb9edd stage2: populate debug info for args passed on stack
* implement cond_br when MCValue is a stack offset
* implement passing compare flags and immediate on stack
2022-01-25 23:51:04 +01:00
Andrew Kelley
ef7eff3939 Sema: coercion of pointers to C pointers 2022-01-25 14:53:41 -07:00
Andrew Kelley
f2835c6a28
Merge pull request #10679 from Luukdegram/wasm-unions
Stage2: wasm - Implement unions
2022-01-25 16:51:57 -05:00
Andrew Kelley
366c767444 link: Elf, Wasm: forward strip flag when linking with LLD 2022-01-25 11:52:48 -07:00
Andrew Kelley
50905d8851 zig cc: detect more linker args
* --whole-archive, -whole-archive
 * --no-whole-archive, -no-whole-archive
 * -s, --strip-all
 * -S, --strip-debug
2022-01-25 11:52:48 -07:00
Luuk de Gram
0682c9ac33
wasm: Implement genTypedValue for enums
This makes all union test cases succeed.
`rem` was also implemented as all we had to do is enable the instruction.
Loading and storing values based on ABI-size was simplified to a direct abiSize() call.

We also enabled all the newly passing test cases and disable them for all non-passing backends.
All of those test cases were verified to see if they perhaps already pass for the c-backend.
2022-01-25 19:29:40 +01:00
Luuk de Gram
e9d122f164
wasm: Implement lowering unions 2022-01-25 19:00:52 +01:00
Luuk de Gram
288b407fa9
wasm: Implement get_union_tag 2022-01-25 19:00:51 +01:00
Luuk de Gram
4b939fb34d
wasm: Implement set_union_tag 2022-01-25 19:00:48 +01:00
Andrew Kelley
a2abbeef90 stage2: rework a lot of stuff
AstGen:
 * rename the known_has_bits flag to known_non_opv to make it better
   reflect what it actually means.
 * add a known_comptime_only flag.
 * make the flags take advantage of identifiers of primitives and the
   fact that zig has no shadowing.
 * correct the known_non_opv flag for function bodies.

Sema:
 * Rename `hasCodeGenBits` to `hasRuntimeBits` to better reflect what it
   does.
   - This function got a bit more complicated in this commit because of
     the duality of function bodies: on one hand they have runtime bits,
     but on the other hand they require being comptime known.
 * WipAnonDecl now takes a LazySrcDecl parameter and performs the type
   resolutions that it needs during finish().
 * Implement comptime `@ptrToInt`.

Codegen:
 * Improved handling of lowering decl_ref; make it work for
   comptime-known ptr-to-int values.
   - This same change had to be made many different times; perhaps we
     should look into merging the implementations of `genTypedValue`
     across x86, arm, aarch64, and riscv.
2022-01-24 21:53:57 -07:00
Andrew Kelley
8bb679bc6e Sema: resolveBody takes a parameter for break blocks
Previously, break instructions which wanted to break out of multiple
nesting layers did not work correctly at comptime.
2022-01-24 21:47:53 -07:00
Andrew Kelley
65576ea2ea llvm backend: fix not updating map after deleting global
This was uncaught UB!
2022-01-24 21:47:53 -07:00
Andrew Kelley
b34f994c0b stage2: type system treats fn ptr and body separately
This commit updates stage2 to enforce the property that the syntax
`fn()void` is a function *body* not a *pointer*. To get a pointer, the
syntax `*const fn()void` is required.

ZIR puts function alignment into the func instruction rather than the
decl because this way it makes it into function types. LLVM backend
respects function alignments.

Struct and Union have methods `fieldSrcLoc` to help look up source
locations of their fields. These trigger full loading, tokenization, and
parsing of source files, so should only be called once it is confirmed
that an error message needs to be printed.

There are some nice new error hints for explaining why a type is
required to be comptime, particularly for structs that contain function
body types.

`Type.requiresComptime` is now moved into Sema because it can fail and
might need to trigger field type resolution. Comptime pointer loading
takes into account types that do not have a well-defined memory layout
and does not try to compute a byte offset for them.

`fn()void` syntax no longer secretly makes a pointer. You get a function
body type, which requires comptime. However a pointer to a function body
can be runtime known (obviously).

Compile errors that report "expected pointer, found ..." are factored
out into convenience functions `checkPtrOperand` and `checkPtrType` and
have a note about function pointers.

Implemented `Value.hash` for functions, enum literals, and undefined values.

stage1 is not updated to this (yet?), so some workarounds and disabled
tests are needed to keep everything working. Should we update stage1 to
these new type semantics? Yes probably because I don't want to add too
much conditional compilation logic in the std lib for the different
backends.
2022-01-24 21:47:53 -07:00
Andrew Kelley
0866fa9d1d
Merge pull request #10688 from topolarity/c-backend-union-support
stage2: Add `union` support to C backend
2022-01-24 23:47:41 -05:00
mparadinha
2e9ce6facd stage2: x64: fix printing of callee preserved regs instructions 2022-01-24 23:19:53 +01:00
joachimschmidt557
7665401500 stage2 ARM: re-enable debug info for arguments
These were disabled during the MIR transition
2022-01-24 23:18:13 +01:00
Cody Tapscott
60e6bf112c Cleanup unnecessary switches in union logic 2022-01-24 12:49:14 -07:00
Cody Tapscott
cb24799368 Run zig fmt 2022-01-24 12:18:16 -07:00
Cody Tapscott
587a4437db Add union support to the C backend.
There are some differences vs. the union encoding in the LLVM backend:
   - Tagged unions with a 0-bit payload do not become their tag type. Instead,
     they are a struct with an empty `union` as their payload field.
   - We do not order the `payload`/`tag` storage based on their alignment
2022-01-24 12:00:04 -07:00
Cody Tapscott
983dfcd3fb Fix rendering of void function args 2022-01-24 12:00:04 -07:00
Cody Tapscott
8c96c64fbb Add support for rendering .enum_numbered 2022-01-24 12:00:04 -07:00
Cody Tapscott
52517e86d6 Avoid identifier conflicts with reserved C keywords 2022-01-24 12:00:01 -07:00
Cody Tapscott
799bd81b08 Add support for rendering .elem_ptr 2022-01-24 11:58:43 -07:00
Andrew Kelley
12c2de6ee2
Merge pull request #10662 from ziglang/doc-comments-zir
Doc comments zir
2022-01-23 18:31:55 -05:00
Andrew Kelley
8a69726209 AstGen: doc comment fixups
* AstGen: use Ast.zig helper methods to avoid copy pasting token counting logic
   - take advantage of the `first_doc_comment` field we already have for
     param AST nodes
 * Add missing ZIR docs
2022-01-23 16:24:46 -07:00
Jimmi Holst Christensen
f1b91bb41b c backend: Implement aligning fields and local/global variables
There are some restrictions here.

- We either need C11 or a compiler that supports the aligned attribute
- We cannot provide align less than the type's natural C alignment.
2022-01-23 17:23:55 -05:00
Jakub Konka
36f13f5911 stage2: remove asserts and comments which are Emit.zig responsibility 2022-01-23 00:31:37 +01:00
Jakub Konka
406c85f9ba macho+elf: fix integer overflow in allocateAtom
If there is a big atom available for re-use in the free list, and
it's the last atom in section, it's ideal capacity might span the
entire section in which case we do not want to calculate the actual
end VM addr of the symbol since it may overflow. Instead, we just take
the max capacity available as end VM addr estimate. In this case,
the max capacity equals `std.math.maxInt(u64)`.
2022-01-22 08:50:01 +01:00
Jakub Konka
062ddb693f stage2: fix improper capacity prealloc in lowerToRm encoding 2022-01-22 00:58:18 +01:00
Jakub Konka
5fb921539b stage2: do not copy args passed via stack into functions locals 2022-01-22 00:46:35 +01:00
Jakub Konka
4a5e75245b stage2: clean up preserving callee regs on the stack
Instead of using `push` and `pop` combo, we now re-use our stack
allocation mechanism which means we don't have to worry about
16-byte stack adjustments on macOS as it is handled automatically
for us. Another benefit is that we don't have to backpatch stack
offsets when pulling args from the stack.
2022-01-22 00:46:35 +01:00
Loris Cro
8c23b81e86 fix incorrect zir offset loading in sema 2022-01-21 22:46:12 +01:00
Loris Cro
ba55e32ef2 add function param doc comment info in zir 2022-01-21 22:00:57 +01:00
Loris Cro
1f56ff8343 add support for more decl attributes in doc comment zir
The previous commit that implemented doc comment zir support for
decls did not properly account for all the possible attribute
keyword combinations (threadlocal, extern, and such).
2022-01-21 22:00:57 +01:00
Loris Cro
98fddd1c54 add field doc comments to zir
Doc comment information is stored in `extra` unconditionally for
each field. This commmit covers Structs, Enums, Unions, and ErrSets.
2022-01-21 22:00:57 +01:00