Program Slicing - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Program Slicing

Description:

Starts with observation that only a portion of a program's behavior is ... Impossibility of evaluating functional equivalence of two different pieces of code. ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 24
Provided by: csU66
Category:

less

Transcript and Presenter's Notes

Title: Program Slicing


1
Program Slicing
  • By
  • Mark Weiser

2
Overview
  • Introduction
  • Slicing Algorithms
  • Dataflow Algorithms
  • Practical Slicing
  • Interprocedural Slicing
  • Slicing Based Metrics
  • Conclusion

3
Introduction
  • Program Slicing New complimentary method of
    program decomposition.
  • Starts with observation that only a portion of a
    programs behavior is of interest.
  • Slicing Criterion If behavior of interest is
    expressed as the values of some sets of variables
    at some set of statements.
  • An executable program, whose behavior must be
    identical to the specified subset of original
    programs behavior is called Slice.

4
Example Of Slice
  • The original program
  • l BEGIN
  • 2 READ(X,Y)
  • 3 TOTAL 0.0
  • 4 SUM 0.0
  • 5 IF Xlt l
  • 6 THEN SUM Y
  • 7 ELSE BEGIN
  • 8 READ(Z)
  • 9 TOTAL XY
  • lO END
  • II WRITE(TOTAL,SUM)
  • 12 END.
  • on the value of Z at statement 12.
  • BEGIN
  • READ(X,Y)
  • IF X lt l
  • THEN
  • ELSE READ(Z)
  • END.
  • Slice on the value of X at statement 9.
  • BEGIN
  • REAB(X,Y)
  • END.

5
Example Of Slice
  • Slice on the value of TOTAL at statement 12.
  • BEGIN
  • READ(X,Y)
  • TOTAL 0
  • IF X lt-- l
  • THEN
  • ELSE TOTAL XY
  • END
  • Slice on the value of X at statement 9.
  • BEGIN
  • REAB(X,Y)
  • END.
  • on the value of Z at statement 12.
  • BEGIN
  • READ(X,Y)
  • IF X lt l
  • THEN
  • ELSE READ(Z)
  • END.
  • Slice on the value of TOTAL at statement 12.
  • BEGIN
  • READ(X,Y)
  • TOTAL 0
  • IF X lt-- l
  • THEN
  • ELSE TOTAL XY
  • END

6
Slicing Algorithms
  • Slicing Source code transformation of program .
  • Window Specifies as a statement and a set of
    variables.
  • Slicing criterion is a pair of lt i,v gt, where
  • i- number of statements
  • v- set of variable values
  • Two desirable properties in a slice
  • Slice obtained from original program by statement
    deletion.
  • Slice behavior should correspond to original
    program behavior.
  • Source code becomes ungrammatical with statement
    deletion.

7
Slicing Algorithm
  • Flowgraph is used to represent a program.
  • Flowgraph is a structure G ltN,E,n0gt, where
  • N set of nodes
  • E set of edges in NxN
  • n0 initial node
  • By deletion of statements (with single successor)
    a new flowgraph is produced.
  • Deleted statement have a single successor which
    also becomes for all the predecessors of the new
    flowgraph.
  • Window for new graph is lt succ(i),v gt.

8
Group Of Statements With A Single Successor
A
A
B
B
C
F
E
D
H
G
F
G
H
9
Slicing Algorithm
  • Identical behavior for slice and original program
    for all inputs which is too strong.
  • Finding smallest slice becomes a problem.
  • Impossibility of evaluating functional
    equivalence of two different pieces of code.
  • Thus, we use dataflow analysis.
  • Consider the example
  • 1 BEGIN
  • 2 READ(X)
  • 3 IF X 0
  • 4 THEN BEGIN
  • Perform infinite loop
  • without changing X.
  • 5 X 1
  • 6 END
  • 7 ELSE X 2
  • 8 END.

10
Dataflow Algorithms
  • Begins with backward tracing similar to reaching
    definitions.
  • Finding which var assignments can reach the vars
    observed through window of the criterion.
  • For example, consider statement yx followed
    by zy.
  • R0,c(n) variables at statement n whose values
    directly effect what is observed through window
    defined by criterion c.
  • Consider dataflow information DEF(n) and REF(n)
    to define R0,c.

11
Dataflow Algorithms
  • Assumption w and x as variable names, with w in
    DEF(n) and x in REF(n) then value of x can
    influence the value of w.
  • Above assumption cannot be applied to multiple
    assignments.
  • More precise formulation
  • RO,C(n), where C lti,vgt, is defined
    recursively as follows
  • RO,C(n) all variables v such that either
  • I. ni and v is in V,
  • or 2. n is an immediate predecessor of
    a node m, such that either
  • a) v is in REF(n) and there is a w in
    both DEF(n) and RO,C(m),
  • or b) v is not in DEF(n) and v is inRo,
    c(m).

12
Dataflow Algorithms
  • If REF and DEF information not obtained by code
    inspection we use interprocedural dataflow
    method.
  • Statements included in slice by R0,c are
    denoted S0,c.
  • S0,c all nodes n such that R0,c(n)
    intersect DEF(n) is non-empty.
  • Consider following example
  • Slicing Criterion C lt5,Zgt
  • l READ(X)
  • 2 IFXltI
  • 3 THEN Z l
  • 4 ELSE Z 2
  • 5 WRITE(Z)

