Homework 1 Simple code generator

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

1 Compiler Construction Intermediate Code Generation.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Honors Compilers The Course Project Feb 28th 2002.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
Compiler Summary Mooly Sagiv html://
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
Version Control with Subversion. What is Version Control Good For? Maintaining project/file history - so you don’t have to worry about it Managing collaboration.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
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 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
{ Graphite Grigory Arashkovich, Anuj Khanna, Anirban Gangopadhyay, Michael D’Egidio, Laura Willson.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Advanced Compiler Design An Introduction to the Javali Compiler Framework Zoltán Majó 1.
CS453 LectureIntroduction1 CS453 Compiler Construction Instructor:Wim Bohm Computer Science Building 470 TA: tba
5-1 5 Compilation  Overview  Compilation phases –syntactic analysis –contextual analysis –code generation  Abstract syntax trees  Case study: Fun language.
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.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CS453 LectureIntroduction1 CS453 Compiler Construction Original Design: Michelle Strout Instructor:Wim Bohm
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
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.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
Problem Solving With C++ SVN ( Version Control ) April 2016.
Open Source Compiler Construction (for the JVM)
Lecture 9 Symbol Table and Attributed Grammars
Teaching Compiler Design
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
The need for Programming Languages
Compiler Design (40-414) Main Text Book:
The architecture of the P416 compiler
Cool Overview and SVN Configuration
Basic 1960s It was designed to emphasize ease of use. Became widespread on microcomputers It is relatively simple. Will make it easier for people with.
Introduction to Computer Science
Testing and Debugging.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
课程名 编译原理 Compiling Techniques
Graph-Based Operational Semantics
Implementing ASTs (in Java) Hal Perkins Autumn 2009
CVS revisions UML diagram
CMPE 152: Compiler Design December 5 Class Meeting
slides borrowed and adapted from Alex Mariakis and CSE 390a
Chapter 11 Introduction to Programming in C
Introduction to Data Structures
Design and Programming
Design Pattern: Visitor
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
COP4020 Programming Languages
Creating your first C program
Programming Languages 2nd edition Tucker and Noonan
Interpreter Pattern.
C-to-LC3 Compiler Over the course of the next two weeks, you will build a program that will compile C code to LC-3 assembly language Don't panic! You.
Computer Science and Engineering
Computer Science and Engineering
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Data Structures & Algorithms
COMPILERS Semantic Analysis
SPL – PS1 Introduction to C++.
IS 135 Business Programming
Presentation transcript:

Homework 1 Simple code generator Aristeidis Mastoras Compiler Design – SS17 (based on slides of Luca Della Toffola from Compiler Design – HS15)

Administrative issues Has everyone found a teammate? Mailing-list: cd1@lists.inf.ethz.ch Please subscribe if we forgot you

Today HW Overview SVN Javali HW1

Today HW Overview SVN Javali HW1

Build a full Javali compiler This course Build a full Javali compiler Lexical Analysis Syntax Analysis Semantic Analysis Code Generation

This course HW2 Parser Lexical Analysis Syntax Analysis Semantic Code Generation Parser

This course HW3 Lexical Analysis Syntax Analysis Semantic Analysis Code Generation

This course HW4 Lexical Analysis Syntax Analysis Semantic Analysis Code Generation

Javali advanced features This course Global optimizations or Javali advanced features HW6 Lexical Analysis Syntax Analysis Semantic Analysis Code Generation

Homework 1 Lexical Analysis Syntax Analysis Semantic Analysis Code Generation

Homework 1 Variable declarations Assignments Simple expressions Lexical Analysis Syntax Analysis Semantic Analysis Code Generation

Homework 1 We give you the parser Variable declarations Assignments Simple expressions Lexical Analysis Syntax Analysis Semantic Analysis Code Generation We give you the parser

Homework 1 Not necessary for now Variable declarations Assignments Simple expressions Lexical Analysis Syntax Analysis Semantic Analysis Code Generation Not necessary for now

Grading Homework task Write your own tests Code quality The compiler “works” Write your own tests Test exhaustively that the compiler works Code quality The code is readable

Good news! HW1 is independent of HW2 The same applies to the next HW You can still do the next HW even if you don’t manage to get this right

Today HW Overview SVN Javali HW1

SVN What is SVN? a version control system it is used to store current and previous versions of (not only) source code you can revert to a previous version

Javali fragment We provide: a compiler skeleton for every HW stored in an SVN repository every team has a different SVN repository

Get your fragment https://svn.inf.ethz.ch/svn/trg/cd_students/2017ss/teams/<YourTeam> Case-sensitive Homework + grades Your submission platform

SVN basics svn checkout https://<your_SVN_repo> Get the remote copy of the repository on your machine. svn commit –m “Message about your changes” Update remote copy of the repository with local changes. svn update Get remote changes of your repository if modified.

SVN basics svn add <file-or-dir-name> Add a file/directory to the local copy. (It requires commit to update the remote copy.) svn remove <file-or-dir-name> Delete a file/directory from the local copy.

SVN basics svn status Report files that are different in the local copy from those in the remote copy. svn diff –r <version-number> <file-name> Report the differences between the local copy and a specific version.

SVN resources Links Software http://svnbook.red-bean.com https://www.google.ch/search?q=svn+tutorial Software Eclipse Subversive Tortoise SVN Command-line

Today HW Overview SVN Javali HW1

Javali Simple OO programming language Subset of Java Javali specification in the course web-site Updated recently, subject to changes (and bugs) When the specification is incomplete Common sense or Java specification apply Use the mailing-list for clarifications or questions

Javali framework We provide a framework skeleton To use for your homework Utility classes and basic tasks Free to modify or create your own Please comply to submission requirements After each homework we provide a solution

Javali framework src test lib javali_tests build.xml Source of the compiler test Source files for testing your compiler lib Compiler dependencies as .JAR files javali_tests Unit-tests in form of .javali files. These are example programs to test build.xml Optional ANT script for command-line (can be used in Eclipse)

Compile the Javali framework Fragment is an Eclipse project, it will build automatically If you don’t use Eclipse, install ANT and type: ant test It compiles automatically too 

How to test your Javali compiler We provide a JUnit-based testing framework A test is a Javali program in the javali_tests directory The testing framework compares the output of your compiler against our reference solution To test your compiler write more Javali programs that cover assignment tasks You need to see green

How to test your Javali compiler Expected results are stored in .javali.exec.ref files .javali.in file determines the standard input One line is equivalent to the result of a read() call. Run the tests using JUnit4 Eclipse provides a GUI to inspect results .javali.err file contains debugging output and error messages

DEMO

Javali framework changes Files that may change per fragment lib/frozenReference.jar @ every fragment build.xml depending on the assignment We will provide details in the recitation Look for new targets Javali specification As previously mentioned

Today HW Overview SVN Javali HW1

Simple code generator Input: Javali Program Output: x86 Assembly .section .data STR_D: .string "%d" var_a: .int 0 .section .text .globl main main: … class Main { void main() { int a; a = 10; write(a); }

? Simple code generator main: … # Emitting a = 10 # Emitting 10 movl $10, %edi movl %edi, var_a # Emitting write(a) # Emitting a movl var_a, %edi sub $16, %esp movl %edi, 4(%esp) movl $STR_D, 0(%esp) call printf add $16, %esp ? class Main { void main() { int a; a = 10; write(a); }

Javali program representation The Intermediate Representation of Javali compiler is an Abstract Syntax Tree ClassDecl MethodDecl class Main { void main() { int a; a = 10; write(a); } Seq Seq VarDecl Assign BuiltInWrite Var IntConst Var We give you the IR, i.e., the AST. You need to generate the assembly code by using the AST.

Another example main: ... # Emitting a = 10 # Emitting 10 Input: Javali Program Output: x86 Assembly main: ... # Emitting a = 10 # Emitting 10 movl $10, %edi movl %edi, var_a # Emitting b = (a + 7) # Emitting (a + 7) # Emitting 7 movl $7, %edi # Emitting a movl var_a, %esi add %edi, %esi movl %esi, var_b         ... class Main { void main() { int a, b; a = 10; b = a + 7; } = b + a 7

Javali Abstract Syntax Tree abstract classes Ast Decl Stmt Expr … … … concrete classes for nodes used from the IR Ast nodes declared in cd/ir/Ast.java

Javali Abstract Syntax Tree Declarations ClassDecl, MethodDecl, VarDecl Statements Assign, BuiltInWrite, BuiltInWriteln, IfElse, MethodCall, WhileLoop, Nop Expressions Var, IntConst, UnaryOp, BinaryOp, BuiltInRead, Index, NewArray, Field, Cast, NullConst, ThisRef, NewObject, BooleanConst

Javali Abstract Syntax Tree Declarations ClassDecl, MethodDecl, VarDecl Statements Assign, BuiltInWrite, BuiltInWriteln, IfElse, MethodCall, WhileLoop, Nop Expressions Var, IntConst, UnaryOp, BinaryOp, BuiltInRead, Index, NewArray, Field, Cast, NullConst, ThisRef, NewObject, BooleanConst Only a subset of the AST nodes are used in Homework 1

Print the Abstract Syntax Tree We provide a utility class to print the AST cd/util/debug/AstDump.java To check the AST for a test program Examine the .parser.ref file, or the .err file. All AST nodes also have a toString() method

DEMO

Traverse the Abstract Syntax Tree How can we traverse the AST? Similarly to a (binary) tree.

Traverse a binary tree How can we traverse a binary tree? 1 public class Visitor { void visit(TreeNode node) { if (node.leftchild != null) visit(node.leftchild); if (node.rightchild != null) visit(node.rightchild); } 2 5 3 4 6 7

Traverse a binary tree What if different nodes have different colors and different behavior? Blue nodes: print “blue” Green nodes: print “green”

Traverse a binary tree abstract class TreeNode { public TreeNode leftchild; public TreeNode rightchild; … } class BlueNode extends TreeNode { … } class GreenNode extends TreeNode { … }

Add node-specific behavior Traverse a binary tree public class Visitor { void visit(TreeNode node) { if (node instanceof BlueNode) print “blue”; else if (node instanceof GreenNode) print “green”; if (node.leftchild != null) visit(node.leftchild); if (node.rightchild != null) visit(node.rightchild); } } Add node-specific behavior

Traverse a binary tree This simple solution works well. But, there are more elegant solutions. Usage of design patterns.

Design patterns … are descriptions of communicating objects and classes customized to solve a general design problem in a particular context Gamma et al. Design Patterns Elements of Reusable Object-Oriented Software

Design patterns Wikipedia says: It is a general reusable solution to a commonly occurring problem within a given context in software design It is a description or template for how to solve a problem that can be used in many different situations

Design patterns There are many design patterns Patterns are related and can be combined Design good software is an art We may need multiple “tools” to solve a problem Examples of patterns that can be useful to build your compiler (and that you may find in our reference solution)

Design patterns - Visitor Intent Defines an operation for an object structure. Description Separates an algorithm from an object structure Does not change the structure Does not change class interface(s) Supports distinct unrelated operations

Visitor design pattern solution class BlueNode extends TreeNode { void accept(Visitor v) { v.blueNode(this); } class GreenNode extends TreeNode { v.greenNode(this); We define a method accept() for each node. The accept method calls the proper method of the visitor class.

Visitor design pattern solution public class Visitor { public void blueNode(BlueNode node) { print “blue”; if (node.leftchild != null) node.leftchild.accept(this); if (node.rightchild != null) node.rightchild.accept(this); } public void greenNode(GreenNode node) { print “green”;

DEMO

Traverse the Abstract Syntax Tree Javali framework implements two main visitors to traverse the AST: ExprVisitor<R,A> for Expressions AstVisitor<R,A> for Statements and Declarations Apply an operation for each AST node Avoid to modify AST class Generic parameters R = result A = argument

Traverse the Abstract Syntax Tree Two visitors for the code generator: ExprGenerator extends ExprVisitor<Register, Void> StmtGenerator extends AstVisitor<Register, Void>

Traverse the Abstract Syntax Tree You can implement another Visitor to traverse the AST of an expression: calculate the required number of registers e.g., 7 * (a + 1) * 7 + a 1

HW1 summary Implementation of simple code generator No stack frame necessary Use .data section slots for each variable Look for throw new ToDoException() Use registers for intermediate results Use optimal number of registers