Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Slicing; Andreas Linder eXtreme Programming lab course 2004.

Similar presentations


Presentation on theme: "Program Slicing; Andreas Linder eXtreme Programming lab course 2004."— Presentation transcript:

1 program Slicing; Andreas Linder eXtreme Programming lab course 2004

2 2 Roadmap What is Program Slicing?(1) History and Intention(2) Basic Knowledge(2) Common Techniques(~14) Specialization „Cross the rubicon“(1) Two More Metrics(2)

3 3 What is Program Slicing? Definition: Reduces programs to those statements that are relevant for a particular computation What the question is: „What program statements potentially affect the value of variable v at statement s?“ int main() { int sum = 0; int i = 1; while (i < 11) { sum = sum + i; i = i + 1; } printf("%d\n", sum); printf("%d\n", i); } int main() { int sum = 0; int i = 1; while (i < 11) { sum = sum + i; i = i + 1; } printf("%d\n", sum); printf("%d\n", i); } Variable i, Last Statement

4 4 History: Foundation Stone Mark Weiser (1981) „Program Slicing“ „Program Slices: Formal psychological and practical investigations of an automatic program abstraction method“ (1979)

5 5 History: Intention Creating slices of programs was intended for use in debugging phases. 67% of programming effort goes into maintenance. (Zelkowitz et. al.) Experienced Programmers™ can reproduce functionality from memory. (Shneiderman) Slices will be remembered when debugging. (Weiser)

6 6 Roadmap/Intermission What is Program Slicing? History and Intention Basic Knowledge Common Techniques Specialization „Cross the rubicon“ Two More Metrics

7 7 Basic Knowledge Precondition: Slicing Criterion Pair (n, V) n is a line number V is a set of observed variables e.g. (8, {i}) Constraint 1. Source code must terminate. Constraint 2. Slices will be executable. Constraint 3. Slices will provide the same (subset of) behaviour. Constraint 4. There will always be at least one slice: The program itself.

8 8 Basic Knowledge Static vs. dynamic slices Static Analysis: All relevant Code. Dynamic Analysis: There will be assumptions about the input. Backward vs. forward slices Piece of Cake

9 9 Common Techniques Control Flow Graph (CFG) Control Dependence Graph Data Flow Graph Program Dependence Graph (PDC) Procedure Dependence Graph System Dependence Graph (SDC) Intra- und Interprocedural Slicing

10 10 Intraprocedural Slicing Example Program (1)read(n); (2)i := 1; (3)sum := 0; (4)product := 1; (5)while i <= n do begin (6)sum := sum + i; (7)product := product * i; (8)i := i + 1; end; (9)write(sum); (10)write(product); Criterion: (10, product)

11 11 Control FLOW Graph (1)read(n); (2)i := 1; (3)sum := 0; (4)product := 1; (5)while i <= n do begin (6)sum := sum + i; (7)product := product * i; (8)i := i + 1; end; (9)write(sum); (10)write(product);

12 12 Control DEPENDENCE Graph (1)read(n); (2)i := 1; (3)sum := 0; (4)product := 1; (5)while i <= n do begin (6)sum := sum + i; (7)product := product * i; (8)i := i + 1; end; (9)write(sum); (10)write(product);

13 13 DATA Flow Graph (1)read(n); (2)i := 1; (3)sum := 0; (4)product := 1; (5)while i <= n do begin (6)sum := sum + i; (7)product := product * i; (8)i := i + 1; end; (9)write(sum); (10)write(product);

14 14 PROGRAM Dependence Graph PDG Easy.

15 15 Interprocedural Slicing Method Build the Inverse Graph Perform a Depth First Search on the vertex containing the slicing criterion. Prevent circles! Mark every visited vertex! Assuming the existence of the PDC, slices can be retrieved in O(n).

