3

I want to write a program which decomposes expressions (in a C code) in which they have function calls and extract each function call to variable. For example:

x = A() + B();

should be changed to :

a = A();
b = B();
x = a + b;

I'm writing it using Clang and RecursiveASTVisitor. Here is my solution. At first I have to look for all the functions and declare a variable for each of them at the first of my main block which all the calls are in. Then Look for Binary Operations which has function call in either sides. Then extract function calls and use variables for instead of them. As I'm a newbie to this I don't know if there is a better of doing this or is this solution works at all?

NEO
  • 2,145
  • 2
  • 26
  • 34
  • You'll have problems if a function is called more than once in an expression (unless that function always returns the same value). – didierc Nov 15 '14 at 13:39
  • 1
    You're right, good Point. So it's better that I generate a variable per function call. – NEO Nov 15 '14 at 13:44
  • Normally compilers may already do that decomposition at some point (when translating the AST to a more assembly like representation). See http://en.m.wikipedia.org/wiki/Static_single_assignment_form – didierc Nov 15 '14 at 13:49
  • If you know that your expression has already been computed somewhere before in the same block of instructions, you can reuse that value and avoid generating a new variable, but otherwise, yes, this is how you do it. – didierc Nov 15 '14 at 13:54
  • @didierc, No that's not what I'm looking for because I want to edit the code in in its high level form not in assembly or intermediate level. – NEO Nov 15 '14 at 18:41
  • I understand, I just wanted to point out that it is also performed at a different stage of compilation. I guess you just want to do some code transformation (as opposed to translation). – didierc Nov 15 '14 at 18:43

0 Answers0