SlideShare a Scribd company logo
1 of 44
CODE GENERATION
    (Part I)
Agenda
•   Introduction
•   Code Generation
•   Issues in the Design of a Code Generator
•   The Target Language
•   Addresses in the Target Code
•   Basic Blocks and Flow Graphs
•   Optimization of Basic Blocks
•   Next…
09/03/12                                       2
Introduction
  • The final phase in our compiler model

Source Front End Intermediate  Code     Intermediate   Code    Target
program              code     Optimizer     code     Generator program




            Position Of a Code Generator




 09/03/12                                                         3
Code Generation
• This phase generates the target code consisting of
   assembly code.
     1. Memory locations are selected for each variable;

     2. Instructions are translated into a sequence of assembly instructions;

     3. Variables and intermediate results are assigned to memory registers.




09/03/12                                                                        4
Issue in the Design of a Code
               Generator
•   Input to the Code Generator
•   The Target Program
•   Instruction Selection
•   Register Allocation
•   Evaluation Order




09/03/12                            5
Input to the Code Generator
• The input to the code generator is
     – The intermediate representation of the source program
       produced by the frontend along with the symbol table.


• Choices for the IR
     –     Three-address representations
     –     Virtual machine representations
     –     Linear representations
     –     Graphical representation




09/03/12                                                       6
The Target Program
• The instruction-set architecture of the target machine
  has a significant impact on the difficulty of constructing a
  good code generator that produces high-quality machine
  code.
• The most common target-machine architecture are
  RISC, CISC, and stack based.
     – A RISC machine typically has many registers, three-address
       instructions, simple addressing modes, and a relatively simple
       instruction-set architecture.




09/03/12                                                                7
Contd…
     – A CISC machine typically has few registers, two-address
       instructions, and variety of addressing modes, several register
       classes, variable-length instructions, and instruction with side
       effects.

     – In a stack-based machine, operations are done by pushing
       operands onto a stack and then performing the operations on
       the operands at the top of the stack.




09/03/12                                                                  8
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   Better


                      ADD #1,a   INC a
09/03/12                                                9
                      Cost = 3   Cost = 2
Register Allocation
• A key problem in code generation is deciding
  what values to hold in what registers.
• Efficient utilization is particularly important.
• The use of registers is often subdivided into two
  sub problems:
     1. Register Allocation, during which we select the set of variables
        that will reside in registers at each point in the program.
     2. Register assignment, during which we pick the specific
        register that a variable will reside in.



09/03/12                                                              10
Contd…
• Finding an optimal assignment of registers to
  variables is difficult, even with single-register
  machine.
• Mathematically, the problem is NP-complete.




09/03/12                                              11
Evaluation Order
• The order in which computations are performed
  can affect the efficiency of the target code.

• Some computation orders require fewer
  registers to hold intermediate results than
  others.

• However, picking a best order in the general
  case is a difficult NP-complete problem.
09/03/12                                          12
The Target Language
• A Simple Target Machine Model
• Program and Instruction Costs




09/03/12                          13
A Simple Target Machine Model
• Our target computer models a three-address
  machine with load and store operations,
  computation operations, jump operations, and
  conditional jumps.

• The underlying computer is a byte-addressable
  machine with n general-purpose registers.



09/03/12                                          14
Contd…
• Assume the following kinds of instructions are
  available:
     –     Load operations
     –     Store operations
     –     Computation operations
     –     Unconditional jumps
     –     Conditional jumps




09/03/12                                           15
Contd…
• Assume a variety of addressing models:
     – A variable name x referring o the memory location
       that is reserved for x.
     – Indexed address, a(r), where a is a variable and r is a
       register.
     – A memory can be an integer indexed by a register, for
       example, LD R1, 100(R2).
     – Two indirect addressing modes: *r and *100(r)
     – Immediate constant addressing mode


09/03/12                                                    16
A Simple Target Machine Model
• Example :

x = y –z ⇒ LD R1, y          x = *p ⇒   LD R1, p
            LD R2, z                     LD R2, 0(R1)
            SUB R1, R1, R2               ST x, R2
            ST x, R1



b = a[i] ⇒ LD R1, i          *p = y ⇒   LD R1, p
            MUL R1, R1, 8                LD R2, y
            LD R2, a(R1)                 ST 0(R1), R2
            ST b, R2

09/03/12                                           17
a[j] = c ⇒ LD R1, c
            LD R2, j
            MUL R2, R2, 8
            ST a(R2), R1




if x < y goto L   ⇒   LD   R1, x
                       LD   R2, y
                       SUB R1, R1, R2
                       BLTZ R1, L


09/03/12                                18
Program and Instruction Costs
• For simplicity, we take the cost of an instruction
  to be one plus the costs associated with the
  addressing modes of the operands.

• Addressing modes involving registers have zero
  additional cost, while those involving a memory
  location or constant in them have an additional
  cost f one.


09/03/12                                               19
Contd…
• For example,
     – LD R0, R1 cost = 1
     – LD R0, M cost = 2
     – LD R1, *100(R2) cost = 3




09/03/12                          20
Addresses in the Target Code
• Static Allocation
• Stack Allocation
• Run-Time Allocation




09/03/12                          21
Static Allocation
• Focus on the following three-address
  statements:
     –     Call callee
     –     Return
     –     Halt
     –     Action




09/03/12                                     22
Static Allocation
• Store return address and return control to caller

• Store
   ST callee.staticArea , #here + 20
   BR callee.codeArea


• Return
   BR *callee.staticArea

09/03/12                                              23
Runtime Addresses for Names
• Assumption: a name in a three-address
  statement is really a pointer to a symbol-
  table entry for that name.

• Note that names must eventually be
  replaced by code to access storage
  locations


09/03/12                                       24
Contd…
• Example:
     – x=0
     – Suppose the symbol-table entry for x contains a relative address
       12
     – x is in a statically allocated area beginning at address static
     – the actual run-time address of x is static + 12
     – The actual assignment: static [ 12] = 0
     – For a static area starting at address 100: LD 112, #0




09/03/12                                                             25
Basic Blocks and Flow Graphs
•   Basic Blocks
•   Next-Use Information
•   Flow Graphs
•   Representation of Flow Graphs
•   Loops




09/03/12                            26
Basic Blocks
• Algorithm: Partitioning three-address instructions
  into basic blocks.

     INPUT: A sequence of three-address instructions.

     OUTPUT: A list of the basic blocks for that sequence in which each
      instruction is assigned to exactly one basic block.




09/03/12                                                             27
Contd…
     METHOD: First, we determine those instructions in the
      intermediate code that are leaders. rules for finding leaders are:

           1. The first three-address instruction in the intermediate code is
           a leader.
           2. Any instruction that is the target of a conditional or
           unconditional jump is a leader.
           3. Any instruction that immediately follows a conditional or
           unconditional jump is a leader.




09/03/12                                                                    28
Contd…
• Find the leaders




09/03/12                 29
Next-Use Information
• The use of a name in a three-address
  statement:
     – Three-address statement i assigns a value to x
     – Statement j has x as an operand
     – Control can flow from statement i to j along a path that has no
       intervening assignments to x
     – Then statement j uses the value of x computed at i .

     – Say that x is live at statement i .




09/03/12                                                                 30
Next-Use Information
• Algorithm (for x=y+z) : Determining the liveness
  and next-use information for each statement in a
  basic block.

     – INPUT: A basic block B of three-address statements. Assume
       the symbol table initially shows all nontemporary variables in B
       as being live on exit.

     – OUTPUT: At each statement i : x = y + z in B, attach to i the
       liveness and next-use information of x, y, and z .



09/03/12                                                                  31
Contd…
     – METHOD: Start at the last statement in B and scan backwards
       to the beginning of B. At each statement i: x = y + z in B, do the
       following:
       1. Attach to i the information currently found in the symbol table
       regarding the next use and
           liveness of x , y, and z.
       2. In the symbol table, set x to "not live" and "no next use."
       3. In the symbol table, set y and z to "live" and the next uses of y
       and z to i.




