Intermediate code generation Jakub Yaghob

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Decision Structures – The Syntax Tree Lecture 22 Fri, Apr 8, 2005.
Intermediate Code Generation
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
1 Languages and Compilers (SProg og Oversættere) Code Generation.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
The Assembly Language Level
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Chapter 9: Subprogram Control
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Python quick start guide
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Pascal Programming Pointers and Dynamic Variables.
Intermediate code generation Simplifying initial assumptions a 3-address code will be used for instructions  choice of instruction mnemonics and layouts.
Code Generation How to produce intermediate or target code.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 3 Translation.
Semantic analysis Jakub Yaghob
Intermediate code Jakub Yaghob
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages and Paradigms
Review: Chapter 5: Syntax directed translation
Compiler Construction
Programming Paradigms
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Intermediate Code Generation
Compiler Optimization and Code Generation
Instructions - Type and Format
Java - Data Types, Variables, and Arrays
Computer Science 210 Computer Organization
Chapter 6 Intermediate-Code Generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
Introduction to Micro Controllers & Embedded System Design
Compiler Design 21. Intermediate Code Generation
Processor Fundamentals
Three Address Code Generation – Backpatching
Languages and Compilers (SProg og Oversættere)
THREE ADDRESS CODE GENERATION
CS 432: Compiler Construction Lecture 11
Flow of Control.
Compiler Construction
Intermediate Code Generation
Run Time Environments 薛智文
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
COMPILERS Semantic Analysis
Runtime Environments What is in the memory?.
Review: For array a[2,5], how would the memory for this array looks like using row major layout? What about column major layout? How to compute the address.
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generating machine-independent intermediate form.
Compiler Construction
Code generation and data types
Presentation transcript:

Intermediate code generation Jakub Yaghob Compiler principles Intermediate code generation Jakub Yaghob

Syntax-directed translation into three-address code Add several attributes to each grammar symbol Placement – a name of an object holding a value of the object Code – a sequence of three-address instructions evaluating the symbol Label – an absolute or relative address to three-address code

Example for our grammar E → ER + T E → T T → TR * F T → F F → ( E ) F → id E.p = newtemp E.c = ER.c | T.c | gen(E.p=ER.p + T.p) E.p = T.p E.c = T.c T.p = newtemp T.c = TR.c | F.c | gen(T.p = TR.p * F.p) T.p = F.p T.c = F.c F.p = E.p F.c = E.c F.p = id.p F.c = ‘’

Example for while (amateur) S→ while E do SR S.L1 = curradr S.L2 = curradr + E.c.size + SR.c.size + 2 S.c = E.c | gen(JZ E.p, S.L2) | SR.c | gen(JMP S.L1) S.L1: E.c JZ E.p, S.L2 SR.c JMP S.L1 S.L2:

Example for while (professional) S→ while E do SR S.L1 = curradr + 1 S.L2 = curradr + SR.c.size + 1 S.c = gen(JMP S.L2) | SR.c | E.c | gen(JNZ E.p, S.L1) JMP S.L2 S.L1: SR.c S.L2: E.c JNZ E.p, S.L1

Declarations Global objects declarations Local objects declarations E.g. global variables in C Local objects declarations E.g. local variables in a function At the start of the function × at the start of a block × in the block body Moving declarations to a “good” place Global declarations in one well-defined place Local declarations at the start of a function Live-variable in the function Determine the size of the object Determine the offset of fields in a structure It does not have to be solved in intermediate code

Assignment Type conversion Structure field access Array unfolding Usually explicit Intermediate code does not know input language semantic Structure field access Use calculated offset from declaration Direct access using a pointer and added constant Array unfolding Multidimensional-array are perceived as one-dimensional (like a memory) – stored in one of the two forms, either row-major (row-by-row) or column-major (column-by-column) One-dimensional array A[lb..ub] of type with width w base + (i-lb) * w i * w + (base – lb * w) Two-dimensional array A[lb1..ub1,lb2..ub2] base + ((i1-lb1) * (ub2-lb2+1) + i2-lb2) * w

Boolean expression Sometimes replaced FALSE=0, TRUE=1 (or anything !=0) Numeric (full) evaluation All parts of the expression are evaluated Pascal Short evaluation if the result is already known during the evaluation, it won’t be evaluated further Jumps in the evaluation C

Switch What we need Finding the case Evaluate the expression Find which value in the list of cases is the same as the value of the expression Execute the statement associated with the value found Finding the case Sequence of conditional branches Small number of cases (<10) Binary search in table [value,caseptr] Binary tree of conditions Table of pointers indexed by value High density of values in the range of case values

Backpatching Single pass translation: how to set a destination address in a jump to a forward label? Forward jump Calling a not yet defined function in a module Can be resolved by a linker Generate branches with the targets of the jumps temporarily left unspecified For each label remember a set of instructions referencing it Determine the address of the label Go through the set of instructions and backpatch the determined address

Procedure calls Evaluate parameters Pass parameters by value or by reference VAR or non-VAR parameters in Pascal Binding real and formal parameters Positional By name Return value