Download presentation

Presentation is loading. Please wait.

Published byJon Devere Modified about 1 year ago

1
1 Chapter 6 Data Types What is a data type? A set of values versus A set of values + set of operations on those values

2
2 Why data types? Data abstraction –Programming style – incorrect design decisions show up at translation time –Modifiability – enhance readability Type checking (semantic analysis) can be done at compile time - the process a translator goes through to determine whether the type information is consistent Compiler uses type information to allocate space for variables Translation efficiency: Type conversion (coercion) can be done at compile time

3
3 Overview cont… Declarations –Explicit type information Type declarations –Give new names to types in type declararion Type checking –Type Inference Rules for determining the types of constructs from available type information –Type equivalence determines if two types are the same Type system –Type construction methods + type inference rules + type equivalence algorithm

4
4 Simple Types Predefined types Enumerated types Pascal type fruit = (apple, orange, banana); C/C++ enum fruit { apple, orange, banana };

5
5 Simple Types Subrange types Pascal type byte = 0..255; minors = 0..19; teens = 13..19; ADA subtype teens is INTEGER range 13..19;

6
6 Data Aggregates and Type Constructors Aggregate (compound) objects and types are constructed from simple types Recursive – can also construct aggregate objects and types from aggregate types Predefined – records, arrays, strings……

7
7 Constructors - Cartesian Product U X V = { (u, v) | u is in U and v is in V} Projection functions –p 1 : U X V -> U and P 2 : U X V -> V Pascal family of languages - record type polygon = record edgeCt: integer; edgeLen: real end; var a : polygon; Cartesian product type INTEGER X REAL Projections a.edgeCt a.edgeLen

8
8 Constructors – Mapping (arrays) The array constructor defines mappings as data aggregates Mapping from an array index to the value stored in that position in the array The domain is the values of the index The range is the values stored in the array

9
9 Constructors Mapping (arrays) C/C++ typedef int little_people_num[3]; little_people_num gollum_city = {0, 0, 0} gollum_city[3] = 5 typedef int matrix[10][20];

10
10 Arrays Design Issues: 1. What types are legal for subscripts? 2. Are subscripting expressions in element references range checked? 3. When are subscript ranges bound? 4. When does allocation take place? 5. What is the maximum number of subscripts? 6. Can array objects be initialized? 7. Are any kind of slices allowed?

11
11 Arrays Indexing is a mapping from indices to elements map(array_name, index_value_list) an element

12
12 Arrays Subscript Types: –FORTRAN, C - integer only –Pascal - any ordinal type (integer, boolean, char, enum) –Ada - integer or enum (includes boolean and char) –Java - integer types only

13
13 Arrays Categories of arrays (based on subscript binding and binding to storage) 1. Static - range of subscripts and storage bindings are static e.g. FORTRAN 77, some arrays in Ada –Advantage: execution efficiency (no allocation or deallocation)

14
14 Arrays 2. Fixed stack dynamic - range of subscripts is statically bound, but storage is bound at elaboration time (at function call time) –e.g. Most Java locals, and C locals that are not static –Advantage: space efficiency

15
15 Arrays 3. Stack-dynamic - range and storage are dynamic (decided at run time), but fixed after initial creation on for the variable’s lifetime –e.g. Ada declare blocks declare STUFF : array (1..N) of FLOAT; begin... end; –Advantage: flexibility - size need not be known until the array is about to be used

16
16 Arrays 4. Heap-dynamic – stored on heap and sizes decided at run time. e.g. (FORTRAN 90) INTEGER, ALLOCATABLE, ARRAY (:,:) :: MAT (Declares MAT to be a dynamic 2-dim array) ALLOCATE (MAT (10,NUMBER_OF_COLS)) (Allocates MAT to have 10 rows and NUMBER_OF_COLS columns) DEALLOCATE MAT (Deallocates MAT ’s storage)

17
17 Arrays 4. Heap-dynamic (continued) –Truly dynamic: In APL, Perl, and JavaScript, arrays grow and shrink as needed –Fixed dynamic: Java, once you declare the size, it doesn’t change

18
18 Arrays Number of subscripts –FORTRAN I allowed up to three –FORTRAN 77 allows up to seven –Others - no limit Array Initialization –Usually just a list of values that are put in the array in the order in which the array elements are stored in memory

19
19 Arrays Array Operations 1. APL - many, see book (p. 240-241) 2. Ada –Assignment; RHS can be an aggregate constant or an array name –Catenation; for all single-dimensioned arrays –Relational operators (= and /= only) 3. FORTRAN 90 –Intrinsics (subprograms) for a wide variety of array operations (e.g., matrix multiplication, vector dot product)

20
20 Arrays Slices –A slice is some substructure of an array; nothing more than a referencing mechanism –Slices are only useful in languages that have array operations

21
21 Arrays Slice Examples: 1. FORTRAN 90 INTEGER MAT (1:4, 1:4) MAT(1:4, 1) - the first column MAT(2, 1:4) - the second row

22
22 Example Slices in FORTRAN 90

23
23 Arrays Implementation of Arrays –Access function maps subscript expressions to an address in the array –Row major (by rows) or column major order (by columns)

24
24 Locating an Element

25
25 Compile-Time Descriptors Single-dimensioned array Multi-dimensional array

26
26 Accessing Formulas – 1D Address(A[i]) = BegAdd + (i-lb)*size + VirtualOrigin +i*size lb: lower bound size: number of bytes of one element Virtual origin allows us to do math once, so don’t have to repeat each time. You must check for valid subscript before you use this formula, as obviously, it doesn’t care what subscript you use.

27
27 Accessing Formulas Multiple Dimensions ub i : upper bound in i th dimension lb i : lower bound in i th dimension length i = ub i –lb i +1 In row-major order Address(A[i,j]) = begAdd + size((i-lb i )*length j + j-lb j ) = VO + i*mult i + j*mult j

28
28 Address(A[i,j]) = begAdd + size((i-lb i )*length j + j-lb j ) = VO + i*mult i + j*mult j = 40 +28i+20j For Example: array of floats A[0..6, 3..7] beginning at location 100 begAdd = 100 size = 4 (if floats take 4 bytes) lb i = 0 ub i = 6 length i = 7 lb j = 3 ub j = 7 length j = 5 VO = 100 + 4*(-3)*5 = 40 mult i = 28 mult j = 20 VO40 lb i 0 ub i 6 mult i 28 lb j 3 ub j 7 mult j 20 repeated for each dimension

29
29 Accessing Formulas Multiple Dimensions In column-major order Address(A[i,j]) = begAdd + size((i-lb i ) + (j-lb j )*length i ) In 3D in row major: Addr(A[I,j,k]) = begAdd + size*((i-lb i )*length j *length k ) + (j-lb j )length k + k-lb k )

30
30 Accessing Formulas Slices Suppose we want only the second row of our previous example. We would need array descriptor to look like a normal 1D array (as when you pass the slice, the receiving function can’t be expected to treat it any differently)

31
31 Address(A[i,j]) = begAdd + size((i-lb i )*length j + j-lb j ) = VO + i*mult i + j*mult j = 40 +28i+20j For Example: array of floats A[0..6, 3..7] beginning at location 100 If we want only the second row, it is like we have hardcoded the j=2 in the accessing formula, A[0..6,2] The accessing formula is simple Just replace j with 2, adjust the VO, and remove the ub,lb, and length associated with j so 40 +28i+20j = 80+28i and the table is changed accordingly (and looks just like the descriptor for a regular 1D array) VO80 lb i 0 ub i 6 mult i 28

32
32 Constructors Union Cartesian products – conjunction of fields Union – disjunction of fields Discriminated or undiscriminated

33
33 Pascal Variant Record – discriminated union type address_range = 0..maxint; address_type = (absolute, offset); safe_address = record case kind: address_type of absolute: (abs_addr:address_range); offset: (off_addr: integer); end

34
34 References A reference is the address of an object under the control of the system – which cannot be used as a value or operated on C++ is perhaps the only language where pointers and references exist together. References in C++ are constant pointers that are dereferenced everytime they are used.

35
35 Constructors Pointer and Recursive Types Some languages (Pascal, Ada) require pointers to be typed PL/1 treated pointers as untyped data objects What is the significance of this for a type checker? C pointers are typed but C allows arithmetic operations on them unlike Pascal and Ada

