McLab Tutorial www.sable.mcgill.ca/mclab Part 5 – Introduction to the McLab Analysis Framework Exploring the Main Components Creating a Simple Analysis.

Slides:



Advertisements
Similar presentations
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
The Singleton Pattern II Recursive Linked Structures.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
Written by: Dr. JJ Shepherd
McLab Tutorial Part 8 – Wrap Up Summary Ongoing and Future Work Further Sources 6/4/2011Wrap Up- 1McLab Tutorial, Laurie Hendren,
McLab Tutorial Part 2 – Introduction to MATLAB Functions and Scripts Data and Variables Other Tricky "Features" 6/4/2011Matlab-
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Data Flow Analysis Compiler Design Nov. 3, 2005.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Chapter 19 Java Data Structures
Data Structures Using C++ 2E
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
McLab Tutorial Laurie Hendren, Rahul Garg and Nurudeen Lameed Other McLab team members: Andrew Casey, Jesse Doherty, Anton Dubrau,
McLab Tutorial Part 6 – Introduction to the McLab Backends MATLAB-to-MATLAB MATLAB-to-Fortran90 (McFor) McVM with JIT 6/4/2011Backends-
McLab Tutorial Part 4 – McLab Intermediate Representations High-level McAST Lower-level McLAST Transforming McAST to McLAST 6/4/2011IR-
Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Chapter 18 Java Collections Framework
1 Graph Coverage (4). Reading Assignment P. Ammann and J. Offutt “Introduction to Software Testing” ◦ Section
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
McLab Tutorial Part 3 – McLab Frontend Frontend organization Introduction to Beaver Introduction to JastAdd 6/4/2011 Frontend-1McLab.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Types in programming languages1 What are types, and why do we need them?
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to Software Testing (2nd edition) Chapter 7.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Optimizing The Optimizer: Improving Data Flow Analysis in Soot Michael Batchelder 4 / 6 / 2005 A COMP-621 Class Project.
Paul Ammann & Jeff Offutt
Lecture 12 Inheritance.
Software Development Java Classes and Methods
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Topic 10: Dataflow Analysis
Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Interpreter Pattern.
Collections Framework
Presentation transcript:

McLab Tutorial Part 5 – Introduction to the McLab Analysis Framework Exploring the Main Components Creating a Simple Analysis Depth-first and Structural Analyses Example: Reaching Definition Analysis 6/4/2011Analysis- 1McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAAAAAAAAA

McLab Analysis Framework A simple static flow analysis framework for MATLAB-like languages Supports the development of intra-procedural forward and backward flow analyses Extensible to new language extensions Facilitates easy adaptation of old analyses to new language extensions Works with McAST and McLAST (a simplified McAST) 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 2

McAST & Basic Traversal Mechanism Traversal Mechanism: – Depth-first traversal – Repeated depth-first traversal 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 3 ASTNode Stmt ForStmtReturnStmtAssignStmtExprStmt

Analysis- 4

The interface NodeCaseHandler Declares all methods for the action to be performed when a node of the AST is visited: public interface NodeCaseHandler { void caseStmt(Stmt node); void caseForStmt(ForStmt node); void caseWhileStmt(WhileStmt node); … } 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 5

The class AbstractNodeCaseHandler public class AbstractNodeCaseHandler implements NodeCaseHandler { … void caseStmt(Stmt node) { caseASTNode(node); } … } Implements the interface NodeCaseHandler Provides default behaviour for each AST node type except for the root node (ASTNode) 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 6

The analyze method Each AST node also implements the method analyze that performs an analysis on the node: public void analyze(NodeCaseHandler handler) handler.caseAssignStmt(this); } 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 7

Analysis- 8

Creating a Traversal/Analysis: 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 9 Involves 3 simple steps: 1.Create a concrete class by extending the class AbstractNodeCaseHandler 2.Provide an implementation for caseASTNode 3.Override the relevant methods of AbstractNodeCaseHandler

An Example: StmtCounter 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 10 Counts the number of statements in an AST Analysis development Steps: 1.Create a concrete class by extending the class AbstractNodeCaseHandler 2.Provide an implementation for caseASTNode 3.Override the relevant methods of AbstractNodeCaseHandler

