Professional Documents
Culture Documents
Chapter 2
Chapter 2
Chapter 2
1
Topics Covered
Introduction: Design Goals
Programming environments
Virtual Computers
Binding Times
Language Paradigms
2
Introduction: Design Goals
• There are several design goals considered whenever a new language i
s to be designed.
•Utility - Is a feature often useful? Can it do important things th
at cannot be done using other features of the language?
•Convenience - Does this feature help avoid excessive writing?
Does this feature add or eliminate clutter (code untidiness) in the
code?
•Efficiency - Is it easy or difficult to translate this feature? Is it p
ossible to translate this feature into efficient code? Will its use imp
rove or degrade the performance of programs?
•Portability - Will this feature be implementable on any platfor
m?
•Readability - Does this form of this feature make a program m
ore readable? Will a programmer other than the designer underst
and the intent easily?
3
• There are several design goals considered whenever a new lan
guage is to be designed.
• Modeling ability - Does this feature help make the meani
ng of a program clear? Will this feature help the programm
er model a problem more fully, more precisely, or more easi
ly?
• Simplicity - Is the language design as a whole simple, unifi
ed, and general, or is it full of dozens of special-purpose fea
tures?
• Semantic clarity - Does every legal program and expressio
n have one defined, unambiguous meaning? Is the meaning
constant during the course of program execution?
4
Programming Environment
• Is a collection of tools used in software
development
• Can be separate file system, text editor, linker, compiler
• UNIX is an older programming environment and
provides a variety of powerful tools where all of its tools
were accessed separately.
• Can also be a large collection of integrated tools, each
accessed through a uniform graphical user interface
(Visual Studio, NetBeans)
5
Source Source Source
code code code
Object files
Linker
Loader
Memory
6
Virtual Computers
• A virtual machine (VM) is a software implementation of a
machine (i.e. a computer) that executes programs like a ph
ysical machine
• A virtual machine is less efficient than a real machine beca
use it accesses the hardware indirectly
• Virtual machines are separated into two major categories,
based on their use and degree of correspondence to any r
eal machine
• System virtual machine
– Multiple OS environments can co-exist on the same
computer, in strong isolation from each other
– Example: VMware workstation
7
Virtual Computers
• Process virtual machine
• A process VM, sometimes called an application virtual machine
– runs as a normal application inside a host OS and suppo
rts a single process.
– Example: Java Virtual Machine
• A process VM is created when that process is started and destr
oyed when it exits
• Its purpose is to provide a platform-independent programming
environment
– abstracts away details of the underlying hardware or op
erating system, and allows a program to execute in the s
ame way on any platform.
8
Binding Times
• A binding is an association between two things, s
uch as a name and the thing it names.
• Binding time is the time at which a binding is crea
ted.
• Basically, binding times are of two types, Static an
d Dynamic (Early and Late)
• Static and Dynamic are very general terms to talk
about binding times
• In general, early binding is associated with greate
r efficiency and late binding is associated with gre
ater flexibility
9
Binding Times - Terminology
• Tradeoff: In general,
10
Binding Times
• Language design time
•the control flow constructs - language semantics
•the set of fundamental (primitive) types
•the available constructors for creating complex types
•Example: the type referred to by the name int.
• Language implementation
• the precision (number of bits) of the fundamental types,
• the coupling of I/O to the operating system’s notion of files
• the organization and maximum sizes of stack and heap
• the handling of run-time exceptions such as arithmetic ove
rflow.
11
Binding Times
• Compile time
• Compilers choose the mapping of high-level constructs to mac
hine code, including the layout of statically defined data in me
mory.
• Link time
• Most compilers support separate compilation (compiling differ
ent modules of a program at different times)
• The linker chooses the overall layout of the modules with respe
ct to one another.
• Inter-module references resolved here
• When a name in one module refers to an object in another mo
dule, the binding between the two is performed by the linker.
12
Binding Times
• Load time
• Load time refers to the point at which the operating system loads th
e program into memory.
• A variable may be bound to a storage cell when the program is load
ed into memory
• Translate virtual memory addresses into physical memory addresse
s.
• Run time
• Most modern operating systems distinguish between virtual and ph
ysical addresses.
• Virtual addresses are chosen at link time; physical addresses can act
ually change at run time.
• The processor’s memory management hardware translates virtual a
ddresses into physical addresses during each individual instruction a
t run time.
13
Binding Times
Consider the following Java assignment statement:
count = count + 5;
• Some of the bindings and their binding times for the parts
of this assignment statement are as follows:
– The type of count is bound at compile time.
– The set of possible values of count is bound at compiler desig
n time (Implementation time)
– The meaning of the operator symbol + is bound at compile ti
me, when the types of its operands have been determined.
– The internal representation of the literal 5 is bound at compile
r design time.
– The value of count is bound at execution time with this statem
ent.
14
15
Binding Times
• Meaning of +
• was defined (as numerical addition) when the language was
defined
• The specific addition operation (float, double, int, etc.),
however, cannot be determined until program translation
time.
• Literal 23
• The meaning of literal 23 as the int value representing the
number 23 is fixed at language definition time.
• However, the actual representation (sequence of bits) for the
int value 23 can vary on different platforms, and it is not
bound until the language implementation (compiler design) is
performed.
16
Binding Times
17
Binding: Type Binding
Before a variable can be referenced in a program, it must be bound to
a data type
The two important aspects of this binding are how the type is specified
and when the binding takes place
Static type binding
Declaration is a static type binding
Explicit declaration
listing variable names and specifies that they are a
particular type
Example: int x,y; float radius;
Implicit declaration
Means of associating variables with types through default
conventions
done by the language processor, either a compiler or an
interpreter
18
Binding: Type Binding
Implicit declaration
Several different bases for implicit variable type bindings.
1. Naming Conventions: FORTRANIf the identifier begins with one
of the letters I, J, K, L, M, or N, or their lowercase versions, it is
implicitly declared to be Integer type; otherwise, it is implicitly
declared to be Real type.
Big damage to reliability because they prevent the
compilation process from detecting some typographical and
programmer errors
2. Type inference (Context)
type of the value assigned to the variable in a declaration
statement will be the type of the variable.
Example: in C# a var declaration of a variable must include
an initial value, whose type is made the type of the variable
24
Storage Bindings and Lifetime
Stack-Dynamic variables
Advantage: recursive subprograms require some form of
dynamic local storage so that each active copy of the recursive
subprogram has its own version of the local variables
Stack trace
25
Storage Bindings and Lifetime
Stack-Dynamic variables
Advantage: Subprograms share the same memory space for
their locals (Compare with Static variables)
Disadvantage:
Run-time allocation/de-allocation overhead
Slower accesses because indirect addressing is required
Subprograms cannot be history sensitive (no static
variables)
26
Storage Bindings and Lifetime
Explicit Heap-Dynamic Variables
Memory cells that are allocated and deallocated by explicit
run-time instructions written by the programmer.
Allocated from and deallocated to the heap
Can only be referenced through pointer or reference
variables
Created by either an
operator (as in C++)
The new keyword
call to a system subprogram provided for that purpose (as
in C).
The malloc subprogram
Type binding is static (Compile time)
Used to create dynamic structures, such as linked lists and
trees, that need to grow and/or shrink during execution
27
Storage Bindings and Lifetime
Explicit Heap-Dynamic Variables
Deallocating or destroying Explicit Heap-Dynamic Variables
Explicit: by using operators (Example C++)
Non-local
local
30
Scope
Static scope
the scope of a variable can be statically determined
—that is, prior to execution.
There are two categories of static-scoped languages
those in which subprograms can be nested, which
creates nested static scopes
those in which subprograms cannot be nested
static scopes are also created by subprograms but
nested scopes are created only by nested class
definitions and blocks
Information: Ada, JavaScript, Common LISP, Scheme,
Fortran 2003+, F#, and Python allow nested
subprograms, but the C-based languages do not.
31
Scope
Static scope
Consider languages that allow nested subprograms
big
sub1
var x = 7;
sub2();
sub2
var y = x;
var x = 3;
sub1();
33
Scope
Blocks
Many languages such as Java and C++ allow new static scopes to
be defined in the midst of executable code.
Allows a section of code to have its own local variables whose
scope is minimized
Such variables are typically stack dynamic, so their storage is
allocated when the section is entered and deallocated when the
section is exited
The C-based languages allow any compound statement to have
declarations and thereby define a new scope.
Such compound statements are called blocks. Consider the following
example and assume list was defined to be an array of integers:
35
Scope: Dynamic scope
Consider the two different call sequences
for sub2 in the example.
1. big calls sub1, which calls sub2. In this
case, the search proceeds from the
local procedure, sub2, to its caller,
sub1, where a declaration for x is
found.
So, the reference to x in sub2 in
this case is to the x declared in
sub1.
2. sub2 is called directly from big. In this
case, the dynamic parent of sub2 is
big, and the reference is to the x
declared in big.
Note that if static scoping were used, in either calling sequence
discussed, the reference to x in sub2 would be to big’s x.
36
Scope and Lifetime
Sometimes related
Consider a variable a declared within a method which have
no method calls.
In this case the lifetime and the scope of the variable
is the same.
Scope:- Spatial
Lifetime:- Temporal
37
Scope and Lifetime
Sometimes not related
Consider a variable declared in a function using the specifier
static in C++
Statically bound to the scope of that function
Statically bound to storage
So, its scope is static and local to the function
But its lifetime extends over the entire execution of
the program of which it is a part
Also unrelated when subprogram calls are involved
38
Scope and Lifetime
Sometimes not related
The scope of the variable sum is completely contained within
the compute function.
It does not extend to the body of the function printheader,
although printheader executes in the midst of the execution of
compute.
However, the lifetime of sum extends over the time during
which printheader executes.
Whatever storage location sum is bound to before the call to
printheader, that binding will continue during and after the
execution of printheader.
compute printheader
…
Variable sum is not visible
int sum;
… … in printheader function
Printheader()
39 ….
Referencing Environment
The referencing environment of a statement is the collection of
all variables that are visible in the statement
40
Named constants
are variables that are bound to values only once
are useful as aids to readability and program reliability.
Readability can be improved, for example, by using the name pi instead
of the constant 3.14159265.
Another important use of named constants is to parameterize a
program. Compare the following codes.
41
Named constants
Binding to values can also be dynamic:
Imperative programming G1
Object-oriented programming G2
Concurrent programming G3
Functional programming G4
Logic programming G5
Generic Programming G6
Scripting languages G7
43