We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
highlighted code code is being generated by the compiler for all switch statements when the level of optimization is lower than Level 4, that DOES NOT EXECUTE. i'm not concerned with eliminating it, which is obviously accomplished by increasing the level of optimization, but i need to know why it's there?????? eg. This is an example of the disassembly associated with a switch statement when optimization set at Level 1...
switch (x) C:0x0013 E508 MOV A,0x08 C:0x0015 120039 LCALL C?CCASE(C:0039) C:0x0018 00 NOP C:0x0019 36 ADDC A,@R0 C:0x001A 00 NOP C:0x001B 00 NOP C:0x001C 36 ADDC A,@R0 C:0x001D 00 NOP C:0x001F 00 NOP C:0x0021 22 RET { case 0: do something case 1: do something .....etc. default: }
It is quite common among compilers for the "basic" code generator to just produce code with no thought for efficiency; it is left to the optimiser so sort out the clever stuff - including dead-code removal.
irony Level 1 is dead code elimination.... this thing inserted dead code when generating every switch statement. does anyone have a better explanation than "compilers produce dead code". i have to account for all unexecuted instructions...
Actually, that is NOT dead code at all. But, it takes a complete example to see the MAGIC behind what's going on. For example, the following function:
int func (unsigned char x) { switch (x) { case 0: return (1); case 1: return (1); case 2: return (3); case 6: return (3); case 9: return (3); case 12: return (3); case 16: return (3); case 27: return (3); case 28: return (3); case 39: return (3); case 45: return (3); default: return (5); } }
; FUNCTION _func (BEGIN) ; SOURCE LINE # 1 ;---- Variable 'x' assigned to Register 'R7' ---- ; SOURCE LINE # 2 ; SOURCE LINE # 3 0000 EF MOV A,R7 0001 120000 E LCALL ?C?CCASE 0004 0000 R DW ?C0002 0006 00 DB 00H 0007 0000 R DW ?C0004 0009 01 DB 01H 000A 0000 R DW ?C0012 000C 02 DB 02H 000D 0000 R DW ?C0012 000F 06 DB 06H 0010 0000 R DW ?C0012 0012 09 DB 09H 0013 0000 R DW ?C0012 0015 0C DB 0CH 0016 0000 R DW ?C0012 0018 10 DB 010H 0019 0000 R DW ?C0012 001B 1B DB 01BH 001C 0000 R DW ?C0012 001E 1C DB 01CH 001F 0000 R DW ?C0012 0021 27 DB 027H 0022 0000 R DW ?C0012 0024 2D DB 02DH 0025 0000 DW 00H 0027 0000 R DW ?C0014 ; SOURCE LINE # 4 ; SOURCE LINE # 5 0029 ?C0002: ; SOURCE LINE # 6 0029 8000 SJMP ?C0015 ; SOURCE LINE # 7 002B ?C0004: ; SOURCE LINE # 8 002B ?C0015: 002B 7E00 MOV R6,#00H 002D 7F01 MOV R7,#01H 002F 22 RET ; SOURCE LINE # 9 0030 ?C0005: ; SOURCE LINE # 10 0030 800E SJMP ?C0023 ; SOURCE LINE # 11 0032 ?C0006: ; SOURCE LINE # 12 0032 ?C0016: 0032 800C SJMP ?C0023 ; SOURCE LINE # 13 0034 ?C0007: ; SOURCE LINE # 14 0034 ?C0017: 0034 800A SJMP ?C0023 ; SOURCE LINE # 15 0036 ?C0008: ; SOURCE LINE # 16 0036 ?C0018: 0036 8008 SJMP ?C0023 ; SOURCE LINE # 17 0038 ?C0009: ; SOURCE LINE # 18 0038 ?C0019: 0038 8006 SJMP ?C0023 ; SOURCE LINE # 19 003A ?C0010: ; SOURCE LINE # 20 003A ?C0020: 003A 8004 SJMP ?C0023 ; SOURCE LINE # 21 003C ?C0011: ; SOURCE LINE # 22 003C ?C0021: 003C 8002 SJMP ?C0023 ; SOURCE LINE # 23 003E ?C0012: ; SOURCE LINE # 24 003E ?C0022: 003E 8000 SJMP ?C0023 ; SOURCE LINE # 25 0040 ?C0013: ; SOURCE LINE # 26 0040 ?C0023: 0040 7E00 MOV R6,#00H 0042 7F03 MOV R7,#03H 0044 22 RET ; SOURCE LINE # 27 0045 ?C0014: ; SOURCE LINE # 28 0045 7E00 MOV R6,#00H 0047 7F05 MOV R7,#05H ; SOURCE LINE # 29 ; SOURCE LINE # 30 0049 ?C0003: 0049 22 RET ; FUNCTION _func (END)
0004 0000 R DW ?C0002 0006 00 DB 00H
0007 0000 R DW ?C0004 0009 01 DB 01H
this is plainly obvious when the file is translated using the #pragma src directive. thank you jon