6517 Commits

Author SHA1 Message Date
Meghan Denny
e878a6633f stage2: implement runtime % and @rem 2021-08-30 00:31:40 -07:00
Meghan Denny
56845082bc stage2/sema: clarify todo arithmetic operator error 2021-08-30 00:30:14 -07:00
Andrew Kelley
861b784454 AstGen: fix incorrectly using decl_val/decl_ref
in identifiers rather than directly referencing the instructions.
2021-08-29 13:43:03 -07:00
Veikka Tuominen
be9b490f84 translate-c: remove now unnecessary mangling of primitive type shadowing
Closes #6382
2021-08-29 15:11:38 +03:00
Isaac Freund
7b863aa8ac AstGen: short-circuit rvalue() if code is unreachable
This avoids generating instructions which will never be reached.
2021-08-29 08:59:27 +00:00
Andrew Kelley
4bb5d17edc AstGen: pre-scan all decls in a namespace
Also:
 * improve the "ambiguous reference" error by swapping the order of
   "declared here" and "also declared here" notes.
 * improve the "not accessible from inner function" error:
   - point out that it has to do with the thing being mutable
   - eliminate the incorrect association with it being a function
   - note where it crosses a namespace boundary
 * struct field types are evaluated in a context that has the struct
   namespace visible. Likewise with align expressions, linksection
   expressions, enum tag values, and union/enum tag argument
   expressions.

Closes #9194
Closes #9622
2021-08-28 16:04:38 -07:00
Andrew Kelley
05cf44933d stage2: delete keywords true, false, undefined, null
The grammar does not need these as keywords; they are merely primitives
provided by the language the same as `void`, `u32`, etc.
2021-08-28 12:10:55 -07:00
Andrew Kelley
6a6e2cd64f AstGen: allow locals with same name as primitives with @"" syntax
This makes local names follow the same rule as declaration names.
2021-08-28 13:28:34 -04:00
Andrew Kelley
f30aa25cbf declarations may collide with primitives with @"" syntax
* stage2 AstGen: add missing compile error for declaring a local
   that shadows a primitive. Even with `@""` syntax, it may not have
   the same name as a primitive.
 * stage2 AstGen: add a compile error for a global declaration
   whose name matches a primitive. However it is allowed when using
   `@""` syntax.
 * stage1: delete all "declaration shadows primitive" compile errors
   because they are now handled by stage2 AstGen.
 * stage1/stage2 AstGen: notice when using `@""` syntax and:
   - treat `_` as a regular identifier
   - skip checking if an identifire is a primitive

Check the new test cases for clarifications on semantics.

closes #6062
2021-08-27 21:34:13 -07:00
Robin Voetter
cfae70ec8e Make slice always return a reference
Previously this returned an rvalue, which leads to unexpected behaviour
when writing expressions such as `x[1..][1..].`
2021-08-27 18:53:03 -04:00
Robin Voetter
3aa533519d Store to mutable pointer in analyzeRef
This function previously attempted to store a value to an immutable
pointer, after which storePtr would yield an error.
2021-08-27 18:53:03 -04:00
Robin Voetter
3a3704be05 Don't use .none_or_ref for for(expr)
We can already know whether the user want the expression to be a pointer or
ref based on whether the asterisk token is used, like with if and switch.
2021-08-27 18:53:03 -04:00
joachimschmidt557
04cafd8137 stage2 ARM: Add qadd, qsub, qdadd, qdsub instructions
These are integer saturating arithmetic instructions
2021-08-26 14:42:28 +02:00
Andrew Kelley
9c95f38a7c stage1: remove incorrect compile error for var redeclaration
Locals are not allowed to shadow declarations, but declarations are
allowed to shadow each other, as long as there are no ambiguous
references.

