Move entity into a separate crate (#297)

This commit is contained in:
Amanieu d'Antras
2018-04-10 19:06:49 +01:00
committed by Dan Gohman
parent 18b2f12150
commit 9e17e62d68
13 changed files with 94 additions and 37 deletions

View File

@@ -14,6 +14,7 @@ build = "build.rs"
name = "cretonne" name = "cretonne"
[dependencies] [dependencies]
cretonne-entity = { path = "../entity" }
# It is a goal of the cretonne crate to have minimal external dependencies. # It is a goal of the cretonne crate to have minimal external dependencies.
# Please don't add any unless they are essential to the task of creating binary # Please don't add any unless they are essential to the task of creating binary
# machine code. Integration tests that need external dependencies can be # machine code. Integration tests that need external dependencies can be

View File

@@ -39,9 +39,10 @@ pub use write::write_function;
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");
#[macro_use] #[macro_use]
pub mod dbg; pub extern crate cton_entity as entity;
#[macro_use] #[macro_use]
pub mod entity; pub mod dbg;
pub mod bforest; pub mod bforest;
pub mod binemit; pub mod binemit;
@@ -52,13 +53,14 @@ pub mod flowgraph;
pub mod ir; pub mod ir;
pub mod isa; pub mod isa;
pub mod loop_analysis; pub mod loop_analysis;
pub mod packed_option;
pub mod print_errors; pub mod print_errors;
pub mod result; pub mod result;
pub mod settings; pub mod settings;
pub mod timing; pub mod timing;
pub mod verifier; pub mod verifier;
pub use entity::packed_option;
mod abi; mod abi;
mod bitset; mod bitset;
mod constant_hash; mod constant_hash;

21
lib/entity/Cargo.toml Normal file
View File

@@ -0,0 +1,21 @@
[package]
authors = ["The Cretonne Project Developers"]
name = "cretonne-entity"
version = "0.4.2"
description = "Data structures using entity references as mapping keys"
license = "Apache-2.0"
documentation = "https://cretonne.readthedocs.io/"
repository = "https://github.com/Cretonne/cretonne"
readme = "README.md"
keywords = ["entity", "set", "map"]
[lib]
name = "cton_entity"
[features]
default = ["std"]
std = []
[badges]
maintenance = { status = "experimental" }
travis-ci = { repository = "Cretonne/cretonne" }

2
lib/entity/README.md Normal file
View File

@@ -0,0 +1,2 @@
This crate contains array-based data structures used by the core Cretonne code
generator which use densely numbered entity references as mapping keys.

View File

@@ -1,6 +1,6 @@
//! A double-ended iterator over entity references and entities. //! A double-ended iterator over entity references and entities.
use entity::EntityRef; use EntityRef;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::slice; use std::slice;

View File

@@ -1,6 +1,6 @@
//! A double-ended iterator over entity references. //! A double-ended iterator over entity references.
use entity::EntityRef; use EntityRef;
use std::marker::PhantomData; use std::marker::PhantomData;
/// Iterate over all keys in order. /// Iterate over all keys in order.

View File

@@ -1,6 +1,6 @@
//! Array-based data structures using densely numbered entity references as mapping keys. //! Array-based data structures using densely numbered entity references as mapping keys.
//! //!
//! This module defines a number of data structures based on arrays. The arrays are not indexed by //! This crate defines a number of data structures based on arrays. The arrays are not indexed by
//! `usize` as usual, but by *entity references* which are integers wrapped in new-types. This has //! `usize` as usual, but by *entity references* which are integers wrapped in new-types. This has
//! a couple advantages: //! a couple advantages:
//! //!
@@ -29,21 +29,26 @@
//! references allocated from an associated memory pool. It has a much smaller footprint than //! references allocated from an associated memory pool. It has a much smaller footprint than
//! `Vec`. //! `Vec`.
mod iter; #![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)]
mod keys; #![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
mod list; #![cfg_attr(feature = "cargo-clippy",
mod map; allow(new_without_default, new_without_default_derive, redundant_field_names))]
mod primary;
mod set;
mod sparse;
pub use self::iter::{Iter, IterMut}; // Turns on no_std and alloc features if std is not available.
pub use self::keys::Keys; #![cfg_attr(not(feature = "std"), no_std)]
pub use self::list::{EntityList, ListPool}; #![cfg_attr(not(feature = "std"), feature(alloc))]
pub use self::map::EntityMap;
pub use self::primary::PrimaryMap; /// This replaces `std` in builds with `core`.
pub use self::set::EntitySet; #[cfg(not(feature = "std"))]
pub use self::sparse::{SparseMap, SparseMapValue, SparseSet}; mod std {
extern crate alloc;
pub use self::alloc::{boxed, string, vec};
pub use core::*;
}
// Re-export core so that the macros works with both std and no_std crates
#[doc(hidden)]
pub extern crate core as __core;
/// A type wrapping a small integer index should implement `EntityRef` so it can be used as the key /// A type wrapping a small integer index should implement `EntityRef` so it can be used as the key
/// of an `EntityMap` or `SparseMap`. /// of an `EntityMap` or `SparseMap`.
@@ -61,9 +66,9 @@ pub trait EntityRef: Copy + Eq {
macro_rules! entity_impl { macro_rules! entity_impl {
// Basic traits. // Basic traits.
($entity:ident) => { ($entity:ident) => {
impl $crate::entity::EntityRef for $entity { impl $crate::EntityRef for $entity {
fn new(index: usize) -> Self { fn new(index: usize) -> Self {
debug_assert!(index < (::std::u32::MAX as usize)); debug_assert!(index < ($crate::__core::u32::MAX as usize));
$entity(index as u32) $entity(index as u32)
} }
@@ -74,7 +79,7 @@ macro_rules! entity_impl {
impl $crate::packed_option::ReservedValue for $entity { impl $crate::packed_option::ReservedValue for $entity {
fn reserved_value() -> $entity { fn reserved_value() -> $entity {
$entity(::std::u32::MAX) $entity($crate::__core::u32::MAX)
} }
} }
}; };
@@ -84,16 +89,34 @@ macro_rules! entity_impl {
($entity:ident, $display_prefix:expr) => { ($entity:ident, $display_prefix:expr) => {
entity_impl!($entity); entity_impl!($entity);
impl ::std::fmt::Display for $entity { impl $crate::__core::fmt::Display for $entity {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut $crate::__core::fmt::Formatter) -> $crate::__core::fmt::Result {
write!(f, "{}{}", $display_prefix, self.0) write!(f, "{}{}", $display_prefix, self.0)
} }
} }
impl ::std::fmt::Debug for $entity { impl $crate::__core::fmt::Debug for $entity {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut $crate::__core::fmt::Formatter) -> $crate::__core::fmt::Result {
(self as &::std::fmt::Display).fmt(f) (self as &$crate::__core::fmt::Display).fmt(f)
} }
} }
}; };
} }
pub mod packed_option;
mod iter;
mod keys;
mod list;
mod map;
mod primary;
mod set;
mod sparse;
pub use self::iter::{Iter, IterMut};
pub use self::keys::Keys;
pub use self::list::{EntityList, ListPool};
pub use self::map::EntityMap;
pub use self::primary::PrimaryMap;
pub use self::set::EntitySet;
pub use self::sparse::{SparseMap, SparseMapValue, SparseSet};

View File

@@ -1,5 +1,5 @@
//! Small lists of entity references. //! Small lists of entity references.
use entity::EntityRef; use EntityRef;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::mem; use std::mem;
@@ -481,8 +481,12 @@ impl<T: EntityRef> EntityList<T> {
mod tests { mod tests {
use super::*; use super::*;
use super::{sclass_for_length, sclass_size}; use super::{sclass_for_length, sclass_size};
use entity::EntityRef; use EntityRef;
use ir::Inst;
/// An opaque reference to an instruction in a function.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Inst(u32);
entity_impl!(Inst, "inst");
#[test] #[test]
fn size_classes() { fn size_classes() {

View File

@@ -1,6 +1,6 @@
//! Densely numbered entity references as mapping keys. //! Densely numbered entity references as mapping keys.
use entity::{EntityRef, Iter, IterMut, Keys}; use {EntityRef, Iter, IterMut, Keys};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::slice; use std::slice;

View File

@@ -1,5 +1,5 @@
//! Densely numbered entity references as mapping keys. //! Densely numbered entity references as mapping keys.
use entity::{EntityRef, Iter, IterMut, Keys}; use {EntityRef, Iter, IterMut, Keys};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::slice; use std::slice;

View File

@@ -1,6 +1,6 @@
//! Densely numbered entity references as set keys. //! Densely numbered entity references as set keys.
use entity::{EntityRef, Keys}; use {EntityRef, Keys};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::vec::Vec; use std::vec::Vec;

View File

@@ -7,7 +7,7 @@
//! > Briggs, Torczon, *An efficient representation for sparse sets*, //! > Briggs, Torczon, *An efficient representation for sparse sets*,
//! ACM Letters on Programming Languages and Systems, Volume 2, Issue 1-4, March-Dec. 1993. //! ACM Letters on Programming Languages and Systems, Volume 2, Issue 1-4, March-Dec. 1993.
use entity::{EntityMap, EntityRef}; use {EntityMap, EntityRef};
use std::mem; use std::mem;
use std::slice; use std::slice;
use std::u32; use std::u32;
@@ -229,8 +229,12 @@ pub type SparseSet<T> = SparseMap<T, T>;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use entity::EntityRef; use EntityRef;
use ir::Inst;
/// An opaque reference to an instruction in a function.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Inst(u32);
entity_impl!(Inst, "inst");
// Mock key-value object for testing. // Mock key-value object for testing.
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]