CSE 3302 Programming Languages

Slides:



Advertisements
Similar presentations
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Advertisements

The University of Adelaide, School of Computer Science
Parameter Passing. Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
CSE 5317/4305 L11: Storage Allocation1 Storage Allocation Leonidas Fegaras.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Runtime Environments Source language issues Storage organization
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Compiler Principle and Technology Prof. Dongming LU April 11th, 2014.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Compiler Construction
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Basic Semantics Associating meaning with language entities.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CS 261 – Data Structures Introduction to C Programming.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
CSC 8505 Compiler Construction Runtime Environments.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
CSE 3302 Programming Languages
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Object Lifetime and Pointers
Dynamic Storage Allocation
Data Types In Text: Chapter 6.
Run-Time Environments Chapter 7
CSE 3302 Programming Languages
Subprograms The basic abstraction mechanism.
CSE 3302 Programming Languages
Programmazione I a.a. 2017/2018.
Semantics CSE 340 – Principles of Programming Languages Spring 2016
CSE 3302 Programming Languages
CSE 3302 Programming Languages
CSC 533: Programming Languages Spring 2015
Topic 3-b Run-Time Environment
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CSE 3302 Programming Languages
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Runtime Environments What is in the memory?.
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
SPL – PS2 C++ Memory Handling.
CSE 3302 Programming Languages
Presentation transcript:

CSE 3302 Programming Languages Control II Procedures and Environments Based on slides by Chengkai Li Kelly Scott French University of Texas at Arlington Lecture 10 – Control II, Spring 2017 1 CSE3302 Programming Languages, UT-Arlington

Procedures vs. Functions no side effect return a value Function call: expression Procedure: side effect, executed for it no return value Procedure call: statement No clear distinction made in most languages C/C++: void Ada/FORTRAN/Pascal: procedure/function Lecture 10 – Control II, Spring 2017 2 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Syntax Terminology: body specification interface or Signature name type of return value parameters (names and types) int f(int y); //declaration int f(int y){ //definition int x; x=y+1; return x; } int f(int y) { int x; x=y+1; return x; } Lecture 10 – Control II, Spring 2017 3 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Procedure Call Caller: Callee: … int f(int y){ f(a); int x; … if (y==0) return 0; x=y+1; return x; } Control transferred from caller to callee, at procedure call Transferred back to caller when execution reaches the end of body Can return early Lecture 10 – Control II, Spring 2017 4 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Environment Environment: binding from names to their attributes stack static(global) area heap (unallocated) automatically-allocated spaces (local variables, procedures (chapter 8) under the control of runtime system both for dynamic binding manually-allocated spaces under the control of programmer Lecture 10 – Control II, Spring 2017 5 CSE3302 Programming Languages, UT-Arlington

Activation Record for Nested Blocks Activation record: memory allocated for the local objects of a block Entering a block: activation record allocated Exit from inner block to surrounding block: activation record released int x; //global { int x,y; x = y*10; { int i; i = x/2; } } x x y Lecture 10 – Control II, Spring 2017 6 CSE3302 Programming Languages, UT-Arlington

