17530 Commits

Author SHA1 Message Date
Andrew Kelley
c3663f2617 LLVM: implement debug info for structs
This involved some significant reworking in order to introduce the
concept of "forward declarations" to the system to break dependency
loops.

The `lowerDebugType` function now takes an `enum { full, fwd }` and is moved
from `DeclGen` to `Object` so that it can be called from `flushModule`.

`DITypeMap` is now an `ArrayHashMap` instead of a `HashMap` so that we can
iterate over the entries in `flushModule` and finalize the forward decl
DITypes into full DITypes.

`DITypeMap` now stores `AnnotatedDITypePtr` values instead of
`*DIType` values. This is an abstraction around a `usize` which assumes
the pointers will be at least 2 bytes aligned and uses the least
significant bit to store whether it is forward decl or a fully resolved
debug info type.

`lowerDebugTypeImpl` is extracted out from `lowerDebugType` and it has a
mechanism for completing a forward decl DIType to a fully resolved one.
The function now contains lowering for struct types. Closes #11095.

There is a workaround for struct types which have not had
`resolveFieldTypes` called in Sema, even by the time `flushModule` is
called. This is a deficiency of Sema that should be addressed, and the
workaround removed. I think Sema needs a new mechanism to queue up type
resolution work instead of doing it in-line, so that it does not cause
false dependency loops. We already have one failing behavior test
because of a false dependency loop.
2022-03-16 00:09:00 -07:00
Andrew Kelley
cfc31b5bbd std.dwarf: avoid some async/await stuff in stage2 builds 2022-03-16 00:09:00 -07:00
Veikka Tuominen
2583be7585 stage2 llvm: fix @extern 2022-03-16 00:09:00 -07:00
Mitchell Hashimoto
394252c9db stage2: move duplicate error set check to AstGen 2022-03-16 01:41:22 -04:00
Andrew Kelley
9a6fa67cbc Sema: only do store_ptr tuple optimization for arrays
Check the big comment in the diff for more details.
Fixes default-initialization of structs from empty struct literals.
2022-03-15 19:21:58 -07:00
Mitchell Hashimoto
fd43434149 stage2: TypeInfo for func with generic return type should set null
Prior to these, the return type was non-null but the value was generic
poison which wasn't usable in user-space. This sets the value to null.
This also adds a behavior test for this.

Co-authored-by: InKryption <inkryption07@gmail.com>
2022-03-15 20:12:22 -04:00
Andrew Kelley
1149e8bb08
Merge pull request #11185 from topolarity/bugfix-11159
stage2: resolve panic on array-like tuple initialization
2022-03-15 20:03:58 -04:00
Cody Tapscott
480e7eec65 stage2: Fix panic on initializing comptime fields in tuple
This resolves https://github.com/ziglang/zig/issues/11159

The problem was that:
  1. We were not correctly deleting the field stores after recognizing
     that an array initializer was a comptime-known value.
  2. LLVM was not checking that the final type had no runtime bits, and
     so would generate an invalid store.

This also adds several test cases for related bugs, just to check these
in for later work.
2022-03-15 17:01:16 -07:00
Cody Tapscott
762c4a876b stage2: comptime fields should not affect opv/comptime-only 2022-03-15 17:01:07 -07:00
Mitchell Hashimoto
7d0b6956c0 stage2: resolve type fully when resolving inferred allocs
We must resolve the type fully so that pointer children (i.e. slices)
are resolved. Additionally, we must resolve even if we can know the
value at comptime because the `alloc_inferred` ZIR always produces a
constant in the AIR.

