Code Generation How to produce intermediate or target code.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Goal: Write Programs in Assembly
Intermediate Code Generation
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
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.
CS 31003: Compilers Introduction to Phases of Compiler.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
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.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
COMPILER CONSTRUCTION
Compiler Principle and Technology Prof. Dongming LU Apr. 29th, 2015.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Cosc 2150: Computer Organization
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
The TINY sample language and it’s compiler
Compiler Chapter# 5 Intermediate code generation.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Introduction to Code Generation and Intermediate Representations
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Programming Languages
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
Intermediate Code Representations
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18.
Code Generation CPSC 388 Ellen Walker Hiram College.
Intermediate Language  Compiler Model Front-End− language dependant part Back-End− machine dependant part [1/34]
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
Language Implementation Overview John Keyser Spring 2016.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Issues in Code Generation CPSC 388 Ellen Walker Hiram College.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
CS 404 Introduction to Compiler Design
COMPILER CONSTRUCTION
A Simple Syntax-Directed Translator
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate code generation Jakub Yaghob
An Attribute Grammar for Tiny
Intermediate Code Generation
Subject Name:COMPILER DESIGN Subject Code:10CS63
Instructions - Type and Format
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.
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation
Intermediate Code Generation Part I
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generating machine-independent intermediate form.
Code generation and data types
Presentation transcript:

Code Generation How to produce intermediate or target code

Code Generation2 Outline  Intermediate code Three-address code P-code  Code generation techniques Using attribute grammar Tree traversal Macro expansion  Address Calculation  Code generation for control statements  Code generation for logical expressions

Code Generation3 Intermediate Code  Intermediate representation for programs  Why intermediate code Reduce amount of work if optimization is done for intermediate code Easy for retargeting compilers  Forms of intermediate code Abstract syntax tree Linearization of abstract syntax tree

Code Generation4 Three-address code: Form of Instructions  x = y op z x, y, and z are addresses of  Variables (perhaps temporaries)  Locations in programs y and z must be differed from x  Operators can be: Arithmetic operators (3-address) Relational operators (3-address) Conditional operators (2-address)  If_false … goto … Jump (1-address) Input/Output Halt  Labels can be assigned to a location

Code Generation5 Example of 3-address Code read (x); if x>0 then {fact:=1; repeat {fact:=fact*x; x:=x-1; } until x==0; write(fact); } read x t1=x>0 if_false t1 goto L1 fact=1 label L2 t2=fact*x fact=t2 t3=x-1 x=t3 t4=x==0 if_false t4 goto L2 write fact label L1 halt

Code Generation6 P-Code  Code for a hypothetical stack machine  For Pascal compilers  No variable name is required  Instructions Load stack Arithmetic and relational operators Jumps  Operations are performed on topmost values on stack  0-address or 1-address instructions

Code Generation7 P-code Instruction  Load: push stack Load value Load address Load constant  Store: save top of stack in memory Destructive store Nondestructive store  Arithmetic operations Add Subtract Multiply  Compare Greater Less equal  Label  Jump Unconditional jump Conditional jump  I/O Read write  Stop

Code Generation8 Example of P-code read (x); if x>0 then {fact:=1; repeat {fact:=fact*x; x:=x-1; } until x==0; write(fact); } loada x read loadv x loadc 0 greater jumpONfalse L1 loada fact loadc 1 store label L2 loada fact loadv fact loadv x mult store loada x loadv x loadc 1 sub store loadv x loadc 0 equ jumpF L2 loadv fact write label L1 stop

Code Generation9 Code Generation Using Synthesizes Attributes  An attribute is created for the sequence of characters representing generated code.  An attribute grammar is written to generate the intermediate/target code.  The value of the attribute is passed from child nodes upto their parent node to construct a larger chunk of code

Code Generation10 Attribute Grammar for P-code Grammar Rules exp 1 -> id = exp 2 exp -> aexp aexp 1 - >aexp 2 + factor aexp -> factor factor -> ( exp ) factor -> num factor -> id Semantic Rules exp 1.code =“loada” || id.strval ++ exp 2.code ++ ”stn” exp.code = aexp.code aexp 1.code = aexp 2.code ++ factor.code ++ “add” aexp.code = factor.code factor.code = exp.code factor.code = “loadc ” || num.strval factor.code = “loadv ” || id.strval

Code Generation11 Generating P-Code:Example exp = id exp aexp factor + () aexp factor + num id num loada x loadv x loadc 3 adi stn loadc 4 adi loadc 4 loada x loadv x loadc 3 adi stn loadv x loadc 3 adi loadv x loadc 3 loadv x loadc 3 adi loada x loadv x loadc 3 adi stn loada x loadv x loadc 3 adi stn loada x loadv x loadc 3 adi stn loadc 4 adi

