Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 2b ISA Support for High-Level Languages

Similar presentations


Presentation on theme: "Topic 2b ISA Support for High-Level Languages"— Presentation transcript:

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

2 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

3 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

4 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

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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

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

18 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

19 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


Download ppt "Topic 2b ISA Support for High-Level Languages"

Similar presentations


Ads by Google