Permalink
Browse files

still trying to get the fgets example working

  • Loading branch information...
1 parent 8db93b8 commit 10db9513e6a82f41c89e7a92ecf99f3eac2cb0aa @duncantl committed Apr 17, 2013
Showing with 77 additions and 6 deletions.
  1. +8 −0 Todo.xml
  2. +25 −5 explorations/fgets.Rdb
  3. +43 −0 simpleCall.R
  4. +1 −1 src/Function.cpp
View
8 Todo.xml
@@ -17,6 +17,14 @@ This will change in the future.
<items>
+
+<item>
+When the routine's return type is VoidType, make certain
+we get a terminator.
+<br/>
+See fgets.Rdb and change the return type to VoidType
+</item>
+
<item>
Should m[["notexists"]] for a Module m raise an error or return NULL.
</item>
View
30 explorations/fgets.Rdb
@@ -34,33 +34,46 @@ library(RLLVMCompile)
mod = Module("fgets")
ee = ExecutionEngine(mod)
-stringPointerType = pointerType(getIntegerType(8L))
+stringType = pointerType(getIntegerType(8L, Rllvm:::getContext(mod)))
FILEType = pointerType(Int32Type) # an opaque struct type?
.str = createGlobalVariable(".str", mod, val = string(1000), linkage = PrivateLinkage)
# we'd like to be able to use .str and have R functions call getGetElementPtr for us.
p = getGetElementPtr(.str)
-createGlobalVariable("ptr", mod, stringPointerType, p)
+.ptr = createGlobalVariable("ptr", mod, stringType, p)
# could specify this information via .routineInfo parameter of compileFunction()
-declareFunction(list(stringPointerType, stringPointerType, Int32Type, FILEType), "fgets", mod)
+declareFunction(list(stringType, stringType, Int32Type, FILEType), "t_fgets", mod)
</r:code>
<r:code>
Fgets =
function(file)
{
- fgets(ptr, 1000L, file) # != NULL # should raise an error if NULL.
+ t_fgets(ptr, 1000L, file) # != NULL # should raise an error if NULL.
+ 1L
}
</r:code>
Now we can compile the function
+<r:code eval="false">
+compileFunction(Fgets, stringType, list(FILEType), mod, name = "Fgets")
+</r:code>
<r:code>
-compileFunction(Fgets, stringPointerType, list(FILEType), mod, name = "Fgets")
+fun = compileFunction(Fgets, Int32Type, list(FILEType), mod, name = "Fgets")
</r:code>
+<q>
+This claims there is a mismatch in the argument types.
+In ../simpleCall.R, we go through each of the parameters and see
+which fail. The problem appears that the type of the global variable
+ptr is not the same as stringType.
+Is it that we are not loading the variable?
+</q>
+
+
Note that we could use <r:var>.str</r:var> rather than <r:var>ptr</r:var> in the call to
<r:func>fgets</r:func> and our compiler should figure out to use
the pointer to <r:var>.str</r:var> via a <r:func>getGetElementPtr</r:func> call.
@@ -69,6 +82,13 @@ the pointer to <r:var>.str</r:var> via a <r:func>getGetElementPtr</r:func> call.
<para>
+<r:code eval="false" id="test">
+dyn.load("explorations/testFgets.so")
+llvmAddSymbol(t_fgets = getNativeSymbolInfo("t_fgets")$address)
+library(RCurl)
+f = CFILE("DESCRIPTION", "r")
+.llvm(fun, f@ref)
+</r:code>
</para>
View
43 simpleCall.R
@@ -0,0 +1,43 @@
+library(RLLVMCompile)
+
+mod = Module("foo")
+FILEType = pointerType(Int32Type) # an opaque struct type?
+declareFunction(list(Int32Type, FILEType), "foo", mod)
+
+bar =
+function(f)
+ foo(f) + 1L
+
+compileFunction(bar, Int32Type, list(FILEType), mod, name = "bar")
+
+
+################
+
+library(RLLVMCompile)
+
+mod = Module("foo")
+FILEType = pointerType(Int32Type) # an opaque struct type?
+declareFunction(list(Int32Type, Int32Type, FILEType), "foo", mod)
+
+bar =
+function(f)
+ foo(2L, f) + 1L
+
+compileFunction(bar, Int32Type, list(FILEType), mod, name = "bar")
+
+######################
+
+library(RLLVMCompile)
+
+mod = Module("foo")
+FILEType = pointerType(Int32Type) # an opaque struct type?
+strType = pointerType(getIntegerType(8, Rllvm:::getContext(mod)))
+declareFunction(list(Int32Type, strType, Int32Type, FILEType), "foo", mod)
+
+bar =
+function(f, s)
+ foo(s, 2L, f) + 1L
+
+compileFunction(bar, Int32Type, list(FILEType, strType), mod, name = "bar")
+
+# So in fgets.Rdb, somehow the pointer type is not
View
2 src/Function.cpp
@@ -139,4 +139,4 @@ R_Function_getReturnType(SEXP r_func)
{
llvm::Function *func = GET_REF(r_func, Function);
return(R_createRef(func->getReturnType(), "Type"));
-}
+}

0 comments on commit 10db951

Please sign in to comment.