The 6809 CPU core from the Vectrex emulator I'm porting has some code that I think may be slowing it down, something like:-
switch(number)
{
case 0: case1: case 2: case 3: case 4:
case 5: case6: case 7: case 8: case 9:
case 10: case11: case 12: case 13: case 14:
case 15: case16: case 17: case 18: case 19:
case 20: case21: case 22: case 23: case 24:
case 25: case26: case 27: case 28: case 29:
/*do some stuff */
break;
case 30: case 31: case 32....
/*and so on*/
}
I'm wondering how the compiler will handle this switch statement.. I'm a lousy asm programmer, so bare with me, but I'm guessing that a switch statement is normally compiled into something a little like this:-
--*pseudo assembly*--
mov r0, number
mov r1, switchstatement
mov r2, [r0 + r1]
switchstatement:
@dcd 1,2,3,4,5,6
1:
;code here
2:
;code here
..etc
Which is fine and dandy. As far as I can tell the switch statement from the Vectrex emulator can be assembled to one of two things:-
1) it can be expanded out, eg:-
case 1: case 2: case 3:
/* code here */
break;
becomes:-
1:
;code here
2:
;code here (same code as 1)
3:
;code here (same code as 1 and 2)
Which will result in a massive jump table, and so a bigger binary, but the performance will be alright (right?).
2) something like:-
mov r0, number
cmp r0, $1
beq 1
cmp r0, $2
beq 1
cmp r0, $3
beq 1
1:
;code here
...Which would be a performance nightmare!
So any ideas on how the compiler would handle this, whether it's worth changing, etc?
Thanks
Robert
switch(number)
{
case 0: case1: case 2: case 3: case 4:
case 5: case6: case 7: case 8: case 9:
case 10: case11: case 12: case 13: case 14:
case 15: case16: case 17: case 18: case 19:
case 20: case21: case 22: case 23: case 24:
case 25: case26: case 27: case 28: case 29:
/*do some stuff */
break;
case 30: case 31: case 32....
/*and so on*/
}
I'm wondering how the compiler will handle this switch statement.. I'm a lousy asm programmer, so bare with me, but I'm guessing that a switch statement is normally compiled into something a little like this:-
--*pseudo assembly*--
mov r0, number
mov r1, switchstatement
mov r2, [r0 + r1]
switchstatement:
@dcd 1,2,3,4,5,6
1:
;code here
2:
;code here
..etc
Which is fine and dandy. As far as I can tell the switch statement from the Vectrex emulator can be assembled to one of two things:-
1) it can be expanded out, eg:-
case 1: case 2: case 3:
/* code here */
break;
becomes:-
1:
;code here
2:
;code here (same code as 1)
3:
;code here (same code as 1 and 2)
Which will result in a massive jump table, and so a bigger binary, but the performance will be alright (right?).
2) something like:-
mov r0, number
cmp r0, $1
beq 1
cmp r0, $2
beq 1
cmp r0, $3
beq 1
1:
;code here
...Which would be a performance nightmare!
So any ideas on how the compiler would handle this, whether it's worth changing, etc?
Thanks
Robert