Run Time Storage Managment The semantics of procedures in a language determines how names are bound to storage during allocation. Information needed during an execution of a procedure is kept in a block of storage called an activation record; storage for names local to the procedure also appears in the activation record. An activation record for a procedure has fields to hold parameters, results, machine-status information, local data, temporaries and the like. Since runtime allocation and de-allocation of activation records occurs as part of the procedure call and return sequences, we focus on the following threeaddress statements: 1. call 2. return 3. halt 4. action, a placeholder for other statements For example, the three-address code for procedures c and p in fig. 4 contains just these kinds of statements. The size and layout of activation records are communicated to the code generator via the information about names that is in the symbol table. For clarity, we show the layout in Fig. 4 rather than the form of the symbol-table entries. We assume that run-time memory is divided into areas for code, static data, and a stack. ADVERTISEMENT
STATIC ALLOCATION Consider the code needed to implement 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, and a GOTO transfers control to the target code for the called procedure: MOV #here +20, callee.static_area GOTO callee.code_area The attributes callee.statatic_area and callee.code_area are constants referring to the address of the activation record and the first instruction for the called procedure, respectively. The source #here+20 in the MOV instruction is the literal return address; it is the address of instruction following the GOTO instruction. The code for a procedure ends with a return to the calling procedure ends with a return to the calling procedure, except the first procedure has no caller, so its final instruction is HALT, which presumably returns control to the operating system. A return from procedure callee is implemented by GOTO *callee.static_area which transfers control to the address saved at the beginning of the activation record.
Example 1: The code in Fig. 5 is constructed from the procedures c and p in Fig. 4. We use the pseudo-instruction ACTION to implement the statement action, which represents three-address code that is not relevant for this discussion. We arbitrarily start the code for these procedures at addresses 100 and 200, respectively, and assume that each ACTION instruction takes 20 bytes. The activation records for the procedures are statically allocated starting at location 300 and 364, respectively. /*code for c*/ 100: ACTION1 120: MOV #140,364 /*save return address 140 */ 132: GOTO 200 /* call p */ 140: ACTION2 160: HALT …… /*code for p*/ 200: ACTION3 220: 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*/ fig 5. Target code for input in fig 4. The instructions starting at address 100 implement the statements action1 ; call p; action2; halt of the first procedure c. Execution therefore starts with the instruction ACTION1 at address 100. The MOV instruction at address 120 saves the return address 140 in the machine-status field, which is the first word in the activation record of p. The GOTO instruction at address 132 transfers control to the first instruction is the target code of the called procedure. Since 140 was saved at address 364 by the call sequence above, *364 represents 140 when the GOTO statement at address 220 is executed. Control therefore returns to address 140 and execution of procedure c resumes. 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 run time. In stack allocation, this position is usually stored in a register, so words in the activation record can be accessed as offsets from the value in this register. The indexed address mode of our target machine is convenient for this purpose. Relative addresses in an activation record can be taken as offsets from any known position in the activation record. For convenience, we shall use positive offsets by maintaining in a register SP a pointer to the beginning of the activation record on top of the stack. 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, thereby deallocating the activation record of the called procedure. ADVERTISEMENT
The code for the 1st procedure initializes the stack by setting SP to the start of the stack area in memory. MOV
#stackstart, SP
/*initialize the stack*/
code for the first procedure HALT
/*terminate execution*/
A procedure call sequence increments SP, saves the return address, and transfers control to the called procedure: ADD
#caller.recordsize, SP
MOV
#here+16, SP
GOTO
/* save return address*/
callee.code_area
The attribute caller.recordsize represents the size of an activation record, so the ADD instruction leaves SP pointing to the beginning of the next activation record. The source #here+16 in the MOV instruction is the address of the instruction following the GOTO; it is saved in the address pointed to by SP. The return sequence consists of two parts. The called procedure transfers control to the return address using GOTO
*0(SP)
/*return to caller*/
The reason for using *0(SP) in the GOTO instruction is that we need two levels of indirection: 0(SP) is the address of the first word in the activation record and *0(SP) is the return address saved there. The second part of the return sequence is in the caller, which decrements SP, thereby restoring SP to its previous value. That is, after the subtraction SP points to the beginning of the activation record of the caller: SUB #caller.recordsize, SP
Example 2 The program in fig .6 is a condensation of the three-address code for the Pascal program for reading and sorting integers. Procedure q is recursive, so more than one activation of q can be alive at the same time. *code for s*/ action1 call q action2 halt
/*code for p*/ action3 return /*code for q*/ action4 call p action5 call q action6 call q return fig.6 Three-address code to illustrate stack allocation
Suppose that the sizes of the activation records for procedures s, p, and q have been determined at compile time to be ssize, psize, and qsize, respectively. The first word in each activation record will hold a return address. We arbitrarily assume that the code for these procedures starts at addresses 100,200 and 300 respectively, and that the stack starts at 600. The target code for the program in fig. 6 is as follows: /*code for s*/ 100: 108: 128: 136: 144: 152: 160: 180:
MOV #600, SP ACTION1 ADD #ssize, SP MOV #152, *SP GOTO 300 SUB #ssize, SP ACTION2 HALT ………
200: ACTION3 220: GOTO *0(SP) ……… 300: 320: 328: 336: 344: 352: 372: 380: 388: 396: 404: 424: 432: 440: 448: 456: 600:
ACTION4 ADD #qsize, SP MOV #344, *SP GOTO 200 SUB #qsize, SP ACTION5 ADD #qsize, SP MOV #396, *SP GOTO 300 SUB #qsize, SP ACTION6 ADD #qsize, SP MOV #448, *SP GOTO 300 SUB #qsize, SP GOTO *0(SP) ………
/*initialize the stack*/ /*call sequence begins*/ /*push return address*/ /*call q*/ /*restore SP*/
/*code for p*/ /*return*/ /*code for q*/ /*conditional jump to 456*/ /*push return address*/ /*call p*/
/*push return address*/ /*call q*/
/*push return address*/ /*call q*/ /*return*/ /*stack starts here*/
We assume that ACTION4 contains a conditional jump to the address 456of the return sequence from q; otherwise, the recursive procedure q is condemned to call itself forever. In an example below, we consider an execution of the program in which the first call of q does not return immediately, but all subsequent calls do. If ssize, psize, and qsize are 20,40, and 60, respectively, then SP is initialized to 600, the starting address of the stack, by the first instruction at address 100. SP holds 620 just before control transfers from s to q, because ssize is 20. Subsequently, when q calls p, the instruction at address 320 increments SP to 680, where the activation record for p begins; SP reverts to 620 after control returns to q. If the next two recursive calls of q return immediately, the maximum value of SP during this execution is 680. However, the last stack location used is 739, since the activation record for q starting at location 680 extends for 60 bytes. RUN-TIME ADDRESSES FOR NAMES The storage allocation strategy and the layout of local data in an activation record for a procedure determine how the storage for names is accessed. If we assume that a name in a three-address statement is really a pointer to a symbol-table entry for the name; it makes the compiler more portable, since the front end need not be changed even if the compiler is moved to a different machine where a different run-time organization is needed. On the other hand, generating the specific sequence of access steps while generating intermediate code can be of significant advantage in an optimizing compiler, since it lets the optimizer take advantage of details it would not even see in the simple three-address statement. In either case, names must eventually be replaced by code to access storage locations. We thus consider some elaborations of the simple three-address statement x := 0. After the declarations in a procedure are processed, suppose the symbol-table entry for x contains a relative address 12 for x. First consider the case in which x is in a statically allocated area beginning at address static. Then the actual run-time address for x is static+12. Although, the compiler can eventually determine the value of static+12 at compile time, the position of the static area may not be known when intermediate code to access the name is generated. In that case, it makes sense to generate three-address code to “compute” static+12, with the understanding that this computation will be carried out during the codegeneration phase, or possibly by the loader, before the program runs. The assignment x := 0 then translates into static[12] := 0 If the static area starts at address 100, the target code for this statement is
MOV #0, 112
On the other hand, suppose our language is one like Pascal and that a display is used to access non-local names. Suppose also that the display is kept in registers, and that x is local to an active procedure whose display pointer is in register R3. Then we may translate the copy x := 0 into the three-address statements t1 := 12+R3 *t1 := 0
in which t1 contains the address of x. This sequence can be implemented by the single machine instruction MOV #0, 12 (R3)
The value in R3 cannot be determined at compile time.
The Darjeeling Bus
It was raining heavily. It was the sort of weather that was normal during the monsoon in Darjeeling. Naina looked out of the rain splattered window, over the lush greenery beneath. The little house on top of the hill had withstood rain and sun for over a hundred years. She ran back into the kitchen to take the teapot off the fire. She sat down in the porch, leaning against the mud wall. She liked the smell when it rained. She looked fondly at the letter she had been carrying in her pocket all day long. It was from her mother for her to open on her 18th birthday. Rohan lived in Kolkata. He was always special. He stood first in class though he hardly ever studied. He had a whole cupboard of medals that he won playing sports at school. His elder brother thought he was the best brother in the world. He loved people and they loved him. More than anything, he loved his childhood friend, Maya. They had been together since they first became friends in pre-school. He felt she was more beautiful than any girl he had ever seen. He got admitted to the most prestigious college in his country. He knew what he was going to do. Get a degree, get a job, marry Maya and have lots of kids. But no person is safe from karma. Life is all about a delicate balance, it is like the art of the tight rope walker. A man can have a thousand extra-ordinary qualities but an imperfection can destroy him. In Rohan's case, the imperfection was that he never lost. He knew not that a man could lose and lose heavily but still come back from it. And so it happened the day three years before today. His shower was not working and Rohan had to go offer his prayers to his favorite God at the temple. He was leaving for college today and there was not much time as he had a train to catch in two hours. Rohan grabbed a towel and ran into his brother's bathroom. As he dried his hair after the bath, he heard the bedroom door open.
He heard whispering voices and he thought he heard a girl's voice. He knew his brother did not have a girlfriend and he was curious. He kept listening hard as he heard his brother say "Rohan's at the temple, no one's home. Come on!" He soon heard the sounds he wanted to hear, they were having sex! Rohan heard the girl moaning softly. Rohan was still a virgin and he felt his cock starting to get hard. Out of his love for Maya, he had never pressured her to have sex. Rohan opened the bathroom door just a little. He saw his brother's cock sliding in and out of the girl. His brother was slamming her doggy style. Through the crack, he could only see the girl from the waist down. He wanted to catch a glimpse of her breasts but he was afraid he would be caught. His brother started grunting and working his hips harder. Rohan let his hands wrap around his cock. He closed his eyes and jerked off to the sound of the hip slamming the girl's ass. Rohan exploded his semen all over the wall. He cleaned up and was now anxious to leave the bathroom lest he miss his train. He peeped out of the door and saw that his brother was now on top. His face was buried in her heavy breasts and so Rohan tip toed his way towards the door. He stepped out and just as he was about to close the door behind him, his brother rolled onto his back and pushed the girl on top. Rohan looked as the girl sat up on his cock, working her hips over his brother's cock and with her left hand, pushed back the long hair covering her face. There are some moments in life that change a man's fate forever and this was one of those. It was Maya. Her beautiful face was engulfed in lust, her eyes closed and her lips moaning softly in harmony with my brother's penetrations. Rohan went blank. His legs seemed to wobble and he fainted. It had been three years since the day and three years since Rohan had gone home. He lied to his parents that he was busy with some research project. His unsuspecting and loving parents visited him regularly during the three years. The last time they visited, they had good news for him. His brother had revealed that he was going to marry Maya and they thought having Maya as their daughter-in-law was a gift from God. That day Rohan felt God had slammed him face down into a black hole and he never had the courage to look up again. Every day since then had been filled with thoughts of suicide. Rohan liked cigarettes. He liked them more than any other thing in the world now. He hardly slept. His dreams took him back to the same scene that haunted him during the day. It was the beautiful face of Maya. It was the day of graduation. Rohan had graduated with a GPA of 1.5. It was the minimum required to graduate and he felt he owed his parents a degree to show them that they had done well their job of raising him. He saw his parents clapping hard when they called out Rohan's name. After the ceremony, Rohan packed his bags and put them in the back of the his dad's van. His brother's marriage was in 10 days and there was lot of work to be done for the elaborate wedding ceremony. Rohan went back to his college room, picked up the backsack and got into his friend's car. He had left a goodbye note on his desk, he knew his parents would come up to his room searching for him if did not show up in 10 minutes. Rohan got aboard a bus. The tickets are to be paid for after you get onto the bus in India, depending on how far you travel. Rohan decided to end his life, go to a solitary place and end the torment that seemed to be eat his brain. Rohan asked the bus conductor how far the bus was going. It was going to Darjeeling. Rohan liked Darjeeling. The scenery is absolutely incredible. On a clear day Kanchanjunga, the third highest peak in the world
watches over the town and you can see villages on other peaks across the multiple valleys. Birds don’t look like they fly as much as glide over the mountain tops. On most days however the clouds roll in and wrap the town in a fine mist that blows through the streets. Rohan smiled. This was first time in 4 years he had a positive picture of something. Maybe it was the fact that he was going to jump off the third highest mountain in the world. He liked the idea. The bus was full. He felt happy the seat next to him was empty. He did not want to sit next to someone. The bus stopped. He heard the conductor talking to a woman. She was asking him if there was space. The conductor spotted the empty seat beside me. She got aboard. She seemed to be in her late thirties and looked very beautiful. Her long hair was tied up in a single ponytail. Others on the bus were looking at her with more lustful thoughts. She wore a blue saree and had a very desirable figure. Her full breasts were noticeable and her shapely hips seemed to interest the men. As she bent forward to push her bag under the seat, her palloo revealed a cleavage that seemed to awake his groin after a four year absence. The smell of a light floral perfume filled Rohan's senses as she sat down. She had light green eyes that seemed to look deep. It seemed to me like the world was suddenly lighter as she turned towards me with a beautiful smile. She had luscious lips, lips so deep and pink it made you want to touch them just to see if they were real. She asked me, "Are you going to Darjeeling?". I nodded my head, unable yet to speak any words. She continued "I lived there for 30 years since the day I was born. Its a wonderful place for the mind to heal, for life grows in every part of its soil. If its your first time visiting, you will know it for yourself". As I looked at more closely, I noticed she had eyes that showed sadness. I immediately knew why I noticed that. I had looked at those eyes for four years in the mirror. It was the same sadness. She seemed to nod in approval as she smiled knowingly. I was suddenly close to tears, grief overwhelmed me as I looked at her knowing someone else has had the misfortune of knowing the pain that was inside me. She caught my hand, and slid her slender, soft fingers into my palm as she pressed my hand in support. I asked her "Who are you?". My voice was almost a whisper. I am not sure she even heard it over the sounds of the bus engine struggling over the steep road. She smiled replying in the same barely audible voice in which I spoke "Does it matter? All that matters is that we were destined to meet. I can see you have lost faith in life the same way I did years ago. I never found someone to help me but it looks like you have me." She looked to see if anyone was watching. Everyone was busy either sleeping or looking out of their windows at the beauty of the countryside. She leaned over and kissed me on my cheek. I felt her warm breath. An electric charge seemed to jolt all my hair into standing up. It was a wonderful feeling, a time when your mind is as peaceful as a saint in meditating in the himalayas. Her lips seemed to press into my skin, transferring a pleasure I had never known. As she rested her head back against the seat, her moist lips had left an impression that made me forget all my pain temporarily. I looked at her astonished not believing what she just bestowed on me, I was now even having second thoughts about the jump off the mountain. She laughed quietly, it sounded like the tinkle of small bells in a gentle wind. She said "I know what you are thinking. Don't think so much. You are much too young to comprehend life and question fate. Just enjoy the moment. I have much to teach you on our short journey together." I looked out of the window, the wind from the open window had the smell of fresh leaves and rain. My pain returned in a while, Maya had
never left me even during the day. She was always there, her smile, those tight breasts with her large pointed nipples and her womanly hips. And below her was my brother thrusting into her, implanting her with his seed. I jumped out of the dream as suddenly as I got into it. Little drops of sweat were dripping down my forehead as I thought of the marriage. 9 days to go, 9 days before I jumped down Kanchenjunga. I thought it as perfect harmony, a rhythm. Their marriage and my death. She seemed to sense my thoughts. She looked with worry at me. She asked "Why? What was so bad that you have chosen this way?". I was astonished, she seemed to not just sense my thoughts but read them perfectly. I told her my story, my life as it happened. I told her how I jerked off to my own Maya being fucked by my brother without even knowing it. I told her how my own brother had for his own happiness had broken all holy laws to seduce Maya. I told her how I felt God had slammed me face down for being a good son, a good brother and for trying to lead a good life. I was in tears, I was crying silently as the bus rolled along in the night. She brought me to her bosom, I let her guide my head onto her full breasts and rest on them. I saw my tear falling on her chest and rolling into her deep cleavage. She told me "You are not normal. There are few innocent, good persons left in today's world. The world is full of corrupt and selfish individuals who are driven by jealousy, envy and greed. You know I was so much like you; naive, innocent, I believed everyone was good and the world was an holy place. It is not. You have potential in you, the same potential that are possessed by those working for God. I will teach you how to live in this world. I will tell you how to administer justice. The weak are always afraid of the righteous. They try to hurt the the path of the righteous man, just like you have been hurt by your morally corrupt and coward of a brother. But you do not have the strength yet. For now, you are in no shape to even understand my words. I have to heal you first, both mind and body." I had no idea when she said the last sentence that my healing would include making love to her non-stop all through the journey. She whispered into my ear and said "Close your eyes and follow your heart." She covered us with a blanket as it was getting cold. I found it was for a different reason when she guided my hands to her breasts. She looked at me with quivering lips and said "Go ahead, please me." I let my hand feel her breast. It was the first time in my life I was touching a woman's boobs. It felt wonderful to cup her breast. I squeezed it softly. She left out a soft moan and quickly opened her eyes to look around. But everyone was fast asleep and the old bus was making way too much noise for anyone to even notice. She leaned against my chest, her back now resting against me. Her hair smelt of wild orchids in the spring, the moonlight gave her hair a silver glow as it flowed down towards her slender waist. I wrapped my right hand across her delicate frame as I traced my finger along her soft tummy. Her skin felt like velvet. She looked fairer than the moon, like an angel in a dream. My finger finally found solace resting against her navel, teasing it and pinching her baby soft skin. This time she moaned louder. Her sensual voice seemed to awaken my cock as it strained against my pants roaring to let it free. My left hand was still busy playing with her breasts. I looked around and slowly loosened the blanket so it fell a little free on one side exposing her right breast completely and part of her left one. She had a magnificent set of tits. They were probably a D-cup, round and full, slightly sagging though that only added to elegance of the shape. I traced the contour of breast with my
finger, it was shaped like a mango. Steep at the top, my finger gradually traced gentler slope and over the top of her puffy nipple. I waited a moment like an explorer that just conquered a mountain. I was the conquerer of her breasts. I pushed her hard nipple into her breast. She was breathing harder now against my neck, making silent noises in deep pleasure. I continued my journey south of her nipple. I watched as the nipple came back out slowly, standing back hard over her breast triumphantly. Her breast had developed into more fullness near the bottom, giving her that lovely shape like a ripe mango ready to devour. She moved her hand into my hair and lowered my head onto her erect nipple. If ever there was heaven on earth, I felt this was it as I lowered my lips onto her breast. Like a hungry baby, I descended on her breast sucking her nipple in short bursts. Her hand pulled at my hair excited, she was breathing fast, eyes closed deep in pleasure. My tongue rolled her hard nipple over. It was a battle between two equals, the meeting of two entities that gave pleasure in mutual company. Her nippled seemed to pierce my tongue as I tried to smother it into submission. And so it seemed like time was boundless and life eternal as we both passed into deep sleep.