In late 1983, in an effort tobootstrap theGNU operating system,Richard Stallman askedAndrew S. Tanenbaum, the author of theAmsterdam Compiler Kit (also known as theFree UniversityCompiler Kit) for permission to use that software for GNU. When Tanenbaum advised him that the compiler was not free, and that only the university was free, Stallman decided to work on a different compiler.[14] His initial plan was to rewrite an existing compiler fromLawrence Livermore National Laboratory fromPastel to C with some help fromLen Tower and others.[15][16] Stallman wrote a new C front end for the Livermore compiler, but then realized that it required megabytes of stack space, an impossibility on a68000 Unix system with only 64 KB, and concluded he would have to write a new compiler from scratch.[15] None of the Pastel compiler code ended up in GCC, though Stallman did use the C front end he had written.[15][17]
GCC was first released March 22, 1987, available byFTP fromMIT.[18] Stallman was listed as the author but cited others for their contributions, including Tower for "parts of the parser, RTL generator, RTL definitions, and of the Vax machine description", Jack Davidson andChristopher W. Fraser for the idea of usingRTL as an intermediate language, and Paul Rubin for writing most of the preprocessor.[19] Described as the "first free software hit" byPeter H. Salus, the GNU compiler arrived just at the time whenSun Microsystems was unbundling its development tools fromits operating system, selling them separately at a higher combined price than the previous bundle, which led many of Sun's users to buy or download GCC instead of the vendor's tools.[20] While Stallman consideredGNU Emacs as his main project, by 1990 GCC supported thirteen computer architectures, was outperforming several vendor compilers, and was used commercially by several companies.[21]
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 ownfork of the compiler, provided they meet the GPL's terms, including its requirements to distributesource 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, as the project favored stability over new features.[22] The FSF kept such close control on what was added to the official version of GCC 2.x (developed since 1992) that GCC was used as one example of the "cathedral" development model inEric S. Raymond's essayThe Cathedral and the Bazaar.
In 1997, a group of developers formed theExperimental/Enhanced GNU Compiler System (EGCS) to merge several experimental forks into a single project.[22][17] The basis of the merger was a development snapshot of GCC (taken around the 2.7.2 and later followed up to 2.8.1 release). Mergers included g77 (Fortran), PGCC (P5Pentium-optimized GCC),[17] many C++ improvements, and many new architectures andoperating system variants.[23]
While both projects followed each other's changes closely, EGCS development proved considerably more vigorous, 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. With the release of GCC 2.95 in July 1999 the two projects were once again united.[24][17] GCC has since been maintained by a varied group of programmers from around the world under the direction of a steering committee.[25]
GCC 3 (2002) removed a front-end forCHILL due to a lack of maintenance.[26]
Regarding language version support for C++ and C, since GCC 11.1 the default target isgnu++17, a superset ofC++17, andgnu11, a superset ofC11, with strict standard support also available. GCC also provides experimental support forC++20 andC++23.[48]
Overview of GCC's extended compilation pipeline, including specialized programs like thepreprocessor,assembler andlinker.GCC follows the 3-stage architecture typical of multi-language and multi-CPUcompilers. Allprogram trees are converted to a common abstract representation at the "middle end", allowingcode optimization andbinary code generation facilities to be shared by all languages.
GCC's external interface followsUnix conventions. Users invoke a language-specific driver program (gcc for C,g++ for C++, etc.), which interpretscommand arguments, calls the actual compiler, runs theassembler on the output, and then optionally runs thelinker to produce a completeexecutable binary.
Each of the language compilers is a separate program that reads source code and outputsmachine code. All have a common internal structure. A per-language front endparses the source code in that language and produces anabstract syntax tree ("tree" for short).
These are, if necessary, converted to the middle end's input representation, calledGENERIC form; the middle end then gradually transforms the program towards its final form.Compiler optimizations andstatic code analysis techniques (such as FORTIFY_SOURCE,[49] a compiler directive that attempts to discover somebuffer overflows) are applied to the code. These work on multiple representations, mostly the architecture-independent GIMPLE representation and the architecture-dependentRTL representation. Finally,machine code is produced using architecture-specificpattern matching originally based on an algorithm of Jack Davidson and Chris Fraser.
GCC was written primarily inC except for parts of theAda front end. The distribution includes the standard libraries for Ada andC++ whose code is mostly written in those languages.[50][needs update] On some platforms, the distribution also includes a low-level runtime library,libgcc, written in a combination of machine-independent C and processor-specificmachine code, designed primarily to handle arithmetic operations that the target processor cannot perform directly.[51]
GCC uses many additional tools in its build, many of which are installed by default by many Unix and Linux distributions (but which, normally, aren't present in Windows installations), includingPerl,[further explanation needed]Flex,Bison, and other common tools. In addition, it currently requires three additional libraries to be present in order to build:GMP,MPC, andMPFR.[52]
In May 2010, the GCC steering committee decided to allow use of aC++ compiler to compile GCC.[53] The compiler was intended to be written mostly in C plus a subset of features from C++. In particular, this was decided so that GCC's developers could use thedestructors andgenerics features of C++.[54]
In August 2012, the GCC steering committee announced that GCC now uses C++ as its implementation language.[55] This means that to build GCC from sources, a C++ compiler is required that understandsISO/IEC C++03 standard.
On May 18, 2020, GCC moved away fromISO/IEC C++03 standard toISO/IEC C++11 standard (i.e. needed to compile, bootstrap, the compiler itself; by default it however compiles later versions of C++).[56]
Front ends consist ofpreprocessing,lexical analysis,syntactic analysis (parsing) and semantic analysis. The goals of compiler front ends are to either accept or reject candidate programs according to the language grammar and semantics, identify errors and handle valid program representations to later compiler stages. This example shows the lexer and parser steps performed for a simple program written inC.
Eachfront end uses a parser to produce theabstract syntax tree of a givensource file. Due to the syntax tree abstraction, source files of any of the different supported languages can be processed by the sameback end. GCC started out usingLALR parsers generated withBison, but gradually switched to hand-writtenrecursive-descent parsers for C++ in 2004,[57] and for C and Objective-C in 2006.[58] As of 2021 all front ends use hand-written recursive-descent parsers.
Until GCC 4.0 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 arelowered to multiple GIMPLE instructions. TheC,C++, andJava 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 converts this more complex form into the simplerSSA-based GIMPLE form that is the common language for a large number of language- and architecture-independent global (function scope) optimizations.
GENERIC is anintermediate representation language used as a "middle end" while compiling source code intoexecutable binaries. A subset, calledGIMPLE, is targeted by all the front ends of GCC.
The middle stage of GCC does all of the code analysis andoptimization, working independently of both the compiled language and the target architecture, starting from the GENERIC[59] representation and expanding it toregister transfer language (RTL). The GENERIC representation contains only the subset of the imperativeprogramming constructs optimized by the middle end.
Optimization can occur during any phase of compilation; however, the bulk of optimizations are performed after the syntax andsemantic analysis of the front end and before thecode generation of the back end; thus a common, though somewhat self-contradictory, name for this part of the compiler is the "middle end."
The exact set of GCC optimizations varies from release to release as it develops, but includes the standard algorithms, such asloop optimization,jump threading,common subexpression elimination,instruction scheduling, and so forth. TheRTL optimizations are of less importance with the addition of global SSA-based optimizations onGIMPLE trees,[63] as RTL optimizations have a much more limited scope, and have less high-level information.
The GCC project includes an implementation of theC++ Standard Library called libstdc++,[65] licensed under the GPLv3 License with an exception to link non-GPL applications when sources are built with GCC.[66]
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 someGimple representation included in the object file.[citation needed] 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
Plugins extend the GCC compiler directly.[67] 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 onGimple representations.[68] Several GCC plugins have already been published, notably:
The Python plugin, which links against libpython, and allows one to invoke arbitrary Python scripts from inside the compiler. The aim is to allow GCC plugins to be written in Python.
The MELT plugin provides a high-levelLisp-like language to extend GCC.[69]
The support of plugins was once a contentious issue in 2007.[70]
The C++ language has an active proposal for transactional memory. It can be enabled in GCC 6 and newer when compiling with-fgnu-tm.[7][71]
Unicode identifiers
Although the C++ language requires support for non-ASCIIUnicode characters inidentifiers, the feature has only been supported since GCC 10. As with the existing handling of string literals, the source file is assumed to be encoded inUTF-8. The feature is optional in C, but has been made available too since this change.[72][73]
C extensions
GNU C extends the C programming language with several non-standard-features, includingnested functions.[74]
TheGCJ Java compiler can target either a native machine language architecture or theJava virtual machine'sJava bytecode.[79] Whenretargeting GCC to a new platform,bootstrapping is often used. Motorola 68000, Zilog Z80, and other processors are also targeted in the GCC versions developed for various Texas Instruments, Hewlett Packard, Sharp, and Casio programmable graphing calculators.[80]
GCC is licensed under theGNU General Public License version 3.[81] TheGCC runtime exception permits compilation of proprietary programs (in addition to free software) with GCC headers and runtime libraries. This does not impact the license terms of GCC source code.[82]
^von Hagen, William (2006).The Definitive Guide to GCC. Definitive Guides (2nd ed.). Apress. p. XXVII.ISBN978-1-4302-0219-6.Archived from the original on April 5, 2024. RetrievedSeptember 25, 2020.So he wrote to VUCK's author asking if GNU could use it. Evidently, VUCK's developer was uncooperative, responding that the university was free but that the compiler was not.
^abcStallman, Richard (September 20, 2011)."About the GNU Project". The GNU Project.Archived from the original on August 9, 2019. RetrievedOctober 9, 2011.
^Puzo, Jerome E., ed. (February 1986)."Gnu's Zoo".GNU's Bulletin.1 (1). Free Software Foundation.Archived from the original on June 23, 2015. RetrievedAugust 11, 2007.
^Stallman, Richard M. (June 22, 2001) [First published 1988],"Contributors to GNU CC",Using and Porting the GNU Compiler Collection (GCC), Free Software Foundation, Inc., p. 7,archived from the original on January 18, 2023, retrievedJune 18, 2015.
^"Linux Information Project". LINFO.Archived from the original on January 3, 2023. RetrievedApril 27, 2010.The GCC has been ported to (i.e., modified to run on) more than 60 platforms, which is more than for any other compiler.
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