14899 Commits

Author SHA1 Message Date
Andrew Kelley
259f3458a1 Sema: implement alloc_inferred_comptime 2021-08-07 11:08:08 -07:00
Klecko
5789036b86 linux: add missing FUTEX definitions 2021-08-07 15:35:27 +03: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
e974d4c429 stage2: get rid of "unable to monomorphize function" error
This commit solves the problem in a much simpler way: putting
runtime-known values in place of non-comptime arguments when
instantiating a generic function.
2021-08-06 17:26:37 -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
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
Andrew Kelley
c7dc451a2a stage2: more debuggable panics
For now these errors are handled via `@panic` rather than `unreachable`.
These are relatively likely bugs to occur at this early stage of
development, and handling them as panics lets us ship release builds
of the compiler without worrying about undefined behavior.

Furthermore, in stage1, `@panic` is implemented to include an error
return trace, while `unreachable` is not. In this case, the error return
traces are extremely helpful in debugging the compiler.
2021-08-05 23:20:53 -07:00
Andrew Kelley
786e238a7f AstGen: fix function declarations
They were putting their return type expressions into the wrong ZIR
block, resulting in a compiler crash.
2021-08-05 23:20:10 -07:00
Andrew Kelley
47f2463b5c std.HashMap: fix getPtrAdapted. AstGen: fix fn param iteration
There was a bug in stage2 regarding iteration of function parameter AST.
This resulted in a false negative "unused parameter" compile error,
which, when fixed, revealed a bug in the std lib HashMap implementation.
2021-08-05 23:17:29 -07:00
Evan Haas
9fd3aeb808 translate-c: handle macros that cast to cv void
Fixes #9507
2021-08-06 09:10:50 +03:00
Chris Gregory
fdd97244fd Make DynamicBitSet.iterator take self as const 2021-08-06 09:09:02 +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
Jakub Konka
011a468381 Link system libc if natively linking frameworks on macOS 2021-08-05 17:07:13 -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
d4468affb7 stage2 generics improvements: anytype and param type exprs
AstGen result locations now have a `coerced_ty` tag which is the same as
`ty` except it assumes that Sema will do a coercion, so it does not
redundantly add an `as` instruction into the ZIR code. This results in
cleaner ZIR and about a 14% reduction of ZIR bytes.

param and param_comptime ZIR instructions now have a block body for
their type expressions. This allows Sema to skip evaluation of the
block in the case that the parameter is comptime-provided. It also
allows a new mechanism to function: when evaluating type expressions of
generic functions, if it would depend on another parameter, it returns
`error.GenericPoison` which bubbles up and then is caught by the
param/param_comptime instruction and then handled.

This allows parameters to be evaluated independently so that the type
info for functions which have comptime or anytype parameters will still
have types populated for parameters that do not depend on values of
previous parameters (because evaluation of their param blocks will return
successfully instead of `error.GenericPoison`).

It also makes iteration over the block that contains function parameters
slightly more efficient since it now only contains the param
instructions.

Finally, it fixes the case where a generic function type expression contains
a function prototype. Formerly, this situation would cause shared state
to clobber each other; now it is in a proper tree structure so that
can't happen. This fix also required adding a field to Sema
`comptime_args_fn_inst` to make sure that the `comptime_args` field
passed into Sema is applied to the correct `func` instruction.

Source location for `node_offset_asm_ret_ty` is fixed; it was pointing at
the asm output name rather than the return type as intended.

Generic function instantiation is fixed, notably with respect to
parameter type expressions that depend on previous parameters, and with
respect to types which must be always comptime-known. This involves
passing all the comptime arguments at a callsite of a generic function,
and allowing the generic function semantic analysis to coerce the values
to the proper types (since it has access to the evaluated parameter type
expressions) and then decide based on the type whether the parameter is
runtime known or not. In the case of explicitly marked `comptime`
parameters, there is a check at the semantic analysis of the `call`
instruction.

Semantic analysis of `call` instructions does type coercion on the
arguments, which is needed both for generic functions and to make up for
using `coerced_ty` result locations (mentioned above).

Tasks left in this branch:
 * Implement the memoization table.
 * Add test coverage.
 * Improve error reporting and source locations for compile errors.
