Programming Language Basics

Static vs Dynamic Distinction

Programming language policies that allow the compiler to make the decision, are referred to as static or compile time policies, while those that postpone the decision until runtime are referred to as dynamic or run time policies.

A real word example is instruction scheduling; on most modern architectures, a hybrid model is used. Compilers statically schedule instructions at compile time, using heuristics to avoid dependencies. Modern microarchitectures typically support dynamic instruction scheduling, which allows the hardware to dynamically schedule instructions during run time. (See: Tomasulo's algorithm).

Scope of declaration

The scope of declaration of x is the region of the program in which uses of x refer to this declaration.

Static scope or Lexical scope means that it's possible to determine the scope of a declaration by looking at the program. Otherwise, the language uses dynamic scope; meaning, when the program runs, the same use of x could refer to several different declarations of x.

Important note:

Java class declarations using the static keyword do not refer to the scope; rather, it asserts that only one copy of the variable, no matter how many class objects are created.

Consider the following example:

public static int x;

This means the variable x refers to data at a particular memory location. Every class object that references x refers to the same memory location.

obj1 = myclass();

obj2 = myclass();

obj3 = myclass();

obj1.x obj2.x and obj3.x all reference the same memory location.

Environments and States

It is also important to distinguish between environments and states, and whether changes occurring during runtime affect he values of data elements or the interpretation of names for that data.

Consider the assignment in the statement: x = y + 1

This assignment changes the value denoted by the name x, which is the value in whatever location is denoted by x.

To understand this, consider the example above, where the use of the word static implies each class object will reference the same memory location for x. If the keyword static is omitted, each object will refer to a unique instance of x.

If each object has it's own location for an instance of x, the environment, which is a mapping from names to locations in the store, will change according to the scope rules. That is, when one object references x, that objects copy of x will map to a location (strore) that differs from another object. The scope refers to the region where the declaration refers to that declaration.

Given x = y + 1, each objects instance copy of x will map to a unique location in memory. The state is a mapping from the location to the actual value.

Object1 will have a copy of x that may point to 0xFAA, object2 0xFFD, object3 0xFFG.

In C programming, the l-value refers to the environment, while the r-value refers to the state.

Example

Consider the following code:

#incldue <stdio.h>

// global declaration of i
unsigned int i;

int f(void) {

    // local declaration
    unsigned int i;
    
    i++;
    printf("Inside of f()\n");
    
    return 0;
}
    

This example includes a global declaration for i, as well as a local declaration within the function f. During execution of f(), the environment changes so that name i refers to the location associate with f()'s local instance. Any use refers to that location; the increment changes the value (r-value) within the scope of the function f, only. Environment and state mappings are dynamic, with a few exceptions:

  • Static versus dynamic binding (names::locations)

    • Most binding of names to locations is dynamic; however, it is possible to assign a permanent location to certain declarations that persists for the lifetime of the program.

  • Static versus dynamic binding (locations::values)

    • Binding locations to values is generally dynamic as well, since we don't know the value until we run the program.

Last updated