2909 Commits

Author SHA1 Message Date
Andrew Kelley
66e5e92a3e
Merge pull request #7592 from LemonBoy/fix-7188
Allow variable captures on multi-prong switch arms
2021-01-04 14:23:01 -08:00
Evan Haas
d95724454c Allow dollar sign $ in identifiers in translate-c
In strictly conforming C, identifiers cannot container dollar signs.
However GCC and Clang allow them by default, so translate-c should
handle them. See http://gcc.gnu.org/onlinedocs/cpp/Tokenization.html
I encountered this in the wild in windows.h

Fixes #7585
2021-01-04 14:14:04 -08:00
g-w1
638f93ebdc stage2: implementation of @setEvalBranchQuota:
`@setEvalBranchQuota` can be called before the comptime/inline call
stack is created.

For example:

```zig
@setEvalBranchQuota(100);
comptime {
    while (true) {}
}
```

Here we need to set the branch_quota before the comptime block creates a
scope for the branch_count.
2021-01-04 12:42:52 -07:00
Andrew Kelley
53a0b7997d
Merge pull request #7681 from kubkon/stage2-aarch64-fn-args
stage2: basic fn args for aarch64
2021-01-03 19:51:38 -08:00
Andrew Kelley
f6644255f5
Merge pull request #7598 from FireFox317/more-llvm-stage2
stage2: More improvements to self-hosted LLVM backend
2021-01-03 16:09:14 -08:00
Evan Haas
5cc131030c Static function declarations with no prototype should not be variadic
If a static function is defined with no argument list and no prototype
is given, it should be treated as a function that takes no arguments
rather than as a variadic function.

Fixes #7594
2021-01-03 15:08:32 -08:00
Jakub Konka
807dc56fd6 stage2: add aarch64 stage2 tests
Fix missing string format specifier in Mach-O used to generate
path to debug symbols bundle.
2021-01-03 23:20:09 +01:00
Timon Kruiper
0151f3b76a stage2: Add support for testing LLVM enabled builds in test-stage2
To make sure that we don't have to rebuild libc for every case, we now
have a seperate cache directory for the global cache, which remains
the same between test runs.

