[Theoretical knowledge] atpcs 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 subprograms. these basic rules include the register usage rules, data stack usage rules, and parameter transfer rules during the subroutine call process. to meet specific needs, modify these basic calling rules to obtain several different subroutine calling rules. These specific calling rules include atpcs that support data stack restriction checks. atpcs with read-only segments are supported. atpcs with no read/write segments are supported. supports the hybrid use of the arm program and the Thumb program atpcs. 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 assembly language programs, it is entirely necessary to rely 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 during the preparation of the subroutine; the use of the data stack must comply with the atpcs rules; use the-APCs option in the assembly compiler...

Ii. Basic atpcs...

Basic atpcs stipulate some basic rules for subprogram calling, including the following three aspects: rules for using registers and their corresponding names; rules for using data stacks; parameter transfer rules. compared with other types of atpcs, the program that meets 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 specific atpcs derived from the 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. These operations are not required for the registers not used in the subroutine. 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 the 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 to the memory address, it is called the ascending stack. based on these two features, four data stacks are available: 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 using the analyticdb compiler contains data frames in the drfat2 format. during debugging, the debugger can use these data frames to view related information in the data stack. for assembly languages, users must use frame pseudo operations 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 increase the data transmission speed. in the target file generated by the analyticdb compiler, the data stack of the external interface is 8 bytes aligned, and the compiler will tell the connector that the data stack in the target file is 8 bytes aligned. for assembler programs, if the target file contains an external call, the following conditions must be met: the data stack of the external interface must be 8-bit aligned, that is to say, after entering the assembly code, the stack pointer of the data stack changes to an even number of characters until the Assembly program calls external code; 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 in sequence. If there are more than four parameters, the remaining words are transmitted to the data stack, the order of the incoming data stack is the opposite of that of the parameter. That is, the last word data 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 passed through the Register 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, atpcs that support data stack restrictions must meet the following rules: the minimum address of an existing stack and the SL must have 256 bytes of space, that is, the memory address indicated by SL must be 256 bytes lower than the minimum address of the occupied stack. when the interrupt processing program can use the user's data stack, the minimum address of the occupied stack and the SL must be kept between the 256-byte memory unit, you must also reserve sufficient memory space for interrupt processing. You cannot modify the SL value in the program. The sp value of the data stack Stack pointer must not be smaller than the SL value.

The compilation/Assembly options related to atpcs that support the data stack restriction check include the following: 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 when you cannot accurately calculate the data stack size required by the program during programming. Option/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 the program design. You can specify this option, which is the default option; option/swstna this option is used if the assembler does not check the data stack, and other programs connected to the assembler specify the option swst/noswst.

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, then specify the swst option. 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 the thumb state ;. use the nointerwork option in the following cases: the program does not contain the Thumb program; you can switch the arm program and the Thumb program on your own. note that the same 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.