procedure and object oriented abstraction
play

Procedure and Object- Oriented Abstraction Scope and storage - PowerPoint PPT Presentation

Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1 Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks Paired function call


  1. Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1

  2. Procedure abstractions Procedures are fundamental programming abstractions  They are used to support dynamically nested blocks   Paired function call and return jumps They have standalone semantics defined by an abstraction interface   input parameters, return values, global side effects Procedures are units of separate compilation  They represent parameterized blocks of computation  float foo(…) float bar(…) int main(int argc, char* argv[]) { { { int a, b, c; float r; float a; …. …. … r = bar(…); return r; a = foo(…) return r; } … } } cs5363 2

  3. Scoping rules  Global and local variables program main(input,output); var x : integer; outer block x 0 function g(z: integer) :integer; begin g := x+z end; h(3) z 3 function h(z: integer) :integer; x 1 var x : integer; begin x := 1; h:=g(z) end; g(12) z 3 begin x := 0; print(h(3)) end  Static scoping  Find global variables in enclosing blocks in program text  Dynamic scoping  Find global variables in the most recently evaluated blocks  Easier to implement in interpreted languages  What is the scoping rule for C/C++, Java? cs5363 3

  4. Simplified memory model  Runtime stack: activation records of blocks/functions  Block entry: add new data to stack  Block exit: remove outdated data  Heap: data of varying lifetime  Variables that last throughout the program  Address may be contained by variables on the runtime stack Data Code …… Stack Program Counter Heap/ static data Activation record pointer(rarp) cs5363 4

  5. Managing Data Storage  Local variables --- activation records on stack  Declared inside a block (e.g. function body)  Enter block: allocate space  Exit block: de-allocate space  Local variables in an enclosing block  Already allocated before entering current Block  Remain allocated after exiting current block  Function parameters and return value  Allocated and initialized before entering function body  Formal parameters dallocated after exiting function body  Global/static variables --- static data areas  Allocated when program is loaded to memory  Storage remain until program exits  Dynamically allocated variables --- heap  Storage dynamically allocated at runtime (e.g., malloc in C)  Storage remain until explicitly de-allocated or garbage collected cs5363 5

  6. Activation Record  Allocate storage for each block dynamically  Allocate an activation record before evaluating each block  Storage for each local variable determined as compile time  Values of local variables evaluated at runtime  Delete the activation record after block exits Allocate AR with space for x, y { int x=0; Set values of x, y int y=x+1; Allocate AR for inner block { int z=(x+y)*(x-y); Set value of z }; Delete AR for inner block }; Delete AR for outer block May need space for intermediate results such as (x+y), (x-y ) cs5363 6

  7. Activation Records For Inline Blocks Caller’s ARP { int x=0; Access link int y=x+1; x 0 { int z=(x+y)*(x-y); y 1 }; }; Caller’s ARP  Push activation record on stack Access link  Set caller ARP to rarp  Set rarp to new AR z -1  Pop activation record off stack x+y 1  Reset rarp to caller’s ARP x-y -1  When making function calls  Caller must also set return Activation record pointer(rarp) address, return value addr, saved registers, and parameters cs5363 7

  8. Activation Records For Procedures  Access link  Pointer to activation record of the Caller’s ARP enclosing block  Return address Access link  Pointer to the instruction immediately following function call Return address  Return-result address  Address of the storage to put the Return-result addr result to be returned  Register save area Register save area  Save register values before function call Parameters  Restore register values before return Local variables  Parameters  Storage for function parameters Activation record pointer(rarp)  Values initialized by caller cs5363 8

  9. Linkage Convention: Implementing Function Calls Precall  Procedure p Push callee’s AR (increment rarp)  prologue Set caller’s ARP  Procedure q Set return address  Set return result addr  l prologue l a c Save live register values  precall initialize formal parameters  return Postreturn  postreturn Restore live register values  Pop callee’s AR(decrement rarp)  epilogue Prologue  Initialize local variables  epilogue Load local environment (access  link) Epilogue Linkage convention:  programs in different files must Deallocate local variables  follow a single contract of Goto return address  function call implementation cs5363 9

  10. Parameter Passing Formal and actual parameters  Formal parameter Parameter declarations and  initializations Pass-by-value  int f (int x) Formal parameters contain values of  actual parameters { Callee cannot change values of actual x := x+1; return x;  parameters }; Pass-by-reference main() {  Formal parameters contain locations int y = 0;  of actual parameters print f(y)+y; Callee can change values of actual  } parameters Formal parameters in activation  record may be aliased Actual parameter  Aliasing: two names refer to same location What about pass-by-pointer (in C)?  cs5363 10

  11. Example: What is the final result? pseudo-code  Draw the activation pass-by-ref records for the evaluation =>2 int f (int x)  What parameter passing is supported { by the languages you x := x+1; return x; know? }; main() { pass-by-value int y = 0; =>1 print f(y)+y; } cs5363 11

  12. Exercise: Managing Function Calls 1: program main(input,output) 2: var x : integar; 3: function f(y : integer) 4: begin 5: f = (x + y) - 2 6: end 7: function g(function h(b:integer):integer) 8: begin 9: var x : integer; 10: x := 7; 11: g = h(x); 12: end 13: begin 14: x := 5; 15: g(f); 16: end cs5363 12

  13. Accessing Variables In Memory  Each memory store has an address Accessing local variable a:  Base address: the starting LoadAI rarp, @a => r1 address of a data area  Local variables of current block loadI @a => r1 activation record pointer (rarp) loadA0 rarp, r1 => r2  Offset: the number of bytes after the base address loadI @a => r1  Local variables of current block Add rarp, r1 => r2 predetermined at compile time load r2 => r3  Address of variable  base address + offset cs5363 13

  14. Accessing Global/Static Variables  Allocated separately in static data area Accessing global variable fee:  Base address unknown until LoadI &fee => r1 program is loaded into memory Load r1 => r2  Use symbolic labels to substitute at compile time  Symbolic labels replaced with Accessing foo.a: runtime value by assembler and loader LoadI &foo => r1  Offset calculated at compile time LoadAI r1, @foo_a => r3  Individual variables: offset=0  Group of data LoadI &foo => r1  layout pre-determined Add r1, @foo_a => r2 Load r2 => r3 cs5363 14

  15. Variables of Enclosing Blocks Outer block control link int x=1; access link int g(int z) { return x+z; } x 1 int h(int z) { h(3) int x = 1; control link return g(z); } h(3) access link print h(3); z 3 x 1 Use access link to find AR of an enclosing block (static scoping) control link  Access link is always set to frame g(3) access link of closest enclosing lexical block z 3 cs5363 15

  16. Coordinates of Variables  Accessing local variables int x=1;  Offset calculated at compile time int g(int z) { return x +z; }  Need to find the base address int h(int z) {  The AR that contains the variable int x = 1;  Lexical level of a block return g(z); }  Number of enclosing scopes print h(3);  g: 1; h: 1; outer-block: 0  For each variable x  Coordinate of x is <n, o>, where Coordinate for x: <0,8>  n: lexical level of block that Lexical level of g: 1 defines x Load instructions:  O: offset of x in it’s AR loadAI rarp, 4 => r1  If a block at lexical level m loadAI r1, 8 => r2 references x  Follow access link m-n times to find the base address for x cs5363 16

  17. Global Display  Allocate a global array Runtime stack (global display)  hold the address of most Level0 AR recent ARs at each lexical … Display level  When pushing a new AR, level0 save the previous AR at Level1 AR level1 the same lexical level, modify global display level2 …  When popping an AR, level3 restore the global display Level2 AR with saved AR at the current lexical level …  To access variable <n,o>  use the ARP at element n Level3 AR of the global display … cs5363 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend