runtime storage management

14
RUNTIME STORAGE MANAGEMENT Praseetha v v MCA B5

Upload: vsunny488

Post on 11-Apr-2015

323 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RUNTIME STORAGE MANAGEMENT

RUNTIME STORAGE MANAGEMENT

Praseetha v v MCA B5

Page 2: RUNTIME STORAGE MANAGEMENT

Storage Allocation Strategies

Two standard storage-allocation strategies are

• Static allocation• Stack allocation

Page 3: RUNTIME STORAGE MANAGEMENT

• In static allocation , the position of an activation record in memory is fixed at compile time.

• In stack allocation, a new activation record is pushed on to the stack for each execution of a procedure.

Page 4: RUNTIME STORAGE MANAGEMENT

• Activation record for a procedure has fields to hold

parameters results machine-status information local data temporaries etc..

Page 5: RUNTIME STORAGE MANAGEMENT

• Since runtime allocation and deallocation of activation records occurs as part of the procedure call and return sequences we focus on the following three address statements.

1. call2. return3. halt4. Action, a place holder for other statements

Page 6: RUNTIME STORAGE MANAGEMENT

Static Allocation

• A call statement in the intermediate code is implemented by a sequence of two target-machine instructions.

• A MOV instruction saves the return address• GOTO transfers control to the target code for

the called procedure. MOV #here+20, callee.static_area GOTO callee.code_area

Page 7: RUNTIME STORAGE MANAGEMENT

The attributes • callee.static_area : Referring to address of the

activation record. • Callee.code_area : Referring to first instruction

for the called procedure.• #here+20 : it is the address of the instruction

following the GOTO statement.• GOTO *callee.static_area : Transfers control to

the address saved at the beginning of the activation record.

Page 8: RUNTIME STORAGE MANAGEMENT

Input to a code generation

/*Code for c */

action1call paction2halt

/* code for p */

action3 return

arr

i

j

return address

buf

n

THREEE ADDRESS CODE

ACTIVATION RECORD For c(64 bytes)

ACTIVATION RECORD For p(88 bytes)

Return address0:

8:

56:

60:

0:

4:

84:

Page 9: RUNTIME STORAGE MANAGEMENT

/* CODE FOR C */100: ACTION1120: MOVE #140 , 364 /* save return address 140*/132: GOTO 200 /* call p */140: ACTION2160: HALT …… /* C0DE FOR P */200: ACTION3220: GOTO *364 /* return to address saved in location 364 */ …….. /* 300-363 hold activation record for c*/300: /*return address*/304: /*local data for c*/ ……………….. /* 364-451 hold activation record for p */364: /*return address*/368: /* local data for p */

Page 10: RUNTIME STORAGE MANAGEMENT

Stack Allocation

• Static Allocation can become stack allocation by using relative addresses for storage in activation records.

• The position of the record for an activation of a procedure is not known until runtime.

• In stack allocation this position is usually stored in a register.

• When a procedure call occurs, the calling procedure increments SP and transfers control to the called procedure.

• After control returns to the caller, it decrements SP, there by deallocating the activation record of the called procedure.

Page 11: RUNTIME STORAGE MANAGEMENT

• The code for the first procedure initializes the stack by setting SP to the start of the stack area in memory.

MOV #stackstart,SP code for the first procedure HALT• The procedure call sequents increments SP,

save the return address, and transfers control to the called procedure.

ADD #caller,recordsize, SP MOV #here+16, *SP GOTO callee.code_area

Page 12: RUNTIME STORAGE MANAGEMENT

Three AddressCode

/* code for s */ Action1Call qAction2halt

/* code for p */Action3return

/* code for q */Action4Call pAction5Call qAction6Call qreturn

/* CODE FOR S */

100: MOVE #600,SP /* initialize the stack */108: ACTION1128: ADD #SSIZE,SP /*call sequence begins*/136: MOV #152, *SP /* push return address */144: GOTO 300 /* call q */152: SUB #sSize, SP /* restore SP */160: ACTION2180: HALT ……….. /* CODE FOR P */200: ACTION3 220: GOTO *0(SP) /* return */ ………… /* code for q */300: ACTION4 /* conditional jump to 456*/320: ADD #qsize,SP328: MOVE #344, *SP /* push return address */

Page 13: RUNTIME STORAGE MANAGEMENT

336: GOTO 200 /* call p */344: SUB #qsize, SP352: ACTION5372: ADD #qsize, SP380: MOV #396, *sp /* push return address */388: GOTO 300 /* call q */396: SUB #qsize, SP404: ACTION6424: ADD #qsize, SP432: MOV #448, *SP440: GOTO 300 /*call q */448: SUB #qsize,SP456: GOTO *0(SP) /* return */ ……….. 600: /* stack starts here */

Page 14: RUNTIME STORAGE MANAGEMENT