Activation Record for Nested Blocks int x; //global { int x,y; x = y*10; { int i; i = x/2; } } x x y X: Nonlocal variable, in the surrounding activation record i Lecture 10 – Control II, Spring 2017 7 CSE3302 Programming Languages, UT-Arlington

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } x x y Lecture 10 – Control II, Spring 2017 8 CSE3302 Programming Languages, UT-Arlington

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } x x x: global variable in defining environment y i Need to retain information in calling environment Lecture 10 – Control II, Spring 2017 9 CSE3302 Programming Languages, UT-Arlington

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } i: local variable in called environment x: global variable in defining environment x x y x,y: local variable in calling environment i Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } Can only access global variables in defining environment No direct access to the local variables in the calling environment (Need to communicate through parameters) x x y i Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Procedure Call Caller: Callee: … int f(int a){ f(i); ...; … ...a...; ...; } actual parameter / argument formal parameter / argument Parameter Passing Mechanisms: When and how to evaluate parameters How actual parameter values are passed to formal parameters How formal parameter values are passed back to actual parameters Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Parameter Passing Mechanisms Pass/Call by Value Pass/Call by Reference Pass/Call by Value-Result Pass/Call by Name Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Example What is the result? void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); } It depends… Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Pass by Value Caller: Callee: … int f(int a){ f(i); ...a...; … } i a Most common one Replace formal parameters by the values of actual parameters Actual parameters: No change Formal parameters: Local variables (C, C++, Java, Pascal) Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Example: Pass By Value void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); i 1 j 2 a b i 1 j 2 a b i 1 j 2 a b Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Are these Pass-by-Value? C: void f(int *p) { *p = 0; } void f(int a[]) { a[0]=0; } Java: void f(Vector v) { v.removeAll(); } Yes! Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Value: Pointers C: void f(int *p) { *p = 0; } main() { int *q; q = (int *) malloc(sizeof(int)); *q = 1; f(q); printf("%d\n", q[0]); } q ? q 1 p q ? 1 p q 1 p q Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Value: Pointers C: void f(int *p) { p = (int *) malloc(sizeof(int)); *p = 0; } main() { int *q; q = (int *) malloc(sizeof(int)); *q = 1; f(q); printf("%d\n", q[0]); } What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Value: Arrays C: void f(int p[]) { p[0] = 0;} main() { int q[10]; q[0]=1; f(q); printf("%d\n", q[0]); } What happens here? Lecture 10 – Control II, Spring 2008 CSE3302 Programming Languages, UT-Arlington

Pass-by-Value: Arrays C: void f(int p[]) { p=(int *) malloc(sizeof(int)); p[0] = 0; } main() { int q[10]; q[0]=1; f(q); printf("%d\n", q[0]); } What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Value: Java Objects void f(Vector v) { v.removeAll(); } main() { Vector vec; vec.addElement(new Integer(1)); f(vec); System.out.println(vec.size()); } What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Value: Java Objects void f(Vector v) { v = new Vector(); v.removeAll(); } main() { Vector vec; vec.addElement(new Integer(1)); f(vec); System.out.println(vec.size()); } What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Pass by Reference Caller: Callee: … int f(int a){ f(i); ...a...; … } i a Formal parameters become alias of actual parameters Actual parameters: changed by changes to formal parameters Examples: Fortran: the only parameter passing mechanism C++ (reference type, &) /Pascal (var) Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Example: Pass By Reference C++ syntax. Not valid in C void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); i 1 j 2 a b i 1 j 2 a b i 2 j 1 a b Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Reference: How to minic it in C? void f(int *p) { *p = 0; } main() { int q; q = 1; f(&q); printf(“%d\n”, q); } It is really pass-by-value. Why? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