An Example: StmtCounter 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis Create a concrete class by extending the class AbstractNodeCaseHandler public class StmtCounter extends AbstractNodeCaseHandler { private int count = 0; … // defines other internal methods }

An Example: StmtCounter --- Cont’d 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis Provide an implementation for caseASTNode public void caseASTNode( ASTNode node){ for(int i=0; i<node.getNumChild(); ++i) { node.getChild(i).analyze(this); }

An Example: StmtCounter --- Cont’d 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis Override the relevant methods of AbstractNodeCaseHandler public void caseStmt(Stmt node) { ++count; caseASTNode(node); }

An Example: StmtCounter --- Cont’d 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 14 public class StmtCounter extends AbstractNodeCaseHandler { private int count = 0; private StmtCounter() { super(); } public static int countStmts(ASTNode tree) { tree.analyze(new StmtCounter()); } public void caseASTNode( ASTNode node){ for(int i=0; i<node.getNumChild(); ++i) { node.getChild(i).analyze(this);} } public void caseStmt(Stmt node) { ++count; caseASTNode(node); }

Tips: Skipping Irrelevant Nodes 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 15 For many analyses, not all nodes in the AST are relevant; to skip unnecessary nodes override the handler methods for the nodes. For Example: public void caseExpr(Expr node) { return; } Ensures that all the children of Expr are skipped

Analysis-16

Flow Facts: The interface FlowSet 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 17 The interface FlowSet provides a generic interface for common operations on flow data public interface FlowSet { public FlowSet clone(); public void copy(FlowSet dest); public void union(FlowSet other); public void intersection(FlowSet other); … }

The Analysis interface 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 18 Provides a common API for all analyses Declares additional methods for setting up an analysis: public interface Analysis extends NodeCaseHandler { public void analyze(); public ASTNode getTree(); public boolean isAnalyzed(); public A newInitialFlow(); … }

Depth-First Analysis 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 19 Traverses the tree structure of the AST by visiting each node in a depth-first order Suitable for developing flow-insensitive analyses Default behavior implemented in the class AbstractDepthFirstAnalysis: AbstractNodeCaseHandler > Analysis AbstractDepthFirstAnalysis

Creating a Depth-First Analysis: 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 20 Involves 2 steps: 1.Create a concrete class by extending the class AbstractDepthFirstAnalysis a)Select a type for the analysis’s data b)Implement the method newInitialFlow c)Implement a constructor for the class 2.Override the relevant methods of AbstractDepthFirstAnalysis

Depth-First Analysis: NameCollector 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 21 Associates all names that are assigned to by an assignment statement to the statement. Collects in one set, all names that are assigned to Names are stored as strings; we use HashSetFlowSet for the analysis’s flow facts. Implements newInitialFlow to return an empty HashSetFlowSet object.

