1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Path Analysis Why path analysis for test case design?
ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Intermediate Code Generation
Course Outline Traditional Static Program Analysis Software Testing
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Chapter 6 Path Testing Software Testing
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt.
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
Program Representations Xiangyu Zhang. CS590F Software Reliability Why Program Representations  Initial representations Source code (across languages).
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
ECE1724F Compiler Primer Sept. 18, 2002.
Chair of Software Engineering Fundamentals of Program Analysis Dr. Manuel Oriol.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
1 CS 201 Compiler Construction Lecture 1 Introduction.
Lecture 6 Program Flow Analysis Forrest Brewer Ryan Kastner Jose Amaral.
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Program Representations Xiangyu Zhang. CS590Z Software Defect Analysis Program Representations  Static program representations Abstract syntax tree;
Intermediate Code. Local Optimizations
ECE355 Fall 2004Software Reliability1 ECE-355 Tutorial Jie Lian.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 1 Defect testing l Testing programs to establish the presence of system defects.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Compiler Chapter# 5 Intermediate code generation.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
Compilers Modern Compiler Design
Chapter 8 Path Testing. Path testing Path testing is a “design structural testing” in that it is based on detailed design & the source code of the program.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Agent program is the one part(class)of Othello program. How many test cases do you have to test? Reversi [Othello]
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Control Flow Analysis Compiler Baojian Hua
White Box Testing by : Andika Bayu H.
Cyclomatic Complexity Philippe CHARMAN Last update:
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
Compiler Principles Fall Compiler Principles Lecture 8: Dataflow & Optimizations 1 Roman Manevich Ben-Gurion University of the Negev.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
High-level optimization Jakub Yaghob
Structural testing, Path Testing
White-Box Testing.
Dataflow Testing G. Rothermel.
Control Flow Analysis CS 4501 Baishakhi Ray.
White-Box Testing.
CS 201 Compiler Construction
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Code Optimization Overview and Examples Control Flow Graph
Control Flow Analysis (Chapter 7)
Data Flow Analysis Compiler Design
8 Code Generation Topics A simple code generator algorithm
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
Whitebox Testing.
CS 201 Compiler Construction
Presentation transcript:

1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3) Do problems 1 and 2 from the representation and analysis paper

2 Intermediate Representations Program analyses grew out of the compiler world, where they were used to help optimize code. Many optimizations depend on analyses of intermediate representations of software, such as: o Parse trees o Abstract syntax trees o Three-address code

3 Intermediate representation Code Compilation and Analysis Parsing, lexical analysis Source program Code generation, optimization Target code Code execution Intermediate representation Analyze intermediate representation, perform additional analysis on the results Use this information for code optimization techniques

4 Tree Representations Representations Parse trees represent concrete syntax Abstract syntax trees represent abstract syntax Concrete versus abstract syntax Concrete syntax shows structure and is language-specific Abstract syntax shows structure

5 Example: Grammar Example 1.a := b + c 2.a = b + c; Grammar for 1 stmtlist  stmt | stmt stmtlist stmt  assign | if-then | … assign  ident “:=“ ident binop ident binop  “+” | “-” | … Grammar for 2 stmtlist  stmt “;” | stmt”;” stmtlist stmt  assign | if-then | … assign  ident “=“ ident binop ident Ÿ binop  “+” | “-” | …

6 Example: Parse Tree Example 1.a := b + c 2.a = b + c; Parse tree for 1Parse tree for 2

7 Example: Parse Tree Example 1.a := b + c 2.a = b + c; stmt stmtlist ident assign a ident“:=“binop cb ident “+” stmt stmtlist ident assign a ident“=“binop cb ident “+” “;” Parse tree for 1Parse tree for 2

8 Example: Abstract Syntax Tree Example 1.a := b + c 2.a = b + c; Abstract syntax tree for 1 and 2 assign a add bc

9 Three Address Code General form: x := y op z May include temporary variables (intermediate values) May reference arrays: a[t1] Specific forms (examples) Assignment Binary: x := y op z Unary: x := op y Copy: x := y Jumps Unconditional: goto (L) Conditional: if x relational-op y goto (L) …

10 Example: Three Address Code if a > 10 then x = y + z else x = y – z … 1.if a > 10 goto (4) 2.x = y – z 3.goto (5) 4.x = y + z 5.… Source code Three address code

