mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-06-30 23:11:41 +00:00
Port core util lib to support no_std
This commit is contained in:
@ -1,3 +1,7 @@
|
||||
use std::string::String;
|
||||
use std::vec::Vec;
|
||||
use std::borrow::ToOwned;
|
||||
|
||||
use parity_wasm::{elements, builder};
|
||||
use optimizer::{import_section, export_section};
|
||||
use byteorder::{LittleEndian, ByteOrder};
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::vec::Vec;
|
||||
|
||||
use parity_wasm::{elements, builder};
|
||||
use rules;
|
||||
|
||||
|
17
src/lib.rs
17
src/lib.rs
@ -1,3 +1,10 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(not(feature = "std"), feature(alloc))]
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
#[macro_use]
|
||||
extern crate alloc;
|
||||
|
||||
extern crate parity_wasm;
|
||||
extern crate byteorder;
|
||||
#[macro_use] extern crate log;
|
||||
@ -22,3 +29,13 @@ pub use gas::inject_gas_counter;
|
||||
pub use ext::{externalize, externalize_mem, underscore_funcs, ununderscore_funcs, shrink_unknown_stack};
|
||||
pub use pack::{pack_instance, Error as PackingError};
|
||||
pub use runtime_type::inject_runtime_type;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
mod std {
|
||||
pub use core::*;
|
||||
pub use alloc::{vec, string, boxed, borrow};
|
||||
|
||||
pub mod collections {
|
||||
pub use alloc::{BTreeMap, BTreeSet};
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,9 @@
|
||||
use std::collections::HashSet;
|
||||
#[cfg(features = "std")]
|
||||
use std::collections::{HashSet as Set};
|
||||
#[cfg(not(features = "std"))]
|
||||
use std::collections::{BTreeSet as Set};
|
||||
use std::vec::Vec;
|
||||
|
||||
use parity_wasm::elements;
|
||||
|
||||
use symbols::{Symbol, expand_symbols, push_code_symbols, resolve_function};
|
||||
@ -19,7 +24,7 @@ pub fn optimize(
|
||||
// which in turn compile in unused imports and leaves unused functions
|
||||
|
||||
// Algo starts from the top, listing all items that should stay
|
||||
let mut stay = HashSet::new();
|
||||
let mut stay = Set::new();
|
||||
for (index, entry) in module.export_section().ok_or(Error::NoExportSection)?.entries().iter().enumerate() {
|
||||
if used_exports.iter().find(|e| **e == entry.field()).is_some() {
|
||||
stay.insert(Symbol::Export(index));
|
||||
|
@ -1,4 +1,7 @@
|
||||
use std::fmt;
|
||||
use std::vec::Vec;
|
||||
use std::borrow::ToOwned;
|
||||
|
||||
use parity_wasm::elements::{
|
||||
self, Section, DataSection, Opcode, DataSegment, InitExpr, Internal, External,
|
||||
ImportCountType,
|
||||
|
14
src/rules.rs
14
src/rules.rs
@ -1,4 +1,8 @@
|
||||
use std::collections::HashMap;
|
||||
#[cfg(features = "std")]
|
||||
use std::collections::{HashMap as Map};
|
||||
#[cfg(not(features = "std"))]
|
||||
use std::collections::{BTreeMap as Map};
|
||||
|
||||
use parity_wasm::elements;
|
||||
|
||||
pub struct UnknownInstruction;
|
||||
@ -10,7 +14,7 @@ pub enum Metering {
|
||||
Fixed(u32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)]
|
||||
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
||||
pub enum InstructionType {
|
||||
Bit,
|
||||
Add,
|
||||
@ -265,7 +269,7 @@ impl InstructionType {
|
||||
#[derive(Debug)]
|
||||
pub struct Set {
|
||||
regular: u32,
|
||||
entries: HashMap<InstructionType, Metering>,
|
||||
entries: Map<InstructionType, Metering>,
|
||||
grow: u32,
|
||||
}
|
||||
|
||||
@ -273,14 +277,14 @@ impl Default for Set {
|
||||
fn default() -> Self {
|
||||
Set {
|
||||
regular: 1,
|
||||
entries: HashMap::new(),
|
||||
entries: Map::new(),
|
||||
grow: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Set {
|
||||
pub fn new(regular: u32, entries: HashMap<InstructionType, Metering>) -> Self {
|
||||
pub fn new(regular: u32, entries: Map<InstructionType, Metering>) -> Self {
|
||||
Set { regular: regular, entries: entries, grow: 0 }
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::vec::Vec;
|
||||
|
||||
use parity_wasm::elements::{self, BlockType, Type};
|
||||
use super::{resolve_func_type, Error};
|
||||
|
||||
|
@ -48,6 +48,9 @@
|
||||
//! between the frames.
|
||||
//! - upon entry into the function entire stack frame is allocated.
|
||||
|
||||
use std::string::String;
|
||||
use std::vec::Vec;
|
||||
|
||||
use parity_wasm::elements::{self, Type};
|
||||
use parity_wasm::builder;
|
||||
|
||||
|
@ -1,8 +1,12 @@
|
||||
#[cfg(features = "std")]
|
||||
use std::collections::{HashMap as Map};
|
||||
#[cfg(not(features = "std"))]
|
||||
use std::collections::{BTreeMap as Map};
|
||||
use std::vec::Vec;
|
||||
|
||||
use parity_wasm::elements::{self, FunctionType, Internal};
|
||||
use parity_wasm::builder;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::{resolve_func_type, Context, Error};
|
||||
|
||||
struct Thunk {
|
||||
@ -22,7 +26,7 @@ pub(crate) fn generate_thunks(
|
||||
// Function indicies which needs to generate thunks.
|
||||
let mut need_thunks: Vec<u32> = Vec::new();
|
||||
|
||||
let mut replacement_map: HashMap<u32, Thunk> = {
|
||||
let mut replacement_map: Map<u32, Thunk> = {
|
||||
let exports = module
|
||||
.export_section()
|
||||
.map(|es| es.entries())
|
||||
@ -42,7 +46,7 @@ pub(crate) fn generate_thunks(
|
||||
.cloned();
|
||||
|
||||
// Replacement map is at least export section size.
|
||||
let mut replacement_map: HashMap<u32, Thunk> = HashMap::new();
|
||||
let mut replacement_map: Map<u32, Thunk> = Map::new();
|
||||
|
||||
for func_idx in exported_func_indicies.chain(table_func_indicies) {
|
||||
let callee_stack_cost = ctx.stack_cost(func_idx).ok_or_else(|| {
|
||||
|
@ -1,7 +1,12 @@
|
||||
use parity_wasm::elements;
|
||||
use std::collections::HashSet;
|
||||
#[cfg(features = "std")]
|
||||
use std::collections::{HashSet as Set};
|
||||
#[cfg(not(features = "std"))]
|
||||
use std::collections::{BTreeSet as Set};
|
||||
use std::vec::Vec;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
|
||||
use parity_wasm::elements;
|
||||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone, Debug)]
|
||||
pub enum Symbol {
|
||||
Type(usize),
|
||||
Import(usize),
|
||||
@ -67,11 +72,11 @@ pub fn push_code_symbols(module: &elements::Module, opcodes: &[elements::Opcode]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expand_symbols(module: &elements::Module, set: &mut HashSet<Symbol>) {
|
||||
pub fn expand_symbols(module: &elements::Module, set: &mut Set<Symbol>) {
|
||||
use self::Symbol::*;
|
||||
|
||||
// symbols that were already processed
|
||||
let mut stop: HashSet<Symbol> = HashSet::new();
|
||||
let mut stop: Set<Symbol> = Set::new();
|
||||
let mut fringe = set.iter().cloned().collect::<Vec<Symbol>>();
|
||||
loop {
|
||||
let next = match fringe.pop() {
|
||||
|
Reference in New Issue
Block a user