Skip to content

Commit

Permalink
Added comments, refactored some code smells. Ready for full review
Browse files Browse the repository at this point in the history
  • Loading branch information
Pfat8equalsD committed Dec 3, 2024
1 parent fb6e31a commit 3d61c19
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 136 deletions.
62 changes: 35 additions & 27 deletions didasm/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bitfield! {
reg, set_reg: 10,12;
rm, set_rm: 13,15;
port, set_port: 8,15;
pc_depls, set_pc_depls: 8,15;
pc_displacement, set_pc_displacement: 8,15;
}

impl BitRange<u16> for Instruction {
Expand Down Expand Up @@ -91,6 +91,7 @@ impl BitRangeMut<u16> for Instruction {
}

impl Display for Instruction {
/// Beautiful representation is default because otherwise we would just print the u16 inside it
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
Expand All @@ -108,15 +109,15 @@ impl Display for Instruction {
#[derive(Debug)]
pub struct FullInstruction {
i: Instruction,
pub depls: Option<u16>,
pub displacement: Option<u16>,
pub imm: Option<u16>,
}

impl Display for FullInstruction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let x = self.i.0;
write!(f, "{:04X}", x)?;
if let Some(x) = self.depls {
if let Some(x) = self.displacement {
write!(f, " {:04X}", x)?;
}
if let Some(x) = self.imm {
Expand All @@ -140,11 +141,12 @@ pub struct Ir {
m: u16,
reg: Option<usize>,
rm: Option<usize>,
depls: Option<Expr>,
displacement: Option<Expr>,
imm: Option<Expr>,
}

impl Ir {
// Intermediate check step. It takes the operands and sees what mod, d, imm, displacement should look like
pub fn from_parsed_statement(ps: ParsedStatement) -> Result<Self, String> {
let ParsedStatement { mnemonic, op1, op2 } = ps;
let mut opcode: Instruction = Instruction(0);
Expand All @@ -163,7 +165,7 @@ impl Ir {
_ => (false, None),
};

let (m, rm, depls) = match (op1, op2) {
let (m, rm, displacement) = match (op1, op2) {
(Some(Reg(_)), Some(Reg(ri))) => (0b11, Some(ri.into()), None),
(Some(Imm(_)), Some(Imm(_))) => {
return Err("Operations between 2 immediate values is not allowed!".to_string());
Expand Down Expand Up @@ -215,14 +217,14 @@ impl Ir {
Some(((x as usize) & 0b1) + ((b as usize) & 0b10)),
None,
),
(Some(Indexed { x: ri, depls } | Based { b: ri, depls }), _)
| (_, Some(Indexed { x: ri, depls } | Based { b: ri, depls })) => {
(0b10, Some(ri.into()), Some(depls))
(Some(Indexed { x: ri, displacement } | Based { b: ri, displacement }), _)
| (_, Some(Indexed { x: ri, displacement } | Based { b: ri, displacement })) => {
(0b10, Some(ri.into()), Some(displacement))
}
(Some(BasedIndexed { b, x, depls }), _) | (_, Some(BasedIndexed { b, x, depls })) => (
(Some(BasedIndexed { b, x, displacement }), _) | (_, Some(BasedIndexed { b, x, displacement })) => (
0b10,
Some(((x as usize) & 0b1) + ((b as usize) & 0b10)),
Some(depls),
Some(displacement),
),
(Some(RegSumAutoincrement { b, x }), _) | (_, Some(RegSumAutoincrement { b, x })) => (
0b01,
Expand All @@ -232,9 +234,9 @@ impl Ir {
(Some(RegSumAutodecrement { b }), _) | (_, Some(RegSumAutodecrement { b })) => {
(0b01, Some(0b100 + ((b as usize >> 1) & 1)), None)
}
(Some(Direct(depls)), _) | (_, Some(Direct(depls))) => (0b01, Some(0b110), Some(depls)),
(Some(Indirect(depls)), _) | (_, Some(Indirect(depls))) => {
(0b01, Some(0b111), Some(depls))
(Some(Direct(displacement)), _) | (_, Some(Direct(displacement))) => (0b01, Some(0b110), Some(displacement)),
(Some(Indirect(displacement)), _) | (_, Some(Indirect(displacement))) => {
(0b01, Some(0b111), Some(displacement))
}
// Cases that should have been placed in _ but explicitly stated
// to utilize the pattern matching mechanism of rust for
Expand All @@ -247,20 +249,22 @@ impl Ir {
m,
reg,
rm,
depls,
displacement,
imm,
})
}

// This does not keep track of errors since they will be detected in the last stage
// It may also have erroneous results however it does not matter since the errors will be detected later
// and the program will halt
pub fn peek_word_count(&self) -> usize {
let Ir {
mnemonic,
depls,
displacement,
imm,
..
} = self;
match (mnemonic, depls, imm) {
match (mnemonic, displacement, imm) {
(General(In | Out | Pushf | Popf) | ControlFlow(Ret | Iret | Hlt), _, _) => 1,
(ConditionalJump(_), _, _) => 1,
(
Expand Down Expand Up @@ -323,12 +327,13 @@ impl Ir {
m,
reg,
rm,
depls,
displacement,
imm,
} = self;
let mut opcode: Instruction = Instruction(0);
opcode.set_op((&mnemonic).into());

// Set generic opcode fields
match &mnemonic {
General(In | Out | Pushf | Popf) | ControlFlow(Ret | Iret | Hlt) => {
opcode.set_in_type(0b1000);
Expand Down Expand Up @@ -359,7 +364,8 @@ impl Ir {
}
}

let depls = depls
// Parse the possible identifiers
let displacement = displacement
.map(|e| match e {
Expr::Id(i) => symbol_table
.get(&i)
Expand All @@ -385,15 +391,17 @@ impl Ir {
})
.transpose()?;

match (mnemonic, reg, rm, depls, imm) {
// Final check of every combination an instruction may take
// Even if technically all building blocks are available, extras will raise a semantic error
match (mnemonic, reg, rm, displacement, imm) {
(General(In | Out), None, None, None, Some(x)) => {
let x: u8 = x
.try_into()
.map_err(|_| format!("Cannot convert '{x}' into an 8 bit unsigned integer"))?;
opcode.set_port(x.into());
Ok(FullInstruction {
i: opcode,
depls: None,
displacement: None,
imm: None,
})
}
Expand All @@ -403,7 +411,7 @@ impl Ir {
(General(Pushf | Popf) | ControlFlow(Ret | Iret | Hlt), None, None, None, None) => {
Ok(FullInstruction {
i: opcode,
depls: None,
displacement: None,
imm: None,
})
}
Expand All @@ -417,7 +425,7 @@ impl Ir {
opcode.set_port((x as u8) as u16);
Ok(FullInstruction {
i: opcode,
depls: None,
displacement: None,
imm: None,
})
}
Expand All @@ -441,7 +449,7 @@ impl Ir {
opcode.set_dir(d as u16);
Ok(FullInstruction {
i: opcode,
depls: None,
displacement: None,
imm: None,
})
}
Expand Down Expand Up @@ -469,7 +477,7 @@ impl Ir {
.transpose()?;
Ok(FullInstruction {
i: opcode,
depls: x,
displacement: x,
imm: None,
})
}
Expand Down Expand Up @@ -549,7 +557,7 @@ impl Ir {

Ok(FullInstruction {
i: opcode,
depls: x,
displacement: x,
imm: y,
})
}
Expand Down Expand Up @@ -578,7 +586,7 @@ impl Ir {
.map_err(|_| format!("{imm} cannot be converted to unsigned 16 bit"))?;
Ok(FullInstruction {
i: opcode,
depls: x,
displacement: x,
imm: Some(imm),
})
}
Expand All @@ -602,7 +610,7 @@ impl Ir {
.map_err(|_| format!("{imm} cannot be converted to unsigned 16 bit"))?;
Ok(FullInstruction {
i: opcode,
depls: None,
displacement: None,
imm: Some(imm),
})
}
Expand Down
Loading

0 comments on commit 3d61c19

Please sign in to comment.