15708 Commits

Author SHA1 Message Date
Ryan Liptak
70ef9bc75c Fix ensureTotalCapacity calls that should be ensureUnusedCapacity calls
If these functions are called more than once, then the array list would no longer be guaranteed to have enough capacity during the appendAssumeCapacity calls. With ensureUnusedCapacity, they will always be guaranteed to have enough capacity regardless of how many times the function is called.
2021-11-01 15:08:41 -04:00
Andrew Kelley
77eefebe65
Merge pull request #10077 from squeek502/arraylist-capacity
std.ArrayList: add ensureTotalCapacityPrecise and update doc comments
2021-11-01 14:28:27 -04:00
joachimschmidt557
002fbb0af0 stage2 AArch64: implement unconditional branches 2021-11-01 14:23:38 -04:00
Lee Cannon
f951bf8aeb correct bug with omit frame pointer logic 2021-11-01 14:14:07 -04:00
Ryan Liptak
a343758141 Update ensureTotalCapacity to ensureTotalCapacityPrecise where it makes sense
These calls are all late-initialization of ArrayList's that were initialized outside the current scope. This allows us to still get the potential memory-saving benefits of the 'precision' of initCapacity.
2021-11-01 00:57:33 -07:00
Ryan Liptak
f49d42729a std.ArrayList: add ensureTotalCapacityPrecise and update doc comments
initCapacity did and still does use the ensureTotalCapacityPrecise logic because the initial capacity of an ArrayList is not important in terms of how it grows, so allocating a more exact slice up-front allows for saving memory when the array list never exceeds that initial allocation size. There are use cases where this precise capacity is useful outside of the `init` function, though, like in instances where the user does not call the `init` function themselves but otherwise knows that an ArrayList is empty so calling `ensureTotalCapacityPrecise` can give the same memory savings that `initCapacity` would have.

Closes #9775
2021-11-01 00:57:24 -07:00
Lee Cannon
83dcfd6205 optimize AstGen.callExpr 2021-10-31 18:11:53 -04:00
Andrew Kelley
8346e011c9
Merge pull request #10068 from leecannon/tracy_improvements
stage2: tracy integration improvements
2021-10-31 17:55:29 -04:00
Jakub Konka
91d93b6395 stage2: move x86_64 codegen to arch/x86_64/CodeGen.zig
This mimics steps taken for aarch64 and preps stage2 x86_64
for a rewrite introducing MIR for this arch.
2021-10-31 17:32:39 -04:00
Jakub Konka
c452d10953
Merge pull request #10066 from joachimschmidt557/stage2-aarch64-mir
stage2 AArch64: introduce MIR
2021-10-31 18:54:19 +01:00
Lee Cannon
2d1efba8c5 watch marks a new frame on every recompilation 2021-10-31 14:35:36 +00:00
Lee Cannon
8a3a6a98aa use named frames to mark the stages of compilation 2021-10-31 14:29:49 +00:00
Lee Cannon
49d8723408 add functionality to trace allocations 2021-10-31 14:16:59 +00:00
Lee Cannon
10c9fa0e08 make tracy.zig more feature complete 2021-10-31 14:12:36 +00:00
joachimschmidt557
8a55e6b6c4
stage2 AArch64: introduce Emit.fail for handling errors in MIR emit 2021-10-31 14:27:05 +01:00
joachimschmidt557
0bdb367ee4
stage2 AArch64: implement emit debug line info 2021-10-31 12:32:11 +01:00
joachimschmidt557
9471e3da35
stage2 AArch64 Emit: implement call_extern and load_memory 2021-10-31 12:32:07 +01:00
joachimschmidt557
7fc89f64b4
stage2 AArch64: begin transition to MIR
This commit includes the transitions for the following instructions:
- add_immediate
- b
- bl
- blr
- brk
- ldp
- ldr
- ldrb
- ldrh
- mov_to_from_sp
- mov_register
- movk
- movz
- nop
- ret
- stp
- str
- strb
- strh
- sub_immediate
- svc
2021-10-31 09:22:24 +01:00
Emily Bellows
969bcb6a59 C backend: implement signed trunc 2021-10-30 16:09:55 -04:00
pfg
1de6f9a267
std: add missing termios def to std/c/linux.zig
Fixes #9707
2021-10-30 18:32:26 +02:00
Andrew Kelley
d6067db062 stage2: implement @popCount for non-vectors 2021-10-29 17:49:02 -07:00
Matthew Borkowski
5ff01bd820 gpa: fix memory limit accounting for large allocations 2021-10-29 19:22:24 -04:00
ominitay
544d7d9982 Add argument for fillFn to Random.init
As suggested by @leecannon, this provides more flexibility to the
`Random` interface. For exmaple, this allows for an implementation to
provide multiple different fill functions.
2021-10-29 19:20:31 -04:00
Andrew Kelley
66b4bd19c0 CLI: clarify help text for -z 2021-10-29 16:18:44 -07:00
Kenta Iwasaki
2cdffc97f0 zig: expose linker options and include '-z notext'
Add an option to allow the '-z notext' option to be passed to the linker
via. the compiler frontend, which is a flag that tells the linker that
relocations in read-only sections are permitted. Certain targets such as
Solana BPF rely on this flag.

Expose all linker options i.e. '-z nodelete', '-z now', '-z relro' in
the compiler frontend. Usage documentation has been updated accordingly.

Expose the '-z notext' flag in the standard library build runner.
2021-10-29 19:18:44 -04:00
InKryption
83a4bb6e69 Make std.meta.trait.isContainer true for opaques
Makes `std.meta.trait.hasFn` work as expected for opaque types with function declarations. Alternative is to add clause directly to `std.meta.trait.hasFn` to account for opaque types.
2021-10-29 20:02:25 +00:00
InKryption
eeb8629bea Use debug.assert instead of testing.expect.
`testing.expect` and friends are intended to be used only in tests; this change reflects that intention.
2021-10-29 20:01:59 +00:00
InKryption
ee038df7e2
Make pointer variables in ( init-lib | init-exe )/build.zig const. (#10057)
* Make `main_tests` variable constant
* Apply same change in init-exe.zig
2021-10-29 13:37:17 -04:00
Andrew Kelley
bbe4a9fa99 C backend: implement trunc for unsigned non-pow2 ints 2021-10-28 18:33:13 -07:00
Andrew Kelley
98009a2f66 C backend: implement trunc instruction
Note that there is not any test coverage yet for integer
truncation involving non-power-of-two integers.
2021-10-28 17:41:45 -07:00
Andrew Kelley
5479c0f9ac C backend: fix @boolToInt 2021-10-28 17:33:05 -07:00
Andrew Kelley
1c93cf52d8 C backend: fix crash when number of Decls passes a threshold
The ensureUnusedCapacity did not reserve a big enough number. I changed
it to no longer guess the capacity because I saw that the number of
possible items was not determinable ahead of time and this can therefore
avoid allocating more memory than necessary.
2021-10-28 17:23:02 -07:00
Andrew Kelley
c59ee3157f C backend: fix ptrtoint and wrap_errunion_err 2021-10-28 17:05:17 -07:00
Andrew Kelley
9ed955e5ca behavior tests: add "passing for C backend" category 2021-10-28 16:37:12 -07:00
Andrew Kelley
d2f9646d98 C backend: fix enough that zig test works
* test_functions: properly add dependencies of the array on test
   functions and test names so that the order comes out correctly.
 * fix lowering of struct literals to add parentheses around the type
   name.
 * omit const qualifier in slices because otherwise slices cannot be
   reassigned even when they are local variables.
 * special case pointer to functions and double pointer to functions in
   renderTypeAndName. This code will need to be cleaned up but for now
   it helps us make progress on other C backend stuff.
 * fix slice element access to lower to `.ptr[` instead of `[`.
 * airSliceElemVal: respect volatile slices
2021-10-28 15:59:14 -07:00
Andrew Kelley
234d94e42b C backend: emit decls sorted by dependencies
The C backend is the only backend that requires each decl to be output
in an order that satisfies the dependency graph. Here it is implemented
with a simple algorithm based on a `remaining_decls` set, using the
`dependencies` edges that are already stored for each Decl.

This satisfies incremental compilation as well as how `zig test` works,
which calls `updateDecl` on `test_functions`.
2021-10-28 13:21:37 -07:00
Andrew Kelley
8e93ec6d24 std.ArrayListUnmanaged: implement writer() 2021-10-28 13:21:17 -07:00
Andrew Kelley
e02caa7e29 zig test: when -ofmt=c, default to zig run as test exec
Previously when running `zig test` with the C backend, it would return
`error.InvalidExe` because it would try to run the C code source file as
a binary. Now, by default, it will try to run it with
`zig run -lc foo.c` and this can be overridden with the standard
`--test-cmd` flags.
2021-10-27 15:28:33 -07:00
Andrew Kelley
6504c57176 Compilation: fix crash in the compile error system
There is a table of `misc_failures` which previously did not allow
multiple errors for the same enum tag. Now it allows this by freeing the
previous compile error and replacing it with the new one. Typically this
will happen because multiple sub-Compilation objects fail with the same
problem, such as not being able to build glibc because of not having
LLVM extensions enabled.
2021-10-27 15:26:59 -07:00
Andrew Kelley
e494ce7604 stage2: fix small memory leak of test_functions when using zig test
The way `zig test` works is that it uses a stand-in

var test_functions: []const TestFn = undefined;

during semantic analysis, but then just before codegen, it swaps out the
value with a constant like this:

const test_functions: []const TestFn = .{foo, bar, baz, etc};

Before this commit, the `Module.Variable` associated with the stand-in
value was leaked; now it is properly cleaned up before being replaced.
2021-10-27 14:52:00 -07:00
Ominitay
c1a5ff34f3 std.rand: Refactor Random interface
These changes have been made to resolve issue #10037. The `Random`
interface was implemented in such a way that causes significant slowdown
when calling the `fill` function of the rng used.

The `Random` interface is no longer stored in a field of the rng, and is
instead returned by the child function `random()` of the rng. This
avoids the performance issues caused by the interface.
2021-10-27 16:07:48 -04:00
Andrew Kelley
9024f27d8f
Merge pull request #10040 from mattbork/extern-fn-scopes
stage2: fix extern fn decl parsing and astgen
2021-10-27 15:58:33 -04:00
LemonBoy
3568cd4b8f stage1: Fix path normalization on Windows
Backport a missing change from `path.zig`.

Fixes #9465
2021-10-27 15:45:42 -04:00
Andrew Kelley
3af9731600 stage2: implement runtime pointer access to global constants
The main problem that motivated these changes is that global constants
which are referenced by pointer would not be emitted into the binary.
This happened because `semaDecl` did not add `codegen_decl` tasks for
global constants, instead relying on the constant values being copied as
necessary. However when the global constants are referenced by pointer,
they need to be sent to the linker to be emitted.

After making global const arrays, structs, and unions get emitted, this
uncovered a latent issue: the anonymous decls that they referenced would
get garbage collected (via `deleteUnusedDecl`) even though they would
later be referenced by the global const.

In order to solve this problem, I introduced `anon_work_queue` which is
the same as `work_queue` except a lower priority. The `codegen_decl`
task for anon decls goes into the `anon_work_queue` ensuring that the
owner decl gets a chance to mark its anon decls as alive before they are
possibly deleted.

This caused a few regressions, which I made the judgement call to add
workarounds for. Two steps forward, one step back, is still progress.

The regressions were:
 * Two behavior tests having to do with unions. These tests were
   intentionally exercising the LLVM constant value lowering, however,
   due to the bug with garbage collection that was fixed in this commit,
   the LLVM code was not getting exercised, and union types/values were
   not implemented correctly, due to me forgetting that LLVM does not
   allow bitcasting aggregate values.
   - This is worked around by allowing those 2 test cases to regress,
     moving them to the "passing for stage1 only" section.
 * The test-stage2 test cases (in test/cases/*) for non-LLVM backends
   previously did not have any calls to lower struct values, but now
   they do. The code that was there was just `@panic("TODO")`. I
   replaced that code with a stub that generates the wrong value. This
   is an intentional miscompilation that will obviously need to get
   fixed before any struct behavior tests pass. None of the current
   tests we have exercise loading any values from these global const
   structs, so there is not a problem until we try to improve these
   backends.
2021-10-26 22:41:19 -07:00
Andrew Kelley
c1fd459f14 stage2: fix crash report code in release builds 2021-10-26 22:08:51 -07:00
Matthew Borkowski
c8a066f408 parse.zig: report error for extern fn with body 2021-10-26 20:12:28 -04:00
Andrew Kelley
11a60e8779 stage2 LLVM backend: fix bitcast
Properly handle when the operand type, the result type, or both, are
by-ref values.
2021-10-26 16:43:18 -07:00
Lewis Gaul
f890de6294
Fix bug in exp2() (#9999)
* Fix bug in exp2_64 to handle negative values (bad translation from C)

* Apply fix to exp2_32() as well, and modify comment on musl behaviour

* Use +%= instead of @addWithOverflow()
2021-10-26 18:57:58 -04:00
Jonathan Marler
ad5b90ab10 multi_array_list: get function take self by value 2021-10-26 18:55:02 -04:00
Andrew Kelley
79702c144d Sema: fix ret_ptr when inlining
Previously, it would emit a ret_ptr AIR instruction but that is not
correct because such an instruction would reference the result pointer
of the caller function rather than the callee function.

Instead, we emit an alloc instruction in this case. `ret_load` already
handles inlining correctly.
2021-10-26 15:43:57 -07:00