09/03/12                                                                 32
Flow Graphs
• A flow graph is a graphical depiction of a
  sequence of instructions with control flow
  edges
• A flow graph can be defined at the
  intermediate code level or target code
  level
               MOV 1,R0          MOV 0,R0
               MOV n,R1          MOV n,R1
               JMP L2            JMP L2
           L1: MUL 2,R0      L1: MUL 2,R0
               SUB 1,R1          SUB 1,R1
09/03/12   L2: JMPNZ R1,L1   L2: JMPNZ R1,L1   33
Loops
• A loop is a collection of basic blocks, such
  that
     – All blocks in the collection are strongly
       connected
     – The collection has a unique entry, and the
       only way to reach a block in the loop is
       through the entry



09/03/12                                            34
Optimizing of Basic Block
•   Compile time evaluation
•   Common sub-expression elimination
•   Code motion
•   Strength Reduction
•   Dead code elimination
•   Algebraic Transformations
Compile-Time Evaluation
• Expressions whose values can be pre-computed
  at the compilation time
• Two ways:
   – Constant folding
   – Constant propagation




                      36
Compile-Time Evaluation
• Constant folding: Evaluation of an expression
  with constant operands to replace the
  expression with single value
• Example:

    area := (22.0/7.0) * r ^ 2



    area := 3.14286 * r ^ 2

                       37
Compile-Time Evaluation
• Constant Propagation: Replace a variable with
  constant which has been assigned to it earlier.
• Example:
                pi := 3.14286
                area = pi * r ^ 2



                area = 3.14286 * r ^ 2
Common Sub-expression Elimination

• Local common sub-expression elimination
   – Performed within basic blocks.




        a := b * c          temp := b * c
        …                   a := temp
                            …
        …                   …
        x := b * c + 5      x := temp + 5
Code Motion
• Moving code from one part of the program
  to other without modifying the algorithm
  – Reduce size of the program
  – Reduce execution frequency of the code
    subjected to movement




                      40
Code Motion
•   Similar to common sub-expression elimination
    but with the objective to reduce code size.



                              temp := x * 2
    If(a<b) then
                              If(a<b) then
      z:= x * 5
                                z:= temp
    else
                              else
      y := x * 5 + 2
                                y := temp + 2
Strength Reduction

• Replacement of an operator with a less costly one.



    X=x^2                        X=x*x


    Y=y*2                        Y=y+y
Dead Code Elimination
• Dead Code are portion of the program which will
  not be executed in Basic block.


    If(a==b)                  If(a==b)
    {                         {
    b=c ;                     b=c ;
    …..                       …..
    return b ;                return b ;
    c=0;                      }
    }
References
• Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman,
  “Compilers: Principles, Techniques, and Tools”
  Addison-Wesley, 1986.

• http://en.wikipedia.org/wiki/Code_generation_(compiler)
• http://www.mec.ac.in/resources/notes/notes/compiler/mo
  dule5/codegenissues.htm




09/03/12                                                44

More Related Content

What's hot

Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 
Types of instructions
Types of instructionsTypes of instructions
Types of instructionsihsanjamil
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignAkhil Kaushik
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lexAnusuya123
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow GraphsJenny Galino
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 

What's hot (20)

Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Three Address code
Three Address code Three Address code
Three Address code
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Types of instructions
Types of instructionsTypes of instructions
Types of instructions
 
Memory management
Memory managementMemory management
Memory management
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - 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
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow Graphs
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
 

Similar to Code generator

Instruction set.pptx
Instruction set.pptxInstruction set.pptx
Instruction set.pptxssuser000e54
 
GCC for ARMv8 Aarch64
GCC for ARMv8 Aarch64GCC for ARMv8 Aarch64
GCC for ARMv8 Aarch64Yi-Hsiu Hsu
 
Lecture 4 assembly language
Lecture 4   assembly languageLecture 4   assembly language
Lecture 4 assembly languagePradeep Kumar TS
 
Computer organization basics
Computer organization  basicsComputer organization  basics
Computer organization basicsDeepak John
 
Linked in nosql_atnetflix_2012_v1
Linked in nosql_atnetflix_2012_v1Linked in nosql_atnetflix_2012_v1
Linked in nosql_atnetflix_2012_v1Sid Anand
 
(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
 
QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...Scality
 
05 instruction set design and architecture
05 instruction set design and architecture05 instruction set design and architecture
05 instruction set design and architectureWaqar Jamil
 
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019 Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019 Unity Technologies
 
Introduction to Computer Architecture
Introduction to Computer ArchitectureIntroduction to Computer Architecture
Introduction to Computer ArchitectureKSundarAPIICSE
 
Computer Organisation and Architecture
Computer Organisation and ArchitectureComputer Organisation and Architecture
Computer Organisation and ArchitectureSubhasis Dash
 
24-02-18 Rejender pratap.pdf
24-02-18 Rejender pratap.pdf24-02-18 Rejender pratap.pdf
24-02-18 Rejender pratap.pdfFrangoCamila
 
CE412 -advanced computer Architecture lecture 1.pdf
CE412 -advanced computer Architecture lecture 1.pdfCE412 -advanced computer Architecture lecture 1.pdf
CE412 -advanced computer Architecture lecture 1.pdfAdelAbougdera
 
Basic Structure of a Computer System
Basic Structure of a Computer SystemBasic Structure of a Computer System
Basic Structure of a Computer SystemAmirthavalli Senthil
 

Similar to Code generator (20)

UNIT-3.pptx
UNIT-3.pptxUNIT-3.pptx
UNIT-3.pptx
 
Instruction set.pptx
Instruction set.pptxInstruction set.pptx
Instruction set.pptx
 
GCC for ARMv8 Aarch64
GCC for ARMv8 Aarch64GCC for ARMv8 Aarch64
GCC for ARMv8 Aarch64
 
Lecture 4 assembly language
Lecture 4   assembly languageLecture 4   assembly language
Lecture 4 assembly language
 
Computer organization basics
Computer organization  basicsComputer organization  basics
Computer organization basics
 
8871077.ppt
8871077.ppt8871077.ppt
8871077.ppt
 
Linked in nosql_atnetflix_2012_v1
Linked in nosql_atnetflix_2012_v1Linked in nosql_atnetflix_2012_v1
Linked in nosql_atnetflix_2012_v1
 
(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)
 
module-3.pptx
module-3.pptxmodule-3.pptx
module-3.pptx
 
QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...QuadIron An open source library for number theoretic transform-based erasure ...
QuadIron An open source library for number theoretic transform-based erasure ...
 
ARM Processors
ARM ProcessorsARM Processors
ARM Processors
 
05 instruction set design and architecture
05 instruction set design and architecture05 instruction set design and architecture
05 instruction set design and architecture
 
Central processor organization
Central processor organizationCentral processor organization
Central processor organization
 
ram.pdf
ram.pdfram.pdf
ram.pdf
 
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019 Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
Intrinsics: Low-level engine development with Burst - Unite Copenhagen 2019
 
Introduction to Computer Architecture
Introduction to Computer ArchitectureIntroduction to Computer Architecture
Introduction to Computer Architecture
 
Computer Organisation and Architecture
Computer Organisation and ArchitectureComputer Organisation and Architecture
Computer Organisation and Architecture
 
24-02-18 Rejender pratap.pdf
24-02-18 Rejender pratap.pdf24-02-18 Rejender pratap.pdf
24-02-18 Rejender pratap.pdf
 
CE412 -advanced computer Architecture lecture 1.pdf
CE412 -advanced computer Architecture lecture 1.pdfCE412 -advanced computer Architecture lecture 1.pdf
CE412 -advanced computer Architecture lecture 1.pdf
 
Basic Structure of a Computer System
Basic Structure of a Computer SystemBasic Structure of a Computer System
Basic Structure of a Computer System
 

More from Tech_MX

Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimationTech_MX
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2Tech_MX
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2Tech_MX
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating SystemTech_MX
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Tech_MX
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pcTech_MX
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbmsTech_MX
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)Tech_MX
 
Memory dbms
Memory dbmsMemory dbms
Memory dbmsTech_MX
 

More from Tech_MX (20)

Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Uid
UidUid
Uid
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimation
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
String & its application
String & its applicationString & its application
String & its application
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Spss
SpssSpss
Spss
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
 
Set data structure
Set data structure Set data structure
Set data structure
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating System
 
Parsing
ParsingParsing
Parsing
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pc
 
More on Lex
More on LexMore on Lex
More on Lex
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbms
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
Memory dbms
Memory dbmsMemory dbms
Memory dbms
 

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 

Code generator

  • 1. CODE GENERATION (Part I)
  • 2. Agenda • Introduction • Code Generation • Issues in the Design of a Code Generator • The Target Language • Addresses in the Target Code • Basic Blocks and Flow Graphs • Optimization of Basic Blocks • Next… 09/03/12 2
  • 3. Introduction • The final phase in our compiler model Source Front End Intermediate Code Intermediate Code Target program code Optimizer code Generator program Position Of a Code Generator 09/03/12 3
  • 4. Code Generation • This phase generates the target code consisting of assembly code. 1. Memory locations are selected for each variable; 2. Instructions are translated into a sequence of assembly instructions; 3. Variables and intermediate results are assigned to memory registers. 09/03/12 4
  • 5. Issue in the Design of a Code Generator • Input to the Code Generator • The Target Program • Instruction Selection • Register Allocation • Evaluation Order 09/03/12 5
  • 6. Input to the Code Generator • The input to the code generator is – The intermediate representation of the source program produced by the frontend along with the symbol table. • Choices for the IR – Three-address representations – Virtual machine representations – Linear representations – Graphical representation 09/03/12 6
  • 7. The Target Program • The instruction-set architecture of the target machine has a significant impact on the difficulty of constructing a good code generator that produces high-quality machine code. • The most common target-machine architecture are RISC, CISC, and stack based. – A RISC machine typically has many registers, three-address instructions, simple addressing modes, and a relatively simple instruction-set architecture. 09/03/12 7
  • 8. Contd… – A CISC machine typically has few registers, two-address instructions, and variety of addressing modes, several register classes, variable-length instructions, and instruction with side effects. – In a stack-based machine, operations are done by pushing operands onto a stack and then performing the operations on the operands at the top of the stack. 09/03/12 8
  • 9. 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 Better ADD #1,a INC a 09/03/12 9 Cost = 3 Cost = 2
  • 10. Register Allocation • A key problem in code generation is deciding what values to hold in what registers. • Efficient utilization is particularly important. • The use of registers is often subdivided into two sub problems: 1. Register Allocation, during which we select the set of variables that will reside in registers at each point in the program. 2. Register assignment, during which we pick the specific register that a variable will reside in. 09/03/12 10
  • 11. Contd… • Finding an optimal assignment of registers to variables is difficult, even with single-register machine. • Mathematically, the problem is NP-complete. 09/03/12 11
  • 12. Evaluation Order • The order in which computations are performed can affect the efficiency of the target code. • Some computation orders require fewer registers to hold intermediate results than others. • However, picking a best order in the general case is a difficult NP-complete problem. 09/03/12 12
  • 13. The Target Language • A Simple Target Machine Model • Program and Instruction Costs 09/03/12 13
  • 14. A Simple Target Machine Model • Our target computer models a three-address machine with load and store operations, computation operations, jump operations, and conditional jumps. • The underlying computer is a byte-addressable machine with n general-purpose registers. 09/03/12 14
  • 15. Contd… • Assume the following kinds of instructions are available: – Load operations – Store operations – Computation operations – Unconditional jumps – Conditional jumps 09/03/12 15
  • 16. Contd… • Assume a variety of addressing models: – A variable name x referring o the memory location that is reserved for x. – Indexed address, a(r), where a is a variable and r is a register. – A memory can be an integer indexed by a register, for example, LD R1, 100(R2). – Two indirect addressing modes: *r and *100(r) – Immediate constant addressing mode 09/03/12 16
  • 17. A Simple Target Machine Model • Example : x = y –z ⇒ LD R1, y x = *p ⇒ LD R1, p LD R2, z LD R2, 0(R1) SUB R1, R1, R2 ST x, R2 ST x, R1 b = a[i] ⇒ LD R1, i *p = y ⇒ LD R1, p MUL R1, R1, 8 LD R2, y LD R2, a(R1) ST 0(R1), R2 ST b, R2 09/03/12 17
  • 18. a[j] = c ⇒ LD R1, c LD R2, j MUL R2, R2, 8 ST a(R2), R1 if x < y goto L ⇒ LD R1, x LD R2, y SUB R1, R1, R2 BLTZ R1, L 09/03/12 18
  • 19. Program and Instruction Costs • For simplicity, we take the cost of an instruction to be one plus the costs associated with the addressing modes of the operands. • Addressing modes involving registers have zero additional cost, while those involving a memory location or constant in them have an additional cost f one. 09/03/12 19
  • 20. Contd… • For example, – LD R0, R1 cost = 1 – LD R0, M cost = 2 – LD R1, *100(R2) cost = 3 09/03/12 20
  • 21. Addresses in the Target Code • Static Allocation • Stack Allocation • Run-Time Allocation 09/03/12 21
  • 22. Static Allocation • Focus on the following three-address statements: – Call callee – Return – Halt – Action 09/03/12 22
  • 23. Static Allocation • Store return address and return control to caller • Store ST callee.staticArea , #here + 20 BR callee.codeArea • Return BR *callee.staticArea 09/03/12 23
  • 24. Runtime Addresses for Names • Assumption: a name in a three-address statement is really a pointer to a symbol- table entry for that name. • Note that names must eventually be replaced by code to access storage locations 09/03/12 24
  • 25. Contd… • Example: – x=0 – Suppose the symbol-table entry for x contains a relative address 12 – x is in a statically allocated area beginning at address static – the actual run-time address of x is static + 12 – The actual assignment: static [ 12] = 0 – For a static area starting at address 100: LD 112, #0 09/03/12 25
  • 26. Basic Blocks and Flow Graphs • Basic Blocks • Next-Use Information • Flow Graphs • Representation of Flow Graphs • Loops 09/03/12 26
  • 27. Basic Blocks • Algorithm: Partitioning three-address instructions into basic blocks. INPUT: A sequence of three-address instructions. OUTPUT: A list of the basic blocks for that sequence in which each instruction is assigned to exactly one basic block. 09/03/12 27
  • 28. Contd… METHOD: First, we determine those instructions in the intermediate code that are leaders. rules for finding leaders are: 1. The first three-address instruction in the intermediate code is a leader. 2. Any instruction that is the target of a conditional or unconditional jump is a leader. 3. Any instruction that immediately follows a conditional or unconditional jump is a leader. 09/03/12 28
  • 29. Contd… • Find the leaders 09/03/12 29
  • 30. Next-Use Information • The use of a name in a three-address statement: – Three-address statement i assigns a value to x – Statement j has x as an operand – Control can flow from statement i to j along a path that has no intervening assignments to x – Then statement j uses the value of x computed at i . – Say that x is live at statement i . 09/03/12 30
  • 31. Next-Use Information • Algorithm (for x=y+z) : Determining the liveness and next-use information for each statement in a basic block. – INPUT: A basic block B of three-address statements. Assume the symbol table initially shows all nontemporary variables in B as being live on exit. – OUTPUT: At each statement i : x = y + z in B, attach to i the liveness and next-use information of x, y, and z . 09/03/12 31
  • 32. Contd… – METHOD: Start at the last statement in B and scan backwards to the beginning of B. At each statement i: x = y + z in B, do the following: 1. Attach to i the information currently found in the symbol table regarding the next use and liveness of x , y, and z. 2. In the symbol table, set x to "not live" and "no next use." 3. In the symbol table, set y and z to "live" and the next uses of y and z to i. 09/03/12 32
  • 33. Flow Graphs • A flow graph is a graphical depiction of a sequence of instructions with control flow edges • A flow graph can be defined at the intermediate code level or target code level MOV 1,R0 MOV 0,R0 MOV n,R1 MOV n,R1 JMP L2 JMP L2 L1: MUL 2,R0 L1: MUL 2,R0 SUB 1,R1 SUB 1,R1 09/03/12 L2: JMPNZ R1,L1 L2: JMPNZ R1,L1 33
  • 34. Loops • A loop is a collection of basic blocks, such that – All blocks in the collection are strongly connected – The collection has a unique entry, and the only way to reach a block in the loop is through the entry 09/03/12 34
  • 35. Optimizing of Basic Block • Compile time evaluation • Common sub-expression elimination • Code motion • Strength Reduction • Dead code elimination • Algebraic Transformations
  • 36. Compile-Time Evaluation • Expressions whose values can be pre-computed at the compilation time • Two ways: – Constant folding – Constant propagation 36
  • 37. Compile-Time Evaluation • Constant folding: Evaluation of an expression with constant operands to replace the expression with single value • Example: area := (22.0/7.0) * r ^ 2 area := 3.14286 * r ^ 2 37
  • 38. Compile-Time Evaluation • Constant Propagation: Replace a variable with constant which has been assigned to it earlier. • Example: pi := 3.14286 area = pi * r ^ 2 area = 3.14286 * r ^ 2
  • 39. Common Sub-expression Elimination • Local common sub-expression elimination – Performed within basic blocks. a := b * c temp := b * c … a := temp … … … x := b * c + 5 x := temp + 5
  • 40. Code Motion • Moving code from one part of the program to other without modifying the algorithm – Reduce size of the program – Reduce execution frequency of the code subjected to movement 40
  • 41. Code Motion • Similar to common sub-expression elimination but with the objective to reduce code size. temp := x * 2 If(a<b) then If(a<b) then z:= x * 5 z:= temp else else y := x * 5 + 2 y := temp + 2
  • 42. Strength Reduction • Replacement of an operator with a less costly one. X=x^2 X=x*x Y=y*2 Y=y+y
  • 43. Dead Code Elimination • Dead Code are portion of the program which will not be executed in Basic block. If(a==b) If(a==b) { { b=c ; b=c ; ….. ….. return b ; return b ; c=0; } }
  • 44. References • Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman, “Compilers: Principles, Techniques, and Tools” Addison-Wesley, 1986. • http://en.wikipedia.org/wiki/Code_generation_(compiler) • http://www.mec.ac.in/resources/notes/notes/compiler/mo dule5/codegenissues.htm 09/03/12 44

