Page 1 of 1
xC: function pointers?
Posted: Sat Apr 09, 2016 3:27 pm
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);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Re: xC: function pointers?
Posted: Mon Apr 11, 2016 1:44 pm
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:
Re: xC: function pointers?
Posted: Tue Apr 12, 2016 8:11 am
by Endre
Thanks for the hint.
Is it also possible to map the client interface function arguments to C?
Re: xC: function pointers?
Posted: Tue Apr 12, 2016 8:11 am
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.
Re: xC: function pointers?
Posted: Mon Apr 25, 2016 6:00 pm
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?
Re: xC: function pointers?
Posted: Tue Apr 26, 2016 3:37 pm
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.
Re: xC: function pointers?
Posted: Tue Jul 26, 2016 11:45 am
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...
...
Re: xC: function pointers?
Posted: Tue Jul 26, 2016 11:45 am
by Endre
y0lo wrote:Double the question.
Is there any way to work with the clean C?
...
Check this
topic. It looks promising.
Re: xC: function pointers?
Posted: Fri Oct 27, 2017 6:30 pm
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