2504 Commits

Author SHA1 Message Date
Andrew Kelley
799fedf612 stage2: pass some error union tests
* Value: rename `error_union` to `eu_payload` and clarify the intended
   usage in the doc comments. The way error unions is represented with
   Value is fixed to not have ambiguous values.
 * Fix codegen for error union constants in all the backends.
 * Implement the AIR instructions having to do with error unions in the
   LLVM backend.
2021-08-07 20:34:28 -07:00
Andrew Kelley
f81b2531cb stage2: pass some pointer tests
* New AIR instructions: ptr_add, ptr_sub, ptr_elem_val, ptr_ptr_elem_val
   - See the doc comments for details.
 * Sema: implement runtime pointer arithmetic.
 * Sema: implement elem_val for many-pointers.
 * Sema: support coercion from `*[N:s]T` to `[*]T`.
 * Type: isIndexable handles many-pointers.
2021-08-07 15:46:53 -07:00
FnControlOption
d19e1709b3 Re-enable 128-bit cmpxchg test 2021-08-07 12:22:10 -07:00
Andrew Kelley
d94252496e
Merge pull request #9531 from squeek502/split-tokenize-generic
Make mem.split and mem.tokenize generic instead of assuming u8
2021-08-07 11:14:45 -07:00
Andrew Kelley
259f3458a1 Sema: implement alloc_inferred_comptime 2021-08-07 11:08:08 -07:00
Andrew Kelley
7d0de54ad4 stage2: fix return pointer result locations
* Introduce `ret_load` ZIR instruction which does return semantics
   based on a corresponding `ret_ptr` instruction. If the return type of
   the function has storage for the return type, it simply returns.
   However if the return type of the function is by-value, it loads the
   return value from the `ret_ptr` allocation and returns that.

 * AstGen: improve `finishThenElseBlock` to not emit break instructions
   after a return instruction in the same block.

 * Sema: `ret_ptr` instruction works correctly in comptime contexts.
   Same with `alloc_mut`.

The test case with a recursive inline function having an implicitly
comptime return value now has a runtime return value because of the fact
that it calls a function in a non-comptime context.
2021-08-06 19:53:04 -07:00
Andrew Kelley
ede76f4fe3 stage2: fix generics with non-comptime anytype parameters
The `comptime_args` field of Fn has a clarified purpose:
For generic function instantiations, there is a `TypedValue` here
for each parameter of the function:
 * Non-comptime parameters are marked with a `generic_poison` for the value.
 * Non-anytype parameters are marked with a `generic_poison` for the type.

Sema now has a `fn_ret_ty` field. Doc comments reproduced here:
> When semantic analysis needs to know the return type of the function whose body
> is being analyzed, this `Type` should be used instead of going through `func`.
> This will correctly handle the case of a comptime/inline function call of a
> generic function which uses a type expression for the return type.
> The type will be `void` in the case that `func` is `null`.
Various places in Sema are modified in accordance with this guidance.

Fixed `resolveMaybeUndefVal` not returning `error.GenericPoison` when
Value Tag of `generic_poison` is encountered.

Fixed generic function memoization incorrect equality checking. The
logic now clearly deals properly with any combination of anytype and
comptime parameters.

Fixed not removing generic function instantiation from the table in case
a compile errors in the rest of `call` semantic analysis. This required
introduction of yet another adapter which I have called
`GenericRemoveAdapter`. This one is nice and simple - it's the same hash
function (the same precomputed hash is passed in) but the equality
function checks pointers rather than doing any logic.

Inline/comptime function calls coerce each argument in accordance with
the function parameter type expressions. Likewise the return type
expression is evaluated and provided (see `fn_ret_ty` above).

There's a new compile error "unable to monomorphize function". It's
pretty unhelpful and will need to get improved in the future. It happens
when a type expression in a generic function did not end up getting
resolved at a callsite. This can happen, for example, if a runtime
parameter is attempted to be used where it needed to be comptime known:

