diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3dce254 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +sum +*.o +*.bc diff --git a/Makefile b/Makefile index 90af0dd..9f15a6d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=clang CFLAGS=-g `llvm-config --cflags` LD=clang++ -LDFLAGS=`llvm-config --cxxflags --ldflags --libs core executionengine jit interpreter analysis native bitwriter --system-libs` +LDFLAGS=`llvm-config --cxxflags --ldflags --libs core executionengine mcjit interpreter analysis native bitwriter --system-libs` all: sum diff --git a/sum.c b/sum.c index 7fce959..25b6fb6 100644 --- a/sum.c +++ b/sum.c @@ -16,6 +16,8 @@ #include #include +typedef int32_t (*funcPtr_t) (int32_t, int32_t); + int main(int argc, char const *argv[]) { LLVMModuleRef mod = LLVMModuleCreateWithName("my_module"); @@ -36,8 +38,10 @@ int main(int argc, char const *argv[]) { LLVMExecutionEngineRef engine; error = NULL; - LLVMLinkInJIT(); + LLVMLinkInMCJIT(); LLVMInitializeNativeTarget(); + LLVMInitializeNativeAsmPrinter(); + LLVMInitializeNativeAsmParser(); if (LLVMCreateExecutionEngineForModule(&engine, mod, &error) != 0) { fprintf(stderr, "failed to create execution engine\n"); abort(); @@ -52,15 +56,13 @@ int main(int argc, char const *argv[]) { fprintf(stderr, "usage: %s x y\n", argv[0]); exit(EXIT_FAILURE); } - long long x = strtoll(argv[1], NULL, 10); - long long y = strtoll(argv[2], NULL, 10); + int32_t x = strtoll(argv[1], NULL, 10); + int32_t y = strtoll(argv[2], NULL, 10); - LLVMGenericValueRef args[] = { - LLVMCreateGenericValueOfInt(LLVMInt32Type(), x, 0), - LLVMCreateGenericValueOfInt(LLVMInt32Type(), y, 0) - }; - LLVMGenericValueRef res = LLVMRunFunction(engine, sum, 2, args); - printf("%d\n", (int)LLVMGenericValueToInt(res, 0)); + { + funcPtr_t funcPtr = (funcPtr_t)LLVMGetPointerToGlobal(engine, sum); + printf("%d\n", funcPtr(x,y)); + } // Write out bitcode to file if (LLVMWriteBitcodeToFile(mod, "sum.bc") != 0) {