JuliaHubOSS / llvm-cbe

resurrected LLVM "C Backend", with improvements
Other
811 stars 138 forks source link

code generator doesnt output buildable code when using virtual classes #162

Closed mgood7123 closed 6 months ago

mgood7123 commented 1 year ago
export RELVER := 1.0.0

release: /usr/bin/clang IR_TO_C__build/tools/llvm-cbe/llvm-cbe
    clang -S -emit-llvm test_1.cpp -o - | IR_TO_C__build/tools/llvm-cbe/llvm-cbe -o test_1.c
    gcc test_1.c -o test_1.exe

/usr/bin/clang:
    sudo apt install -y clang

/usr/bin/llvm-ar:
    sudo apt install -y llvm

/usr/include/llvm:
    sudo apt install -y llvm-dev

IR_TO_C__build:
    mkdir IR_TO_C__build

llvm-cbe:
    git clone https://github.com/JuliaComputingOSS/llvm-cbe

IR_TO_C__build/tools/llvm-cbe/llvm-cbe: /usr/bin/llvm-ar /usr/include/llvm IR_TO_C__build llvm-cbe
    bash -c "cd IR_TO_C__build; cmake -S ../llvm-cbe"
    bash -c "cd IR_TO_C__build; make llvm-cbe"

clean:
    rm -f *.c
    rm -f *.exe
class A {
public:
    virtual int foo() {
        return 1;
    }
};

class B : public A {
public:
    int foo() override {
        return 2;
    }
};

int main() {
    A a;
    B b;
    return a.foo() + b.foo();
}
neon@neon:/CPP_TO_C$ make
clang -S -emit-llvm test_1.cpp -o - | IR_TO_C__build/tools/llvm-cbe/llvm-cbe -o test_1.c
gcc test_1.c -o test_1.exe
test_1.c:95:101: error: ‘_ZTI1A’ undeclared here (not in a function); did you mean ‘_ZTV1A’?
   95 | const struct l_unnamed_1 _ZTV1A __attribute__((common)) = { { { ((uint8_t*)/*NULL*/0), ((uint8_t*)(&_ZTI1A)), ((uint8_t*)_ZN1A3fooEv) } } };
      |                                                                                                     ^~~~~~
      |                                                                                                     _ZTV1A
test_1.c:97:134: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   97 | truct l_unnamed_2 _ZTI1A __attribute__((common)) = { ((uint8_t*)((&(&_ZTVN10__cxxabiv117__class_type_infoE)[((int64_t)2)]))), ((&_ZTS1A.array[((int32_t)0)])) };
      |                                                                                                                               ^

test_1.c:98:101: error: ‘_ZTI1B’ undeclared here (not in a function); did you mean ‘_ZTV1B’?
   98 | const struct l_unnamed_1 _ZTV1B __attribute__((common)) = { { { ((uint8_t*)/*NULL*/0), ((uint8_t*)(&_ZTI1B)), ((uint8_t*)_ZN1B3fooEv) } } };
      |                                                                                                     ^~~~~~
      |                                                                                                     _ZTV1B
test_1.c:100:137: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  100 | ct l_unnamed_3 _ZTI1B __attribute__((common)) = { ((uint8_t*)((&(&_ZTVN10__cxxabiv120__si_class_type_infoE)[((int64_t)2)]))), ((&_ZTS1B.array[((int32_t)0)])), ((uint8_t*)(&_ZTI1A)) };
      |                                                                                                                               ^

make: *** [makefile:5: release] Error 1
neon@neon:/CPP_TO_C$