Common Features of realview Compiler

Source: Internet
Author: User
I. keywords and operators

1. _ Align (n):Indicates that the CompilernByte boundary alignmentVariable.

For local variables,nValues can be 1, 2, 4, or 8.

For global variables,nIt can have any value of 0x80000000 power up to 2.

    __alignKeyword close to variable nameFrontPlace.

Note:

Only alignment can be performed. That is to say, the object of two bytes can be aligned by four bytes, but the object of four bytes cannot be aligned by two bytes.

Example:

_ Align (8) Char buffer [128];/* buffer starts from the 8-byte alignment boundary */void Foo (void ){... _ align (16) int I;/* The align value cannot be used in local variables */...} _ align (16) int I;/* as a global variable, this alignment value is allowed */

2. _ ASM:This keyword is used to pass information from the compiler to the arm assembler armasm.

Usage:

A. Embedded Assembler

Available__asmKeyword declares or defines Embedded Assembler functions. For example:

    __asm void my_strcpy(const char *src, char *dst); 
B. inline assembler

Available__asmMerge inline assembly into the function with the keyword. For example:

    int qadd(int i, int j)    {        int res;        __asm        {            QADD   res, i, j        }        return res;    }

C. assembler labels

Available__asmKeyword: Specifies the assembler label for the C symbol. For example:

    int count __asm__("count_v1"); // export count_v1, not count
D. Named register variables

Available__asmThe keyword declares the named register variable. For example:

    register int foo __asm("r0");

3. _ forceinline: ForcedThe compiler compiles C or C ++ letters inline.

Note:

    __forceinline The semantics of is exactly the same as that of the C ++ inline keyword. The compiler tries to limit inline__forceinlineWithout considering its features. However, if this problem occurs, the compiler will not inline the function. For example, recursive functions are inline only once.

Note:

This keyword has equivalent Function Attributes__attribute__((always_inline)).

Example:

    __forceinline static int max(int x, in y)    {        return x > y ? x : y; // always inline if possible    }

4._ Inline:Prompt compiler inUnder reasonable circumstancesInline compilation of C or C ++ functions.

Note:

    __inline The semantics of is exactly the same as that of the C ++ inline keyword.

Example:

    __inline int f(int x)    {        return x*5+1;    }    int g(int x, int y)    {        return f(x) + f(y);    }

5.__packed:Set the alignment boundary of all valid types to 1. This means:

  • Do not insert fill to align the compressed object

  • Use unaligned access to read or write compressed objects.

Use__packedAfter the qualifier declares the structure or union,__packedApplies to this structure or all members of the Union. Members or the end of the structure are not filled. Required__packedDeclare all sub-structures of the compressed structure. You can separately compress integer subfields of non-compressed structures.

Usage:

To map a structure to an external data structure or access non-alignment data,__packed The qualifier is very useful. However, because the access overhead is relatively high, it usually does not help to save the data size. By compressing only fields in the structure to be compressed, you can reduce the number of unaligned access.

Note:

The hardware does not support arm processors with Non-Aligned access (for example, those before armv6, when you access data that is not aligned, code size and execution speed may incur high costs. RequiredMinimizeData access by compressing the structure to avoid code size increase and performance reduction.

Example 1-Compression structure

Typedef _ packed struct {char X; // All struct members are limited by _ packed int y;} X; // struct occupies 5 bytes. If _ packed is not used, this structure occupies 8 bytes of int f (x * P) {return p-> Y; // execute an unaligned read data} typedef struct {short X; char y; _ packed int Z; // only this variable is constrained by _ packed char a;} y; // This struct occupies 8 bytes of int g (y * P) {return p-> Z + P-> X; // only the read zone Z is not aligned}

Example 2-Pointer to the compression type

Typedef _ packed int * PPI;/* points to a _ packed int variable */_ packed int * P; /* points to a _ packed int variable */PPI P2; /* the type of 'p2 'and 'P' is the same * // * _ packed can be regarded as a qualifier * // * Like 'const' or 'volatile' */typedef int * PI; /* points to an int variable */_ packed PI P3; /* A _ packed pointer pointing to a normal int variable * // * -- 'p' and 'p2' are not of the same type */int * _ packed P4; /* 'p4 'and 'p3' are of the same type */

 

Ii. Function Attributes

1._ Attribute _ (always_inline )):This function property indicates that the function must be inline.

Same as _ forceinline.

Example:

    static int max(int x, int y) __attribute__((always_inline))    {        return x > y ? x : y; // always inline if possible    }

2._ Attribute _ (used )):Indicates that the compiler retains the static function in the object file, even if the function is removed from reference.

Example:

Static int keep_this (INT) _ attribute _ (used);/* keep the target file, and the compiler does not perform space optimization */

 

3._ Attribute _ (unused )):unusedFunction properties prohibit the compiler from generating warnings when the function is not referenced. This does not change the process of deleting unused functions.

Example:

   static int Function_Attributes_unused_0(int b) __attribute__ ((unused));

4._ Attribute _ (Section ("name"))):AvailablesectionFunction properties place the code in different sections of the image.

