SlideShare a Scribd company logo
1 of 62
CODE GENERATION
Introduction
• The final phase of our compiler model is code
  generator.
• It takes input from the intermediate
  representation with supplementary
  information in symbol table of the source
  program and produces as output an
  equivalent target program.
• Code generator main tasks:
  – Instruction selection
  – Register allocation and assignment
  – Instruction ordering
• Instruction selection
  • choose appropriate target-machine instructions to
    implement the IR statements
• Register allocation and assignment
  • decide what values to keep in which registers
• Instruction ordering
  • decide in what order to schedule the execution of
    instructions
Issues in the design of code generator
•   Input to the code generator
•   Target program
•   Memory management
•   Instruction selection
•   Register allocation
•   Choice of evaluation order
•   Approaches to code generation
Issues in the design of code generator
• Input to the code generator
  – IR + Symbol table
  – IR has several choices
     • Postfix notation
     • Syntax tree
     • Three address code
  – We assume front end produces low-level IR, i.e.
    values of names in it can be directly manipulated
    by the machine instructions.
  – Syntactic and semantic errors have been already
    detected
Issues in the design of code generator
• The target program
  – The output of code generator is target program.
  – Output may take variety of forms
     • Absolute machine language(executable code)
     • Relocatable machine language(object files for linker)
     • Assembly language(facilitates debugging)
  – Absolute machine language has advantage that it can
    be placed in a fixed location in memory and
    immediately executed.
  – Relocatable machine language program allows
    subprograms to be compiled separately.
  – Producing assembly language program as o/p makes
    the process of code generation somewhat easier.
Issues in the design of code generator
• Memory management
  – Mapping names in the source program to
    addresses of data objects in run time memory is
    done by front end & code generator.
  – If a machine code is being generated, labels in
    three address statements have to be converted to
    addresses of instructions. This process is
    analogous to “backpatching” technique.
Issues in the design of code generator
• Instruction selection
  – Uniformity and completeness of the instruction
    set are imp factors
  – If we do not care about the efficiency of the target
    program, instruction selection is straightforward.
  – The quality of the generated code is determined
    by its speed and size.
  – For ex,
                      x=y+z

                    LD     R0, y
                    ADD    R0, R0, z
                    ST     x, R0
a=b+c
 d=a+e

LD    R0, b
ADD   R0, R0, c
ST    a, R0
LD    R0, a
ADD   R0, R0, e
ST    d, R0
Issues in the design of code generator
• Register allocation
  – Instructions involving register operands are
    usually shorter and faster than those involving
    operands in memory.
  – Two subproblems
     • Register allocation: select the set of variables that will
       reside in registers at each point in the program
     • Register assignment: select specific register that a
       variable reside in
  – Complications imposed by the hardware
    architecture
     • Example: register pairs for multiplication and division
• The multiplication instruction is of the form
          M x, y
where x, is the multiplicand, is the even register
of an even/odd register pair.
• The multiplicand value is taken from the odd
  register pair. The multiplier y is a single
  register. The product occupies the entire
  even/odd register pair.
t=a+b
     t=t*c            t=a+b
     T=t/d            t=t+c
                      T=t/d




L        R1, a   L            R0, a
A        R1, b   A            R0, b
M        R0, c   A            R0, c
D        R0, d   SRDA         R0, 32
ST       R1, t   (shift right double arithmetic)
                 D            R0, d
                 ST           R1, t
INC a

MOV a, R0
ADD #1,R0
MOV R0, a
Issues in the design of code generator
• Approaches to code generator
  – Criterion for a code generator is to produce
    correct code.
  – Given the premium on correctness, designing a
    code generator so it can be easily implemented,
    tested, and maintained is an important design
    goal.
Issues in the design of code generator
• Choice of evaluation order
  – The order in which computations are performed
    can affect the efficiency of the target code.
  – When instructions are independent, their evaluation order
    can be changed
                                  MOV   a,R0
                                  ADD   b,R0
                                  MOV   R0,t1
                    t1:=a+b       MOV   c,R1
                    t2:=c+d       ADD   d,R1
  a+b-(c+d)*e                     MOV   e,R0
                    t3:=e*t2
                    t4:=t1-t3     MUL   R1,R0     MOV   c,R0
                                  MOV   t1,R1     ADD   d,R0
                reorder           SUB   R0,R1     MOV   e,R1
                                  MOV   R1,t4     MUL   R0,R1
                    t2:=c+d                       MOV   a,R0
                    t3:=e*t2                      ADD   b,R0
                    t1:=a+b                       SUB   R1,R0
                    t4:=t1-t3                     MOV   R0,t4
