Commit graph

263 commits

Author SHA1 Message Date
Jacob Young
6e0de1d116 InternPool: port most of value tags 2023-06-10 20:47:54 -07:00
Andrew Kelley
7e19c95668 Sema: move inferred_alloc_const/mut_type to InternPool
Now, all types are migrated to use `InternPool`. The `Type.Tag` enum is
deleted in this commit.
2023-06-10 20:47:54 -07:00
Andrew Kelley
dfb3521160 compiler: remove var_args_param_type from SimpleType
This is now represented instead by a special `InternPool.Index.Tag` that
has no corresponding type/value.
2023-06-10 20:47:53 -07:00
Andrew Kelley
17882162b3 stage2: move function types to InternPool 2023-06-10 20:47:53 -07:00
Andrew Kelley
88dbd62bcb stage2: move enum tag values into the InternPool
I'm seeing a new assertion trip: the call to `enumTagFieldIndex` in the
implementation of `@Type` is attempting to query the field index of an
union's enum tag, but the type of the enum tag value provided is not the
same as the union's tag type. Most likely this is a problem with type
coercion, since values are now typed.

Another problem is that I added some hacks in std.builtin because I
didn't see any convenient way to access them from Sema. That should
definitely be cleaned up before merging this branch.
2023-06-10 20:46:17 -07:00
Andrew Kelley
75900ec1b5 stage2: move integer values to InternPool 2023-06-10 20:42:29 -07:00
Andrew Kelley
2f05b1482a Sema: update core comptime detection logic to be InternPool aware
* Add some assertions to make sure instructions are not none. I tested
   all these with master branch as well and made sure the behavior tests
   still passed with the assertions intact (along with a handful of
   callsite updates).
 * Fix Sema.resolveMaybeUndefValAllowVariablesMaybeRuntime not noticing
   that interned values are comptime-known. This was causing all kinds
   of chaos.
 * Fix print_air writeType calling tag() without checking for ip_index
2023-06-10 20:42:28 -07:00
Andrew Kelley
ca3cf93b21 stage2: move most simple values to InternPool 2023-06-10 20:40:04 -07:00
Andrew Kelley
9aec2758cc stage2: start the InternPool transition
Instead of doing everything at once which is a hopelessly large task,
this introduces a piecemeal transition that can be done in small
increments at a time.

This is a minimal changeset that keeps the compiler compiling. It only
uses the InternPool for a small set of types.

Behavior tests are not passing.

Air.Inst.Ref and Zir.Inst.Ref are separated into different enums but
compile-time verified to have the same fields in the same order.

The large set of changes is mainly to deal with the fact that most Type
and Value methods now require a Module to be passed in, so that the
InternPool object can be accessed.
2023-06-10 20:40:03 -07:00
mlugg
38b83d9d93 Zir: eliminate field_call_bind and field_call_bind_named
This commit removes the `field_call_bind` and `field_call_bind_named` ZIR
instructions, replacing them with a `field_call` instruction which does the bind
and call in one.

`field_call_bind` is an unfortunate instruction. It's tied into one very
specific usage pattern - its result can only be used as a callee. This means
that it creates a value of a "pseudo-type" of sorts, `bound_fn` - this type used
to exist in Zig, but now we just hide it from the user and have AstGen ensure
it's only used in one way. This is quite silly - `Type` and `Value` should, as
much as possible, reflect real Zig types and values.

It makes sense to instead encode the `a.b()` syntax as its own ZIR instruction,
so that's what we do here. This commit introduces a new instruction,
`field_call`. It's like `call`, but rather than a callee ref, it contains a ref
to the object pointer (`&a` in `a.b()`) and the string field name (`b`). This
eliminates `bound_fn` from the language, and slightly decreases the size of
generated ZIR - stats below.

This commit does remove a few usages which used to be allowed:
- `@field(a, "b")()`
- `@call(.auto, a.b, .{})`
- `@call(.auto, @field(a, "b"), .{})`

These forms used to work just like `a.b()`, but are no longer allowed. I believe
this is the correct choice for a few reasons:
- `a.b()` is a purely *syntactic* form; for instance, `(a.b)()` is not valid.
  This means it is *not* inconsistent to not allow it in these cases; the
  special case here isn't "a field access as a callee", but rather this exact
  syntactic form.
