Control to Goto Statement

From emmtrix Wiki
Jump to navigation Jump to search

Control to Goto is a transformation that replaces loops, conditionals, and lazy evaluation operators with goto statements while preserving program logic. This restructuring produces C code that resembles low-level assembly, aiding performance analysis and optimization. Though goto is rarely used in modern coding, this transformation benefits compiler optimizations and static analysis.

Control to Goto Statement Transformation in emmtrix Studio

emmtrix Studio can implement Control to Goto statement using #pragma directives or via the GUI. Control to Goto is a transformation that replaces all control statements and all lazy evaluation operators with goto statements and other code lines needed to preserve the code semantics. Control statements include loop statements (for, while, do while) and choice statements (if, switch). Lazy evaluation operators include statements with logical operators (e.g. &&, ||) and statements with ternary operators (?:).

Typical Usage and Benefits

The most common reason for the application of the transformation is the generation of C code with a structure closer to the structure of assembly code. This allows for a more accurate performance estimation of the generated code.

Example

/* The following code tests control to Goto transformation applied to main function.
 * In the given example, labels while1_start and while1_end are created in order for goto statements
   to be able to navigate. 
 */   

#pragma EMX_TRANSFORMATION ControlToGoto
int main(void) {
    int i = 0;
    while (i < 3) {
        printf(
            while % d\ n, i);
        i++;
    }
    return 0;
}
/* The following code is the generated code after the transformation has been applied.
 */

int main(void) {
    int i = 0;
    while1_start: ;
    if (!(i < 3)) goto while1_end;
    printf(
        while % d\ n, i);
    i = i + 1;
    goto while1_start;
    while1_end: ;
    return 0;
}