Problems with high-level interface to LLVM

Hi there, I use llvm package for building compiler for a small C-like language and it have some problems with high-level interface that package provides. I use simple scheme: [parser] -> (untyped AST) -> [analyser] -> (typed AST) -> [LLVM] -> (object code). At first, as I want to use high-level interface, typed AST must satisfy all needed class constraints. For simple types and operations this can be solved by adding neccessary constraints to the corresponding definitions. But it's not very good solution, because in general typed AST can represent high-level data structures, that would have certain representation, known when generating code, but not checking logic. Problem becomes much more complex when I want to add function calls into my language. Type class CallArgs has an instance CallArgs (IO a) (CodeGenFunction r (Value a)), which can't be given as a constraint in typed AST because of free variable r. For a long time I have had no idea of how I can add function calls into my language. The only solution I see at the moment is to reject high-level llvm interface provided by llvm library and use directly FFI bindings to C. But I can't get rid of feeling that I'm missing something or doing all stuff totally wrong. It would be great if someone, who has experience in using high-level interface to LLVM for building a compiler, could help me. Thanks in advance, Nick

Николай Кудасов wrote:
Problem becomes much more complex when I want to add function calls into my language. Type class CallArgs has an instance CallArgs (IO a) (CodeGenFunction r (Value a)), which can't be given as a constraint in typed AST because of free variable r. For a long time I have had no idea of how I can add function calls into my language.
RankNTypes? First I was also annoyed by this 'r' type parameter. But I think it is there in order to assert that LLVM code blocks have the same return type in 'ret' instructions when composing them.
The only solution I see at the moment is to reject high-level llvm interface provided by llvm library and use directly FFI bindings to C.
I have not tried, but I would also think that the strongly typed llvm binding makes writing a compiler hard. However I enjoy it for just-in-time compilation and building an embedded domain specific language. For writing a compiler you might prefer the LLVM bindings that are created by David Terei for GHC's LLVM backend. PS: Also posted to haskell-llvm mailing list.
participants (2)
-
Henning Thielemann
-
Николай Кудасов