base code

This commit is contained in:
Lorenzo Torres 2025-03-12 19:56:19 +01:00
parent fc39b27715
commit 4f45899a3c
197 changed files with 151568 additions and 2 deletions

2
src/assets.zig Normal file
View file

@ -0,0 +1,2 @@
const c = @import("../c.zig");
const std = @import("std");

5
src/c.zig Normal file
View file

@ -0,0 +1,5 @@
pub usingnamespace @cImport({
@cDefine("GLFW_INCLUDE_NONE", {});
@cInclude("vulkan/vulkan.h");
@cInclude("GLFW/glfw3.h");
});

45
src/main.zig Normal file
View file

@ -0,0 +1,45 @@
const std = @import("std");
const c = @import("c.zig");
const window = @import("render/window.zig");
const config = @import("config");
const Renderer = @import("render/renderer_vulkan.zig");
const math = @import("math.zig");
const Parser = @import("vm/parse.zig");
const vm = @import("vm/vm.zig");
const wasm = @import("vm/wasm.zig");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
{
var global_runtime = wasm.GlobalRuntime.init(allocator);
defer global_runtime.deinit();
try global_runtime.addFunction("debug", wasm.debug);
const file = try std.fs.cwd().openFile("assets/core.wasm", .{});
const module = try Parser.parseWasm(allocator, file.reader());
var runtime = try vm.Runtime.init(allocator, module, &global_runtime);
defer runtime.deinit(allocator);
var parameters = [_]usize{};
try runtime.callExternal(allocator, "fibonacci", &parameters);
const w = try window.Window.create(800, 600, "sideros");
defer w.destroy();
// TODO: Renderer.destroy should not return an error?
var r = try Renderer.create(allocator, w);
defer r.destroy() catch {};
while (!w.shouldClose()) {
c.glfwPollEvents();
try r.tick();
}
try r.device.waitIdle();
}
if (gpa.detectLeaks()) {
return error.leaked_memory;
}
}

64
src/math.zig Normal file
View file

@ -0,0 +1,64 @@
const std = @import("std");
pub const tan = std.math.tan;
pub const cos = std.math.cos;
pub const sin = std.math.sin;
pub const rad = std.math.degreesToRadians;
pub const Matrix = struct {
rows: [4]@Vector(4, f32),
pub fn lookAt(eye: @Vector(3, f32), target: @Vector(3, f32), arbitrary_up: @Vector(3, f32)) Matrix {
const forward = normalize(eye - target);
const right = normalize(cross(arbitrary_up, forward));
const up = cross(forward, right);
const view = [_]@Vector(4, f32){
@Vector(4, f32){ right[0], right[1], right[2], 0.0 },
@Vector(4, f32){ up[0], up[1], up[2], 0.0 },
@Vector(4, f32){ forward[0], forward[1], forward[2], 0.0 },
@Vector(4, f32){ 0.0, 0.0, 1.0, eye[2] },
};
return Matrix{
.rows = view,
};
}
pub fn perspective(fov: f32, aspect: f32, near: f32, far: f32) Matrix {
const projection = [_]@Vector(4, f32){
@Vector(4, f32){ 1.0 / (aspect * tan(fov / 2.0)), 0.0, 0.0, 0.0 },
@Vector(4, f32){ 0.0, 1.0 / tan(fov / 2.0), 0.0, 0.0 },
@Vector(4, f32){ 0.0, 0.0, -((far + near) / (far - near)), -((2 * far * near) / (far - near)) },
@Vector(4, f32){ 0.0, 0.0, -1.0, 1.0 },
};
return Matrix{
.rows = projection,
};
}
pub fn identity() Matrix {
const view = [_]@Vector(4, f32){
@Vector(4, f32){ 1.0, 0.0, 0.0, 0.0 },
@Vector(4, f32){ 0.0, 1.0, 0.0, 0.0 },
@Vector(4, f32){ 0.0, 0.0, 1.0, 0.0 },
@Vector(4, f32){ 0.0, 0.0, 0.0, 1.0 },
};
return Matrix{
.rows = view,
};
}
};
pub fn dot(a: @Vector(3, f32), b: @Vector(3, f32)) f32 {
return @reduce(.Add, a * b);
}
pub fn cross(a: @Vector(3, f32), b: @Vector(3, f32)) @Vector(3, f32) {
return @Vector(3, f32){ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] };
}
pub fn normalize(a: @Vector(3, f32)) @Vector(3, f32) {
return a / @as(@Vector(3, f32), @splat(@sqrt(dot(a, a))));
}

119
src/render/mesh.zig Normal file
View file

