Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Slicing for Refactoring Advanced SW Tools Seminar Jan 2005Yossi Peery.

Similar presentations


Presentation on theme: "Program Slicing for Refactoring Advanced SW Tools Seminar Jan 2005Yossi Peery."— Presentation transcript:

1 Program Slicing for Refactoring Advanced SW Tools Seminar Jan 2005Yossi Peery

2 Jan-05Yossi PeeryAdvanced SW Tools Seminar Agenda Slicing Overview Slicing Algorithms Slicing with Inference Rules Refactoring Overview Slice Extraction Refactoring Example NATE – Slicing Based Refactoring Tool

3 Jan-05Yossi PeeryAdvanced SW Tools Seminar Starter

4 Jan-05Yossi PeeryAdvanced SW Tools Seminar Program Slicing History Mark Weiser, 1981 Experimented with programmers to show that slices are: “The mental abstraction people make when they are debugging a program” [Weiser] Used Data Flow Equations Ottenstein & Ottenstein – PDG, 1984 Horowitz, Reps & Binkly – SDG, 1990

5 Jan-05Yossi PeeryAdvanced SW Tools Seminar What is a Slice? All the statements of a program that may affect the values of some variables in a set V at some point of interest p. Slicing Criterion: C = (p, V)

6 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slice Example A slice for the criterion (10, {product})

7 Jan-05Yossi PeeryAdvanced SW Tools Seminar What is it good for? Debugging Program Comprehension Reverse Engineering Program Testing Measuring Program Metrics Coverage, Overlap, Clustering Refactoring

8 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Properties Static Slicing Statically available information only No assumptions made on input Computed slice can never be accurate (minimal slice) Problem is undecidable – reduction to the halting problem Current static methods can only compute approximations Result may not be usefull

9 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Properties Dynamic Slicing Computed on a given input actual instead of might Useful for applications that provide are input driven (debugging, testing) Criterion: (n=-3, 5, {sign})

10 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Properties Amorphous & Semantic Slicing Allows any semantic preserving transformations Used for program comprehension and reverse engineering Instead of:We write if (n >= 0) if (n < 0) ;sign := -1 else sign := -1

11 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Properties Backward Slicing Original Slicing Method Backward Traversal of Program Flow Slicing starts from point p (C = (p, V)) Examines statements that are executed before p (in run-time) Keep statements that affect value of V at p, or execution of p. Not only statements that appear before p

12 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Properties Forward Slicing Forward Traversal of Program Flow Slicing starts from p (C = (p, V)) Examine all statements that are executed after p Keep statements that are affected by the values of V at p or by the execution of p Shows downstream code that depend on a specific variable or statement Can show the code affected by a modification to a single statement

13 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Properties Intraprocedural Slicing Computes slice within one procedure Assumes worse case for function calls Interprocedural Slicing Compute slice over an entire program Two ways for crossing procedure boundary Up – going from sliced procedure into calling procedure Down – going from sliced procedure into called procedure Must Be Context Sensitive

14 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithm CFG – Control Flow Graph Each program statement is a node A directed edge will connect between any 2 nodes that represent statements with a possible control flow between them. Special nodes: Start, Stop Definitions - There is a directed path from I to j - Set of nodes that are influenced by i - all of the variables that are defined (modified) at statement i. - all of the variables that are referenced (used) at statement i.

15 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithms

16 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithms Data Flow Equations (Weiser) Iterative Process (Over CFG) Compute consecutive sets of “relevant” variables for each node in the CFG using data dependencies Control dependences are not computed explicitly Variables of control predicates (if, while) are “indirectly relevant” if any one of the statements in their body is relevant Start with slicing criterion: C = (p, V) Continue until a fixed point is reached – last iteration didn’t find new relevant statements

17 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithms Iteration 0: Iteration k+1:

18 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithm

19 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithms Issues with algorithm Output statements are not included in slice Solution: print(x) ≡ out = out + x, out V Interprocedural Slicing Solution proposed by Weiser Can go up or down procedure calls Actual parameters of function call are changed to call parameters (or the opposite) Variables not in scope are removed Is not “context sensitive” – too inaccurate

20 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithms PDG – Program Dependance Graphs Each node represents a statement (like CFG) Directed Edges represent: Control Dependence (Bold Lines) – between a predicate and the statements it controls Data Dependence (Regular Lines) – between statements modifying a variable and those that may reference it Special “Entry” node is connected to all nodes that are not control dependant

21 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithms

22 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithm Slicing with PDG Slicing criterion is less general: C = ( p, Def(p) ∩ Ref(p) ) Graph is computed for a single procedure Slicing becomes a reachability problem A slice consits of all the nodes that have a directed path to the node in the slicing criterion are in the Linear in time, after graph is calculated Issues Method isn’t interprocedural

23 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithms SDG – System Dependence Graph New nodes: Call Site, Procedure Entry, Actual-in-argument, Actual-out-argument, Formal-in-parameter, Formal-out-parameter New edges: Call Edge – connect “call site” and “procedure entry” Parameter-In Edge – connect “Actual-in” with “Formal-in” Parameter-Out-Edge – connect “Actual-out” with “Formal-out”

24 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithm

25 Jan-05Yossi PeeryAdvanced SW Tools Seminar