- The second argument to `@call` looks much more visually distinct from the
  callee in standard call syntax. To me, this makes it seem strange for that
  argument to not work like a normal expression in this context.
- A more practical argument: it's confusing! `@field` and `@call` are used in
  very different contexts to standard function calls: the former normally hints
  at some comptime machinery, and the latter that you want more precise control
  over parts of a function call. In these contexts, you don't want implicit
  arguments adding extra confusion: you want to be very explicit about what
  you're doing.

Lastly, some stats. I mentioned before that this change slightly reduces the
size of ZIR - this is due to two instructions (`field_call_bind` then `call`)
being replaced with one (`field_call`). Here are some numbers:

+--------------+----------+----------+--------+
| File         | Before   | After    | Change |
+--------------+----------+----------+--------+
| Sema.zig     | 4.72M    | 4.53M    | -4%    |
| AstGen.zig   | 1.52M    | 1.48M    | -3%    |
| hash_map.zig | 283.9K   | 276.2K   | -3%    |
| math.zig     | 312.6K   | 305.3K   | -2%    |
+--------------+----------+----------+--------+
2023-05-20 12:27:48 -07:00
Andrew Kelley
447a302990 Sema: eliminate Type.Tag.var_args_param
This was a special type tag used for hacky stuff in Semantic Analysis.
Move the hacky stuff to use a dedicated `Air.Inst.Ref` instead.

This way, `var_args_param` is not involved in the type system or intern
pool.
2023-05-19 11:05:05 -07:00
dweiller
64e319f555 add optional sentinel to slice_length ZIR 2023-05-07 15:55:21 +10:00
dweiller
a62b5d84d8 zir: add slice_length tag 2023-05-07 15:55:20 +10:00
Jacob Young
df4849c4f5 AstGen: cleanup previous fix
Allocating an extended tag is much cleaner and easier to reason about
than reusing an existing tag.  The previous `.data = undefined` was a
clear indication that we don't have any data to store, and so might as
well store an extended tag in that space almost for free.
2023-05-05 16:09:59 -04:00
mlugg
152c7b1885 Implement multi-argument @min/@max and notice bounds
Resolves: #14039
2023-05-02 16:46:27 -07:00
Andrew Kelley
a5c910adb6 change semantics of @memcpy and @memset
Now they use slices or array pointers with any element type instead of
requiring byte pointers.

This is a breaking enhancement to the language.

The safety check for overlapping pointers will be implemented in a
future commit.

