summaryrefslogtreecommitdiff
path: root/test cases/frameworks/15 llvm
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2017-04-13 14:46:36 -0700
committerDylan Baker <dylan@pnwbakers.com>2017-05-03 14:35:38 -0700
commit4bee51655bea9c8bebd3c55414d7daf13591fb59 (patch)
tree427a07b1c87dcfc7c9ccdead7b634ff23fa9a496 /test cases/frameworks/15 llvm
parent4334c960624f2981e536a46697e429a43fc19f36 (diff)
downloadmeson-4bee51655bea9c8bebd3c55414d7daf13591fb59.tar.gz
Add dependency for LLVM. Fixes #1611
This adds a depdendncy wrapper for llvm-config based on the wxwidgets dependency. IT handles libs, version, include dir, and the llvm unique concept of components. These components are individual pieces of the LLVM library that may or may not be available depending on the platform.
Diffstat (limited to 'test cases/frameworks/15 llvm')
-rw-r--r--test cases/frameworks/15 llvm/meson.build10
-rw-r--r--test cases/frameworks/15 llvm/sum.c76
2 files changed, 86 insertions, 0 deletions
diff --git a/test cases/frameworks/15 llvm/meson.build b/test cases/frameworks/15 llvm/meson.build
new file mode 100644
index 000000000..582ff3740
--- /dev/null
+++ b/test cases/frameworks/15 llvm/meson.build
@@ -0,0 +1,10 @@
+project('llvmtest', ['c', 'cpp'], default_options : ['c_std=c99'])
+
+llvm_dep = dependency(
+ 'llvm',
+ modules : ['bitwriter', 'asmprinter', 'executionengine', 'target',
+ 'mcjit', 'nativecodegen'],
+ required : true,
+)
+
+executable('sum', 'sum.c', dependencies : llvm_dep)
diff --git a/test cases/frameworks/15 llvm/sum.c b/test cases/frameworks/15 llvm/sum.c
new file mode 100644
index 000000000..a93588e5e
--- /dev/null
+++ b/test cases/frameworks/15 llvm/sum.c
@@ -0,0 +1,76 @@
+/** This code is public domain, and taken from
+ * https://github.com/paulsmith/getting-started-llvm-c-api/blob/master/sum.c
+ */
+/**
+ * LLVM equivalent of:
+ *
+ * int sum(int a, int b) {
+ * return a + b;
+ * }
+ */
+
+#include <llvm-c/Core.h>
+#include <llvm-c/ExecutionEngine.h>
+#include <llvm-c/Target.h>
+#include <llvm-c/Analysis.h>
+#include <llvm-c/BitWriter.h>
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char const *argv[]) {
+ LLVMModuleRef mod = LLVMModuleCreateWithName("my_module");
+
+ LLVMTypeRef param_types[] = { LLVMInt32Type(), LLVMInt32Type() };
+ LLVMTypeRef ret_type = LLVMFunctionType(LLVMInt32Type(), param_types, 2, 0);
+ LLVMValueRef sum = LLVMAddFunction(mod, "sum", ret_type);
+
+ LLVMBasicBlockRef entry = LLVMAppendBasicBlock(sum, "entry");
+
+ LLVMBuilderRef builder = LLVMCreateBuilder();
+ LLVMPositionBuilderAtEnd(builder, entry);
+ LLVMValueRef tmp = LLVMBuildAdd(builder, LLVMGetParam(sum, 0), LLVMGetParam(sum, 1), "tmp");
+ LLVMBuildRet(builder, tmp);
+
+ char *error = NULL;
+ LLVMVerifyModule(mod, LLVMAbortProcessAction, &error);
+ LLVMDisposeMessage(error);
+
+ LLVMExecutionEngineRef engine;
+ error = NULL;
+ LLVMLinkInMCJIT();
+ LLVMInitializeNativeAsmPrinter();
+ LLVMInitializeNativeTarget();
+ if (LLVMCreateExecutionEngineForModule(&engine, mod, &error) != 0) {
+ fprintf(stderr, "failed to create execution engine\n");
+ abort();
+ }
+ if (error) {
+ fprintf(stderr, "error: %s\n", error);
+ LLVMDisposeMessage(error);
+ exit(EXIT_FAILURE);
+ }
+
+ if (argc < 3) {
+ 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);
+
+ LLVMGenericValueRef args[] = {
+ LLVMCreateGenericValueOfInt(LLVMInt32Type(), x, 0),
+ LLVMCreateGenericValueOfInt(LLVMInt32Type(), y, 0)
+ };
+ LLVMGenericValueRef res = LLVMRunFunction(engine, sum, 2, args);
+ printf("%d\n", (int)LLVMGenericValueToInt(res, 0));
+
+ // Write out bitcode to file
+ if (LLVMWriteBitcodeToFile(mod, "sum.bc") != 0) {
+ fprintf(stderr, "error writing bitcode to file, skipping\n");
+ }
+
+ LLVMDisposeBuilder(builder);
+ LLVMDisposeExecutionEngine(engine);
+}