Menu

#2595 z80: smallc and _z88dk_callee c functions not flagged as error during compile

closed-fixed
None
Z80
5
2017-04-10
2017-03-24
alvin
No

sdcc #9852 (MINGW64)

sdcc is able to call smallc and z88dk_callee functions that are written in assembly language but it is not able to compile c functions using these linkages. Instead, sdcc silently ignores these attributes and produces standard c linkage code.

Unfortunately this can lead to a lot of hard to find errors in user code.

Here's an example using qsort where sdcc is interfacing with a c library written in assembly language using smallc calling convention:

#define NUM 5000

typedef unsigned int size_t;

unsigned int numbers[NUM];

extern void qsort(void *base,size_t nmemb,size_t size,void *compar) __smallc;

int ascending_order(int *a, int *b) __smallc
{
   return *a - *b;
}

void main(void)
{
   qsort(numbers, NUM, sizeof(int), ascending_order);
}

sdcc -mz80 -S test.c

sdcc correctly generates a smallc call to qsort() but the smallc attribute on ascending_order() is ignored and ascending_order() is compiled using standard linkage. qsort calls ascending_order() assuming smallc linkage which effectively causes the a,b parameters to be reversed and instead of an ascending order sort, a descending order sort occurs. In other situations there can be program crashes instead.

A similar situation exists with z88dk_callee. sdcc cannot compile c functions that are declared z88dk_callee but instead of generating an error, it silently ignores this and produces a standard function that will lead to crashes during execution.

Both cases should result in compile errors rather than silently generating incorrect code.

The original issue came up in z88dk's small c library:
https://github.com/z88dk/z88dk/issues/127

Discussion

  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    The error messages have been implemented in revision [r9867]. In revision [r9868], support for the Small-C calling convention in callees has been implemented.

    However, support for variable arguments in functions using the Small-C calling convention is not implemented. And for a callee, there is no corresponding error message either.
    There is also no support for using the Small-C calling convention with an xstack callee (but there is an error message now).

    Philipp

     

    Last edit: Maarten Brock 2017-05-14
  • alvin

    alvin - 2017-04-10

    In revision #9868, support for the Small-C calling convention in callees has been implemented.

    I was under the impression this was already working or at least it seemed like it was!

     
    • Philipp Klaus Krause

      This bug report was about it not working (and there not being an error message). And indeed, previously, Small-C calling convention was supported on the caller side, but not the callee side (as documented in the manual).
      Small-C support was for calling legacy asm functions that used Small-C calling. Now there is also support for function definition with Small-C calling convention.

      Philipp

       

Log in to post a comment.