Topic 2b ISA Support for High-Level Languages

Slides:



Advertisements
Similar presentations
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.
Advertisements

The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
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
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
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
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Spr 2015, Feb 9... ELEC / Lecture 4 1 ELEC / Computer Architecture and Design Spring 2015 Compiling and Executing Programs.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Lecture 4: MIPS Instruction Set
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
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.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Instructions - Type and Format
CSCI206 - Computer Organization & Programming
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Stack Frame Linkage.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Lecture 5: Procedure Calls
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Topic2d High-Level languages and System Software (Toolchain)
Procedures and Calling Conventions
Computer Architecture Procedure Calls
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Presentation transcript:

Topic 2b ISA Support for High-Level Languages Introduction to Computer Systems Engineering (CPEG 323) 9/14/2019 cpeg323-05F\Topic2b-323

What are Processors For? Most processors run code written in a high-level language, and many are used in multi-user environments. It is therefore necessary to understand: How high-level languages are represented in machine language The operation of compilers, linkers, loaders and debuggers What ISA features support the needs of high-level languages What ISA features support the needs of the operating system 9/14/2019 cpeg323-05F\Topic2b-323

Overview of Compiler Source program Machine code Example: INPUT PROGRAM: int foo( ) { int x=0; return (x + x) ; } 9/14/2019 cpeg323-05F\Topic2b-323

Output Assembly Code (unoptimized) foo: sub $sp, $sp, 8 % Push stack frame sw $fp, 8($sp) % Save old frame pointer add $fp, $sp, 8 % Set new frame pointer sw $ra, -4($fp) % Save return address add $t0, $a0 $a0 % Addition move $v0, $t0 % Copy return value lw $ra, -4(%fp) % Restore return address lw $fp, 0($fp) % Restore frame pointer add $sp, $sp, 8 % Pop stack frame jr $ra % Jump to return address 9/14/2019 cpeg323-05F\Topic2b-323

Output Assembly Code (optimized) foo: add $v0, $a0, $a0 % Set result jr $ra % Jump to return address 9/14/2019 cpeg323-05F\Topic2b-323

MIPS assembly code of the procedure swap swap(int v [ ], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } MIPS assembly code of the procedure swap 9/14/2019 cpeg323-05F\Topic2b-323

Tree of Function Activations Starting from the top (e.g., main () in C/C++), the history of function calls forms a tree A path following the sequence function calls and returns forms a “depth-first traversal” of the tree At any point in time, the only function calls whose variables need to be stored are the direct ancestors of the function call currently being executed. 9/14/2019 cpeg323-05F\Topic2b-323

Example: Recursive Function Calls 4 5 fib(4) 3 2 3 fib(3) fib(2) 2 1 1 1 1 2 1 fib(2) fib(1) fib(1) fib(0) 1 1 1 fib(1) fib(0) Input value Return value 9/14/2019 cpeg323-05F\Topic2b-323

Calling a Leaf Function To call a function which never calls another and doesn’t require much memory: Caller: put arguments to the function in $a0-$a3 If anything important in $t0-9, move them now! jal to the function Function: do your job (use $t0-$t9 if needed) Jr to $ra Caller: continue where you left off 9/14/2019 cpeg323-05F\Topic2b-323

Stack Frames If a function needs more memory and/or may call others, it uses a stack frame, which holds: Automatic variables (non-static variables declared within function) Arguments to the function (just another type of local variable) The “return address” (since $ra overwritten by call) Saved registers from caller ($s0-$s7) if you need to use them “Spill” registers, including $t0-$t9 when calling others 9/14/2019 cpeg323-05F\Topic2b-323

Illustration of Stack Frames High address $fp $fp $sp $fp Saved argument Register (if any) $sp Saved return address Saved saved Registers (if any) Local arrays and Structures (if any) $sp Low address a b c 9/14/2019 cpeg323-05F\Topic2b-323

Calling a Non-Leaf Function (Caller) Put arguments to the function in $a0-$a3 Save contents of $t0-9 if they will be needed later If more than 4 args, push them onto stack jal (or jral) to beginning of the function code 9/14/2019 cpeg323-05F\Topic2b-323

Calling a Non-Leaf Function (Callee) Push current fp onto stack Move fp to top of frame (just below old sp) Set sp to (fp – frame size) Frame size is the same for every call of the same function Known at compile-time Use displacement addressing to get at local variables Save $s0-$s7 (whichever you need to reuse) and $ra in frame Save $a0-$a3 to frame if needed (e.g., calling another function) 9/14/2019 cpeg323-05F\Topic2b-323

Returning from Non-Leaf Function (Callee) Put return values (if any) in $v0 and $v1 Restore $s0-$s7 (whichever were saved) and $ra from frame Restore sp to just above current fp Restore old fp from stack frame Jump to $ra (jr) Caller can get return args in $v0 and $v1, if any 9/14/2019 cpeg323-05F\Topic2b-323

Other Storage: Global Variables In C/C++, “global variables” are Variables declared outside of any functions Static variables (inside or outside a function) Static data members of a class (C++) Properties: Only one copy of each (unlike automatic variables) Initialization allowed (set value before main () starts) All in one region of memory, accessed through $gp (r28) 9/14/2019 cpeg323-05F\Topic2b-323

Other Storage: Dynamic Storage (Heap) In C/C++, the “heap” contains Blocks of memory allocated by malloc () etc. Objects created using the new keyword (C++) Properties: Stored in a big chunk of memory between globals and stack Controlled by the programming language’s library (e.g., libc) Can be grown if needed No dedicated reg. Like $gp; everything goes through pointers 9/14/2019 cpeg323-05F\Topic2b-323

Typical Layout of Program stack Dynamic date Reserved Text Static data $sp efff ffff hex $gp 1000 8000 1000 8000 pc 0040 0000 (From Patterson and Hennessy, p. 152; COPYRIGHT 1988 MORGAN KAUFMANN PUBLISHERS, INC. ALL RRIGHTS RESERVED) 9/14/2019 cpeg323-05F\Topic2b-323

What an Executable Program Looks Like When you execute a program, it is in the form of an “executable” The executable contains everything you need to run your program Every function used, starting with main() – the “text segment” Values of all initialized global variables – the “data segment” Information about uninitialized globals Every function and every global variable has a unique address in memory 9/14/2019 cpeg323-05F\Topic2b-323

Executing an Executable When you execute a program, the loader: Allocates space for your program (details vary by OS) Copies the text and data segments of the executable to memory Jumps to a known starting address (specified in the executable) Once the executable starts running at that starting address, it Initializes regs such as $gp and $sp; initializes heap (if used) Sets uninitialized globals to 0 (if the language requires this) Sets up command line args into data structure (e.g., argc/argv) Does jal to start of main () function 9/14/2019 cpeg323-05F\Topic2b-323