![Page 1: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/1.jpg)
G++ Internals
Dodji Seketeli <[email protected]>
Eigen Summit - February 2010 - Paris, France
Dodji Seketeli <[email protected]> - G++ Internals 1/25
![Page 2: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/2.jpg)
Plan of the talk
Source code
Compiler pipeline
Internal representations
Class analysis process
Class template representation
Template instantiation process
Questions
Dodji Seketeli <[email protected]> - G++ Internals 2/25
![Page 3: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/3.jpg)
Outline
Source code
Compiler pipeline
Internal representations
Class analysis process
Class template representation
Template instantiation process
Questions
Dodji Seketeli <[email protected]> - G++ Internals 3/25
![Page 4: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/4.jpg)
Directories
Top Directory Directory gcc/
gcc/libada/libcpp/libgcc/libgfortran/libgomp/libiberty/libjava/libobjc/libstdc++-v3/
<root>/ Code ofcompilersare here
Code of C++standard library
ada/cp/doc/fortran/java/objc/objcp/
<root/gcc>/
Code ofG++is here
Dodji Seketeli <[email protected]> - G++ Internals 4/25
![Page 5: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/5.jpg)
Some Numbers
I GCC is more than 2,3 millions of LOC
I G++ specific lines of code is around 84 KLOC
I GCC full testsuite is around 720 KLOC
I G++ testsuite is around 90 KLOC
Dodji Seketeli <[email protected]> - G++ Internals 5/25
![Page 6: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/6.jpg)
Outline
Source code
Compiler pipeline
Internal representations
Class analysis process
Class template representation
Template instantiation process
Questions
Dodji Seketeli <[email protected]> - G++ Internals 6/25
![Page 7: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/7.jpg)
Pipeline overview
I Front-EndsI Have own
representationI Emit GENERIC
I Middle-End works onGIMPLE
I Back-End works on RTL
C++
GENERIC
GIMPLE
RTL
Assembly
C Front ends
Interprocedural analysis
SSAOptimizers
RTLOptimizers
MiddleEnd
BackEnd
Dodji Seketeli <[email protected]> - G++ Internals 7/25
![Page 8: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/8.jpg)
Detailed compilation process
main(main.c)
toplev_main(toplev.c)
do_compile(toplev.c)
compile_file(toplev.c)
lang_hook.parse_file(hook implemented
by front ends)
c_parse_file(parser.c)
cp_parser_translation_unit(parser.c)
lang_hook.write_final_globals(hook implemented
by front ends)
cp_write_global_declarations(decl2.c)
cgraph_finalize_compilation_unit(cgraphunit.c)
To be replaced by FEs
Parses compiler cmd lineInitializes backend
Analyzes cmd line flags
Initializes cgraph
cgraph_analyze_functions(cgraphunit.c)
cgraph_optimize(cgraphunit.c)
parsing and semantic analysistransforms every function into GENERIC
Builds a callgraphgimplifies reachable functions
Dodji Seketeli <[email protected]> - G++ Internals 8/25
![Page 9: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/9.jpg)
Outline
Source code
Compiler pipeline
Internal representations
Class analysis process
Class template representation
Template instantiation process
Questions
Dodji Seketeli <[email protected]> - G++ Internals 9/25
![Page 10: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/10.jpg)
GENERIC Representation
I Can represent a full translation unit
I Based on treesTM
I A tree is a pointer type
I Can point to different kind of nodes
I Many macros available to manipulate trees
t r e e f u n c = p a r s e f u n c t i o n ( ) ;i f (TREE CODE ( f u n c ) == FUNCTION DECL)
p r i n t f ( ” I t worked !\ n ”) ;e l s e
p r i n t f ( ”Grr r , i t f a i l e d \n ”) ;
I GENERIC trees defined and documented in gcc/tree.def
I Accessor macros and utility functions in gcc/tree.h
Dodji Seketeli <[email protected]> - G++ Internals 10/25
![Page 11: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/11.jpg)
G++ Internal representation
I Based on trees
I Superset of GENERIC
I G++ trees defined and documented in gcc/cp/cp-tree.def
I Accessors macros and utility functions in gcc/cp/cp-tree.h andgcc/cp/tree.c
Dodji Seketeli <[email protected]> - G++ Internals 11/25
![Page 12: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/12.jpg)
G++ Internal representation
template <c l a s s T>v o i d f o o ( v o i d ){}
TEMPLATE_DECL
DECL_TEMPLATE_RESULT
DECL_NAME
DECL_TEMPLATE_PARMS
FUNCTION_DECL
IDENTIFIER_NODE(foo)
void_type_node
TREE_LIST
TREE_LIST(void_list_node)
DECL_ARG_TYPES
TREE_TYPE DECL_NAME
Dodji Seketeli <[email protected]> - G++ Internals 12/25
![Page 13: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/13.jpg)
GIMPLE Representation
I Used by Middle-End
I 3 address representation
t1 = a + b ;
I Restricted grammar to simplify job of optimizers
I No hidden or implicit side effect
I Local variables of scalar types treated as “registers” (real operands)
I Globals and non-scalar types treated as “memory” (virtual operand).
t1 = a [ 5 ] ;t2 = ∗p1 ;
Dodji Seketeli <[email protected]> - G++ Internals 13/25
![Page 14: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/14.jpg)
GIMPLE Representation
I Can be incrementally lowered (2 levels currently)I High GIMPLE: lexical scopes
i f ( t1 == 0)t2 = 5 ;
r e t u r n 0 ;
I Low GIMPLE: no lexical scopes
i f ( t1 == 0) <L1 , L2>L1 :t2 = 5 ;goto L3 ;L2 :L3 :r e t u r n 0 ;
I Simplified control flowI Loops represented as gotosI No lexical scope (low GIMPLE)
Dodji Seketeli <[email protected]> - G++ Internals 14/25
![Page 15: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/15.jpg)
GIMPLE Representation
GENERIC High GIMPLE Low GIMPLE
i f ( f o o ( a + b , c ) )c = b++ / a
e n d i fr e t u r n c
t1 = a + bt2 = f o o ( t1 , c )i f ( t2 != 0)
t3 = bb = b + 1c = t3 / a
e n d i fr e t u r n c
t1 = a + bt2 = f o o ( t1 , c )i f ( t2 != 0) <L1 , L2>L1 :t3 = bb = b + 1c = t3 / agoto L3L2 :L3 :r e t u r n c
Dodji Seketeli <[email protected]> - G++ Internals 15/25
![Page 16: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/16.jpg)
Outline
Source code
Compiler pipeline
Internal representations
Class analysis process
Class template representation
Template instantiation process
Questions
Dodji Seketeli <[email protected]> - G++ Internals 16/25
![Page 17: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/17.jpg)
Class representation
Code Representation
s t r u c t Foo{
i n t m;
v o i dmethod ( ){
c h a r j ;}
} ;
RECORD_TYPE
IDENTIFIER_NODE(Foo)
FIELD_DECL FUNCTION_DECL
TYPE_IDENTIFIERTYPE_FIELDS
TYPE_METHODS
INTEGER_TYPE
TREE_TYPE
IDENTIFIER_NODE(m)
DECL_NAME
METHOD_TYPE IDENTIFIER_NODE(method)
void_type_node
TREE_TYPE
DECL_NAME
TREE_TYPE
TREE_LIST(void_list_node)
TREE_ARG_TYPES
Dodji Seketeli <[email protected]> - G++ Internals 17/25
![Page 18: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/18.jpg)
Class analysis process
1/ cp_parser_translation_unit(parser.c)
2/ cp_parser_class_specifier(parser.c)
3/ cp_parser_class_head(parser.c)
4/ xref_tag(decl.c)
5/ begin_class_definition(semantics.c)
6/ cp_parser_member_declaration(parser.c)
7/ finish_member_declaration(semantics.c)
8/ finish_struct(semantics.c)
1. cp parser translation unit
I Parser entry point (recursive descent)
2. cp parser class specifier
I parses the entire class
3. cp parser class head
I Parses E.g: class foo
4. xref tag : Declare the new typeI Create (non-complete) type
Inserts it into symbols table
5. begin class definitionI Setup new type definition
E.g: Open class scope
6. cp parser member declarationI called to parse each class member
7. finish member declarationI Add parsed member to current class type
8. finish structI Layout the typeI Emit type debug info
Dodji Seketeli <[email protected]> - G++ Internals 18/25
![Page 19: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/19.jpg)
Outline
Source code
Compiler pipeline
Internal representations
Class analysis process
Class template representation
Template instantiation process
Questions
Dodji Seketeli <[email protected]> - G++ Internals 19/25
![Page 20: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/20.jpg)
class template representation
template <c l a s s T>s t r u c t Foo{
T m;
Tget m ( ){
r e t u r n m;}
} ;
RECORD_TYPE
IDENTIFIER_NODE(Foo)
FIELD_DECL FUNCTION_DECL
TYPE_IDENTIFIERTYPE_FIELDS
TYPE_METHODS
TEMPLATE_TYPE_PARM
TREE_TYPE
IDENTIFIER_NODE(m)
METHOD_TYPE IDENTIFIER_NODE(get_m)
TREE_TYPEDECL_NAME
TREE_TYPE
TREE_LIST(void_list_node)
TREE_ARG_TYPES
TEMPLATE_DECL
DECL_NAMETREE_TYPE
TYPE_DECL
DECL_TEMPLATE_RESULT
TYPE_IDENTIFIER
DECL_NAME
DECL_NAME
Dodji Seketeli <[email protected]> - G++ Internals 20/25
![Page 21: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/21.jpg)
Outline
Source code
Compiler pipeline
Internal representations
Class analysis process
Class template representation
Template instantiation process
Questions
Dodji Seketeli <[email protected]> - G++ Internals 21/25
![Page 22: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/22.jpg)
Template instantiation process
1/cp_parser_simple_declarationparses grammar:simple-declaration: decl-specifier-seq [opt] init-declarator-list [opt]
2/cp_parser_template_id
3/cp_parser_init_declarator
4/start_decl
5/instantiate_class_template
During decl-specifier-seq parsing
I So we want to parse:Foo<int> bar;
1. Parses the declarationI Sequence of decl specifiersI Init declarator
2. Parses template-idI Parse template nameI Look it upI Parses templ arg intI Create non-complete type
3. Parse the init-declaratorI Parse declarator bar
4. Add declarator into sym tableI Complete type Foo<int>I Semantic analysis
5. Instantiate Foo<int>I Find most specialized
template FooI Substitute int
into template parmsI Semantic analysis
Dodji Seketeli <[email protected]> - G++ Internals 22/25
![Page 23: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/23.jpg)
Outline
Source code
Compiler pipeline
Internal representations
Class analysis process
Class template representation
Template instantiation process
Questions
Dodji Seketeli <[email protected]> - G++ Internals 23/25
![Page 24: G++ Internals - TuxFamilydownloads.tuxfamily.org/eigen/meetings/paris2010/Dodji-GCC.pdf · I GCC is more than 2,3 millions of LOC I G++ speci c lines of code is around 84 KLOC I GCC](https://reader034.vdocuments.us/reader034/viewer/2022042120/5e9a3006b36a812b0248f085/html5/thumbnails/24.jpg)
Contact
I irc :// oftc .net#gcc
I git clone git ://gcc.gnu.org/git/gcc
I http://gcc.gnu.org/onlinedocs/gccint/
I http://gcc.gnu.org
Dodji Seketeli <[email protected]> - G++ Internals 24/25