
CS 715 Gnu Compiler Collection: Outline 1/55 Outline CS 715: The Design and Implementation of Gnu Compiler Generation Framework Uday Khedker • An Overview of Compilation Introduction, compilation sequence, compilation models GCC Resource Center, • Department of Computer Science and Engineering, GCC: The Great Compiler Challenge Indian Institute of Technology, Bombay Difficulties in understanding GCC • Meeting the GCC Challenge: CS 715 The course plan January 2011 Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 2/55 Binding Nothing is known except the problem Overall strategy, algorithm, data structures etc. Functions, variables, their types etc. Part 1 No.of Machine instructions, registers etc. unbound Introduction to Compilation objects Addresses of functions, external data etc. Actual addresses of code and data Values of variables Conceptualisation Coding Compiling Linking Loading Execution Time Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 3/55 CS 715 Gnu Compiler Collection: Introduction to Compilation 4/55 Implementation Mechanisms Implementation Mechanisms as “Bridges” • “Gap” between the “levels” of program specification and execution State : Variables Source Program Source Program Program Specification Operations: Expressions, Input Data Control Flow Translator Interpreter Translation Interpretation Target Program Machine State : Memory, Machine Registers Machine Operations: Machine Instructions Uday Khedker GRC, IIT Bombay Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 5/55 CS 715 Gnu Compiler Collection: Introduction to Compilation 5/55 High and Low Level Abstractions High and Low Level Abstractions Condition Input C statement Input C statement False Part a = b<10?b:c; a = b<10?b:c; True Part Spim Assembly Equivalent Spim Assembly Equivalent lw $t0,4($fp) ; t0<-b # Is b smaller lw $t0,4($fp) ; t0<-b # Is b smaller slti $t0, $t0, 10 ; t0 <- t0 < 10 # than 10? slti $t0, $t0, 10 ; t0 <- t0 < 10 # than 10? not $t0,$t0 ; t0<-!t0 not $t0,$t0 ; t0<-!t0 bgtz $t0, L0: ; if t0>=0 goto L0 bgtz $t0, L0: ; if t0>=0 goto L0 lw $t0,4($fp) ; t0<-b # YES lw $t0,4($fp) ; t0<-b # YES b L1: ; gotoL1 b L1: ; gotoL1 L0: lw $t0, 8($fp) ;L0: t0 <- c # NO L0: lw $t0, 8($fp) ;L0: t0 <- c # NO L1: sw 0($fp), $t0 ;L1: a <- t0 L1: sw 0($fp), $t0 ;L1: a <- t0 Uday Khedker GRC, IIT Bombay Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 5/55 CS 715 Gnu Compiler Collection: Introduction to Compilation 6/55 High and Low Level Abstractions Implementation Mechanisms NOT Condition Conditional jump Fall through • Translation = Analysis + Synthesis True Part Input C statement Interpretation = Analysis + Execution a = b<10?b:c; False Part Equivalent Spim Assembly Equivalent • Translation Instructions lw $t0,4($fp) ; t0<-b # Is b smaller Instructions slti $t0, $t0, 10 ; t0 <- t0 < 10 # than 10? not $t0,$t0 ; t0<-!t0 bgtz $t0, L0: ; if t0>=0 goto L0 Actions Implied lw $t0,4($fp) ; t0<-b # YES Interpretation Instructions b L1: ; gotoL1 by Instructions L0: lw $t0, 8($fp) ;L0: t0 <- c # NO L1: sw 0($fp), $t0 ;L1: a <- t0 Uday Khedker GRC, IIT Bombay Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 7/55 CS 715 Gnu Compiler Collection: Introduction to Compilation 8/55 Language Implementation Models Language Processor Models Back Synthesis Compilation End C,C++ Front Optimizer Analysis End Fall through Conditional jump Virtual Java, C# Execution Interpretation Machine Uday Khedker GRC, IIT Bombay Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 9/55 CS 715 Gnu Compiler Collection: Introduction to Compilation 10/55 Typical Front Ends Typical Back Ends AST or Linear IR Source Parser + Symbol Table m/c Ind. Program IR Register AST Allocator Parse Tokens Tree m/c Ind. m/c Code m/c m/c Dep.Instruction Ind. Dep. Semantic Optimizer IR Generator IR OptimizerScheduler Scanner Analyzer − Compile time − Instruction Selection Peephole evaluations − Local Reg Allocation Optimizer − Eliminating − Choice of Order of Symtab Error redundant Evaluation Handler Handler computations Assembly Code Uday Khedker GRC, IIT Bombay Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: An Overview of Compilation Phases 11/55 The Structure of a Simple Compiler FrontEnd BackEnd Part 2 Instruction Assembly An Overview of Compilation Phases Parser AST Selector Insn Emitter Assembly Semantic Symtab Register Program Scanner Analyser Handler Allocator Source Program Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: An Overview of Compilation Phases 12/55 CS 715 Gnu Compiler Collection: An Overview of Compilation Phases 13/55 Translation Sequence in Our Compiler: Parsing Translation Sequence in Our Compiler: Semantic Analysis a=b<10?b:c; AsgnStmnt a=b<10?b:c; AsgnStmnt = Input Input name ; ; ?: (int) Lhs = E Lhs = E (a,int) < name E E : E name E E : E name name ? ? (bool) (b,int) (c,int) E < E name name E < E name name name num name num name num (b,int) (10,int) Parse Tree Parse Tree Abstract Syntax Tree (with attributes) Issues: • Grammar rules, terminals, non-terminals Issues: • Order of application of grammar rules • Symbol tables eg. is it (a = b<10?) followed by (b:c)? Have variables been declared? What are their types? What is their scope? • Values of terminal symbols • eg. string “10” vs. integer number 10. Type consistency of operators and operands The result of computing b<10? is bool and not int Uday Khedker GRC, IIT Bombay Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: An Overview of Compilation Phases 14/55 CS 715 Gnu Compiler Collection: An Overview of Compilation Phases 15/55 Translation Sequence in Our Compiler: IR Generation Translation Sequence in Our Compiler: Instruction Selection a=b<10?b:c; AsgnStmnt = a=b<10?b:c; AsgnStmnt = Input name Input name ; (a,int) ?: (int) ; (a,int) ?: (int) Tree List Lhs = E Tree List Lhs = E = < = < name E ? E : E name name name E ? E : E name name < (bool) (b,int) (c,int) < (bool) (b,int) (c,int) T0 T0 E E E E 10 < name name 10 < name name b name num b name num IfGoto IfGoto name num (b,int) (10,int) name num (b,int) (10,int) Not L0: Parse Tree Abstract Syntax Tree Not L0: Parse Tree Abstract Syntax Tree (with attributes) (with attributes) T0 T0 = Issues: = Instruction List Issues: T T ← 1 b • 1 b T0 b • Cover trees with as few Goto Convert to maximal trees which can be Goto T ← T < 10 0 0 machine instructions as implemented without altering control flow T0 ← ! T0 L1: L1: possible Simplifies instruction selection and scheduling, if T0 > 0 goto L0: L0: = L0: = ← register allocation etc. T1 b • Use temporaries and local T1 c T1 c goto L1: = • = registers L1: Linearise control flow by flattening nested L1: L0: T1 ← c ← a T1 control constructs a T1 L1: a T1 Uday Khedker GRC, IIT Bombay Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: An Overview of Compilation Phases 16/55 Translation Sequence in Our Compiler: Emitting Instructions a=b<10?b:c; AsgnStmnt = Input name Issues: ; (a,int) ?: (int) Tree List Lhs = E = • Offsets of variables in the < Part 3 name E ? E : E name name < stack frame (bool) (b,int) (c,int) T0 E E 10 • < name name b Actual register numbers name num IfGoto Compilation Models, nameand num assembly mnemonics (b,int) (10,int) Not L0: • Parse Tree Abstract Syntax Tree Code to construct and (with attributes) Instruction Selection, and T0 discard activation records = Instruction List Assembly Code Retargetability T1 b T0 ← b lw $t0, 4($fp) Goto T0 ← T0 < 10 slti $t0, $t0, 10 T ← ! T not $t0, $t0 L1: 0 0 if T > 0 goto L0: bgtz $t0, L0: L0: = 0 T1 ← b lw $t0, 4($fp) T1 c goto L1: b L1: = L1: L0: T1 ← c L0: lw $t0, 8($fp) ← a T1 L1: a T1 L1: sw 0($fp), $t0 Uday Khedker GRC, IIT Bombay CSGnu 715 Compiler Collection: Compilation Models, Instruction Selection, and Retargetability 17/55 CSGnu 715 Compiler Collection: Compilation Models, Instruction Selection, and Retargetability 18/55 Compilation Models Retargetability in Aho Ullman Model Aho Ullman Davidson Fraser Model Model Aho Ullman: Instruction selection Front End Front End Input Source Program Front End • over optimized IR using Instruction selection AST • over optimized IR using AST • intelligent tree tiling based AST algorithms • intelligent tree tiling based algorithms Expander Optimizer Optimizer Key idea in retargetability: Davidson Fraser: Instruction selection register transfers m/c indep. IR • Machine independent IR is expressed in the m/c indep. IR • over AST using form of trees • Optimizer • simple full tree matching based Code Machine instructions are described in the Code algorithms that generate Generator form of trees register transfers Generator • naive code which is • Trees in the IR are tiled using the instruction ◮ machine dependent, and is Recognizer Target Program trees Target Program ◮ optimized subsequently Target Program Uday Khedker GRC, IIT Bombay Uday Khedker GRC, IIT Bombay CSGnu 715 Compiler Collection: Compilation Models, Instruction Selection, and Retargetability 19/55 CSGnu 715 Compiler Collection: Compilation Models, Instruction Selection, and Retargetability 20/55 Retargetability in Davidson Fraser Model Full Tree Matching (Davidson Fraser Model) Front End Instructions are viewed as independent non-composable rules Instruction selection AST • over AST using Machine Instructions Subject Tree (IR) Modified Trees • simple full tree matching based algorithms Expander = that generate
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages18 Page
-
File Size-