1 / 13

Languages and Compilers (SProg og Oversættere)

Languages and Compilers (SProg og Oversættere). Run-time organization. Run-time organization. Storage allocation strategies: static vs. dynamic Activation records (sometimes called frames) Parameter parsing. Runtime organization.

hedwig
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) Run-time organization

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

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

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

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

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

  7. Static Arrays 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’ 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] ‘ ’

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

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

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

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

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

  13. Arguments: by value or by reference Some programming languages allow to two kinds of function/procedure parameters. Example: in Triangle (similar in Pascal) Constant/Value parameter Var/reference 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

More Related