purged .unwrap()
This commit is contained in:
+69
-32
@@ -22,13 +22,17 @@ fn build_instruction(node: Node) -> Result<Instruction, AssembleError> {
|
||||
match opcode {
|
||||
Opcode::Nop => Ok(Instruction::Nop),
|
||||
Opcode::Mov => {
|
||||
let src = expect_token!(args.first().unwrap(), Register)?;
|
||||
let dest = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let src =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let dest =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
Ok(Instruction::Mov(args!(R, sr1: src, dr: dest)))
|
||||
}
|
||||
Opcode::Movs => {
|
||||
let src = expect_token!(args.first().unwrap(), Register)?;
|
||||
let dest = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let src =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let dest =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
Ok(Instruction::MovSigned(args!(R, sr1: src, dr: dest)))
|
||||
}
|
||||
Opcode::Ldb
|
||||
@@ -39,9 +43,12 @@ fn build_instruction(node: Node) -> Result<Instruction, AssembleError> {
|
||||
| Opcode::Stb
|
||||
| Opcode::Stw
|
||||
| Opcode::Sth => {
|
||||
let base = expect_token!(args.first().unwrap(), Register)?;
|
||||
let dest = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let offset = expect_token!(args.get(2).unwrap(), Immediate)?;
|
||||
let base =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let dest =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
let offset =
|
||||
expect_token!(args.get(2).expect("The spanish Inquisition"), Immediate)?;
|
||||
let args = args!(I, immediate: offset as u16, r1: base, r2: dest);
|
||||
|
||||
match opcode {
|
||||
@@ -57,15 +64,20 @@ fn build_instruction(node: Node) -> Result<Instruction, AssembleError> {
|
||||
}
|
||||
}
|
||||
Opcode::Lli => {
|
||||
let value = expect_token!(args.first().unwrap(), Immediate)?;
|
||||
let dest = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let value =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Immediate)?;
|
||||
let dest =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
let args = args!(I, immediate: value as u16, r1: dest);
|
||||
|
||||
Ok(Instruction::LoadLowerImmediate(args))
|
||||
}
|
||||
Opcode::Lui => {
|
||||
let value = expect_token!(args.first().unwrap(), Immediate)? >> 16;
|
||||
let dest = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let value =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Immediate)?
|
||||
>> 16;
|
||||
let dest =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
let args = args!(I, immediate: value as u16, r1: dest);
|
||||
|
||||
Ok(Instruction::LoadUpperImmediate(args))
|
||||
@@ -77,8 +89,10 @@ fn build_instruction(node: Node) -> Result<Instruction, AssembleError> {
|
||||
| Opcode::Jge
|
||||
| Opcode::Jlt
|
||||
| Opcode::Jle => {
|
||||
let address = expect_token!(args.first().unwrap(), Immediate)?;
|
||||
let offset = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let address =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Immediate)?;
|
||||
let offset =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
let args = args!(I, immediate: address as u16, r1: offset);
|
||||
|
||||
match opcode {
|
||||
@@ -93,26 +107,36 @@ fn build_instruction(node: Node) -> Result<Instruction, AssembleError> {
|
||||
}
|
||||
}
|
||||
Opcode::Cmp => {
|
||||
let left = expect_token!(args.first().unwrap(), Register)?;
|
||||
let right = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let left =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let right =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
Ok(Instruction::Compare(args!(R, sr1: left, sr2: right)))
|
||||
}
|
||||
Opcode::Inc => {
|
||||
let reg = expect_token!(args.first().unwrap(), Register)?;
|
||||
let reg =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
Ok(Instruction::Increment(args!(R, sr1: reg)))
|
||||
}
|
||||
Opcode::Dec => {
|
||||
let reg = expect_token!(args.first().unwrap(), Register)?;
|
||||
let reg =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
Ok(Instruction::Decrement(args!(R, sr1: reg)))
|
||||
}
|
||||
Opcode::Shl => {
|
||||
let reg = expect_token!(args.first().unwrap(), Register)?;
|
||||
let amount = expect_token!(args.get(1).unwrap(), Immediate)? as u8;
|
||||
let reg =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let amount =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Immediate)?
|
||||
as u8;
|
||||
Ok(Instruction::ShiftLeft(args!(R, sr1: reg, shamt: amount)))
|
||||
}
|
||||
Opcode::Shr => {
|
||||
let reg = expect_token!(args.first().unwrap(), Register)?;
|
||||
let amount = expect_token!(args.get(1).unwrap(), Immediate)? as u8;
|
||||
let reg =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let amount =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Immediate)?
|
||||
as u8;
|
||||
Ok(Instruction::ShiftRight(args!(R, sr1: reg, shamt: amount)))
|
||||
}
|
||||
Opcode::Add
|
||||
@@ -123,9 +147,12 @@ fn build_instruction(node: Node) -> Result<Instruction, AssembleError> {
|
||||
| Opcode::Nand
|
||||
| Opcode::Nor
|
||||
| Opcode::Xnor => {
|
||||
let left = expect_token!(args.first().unwrap(), Register)?;
|
||||
let right = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let dest = expect_token!(args.get(2).unwrap(), Register)?;
|
||||
let left =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let right =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
let dest =
|
||||
expect_token!(args.get(2).expect("The spanish Inquisition"), Register)?;
|
||||
let args = args!(R, sr1: left, sr2: right, dr: dest);
|
||||
|
||||
match opcode {
|
||||
@@ -141,9 +168,13 @@ fn build_instruction(node: Node) -> Result<Instruction, AssembleError> {
|
||||
}
|
||||
}
|
||||
Opcode::Iadd | Opcode::Isub => {
|
||||
let reg = expect_token!(args.first().unwrap(), Register)?;
|
||||
let immediate = expect_token!(args.get(1).unwrap(), Immediate)? as u16;
|
||||
let dest = expect_token!(args.get(2).unwrap(), Register)?;
|
||||
let reg =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let immediate =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Immediate)?
|
||||
as u16;
|
||||
let dest =
|
||||
expect_token!(args.get(2).expect("The spanish Inquisition"), Register)?;
|
||||
let args = args!(I, immediate: immediate, r1: reg, r2: dest);
|
||||
|
||||
match opcode {
|
||||
@@ -153,22 +184,28 @@ fn build_instruction(node: Node) -> Result<Instruction, AssembleError> {
|
||||
}
|
||||
}
|
||||
Opcode::Not => {
|
||||
let reg = expect_token!(args.first().unwrap(), Register)?;
|
||||
let dest = expect_token!(args.get(1).unwrap(), Register)?;
|
||||
let reg =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Register)?;
|
||||
let dest =
|
||||
expect_token!(args.get(1).expect("The spanish Inquisition"), Register)?;
|
||||
Ok(Instruction::Not(args!(R, sr1: reg, dr: dest)))
|
||||
}
|
||||
Opcode::Int => {
|
||||
let code = expect_token!(args.first().unwrap(), Immediate)? as u8;
|
||||
let code =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Immediate)?
|
||||
as u8;
|
||||
Ok(Instruction::Interrupt(Interrupt::Software(code)))
|
||||
}
|
||||
Opcode::Irt => Ok(Instruction::IntReturn),
|
||||
Opcode::Hlt => Ok(Instruction::Halt),
|
||||
Opcode::Data => {
|
||||
let immediate = expect_token!(args.first().unwrap(), Immediate)?;
|
||||
let immediate =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Immediate)?;
|
||||
Ok(Instruction::Data(immediate))
|
||||
}
|
||||
Opcode::Segment => {
|
||||
let immediate = expect_token!(args.first().unwrap(), Immediate)?;
|
||||
let immediate =
|
||||
expect_token!(args.first().expect("The spanish Inquisition"), Immediate)?;
|
||||
Ok(Instruction::Segment(immediate))
|
||||
}
|
||||
Opcode::Db
|
||||
|
||||
+22
-13
@@ -44,7 +44,7 @@ fn try_expand(
|
||||
|
||||
fn expand_push(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let label = current.label();
|
||||
let reg = expect_type!(current.arg(0).unwrap(), Register)?;
|
||||
let reg = expect_type!(current.arg(0).expect("The spanish Inquisition"), Register)?;
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(
|
||||
@@ -68,7 +68,7 @@ fn expand_push(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError
|
||||
|
||||
fn expand_pop(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let label = current.label();
|
||||
let reg = expect_type!(current.arg(0).unwrap(), Register)?;
|
||||
let reg = expect_type!(current.arg(0).expect("The spanish Inquisition"), Register)?;
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(
|
||||
@@ -92,9 +92,10 @@ fn expand_pop(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError>
|
||||
|
||||
fn expand_ldx(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let opcode = current.opcode();
|
||||
let name = expect_type!(current.arg(0).unwrap(), Symbol)?;
|
||||
let reg = expect_type!(current.arg(1).unwrap(), Register)?;
|
||||
let offset = expect_type!(current.arg(2).unwrap(), Immediate)?;
|
||||
let name = expect_type!(current.arg(0).expect("The spanish Inquisition"), Symbol)?;
|
||||
let reg = expect_type!(current.arg(1).expect("The spanish Inquisition"), Register)?;
|
||||
let offset =
|
||||
expect_type!(current.arg(2).expect("The spanish Inquisition"), Immediate)?;
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(current.label(), Opcode::Lli, name.clone(), reg.clone()),
|
||||
@@ -107,9 +108,10 @@ fn expand_ldx(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError>
|
||||
|
||||
fn expand_stx(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let opcode = current.opcode();
|
||||
let base = expect_type!(current.arg(0).unwrap(), Register)?;
|
||||
let dest = expect_type!(current.arg(1).unwrap(), Symbol)?;
|
||||
let offset = expect_type!(current.arg(2).unwrap(), Immediate)?;
|
||||
let base = expect_type!(current.arg(0).expect("The spanish Inquisition"), Register)?;
|
||||
let dest = expect_type!(current.arg(1).expect("The spanish Inquisition"), Symbol)?;
|
||||
let offset =
|
||||
expect_type!(current.arg(2).expect("The spanish Inquisition"), Immediate)?;
|
||||
nodes.extend(vec![
|
||||
node!(
|
||||
current.label(),
|
||||
@@ -130,8 +132,12 @@ fn expand_stx(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError>
|
||||
}
|
||||
|
||||
fn expand_lwi(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let val = expect_type!(current.arg(0).unwrap(), Symbol, Immediate)?;
|
||||
let reg = expect_type!(current.arg(1).unwrap(), Register)?;
|
||||
let val = expect_type!(
|
||||
current.arg(0).expect("The spanish Inquisition"),
|
||||
Symbol,
|
||||
Immediate
|
||||
)?;
|
||||
let reg = expect_type!(current.arg(1).expect("The spanish Inquisition"), Register)?;
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(current.label(), Opcode::Lli, val.clone(), reg.clone()),
|
||||
@@ -142,8 +148,10 @@ fn expand_lwi(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError>
|
||||
}
|
||||
|
||||
fn expand_resx(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let region_label = expect_token!(current.arg(0).unwrap(), Symbol)?;
|
||||
let size = expect_token!(current.arg(1).unwrap(), Immediate)?;
|
||||
let region_label =
|
||||
expect_token!(current.arg(0).expect("The spanish Inquisition"), Symbol)?;
|
||||
let size =
|
||||
expect_token!(current.arg(1).expect("The spanish Inquisition"), Immediate)?;
|
||||
|
||||
let units_per = match current.opcode() {
|
||||
Opcode::Resb => 4,
|
||||
@@ -165,7 +173,8 @@ fn expand_resx(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError
|
||||
}
|
||||
|
||||
fn expand_dx(current: Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let region_label = expect_token!(current.arg(0).unwrap(), Symbol)?;
|
||||
let region_label =
|
||||
expect_token!(current.arg(0).expect("The spanish Inquisition"), Symbol)?;
|
||||
let size = match current.opcode() {
|
||||
Opcode::Db => 4,
|
||||
Opcode::Dh => 2,
|
||||
|
||||
+27
-12
@@ -59,7 +59,10 @@ impl Parser {
|
||||
let mut dependencies = Vec::new();
|
||||
for node in nodes {
|
||||
if let Opcode::Include = node.opcode() {
|
||||
let path = expect_token!(node.args().get(1).unwrap(), StringLit)?;
|
||||
let path = expect_token!(
|
||||
node.args().get(1).expect("The spanish Inquisition"),
|
||||
StringLit
|
||||
)?;
|
||||
dependencies.push(PathBuf::from(path));
|
||||
}
|
||||
}
|
||||
@@ -242,12 +245,16 @@ impl Parser {
|
||||
Opcode::Db => {
|
||||
// db can take string literals or u8 immediates
|
||||
while !self.tokens.is_empty() {
|
||||
match self.tokens.last().unwrap() {
|
||||
match self.tokens.last().expect("The spanish Inquisition") {
|
||||
Token::StringLit(_) => {
|
||||
values.push(self.tokens.pop().unwrap());
|
||||
values.push(
|
||||
self.tokens.pop().expect("The spanish Inquisition"),
|
||||
);
|
||||
}
|
||||
Token::Immediate(val) if *val <= u8::MAX as u32 => {
|
||||
values.push(self.tokens.pop().unwrap());
|
||||
values.push(
|
||||
self.tokens.pop().expect("The spanish Inquisition"),
|
||||
);
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
@@ -257,12 +264,16 @@ impl Parser {
|
||||
Opcode::Dh => {
|
||||
// dh can take u16 immediates
|
||||
while !self.tokens.is_empty() {
|
||||
match self.tokens.last().unwrap() {
|
||||
match self.tokens.last().expect("The spanish Inquisition") {
|
||||
Token::StringLit(_) => {
|
||||
values.push(self.tokens.pop().unwrap());
|
||||
values.push(
|
||||
self.tokens.pop().expect("The spanish Inquisition"),
|
||||
);
|
||||
}
|
||||
Token::Immediate(val) if *val <= u16::MAX as u32 => {
|
||||
values.push(self.tokens.pop().unwrap());
|
||||
values.push(
|
||||
self.tokens.pop().expect("The spanish Inquisition"),
|
||||
);
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
@@ -272,12 +283,16 @@ impl Parser {
|
||||
Opcode::Dw => {
|
||||
// dw can take u32 immediates
|
||||
while !self.tokens.is_empty() {
|
||||
match self.tokens.last().unwrap() {
|
||||
match self.tokens.last().expect("The spanish Inquisition") {
|
||||
Token::StringLit(_) => {
|
||||
values.push(self.tokens.pop().unwrap());
|
||||
values.push(
|
||||
self.tokens.pop().expect("The spanish Inquisition"),
|
||||
);
|
||||
}
|
||||
Token::Immediate(_) => {
|
||||
values.push(self.tokens.pop().unwrap());
|
||||
values.push(
|
||||
self.tokens.pop().expect("The spanish Inquisition"),
|
||||
);
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
@@ -294,7 +309,7 @@ impl Parser {
|
||||
if self.tokens.is_empty() {
|
||||
Err(AssembleError::UnexpectedEof)
|
||||
} else {
|
||||
Ok(self.tokens.pop().unwrap())
|
||||
Ok(self.tokens.pop().expect("The spanish Inquisition"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +317,7 @@ impl Parser {
|
||||
if self.tokens.is_empty() {
|
||||
Err(AssembleError::UnexpectedEof)
|
||||
} else {
|
||||
Ok(self.tokens.last().unwrap().clone())
|
||||
Ok(self.tokens.last().expect("The spanish Inquisition").clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,9 @@ pub fn resolve_symbols(nodes: &mut [Node]) -> Result<(), AssembleError> {
|
||||
for node in nodes.iter_mut() {
|
||||
match node.opcode() {
|
||||
Opcode::Lli => {
|
||||
if let Token::Symbol(symbol) = node.arg(0).unwrap() {
|
||||
if let Token::Symbol(symbol) =
|
||||
node.arg(0).expect("The spanish Inquisition")
|
||||
{
|
||||
if let Some(address) = symbol_table.get(&symbol) {
|
||||
node.tokens[0] = Token::Immediate(*address);
|
||||
} else {
|
||||
@@ -23,7 +25,9 @@ pub fn resolve_symbols(nodes: &mut [Node]) -> Result<(), AssembleError> {
|
||||
}
|
||||
}
|
||||
Opcode::Lui => {
|
||||
if let Token::Symbol(symbol) = node.arg(0).unwrap() {
|
||||
if let Token::Symbol(symbol) =
|
||||
node.arg(0).expect("The spanish Inquisition")
|
||||
{
|
||||
if let Some(address) = symbol_table.get(&symbol) {
|
||||
node.tokens[0] = Token::Immediate(*address);
|
||||
} else {
|
||||
@@ -38,7 +42,9 @@ pub fn resolve_symbols(nodes: &mut [Node]) -> Result<(), AssembleError> {
|
||||
| Opcode::Jge
|
||||
| Opcode::Jlt
|
||||
| Opcode::Jle => {
|
||||
if let Token::Symbol(symbol) = node.arg(0).unwrap() {
|
||||
if let Token::Symbol(symbol) =
|
||||
node.arg(0).expect("The spanish Inquisition")
|
||||
{
|
||||
if let Some(address) = symbol_table.get(&symbol) {
|
||||
node.tokens[0] = Token::Immediate(*address);
|
||||
} else {
|
||||
@@ -71,12 +77,16 @@ pub fn resolve_dependencies(mut nodes: Vec<Node>) -> Result<Vec<Node>, AssembleE
|
||||
for node in &nodes {
|
||||
if let Opcode::Include = node.opcode() {
|
||||
// we want the path, and the name
|
||||
let name = if let Token::Symbol(name) = node.arg(0).unwrap() {
|
||||
let name = if let Token::Symbol(name) =
|
||||
node.arg(0).expect("The spanish Inquisition")
|
||||
{
|
||||
name.name.clone()
|
||||
} else {
|
||||
unreachable!()
|
||||
}; //node.2.get(0).unwrap()
|
||||
let path = if let Token::StringLit(path) = node.arg(1).unwrap() {
|
||||
}; //node.2.get(0).expect("The spanish Inquisition")
|
||||
let path = if let Token::StringLit(path) =
|
||||
node.arg(1).expect("The spanish Inquisition")
|
||||
{
|
||||
path
|
||||
} else {
|
||||
unreachable!()
|
||||
|
||||
@@ -254,7 +254,7 @@ impl Executable for Instruction {
|
||||
// To populate the lower 16 bits, see LLI.
|
||||
Self::LoadUpperImmediate(a) => {
|
||||
*cpu.reg(a.r1) =
|
||||
(cpu.get(a.r1) & 0x0000_FFFF) | u32::from(a.immediate) << 16;
|
||||
(cpu.get(a.r1) & 0x0000_FFFF) | (u32::from(a.immediate) << 16);
|
||||
}
|
||||
|
||||
// Unconditionally jumps to the calculated address or direct address
|
||||
|
||||
@@ -128,7 +128,7 @@ impl Component for MemoryInspector {
|
||||
// combine all 4 bytes in the chunk into a u32
|
||||
let combined = chunk
|
||||
.iter()
|
||||
.fold(0u32, |acc, &byte| acc << 8 | u32::from(byte));
|
||||
.fold(0u32, |acc, &byte| (acc << 8) | u32::from(byte));
|
||||
|
||||
ui.monospace(format!("{combined}"));
|
||||
ui.monospace(format!("{}", Instruction::decode(combined).unwrap_or(Instruction::Nop)));
|
||||
|
||||
Reference in New Issue
Block a user