Hi,

first, let me says that it is really nice to be able to use an open source C compiler with PIC microcontrollers :-)   Many thanks for this good job !

Now, less cool : if I am not wrong, the code generated from the program below is incorrect :

#pragma stack 0x200 255
#include <pic18fregs.h>

typedef unsigned char uchar;
typedef unsigned int uint;

typedef struct
    {
        unsigned :8;
        unsigned :8;
        uchar bDescIndex;                // For Configuration and String DSC Only
        uchar bDescType;                 // Device, Configuration, String, 
        uint wLanguageID;                // Language ID
        unsigned :8
        unsigned :8;
} StandardRequest;

volatile far StandardRequest __at (0x500) SetupBuffer;
static uint  num_bytes_to_be_send;

uchar main(void)
{   
    uchar unknown_request; 
    uchar i;   
 
    SetupBuffer.bDescType = 1;

    unknown_request = 0;

    switch(SetupBuffer.bDescType)
        {        
 
            case 1:
               num_bytes_to_be_send = 1;
               break;
            case 2:
                num_bytes_to_be_send = 1;
                break;
            case 3:
                num_bytes_to_be_send = 1;
                break;
            default:
                unknown_request = 1;
                break;
        }
    return !unknown_request;
}

The command line used is sdcc -mpic16  -p18f452 -c ep0.c -o ep0.o
and I am using the sdcc build from feb 20, 2006.

Here is the resulting code :

                          ;    .line    40; ep0.c    uchar main(void)
00002a   c000     movff    0, 0xfe5               MOVFF    r0x00, POSTDEC1
00002c   ffe5
00002e   c001     movff    0x1, 0xfe5             MOVFF    r0x01, POSTDEC1
000030   ffe5
                                           ;    .line    45; ep0.c    SetupBuffer.bDescType = 1;
000032   ee05     lfsr    0, 0x503                LFSR    0x00, (_SetupBuffer + 3)
000034   f003
000036   0e01     movlw    0x1                    MOVLW    0x01
000038   6eef     movwf    0xef, 0                MOVWF    INDF0
                                           ;    .line    51; ep0.c    unknown_request = 0;
00003a   6a00     clrf    0, 0                    CLRF    r0x00
                                           ;    .line    53; ep0.c    switch(SetupBuffer.bDescType)
00003c   ee05     lfsr    0, 0x503                LFSR    0x00, (_SetupBuffer + 3)
00003e   f003
000040   cfef     movff    0xfef, 0x1             MOVFF    INDF0, r0x01
000042   f001
000044   0e01     movlw    0x1                    MOVLW    0x01
000046   5c01     subwf    0x1, 0, 0              SUBWF    r0x01, W
000048   a0d8     btfss    0xd8, 0, 0             BTFSS    STATUS, 0
00004a   ef60     goto    0xc0                    GOTO    _00108_DS_
00004c   f000
00004e   0e04     movlw    0x4                    MOVLW    0x04
000050   5c01     subwf    0x1, 0, 0              SUBWF    r0x01, W
000052   b0d8     btfsc    0xd8, 0, 0             BTFSC    STATUS, 0
000054   ef60     goto    0xc0                    GOTO    _00108_DS_
000056   f000
000058   0eff     movlw    0xff                   MOVLW    0xff
00005a   2601     addwf    0x1, 0x1, 0            ADDWF    r0x01, F
00005c   c000     movff    0, 0xfe5               MOVFF    r0x00, POSTDEC1
00005e   ffe5
000060   c001     movff    0x1, 0xfe5             MOVFF    r0x01, POSTDEC1
000062   ffe5
000064   6a01     clrf    0x1, 0                  CLRF    r0x01
000066   3401     rlcf    0x1, 0, 0               RLCF    r0x01, W
000068   3601     rlcf    0x1, 0x1, 0             RLCF    r0x01, F
00006a   34e8     rlcf    0xe8, 0, 0              RLCF    WREG, W
00006c   3601     rlcf    0x1, 0x1, 0             RLCF    r0x01, F
00006e   0bfc     andlw    0xfc                   ANDLW    0xfc
000070   6e00     movwf    0, 0                   MOVWF    r0x00
000072   0e00     movlw    0                      MOVLW    UPPER(_00116_DS_)
000074   6efb     movwf    0xfb, 0                MOVWF    PCLATU
000076   0e00     movlw    0                      MOVLW    HIGH(_00116_DS_)
000078   6efa     movwf    0xfa, 0                MOVWF    PCLATH
00007a   0e8a     movlw    0x8a                   MOVLW    LOW(_00116_DS_)
00007c   2600     addwf    0, 0x1, 0              ADDWF    r0x00, F
00007e   5001     movf    0x1, 0, 0               MOVF    r0x01, W
000080   22fa     addwfc    0xfa, 0x1, 0          ADDWFC    PCLATH, F
000082   b0d8     btfsc    0xd8, 0, 0             BTFSC    STATUS, 0
000084   2afb     incf    0xfb, 0x1, 0            INCF    PCLATU, F
000086   5000     movf    0, 0, 0                 MOVF    r0x00, W
000088   6ef9     movwf    0xf9, 0                MOVWF    PCL
                                           _00116_DS_:
00008a   ef4b     goto    0x96                    GOTO    _00105_DS_
00008c   f000
00008e   ef52     goto    0xa4                    GOTO    _00106_DS_
000090   f000
000092   ef59     goto    0xb2                    GOTO    _00107_DS_
000094   f000
                                           _00105_DS_:
                                           ;    .line    57; ep0.c    num_bytes_to_be_send = 1;
000096   0e01     movlw    0x1                    MOVLW    0x01
000098   0100     movlb    0                      BANKSEL    _num_bytes_to_be_send
00009a   6f80     movwf    0x80, 0x1              MOVWF    _num_bytes_to_be_send, B
00009c   0100     movlb    0                      BANKSEL    (_num_bytes_to_be_send + 1)
00009e   6b81     clrf    0x81, 0x1               CLRF    (_num_bytes_to_be_send + 1), B
                                           ;    .line    58; ep0.c    break;
0000a0   ef62     goto    0xc4                    GOTO    _00109_DS_
0000a2   f000
                                           _00106_DS_:
                                           ;    .line    60; ep0.c    num_bytes_to_be_send = 1;
0000a4   0e01     movlw    0x1                    MOVLW    0x01
0000a6   0100     movlb    0                      BANKSEL    _num_bytes_to_be_send
0000a8   6f80     movwf    0x80, 0x1              MOVWF    _num_bytes_to_be_send, B
0000aa   0100     movlb    0                      BANKSEL    (_num_bytes_to_be_send + 1)
0000ac   6b81     clrf    0x81, 0x1               CLRF    (_num_bytes_to_be_send + 1), B
                                           ;    .line    61; ep0.c    break;
0000ae   ef62     goto    0xc4                    GOTO    _00109_DS_
0000b0   f000
                                           _00107_DS_:
                                           ;    .line    63; ep0.c    num_bytes_to_be_send = 1;
0000b2   0e01     movlw    0x1                    MOVLW    0x01
0000b4   0100     movlb    0                      BANKSEL    _num_bytes_to_be_send
0000b6   6f80     movwf    0x80, 0x1              MOVWF    _num_bytes_to_be_send, B
0000b8   0100     movlb    0                      BANKSEL    (_num_bytes_to_be_send + 1)
0000ba   6b81     clrf    0x81, 0x1               CLRF    (_num_bytes_to_be_send + 1), B
                                           ;    .line    64; ep0.c    break;
0000bc   ef62     goto    0xc4                    GOTO    _00109_DS_
0000be   f000
                                           _00108_DS_:
                                           ;    .line    66; ep0.c    unknown_request = 1;
0000c0   0e01     movlw    0x1                    MOVLW    0x01
0000c2   6e00     movwf    0, 0                   MOVWF    r0x00
                                           _00109_DS_:
                                           ;    .line    69; ep0.c    return !unknown_request;
0000c4   5000     movf    0, 0, 0                 MOVF    r0x00, W
0000c6   80d8     bsf    0xd8, 0, 0               BSF    STATUS, 0
0000c8   66e8     tstfsz    0xe8, 0               TSTFSZ    WREG
0000ca   90d8     bcf    0xd8, 0, 0               BCF    STATUS, 0
0000cc   6a00     clrf    0, 0                    CLRF    r0x00
0000ce   3600     rlcf    0, 0x1, 0               RLCF    r0x00, F
0000d0   5000     movf    0, 0, 0                 MOVF    r0x00, W
0000d2   cfe4     movff    0xfe4, 0x1             MOVFF    PREINC1, r0x01
0000d4   f001
0000d6   cfe4     movff    0xfe4, 0               MOVFF    PREINC1, r0x00
0000d8   f000
0000da   0012     return    0                     RETURN   

As you can see, unknown_request is stored in r0x00. It looks like it is overwritten during the switch computations. The return value can then be wrong...
Turning this automatic variable into a static one solves the problem.


Pierre