It is really pass-by-value C: void f(int *p) { p = (int *) malloc(sizeof(int)); *p = 0; } main() { int q; q = 1; f(&q); printf(“%d\n”, q); } Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Reference: C++ Constant Reference void f(const int & p) { int a = p; p = 0; } main(){ int q; q = 1; f(q); printf("%d\n", q); What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Reference: C++ Reference-to-Pointer void f(int * &p) { *p = 0; } main(){ int *q; int a[10]; a[0]=1; q=a; f(q); printf("%d, %d\n", q[0], a[0]); } What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Reference: C++ Reference-to-Pointer void f(int * &p) { p = new int; *p = 0; } main(){ int *q; int a[10]; a[0]=1; q=a; f(q); printf("%d, %d\n", q[0], a[0]); } What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Reference: C++ Reference-to-Array void f(int (&p)[10]) { p[0]=0; } main(){ int *q; int a[10]; a[0]=1; q = a; f(a); printf("%d, %d\n", q[0], a[0]); What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Pass by Value-Result Caller: Callee: … int f(int a){ f(i); ...a...; … } i a i a Combination of Pass-by-Value and Pass-by-Reference (Pass-by-Reference without aliasing) Replace formal parameters by the values of actual parameters Value of formal parameters are copied back to actual parameters Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Example: Pass By Value-Result void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); i 1 j 2 a b i 1 j 2 a b i 2 j 1 a b Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Unspecified Issues void f(int a, int b) { a = 1; b = 2; } main(){ int i=0; f(i,i); printf(“i=%d\n”, i); i a b i a b 1 ? 2 Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Pass by Name Caller: Callee: … int f(int a){ f(i); ...a...; … } Actual parameters only evaluated when they are needed The same parameter can be evaluated multiple times Evaluated in calling environment Essentially equivalent to normal order evaluation Example: Algol 60 Not adopted by any major languages due to implementation difficulty Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Example: Pass By Name void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); a (i) 1 j 2 temp a (i) 1 b (j) 2 temp a (i) 2 b (j) temp 1 Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Pass-by-Name: Side Effects int p[3]={1,2,3}; int i; void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ i = 1; swap(i, p[i]); printf(“%d, %d\n”, i, p[i]); What happens here? Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Some Variants Pass by Name Evaluated at every use, in the calling environment Pass by Need Evaluated once, memorized for future use Pass by Text (Macro) Evaluated using the called environment. All belong to Non-strict evaluation (lazy evaluation) Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Comparisons Call by Value Efficient. No additional level of indirection. Less flexible and less efficient without pointer. (array, struct, union as parameters) Call by Reference Require one additional level of indirection (explicit dereferencing) If a parameter is not variable (e.g., constant), a memory space must be allocated for it, in order to get a reference. Easiest to implement. Call by Value-Result You may not want to change actual parameter values when facing exceptions. Call by Name Lazy evaluation Difficult to implement Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Heap-Allocated Data Dynamic memory management The simplest heap allocator that does not reclaim storage: char heap[heap_size]; int end_of_heap = 0; void* malloc ( int size ) { void* loc = (void*) &heap[end_of_heap]; end_of_heap += size; return loc; }; Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington With a Free List Need to recycle the dynamically allocated data that are not used This is done using free in C or delete in C++ Need to link all deallocated data in the heap into a list Initially, the free list contains only one element that covers the entire heap (ie, it's heap_size bytes long) typedef struct Header { struct Header *next; int size; } Header; Header* free_list; free simply puts the recycled cell at the beginning of the free list: void free ( void* x ) { if ("size of *x" <= sizeof(Header)) return; ((Header*) x)->next = free_list; ((Header*) x)->size = "size of *x"; free_list = (Header*) x; }; Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Malloc malloc first searches the free list to find a cell large enough to fit the given number of bytes. If it finds one, it gets a chunk out of the cell leaving the rest untouched: void* malloc ( int size ) { Header* prev = free_list; for (Header* r=free_list; r!=0; prev=r, r=r->next) if (r->size > size+sizeof(Header)) { Header* new_r = (Header*) (((char*) r)+size); new_r->next = r->next; new_r->size = r->size; if (prev==free_list) free_list = new_r; else prev->next = new_r; return (void*) r; }; void* loc = (void*) &heap[end_of_heap]; end_of_heap += size; return loc; }; Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

CSE3302 Programming Languages, UT-Arlington Reference Counting Keeping track of pointer assignments If more than one objects point to a dynamically allocated object, then the latter object should be deleted only if all objects that point to it do not need it any more you need to keep track of how many pointers are pointing to each object Used to be popular for OO languages like C++ Note: this is not automatic garbage collection because the programmer again is responsible of putting counters to every object to count references This method is easy to implement for languages like C++ where you can redefine the assignment operator dest=source, when both dest and source are pointers to data Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington

Reference Counting (cont.) Instead of using C++ for a pointer to an object C, we use Ref<C>, where the template Ref provides reference counting to C: template< class T > class Ref { private: int count; T* pointer; void MayDelete () { if (count==0) delete pointer; }; void Copy ( const Ref &sp ) { ++sp.count; count = sp.count; pointer = sp.pointer; public: Ref ( T* ptr = 0 ) : count(1), pointer(ptr) {}; Ref ( const Ref &sp ) { Copy(sp); }; ~Ref () { MayDelete(); }; T* operator-> () { return pointer; }; Ref& operator= ( const Ref &sp ) { if (this != &sp) { count--; MayDelete(); Copy(sp); }; return *this; Lecture 10 – Control II, Spring 2017 CSE3302 Programming Languages, UT-Arlington