RedditPP Mascot

RedditPP

r/Compilers • … • u/smog_alado

Converting C "basic blocks" into structured code

I have a compiler that emits C code in "basic blocks":

B1: print("before"); x = 10; goto B2;

B2: if (x <= 0) goto B4 else goto B3;

B3: print("looping") x = x - 1; goto B2;

B4: print("after"); return 0;

When I debug the generated C, sometimes I wish it would appear in a more structured style:

print("before"); x = 10; while (x >= 0) { print("looping"); x = x - 1; } print("after"); return 0;

Is there by any chance some existing tool that can do this for me? Some C->C transpiler that turns gotos into structured code? I'd prefer to not have to implement this myself if I could get away with it.

— 0