Variables & Storage

download Variables & Storage

of 34

Transcript of Variables & Storage

  • 8/12/2019 Variables & Storage

    1/34

    3-1

    Variables and Storage

    A simple storage model.

    Simple and composite variables.

    Copy semantics vsreference semantics.

    Lifetime.

  • 8/12/2019 Variables & Storage

    2/34

    3-2

    Variables and Storage (contd)

    Commands.

    Expressions with side effects.

    Implementation notes.

    Storage for global and local variables

  • 8/12/2019 Variables & Storage

    3/34

    3-3

    An abstract model of storage (1)

    In functional/logic PLs (as in mathematics), a variable

    stands for a fixed but unknown value.

    In imperative/OO PLs, a variableis a container for a

    value, which may be inspected and updated as often asdesired.

    Such a variable can be used to model a real-world object

    whose state changes over time.

  • 8/12/2019 Variables & Storage

    4/34

    3-4

    An abstract model of storage (2)

    To understand such variables,assume a simple abstract model ofstorage:

    A storeis a collection of storage cells.

    Each storage cell has a unique address. Each storage cell is either allocatedor

    unallocated.

    Each allocated storage cell containseither a simple value or undefined.

    true

    3.14

    7

    ?

    unallocated

    unallocated

    cells

    allocated

    allocated

    allocated

    cells

    undefined

  • 8/12/2019 Variables & Storage

    5/34

    3-5

    Simple vscomposite variables

    A simple valueis one that can be stored in a single storagecell (typically a primitive value or a pointer).

    A simple variableoccupies a single allocated storage cell.

    A composite variableoccupies a group of allocatedstorage cells.

  • 8/12/2019 Variables & Storage

    6/34

    3-6

    Simple variables

    When a simple variable is declared, a storage cell is

    allocated for it.

    Assignment to the simple variable updates that storage cell.

    At the end of the block, that storage cell is deallocated.

    Animation (Ada):

    declare

    n: Integer;begin

    n := 0;

    n := n+1;

    end;

    n ?

    0n

    1n

  • 8/12/2019 Variables & Storage

    7/34

    3-7

    Composite variables (1)

    A variable of a composite type has the same structure as a

    value of that type. For instance:

    A record variable is a tuple of component variables.

    An array variable is a mapping from an index range to a group of

    component variables.

    The component variables can be inspected and updated

    either totally or selectively.

  • 8/12/2019 Variables & Storage

    8/34

    3-8

    Composite variables (2)

    Animation (Ada):

    declare

    typeDate is

    record

    y: Year_Number;m: Month;

    d: Day_Number;

    endrecord;

    xmas, today: Date;

    beginxmas.d := 25;

    xmas.m := dec;

    xmas.y := 2004;

    today := xmas;

    end;

    ?

    ?

    ?

    ?

    ?

    ?xmas today

    ?

    25

    ?

    ?

    ?

    ?xmas today

    dec

    25

    ?

    ?

    ?

    ?xmas today

    dec

    25

    2004

    ?

    ?

    ?xmas today

    dec

    25

    2004

    ?

    ?

    ?xmas today

    dec

    25

    2004

    dec

    25

    2004xmas today

  • 8/12/2019 Variables & Storage

    9/34

    3-9

    Total vsselective update

    Total updateof a composite variable means updating it

    with a new (composite) value in a single step:

    today := xmas;

    Selective updateof a composite variable means updating asingle component:

    today.y := 2004;

  • 8/12/2019 Variables & Storage

    10/34

    3-10

    Copy semantics vsreference semantics

    What exactly happens when a composite value is assigned

    to a variable of the same type?

    Copy semantics: All components of the composite value

    are copied into the corresponding components of thecomposite variable.

    Reference semantics: The composite variable is made to

    contain a pointer (or reference) to the composite value.

    C and Ada adopt copy semantics.

    Java adopts copy semantics for primitive values, but

    reference semantics for objects.

  • 8/12/2019 Variables & Storage

    11/34

    3-11

    Example: Ada copy semantics (1)

    Declarations:

    typeDate is

    record

    y: Year_Number;

    m: Month;d: Day_Number;

    endrecord;

    dateA: Date := (2004, jan, 1);

    dateB: Date;

    Effect of copy semantics:

    dateB := dateA;

    dateB.y := 2005;jan

    1

    2004dateA

    ?

    ?

    ?dateB

    jan

    1

    2004dateA

    jan

    1

    2004dateB

    jan

    1

    2004dateA

    jan

    1

    2005dateB

  • 8/12/2019 Variables & Storage

    12/34

    3-12

    Example: Java reference semantics (1)

    Declarations:

    classDate {

    inty, m, d;

    publicDate (inty, intm, intd) { }

    }Date dateR = newDate(2004, 1, 1);

    Date dateS = newDate(2004, 12, 25);

    Effect of reference semantics:

    dateS = dateR;

    1

    1

    2004

    dateR

    12

    25

    2004

    dateS

    1

    1

    2004

    dateR dateS

    1

    1

    2004

    dateR dateS

  • 8/12/2019 Variables & Storage

    13/34

    3-13

    Example: Java reference semantics (2)

    We can achieve the effectof copy semantics in Java by

    cloning:

    Date dateR = newDate(2004, 4, 1);

    dateT = dateR.clone();

  • 8/12/2019 Variables & Storage

    14/34

    3-14

    Lifetime (1)

    Every variable is created(or allocated) at some definite

    time, and destroyed(or deallocated) at some later time

    when it is no longer needed.

    A variables lifetimeis the interval between its creationand destruction.

    A variable occupies storage cells only during its lifetime.

    When the variable is destroyed, the storage cells that it

    occupied may be deallocated (and subsequently allocatedfor some other purpose).

  • 8/12/2019 Variables & Storage

    15/34

    3-15

    Lifetime (2)

    A global variables lifetime is the programs run-time. It

    is created by a global declaration.

    A local variables lifetime is an activation of a block. It is

    created by a declaration within that block, and destroyedon exit from that block.

  • 8/12/2019 Variables & Storage

    16/34

    3-16

    Commands

    A command(orstatement) is a PL construct that will beexecutedto update variables.

    Commands are characteristic of imperative and OO (butnot functional) PLs.

    Forms of commands:

    skips

    assignments

    procedure calls

    sequential commands

    conditional commands

    iterative commands.

  • 8/12/2019 Variables & Storage

    17/34

    3-17

    Skips

    A skipis a command with no effect.

    Typical forms:

    ; in C and Java

    null; in Ada.

    Skips are useful mainly within conditional commands.

  • 8/12/2019 Variables & Storage

    18/34

    3-18

    Assignments

    An assignmentstores a value in a variable.

    Single assignment:

    V=E; in C and Java

    V:=E; in Ada

    the value of expressionEis stored in variable V.

    Multiple assignment:

    V1== V

    n=

    E;

    in C and Java

    the value ofEis stored in each of V1,, Vn.

  • 8/12/2019 Variables & Storage

    19/34

    3-19

    Procedure calls

    A procedure callachieves its effect by applying aprocedure to some arguments.

    Typical form:

    P(E1, , En);

    HerePdetermines the procedure to be applied, andE1,,Enare evaluated to determine the arguments. Eachargument may be either a value or (sometimes) a referenceto a variable.

    The net effect of the procedure call is to update variables.The procedure achieves this effect by updating variables

    passed by reference, and/or by updating global variables.(But updating its local variables has no neteffect.)

  • 8/12/2019 Variables & Storage

    20/34

    3-20

    Sequential commands

    Sequential, conditional, and iterative commands (found in

    all imperative/OO PLs) are ways of composing commands

    to achieve different control flows. Control flow matters

    because commands update variables, so the order in which

    they are executed makes a difference.

    A sequential commandspecifies that two (or more)

    commands are to be executed in sequence. Typical form:

    C1C2

    command C1is executed before command C2.

  • 8/12/2019 Variables & Storage

    21/34

    3-21

    Conditional commands

    A conditional commandchooses one of its subcommands

    to execute, depending on a condition.

    An if-command chooses from twosubcommands, using a

    boolean condition. A case-command chooses fromseveralsubcommands.

  • 8/12/2019 Variables & Storage

    22/34

    3-22

    If-commands (1)

    Typical forms (Ada and C/Java, respectively):

    ifEthen if(E)

    C1 C1

    else else

    C2 C

    2endif;

    ifEyields true, C1is executed; otherwise C2is executed.

    Emust be of

    type Boolean

  • 8/12/2019 Variables & Storage

    23/34

    3-23

    If-commands (2)

    Generalisation to multiple conditions (in Ada):

    ifE1then

    C1

    elsifE2then

    C2

    elsifEnthen

    Cn

    else

    C0

    endif;

    ifE1, ,Ei-1all yieldfalsebutEiyields true, then Ciis

    executed; otherwise C0is executed.

    E1, ,Enmust be

    of type Boolean

  • 8/12/2019 Variables & Storage

    24/34

    3-24

    Case-commands (1)

    In Ada:

    caseEis

    whenv1=>

    C1

    whenvn=>

    Cn

    whenothers=>

    C0

    endcase;

    if the value ofEequals some vi, then Ciis executed;

    otherwise C0is executed.

    v1, , vnmust be distinct

    values of that type

    Emust be of some primitive

    type other than Float

  • 8/12/2019 Variables & Storage

    25/34

    3-25

    Case-commands (2)

    In C and Java:

    switch(E) {

    casev1:

    C1

    casevn:

    Cn

    default:

    C0

    }

    if the value ofEequals some vi, then Ci, , Cn, C0are

    allexecuted; otherwise only C0is executed.

    v1, , vnmust be integers,not necessarily distinct

    Emust be of integer type

  • 8/12/2019 Variables & Storage

    26/34

    3-26

    Example: Ada case-command

    Code:

    today: Date;

    casetoday.m is

    whenjan => put("JAN");

    whenfeb => put("FEB");

    whennov => put("NOV");

    whendec => put("DEC");

    endcase;

  • 8/12/2019 Variables & Storage

    27/34

    3-27

    Example: Java switch-command

    breaks

    are

    essential

  • 8/12/2019 Variables & Storage

    28/34

    3-28

    Example: Java switch-command (contd)

    breaks

    are

    essential

  • 8/12/2019 Variables & Storage

    29/34

    3-29

    Iterative commands

    An iterative command(or loop) repeatedly executes a

    subcommand, which is called the loop body.

    Each execution of the loop body is called an iteration.

    Classification of iterative commands:

    Indefinite iteration: the number of iterations are not

    predetermined.

    Definite iteration: the number of iterations are predetermined.

  • 8/12/2019 Variables & Storage

    30/34

    3-30

    Indefinite iteration

    Indefinite iteration is most commonly supported by the

    while-command. Typical forms (Ada and C/Java):

    whileEloop while(E)

    C C

    endloop;

  • 8/12/2019 Variables & Storage

    31/34

    3-31

    Rmust be of some primitive

    type other than Float

    Definite iteration

    Definite iteration is characterized by a control sequence, a

    predetermined sequence of values that are successively

    assigned (or bound) to a control variable.

    Ada for-command:

    forVinRloop

    C

    endloop;

    the control sequence consists of all values in the rangeR,

    in ascending order.

  • 8/12/2019 Variables & Storage

    32/34

    3-32

    Implementation notes

    Each variable occupies storage space throughout its

    lifetime. That storage space must be allocated at the start of

    the variables lifetime (or before), and deallocated at the

    end of the variables lifetime (or later).

    The amount of storage space occupied by each variable

    depends on its type.

    Assume that the PL is statically typed: all variables types

    are declared explicitly, or the compiler can infer them.

  • 8/12/2019 Variables & Storage

    33/34

    3-33

    Storage for global and local variables (1)

    A global variables lifetime is the programs entire run-

    time. So the compiler can allocate a fixed storage space to

    each global variable.

    A local variables lifetime is an activation of the block in

    which the variable is declared. The lifetimes of local

    variables are nested. So the compiler allocates storage

    space to local variables on a stack.

  • 8/12/2019 Variables & Storage

    34/34

    3-34

    Storage for global and local variables (2)

    At any given time, the stack contains several activation

    frames.

    Each activation frame contains

    enough space for the local variables

    of a particular procedure.

    housekeepingdata

    local variableslocal variableslocal variables

    An activation frame is:

    pushed on to the stack when a procedure is called

    popped off the stack when the procedure returns.