2021-08-04 21:11:31 -07:00
Jakub Konka
bd8baefaaa Update x86_64-macos headers 2021-08-05 00:56:32 +02:00
Jakub Konka
ee6f7fee29 libstd: add ArrayHashMap.popOrNull function
which internally calls `ArrayHashMap.pop`, however, returns `?KV`
instead and performs the bounds checking automatically.

This function correponds to `ArrayList.popOrNull` and is meant
to fill the gap for situations where we want the quick lookup offered
by the hash map with elegant ability to iterate and pop of the
container with automatic bound checking that plugs in well with
a `while`-loop such as

```zig
var map = std.ArrayHashMap(K, V).init(allocator);
map.deinit();
while (map.popOrNull()) |entry| {
  // ... do something
}
assert(map.count() == 0);
```
2021-08-04 09:47:42 +02:00
Andrew Kelley
382d201781 stage2: basic generic functions are working
The general strategy is that Sema will pre-map comptime arguments into
the inst_map, and then re-run the block body that contains the `param`
and `func` instructions. This re-runs all the parameter type expressions
except with comptime values populated.

In Sema, param instructions are now handled specially: they detect
whether they are comptime-elided or not. If so, they skip putting a
value in the inst_map, since it is already pre-populated. If not, then
they append to the `fields` field of `Sema` for use with the `func`
instruction.

So when the block body is re-run, a new function is generated with
all the comptime arguments elided, and the new function type has only
runtime parameters in it. TODO: give the generated Decls better names
than "foo__anon_x".

The new function is then added to the work queue to have its body
analyzed and a runtime call AIR instruction to the new function is
emitted.

When the new function gets semantically analyzed, comptime parameters are
pre-mapped to the corresponding `comptime_args` values rather than
mapped to an `arg` AIR instruction. `comptime_args` is a new field that
`Fn` has which is a `TypedValue` for each parameter. This field is non-null
for generic function instantiations only. The values are the comptime
arguments. For non-comptime parameters, a sentinel value is used. This is
because we need to know the information of which parameters are
comptime-known.

Additionally:
 * AstGen: align and section expressions are evaluated in the scope that
   has comptime parameters in it.

There are still some TODO items left; see the BRANCH_TODO file.
2021-08-03 22:34:22 -07:00
Andrew Kelley
609b84611d stage2: rework runtime, comptime, inline function calls
* ZIR function instructions encode the index of the block that
   contains the function instruction. This allows Zig to later scan the
   block and find the parameter instructions, which is needed for
   semantically analyzing function bodies.

 * Runtime function calls insert AIR arg instructions and then inserts
   Sema inst_map entries mapping the ZIR param instructions to them.

 * comptime/inline function call inserts Sema inst_map entries mapping
   the ZIR param instructions to the AIR callsite arguments.

With this commit we are back to the tests passing.
2021-08-03 17:29:59 -07:00
Carlos Zúñiga
aad4598367 ci: remove extra zig directory in windows builds
From $prefix/lib/zig/std/std.zig to $prefix/lib/std/std.zig
2021-08-03 08:49:18 +03:00
Evan Haas
bc18e93825 translate-c: better codegen for pointer index by int literal
#8589 introduced correct handling of signed (possibly negative) array access
of pointers. Since unadorned integer literals in C are signed, this resulted
in inefficient generated code when indexing a pointer by a non-negative
integer literal.
2021-08-03 08:38:15 +03:00
Andrew Kelley
1472dc3ddb stage2: update ZIR for generic functions
ZIR encoding for functions is changed in preparation for generic
function support. As an example:

```zig
const std = @import("std");
const expect = std.testing.expect;

test "example" {
    var x: usize = 0;
    x += checkSize(i32, 1);
    x += checkSize(bool, true);
    try expect(x == 5);
}

fn checkSize(comptime T: type, x: T) usize {
    _ = x;
    return @sizeOf(T);
}
```

Previous ZIR for the `checkSize` function:

