0

Does anyone know of any way (compiler flags/tools) to automatically alter a piece of C source code to change the common arithmetic multi-operand operations into two-operand ones?

i.e:

(a = b + c * d - e )  ---becomes-->   
x = c * d  
y = x - e  
a = b + y
ginsunuva
  • 21
  • 1
  • 4
  • 2
    It is not clear what you're asking. Are you looking for a C code to achieve your goal? – CroCo Oct 03 '14 at 04:14
  • Looking for any tool to automatically do it – ginsunuva Oct 03 '14 at 05:09
  • Do you realize when you say `automatically`, you implicitly include all possible ways of writing arithmetic equations according to C standard. Do you see the problem? – CroCo Oct 03 '14 at 05:59
  • @CroCo: Seems perfectly clear what he is asking to do. He want his program automatically revised to meet his conditions for arithmetic. Yes, all possible permutations of the operands might be possible, but a reasonable man would probably accept break them up left-to-right according to operator precedence. (Compilers do exactly this internally, when building so-called "triples"). – Ira Baxter Oct 03 '14 at 06:33

1 Answers1

0

Yes. You want a program transformation system (PTS). These are designed to parse code, apply code-shuffling operations defined in terms of syntax, and then regenerate source code with the changes. They tend to use ASTs to ensure that there is no problem misinterpreting text.

To do this right, you have convince the tool to pick you your operand pairs, compute the type of the subexpression, and manufacture temporary variables with the appropriate types. And it has to be able to parse and analyze C, which is rather hard to parse esp. if you include preprocessor conditionals and macros. Not many PTS are capable of this. Practically nothing else is capable at all.

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