Hey Philip,

The switch opcode is constant, and doesn't compare for each target.

But a C# switch can go linear, as:

switch (foo) {
case 42: return 1;
case 74: return 2;
case 96: return 3;
}

will be compiled as:

if (foo == 42) goto case_42;
if (foo == 74) goto case_74;
if (foo == 96) goto case_96;
goto default;

case_42: return 1;
case_74: return 2;
case_96: return 3;
default:

Other than that, you should be limited to Int32.MaxValue for the
number of switch targets.

On 4/17/09, Philip_L <[email protected]> wrote:
>
>  Thanks Jb. BTW, do you know if switch statements in IL are done in
>  linear or constant time? For example, if I have an a set of
>  instruction labels as jump targets in an array using the
>  OpCodes.Switch instruction and I already have the target index
>  computed at runtime, will it still have to do a comparison for each
>  case? And is there a limit on how many labels one can have in a switch
>  statement done in IL?
>
>
>  On Apr 18, 12:57 am, Jb Evain <[email protected]> wrote:
>  > Hey Philip,
>  >
>
> > On 4/6/09, Philip_L <[email protected]> wrote:
>  >
>  > >  switch(someString)
>  > >  {
>  > >  case "a":
>  > >  break;
>  > >  case "b":
>  > >  break;
>  > >  default:
>  > >  break;
>  >
>  > That's compiled using a Dictionary<>. A switch is quite complex
>  > actually. If you have:
>  > int a;
>  > switch (a) {
>  > case 0:
>  > case 1:
>  > case 12:
>  > case 74:
>  >
>  > }
>  >
>  > It's likely that only the case 0 and 1 will be compiled using a switch
>  > opcode, the rest will be compiled as a short serie of comparison. I
>  > suggest you write a few examples and see in ildasm what csc emits,
>  > it's quite interesting (I had to do that for Cecil.Decompiler, it's
>  > actually not that fun to reconstruct a switch statement from what is
>  > actually compiled).
>  >
>  > --
>
> > Jb Evain  <[email protected]>
>  >
>


-- 
Jb Evain  <[email protected]>

--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---

Reply via email to