|Initial release||May 23, 1987|
|Stable release||4.7.0 / March 22, 2012; 2 months ago|
|Written in||C, C++|
|License||GNU General Public License (version 3 or later)|
The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain. As well as being the official compiler of the unfinished GNU operating system, GCC has been adopted as the standard compiler by most other modern Unix-like computer operating systems, including Linux, and the BSD family. A port to RISC OS has also been developed extensively in recent years. There is also an old (3.0) port of GCC to Plan9, running under its ANSI/POSIX Environment (APE). GCC is also available for the widely-used Microsoft Windows operating systems, and for the ARM processor used by many portable devices.
GCC has been ported to a wide variety of processor architectures, and is widely deployed as a tool in commercial, proprietary and closed source software development environments. GCC is also available for most embedded platforms, including Symbian (called gcce), AMCC and Freescale Power Architecture-based chips. The compiler can target a wide variety of platforms, including videogame consoles such as the PlayStation 2 and Dreamcast. Several companies make a business out of supplying and supporting GCC ports to various platforms, and chip manufacturers today consider a GCC port almost essential to the success of an architecture.
Originally named the GNU C Compiler, because it only handled the C programming language, GCC 1.0 was released in 1987, and the compiler was extended to compile C++ in December of that year. Front ends were later developed for Objective-C, Objective-C++, Fortran, Java, Ada, and Go among others.
Richard Stallman‘s initial plan was to rewrite an existing compiler from Lawrence Livermore Lab from Pastel to C with some help from Len Tower and others. Stallman wrote a new C front end for the Livermore compiler but then realized that it required megabytes of stack space, an impossibility on a 68000 Unix system with only 64K, and concluded he would have to write a new compiler from scratch. None of the Pastel compiler code ended up in GCC, though Stallman did use the C front end he had written.
GCC was first released March 22, 1987, available by ftp from MIT. Stallman was listed as the author but cited others for their contributions, including Jack Davidson and Christopher Fraser for the idea of using RTL as an intermediate language, Paul Rubin for writing most of the preprocessor and Leonard Tower for “parts of the parser, RTL generator, RTL definitions, and of the Vax machine description.”
By 1991, GCC 1.x had reached a point of stability, but architectural limitations prevented many desired improvements, so the FSF started work on GCC 2.x.
As GCC was licensed under the GPL, programmers wanting to work in other directions—particularly those writing interfaces for languages other than C—were free to develop their own fork of the compiler (provided they meet the GPL’s terms, including its requirements to distribute source code). Multiple forks proved inefficient and unwieldy, however, and the difficulty in getting work accepted by the official GCC project was greatly frustrating for many. The FSF kept such close control on what was added to the official version of GCC 2.x that GCC was used as one example of the “cathedral” development model in Eric S. Raymond‘s essay The Cathedral and the Bazaar.
In 1997, a group of developers formed EGCS (Experimental/Enhanced GNU Compiler System), to merge several experimental forks into a single project. The basis of the merger was a GCC development snapshot taken between the 2.7 and 2.81 releases. Projects merged included g77 (FORTRAN), PGCC (P5 Pentium-optimized GCC), many C++ improvements, and many new architectures and operating system variants.
EGCS development proved considerably more vigorous than GCC development, so much so that the FSF officially halted development on their GCC 2.x compiler, “blessed” EGCS as the official version of GCC and appointed the EGCS project as the GCC maintainers in April 1999. Furthermore, the project explicitly adopted the “bazaar” model over the “cathedral” model. With the release of GCC 2.95 in July 1999, the two projects were once again united.
GCC is now maintained by a varied group of programmers from around the world, under the direction of a steering committee. It has been ported to more kinds of processors and operating systems than any other compiler.
GCC stable release
The current stable version of GCC is 4.7.0, which was released on March 22, 2012.
GCC 4.6 supports many new Objective-C features, such as declared and synthesized properties, dot syntax, fast enumeration, optional protocol methods, method/protocol/class attributes, class extensions and a new GNU Objective-C runtime API. It also supports the Go programming language and includes the
libquadmath library, which provides quadruple-precision mathematical functions on targets supporting the
__float128 datatype. The library is used to provide the
REAL(16) type in GNU Fortran on such targets.
GCC uses many standard tools in its build, including Perl, Flex, Bison, and other common tools. In addition it currently requires three additional libraries to be present in order to build: GMP, MPC, and MPFR. Some optimization features need extra libraries, like Parma Polyhedra Library or Cloog (but GCC could be built without them).
The previous major version, 4.5, was initially released on April 14, 2010 (last minor version is 4.5.3, released on April 29, 2011). It included several minor new features (new targets, new language dialects) and a couple of major new features:
- Link-time optimization optimizes across object file boundaries to directly improve the linked binary. Link-time optimization relies on an intermediate file containing the serialization of some -Gimple- representation included in the object file . The file is generated alongside the object file during source compilation. Each source compilation generates a separate object file and link-time helper file. When the object files are linked, the compiler is executed again and uses the helper files to optimize code across the separately compiled object files.
- Plugins can extend the GCC compiler directly . Plugins allow a stock compiler to be tailored to specific needs by external code loaded as plugins. For example, plugins can add, replace, or even remove middle–end passes operating on Gimplerepresentations. Several GCC plugins have already been published, notably:
The trunk concentrates the major part of the development efforts, where new features are implemented and tested. Eventually, the code from the trunk will become the next major release of GCC, with version 4.8.
GCC is often chosen for developing software that is required to execute on a wide variety of hardware and/or operating systems. System-specific compilers provided by hardware or OS vendors can differ substantially, complicating both the software’s source code and the scripts that invoke the compiler to build it. With GCC, most of the compiler is the same on every platform, so only code that explicitly uses platform-specific features must be rewritten for each system.
The standard compiler release 4.6 includes front ends for C (gcc), C++ (g++), Objective-C (gobjc), Fortran (gfortran), Java (gcj), Ada (GNAT), and Go (gccgo). Also available, but not in standard are Pascal (gpc), Mercury, Modula-2, Modula-3, PL/I, D (gdc), and VHDL (ghdl). A popular parallel language extension, OpenMP, is also supported.
The Fortran front end was g77 before version 4.0, which only supports FORTRAN 77. In newer versions, g77 is dropped in favor of the new gfortran front end that supports Fortran 95 and parts of Fortran 2003 as well. As the later Fortran standards incorporate the F77 standard, standards-compliant F77 code is also standards-compliant F90/95 code, and so can be compiled without trouble in gfortran. A front-end for CHILL was dropped due to a lack of maintenance.
GCC target processor families as of version 4.3 include:
Lesser-known target processors supported in the standard release have included:
Additional processors have been supported by GCC versions maintained separately from the FSF version:
GCC’s external interface is generally standard for a UNIX compiler. Users invoke a driver program named
gcc, which interprets command arguments, decides which language compilers to use for each input file, runs the assembler on their output, and then possibly runs the linker to produce a complete executable binary.
Each of the language compilers is a separate program that inputs source code and outputs machine code. All have a common internal structure. A per-language front end parses the source code in that language and produces an abstract syntax tree (“tree” for short).
These are, if necessary, converted to the middle-end’s input representation, called GENERIC form; the middle-end then gradually transforms the program towards its final form. Compiler optimizations and static code analysis techniques (such as FORTIFY_SOURCE, a compiler directive that attempts to discover some buffer overflows) are applied to the code. These work on multiple representations, mostly the architecture-independent GIMPLE representation and the architecture-dependent RTL representation. Finally, machine code is produced using architecture-specific pattern matching originally based on an algorithm of Jack Davidson and Chris Fraser.
GCC is written primarily in C except for parts of the Ada front end. The distribution includes the standard libraries for Ada, C++, and Java whose code is mostly written in those languages. On some platforms, the distribution also includes a low-level runtime library, libgcc, written in a combination of machine-independent C and processor-specific machine code, designed primarily to handle arithmetic operations that the target processor cannot perform directly.
In May 2010, the GCC steering committee decided to allow use of a C++ compiler to compile GCC. The compiler will be written in C plus a subset of features from C++. In particular, this was decided so that GCC’s developers could use the “destructors” and “generics” features of C++.
Frontends vary internally, having to produce trees that can be handled by the backend. Currently, the parsers are all hand-coded recursive descent parsers, though there is no reason why a parser generator could not be used for new front-ends in the future (version 2 of the C compiler used a bison based grammar).
Until recently, the tree representation of the program was not fully independent of the processor being targeted.
The meaning of a tree was somewhat different for different language front-ends, and front-ends could provide their own tree codes. This was simplified with the introduction of GENERIC and GIMPLE, two new forms of language-independent trees that were introduced with the advent of GCC 4.0. GENERIC is more complex, based on the GCC 3.x Java front-end’s intermediate representation. GIMPLE is a simplified GENERIC, in which various constructs are lowered to multiple GIMPLE instructions. The C, C++ and Java front ends produce GENERIC directly in the front end. Other front ends instead have different intermediate representations after parsing and convert these to GENERIC.
In either case, the so-called “gimplifier” then lowers this more complex form into the simpler SSA-based GIMPLE form that is the common language for a large number of new powerful language- and architecture-independent global (function scope) optimizations.
GENERIC and GIMPLE
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. The GENERIC representation contains only the subset of the imperative programming constructs optimised by the middle-end.
In transforming the source code to GIMPLE, complex expressions are split into a three address code using temporary variables. This representation was inspired by the SIMPLE representation proposed in the McCAT compiler by Laurie J. Hendren for simplifying the analysis and optimization of imperative programs.
Optimization can occur during any phase of compilation, however the bulk of optimizations are performed after the syntax and semantic analysis of the front-end and before the code generation of the back-end, thus a common, even though somewhat contradictory, name for this part of the compiler is “middle end.”
The exact set of GCC optimizations varies from release to release as it develops, but includes the standard algorithms, such as loop optimization, jump threading, common subexpression elimination, instruction scheduling, and so forth. The RTL optimizations are of less importance with the addition of global SSA-based optimizations on GIMPLE trees, as RTL optimizations have a much more limited scope, and have less high-level information.
Some of these optimizations performed at this level include dead code elimination, partial redundancy elimination, global value numbering, sparse conditional constant propagation, and scalar replacement of aggregates. Array dependence based optimizations such as automatic vectorization and automatic parallelization are also performed. Profile-guided optimization is also possible as demonstrated here: http://gcc.gnu.org/install/build.html#TOC4
The behavior of GCC’s back end is partly specified by preprocessor macros and functions specific to a target architecture, for instance to define the endianness, word size, and calling conventions. The front part of the back end uses these to help decide RTL generation, so although GCC’s RTL is nominally processor-independent, the initial sequence of abstract instructions is already adapted to the target. At any moment, the actual RTL instructions forming the program representation have to comply with the machine description of the target architecture.
The machine description file contains RTL patterns, along with operand constraints, and code snippets to output the final assembly. The constraints indicate that a particular RTL pattern might only apply (for example) to certain hardware registers, or (for example) allow immediate operand offsets of only a limited size (e.g. 12, 16, 24, … bit offsets, etc.). During RTL generation, the constraints for the given target architecture are checked. In order to issue a given snippet of RTL, it must match one (or more) of the RTL patterns in the machine description file, and satisfy the constraints for that pattern; otherwise, it would be impossible to convert the final RTL into machine code.
Towards the end of compilation, valid RTL is reduced to a strict form in which each instruction refers to real machine registers and a pattern from the target’s machine description file. Forming strict RTL is a complicated task; an important step is register allocation, where real, hardware registers are chosen to replace the initially-assigned pseudo-registers. This is followed by a “reloading” phase; any pseudo-registers that were not assigned a real hardware register are ‘spilled’ to the stack, and RTL to perform this spilling is generated. Likewise, offsets that are too large to fit in an actual instruction must be broken up and replaced by RTL sequences that will obey the offset constraints.
In the final phase the machine code is built by calling a small snippet of code, associated with each pattern, to generate the real instructions from the target’s instruction set, using the final registers, offsets and addresses chosen during the reload phase. The assembly-generation snippet may be just a string; in which case, a simple string substitution of the registers, offsets, and/or addresses into the string is performed. The assembly-generation snippet may also be a short block of C code, performing some additional work, but ultimately returning a string containing the valid machine code.
Debugging GCC programs
The primary tool used to debug GCC code is the GNU Debugger (gdb). Among more specialized tools are Valgrind, for finding memory errors and leaks, and the graph profiler (gprof) that can determine how much time is spent in which routines, and how often they are called; this requires programs to be compiled with profiling options.
- ^ a b “GCC Releases”. GNU Project. Retrieved 2006-12-27.
- ^ “Porting alien software to Plan 9 |”. Bell Labs, Lucent. Retrieved 2011-09-06.
- ^ “Symbian GCC Improvement Project”. Retrieved 2007-11-08.
- ^ “Linux Board Support Packages”. Retrieved 2008-08-07.
- ^ “setting up gcc as a cross-compiler”. ps2stuff. 2002-06-08. Retrieved 2008-12-12.[dead link]
- ^ “sh4 g++ guide”. Archived from the original on 2002-12-20. Retrieved 2008-12-12. “This guide is intended for people who want to compile C++ code for their Dreamcast systems”
- ^ “FSF Service Directory”.
- ^ “Programming Languages Supported by GCC”. GNU Project. Retrieved 2011-11-25.
- ^ a b c Stallman, Richard (September 20, 2011). “About the GNU Project”. The GNU Project. Retrieved October 9, 2011. “Hoping to avoid the need to write the whole compiler myself, I obtained the source code for the Pastel compiler, which was a multiplatform compiler developed at Lawrence Livermore Lab. It supported, and was written in, an extended version of Pascal, designed to be a system-programming language. I added a C front end, and began porting it to the Motorola 68000 computer. But I had to give that up when I discovered that the compiler needed many megabytes of stack space, and the available 68000 Unix system would only allow 64k. … I concluded I would have to write a new compiler from scratch. That new compiler is now known as GCC; none of the Pastel compiler is used in it, but I managed to adapt and use the C front end that I had written.”
- ^ Puzo, Jerome E., ed. (February 1986). “Gnu’s Zoo”. GNU’S Bulletin (Free Software Foundation) 1 (1). Retrieved 2007-08-11. “Although I have a portable C and Pascal compiler, … most of the compiler is written in Pastel, … so it must all be rewritten into C. Len Tower, the sole full-time GNU staff person, is working on this, with one or two assistants.”
- ^ Richard M. Stallman (forwarded by Leonard H. Tower Jr.) (March 22, 1987). “GNU C compiler beta test release”. comp.lang.c. (Web link). “The GNU C compiler is now available for ftp from the file /u2/emacs/gcc.tar on prep.ai.mit.edu. This includes machine descriptions for vax and sun, 60 pages of documentation on writing machine descriptions … the ANSI standard (Nov 86) C preprocessor and 30 pages of reference manual for it. This compiler compiles itself correctly on the 68020 and did so recently on the vax. It recently compiled Emacs correctly on the 68020, and has also compiled tex-in-C and Kyoto Common Lisp.”. Retrieved October 9, 2011.
- ^ Stallman, Richard M. (24 April 1988), “Contributors to GNU CC”, Internals of GNU CC, Free Software Foundation, Inc., p. 7, retrieved October 3, 2011, “The idea of using RTL and some of the optimization ideas came from the U. of Arizona Portable Optimizer, written by Jack Davidson and Christopher Fraser. … Leonard Tower wrote parts of the parser, RTL generator, RTL definitions, and of the Vax machine description.”
- ^ a b Henkel-Wallace, David (15 August 1997), A new compiler project to merge the existing GCC forks, retrieved May 25, 2012, “On the other hand, Cygnus, the Linux folks, the pgcc folks, the Fortran folks and many others have done development work which has not yet gone into the GCC2 tree despite years of efforts to make it possible. … These forks are painful and waste time …”
- ^ “Pentium Compiler FAQ”.
- ^ “A Brief History of GCC”.
- ^ “The Short History of GCC development”.
- ^ “GCC steering committee”.
- ^ Linux Information Project (LINFO) accessed 2010-04-27
- ^ “GCC Front Ends”, gnu.org, Retrieved November 25, 2011.
- ^ “gdc project on bitbucket”. Retrieved 3 July 2010.
- ^ “Fortran 2003 Features in GNU Fortran”.
- ^ [PATCH] Remove chill, gcc.gnu.org, Retrieved July 29, 2010.
- ^ “GCC UPC (GCC Unified Parallel C) | intrepid.com”. intrepid.com<!. 2006-02-20. Retrieved 2009-03-11.
- ^ “Hexagon Project Wiki”. ” “Hexagon dowload”.
- ^ “sx-gcc: port gcc to nec sx vector cpu”.
- ^ “The GNU Compiler for the Java Programming Language”. Retrieved 2010-04-22.
- ^ “Security Features: Compile Time Buffer Checks (FORTIFY_SOURCE)”. fedoraproject.org. Retrieved 2009-03-11.
- ^ “languages used to make GCC”.
- ^ GCC Internals, GCC.org, Retrieved March 01, 2010.
- ^ “GCC allows C++ – to some degree”. The H. 1 June 2010.
- ^ “An email by Richard Stallman on emacs-devel”. “The reason the GCC developers wanted to use it is for destructors and generics. These aren’t much use in Emacs, which has GC and in which data types are handled at the Lisp level.”
- ^ GENERIC in GNU Compiler Collection Internals
- ^ GIMPLE in GNU Compiler Collection Internals
- ^ McCAT
- ^ Laurie J. Hendren
- ^ From Source to Binary: The Inner Workings of GCC, by Diego Novillo, Red Hat Magazine, December 2004
- Richard Stallman: Using the GNU Compiler Collection (GCC), Free Software Foundation, 2008.
- Richard Stallman: GNU Compiler Collection (GCC) Internals, Free Software Foundation, 2008.
- Brian J. Gough: An Introduction to GCC, Network Theory Ltd., 2004 (Revised August 2005). ISBN 0-9541617-9-3.
- Arthur Griffith, GCC: The Complete Reference. McGrawHill/Osborne, 2002. ISBN 0-07-222405-3.
|Wikibooks has a book on the topic of|
- GCC homepage
- The official GCC manuals and user documentation, by the GCC developers
- Collection of GCC 4.0.2 architecture and internals documents at I.I.T. Bombay.
- Kerner, Sean Michael (2006-03-02). “New GCC Heavy on Optimization”. internetnews.com.
- Kerner, Sean Michael (2005-04-22). “Open Source GCC 4.0: Older, Faster”. internetnews.com.
- From Source to Binary: The Inner Workings of GCC, by Diego Novillo, Red Hat Magazine, December 2004
- A 2003 paper on GENERIC and GIMPLE
- Marketing Cygnus Support, an essay covering GCC development for the 1990s, with 30 monthly reports for in the “Inside Cygnus Engineering” section near the end.
- EGCS 1.0 announcement
- EGCS 1.0 features list
- Fear of Forking, an essay by Rick Moen recording seven well-known forks, including the GCC/EGCS one
- A compiler course project based on GCC at the University of Rochester
- The stack-smashing protector, a GCC extension