Path: blob/main/cranelift/assembler-x64/meta/src/dsl/custom.rs
1693 views
//! Define customizations available for aspects of the assembler.1//!2//! When a customization is applied to an instruction, the generated code will3//! call the corresponding function in a `custom` module4//! (`custom::<customization>::<inst>`). E.g., to modify the display of a `NOP`5//! instruction with format `M`, the generated assembler will call:6//! `custom::display::nop_m(...)`.78use core::fmt;9use std::ops::BitOr;1011#[derive(PartialEq, Debug)]12pub enum Customization {13/// Modify the disassembly of an instruction.14Display,15/// Modify how an instruction is emitted into the code buffer.16Encode,17/// Modify the instruction mnemonic (see [`crate::dsl::Inst::mnemonic`]);18/// this customization is irrelevant if [`CustomOperation::Display`] is also19/// specified.20Mnemonic,21/// Modify how a register allocator visits the operands of an instruction.22Visit,23}2425impl fmt::Display for Customization {26fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {27fmt::Debug::fmt(self, f)28}29}3031impl BitOr for Customization {32type Output = Custom;33fn bitor(self, rhs: Self) -> Self::Output {34assert_ne!(self, rhs, "duplicate custom operation: {self:?}");35Custom(vec![self, rhs])36}37}3839impl BitOr<Customization> for Custom {40type Output = Custom;41fn bitor(mut self, rhs: Customization) -> Self::Output {42assert!(43!self.0.contains(&rhs),44"duplicate custom operation: {rhs:?}"45);46self.0.push(rhs);47self48}49}5051#[derive(PartialEq, Default)]52pub struct Custom(Vec<Customization>);5354impl Custom {55#[must_use]56pub fn is_empty(&self) -> bool {57self.0.is_empty()58}5960pub fn iter(&self) -> impl Iterator<Item = &Customization> {61self.0.iter()62}6364pub fn contains(&self, operation: Customization) -> bool {65self.0.contains(&operation)66}67}6869impl fmt::Display for Custom {70fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {71write!(72f,73"{}",74self.075.iter()76.map(ToString::to_string)77.collect::<Vec<_>>()78.join(" | ")79)80}81}8283impl From<Customization> for Custom {84fn from(operation: Customization) -> Self {85Custom(vec![operation])86}87}888990