140 likes | 488 Views
An Overview to Compiler Design. Outline. An Overview of Compiler Structure Front End Middle End Back End. Reading. Slides and Lecture Notes Aho,Lam,Sethi,Ullman: Chapter 1.4 ~ 1.5 Chapter 2.1 ~ 2.7 Chapter 3.1 ~ 3.5 Chapter 4.1 ~ 4.5.
E N D
An Overview toCompiler Design \course\cpeg421-08s\Topic-1a.ppt
Outline • An Overview of Compiler Structure • Front End • Middle End • Back End \course\cpeg421-08s\Topic-1a.ppt
Reading Slides and Lecture Notes Aho,Lam,Sethi,Ullman: Chapter 1.4 ~ 1.5 Chapter 2.1 ~ 2.7 Chapter 3.1 ~ 3.5 Chapter 4.1 ~ 4.5 \course\cpeg421-08s\Topic-1a.ppt
A Review on CompilerStructure/Design • Overall structure • Front-end: lexical and syntax analysis • Middle-end: machine independent code analysis and (scalar, and sometimes loop nest ) optimization • Back-end: Machine dependent code analysis and optimization \course\cpeg421-08s\Topic-1a.ppt
int foo( ) { int x; return (x + x) ; } Motivation for Compiler optimization COMPILER Source Program Machine Code Input program example: \course\cpeg421-08s\Topic-1a.ppt
foo: sub $sp, $sp, 8 sw $fp, 8($sp) add $fp, $sp, 8 sw $ra, -4($fp) add $t0, $a0 $a0 move $v0, $t0 lw $ra, -4($fp) lw $fp, 0($fp) add $sp, $sp, 8 jr $ra Output Assembly Code (non-optimized) \course\cpeg421-08s\Topic-1a.ppt
foo: sub $sp, $sp, 8 % Push stack frame sw $fp, 8($sp) % Save old frame pointer add $fp, $sp, 8 % Set new frame pointer sw $ra, -4($fp) % Save return address add $t0, $a0 $a0 % Addition move $v0, $t0 % Copy return value lw $ra, -4($fp) % Restore return address lw $fp, 0($fp) % Restore frame pointer add $sp, $sp, 8 % Pop stack frame jr $ra % Jump to return address Output Assembly Code (non-optimized) \course\cpeg421-08s\Topic-1a.ppt
Output Assembly Code -- Revisited (non-optimized) foo: sub $sp, $sp, 8 % Push stack frame (for what?) sw $fp, 8($sp) % Save old frame pointer (for what?) add $fp, $sp, 8 % Set new frame pointer sw $ra, -4($fp) % Save return address (for what?) add $t0, $a0, $a0 % Addition move $v0, $t0 % Copy return value lw $ra, -4($fp) % Restore return address lw $fp, 0($fp) % Restore frame pointer add $sp, $sp, 8 % Pop stack frame jr $ra % Jump to return address \course\cpeg421-08s\Topic-1a.ppt
foo: add $v0, $a0, $a0 % Set result jr $ra % Jump to return address Output Assembly Code(optimized) \course\cpeg421-08s\Topic-1a.ppt
Runtime Memory Organization frame-1 code for procedure 1 Entry point for procedure 1 Space for arguments (parameters) frame-2 code area Space for bookkeeping Information, including Return address frame-3 code for procedure 2 global/static area Entry point for procedure 2 … stack Space in local data … free space Space for local temporaries code for procedure n Runtime stack Entry point for procedure n A stack frame heap Code memory Address space \course\cpeg421-08s\Topic-1a.ppt
Phases of a Compiler Source program Intermediate-code Generator Lexical Analyzer (Scanner) Non-optimized Intermediate Code Tokens Intermediate-code Optimizer Syntax Analyzer (Parser) Optimized Intermediate Code Parsetree Target-code Generator/Opt Semantic Analyzer Target machine code Abstract Syntax Tree w/ Attributes \course\cpeg421-08s\Topic-1a.ppt
String of characters String of characters String of tokens String of tokens Parse tree Parse tree Abstract Syntax tree Abstract syntax tree Medium-level intermediate code Low-level intermediate code Medium-level intermediate code Low-level intermediate code Low-level intermediate code Relocatable object module or runnable machine code Relocatable object module or runnable machine code Low-level Model Mixed-level Model Lexical analyzer Lexical analyzer Parser Parser Semantic analyzer Semantic analyzer Intermediate-code generator Translator Optimizer Optimizer Code generator Final assembly Postpass optimizer Two models of compiler structures (Muchnick, pp. 08) \course\cpeg421-08s\Topic-1a.ppt
A Good Compiler Infrastructure Needed – A modern View Front end Interprocedural Analysis and Optimization Good IR Loop Nest Optimization and Parallelization Global (Scalar) Optimization Middle-End Backend Code Generation \course\cpeg421-08s\Topic-1a.ppt