aboutsummaryrefslogtreecommitdiff
path: root/src/link/C
AgeCommit message (Collapse)Author
2022-07-27move zig.h to become an installation fileAndrew Kelley
Now instead of zig.h being baked into the compiler binary, it is a header file distributed along with all the other header files distributed with Zig. Closes #11643
2022-05-16zig.h: add casts for overflow arithmetic operationsAndrew Kelley
This avoids the following error: ``` error: incompatible pointer types passing 'int64_t *' (aka 'long long *') to parameter of type 'long *' overflow = __builtin_saddl_overflow(lhs, rhs, res); ^~~ ``` My previous understanding was that this error would not occur because prior to this line we check that int64_t is equivalent to long, like this: ```c ``` However, it appears that this is still a warning in C if int64_t is primarily aliased to `long long`, even though `long` and `long long` are the same thing.
2022-05-16zig.h: improve overflow shlAndrew Kelley
* zig_addo_u128: fix type-o * redo the shift-left overflow inline functions. no need to depend on compiler-rt.
2022-05-16C backend: implement overflow arithmeticAndrew Kelley
Most of the work here was additions to zig.h. The lowering code is mainly responsible for calling the correct function name depending on the operand type. Some of the compiler-rt calls here are not implemented yet and are non-standard symbols due to the C programming language not needing them. After this commit, the behavior tests with -ofmt=c are passing again.
2022-03-19CBE: implement mod, divFloor, divTruncDaniele Cocca
2022-03-15CBE: `#undef linux` in zig.hDaniele Cocca
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]: https://github.com/ziglang/zig/blob/8c32d989c995f8675f1824fb084245b833b26223/lib/std/target.zig#L224 [^2]: https://gcc.gnu.org/onlinedocs/cpp/System-specific-Predefined-Macros.html#System-specific-Predefined-Macros
2022-03-14CBE: split {clz,ctz,mod,popCount,byteSwap,bitReverse} by typeDaniele Cocca
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-14Remove signed_type from zig_{clz,ctz,popcount}Daniele Cocca
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-13CBE: implement popCount, byteSwap, bitReverse for ints <= 128 bitsDaniele Cocca
This folds the airCountZeroes() code from 226fcd7c709ec664c5d883042cf7beb3026f66cb back into airBuiltinCall(), since most of these builtins happen to require the same arguments and can be unified under a common function signature.
2022-03-11CBE: implement clz, ctz for ints <= 128 bitsDaniele Cocca
2022-03-03stage2: cleanups to wasm memory intrinsicsAndrew Kelley
* AIR: use pl_op instead of ty_pl for wasm_memory_size. No need to store the type because the type is always `u32`. * AstGen: use coerced_ty for `@wasmMemorySize` and `@wasmMemoryGrow` and do the coercions in Sema. * Sema: use more accurate source locations for errors. * Provide more information in the compiler error message. * Codegen: use liveness data to avoid lowering unused `@wasmMemorySize`. * LLVM backend: add implementation - I wasn't able to test it because we are hitting a linker error for `-target wasm32-wasi -fLLVM`. * C backend: use `zig_unimplemented()` instead of silently doing wrong behavior for these builtins. * behavior tests: branch only on stage2_arch for inclusion of the wasm.zig file. We would change it to `builtin.cpu.arch` but that is causing a compiler crash on some backends.
2022-03-03cbe: Implement wasm builtinsLuuk de Gram
This implements the wasm builtins by lowering to builtins that are supported by c-compilers. In this case: Clang. This also simplifies the `AIR` instruction as it now uses the payload field of `ty_pl` and `pl_op` directly to store the index argument rather than storing it inside Extra. This saves us 4 bytes per builtin call.
2022-01-23c backend: Implement aligning fields and local/global variablesJimmi Holst Christensen
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-18stage2: implement `@prefetch`Andrew Kelley
This reverts commit f423b5949b8722d4b290f57c3d06d015e39217b0, re-instating commit d48e4245b68bf25c7f41804a5012ac157a5ee546.
2022-01-18Revert "stage2: implement `@prefetch`"Andrew Kelley
This reverts commit d48e4245b68bf25c7f41804a5012ac157a5ee546. I have no idea why this is failing Drone CI, but in a branch, reverting this commit solved the problem.
2022-01-15stage2: implement `@prefetch`Andrew Kelley
2021-12-21C backend: implement `ret_addr`Andrew Kelley
2021-11-09stage2: Add support for floats in the C backend (#10059)Zen1th
* Implement float type * Fix int and float undefined value * Handle NaN constants, preserving bit pattern
2021-10-19src/link/C/zig.h: Fix indentSizhe Zhao
2021-09-28saturating arithmetic modificationsAndrew Kelley
* Remove the builtins `@addWithSaturation`, `@subWithSaturation`, `@mulWithSaturation`, and `@shlWithSaturation` now that we have first-class syntax for saturating arithmetic. * langref: Clarify the behavior of `@shlExact`. * Ast: rename `bit_shift_left` to `shl` and `bit_shift_right` to `shr` for consistency. * Air: rename to include underscore separator with consistency with the rest of the ops. * Air: add shl_exact instruction * Use non-extended tags for saturating arithmetic, to keep it simple so that all the arithmetic operations can be done the same way. - Sema: unify analyzeArithmetic with analyzeSatArithmetic - implement comptime `+|`, `-|`, and `*|` - allow float operands to saturating arithmetic * `<<|` allows any integer type for the RHS. * C backend: fix rebase conflicts * LLVM backend: reduce the amount of branching for arithmetic ops * zig.h: fix magic number not matching actual size of C integer types
2021-09-28sat-arithmetic: fix shl methods in cbeTravis Staloch
2021-09-28sat-arithmetic: fix docgenTravis Staloch
2021-09-28sat-arithmetic: add c backend supportTravis Staloch
- modify AstGen binOpExt()/assignBinOpExt() to accept generic extended payload T - rework Sema zirSatArithmetic() to use existing sema.analyzeArithmetic() by adding an `opt_extended` parameter. - add airSatOp() to codegen/c.zig - add saturating functions to src/link/C/zig.h
2021-09-24stage2: implement `@memset` and `@memcpy` builtinsAndrew Kelley
2021-09-15stage2: implement `@atomicRmw` and `@atomicLoad`Andrew Kelley
* langref: add some more "see also" links for atomics * Add the following AIR instructions - atomic_load - atomic_store_unordered - atomic_store_monotonic - atomic_store_release - atomic_store_seq_cst - atomic_rmw * Implement those AIR instructions in LLVM and C backends. * AstGen: make the `ty` result locations for `@atomicRmw`, `@atomicLoad`, and `@atomicStore` be `coerced_ty` to avoid unnecessary ZIR instructions when Sema will be doing the coercions redundantly. * Sema for `@atomicLoad` and `@atomicRmw` is done, however Sema for `@atomicStore` is not yet implemented. - comptime eval for `@atomicRmw` is not yet implemented. * Sema: flesh out `coerceInMemoryAllowed` a little bit more. It can now handle pointers.
2021-09-15stage2: implement `@fence`Andrew Kelley
2021-09-14stage2: implement cmpxchg and improve comptime evalAndrew Kelley
* Implement Sema for `@cmpxchgWeak` and `@cmpxchgStrong`. Both runtime and comptime codepaths are implement. * Implement Codegen for LLVM backend and C backend. * Add LazySrcLoc.node_offset_builtin_call_argX 3...5 * Sema: rework comptime control flow. - `error.ComptimeReturn` is used to signal that a comptime function call has returned a result (stored in the Inlining struct). `analyzeCall` notices this and handles the result. - The ZIR instructions `break_inline`, `block_inline`, `condbr_inline` are now redundant and can be deleted. `break`, `block`, and `condbr` function equivalently inside a comptime scope. - The ZIR instructions `loop` and `repeat` also are modified to directly perform comptime control flow inside a comptime scope, skipping an unnecessary mechanism for analysis of runtime code. This makes Zig perform closer to an interpreter when evaluating comptime code. * Sema: zirRetErrValue looks at Sema.ret_fn_ty rather than sema.func for adding to the inferred error set. This fixes a bug for inlined/comptime function calls. * Implement ZIR printing for cmpxchg. * stage1: make cmpxchg respect --single-threaded - Our LLVM C++ API wrapper failed to expose this boolean flag before. * Fix AIR printing for struct fields showing incorrect liveness data.
2021-07-08C backend: cleanups to wrapping int operationsAndrew Kelley
* less branching by passing parameters in the main op code switch. * properly pass the target when asking the type system for int info. * handle u8, i16, etc when it is represented using int_unsigned/int_signed tag. * compile error instead of assertion failure for unimplemented cases (greater than 64 bits integer). * control flow cleanups * zig.h: expand macros into inline functions * reduce the complexity of the test case by making it one test case that calls multiple functions. Also fix the problem of c_int max value mismatch between host and target.
2021-07-08C backend: add/sub/mul wrapping for the C backendMatt Knight
2021-07-07stage2: implement `@panic` and beginnigs of inferred error setsAndrew Kelley
* ZIR: add two instructions: - ret_err_value_code - ret_err_value * AstGen: add countDefers and utilize it to emit more efficient ZIR for return expressions in the presence of defers. * AstGen: implement |err| payloads for `errdefer` syntax. - There is not an "unused capture" error for it yet. * AstGen: `return error.Foo` syntax gets a hot path in return expressions, using the new ZIR instructions. This also is part of implementing inferred error sets, since we need to tell Sema to add an error value to the inferred error set before it gets coerced. * Sema: implement `@setCold`. - Implement `@setCold` support for C backend. * `@panic` and regular safety panics such as `unreachable` now properly invoke `std.builtin.panic`. * C backend: improve pointer and function value rendering. * C linker: fix redundant typedefs. * Add Type.error_set_inferred. * Fix Value.format for enum_literal, enum_field_index, bytes. * Remove the C backend test that checks for identical text I measured a 14% reduction in Total ZIR Bytes from master branch for std/os.zig.
2021-05-11stage2: add `owns_tv` flag to `Module.Decl`Andrew Kelley
Decl objects need to know whether they are the owner of the Type/Value associated with them, in order to decide whether to destroy the associated Namespace, Fn, or Var when cleaning up.
2021-01-05stage2: C backend: re-implement emit-hAndrew Kelley
and also mark functions as `extern "C"` as appropriate to support c++ compilers.
2021-01-05stage2: C backend: remove format() hackeryAndrew Kelley
All C backend tests passing now, except for emit-h tests. Next task in the branch is to restore emit-h.
2021-01-05stage2: rework the C backendAndrew Kelley
* std.ArrayList gains `moveToUnmanaged` and dead code `ArrayListUnmanaged.appendWrite` is deleted. * emit_h state is attached to Module rather than Compilation. * remove the implementation of emit-h because it did not properly integrate with incremental compilation. I will re-implement it in a follow-up commit. * Compilation: use the .codegen_failure tag rather than .dependency_failure tag for when `bin_file.updateDecl` fails. C backend: * Use a CValue tagged union instead of strings for C values. * Cleanly separate state into Object and DeclGen: - Object is present only when generating a .c file - DeclGen is present for both generating a .c and .h * Move some functions into their respective Object/DeclGen namespace. * Forward decls are managed by the incremental compilation frontend; C backend no longer renders function signatures based on callsites. For simplicity, all functions always get forward decls. * Constants are managed by the incremental compilation frontend. C backend no longer has a "constants" section. * Participate in incremental compilation. Each Decl gets an ArrayList for its generated C code and it is updated when the Decl is updated. During flush(), all these are joined together in the output file. * The new CValue tagged union is used to clean up using of assigning to locals without an additional pointer local. * Fix bug with bitcast of non-pointers making the memcpy destination immutable.