cs 31003 compilers n.
Skip this Video
Loading SlideShow in 5 Seconds..
CS 31003: Compilers PowerPoint Presentation
Download Presentation
CS 31003: Compilers

Loading in 2 Seconds...

play fullscreen
1 / 16

CS 31003: Compilers - PowerPoint PPT Presentation

  • Uploaded on

CS 31003: Compilers. Introduction to Phases of Compiler. Review: Phases of Compiler. Next few lectures will focus on phases of compiler In particular, Intermediate Code Generation Code Optimization Code Generation. Intermediate Code Generation.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

CS 31003: Compilers

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs 31003 compilers

CS 31003: Compilers

Introduction to Phases of Compiler

review phases of compiler
Review: Phases of Compiler
  • Next few lectures will focus on phases of compiler
  • In particular,
    • Intermediate Code Generation
    • Code Optimization
    • Code Generation
intermediate code generation
Intermediate Code Generation
  • In the process of translating a source program into target code , a compiler may construct one or more intermediate representations, which can have a variety of forms.
  • Syntax trees are a form of intermediate representation; they are commonly used during syntax and semantic analysis
intermediate code generation1
Intermediate Code Generation
  • After syntax and semantic analysis of the source program, many compilers generate an explicit low level or machine like intermediate representation, which we can think of as a program for an abstract machine.
  • This has two important properties:
  • Should be easy to produce
  • Should be easy to translate into target machine code
three address code
Three Address Code
  • An intermediate form called three-address code,which consists of a sequence of assembly like instructions with three operands per instruction.
  • Each operand can act like register.





three address code1
Three Address Code
  • Some points about three address instructions:
    • Three address assignment has at most one operator on the right side.Thus fix the order in which operations are to be done
    • The compiler must genarate a temporary name to hold the value computed by a three address instruction.
    • Some three address instruction have fewer than three operands.
code optimization
Code Optimization
  • The machine-independent code-optimization phase attempts to improve the intermediate code so that better target code will result.
  • Usually better means faster,but other objectives may be desired,such as shorter code,or target code that consumes less power.
  • A simple intermediate code generation algorithm followed by code optimization is a reasonable way to generate good target code.
code optimization1
Code Optimization
  • The optimizer can deduce that the conversion of 60 from integer to floating point can be done once and for all at compile time , by replacing the integer 60 by the floating point number 60.0.
  • t3 is used only once to transmit its value to id1 so the optimizer can transform into shorter sequence

t1=id3* 60.0


code optimization2
Code Optimization
  • There is a great variation in the amount of code optimization different compilers perform.
  • In those that do the most, the so called “optimizing compilers”, take significant time in this phase.

Why to use optimization:

  • There are simple optimizations that significantly improve the running time of target program without slowing down compilation too much
code generation
Code Generation
  • The code generator takes as input an intermediate representation of source program and maps it into target language
  • If the target language is machine code registers or memory locations are selected for each of the variables used by the program. Then the intermediate instructions are translated into sequence of machine instructions that perform the same task.
code generation1
Code Generation
  • A crucial aspect of code generation is the judicious assignment of registers to variables
  • Using registers R1 and R2, the previous intermediate code gets translated into machine code:

LDF R2,id3

MULF R2,R2,#60.0

LDF R1,id2


STF id1,R1

code generation2
Code Generation
  • The first operand of each instruction specifies a destination. The F in each instruction tells us that it deals with floating –point numbers. The code in line 1 loads the contents of address id3 into register R2,then it multiplies it with floating point constant 60.0
  • The third instruction moves id2 into register R1.Finally the value in register R1 is stored the address of id1,so the code correctly implements the assignment statement.
lexical analysis
Lexical Analysis
  • A lexical analyzer reads characters from the input and groups them into “token objects”.
  • A sequence of input characters that comprises a single token is called lexeme. Thus we can say that the lexical analyzer insulates a parser from the lexeme representation of tokens
lexical analysis1
Lexical Analysis
  • Tasks of lexical analyzer:
    • Task 1:
      • Reads string of characters as input
      • Identify lexemes
      • Send the tokens to Syntax analyzer to convert into syntax trees.
    • Task 2:
      • Remove white spaces.
    • Task 3:
      • Swap x with successor
      • Remembers all the line numbers to report the line number in case of error
lexical analysis2
Lexical Analysis

get Token

Lexical Analyzer




Symbol Table

the end
The End
  • Up next:

Detailed Discussion on Lexical Analyzer.