1 //! This module predefines all the Cranelift scalar types. 2 3 #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] 4 pub(crate) enum Bool { 5 /// 1-bit bool. 6 B1 = 1, 7 /// 8-bit bool. 8 B8 = 8, 9 /// 16-bit bool. 10 B16 = 16, 11 /// 32-bit bool. 12 B32 = 32, 13 /// 64-bit bool. 14 B64 = 64, 15 /// 128-bit bool. 16 B128 = 128, 17 } 18 19 /// This provides an iterator through all of the supported bool variants. 20 pub(crate) struct BoolIterator { 21 index: u8, 22 } 23 24 impl BoolIterator { new() -> Self25 pub fn new() -> Self { 26 Self { index: 0 } 27 } 28 } 29 30 impl Iterator for BoolIterator { 31 type Item = Bool; next(&mut self) -> Option<Self::Item>32 fn next(&mut self) -> Option<Self::Item> { 33 let res = match self.index { 34 0 => Some(Bool::B1), 35 1 => Some(Bool::B8), 36 2 => Some(Bool::B16), 37 3 => Some(Bool::B32), 38 4 => Some(Bool::B64), 39 5 => Some(Bool::B128), 40 _ => return None, 41 }; 42 self.index += 1; 43 res 44 } 45 } 46 47 #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] 48 pub(crate) enum Int { 49 /// 8-bit int. 50 I8 = 8, 51 /// 16-bit int. 52 I16 = 16, 53 /// 32-bit int. 54 I32 = 32, 55 /// 64-bit int. 56 I64 = 64, 57 /// 128-bit int. 58 I128 = 128, 59 } 60 61 /// This provides an iterator through all of the supported int variants. 62 pub(crate) struct IntIterator { 63 index: u8, 64 } 65 66 impl IntIterator { new() -> Self67 pub fn new() -> Self { 68 Self { index: 0 } 69 } 70 } 71 72 impl Iterator for IntIterator { 73 type Item = Int; next(&mut self) -> Option<Self::Item>74 fn next(&mut self) -> Option<Self::Item> { 75 let res = match self.index { 76 0 => Some(Int::I8), 77 1 => Some(Int::I16), 78 2 => Some(Int::I32), 79 3 => Some(Int::I64), 80 4 => Some(Int::I128), 81 _ => return None, 82 }; 83 self.index += 1; 84 res 85 } 86 } 87 88 #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] 89 pub(crate) enum Float { 90 F32 = 32, 91 F64 = 64, 92 } 93 94 /// Iterator through the variants of the Float enum. 95 pub(crate) struct FloatIterator { 96 index: u8, 97 } 98 99 impl FloatIterator { new() -> Self100 pub fn new() -> Self { 101 Self { index: 0 } 102 } 103 } 104 105 /// This provides an iterator through all of the supported float variants. 106 impl Iterator for FloatIterator { 107 type Item = Float; next(&mut self) -> Option<Self::Item>108 fn next(&mut self) -> Option<Self::Item> { 109 let res = match self.index { 110 0 => Some(Float::F32), 111 1 => Some(Float::F64), 112 _ => return None, 113 }; 114 self.index += 1; 115 res 116 } 117 } 118 119 /// A type representing CPU flags. 120 /// 121 /// Flags can't be stored in memory. 122 #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] 123 pub(crate) enum Flag { 124 /// CPU flags from an integer comparison. 125 IFlags, 126 /// CPU flags from a floating point comparison. 127 FFlags, 128 } 129 130 /// Iterator through the variants of the Flag enum. 131 pub(crate) struct FlagIterator { 132 index: u8, 133 } 134 135 impl FlagIterator { new() -> Self136 pub fn new() -> Self { 137 Self { index: 0 } 138 } 139 } 140 141 impl Iterator for FlagIterator { 142 type Item = Flag; next(&mut self) -> Option<Self::Item>143 fn next(&mut self) -> Option<Self::Item> { 144 let res = match self.index { 145 0 => Some(Flag::IFlags), 146 1 => Some(Flag::FFlags), 147 _ => return None, 148 }; 149 self.index += 1; 150 res 151 } 152 } 153 154 #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] 155 pub(crate) enum Reference { 156 /// 32-bit reference. 157 R32 = 32, 158 /// 64-bit reference. 159 R64 = 64, 160 } 161 162 /// This provides an iterator through all of the supported reference variants. 163 pub(crate) struct ReferenceIterator { 164 index: u8, 165 } 166 167 impl ReferenceIterator { new() -> Self168 pub fn new() -> Self { 169 Self { index: 0 } 170 } 171 } 172 173 impl Iterator for ReferenceIterator { 174 type Item = Reference; next(&mut self) -> Option<Self::Item>175 fn next(&mut self) -> Option<Self::Item> { 176 let res = match self.index { 177 0 => Some(Reference::R32), 178 1 => Some(Reference::R64), 179 _ => return None, 180 }; 181 self.index += 1; 182 res 183 } 184 } 185 186 #[cfg(test)] 187 mod iter_tests { 188 use super::*; 189 190 #[test] bool_iter_works()191 fn bool_iter_works() { 192 let mut bool_iter = BoolIterator::new(); 193 assert_eq!(bool_iter.next(), Some(Bool::B1)); 194 assert_eq!(bool_iter.next(), Some(Bool::B8)); 195 assert_eq!(bool_iter.next(), Some(Bool::B16)); 196 assert_eq!(bool_iter.next(), Some(Bool::B32)); 197 assert_eq!(bool_iter.next(), Some(Bool::B64)); 198 assert_eq!(bool_iter.next(), Some(Bool::B128)); 199 assert_eq!(bool_iter.next(), None); 200 } 201 202 #[test] int_iter_works()203 fn int_iter_works() { 204 let mut int_iter = IntIterator::new(); 205 assert_eq!(int_iter.next(), Some(Int::I8)); 206 assert_eq!(int_iter.next(), Some(Int::I16)); 207 assert_eq!(int_iter.next(), Some(Int::I32)); 208 assert_eq!(int_iter.next(), Some(Int::I64)); 209 assert_eq!(int_iter.next(), Some(Int::I128)); 210 assert_eq!(int_iter.next(), None); 211 } 212 213 #[test] float_iter_works()214 fn float_iter_works() { 215 let mut float_iter = FloatIterator::new(); 216 assert_eq!(float_iter.next(), Some(Float::F32)); 217 assert_eq!(float_iter.next(), Some(Float::F64)); 218 assert_eq!(float_iter.next(), None); 219 } 220 221 #[test] flag_iter_works()222 fn flag_iter_works() { 223 let mut flag_iter = FlagIterator::new(); 224 assert_eq!(flag_iter.next(), Some(Flag::IFlags)); 225 assert_eq!(flag_iter.next(), Some(Flag::FFlags)); 226 assert_eq!(flag_iter.next(), None); 227 } 228 229 #[test] reference_iter_works()230 fn reference_iter_works() { 231 let mut reference_iter = ReferenceIterator::new(); 232 assert_eq!(reference_iter.next(), Some(Reference::R32)); 233 assert_eq!(reference_iter.next(), Some(Reference::R64)); 234 assert_eq!(reference_iter.next(), None); 235 } 236 } 237