diff options
| Commit | 9c656c296827172589243d588f57f9ab2745efc6 (patch) | |
|---|---|---|
| Author | 2025-02-14 08:50:49 +0200 | |
| Committer | 2025-02-14 08:50:49 +0200 | |
| Files | ac637c6034d5e8e2dcb50f609b4b063ca7422add /assembler | |
| tonkobali-9c656c296827172589243d588f57f9ab2745efc6.tar tonkobali-9c656c296827172589243d588f57f9ab2745efc6.tar.gz tonkobali-9c656c296827172589243d588f57f9ab2745efc6.zip | ||
| Parent | 9517d3d05e86621b6c4ed58edd7f5821632e0872 (diff) | |
feat(assembler): Implement beq and mov instructions
3 files changed, 97 insertions, 2 deletions
| -rw-r--r-- | assembler/example.s | 45 | ||||
| -rw-r--r-- | assembler/src/parser/code_gen.rs | 16 | ||||
| -rw-r--r-- | assembler/src/parser/instruction.rs | 38 |
diff --git a/assembler/example.s b/assembler/example.s index 8b92ddf..b7c45ee 100644 --- a/assembler/example.s +++ b/assembler/example.s @@ -1,2 +1,43 @@ -add 331, 95 -jmp %0 +beq condition, if_true +jmp if_false + +if_true: + mov 517, %1 + jmp endif + +if_false: + mov 1952, %1 + +endif: + jmp %0 + +condition = 0 + +; 0 beq Memory(0), Instruction(2), +; 0: subleq Memory(0), Memory(0), Instruction(2), +; 1: subleq Memory(0), Memory(0), Instruction(4), +; 2: subleq Memory(0), Memory(0), Instruction(3), +; 3: subleq Memory(0), Memory(0), Instruction( 5 ), +; +; 1 jmp Instruction(4), +; 4: subleq Memory(0), Memory(0), Instruction( 10 ), +; +;if_true: +; 2 mov Constants(0), Memory(1), +; 5: subleq Memory(1), Memory(1), Instruction(6), +; 6: subleq Constants(0), Memory(0), Instruction(7), +; 7: subleq Memory(0), Memory(1), Instruction(8), +; 8: subleq Memory(0), Memory(0), Instruction(9), +; +; 3 jmp Instruction(5), +; 9: subleq Memory(0), Memory(0), Instruction( 14 ), +; +;if_false: +; 4 mov Constants(1), Memory(4), +; 10: subleq Memory(1), Memory(1), Instruction(11), +; 11: subleq Constants(1), Memory(0), Instruction(12), +; 12: subleq Memory(0), Memory(1), Instruction(13), +; 13: subleq Memory(0), Memory(0), Instruction(14), +; +; 5 jmp Memory(0), +; 14: subleq Memory(0), Memory(0), Memory(0), diff --git a/assembler/src/parser/code_gen.rs b/assembler/src/parser/code_gen.rs index 74a4fad..1c5b95a 100644 --- a/assembler/src/parser/code_gen.rs +++ b/assembler/src/parser/code_gen.rs @@ -5,6 +5,22 @@ impl Program { /* Intermediate code generation */ pub(crate) fn collapse_instructions(&mut self) { + // Fix instruction addresses + let mut offsets = Vec::new(); + for instr in &self.instructions { + offsets.push(instr.collapse_len() + offsets.last().unwrap_or(&0)); + } + offsets.insert(0, 0); + + for instr in &mut self.instructions { + for arg in &mut instr.args { + if let Address::Instruction(num) = arg { + *arg = Address::Instruction(offsets[*num]); + } + } + } + + // Collapse all instructions let mut collapsed = Vec::new(); for instr in &mut self.instructions { diff --git a/assembler/src/parser/instruction.rs b/assembler/src/parser/instruction.rs index 18d67b7..cc0523f 100644 --- a/assembler/src/parser/instruction.rs +++ b/assembler/src/parser/instruction.rs @@ -52,6 +52,20 @@ impl Instruction { "add" => { self.check_args_len(2)?; }, + "mov" => { + self.check_args_len(2)?; + + if ! self.args[1].is_location() { + return Err(ArgumentError::NotLocation); + } + }, + "beq" => { + self.check_args_len(2)?; + + if ! self.args[1].is_location() { + return Err(ArgumentError::NotLocation); + } + }, _ => return Err(ArgumentError::UnknownInstruction), } Ok(()) @@ -71,6 +85,30 @@ impl Instruction { Instruction::new_subleq(&Z, &self.args[1], &Address::Instruction(next + 2)), Instruction::new_subleq(&Z, &Z, &Address::Instruction(next + 3)), ], + "mov" => vec![ + Instruction::new_subleq(&self.args[1], &self.args[1], &Address::Instruction(next + 1)), + Instruction::new_subleq(&self.args[0], &Z, &Address::Instruction(next + 2)), + Instruction::new_subleq(&Z, &self.args[1], &Address::Instruction(next + 3)), + Instruction::new_subleq(&Z, &Z, &Address::Instruction(next + 4)), + ], + "beq" => vec![ + Instruction::new_subleq(&self.args[0], &Z, &Address::Instruction(next + 2)), + Instruction::new_subleq(&Z, &Z, &Address::Instruction(next + 4)), + Instruction::new_subleq(&Z, &Z, &Address::Instruction(next + 3)), + Instruction::new_subleq(&Z, &self.args[0], &self.args[1] ), + ], + _ => panic!("Unknown instruction {}!", self.name), + } + } + + pub(crate) fn collapse_len(&self) -> usize { + match &*self.name { + "subleq" | "jmp" + => 1, + "add" + => 3, + "mov" | "beq" + => 4, _ => panic!("Unknown instruction {}!", self.name), } } |
