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.
This commit is contained in:
Anthony Foxclaw 2020-02-09 15:16:38 -05:00
parent 76af517a49
commit 86f060e0f5
10 changed files with 120 additions and 31 deletions

View file

@ -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<CompleteStr, AssemblerInstruction>,
named!(instruction_two<CompleteStr, AssemblerInstruction>,
do_parse!(
o: opcode_load >>
o: opcode >>
opt!(multispace) >>
(
AssemblerInstruction{
opcode: o,
operand1: None,
operand2: None,
operand3: None
}
)
)
);
named!(pub instruction<CompleteStr, AssemblerInstruction>,
do_parse!(
ins: alt!(
instruction_one |
instruction_two
) >>
(
ins
)
)
);
named!(instruction_one<CompleteStr, AssemblerInstruction>,
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
}
))
);
}
}