1 adapted from slides for comp 144 programming language concepts spring 2002 by felix...

25
1 Adapted from slides for Adapted from slides for COMP 144 Programming Language COMP 144 Programming Language Concepts Concepts Spring 2002 Spring 2002 by by Felix Hernandez-Campos Felix Hernandez-Campos The University of North Carolina at The University of North Carolina at Chapel Hill Chapel Hill

Upload: mervyn-hudson

Post on 27-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

11

Adapted from slides forAdapted from slides for

COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts

Spring 2002Spring 2002

byby

Felix Hernandez-CamposFelix Hernandez-Campos

The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill

22

Data TypesData Types

• Computers manipulate sequences of bitsComputers manipulate sequences of bits

• But most programs manipulate more general dataBut most programs manipulate more general data– NumbersNumbers– StringString– ListsLists– ……

• Programming languages provide data types that raise Programming languages provide data types that raise the level of abstraction from bits to datathe level of abstraction from bits to data

– But computer hardware only knows about bits!But computer hardware only knows about bits!

33

Data TypesData TypesThe Purpose of TypesThe Purpose of Types

• Types provide Types provide implicit contextimplicit context– Compilers can infer more information, so programmers Compilers can infer more information, so programmers

write less codewrite less code– E.g. E.g. the expression a+b in Java may be adding two integer, the expression a+b in Java may be adding two integer,

two floats or two strings depending on the contexttwo floats or two strings depending on the context

• Types provides a set of semantically Types provides a set of semantically valid valid operationsoperations

– Compilers can detect semantic mistakesCompilers can detect semantic mistakes– E.g.E.g. Python’s lists support Python’s lists support append()append() and and pop()pop(), but , but

complex numbers do not.complex numbers do not.

44

Type SystemsType Systems

• High-level languages have High-level languages have type systemstype systems– All objects and expressions have a typeAll objects and expressions have a type– E.g.E.g. void (*)(const int)void (*)(const int) is the type of a C++ function is the type of a C++ function

• A type system consists ofA type system consists of– A mechanism for defining types and associating them with A mechanism for defining types and associating them with

certain language constructscertain language constructs– A set of rules for A set of rules for type checkingtype checking

» Type equivalenceType equivalence» Type compatibilityType compatibility» Type inferenceType inference

55

Type SystemsType SystemsType CheckingType Checking

• Type checking is the process of ensuring that a Type checking is the process of ensuring that a program obeys the language’s program obeys the language’s type compatibility type compatibility rulesrules

• Strongly typed languagesStrongly typed languages always detect types errors always detect types errors– Weakly typed languagesWeakly typed languages do not do not– All expressions and objects must have a typeAll expressions and objects must have a type– All operations must be applied in appropriate type contextsAll operations must be applied in appropriate type contexts

• Statically typed languagesStatically typed languages are strongly typed language are strongly typed language in which all type checking occurs at compile typein which all type checking occurs at compile type

– Dynamically typed languagesDynamically typed languages: some checking at run-time: some checking at run-time

66

What is a type?What is a type?

• Three points of view:Three points of view:– Denotational: Denotational: a set of valuesa set of values– Constructive: Constructive: a type is built-in type or a composite typea type is built-in type or a composite type

» Composite types are created using type constructorsComposite types are created using type constructors» E.g.E.g. In Java, In Java, booleanboolean is a built-in type, while is a built-in type, while boolean[]boolean[] is a is a

composite typecomposite type

– Abstraction-based: Abstraction-based: a type is an interface that defines a set a type is an interface that defines a set of consistent operationsof consistent operations

• These points of view complement each otherThese points of view complement each other

77

Classification of TypesClassification of TypesBuilt-in TypesBuilt-in Types

• Built-in/Primitive/Elementary typesBuilt-in/Primitive/Elementary types– Mimic hardware unitsMimic hardware units– E.g.E.g. boolean, character, integer, real (float) boolean, character, integer, real (float)

• Their implementation varies across languagesTheir implementation varies across languages

• CharactersCharacters are traditionally one-byte quantities are traditionally one-byte quantities using the ASCII character setusing the ASCII character set

– Early computers had a different byte sizesEarly computers had a different byte sizes» Byte = 8 bits standardized by Fred Brooks Byte = 8 bits standardized by Fred Brooks et al.et al. thanks to the thanks to the

IBM System/360IBM System/360

– Other character sets have also been usedOther character sets have also been used

88

Built-in TypesBuilt-in TypesUnicode CharactersUnicode Characters

• Newer languages have built-in characters that Newer languages have built-in characters that support the Unicode character setsupport the Unicode character set

– http://www.unicode.org/unicode/standard/WhatIsUnicode.http://www.unicode.org/unicode/standard/WhatIsUnicode.htmlhtml

– Unicode is implemented using two-byte quantitiesUnicode is implemented using two-byte quantities– E.g.E.g. Java Java

» http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.htmlhttp://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

– E.g.E.g. Python Python » u”¡Hola!”u”¡Hola!”» http://www.python.org/doc/current/tut/node5.html#SECTION005130000http://www.python.org/doc/current/tut/node5.html#SECTION005130000

000000000000000000000000

99

Built-in TypesBuilt-in TypesNumeric TypesNumeric Types

• Most languages support integers and floatsMost languages support integers and floats– The range of value is implementation dependentThe range of value is implementation dependent

• Some languages support other numeric typesSome languages support other numeric types– Complex numbers (Complex numbers (e.g. e.g. Fortran, Python)Fortran, Python)– Rational numbers (Rational numbers (e.g.e.g. Scheme, Common Lisp) Scheme, Common Lisp)– Signed and unsigned integers (Signed and unsigned integers (e.g.e.g. C, Modula-2) C, Modula-2)– Fixed point numbers (Fixed point numbers (e.g.e.g. Ada) Ada)