16 16 Interprocedural Slicing Example Program procedure Example; begin read(n); i := 1; sum := 0; product := 1; while i <= n do begin Add(sum, i); Multiply(product, i); Add(i, 1); end; (9)write(sum); (10)write(product); end; Criterion: (10, product) procedure Add(a, b); begin a := a + b; end; procedure Multiply(c, d); begin j := 1; k := 0; while j <= d do begin Add(k, c); Add(j, 1); end; c := k; end;

17 17 SYSTEM Dependence Graph (Horwitz, Reps, Binkley) Easy, too.

18 18 Program Dependence Graph as a member of the SDG Multiply(product, i)

19 19 Preparing a method call Before the Call: Calling procedure copies actual parameters to temporary variables Parameter-in edges Parameter-out edges

20 20 PROCEDURE Dependence Graph as a member of the SDG Method call: Initialize formal parameters Before returning: Save formal parameters back to temporary vars.

21 21 Procedure Dependence Graph Quite the same for the Multiply procedure.

22 22 Interprocedural Slicing Method Invert the Graph! Phase 1: Start from criterion and ascend. Follow Data Flow, Control Flow, Call and Parameter-In DO NOT follow Def-Order, Parameter-Out Phase 2: Restart and descend. Follow Data Flow, Control Flow, Parameter-Out DO NOT follow Def-Order, Call, Parameter-In Again: Remember your visits!

23 23 Roadmap/Intermission What is Program Slicing? History and Intention Basic Knowledge Common Techniques Specialization „Cross the rubicon“ Two More Metrics

24 24 Crossing the Rubicon Another possibility to use Slicing is in implementing a special Refactoring “Extract method” Idea: Identifying procedures with a low Coverage value. Statement Extraction: Build slices for each/some value(s) computed in the method. Saving one of these back to its old name.

25 25 Very simple stupid Example begin dog = 0; for i := 0 to 5 do dog = dog + i; write(dog); dog = 0; for i := 5 downto 1 do dog = dog * i; write(dog); end;

26 26 Very simple stupid Example begin 1dog = 0; 2for i := 0 to 5 do 3dog = dog + i; 4write(dog); 5 dog = 0; 6for i := 5 downto 1 do 7dog = dog * i; 8write(dog); end; Criterion (7,{dog}) begin 1dog = 0; 2for i := 0 to 5 do 3dog = dog + i; 4write(dog); 5 dog = 0; 6for i := 5 downto 1 do 7dog = dog * i; 8write(dog); end; Criterion (4,{dog}) Coverage = 0,5Clustering = 1

27 27 Very simple stupid Example extractedProcedure2: begin dog = 0; for i := 5 downto 1 do dog = dog * i; write(dog); end; extractedProcedure1: begin dog = 0; for i := 0 to 5 do dog = dog + i; write(dog); end;

28 28 Roadmap/Intermission What is Program Slicing? History and Intention Basic Knowledge Common Techniques Specialization „Cross the rubicon“ Two More Metrics

29 29 Coverage Metric Compare the length of slices to the length of program/procedure! ~ ratio of mean slice length to program/procedure length. Low value (= long program, many short slices): Several distinct conceptual purposes

30 30 Overlap Slice How many statements in a slice are found only in that slice? ~ mean of the ratios of non-unique to unique statements. High overlap indicates very independent code.

31 31 Clustering Metric Are slices reflected in the code layout? ~ mean of the ratio of statements formerly adjacent to total statements in each slice. Low Value: Slices are intertwined spaghettically High Value: good statement grouping

32 32 Remember? Static slice dynamic slice Backward slice Forward slice Control Flow Graph Control Dependence Graph Program Dependence Graph Procedure Dependence Graph System Dependence Graph Data Flow Graph Coverage Overlap Clustering Interprocedural Slicing Intraprocedural Slicing Slicing Criterion Weiser First kiss Parameter-In/Out Extract Method QUESTIONS ? Thank you.


Download ppt "Program Slicing; Andreas Linder eXtreme Programming lab course 2004."

Similar presentations


Ads by Google