This invention relates to subprogram return operations in microprocessors.
Programs frequently feature subroutines which perform a specific task. After the task is performed, program flow returns from the subroutine to the main program. One common mechanism for performing a subroutine return involves conditionally or unconditionally moving the contents of a return address register into a program counter and then continuing program execution. A return value register may also be updated with a constant literal that may represent a Boolean value. Another approach to subprogram returns is to “pop” a return address from the stack and into the program counter and continue program execution from there. This operation may also pop any spooled-out register file contents from the stack into the register file.
These methods for performing subroutine returns take several cycles to execute. In
A similar issue exists for a return instruction (“RETMEM”) popping the return address register from a stack in memory. As shown in
It would be advantageous to provide a more efficient subroutine return operation.
In an exemplary embodiment, an instruction is fetched which requires a return operation and sets status flags based on the contents of a return value register. The status flags are set in parallel with at least one other operation required to process the return instruction. The status flags are set before one of the following occurs: i) contents of a return address register are moved into a program counter; or ii) a return address is popped from a stack and into the program counter. In another embodiment, a processor-readable storage medium causes a processor to perform this subroutine return operation.
In yet another exemplary embodiment of the invention, a subroutine return operation places a return address into a program counter. A test operation is executed on a return value register; the test operation is performed in parallel with at least one other operation required to process the return operation. The program flow is changed to a target address. Each of the above-mentioned steps is performed in response to a single instruction. In one embodiment, a processor-readable storage medium stores an instruction that causes a processor to perform this subroutine return operation.
A more efficient subroutine return operation is provided in which status flags are updated (in the processor's status register) according to a test of the return value register during the subroutine return operation. (In the prior art, test operations, for instance, a test of the return value register, are performed in response to a separate instruction.) In one embodiment, the status flags are set in parallel with operations to execute single instructions such as conditional return instructions as well as single instructions incorporating a return operation. The instructions are stored in a processor-readable medium, which includes any medium that can store or transfer information, such as an electronic circuit, a semiconductor memory device, a ROM, a flash memory, a floppy diskette, a compact disc, an optical disc, etc.
These instructions can be executed by existing hardware. In
In one embodiment of the invention, test operations are performed in parallel with other operations during execution of instructions with the more efficient subroutine return operation. In one embodiment, shown in
In another embodiment, a test operation may be performed during execution of a return instruction (“pop_with_test”) popping the return address register from a stack in memory. In
Other embodiments of the invention may vary from the embodiments discussed above. These embodiments may require fewer or additional clock cycles to execute instructions. Other embodiments may require different hardware to execute the instructions. Still other embodiments may be incorporated into different subprogram return operations and instructions.
Various signals are required from the control/decode unit. Returning to
pcmux_sel 114—Selector signal used to choose if the program counter is going to be updated with the sequential program address or the address given by the return instruction.
wbmux_sel 116—Selector signal used by the writeback stage to determine which address is to be written into the register file or into the program counter. If signal is logic “0,” the address comes from the ALU result from the EX/MEM pipeline stage. If the signal is logic “1,” the address comes from the data memory.
as_ctrl 120—Control signal used to choose if the adder in the ALU will perform subtraction or addition on the operands from the register file.
zeromux_sel 122—Signal used to force input operand B to the ALU to integer value zero.
readreg1124—Register file register number for operand 1.
readreg2126—Register file register for operand 2.
loadflag 128—Control signal to allow the status register to update the flag settings.
writeaddr130—Register file register number for the register where the result is written back.
The following table lists exemplary outputs from the control/decode unit in the cycles of the RET instruction. The registers identified in the table are:
R12—the Return Value Register. Test operations are performed on this register.
LR—the Link Register. Keeps the address to return to after the subprogram has completed. LR may also be referred to as the Return Address Register (RAR)
PC—the Program Counter. Holds the address of the currently executing instruction.
The following table lists exemplary outputs from the control/decode unit in the cycles of the return_with_test instruction.
The following table lists exemplary outputs from the control/decode unit in the cycles of the pop_with_test instruction.
The “return_with_test” and “pop_with_test” instructions can be executed as part of other instructions. For instance, the “return_with_test” instruction can be executed as part of a conditional return instruction, in which there is a return from the subroutine if a specified condition is true. Values are moved into the return register, the return value is tested, and flags are set. A specific example of this instruction is the “ret{cond4}” instruction in the ATMEL AVR32 instruction set. The following pseudocode describes the ret{cond4} instruction (SP is the stack pointer register):
The following table explains some of the mnemonics used above and the pseudocode for the “Load Multiple Registers” instruction, below:
The operation CP R12, 0 is a comparison or subtraction operation without operation. In this particular case, the result of the operation32 R12−0.
Another instruction in which the “return_with_test” operation may be employed is the “Load Multiple Registers” instruction from the AVR32 instruction set. This instruction loads consecutive words pointed to by the register pointer into the register specified in the instruction. The PC can be loaded, resulting in a jump to the loaded target address. If the PC is loaded, the return value in R12 is tested and the flags are updated. The return value optionally may be set to −1, 0, or 1. The following pseudocode describes this instruction (SP is a stack pointer):
Similar instructions employing the “pop_with_test” operation may be employed in which words pointed to by SP are loaded into registers specified in the instruction.
An instruction in which the “pop_with_test” operation may be employed is the Pop Multiple Registers from Stack (“POPM”) instruction from the AVR32 instruction set. This instruction loads the consecutive words pointed to by SP into the registers specified in the instruction.
While specific examples have been cited above showing how the subroutine return operation may be employed in different instructions, other embodiments may incorporate the subroutine operation into different instructions.
One advantage of the more efficient subroutine return operations is the reduction in code size, since an explicit “test return register” instruction can be eliminated since the test operation may be performed implicitly by the return operation. Another advantage is that execution time is reduced since the return register test is performed in parallel with the fetching of the instruction to which the program will return.
The instructions and operations described above may be employed in both RISC and CISC machines.
Although the present invention has been described in terms of specific exemplary embodiments, one skilled in the art will recognize variations and additions to the embodiments may be made without departing from the principles of the present invention. For instance, return operations may require more or fewer cycles to be executed, or the return operations may be part of different instructions, or the processors executing the return operations may have different architectures. In another embodiment, more hardware may be added so the return operations could be completed in one cycle (i.e., the two micro-operations performed in response to a single instruction are completed in one cycle).
This application is a continuation of U.S. patent application Ser. No. 11/149,611, filed on Jun. 10, 2005, which is incorporated herein by reference in its entirety.
Number | Date | Country | |
---|---|---|---|
Parent | 11149611 | Jun 2005 | US |
Child | 12795582 | US |