#733 Incorrect code generation calling variable argument function

closed-fixed
Bernhard Held
5
2013-05-25
2004-04-01
Anonymous
No

If a called function has a variable number of arguments
and one of the arguments is the result of an inline
function call (see below), and the code is compiled
with the --stack-auto option, sdcc generates the wrong
assembly code for the call. The parameters are passed
the wrong way around and (possibly, didn't investigate
fully and probably not apparent in this small example)
one is overwritten with the other during the start of
the execution of the inline function. This is the
smallest test case I could make:

unsigned char foo(unsigned char a, ...) { return a; }
unsigned char bar(unsigned char a, unsigned char b) {
return a + b; }

void main(void) { foo(0, bar(1, 2)); }

If you compile using "sdcc --stack-auto" and inspect
the resulting .asm file you will see this:

;------------------------------------------------------------
;Allocation info for local variables in function 'main'
;------------------------------------------------------------
;------------------------------------------------------------
;sdcctest.c:4: void main(void) { foo(0, bar(1, 2)); }
; -----------------------------------------
; function main
; -----------------------------------------
_main:
push _bp
mov _bp,sp
; genIpush
mov a,#0x01
push acc
; genCall
mov dpl,#0x02
lcall _bar

"sdcc -v" gives:

SDCC :
mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08
2.4.1 (Mar 30 2004) (CYGWIN)

You can contact me at samghost@mpx.net for further
correspondence. Thanks,

--
Sam Brightman

Discussion

  • Bernhard Held
    Bernhard Held
    2004-04-11

    Logged In: YES
    user_id=203539

    Fixed in SDCCast.c 1.234

     
  • Bernhard Held
    Bernhard Held
    2004-04-11

    • labels: --> C-Front End
    • milestone: --> fixed
    • assigned_to: nobody --> bernhardheld
    • status: open --> closed-fixed