hashx/
compiler.rs

1//! Architecture-specific compiled implementations for HashX
2//!
3//! This module provides a consistent interface across configurations and
4//! targets via the [`Executable`] struct and the [`Architecture`] trait.
5//! When the compiler is unavailable, an `Executable` is empty and the
6//! `Architecture` is defined here as a stub implementation. Otherwise, the
7//! `Executable` wraps a mmap buffer from the `dynasmrt` crate and the
8//! `Architecture` is implemented in a CPU-specific way.
9
10use crate::{program::InstructionArray, register::RegisterFile, CompilerError};
11
12#[cfg(all(feature = "compiler", target_arch = "x86_64"))]
13mod x86_64;
14
15#[cfg(all(feature = "compiler", target_arch = "aarch64"))]
16mod aarch64;
17
18#[cfg(all(
19    feature = "compiler",
20    any(target_arch = "x86_64", target_arch = "aarch64")
21))]
22mod util;
23
24/// Wrapper for a compiled program, empty when compiler support is disabled
25pub(crate) struct Executable {
26    /// Mapped memory, read-only and executable
27    ///
28    /// On platforms with compiler support, this item is present.
29    /// If the compiler is unavailable, Executable will be empty.
30    #[cfg(all(
31        feature = "compiler",
32        any(target_arch = "x86_64", target_arch = "aarch64")
33    ))]
34    buffer: util::ExecutableBuffer,
35}
36
37/// Default implementation for [`Architecture`], used
38/// when the compiler is disabled or the target architecture is unsupported
39#[cfg(any(
40    not(feature = "compiler"),
41    not(any(target_arch = "x86_64", target_arch = "aarch64"))
42))]
43impl Architecture for Executable {
44    fn compile(_program: &InstructionArray) -> Result<Self, CompilerError> {
45        Err(CompilerError::NotAvailable)
46    }
47
48    fn invoke(&self, _regs: &mut RegisterFile) {
49        unreachable!();
50    }
51}
52
53/// Default implementation for [`Debug`] on [`Executable`], when the compiler
54/// is currently disabled or unsupported on the target
55///
56/// There should never be an [`Executable`] instance in these cases.
57/// Always panics.
58#[cfg(any(
59    not(feature = "compiler"),
60    not(any(target_arch = "x86_64", target_arch = "aarch64"))
61))]
62impl std::fmt::Debug for Executable {
63    fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        unreachable!()
65    }
66}
67
68/// Trait that adds architecture-specific functionality to Executable
69pub(crate) trait Architecture
70where
71    Self: Sized,
72{
73    /// Compile an array of instructions into an Executable
74    fn compile(program: &InstructionArray) -> Result<Self, CompilerError>;
75
76    /// Run the compiled code, with a RegisterFile for input and output
77    fn invoke(&self, regs: &mut RegisterFile);
78}