Also make sure to destory the Compilation before executing a child process,
otherwise the compiler deadlocks. (#7596)
2021-01-03 21:13:38 +01:00
Andrew Kelley
654832253a stage2: support recursive inline/comptime functions
zir.Inst no longer has an `analyzed_inst` field. This is previously how
we mapped ZIR to their TZIR counterparts, however with the way inline
and comptime function calls work, we can potentially have the same ZIR
structure being analyzed by multiple different analyses, such as during
a recursive inline function call. This would cause the `analyzed_inst`
field to become clobbered. So instead, we use a table to map the
instructions to their semantically analyzed counterparts. This will help
with multi-threaded compilation as well.

Scope.Block.Inlining is split into 2 different layers of "sharedness".
The first layer is shared by the whole inline/comptime function call
stack. It contains the callsite where something is being inlined and the
branch count/quota. The second layer is different per function call but
shared by all the blocks within the function being inlined.

Add support for debug dumping br and brvoid TZIR instructions.

Remove the "unreachable code" error. It was happening even for this case:

```zig
if (comptime_condition) return;
bar(); // error: unreachable code
```

We will need smarter logic for when it is legal to emit this compile
error.

Remove the ZIR test cases. These are redundant with other higher level
Zig source tests we have, and maintaining support for ZIRModule as a
first-class top level abstraction is getting in the way of clean
compiler design for the main use case. We will have ZIR/TZIR based test
cases someday to help with testing optimization passes and ZIR to TZIR
analysis, but as is, these test cases are not accomplishing that, and
they are getting in the way.
2021-01-02 22:42:07 -07:00
Andrew Kelley
50a530196c stage2: fix handling compile error in inline fn call
* scopes properly inherit inlining information
 * compile errors of inline function calls are properly attached to the
   caller rather than the callee.
   - added a test case for this
 * --watch still opens a repl if compile errors happen.
2021-01-02 19:11:56 -07:00
Andrew Kelley
006e7f6805 stage2: re-use ZIR for comptime and inline calls
Instead of freeing ZIR after semantic analysis, we keep it around so
that it can be used for comptime calls, inline calls, and generic
function calls. ZIR memory is now managed by the Decl arena.

Debug dump() functions are conditionally compiled; only available in
Debug builds of the compiler.

Add a test for an inline function call.
2021-01-02 19:11:55 -07:00
Andrew Kelley
9362f382ab stage2: implement function call inlining in the frontend
* remove the -Ddump-zir thing. that's handled through --verbose-ir
 * rework Fn to have an is_inline flag without requiring any more memory
   on the heap per function.
 * implement a rough first version of dumping typed zir (tzir) which is
   a lot more helpful for debugging than what we had before. We don't
   have a way to parse it though.
 * keep track of whether the inline-ness of a function changes because
   if it does we have to go update callsites.
 * add compile error for inline and export used together.

inline function calls and comptime function calls are implemented the
same way. A block instruction is set up to capture the result, and then
a scope is set up that has a flag for is_comptime and some state if the
scope is being inlined.

when analyzing `ret` instructions, zig looks for inlining state in the
scope, and if found, treats `ret` as a `break` instruction instead, with
the target block being the one set up at the inline callsite.

Follow-up items:
 * Complete out the debug TZIR dumping code.
 * Don't redundantly generate ZIR for each inline/comptime function
   call. Instead we should add a new state enum tag to Fn.
 * comptime and inlining branch quotas.
 * Add more test cases.
2021-01-02 19:11:19 -07:00
Andrew Kelley
fea8659b82 stage2: comptime function calls
* Function calls that happen in a comptime scope get called at
   compile-time. We do this by putting the parameters in place as
   constant values and then running regular function analysis on the
   body.
 * Added `Scope.Block.dump()` for debugging purposes.
 * Fixed some code to call `identifierTokenString` rather than
   `tokenSlice`, making it work for `@""` syntax.
 * Implemented `Value.copy` for big integers.

Follow-up issues to tackle:
 * Adding compile errors to the callsite instead of the callee Decl.
 * Proper error notes for "called from here".
   - Related: #7555
 * Branch quotas.
 * ZIR support?
2021-01-02 19:10:11 -07:00
LemonBoy
d2f6fa1608 Fix more stray uses of {} for formatting strings 2021-01-02 17:12:57 -07:00
LemonBoy
4420afe64d tests: Use {s} instead of {} when formatting strings 2021-01-02 17:12:57 -07:00
LemonBoy
dd973fb365 std: Use {s} instead of {} when printing strings 2021-01-02 17:12:57 -07:00
joachimschmidt557
a2ab2fb9b0
stage2 ARM: Add simple tests for conditional branching 2021-01-01 12:22:17 +01:00
Andrew Kelley
982acc22fd stage2: compile error for invalid var type 2020-12-31 17:25:42 -07:00
Andrew Kelley
7deb1f4f6c stage2: type inference for local var 2020-12-31 02:42:48 -07:00
Andrew Kelley
a46d24af1c stage2: inferred local variables
This patch introduces the following new things:

Types:
 - inferred_alloc
   - This is a special value that tracks a set of types that have been stored
     to an inferred allocation. It does not support most of the normal type queries.
     However it does respond to `isConstPtr`, `ptrSize`, `zigTypeTag`, etc.
   - The payload for this type simply points to the corresponding Value
     payload.

Values:
 - inferred_alloc
   - This is a special value that tracks a set of types that have been stored
     to an inferred allocation. It does not support any of the normal value queries.

ZIR instructions:
 - store_to_inferred_ptr,
   - Same as `store` but the type of the value being stored will be used to infer
     the pointer type.
 - resolve_inferred_alloc
   - Each `store_to_inferred_ptr` puts the type of the stored value into a set,
     and then `resolve_inferred_alloc` triggers peer type resolution on the set.
     The operand is a `alloc_inferred` or `alloc_inferred_mut` instruction, which
     is the allocation that needs to have its type inferred.

Changes to the C backend:
 * Implements the bitcast instruction. If the source and dest types
   are both pointers, uses a cast, otherwise uses memcpy.
 * Tests are run with -Wno-declaration-after-statement. Someday we can
   conform to this but not today.

In ZIR form it looks like this:

```zir
fn_body main { // unanalyzed
  %0 = dbg_stmt()
=>%1 = alloc_inferred()
  %2 = declval_in_module(Decl(add))
  %3 = deref(%2)
  %4 = param_type(%3, 0)
  %5 = const(TypedValue{ .ty = comptime_int, .val = 1})
  %6 = as(%4, %5)
  %7 = param_type(%3, 1)
  %8 = const(TypedValue{ .ty = comptime_int, .val = 2})
  %9 = as(%7, %8)
  %10 = call(%3, [%6, %9], modifier=auto)
=>%11 = store_to_inferred_ptr(%1, %10)
=>%12 = resolve_inferred_alloc(%1)
  %13 = dbg_stmt()
  %14 = ret_type()
  %15 = const(TypedValue{ .ty = comptime_int, .val = 3})
  %16 = sub(%10, %15)
  %17 = as(%14, %16)
  %18 = return(%17)
} // fn_body main
```

I have not played around with very many test cases yet. Some interesting
ones that I want to look at before merging:

```zig
var x = blk: {
  var y = foo();
  y.a = 1;
  break :blk y;
};
```

In the above test case, x and y are supposed to alias.

```zig
var x = if (bar()) blk: {
  var y = foo();
  y.a = 1;
  break :blk y;
} else blk: {
  var z = baz();
  z.b = 1;
  break :blk z;
};
```

In the above test case, x, y, and z are supposed to alias.

I also haven't tested with `var` instead of `const` yet.
2020-12-31 01:54:02 -07:00
LemonBoy
2561168adb std: Clean up some tests
No functional changes, remove some dead code.
2020-12-30 09:42:21 +01:00
LemonBoy
88634f0481 stage1: Allow variable capture for multi-prong switch arms
Handle the multi-prong case as we do with range cases.

Closes #7188
2020-12-30 09:42:20 +01:00
Andrew Kelley
d18b6785bb stage2: C backend improvements
* Module: improve doc comments
 * C backend: improve const-correctness
 * C backend: introduce renderTypeAndName
 * C backend: put `static` on functions when appropriate
 * C backend: fix not handling errors in genBinOp
 * C backend: handle more IR instructions
   - alloc, store, boolean comparisons, ret_ptr
 * C backend: call instruction properly stores its result
 * test harness: ensure execution tests have empty stderr
2020-12-29 17:56:30 -07:00
Andrew Kelley
813d3308cc stage2: update C backend test cases for new output 2020-12-28 20:32:13 -07:00
Andrew Kelley
7561fca435 stage2: improve test harness to support executing generated C code 2020-12-28 20:01:17 -07:00
Andrew Kelley
a54ccd8537 stage2: C backend: implement @breakpoint and clean up test harness 2020-12-28 18:43:01 -07:00
Andrew Kelley
f75d4cbe56 Revert "stage2: add compile log statement (#7191)"
The addition of `addDeclErr` introduced a memory leak at every call
site, and I also would like to push back on having more than 1
compilation error per `Decl`.

This reverts commit 1634d45f1d53c8d7bfefa56ab4d2fa4cc8218b6d.
2020-12-28 11:24:53 -07:00
LemonBoy
fffb0904f8 stage1: Prevent crash with some lazy pointer types
Make sure the child element is not undefined, let's catch this problem
early on.

Closes #7568
2020-12-28 15:27:31 +02:00
Veikka Tuominen
d96f5b38a3
Merge pull request #7556 from Vexu/translate-c
some fixes
2020-12-28 00:55:44 +02:00
g-w1
d6e9862049 add test for @compileError in zig code, not only zir 2020-12-26 11:34:15 -05:00
g-w1
7f512f2236 fix test cases to use str zir inst instead of just a quoted string 2020-12-26 11:22:16 -05:00
Veikka Tuominen
795770bcb4
stage1: const_values_equal error union, improve remaining "TODO" panics 2020-12-26 13:29:48 +02:00
Veikka Tuominen
50e8c3882a
translate-c: demote variadic functions to declarations 2020-12-26 13:02:17 +02:00
Veikka Tuominen
641bf4c46e
Merge pull request #7552 from Vexu/stage2-continue
stage2 continue
2020-12-26 12:26:16 +02:00
g-w1
1634d45f1d
stage2: add compile log statement (#7191) 2020-12-26 02:40:49 +02:00
Veikka Tuominen
a50759325c
stage2: add error for unused labels 2020-12-26 02:36:12 +02:00
Veikka Tuominen
40aad4f47e
stage2: break and continue out of loops 2020-12-26 02:26:53 +02:00
Vexu
1780132d7c
fix tests 2020-12-25 14:58:14 +02:00
Vexu
0bc82a9070
stage1: validate pointer attributes when coercing anon literals 2020-12-25 14:58:13 +02:00
Vexu
990eccf282
stage1: implement type coercion of pointer to anon list to array/struct/union/slice 2020-12-25 14:57:46 +02:00
Vexu
ed028fd660
stage1: implement type coercion of anon list to array 2020-12-25 14:57:46 +02:00
Evan Haas
830bc41b1f Correctly cast bool to signed int in translate-c
Previously casting a bool to an int would result in the following Zig code:

    @intCast(c_int, @bitCast(i1, @intCast(u1, @boolToInt(b))));

This is incorrect if `b` is true, since bitcasting a `u1` with the value 1
to an `i1` will result in the value -1. Instead, generate the following code:

    @as(c_int, @boolToInt(b));

Since @boolToInt returns a `u1`, this is only disallowed if the destination
type is one-bit and signed, which can only happen if it's a bitfield
(currently not supported by translate-c)
2020-12-25 14:38:31 +02:00
LemonBoy
e18abab55a stage1: Create a new declaration scope for union enum types
Making the enum type share the scope with the parent union means every
declaration "bleeds" into the enum scope.
Let's mint a fresh empty scope for the enum type.

Thanks to @Vexu for the test case.

Closes #7532
2020-12-24 15:57:12 +02:00
Veikka Tuominen
e5aab62228
move ArrayListSentineled to std lib orphanage 2020-12-23 16:24:22 +02:00
Veikka Tuominen
ce7dcf2294
Merge pull request #7507 from joachimschmidt557/stage2-arm
stage2 ARM: implement basic binary bitwise operations
2020-12-23 12:17:40 +02:00
g-w1
cb3198af2a
stage2: @TypeOf (#7475)
* stage2: add @TypeOf

* stage2: discriminate on what type of @builtinCall in nodeMayNeedMemoryLocation

* merge upstream into my stash

* add type equality to make easier to test and defer free the types

* remove addDeclErr, I dont know why I added it, its from a different branch that im working on

* add tests

* update error message to match stage1

* use ComptimeStringMap and update which nodes don't need memory from vexu's suggestions

* fix typo

Co-authored-by: Veikka Tuominen <git@vexu.eu>

* make @TypeOf(single_arg) go to .typeof zir inst and add test for that

* unioninit, as, reduce change mayneedmemorylocation

Co-authored-by: Veikka Tuominen <git@vexu.eu>
2020-12-23 01:26:36 +02:00
g-w1
ea18f894f5
Peer type resolution with unsigned ints and larger signed ints 2020-12-23 00:05:42 +02:00
Veikka Tuominen
03113d9246
Merge pull request #7111 from tetsuo-cpp/emit-h
Implement emit-h
2020-12-23 00:01:22 +02:00
Evan Haas
ccdb81fb31 Improve handling of C compiler intrinsics in translate-c
C compiler intrinsics can only appear as part of a function call. When called
they are implicitly cast to a function pointer; treat this as a non-null
pointer so that it emits as a regular Zig function call.

Put `pub usingnamespace @import("std").c.builtins;` at the top of translated
C files so that they will have access to builtin functions defined there.

Fixes #6707
2020-12-22 23:59:30 +02:00
Alex Cameron
58bd6c5f8e Add tests for emit-h functionality. 2020-12-23 01:14:35 +11:00