Skip to content

Commit

Permalink
Derived Clone and Copy for DataKind. Added locals to LocalFunction.
Browse files Browse the repository at this point in the history
  • Loading branch information
lbrande committed Mar 16, 2022
1 parent 9d6c9de commit 520ad45
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/function_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ impl FunctionBuilder {
/// let function_id = builder.finish(vec![], &mut module.funcs);
/// # let _ = function_id;
/// ```
pub fn finish(self, args: Vec<LocalId>, funcs: &mut ModuleFunctions) -> FunctionId {
let func = LocalFunction::new(args, self);
pub fn finish(self, args: Vec<LocalId>, locals: Vec<LocalId>, funcs: &mut ModuleFunctions) -> FunctionId {
let func = LocalFunction::new(args, locals, self);
funcs.add_local(func)
}

/// Returns the [crate::LocalFunction] built by this builder.
pub fn local_func(self, args: Vec<LocalId>) -> LocalFunction {
LocalFunction::new(args, self)
pub fn local_func(self, args: Vec<LocalId>, locals: Vec<LocalId>) -> LocalFunction {
LocalFunction::new(args, locals, self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/module/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Data {
}

/// The kind of data segment: passive or active.
#[derive(Debug)]
#[derive(Clone, Copy, Debug)]
pub enum DataKind {
/// An active data segment that is automatically initialized at some address
/// in a static memory.
Expand All @@ -41,7 +41,7 @@ pub enum DataKind {
}

/// The parts of a data segment that are only present in active data segments.
#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug)]
pub struct ActiveData {
/// The memory that this active data segment will be automatically
/// initialized in.
Expand Down
9 changes: 7 additions & 2 deletions src/module/functions/local_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ pub struct LocalFunction {

/// Arguments to this function, and the locals that they're assigned to.
pub args: Vec<LocalId>,

/// Locals of this function, excluding arguments.
pub locals: Vec<LocalId>,
//
// TODO: provenance: (InstrSeqId, usize) -> offset in code section of the
// original instruction. This will be necessary for preserving debug info.
}

impl LocalFunction {
/// Creates a new definition of a local function from its components.
pub(crate) fn new(args: Vec<LocalId>, builder: FunctionBuilder) -> LocalFunction {
LocalFunction { args, builder }
pub(crate) fn new(args: Vec<LocalId>, locals: Vec<LocalId>, builder: FunctionBuilder) -> LocalFunction {
LocalFunction { args, locals, builder }
}

/// Construct a new `LocalFunction`.
Expand All @@ -42,13 +45,15 @@ impl LocalFunction {
id: FunctionId,
ty: TypeId,
args: Vec<LocalId>,
locals: Vec<LocalId>,
mut body: wasmparser::BinaryReader<'_>,
on_instr_pos: Option<&(dyn Fn(&usize) -> InstrLocId + Sync + Send + 'static)>,
mut validator: FuncValidator<ValidatorResources>,
) -> Result<LocalFunction> {
let mut func = LocalFunction {
builder: FunctionBuilder::without_entry(ty),
args,
locals,
};

let result: Vec<_> = module.types.get(ty).results().iter().cloned().collect();
Expand Down
7 changes: 5 additions & 2 deletions src/module/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ impl Module {

// Next up comes all the locals of the function.
let mut reader = body.get_binary_reader();
let mut locals = Vec::new();
for _ in 0..reader.read_var_u32()? {
let pos = reader.original_position();
let count = reader.read_var_u32()?;
Expand All @@ -375,20 +376,21 @@ impl Module {
for _ in 0..count {
let local_id = self.locals.add(ty);
let idx = indices.push_local(id, local_id);
locals.push(local_id);
if self.config.generate_synthetic_names_for_anonymous_items {
let name = format!("l{}", idx);
self.locals.get_mut(local_id).name = Some(name);
}
}
}

bodies.push((id, reader, args, ty, validator));
bodies.push((id, reader, args, locals, ty, validator));
}

// Wasm modules can often have a lot of functions and this operation can
// take some time, so parse all function bodies in parallel.
let results = maybe_parallel!(bodies.(into_iter | into_par_iter))
.map(|(id, body, args, ty, validator)| {
.map(|(id, body, args, locals, ty, validator)| {
(
id,
LocalFunction::parse(
Expand All @@ -397,6 +399,7 @@ impl Module {
id,
ty,
args,
locals,
body,
on_instr_pos,
validator,
Expand Down

0 comments on commit 520ad45

Please sign in to comment.