SlideShare a Scribd company logo
1 of 36
GCC
RTL & MACHINE DESCRIPTION
By Priyatham Bollimpalli
Little review of basics
What are the stages of a compiler?
Compilation Stage-1:Preprocessing
 Performed by a program called the preprocessor
 Modifies the source code (in RAM) according to
preprocessor directives (preprocessor commands)
embedded in the source code
 Strips comments and white space from the code
 The source code as stored on disk is not modified
Compilation Stage-2:Compilation
 Performed by a program called the compiler
 Translates the preprocessor-modified source code into
object code
 Checks for syntax errors and warnings
 Saves the object code to a disk file, if instructed to do
 If any compiler errors are received, no object code file
will be generated
 An object code file will be generated if only warnings,
not errors, are received
Compilation Stage-3:Linking
 Combines the program object code with other object
code to produce the executable file
 The other object code can come from the Run-Time
Library, other libraries, or object files that you have
created
 Saves the executable code to a disk file. On the Linux
system, that file is called a.out
 If any linker errors are received, no executable file will
be generated
Compiler Phases
• You know about front end and some back end phases
Why Front End?
 Machine Independent
 Can be written in a high level language
 Re-use Oriented Programming
 Lessens Time Required to Generate New
Compilers
 Makes developing new programming languages
simpler
After Front End?
 All language compilers
 Read source code
 Output assembly code
 Language compilers have different front ends
 Each front end parses input and produces an
abstract syntax tree
 AST is converted to a common middle-end format
 GENERIC or GIMPLE
 Next these are converted to RTL (Register Transfer
Language)
“Middle-End”
 GENERIC is an intermediate representation
language used as a "middle-end" while
compiling source code into executable binaries.
 A subset, called GIMPLE, is targeted by all the
front-ends of GCC
 The middle stage of GCC does all the code
analysis and optimization, working
independently of both the compiled language
and the target architecture, starting from the
GENERIC representation and expanding it to
Register Transfer Language.
Why “Middle-End”?
 In transforming the source code to GIMPLE,
complex expressions are split into a three
address code using temporary variables
 This is for simplifying the analysis and
optimization of imperative programs
 In GCC, RTL is generated from the GIMPLE
representation
 Important to know about RTL to get an idea of
how assembly code is generated from RTL with
the help of templates
RTL
 Register transfer language (RTL) : A Scheme-like
language based on virtual registers
 Sometimes, initial RTL is generated with hints
about the target machine
 RTL is refined through many (58) passes. Details at
http://gcc.gnu.org/onlinedocs/gccint/RTL-passes.html
 Final passes use target machine registers and
instructions
 From there, conversion to machine-specific
assembly language is very easy
An Example
 GCC’s RTL is usually written in a form which looks like a
Lisp S-expression. For example:
(set (reg:SI 140)
(plus:SI (reg:SI 138)
(reg:SI 139)))
 It says "add the contents of register 138 to the contents
of register 139 and store the result in register 140".
 The SI specifies the access mode for each registers.
Here it is "SImode", i.e. "access the register as 32-bit
integer"
Example of an RTL Node
(plus:SI (reg:SI 100) (reg:SI 150))
GET_CODE: addition
GET_MODE:
four byte integer
XEXP (x, 0):
first operand: a REG
XEXP (x, 1):
second operand: a REG
Generating Code for If-Then-Else
if (cond1)
…
elseif (cond2)
…
else
…
Call expand_start_tree with tree
for cond1.
Generate code.
Call expand_start_elseif with tree
for cond2.
Generate code.
Call expand_start_else.
Generate code.
Call expand_end_cond.
Generating Code for Loops
 Uses a struct nesting; need not know format, take
structure given and pass back.
 expand_start_loop
 Make a new loop.
 expand_start_loop_continue_elsewhere
 Similar but provide continuation point
 expand_end_loop
 Mark the end of the loop
 expand_{continue,exit}_loop
 Exit or continue specified loop
 Flags are used in an RTL Expressions
Examples of Optimizations
 Many types of optimizations during RTL generation
 Merging of comparisons
a >= 10 && a <= 100 becomes
(unsigned) a - 10 <= 90
 if (cond) x = A; else x = B; into
x = B; if (cond) x = A;
 xor (not X) (not Y) to
(xor X Y)
 Extensive theory and research
