Call stack: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
VolkovBot (discussione | contributi)
m Bot: Aggiungo: pt:Pilha de chamada
traduzione abbandonata
Riga 1:
{{T|lingua=inglese|argomento=informatica|data=marzo 2007}}
In [[informatica]], un '''call stack''' (dall'inglese ''pila di chiamate'') è uno [[stack]] (ovvero una specie di pila) che immagazzina informazioni sulle [[subroutine]] attive del [[computer]] (le subroutine attive sono quelle che sono state chiamate ma la cui esecuzione non è terminata). Questo tipo di pila è spesso chiamato anche execution stack, control stack, function stack, o run-time stack e spesso semplicemente "stack".
 
Line 11 ⟶ 10:
 
== Scopi del call stack ==
Come descritto sopra, loLo scopo primario del call stack è:
 
Come descritto sopra, lo scopo primario del call stack è:
 
* '''immagazzinare gli indirizzi di ritorno''' - Quando una subroutine viene chiamata, l'indirizzo dell'istruzione di ritorno deve essere salvato da qualche parte. Utilizzando una pila per salvare gli indirizzi di ritorno si hanno importanti vantaggi rispetto alle alternative. Uno di questi è che ogni task ha il proprio stack e pertanto le subroutine possono essere "rientranti" e quindi possono essere attive simultaneamente per task diversi che fanno cose diverse. Un altro vantaggio è che la [[ricorsione]] è automaticamente supportata. Quando una funzione chiama sé stessa ricorsivamente, un indirizzo di ritorno necessita di essere registrato per ogni attivazione della funzione così da poter essere usato per il ritorno da ogni attivazione della funzione. Questa capacità è automatica con un stack.
Line 27 ⟶ 25:
 
* '''Ambiente dei contenitori di subroutines''' - Alcuni linguaggi di programmazione (come [[Pascal (programmazione)|Pascal]] e [[Ada (programmazione)|Ada]]) supportano [[Funzioni annidate|subroutines annidate]], permettendo ad una routine interna di accedere al contesto della sua routine contenitore, cioè' ai parametri e alle variabili locali nell'ambiente della routine esterna. Tali lingue generalmente permettono alle routines di effettuare chiamate ricorsive (la funzione richiama se stessa), ottenendo multiple call stacks per le chiamate di routines delle routines interne, ciascuna delle quali punta sullo stesso ambiente della routine esterna. Questo tipo di call frame è anche conosciuto come ''display''.
 
<!--DA TRADURRE
 
* '''Altro return state''' – Besides the return address, in some environments there may be other machine or software states that need to be restored when a subroutine returns. This might include things like privilege level, exception handling information, arithmetic modes, and so on. If needed, this may be stored in the call stack just as the return address is.
 
The typical call stack is used for the return address, locals, and parameters (known as a ''call frame''). In some environments there may be more or fewer functions assigned to the call stack. In the [[Forth (programming language)|Forth programming language]], for example, only the return address and local variables are stored on the call stack (which in that environment is named the ''return stack''); parameters are stored on a separate ''data stack''. Most Forths also have a third stack for [[floating point]] parameters.
 
==Structure==
A call stack is composed of '''stack frames''' (sometimes called '''activation records'''). Each stack frame corresponds to a call to a subroutine which has not yet terminated with a return. For example, if a subroutine named <code>DrawLine</code> is currently running, having just been called by a subroutine <code>DrawSquare</code>, the top part of the call stack might be laid out like this (where the stack is growing towards the top):
[[Immagine:Call stack layout.svg|center]]
The stack frame at the top of the stack is for the currently executing routine. In the most common approach the stack frame includes space for the local variables of the routine, the return address back to the routine's caller, and the parameter values passed into the routine. The memory locations within a frame are often accessed via a register called the '''stack pointer''', which also serves to indicate the current top of the stack. Alternatively, memory within the frame may be accessed via a separate register, often termed the '''frame pointer''', which typically points to some fixed point in the frame structure, such as the ___location for the return address.
 
Stack frames are not all the same size. Different subroutines have differing numbers of parameters, so that part of the stack frame will be different for different subroutines, although usually fixed across all activations of a particular subroutine. Similarly, the amount of space needed for local variables will be different for different subroutines. In fact, some languages support dynamic allocations of memory for local variables on the stack, in which case the size of the locals area will vary from activation to activation of a subroutine, and is not known when the subroutine is [[compiler|compiled]]. In the latter case, access via a frame pointer, rather than via the stack pointer, is usually necessary since the offsets from the stack top to values such as the return address would not be known at compile time.
 
In many systems a stack frame has a field to contain the previous value of the frame pointer register, the value it had while the caller was executing. For example, in the diagram above, the stack frame of <code>DrawLine</code> would have a memory ___location holding the frame pointer value that <code>DrawSquare</code> uses. The value is saved upon entry to the subroutine and restored for the return. Having such a field in a known ___location in the stack frame allows code to access each frame successively underneath the currently executing routine's frame.
 
Programming languages that support [[nested function|nested subroutines]] have a field in the call frame that points to the call frame of the outer routine that invoked the inner (nested) routine. This is also called a '''display'''. This pointer provides the inner routine (as well as any other inner routines it may invoke) access to the parameters and local variables of the outer invoking routine.
 
For some purposes, the stack frame of a subroutine and that of its caller can be considered to overlap, the overlap consisting of the area where the parameters are passed from the caller to the callee. In some environments, the caller pushes each argument onto the stack, thus extending its stack frame, then invokes the callee. In other environments, the caller has a preallocated area at the top of its stack frame to hold the arguments it supplies to other subroutines it calls. This area is sometimes termed the '''outgoing arguments area''' or '''callout area'''. Under this approach, the size of the area is calculated by the compiler to be the largest needed by any called subroutine.
 
==Use==
===Call site processing===
Usually the call stack manipulation needed at the site of a call to a subroutine is minimal (which is good since there can be many call sites for each subroutine to be called). The values for the actual arguments are evaluated at the call site, since they are specific to the particular call, and either pushed onto the stack or placed into registers, as determined by the [[calling convention]] being used. The actual call instruction, such as "Branch and Link," is then typically executed to transfer control to the code of the target subroutine.
 
===Callee processing===
In the called subroutine, the first code executed is usually termed the [[function prologue|subroutine prologue]], since it does the necessary housekeeping before the code for the statements of the routine is begun.
 
The prologue will commonly save the return address left in a register by the call instruction by pushing the value onto the call stack. Similarly, the current stack pointer and/or frame pointer values may be pushed. Alternatively, some instruction set architectures automatically provide comparable functionality as part of the action of the call instruction itself, and in such an environment the prologue need not do this.
 
If frame pointers are being used, the prologue will typically set the new value of the frame pointer register from the stack pointer. Space on the stack for local variables can then be allocated by incrementally changing the stack pointer.
 
The [[Forth (programming language)|Forth programming language]] allows explicit winding of the call stack (called there the "return stack"). The [[Scheme (programming language)|Scheme programming language]] allows the winding of special frames on the stack through a "[[dynamic wind]]".
 
===Return processing===
When a subroutine is ready to return, it executes an epilogue that undoes the steps of the prologue. This will typically restore saved register values (such as the frame pointer value) from the stack frame, pop the entire stack frame off the stack by changing the stack pointer value, and finally branch to the instruction at the return address. Under many calling conventions the items popped off the stack by the epilogue include the original argument values, in which case there usually are no further stack manipulations that need to be done by the caller. With some calling conventions, however, it is the caller's responsibility to remove the arguments from the stack after the return.
 
===Unwinding===
Returning from the called function will pop the top frame off of the stack, perhaps leaving a return value.
 
Some languages (such as Pascal) allow a global [[GOTO|goto]] statement to transfer control out of a nested function and into a previously invoked outer function. This operation requires the stack to be unwound, removing as many stack frames as necessary to restore the proper context to transfer control to the target statement within the enclosing outer function. Such transfers of control are generally used only for error handling.
 
Other languages provide [[exception handling]], which also requires unwinding of the stack. The stack frame of a function contains one or more entries specifying exception handlers. When an exception is thrown, the stack is unwound until an exception handler is found that is prepared to handle (catch) the exception. [[Common Lisp]] allows control of what happens when the stack is unwound by using the <code>unwind-protect</code> special operator.
 
When applying a [[continuation]], the stack is unwound and then rewound with the stack of the continuation. This is not the only way to implement continuations; for example, using multiple, explicit stacks, application of a continuation can simply activate its stack and wind a value to be passed.
 
==Security==
The mixing of control flow data affecting the execution of code (return addresses, saved frame pointers) and simple program data (parameters, return values) in a call stack is a security risk, possibly [[exploit (computer security)|exploit]]able through [[buffer overflow]]s (in which article the risk and exploitation are explained).
-->
 
== Voci correlate ==