Lecture 2. Program Representation - Iowa State...

19
Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture 2. Program Representation 2014.9 1 / 18

Transcript of Lecture 2. Program Representation - Iowa State...

Page 1: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Lecture 2. Program Representation

Wei Le

2014.9

Wei Le Lecture 2. Program Representation 2014.9 1 / 18

Page 2: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Frequently Used Program Representations in ProgramAnalysis

Abstract Syntax Tree (AST)

Control Flow Graph (CFG), Inter-Procedural Control Flow Graph

Call Graph

Dependency Graphs

Research Related to Program Representation

Wei Le Lecture 2. Program Representation 2014.9 2 / 18

Page 3: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Building Abstract Syntax Trees

Lexical analysis (scanner, lexer, lexical analyzer): from a string to tokens

Syntactical analysis or parsing (parser): from tokens to trees, determine whether aprogram is part of the language

Wei Le Lecture 2. Program Representation 2014.9 3 / 18

Page 4: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Abstract Syntax Tree (AST)

Abstract syntax trees or syntax trees, represents the hierarchical syntacticstructure of the source

AST does not contain all information from the source code. e.g., spacing,comments, brackets, parentheses

Any ambiguity has been resolved: E.g., a + b + c produces the same AST as(a + b) + c

Wei Le Lecture 2. Program Representation 2014.9 4 / 18

Page 5: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

AST and Program Analysis

Profile program structure information: how many loops are there in the code?

Help instrumentation: e.g., insert printf after a call

Design a specification language: Flex and Bison: two important open sourcetools to generate scanner and parser

Demo: srcxml

(Example from Maprle)

Wei Le Lecture 2. Program Representation 2014.9 5 / 18

Page 6: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Call Graph

Wei Le Lecture 2. Program Representation 2014.9 6 / 18

Page 7: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Call Graph Example

Wei Le Lecture 2. Program Representation 2014.9 7 / 18

Page 8: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Control Flow Graphs

A directed graph, where

each node represents a statement

each edge represents a control flow transfer

Variations of Control Flow Graphs

We usually don’t include declarations (e.g., int x;)

May want a unique entry and exit node: All nodes without a predecessorshould be pointed to by entry; All nodes without a successor should point toexit

May group statements into basic blocks: A sequence of instructions with nobranches into or out of the block

Wei Le Lecture 2. Program Representation 2014.9 8 / 18

Page 9: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Control Flow Graphs: Examples

Wei Le Lecture 2. Program Representation 2014.9 9 / 18

Page 10: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Inter-procedural Control Flow Graphs

A control flow graph (CFG ) of a procedure is a graph G = (N,E ), where thenodes in N represent statements of the procedure and the edges in E representthe transfer of the control between two statements. Two distinguished nodesentry ∈ N and exit ∈ N represent the unique entry and exit of the procedure.

An interprocedural control flow graph (ICFG) of a program is a collection ofcontrol flow graphs {Gi} such that Gi represents a procedure in the program.Suppose call(s) represents the procedure called from a callsite s. Then for eachcallsite n in an ICFG, there exists an edge from n to the entry of the procedurecall(n), and also there exists an edge from the exit of call(n) to n

Wei Le Lecture 2. Program Representation 2014.9 10 / 18

Page 11: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

CFG vs. AST

Both structural

CFG is simpler and specifies explicitly the control flow information

AST is more faithful

Wei Le Lecture 2. Program Representation 2014.9 11 / 18

Page 12: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Program Dependence Graphs (PDG) [1]

Node: statementsEdge: control and data dependence edges

Control dependence between two statement nodes exists if one statementcontrols the execution of the other (e.g. through if- or while-statements).

Data dependence between two statement nodes exists if a definition of avariable at one statement might reach the usage of the same variable atanother statement.

Wei Le Lecture 2. Program Representation 2014.9 12 / 18

Page 13: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Control Dependence

Wei Le Lecture 2. Program Representation 2014.9 13 / 18

Page 14: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Control Dependence Graph

Wei Le Lecture 2. Program Representation 2014.9 14 / 18

Page 15: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

System Dependence Graphs (SPDG)

Wei Le Lecture 2. Program Representation 2014.9 15 / 18

Page 16: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Applications of PDG

Program Integration

Debugging

Automatic Parallelization

Wei Le Lecture 2. Program Representation 2014.9 16 / 18

Page 17: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Research Related Topics: MVICFG-2014

Wei Le Lecture 2. Program Representation 2014.9 17 / 18

Page 18: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

MVICFG

Wei Le Lecture 2. Program Representation 2014.9 18 / 18

Page 19: Lecture 2. Program Representation - Iowa State Universityweb.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf · Lecture 2. Program Representation Wei Le 2014.9 Wei Le Lecture

Program Representation

Karl J. Ottenstein and Linda M. Ottenstein.The program dependence graph in a software development environment.SIGPLAN Not., 19(5):177–184, April 1984.

Wei Le Lecture 2. Program Representation 2014.9 18 / 18