Presentation is loading. Please wait.

Presentation is loading. Please wait.

Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

Similar presentations


Presentation on theme: "Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture."— Presentation transcript:

1 Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

2 Runtime Organization (Chapter 6) 2 What This Chapter is About A compiler translates a program from a high-level language into an equivalent program in a low-level language. The low-level program must be equivalent to the high-level program. => High-level concepts must be modeled in terms of the low-level machine. This chapter is not about the compilation process itself, but about the way we represent high-level structures in terms of a typical low-level machine’s memory architecture and machine instructions. => We need to know this before we can talk about code generation.

3 Runtime Organization (Chapter 6) 3 What This Chapter is About High-level Program Low-level Language Processor How to model high-level computational structures and data structures in terms of low-level memory and machine instructions. Procedures Expressions Variables Arrays Records Objects Methods Registers Machine Instructions Bits and Bytes Machine Stack How to model ?

4 Runtime Organization (Chapter 6) 4 What This Chapter is About Data Representation: how to represent values of the source language on the target machine. Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) Stack Storage Allocation: How to organize storage for variables (considering lifetimes of global and local variables) Routines: How to implement procedures, functions (and how to pass their parameters and return values) Heap Storage Allocation: How to organize storage for variables (considering lifetimes of heap variables) Object Orientation: Runtime organization for OO languages (how to handle classes, objects, methods)

5 Runtime Organization (Chapter 6) 5 Data Representation Data Representation: how to represent values of the source language on the target machine. Records Arrays Strings Integer Char ? 00..10 01..00... High level data structures 0: 1: 2: 3: Low level memory model word Note: addressing schema and size of “memory units” may vary …

6 Runtime Organization (Chapter 6) 6 Data Representation Important properties of a representation schema: non-confusion: different values of a given type should have different representations uniqueness: Each value should always have the same representation. These properties are very desirable (why?), but in practice they are not always satisfied: Example: confusion: approximated floating point numbers. non-uniqueness: one’s complement representation of integers.

7 Runtime Organization (Chapter 6) 7 Data Representation Important issues in data representation: constant-size representation: The representation of all values of a given type should occupy the same amount of space. direct versus indirect representation x bit pattern pointer Direct representation of a value x Indirect representation of a value x Q: What reasons could there be for choosing indirect representations?

8 Runtime Organization (Chapter 6) 8 Indirect Representation small x bit pattern Q: What reasons could there be for choosing indirect representations? To make the representation “constant size” even if representation requires different amounts of memory for different values. large x bit pattern Both are represented by pointers =>Same size

9 Runtime Organization (Chapter 6) 9 Indirect versus Direct The choice between indirect and direct representation is a key decision for a language designer/implementer. Direct representations are often preferable for efficiency: More efficient access (no need to follow pointers) More efficient storage (e.g. stack rather than heap allocation) For types with widely varying size of representation it is almost a must to use indirect representation (see previous slide) Languages like Pascal, C, C ++ try to use direct representation wherever possible. Languages like Scheme, ML use mostly indirect representation everywhere (because of higher order functions) Java: primitive types direct, “reference types” (objects) indirect.

10 Runtime Organization (Chapter 6) 10 Data Representation We now survey representation of the more common data types found in programming languages, assuming direct representations wherever possible. We will discuss representation of values of: Primitive Types Record Types Disjoint Union Types Static and Dynamic Array Types Recursive Types We will use the following notations (if T is a type): #[T]The cardinality of the type (i.e. the number of possible different values) size[T]The size of the representation (in number of bits/bytes)

11 Runtime Organization (Chapter 6) 11 Data Representation: Primitive Types What is a primitive type? The primitive types of a programming language are those types that cannot be decomposed into simpler types. For example integer, boolean, char, etc. Type: boolean Has two values true and false => #[ boolean ] = 2 => size[ boolean ] ≥ 1 bit Note: In general, if #[T] = n then size[T] ≥ log 2 n bits Value false true Possible Representations 1bitbyte(option 1)byte(option2) 000000000 00000000 10000000111111111

