Skip to content

Commit

Permalink
Merge pull request #8554 from ziglang/stage2-whole-file-astgen
Browse files Browse the repository at this point in the history
Stage2 whole file astgen
  • Loading branch information
andrewrk authored May 19, 2021
2 parents d228d86 + 6672366 commit 6435750
Show file tree
Hide file tree
Showing 359 changed files with 21,950 additions and 14,681 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,6 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/src/codegen/llvm.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/llvm/bindings.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/riscv64.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/spu-mk2.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/wasm.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/x86_64.zig"
"${CMAKE_SOURCE_DIR}/src/glibc.zig"
Expand Down Expand Up @@ -595,7 +594,7 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/src/type.zig"
"${CMAKE_SOURCE_DIR}/src/value.zig"
"${CMAKE_SOURCE_DIR}/src/windows_sdk.zig"
"${CMAKE_SOURCE_DIR}/src/zir.zig"
"${CMAKE_SOURCE_DIR}/src/Zir.zig"
"${CMAKE_SOURCE_DIR}/src/Sema.zig"
)

Expand Down
43 changes: 28 additions & 15 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const builtin = @import("builtin");
const std = @import("std");
const builtin = std.builtin;
const Builder = std.build.Builder;
const tests = @import("test/tests.zig");
const BufMap = std.BufMap;
Expand Down Expand Up @@ -54,6 +54,7 @@ pub fn build(b: *Builder) !void {
const skip_compile_errors = b.option(bool, "skip-compile-errors", "Main test suite skips compile error tests") orelse false;
const skip_run_translated_c = b.option(bool, "skip-run-translated-c", "Main test suite skips run-translated-c tests") orelse false;
const skip_stage2_tests = b.option(bool, "skip-stage2-tests", "Main test suite skips self-hosted compiler tests") orelse false;
const skip_install_lib_files = b.option(bool, "skip-install-lib-files", "Do not copy lib/ files to installation prefix") orelse false;

const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;
const is_stage1 = b.option(bool, "stage1", "Build the stage1 compiler, put stage2 behind a feature flag") orelse false;
Expand All @@ -62,19 +63,23 @@ pub fn build(b: *Builder) !void {
const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse (is_stage1 or static_llvm);
const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h");

b.installDirectory(InstallDirectoryOptions{
.source_dir = "lib",
.install_dir = .Lib,
.install_subdir = "zig",
.exclude_extensions = &[_][]const u8{
"test.zig",
"README.md",
".z.0",
".z.9",
".gz",
"rfc1951.txt",
},
});
if (!skip_install_lib_files) {
b.installDirectory(InstallDirectoryOptions{
.source_dir = "lib",
.install_dir = .Lib,
.install_subdir = "zig",
.exclude_extensions = &[_][]const u8{
"README.md",
".z.0",
".z.9",
".gz",
"rfc1951.txt",
},
.blank_extensions = &[_][]const u8{
"test.zig",
},
});
}

if (only_install_lib_files)
return;
Expand All @@ -83,6 +88,12 @@ pub fn build(b: *Builder) !void {
const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm;
const strip = b.option(bool, "strip", "Omit debug information") orelse false;

const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: {
if (strip) break :blk @as(u32, 0);
if (mode != .Debug) break :blk 0;
break :blk 4;
};

const main_file = if (is_stage1) "src/stage1.zig" else "src/main.zig";

var exe = b.addExecutable("zig", main_file);
Expand All @@ -93,6 +104,7 @@ pub fn build(b: *Builder) !void {
toolchain_step.dependOn(&exe.step);
b.default_step.dependOn(&exe.step);

exe.addBuildOption(u32, "mem_leak_frames", mem_leak_frames);
exe.addBuildOption(bool, "skip_non_native", skip_non_native);
exe.addBuildOption(bool, "have_llvm", enable_llvm);
if (enable_llvm) {
Expand Down Expand Up @@ -228,6 +240,7 @@ pub fn build(b: *Builder) !void {
test_stage2.addBuildOption(bool, "enable_qemu", is_qemu_enabled);
test_stage2.addBuildOption(bool, "enable_wine", is_wine_enabled);
test_stage2.addBuildOption(bool, "enable_wasmtime", is_wasmtime_enabled);
test_stage2.addBuildOption(u32, "mem_leak_frames", mem_leak_frames * 2);
test_stage2.addBuildOption(bool, "enable_darling", is_darling_enabled);
test_stage2.addBuildOption(?[]const u8, "glibc_multi_install_dir", glibc_multi_dir);
test_stage2.addBuildOption([]const u8, "version", version);
Expand Down Expand Up @@ -266,7 +279,7 @@ pub fn build(b: *Builder) !void {
toolchain_step.dependOn(tests.addPkgTests(
b,
test_filter,
"test/stage1/behavior.zig",
"test/behavior.zig",
"behavior",
"Run the behavior tests",
modes,
Expand Down
52 changes: 16 additions & 36 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,6 @@ const nan = std.math.nan(f128);
{#code_release_fast#}
{#code_disable_cache#}
const std = @import("std");
const builtin = std.builtin;
const big = @as(f64, 1 << 40);

export fn foo_strict(x: f64) f64 {
Expand Down Expand Up @@ -2589,7 +2588,7 @@ test "default struct initialization fields" {
exactly their bit width.
</li>
<li>{#syntax#}bool{#endsyntax#} fields use exactly 1 bit.</li>
<li>A {#link|packed enum#} field uses exactly the bit width of its integer tag type.</li>
<li>An {#link|enum#} field uses exactly the bit width of its integer tag type.</li>
<li>A {#link|packed union#} field uses exactly the bit width of the union field with
the largest bit width.</li>
<li>Non-ABI-aligned fields are packed into the smallest possible
Expand All @@ -2603,7 +2602,7 @@ test "default struct initialization fields" {
</p>
{#code_begin|test#}
const std = @import("std");
const builtin = std.builtin;
const native_endian = @import("builtin").target.cpu.arch.endian();
const expect = std.testing.expect;

const Full = packed struct {
Expand All @@ -2625,7 +2624,7 @@ fn doTheTest() !void {
try expect(@sizeOf(Divided) == 2);
var full = Full{ .number = 0x1234 };
var divided = @bitCast(Divided, full);
switch (builtin.endian) {
switch (native_endian) {
.Big => {
try expect(divided.half1 == 0x12);
try expect(divided.quarter3 == 0x3);
Expand Down Expand Up @@ -3015,25 +3014,6 @@ export fn entry(foo: Foo) void { }
{#code_end#}
{#header_close#}

{#header_open|packed enum#}
<p>By default, the size of enums is not guaranteed.</p>
<p>{#syntax#}packed enum{#endsyntax#} causes the size of the enum to be the same as the size of the
integer tag type of the enum:</p>
{#code_begin|test#}
const std = @import("std");

test "packed enum" {
const Number = packed enum(u8) {
one,
two,
three,
};
try std.testing.expect(@sizeOf(Number) == @sizeOf(u8));
}
{#code_end#}
<p>This makes the enum eligible to be in a {#link|packed struct#}.</p>
{#header_close#}

{#header_open|Enum Literals#}
<p>
Enum literals allow specifying the name of an enum field without specifying the enum type:
Expand Down Expand Up @@ -4255,7 +4235,7 @@ test "noreturn" {
<p>Another use case for {#syntax#}noreturn{#endsyntax#} is the {#syntax#}exit{#endsyntax#} function:</p>
{#code_begin|test#}
{#target_windows#}
pub extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(if (@import("builtin").arch == .i386) .Stdcall else .C) noreturn;
pub extern "kernel32" fn ExitProcess(exit_code: c_uint) callconv(if (@import("builtin").target.cpu.arch == .i386) .Stdcall else .C) noreturn;

test "foo" {
const value = bar() catch ExitProcess(1);
Expand Down Expand Up @@ -4290,7 +4270,7 @@ export fn sub(a: i8, b: i8) i8 { return a - b; }
// at link time, when linking statically, or at runtime, when linking
// dynamically.
// The callconv specifier changes the calling convention of the function.
extern "kernel32" fn ExitProcess(exit_code: u32) callconv(if (@import("builtin").arch == .i386) .Stdcall else .C) noreturn;
extern "kernel32" fn ExitProcess(exit_code: u32) callconv(if (@import("builtin").target.cpu.arch == .i386) .Stdcall else .C) noreturn;
extern "c" fn atan2(a: f64, b: f64) f64;

// The @setCold builtin tells the optimizer that a function is rarely called.
Expand Down Expand Up @@ -7596,7 +7576,7 @@ export fn @"A function name that is a complete sentence."() void {}
The {#syntax#}fence{#endsyntax#} function is used to introduce happens-before edges between operations.
</p>
<p>
{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("builtin").AtomicOrder{#endsyntax#}.
{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("std").builtin.AtomicOrder{#endsyntax#}.
</p>
{#see_also|Compile Variables#}
{#header_close#}
Expand Down Expand Up @@ -7799,8 +7779,8 @@ test "@hasDecl" {
</p>
<ul>
<li>{#syntax#}@import("std"){#endsyntax#} - Zig Standard Library</li>
<li>{#syntax#}@import("builtin"){#endsyntax#} - Compiler-provided types and variables.
The command <code>zig builtin</code> outputs the source to stdout for reference.
<li>{#syntax#}@import("builtin"){#endsyntax#} - Target-specific information
The command <code>zig build-exe --show-builtin</code> outputs the source to stdout for reference.
</li>
</ul>
{#see_also|Compile Variables|@embedFile#}
Expand Down Expand Up @@ -7931,11 +7911,11 @@ mem.set(u8, dest, c);{#endsyntax#}</pre>
</p>
{#code_begin|test#}
const std = @import("std");
const builtin = @import("builtin");
const native_arch = @import("builtin").target.cpu.arch;
const expect = std.testing.expect;

test "@wasmMemoryGrow" {
if (builtin.arch != .wasm32) return error.SkipZigTest;
if (native_arch != .wasm32) return error.SkipZigTest;

var prev = @wasmMemorySize(0);
try expect(prev == @wasmMemoryGrow(0, 1));
Expand Down Expand Up @@ -8103,7 +8083,7 @@ test "foo" {
{#header_close#}

{#header_open|@setFloatMode#}
<pre>{#syntax#}@setFloatMode(mode: @import("builtin").FloatMode){#endsyntax#}</pre>
<pre>{#syntax#}@setFloatMode(mode: @import("std").builtin.FloatMode){#endsyntax#}</pre>
<p>
Sets the floating point mode of the current scope. Possible values are:
</p>
Expand Down Expand Up @@ -8292,7 +8272,7 @@ test "vector @splat" {
{#header_close#}

{#header_open|@reduce#}
<pre>{#syntax#}@reduce(comptime op: builtin.ReduceOp, value: anytype) std.meta.Child(value){#endsyntax#}</pre>
<pre>{#syntax#}@reduce(comptime op: std.builtin.ReduceOp, value: anytype) std.meta.Child(value){#endsyntax#}</pre>
<p>
Transforms a {#link|vector|Vectors#} into a scalar value by performing a
sequential horizontal reduction of its elements using the specified operator {#syntax#}op{#endsyntax#}.
Expand Down Expand Up @@ -8584,7 +8564,7 @@ test "integer truncation" {
{#header_close#}

{#header_open|@Type#}
<pre>{#syntax#}@Type(comptime info: @import("builtin").TypeInfo) type{#endsyntax#}</pre>
<pre>{#syntax#}@Type(comptime info: std.builtin.TypeInfo) type{#endsyntax#}</pre>
<p>
This function is the inverse of {#link|@typeInfo#}. It reifies type information
into a {#syntax#}type{#endsyntax#}.
Expand Down Expand Up @@ -8626,7 +8606,7 @@ test "integer truncation" {
</ul>
{#header_close#}
{#header_open|@typeInfo#}
<pre>{#syntax#}@typeInfo(comptime T: type) @import("std").builtin.TypeInfo{#endsyntax#}</pre>
<pre>{#syntax#}@typeInfo(comptime T: type) std.builtin.TypeInfo{#endsyntax#}</pre>
<p>
Provides type reflection.
</p>
Expand Down Expand Up @@ -9647,7 +9627,7 @@ test "string literal to constant slice" {
</p>
{#code_begin|syntax#}
const builtin = @import("builtin");
const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';
const separator = if (builtin.os.tag == builtin.Os.windows) '\\' else '/';
{#code_end#}
<p>
Example of what is imported with {#syntax#}@import("builtin"){#endsyntax#}:
Expand All @@ -9672,7 +9652,7 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';
</p>
{#code_begin|test|detect_test#}
const std = @import("std");
const builtin = std.builtin;
const builtin = @import("builtin");
const expect = std.testing.expect;

test "builtin.is_test" {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Thread/AutoResetEvent.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
state: usize = UNSET,

const std = @import("../std.zig");
const builtin = @import("builtin");
const builtin = std.builtin;
const testing = std.testing;
const assert = std.debug.assert;
const StaticResetEvent = std.Thread.StaticResetEvent;
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Thread/StaticResetEvent.zig
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub const AtomicEvent = struct {
while (true) {
if (waiting == WAKE) {
rc = windows.ntdll.NtWaitForKeyedEvent(handle, key, windows.FALSE, null);
assert(rc == .WAIT_0);
assert(rc == windows.NTSTATUS.WAIT_0);
break;
} else {
waiting = @cmpxchgWeak(u32, waiters, waiting, waiting - WAIT, .Acquire, .Monotonic) orelse break;
Expand All @@ -271,7 +271,7 @@ pub const AtomicEvent = struct {
}
return error.TimedOut;
},
.WAIT_0 => {},
windows.NTSTATUS.WAIT_0 => {},
else => unreachable,
}
}
Expand Down
36 changes: 30 additions & 6 deletions lib/std/array_hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const trait = meta.trait;
const autoHash = std.hash.autoHash;
const Wyhash = std.hash.Wyhash;
const Allocator = mem.Allocator;
const builtin = @import("builtin");
const builtin = std.builtin;
const hash_map = @This();

pub fn AutoArrayHashMap(comptime K: type, comptime V: type) type {
Expand Down Expand Up @@ -158,10 +158,20 @@ pub fn ArrayHashMap(
return self.unmanaged.getOrPutValue(self.allocator, key, value);
}

/// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
pub const ensureCapacity = ensureTotalCapacity;

/// Increases capacity, guaranteeing that insertions up until the
/// `expected_count` will not cause an allocation, and therefore cannot fail.
pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {
return self.unmanaged.ensureCapacity(self.allocator, new_capacity);
pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void {
return self.unmanaged.ensureTotalCapacity(self.allocator, new_capacity);
}

/// Increases capacity, guaranteeing that insertions up until
/// `additional_count` **more** items will not cause an allocation, and
/// therefore cannot fail.
pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void {
return self.unmanaged.ensureUnusedCapacity(self.allocator, additional_count);
}

/// Returns the number of total elements which may be present before it is
Expand Down Expand Up @@ -472,10 +482,13 @@ pub fn ArrayHashMapUnmanaged(
return res.entry;
}

/// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
pub const ensureCapacity = ensureTotalCapacity;

/// Increases capacity, guaranteeing that insertions up until the
/// `expected_count` will not cause an allocation, and therefore cannot fail.
pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void {
try self.entries.ensureCapacity(allocator, new_capacity);
pub fn ensureTotalCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void {
try self.entries.ensureTotalCapacity(allocator, new_capacity);
if (new_capacity <= linear_scan_max) return;

// Ensure that the indexes will be at most 60% full if
Expand All @@ -501,6 +514,17 @@ pub fn ArrayHashMapUnmanaged(
}
}

/// Increases capacity, guaranteeing that insertions up until
/// `additional_count` **more** items will not cause an allocation, and
/// therefore cannot fail.
pub fn ensureUnusedCapacity(
self: *Self,
allocator: *Allocator,
additional_capacity: usize,
) !void {
return self.ensureTotalCapacity(allocator, self.count() + additional_capacity);
}

/// Returns the number of total elements which may be present before it is
/// no longer guaranteed that no allocations will be performed.
pub fn capacity(self: Self) usize {
Expand Down Expand Up @@ -1310,7 +1334,7 @@ test "reIndex" {
}

test "fromOwnedArrayList" {
comptime const array_hash_map_type = AutoArrayHashMap(i32, i32);
const array_hash_map_type = AutoArrayHashMap(i32, i32);
var al = std.ArrayListUnmanaged(array_hash_map_type.Entry){};
const hash = getAutoHashFn(i32);

Expand Down
Loading

0 comments on commit 6435750

Please sign in to comment.