Designers of program compilers are faced with the question of when to translate a statement written in the source lan-guage into final instructions in machine language (see also assembler). This can happen at different times depending on the nature of the statement and the decision of the com-piler designer.
Many programming languages use formal data types (such as integer, floating point, double, string, and so on) that result in allocation of an exact amount of storage space to hold the data (see data types). A statement that declares a variable with such a type can be effectively bound imme-diately (that is, a final machine code statement can be gen-erated). This is also called compile-time binding.
However, there are a variety of statements for which binding must be deferred until more information becomes available. For example, it is common for programmers to use libraries of precompiled routines. A statement that calls such a routine cannot be turned immediately into machine lan-guage because the compiler doesn’t know the actual address where the routine will be embedded in the final compiled program. (That address will be determined by a program called a linker that links the object code from the source program to the library routines called upon by that code.)
Another aspect of binding arises when there is more than one object in a program with the same name. In lan-guages such as C or Pascal that use a nested block struc-ture, lexical binding can determine that a name refers to the closest declaration of that name—that is, the smallest scope that contains that name (see variable). In a few languages such as Lisp, however, the reference for a name depends on how (or for what) the function is being called, so binding can be done only at run time.
Binding and Object-Oriented Languages
The use of polymorphism in object-oriented languages such as C++ raises a similar issue. Here there can be a base class and a hierarchy of derived classes. A function in the base class can be declared to be virtual, and versions of the same function can be declared in the derived classes. In this case a statement containing a pointer to the function in the base class cannot be bound until run time, because only then will it be known which version of the virtual function is being called. However, compilers for object-oriented lan-guages can be written so they do early binding on state-ments for which it is safe (such as those involving static data types), but do dynamic binding when necessary.
From the point of view of efficiency, early binding is bet-ter because memory can be allocated efficiently. Dynamic binding provides greater flexibility, however, and facilitates debugging—for example, because the name of a variable is normally lost once it is bound and the machine code is generated.
No comments:
Post a Comment