Editor's Notes

  1. Familiarity with the target machine and its instruction set is a prerequisite for designing a good code generator. Unfortunately, in a general discussion of code generation it is not possible to describe any target machine in sufficient detail to generate good code for a complete language on that machine. lec08-memoryorg September 3, 2012
  2. • Load operations: The instruction LD dst, addr loads the value in location addr into location dst. This instruction denotes the assignment dst = addr. The most common form of this instruction is LD 1&apos;, x which loads the value in location x into register r. An instruction of the form LD 1&apos;1 , 1&apos;2 is a register-to-register copy in which the contents of register 1&apos;2 are copied into register 1&apos;1 . • Store operations: The instruction S1 x, r stores the value in register r into the location x. This instruction denotes the assignment x = r . • Computation operations of the form O P dst, srCl , srC2 , where O P is a operator like ADD or SUB, and dst, srCl , and srC2 are locations, not necessarily distinct. The effect of this machine instruction is to apply the operation represented by OP to the values in locations srCI and srC2 , and place the result of this operation in location dst. For example, SUB rl , r2 , 1&apos;3 computes ri = r2 - 1&apos;3 . Any value formerly stored in rl is lost, but if 1&apos;1 is r2 or 1&apos;3 , the old value is read first. Unary operators that take only one operand do not have a src2 . Unconditional jumps: The instruction BR L causes control to branch to the machine instruction with label L. (BR stands for branch.) • Conditional jumps of the form Bcond r, L, where r is a register, L is a label, and cond stands for any of the common tests on values in the register r. For example, BLT2 r, L causes a jump to label L if the value in register r is less than zero, and allows control to pass to the next machine instruction if not. lec08-memoryorg September 3, 2012
  3. We assume our target machine has a variety of addressing modes: • In instructions, a location can be a variable name x referring to the memory location that is reserved for x (that is, the I-value of x) . • A location can also be an indexed address of the form a (r), where a is a variable and r is a register. The memory location denoted by a(r) is computed by taking the I-value of a and adding to it the value in register r. For example, the instruction LD Rl , a (R2 ) has the effect of setting Rl = contents ( a + contents (R2) ) , where contents(x) denotes the contents of the register or memory location represented by x. This addressing mode is useful for accessing arrays, where a is the base address of the array (that is, the address of the first element), and r holds the number of bytes past that address we wish to go to reach one of the elements of arrCLY a. • A memory location can be an integer indexed by a register. For example, LD Rl , 100 (R2 ) has the effect of setting R l = contents (100 + contents (R2) ) , that is, of loading into R l the value in the memory location obtained by adding 100 to the contents of register R2. This feature is useful for following pointers, as we shall see in the example below. • We also allow two indirect addressing modes: *r means the memory location found in the location rep&apos;resented by the contents of register r and * 100 (r) means the memory location found in the location obtained by adding 100 to the contents of r. For example, LD Rl , * 100 (R2) has the effect of setting Rl = contents (contents (100 + contents (R2) ) ) , that is, of loading into Rl the value in the memory location stored in the memory location obtained by adding 100 to the contents of register R2. • Finally, we allow an immediate constant addressing mode. The constant is prefixed by #. The instruction LD Rl , # 100 loads the integer 100 into register Rl , and ADD Rl , Rl , # 100 adds the integer 100 into register R1. lec08-memoryorg September 3, 2012
  4. #here 表示当前指令的指针 20 是三个常量 + 两个指令 共 5 个 word 因此, here+20 是 BR 指令后的指令 The 8T instruction saves the return address at the beginning of the activation record for .callee, and the BR transfers control to the target code for the called procedure callee. The attribute before callee. staticArea is a constant that gives the address of the beginning of the activation record for callee, and the attribute callee. codeA rea is a constant referring to the address of the first instruction of the called procedure callee in the Code area of the run-time memory. The operand #here + 20 in the 8T instruction is the literal return address; it is the address of the instruction following the BR instruction. We assume that #here is the address of the current instruction and that the three constants plus the two instructions in the calling sequence have a length of 5 words or 20 bytes. lec08-memoryorg September 3, 2012
  5. The basic blocks are maximal sequences of consecutive three-address instructions with the properties that (a) The flow of control can only enter the basic block through the first instruction in the block. That is, there are no jumps into the middle of the block. (b) Control will leave the block without halting or branching, except possibly at the last instruction in the block. lec08-memoryorg September 3, 2012
  6. lec08-memoryorg September 3, 2012
  7. The leaders: 1, 2, 3, 10, 12, and 13. lec08-memoryorg September 3, 2012
  8. Knowing when the value of a variable will be used next is essential for generating good code. If the value of a variable that is currently in a register will never be referenced subsequently, then that register can be assigned to another variable. lec08-memoryorg September 3, 2012
  9. Here we have used + as a symbol representing any operator. If the three-address statement i is of the form x = + Y or x = y, the steps are the same as above, ignoring z. Note that the order of steps (2) and (3) may not be interchanged because x may be y or z. 第一步每当开始对一个基本块进行处理时,把块中各变量在符号表相应登记项的待用信息栏置为“无待用”,且依各变量在基本块的出口活跃与否,将相应的活跃信息栏置为“活跃”或“非活跃”。 第二步从基本块的出口开始,反向扫视基本块中的各四元式,设当前正扫视的四元式为 (i)A∶=B OP C ,    对 (i) 依次作如下的处理:    (1) 把符号表中当前所记录之变量 A , B 与 C 的待用信息及活跃信息附加到四元式 (i) 上;    (2) 在符号表中,把与 A 相应的待用信息栏及活跃信息栏分别置为“无待用”及“非活跃”;    (3) 在符号表中,把 B 和 C 的待用信息栏均置为 (i) ,把它们的活跃信息栏置为“活跃”。 lec08-memoryorg September 3, 2012