closes #14040
2023-04-25 11:23:40 -07:00
mlugg
35d82d31be Add @inComptime builtin
Resolves: #868
2023-04-23 13:16:42 -07:00
Andrew Kelley
e2fe1907ec add c_char type
closes #875
2023-04-13 02:47:16 -04:00
mlugg
ccf670c2b0 Zir: implement explicit block_comptime instruction
Resolves: #7056
2023-04-12 12:06:19 -04:00
Loris Cro
602029bb2f
Autodoc usingnamespace (#15216)
* autodoc: init support for usingnamespace decls

* autodoc: don't build autodoc when building zig2.c

* autodoc: usingnamespace decls support in frontend (#15203)

* autodoc: init support for usingnamespace decls

* autodoc: usingnamespace decls support in frontend

---------

Co-authored-by: Krzysztof Wolicki <46651553+der-teufel-programming@users.noreply.github.com>
2023-04-12 01:14:02 +00:00
Robin Voetter
3357c59ceb new builtins: @workItemId, @workGroupId, @workGroupSize
* @workItemId returns the index of the work item in a work group for a
  dimension.
* @workGroupId returns the index of the work group in the kernel dispatch for a
  dimension.
* @workGroupSize returns the size of the work group for a dimension.

These builtins are mainly useful for GPU backends. They are currently only
implemented for the AMDGCN LLVM backend.
2023-03-30 12:20:24 +03:00
John Schmidt
ec445fb6b8 Improve error messages for break type coercion 2023-03-21 15:09:42 +02:00
Andrew Kelley
23295f64ca fix ZIR decoding of error notes 2023-03-15 10:48:14 -07:00
r00ster91
6218e40046 Zir: fix outdated comment 2023-03-06 18:18:52 -05:00
r00ster91
65368683ad add @trap builtin
This introduces a new builtin function that compiles down to something that results in an illegal instruction exception/interrupt.
It can be used to exit a program abnormally.

This implements the builtin for all backends.
2023-03-04 12:08:19 +01:00
r00ster91
d6bd00e855 Zir: move set_cold from Inst.Tag to Inst.Extended
If I could mark a builtin function as cold, I would mark @setCold as cold.
We have run out of `Zir.Inst.Tag`s so I had to move a tag from Zir.Inst.Tag to
Zir.Inst.Extended. This is because a new noreturn builtin will be added and
noreturn builtins cannot be part of Inst.Tag:
```
/// `noreturn` instructions may not go here; they must be part of the main `Tag` enum.
pub const Extended = enum(u16) {
```

Here's another reason I went for @setCold:
```
$ git grep setRuntimeSafety | wc -l
322
$ git grep setCold | wc -l
79
$ git grep setEvalBranchQuota | wc -l
82
```

This also simply removes @setCold from Autodoc and the docs frontend because
as far as I could understand it, builtins represented using Zir extended
instructions are not yet supported because I couldn't find
@setStackAlign or @setFloatMode there, either.
2023-03-03 21:16:21 +01:00
Andrew Kelley
12a7a0d76f omit safety check when incrementing for loop counter
Since for loops are statically analyzed to have an upper bound, and the
loop counter is a usize, it is impossible for it to overflow.
2023-02-18 19:20:19 -07:00
Andrew Kelley
74db8c2e83 omit safety checks for element access in for loops
One of the main points of for loops is that you can safety check the
length once, before entering the loop, and then safely assume that every
element inside the loop is in bounds.

In master branch, the safety checks are incorrectly intact even inside
for loops. This commit fixes it. It's especially nice with multi-object
loops because the number of elided checks is N * M where N is how many
iterations and M is how many objects.
2023-02-18 19:20:19 -07:00
Andrew Kelley
293d6bdc73 AstGen: back to index-based for loops 2023-02-18 19:17:20 -07:00
Andrew Kelley
841add6890 AstGen: finish multi-object for loops
This strategy uses pointer arithmetic to iterate through the loop. This
has a problem, however, which is tuples. AstGen does not know whether a
given indexable is a tuple or can be iterated based on contiguous
memory. Tuples unlike other indexables cannot be represented as a
many-item pointer that is incremented as the loop counter.

So, after this commit, I will modify AstGen back closer to how @vexu had
it before, using a counter and array element access.
2023-02-18 19:17:20 -07:00
Andrew Kelley
faa44e2e58 AstGen: rework multi-object for loop
* Allow unbounded looping.
* Lower by incrementing raw pointers for each iterable rather than
  incrementing a single index variable. This elides safety checks
  without any analysis required thanks to the length assertion and
  lowers to decent machine code even in debug builds.
  - An "end" value is selected, prioritizing a counter if possible,
    falling back to a runtime calculation of ptr+len on a slice input.
* Specialize on the pattern `0..`, avoiding an unnecessary subtraction
  instruction being emitted.
* Add the `for_check_lens` ZIR instruction.
2023-02-18 19:17:20 -07:00
Veikka Tuominen
7199d7c777 split @qualCast into @constCast and @volatileCast 2023-02-15 01:43:57 +02:00
Veikka Tuominen
f16c10a86b implement @qualCast 2023-01-30 18:55:57 +02:00
Veikka Tuominen
83673a8b5f fix errdefers in functions that can't return errors
This was broken by 58caed1c71

Closes #14053
2023-01-10 19:17:42 +02:00
Veikka Tuominen
0ecec5fcca resolve some TODOs 2023-01-05 14:26:53 +02:00
Veikka Tuominen
54160e7f6a Sema: make overflow arithmetic builtins return tuples 2022-12-27 15:13:14 +02:00
Andrew Kelley
aca9c74e80
Merge pull request #13914 from Vexu/variadic
implement defining C variadic functions
2022-12-18 16:24:13 -05:00
r00ster91
aac2d6b56f std.builtin: rename Type.UnionField and Type.StructField's field_type to type 2022-12-17 14:11:33 +01:00
Veikka Tuominen
9bb1104e37 implement defining C variadic functions 2022-12-17 13:22:09 +02:00
Veikka Tuominen
7b2a936173 remove stack option from @call 2022-12-13 12:52:21 +02:00
Veikka Tuominen
f20e449fd6 Sema: improve error for mismatched type in implicit return
Closes #2653
2022-12-03 00:48:03 +02:00
Veikka Tuominen
80575face7 AstGen: implement tuple declarations 2022-11-23 12:13:39 +02:00
Stevie Hryciw
04f3067a79 run zig fmt on everything checked by CI 2022-11-18 19:22:42 +00:00
Cody Tapscott
d060cbbec7 stage2: Keep error return traces alive when storing to const
This change extends the "lifetime" of the error return trace associated
with an error to continue throughout the block of a `const` variable
that it is assigned to.

This is necessary to support patterns like this one in test_runner.zig:
```zig
const result = foo();
if (result) |_| {
    // ... success logic
} else |err| {
    // `foo()` should be included in the error trace here
    return error.TestFailed;
}
```

To make this happen, the majority of the error return trace popping logic
needed to move into Sema, since `const x = foo();` cannot be examined
syntactically to determine whether it modifies the error return trace. We
also have to make sure not to delete pertinent block information before it
makes it to Sema, so that Sema can pop/restore around blocks correctly.

* Why do this only for `const` and not `var`? *

There is room to relax things for `var`, but only a little bit. We could
do the same thing we do for const and keep the error trace alive for the
remainder of the block where the *assignment* happens. Any wider scope
would violate the stack discipline for traces, so it's not viable.

In the end, I decided the most consistent behavior for the user is just
to kill all error return traces assigned to a mutable `var`.
2022-10-21 12:40:29 -07:00
Cody Tapscott
3007fdde45 stage2: Pop error trace when storing error to var/const
In order to enforce a strict stack discipline for error return traces,
we cannot track error return traces that are stored in variables:

  ```zig
  const x = errorable(); // errorable()'s error return trace is killed here

  // v-- error trace starts here instead
  return x catch error.UnknownError;
  ```

In order to propagate error return traces, function calls need to be passed
directly to an error-handling expression (`if`, `catch`, `try` or `return`):

  ```zig
  // When passed directly to `catch`, the return trace is propagated
  return errorable() catch error.UnknownError;

  // Using a break also works
  return blk: {
      // code here
      break :blk errorable();
  } catch error.UnknownError;
  ```

Why do we need this restriction? Without it, multiple errors can co-exist
with their own error traces. Handling that situation correctly means either:
  a. Dynamically allocating trace memory and tracking lifetimes, OR
  b. Allowing the production of one error to interfere with the trace of another
     (which is the current status quo)

This is piece (3/3) of https://github.com/ziglang/zig/issues/1923#issuecomment-1218495574
2022-10-21 10:44:20 -07:00
Veikka Tuominen
5316a00a18 stage2: properly reset error return trace index 2022-10-21 10:43:42 -07:00
Ali Chraghi
ca27055cda all: rename @maximum to @max and @minimum to @min 2022-10-18 14:15:16 +03:00
Andrew Kelley
a1486e1e1e stage2: allow comptime expressions for inline asm
It is not yet determined whether the Zig language will land on
text-based string concatenation for inline assembly, as Zig 0.9.1
allows, and as this commit allows, or whether it will introduce a new
assembly syntax more integrated with the rest of the language. Until
this decision is made, this commit relaxes the restriction which was
preventing inline assembly expressions from using comptime expressions
for the assembly source code.
2022-10-17 16:31:23 -04:00
Robin Voetter
5d429b03e3
stage2: add @addrSpaceCast builtin 2022-10-12 20:36:12 +02:00
Veikka Tuominen
0b1dd845d9 stage2: add error for non-void error union payload being ignored
See https://github.com/ziglang/zig/pull/6060#discussion_r471032912
2022-10-06 15:39:06 +03:00