Depth-First Analysis: NameCollector --- Cont’d 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis Create a concrete class by extending the class AbstractDepthFirstAnalysis public class NameCollector extends AbstractDepthFirstAnalysis > { private int HashSetFlowSet fullSet; public NameCollector(ASTNode tree) { super(tree); fullSet = newInitialFlow(); } … // defines other internal methods }

Depth-First Analysis: NameCollector --- Cont’d 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis Override the relevant methods of AbstractDepthFirstAnalysis private boolean inLHS = false; public void caseName(Name node) { if (inLHS) currentSet.add(node.getID()); }

Depth-First Analysis: NameCollector --- Cont’d 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis Override the relevant methods of AbstractDepthFirstAnalysis public void caseAssignStmt(AssignStmt node) { inLHS = true; currentSet = newInitialFlowSet(); analyze(node.getLHS()); flowSets.put(node, currentSet); fullSet.addAll(currentSet); inLHS = false; }

Depth-First Analysis: NameCollector --- Cont’d 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis Override the relevant methods of AbstractDepthFirstAnalysis public void caseParameterizedExpr (ParameterizedExpr node) { analyze(node.getTarget()); } …

Structural Analysis 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 26 Suitable for developing flow-sensitive analyses Computes information to approximate the runtime behavior of a program. Provides mechanism for: – analyzing control structures such as if-else, while and for statements; – handling break and continue statements Provides default implementations for relevant methods May be forward or backward analysis

Structural Analysis Class Hierarchy 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 27 AbstractNodeCaseHandler > StructuralAnalysis AbstractStructuralAnalysis > Analysis AbstractSimpleStructuralForwardAnalysis AbstractStructuralBackwardAnalysisAbstractStructuralForwardAnalysis AbstractSimpleStructuralBackwardAnalysis

The interface StructuralAnalysis 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 28 Extends the Analysis interface Declares more methods for structural type analysis: public interface StructuralAnalysis extends Analysis { public Map getOutFlowSets(); public Map getInFlowSets(); public void merge(A in1, A in2, A out); public void copy(A source, A dest); … }

Developing a Structural Analysis Involves the following steps: 1.Select a representation for the analysis’s data 2.Create a concrete class by extending the class: AbstractSimpleStructuralForwardAnalysis for a forward analysis and AbstractSimpleStructuralBackwardAnalysis for a backward analysis 3.Implement a suitable constructor for the analysis and the method newInitialFlow 4.Implement the methods merge and copy 5.Override the relevant node case handler methods and other methods 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 29

Analysis-30

Example: Reaching Definition Analysis For every statement s, for every variable v defined by the program, compute the set of all definitions or assignment statements that assign to v and that may reach the statement s A definition d for a variable v reaches a statement s, if there exists a path from d to s and v is not re-defined along that path. 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis- 31

Reach Def Analysis: An Implementation Step 1 Select a representation for the analysis’s data: HashMapFlowSet > We use a map for the flow data: An entry is an ordered pair (v, defs) where v denotes a variable and defs denotes the set of definitions for v that may reach a given statement. 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis-32

Reach Def Analysis: An Implementation Step 2 Create a concrete class by extending the class: AbstractSimpleStructuralForwardAnalysis for a forward analysis: public class ReachingDefs extends AbstractSimpleStructuralForwardAnalysis >> { … } 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis-33

Reach Def Analysis: An Implementation Step 3 Implement a suitable constructor and the method newInitialFlow for the analysis: public ReachingDefs(ASTNode tree) { super(tree); currentOutSet = newInitialFlow(); } public HashMapFlowSet > newInitialFlow() { return new HashMapFlowSet >(); } 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis-34

Reach Def Analysis: An Implementation Step 4a Implement the methods merge and copy: public void merge (HashMapFlowSet > in1, HashMapFlowSet > in2, HashMapFlowSet > out) { union(in1, in2, out); } public void copy(HashMapFlowSet > src, HashMapFlowSet > dest) { src.copy(dest); } 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis-35

Reach Def Analysis: An Implementation Step 4b public void union (HashMapFlowSet > in1, HashMapFlowSet > in2, HashMapFlowSet > out) { Set keys = new HashSet (); keys.addAll(in1.keySet()); keys.addAll(in2.keySet()); for (String v: keys) { Set defs = new HashSet (); if (in1.containsKey(v)) defs.addAll(in1.get(v)); if (in2.containsKey(v)) defs.addAll(in2.get(v)); out.add(v, defs); } 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis-36

Reach Def Analysis: An Implementation Step 5a Override the relevant node case handler methods and other methods : override caseAssignStmt(AssignStmt node) public void caseAssignStmt(AssignStmt node) { inFlowSets.put(node, currentInSet.clone() ); currentOutSet = new HashMapFlowSet > (); copy(currentInSet, currentOutSet); HashMapFlowSet > gen = new HashMapFlowSet > (); HashMapFlowSet > kill = new HashMapFlowSet > (); 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis-37

Reach Def Analysis: An Implementation Step 5b // compute out = (in - kill) + gen // compute kill for( String s : node.getLValues() ) if (currentOutSet.containsKey(s)) kill.add(s, currentOutSet.get(s)); // compute gen for( String s : node.getLValues()){ Set defs = new HashSet (); defs.add(node); gen.add(s, defs); } 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis-38

Reach Def Analysis: An Implementation Step 5c // compute (in - kill) Set keys = kill.keySet(); for (String s: keys) currentOutSet.removeByKey(s); // compute (in - kill) + gen currentOutSet = union(currentOutSet, gen); // associate the current out set to the node outFlowSets.put( node, currentOutSet.clone() ); } 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedAnalysis-39