#![warn(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
#![allow(clippy::too_many_arguments)]
use asmkit::{
Arch, AsmError, CodeBuffer, CodeBufferFinalized, Environment, RelocDistance, RelocTarget, Span,
};
use offlineasm_ir::Lbl;
mod aarch64;
mod backward;
mod insn;
mod lower;
mod riscv64;
mod x86;
pub(crate) mod ir {
pub use crate::lower::{LabelResolver, LowerError, PatchHandle};
pub use offlineasm_ir::*;
}
pub use offlineasm_ir::regs;
#[doc = include_str!("../docs/reference.md")]
pub mod reference {}
#[allow(non_upper_case_globals, non_snake_case)]
pub mod mnem {
include!(concat!(env!("OUT_DIR"), "/mnem.rs"));
}
pub use asmkit::{
CodeLocationData, CodeLocationJump, DataLabel, ExternalName, JitAllocator, JitAllocatorOptions,
LoadedCode, PatchableJump, ResetPolicy, UserExternalName,
};
pub use ir::{
Disp, Extern, Fpr, Gpr, Label, LowerError, Mem, Mnemonic, Op, PatchHandle, Reach, Scratch,
SymId, SUPPORTED_MNEMONICS,
};
pub use onlineasm_macros::onlineasm;
pub use regs::{RegInfo, RegKind, VReg};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Target {
X64,
AArch64,
RISCV64,
}
#[allow(clippy::from_over_into)]
impl Into<Arch> for Target {
fn into(self) -> Arch {
match self {
Self::AArch64 => Arch::AArch64,
Self::RISCV64 => Arch::RISCV64,
Self::X64 => Arch::X64,
}
}
}
impl Target {
pub const ALL: [Target; 3] = [Target::X64, Target::AArch64, Target::RISCV64];
pub const fn callee_saved_gprs(self) -> &'static [&'static str] {
match self {
Target::X64 => &["csr0", "csr1", "csr2", "csr3", "csr4"],
Target::AArch64 => &[
"csr0", "csr1", "csr2", "csr3", "csr4", "csr5", "csr6", "csr7", "csr8", "csr9",
],
Target::RISCV64 => &[
"csr0", "csr1", "csr2", "csr3", "csr4", "csr5", "csr6", "csr7", "csr8", "csr9",
"csr10",
],
}
}
pub const fn callee_saved_gprs_for(self, abi: Abi) -> &'static [&'static str] {
match (self, abi) {
(Target::X64, Abi::Windows) => {
&["csr0", "csr1", "csr2", "csr3", "csr4", "csr5", "csr6"]
}
_ => self.callee_saved_gprs(),
}
}
pub const fn callee_saved_fprs(self) -> &'static [&'static str] {
match self {
Target::X64 => &[],
Target::AArch64 => &[
"csfr0", "csfr1", "csfr2", "csfr3", "csfr4", "csfr5", "csfr6", "csfr7",
],
Target::RISCV64 => &[
"csfr0", "csfr1", "csfr2", "csfr3", "csfr4", "csfr5", "csfr6", "csfr7",
],
}
}
pub const fn callee_saved_fprs_for(self, abi: Abi) -> &'static [&'static str] {
let _ = abi;
self.callee_saved_fprs()
}
pub const fn native() -> Self {
#[cfg(target_arch = "x86_64")]
{
Target::X64
}
#[cfg(target_arch = "aarch64")]
{
Target::AArch64
}
#[cfg(target_arch = "riscv64")]
{
Target::RISCV64
}
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64"
)))]
{
panic!("onlineasm: unsupported host architecture")
}
}
pub const fn arch(self) -> Arch {
match self {
Target::X64 => Arch::X64,
Target::AArch64 => Arch::AArch64,
Target::RISCV64 => Arch::RISCV64,
}
}
pub const fn as_str(self) -> &'static str {
match self {
Target::X64 => "x86_64",
Target::AArch64 => "aarch64",
Target::RISCV64 => "riscv64",
}
}
pub(crate) const fn info(self) -> &'static TargetInfo {
match self {
Target::X64 => &x86::INFO,
Target::AArch64 => &aarch64::INFO,
Target::RISCV64 => &riscv64::INFO,
}
}
pub const fn registers(self) -> &'static [RegInfo] {
self.info().registers
}
}
impl std::fmt::Display for Target {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
pub(crate) type LowerFn = fn(
&mut CodeBuffer,
&Scratch,
&[asmkit::Label],
u32,
Mnemonic,
&[Op],
Abi,
) -> Result<(), LowerError>;
pub(crate) type LowerPatchedFn = fn(
&mut CodeBuffer,
&Scratch,
&[asmkit::Label],
u32,
Mnemonic,
&[Op],
Abi,
) -> Result<PatchHandle, LowerError>;
pub(crate) struct TargetInfo {
pub(crate) scratch: Scratch,
pub(crate) registers: &'static [RegInfo],
pub(crate) vreg_gpr: &'static [(u16, Gpr)],
pub(crate) vreg_fpr: &'static [(u16, Fpr)],
pub(crate) lower: LowerFn,
pub(crate) lower_patched: LowerPatchedFn,
}
fn lookup_gpr(table: &[(u16, Gpr)], id: u16) -> Option<Gpr> {
table.iter().find(|&&(i, _)| i == id).map(|&(_, g)| g)
}
fn lookup_fpr(table: &[(u16, Fpr)], id: u16) -> Option<Fpr> {
table.iter().find(|&&(i, _)| i == id).map(|&(_, v)| v)
}
#[inline]
fn reject_offlineasm_only(mnemonic: Mnemonic, ops: &[Op]) -> Result<(), LowerError> {
let bad = |msg: &str| LowerError::BadOperand {
mnemonic: mnemonic.as_str().to_string(),
msg: msg.to_string(),
};
for op in ops {
match op {
Op::Sym(_) => return Err(bad("symbolic immediates are offlineasm-only")),
Op::Mem(m) if matches!(m.disp, Disp::Sym(_)) => {
return Err(bad("symbolic displacements are offlineasm-only"))
}
_ => {}
}
}
Ok(())
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub enum Abi {
#[default]
SysV,
Windows,
Darwin,
}
impl Abi {
pub fn detect_for(target: Target) -> Self {
let host = if cfg!(target_arch = "x86_64") {
Some(Target::X64)
} else if cfg!(target_arch = "aarch64") {
Some(Target::AArch64)
} else if cfg!(target_arch = "riscv64") {
Some(Target::RISCV64)
} else {
None
};
if cfg!(target_os = "windows") && host == Some(target) && target == Target::X64 {
Abi::Windows
} else if cfg!(target_vendor = "apple") && host == Some(target) {
Abi::Darwin
} else {
Abi::SysV
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct AssemblerOptions {
pub environment: Environment,
pub abi: Abi,
}
impl Default for AssemblerOptions {
fn default() -> Self {
Self {
environment: Environment::host(),
abi: Abi::detect_for(Target::native()),
}
}
}
impl AssemblerOptions {
pub fn detected(target: Target) -> Self {
Self {
environment: Environment::new(target.into()),
abi: Abi::detect_for(target),
}
}
pub fn with_environment(mut self, env: Environment) -> Self {
if !matches!(env.arch(), Arch::AArch64 | Arch::RISCV64 | Arch::X64) {
todo!("unsupported architecture")
}
self.environment = env;
self
}
pub fn with_pic(mut self, pic: bool) -> Self {
self.environment.set_pic(pic);
self
}
pub fn with_abi(mut self, abi: Abi) -> Self {
self.abi = abi;
self
}
pub fn target(&self) -> Target {
match self.environment.arch() {
Arch::AArch64 => Target::AArch64,
Arch::RISCV64 => Target::RISCV64,
Arch::X64 => Target::X64,
_ => unreachable!(),
}
}
}
pub use asmkit;
pub struct Assembler<const BACKWARD: bool = false> {
info: &'static TargetInfo,
target: Target,
buf: CodeBuffer,
ctx: Scratch,
labels: Vec<asmkit::Label>,
label_base: u32,
externs: Vec<(ExternalName, Extern)>,
abi: Abi,
tape: backward::ReverseTape,
}
pub type ForwardAssembler = Assembler<false>;
pub type BackwardAssembler = Assembler<true>;
impl<const BACKWARD: bool> Assembler<BACKWARD> {
fn with_options_inner(options: AssemblerOptions) -> Self {
let target = options.target();
let info = target.info();
Self {
info,
target,
buf: CodeBuffer::new(options.environment),
ctx: info.scratch,
labels: Vec::new(),
label_base: 0,
externs: Vec::new(),
abi: options.abi,
tape: backward::ReverseTape::new(),
}
}
pub const fn is_backward(&self) -> bool {
BACKWARD
}
pub fn extern_name(&mut self, name: impl Into<ExternalName>, reach: Reach) -> Extern {
let name = name.into();
let reach = if self.pic() { Reach::Far } else { reach };
if let Some((_, ext)) = self
.externs
.iter()
.find(|(n, e)| *n == name && e.reach == reach)
{
return *ext;
}
let distance = match reach {
Reach::Near => RelocDistance::Near,
Reach::Far => RelocDistance::Far,
};
let sym = self.buf.add_symbol(name.clone(), distance);
let ext = Extern {
id: ir::ExternId(sym.id()),
reach,
};
self.externs.push((name, ext));
ext
}
pub fn extern_far(&mut self, name: impl Into<ExternalName>) -> Extern {
self.extern_name(name, Reach::Far)
}
pub fn extern_near(&mut self, name: impl Into<ExternalName>) -> Extern {
self.extern_name(name, Reach::Near)
}
pub fn code_buffer(&self) -> &CodeBuffer {
&self.buf
}
pub fn environment(&self) -> &Environment {
self.code_buffer().env()
}
pub const fn abi(&self) -> Abi {
self.abi
}
pub fn pic(&self) -> bool {
self.environment().pic()
}
pub fn target(&self) -> Target {
self.target
}
pub fn scratch(&self) -> Scratch {
self.ctx
}
pub fn try_vreg_gpr(&self, vreg: VReg) -> Option<Gpr> {
(vreg.kind() == RegKind::Gpr)
.then(|| lookup_gpr(self.info.vreg_gpr, vreg.id()))
.flatten()
}
pub fn try_vreg_fpr(&self, vreg: VReg) -> Option<Fpr> {
(vreg.kind() == RegKind::Fpr)
.then(|| lookup_fpr(self.info.vreg_fpr, vreg.id()))
.flatten()
}
#[allow(clippy::panic)]
pub fn vreg_gpr(&self, vreg: VReg) -> Gpr {
self.try_vreg_gpr(vreg).unwrap_or_else(|| {
panic!(
"onlineasm: cannot use register `{}` on {}",
vreg.name(),
self.target
)
})
}
#[allow(clippy::panic)]
pub fn vreg_fpr(&self, vreg: VReg) -> Fpr {
self.try_vreg_fpr(vreg).unwrap_or_else(|| {
panic!(
"onlineasm: cannot use register `{}` on {}",
vreg.name(),
self.target
)
})
}
pub fn label(&mut self) -> Label {
if BACKWARD {
self.tape.next_virtual()
} else {
self.label_forward()
}
}
fn label_forward(&mut self) -> Label {
let l = self.buf.get_label();
self.labels.push(l);
Label(self.label_base.saturating_add(self.labels.len() as u32 - 1))
}
#[allow(clippy::panic)]
fn asm_label(&self, label: Label) -> asmkit::Label {
match label
.0
.checked_sub(self.label_base)
.and_then(|i| self.labels.get(i as usize))
{
Some(&l) => l,
None => panic!("onlineasm: label handle does not belong to this assembler"),
}
}
pub fn bind(&mut self, label: Label) {
if BACKWARD {
self.tape.push_bind(label);
} else {
self.bind_forward(label);
}
}
fn bind_forward(&mut self, label: Label) {
let l = self.asm_label(label);
self.buf.bind_label(l);
}
pub fn label_address_near(&mut self, dst: impl IntoOperand, label: Label) {
let dst = dst.into_operand(self);
if let (false, Target::AArch64, Op::Gpr(g)) = (BACKWARD, self.target, &dst) {
let l = self.asm_label(label);
let at = self.buf.cur_offset();
self.buf
.use_label_at_offset(at, l, asmkit::LabelUse::A64Adr21);
self.buf.put4(0x1000_0000 | u32::from(g.0));
return;
}
self.emit_checked(Mnemonic::Move, &[dst, Op::Label(Lbl::Bound(label))]);
}
#[allow(clippy::panic)]
pub fn offset(&self, label: Label) -> u32 {
if BACKWARD {
if self.tape.is_virtual(label) {
u32::MAX
} else {
panic!("onlineasm: label handle does not belong to this assembler");
}
} else {
self.buf.label_offset(self.asm_label(label))
}
}
pub fn align(&mut self, alignment: u32) {
assert!(
alignment.is_power_of_two(),
"onlineasm: alignment {alignment} is not a power of two"
);
if BACKWARD {
self.tape.push_align(alignment);
} else {
self.align_forward(alignment);
}
}
fn align_forward(&mut self, alignment: u32) {
if self.target == Target::X64 {
self.buf.align_to(alignment);
return;
}
while self.buf.cur_offset() & (alignment - 1) != 0 {
let before = self.buf.cur_offset();
if self.emit_forward(Mnemonic::Nop, &[]).is_err() {
unreachable!("the built-in `nop` lowering cannot fail");
}
assert!(
self.buf.cur_offset() > before,
"onlineasm: `nop` did not advance the buffer (buffer error: {:?})",
self.buf.error()
);
}
}
pub fn embed_data(&mut self, data: &[u8]) -> Label {
if BACKWARD {
self.tape.push_embed(data)
} else {
self.embed_data_forward(data)
}
}
pub fn embed_u64(&mut self, bits: u64) -> Label {
self.embed_data(&bits.to_ne_bytes())
}
fn embed_data_forward(&mut self, data: &[u8]) -> Label {
let constant = self.buf.add_constant(data.to_vec());
self.labels.push(self.buf.get_label_for_constant(constant));
Label(
self.label_base
.saturating_add(self.labels.len() as u32 - 1),
)
}
pub fn emit_data(&mut self, bytes: &[u8]) {
if BACKWARD {
self.tape.push_data(None, 1, bytes);
} else {
self.emit_data_forward(bytes);
}
}
pub fn emit_u32(&mut self, v: u32) {
self.emit_data(&v.to_le_bytes());
}
pub fn emit_u64(&mut self, v: u64) {
self.emit_data(&v.to_le_bytes());
}
pub fn align_data(&mut self, alignment: u32) {
assert!(
alignment.is_power_of_two(),
"onlineasm: alignment {alignment} is not a power of two"
);
if BACKWARD {
if alignment > 1 {
self.tape.push_data(None, alignment, &[]);
}
} else {
self.align_data_forward(alignment);
}
}
pub fn bind_data(&mut self, label: Label, alignment: u32, bytes: &[u8]) {
if BACKWARD {
self.tape.push_data(Some(label), alignment, bytes);
} else {
self.align_data_forward(alignment);
self.bind_forward(label);
self.emit_data_forward(bytes);
}
}
fn emit_data_forward(&mut self, bytes: &[u8]) {
for &byte in bytes {
self.buf.put1(byte);
}
}
fn align_data_forward(&mut self, alignment: u32) {
if alignment > 1 {
self.buf.align_to(alignment);
}
}
pub fn op(&self, v: impl IntoOperand) -> Op {
v.into_operand(self)
}
pub fn mem(&self, base: impl IntoOperand, disp: i32) -> Mem {
match self.op(base) {
Op::Label(l) => {
assert!(
disp == 0,
"onlineasm: a label-addressed memory operand has no displacement"
);
Mem::absolute(self.label_disp(l, false))
}
Op::Gpr(g) => Mem {
base: Some(g),
index: None,
disp: Disp::Const(disp),
},
other => panic!("onlineasm: expected a register operand, got {other:?}"),
}
}
pub fn mem_label(&self, label: Label) -> Mem {
Mem::absolute(self.label_disp(Lbl::Bound(label), false))
}
pub fn mem_label_far(&self, label: Label) -> Mem {
Mem::absolute(self.label_disp(Lbl::Bound(label), true))
}
fn label_disp(&self, l: Lbl, far: bool) -> Disp {
let Lbl::Bound(label) = l else {
panic!("onlineasm: a label-addressed memory operand needs a bound label")
};
if far {
Disp::LabelFar(label)
} else {
Disp::Label(label)
}
}
pub fn mem_indexed(
&self,
base: impl IntoOperand,
index: impl IntoOperand,
scale: u8,
disp: i32,
) -> Mem {
assert!(
matches!(scale, 1 | 2 | 4 | 8),
"scale must be 1, 2, 4, or 8"
);
Mem {
base: Some(self.gpr_operand(base)),
index: Some((self.gpr_operand(index), scale)),
disp: Disp::Const(disp),
}
}
#[allow(clippy::panic)]
fn gpr_operand(&self, v: impl IntoOperand) -> Gpr {
match v.into_operand(self) {
Op::Gpr(g) => g,
other => panic!("onlineasm: expected a register operand, got {other:?}"),
}
}
pub fn emit(&mut self, mnemonic: Mnemonic, ops: &[Op]) -> Result<(), LowerError> {
if BACKWARD {
reject_offlineasm_only(mnemonic, ops)?;
self.tape.push_emit(mnemonic, ops);
Ok(())
} else {
self.emit_forward(mnemonic, ops)
}
}
fn emit_forward(&mut self, mnemonic: Mnemonic, ops: &[Op]) -> Result<(), LowerError> {
reject_offlineasm_only(mnemonic, ops)?;
(self.info.lower)(
&mut self.buf,
&self.ctx,
&self.labels,
self.label_base,
mnemonic,
ops,
self.abi,
)
}
#[allow(clippy::panic)]
fn emit_checked(&mut self, mnemonic: Mnemonic, ops: &[Op]) {
if let Err(e) = self.emit(mnemonic, ops) {
panic!("onlineasm: lowering `{mnemonic}` failed: {e}");
}
}
pub fn emit_patched(
&mut self,
mnemonic: Mnemonic,
ops: &[Op],
) -> Result<PatchHandle, LowerError> {
if BACKWARD {
reject_offlineasm_only(mnemonic, ops)?;
Ok(self.tape.push_emit_patched(mnemonic, ops))
} else {
self.emit_forward_patched(mnemonic, ops)
}
}
fn emit_forward_patched(
&mut self,
mnemonic: Mnemonic,
ops: &[Op],
) -> Result<PatchHandle, LowerError> {
reject_offlineasm_only(mnemonic, ops)?;
(self.info.lower_patched)(
&mut self.buf,
&self.ctx,
&self.labels,
self.label_base,
mnemonic,
ops,
self.abi,
)
}
fn finalize(
&mut self,
labels: &[asmkit::Label],
label_base: u32,
patch_base: u32,
virtual_patches: Vec<PatchHandle>,
) -> Result<Finished, AsmError> {
self.buf.emit_island(u32::MAX)?;
let inner = self.buf.finish_owned()?;
let offsets: Vec<u32> = labels.iter().map(|l| inner.label_offset(*l)).collect();
self.buf.clear();
Ok(Finished {
inner: Box::new(inner),
offsets,
label_base,
patch_base,
linked: false,
virtual_patches,
})
}
pub fn reset(&mut self) {
self.label_base = self.label_base.saturating_add(self.labels.len() as u32);
self.buf.clear();
self.labels.clear();
self.externs.clear();
self.tape.reset();
}
}
impl Assembler<false> {
pub fn new(target: Target) -> Self {
Self::with_options(AssemblerOptions::detected(target))
}
pub fn with_options(options: AssemblerOptions) -> Self {
Self::with_options_inner(options)
}
pub fn native() -> Self {
Self::new(Target::native())
}
pub fn finish(&mut self) -> Result<Finished, AsmError> {
let labels = std::mem::take(&mut self.labels);
let res = self.finalize(&labels, self.label_base, 0, Vec::new());
self.labels = labels;
self.reset();
res
}
}
impl Assembler<true> {
pub fn new_backward(target: Target) -> Self {
Self::with_options_backward(AssemblerOptions::detected(target))
}
pub fn with_options_backward(options: AssemblerOptions) -> Self {
Self::with_options_inner(options)
}
pub fn native_backward() -> Self {
Self::new_backward(Target::native())
}
pub fn finish(&mut self) -> Result<Finished, BackwardFinishError> {
let res = self.replay();
self.reset();
res
}
fn replay(&mut self) -> Result<Finished, BackwardFinishError> {
let virtual_count = self.tape.virtual_count();
let patch_count = self.tape.patch_count();
let virtual_base = self.tape.virtual_base();
let patch_base = self.tape.patch_base();
let mut items = self.tape.take();
let mut embedded: Vec<Option<&[u8]>> = vec![None; virtual_count as usize];
for item in &items {
if let backward::TapeItem::Embed { data, label } = item {
if let Some(index) = label
.0
.checked_sub(virtual_base)
.and_then(|i| usize::try_from(i).ok())
{
embedded[index] = Some(data);
}
}
}
let mut real: Vec<Label> = Vec::with_capacity(virtual_count as usize);
for payload in embedded {
match payload {
Some(data) => real.push(self.embed_data_forward(data)),
None => real.push(self.label_forward()),
}
}
let mut patches: Vec<PatchHandle> = Vec::with_capacity(patch_count as usize);
let items_len = items.len();
for (position, item) in items.iter_mut().rev().enumerate() {
let index = items_len - 1 - position;
match item {
backward::TapeItem::Emit { mnemonic, ops } => {
backward::remap_ops(ops, &real, virtual_base);
self.emit_forward(*mnemonic, ops)
.map_err(|source| BackwardFinishError::Lower { index, source })?;
}
backward::TapeItem::EmitPatched { mnemonic, ops } => {
backward::remap_ops(ops, &real, virtual_base);
let handle = self
.emit_forward_patched(*mnemonic, ops)
.map_err(|source| BackwardFinishError::Lower { index, source })?;
patches.push(handle);
}
backward::TapeItem::Bind(v) => {
self.bind_forward(backward::remap_label(*v, &real, virtual_base));
}
backward::TapeItem::Align(alignment) => self.align_forward(*alignment),
backward::TapeItem::Embed { .. } => {}
backward::TapeItem::Data { data, label, align } => {
self.align_data_forward(*align);
if let Some(v) = label {
self.bind_forward(backward::remap_label(*v, &real, virtual_base));
}
self.emit_data_forward(data);
}
}
}
patches.reverse();
let labels: Vec<asmkit::Label> = real.iter().map(|r| self.asm_label(*r)).collect();
self.finalize(&labels, virtual_base, patch_base, patches)
.map_err(BackwardFinishError::Finalize)
}
}
impl Default for Assembler<false> {
fn default() -> Self {
Self::native()
}
}
#[derive(Debug)]
pub enum BackwardFinishError {
Lower { index: usize, source: LowerError },
Finalize(AsmError),
}
impl std::fmt::Display for BackwardFinishError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Lower { index, source } => {
write!(f, "replaying recorded call {index} failed: {source}")
}
Self::Finalize(e) => write!(f, "finalization failed: {e}"),
}
}
}
impl std::error::Error for BackwardFinishError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Lower { source, .. } => Some(source),
Self::Finalize(e) => Some(e),
}
}
}
pub struct Finished {
inner: Box<CodeBufferFinalized>,
offsets: Vec<u32>,
label_base: u32,
patch_base: u32,
linked: bool,
virtual_patches: Vec<PatchHandle>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PatchLocation {
Jump(CodeLocationJump),
Data(CodeLocationData),
}
impl Finished {
pub fn data(&self) -> &[u8] {
self.inner.data()
}
pub fn offset(&self, label: Label) -> Option<u32> {
let index = label.0.checked_sub(self.label_base)? as usize;
match self.offsets.get(index) {
Some(&off) if off != u32::MAX => Some(off),
_ => None,
}
}
pub fn location(&self, handle: PatchHandle) -> Option<PatchLocation> {
if self.linked {
return None;
}
let real = match handle {
PatchHandle::Virtual(id) => {
let index = id.checked_sub(self.patch_base)? as usize;
*self.virtual_patches.get(index)?
}
real => real,
};
Some(match real {
PatchHandle::Jump(jump) => PatchLocation::Jump(self.inner.location_of(jump)),
PatchHandle::Data(data) => PatchLocation::Data(self.inner.location_of(data)),
PatchHandle::Virtual(_) => {
unreachable!("onlineasm: resolved patch handle is never virtual")
}
})
}
pub fn allocate(&self, jit: &mut JitAllocator) -> Result<Span, AsmError> {
if self.has_externs() {
return Err(AsmError::InvalidState);
}
self.inner.allocate(jit)
}
pub fn has_externs(&self) -> bool {
self.inner
.relocs()
.iter()
.any(|r| matches!(r.target, RelocTarget::Sym(_)))
}
pub fn load(
&self,
jit: &mut JitAllocator,
resolve: impl Fn(&ExternalName) -> *const u8,
) -> Result<LoadedCode, AsmError> {
self.inner.load(jit, resolve)
}
pub fn link(images: impl IntoIterator<Item = Finished>) -> Result<Finished, AsmError> {
let mut linker = asmkit::Linker::new();
let mut any = false;
for image in images {
linker.add_buffer(*image.inner);
any = true;
}
if !any {
return Err(AsmError::NoCodeGenerated);
}
Ok(Finished {
inner: Box::new(linker.link()?),
offsets: Vec::new(),
label_base: 0,
patch_base: 0,
linked: true,
virtual_patches: Vec::new(),
})
}
pub fn inner(&self) -> &CodeBufferFinalized {
&self.inner
}
}
pub unsafe fn repatch_u64(
jit: &mut JitAllocator,
span: &mut Span,
location: &PatchLocation,
value: u64,
) -> Result<(), AsmError> {
let PatchLocation::Data(data) = *location else {
return Err(AsmError::InvalidArgument);
};
unsafe { asmkit::repatch_value_span(jit, span, data, value) }
}
pub trait IntoOperand {
fn into_operand<const BACKWARD: bool>(self, asm: &Assembler<BACKWARD>) -> Op;
}
impl IntoOperand for Op {
fn into_operand<const BACKWARD: bool>(self, _asm: &Assembler<BACKWARD>) -> Op {
self
}
}
macro_rules! imm_operand {
($($t:ty),* $(,)?) => {
$(impl IntoOperand for $t {
fn into_operand<const BACKWARD: bool>(self, _asm: &Assembler<BACKWARD>) -> Op {
Op::Imm(self as i64)
}
})*
};
}
imm_operand!(i8, i16, i32, i64, u8, u16, u32, u64, isize, usize);
impl IntoOperand for Gpr {
fn into_operand<const BACKWARD: bool>(self, _asm: &Assembler<BACKWARD>) -> Op {
Op::Gpr(self)
}
}
impl IntoOperand for Fpr {
fn into_operand<const BACKWARD: bool>(self, _asm: &Assembler<BACKWARD>) -> Op {
Op::Fpr(self)
}
}
impl IntoOperand for Mem {
fn into_operand<const BACKWARD: bool>(self, _asm: &Assembler<BACKWARD>) -> Op {
Op::Mem(self)
}
}
impl IntoOperand for Extern {
fn into_operand<const BACKWARD: bool>(self, _asm: &Assembler<BACKWARD>) -> Op {
Op::Extern(self)
}
}
impl IntoOperand for Label {
fn into_operand<const BACKWARD: bool>(self, _asm: &Assembler<BACKWARD>) -> Op {
Op::Label(Lbl::Bound(self))
}
}
impl IntoOperand for VReg {
fn into_operand<const BACKWARD: bool>(self, asm: &Assembler<BACKWARD>) -> Op {
match self.kind() {
RegKind::Gpr => Op::Gpr(asm.vreg_gpr(self)),
RegKind::Fpr => Op::Fpr(asm.vreg_fpr(self)),
}
}
}
impl<T: IntoOperand + Copy> IntoOperand for &T {
fn into_operand<const BACKWARD: bool>(self, asm: &Assembler<BACKWARD>) -> Op {
(*self).into_operand(asm)
}
}