reoganised project
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "assembler"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "assembler"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lib]
|
||||
name = "assembler"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../dsa_common" }
|
||||
num_cpus = "1.17.0"
|
||||
threadpool = "1.8.1"
|
||||
Binary file not shown.
@@ -0,0 +1,265 @@
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs,
|
||||
path::{self, Path, PathBuf},
|
||||
sync::{Arc, Mutex},
|
||||
thread::{self, JoinHandle},
|
||||
};
|
||||
|
||||
use crate::assembler::{AssembleError, Token, expand_pseudo_ops, lexer, quick_hash};
|
||||
use crate::assembler::{Node, Parser, resolve_dependencies};
|
||||
// use crate::util::logging::Logger;
|
||||
|
||||
// pub fn new_assemble(path: &Path) {
|
||||
// let program = Program::new();
|
||||
// let program_ref = ProgramRef::new(program);
|
||||
|
||||
// let task = Module::build(path.to_path_buf(), program_ref.clone());
|
||||
// program_ref.add_task(task);
|
||||
|
||||
// // wait on all tasks to finish
|
||||
// for task in program_ref.get_tasks() {
|
||||
// let module = task.module.join().unwrap();
|
||||
// program_ref.add_module(module);
|
||||
// }
|
||||
// }
|
||||
|
||||
pub struct Program {
|
||||
pub main_path: PathBuf,
|
||||
registry: HashSet<u64>,
|
||||
modules: Vec<Module>,
|
||||
tasks: Vec<Task>,
|
||||
logger: Logger,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
registry: HashSet::new(),
|
||||
modules: Vec::new(),
|
||||
tasks: Vec::new(),
|
||||
main_path: PathBuf::new(),
|
||||
logger: Logger::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_task(&mut self, task: Task) {
|
||||
self.tasks.push(task);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Program {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ProgramRef {
|
||||
program: Arc<Mutex<Program>>,
|
||||
}
|
||||
|
||||
impl ProgramRef {
|
||||
#[must_use]
|
||||
pub fn new(program: Program) -> Self {
|
||||
Self {
|
||||
program: Arc::new(Mutex::new(program)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(&self, path: &Path) {
|
||||
self.program
|
||||
.lock()
|
||||
.expect("Failed to acquire program lock")
|
||||
.registry
|
||||
.insert(quick_hash(path));
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_registered(&self, path: &Path) -> bool {
|
||||
self.program
|
||||
.lock()
|
||||
.expect("Failed to acquire program lock")
|
||||
.registry
|
||||
.contains(&quick_hash(path))
|
||||
}
|
||||
|
||||
// pub fn get_tasks(&self) -> Vec<&Task> {
|
||||
// self.program.lock().unwrap().tasks.iter().collect()
|
||||
// }
|
||||
|
||||
pub fn add_task(&self, task: Task) {
|
||||
self.program
|
||||
.lock()
|
||||
.expect("Failed to acquire program lock")
|
||||
.add_task(task);
|
||||
}
|
||||
|
||||
pub fn add_module(&self, module: Module) {
|
||||
self.program
|
||||
.lock()
|
||||
.expect("Failed to acquire program lock")
|
||||
.modules
|
||||
.push(module);
|
||||
}
|
||||
|
||||
pub fn log(&self, message: &str) {
|
||||
self.program
|
||||
.lock()
|
||||
.expect("Failed to acquire program lock")
|
||||
.logger
|
||||
.log(message);
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for ProgramRef {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
program: self.program.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Module {
|
||||
pub path: PathBuf,
|
||||
pub hash: u64,
|
||||
pub nodes: Vec<Node>,
|
||||
program: ProgramRef,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
#[must_use]
|
||||
pub const fn new(
|
||||
path: PathBuf,
|
||||
hash: u64,
|
||||
nodes: Vec<Node>,
|
||||
program: ProgramRef,
|
||||
) -> Self {
|
||||
Self {
|
||||
path,
|
||||
hash,
|
||||
nodes,
|
||||
program,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(path: PathBuf, program: ProgramRef) -> Result<Task, AssembleError> {
|
||||
// Spawn a thread that creates the main function and executes the lexer and
|
||||
// parser.
|
||||
let handle = thread::spawn(move || {
|
||||
let mut module =
|
||||
Self::new(path.clone(), quick_hash(&path), Vec::new(), program.clone());
|
||||
|
||||
match module.lex() {
|
||||
Ok(tokens) => {
|
||||
module.parse(tokens);
|
||||
module.expand();
|
||||
module.prepare_dependencies();
|
||||
module
|
||||
}
|
||||
Err(why) => {
|
||||
eprintln!(
|
||||
"Error building program at path `{}`: {why}",
|
||||
path.display()
|
||||
);
|
||||
|
||||
// TODO: Find a way to make this work without panicking.
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Task { module: handle })
|
||||
}
|
||||
|
||||
fn lex(&self) -> Result<Vec<Token>, AssembleError> {
|
||||
if let Ok(path) = self.path.canonicalize() {
|
||||
self.program.log(&format!(
|
||||
"{:20} {:20} [{}]",
|
||||
"Building",
|
||||
self.get_filename(),
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
|
||||
let src = fs::read_to_string(&self.path)
|
||||
.map_err(|_| AssembleError::InvalidFile(self.path.clone()))?;
|
||||
|
||||
let file_hash = quick_hash(&self.path);
|
||||
|
||||
self.program
|
||||
.log(&format!("{:20} {:20}", "Tokenising", self.get_filename()));
|
||||
|
||||
lexer::lexer(src, file_hash)
|
||||
}
|
||||
|
||||
fn parse(&mut self, tokens: Vec<Token>) -> Result<(), AssembleError> {
|
||||
self.program
|
||||
.log(&format!("{:20} {:20}", "Parsing", self.get_filename()));
|
||||
|
||||
let parsed = Parser::parse_nodes(tokens)?;
|
||||
self.nodes = parsed;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand(&mut self) -> Result<(), AssembleError> {
|
||||
self.program
|
||||
.log(&format!("{:20} {:20}", "Expanding", self.get_filename()));
|
||||
|
||||
let expanded = expand_pseudo_ops(self.nodes.clone(), self.hash)?;
|
||||
self.nodes = expanded;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prepare_dependencies(&self) -> Result<(), AssembleError> {
|
||||
let nodes = resolve_dependencies(
|
||||
self.nodes.clone(),
|
||||
self.path.parent().expect("File should have a parent path!"),
|
||||
)?;
|
||||
|
||||
let dependencies = Parser::get_dependencies(&nodes, &self.path)?;
|
||||
|
||||
for dep in dependencies {
|
||||
if self.program.is_registered(&dep) {
|
||||
// we have already built this module!
|
||||
continue;
|
||||
}
|
||||
|
||||
self.program.register(&dep);
|
||||
|
||||
// create new module
|
||||
// add the task to the program
|
||||
|
||||
match Self::build(dep, self.program.clone()) {
|
||||
Ok(task) => self.program.add_task(task),
|
||||
Err(why) => {
|
||||
eprintln!("Error building program: {why}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Gets the filename from a [`PathBuf`].
|
||||
fn get_filename(&self) -> &str {
|
||||
self.path
|
||||
.file_name()
|
||||
.and_then(|f| f.to_str())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Gets the parent filepath from a [`PathBuf`].
|
||||
fn get_parent(&self) -> &str {
|
||||
self.path
|
||||
.parent()
|
||||
.and_then(|f| f.to_str())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Task {
|
||||
module: JoinHandle<Module>,
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
use common::{args, prelude::*};
|
||||
|
||||
use crate::assembler::model::{Node, Opcode};
|
||||
use crate::{assembler::AssembleError, expect_token};
|
||||
|
||||
fn log(message: &str) {
|
||||
println!("\x1b[32mINFO:\x1b[0m {message}");
|
||||
}
|
||||
|
||||
pub fn codegen(nodes: Vec<Node>) -> Result<Vec<Instruction>, AssembleError> {
|
||||
let mut instructions = vec![];
|
||||
|
||||
for node in nodes {
|
||||
instructions.push(build_instruction(&node)?);
|
||||
}
|
||||
|
||||
log("Assembly Successful ✅");
|
||||
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
fn build_instruction(node: &Node) -> Result<Instruction, AssembleError> {
|
||||
let opcode = node.opcode();
|
||||
let args = node.args();
|
||||
|
||||
match opcode {
|
||||
Opcode::Nop => Ok(Instruction::Nop),
|
||||
Opcode::Mov | Opcode::Movs => build_mov_instruction(opcode, &args),
|
||||
Opcode::Ldb
|
||||
| Opcode::Ldw
|
||||
| Opcode::Ldh
|
||||
| Opcode::Ldbs
|
||||
| Opcode::Ldhs
|
||||
| Opcode::Stb
|
||||
| Opcode::Stw
|
||||
| Opcode::Sth => build_memory_instruction(opcode, &args),
|
||||
Opcode::Lli | Opcode::Lui => build_load_immediate_instruction(opcode, &args),
|
||||
Opcode::Jmp
|
||||
| Opcode::Jeq
|
||||
| Opcode::Jne
|
||||
| Opcode::Jgt
|
||||
| Opcode::Jge
|
||||
| Opcode::Jlt
|
||||
| Opcode::Jle => build_jump_instruction(opcode, &args),
|
||||
Opcode::Cmp => build_compare_instruction(&args),
|
||||
Opcode::Inc | Opcode::Dec => build_inc_dec_instruction(opcode, &args),
|
||||
Opcode::Shl | Opcode::Shr => build_shift_instruction(opcode, &args),
|
||||
Opcode::Add
|
||||
| Opcode::Sub
|
||||
| Opcode::And
|
||||
| Opcode::Or
|
||||
| Opcode::Xor
|
||||
| Opcode::Nand
|
||||
| Opcode::Nor
|
||||
| Opcode::Xnor => build_arithmetic_instruction(opcode, &args),
|
||||
Opcode::AddI | Opcode::SubI => {
|
||||
build_arithmetic_immediate_instruction(opcode, &args)
|
||||
}
|
||||
Opcode::Not => build_not_instruction(&args),
|
||||
Opcode::Int => build_interrupt_instruction(&args),
|
||||
Opcode::Irt => Ok(Instruction::IntReturn),
|
||||
Opcode::Hlt => Ok(Instruction::Halt),
|
||||
Opcode::Data => build_data_instruction(&args),
|
||||
Opcode::Segment => build_segment_instruction(&args),
|
||||
// These pseudo-instructions should have been expanded!
|
||||
Opcode::Db
|
||||
| Opcode::Dh
|
||||
| Opcode::Dw
|
||||
| Opcode::Resb
|
||||
| Opcode::Resh
|
||||
| Opcode::Resw
|
||||
| Opcode::Push
|
||||
| Opcode::Pop
|
||||
| Opcode::Lwi
|
||||
| Opcode::Include
|
||||
| Opcode::Call
|
||||
| Opcode::Return
|
||||
| Opcode::Pusha
|
||||
| Opcode::Popa => Err(AssembleError::InvalidArg),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_mov_instruction(
|
||||
opcode: Opcode,
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(src_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(dest_token) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let src = expect_token!(src_token, Register)?;
|
||||
let dest = expect_token!(dest_token, Register)?;
|
||||
|
||||
match opcode {
|
||||
Opcode::Mov => Ok(Instruction::Mov(args!(R, sr1: src, dr: dest))),
|
||||
Opcode::Movs => Ok(Instruction::MovSigned(args!(R, sr1: src, dr: dest))),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_memory_instruction(
|
||||
opcode: Opcode,
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(base_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(dest_token) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
let Some(offset_token) = args.get(2) else {
|
||||
return Err(AssembleError::MissingArgument(2));
|
||||
};
|
||||
|
||||
let base = expect_token!(base_token, Register)?;
|
||||
let dest = expect_token!(dest_token, Register)?;
|
||||
let offset = expect_token!(offset_token, Immediate)?;
|
||||
let instruction_args = args!(I, immediate: offset as u16, r1: base, r2: dest);
|
||||
|
||||
match opcode {
|
||||
Opcode::Ldb => Ok(Instruction::LoadByte(instruction_args)),
|
||||
Opcode::Ldw => Ok(Instruction::LoadWord(instruction_args)),
|
||||
Opcode::Ldh => Ok(Instruction::LoadHalfword(instruction_args)),
|
||||
Opcode::Ldbs => Ok(Instruction::LoadByteSigned(instruction_args)),
|
||||
Opcode::Ldhs => Ok(Instruction::LoadHalfwordSigned(instruction_args)),
|
||||
Opcode::Stb => Ok(Instruction::StoreByte(instruction_args)),
|
||||
Opcode::Stw => Ok(Instruction::StoreWord(instruction_args)),
|
||||
Opcode::Sth => Ok(Instruction::StoreHalfword(instruction_args)),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_load_immediate_instruction(
|
||||
opcode: Opcode,
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(value_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(dest_token) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let value = expect_token!(value_token, Immediate)?;
|
||||
let dest = expect_token!(dest_token, Register)?;
|
||||
|
||||
match opcode {
|
||||
Opcode::Lli => {
|
||||
let instruction_args = args!(I, immediate: value as u16, r1: dest);
|
||||
Ok(Instruction::LoadLowerImmediate(instruction_args))
|
||||
}
|
||||
Opcode::Lui => {
|
||||
let upper_value = value >> 16;
|
||||
let instruction_args = args!(I, immediate: upper_value as u16, r1: dest);
|
||||
Ok(Instruction::LoadUpperImmediate(instruction_args))
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_jump_instruction(
|
||||
opcode: Opcode,
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(address_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(offset_token) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let address = expect_token!(address_token, Immediate)?;
|
||||
let offset = expect_token!(offset_token, Register)?;
|
||||
let instruction_args = args!(I, immediate: address as u16, r1: offset);
|
||||
|
||||
match opcode {
|
||||
Opcode::Jmp => Ok(Instruction::Jump(instruction_args)),
|
||||
Opcode::Jeq => Ok(Instruction::JumpEq(instruction_args)),
|
||||
Opcode::Jne => Ok(Instruction::JumpNeq(instruction_args)),
|
||||
Opcode::Jgt => Ok(Instruction::JumpGt(instruction_args)),
|
||||
Opcode::Jge => Ok(Instruction::JumpGe(instruction_args)),
|
||||
Opcode::Jlt => Ok(Instruction::JumpLt(instruction_args)),
|
||||
Opcode::Jle => Ok(Instruction::JumpLe(instruction_args)),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_compare_instruction(
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(left_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(right_token) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let left = expect_token!(left_token, Register)?;
|
||||
let right = expect_token!(right_token, Register)?;
|
||||
Ok(Instruction::Compare(args!(R, sr1: left, sr2: right)))
|
||||
}
|
||||
|
||||
fn build_inc_dec_instruction(
|
||||
opcode: Opcode,
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(reg_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let reg = expect_token!(reg_token, Register)?;
|
||||
match opcode {
|
||||
Opcode::Inc => Ok(Instruction::Increment(args!(R, sr1: reg))),
|
||||
Opcode::Dec => Ok(Instruction::Decrement(args!(R, sr1: reg))),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_shift_instruction(
|
||||
opcode: Opcode,
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(src_reg) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(r_shamt) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(i_shamt) = args.get(2) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
let Some(dest_reg) = args.get(3) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let src = expect_token!(src_reg, Register)?;
|
||||
let r_shamt = expect_token!(r_shamt, Register)?;
|
||||
let i_shamt = expect_token!(i_shamt, Immediate)? as u8;
|
||||
let dest = expect_token!(dest_reg, Register)?;
|
||||
|
||||
match opcode {
|
||||
Opcode::Shl => Ok(Instruction::ShiftLeft(
|
||||
args!(R, sr1: src, sr2: r_shamt, shamt: i_shamt, dr: dest),
|
||||
)),
|
||||
Opcode::Shr => Ok(Instruction::ShiftRight(
|
||||
args!(R, sr1: src, sr2: r_shamt, shamt: i_shamt, dr: dest),
|
||||
)),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_arithmetic_instruction(
|
||||
opcode: Opcode,
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(left_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(right_token) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
let Some(dest_token) = args.get(2) else {
|
||||
return Err(AssembleError::MissingArgument(2));
|
||||
};
|
||||
|
||||
let left = expect_token!(left_token, Register)?;
|
||||
let right = expect_token!(right_token, Register)?;
|
||||
let dest = expect_token!(dest_token, Register)?;
|
||||
let instruction_args = args!(R, sr1: left, sr2: right, dr: dest);
|
||||
|
||||
match opcode {
|
||||
Opcode::Add => Ok(Instruction::Add(instruction_args)),
|
||||
Opcode::Sub => Ok(Instruction::Sub(instruction_args)),
|
||||
Opcode::And => Ok(Instruction::And(instruction_args)),
|
||||
Opcode::Or => Ok(Instruction::Or(instruction_args)),
|
||||
Opcode::Xor => Ok(Instruction::Xor(instruction_args)),
|
||||
Opcode::Nand => Ok(Instruction::Nand(instruction_args)),
|
||||
Opcode::Nor => Ok(Instruction::Nor(instruction_args)),
|
||||
Opcode::Xnor => Ok(Instruction::Xnor(instruction_args)),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_arithmetic_immediate_instruction(
|
||||
opcode: Opcode,
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(reg_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(immediate_token) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
let Some(dest_token) = args.get(2) else {
|
||||
return Err(AssembleError::MissingArgument(2));
|
||||
};
|
||||
|
||||
let reg = expect_token!(reg_token, Register)?;
|
||||
let immediate = expect_token!(immediate_token, Immediate)? as u16;
|
||||
let dest = expect_token!(dest_token, Register)?;
|
||||
let instruction_args = args!(I, immediate: immediate, r1: reg, r2: dest);
|
||||
|
||||
match opcode {
|
||||
Opcode::AddI => Ok(Instruction::AddImmediate(instruction_args)),
|
||||
Opcode::SubI => Ok(Instruction::SubImmediate(instruction_args)),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_not_instruction(
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(reg_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
let Some(dest_token) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let reg = expect_token!(reg_token, Register)?;
|
||||
let dest = expect_token!(dest_token, Register)?;
|
||||
Ok(Instruction::Not(args!(R, sr1: reg, dr: dest)))
|
||||
}
|
||||
|
||||
fn build_interrupt_instruction(
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(code_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let code = expect_token!(code_token, Immediate)? as u8;
|
||||
Ok(Instruction::Interrupt(Interrupt::Software(code)))
|
||||
}
|
||||
|
||||
fn build_data_instruction(
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(immediate_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let immediate = expect_token!(immediate_token, Immediate)?;
|
||||
Ok(Instruction::Data(immediate))
|
||||
}
|
||||
|
||||
fn build_segment_instruction(
|
||||
args: &[crate::assembler::model::Token],
|
||||
) -> Result<Instruction, AssembleError> {
|
||||
let Some(immediate_token) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let immediate = expect_token!(immediate_token, Immediate)?;
|
||||
Ok(Instruction::Segment(immediate))
|
||||
}
|
||||
@@ -0,0 +1,368 @@
|
||||
use common::prelude::Register;
|
||||
|
||||
use crate::assembler::model::{Node, Opcode, Token};
|
||||
use crate::{assembler::AssembleError, expect_token, expect_type, node};
|
||||
|
||||
pub fn expand_pseudo_ops(
|
||||
mut nodes: Vec<Node>,
|
||||
module: u64,
|
||||
) -> Result<Vec<Node>, AssembleError> {
|
||||
let mut result = Vec::<Node>::with_capacity(nodes.len());
|
||||
|
||||
for node in &mut nodes {
|
||||
if try_expand(node.clone(), &mut result, module).is_err() {
|
||||
result.push(node.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn try_expand(
|
||||
node: Node,
|
||||
result: &mut Vec<Node>,
|
||||
_module: u64,
|
||||
) -> Result<(), AssembleError> {
|
||||
match node.opcode() {
|
||||
Opcode::Push => expand_push(&node, result)?,
|
||||
Opcode::Pop => expand_pop(&node, result)?,
|
||||
Opcode::Pusha => expand_pusha(&node, result)?,
|
||||
Opcode::Popa => expand_popa(&node, result)?,
|
||||
Opcode::Call => expand_call(&node, result)?,
|
||||
Opcode::Return => expand_return(&node, result),
|
||||
Opcode::Ldb | Opcode::Ldbs | Opcode::Ldh | Opcode::Ldhs | Opcode::Ldw => {
|
||||
expand_ldx(&node, result)?;
|
||||
}
|
||||
Opcode::Stb | Opcode::Sth | Opcode::Stw => expand_stx(&node, result)?,
|
||||
|
||||
Opcode::Lwi => expand_lwi(&node, result)?,
|
||||
Opcode::Resb | Opcode::Resh | Opcode::Resw => expand_resx(&node, result)?,
|
||||
Opcode::Db | Opcode::Dh | Opcode::Dw => expand_dx(&node, result)?,
|
||||
_ => result.push(node),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_push(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let label = current.label();
|
||||
let Ok(arg0) = current.arg(0) else {
|
||||
return Err(AssembleError::Generic);
|
||||
};
|
||||
|
||||
let reg = expect_type!(arg0, Register)?;
|
||||
let spr = Token::Register(Register::Spr);
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(label, Opcode::SubI, spr, 4, spr),
|
||||
node!(None, Opcode::Stw, reg, spr, 0),
|
||||
]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_pusha(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let label = current.label();
|
||||
let Ok(arg0) = current.arg(0) else {
|
||||
return Err(AssembleError::Generic);
|
||||
};
|
||||
|
||||
let count = expect_token!(arg0, Immediate)?;
|
||||
let spr = Token::Register(Register::Spr);
|
||||
let registers: Vec<Register> = Register::general();
|
||||
|
||||
nodes.push(node!(
|
||||
label,
|
||||
Opcode::SubI,
|
||||
spr,
|
||||
Token::Immediate(count * 4),
|
||||
spr
|
||||
));
|
||||
|
||||
nodes.extend((0..count).rev().map(|i| {
|
||||
node!(
|
||||
None,
|
||||
Opcode::Stw,
|
||||
Token::Register(registers[i as usize]),
|
||||
spr,
|
||||
Token::Immediate(i * 4)
|
||||
)
|
||||
}));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_popa(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let label = current.label();
|
||||
|
||||
let Ok(arg0) = current.arg(0) else {
|
||||
return Err(AssembleError::Generic);
|
||||
};
|
||||
|
||||
let count = expect_token!(arg0, Immediate)?;
|
||||
let spr = Token::Register(Register::Spr);
|
||||
let registers: Vec<Register> = Register::general();
|
||||
|
||||
nodes.extend((0..count).rev().map(|i| {
|
||||
node!(
|
||||
{ if i == 0 { label.clone() } else { None } },
|
||||
Opcode::Ldw,
|
||||
spr,
|
||||
Token::Register(registers[i as usize]),
|
||||
Token::Immediate(i * 4)
|
||||
)
|
||||
}));
|
||||
|
||||
nodes.push(node!(
|
||||
None,
|
||||
Opcode::AddI,
|
||||
spr,
|
||||
Token::Immediate(count * 4),
|
||||
spr
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_call(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let label = current.label();
|
||||
|
||||
let Ok(arg0) = current.arg(0) else {
|
||||
return Err(AssembleError::Generic);
|
||||
};
|
||||
|
||||
let addr = expect_type!(arg0, Symbol)?;
|
||||
let spr = Token::Register(Register::Spr);
|
||||
let pcx = Token::Register(Register::Pcx);
|
||||
let zero = Token::Register(Register::Zero);
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(label, Opcode::SubI, spr, 4, spr),
|
||||
node!(None, Opcode::Stw, pcx, spr, 0),
|
||||
node!(None, Opcode::Jmp, addr, zero),
|
||||
]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_return(current: &Node, nodes: &mut Vec<Node>) {
|
||||
let label = current.label();
|
||||
let spr = Token::Register(Register::Spr);
|
||||
let ret = Token::Register(Register::Ret);
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(label, Opcode::Ldw, spr, ret, 0),
|
||||
node!(None, Opcode::AddI, spr, 4, spr),
|
||||
node!(None, Opcode::Jmp, 4, ret),
|
||||
]);
|
||||
}
|
||||
|
||||
fn expand_pop(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let label = current.label();
|
||||
|
||||
let Ok(arg0) = current.arg(0) else {
|
||||
return Err(AssembleError::Generic);
|
||||
};
|
||||
|
||||
let reg = expect_type!(arg0, Register)?;
|
||||
let spr = Token::Register(Register::Spr);
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(label, Opcode::Ldw, spr, reg, 0),
|
||||
node!(None, Opcode::AddI, spr, 4, spr),
|
||||
]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_ldx(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let opcode = current.opcode();
|
||||
let args: Vec<Token> = current.args().into_iter().take(3).collect();
|
||||
|
||||
let Some(name) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let Some(reg) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let Some(offset) = args.get(2) else {
|
||||
return Err(AssembleError::MissingArgument(2));
|
||||
};
|
||||
|
||||
let name = expect_type!(name, Symbol)?;
|
||||
let reg = expect_type!(reg, Register)?;
|
||||
let offset = expect_type!(offset, Immediate)?;
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(current.label(), Opcode::Lli, name, reg),
|
||||
node!(None, Opcode::Lui, name, reg),
|
||||
node!(None, opcode, reg, reg, offset),
|
||||
]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_stx(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let opcode = current.opcode();
|
||||
|
||||
let args: Vec<Token> = current.args().into_iter().take(3).collect();
|
||||
|
||||
let Some(base) = args.first() else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let Some(dest) = args.get(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let Some(offset) = args.get(2) else {
|
||||
return Err(AssembleError::MissingArgument(2));
|
||||
};
|
||||
|
||||
let base = expect_type!(base, Register)?;
|
||||
let dest = expect_type!(dest, Symbol)?;
|
||||
let offset = expect_type!(offset, Immediate)?;
|
||||
let temp = Token::Register(Register::Acc);
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(current.label(), Opcode::Lli, dest, temp),
|
||||
node!(None, Opcode::Lui, dest, temp),
|
||||
node!(None, opcode, base, temp, offset),
|
||||
]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_lwi(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let Ok(val) = current.arg(0) else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let Ok(reg) = current.arg(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let val = expect_type!(val, Symbol, Immediate)?;
|
||||
let reg = expect_type!(reg, Register)?;
|
||||
|
||||
nodes.extend(vec![
|
||||
node!(current.label(), Opcode::Lli, val, reg),
|
||||
node!(None, Opcode::Lui, val, reg),
|
||||
]);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_resx(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let Ok(region_label) = current.arg(0) else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let Ok(size) = current.arg(1) else {
|
||||
return Err(AssembleError::MissingArgument(1));
|
||||
};
|
||||
|
||||
let region_label = expect_token!(region_label, Symbol)?;
|
||||
let size = expect_token!(size, Immediate)?;
|
||||
|
||||
let units_per = match current.opcode() {
|
||||
Opcode::Resb => 4,
|
||||
Opcode::Resh => 2,
|
||||
Opcode::Resw => 1,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let mut buffer = vec![];
|
||||
// push the inital node with the label
|
||||
for _ in 0..size.div_ceil(units_per) {
|
||||
// push the rest of the nodes
|
||||
buffer.push(node!(None, Opcode::Data, 0));
|
||||
}
|
||||
buffer[0].symbol = Some(region_label);
|
||||
nodes.extend(buffer);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expand_dx(current: &Node, nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let Ok(region_label) = current.arg(0) else {
|
||||
return Err(AssembleError::MissingArgument(0));
|
||||
};
|
||||
|
||||
let region_label = expect_token!(region_label, Symbol)?;
|
||||
let size = match current.opcode() {
|
||||
Opcode::Db => 4,
|
||||
Opcode::Dh => 2,
|
||||
Opcode::Dw => 1,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let mut buffer = vec![];
|
||||
|
||||
let mut args = current.args();
|
||||
let _label = args.remove(0);
|
||||
|
||||
for word in process_dx_data(args, size)? {
|
||||
buffer.push(node!(None, Opcode::Data, Token::Immediate(word)));
|
||||
}
|
||||
buffer[0].symbol = Some(region_label);
|
||||
|
||||
nodes.extend(buffer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_dx_data(args: Vec<Token>, size: usize) -> Result<Vec<u32>, AssembleError> {
|
||||
assert!(matches!(size, 1 | 2 | 4));
|
||||
|
||||
let mut buffer = Vec::<u8>::new();
|
||||
|
||||
// Process each token
|
||||
for token in args {
|
||||
match token {
|
||||
Token::StringLit(mut s) => {
|
||||
s.push('\0');
|
||||
// Split string into chars and write as bytes
|
||||
for ch in s.chars() {
|
||||
// Convert char to bytes (UTF-8 encoding)
|
||||
let mut char_buf = [0u8; 4];
|
||||
let char_bytes = ch.encode_utf8(&mut char_buf);
|
||||
buffer.extend_from_slice(char_bytes.as_bytes());
|
||||
}
|
||||
}
|
||||
Token::Immediate(value) => {
|
||||
// Split u32 into bytes (little-endian)
|
||||
buffer.extend_from_slice(&value.to_be_bytes());
|
||||
}
|
||||
_ => {
|
||||
return Err(AssembleError::Generic);
|
||||
}
|
||||
}
|
||||
|
||||
// Pad buffer to alignment boundary with zeros
|
||||
let remainder = buffer.len() % size;
|
||||
if remainder != 0 {
|
||||
let padding = size - remainder;
|
||||
buffer.resize(buffer.len() + padding, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert byte buffer to u32 chunks
|
||||
// Pad final buffer to u32 boundary if needed
|
||||
let remainder = buffer.len() % 4;
|
||||
if remainder != 0 {
|
||||
let padding = 4 - remainder;
|
||||
buffer.resize(buffer.len() + padding, 0);
|
||||
}
|
||||
|
||||
// Convert bytes to u32s efficiently using chunks_exact
|
||||
let result = buffer
|
||||
.chunks_exact(4)
|
||||
.map(|chunk| {
|
||||
// Convert 4 bytes to u32 (little-endian)
|
||||
u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]])
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::assembler::AssembleError;
|
||||
use crate::assembler::model::{Module, Opcode, Symbol, Token};
|
||||
use common::prelude::Register;
|
||||
|
||||
pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleError> {
|
||||
let mut tokens = Vec::new();
|
||||
|
||||
let lines = program.lines();
|
||||
let mut literal = String::new();
|
||||
|
||||
for line in lines {
|
||||
for (i, token) in line.split_whitespace().enumerate() {
|
||||
if token.starts_with("//") {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Some(stripped) = token.strip_prefix('"') {
|
||||
literal.push_str(stripped);
|
||||
}
|
||||
|
||||
if !literal.is_empty() {
|
||||
if !token.starts_with('"') {
|
||||
if i > 0 {
|
||||
literal.push(' ');
|
||||
}
|
||||
literal.push_str(token);
|
||||
}
|
||||
|
||||
if token.ends_with('"') {
|
||||
literal.pop(); // remove the closing quote
|
||||
|
||||
tokens.push(Token::StringLit(literal));
|
||||
literal = String::new();
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
let token = token.trim_end_matches(',');
|
||||
if token.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(token) = parse_register(token)? {
|
||||
tokens.push(token);
|
||||
} else if let Some(token) = parse_opcode(token)? {
|
||||
tokens.push(token);
|
||||
} else if let Some(token) = parse_hex(token)? {
|
||||
tokens.push(token);
|
||||
} else if let Some(token) = parse_octal(token)? {
|
||||
tokens.push(token);
|
||||
} else if let Some(token) = parse_binary(token)? {
|
||||
tokens.push(token);
|
||||
} else if let Some(token) = parse_decimal(token)? {
|
||||
tokens.push(token);
|
||||
} else if let Some(token) = parse_label(token, module)? {
|
||||
tokens.push(token);
|
||||
} else if let Some(token) = parse_symbol(token, module)? {
|
||||
tokens.push(token);
|
||||
} else {
|
||||
return Err(AssembleError::Generic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// println!("{:#?}", tokens);
|
||||
|
||||
Ok(tokens)
|
||||
}
|
||||
pub fn parse_register(token: &str) -> Result<Option<Token>, AssembleError> {
|
||||
Ok(Register::try_from(token).map(Token::Register).ok())
|
||||
}
|
||||
|
||||
pub fn parse_opcode(token: &str) -> Result<Option<Token>, AssembleError> {
|
||||
if Opcode::OPCODES.contains(&token) {
|
||||
Ok(Some(Token::Opcode(Opcode::from_str(token).expect(
|
||||
"Opcode::from_str failed for a valid opcode token",
|
||||
))))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_hex(token: &str) -> Result<Option<Token>, AssembleError> {
|
||||
if (token.len() < 3) | !token.starts_with("0x") {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let Some(lit) = &token.get(2..) else {
|
||||
return Err(AssembleError::InvalidArg);
|
||||
};
|
||||
|
||||
u32::from_str_radix(lit, 16).map_or(Err(AssembleError::Generic), |value| {
|
||||
Ok(Some(Token::Immediate(value)))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_octal(token: &str) -> Result<Option<Token>, AssembleError> {
|
||||
if (token.len() < 3) | !token.starts_with("0o") {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let Some(lit) = &token.get(2..) else {
|
||||
return Err(AssembleError::InvalidArg);
|
||||
};
|
||||
|
||||
u32::from_str_radix(lit, 8).map_or(Err(AssembleError::Generic), |value| {
|
||||
Ok(Some(Token::Immediate(value)))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_binary(token: &str) -> Result<Option<Token>, AssembleError> {
|
||||
if (token.len() < 3) | !token.starts_with("0b") {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let Some(lit) = &token.get(2..) else {
|
||||
return Err(AssembleError::InvalidArg);
|
||||
};
|
||||
|
||||
u32::from_str_radix(lit, 2).map_or(Err(AssembleError::Generic), |value| {
|
||||
Ok(Some(Token::Immediate(value)))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_decimal(token: &str) -> Result<Option<Token>, AssembleError> {
|
||||
let Ok(tok) = token.parse::<u32>() else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
Ok(Some(Token::Immediate(tok)))
|
||||
}
|
||||
|
||||
pub fn parse_label(token: &str, module: u64) -> Result<Option<Token>, AssembleError> {
|
||||
if token.ends_with(':') {
|
||||
Ok(Some(Token::Symbol(Symbol {
|
||||
name: token[0..token.len() - 1].to_string(),
|
||||
module: Module::Resolved(module),
|
||||
})))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_symbol(token: &str, module: u64) -> Result<Option<Token>, AssembleError> {
|
||||
let Some(tokc) = token.chars().next() else {
|
||||
return Err(AssembleError::Generic); // TODO: What is this error?
|
||||
};
|
||||
|
||||
if tokc.is_numeric() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let mut split = token.splitn(2, "::");
|
||||
let Some(symbol1) = split.next() else {
|
||||
return Err(AssembleError::InvalidArg);
|
||||
};
|
||||
let symbol1 = symbol1.to_string();
|
||||
|
||||
if let Some(symbol2) = split.next() {
|
||||
Ok(Some(Token::Symbol(Symbol {
|
||||
name: symbol2.to_string(),
|
||||
module: Module::Unresolved(symbol1),
|
||||
})))
|
||||
} else {
|
||||
Ok(Some(Token::Symbol(Symbol {
|
||||
name: symbol1,
|
||||
module: Module::Resolved(module),
|
||||
})))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
//! Macros used throughout the assembler
|
||||
|
||||
use crate::assembler::model::{Node, Opcode, Symbol, Token};
|
||||
/// Parse DSA assembly code with optional formatting
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rs
|
||||
/// use assembler::macros::dsa;
|
||||
/// // With formatting:
|
||||
/// let nodes = dsa!(hash, "mov r1, {}", 42)?;
|
||||
///
|
||||
/// // Without formatting:
|
||||
/// let nodes = dsa!(hash, "mov r1, 42")?;
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! dsa {
|
||||
// Version with formatting arguments
|
||||
($hash:expr, $input:expr, $($args:expr),+) => {{
|
||||
let input = format!($input, $($args),+);
|
||||
let tokens = $crate::lexer::lexer(input, $hash)?;
|
||||
let parsed = $crate::parser::Parser::parse_nodes(tokens)?;
|
||||
parsed
|
||||
}};
|
||||
// Version without formatting
|
||||
($hash:expr, $input:expr) => {{
|
||||
let input = String::from($input);
|
||||
let tokens = $crate::lexer::lexer(input, $hash)?;
|
||||
let parsed = $crate::parser::Parser::parse_nodes(tokens)?;
|
||||
parsed
|
||||
}};
|
||||
}
|
||||
|
||||
/// Creates a new Node with the given symbol, opcode, and tokens
|
||||
#[macro_export]
|
||||
macro_rules! node {
|
||||
($symbol: expr, $opcode: expr, args: $tokens: expr) => {
|
||||
$crate::assembler::model::Node::new($symbol.clone(), $opcode.clone(), $tokens.clone())
|
||||
};
|
||||
|
||||
($symbol: expr, $opcode: expr, $($tokens: expr),+) => {
|
||||
$crate::assembler::model::Node::new(
|
||||
$symbol.clone(),
|
||||
$opcode.clone(),
|
||||
vec![$(node!(@convert_token $tokens)),+]
|
||||
)
|
||||
};
|
||||
|
||||
($symbol: expr, $opcode: expr) => {
|
||||
$crate::assembler::model::Node::new(
|
||||
$symbol.clone(),
|
||||
$opcode.clone(),
|
||||
Vec::new()
|
||||
)
|
||||
};
|
||||
|
||||
(@convert_token $token: literal) => {
|
||||
$crate::assembler::model::Token::Immediate($token)
|
||||
};
|
||||
|
||||
(@convert_token $token: expr) => {
|
||||
$token.clone()
|
||||
};
|
||||
}
|
||||
|
||||
/// Extracts a specific token type from a token
|
||||
#[macro_export]
|
||||
macro_rules! expect_token {
|
||||
($token:expr, Symbol) => {
|
||||
match $token {
|
||||
$crate::assembler::model::Token::Symbol(value) => Ok(value.clone()),
|
||||
other => Err($crate::assembler::AssembleError::UnexpectedToken(
|
||||
other.clone(),
|
||||
$crate::assembler::model::TokenType::Symbol,
|
||||
)),
|
||||
}
|
||||
};
|
||||
($token:expr, Register) => {
|
||||
match $token {
|
||||
$crate::assembler::model::Token::Register(value) => Ok(value.clone()),
|
||||
other => Err($crate::assembler::AssembleError::UnexpectedToken(
|
||||
other.clone(),
|
||||
$crate::assembler::model::TokenType::Register,
|
||||
)),
|
||||
}
|
||||
};
|
||||
($token:expr, Immediate) => {
|
||||
match $token {
|
||||
$crate::assembler::model::Token::Immediate(value) => Ok(value.clone()),
|
||||
other => Err($crate::assembler::AssembleError::UnexpectedToken(
|
||||
other.clone(),
|
||||
$crate::assembler::model::TokenType::Immediate,
|
||||
)),
|
||||
}
|
||||
};
|
||||
($token:expr, StringLit) => {
|
||||
match $token {
|
||||
$crate::assembler::model::Token::StringLit(value) => Ok(value.clone()),
|
||||
other => Err($crate::assembler::AssembleError::UnexpectedToken(
|
||||
other.clone(),
|
||||
$crate::assembler::model::TokenType::StringLit,
|
||||
)),
|
||||
}
|
||||
};
|
||||
($token:expr, Opcode) => {
|
||||
match $token {
|
||||
$crate::assembler::model::Token::Opcode(value) => Ok(value.clone()),
|
||||
other => Err($crate::assembler::AssembleError::UnexpectedToken(
|
||||
other.clone(),
|
||||
$crate::assembler::model::TokenType::Opcode,
|
||||
)),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Checks if a token matches any of the specified types
|
||||
#[macro_export]
|
||||
macro_rules! expect_type {
|
||||
($token:expr, $($variant:ident),+) => {{
|
||||
let token = $token;
|
||||
match &token {
|
||||
$(
|
||||
$crate::assembler::model::Token::$variant(_) => Ok(token.clone()),
|
||||
)+
|
||||
other => {
|
||||
let expected_type = expect_type!(@get_first_type $($variant),+);
|
||||
Err($crate::assembler::AssembleError::UnexpectedToken(
|
||||
other.clone().clone(),
|
||||
expected_type,
|
||||
))
|
||||
}
|
||||
}
|
||||
}};
|
||||
|
||||
(@get_first_type Symbol $(, $rest:ident)*) => { $crate::assembler::model::TokenType::Symbol };
|
||||
(@get_first_type Register $(, $rest:ident)*) => { $crate::assembler::model::TokenType::Register };
|
||||
(@get_first_type Immediate $(, $rest:ident)*) => { $crate::assembler::model::TokenType::Immediate };
|
||||
(@get_first_type StringLit $(, $rest:ident)*) => { $crate::assembler::model::TokenType::StringLit };
|
||||
(@get_first_type Opcode $(, $rest:ident)*) => { $crate::assembler::model::TokenType::Opcode };
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
#![allow(dead_code, unused)]
|
||||
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fmt, fs,
|
||||
hash::{DefaultHasher, Hash, Hasher},
|
||||
path::{Path, PathBuf},
|
||||
sync::{
|
||||
Arc, Mutex,
|
||||
mpsc::{self, Receiver, Sender},
|
||||
},
|
||||
thread,
|
||||
};
|
||||
|
||||
use common::{
|
||||
build::{BuildError, Builder},
|
||||
logging::{LogReceiver, Logger},
|
||||
prelude::Instruction,
|
||||
};
|
||||
|
||||
// Module declarations
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
|
||||
#[allow(clippy::module_inception)]
|
||||
// pub mod assembler;
|
||||
pub mod codegen;
|
||||
pub mod expand;
|
||||
pub mod lexer;
|
||||
pub mod model;
|
||||
pub mod parser;
|
||||
pub mod resolver;
|
||||
|
||||
// Re-exports
|
||||
pub use self::{
|
||||
codegen::codegen,
|
||||
expand::expand_pseudo_ops,
|
||||
lexer::lexer,
|
||||
model::{Module, Node, Opcode, Symbol, Token, TokenType},
|
||||
parser::{Parser, Program},
|
||||
resolver::{create_sections, resolve_dependencies, resolve_symbols},
|
||||
};
|
||||
|
||||
pub struct Assembler {
|
||||
src_path: PathBuf,
|
||||
result_tx: mpsc::Sender<Result<Vec<u8>, AssembleError>>,
|
||||
result_rx: Option<mpsc::Receiver<Result<Vec<u8>, AssembleError>>>,
|
||||
is_running: bool,
|
||||
|
||||
logs_rx: LogReceiver,
|
||||
}
|
||||
|
||||
impl From<AssembleError> for BuildError {
|
||||
fn from(err: AssembleError) -> Self {
|
||||
Self::Generic(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Builder for Assembler {
|
||||
type Output = Vec<u8>;
|
||||
|
||||
fn logs(&self) -> Vec<String> {
|
||||
self.logs_rx.logs()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
fn new(src_path: impl Into<PathBuf>) -> Self {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
Self {
|
||||
src_path: src_path.into(),
|
||||
result_tx: tx,
|
||||
result_rx: Some(rx),
|
||||
is_running: false,
|
||||
|
||||
// for logging
|
||||
logs_rx: LogReceiver::new(true),
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the compilation process in a separate thread
|
||||
fn start(&mut self) {
|
||||
if self.is_running {
|
||||
return;
|
||||
}
|
||||
|
||||
let src = self.src_path.clone();
|
||||
let tx = self.result_tx.clone();
|
||||
let logger = self.logs_rx.logger();
|
||||
|
||||
thread::spawn(move || {
|
||||
if let Ok(res) = assemble(&src, &logger) {
|
||||
let buffer: Vec<u8> = res
|
||||
.iter()
|
||||
.flat_map(|instruction| instruction.encode().to_be_bytes())
|
||||
.collect();
|
||||
tx.send(Ok(buffer))
|
||||
.expect("Failed to send compilation result from worker thread");
|
||||
}
|
||||
});
|
||||
|
||||
self.is_running = true;
|
||||
}
|
||||
|
||||
fn poll(&mut self) -> Option<Result<Self::Output, common::build::BuildError>> {
|
||||
if !self.is_running {
|
||||
return None;
|
||||
}
|
||||
|
||||
match self
|
||||
.result_rx
|
||||
.as_ref()
|
||||
.expect("result_rx should be Some while compilation is running")
|
||||
.try_recv()
|
||||
{
|
||||
Ok(result) => {
|
||||
self.is_running = false;
|
||||
Some(result.map_err(std::convert::Into::into))
|
||||
}
|
||||
Err(mpsc::TryRecvError::Empty) => None,
|
||||
Err(mpsc::TryRecvError::Disconnected) => {
|
||||
self.is_running = false;
|
||||
Some(Err(BuildError::Generic(String::from(
|
||||
"Compilation terminated before a result was returned",
|
||||
))))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Block until compilation is complete and return the result
|
||||
fn output(&mut self) -> Result<Self::Output, common::build::BuildError> {
|
||||
if let Ok(result) = self
|
||||
.result_rx
|
||||
.take()
|
||||
.expect("result_rx should be Some while waiting for compilation result")
|
||||
.recv()
|
||||
{
|
||||
self.is_running = false;
|
||||
result.map_err(std::convert::Into::into)
|
||||
} else {
|
||||
self.is_running = false;
|
||||
Err(BuildError::Generic(String::from(
|
||||
"Compilation terminated before a result was returned",
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Assembler {}
|
||||
|
||||
fn assemble(src: &Path, logger: &Logger) -> Result<Vec<Instruction>, AssembleError> {
|
||||
let mut modules = HashSet::new();
|
||||
let mut program = Program::new();
|
||||
|
||||
let hash = quick_hash(src);
|
||||
|
||||
if modules.contains(&hash) {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
prepare_dependency(src, &mut modules, &mut program, &logger)?;
|
||||
|
||||
let mut nodes = program.nodes.clone();
|
||||
|
||||
create_sections(&mut nodes)?;
|
||||
resolve_symbols(&mut nodes)?;
|
||||
|
||||
logger.info("Generating assembly output...");
|
||||
|
||||
let instructions = codegen(nodes)?;
|
||||
|
||||
logger.info("Compilation Successful");
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
fn prepare_dependency(
|
||||
path: &Path,
|
||||
modules: &mut HashSet<u64>,
|
||||
program: &mut Program,
|
||||
logger: &Logger,
|
||||
) -> Result<(), AssembleError> {
|
||||
let filename = path
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.expect("Failed to get file name from path");
|
||||
|
||||
if let Ok(path) = path.canonicalize() {
|
||||
logger.info(&format!(
|
||||
"{:20} {:20} [{}]",
|
||||
"Building",
|
||||
filename,
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
|
||||
let src = fs::read_to_string(path)
|
||||
.map_err(|_| AssembleError::InvalidFile(path.to_path_buf()))?;
|
||||
let file_hash = quick_hash(path);
|
||||
|
||||
logger.info(&format!("{:20} {:20}", "Tokenising", filename));
|
||||
let tokens = lexer::lexer(src, file_hash)?;
|
||||
|
||||
logger.info(&format!("{:20} {:20}", "Parsing", filename));
|
||||
let parsed = Parser::parse_nodes(tokens)?;
|
||||
|
||||
logger.info(&format!("{:20} {:20}", "Resolving Deps", filename));
|
||||
// Get the parent directory of the source file to use as the base directory
|
||||
let base_dir = path
|
||||
.parent()
|
||||
.ok_or_else(|| AssembleError::InvalidFile(path.to_path_buf()))?;
|
||||
let mut nodes = expand_pseudo_ops(parsed, file_hash)?;
|
||||
nodes = resolve_dependencies(nodes, base_dir)?;
|
||||
|
||||
let deps = Parser::get_dependencies(&nodes, path)?;
|
||||
|
||||
logger.info(&format!("{:20} {:20}", "Expanding Pseudo-ops", filename));
|
||||
|
||||
// add a section instruction
|
||||
nodes.insert(
|
||||
0,
|
||||
node!(None, Opcode::Segment, Token::Immediate(file_hash as u32)),
|
||||
);
|
||||
|
||||
// for n in &nodes {
|
||||
// println!("{n}");
|
||||
// }
|
||||
|
||||
program.add_module(nodes);
|
||||
|
||||
for dep in deps {
|
||||
logger.info(&format!(
|
||||
"{:20} {:20}",
|
||||
"Including",
|
||||
dep.file_name()
|
||||
.and_then(|f| f.to_str())
|
||||
.expect("Dependency path has no file name or is not valid UTF-8")
|
||||
));
|
||||
|
||||
let dep_hash = quick_hash(&dep);
|
||||
if modules.insert(dep_hash) {
|
||||
prepare_dependency(dep.as_path(), modules, program, logger)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum AssembleError {
|
||||
Generic,
|
||||
UnexpectedEof,
|
||||
InvalidFile(PathBuf),
|
||||
UnexpectedToken(Token, TokenType),
|
||||
InvalidArg,
|
||||
UndefinedSymbol(Symbol),
|
||||
/// Contains the nth element missing from the instruction.
|
||||
MissingArgument(u8),
|
||||
}
|
||||
|
||||
impl fmt::Display for AssembleError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Generic => write!(f, "Generic error"),
|
||||
Self::UnexpectedToken(tok, expected) => {
|
||||
write!(f, "Unexpected token {tok:?}, expected {expected:?}")
|
||||
}
|
||||
Self::UnexpectedEof => write!(f, "Unexpected end of file"),
|
||||
Self::InvalidFile(path) => write!(f, "Invalid file `{}`", path.display()),
|
||||
Self::InvalidArg => write!(f, "Invalid argument"),
|
||||
Self::UndefinedSymbol(symbol) => {
|
||||
write!(f, "Undefined symbol {symbol}")
|
||||
}
|
||||
Self::MissingArgument(n) => {
|
||||
write!(f, "Missing argument #{n} from instruction arguments.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn quick_hash(value: &Path) -> u64 {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
value
|
||||
.canonicalize()
|
||||
.expect("Failed to canonicalize path for quick_hash")
|
||||
.to_str()
|
||||
.hash(&mut hasher);
|
||||
|
||||
hasher.finish()
|
||||
}
|
||||
@@ -0,0 +1,438 @@
|
||||
use std::{fmt, str::FromStr};
|
||||
|
||||
use common::prelude::Register;
|
||||
|
||||
use crate::assembler::AssembleError;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Node {
|
||||
pub symbol: Option<Symbol>,
|
||||
pub opcode: Opcode,
|
||||
pub tokens: Vec<Token>,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
#[must_use]
|
||||
pub const fn new(symbol: Option<Symbol>, opcode: Opcode, tokens: Vec<Token>) -> Self {
|
||||
Self {
|
||||
symbol,
|
||||
opcode,
|
||||
tokens,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn label(&self) -> Option<Symbol> {
|
||||
self.symbol.clone()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn opcode(&self) -> Opcode {
|
||||
self.opcode
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn args(&self) -> Vec<Token> {
|
||||
self.tokens.clone()
|
||||
}
|
||||
|
||||
pub fn arg(&self, index: usize) -> Result<Token, AssembleError> {
|
||||
self.args()
|
||||
.get(index)
|
||||
.cloned()
|
||||
.ok_or(AssembleError::InvalidArg)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Node {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let symbol = self
|
||||
.label()
|
||||
.as_ref()
|
||||
.map_or_else(String::new, |symbol| format!("{symbol}:\n"));
|
||||
|
||||
let args = self
|
||||
.args()
|
||||
.into_iter()
|
||||
.map(|arg| arg.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
|
||||
write!(
|
||||
f,
|
||||
"\x1b[93m{} \t\x1b[94m{} \x1b[37m{} \x1b[0m",
|
||||
symbol,
|
||||
self.opcode(),
|
||||
args,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Symbol {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{} [ID:{}]", self.name, self.module)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Module {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Unresolved(name) => write!(f, "{name}"),
|
||||
Self::Resolved(name) => write!(f, "{name}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Opcode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Nop => write!(f, "nop"),
|
||||
Self::Mov => write!(f, "mov"),
|
||||
Self::Movs => write!(f, "movs"),
|
||||
Self::Ldb => write!(f, "ldb"),
|
||||
Self::Ldbs => write!(f, "ldbs"),
|
||||
Self::Ldh => write!(f, "ldh"),
|
||||
Self::Ldhs => write!(f, "ldhs"),
|
||||
Self::Ldw => write!(f, "ldw"),
|
||||
Self::Stb => write!(f, "stb"),
|
||||
Self::Sth => write!(f, "sth"),
|
||||
Self::Stw => write!(f, "stw"),
|
||||
Self::Lli => write!(f, "lli"),
|
||||
Self::Lui => write!(f, "lui"),
|
||||
Self::Jmp => write!(f, "jmp"),
|
||||
Self::Jeq => write!(f, "jeq"),
|
||||
Self::Jne => write!(f, "jne"),
|
||||
Self::Jgt => write!(f, "jgt"),
|
||||
Self::Jge => write!(f, "jge"),
|
||||
Self::Jlt => write!(f, "jlt"),
|
||||
Self::Jle => write!(f, "jle"),
|
||||
Self::Cmp => write!(f, "cmp"),
|
||||
Self::Inc => write!(f, "inc"),
|
||||
Self::Dec => write!(f, "dec"),
|
||||
Self::Shl => write!(f, "shl"),
|
||||
Self::Shr => write!(f, "shr"),
|
||||
Self::Add => write!(f, "add"),
|
||||
Self::Sub => write!(f, "sub"),
|
||||
Self::And => write!(f, "and"),
|
||||
Self::Or => write!(f, "or"),
|
||||
Self::Not => write!(f, "not"),
|
||||
Self::Xor => write!(f, "xor"),
|
||||
Self::Nand => write!(f, "nand"),
|
||||
Self::Nor => write!(f, "nor"),
|
||||
Self::Xnor => write!(f, "xnor"),
|
||||
Self::Int => write!(f, "int"),
|
||||
Self::Irt => write!(f, "irt"),
|
||||
Self::Hlt => write!(f, "hlt"),
|
||||
Self::AddI => write!(f, "addi"),
|
||||
Self::SubI => write!(f, "subi"),
|
||||
Self::Db => write!(f, "db"),
|
||||
Self::Dh => write!(f, "dh"),
|
||||
Self::Dw => write!(f, "dw"),
|
||||
Self::Resb => write!(f, "resb"),
|
||||
Self::Resh => write!(f, "resh"),
|
||||
Self::Resw => write!(f, "resw"),
|
||||
Self::Push => write!(f, "push"),
|
||||
Self::Pop => write!(f, "pop"),
|
||||
Self::Lwi => write!(f, "lwi"),
|
||||
Self::Call => write!(f, "call"),
|
||||
Self::Return => write!(f, "return"),
|
||||
Self::Pusha => write!(f, "pusha"),
|
||||
Self::Popa => write!(f, "popa"),
|
||||
|
||||
// meta instructions
|
||||
Self::Include => write!(f, "include"),
|
||||
Self::Data => write!(f, "data"),
|
||||
Self::Segment => write!(f, "[SEGMENT]"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Eq)]
|
||||
pub struct Symbol {
|
||||
pub name: String,
|
||||
pub module: Module,
|
||||
}
|
||||
|
||||
impl std::hash::Hash for Symbol {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.name.hash(state);
|
||||
self.module.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Symbol {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.name == other.name && self.module == other.module
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Module {
|
||||
Resolved(u64),
|
||||
Unresolved(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Token {
|
||||
Symbol(Symbol),
|
||||
Register(Register),
|
||||
Immediate(u32),
|
||||
StringLit(String),
|
||||
Opcode(Opcode),
|
||||
}
|
||||
|
||||
impl fmt::Display for Token {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Symbol(symbol) => write!(f, "{symbol}"),
|
||||
Self::Register(register) => write!(f, "{register}",),
|
||||
Self::Immediate(immediate) => write!(f, "{immediate}",),
|
||||
Self::StringLit(string_lit) => write!(f, "{string_lit}",),
|
||||
Self::Opcode(opcode) => write!(f, "{opcode}",),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
pub enum TokenType {
|
||||
Symbol,
|
||||
Register,
|
||||
Immediate,
|
||||
StringLit,
|
||||
Opcode,
|
||||
}
|
||||
|
||||
impl TokenType {
|
||||
#[must_use]
|
||||
pub const fn from_token(token: &Token) -> Self {
|
||||
match token {
|
||||
Token::Symbol(_) => Self::Symbol,
|
||||
Token::Register(_) => Self::Register,
|
||||
Token::Immediate(_) => Self::Immediate,
|
||||
Token::StringLit(_) => Self::StringLit,
|
||||
Token::Opcode(_) => Self::Opcode,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Opcode {
|
||||
// Real instructions (0x00-0x26)
|
||||
Nop,
|
||||
Mov,
|
||||
Movs,
|
||||
Ldb,
|
||||
Ldbs,
|
||||
Ldh,
|
||||
Ldhs,
|
||||
Ldw,
|
||||
Stb,
|
||||
Sth,
|
||||
Stw,
|
||||
Lli,
|
||||
Lui,
|
||||
Jmp,
|
||||
Jeq,
|
||||
Jne,
|
||||
Jgt,
|
||||
Jge,
|
||||
Jlt,
|
||||
Jle,
|
||||
Cmp,
|
||||
Inc,
|
||||
Dec,
|
||||
Shl,
|
||||
Shr,
|
||||
Add,
|
||||
Sub,
|
||||
And,
|
||||
Or,
|
||||
Not,
|
||||
Xor,
|
||||
Nand,
|
||||
Nor,
|
||||
Xnor,
|
||||
Int,
|
||||
Irt,
|
||||
Hlt,
|
||||
AddI,
|
||||
SubI,
|
||||
|
||||
// Pseudo-instructions
|
||||
Db,
|
||||
Dh,
|
||||
Dw,
|
||||
Resb,
|
||||
Resh,
|
||||
Resw,
|
||||
Push,
|
||||
Pop,
|
||||
Pusha,
|
||||
Popa,
|
||||
Lwi,
|
||||
Call,
|
||||
Return,
|
||||
|
||||
// meta instructions (these aren't present in the binary as instructions)
|
||||
Include,
|
||||
Data,
|
||||
Segment,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum OpcodeFromStrError {
|
||||
InvalidRegister(&'static str),
|
||||
InvalidOpcode(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for OpcodeFromStrError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::InvalidRegister(reg) => write!(f, "register does not exist: {reg}"),
|
||||
Self::InvalidOpcode(op) => write!(f, "instruction does not exist: {op}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for OpcodeFromStrError {}
|
||||
|
||||
impl FromStr for Opcode {
|
||||
type Err = OpcodeFromStrError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"nop" => Ok(Self::Nop),
|
||||
"mov" => Ok(Self::Mov),
|
||||
"movs" => Ok(Self::Movs),
|
||||
"ldb" => Ok(Self::Ldb),
|
||||
"ldbs" => Ok(Self::Ldbs),
|
||||
"ldh" => Ok(Self::Ldh),
|
||||
"ldhs" => Ok(Self::Ldhs),
|
||||
"ldw" => Ok(Self::Ldw),
|
||||
"stb" => Ok(Self::Stb),
|
||||
"sth" => Ok(Self::Sth),
|
||||
"stw" => Ok(Self::Stw),
|
||||
"lli" => Ok(Self::Lli),
|
||||
"lui" => Ok(Self::Lui),
|
||||
"jmp" => Ok(Self::Jmp),
|
||||
"jeq" => Ok(Self::Jeq),
|
||||
"jne" => Ok(Self::Jne),
|
||||
"jgt" => Ok(Self::Jgt),
|
||||
"jge" => Ok(Self::Jge),
|
||||
"jlt" => Ok(Self::Jlt),
|
||||
"jle" => Ok(Self::Jle),
|
||||
"cmp" => Ok(Self::Cmp),
|
||||
"inc" => Ok(Self::Inc),
|
||||
"dec" => Ok(Self::Dec),
|
||||
"shl" => Ok(Self::Shl),
|
||||
"shr" => Ok(Self::Shr),
|
||||
"add" => Ok(Self::Add),
|
||||
"sub" => Ok(Self::Sub),
|
||||
"and" => Ok(Self::And),
|
||||
"or" => Ok(Self::Or),
|
||||
"not" => Ok(Self::Not),
|
||||
"xor" => Ok(Self::Xor),
|
||||
"nand" => Ok(Self::Nand),
|
||||
"nor" => Ok(Self::Nor),
|
||||
"xnor" => Ok(Self::Xnor),
|
||||
"int" => Ok(Self::Int),
|
||||
"irt" => Ok(Self::Irt),
|
||||
"hlt" => Ok(Self::Hlt),
|
||||
"addi" => Ok(Self::AddI),
|
||||
"subi" => Ok(Self::SubI),
|
||||
"db" => Ok(Self::Db),
|
||||
"dh" => Ok(Self::Dh),
|
||||
"dw" => Ok(Self::Dw),
|
||||
"resb" => Ok(Self::Resb),
|
||||
"resh" => Ok(Self::Resh),
|
||||
"resw" => Ok(Self::Resw),
|
||||
"push" => Ok(Self::Push),
|
||||
"pop" => Ok(Self::Pop),
|
||||
"lwi" => Ok(Self::Lwi),
|
||||
"include" => Ok(Self::Include),
|
||||
"call" => Ok(Self::Call),
|
||||
"return" => Ok(Self::Return),
|
||||
"pusha" => Ok(Self::Pusha),
|
||||
"popa" => Ok(Self::Popa),
|
||||
_ => Err(OpcodeFromStrError::InvalidOpcode(s.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Opcode {
|
||||
pub const OPCODES: &[&str] = &[
|
||||
// Real instructions (0x00-0x26)
|
||||
"nop", "mov", "movs", "ldb", "ldbs", "ldh", "ldhs", "ldw", "stb", "sth", "stw",
|
||||
"lli", "lui", "jmp", "jeq", "jne", "jgt", "jge", "jlt", "jle", "cmp", "inc",
|
||||
"dec", "shl", "shr", "add", "sub", "and", "or", "not", "xor", "nand", "nor",
|
||||
"xnor", "int", "irt", "hlt", "addi", "subi", // Pseudo-instructions
|
||||
"db", "dh", "dw", "resb", "resh", "resw", "push", "pop", "lwi", "call", "return",
|
||||
"pusha", "popa", // meta instructions
|
||||
"include",
|
||||
];
|
||||
|
||||
#[must_use]
|
||||
pub const fn to_opcode_value(&self) -> Option<u8> {
|
||||
match self {
|
||||
Self::Nop => Some(0x00),
|
||||
Self::Mov => Some(0x01),
|
||||
Self::Movs => Some(0x02),
|
||||
Self::Ldb => Some(0x03),
|
||||
Self::Ldbs => Some(0x04),
|
||||
Self::Ldh => Some(0x05),
|
||||
Self::Ldhs => Some(0x06),
|
||||
Self::Ldw => Some(0x07),
|
||||
Self::Stb => Some(0x08),
|
||||
Self::Sth => Some(0x09),
|
||||
Self::Stw => Some(0x0A),
|
||||
Self::Lli => Some(0x0B),
|
||||
Self::Lui => Some(0x0C),
|
||||
Self::Jmp => Some(0x0D),
|
||||
Self::Jeq => Some(0x0E),
|
||||
Self::Jne => Some(0x0F),
|
||||
Self::Jgt => Some(0x10),
|
||||
Self::Jge => Some(0x11),
|
||||
Self::Jlt => Some(0x12),
|
||||
Self::Jle => Some(0x13),
|
||||
Self::Cmp => Some(0x14),
|
||||
Self::Inc => Some(0x15),
|
||||
Self::Dec => Some(0x16),
|
||||
Self::Shl => Some(0x17),
|
||||
Self::Shr => Some(0x18),
|
||||
Self::Add => Some(0x19),
|
||||
Self::Sub => Some(0x1A),
|
||||
Self::And => Some(0x1B),
|
||||
Self::Or => Some(0x1C),
|
||||
Self::Not => Some(0x1D),
|
||||
Self::Xor => Some(0x1E),
|
||||
Self::Nand => Some(0x1F),
|
||||
Self::Nor => Some(0x20),
|
||||
Self::Xnor => Some(0x21),
|
||||
Self::Int => Some(0x22),
|
||||
Self::Irt => Some(0x23),
|
||||
Self::Hlt => Some(0x24),
|
||||
Self::AddI => Some(0x25),
|
||||
Self::SubI => Some(0x26),
|
||||
Self::Segment => Some(0x27),
|
||||
|
||||
// Pseudo-instructions don't have opcode values
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn is_pseudo_instruction(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Db
|
||||
| Self::Dh
|
||||
| Self::Dw
|
||||
| Self::Resb
|
||||
| Self::Resh
|
||||
| Self::Resw
|
||||
| Self::Push
|
||||
| Self::Pop
|
||||
| Self::Lwi
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::assembler::TokenType;
|
||||
use crate::{assembler::AssembleError, expect_token, expect_type, node};
|
||||
|
||||
use crate::assembler::model::{Node, Opcode, Token};
|
||||
use common::prelude::*;
|
||||
|
||||
pub struct Parser {
|
||||
tokens: Vec<Token>,
|
||||
nodes: Vec<Node>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Program {
|
||||
pub nodes: Vec<Node>,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
#[must_use]
|
||||
pub const fn new() -> Self {
|
||||
Self { nodes: vec![] }
|
||||
}
|
||||
|
||||
pub fn add_module(&mut self, module: Vec<Node>) {
|
||||
self.nodes.extend(module);
|
||||
}
|
||||
|
||||
pub fn parser(&mut self) -> Parser {
|
||||
Parser {
|
||||
tokens: vec![],
|
||||
nodes: self.nodes.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Program {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Parser {
|
||||
pub fn parse_nodes(tokens: Vec<Token>) -> Result<Vec<Node>, AssembleError> {
|
||||
let mut self_ = Self {
|
||||
tokens: tokens.into_iter().rev().collect(),
|
||||
nodes: vec![],
|
||||
};
|
||||
|
||||
while !self_.tokens.is_empty() {
|
||||
let ins = self_.parse_instruction()?;
|
||||
self_.nodes.push(ins);
|
||||
}
|
||||
|
||||
Ok(self_.nodes.clone())
|
||||
}
|
||||
|
||||
pub fn get_dependencies(
|
||||
nodes: &Vec<Node>,
|
||||
source_path: &Path,
|
||||
) -> Result<Vec<PathBuf>, AssembleError> {
|
||||
let mut dependencies = Vec::new();
|
||||
// Get the parent directory of the source file to use as the base directory
|
||||
let base_dir = source_path
|
||||
.parent()
|
||||
.ok_or_else(|| AssembleError::InvalidFile(source_path.to_path_buf()))?;
|
||||
|
||||
for node in nodes {
|
||||
if node.opcode() == Opcode::Include {
|
||||
let path_str = expect_token!(
|
||||
node.args().get(1).ok_or(AssembleError::Generic)?,
|
||||
StringLit
|
||||
)?;
|
||||
let path = PathBuf::from(path_str);
|
||||
|
||||
// If the path is not absolute, make it relative to the base directory
|
||||
let full_path = if path.is_absolute() {
|
||||
path
|
||||
} else {
|
||||
base_dir.join(path)
|
||||
};
|
||||
|
||||
dependencies.push(full_path);
|
||||
}
|
||||
}
|
||||
Ok(dependencies)
|
||||
}
|
||||
|
||||
#[expect(clippy::too_many_lines, clippy::cognitive_complexity)]
|
||||
fn parse_instruction(&mut self) -> Result<Node, AssembleError> {
|
||||
if self.tokens.is_empty() {
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
// check if the Node starts with a label
|
||||
let label = expect_token!(self.peek_next()?, Symbol).ok();
|
||||
if label.is_some() {
|
||||
self.tokens.pop();
|
||||
}
|
||||
|
||||
let opcode = expect_token!(self.next()?, Opcode)?;
|
||||
let args: Vec<Token>;
|
||||
|
||||
#[allow(clippy::match_same_arms)]
|
||||
match opcode {
|
||||
// R-type instructions
|
||||
Opcode::Mov | Opcode::Movs => {
|
||||
let reg1 = expect_type!(self.next()?, Register, Symbol)?;
|
||||
let reg2 = expect_type!(self.next()?, Register, Symbol)?;
|
||||
args = vec![reg1, reg2];
|
||||
}
|
||||
|
||||
Opcode::Ldb | Opcode::Ldbs | Opcode::Ldh | Opcode::Ldhs | Opcode::Ldw => {
|
||||
let base = expect_type!(self.next()?, Register, Symbol)?;
|
||||
let dest = expect_type!(self.next()?, Register)?;
|
||||
|
||||
let offset = match self.peek_next() {
|
||||
Ok(next) if expect_type!(next.clone(), Immediate).is_ok() => {
|
||||
self.next()?
|
||||
}
|
||||
_ => Token::Immediate(0),
|
||||
};
|
||||
|
||||
args = vec![base, dest, offset];
|
||||
}
|
||||
Opcode::Stb | Opcode::Sth | Opcode::Stw => {
|
||||
let base = expect_type!(self.next()?, Register)?;
|
||||
let dest = expect_type!(self.next()?, Register, Symbol)?;
|
||||
|
||||
let offset = match self.peek_next() {
|
||||
Ok(next) if expect_type!(next.clone(), Immediate).is_ok() => {
|
||||
self.next()?
|
||||
}
|
||||
_ => Token::Immediate(0),
|
||||
};
|
||||
args = vec![base, dest, offset];
|
||||
}
|
||||
|
||||
Opcode::Add
|
||||
| Opcode::Sub
|
||||
| Opcode::And
|
||||
| Opcode::Or
|
||||
| Opcode::Xor
|
||||
| Opcode::Nand
|
||||
| Opcode::Nor
|
||||
| Opcode::Xnor => {
|
||||
let src1 = expect_type!(self.next()?, Register, Symbol)?;
|
||||
let src2 = expect_type!(self.next()?, Register, Symbol)?;
|
||||
let dest = expect_type!(self.next()?, Register, Symbol)?;
|
||||
args = vec![src1, src2, dest];
|
||||
}
|
||||
|
||||
Opcode::Not | Opcode::Cmp => {
|
||||
let src = expect_type!(self.next()?, Register, Symbol)?;
|
||||
let dest = expect_type!(self.next()?, Register, Symbol)?;
|
||||
args = vec![src, dest];
|
||||
}
|
||||
Opcode::Shl | Opcode::Shr => {
|
||||
let src = expect_type!(self.next()?, Register, Symbol)?;
|
||||
|
||||
// First operand after src: could be immediate or register
|
||||
let first = self.next()?;
|
||||
|
||||
let (r_shamt, i_shamt) = match first {
|
||||
Token::Register(_) => (
|
||||
first,
|
||||
if let Ok(tok) = self.peek_next() {
|
||||
if expect_type!(tok, Immediate).is_ok() {
|
||||
self.next()?
|
||||
} else {
|
||||
Token::Immediate(0)
|
||||
}
|
||||
} else {
|
||||
Token::Immediate(0)
|
||||
},
|
||||
),
|
||||
Token::Immediate(_) => (Token::Register(Register::Zero), first),
|
||||
_ => {
|
||||
return Err(AssembleError::UnexpectedToken(
|
||||
first,
|
||||
TokenType::Immediate,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let dest = if let Ok(tok) = self.peek_next() {
|
||||
if expect_type!(tok, Register).is_ok() {
|
||||
self.next()?
|
||||
} else {
|
||||
src.clone() // Default to src if no dest specified
|
||||
}
|
||||
} else {
|
||||
src.clone() // Default to src if no dest specified
|
||||
};
|
||||
|
||||
args = vec![src, r_shamt, i_shamt, dest];
|
||||
}
|
||||
|
||||
Opcode::Inc | Opcode::Dec => {
|
||||
let reg = expect_type!(self.next()?, Register, Symbol)?;
|
||||
args = vec![reg];
|
||||
}
|
||||
|
||||
Opcode::Include => {
|
||||
let mod_name = expect_type!(self.next()?, Symbol)?;
|
||||
let path = expect_type!(self.next()?, StringLit)?;
|
||||
args = vec![mod_name, path];
|
||||
}
|
||||
|
||||
// J-type instructions
|
||||
Opcode::Jmp
|
||||
| Opcode::Jeq
|
||||
| Opcode::Jne
|
||||
| Opcode::Jgt
|
||||
| Opcode::Jge
|
||||
| Opcode::Jlt
|
||||
| Opcode::Jle => {
|
||||
let imm = expect_type!(self.next()?, Immediate, Symbol)?;
|
||||
let offset = match self.peek_next() {
|
||||
Ok(token) => {
|
||||
if expect_type!(token, Register).is_ok() {
|
||||
self.next()?
|
||||
} else {
|
||||
Token::Register(Register::Zero)
|
||||
}
|
||||
}
|
||||
Err(_) => Token::Register(Register::Zero),
|
||||
};
|
||||
args = vec![imm, offset];
|
||||
}
|
||||
|
||||
Opcode::Call => {
|
||||
let addr = expect_type!(self.next()?, Symbol)?;
|
||||
args = vec![addr];
|
||||
}
|
||||
|
||||
// I-type instructions
|
||||
Opcode::Lui | Opcode::Lli | Opcode::Lwi => {
|
||||
let imm = expect_type!(self.next()?, Immediate, Symbol)?;
|
||||
let reg = expect_type!(self.next()?, Register)?;
|
||||
args = vec![imm, reg];
|
||||
}
|
||||
|
||||
// Immediate Arithmetic
|
||||
Opcode::AddI | Opcode::SubI => {
|
||||
let reg = expect_type!(self.next()?, Register)?;
|
||||
let imm = expect_type!(self.next()?, Immediate)?;
|
||||
let reg2 = if expect_type!(self.peek_next()?, Register).is_ok() {
|
||||
self.next()?
|
||||
} else {
|
||||
reg.clone()
|
||||
};
|
||||
args = vec![reg, imm, reg2];
|
||||
}
|
||||
|
||||
// D-type pseudoinstructions (data definition)
|
||||
Opcode::Resb | Opcode::Resh | Opcode::Resw => {
|
||||
let name = expect_type!(self.next()?, Symbol)?;
|
||||
let num = expect_type!(self.next()?, Immediate)?;
|
||||
args = vec![name, num];
|
||||
}
|
||||
|
||||
Opcode::Db | Opcode::Dh | Opcode::Dw => {
|
||||
args = self.parse_data_definition(opcode)?;
|
||||
}
|
||||
|
||||
// E-type pseudoinstructions (stack operations)
|
||||
Opcode::Push | Opcode::Pop => {
|
||||
let reg = expect_type!(self.next()?, Register, Symbol)?;
|
||||
args = vec![reg];
|
||||
}
|
||||
|
||||
Opcode::Pusha | Opcode::Popa => {
|
||||
let count =
|
||||
expect_type!(self.next()?, Immediate).unwrap_or(Token::Immediate(8));
|
||||
args = vec![count];
|
||||
}
|
||||
|
||||
// Special instructions
|
||||
Opcode::Int => {
|
||||
let val = expect_type!(self.next()?, Immediate)?;
|
||||
args = vec![val];
|
||||
}
|
||||
|
||||
// Instructions with no arguments
|
||||
Opcode::Hlt | Opcode::Nop | Opcode::Irt | Opcode::Return => {
|
||||
args = vec![];
|
||||
}
|
||||
|
||||
Opcode::Data | Opcode::Segment => {
|
||||
return Err(AssembleError::Generic);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(node!(label, opcode, args: args))
|
||||
}
|
||||
|
||||
fn parse_data_definition(
|
||||
&mut self,
|
||||
opcode: Opcode,
|
||||
) -> Result<Vec<Token>, AssembleError> {
|
||||
let mut values = Vec::new();
|
||||
|
||||
let name = expect_type!(self.next()?, Symbol)?;
|
||||
values.push(name);
|
||||
|
||||
match opcode {
|
||||
Opcode::Db => {
|
||||
// db can take string literals or u8 immediates
|
||||
while !self.tokens.is_empty() {
|
||||
let token = self
|
||||
.tokens
|
||||
.last()
|
||||
.expect("Expected a token for data definition, but found none");
|
||||
|
||||
match token {
|
||||
Token::StringLit(_) => {
|
||||
values.push(self.tokens.pop().expect(
|
||||
"Expected a token for data definition, but found none",
|
||||
));
|
||||
}
|
||||
Token::Immediate(val) if u8::try_from(*val).is_ok() => {
|
||||
values.push(self.tokens.pop().expect(
|
||||
"Expected a token for data definition, but found none",
|
||||
));
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Opcode::Dh => {
|
||||
// dh can take u16 immediates
|
||||
while !self.tokens.is_empty() {
|
||||
let token = self
|
||||
.tokens
|
||||
.last()
|
||||
.expect("Expected a token for data definition, but found none");
|
||||
|
||||
match token {
|
||||
Token::StringLit(_) => {
|
||||
values.push(self.tokens.pop().expect(
|
||||
"Expected a token for data definition, but found none",
|
||||
));
|
||||
}
|
||||
Token::Immediate(val) if u16::try_from(*val).is_ok() => {
|
||||
values.push(self.tokens.pop().expect(
|
||||
"Expected a token for data definition, but found none",
|
||||
));
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Opcode::Dw => {
|
||||
// dw can take u32 immediates
|
||||
while !self.tokens.is_empty() {
|
||||
match self
|
||||
.tokens
|
||||
.last()
|
||||
.expect("Expected a token for data definition, but found none")
|
||||
{
|
||||
Token::StringLit(_) => {
|
||||
values.push(self.tokens.pop().expect(
|
||||
"Expected a token for data definition, but found none",
|
||||
));
|
||||
}
|
||||
Token::Immediate(val) => {
|
||||
values.push(self.tokens.pop().expect(
|
||||
"Expected a token for data definition, but found none",
|
||||
));
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
fn next(&mut self) -> Result<Token, AssembleError> {
|
||||
if self.tokens.is_empty() {
|
||||
Err(AssembleError::UnexpectedEof)
|
||||
} else {
|
||||
Ok(self
|
||||
.tokens
|
||||
.pop()
|
||||
.expect("tokens vector was unexpectedly empty in next()"))
|
||||
}
|
||||
}
|
||||
|
||||
fn peek_next(&self) -> Result<Token, AssembleError> {
|
||||
if self.tokens.is_empty() {
|
||||
Err(AssembleError::UnexpectedEof)
|
||||
} else {
|
||||
Ok(self
|
||||
.tokens
|
||||
.last()
|
||||
.expect("peek_next called on empty tokens vector")
|
||||
.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::canonicalize,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use common::prelude::Register;
|
||||
|
||||
use crate::assembler::model::{Module, Node, Opcode, Symbol, Token};
|
||||
use crate::assembler::quick_hash;
|
||||
use crate::{assembler::AssembleError, node};
|
||||
|
||||
pub fn resolve_symbols(nodes: &mut [Node]) -> Result<(), AssembleError> {
|
||||
let symbol_table = generate_symbol_table(nodes);
|
||||
|
||||
for node in nodes.iter_mut() {
|
||||
match node.opcode() {
|
||||
Opcode::Jmp
|
||||
| Opcode::Jeq
|
||||
| Opcode::Jne
|
||||
| Opcode::Jgt
|
||||
| Opcode::Jge
|
||||
| Opcode::Jlt
|
||||
| Opcode::Jle
|
||||
| Opcode::Lli
|
||||
| Opcode::Lui => {
|
||||
if let Token::Symbol(symbol) = node
|
||||
.arg(0)
|
||||
.expect("Expected argument 0 for jump-like opcode")
|
||||
{
|
||||
if let Some(address) = symbol_table.get(&symbol) {
|
||||
node.tokens[0] = Token::Immediate(*address);
|
||||
} else {
|
||||
return Err(AssembleError::UndefinedSymbol(symbol));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_symbol_table(nodes: &[Node]) -> HashMap<Symbol, u32> {
|
||||
let mut table = HashMap::new();
|
||||
|
||||
for (i, node) in nodes.iter().enumerate() {
|
||||
if let Some(symbol) = node.label() {
|
||||
table.insert(symbol, 4 * i as u32);
|
||||
}
|
||||
}
|
||||
|
||||
table
|
||||
}
|
||||
|
||||
pub fn resolve_dependencies(
|
||||
mut nodes: Vec<Node>,
|
||||
base_dir: &Path,
|
||||
) -> Result<Vec<Node>, AssembleError> {
|
||||
// First we get a list of imports.
|
||||
let mut dependencies = Vec::new();
|
||||
for node in &nodes {
|
||||
if node.opcode() == Opcode::Include {
|
||||
// we want the path, and the name
|
||||
let name = if let Token::Symbol(name) = node
|
||||
.arg(0)
|
||||
.expect("Expected argument #0 for Include directive.")
|
||||
{
|
||||
name.name.clone()
|
||||
} else {
|
||||
unreachable!()
|
||||
}; //node.2.get(0).unwrap()
|
||||
|
||||
let Ok(Token::StringLit(path)) = node.arg(1) else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
let full_path = base_dir.join(path);
|
||||
let canonical_path = full_path
|
||||
.canonicalize()
|
||||
.map_err(|_| AssembleError::InvalidFile(full_path.clone()))?;
|
||||
|
||||
let hash = quick_hash(&canonical_path);
|
||||
|
||||
dependencies.push((name, hash));
|
||||
}
|
||||
}
|
||||
|
||||
let mut changes = Vec::<(u32, u32, Symbol)>::new();
|
||||
// now we resolve the symbols on all the nodes
|
||||
// we need to check all operands for unresolved signals
|
||||
for (i, node) in nodes.clone().iter().enumerate() {
|
||||
let Node {
|
||||
tokens: operands, ..
|
||||
} = node;
|
||||
for (j, token) in operands.iter().enumerate() {
|
||||
if let Token::Symbol(symbol) = token {
|
||||
for d in &dependencies {
|
||||
if let Module::Unresolved(name) = symbol.module.clone() {
|
||||
if name != d.0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let symbol = Symbol {
|
||||
name: symbol.name.clone(),
|
||||
module: Module::Resolved(d.1),
|
||||
};
|
||||
changes.push((i as u32, j as u32, symbol));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i, j, symbol) in changes {
|
||||
nodes[i as usize].tokens[j as usize] = Token::Symbol(symbol);
|
||||
}
|
||||
|
||||
Ok(nodes)
|
||||
}
|
||||
|
||||
pub fn create_sections(nodes: &mut Vec<Node>) -> Result<(), AssembleError> {
|
||||
let mut res = Vec::<Node>::with_capacity(nodes.len());
|
||||
|
||||
res.push(node!(None, Opcode::Segment, Token::Immediate(0)));
|
||||
|
||||
for n in nodes.iter() {
|
||||
if n.opcode() == Opcode::Data {
|
||||
res.push(n.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let start = res.len() + 1;
|
||||
res.insert(
|
||||
0,
|
||||
node!(
|
||||
None,
|
||||
Opcode::Jmp,
|
||||
Token::Immediate(start as u32 * 4),
|
||||
Token::Register(Register::Zero)
|
||||
),
|
||||
);
|
||||
for n in nodes.iter() {
|
||||
if !matches!(n.opcode(), Opcode::Data | Opcode::Include) {
|
||||
res.push(n.clone());
|
||||
}
|
||||
}
|
||||
|
||||
*nodes = res;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#![deny(
|
||||
clippy::unwrap_used,
|
||||
clippy::nursery,
|
||||
clippy::perf,
|
||||
clippy::pedantic,
|
||||
clippy::complexity
|
||||
)]
|
||||
#![allow(
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::missing_panics_doc,
|
||||
clippy::missing_errors_doc,
|
||||
clippy::match_wildcard_for_single_variants
|
||||
)]
|
||||
|
||||
pub mod assembler;
|
||||
pub mod image_builder;
|
||||
pub mod tooling;
|
||||
mod util;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::assembler::Assembler;
|
||||
pub use crate::image_builder;
|
||||
pub use crate::tooling::brainf;
|
||||
pub use crate::tooling::project;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
use common::{self as _, build::Builder};
|
||||
|
||||
use assembler::{
|
||||
prelude::*,
|
||||
tooling::{brainf, project},
|
||||
};
|
||||
use std::{fs, io::Write, path::PathBuf};
|
||||
|
||||
fn main() {
|
||||
// Parse command line arguments
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
||||
if args.len() == 2 && args[1] == "init" {
|
||||
project::tool_libcreate();
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
if args.len() == 2 && args[1] == "brainf" {
|
||||
let src = PathBuf::from("brainf.bf");
|
||||
let result = brainf::build(&src);
|
||||
|
||||
let mut file = match fs::File::create("brainf.dsb") {
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create output file: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
for instruction in result {
|
||||
if let Err(e) = file.write(&instruction.encode().to_be_bytes()) {
|
||||
eprintln!("Failed to write to output file: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
if args.len() != 5 || args[1] != "-i" || args[3] != "-o" {
|
||||
eprintln!("Usage: {} -i input_path -o output_path", args[0]);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let input_path = &args[2];
|
||||
let output_path = &args[4];
|
||||
|
||||
let mut engine = Assembler::new(PathBuf::from(input_path));
|
||||
engine.start();
|
||||
let result = engine.output().expect("assembler failed.");
|
||||
|
||||
if let Err(e) = fs::write(output_path, result) {
|
||||
eprintln!("Failed to write to output file: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use common::prelude::*;
|
||||
|
||||
use crate::{
|
||||
assembler::{
|
||||
Module, Node, Opcode, Symbol, Token, codegen, create_sections, expand_pseudo_ops,
|
||||
resolve_symbols,
|
||||
},
|
||||
node,
|
||||
};
|
||||
|
||||
#[must_use]
|
||||
pub fn build(src: &Path) -> Vec<Instruction> {
|
||||
let src = fs::read_to_string(src).expect("Failed to read source file");
|
||||
let mut nodes = parse(&src);
|
||||
|
||||
// we need to expand pseudoinstructions etc now
|
||||
nodes = expand_pseudo_ops(nodes, 0).expect("Failed to expand pseudo-operations");
|
||||
|
||||
create_sections(&mut nodes).expect("Failed to create sections");
|
||||
|
||||
for n in &nodes {
|
||||
println!("{n}");
|
||||
}
|
||||
|
||||
resolve_symbols(&mut nodes).expect("Failed to resolve symbols");
|
||||
|
||||
codegen(nodes).expect("Failed to generate code from nodes")
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
#[expect(clippy::too_many_lines)]
|
||||
pub fn parse(src: &str) -> Vec<Node> {
|
||||
let stack = Token::Immediate(0x10000);
|
||||
let acc = Token::Register(Register::Acc);
|
||||
let rga = Token::Register(Register::Rga);
|
||||
|
||||
let bpr = Token::Register(Register::Bpr);
|
||||
let spr = Token::Register(Register::Spr);
|
||||
let mut nodes = Vec::<Node>::new();
|
||||
|
||||
// Define symbols
|
||||
let print_start = Symbol {
|
||||
name: "print".to_string(),
|
||||
module: Module::Resolved(0),
|
||||
};
|
||||
|
||||
let tokens = lex(src);
|
||||
|
||||
// let _id = 0;
|
||||
let mut idstack = Vec::<u32>::new();
|
||||
|
||||
nodes.extend(vec![
|
||||
// set up a stack
|
||||
node!(None, Opcode::Lwi, stack, bpr),
|
||||
node!(None, Opcode::Mov, bpr, spr),
|
||||
// set up the data pointer
|
||||
node!(
|
||||
Some(Symbol {
|
||||
name: "main".to_string(),
|
||||
module: Module::Resolved(0)
|
||||
}),
|
||||
Opcode::Lwi,
|
||||
Token::Immediate(0x30000),
|
||||
rga
|
||||
),
|
||||
]);
|
||||
|
||||
for (id, tok) in tokens.iter().enumerate() {
|
||||
match tok {
|
||||
BfToken::Inc => {
|
||||
// inc acc
|
||||
nodes.extend(vec![node!(None, Opcode::Inc, acc)]);
|
||||
}
|
||||
BfToken::Dec => {
|
||||
// dec acc
|
||||
nodes.extend(vec![node!(None, Opcode::Dec, acc)]);
|
||||
}
|
||||
BfToken::IncPtr => {
|
||||
// stb acc, rga
|
||||
// add rga, 4
|
||||
// ldb rga, acc
|
||||
nodes.extend(vec![
|
||||
node!(None, Opcode::Stw, acc, rga, 0),
|
||||
node!(None, Opcode::AddI, rga, 4, rga),
|
||||
node!(None, Opcode::Ldw, rga, acc, 0),
|
||||
]);
|
||||
}
|
||||
BfToken::DecPtr => {
|
||||
// stb acc, rga
|
||||
// sub rga, 4
|
||||
// ldb rga, acc
|
||||
nodes.extend(vec![
|
||||
node!(None, Opcode::Stw, acc, rga, 0),
|
||||
node!(None, Opcode::SubI, rga, 4, rga),
|
||||
node!(None, Opcode::Ldw, rga, acc, 0),
|
||||
]);
|
||||
}
|
||||
BfToken::Out => {
|
||||
// push rga
|
||||
// call print
|
||||
// pop zero
|
||||
nodes.extend(vec![
|
||||
node!(None, Opcode::Push, acc),
|
||||
node!(None, Opcode::Call, Token::Symbol(print_start.clone())),
|
||||
node!(None, Opcode::Pop, Token::Register(Register::Zero)),
|
||||
]);
|
||||
}
|
||||
BfToken::In => {
|
||||
// Read a byte from input and store it at the current data pointer
|
||||
// Assuming we have an input function mapped to a specific memory location or I/O port
|
||||
nodes.extend(vec![
|
||||
// Read input (assuming input is mapped to memory address 0x40000)
|
||||
node!(None, Opcode::Ldw, Token::Immediate(0x40000), acc, 0),
|
||||
// Store the input byte at the current data pointer
|
||||
]);
|
||||
}
|
||||
BfToken::Forward => {
|
||||
// Start of loop [
|
||||
let loop_start = format!("loop_start_{id}");
|
||||
let loop_end = format!("loop_end_{id}");
|
||||
|
||||
// Push the current position for the matching ]
|
||||
idstack.push(id as u32);
|
||||
|
||||
// Load current cell value and check if zero
|
||||
nodes.extend(vec![
|
||||
// Compare with zero
|
||||
node!(None, Opcode::Cmp, acc, Token::Register(Register::Zero)),
|
||||
// If zero, jump to end of loop
|
||||
node!(
|
||||
None,
|
||||
Opcode::Jeq,
|
||||
Token::Symbol(Symbol {
|
||||
name: loop_end,
|
||||
module: Module::Resolved(0),
|
||||
}),
|
||||
Token::Register(Register::Zero)
|
||||
),
|
||||
]);
|
||||
|
||||
// Add label for loop start
|
||||
nodes.push(node!(
|
||||
Some(Symbol {
|
||||
name: loop_start,
|
||||
module: Module::Resolved(0),
|
||||
}),
|
||||
Opcode::Nop
|
||||
));
|
||||
}
|
||||
BfToken::Back => {
|
||||
// End of loop ]
|
||||
if let Some(start_id) = idstack.pop() {
|
||||
let loop_start = format!("loop_start_{start_id}");
|
||||
let loop_end = format!("loop_end_{start_id}");
|
||||
|
||||
// Jump back to the start of the loop
|
||||
nodes.extend(vec![
|
||||
// Compare with zero
|
||||
node!(None, Opcode::Cmp, acc, Token::Register(Register::Zero)),
|
||||
// If not zero, jump back to start of loop
|
||||
node!(
|
||||
None,
|
||||
Opcode::Jne,
|
||||
Token::Symbol(Symbol {
|
||||
name: loop_start,
|
||||
module: Module::Resolved(0),
|
||||
}),
|
||||
Token::Register(Register::Zero)
|
||||
),
|
||||
// Add label for loop end
|
||||
node!(
|
||||
Some(Symbol {
|
||||
name: loop_end,
|
||||
module: Module::Resolved(0),
|
||||
}),
|
||||
Opcode::Nop
|
||||
),
|
||||
]);
|
||||
} else {
|
||||
// Unmatched closing bracket - could add error handling here
|
||||
eprintln!("Warning: Unmatched ']' at position {id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nodes.push(node!(None, Opcode::Hlt));
|
||||
|
||||
insert_lib(&mut nodes);
|
||||
|
||||
nodes
|
||||
}
|
||||
|
||||
fn insert_lib(nodes: &mut Vec<Node>) {
|
||||
let bpr = Token::Register(Register::Bpr);
|
||||
let spr = Token::Register(Register::Spr);
|
||||
let rg0 = Token::Register(Register::Rg0);
|
||||
let rg1 = Token::Register(Register::Rg1);
|
||||
|
||||
let print_start = Symbol {
|
||||
name: "print".to_string(),
|
||||
module: Module::Resolved(0),
|
||||
};
|
||||
let current = Symbol {
|
||||
name: "current".to_string(),
|
||||
module: Module::Resolved(0),
|
||||
};
|
||||
// set up the program framework.
|
||||
nodes.extend(vec![
|
||||
// set display to 0x20000
|
||||
node!(
|
||||
None,
|
||||
Opcode::Dw,
|
||||
Token::Symbol(current.clone()),
|
||||
Token::Immediate(0x20000)
|
||||
),
|
||||
// print function
|
||||
// initialisation
|
||||
node!(Some(print_start), Opcode::Push, bpr),
|
||||
node!(None, Opcode::Mov, spr, bpr),
|
||||
// function body
|
||||
node!(None, Opcode::Ldw, bpr, rg0, Token::Immediate(8)),
|
||||
node!(
|
||||
None,
|
||||
Opcode::Ldw,
|
||||
Token::Symbol(current.clone()), // Load address of current
|
||||
rg1,
|
||||
Token::Immediate(0)
|
||||
),
|
||||
node!(None, Opcode::Stb, rg0, rg1, Token::Immediate(0)),
|
||||
node!(None, Opcode::AddI, rg1, Token::Immediate(1), rg1),
|
||||
// function return according to spec.
|
||||
node!(
|
||||
None,
|
||||
Opcode::Stw,
|
||||
rg1,
|
||||
Token::Symbol(current), // Store back to current
|
||||
Token::Immediate(0)
|
||||
),
|
||||
node!(None, Opcode::Mov, bpr, spr),
|
||||
node!(None, Opcode::Pop, bpr),
|
||||
node!(None, Opcode::Return),
|
||||
]);
|
||||
}
|
||||
|
||||
enum BfToken {
|
||||
Inc,
|
||||
Dec,
|
||||
IncPtr,
|
||||
DecPtr,
|
||||
Out,
|
||||
In,
|
||||
Forward,
|
||||
Back,
|
||||
}
|
||||
|
||||
fn lex(src: &str) -> Vec<BfToken> {
|
||||
src.chars()
|
||||
.filter_map(|c| match c {
|
||||
'+' => Some(BfToken::Inc),
|
||||
'-' => Some(BfToken::Dec),
|
||||
'>' => Some(BfToken::IncPtr),
|
||||
'<' => Some(BfToken::DecPtr),
|
||||
'.' => Some(BfToken::Out),
|
||||
',' => Some(BfToken::In),
|
||||
'[' => Some(BfToken::Forward),
|
||||
']' => Some(BfToken::Back),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn _create_symbol(id: u32) -> Symbol {
|
||||
Symbol {
|
||||
name: format!("label_{id}"),
|
||||
module: Module::Resolved(0),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod brainf;
|
||||
pub mod project;
|
||||
@@ -0,0 +1,93 @@
|
||||
use crate::util::input;
|
||||
|
||||
pub fn tool_libcreate() {
|
||||
let mut ptype: String;
|
||||
loop {
|
||||
ptype = input("Enter project type (bin|lib)");
|
||||
if ptype == "bin" || ptype == "lib" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let project_name = input("Enter project name");
|
||||
let project_path = input("Enter Directory to create project in");
|
||||
|
||||
println!("[ Creating new {ptype} project {project_name} in {project_path} ]");
|
||||
|
||||
let template = match ptype.as_str() {
|
||||
"bin" => generate_bin_template(&project_name),
|
||||
"lib" => generate_lib_template(&project_name),
|
||||
_ => panic!("Invalid project type"),
|
||||
};
|
||||
|
||||
let path = format!("{project_path}/{project_name}.dsa");
|
||||
std::fs::write(path, template).expect("Unable to write file");
|
||||
}
|
||||
|
||||
fn generate_lib_template(module_name: &str) -> String {
|
||||
format!(
|
||||
r#"// {module_name}.dsa
|
||||
// usage:
|
||||
//
|
||||
// include {module_name} "<relative path>"
|
||||
//
|
||||
// usage for {module_name}_main:
|
||||
// push (arg1)
|
||||
// push (arg0)
|
||||
// call {module_name}::{module_name}_main
|
||||
// pop (arg0)
|
||||
// pop (arg1)
|
||||
|
||||
// Example data declarations
|
||||
// dw example_data: 0x0000
|
||||
|
||||
// Main function template
|
||||
{module_name}_main:
|
||||
// the correct way to start a function as defined by the calling convention
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
// explanation of how to access args
|
||||
ldw bpr, rg0, 8 // arg 0
|
||||
ldw bpr, rg0, 12 // arg 1
|
||||
|
||||
// your code goes here
|
||||
// Example: load example_data into rg1
|
||||
// ldw example_data, rg1
|
||||
|
||||
// the correct way to end a function as defined by the calling convention
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
fn generate_bin_template(project_name: &str) -> String {
|
||||
format!(
|
||||
r#"// {project_name}.dsa
|
||||
// Binary executable project
|
||||
|
||||
// Example Dependencies
|
||||
// include math "libs/math/math.dsa"
|
||||
include print "../resources/dsa/print.dsa"
|
||||
|
||||
// Data declarations - It is best practice to include these before any code!
|
||||
dw message: "Hello from {project_name}.dsa!" // strings are automatically null terminated!
|
||||
|
||||
// Program entry point - execution starts at the first non-definition line
|
||||
{project_name}:
|
||||
// Getting started: Calling external functions
|
||||
// Syntax: push (arg1), push (arg0), call namespace::function, pop (arg0), pop (arg1)
|
||||
|
||||
// Example: Print a string (if print library is included)
|
||||
ldw message, rg0 // load address of message
|
||||
push rg0 // push argument
|
||||
call print::print // call the print function
|
||||
pop rg0 // clean up stack
|
||||
|
||||
// Program must end with halt instruction
|
||||
halt
|
||||
"#,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused)]
|
||||
use std::{fmt, sync::mpsc::Sender};
|
||||
|
||||
// pub struct Entry {
|
||||
// etype: EntryType,
|
||||
// pub message: String,
|
||||
// }
|
||||
|
||||
// #[derive(Copy, Clone, Eq, PartialEq)]
|
||||
// enum EntryType {
|
||||
// Debug,
|
||||
// Info,
|
||||
// Warn,
|
||||
// Error,
|
||||
// Fatal,
|
||||
// }
|
||||
|
||||
// impl fmt::Display for EntryType {
|
||||
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// write!(
|
||||
// f,
|
||||
// "{:<5}",
|
||||
// match self {
|
||||
// Self::Debug => "DEBUG",
|
||||
// Self::Info => "INFO",
|
||||
// Self::Warn => "WARN",
|
||||
// Self::Error => "ERROR",
|
||||
// Self::Fatal => "FATAL",
|
||||
// }
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl fmt::Display for Entry {
|
||||
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// write!(f, "{}: {}", self.etype, self.message)
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,13 @@
|
||||
pub mod logging;
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
pub fn input(prompt: &str) -> String {
|
||||
print!("{prompt}\n > ");
|
||||
std::io::stdout().flush().expect("Failed to flush stdout");
|
||||
let mut input = String::new();
|
||||
std::io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("Failed to read line from stdin");
|
||||
input.trim().to_string()
|
||||
}
|
||||
Reference in New Issue
Block a user