Inline Assembler Question

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
GerhardNorkus
Active Member
Posts: 55
Joined: Wed Jan 05, 2011 2:15 pm

Inline Assembler Question

Post by GerhardNorkus »

Hi All,

Simple question...I hope. How do I access a function local variable with the inline assembler using ldw?

Example of the code below:

Code: Select all

long nGlobalVar ;
void MyFunction (int a)
{
    int b ;
    // This is global access
    asm("ldw r0, dp[nGlobalVar]") ;
   
    // How do I correctly do local access?
    // This will not compile correctly
    asm("ldw r0, sp[a]") ;
   
    // I can get the following to work, and avoid
    // the clobber registers, but I really want to simply
    // use a syntax similar to the line above
    asm("add r0, %0, 0" : : "r"(a) : "r0","r1","r2") ;
    // The above code will expand to 
    //     ldw r11, sp[1]
    //     add r0, r11, 0
    // I want tight code    : (
}


User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am
Contact:

Post by segher »

You would write something like

Code: Select all

unsigned x, y;
asm("ldw %0,%1" : "=r"(x) : "m"(y));
but with the current tools that results in

Code: Select all

Could not match memory address.  Inline asm failure!
Another option is to pass the address of the var instead, like

Code: Select all

unsigned x, y;
asm("ldw %0,%1" : "=r"(x) : "p"(&y));
but that results in

Code: Select all

xcc2: /build/swnb/autobuild/Linux/sb_20110630_1915_02_1981/tools_llvm/src/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp:5326: void llvm::SelectionDAGLowering::visitInlineAsm(llvm::CallSite): Assertion `(OpInfo.ConstraintType == TargetLowering::C_RegisterClass || OpInfo.ConstraintType == TargetLowering::C_Register) && "Unknown constraint type!"' failed.
debug.c:189: internal compiler error: Aborted
Anyway, in general, it is best to pass things in registers, since XS1 is
a load-store architecture (that is, all instructions that access memory
do not compute things as well). It seems what you really want to do
is force things into the r0 register? If you can explain why, perhaps
there is a good solution.
yzoer
XCore Addict
Posts: 133
Joined: Tue Dec 15, 2009 10:23 pm

Post by yzoer »

Hi!

Is there any specific reason you want to inline code? It's really easy to setup an external .S (capitalized, so it gets pre-processed) file and call that directly from XC.

Just wondering..

-Yvo
Post Reply