分享

The near and far keywords(DSP & CCS)

 黄南山 2017-08-15

// 摘自: TMS320C6000 Optimizing Compiler v7.4 User`s Guide.pdf

====================================================================================================

The C6000 C/C++ compiler extends the C/C++ language with the near and far keywords to specify how global and static variables are accessed and how functions are called. 

Syntactically, the near and far keywords are treated as storage class modifiers. They can appear before, after, or in between the storage class specifiers and types. With the exception of near and far, two storageclass modifiers cannot be used together in a single declaration. The following examples are legal combinations of near and far with other storage class modifiers:

  1. far static int x;  
  2. static near int x;  
  3. static int far x;  
  4. far int foo();  
  5. static far int foo();  

near and far Data Objects


Global and static data objects can be accessed in the following two ways: 

near keywordThe compiler assumes that the data item can be accessed relative to the data page pointer. For example:

LDW  *+dp(_address), a0
far keywordThe compiler cannot access the data item via the DP. This can be required if the total amount of  program data is larger than the offset allowed (32K) from the DP. For example:
MVKL  _address, a1
MVKH  _address, a1

LDW   *a1, a0

Once a variable has been defined to be far, all external references to this variable in other C files or headers must also contain the far keyword. This is also true of the near keyword. However, you will get compiler or linker errors when the far keyword is not used everywhere. Not using the near keyword everywhere only leads to slower data access times.


If you use the DATA_SECTION pragma, the object is indicated as a far variable, and this cannot be overridden. If you reference this object in another file, then you need to use extern far when declaring this object in the other source file. This ensures access to the variable, since the variable might not be in the .bss section. For details, seeSection 6.9.6.


NOTE: Defining Global Variables in Assembly Code

         If you also define a global variable in assembly code with the .usect directive (where the variable is not assigned in the .bss section) or you allocate a variable into separate section using a #pragma DATA_SECTION directive; and you want to reference that variable in C code, you must declare the variable as extern far. This ensures the compiler does not try to generate an illegal access of the variable by way of the data page pointer. 

When data objects do not have the near or far keyword specified, the compiler will use far accesses to aggregate data and near accesses to non-aggregate data. For more information on the data memory model and ways to control accesses to data, see Section 7.1.5.1.


Near and far Function Calls


Function calls can be invoked in one of two ways:
near keyword   The compiler assumes that destination of the call is within ± 1 M word of the caller. Here the compiler uses the PC-relative branch instruction.

B _func


far keyword    The compiler is told by you that the call is not within ± 1 M word.
MVKL _func, al
MVKH _func, al
B _func


By default, the compiler generates small-memory model code, which means that every function call is
handled as if it were declared near, unless it is actually declared far.
For more information on function calls, see Section 7.1.6.

-------------------------------------------------------------------------------------------------------------------------------


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多