@ -0,0 +1,119 @@
const c = @import("../c.zig");
const std = @import("std");
const vk = @import("vulkan.zig");
const Allocator = std.mem.Allocator;
pub const Vertex = struct {
position: [3]f32,
pub fn create(x: f32, y: f32, z: f32) Vertex {
return Vertex{
.position = .{ x, y, z },
};
}
pub fn bindingDescription() c.VkVertexInputBindingDescription {
const binding_description: c.VkVertexInputBindingDescription = .{
.binding = 0,
.stride = @sizeOf(Vertex),
.inputRate = c.VK_VERTEX_INPUT_RATE_VERTEX,
};
return binding_description;
}
pub fn attributeDescription() c.VkVertexInputAttributeDescription {
const attribute_description: c.VkVertexInputAttributeDescription = .{
.location = 0,
.binding = 0,
.format = c.VK_FORMAT_R32G32B32_SFLOAT,
.offset = 0,
};
return attribute_description;
}
};
pub const Mesh = struct {
vertex_buffer: vk.Buffer,
index_buffer: vk.Buffer,
pub fn createVertexBuffer(device: anytype) !vk.Buffer {
const vertices = [_]Vertex{
Vertex.create(0.5, -0.5, 0.0),
Vertex.create(0.5, 0.5, 0.0),
Vertex.create(-0.5, 0.5, 0.0),
Vertex.create(-0.5, -0.5, 0.0),
};
var data: [*c]?*anyopaque = null;
const buffer = try device.createBuffer(vk.BufferUsage{ .transfer_src = true }, vk.BufferFlags{ .host_visible = true, .host_coherent = true }, @sizeOf(Vertex) * vertices.len);
try vk.mapError(c.vkMapMemory(
device.handle,
buffer.memory,
0,
buffer.size,
0,
@ptrCast(&data),
));
if (data) |ptr| {
const gpu_vertices: [*]Vertex = @ptrCast(@alignCast(ptr));
@memcpy(gpu_vertices, vertices[0..]);
}
c.vkUnmapMemory(device.handle, buffer.memory);
const vertex_buffer = try device.createBuffer(vk.BufferUsage{ .vertex_buffer = true, .transfer_dst = true }, vk.BufferFlags{ .device_local = true }, @sizeOf(Vertex) * vertices.len);
try buffer.copyTo(device, vertex_buffer);
buffer.destroy(device.handle);
return vertex_buffer;
}
pub fn createIndexBuffer(device: anytype) !vk.Buffer {
const indices = [_]u16{ 0, 1, 2, 3, 0, 2 };
var data: [*c]?*anyopaque = null;
const buffer = try device.createBuffer(vk.BufferUsage{ .transfer_src = true }, vk.BufferFlags{ .host_visible = true, .host_coherent = true }, @sizeOf(u16) * indices.len);
try vk.mapError(c.vkMapMemory(
device.handle,
buffer.memory,
0,
buffer.size,
0,
@ptrCast(&data),
));
if (data) |ptr| {
const gpu_indices: [*]u16 = @ptrCast(@alignCast(ptr));
@memcpy(gpu_indices, indices[0..]);
}
c.vkUnmapMemory(device.handle, buffer.memory);
const index_buffer = try device.createBuffer(vk.BufferUsage{ .index_buffer = true, .transfer_dst = true }, vk.BufferFlags{ .device_local = true }, @sizeOf(u16) * indices.len);
try buffer.copyTo(device, index_buffer);
buffer.destroy(device.handle);
return index_buffer;
}
pub fn create(device: anytype) !Mesh {
const vertex_buffer = try Mesh.createVertexBuffer(device);
const index_buffer = try Mesh.createIndexBuffer(device);
return Mesh{
.vertex_buffer = vertex_buffer,
.index_buffer = index_buffer,
};
}
};

View file

@ -0,0 +1,94 @@
const c = @import("../c.zig");
const std = @import("std");
const vk = @import("vulkan.zig");
const window = @import("window.zig");
const mesh = @import("mesh.zig");
const Allocator = std.mem.Allocator;
const Renderer = @This();
instance: vk.Instance,
surface: vk.Surface,
physical_device: vk.PhysicalDevice,
device: vk.Device(2),
render_pass: vk.RenderPass(2),
swapchain: vk.Swapchain(2),
graphics_pipeline: vk.GraphicsPipeline(2),
current_frame: u32,
vertex_buffer: vk.Buffer,
index_buffer: vk.Buffer,
pub fn create(allocator: Allocator, w: window.Window) !Renderer {
const instance = try vk.Instance.create();
const surface = try vk.Surface.create(instance, w);
var physical_device = try vk.PhysicalDevice.pick(allocator, instance);
const device = try physical_device.create_device(surface, allocator, 2);
const vertex_shader = try device.createShader("shader_vert");
defer device.destroyShader(vertex_shader);
const fragment_shader = try device.createShader("shader_frag");
defer device.destroyShader(fragment_shader);
const render_pass = try vk.RenderPass(2).create(allocator, device, surface, physical_device);
const swapchain = try vk.Swapchain(2).create(allocator, surface, device, physical_device, w, render_pass);
const graphics_pipeline = try vk.GraphicsPipeline(2).create(device, swapchain, render_pass, vertex_shader, fragment_shader);
// TODO: I think the renderer shouldn't have to interact with buffers. I think the API should change to
// something along the lines of
// renderer.begin()
// renderer.render(triangle);
// renderer.render(some_other_thing);
// ...
// renderer.submit()
const triangle = try mesh.Mesh.create(device);
return Renderer{
.instance = instance,
.surface = surface,
.physical_device = physical_device,
.device = device,
.render_pass = render_pass,
.swapchain = swapchain,
.graphics_pipeline = graphics_pipeline,
.current_frame = 0,
// TODO: Why are we storing the buffer and not the Mesh?
.vertex_buffer = triangle.vertex_buffer,
.index_buffer = triangle.index_buffer,
};
}
pub fn destroy(self: Renderer) !void {
try self.device.waitIdle();
self.index_buffer.destroy(self.device.handle);
self.vertex_buffer.destroy(self.device.handle);
self.graphics_pipeline.destroy(self.device);
self.swapchain.destroy(self.device);
self.render_pass.destroy(self.device);
self.device.destroy();
self.surface.destroy(self.instance);
self.instance.destroy();
}
// TODO: tick is maybe a bad name? something like present() or submit() is better?
pub fn tick(self: *Renderer) !void {
try self.device.waitFence(self.current_frame);
const image = try self.swapchain.nextImage(self.device, self.current_frame);
try self.device.resetCommand(self.current_frame);
try self.device.beginCommand(self.current_frame);
self.render_pass.begin(self.swapchain, self.device, image, self.current_frame);
self.graphics_pipeline.bind(self.device, self.current_frame);
self.device.bindVertexBuffer(self.vertex_buffer, self.current_frame);
self.device.bindIndexBuffer(self.index_buffer, self.current_frame);
self.device.bindDescriptorSets(self.graphics_pipeline, self.current_frame);
self.device.draw(@intCast(self.index_buffer.size / @sizeOf(u16)), self.current_frame);
self.render_pass.end(self.device, self.current_frame);
try self.device.endCommand(self.current_frame);
try self.device.submit(self.swapchain, image, self.current_frame);
self.current_frame = (self.current_frame + 1) % 2;
}

1097
src/render/vulkan.zig Normal file

File diff suppressed because it is too large Load diff

64
src/render/window.zig Normal file
View file

@ -0,0 +1,64 @@
const c = @import("../c.zig");
const std = @import("std");
pub const Error = error{
platform_unavailable,
platform_error,
};
pub fn getExtensions() [][*c]const u8 {
var extension_count: u32 = undefined;
const raw: [*c][*c]const u8 = c.glfwGetRequiredInstanceExtensions(&extension_count);
const extensions = raw[0..extension_count];
return extensions;
}
pub const Window = struct {
title: []const u8,
width: usize,
height: usize,
raw: *c.GLFWwindow,
pub fn create(width: usize, height: usize, title: []const u8) !Window {
if (c.glfwInit() != c.GLFW_TRUE) {
const status = c.glfwGetError(null);
return switch (status) {
c.GLFW_PLATFORM_UNAVAILABLE => Error.platform_unavailable,
c.GLFW_PLATFORM_ERROR => Error.platform_error,
else => unreachable,
};
}
c.glfwWindowHint(c.GLFW_RESIZABLE, c.GLFW_FALSE);
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
const raw = c.glfwCreateWindow(@intCast(width), @intCast(height), title.ptr, null, null);
c.glfwShowWindow(raw);
return Window{
.title = title,
.width = width,
.height = height,
.raw = raw.?,
};
}
pub fn shouldClose(self: Window) bool {
return c.glfwWindowShouldClose(self.raw) == c.GLFW_TRUE;
}
pub fn size(self: Window) struct { usize, usize } {
var width: u32 = undefined;
var height: u32 = undefined;
c.glfwGetFramebufferSize(self.raw, @ptrCast(&width), @ptrCast(&height));
return .{ @intCast(width), @intCast(height) };
}
pub fn destroy(self: Window) void {
c.glfwDestroyWindow(self.raw);
c.glfwTerminate();
}
};

288
src/vm/parse.zig Normal file
View file

