1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Chapter 10 implementing subprograms

75 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Implementing Subprograms
Trường học Addison-Wesley
Năm xuất bản 2006
Định dạng
Số trang 75
Dung lượng 572,93 KB

Nội dung

Chapter 10 Implementing Subprograms ISBN 0-321-33025-0 Chapter 10 Topics • The General Semantics of Calls and Returns • Implementing “Simple” Subprograms • Implementing Subprograms with StackDynamic Local Variables • Nested Subprograms ã Blocks ã Implementing Dynamic Scoping Copyright â 2006 Addison-Wesley All rights reserved 1-2 The General Semantics of Calls and Returns • The subprogram call and return operations of a language are together called its subprogram linkage • Any implementation method for subprograms must be based on the semantics of subprogram linkage • Subprogram calls are strictly nested: the caller waits until the callee has terminated Copyright © 2006 Addison-Wesley All rights reserved 1-3 The General Semantics … (cont.) A B C D call C call B Copyright © 2006 Addison-Wesley All rights reserved call D 1-4 The General Semantics … (cont.) • A subprogram call has two semantic actions associated with it – Call semantics – Return semantics Copyright © 2006 Addison-Wesley All rights reserved 1-5 Call Semantics • Save the execution status of the caller • Carry out the parameter-passing process • Pass the return address to the callee • Transfer control to the callee • The call must cause some mechanism to be created to provide access to nonlocal variables that are visible to the callee Copyright © 2006 Addison-Wesley All rights reserved 1-6 Return Semantics • If the subprogram has parameters that are out mode or inout mode, the first action is to move the local values of the associated formal parameters to the actual parameters • If it is a function, move the functional value to a place the caller can get it • Deallocate the storage used for local variables • Restore the execution status of the caller • Transfer control back to the caller Copyright © 2006 Addison-Wesley All rights reserved 1-7 Implementing “Simple” Subprograms: Parts • Required Storage: Status information of the caller, parameters, return address, and functional value (if it is a function) • Two separate parts: the actual code and the noncode part (local variables and data that can change) • The format, or layout, of the noncode part of an executing subprogram is called an activation record (AR) Copyright © 2006 Addison-Wesley All rights reserved 1-8 Parts (cont.) • An activation record instance (ARI) is a concrete example of an AR • The AR of the callee is placed at the top of the run-time stack, directly above the AR of the caller • ARs vary in size Sizes are usually determined at compile time • Every AR contains the same kind of information about the caller and the callee Copyright © 2006 Addison-Wesley All rights reserved 1-9 An AR for “Simple” Subprograms [Functional value] Local variables Parameters Return address Copyright © 2006 Addison-Wesley All rights reserved 1-10 Blocks • Block is user-specified local scope for variables • An example in C/C++ { int temp = a[i]; a[i] = a[j]; a[j] = temp; } – The lifetime of temp in the above example begins when control enters the block – An advantage of using a local variable like temp is that it cannot be interfered with any other variable with the same name Copyright © 2006 Addison-Wesley All rights reserved 1-61 Implementing Blocks • Two Methods: Treat blocks as parameterless subprograms  Every block has an ARI, it is created every time the block is executed Since the maximum storage required for a block can be statically determined, this amount of space can be allocated on top of the current ARI  Must use a different method to access locals  A little more work for the compiler writer Copyright © 2006 Addison-Wesley All rights reserved 1-62 Example void MAIN() { int x, y, z; while (…) { int a, b, c; … while (…) { int d, e; … } } while (…) { int f, g; … } … } Copyright © 2006 Addison-Wesley All rights reserved e Block variables d c b and g a and f z y ARI for MAIN Locals x … 1-63 Implementing Parameters That Are Subprogram Names • Static chaining – A subprogram that passes a subprogram name as a parameter must have in its static ancestors the unit in which that passed subprogram was declared – The compiler can simply pass the link to the static parent of the subprogram to be passed, along with the subprogram name The ARI of the subprogram that is passed is then initialized with this link in its static link field Copyright © 2006 Addison-Wesley All rights reserved 1-64 Implementing Parameters That Are Subprogram Names (cont.) • Display  The entire existing display is saved for every call to a subprogram that has been passed as a parameter, often in the ARI of the subprogram in execution  When the passed subprogram terminates, the complete saved display replaces the display used for the passed subprogram's execution Copyright © 2006 Addison-Wesley All rights reserved 1-65 Referencing Environment Confusion program MAIN; procedure SUBl; begin ARI for SUB3 … end; procedure SUB2(procedure SUBX); ARI for SUB2 var SUM : real; called from procedure SUB3; SUB2 begin SUM := 0.0; … end; ARI for SUB2 begin called from SUBX; MAIN SUB2(SUB3); … end; ARI for MAIN begin … SUB2(SUB1); … end.Copyright © 2006 Addison-Wesley All rights reserved Top Dynamic link Static link Return (to SUB2) Local SUB3 Parameter Dynamic link Static link Return (to SUB2) Local SUB1 Parameter Dynamic link Static link Return (to MAIN) SUM SUBX SUM SUBX X 1-66 Implementing Dynamic Scoping • Deep Access: non-local references are found by searching the ARIs on the dynamic chain • Shallow Access: An alternative implementation method, not an alternative semantics Copyright © 2006 Addison-Wesley All rights reserved 1-67 Deep Access • This concept is somewhat similar to that of accessing nonlocal variables in a static scoped language, except that the dynamic chain is followed • The dynamic chain links together all ARIs of subprograms in the reverse of the order in which they were activated • This method is called deep access because access may require searches deep in the stack Copyright © 2006 Addison-Wesley All rights reserved 1-68 procedure C; integer x, z; begin x := u + v; … end; procedure B; integer w, x; begin … end; procedure A; integer v, w; begin … end; program MAIN; integer v, u; begin … end; Copyright © 2006 Addison-Wesley All rights reserved Top ARI for C Local Local Dynamic link Return (to B) z x ARI for B Local Local Dynamic link Return (to A) x w ARI for A Local Local Dynamic link Return (to A) w v ARI for A Local Local Dynamic link Return (to MAIN) w v ARI for MAIN Local Local u v 1-69 Evaluations of Deep Access • Length of chain cannot be statically determined Every ARI in the chain must be searched until the first instance of the variable is found – This is one reason why dynamic-scoped languages typically have slower execution speeds than staticscoped languages • ARIs must store the names of variables for the search process, whereas in static-scoped language implementations only the values are required: nesting_depth and local_offset Copyright © 2006 Addison-Wesley All rights reserved 1-70 Shallow Access • Local variables in subprograms are not stored in the ARIs of those subprograms There is a separate stack for each variable name in a complete program • Every time a new variable is created by a declaration at the beginning of a subprogram, the variable is given a cell on the stack for its name • When a subprogram terminates, the lifetime of its local variables ends, and the cells/stack for those variable names are popped Copyright © 2006 Addison-Wesley All rights reserved 1-71 procedure C; int x, z; begin x := u + v; … end; procedure B; int w, x; begin … end; A B procedure A; int v, w; A C A begin MAIN MAIN B C A … u v x z w end; MAIN  A  A  B  C program MAIN; The names on the top of stacks indicate int v, u; begin the program units where the variables … are declared end; Copyright © 2006 Addison-Wesley All rights reserved 1-72 Deep Access vs Shallow Access • The choice between shallow and deep access to nonlocals depends on the relative frequencies of subprogram calls and nonlocal references – The deep access provides fast subprogram linkage, but references to nonlocals, especially references to distant nonlocals are costly – The shallow access provides much faster references to nonlocals, especially distant nonlocals, but is more costly in terms of subprogram linkage Copyright © 2006 Addison-Wesley All rights reserved 1-73 Summary • Subprogram linkage semantics requires many action by the implementation • Simple subprograms have relatively basic actions • Stack-dynamic languages are more complex • Subprograms with stack-dynamic local variables and nested subprograms have two components – Actual code – Activation record Copyright © 2006 Addison-Wesley All rights reserved 1-74 Summary (cont.) • Activation record instances contain formal parameters and local variables among other things • Static chains are the primary method of implementing accesses to non-local variables in static-scoped languages with nested subprograms • Access to non-local variables in dynamic-scoped languages can be implemented by use of the dynamic chain or thru some central variable table method Copyright © 2006 Addison-Wesley All rights reserved 1-75 .. .Chapter 10 Topics • The General Semantics of Calls and Returns • Implementing “Simple” Subprograms • Implementing Subprograms with StackDynamic Local Variables • Nested Subprograms. .. Transfer control back to the caller Copyright © 2006 Addison-Wesley All rights reserved 1-7 Implementing “Simple” Subprograms: Parts • Required Storage: Status information of the caller, parameters,... reserved 1-9 An AR for “Simple” Subprograms [Functional value] Local variables Parameters Return address Copyright © 2006 Addison-Wesley All rights reserved 1 -10 MAIN Common storage Local variables

Ngày đăng: 23/03/2022, 08:30