【发布时间】:2022-01-22 09:13:16
【问题描述】:
WASM 文本格式有结构吗?
(module
(type (; can a type be a `struct` like in C or rust? ;) )
(; rest of module ;)
)
我使用this WasmExplorer tool将以下c++编译成wasm
struct MyStruct {
int MyField;
long MyOtherField;
};
MyStruct returnMyStruct(int myField){
return MyStruct {
MyField: myField,
MyOtherField: myField * 2
};
}
它输出以下内容,但我无法理解 WASM 在做什么。
(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "_Z14returnMyStructi" (func $_Z14returnMyStructi))
(func $_Z14returnMyStructi (; 0 ;) (param $0 i32) (param $1 i32)
(i32.store
(get_local $0)
(get_local $1)
)
(i32.store offset=4
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
)
生成的函数没有返回类型,它使用i32.store 和i32.shl 以及偏移量。它是否将结构存储在内存中的某个地方?
非常感谢您解释它的工作原理和原因。
【问题讨论】:
-
你处理过组装吗?我的 2 美分是你不应该太在意输出的 WASM。它被设计为永远不会直接编写。
-
我想写一个直接写 WASM 的编译器,用于教育目的。因此,我只关心 WASM,而不关心 C++
标签: c++ webassembly