Fixes #11181
2022-03-15 19:55:21 -04:00
Andrew Kelley
2c434cddd6 AstGen: add missing coercion for const locals
A const local which had its init expression write to the result pointer,
but then gets elided to directly initialize, was missing the coercion to
the type annotation.
2022-03-15 16:41:10 -07:00
Andrew Kelley
d4a0d5f959 Sema: implement @truncate for SIMD vectors 2022-03-15 15:09:48 -07:00
Andrew Kelley
ea4d2759a5
Merge pull request #11180 from jmc-88/cbe3
CBE: amending an incorrect test name, plus two small fixes
2022-03-15 17:04:23 -04:00
William Sengir
6de8b4bc3d std.dwarf: implement basic DWARF 5 parsing
DWARF 5 moves around some fields and adds a few new ones that can't be
parsed or ignored by our current DWARF 4 parser. This isn't a complete
implementation of DWARF 5, but this is enough to make stack traces
mostly work. Line numbers from C++ don't show up, but I know the info
is there. I think the answer is to iterate through .debug_line_str in
getLineNumberInfo, but I didn't want to fall into an even deeper rabbit
hole tonight.
2022-03-15 16:53:45 -04:00
Daniele Cocca
71ca0b176f CBE: #undef linux in zig.h
Compilers will sometimes `#define linux 1` if the operating system in
use is Linux. This clashes with the code produced by the C backend when
processing the stdlib, e.g. std.Target.Os.VersionRange [^1] which is a
struct containing a field named `linux`.

The output of the C backend doesn't rely on this macro being defined,
and other code also shouldn't rely on it -- e.g. quoting from the GCC
documentation [^2]:

  """
  The C standard requires that all system-specific macros be part of
  the reserved namespace. All names which begin with two underscores, or
  an underscore and a capital letter, are reserved for the compiler and
  library to use as they wish. However, historically system-specific
  macros have had names with no special prefix; for instance, it is
  common to find unix defined on Unix systems.

  [...]

  We are slowly phasing out all predefined macros which are outside the
  reserved namespace. You should never use them in new programs, and we
  encourage you to correct older code to use the parallel macros
  whenever you find it. We don’t recommend you use the system-specific
  macros that are in the reserved namespace, either. It is better in the
  long run to check specifically for features you need
  """

[^1]: 8c32d989c9/lib/std/target.zig (L224)
[^2]: https://gcc.gnu.org/onlinedocs/cpp/System-specific-Predefined-Macros.html#System-specific-Predefined-Macros
2022-03-15 19:05:15 +00:00
Daniele Cocca
79f74943b5 CBE: fix output of airMinMax()
This was trying to output a ternary operator, but the output was broken
by the lack of a '?'.
2022-03-15 19:00:16 +00:00
Daniele Cocca
9b5737b5a6 s/testClz/testCtz/g
The test name here was likely the result of a bad copy-paste, as the
test code is actually testing for trailing zeroes, not leading zeroes.
2022-03-15 19:00:16 +00:00
Ali Chraghi
47e004d975 remove TODO 2022-03-15 13:49:41 -04:00
Tom Maenan Read Cutting
9bb19a090e std: Add elf.EM, coff.MachineType to Target.CPU.Arch conversions
target.Arch already supports finding the correct encoding for either
target, so being able to do the inverse has use cases for when parsing
files of an unknown target (i.e. for zar).
2022-03-15 13:48:42 -04:00
Andrew Kelley
c64279b15b Sema: fix shl_sat with comptime rhs 2022-03-14 23:15:01 -07:00
Andrew Kelley
1adb15098c LLVM: clean up airUnaryOp to call callFloatUnary
and make callFloatUnary support vectors. I tried to make it use
getIntrinsic, but that resulted in tripping `assert(id != 0)`.
2022-03-14 21:43:57 -07:00
Andrew Kelley
9eceba2485
Merge pull request #11128 from topolarity/comptime-memory-reinterp
stage2: Track parent type for `.elem_ptr`, `.field_ptr`, and `.*_payload_ptr`
2022-03-15 00:43:26 -04:00
Andrew Kelley
2f92d1a026 stage2: fixups for topolarity-comptime-memory-reinterp branch
* don't store `has_well_defined_layout` in memory.
 * remove struct `hasWellDefinedLayout` logic. it's just
   `layout != .Auto`. This means we only need one implementation, in
   Type.
 * fix some of the cases being wrong in `hasWellDefinedLayout`, such as
   optional pointers.
 * move `tag_ty_inferred` field into a position that makes it more
   obvious how the struct layout will be done. Also we don't have a
   compiler that intelligently moves fields around so this layout is
   better.
 * Sema: don't `resolveTypeLayout` in `zirCoerceResultPtr` unless
   necessary.
 * Rename `ComptimePtrLoadKit` `target` field to `pointee` to avoid
   confusion with `target`.
