Problems with function pointers & nstackwords

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Problems with function pointers & nstackwords

Post by Interactive_Matter »

I am trying to migrate some C code from my XC-1 to the XC-2 funnily enough it will not link at all! It always tells me
../src/main.xc:(.text+0x80): Error: Meta information ("bnj_parse.nstackwords") for function "bnj_parse" cannot be determined.
../src/main.xc:(.text+0x80): Error: lower bound could not be calculated (function is recursive?).
In that specific function a function pointer is used. But it really puzzles me that it compiles fine on XC-1 (in a different project).
What am I missing? Can anybody give me a pointer?

Thanks

Marcus


Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

XC does not support pointers. Therefore you cannot have function pointers in your XC code.

If that code is to be compiled as C I would guess it should live in a file with a .c extension so that the correct compiler is run over it.

But then again, just guessing, the scenario goes like this: If a function, f(), is making a call through a pointer to a functions p1(), p2(), p3()....then the compiler has no idea which actual function is being called at run time. Therefore it cannot calculate how many stack words are going to be used by f().
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

The following works for me when placed in a .c file:

Code: Select all

int (*pt2Function)(float, char, char) = 0;

int DoItA (float a, char b, char c)
{
	printf("DoIt A\n");
	return (0);
}

int DoItB (float a, char b, char c)
{
	printf("DoIt B\n");
	return (1);
}

void doSomething()
{
	int result;
    x = 1;
    pt2Function = &DoItA;
    result = pt2Function (12, 'a', 'b');
    pt2Function = &DoItB;
    result = pt2Function (12, 'a', 'b');
}
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

You need:

#pragma stackfunction 100

placed just before the function causing the problem. Change 100 to whatever stack size you think you may need.

That code I posted above compiles and runs provided there is only one thread in a core. As soon as I start another thread I get the stackwords error. The thing compiles OK but Mapper/Linker does not like it.

Looking in the assembler output for that code I see things like:

Code: Select all

        .globl  DoItB.nstackwords
        .linkset        DoItB.nstackwords,9 + (puts.nstackwords)
For the functions DoItA() and DoItB() but there is no such nstackwords definition for doSomethingInC()

So surely when the linker is allocating stacks to threads it fails as it does not know the stack size requirements. With only one thread that is not an issue. With many threads we have to manually set the stack size.
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Post by Interactive_Matter »

Thanks,

You are completly right. It did not work in Release on the XC-1 too. So it is obviously a multithread/optimization issue (on XC-2 there are a lot of threads in parallel).
Thanks for the precise and fast answer!

Marcus
Heater
Respected Member
Posts: 296
Joined: Thu Dec 10, 2009 10:33 pm

Post by Heater »

That's odd. You are right Debug or Release does have an effect on this problem. No idea why except I have a feeling that Release removes some checks. Anyway now we both know something new.
Glad to be of help, I could not sleep last night so your problem was niggling at me:)
mculibrk
Active Member
Posts: 38
Joined: Tue Jul 13, 2010 2:57 pm

Post by mculibrk »

A question regarding this "Debug/Release" profiles and pragmas...

Is there any "hardcoded/hidden" differences between the Debug and Release profiles? I mean, is there any setting which cannot be seen/changed in the "settings" part of the profile configuration?

Also are all the #pragma options listed somewhere? I found some of the parameters listed in the docs but I'm almost sure not all pragmas seen here in the fourm are explained.

regards,
mculibrk
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Post by Interactive_Matter »

I suspect -o2/-o0 to contribuite to that behaviour