From 2d654ff47a51884cc4f73cc51b4bf6c06fce9839 Mon Sep 17 00:00:00 2001
From: Jan Hoenig <hrominium@gmail.com>
Date: Mon, 13 Mar 2017 19:13:56 +0100
Subject: [PATCH] backup

---
 backends/llvm.py |  8 +++-----
 llvm/jit.py      | 22 ++++++++++++++++++++--
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/backends/llvm.py b/backends/llvm.py
index b97f1c4e5..330dbb032 100644
--- a/backends/llvm.py
+++ b/backends/llvm.py
@@ -10,12 +10,10 @@ from ..types import DataType
 from ..astnodes import Indexed
 
 
-def generateLLVM(ast_node):
+def generateLLVM(ast_node, module=ir.Module(), builder=ir.IRBuilder()):
     """
     Prints the ast as llvm code
     """
-    module = ir.Module()
-    builder = ir.IRBuilder()
     printer = LLVMPrinter(module, builder)
     return printer._print(ast_node)
 
@@ -111,10 +109,10 @@ class LLVMPrinter(Printer):
 
     def _print_KernelFunction(self, function):
         return_type = self.void
-        # TODO argument in their own call?
+        # TODO argument in their own call? -> nope
         parameter_type = []
         for parameter in function.parameters:
-            # TODO what bout ptr shape and stride argument?
+            # TODO what about ptr shape and stride argument?
             if parameter.isFieldArgument:
                 parameter_type.append(self.fp_pointer)
             else:
diff --git a/llvm/jit.py b/llvm/jit.py
index 2b13d7e7b..345161811 100644
--- a/llvm/jit.py
+++ b/llvm/jit.py
@@ -1,6 +1,9 @@
 import llvmlite.binding as llvm
+import llvmlite.ir as ir
 import logging.config
 
+import ctypes as ct
+
 logger = logging.getLogger(__name__)
 
 
@@ -65,7 +68,22 @@ class Eval(object):
             with open('gen.o', 'wb') as f:
                 f.write(target_machine.emit_object(llvmmod))
 
-            # fptr = CFUNCTYPE(c_double, c_double, c_double)(ee.get_function_address('add2'))
+            fptr = {}
+            # TODO cpujit has its own string version
+            types = {str(ir.DoubleType()): ct.c_double,
+                     str(ir.IntType(64)): ct.c_int64,  # TODO int width?
+                     str(ir.FloatType()): ct.c_float,
+                     str(ir.VoidType()): None,  # TODO thats a void pointer use None???
+                     str(ir.DoubleType().as_pointer()): ct.POINTER(ct.c_double),
+                     str(ir.IntType(64).as_pointer()): ct.POINTER(ct.c_int),  # TODO int width?
+                     str(ir.FloatType().as_pointer()): ct.POINTER(ct.c_float),
+                     #ir.VoidType().as_pointer(): ct.c_void_p,  # TODO there is no void pointer in llvmlite?
+                     }  # TODO Aggregate types
+            for function in module.functions:
+                if not function.is_declaration:
+                    print(function.name)
+                    print(type(function))
+                    fptr[function.name] = ct.CFUNCTYPE(types[str(function.ftype.return_type)], *[types[str(arg)] for arg in function.ftype.args])(ee.get_function_address(function.name))
             # result = fptr(2, 3)
             # print(result)
-            return 0
+            return fptr
-- 
GitLab