Chapter 9 Imperative and object-oriented languages 1.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

Intermediate Code Generation
Programming Languages and Paradigms
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
Compiling Object Oriented Programs Mooly Sagiv Chapter
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Compiler Construction
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
ISBN Chapter 6 Data Types: Structured types.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Elementary Data Types Scalar Data Types Numerical Data Types Other
1) Introduction to higher level types 2) Arrays and their declaration 3) Assigning values to array elements 4) Operations on arrays 5) Ada array attributes.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Assembler, Linker and OO Paradigm Professor Yihjia Tsai Tamkang University.
Arrays and Pointers in C Alan L. Cox
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Compiler Construction
Introduction to Computer Systems and the Java Programming Language.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
1 Intro to Data Structures and ADTs Chapter 2. 2 Goal of Data Structures Organize data Facilitate efficient … –storage –retrieval –manipulation Select.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
COMP 103 Bitsets. 2 Sets, and more Sets!  Unsorted Array  Sorted ArrayO(n) for at least one of  Linked Listcontains, add, remove  Binary Search TreeO(log.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Object Oriented Software Development 4. C# data types, objects and references.
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
BASIC VHDL LANGUAGE ELEMENTS Digital Design for Instrumentation with VHDL 1.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Chapter 6 – Data Types CSCE 343.
Java Primer 1: Types, Classes and Operators
Programming Language Concepts (CIS 635)
DATA HANDLING.
C Structures, Unions, Bit Manipulations and Enumerations
Introduction to Abstract Data Types
Lecture 18 Arrays and Pointer Arithmetic
Course Overview PART I: overview material PART II: inside a compiler
Names of variables, functions, classes
C Language B. DHIVYA 17PCA140 II MCA.
A type is a collection of values
Presentation transcript:

Chapter 9 Imperative and object-oriented languages 1

Source language data representation and handling In the source language, a data item has a type, which may be a basic, built-in type of the language, or a constructed type, built using one of the type constructions in the language. The target language data types are usually limited to single bytes, integers of various sizes, address representations, and floating point umbers of several sizes. 2

Basic types The usual basic types in a source language are characters, integers of several sizes, and floating point numbers of several sizes. The source language operations on these typically are arithmetic operations, assignment and comparison –The arithmetic operations: addition, subtraction, multiplication, division, and remainder. –All these can be mapped directly to the target language data types and operations. 3

Basic types Some source languages also have a void type, corresponding to no value at all. A target language representation for a void type is not needed. 4

Enumeration types An enumeration type defines a set of names to be the values of the new data type. The run-time representation is an integer, with values usually ranging from 0 to the number of enumeration values minus 1. –example: months ={Jan, Feb, …, Dec} Jan – 0 Feb – 1 … Dec – 11 (12-1) 5

Enumeration types An enumeration type which is available in many languages, including some that do not have explicit enumeration types, is the Boolean type, with false and true as enumeration literals. –example: Boolean type = {false, true} 0 1 6

Pointer type Most imperative and object-oriented languages support a pointer type of some kind. Pointer represent the addresses of source language data structures. Its target type is an unsigned integer of a size large enough to represent an address. 7

Pointer type Operation: copying, assignment, comparison, etc. The one operation that is particular to pointers is dereferencing, which consists of obtaining the value of the data structure that the pointer refers to. If the value is small to fit in a register: a single machine instruction. If the value is large: a pointer to a record or array. 8

Pointer type For example: the assignment q= *p; in which q is the name of a record variable of type T and p is a pointer to a record of the same type, may be translated to a call of a library routine byte_copy() byte_copy (&q, p, sizeof(T)); 9

Record types A record, also called a structure, is a data item in which a fixed number of members of possibly different types are grouped together. In the target language, these members are represented consecutively in a memory area that is large enough to contain all members. 10

Record types For example: struct example { int member1; double member2; }; is represented as follows: member 1 member 2 11

Record types The Scalable Processor ARChitecture (SPARC) processor requires an int (a 4-byte quantity) to have an address that is a multiple of 4 (is 4-byte aligned), and a double (an 8- byte quantity) to be 8-byte aligned. is represented as follows: member 1 member 2 gap 12

Union type A union type is a data type of which the values are of one of a set of types. For example: a variable a of type union { int m1; float m2; }a,b; holds either int m1 or float m2 for example a.m1 b.m2 the program should be aware of which one is present 13

Array type An array type describes data structures which consist of series of items (also called elements) of the same type. An array can have one dimension (a vector), two dimensions (a matrix), or more. For example: A: array [1..3] of integer; A[1] A[2] A[3] B: array [1..2, 1..3] of integer row column 14

Array type The run-time representation of the array consists of a consecutive sequence of the representation of the elements; the address of the first element is called the base address of the array. All languages that support arrays also support element selection, which is usually called indexing. 15

Array type Assume an n-dimensional array A with base address base (A), where dimension k has lower bound LB k and upper bound UB k. The number of elements along dimension k is then LEN k = UB k -LB k +1 dimension 1 (row) lower bound LB 1 = 1 upper bound UB 1 = 2 dimension 2 (column) LB 1 = 1 UB 2 = 3 number of elements in dimension1.LEN 1 = 2 number of elements in dimension2.LEN 2 = 3 16

Array type How to compute the location (address) of A[1, 3], i 1 =1, i 2 =3. base (A) + [(i 1 -LB 1 )*LEN 2 + (i 2 -LB 2 )] * element size = base (A) + [(1-1) * 3 + (3-1)] * element size = base (A) +2 * element size 17

Array type 3 dimensions: base (A) + [(i1-LB 1 ) * LEN 2 * LEN 3 +(i 2 -LB 2 ) * LEN 3 + (i 3 -LB 3 )] * element size Example: A: array [1...2, 1…3, 1…4] of integer assume base (A) = 0000, an integer takes 4 bytes A[2,2,3] = base (A) + [(2-1) * 3 * 4 + (2-1) * 4 + (3-1)] * 4 = =72 18

Array type K dimensions: base (A) + [(i 1 -LB 1 ) * LEN 2 * LEN 3 * …LEN k + (i 2 -LB 2 ) * LEN 3 * …LEN k + … (i k-1 – LB k-1 ) * LEN k + (i k – LB k ) ] *element size 19

Set type If it is a small sub-range of integer, it can be implemented as bitset. e.g. The set of integers ranging 0-31 can be represented in a 32-bit word. 2nd element in the set, other elements not. Operations: –Set union is implemented with a bitwise OR –Set intersection is implemented with a bitwise AND, –Symmetric set difference is implemented with a bitwise EXCLUSIVE OR, –Set difference is implemented with a bitwise NOT ……….0 20

Object type An object is a record with built-in methods, with some additional features. Its type is a class. Operations: –field selection: o1.i1, o1.f1, o2.i1, o2.f2 –Copying: set o1 to o2 –Method invocation: o1.m1() 21

Object type Object also have constructors and destructors, which are methods that are to be invoked on object creation and object removal, respectively. class A { int a1; int a2; method A(); method m1(); method m2(); }; Method of object constructor, destructor. o1 = A (); destruct-A(o1); 22

Object type Target code of o1 is: Method table for A: a1 a2 A_A m1_A m2_A 23

Object type Assume: m2_A() has one integer parameter and returns no value and where Class_A is the C type name for class A Target code for m2_A: void m2_A (class_A *this, int i) { Body of m2_A, } 24

Inheritance Inheritance allows the programmer to base a class B on a class A, so that B inherits the methods and fields of A, in additional to its own fields and methods. This feature is also known as type extension: class B extends class A with zero or more fields and methods. Class A is the parent class of class B, and class B is a subclass of class A. 25

Inheritance class B extends class A { int b1; method B; method m3_B; } o2 = B() o2 looks like: b1 a2 a1 26

Method overriding When a class B extends a class A, it may redefine one or more of A’s methods; this feature is called method overriding. –Method declaration: method m1(); –Method definition: method m1() {a=1;} –Method re-definition: in subclass, method m1() {a=2;} –Abstract class: a class in which at least one method is declared, but not defined. The actual methods must then be defined in classes that extend the abstract class. 27

Method overriding class A { … method m1(); {…} method m2(); {…} }; class B extends A { … method m2() {…} method m3() {…} }; 28

Method overriding Method table for A: Method table for B: a is an object of A b is an object of B a.m2() invokes m2_A_A b.m2() invokes m2_A_B m1_A_A m2_A_A m1_A_A m2_A_B m3_B_B 29