2022-03-14 21:43:03 -07:00
Cody Tapscott
50a1ca24ca Add test for issue #11139 2022-03-14 21:43:02 -07:00
Cody Tapscott
1f76b4c6b8 stage2 llvm: Respect container type when lowering parent pointers
We need to make sure that we bitcast our pointers correctly before
we use get_element_ptr to compute the offset for the parent
pointer.

This also includes a small fix-up for a problem where ptrs to const
i64/u64 were not using the correct type in >1-level decl chains
(where we call lowerParentPtr recursively)
2022-03-14 21:42:43 -07:00
Cody Tapscott
5fa057053c stage2 sema: Respect container_ty of parent ptrs
The core change here is that we no longer blindly trust that parent
pointers (.elem_ptr, .field_ptr, .eu_payload_ptr, .union_payload_ptr)
were derived from the "true" type of the underlying decl. When types
diverge, direct dereference fails and we are forced to bitcast, as
usual.

In order to maximize our chances to have a successful bitcast, this
includes several changes to the dereference procedure:
   - `root` is now `parent` and is the largest Value containing the
     dereference target, with the condition that its layout and the
     byte offset of the target within are both well-defined.
   - If the target cannot be dereferenced directly, because the
     pointers were not derived from the true type of the underlying
     decl, then it is returned as null.
   - `beginComptimePtrDeref` now accepts an optional array_ty param,
     which is used to directly dereference an array from an elem_ptr,
     if necessary. This allows us to dereference array types without
     well-defined layouts (e.g. `[N]?u8`) at an offset

The load_ty also allows us to correctly "over-read" an .elem_ptr to an
array of [N]T, if necessary. This makes direct dereference work for
array types even in the presence of an offset, which is necessary if
the array has no well-defined layout (e.g. loading from `[6]?u8`)
2022-03-14 21:42:43 -07:00
Cody Tapscott
54426bdc82 stage2: Fix assertion in struct field offset when all fields are 0-size 2022-03-14 21:42:42 -07:00
Cody Tapscott
34a6fcd88e stage2: Add hasWellDefinedLayout() to type.zig and Sema.zig
This follows the same strategy as sema.typeRequiresComptime() and
type.comptimeOnly(): Two versions of the function, one which performs
resolution just-in-time and another which asserts that resolution is
complete.

Thankfully, this doesn't cause very viral type resolution, since
auto-layout structs and unions are very common and are known to not have
a well-defined layout without resolving their fields.
2022-03-14 21:42:42 -07:00
Cody Tapscott
bbd750ff05 stage2: Add container_ty/elem_ty to elem_ptr, field_ptr, *_payload_ptr Values 2022-03-14 21:42:42 -07:00
Andrew Kelley
a2a5d3c288
Merge pull request #11167 from mitchellh/codegen-arrays
stage2: codegen of arrays should use type length, not value length
2022-03-15 00:40:32 -04:00
William Sengir
c757f19790 stage2: add debug info for globals in the LLVM backend
LLVM backend: generate DIGlobalVariable's for non-function globals and
rename linkage names when exporting functions and globals.

zig_llvm.cpp: add some wrappers to convert a handful of DI classes
into DINode's since DIGlobalVariable is not a DIScope like the others.

zig_llvm.cpp: add some wrappers to allow replacing the LinkageName of
DISubprogram and DIGlobalVariable.

zig_llvm.cpp: fix DI class mixup causing nonsense reinterpret_cast.

The end result is that GDB is now usable since you now no longer need
to manually cast every global nor fully qualify every export.
2022-03-15 00:38:20 -04:00
Andrew Kelley
f36bf8506c
Merge pull request #11164 from mitchellh/reify-union
stage2: reify unions
2022-03-15 00:21:11 -04:00
Mitchell Hashimoto
67647154c1
stage2: apply fix for #11165 to codegen.zig for native backends
Co-authored-by: Cody Tapscott <topolarity@tapscott.me>
2022-03-14 20:00:17 -07:00
Mitchell Hashimoto
a859f94644
stage2: LLVM codegen of arrays should use type length, not value length
It is possible for the value length to be longer than the type because
we allow in-memory coercing of types such as `[5:0]u8` to `[5]u8`. In
such a case, the value length is 6 but the type length if 5.

