12.07.2015 Views

PGI User's Guide

PGI User's Guide

PGI User's Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 14. C/C++ Inline Assembly and IntrinsicsThe modifiers that begin with a backslash "\" (e.g., "\n") have the same effect as they do in a printf formatstring. The modifiers that are preceded with a "%" are used to modify a particular operand.These modifiers begin with either a backslash "\" or a percent "%" For example, "%b0" means, "produce thebyte or 8 bit version of operand 0". If operand 0 is a register, it will produce a byte register such as %al, %bl,%cl, and so on.Consider this example:void example19(){int a = 1;int *p = &a;asm ("add%z0 %q1, %a0": "=&p" (p) : "r" (a), "0" (p) );}On an x86 target, the compiler produces the following instruction for the asm string shown in the precedingexample:addl %ecx, (%eax)The "%z0" modifier produced an 'l' (lower-case 'L') suffix because the size of pointer p is 32 bits on x86.The "%q1" modifier produced the word register name for variable a. The "%a0" instructs the compiler to addparentheses around operand 0, hence "(%eax)".On an x86_64 target, the compiler produces the following instruction for the asm string shown in thepreceding example:addq %rcx, (%rax)The "%z0" modifier produced a 'q' suffix because the size of pointer p is 64-bit on x86_64. Because x86_64supports quad word registers, the "%q1" modifier produced the quad word register name (%rax) for variablea.Extended Asm MacrosAs with traditional inline assembly, described in“Inline Assembly,” on page 175, extended asm can be used ina macro. For example, you can use the following macro to access the runtime stack pointer.#define GET_SP(x) \asm("mov %%sp, %0": "=m" (##x):: "%sp" );void example20(){void * stack_pointer;GET_SP(stack_pointer);}The GET_SP macro assigns the value of the stack pointer to whatever is inserted in its argument (for example,stack_pointer). Another "C" extension known as statement expressions is used to write the GET_SP macroanother way:#define GET_SP2 ({ \void *my_stack_ptr; \asm("mov %%sp, %0": "=m" (my_stack_ptr) :: "%sp" ); \my_stack_ptr; \})189

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!