8168 Commits

Author SHA1 Message Date
Andrew Kelley
4fa5060633 Sema: take advantage of checkIntOrVectorAllowComptime 2022-03-17 18:04:06 -07:00
John Schmidt
d7d2ccb7af Avoid index out of bounds for one-valued types in zirValidateArrayInit
Previously, the code assumed that `ptr_elem_ptr` was always followed by
a `store`, but this is not true for types with one value (such as `u0`).
2022-03-17 18:00:48 -07:00
John Schmidt
adfcc8851b Implement @byteSwap for vectors
Make the behavior tests for this a little more primitive to exercise as
little extra functionality as possible.
2022-03-17 18:00:48 -07:00
Andrew Kelley
7233a3324a stage2: implement @reduce
Notably, Value.eql and Value.hash are improved to treat NaN as equal to
itself, so that Type/Value can be hash map keys. Likewise float hashing
normalizes the float value before computing the hash.
2022-03-17 17:24:35 -07:00
Andrew Kelley
76e103057e zig_llvm.h: fix some declarations not being C compatible 2022-03-17 17:24:11 -07:00
Andrew Kelley
1ee7a81627 Revert "link.Elf: swap order of GOT and text program headers"
This reverts commit 8e7b1a74ac4568a2b806026f210ae59e52d2768d.

Sorry, I should have put up a PR and ran that one by Jakub and done some
more inspection.

This causes problems with gdb:

BFD: /home/andy/dev/zig/build-release/test: invalid string offset 3254779904 >= 153524 for section `.shstrtab'
2022-03-17 11:41:17 -07:00
Andrew Kelley
8e7b1a74ac link.Elf: swap order of GOT and text program headers
This makes Valgrind happier because it results in the program headers
being sorted by VirtAddr in the ELF file.
2022-03-17 11:36:17 -07:00
Jakub Konka
4012fcb0a3 macho: do not allocate new GOT atom when target atom has grown
That's the entire point of our GOT so that we don't have to do that;
simply overwrite the address in the GOT.
2022-03-17 12:48:02 +01:00
Jakub Konka
d53e0234a3 macho: fix snapshot generation 2022-03-17 11:41:08 +01:00
Joachim Schmidt
3c3826bf93
Merge pull request #11192 from joachimschmidt557/stage2-arm
stage2 ARM: misc improvements
2022-03-17 09:40:41 +01:00
Andrew Kelley
87779cfd93 stage2: prevent UB in the LLVM backend
* Sema: fix `zirTypeInfo` allocating with the wrong arenas for some
   stuff.
 * LLVM: split `airDbgInline` into two functions, one for each AIR tag.
   - remove the redundant copy to type_map_arena. This is the first
     thing that lowerDebugType does so this hack was probably just
     accidentally avoiding UB (which is still present prior to this
     commit).
   - don't store an inline fn inst into the di_map for the generic
     decl.
   - use a dummy function type for the debug info to avoid whatever UB
     is happening.
   - we are now ignoring the function type passed in with the
     dbg_inline_begin and dbg_inline_end.
 * behavior tests: prepare the vector tests to be enabled one at a time.

Mitigates #11199.
2022-03-17 00:00:41 -07:00
Mitchell Hashimoto
79d3780fbd stage2: bit_not on u0 is always 0 2022-03-16 23:18:35 -07:00
John Schmidt
c8ed813097 Implement @mulAdd for vectors 2022-03-16 20:11:05 -07:00
Daniele Cocca
312536540b CBE: better handling of sentineled slices/arrays
Adds the sentinel element to the type name to avoid ambiguous
declarations, and outputs the sentinel element (if needed) even in what
would otherwise be empty arrays.
2022-03-16 19:58:45 -07:00
Andrew Kelley
d78b8c10b9 LLVM: fix slice debug info and functions
with return types that have no runtime bits
2022-03-16 17:22:16 -07:00
Andrew Kelley
cdeb1fb881 Sema: different solution to elemVal
I think that reusing the ComptimePtrLoad infrastructure is ultimately
less logic and more robust than adding a `direct` flag to elem_ptr.

 * Some code in zirTypeInfo needed to be fixed to create proper
   Type/Value encodings.
 * comptime elemVal works by constructing an elem_ptr Value and then
   using the already existing pointerDeref function.

There are some remaining calls to Value.elemValue which should be
considered code smells at this point.
2022-03-16 16:29:08 -07:00
Mitchell Hashimoto
418197b6c5 stage2: elem_ptr needs to know if slice or direct access
This fixes one of the major issues plaguing the `std.sort` comptime tests.
The high level issue is that at comptime, we need to know whether `elem_ptr` is
being used to subslice an array-like pointer or access a child value. High-level
example:

    var x: [2][2]i32 = undefined;
    var a = &x[0]; // elem_ptr, type *[2]i32

    var y: [5]i32 = undefined;
    var b = y[1..3]; // elem_ptr, type *[2]i32

`a` is pointing directly to the 0th element of `x`. But `b` is
subslicing the 1st and 2nd element of `y`. At runtime with a well
defined memory layout, this is an inconsequential detail. At comptime,
the values aren't laid out exactly in-memory so we need to know the
difference.

This becomes an issue specifically in this case:

    var c: []i32 = a;
    var d: []i32 = b;

When converting the `*[N]T` to `[]T` we need to know what array to point
to. For runtime, its all the same. For comptime, we need to know if its
the parent array or the child value.

See the behavior tests for more details.

This commit fixes this by adding a boolean to track this on the
`elem_ptr`. We can't just immediately deref the child for `&x[0]`
because it is legal to ptrCast it to a many-pointer, do arithmetic, and
then cast it back (see behavior test) so we need to retain access to the
"parent" indexable.
2022-03-16 16:26:54 -07:00
Andrew Kelley
94672dfb19 stage2: fully resolve fn types after analyzing its body 2022-03-16 13:31:53 -07:00
joachimschmidt557
dcc1de12b0
stage2 ARM: implement addwrap, subwrap, mulwrap 2022-03-16 20:20:07 +01:00
joachimschmidt557
2412ac2c5f
stage2 ARM: fix shl for ints with bits < 32 2022-03-16 20:20:07 +01:00
joachimschmidt557
0eebdfcad3
stage2 ARM: fix bitwise negation of ints with bits < 32 2022-03-16 20:20:07 +01:00
joachimschmidt557
ca1ffb0951
stage2 ARM: genSetStack for stack_argument_offset 2022-03-16 20:19:58 +01:00
Andrew Kelley
1f313b3d7c LLVM: make the load function copy isByRef=true types 2022-03-16 11:52:22 -07:00
Veikka Tuominen
49c0bb1f33
Merge pull request #11177 from Vexu/dbg_func
Add debug info for inlined calls
2022-03-16 20:40:16 +02:00
Andrew Kelley
d4a7a9ac4c
Merge pull request #11191 from Snektron/zig-gdb-improvements
Zig gdb improvements
2022-03-16 14:08:06 -04:00
Robin Voetter
0bd84e03b9 gdb: add printer for selfhosted Value 2022-03-16 17:57:31 +01:00
Robin Voetter
6830bcbb0a gdb: add printer for selfhosted Type 2022-03-16 17:40:21 +01:00
Veikka Tuominen
d83a26f068 stage2 llvm: keep track of inlined functions 2022-03-16 10:53:41 +02:00
Veikka Tuominen
0343811836 Sema: emit dbg_func around inline calls 2022-03-16 09:34:26 +02:00
Veikka Tuominen
0f112b9f6d AstGen: emit dbg_stmt before function calls and branch conditions 2022-03-16 09:27:31 +02:00
Veikka Tuominen
af2b03de83 Type: implement ptrInfo for optional pointers 2022-03-16 09:26:10 +02:00
Andrew Kelley
8a43d67c3b LLVM: fix LLVM assertion when slicing 2022-03-16 00:09:00 -07:00
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
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
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
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
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
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