12279 Commits

Author SHA1 Message Date
mlugg
569ae762e1
compiler: allow cast builtins to coerce result to error union or optional
Also improves some error messages
2023-06-25 13:28:32 +01:00
mlugg
8afadee45a
AstGen: use usize as result type of for loop range operands 2023-06-25 13:28:31 +01:00
Frank Denis
e7f872c9c6
wasi-libc: compile emmalloc.c without strict aliasing (#16157)
emmalloc.c does a fair amount of type punning in order to access
the size of memory regions and traverse them.

Unfortunately, that can lead to unwanted optimizations.

This simple test case currently triggers a memory fault:

int main(void) {
    char * volatile p = malloc(1);
    p = realloc(p, 12);
    p = malloc(1);
    printf("%p\n", p);
}

Work around this by adding "-fno-strict-aliasing" when compiling
that file.
2023-06-25 11:24:54 +02:00
Ryan Liptak
b111702940 Recognize the .res extension and link it as if it were an object file
.res files are compiled Windows resource files that get linked into executables/libraries. The linker knows what to do with them, but previously you had to trick Zig into thinking it was an object file (by renaming it to have the .obj extension, for example).

After this commit, the following works:

    zig build-exe main.zig resource.res

or, in build.zig:

    exe.addObjectFile("resource.res");

Closes #6488
2023-06-25 01:42:12 -07:00
Andrew Kelley
9684947faa compiler: start moving safety-checks into backends
This actually used to be how it worked in stage1, and there was this
issue to change it: #2649

So this commit is a reversal to that idea. One motivation for that issue
was avoiding emitting the panic handler in compilations that do not have
any calls to panic. This commit only resolves the panic handler in the
event of a safety check function being emitted, so it does not have that
flaw.

The other reason given in that issue was for optimizations that elide
safety checks. It's yet to be determined whether that was a good idea or
not; this can get re-explored when we start adding optimization passes
to AIR.

This commit adds these AIR instructions, which are only emitted if
`backendSupportsFeature(.safety_checked_arithmetic)` is true:
 * add_safe
 * sub_safe
 * mul_safe

It removes these nonsensical AIR instructions:
 * addwrap_optimized
 * subwrap_optimized
 * mulwrap_optimized

The safety-checked arithmetic functions push the burden of invoking the
panic handler into the backend. This makes for a messier compiler
implementation, but it reduces the amount of AIR instructions emitted by
Sema, which reduces time spent in the secondary bottleneck of the
compiler. It also generates more compact LLVM IR, reducing time spent in
the primary bottleneck of the compiler.

Finally, it eliminates 1 stack allocation per safety-check which was
being used to store the resulting tuple. These allocations were going to
be annoying when combined with suspension points.
2023-06-25 01:41:08 -07:00
Andrew Kelley
82ad66b2f2 liveness: fix merge typo logic error
Oops, I made a terrible mistake when applying a small fixup when merging
f10b9e8fd72aae33b127c18e3f8a64a4f56b1b69.
2023-06-25 01:02:55 -07:00
Andrew Kelley
a31ba25a3d
Merge pull request #16188 from kcbanner/fix_cbe_airErrUnionPayloadPtrSet
cbe: fix crash caused by calling `mod.intValue` on `type_inferred_error_set`
2023-06-24 21:53:59 -07:00
mlugg
67997a699a cbe: codegen int_from_ptr of slice correctly
CBE was translating to access the `len` field rather than `ptr`.
Air.zig specifies that this operation is valid on a slice.
2023-06-24 16:56:40 -07:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
mlugg
447ca4e3ff translate-c: update to new cast builtin syntax 2023-06-24 16:47:49 -07:00
mlugg
be0c69957e compiler: remove destination type from cast builtins
Resolves: #5909
2023-06-24 16:47:49 -07:00
Matteo Briani
8ced903d61
Add support for the "--version" linker argument (#16154)
closes #15549

Co-authored-by: Matteo Briani <matteo.briani@icvox.com>
2023-06-24 16:24:50 -04:00
Jacob Young
d3fed1a87e cbe: fix another instance of calling intValue with an error type 2023-06-24 16:22:00 -04:00
Xavier Bouchoux
f10b9e8fd7 update Liveness to detect that safety checks do not modify memory
followup to [25d3713b07a100d8fdb349317db97fd9d0c1e366]
Resolves #12215

Previous code didn't account for the extra unreach() that now exists in the air:

```
 %29!= block(void, {
    %30!= cond_br(%22!, {
      %31!= br(%29, @Air.Inst.Ref.void_value)
    }, {
      %2! %15!
      %27!= call(%26, [%19!, %21])
      %28!= unreach()
    })
  } %22!)
```
2023-06-24 12:57:46 -07:00
kcbanner
baa2b62e88 cbe: fix crash caused by calling mod.intValue on type_inferred_error_set 2023-06-24 13:49:24 -04:00
Andrew Kelley
a5e15eced0 LLVM: move many DeclGen methods to Object
DeclGen/FuncGen methods are for things that pertain to a particular
declaration or function, while Object methods are for things that
pertain to the entire LLVM Module. Many methods were in the wrong
category, such as type and value lowering.

This is a prerequisite commit for a local branch I am working on, which
needs to be able to call lowerValue() without the context of any
particular function or declaration.
2023-06-24 02:29:49 -07:00
Ali Chraghi
ff0a88b133 spirv: fix a few conflicts caused by intern-pool 2023-06-23 23:52:45 -07:00
Andrew Kelley
5288929ffd sema.addConstant: remove type parameter
Now that InternPool migration is finished, all values have types. So
only the value parameter is required.
2023-06-23 21:59:42 -07:00
kcbanner
5fc5e4fbe0 sema: Fix overflow when analyzing an inline switch prong range that ends on the maximum value of the switched type 2023-06-23 18:28:33 -07:00
kcbanner
9d66481e3d llvm: fixup elem_count argument of ZigLLVMCreateDebugArrayType to be i64
The signature is `getOrCreateSubrange(int64_t  Lo, int64_t  Count)`, so this updates the bindings to match.

This fixes a crash in `lowerDebugTypeImpl` when analyzing slices that have a length of 2^32 or
larger (up to `2^64 >> 3`, which still crashes, because above that the array size in bits overflows u64).
2023-06-23 14:53:17 -07:00
Jacob Young
6aa88ecc54 Type/Value: garbage collect some methods 2023-06-22 11:45:33 -07:00
Jacob Young
c60896743d Value: handle more legacy tags when writing extern struct to memory
Closes #16130
2023-06-22 08:07:02 -07:00
d18g
991e00c270
objcopy.zig allow outputting zero length sections (#16121)
Co-authored-by: Andrew Kelley <andrew@ziglang.org>
2023-06-22 01:55:34 -07:00
Jacob Young
1d38817f5d cbe: key lifetime? 2023-06-22 00:24:26 -04:00
Andrew Kelley
12813d5912
Merge pull request #16105 from jacobly0/intern-pool-opt
InternPool: various optimizations
2023-06-21 00:07:49 -07:00
Jacob Young
a257e33fff Type: remove arbitrary restrictions on param and return types
Opaque and `noreturn` makes sense since they don't represent real
values, but `null` and `undefined` are perfectly normal
comptime-only values.

Closes #16088
2023-06-20 21:51:01 -07:00
jacobly0
e2eabbbc51
Merge pull request #16098 from ziglang/musl-v1.2.4
update musl from v1.2.3 to v1.2.4
2023-06-20 23:14:22 -04:00
Jacob Young
8875efe548 Sema: fix auto-numbered enums with signed tag types
Closes #16095
2023-06-20 20:09:28 -07:00
IntegratedQuantum
3267eb3a28 Fix @enumToInt and @tagName for auto-numbered enums with signed tag type. 2023-06-20 20:09:28 -07:00
Andrew Kelley
b77679039f
Merge pull request #15415 from ehaas/c-char-signedness
Set `c_char` signedness based on the target
2023-06-20 16:41:58 -07:00
Jakub Konka
d2b2567119 macho: do not skip scanning relocs if the sym is non-extern 2023-06-21 00:31:01 +02:00
Jakub Konka
7b5bd3a93f
Merge pull request #16097 from ziglang/macho-faster-uuid
macho: parallelize UUID hash calculation at the expense of full compatibility with ld64
2023-06-21 00:03:52 +02:00
d18g
939e4d81e1 Update objcopy.zig
Fix `--only-section=` handling
2023-06-20 13:12:19 -07:00
Jacob Young
96cdd51c14 Type: delete legacy allocation functions 2023-06-20 14:02:09 -04:00
Jacob Young
52ec121469 Sema: optimize callers of indexToKey 2023-06-20 14:02:09 -04:00
Jacob Young
d7bd4f339c Sema: optimize value resolution 2023-06-20 14:02:09 -04:00
Jacob Young
6b56f4ff0b Value: optimize isPtrToThreadLocal 2023-06-20 14:02:09 -04:00
Jacob Young
d69e324eae Value: optimize isRuntimeValue 2023-06-20 14:02:08 -04:00
Jacob Young
ad3e0e4eb4 Sema: optimize typeHasOnePossibleValue 2023-06-20 14:02:08 -04:00
Jacob Young
3c4b58cea7 Air: optimize around byval struct passing compiler bug 2023-06-20 14:02:08 -04:00
Andrew Kelley
a5d8c310ee musl: update src files to v1.2.4 2023-06-20 12:55:38 -04:00
Andrew Kelley
cdcfd15d3c musl: deal with zig rename of i386 to x86
musl uses "i386" for this while Zig has switched to "x86".
2023-06-20 12:55:38 -04:00
Jacob Young
51cbb081e9 wasm: fix decl alignment 2023-06-20 11:36:50 -04:00
Jakub Konka
eb1050b83a macho: fix 32bit compilation issues 2023-06-20 10:08:54 +02:00
Evan Haas
fb9d6b8bd9
codegen: Set c_char signedness based on the target 2023-06-20 00:26:42 -07:00
Andrew Kelley
a72d634b73
Merge pull request #16046 from BratishkaErik/issue-6128
Renaming `@xtoy` to `@YfromX`
2023-06-19 22:36:24 -07:00
Jacob Young
8bd07fb1be Sema: fix type in a @floatToInt safety check
Unblocks #15981
2023-06-19 13:53:44 -07:00
Jakub Konka
22540e5402 macho: exclude code signature padding from uuid calculation 2023-06-19 22:50:08 +02:00
Andrew Kelley
7d6fcf0831
Merge pull request #15012 from xxxbxxx/objcpy-elf-compress
objcopy: add support for --compress-debug-sections
2023-06-19 13:15:40 -07:00
Jacob Young
8fcc28d302 Module: add support for multiple global asm blocks per decl
Closes #16076
2023-06-19 13:12:04 -07:00