Code Generation Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html Chapter 4.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Intermediate Code Generation
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Computer Architecture Lecture 7 Compiler Considerations and Optimizations.
1 Languages and Compilers (SProg og Oversættere) Code Generation.
Some Properties of SSA Mooly Sagiv. Outline Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Introduction to Advanced Topics Chapter 1 Mooly Sagiv Schrierber
Compiler construction in4020 – lecture 8 Koen Langendoen Delft University of Technology The Netherlands.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Code Generation Simple Register Allocation Mooly Sagiv html:// Chapter
Previous finals up on the web page use them as practice problems look at them early.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Run time vs. Compile time
Code Generation Mooly Sagiv html:// Chapter 4.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Schedule Midterm out tomorrow, due by next Monday Final during finals week Project updates next week.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Code Generation Mooly Sagiv html:// Chapter 4.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction CPSC 388 Ellen Walker Hiram College.
A System to Generate Test Data and Symbolically Execute Programs Lori A. Clarke Presented by: Xia Cheng.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
C-- by Aksel Gresvig. Outline ● What is C--? How does it work? ● What C-- is not ● Relationship to C ● What does it look like? Examples ● Elements of.
Code Optimization Overview and Examples
A Simple Syntax-Directed Translator
Introduction to Advanced Topics Chapter 1 Text Book: Advanced compiler Design implementation By Steven S Muchnick (Elsevier)
Compiler Construction (CS-636)
Overview of Compilation The Compiler BACK End
Optimization Code Optimization ©SoftMoore Consulting.
Stacks Chapter 4.
For Example: User level quicksort program Three address code.
Instructions - Type and Format
Code Generation.
Overview of Compilation The Compiler BACK End
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Course Overview PART I: overview material PART II: inside a compiler
Presentation transcript:

Code Generation Mooly Sagiv html:// Chapter 4

Basic Compiler Phases

Outline Interpreters Code Generation (next week)

Types of Interpreters Recursive –Recursively traverse the tree –Uniform data representation –Conceptually clean –Excellent error detection –1000x slower than compiler Iterative –Closer to CPU –One flat loop –Explicit stack –Good error detection –30x slower than compiler –Can invoke compiler on code fragments

Input language (Overview) Fully parameterized expressions Arguments can be a single digit expression  digit | ‘(‘ expression operator expression ‘)’ operator  ‘+’ | ‘*’ digit  ‘0’ | ‘1’ | ‘2’ | ‘3’ | ‘4’ | ‘5’ | ‘6’ | ‘7’ | ‘8’ | ‘9’

#include "parser.h" #include " backend.h " static int Interpret_expression(Expression *expr) { switch (expr->type) { case 'D': return expr->value; break; case 'P': { int e_left = Interpret_expression(expr->left); int e_right = Interpret_expression(expr->right); switch (expr->oper) { case '+': return e_left + e_right; case '*': return e_left * e_right; }} break; } void Process(AST_node *icode) { printf("%d\n", Interpret_expression(icode)); }

Uniform self-identifying data representation The types of the sizes of program data values are not known when the interpreter is written Uniform representation of data types –Type –Size The value is a pointer

Example: Complex Number

Status Indicator Direct control flow of the interpreter Possible values –Normal mode –Errors –Jumps –Exceptions –Return

Example: Interpreting C Return PROCEDURE Elaborate return with expression statement (RWE node): SET Result To Evaluate expression (RWE node. expression); IF Status. mode /= Normal mode: Return mode; SET Status. mode To Return mode; SET Status. value TO Result;

Interpreting If-Statement

Symbol table Stores content of variables, named constants, … For every variable V of type T –A pointer to the name of V –The file name and the line it is declared –Kind of declaration –A pointer to T –A pointer to newly allocated space –Initialization bit –Language dependent information (e.g. scope)

Summary Recursive Interpreters Can be implemented quickly –Debug the programming language Not good for heavy-duty interpreter –Slow –Can employ general techniques to speed the recursive interpreter Memoization Tail call elimination Partial evaluation

Threaded AST Annotated AST Every node is connected to the immediate successor in the execution Control flow graph –Nodes Basic execution units –expressions –assignments –Edges Transfer of control –sequential –while –…

C Example while ((x > 0) && (x < 10)) { x = x + y ; y = y – 1 ; } while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y T exit F

Threading the AST(3.2.1) One preorder AST pass Every type of AST has its threading routine Maintains Last node pointer –Global variable Set successor of Last pointer when node is visited

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main T

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main T

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main T

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main T

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main T

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main T

while and seq ass id + x x y > x const 0 < id x const 10 + id y const 1 id y Last node pointer main First node pointer T

Demo Compiler

Conditional Statement if cond then_partelse_part Last node pointer

Conditional Statement if cond then_partelse_part Last node pointer End_If T F

Iterative Interpretation Closed to CPU One flat loop with one big case statement Use explicit stack –Intermediate results –Local variables Requires fully annotated threaded AST –Active-node-pointer (interpreted node)

Demo Compiler

Conditional Statements

Iterative Interpreters (Summary) Different AST representations Faster than recursive interpreters –Some interpretative overhead is eliminated Portable Secure Similarities with the compiler

Code Generation Transform the AST into machine code Machine instructions can be described by tree patterns Replace tree-nodes by machine instruction –Tree rewriting –Replace subtrees Applicable beyond compilers

a := (b[4*c+d]*2)+9

leal movsbl

9 Ra + * 2 mem *Rd Rc4

9 Ra + * 2 Rt Load_Byte (b+Rd)[Rc], 4, Rt

Ra Load_Byte (b+Rd)[Rc], 4, Rt Load_address 9[Rt], 2, Ra

Code generation issues Code selection Register allocation Instruction ordering

Simplifications Consider small parts of AST at time Simplify target machine Use simplifying conventions

Overall Structure