use super::ir::{Disp, Label, Lbl, Mnemonic, Op};
use super::lower::PatchHandle;
use smallvec::SmallVec;
pub(super) type Ops = SmallVec<[Op; 4]>;
pub(super) enum TapeItem {
Emit { mnemonic: Mnemonic, ops: Ops },
EmitPatched { mnemonic: Mnemonic, ops: Ops },
Bind(Label),
Align(u32),
Embed { data: Box<[u8]>, label: Label },
Data {
data: Box<[u8]>,
label: Option<Label>,
align: u32,
},
}
pub(super) struct ReverseTape {
items: Vec<TapeItem>,
next_virtual: u32,
next_patch: u32,
virtual_base: u32,
patch_base: u32,
}
impl ReverseTape {
pub(super) fn new() -> Self {
Self {
items: Vec::new(),
next_virtual: 0,
next_patch: 0,
virtual_base: 0,
patch_base: 0,
}
}
pub(super) fn reset(&mut self) {
self.items.clear();
self.virtual_base = self.virtual_base.saturating_add(self.next_virtual);
self.patch_base = self.patch_base.saturating_add(self.next_patch);
self.next_virtual = 0;
self.next_patch = 0;
}
pub(super) fn virtual_base(&self) -> u32 {
self.virtual_base
}
pub(super) fn patch_base(&self) -> u32 {
self.patch_base
}
pub(super) fn push_emit(&mut self, mnemonic: Mnemonic, ops: &[Op]) {
self.items.push(TapeItem::Emit {
mnemonic,
ops: SmallVec::from_slice(ops),
});
}
pub(super) fn push_emit_patched(&mut self, mnemonic: Mnemonic, ops: &[Op]) -> PatchHandle {
let id = self.patch_base.saturating_add(self.next_patch);
self.next_patch += 1;
self.items.push(TapeItem::EmitPatched {
mnemonic,
ops: SmallVec::from_slice(ops),
});
PatchHandle::Virtual(id)
}
pub(super) fn push_bind(&mut self, label: Label) {
self.check_virtual(label);
self.items.push(TapeItem::Bind(label));
}
pub(super) fn push_align(&mut self, alignment: u32) {
self.items.push(TapeItem::Align(alignment));
}
pub(super) fn push_embed(&mut self, data: &[u8]) -> Label {
let label = self.next_virtual();
self.items.push(TapeItem::Embed {
data: data.into(),
label,
});
label
}
pub(super) fn push_data(&mut self, label: Option<Label>, align: u32, data: &[u8]) {
if let Some(label) = label {
self.check_virtual(label);
}
self.items.push(TapeItem::Data {
data: data.into(),
label,
align,
});
}
pub(super) fn next_virtual(&mut self) -> Label {
let id = self.virtual_base.saturating_add(self.next_virtual);
self.next_virtual += 1;
Label(id)
}
pub(super) fn is_virtual(&self, label: Label) -> bool {
label.0 >= self.virtual_base && label.0 - self.virtual_base < self.next_virtual
}
pub(super) fn virtual_count(&self) -> u32 {
self.next_virtual
}
pub(super) fn patch_count(&self) -> u32 {
self.next_patch
}
pub(super) fn take(&mut self) -> Vec<TapeItem> {
std::mem::take(&mut self.items)
}
#[allow(clippy::panic)]
fn check_virtual(&self, label: Label) {
if !self.is_virtual(label) {
panic!("onlineasm: label handle does not belong to this assembler");
}
}
}
#[allow(clippy::panic)]
pub(super) fn remap_label(v: Label, real: &[Label], base: u32) -> Label {
let index = v.0.checked_sub(base).and_then(|i| usize::try_from(i).ok());
match index.and_then(|i| real.get(i)) {
Some(&r) => r,
None => panic!("onlineasm: label handle does not belong to this assembler"),
}
}
pub(super) fn remap_op(op: Op, real: &[Label], base: u32) -> Op {
match op {
Op::Label(Lbl::Bound(v)) => Op::Label(Lbl::Bound(remap_label(v, real, base))),
Op::Mem(mut m) => {
m.disp = match m.disp {
Disp::Label(v) => Disp::Label(remap_label(v, real, base)),
Disp::LabelFar(v) => Disp::LabelFar(remap_label(v, real, base)),
other => other,
};
Op::Mem(m)
}
other => other,
}
}
pub(super) fn remap_ops(ops: &mut [Op], real: &[Label], base: u32) {
for op in ops {
*op = remap_op(*op, real, base);
}
}