binding - ncatwilliams.comp.ncat.edu/comp360/binding.pdf · location •in java, ... offsets from...

38
Binding COMP360

Upload: leque

Post on 26-Aug-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Binding

COMP360

Page 2: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

“One Ring to rule them all,

One Ring to find them,

One Ring to bring them all,

And in the darkness bind them.”

inscription on the One Ring

J. R. R. Tolkien

Page 3: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Reading

Read chapter 10 of the textbook

Page 4: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Binding

•A binding is an association between two things, such as a name and the thing it names

•A name is exactly what you think it is• Most names are identifiers• symbols (like '+') can also be names

• The scope of a binding is the part of the program in which the binding is active

from Programming Language Pragmatics, 4th ed, by Michael Scott

Page 5: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

What are we binding?

•At some point a variable name is bound to a memory location

• In Java, the keyword this means this object. It is bound to a specific object when the method is executed

•Keywords (such as final, synchronized or long) are bound to a specific concept at language design

Page 6: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Binding Time

•Binding Time is the point at which a binding is created or, more generally, the point at which any implementation decision is made

• Some bindings are made before the program is executed

•Other bindings are made during program execution

from Programming Language Pragmatics, 4th ed, by Michael Scott

Page 7: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Binding Time Examples

• Language design time – meaning of keywords

• Language implementation time – implementation of types

•Program writing time – names to concepts

•Compile time – variable names to relative addresses, program statements to machine language

• Link time - layout of whole program in memory

Page 8: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Run Time Binding Examples

•Program load – variables and methods assigned to physical addresses

•Method Entry – actual parameter values bound to formal parameters, local variables bound to memory locations

Page 9: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Static and Dynamic

• The terms STATIC and DYNAMIC are generally used to refer to things bound before run time and at run time, respectively

• The period of time from creation to destruction is called the LIFETIME of a binding• If an object outlives binding, it's garbage• If a binding outlives an object, it's a dangling reference

from Programming Language Pragmatics, 4th ed, by Michael Scott

Page 10: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Pros and Cons

• In general, early binding times are associated with greater efficiency

• Later binding times are associated with greater flexibility

•Compiled languages tend to have early binding times

• Interpreted languages tend to have later binding times

•We generally talk about the binding of identifiers to the variables they name

from Programming Language Pragmatics, 4th ed, by Michael Scott

Page 11: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

When is a value bound to dog?

int myMethod( double dog )

A. Writing of the program

B. Compile time

C. Program linking

D. Program load

E. Method entry

Page 12: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Storage Management

• Storage Allocation mechanisms• Static• Stack• Heap

• Static allocation for• code• globals• static or own variables• explicit constants (including strings, sets, etc)• scalars may be stored in the instructions

from Programming Language Pragmatics, 4th ed, by Michael Scott

Page 13: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Stack

•Call stack for• parameters• local variables• temporaries

•Why a stack?• allocate space for recursive routines

(not necessary in FORTRAN – no recursion)• reuse space

(in all programming languages)

from Programming Language Pragmatics, 4th ed, by Michael Scott

Page 14: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Stack Contents

• Contents of a stack frame• arguments and returns• local variables• temporaries• bookkeeping (saved registers, line number static link, etc.)

• Local variables and arguments are assigned fixed OFFSETS from the stack pointer or frame pointer at compile time

from Programming Language Pragmatics, 4th ed, by Michael Scott

Page 15: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Binding an Array Size

•Binding when program is written

• in myprog.c

int rabbit[123];

Page 16: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Binding an Array Size

•Binding based on a configuration file

• in config.h

#define SIZE 123

• in myprog.c

#include <config.h>

int rabbit[SIZE];

Page 17: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Binding an Array Size

•Binding at execution time

• in myprog.c

int arraysize; // size of the array

cin >> arraysize; // read size

int *rabbit; // pointer to array

rabbit = new int[arraysize];

Page 18: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Binding an Array Size

•Dynamic size of array-like data structure

• in myprog.java

java.util.ArrayList<int> rabbit = new java.util.ArrayList<>();

Page 19: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Variable Scope

• The “Scope” of a variable refers to where you can use a variable

•Where you declare a variable determines where it can be used

• The scope of a binding is the part of the program in which the binding is active

Page 20: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Variable Range

•Variables defined in a block can only be used in that block following the variable declaration

