Skip to content

LLVM 5.0 Error Calling Function #45

@coffee-cup

Description

@coffee-cup

I am following along with the tutorial using llvm-hs-5.1.0 and llvm-hs-pure-5.1.0 and with the exception of a few minor issues between the versions, most things are working correctly.

However, I am stuck on an error I am getting when trying to call a previously defined function.

Using the code up to chapter 3 I can do

ready> def foo(a) a;
; ModuleID = 'my cool jit'
source_filename = \"<string>\"

define double @foo(double %a) {
  entry:
  %0 = alloca double
  store double %a, double* %0
  %1 = load double, double* %0
  ret double %1
}

ready> def bar() foo(3);
EncodeException "The serialized GlobalReference has type 

PointerType {
  pointerReferent = FunctionType {
    resultType = FloatingPointType { floatingPointType = DoubleFP }, 
    argumentTypes = [ FloatingPointType { floatingPointType = DoubleFP } ], 
    isVarArg = False
  }, 
  pointerAddrSpace = AddrSpace 0
} 

but should have type 

FloatingPointType { floatingPointType = DoubleFP }"

I believe the problem is from trying to create the constant global reference operand in the externf function.

I can fix the error when supplying a type

-- Codegen.hs
externf :: Type -> Name -> Operand
externf ty nm = ConstantOperand (C.GlobalReference ty nm)

-- Emit.hs
cgen (S.Call fn args) = do
  largs <- mapM cgen args
  let fnT = AST.PointerType { AST.pointerReferent = (AST.FunctionType { AST.resultType = double, AST.argumentTypes = [], AST.isVarArg = False}) , AST.pointerAddrSpace = AddrSpace 0 }
  let oper = externf fnT (AST.Name fn)
  call oper largs

However, this is very specific to this exact function. Preferably, the type would be determined automatically.

So in summary, how can I find the type of the function I am calling so I can pass it into externf?

Note: This error has been added in llvm-hs 5.0 https://github.com/llvm-hs/llvm-hs/blob/4b8269a3be55e84a75de41cdc481c589d161b7f3/llvm-hs/test/LLVM/Test/Regression.hs#L98

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions