开发者

Exporting C++ Classes to LLVM bitcode

is there any possibility to compile a C++ class into开发者_Go百科 LLVM bitcode? Whenever I compile a class like this

class MyClass {
public:
MyClass {};
int i() { return 0; };
};

using clang -emit-llvm -c MyClass.cpp -o MyClass.bc the resulting bitcode file seems to be empty: llvm-nm MyClass.bc does not return anything.

Is there any way to make this work?

Cheers,

Manuel


nm doesn't return anything because you're not instantiating any objects. Your whole code is optimized out. Add this to your code and you'll see it built:

class MyClass {
public:
    MyClass() {};
    int i() { return 0; };
    int j(int x);
};

int MyClass::j(int x) {
    return x + 2;
}

Now you have something to build

$ clang -emit-llvm -c class.cpp
$ nm class.o
---------------- T __ZN7MyClass1jEi
$ clang -emit-llvm -S class.cpp
$ cat class.s
; ModuleID = 'class.cpp'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-apple-macosx10.6.7"

%class.MyClass = type { i8 }

define i32 @_ZN7MyClass1jEi(%class.MyClass* %this, i32 %x) nounwind ssp align 2 {
  %1 = alloca %class.MyClass*, align 8
  %2 = alloca i32, align 4
  store %class.MyClass* %this, %class.MyClass** %1, align 8
  store i32 %x, i32* %2, align 4
  %3 = load %class.MyClass** %1
  %4 = load i32* %2, align 4
  %5 = add nsw i32 %4, 2
  ret i32 %5
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