26 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithms Context Sensitivity Can not be solved by data flow equations The procedure, included through the procedure will include the call site and consequently Solved by SDG New summary edges (dotted) represent transitive dependences between “actual- in” and “actual-out” nodes. Slice is calculated in 2 passes (instead of 1): 1.Follow all edges except “parameter-out” 2.Follow all edges except “parameter-in”

27 Jan-05Yossi PeeryAdvanced SW Tools Seminar

28 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing Algorithm SDG - Issues Slicing remains a reachability problem SDG of a program is complex and costly to build (time, space) After computation, many different slices can be found using the same graph Is not efficient for developing code OO concepts and unstructured control flow (jump statements, exceptions) further complicate the graph

29 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing with Inference Rules Concept Use inference rules when traversing backwards the flow of the program to determine relevant statements Rules are applied on a specific configuration of S – Statement or sequence of statements that have been analyized Γ – Current slicing context R – Set of statements that are relevant (so far) Similar in nature to data flow equations method

30 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing with Inference Rules Context: Inference Rule: Initial Configuration: Our Example Final Configuration:

31 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing with Inference Rules Rule Example Inference rules are defined so that, at each step, there is at most one rule that matches the configuration

32 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slicing with Inference Rules Features Supports interprocedural slicing Context Sensitive Can be extended to support other language features such as: Complex expressions Array access Variable declarations Structured Jumps (break, continue) Object-oriented slicing (scoping, polymorphism) Aliasing

33 Jan-05Yossi PeeryAdvanced SW Tools Seminar Refactoring Overview Gradually improving design of existing code Source code transformations that, Preserve behavior of original system Manually or Automated Introduced by William Opdyke, 1992 Formally defined the reasonable behavior preservation degree expected from a refactoring tool Formally Disciplined by Martin Fowler 2000 Formal description of a refactoring Catalog of refactoring techniques

34 Jan-05Yossi PeeryAdvanced SW Tools Seminar Refactoring Overview Over 70 refactoring techniques can be found at: www.refactoring.com/catalog/index.html Refactoring categories: Composing Methods, Moving features between Objects, Organizing Data, Making Method Calls Simpler Some refactorings: Rename Method, Extract Method, Move Method, Replace Conditional with Polymorphism

35 Jan-05Yossi PeeryAdvanced SW Tools Seminar Refactoring Overview

36 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slice Extraction Refactoring

37 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slice Extraction Refactoring

38 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slice Extraction Refactoring Idea introduced by K. Maruyama, 2001 Is not limited to consecutive statements (like extract method) Allows the untangling of a single concern from a complex method Extracted slice can be refactored to New Method New Object New Aspect

39 Jan-05Yossi PeeryAdvanced SW Tools Seminar Slice Extraction Refactoring Slice Extraction Refactoring Concerns Not all of the statements in the slice can be deleted Deleted statements are determined by re-slicing for variables in statements that were not sliced: Preconditions & Limitations Clean compilation Return statement Global-scoped variables Input/Output statements

40 Jan-05Yossi PeeryAdvanced SW Tools Seminar Example – original code

41 Jan-05Yossi PeeryAdvanced SW Tools Seminar Example – Extract as Method

42 Jan-05Yossi PeeryAdvanced SW Tools Seminar Example – Extract as Object

43 Jan-05Yossi PeeryAdvanced SW Tools Seminar Example – Extract as Aspect

44 Jan-05Yossi PeeryAdvanced SW Tools Seminar Example – Extract as Aspect

45 Jan-05Yossi PeeryAdvanced SW Tools Seminar NATE – Slicing based Refactoring Tool Oxford University – Programming Tools Group http://web.comlab.ox.ac.uk/oucl/research/areas/progtools/proje cts/nate/nate.html Slicing based refactoring techniques for the Java programming language Currently supports a small subset of Java (March 2004) Extract Slice as Method Refactoring An Eclipse Plug-in

46 Jan-05Yossi PeeryAdvanced SW Tools Seminar NATE – Slicing based Refactoring Tool How is it used? Programmer selects: slicing criterion Name for new extracted method Tool performs: Compute Slice Check refactoring preconditions If extraction is possible – perform transformation Show original and transformed code in a preview dialog to the user for confirmation User can cancel any operation

47 Jan-05Yossi PeeryAdvanced SW Tools Seminar Implementation Uses JDT plug-in for access to the AST Slicing is done with inference rules The AST node of a statement is associated with a related inference rule Intensive use of visitor pattern to visit the AST and its related rules Each AST node can be marked as relevant or not NATE – Slicing based Refactoring Tool

48 Jan-05Yossi PeeryAdvanced SW Tools Seminar

49 Jan-05Yossi PeeryAdvanced SW Tools Seminar Refernces “Untangling: A Slice Extraction Refactoring - Ran Ettinger and Mathieu Verbaere (March 2004) “Program Slicing for Refactoring” - Mathieu Verbaere (September 2003) “Automated Tools for Refactoring” - Ran Ettinger (June 2003) “Program Slicing” - Mark Weiser (1981)


Download ppt "Program Slicing for Refactoring Advanced SW Tools Seminar Jan 2005Yossi Peery."

Similar presentations


Ads by Google