Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Open Source Compiler Construction for the JVM [LCA2011 Miniconf]

1 751 vues

Publié le

LCA2011 miniconf presentation on compiler construction for the JVM.

Publié dans : Technologie
  • Soyez le premier à commenter

Open Source Compiler Construction for the JVM [LCA2011 Miniconf]

  1. 1. Compiler Construction for the JVM Tom Lee [email_address] Senior Consultant Shine Technologies http://www.shinetech.com
  2. 2. Overview <ul><li>Who am I?
  3. 3. Why target the JVM?
  4. 4. Compiler Construction 101
  5. 5. “Hello World”
  6. 6. Scala's Parser Combinators
  7. 7. “Hello World” in AST form
  8. 8. Representing ASTs in Scala
  9. 9. Compiling ASTs with BCEL
  10. 10. A Small Proof of Concept </li></ul>
  11. 11. Who am I? <ul><li>Senior Consultant for Shine Technologies </li><ul><li>http://www.shinetech.com </li></ul><li>Hobbyist compiler enthusiast
  12. 12. Open source contributor </li><ul><li>(C)Python </li><ul><li>try/except/finally syntax in 2.5
  13. 13. Compilation of ASTs within Python in 2.6
  14. 14. Beginnings of AST optimizer in 2.7 / 3.0 ... </li><ul><li>… but got distracted by real life :( </li></ul></ul><li>… and others </li></ul></ul>
  15. 15. Why target the JVM? <ul><li>Third-party libraries
  16. 16. Highly tuned* </li><ul><li>*For long-running processes </li></ul><li>Memory management
  17. 17. Enterprise friendly </li><ul><li>Whatever that means :) </li></ul></ul>
  18. 18. Compiler Construction 101
  19. 19. “Hello World” puts(“Hello World”);
  20. 20. “Hello World” (cont.) puts ( “Hello World” ) ;
  21. 21. “Hello World” (cont.) putsStmt -> “puts” “(“ str “)” “;” str -> STR STR -> regex(“[^”]*”)
  22. 22. “Hello World” (cont.) call -> name “(“ str “)” “;” name -> ID str -> STR ID -> regex([a-zA-Z_][a-zA-Z_0-9]*) STR -> regex(“[^”]*”)
  23. 23. “Hello World” (cont.) stmt -> expr “;” expr -> str | call call -> name “(“ args? “)” args -> expr ( “,” expr)* str -> STR name -> ID STR -> regex(“[^”]*”) ID -> regex([a-zA-Z_][a-zA-Z_0-9]*) By deriving these grammars, we make it easy to reproduce them in Scala. How?
  24. 24. Scala's Parser Combinators <ul><li>DSL for describing parsers in Scala
  25. 25. Act upon parse results </li><ul><li>e.g. construct AST </li></ul><li>AST -> Abstract Syntax Tree </li><ul><li>Logical representation of your program </li></ul></ul>
  26. 26. Scala's Parser Combinators (cont.) expr -> str | call call -> name “(“ args? “)” “;” ... Grammar def expr = str | call def call = name ~ (“(“ ~> args? <~ “)”) “;” ... Scala
  27. 27. “Hello World” in AST form
  28. 28. “Hello World” in AST form (cont.) o_O For the sake of simplicity, we'll pretend all our programs consist of a single expression...
  29. 29. Representing ASTs in Scala <ul><li>Trait for AST nodes
  30. 30. Trait for expressions
  31. 31. Trait for statements
  32. 32. Case classes for everything else </li></ul>
  33. 33. Representing ASTs in Scala (cont.) trait AST {} // trait Stmt extends AST {} trait Expr extends AST {} case class Call(name : Name, args : List[Expr]) extends Expr case class Name(id : String) extends Expr case class Str(value : String) extends Expr
  34. 34. Compiling ASTs with BCEL <ul><li>Generate builtins </li><ul><li>e.g. the “puts” function. </li></ul><li>Visit each AST node
  35. 35. Generate logically equivalent bytecode
  36. 36. ???? </li><ul><li>Or: Beware of the verifier. </li></ul><li>Profit! </li></ul>
  37. 37. A Small Proof of Concept <ul><li>Compiles our “language” to JVM bytecode </li></ul>
  38. 38. Summary <ul><li>Write a grammar
  39. 39. Convert it to its parser combinator equivalent
  40. 40. Act upon parse results to construct an AST
  41. 41. Generate logically equivalent JVM bytecode </li></ul>

×