Atpcs (ARM-thumb procedure call standard) Overview

Source: Internet
Author: User

In order to enable mutual calls between separately compiled C and assembler programs, certain rules must be specified for calls between subprograms. atpcs are the basic rules for ARM program and Thumb program neutron program calls...

I. atpcs overview...


Atpcs specifies the basic rules for calling between subprograms. These rules include the register usage rules, data stack usage rules, and parameter transfer rules during the subprogram call process.
These basic calling rules are modified to obtain several different subroutine calling rules. These specific calling rules include atpcs that support data stack restriction checks.
Atpcs with read-only segments and those with read/write segments are supported. atpcs with arm programs and thumb programs are supported.
Atpcs for processing floating point operations...

All subprograms with call relationships must comply with the same atpcs. the compiler or assembler sets corresponding properties in the target file in ELF format to identify the atpcs type selected by the user. the corresponding C language library is available for different types of atpcs, And the connector connects to the corresponding C language library according to the atpcs type specified by the user...

The C language subprograms compiled using the C language compiler of Ads meet the user-specified atpcs type.
However, for an assembly language program, it depends entirely on the user to ensure that each subroutine meets the selected atpcs type. Specifically, the assembly language subroutine must meet the following three conditions:
The corresponding atpcs rules must be observed when writing subprograms; the use of data stacks must comply with the atpcs rules; the-APCs option should be used in the compilation compiler...

Ii. Basic atpcs...

The basic atpcs stipulate some basic rules for subprogram calls, including the following three aspects: the usage rules of each register and their corresponding names;
Data stack usage rules; parameter transfer rules. Compared with other types of atpcs, programs that meet the requirements of the basic atpcs can run faster and consume less memory.
However, it cannot provide the following support: arm program and Thumb program call each other; data and code location-independent support; Sub-program reentrant; data stack check support.
The other types of derived atpcs are formed by adding other rules on the basis of the Basic atpcs. The purpose is to provide the above functions...

Register usage rules:

1. The subroutine passes through the Register R0 ~ R3 to pass parameters. The register can be recorded as a0 ~ A3. The called subroutine does not need to recover the register R0 ~ before returning ~ R3 content.

2. In the subroutine, use R4 ~ R11 to save the local variable, then register R4 ~ R11 can be recorded as V1 ~ V8
. If V1 ~ is used in the subroutine ~ Some registers of V8, the values of these registers must be saved when the subroutine enters, and the values of these registers must be restored before return. For registers not used in the subroutine
You do not need to perform these operations. In the Thumb program, you can only use the register R4 ~ R7 to save local variables.

3. Register R12 is used as the scratch register between subprograms and recorded as an IP address. This usage rule is often found in the connection code segment of subprograms.

4. Register R13 is used as the data stack pointer and recorded as sp. in the subroutine, register R13 cannot be used for other purposes. The value of register SP when it enters the subroutine must be equal to the value when it exits the subroutine.

5. Register R14 is used as the connection register and recorded as LR. It is used to save the return address of the subroutine. If the return address is saved in the subroutine, R14 can be used for other purposes.

6. Register R15 is a program counter and is recorded as a PC. It cannot be used for other purposes.

7. The registers in atpcs are predefined in the arm compiler and assembler.

Data stack usage rules


A stack pointer can usually point to different locations. when the stack pointer points to the top element of the stack (that is, the data element of the last stack), it is called the full stack. when the stack pointer points to an element adjacent to the top element of the stack, it is called
Empty stack. The growth direction of the data stack can also be different. When the data stack increases to the address with reduced memory, it is called the descending stack;
When the data stack increases towards the memory address, it is called the ascending stack. Combining these two features, the following four data stacks can be used: FD ed fa ea.
Atpcs specifies that the data stack is fd type and the operations on the data stack are 8-byte aligned. The following is a data stack example and related terms.

1. Data stack Stack pointer. Stack pointer points to the memory address of the last data written to the stack.

2. the base address of the data stack. stack base is the highest address of the index data stack. because the data stack in atpcs is fd type, in fact, the first memory unit occupied by data in the data stack is the next memory unit of the base address.

3. data stack boundaries. Stack limit is the lowest memory unit address available in the data stack.

4. Occupied data stack. Used stack is the area between the base address of the data stack and the data stack Stack pointer, including the memory units corresponding to the data stack Stack pointer.

5. The data frame (stack frames) in the data stack refers to the Region allocated to the subroutine for storing registers and local variables in the data stack.

The data stack of the interrupted program can be used for handling abnormal interruptions. In this case, the user must ensure that the data stack of the interrupted program is large enough.
The target code generated by the analyticdb compiler contains data frames in the drfat2 format. During debugging, the debugger can use these data frames to view the relevant information in the data stack.
The user must use the frame pseudo operation to describe data frames in the data stack. The arm assembler generates data frames in the drfat2 format in the target file based on these pseudo operations.


In armv5te, the batch transfer command ldrd/strd requires that the data stack be 8-byte aligned to improve the data transmission speed. Data of external interfaces in the target file generated by the ads Compiler
The stack is 8-byte aligned, and the compiler will tell the connector that the data stack in the target file is 8-byte aligned.
For assembler programs, if the target file contains external calls, the following conditions must be met:
The data stack of the external interface must be 8-bit aligned, that is, after entering the assembly code, until the Assembly program calls the external code, the stack pointer is changed to an even number of characters;
Using the preserve8 pseudo operation in the assembler tells the connector that this assembler is 8-byte aligned.

