Download - Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Transcript
Page 1: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Bindings

Page 2: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Typical stages of program execution

• First, the program is compiled• Then everything is loaded into memory• Then it is linked to any library routines it uses

– Linking and loading are often combined into a single step

• Finally, the program executes

Page 3: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Binding times

• A binding is an association of two things• Typically, a name is bound to something• Bindings can occur at different types. Examples:

– Language design--keywords to language constructs

– Compilation--Variable names to types

– Linking--Names to library units

– Loading--Code and data to storage locations

– Execution--Variable names to values

• These are language-dependent examples!

Page 4: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

FORTRAN bindings

• FORTRAN distinguishes declarations from statements• Declarations are “non-executable” • Declarations bind names to types (at “compile time”)

and to storage locations (at “load time”)• Declarations may bind initial values to variables• Statements are executable (done at “run time”)• Statements may also bind values to variables

Page 5: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Why binding time matters

• This function works correctly only the first time• Some variables in C are initialized the same way

FUNCTION SUM (A, N) DIMENSION A(N) -- specify A as an array of size N INTEGER TOTAL/0/ -- set TOTAL = 0 (at compile time) DO 50 I=1, N -- do statement 50 N times50 TOTAL=TOTAL+A(I) -- add A[i] to TOTAL RETURN TOTAL -- return TOTAL as the result END

Page 6: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Storage allocation in FORTRAN

• The FORTRAN compiler binds all variables to storage locations relative to the start of the program unit

• The FORTRAN loader binds the relative locations to absolute locations

• This technique cannot support dynamic storage allocation

• Without dynamic allocation, recursion is impossible• FORTRAN versions that support recursion don't use

this storage model

Page 7: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

A FORTRAN binding problem

• FORTRAN binds floating-point literals to storage locations

• The compiler puts the value into the storage location• The language does not adequately protect the storage

location• It’s possible to bind a new value to the storage location• Therefore, constants aren’t necessarily constant!

Page 8: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Storage allocation in Algol 60

• An area of storage is set aside for “the stack”• The Algol compiler binds variables to locations

relative to “the top of the stack”• The “top of the stack” is bound during execution• This technique is ideally suited for supporting

recursion• Storage allocated on the stack must be discarded

in reverse order of acquisition

Page 9: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Bindings in Java

• Method variables are bound much like in Algol 60• Java does not initialize method variables, but

checks to make sure that you do– It is impossible to make this check perfectly

• Class and instance variables are initialized during object construction– Default is to initialize to zero (null)

• Messages are dynamically bound to methods

Page 10: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Overloading

• O-O languages permit method overloading– Methods with the same name but different signatures

• Ada and Prolog also permit operator overloading• In most languages, operators (+, -, *, /, &, etc.) are bound

at language design time• Ada permits operator overloading during compilation• Prolog permits operator overloading during execution• Overloaded operators are hard to compile, easy to misuse

Page 11: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

Summary

• Bindings can happen at various times• Earlier bindings are more efficient; later bindings

are more flexible• Sometimes the same kind of binding can occur at

different times• Mysterious errors can result if bindings don’t

occur when you expect them to

Page 12: Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.

The End