SlideShare a Scribd company logo
Code Generation Part I Chapter 8 (1 st  ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error Semantic error Intermediate code Code Generator Intermediate code Target program
Code Generation Code produced by compiler must be correct Source-to-target program transformation should be  semantics preserving Code produced by compiler should be of high quality Effective use of target machine resources Heuristic techniques should be used to generate good but suboptimal code, because generating optimal code is undecidable
Target Program Code The back-end code generator of a compiler may generate different forms of code, depending on the requirements: Absolute machine code (executable code) Relocatable machine code (object files for linker) Assembly language (facilitates debugging) Byte code forms for interpreters (e.g. JVM)
The 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 , …,  R n- 1 Two-address instructions of the form op   source ,  destination
The Target Machine: Op-codes and Address Modes 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  dest. ) Address modes Mode Form Address Added 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, non-super-scalar processor with fixed instruction costs Realistic machines have deep pipelines, I-cache, D-cache, etc. Define the cost of instruction = 1 + cost( source -mode) + cost( destination -mode)
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
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 ADD #1,a INC a Cost = 6 Cost = 3 Cost = 2 Better Best
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
Need for Global Machine-Specific Code Optimizations Suppose we translate three-address code x := y + z to: MOV  y ,R0 ADD  z ,R0 MOV R0, x Then, we translate a:=b+c d:=a+e to: MOV a,R0 ADD b,R0 MOV R0,a MOV a,R0 ADD e,R0 MOV R0,d Redundant
Register Allocation and Assignment Efficient utilization of the limited set of registers is important to generate good code Registers are assigned by Register allocation  to select the set of variables that will reside in registers at a point in the code Register assignment  to pick the specific register that a variable will reside in Finding an optimal register assignment in general is NP-complete
Example t:=a*b t:=t+a t:=t/d MOV a,R1 MUL b,R1 ADD a,R1 DIV d,R1 MOV R1,t t:=a*b t:=t+a t:=t/d MOV a,R0 MOV R0,R1 MUL b,R1 ADD R0,R1 DIV d,R1 MOV R1,t {  R1 = t  } {  R0 = a ,  R1 = t  }
Choice of Evaluation Order When instructions are independent, their evaluation order can be changed t1:=a+b t2:=c+d t3:=e*t2 t4:=t1-t3 a+b-(c+d)*e MOV a,R0 ADD b,R0 MOV R0,t1 MOV c,R1 ADD d,R1 MOV e,R0 MUL R1,R0 MOV t1,R1 SUB R0,R1 MOV R1,t4 t2:=c+d t3:=e*t2 t1:=a+b t4:=t1-t3 MOV c,R0 ADD d,R0 MOV e,R1 MUL R0,R1 MOV a,R0 ADD b,R0 SUB R1,R0 MOV R0,t4 reorder
Generating Code for Stack Allocation of Activation Records t1 := a + b param t1 param c t2 := call foo,2 … func foo … return t1 100: ADD #16,SP 108: MOV a,R0 116: ADD b,R0 124: MOV R0,4(SP) 132: MOV c,8(SP) 140: MOV #156,*SP 148: GOTO 500 156: MOV 12(SP),R0 164: SUB #16,SP 172: … 500: … 564: MOV R0,12(SP) 572: GOTO *SP Return to caller Store return value Push frame Store a+b Store c Store return address Jump to foo Get return value Remove frame Note: Language and machine dependent Here we assume C-like implementation with SP and no FP

More Related Content

PPT
Intermediate code generation
PPTX
Three address code In Compiler Design
PPTX
Three address code generation
PPTX
Code optimization
Intermediate code generation
Three address code In Compiler Design
Three address code generation
Code optimization

What's hot (20)

PPTX
Lecture 12 intermediate code generation
PDF
Compiler unit 5
PPT
Chapter 6 intermediate code generation
PPT
Interm codegen
PPTX
Intermediate code generation1
PPTX
Register allocation and assignment
PPT
Chapter Eight(1)
PPTX
Three Address code
PPT
Lecture 03 lexical analysis
PPT
Chapter Eight(2)
PPTX
Intermediate code
PPT
Intermediate code generation
PPTX
Software Construction Assignment Help
PPTX
Computer Science Assignment Help
PPT
Intermediate code generation (Compiler Design)
PPTX
Dag representation of basic blocks
PPTX
Basic Block
PPT
0015.register allocation-graph-coloring
PPTX
Assignment statements
Lecture 12 intermediate code generation
Compiler unit 5
Chapter 6 intermediate code generation
Interm codegen
Intermediate code generation1
Register allocation and assignment
Chapter Eight(1)
Three Address code
Lecture 03 lexical analysis
Chapter Eight(2)
Intermediate code
Intermediate code generation
Software Construction Assignment Help
Computer Science Assignment Help
Intermediate code generation (Compiler Design)
Dag representation of basic blocks
Basic Block
0015.register allocation-graph-coloring
Assignment statements
Ad

Similar to Ch9a (20)

