Commit Graph

127 Commits

Author SHA1 Message Date
Scibuild
2e15a404e2 C backend: errors and optionals
* bitcast treats all pointers as pointers
 * correctly unwrapping error unions with pointers
 * equality operators for primitive optional types
2021-11-23 13:46:56 -07:00
Andrew Kelley
e08b6149ab Sema: fix alignment of type-inferred locals 2021-11-22 20:30:20 -07:00
Andrew Kelley
af1a5751d9
Merge pull request #10165 from jmc-88/cbe
CBE: add .optional_single_mut_pointer and .optional_single_const_pointer to Type.childType
2021-11-22 21:36:24 -05:00
Robin Voetter
2b589d71fb stage2: move some tests which are now passing 2021-11-22 04:36:57 +01:00
Daniele Cocca
b3f4abbc5a CBE: mark align.zig and error.zig as passing
Newly passing as of 3794a32d89.
2021-11-17 23:43:19 +00:00
drew
0249344a47 cast -> cast_llvm, cast_c -> cast (doesn't work on LLVM backend) 2021-11-16 16:51:31 -07:00
drew
cf99afc525 add generics behavior test
-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-16 16:51:31 -07:00
drew
ad4627ea3b small changes + align tests obviously shouldn't have passed 2021-11-16 16:51:31 -07:00
drew
9bf1681990 C backend: basic big ints, fix airPtrToInt, array references, pointer 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-16 16:51:31 -07:00
LemonBoy
952d865bd2 stage1: Fix caching of LLVM builtin fns
The cache entry must take into account the fact some functions operate on scalar types and some other on vectors of scalar types.

Fixes #10147
2021-11-16 16:35:56 -05:00
Daniele Cocca
29f531bec9 CBE: memset(..., 0xaa, ...) undefined values
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-14 22:49:50 -05:00
Emily Bellows
e3d638a49e C backend: while, struct tests, better undefined global handling
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-08 14:23:55 -05:00
Daniele Cocca
77ffffc7cc
Add more entries to the list of stage1+stage2+CBE passing tests (#10094)
* CBE: mark call.zig tests as passing

* CBE: mark enum.zig tests as passing

* CBE: mark defer.zig tests as passing

* CBE: mark hasdecl.zig tests as passing

* CBE: mark hasfield.zig tests as passing

* CBE: mark ptrcast.zig tests as passing

* CBE: mark bitcast.zig tests as passing

* CBE: mark pub_enum.zig tests as passing

* CBE: mark underscore.zig tests as passing

* CBE: mark usingnamespace.zig tests as passing

* CBE: mark bugs/655.zig tests as passing

* CBE: mark bugs/679.zig tests as passing

* CBE: mark bugs/704.zig tests as passing

* CBE: mark bugs/1486.zig tests as passing

* CBE: mark bugs/2346.zig tests as passing

* CBE: mark bugs/2889.zig tests as passing

* CBE: mark bugs/4560.zig tests as passing

* CBE: mark bugs/4769_a.zig tests as passing

* CBE: mark bugs/4769_b.zig tests as passing

* CBE: mark bugs/6850.zig tests as passing
2021-11-04 14:24:46 -04:00
Emily Bellows
674932e503 C backend: implement ?void, and other zero sized types 2021-11-02 12:45:29 -04:00
Emily Bellows
969bcb6a59 C backend: implement signed trunc 2021-10-30 16:09:55 -04:00
Andrew Kelley
d6067db062 stage2: implement @popCount for non-vectors 2021-10-29 17:49:02 -07:00
Andrew Kelley
98009a2f66 C backend: implement trunc instruction
Note that there is not any test coverage yet for integer
truncation involving non-power-of-two integers.
2021-10-28 17:41:45 -07:00
Andrew Kelley
5479c0f9ac C backend: fix @boolToInt 2021-10-28 17:33:05 -07:00
Andrew Kelley
1c93cf52d8 C backend: fix crash when number of Decls passes a threshold
The ensureUnusedCapacity did not reserve a big enough number. I changed
it to no longer guess the capacity because I saw that the number of
possible items was not determinable ahead of time and this can therefore
avoid allocating more memory than necessary.
2021-10-28 17:23:02 -07:00
Andrew Kelley
c59ee3157f C backend: fix ptrtoint and wrap_errunion_err 2021-10-28 17:05:17 -07:00
Andrew Kelley
9ed955e5ca behavior tests: add "passing for C backend" category 2021-10-28 16:37:12 -07:00
Robin Voetter
7062c8a886 stage2: comptime slice of pointer to hardcoded address 2021-10-25 20:41:15 +02:00
Andrew Kelley
01c1f41520 stage2: slice and alignment fixes
* Fix backend using wrong union field of the slice instruction.
 * LLVM backend properly sets alignment on global variables.
 * Sema: add coercion for *T to *[1]T
 * Sema: pointers to Decls with explicit alignment now have alignment
   metadata in them.
2021-10-22 17:12:12 -07:00
Andrew Kelley
069c83d58c stage2: change @bitCast to always be by-value
After a discussion about language specs, this seems like the best way to
go, because it's simpler to reason about both for humans and compilers.

The `bitcast_result_ptr` ZIR instruction is no longer needed.

This commit also implements writing enums, arrays, and vectors to
virtual memory at compile-time.

This unlocked some more of compiler-rt being able to build, which
in turn unlocks saturating arithmetic behavior tests.

There was also a memory leak in the comptime closure system which is now
fixed.
2021-10-22 15:35:35 -07:00
Andrew Kelley
0a6851cc6d stage2: implement comptime loads through casted pointers 2021-10-21 22:56:11 -07:00
Andrew Kelley
7f70c27e9d stage2: more division support
AIR:
 * div is renamed to div_trunc.
 * Add div_float, div_floor, div_exact.
   - Implemented in Sema and LLVM codegen. C backend has a stub.

Improvements to std.math.big.Int:
 * Add `eqZero` function to `Mutable`.
 * Fix incorrect results for `divFloor`.

Compiler-rt:
 * Add muloti4 to the stage2 section.
2021-10-21 19:05:26 -07:00
Robin Voetter
a3c9bfef30 stage2: truncation
* Also fixes a related case where big int truncate would assume that the
input fits in the output limbs buffer
2021-10-21 20:10:27 -04:00
Andrew Kelley
4cb5fed10b AstGen: make the index variable of inline for a alloc_comptime
Before it was being emitted as an `alloc` which caused inline for loops
to not work correctly.
2021-10-20 15:34:10 -07:00
Matthew Borkowski
79a3dfcfd8 astgen.zig: fix false positive in breakExpr's checking for store_to_block_ptr 2021-10-18 13:18:47 -04:00
Andrew Kelley
7b00bef6bf Sema: resolveTypeFields before accessing type fields 2021-10-17 21:53:59 -07:00
Meghan Denny
0ef2e2520a stage2: implement @hasField
struct and union are kept in stage1 because struct/unionFieldCount are returning zero
2021-10-17 21:42:22 -07:00
Meghan Denny
7d0a74456b alphebetize behavior tests 2021-10-17 21:42:22 -07:00
Andrew Kelley
e9d1e5e533 stage2: LLVM backend: lower constant field/elem ptrs 2021-10-17 17:02:20 -07:00
Andrew Kelley
fdd11f6cee Sema: coercion from error sets to anyerror 2021-10-16 12:41:03 -07:00
Andrew Kelley
82ec56e47e
Merge pull request #9954 from Snektron/shifts
Big int saturating left shift
2021-10-16 15:06:13 -04:00
Meghan Denny
6f30c8c098 elevate more passing tests 2021-10-16 15:04:48 -04:00
Robin Voetter
f6bf24b2f3 stage2: comptime saturating shl 2021-10-16 11:32:05 +02:00
Meghan
1df926b24e stage2: @hasDecl is passing 2021-10-16 04:09:16 -04:00
Andrew Kelley
cacd5366a6 stage2: LLVM backend: implement wrap_optional AIR
and move over some passing tests
2021-10-14 22:16:26 -07:00
Andrew Kelley
55eea3b045 stage2: implement @minimum and @maximum, including vectors
* std.os: take advantage of `@minimum`. It's probably time to
   deprecate `std.min` and `std.max`.
 * New AIR instructions: min and max
 * Introduce SIMD vector support to stage2
 * Add `@Type` support for vectors
 * Sema: add `checkSimdBinOp` which can be re-used for other arithmatic
   operators that want to support vectors.
 * Implement coercion from vectors to arrays.
   - In backends this is handled with bitcast for vector to array,
     however maybe we want to reduce the amount of branching by
     introducing an explicit AIR instruction for it in the future.
 * LLVM backend: implement lowering vector types
 * Sema: Implement `slice.ptr` at comptime
 * Value: improve `numberMin` and `numberMax` to support floats in
   addition to integers, and make them behave properly in the presence
   of NaN.
2021-10-14 21:17:30 -07:00
Andrew Kelley
ed5a5e2293 move behavior tests that are passing for stage2 2021-10-13 21:43:19 -07:00
Andrew Kelley
e851d89113 Sema: implement comptime coerce_result_ptr and alloc
This is progress towards being able to use `builtin.cpu.arch` instead of
the `stage2_arch` workaround. The next blocker is comptime `elemVal`.
2021-10-13 14:16:01 -07:00
Andrew Kelley
6d6cf59847 stage2: support nested structs and arrays and sret
* Add AIR instructions: ret_ptr, ret_load
   - This allows Sema to be blissfully unaware of the backend's decision
     to implement by-val/by-ref semantics for struct/union/array types.
     Backends can lower these simply as alloc, load, ret instructions,
     or they can take advantage of them to use a result pointer.
 * Add AIR instruction: array_elem_val
   - Allows for better codegen for `Sema.elemVal`.
 * Implement calculation of ABI alignment and ABI size for unions.
 * Before appending the following AIR instructions to a block,
   resolveTypeLayout is called on the type:
   - call - return type
   - ret - return type
   - store_ptr - elem type
 * Sema: fix memory leak in `zirArrayInit` and other cleanups to this
   function.
 * x86_64: implement the full x86_64 C ABI according to the spec
 * Type: implement `intInfo` for error sets.
 * Type: implement `intTagType` for tagged unions.

The Zig type tag `Fn` is now used exclusively for function bodies.
Function pointers are modeled as `*const T` where `T` is a `Fn` type.
 * The `call` AIR instruction now allows a function pointer operand as
   well as a function operand.
 * Sema now has a coercion from function body to function pointer.
 * Function type syntax, e.g. `fn()void`, now returns zig tag type of
   Pointer with child Fn, rather than Fn directly.
   - I think this should probably be reverted. Will discuss the lang
     specs before doing this. Idea being that function pointers would
     need to be specified as `*const fn()void` rather than `fn() void`.

LLVM backend:
 * Enable calling the panic handler (previously this just
   emitted `@breakpoint()` since the backend could not handle the panic
   function).
 * Implement sret
 * Introduce `isByRef` and implement it for structs and arrays. Types
   that are `isByRef` are now passed as pointers to functions, and e.g.
   `elem_val` will return a pointer instead of doing a load.
 * Move the function type creating code from `resolveLlvmFunction` to
   `llvmType` where it belongs; now there is only 1 instance of this
   logic instead of two.
 * Add the `nonnull` attribute to non-optional pointer parameters.
 * Fix `resolveGlobalDecl` not using fully-qualified names and not using
   the `decl_map`.
 * Implement `genTypedValue` for pointer-like optionals.
 * Fix memory leak when lowering `block` instruction and OOM occurs.
 * Implement volatile checks where relevant.
2021-10-11 11:39:12 -07:00
Andrew Kelley
78902db68b stage2: fix comptime @bitCast
Before, Sema for comptime `@bitCast` would return the same Value but
change the Type. This gave invalid results because, for example, an
integer Value when the Type is a float would be interpreted numerically,
but `@bitCast` needs it to reinterpret how they would be stored in
memory.

This requires a mechanism to serialize a Value to a byte buffer and
deserialize a Value from a byte buffer.

Not done yet, but needs to happen: comptime dereferencing a pointer
to a Decl needs to perform a comptime bitcast on the loaded value.
Currently the value is silently wrong in the same way that `@bitCast`
was silently wrong before this commit.

The logic in Value for handling readFromMemory for large integers is
only correct for small integers. It needs to be fleshed out for proper
big integers.

As part of this change:
 * std.math.big.Int: initial implementations of readTwosComplement and
   writeTwosComplement. They only support bit_count <= 128 so far and
   panic otherwise.
 * compiler-rt: move the compareXf2 exports over to the stage2 section.
   Even with the improvements in this commit, I'm still seeing test
   failures in the widening behavior tests; more investigation is
   needed.
2021-10-04 23:30:04 -07:00
Andrew Kelley
ac2333ee63 stage2: fix Type max/min int calculation
This was an attempt to move saturating_arithmetic.zig to the "passing
for stage2" section, which did not pan out due to the discovery of 2
prerequisite items that need to be done, but I did make a bug fix along
the way of the calculation of max/min integers.

This commit also simplifies the saturating arithmetic behavior tests to
depend on less of the zig language that is not related to saturating
arithmetic.
2021-10-04 12:23:49 -07:00
Andrew Kelley
c4df9bf56f AstGen: fix while and for with unreachable bodies
Companion commit to 61a53a5875.

This commit also moves over a bunch of behavior test cases to the
passing-for-stage2 section.
2021-10-02 20:15:03 -07:00
Andrew Kelley
1d1f6a0421 move some behavior tests to the "passing for stage2" section 2021-09-29 11:33:22 -07:00
Andrew Kelley
79bc5891c1 stage2: more arithmetic support
* AIR: add `mod` instruction for modulus division
   - Implement for LLVM backend
 * Sema: implement `@mod`, `@rem`, and `%`.
 * Sema: fix comptime switch evaluation
 * Sema: implement comptime shift left
 * Sema: fix the logic inside analyzeArithmetic to handle all the
   nuances between the different mathematical operations.
   - Implement comptime wrapping operations
2021-09-28 16:12:24 -07:00
Martin Wickham
1cc5d4e758
Stage 2: Support inst.func() syntax (#9827)
* Merge call zir instructions to make space for field_call
* Fix bug with comptime known anytype args
* Delete the param_type zir instruction
* Move some passing tests to stage 2
* Implement a.b() function calls
* Add field_call_bind support for call and field builtins
2021-09-28 12:00:35 -05:00
Martin Wickham
a0a847f2e4
Stage2: Implement comptime closures and the This builtin (#9823) 2021-09-23 13:17:06 -04:00