Example:

In the following exampleFunction_Attributes_section_0In Ro Sectionnew_section, Instead.text.

   1: void Function_Attributes_section_0 (void) 
   2:     __attribute__ ((section ("new_section")));
   3: void Function_Attributes_section_0 (void)
   4: {
   5:     static int aStatic =0;
   6:     aStatic++;
   7: }
Iii. variable attributes

1._ Attribute _ ((address))):You can use this variable property to specify the absolute address of the variable.

The variable is placed in its own section, and the compiler will specify the appropriate type for the section containing the variable:

  • Read-Only variables are placed in the ro type section.

  • Initialized read/write variables are placed in the RW Type section.

    Specifically, variables whose Explicit initialization is zero are placed in RW instead of Zi. These variables are not suitable for Compiler-to-RW optimization.

  • Uninitialized variables are placed in the Zi type section.

Syntax:

    __attribute__((at(address)))

Where,Address is the variable address.

Note:

The linker cannot always be placedatVariable property generation section. If the node cannot be placed at the specified address, the linker displays an error message.

 Example:

    const int x1 __attribute__((at(0x10000))) = 10; /* RO */    int x2 __attribute__((at(0x12000))) = 10;       /* RW */    int x3 __attribute__((at(0x14000))) = 0;        /* RW, not ZI */    int x4 __attribute__((at(0x16000)));            /* ZI */

Extension:

In the absacc. h provided by the Keil MDK, macro _ at is defined as follows:

   #ifndef __at   #define __at(_addr) __attribute__ ((at(_addr)))   #endif

So as long as the module contains the absacc. h header file, you can use _ at to specify the absolute address of the variable.

 2._ Attribute _ (zero_init )):The compiler does not perform zero initialization for modified variables.

    sectionAttribute specified variables must be placed in a specific data section.zero_initAttribute specifies that the variable without an initial value is placed in the Zi data section. If the Program specifies an initial value, an error is reported.

Example:

_ Attribute _ (zero_init) int X;/* in section ". BSS "*/
   __attribute__((section("mybss"), zero_init)) int y;  /* in section "mybss" */

3._ Attribute _ (Section ("name "))):Generally, the arm compiler places the objects it generates in the Section, suchdataAnd
bss. However, you may need to use another data section or want variables to appear in a special section, for example, to facilitate ing to special hardware.sectionAttribute specified variables must be placed in a specific data section. If you usesectionAttribute, the read-only variables are placed in the RO data section, and the read/write variables are placed in the RW data section, unless you use
zero_initAttribute. In this case, variables are placed in the Zi section.

Example:

   1: /* in RO section */
   2: const int descriptor[3] __attribute__ ((section ("descr"))) = { 1,2,3 };
   3: /* in RW section */
   4: long long rw[10] __attribute__ ((section ("RW")));
   5: /* in ZI section */

6: long long altstack[10] __attribute__ ((section ("STACK"), zero_init));

4._ Attribute _ (used )):This variable Property indicates that the compiler retains a static variable in the object file, even if the reference to this variable is removed.

Example:

   1: static int lose_this = 1;
   2: static int keep_this __attribute__((used)) = 2;     // retained in object file
   3: static int keep_this_too __attribute__((used)) = 3; // retained in object file

5._ Attribute _ (unused )):Generally, if a variable is declared but never referenced, the compiler will issue a warning. This attribute indicates that the compiler is not expected to use a variable, and instructs the compiler not to issue a warning when the variable is not used.

Example:

   1: void Variable_Attributes_unused_0()
   2: {
   3:     static int aStatic =0;
   4:     int aUnused __attribute__ ((unused));
   5:     int bUnused;
   6:     aStatic++;
   7: }

Iv. inline instruction Functions

1._ Breakpoint:This internal function is inserted in the instruction stream generated by the compiler.BKPTCommand. It allows the inclusion of breakpoint commands in c or C ++ code.

Syntax:

    void __breakpoint(int val)

Where,valIt is an integer of the compile time, and its range is:

    0 ... 65535

If you want to compile the source code into arm code

    0 ... 255

If you want to compile the source code into thumb code.

Note:

Is not supportedBKPTThe compiler cannot identify the purpose of the command during compilation.__breakpointInternal function. In this case, the compiler generates a warning or error.

If notBKPTIf the command is executed in the instruction architecture, an undefined instruction trap is generated.

Example:

   void func(void)   {       ...        __breakpoint(0xF02C);       ...    }

2. _ NOP:This internal function is inserted in the instruction stream generated by the compiler.NOPCommand or equivalent code sequence. For each__nopThe internal function generatesNOPCommand.

Note:The compiler does not optimize deletion.NOPCommand.

Syntax:

    void __nop(void)

 

 

Note: This article is compiled in the realview compiler Reference Guide. Part 4: Compiler features.

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.