13
Dataflow Algorithms
  • Considering branch statements that may not
    execute some statements we use the inverse
    denominator.
  • With this we could exert an indirect influence
    over any variable directly influenced by x.
  • Slice properties
  • Let C lti,vgt, D lti,wgt, E lti,v union wgt,
  • C, D, and E are all slicing criteria .
  • Property
  • SC union SD SE
  • RC union RID RE
  • Even this can produce anomalies as shown in next
    example .

14
Dataflow Algorithms
  • l A constant
  • 2 WHILE P(K)
  • DO
  • BEGIN
  • 3 IF Q(C)
  • THEN BEGIN
  • 4 BA
  • 5 XL
  • END
  • ELSE BEGIN
  • 6 CB
  • 7 Y2
  • END
  • 8 KKI
  • END
  • 9 ZXY
  • l0 WRITE(Z)
  • Criterion lt I0,Z gt
  • The first level of anlaysis gives
  • SO 5,7,g
  • BO 2,3
  • Because statement 3 uses C, the next level shows
  • Sl 1,2,3,4,5,6,7,8,9

15
Practical Slicing
  • Preliminary indications that slicing is useful
    were obtained by showing that experienced
    programmers already use slicing during debugging.
  • Later Slicing tools are constructed.
  • Using these results show that slicing is fairly
    fast and eliminates large number of unnecessary
    statements.
  • Practically experiment was conducted in which
    participants were 21 experienced programmers from
    University of Michigan.
  • This shows difference between slicing taught or
    not taught as debugging technique.

16
Slicing Real Programs
  • First slicer was implemented to DAVE program for
    analyzing FORTRAN source code.
  • This could slice only main program.
  • Prototype system slicing criteria based on
    variables whose values were printed near the end
    of program.
  • Results from
    Prototype Slicer
  • PROGRAM Number of Lines
    Average Slice Size
  • MMGS 15 15
  • PH2B 60 20
  • TALLY 67 25
  • MAIN 380 lO0

17
Slicing Real Programs
  • MMGS is a matrix multiplication subroutine in IBM
    Scientific Subroutine Library.
  • Code was difficult to slice. Not possible even by
    trivial slicing criterion as trivial is not
    applicable to all mathematical software.
  • Now slicer is implemented(to slice programs in
    SIMPL family) for a language independent
    representation.
  • A program is incorporated into the SIMPL compiler
    which writes to a file all the information
    necessary to slice program.
  • Programmer is not aware of this file until
    program is sliced.

18
Interprocedural Slicing
  • The SIMPL slicer performs full interprocedural
    dataflow analysis.
  • Two steps to slice systems of subroutines
  • Single slice is made. Summary dataflow
    information about call to other procedures is
    used but no attempt is made to slice those
    procedures.
  • To procedure calls that influence variables
    relevant to slice in step one, a new slicing
    criterion is created in the call procedure.
  • Steps 1 and 2 are repeated until no new
    criterion is generated.
  • Two ways slicing can cross a procedure boundary.
  • This is shown in the example presented.

19
Interprocedural Slicing
  • Extending slices to called and calling routines
  • l READ(A,B)
  • 2 CALL Q(A,B)
  • 3 ZAB
  • procedure Q(var x,y integer)
  • 4 XO
  • 5 Y X3
  • Slicing on criterion Clt3,zgt causes new
    slicing criterion Clt6,x,ygt to be generated.
    The complete on C is 2,3,4,5.
  • Now take Dlt4,ygt causes new one Dlt2,Bgt to
    be generated. Complete slice on D is 1.

20
Slicing Based Metrics
  • Two investigations in progress
  • Use slicing in program debugging and maintenance
    aid.
  • Second is slicing based metrics. Number of slices
    their spacial arrangement etc.. hold significant
    information about restructuring a program.
  • Some slicing based metrics
  • Coverage ratio of mean slice length to program
    length.
  • Overlap mean of ratios of non-unique to unique
    statements in each slice.
  • Clustering reveals the degree to which slices
    are reflected in the original code layout.
  • Parallelism number of slices which have few
    statements in common.
  • Tightness measures number of statements which
    are in every slice.
  • These metrics are applied in student-written
    load-and-go compilers.

21
Slicing Based Metrics
  • Slicing on every output statement lead to many
    similar slices.
  • Clustering together slices gives a more
    meaningful set of slices on which to apply
    metrics.
  • Fo r instance, the 48 output statements in one
    compiler could be clustered into seven
    categories
  • 1)object code interpretation messages and errors
  • 2). source code scanning messages and errors.
  • 3)miscellaneous fixed messages(e.g "EXECUTION
    BEGINS")
  • 4) global error messages (e.g. "NO PROCEDURES")
  • 5) symbol table listing
  • 6)object code listing
  • 7) symbol table error messages.

22
Slicing Based Metrics
  • The 48 original slicing criteria generated 217
    additional slicing criteria as a result of called
    and calling procedures.
  • Calculating the 217 intraproceduarl slices took
    about 10 minutes of cpu time.

23
Conclusion
  • Limited to code already written, it may prove
    useful during debugging, testing, and
    maintenance.
  • Many slicing decompositions can be chosen by
    selecting appropriate criteria.
  • This paper concentrates on basic methods for
    slicing programs.
  • Future work on slicing-based programming aids and
    program metrics is necessary .
Write a Comment
User Comments (0)
About PowerShow.com