Scope binding and Data types - PowerPoint PPT Presentation

About This Presentation
Title:

Scope binding and Data types

Description:

Type of count: bound at compile time. Value range of count: bound at compiler design time ... Structured type compatibility: two variables have identical structures ... – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 52
Provided by: pllabCs
Category:
Tags: binding | compile | data | scope | types

less

Transcript and Presenter's Notes

Title: Scope binding and Data types


1
Scope binding and Data types
  • Kun-Yuan Hsieh
  • kyshieh_at_pllab.cs.nthu.edu.tw
  • Programming Language Lab., NTHU

2
Names
  • Identify labels, variables, subprograms,
    parameters, etc.
  • Name length
  • Earliest PL 1
  • FORTRAN I max 6
  • CORBOL max 30
  • FORTRAN 90 ANSI C max 31
  • Java, C no limit, implementers often impose
    one
  • Case sensitive (e.g. StrLeng ! strleng)
  • C, C, and Java are
  • Problems poor readability writability

3
Names (cont)
  • Keyword
  • word that is special only in certain contexts
  • (in FORTRAN) (in FORTRAN)
  • REAL APPLE REAL INTEGER
  • REAL 3.4 INTEGER REAL
  • Reserved word
  • word that cannot be used as a name
  • (in C) for, while, if, else
  • Predefined words
  • words that have been defined by public libraries
  • (in C) printf, scan, createfile

4
Variable (var)
  • A var is an abstraction of a memory cell
  • A var can be viewed as below(Name, Address,
    Value, Type, Lifetime, Scope)
  • Name
  • Referred to as identifier (id)
  • Address
  • The memory address with which it is associated
  • May have different address at different times
    during execution (e.g. local vars in a recursive
    subprogram)
  • May have different address at different places in
    a program (e.g. count defined in sub1 and sub2

5
Variable
  • Alias
  • More than one variables are used to access the
    same memory location
  • (in C) p d (p and d are aliases)
  • Type
  • Determines the range of its values, and the set
    of ops defined
  • (in C) unsigned int count (range 0
    65535)
  • Value
  • The contents of the memory cell associated with
    the name

6
Binding
  • Binding is an association between two attributes
  • Ex
  • int count
  • count count 5
  • Type of count bound at compile time
  • Value range of count bound at compiler design
    time
  • Value of count bound at execution time
  • Definition of op bound at language design time

7
Static dynamic binding
  • Static binding it first occurs before run time
    and remains unchanged in execution
  • Dynamic binding it first occurs during run time
    or can change in execution
  • Discussed issues type storage
  • Type binding
  • How is type specified?
  • When does the binding take place?
  • Storage binding
  • When does the allocation take place?

8
Static type binding
  • Static type binding explicit implicit
    declaration
  • Explicit declaration
  • (in C) int A, B double C
  • Implicit declaration
  • (in FORTRAN) name begins with I, J, K, L, M, N
    are INTEGER, others are REAL
  • (C/C) declaration definition
  • declaration specifies types but do not cause
    allocation
  • external declaration is used for type checking
  • definition specifies types also causes allocation
  • only declares in header files but defines in C
    files

9
Dynamic type binding
  • Variable is bound to a type when assigned a value
    in an assignment statement
  • (in JavaScript) list 10.2, 3.5
  • list count
  • Advantage generic programming
  • process a list of data with any type
  • Disadvantage poor error detection
  • i x (int) and i y (double)!!!
  • Disadvantage large cost
  • storage of a variable must be of varying size

10
Storage binding
  • The lifetime of a variable is the time during
    which it is bound to a specific memory location
  • static variables
  • stack-dynamic variables
  • explicit heap-dynamic variables
  • implicit heap-dynamic variables

11
Static variables
  • Vars that bound to memory cells before execution
    begins and unbound when execution terminates
  • static storage binding
  • e.g. global variables
  • (in C) static int count // history sensitive
  • Advantages
  • code efficiency by direct addressing
  • no allocation/deallocation during runtime
  • Disadvantages
  • less flexibility cannot support recursive
  • no storage sharing

12
Stack-dynamic variables
  • Vars whose storage bindings are created when
    declarations are executed
  • dynamic storage binding
  • static type binding
  • allocated from the run-time stack
  • Advantages
  • storage sharing, support recursive
  • Disadvantages
  • overhead of allocation / deallocation

13
Example I
  • include ltstdio.hgt
  • int count
  • main( )
  • int i
  • for (i0 ilt10 i)
  • test( )
  • test( )
  • int i
  • static int count 0
  • count count 1

virtual address space
testi
maini
run-time stack ptr
static var ptr
testcount
count
14
Example I
  • include ltstdio.hgt
  • int count
  • main( )
  • int i
  • for (i0 ilt10 i)
  • test( )
  • test( )
  • int i
  • static int count 0
  • count count 1

Type-binding Storage-binding
count static static
maini static stack-dynamic
testi static stack-dynamic
testcount Static static
15
Explicit heap-dynamic variables
  • Vars that are allocated and deallocated by
    explicit statements during run-time
  • Dynamic storage binding
  • Referenced through pointers or references
  • (in C) malloc( ) and free( ) (in C) new and
    delete
  • Memory leak is a major cause of SW bugs
  • Heap a section of virtual address space reserved
    for dynamic memory allocation
  • heap fragmentation a major cause of SW perf bugs
  • Java objects are explicit heap-dynamic vars
  • implicit garbage collection (no free or delete)

16
Example II
  • include ltstdio.hgt
  • main( )
  • int i
  • test( )
  • test( )
  • int i
  • char space
  • static int count 0
  • space (char ) malloc(64)
  • // memory leak

virtual address space
heap
testspace
testi
run-time stack ptr
maini
static var ptr
testcount
17
Example II
  • include ltstdio.hgt
  • main( )
  • int i
  • test( )
  • test( )
  • int i
  • char space
  • static int count 0
  • space (char ) malloc(64)
  • free(space)

virtual address space
heap
testi
run-time stack ptr
maini
static var ptr
testcount
18
Implicit heap-dynamic variables
  • Vars that are bound to heap storage only when
    they are assigned values
  • dynamic storage binding
  • Strings and arrays in Perl and JavaScript
  • _at_names door, windows, table
  • names3 bed
  • Advantages
  • highest degree of flexibility
  • Disadvantages
  • huge run-time overhead
  • some loss of error detection by compiler

19
Type checking
  • The activity of ensuring that the operands of an
    operator are of compatible types
  • (int) A (int) B (real) C
  • int real ? okay for the operator
  • (int real) converts to (real real) ? type
    coercion
  • int real ? type error
  • Static type checking done in compile time
  • Dynamic type checking done in run time

20
Strong typing
  • Strong-typed if type errors are always detected
  • Yes (Ada, Java), No (Pascal, C, C)
  • Example union in C type error cannot be
    detected
  • typedef union float A int B Sample
  • Sample data
  • main( )
  • float R1 3.231 int N1
  • data.A R1
  • ..
  • N1 data.B // N1 1078905012

21
Type compatibility
  • Name type compatibility two variables declared
    with the same type name
  • Easy to implement but less flexibility
  • type days integer type months integer
  • days N1 months N2
  • N1 and N2 are not compatible
  • Structured type compatibility two variables have
    identical structures
  • More flexible, but harder to implement
  • N1 and N2 above are compatible

22
Scope
  • The range of statements over which it is visible
    (it can be referenced)
  • Static scope determined in compile time
  • Search based on declaration sequence
  • Dynamic scope determined in run-time
  • Search based on the call stack sequence

23
Scope example
Proc A reference environment Static scope
A ? main Dynamic scope A ? B ? main
  • proc main
  • var X1, X2
  • proc A
  • var X1, X2
  • end
  • proc B
  • var X1, X2
  • call A
  • end
  • call B
  • end

24
Static dynamic scope
Static scope (3?1) x bigs x (4) x bigs
x (2) x sub2s x (5?1) x bigs x Dynamic
scope (3?1) x bigs x (4) x bigs x (2) x
sub2s x (5?1) x sub2s x
  • Procedure big
  • var x integer
  • procedure sub1
  • begin
  • x ? (1)
  • end
  • procedure sub2
  • var x integer
  • begin
  • x ? (2)
  • sub1
  • end
  • begin
  • sub1 ? (3)
  • x ? (4)
  • sub2 ? (5)

25
Static vs. dynamic scope
  • Static scope
  • easier to read
  • reference statically resolved
  • fast execution
  • Dynamic scope
  • harder to read
  • reference dynamically resolved
  • slow execution

26
Block
  • Block a method of creating static scopes inside
    program unit
  • Global reference uses operator
  • (in C/C)
  • for ()
  • int index
  • index temp
  • .
  • temp index

27
Lifetime Scope
  • Scope and lifetime are closely related but
    different concepts
  • Example in C
  • static scope
  • count scope only in home( )
  • count lifetime ends at the return of office( )

void office( ) / end of office
/ void home( ) int count
office( ) / end of home /
28
Named constants
  • Named constant variable that is bound to a value
    only when it is bound to storage
  • value cannot be changed later
  • e.g. final in Java, const in C or C
  • readability modifiability
  • different from define (no storage allocated)
  • Ex
  • (in C)
  • void students( )
  • const int count 100
  • for (i 0 i lt count i) total
    scoresi

29
Data types
30
Primitive data types
  • Those are not defined in terms of other data
    types
  • Numeric types
  • Integer
  • Floating-point
  • Decimal
  • Boolean types
  • Character types

31
Numeric Types
  • Integer
  • Several sizes
  • Byte, short, int, long
  • Range limited
  • Floating point
  • Approximation to model real numbers
  • IEEE float
  • IEEE double

32
Numeric Types
  • Decimal
  • For business applications
  • Store a fixed number of decimal digits (coded)
  • Advantage accuracy
  • Disadvantages limited range, wastes memory
  • e.g. 924.1 in an 6-byte data w/ a 4-byte integer
    part
  • 00 00 09 24 10 00

33
Boolean character types
  • Boolean
  • Could be implemented as bits, but often as bytes
  • Advantage readability
  • Character
  • ASCII one byte per char (0 127)
  • Unicode 2 bytes per char (multi-language
    support)

34
Character String Types
  • Values are sequences of characters
  • C and C
  • Not primitive
  • Use char arrays and a library of functions that
    provide operations

35
User-defined ordinal types
  • Ordinal type the range of possible values can be
    easily associated with positive integers
  • enumeration subrange
  • Enumeration type
  • (C)enum colors red, blue, yellowcolors
    mycolor blue
  • C and C are implicitly converted to
    integermycolor assigns green to mycolor
  • Readability code is more meaningful
  • Reliability compiler can check operations and
    ranges

36
Subrange types
  • A contiguous subsequence of an ordinal type
  • (Pascal) type uppercase A .. Z
  • index 1..100
  • Subrange types are different from derived types
  • (Ada) type Cash is new INTEGER range 1..100
  • subtype Score is INTEGER range 1..100
  • type Cash (derived) is not compatible with any
    INTEGER type
  • type Score (subrange) is compatible with any
    INTEGER type

37
Array types
  • An aggregate of homogeneous data elements
  • Indexing mapping to an element
  • Subscript range checking
  • C, C, FORTRAN no range checking
  • Pascal, Ada, and Java yes
  • Subscript type
  • FORTRAN, C, Java integer only
  • Pascal any ordinal type (int, char, boolean,
    enum)
  • of subscripts
  • FORTRAN I 3 (max)
  • FORTRAN 77 7 (max)
  • Others no limit

38
Array categories (4)
  • Static array
  • storage statically bound
  • subscript range statically bound
  • e.g. global arrays in C/C
  • Fixed stack-dynamic array
  • storage dynamically bound
  • subscript range statically bound
  • e.g. local arrays in C/C

include ltstdio.hgt int data10 main( )

include ltstdio.hgt int data10 main( )
int bounds20
39
Array categories (4) (cont)
  • Stack-dynamic array
  • storage dynamic bound
  • subscript range dynamic
  • only bound once
  • (Ada) GET(LEN)
  • declare
  • LIST array (1..LEN) of integer
  • begin
  • end
  • Heap-dynamic array
  • both dynamic bound
  • bound multiple times
  • e.g. (FORTRAN 90)
  • INTEGER, ALLOCTABLE, ARRAY (,) MAT
  • (MAT is 2-dim array)
  • ALLOCATE(MAT(10, 5))
  • (MAT has 10 rows, 5 cols)
  • DEALLOCATE MAT
  • (deallocate MATs storage)

40
Array categories (4) (cont)
  • Heap-dynamic array in C/C
  • sample( )
  • int bounds, A
  • bound (int ) malloc(40)
  • A bound3
  • free(bound)
  • bound (int ) malloc(80)
  • A bound10
  • free(bound)
  • Heap-dynamic array in Perl/JavaScript
  • _at_home couch, chair, table, stove
  • // home0 couch
  • // home1 chair
  • // home2 table
  • // home3 stove
  • home4 bed

41
Array implementation
  • One-dim array address calculation
  • addr(aj) addr(a0) j elementSize
  • Ex
  • int bounds10 // one int 4 bytes
  • bounds0 1200 ?? bounds4
  • Row-major allocation
  • addr(ai,j) addr(a0,0) ((i n) j)
    elementSize
  • exa0, 3 4 7
  • a1, 6 2 5 ? 3, 4, 7, 6, 2, 5, 1,
    3, 8
  • a2, 1 3 8
  • a0,0 1200 ?? a1,2

1216
1220
42
Column-major allocation
  • Column-major allocation
  • addr(ai, j) addr(a0,0) ((j n) i)
    elementSize
  • Ex
  • a0, 3 4 7
  • a1, 6 2 5 ? 3, 6, 1, 4, 2, 3, 7,
    5, 8
  • a2, 1 3 8
  • a0,0 1200 ?? a1,2

1228
43
Associative arrays
  • an unordered collection of data elements that are
    indexed by an equal of keys
  • example in Perl
  • salaries John gt 5000, Tom gt 3500,
    Mary gt 6000
  • salariesPerry 6500 add an entry into
    this array
  • tax salariesJohn tax is assigned
    with 5000
  • delete salariesTom remove the entry with
    key Tom
  • _at_salaries () remove all entries
    in this array

44
Record types
  • A heterogeneous aggregate of data elements
    identified by names
  • Ex
  • (Ada) EMPLOYEE_RECORD
  • record
  • EMPLOYEE_NAME
  • record
  • FIRST STRING (1..20)
  • LAST STRING (1..20)
  • end record
  • SALARY FLOAT
  • end record

45
Record types (cont)
  • C/C struct
  • Field reference
  • COBOL field OF Name1 OF OF NameN
  • Others Name1.Name2. .NameN.field
  • Pascal and Module-2 provide with clause
  • with employee do
  • begin
  • name Bob
  • age 42
  • end
  • Implementation field are stored in adjacent
    memory, and accessed through offset address

46
Array vs. Record
data 1200
size 4
lower 0
upper 0
  • Array same type of data
  • int data100
  • A data35
  • data35

1340
  • Record diff type of data
  • struct char name32
  • int age
  • float salary dd
  • A dd.age
  • dd.age

dd 1600
name 0
age 32
salary 36
1632
47
Union types
  • Whose variables are allowed to store diff type
    values at diff time
  • FORTRAN EQUIVALENCE
  • C and C union (free union free from type
    checking)

typedef union float A short B
Sample Sample data main( ) float R1
3.231 short N1 data.A R1 N1
data.B // N1 error number
B
data
A
48
Pointer type
  • Usage indirect addressing dynamic storage
    management
  • Operator (C, C), access (Ada), (Pascal)
  • Problems
  • Dangling pointer points to a heap-dynamic var
    that has been deallocated
  • Memory leakage (aka lost heap-dynamic var) an
    allocated var that is no longer accessible
  • These 2 problems cause a large part of bugs in
    software

int p1, p2 p1 (int) malloc(sizeof(int)) p2
p1 delete p1 value p2
int p1, p2 p1 (int) malloc(sizeof(int)) p2
(int) malloc(sizeof(int)) p1 (int)
malloc(sizeof(int)) delete p1
49
Solution to pointer problems
  • Tombstone approach
  • Each dynamic storage has a special cell, called
    tombstone, that points to this storage
  • Pointer(s) point to the tombstone
  • Deallocate set tombstone to NULL
  • Disads cost in time space as tombstone is
    never deallocated and reused

Heap
Heap
dynamic variable
0
tombstone
dangling pointers
50
Solution to pointer problems
  • Locks-and-keys approach
  • Pointer value represented by (key, address)
  • Dynamic storage represented by (lock, storage)
  • Each access will first check if (key lock),
    unmatched pair causes run-time error.
  • Deallocation lock ? invalid value

51
Heap management for reclaiming garbage
  • Reference counters
  • maintaining a counter in every cell
  • reclaim if (counter 0)
  • Space required is significant
  • Execution time overhead for mantaining the
    counter
  • Garbage collection
  • Run-time system allocated storage
  • Garbage collection if no available cells
  • 3 phases
  • Invalidate all cell
  • Trace pointer to mark valid ones
  • Sweep the invalid to available list
Write a Comment
User Comments (0)
About PowerShow.com