3

我一直在玩 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)

然而,它却假装对象是空的……这是为什么呢?

4

1 回答 1

3

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)
  )
)
于 2017-04-19T19:49:09.260 回答