Docsity

Basic Program Analysis, Slides of Compiler Design

Partial preview of the text

Download Basic Program Analysis and more Slides Compiler Design in PDF only on Docsity!

Basic Program Analysis

Suman Jana

*some slides are borrowed from Baishakhi Ray and Ras Bodik

Our Goal Program Analyzer Source code Security bugs Program analyzer must be able to understand program proper?es (e.g., can a variable be NULL at a par?cular program point? ) Must perform control and data flow analysis

Compiler Overview

  • Abstract Syntax Tree : Source code parsed to produce AST
  • Control Flow Graph: AST is transformed to CFG
  • Data Flow Analysis: operates on CFG

The Structure of a Compiler 5

scanner

parser

checker

code gen

Source code (stream of characters)

stream of tokens

Abstract Syntax Tree (AST)

AST with annota?ons (types, declara?ons)

Machine/byte code

Example

  • Source Code 4*(2+3)
  • Parser input

NUM(4) TIMES LPAR NUM(2) PLUS NUM(3) RPAR

  • Parser output (AST): Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5 7

NUM(4)

NUM(2) NUM(3)

Parse tree for the example: 4*(2+3) Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5 8

leaves are tokens

NUM(4) TIMES LPAR NUM(2) PLUS NUM(3) RPAR

EXPR

EXPR

EXPR

Parse tree for example: if (x==y) {a=1;} Adopted From UC Berkeley: Prof. Bodik CS 164 Lecture 5 10

IF LPAR ID == ID RPAR LBR ID = INT SEMI RBR

EXPR EXPR

STMT

BLOCK

STMT

leaves are tokens

Parse Tree

  • Representa?on of grammars in a tree-like form.
  • Is a one-to-one mapping from the grammar to a tree-form.

A parse tree pictorially shows how the start

symbol of a grammar derives a string in the

language. … Dragon Book

Abstract Syntax Tree (AST)

  • Simplified syntac?c representa?ons of the source code, and they're most open expressed by the data structures of the language used for implementa?on
  • Without showing the whole syntac?c cluNer, represents the parsed string in a structured way, discarding all informa?on that may be important for parsing the string, but isn't needed for analyzing it.

ASTs differ from parse trees because superficial

dis?nc?ons of form, unimportant for transla?on,

do not appear in syntax trees.. … Dragon Book

C Statement: return a + 2 AST

Control Flow Graph & Analysis

RepresenBng Control Flow Adopted From U Penn CIS 570: Modern Programming Language Implementa?on (Autumn 2006) High-level representation

  • Control flow is implicit in an AST Low-level representation:
  • Use a Control-flow graph (CFG)
  • Nodes represent statements (low-level linear IR)
  • Edges represent explicit flow of control

Basic Blocks

  • A basic block is a sequence of straight line code that can be entered only at the beginning and exited only at the end Adopted From U Penn CIS 570: Modern Programming Language Implementa?on (Autumn 2006) g := f h := t – g If e > 0?

Building basic blocks

  • Identify leaders
    • The first instruction in a procedure, or
    • The target of any branch, or
    • An instruction immediately following a

branch (implicit target)

  • Gobble all subsequent instructions until the

next leader

Basic Block Example 1 2 a := 0 b := a * b 3 L1: c := b/d 4 5 6 if c < x goto L e := b / c f := e + 1 7 L2: g := f 8 9 h := t - g if e > 0 goto L 10 goto L 11 L3: return Leaders?

  • {1, 3, 5, 7, 10, 11} Blocks?
  • {1, 2}
  • {3, 4}
  • {5, 6}
  • {7, 8, 9}
  • {10}
  • {11} Adopted From U Penn CIS 570: Modern Programming Language Implementa?on (Autumn 2006)