Presentation is loading. Please wait.

Presentation is loading. Please wait.

C. M. Overstreet Old Dominion University Fall 2007

Similar presentations


Presentation on theme: "C. M. Overstreet Old Dominion University Fall 2007"— Presentation transcript:

1 C. M. Overstreet Old Dominion University Fall 2007
CS 110: Code Analysis C. M. Overstreet Old Dominion University Fall 2007

2 Overview Why code analysis? Examples:
some easy some hard some impossible Will give a few examples of where used 3/20/06

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 some of the time? That finds all infinite loops Might be a useful compiler feature 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? 3/20/06

4 Examples Is this an infinite loop? How do you know?
while ( x < Max ) { if ( x > 10 ) x--; y = Max + x; } Is this an infinite loop? How do you know? while ( x < MaxVal ) { ftn1(); y = anotherFunction( a, b, c ); j = x + 1; 3/20/06

5 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? 3/20/06

6 Static & Dynamic Analysis
From their beginning, compilers have tried to optimize code. Done primarily by static analysis Figure things out by analyzing source code Some useful information about code can only be obtained by running to code and monitoring its behaviors. This is dynamic analysis 3/20/06

7 Possible Common Compiler Optimizations
Flag unused variables Very common Make loops more efficient by reordering code Eliminating code that cannot affect program output Generally 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 2,000,000 lines of source code 3/20/06

8 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 program performance is crucial. What are memory leaks? 3/20/06

9 Example 3: uninitialized variables
Uninitialized variables are usually undesirable. Should compilers make them illegal? Can compilers find all of them? 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 3/20/06

10 Another example: side effects of function calls
Consider the statement: x = f(&a,b) + a + g(&a,c); Can be a dangerous statement OK if you always run the code using the same compiler And the compiler never gets updated Careful programmers avoid this type of coding. Why? Fix? 3/20/06

11 Another example Consider the program
#include <iostream> using namespace std; int main() { int a[5], i; a[0] = 2; a[1] = 4; a[2] = 8; a[3] = 32; a[4] = 64; i = 2; i = a[i++]; cout << i << endl; return 0; } What’s the difference in “++i” and “i++”? What’s the value of i? Run on three compilers (Microsoft, gnu, Sun) Got three different answers 3/20/06

12 Weiser: Program Slices
slice criterion: <13, {z}> 1, 2, 3, 6, 8, 9, 11, 13 slice criterion: <10, {x}> 1, 2, 3, 13 slice criterion: <13, {total}> 1, 2, 3, 4, 6, 8, 10, 11, 13 1 void main() 2 { int x, y, total, sum, z; 3 cin >> x >> y; 4 total = 0.0; 5 sum = 0.0; 6 if ( x < 1 ) 7 sum = y; 8 else { 9 cin >> z; 10 total = x*y; 11 } 12 cout << total << sum << endl; 13 } 3/20/06


Download ppt "C. M. Overstreet Old Dominion University Fall 2007"

Similar presentations


Ads by Google