Skip to content

Commit

Permalink
use fixed writer in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Dec 21, 2024
1 parent 655375f commit a037106
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/arch/wasm/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1193,9 +1193,9 @@ pub const Function = extern struct {
const locals = wasm.all_zcu_locals.items[f.locals_off..][0..f.locals_len];
try code.ensureUnusedCapacity(gpa, 5 + locals.len * 6 + 38);

std.leb.writeUleb128(code.writer(gpa), @as(u32, @intCast(locals.len))) catch unreachable;
std.leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(locals.len))) catch unreachable;
for (locals) |local| {
std.leb.writeUleb128(code.writer(gpa), @as(u32, 1)) catch unreachable;
std.leb.writeUleb128(code.fixedWriter(), @as(u32, 1)) catch unreachable;
code.appendAssumeCapacity(local);
}

Expand All @@ -1205,30 +1205,30 @@ pub const Function = extern struct {
const sp_global: Wasm.GlobalIndex = .stack_pointer;
// load stack pointer
code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_get));
std.leb.writeULEB128(code.writer(gpa), @intFromEnum(sp_global)) catch unreachable;
std.leb.writeULEB128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
// store stack pointer so we can restore it when we return from the function
code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));
leb.writeUleb128(code.writer(gpa), f.prologue.sp_local) catch unreachable;
leb.writeUleb128(code.fixedWriter(), f.prologue.sp_local) catch unreachable;
// get the total stack size
const aligned_stack: i32 = @intCast(stack_alignment.forward(f.prologue.stack_size));
code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
leb.writeIleb128(code.writer(gpa), aligned_stack) catch unreachable;
leb.writeIleb128(code.fixedWriter(), aligned_stack) catch unreachable;
// subtract it from the current stack pointer
code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_sub));
// Get negative stack alignment
const neg_stack_align = @as(i32, @intCast(align_bytes)) * -1;
code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
leb.writeIleb128(code.writer(gpa), neg_stack_align) catch unreachable;
leb.writeIleb128(code.fixedWriter(), neg_stack_align) catch unreachable;
// Bitwise-and the value to get the new stack pointer to ensure the
// pointers are aligned with the abi alignment.
code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_and));
// The bottom will be used to calculate all stack pointer offsets.
code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));
leb.writeUleb128(code.writer(gpa), f.prologue.bottom_stack_local) catch unreachable;
leb.writeUleb128(code.fixedWriter(), f.prologue.bottom_stack_local) catch unreachable;
// Store the current stack pointer value into the global stack pointer so other function calls will
// start from this value instead and not overwrite the current stack.
code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set));
std.leb.writeULEB128(code.writer(gpa), @intFromEnum(sp_global)) catch unreachable;
std.leb.writeULEB128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
}

var emit: Emit = .{
Expand Down

0 comments on commit a037106

Please sign in to comment.