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