@ -0,0 +1,288 @@
const std = @import("std");
const wasm = @import("wasm.zig");
const Allocator = std.mem.Allocator;
pub const Error = error{
malformed_wasm,
invalid_utf8,
};
pub const Module = struct {
types: []FunctionType,
imports: std.ArrayList(Import),
exports: std.StringHashMap(u32),
functions: []u32,
memory: Memory,
code: []FunctionBody,
funcs: std.ArrayList(Function),
pub fn deinit(self: *Module, allocator: Allocator) void {
for (self.types) |t| {
t.deinit(allocator);
}
allocator.free(self.types);
for (self.imports.items) |i| {
i.deinit(allocator);
}
self.imports.deinit();
var iter = self.exports.iterator();
while (iter.next()) |entry| {
allocator.free(entry.key_ptr.*);
}
self.exports.deinit();
allocator.free(self.functions);
for (self.code) |f| {
for (f.locals) |l| {
allocator.free(l.types);
}
allocator.free(f.code);
}
allocator.free(self.code);
self.funcs.deinit();
}
};
pub const FunctionScope = enum {
external,
internal,
};
pub const Function = union(FunctionScope) {
external: u8,
internal: u8,
};
// TODO: refactor locals
pub const Local = struct {
types: []u8,
};
pub const FunctionBody = struct {
locals: []Local,
code: []u8,
};
pub const Memory = struct {
initial: u32,
max: u32,
};
pub const FunctionType = struct {
parameters: []u8,
results: []u8,
pub fn deinit(self: FunctionType, allocator: Allocator) void {
allocator.free(self.parameters);
allocator.free(self.results);
}
};
pub const Import = struct {
name: []u8,
module: []u8,
signature: u32,
pub fn deinit(self: Import, allocator: Allocator) void {
allocator.free(self.name);
allocator.free(self.module);
}
};
pub fn parseType(t: u8) wasm.Type {
return @enumFromInt(t);
}
pub fn parseName(allocator: Allocator, stream: anytype) ![]u8 {
const size = try std.leb.readULEB128(u32, stream);
const str = try allocator.alloc(u8, size);
if (try stream.read(str) != size) {
// TODO: better error
return Error.malformed_wasm;
}
if (!std.unicode.utf8ValidateSlice(str)) return Error.invalid_utf8;
return str;
}
// TODO: parse Global Section
// TODO: Consider Arena allocator
pub fn parseWasm(allocator: Allocator, stream: anytype) !Module {
var types: []FunctionType = undefined;
var imports = std.ArrayList(Import).init(allocator);
var exports = std.StringHashMap(u32).init(allocator);
var funcs = std.ArrayList(Function).init(allocator);
var functions: []u32 = undefined;
var memory: Memory = undefined;
var code: []FunctionBody = undefined;
// Parse magic
if (!(try stream.isBytes(&[_]u8{ 0x00, 0x61, 0x73, 0x6d }))) return Error.malformed_wasm;
// Parse version
if (!(try stream.isBytes(&[_]u8{ 0x01, 0x00, 0x00, 0x00 }))) return Error.malformed_wasm;
// NOTE: This ensures that (in this block) illegal behavior is safety-checked.
// This slows down the code but since this function is only called at the start
// I believe it is better to take the ``hit'' in performance (should only be @enumFromInt)
// rather than having undefined behavior when user provides an invalid wasm file.
@setRuntimeSafety(true);
loop: while (stream.readByte()) |byte| {
const section_size = try std.leb.readULEB128(u32, stream);
switch (@as(std.wasm.Section, @enumFromInt(byte))) {
std.wasm.Section.custom => {
// TODO: unimplemented
break :loop;
},
std.wasm.Section.type => {
const type_count = try std.leb.readULEB128(u32, stream);
types = try allocator.alloc(FunctionType, type_count);
for (types) |*t| {
if (!(try stream.isBytes(&.{0x60}))) return Error.malformed_wasm;
const params_count = try std.leb.readULEB128(u32, stream);
t.parameters = try allocator.alloc(u8, params_count);
if (try stream.read(t.parameters) != params_count) {
// TODO: better errors
return Error.malformed_wasm;
}
const results = try std.leb.readULEB128(u32, stream);
t.results = try allocator.alloc(u8, results);
if (try stream.read(t.results) != results) {
// TODO: better errors
return Error.malformed_wasm;
}
}
},
std.wasm.Section.import => {
// Can there be more than one import section?
const import_count = try std.leb.readULEB128(u32, stream);
for (0..import_count) |i| {
const mod = try parseName(allocator, stream);
const nm = try parseName(allocator, stream);
const b = try stream.readByte();
switch (@as(std.wasm.ExternalKind, @enumFromInt(b))) {
std.wasm.ExternalKind.function => try funcs.append(.{ .external = @intCast(i) }),
// TODO: not implemented
std.wasm.ExternalKind.table => {},
std.wasm.ExternalKind.memory => {},
std.wasm.ExternalKind.global => {},
}
const idx = try std.leb.readULEB128(u32, stream);
try imports.append(.{
.module = mod,
.name = nm,
.signature = idx,
});
}
},
std.wasm.Section.function => {
const function_count = try std.leb.readULEB128(u32, stream);
functions = try allocator.alloc(u32, function_count);
for (functions) |*f| {
f.* = try std.leb.readULEB128(u32, stream);
}
},
std.wasm.Section.table => {
// TODO: not implemented
try stream.skipBytes(section_size, .{});
},
std.wasm.Section.memory => {
const memory_count = try std.leb.readULEB128(u32, stream);
for (0..memory_count) |_| {
const b = try stream.readByte();
const n = try std.leb.readULEB128(u32, stream);
var m: u32 = 0;
switch (b) {
0x00 => {},
0x01 => m = try std.leb.readULEB128(u32, stream),
else => return Error.malformed_wasm,
}
// TODO: support multiple memories
memory = .{
.initial = n,
.max = m,
};
}
},
std.wasm.Section.global => {
// TODO: unimplemented
try stream.skipBytes(section_size, .{});
},
// TODO: Can there be more than one export section? Otherwise we can optimize allocations
std.wasm.Section.@"export" => {
const export_count = try std.leb.readULEB128(u32, stream);
for (0..export_count) |_| {
const nm = try parseName(allocator, stream);
const b = try stream.readByte();
const idx = try std.leb.readULEB128(u32, stream);
switch (@as(std.wasm.ExternalKind, @enumFromInt(b))) {
std.wasm.ExternalKind.function => try exports.put(nm, idx),
// TODO: unimplemented,
std.wasm.ExternalKind.table => allocator.free(nm),
std.wasm.ExternalKind.memory => allocator.free(nm),
std.wasm.ExternalKind.global => allocator.free(nm),
}
}
},
std.wasm.Section.start => {
// TODO: unimplemented
try stream.skipBytes(section_size, .{});
},
std.wasm.Section.element => {
// TODO: unimplemented
try stream.skipBytes(section_size, .{});
},
std.wasm.Section.code => {
const code_count = try std.leb.readULEB128(u32, stream);
code = try allocator.alloc(FunctionBody, code_count);
for (0..code_count) |i| {
const code_size = try std.leb.readULEB128(u32, stream);
const local_count = try std.leb.readULEB128(u32, stream);
const locals = try allocator.alloc(Local, local_count);
for (locals) |*l| {
const n = try std.leb.readULEB128(u32, stream);
l.types = try allocator.alloc(u8, n);
@memset(l.types, try stream.readByte());
}
code[i].locals = locals;
// TODO: maybe is better to parse code into ast here and not do it every frame?
// FIXME: This calculation is plain wrong. Resolving above TODO should help
code[i].code = try allocator.alloc(u8, code_size - local_count - 1);
// TODO: better error reporting
if (try stream.read(code[i].code) != code_size - local_count - 1) return Error.malformed_wasm;
const f = Function{ .internal = @intCast(i) };
try funcs.append(f);
}
},
std.wasm.Section.data => {
// TODO: unimplemented
try stream.skipBytes(section_size, .{});
},
std.wasm.Section.data_count => {
// TODO: unimplemented
try stream.skipBytes(section_size, .{});
},
else => return Error.malformed_wasm,
}
} else |err| switch (err) {
error.EndOfStream => {},
else => return err,
}
return Module{
.types = types,
.imports = imports,
.functions = functions,
.memory = memory,
.exports = exports,
.code = code,
.funcs = funcs,
};
}

