about summary refs log tree commit diff
path: root/library/stdarch/crates/stdarch-gen-arm/src/matching.rs
blob: 0c480620428277aeaf4f5aa1152b23d679893f5f (plain)
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
use proc_macro2::TokenStream;
use quote::ToTokens;
use serde::{Deserialize, Serialize};
use std::fmt;

use crate::context::{self, LocalContext};
use crate::typekinds::{BaseType, BaseTypeKind, TypeKind};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MatchSizeValues<T> {
    pub default: T,
    pub byte: Option<T>,
    pub halfword: Option<T>,
    pub doubleword: Option<T>,
}

impl<T> MatchSizeValues<T> {
    pub fn get(&mut self, ty: &TypeKind, ctx: &LocalContext) -> context::Result<&T> {
        let base_ty = if let Some(w) = ty.wildcard() {
            ctx.provide_type_wildcard(w)?
        } else {
            ty.clone()
        };

        if let BaseType::Sized(_, bitsize) = base_ty.base_type().unwrap() {
            match (bitsize, &self.byte, &self.halfword, &self.doubleword) {
                (64, _, _, Some(v)) | (16, _, Some(v), _) | (8, Some(v), _, _) => Ok(v),
                _ => Ok(&self.default),
            }
        } else {
            Err(format!("cannot match bitsize to unsized type {ty:?}!"))
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MatchKindValues<T> {
    pub default: T,
    pub float: Option<T>,
    pub unsigned: Option<T>,
}

impl<T> MatchKindValues<T> {
    pub fn get(&mut self, ty: &TypeKind, ctx: &LocalContext) -> context::Result<&T> {
        let base_ty = if let Some(w) = ty.wildcard() {
            ctx.provide_type_wildcard(w)?
        } else {
            ty.clone()
        };

        match (
            base_ty.base_type().unwrap().kind(),
            &self.float,
            &self.unsigned,
        ) {
            (BaseTypeKind::Float, Some(v), _) | (BaseTypeKind::UInt, _, Some(v)) => Ok(v),
            _ => Ok(&self.default),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum SizeMatchable<T> {
    Matched(T),
    Unmatched {
        match_size: Option<TypeKind>,
        #[serde(flatten)]
        values: MatchSizeValues<Box<T>>,
    },
}

impl<T: Clone> SizeMatchable<T> {
    pub fn perform_match(&mut self, ctx: &LocalContext) -> context::Result {
        match self {
            Self::Unmatched {
                match_size: None,
                values: MatchSizeValues { default, .. },
            } => *self = Self::Matched(*default.to_owned()),
            Self::Unmatched {
                match_size: Some(ty),
                values,
            } => *self = Self::Matched(*values.get(ty, ctx)?.to_owned()),
            _ => {}
        }
        Ok(())
    }
}

impl<T: fmt::Debug> AsRef<T> for SizeMatchable<T> {
    fn as_ref(&self) -> &T {
        if let SizeMatchable::Matched(v) = self {
            v
        } else {
            panic!("no match for {self:?} was performed");
        }
    }
}

impl<T: fmt::Debug> AsMut<T> for SizeMatchable<T> {
    fn as_mut(&mut self) -> &mut T {
        if let SizeMatchable::Matched(v) = self {
            v
        } else {
            panic!("no match for {self:?} was performed");
        }
    }
}

impl<T: fmt::Debug + ToTokens> ToTokens for SizeMatchable<T> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        self.as_ref().to_tokens(tokens)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum KindMatchable<T> {
    Matched(T),
    Unmatched {
        match_kind: Option<TypeKind>,
        #[serde(flatten)]
        values: MatchKindValues<Box<T>>,
    },
}

impl<T: Clone> KindMatchable<T> {
    pub fn perform_match(&mut self, ctx: &LocalContext) -> context::Result {
        match self {
            Self::Unmatched {
                match_kind: None,
                values: MatchKindValues { default, .. },
            } => *self = Self::Matched(*default.to_owned()),
            Self::Unmatched {
                match_kind: Some(ty),
                values,
            } => *self = Self::Matched(*values.get(ty, ctx)?.to_owned()),
            _ => {}
        }
        Ok(())
    }
}

impl<T: fmt::Debug> AsRef<T> for KindMatchable<T> {
    fn as_ref(&self) -> &T {
        if let KindMatchable::Matched(v) = self {
            v
        } else {
            panic!("no match for {self:?} was performed");
        }
    }
}

impl<T: fmt::Debug> AsMut<T> for KindMatchable<T> {
    fn as_mut(&mut self) -> &mut T {
        if let KindMatchable::Matched(v) = self {
            v
        } else {
            panic!("no match for {self:?} was performed");
        }
    }
}

impl<T: fmt::Debug + ToTokens> ToTokens for KindMatchable<T> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        self.as_ref().to_tokens(tokens)
    }
}