closes #678
2021-08-24 22:35:37 -07:00
Takeshi Yoneda
5c79370e9c
wasm: pass --export-dynamic to wasm-ld for WASI reactors. (#9605)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2021-08-24 14:03:50 +02:00
Frank Denis
9e3ec98937
Don't define valgrind_support on macOS (#9612)
Unfortunately, Valgrind for macOS has been broken for years,
and the Homebrew formula is only for Linux.
2021-08-23 20:06:02 -04:00
Andrew Kelley
bb38931c71 stage1: @intToEnum implicitly does an @intCast
This is a backwards-compatible language change.

Previously, `@intToEnum` coerced its integer operand to the integer tag
type of the destination enum type, often requiring the callsite to
additionally wrap the operand in an `@intCast`. Now, the `@intCast` is
implicit, and any integer operand can be passed to `@intToEnum`.

The same as before, it is illegal behavior to pass any integer which does
not have a corresponding enum tag.
2021-08-23 15:30:57 -07:00
Richard Eklycke
62e3d67605 Fix typo introduced in 50a29f7
Now the last argument will be checked as well
2021-08-22 13:29:06 -04:00
Jacob G-W
4ac37eb484 stage2 Air: add struct_field_ptr_index_{0..3}
Since these are very common, it will save memory.
2021-08-21 23:52:55 -04:00
Andrew Kelley
f378b0adce stage2: comptime function with the same args is memoized
* Introduce `memoized_calls` to `Module` which stores all the comptime
   function calls that are cached. It is keyed on the `*Fn` and the
   comptime arguments, but it does not yet properly detect comptime function
   pointers and avoid memoizing in this case. So it will have false
   positives for when a comptime function call mutates data through a
   pointer parameter.
 * Sema: Add a new helper function: `resolveConstMaybeUndefVal`
 * Value: add `enumToInt` method and use it in `zirEnumToInt`. It is
   also used by the hashing function.
 * Value: fix representation of optionals to match error unions.
   Previously it would not handle nested optionals correctly. Now it
   matches the memory layout of error unions and supports nested
   optionals properly. This required changes in all the backends for
   generating optional constants.
 * TypedValue gains `eql` and `hash` methods.
 * Value: Implement hashing for floats, optionals, and enums.
   Additionally, the zig type tag is added to the hash, where it was not
   previously, so that values of differing types will get different
   hashes.
2021-08-21 20:47:42 -07:00
Andrew Kelley
2b40815a22 stage2: fix wrong value for Decl owns_tv
In the case of a comptime function call of a function that returns a
type, resulting in a compiler crash on deinit().
2021-08-21 20:47:42 -07:00
Jonathan Marler
f28868e8fd mingw.zig: fix logic to add crt sources
The current version of code uses isARM to check if we are compiling to any arm target then checks the target bit width to either add the 32-bit sources or 64-bit source.  However, isARM only returns true for 32-bit targets, and isAARCH64 is for the 64-bit targets.

I also replaced the unreachable with a @panic when we receive an unsupported arch because this code is reachable and should turn into an error.
2021-08-21 20:40:07 +03:00
Veikka Tuominen
724995e892 translate-c: avoid repeating string in type when making it mutable 2021-08-21 16:32:09 +03:00
Evan Haas
3b25205833 translate-c: allow string literals to be used as char *
In C the type of string literals is `char *`, so when using them in
a non-const context we have to cast the const away.

Fixes #9126
2021-08-21 16:32:09 +03:00
Tamas Kenez
4c18da1ef3 Fix args when calling clang::ASTUnit::LoadFromCommandLine.
clang::ASTUnit::LoadFromCommandLine interprets the first argument as
the name of program (like the main function).
This change shifts the arguments passing "" for the first argument.
2021-08-21 14:49:01 +03:00
Andrew Kelley
f0176eec4a stage2: support comptime fn call returning type
...when the field type expressions reference locals as well as
comptime function parameters.
2021-08-20 17:44:03 -07:00
joachimschmidt557
5806c386eb stage2 codegen: re-allocate result register in finishAir
In some cases (such as bitcast), an operand may be the same MCValue as
the result. If that operand died and was a register, it was freed by
processDeath. We have to "re-allocate" the register.
2021-08-20 19:06:30 -04:00
joachimschmidt557
e48d7bbb99 stage2 ARM: Implement loading from memory 2021-08-20 19:05:50 -04:00
Andrew Kelley
a2438357e1
Merge pull request #9597 from joachimschmidt557/stage2-arm-bitshift
stage2 ARM: implement bitshifts
2021-08-20 19:05:05 -04:00
Andrew Kelley
0cd361219c stage2: field type expressions support referencing locals
The big change in this commit is making `semaDecl` resolve the fields if
the Decl ends up being a struct or union. It needs to do this while
the `Sema` is still in scope, because it will have the resolved AIR
instructions that the field type expressions possibly reference. We do
this after the decl is populated and set to `complete` so that a `Decl`
may reference itself.

Everything else is fixes and improvements to make the test suite pass
again after making this change.

 * New AIR instruction: `ptr_elem_ptr`
   - Implemented for LLVM backend
 * New Type tag: `type_info` which represents `std.builtin.TypeInfo`. It
   is used by AstGen for the operand type of `@Type`.
 * ZIR instruction `set_float_mode` uses `coerced_ty` to avoid
   superfluous `as` instruction on operand.
 * ZIR instruction `Type` uses `coerced_ty` to properly handle result
   location type of operand.

 * Fix two instances of `enum_nonexhaustive` Value Tag not handled
   properly - it should generally be handled the same as `enum_full`.
 * Fix struct and union field resolution not copying Type and Value
   objects into its Decl arena.
 * Fix enum tag value resolution discarding the ZIR=>AIR instruction map
   for the child Sema, when they still needed to be accessed.
 * Fix `zirResolveInferredAlloc` use-after-free in the AIR instructions
   data array.
 * Fix `elemPtrArray` not respecting const/mutable attribute of pointer
   in the result type.
 * Fix LLVM backend crashing when `updateDeclExports` is called before
   `updateDecl`/`updateFunc` (which is, according to the API, perfectly
   legal for the frontend to do).
 * Fix LLVM backend handling element pointer of pointer-to-array. It
   needed another index in the GEP otherwise LLVM saw the wrong type.
 * Fix LLVM test cases not returning 0 from main, causing test failures.
   Fixes a regression introduced in
   6a5094872f10acc629543cc7f10533b438d0283a.

 * Implement comptime shift-right.
 * Implement `@Type` for integers and `@TypeInfo` for integers.
 * Implement union initialization syntax.
 * Implement `zirFieldType` for unions.
 * Implement `elemPtrArray` for a runtime-known operand.

 * Make `zirLog2IntType` support RHS of shift being `comptime_int`. In
   this case it returns `comptime_int`.

The motivating test case for this commit was originally:

```zig
test "example" {
    var l: List(10) = undefined;
    l.array[1] = 1;
}

fn List(comptime L: usize) type {
    var T = u8;
    return struct {
        array: [L]T,
    };
}
```

However I changed it to:

```zig
test "example" {
    var l: List = undefined;
    l.array[1] = 1;
}

const List = blk: {
    const T = [10]u8;
    break :blk struct {
        array: T,
    };
};
```

Which ended up being a similar, smaller problem. The former test case
will require a similar solution in the implementation of comptime
function calls - checking if the result of the function call is a struct
or union, and using the child `Sema` before it is destroyed to resolve
the fields.
2021-08-20 15:41:57 -07:00
joachimschmidt557
f9e50a5830
stage2 ARM: implement bitshifting for 32-bit integers 2021-08-20 23:17:46 +02:00
joachimschmidt557
7aecf90d2e
stage2 ARM: add lsl, lsr, asr, ror psuedo-instructions 2021-08-20 14:25:25 +02:00
Andrew Kelley
6a5094872f
Merge pull request #9587 from g-w1/sh
stage2: implement shr and shl
2021-08-19 23:47:44 -04:00
Jacob G-W
2e22f7e5a5 stage2: implement shl
This is implemented in the llvm and cbe backends.
x86_64 will take a bit more time.
2021-08-19 16:18:42 -04:00
Jacob G-W
2e6ce11eb2 stage2: implement shr and boilerplate for shl
This implements it in the llvm and c backends.
x86_64 will have to be a little more work.
2021-08-19 16:18:40 -04:00
Jacob G-W
df10e998ee stage2 x86_64: enable bitwise and + or and add tests 2021-08-19 22:21:16 +03:00
Jacob G-W
d785dc49aa stage2: add error set type equality 2021-08-19 14:09:53 +03:00
Jacob G-W
2129cc5c54 stage2: fix typo
this made errors go from stuff like:
> type comptime_int cannot represent integer value 40
to
> type u5 cannot represent integer value 40
which makes much more sense
2021-08-19 13:48:52 +03:00
Robin Voetter
5cd1d42a35
Add mask before truncating dereferenced bit pointers (#9584) 2021-08-19 01:20:50 -04:00
Jakub Konka
e2303840de Fix AST and build errors 2021-08-15 23:50:39 +02:00
Jakub Konka
d5f7963331 macho: adhoc code sign binaries targeting aarch64-xxx-simulator 2021-08-15 18:29:19 +02:00
Jakub Konka
f82c26eb04 macho: don't embed codesig unless targeting aarch64-macos
When developing an iOS app for example, the developer is required
to use Apple's codesign utility to generate a valid signature
as done by Xcode.
2021-08-15 18:07:30 +02:00
Jakub Konka
1373df4c34
Merge pull request #9227 from mathetake/libc-wasi-test
WASI,libc: fix libstd with wasi-libc linkage, and enable tests.
2021-08-13 21:56:32 +02:00
Jakub Konka
c53423f8aa macho: don't cache unused link options
This will change incrementally once we support more linker options.
2021-08-13 10:46:53 +02:00
Andrew Kelley
55c6a784a5 stage2 frontend improvements - @ptrCast and optionals
* AstGen: use coerced_ty ResultLoc on array types and rely on Sema
   doing type coercion, to reduce the size of the ZIR for these
   instructions.

 * Sema: implement `@ptrCast`.

 * Sema: implement coercion from `T` to `?T` with an intermediate
   coercion rather than equality check.
2021-08-12 10:48:54 -07:00
Andrew Kelley
6eecc4af99 stage2 llvm backend: implement const inttoptr 2021-08-12 10:48:54 -07:00
Andrew Kelley
a0670e748e
Merge pull request #9166 from joachimschmidt557/stage2
stage2 Sema: Add error notes to unresolvable peer types
2021-08-12 13:22:27 -04:00
Jakub Konka
f49aa960a9 macho: use ArrayHashMap.popOrNull where applicable 2021-08-12 10:20:57 +02:00
Jakub Konka
f01366e8b3 macho: exclude tentative def before checking for collision
when resolving global symbols.
2021-08-12 10:20:57 +02:00
Jakub Konka
35403d41ce macho: use array hashmaps for quick lookups
as containers for unresolved and tentative definitions when resolving
symbols.
2021-08-12 10:20:57 +02:00