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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
//! # The `flp_framework::address` Module //! //! This module contains some of the basic primitive address //! types supported by Floorplan including void addresses, //! words, bytes, and so on. The implementations in this file //! look much like what gets generated for Floorplan-defined //! types, but are manually coded here to provide stronger //! control over how they get used. //! //! For information on acquiring the Floorplan compiler itself, //! go see the [GitHub project here][github-project]. //! //! [github-project]: https://github.com/RedlineResearch/floorplan #![allow(non_snake_case)] #![allow(dead_code)] use std::cmp; use std::fmt; use std::mem::size_of as size_of; use super::*; /// A `VoidAddr`, much like a `void*` in C, is available for those /// in need of a quick-and-dirty way of representing the value of a /// pointer without actually being able to read from or write to that /// pointer. #[repr(C)] #[derive(Copy, Clone, Eq, Hash)] pub struct VoidAddr(usize); deriveAddrReqs!(VoidAddr); impl Address for VoidAddr { /// Construct a void address: /// /// ```rust /// # use flp_framework::*; /// let va = VoidAddr::from_usize(0xdeadbeef); /// ``` fn from_usize(val : usize) -> VoidAddr { VoidAddr(val) } /// Deconstruct a void address: /// /// ```rust /// # use flp_framework::*; /// let va = VoidAddr::from_usize(0xdeadbeef); /// assert!(va.as_usize() == 3735928559); /// ``` fn as_usize(&self) -> usize { self.0 } /// Trivially verify that this void address looks valid. /// All values are valid: /// /// ```rust /// # use flp_framework::*; /// let va = VoidAddr::from_usize(0xdeadbeef); /// assert!(va.verify()); /// ``` fn verify(self) -> bool { true } /// Void addresses cannot be accessed. #[inline(always)] fn load<T: Copy> (&self) -> T { panic!("Can't load() a VoidAddr!") } /// Void addresses cannot be accessed. #[inline(always)] fn store<T> (&self, _value: T) { panic!("Can't store() a VoidAddr!") } /// Void addresses cannot be accessed. #[inline(always)] fn memset(&self, _char: u8, _length: usize) { panic!("Can't memset() a VoidAddr!") } } impl VoidAddr { /// A void address can be constructed from any other address type. pub fn from_addr<A: Address>(ptr : A) -> Self { VoidAddr(ptr.as_usize()) } } /// A generic address that points to a memory location containing an address. #[repr(C)] #[derive(Copy, Clone, Eq, Hash)] pub struct AddrAddr(usize); deriveAddr!(AddrAddr, 1 << size_of::<usize>()); /// Determine whether or not the nth-lowest bit of a byte is set. #[inline(always)] pub fn test_nth_bit(value: u8, index: usize) -> bool { value & (1 << index) != 0 } /// Mask for just the nth-lowest bits of a byte. #[inline(always)] pub fn lower_bits(value: u8, len: usize) -> u8 { value & ((1 << len) - 1) } /// A `Word` is a value representing data, useful for its ability to /// be addressed and subsequently accessed by a `WordAddr`. Suppose /// you have some word address in `wa` and you want to ensure that the lowest /// bit of that word is set: /// /// ```rust /// # #![feature(rustc_private)] /// # use std::alloc::{alloc, Layout}; /// # use flp_framework::*; /// # let wa_alloc = unsafe { alloc(Layout::new::<usize>()) }; /// # let wa = WordAddr::from_ptr(wa_alloc); /// # wa.store(WordAddr::from_usize(0b1)); /// let w : Word = wa.load(); /// assert!(w.is_aligned_to(2) == false); /// ``` #[repr(C)] #[derive(Copy, Clone, Eq, Hash)] pub struct Word(usize); deriveAddrReqs!(Word); /// Computes the number of consecutive lower-order bits that are /// set to zero in a `usize` value. fn trailing_zeros(x : usize) -> usize { if x % 2 != 0 { 0 } else { trailing_zeros(x / 2) } } /// The number of bytes in a word of memory. pub const BYTES_IN_WORD : usize = size_of::<usize>(); /// Log base 2 of the number of bytes in a word of memory. #[cfg(target_pointer_width = "32")] pub const LOG_BYTES_IN_WORD : usize = 2; /// Log base 2 of the number of bytes in a word of memory. #[cfg(target_pointer_width = "64")] pub const LOG_BYTES_IN_WORD : usize = 3; /// Only 32-bit and 64-bit architectures are currently supported by Floorplan. #[cfg(all(not(target_pointer_width = "64"), not(target_pointer_width = "32")))] pub const LOG_BYTES_IN_WORD : usize = panic!("Unsupported word size."); /// A `WordAddr` is a value representing the address of a word of memory. #[repr(C)] #[derive(Copy, Clone, Eq, Hash)] pub struct WordAddr(usize); impl Address for Word { /// Constructor for a word value from a raw `usize`. /// /// ```rust /// # use flp_framework::*; /// let w = Word::from_usize(0xff); /// ``` fn from_usize(val : usize) -> Word { Word(val) } /// Deconstructor for a word value into a raw `usize` value. /// /// ```rust /// # use flp_framework::*; /// let w = Word::from_usize(0xff); /// assert!(w.as_usize() == 255); /// ``` fn as_usize(&self) -> usize { self.0 } /// All possible word values are valid: /// /// ```rust /// # use flp_framework::*; /// let w = Word::from_usize(0xff); /// assert!(w.verify()); /// ``` fn verify(self) -> bool { true } /// A word cannot be accessed. #[inline(always)] fn load<T: Copy> (&self) -> T { panic!("Can't load() a Word!") } /// A word cannot be accessed. #[inline(always)] fn store<T> (&self, _value: T) { panic!("Can't store() a Word!") } /// Raw word values are not writeable. fn memset(&self, _char: u8, _length: usize) { panic!("Can't memset() a Word!") } } deriveAddrReqs!(WordAddr); impl Address for WordAddr { /// Constructor for a word address from a raw `usize`. /// /// ```rust /// # use flp_framework::*; /// let wa = WordAddr::from_usize(0xdeadbeef); /// ``` fn from_usize(val : usize) -> WordAddr { WordAddr(val) } /// Deconstructor for a word address into a raw `usize` value. /// /// ```rust /// # use flp_framework::*; /// let wa = WordAddr::from_usize(0xdeadbeef); /// assert!(wa.as_usize() == 3735928559); /// ``` fn as_usize(&self) -> usize { self.0 } /// All possible word values are valid: /// /// ```rust /// # use flp_framework::*; /// let wa = WordAddr::from_usize(0xdeadbeef); /// assert!(wa.verify()); /// ``` fn verify(self) -> bool { true } /// A generic address to a raw word cannot be accessed. #[inline(always)] fn load<T: Copy> (&self) -> T { unsafe { *(self.as_usize() as *mut T) }} /// A generic address to a raw word cannot be accessed. #[inline(always)] fn store<T> (&self, value: T) { unsafe { *(self.as_usize() as *mut T) = value; }} } /// A `Byte` is a value representing data, useful for its ability to /// be addressed and subsequently accessed by a `ByteAddr`. Suppose /// you have some byte address in `ba` and you want to ensure that the lowest /// bit of that word is not set: /// /// ```rust /// # #![feature(rustc_private)] /// # use std::alloc::{alloc, Layout}; /// # use flp_framework::*; /// # let ba_alloc = unsafe { alloc(Layout::new::<usize>()) }; /// # let ba = ByteAddr::from_ptr(ba_alloc); /// # ba.store(ByteAddr::from_usize(0b0)); /// let b : Byte = ba.load(); /// assert!(b.is_aligned_to(2) == true); /// ``` #[repr(C)] #[derive(Copy, Clone, Eq, Hash)] pub struct Byte(u8); deriveAddrReqs!(Byte); // TODO: both this impl and the one for `Word` need to be // changed to represent a `Value` or `Prim` trait to show // that the associated methods represent operations over primitive // values and not operations over an address type. impl Address for Byte { /// Constructor for a byte value from a raw `usize`. /// /// ```rust /// # use flp_framework::*; /// let w = Byte::from_usize(0xff); /// ``` fn from_usize(val : usize) -> Byte { Byte(val as u8) } /// Deconstructor for a byte value into a raw `usize` value. /// /// ```rust /// # use flp_framework::*; /// let w = Byte::from_usize(0xff); /// assert!(w.as_usize() == 255); /// ``` fn as_usize(&self) -> usize { self.0 as usize } /// All possible byte values are valid: /// /// ```rust /// # use flp_framework::*; /// let w = Byte::from_usize(0xff); /// assert!(w.verify()); /// ``` fn verify(self) -> bool { true } /// A byte cannot be accessed. #[inline(always)] fn load<T: Copy> (&self) -> T { panic!("Can't load() a Byte!") } /// A byte cannot be accessed. #[inline(always)] fn store<T> (&self, _value: T) { panic!("Can't store() a Byte!") } /// Raw byte values are not writeable. fn memset(&self, _char: u8, _length: usize) { panic!("Can't memset() a Byte!") } } /// A `ByteAddr` is a value representing the address of zero or more bytes of memory. #[repr(C)] #[derive(Copy, Clone, Eq, Hash)] pub struct ByteAddr(usize); deriveAddrReqs!(ByteAddr); impl Address for ByteAddr { /// Constructor for a byte address from a raw `usize`. /// /// ```rust /// # use flp_framework::*; /// let ba = ByteAddr::from_usize(0xdeadbeef); /// ``` fn from_usize(val : usize) -> ByteAddr { ByteAddr(val) } /// Deconstructor for a byte address into a raw `usize` value. /// /// ```rust /// # use flp_framework::*; /// let ba = ByteAddr::from_usize(0xdeadbeef); /// assert!(ba.as_usize() == 3735928559); /// ``` fn as_usize(&self) -> usize { self.0 } /// All byte addresses are valid: /// /// ```rust /// # use flp_framework::*; /// let ba = ByteAddr::from_usize(0xdeadbeef); /// assert!(ba.verify()); /// ``` fn verify(self) -> bool { true } /// A generic address to a raw byte cannot be accessed. #[inline(always)] fn load<T: Copy> (&self) -> T { unsafe { *(self.as_usize() as *mut T) }} /// A generic address to a raw byte cannot be accessed. #[inline(always)] fn store<T> (&self, value: T) { unsafe { *(self.as_usize() as *mut T) = value; }} } #[cfg(test)] mod tests { use super::*; pub const VAL : u8 = 0b000011_11; #[test] pub fn v0() { assert_eq!(test_nth_bit(VAL, 0), true); } #[test] pub fn v1() { assert_eq!(test_nth_bit(VAL, 1), true); } #[test] pub fn v2() { assert_eq!(test_nth_bit(VAL, 2), true); } #[test] pub fn v3() { assert_eq!(test_nth_bit(VAL, 3), true); } #[test] pub fn v4() { assert_eq!(test_nth_bit(VAL, 4), false); } #[test] pub fn v5() { assert_eq!(test_nth_bit(VAL, 5), false); } #[test] pub fn v6() { assert_eq!(test_nth_bit(VAL, 6), false); } #[test] pub fn v7() { assert_eq!(test_nth_bit(VAL, 7), false); } }