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 29 June 2013 29 June 2013 Overview: Outline 1/42 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 29 June 2013 Overview: Introduction to Compilation 2/42 Implementation Mechanisms Source Program Translator Target Program Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: Introduction to Compilation 2/42 Implementation Mechanisms Source Program Input Data Translator Target Program Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: Introduction to Compilation 2/42 Implementation Mechanisms Source Program Source Program Input Data Translator Interpreter Target Program Machine Machine Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: Introduction to Compilation 3/42 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 29 June 2013 Overview: Introduction to Compilation 3/42 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 29 June 2013 Overview: Introduction to Compilation 3/42 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 29 June 2013 Overview: Introduction to Compilation 3/42 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 29 June 2013 Overview: Introduction to Compilation 4/42 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 29 June 2013 Overview: Introduction to Compilation 5/42 Implementation Mechanisms • Translation = Analysis + Synthesis Interpretation = Analysis + Execution Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: Introduction to Compilation 5/42 Implementation Mechanisms • Translation = Analysis + Synthesis Interpretation = Analysis + Execution Equivalent • Translation Instructions Instructions Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: Introduction to Compilation 5/42 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 29 June 2013 Overview: Introduction to Compilation 6/42 Language Implementation Models Synthesis Compilation Analysis Execution Interpretation Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: Introduction to Compilation 7/42 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 29 June 2013 Overview: An Overview of Compilation Phases 8/42 The Structure of a Simple Compiler Parser Semantic Symtab Scanner Analyser Handler Source Program Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: An Overview of Compilation Phases 8/42 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 29 June 2013 Overview: An Overview of Compilation Phases 8/42 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 29 June 2013 Overview: An Overview of Compilation Phases 9/42 Translation Sequence in Our Compiler: Parsing a=b<10?b:c; Input Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: An Overview of Compilation Phases 9/42 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 29 June 2013 Overview: An Overview of Compilation Phases 10/42 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 29 June 2013 Overview: An Overview of Compilation Phases 10/42 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 29 June 2013 Overview: An Overview of Compilation Phases 11/42 Translation Sequence in Our Compiler: IR Generation 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) Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: An Overview of Compilation Phases 11/42 Translation Sequence in Our Compiler: IR Generation a=b<10?b:c; AsgnStmnt = Input name ; (a,int) ?: (int) Tree List Lhs = E = < name E E : E name name ? (bool) (b,int) (c,int) T0 Not < E < E name name name num 10 b name num (b,int) (10,int) IfGoto Parse Tree Abstract Syntax Tree (with attributes) T0 L0: = Issues: T1 b • Convert to maximal trees which can be Goto implemented without altering control flow L1: Simplifies instruction selection and scheduling, L0: = register allocation etc. T1 c L1: = • Linearise control flow by flattening nested a T1 control constructs Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: An Overview of Compilation Phases 12/42 Translation Sequence in Our Compiler: Instruction Selection a=b<10?b:c; AsgnStmnt = Input name ; (a,int) ?: (int) Tree List Lhs = E = < name E E : E name name ? (bool) (b,int) (c,int) T0 Not < E < E name name name num 10 b name num (b,int) (10,int) IfGoto Parse Tree Abstract Syntax Tree (with attributes) T0 L0: = T1 b Goto L1: L0: = T1 c L1: = a T1 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: An Overview of Compilation Phases 12/42 Translation Sequence in Our Compiler: Instruction Selection a=b<10?b:c; AsgnStmnt = Input name ; (a,int) ?: (int) Tree List Lhs = E = < name E E : E name name ? (bool) (b,int) (c,int) T0 Not < E < E name name name num 10 b name num (b,int) (10,int) IfGoto Parse Tree Abstract Syntax Tree (with attributes) T0 L0: = Instruction List Issues: ← T1 b T0 b • Cover trees with as few T ← T < 10 Goto 0 0 machine instructions as T0 ← ! T0 L1: possible if T0 > 0 goto L0: L0: = ← T1 b • Use temporaries and local T c 1 goto L1: registers L1: = L0: T1 ← c ← a T1 L1: a T1 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: An Overview of Compilation Phases 12/42 Translation Sequence in Our Compiler: Instruction Selection a=b<10?b:c; AsgnStmnt = Input name ; (a,int) ?: (int) Tree List Lhs = E = < name E E : E name name ? (bool) (b,int) (c,int) T0 Not < E < E name name name num 10 b name num (b,int) (10,int) IfGoto Parse Tree Abstract Syntax Tree (with attributes) T0 L0: = Instruction List Issues: ← T1 b T0 b • Cover trees with as few T ← T < 10 Goto 0 0 machine instructions as T0 ← ! T0 L1: possible if T0 > 0 goto L0: L0: = ← T1 b • Use temporaries and local T c 1 goto L1: registers L1: = L0: T1 ← c ← a T1 L1: a T1 Essential Abstractions in GCC GCC Resource Center, IIT Bombay 29 June 2013 Overview: An Overview of Compilation Phases 13/42 Translation Sequence in Our Compiler: Emitting Instructions a=b<10?b:c; AsgnStmnt = Input name ; (a,int) ?: (int) Tree List Lhs = E = < name E E : E name name ? (bool) (b,int) (c,int) T0 Not < E < E name name name num 10 b name num (b,int) (10,int)