[haskell-llvm] Generating function prototype and function call

José Romildo Malaquias j.romildo at gmail.com
Thu Nov 22 18:28:10 GMT 2012


Hello.

In the toy compiler I am writing I have the following (simplified) types
to represent the absract syntax tree:

  data Type = INT
            | LOG
            | FUN [Type] Type
            | UNIT

  type Name = String

  data TE = TE Type Exp

  data Exp = IntE Int32
           | LogE Bool
           | CallE Name [TE]

The base environment has bindings for the functions from the standard
library:

  baseEnv = [(Name,Type)]
  baseEnv = [ ("print",FUN [INT] UNIT) ]

The code generator is called after semantic analysis. That means the
program being compiled is error free.

The code generator should build declarations for the functions on the
base environment, and the definition of the main function containing an
expression.

  cgBaseEnv = mapM cgPrototype baseEnv

  cgPrototype (name, FUN formals result) =
    -- INCOMPLETE: build the function prototype

  cg env (TE t e) =
    case e of
      IntE n -> return n
      LogE b -> return b
      CallE n args ->
        do actuals <- mapM (cg env) args
           let Just f = lookup n env
           -- INCOMPLETE: apply f actuals

  codegen prog =
    do env <- cgBaseEnv
       cg env prog

I am clueless on how to complete the code generator using the high level
LLVM bidings api to Haskell.

I have used a similar structure when implementing the compiler in OCaml
using the default OCaml bindings distributed with LLVM.

Any help is welcome.

Romildo



More information about the Haskell-llvm mailing list