Skip to main content

cranelift_assembler_x64_meta/dsl/
features.rs

1//! A DSL for describing x64 CPU features.
2
3use core::fmt;
4use std::ops::{BitAnd, BitOr};
5
6/// A boolean term of CPU features.
7///
8/// An instruction is valid when the boolean term (a recursive tree of `AND` and
9/// `OR` terms) is satisfied.
10///
11/// ```
12/// # use cranelift_assembler_x64_meta::dsl::{Features, Feature};
13/// let fs = Feature::_64b | Feature::compat;
14/// assert_eq!(fs.to_string(), "(_64b | compat)");
15/// ```
16#[derive(PartialEq)]
17pub enum Features {
18    And(Box<Features>, Box<Features>),
19    Or(Box<Features>, Box<Features>),
20    Feature(Feature),
21}
22
23impl Features {
24    pub(crate) fn is_sse(&self) -> bool {
25        use Feature::*;
26        match self {
27            Features::And(lhs, rhs) => lhs.is_sse() || rhs.is_sse(),
28            Features::Or(lhs, rhs) => lhs.is_sse() || rhs.is_sse(),
29            Features::Feature(feature) => {
30                matches!(feature, sse | sse2 | sse3 | ssse3 | sse41 | sse42)
31            }
32        }
33    }
34}
35
36impl fmt::Display for Features {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Features::And(lhs, rhs) => write!(f, "({lhs} & {rhs})"),
40            Features::Or(lhs, rhs) => write!(f, "({lhs} | {rhs})"),
41            Features::Feature(feature) => write!(f, "{feature:#?}"),
42        }
43    }
44}
45
46impl<T> BitOr<T> for Features
47where
48    T: Into<Features>,
49{
50    type Output = Features;
51    fn bitor(self, rhs: T) -> Self::Output {
52        Features::Or(Box::new(self), Box::new(rhs.into()))
53    }
54}
55
56impl<T> BitAnd<T> for Features
57where
58    T: Into<Features>,
59{
60    type Output = Features;
61    fn bitand(self, rhs: T) -> Self::Output {
62        Features::And(Box::new(self), Box::new(rhs.into()))
63    }
64}
65
66/// A CPU feature.
67///
68/// IA-32e mode is the typical mode of operation for modern 64-bit x86
69/// processors. It consists of two sub-modes:
70/// - __64-bit mode__: uses the full 64-bit address space
71/// - __compatibility mode__: allows use of legacy 32-bit code
72///
73/// Other features listed here should match the __CPUID Feature Flags__ column
74/// of the instruction tables of the x64 reference manual.
75#[derive(Clone, Copy, Debug, PartialEq)]
76#[allow(non_camel_case_types, reason = "makes DSL definitions easier to read")]
77pub enum Feature {
78    _64b,
79    compat,
80    sse,
81    sse2,
82    sse3,
83    ssse3,
84    sse41,
85    sse42,
86    bmi1,
87    bmi2,
88    lzcnt,
89    popcnt,
90    avx,
91    avx2,
92    avx512f,
93    avx512vl,
94    avx512dq,
95    avx512bitalg,
96    avx512vbmi,
97    cmpxchg16b,
98    fma,
99    avx_vnni,
100    avx512vnni,
101}
102
103/// List all CPU features.
104///
105/// It is critical that this list contains _all_ variants of the [`Feature`]
106/// `enum`. We use this list here in the `meta` level so that we can accurately
107/// transcribe each variant to an `enum` available in the generated layer above.
108/// If this list is incomplete, we will (fortunately) see compile errors for
109/// generated functions that use the missing variants.
110pub const ALL_FEATURES: &[Feature] = &[
111    Feature::_64b,
112    Feature::compat,
113    Feature::sse,
114    Feature::sse2,
115    Feature::sse3,
116    Feature::ssse3,
117    Feature::sse41,
118    Feature::sse42,
119    Feature::bmi1,
120    Feature::bmi2,
121    Feature::lzcnt,
122    Feature::popcnt,
123    Feature::avx,
124    Feature::avx2,
125    Feature::avx512f,
126    Feature::avx512vl,
127    Feature::avx512dq,
128    Feature::avx512bitalg,
129    Feature::avx512vbmi,
130    Feature::cmpxchg16b,
131    Feature::fma,
132    Feature::avx_vnni,
133    Feature::avx512vnni,
134];
135
136impl fmt::Display for Feature {
137    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
138        fmt::Debug::fmt(self, f)
139    }
140}
141
142impl From<Feature> for Features {
143    fn from(f: Feature) -> Self {
144        Features::Feature(f)
145    }
146}
147
148impl<T> BitAnd<T> for Feature
149where
150    T: Into<Features>,
151{
152    type Output = Features;
153    fn bitand(self, rhs: T) -> Self::Output {
154        Features::from(self) & rhs.into()
155    }
156}
157
158impl<T> BitOr<T> for Feature
159where
160    T: Into<Features>,
161{
162    type Output = Features;
163    fn bitor(self, rhs: T) -> Self::Output {
164        Features::from(self) | rhs.into()
165    }
166}