diff --git a/crates/core/src/ir.rs b/crates/core/src/ir.rs index 10057aa..c7e7f30 100644 --- a/crates/core/src/ir.rs +++ b/crates/core/src/ir.rs @@ -7,6 +7,7 @@ use crate::{ diagnostic::Diagnostics, resolve::SymbolKind, source::Span, + stdlib, types::{Type, TypedModule}, }; @@ -352,6 +353,17 @@ pub enum CallBoundary { HostImport { module: String, name: String }, } +impl CallBoundary { + pub fn stdlib(strategy: stdlib::MemberStrategy, member: &str) -> Self { + match strategy { + stdlib::MemberStrategy::HostImport => { + Self::HostImport { module: stdlib::STDLIB_IO_HOST_MODULE.into(), name: member.into() } + } + _ => Self::Internal, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct Local { pub id: LocalId, @@ -367,6 +379,31 @@ pub struct Block { pub span: Span, } +impl Block { + pub fn contains_indirect_call(&self) -> bool { + self.contains_expression(Expression::contains_indirect_call) + } + + pub fn contains_anonymous_function(&self) -> bool { + self.contains_expression(Expression::contains_anonymous_function) + } + + pub fn contains_constructor(&self) -> bool { + self.contains_expression(Expression::contains_constructor) + } + + pub fn contains_record_update(&self) -> bool { + self.contains_expression(Expression::contains_record_update) + } + + fn contains_expression(&self, predicate: impl Fn(&Expression) -> bool) -> bool { + self.instructions + .iter() + .any(|instruction| predicate(instruction.expression())) + || predicate(&self.result) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum Instruction { Evaluate { @@ -386,6 +423,15 @@ pub enum Instruction { }, } +impl Instruction { + pub fn expression(&self) -> &Expression { + match self { + Self::Evaluate { expression, .. } => expression, + Self::LocalSet { value, .. } | Self::AssertMatch { value, .. } => value, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct FailurePath { pub reason: FailureReason, @@ -408,6 +454,105 @@ pub struct Expression { pub kind: ExpressionKind, } +impl Expression { + pub fn contains_indirect_call(&self) -> bool { + matches!(self.kind, ExpressionKind::IndirectCall(_)) || self.children().any(Self::contains_indirect_call) + } + + pub fn contains_anonymous_function(&self) -> bool { + matches!(self.kind, ExpressionKind::AnonymousFunction(_)) + || self.children().any(Self::contains_anonymous_function) + } + + pub fn contains_constructor(&self) -> bool { + matches!(self.kind, ExpressionKind::Constructor(_)) || self.children().any(Self::contains_constructor) + } + + pub fn contains_record_update(&self) -> bool { + matches!(self.kind, ExpressionKind::RecordUpdate { .. }) || self.children().any(Self::contains_record_update) + } + + pub fn children(&self) -> ExpressionChildren<'_> { + let mut children = Vec::new(); + match &self.kind { + ExpressionKind::DirectCall(call) => children.extend(call.arguments.iter().map(|argument| &argument.value)), + ExpressionKind::IndirectCall(call) => { + children.push(call.callee.as_ref()); + children.extend(call.arguments.iter().map(|argument| &argument.value)); + } + ExpressionKind::Pipeline(pipeline) => { + children.push(pipeline.input.as_ref()); + children.push(pipeline.call.as_ref()); + } + ExpressionKind::Use(use_) => { + children.push(use_.callback.as_ref()); + children.push(use_.call.as_ref()); + } + ExpressionKind::Branch(branch) => { + children.extend(branch.subjects.iter()); + for clause in &branch.clauses { + if let Some(guard) = &clause.guard { + children.push(guard); + } + children.push(clause.body.as_ref()); + } + } + ExpressionKind::Tuple(items) | ExpressionKind::List(items) => children.extend(items.iter()), + ExpressionKind::BitArrayConcat { left, right } + | ExpressionKind::Compare { left, right, .. } + | ExpressionKind::RuntimeEquality { left, right } => { + children.push(left.as_ref()); + children.push(right.as_ref()); + } + ExpressionKind::BitStringDeconstruct { bit_array, .. } + | ExpressionKind::FieldAccess { record: bit_array, .. } + | ExpressionKind::TupleElement { tuple: bit_array, .. } + | ExpressionKind::ListDeconstruct { list: bit_array, .. } => children.push(bit_array.as_ref()), + ExpressionKind::Record(record) => children.extend(record.fields.iter().map(|field| &field.value)), + ExpressionKind::Constructor(constructor) => children.extend(constructor.arguments.iter()), + ExpressionKind::RecordUpdate { record, fields, .. } => { + children.push(record.as_ref()); + children.extend(fields.iter().filter_map(|field| field.value.as_ref())); + } + ExpressionKind::ListCons { head, tail } => { + children.push(head.as_ref()); + children.push(tail.as_ref()); + } + ExpressionKind::Memory(operation) => match operation { + MemoryOperation::Allocate { bytes } | MemoryOperation::Load { address: bytes, .. } => { + children.push(bytes.as_ref()) + } + MemoryOperation::Store { address, value } => { + children.push(address.as_ref()); + children.push(value.as_ref()); + } + }, + ExpressionKind::Literal(_) + | ExpressionKind::LocalGet(_) + | ExpressionKind::FunctionValue(_) + | ExpressionKind::AnonymousFunction(_) + | ExpressionKind::BitArray(_) + | ExpressionKind::Failure(_) => {} + } + ExpressionChildren { children, index: 0 } + } +} + +pub struct ExpressionChildren<'a> { + children: Vec<&'a Expression>, + index: usize, +} + +impl<'a> Iterator for ExpressionChildren<'a> { + type Item = &'a Expression; + + fn next(&mut self) -> Option { + let child = self.children.get(self.index).copied(); + self.index += usize::from(child.is_some()); + child + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum ExpressionKind { Literal(Literal), diff --git a/crates/core/src/ir/lowerer.rs b/crates/core/src/ir/lowerer.rs index 6055fa6..5513c04 100644 --- a/crates/core/src/ir/lowerer.rs +++ b/crates/core/src/ir/lowerer.rs @@ -5,7 +5,7 @@ use crate::ast::{self, Declaration as AstDeclaration, Expression as AstExpressio use crate::diagnostic::{Diagnostic, DiagnosticCode, Diagnostics, Label}; use crate::labels::{FunctionLabelMap, call_argument_order, function_label_map, use_callback_placement}; use crate::resolve::ReferenceTarget; -use crate::stdlib::{MemberStrategy, STDLIB_IO_HOST_MODULE, StdlibRegistry}; +use crate::stdlib::{MemberStrategy, StdlibRegistry}; use crate::types::{ConstructorInfo, TypedModule}; use bit_slices::{ast_bit_array_literal, bit_array_literal, bit_string_pattern_segments}; @@ -136,7 +136,7 @@ impl Lowerer { fn validate_concrete_runtime_types(&mut self) { for function in self.module.functions.clone() { - if type_has_generic(&function.type_) { + if function.type_.has_generic() { self.diagnostics.push( Diagnostic::new( DiagnosticCode::LoweringError, @@ -150,7 +150,7 @@ impl Lowerer { } } for expression in self.module.expressions.clone() { - if type_has_generic(&expression.type_) { + if expression.type_.has_generic() { self.diagnostics.push( Diagnostic::new( DiagnosticCode::LoweringError, @@ -930,7 +930,7 @@ impl Lowerer { arguments: self.lower_ordered_call_arguments(context, call)?, abi: call_abi( &function_type, - stdlib_boundary(stdlib_call.strategy, &stdlib_call.member), + CallBoundary::stdlib(stdlib_call.strategy, &stdlib_call.member), ), }), }); @@ -1146,7 +1146,7 @@ impl Lowerer { } fn lower_stdlib_host_imports(&self, ast: &ast::Module) -> Vec { - let used_host_calls = used_stdlib_host_calls(ast); + let used_host_calls = ast.used_stdlib_host_calls(); let mut imports = Vec::new(); for import in &ast.imports { let Some(module) = StdlibRegistry::new().module(&import.module.text).cloned() else { @@ -1181,10 +1181,7 @@ impl Lowerer { params: locals.clone(), locals, return_type: *return_type, - abi: call_abi( - &type_, - CallBoundary::HostImport { module: STDLIB_IO_HOST_MODULE.into(), name: member.name.into() }, - ), + abi: call_abi(&type_, CallBoundary::stdlib(member.strategy, member.name)), body: Block { instructions: Vec::new(), result: Box::new(self.nil_expression(import.span)), @@ -1260,23 +1257,18 @@ fn stdlib_lowered_name(module: &str, member: &str) -> String { format!("__stdlib_{}_{}", module.replace('/', "_"), member) } -// TODO: make this a "static" method/constructor -fn stdlib_boundary(strategy: MemberStrategy, member: &str) -> CallBoundary { - match strategy { - MemberStrategy::HostImport => { - CallBoundary::HostImport { module: STDLIB_IO_HOST_MODULE.into(), name: member.into() } - } - _ => CallBoundary::Internal, - } +trait UsedStdlibHostCalls { + fn used_stdlib_host_calls(&self) -> HashSet<(String, String)>; } -/// TODO: This could be an instance method on [ast::Module] -fn used_stdlib_host_calls(module: &ast::Module) -> HashSet<(String, String)> { - let mut calls = HashSet::new(); - for declaration in &module.declarations { - collect_stdlib_host_calls_in_declaration(module, declaration, &mut calls); +impl UsedStdlibHostCalls for ast::Module { + fn used_stdlib_host_calls(&self) -> HashSet<(String, String)> { + let mut calls = HashSet::new(); + for declaration in &self.declarations { + collect_stdlib_host_calls_in_declaration(self, declaration, &mut calls); + } + calls } - calls } fn collect_stdlib_host_calls_in_declaration( @@ -1474,19 +1466,6 @@ fn substitute_type_generics(type_: &Type, substitutions: &HashMap) } } -// TODO: instance method -fn type_has_generic(type_: &Type) -> bool { - match type_ { - Type::Generic(_) => true, - Type::Tuple(items) => items.iter().any(type_has_generic), - Type::List(item) => type_has_generic(item), - Type::Record { fields, .. } => fields.iter().any(|field| type_has_generic(&field.type_)), - Type::Custom { args, .. } | Type::Opaque { args, .. } => args.iter().any(type_has_generic), - Type::Function { params, return_type } => params.iter().any(type_has_generic) || type_has_generic(return_type), - Type::Int | Type::Float | Type::String | Type::BitArray | Type::Bool | Type::Nil => false, - } -} - #[derive(Default)] pub(crate) struct FunctionContext { pub(super) locals: Vec, diff --git a/crates/core/src/types.rs b/crates/core/src/types.rs index 0cdd0e3..7a3678e 100644 --- a/crates/core/src/types.rs +++ b/crates/core/src/types.rs @@ -61,6 +61,20 @@ impl From<&LiteralKind> for Type { } } +impl Type { + pub fn has_generic(&self) -> bool { + match self { + Self::Generic(_) => true, + Self::Tuple(items) => items.iter().any(Self::has_generic), + Self::List(item) => item.has_generic(), + Self::Record { fields, .. } => fields.iter().any(|field| field.type_.has_generic()), + Self::Custom { args, .. } | Self::Opaque { args, .. } => args.iter().any(Self::has_generic), + Self::Function { params, return_type } => params.iter().any(Self::has_generic) || return_type.has_generic(), + _ => false, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct FieldInfo { pub name: String, diff --git a/crates/core/src/wasm.rs b/crates/core/src/wasm.rs index c5f9b1e..088645e 100644 --- a/crates/core/src/wasm.rs +++ b/crates/core/src/wasm.rs @@ -1,5 +1,8 @@ +mod binary; mod builder; +mod encode; mod helpers; +mod validator; use std::{ collections::{HashMap, HashSet}, @@ -333,23 +336,23 @@ impl Emitter { } let previous_current = self.current.clone(); - if block_contains_indirect_call(&function.body) { + if function.body.contains_indirect_call() { writeln!(self.functions, " (local $__callee i32)").expect("write WAT"); self.current.scratch = Some("__callee".into()); } - if block_contains_anonymous_function(&function.body) { + if function.body.contains_anonymous_function() { writeln!(self.functions, " (local $__capture_slots i32)").expect("write WAT"); self.current.capture_slots = Some("__capture_slots".into()); } - if block_contains_record_update(&function.body) || block_contains_constructor(&function.body) { + if function.body.contains_record_update() || function.body.contains_constructor() { writeln!(self.functions, " (local $__record_update_slots i32)").expect("write WAT"); self.current.record_update_slots = Some("__record_update_slots".into()); } - if block_contains_record_update(&function.body) { + if function.body.contains_record_update() { writeln!(self.functions, " (local $__record_update_source i32)").expect("write WAT"); self.current.record_update_source = Some("__record_update_source".into()); } - let debug_locals = block_debug_local_types(&function.body); + let debug_locals = DebugLocalTypes::from(&function.body); if debug_locals.i32 { writeln!(self.functions, " (local $__debug_i32 i32)").expect("write WAT"); self.current.debug_i32 = Some("__debug_i32".into()); @@ -1774,19 +1777,15 @@ struct DebugLocalTypes { f64: bool, } -fn block_debug_local_types(block: &ir::Block) -> DebugLocalTypes { - let mut locals = DebugLocalTypes::default(); - for instruction in &block.instructions { - match instruction { - Instruction::Evaluate { expression, .. } - | Instruction::LocalSet { value: expression, .. } - | Instruction::AssertMatch { value: expression, .. } => { - expression_debug_local_types(expression, &mut locals) - } +impl From<&ir::Block> for DebugLocalTypes { + fn from(block: &ir::Block) -> Self { + let mut locals = Self::default(); + for instruction in &block.instructions { + expression_debug_local_types(instruction.expression(), &mut locals); } + expression_debug_local_types(&block.result, &mut locals); + locals } - expression_debug_local_types(&block.result, &mut locals); - locals } fn expression_debug_local_types(expression: &ir::Expression, locals: &mut DebugLocalTypes) { @@ -1888,288 +1887,6 @@ fn expression_debug_local_types(expression: &ir::Expression, locals: &mut DebugL } } -fn block_contains_indirect_call(block: &ir::Block) -> bool { - block.instructions.iter().any(|instruction| match instruction { - Instruction::Evaluate { expression, .. } - | Instruction::LocalSet { value: expression, .. } - | Instruction::AssertMatch { value: expression, .. } => expression_contains_indirect_call(expression), - }) || expression_contains_indirect_call(&block.result) -} - -fn block_contains_anonymous_function(block: &ir::Block) -> bool { - block.instructions.iter().any(|instruction| match instruction { - Instruction::Evaluate { expression, .. } - | Instruction::LocalSet { value: expression, .. } - | Instruction::AssertMatch { value: expression, .. } => expression_contains_anonymous_function(expression), - }) || expression_contains_anonymous_function(&block.result) -} - -fn block_contains_constructor(block: &ir::Block) -> bool { - block.instructions.iter().any(|instruction| match instruction { - Instruction::Evaluate { expression, .. } - | Instruction::LocalSet { value: expression, .. } - | Instruction::AssertMatch { value: expression, .. } => expression_contains_constructor(expression), - }) || expression_contains_constructor(&block.result) -} - -fn expression_contains_constructor(expression: &ir::Expression) -> bool { - match &expression.kind { - ExpressionKind::Constructor(_) => true, - ExpressionKind::DirectCall(call) => call - .arguments - .iter() - .any(|argument| expression_contains_constructor(&argument.value)), - ExpressionKind::IndirectCall(call) => { - expression_contains_constructor(&call.callee) - || call - .arguments - .iter() - .any(|argument| expression_contains_constructor(&argument.value)) - } - ExpressionKind::Branch(branch) => { - branch.subjects.iter().any(expression_contains_constructor) - || branch.clauses.iter().any(|clause| { - clause.guard.as_ref().is_some_and(expression_contains_constructor) - || expression_contains_constructor(&clause.body) - }) - } - ExpressionKind::Tuple(items) | ExpressionKind::List(items) => items.iter().any(expression_contains_constructor), - ExpressionKind::BitArrayConcat { left, right } - | ExpressionKind::Compare { left, right, .. } - | ExpressionKind::RuntimeEquality { left, right } => { - expression_contains_constructor(left) || expression_contains_constructor(right) - } - ExpressionKind::BitStringDeconstruct { bit_array, .. } - | ExpressionKind::FieldAccess { record: bit_array, .. } - | ExpressionKind::TupleElement { tuple: bit_array, .. } - | ExpressionKind::ListDeconstruct { list: bit_array, .. } => expression_contains_constructor(bit_array), - ExpressionKind::Record(record) => record - .fields - .iter() - .any(|field| expression_contains_constructor(&field.value)), - ExpressionKind::RecordUpdate { record, fields, .. } => { - expression_contains_constructor(record) - || fields - .iter() - .filter_map(|field| field.value.as_ref()) - .any(expression_contains_constructor) - } - ExpressionKind::ListCons { head, tail } => { - expression_contains_constructor(head) || expression_contains_constructor(tail) - } - ExpressionKind::Memory(operation) => match operation { - ir::MemoryOperation::Allocate { bytes } => expression_contains_constructor(bytes), - ir::MemoryOperation::Load { address, .. } => expression_contains_constructor(address), - ir::MemoryOperation::Store { address, value } => { - expression_contains_constructor(address) || expression_contains_constructor(value) - } - }, - ExpressionKind::Pipeline(pipeline) => { - expression_contains_constructor(&pipeline.input) || expression_contains_constructor(&pipeline.call) - } - ExpressionKind::Use(use_) => { - expression_contains_constructor(&use_.callback) || expression_contains_constructor(&use_.call) - } - _ => false, - } -} - -fn block_contains_record_update(block: &ir::Block) -> bool { - block.instructions.iter().any(|instruction| match instruction { - Instruction::Evaluate { expression, .. } - | Instruction::LocalSet { value: expression, .. } - | Instruction::AssertMatch { value: expression, .. } => expression_contains_record_update(expression), - }) || expression_contains_record_update(&block.result) -} - -fn expression_contains_record_update(expression: &ir::Expression) -> bool { - match &expression.kind { - ExpressionKind::RecordUpdate { .. } => true, - ExpressionKind::DirectCall(call) => call - .arguments - .iter() - .any(|argument| expression_contains_record_update(&argument.value)), - ExpressionKind::IndirectCall(call) => { - expression_contains_record_update(&call.callee) - || call - .arguments - .iter() - .any(|argument| expression_contains_record_update(&argument.value)) - } - ExpressionKind::Branch(branch) => { - branch.subjects.iter().any(expression_contains_record_update) - || branch.clauses.iter().any(|clause| { - clause.guard.as_ref().is_some_and(expression_contains_record_update) - || expression_contains_record_update(&clause.body) - }) - } - ExpressionKind::Tuple(items) | ExpressionKind::List(items) => { - items.iter().any(expression_contains_record_update) - } - ExpressionKind::BitArrayConcat { left, right } - | ExpressionKind::Compare { left, right, .. } - | ExpressionKind::RuntimeEquality { left, right } => { - expression_contains_record_update(left) || expression_contains_record_update(right) - } - ExpressionKind::BitStringDeconstruct { bit_array, .. } - | ExpressionKind::FieldAccess { record: bit_array, .. } - | ExpressionKind::TupleElement { tuple: bit_array, .. } - | ExpressionKind::ListDeconstruct { list: bit_array, .. } => expression_contains_record_update(bit_array), - ExpressionKind::Record(record) => record - .fields - .iter() - .any(|field| expression_contains_record_update(&field.value)), - ExpressionKind::Constructor(constructor) => constructor.arguments.iter().any(expression_contains_record_update), - ExpressionKind::ListCons { head, tail } => { - expression_contains_record_update(head) || expression_contains_record_update(tail) - } - ExpressionKind::Memory(operation) => match operation { - ir::MemoryOperation::Allocate { bytes } => expression_contains_record_update(bytes), - ir::MemoryOperation::Load { address, .. } => expression_contains_record_update(address), - ir::MemoryOperation::Store { address, value } => { - expression_contains_record_update(address) || expression_contains_record_update(value) - } - }, - ExpressionKind::Pipeline(pipeline) => { - expression_contains_record_update(&pipeline.input) || expression_contains_record_update(&pipeline.call) - } - ExpressionKind::Use(use_) => { - expression_contains_record_update(&use_.callback) || expression_contains_record_update(&use_.call) - } - _ => false, - } -} - -fn expression_contains_anonymous_function(expression: &ir::Expression) -> bool { - match &expression.kind { - ExpressionKind::AnonymousFunction(_) => true, - ExpressionKind::DirectCall(call) => call - .arguments - .iter() - .any(|argument| expression_contains_anonymous_function(&argument.value)), - ExpressionKind::IndirectCall(call) => { - expression_contains_anonymous_function(&call.callee) - || call - .arguments - .iter() - .any(|argument| expression_contains_anonymous_function(&argument.value)) - } - ExpressionKind::Branch(branch) => { - branch.subjects.iter().any(expression_contains_anonymous_function) - || branch.clauses.iter().any(|clause| { - clause - .guard - .as_ref() - .is_some_and(expression_contains_anonymous_function) - || expression_contains_anonymous_function(&clause.body) - }) - } - ExpressionKind::Tuple(items) | ExpressionKind::List(items) => { - items.iter().any(expression_contains_anonymous_function) - } - ExpressionKind::BitArrayConcat { left, right } - | ExpressionKind::Compare { left, right, .. } - | ExpressionKind::RuntimeEquality { left, right } => { - expression_contains_anonymous_function(left) || expression_contains_anonymous_function(right) - } - ExpressionKind::BitStringDeconstruct { bit_array, .. } - | ExpressionKind::FieldAccess { record: bit_array, .. } - | ExpressionKind::TupleElement { tuple: bit_array, .. } - | ExpressionKind::ListDeconstruct { list: bit_array, .. } => expression_contains_anonymous_function(bit_array), - ExpressionKind::Record(record) => record - .fields - .iter() - .any(|field| expression_contains_anonymous_function(&field.value)), - ExpressionKind::Constructor(constructor) => { - constructor.arguments.iter().any(expression_contains_anonymous_function) - } - ExpressionKind::RecordUpdate { record, fields, .. } => { - expression_contains_anonymous_function(record) - || fields - .iter() - .filter_map(|field| field.value.as_ref()) - .any(expression_contains_anonymous_function) - } - ExpressionKind::ListCons { head, tail } => { - expression_contains_anonymous_function(head) || expression_contains_anonymous_function(tail) - } - ExpressionKind::Memory(operation) => match operation { - ir::MemoryOperation::Allocate { bytes } => expression_contains_anonymous_function(bytes), - ir::MemoryOperation::Load { address, .. } => expression_contains_anonymous_function(address), - ir::MemoryOperation::Store { address, value } => { - expression_contains_anonymous_function(address) || expression_contains_anonymous_function(value) - } - }, - ExpressionKind::Pipeline(pipeline) => { - expression_contains_anonymous_function(&pipeline.input) - || expression_contains_anonymous_function(&pipeline.call) - } - ExpressionKind::Use(use_) => { - expression_contains_anonymous_function(&use_.callback) || expression_contains_anonymous_function(&use_.call) - } - _ => false, - } -} - -fn expression_contains_indirect_call(expression: &ir::Expression) -> bool { - match &expression.kind { - ExpressionKind::IndirectCall(_) => true, - ExpressionKind::DirectCall(call) => call - .arguments - .iter() - .any(|argument| expression_contains_indirect_call(&argument.value)), - ExpressionKind::Branch(branch) => { - branch.subjects.iter().any(expression_contains_indirect_call) - || branch.clauses.iter().any(|clause| { - clause.guard.as_ref().is_some_and(expression_contains_indirect_call) - || expression_contains_indirect_call(&clause.body) - }) - } - ExpressionKind::Tuple(items) | ExpressionKind::List(items) => { - items.iter().any(expression_contains_indirect_call) - } - ExpressionKind::BitArrayConcat { left, right } - | ExpressionKind::Compare { left, right, .. } - | ExpressionKind::RuntimeEquality { left, right } => { - expression_contains_indirect_call(left) || expression_contains_indirect_call(right) - } - ExpressionKind::BitStringDeconstruct { bit_array, .. } - | ExpressionKind::FieldAccess { record: bit_array, .. } - | ExpressionKind::TupleElement { tuple: bit_array, .. } - | ExpressionKind::ListDeconstruct { list: bit_array, .. } => expression_contains_indirect_call(bit_array), - ExpressionKind::Record(record) => record - .fields - .iter() - .any(|field| expression_contains_indirect_call(&field.value)), - ExpressionKind::Constructor(constructor) => constructor.arguments.iter().any(expression_contains_indirect_call), - ExpressionKind::RecordUpdate { record, fields, .. } => { - expression_contains_indirect_call(record) - || fields - .iter() - .filter_map(|field| field.value.as_ref()) - .any(expression_contains_indirect_call) - } - ExpressionKind::ListCons { head, tail } => { - expression_contains_indirect_call(head) || expression_contains_indirect_call(tail) - } - ExpressionKind::Memory(operation) => match operation { - ir::MemoryOperation::Allocate { bytes } => expression_contains_indirect_call(bytes), - ir::MemoryOperation::Load { address, .. } => expression_contains_indirect_call(address), - ir::MemoryOperation::Store { address, value } => { - expression_contains_indirect_call(address) || expression_contains_indirect_call(value) - } - }, - ExpressionKind::Literal(_) - | ExpressionKind::LocalGet(_) - | ExpressionKind::FunctionValue(_) - | ExpressionKind::AnonymousFunction(_) - | ExpressionKind::Pipeline(_) - | ExpressionKind::Use(_) - | ExpressionKind::BitArray(_) - | ExpressionKind::Failure(_) => false, - } -} - fn integer_operator_instruction(function: &str) -> &'static str { match function { "__op_add" => "i64.add", diff --git a/crates/core/src/wasm/binary.rs b/crates/core/src/wasm/binary.rs new file mode 100644 index 0000000..156c451 --- /dev/null +++ b/crates/core/src/wasm/binary.rs @@ -0,0 +1,191 @@ +use super::{builder::*, encode::*}; + +pub(super) struct BinaryEmitter<'a> { + module: &'a Module, +} + +impl<'a> BinaryEmitter<'a> { + pub(super) fn new(module: &'a Module) -> Self { + Self { module } + } + + pub(super) fn emit(&self) -> Vec { + let mut out = b"\0asm\x01\0\0\0".to_vec(); + for section in self.module.custom_sections.iter().map(Self::custom_section) { + out.extend(section); + } + self.section(&mut out, 1, self.type_section()); + self.section(&mut out, 2, self.import_section()); + self.section(&mut out, 3, self.function_section()); + self.section(&mut out, 4, self.table_section()); + self.section(&mut out, 5, self.memory_section()); + self.section(&mut out, 7, self.export_section()); + self.section(&mut out, 10, self.code_section()); + self.section(&mut out, 11, self.data_section()); + out + } + + fn section(&self, out: &mut Vec, id: u8, payload: Vec) { + if payload.is_empty() { + return; + } + out.push(id); + encode_u32(payload.len() as u32, out); + out.extend(payload); + } + + fn custom_section(section: &CustomSection) -> Vec { + let mut payload = Vec::new(); + encode_name(§ion.name, &mut payload); + payload.extend(§ion.bytes); + let mut out = vec![0]; + encode_u32(payload.len() as u32, &mut out); + out.extend(payload); + out + } + + fn type_section(&self) -> Vec { + if self.module.types.is_empty() { + return Vec::new(); + } + let mut out = Vec::new(); + encode_u32(self.module.types.len() as u32, &mut out); + for type_ in &self.module.types { + out.push(0x60); + encode_vec_types(&type_.params, &mut out); + encode_vec_types(&type_.results, &mut out); + } + out + } + + fn import_section(&self) -> Vec { + if self.module.imports.is_empty() { + return Vec::new(); + } + let mut out = Vec::new(); + encode_u32(self.module.imports.len() as u32, &mut out); + for import in &self.module.imports { + encode_name(&import.module, &mut out); + encode_name(&import.name, &mut out); + match &import.desc { + ImportDesc::Function(type_id) => { + out.push(0x00); + encode_u32(type_id.0, &mut out); + } + ImportDesc::Table(table) => { + out.push(0x01); + encode_table(table, &mut out); + } + ImportDesc::Memory(memory) => { + out.push(0x02); + encode_limits(memory.minimum_pages, memory.maximum_pages, &mut out); + } + } + } + out + } + + fn function_section(&self) -> Vec { + if self.module.functions.is_empty() { + return Vec::new(); + } + let mut out = Vec::new(); + encode_u32(self.module.functions.len() as u32, &mut out); + for function in &self.module.functions { + encode_u32(function.type_id.0, &mut out); + } + out + } + + fn table_section(&self) -> Vec { + if self.module.tables.is_empty() { + return Vec::new(); + } + let mut out = Vec::new(); + encode_u32(self.module.tables.len() as u32, &mut out); + for table in &self.module.tables { + encode_table(table, &mut out); + } + out + } + + fn memory_section(&self) -> Vec { + if self.module.memories.is_empty() { + return Vec::new(); + } + let mut out = Vec::new(); + encode_u32(self.module.memories.len() as u32, &mut out); + for memory in &self.module.memories { + encode_limits(memory.minimum_pages, memory.maximum_pages, &mut out); + } + out + } + + fn export_section(&self) -> Vec { + if self.module.exports.is_empty() { + return Vec::new(); + } + let mut out = Vec::new(); + encode_u32(self.module.exports.len() as u32, &mut out); + for export in &self.module.exports { + encode_name(&export.name, &mut out); + match export.desc { + ExportDesc::Function(id) => { + out.push(0x00); + encode_u32(id.0, &mut out); + } + ExportDesc::Table(id) => { + out.push(0x01); + encode_u32(id.0, &mut out); + } + ExportDesc::Memory(id) => { + out.push(0x02); + encode_u32(id.0, &mut out); + } + } + } + out + } + + fn code_section(&self) -> Vec { + if self.module.functions.is_empty() { + return Vec::new(); + } + let mut out = Vec::new(); + encode_u32(self.module.functions.len() as u32, &mut out); + for function in &self.module.functions { + let mut body = Vec::new(); + encode_locals(&function.locals, &mut body); + for instruction in &function.body { + encode_instruction(instruction, &mut body, self.module); + } + body.push(0x0b); + encode_u32(body.len() as u32, &mut out); + out.extend(body); + } + out + } + + fn data_section(&self) -> Vec { + if self.module.data_segments.is_empty() { + return Vec::new(); + } + let mut out = Vec::new(); + encode_u32(self.module.data_segments.len() as u32, &mut out); + for segment in &self.module.data_segments { + if segment.memory.0 == 0 { + out.push(0x00); + } else { + out.push(0x02); + encode_u32(segment.memory.0, &mut out); + } + for instruction in &segment.offset { + encode_instruction(instruction, &mut out, self.module); + } + out.push(0x0b); + encode_u32(segment.bytes.len() as u32, &mut out); + out.extend(&segment.bytes); + } + out + } +} diff --git a/crates/core/src/wasm/builder.rs b/crates/core/src/wasm/builder.rs index 6e56036..d7f89aa 100644 --- a/crates/core/src/wasm/builder.rs +++ b/crates/core/src/wasm/builder.rs @@ -5,6 +5,13 @@ #![allow(dead_code)] +use std::fmt; + +use super::{ + binary::BinaryEmitter, + validator::{ValidationResult, Validator}, +}; + #[derive(Debug, Default, Clone, PartialEq, Eq)] pub(crate) struct Module { pub(crate) types: Vec, @@ -41,23 +48,37 @@ impl Module { } pub(crate) fn push_memory(&mut self, memory: Memory) -> MemoryId { - let id = MemoryId(self.memories.len() as u32); + let id = MemoryId((self.imported_memory_count() + self.memories.len()) as u32); self.memories.push(memory); id } pub(crate) fn push_table(&mut self, table: Table) -> TableId { - let id = TableId(self.tables.len() as u32); + let id = TableId((self.imported_table_count() + self.tables.len()) as u32); self.tables.push(table); id } - fn imported_function_count(&self) -> usize { + pub(super) fn imported_function_count(&self) -> usize { self.imports .iter() .filter(|import| matches!(import.desc, ImportDesc::Function(_))) .count() } + + pub(super) fn imported_memory_count(&self) -> usize { + self.imports + .iter() + .filter(|import| matches!(import.desc, ImportDesc::Memory(_))) + .count() + } + + pub(super) fn imported_table_count(&self) -> usize { + self.imports + .iter() + .filter(|import| matches!(import.desc, ImportDesc::Table(_))) + .count() + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -103,6 +124,19 @@ pub(crate) enum ValueType { ExternRef, } +impl fmt::Display for ValueType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::I32 => "i32", + Self::I64 => "i64", + Self::F32 => "f32", + Self::F64 => "f64", + Self::FuncRef => "funcref", + Self::ExternRef => "externref", + }) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct Import { pub(crate) module: String, @@ -157,6 +191,15 @@ pub(crate) enum ReferenceType { ExternRef, } +impl fmt::Display for ReferenceType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::FuncRef => "funcref", + Self::ExternRef => "externref", + }) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct Export { pub(crate) name: String, @@ -379,6 +422,304 @@ pub(crate) enum ControlEffect { Unreachable, } +impl Module { + pub(crate) fn validate(&self) -> ValidationResult<()> { + Validator::new(self).validate() + } + + pub(crate) fn to_wasm_bytes(&self) -> ValidationResult> { + self.validate()?; + Ok(BinaryEmitter::new(self).emit()) + } + + pub(crate) fn to_wat(&self) -> ValidationResult { + self.validate()?; + Ok(WatRenderer::new(self).render()) + } +} + +struct WatRenderer<'a> { + module: &'a Module, + wat: String, + indent: usize, +} + +impl<'a> WatRenderer<'a> { + fn new(module: &'a Module) -> Self { + Self { module, wat: String::new(), indent: 0 } + } + + fn render(mut self) -> String { + self.line("(module"); + self.indent += 1; + for type_ in &self.module.types { + self.line(&format!( + "(type (func{}{}))", + params_wat(&type_.params), + results_wat(&type_.results) + )); + } + for import in &self.module.imports { + self.render_import(import); + } + for table in &self.module.tables { + self.line(&format!( + "(table {}{} {})", + table.minimum, + max_suffix(table.maximum), + table.element_type + )); + } + for memory in &self.module.memories { + self.line(&format!( + "(memory {}{})", + memory.minimum_pages, + max_suffix(memory.maximum_pages) + )); + } + for function in &self.module.functions { + self.render_function(function); + } + for export in &self.module.exports { + self.render_export(export); + } + for segment in &self.module.data_segments { + self.render_data_segment(segment); + } + self.indent -= 1; + self.line(")"); + self.wat + } + + fn render_import(&mut self, import: &Import) { + match &import.desc { + ImportDesc::Function(type_id) => { + let type_ = &self.module.types[type_id.0 as usize]; + self.line(&format!( + "(import {:?} {:?} (func (type {}){}{}))", + import.module, + import.name, + type_id.0, + params_wat(&type_.params), + results_wat(&type_.results) + )); + } + ImportDesc::Memory(memory) => self.line(&format!( + "(import {:?} {:?} (memory {}{}))", + import.module, + import.name, + memory.minimum_pages, + max_suffix(memory.maximum_pages) + )), + ImportDesc::Table(table) => self.line(&format!( + "(import {:?} {:?} (table {}{} {}))", + import.module, + import.name, + table.minimum, + max_suffix(table.maximum), + table.element_type + )), + } + } + + fn render_function(&mut self, function: &Function) { + let type_ = &self.module.types[function.type_id.0 as usize]; + let name = function + .name + .as_deref() + .map(wat_id) + .map(|name| format!(" {name}")) + .unwrap_or_default(); + self.line(&format!( + "(func{name} (type {}){}{}", + function.type_id.0, + params_wat(&type_.params), + results_wat(&type_.results) + )); + self.indent += 1; + for local in &function.locals { + self.line(&format!("(local {})", local.type_)); + } + for instruction in &function.body { + self.render_instruction(instruction); + } + self.indent -= 1; + self.line(")"); + } + + fn render_export(&mut self, export: &Export) { + let desc = match export.desc { + ExportDesc::Function(id) => format!("func {}", id.0), + ExportDesc::Memory(id) => format!("memory {}", id.0), + ExportDesc::Table(id) => format!("table {}", id.0), + }; + self.line(&format!("(export {:?} ({desc}))", export.name)); + } + + fn render_data_segment(&mut self, segment: &DataSegment) { + let offset = segment + .offset + .iter() + .map(instruction_inline_wat) + .collect::>() + .join(" "); + self.line(&format!( + "(data (memory {}) (offset {offset}) {:?})", + segment.memory.0, + String::from_utf8_lossy(&segment.bytes) + )); + } + + fn render_instruction(&mut self, instruction: &Instruction) { + match instruction { + Instruction::Block { type_, body } => { + self.line(&format!( + "block{}{}", + params_wat(&type_.params), + results_wat(&type_.results) + )); + self.indent += 1; + for instruction in body { + self.render_instruction(instruction); + } + self.indent -= 1; + self.line("end"); + } + Instruction::Loop { type_, body } => { + self.line(&format!( + "loop{}{}", + params_wat(&type_.params), + results_wat(&type_.results) + )); + self.indent += 1; + for instruction in body { + self.render_instruction(instruction); + } + self.indent -= 1; + self.line("end"); + } + Instruction::If { type_, then_body, else_body } => { + self.line(&format!( + "if{}{}", + params_wat(&type_.params), + results_wat(&type_.results) + )); + self.indent += 1; + for instruction in then_body { + self.render_instruction(instruction); + } + if !else_body.is_empty() { + self.indent -= 1; + self.line("else"); + self.indent += 1; + for instruction in else_body { + self.render_instruction(instruction); + } + } + self.indent -= 1; + self.line("end"); + } + _ => self.line(&instruction_inline_wat(instruction)), + } + } + + fn line(&mut self, text: &str) { + for _ in 0..self.indent { + self.wat.push_str(" "); + } + self.wat.push_str(text); + self.wat.push('\n'); + } +} + +fn instruction_inline_wat(instruction: &Instruction) -> String { + match instruction { + Instruction::Unreachable => "unreachable".into(), + Instruction::Nop => "nop".into(), + Instruction::Br { depth, .. } => format!("br {depth}"), + Instruction::BrIf { depth, .. } => format!("br_if {depth}"), + Instruction::Return { .. } => "return".into(), + Instruction::Call { function, .. } => format!("call {}", function.0), + Instruction::CallIndirect { table, type_id, .. } => { + format!("call_indirect (type {}) (table {})", type_id.0, table.0) + } + Instruction::Drop(_) => "drop".into(), + Instruction::Select(_) => "select".into(), + Instruction::LocalGet { local, .. } => format!("local.get {}", local.0), + Instruction::LocalSet { local, .. } => format!("local.set {}", local.0), + Instruction::LocalTee { local, .. } => format!("local.tee {}", local.0), + Instruction::I32Const(value) => format!("i32.const {value}"), + Instruction::I64Const(value) => format!("i64.const {value}"), + Instruction::F32Const(value) => format!("f32.const {}", f32::from_bits(*value)), + Instruction::F64Const(value) => format!("f64.const {}", f64::from_bits(*value)), + Instruction::I32Eqz => "i32.eqz".into(), + Instruction::I32Eq => "i32.eq".into(), + Instruction::I32Ne => "i32.ne".into(), + Instruction::I32LtS => "i32.lt_s".into(), + Instruction::I32GtS => "i32.gt_s".into(), + Instruction::I32LeS => "i32.le_s".into(), + Instruction::I32GeS => "i32.ge_s".into(), + Instruction::I64Eqz => "i64.eqz".into(), + Instruction::I64Eq => "i64.eq".into(), + Instruction::F64Eq => "f64.eq".into(), + Instruction::F64Ne => "f64.ne".into(), + Instruction::F64Lt => "f64.lt".into(), + Instruction::F64Gt => "f64.gt".into(), + Instruction::F64Le => "f64.le".into(), + Instruction::F64Ge => "f64.ge".into(), + Instruction::I32Add => "i32.add".into(), + Instruction::I32Sub => "i32.sub".into(), + Instruction::I32Mul => "i32.mul".into(), + Instruction::I32DivS => "i32.div_s".into(), + Instruction::I64Add => "i64.add".into(), + Instruction::I64Sub => "i64.sub".into(), + Instruction::I64Mul => "i64.mul".into(), + Instruction::I64DivS => "i64.div_s".into(), + Instruction::F64Add => "f64.add".into(), + Instruction::F64Sub => "f64.sub".into(), + Instruction::F64Mul => "f64.mul".into(), + Instruction::F64Div => "f64.div".into(), + Instruction::I32Load(arg) => memory_wat("i32.load", arg), + Instruction::I32Store(arg) => memory_wat("i32.store", arg), + Instruction::I64Load(arg) => memory_wat("i64.load", arg), + Instruction::I64Store(arg) => memory_wat("i64.store", arg), + Instruction::F64Load(arg) => memory_wat("f64.load", arg), + Instruction::F64Store(arg) => memory_wat("f64.store", arg), + Instruction::Block { .. } | Instruction::Loop { .. } | Instruction::If { .. } => unreachable!(), + } +} + +fn memory_wat(opcode: &str, arg: &MemoryArg) -> String { + format!("{opcode} offset={} align={}", arg.offset, 1_u32 << arg.align) +} + +fn params_wat(types: &[ValueType]) -> String { + types + .iter() + .map(|type_| format!(" (param {type_})")) + .collect::() +} + +fn results_wat(types: &[ValueType]) -> String { + types + .iter() + .map(|type_| format!(" (result {type_})")) + .collect::() +} + +fn max_suffix(maximum: Option) -> String { + maximum.map(|maximum| format!(" {maximum}")).unwrap_or_default() +} + +fn wat_id(name: &str) -> String { + let mut out = String::from("$"); + out.extend( + name.chars() + .map(|char| if char.is_ascii_alphanumeric() || char == '_' { char } else { '_' }), + ); + out +} + #[cfg(test)] mod tests { use super::*; @@ -409,4 +750,115 @@ mod tests { assert_eq!(branch_effect.consumes, vec![ValueType::I32, ValueType::I32]); assert_eq!(branch_effect.produces, vec![ValueType::I32]); } + + #[test] + fn validates_stack_locals_branches_and_call_signatures() { + let mut module = Module::new(); + let host_type = module.push_type(FunctionType::new([ValueType::I32], [ValueType::I32])); + let void_type = module.push_type(FunctionType::new([], [])); + module.push_import(Import { module: "env".into(), name: "host".into(), desc: ImportDesc::Function(host_type) }); + let mut function = Function::new(void_type); + function.body = vec![ + Instruction::LocalGet { local: LocalId(99), type_: ValueType::I32 }, + Instruction::Call { function: FunctionId(0), type_: FunctionType::new([], []) }, + Instruction::Block { + type_: BlockType::new([], [ValueType::I32]), + body: vec![Instruction::Br { depth: 0, results: vec![] }], + }, + ]; + module.push_function(function); + + let errors = module.validate().expect_err("module should not validate"); + let messages = errors + .into_iter() + .map(|error| error.message) + .collect::>() + .join("\n"); + assert!(messages.contains("unknown local index"), "{messages}"); + assert!(messages.contains("call to function 0 has signature"), "{messages}"); + assert!(messages.contains("branch depth 0 has results"), "{messages}"); + } + + #[test] + fn emits_valid_wasm_bytes_directly_from_structured_module() { + let module = add_module(); + let bytes = module + .to_wasm_bytes() + .expect("structured module should validate and emit"); + assert_eq!(&bytes[..8], b"\0asm\x01\0\0\0"); + + let engine = wasmtime::Engine::default(); + let wasm = wasmtime::Module::from_binary(&engine, &bytes).expect("direct bytes should be valid wasm"); + let mut store = wasmtime::Store::new(&engine, ()); + let instance = wasmtime::Instance::new(&mut store, &wasm, &[]).expect("module should instantiate"); + let add = instance + .get_typed_func::<(i32, i32), i32>(&mut store, "add") + .expect("add export should exist"); + assert_eq!(add.call(&mut store, (20, 22)).expect("add call should succeed"), 42); + } + + #[test] + fn renders_wat_from_structured_module() { + let module = add_module(); + let wat = module.to_wat().expect("structured module should render to wat"); + assert!(wat.contains("(type (func (param i32) (param i32) (result i32)))")); + assert!(wat.contains("i32.add")); + assert!(wat.contains("(export \"add\" (func 0))")); + wat::parse_str(&wat).expect("rendered wat should assemble"); + } + + #[test] + fn emits_type_indexed_block_signatures() { + let mut module = Module::new(); + let type_id = module.push_type(FunctionType::new([ValueType::I32], [ValueType::I32])); + let mut function = Function::new(type_id); + function.params = vec![Local { name: None, type_: ValueType::I32 }]; + function.body = vec![ + Instruction::LocalGet { local: LocalId(0), type_: ValueType::I32 }, + Instruction::Block { + type_: BlockType::new([ValueType::I32], [ValueType::I32]), + body: vec![Instruction::I32Const(1), Instruction::I32Add], + }, + ]; + let function_id = module.push_function(function); + module + .exports + .push(Export { name: "inc".into(), desc: ExportDesc::Function(function_id) }); + wat::parse_str(module.to_wat().expect("typed block module should render")) + .expect("typed block wat should assemble"); + + let engine = wasmtime::Engine::default(); + let wasm = wasmtime::Module::from_binary( + &engine, + &module.to_wasm_bytes().expect("typed block module should emit"), + ) + .expect("typed block bytes should be valid wasm"); + let mut store = wasmtime::Store::new(&engine, ()); + let instance = wasmtime::Instance::new(&mut store, &wasm, &[]).expect("module should instantiate"); + let inc = instance + .get_typed_func::(&mut store, "inc") + .expect("inc export should exist"); + assert_eq!(inc.call(&mut store, 41).expect("inc call should succeed"), 42); + } + + fn add_module() -> Module { + let mut module = Module::new(); + let type_id = module.push_type(FunctionType::new([ValueType::I32, ValueType::I32], [ValueType::I32])); + let mut function = Function::new(type_id); + function.name = Some("add".into()); + function.params = vec![ + Local { name: Some("left".into()), type_: ValueType::I32 }, + Local { name: Some("right".into()), type_: ValueType::I32 }, + ]; + function.body = vec![ + Instruction::LocalGet { local: LocalId(0), type_: ValueType::I32 }, + Instruction::LocalGet { local: LocalId(1), type_: ValueType::I32 }, + Instruction::I32Add, + ]; + let function_id = module.push_function(function); + module + .exports + .push(Export { name: "add".into(), desc: ExportDesc::Function(function_id) }); + module + } } diff --git a/crates/core/src/wasm/encode.rs b/crates/core/src/wasm/encode.rs new file mode 100644 index 0000000..79a3c08 --- /dev/null +++ b/crates/core/src/wasm/encode.rs @@ -0,0 +1,238 @@ +use super::builder::*; + +pub(super) fn encode_instruction(instruction: &Instruction, out: &mut Vec, module: &Module) { + match instruction { + Instruction::Unreachable => out.push(0x00), + Instruction::Nop => out.push(0x01), + Instruction::Block { type_, body } => { + out.push(0x02); + encode_block_type(type_, out, module); + for instruction in body { + encode_instruction(instruction, out, module); + } + out.push(0x0b); + } + Instruction::Loop { type_, body } => { + out.push(0x03); + encode_block_type(type_, out, module); + for instruction in body { + encode_instruction(instruction, out, module); + } + out.push(0x0b); + } + Instruction::If { type_, then_body, else_body } => { + out.push(0x04); + encode_block_type(type_, out, module); + for instruction in then_body { + encode_instruction(instruction, out, module); + } + if !else_body.is_empty() { + out.push(0x05); + for instruction in else_body { + encode_instruction(instruction, out, module); + } + } + out.push(0x0b); + } + Instruction::Br { depth, .. } => { + out.push(0x0c); + encode_u32(*depth, out); + } + Instruction::BrIf { depth, .. } => { + out.push(0x0d); + encode_u32(*depth, out); + } + Instruction::Return { .. } => out.push(0x0f), + Instruction::Call { function, .. } => { + out.push(0x10); + encode_u32(function.0, out); + } + Instruction::CallIndirect { table, type_id, .. } => { + out.push(0x11); + encode_u32(type_id.0, out); + encode_u32(table.0, out); + } + Instruction::Drop(_) => out.push(0x1a), + Instruction::Select(_) => out.push(0x1b), + Instruction::LocalGet { local, .. } => { + out.push(0x20); + encode_u32(local.0, out); + } + Instruction::LocalSet { local, .. } => { + out.push(0x21); + encode_u32(local.0, out); + } + Instruction::LocalTee { local, .. } => { + out.push(0x22); + encode_u32(local.0, out); + } + Instruction::I32Const(value) => { + out.push(0x41); + encode_i32(*value, out); + } + Instruction::I64Const(value) => { + out.push(0x42); + encode_i64(*value, out); + } + Instruction::F32Const(value) => { + out.push(0x43); + out.extend(value.to_le_bytes()); + } + Instruction::F64Const(value) => { + out.push(0x44); + out.extend(value.to_le_bytes()); + } + Instruction::I32Eqz => out.push(0x45), + Instruction::I32Eq => out.push(0x46), + Instruction::I32Ne => out.push(0x47), + Instruction::I32LtS => out.push(0x48), + Instruction::I32GtS => out.push(0x4a), + Instruction::I32LeS => out.push(0x4c), + Instruction::I32GeS => out.push(0x4e), + Instruction::I64Eqz => out.push(0x50), + Instruction::I64Eq => out.push(0x51), + Instruction::F64Eq => out.push(0x61), + Instruction::F64Ne => out.push(0x62), + Instruction::F64Lt => out.push(0x63), + Instruction::F64Gt => out.push(0x64), + Instruction::F64Le => out.push(0x65), + Instruction::F64Ge => out.push(0x66), + Instruction::I32Add => out.push(0x6a), + Instruction::I32Sub => out.push(0x6b), + Instruction::I32Mul => out.push(0x6c), + Instruction::I32DivS => out.push(0x6d), + Instruction::I64Add => out.push(0x7c), + Instruction::I64Sub => out.push(0x7d), + Instruction::I64Mul => out.push(0x7e), + Instruction::I64DivS => out.push(0x7f), + Instruction::F64Add => out.push(0xa0), + Instruction::F64Sub => out.push(0xa1), + Instruction::F64Mul => out.push(0xa2), + Instruction::F64Div => out.push(0xa3), + Instruction::I32Load(arg) => encode_memory_instruction(0x28, arg, out), + Instruction::I64Load(arg) => encode_memory_instruction(0x29, arg, out), + Instruction::F64Load(arg) => encode_memory_instruction(0x2b, arg, out), + Instruction::I32Store(arg) => encode_memory_instruction(0x36, arg, out), + Instruction::I64Store(arg) => encode_memory_instruction(0x37, arg, out), + Instruction::F64Store(arg) => encode_memory_instruction(0x39, arg, out), + } +} + +pub(super) fn encode_memory_instruction(opcode: u8, arg: &MemoryArg, out: &mut Vec) { + out.push(opcode); + encode_u32(arg.align, out); + encode_u32(arg.offset, out); +} + +pub(super) fn encode_locals(locals: &[Local], out: &mut Vec) { + let mut groups: Vec<(u32, ValueType)> = Vec::new(); + for local in locals { + match groups.last_mut() { + Some((count, type_)) if *type_ == local.type_ => *count += 1, + _ => groups.push((1, local.type_)), + } + } + encode_u32(groups.len() as u32, out); + for (count, type_) in groups { + encode_u32(count, out); + out.push(u8::from(type_)); + } +} + +pub(super) fn encode_block_type(type_: &BlockType, out: &mut Vec, module: &Module) { + match (type_.params.as_slice(), type_.results.as_slice()) { + ([], []) => out.push(0x40), + ([], [result]) => out.push(u8::from(*result)), + _ => { + let type_id = module + .types + .iter() + .position(|candidate| candidate == &FunctionType::new(type_.params.clone(), type_.results.clone())) + .expect("validated block type should exist in type section"); + encode_i64(type_id as i64, out); + } + } +} + +pub(super) fn encode_table(table: &Table, out: &mut Vec) { + out.push(u8::from(table.element_type)); + encode_limits(table.minimum, table.maximum, out); +} + +pub(super) fn encode_limits(minimum: u32, maximum: Option, out: &mut Vec) { + match maximum { + Some(maximum) => { + out.push(0x01); + encode_u32(minimum, out); + encode_u32(maximum, out); + } + None => { + out.push(0x00); + encode_u32(minimum, out); + } + } +} + +pub(super) fn encode_vec_types(types: &[ValueType], out: &mut Vec) { + encode_u32(types.len() as u32, out); + for type_ in types { + out.push(u8::from(*type_)); + } +} + +impl From for u8 { + fn from(type_: ValueType) -> Self { + match type_ { + ValueType::I32 => 0x7f, + ValueType::I64 => 0x7e, + ValueType::F32 => 0x7d, + ValueType::F64 => 0x7c, + ValueType::FuncRef => 0x70, + ValueType::ExternRef => 0x6f, + } + } +} + +pub(super) fn encode_name(name: &str, out: &mut Vec) { + encode_u32(name.len() as u32, out); + out.extend(name.as_bytes()); +} + +pub(super) fn encode_u32(mut value: u32, out: &mut Vec) { + loop { + let mut byte = (value & 0x7f) as u8; + value >>= 7; + if value != 0 { + byte |= 0x80; + } + out.push(byte); + if value == 0 { + break; + } + } +} + +pub(super) fn encode_i32(value: i32, out: &mut Vec) { + encode_i64(value as i64, out); +} + +pub(super) fn encode_i64(mut value: i64, out: &mut Vec) { + loop { + let byte = (value as u8) & 0x7f; + value >>= 7; + let done = (value == 0 && (byte & 0x40) == 0) || (value == -1 && (byte & 0x40) != 0); + out.push(if done { byte } else { byte | 0x80 }); + if done { + break; + } + } +} + +impl From for u8 { + fn from(type_: ReferenceType) -> Self { + match type_ { + ReferenceType::FuncRef => 0x70, + ReferenceType::ExternRef => 0x6f, + } + } +} diff --git a/crates/core/src/wasm/validator.rs b/crates/core/src/wasm/validator.rs new file mode 100644 index 0000000..9429e33 --- /dev/null +++ b/crates/core/src/wasm/validator.rs @@ -0,0 +1,482 @@ +use super::builder::*; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct ValidationError { + pub(crate) message: String, +} + +impl ValidationError { + fn new(message: impl Into) -> Self { + Self { message: message.into() } + } +} + +pub(crate) type ValidationResult = Result>; + +pub(super) struct Validator<'a> { + module: &'a Module, + errors: Vec, +} + +impl<'a> Validator<'a> { + pub(super) fn new(module: &'a Module) -> Self { + Self { module, errors: Vec::new() } + } + + pub(super) fn validate(mut self) -> ValidationResult<()> { + self.validate_module(); + if self.errors.is_empty() { Ok(()) } else { Err(self.errors) } + } + + fn validate_module(&mut self) { + for (id, import) in self.module.imports.iter().enumerate() { + self.validate_import(id, import); + } + for export in &self.module.exports { + self.validate_export(export); + } + for segment in &self.module.data_segments { + if self.memory_type(segment.memory).is_none() { + self.error(format!("unknown memory index {} in data segment", segment.memory.0)); + } + let mut context = FunctionContext::constant_expression(); + if self.validate_sequence(&segment.offset, &mut context, vec![ValueType::I32]) != Some(vec![ValueType::I32]) + { + self.error("data segment offset must leave one i32 on the stack"); + } + } + for (defined_index, function) in self.module.functions.iter().enumerate() { + self.validate_function(defined_index, function); + } + } + + fn validate_import(&mut self, id: usize, import: &Import) { + match &import.desc { + ImportDesc::Function(type_id) => { + if self.type_(type_id).is_none() { + self.error(format!("import {id} references unknown function type {}", type_id.0)); + } + } + ImportDesc::Memory(memory) => self.validate_limits( + memory.minimum_pages, + memory.maximum_pages, + format!("imported memory {id}"), + ), + ImportDesc::Table(table) => { + self.validate_limits(table.minimum, table.maximum, format!("imported table {id}")) + } + } + } + + fn validate_export(&mut self, export: &Export) { + match export.desc { + ExportDesc::Function(id) => { + if self.function_type(id).is_none() { + self.error(format!( + "export `{}` references unknown function index {}", + export.name, id.0 + )); + } + } + ExportDesc::Memory(id) => { + if self.memory_type(id).is_none() { + self.error(format!( + "export `{}` references unknown memory index {}", + export.name, id.0 + )); + } + } + ExportDesc::Table(id) => { + if self.table_type(id).is_none() { + self.error(format!( + "export `{}` references unknown table index {}", + export.name, id.0 + )); + } + } + } + } + + fn validate_function(&mut self, defined_index: usize, function: &Function) { + let Some(type_) = self.type_(&function.type_id).cloned() else { + self.error(format!( + "function {defined_index} references unknown type {}", + function.type_id.0 + )); + return; + }; + + let params = function.params.iter().map(|local| local.type_).collect::>(); + if params != type_.params { + self.error(format!( + "function {defined_index} params {:?} do not match signature {:?}", + params, type_.params + )); + } + + let mut context = FunctionContext::new( + function + .params + .iter() + .chain(&function.locals) + .map(|local| local.type_) + .collect(), + type_.results.clone(), + ); + let actual = self.validate_sequence(&function.body, &mut context, type_.results.clone()); + if let Some(actual) = actual + && actual != type_.results + { + self.error(format!( + "function {defined_index} leaves stack {:?}, expected {:?}", + actual, type_.results + )); + } + } + + fn validate_sequence( + &mut self, instructions: &[Instruction], context: &mut FunctionContext, expected: Vec, + ) -> Option> { + self.validate_sequence_from(instructions, context, Vec::new(), expected) + } + + fn validate_sequence_from( + &mut self, instructions: &[Instruction], context: &mut FunctionContext, mut stack: Vec, + expected: Vec, + ) -> Option> { + let mut unreachable = false; + for instruction in instructions { + if unreachable { + stack.clear(); + } + if !self.validate_instruction(instruction, context, &mut stack, unreachable) { + unreachable = true; + } + } + + if unreachable { Some(expected) } else { Some(stack) } + } + + fn validate_instruction( + &mut self, instruction: &Instruction, context: &mut FunctionContext, stack: &mut Vec, + unreachable: bool, + ) -> bool { + if !self.validate_instruction_references(instruction, context) { + return true; + } + + match instruction { + Instruction::Block { type_, body } => { + self.validate_block_type(type_); + if !self.pop_types(stack, &type_.params, instruction, unreachable) { + return true; + } + context.labels.push(LabelType { branch_results: type_.results.clone() }); + let actual = self.validate_sequence_from(body, context, type_.params.clone(), type_.results.clone()); + context.labels.pop(); + if let Some(actual) = actual + && actual != type_.results + { + self.error(format!("block leaves stack {actual:?}, expected {:?}", type_.results)); + } + stack.extend(type_.results.clone()); + true + } + Instruction::Loop { type_, body } => { + self.validate_block_type(type_); + if !self.pop_types(stack, &type_.params, instruction, unreachable) { + return true; + } + context.labels.push(LabelType { branch_results: type_.params.clone() }); + let actual = self.validate_sequence_from(body, context, type_.params.clone(), type_.results.clone()); + context.labels.pop(); + if let Some(actual) = actual + && actual != type_.results + { + self.error(format!("loop leaves stack {actual:?}, expected {:?}", type_.results)); + } + stack.extend(type_.results.clone()); + true + } + Instruction::If { type_, then_body, else_body } => { + self.validate_block_type(type_); + let mut consumes = type_.params.clone(); + consumes.push(ValueType::I32); + if !self.pop_types(stack, &consumes, instruction, unreachable) { + return true; + } + + context.labels.push(LabelType { branch_results: type_.results.clone() }); + let then_actual = + self.validate_sequence_from(then_body, context, type_.params.clone(), type_.results.clone()); + let else_actual = + self.validate_sequence_from(else_body, context, type_.params.clone(), type_.results.clone()); + context.labels.pop(); + + if let Some(actual) = then_actual + && actual != type_.results + { + self.error(format!( + "if then branch leaves stack {actual:?}, expected {:?}", + type_.results + )); + } + if let Some(actual) = else_actual + && actual != type_.results + { + self.error(format!( + "if else branch leaves stack {actual:?}, expected {:?}", + type_.results + )); + } + stack.extend(type_.results.clone()); + true + } + Instruction::Br { depth, results } => { + self.validate_branch(*depth, results, context); + self.pop_types(stack, results, instruction, unreachable); + false + } + Instruction::BrIf { depth, results } => { + self.validate_branch(*depth, results, context); + self.apply_stack_effect(stack, instruction, unreachable); + true + } + Instruction::Return { results } => { + if results != &context.return_types { + self.error(format!( + "return has results {results:?}, expected {:?}", + context.return_types + )); + } + self.pop_types(stack, results, instruction, unreachable); + false + } + Instruction::Unreachable => false, + _ => { + self.apply_stack_effect(stack, instruction, unreachable); + true + } + } + } + + fn validate_instruction_references(&mut self, instruction: &Instruction, context: &FunctionContext) -> bool { + let mut valid = true; + match instruction { + Instruction::Call { function, type_ } => match self.function_type(*function) { + Some(actual) if actual == type_ => {} + Some(actual) => { + self.error(format!( + "call to function {} has signature {type_:?}, expected {actual:?}", + function.0 + )); + valid = false; + } + None => { + self.error(format!("call references unknown function index {}", function.0)); + valid = false; + } + }, + Instruction::CallIndirect { table, type_id, type_ } => { + if self.table_type(*table).is_none() { + self.error(format!("call_indirect references unknown table index {}", table.0)); + valid = false; + } + match self.type_(type_id) { + Some(actual) if actual == type_ => {} + Some(actual) => { + self.error(format!( + "call_indirect type {type_id:?} is {actual:?}, instruction uses {type_:?}" + )); + valid = false; + } + None => { + self.error(format!("call_indirect references unknown type index {}", type_id.0)); + valid = false; + } + } + } + Instruction::LocalGet { local, type_ } + | Instruction::LocalSet { local, type_ } + | Instruction::LocalTee { local, type_ } => match context.locals.get(local.0 as usize) { + Some(actual) if actual == type_ => {} + Some(actual) => { + self.error(format!( + "local {} has type {actual:?}, instruction uses {type_:?}", + local.0 + )); + valid = false; + } + None => { + self.error(format!("instruction references unknown local index {}", local.0)); + valid = false; + } + }, + Instruction::I32Load(arg) + | Instruction::I32Store(arg) + | Instruction::I64Load(arg) + | Instruction::I64Store(arg) + | Instruction::F64Load(arg) + | Instruction::F64Store(arg) + if self.memory_type(arg.memory).is_none() => + { + self.error(format!( + "memory instruction references unknown memory index {}", + arg.memory.0 + )); + valid = false; + } + _ => {} + } + valid + } + + fn apply_stack_effect(&mut self, stack: &mut Vec, instruction: &Instruction, unreachable: bool) { + let effect = instruction.stack_effect(); + if self.pop_types(stack, &effect.consumes, instruction, unreachable) { + stack.extend(effect.produces); + } + } + + fn pop_types( + &mut self, stack: &mut Vec, expected: &[ValueType], instruction: &Instruction, unreachable: bool, + ) -> bool { + if unreachable { + return true; + } + if stack.len() < expected.len() { + self.error(format!( + "stack underflow for {instruction:?}: expected {expected:?}, stack was {stack:?}" + )); + return false; + } + let start = stack.len() - expected.len(); + if stack[start..] != *expected { + self.error(format!( + "stack mismatch for {instruction:?}: expected top {expected:?}, stack was {stack:?}" + )); + return false; + } + stack.truncate(start); + true + } + + fn validate_block_type(&mut self, type_: &BlockType) { + if (type_.params.len() > 0 || type_.results.len() > 1) && self.block_type_id(type_).is_none() { + self.error(format!( + "block type params {:?} results {:?} must be present in the module type section", + type_.params, type_.results + )); + } + } + + fn block_type_id(&self, type_: &BlockType) -> Option { + let function_type = FunctionType::new(type_.params.clone(), type_.results.clone()); + self.module + .types + .iter() + .position(|candidate| candidate == &function_type) + .map(|index| TypeId(index as u32)) + } + + fn validate_branch(&mut self, depth: u32, results: &[ValueType], context: &FunctionContext) { + match context.labels.iter().rev().nth(depth as usize) { + Some(label) if label.branch_results == results => {} + Some(label) => self.error(format!( + "branch depth {depth} has results {results:?}, expected {:?}", + label.branch_results + )), + None => self.error(format!("branch references unknown label depth {depth}")), + } + } + + fn validate_limits(&mut self, minimum: u32, maximum: Option, item: impl std::fmt::Display) { + if let Some(maximum) = maximum + && maximum < minimum + { + self.error(format!("{item} maximum {maximum} is below minimum {minimum}")); + } + } + + fn type_(&self, id: &TypeId) -> Option<&FunctionType> { + self.module.types.get(id.0 as usize) + } + + fn function_type(&self, id: FunctionId) -> Option<&FunctionType> { + let imported_count = self.module.imported_function_count(); + let index = id.0 as usize; + if index < imported_count { + self.module + .imports + .iter() + .filter_map(|import| match import.desc { + ImportDesc::Function(type_id) => self.type_(&type_id), + ImportDesc::Memory(_) | ImportDesc::Table(_) => None, + }) + .nth(index) + } else { + let function = self.module.functions.get(index - imported_count)?; + self.type_(&function.type_id) + } + } + + fn memory_type(&self, id: MemoryId) -> Option<&Memory> { + let imported_count = self.module.imported_memory_count(); + let index = id.0 as usize; + if index < imported_count { + self.module + .imports + .iter() + .filter_map(|import| match &import.desc { + ImportDesc::Memory(memory) => Some(memory), + ImportDesc::Function(_) | ImportDesc::Table(_) => None, + }) + .nth(index) + } else { + self.module.memories.get(index - imported_count) + } + } + + fn table_type(&self, id: TableId) -> Option<&Table> { + let imported_count = self.module.imported_table_count(); + let index = id.0 as usize; + if index < imported_count { + self.module + .imports + .iter() + .filter_map(|import| match &import.desc { + ImportDesc::Table(table) => Some(table), + ImportDesc::Function(_) | ImportDesc::Memory(_) => None, + }) + .nth(index) + } else { + self.module.tables.get(index - imported_count) + } + } + + fn error(&mut self, message: impl Into) { + self.errors.push(ValidationError::new(message)); + } +} + +#[derive(Debug, Clone)] +struct FunctionContext { + locals: Vec, + return_types: Vec, + labels: Vec, +} + +impl FunctionContext { + fn new(locals: Vec, return_types: Vec) -> Self { + Self { locals, return_types: return_types.clone(), labels: vec![LabelType { branch_results: return_types }] } + } + + fn constant_expression() -> Self { + Self { locals: Vec::new(), return_types: Vec::new(), labels: Vec::new() } + } +} + +#[derive(Debug, Clone)] +struct LabelType { + branch_results: Vec, +} diff --git a/docs/src/internal/tasks/15_cli_and_build_outputs.md b/docs/src/internal/tasks/15_cli_and_build_outputs.md index 519eef2..c10392e 100644 --- a/docs/src/internal/tasks/15_cli_and_build_outputs.md +++ b/docs/src/internal/tasks/15_cli_and_build_outputs.md @@ -34,10 +34,10 @@ Make the CLI compile projects and produce useful artifacts. - [x] Define compiler-owned Wasm module, import, function, local, memory, table, export, data segment, and custom-section data structures. - [x] Define typed Wasm instruction enums with explicit operand-stack effects. -- [ ] Add a validation pass for instruction stack effects, branch result types, +- [x] Add a validation pass for instruction stack effects, branch result types, local indices, function signatures, and call signatures. -- [ ] Emit Wasm bytes from the structured module without going through WAT. -- [ ] Keep optional WAT output by rendering the structured module, not by using +- [x] Emit Wasm bytes from the structured module without going through WAT. +- [x] Keep optional WAT output by rendering the structured module, not by using handwritten backend strings as the source of truth. - [ ] Move scalar operations, direct calls, branches, locals, and exports to the structured builder.