Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
The University of Adelaide, School of Computer Science
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
Run time vs. Compile time
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See P&H 2.8 and 2.12.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 4: MIPS Instruction Set
CSC 8505 Compiler Construction Runtime Environments.
Compiler Construction Code Generation Activation Records
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Storage Allocation Mechanisms
Lecture 7 Macro Review Stack Frames
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Run-Time Environments Chapter 7
COMPILERS Activation Records
The Stack.
Run-Time Storage Organization
Run-Time Storage Organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Activation Records and Function Calls
Prof. Hakim Weatherspoon
Stack Frame Linkage.
Understanding Program Address Space
Lecture 5: Procedure Calls
10/4: Lecture Topics Overflow and underflow Logical operations
UNIT V Run Time Environments.
Program and memory layout
Program and memory layout
Runtime Environments What is in the memory?.
Program Compilation and Execution
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Runtime Stack Activation record for hanoi;
Presentation transcript:

Program Compilation and Execution

Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division of memory into stack, heap Draw logical division of memory into stack, heap Compare and contrast stack and heap Compare and contrast stack and heap List variables stored in heap; stack; neither List variables stored in heap; stack; neither Describe 2 different function calling conventions Describe 2 different function calling conventions

Logical Memory Layout Stack Heap DYNAMICDYNAMIC Static Space Currently not in use

Dice.c int NUM = ; main(){ int i, roll, *counts; int i, roll, *counts; counts = (int *) malloc (13 * sizeof(int)); counts = (int *) malloc (13 * sizeof(int)); for (i = 0; i < 13; i++) counts[i] = 0; for (i = 0; i < 13; i++) counts[i] = 0; for ( i = 0; i < NUM; i++) for ( i = 0; i < NUM; i++) roll = rand() % 6 + rand() %6 + 2 roll = rand() % 6 + rand() %6 + 2 counts[roll]++; counts[roll]++; for (i = 2; i < 13; i++) for (i = 2; i < 13; i++) printf(“There were %d rolls of %d\n”, printf(“There were %d rolls of %d\n”, counts[i],i); counts[i],i);}

Where Are the Variables? Stack Heap DYNAMICDYNAMIC Space for 13 ints – pointed to by counts i; roll; counts Static Space Num “There were %d rolls of %d\n” Currently not in use

Runtime Stack Activation record for main Activation Record for Main Activation record for Hanoi; Activation record for Hanoi; Activation record for Hanoi; Activation record for Hanoi;

Support for Function Calls Call usually simple, but setup not Call usually simple, but setup not call printf call printf what comes before and after call? what comes before and after call? Support recursion? Support recursion? No --- simple, use static space No --- simple, use static space Yes --- runtime stack of activation record(s) Yes --- runtime stack of activation record(s) Return value(s) Return value(s)

Activation Record Information Parameters Parameters Locals Locals Compiler Temporaries Compiler Temporaries Register Save/Restore Register Save/Restore Previous Activation Record Previous Activation Record Previous Scope Previous Scope Anything else? Anything else?

A Function Call Convention fp and sp defined by assembler fp and sp defined by assembler Caller function (in order) Caller function (in order) push parameters on stack, in reverse push parameters on stack, in reverse push fp push fp fp = sp - 4 fp = sp - 4 call callee call callee fp = 0 (fp) fp = 0 (fp) sp = sp – (size of arguments + 4) sp = sp – (size of arguments + 4) Callee function (in order) Callee function (in order) sp = sp + size of function sp = sp + size of function execute function execute function sp = sp – size of function sp = sp – size of function return return

An Activation Record -12(fp) – parameter 3 Reg save … Temps … 8(fp) – local 2 4(fp) – local 1 0(fp) – old fp -4(fp) – parameter 1 -8(fp) – parameter 2 fp Reg save … sp Next stack location – beyond the stack

Function Call Convention (2) Alpha conventions Alpha conventions Parameters passed in registers, r16 - r24 or d16 - d24 Parameters passed in registers, r16 - r24 or d16 - d24 Return in r0 (or d0) Return in r0 (or d0) Registers r1 - r8, d1 - d8 are NOT saved across function call invocations (caller save) Registers r1 - r8, d1 - d8 are NOT saved across function call invocations (caller save) Registers r25 - r31 and d25 - d31 ARE saved across function call invocations (callee save) Registers r25 - r31 and d25 - d31 ARE saved across function call invocations (callee save)