Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture Slides for Compiler Structure and Abstract Syntax Tree | CMSC 631, Study notes of Computer Science

Material Type: Notes; Class: PROG ANLYS&UNDERSTANDING; Subject: Computer Science; University: University of Maryland; Term: Fall 2004;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-i24
koofers-user-i24 🇺🇸

10 documents

1 / 6

Toggle sidebar

Related documents


Partial preview of the text

Download Lecture Slides for Compiler Structure and Abstract Syntax Tree | CMSC 631 and more Study notes Computer Science in PDF only on Docsity! Data Flow Analysis CMSC 631 — Program Analysis and Understanding Fall 2004 2CMSC 631, Fall 2004 • Source code parsed to produce AST • AST transformed to CFG • Data flow analysis operates on control flow graph (and other intermediate representations) Compiler Structure Source Code Abstract Syntax Tree Control Flow Graph Object Code 3CMSC 631, Fall 2004 Abstract Syntax Tree (AST) • Programs are written in text ■ I.e., sequences of characters ■ Awkward to work with • First step: Convert to structured representation ■ Use lexer (like flex) to recognize tokens - Sequences of characters that make words in the language ■ Use parser (like bison) to group words structurally - And, often, to produce AST 4CMSC 631, Fall 2004 Abstract Syntax Tree Example x := a + b; y := a * b; while (y > a) { a := a + 1; x := a + b } Program := x + a b while > y a Block := a + a 1 ... ... 5CMSC 631, Fall 2004 ASTs • ASTs are abstract ■ They don’t contain all information in the program - E.g., spacing, comments, brackets, parentheses ■ Any ambiguity has been resolved - E.g., a + b + c produces the same AST as (a + b) + c • For more info, see CMSC 430 ■ In this class, we will generally begin at the AST level 6CMSC 631, Fall 2004 Disadvantages of ASTs • AST has many similar forms ■ E.g., for, while, repeat...until ■ E.g., if, ?:, switch • • Expressions in AST may be complex, nested ■ (42 * y) + (z > 5 ? 12 * z : z + 20) • Want simpler representation for analysis ■ ...at least, for dataflow analysis 7CMSC 631, Fall 2004 Control-Flow Graph (CFG) • A directed graph where ■ Each node represents a statement ■ Edges represent control flow • Statements may be ■ Assignments x := y op z or x := op z ■ Copy statements x := y ■ Branches goto L or if x relop y goto L ■ etc. 8CMSC 631, Fall 2004 x := a + b; y := a * b; while (y > a) { a := a + 1; x := a + b } Control-Flow Graph Example x := a + b y := a * b y > a a := a + 1 x := a + b 9CMSC 631, Fall 2004 Variations on CFGs • We usually don’t include declarations (e.g., int x;) ■ But there’s usually something in the implementation • May want a unique entry and exit node ■ Won’t matter for the examples we give • May group statements into basic blocks ■ A sequence of instructions with no branches into or out of the block 10CMSC 631, Fall 2004 Control-Flow Graph w/Basic Blocks • Can lead to more efficient implementations • But more complicated to explain, so... ■ We’ll use single-statement blocks in lecture today x := a + b; y := a * b; while (y > a + b) { a := a + 1; x := a + b } x := a + b y := a * b y > a a := a + 1 x := a + b 11CMSC 631, Fall 2004 CFG vs. AST • CFGs are much simpler than ASTs ■ Fewer forms, less redundancy, only simple expressions ■ • But...AST is a more faithful representation ■ CFGs introduce temporaries ■ Lose block structure of program ■ • So for AST, ■ Easier to report error + other messages ■ Easier to explain to programmer ■ Easier to unparse to produce readable code 12CMSC 631, Fall 2004 • A framework for proving facts about programs • Reasons about lots of little facts • Little or no interaction between facts ■ Works best on properties about how program computes • Based on all paths through program ■ Including infeasible paths Data Flow Analysis 25CMSC 631, Fall 2004 • Most data flow analyses can be classified this way ■ A few don’t fit: bidirectional analysis • Lots of literature on data flow analysis Space of Data Flow Analyses May Must Forward Reaching definitions Available expressions Backward Live variables Very busy expressions 26CMSC 631, Fall 2004 • Typically, data flow facts form a lattice ■ Example: Available expressions Data Flow Facts and Lattices ! ⊥ a+b, a*b, a+1 a+b, a*b a+b, a+1 a+b a*b, a+1 a*b a+1 (none) “top” “bottom” 27CMSC 631, Fall 2004 • A partial order is a pair such that ■ ■ ■ ■ Partial Orders (P,≤) ≤ ⊆ P × P ≤ is reflexive: x ≤ x ≤ is anti-symmetric: x ≤ y and y ≤ x⇒ x = y ≤ is transitive: x ≤ y and y ≤ z ⇒ x ≤ z 28CMSC 631, Fall 2004 • A partial order is a lattice if and are defined on any set: ■ is the meet or greatest lower bound operation: - - ■ is the join or least upper bound operation: - - Lattices x ! y ≤ x and x ! y ≤ y if z ≤ x and z ≤ y, then z ≤ x " y if x ≤ z and y ≤ z, then x " y ≤ z x ≤ x " y and y ≤ x " y ! ! ! ! 29CMSC 631, Fall 2004 • A finite partial order is a lattice if meet and join exist for every pair of elements • A lattice has unique elements and such that ■ ■ • In a lattice, Lattices (cont’d) x ! ⊥ = ⊥ x ! " = x x ! ⊥ = x ⊥ ! x ! " = " x ≤ y iff x " y = x x ≤ y iff x # y = y 30CMSC 631, Fall 2004 Useful Lattices • (2S, ⊆) forms a lattice for any set S ■ 2S is the powerset of S (set of all subsets) • If (S, ≤) is a lattice, so is (S, ≥) ■ I.e., lattices can be flipped • The lattice for constant propagation 1 2 3 ! ⊥ ... 31CMSC 631, Fall 2004 • Out(s) = Gen(s) for all statements s ■ Or, if you want, Out(s) = Top • W := { all statements } (worklist) • repeat ■ Take s from W ■ In(s) := ∩s′ ∊ pred(s) Out(s′) ■ temp := Gen(s) ∪ (In(s) - Kill(s)) ■ if (temp != Out(s)) { - Out(s) := temp - W := W ∪ succ(s) ■ } • until W = ∅ Forward Must Data Flow Algorithm 32CMSC 631, Fall 2004 • A function f on a partial order is monotonic if • Easy to check that operations to compute In and Out are monotonic ■ In(s) := ∩s′ ∊ pred(s) Out(s′) ■ temp := Gen(s) ∪ (In(s) - Kill(s)) • Putting these two together, ■ temp := Monotonicity x ≤ y ⇒ f(x) ≤ f(y) fs(!s′∈pred(s)Out(s′)) 33CMSC 631, Fall 2004 • We know the algorithm terminates because ■ The lattice has finite height ■ The operations to compute In and Out are monotonic ■ On every iteration, we remove a statement from the worklist and/or move down the lattice Termination
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved