Previously we would assign the error message to Sema and then never
clear it even when destroying the error message, which caused memory
corruption.
Closes#12437
Add handling for these additional `MCValue`s:
* `.immediate` - lower to `DW.OP.consts` or `DW.OP.constu` depending
on signedness followed by popping off the DWARF stack with
`DW.OP.stack_value`
* `.undef` - lower to `DW.OP.implicit_value`
* `.none` - lower to `DW.OP.lit0` followed by popping off the DWARF
stack with `DW.OP.stack_value`
For any remaining unhandled case, we generate `DW.OP.nop` in order
not to mess up remaining DWARF info.
When a local is no longer referenced or used, free it
so the local can be re-used by another instruction.
This means we generate less locals. Freeing this local
is a manual action and must only be used on temporaries
or where we are sure the local is not referenced by a
different AIR instruction, as that creates UB.
We now also no longer store a `WValue` when its tag is set to `none`
as those may never be referenced by any AIR instruction.
An assertion is done to make sure we never store a reference to a
`stack` value in our resolved instructions.
We internally use a lot of `load`'s that used to put
the result in a newly created local. For instance, when is considered
byRef or when we need a specific field/element/bytes from a larger type.
However, sometimes we want to directly use this value and then forget about
it, which means storing it in a local first is wasted instructions as well
as wasted locals that shouldn't be generated in the first place.
With this change it's explicit and requires the usage of `toLocal`.
This also does it for `wrapBinOp` which internally uses the already
refactored `binOp` and `wrapOperand` heavily simplifying this
function and not duplicate the logic from `binOp`
By keeping the result on the stack, we prevent codegen
from generating unneccesary locals when we have subsequent instructions
that do not have to be re-used.
Rather than always creating a new local and storing the result of
a binary operation into said local, we now leave it on top of the stack.
This allows for better codegen as we need less instructions, as well
as less total amount of locals.
When a local is no longer needed (for instance, it was used as
a temporary during arithmetic), it can be appended to one of
the typed freelists. This allows us to re-use locals and therefore
require less locals, reducing the binary size, as well as runtime
initialization.
This effectively reverts 22690efcc2378222503cb8aaad26a6f4a539f5aa,
re-opening #11818.
This had the following problems:
* Buggy on some targets (macOS, Windows)
* Messy output to the terminal
These problems need to be solved before moving forward with this.
This is likely the cause of the flaky test failures in master branch.
Since we have some test coverage for incremental compilation, it's not
OK to leave proper memory management of Fn objects as "TODO".
This improves the following test case:
```zig
pub fn main() !void {
try foo();
}
fn foo() !void {
return error.Bad;
}
```
The error return trace now points to the `try` token instead of pointing
to the foo() function call, matching stage1.
Closes#12308.
Sema: insert an error return trace frame when appropriate when analyzing
ret_load. Also optimize the instructions to avoid an unnecessary block
sent to the backends.
AstGen: always emit a dbg_stmt for return expressions, in between the
defer instructions and the return instruction.
This improves the following test case:
```zig
pub fn main() !void {
return foo();
}
fn foo() !void {
return error.Bad;
}
```
The error return trace now points to the return token instead of
pointing to the foo() function call, matching stage1.
stage2 was adding bogus error return trace frames when an error was not
being returned. This commit makes several improvements:
* Make a runtime check if necessary to only emit a frame into the error
return trace when an actual error is returned.
* Use the `analyzeIsNonErrComptimeOnly` machinery to avoid runtime
checks when it is compile-time-known that the value is an error, or a
non-error.
* Make std.builtin.returnError take a non-optional stack trace pointer.
closes#12174