pulley_interpreter/op.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
//! Pulley bytecode operations with their operands.
use crate::imms::*;
use crate::regs::*;
macro_rules! define_op {
(
$(
$( #[$attr:meta] )*
$snake_name:ident = $name:ident $( { $( $field:ident : $field_ty:ty ),* } )? ;
)*
) => {
/// A complete, materialized operation/instruction.
///
/// This type is useful for debugging, writing tests, etc... but is not
/// actually ever used by the interpreter, encoder, or decoder, all of
/// which avoid materializing ops.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum Op {
$(
$( #[$attr] )*
$name($name),
)*
/// An extended operation/instruction.
ExtendedOp(ExtendedOp),
}
$(
$( #[$attr] )*
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct $name { $(
$(
// TODO: add doc comments to all fields and update all
// the macros to match them.
#[allow(missing_docs)]
pub $field : $field_ty,
)*
)? }
impl From<$name> for Op {
#[inline]
fn from(op: $name) -> Self {
Self::$name(op)
}
}
)*
};
}
for_each_op!(define_op);
impl From<ExtendedOp> for Op {
#[inline]
fn from(op: ExtendedOp) -> Self {
Op::ExtendedOp(op)
}
}
macro_rules! define_extended_op {
(
$(
$( #[$attr:meta] )*
$snake_name:ident = $name:ident $( { $( $field:ident : $field_ty:ty ),* } )? ;
)*
) => {
/// An extended operation/instruction.
///
/// These tend to be colder than `Op`s.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ExtendedOp {
$(
$( #[$attr] )*
$name($name),
)*
}
$(
$( #[$attr] )*
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct $name { $(
$(
// TODO: add doc comments to all fields and update all
// the macros to match them.
#[allow(missing_docs)]
pub $field : $field_ty,
)*
)? }
impl From<$name> for Op {
#[inline]
fn from(op: $name) -> Self {
Self::ExtendedOp(ExtendedOp::$name(op))
}
}
impl From<$name> for ExtendedOp {
#[inline]
fn from(op: $name) -> Self {
Self::$name(op)
}
}
)*
};
}
for_each_extended_op!(define_extended_op);
macro_rules! define_op_encode {
(
$(
$( #[$attr:meta] )*
$snake_name:ident = $name:ident $( { $( $field:ident : $field_ty:ty ),* } )? ;
)*
) => {
impl Op {
/// Encode this op into the given sink.
#[cfg(feature = "encode")]
pub fn encode<E>(&self, into: &mut E)
where
E: Extend<u8>,
{
match self {
$(
Self::$name(op) => op.encode(into),
)*
Self::ExtendedOp(op) => op.encode(into),
}
}
}
$(
impl $name {
/// Encode this
#[doc = concat!("`", stringify!($name), "`")]
/// op into the given sink.
#[cfg(feature = "encode")]
pub fn encode<E>(&self, into: &mut E)
where
E: Extend<u8>,
{
crate::encode::$snake_name(into $( $( , self.$field )* )?);
}
}
)*
};
}
for_each_op!(define_op_encode);
macro_rules! define_extended_op_encode {
(
$(
$( #[$attr:meta] )*
$snake_name:ident = $name:ident $( { $( $field:ident : $field_ty:ty ),* } )? ;
)*
) => {
impl ExtendedOp {
/// Encode this extended op into the given sink.
#[cfg(feature = "encode")]
pub fn encode<E>(&self, into: &mut E)
where
E: Extend<u8>,
{
match self {
$(
Self::$name(op) => op.encode(into),
)*
}
}
}
$(
impl $name {
/// Encode this
#[doc = concat!("`", stringify!($name), "`")]
/// op into the given sink.
#[cfg(feature = "encode")]
pub fn encode<E>(&self, into: &mut E)
where
E: Extend<u8>,
{
crate::encode::$snake_name(into $( $( , self.$field )* )?);
}
}
)*
};
}
for_each_extended_op!(define_extended_op_encode);
/// A visitor that materializes whole `Op`s as it decodes the bytecode stream.
#[cfg(feature = "decode")]
#[derive(Default)]
pub struct MaterializeOpsVisitor<B> {
bytecode: B,
}
#[cfg(feature = "decode")]
impl<B> MaterializeOpsVisitor<B> {
/// Create a new op-materializing visitor for the given bytecode.
pub fn new(bytecode: B) -> Self {
Self { bytecode }
}
}
macro_rules! define_materialize_op_visitor {
(
$(
$( #[$attr:meta] )*
$snake_name:ident = $name:ident $( { $( $field:ident : $field_ty:ty ),* } )? ;
)*
) => {
#[cfg(feature = "decode")]
impl<B: crate::decode::BytecodeStream> crate::decode::OpVisitor for MaterializeOpsVisitor<B> {
type BytecodeStream = B;
fn bytecode(&mut self) -> &mut Self::BytecodeStream {
&mut self.bytecode
}
type Return = crate::op::Op;
$(
$( #[$attr] )*
fn $snake_name(&mut self $( $( , $field : $field_ty )* )? ) -> Self::Return {
crate::op::Op::$name(crate::op::$name { $( $(
$field,
)* )? })
}
)*
}
};
}
for_each_op!(define_materialize_op_visitor);
macro_rules! define_materialize_extended_op_visitor {
(
$(
$( #[$attr:meta] )*
$snake_name:ident = $name:ident $( { $( $field:ident : $field_ty:ty ),* } )? ;
)*
) => {
#[cfg(feature = "decode")]
impl<B: crate::decode::BytecodeStream> crate::decode::ExtendedOpVisitor for MaterializeOpsVisitor<B> {
$(
$( #[$attr] )*
fn $snake_name(&mut self $( $( , $field : $field_ty )* )? ) -> Self::Return {
crate::op::ExtendedOp::$name(crate::op::$name { $( $(
$field,
)* )? }).into()
}
)*
}
};
}
for_each_extended_op!(define_materialize_extended_op_visitor);