|
1 | 1 | use std::str;
|
2 | 2 |
|
3 |
| -use wasmparser::CustomSectionKind; |
| 3 | +use wasmparser::{CustomSectionKind, TypeOrFuncType}; |
4 | 4 | use wasmparser::ExternalKind;
|
5 | 5 | use wasmparser::FuncType;
|
6 | 6 | use wasmparser::ImportSectionEntryType;
|
@@ -63,6 +63,32 @@ impl Global {
|
63 | 63 | },
|
64 | 64 | }
|
65 | 65 | }
|
| 66 | + |
| 67 | + pub fn in_memory_size(&self) -> usize { |
| 68 | + let typ = match self { |
| 69 | + Global::Imported { |
| 70 | + content_type, |
| 71 | + .. |
| 72 | + } => content_type, |
| 73 | + Global::InModule { |
| 74 | + content_type, |
| 75 | + .. |
| 76 | + } => content_type |
| 77 | + }; |
| 78 | + |
| 79 | + match typ { |
| 80 | + Type::I32 => 4, |
| 81 | + Type::I64 => 8, |
| 82 | + Type::F32 => 4, |
| 83 | + Type::F64 => 8, |
| 84 | + Type::V128 => 16, |
| 85 | + Type::AnyFunc => 4, |
| 86 | + Type::AnyRef => 8, |
| 87 | + Type::Func => 4, |
| 88 | + Type::EmptyBlockType => 0, |
| 89 | + Type::Null => 0, |
| 90 | + } |
| 91 | + } |
66 | 92 | }
|
67 | 93 |
|
68 | 94 | #[derive(Clone, Debug)]
|
@@ -217,8 +243,8 @@ pub struct TableInitializer {
|
217 | 243 |
|
218 | 244 | #[derive(Clone, Debug, PartialEq)]
|
219 | 245 | pub enum Instruction {
|
220 |
| - BlockStart { produced_type: Option<Type> }, |
221 |
| - LoopStart { produced_type: Option<Type> }, |
| 246 | + BlockStart { produced_type: Option<TypeOrFuncType> }, |
| 247 | + LoopStart { produced_type: Option<TypeOrFuncType> }, |
222 | 248 | End,
|
223 | 249 |
|
224 | 250 | Br { depth: u32 },
|
@@ -418,15 +444,15 @@ impl<'a> From<&'a Operator<'a>> for Instruction {
|
418 | 444 | fn from(o: &Operator<'a>) -> Self {
|
419 | 445 | match *o {
|
420 | 446 | Operator::Block { ty } => {
|
421 |
| - let produced_type = if ty == Type::EmptyBlockType { |
| 447 | + let produced_type = if ty == TypeOrFuncType::Type(Type::EmptyBlockType) { |
422 | 448 | None
|
423 | 449 | } else {
|
424 | 450 | Some(ty)
|
425 | 451 | };
|
426 | 452 | Instruction::BlockStart { produced_type }
|
427 | 453 | }
|
428 | 454 | Operator::Loop { ty } => {
|
429 |
| - let produced_type = if ty == Type::EmptyBlockType { |
| 455 | + let produced_type = if ty == TypeOrFuncType::Type(Type::EmptyBlockType) { |
430 | 456 | None
|
431 | 457 | } else {
|
432 | 458 | Some(ty)
|
@@ -801,6 +827,25 @@ impl WasmModule {
|
801 | 827 | m
|
802 | 828 | }
|
803 | 829 |
|
| 830 | + pub fn log_diagnostics(&self) { |
| 831 | + let global_variable_memory_use: usize = self.globals.iter().map(|g| g.in_memory_size()).sum(); |
| 832 | + info!("Globals taking up {} bytes", global_variable_memory_use); |
| 833 | + |
| 834 | + let mut data_initializer_memory_use = 0; |
| 835 | + for initializer in &self.data_initializers { |
| 836 | + for body_bytes in &initializer.body { |
| 837 | + data_initializer_memory_use += body_bytes.len(); |
| 838 | + } |
| 839 | + } |
| 840 | + info!("Data initializers taking up {} bytes", data_initializer_memory_use); |
| 841 | + |
| 842 | + let mut function_table_entries = 0; |
| 843 | + for initializer in &self.table_initializers { |
| 844 | + function_table_entries += initializer.function_indexes.len(); |
| 845 | + } |
| 846 | + info!("Function table entries {} (ignoring fragmentation)", function_table_entries); |
| 847 | + } |
| 848 | + |
804 | 849 | fn implement_function(&mut self, mut f: ImplementedFunction) {
|
805 | 850 | for existing_function in &mut self.functions {
|
806 | 851 | if let Some((ty, ty_index)) = existing_function.declared_but_unimplemented() {
|
|
0 commit comments