1

What I want to do is 1. Parse C code to generate AST 2. Modify the AST 3. Compile the new AST without changing the .c source file.

Is there any tool I can use to do this? If not, is there any tool I can use to do modification on assembly code?

I looked at gcc-plugin, there is very little documentation on it, I can only figure out how to print information when the gcc is parsing the source code (e.g. variable types), but I don't know how I can modify them.

  • Does this answer your question? [Modification of the AST-tree of the GCC compiler](https://stackoverflow.com/questions/6962873/modification-of-the-ast-tree-of-the-gcc-compiler) – jonathan Apr 13 '20 at 19:31
  • Hey there. You'd need to make a plugin for the compiler for that. There's no standardized way for it. – jonathan Apr 13 '20 at 19:31

1 Answers1

0

Our DMS Software Reenginering Toolkit with its C front end is designed to do this.

DMS provides general parsing and transformation capability. The C Front End builds on top of DMS, knows many dialects of C, can parse them to ASTs including preserving in many cases preprocessor conditional.

One then uses DMS's rewrite capability to make changes to the AST. Rather than hacking at the tree nodes directly, one usually writes a rule conceptually of the form:

if (sourceCodePattern) and condition then replacementCodepattern

where sourceCodePattern and replacementCodePattern are fragments of C code with variable placeholders. An example:

 rule make_autoinc(l: lefthandside, e:expression):statement->statement
     " \l = \l + \e ; " ->  " \l++; ";

After all your rewrite rules have been applied, DMS can then prettyprint the modified source code back to a file, preserving indentation, number radix, comments etc as perfectly valid, compilable source code.

What you do with it after that is your business.

The C front end can also build symbol tables and construct control and data flow information, which is often really necessary to carry out desired changes.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341