Code Generation12 Attribute Grammar for 3-address Code Grammar Rules exp 1 -> id = exp 2 exp -> aexp aexp 1 - >aexp 2 +fac tor aexp -> factor factor -> ( exp ) factor -> num factor -> id Semantic Rules exp 1.name = exp 2.name; exp 1.code=exp 2.code++id.strval ||”=“||exp 2.name exp.name = aexp.name; exp.code = aexp.code exp 1.name = newtemp(); aexp 1.code=aexp 2.code++facto r.code++ aexp 1.name||”=“||aexp 2.name||”+“||factor.name aexp.name=factor.name;aexp.c ode=factor.code factor.name =exp.name; factor.code = exp.code factor.name = num.strval; factor.code = “” factor.name = id.strval; factor.code = “”

Code Generation13 Generating 3-address Code:Example exp = id exp aexp factor + () aexp factor + num id num x 4 x t1 t1=x+3 3 t1 t1=x+3 t1 t1=x+3 x=t1 t1 t1=x+3 x=t1 t1 t1=x+3 x=t1 t2 t1=x+3 x=t1 t2=t1+4 t2 t1=x+3 x=t1 t2=t1+4 4

Code Generation14 Practical Code Generation: Tree Traversal procedure genCode ( t : node ) { if ( t is not null) then {generate code to prepare for code of left child; genCode (left child of t ); generate code to prepare for code of right child; genCode (right child of t ); generate code to implement the action of t ; }

Code Generation15 Practical Code Generation for P-code gencode ( T : Tree ) { if ( T is not null) { switch ( T. type ) case plusnode : { gencode ( T. lchild ); gencode ( T. rchild ); emitCode(“add”); } case asgnnode : { emitcode( “loada”, T. strval ); gencode ( T.r child ); emitcode (“stn”); } case constnode : { emitcode (“loadc”, t. strval ); } case idnode : { emitcode (“loadv”, t. strval ); } default : { emitcode (“error”); } }

Code Generation16 Macaro Expansion  From intermediate code to target code  Example: 3-address code: x=y+n P code: loada x loadv y loadc n adi sto

Code Generation17 Address Calculation  Addressing operations  Array references  Record structure references

Code Generation18 Addressing Operations 3-address code address of x  &x indirect address (content pointed by x)  *x P-code address of x  loada x indirect load  ind x  (load *(top+ x)) indexed address  ixa x  (load top*x+(top-1))

Code Generation19 Array References 3-address code  x=a[i] t1=i*elesize(a) t2=&a+t1 x=*t2  a[i]=x t1=i*elesize(a) t2=&a+t1 *t2=x P-code  x=a[i] loada x loada a loadv i ixa elesize(a) ind 0 sto  a[i]=x loada a loadv i ixa elesize(a) loadv x sto Find offset Find address Find offset Find address Find content

Code Generation20 More Complex Array References 3-address code  a[i+1]=a[j+2]+3 t1=j+2 t2=t1*elesize(a) t3=&a+t2 t4=*t3 t5=t4+3 t6=i+1 t7=t6*elesize(a) t8=&a+t7 *t8=t5 P code  a[i+1]=a[j+2]+3 loada a loadv i loadc 1 adi ixa elesize(a) loada a loadv j loadc 2 adi ixa elesize(a) ind 0 loadc 3 adi sto t4=a[t1] a[t6]=t5t1=a[j+2] t2=&a[i+1] a[t6]=t5 t4=a[t1] t2=&a[i+1] t1=a[j+2]

Code Generation21 Structure References typedef struct rec {int i; char c; int j; } Entry; Entry x; 3-address code  Address of a field t1=&x+offset(x,j)  Content of a field t1=&x+offset(x,j) t2=*t1 P code  Address of a field loada x loadc offset(x.j) ixa 1  Content of a field loada x ind offset(x.j) x.i x.j x.c base address of x Offset of x.j Offset of x.c

Code Generation22 Code Generation for Control Statements  If Statements  While Statements  Logical Expressions

Code Generation23 Code Generation for If-Statements IF ( E ) S1 ELSE S2  3-address code <code evaluating E and assigning to t1> if_false t1 goto L1 goto L2 label L1 label L2  P code jumpF L1 jump L2 label L1 label L2

Code Generation24 Code Generation for While-Statements WHILE ( E ) S  3-address code label L1 <code evaluating E and assigning to t1> if_false t1 goto L2 goto L1 label L2  P code label L1 jumpF L2 jump L1 label L2

Code Generation25 Generating Labels  Forward jump Label must be generated before defining the label  For intermediate code Generate label at the jump instruction Store the label until the actual location is found  For target code (assembly) Leave the destination address in the jump instruction When the destination address is found, go back and fill the address (backpatching) Short jump or long jump?  Leave space enough for long jump  If only short jump is required, the extra space is filled with nop. label L1 jumpF L2 jump L1 label L2

Code Generation26 Code Generation for Logical Expressions  Data types and operators Use Boolean data type and operators if included in the target/intermediate language Use integer 0/1 and bitwise opeartors  Short-circuit evaluation IF a AND b THEN S  If a is false, the whole exp is false there is no need to evaluate b IF a OR b THEN S  If a is true, the whole exp is true there is no need to evaluate b  Intermediate code for IF a AND b THEN S if_false a goto L1 if_false b goto L1 Label L1  Intermediate code for IF a OR b THEN S if_false a goto L1 goto L2 Label L1 if_false b goto L3 Label L2 Label L3