CS 715: the Design and Implementation of Gnu Compiler Generation Framework
Total Page:16
File Type:pdf, Size:1020Kb
CS 715: The Design and Implementation of Gnu Compiler Generation Framework Uday Khedker (www.cse.iitb.ac.in/˜uday) GCC Resource Center, Department of Computer Science and Engineering, Indian Institute of Technology, Bombay January 2012 CS 715 Gnu Compiler Collection: Outline 1/50 Outline • An Overview of Compilation Introduction, compilation sequence, compilation models • GCC: The Great Compiler Challenge Difficulties in understanding GCC • Meeting the GCC Challenge: CS 715 The course plan Uday Khedker GRC, IIT Bombay Part 1 Introduction to Compilation CS 715 Gnu Compiler Collection: Introduction to Compilation 2/50 Binding Nothing is known except the problem No.of unbound objects Time Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 2/50 Binding Overall strategy, algorithm, data structures etc. No.of unbound objects Conceptualisation Time Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 2/50 Binding Functions, variables, their types etc. No.of unbound objects Conceptualisation Coding Time Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 2/50 Binding No.of Machine instructions, registers etc. unbound objects Conceptualisation Coding Compiling Time Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 2/50 Binding No.of unbound objects Addresses of functions, external data etc. Conceptualisation Coding Compiling Linking Time Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 2/50 Binding No.of unbound objects Actual addresses of code and data Conceptualisation Coding Compiling Linking Loading Time Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 2/50 Binding No.of unbound objects Values of variables Conceptualisation Coding Compiling Linking Loading Execution Time Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 3/50 Implementation Mechanisms Source Program Translator Target Program Machine Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 3/50 Implementation Mechanisms Source Program Input Data Translator Target Program Machine Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 3/50 Implementation Mechanisms Source Program Source Program Input Data Translator Interpreter Target Program Machine Machine Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 4/50 Implementation Mechanisms as “Bridges” • “Gap” between the “levels” of program specification and execution Program Specification Machine Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 4/50 Implementation Mechanisms as “Bridges” • “Gap” between the “levels” of program specification and execution Program Specification Translation Machine Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 4/50 Implementation Mechanisms as “Bridges” • “Gap” between the “levels” of program specification and execution Program Specification Translation Interpretation Machine Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 4/50 Implementation Mechanisms as “Bridges” • “Gap” between the “levels” of program specification and execution State : Variables Program Specification Operations: Expressions, Control Flow Translation Interpretation State : Memory, Registers Machine Operations: Machine Instructions Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 5/50 High and Low Level Abstractions Input C statement a = b<10?b:c; Spim Assembly Equivalent lw $t0,4($fp) ; t0<-b # Is b smaller slti $t0, $t0, 10 ; t0 <- t0 < 10 # than 10? not $t0,$t0 ; t0<-!t0 bgtz $t0, L0: ; if t0>=0 goto L0 lw $t0,4($fp) ; t0<-b # YES b L1: ; gotoL1 L0: lw $t0, 8($fp) ;L0: t0 <- c # NO L1: sw 0($fp), $t0 ;L1: a <- t0 Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 5/50 High and Low Level Abstractions Condition Conditional jump Fall through Input C statement False Part a = b<10?b:c; True Part Spim Assembly Equivalent lw $t0,4($fp) ; t0<-b # Is b smaller slti $t0, $t0, 10 ; t0 <- t0 < 10 # than 10? not $t0,$t0 ; t0<-!t0 bgtz $t0, L0: ; if t0>=0 goto L0 lw $t0,4($fp) ; t0<-b # YES b L1: ; gotoL1 L0: lw $t0, 8($fp) ;L0: t0 <- c # NO L1: sw 0($fp), $t0 ;L1: a <- t0 Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 5/50 High and Low Level Abstractions NOT Condition Input C statement True Part a = b<10?b:c; False Part Spim Assembly Equivalent lw $t0,4($fp) ; t0<-b # Is b smaller slti $t0, $t0, 10 ; t0 <- t0 < 10 # than 10? not $t0,$t0 ; t0<-!t0 bgtz $t0, L0: ; if t0>=0 goto L0 lw $t0,4($fp) ; t0<-b # YES b L1: ; gotoL1 L0: lw $t0, 8($fp) ;L0: t0 <- c # NO L1: sw 0($fp), $t0 ;L1: a <- t0 Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 5/50 High and Low Level Abstractions NOT Condition Conditional jump Fall through Input C statement True Part a = b<10?b:c; False Part Spim Assembly Equivalent lw $t0,4($fp) ; t0<-b # Is b smaller slti $t0, $t0, 10 ; t0 <- t0 < 10 # than 10? not $t0,$t0 ; t0<-!t0 bgtz $t0, L0: ; if t0>=0 goto L0 lw $t0,4($fp) ; t0<-b # YES b L1: ; gotoL1 L0: lw $t0, 8($fp) ;L0: t0 <- c # NO L1: sw 0($fp), $t0 ;L1: a <- t0 Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 6/50 Implementation Mechanisms • Translation = Analysis + Synthesis Interpretation = Analysis + Execution Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 6/50 Implementation Mechanisms • Translation = Analysis + Synthesis Interpretation = Analysis + Execution Equivalent • Translation Instructions Instructions Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 6/50 Implementation Mechanisms • Translation = Analysis + Synthesis Interpretation = Analysis + Execution Equivalent • Translation Instructions Instructions Actions Implied Interpretation Instructions by Instructions Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 7/50 Language Implementation Models Synthesis Compilation Analysis Execution Interpretation Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 8/50 Language Processor Models Back End C,C++ Front Optimizer End Virtual Java, C# Machine Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 9/50 Typical Front Ends Parser Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 9/50 Typical Front Ends Source Parser Program Tokens Scanner Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 9/50 Typical Front Ends AST or Linear IR Source Parser + Program Symbol Table AST Parse Tokens Tree Semantic Scanner Analyzer Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 9/50 Typical Front Ends AST or Linear IR Source Parser + Program Symbol Table AST Parse Tokens Tree Semantic Scanner Analyzer Symtab Error Handler Handler Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 10/50 Typical Back Ends m/c Ind. IR m/c Ind. m/c Ind. Optimizer IR − Compile time evaluations − Eliminating redundant computations Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 10/50 Typical Back Ends m/c Ind. IR m/c Ind. m/c Code m/c Ind. Dep. Optimizer IR Generator IR − Compile time − Instruction Selection evaluations − Local Reg Allocation − Eliminating − Choice of Order of redundant Evaluation computations Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 10/50 Typical Back Ends m/c Ind. IR m/c Ind. m/c Code m/c m/c Dep. Ind. Dep. Optimizer IR Generator IR Optimizer − Compile time − Instruction Selection evaluations − Local Reg Allocation − Eliminating − Choice of Order of redundant Evaluation computations Assembly Code Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: Introduction to Compilation 10/50 Typical Back Ends m/c Ind. IR Register Allocator m/c Ind. m/c Code m/c Instruction Ind. Dep. Optimizer IR Generator IR Scheduler − Compile time − Instruction Selection Peephole evaluations − Local Reg Allocation Optimizer − Eliminating − Choice of Order of redundant Evaluation computations Assembly Code Uday Khedker GRC, IIT Bombay Part 2 An Overview of Compilation Phases CS 715 Gnu Compiler Collection: An Overview of Compilation Phases 11/50 The Structure of a Simple Compiler Parser Semantic Symtab Scanner Analyser Handler Source Program Uday Khedker GRC, IIT Bombay CS 715 Gnu Compiler Collection: An Overview of Compilation Phases 11/50 The Structure of a Simple Compiler Instruction Assembly 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 11/50 The Structure of a Simple Compiler FrontEnd BackEnd Instruction Assembly 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/50 Translation Sequence in Our Compiler: Parsing a=b<10?b:c; Input