20

So I now have a fairly complete LISP (scheme) interpreter written in haskell. Just for fun I want to try to have it compile down to LLVM. Most of the code generation seems pretty straight forward, but I'm at a loss as to how to generate code for a lambda expression (kind of important in lisp ;) ) and how to manage the heap when I encounter a define expression.

How might I generated code for these expressions?

Note: I can generate code for the body of the lambda expression, What is confusing me is how to "put" that code somewhere and make it callable.

John F. Miller
  • 26,961
  • 10
  • 71
  • 121
  • 2
    You may want to implement an explicit lambda lifting pass prior to your code generation (and since it is Scheme, you're likely to be doing a CPS-transform prior to that). It will leave you with only the global functions and an explicit closure envoronments allocation. – SK-logic Jun 16 '11 at 16:20

1 Answers1

10

See Lennart's blog post: http://augustss.blogspot.com/2009/06/more-llvm-recently-someone-asked-me-on.html

Look at the compileFunction function. In particular, newFunction in the LLVM core: http://hackage.haskell.org/packages/archive/llvm/0.9.1.2/doc/html/LLVM-Core.html#g:23

fuz
  • 88,405
  • 25
  • 200
  • 352
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • The language that is compiled in the blog post is first order and doesn't handle closures at all. I don't see how that is helpful for compiling scheme lambdas. – sepp2k Jun 15 '11 at 19:17
  • 1
    @sepp2k You'll need to do closure conversion first, which I assume John is doing, since he's reading SICP. – Don Stewart Jun 15 '11 at 19:22
  • I could be wrong, but I don't remember closure conversion being covered in SICP. – okonomichiyaki Jun 15 '11 at 20:14