Machine Descriptions
 Generation of ASM from RTL
 Depends only on target machine
 A machine description has two parts
 a file of instruction patterns (.md file)
 a C header file of macro definitions (around 500)
 Linked to file md in build directory.
 Written in RTL, represented in LISP-like format.
 Read by various programs to generate .h and .c files
that become part of GCC.
Templates
 Bulk of MD file
 Specified with define_insn
 For simple and direct instructions
 template given is inserted into the insn list
 Some specified with define_expand.One of the
following may happen based on the condition logic
 The condition logic may manually create new insns for the
insn list and invoke DONE.
 For certain named patterns, it may invoke FAIL to tell the
compiler to use an alternate way of performing that task.
 If it invokes neither DONE nor FAIL, the template given in the
pattern is inserted, as if the it were a define_insn
define_insn
(define_insn "addsi3_internal"
[(set (match_operand:SI 0 "register_operand"
"=d,d")(plus:SI (match_operand:SI 1 "reg_or_0_operand"
"dJ,dJ")(match_operand:SI 2 "arith_operand" "d,Q")))]
"!TARGET_MIPS16"
"@
addut%0,%z1,%2
addiut%0,%z1,%2"
[(set_attr "type" "arith")
(set_attr "mode" "SI")] )
Syntax
The basic structure of a define_insn in MD is:
(define_insn
KEY (also called NAME - Optional)
RTL TEMPLATE
C CONDITION
ASM
OPTIONAL ATTRIBUTES SPECIFICATION )
 condition -string (C expression) that is the final test to
decide whether an insn body matches this pattern
Example
Consider a define_insn expression from mips.md file
Here the RTL template is
This can be represented in a tree form as
 (match_operand:m n predicate constraint)
 operand number n of the insn
 predicate is a string that is the name of a function that
accepts two arguments, an expression and a machine
mode.
 constraint controls reloading and the choice of the best
register class to use for a value
 m memory operand is allowed
 r register operand is allowed provided that it is in
a general register
 = write only
 f floating operand
