Type & Object attribute

Type attributes

Type attributes define how a function is called, or how a data object is accessed. This means that if you use a type attribute, it must be specified both when a function or data object is defined and when it is declared.

You can either place the type attributes directly in your source code, or use the pragma directive #pragma type_attribute.

The following general type attributes are available:

    • Function type attributes affect how the function should be called: __arm, __fiq, __interwork, __irq, __swi, and __thumb
    • Data type attributes: __big_endian, const, __little_endian, __packed, and volatile

You can specify as many type attributes as required for each level of pointer indirection.

To read more about the type qualifiers const and volatile, see the IAR C/C++ Development Guide for ARM®.

Syntax for type attributes used on data objects

In general, type attributes for data objects follow the same syntax as the type qualifiers const and volatile.

The following declaration assigns the __little_endian type attribute to the variables i and j; in other words, the variables i and j will be accessed with little endian byte order. The variables k and l behave in the same way:

__little_endian int i, j; 
int __little_endian k, l; 

Note that the attribute affects both identifiers.

The following declaration of i and j is equivalent with the previous one:

#pragma type_attribute=__little_endian 
int i, j; 

The advantage of using pragma directives for specifying keywords is that it offers you a method to make sure that the source code is portable. Note that the pragma directive has no effect if a memory attribute is already explicitly declared.

Syntax for type attributes on data pointers

The syntax for declaring pointers using type attributes follows the same syntax as the type qualifiers const and volatile:

int __little_endian * p;
 
The int object will be accessed in little endian byte order.
int * __little_endian p;
 
The pointer will be accessed in little endian byte order.
__little_endian int * p;
 
The pointer will be accessed in little endian byte order.

Syntax for type attributes on functions

The syntax for using type attributes on functions differs slightly from the syntax of type attributes on data objects. For functions, the attribute must be placed either in front of the return type, or in parentheses, for example:

__irq __arm void my_handler(void); 

or

void (__irq __arm my_handler)(void); 

The following declaration of my_handler is equivalent with the previous one:

#pragma type_attribute=__irq __arm 
void my_handler(void); 
 
 

Object attributes

Object attributes affect the internal functionality of functions and data objects, but not how the function is called or how the data is accessed. This means that an object attribute does not need to be present in the declaration of an object.

The following object attributes are available:

    • Object attributes that can be used for variables: __no_init
    • Object attributes that can be used for functions and variables: location, @, __root, and __weak,
    • Object attributes that can be used for functions: __intrinsic, __nested, __noreturn, and __ramfunc.

You can specify as many object attributes as required for a specific function or data object.

For more information about location and @, see the IAR C/C++ Development Guide for ARM®.

Syntax for object attributes

The object attribute must be placed in front of the type. For example, to place myarray in memory that is not initialized at startup:

__no_init int myarray[10];  

The #pragma object_attribute directive can also be used. The following declaration is equivalent to the previous one:

#pragma object_attribute=__no_init 
int myarray[10]; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值