GCC Inline compilation

Source: Internet
Author: User

Well, first of all, this article is for GCC only. Please disregard this article for other compilers.

Sometimes we want to use the inline assembler in C/C + + code, because there is no corresponding function or syntax available. For example, when I recently wrote an FIR program on arm, I needed to saturate the final result, but GCC did not provide a function like SSAT, so I had to embed assembly instructions in C code.

1. Getting Started

The biggest problem with embedding a compilation in C is how to associate a C-language variable with a directive operand. Of course, GCC has helped us figure it out. The following is a simple example.

ASM ("Fsinx%1,%0": "=f" (Result): "F" (angle));

Here we do not need to pay attention to what the Fsinx command is doing; just need to know that this instruction requires two floating-point registers as operands. As a full-time, C-language gcc compiler, it's no way to know what operands fsinx this assembly instruction requires, which requires the program ape to inform GCC that the method is "=f" and "F" behind the instruction, indicating that this is two floating-point register operands. This is referred to as the operand rule (constraint). The rule preceded by "=" indicates that this is an output operand, otherwise it is an input operand. Constraint the variable that is associated with the register in parentheses. This way, GCC will know how to turn this embedded assembly statement into an actual assembly instruction:

Fsinx: Assembly instruction Name

%1,% 0: operand corresponding to assembly instruction

"=f" (Result): Operand%0 is a floating-point register associated with the variable result ("associative" means that GCC executes this assembly instruction and sends the contents of the register%0 to the variable result)

"F" (angle): Operand%1 is a floating-point register associated with the variable angle ("associative" means that GCC will first read the value of the variable angle to register%1 before executing this assembly instruction)

Therefore, this embedded Assembly will be converted to at least three assembly instructions (non-optimized):

1> load the value of the angle variable to register%1

2> Fsinx Assembly instruction, source register%1, target register%0

3> storing the value of the register%0 to the variable result

Of course, the above narrative may not be applicable at a high level of optimization, for example, the source operand may already be in a floating-point register.

Here we also see the meaning of the constraint "=" symbol: GCC needs to know whether this operand is loaded from the variable to the register before the embedded assembly is executed, or stored in the variable from the register after execution.

Commonly used constraints have the following (see the GCC Manual for more details):

M memory operand

Number of R Register operands

I immediate number operand (integer)

F floating-point register operand

F immediate number operand (floating point)

From this chestnut can also be seen in the basic format of the embedded assembly:

ASM ("Assembly instruction": "= output operand rule" (associative variable): "Input operand rule" (associative variable));

The output operand must be an lvalue;

2. Multiple input/output operands, or no output operand

What if there is more than one input/output operand for an instruction? For example, ARM has a number of instructions that are three operand instructions. This time separate multiple rules with commas:

ASM ("Add%0,%1,%2": "=r" (sum): "R" (a), "R" (b));

Each operand rule corresponds to the operand in order%0,% 1,% 2.

For cases where there is no output operand, there is no output rule after the assembly instruction, so there are two consecutive colons followed by an input rule.

GCC Inline compilation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.