Page 1 of 1

Inline Assembler Question

Posted: Thu Mar 08, 2012 3:23 pm
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    : (
}

Re: Inline Assembler Question

Posted: Thu Mar 08, 2012 8:25 pm
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.

Re: Inline Assembler Question

Posted: Tue Apr 10, 2012 5:40 pm
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