From 66a150e67acded290bd7259ca7cc5113e428f5c0 Mon Sep 17 00:00:00 2001 From: Lachlan Sneff Date: Mon, 15 Jan 2018 16:21:42 -0500 Subject: [PATCH] lib/wasm works with no_std --- lib/wasm/Cargo.toml | 12 ++++++++++++ lib/wasm/src/code_translator.rs | 3 +++ lib/wasm/src/environ/dummy.rs | 4 ++++ lib/wasm/src/environ/spec.rs | 4 ++++ lib/wasm/src/lib.rs | 25 +++++++++++++++++++++++++ lib/wasm/src/module_translator.rs | 3 +++ lib/wasm/src/sections_translator.rs | 4 ++++ lib/wasm/src/state.rs | 3 +++ 8 files changed, 58 insertions(+) diff --git a/lib/wasm/Cargo.toml b/lib/wasm/Cargo.toml index 2bad5f8d2a..448166d568 100644 --- a/lib/wasm/Cargo.toml +++ b/lib/wasm/Cargo.toml @@ -15,5 +15,17 @@ wasmparser = "0.14.1" cretonne = { path = "../cretonne", version = "0.1.0" } cretonne-frontend = { path = "../frontend", version = "0.1.0" } +[dependencies.hashmap_core] +version = "0.1.1" +optional = true +[dependencies.error_core] +version = "0.1.0" +optional = true + [dev-dependencies] tempdir = "0.3.5" + +[features] +# Currently, the only feature is the `no_std` feature. +# Enabling this disables use of `stdlib`. +no_std = ["hashmap_core", "error_core", "cretonne/no_std", "cretonne-frontend/no_std"] \ No newline at end of file diff --git a/lib/wasm/src/code_translator.rs b/lib/wasm/src/code_translator.rs index 903b9cd9e1..ea33f311a9 100644 --- a/lib/wasm/src/code_translator.rs +++ b/lib/wasm/src/code_translator.rs @@ -34,6 +34,9 @@ use std::collections::{HashMap, hash_map}; use environ::{FuncEnvironment, GlobalValue}; use std::{i32, u32}; +// this is for no_std builds, but has no affect on regular builds +use std::vec::Vec; + /// Translates wasm operators into Cretonne IL instructions. Returns `true` if it inserted /// a return. pub fn translate_operator( diff --git a/lib/wasm/src/environ/dummy.rs b/lib/wasm/src/environ/dummy.rs index 5557d842f6..9238cbf665 100644 --- a/lib/wasm/src/environ/dummy.rs +++ b/lib/wasm/src/environ/dummy.rs @@ -11,6 +11,10 @@ use cretonne::settings; use wasmparser; use std::error::Error; +// this is for no_std builds, but has no affect on regular builds +use std::vec::Vec; +use std::string::String; + /// Compute a `ir::ExternalName` for a given wasm function index. fn get_func_name(func_index: FunctionIndex) -> ir::ExternalName { ir::ExternalName::user(0, func_index as u32) diff --git a/lib/wasm/src/environ/spec.rs b/lib/wasm/src/environ/spec.rs index 31e4782918..c8a8f54111 100644 --- a/lib/wasm/src/environ/spec.rs +++ b/lib/wasm/src/environ/spec.rs @@ -6,6 +6,10 @@ use cretonne::settings::Flags; use translation_utils::{SignatureIndex, FunctionIndex, TableIndex, GlobalIndex, MemoryIndex, Global, Table, Memory}; +// this is for no_std builds, but has no affect on regular builds +use std::vec::Vec; +use std::string::String; + /// The value of a WebAssembly global variable. #[derive(Clone, Copy)] pub enum GlobalValue { diff --git a/lib/wasm/src/lib.rs b/lib/wasm/src/lib.rs index b5b79049fe..dde500af97 100644 --- a/lib/wasm/src/lib.rs +++ b/lib/wasm/src/lib.rs @@ -9,8 +9,20 @@ //! //! The main function of this module is [`translate_module`](fn.translate_module.html). +#![cfg_attr(feature = "no_std", no_std)] #![deny(missing_docs)] +#![cfg_attr(feature = "no_std", feature(alloc))] + +#[cfg(feature = "no_std")] +#[macro_use] +extern crate alloc; + +#[cfg(feature = "no_std")] +extern crate hashmap_core; +#[cfg(feature = "no_std")] +extern crate error_core; + extern crate wasmparser; extern crate cton_frontend; #[macro_use(dbg)] @@ -29,3 +41,16 @@ pub use module_translator::translate_module; pub use environ::{FuncEnvironment, ModuleEnvironment, DummyEnvironment, GlobalValue}; pub use translation_utils::{FunctionIndex, GlobalIndex, TableIndex, MemoryIndex, SignatureIndex, Global, GlobalInit, Table, Memory}; + +#[cfg(feature = "no_std")] +mod std { + pub use alloc::vec; + pub use alloc::string; + pub use core::{u32, i32, str, cmp}; + pub mod collections { + pub use hashmap_core::{HashMap, map as hash_map}; + } + pub mod error { + pub use error_core::Error; + } +} \ No newline at end of file diff --git a/lib/wasm/src/module_translator.rs b/lib/wasm/src/module_translator.rs index d75f60a35e..2786f9094e 100644 --- a/lib/wasm/src/module_translator.rs +++ b/lib/wasm/src/module_translator.rs @@ -8,6 +8,9 @@ use sections_translator::{SectionParsingError, parse_function_signatures, parse_ parse_elements_section, parse_data_section}; use environ::ModuleEnvironment; +// this is for no_std builds, but has no affect on regular builds +use std::string::String; + /// Translate a sequence of bytes forming a valid Wasm binary into a list of valid Cretonne IL /// [`Function`](../cretonne/ir/function/struct.Function.html). /// Returns the functions and also the mappings for imported functions and signature between the diff --git a/lib/wasm/src/sections_translator.rs b/lib/wasm/src/sections_translator.rs index d02a4a7c2a..ff641a6889 100644 --- a/lib/wasm/src/sections_translator.rs +++ b/lib/wasm/src/sections_translator.rs @@ -17,6 +17,10 @@ use wasmparser; use std::str::from_utf8; use environ::ModuleEnvironment; +// this is for no_std builds, but has no affect on regular builds +use std::vec::Vec; +use std::string::String; + pub enum SectionParsingError { WrongSectionContent(String), } diff --git a/lib/wasm/src/state.rs b/lib/wasm/src/state.rs index f9f76d2934..625e39b4e9 100644 --- a/lib/wasm/src/state.rs +++ b/lib/wasm/src/state.rs @@ -8,6 +8,9 @@ use environ::{FuncEnvironment, GlobalValue}; use std::collections::HashMap; use translation_utils::{GlobalIndex, MemoryIndex, SignatureIndex, FunctionIndex}; +// this is for no_std builds, but has no affect on regular builds +use std::vec::Vec; + /// A control stack frame can be an `if`, a `block` or a `loop`, each one having the following /// fields: ///