Update no_std support for Rust 2018 Edition.

With Rust 2018 Edition, the `mod std` trick to alias `core` names to
`std` no longer works, so switch to just having the code use `core`
explicitly.

So instead, switch to just using `core::*` for things that in core.
This is more consistent with other Rust no_std code. And it allows
us to enable `no_std` mode unconditionally in the crates that support
it, which makes testing a little easier.

There actually three cases:

 - For things in std and also in core, like `cmp`: Just use them via
   `core::*`.

 - For things in std and also in alloc, like `Vec`: Import alloc as std, as
   use them from std. This allows them to work on both stable (which
   doesn't provide alloc, but we don't support no_std mode anyway) and
   nightly.

 - For HashMap and similar which are not in core or alloc, import them in
   the top-level lib.rs files from either std or the third-party hashmap_core
   crate, and then have the code use super::hashmap_core.

Also, no_std support continues to be "best effort" at this time and not
something most people need to be testing.
This commit is contained in:
Dan Gohman
2019-01-07 11:04:58 -08:00
parent 50a045363c
commit aeb9161e2c
118 changed files with 322 additions and 355 deletions

View File

@@ -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<FE: FuncEnvironment + ?Sized>(
// 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();

View File

@@ -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;

View File

@@ -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.

View File

@@ -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");

View File

@@ -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,

View File

@@ -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

View File

@@ -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.