📄 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.6 2002/01/17 21:47:47 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.@section Programming ModelThis section discusses the programming model for theSPARC architecture.@subsection Non-Floating Point RegistersThe SPARC architecture defines thirty-twonon-floating point registers directly visible to the programmer.These are divided into four sets:@itemize @bullet@item input registers@item local registers@item output registers@item global registers@end itemizeEach register is referred to by either two or threenames in the SPARC reference manuals. First, the registers arereferred to as r0 through r31 or with the alternate notationr[0] through r[31]. Second, each register is a member of one ofthe four sets listed above. Finally, some registers have anarchitecturally defined role in the programming model whichprovides an alternate name. The following table describes themapping between the 32 registers and the register sets:@ifset use-ascii@example@group +-----------------+----------------+------------------+ | Register Number | Register Names | Description | +-----------------+----------------+------------------+ | 0 - 7 | g0 - g7 | Global Registers | +-----------------+----------------+------------------+ | 8 - 15 | o0 - o7 | Output Registers | +-----------------+----------------+------------------+ | 16 - 23 | l0 - l7 | Local Registers | +-----------------+----------------+------------------+ | 24 - 31 | i0 - i7 | Input Registers | +-----------------+----------------+------------------+@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 1.75in{\enskip\hfil#\hfil}&\vrule#\cr\noalign{\hrule}&\bf Register Number &&\bf Register Names&&\bf Description&\cr\noalign{\hrule}&0 - 7&&g0 - g7&&Global Registers&\cr\noalign{\hrule}&8 - 15&&o0 - o7&&Output Registers&\cr\noalign{\hrule}&16 - 23&&l0 - l7&&Local Registers&\cr\noalign{\hrule}&24 - 31&&i0 - i7&&Input Registers&\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 Number</STRONG></TD> <TD ALIGN=center><STRONG>Register Names</STRONG></TD> <TD ALIGN=center><STRONG>Description</STRONG></TD><TR><TD ALIGN=center>0 - 7</TD> <TD ALIGN=center>g0 - g7</TD> <TD ALIGN=center>Global Registers</TD></TR><TR><TD ALIGN=center>8 - 15</TD> <TD ALIGN=center>o0 - o7</TD> <TD ALIGN=center>Output Registers</TD></TR><TR><TD ALIGN=center>16 - 23</TD> <TD ALIGN=center>l0 - l7</TD> <TD ALIGN=center>Local Registers</TD></TR><TR><TD ALIGN=center>24 - 31</TD> <TD ALIGN=center>i0 - i7</TD> <TD ALIGN=center>Input Registers</TD></TR> </TABLE></CENTER>@end html@end ifsetAs mentioned above, some of the registers servedefined roles in the programming model. The following tabledescribes the role of each of these registers:@ifset use-ascii@example@group +---------------+----------------+----------------------+ | Register Name | Alternate Name | Description | +---------------+----------------+----------------------+ | g0 | na | reads return 0 | | | | writes are ignored | +---------------+----------------+----------------------+ | o6 | sp | stack pointer | +---------------+----------------+----------------------+ | i6 | fp | frame pointer | +---------------+----------------+----------------------+ | i7 | na | return address | +---------------+----------------+----------------------+@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 1.75in{\enskip\hfil#\hfil}&\vrule#\cr\noalign{\hrule}&\bf Register Name &&\bf Alternate Names&&\bf Description&\cr\noalign{\hrule}&g0&&NA&&reads return 0; &\cr&&&&&writes are ignored&\cr\noalign{\hrule}&o6&&sp&&stack pointer&\cr\noalign{\hrule}&i6&&fp&&frame pointer&\cr\noalign{\hrule}&i7&&NA&&return address&\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>g0</TD> <TD ALIGN=center>NA</TD> <TD ALIGN=center>reads return 0 ; writes are ignored</TD></TR><TR><TD ALIGN=center>o6</TD> <TD ALIGN=center>sp</TD> <TD ALIGN=center>stack pointer</TD></TR><TR><TD ALIGN=center>i6</TD> <TD ALIGN=center>fp</TD> <TD ALIGN=center>frame pointer</TD></TR><TR><TD ALIGN=center>i7</TD> <TD ALIGN=center>NA</TD> <TD ALIGN=center>return address</TD></TR> </TABLE></CENTER>@end html@end ifset@subsection Floating Point RegistersThe SPARC V7 architecture includes thirty-two,thirty-two bit registers. These registers may be viewed asfollows:@itemize @bullet@item 32 single precision floating point or integer registers(f0, f1, ... f31)@item 16 double precision floating point registers (f0, f2,f4, ... f30)@item 8 extended precision floating point registers (f0, f4,f8, ... f28)@end itemizeThe floating point status register (fpsr) specifiesthe behavior of the floating point unit for rounding, containsits condition codes, version specification, and trap information.A queue of the floating point instructions which havestarted execution but not yet completed is maintained. Thisqueue is needed to support the multiple cycle nature of floatingpoint operations and to aid floating point exception traphandlers. Once a floating point exception has been encountered,the queue is frozen until it is emptied by the trap handler.The floating point queue is loaded by launching instructions.It is emptied normally when the floating point completes alloutstanding instructions and by floating point exceptionhandlers with the store double floating point queue (stdfq)instruction.@subsection Special RegistersThe SPARC architecture includes two special registerswhich are critical to the programming model: the Processor StateRegister (psr) and the Window Invalid Mask (wim). The psrcontains the condition codes, processor interrupt level, trapenable bit, supervisor mode and previous supervisor mode bits,version information, floating point unit and coprocessor enablebits, and the current window pointer (cwp). The cwp field ofthe psr and wim register are used to manage the register windowsin the SPARC architecture. The register windows are discussedin more detail below.@section Register WindowsThe SPARC architecture includes the concept ofregister windows. An overly simplistic way to think of thesewindows is to imagine them as being an infinite supply of"fresh" register sets available for each subroutine to use. Inreality, they are much more complicated.The save instruction is used to obtain a new registerwindow. This instruction decrements the current window pointer,thus providing a new set of registers for use. This registerset includes eight fresh local registers for use exclusively bythis subroutine. When done with a register set, the restoreinstruction increments the current window pointer and theprevious register set is once again available.The two primary issues complicating the use ofregister windows are that (1) the set of register windows isfinite, and (2) some registers are shared between adjacentregisters windows.Because the set of register windows is finite, it ispossible to execute enough save instructions withoutcorresponding restore's to consume all of the register windows.This is easily accomplished in a high level language becauseeach subroutine typically performs a save instruction uponentry. Thus having a subroutine call depth greater than thenumber of register windows will result in a window overflowcondition. The window overflow condition generates a trap whichmust be handled in software. The window overflow trap handleris responsible for saving the contents of the oldest registerwindow on the program stack.Similarly, the subroutines will eventually completeand begin to perform restore's. If the restore results in theneed for a register window which has previously been written tomemory as part of an overflow, then a window underflow conditionresults. Just like the window overflow, the window underflowcondition must be handled in software by a trap handler. Thewindow underflow trap handler is responsible for reloading thecontents of the register window requested by the restoreinstruction from the program stack.The Window Invalid Mask (wim) and the Current WindowPointer (cwp) field in the psr are used in conjunction to managethe finite set of register windows and detect the windowoverflow and underflow conditions. The cwp contains the indexof the register window currently in use. The save instructiondecrements the cwp modulo the number of register windows.Similarly, the restore instruction increments the cwp modulo thenumber of register windows. Each bit in the wim representsrepresents whether a register window contains valid information.The value of 0 indicates the register window is valid and 1indicates it is invalid. When a save instruction causes the cwpto point to a register window which is marked as invalid, awindow overflow condition results. Conversely, the restoreinstruction may result in a window underflow condition.Other than the assumption that a register window isalways available for trap (i.e. interrupt) handlers, the SPARCarchitecture places no limits on the number of register windowssimultaneously marked as invalid (i.e. number of bits set in thewim). However, RTEMS assumes that only one register window ismarked invalid at a time (i.e. only one bit set in the wim).This makes the maximum possible number of register windowsavailable to the user while still meeting the requirement thatwindow overflow and underflow conditions can be detected.The window overflow and window underflow traphandlers are a critical part of the run-time environment for aSPARC application. The SPARC architectural specification allowsfor the number of register windows to be any power of two lessthan or equal to 32. The most common choice for SPARCimplementations appears to be 8 register windows. This resultsin the cwp ranging in value from 0 to 7 on most implementations.The second complicating factor is the sharing ofregisters between adjacent register windows. While eachregister window has its own set of local registers, the inputand output registers are shared between adjacent windows. Theoutput registers for register window N are the same as the inputregisters for register window ((N - 1) modulo RW) where RW isthe number of register windows. An alternative way to think ofthis is to remember how parameters are passed to a subroutine onthe SPARC. The caller loads values into what are its outputregisters. Then after the callee executes a save instruction,those parameters are available in its input registers. This isa very efficient way to pass parameters as no data is actuallymoved by the save or restore instructions.@section Call and Return MechanismThe SPARC architecture supports a simple yeteffective call and return mechanism. A subroutine is invokedvia the call (call) instruction. This instruction places thereturn address in the caller's output register 7 (o7). Afterthe callee executes a save instruction, this value is availablein input register 7 (i7) until the corresponding restoreinstruction is executed.The callee returns to the caller via a jmp to thereturn address. There is a delay slot following thisinstruction which is commonly used to execute a restoreinstruction -- if a register window was allocated by thissubroutine.It is important to note that the SPARC subroutinecall and return mechanism does not automatically save andrestore any registers. This is accomplished via the save andrestore instructions which manage the set of registers windows.@section Calling MechanismAll RTEMS directives are invoked using the regularSPARC calling convention via the call instruction.@section Register UsageAs discussed above, the call instruction does notautomatically save any registers. The save and restoreinstructions are used to allocate and deallocate registerwindows. When a register window is allocated, the new set oflocal registers are available for the exclusive use of thesubroutine which allocated this register set.@section Parameter PassingRTEMS assumes that arguments are placed in thecaller's output registers with the first argument in outputregister 0 (o0), the second argument in output register 1 (o1),and so forth. Until the callee executes a save instruction, theparameters are still visible in the output registers. After thecallee executes a save instruction, the parameters are visiblein the corresponding input registers. The following pseudo-codeillustrates the typical sequence used to call a RTEMS directivewith three (3) arguments:@exampleload third argument into o2load second argument into o1load first argument into o0invoke 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 calling conventions.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -