📄 callconv.t
字号:
@c@c COPYRIGHT (c) 1988-2002.@c On-Line Applications Research Corporation (OAR).@c All rights reserved.@c@c $Id: callconv.t,v 1.8 2002/01/17 21:47:46 joel Exp $@c@chapter Calling Conventions@section IntroductionEach high-level language compiler generatessubroutine entry and exit code based upon a set of rules knownas the compiler's calling convention. These rules address thefollowing issues:@itemize @bullet@item register preservation and usage@item parameter passing@item call and return mechanism@end itemizeA compiler's calling convention is of importance wheninterfacing to subroutines written in another language eitherassembly or high-level. Even when the high-level language andtarget processor are the same, different compilers may usedifferent calling conventions. As a result, calling conventionsare both processor and compiler dependent.RTEMS supports the Embedded Application Binary Interface (EABI)calling convention. Documentation for EABI is available by sendinga message with a subject line of "EABI" to eabi@@goth.sis.mot.com.@section Programming ModelThis section discusses the programming model for thePowerPC architecture.@subsection Non-Floating Point RegistersThe PowerPC architecture defines thirty-two non-floating point registersdirectly visible to the programmer. In thirty-two bit implementations, eachregister is thirty-two bits wide. In sixty-four bit implementations, eachregister is sixty-four bits wide.These registers are referred to as @code{gpr0} to @code{gpr31}.Some of the registers serve defined roles in the EABI programming model. The following table describes the role of each of these registers:@ifset use-ascii@example@group +---------------+----------------+------------------------------+ | Register Name | Alternate Name | Description | +---------------+----------------+------------------------------+ | r1 | sp | stack pointer | +---------------+----------------+------------------------------+ | | | global pointer to the Small | | r2 | na | Constant Area (SDA2) | +---------------+----------------+------------------------------+ | r3 - r12 | na | parameter and result passing | +---------------+----------------+------------------------------+ | | | global pointer to the Small | | r13 | na | Data Area (SDA) | +---------------+----------------+------------------------------+@end group@end example@end ifset@ifset use-tex@sp 1@tex\centerline{\vbox{\offinterlineskip\halign{\vrule\strut#&\hbox to 1.75in{\enskip\hfil#\hfil}&\vrule#&\hbox to 1.75in{\enskip\hfil#\hfil}&\vrule#&\hbox to 2.50in{\enskip\hfil#\hfil}&\vrule#\cr\noalign{\hrule}&\bf Register Name &&\bf Alternate Names&&\bf Description&\cr\noalign{\hrule}&r1&&sp&&stack pointer&\cr\noalign{\hrule}&r2&&NA&&global pointer to the Small&\cr&&&&&Constant Area (SDA2)&\cr\noalign{\hrule}&r3 - r12&&NA&¶meter and result passing&\cr\noalign{\hrule}&r13&&NA&&global pointer to the Small&\cr&&&&&Data Area (SDA2)&\cr\noalign{\hrule}}}\hfil}@end tex@end ifset @ifset use-html@html<CENTER> <TABLE COLS=3 WIDTH="80%" BORDER=2><TR><TD ALIGN=center><STRONG>Register Name</STRONG></TD> <TD ALIGN=center><STRONG>Alternate Name</STRONG></TD> <TD ALIGN=center><STRONG>Description</STRONG></TD></TR><TR><TD ALIGN=center>r1</TD> <TD ALIGN=center>sp</TD> <TD ALIGN=center>stack pointer</TD></TR><TR><TD ALIGN=center>r2</TD> <TD ALIGN=center>na</TD> <TD ALIGN=center>global pointer to the Small Constant Area (SDA2)</TD></TR><TR><TD ALIGN=center>r3 - r12</TD> <TD ALIGN=center>NA</TD> <TD ALIGN=center>parameter and result passing</TD></TR><TR><TD ALIGN=center>r13</TD> <TD ALIGN=center>NA</TD> <TD ALIGN=center>global pointer to the Small Data Area (SDA)</TD></TR> </TABLE></CENTER>@end html@end ifset@subsection Floating Point RegistersThe PowerPC architecture includes thirty-two, sixty-four bitfloating point registers. All PowerPC floating point instructions interpret these registers as 32 double precision floating point registers,regardless of whether the processor has 64-bit or 32-bit implementation.The floating point status and control register (fpscr) records exceptionsand the type of result generated by floating-point operations.Additionally, it controls the rounding mode of operations and allows thereporting of floating exceptions to be enabled or disabled.@subsection Special RegistersThe PowerPC architecture includes a number of special registerswhich are critical to the programming model:@table @b@item Machine State RegisterThe MSR contains the processor mode, power management mode, endian mode,exception information, privilege level, floating point available andfloating point excepiton mode, address translation information andthe exception prefix.@item Link RegisterThe LR contains the return address after a function call. This registermust be saved before a subsequent subroutine call can be made. Theuse of this register is discussed further in the @b{Call and ReturnMechanism} section below.@item Count RegisterThe CTR contains the iteration variable for some loops. It may also be usedfor indirect function calls and jumps.@end table@section Call and Return MechanismThe PowerPC architecture supports a simple yet effective calland return mechanism. A subroutine is invokedvia the "branch and link" (@code{bl}) and"brank and link absolute" (@code{bla})instructions. This instructions place the return addressin the Link Register (LR). The callee returns to the caller by executing a "branch unconditional to the link register" (@code{blr})instruction. Thus the callee returns to the caller via a jumpto the return address which is stored in the LR.The previous contents of the LR are not automatically saved by either the @code{bl} or @code{bla}. It is the responsibilityof the callee to save the contents of the LR before invokinganother subroutine. If the callee invokes another subroutine,it must restore the LR before executing the @code{blr} instructionto return to the caller.It is important to note that the PowerPC subroutinecall and return mechanism does not automatically save andrestore any registers.The LR may be accessed as special purpose register 8 (@code{SPR8}) using the"move from special register" (@code{mfspr}) and"move to special register" (@code{mtspr}) instructions.@section Calling MechanismAll RTEMS directives are invoked using the regularPowerPC EABI calling convention via the @code{bl} or@code{bla} instructions.@section Register UsageAs discussed above, the call instruction does notautomatically save any registers. It is the responsibilityof the callee to save and restore any registers which must be preservedacross subroutine calls. The callee is responsible for saving callee-preserved registers to the program stack and restoring thembefore returning to the caller.@section Parameter PassingRTEMS assumes that arguments are placed in thegeneral purpose registers with the first argument in register 3 (@code{r3}), the second argument in general purposeregister 4 (@code{r4}), and so forth until the seventhargument is in general purpose register 10 (@code{r10}). If there are more than seven arguments, then subsequent argumentsare placed on the program stack. The following pseudo-codeillustrates the typical sequence used to call a RTEMS directivewith three (3) arguments:@exampleload third argument into r5load second argument into r4load first argument into r3invoke directive@end example@section User-Provided RoutinesAll user-provided routines invoked by RTEMS, such asuser extensions, device drivers, and MPCI routines, must alsoadhere to these same calling conventions.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -