From 922f3c530c69987e99972073c1ca7932b9262517 Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 3 Jun 2019 00:07:11 -0300 Subject: [PATCH 1/3] chunk: fix disasm on the new const-load op codes --- src/chunk.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/chunk.zig b/src/chunk.zig index abc54af..cbebaf1 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -220,13 +220,13 @@ pub const Chunk = struct { } else if (instruction == OpCode.Pop) { return try simpleInstruction(stdout, "OP_POP", index); } else if (instruction == OpCode.DefineGlobal) { - return try simpleInstruction(stdout, "OP_DEFGLOBAL", index); + return try constantInstruction(stdout, "OP_DEFGLOBAL", self, index); } else if (instruction == OpCode.DefineGlobalLong) { - return try simpleInstruction(stdout, "OP_DEFGLOBAL_LONG", index); + return try constantLongInstruction(stdout, "OP_DEFGLOBAL_LONG", self, index); } else if (instruction == OpCode.GetGlobal) { - return try simpleInstruction(stdout, "OP_GETGLOBAL", index); + return try constantInstruction(stdout, "OP_GETGLOBAL", self, index); } else if (instruction == OpCode.GetGlobalLong) { - return try simpleInstruction(stdout, "OP_GETGLOBAL_LONG", index); + return try constantLongInstruction(stdout, "OP_GETGLOBAL_LONG", self, index); } else { try stdout.print("Unknown opcode: {}\n", instruction); return index + 1; From 8bc220d2f88f6eca09206edf479bf83ccb934903 Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 3 Jun 2019 00:55:50 -0300 Subject: [PATCH 2/3] chunk: split writing side-effects into own function split writeConstant() into that and writeConstantRaw() for the places where we don't want OP_CONSTANT written as well. this caused a bug where doing `"some const string" + x` would cause an unecessary OP_CONSTANT to be added for the x variable and would cause the wrong result to be given. - main: reset stack on repl tick (?) --- src/chunk.zig | 34 ++++++++++++++++++++++++++-------- src/compiler.zig | 8 +++++++- src/main.zig | 2 ++ src/vm.zig | 7 +++++++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/chunk.zig b/src/chunk.zig index cbebaf1..7a89c3e 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -134,7 +134,7 @@ pub const Chunk = struct { return self.constants.count - 1; } - pub fn writeConstant( + pub fn writeConstantRaw( self: *Chunk, val: value.Value, line: usize, @@ -143,10 +143,7 @@ pub const Chunk = struct { var constant_idx = self.constants.count - 1; if (constant_idx < 256) { - try self.write(OpCode.Constant, line); - var idx_small = @intCast(u8, constant_idx); - try self.write(idx_small, line); return ConstantIndex{ .Small = idx_small }; } else { var idx_u24: u24 = @intCast(u24, constant_idx); @@ -157,14 +154,35 @@ pub const Chunk = struct { const v2: u8 = @intCast(u8, (idx_u24 >> 8) & mask); const v3: u8 = @intCast(u8, (idx_u24 >> 16) & mask); - try self.write(OpCode.ConstantLong, line); - try self.write(v3, line); - try self.write(v2, line); - try self.write(v1, line); return ConstantIndex{ .Long = []u8{ v3, v2, v1 } }; } } + pub fn writeConstant( + self: *Chunk, + val: value.Value, + line: usize, + ) !ConstantIndex { + var idx = try self.writeConstantRaw(val, line); + + switch (idx) { + .Small => |idx_small| blk: { + try self.write(OpCode.Constant, line); + try self.write(idx_small, line); + break :blk; + }, + .Long => |long_u8| blk: { + try self.write(OpCode.ConstantLong, line); + try self.write(long_u8[0], line); + try self.write(long_u8[1], line); + try self.write(long_u8[2], line); + }, + else => unreachable, + } + + return idx; + } + pub fn disassembleInstruction( self: *Chunk, stdout: var, diff --git a/src/compiler.zig b/src/compiler.zig index de0f25a..fb3ee39 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -253,7 +253,13 @@ pub const Compiler = struct { } fn namedVariable(self: *Compiler, tok: *Token) !void { - var idx = try self.identifierConstant(tok); + // writeConstant always writes OP_CODE which may be not + // what we want, so. + var idx = try self.currentChunk().writeConstantRaw(values.ObjVal(try objects.copyString( + self.vmach, + tok.lexeme, + )), tok.line); + try self.emitConstWithIndex( chunks.OpCode.GetGlobal, chunks.OpCode.GetGlobalLong, diff --git a/src/main.zig b/src/main.zig index a38e2a3..3983140 100644 --- a/src/main.zig +++ b/src/main.zig @@ -85,6 +85,8 @@ fn runPrompt(allocator: *Allocator) !void { else => return err, } }; + + vmach.resetStack(); } } diff --git a/src/vm.zig b/src/vm.zig index 24a72f8..a0decdf 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -237,6 +237,13 @@ pub const VM = struct { fn doGetGlobal(self: *VM, name: []u8) !void { var kv_opt = self.globals.get(name); + // take out the OP_CONST loaded before, if any + // note this is a complete hack. + //var val = self.peek(0); + //if (val.vtype == .Object and val.as.Object.otype == .String and std.mem.compare(u8, val.as.Object.value.String, name) == .Equal) { + // _ = self.pop(); + //} + if (kv_opt) |kv| { try self.push(kv.value); } else { From 9f45dea2c0308ebb19d2fde4cb8305facdea2d0e Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 3 Jun 2019 00:58:44 -0300 Subject: [PATCH 3/3] vm: remove uneeded hack --- src/vm.zig | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/vm.zig b/src/vm.zig index a0decdf..24a72f8 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -237,13 +237,6 @@ pub const VM = struct { fn doGetGlobal(self: *VM, name: []u8) !void { var kv_opt = self.globals.get(name); - // take out the OP_CONST loaded before, if any - // note this is a complete hack. - //var val = self.peek(0); - //if (val.vtype == .Object and val.as.Object.otype == .String and std.mem.compare(u8, val.as.Object.value.String, name) == .Equal) { - // _ = self.pop(); - //} - if (kv_opt) |kv| { try self.push(kv.value); } else {