Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing with gcc's intermediate GIMPLE format

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?

like image 567
MetallicPriest Avatar asked Feb 01 '12 16:02

MetallicPriest


People also ask

What is gimple?

GIMPLE is a three-address representation derived from GENERIC by breaking down GENERIC expressions into tuples of no more than 3 operands (with some exceptions like function calls).

What is gimple in open source?

GIMPLE is a simplified GENERIC, in which various constructs are lowered to multiple GIMPLE instructions. The C, C++, and Java front ends produce GENERIC directly in the front end. Other front ends instead have different intermediate representations after parsing and convert these to GENERIC.

What is intermediate representation in compiler design?

An intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An IR is designed to be conducive to further processing, such as optimization and translation.


2 Answers

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.

like image 104
Necrolis Avatar answered Sep 18 '22 08:09

Necrolis


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.

like image 22
neel Avatar answered Sep 21 '22 08:09

neel