Writing a GCC Front End
Many kinds of expr trees are available that represent the various kinds of expressions in a program. These are similar to C expressions but are more general in some ways. For instance, trees do not distinguish between if statements and conditional expressions—both are represented by a COND_EXPR, with the only difference being that an if statement has void type. Here's how we can build an expression that adds our variable to itself:
tree addition = build2 (PLUS_EXPR, type_jchar, local, local);
Trees that represent statements are linked together using a special iterator API. Here is how we would chain together two statements, s1 and s2:
tree result = alloc_stmt_list (); tree_stmt_iterator out = tsi_start (result); tsi_link_after (&out, s1, TSI_CONTINUE_LINKING); tsi_link_after (&out, s2, TSI_CONTINUE_LINKING); // Now `result' holds the list of statements.
Other kinds of tree nodes exist; read tree.def and the manual for a more complete understanding. It also is possible for a front end to define its own tree codes; however, if you have your own AST, you should not need to do this.
The overall structure of the program you generate probably is going to resemble a translation unit decl, which would contain types, variables and functions.
Once you've built the trees representing a function, a global variable or a type for which you want to generate debugging information, you need to pass the tree to the appropriate function to handle the rest of compilation. Three such functions are available at present: rest_of_decl_compilation handles compilation for a decl node, cgraph_finalize_function handles compilation for a function and rest_of_type_compilation handles compilation for a type.
Although GCC has a fair number of internal consistency checks, it still is too easy to provoke crashes in code that are unrelated to your front end. In many cases, you can move up the stack, printing whatever trees are being manipulated, until you find some discrepancy caused by incorrect tree generation. This technique requires surprisingly little general GCC knowledge in order to effectively debug your code.
GCC has some handy debug functionality. In the debugger you can call the debug_tree function to print a tree. You also can use the -fdump-tree family of command-line options to print trees after various passes have been run.
My experience writing gcjx has been that lowering its strongly typed intermediate representation to trees is quite simple. The tree back end to gcjx, one back end among several, represents roughly 10% of the total code of the compiler. Although unfinished, it currently weighs in at 6,000 lines of code (raw wc -l count)—around the same size as the bytecode back end. One inference to draw from this is if you already have a compiler, the task of attaching it to GCC can be accomplished easily.
As trees are high-level, I haven't looked at any RTL while writing this front end. I haven't spent any time at all thinking about or dealing with processor-specific issues. Unless your language has some esoteric requirements, this ought to hold for you as well.
The statically typed AST in gcjx is easily reused. Currently, there are four back ends, and I expect to write more later. For instance, it would be simple to build a back end that writes a cross-reference representation of the program to a database. Or, it would be possible to write a back end that walks the AST searching for typical errors, akin to the FindBugs program. This consideration would be even more compelling for languages, which, unlike Java, don't already have a wealth of analysis tools.
The process of writing a front end certainly could be made even easier. For instance, there is no need to require lang-specs.h. Instead, a front end could install a description file that the GCC driver would read at startup. Similarly, lang.opt probably could be dispensed with. With more work, it even would be possible to compile front ends separately from the rest of GCC.
Resources for this article: /article/8138.
Tom Tromey has been involved with free software since 1991 and has worked on many different programs. He currently is employed as an engineer at Red Hat, working on GCJ.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Server Hardening
- May 2016 Issue of Linux Journal
- EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager
- The Humble Hacker?
- BitTorrent Inc.'s Sync
- The US Government and Open-Source Software
- The Death of RoboVM
- Open-Source Project Secretly Funded by CIA
- New Container Image Standard Promises More Portable Apps
- ACI Worldwide's UP Retail Payments
In modern computer systems, privacy and security are mandatory. However, connections from the outside over public networks automatically imply risks. One easily available solution to avoid eavesdroppers’ attempts is SSH. But, its wide adoption during the past 21 years has made it a target for attackers, so hardening your system properly is a must.
Additionally, in highly regulated markets, you must comply with specific operational requirements, proving that you conform to standards and even that you have included new mandatory authentication methods, such as two-factor authentication. In this ebook, I discuss SSH and how to configure and manage it to guarantee that your network is safe, your data is secure and that you comply with relevant regulations.Get the Guide