153 Commits

Author SHA1 Message Date
Sam Connelly 17e0afd0e5 feat: init eZ80 arch via CBE 2026-04-16 19:21:16 +02:00
Justus Klausecker 047df44d71 cbe: fix switch statements on large types
`switch` statements on types >128bits are now lowered to conditionals.
This is necessary because Zig lowers integers with more than 128 bits to
bigints, which are not 'native' integers and thus cannot be used as case
values.
128-bit integers get special treatment because Zig will emit actual 128bit
ints if they are supported by the target. They still *may* be lowered to
bigints though, e.g. for 32-bit targets or MSVC. To solve this, this commit
adds a bunch of switch macros to `zig.h` which will either resolve to an
actual `switch` statement or to conditionals. The `if` statements this
approach can generate are not as optimal as they could be but I think this
is a good trade-off since the generated `switch` statements are still the
same as the ones generated for smaller integers. Also the macros result
in pretty readable code.
2026-03-18 15:47:18 +01:00
Matthew Lugg 3a3ac10345 cbe: fix type layouts, and statically assert them 2026-03-10 10:26:15 +00:00
Matthew Lugg 5cc12da1c0 cbe: rework CType and other major refactors
The goal of these changes is to allow the C backend to support the new
lazier type resolution system implemented by the frontend. This required
a full rewrite of the `CType` abstraction, and major changes to the C
backend "linker".

The `DebugConstPool` abstraction introduced in a previous commit turns
out to be useful for the C backend to codegen types. Because this use
case is not debug information but rather general linking (albeit when
targeting an unusual object format), I have renamed the abstraction to
`ConstPool`. With it, the C linker is told when a type's layout becomes
known, and can at that point generate the corresponding C definitions,
rather than deferring this work until `flush`.

The work done in `flush` is now more-or-less *solely* focused on
collecting all of the buffers into a big array for a vectored write.
This does unfortunately involve a non-trivial graph traversal to emit
type definitions in an appropriate order, but it's still quite fast in
practice, and it operates on fairly compact dependency data. We don't
generate the actual type *definitions* in `flush`; that happens during
compilation using `ConstPool` as discussed above. (We do generate the
typedefs for underaligned types in `flush`, but that's a trivial amount
of work in most cases.)

`CType` is now an ephemeral type: it is created only when we render a
type (the logic for which has been pushed into just 2 or 3 functions in
`codegen.c`---most of the backend now operates on unmolested Zig `Type`s
instead). C types are no longer stored in a "pool", although the type
"dependencies" of generated C code (that is, the struct, unions, and
typedefs which the generated code references) are tracked (in some
simple hash sets) and given to the linker so it can codegen the types.
2026-03-10 10:26:12 +00:00
Jacob Young b5bd494606 std.Threaded: replace more kernel32 functions with ntdll 2026-02-07 00:02:50 -05:00
Matthew Lugg 5df5e2ed26 zig.h: drop dependency on deleted compiler_rt functions
It turns out we did use these in the C backend. However, it's really
just as easy, if not easier, to replicate the logic directly in C.

Synchronizes stage1/zig.h to make sure the bootstrap doesn't depend on
these functions either. The actual zig1 tarball is unmodified because
regenerating it is unnecessary in this instance.
2025-11-12 16:00:16 +00:00
Alex Rønne Petersen f5d31cff71 zig.h: add kvx support 2025-11-10 09:40:44 +01:00
Alex Rønne Petersen a7119d4269 remove all IBM AIX and z/OS support
As with Solaris (dba1bf9353), we have no way to
actually audit contributions for these OSs. IBM also makes it even harder than
Oracle to actually obtain these OSs.

closes #23695
closes #23694
closes #3655
closes #23693
2025-10-29 14:25:51 +01:00
GasInfinity 9d3bd3c502 feat: init 16-bit x86 support in zig.h 2025-10-27 11:19:08 +01:00
Alex Rønne Petersen afc6e88413 zig.h: add or1k support 2025-10-18 22:26:52 +02:00
mlugg 3f84b6c80e cbe: workaround GCC miscompilation
This was causing a zig2 miscomp, which emitted slightly broken debug
information, which caused extremely slow stack unwinding. We're working
on fixing or reporting this upstream, but we can use this workaround for
now, because GCC guarantees arithmetic signed shift.
2025-09-30 13:44:56 +01:00
Alex Rønne Petersen 6208e74145 std.zig.system: implement native CPU detection for LoongArch
ref #4591
2025-09-18 12:42:24 +02:00
Jacob Young 38dfa6537e cbe: emit nonstring attribute
Closes #24545
2025-08-11 11:30:51 -07:00
Jacob Young 0bf8617d96 x86_64: add support for pie executables 2025-06-06 23:42:14 -07:00
Jacob Young 58d2bd601e x86_64: rewrite scalar <<|
Closes #23035
2025-05-17 18:00:17 -04:00
dweiller 898ca82458 compiler: add @memmove builtin 2025-04-26 13:34:16 +10:00
Alex Rønne Petersen e11ac02662 cbe: Implement support for -fno-builtin and @disableIntrinsics(). 2025-02-23 04:08:58 +01:00
Alex Rønne Petersen d7f9b5a661 zig.h: Add new zig_gcc macro to indicate GCC.
As opposed to "GNU C" (zig_gnuc) which includes Clang.