12 Runtime Organization (Chapter 6) 12 Data Representation: Primitive Types Type: integer Fixed size representation, usually dependent (i.e. chosen based on) what is efficiently supported by target machine. Typically uses one word (16 bits, 32 bits, or 64 bits) of storage. size[ integer ] = word (= 16 bits) => # [ integer ] ≤ 2 16 = 65536 Modern processors use two’s complement representation of integers 1000010010010101 Multiply by -(2 15 )Multiply by 2 n Value = -1*2 15 +0*2 14 +…+0*2 3 +1*2 2 +1*2 1 +1*2 0 n = position from right Q: What range of values can be represented with 16 bits?

13 Runtime Organization (Chapter 6) 13 Data Representation: Primitive Types Example: Primitive types in TAM (Triangle Abstract Machine) Type Boolean Char Integer Representation 00...00 and 00...01 Unicode Two’s complement Size 1 word Example: A (possible) representation of primitive types on a Pentium Type Boolean Char Integer Representation 00...00 and 11..11 ASCII Two’s complement Size 1 byte 1 word

14 Runtime Organization (Chapter 6) 14 Data Representation: Composite Types Composite types are types which are not “atomic”, but which are constructed from more primitive types. Records (called structs in C and classes in C ++ ) Aggregates of several values of possibly several different types Arrays Aggregates of several values of the same type Disjoint Union Types Disjoint unions are the “dual” of records. They can have values of several different types, but not more than one at the same time.

15 Runtime Organization (Chapter 6) 15 Data Representation: Records Example: Triangle Records type Date = record y : Integer, m : Integer, d : Integer end; type Details = record female : Boolean, dob : Date, status : Char end; var today: Date; var my: Details type Date = record y : Integer, m : Integer, d : Integer end; type Details = record female : Boolean, dob : Date, status : Char end; var today: Date; var my: Details

16 Runtime Organization (Chapter 6) 16 Data Representation: Records Example: Triangle Record Representation today.m 2004 3 today.y today.d 2 my.dob.m 1970 5 my.dob.y my.dob.d 17 false ‘u’ my.female my.dob my.status … 1 word:

17 Runtime Organization (Chapter 6) 17 Data Representation: Records Records occur in some form or other in most programming languages: Ada, Pascal, Triangle (here they are actually called records) C, C ++ (here they are called structs) The usual representation of a record type is just the concatenation of individual representations of each of its component types. r.I 1 r.I 2 r.I n value of type T 1 value of type T 2 value of type T n type T = record I 1 : T 1 ; I 2 : T 2 ;... I n : T n ; end; var r: T; type T = record I 1 : T 1 ; I 2 : T 2 ;... I n : T n ; end; var r: T;

18 Runtime Organization (Chapter 6) 18 Data Representation: Records Example: size[ Date ] = 3*size[ integer ] = 3 words address[today.y] = address[today]+0 address[today.m] = address[today]+1 address[today.d] = address[today]+2 address[my.dob.m] = address[my.dob]+1 = address[my]+2 Q: How much space does a record take? And how to access record elements? Note: these formulas assume that addresses are indexes of words (not bytes) in memory (otherwise multiply these offsets by 2 or 4)

19 Runtime Organization (Chapter 6) 19 Data Representation: Records Problem: Real machines: Don’t address words Have alignment concerns Example: C/C ++ on the x86 architecture size[boolean] = 1 byte size[char] = 1 byte size[int] = 4 bytes Alignment: all data must be laid out on “natural” boundaries Not for correctness, but for performance

20 Runtime Organization (Chapter 6) 20 Alignment example struct Date { int y, m, d; }; struct Details { bool female; Date dob; char status; }; struct Date { int y, m, d; }; struct Details { bool female; Date dob; char status; }; fyyy ymmm mddd ds f ??? yyyy mmmm dddd s???

21 Runtime Organization (Chapter 6) 21 Data Representation: Disjoint Unions What are disjoint unions? Like a record, has elements which are of different types. But the elements never exist at the same time. A “type tag” determines which of the elements is currently valid. Example: Pascal variant records type Number = record case discrete: Boolean of true: (i: Integer); false: (r: Real) end; var num: Number type Number = record case discrete: Boolean of true: (i: Integer); false: (r: Real) end; var num: Number Mathematically we write disjoint union types as: T = T 1 | … | T n

22 Runtime Organization (Chapter 6) 22 Data Representation: Disjoint Unions Example: Pascal variant records representation type Number = record case discrete: Boolean of true: (i: Integer); false: (r: Real) end; var num: Number type Number = record case discrete: Boolean of true: (i: Integer); false: (r: Real) end; var num: Number Assuming size[Integer]=size[Boolean]=1 and size[Real]=2, then size[Number] = size[Boolean] + MAX(size[Integer], size[Real]) = 1 + MAX(1, 2) = 3 num.i true 15 num.discrete unused num.r false num.discrete 3.14

23 Runtime Organization (Chapter 6) 23 Data Representation: Disjoint Unions type T = record case I tag : T tag of v 1 : (I 1 : T 1 ); v 2 : (I 2 : T 2 );... v n : (I n : T n ); end; var u: T type T = record case I tag : T tag of v 1 : (I 1 : T 1 ); v 2 : (I 2 : T 2 );... v n : (I n : T n ); end; var u: T v1v1 type T 1 v2v2 type T 2 vnvn type T n or … u.I 1 u.I 2 u.I tag u.I n u.I tag or size[T] = size[T tag ] + MAX(size[T 1 ],..., size[T n ]) address[u.I tag ] = address[u] address[u.I 1 ] = address[u]+size[T tag ]... address[u.I n ] = address[u]+size[T tag ]

24 Runtime Organization (Chapter 6) 24 Arrays An array is a composite data type; an array value consists of multiple values of the same type. Arrays are in some sense like records, except that their elements all have the same type. The elements of arrays are typically indexed using an integer value (In some languages such as for example Pascal, also other “ordinal” types can be used for indexing arrays). Two kinds of arrays (with different runtime representation schemas): static arrays: their size (number of elements) is known at compile time. dynamic arrays: their size can not be known at compile time because the number of elements may vary at run-time. Q: Which are the “cheapest” arrays? Why?

25 Runtime Organization (Chapter 6) 25 Static Arrays Example: type Name = array 6 of Char; var me: Name; var names: array 2 of Name type Name = array 6 of Char; var me: Name; var names: array 2 of Name ‘K’ ‘r’ ‘i’ ‘s’ ‘ ’ me[0] me[1] me[2] me[3] me[4] me[5] ‘J’ ‘o’ ‘h’ ‘n’ ‘ ’ names[0][0] names[0][1] names[0][2] names[0][3] names[0][4] names[0][5] Name ‘S’ ‘o’ ‘p’ ‘h’ ‘i’ ‘a’ names[1][0] names[1][1] names[1][2] names[1][3] names[1][4] names[1][5] Name

26 Runtime Organization (Chapter 6) 26 Static Arrays Example: type Coding = record Char c, Integer n end var code: array 3 of Coding type Coding = record Char c, Integer n end var code: array 3 of Coding ‘K’ 5 code[0].c code[0].n Coding ‘i’ 22 code[1].c code[1].n Coding ‘d’ 4 code[2].c code[2].n Coding

27 Runtime Organization (Chapter 6) 27 Static Arrays type T = array n of TE; var a : T; type T = array n of TE; var a : T; a[0] a[1] a[2] a[n-1] size[T] = n * size[TE] address[ a[0] ] = address[a] address[ a[1] ] = address[a]+size[TE] address[ a[2] ] = address[a]+ 2 *size[TE] … address[ a[k] ] = address[a]+ k *size[TE] …


Download ppt "Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture."

Similar presentations


Ads by Google