Homework 1 Simple code genera/on. Luca Della Toffola Compiler Design HS15

Similar documents
Homework 3 Semantic Analysis. Remi Meier Compiler Design

KU Compilerbau - Programming Assignment

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

Design Pa*erns. + Anima/on Undo/Redo Graphics and Hints

CSE 70 Final Exam Fall 2009

Ways to implement a language

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

Principles of Programming Languages

Compila(on /15a Lecture 6. Seman(c Analysis Noam Rinetzky

KU Compilerbau - Programming Assignment

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Design pa*erns. Based on slides by Glenn D. Blank

Principles of Programming Languages

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

Objec&ves. Servlets Review JSPs Web Applica&on Organiza&on Version Control. May 3, 2016 Sprenkle - CS335 1

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Code Genera*on for Control Flow Constructs

Automa'c Test Genera'on

EDA180: Compiler Construc6on. More Top- Down Parsing Abstract Syntax Trees Görel Hedin Revised:

Advanced Topics in MNIT. Lecture 1 (27 Aug 2015) CADSL

Sec$on 2: Specifica)on, ADTs, RI WITH MATERIAL FROM MANY

A Short Summary of Javali

Design Pa*erns. Philippe Collet. Master 1 IFI Interna3onal h8p://dep3nfo.unice.fr/twiki/bin/view/minfo/soeeng1213. P.

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

12/7/09. How is a programming language processed? Picasso Design. Collaborating with Subversion Discussion of Preparation Analyses.

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Josh Bloch Charlie Garrod Darya Melicher

Announcements. Working in pairs is only allowed for programming assignments and not for homework problems. H3 has been posted

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

Research opportuni/es with me

SDC Design patterns GoF

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Remedial Java - Excep0ons 3/09/17. (remedial) Java. Jars. Anastasia Bezerianos 1

Design Patterns Reid Holmes

CS164: Programming Assignment 3 Dpar Parser Generator and Decaf Parser

CSE302: Compiler Design

Java Programming Unit 7. Error Handling. Collec7ons

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Design Principles & Prac4ces

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

Architectural Requirements Phase. See Sommerville Chapters 11, 12, 13, 14, 18.2

Related Course Objec6ves

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

Josh Bloch Charlie Garrod Darya Melicher

LECTURE 3. Compiler Phases

Practical C Programming

J, K F, G, H. Library/framework, 168 LIKE() predicate, 142 Load-balancing server (LBS), 120 Lock on check out (LOCO), 1

Object Oriented Design (OOD): The Concept

Extensible Compiler Architecture Examples from JModelica.org

Introduction to Programming Using Java (98-388)

Charlie Garrod Michael Hilton

A System for Genera/ng Sta/c Analyzers for Machine Instruc/ons (TSL)

CISC327 - So*ware Quality Assurance

CSE-304 Programming Assignment #4

Why OO programming? want but aren t. Ø What are its components?

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

CISC327 - So*ware Quality Assurance

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Writing Evaluators MIF08. Laure Gonnord

P2: Collaborations. CSE 335, Spring 2009

Introduc)on. Tristan Naumann. Sahar Hasan. Steve Hehl. Brian Lu

Syntax Errors; Static Semantics

Program Representations

Java Programming Unit 7. Error Handling. Excep8ons.

Founda'ons of So,ware Engineering. Sta$c analysis (1/2) Claire Le Goues

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

CSE Compilers. Reminders/ Announcements. Lecture 15: Seman9c Analysis, Part III Michael Ringenburg Winter 2013

Java. Package, Interface & Excep2on

Alex Mariakakis CSE 331, Autumn 2013 With material from Krysta Yousoufian, Marty Stepp, Mike Ernst, and others

Con$nuous Integra$on Development Environment. Kovács Gábor

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

CS 536 Spring Programming Assignment 1 Identifier Cross-Reference Analysis

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised:

6.170 Laboratory in Software Engineering Eclipse Reference for 6.170

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

CS2383 Programming Assignment 3

CS131 Compilers: Programming Assignment 2 Due Tuesday, April 4, 2017 at 11:59pm

High-Level Synthesis Creating Custom Circuits from High-Level Code

CSE302: Compiler Design