PPTX
Bp150522
PPTX
Code generation
PPT
Instruction Set Architecture (ISA)
PDF
Issues in the design of Code Generator
PPT
COMPILER_DESIGN_CLASS 2.ppt
PPTX
COMPILER_DESIGN_CLASS 1.pptx
PPT
Instruction Set Architecture
PPTX
Compiler Design_Code generation techniques.pptx
PPT
ch 3_The CPU_modified.ppt of central processing unit
PPT
Code generator
PPT
(246431835) instruction set principles (2) (1)
PPT
COA Chapter 3 final edited*Minimum 40 characters required.ppt
PDF
Ec 252 ec-252-l10-instruction sets and addressing modes
PPTX
Presentation2.pptx vtu computer organization and architectural
PPTX
Instruction-Sets-2.pptx
PPT
W8_1: Intro to UoS Educational Processor
PPTX
05 instruction set design and architecture
PPTX
Computer Architecture Lecture slide 2. course se 222
PPTX
CSe_Cumilla Bangladesh_Country CSE CSE213_5.ppt
PPT
IS 139 Lecture 6
Bp150522
Code generation
Instruction Set Architecture (ISA)
Issues in the design of Code Generator
COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 1.pptx
Instruction Set Architecture
Compiler Design_Code generation techniques.pptx
ch 3_The CPU_modified.ppt of central processing unit
Code generator
(246431835) instruction set principles (2) (1)
COA Chapter 3 final edited*Minimum 40 characters required.ppt
Ec 252 ec-252-l10-instruction sets and addressing modes
Presentation2.pptx vtu computer organization and architectural
Instruction-Sets-2.pptx
W8_1: Intro to UoS Educational Processor
05 instruction set design and architecture
Computer Architecture Lecture slide 2. course se 222
CSe_Cumilla Bangladesh_Country CSE CSE213_5.ppt
IS 139 Lecture 6
Ad

More from kinnarshah8888 (13)

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Machine learning based COVID-19 study performance prediction
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
A Presentation on Artificial Intelligence
PDF
KodekX | Application Modernization Development
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
A Presentation on Artificial Intelligence
KodekX | Application Modernization Development
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf

Ch9a

  • 1. Code Generation Part I Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  • 2. Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error Semantic error Intermediate code Code Generator Intermediate code Target program
  • 3. Code Generation Code produced by compiler must be correct Source-to-target program transformation should be semantics preserving Code produced by compiler should be of high quality Effective use of target machine resources Heuristic techniques should be used to generate good but suboptimal code, because generating optimal code is undecidable
  • 4. Target Program Code The back-end code generator of a compiler may generate different forms of code, depending on the requirements: Absolute machine code (executable code) Relocatable machine code (object files for linker) Assembly language (facilitates debugging) Byte code forms for interpreters (e.g. JVM)
  • 5. The 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 , …, R n- 1 Two-address instructions of the form op source , destination
  • 6. The Target Machine: Op-codes and Address Modes 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 dest. ) Address modes Mode Form Address Added 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
  • 7. Instruction Costs Machine is a simple, non-super-scalar processor with fixed instruction costs Realistic machines have deep pipelines, I-cache, D-cache, etc. Define the cost of instruction = 1 + cost( source -mode) + cost( destination -mode)
  • 8. 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
  • 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 ADD #1,a INC a Cost = 6 Cost = 3 Cost = 2 Better Best
  • 10. 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
  • 11. Need for Global Machine-Specific Code Optimizations Suppose we translate three-address code x := y + z to: MOV y ,R0 ADD z ,R0 MOV R0, x Then, we translate a:=b+c d:=a+e to: MOV a,R0 ADD b,R0 MOV R0,a MOV a,R0 ADD e,R0 MOV R0,d Redundant
  • 12. Register Allocation and Assignment Efficient utilization of the limited set of registers is important to generate good code Registers are assigned by Register allocation to select the set of variables that will reside in registers at a point in the code Register assignment to pick the specific register that a variable will reside in Finding an optimal register assignment in general is NP-complete
  • 13. Example t:=a*b t:=t+a t:=t/d MOV a,R1 MUL b,R1 ADD a,R1 DIV d,R1 MOV R1,t t:=a*b t:=t+a t:=t/d MOV a,R0 MOV R0,R1 MUL b,R1 ADD R0,R1 DIV d,R1 MOV R1,t { R1 = t } { R0 = a , R1 = t }
  • 14. Choice of Evaluation Order When instructions are independent, their evaluation order can be changed t1:=a+b t2:=c+d t3:=e*t2 t4:=t1-t3 a+b-(c+d)*e MOV a,R0 ADD b,R0 MOV R0,t1 MOV c,R1 ADD d,R1 MOV e,R0 MUL R1,R0 MOV t1,R1 SUB R0,R1 MOV R1,t4 t2:=c+d t3:=e*t2 t1:=a+b t4:=t1-t3 MOV c,R0 ADD d,R0 MOV e,R1 MUL R0,R1 MOV a,R0 ADD b,R0 SUB R1,R0 MOV R0,t4 reorder
  • 15. Generating Code for Stack Allocation of Activation Records t1 := a + b param t1 param c t2 := call foo,2 … func foo … return t1 100: ADD #16,SP 108: MOV a,R0 116: ADD b,R0 124: MOV R0,4(SP) 132: MOV c,8(SP) 140: MOV #156,*SP 148: GOTO 500 156: MOV 12(SP),R0 164: SUB #16,SP 172: … 500: … 564: MOV R0,12(SP) 572: GOTO *SP Return to caller Store return value Push frame Store a+b Store c Store return address Jump to foo Get return value Remove frame Note: Language and machine dependent Here we assume C-like implementation with SP and no FP