aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
AgeCommit message (Collapse)Author
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
2022-01-15stage2: fix Decl garbage collection not marking enoughAndrew Kelley
It is the job of codegen backends to mark Decls that are referenced as alive so that the frontend does not sweep them with the garbage. This commit unifies the code between the backends with an added method on Decl. The implementation is more complete than before, switching on the Decl val tag and recursing into sub-values. As a result, two more array tests are passing.
2022-01-13stage2: detection of comptime array literalsAndrew Kelley
Introduce `validate_array_init_comptime`, similar to `validate_struct_init_comptime` introduced in 713d2a9b3883942491b40738245232680877cc66. `zirValidateArrayInit` is improved to detect comptime array literals and emit AIR accordingly. This code is very similar to the changes introduced in that same commit for `zirValidateStructInit`. The C backend needed some improvements to continue passing the same set of tests: * `resolveInst` for arrays now will add a local `static const` with the array value and so then `elem_val` instructions reference that local. It memoizes accesses using `value_map`, which is changed to use `Air.Inst.Ref` as the key rather than `Air.Inst.Index`. * This required a mechanism for writing to a "header" which is lines that appear at the beginning of a function body, before everything else. * dbg_stmt output comments rather than `#line` directives. TODO comment reproduced here: We need to re-evaluate whether to emit these or not. If we naively emit these directives, the output file will report bogus line numbers because every newline after the #line directive adds one to the line. We also don't print the filename yet, so the output is strictly unhelpful. If we wanted to go this route, we would need to go all the way and not output newlines until the next dbg_stmt occurs. Perhaps an additional compilation option is in order? `Value.elemValue` is improved to support `elem_ptr` values.
2022-01-13stage2: fix build on 32-bit ISAsAndrew Kelley
Fixes regression introduced in 93b854eb745ab3294054ae71150fe60f134f4d10.
2022-01-12stage2: implement `@ctz` and `@clz` including SIMDAndrew Kelley
AIR: * `array_elem_val` is now allowed to be used with a vector as the array type. * New instructions: splat, vector_init AstGen: * The splat ZIR instruction uses coerced_ty for the ResultLoc, avoiding an unnecessary `as` instruction, since the coercion will be performed in Sema. * Builtins that accept vectors now ignore the type parameter. Comment from this commit reproduced here: The accepted proposal #6835 tells us to remove the type parameter from these builtins. To stay source-compatible with stage1, we still observe the parameter here, but we do not encode it into the ZIR. To implement this proposal in stage2, only AstGen code will need to be changed. Sema: * `clz` and `ctz` ZIR instructions are now handled by the same function which accept AIR tag and comptime eval function pointer to differentiate. * `@typeInfo` for vectors is implemented. * `@splat` is implemented. It takes advantage of `Value.Tag.repeated` 😎 * `elemValue` is implemented for vectors, when the index is a scalar. Handling a vector index is still TODO. * Element-wise coercion is implemented for vectors. It could probably be optimized a bit, but it is at least complete & correct. * `Type.intInfo` supports vectors, returning int info for the element. * `Value.ctz` initial implementation. Needs work. * `Value.eql` is implemented for arrays and vectors. LLVM backend: * Implement vector support when lowering `array_elem_val`. * Implement vector support when lowering `ctz` and `clz`. * Implement `splat` and `vector_init`.
2022-01-08stage2: @errorName sema+llvmRobin Voetter
2022-01-06Sema: const inferred alloc infers comptime-nessAndrew Kelley
const locals now detect if the value ends up being comptime known. In such case, it replaces the runtime AIR instructions with a decl_ref const. In the backends, some more sophisticated logic for marking decls as alive was needed to prevent Decls incorrectly being garbage collected that were indirectly referenced in such manner.
2021-12-30CBE; implement airLoad and airStore for arrays (#10452)drew
Effectively a small continuation of #10152 This allows the for.zig behavior tests to pass. Unfortunately to fully test everything I had to move a lot of behavior tests from array.zig; most of them now pass (sorry @rainbowbismuth!) I'm also conflicted on how I store constants into arrays because it's kind of stupid; array's can't be re-initialized using the same syntax, so instead of initializing each element, a new array is made which is copied into the destination. This also required that renderValue can't emit string literals for byte arrays given that they need to always have an extra byte for the NULL terminator, meaning that strings are no longer grep-able in the output.
2021-12-27stage2: make tests/behaviour/void.zig work with c backendMatthew Hall
* fix initialisation of void* fields of structs (initialises to 0xaa.. rather than {}) * don't generate struct fields when the field type does not have codegen bits * in airAlloc generate a void* literal if the element type does not have codegen bits
2021-12-27stage2: LLVM backend: implement `@tagName` for enumsAndrew Kelley
Introduced a new AIR instruction: `tag_name`. Reasons to do this instead of lowering it in Sema to a switch, function call, array lookup, or if-else tower: * Sema is a bottleneck; do less work in Sema whenever possible. * If any optimization passes run, and the operand to becomes comptime-known, then it could change to have a comptime result value instead of lowering to a function or array or something which would then have to be garbage-collected. * Backends may want to choose to use a function and a switch branch, or they may want to use a different strategy. Codegen for `@tagName` is implemented for the LLVM backend but not any others yet. Introduced some new `Type` tags: * `const_slice_u8_sentinel_0` * `manyptr_const_u8_sentinel_0` The motivation for this was to make typeof() on the tag_name AIR instruction non-allocating. A bunch more enum tests are passing now.
2021-12-21C backend: implement `ret_addr`Andrew Kelley
2021-12-21stage2: @shlWithOverflowRobin Voetter
2021-12-21stage2: @subWithOverflowRobin Voetter
2021-12-21stage2: @mulWithOverflowRobin Voetter
2021-12-21stage2: @returnAddress()Robin Voetter
2021-12-21stage2: @addWithOverflowRobin Voetter
2021-12-21stage2: allow multiple inferred error sets per FnRobin Voetter
This allows the inferred error set of comptime and inline invocations to be resolved separately from the inferred error set of the runtime version or other comptime/inline invocations.
2021-12-21stage2: move inferred error set state into funcRobin Voetter
2021-11-30allocgate: renamed getAllocator function to allocatorLee Cannon
2021-11-30allocgate: stage 1 and 2 buildingLee Cannon
2021-11-30allocgate: std Allocator interface refactorLee Cannon
2021-11-30std lib API deprecations for the upcoming 0.9.0 releaseAndrew Kelley
See #3811
2021-11-24stage2: fix unwrap function call with optional pointer return valueAndrew Kelley
2021-11-23C backend: avoid branching multiple times on AIR tagAndrew Kelley
for cmp_eq and cmp_neq.
2021-11-23C backend: errors and optionalsScibuild
* bitcast treats all pointers as pointers * correctly unwrapping error unions with pointers * equality operators for primitive optional types
2021-11-16stage2: LLVM backend: memset to 0xaa for undefined storesAndrew Kelley
Also support `one` and `int_big_positive` tags for const pointers.
2021-11-16correct misnamed variables caused by copy-pastedrew
2021-11-16fix array airStoreUndefined for arraysdrew
2021-11-16add generics behavior testdrew
-airLoad and airStore now properly report an error if they are used with an array, instead of having the C compiler emit a vague error -airStoreUndefined now works with array types -structFieldPtr now works with array types, allowing generics' tests to pass
2021-11-16simplify thingsdrew
2021-11-16fix assumption where all positive big ints are unsigneddrew
2021-11-16make it more clear we should do UB wrapping optimizations for ptr arithmeticdrew
2021-11-16small changes + align tests obviously shouldn't have passeddrew
2021-11-16fmtdrew
2021-11-16C backend: basic big ints, fix airPtrToInt, array references, pointer ↵drew
arithmetic UB with NULL, implement airPtrElemPtr/Val, fix redundant indirection/references with arrays -add additional test cases that were found to be passing -add basic int128 test cases which previously did not pass but weren't covered -most test cases in cast.zig now pass -i128/u128 or smaller int constants can now be rendered -unsigned int constants are now always suffixed with 'u' to prevent random compile errors -pointers with a val tag of 'zero' now just emit a 0 constant which coerces to the pointer type and fixes some warnings with ordered comparisons -pointers with a val tag of 'one' are now casted back to the pointer type -support pointers with a u64 val -fix bug where rendering an array's type will emit more indirection than is needed -render uint128_t/int128_t manually when needed -implement ptr_add/sub AIR handlers manually so they manually cast to int types which avoids UB if the result or ptr operand is NULL -implement airPtrElemVal/Ptr -airAlloc for arrays will not allocate a ref as the local for the array is already a reference/pointer to the array itself -fix airPtrToInt by casting to the int type
2021-11-14CBE: memset(..., 0xaa, ...) undefined valuesDaniele Cocca
This commit makes airStore() handle undefined values directly instead of delegating to renderValue(): the call to renderValue() happens too late, when "dest = " has already been written to the stream, at which point there's no sane way to initialize e.g. struct values by assignment. Instead, we make airStore() use memset(dest, 0xaa, sizeof(dest)), which should transparently handle all types. Also moves the newly-passing tests to the top of test/behavior.zig.
2021-11-11c codegen: fix airIsNull with pointersJacob G-W
2021-11-10C backend: Improve lowering of Zig types to C typesThomas Ives
1. Changed Zig pointers to functions to be typedef'd so then we can treat them the same as other types. 2. Distinguished between const slices (zig_L prefix) and mut slices (zig_M prefix). 3. Changed lowering of Zig "const pointers" (e.g. *const u8) to to C "pointers to const" (e.g. const char *) rather than C "const pointers" (e.g. char * const) 4. Ensured that all typedefs are "linked" even if the decl doesn't require any forward declarations 5. Added test that exercises function pointer type rendering 6. Changed .slice_ptr instruction to allocate pointer local rather than a uintptr_t local
2021-11-09Sema: implement coerce_result_ptr for optionalsAndrew Kelley
New AIR instruction: `optional_payload_ptr_set` It's like `optional_payload_ptr` except it sets the non-null bit. When storing to the payload via a result location that is an optional, `optional_payload_ptr_set` is now emitted. There is a new algorithm in `zirCoerceResultPtr` which stores a dummy value through the result pointer into a temporary block, and then pops off the AIR instructions from the temporary block in order to determine how to transform the result location pointer in case any in-between coercions need to happen. Fixes a couple of behavior tests regarding optionals.
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-11-08C backend: restore handling of .NoReturn in function signatureEmily Bellows
2021-11-08C backend: while, struct tests, better undefined global handlingEmily Bellows
1. Function signatures that return a no member struct return void 2. Undefined var decls don't get a value generated for them 3. Don't generate bitcast code if the result isn't used, since bitcast is a pure function. Right now struct handling code generates some weird unused bitcast AIR, and this optimization side steps that issue.
2021-11-02C backend: implement ?void, and other zero sized typesEmily Bellows
2021-10-30C backend: implement signed truncEmily Bellows
2021-10-29stage2: implement `@popCount` for non-vectorsAndrew Kelley
2021-10-28C backend: implement trunc for unsigned non-pow2 intsAndrew Kelley
2021-10-28C backend: implement trunc instructionAndrew Kelley
Note that there is not any test coverage yet for integer truncation involving non-power-of-two integers.