1 / 12

CS 110: Code Analysis

CS 110: Code Analysis. C. M. Overstreet Old Dominion University Fall 2007. Overview. Why code analysis? Examples: some easy some hard some impossible Will give a few examples of where used. Example 1: elimination of infinite loops. Infinite loops are usually undesirable.

dunn
Download Presentation

CS 110: Code Analysis

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

  4. Examples 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; } • Is this an infinite loop? • How do you know?

  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?

  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

  7. Possible Common Compiler Optimizations • Flag unused variables • Very common • Make loops more efficient by reordering code • Very common • 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

  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?

  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

  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?

  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

  12. 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 } 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 Weiser: Program Slices

More Related