Languages and Compilers SProg og Oversttere Runtime organization

  • Slides: 13
Download presentation
Languages and Compilers (SProg og Oversættere) Run-time organization 1

Languages and Compilers (SProg og Oversættere) Run-time organization 1

Run-time organization – Storage allocation strategies: static vs. dynamic – Activation records (sometimes called

Run-time organization – Storage allocation strategies: static vs. dynamic – Activation records (sometimes called frames) – Parameter parsing 2

Runtime organization • Data Representation: how to represent values of the source language on

Runtime organization • Data Representation: how to represent values of the source language on the target machine. • Primitives, arrays, structures, unions, pointers • Storage Allocation: How to organize storage for variables (considering different lifetimes of global, local and heap variables) • Activation records, static links • Routines: How to implement procedures, functions (and how to pass their parameters and return values) • Value vs. reference, closures, recursion 3

Data Representation Important issues in data representation: • constant-size representation: The representation of all

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

Indirect Representation Q: What reasons could there be for choosing indirect representations? To make

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

Arrays An array is a composite data type, an array value consists of multiple

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? 6

Static Arrays Example: type Name = array 6 of Char; var me: Name; var

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

Static Storage Allocation Example: Global variables in Triangle let type Date = record y:

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

Stack Storage Allocation Now we will look at allocation of local variables Example: When

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 9

RECAP: TAM Frame Layout Summary arguments LB ST dynamic link static link return address

RECAP: TAM Frame Layout Summary arguments LB ST dynamic link static link return address local variables and intermediate results Arguments for current procedure they were put here by the caller. Link data Local data, grows and shrinks during execution. 10

Accessing global/local variables Q: Is the stack frame for a procedure always on the

Accessing global/local variables Q: Is the stack frame for a procedure always on the same position on the stack? time global 1 Y Z Y 2 call depth Z G G Y Z Y 11

Accessing non-local variables proc P() SB proc Q(). . . proc S(). . .

Accessing non-local variables proc P() SB proc Q(). . . proc S(). . . globals L 1 time G Dynamic L. Static Link P P() frame S() frame LB S Q() frame Q ST 12

Arguments: by value or by reference Some programming languages allow to two kinds of

Arguments: by value or by reference Some programming languages allow to two kinds of function/procedure parameters. Example: in Triangle (similar in Pascal) Var/reference parameter Constant/Value parameter let proc S(var n: Integer, i: Integer) ~ n: =n+i; var today: record y: integer, m: Integer, d: Integer end; in begin b : = {y~2002, m ~ 2, d ~ 22}; S(var b. m, 6); end 13