Your SlideShare is downloading. ×
0
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Open Source Compiler Construction for the JVM [LCA2011 Miniconf]

1,237

Published on

LCA2011 miniconf presentation on compiler construction for the JVM.

LCA2011 miniconf presentation on compiler construction for the JVM.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,237
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Compiler Construction for the JVM Tom Lee [email_address] Senior Consultant Shine Technologies http://www.shinetech.com
  • 2. Overview <ul><li>Who am I?
  • 3. Why target the JVM?
  • 4. Compiler Construction 101
  • 5. “Hello World”
  • 6. Scala's Parser Combinators
  • 7. “Hello World” in AST form
  • 8. Representing ASTs in Scala
  • 9. Compiling ASTs with BCEL
  • 10. A Small Proof of Concept </li></ul>
  • 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. Open source contributor </li><ul><li>(C)Python </li><ul><li>try/except/finally syntax in 2.5
  • 13. Compilation of ASTs within Python in 2.6
  • 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. Why target the JVM? <ul><li>Third-party libraries
  • 16. Highly tuned* </li><ul><li>*For long-running processes </li></ul><li>Memory management
  • 17. Enterprise friendly </li><ul><li>Whatever that means :) </li></ul></ul>
  • 18. Compiler Construction 101
  • 19. “Hello World” puts(“Hello World”);
  • 20. “Hello World” (cont.) puts ( “Hello World” ) ;
  • 21. “Hello World” (cont.) putsStmt -> “puts” “(“ str “)” “;” str -> STR STR -> regex(“[^”]*”)
  • 22. “Hello World” (cont.) call -> name “(“ str “)” “;” name -> ID str -> STR ID -> regex([a-zA-Z_][a-zA-Z_0-9]*) STR -> regex(“[^”]*”)
  • 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. Scala's Parser Combinators <ul><li>DSL for describing parsers in Scala
  • 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. Scala's Parser Combinators (cont.) expr -> str | call call -> name “(“ args? “)” “;” ... Grammar def expr = str | call def call = name ~ (“(“ ~> args? <~ “)”) “;” ... Scala
  • 27. “Hello World” in AST form
  • 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. Representing ASTs in Scala <ul><li>Trait for AST nodes
  • 30. Trait for expressions
  • 31. Trait for statements
  • 32. Case classes for everything else </li></ul>
  • 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. Compiling ASTs with BCEL <ul><li>Generate builtins </li><ul><li>e.g. the “puts” function. </li></ul><li>Visit each AST node
  • 35. Generate logically equivalent bytecode
  • 36. ???? </li><ul><li>Or: Beware of the verifier. </li></ul><li>Profit! </li></ul>
  • 37. A Small Proof of Concept <ul><li>Compiles our “language” to JVM bytecode </li></ul>
  • 38. Summary <ul><li>Write a grammar
  • 39. Convert it to its parser combinator equivalent
  • 40. Act upon parse results to construct an AST
  • 41. Generate logically equivalent JVM bytecode </li></ul>

×