Skip to content

Chapter 15: A Virutal Machine #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ Each commit corresponds to one chapter in the book:
* [Chapter 12: Classes](https://github.com/jeschkies/lox-rs/commit/337896b3dae4087ad889dca2f3cca32ed025134b)
* [Chapter 13: Inheritance](https://github.com/jeschkies/lox-rs/commit/0207ecc8fca1af20667c69cefb4fa5f277330ca3)

## Part III: A Bytecode Virtual Machine
## Part III: A Bytecode Virtual Machine
* [Chapter 14: Chunks of Bytecode](https://github.com/jeschkies/lox-rs/commit/bcec748d59b568c3b6ce93d6d07b40b14f44caa0)
3 changes: 3 additions & 0 deletions bytecode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ version = "0.1.0"
authors = ["Karsten Jeschkies <[email protected]>"]
edition = "2018"

[features]
debug_trace_execution = []

[dependencies]
20 changes: 15 additions & 5 deletions bytecode/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use std::ptr;
#[derive(Debug)]
pub enum OpCode {
OpConstant(usize),
OpAdd,
OpSubtract,
OpMultiply,
OpDivide,
OpNegate,
OpReturn,
}

Expand All @@ -22,7 +27,7 @@ pub struct Chunk {
// and `OpConstant` with the index as two bytes. Rust's enum will use two bytes for both. As this
// implementation is for learning purposes this solution should suffice. A safe Rust
// implementation should use `Vec<OpCode>` instead.
code: *mut OpCode,
pub code: *mut OpCode,
}

impl Chunk {
Expand Down Expand Up @@ -99,7 +104,7 @@ impl Drop for Chunk {
}

impl<'a> IntoIterator for &'a Chunk {
type Item = OpCode;
type Item = &'a OpCode;
type IntoIter = ChunkIter<'a>;

fn into_iter(self) -> Self::IntoIter {
Expand All @@ -116,13 +121,18 @@ pub struct ChunkIter<'a> {
}

impl<'a> Iterator for ChunkIter<'a> {
type Item = OpCode;
type Item = &'a OpCode;

fn next(&mut self) -> Option<Self::Item> {
if self.offset < self.chunk.count {
let result: OpCode;
let result: &OpCode;
unsafe {
result = self.chunk.code.offset(self.offset as isize).read();
result = self
.chunk
.code
.offset(self.offset as isize)
.as_ref()
.expect("Could not read Chunk.");
}
self.offset += 1;
Some(result)
Expand Down
9 changes: 7 additions & 2 deletions bytecode/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn simple_instruction(name: &str) {
println!("{}", name)
}

fn disassemble_instruction(chunk: &Chunk, op_code: OpCode, i: usize) {
pub fn disassemble_instruction(chunk: &Chunk, op_code: &OpCode, i: usize) {
// Note: The index is not really the offset if the op code has different sizes. For now all
// op codes have the same size.
print!("{:04} ", i);
Expand All @@ -30,8 +30,13 @@ fn disassemble_instruction(chunk: &Chunk, op_code: OpCode, i: usize) {
}
match op_code {
OpCode::OpConstant(constant_index) => {
constant_instruction("OP_CONSTANT", chunk, constant_index)
constant_instruction("OP_CONSTANT", chunk, *constant_index)
}
OpCode::OpAdd => simple_instruction("OP_ADD"),
OpCode::OpSubtract => simple_instruction("OP_SUBTRACT"),
OpCode::OpMultiply => simple_instruction("OP_MULTIPLY"),
OpCode::OpDivide => simple_instruction("OP_DIVIDE"),
OpCode::OpNegate => simple_instruction("OP_NEGATE"),
OpCode::OpReturn => simple_instruction("OP_RETURN"),
_ => println!("Unknown opcode {:?}", op_code),
}
Expand Down
17 changes: 16 additions & 1 deletion bytecode/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@ mod chunk;
mod debug;
mod memory;
mod value;
mod vm;

use chunk::{Chunk, OpCode};
use debug::disassemble_chunk;
use vm::VM;

fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
let mut chunk = Chunk::new();

let constant = chunk.add_constant(1.2);
chunk.write_chunk(OpCode::OpConstant(constant), 123);

let constant_2 = chunk.add_constant(3.4);
chunk.write_chunk(OpCode::OpConstant(constant_2), 123);

chunk.write_chunk(OpCode::OpAdd, 123);

let constant_3 = chunk.add_constant(5.6);
chunk.write_chunk(OpCode::OpConstant(constant_3), 123);

chunk.write_chunk(OpCode::OpDivide, 123);
chunk.write_chunk(OpCode::OpNegate, 123);

chunk.write_chunk(OpCode::OpReturn, 123);

let mut vm = VM::new(&chunk);

disassemble_chunk(&chunk, "test chunk");
vm.interpret();

// No need to free chunk since we implemented `Drop`.
Ok(())
Expand Down
87 changes: 87 additions & 0 deletions bytecode/src/vm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::chunk::{Chunk, OpCode};
use crate::debug::disassemble_instruction;
use crate::value::{print_value, Value};

macro_rules! binary_op{
( $sel:ident, $op:tt ) => {
{
let b = $sel.stack.pop().expect("The stack was empty!");
let a = $sel.stack.pop().expect("The stack was empty!");
$sel.stack.push(a $op b);
}
};
}

static STACK_MAX: usize = 245;

pub struct VM<'a> {
chunk: &'a Chunk,
ip: *const OpCode,
stack: Vec<Value>,
}

// TODO: replace with Result<_, Error>
pub enum InterpretResult {
Ok,
CompileError,
RuntimeError,
}

impl<'a> VM<'a> {
pub fn new(chunk: &'a Chunk) -> Self {
VM {
chunk: chunk,
ip: chunk.code,
stack: Vec::with_capacity(STACK_MAX),
}
}

pub fn interpret(mut self) -> InterpretResult {
self.run()
}

fn run(mut self) -> InterpretResult {
let mut position: usize = 0; // TODO: infer position from self.ip.
loop {
let instruction: OpCode = unsafe {
let r = self.ip.read();
self.ip = self.ip.add(1);
r
};

if cfg!(feature = "debug_trace_execution") {
print!(" ");
for slot in &self.stack {
print!("[{:?}]", slot);
}
println!();
disassemble_instruction(self.chunk, &instruction, position);
position += 1;
}

match instruction {
OpCode::OpConstant(index) => {
let constant = self.read_constant(index);
self.stack.push(constant);
}
OpCode::OpAdd => binary_op!(self, +),
OpCode::OpSubtract => binary_op!(self, -),
OpCode::OpMultiply => binary_op!(self, *),
OpCode::OpDivide => binary_op!(self, /),
OpCode::OpNegate => {
let value = self.stack.pop().expect("The stack was empty!");
self.stack.push(-value);
}
OpCode::OpReturn => {
print_value(self.stack.pop().expect("The stack was empty!"));
println!();
return InterpretResult::Ok;
}
}
}
}

fn read_constant(&self, index: usize) -> Value {
self.chunk.constants[index]
}
}