Menu

#963 Split switch statements that contain contigous and single values

open
nobody
None
5
2024-11-21
2024-11-21
Under4Mhz
No

Would it be possible to optimize switch statements by splitting them into up into groups of multiple switch statements.

If I have a switch statement of 0 to 6 contiguous, sdcc will use a jump table.

If I have a switch statement of 0 to 6 and 12, sdcc will create asm that will step through each item individually.

For the second case, where I have a switch statement of 0 to 6 and 12, would it be possible to split these into two switch statements internally. One switch with 0 to 6, and a second with 12.

This is a simple case. There's likely to be multiple contiguous and single items in a switch statement.

Doing this manually can be a bit impractical, since I'm usually using enums who's order isn't obvious or who's order may change over time.

switch1 below will be nicely optimised into a jump table:

void switch1(int in) {

    int out = 99;
    switch( in ) {

        case 0:
            out = 0;
            break;
        case 1:
            out = 1;
            break;
        case 2:
            out = 2;
            break;
        case 3:
            out = 3;
            break;
        case 4:
            out = 4;
            break;
        case 5:
            out = 5;
            break;
        case 6:
            out = 6;
            break;
        default:
            break;
    }

    printf( "result is %d\n", out );
}

switch2 will be a long list of or's and sub's

void switch2(int in) {

    int out = 99;
    switch( in ) {

        case 0:
            out = 0;
            break;
        case 1:
            out = 1;
            break;
        case 2:
            out = 2;
            break;
        case 3:
            out = 3;
            break;
        case 4:
            out = 4;
            break;
        case 5:
            out = 5;
            break;
        case 6:
            out = 6;
            break;
        case 12:
            out = 12;
            break;
        case 18:
            out = 18;
            break;
        default:
            break;
    }

    printf( "result is %d\n", out );
}

When I write the code of switch2, I'd like sdcc to automatically and internally create the same assembly as switch3 would do.

// I write the code for switch2, but sdcc will generate the code identical to switch3
void switch3(int in) {

    int out = 99;
    switch( in ) {

        case 0:
            out = 0;
            break;
        case 1:
            out = 1;
            break;
        case 2:
            out = 2;
            break;
        case 3:
            out = 3;
            break;
        case 4:
            out = 4;
            break;
        case 5:
            out = 5;
            break;
        case 6:
            out = 6;
            break;
    }

    switch( in ) {

        case 12:
            out = 12;
            break;
        case 18:
            out = 18;
            break;
        default:
            break;
    }

    printf( "result is %d\n", out );
}

Related

Feature Requests: #964

Discussion


Log in to post a comment.

MongoDB Logo MongoDB