Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code generation for switch statements #233

Closed
2 tasks done
adrianoc opened this issue May 3, 2023 · 0 comments
Closed
2 tasks done

Improve code generation for switch statements #233

adrianoc opened this issue May 3, 2023 · 0 comments
Labels
enhancement New feature or request 💗 good first feature a feature that is relatively easy to implement

Comments

@adrianoc
Copy link
Owner

adrianoc commented May 3, 2023

There are at least 2 potential improvements:

  • Better naming variables used to store the instructions representing the start of code for each case, i.e, instead of naming them Nop_[number], use something like CaseCode_[index]_[number] or Case_[index]_[number] or similar. This improves code readability

  • If there is a default case, do not emit unreachable code (See code below).

class Foo
{
    void Bar(int j)
    {
        switch(j)
        {
            case 1: System.Console.WriteLine(1); break;
            case 2: System.Console.WriteLine(2); break;
            default: System.Console.WriteLine(3); break;
        }
    }
}
//case 2: (condition)
il_Bar_2.Emit(OpCodes.Ldloc, lv_switchCondition_4);
il_Bar_2.Emit(OpCodes.Ldc_I4, 2);
il_Bar_2.Emit(OpCodes.Beq_S, Nop_7);

// Jumps to the *default* handling
il_Bar_2.Emit(OpCodes.Br, Nop_8); 

// Unreachable code jumping to the end of the switch. If there's a `default` case, this should not be emitted.
il_Bar_2.Emit(OpCodes.Br, Nop_5);
@adrianoc adrianoc added 💗 good first feature a feature that is relatively easy to implement enhancement New feature or request labels May 3, 2023
adrianoc added a commit that referenced this issue Jul 28, 2023
- use better label variable names for end of switch / each switch section
- does not emit a jump to the end of the switch at the last case before a 'default'
- adds individual switch sections to snippet / cecilified mapping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 💗 good first feature a feature that is relatively easy to implement
Projects
None yet
Development

No branches or pull requests

1 participant