Open Source Compiler Construction (for the JVM)

Slides:



Advertisements
Similar presentations
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Advertisements

Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CPS 506 Comparative Programming Languages Syntax Specification.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Programming Languages
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Intermediate Code Representations
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CSCE 314 Programming Languages
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
MiniJava Compiler A multi-back-end JIT compiler of Java.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Chapter 1. Introduction.
Teaching Compiler Design
Advanced Computer Systems
01 – Overview of Compilers
Intro to compilers Based on end of Ch. 1 and start of Ch. 2 of textbook, plus a few additional references.
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
CS 3304 Comparative Languages
A Simple Syntax-Directed Translator
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compiler Construction (CS-636)
Topic: Difference b/w JDK, JRE, JIT, JVM
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Compiler Chapter 9. Intermediate Languages
Overview of Compilation The Compiler BACK End
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
课程名 编译原理 Compiling Techniques
Dept of Computer Science
CS 536 / Fall 2017 Introduction to programming languages and compilers
CSc 453 Compilers & Systems Software 00. Background
Introduction to Compiler Construction
Basic Program Analysis: AST
Intermediate Representations Hal Perkins Autumn 2011
Compiler 薛智文 TH 6 7 8, DTH Spring.
CSE401 Introduction to Compiler Construction
Overview of Compilation The Compiler BACK End
COP4020 Programming Languages
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler design.
Intermediate Representations Hal Perkins Autumn 2005
Introduction to Compiler Construction
A Level Computer Science Topic 5: Computer Architecture and Assembly
COP4020 Programming Languages
COP4020 Programming Languages
Compiler 薛智文 M 2 3 4, DTH Spring.
Introduction to Compiler Construction
Presentation transcript:

Open Source Compiler Construction (for the JVM) Tom Lee Shine Technologies OSCON Java 2011

Overview About the Java Virtual Machine About Scala About Apache BCEL A Generalized Compiler Architecture Introducing “Awesome” Writing a Stub Code Generator for “Awesome” Abstract Syntax Trees (ASTs) Parsing “Awesome” with Scala What Next?

About the Java Virtual Machine Stack-based machine. Operands pushed onto a stack, operated upon by opcodes. Heavily influenced by “Java The Language”. Allows interop between various source languages. JVM bytecode is architecture-independent. Multiple implementations. Oracle / HotSpot Apache / Harmony OpenJDK Many more...

About Scala http://www.scala-lang.org/ Functional/OOP hybrid programming language First class functions Pattern matching Classes & traits Etc. Runs on the JVM. And .NET. Own standard library (on top of the Java standard library).

About Apache BCEL http://jakarta.apache.org/bcel/ “Bytecode Engineering Library” Emit JVM bytecode with a reasonably straightforward API. Also supports reading class files, modifying classes, etc. But we won't use that stuff here.

A Generalized Compiler Architecture Scanner matches patterns in source code, outputs tokens. Parser organizes tokens into an Abstract Syntax Tree (AST). Or a Parse Tree. Semantic checks or optimizations of the AST may occur here. Code Generator traverses the AST to produce target code.

Introducing “Awesome” program ::= (expression ';')* expression ::= sum sum ::= product (('+' | '-') expression)? product ::= number (('*' | '/') expression)? number ::= /[0-9]+/ The “Awesome” compiler will generate JVM bytecode to display each parsed expression. (Awesome, eh?)

Writing a Stub Code Generator for “Awesome” Work backwards and write a stub code generator first. Immediate feedback! A solid foundation on which to build the rest of the compiler. Generate a “Hello World” class file with BCEL. We'll make the code generator do “real” stuff later.

Abstract Syntax Trees (ASTs) A logical, in-memory representation of the source program. Constructed by the parser. Semantic checks and optimizations possible at the AST level. Outside the scope of this presentation – sorry! Let's add the beginnings of an AST to our compiler.

Parsing “Awesome” with Scala Use parser combinators. Combine small parsing functions together to describe a language. Scanner and parser wrt the Generalized Compiler Architecture. Describe languages using something of a pseudo-EBNF. You can write your own parsers too. e.g. for string literals. We'll use built-in parsers to identify integers & semi-colons.

The “Awesome” Grammar Revisited program ::= (expression ';')* expression ::= sum sum ::= product (('+' | '-') expression)? product ::= number (('*' | '/') expression)? number ::= /[0-9]+/

What next? Variables? Conditional logic? For/while loops? Function calls?

Summary Write the code generator first for instant gratification. Use Scala parser combinators to build an AST from your input. Update the code generator to walk the AST, emitting equivalent bytecode for each node using BCEL. Iterate & add new features!

That's All, Folks! Shine Technologies All code from this presentation will be available from http://github.com/thomaslee/oscon2011-awesome Shine Technologies www http://www.shinetech.com github shinetech twitter @realshinetech Tom Lee www http://tomlee.co email me@tomlee.co github thomaslee twitter @tglee