Playing with gcc's intermediate GIMPLE format
Asked Answered
M

4

18

According to this article gcc uses several intermediate formats before generating code. I read that the GIMPLE format uses three address code, which seems to be the easiest intermediate language to use. But I need some more detail, as I need to build a tool that can take the intermediate code and insert some code to it before generating the final code.

For this I first need to know how can I even generate the GIMPLE format code and save it in a file. So I'm looking for some documents and examples. Also, if anyone has worked with such things, can I know the complexity of this task, which is to insert some code into the intermediate code?

Morvin answered 1/2, 2012 at 16:26 Comment(4)
This is one link, gcc.gnu.org/wiki/GimpleFrontEndMorvin
A good example is at gcc.gnu.org/onlinedocs/gcc-4.3.4/gccint/GIMPLE-Example.htmlMorvin
if you are looking for a way to generate code, you might look at llvm. At least it is much better documented.Leix
Related: #1496997Kiddy
S
20

You might find it easier to write a plugin for GCC, which would allow you to hook the GIMPLE generation and alter it inside GCC, which should drop the downtime of saving, editing then trying to compile from GIMPLE form. MELT is one such plugin (though it offers way more than just altering the lower level representations). There is also a nice PDF here on GIMPLE altering plugins.

Else, you can look here for information on how GCC's GIMPLE works. In terms of dumping GIMPLE out:

You can request to dump a C-like representation of the GIMPLE form with the flag -fdump-tree-gimple.

Sunken answered 1/2, 2012 at 17:1 Comment(3)
I'm the main developer of MELT, and I have added a few days ago new tutorial slides on gcc-melt.org/GCC-MELT-HiPEAC2012.pdf (there are also other stuff of gcc-melt.org ...)Melon
gcc-melt.org is dead, new link is starynkevitch.net/basile/gcc-melt/GCC-MELT-HiPEAC2012.pdfScrip
@Scrip pitty the site went down, I have updated with your link, though next time just (propose an) edit to my answer :)Sunken
P
15

You can easily generate GIMPLE representation of any file using the flag -fdump-tree-gimple.

If you want to write a plugin, then you might be interested in how passes work on GCC. You can see the output of each pass with flags of the form:

-fdump-<ir>-<passname>

where ir could be:

  • tree : Intraprocedural passes on GIMPLE
  • ipa : Interprocedural passes on GIMPLE
  • rtl : Intraprocedural passes on RTL

Use <passname> = all to see all the dumps, e.g. -fdump-ipa-all.

Publia answered 13/7, 2012 at 12:23 Comment(0)
D
1

I tried the flag -fdump-tree-gimple. It works only for the C/C++ language, and not for other languages such as Java, Ada, Fortran, and Objective-C)

Deon answered 18/9, 2012 at 18:12 Comment(1)
The question is tagged C. So the other languages are of no relevance.Tumultuous
I
1

It is now possible, since GCC 7, to use __GIMPLE in a function's declaration specifiers, along with the -fgimple command-line flag, to have GCC directly compile GIMPLE. This is explained in more detail in the documentation for GCCs internals

Similar infrastructure is also available for RTL

Introrse answered 30/1, 2024 at 0:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.