gpt4 book ai didi

WebAssembly 不能编译简单的类?

转载 作者:行者123 更新时间:2023-12-05 04:11:00 25 4
gpt4 key购买 nike

我一直在使用 WebAssembly Explorer 来适应一般概念,我相信我得到了错误的输出:

C++代码:

class Rectangle {
void draw(int fooBar) {};
};

webassembly 的输出:

(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
)

老实说,这看起来不对。为什么不显示功能?我实际上希望资源管理器导出一个可能看起来像这样的函数:

  (export "_Z4drawi" (func $_Z4drawi))
(func $_Z4drawi (param $0 i32)

相反,它会假装对象是空的……这是为什么呢?

最佳答案

LLVM 正在删除您的函数,因为它未被使用。

尝试使用它并使其成为非内联的(以防止它也被淘汰):

class Rectangle {
public:
Rectangle() {}
__attribute__((noinline)) void draw(int fooBar) {}
};

int main() {
Rectangle r;
r.draw(42);
}

你得到:

(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "main" (func $main))
(func $main (result i32)
(local $0 i32)
(i32.store offset=4
(i32.const 0)
(tee_local $0
(i32.sub
(i32.load offset=4
(i32.const 0)
)
(i32.const 16)
)
)
)
(call $_ZN9Rectangle4drawEi
(i32.add
(get_local $0)
(i32.const 8)
)
(i32.const 42)
)
(i32.store offset=4
(i32.const 0)
(i32.add
(get_local $0)
(i32.const 16)
)
)
(i32.const 0)
)
(func $_ZN9Rectangle4drawEi (param $0 i32) (param $1 i32)
)
)

使用 no-inline 是一种围绕优化琐碎代码的技巧。您也可以将其标记为已使用:

class Rectangle {
public:
Rectangle() {}
__attribute__((used)) void draw(int fooBar) {}
};

然后你会得到:

(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(func $_ZN9Rectangle4drawEi (param $0 i32) (param $1 i32)
)
)

关于WebAssembly 不能编译简单的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43504753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com