Parameter transfer rules.

Based on whether the number of parameters is fixed, the subprograms can be divided into subprograms with fixed numbers of parameters and subprograms with variable numbers of parameters. The parameter transfer rules of these two subprograms are different.

1. Rules for passing subroutine parameters with variable number of parameters


When there are no more than four subprograms with variable number of parameters, you can use registers R0 ~ R3 to pass parameters. When there are more than four parameters, you can also use the data stack to pass parameters.
When passing parameters, all parameters are considered as word data stored in a continuous memory unit. Then, name data is transmitted to registers r0, R1, R2, and R3;
If there are more than four parameters, the remaining words are transmitted to the data stack. The order of the incoming data is the opposite to that of the parameter. That is, the last word is first imported into the stack.
According to the above rules, a floating point number parameter can be transmitted through registers, data stacks, or half through registers, and the other half through data stacks.

2. Rules for passing subroutine parameters with fixed number of parameters


For a subroutine with a fixed number of parameters, the rules for passing parameters are different from those for passing parameters with variable numbers. If the system contains hardware components for floating-point operations, the floating-point parameters are transmitted according to the following rules:
Each floating-point parameter is processed in order; each floating-point parameter is assigned a FP register. The allocation method is a set of continuous FP registers that meet the needs of this floating-point parameter and have the smallest number. the first integer parameter is stored
R0 ~ R3. Other parameters are passed through the data stack.

Rules for returning subroutine results

1. If the result is a 32-bit integer, it can be returned through the R0 register.

2. if the result is a 64-bit integer, it can be returned through R0 and R1, and so on.

3. When the result is a floating point number, it can be returned through the floating point operation register F0, D0 or S0.

4. When the result is a composite floating point, you can use the register f0-fN or D0 ~ DN to return.

5. For results with more digits, the call memory is required.

Iii. Specific atpcs...

A. atpcs that support data stack restriction checks.


If you can accurately calculate the total amount of memory required by the program during the program design period, you do not need to check the data stack, but it is usually difficult to do so, check the data stack.
When performing a data stack check, register R10 is used as the data stack limit pointer. Then register R10 is recorded as SL. the user cannot control the register in the program. specifically, data stack restrictions are supported.
Atpcs must meet the following rules:
There must be a space of 256 bytes between the minimum address of the occupied stack and the SL, that is, the memory address indicated by the SL must be 256 bytes lower than the minimum address of the occupied stack. when the processing program is interrupted
When a user's data stack can be used, apart from the 256-byte memory units that must be retained between the minimum address of the occupied stack and the SL, sufficient memory space must be reserved for interrupt processing;
The user cannot modify the SL value in the program. The sp value of the data stack Stack pointer must be no less than the SL value.

The compilation/Assembly options related to atpcs that support the data stack restriction check are as follows: Option/swst
Indicates that the code generated by the compiler complies with the atpcs that support the data stack restriction check. You must specify this option if you cannot accurately calculate the data stack size required by the program during programming.
/Noswst indicates that the code generated by the compiler does not support the data stack restriction check function. You can accurately calculate the data stack size required by the program during programming. You can specify this option, this option is Mo
Yes; Option/swstna
This option is used if the assembler does not check the data stack and other programs connected to the assembler specify the swst/noswst option.

Compile the atpcs assembly language program that complies with the data stack restriction check.

For C Programs and C ++ programs, if swst is specified during compilation, the generated target code will comply with the atpcs that support the data stack restriction check.
For assembly language programs, if you want to comply with the atpcs that support the data stack restriction check, you must meet the atpcs requirements when writing the program, and then specify the option
Swst. The following describes some requirements for compiling assembly language programs.

Leaf subprograms are subprograms that do not call other programs.

Leaf subprograms with a data stack of less than 256 bytes are not allowed to perform data stack check. This rule is also applicable if the leaf subprogram data stacks composed of several subprograms are less than 256 bytes; for non-leaf subprograms whose data stack size is less than 256 bytes, the following code segment can be used for data stack check.

Arm program usage: sub sp, SP, # size; # The size must be retained between SP and SL.

CMP sp, SL;

Bllo _ arm_stack_overflow

The thumb program uses: add SP, #-size; # size is the size of space that must be retained between SP and SL.

CMP sp, SL;

Bllo _ thumb_stack_overflow

A subroutine with a data stack larger than 256 bytes. To ensure that the sp value is not smaller than the minimum address value of the memory units available in the data stack, a register must be introduced.

The arm program uses the following code: Sub IP, SP, # size;

Cmp ip, SL;

Bllo _ arm_stack_overflow

The thumb program uses the following code: ldr wr, #-size;

Add WR, SP;

Cmp wr, SL;

Bllo _ thumb_stack_overflow

Atpcs with read-only segments not related...

Atpcs with no read/write segments are supported...

Support for the combination of arm program and Thumb program atpcs...


During compilation or assembly, use/intework to tell the compiler or assembler that the target code must comply with the atpcs that support the combination of arm program and Thumb program. It is used in the following scenarios:
In the program, the arm program calls the Thumb program. In the program, the Thumb program calls the arm program. You need a connector to switch the arm State and thumb state.
Under the following circumstances, use the option nointerwork: the program does not contain the Thumb program. You can switch between the arm program and the Thumb program on your own.
The C/C ++ program cannot contain both arm commands and thumb commands.



 

 

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.