xC: function pointers?

If you have a simple question and just want an answer.
Post Reply
Endre
Active Member
Posts: 38
Joined: Fri Jan 01, 2016 10:13 am

xC: function pointers?

Post by Endre »

Hello,

Is it possible to declare function pointer in xC?

I have the following code:

Code: Select all

typedef struct {
    ComType retType;
    char * unsafe name;
    uint8 numOfArgs;
    ComArg * unsafe args;
    void (* unsafe handler)(ComContext & ctx, client interface ComRxIf rxIf, client interface ComTxIf txIf);
} ComFunc;
For this I get this error:

Code: Select all

.././inc/Shell.h:45:5: error: cannot declare pointer to function
    void (* unsafe handler)(ComContext & ctx, client interface ComRxIf rxIf, client interface ComTxIf txIf);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


peter
XCore Addict
Posts: 230
Joined: Wed Mar 10, 2010 12:46 pm

Post by peter »

That is correct, XC does not support function pointers. However, there is nothing to prevent you from casting them to a different type and then using a c-wrapper to call a function pointer. For example:

main.xc:

Code: Select all

void * unsafe get_f_pointer();
void call_f_pointer(void * unsafe x);

int main()
{
  void * unsafe x = get_f_pointer();
  call_f_pointer(x);
  return 0;
}
f_pointer.c

Code: Select all

#include <print.h>

typedef void (*f_ptr)(void);

static void g()
{
  printstrln("Called g()\n");
}

void *get_f_pointer()
{
  return (void*)g;
}

void call_f_pointer(void *f)
{
  ((f_ptr)f)();
}
Compile:

Code: Select all

xcc -target=XCORE-200-EXPLORER main.xc f_pointer.c -o test.xe
Run:

Code: Select all

xsim test.xe
Called g()
Endre
Active Member
Posts: 38
Joined: Fri Jan 01, 2016 10:13 am

Post by Endre »

Thanks for the hint.
Is it also possible to map the client interface function arguments to C?
User avatar
data
Active Member
Posts: 43
Joined: Wed Apr 06, 2011 8:02 pm

Post by data »

Endre wrote:Thanks for the hint.
Is it also possible to map the client interface function arguments to C?
You can pass them to C/C++ as unsigned ints, and you can pass them back to XC in the same way. <xccompat.h> provides macros for doing that.
Endre
Active Member
Posts: 38
Joined: Fri Jan 01, 2016 10:13 am

Post by Endre »

I couldn't yet figure out how to get the address of an xC function.

Code: Select all

static unsafe void putFirst(void * unsafe vctx, uint8 value);
static unsafe void put(void * unsafe vctx, uint8 value);
...
    CzokDecoder_init(&ctx.dec, &dCtx, &putFirst, &put);
...
For this I got compile errors:

Code: Select all

../src/ComRxMUX.xc:64:39: error: cannot take address of function
    CzokDecoder_init(&ctx.dec, &dCtx, &putFirst, &put);
                                      ^~~~~~~~~
../src/ComRxMUX.xc:64:50: error: cannot take address of function
    CzokDecoder_init(&ctx.dec, &dCtx, &putFirst, &put);
                                                 ^~~~
Is there a way to get the function address?
Endre
Active Member
Posts: 38
Joined: Fri Jan 01, 2016 10:13 am

Post by Endre »

Since I feel that a C like language without function pointers is like a fast running rabbit without legs, I have tried to organize my code into 2 parts: 1. C part contains everything without IPC, event handling and core assignments, 2. xC part which contains IPC, event handling and core assignments.

This approach has been failed when I wanted to send a notification from C code via this extern xC function:

Code: Select all

void sendPacketReady(uint comRxIf) {
    server interface ComRxIf rxIf = (server interface ComRxIf)comRxIf;
    rxIf.pakcetReady();
}
The error:

Code: Select all

../src/ComRxMUX.xc:26:37: error: cast specifies resource type
    server interface ComRxIf rxIf = (server interface ComRxIf)comRxIf;
                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Is it possible to develop XMOS application in raw C? Is there an API for that?
With that I'd loose the comfort and safety of xC and also loose its limitations.
y0lo
Member
Posts: 12
Joined: Tue Sep 08, 2015 8:47 am

Post by y0lo »

Double the question.
Is there any way to work with the clean C?

xC has the next fatal limitations:
1. Compatibility with C-libraries
2. Pointers! Do xC developers believe that I'm so stupid for this? Guys...
3. Packed structures. No comments...
...
Endre
Active Member
Posts: 38
Joined: Fri Jan 01, 2016 10:13 am

Post by Endre »

y0lo wrote:Double the question.
Is there any way to work with the clean C?
...
Check this topic. It looks promising.
User avatar
BEBDigitalAudio
Experienced Member
Posts: 82
Joined: Thu Nov 11, 2010 7:45 pm

Post by BEBDigitalAudio »

y0lo wrote:Double the question.
Is there any way to work with the clean C?

xC has the next fatal limitations:
1. Compatibility with C-libraries
2. Pointers! Do xC developers believe that I'm so stupid for this? Guys...
3. Packed structures. No comments...
...
Sorry to say that, but you probably do not understand why XC is made in that way. The rules that XC follows are the same as in Java or Ada. Pointers exist in XC, Java or Ada, but you can't play with them directly for security reasons.
And about the compatibility with C libraries... sorry, but XC is compatible with C and C++. All the programs I write for our XMOS based platform are mixing XC and C, and this works greatly. I have plenty of C libraries mixed in my application and I never got any problems with that
Post Reply