Channels ▼

Al Williams

Dr. Dobb's Bloggers

Compiler Translation

September 24, 2013

When I build a substantial CPU, I often use my universal assembler hack to get a quick cross assembler going so I can write code. However, I often wish I could easily port a C compiler to my new target. The obvious answer, of course, is to retarget gcc. That sounds good in theory, but if you've ever tried porting gcc's backend to a new architecture, it is a daunting task.

Another common alternative to gcc is CLANG/LLVM. The gcc compiler compiles to an "intermediate language" (IL) that the machine code generator then converts to a specific assembly language. LLVM is similar to IL and there are a variety of frontends that compile languages to LLVM (CLANG is the one for the C language). The LLVM software can translate into different machine languages as a compiler or just in time, in some cases. There is also a gcc retarget that emits LLVM intermediate code.

Either way you go, you don't have to worry about all the compiler-specific details. You only need to figure out how to bridge from the compiler's intermediate representation to your machine code (usually, the backend emits assembly language as text, which is further compiled by something like gas, the GNU Assembler).

There are a few other options, too. The lcc compiler started out as a book in 1995. It uses iburg to do code generation, and that tool might be useful with some other retargeting projects, as well. The Lance compiler is available for limited use without a license. Although the vbcc compiler hasn't been updated since 2011, the documentation of its backend looked very good and it appeared to be one of the easier compilers to port.

Whatever tool you use, it looks like a big project and one I haven't attempted yet. There are a few tutorials floating around, although none seem complete or, at least, current. For LLVM, there is a tutorial that targets a special CPU and the official documentation. For gcc there are several choices, including a Master's thesis and the official documentation.

Since the CPUs I typically want to target are embedded, I may revisit SDCC, which has matured quite a bit. It appears to be retargetable (read Chapter 9 of the documentation) and has a reasonably small set of iCode primitives (which has nothing to do with Apple, by the way).

There are doubtlessly other retargetable CPUs out there, and I suppose I'll eventually bite the bullet and retarget one for One-Der or one of my other CPUs.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Comments:

ubm_techweb_disqus_sso_-b51f7a869a1175412e56da18be4810cb
2013-10-07T18:33:23

I have looked at tcc in the past. Thanks for reminding me about it. Bellard is quite prolific and a visit to his main page is always worth an hour of distraction (the PC emulator in JavaScript may be my favorite).


Permalink
AndrewBinstock
2013-10-07T08:18:50

Good point. tcc (tiny c compiler) compiles all of c89 and some of c99. For interested readers, it can be found here: http://bellard.org/tcc/


Permalink
ubm_techweb_disqus_sso_-62b85f243b970f215943aabb3025ea25
2013-10-05T20:06:28

Have you ever tried tcc? It compiles a reasonable subset of 'C' and you only need modify a yacc file.


Permalink


Video