|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::atomic::AtomicU32;
|
|
|
|
|
|
|
|
use crate::asm::data::{Rd, RdData};
|
|
|
|
use crate::asm::data::literal::{Label, Value};
|
|
|
|
use crate::asm::error::{AsmError, Error};
|
|
|
|
use crate::asm::instr::{Cond, Instr, Op, Routine};
|
|
|
|
use crate::builtin::defs::BuiltinOp;
|
|
|
|
|
|
|
|
/// A trait for something that can turn into multiple instructions
|
|
|
|
pub trait Flatten {
|
|
|
|
fn flatten(self, label_num: &AtomicU32) -> Result<Vec<Op>, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Flatten for Instr {
|
|
|
|
fn flatten(self, label_num: &AtomicU32) -> Result<Vec<Op>, Error> {
|
|
|
|
let mut ops = vec![self.op];
|
|
|
|
|
|
|
|
if let Some(branches) = self.branches {
|
|
|
|
let labels = HashMap::<Cond, u32>::new();
|
|
|
|
let branch_count = branches.len();
|
|
|
|
let end_lbl = Label::unique(label_num);
|
|
|
|
for (cnt, (cond, branch)) in branches.into_iter().enumerate() {
|
|
|
|
if labels.contains_key(&cond) {
|
|
|
|
return Err(Error::Asm(AsmError::ConditionalAlreadyUsed(cond)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let next_lbl = if cnt == branch_count - 1 {
|
|
|
|
end_lbl.clone()
|
|
|
|
} else {
|
|
|
|
Label::unique(label_num)
|
|
|
|
};
|
|
|
|
ops.push(BuiltinOp::JumpIf(!cond, next_lbl.clone()).into());
|
|
|
|
|
|
|
|
for branch_instr in branch {
|
|
|
|
ops.extend(branch_instr.flatten(label_num)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
if cnt != branch_count - 1 {
|
|
|
|
ops.push(BuiltinOp::Jump(end_lbl.clone()).into());
|
|
|
|
ops.push(BuiltinOp::Label(next_lbl).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ops.push(BuiltinOp::Label(end_lbl).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ops)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Flatten for Routine {
|
|
|
|
fn flatten(self, label_num: &AtomicU32) -> Result<Vec<Op>, Error> {
|
|
|
|
let mut ops = vec![
|
|
|
|
BuiltinOp::Routine(self.name.clone()).into(),
|
|
|
|
];
|
|
|
|
|
|
|
|
for instr in self.body {
|
|
|
|
ops.extend(instr.flatten(label_num)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
ops.push(BuiltinOp::Barrier(Some(format!("Routine \"{}\" overrun", self.name).into())).into());
|
|
|
|
|
|
|
|
numbered_labels_to_skips(ops)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert jumps to relative skips
|
|
|
|
fn numbered_labels_to_skips(ops: Vec<Op>) -> Result<Vec<Op>, Error> {
|
|
|
|
let mut label_positions = HashMap::<Label, usize>::new();
|
|
|
|
for (n, op) in ops.iter().enumerate() {
|
|
|
|
if let Op::BuiltIn(BuiltinOp::Label(name @ Label::Numbered(_))) = op {
|
|
|
|
label_positions.insert(name.clone(), n - label_positions.len());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cleaned = vec![];
|
|
|
|
let mut skipped = 0;
|
|
|
|
for (n, op) in ops.into_iter().enumerate() {
|
|
|
|
match op {
|
|
|
|
Op::BuiltIn(BuiltinOp::Label(Label::Numbered(_))) => {
|
|
|
|
skipped += 1;
|
|
|
|
}
|
|
|
|
Op::BuiltIn(BuiltinOp::Jump(target @ Label::Numbered(_))) => {
|
|
|
|
if let Some(dest) = label_positions.get(&target) {
|
|
|
|
let skip = *dest as isize - n as isize + skipped;
|
|
|
|
cleaned.push(Op::BuiltIn(BuiltinOp::Skip(Rd::new(RdData::Immediate(skip as Value)))));
|
|
|
|
} else {
|
|
|
|
return Err(Error::Asm(AsmError::LabelNotDefined(target)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Op::BuiltIn(BuiltinOp::JumpIf(cond, target @ Label::Numbered(_))) => {
|
|
|
|
if let Some(dest) = label_positions.get(&target) {
|
|
|
|
let skip = *dest as isize - n as isize + skipped;
|
|
|
|
cleaned.push(Op::BuiltIn(BuiltinOp::SkipIf(cond, Rd::new(RdData::Immediate(skip as Value)))));
|
|
|
|
} else {
|
|
|
|
return Err(Error::Asm(AsmError::LabelNotDefined(target)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other => {
|
|
|
|
cleaned.push(other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(cleaned)
|
|
|
|
}
|