| ... | ... | @@ -18,16 +18,16 @@ const WValue = union(enum) { |
| 18 | 18 | none: void, |
| 19 | 19 | /// Index of the local variable |
| 20 | 20 | local: u32, |
| 21 | | /// A constant instruction |
| 21 | /// Instruction holding a constant `Value` |
| 22 | 22 | constant: *Inst, |
| 23 | | /// Each newly created wasm block have a label |
| 24 | | /// in the form of an index. |
| 23 | /// Block label |
| 25 | 24 | block_idx: u32, |
| 26 | 25 | }; |
| 27 | 26 | |
| 28 | | pub const ValueTable = std.AutoArrayHashMap(*Inst, WValue); |
| 27 | /// Hashmap to store generated `WValue` for each `Inst` |
| 28 | pub const ValueTable = std.AutoHashMap(*Inst, WValue); |
| 29 | 29 | |
| 30 | | /// Using a given Zig type, returns the corresponding wasm value type |
| 30 | /// Using a given `Type`, returns the corresponding wasm value type |
| 31 | 31 | fn genValtype(ty: Type) ?u8 { |
| 32 | 32 | return switch (ty.tag()) { |
| 33 | 33 | .f32 => 0x7D, |
| ... | ... | @@ -40,24 +40,22 @@ fn genValtype(ty: Type) ?u8 { |
| 40 | 40 | |
| 41 | 41 | /// Code represents the `Code` section of wasm that |
| 42 | 42 | /// belongs to a function |
| 43 | | pub const Code = struct { |
| 43 | pub const Context = struct { |
| 44 | 44 | /// Reference to the function declaration the code |
| 45 | 45 | /// section belongs to |
| 46 | 46 | decl: *Decl, |
| 47 | 47 | gpa: *mem.Allocator, |
| 48 | 48 | /// Table to save `WValue`'s generated by an `Inst` |
| 49 | 49 | values: ValueTable, |
| 50 | | /// `bytes` contains the wasm instructions that have been emitted |
| 51 | | /// this is what will be emitted after codegen to write the wasm binary |
| 50 | /// `bytes` contains the wasm bytecode belonging to the 'code' section. |
| 52 | 51 | bytes: ArrayList(u8), |
| 53 | 52 | /// Contains the generated function type bytecode for the current function |
| 53 | /// found in `decl` |
| 54 | 54 | func_type_data: ArrayList(u8), |
| 55 | 55 | /// The index the next local generated will have |
| 56 | 56 | /// NOTE: arguments share the index with locals therefore the first variable |
| 57 | 57 | /// will have the index that comes after the last argument's index |
| 58 | 58 | local_index: u32 = 0, |
| 59 | | /// The index the next argument generated will have |
| 60 | | arg_index: u32 = 0, |
| 61 | 59 | /// If codegen fails, an error messages will be allocated and saved |
| 62 | 60 | /// in `err_msg` |
| 63 | 61 | err_msg: *Compilation.ErrorMsg, |
| ... | ... | @@ -67,14 +65,15 @@ pub const Code = struct { |
| 67 | 65 | CodegenFail, |
| 68 | 66 | }; |
| 69 | 67 | |
| 70 | | fn fail(self: *Code, src: usize, comptime fmt: []const u8, args: anytype) InnerError { |
| 68 | /// Sets `err_msg` on `Context` and returns `error.CodegemFail` which is caught in link/Wasm.zig |
| 69 | fn fail(self: *Context, src: usize, comptime fmt: []const u8, args: anytype) InnerError { |
| 71 | 70 | self.err_msg = try Compilation.ErrorMsg.create(self.gpa, src, fmt, args); |
| 72 | 71 | return error.CodegenFail; |
| 73 | 72 | } |
| 74 | 73 | |
| 75 | | /// Returns the `WValue` for the given `inst` |
| 76 | | /// creates a new WValue for constants and returns that instead |
| 77 | | fn resolveInst(self: Code, inst: *Inst) !WValue { |
| 74 | /// Resolves the `WValue` for the given instruction `inst` |
| 75 | /// When the given instruction has a `Value`, it returns a constant instead |
| 76 | fn resolveInst(self: Context, inst: *Inst) WValue { |
| 78 | 77 | if (inst.value()) |_| { |
| 79 | 78 | return WValue{ .constant = inst }; |
| 80 | 79 | } |
| ... | ... | @@ -83,23 +82,19 @@ pub const Code = struct { |
| 83 | 82 | } |
| 84 | 83 | |
| 85 | 84 | /// Writes the bytecode depending on the given `WValue` in `val` |
| 86 | | fn emitWValue(self: *Code, val: WValue) !void { |
| 85 | fn emitWValue(self: *Context, val: WValue) InnerError!void { |
| 87 | 86 | const writer = self.bytes.writer(); |
| 88 | 87 | switch (val) { |
| 89 | | .none => unreachable, |
| 90 | | .block_idx => unreachable, |
| 91 | | // loads the local onto the stack at the given index |
| 88 | .none, .block_idx => {}, |
| 92 | 89 | .local => |idx| { |
| 93 | | // local.set |
| 94 | | try writer.writeByte(0x20); |
| 90 | try writer.writeByte(0x20); // local.get |
| 95 | 91 | try leb.writeULEB128(writer, idx); |
| 96 | 92 | }, |
| 97 | | // creates a new constant onto the stack |
| 98 | | .constant => |inst| try self.emitConstant(inst.castTag(.constant).?), |
| 93 | .constant => |inst| try self.emitConstant(inst.castTag(.constant).?), // creates a new constant onto the stack |
| 99 | 94 | } |
| 100 | 95 | } |
| 101 | 96 | |
| 102 | | fn genFunctype(self: *Code) !void { |
| 97 | fn genFunctype(self: *Context) InnerError!void { |
| 103 | 98 | const ty = self.decl.typed_value.most_recent.typed_value.ty; |
| 104 | 99 | const writer = self.func_type_data.writer(); |
| 105 | 100 | |
| ... | ... | @@ -114,7 +109,7 @@ pub const Code = struct { |
| 114 | 109 | ty.fnParamTypes(params); |
| 115 | 110 | for (params) |param_type| { |
| 116 | 111 | const val_type = genValtype(param_type) orelse |
| 117 | | return self.fail(self.decl.src(), "TODO: Wasm generate wasm type value for type '{s}'", .{param_type.tag()}); |
| 112 | return self.fail(self.decl.src(), "TODO: Wasm codegen - arg type value for type '{s}'", .{param_type.tag()}); |
| 118 | 113 | try writer.writeByte(val_type); |
| 119 | 114 | } |
| 120 | 115 | } |
| ... | ... | @@ -126,14 +121,14 @@ pub const Code = struct { |
| 126 | 121 | else => |ret_type| { |
| 127 | 122 | try leb.writeULEB128(writer, @as(u32, 1)); |
| 128 | 123 | const val_type = genValtype(return_type) orelse |
| 129 | | return self.fail(self.decl.src(), "TODO: Wasm generate wasm return type value for type '{s}'", .{ret_type}); |
| 124 | return self.fail(self.decl.src(), "TODO: Wasm codegen - return type value for type '{s}'", .{ret_type}); |
| 130 | 125 | try writer.writeByte(val_type); |
| 131 | 126 | }, |
| 132 | 127 | } |
| 133 | 128 | } |
| 134 | 129 | |
| 135 | 130 | /// Generates the wasm bytecode for the given `code` |
| 136 | | pub fn gen(self: *Code) !void { |
| 131 | pub fn gen(self: *Context) InnerError!void { |
| 137 | 132 | assert(self.bytes.items.len == 0); |
| 138 | 133 | try self.genFunctype(); |
| 139 | 134 | const writer = self.bytes.writer(); |
| ... | ... | @@ -156,7 +151,7 @@ pub const Code = struct { |
| 156 | 151 | const elem_type = alloc.base.ty.elemType(); |
| 157 | 152 | |
| 158 | 153 | const wasm_type = genValtype(elem_type) orelse |
| 159 | | return self.fail(inst.src, "TODO: Wasm generate wasm type value for type '{s}'", .{elem_type.tag()}); |
| 154 | return self.fail(inst.src, "TODO: Wasm codegen - valtype for type '{s}'", .{elem_type.tag()}); |
| 160 | 155 | |
| 161 | 156 | try locals.append(wasm_type); |
| 162 | 157 | } |
| ... | ... | @@ -169,16 +164,9 @@ pub const Code = struct { |
| 169 | 164 | try leb.writeULEB128(writer, local); // valtype |
| 170 | 165 | } |
| 171 | 166 | |
| 172 | | for (mod_fn.body.instructions) |inst| { |
| 173 | | const result = try self.genInst(inst); |
| 174 | | |
| 175 | | if (result != .none) { |
| 176 | | try self.values.putNoClobber(inst, result); |
| 177 | | } |
| 178 | | } |
| 167 | try self.genBody(mod_fn.body); |
| 179 | 168 | |
| 180 | | // Write 'end' opcode |
| 181 | | try writer.writeByte(0x0B); |
| 169 | try writer.writeByte(0x0B); // end |
| 182 | 170 | |
| 183 | 171 | // Fill in the size of the generated code to the reserved space at the |
| 184 | 172 | // beginning of the buffer. |
| ... | ... | @@ -186,8 +174,9 @@ pub const Code = struct { |
| 186 | 174 | leb.writeUnsignedFixed(5, self.bytes.items[0..5], @intCast(u32, size)); |
| 187 | 175 | } |
| 188 | 176 | |
| 189 | | fn genInst(self: *Code, inst: *Inst) !WValue { |
| 177 | fn genInst(self: *Context, inst: *Inst) InnerError!WValue { |
| 190 | 178 | return switch (inst.tag) { |
| 179 | .add => self.genAdd(inst.castTag(.add).?), |
| 191 | 180 | .alloc => self.genAlloc(inst.castTag(.alloc).?), |
| 192 | 181 | .arg => self.genArg(inst.castTag(.arg).?), |
| 193 | 182 | .call => self.genCall(inst.castTag(.call).?), |
| ... | ... | @@ -201,20 +190,27 @@ pub const Code = struct { |
| 201 | 190 | }; |
| 202 | 191 | } |
| 203 | 192 | |
| 204 | | fn genRet(self: *Code, inst: *Inst.UnOp) !WValue { |
| 205 | | const operand = try self.resolveInst(inst.operand); |
| 193 | fn genBody(self: *Context, body: ir.Body) InnerError!void { |
| 194 | for (body.instructions) |inst| { |
| 195 | const result = try self.genInst(inst); |
| 196 | try self.values.putNoClobber(inst, result); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | fn genRet(self: *Context, inst: *Inst.UnOp) InnerError!WValue { |
| 201 | const operand = self.resolveInst(inst.operand); |
| 206 | 202 | try self.emitWValue(operand); |
| 207 | 203 | return WValue.none; |
| 208 | 204 | } |
| 209 | 205 | |
| 210 | | fn genCall(self: *Code, inst: *Inst.Call) !WValue { |
| 206 | fn genCall(self: *Context, inst: *Inst.Call) InnerError!WValue { |
| 211 | 207 | const func_inst = inst.func.castTag(.constant).?; |
| 212 | 208 | const func = func_inst.val.castTag(.function).?.data; |
| 213 | 209 | const target = func.owner_decl; |
| 214 | 210 | const target_ty = target.typed_value.most_recent.typed_value.ty; |
| 215 | 211 | |
| 216 | 212 | for (inst.args) |arg| { |
| 217 | | const arg_val = try self.resolveInst(arg); |
| 213 | const arg_val = self.resolveInst(arg); |
| 218 | 214 | try self.emitWValue(arg_val); |
| 219 | 215 | } |
| 220 | 216 | |
| ... | ... | @@ -230,39 +226,47 @@ pub const Code = struct { |
| 230 | 226 | return WValue.none; |
| 231 | 227 | } |
| 232 | 228 | |
| 233 | | fn genAlloc(self: *Code, inst: *Inst.NoOp) !WValue { |
| 229 | fn genAlloc(self: *Context, inst: *Inst.NoOp) InnerError!WValue { |
| 234 | 230 | defer self.local_index += 1; |
| 235 | 231 | return WValue{ .local = self.local_index }; |
| 236 | 232 | } |
| 237 | 233 | |
| 238 | | fn genStore(self: *Code, inst: *Inst.BinOp) !WValue { |
| 234 | fn genStore(self: *Context, inst: *Inst.BinOp) InnerError!WValue { |
| 239 | 235 | const writer = self.bytes.writer(); |
| 240 | 236 | |
| 241 | | const lhs = try self.resolveInst(inst.lhs); |
| 242 | | |
| 243 | | const rhs = try self.resolveInst(inst.rhs); |
| 237 | const lhs = self.resolveInst(inst.lhs); |
| 238 | const rhs = self.resolveInst(inst.rhs); |
| 244 | 239 | try self.emitWValue(rhs); |
| 245 | 240 | |
| 246 | 241 | try writer.writeByte(0x21); // local.set |
| 247 | 242 | try leb.writeULEB128(writer, lhs.local); |
| 248 | | |
| 249 | 243 | return WValue.none; |
| 250 | 244 | } |
| 251 | 245 | |
| 252 | | fn genLoad(self: *Code, inst: *Inst.UnOp) !WValue { |
| 246 | fn genLoad(self: *Context, inst: *Inst.UnOp) InnerError!WValue { |
| 253 | 247 | const operand = self.resolveInst(inst.operand); |
| 254 | | |
| 255 | | // ensure index to local |
| 256 | | return WValue{ .local = operand.local }; |
| 248 | try self.emitWValue(operand); |
| 249 | return WValue.none; |
| 257 | 250 | } |
| 258 | 251 | |
| 259 | | fn genArg(self: *Code, inst: *Inst.Arg) !WValue { |
| 252 | fn genArg(self: *Context, inst: *Inst.Arg) InnerError!WValue { |
| 260 | 253 | // arguments share the index with locals |
| 261 | 254 | defer self.local_index += 1; |
| 262 | 255 | return WValue{ .local = self.local_index }; |
| 263 | 256 | } |
| 264 | 257 | |
| 265 | | fn emitConstant(self: *Code, inst: *Inst.Constant) !void { |
| 258 | fn genAdd(self: *Context, inst: *Inst.BinOp) InnerError!WValue { |
| 259 | const lhs = self.resolveInst(inst.lhs); |
| 260 | const rhs = self.resolveInst(inst.rhs); |
| 261 | |
| 262 | try self.emitWValue(lhs); |
| 263 | try self.emitWValue(rhs); |
| 264 | |
| 265 | try self.bytes.append(0x6A); // i32.add |
| 266 | return WValue.none; |
| 267 | } |
| 268 | |
| 269 | fn emitConstant(self: *Context, inst: *Inst.Constant) InnerError!void { |
| 266 | 270 | const writer = self.bytes.writer(); |
| 267 | 271 | switch (inst.base.ty.tag()) { |
| 268 | 272 | .u32 => { |