aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/wasm.zig
diff options
context:
space:
mode:
authorjoachimschmidt557 <joachim.schmidt557@outlook.com>2021-05-09 15:20:08 +0800
committerAndrew Kelley <andrew@ziglang.org>2021-05-22 21:15:25 -0400
commite8236551abdb7bb74811e5e9dc9890cb2abbd269 (patch)
treea7cdaddfb3e7b5cd3f760b682e6ded775a68bfc5 /src/codegen/wasm.zig
parent1c636e2564e2fc2e8e4b6b1edbc782592ee3d2d7 (diff)
downloadzig-e8236551abdb7bb74811e5e9dc9890cb2abbd269.tar.gz
zig-e8236551abdb7bb74811e5e9dc9890cb2abbd269.zip
stage2: Move BlockData out of ir.Inst.Block
Diffstat (limited to 'src/codegen/wasm.zig')
-rw-r--r--src/codegen/wasm.zig22
1 files changed, 8 insertions, 14 deletions
diff --git a/src/codegen/wasm.zig b/src/codegen/wasm.zig
index 40be7bc618..36b131e9b1 100644
--- a/src/codegen/wasm.zig
+++ b/src/codegen/wasm.zig
@@ -14,7 +14,6 @@ const Inst = ir.Inst;
const Type = @import("../type.zig").Type;
const Value = @import("../value.zig").Value;
const Compilation = @import("../Compilation.zig");
-const AnyMCValue = @import("../codegen.zig").AnyMCValue;
const LazySrcLoc = Module.LazySrcLoc;
const link = @import("../link.zig");
const TypedValue = @import("../TypedValue.zig");
@@ -29,8 +28,6 @@ const WValue = union(enum) {
constant: *Inst,
/// Offset position in the list of bytecode instructions
code_offset: usize,
- /// The label of the block, used by breaks to find its relative distance
- block_idx: u32,
/// Used for variables that create multiple locals on the stack when allocated
/// such as structs and optionals.
multi_value: u32,
@@ -492,6 +489,8 @@ pub const Context = struct {
gpa: *mem.Allocator,
/// Table to save `WValue`'s generated by an `Inst`
values: ValueTable,
+ /// Mapping from *Inst.Block to block ids
+ blocks: std.AutoArrayHashMapUnmanaged(*Inst.Block, u32) = .{},
/// `bytes` contains the wasm bytecode belonging to the 'code' section.
code: ArrayList(u8),
/// Contains the generated function type bytecode for the current function
@@ -521,6 +520,7 @@ pub const Context = struct {
pub fn deinit(self: *Context) void {
self.values.deinit(self.gpa);
+ self.blocks.deinit(self.gpa);
self.locals.deinit(self.gpa);
self.* = undefined;
}
@@ -590,7 +590,6 @@ pub const Context = struct {
fn emitWValue(self: *Context, val: WValue) InnerError!void {
const writer = self.code.writer();
switch (val) {
- .block_idx => unreachable, // block_idx cannot be referenced
.multi_value => unreachable, // multi_value can never be written directly, and must be accessed individually
.none, .code_offset => {}, // no-op
.local => |idx| {
@@ -968,13 +967,9 @@ pub const Context = struct {
const block_ty = try self.genBlockType(block.base.src, block.base.ty);
try self.startBlock(.block, block_ty, null);
- block.codegen = .{
- // we don't use relocs, so using `relocs` is illegal behaviour.
- .relocs = undefined,
- // Here we set the current block idx, so breaks know the depth to jump
- // to when breaking out.
- .mcv = @bitCast(AnyMCValue, WValue{ .block_idx = self.block_depth }),
- };
+ // Here we set the current block idx, so breaks know the depth to jump
+ // to when breaking out.
+ try self.blocks.putNoClobber(self.gpa, block, self.block_depth);
try self.genBody(block.body);
try self.endBlock();
@@ -1091,10 +1086,9 @@ pub const Context = struct {
try self.emitWValue(operand);
}
- // every block contains a `WValue` with its block index.
+ // We map every block to its block index.
// We then determine how far we have to jump to it by substracting it from current block depth
- const wvalue = @bitCast(WValue, br.block.codegen.mcv);
- const idx: u32 = self.block_depth - wvalue.block_idx;
+ const idx: u32 = self.block_depth - self.blocks.get(br.block).?;
const writer = self.code.writer();
try writer.writeByte(wasm.opcode(.br));
try leb.writeULEB128(writer, idx);