1 / 31

program Slicing;

program Slicing;. Andreas Linder eXtreme Programming lab course 2004. 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). What is Program Slicing?.

mdavison
Download Presentation

program Slicing;

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. program Slicing; Andreas Linder eXtreme Programming lab course 2004

  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. What is Program Slicing? 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); } • 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?“ Variable i, Last Statement

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

  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. Roadmap/Intermission • What is Program Slicing? • History and Intention • Basic Knowledge • Common Techniques • Specialization „Cross the rubicon“ • Two More Metrics

  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. 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. 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. Intraprocedural SlicingExample Program Criterion: (10, product) • read(n); • i := 1; • sum := 0; • product := 1; • while i <= n do begin • sum := sum + i; • product := product * i; • i := i + 1; • end; • (9)write(sum); • (10)write(product);

  11. Control FLOW Graph • product := product * i; • i := i + 1; • end; • (9)write(sum); • (10)write(product); • read(n); • i := 1; • sum := 0; • product := 1; • while i <= n do begin • sum := sum + i;

  12. Control DEPENDENCE Graph • product := product * i; • i := i + 1; • end; • (9)write(sum); • (10)write(product); • read(n); • i := 1; • sum := 0; • product := 1; • while i <= n do begin • sum := sum + i;

  13. DATA Flow Graph • product := product * i; • i := i + 1; • end; • (9)write(sum); • (10)write(product); • read(n); • i := 1; • sum := 0; • product := 1; • while i <= n do begin • sum := sum + i;

  14. PROGRAM Dependence GraphPDG Easy.

  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. Interprocedural SlicingExample 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; 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; Criterion: (10, product)

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

  18. Program Dependence Graphas a member of the SDG Multiply(product, i)

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

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

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

  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. Roadmap/Intermission • What is Program Slicing? • History and Intention • Basic Knowledge • Common Techniques • Specialization „Cross the rubicon“ • Two More Metrics

  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. 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. Very simple stupid Example begin 1 dog = 0; 2 for i := 0 to 5 do 3 dog = dog + i; 4 write(dog); 5 dog = 0; 6 for i := 5 downto 1 do 7 dog = dog * i; 8 write(dog); end; Criterion (7,{dog}) begin 1 dog = 0; 2 for i := 0 to 5 do 3 dog = dog + i; 4 write(dog); 5 dog = 0; 6 for i := 5 downto 1 do 7 dog = dog * i; 8 write(dog); end; Criterion (4,{dog}) Coverage = 0,5 Clustering = 1

  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. Roadmap/Intermission • What is Program Slicing? • History and Intention • Basic Knowledge • Common Techniques • Specialization „Cross the rubicon“ • Two More Metrics

  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. 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

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

More Related