diff --git a/cranelift/README.md b/cranelift/README.md index 5f579b0eb6..f8bd1e8c15 100644 --- a/cranelift/README.md +++ b/cranelift/README.md @@ -94,7 +94,7 @@ The following crates support \`no\_std\`, although they do depend on liballoc: - cranelift-native - cranelift-wasm - cranelift-module - - cranelift-simplejit + - cranelift-preopt - cranelift To use no\_std mode, disable the std feature and enable the core diff --git a/cranelift/fuzz/Cargo.toml b/cranelift/fuzz/Cargo.toml index a78d0ed3fc..0285feaa51 100644 --- a/cranelift/fuzz/Cargo.toml +++ b/cranelift/fuzz/Cargo.toml @@ -3,6 +3,7 @@ name = "clif-wasm-fuzz" version = "0.0.1" authors = ["foote@fastly.com"] publish = false +edition = "2018" [package.metadata] cargo-fuzz = true diff --git a/cranelift/test-no_std.sh b/cranelift/test-no_std.sh index 32c354c065..dab7e3afbd 100755 --- a/cranelift/test-no_std.sh +++ b/cranelift/test-no_std.sh @@ -13,7 +13,7 @@ function banner { } # Test those packages which have no_std support. -LIBS="codegen frontend wasm native preopt module simplejit umbrella" +LIBS="codegen frontend wasm native preopt module entity bforest umbrella" for LIB in $LIBS; do banner "Rust unit tests in $LIB" pushd "lib/$LIB" >/dev/null diff --git a/lib/bforest/Cargo.toml b/lib/bforest/Cargo.toml index 0181e32a83..36ec8a752c 100644 --- a/lib/bforest/Cargo.toml +++ b/lib/bforest/Cargo.toml @@ -17,6 +17,7 @@ cranelift-entity = { path = "../entity", version = "0.26.0", default-features = [features] default = ["std"] std = ["cranelift-entity/std"] +core = [] [badges] maintenance = { status = "experimental" } diff --git a/lib/bforest/src/lib.rs b/lib/bforest/src/lib.rs index e78292600e..7b6beb2599 100644 --- a/lib/bforest/src/lib.rs +++ b/lib/bforest/src/lib.rs @@ -31,24 +31,24 @@ clippy::use_self ) )] -// Turns on no_std and alloc features if std is not available. -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![cfg_attr(not(feature = "std"), feature(alloc))] -/// This replaces `std` in builds with `core`. +#[cfg(test)] #[cfg(not(feature = "std"))] -mod std { - extern crate alloc; - pub use self::alloc::{boxed, string, vec}; - pub use core::*; -} +#[macro_use] +extern crate alloc as std; +#[cfg(test)] +#[cfg(feature = "std")] +#[macro_use] +extern crate std; #[macro_use] extern crate cranelift_entity as entity; use crate::entity::packed_option; -use std::borrow::BorrowMut; -use std::cmp::Ordering; +use core::borrow::BorrowMut; +use core::cmp::Ordering; mod map; mod node; diff --git a/lib/bforest/src/map.rs b/lib/bforest/src/map.rs index 313b870f49..3cf8421a30 100644 --- a/lib/bforest/src/map.rs +++ b/lib/bforest/src/map.rs @@ -3,8 +3,8 @@ use super::{Comparator, Forest, Node, NodeData, NodePool, Path, INNER_SIZE}; use crate::packed_option::PackedOption; #[cfg(test)] -use std::fmt; -use std::marker::PhantomData; +use core::fmt; +use core::marker::PhantomData; #[cfg(test)] use std::string::String; @@ -429,7 +429,7 @@ where mod tests { use super::super::NodeData; use super::*; - use std::mem; + use core::mem; use std::vec::Vec; #[test] diff --git a/lib/bforest/src/node.rs b/lib/bforest/src/node.rs index 583fe0f78e..f0dbc4d7f5 100644 --- a/lib/bforest/src/node.rs +++ b/lib/bforest/src/node.rs @@ -1,8 +1,8 @@ //! B+-tree nodes. use super::{slice_insert, slice_shift, Forest, Node, SetValue, INNER_SIZE}; -use std::borrow::{Borrow, BorrowMut}; -use std::fmt; +use core::borrow::{Borrow, BorrowMut}; +use core::fmt; /// B+-tree node. /// @@ -584,7 +584,7 @@ where #[cfg(test)] mod tests { use super::*; - use std::mem; + use core::mem; use std::string::ToString; // Forest impl for a set implementation. diff --git a/lib/bforest/src/path.rs b/lib/bforest/src/path.rs index 7ccb9ee6f5..1df66b7ef7 100644 --- a/lib/bforest/src/path.rs +++ b/lib/bforest/src/path.rs @@ -2,11 +2,11 @@ use super::node::Removed; use super::{slice_insert, slice_shift, Comparator, Forest, Node, NodeData, NodePool, MAX_PATH}; -use std::borrow::Borrow; -use std::marker::PhantomData; +use core::borrow::Borrow; +use core::marker::PhantomData; #[cfg(test)] -use std::fmt; +use core::fmt; pub(super) struct Path { /// Number of path entries including the root and leaf nodes. @@ -706,7 +706,7 @@ impl fmt::Display for Path { mod tests { use super::super::{Forest, NodeData, NodePool}; use super::*; - use std::cmp::Ordering; + use core::cmp::Ordering; struct TC(); diff --git a/lib/bforest/src/pool.rs b/lib/bforest/src/pool.rs index 0eb873ccab..1b51cfe56b 100644 --- a/lib/bforest/src/pool.rs +++ b/lib/bforest/src/pool.rs @@ -5,8 +5,8 @@ use super::Comparator; use super::{Forest, Node, NodeData}; use crate::entity::PrimaryMap; #[cfg(test)] -use std::fmt; -use std::ops::{Index, IndexMut}; +use core::fmt; +use core::ops::{Index, IndexMut}; /// A pool of nodes, including a free list. pub(super) struct NodePool { @@ -84,8 +84,8 @@ impl NodePool { F::Key: fmt::Display, { use crate::entity::SparseSet; - use std::borrow::Borrow; - use std::cmp::Ordering; + use core::borrow::Borrow; + use core::cmp::Ordering; use std::vec::Vec; // The root node can't be an inner node with just a single sub-tree. It should have been diff --git a/lib/bforest/src/set.rs b/lib/bforest/src/set.rs index e73171d368..72e517561f 100644 --- a/lib/bforest/src/set.rs +++ b/lib/bforest/src/set.rs @@ -3,8 +3,8 @@ use super::{Comparator, Forest, Node, NodeData, NodePool, Path, SetValue, INNER_SIZE}; use crate::packed_option::PackedOption; #[cfg(test)] -use std::fmt; -use std::marker::PhantomData; +use core::fmt; +use core::marker::PhantomData; #[cfg(test)] use std::string::String; @@ -357,7 +357,7 @@ where mod tests { use super::super::NodeData; use super::*; - use std::mem; + use core::mem; use std::vec::Vec; #[test] diff --git a/lib/codegen/meta-python/gen_instr.py b/lib/codegen/meta-python/gen_instr.py index 57be62a5d4..1c6905f1cd 100644 --- a/lib/codegen/meta-python/gen_instr.py +++ b/lib/codegen/meta-python/gen_instr.py @@ -257,8 +257,8 @@ def gen_instruction_data_impl(fmt): 'pub fn eq(&self, other: &Self, pool: &ir::ValueListPool)' ' -> bool {', '}'): - with fmt.indented('if ::std::mem::discriminant(self) != ' - '::std::mem::discriminant(other) {', '}'): + with fmt.indented('if ::core::mem::discriminant(self) != ' + '::core::mem::discriminant(other) {', '}'): fmt.line('return false;') with fmt.indented('match (self, other) {', '}'): for f in InstructionFormat.all_formats: @@ -301,7 +301,7 @@ def gen_instruction_data_impl(fmt): hash the contents of any `ValueLists`. """) with fmt.indented( - 'pub fn hash' + 'pub fn hash' '(&self, state: &mut H, pool: &ir::ValueListPool) {', '}'): with fmt.indented('match *self {', '}'): @@ -323,13 +323,13 @@ def gen_instruction_data_impl(fmt): members.append(field.member) pat = n + ' { ' + ', '.join(members) + ' }' with fmt.indented(pat + ' => {', '}'): - fmt.line('::std::hash::Hash::hash( ' - '&::std::mem::discriminant(self), state);') - fmt.line('::std::hash::Hash::hash(&opcode, state);') + fmt.line('::core::hash::Hash::hash( ' + '&::core::mem::discriminant(self), state);') + fmt.line('::core::hash::Hash::hash(&opcode, state);') for field in f.imm_fields: - fmt.line('::std::hash::Hash::hash(&{}, state);' + fmt.line('::core::hash::Hash::hash(&{}, state);' .format(field.member)) - fmt.line('::std::hash::Hash::hash({}, state);' + fmt.line('::core::hash::Hash::hash({}, state);' .format(args)) diff --git a/lib/codegen/src/abi.rs b/lib/codegen/src/abi.rs index 954c40b643..4c0b8d4dbc 100644 --- a/lib/codegen/src/abi.rs +++ b/lib/codegen/src/abi.rs @@ -4,7 +4,7 @@ //! `TargetIsa::legalize_signature()` method. use crate::ir::{AbiParam, ArgumentExtension, ArgumentLoc, Type}; -use std::cmp::Ordering; +use core::cmp::Ordering; use std::vec::Vec; /// Legalization action to perform on a single argument or return value when converting a diff --git a/lib/codegen/src/binemit/memorysink.rs b/lib/codegen/src/binemit/memorysink.rs index 3d7d673719..18aa578f09 100644 --- a/lib/codegen/src/binemit/memorysink.rs +++ b/lib/codegen/src/binemit/memorysink.rs @@ -16,7 +16,7 @@ use super::{Addend, CodeOffset, CodeSink, Reloc}; use crate::ir::{ExternalName, JumpTable, SourceLoc, TrapCode}; -use std::ptr::write_unaligned; +use core::ptr::write_unaligned; /// A `CodeSink` that writes binary machine code directly into memory. /// diff --git a/lib/codegen/src/binemit/mod.rs b/lib/codegen/src/binemit/mod.rs index c4ad46c7df..63fa7ef40b 100644 --- a/lib/codegen/src/binemit/mod.rs +++ b/lib/codegen/src/binemit/mod.rs @@ -13,7 +13,7 @@ pub use self::shrink::shrink_instructions; pub use crate::regalloc::RegDiversions; use crate::ir::{ExternalName, Function, Inst, JumpTable, SourceLoc, TrapCode}; -use std::fmt; +use core::fmt; /// Offset in bytes from the beginning of the function. /// diff --git a/lib/codegen/src/bitset.rs b/lib/codegen/src/bitset.rs index 10d624d8a2..ccbe413175 100644 --- a/lib/codegen/src/bitset.rs +++ b/lib/codegen/src/bitset.rs @@ -5,9 +5,9 @@ //! //! If you would like to add support for larger bitsets in the future, you need to change the trait //! bound Into and the u32 in the implementation of `max_bits()`. -use std::convert::{From, Into}; -use std::mem::size_of; -use std::ops::{Add, BitOr, Shl, Sub}; +use core::convert::{From, Into}; +use core::mem::size_of; +use core::ops::{Add, BitOr, Shl, Sub}; /// A small bitset built on a single primitive integer type #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/lib/codegen/src/cfg_printer.rs b/lib/codegen/src/cfg_printer.rs index c15ec134b9..6a9932febe 100644 --- a/lib/codegen/src/cfg_printer.rs +++ b/lib/codegen/src/cfg_printer.rs @@ -1,6 +1,6 @@ //! The `CFGPrinter` utility. -use std::fmt::{Display, Formatter, Result, Write}; +use core::fmt::{Display, Formatter, Result, Write}; use crate::flowgraph::{BasicBlock, ControlFlowGraph}; use crate::ir::instructions::BranchInfo; diff --git a/lib/codegen/src/dbg.rs b/lib/codegen/src/dbg.rs index f19c222caf..1d814ceedb 100644 --- a/lib/codegen/src/dbg.rs +++ b/lib/codegen/src/dbg.rs @@ -1,5 +1,5 @@ //! Debug tracing helpers. -use std::fmt; +use core::fmt; /// Prefix added to the log file names, just before the thread name or id. pub static LOG_FILENAME_PREFIX: &str = "cranelift.dbg."; diff --git a/lib/codegen/src/dominator_tree.rs b/lib/codegen/src/dominator_tree.rs index 9d20eab0d4..feea33a0ef 100644 --- a/lib/codegen/src/dominator_tree.rs +++ b/lib/codegen/src/dominator_tree.rs @@ -6,9 +6,9 @@ use crate::ir::instructions::BranchInfo; use crate::ir::{Ebb, ExpandedProgramPoint, Function, Inst, Layout, ProgramOrder, Value}; use crate::packed_option::PackedOption; use crate::timing; -use std::cmp; -use std::cmp::Ordering; -use std::mem; +use core::cmp; +use core::cmp::Ordering; +use core::mem; use std::vec::Vec; /// RPO numbers are not first assigned in a contiguous way but as multiples of STRIDE, to leave diff --git a/lib/codegen/src/flowgraph.rs b/lib/codegen/src/flowgraph.rs index 4412e53599..335ca15ce4 100644 --- a/lib/codegen/src/flowgraph.rs +++ b/lib/codegen/src/flowgraph.rs @@ -28,7 +28,7 @@ use crate::entity::SecondaryMap; use crate::ir::instructions::BranchInfo; use crate::ir::{Ebb, Function, Inst}; use crate::timing; -use std::mem; +use core::mem; /// A basic block denoted by its enclosing Ebb and last instruction. #[derive(PartialEq, Eq)] diff --git a/lib/codegen/src/fx.rs b/lib/codegen/src/fx.rs index eb3b7f2b36..36eb62df90 100644 --- a/lib/codegen/src/fx.rs +++ b/lib/codegen/src/fx.rs @@ -10,10 +10,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::collections::{HashMap, HashSet}; -use std::default::Default; -use std::hash::{BuildHasherDefault, Hash, Hasher}; -use std::ops::BitXor; +use super::{HashMap, HashSet}; +use core::default::Default; +use core::hash::{BuildHasherDefault, Hash, Hasher}; +use core::ops::BitXor; pub type FxHashMap = HashMap>; pub type FxHashSet = HashSet>; diff --git a/lib/codegen/src/ir/builder.rs b/lib/codegen/src/ir/builder.rs index 7ed9a8e79e..f84e23eab8 100644 --- a/lib/codegen/src/ir/builder.rs +++ b/lib/codegen/src/ir/builder.rs @@ -59,7 +59,7 @@ pub trait InstInserterBase<'f>: Sized { fn insert_built_inst(self, inst: Inst, ctrl_typevar: Type) -> &'f mut DataFlowGraph; } -use std::marker::PhantomData; +use core::marker::PhantomData; /// Builder that inserts an instruction at the current position. /// diff --git a/lib/codegen/src/ir/condcodes.rs b/lib/codegen/src/ir/condcodes.rs index d7208278c7..743e30954d 100644 --- a/lib/codegen/src/ir/condcodes.rs +++ b/lib/codegen/src/ir/condcodes.rs @@ -4,8 +4,8 @@ //! are different rules for comparing integers and floating point numbers, so they use different //! condition codes. -use std::fmt::{self, Display, Formatter}; -use std::str::FromStr; +use core::fmt::{self, Display, Formatter}; +use core::str::FromStr; /// Common traits of condition codes. pub trait CondCode: Copy { diff --git a/lib/codegen/src/ir/dfg.rs b/lib/codegen/src/ir/dfg.rs index 7d3e5c94ea..93ea2bbe8c 100644 --- a/lib/codegen/src/ir/dfg.rs +++ b/lib/codegen/src/ir/dfg.rs @@ -10,11 +10,11 @@ use crate::ir::{Ebb, FuncRef, Inst, SigRef, Signature, Type, Value, ValueList, V use crate::isa::TargetIsa; use crate::packed_option::ReservedValue; use crate::write::write_operands; -use std::fmt; -use std::iter; -use std::mem; -use std::ops::{Index, IndexMut}; -use std::u16; +use core::fmt; +use core::iter; +use core::mem; +use core::ops::{Index, IndexMut}; +use core::u16; /// A data flow graph defines all instructions and extended basic blocks in a function as well as /// the data flow dependencies between them. The DFG also tracks values which can be either diff --git a/lib/codegen/src/ir/entities.rs b/lib/codegen/src/ir/entities.rs index b669cf77b6..a0b694ad84 100644 --- a/lib/codegen/src/ir/entities.rs +++ b/lib/codegen/src/ir/entities.rs @@ -20,8 +20,8 @@ //! format. use crate::entity::entity_impl; -use std::fmt; -use std::u32; +use core::fmt; +use core::u32; /// An opaque reference to an extended basic block in a function. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -305,8 +305,8 @@ impl From for AnyEntity { #[cfg(test)] mod tests { use super::*; + use core::u32; use std::string::ToString; - use std::u32; #[test] fn value_with_number() { @@ -320,7 +320,7 @@ mod tests { #[test] fn memory() { use crate::packed_option::PackedOption; - use std::mem; + use core::mem; // This is the whole point of `PackedOption`. assert_eq!( mem::size_of::(), diff --git a/lib/codegen/src/ir/extfunc.rs b/lib/codegen/src/ir/extfunc.rs index 9c17f34556..ada7bab8ca 100644 --- a/lib/codegen/src/ir/extfunc.rs +++ b/lib/codegen/src/ir/extfunc.rs @@ -7,8 +7,8 @@ use crate::ir::{ArgumentLoc, ExternalName, SigRef, Type}; use crate::isa::{CallConv, RegInfo, RegUnit}; -use std::fmt; -use std::str::FromStr; +use core::fmt; +use core::str::FromStr; use std::vec::Vec; /// Function signature. diff --git a/lib/codegen/src/ir/extname.rs b/lib/codegen/src/ir/extname.rs index 7a9da78f7f..5fd8600882 100644 --- a/lib/codegen/src/ir/extname.rs +++ b/lib/codegen/src/ir/extname.rs @@ -5,9 +5,9 @@ //! Cranelift, which compiles functions independently. use crate::ir::LibCall; -use std::cmp; -use std::fmt::{self, Write}; -use std::str::FromStr; +use core::cmp; +use core::fmt::{self, Write}; +use core::str::FromStr; const TESTCASE_NAME_LENGTH: usize = 16; @@ -120,8 +120,8 @@ impl FromStr for ExternalName { mod tests { use super::ExternalName; use crate::ir::LibCall; + use core::u32; use std::string::ToString; - use std::u32; #[test] fn display_testcase() { diff --git a/lib/codegen/src/ir/function.rs b/lib/codegen/src/ir/function.rs index 85bfdb5207..e0ad323d0a 100644 --- a/lib/codegen/src/ir/function.rs +++ b/lib/codegen/src/ir/function.rs @@ -16,7 +16,7 @@ use crate::ir::{JumpTableOffsets, JumpTables}; use crate::isa::{CallConv, EncInfo, Encoding, Legalize, TargetIsa}; use crate::regalloc::RegDiversions; use crate::write::write_function; -use std::fmt; +use core::fmt; /// A function. /// diff --git a/lib/codegen/src/ir/globalvalue.rs b/lib/codegen/src/ir/globalvalue.rs index 6c7d8032f6..5dc7792867 100644 --- a/lib/codegen/src/ir/globalvalue.rs +++ b/lib/codegen/src/ir/globalvalue.rs @@ -3,7 +3,7 @@ use crate::ir::immediates::{Imm64, Offset32}; use crate::ir::{ExternalName, GlobalValue, Type}; use crate::isa::TargetIsa; -use std::fmt; +use core::fmt; /// Information about a global value declaration. #[derive(Clone)] diff --git a/lib/codegen/src/ir/heap.rs b/lib/codegen/src/ir/heap.rs index 8495fd51e3..8a4b4e84b9 100644 --- a/lib/codegen/src/ir/heap.rs +++ b/lib/codegen/src/ir/heap.rs @@ -2,7 +2,7 @@ use crate::ir::immediates::Uimm64; use crate::ir::{GlobalValue, Type}; -use std::fmt; +use core::fmt; /// Information about a heap declaration. #[derive(Clone)] diff --git a/lib/codegen/src/ir/immediates.rs b/lib/codegen/src/ir/immediates.rs index 9badf2fcfd..21d8376e37 100644 --- a/lib/codegen/src/ir/immediates.rs +++ b/lib/codegen/src/ir/immediates.rs @@ -4,10 +4,10 @@ //! Each type here should have a corresponding definition in the `cranelift.immediates` Python //! module in the meta language. -use std::fmt::{self, Display, Formatter}; -use std::mem; -use std::str::FromStr; -use std::{i32, u32}; +use core::fmt::{self, Display, Formatter}; +use core::mem; +use core::str::FromStr; +use core::{i32, u32}; /// 64-bit immediate signed integer operand. /// @@ -729,10 +729,10 @@ impl FromStr for Ieee64 { #[cfg(test)] mod tests { use super::*; - use std::fmt::Display; - use std::str::FromStr; + use core::fmt::Display; + use core::str::FromStr; + use core::{f32, f64}; use std::string::ToString; - use std::{f32, f64}; #[test] fn format_imm64() { diff --git a/lib/codegen/src/ir/instructions.rs b/lib/codegen/src/ir/instructions.rs index 4f9cf5125e..ee4a0b416a 100644 --- a/lib/codegen/src/ir/instructions.rs +++ b/lib/codegen/src/ir/instructions.rs @@ -6,9 +6,9 @@ //! A large part of this module is auto-generated from the instruction descriptions in the meta //! directory. -use std::fmt::{self, Display, Formatter}; -use std::ops::{Deref, DerefMut}; -use std::str::FromStr; +use core::fmt::{self, Display, Formatter}; +use core::ops::{Deref, DerefMut}; +use core::str::FromStr; use std::vec::Vec; use crate::ir; @@ -561,7 +561,7 @@ mod tests { #[test] fn opcodes() { - use std::mem; + use core::mem; let x = Opcode::Iadd; let mut y = Opcode::Isub; @@ -590,7 +590,7 @@ mod tests { #[test] fn instruction_data() { - use std::mem; + use core::mem; // The size of the `InstructionData` enum is important for performance. It should not // exceed 16 bytes. Use `Box` out-of-line payloads for instruction formats that // require more space than that. It would be fine with a data structure smaller than 16 diff --git a/lib/codegen/src/ir/jumptable.rs b/lib/codegen/src/ir/jumptable.rs index e436ea412f..a40ee48e74 100644 --- a/lib/codegen/src/ir/jumptable.rs +++ b/lib/codegen/src/ir/jumptable.rs @@ -4,8 +4,8 @@ //! The actual table of destinations is stored in a `JumpTableData` struct defined in this module. use crate::ir::entities::Ebb; -use std::fmt::{self, Display, Formatter}; -use std::slice::{Iter, IterMut}; +use core::fmt::{self, Display, Formatter}; +use core::slice::{Iter, IterMut}; use std::vec::Vec; /// Contents of a jump table. diff --git a/lib/codegen/src/ir/layout.rs b/lib/codegen/src/ir/layout.rs index 748217847f..e1015f7f3f 100644 --- a/lib/codegen/src/ir/layout.rs +++ b/lib/codegen/src/ir/layout.rs @@ -8,9 +8,9 @@ use crate::ir::progpoint::{ExpandedProgramPoint, ProgramOrder}; use crate::ir::{Ebb, Inst}; use crate::packed_option::PackedOption; use crate::timing; +use core::cmp; +use core::iter::{IntoIterator, Iterator}; use log::debug; -use std::cmp; -use std::iter::{IntoIterator, Iterator}; /// The `Layout` struct determines the layout of EBBs and instructions in a function. It does not /// contain definitions of instructions or EBBs, but depends on `Inst` and `Ebb` entity references @@ -745,7 +745,7 @@ mod tests { use crate::cursor::{Cursor, CursorPosition}; use crate::entity::EntityRef; use crate::ir::{Ebb, Inst, ProgramOrder, SourceLoc}; - use std::cmp::Ordering; + use core::cmp::Ordering; use std::vec::Vec; struct LayoutCursor<'f> { diff --git a/lib/codegen/src/ir/libcall.rs b/lib/codegen/src/ir/libcall.rs index c282de34e4..3617f906d6 100644 --- a/lib/codegen/src/ir/libcall.rs +++ b/lib/codegen/src/ir/libcall.rs @@ -5,8 +5,8 @@ use crate::ir::{ Signature, Type, }; use crate::isa::{CallConv, RegUnit, TargetIsa}; -use std::fmt; -use std::str::FromStr; +use core::fmt; +use core::str::FromStr; /// The name of a runtime library routine. /// diff --git a/lib/codegen/src/ir/memflags.rs b/lib/codegen/src/ir/memflags.rs index 1a1f48d015..87fd6bf3ab 100644 --- a/lib/codegen/src/ir/memflags.rs +++ b/lib/codegen/src/ir/memflags.rs @@ -1,6 +1,6 @@ //! Memory operation flags. -use std::fmt; +use core::fmt; enum FlagBit { Notrap, diff --git a/lib/codegen/src/ir/progpoint.rs b/lib/codegen/src/ir/progpoint.rs index abe2dd786f..bd03c120cd 100644 --- a/lib/codegen/src/ir/progpoint.rs +++ b/lib/codegen/src/ir/progpoint.rs @@ -2,9 +2,9 @@ use crate::entity::EntityRef; use crate::ir::{Ebb, Inst, ValueDef}; -use std::cmp; -use std::fmt; -use std::u32; +use core::cmp; +use core::fmt; +use core::u32; /// A `ProgramPoint` represents a position in a function where the live range of an SSA value can /// begin or end. It can be either: diff --git a/lib/codegen/src/ir/sourceloc.rs b/lib/codegen/src/ir/sourceloc.rs index 3f2add7346..ab5722a57d 100644 --- a/lib/codegen/src/ir/sourceloc.rs +++ b/lib/codegen/src/ir/sourceloc.rs @@ -3,7 +3,7 @@ //! Cranelift tracks the original source location of each instruction, and preserves the source //! location when instructions are transformed. -use std::fmt; +use core::fmt; /// A source location. /// diff --git a/lib/codegen/src/ir/stackslot.rs b/lib/codegen/src/ir/stackslot.rs index bf2d555bf2..4c94a6b012 100644 --- a/lib/codegen/src/ir/stackslot.rs +++ b/lib/codegen/src/ir/stackslot.rs @@ -6,11 +6,11 @@ use crate::entity::{Iter, IterMut, Keys, PrimaryMap}; use crate::ir::{StackSlot, Type}; use crate::packed_option::PackedOption; -use std::cmp; -use std::fmt; -use std::ops::{Index, IndexMut}; -use std::slice; -use std::str::FromStr; +use core::cmp; +use core::fmt; +use core::ops::{Index, IndexMut}; +use core::slice; +use core::str::FromStr; use std::vec::Vec; /// The size of an object on the stack, or the size of a stack frame. diff --git a/lib/codegen/src/ir/table.rs b/lib/codegen/src/ir/table.rs index 003eb710b9..9e436cca64 100644 --- a/lib/codegen/src/ir/table.rs +++ b/lib/codegen/src/ir/table.rs @@ -2,7 +2,7 @@ use crate::ir::immediates::Uimm64; use crate::ir::{GlobalValue, Type}; -use std::fmt; +use core::fmt; /// Information about a table declaration. #[derive(Clone)] diff --git a/lib/codegen/src/ir/trapcode.rs b/lib/codegen/src/ir/trapcode.rs index e0d7ddfe55..273a77670c 100644 --- a/lib/codegen/src/ir/trapcode.rs +++ b/lib/codegen/src/ir/trapcode.rs @@ -1,7 +1,7 @@ //! Trap codes describing the reason for a trap. -use std::fmt::{self, Display, Formatter}; -use std::str::FromStr; +use core::fmt::{self, Display, Formatter}; +use core::str::FromStr; /// A trap code describing the reason for a trap. /// diff --git a/lib/codegen/src/ir/types.rs b/lib/codegen/src/ir/types.rs index ceffe04e9c..2d946e04f8 100644 --- a/lib/codegen/src/ir/types.rs +++ b/lib/codegen/src/ir/types.rs @@ -1,7 +1,7 @@ //! Common types for the Cranelift code generator. -use std::default::Default; -use std::fmt::{self, Debug, Display, Formatter}; +use core::default::Default; +use core::fmt::{self, Debug, Display, Formatter}; use target_lexicon::{PointerWidth, Triple}; /// The type of an SSA value. diff --git a/lib/codegen/src/ir/valueloc.rs b/lib/codegen/src/ir/valueloc.rs index f2c5151f09..0cbcd5d2c3 100644 --- a/lib/codegen/src/ir/valueloc.rs +++ b/lib/codegen/src/ir/valueloc.rs @@ -5,7 +5,7 @@ use crate::ir::StackSlot; use crate::isa::{RegInfo, RegUnit}; -use std::fmt; +use core::fmt; /// Value location. #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/lib/codegen/src/isa/arm32/mod.rs b/lib/codegen/src/isa/arm32/mod.rs index a184061ac0..7ceacf036b 100644 --- a/lib/codegen/src/isa/arm32/mod.rs +++ b/lib/codegen/src/isa/arm32/mod.rs @@ -15,8 +15,8 @@ use crate::isa::enc_tables::{self as shared_enc_tables, lookup_enclist, Encoding use crate::isa::Builder as IsaBuilder; use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa}; use crate::regalloc; +use core::fmt; use std::boxed::Box; -use std::fmt; use target_lexicon::{Architecture, Triple}; #[allow(dead_code)] diff --git a/lib/codegen/src/isa/arm32/settings.rs b/lib/codegen/src/isa/arm32/settings.rs index d32483100f..5490c8c2b3 100644 --- a/lib/codegen/src/isa/arm32/settings.rs +++ b/lib/codegen/src/isa/arm32/settings.rs @@ -1,7 +1,7 @@ //! ARM32 Settings. use crate::settings::{self, detail, Builder}; -use std::fmt; +use core::fmt; // Include code generated by `lib/codegen/meta-python/gen_settings.py`. This file contains a public // `Flags` struct with an impl for all of the settings defined in diff --git a/lib/codegen/src/isa/arm64/mod.rs b/lib/codegen/src/isa/arm64/mod.rs index b438e49949..84277301c3 100644 --- a/lib/codegen/src/isa/arm64/mod.rs +++ b/lib/codegen/src/isa/arm64/mod.rs @@ -15,8 +15,8 @@ use crate::isa::enc_tables::{lookup_enclist, Encodings}; use crate::isa::Builder as IsaBuilder; use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa}; use crate::regalloc; +use core::fmt; use std::boxed::Box; -use std::fmt; use target_lexicon::Triple; #[allow(dead_code)] diff --git a/lib/codegen/src/isa/arm64/settings.rs b/lib/codegen/src/isa/arm64/settings.rs index 6ef1fcb258..e7d0abdf6c 100644 --- a/lib/codegen/src/isa/arm64/settings.rs +++ b/lib/codegen/src/isa/arm64/settings.rs @@ -1,7 +1,7 @@ //! ARM64 Settings. use crate::settings::{self, detail, Builder}; -use std::fmt; +use core::fmt; // Include code generated by `lib/codegen/meta-python/gen_settings.py`. This file contains a public // `Flags` struct with an impl for all of the settings defined in diff --git a/lib/codegen/src/isa/call_conv.rs b/lib/codegen/src/isa/call_conv.rs index 9caff3ebaa..d4b4d3999b 100644 --- a/lib/codegen/src/isa/call_conv.rs +++ b/lib/codegen/src/isa/call_conv.rs @@ -1,5 +1,5 @@ -use std::fmt; -use std::str; +use core::fmt; +use core::str; use target_lexicon::{CallingConvention, Triple}; /// Calling convention identifiers. diff --git a/lib/codegen/src/isa/enc_tables.rs b/lib/codegen/src/isa/enc_tables.rs index ccdbc28ebd..323473fe2b 100644 --- a/lib/codegen/src/isa/enc_tables.rs +++ b/lib/codegen/src/isa/enc_tables.rs @@ -7,7 +7,7 @@ use crate::constant_hash::{probe, Table}; use crate::ir::{Function, InstructionData, Opcode, Type}; use crate::isa::{Encoding, Legalize}; use crate::settings::PredicateView; -use std::ops::Range; +use core::ops::Range; /// A recipe predicate. /// diff --git a/lib/codegen/src/isa/encoding.rs b/lib/codegen/src/isa/encoding.rs index bf6a001b07..118bfa2006 100644 --- a/lib/codegen/src/isa/encoding.rs +++ b/lib/codegen/src/isa/encoding.rs @@ -4,7 +4,7 @@ use crate::binemit::CodeOffset; use crate::ir::{Function, Inst}; use crate::isa::constraints::{BranchRange, RecipeConstraints}; use crate::regalloc::RegDiversions; -use std::fmt; +use core::fmt; /// Bits needed to encode an instruction as binary machine code. /// diff --git a/lib/codegen/src/isa/mod.rs b/lib/codegen/src/isa/mod.rs index 291fdfb76c..ab77269ea5 100644 --- a/lib/codegen/src/isa/mod.rs +++ b/lib/codegen/src/isa/mod.rs @@ -63,9 +63,9 @@ use crate::result::CodegenResult; use crate::settings; use crate::settings::SetResult; use crate::timing; +use core::fmt; use failure_derive::Fail; use std::boxed::Box; -use std::fmt; use target_lexicon::{Architecture, PointerWidth, Triple}; #[cfg(build_riscv)] diff --git a/lib/codegen/src/isa/registers.rs b/lib/codegen/src/isa/registers.rs index 4941712618..6baebd7237 100644 --- a/lib/codegen/src/isa/registers.rs +++ b/lib/codegen/src/isa/registers.rs @@ -1,7 +1,7 @@ //! Data structures describing the registers in an ISA. use crate::entity::EntityRef; -use std::fmt; +use core::fmt; /// Register units are the smallest units of register allocation. /// diff --git a/lib/codegen/src/isa/riscv/abi.rs b/lib/codegen/src/isa/riscv/abi.rs index cd33de404e..59b266f7ca 100644 --- a/lib/codegen/src/isa/riscv/abi.rs +++ b/lib/codegen/src/isa/riscv/abi.rs @@ -11,7 +11,7 @@ use crate::abi::{legalize_args, ArgAction, ArgAssigner, ValueConversion}; use crate::ir::{self, AbiParam, ArgumentExtension, ArgumentLoc, ArgumentPurpose, Type}; use crate::isa::RegClass; use crate::regalloc::RegisterSet; -use std::i32; +use core::i32; use target_lexicon::Triple; struct Args { diff --git a/lib/codegen/src/isa/riscv/binemit.rs b/lib/codegen/src/isa/riscv/binemit.rs index cd153f4ac1..64f3c00298 100644 --- a/lib/codegen/src/isa/riscv/binemit.rs +++ b/lib/codegen/src/isa/riscv/binemit.rs @@ -5,7 +5,7 @@ use crate::ir::{Function, Inst, InstructionData}; use crate::isa::{RegUnit, StackBaseMask, StackRef}; use crate::predicates::is_signed_int; use crate::regalloc::RegDiversions; -use std::u32; +use core::u32; include!(concat!(env!("OUT_DIR"), "/binemit-riscv.rs")); diff --git a/lib/codegen/src/isa/riscv/mod.rs b/lib/codegen/src/isa/riscv/mod.rs index 7382809893..60154cf162 100644 --- a/lib/codegen/src/isa/riscv/mod.rs +++ b/lib/codegen/src/isa/riscv/mod.rs @@ -15,8 +15,8 @@ use crate::isa::enc_tables::{self as shared_enc_tables, lookup_enclist, Encoding use crate::isa::Builder as IsaBuilder; use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa}; use crate::regalloc; +use core::fmt; use std::boxed::Box; -use std::fmt; use target_lexicon::{PointerWidth, Triple}; #[allow(dead_code)] @@ -129,7 +129,7 @@ mod tests { use crate::ir::{Function, InstructionData, Opcode}; use crate::isa; use crate::settings::{self, Configurable}; - use std::str::FromStr; + use core::str::FromStr; use std::string::{String, ToString}; use target_lexicon::triple; diff --git a/lib/codegen/src/isa/riscv/settings.rs b/lib/codegen/src/isa/riscv/settings.rs index 2530c4a860..660c1ef669 100644 --- a/lib/codegen/src/isa/riscv/settings.rs +++ b/lib/codegen/src/isa/riscv/settings.rs @@ -1,7 +1,7 @@ //! RISC-V Settings. use crate::settings::{self, detail, Builder}; -use std::fmt; +use core::fmt; // Include code generated by `lib/codegen/meta-python/gen_settings.py`. This file contains a public // `Flags` struct with an impl for all of the settings defined in diff --git a/lib/codegen/src/isa/x86/abi.rs b/lib/codegen/src/isa/x86/abi.rs index fd09a9511c..39852fce90 100644 --- a/lib/codegen/src/isa/x86/abi.rs +++ b/lib/codegen/src/isa/x86/abi.rs @@ -14,7 +14,7 @@ use crate::isa::{CallConv, RegClass, RegUnit, TargetIsa}; use crate::regalloc::RegisterSet; use crate::result::CodegenResult; use crate::stack_layout::layout_stack; -use std::i32; +use core::i32; use target_lexicon::{PointerWidth, Triple}; /// Argument registers for x86-64 diff --git a/lib/codegen/src/isa/x86/mod.rs b/lib/codegen/src/isa/x86/mod.rs index 10ec0e5269..acefe0e786 100644 --- a/lib/codegen/src/isa/x86/mod.rs +++ b/lib/codegen/src/isa/x86/mod.rs @@ -17,8 +17,8 @@ use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa}; use crate::regalloc; use crate::result::CodegenResult; use crate::timing; +use core::fmt; use std::boxed::Box; -use std::fmt; use target_lexicon::{PointerWidth, Triple}; #[allow(dead_code)] diff --git a/lib/codegen/src/isa/x86/settings.rs b/lib/codegen/src/isa/x86/settings.rs index 52eccaf9f8..b638ea7ee8 100644 --- a/lib/codegen/src/isa/x86/settings.rs +++ b/lib/codegen/src/isa/x86/settings.rs @@ -1,7 +1,7 @@ //! x86 Settings. use crate::settings::{self, detail, Builder}; -use std::fmt; +use core::fmt; // Include code generated by `lib/codegen/meta-python/gen_settings.py`. This file contains a public // `Flags` struct with an impl for all of the settings defined in diff --git a/lib/codegen/src/legalizer/split.rs b/lib/codegen/src/legalizer/split.rs index 9e27ae2397..67d947f4a9 100644 --- a/lib/codegen/src/legalizer/split.rs +++ b/lib/codegen/src/legalizer/split.rs @@ -67,7 +67,7 @@ use crate::cursor::{Cursor, CursorPosition, FuncCursor}; use crate::flowgraph::{BasicBlock, ControlFlowGraph}; use crate::ir::{self, Ebb, Inst, InstBuilder, InstructionData, Opcode, Type, Value, ValueDef}; -use std::iter; +use core::iter; use std::vec::Vec; /// Split `value` into two values using the `isplit` semantics. Do this by reusing existing values diff --git a/lib/codegen/src/lib.rs b/lib/codegen/src/lib.rs index f73167d7fc..48f5a27902 100644 --- a/lib/codegen/src/lib.rs +++ b/lib/codegen/src/lib.rs @@ -40,15 +40,20 @@ clippy::use_self ) )] -// Turns on no_std and alloc features if std is not available. -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![cfg_attr(not(feature = "std"), feature(alloc))] -// TODO: Remove this workaround once https://github.com/rust-lang/rust/issues/27747 is done. -#![cfg_attr(not(feature = "std"), feature(slice_concat_ext))] #[cfg(not(feature = "std"))] #[macro_use] -extern crate alloc; +extern crate alloc as std; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; + +#[cfg(not(feature = "std"))] +use hashmap_core::{map as hash_map, HashMap, HashSet}; +#[cfg(feature = "std")] +use std::collections::{hash_map, HashMap, HashSet}; pub use crate::context::Context; pub use crate::legalizer::legalize_function; @@ -101,20 +106,5 @@ mod unreachable_code; pub use crate::result::{CodegenError, CodegenResult}; -/// This replaces `std` in builds with `core`. -#[cfg(not(feature = "std"))] -mod std { - pub use alloc::{boxed, slice, string, vec}; - pub use core::*; - pub mod collections { - #[allow(unused_extern_crates)] - extern crate hashmap_core; - - pub use self::hashmap_core::map as hash_map; - pub use self::hashmap_core::{HashMap, HashSet}; - pub use alloc::collections::BTreeSet; - } -} - /// Version number of this crate. pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/lib/codegen/src/partition_slice.rs b/lib/codegen/src/partition_slice.rs index b1e0f95f85..a4ee129008 100644 --- a/lib/codegen/src/partition_slice.rs +++ b/lib/codegen/src/partition_slice.rs @@ -1,6 +1,6 @@ //! Rearrange the elements in a slice according to a predicate. -use std::mem; +use core::mem; /// Rearrange the elements of the mutable slice `s` such that elements where `p(t)` is true precede /// the elements where `p(t)` is false. diff --git a/lib/codegen/src/print_errors.rs b/lib/codegen/src/print_errors.rs index c4f48ef300..2361cb714a 100644 --- a/lib/codegen/src/print_errors.rs +++ b/lib/codegen/src/print_errors.rs @@ -8,9 +8,9 @@ use crate::isa::TargetIsa; use crate::result::CodegenError; use crate::verifier::{VerifierError, VerifierErrors}; use crate::write::{decorate_function, FuncWriter, PlainWriter}; +use core::fmt; +use core::fmt::Write; use std::boxed::Box; -use std::fmt; -use std::fmt::Write; use std::string::{String, ToString}; use std::vec::Vec; diff --git a/lib/codegen/src/ref_slice.rs b/lib/codegen/src/ref_slice.rs index 251b601d00..2fad921cea 100644 --- a/lib/codegen/src/ref_slice.rs +++ b/lib/codegen/src/ref_slice.rs @@ -7,7 +7,7 @@ //! //! Despite their using an unsafe block, these functions are completely safe. -use std::slice; +use core::slice; pub fn ref_slice(s: &T) -> &[T] { unsafe { slice::from_raw_parts(s, 1) } diff --git a/lib/codegen/src/regalloc/affinity.rs b/lib/codegen/src/regalloc/affinity.rs index 39d23b31bb..bbd29f8f2e 100644 --- a/lib/codegen/src/regalloc/affinity.rs +++ b/lib/codegen/src/regalloc/affinity.rs @@ -10,7 +10,7 @@ use crate::ir::{AbiParam, ArgumentLoc}; use crate::isa::{ConstraintKind, OperandConstraint, RegClassIndex, RegInfo, TargetIsa}; -use std::fmt; +use core::fmt; /// Preferred register allocation for an SSA value. #[derive(Clone, Copy, Debug)] diff --git a/lib/codegen/src/regalloc/coalescing.rs b/lib/codegen/src/regalloc/coalescing.rs index cf290e1f31..f59435d170 100644 --- a/lib/codegen/src/regalloc/coalescing.rs +++ b/lib/codegen/src/regalloc/coalescing.rs @@ -17,11 +17,11 @@ use crate::regalloc::affinity::Affinity; use crate::regalloc::liveness::Liveness; use crate::regalloc::virtregs::{VirtReg, VirtRegs}; use crate::timing; +use core::cmp; +use core::fmt; +use core::iter; +use core::slice; use log::debug; -use std::cmp; -use std::fmt; -use std::iter; -use std::slice; use std::vec::Vec; // # Implementation diff --git a/lib/codegen/src/regalloc/coloring.rs b/lib/codegen/src/regalloc/coloring.rs index 566dd7dee3..12ef018329 100644 --- a/lib/codegen/src/regalloc/coloring.rs +++ b/lib/codegen/src/regalloc/coloring.rs @@ -57,8 +57,8 @@ use crate::regalloc::register_set::RegisterSet; use crate::regalloc::solver::{Solver, SolverError}; use crate::regalloc::RegDiversions; use crate::timing; +use core::mem; use log::debug; -use std::mem; /// Data structures for the coloring pass. /// diff --git a/lib/codegen/src/regalloc/diversion.rs b/lib/codegen/src/regalloc/diversion.rs index 516f95c610..6e9b1f23cd 100644 --- a/lib/codegen/src/regalloc/diversion.rs +++ b/lib/codegen/src/regalloc/diversion.rs @@ -8,11 +8,11 @@ //! EBB. use crate::fx::FxHashMap; +use crate::hash_map::{Entry, Iter}; use crate::ir::{InstructionData, Opcode}; use crate::ir::{StackSlot, Value, ValueLoc, ValueLocations}; use crate::isa::{RegInfo, RegUnit}; -use std::collections::hash_map::{Entry, Iter}; -use std::fmt; +use core::fmt; /// A diversion of a value from its original location to a new register or stack location. /// diff --git a/lib/codegen/src/regalloc/liveness.rs b/lib/codegen/src/regalloc/liveness.rs index 186aaadd60..5e21c14c90 100644 --- a/lib/codegen/src/regalloc/liveness.rs +++ b/lib/codegen/src/regalloc/liveness.rs @@ -183,8 +183,8 @@ use crate::isa::{EncInfo, OperandConstraint, TargetIsa}; use crate::regalloc::affinity::Affinity; use crate::regalloc::liverange::{LiveRange, LiveRangeContext, LiveRangeForest}; use crate::timing; -use std::mem; -use std::ops::Index; +use core::mem; +use core::ops::Index; use std::vec::Vec; /// A set of live ranges, indexed by value number. diff --git a/lib/codegen/src/regalloc/liverange.rs b/lib/codegen/src/regalloc/liverange.rs index a1012ef1c4..5629ea40ec 100644 --- a/lib/codegen/src/regalloc/liverange.rs +++ b/lib/codegen/src/regalloc/liverange.rs @@ -111,8 +111,8 @@ use crate::bforest; use crate::entity::SparseMapValue; use crate::ir::{Ebb, ExpandedProgramPoint, Inst, Layout, ProgramOrder, ProgramPoint, Value}; use crate::regalloc::affinity::Affinity; -use std::cmp::Ordering; -use std::marker::PhantomData; +use core::cmp::Ordering; +use core::marker::PhantomData; /// Global live range of a single SSA value. /// @@ -461,7 +461,7 @@ mod tests { use crate::entity::EntityRef; use crate::ir::{Ebb, Inst, Value}; use crate::ir::{ExpandedProgramPoint, ProgramOrder}; - use std::cmp::Ordering; + use core::cmp::Ordering; use std::vec::Vec; // Dummy program order which simply compares indexes. diff --git a/lib/codegen/src/regalloc/pressure.rs b/lib/codegen/src/regalloc/pressure.rs index 1e45942585..cd1ef956a5 100644 --- a/lib/codegen/src/regalloc/pressure.rs +++ b/lib/codegen/src/regalloc/pressure.rs @@ -38,9 +38,9 @@ use crate::isa::registers::{RegClass, RegClassMask, RegInfo, MAX_TRACKED_TOPRCS}; use crate::regalloc::RegisterSet; -use std::cmp::min; -use std::fmt; -use std::iter::ExactSizeIterator; +use core::cmp::min; +use core::fmt; +use core::iter::ExactSizeIterator; /// Information per top-level register class. /// @@ -275,9 +275,9 @@ mod tests { use super::Pressure; use crate::isa::{RegClass, TargetIsa}; use crate::regalloc::RegisterSet; - use std::borrow::Borrow; + use core::borrow::Borrow; + use core::str::FromStr; use std::boxed::Box; - use std::str::FromStr; use target_lexicon::triple; // Make an arm32 `TargetIsa`, if possible. diff --git a/lib/codegen/src/regalloc/register_set.rs b/lib/codegen/src/regalloc/register_set.rs index df73a3cda0..29b6df7625 100644 --- a/lib/codegen/src/regalloc/register_set.rs +++ b/lib/codegen/src/regalloc/register_set.rs @@ -6,10 +6,10 @@ //! share a register unit can't be in use at the same time. use crate::isa::registers::{RegClass, RegInfo, RegUnit, RegUnitMask}; -use std::char; -use std::fmt; -use std::iter::ExactSizeIterator; -use std::mem::size_of_val; +use core::char; +use core::fmt; +use core::iter::ExactSizeIterator; +use core::mem::size_of_val; /// Set of registers available for allocation. #[derive(Clone)] diff --git a/lib/codegen/src/regalloc/solver.rs b/lib/codegen/src/regalloc/solver.rs index ceb6cdfb4b..be1c9a2d27 100644 --- a/lib/codegen/src/regalloc/solver.rs +++ b/lib/codegen/src/regalloc/solver.rs @@ -104,11 +104,11 @@ use crate::entity::{SparseMap, SparseMapValue}; use crate::ir::Value; use crate::isa::{RegClass, RegUnit}; use crate::regalloc::register_set::RegSetIter; +use core::cmp; +use core::fmt; +use core::mem; +use core::u16; use log::debug; -use std::cmp; -use std::fmt; -use std::mem; -use std::u16; use std::vec::Vec; /// A variable in the constraint problem. @@ -1134,8 +1134,8 @@ mod tests { use crate::ir::Value; use crate::isa::{RegClass, RegInfo, RegUnit, TargetIsa}; use crate::regalloc::RegisterSet; + use core::str::FromStr; use std::boxed::Box; - use std::str::FromStr; use target_lexicon::triple; // Make an arm32 `TargetIsa`, if possible. diff --git a/lib/codegen/src/regalloc/spilling.rs b/lib/codegen/src/regalloc/spilling.rs index 7bbf26422c..61458f4066 100644 --- a/lib/codegen/src/regalloc/spilling.rs +++ b/lib/codegen/src/regalloc/spilling.rs @@ -27,8 +27,8 @@ use crate::regalloc::pressure::Pressure; use crate::regalloc::virtregs::VirtRegs; use crate::timing; use crate::topo_order::TopoOrder; +use core::fmt; use log::debug; -use std::fmt; use std::vec::Vec; /// Return a top-level register class which contains `unit`. diff --git a/lib/codegen/src/regalloc/virtregs.rs b/lib/codegen/src/regalloc/virtregs.rs index 3fe55474de..584ad9c53a 100644 --- a/lib/codegen/src/regalloc/virtregs.rs +++ b/lib/codegen/src/regalloc/virtregs.rs @@ -19,8 +19,8 @@ use crate::entity::{Keys, PrimaryMap, SecondaryMap}; use crate::ir::{Function, Value}; use crate::packed_option::PackedOption; use crate::ref_slice::ref_slice; -use std::cmp::Ordering; -use std::fmt; +use core::cmp::Ordering; +use core::fmt; use std::vec::Vec; /// A virtual register reference. diff --git a/lib/codegen/src/scoped_hash_map.rs b/lib/codegen/src/scoped_hash_map.rs index 97d073151b..fe22e26ce3 100644 --- a/lib/codegen/src/scoped_hash_map.rs +++ b/lib/codegen/src/scoped_hash_map.rs @@ -1,13 +1,12 @@ //! `ScopedHashMap` //! -//! This module defines a struct `ScopedHashMap` which defines a `HashMap`-like +//! This module defines a struct `ScopedHashMap` which defines a `FxHashMap`-like //! container that has a concept of scopes that can be entered and exited, such that //! values inserted while inside a scope aren't visible outside the scope. use crate::fx::FxHashMap; -use std::collections::hash_map; -use std::hash::Hash; -use std::mem; +use core::hash::Hash; +use core::mem; struct Val { value: V, @@ -17,7 +16,7 @@ struct Val { /// A view into an occupied entry in a `ScopedHashMap`. It is part of the `Entry` enum. pub struct OccupiedEntry<'a, K: 'a, V: 'a> { - entry: hash_map::OccupiedEntry<'a, K, Val>, + entry: super::hash_map::OccupiedEntry<'a, K, Val>, } impl<'a, K, V> OccupiedEntry<'a, K, V> { @@ -29,7 +28,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// A view into a vacant entry in a `ScopedHashMap`. It is part of the `Entry` enum. pub struct VacantEntry<'a, K: 'a, V: 'a> { - entry: hash_map::VacantEntry<'a, K, Val>, + entry: super::hash_map::VacantEntry<'a, K, Val>, next_key: Option, depth: usize, } @@ -53,7 +52,7 @@ pub enum Entry<'a, K: 'a, V: 'a> { Vacant(VacantEntry<'a, K, V>), } -/// A wrapper around a `HashMap` which adds the concept of scopes. Items inserted +/// A wrapper around a `FxHashMap` which adds the concept of scopes. Items inserted /// within a scope are removed when the scope is exited. /// /// Shadowing, where one scope has entries with the same keys as a containing scope, @@ -77,10 +76,10 @@ where } } - /// Similar to `HashMap::entry`, gets the given key's corresponding entry in the map for + /// Similar to `FxHashMap::entry`, gets the given key's corresponding entry in the map for /// in-place manipulation. pub fn entry(&mut self, key: K) -> Entry { - use self::hash_map::Entry::*; + use super::hash_map::Entry::*; match self.map.entry(key) { Occupied(entry) => Entry::Occupied(OccupiedEntry { entry }), Vacant(entry) => { @@ -104,7 +103,7 @@ where pub fn decrement_depth(&mut self) { // Remove all elements inserted at the current depth. while let Some(key) = self.last_insert.clone() { - use self::hash_map::Entry::*; + use crate::hash_map::Entry::*; match self.map.entry(key) { Occupied(entry) => { if entry.get().depth != self.current_depth { diff --git a/lib/codegen/src/settings.rs b/lib/codegen/src/settings.rs index a8ce0166f3..d0f5d5b13e 100644 --- a/lib/codegen/src/settings.rs +++ b/lib/codegen/src/settings.rs @@ -22,16 +22,12 @@ use crate::constant_hash::{probe, simple_hash}; use crate::isa::TargetIsa; +use core::fmt; +use core::str; use failure_derive::Fail; use std::boxed::Box; -use std::fmt; -use std::str; use std::string::{String, ToString}; -// TODO: Remove this workaround once https://github.com/rust-lang/rust/issues/27747 is done. -#[cfg(not(feature = "std"))] -use std::slice::SliceConcatExt; - /// A string-based configurator for settings groups. /// /// The `Configurable` protocol allows settings to be modified by name before a finished `Flags` @@ -111,10 +107,21 @@ fn parse_bool_value(value: &str) -> SetResult { fn parse_enum_value(value: &str, choices: &[&str]) -> SetResult { match choices.iter().position(|&tag| tag == value) { Some(idx) => Ok(idx as u8), - None => Err(SetError::BadValue(format!( - "any among {}", - choices.join(", ") - ))), + None => { + // TODO: Use `join` instead of this code, once + // https://github.com/rust-lang/rust/issues/27747 is resolved. + let mut all_choices = String::new(); + let mut first = true; + for choice in choices { + if first { + first = false + } else { + all_choices += ", "; + } + all_choices += choice; + } + Err(SetError::BadValue(format!("any among {}", all_choices))) + } } } @@ -204,7 +211,7 @@ impl<'a> PredicateView<'a> { /// code in other modules. pub mod detail { use crate::constant_hash; - use std::fmt; + use core::fmt; /// An instruction group template. pub struct Template { diff --git a/lib/codegen/src/simple_gvn.rs b/lib/codegen/src/simple_gvn.rs index 576934071d..21a0b9beb9 100644 --- a/lib/codegen/src/simple_gvn.rs +++ b/lib/codegen/src/simple_gvn.rs @@ -5,8 +5,8 @@ use crate::dominator_tree::DominatorTree; use crate::ir::{Function, Inst, InstructionData, Opcode, Type}; use crate::scoped_hash_map::ScopedHashMap; use crate::timing; -use std::cell::{Ref, RefCell}; -use std::hash::{Hash, Hasher}; +use core::cell::{Ref, RefCell}; +use core::hash::{Hash, Hasher}; use std::vec::Vec; /// Test whether the given opcode is unsafe to even consider for GVN. diff --git a/lib/codegen/src/stack_layout.rs b/lib/codegen/src/stack_layout.rs index 54eff6a759..0f5c8ae639 100644 --- a/lib/codegen/src/stack_layout.rs +++ b/lib/codegen/src/stack_layout.rs @@ -3,7 +3,7 @@ use crate::ir::stackslot::{StackOffset, StackSize, StackSlotKind}; use crate::ir::StackSlots; use crate::result::{CodegenError, CodegenResult}; -use std::cmp::{max, min}; +use core::cmp::{max, min}; /// Compute the stack frame layout. /// diff --git a/lib/codegen/src/timing.rs b/lib/codegen/src/timing.rs index b3aaab1f41..16f729f71d 100644 --- a/lib/codegen/src/timing.rs +++ b/lib/codegen/src/timing.rs @@ -2,7 +2,7 @@ //! //! This modules provides facilities for timing the execution of individual compilation passes. -use std::fmt; +use core::fmt; pub use self::details::{add_to_current, take_current, PassTimes, TimingToken}; diff --git a/lib/codegen/src/topo_order.rs b/lib/codegen/src/topo_order.rs index 9486b054f4..7dcec7274b 100644 --- a/lib/codegen/src/topo_order.rs +++ b/lib/codegen/src/topo_order.rs @@ -94,7 +94,7 @@ mod tests { use crate::dominator_tree::DominatorTree; use crate::flowgraph::ControlFlowGraph; use crate::ir::{Function, InstBuilder}; - use std::iter; + use core::iter; #[test] fn empty() { diff --git a/lib/codegen/src/verifier/liveness.rs b/lib/codegen/src/verifier/liveness.rs index c77ac82e16..0d18504d31 100644 --- a/lib/codegen/src/verifier/liveness.rs +++ b/lib/codegen/src/verifier/liveness.rs @@ -8,7 +8,7 @@ use crate::regalloc::liveness::Liveness; use crate::regalloc::liverange::LiveRange; use crate::timing; use crate::verifier::{VerifierErrors, VerifierStepResult}; -use std::cmp::Ordering; +use core::cmp::Ordering; /// Verify liveness information for `func`. /// diff --git a/lib/codegen/src/verifier/mod.rs b/lib/codegen/src/verifier/mod.rs index 1268ed489f..d139c54681 100644 --- a/lib/codegen/src/verifier/mod.rs +++ b/lib/codegen/src/verifier/mod.rs @@ -72,10 +72,10 @@ use crate::isa::TargetIsa; use crate::iterators::IteratorExtras; use crate::settings::FlagsOrIsa; use crate::timing; +use core::cmp::Ordering; +use core::fmt::{self, Display, Formatter, Write}; use failure_derive::Fail; -use std::cmp::Ordering; use std::collections::BTreeSet; -use std::fmt::{self, Display, Formatter, Write}; use std::string::String; use std::vec::Vec; diff --git a/lib/codegen/src/write.rs b/lib/codegen/src/write.rs index c01e7b8bd4..7731918c59 100644 --- a/lib/codegen/src/write.rs +++ b/lib/codegen/src/write.rs @@ -8,7 +8,7 @@ use crate::ir::entities::AnyEntity; use crate::ir::{DataFlowGraph, Ebb, Function, Inst, SigRef, Type, Value, ValueDef}; use crate::isa::{RegInfo, TargetIsa}; use crate::packed_option::ReservedValue; -use std::fmt::{self, Write}; +use core::fmt::{self, Write}; use std::string::String; use std::vec::Vec; diff --git a/lib/entity/Cargo.toml b/lib/entity/Cargo.toml index ca28428af9..798a3caacf 100644 --- a/lib/entity/Cargo.toml +++ b/lib/entity/Cargo.toml @@ -14,6 +14,7 @@ edition = "2018" [features] default = ["std"] std = [] +core = [] [badges] maintenance = { status = "experimental" } diff --git a/lib/entity/src/boxed_slice.rs b/lib/entity/src/boxed_slice.rs index e619238387..2030c6b536 100644 --- a/lib/entity/src/boxed_slice.rs +++ b/lib/entity/src/boxed_slice.rs @@ -3,9 +3,10 @@ use crate::iter::{Iter, IterMut}; use crate::keys::Keys; use crate::EntityRef; -use std::marker::PhantomData; -use std::ops::{Index, IndexMut}; -use std::slice; +use core::marker::PhantomData; +use core::ops::{Index, IndexMut}; +use core::slice; +use std::boxed::Box; /// A slice mapping `K -> V` allocating dense entity references. /// @@ -140,6 +141,7 @@ where mod tests { use super::*; use crate::primary::PrimaryMap; + use std::vec::Vec; // `EntityRef` impl for testing. #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/lib/entity/src/iter.rs b/lib/entity/src/iter.rs index 27053a0962..8c681023d2 100644 --- a/lib/entity/src/iter.rs +++ b/lib/entity/src/iter.rs @@ -1,9 +1,9 @@ //! A double-ended iterator over entity references and entities. use crate::EntityRef; -use std::iter::Enumerate; -use std::marker::PhantomData; -use std::slice; +use core::iter::Enumerate; +use core::marker::PhantomData; +use core::slice; /// Iterate over all keys in order. pub struct Iter<'a, K: EntityRef, V> diff --git a/lib/entity/src/keys.rs b/lib/entity/src/keys.rs index ea5bd89441..bfbaa0cb90 100644 --- a/lib/entity/src/keys.rs +++ b/lib/entity/src/keys.rs @@ -1,10 +1,10 @@ //! A double-ended iterator over entity references. //! -//! When `std::iter::Step` is stabilized, `Keys` could be implemented as a wrapper around -//! `std::ops::Range`, but for now, we implement it manually. +//! When `core::iter::Step` is stabilized, `Keys` could be implemented as a wrapper around +//! `core::ops::Range`, but for now, we implement it manually. use crate::EntityRef; -use std::marker::PhantomData; +use core::marker::PhantomData; /// Iterate over all keys in order. pub struct Keys { diff --git a/lib/entity/src/lib.rs b/lib/entity/src/lib.rs index ce34c1e50f..8d5579000b 100644 --- a/lib/entity/src/lib.rs +++ b/lib/entity/src/lib.rs @@ -50,17 +50,15 @@ clippy::use_self ) )] -// Turns on no_std and alloc features if std is not available. -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![cfg_attr(not(feature = "std"), feature(alloc))] -/// This replaces `std` in builds with `core`. #[cfg(not(feature = "std"))] -mod std { - extern crate alloc; - pub use self::alloc::{boxed, string, vec}; - pub use core::*; -} +#[macro_use] +extern crate alloc as std; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; // Re-export core so that the macros works with both std and no_std crates #[doc(hidden)] diff --git a/lib/entity/src/list.rs b/lib/entity/src/list.rs index 4dc3ce575d..009b3d70b1 100644 --- a/lib/entity/src/list.rs +++ b/lib/entity/src/list.rs @@ -1,8 +1,8 @@ //! Small lists of entity references. use crate::packed_option::ReservedValue; use crate::EntityRef; -use std::marker::PhantomData; -use std::mem; +use core::marker::PhantomData; +use core::mem; use std::vec::Vec; /// A small list of entity references allocated from a pool. diff --git a/lib/entity/src/map.rs b/lib/entity/src/map.rs index e0f331106b..9410cc2f90 100644 --- a/lib/entity/src/map.rs +++ b/lib/entity/src/map.rs @@ -3,9 +3,9 @@ use crate::iter::{Iter, IterMut}; use crate::keys::Keys; use crate::EntityRef; -use std::marker::PhantomData; -use std::ops::{Index, IndexMut}; -use std::slice; +use core::marker::PhantomData; +use core::ops::{Index, IndexMut}; +use core::slice; use std::vec::Vec; /// A mapping `K -> V` for densely indexed entity references. diff --git a/lib/entity/src/packed_option.rs b/lib/entity/src/packed_option.rs index cf246a2498..0757e9e19b 100644 --- a/lib/entity/src/packed_option.rs +++ b/lib/entity/src/packed_option.rs @@ -7,8 +7,8 @@ //! This module provides a `PackedOption` for types that have a reserved value that can be used //! to represent `None`. -use std::fmt; -use std::mem; +use core::fmt; +use core::mem; /// Types that have a reserved value which can't be created any other way. pub trait ReservedValue: Eq { diff --git a/lib/entity/src/primary.rs b/lib/entity/src/primary.rs index 8f1daed7e3..d3fc7470c0 100644 --- a/lib/entity/src/primary.rs +++ b/lib/entity/src/primary.rs @@ -3,10 +3,11 @@ use crate::boxed_slice::BoxedSlice; use crate::iter::{Iter, IterMut}; use crate::keys::Keys; use crate::EntityRef; -use std::iter::FromIterator; -use std::marker::PhantomData; -use std::ops::{Index, IndexMut}; -use std::slice; +use core::iter::FromIterator; +use core::marker::PhantomData; +use core::ops::{Index, IndexMut}; +use core::slice; +use std::boxed::Box; use std::vec::Vec; /// A primary mapping `K -> V` allocating dense entity references. diff --git a/lib/entity/src/set.rs b/lib/entity/src/set.rs index 80765130e6..f89e96538a 100644 --- a/lib/entity/src/set.rs +++ b/lib/entity/src/set.rs @@ -2,7 +2,7 @@ use crate::keys::Keys; use crate::EntityRef; -use std::marker::PhantomData; +use core::marker::PhantomData; use std::vec::Vec; /// A set of `K` for densely indexed entity references. @@ -80,7 +80,7 @@ where #[cfg(test)] mod tests { use super::*; - use std::u32; + use core::u32; // `EntityRef` impl for testing. #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/lib/entity/src/sparse.rs b/lib/entity/src/sparse.rs index a6a43db5e0..83c519f47f 100644 --- a/lib/entity/src/sparse.rs +++ b/lib/entity/src/sparse.rs @@ -9,9 +9,9 @@ use crate::map::SecondaryMap; use crate::EntityRef; -use std::mem; -use std::slice; -use std::u32; +use core::mem; +use core::slice; +use core::u32; use std::vec::Vec; /// Trait for extracting keys from values stored in a `SparseMap`. diff --git a/lib/frontend/src/frontend.rs b/lib/frontend/src/frontend.rs index 06fbbecec5..c1b3e52114 100644 --- a/lib/frontend/src/frontend.rs +++ b/lib/frontend/src/frontend.rs @@ -608,7 +608,7 @@ impl<'a> FunctionBuilder<'a> { "`size` is not a power of two" ); assert!( - access_size >= u64::from(::std::cmp::min(src_align, dest_align)), + access_size >= u64::from(::core::cmp::min(src_align, dest_align)), "`size` is smaller than `dest` and `src`'s alignment value." ); @@ -777,7 +777,7 @@ impl<'a> FunctionBuilder<'a> { "`size` is not a power of two" ); assert!( - access_size >= u64::from(::std::cmp::min(src_align, dest_align)), + access_size >= u64::from(::core::cmp::min(src_align, dest_align)), "`size` is smaller than `dest` and `src`'s alignment value." ); let load_and_store_amount = size / access_size; @@ -962,8 +962,8 @@ mod tests { #[test] fn memcpy() { + use core::str::FromStr; use cranelift_codegen::{isa, settings}; - use std::str::FromStr; let shared_builder = settings::builder(); let shared_flags = settings::Flags::new(shared_builder); @@ -1023,8 +1023,8 @@ ebb0: #[test] fn small_memcpy() { + use core::str::FromStr; use cranelift_codegen::{isa, settings}; - use std::str::FromStr; let shared_builder = settings::builder(); let shared_flags = settings::Flags::new(shared_builder); diff --git a/lib/frontend/src/lib.rs b/lib/frontend/src/lib.rs index 90c5b5d908..7379503769 100644 --- a/lib/frontend/src/lib.rs +++ b/lib/frontend/src/lib.rs @@ -174,12 +174,20 @@ clippy::use_self ) )] -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![cfg_attr(not(feature = "std"), feature(alloc))] #[cfg(not(feature = "std"))] #[macro_use] -extern crate alloc; +extern crate alloc as std; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; + +#[cfg(not(feature = "std"))] +use hashmap_core::HashMap; +#[cfg(feature = "std")] +use std::collections::HashMap; pub use crate::frontend::{FunctionBuilder, FunctionBuilderContext}; pub use crate::switch::Switch; @@ -190,19 +198,5 @@ mod ssa; mod switch; mod variable; -/// This replaces `std` in builds with `core`. -#[cfg(not(feature = "std"))] -mod std { - pub use alloc::{string, vec}; - pub use core::*; - pub mod collections { - #[allow(unused_extern_crates)] - extern crate hashmap_core; - - pub use self::hashmap_core::map as hash_map; - pub use self::hashmap_core::{HashMap, HashSet}; - } -} - /// Version number of this crate. pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/lib/frontend/src/ssa.rs b/lib/frontend/src/ssa.rs index 1046e960a2..0d48c314af 100644 --- a/lib/frontend/src/ssa.rs +++ b/lib/frontend/src/ssa.rs @@ -6,6 +6,8 @@ //! Lecture Notes in Computer Science, vol 7791. Springer, Berlin, Heidelberg use crate::Variable; +use core::mem; +use core::u32; use cranelift_codegen::cursor::{Cursor, FuncCursor}; use cranelift_codegen::entity::{EntityRef, PrimaryMap, SecondaryMap}; use cranelift_codegen::ir::immediates::{Ieee32, Ieee64}; @@ -14,8 +16,6 @@ use cranelift_codegen::ir::types::{F32, F64}; use cranelift_codegen::ir::{Ebb, Function, Inst, InstBuilder, InstructionData, Type, Value}; use cranelift_codegen::packed_option::PackedOption; use cranelift_codegen::packed_option::ReservedValue; -use std::mem; -use std::u32; use std::vec::Vec; /// Structure containing the data relevant the construction of SSA for a given function. diff --git a/lib/frontend/src/switch.rs b/lib/frontend/src/switch.rs index 5ecb4edc38..653afbaed1 100644 --- a/lib/frontend/src/switch.rs +++ b/lib/frontend/src/switch.rs @@ -1,8 +1,8 @@ +use super::HashMap; use crate::frontend::FunctionBuilder; use cranelift_codegen::ir::condcodes::IntCC; use cranelift_codegen::ir::*; use log::debug; -use std::collections::HashMap; use std::vec::Vec; type EntryIndex = u64; @@ -334,7 +334,7 @@ ebb10: #[test] fn switch_min_index_value() { - let func = setup!(0, [::std::i64::MIN as u64, 1,]); + let func = setup!(0, [::core::i64::MIN as u64, 1,]); assert_eq!( func, "ebb0: @@ -350,7 +350,7 @@ ebb10: #[test] fn switch_max_index_value() { - let func = setup!(0, [::std::i64::MAX as u64, 1,]); + let func = setup!(0, [::core::i64::MAX as u64, 1,]); assert_eq!( func, "ebb0: diff --git a/lib/frontend/src/variable.rs b/lib/frontend/src/variable.rs index ad60006dd7..dddcd7490b 100644 --- a/lib/frontend/src/variable.rs +++ b/lib/frontend/src/variable.rs @@ -5,8 +5,8 @@ //! their own index types to use them directly. Frontends which don't //! can use the `Variable` defined here. +use core::u32; use cranelift_codegen::entity::EntityRef; -use std::u32; ///! An opaque reference to a variable. #[derive(Copy, Clone, PartialEq, Eq, Debug)] diff --git a/lib/module/Cargo.toml b/lib/module/Cargo.toml index 9a564c8b31..dbd9f95335 100644 --- a/lib/module/Cargo.toml +++ b/lib/module/Cargo.toml @@ -14,7 +14,7 @@ edition = "2018" cranelift-codegen = { path = "../codegen", version = "0.26.0", default-features = false } cranelift-entity = { path = "../entity", version = "0.26.0", default-features = false } hashmap_core = { version = "0.1.9", optional = true } -failure = "0.1.1" +failure = { version = "0.1.1", default-features = false } log = { version = "0.4.6", default-features = false } [features] diff --git a/lib/module/src/backend.rs b/lib/module/src/backend.rs index 37e235edb7..956c206948 100644 --- a/lib/module/src/backend.rs +++ b/lib/module/src/backend.rs @@ -4,10 +4,10 @@ use crate::DataContext; use crate::Linkage; use crate::ModuleNamespace; use crate::ModuleResult; +use core::marker; use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::Context; use cranelift_codegen::{binemit, ir}; -use std::marker; /// A `Backend` implements the functionality needed to support a `Module`. /// diff --git a/lib/module/src/data_context.rs b/lib/module/src/data_context.rs index d941e9c411..335dd9a18d 100644 --- a/lib/module/src/data_context.rs +++ b/lib/module/src/data_context.rs @@ -61,8 +61,8 @@ impl DataContext { init: Init::Uninitialized, function_decls: PrimaryMap::new(), data_decls: PrimaryMap::new(), - function_relocs: Vec::new(), - data_relocs: Vec::new(), + function_relocs: vec![], + data_relocs: vec![], }, } } diff --git a/lib/module/src/lib.rs b/lib/module/src/lib.rs index db9b793369..ce6f00aff0 100644 --- a/lib/module/src/lib.rs +++ b/lib/module/src/lib.rs @@ -18,13 +18,20 @@ clippy::use_self ) )] -// Turns on no_std and alloc features if std is not available. -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![cfg_attr(not(feature = "std"), feature(alloc))] #[cfg(not(feature = "std"))] -#[cfg_attr(test, macro_use)] -extern crate alloc; +#[macro_use] +extern crate alloc as std; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; + +#[cfg(not(feature = "std"))] +use hashmap_core::{map as hash_map, HashMap}; +#[cfg(feature = "std")] +use std::collections::{hash_map, HashMap}; mod backend; mod data_context; @@ -36,19 +43,5 @@ pub use crate::module::{ DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleError, ModuleNamespace, ModuleResult, }; -/// This replaces `std` in builds with `core`. -#[cfg(not(feature = "std"))] -mod std { - pub use alloc::{borrow, boxed, string, vec}; - pub use core::*; - pub mod collections { - #[allow(unused_extern_crates)] - extern crate hashmap_core; - - pub use self::hashmap_core::map as hash_map; - pub use self::hashmap_core::{HashMap, HashSet}; - } -} - /// Version number of this crate. pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/lib/module/src/module.rs b/lib/module/src/module.rs index 1d5a33130a..0fec7d39ce 100644 --- a/lib/module/src/module.rs +++ b/lib/module/src/module.rs @@ -5,6 +5,7 @@ // TODO: Factor out `ir::Function`'s `ext_funcs` and `global_values` into a struct // shared with `DataContext`? +use super::HashMap; use crate::data_context::DataContext; use crate::Backend; use cranelift_codegen::entity::{entity_impl, PrimaryMap}; @@ -12,7 +13,6 @@ use cranelift_codegen::{binemit, ir, isa, CodegenError, Context}; use failure::Fail; use log::info; use std::borrow::ToOwned; -use std::collections::HashMap; use std::string::String; use std::vec::Vec; @@ -398,7 +398,7 @@ where signature: &ir::Signature, ) -> ModuleResult { // TODO: Can we avoid allocating names so often? - use std::collections::hash_map::Entry::*; + use super::hash_map::Entry::*; match self.names.entry(name.to_owned()) { Occupied(entry) => match *entry.get() { FuncOrDataId::Func(id) => { @@ -435,7 +435,7 @@ where writable: bool, ) -> ModuleResult { // TODO: Can we avoid allocating names so often? - use std::collections::hash_map::Entry::*; + use super::hash_map::Entry::*; match self.names.entry(name.to_owned()) { Occupied(entry) => match *entry.get() { FuncOrDataId::Data(id) => { diff --git a/lib/native/src/lib.rs b/lib/native/src/lib.rs index 4322512ec3..010acb25ac 100644 --- a/lib/native/src/lib.rs +++ b/lib/native/src/lib.rs @@ -23,7 +23,7 @@ clippy::use_self ) )] -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] use cranelift_codegen::isa; use cranelift_codegen::settings::Configurable; diff --git a/lib/preopt/src/constant_folding.rs b/lib/preopt/src/constant_folding.rs index 1fb5d116eb..2d7c1c2dbb 100644 --- a/lib/preopt/src/constant_folding.rs +++ b/lib/preopt/src/constant_folding.rs @@ -102,7 +102,7 @@ fn resolve_value_to_imm(dfg: &ir::DataFlowGraph, value: ir::Value) -> Option Option { - use std::num::Wrapping; + use core::num::Wrapping; match opcode { ir::Opcode::Iadd => { diff --git a/lib/preopt/src/lib.rs b/lib/preopt/src/lib.rs index b93e914cb2..310d1ba614 100644 --- a/lib/preopt/src/lib.rs +++ b/lib/preopt/src/lib.rs @@ -18,11 +18,15 @@ clippy::use_self ) )] -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![cfg_attr(not(feature = "std"), feature(alloc))] #[cfg(not(feature = "std"))] -extern crate alloc; +#[macro_use] +extern crate alloc as std; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; mod constant_folding; @@ -49,10 +53,3 @@ where ctx.verify_if(fisa)?; Ok(()) } - -/// This replaces `std` in builds with `core`. -#[cfg(not(feature = "std"))] -mod std { - pub use alloc::{boxed, slice, string, vec}; - pub use core::*; -} diff --git a/lib/serde/Cargo.toml b/lib/serde/Cargo.toml index a8a34e64b2..24aa976671 100644 --- a/lib/serde/Cargo.toml +++ b/lib/serde/Cargo.toml @@ -5,7 +5,6 @@ authors = ["The Cranelift Project Developers"] description = "Serializer/Deserializer for Cranelift IR" repository = "https://github.com/CraneStation/cranelift" license = "Apache-2.0 WITH LLVM-exception" -categories = ["no-std"] readme = "README.md" keywords = ["webassembly", "serde"] edition = "2018" @@ -19,13 +18,8 @@ clap = "2.32.0" serde = "1.0.8" serde_derive = "1.0.75" serde_json = "1.0.26" -cranelift-codegen = { path = "../codegen", version = "0.26.0", default-features = false } -cranelift-reader = { path = "../reader", version = "0.26.0", default-features = false } - -[features] -default = ["std"] -std = ["cranelift-codegen/std"] -core = ["cranelift-codegen/core"] +cranelift-codegen = { path = "../codegen", version = "0.26.0" } +cranelift-reader = { path = "../reader", version = "0.26.0" } [badges] maintenance = { status = "experimental" } diff --git a/lib/simplejit/Cargo.toml b/lib/simplejit/Cargo.toml index 440c4072f1..9fb1d3abd4 100644 --- a/lib/simplejit/Cargo.toml +++ b/lib/simplejit/Cargo.toml @@ -5,19 +5,18 @@ authors = ["The Cranelift Project Developers"] description = "A simple JIT library backed by Cranelift" repository = "https://github.com/CraneStation/cranelift" documentation = "https://cranelift.readthedocs.io/" -categories = ["no-std"] license = "Apache-2.0 WITH LLVM-exception" readme = "README.md" edition = "2018" [dependencies] -cranelift-codegen = { path = "../codegen", version = "0.26.0", default-features = false } -cranelift-module = { path = "../module", version = "0.26.0", default-features = false } -cranelift-native = { path = "../native", version = "0.26.0", default-features = false } +cranelift-codegen = { path = "../codegen", version = "0.26.0" } +cranelift-module = { path = "../module", version = "0.26.0" } +cranelift-native = { path = "../native", version = "0.26.0" } region = "1.0.0" -libc = { version = "0.2.42", default-features = false } +libc = { version = "0.2.42" } errno = "0.2.4" -target-lexicon = { version = "0.2.0", default-features = false } +target-lexicon = { version = "0.2.0" } [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3", features = ["winbase", "memoryapi"] } @@ -27,11 +26,6 @@ cranelift = { path = "../umbrella", version = "0.26.0" } cranelift-frontend = { path = "../frontend", version = "0.26.0" } cranelift-entity = { path = "../entity", version = "0.26.0" } -[features] -default = ["std"] -std = ["libc/use_std", "cranelift-codegen/std", "cranelift-module/std", "cranelift-native/std", "target-lexicon/std"] -core = ["cranelift-codegen/core", "cranelift-module/core", "cranelift-native/core"] - [badges] maintenance = { status = "experimental" } travis-ci = { repository = "CraneStation/cranelift" } diff --git a/lib/umbrella/src/lib.rs b/lib/umbrella/src/lib.rs index 6db0fab1f8..f97ed443ce 100644 --- a/lib/umbrella/src/lib.rs +++ b/lib/umbrella/src/lib.rs @@ -22,6 +22,7 @@ clippy::use_self ) )] +#![no_std] /// Provide these crates, renamed to reduce stutter. pub use cranelift_codegen as codegen; diff --git a/lib/wasm/Cargo.toml b/lib/wasm/Cargo.toml index 45c2419bf7..88aae1cecf 100644 --- a/lib/wasm/Cargo.toml +++ b/lib/wasm/Cargo.toml @@ -19,7 +19,7 @@ hashmap_core = { version = "0.1.9", optional = true } failure = { version = "0.1.1", default-features = false, features = ["derive"] } failure_derive = { version = "0.1.1", default-features = false } log = { version = "0.4.6", default-features = false } -cast = { version = "0.2.2" } +cast = { version = "0.2.2", default-features = false } [dev-dependencies] wabt = "0.7.0" diff --git a/lib/wasm/src/code_translator.rs b/lib/wasm/src/code_translator.rs index 43b4af8f9e..ad0d75e2da 100644 --- a/lib/wasm/src/code_translator.rs +++ b/lib/wasm/src/code_translator.rs @@ -22,18 +22,17 @@ //! //! That is why `translate_function_body` takes an object having the `WasmRuntime` trait as //! argument. +use super::{hash_map, HashMap}; use crate::environ::{FuncEnvironment, GlobalVariable, ReturnMode, WasmError, WasmResult}; use crate::state::{ControlStackFrame, TranslationState}; use crate::translation_utils::{f32_translation, f64_translation, num_return_values, type_to_type}; use crate::translation_utils::{FuncIndex, MemoryIndex, SignatureIndex, TableIndex}; +use core::{i32, u32}; use cranelift_codegen::ir::condcodes::{FloatCC, IntCC}; use cranelift_codegen::ir::types::*; use cranelift_codegen::ir::{self, InstBuilder, JumpTableData, MemFlags}; use cranelift_codegen::packed_option::ReservedValue; use cranelift_frontend::{FunctionBuilder, Variable}; -use std::collections::{hash_map, HashMap}; -use std::vec::Vec; -use std::{i32, u32}; use wasmparser::{MemoryImmediate, Operator}; // Clippy warns about "flags: _" but its important to document that the flags field is ignored @@ -288,7 +287,7 @@ pub fn translate_operator( // Here we have jump arguments, but Cranelift's br_table doesn't support them // We then proceed to split the edges going out of the br_table let return_count = jump_args_count; - let mut dest_ebb_sequence = Vec::new(); + let mut dest_ebb_sequence = vec![]; let mut dest_ebb_map = HashMap::new(); for depth in &*depths { let branch_ebb = match dest_ebb_map.entry(*depth as usize) { @@ -984,7 +983,7 @@ fn get_heap_addr( addr_ty: Type, builder: &mut FunctionBuilder, ) -> (ir::Value, i32) { - use std::cmp::min; + use core::cmp::min; let mut adjusted_offset = u64::from(offset); let offset_guard_size: u64 = builder.func.heaps[heap].offset_guard_size.into(); diff --git a/lib/wasm/src/environ/dummy.rs b/lib/wasm/src/environ/dummy.rs index b52b5bfc1d..28cea89e77 100644 --- a/lib/wasm/src/environ/dummy.rs +++ b/lib/wasm/src/environ/dummy.rs @@ -18,6 +18,7 @@ use cranelift_codegen::ir::types::*; use cranelift_codegen::ir::{self, InstBuilder}; use cranelift_codegen::isa::TargetFrontendConfig; use cranelift_entity::{EntityRef, PrimaryMap}; +use std::boxed::Box; use std::string::String; use std::vec::Vec; diff --git a/lib/wasm/src/environ/spec.rs b/lib/wasm/src/environ/spec.rs index f79152f5be..50ecca02c6 100644 --- a/lib/wasm/src/environ/spec.rs +++ b/lib/wasm/src/environ/spec.rs @@ -9,13 +9,13 @@ use crate::translation_utils::{ FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex, }; +use core::convert::From; use cranelift_codegen::cursor::FuncCursor; use cranelift_codegen::ir::immediates::Offset32; use cranelift_codegen::ir::{self, InstBuilder}; use cranelift_codegen::isa::TargetFrontendConfig; use failure_derive::Fail; use std::boxed::Box; -use std::convert::From; use wasmparser::BinaryReaderError; /// The value of a WebAssembly global variable. diff --git a/lib/wasm/src/lib.rs b/lib/wasm/src/lib.rs index 4bfaf93012..edd5c3215f 100644 --- a/lib/wasm/src/lib.rs +++ b/lib/wasm/src/lib.rs @@ -27,9 +27,21 @@ clippy::use_self ) )] -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] #![cfg_attr(not(feature = "std"), feature(alloc))] +#[cfg(not(feature = "std"))] +#[macro_use] +extern crate alloc as std; +#[cfg(feature = "std")] +#[macro_use] +extern crate std; + +#[cfg(not(feature = "std"))] +use hashmap_core::{map as hash_map, HashMap}; +#[cfg(feature = "std")] +use std::collections::{hash_map, HashMap}; + mod code_translator; mod environ; mod func_translator; @@ -50,23 +62,5 @@ pub use crate::translation_utils::{ TableIndex, }; -#[cfg(not(feature = "std"))] -mod std { - extern crate alloc; - - pub use self::alloc::string; - pub use self::alloc::vec; - pub use core::convert; - pub use core::fmt; - pub use core::option; - pub use core::{cmp, i32, str, u32}; - pub mod collections { - #[allow(unused_extern_crates)] - extern crate hashmap_core; - - pub use self::hashmap_core::{map as hash_map, HashMap}; - } -} - /// Version number of this crate. pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/lib/wasm/src/sections_translator.rs b/lib/wasm/src/sections_translator.rs index 0a3aff5803..2c40010169 100644 --- a/lib/wasm/src/sections_translator.rs +++ b/lib/wasm/src/sections_translator.rs @@ -12,9 +12,9 @@ use crate::translation_utils::{ type_to_type, FuncIndex, Global, GlobalIndex, GlobalInit, Memory, MemoryIndex, SignatureIndex, Table, TableElementType, TableIndex, }; +use core::str::from_utf8; use cranelift_codegen::ir::{self, AbiParam, Signature}; use cranelift_entity::EntityRef; -use std::str::from_utf8; use std::vec::Vec; use wasmparser::{ self, CodeSectionReader, Data, DataSectionReader, Element, ElementSectionReader, Export, diff --git a/lib/wasm/src/state.rs b/lib/wasm/src/state.rs index 80d1f5fe23..f392631237 100644 --- a/lib/wasm/src/state.rs +++ b/lib/wasm/src/state.rs @@ -3,10 +3,10 @@ //! The `TranslationState` struct defined in this module is used to keep track of the WebAssembly //! value and control stacks during the translation of a single function. +use super::HashMap; use crate::environ::{FuncEnvironment, GlobalVariable}; use crate::translation_utils::{FuncIndex, GlobalIndex, MemoryIndex, SignatureIndex, TableIndex}; use cranelift_codegen::ir::{self, Ebb, Inst, Value}; -use std::collections::HashMap; use std::vec::Vec; /// A control stack frame can be an `if`, a `block` or a `loop`, each one having the following diff --git a/lib/wasm/src/translation_utils.rs b/lib/wasm/src/translation_utils.rs index cdfe276d46..7ea730c9cb 100644 --- a/lib/wasm/src/translation_utils.rs +++ b/lib/wasm/src/translation_utils.rs @@ -1,7 +1,7 @@ //! Helper functions and structures for the translation. +use core::u32; use cranelift_codegen::entity::entity_impl; use cranelift_codegen::ir; -use std::u32; use wasmparser; /// Index type of a function (imported or defined) inside the WebAssembly module.