-6

I am trying to generate multiple programs from a given C/C++ program with loops unrolled. For example, I would like to convert something like this:

i = 0;
while (i < 5) {
    printf("Hello");
    i++;
}

to this:

i = 0;
printf("Hello");
i++;
while (i < 5) {
    printf("Hello");
    i++;
}

In the above example, I'd like to generate 5 programs each with a different number of loops unrolled. The compiler flags that I have looked at so far don't seem to generate a program but just optimize it. Is there any straightforward automated way of doing this?

Edit: I don't understand why this is being downvoted. The question linked as being a duplicate isn't relevant.

Amit Rege
  • 37
  • 4

2 Answers2

0

Is there any straightforward automated way of doing this?

This is an unusual requirement, as such I think you will have a really difficult time to find a tool that does this.

Without knowing what you are trying to achieve, what problem you are trying to resolve we can't give you a solution, other than manual unroll:

printf("Hello");
printf("Hello");
printf("Hello");
printf("Hello");
printf("Hello");
bolov
  • 72,283
  • 15
  • 145
  • 224
0

If the programs you want to change are all written by yourself and you know they're structure is simple, you may get along witha little magic of regular expressions.

For modifying arbitrary programs reliably, you must use a proper parser, detect constant for loops in the abstract syntax tree and then write back the modified syntax tree.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121