Presentation is loading. Please wait.

Presentation is loading. Please wait.

Languages and Compilers (SProg og Oversættere)

Similar presentations


Presentation on theme: "Languages and Compilers (SProg og Oversættere)"— Presentation transcript:

1 Languages and Compilers (SProg og Oversættere)
Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to John Mitchell who’s slides this lecture is based on.

2 Varieties of OO languages
class-based languages behavior of object determined by its class object-based objects defined directly multi-methods operation depends on all operands

3 History Simula 1960’s Smalltalk 1970’s C++ 1980’s Java 1990’s
Object concept used in simulation Smalltalk ’s Object-oriented design, systems C ’s Adapted Simula ideas to C Java ’s Distributed programming, internet

4 Objects An object consists of Object-oriented program: hidden data
instance variables, also called member data hidden functions also possible public operations methods or member functions can also have public variables in some languages Object-oriented program: Send messages to objects hidden data method1 msg1 . . . methodn msgn

5 What’s interesting about this?
Universal encapsulation construct Data structure File system Database Window Integer Metaphor usefully ambiguous sequential or concurrent computation distributed, sync. or async. communication

6 Object-oriented programming
Programming methodology organize concepts into objects and classes build extensible systems Language concepts encapsulate data and functions into objects subtyping allows extensions of data types inheritance allows reuse of implementation dynamic lookup

7 Dynamic Lookup In object-oriented programming,
object  message (arguments) object.method(arguments) code depends on object and message In conventional programming, operation (operands) meaning of operation is always the same

8 Example Add two numbers x  add (y)
different add if x is integer, complex Conventional programming add (x, y) function add has fixed meaning Important distinction: Overloading is resolved at compile time, Dynamic lookup at run time.

9 Encapsulation Builder of a concept has detailed view
User of a concept has “abstract” view Encapsulation is the mechanism for separating these two views message Object

10 Comparison Traditional approach to encapsulation is through abstract data types Advantage Separate interface from implementation Disadvantage Not extensible in the way that OOP is We will look at ADT’s example to see what problem is

11 Abstract data types with abstype q mk_Queue : unit -> q
is_empty : q -> bool insert : q * elem -> q remove : q -> elem is … in program end

12 Priority Q, similar to Queue
abstype pq with mk_Queue : unit -> pq is_empty : pq -> bool insert : pq * elem -> pq remove : pq -> elem is … in program end But cannot intermix pq’s and q’s

13 Abstract Data Types Guarantee invariants of data structure
only functions of the data type have access to the internal representation of data Limited “reuse” Cannot apply queue code to pqueue, except by explicit parameterization, even though signatures identical Cannot form list of points, colored points Data abstraction is important part of OOP, innovation is that it occurs in an extensible form

14 Subtyping and Inheritance
Interface The external view of an object Subtyping Relation between interfaces Implementation The internal representation of an object Inheritance Relation between implementations

15 Object Interfaces Interface Example: point
The messages understood by an object Example: point x-coord : returns x-coordinate of a point y-coord : returns y-coordinate of a point move : method for changing location The interface of an object is its type.

16 Subtyping If interface A contains all of interface B, then A objects can also be used B objects. Point x-coord y-coord move Colored_point x-coord y-coord color move change_color Colored_point interface contains Point Colored_point is a subtype of Point

17 Inheritance Implementation mechanism
New objects may be defined by reusing implementations of other objects

18 Example Subtyping Inheritance class Point class Colored_point private
float x, y public point move (float dx, float dy); class Colored_point float x, y; color c point move(float dx, float dy); point change_color(color newc); Subtyping Colored points can be used in place of points Property used by client program Inheritance Colored points can be implemented by resuing point implementation Propetry used by implementor of classes

19 OO Program Structure Group data and functions Class Subtyping
Defines behavior of all objects that are instances of the class Subtyping Place similar data in related classes Inheritance Avoid reimplementing functions that are already defined

20 Example: Geometry Library
Define general concept shape Implement two shapes: circle, rectangle Functions on implemented shapes center, move, rotate, print Anticipate additions to library

21 Shapes Interface of every shape must include
center, move, rotate, print Different kinds of shapes are implemented differently Square: four points, representing corners Circle: center point and radius

22 Subtype hierarchy General interface defined in the shape class
Circle Rectangle General interface defined in the shape class Implementations defined in circle, rectangle Extend hierarchy with additional shapes

23 Code placed in classes Dynamic lookup Conventional organization center
move rotate print Circle c_center c_move c_rotate c_print Rectangle r_center r_move r_rotate r_print Dynamic lookup circle  move(x,y) calls function c_move Conventional organization Place c_move, r_move in move function

24 Example use: Processing Loop
Remove shape from work queue Perform action Control loop does not know the type of each shape

25 Subtyping differs from inheritance
Collection Indexed Set Array Dictionary Sorted Set String Subtyping Inheritance

26 Representation of objects
access link real x 1.0 real y 2.5 proc equals proc distance code for equals code for distance Object is represented by activation record with access link to find global variables according to static scoping

27 Run-time representation of point
to superclass Object Point class Template Point object x class y x 3 y 2 Method dictionary code newX:Y: ... Detail: class method shown in dictionary, but lookup procedure distinguishes class and instance methods ... move code

28 Access Control In many OOPLs it is possible to declare attributes (and methods) private or public or protected etc. This has no effect on the running program, but simply means that the compiler will reject programs which violate the access-rules specified The control is done as part of static semantic analysis

29 Dynamic (late) Binding
Consider the method call: x.f(a,b) where is f defined? in the class (type)of x? Or in a predecessor? If multiple inheritance is supported then the entire predecessor graph must be searched: This costs a large overhead in dynamic typed languages like Smalltalk (normally these languages don’t support multiple inheritance) In static typed languages like Java, Eiffel, C++ the compiler is able to analyse the class-hierarchy (or more precise: the graph) for x and create a display-array containing addresses for all methods of an object (including inherited methods) According to Meyer the overhead of this compared to static binding is at most 30%, and overhead decreases with complexity of the method

30 fig14.2

31 fig14.3

32 Multiple Inheritance In the case of simple inheritance, each class may have one direct predecessor; multiple inheritance allows a class to have several direct predecessors. In this case the simple ways of accessing attributes and binding method-calls (shown previously) don’t work. The problem: if class C inherits class A and class B the objects of class C cannot begin with attributes inherited from A and at the same time begin with attributes inherited from B. In addition to these implementation problems multiple inheritance also introduces problems at the language (conceptual) level. Person - Lærer - Student - Instructor - Problemet

33 Attribute Offset with Multiple Inheritance
The problem is actually a graph colouring problem: let each name be represented by a vertex if two names are in the same scope (perhaps through inheritance) then they are connected by an edge all vertices which are adjacent must have different offsets this approach may cause empty slots in an object (fig. 14.4) the offsets (and the waste of space) may be moved to the class-descriptor (and there much more objects than classes) costing an overhead of two instructions per fetch or store (fig. 14.5) this analysis can be done compile-time a similar technique may be used for dynamic binding of methods

34 Implementation of Object Oriented Languages
Implementation of Object Oriented Languages differs only slightly from implementations of block structured imperative languages Some additional work to do for the contextual analysis Access control, e.g. private, public, procted directives Subtyping can be tricky to implement correctly The main difference is that methods usually have to be looked up dynamically, thus adding a bit of run-time overhead Only multiple inheritance poses a bigger problem


Download ppt "Languages and Compilers (SProg og Oversættere)"

Similar presentations


Ads by Google