Skip to content
This repository was archived by the owner on Jul 9, 2020. It is now read-only.

update sum.c to LLVM-3.6 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 10 additions & 9 deletions sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,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();
Expand All @@ -52,15 +54,14 @@ 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));
{
int32_t (*funcPtr) (int32_t, int32_t)
= LLVMGetPointerToGlobal(engine, sum);
printf("%d\n", funcPtr(x,y));
}

// Write out bitcode to file
if (LLVMWriteBitcodeToFile(mod, "sum.bc") != 0) {
Expand Down