use super::{
allocated_abstract_instruction_set::AllocatedAbstractInstructionSet, register_allocator,
};
use crate::asm_lang::{allocated_ops::AllocatedOp, Op, RealizedOp};
use std::fmt;
use sway_error::error::CompileError;
#[derive(Clone)]
pub struct AbstractInstructionSet {
pub(crate) function: Option<(String, bool)>,
pub(crate) ops: Vec<Op>,
}
impl AbstractInstructionSet {
pub(crate) fn allocate_registers(
self,
) -> Result<AllocatedAbstractInstructionSet, CompileError> {
Ok(AllocatedAbstractInstructionSet {
function: self.function,
ops: register_allocator::allocate_registers(&self.ops)?,
})
}
}
impl fmt::Display for AbstractInstructionSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
".program:\n{}",
self.ops
.iter()
.filter_map(|x| {
let line = format!("{x}");
if line.is_empty() {
None
} else {
Some(line)
}
})
.collect::<Vec<_>>()
.join("\n")
)
}
}
pub struct RealizedAbstractInstructionSet {
pub(super) ops: Vec<RealizedOp>,
}
impl RealizedAbstractInstructionSet {
pub(crate) fn lower_to_allocated_ops(self) -> Vec<AllocatedOp> {
self.ops
.into_iter()
.map(
|RealizedOp {
opcode,
comment,
owning_span,
}| {
AllocatedOp {
opcode,
comment,
owning_span,
}
},
)
.collect::<Vec<_>>()
}
}