CS 110: Code Analysis - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

CS 110: Code Analysis

Description:

... infinite loops. Infinite loops are usually undesirable. But not always. ... Uninitialized variables are usually undesirable. Should compilers make them illegal? ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 11
Provided by: movers5
Learn more at: https://www.cs.odu.edu
Category:

less

Transcript and Presenter's Notes

Title: CS 110: Code Analysis


1
CS 110 Code Analysis
  • C. M. Overstreet
  • Old Dominion University
  • Fall 2005

2
Overview
  • Some motivation for code analysis
  • Some examples some easy, some hard, some
    impossible
  • Some examples of where used

3
Example 1 elimination of infinite loops
  • Infinite loops are usually undesirable.
  • But not always. Why?
  • Can you write a program that checks another
    program for infinite loops?
  • That works all of the time?
  • Initial idea just consider while loops
  • Check loop body to see if any action modifies
    some loop control variables.
  • Does this work?
  • If no change, is loop infinite?
  • Can one always do this? How? What about input
    data?
  • What about for loops?

4
Example 1 (cont.)
  • Point often partial solutions are useful even if
    a general solution does not exist.
  • Question What is the Halting Problem and why is
    it important?

5
Static Dynamic Analysis
  • From their beginning, compilers have tried to
    optimize code.
  • Done by primarily by static analysis
  • Figure things out by analyzing source code
  • Many types of useful information about code
    itself can only be obtained by running to code
    and monitoring its behaviors.
  • This is dynamic analysis

6
Possible Common Compiler Optimizations
  • Flag unused variables
  • Very common
  • Make loops more efficient by moving code around
  • Very common
  • Eliminating code that cannot affect program
    output
  • Not done too hard!
  • Worked on a project for Navy a few years ago in
    which program manager suspected 80 of code was
    useless
  • About 1,000,000 lines of source code

7
Examples of analysis tools
  • gcov
  • Tool to identify which statements have been
    executed while testing a program
  • gprof
  • Tool to tell a programmer which parts of a
    program use the most execution time. Useful when
    improving program speed.
  • What are memory leaks?

8
Example 3 uninitialized variables
  • Uninitialized variables are usually undesirable.
  • Should compilers make them illegal?
  • Can compilers make them illegal?
  • Can you write a program which detects
    uninitialized variables?
  • Always? Sometimes?
  • Using static analysis?
  • i.e., uninitialized vars detected at compile time
  • Using dynamic analysis?
  • i.e. uninitialized vars detected at run time

9
Another example side effects of function calls
  • Consider the statement
  • x f(a,b) a g(a,c)
  • Considered to be a dangerous statement.
    "Careful" programmers avoid this type of coding.
  • Why?

10
Weiser Program Slices
  • slice criterion lt13, zgt
  • 1, 2, 3, 6, 8, 9, 11, 13
  • slice criterion lt10, xgt
  • 1, 2, 3, 13
  • slice criterion lt13, totalgt
  • 1, 2, 3, 4, 6, 8, 10, 11, 13
  • 1 void main()
  • 2 int x, y, total, sum, z
  • 3 cin gtgt x gtgt y
  • 4 total 0.0
  • 5 sum 0.0
  • 6 if ( x lt 1 )
  • 7 sum y
  • 8 else
  • 9 cin gtgt z
  • 10 total xy
  • 11
  • 12 cout ltlt total ltlt sum ltlt endl
  • 13
Write a Comment
User Comments (0)
About PowerShow.com