Skip to content

Commit

Permalink
Update to 0.12.0-dev.3438+5c628312b
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix "xq" Queißner committed Mar 27, 2024
1 parent cfe5283 commit bfb089b
Show file tree
Hide file tree
Showing 21 changed files with 736 additions and 480 deletions.
48 changes: 28 additions & 20 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 Builder = std.build.Builder;
const Build = std.Build;

fn sdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("sdkPath requires an absolute path!");
Expand Down Expand Up @@ -30,20 +30,20 @@ const examples = [_]Example{
},
};

pub fn build(b: *Builder) !void {
pub fn build(b: *Build) !void {
const version_tag = b.option([]const u8, "version", "Sets the version displayed in the docs and for `lola version`");

const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});

const mod_args = b.dependency("args", .{}).module("args");
const mod_interface = b.dependency("interface", .{}).module("interface.zig");
// const mod_interface = b.dependency("interface", .{}).module("interface.zig");
const mod_any_pointer = b.dependency("any_pointer", .{}).module("any-pointer");

const mod_lola = b.addModule("lola", .{
.source_file = .{ .path = "src/library/main.zig" },
.dependencies = &.{
.{ .name = "interface", .module = mod_interface },
.root_source_file = .{ .path = "src/library/main.zig" },
.imports = &.{
// .{ .name = "interface", .module = mod_interface },
.{ .name = "any-pointer", .module = mod_any_pointer },
},
});
Expand All @@ -57,17 +57,18 @@ pub fn build(b: *Builder) !void {
.optimize = optimize,
.target = target,
});
exe.addModule("lola", mod_lola);
exe.addModule("args", mod_args);
exe.addAnonymousModule("build_options", .{
.source_file = build_options.getSource(),
exe.root_module.addImport("lola", mod_lola);
exe.root_module.addImport("args", mod_args);
exe.root_module.addAnonymousImport("build_options", .{
.root_source_file = build_options.getSource(),
});
b.installArtifact(exe);

const benchmark_renderer = b.addExecutable(.{
.name = "benchmark-render",
.root_source_file = .{ .path = "src/benchmark/render.zig" },
.optimize = optimize,
.target = b.host,
});
b.installArtifact(benchmark_renderer);

Expand Down Expand Up @@ -96,8 +97,9 @@ pub fn build(b: *Builder) !void {
.name = b.fmt("benchmark-{s}", .{@tagName(benchmark_mode)}),
.root_source_file = .{ .path = "src/benchmark/perf.zig" },
.optimize = benchmark_mode,
.target = b.host,
});
benchmark.addModule("lola", mod_lola);
benchmark.root_module.addImport("lola", mod_lola);

const run_benchmark = b.addRunArtifact(benchmark);
run_benchmark.addArg(b.pathFromRoot("benchmarks/code"));
Expand All @@ -106,13 +108,14 @@ pub fn build(b: *Builder) !void {
render_benchmark.step.dependOn(&run_benchmark.step);
}

const wasm_runtime = b.addSharedLibrary(.{
const wasm_runtime = b.addExecutable(.{
.name = "lola",
.root_source_file = .{ .path = "src/wasm-compiler/main.zig" },
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
.optimize = .ReleaseSafe,
});
wasm_runtime.addModule("lola", mod_lola);
wasm_runtime.entry = .disabled;
wasm_runtime.root_module.addImport("lola", mod_lola);
b.installArtifact(wasm_runtime);

const examples_step = b.step("examples", "Compiles all examples");
Expand All @@ -123,19 +126,24 @@ pub fn build(b: *Builder) !void {
.optimize = optimize,
.target = target,
});
example_exe.addModule("lola", mod_lola);
example_exe.root_module.addImport("lola", mod_lola);

examples_step.dependOn(&b.addInstallArtifact(example_exe, .{}).step);
}

const compiler_lola_mod = b.createModule(.{
.root_source_file = .{ .path = "src/test/compiler.lola" },
});

var main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/library/test.zig" },
.optimize = optimize,
.target = .{},
.target = b.host,
});
main_tests.addModule("interface", mod_interface);
main_tests.addModule("any-pointer", mod_any_pointer);
main_tests.main_pkg_path = .{ .path = "." };
// main_tests.root_module.addImport("interface", mod_interface);
main_tests.root_module.addImport("any-pointer", mod_any_pointer);
main_tests.root_module.addImport("compiler.lola", compiler_lola_mod);
// main_tests.main_pkg_path = .{ .path = "." };

