Machine Translated by Google It i s a rare book i n C hina that comprehensively and systematically i ntroduces t he design a nd implementation of GCC, and conducts i n-depth a nalysis and summary of t he overall design, m ain code s tructure and implementation details o f GCC source c ode system Huazhang Technology This b ook c ombines the GCC4.4.0 source c ode, revolves around the G CC c ompilation process, and uses the i ntermediate r epresentations of AST, GIMPLE, a nd RTL i n G CC as t he main l ine, a nd d escribes a clear r oadmap f rom source code to target m achine assembly c ode for readers In-depth analysis GCC Wang Y agang ÿ Weaving C hopsticks © Machinery I ndustry Press China Machine Press Machine Translated by Google This book c ombines GC C 4.4.0 source code, around GC C compilation p rocess , The design f ramework of G CC i s introduced in d etail And the implementation p rocess, f rom s ource c ode t o A ST, f rom A ST to GIMP LE . From GIMP L E to R TL , a nd f rom R TL t o t he f inal t arget machine c ode. The detailed p rocess i nvolves t he d etailed analysis a nd g eneration p rocess o f the intermediate r epresentation of e ach s tage, so t hat O n the b asis of understanding the principles of c ompilation, readers c an f urther grasp t he overall p rocess a nd implementation d etails of i ts i mplementation, so t hat m ore readers c an no longer stay a t t he t heoretical l evel o f c ompilation technology, but can see the implementation p rocess o f a live c ompilation system e xample. This book h as 1 2 chapters i n t otal. C hapter 1 i s a n overview of GCC, C hapter 2 i ntroduces GCC source c ode a nalysis tools, C hapter 3 introduces the overall structure of GCC, and Chapter 4 i ntroduces from source code t o AST / GE NE R I C (cid:127) Chapter 5 C hapter 6 i ntroduces GIMPLE processing and optimization , Chapter 7 i ntroduces RTL , Chapter 8 i ntroduces machine description file $ { target}.md , C hapter 9 i ntroduces machine description File$ { target } Chapter 1 0 introduces from .[ch], GIMP LE to RTL , C hapter 1 1 introduces RTsuLp pproorcte fsosr in nCeghw a a pn tatder gr o ep1 tt2 ip m irnioztcraoetdisousnco,e ra ssn(cid:127) d This book i s b ased on the a uthor's own scientific research work practice a nd s cientific r esearch interests. It t ook more than three years to study and s tudy the source c ode o f GC C4.4.0 a ssiduously. It i s a s ummary of his own e xperience i n l earning a nd analyzing t he c ompilation system. Examples Rich a nd p ractical. Machine Translated by Google Source code splitting series In-depth analysis GCC Wang Yagang ÿ W eaving C hopsticks Mechanical I ndustry P ress China Machine Press Machine Translated by Google Cataloging in P ublication ( CIP) data In -depth a nalysis of G CC / e dited by Wang Y agang. One B eijing: M achinery I ndustry Press, 2017.1 ( source code a nalysis series) ISBN 978-7-111-55632-9 I. D eep...II. King...III. Application s oftware IV. T P317 China E dition L ibrary CIP Data N uclei (2016 ) No. 317737 In-depth analysis of G CC Publishing and distribution: M achinery W angye Publishing House "122 Wanwanzhuang Street, Xicheng District, Beijing: No. 100037) Your editor in c harge: Zhang M engling Printing: B eijing C hYenoguxr i pnr Wooefriyeea d Perrin: tD inogn g C o ., Ltd. Open v olume: 186mm x 240mm 1/16 B ook number: ISBN 978 - 7-111-55632-9 Jili Edition: F ebruary 2017 1 st edition 1st printing sheet: 34.25 P rice: 99.00 yuan For those who purchase this book , i f t here a re missing p ages , u pside-down pages, or m issingp ages, t he Publishing Department o f this agency will replace t hem. @hzbook.com All r ights r eserved. I nfringement must be investigated. Back cover without anti- counterfeiting label i s a pirated b ook. Legal advisor: B eijing D acheng Law Firm "Guang/Bu Xiaodong Machine Translated by Google Talk i s cheap, show me the c ode. -------Linus Torralds Machine Translated by Google foreword GC C ( GN U C ompiler C ol l e ct i on, G NU Compiler S uite) is a s et of p rogramming l anguage compilation tools d eveloped by GNU, and is the most i mportant part of the GNU p roject. A fter nearly 30 y ears o f d evelopment, G CC not o nly s upports many It also s upports various mainstream p rocessor platforms and o perating s ystem platforms, a nd h as b ecome a r ecognized cross-platform c ompiler The d e f acto s tandard o f , has also b ecome a s uccessful example of compiler d esign. As a user o f the GCC c ompiler and a l over of source code r eading, I h ave a lways wanted t o w rite a t echnical book a bout GCC. In 2 002, I s tarted to do some software development o n t he L inux system, and G CC was t he c ompilation tool I u sed. I o ne I have a lways been c urious about the conversion process from s ource code t o o bject code, a nd w ant t o k now what GCC has done in t his process ? How i s GCC d esigned, and w hat meaning do t hose t housands of G CC source code f iles r epresent? A t that time , I o ften fantasized that i t w ould be g reat i f I c ould thoroughly analyze a nd understand t he G CC source code! S ince then, in a ddition t o t eaching a nd scientific research, I occasionally r ead t he relevant s ource codes of G CC, but I o ften feel a t a l oss w hen looking at t he v arious G CC source codes. There i s a kind o f embarrassment that " the t iger e ats the s ky a nd cannot c law". S o t he a nalysis o f t he G CC source code w as p ut o n h old, b ut the feeling o f finding o ut is always lingering. Since 2 012, I h ave m ore l eisure t ime. After a p eriod o f h esitation, I have t he u rge to a nalyze t he s ource code o f G CC Came to m ind a gain. I k now, this t ime i t's g oing to be real, I want t o d o s omething I l ike. Why? I have f ree time, I w ant to do something that i nterests me. In t he G CC enthusiast e xchange group I created, f riends o ften ask, is t here any i nformation a bout GCC? Most people w ill s ay, yes - please s ee the o fficial documentation! I a lso went t o s ee Look, y es, G CC has m ore d etailed official documents, i ncluding gc cin ter na l and user manuals. However, these d ocuments The content is huge a nd c omplex, lacking the content o f s ystematically analyzing the G CC design framework and workflow, a nd most of t he content is scattered for readers, making beginners at a l oss. So I thought, w hy n ot a nalyze the G CC system and i ntroduce the d esign and implementation of GCC to GCC e nthusiasts in a c learer a nd m ore s ystematic w ay? What? This book will focus on GCC; the compilation process, and introduce in detail from s ource code to A ST, from A ST t o G IMPLE, f rom G IMPLE Machine Translated by Google V To RTL, a nd from RTL t o t he detailed process of t he final target m achine a ssembly code, involving t he details o f the intermediate r epresentation of each s tage Analysis, g eneration process. T his book p rovides a large number o f c harts and e xamples, showing t he overall workflow a nd working details o f the GCC compilation s ystem. Another f eature o f this book is t o analyze the source code o f G C C 4.4.0 , so that r eaders can u nderstand the s ource code of compilation On t he basis o f theory, we can f urther g rasp t he overall p rocess and d etails o f i ts implementation, so t hat more readers' u nderstanding of compilation t echnology is no l onger just at the theoretical level, but to show t hem the implementation p rocess of a compilation s ystem example. How? The c ontent involved in t he GCC source c ode i s very complex, a nd i t i s difficult t o fully describe it in a book, so t his book u ses t he GCC intermediate t able Show the m ain line, analyze in detail t he three intermediate r epresentations ( AST , G 1 MP L E and RTL) used b y G CC in t he whole p rocess from the source code to the g eneration of t he target m achine a ssembly c ode, and analyze the basics o f t hese t hree i ntermediate representations The concept and generation p rocess a re d escribed in detail, and the optimization p rocessing b ased on G I M P L E a nd RTL is introduced, so a s t o d escribe a c lear roadmap from s ource c ode to t arget m achine a ssembly c ode. Who? This b ook i s m ainly aimed at c ollege students a nd g raduate students w ho l ove t he theory of compilation s ystem and its r ealization. I t can also b e u sed a s a useful r eference for R&D engineers who develop and transplant compilation systems i n e nterprises. When writing this book, t here is a kind of s pirit s upporting m e, I believe that " interest" p lus " persistence" is victory! Analyzing G CC i s n ot a matter of a y ear or a h alf, i t will take 3 years, 5 y ears, o r e ven l onger, but I can insist, I will u se My p ersistence is i n exchange for an in-depth a nalysis of G CC, s o that m ore GCC enthusiasts can get familiar with i t, get in touch with i t, understand i t, and participate m ore in the d evelopment a nd maintenance of GCC. Thanks to my lover and c hildren for g iving me t he warmth a nd a ffection o f my family. Thanks t o my father o n t he sick bed, a lthough h e c an't tell m e words, but his b ig h ands still often caress m y h ead. T hanks t o m y elderly a nd f rail m other, a nd to my father w ho h as always taken care o f me, f or letting m e k now what p ersistence is and what i s not giving up! Thanks to the c olleagues of t he G PU project t eam of X i'an U niversity of P osts and Telecommunications f or their valuable suggestions i n the writing of this b ook. The w riting o f t his b ook w as supported by the key p rojects of t he National Natural S cience Foundation of China (Project N o.: 6 1 1 3 6 0 0 2 ) a nd the S haanxi Provincial D epartment o f Education Funded b y scientific research plan project (project n umber: 1 4JK1674 ). In view of the limited l evel o f t he a uthor, some p ersonal v iewpoints w ere also introduced in the process of analyzing and writing this b ook, so i t is inevitable that some For s ome deviations and mistakes in u nderstanding, r eaders a re welcome to c riticize a nd correct me. I f you have a ny comments a nd suggestions, p lease c ontact the author lazy_ [email protected]. Thanks! Wang Yagang Xi'an University of Posts a nd Telecommunications in O ctober 2 016 Machine Translated by Google Table of c ontents foreword Chapter 1 O verview of GCC.................................................1 4.3 T ree Node S tructure .................................. 33 4.3.1 s truct tree_base...................................35 1.1 The emergence and d evelopment o f GCC.................................... 1 4.3.2 s truct tree_common.............36 1.2 Features o f G CC....................................2 1.3 G CC c ode 4.3.3 Constant Node....................................38 analysis. ...................................3 4.3.4 Identifier N odes................................ 4 2 Chapter 2 G CC S ource Code A nalysis Tool(cid:127)(cid:127)… 4 4.3.5 Declaring N odes................................44 4.3.6 s truct t ree_decl_minimal... 4 6 2.1 vim+ctags code reading t ool...................4 2 .2 GNUgdb 4.3.7 s truct t ree__decl_common... 46 debugging tool........... ..........6 4.3.8 s truct t ree_field_decl...49 2.3 GNU binutils tools....................................8 4.3.9 s truct t ree_decl_with_rtl... 55 2.4 shell tool a nd g raphviz d rawing t ool(cid:127)(cid:127)...11 4.3.10 struct t ree_label_decl...........55 2.5 GCC Debugging O ptions....................................13 4.3.11 struct t ree_result_decl... 5 6 Chapter 3 O verall S tructure of G CC....................16 4.3.12 struct t ree_const_decl... 5 7 3.1 D irectory structure of GCC...................................16 4.3.13 struct t ree_parm_decl...........57 4.3.14 struct t ree_decl_with_vis...........59 3.2 L ogical structure of GCC........... .............. 18 3.3 GCC source c ode c ompilation................... .......... 20 4.3.15 struct t ree_var_decl...........59 3.3.1 Configuration.................................................21 4.3.16 struct t ree_decl_non_ 3.3.2 Compilation................................................. 23 common...................................62 3.3.3 I nstallation.................................... 25 4.3.17 struct tree_function_decl...........62 4.3.18 struct tree_type_decl...........64 Chapter 4 F rom s ource c ode to AST/ 4.3.19 Type Node...................................67 GENERIC ................................. 2 6 4.3.20 tree_list node.................... 6 8 4.3.21 4.1 Abstract Syntax T ree ................................. 2 6 4.2 T ree expression n ode........... ..........71 Nodes Statement ...................................28 4.3.22 Statement Node ................................. 73 Machine Translated by Google vn 4.3.23 Other T ree Nodes .................................75 5.7.2 S TATEMENT_LIST_EXPR 4.4 A ST output and diagram....................76 4.5 A ST GIMPLE G ENERATION OF NODES...........159 generation............ ................................83 5.7.3 M OD1FY_EXPR node 4.5.1 L exical Analysis.................................................84 GIMPLE GENERATION ................................. 160 4.5.2 T he L exical Analysis Process...................................90 5.7.4 P OSTINCREMENT_EXPR 4.5.3 S yntax A nalysis...................................98 4.5.4 Syntax GIMPLE G ENERATION OF N ODES...........162 Analysis Process.... ................................99 5.8 Example A nalysis ................................................. 172 4.5.5 c_parse_file...................................103 5.9 Summary....................................................176 4.5.6 c_parser_translation_unit... 105 4.5.7 c_parser_extemal_ CHAPTER 6 GIMPLE P ROCESSING A ND ITS OPTIMIZATION 1 77 declaration....................................105 6.1 GCC Pass...................................................177 4.5.8 c_parser_declaration_ 6.1.1 Core D ata S tructures .................................. 177 orfhdef................................................107 6.1.2 T ypes o f P ass.................................... 179 4.5.9 c _parser_declspecs ................................................................. 1 12 6.1.3 I nitialization of t he Pass chain....................182 4.6 S ummary ................................................. 114 6.1.4 E xecution of t he Pass........................... .... 184 6.2 Table of Pass 1J ....................................187 Chapter 5 F rom A ST/GENERIC to 6.3 Example of G IMPLE Pass.................................... 193 GIMPLE.................................115 6.3.1 p ass r emove u seless s tmts (cid:127)(cid:127)(cid:127) 193 5.1 G IMPLE.................................................... 115 6.3.2 p asslowercf....................................195 5.2 G IMPLE Statements....................................119 6.3.3 p assbuildcfg...................................197 5.3 R epresentation and storage o f G IMPLE... 122 5.4 6.3.4 p assbuildcgraphedges... 203 Operands of G IMPLE s tatements........................... . 128 5.5 6.3.5 p assbuildssa...................................205 Basics of G IMPLE statement s equences 6.3.6 p assaloptimizations.....206 Operation................................................132 6.3.7 p assexpand...................................207 5.6 G eneration o f G IMPLE..................................135 6.4 Summary...................................................207 5.6.1 g implifyfunctiontree..................................................................136 Chapter 7 R TL ................................208 5.6.2 gimplifybody.................................................138 5.6.3 gimlify_parameters... 1 39 7.1 Object Types in R TL ................................209 5.6.4 gimplifystmt...................................144 7.2 RTX_CODE................................................210 5.6.5 gimplifyexpr...................................144 7.3 Types o f R TX...................................210 5.7 E xamples of G IMPLE t ransformations ................................................. 1 57 7.4 RTX output format ................................. 212 5.7.1 BIND_EXPR node's 7.5 RTX operands...................................213 7.6 RTX GIMPLE G ENERATION ................................. 158 machine mode........ ...................................216 Machine Translated by Google VIII 7.7 S torage o f R TX..................................... 219 8.7 Summary ................................................. 271 7.8 R TX e xpressions ................................. 222 Chapter 9 Machine 7.8.1 C onstants ................................. 225 Description File ${target}.[ch]...........272 7.8.2 R egisters and memory...................................227 7.8.3 Arithmetic operations........................... ...................228 9.1 target m ...................................................272 7.8.4 C omparison operations..................................... 230 9.1.1 Definition of struct gcc t arget ...273 9.1.2 7.8.5 S ide E ffects ................................. 230 Initialization of targetm... 277 9 .2 C ompilation d river 7.9 I R-RTL.................................................... 232 and o ptions..... ...................................279 7.9.1 I NSNs................................................. 233 9.2.1 Compilation o ptions...................280 7.9.2 J UMP_INSN ................................. 234 9.2.2 SPEC l anguage a nd S PEC f iles....281 7.9.3 C ALL_INSN................................................235 9.2.3 Machine D ependent Compilation Options ..........285 7.9.4 B ARRIER ................................. 235 9.3 Storage Layout................................................286 9.3. 1 B it O rder 7.9.5 C ODE_LABEL................................236 and B yte O rder .................. 2 86 7.9.6 N OTE ................................................. 237 9.3.2 Type W idth .................................. 287 9 .3.3 Machine 7.10 S ummary...................................................238 Mode Hoisting... ...................287 9.3.4 S torage Alignment .................................. 287 ..288 Chapter 8 M achine 9.3.5 D ata T ypes i n Programming L anguages Description F ile $ {target}.md... 239 Storage Layout ................................. 289 8.1 M achine description f ile...................................240 8 .2 I nstruction 9.4 Register usage....................................290 9 .4.1 B asic r egisters template..... ................................................ 241 Description ................................ 290 9 .4.2 Register Allocation 8.2.1 T emplate name ................................................. 242 Sequence .......................... 297 8.2.2 R TL t emplates.................................... 246 9.4.3 Machine M ode................................298 8.2.3 C onditions...................................256 8.2.4 O utput 9.4.4 Register T ypes................................... 300 templates.... ...................................256 8.2.5 9.5 Description of stack and f unction call s pecification... 3 07 9 .5.1 B asic Attributes................................ ...................256 characteristics o f stack........... ....309 9 .5.2 R egister 8.3 D efining R TL S equences ................................. 257 Elimination.....................313 9 .5.3 Function S tack Frame 8.4 I nstruction s plitting....................................263 8.5 Management... ...................315 Enumerators................................................. 266 8.5.1 mode 9.5.4 Parameter p assing ................................................. 316 enumerator....................266 8 .5.2 c ode 9.5.5 Function r eturn v alue....................318 9 .5.6 i386 machine enumerator........... ..........268 stack frame............ .......... 3 18 8.6 P eephole Optimization ................................. 269 9.6 A ddressing Modes.................................................325 8.6.1 define_peephole ................................................. 2 69 9.7 Partitioning of Assembly Code ................................. 326 8.6.2 d efine jeephole2...................................270 9.8 Assembly L anguage f or D efining O utput ................................. 333