```zig
fn foo(x: anytype) [x]u8 { _ = x; }
```

In this example, even if we pass a number such as `10` for `x`, it is
not marked `comptime`, so `x` will have a runtime known value, making
the return type unable to resolve.

In the LLVM backend I implement cmp instructions for float types to pass
some behavior tests that used floats.
2021-08-06 16:24:39 -07:00
Ryan Liptak
d31352ee85 Update all usages of mem.split/mem.tokenize for generic version 2021-08-06 02:01:47 -07:00
Andrew Kelley
ea7bdeb67d
Merge pull request #9517 from ziglang/generic-functions
stage2 generic functions
2021-08-05 23:32:42 -07:00
Andrew Kelley
7e9b23e6dc Sema: respect requiresComptime of function return types
When doing a function call, if the return type requires comptime, the
function is analyzed as an inline/comptime call.

There is an important TODO here. I will reproduce the comment from this
commit:

> In the case of a comptime/inline function call of a generic function,
> the function return type needs to be the resolved return type based on
> the function parameter type expressions being evaluated with comptime arguments
> passed in. Otherwise, it ends up being .generic_poison and failing the
> comptime/inline function call analysis.
2021-08-05 23:26:11 -07:00
Evan Haas
9fd3aeb808 translate-c: handle macros that cast to cv void
Fixes #9507
2021-08-06 09:10:50 +03:00
Andrew Kelley
c03a04a589 stage2: return type expressions of generic functions
* ZIR encoding for function instructions have a body for the return
   type. This lets Sema for generic functions do the same thing it does
   for parameters, handling `error.GenericPoison` in the evaluation of
   the return type by marking the function as generic.

 * Sema: fix missing block around the new Decl arena finalization. This
   led to a memory corruption.

 * Added some floating point support to the LLVM backend but didn't get
   far enough to pass any new tests.
2021-08-05 19:19:19 -07:00
Andrew Kelley
e9e3a29946 stage2: implement generic function memoization
Module has a new field `monomorphed_funcs` which stores the set of
`*Module.Fn` objects which are generic function instantiations.
The hash is based on hashes of comptime values of parameters known to be
comptime based on an explicit comptime keyword or must-be-comptime
type expressions that can be evaluated without performing monomorphization.
This allows function calls to be semantically analyzed cheaply for
generic functions which are already instantiated.

The table is updated with a single `getOrPutAdapted` in the semantic
analysis of `call` instructions, by pre-allocating the `Fn` object and
passing it to the child `Sema`.
2021-08-05 16:37:21 -07:00
Andrew Kelley
f58cbef165 stage2: std.mem.eql works now
* The `indexable_ptr_len` ZIR instruction now uses a `none_or_ref`
   ResultLoc. This prevents an unnecessary `ref` instruction from being
   emitted.
 * Sema: Fix `analyzeCall` using the incorrect ZIR object for the
   generic function callee.
 * LLVM backend: `genTypedValue` supports a `Slice` type encoded with
   the `decl_ref` `Value`.
2021-08-04 23:02:13 -07:00
Andrew Kelley
b465037a65 move some behavior tests to the "passing for stage2" section 2021-08-01 23:27:38 -07:00
Andrew Kelley
ddf14323ea stage2: implement @truncate 2021-08-01 16:13:58 -07:00
Andrew Kelley
6ae0825e7f Sema: implement comptime variables
Sema now properly handles alloc_inferred and alloc_inferred_mut ZIR
instructions inside a comptime execution context. In this case it
creates Decl objects and points to them with the new `decl_ref_mut`
Value Tag. `storePtr` is updated to mutate such Decl types and values.
In this case it destroys the old arena and makes a new one, preventing
memory growth during comptime code execution.