This addresses #22890 but does not fix it until a zig1.wasm update.
2025-02-16 00:21:51 +01:00
Jacob Young 5433e0438c cbe: fix ub triggered by mulw overflowing the promoted type
Closes #21914
2025-02-10 17:22:16 -08:00
Vahur Sinijärv c4445bc156 Fix issues in zig.h when building with clang-cl on Windows (#20331)
Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
2025-01-29 23:21:44 +00:00
Alex Rønne Petersen aa37a5a0c4 zig.h: Add some casts to __atomic macros to shut Clang warnings up.
This is a stupid Clang-ism:

    ❯ cat test.c
    int main() {
        int value = 42;
        int const *value_ptr = &value;
        int location;
        __atomic_store(&location, value_ptr, __ATOMIC_SEQ_CST);
    }
    ❯ gcc test.c -fsyntax-only
    ❯ clang test.c -fsyntax-only
    test.c:5:31: warning: passing 'const int *' to parameter of type 'int *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
        5 |     __atomic_store(&location, value_ptr, __ATOMIC_SEQ_CST);
          |                               ^~~~~~~~~
    1 warning generated.

I have no idea why Clang doesn't define these builtins as taking const pointers
for the parameters that are only read from. Anyway, after the next zig1.wasm
update, this change should shut up these warnings that we've been seeing in CI
during bootstrap for ages.
2025-01-28 13:01:35 +01:00
Alex Rønne Petersen c27488a387 zig.h: Add zig_gnuc_asm macro to indicate availability of GNU-style inline asm. 2024-12-03 12:11:05 +01:00
Alex Rønne Petersen f058b147d3 zig.h: Introduce operating system macros. 2024-12-03 12:07:34 +01:00
Alex Rønne Petersen 084ec97811 zig.h: Introduce object format macros. 2024-12-03 12:07:34 +01:00
Alex Rønne Petersen ace8396076 zig.h: Move some macros and includes closer to where they're used. 2024-12-03 12:07:34 +01:00
Alex Rønne Petersen 0e934c7df1 zig.h: Use C99/C11 headers if available regardless of language standard.
If present, these headers are usable even when compiling for older C language
versions.

Most notably, this enables zig.h atomics to work with slimcc and TinyCC in C99
mode (and earlier).
2024-12-03 12:07:34 +01:00
Alex Rønne Petersen 1c43c2c209 zig.h: Remove duplicate and unguarded stdint.h include.
We already do this for C99 only later in the file.
2024-12-03 12:07:34 +01:00
Alex Rønne Petersen 125bf05c27 zig.h: Implement zig_unreachable() with __assume(0) for MSVC. 2024-12-03 12:07:34 +01:00
Alex Rønne Petersen 062feb6c82 zig.h: Implement zig_frame_address() with _AddressOfReturnAddress() for MSVC. 2024-12-03 12:07:34 +01:00
Alex Rønne Petersen 37538f19cd zig.h: Use zig_*_windows_teb() inline assembly with TinyCC and slimcc. 2024-12-03 12:07:34 +01:00
Alex Rønne Petersen 204df19d79 zig.h: Avoid depending on cpuid.h for zig_x86_cpuid().
Just use inline assembly instead, which is what that header does. Also make sure
we have a dummy implementation when inline assembly is unavailable.
2024-12-03 12:07:32 +01:00
Alex Rønne Petersen 24b57f4d30 zig.h: Introduce architecture macros to simplify checks throughout. 2024-12-03 11:57:21 +01:00
Alex Rønne Petersen ba66814185 zig.h: Don't use __debugbreak() for MinGW.
It seems like only Clang supports this in MinGW mode, and Clang will already
take the __builtin_debugtrap() path anyway.
2024-12-03 11:57:20 +01:00
Alex Rønne Petersen d19e1919da zig.h: Define zig_gnuc for both GCC and Clang and stop using __GNUC__.
There's only one spot in the header where we want to handle GCC but not Clang,
so just add an extra check there.
2024-12-03 11:57:20 +01:00
Alex Rønne Petersen dd1d9cce3c zig.h: Add a zig_msvc macro to be used instead of _MSC_VER. 2024-12-03 11:57:20 +01:00
Alex Rønne Petersen f29daebd90 zig.h: Use __thread for slimcc in pre-C11 mode. 2024-12-03 11:57:20 +01:00
Alex Rønne Petersen fa6946f645 zig.h: Add support for C23 keywords.
The old spellings from C11 are deprecated in C23.
2024-12-03 11:57:20 +01:00
Alex Rønne Petersen 023a7fefb1 zig.h: Add TinyCC support where appropriate.
tcc doesn't have __has_attribute and __has_builtin, so we need to manually check
for tcc in these preprocessor paths.
2024-12-03 11:57:20 +01:00
Alex Rønne Petersen 43a1b88bc8 zig.h: Use __BYTE_ORDER__ instead of __LITTLE_ENDIAN__ for endianness check.
The former is supported by most compilers while the latter is Clang-only.
2024-12-03 11:57:20 +01:00
Alex Rønne Petersen aa19e71a27 zig.h: Add zig_trap() and zig_breakpoint() implementations for Hexagon. 2024-11-24 08:19:15 +01:00
Alex Rønne Petersen 6b2c8fc688 zig.h: Improve portability of zig_*_windows_teb() helpers.
* Make it work for thumb and aarch64.
* Clean up std.os.windows.teb() a bit.

I also updated stage1/zig.h since the changes are backwards-compatible and are
necessary due to the std.os.windows changes that call the newly-added functions.
2024-11-04 11:59:38 +01:00
David Rubin 043b1adb8d remove @fence (#21585)
closes #11650
2024-10-04 22:21:27 +00:00
Alex Rønne Petersen 5daf47a212 zig.h: Improve portability of zig_trap() and zig_breakpoint(). 2024-09-23 17:34:15 -07:00
kcbanner 2a0238e5e3 zig.h: fixup pointer atomic load definitions 2024-07-26 10:42:10 -04:00
kcbanner c318710653 zig.h: expand zig_msvc_atomic_load_ into version for relaxed, acquire, and seq_cst 2024-07-13 19:45:45 -04:00
kcbanner bc28454b43 zig.h: replace _InterlockedExchangeAdd with a plain volatile load
This was causing zig2.exe to crash during bootstrap, because there was an atomic
load of read-only memory, and the attempt to write to it as part of the (idempotent)
atomic exchange was invalid.

Aligned reads (of u32 / u64) are atomic on x86 / x64, so this is replaced with an
optimization-proof load (`__iso_volatile_load8*`) and a reordering barrier.
2024-07-13 17:46:24 -04:00
Jacob Young 00da182e68 cbe: fix for export changes 2024-07-04 21:01:42 +01:00
kcbanner 0d12caee29 - Fix ypedef -> typedef 2024-06-21 00:12:13 -04:00
Andrew Kelley 9be8a9000f Revert "implement @expect builtin (#19658)"
This reverts commit a7de02e052.

This did not implement the accepted proposal, and I did not sign off
on the changes. I would like a chance to review this, please.
2024-05-22 09:57:43 -07:00
David Rubin a7de02e052 implement @expect builtin (#19658)
* implement `@expect`

* add docs

* add a second arg for expected bool

* fix typo

* move `expect` to use BinOp

* update to newer langref format
2024-05-22 10:51:16 -05:00