Another Example
 (define_insn "tstsi"
[(set (cc0)
(match_operand:SI 0 "general_operand" "rm"))]
""
{
if (TARGET_68020 || !ADDRESS_REG_P (operands[0]))
return "tstl %0";
return "cmpl #0,%0";
})
 Sets the condition codes based on the value of a general operand.
 The name `tstsi' means "test a SImode value" - when it is necessary to test
such a value, an insn to do so can be constructed using this pattern.
 Based on the kind of operand and the specific type of CPU for which
code is being generated, Output is generated.
 (define_insn "addsi3"
[(set (match_operand:SI 0 "general_operand" "=r,m")
(plus:SI (match_operand:SI 1 "general_operand" "0,0")
(match_operand:SI 2 "general_operand" "g,r")))]
""
"@
addr %2,%0
addm %2,%0")
 What deos it do?
define_expand
 A define_expand can produce more than one RTL
instruction
 Syntax
 Optional name
 RTL Template
 A condition
 Preparatory statements
 The condition logic may manually create new insns for
the insn list, say via emit_insn(), and invoke DONE
Example
Jump Instructions
 The machine description should define a single pattern,
usually a define_expand, which expands to all the
required insns.
 Usually, this would be a comparison insn to set the
condition code and a separate branch insn testing the
condition code and branching or not according to its
value. For many machines, however, separating
compares and branches is limiting
 So define_expand is used in GCC
 Widely used in conditional and looping constructs
Loop Instructions
 Some machines have special jump instructions that can be utilized
to make loops more efficient
 GCC has three special named patterns to support low overhead
looping
 decrement_and_branch_until_zero
 doloop_begin
 doloop_end
Last step
 Once the insn list is generated, various optimization
passes convert, replace, and rearrange the insns in the
insn list.
 For this, the define_split and define_peephole
patterns get used.
(set (cc0) (reg:SI 100)
(set (pc) (if_then_else (eq (cc0) (const_int 0)) (label_ref 18) (pc)))
to
(set (pc) (if_then_else (eq (reg:SI 100) (const_int 0))
(label_ref 18) (pc)))
 Finally we obtain the assembly code!!
Facts
 This is just a tiny whisk of hair on tip of the iceberg. The
GCC documentation is huge!!
Size of GCC
 Machine Descriptions : 421,741 lines
 *.md: 155,244
 *.h: 135,778
 *.c: 126,872
 Makefile insertions: 3,847
 Distributed front ends: 357,744 lines
 Base compiler: 328,297 lines
Facts
 Distributed front ends: 357,744 lines
 C: 23,315
 C++: 97,642
 Chill: 42,225
 F77: 133,121
 Java: 47,284
 Objective-C: 7,973 + 6,880 (library)
 Base compiler: 328,297
 Optimizer: 70,853
 Generation of compiler code: 14,216
 Total size: 1,107,782 lines
References
 http://www.wikipedia.org/
 http://gcc.gnu.org/onlinedocs/gccint/
 http://www.gnat.com/~kenner/gcctut.ppt
 http://www.cse.iitb.ac.in/~uday/
GCC RTL and Machine Description

More Related Content

What's hot (20)

Toolchain
ToolchainToolchain
Toolchain
 
G++ & GCC
G++ & GCCG++ & GCC
G++ & GCC
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction Selection
 
Introduction to the LLVM Compiler System
Introduction to the LLVM  Compiler SystemIntroduction to the LLVM  Compiler System
Introduction to the LLVM Compiler System
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Why Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your ShellWhy Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your Shell
 
Embedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile DevicesEmbedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile Devices
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Introduction to gdb
Introduction to gdbIntroduction to gdb
Introduction to gdb
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Modern C++
Modern C++Modern C++
Modern C++
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
MPI
MPIMPI
MPI
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
The Internals of "Hello World" Program
The Internals of "Hello World" ProgramThe Internals of "Hello World" Program
The Internals of "Hello World" Program
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 

Viewers also liked

Design Thinking and Innovation Course - Day 4 - Synthesis
Design Thinking and Innovation Course - Day 4 - SynthesisDesign Thinking and Innovation Course - Day 4 - Synthesis
Design Thinking and Innovation Course - Day 4 - SynthesisIngo Rauth
 
Connect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low EnergyConnect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low EnergyGabor Paller
 
Fundamentals of FPGA
Fundamentals of FPGAFundamentals of FPGA
Fundamentals of FPGAvelamakuri
 

Viewers also liked (7)

Syntutic
SyntuticSyntutic
Syntutic
 
Design Thinking and Innovation Course - Day 4 - Synthesis
Design Thinking and Innovation Course - Day 4 - SynthesisDesign Thinking and Innovation Course - Day 4 - Synthesis
Design Thinking and Innovation Course - Day 4 - Synthesis
 
Connect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low EnergyConnect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low Energy
 
FPGA Introduction
FPGA IntroductionFPGA Introduction
FPGA Introduction
 
What is FPGA?
What is FPGA?What is FPGA?
What is FPGA?
 
FPGA
FPGAFPGA
FPGA
 
Fundamentals of FPGA
Fundamentals of FPGAFundamentals of FPGA
Fundamentals of FPGA
 

Similar to GCC RTL and Machine Description

1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdfSemsemSameer1
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkAlexey Smirnov
 
Compiler design notes phases of compiler
Compiler design notes phases of compilerCompiler design notes phases of compiler
Compiler design notes phases of compilerovidlivi91
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assemblyAbdul Khan
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docxeugeniadean34240
 
Feature and platform testing with CMake
Feature and platform testing with CMakeFeature and platform testing with CMake
Feature and platform testing with CMakeRichard Thomson
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeDmitri Nesteruk
 
AmiBroker AFL to DLL Conversion
AmiBroker  AFL to DLL ConversionAmiBroker  AFL to DLL Conversion
AmiBroker AFL to DLL Conversionafl2dll
 
Virtual platform
Virtual platformVirtual platform
Virtual platformsean chen
 

Similar to GCC RTL and Machine Description (20)

1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions Framework
 
Compiler design notes phases of compiler
Compiler design notes phases of compilerCompiler design notes phases of compiler
Compiler design notes phases of compiler
 
Embedded C.pptx
Embedded C.pptxEmbedded C.pptx
Embedded C.pptx
 
Unit-2.pptx
Unit-2.pptxUnit-2.pptx
Unit-2.pptx
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assembly
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
 
Feature and platform testing with CMake
Feature and platform testing with CMakeFeature and platform testing with CMake
Feature and platform testing with CMake
 
C tutorials
C tutorialsC tutorials
C tutorials
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
 
Gcc porting
Gcc portingGcc porting
Gcc porting
 
AmiBroker AFL to DLL Conversion
AmiBroker  AFL to DLL ConversionAmiBroker  AFL to DLL Conversion
AmiBroker AFL to DLL Conversion
 
Readme
ReadmeReadme
Readme
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Qe Reference
Qe ReferenceQe Reference
Qe Reference
 
Virtual platform
Virtual platformVirtual platform
Virtual platform
 

More from Priyatham Bollimpalli

Meta Machine Learning: Hyperparameter Optimization
Meta Machine Learning: Hyperparameter OptimizationMeta Machine Learning: Hyperparameter Optimization
Meta Machine Learning: Hyperparameter OptimizationPriyatham Bollimpalli
 
Science and Ethics: The Manhattan Project during World War II
Science and Ethics: The Manhattan Project during World War IIScience and Ethics: The Manhattan Project during World War II
Science and Ethics: The Manhattan Project during World War IIPriyatham Bollimpalli
 
Kernel Descriptors for Visual Recognition
Kernel Descriptors for Visual RecognitionKernel Descriptors for Visual Recognition
Kernel Descriptors for Visual RecognitionPriyatham Bollimpalli
 
Design and Fabrication of 4-bit processor
Design and Fabrication of  4-bit processorDesign and Fabrication of  4-bit processor
Design and Fabrication of 4-bit processorPriyatham Bollimpalli
 
Interface for Finding Close Matches from Translation Memory
Interface for Finding Close Matches from Translation MemoryInterface for Finding Close Matches from Translation Memory
Interface for Finding Close Matches from Translation MemoryPriyatham Bollimpalli
 
The problem of Spatio-Temporal Invariant Points in Videos
The problem of Spatio-Temporal Invariant Points in VideosThe problem of Spatio-Temporal Invariant Points in Videos
The problem of Spatio-Temporal Invariant Points in VideosPriyatham Bollimpalli
 
Literature Survey on Interest Points based Watermarking
Literature Survey on Interest Points based WatermarkingLiterature Survey on Interest Points based Watermarking
Literature Survey on Interest Points based WatermarkingPriyatham Bollimpalli
 

More from Priyatham Bollimpalli (10)

Meta Machine Learning: Hyperparameter Optimization
Meta Machine Learning: Hyperparameter OptimizationMeta Machine Learning: Hyperparameter Optimization
Meta Machine Learning: Hyperparameter Optimization
 
Science and Ethics: The Manhattan Project during World War II
Science and Ethics: The Manhattan Project during World War IIScience and Ethics: The Manhattan Project during World War II
Science and Ethics: The Manhattan Project during World War II
 
Kernel Descriptors for Visual Recognition
Kernel Descriptors for Visual RecognitionKernel Descriptors for Visual Recognition
Kernel Descriptors for Visual Recognition
 
Auction Portal
Auction PortalAuction Portal
Auction Portal
 
IIT JEE Seat Allocation System
IIT JEE Seat Allocation System IIT JEE Seat Allocation System
IIT JEE Seat Allocation System
 
Design and Fabrication of 4-bit processor
Design and Fabrication of  4-bit processorDesign and Fabrication of  4-bit processor
Design and Fabrication of 4-bit processor
 
Library Management System
Library  Management  SystemLibrary  Management  System
Library Management System
 
Interface for Finding Close Matches from Translation Memory
Interface for Finding Close Matches from Translation MemoryInterface for Finding Close Matches from Translation Memory
Interface for Finding Close Matches from Translation Memory
 
The problem of Spatio-Temporal Invariant Points in Videos
The problem of Spatio-Temporal Invariant Points in VideosThe problem of Spatio-Temporal Invariant Points in Videos
The problem of Spatio-Temporal Invariant Points in Videos
 
Literature Survey on Interest Points based Watermarking
Literature Survey on Interest Points based WatermarkingLiterature Survey on Interest Points based Watermarking
Literature Survey on Interest Points based Watermarking
 

Recently uploaded

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 

Recently uploaded (20)

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 

GCC RTL and Machine Description

  • 1. GCC RTL & MACHINE DESCRIPTION By Priyatham Bollimpalli
  • 2. Little review of basics What are the stages of a compiler?
  • 3. Compilation Stage-1:Preprocessing  Performed by a program called the preprocessor  Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code  Strips comments and white space from the code  The source code as stored on disk is not modified
  • 4. Compilation Stage-2:Compilation  Performed by a program called the compiler  Translates the preprocessor-modified source code into object code  Checks for syntax errors and warnings  Saves the object code to a disk file, if instructed to do  If any compiler errors are received, no object code file will be generated  An object code file will be generated if only warnings, not errors, are received
  • 5. Compilation Stage-3:Linking  Combines the program object code with other object code to produce the executable file  The other object code can come from the Run-Time Library, other libraries, or object files that you have created  Saves the executable code to a disk file. On the Linux system, that file is called a.out  If any linker errors are received, no executable file will be generated
  • 6. Compiler Phases • You know about front end and some back end phases
  • 7. Why Front End?  Machine Independent  Can be written in a high level language  Re-use Oriented Programming  Lessens Time Required to Generate New Compilers  Makes developing new programming languages simpler
  • 8. After Front End?  All language compilers  Read source code  Output assembly code  Language compilers have different front ends  Each front end parses input and produces an abstract syntax tree  AST is converted to a common middle-end format  GENERIC or GIMPLE  Next these are converted to RTL (Register Transfer Language)
  • 9. “Middle-End”  GENERIC is an intermediate representation language used as a "middle-end" while compiling source code into executable binaries.  A subset, called GIMPLE, is targeted by all the front-ends of GCC  The middle stage of GCC does all the code analysis and optimization, working independently of both the compiled language and the target architecture, starting from the GENERIC representation and expanding it to Register Transfer Language.
  • 10. Why “Middle-End”?  In transforming the source code to GIMPLE, complex expressions are split into a three address code using temporary variables  This is for simplifying the analysis and optimization of imperative programs  In GCC, RTL is generated from the GIMPLE representation  Important to know about RTL to get an idea of how assembly code is generated from RTL with the help of templates
  • 11. RTL  Register transfer language (RTL) : A Scheme-like language based on virtual registers  Sometimes, initial RTL is generated with hints about the target machine  RTL is refined through many (58) passes. Details at http://gcc.gnu.org/onlinedocs/gccint/RTL-passes.html  Final passes use target machine registers and instructions  From there, conversion to machine-specific assembly language is very easy
  • 12. An Example  GCC’s RTL is usually written in a form which looks like a Lisp S-expression. For example: (set (reg:SI 140) (plus:SI (reg:SI 138) (reg:SI 139)))  It says "add the contents of register 138 to the contents of register 139 and store the result in register 140".  The SI specifies the access mode for each registers. Here it is "SImode", i.e. "access the register as 32-bit integer"
  • 13. Example of an RTL Node (plus:SI (reg:SI 100) (reg:SI 150)) GET_CODE: addition GET_MODE: four byte integer XEXP (x, 0): first operand: a REG XEXP (x, 1): second operand: a REG
  • 14. Generating Code for If-Then-Else if (cond1) … elseif (cond2) … else … Call expand_start_tree with tree for cond1. Generate code. Call expand_start_elseif with tree for cond2. Generate code. Call expand_start_else. Generate code. Call expand_end_cond.
  • 15. Generating Code for Loops  Uses a struct nesting; need not know format, take structure given and pass back.  expand_start_loop  Make a new loop.  expand_start_loop_continue_elsewhere  Similar but provide continuation point  expand_end_loop  Mark the end of the loop  expand_{continue,exit}_loop  Exit or continue specified loop  Flags are used in an RTL Expressions
  • 16. Examples of Optimizations  Many types of optimizations during RTL generation  Merging of comparisons a >= 10 && a <= 100 becomes (unsigned) a - 10 <= 90  if (cond) x = A; else x = B; into x = B; if (cond) x = A;  xor (not X) (not Y) to (xor X Y)  Extensive theory and research
  • 17. Machine Descriptions  Generation of ASM from RTL  Depends only on target machine  A machine description has two parts  a file of instruction patterns (.md file)  a C header file of macro definitions (around 500)  Linked to file md in build directory.  Written in RTL, represented in LISP-like format.  Read by various programs to generate .h and .c files that become part of GCC.
  • 18. Templates  Bulk of MD file  Specified with define_insn  For simple and direct instructions  template given is inserted into the insn list  Some specified with define_expand.One of the following may happen based on the condition logic  The condition logic may manually create new insns for the insn list and invoke DONE.  For certain named patterns, it may invoke FAIL to tell the compiler to use an alternate way of performing that task.  If it invokes neither DONE nor FAIL, the template given in the pattern is inserted, as if the it were a define_insn
  • 19. define_insn (define_insn "addsi3_internal" [(set (match_operand:SI 0 "register_operand" "=d,d")(plus:SI (match_operand:SI 1 "reg_or_0_operand" "dJ,dJ")(match_operand:SI 2 "arith_operand" "d,Q")))] "!TARGET_MIPS16" "@ addut%0,%z1,%2 addiut%0,%z1,%2" [(set_attr "type" "arith") (set_attr "mode" "SI")] )
  • 20. Syntax The basic structure of a define_insn in MD is: (define_insn KEY (also called NAME - Optional) RTL TEMPLATE C CONDITION ASM OPTIONAL ATTRIBUTES SPECIFICATION )  condition -string (C expression) that is the final test to decide whether an insn body matches this pattern
  • 21.
  • 22. Example Consider a define_insn expression from mips.md file
  • 23. Here the RTL template is
  • 24. This can be represented in a tree form as
  • 25.  (match_operand:m n predicate constraint)  operand number n of the insn  predicate is a string that is the name of a function that accepts two arguments, an expression and a machine mode.  constraint controls reloading and the choice of the best register class to use for a value  m memory operand is allowed  r register operand is allowed provided that it is in a general register  = write only  f floating operand
  • 26. Another Example  (define_insn "tstsi" [(set (cc0) (match_operand:SI 0 "general_operand" "rm"))] "" { if (TARGET_68020 || !ADDRESS_REG_P (operands[0])) return "tstl %0"; return "cmpl #0,%0"; })  Sets the condition codes based on the value of a general operand.  The name `tstsi' means "test a SImode value" - when it is necessary to test such a value, an insn to do so can be constructed using this pattern.  Based on the kind of operand and the specific type of CPU for which code is being generated, Output is generated.
  • 27.  (define_insn "addsi3" [(set (match_operand:SI 0 "general_operand" "=r,m") (plus:SI (match_operand:SI 1 "general_operand" "0,0") (match_operand:SI 2 "general_operand" "g,r")))] "" "@ addr %2,%0 addm %2,%0")  What deos it do?
  • 28. define_expand  A define_expand can produce more than one RTL instruction  Syntax  Optional name  RTL Template  A condition  Preparatory statements  The condition logic may manually create new insns for the insn list, say via emit_insn(), and invoke DONE
  • 30. Jump Instructions  The machine description should define a single pattern, usually a define_expand, which expands to all the required insns.  Usually, this would be a comparison insn to set the condition code and a separate branch insn testing the condition code and branching or not according to its value. For many machines, however, separating compares and branches is limiting  So define_expand is used in GCC  Widely used in conditional and looping constructs
  • 31. Loop Instructions  Some machines have special jump instructions that can be utilized to make loops more efficient  GCC has three special named patterns to support low overhead looping  decrement_and_branch_until_zero  doloop_begin  doloop_end
  • 32. Last step  Once the insn list is generated, various optimization passes convert, replace, and rearrange the insns in the insn list.  For this, the define_split and define_peephole patterns get used. (set (cc0) (reg:SI 100) (set (pc) (if_then_else (eq (cc0) (const_int 0)) (label_ref 18) (pc))) to (set (pc) (if_then_else (eq (reg:SI 100) (const_int 0)) (label_ref 18) (pc)))  Finally we obtain the assembly code!!
  • 33. Facts  This is just a tiny whisk of hair on tip of the iceberg. The GCC documentation is huge!! Size of GCC  Machine Descriptions : 421,741 lines  *.md: 155,244  *.h: 135,778  *.c: 126,872  Makefile insertions: 3,847  Distributed front ends: 357,744 lines  Base compiler: 328,297 lines
  • 34. Facts  Distributed front ends: 357,744 lines  C: 23,315  C++: 97,642  Chill: 42,225  F77: 133,121  Java: 47,284  Objective-C: 7,973 + 6,880 (library)  Base compiler: 328,297  Optimizer: 70,853  Generation of compiler code: 14,216  Total size: 1,107,782 lines
  • 35. References  http://www.wikipedia.org/  http://gcc.gnu.org/onlinedocs/gccint/  http://www.gnat.com/~kenner/gcctut.ppt  http://www.cse.iitb.ac.in/~uday/