Yacc Yet Another Compiler Compiler

CSE wi Final Exam 3/12/18. Name UW ID#

Implementing Classes, Arrays, and Assignments

Project Compiler. CS031 TA Help Session November 28, 2011

CS 520/620 Advanced Software Engineering Fall September 27, 2016

CS 267: Automated Verification. Lecture 18, Part 2: Data Model Analysis for Web Applications. Instructor: Tevfik Bultan

Compiler Optimization Intermediate Representation

Compiler Construction. (1 Design practical)

Lecture 4: Build Systems, Tar, Character Strings

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

CISC327 - So*ware Quality Assurance

Lecture 4: Observer Pattern, Event Library and Componentization

CSE 374 Programming Concepts & Tools

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

JUnit tes)ng. Elisa Turrini

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Modifying an Exis.ng Commercial Product for Cryptographic Module Evalua.on

Declaring and ini,alizing 2D arrays

Transcription:

Homework 1 Simple code genera/on Luca Della Toffola Compiler Design HS15 1

Administra1ve issues Has everyone found a team- mate? Mailing- list: cd1@lists.inf.ethz.ch Please subscribe if we forgot you 2

Today HW1 SVN Javali Design PaFerns 3

Today HW1 SVN Javali Design PaFerns 4

This course Build a full Javali compiler Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on 5

This course HW2 Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on 6

This course HW3 Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on 7

This course HW4 Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on 8

This course Global op1miza1ons or Javali advanced features HW6 Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on 9

Homework 1 Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on 10

Homework 1 Variable declara1ons Assignments Simple expressions Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on 11

Homework 1 Variable declara1ons Assignments Simple expressions Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on We give you the parser 12

Homework 1 Variable declara1ons Assignments Simple expressions Lexical Analysis Syntax Analysis Seman1c Analysis Code Produc1on Not necessary for now 13

Homework 1 - Examples class Main { void main() { write(222); writeln(); } } 14

Homework 1 - Examples class Main { void main() { int i, j; i = 1; j = 2; write(i + j); } } 15

Grading scheme Homework tasks Test- cases (quan1ty and correctness) Code quality Code coverage (depending on the assignment) 16

Today HW1 SVN Javali Design PaFerns 17

Get your fragment hfps://svn.inf.ethz.ch/svn/trg/cd_students/2015hs/teams/<yourteam> Case- sensi1ve Homework + grades Your submission pla_orm 18

SVN basics svn checkout h,ps://<your_svn_repo> Get the remote copy of the repository on 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 19

SVN resources Links hxp://svnbook.red- bean.com hxps://www.google.ch/search?q=svn+tutorial So`ware Eclipse Subversive Tortoise SVN Command- line 20

Today HW1 SVN Javali Design PaFerns 21

Javali Simple OO programming language Subset of Java Javali specifica1on in the course web- site Updated recently, subject to changes (and bugs) When the specifica/on is incomplete Common sense or Java specifica1on apply Use the mailing- list for clarifica1ons or ques1ons 22

Javali framework We provide a framework skeleton To use for your homework U/lity classes and basic tasks Free to modify or create your own Please comply to submission requirements Ager each homework we provide a solu/on 23

Javali framework src Source of the compiler test Source files for tes7ng 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 Op7onal ANT script for command- line (can be used in Eclipse) 24

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

DEMO 26

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

How to test your Javali compiler Expected results are stored in.javali.x.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 28

DEMO 29

Javali framework catches Files that may change per fragment lib/frozenreference.jar @ every fragment build.xml depending on the assignment We will provide details in the recita/on Look for new targets Javali specifica1on As previously men/oned 30

Javali program representa1on HW0 class Main { void main() { write(222); writeln(); } }?.text main: move 222 - > r0 prin_ %d,r0 prin_ \n return 0 31

Javali program representa1on ClassDecl class Main { void main() { write(222); writeln(); } } MethodDecl Seq Seq BuiltInWrite BuiltInWriteLn.text main: move 222 - > r0 prin_ %d,r0 prin_ \n return 0 IntConst 32

ClassDecl MethodDecl Seq Seq BuiltInWrite BuiltInWriteLn IntConst 33

