From 86f060e0f5ac181e2fb4e4ac467ae78966a3d5b4 Mon Sep 17 00:00:00 2001 From: Anthony Foxclaw <35226681+tonytins@users.noreply.github.com> Date: Sun, 9 Feb 2020 15:16:38 -0500 Subject: [PATCH] Implemented HLT instruction - Instructions are now detected automatically based on context - Since MIPs can't decide if it wants to be open or not, Corten will be aiming to be more of a RISC-V VM in the long term. --- README.md | 2 +- docs/README.md | 2 +- docs/spec.md | 14 +++---- src/assembler.rs | 30 +++++++++++++- src/assembler/instruction_parser.rs | 61 ++++++++++++++++++++++++----- src/assembler/opcode_parser.rs | 20 +++++++--- src/assembler/operand_parser.rs | 2 +- src/assembler/program_parser.rs | 4 +- src/instruction.rs | 13 +++++- src/repl.rs | 3 +- 10 files changed, 120 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index eb2f85f..3976978 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Corten -Corten is a [MIPS64 Release 6](https://en.wikipedia.org/wiki/MIPS_architecture#MIPS32/MIPS64_Release_6) virtual machine written in Rust as a hobby and based on Fletcher Haynes's [So you want to build a language VM](https://blog.subnetzero.io/post/building-language-vm-part-01/) tutorial. +Corten is a [RISC-V](https://rv8.io/isa.html) virtual machine written in Rust as a hobby and based on Fletcher Haynes's [So you want to build a language VM](https://blog.subnetzero.io/post/building-language-vm-part-01/) tutorial. ## Build Status diff --git a/docs/README.md b/docs/README.md index dc708f6..4cf0c4a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # Welcome -Corten is a [MIPS64 Release 6](https://en.wikipedia.org/wiki/MIPS_architecture#MIPS32/MIPS64_Release_6) virtual machine written in Rust as a hobby and based on Fletcher Haynes's [So you want to build a language VM](https://blog.subnetzero.io/post/building-language-vm-part-01/) tutorial. +Corten is a [RISC-V](https://rv8.io/isa.html) virtual machine written in Rust as a hobby and based on Fletcher Haynes's [So you want to build a language VM](https://blog.subnetzero.io/post/building-language-vm-part-01/) tutorial. ## Specifications diff --git a/docs/spec.md b/docs/spec.md index fc695a8..29b119b 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -1,14 +1,14 @@ # Specifications -## Instruction Set +## Instruction Set -| Opcode | Function | Comment | +| Code | Operation | Comment | | --- | --- | --- | | 0 | LOAD | Load program | -| 1 | ADD | -| 2 | SUB | -| 3 | MUL | -| 4 | DIV | +| 1 | ADD | Add | +| 2 | SUB | Subtract +| 3 | MUL | Multiply | +| 4 | DIV | Division | | 5 | HLT | Halt | | 6 | JMP | Jump | | 7 | JMPF | Jump forward | @@ -20,5 +20,5 @@ | 13 | LTE | Less then or equal to | | 14 | LT | Less then | | 15 | JMPE | Jump if equal | -| 16 | NOP | +| 16 | NOP | No Operation | | _ | IGL | Illegal action | \ No newline at end of file diff --git a/src/assembler.rs b/src/assembler.rs index 834bbe9..f4a9499 100644 --- a/src/assembler.rs +++ b/src/assembler.rs @@ -1,4 +1,7 @@ #![allow(dead_code)] + +use nom::types::CompleteStr; + pub mod instruction_parser; pub mod opcode_parser; pub mod operand_parser; @@ -92,4 +95,29 @@ impl From for u8 { Opcode::IGL => 100, } } -} \ No newline at end of file +} + +impl<'a> From> for Opcode { + fn from(v: CompleteStr<'a>) -> Self { + match v { + CompleteStr("load") => Opcode::LOAD, + CompleteStr("add") => Opcode::ADD, + CompleteStr("sub") => Opcode::SUB, + CompleteStr("mul") => Opcode::MUL, + CompleteStr("div") => Opcode::DIV, + CompleteStr("hlt") => Opcode::HLT, + CompleteStr("jmp") => Opcode::JMP, + CompleteStr("jmpf") => Opcode::JMPF, + CompleteStr("jmpb") => Opcode::JMPB, + CompleteStr("eq") => Opcode::EQ, + CompleteStr("neq") => Opcode::NEQ, + CompleteStr("gte") => Opcode::GTE, + CompleteStr("gt") => Opcode::GT, + CompleteStr("lte") => Opcode::LTE, + CompleteStr("lt") => Opcode::LT, + CompleteStr("jmpe") => Opcode::JMPE, + CompleteStr("nop") => Opcode::NOP, + _ => Opcode::IGL, + } + } +} diff --git a/src/assembler/instruction_parser.rs b/src/assembler/instruction_parser.rs index ddcdf11..21b1d83 100644 --- a/src/assembler/instruction_parser.rs +++ b/src/assembler/instruction_parser.rs @@ -1,9 +1,8 @@ -use nom::*; -use crate::assembler::Token; -use crate::assembler::opcode_parser::opcode_load; +use crate::assembler::opcode_parser::opcode; use crate::assembler::operand_parser::integer_operand; use crate::assembler::register_parser::register; - +use crate::assembler::Token; +use nom::*; use nom::types::CompleteStr; #[derive(Debug, PartialEq)] @@ -30,7 +29,6 @@ impl AssemblerInstruction { } }; - for operand in &[&self.operand1, &self.operand2, &self.operand3] { if let Some(token) = operand { AssemblerInstruction::extract_operand(token, &mut results) @@ -44,7 +42,7 @@ impl AssemblerInstruction { match t { Token::Register { reg_num } => { results.push(*reg_num); - }, + } Token::Number { value } => { let conv = *value as u16; let byte1 = conv; @@ -52,19 +50,45 @@ impl AssemblerInstruction { results.push(byte2 as u8); results.push(byte1 as u8); - }, + } _ => { println!("Opcode found in operand field"); std::process::exit(1); } }; } - } -named!(pub instruction_one, +named!(instruction_two, do_parse!( - o: opcode_load >> + o: opcode >> + opt!(multispace) >> + ( + AssemblerInstruction{ + opcode: o, + operand1: None, + operand2: None, + operand3: None + } + ) + ) +); + +named!(pub instruction, + do_parse!( + ins: alt!( + instruction_one | + instruction_two + ) >> + ( + ins + ) + ) +); + +named!(instruction_one, + do_parse!( + o: opcode >> r: register >> i: integer_operand >> ( @@ -99,4 +123,21 @@ mod instruction_parser_test { )) ); } + + #[test] + fn test_parse_instruction_form_two() { + let result = instruction_two(CompleteStr("hlt\n")); + assert_eq!( + result, + Ok(( + CompleteStr(""), + AssemblerInstruction { + opcode: Token::Opcode { code: Opcode::HLT }, + operand1: None, + operand2: None, + operand3: None + } + )) + ); + } } diff --git a/src/assembler/opcode_parser.rs b/src/assembler/opcode_parser.rs index 8d4406f..f19e178 100644 --- a/src/assembler/opcode_parser.rs +++ b/src/assembler/opcode_parser.rs @@ -1,11 +1,18 @@ #![allow(unused_imports)] use nom::*; -use nom::{digit, types::CompleteStr}; +use nom::{alpha1, digit, types::CompleteStr}; -use crate::assembler::{Token, Opcode}; +use crate::assembler::{Opcode, Token}; -named!(pub opcode_load, - do_parse!(tag!("load") >> (Token::Opcode{code: Opcode::LOAD})) +named!(pub opcode, + do_parse!( + opcode: alpha1 >> + ( + { + Token::Opcode{code: Opcode::from(opcode)} + } + ) + ) ); #[cfg(test)] @@ -15,10 +22,13 @@ mod opcode_parser_test { #[test] fn test_parser_op_load() { // Test that opcode is dected and parsed correctly - let result = opcode_load(CompleteStr("load")); + let result = opcode(CompleteStr("load")); assert_eq!(result.is_ok(), true); let (rest, token) = result.unwrap(); assert_eq!(token, Token::Opcode { code: Opcode::LOAD }); assert_eq!(rest, CompleteStr("")); + let result = opcode(CompleteStr("alod")); + let (_, token) = result.unwrap(); + assert_eq!(token, Token::Opcode { code: Opcode::IGL }); } } diff --git a/src/assembler/operand_parser.rs b/src/assembler/operand_parser.rs index 110d9fe..dddd28c 100644 --- a/src/assembler/operand_parser.rs +++ b/src/assembler/operand_parser.rs @@ -21,7 +21,7 @@ mod reg_parser_test { use super::*; #[test] - fn test_opcode_load() { + fn test_opcode() { let result = integer_operand(CompleteStr("#10")); let (rest, value) = result.unwrap(); assert_eq!(rest, CompleteStr("")); diff --git a/src/assembler/program_parser.rs b/src/assembler/program_parser.rs index a0ac7bf..ed19fc2 100644 --- a/src/assembler/program_parser.rs +++ b/src/assembler/program_parser.rs @@ -1,7 +1,7 @@ use nom::types::CompleteStr; use nom::*; -use crate::assembler::instruction_parser::{instruction_one, AssemblerInstruction}; +use crate::assembler::instruction_parser::{instruction, AssemblerInstruction}; #[derive(Debug, PartialEq)] pub struct Program { @@ -20,7 +20,7 @@ impl Program { named!(pub program, do_parse!( - instructions: many1!(instruction_one) >> + instructions: many1!(instruction) >> ( Program { instructions diff --git a/src/instruction.rs b/src/instruction.rs index e84f5af..ff68fa4 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -15,8 +15,9 @@ impl Instruction { #[cfg(test)] mod instruction_tests { - use crate::instruction::*; use crate::assembler::Opcode; + use crate::instruction::*; + use nom::types::CompleteStr; #[test] fn test_crate_hlt() { @@ -29,4 +30,14 @@ mod instruction_tests { let inst = Instruction::new(Opcode::HLT); assert_eq!(inst.opcode, Opcode::HLT); } + + #[test] + fn test_str_to_opcode() { + let opcode = Opcode::from(CompleteStr("load")); + assert_eq!(opcode, Opcode::LOAD); + let opcode = Opcode::from(CompleteStr("add")); + assert_eq!(opcode, Opcode::ADD); + let opcode = Opcode::from(CompleteStr("illegal")); + assert_eq!(opcode, Opcode::IGL); + } } diff --git a/src/repl.rs b/src/repl.rs index 2154bcc..1feb3ff 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -2,11 +2,10 @@ use std; use std::io; use std::io::Write; -use crate::vm::VM; use crate::assembler::program_parser::program; +use crate::vm::VM; use metacrate::crate_version; use rbtag::{BuildDateTime, BuildInfo}; -use nom::types::CompleteStr; #[derive(BuildDateTime, BuildInfo)] struct BuildTag;