```zir
  [165] checkSize line(10) hash(0226f62e189fd0b1c5fca02cf4617562): %55 = block_inline({
    %56 = decl_val("T") token_offset:11:35
    %57 = as_node(@Ref.type_type, %56) node_offset:11:35
    %69 = extended(func([comptime @Ref.type_type, %57], @Ref.usize_type, {
      %58 = arg("T") token_offset:11:23
      %59 = as_node(@Ref.type_type, %58) node_offset:11:35
      %60 = arg("x") token_offset:11:32
      %61 = dbg_stmt(11, 4)
```

ZIR for the `checkSize` function after this commit:

```zir
  [157] checkSize line(10) hash(0226f62e189fd0b1c5fca02cf4617562): %55 = block_inline({
    %56 = param_comptime("T", @Ref.type_type) token_offset:11:23
    %57 = as_node(@Ref.type_type, %56) node_offset:11:35
    %58 = param("x", %57) token_offset:11:32
    %67 = func(@Ref.usize_type, {
      %59 = dbg_stmt(11, 4)
```

Noted differences:
 * Previously the type expression was redundantly repeated.
 * Previously the parameter names were redundantly stored in the ZIR
   extra array.
 * Instead of `arg` ZIR instructions as the first instructions within a
   function body, they are now outside the function body, in the same
   block as the `func` instruction. There are variants:
   - param
   - param_comptime
   - param_anytype
   - param_anytype_comptime
 * The param instructions additionally encode the type.
 * Because of the param instructions, the `func` instruction no longer
   encodes the list of parameter types or the comptime bits.

It's implied that Sema will collect the parameters so that when a `func`
instruction is encountered, they will be implicitly used to construct
the function's type. This is so that we can satisfy all 3 ways of
performing semantic analysis on a function:

 1. runtime: Sema will insert AIR arg instructions for each parameter,
    and insert into the Sema inst_map ZIR param => AIR arg.

 2. comptime/inline: Sema will insert into the inst_map ZIR param =>
    callsite arguments.

 3. generic: Sema will map *only the comptime* ZIR param instructions to
    the AIR instructions for the comptime arguments at the callsite, and
    then re-run Sema for the function's Decl. This will produce a new
    function which is the monomorphized function.

Additionally:

 * AstGen: Update usage of deprecated `ensureCapacity` to
   `ensureUnusedCapacity` or `ensureTotalCapacity`.
 * Introduce `Type.fnInfo` for getting a bunch of data about a function
   type at once, and use it in `analyzeCall`.

This commit starts a branch to implement generic functions in stage2.
Test regressions have not been addressed yet.
2021-08-02 21:56:10 -07:00
Jakub Konka
41d7787b69 macho: remove obsolete pack/unpack dylib ordinal fns
Remove some unused debugging machinery such as full printing of the
symtab after symbol resolution. It was there only for the time of
rewriting the linker.
2021-08-02 19:49:32 +02:00
Jakub Konka
bf25650974 macho: refactor management of section ordinals
Instead of storing a two-way relation (seg,sect) <=> ordinal
we get the latter with `getIndex((seg, sect))`.
2021-08-02 19:49:32 +02:00
Jakub Konka
f3b328ee8c macho: refactor tracking of referenced dylibs
Now, index in the global referenced array hashmap is equivalent to
the dylib's ordinal in the final linked image.
2021-08-02 19:49:32 +02:00
Jakub Konka
159cd528b1 Add -Denable-macos-sdk explicit flag to build.zig
This way, we can explicitly signal if a test requires the presence
of macOS SDK to build. For instance, when testing our in-house
MachO linker for correctly linking Objective-C, we require the
presence of the SDK on the host system, and we can enforce this
with `-Denable-macos-sdk` flag to `zig build test-standalone`.
2021-08-02 13:41:58 -04:00
Jakub Konka
68e26a2cee std: check for overflow in writeCurrentStackTrace
On arm64 macOS, the address of the last frame is 0x0 rather than
a positive value like 0x1 on x86_64 macOS, therefore, we overflow
an integer trying to subtract 1 when printing the stack trace. This
patch fixes it by first checking for this condition before trying
to subtract 1.

