Skip to content

Commit

Permalink
zig 0.12.0-dev.2150+63de8a598
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Jan 12, 2024
1 parent e17af30 commit 02e9c81
Show file tree
Hide file tree
Showing 28 changed files with 535 additions and 453 deletions.
68 changes: 33 additions & 35 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const Build = std.build;
const Build = std.Build;

const BuzzDebugOptions = struct {
debug: bool,
Expand All @@ -10,7 +10,7 @@ const BuzzDebugOptions = struct {
stop_on_report: bool,
placeholders: bool,

pub fn step(self: BuzzDebugOptions, options: *std.build.OptionsStep) void {
pub fn step(self: BuzzDebugOptions, options: *Build.Step.Options) void {
options.addOption(@TypeOf(self.debug), "debug", self.debug);
options.addOption(@TypeOf(self.stack), "debug_stack", self.stack);
options.addOption(@TypeOf(self.current_instruction), "debug_current_instruction", self.current_instruction);
Expand All @@ -26,7 +26,7 @@ const BuzzJITOptions = struct {
debug: bool,
prof_threshold: f128 = 0.05,

pub fn step(self: BuzzJITOptions, options: *std.build.OptionsStep) void {
pub fn step(self: BuzzJITOptions, options: *Build.Step.Options) void {
options.addOption(@TypeOf(self.debug), "jit_debug", self.debug);
options.addOption(@TypeOf(self.always_on), "jit_always_on", self.always_on);
options.addOption(@TypeOf(self.on), "jit", self.on);
Expand All @@ -43,7 +43,7 @@ const BuzzGCOptions = struct {
next_gc_ratio: usize,
next_full_gc_ratio: usize,

pub fn step(self: BuzzGCOptions, options: *std.build.OptionsStep) void {
pub fn step(self: BuzzGCOptions, options: *Build.Step.Options) void {
options.addOption(@TypeOf(self.debug), "gc_debug", self.debug);
options.addOption(@TypeOf(self.debug_light), "gc_debug_light", self.debug_light);
options.addOption(@TypeOf(self.debug_access), "gc_debug_access", self.debug_access);
Expand All @@ -61,9 +61,9 @@ const BuzzBuildOptions = struct {
debug: BuzzDebugOptions,
gc: BuzzGCOptions,
jit: BuzzJITOptions,
target: std.zig.CrossTarget,
target: Build.ResolvedTarget,

pub fn step(self: @This(), b: *Build) *std.build.OptionsStep {
pub fn step(self: @This(), b: *Build) *Build.Module {
var options = b.addOptions();
options.addOption(@TypeOf(self.version), "version", self.version);
options.addOption(@TypeOf(self.sha), "sha", self.sha);
Expand All @@ -73,15 +73,11 @@ const BuzzBuildOptions = struct {
self.gc.step(options);
self.jit.step(options);

return options;
return options.createModule();
}

pub fn needLibC(self: @This()) bool {
// TODO: remove libc if possible
// mir can be built with musl libc
// mimalloc can be built with musl libc
// longjmp/setjmp need to be removed
return self.target.isLinux() or self.mimalloc;
pub fn needLibC(_: @This()) bool {
return true;
}
};

Expand All @@ -92,7 +88,7 @@ fn get_buzz_prefix(b: *Build) []const u8 {
pub fn build(b: *Build) !void {
// Check minimum zig version
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse("0.12.0-dev.1253+b798aaf49") catch return;
const min_zig = std.SemanticVersion.parse("0.12.0-dev.2150+63de8a598") catch return;
if (current_zig.order(min_zig).compare(.lt)) {
@panic(b.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig }));
}
Expand Down Expand Up @@ -240,6 +236,8 @@ pub fn build(b: *Build) !void {
},
};

const build_option_module = build_options.step(b);

var sys_libs = std.ArrayList([]const u8).init(b.allocator);
defer sys_libs.deinit();
var includes = std.ArrayList([]const u8).init(b.allocator);
Expand Down Expand Up @@ -296,7 +294,7 @@ pub fn build(b: *Build) !void {

var exe = b.addExecutable(.{
.name = "buzz",
.root_source_file = Build.FileSource.relative("src/main.zig"),
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = build_mode,
});
Expand All @@ -323,13 +321,13 @@ pub fn build(b: *Build) !void {
if (build_options.needLibC()) {
exe.linkLibC();
}
exe.main_mod_path = .{ .path = "." };
// exe.main_mod_path = .{ .path = "." };

exe.addOptions("build_options", build_options.step(b));
exe.root_module.addImport("build_options", build_option_module);

var lib = b.addSharedLibrary(.{
.name = "buzz",
.root_source_file = Build.FileSource.relative("src/buzz_api.zig"),
.root_source_file = .{ .path = "src/buzz_api.zig" },
.target = target,
.optimize = build_mode,
});
Expand All @@ -346,14 +344,14 @@ pub fn build(b: *Build) !void {
if (build_options.needLibC()) {
lib.linkLibC();
}
lib.main_mod_path = .{ .path = "src" };
// lib.main_mod_path = .{ .path = "src" };

lib.addOptions("build_options", build_options.step(b));
lib.root_module.addImport("build_options", build_option_module);

lib.linkLibrary(lib_pcre2);
if (lib_mimalloc) |mimalloc| {
lib.linkLibrary(mimalloc);
if (lib.target.getOsTag() == .windows) {
if (lib.root_module.resolved_target.?.result.os.tag == .windows) {
lib.linkSystemLibrary("bcrypt");
}
}
Expand Down Expand Up @@ -412,11 +410,11 @@ pub fn build(b: *Build) !void {

// TODO: this section is slow. Modifying Buzz parser shouldn't trigger recompile of all buzz dynamic libraries

var libs = [_]*std.build.LibExeObjStep{undefined} ** lib_names.len;
var libs = [_]*std.Build.Step.Compile{undefined} ** lib_names.len;
for (lib_paths, 0..) |lib_path, index| {
var std_lib = b.addSharedLibrary(.{
.name = lib_names[index],
.root_source_file = Build.FileSource.relative(lib_path),
.root_source_file = .{ .path = lib_path },
.target = target,
.optimize = build_mode,
});
Expand All @@ -436,16 +434,16 @@ pub fn build(b: *Build) !void {
if (build_options.needLibC()) {
std_lib.linkLibC();
}
std_lib.main_mod_path = .{ .path = "src" };
// std_lib.main_mod_path = .{ .path = "src" };
std_lib.linkLibrary(lib_pcre2);
if (lib_mimalloc) |mimalloc| {
std_lib.linkLibrary(mimalloc);
if (std_lib.target.getOsTag() == .windows) {
if (std_lib.root_module.resolved_target.?.result.os.tag == .windows) {
std_lib.linkSystemLibrary("bcrypt");
}
}
std_lib.linkLibrary(lib);
std_lib.addOptions("build_options", build_options.step(b));
std_lib.root_module.addImport("build_options", build_option_module);

// Adds `$BUZZ_PATH/lib` and `/usr/local/lib/buzz` as search path for other shared lib referenced by this one (libbuzz.dylib most of the time)
std_lib.addRPath(
Expand All @@ -465,14 +463,14 @@ pub fn build(b: *Build) !void {

for (all_lib_names) |name| {
const step = b.addInstallLibFile(
std.build.FileSource.relative(b.fmt("src/lib/{s}.buzz", .{name})),
.{ .path = b.fmt("src/lib/{s}.buzz", .{name}) },
b.fmt("buzz/{s}.buzz", .{name}),
);
install_step.dependOn(&step.step);
}

const tests = b.addTest(.{
.root_source_file = Build.FileSource.relative("src/main.zig"),
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = build_mode,
});
Expand All @@ -491,11 +489,11 @@ pub fn build(b: *Build) !void {
tests.linkLibrary(lib_pcre2);
if (lib_mimalloc) |mimalloc| {
tests.linkLibrary(mimalloc);
if (tests.target.getOsTag() == .windows) {
if (tests.root_module.resolved_target.?.result.os.tag == .windows) {
tests.linkSystemLibrary("bcrypt");
}
}
tests.addOptions("build_options", build_options.step(b));
tests.root_module.addImport("build_options", build_option_module);

const test_step = b.step("test", "Run all the tests");
const run_tests = b.addRunArtifact(tests);
Expand All @@ -505,7 +503,7 @@ pub fn build(b: *Build) !void {
test_step.dependOn(&run_tests.step);
}

pub fn buildPcre2(b: *Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) !*Build.Step.Compile {
pub fn buildPcre2(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*Build.Step.Compile {
const copyFiles = b.addWriteFiles();
copyFiles.addCopyFileToSource(
.{ .path = "vendors/pcre2/src/config.h.generic" },
Expand Down Expand Up @@ -571,7 +569,7 @@ pub fn buildPcre2(b: *Build, target: std.zig.CrossTarget, optimize: std.builtin.
return lib;
}

pub fn buildMimalloc(b: *Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) !*Build.Step.Compile {
pub fn buildMimalloc(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*Build.Step.Compile {
const lib = b.addStaticLibrary(
.{
.name = "mimalloc",
Expand All @@ -583,7 +581,7 @@ pub fn buildMimalloc(b: *Build, target: std.zig.CrossTarget, optimize: std.built
lib.addIncludePath(.{ .path = "./vendors/mimalloc/include" });
lib.linkLibC();

if (lib.target.getOsTag() == .macos) {
if (lib.root_module.resolved_target.?.result.os.tag == .macos) {
var macOS_sdk_path = std.ArrayList(u8).init(b.allocator);
try macOS_sdk_path.writer().print(
"{s}/usr/include",
Expand Down Expand Up @@ -630,7 +628,7 @@ pub fn buildMimalloc(b: *Build, target: std.zig.CrossTarget, optimize: std.built
"./vendors/mimalloc/src/stats.c",
"./vendors/mimalloc/src/prim/prim.c",
},
.flags = if (lib.optimize != .Debug)
.flags = if (lib.root_module.optimize != .Debug)
&.{
"-DNDEBUG=1",
"-DMI_SECURE=0",
Expand All @@ -644,7 +642,7 @@ pub fn buildMimalloc(b: *Build, target: std.zig.CrossTarget, optimize: std.built
return lib;
}

pub fn buildLinenoise(b: *Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) !*Build.Step.Compile {
pub fn buildLinenoise(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*Build.Step.Compile {
const lib = b.addStaticLibrary(.{
.name = "linenoise",
.target = target,
Expand Down
2 changes: 1 addition & 1 deletion src/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ pub fn toValue(self: Self, node: Node.Index, gc: *GarbageCollector) Error!Value
const elements = self.nodes.items(.components)[node].String;

var string = std.ArrayList(u8).init(gc.allocator);
var writer = &string.writer();
const writer = &string.writer();
for (elements) |element| {
try (try self.toValue(element, gc)).toString(writer);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub fn emitConstant(self: *Self, location: Ast.TokenIndex, value: Value) !void {
}

pub fn makeConstant(self: *Self, value: Value) !u24 {
var constant: u24 = try self.current.?.function.?.chunk.addConstant(null, value);
const constant: u24 = try self.current.?.function.?.chunk.addConstant(null, value);
if (constant > Chunk.max_constants) {
self.reportError("Too many constants in one chunk.");
return 0;
Expand Down Expand Up @@ -1142,7 +1142,7 @@ fn generateCall(self: *Self, node: Ast.Node.Index, breaks: ?*std.ArrayList(usize
try self.emit(locations[node], @intCast(arg_count - correct_index - 1));

// Switch it in the reference
var temp = arguments_order_ref.items[index];
const temp = arguments_order_ref.items[index];
arguments_order_ref.items[index] = arguments_order_ref.items[correct_index];
arguments_order_ref.items[correct_index] = temp;

Expand Down Expand Up @@ -2026,7 +2026,7 @@ fn generateFunction(self: *Self, node: Ast.Node.Index, breaks: ?*std.ArrayList(u
return null;
}

var enclosing = self.current;
const enclosing = self.current;
self.current = try self.gc.allocator.create(Frame);
self.current.?.* = .{
.enclosing = enclosing,
Expand Down
10 changes: 5 additions & 5 deletions src/FFI.zig
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub fn parseTypeExpr(self: *Self, ztype: []const u8) !?*Zdef {

full.writer().print("const zig_type: {s};", .{ztype}) catch @panic("Out of memory");

var zdef = try self.parse(
const zdef = try self.parse(
null,
Token.identifier(full.items),
true,
Expand Down Expand Up @@ -363,7 +363,7 @@ fn getZdef(self: *Self, decl_index: Ast.Node.Index) !?*Zdef {
const zdef = try self.getZdef(decl.data.lhs);

if (zdef) |uzdef| {
var opt_zdef = try self.gc.allocator.create(Zdef);
const opt_zdef = try self.gc.allocator.create(Zdef);
opt_zdef.* = Zdef{
.zig_type = .{
.Optional = .{
Expand Down Expand Up @@ -704,7 +704,7 @@ fn ptrType(self: *Self, tag: Ast.Node.Tag, decl_index: Ast.Node.Index) anyerror!

// Is it a null terminated string?
// zig fmt: off
var zdef = try self.gc.allocator.create(Zdef);
const zdef = try self.gc.allocator.create(Zdef);
zdef.* = if (ptr_type.const_token != null
and child_type.zig_type == .Int
and child_type.zig_type.Int.bits == 8
Expand Down Expand Up @@ -850,12 +850,12 @@ fn fnProto(self: *Self, tag: Ast.Node.Tag, decl_index: Ast.Node.Index) anyerror!
parameters_zig_types.shrinkAndFree(parameters_zig_types.items.len);
zig_fn_type.params = parameters_zig_types.items;

var type_def = o.ObjTypeDef{
const type_def = o.ObjTypeDef{
.def_type = .Function,
.resolved_type = .{ .Function = function_def },
};

var zdef = try self.gc.allocator.create(Zdef);
const zdef = try self.gc.allocator.create(Zdef);
zdef.* = .{
.zig_type = ZigType{ .Fn = zig_fn_type },
.type_def = try self.gc.type_registry.getTypeDef(type_def),
Expand Down
Loading

0 comments on commit 02e9c81

Please sign in to comment.