How to save a tree in gcc intermediate representation? Is it possible?

114 views Asked by At

I want to save a tree (specifically the type of a tree) in a file in binary form, and I need to load this tree in other compilation unit. For example:

I have a main.c with 2 function:

myTypeStruct getWhatever(){
   myTypeStruct my;
   ... doSomething ...
   return my;
}

int main(){
    myTypeStruct my = getWhatever();
   ... doSomething else with my... 
}

and I want to save the type of the structure (myTypeStruct) and load it into another compilation unit involving a test.c as:

int main(){
   ... doSomeTest ...
}

With a gcc plugin, I want to load the type and build a variable of that type to transform test.c in something like:

int main(){
   myTypeStruct my;
   ... doSomething with my...
   ... doSomeTest ...
}

I know that a tree is a pointer to a tree_node, and a tree_node is a union of structures. The problem is that a tree has a relationship with it, and with a series of unintelligible structures. I need to know which data need a tree when building a variable of a specific type.

PD: There is insufficient documentation about how LTO do things like that. PD2: sorry for my English

1

There are 1 answers

2
Tom Tromey On

If I understand correctly, what you want to do is serialize the type and then, in a subsequent compilation, read it in using your plugin, to do some other transform.

There is no built-in support for this.

Maybe it could be done using the built-in pre-compiled header support. The core idea is to load the PCH in the compilation that invokes your plugin. So, you'd arrange to make a PCH holding the types you want to re-read, and then your plugin could simply look up the types by name.

Using LTO is tempting, because it has all the streaming support, but I think the LTO code at present is not very repurposeable.

Another approach is to write your own serializer. I happen to have done this for an experimental PCH plugin I wrote. Perhaps something like this would work for you.