From AST to Code Generation Professor Yihjia Tsai Tamkang University.

Slides:



Advertisements
Similar presentations
Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.
Advertisements

P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Register allocation Morgensen, Torben. "Register Allocation." Basics of Compiler Design. pp from (
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
1 Code generation Our book's target machine (appendix A): opcode source1, source2, destination add r1, r2, r3 addI r1, c, r2 loadI c, r2 load r1, r2 loadAI.
Register Allocation Mooly Sagiv Schrierber Wed 10:00-12:00 html://
COMPILERS Register Allocation hussein suleman uct csc305w 2004.
1 CS 201 Compiler Construction Machine Code Generation.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Program Representations. Representing programs Goals.
Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter Before next class:
Compiler construction in4020 – lecture 8 Koen Langendoen Delft University of Technology The Netherlands.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Cpeg421-08S/final-review1 Course Review Tom St. John.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
CS 536 Spring Code generation I Lecture 20.
Code Generation Professor Yihjia Tsai Tamkang University.
Code Generation Simple Register Allocation Mooly Sagiv html:// Chapter
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Annotating Abstract Syntax Tree Professor Yihjia Tsai Tamkang University.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Code Generation Mooly Sagiv html:// Chapter 4.
Intermediate Code. Local Optimizations
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Code Generation Mooly Sagiv html:// Chapter 4.
Precision Going back to constant prop, in what cases would we lose precision?
4. Processing the intermediate code From: Chapter 4, Modern Compiler Design, by Dick Grunt et al.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
7. Code Generation Chih-Hung Wang Compilers References 1. C. N. Fischer, R. K. Cytron and R. J. LeBlanc. Crafting a Compiler. Pearson Education Inc., 2010.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.
Register Allocation CS 471 November 12, CS 471 – Fall 2007 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
More Code Generation and Optimization Pat Morin COMP 3002.
Compilation (Semester A, 2013/14)
Global Register Allocation Based on
The compilation process
Optimization Code Optimization ©SoftMoore Consulting.
Unit IV Code Generation
CS 201 Compiler Construction
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
Code Generation Part II
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
Live Variables – Basic Block
CS 201 Compiler Construction
Presentation transcript:

From AST to Code Generation Professor Yihjia Tsai Tamkang University

Summary of lecture 8 interpretation recursive iterative simple code generation code per AST node stack and register machines weighted register allocation register spilling program text annotated AST front-end assembly back-endinterpreter intermediate code generation

Quiz 4.7 Can the weight of a tree can also be used to reduce the maximum stack height when generating code for a stack machine? If not, why? If yes, how?

Overview code generation for basic blocks [instruction selection: BURS] register allocation: graph coloring instruction ordering: ladder sequences annotated AST assembly code generator assembler object file linker executable library

Code generation for basic blocks improve quality of code emitted by simple code generation consider multiple AST nodes at a time generate code for maximal basic blocks that cannot be extended by including adjacent AST nodes basic block: a part of the control graph that contains no splits (jumps) or combines (labels)

Code generation for basic blocks a basic block consists of expressions and assignments fixed sequence (;) limits code generation an AST is too restrictive { int n; n = a+1; x = (b+c) * n; n = n+1; y = (b+c) * n; }

Example AST +n =: a1+n *x bc +n n1+n *y bc ;;; { int n; n = a+1; x = (b+c) * n; n = n+1; y = (b+c) * n; }

Dependency graph convert AST to a directed acyclic graph (dag) capturing essential data dependencies data flow inside expressions: operands must be evaluated before operator is applied data flow from a value assigned to variable V to the use of V: the usage of V is not affected by other assignments

AST to dependency graph replace arcs by downwards arrows (upwards for destination under assignment) insert data dependencies from use of V to preceding assignment to V insert data dependencies between consecutive assignments to V add roots to the graph (output variables) remove ;-nodes and connecting arrows AST dependency graph

+ b c a + 1 * x ++ 1 * y Example dependency graph { int n; n = a+1; x = (b+c) * n; n = n+1; y = (b+c) * n; }

+ b c a + 1 * x ++ 1 * y Common subexpression elimination common subexpressions occur multiple times and evaluate to the same value { int n; n = a+1; x = (b+c) * n; n = n+1; y = (b+c) * n; } + b c a + 1 * x + 1 * y

Exercise (7 min.) given the code fragment draw the dependency graph before and after common subexpression elimination. x := a*a + 2*a*b + b*b; y := a*a – 2*a*b + b*b;

Answers

dependency graph before CSE * a a * 2 a * b + * b b + x * a a * 2 a * b - * b b + y

Answers dependency graph after CSE * 2 * + + x * a * b * a a * 2 a * b - * b b + y

Answers dependency graph after CSE * 2 * + + x - + y * a * b

From dependency graph to code target: register machine with additional operations on memory reg op:= regAdd_Reg R2, R1 reg op:= memAdd_Mem x, R1 rewrite nodes with machine instruction templates, and linearize the result instruction ordering:ladder sequences register allocation:graph coloring

Linearization of the data dependency graph example: (a+b)*c – d definition of a ladder sequence each root node is a ladder sequence a ladder sequence S ending in operator node N can be extended with the left operand of N if operator N is communitative then S may also extended with the right operand of N Load_Mem a, R1 Add_Mem b, R1 Mul_Mem, c, R1 Sub_Mem d, R1

code generation for a ladder sequence + b c * x a * y + b c * x Linearization of the data dependency graph

RTRT code generation for a ladder sequence instructions from bottom to top, one register + b c * x Store_Mem R 1, x + b c * x RTRT RTRT Mul_Reg R T, R 1 RTRT Add_Mem c, R 1 Load_Mem b, R 1

Linearization of the data dependency graph late evaluation – don’t occupy registers note: code blocks produced in reverse order select ladder sequence S without additional incoming dependencies introduce temporary registers for non-leaf operands, which become additional roots generate code for S, using R1 as the ladder register remove S from the graph

Example code generation Load_Const 1, R1 Add_Reg R3, R1 Mul_Reg, R2, R1 Store_Mem R1, y 1) ladder: y, *, + Load_Reg R2, R1 Mul_Reg R3, R1 Store_Mem R1, x 2) ladder: x, * Load_Mem b, R1 Add_Mem c, R1 Load_Reg R1, R2 3) ladder: R2, + Load_Const 1, R1 Add_Mem c, R1 Load_Reg R1, R3 4) ladder: R3, + R2 R3 + b c a + 1 * x + 1 * y