11 Larger Example: 3 Address Code (1)i := m-1 (2)j := n (3)t1 := 4*n (4)v := a[t1] (5)i := i+1 (6)t2 := 4*I (7)t3 := a[t2] (8)if t3 < v goto (5) (9)j := j-1 (10)t4 := 4*j (11)t5 := a[t4] (12)if t5 > v goto (9) (13)if I >= j goto (23) (14)t6 := 4*I (15)x := a[t6] (16) t7 := 4*I (17) t8 := 4*j (18) t9 := a[t8] (19) a[t7] := t9 (20) t10 := 4*j (21) a[t10] := x (22) goto (5) (23) t11 := 4*I (24) x := a[t11] (25) t12 := 4*I (26) t13 := 4*n (27) t14 := a[t13] (28) a[t12] := t14 (29) t15 := 4*n (30) a[t15] := x

12 Control Flow Graph One of the most basic program representations Nodes represent statements or basic blocks Edges represent flow of control between nodes To build a CFG:  Construct basic blocks  Join blocks together with labeled edges

13 Basic Blocks A basic block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without halt or possibility of branch except at the end A basic block may or may not be maximal For compiler optimizations, maximal basic blocks are desirable For software engineering tasks, basic blocks that represent one source code statement are often used

14 Computing Basic Blocks (algorithm) Input: a sequence of procedure statements Output: A list of basic blocks with each statement in exactly one block Method:  Determine the set of leaders: the first statements of basic blocks, using the following rules: oThe first statement in the procedure is a leader oAny statement that is the target of a conditional or unconditional goto statement is a leader. oAny statement that immediately follows a conditional or unconditional goto statement is a leader.  Construct the basic blocks using the leaders. For each leader, its basic block consists of the leader and all statements up to but not including the next leader or the end of the procedure.

15 (1)i := m-1 (2)j := n (3)t1 := 4*n (4)v := a[t1] (5)i := i+1 (6)t2 := 4*I (7)t3 := a[t2] (8)if t3 < v goto (5) (9)j := j-1 (10)t4 := 4*j (11)t5 := a[t4] (12)if t5 > v goto (9) (13)if I >= j goto (23) (14) t6 := 4*l (15) x := a[t6] (16) t7 := 4*I (17) t8 := 4*j (18) t9 := a[t8] (19) a[t7] := t9 (20) t10 := 4*j (21) a[t10] := x (22) goto (5) (23) t11 := 4*I (24) x := a[t11] (25) t12 := 4*I (26) t13 := 4*n (27) t14 := a[t13] (28) a[t12] := t14 (29) t15 := 4*n (30) a[t15] := x Example: Compute Basic Blocks on this 3 Address Code

16 (1)i := m-1 (2)j := n (3)t1 := 4*n (4)v := a[t1] (5)i := i+1 (6)t2 := 4*I (7)t3 := a[t2] (8)if t3 < v goto (5) (9)j := j-1 (10)t4 := 4*j (11)t5 := a[t4] (12)if t5 > v goto (9) (13)if I >= j goto (23) (14) t6 := 4*I (15) x := a[t6] (16) t7 := 4*I (17) t8 := 4*j (18) t9 := a[t8] (19) a[t7] := t9 (20) t10 := 4*j (21) a[t10] := x (22) goto (5) (23) t11 := 4*I (24) x := a[t11] (25) t12 := 4*I (26) t13 := 4*n (27) t14 := a[t13] (28) a[t12] := t14 (29) t15 := 4*n (30) a[t15] := x Example: Compute Basic Blocks on this 3 Address Code: leaders

17 (1)i := m-1 (2)j := n (3)t1 := 4*n (4)v := a[t1] (5)i := i+1 (6)t2 := 4*I (7)t3 := a[t2] (8)if t3 < v goto (5) (9)j := j-1 (10)t4 := 4*j (11)t5 := a[t4] (12)if t5 > v goto (9) (13)if I >= j goto (23) (14) t6 := 4*I (15) X := a[t6] (16) t7 := 4*I (17) t8 := 4*j (18) t9 := a[t8] (19) a[t7] := t9 (20) t10 := 4*j (21) a[t10] := x (22) goto (5) (23) t11 := 4*I (24) x := a[t11] (25) t12 := 4*I (26) t13 := 4*n (27) t14 := a[t13] (28) a[t12] := t14 (29) t15 := 4*n (30) a[t15] := x Example: Compute Basic Blocks on this 3 Address Code: blocks

18 Computing Control Flow Graph from Basic Blocks (algorithm) Input: a list of basic blocks Output: A list of control-flow graph (CFG) nodes and edges Method:  Create entry and exit nodes; create edge (entry, B1); create (Bk, exit) for each Bk that represents an exit from program  Add CFG edge from Bi to Bj if Bj can immediately follow Bi in some execution, i.e., oThere is a conditional or unconditional goto from the last statement of Bi to the first statement of Bj or oBj immediately follows Bi in the order of the program and Bi does not end in an unconditional goto statement  Label edges that represent conditional transfers of control as “T” (true) or “F” (false); other edges are unlabeled

19 (1)i := m-1 (2)j := n (3)t1 := 4*n (4)v := a[t1] (5)i := i+1 (6)t2 := 4*I (7)t3 := a[t2] (8)if t3 < v goto (5) (9)j := j-1 (10)t4 := 4*j (11)t5 := a[t4] (12)if t5 > v goto (9) (13)if I >= j goto (23) (14) t6 := 4*I (15) X := a[t6] (16) t7 := 4*I (17) t8 := 4*j (18) t9 := a[t8] (19) a[t7] := t9 (20) t10 := 4*j (21) a[t10] := x (22) goto (5) (23) t11 := 4*I (24) x := a[t11] (25) t12 := 4*I (26) t13 := 4*n (27) t14 := a[t13] (28) a[t12] := t14 (29) t15 := 4*n (30) a[t15] := x Example: Compute Control Flow Graph from Basic Blocks B1 B2 B3 B4 B5 B6

20 Example: Compute Control Flow Graph from Basic Blocks B1 B2 B3 B4 B6 B5 T F T T F F Entry Exit

21 Computing Control Flow from Source Code (example) Procedure AVG S1 count = 0 S2 fread(fptr, n) S3 while (not EOF) do S4 if (n < 0) S5 return (error) else S6 nums[count] = n S7 count ++ endif S8 fread(fptr, n) endwhile S9 avg = mean(nums,count) S10 return(avg)

22 Computing Control Flow from Source Code (example) Procedure AVG S1 count = 0 S2 fread(fptr, n) S3 while (not EOF) do S4 if (n < 0) S5 return (error) else S6 nums[count] = n S7 count ++ endif S8 fread(fptr, n) endwhile S9 avg = mean(nums,count) S10 return(avg) S1 S2 S3 S4 S5S6 S7 S8 S9 S10 entry exit F T F T

23 Computing Control Flow from Source Code (maximal basic blocks) Procedure AVG S1 count = 0 S2 fread(fptr, n) S3 while (not EOF) do S4 if (n < 0) S5 return (error) else S6 nums[count] = n S7 count ++ endif S8 fread(fptr, n) endwhile S9 avg = mean(nums,count) S10 return(avg) S1 S2 S3 S4 S5S6 S7 S8 S9 S10 entry exit F T F T

24 Computing Control Flow from Source Code (another example) Procedure Trivial S1 read (n) S2 switch (n) case 1: S3 write (“one”) break case 2: S4 write (“two”) case 3: S5 write (“three”) break default S6 write (“Other”) endswitch end Trivial

25 Computing Control Flow from Source Code (another example) Procedure Trivial S1 read (n) S2 switch (n) case 1: S3 write (“one”) break case 2: S4 write (“two”) case 3: S5 write (“three”) break default S6 write (“Other”) endswitch end Trivial S1 S2 S3S4 S5S6 entry exit

26 Computing Control Flow from Source Code (maximal basic blocks) Procedure Trivial S1 read (n) S2 switch (n) case 1: S3 write (“one”) break case 2: S4 write (“two”) case 3: S5 write (“three”) break default S6 write (“Other”) endswitch end Trivial S1 S2 S3S4 S5S6 entry exit

27 Applications of Control Flow Program understanding: In CFGs, program structure and flow are explicit Complexity: Cyclomatic (McCabe’s) Computed in several ways: Edges –nodes +2 Number of regions in CFG Number of decision statements + 1 (if program is structured) Indicates number of test cases needed; indicates difficulty of maintaining

28 Applications of Control Flow Testing: branch, path, basis path Branch: must test 1  2, 1  3, 4  5, 4  8, 5  6, 5  7 Path: infinite number because of loop Basis path: set of paths such that each path executes at least one more edge (cyclomatic complexity gives max necessary); example: 1,2,4,8; 1,3,4,5,6,7,4,

29 Applications of Control Flow Support for Other Analyses: Dominator Postdominator Data dependence Control dependence Points-to Regression test selection

30 Terminology: Levels of Analysis Local: within a single basic block or statement Intraprocedural: within a single procedure, function, or method (sometimes intramethod) Interprocedural: across procedure boundaries, procedure call, shared globals, etc. Intraclass: within a single class Interclass: across class boundaries...

Exercise How would we represent interprocedural control flow; that is, control flow involving an entire program containing several procedures? 31 Procedure A begin S3 call B() S4 end Procedure Main begin S1 if (P1) then call A() else call B() endif call A() S2 end Procedure B begin S5 end

Step 1: Draw individual CFGs 32

Step 2: Connect CFGs (how?) 33