Target machine
• Implementing code generation requires thorough
  understanding of the target machine architecture and its
  instruction set

• Our (hypothetical) machine:
   – Byte-addressable (word = 4 bytes)
   – Has n general purpose registers R0, R1, …, Rn-1
   – Two-address instructions of the form
               op source, destination
   – Op – op-code
   – Source, destination – data fields
The Target Machine: Op-codes
•   Op-codes (op), for example
•   MOV (move content of source to destination)
•   ADD (add content of source to destination)
•   SUB (subtract content of source from
                                 destination)

• There are also other ops




                                                  18
The Target Machine: Address modes
• Addressing mode: Different ways in which location of an
  operand can be specified in the instruction.

                                                        Added
        Mode           Form          Address
                                                         Cost
      Absolute          M               M                 1
      Register          R               R                 0
       Indexed         c(R)       c+contents(R)           1

   Indirect register   *R           contents(R)           0

   Indirect indexed *c(R)     contents(c+contents(R))     1

        Literal         #c             N/A                1
Instruction Costs
• Machine is a simple processor with fixed instruction
  costs
• In most of the machines and in most of the
  instructions the time taken to fetch an instruction
  from memory exceeds the time spent executing the
  instruction. So reducing the length of the instruction
  has an extra benefit.




                                                           20
Examples

Instruction         Operation                                        Cost
MOV R0,R1           Store content(R0) into register R1                   1
MOV R0,M            Store content(R0) into memory location M             2
MOV M,R0            Store content(M) into register R0                    2
MOV 4(R0),M         Store contents(4+contents(R0)) into M                3
MOV *4(R0),M        Store contents(contents(4+contents(R0))) into M      3
MOV #1,R0           Store 1 into R0                                      2
ADD 4(R0),*12(R1)   Add contents(4+contents(R0))
                      to value at location contents(12+contents(R1))     3




                                                                        21
Instruction Selection
• Instruction selection is important to obtain efficient
  code
• Suppose we translate three-address code
        x:= y + z
  to:   MOV y,R0
        ADD z,R0
        MOV R0,x               a:=a+1      MOV a,R0
                                           ADD #1,R0
                                           MOV R0,a
                                           Cost = 6
                      Better   Best


                    ADD #1,a   INC a
                    Cost = 3   Cost = 2                    22
Instruction Selection: Utilizing
            Addressing Modes
• Suppose we translate a:=b+c into
      MOV b,R0
      ADD c,R0
      MOV R0,a
• Assuming addresses of a, b, and c are stored in R0,
  R1, and R2
     MOV *R1,*R0
     ADD *R2,*R0
• Assuming R1 and R2 contain values of b and c
     ADD R2,R1
     MOV R1,a

                                                        23
Run Time Storage Management
• The information needed during an execution of a
  procedure is kept in a block of storage called
  activation record.
• Whenever a procedure is called a stack frame is
  allocated on the stack
• The information stored in the stack are return
  address, local variables, temporaries, return
  values etc.
• 2 standard storage allocation strategies namely
  – Static allocation
  – Stack allocation
• In static allocation the position of an
  activation record in memory is fixed at
  compile time.
• In stack allocation a new activation record is
  pushed onto the stack for each execution of
  the procedure. The record is popped when the
  activation ends.
• An activation record for a procedure has
  fields to hold parameters, results, machine-
  status information, local data, temporaries
  and the like. Since run-time allocation and de-
  allocation of activation records occurs as part
  of the procedure call and return sequences,
  we focus on the following three-address
  statements:
1. call
2. return
3. halt
4. action, a placeholder for other statements
Static allocation
• A call statement, needs a move instruction to save
  the return address and GOTO transfers control to the
  target code for the called procedure.


   – MOV instruction saves return address.
   – GOTO transfers control to the target code for the called
     procedure.
   – Callee.static_area, calle.code_area are constants referring
     to the address of activation record and first instruction for
     the called procedure.
   – #here+20 in the mov instruction is return address.
   – Cost of 2 instructions is 5
Three address code
Activation record         Activation record
      for c(64 bytes)           for p(88 bytes)

0:     Return address     0:    Return address

8:                        4:
              arr                         buf


56:
               I          84:              n

60:            J
Target code
Stack allocation
• Static allocation can become stack allocation by using
  relative addresses for storage in activation records. The
  position of the record for an activation of a procedure is not
  known until run time.
• In stack allocation, this position is usually stored in a
  register, so words in the activation record can be accessed
  as offsets from the value in this register
• When a procedure call occurs, the calling procedure
  increments SP and transfers control to the called
  procedure.
• After control returns to the caller, it decrements SP, thereby
  de-allocating the activation record of the called procedure.
• The code for the 1st procedure initializes the stack by
  setting SP to the start of the stack area in memory.
      MOV #stackstart, SP         /*initialize the stack*/
      code for the first procedure
      HALT                    /*terminate execution*/
• A procedure call sequence increments SP, saves the
  return address, and transfers control to the called
  procedure:
      ADD #caller.recordsize, SP
      MOV #here+16, SP                   /* save return address*/
      GOTO callee.code_area
• The attribute caller.recordsize represents the size of an
  activation record, so the ADD instruction leaves SP
  pointing to the beginning of the next activation record.
• The source #here+16 in the MOV instruction is the address
  of the instruction following the GOTO; it is saved in the
  address pointed to by SP.

• The return sequence consists of two parts. The called
  procedure transfers control to the return address using
       GOTO *0(SP)       /*return to caller*/


   – The reason for using *0(SP) in the GOTO instruction is that we
     need two levels of indirection: 0(SP) is the address of the first
     word in the activation record and *0(SP) is the return address
     saved there.
   – The second part of the return sequence is in the caller, which
     decrements SP, thereby restoring SP to its previous value. That
     is, after the subtraction SP points to the beginning of the
     activation record of the caller:
       SUB #caller.recordsize, SP
The following program is a condensation of
the three-address code for the Pascal
program for reading and sorting integers.
Procedure q is recursive, so more than one
activation of q can be alive at the same
                   time.
600:   /*stack starts here*/
Register allocation and assignment
• Values in registers are easier and faster to access
  than memory
   – Reserve a few registers for stack pointers, base
     addresses etc
   – Efficiently utilize the rest of general-purpose registers
• Register allocation
   – At each program point, select a set of values to reside
     in registers
• Register assignment
   – Pick a specific register for each value, subject to
     hardware constraints
   – Register classes: not all registers are equal
• One approach to register allocation and
  assignment is to assign specific values in a
  object program to certain registers.
• Advantage of this method is that design of
  compiler will become easy.
• Disadvantage is that, registers will be used
  inefficiently, certain registers will go unused
  and unnecessary loads and stores are
  generated.
Global Register Allocation
• In code generation algo, registers were used
  to hold values of a single basic block.
• At the end of a block, store instructions were
  needed in order to store the live values in
  memory.
• Live variables : are the ones which are going
  to be used in subsequent blocks.
• So, LRA was slightly modified for the registers
  in order to hold the frequently used variables
  and keep these registers consistent across
  block boundaries(globally)
• Programs will spend most of their time in
  inner loops, a natural approach is that to keep
  a frequently used value in a fixed registers
  throughout the loop.
• But number of registers is fixed.
• Programs are written as if there are only two
  kinds of memory: main memory and disk
• Programmer is responsible for moving data
  from disk to memory (e.g., file I/O)
• Hardware is responsible for moving data
  between memory and caches
• Compiler is responsible for moving data
  between memory and registers
Usage counts
• If a variable say x, is in register then we can
  say that we have saved 1 cost.
• If a variable x, is defined somewhere outside
  the loop( a basic block), then for every usage
  of variable x in block we will save 1 cost.
• For every variable x computed in block, if it is
  live on exit from block, then count a saving of
  2, since it is not necessary to store it at the
  end of the block.
Where
        use(x,B) is the number of times x is used in B
prior to any definition of x.
       live(x,B) is 1 if x is live on exit from B and is
assigned a value in B., 0 otherwise.
B1      B2      B3      B4     Total count
A   (0+2)   (1+0)   (1+0)   (0+0)       4

B   (2+0)   (0+0)   (0+2)   (0+2)       6

C   (1+0)   (0+0)   (1+0)   (1+0)       3


D   (1+2)   (1+0)   (1+0)   (1+0)       6


E   (0+2)   (0+0)   (0+2)   (0+0)       4

F   (1+0)   (0+2)   (1+0)   (0+0)       4
Code sequence using global register assignment
Register assignment for outer loops
• If an outer loop L1 contains an inner loop L2,
  the names allocated registers in L2 need not
  be allocated registers in L1-L2.
• However if name x is allocated a register in
  loop L1 but not in L2, we must store x on
  entrance to L2 and load x on exit from L2.
• Similarly, if we choose to allocate x a register
  in L2 but not L1, we must load x on entrance
  to L2 and store x on exit from L2.
Register allocation by graph coloring
• When a reg is needed for a computation but all
  available reg are in use, the contents of one of the used
  reg must be stored(spilled) into memory location.

• Perform dataflow liveness analysis over a CFG:
   – collect variable liveness info at every program point

• Build an interference graph:
   – each node represents a temporary value
   – each edge represents an interference between two
     temporaries—they can’t be assigned to the same register
• Color the interference graph:
  – colors = registers; want to use as few colors as
    possible
  – for a machine with K registers, decide whether the
    interference graph is K-colorable
  – if yes, the coloring gives a register assignment
  – If no, need to spill some temporaries to memory
Peephole Optimization
• Statement by statement code generation
  strategy often produces target code that
  contains redundant instructions and
  suboptimal constructs.
• No guarantee that resulting code is optimal
  under any mathematical measure.
• Peephole optimization: a short sequence of
  target instructions(peephole instructions) that
  can be replaced by shorter or faster sequence
  instructions.
• Repeated passes over the target code are
  necessary to get the maximum benefit.
• Characteristics of peephole optimization
  – Redundant- instruction elimination
  – Flow of control optimizations
  – Algebraic simplifications
  – Use of machine idioms
Redundant Loads and Stores
Unreachable code
• Unlabeled instruction following an unconditional jump
  may be eliminated
Flow of control optimizations
Algebraic simplification
• Eliminate the instructions like the following
  – x = x+0
  – x = x*1

  These type of instructions are often produced by
  straightforward intermediate code generation
  algorithms, and they can be easily eliminated by
  peephole optimization.
Reduction in strength
• Reduction in strength replaces expensive
  operations by equivalent cheaper ones on the
  target machine.
  ex,
      X2 = X * X
Use of machine idioms
• The target machine may have hardware
  instructions to implement certain specific
  operations efficiently.
• Detecting situations that permit the use of
  these instructions can reduce execution time
  significantly.
• Ex,
  – Increment and decrement addressing modes

More Related Content

What's hot

Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code GeneratorDarshan sai Reddy
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit IIManoj Patil
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysisDattatray Gandhmal
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representationSravanthi Emani
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree Divya Ks
 

What's hot (20)

Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
System Programming Unit II
System Programming Unit IISystem Programming Unit II
System Programming Unit II
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Back patching
Back patchingBack patching
Back patching
 

Viewers also liked

Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Tech_MX
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocksJothi Lakshmi
 
Code Optimization
Code OptimizationCode Optimization
Code Optimizationguest9f8315
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow GraphsJenny Galino
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler designAnul Chaudhary
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsingkunj desai
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFAkunj desai
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical AnalysisMunni28
 
Programming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwaresProgramming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwaresNisarg Amin
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / TranslatorsProject Student
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automatadeepinderbedi
 

Viewers also liked (20)

Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow Graphs
 
Code generator
Code generatorCode generator
Code generator
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
Dfa vs nfa
Dfa vs nfaDfa vs nfa
Dfa vs nfa
 
optimization of DFA
optimization of DFAoptimization of DFA
optimization of DFA
 
DFA Minimization
DFA MinimizationDFA Minimization
DFA Minimization
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Programming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwaresProgramming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwares
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / Translators
 
Nfa vs dfa
Nfa vs dfaNfa vs dfa
Nfa vs dfa
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 

Similar to Code generation

MICROCONTROLLERS-module2 (7).pptx
MICROCONTROLLERS-module2 (7).pptxMICROCONTROLLERS-module2 (7).pptx
MICROCONTROLLERS-module2 (7).pptxAmoghR3
 
Computer organization and architecture
Computer organization and architectureComputer organization and architecture
Computer organization and architectureSubesh Kumar Yadav
 
ARM Architecture Instruction Set
ARM Architecture Instruction SetARM Architecture Instruction Set
ARM Architecture Instruction SetDwight Sabio
 
Module 2 PPT of ES.pptx
Module 2 PPT of ES.pptxModule 2 PPT of ES.pptx
Module 2 PPT of ES.pptxshruthi810379
 
Lec3 instructions branch carl hamcher
Lec3 instructions branch carl hamcher Lec3 instructions branch carl hamcher
Lec3 instructions branch carl hamcher Venkata Krishnakanth P
 
Program execution, straight line sequence and branching
Program execution, straight line sequence and branchingProgram execution, straight line sequence and branching
Program execution, straight line sequence and branchingJyotiprakashMishra18
 
8051 microcontroller notes continuous
8051 microcontroller notes continuous 8051 microcontroller notes continuous
8051 microcontroller notes continuous THANDAIAH PRABU
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.pptsteffydean
 
Winter training,Readymade Projects,Buy Projects,Corporate Training
Winter training,Readymade Projects,Buy Projects,Corporate TrainingWinter training,Readymade Projects,Buy Projects,Corporate Training
Winter training,Readymade Projects,Buy Projects,Corporate TrainingTechnogroovy
 
(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)Alveena Saleem
 
Arm instruction set
Arm instruction setArm instruction set
Arm instruction setPriyangaKR1
 

Similar to Code generation (20)

Ch9a
Ch9aCh9a
Ch9a
 
Bp150522
Bp150522Bp150522
Bp150522
 
MICROCONTROLLERS-module2 (7).pptx
MICROCONTROLLERS-module2 (7).pptxMICROCONTROLLERS-module2 (7).pptx
MICROCONTROLLERS-module2 (7).pptx
 
Computer organization and architecture
Computer organization and architectureComputer organization and architecture
Computer organization and architecture
 
ARM instruction set
ARM instruction  setARM instruction  set
ARM instruction set
 
ARM Architecture Instruction Set
ARM Architecture Instruction SetARM Architecture Instruction Set
ARM Architecture Instruction Set
 
Instruction types
Instruction typesInstruction types
Instruction types
 
Module 2 PPT of ES.pptx
Module 2 PPT of ES.pptxModule 2 PPT of ES.pptx
Module 2 PPT of ES.pptx
 
Lec3 instructions branch carl hamcher
Lec3 instructions branch carl hamcher Lec3 instructions branch carl hamcher
Lec3 instructions branch carl hamcher
 
Program execution, straight line sequence and branching
Program execution, straight line sequence and branchingProgram execution, straight line sequence and branching
Program execution, straight line sequence and branching
 
Addressing modes
Addressing modesAddressing modes
Addressing modes
 
Auto Tuning
Auto TuningAuto Tuning
Auto Tuning
 
8051 microcontroller notes continuous
8051 microcontroller notes continuous 8051 microcontroller notes continuous
8051 microcontroller notes continuous
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.ppt
 
UNIT 2 ERTS.ppt
UNIT 2 ERTS.pptUNIT 2 ERTS.ppt
UNIT 2 ERTS.ppt
 
Winter training,Readymade Projects,Buy Projects,Corporate Training
Winter training,Readymade Projects,Buy Projects,Corporate TrainingWinter training,Readymade Projects,Buy Projects,Corporate Training
Winter training,Readymade Projects,Buy Projects,Corporate Training
 
(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)
 
Arm instruction set
Arm instruction setArm instruction set
Arm instruction set
 
module-3.pptx
module-3.pptxmodule-3.pptx
module-3.pptx
 
UNIT-3.pptx
UNIT-3.pptxUNIT-3.pptx
UNIT-3.pptx
 

Recently uploaded

EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Recently uploaded (20)

EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 

Code generation

  • 2. Introduction • The final phase of our compiler model is code generator. • It takes input from the intermediate representation with supplementary information in symbol table of the source program and produces as output an equivalent target program. • Code generator main tasks: – Instruction selection – Register allocation and assignment – Instruction ordering
  • 3. • Instruction selection • choose appropriate target-machine instructions to implement the IR statements • Register allocation and assignment • decide what values to keep in which registers • Instruction ordering • decide in what order to schedule the execution of instructions
  • 4.
  • 5. Issues in the design of code generator • Input to the code generator • Target program • Memory management • Instruction selection • Register allocation • Choice of evaluation order • Approaches to code generation
  • 6. Issues in the design of code generator • Input to the code generator – IR + Symbol table – IR has several choices • Postfix notation • Syntax tree • Three address code – We assume front end produces low-level IR, i.e. values of names in it can be directly manipulated by the machine instructions. – Syntactic and semantic errors have been already detected
  • 7. Issues in the design of code generator • The target program – The output of code generator is target program. – Output may take variety of forms • Absolute machine language(executable code) • Relocatable machine language(object files for linker) • Assembly language(facilitates debugging) – Absolute machine language has advantage that it can be placed in a fixed location in memory and immediately executed. – Relocatable machine language program allows subprograms to be compiled separately. – Producing assembly language program as o/p makes the process of code generation somewhat easier.
  • 8. Issues in the design of code generator • Memory management – Mapping names in the source program to addresses of data objects in run time memory is done by front end & code generator. – If a machine code is being generated, labels in three address statements have to be converted to addresses of instructions. This process is analogous to “backpatching” technique.
  • 9. Issues in the design of code generator • Instruction selection – Uniformity and completeness of the instruction set are imp factors – If we do not care about the efficiency of the target program, instruction selection is straightforward. – The quality of the generated code is determined by its speed and size. – For ex, x=y+z LD R0, y ADD R0, R0, z ST x, R0
  • 10. a=b+c d=a+e LD R0, b ADD R0, R0, c ST a, R0 LD R0, a ADD R0, R0, e ST d, R0
  • 11. Issues in the design of code generator • Register allocation – Instructions involving register operands are usually shorter and faster than those involving operands in memory. – Two subproblems • Register allocation: select the set of variables that will reside in registers at each point in the program • Register assignment: select specific register that a variable reside in – Complications imposed by the hardware architecture • Example: register pairs for multiplication and division
  • 12. • The multiplication instruction is of the form M x, y where x, is the multiplicand, is the even register of an even/odd register pair. • The multiplicand value is taken from the odd register pair. The multiplier y is a single register. The product occupies the entire even/odd register pair.
  • 13. t=a+b t=t*c t=a+b T=t/d t=t+c T=t/d L R1, a L R0, a A R1, b A R0, b M R0, c A R0, c D R0, d SRDA R0, 32 ST R1, t (shift right double arithmetic) D R0, d ST R1, t
  • 14. INC a MOV a, R0 ADD #1,R0 MOV R0, a
  • 15. Issues in the design of code generator • Approaches to code generator – Criterion for a code generator is to produce correct code. – Given the premium on correctness, designing a code generator so it can be easily implemented, tested, and maintained is an important design goal.
  • 16. Issues in the design of code generator • Choice of evaluation order – The order in which computations are performed can affect the efficiency of the target code. – When instructions are independent, their evaluation order can be changed MOV a,R0 ADD b,R0 MOV R0,t1 t1:=a+b MOV c,R1 t2:=c+d ADD d,R1 a+b-(c+d)*e MOV e,R0 t3:=e*t2 t4:=t1-t3 MUL R1,R0 MOV c,R0 MOV t1,R1 ADD d,R0 reorder SUB R0,R1 MOV e,R1 MOV R1,t4 MUL R0,R1 t2:=c+d MOV a,R0 t3:=e*t2 ADD b,R0 t1:=a+b SUB R1,R0 t4:=t1-t3 MOV R0,t4
  • 17. Target machine • Implementing code generation requires thorough understanding of the target machine architecture and its instruction set • Our (hypothetical) machine: – Byte-addressable (word = 4 bytes) – Has n general purpose registers R0, R1, …, Rn-1 – Two-address instructions of the form op source, destination – Op – op-code – Source, destination – data fields
  • 18. The Target Machine: Op-codes • Op-codes (op), for example • MOV (move content of source to destination) • ADD (add content of source to destination) • SUB (subtract content of source from destination) • There are also other ops 18
  • 19. The Target Machine: Address modes • Addressing mode: Different ways in which location of an operand can be specified in the instruction. Added Mode Form Address Cost Absolute M M 1 Register R R 0 Indexed c(R) c+contents(R) 1 Indirect register *R contents(R) 0 Indirect indexed *c(R) contents(c+contents(R)) 1 Literal #c N/A 1
  • 20. Instruction Costs • Machine is a simple processor with fixed instruction costs • In most of the machines and in most of the instructions the time taken to fetch an instruction from memory exceeds the time spent executing the instruction. So reducing the length of the instruction has an extra benefit. 20
  • 21. Examples Instruction Operation Cost MOV R0,R1 Store content(R0) into register R1 1 MOV R0,M Store content(R0) into memory location M 2 MOV M,R0 Store content(M) into register R0 2 MOV 4(R0),M Store contents(4+contents(R0)) into M 3 MOV *4(R0),M Store contents(contents(4+contents(R0))) into M 3 MOV #1,R0 Store 1 into R0 2 ADD 4(R0),*12(R1) Add contents(4+contents(R0)) to value at location contents(12+contents(R1)) 3 21
  • 22. Instruction Selection • Instruction selection is important to obtain efficient code • Suppose we translate three-address code x:= y + z to: MOV y,R0 ADD z,R0 MOV R0,x a:=a+1 MOV a,R0 ADD #1,R0 MOV R0,a Cost = 6 Better Best ADD #1,a INC a Cost = 3 Cost = 2 22
  • 23. Instruction Selection: Utilizing Addressing Modes • Suppose we translate a:=b+c into MOV b,R0 ADD c,R0 MOV R0,a • Assuming addresses of a, b, and c are stored in R0, R1, and R2 MOV *R1,*R0 ADD *R2,*R0 • Assuming R1 and R2 contain values of b and c ADD R2,R1 MOV R1,a 23
  • 24. Run Time Storage Management • The information needed during an execution of a procedure is kept in a block of storage called activation record. • Whenever a procedure is called a stack frame is allocated on the stack • The information stored in the stack are return address, local variables, temporaries, return values etc. • 2 standard storage allocation strategies namely – Static allocation – Stack allocation
  • 25. • In static allocation the position of an activation record in memory is fixed at compile time. • In stack allocation a new activation record is pushed onto the stack for each execution of the procedure. The record is popped when the activation ends.
  • 26. • An activation record for a procedure has fields to hold parameters, results, machine- status information, local data, temporaries and the like. Since run-time allocation and de- allocation of activation records occurs as part of the procedure call and return sequences, we focus on the following three-address statements: 1. call 2. return 3. halt 4. action, a placeholder for other statements
  • 27. Static allocation • A call statement, needs a move instruction to save the return address and GOTO transfers control to the target code for the called procedure. – MOV instruction saves return address. – GOTO transfers control to the target code for the called procedure. – Callee.static_area, calle.code_area are constants referring to the address of activation record and first instruction for the called procedure. – #here+20 in the mov instruction is return address. – Cost of 2 instructions is 5
  • 29. Activation record Activation record for c(64 bytes) for p(88 bytes) 0: Return address 0: Return address 8: 4: arr buf 56: I 84: n 60: J
  • 31. Stack allocation • Static allocation can become stack allocation by using relative addresses for storage in activation records. The position of the record for an activation of a procedure is not known until run time. • In stack allocation, this position is usually stored in a register, so words in the activation record can be accessed as offsets from the value in this register • When a procedure call occurs, the calling procedure increments SP and transfers control to the called procedure. • After control returns to the caller, it decrements SP, thereby de-allocating the activation record of the called procedure.
  • 32. • The code for the 1st procedure initializes the stack by setting SP to the start of the stack area in memory. MOV #stackstart, SP /*initialize the stack*/ code for the first procedure HALT /*terminate execution*/ • A procedure call sequence increments SP, saves the return address, and transfers control to the called procedure: ADD #caller.recordsize, SP MOV #here+16, SP /* save return address*/ GOTO callee.code_area • The attribute caller.recordsize represents the size of an activation record, so the ADD instruction leaves SP pointing to the beginning of the next activation record.
  • 33. • The source #here+16 in the MOV instruction is the address of the instruction following the GOTO; it is saved in the address pointed to by SP. • The return sequence consists of two parts. The called procedure transfers control to the return address using GOTO *0(SP) /*return to caller*/ – The reason for using *0(SP) in the GOTO instruction is that we need two levels of indirection: 0(SP) is the address of the first word in the activation record and *0(SP) is the return address saved there. – The second part of the return sequence is in the caller, which decrements SP, thereby restoring SP to its previous value. That is, after the subtraction SP points to the beginning of the activation record of the caller: SUB #caller.recordsize, SP
  • 34. The following program is a condensation of the three-address code for the Pascal program for reading and sorting integers. Procedure q is recursive, so more than one activation of q can be alive at the same time.
  • 35.
  • 36.
  • 37.
  • 38. 600: /*stack starts here*/
  • 39. Register allocation and assignment • Values in registers are easier and faster to access than memory – Reserve a few registers for stack pointers, base addresses etc – Efficiently utilize the rest of general-purpose registers • Register allocation – At each program point, select a set of values to reside in registers • Register assignment – Pick a specific register for each value, subject to hardware constraints – Register classes: not all registers are equal
  • 40. • One approach to register allocation and assignment is to assign specific values in a object program to certain registers. • Advantage of this method is that design of compiler will become easy. • Disadvantage is that, registers will be used inefficiently, certain registers will go unused and unnecessary loads and stores are generated.
  • 41. Global Register Allocation • In code generation algo, registers were used to hold values of a single basic block. • At the end of a block, store instructions were needed in order to store the live values in memory. • Live variables : are the ones which are going to be used in subsequent blocks.
  • 42. • So, LRA was slightly modified for the registers in order to hold the frequently used variables and keep these registers consistent across block boundaries(globally) • Programs will spend most of their time in inner loops, a natural approach is that to keep a frequently used value in a fixed registers throughout the loop. • But number of registers is fixed.
  • 43. • Programs are written as if there are only two kinds of memory: main memory and disk • Programmer is responsible for moving data from disk to memory (e.g., file I/O) • Hardware is responsible for moving data between memory and caches • Compiler is responsible for moving data between memory and registers
  • 44. Usage counts • If a variable say x, is in register then we can say that we have saved 1 cost. • If a variable x, is defined somewhere outside the loop( a basic block), then for every usage of variable x in block we will save 1 cost. • For every variable x computed in block, if it is live on exit from block, then count a saving of 2, since it is not necessary to store it at the end of the block.
  • 45. Where use(x,B) is the number of times x is used in B prior to any definition of x. live(x,B) is 1 if x is live on exit from B and is assigned a value in B., 0 otherwise.
  • 46.
  • 47.
  • 48.
  • 49. B1 B2 B3 B4 Total count A (0+2) (1+0) (1+0) (0+0) 4 B (2+0) (0+0) (0+2) (0+2) 6 C (1+0) (0+0) (1+0) (1+0) 3 D (1+2) (1+0) (1+0) (1+0) 6 E (0+2) (0+0) (0+2) (0+0) 4 F (1+0) (0+2) (1+0) (0+0) 4
  • 50. Code sequence using global register assignment
  • 51. Register assignment for outer loops • If an outer loop L1 contains an inner loop L2, the names allocated registers in L2 need not be allocated registers in L1-L2. • However if name x is allocated a register in loop L1 but not in L2, we must store x on entrance to L2 and load x on exit from L2. • Similarly, if we choose to allocate x a register in L2 but not L1, we must load x on entrance to L2 and store x on exit from L2.
  • 52. Register allocation by graph coloring • When a reg is needed for a computation but all available reg are in use, the contents of one of the used reg must be stored(spilled) into memory location. • Perform dataflow liveness analysis over a CFG: – collect variable liveness info at every program point • Build an interference graph: – each node represents a temporary value – each edge represents an interference between two temporaries—they can’t be assigned to the same register
  • 53. • Color the interference graph: – colors = registers; want to use as few colors as possible – for a machine with K registers, decide whether the interference graph is K-colorable – if yes, the coloring gives a register assignment – If no, need to spill some temporaries to memory
  • 54. Peephole Optimization • Statement by statement code generation strategy often produces target code that contains redundant instructions and suboptimal constructs. • No guarantee that resulting code is optimal under any mathematical measure. • Peephole optimization: a short sequence of target instructions(peephole instructions) that can be replaced by shorter or faster sequence instructions.
  • 55. • Repeated passes over the target code are necessary to get the maximum benefit. • Characteristics of peephole optimization – Redundant- instruction elimination – Flow of control optimizations – Algebraic simplifications – Use of machine idioms
  • 57. Unreachable code • Unlabeled instruction following an unconditional jump may be eliminated
  • 58. Flow of control optimizations
  • 59.
  • 60. Algebraic simplification • Eliminate the instructions like the following – x = x+0 – x = x*1 These type of instructions are often produced by straightforward intermediate code generation algorithms, and they can be easily eliminated by peephole optimization.
  • 61. Reduction in strength • Reduction in strength replaces expensive operations by equivalent cheaper ones on the target machine. ex, X2 = X * X
  • 62. Use of machine idioms • The target machine may have hardware instructions to implement certain specific operations efficiently. • Detecting situations that permit the use of these instructions can reduce execution time significantly. • Ex, – Increment and decrement addressing modes