On Sunday 25 November 2007 05:28, Aaron Gray wrote:> > On Sunday 25 November 2007 03:42, Christopher Lamb wrote: > >> Try this google query. I know there's been some discussion/work on > >> OCaml and LLVM. > >> > >> site:lists.cs.uiuc.edu/pipermail/llvmdev OCaml interface > > > > I just rediscovered the OCaml bindings in bindings/ocaml (rather than the > > ones > > in test/Bindings/OCaml!). They do indeed look quite complete but I can't > > find > > any examples using them. I think a translation of the tutorial would be > > most > > welcome and about 10x shorter. ;-) > > Lexing is the one issue though.How do you mean? I'm just fiddling around with it now. The lexer, parser and AST written using camlp4 might look something like this in OCaml: type ast | Num of float | Var of string | BinOp of [ `Add | `Sub | `Mul | `Less ] * ast * ast | Call of string * ast list | Function of string * string list * ast open Camlp4.PreCast;; let expr = Gram.Entry.mk "expr" ;; EXTEND Gram expr: [ [ e1 = expr; "+"; e2 = expr -> BinOp(`Add, e1, e2) | e1 = expr; "-"; e2 = expr -> BinOp(`Sub, e1, e2) ] | [ e1 = expr; "*"; e2 = expr -> BinOp(`Mul, e1, e2) ] | [ e1 = expr; "<"; e2 = expr -> BinOp(`Less, e1, e2) ] | [ "("; e = expr; ")" -> e ] | [ f = STRING; "("; args = LIST0 expr; ")" -> Call(f, args) ] | [ "def"; f = STRING; "("; vars = LIST0 [ s = STRING -> s ]; ")"; body = expr -> Function(f, vars, body) ] | [ x = FLOAT -> Num(float_of_string x) ] | [ v = LIDENT -> Var v ] ]; END;; Probably better to use conventional lex and yacc though... -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/products/?e
>> Lexing is the one issue though. > > How do you mean?I think he's observing that a majority of the tutorial code is actually spent on the lexer and parser, not on the llvm-specific pieces.> I'm just fiddling around with it now. The lexer, parser and AST > written using > camlp4 might look something like this in OCaml:Sure, the existing tutorial would be shorter if the lexer/parser used lex/yacc too, but that wasn't the goal :). The goal was the make it as simple and easy for newbies to understand, even if they didn't have any previous compiler experience. -Chris
On Sunday 25 November 2007 21:00, Chris Lattner wrote:> >> Lexing is the one issue though. > > > > How do you mean? > > I think he's observing that a majority of the tutorial code is > actually spent on the lexer and parser, not on the llvm-specific pieces.Right.> > I'm just fiddling around with it now. The lexer, parser and AST > > written using camlp4 might look something like this in OCaml: > > Sure, the existing tutorial would be shorter if the lexer/parser used > lex/yacc too, but that wasn't the goal :). The goal was the make it > as simple and easy for newbies to understand, even if they didn't have > any previous compiler experience.Absolutely. I think the most productive angle for me would be to write a higher-level front-end in OCaml rather than simply translating the existing tutorial into OCaml. Ultimately, a minimal compiler for a statically-typed functional programming language that gave good performance for some cases (probably non-GC-intensive numeric code) would be an incredibly compelling demo for the use of LLVM in general compiler writing. This looks perfectly feasible to me. I got the impression from some of the blurb that I read that the optimizer in LLVM might even be able to automate localized unboxing. Is that true? If so, that would make things a lot easier... -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/products/?e