const test_step = b.step("test", "Run test suite");
test_step.dependOn(&b.addRunArtifact(main_tests).step);
Expand Down Expand Up @@ -168,7 +176,7 @@ pub fn build(b: *Builder) !void {

// execute in the zig-cache directory so we have a "safe" playfield
// for file I/O
runlib_test.cwd = "zig-cache/tmp";
runlib_test.cwd = .{ .path = "zig-cache/tmp" };

// `Exit(123)` is the last call in the runtime suite
runlib_test.expectExitCode(123);
Expand Down
18 changes: 10 additions & 8 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

.dependencies = .{
.args = .{
.url = "https://github.com/MasterQ32/zig-args/archive/bb2eced8ddf28114b3a1ff761c2d80b90b1a61e2.tar.gz",
.hash = "12208556082c280af264ca2a174fd04fc7a35f865bfe59e2c61f4a977bf7a65063e4",
.url = "https://github.com/MasterQ32/zig-args/archive/01d72b9a0128c474aeeb9019edd48605fa6d95f7.tar.gz",
.hash = "12208a1de366740d11de525db7289345949f5fd46527db3f89eecc7bb49b012c0732",
},
.any_pointer = .{
.url = "https://github.com/MasterQ32/any-pointer/archive/88bd1b406d58bc9e82159581ea3e71a004011058.tar.gz",
.hash = "1220d00cd17f9b1f840ea4bee9b552547e1ca5938517018078046c83b71f332d3b5e",
},
.interface = .{
.url = "https://github.com/MasterQ32/interface.zig/archive/a5ebd920d2953167152f94c005043d49c258e7a1.tar.gz",
.hash = "122039c0c0e050acb36fb7b7eee5e0051695bf1964d3835b74e7e02790b922bc3e33",
.url = "https://github.com/MasterQ32/any-pointer/archive/e57d13c9f5476d9cf6dc3fa541d5a78827da96fb.tar.gz",
.hash = "12205a42860518c1ef0391686d047a3ec16ef0e7f70f0b0bba5c597bdd6afbcd0d10",
},
// .interface = .{
// .url = "https://github.com/MasterQ32/interface.zig/archive/a5ebd920d2953167152f94c005043d49c258e7a1.tar.gz",
// .hash = "122039c0c0e050acb36fb7b7eee5e0051695bf1964d3835b74e7e02790b922bc3e33",
// },
},

.paths = .{"."},
}
2 changes: 1 addition & 1 deletion examples/host/minimal-host/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn main() anyerror!u8 {
while (true) {

// Run the virtual machine for up to 150 instructions
var result = vm.execute(150) catch |err| {
const result = vm.execute(150) catch |err| {
// When the virtua machine panics, we receive a Zig error
std.debug.print("LoLa panic: {s}\n", .{@errorName(err)});
return 1;
Expand Down
8 changes: 4 additions & 4 deletions examples/host/serialization/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ fn run_serialization(allocator: std.mem.Allocator, serialization_buffer: []u8) !
var vm = try lola.runtime.VM.init(allocator, &env);
defer vm.deinit();

var result = try vm.execute(405);
const result = try vm.execute(405);
std.debug.assert(result == .exhausted); // we didn't finish running our nice example

var stdout = std.io.getStdOut().writer();
const stdout = std.io.getStdOut().writer();
try stdout.writeAll("Suspend at\n");
try vm.printStackTrace(stdout);

{
var stream = std.io.fixedBufferStream(serialization_buffer);
var writer = stream.writer();
const writer = stream.writer();

try compile_unit.saveToStream(writer);

Expand All @@ -113,7 +113,7 @@ fn run_serialization(allocator: std.mem.Allocator, serialization_buffer: []u8) !

fn run_deserialization(allocator: std.mem.Allocator, serialization_buffer: []u8) !void {
var stream = std.io.fixedBufferStream(serialization_buffer);
var reader = stream.reader();
const reader = stream.reader();

// Trivial deserialization:
// Just load the compile unit from disk again
Expand Down
10 changes: 5 additions & 5 deletions src/benchmark/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn main() !u8 {
return 1;
}

var src_dir = try std.fs.cwd().openIterableDir(argv[1], .{});
var src_dir = try std.fs.cwd().openDir(argv[1], .{ .iterate = true });
defer src_dir.close();

var dst_dir = try std.fs.cwd().openDir(argv[2], .{});
Expand All @@ -33,7 +33,7 @@ pub fn main() !u8 {
.data = undefined,
};

var file = try src_dir.dir.openFile(entry.name, .{ .mode = .read_only });
var file = try src_dir.openFile(entry.name, .{ .mode = .read_only });
defer file.close();

series.data = try loadSeries(gpa.allocator(), file);
Expand Down Expand Up @@ -85,10 +85,10 @@ pub fn renderSeriesSet(dst_dir: std.fs.Dir, file_name: []const u8, all_series: [

const time_range = end_time - start_time;

var size_x: f32 = 350;
var size_y: f32 = 200;
const size_x: f32 = 350;
const size_y: f32 = 200;

var legend_size: f32 = 50;
const legend_size: f32 = 50;

const viewport_size: f32 = size_x - legend_size;

Expand Down
14 changes: 7 additions & 7 deletions src/frontend/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ fn compile(options: CompileCLI, files: []const []const u8) !u8 {
name
else blk: {
var name = try allocator.alloc(u8, inname.len + 3);
std.mem.copy(u8, name[0..inname.len], inname);
std.mem.copy(u8, name[inname.len..], ".lm");
@memcpy(name[0..inname.len], inname);
@memcpy(name[inname.len..], ".lm");
break :blk name;
};
defer if (options.output == null)
Expand Down Expand Up @@ -323,7 +323,7 @@ fn run(options: RunCLI, files: []const []const u8) !u8 {
defer vm.deinit();

while (true) {
var result = vm.execute(options.limit) catch |err| {
const result = vm.execute(options.limit) catch |err| {
var stderr = std.io.getStdErr().writer();

if (builtin.mode == .Debug) {
Expand Down Expand Up @@ -378,8 +378,8 @@ fn run(options: RunCLI, files: []const []const u8) !u8 {
var timer = try std.time.Timer.start();

emulation: while (true) {
var result = vm.execute(options.limit) catch |err| {
var stderr = std.io.getStdErr().writer();
const result = vm.execute(options.limit) catch |err| {
const stderr = std.io.getStdErr().writer();
try stderr.print("Panic during execution: {s}\n", .{@errorName(err)});
try stderr.print("Call stack:\n", .{});

Expand Down Expand Up @@ -435,7 +435,7 @@ fn run(options: RunCLI, files: []const []const u8) !u8 {

fn compileFileToUnit(allocator: std.mem.Allocator, fileName: []const u8) !lola.CompileUnit {
const maxLength = 1 << 20; // 1 MB
var source = blk: {
const source = blk: {
var file = try std.fs.cwd().openFile(fileName, .{ .mode = .read_only });
defer file.close();

Expand All @@ -462,7 +462,7 @@ fn compileFileToUnit(allocator: std.mem.Allocator, fileName: []const u8) !lola.C
if (!successful)
return error.CompileError;

var compile_unit = try lola.compiler.generateIR(allocator, pgm, fileName);
const compile_unit = try lola.compiler.generateIR(allocator, pgm, fileName);
errdefer compile_unit;

return compile_unit;
Expand Down
44 changes: 22 additions & 22 deletions src/library/common/CompileUnit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn loadFromStream(allocator: std.mem.Allocator, stream: anytype) !CompileUni
};
if (!std.mem.eql(u8, &header, "LoLa\xB9\x40\x80\x5A"))
return error.InvalidFormat;
const version = try stream.readIntLittle(u32);
const version = try stream.readInt(u32, .little);
if (version != 1)
return error.UnsupportedVersion;

Expand All @@ -82,12 +82,12 @@ pub fn loadFromStream(allocator: std.mem.Allocator, stream: anytype) !CompileUni

unit.comment = try unit.arena.allocator().dupe(u8, utility.clampFixedString(&comment));

unit.globalCount = try stream.readIntLittle(u16);
unit.temporaryCount = try stream.readIntLittle(u16);
unit.globalCount = try stream.readInt(u16, .little);
unit.temporaryCount = try stream.readInt(u16, .little);

const functionCount = try stream.readIntLittle(u16);
const codeSize = try stream.readIntLittle(u32);
const numSymbols = try stream.readIntLittle(u32);
const functionCount = try stream.readInt(u16, .little);
const codeSize = try stream.readInt(u32, .little);
const numSymbols = try stream.readInt(u32, .little);

if (functionCount > codeSize or numSymbols > codeSize) {
// It is not reasonable to have multiple functions per
Expand All @@ -104,8 +104,8 @@ pub fn loadFromStream(allocator: std.mem.Allocator, stream: anytype) !CompileUni
var name: [128]u8 = undefined;
try stream.readNoEof(&name);

const entryPoint = try stream.readIntLittle(u32);
const localCount = try stream.readIntLittle(u16);
const entryPoint = try stream.readInt(u32, .little);
const localCount = try stream.readInt(u16, .little);

fun.* = Function{
.name = try unit.arena.allocator().dupe(u8, utility.clampFixedString(&name)),
Expand All @@ -119,9 +119,9 @@ pub fn loadFromStream(allocator: std.mem.Allocator, stream: anytype) !CompileUni
unit.code = code;

for (debugSymbols) |*sym| {
const offset = try stream.readIntLittle(u32);
const sourceLine = try stream.readIntLittle(u32);
const sourceColumn = try stream.readIntLittle(u16);
const offset = try stream.readInt(u32, .little);
const sourceLine = try stream.readInt(u32, .little);
const sourceColumn = try stream.readInt(u16, .little);
sym.* = DebugSymbol{
.offset = offset,
.sourceLine = sourceLine,
Expand All @@ -142,25 +142,25 @@ pub fn loadFromStream(allocator: std.mem.Allocator, stream: anytype) !CompileUni
/// Saves a compile unit to a data stream.
pub fn saveToStream(self: CompileUnit, stream: anytype) !void {
try stream.writeAll("LoLa\xB9\x40\x80\x5A");
try stream.writeIntLittle(u32, 1);
try stream.writeInt(u32, 1, .little);
try stream.writeAll(self.comment);
try stream.writeByteNTimes(0, 256 - self.comment.len);
try stream.writeIntLittle(u16, self.globalCount);
try stream.writeIntLittle(u16, self.temporaryCount);
try stream.writeIntLittle(u16, @as(u16, @intCast(self.functions.len)));
try stream.writeIntLittle(u32, @as(u32, @intCast(self.code.len)));
try stream.writeIntLittle(u32, @as(u32, @intCast(self.debugSymbols.len)));
try stream.writeInt(u16, self.globalCount, .little);
try stream.writeInt(u16, self.temporaryCount, .little);
try stream.writeInt(u16, @as(u16, @intCast(self.functions.len)), .little);
try stream.writeInt(u32, @as(u32, @intCast(self.code.len)), .little);
try stream.writeInt(u32, @as(u32, @intCast(self.debugSymbols.len)), .little);
for (self.functions) |fun| {
try stream.writeAll(fun.name);
try stream.writeByteNTimes(0, 128 - fun.name.len);
try stream.writeIntNative(u32, fun.entryPoint);
try stream.writeIntNative(u16, fun.localCount);
try stream.writeInt(u32, fun.entryPoint, .little);
try stream.writeInt(u16, fun.localCount, .little);
}
try stream.writeAll(self.code);
for (self.debugSymbols) |sym| {
try stream.writeIntNative(u32, sym.offset);
try stream.writeIntNative(u32, sym.sourceLine);
try stream.writeIntNative(u16, sym.sourceColumn);
try stream.writeInt(u32, sym.offset, .little);
try stream.writeInt(u32, sym.sourceLine, .little);
try stream.writeInt(u16, sym.sourceColumn, .little);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/library/common/Decoder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn readRaw(self: *Decoder, dest: []u8) !void {
return error.EndOfStream;
if (self.offset + dest.len > self.data.len)
return error.NotEnoughData;
std.mem.copy(u8, dest, self.data[self.offset .. self.offset + dest.len]);
@memcpy(dest, self.data[self.offset .. self.offset + dest.len]);
self.offset += @as(u32, @intCast(dest.len));
}

Expand All @@ -49,7 +49,7 @@ pub fn read(self: *Decoder, comptime T: type) !T {

const data = try self.readBytes(@sizeOf(T));
switch (T) {
u8, u16, u32 => return std.mem.readIntLittle(T, &data),
u8, u16, u32 => return std.mem.readInt(T, &data, .little),
f64 => return @as(f64, @bitCast(data)),
ir.InstructionName => return try std.meta.intToEnum(ir.InstructionName, data[0]),
else => @compileError("Unsupported type " ++ @typeName(T) ++ " for Decoder.read!"),
Expand Down
Loading

0 comments on commit bfb089b

Please sign in to comment.