Download presentation
Presentation is loading. Please wait.
Published byJuliet Hardy Modified over 8 years ago
1
Advanced Compiler Design An Introduction to the Javali Compiler Framework Zoltán Majó 1
2
Javali Simple object-oriented programming language Your tasks in this lecture are: Implement an SSA-based IR Implement some compiler optimizations Come up with your own project ... and you have to do all this for Javali 2
3
Javali You may build your own Javali compiler We provide a framework that already implements Parsing IR Code generation Semantic checking You can extend the framework 3
4
Outline Building the Control Flow Graph (CFG) Debugging & Testing 4
5
class Main { void main() { int count, i, zeros, notZeros; count = read(); i = 0; zeros = 0; notZeros = 0; while (i < count) { input = read(); if (input == 0) { zeros++; } else { notZeros++; } i++; } write(zeros); write(notZeros); } } 5 Example: counting # of zero/non-zero elements Program code
6
class Main { void main() { int count, i, zeros, notZeros; count = read(); i = 0; zeros = 0; notZeros = 0; while (i < count) { input = read(); if (input == 0) { zeros++; } else { notZeros++; } i++; } write(zeros); write(notZeros); } 6 Constructing the Control Flow Graph BB1 count = read(); i = 0; zeros = 0; notZeros = 0; BB7 write(zeros); write(notZeros); BB6 i++; true false Program codeControl Flow Graph BB4 zeros++; BB5 notZeros++; true false BB3 input = read(); COND(input == 0) BB2 COND(i < count)
7
class Main { void main() { int count, i, zeros, notZeros; count = read(); i = 0; zeros = 0; notZeros = 0; while (i < count) { input = read(); if (input == 0) { zeros++; } else { notZeros++; } i++; } write(zeros); write(notZeros); } 7 Implementing CFG Construction Program codeAbstract Syntax Tree Assign Var::countBuiltInRead WhileLoop BinaryOp::< Var::iVar::count Seq Assign Var::inputBuiltInRead...
8
Simple idea: for each method of the program { walk through the method’s list of AST nodes add each AST node to the current basic block } More action required: IfElse and WhileLoop AST nodes Create new basic blocks if necessary Connect new basic blocks to existing ones so that connections reflect the method’s control flow Not allowed in the CFG: IfElse and WhileLoop AST nodes Use Visitor pattern to traverse AST: extend AstVisitor Add all this functionality to CFGBuilder.build() 8 Implementing CFG Construction
9
Debugging In debug mode compiler dumps CFG into a *cfg.dot file Use dotty from the Graphviz package to visualize CFG 9 Testing Example programs included in the javali_tests directory Get reference CFG generated by our server: run TestSamplePrograms as JUnit test
10
Thanks! Questions? Get fragment from: https://svn.inf.ethz.ch/svn/trg/cd_students/2012ss/teams/ /CD2_A0/ 10
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.