Javali Abstract Syntax Tree Ast Decl Stmt Expr Ast nodes declared in cd/ir/ast.java 34

Javali Abstract Syntax Tree Declara1ons ClassDecl, MethodDecl, VarDecl, ConstDecl Local variables Statements or fields Assign, BuiltInWrite, BuiltInWriteln, IfElse, MethodCall, WhileLoop Expressions In HW4 you will IntConst, UnaryOp, BinaryOp, need BuiltInRead to handle more Simply a + expr expressions constant - expr expr OP expr value 35

Print the Abstract Syntax Tree We provide an u/lity class to print the AST cd/u1l/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 36

DEMO 37

Traverse the Abstract Syntax Tree Implemented design paxern: Visitor ExprVisitor<R,A> and AstVisitor<R,A> Apply an opera/on for each AST node Avoid to modify R = result Generic AST class A parameters = argument Details and implementa/on example later 38

Javali program representa1on AstCodeGenerator ClassDecl class Main { void main() { write(222); writeln(); } } MethodDecl Seq Seq BuiltInWrite BuiltInWriteLn.text main: move 222 - > r0 prin_ %d,r0 prin_ \n return 0 2 visitors StmtGenerator ExprGenerator IntConst 39

Javali code- genera1on ClassDecl MethodDecl Seq Seq.text main: move 222 - > r0 prin_ %d,r0 prin_ \n return 0 BuiltInWrite BuiltInWriteLn IntConst 40

HW1 details No stack frame necessary Use.data sec1on slots for each variable Look for throw new TodoExcep1on() Use registers for intermediate results Use op1mal number of registers 41

Today HW1 SVN Javali Design PaFerns 42

Design PaFerns are descrip7ons of communica7ng objects and classes customized to solve a general design problem in a par7cular context Gamma et al. Design PaFerns Elements of Reusable Object- Oriented So`ware 43

Singleton Observer MVC Iterator Facade Decorator Factory Visitor Mediator 44

Singleton Observer MVC Iterator Facade Decorator Factory Visitor Mediator 45

Design PaFerns - Visitor Intent Define an opera/on for an object structure. Descrip1on + Do not change the structure + Do not change class interface(s) + Support dis/nct unrelated opera/ons 46

Design PaFerns PaXerns are related and can be combined Design good sogware is an art We may need mul/ple tools to solve a problem Examples of paxerns that can be useful to build your compiler (and that you may find in our reference solu7on) 47

Design PaFerns - Singleton Intent Ensure a class has only one instance, one global access point. Descrip1on + Avoid global variable usage + Controlled access to single instance 48

TypeSymbol == Main ClassDecl Only one instance per symbol. There is only one Main class. MethodDecl Seq Seq BuiltInWrite BuiltInWriteLn TypeSymbol == int IntConst 49

ClassDecl MethodDecl Seq Seq class TypeSymbol { sta1c Map<String, TypeSymbol> syms =... ; private String typename = /* INVALID */; private TypeSymbol() {... } public sta1c TypeSymbol newtype(string name) { TypeSymbol newtype =... ; if (syms.exists(name) { /* WARNING */ }... syms.put(name, newtype);... return newtype; } } BuiltInWrite BuiltInWriteLn IntConst 50

Design PaFerns - Factory Intent Interface to create families of related objects without exposing the concrete classes. Descrip1on + Specific applica/on code depending on configura/on + Cannot an/cipate object classes + Responsibility delega/on 51

public sta1c AstCodeGenerator createcodegenerator(main main, Writer out) { if (Config.useFullCodeGenerator()) { } else return new AstCodeGeneratorHW4(main, out); return new AstCodeGenerator(main, out); this.regman = RegisterManager.get(); RegisterManager get() { if (Config.use64bits()) { return new X64RegisterManager(); } else { return new X86RegisterManager(); } return null; } 52

Design PaFerns - Facade Intent Unified interface to a set of interfaces to make an higher- level system that it is easier to use. Descrip1on + Reduced complexity + Introduce layering in a complex system + Responsibility delega/on (again) 53

Compiler Parser Main class in the framework Ast Compiler.compile() Symbol CodeGenerator Lexer Token BytecodeCG X86CG 54

DEMO 55