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.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- Stunnel Security for Oracle
- SourceClear Open
- SUSE LLC's SUSE Manager
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- My +1 Sword of Productivity
- Managing Linux Using Puppet
- Tech Tip: Really Simple HTTP Server with Python
- Non-Linux FOSS: Caffeine!
- Google's SwiftShader Released
- Doing for User Space What We Did for Kernel Space
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide