| ... | @@ -3,9 +3,12 @@ const Allocator = std.mem.Allocator; | ... | @@ -3,9 +3,12 @@ const Allocator = std.mem.Allocator; |
| 3 | const ArrayList = std.ArrayList; | 3 | const ArrayList = std.ArrayList; |
| 4 | const assert = std.debug.assert; | 4 | const assert = std.debug.assert; |
| 5 | const leb = std.debug.leb; | 5 | const leb = std.debug.leb; |
| | 6 | const mem = std.mem; |
| 6 | | 7 | |
| 7 | const Decl = @import("../Module.zig").Decl; | 8 | const Decl = @import("../Module.zig").Decl; |
| | 9 | const Inst = @import("../ir.zig").Inst; |
| 8 | const Type = @import("../type.zig").Type; | 10 | const Type = @import("../type.zig").Type; |
| | 11 | const Value = @import("../value.zig").Value; |
| 9 | | 12 | |
| 10 | fn genValtype(ty: Type) u8 { | 13 | fn genValtype(ty: Type) u8 { |
| 11 | return switch (ty.tag()) { | 14 | return switch (ty.tag()) { |
| ... | @@ -56,10 +59,10 @@ pub fn genCode(buf: *ArrayList(u8), decl: *Decl) !void { | ... | @@ -56,10 +59,10 @@ pub fn genCode(buf: *ArrayList(u8), decl: *Decl) !void { |
| 56 | try leb.writeULEB128(writer, @as(u32, 0)); | 59 | try leb.writeULEB128(writer, @as(u32, 0)); |
| 57 | | 60 | |
| 58 | // Write instructions | 61 | // Write instructions |
| 59 | | 62 | // TODO: check for and handle death of instructions |
| 60 | // TODO: actually implement codegen | 63 | const tv = decl.typed_value.most_recent.typed_value; |
| 61 | try writer.writeByte(0x41); // i32.const | 64 | const mod_fn = tv.val.cast(Value.Payload.Function).?.func; |
| 62 | try leb.writeILEB128(writer, @as(i32, 42)); | 65 | for (mod_fn.analysis.success.instructions) |inst| try genInst(writer, inst); |
| 63 | | 66 | |
| 64 | // Write 'end' opcode | 67 | // Write 'end' opcode |
| 65 | try writer.writeByte(0x0B); | 68 | try writer.writeByte(0x0B); |
| ... | @@ -68,3 +71,49 @@ pub fn genCode(buf: *ArrayList(u8), decl: *Decl) !void { | ... | @@ -68,3 +71,49 @@ pub fn genCode(buf: *ArrayList(u8), decl: *Decl) !void { |
| 68 | // beginning of the buffer. | 71 | // beginning of the buffer. |
| 69 | leb.writeUnsignedFixed(5, buf.items[0..5], @intCast(u32, buf.items.len - 5)); | 72 | leb.writeUnsignedFixed(5, buf.items[0..5], @intCast(u32, buf.items.len - 5)); |
| 70 | } | 73 | } |
| | 74 | |
| | 75 | fn genInst(writer: ArrayList(u8).Writer, inst: *Inst) !void { |
| | 76 | return switch (inst.tag) { |
| | 77 | .dbg_stmt => {}, |
| | 78 | .ret => genRet(writer, inst.castTag(.ret).?), |
| | 79 | else => error.TODOImplementMoreWasmCodegen, |
| | 80 | }; |
| | 81 | } |
| | 82 | |
| | 83 | fn genRet(writer: ArrayList(u8).Writer, inst: *Inst.UnOp) !void { |
| | 84 | switch (inst.operand.tag) { |
| | 85 | .constant => { |
| | 86 | const constant = inst.operand.castTag(.constant).?; |
| | 87 | switch (inst.operand.ty.tag()) { |
| | 88 | .u32 => { |
| | 89 | try writer.writeByte(0x41); // i32.const |
| | 90 | try leb.writeILEB128(writer, constant.val.toUnsignedInt()); |
| | 91 | }, |
| | 92 | .i32 => { |
| | 93 | try writer.writeByte(0x41); // i32.const |
| | 94 | try leb.writeILEB128(writer, constant.val.toSignedInt()); |
| | 95 | }, |
| | 96 | .u64 => { |
| | 97 | try writer.writeByte(0x42); // i64.const |
| | 98 | try leb.writeILEB128(writer, constant.val.toUnsignedInt()); |
| | 99 | }, |
| | 100 | .i64 => { |
| | 101 | try writer.writeByte(0x42); // i64.const |
| | 102 | try leb.writeILEB128(writer, constant.val.toSignedInt()); |
| | 103 | }, |
| | 104 | .f32 => { |
| | 105 | try writer.writeByte(0x43); // f32.const |
| | 106 | // TODO: enforce LE byte order |
| | 107 | try writer.writeAll(mem.asBytes(&constant.val.toFloat(f32))); |
| | 108 | }, |
| | 109 | .f64 => { |
| | 110 | try writer.writeByte(0x44); // f64.const |
| | 111 | // TODO: enforce LE byte order |
| | 112 | try writer.writeAll(mem.asBytes(&constant.val.toFloat(f64))); |
| | 113 | }, |
| | 114 | else => return error.TODOImplementMoreWasmCodegen, |
| | 115 | } |
| | 116 | }, |
| | 117 | else => return error.TODOImplementMoreWasmCodegen, |
| | 118 | } |
| | 119 | } |