Commit 27df3a88 authored by qinsoon's avatar qinsoon

[wip] util mod

parent cb8438b7
......@@ -24,4 +24,5 @@ crate-type = ["rlib"]
memmap = "0.4.0"
memsec = "0.1.9"
byteorder = "0.5.3"
rustc-serialize = "*"
\ No newline at end of file
rustc-serialize = "*"
doubly = "1.1.3"
\ No newline at end of file
......@@ -12,12 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#![allow(dead_code)]
use std::cmp;
use std::fmt;
use std::mem;
/// Address represents an arbitrary address. This is designed to represent
/// address and do address arithmetic mostly in a safe way, and to allow
/// mark some operations as unsafe. This type needs to be zero overhead
/// (memory wise and time wise).
/// The idea is from the paper High-level Low-level Programming (VEE09),
/// and JikesRVM.
#[repr(C)]
#[derive(Copy, Clone, Eq, Hash)]
pub struct Address(usize);
......
This diff is collapsed.
// Copyright 2017 The Australian National University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use ast::ir::*;
use ast::types::*;
use ast::ptr::*;
pub fn make_i32_const(id: MuID, val: i32) -> P<Value> {
unimplemented!()
}
......@@ -12,30 +12,82 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! Utility crate that serves Zebu includes:
//! * data structures
//! * double linked list
//! * linked hashmap/set
//! * extra functions for existing types
//! * string
//! * vector
//! * Address/ObjectReference type
//! * utility functions for
//! * memory
//! * mathematics
//! * bit operations
extern crate byteorder;
extern crate rustc_serialize;
extern crate doubly;
// these type aliases make source code easier to read
/// size in bits
pub type BitSize = usize;
pub type ByteOffset = isize;
/// size in bytes
pub type ByteSize = usize;
/// offset in byte
pub type ByteOffset = isize;
/// word value
pub type Word = usize;
/// Zebu make an assumption that it will only support 64 bits architecture
/// However, ideally we should always use pointer size, or pointer-size type defined here.
/// But we may have hard coded u64 or 64 somewhere.
// TODO: fix the hard code
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub const LOG_POINTER_SIZE : usize = 3;
/// pointer size in byte
pub const POINTER_SIZE : ByteSize = 1 << LOG_POINTER_SIZE;
/// word size in byte
pub const WORD_SIZE : ByteSize = 1 << LOG_POINTER_SIZE;
pub mod mem;
/// linked hashmap implementation copied from container-rs with modification
mod linked_hashmap;
/// linked hashset implementation based on LinkedHashMap
mod linked_hashset;
mod doubly;
// re-export these data structures
pub use linked_hashmap::LinkedHashMap;
pub use linked_hashset::LinkedHashSet;
pub use doubly::DoublyLinkedList;
pub use self::doubly::DoublyLinkedList;
/// mem module:
/// * conversions of bit representations
/// * re-export memmap and memsec crate
pub mod mem;
/// mathematics utilities
pub mod math;
mod address;
/// Address represents an arbitrary memory address (valid or not)
pub use address::Address;
/// ObjectReference is a reference to an object (the address is guaranteed to be valid with an object)
pub use address::ObjectReference;
// These modules provide operations on Vector, and String.
// They are not found in the standard library.
// (maybe because they are likely inefficient?)
/// vector utilities
pub mod vec_utils;
/// string utilities
pub mod string_utils;
/// bit operations
pub mod bit_utils;
/// the macro to create LinkedHashMap
#[macro_export]
macro_rules! linked_hashmap {
(@single $($x:tt)*) => (());
......@@ -54,6 +106,7 @@ macro_rules! linked_hashmap {
};
}
/// the macro to create LinkedHashSet
#[macro_export]
macro_rules! linked_hashset {
(@single $($x:tt)*) => (());
......@@ -72,6 +125,7 @@ macro_rules! linked_hashset {
};
}
/// print trace!() log if condition is true (the condition should be a constant boolean)
#[macro_export]
macro_rules! trace_if {
($cond: expr, $($arg:tt)*) => {
......@@ -81,6 +135,7 @@ macro_rules! trace_if {
}
}
/// print info!() log if condition is true (the condition should be a constant boolean)
#[macro_export]
macro_rules! info_if {
($cond: expr, $($arg:tt)*) => {
......@@ -90,6 +145,7 @@ macro_rules! info_if {
}
}
/// print debug!() log if condition is true (the condition should be a constant boolean)
#[macro_export]
macro_rules! debug_if {
($cond: expr, $($arg:tt)*) => {
......@@ -99,6 +155,7 @@ macro_rules! debug_if {
}
}
/// print warn!() log if condition is true (the condition should be a constant boolean)
#[macro_export]
macro_rules! warn_if {
($cond: expr, $($arg:tt)*) => {
......@@ -108,6 +165,7 @@ macro_rules! warn_if {
}
}
/// print error!() log if condition is true (the condition should be a constant boolean)
#[macro_export]
macro_rules! error_if {
($cond: expr, $($arg:tt)*) => {
......@@ -116,17 +174,3 @@ macro_rules! error_if {
}
}
}
pub mod math;
mod address;
pub use address::Address;
pub use address::ObjectReference;
// This porvides some missing operations on Vec.
// They are not included in the standard libarary.
// (because they are likely inefficient?)
pub mod vec_utils;
pub mod bit_utils;
pub mod string_utils;
// Copyright 2017 The Australian National University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// this implementation is copied from crate 'linked_hash_map'
// copied it here, and added some necessary implementation
// 1. RustcEncodable/Decodable
// 2. values_mut()
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
......@@ -27,6 +8,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// The implementation is copied from crate 'container-rs/linked-hash-map'
// We added these functions:
// 1. RustcEncodable/Decodable (deprecated)
// 2. values_mut()
//! A `HashMap` wrapper that holds key-value pairs in insertion order.
//!
//! # Examples
......
......@@ -19,6 +19,7 @@ use std::borrow::Borrow;
use linked_hashmap::LinkedHashMap;
use linked_hashmap::Keys;
/// A LinkedHashSet based on LinkedHashMap implementation
pub struct LinkedHashSet<K, S = RandomState>(LinkedHashMap<K, (), S>);
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
......@@ -48,12 +49,7 @@ impl<K: Hash + Eq> LinkedHashSet<K> {
LinkedHashSet(LinkedHashMap::new())
}
pub fn new1(val: K) -> Self {
let mut ret = LinkedHashSet::new();
ret.insert(val);
ret
}
/// consumes a vector to a LinkedHashSet (removes duplicated elements)
pub fn from_vec(from: Vec<K>) -> Self {
let mut ret = LinkedHashSet::new();
......@@ -64,6 +60,7 @@ impl<K: Hash + Eq> LinkedHashSet<K> {
ret
}
/// consumes the LinkedHashSet to a vector
pub fn to_vec(mut self) -> Vec<K> {
let mut ret = vec![];
......@@ -74,16 +71,19 @@ impl<K: Hash + Eq> LinkedHashSet<K> {
ret
}
/// clears the set
pub fn clear(&mut self) {
self.0.clear();
}
}
impl<K: Hash + Eq, S: BuildHasher> LinkedHashSet<K, S> {
/// returns size
pub fn len(&self) -> usize {
self.0.len()
}
/// returns the first element from the set
pub fn pop_front(&mut self) -> Option<K> {
match self.0.pop_front() {
Some((k, _)) => Some(k),
......@@ -91,6 +91,7 @@ impl<K: Hash + Eq, S: BuildHasher> LinkedHashSet<K, S> {
}
}
/// returns the last element from the set
pub fn pop_back(&mut self) -> Option<K> {
match self.0.pop_back() {
Some((k, _)) => Some(k),
......@@ -98,10 +99,12 @@ impl<K: Hash + Eq, S: BuildHasher> LinkedHashSet<K, S> {
}
}
/// inserts an element at the back
pub fn insert(&mut self, k: K) -> Option<()> {
self.0.insert(k, ())
}
/// returns true if the set contains the element, otherwise returns false
pub fn contains<Q: ?Sized>(&self, k: &Q) -> bool
where K: Borrow<Q>,
Q: Eq + Hash
......@@ -109,21 +112,25 @@ impl<K: Hash + Eq, S: BuildHasher> LinkedHashSet<K, S> {
self.0.contains_key(k)
}
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<()>
/// removes an element from the set, do nothing if the set does not contain the element
pub fn remove<Q: ?Sized>(&mut self, k: &Q)
where K: Borrow<Q>,
Q: Eq + Hash
{
self.0.remove(k)
self.0.remove(k);
}
/// gets an Keys iterator for the set
pub fn iter(&self) -> Keys<K, ()> {
self.0.keys()
}
/// returns true if the set is empty
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// pops all elements from the other LinkedHashSet, and adds to this LinkedHashSet
pub fn add_all(&mut self, mut other: Self) {
while !other.is_empty() {
let entry = other.pop_front().unwrap();
......@@ -131,12 +138,14 @@ impl<K: Hash + Eq, S: BuildHasher> LinkedHashSet<K, S> {
}
}
/// pops all elements from a vector, and adds to this LinkedHashSet
pub fn add_from_vec(&mut self, mut vec: Vec<K>) {
while !vec.is_empty() {
self.insert(vec.pop().unwrap());
}
}
/// returns true if two LinkedHashSets have same elements (ignore order)
pub fn equals(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment