Docsity

Programming Languages and Storage Allocation - Lecture Slides | ECS 140A, Assignments of Programming Languages

Toggle sidebar
bg1
1
Prof. Su ECS 140A Lecture 4 1
Programming Languages
ECS 140A
13:40-15:00 Tu/Th
55 Roessler
Prof. Su ECS 140A Lecture 4 2
Announcements
•HW 1 due today in class
•
HW2 due Thursday, April 23
rd
–
Start early!
Prof. Su ECS 140A Lecture 4 3
Lecture Outline
•Basic semantics of programming languages
•How to specify the meaning of
names
(or identifiers)
–What are attributes of names?
–How to associate attributes with names (
bindings
)?
•Static versus dynamic bindings
–
How long do the bindings last (
scoping
)?
•
Static versus dynamic scoping
•
Storage allocation
–
Global
–
Stack
–
Heap
pf3
pf4
pf5
pf8

Partial preview of the text

Download Programming Languages and Storage Allocation - Lecture Slides | ECS 140A and more Assignments Programming Languages in PDF only on Docsity!

Prof. Su ECS 140A Lecture 4 1

Programming Languages

ECS 140A

13:40-15:00 Tu/Th

55 Roessler

Prof. Su ECS 140A Lecture 4 2

Announcements

  •  HW 1 due today in class
  •  HW2 due Thursday, April 23rd
    •  Start early!

Prof. Su ECS 140A Lecture 4 3

Lecture Outline

  •  Basic semantics of programming languages
  •  How to specify the meaning of names (or identifiers)
    •  What are attributes of names?
    •  How to associate attributes with names ( bindings)?
      •  Static versus dynamic bindings
    •  How long do the bindings last ( scoping)?
      •  Static versus dynamic scoping
  •  Storage allocation
    •  Global
    •  Stack
    •  Heap

Prof. Su ECS 140A Lecture 4 4

How to Specify Semantics

  •  Language reference manuals
    •  Commonly used
    •  But lack of precision
  •  Reference implementations
    •  One can conduct experiments to get answers
    •  But questions about a program cannot be answered in advance
  •  Formal definitions
    •  Precise
    •  But also complex and abstract

Prof. Su ECS 140A Lecture 4 5

Names in Programming Languages

  •  The use of names is a fundamental abstraction
    •  Name variables, functions, parameters, constants, …
  •  How to form names (or identifiers)
    •  What can be used in a name?
      •  Usually some combination of letters, digits, and/or _
      •  C: (letter | _) {letter | digit | _}
      •  Java: (JavaLetter) {JavaLetter | JavaDigit}
        •  JavaLetter: A-Z, a-z, _, and $ (may also Unicode chars)
      •  Many do not allow _ in names: Pascal, Modula-2, FORTRAN
    •  Maximum length of a name
      •  FORTRAN: no more than 6
      •  C, C++, Java: no restrictions

Prof. Su ECS 140A Lecture 4 6

Names in Programming Languages (cont.)

  •  Are names case sensitive?
    •  Most are case sensitive
    •  Some are not: Ada
  •  Are there any special reserved words?
    •  Most do have reserved words (e.g., int, if, while, for, …)
    •  Recall FORTRAN no keywords

Prof. Su ECS 140A Lecture 4 10

Binding (cont.)

  •  Consider:

int count; …

count = count + 1;

  •  Example bindings (and the binding times):
    •  Possible types for count
    •  Possible meanings for +
    •  Possible values for count
    •  Representation of 1
    •  Type of count, meaning of +
    •  Value of count

Prof. Su ECS 140A Lecture 4 11

Early vs. Late Binding

  •  Early binding: before runtime
    •  Static binding
  •  Late binding: at runtime
    •  Dynamic binding
  •  Trade-offs between efficiency and flexibility
    •  Early binding usually leads to more efficient execution
    •  Late binding usually leads to more flexibility
    •  Examples:
      •  What if count is not bound to its type until runtime
      •  What happens with dynamic dispatch (C++ virtual method call)

Prof. Su ECS 140A Lecture 4 12

How Are Bindings Maintained

  •  Typically in some data structures
    •  Maintain the (name, attribute) binding tuples
    •  Either during translation ( symbol table)
    •  Or during execution ( environment)
    •  Or both
  •  Binding functions for compiled programs
    •  Symbol table: names -> static attributes
    •  Environment: names -> locations
    •  Memory: locations -> values
      •  Also known as store or state
  •  For interpreted programs
    •  Environment: names -> attributes (both locations and values)

Prof. Su ECS 140A Lecture 4 13

Declarations

  •  A principal method for establishing bindings
  •  Example 1: int x = 0
    •  Explicitly specifies data type of x and its initial value
    •  Implicitly specifies scope and location in memory
  •  Example 2: double f (int x); // C function prototype
    •  Explicitly specifies f’s type: int -> double
    •  Implicitly specifies nothing else
    •  Needs another declaration specifying code

Prof. Su ECS 140A Lecture 4 14

Scope

  •  The scope of a binding is the region of the program

over which the binding applies

  •  Example: In languages with nested blocks

int x; // binds x to int { double x; … } // binds x to double

  •  Example: In languages with subroutines
    •  Upon entry
      •  Create bindings for new local variables
      •  Hide bindings for variables that are re-declared (“scope holes”)
      •  Reference the variables
    •  Upon exit
      •  Remove bindings for local variables
      •  Re-activate bindings for variables that were hidden

Prof. Su ECS 140A Lecture 4 15

Lexical Scope

  •  Static (or lexical) scope:
    •  Scope is defined in terms of the lexical structure of programs
    •  The compiler can determine the scopes
    •  All bindings for names can be resolved by examining the program
    •  Choose the most recent, active binding made at compile time
    •  Most compiled languages employ static scope rules (C, Pascal)
  •  Classical example: the “most closely nested rule”
    •  Used in block structured languages such as Algol 60 and Pascal
    •  A name is known in the scope where it is declared and in each enclosed scope, unless it is re-declared in an enclosed scope
    •  To resolve a reference to a name, examine the local scope and statically enclosing scopes until a binding is found

Prof. Su ECS 140A Lecture 4 19

Symbol Table

  •  A mechanism to associate attributes with names
    •  Use declarations to add (name, attribute) tuples in the table
    •  Use the table to access attributes every time a name is referenced
  •  Scope stack (a typical implementation)
    •  Enter scope: create a symbol table for the scope and push it on the stack
    •  Exit scope: pop off top symbol table stack
    •  Search name: search top symbol table first, and so on until a match is found

Prof. Su ECS 140A Lecture 4 20

Symbol Table Example

int local to main

x

public static void method

f

name bindings

main publicvoid static method

public static int

args

String[] parameter of main

public class Test { public static int x = 2; public static void f() { System.out.println(x); } public static void main (String [] args) { int x = 3; f(); } }

Prof. Su ECS 140A Lecture 4 21

Storage Bindings and Lifetime

  •  Key events
    •  Creation of data objects
    •  Creation of bindings
    •  References to names that use bindings
    •  Deactivation and re-activation of bindings that may be temporarily unusable
    •  Destruction of bindings
    •  Destruction of objects
  •  Lifetime or extent of names
    •  Time between creation and destruction of a name-object binding
  •  Lifetime or extent of objects
    •  Time between creation and destruction of an object
    •  Dangling reference: a binding to an object that is no longer live

Prof. Su ECS 140A Lecture 4 22

Storage Bindings and Lifetime

Divide variables into three

kinds based on their lifetime:

  •  Static
  •  Stack
  •  Heap

Prof. Su ECS 140A Lecture 4 23

Storage Management

  •  Static allocation
    •  Code
    •  Global variables
    •  Static variables
    •  Constants (e.g., integers, strings, etc)
  •  Stack allocation
    •  Parameters
    •  Local variables
    •  Temporaries
    •  Activation records
  •  Heap allocation
    •  Dynamically allocated objects
    •  Through malloc, new, etc
    •  Aliases, garbage, garbage collection

Prof. Su ECS 140A Lecture 4 24

Next Time

  •  Types