559
src/vm/vm.zig Normal file
View file

@ -0,0 +1,559 @@
const std = @import("std");
const wasm = @import("wasm.zig");
const Parser = @import("parse.zig");
const Allocator = std.mem.Allocator;
const AllocationError = error{OutOfMemory};
pub fn leb128Decode(comptime T: type, bytes: []u8) struct { len: usize, val: T } {
switch (@typeInfo(T)) {
.int => {},
else => @compileError("LEB128 integer decoding only support integers, but got " ++ @typeName(T)),
}
if (@typeInfo(T).int.bits != 32 and @typeInfo(T).int.bits != 64) {
@compileError("LEB128 integer decoding only supports 32 or 64 bits integers but got " ++ std.fmt.comptimePrint("{d} bits", .{@typeInfo(T).int.bits}));
}
var result: T = 0;
// TODO: is the type of shift important. Reading Wikipedia (not very much tho) it seems like we can use u32 and call it a day...
var shift: if (@typeInfo(T).int.bits == 32) u5 else u6 = 0;
var byte: u8 = undefined;
var len: usize = 0;
for (bytes) |b| {
len += 1;
result |= @as(T, @intCast((b & 0x7f))) << shift;
if ((b & (0x1 << 7)) == 0) {
byte = b;
break;
}
shift += 7;
}
if (@typeInfo(T).int.signedness == .signed) {
const size = @sizeOf(T) * 8;
if (shift < size and (byte & 0x40) != 0) {
result |= (~@as(T, 0) << shift);
}
}
return .{ .len = len, .val = result };
}
pub fn decodeLittleEndian(comptime T: type, bytes: []u8) T {
if (T != i32 and T != i64) {
return @as(T, 0);
} else {
var value = @as(T, 0);
for (0..@sizeOf(T)) |b| {
value |= ((bytes[b]) << @intCast((@sizeOf(T) - b - 1)));
}
return value;
}
}
pub fn encodeLittleEndian(comptime T: type, bytes: *[]u8, value: T) void {
for (0..@sizeOf(T)) |b| {
bytes.*[b] = @intCast(((value >> @intCast((@sizeOf(T) - b - 1))) & 0xff));
}
}
pub const CallFrame = struct {
program_counter: usize,
code: []u8,
locals: []Value,
};
const ValueType = enum {
i32,
i64,
};
pub const Value = union(ValueType) {
i32: i32,
i64: i64,
};
pub const Runtime = struct {
module: Parser.Module,
stack: std.ArrayList(Value),
call_stack: std.ArrayList(CallFrame),
memory: []u8,
global_runtime: *wasm.GlobalRuntime,
labels: std.ArrayList(usize),
pub fn init(allocator: Allocator, module: Parser.Module, global_runtime: *wasm.GlobalRuntime) !Runtime {
const memory = try allocator.alloc(u8, module.memory.max);
return Runtime{
.module = module,
.stack = try std.ArrayList(Value).initCapacity(allocator, 10),
.call_stack = try std.ArrayList(CallFrame).initCapacity(allocator, 5),
.labels = try std.ArrayList(usize).initCapacity(allocator, 2),
.memory = memory,
.global_runtime = global_runtime,
};
}
pub fn deinit(self: *Runtime, allocator: Allocator) void {
self.module.deinit(allocator);
self.stack.deinit();
self.labels.deinit();
self.call_stack.deinit();
allocator.free(self.memory);
}
pub fn executeFrame(self: *Runtime, allocator: Allocator, frame: *CallFrame) !void {
var for_loop = false;
loop: while (true) {
const byte: u8 = frame.code[frame.program_counter];
frame.program_counter += 1;
std.debug.print("b: {x}\n", .{byte});
switch (byte) {
0x03 => {
try self.labels.append(frame.program_counter);
frame.program_counter += 1;
//const a = frame.code[frame.program_counter];
for_loop = true;
},
0x0d => {
const label = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += label.len;
var address = @as(usize, 0);
for (0..(label.val)) |_| {
address = self.labels.pop().?;
}
if (self.stack.pop().?.i32 != 0) {
frame.program_counter = address;
}
},
0x20 => {
const integer = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += integer.len;
try self.stack.append(frame.locals[integer.val]);
},
0x21 => {
const integer = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += integer.len;
frame.locals[integer.val] = self.stack.pop().?;
},
0x22 => {
const integer = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += integer.len;
frame.locals[integer.val] = self.stack.pop().?;
try self.stack.append(Value{ .i32 = @intCast(integer.val) });
},
0x28 => {
const address = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += address.len;
const offset = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += offset.len;
const start = (address.val + offset.val);
const end = start + @sizeOf(u32);
try self.stack.append(Value{ .i32 = decodeLittleEndian(i32, self.memory[start..end]) });
},
0x29 => {
const address = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += address.len;
const offset = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += offset.len;
const start = (address.val + offset.val);
const end = start + @sizeOf(u64);
try self.stack.append(Value{ .i64 = decodeLittleEndian(i64, self.memory[start..end]) });
},
0x36 => {
const address = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += address.len;
const offset = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += offset.len;
const start = (address.val + offset.val);
const end = start + @sizeOf(u32);
try self.stack.append(Value{ .i32 = decodeLittleEndian(i32, self.memory[start..end]) });
},
0x37 => {
const address = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += address.len;
const offset = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += offset.len;
const start = (address.val + offset.val);
const end = start + @sizeOf(u32);
encodeLittleEndian(i32, @constCast(&self.memory[start..end]), self.stack.pop().?.i32);
},
0x38 => {
const address = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += address.len;
const offset = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += offset.len;
const start = (address.val + offset.val);
const end = start + @sizeOf(u64);
encodeLittleEndian(i64, @constCast(&self.memory[start..end]), self.stack.pop().?.i64);
},
0x41 => {
const integer = leb128Decode(i32, frame.code[frame.program_counter..]);
frame.program_counter += integer.len;
try self.stack.append(Value{ .i32 = integer.val });
},
0x42 => {
const integer = leb128Decode(i64, frame.code[frame.program_counter..]);
frame.program_counter += integer.len;
try self.stack.append(Value{ .i64 = integer.val });
},
0x45 => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 == 0))) });
},
0x46 => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 == self.stack.pop().?.i32))) });
},
0x47 => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 != self.stack.pop().?.i32))) });
},
0x48 => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 < self.stack.pop().?.i32))) });
},
0x49 => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) < @as(u32, @bitCast(self.stack.pop().?.i32))))) });
},
0x4a => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 > self.stack.pop().?.i32))) });
},
0x4b => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) > @as(u32, @bitCast(self.stack.pop().?.i32))))) });
},
0x4c => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 <= self.stack.pop().?.i32))) });
},
0x4d => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) <= @as(u32, @bitCast(self.stack.pop().?.i32))))) });
},
0x4e => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i32 >= self.stack.pop().?.i32))) });
},
0x4f => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u32, @bitCast(self.stack.pop().?.i32)) >= @as(u32, @bitCast(self.stack.pop().?.i32))))) });
},
0x50 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 == 0))) });
},
0x51 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 == self.stack.pop().?.i64))) });
},
0x52 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 != self.stack.pop().?.i64))) });
},
0x53 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 < self.stack.pop().?.i64))) });
},
0x54 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) < @as(u64, @bitCast(self.stack.pop().?.i64))))) });
},
0x55 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 > self.stack.pop().?.i64))) });
},
0x56 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) > @as(u64, @bitCast(self.stack.pop().?.i64))))) });
},
0x57 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 <= self.stack.pop().?.i64))) });
},
0x58 => {
try self.stack.append(Value{ .i64 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) <= @as(u64, @bitCast(self.stack.pop().?.i64))))) });
},
0x59 => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(self.stack.pop().?.i64 >= self.stack.pop().?.i64))) });
},
0x5a => {
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(@as(u64, @bitCast(self.stack.pop().?.i64)) >= @as(u64, @bitCast(self.stack.pop().?.i64))))) });
},
0x67 => {
var i = @as(i32, 0);
const number = self.stack.pop().?.i32;
for (0..@sizeOf(i32)) |b| {
if (number & (@as(i32, 0x1) << @intCast((@sizeOf(i32) - b - 1))) == 1) {
break;
}
i += 1;
}
try self.stack.append(Value{ .i32 = i });
},
0x68 => {
var i = @as(i32, 0);
const number = self.stack.pop().?.i32;
for (0..@sizeOf(i32)) |b| {
if (number & (@as(i32, 0x1) << @intCast(b)) == 1) {
break;
}
i += 1;
}
try self.stack.append(Value{ .i32 = i });
},
0x69 => {
var i = @as(i32, 0);
const number = self.stack.pop().?.i32;
for (0..@sizeOf(i32)) |b| {
if (number & (@as(i32, 0x1) << @intCast(b)) == 1) {
i += 1;
}
}
try self.stack.append(Value{ .i32 = i });
},
0x6a => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 + b.i32 });
},
0x6b => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 - b.i32 });
},
0x6c => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 * b.i32 });
},
0x6d => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = @divTrunc(a.i32, b.i32) });
},
0x6e => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) / @as(u32, @bitCast(b.i32)))) });
},
0x6f => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = @rem(a.i32, b.i32) });
},
0x70 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) % @as(u32, @bitCast(b.i32)))) });
},
0x71 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 & b.i32 });
},
0x72 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 | b.i32 });
},
0x73 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 ^ b.i32 });
},
0x74 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 << @intCast(b.i32) });
},
0x75 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = a.i32 >> @intCast(b.i32) });
},
0x76 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = @as(i32, @bitCast(@as(u32, @bitCast(a.i32)) >> @intCast(@as(u32, @bitCast(b.i32))))) });
},
0x77 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = (a.i32 << @intCast(@as(u32, @bitCast(b.i32)))) | (a.i32 >> @intCast((@sizeOf(u32) * 8 - b.i32))) });
},
0x78 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i32 = (a.i32 >> @intCast(@as(u32, @bitCast(b.i32)))) | (a.i32 << @intCast((@sizeOf(u32) * 8 - b.i32))) });
},
0x79 => {
var i = @as(i64, 0);
const number = self.stack.pop().?.i64;
for (0..@sizeOf(i64)) |b| {
if (number & (@as(i64, 0x1) << @intCast((@sizeOf(i64) - b - 1))) == 1) {
break;
}
i += 1;
}
try self.stack.append(Value{ .i64 = i });
},
0x7a => {
var i = @as(i64, 0);
const number = self.stack.pop().?.i64;
for (0..@sizeOf(i64)) |b| {
if (number & (@as(i64, 0x1) << @intCast(b)) == 1) {
break;
}
i += 1;
}
try self.stack.append(Value{ .i64 = i });
},
0x7b => {
var i = @as(i64, 0);
const number = self.stack.pop().?.i64;
for (0..@sizeOf(i64)) |b| {
if (number & (@as(i64, 0x1) << @intCast(b)) == 1) {
i += 1;
}
}
try self.stack.append(Value{ .i64 = i });
},
0x7c => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 + b.i64 });
},
0x7d => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 - b.i64 });
},
0x7e => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 * b.i64 });
},
0x7f => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = @divTrunc(a.i64, b.i64) });
},
0x80 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) / @as(u64, @bitCast(b.i64)))) });
},
0x81 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = @rem(a.i64, b.i64) });
},
0x82 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) % @as(u64, @bitCast(b.i64)))) });
},
0x83 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 & b.i64 });
},
0x84 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 | b.i64 });
},
0x85 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 ^ b.i64 });
},
0x86 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 << @intCast(b.i64) });
},
0x87 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = a.i64 >> @intCast(b.i64) });
},
0x88 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = @as(i64, @bitCast(@as(u64, @bitCast(a.i64)) >> @intCast(@as(u64, @bitCast(b.i64))))) });
},
0x89 => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = (a.i64 << @intCast(@as(u64, @bitCast(b.i64)))) | (a.i64 >> @intCast((@sizeOf(u64) * 8 - b.i64))) });
},
0x8a => {
const a = self.stack.pop().?;
const b = self.stack.pop().?;
try self.stack.append(.{ .i64 = (a.i64 >> @intCast(@as(u64, @bitCast(b.i64)))) | (a.i64 << @intCast((@sizeOf(u64) * 8 - b.i64))) });
},
0x10 => {
const integer = leb128Decode(u32, frame.code[frame.program_counter..]);
frame.program_counter += integer.len;
self.call(allocator, integer.val, &[_]usize{}) catch {};
},
0xb => {
if (for_loop) {
frame.program_counter += 1;
} else {
break :loop;
}
},
else => std.debug.print("instruction {} not implemented\n", .{byte}),
}
}
}
pub fn callExternal(self: *Runtime, allocator: Allocator, name: []const u8, parameters: []usize) !void {
if (self.module.exports.get(name)) |function| {
try self.call(allocator, function, parameters);
}
}
pub fn call(self: *Runtime, allocator: Allocator, function: usize, parameters: []usize) AllocationError!void {
const f = self.module.funcs.items[function];
switch (f) {
.internal => {
const function_type = self.module.types[self.module.functions[f.internal]];
var frame = CallFrame{
.code = self.module.code[f.internal].code,
.program_counter = 0x0,
.locals = try allocator.alloc(Value, self.module.code[f.internal].locals.len + function_type.parameters.len),
};
for (parameters, 0..) |p, i| {
switch (Parser.parseType(function_type.parameters[i])) {
.i32 => {
frame.locals[i] = .{ .i32 = @intCast(p) };
},
.i64 => {
frame.locals[i] = .{ .i64 = @intCast(p) };
},
else => unreachable,
}
}
for (self.module.code[f.internal].locals, function_type.parameters.len..) |local, i| {
switch (Parser.parseType(local.types[0])) {
.i32 => {
frame.locals[i] = .{ .i32 = 0 };
},
.i64 => {
frame.locals[i] = .{ .i64 = 0 };
},
else => unreachable,
}
}
try self.executeFrame(allocator, &frame);
allocator.free(frame.locals);
},
.external => {
const name = self.module.imports.items[f.external].name;
if (self.global_runtime.functions.get(name)) |external| {
external(&self.stack);
}
},
}
}
};

35
src/vm/wasm.zig Normal file
View file

@ -0,0 +1,35 @@
const vm = @import("vm.zig");
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const Type = enum(u8) {
i32 = 0x7f,
i64 = 0x7e,
f32 = 0x7d,
f64 = 0x7c,
v128 = 0x7b,
};
pub const GlobalRuntime = struct {
functions: std.StringHashMap(*const fn (stack: *std.ArrayList(vm.Value)) void),
pub fn init(allocator: Allocator) GlobalRuntime {
return GlobalRuntime{
.functions = std.StringHashMap(*const fn (stack: *std.ArrayList(vm.Value)) void).init(allocator),
};
}
pub fn deinit(self: *GlobalRuntime) void {
self.functions.deinit();
}
pub fn addFunction(self: *GlobalRuntime, name: []const u8, function: *const fn (stack: *std.ArrayList(vm.Value)) void) !void {
try self.functions.put(name, function);
}
};
pub fn debug(stack: *std.ArrayList(vm.Value)) void {
const a = stack.pop().?;
std.debug.print("{}\n", .{a.i32});
}