Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection

Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection

Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Under the Hood of the GNU Compiler Collection Morgan Deters Ron Cytron [email protected] [email protected] Programming Logic Group Distributed Object Computing Laboratory Technical University of Catalonia Washington University Barcelona, Spain St. Louis, Missouri CopyrightCopyright is is held held by by the the author/owner(s). author/owner(s). PLDIPLDI’08,’08, June June 9, 9, 2008, 2008, Tuscon, Tuscon, Arizona, Arizona, USA USA Copyright ©Copyright Deters and Ron Cytron Morgan 2005–2008 Demystifying GCC Morgan Deters and Ron Cytron Tutorial Objectives • Introduce the internals of GCC 4.3.0 (March 2008) Java and C++ front-ends Optimizations Back-end structure • How to modify, or write your own Front end • New languages, new features Middle end • Analysis, optimization Back end • Machine-specific targets • How to debug/improve GCC PLDI 2008 2 Tuscon, Arizona Tutorial Objectives 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron CC G Demystifying Introduction What is GCC? Why use GCC? What does compilation with GCC look like? PLDI 2008 3 Tuscon, Arizona 2008年8月12日星期二 Demystifying GCC Morgan Deters and Ron Cytron What is GCC ? • A compiler for multiple languages… C C++ Java Objective-C/C++ FORTRAN Ada PLDI 2008 4 Tuscon, Arizona Introduction 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron What is GCC ? • …supporting multiple targets arc arm avr bfin c4x cris crx fr30 frv h8300 i386 ia64 iq2000 m32c m32r m68hc11 m68k mcore mips mmix mn10300 mt pa pdp11 rs6000 s390 sh sparc stormy16 v850 vax xtensa TheseThese areare code code generators;generators;variantsvariants areare alsoalso supportedsupported ((e.g.e.g.powerpcpowerpc is is a a “variant”“variant” of of thethe rs6000 rs6000 code code generator) generator) PLDI 2008 5 Tuscon, Arizona Introduction 2008年8月12日星期二 Demystifying GCC Morgan Deters and Ron Cytron What GCC is not • GCC is not an assembler (see GNU binutils) a C library (see glibc) a debugger (see gdb) an IDE PLDI 2008 6 Tuscon, Arizona Introduction 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron Why use GCC as an R&D platform? • Research is immediately usable by everyone Large development community and user base GCC is a modern, practical compiler • multiple architectures, full standard languages, optimizations • debugging support • You can meet GCC halfway modular: hack some parts, rely on the others • Can incorporate bug fixes that come along minor version upgrades (e.g. 3.3.x Æ 3.4.x) – no big deal major version upgrades (e.g. 3.x Æ 4.x) – more of a pain • Need not maintain code indefinitely (if incorporated) PLDI 2008 7 Tuscon, Arizona Introduction 2008年8月12日星期二 Demystifying GCC Morgan Deters and Ron Cytron The GCC project and the GPL • Open-source GNU General Public License (GPL) • Changes made to GCC source code or associated libraries must also be GPLed • However, compiler and libraries can be used/linked against in non-GPL development YourYour improvementsimprovements toto GCCGCC mustmust bebe open-source,open-source, butbut youryour customerscustomers needneed notnot open-sourceopen-source theirtheir programsprograms toto useuse youryour stuffstuff PLDI 2008 8 Tuscon, Arizona Introduction 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron Typical structure of GCC compilation sourcesource programprogram gcc/g++/gcj assembly compiler assembly assembler linker compiler programprogram assembler linker ELFELF object object PLDI 2008 9 Tuscon, Arizona Introduction 2008年8月12日星期二 Demystifying GCC Morgan Deters and Ron Cytron Inside the compiler compiler (C, C++, Java) parser / parser / treetree semantic RTLRTL passes passes semantic optimizationsoptimizations checkerchecker targettarget arch arch gimplifiergimplifier expanderexpander instructioninstruction selection selection trees RTL PLDI 2008 10 Tuscon, Arizona Introduction 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron CC G Demystifying GCC Basics How do you build GCC? How do you navigate the source tree? PLDI 2008 11 Tuscon, Arizona 2008年8月12日星期二 Demystifying GCC Morgan Deters and Ron Cytron GCC Basics: Getting Started • Requirements to build GCC usual suite of UNIX tools (C compiler, assembler/linker, GNU Make, tar, awk, POSIX shell) • For development GNU m4 and GNU autotools (autoconf/automake/libtool) gperf bison, flex autogen, guile, gettext, perl, Texinfo, diffutils, patch, … • Obtaining GCC sources gcc.gnu.org or local mirror (see gcc.gnu.org/mirrors.html) get gcc-core package, then language add-ons • gcc-java requires gcc-g++ PLDI 2008 12 Tuscon, Arizona GCC Basics 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron Source Metrics • 492 Mbytes of material downloaded to build GCC • 2.7 Gbytes after build • As of 4.3.0 Need mpfr – Multiple precision floating point arithmetic Need gmp – Multiple precision integer arithmetic Need Eclipse – for Java front-end PLDI 2008 13 Tuscon, Arizona GCC Basics 2008年8月12日星期二 Demystifying GCC Morgan Deters and Ron Cytron Building GCC from sources • Configure it in a separate build directory from sources /path/to/source/directory/configure options… --prefix=install-location --enable-languages=comma-separated-language-list • To see the list of available languages: grep language= */config-lang.in --enable-checking • turns on sanity checks (especially on intermediate representation) • Build it ! Environment variables useful when debugging compiler/runtime • CFLAGS stage 1 flags (using host C compiler) • BOOT_CFLAGS stage 2 and stage 3 flags (using stage 1 GCC) • CFLAGS_FOR_TARGET flags for new GCC building target binaries • CXXFLAGS_FOR_TARGET flags for new GCC building libstdc++/others • GCJFLAGS flags for new GCC building Java runtime •‘-O0 –ggdb3’ is recommended when debugging PLDI 2008 14 Tuscon, Arizona GCC Basics 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron Building GCC from sources • Build it ! continued… make bootstrap (to bootstrap) or make (to not) • bootstrap useful when compiling with non-GCC host compiler • during development, non-bootstrap is faster and also better at recompiling just those sources that have changed use make’s -j option to speed things up on MP/dual core make bootstrap-lean • cleans up between stages, uses less disk make profiledbootstrap • faster compiler produced, but need GCC host • –j unsupported • Install it ! make install PLDI 2008 15 Tuscon, Arizona GCC Basics 2008年8月12日星期二 Demystifying GCC Morgan Deters and Ron Cytron Building a cross-compiler • Code generator can be built for any target runtime libraries then are built using that code generator • Since GCC outputs assembly, you actually need a full cross development toolchain Dan Kegel’s crosstool automates a GNU/Linux cross chain for popular configurations: • Linux kernel headers • GNU binutils •glibc •gcc • see kegel.com/crosstool PLDI 2008 16 Tuscon, Arizona GCC Basics 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron GCC Basics: Getting Around • Other tools recommended when hacking GCC GNU Screen attach/reattach terminal sessions etags navigation to source definitions (emacs) ctags navigation to source definitions (vi) c++filt demangle C++/Java mangled symbols readelf decompose ELF files objdump object file dumper/disassembler gdb GNU debugger PLDI 2008 17 Tuscon, Arizona GCC Basics 2008年8月12日星期二 Demystifying GCC Morgan Deters and Ron Cytron GCC Drivers • gcc, g++, gcj are drivers, not compilers They will execute (as appropriate): • compiler (cc1, cc1plus, jc1) • Java program main entry point generation (jvgenmain) • assembler (as) • linker (collect2) • Differences between drivers include active #defines, default libraries, other behavior but can use any driver for any source language PLDI 2008 18 Tuscon, Arizona GCC Basics 2008年8月12日星期二 Copyright (c) 2005-2008 Morgan Deters and Ron Cytron June 2008 Demystifying GCC: PLDI 2008 Under the Hood of the GNU Compiler Collection Demystifying GCC Morgan Deters and Ron Cytron Most useful driver options for debugging -E preprocess, don’t compile -S compile, don’t assemble -H verbose header inclusion -save-temps save temporary files -print-search-dirs print search paths -v verbose (see what the driver does) -g include debugging symbols --help get command line help --version show full version info -dumpversion show minimal version info PLDI 2008 19 Tuscon, Arizona GCC Basics 2008年8月12日星期二 Demystifying

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    52 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us