Additionally:

 * Fix `storePtr` to emit a compile error for a pointer comptime-known
   to be undefined.
 * Fix `storePtr` to emit runtime instructions for all the cases that a
   pointer is comptime-known but does not support comptime
   dereferencing, such as `@intToPtr` on a hard-coded address, or an
   extern function.
 * Fix `ret_coerce` not coercing inside inline function call context.
2021-08-01 12:36:04 -07:00
Michael Dusan
ed174b7386 stage1: fix anon struct naming in certain cases
- generic "struct:L:C" naming if rloc is NodeTypeStructValueField
- generic "struct:L:C" naming if rloc is NodeTypeFnCallExpr
- move some tests from test/behavior/misc to test/behavior/typename

closes #4330
closes #9339
2021-07-28 18:17:11 -04:00
Robin Voetter
cdeea3b094 minimum/maximum builtins 2021-07-26 20:41:00 -04:00
Robin Voetter
50a29f7c21 Add @select
@select(
    comptime T: type,
    pred: std.meta.Vector(len, bool),
    a: std.meta.Vector(len, T),
    b: std.meta.Vector(len, T)
) std.meta.Vector(len, T)

Constructs a vector from a & b, based on the values in the predicate vector. For indices where the predicate value is true, the corresponding
element from the a vector is selected, and otherwise from b.
2021-07-26 20:05:48 -04:00
Auguste Rame
c619b85f67 ctz + clz 2021-07-25 22:43:52 -04:00
Auguste Rame
ecca829bcb Add vector support for @popCount 2021-07-25 20:35:55 -04:00
Daniele Cocca
8cde0c91c6 Fix "unused local constant" error
Amends b009aca38a861f74fd5378db19c65db286ad397e.
The PR predated the introduction of unused variable/constant checks,
thus the build checks weren't reporting this failure until later when
merged into master.
2021-07-15 20:56:24 -04:00
Daniele Cocca
b009aca38a src: return a null-terminated slice 2021-07-15 15:12:44 -04:00
leesongun
132b18e2b3
Fix bigint_shl (#9305) 2021-07-13 10:16:57 +03:00
Jacob G-W
ec36ac8b21 stage2 astgen: provide 3 more errors for invalid inline assembly 2021-07-08 14:33:01 -04:00
leesongun
cff7ecee07
Fix unexpected truncation behavior with comptime_int larger than u64 range (#9303)
Closes #9299
2021-07-06 12:42:18 +03:00
g-w1
ef56e42a2a
stage2: fix unreachable in union(enum) with anytype payload 2021-06-27 12:26:29 +03:00
Andrew Kelley
fc185a6f71 stage1: @shuffle type and mask params in comptime scope 2021-06-23 09:52:09 -07:00
Andrew Kelley
06412e04f9 cleanups related to unused params 2021-06-21 17:03:04 -07:00
Jacob G-W
9fffffb07b fix code broken from previous commit 2021-06-21 17:03:03 -07:00
Jacob G-W
641ecc260f std, src, doc, test: remove unused variables 2021-06-21 17:03:03 -07:00
Daniele Cocca
9e88356282 embedFile: change notation from [X:0] to [N:0]
This is for consistency with the documentation on sentinel-terminated
{arrays,slices,pointers} which already use `N` for a comptime-inferred
size rather than `X`.

Also adds a behavioral test to assert that a string literal is returned.
2021-06-16 22:03:02 +01:00
Daniele Cocca
9be2f76741 typeName: amend return type to string literal
This was already the case, but the documentation failed to point out
that the returned value is of type `*const [N:0]u8`, i.e. that of a
string literal.

Also adds a behavioral test to assert that this is the case.
2021-06-16 22:03:02 +01:00
Daniele Cocca
171102ea7c errorName: return a null-terminated slice 2021-06-16 22:02:52 +01:00
Daniele Cocca
00e1c0082c tagName: return a null-terminated slice 2021-06-16 22:02:42 +01:00
Veikka Tuominen
e63ff4f1c1 add ast-check flag to zig fmt, fix found bugs 2021-06-14 00:16:40 +03:00
Veikka Tuominen
029fe6c9ab meta.cast: handle casts from negative ints to ptrs 2021-06-13 16:53:01 +03:00
Exonorid
f63338195d Renamed @byteOffsetOf to @offsetOf 2021-06-12 19:16:01 +03:00
Andrew Kelley
138afd5cbf zig fmt 2021-06-10 20:13:43 -07:00
daurnimator
916b645fc1 Have std.fmt functions take case as an enum 2021-06-10 08:33:42 +03:00
Jakub Konka
e8e305b67c Re-enable multiple wasm32 vector tests
Fixes #5339
2021-06-09 23:54:23 -04:00
Matthew Borkowski
9ac6d28614 stage1: make @truncate to an integer type of different sign an error at comptime too 2021-06-08 20:58:30 +03:00
LemonBoy
fc8791c133 stage1: Allow array-like initialization for tuple types
This small change makes working with tuple types much easier, allowing
the use of anonymous (eg. obtained with meta.ArgsTuple) tuples in more
places without the need for specifying each (quoted!) field name in the
initializer.
2021-06-06 21:21:42 -04:00
Michael Dusan
0bda8435e0 test: re-enable dragonfly tests
Tests with LLVM assertions enabled are no longer failing.

closes #3563
2021-06-05 22:08:36 -04:00
Martin Wickham
fc9430f567 Breaking hash map changes for 0.8.0
- hash/eql functions moved into a Context object
- *Context functions pass an explicit context
- *Adapted functions pass specialized keys and contexts
- new getPtr() function returns a pointer to value
- remove functions renamed to fetchRemove
- new remove functions return bool
- removeAssertDiscard deleted, use assert(remove(...)) instead
- Keys and values are stored in separate arrays
- Entry is now {*K, *V}, the new KV is {K, V}
- BufSet/BufMap functions renamed to match other set/map types
- fixed iterating-while-modifying bug in src/link/C.zig
2021-06-03 17:02:16 -05:00
Andrew Kelley
c12704a339
Merge pull request #8918 from ziglang/stage1-tokenizer
stage1: rework tokenizer to match stage2
2021-05-28 21:54:01 -04:00
LemonBoy
d3fb9f9532 stage1: Fix for atomicrmw xchg on fp types
Bitcast the pointer and operands to integer types having the same size,
working around LLVM inability to lower a LL/SC operation when the
operands have floating-point types (and are reasonably sized).

Closes #4457
2021-05-28 19:18:42 -04:00
Andrew Kelley
2a990d6966 stage1: rework tokenizer to match stage2
* Extracts AstGen logic from ir.cpp into astgen.cpp. Reduces the
   largest file of stage1 from 33,551 lines to 25,510.
 * tokenizer: rework it completely to match the stage2 tokenizer logic.
   They can now be maintained together; when one is changed, the other
   can be changed in the same way.
   - Each token now takes up 13 bytes instead of 64 bytes. The tokenizer
     does not parse char literals, string literals, integer literals,
     etc into meaningful data. Instead, that happens during parsing or
     astgen.
   - no longer store line offsets. Error messages scan source
     files to find the line/column as needed (same as stage2).
   - main loop: instead of checking the loop, handle a null byte
     explicitly in the switch statements. This is a nice improvement
     that we may want to backport to stage2.
   - delete some dead tokens, artifacts of past syntax that no longer
     exists.
 * Parser: fix a TODO by parsing builtin functions as tokens rather than
   `@` as a separate token. This is how stage2 does it.
 * Remove some debugging infrastructure. These will need to be redone,
   if at all, as the code migrates to match stage2.
   - remove the ast_render code.
   - remove the IR debugging stuff
   - remove teh token printing code
2021-05-28 12:58:40 -07:00
Andrew Kelley
910f67f77b behavior tests: re-enable commented out test
Not sure why it was failing, also not sure why it started passing again
2021-05-17 16:09:20 -07:00