Exercise (7 min.) generate code for the following dependency graph * 2 * + + x - + y * a * b

Answers

* 2 * + + x - + y * a * b Load_Reg R2, R1 Add_Reg R3, R1 Add_Reg, R4, R1 Store_Mem R1, x 1) ladder: x, +, + Load_Reg R2, R1 Sub_Reg R3, R1 Add_Reg, R4, R1 Store_Mem R1, y 2) ladder: y, +, - R2 R3 R4 Load_Const 2, R1 Mul_Reg Ra, R1 Mul_Reg, Rb, R1 Load_Reg R1, R3 3) ladder: R3, *, * Load_Reg Ra, R1 Mul_Reg Ra, R1 Load_Reg R1, R2 4) ladder: R2, * Load_Reg Rb, R1 Mul_Reg Rb, R1 Load_Reg R1, R4 5) ladder: R4, *

Register allocation by graph coloring procedure-wide register allocation only live variables require register storage two variables(values) interfere when their live ranges overlap dataflow analysis: a variable is live at node N if the value it holds is used on some path further down the control-flow graph; otherwise it is dead

Live analysis a := read(); b := read(); c := read(); d := a + b*c; d < 10 e := c+8; print(c); f := 10; e := f + d; print(f); print(e); f c e e a b d a := read(); b := read(); c := read(); d := a + b*c; if (d < 10 ) then e := c+8; print(c); else f := 10; e := f + d; print(f); fi print(e);

Register interference graph ab e d c f a := read(); b := read(); c := read(); d := a + b*c; d < 10 e := c+8; print(c); f := 10; e := f + d; print(f); print(e); f c e e a b d

Graph coloring NP complete problem heuristic: color easy nodes last find node N with lowest degree remove N from the graph color the simplified graph set color of N to the first color that is not used by any of N’s neighbors ab e d c f

Exercise (7 min.) given that a and b are live on entry and dead on exit, and that x and y are live on exit: (a) construct the register interference graph (b) color the graph; how many register are needed? { int tmp_2ab = 2*a*b; int tmp_aa = a*a; int tmp_bb = b*b; x := tmp_aa + tmp_2ab + tmp_bb; y := tmp_aa - tmp_2ab + tmp_bb; }

Answers

4 registers are needed atmp_2ab tmp_bb xy tmp_aab

Code optimization preprocessing constant folding a[1]  *(a+4*1)  *(a+4) strength reduction 4*i  i<<2 in-lining... postprocessing peephole optimization: replace inefficient patterns Load_Reg R2, R1 Load_Reg R1, R2 Load_Reg R2, R1

Summary dependency graphs code generation for basic blocks instruction selection: BURS register allocation: graph coloring instruction ordering: ladder sequences annotated AST assembly code generator assembler object file linker executable library