1 / 68

High Level Languages

High Level Languages. Provide features that are independent of any particular computer architecture Machine independence Ease of programming Core issues in language design Control flow constructs Types Subroutines Classes Names. Identifiers.

dickeyr
Télécharger la présentation

High Level Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. High Level Languages • Provide features that are independent of any particular computer architecture • Machine independence • Ease of programming • Core issues in language design • Control flow constructs • Types • Subroutines • Classes • Names Names, Scopes and Bindings

  2. Identifiers • A fundamental abstraction is the use of names or identifiers, to denote language entities or concepts • Names can denote anything, including procedures, types, constants and variables • A declaration of a name introduces a new sense in which the name may be used Names, Scopes and Bindings

  3. Identifiers • Normally we think only of associating types with identifiers • In addition to types, other attributes may be associated with an identifier • Values, storable quantities • Locations, places where the values can be stored • … Names, Scopes and Bindings

  4. Names • Ultimately things in a program have to be implemented in some way • Variables are stored in memory • Subroutines/functions/methods are stored in memory • Types and built in functions • A name is a mnemonic character string used to represent something else • Names allow us to refer to variables, constants, operations, types, functions using symbolic identifiers rather than using addresses Names, Scopes and Bindings

  5. What is a Name? • The term identifier is often used interchangeably with name • Language design issues • What is the maximum length of a name? • What characters can be used in a name? • Are names case sensitive? • Are the special words reserved words or keywords? Names, Scopes and Bindings

  6. Keywords • A keyword is special only in certain contexts REAL APPLE REAL = 3.4 • A reserved word is a special word of the programming language that cannot be used as a name • Predefined names have predefined meanings but can be redefined by the user Names, Scopes and Bindings

  7. Variables • An abstraction of a memory location(s) • One of the most common uses of names • A variable can have several attributes • Name • Address • Value • Type • Lifetime • Scope Names, Scopes and Bindings

  8. Binding • A binding is an association between two things • A name and the thing it names • Binding time is the time at which a binding is created • Possible binding times include • Language design • Language implementation • Program writing • Link time • Load time • Run time Static Bindings Names, Scopes and Bindings

  9. Early versus Late Binding • The binding of names before runtime is called static or early binding • More efficient • The binding of names at runtime is called dynamic or late binding • More flexible • Consider polymorphism in a program Names, Scopes and Bindings

  10. Stack.java public interface Stack { public void push( int data ); public int pop(); public boolean isEmpty(); public boolean isFull(); } Names, Scopes and Bindings

  11. ArrayStack.java public class ArrayStack implements Stack { private int[] theStack; private int tos; public ArrayStack( int cap ) { theStack=new int[ cap ]; tos=-1; } public void push( int value ) throws StackException { if ( isFull() ) throw new StackException(); tos++; theStack[ tos ] = value; } public int pop() throws StackException { if ( isEmpty() ) throw new StackException(); int retVal = theStack[ tos ]; tos--; return retVal; } public boolean isEmpty() { return tos == -1; } public boolean isFull() { return tos == theStack.length - 1; } } Names, Scopes and Bindings

  12. LinkedStack.java public class LinkedStack implements Stack { private static class Node { public int data; public Node next; public Node( int d, Node n ) { data = d; next = n; } } private Node tos; public LinkedStack() { tos = null; } public void push( int value ) { tos = new Node( value, tos ); } public int pop() throws StackException { if ( isEmpty() ) throw new StackException(); int retVal = tos.data; tos = tos.next; return retVal; } public boolean isEmpty() { return tos == null; } public boolean isFull() { return false; } } Names, Scopes and Bindings

  13. TestStack.java import java.util.Random; public class TestStack { public static void runTests( Stack s ) throws StackException { for ( int i = 0; i < 10; i++ ) s.push( i ); for ( int i = 10; i > 0; i-- ) { if ( s.pop() + 1 != i ) throw new StackException(); } } public static void main( String args[] ) { Random rng = new Random(); Stack s; if ( rng.nextInt( 2 ) == 0 ) s = new ArrayStack( 10 ); else s = new LinkedStack(); try { runTests( s ); } catch ( StackException e ) { e.printStackTrace(); } } } Names, Scopes and Bindings

  14. Object Lifetime • There are several key events that take place during the life time of a name • Creation of objects • Creation of bindings • Use of bindings • Deactivation and reactivation of bindings • Destruction of bindings • Destructions of objects • The binding’s lifetime is the period of time between the creation and destruction of a name-to-object bindings • Notice that object life time and binding life time need not coincide Names, Scopes and Bindings

  15. Different Lifetimes import java.awt.Point; import java.util.List; import java.util.ArrayList; public class Binding { public static int NUM_POINTS = 100; public static Point makePoint( int x, int y ) { Point p = new Point( x, y ); return p; } public static void main( String args[] ) { List<Point> myPoints = new ArrayList<Point>(); for ( int i = 0; i < NUM_POINTS; i++ ) { myPoints.add( makePoint( i, i ) ); } } } Names, Scopes and Bindings

  16. Storage Management • Object lifetimes generally correspond to one of 3 storage allocation mechanisms • Static • Objects given absolute address that stays fixed throughout the program • Stack • Objects are allocated and deallocated as needed. Typically on the system stack • Heap • Objects are allocated and deallocated as needed Names, Scopes and Bindings

  17. Static Allocation • Space for the object is allocated in the binary at compile-time • These objects have a lifetime as long as the binary which contains them exists • No allocation overhead at runtime • Objects (and the values) are persistent • Examples • Global Variables • Class Variables Names, Scopes and Bindings

  18. FORTRAN Subprogram FORTRAN Executable subroutine iswap(a, b) integer a, b integer tmp tmp = a a = b b = tmp return end tmp Every time mult() is called a is stored here Names, Scopes and Bindings

  19. Reference vs. Value • Fortran 77 uses the so-called call-by-reference paradigm • This means that instead of just passing the values of the function/subroutine arguments, the memory address of the arguments (pointers) are passed instead. • Java uses call-by-value • Here the values of the parameters are passed to the method. Changes made to the arguments in the method do not have an effect outside of the method. • You have to be careful about this when writing Fortran code, because it is easy to introduce undesired side effects. For example, sometimes it is tempting to use an input parameter in a subprogram as a local variable and change its value. • You should never do this since the new value will then propagate back to the calling program with an unexpected value! Names, Scopes and Bindings

  20. FORTRAN Subprogram subroutine iswap(a, b) integer a, b integer tmp tmp = a a = b b = tmp return end program callex integer m, n m = 1 n = 2 call iswap(m, n) write(*,*) m, n stop end tmp m n Names, Scopes and Bindings

  21. Recursion public class Factorial { public static int factorial( int n ) { if ( n == 1 ) { return 1; } else { return n * factorial( n – 1 ); } } public static void main( String args[] ) { System.out.println( factorial( 3 ) ); } } Names, Scopes and Bindings

  22. factorial(3) n = 3 return 3 * factorial(2) factorial(2) n = 2 return 2 * factorial(1) factorial(1) n = 1 return 1 factorial(3) Answer: 6 return 2 factorial(2) return 1 factorial(1) Names, Scopes and Bindings

  23. FORTRAN Subprogram FORTRAN Executable subroutine mult (a, b) integer a, b if (a .eq. 1 ) then mult = b else mult = mult( a – 1, b ) + a return end a Every time mult() is called a is stored here Names, Scopes and Bindings

  24. Stack-Based Allocation • If you think about the lifetime of a typical local variable it follows the activation of a function • Created when function called • Destroyed when function returns • Many compilers use the stack to store locals • Each instance of a function has its own frame called an activation record • More flexible but allocation overhead incurred at runtime Names, Scopes and Bindings

  25. The typical call stack is used for the return address, locals, and parameters. Storing the return address Local data storage Parameter passing Evaluation stack Other return state Call Stack Image from Wikipedia: http://en.wikipedia.org/wiki/Stack_frame Names, Scopes and Bindings

  26. Recursion Revisited Return to main public class Factorial { public static int factorial( int n ) { if ( n == 1 ) { return 1; } else { return n * factorial( n – 1 ); } } public static void main( String args[] ) { System.out.println( factorial( 3 ) ); } } n = 3 Names, Scopes and Bindings

  27. Recursion Revisited Return to main public class Factorial { public static int factorial( int n ) { if ( n == 1 ) { return 1; } else { return n * factorial( n – 1 ); } } public static void main( String args[] ) { System.out.println( factorial( 3 ) ); } } n = 3 Return n = 2 Names, Scopes and Bindings

  28. Recursion Revisited Return to main public class Factorial { public static int factorial( int n ) { if ( n == 1 ) { return 1; } else { return n * factorial( n – 1 ); } } public static void main( String args[] ) { System.out.println( factorial( 3 ) ); } } n = 3 Return n = 2 Return n = 1 Names, Scopes and Bindings

  29. Heap Based Allocation • A heap is a region of storage in which memory can be allocated and deallocated at arbitrary times • Typically where storage for dynamically allocated objects comes from • Dynamically allocated objects remain allocated until deallocated explicitly, either by the programmer or by a garbage collector Names, Scopes and Bindings

  30. Typical Memory Layout Code Static Data Stack Heap Names, Scopes and Bindings

  31. Managing the Heap • As a program runs it will allocate and deallocate memory from the heap • How should this be managed? Free Free Names, Scopes and Bindings

  32. Fixed Sized Blocks • Heap space is always allocated in fixed size blocks • Program may actually get more memory than it asks for • Internal fragmentation • Allocation/deallocation is fast with very little overhead • A list of available blocks can tell you what is free • When memory is deallocated its blocks are simply added to the free list Names, Scopes and Bindings

  33. Variable Sized Blocks • Program is given exactly the memory it asks for • No internal fragmentation • Can lead to external fragmentation • All available blocks may be too small, but taken together there is enough space • Compaction • Problem if addresses are known to program • Allocation schemes • First Fit • Best Fit Names, Scopes and Bindings

  34. Garbage Collection • In the previous strategies it was up to the programmer to indicate when memory is no longer required • Garbage collection is a technique where the system attempts to find memory that can no longer be accessed and deallocates it • Pros • Programmer does not need to worry about it • Cons • Stop computation, recover memory, restart • Interleave computation and memory recovery Names, Scopes and Bindings

  35. Reference Counting • A simple technique is to keep track of the number of references to a memory block • Maintained by the system • Remove when reference count becomes 0 • Costs • Every memory block requires a reference count field • The field must be updated, which increases the time taken by assignment statements Names, Scopes and Bindings

  36. Objects Referring to Objects Ref count = 1 Ref count = 1 Ref count = 2 Ref count = 2 Ref count = 1 Names, Scopes and Bindings

  37. Objects Referring to Objects Ref count = 1 Ref count = ? Ref count = 1 Ref count = 1 Names, Scopes and Bindings

  38. Garbage Collection • Many different algorithms • Reference counting • Mark and sweep • Stop and copy Names, Scopes and Bindings

  39. First algorithm developed to handle cyclic reference Garbage is allowed to accumulate until all available memory has been exhausted Each memory block is given a boolean marker that indicates whether or not the block has been marked. At the start of the algorithm all blocks are unmarked Consists of two phases Mark phase Sweep phase Pros Identifies garbage even in the presence of reference cycles Cons Scans active data (twice) and inactive data (once), Program is suspended while algorithm runs Mark and Sweep void mark( Object p ) { if ( !p.marked ) { for each Object q referred to by p mark( q ); } void sweep() { for each Object p in the heap if ( p.marked ) { p.marked = false; } else { heap.release( p ); } } Names, Scopes and Bindings

  40. Stop and Copy • Heap is divided into an active and inactive region • All dynamically allocated objects are placed in the active region • When memory in the active region is exhausted, all of the live objects are copied to the inactive region • Garbage items are left behind • Copied objects are stored in contiguous memory locations in the new region • Heap is automatically de-fragmented • Once copy is complete, regions reverse roles • Pros • Handles cycles • Defragments the heap • Cons • Wastes memory • Copies all of the live objects Names, Scopes and Bindings

  41. Scope • The region in a program in which a binding is active is its scope • Declarations are usually associated with a block • A new scope is created upon entry to the block • Bindings may within the block are only active in that block • Scope describes the visibility of a binding Names, Scopes and Bindings

  42. Lexical And Dynamic Scope • Under lexical scope rules, the binding of name occurrences can be done statically at compile time • Block structured languages • Under dynamic scope rules, binding of name occurrences is done dynamically at run time • Declarations are processed as they are encountered along an execution path Names, Scopes and Bindings

  43. Lexical Scope • Simplest rule is global scope • All bindings are active every where • Used in early versions of BASIC • Early versions of FORTRAN distinguished between local and global scope • Scope of local variables was the subprogram in which they were defined • Modern languages tend to use nested scope • Closest nested scope rule Names, Scopes and Bindings

  44. Nested Subroutines procedure P1( A1 : T1 ); var X : real; procedure P2( A2 : T2 ); procedure P3( A3 : T3 ); begin (* Body of P3 *) end begin (* Body of P2 *) end; procedure P4( A4 : T4 ); function F1( A5 : T5 ) : T6; var X : integer; begin (* Body of F1 *) end; begin (* Body of P4 *) end; begin (* Body of P1 *) end;. Names, Scopes and Bindings

  45. Nested Subroutines procedure P1( A1 : T1 ); var X : real; procedure P2( A2 : T2 ); procedure P3( A3 : T3 ); begin (* Body of P3 *) end begin (* Body of P2 *) end; procedure P4( A4 : T4 ); function F1( A5 : T5 ) : T6; var X : integer; begin (* Body of F1 *) end; begin (* Body of P4 *) end; begin (* Body of P1 *) end;. Names, Scopes and Bindings

  46. Nested Subroutines procedure P1( A1 : T1 ); var X : real; procedure P2( A2 : T2 ); procedure P3( A3 : T3 ); begin (* Body of P3 *) end begin (* Body of P2 *) end; procedure P4( A4 : T4 ); function F1( A5 : T5 ) : T6; var X : integer; begin (* Body of F1 *) end; begin (* Body of P4 *) end; begin (* Body of P1 *) end;. Names, Scopes and Bindings

  47. Nested Subroutines procedure P1( A1 : T1 ); var X : real; procedure P2( A2 : T2 ); procedure P3( A3 : T3 ); begin (* Body of P3 *) end begin (* Body of P2 *) end; procedure P4( A4 : T4 ); function F1( A5 : T5 ) : T6; var X : integer; begin (* Body of F1 *) end; begin (* Body of P4 *) end; begin (* Body of P1 *) end;. Hole in scope of X since nested declaration hides it Some languages provide qualifiers (scope resolution operators) to access hidden variables (this or super) Names, Scopes and Bindings

  48. Implementation procedure P1( A1 : real ); var X : real; procedure P2( A2 : real ); procedure P3( A3 : real ); begin X: = A3 * A2; end begin P3( 20 ); end; procedure P4( A4 : real ); function F1( A5 : real ) : real; var X : integer; begin X := 10; F1 := X + A5; end; begin F1( A4 ) end; begin X := 1024; P2( 10 ); P4( 5 ); end;. 1024 X Names, Scopes and Bindings

  49. Implementation procedure P1( A1 : real ); var X : real; procedure P2( A2 : real ); procedure P3( A3 : real ); begin X: = A3 * A2; end begin P3( 20 ); end; procedure P4( A4 : real ); function F1( A5 : real ) : real; var X : integer; begin X := 10; F1 := X + A5; end; begin F1( A4 ) end; begin X := 1024; P2( 10 ); P4( 5 ); end;. 10 A2 Return to P1 1024 X Names, Scopes and Bindings

  50. Implementation procedure P1( A1 : real ); var X : real; procedure P2( A2 : real ); procedure P3( A3 : real ); begin X := A3 * A2; end begin P3( 20 ); end; procedure P4( A4 : real ); function F1( A5 : real ) : real; var X : integer; begin X := 10; F1 := X + A5; end; begin F1( A4 ) end; begin X := 1024; P2( 10 ); P4( 5 ); end;. 20 A3 Return to P2 10 A2 Return to P1 1024 X Names, Scopes and Bindings

More Related