An Overview of Compilation and GCC
Total Page:16
File Type:pdf, Size:1020Kb
Workshop on Essential Abstractions in GCC An Overview of Compilation and GCC GCC Resource Center (www.cse.iitb.ac.in/grc) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay 30 June 2011 30 June 2011 Overview: Outline 1/43 Outline • Introduction to Compilation • An Overview of Compilation Phases • An Overview of GCC Essential Abstractions in GCC GCC Resource Center, IIT Bombay Part 1 Introduction to Compilation 30 June 2011 Overview: Introduction to Compilation 2/43 Binding Nothing is known except the problem No.of unbound objects Time Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 2/43 Binding Overall strategy, algorithm, data structures etc. No.of unbound objects Conceptualisation Time Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 2/43 Binding Functions, variables, their types etc. No.of unbound objects Conceptualisation Coding Time Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 2/43 Binding No.of Machine instructions, registers etc. unbound objects Conceptualisation Coding Compiling Time Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 2/43 Binding No.of unbound objects Addresses of functions, external data etc. Conceptualisation Coding Compiling Linking Time Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 2/43 Binding No.of unbound objects Actual addresses of code and data Conceptualisation Coding Compiling Linking Loading Time Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 2/43 Binding No.of unbound objects Values of variables Conceptualisation Coding Compiling Linking Loading Execution Time Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 3/43 Implementation Mechanisms Source Program Translator Target Program Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 3/43 Implementation Mechanisms Source Program Input Data Translator Target Program Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 3/43 Implementation Mechanisms Source Program Source Program Input Data Translator Interpreter Target Program Machine Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 4/43 Implementation Mechanisms as “Bridges” • “Gap” between the “levels” of program specification and execution Program Specification Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 4/43 Implementation Mechanisms as “Bridges” • “Gap” between the “levels” of program specification and execution Program Specification Translation Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 4/43 Implementation Mechanisms as “Bridges” • “Gap” between the “levels” of program specification and execution Program Specification Translation Interpretation Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 4/43 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 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 5/43 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 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 5/43 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 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 5/43 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 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 5/43 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 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 6/43 Implementation Mechanisms • Translation = Analysis + Synthesis Interpretation = Analysis + Execution Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 6/43 Implementation Mechanisms • Translation = Analysis + Synthesis Interpretation = Analysis + Execution Equivalent • Translation Instructions Instructions Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 6/43 Implementation Mechanisms • Translation = Analysis + Synthesis Interpretation = Analysis + Execution Equivalent • Translation Instructions Instructions Actions Implied Interpretation Instructions by Instructions Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 7/43 Language Implementation Models Synthesis Compilation Analysis Execution Interpretation Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: Introduction to Compilation 8/43 Language Processor Models Back End C,C++ Front Optimizer End Virtual Java, C# Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay Part 2 An Overview of Compilation Phases 30 June 2011 Overview: An Overview of Compilation Phases 9/43 The Structure of a Simple Compiler Parser Semantic Symtab Scanner Analyser Handler Source Program Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: An Overview of Compilation Phases 9/43 The Structure of a Simple Compiler Instruction Assembly Parser AST Selector Insn Emitter Assembly Semantic Symtab Register Program Scanner Analyser Handler Allocator Source Program Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: An Overview of Compilation Phases 9/43 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 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: An Overview of Compilation Phases 10/43 Translation Sequence in Our Compiler: Parsing a=b<10?b:c; Input Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: An Overview of Compilation Phases 10/43 Translation Sequence in Our Compiler: Parsing a=b<10?b:c; AsgnStmnt Input ; Lhs = E name E ? E : E E < E name name name num Parse Tree Issues: • Grammar rules, terminals, non-terminals • Order of application of grammar rules eg. is it (a = b<10?) followed by (b:c)? • Values of terminal symbols eg. string “10” vs. integer number 10. Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: An Overview of Compilation Phases 11/43 Translation Sequence in Our Compiler: Semantic Analysis a=b<10?b:c; AsgnStmnt Input ; Lhs = E name E ? E : E E < E name name name num Parse Tree Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: An Overview of Compilation Phases 11/43 Translation Sequence in Our Compiler: Semantic Analysis a=b<10?b:c; AsgnStmnt = Input name ; ?: (int) Lhs = E (a,int) < name E E : E name name ? (bool) (b,int) (c,int) E < E name name name num name num (b,int) (10,int) Parse Tree Abstract Syntax Tree (with attributes) Issues: • Symbol tables Have variables been declared? What are their types? What is their scope? • Type consistency of operators and operands The result of computing b<10? is bool and not int Essential Abstractions in GCC GCC Resource Center, IIT Bombay 30 June 2011 Overview: An Overview of Compilation Phases 12/43 Translation Sequence in Our Compiler: IR Generation a=b<10?b:c; AsgnStmnt = Input name ; ?: