1 / 88

Languages and Compilers (SProg og Oversættere)

Languages and Compilers (SProg og Oversættere). Bent Thomsen Department of Computer Science Aalborg University. With acknowledgement to Norm Hutchinson whose slides this lecture is based on. input. input. input. output. output. output. Source Text. AST. Decorated AST. Object Code.

kali
Download Presentation

Languages and Compilers (SProg og Oversættere)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. input input input output output output Source Text AST Decorated AST Object Code Where are we (going)? A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases. Dependency diagram of a typical Multi Pass Compiler: Compiler Driver calls calls calls Syntactic Analyzer Contextual Analyzer Code Generator

  3. Java Program Triangle Program C Program Compile Compile Compile JVM Program x86 Program TAM Program Run Run Run Result Result Result We shall look at this in more detail the next couple of lectures Code Generation A compiler translates a program from a high-level language into an equivalent program in a low-level language.

  4. TAM machine architecture • TAM is a stack machine • There are no data registers as in register machines. • The temporary data are stored in the stack. • But, there are special registers (Table C.1 of page 407) • TAM Instruction Set • Instruction Format (Figure C.5 of page 408) • op: opcode (4 bits) • r: special register number (4 bits) • n: size of the operand (8 bits) • d: displacement (16 bits) • Instruction Set • Table C.2 of page 409

  5. TAM Registers

  6. TAM Machine code • Machine code are 32 bits instructions in the code store • op (4 bits), type of instruction • r (4 bits), register • n (8 bits), size • d (16 bits), displacement • Example: LOAD (1) 3[LB]: • op = 0 (0000) • r = 8 (1000) • n = 1 (00000001) • d = 3 (0000000000000011) • 0000 1000 0000 0001 0000 0000 0000 0011

  7. TAM Instruction set

  8. TAM machine architecture • Two Storage Areas • Code Store (32 bits words) • Code Segment: to store the code of the program to run • Pointed to by CB and CT • Primitive Segment: to store the code for primitive operations • Pointed to by PB and PT • Data Store (16 bits words) • Stack • global segment at the base of the stack • Pointed to by SB • stack area for stack frames of procedure and function calls • Pointed to by LB and ST • Heap • heap area for the dynamic allocation of variables • Pointed to by HB and HT

  9. TAM machine architecture

  10. Expression Evaluation on a Stack Machine (SM) Stack machine: On a stack machine, the intermediate results are stored on a stack. Operations take their arguments from the top of the stack and put the result back on the stack. Typical Instructions: STORE a LOAD x MULT SUB ADD • Stack machine: • Very natural for expression evaluation (see examples on next two pages). • Requires more instructions for the same expression, but the instructions are simpler.

  11. Expression Evaluation on a Stack Machine Example 1: Computing (a * b) + (1 - (c * 2)) on a stack machine. LOAD a //stack: a LOAD b //stack: a b MULT //stack: (a*b) LOAD #1//stack: (a*b) 1 LOAD c//stack: (a*b) 1 c LOAD #2//stack: (a*b) 1 c 2 MULT//stack: (a*b) 1 (c*2) SUB //stack: (a*b) (1-(c*2)) ADD //stack: (a*b)+(1-(c*2)) Note the correspondence between the instructions and the expression written in postfix notation: a b * 1 c 2 * - +

  12. Expression Evaluation on a Stack Machine Example 2: Computing (0 < n) && odd(n) on a stack machine. LOAD #0//stack: 0 LOAD n //stack: 0 n LT //stack: (0<n) LOAD n//stack: (0<n) n CALL odd//stack: (0<n) odd(n) AND //stack: (0<n)&&odd(n) This example illustrates that calling functions/procedures fits in just as naturally with the stack machine evaluation model as operations that correspond to machine instructions. In register machines this is much more complicated, because a stack must be created in memory for managing subroutine calls/returns.

  13. The “Phases” of a Compiler Source Program Syntax Analysis Error Reports Abstract Syntax Tree Contextual Analysis Error Reports Decorated Abstract Syntax Tree Code Generation Next lecture Object Code

  14. Storage Allocation 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 lecture is not about the code generation phase 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.

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

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

  17. 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, but in practice they are not always satisfied: • Example: • confusion: approximated floating point numbers. • non-uniqueness: one’s complement representation of integers • +0 and -0

  18. 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 Indirect representation of a value x Direct representation of a value x x bit pattern • x bit pattern handle

  19. Indirect Representation 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. • small x bit pattern Both are represented by pointers =>Same size • big x bit pattern

  20. 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 class” (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 polymorphic higher order functions) Java: primitive types direct, “reference types” indirect, e.g. objects and arrays.

  21. Data Representation • We now survey representation of the data types found in the Triangle programming language, assuming direct representations wherever possible. • We will discuss representation of values of: • Primitive Types • Record Types • Static Array Types We will use the following notations (if T is a type): #[T] The cardinality of the type (i.e. the number of possible values) size[T] The size of the representation (in number of bits/bytes)

  22. 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 Possible Representation 1bit byte(option 1) byte(option2) 0 00000000 00000000 1 00000001 11111111 Value false true Note: In general if #[T] = n then size[T] ≥ log2n bits

  23. 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] ≤ 216 = 65536 Modern processors use two’s complement representation of integers Multiply with -(215) n = position from left Multiply with 2n 1 0 0 0 0 1 0 0 1 0 0 1 0 1 1 1 Value = -1.215 +0.214 +…+0.23+1.22 +1.21 +1.20

  24. 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 1 word 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 byte 1 word

  25. 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) • Aggregates of several values of several different types • Arrays • Aggregates of several values of the same type • Variant Records or Disjoined Unions • (Pointers or References) • (Objects) • (Functions)

  26. 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

  27. Data Representation: Records Example: Triangle Record Representation false my.female today.y my.dob.y 2002 1970 2 5 today.m my.dob my.dob.m 5 17 today.d my.dob.d ‘u’ my.status … 1 word:

  28. 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. value of type T1 r.I1 r.I2 value of type T2 r.In value of type Tn

  29. Data Representation: Records Q: How much space does a record take? And how to access record elements? 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 Note: these formulas assume that addresses are indexes of words (not bytes) in memory (otherwise multiply offsets by 2)

  30. 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 Mathematically we write disjoint union types as: T = T1 | … | Tn

  31. 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 num.discrete num.discrete true false 3.14 15 num.i num.r unused 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

  32. Data Representation: Disjoint Unions type T = record case Itag: Ttagof v1: (I1: T1); v2: (I2: T2); ... vn: (In: Tn); end; var u: T size[T] = size[Ttag] + MAX(size[T1], ..., size[Tn]) address[u.Itag ] = address[u] address[u.I1] = address[u]+size[Ttag] ... address[u.In] = address[u]+size[Ttag] u.Itag v1 u.Itag v2 u.Itag vn u.I1 type T1 u.I2 type T2 u.In type Tn or … or or

  33. 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?

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

  35. code[0].c ‘K’ Coding code[0].n 5 code[1].c ‘i’ Coding code[1].n 22 code[2].c ‘d’ Coding code[2].n 4 Static Arrays Example: • type Coding = record • Char c, Integer n • end • var code: array 3 of Coding

  36. Static Arrays typeT = arraynofTE; var a : T; a[0] 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[i]] = address[a]+i*size[TE] … a[1] a[2] a[n-1]

  37. Dynamic Arrays Dynamic arrays are arrays whose size is not known until run time. Example 1: Java Arrays (all arrays in Java are dynamic) char[ ] buffer; buffer = new char[buffersize]; ... for (int i=0; i<buffer.length; i++) buffer[i] = ‘ ’; Dynamic array: no size given in declaration Array creation at runtime determines size Can ask for size of an array at run time Q: How could we represent Java arrays?

  38. Dynamic Arrays Java Arrays char[ ] buffer; buffer = new char[len]; A possible representation for Java arrays buffer[0] ‘C’ buffer[1] ‘o’ buffer.length 7 buffer[2] ‘m’ buffer.origin • buffer[3] ‘p’ buffer[4] ‘i’ buffer[5] ‘l’ buffer[6] ‘e’

  39. Dynamic Arrays Java Arrays char[ ] buffer; buffer = new char[len]; Another possible representation for Java arrays buffer.length 7 buffer • buffer[0] ‘C’ buffer[1] ‘o’ buffer[2] ‘m’ Note: In reality Java also stores a type in its representation for arrays, because Java arrays are objects (instances of classes). buffer[3] ‘p’ buffer[4] ‘i’ buffer[5] ‘l’ buffer[6] ‘e’

  40. Static Storage Allocation Example: Global variables in Triangle let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; in ... Exist as long as program is running • Compiler can: • compute exactly how much memory is needed for globals. • allocate memory at a fixed position for each global variable.

  41. Static Storage Allocation Example: Global variables in Triangle let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; a[0] a a[1] a[2] b c address[a] = 0 address[b] = 3 address[c] = 4 address[t] = 5 t.y t t.m t.d

  42. as long as the program is running when procedure Y is active when procedure Z is active Stack Storage Allocation Now we will look at allocation of local variables Example: When do the variables in this program “exist” let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y() ~ let var d: Integer; var e: ... in ... ; proc Z() ~ let var f: Integer; in begin ...; Y(); ... end in begin ...; Y(); ...; Z(); end

  43. time global Y Z Z 1 Y 2 call depth Stack Storage Allocation A “picture” of our program running: Start of program End of program 1) Procedure activation behaves like a stack (LIFO). 2) The local variables “live” as long as the procedure they are declared in. 1+2 => Allocation of locals on the “call stack” is a good model.

  44. Stack Storage Allocation: Accessing locals/globals First time around, we assume that in a procedure only local variables declared in that procedure and global variables are accessible. We will extend on this later to include nested scopes and parameters. • A stack allocation model (under the above assumption): • Globals are allocated at the base of the stack. • Stack contains “frames”. • Each frame correspond to a currently active procedure. (It is often called an “activation frame”) • When a procedure is called (activated) a frame is pushed on the stack • When a procedure returns, its frame is popped from the stack.

  45. Stack Storage Allocation: Accessing locals/globals SB SB = Stack base LB = Locals base ST = Stack top globals call frame Dynamic link LB call frame ST

  46. What’s in a Frame? LB dynamic link Link data return address locals Local data ST • A frame contains • A dynamic link: to next frame on the stack (the frame of the caller) • Return address • Local variables for the current activation

  47. new call frame for f() What Happens when Procedure is called? SB = Stack base LB = Locals base ST = Stack top call frame LB call frame • When procedure f() is called • push new f() call frame on top of stack. • Make dynamic link in new frame point to old LB • Update LB (becomes old ST) ST

  48. current call frame for f() current call frame for f() What Happens when Procedure returns? • When procedure f() returns • Update LB (from dynamic link) • Update ST (to old LB) call frame LB Note, updating the ST implicitly “destroys” or “pops” the frame. ST

  49. time global Y Z Z 1 Y 2 call depth G G Y Z Y Accessing global/local variables Q: Is the stack frame for a procedure always on the same position on the stack? A: No, look at picture of procedure activation below. Imagine what the stack looks like at each point in time.

  50. Accessing global/local variables RECAP: We are still working under the assumption of a “flat” block structure How do we access global and local variables on the stack? The global frame is always at the same place in the stack. => Address global variables relative to SB A typical instruction to access a global variable: LOAD 4[SB] Frames are not always on the same position in the stack. Depends on the number of frames already on the stack. => Address local variables relative to LB A typical instruction to access a local variable: LOAD 3[LB]

More Related