The `.repeated` value type already got this right, so this is extending
similar logic out to `.aggregate` and `.bytes`. Both scenarios are
tested in behavior tests.

Fixes #11165
2022-03-14 17:41:12 -07:00
Andrew Kelley
84f96779c3
Merge pull request #11143 from jmc-88/cbe
CBE: Implement popCount, byteSwap, bitReverse for ints <= 128 bits
2022-03-14 18:23:00 -04:00
Curtis Tate Wilkinson
3bb4c0c789
zig fmt: Resolve #11131 loss of comment on switch cases
Correct switch cases dropping comments in certain situations by
checking for the presence of the comment before collapsing to one line.
2022-03-14 23:10:59 +01:00
Mitchell Hashimoto
53de31d62c
stage2: enum reification should use sliceLen and elemValue 2022-03-14 14:38:05 -07:00
Mitchell Hashimoto
edd07aa808
stage2: reify unions 2022-03-14 14:34:28 -07:00
Daniele Cocca
8643591c9a CBE: split {clz,ctz,mod,popCount,byteSwap,bitReverse} by type
This also surfaces the fact that clz, ctz and popCount didn't actually
support 128 bit integers, despite what was claimed by
226fcd7c709ec664c5d883042cf7beb3026f66cb. This was partially hidden by
the fact that the test code for popCount only exercised 128 bit integers
in a comptime context. This commit duplicates that test case for runtime
ints too.
2022-03-14 19:43:31 +00:00
Andrew Kelley
5ea94e7715 stage2: rework Value storage of structs and arrays
Now they both use `Value.Tag.aggregate`.

Additionally the LLVM backend now has implemented lowering of
tuple values.
2022-03-14 12:28:52 -07:00
Mitchell Hashimoto
1ebe3bd01d
stage2: reify structs and tuples (#11144)
Implements `@Type` for structs, anon structs, and tuples. This is another place that would probably benefit from a `.reified_struct` type tag but will defer for later in the interest of getting tests passing first.
2022-03-14 13:47:35 -04:00
Andrew Kelley
5919b10048
Merge pull request #11155 from ziglang/stage2-float-fixes
stage2 float fixes
2022-03-14 12:38:56 -04:00
Andrew Kelley
b2a1b4c085 Sema: improve lowering of stores to bitcasted vector pointers
Detect if we are storing an array operand to a bitcasted vector pointer.
If so, we instead reach through the bitcasted pointer to the vector pointer,
bitcast the array operand to a vector, and then lower this as a store of
a vector value to a vector pointer. This generally results in better code,
as well as working around an LLVM bug.

See #11154
2022-03-14 00:11:46 -07:00
Andrew Kelley
eee989d2a0 Sema: Type.abiSize asserts instead of using max with alignment
ABI size is guaranteed to always be >= alignment.
2022-03-14 00:11:46 -07:00
Andrew Kelley
2a50a4629b freestanding libc: include roundl 2022-03-14 00:11:46 -07:00
Andrew Kelley
547e6f6ee1 disable failing nvptx test cases
See #10968
2022-03-14 00:11:46 -07:00
Andrew Kelley
7a477a1110 LLVM: fix int_to_float signedness detection
It was checking if the result (float) type was a signed int rather than
checking the operand (integer) type.
2022-03-14 00:11:46 -07:00
Andrew Kelley
d42d31f72f basic language features do not belong in std.meta 2022-03-14 00:11:46 -07:00
Andrew Kelley
eeaaefb925 LLVM: fix debug info for local vars
Previously we incorrectly used the pointer type as the debug info type.
2022-03-14 00:11:46 -07:00
Daniele Cocca
d912699e08 Remove signed_type from zig_{clz,ctz,popcount}
This parameter is only currently needed by zig_byte_swap() and
zig_bit_reverse(). This commit adds an option to airBuiltinCall() to
allow emitting the signedness information only when needed, removing
this unused parameter from the other builtins.
2022-03-14 01:04:24 +00:00