-1

If I have such a structure:

struct test{
   float c,
         f,
         ops;
};

How can I modify the GCC compiler source code to make it as follows:

struct test{
  double c,
         f,
         ops;
};

I now have such a requirement, I need to modify the gcc source code so that when he compiles a structure of a certain mode, he changes its type to a specified type.

Thank you!

zhugl
  • 1
  • 2
  • 2
    Hello and welcome! Can you tell us some more about your situation? There is almost surely an easier way than modifing gcc. – g_bor Sep 20 '20 at 12:50
  • Please explain in written English why do you consider this approach. For what employer? For what kind of software? Is it coded in C or in C++ ? – Basile Starynkevitch Sep 20 '20 at 13:04
  • Mainly use C language to code, I think it may take 2-3 months to complete this task. The specific details about the work are not convenient to explain in detail, thank you! – zhugl Sep 20 '20 at 13:16
  • **In 3 months you are sure to fail.** Consider starting right now looking for a new job. And if you cannot give details, you'll need more than a full year. I do speak by bitter experience. – Basile Starynkevitch Sep 20 '20 at 13:18
  • Well, maybe I am not familiar with the difficulty of the content of this task. I thought it was just to get the data type of the field when the gcc front-end parsed the struct and make changes.very thank you. – zhugl Sep 20 '20 at 13:23
  • Have fun trying (then failing). Feel free to email me. I voted to close your question, since it is lacking details and motivations. If your code base is less than 100KLOC, your approach is not worth the efforts. It smells badly as some [XY problem](https://en.wikipedia.org/wiki/XY_problem). Please explain what is your actual problem – Basile Starynkevitch Sep 20 '20 at 13:24

1 Answers1

2

your goal is very ambitious!

A possible approach could be to develop your own GCC plugin doing that job.

My recommendations:

  • budget several months of your time for that work (and perhaps several years) - at least 6 months full time to get a "proof of concept" thing which would fail on most code bases (in C). For C++, add another full year.

  • read carefully the C11 standard n1570 (if you target C), and the C++11 standard n3337 (if you target C++). That effort alone may take you a full month.

  • make your plugin open source software, and put its code quickly (e.g. under LGPL license) on some repository such as github or gitlab.

  • target the latest available version of GCC. In September 2020, that means GCC 10. Plugins and GCC APIs are changing incompatibly from one version to the next. If you need to stick to GCC 8 specifically, be prepared to spend a big amount of money for companies like AdaCore.

  • read carefully the documentation on GCC internals. You need to understand the GENERIC representation.

  • study very carefully the gcc/tree.def and gcc/treestruct.def and gcc/gimple.def files of GCC source code. You need to basically understand every line in them.

  • study very carefully the gcc/passes.def file of GCC source code. Again, you need to understand every line in that file.

  • learn to compile GCC from its source code. You certainly want to build it with g++ -Wall -Wextra -g -O1

  • read this draft report.

  • ask help in written English on the gcc@gcc.gnu.org mailing list, but have some working plugin before.

  • consider making a PhD out of this work. It is worth one. Alexandre Lissy in France got a PhD on a very similar topic.

If your code base is in C, consider using Frama-C (or Clang) and design your tool as a C to C transpiler.

Perhaps clever preprocessor tricks like #define float double followed by #undef float might be enough.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • You said that there are some effective plugins before. I don't know much about this part. Where can I see the related plugins? Thank you! – zhugl Sep 20 '20 at 12:58
  • I do know that Huawei research lab in Paris is working on similar stuff. That means a team of ten PhD level software developers in central Paris. So a budget above 1M€ (or a million US$). Please check your budget before starting to work. – Basile Starynkevitch Sep 20 '20 at 13:09
  • Feel free to contact me by email to `basile@starynkevitch.net` but do mention the URL of your question in your email, and the context of your work (which seems **very ambitious** ...) – Basile Starynkevitch Sep 20 '20 at 13:13