36
36 Type Equivalence When are two types the same Structural equivalence Declaration equivalence Name equivalence

37
37 Structural Equivalence Two types are the same if they have the same structure i.e. they are constructed in exactly the same way using the same type constructors from the same simple types May look alike even when we wanted them to be treated as different.

38
38 Structural Type Equivalence typedef int anarray[10]; typedef struct { anarray x; int y;} struct1; typedef struct { int x[10]; int y; }struct2; typedef int anarray[10]; typedef struct { anarray a; int b; }struct3; typedef int anarray[10]; typedef struct { int b; anarray a; }struct4; (Note we are just using the syntax of C as an example. C does NOT use structural equivalence for structs

39
39 Structural Equivalence Check representing types as trees –Check equivalence recursively on subtrees Consider… Dynamic arrays Type array1 = array[-1..9] of integer; array2 = array[0..10] of integer; Array (INTEGER range <>) of INTEGER

40
40 Name Equivalence Two name types are equivalent only if they have the exact same type name Name equivalence in Ada and C ar1 and ar2 are not considered name equivalent typedef int ar1[10]; typedef ar1 ar2; typedef int age; type ar1 is array (INTEGER range1..10) of INTEGER; type ar2 is new ar1; type age is new INTEGER;

41
41 Name equivalence… v1: ar1; v2: ar1; v3: ar2; v4: array (INTEGER range 1..100) of INTEGER; v5: array (INTEGER range 1..100) of INTEGER; v4 and v4 cannot be name equivalent, as there is no name. v6,v7: array (INTEGER range 1..100) of INTEGER;

42
42 Declaration Equivalent Lead back to the same original structure declaration via a series of redeclarations type t1 = array [1..10] of integer; t2 = t1; t3 = t2; These are the same type type t4 = array [1..10] of integer; t5 = array [1..10] of integer; These are different types.

43
43 Type Checking Involves the application of a type equivalence algorithm to expressions and statements to determine if they make sense Any attempt to manipulate a data object with an illegal operation is a type error Program is said to be type safe (or type secure) if guaranteed to have no type errors Static versus dynamic type checking Run time errors

44
44 Type Checking… Strong typing and type checking –Strong guarantees type safety A language is strongly typed if its type system guarantees statically (as far as possible) that no data-corrupting errors can occur during execution. Statically typed versus dynamically typed –Static (type of every program expression be known at compile time) All variables are declared with an associated type All operations are specified by stating the types of the required operands and the type of the result Java is strongly typed Python is dynamic typed and strong typed

45
45 Strongly typed /* Python code */ >>> foo = "x" >>> foo = foo + 2 Traceback (most recent call last): File " ", line 1, in ? foo = foo + 2 TypeError: cannot concatenate 'str' and 'int' objects >>> foo is of str type. In the second line, we're attempting to add 2 to a variable of str type. A TypeError is returned, indicating that a str object cannot be concatenated with an int object. This is what characterizes strong typed languages: variables are bound to a particular data type.

46
46 Weakly Typed /* PHP code */ In this example, foo is initially a string type. In the second line, we add this string variable to 2, an integer. This is permitted in PHP, and is characteristic of all weak typed languages. C is static typed and weak typed as we can cast a variable to be a different type (which should not be allowed if it isn’t of that type)

47
47 Type Conversion r is float and j is integer r = j + 45.6 i is integer and j is integer i = j + 45.6

48
48 Type Conversion… Modula2 i := TRUNC (FLOAT(j) + 45.6) Explicit type conversion –type conversion functions Implicit conversion –coercion –can weaken type checking – as lots of coercions are allowed. For example, if (a=b) coerces int to boolean.

49
49 Type conversion… Casts –A value or object of one type is preceded by a type name (int) 3.14 Often does not cause a conversion to take place. Internal representation is reinterpreted as a new type CARDINAL(-1) 65535 This is very dangerous!

50
50 Type Conversions Def: A mixed-mode expression is one that has operands of different types Def: A coercion is an implicit type conversion The disadvantage of coercions: –They decrease in the type error detection ability of the compiler In most languages, all numeric types are coerced in expressions, using widening conversions In Ada, there are virtually no coercions in expressions

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google