{// you cannot use the variable moth heredouble moth = 72.5;

// you may use the variable moth here// this is the scope of moth

}// The variable moth is not allowed here

Page 21: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Out of Scope

int cat;

if (whatever == 0) {

int dog = 1;

} else {

int dog = 2;

}

cat = dog * 3; // Error, dog is out of scope

Page 22: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

This works

int cat, dog;

if (whatever == 0) {

dog = 1;

} else {

dog = 2;

}

cat = dog * 3; // Only one dog variable

Page 23: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Method Parameter Scope

• The parameters defined in a method header can be used throughout that method

•Method parameter variables cannot be used in another method

void aMethod( int dog, double cat ) {

You can use dog and cat throughout this method

}

Page 24: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Declarations First• Java and C++ allow you to put variable declarations

anywhere in a program

• It is usually advantageous to declare a variable at the beginning of a block

• Some older programming languages required you to put all variable declarations at the beginning of a block• Fortran – All declarations are before executable

statements• Cobol – declarations are in the “data division”

Page 25: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

for Loop Scope

•A loop counter variable only has scope in the loop

for (int cow = 0; cow < 28; cow++) {

// loop body

// the loop counter, cow, can be used here

}

// You cannot use cow after the loop

Page 26: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Local Variablespublic static void main( ) {

double cat = 5, bird = 47; Scope of

cat = myfunc( bird ); cat and bird

System.out.print(cat);

}

double myfunc(double cow) {

double bull; Scope of

bull = cow * 2.0; cow and bull

return bull;

}

Page 27: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Multiple Variable with the Same Name

• In different parts of a program, you can use the same variable name, but it will mean a different variable

• This can be confusing and should be avoided

Page 28: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Local Variables (Tricky)public static void main( ) {

double cat = 5, bird = 47; Scope of

cat = myfunc( bird ); cat and bird

System.out.print(cat);

}

int myfunc(double cow) {

int bird; Scope of

bird = (int)cow * 2.0; cow and bird

return bird; (different bird)

}

Page 29: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Variables

•A C++ or Java method can use three different types of variables• local variables defined in the method• parameter variables• object instance variables

•Object instance variables can be used in any of the methods of the class

Page 30: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Name Collisionpublic class Collide {

int rat = 3;

public int square( int rat ) {

return rat * rat;

}

}

Collide thing = new Collide();

int turtle = thing.square( 2 );

• turtle is set to 4

Page 31: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Variable Priority

•A method cannot have a local variable the same name as a parameter variable

• If a class instance variable has the same name as a local variable or parameter, the method will always use the local variable or parameter

Page 32: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

More Name Collisionspublic class Collide {

int rat = 3;

public int square( int mouse) {

int rat = mouse * mouse;

return rat;

}

}

• The class instance variable rat is a different memory location from the local variable rat

Page 33: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Name Collisionpublic class Collide {

int rat = 3;

public int square( int rat ) {

return this.rat * rat;

}

}

Collide thing = new Collide();

int turtle = thing.square( 2 );

• turtle gets the value 6

Page 34: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Separate Compilation

• Java, C++ and many languages allow modules to be compiled separately

•Rules for how variables work with separate compilation are messy

• In C++, static on a function or variable outside a function or private in Java means it is usable only in the current source file

• This static is a different notion from the staticvariables inside a function

Page 35: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

What is printed by this program?

A. 4 4

B. 4 5

C. 5 5

D. none of above

public class Click {static int crow= 5, raven= 7;static void myfunc(int parrot) {

int crow;crow = parrot + 1;System.out.print( crow );

}public static void main(…) {

Click ques = new Click();ques.myfunc( 3 );System.out.print( crow );

} }

Page 36: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

What is printed by this program?

A. 4 4

B. 4 5

C. 5 5

D. none of above

public class Click {static int crow= 5, raven= 7;static void myfunc(int parrot) {

// changed here

crow = parrot + 1;System.out.print( crow );

}public static void main(…) {

Click ques = new Click();ques.myfunc( 3 );System.out.print(crow );

} }

Page 37: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

Created Elsewhere

•extern on a C++ variable or function means that it is declared in another source file

• In Java and C++, method headers without bodies are extern by default

Page 38: Binding - NCATwilliams.comp.ncat.edu/comp360/Binding.pdf · location •In Java, ... OFFSETS from the stack pointer or frame pointer at ... •Cobol –declarations are in the “data

No class next week

Spring break is

March 5 to March 11, 2017