Note that we do not need to signal the `SignalIterator` about this
as it will correctly detect this condition on the subsequent iteration
and return `null`, thus terminating the loop.
2021-08-02 13:40:53 -04:00
N00byEdge
871f6343f4 Move iovec and log levels to bits/posix.zig
This lets only the OSes that uses them to import them, and removes
dependencies on bits.zig for the os/<os>/<arch>.zig files
2021-08-02 11:05:05 +00:00
N00byEdge
934df5bd44 Make linux syscalls accessible with non-Linux target OS 2021-08-02 11:05:05 +00:00
Andrew Kelley
b465037a65 move some behavior tests to the "passing for stage2" section 2021-08-01 23:27:38 -07:00
Jakub Konka
eba153f88f
Merge pull request #9501 from ziglang/macho-objc-cleanup
Add standalone Objective-C enabled on macOS only
2021-08-02 08:01:15 +02:00
Andrew Kelley
dae4c18aa7 stage2: ZIR encodes comptime parameters
`func_extended` ZIR instructions now have a one of the unused flags used
as a `has_comptime_bits` boolean. When set, it means 1 or more
parameters are `comptime`. In this case, there is a u32 per every 32
parameters (usually just 1 u32) with each bit indicating whether the
corresponding parameter is `comptime`.

Sema uses this information to correctly mark generic functions as
generic. There is now a TODO compile error in place in case a generic
function call happens. A future commit will do the generic function call
implementation.
2021-08-01 22:04:18 -07:00
Andrew Kelley
d5f173d28f
Merge pull request #9496 from Luukdegram/stage2-wasm
stage2: wasm - Wrapping, intcast and optionals
2021-08-01 20:33:55 -04:00
Andrew Kelley
ddf14323ea stage2: implement @truncate 2021-08-01 16:13:58 -07:00
Jakub Konka
0ce54a1416 add standalone Objective-C enabled on macOS only 2021-08-01 22:48:39 +02: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
Luuk de Gram
6e139d124b
wasm: Resolve feedback (wrapping arbitrary int sizes)
- This ensures we honor the user's integer size when performing wrapping operations.
- Also, instead of using ensureCapacity, we now use ensureUnusedCapacity.
2021-08-01 21:30:06 +02:00
Jakub Konka
d794f4cd2a macho: add runaway section id when sorting sections 2021-08-01 18:05:07 +02:00
Meghan
7e52a096db
langref- fix packed struct error code 2021-08-01 12:42:48 +03:00
Meghan
32069d2330
langref- fix use after block error code 2021-08-01 12:42:05 +03:00
Luuk de Gram
a861b7d160
wasm: Test cases for optionals 2021-08-01 11:07:23 +02:00
Luuk de Gram
61de59e121
wasm: Implement optionals
This uses the same approach as error unions,
meaning it's a `WValue` with its tag set to `multi_value`.
The initial index of the multi_value will contain the null-tag, used to check if the value
is null or not. The other values will be the payload.

To support the `.?` shorthand syntax, we save the result from checking the null-tag
into a new local, which can then be loaded later in the block to either hit `unreachable` or
set the actual payload value.

Currently, it seems `.?` and `orelse unreachable` results in different AIR structure.
TODO: Is this expected?
2021-08-01 11:07:23 +02:00
Luuk de Gram
e58976542b
wasm: Test cases for wrap+intcast instructions 2021-08-01 11:07:23 +02:00
Luuk de Gram
5667ab7dcd
wasm: Implement wrapping operands, add opcodes to wasm.zig
- Some opcodes have the incorrect value set in std.
- Some opcodes were missing and have now been added to std.
- Adding wrapping operands for add,sub and mul.
- Implement intCast which either extends or shortens the type.
2021-08-01 11:07:23 +02:00
Žiga Željko
5589edf45c fix help for ast-check command 2021-08-01 03:57:34 -04:00
joachimschmidt557
0d09c6aed8 stage2 ARM: fix stack alignment
Acording to the AAPCS32, the stack alignment at public interfaces
should be 8, not 4.
2021-08-01 03:57:08 -04:00
Jakub Konka
0ce56f9305 macho: fix Trie and CodeSignature unit tests
after the cleanup.
2021-08-01 09:06:56 +02:00
Jakub Konka
58bc713c17 macho: make Trie accept allocator as a param
instead of storing it as a member of Trie struct.
2021-08-01 09:06:56 +02:00