I. keywords and operators
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.
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:
Use__packed
After the qualifier declares the structure or union,__packed
Applies to this structure or all members of the Union. Members or the end of the structure are not filled. Required__packed
Declare 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 )):unused
Function 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
"))):Availablesection
Function properties place the code in different sections of the image.
Example:
In the following exampleFunction_Attributes_section_0
In 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 attributes1._ 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 placedat
Variable 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.
section
Attribute specified variables must be placed in a specific data section.zero_init
Attribute 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, suchdata
And
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.section
Attribute specified variables must be placed in a specific data section. If you usesection
Attribute, 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_init
Attribute. 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.BKPT
Command. It allows the inclusion of breakpoint commands in c or C ++ code.
Syntax:
void __breakpoint(int val)
Where,val
It 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 supportedBKPT
The compiler cannot identify the purpose of the command during compilation.__breakpoint
Internal function. In this case, the compiler generates a warning or error.
If notBKPT
If 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.NOP
Command or equivalent code sequence. For each__nop
The internal function generatesNOP
Command.
Note:The compiler does not optimize deletion.NOP
Command.
Syntax:
void __nop(void)
Note: This article is compiled in the realview compiler Reference Guide. Part 4: Compiler features.