• Some languages distinguish numeric types Some languages distinguish numeric types depending on their precisiondepending on their precision

– Single vs. double precision numbersSingle vs. double precision numbers» C’s C’s intint (4 bytes) and (4 bytes) and longlong (8 bytes) (8 bytes)

1010

Classification of TypesClassification of TypesEnumerationsEnumerations

• Enumeration improve program readability and error Enumeration improve program readability and error checkingchecking

• They were first introduced in PascalThey were first introduced in Pascal– E.g. E.g. type weekday = (sun, mon, tue, wed, thu, type weekday = (sun, mon, tue, wed, thu, fri, sat);fri, sat);

– They define an order, so they can be used in enumeration-They define an order, so they can be used in enumeration-controlled loopscontrolled loops

– The same feature is available in CThe same feature is available in C» enum weekday {sun, mon, tue, wed, thu, fri, sat};enum weekday {sun, mon, tue, wed, thu, fri, sat};

– Other languages use constants to define enumerationOther languages use constants to define enumeration– Pascal’s approach is more complete: integers and Pascal’s approach is more complete: integers and

enumerations are not compatibleenumerations are not compatible

1111

Classification of TypesClassification of TypesSubrangesSubranges

• Subranges improve program readability and error Subranges improve program readability and error checkingchecking

• They were first introduced in PascalThey were first introduced in Pascal– E.g. E.g. type test_score = 0..100; type test_score = 0..100;

workday = mon..fri;workday = mon..fri;

– They define an order, so they can be used in enumeration-They define an order, so they can be used in enumeration-controlled loopscontrolled loops

1212

Classification of TypesClassification of TypesComposite TypesComposite Types

• Composite/Constructed types are created applying a Composite/Constructed types are created applying a constructor to one or more simpler typesconstructor to one or more simpler types

• ExamplesExamples– RecordsRecords– Variant RecordsVariant Records– ArraysArrays– SetsSets– PointersPointers– ListsLists– FilesFiles

1313

Classification of TypesClassification of TypesOrthogonalityOrthogonality

• Orthogonality is an important property in the design Orthogonality is an important property in the design of type systemsof type systems

• More orthogonal languages support have more More orthogonal languages support have more flexible composite typesflexible composite types

• Scheme is a good exampleScheme is a good example

1414

RecordsRecords

1515

RecordsRecordsMemory LayoutMemory Layout

• Basic layout in 32-bit machinesBasic layout in 32-bit machines– There may be There may be holesholes in the allocation of memory in the allocation of memory

1616

RecordsRecordsMemory LayoutMemory Layout

• Packed layoutPacked layout

• Rearranging record fieldRearranging record field

1717

RecordsRecordsImplications of Memory LayoutImplications of Memory Layout

• More memory efficient layouts have several More memory efficient layouts have several drawbacksdrawbacks

– Assignments require multiple instructionsAssignments require multiple instructions» Masking and shifting operationsMasking and shifting operations

– Access to record elements requires multiple instructionsAccess to record elements requires multiple instructions» Masking and shifting operationsMasking and shifting operations

• Holes complicate record equality Holes complicate record equality – Requires field by field comparison or default values in Requires field by field comparison or default values in

holesholes– Some languages forbid record comparisonSome languages forbid record comparison

» E.g.E.g. Pascal, C Pascal, C

1818

Variant RecordsVariant Records

• A variant record A variant record provides two or provides two or more alternative more alternative fields or collections fields or collections of fields, of fields, butbut only only one bit is valid at one bit is valid at any given timeany given time

– They are called They are called unions in C/C++unions in C/C++

1919

Variant RecordsVariant RecordsMemory LayoutMemory Layout

• Some languages, like C, do not check whether Some languages, like C, do not check whether variant records are properly accessedvariant records are properly accessed

• Other languages keep track of the value in an Other languages keep track of the value in an additional field, increasing safetyadditional field, increasing safety

2020

ArraysArraysMemory LayoutMemory Layout

• Arrays are usually stored in contiguous locationsArrays are usually stored in contiguous locations

• Two common orders for 2D arraysTwo common orders for 2D arrays

2121

ArraysArraysMemory LayoutMemory Layout

• Contiguous allocation vs. row pointersContiguous allocation vs. row pointers

2222

ArraysArraysAddress CalculationsAddress Calculations

• Code generation for a 3D arrayCode generation for a 3D array– A: array [LA: array [L11..U..U11] of array [L] of array [L22..U..U22] of array [L] of array [L33..U..U33] of ] of

elem_typeelem_type

2323

ArraysArraysAddress CalculationsAddress Calculations

Row-MajorRow-MajorOrderOrder

Compile-Time Compile-Time ConstantConstant

OptimizationOptimization

2424

Pointer Data TypePointer Data Type

• Contains an address or NULLContains an address or NULL

• Provides indirect addressing and dynamic storage Provides indirect addressing and dynamic storage managementmanagement

• Operations – assignment and dereferencingOperations – assignment and dereferencing

• Problems – dangling pointers, memory leaksProblems – dangling pointers, memory leaks

• Language question – does it provide solutions to Language question – does it provide solutions to these problems? (ex. C++)these problems? (ex. C++)

2525

Reference Data TypeReference Data Type

• Like a pointer but usually more limitedLike a pointer but usually more limited

• C++ reference type is constant – can’t be reassignedC++ reference type is constant – can’t be reassigned

• Java reference type is like a pointer except safer – Java reference type is like a pointer except safer – why?why?