CSC258: Computer Organization. Functions and the Compiler Tool Chain

Similar documents
Implementing Procedure Calls

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Anne Bracy CS 3410 Computer Science Cornell University

Functions and Procedures

CA Compiler Construction

Anne Bracy CS 3410 Computer Science Cornell University

Functions in MIPS. Functions in MIPS 1

Today. Putting it all together

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

ECE232: Hardware Organization and Design

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Lecture 5. Announcements: Today: Finish up functions in MIPS

Procedures and Stacks

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

CSC258: Computer Organization. Microarchitecture

MIPS Assembly (Functions)

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

CS153: Compilers Lecture 8: Compiling Calls

CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading)

Lecture 6: Assembly Programs

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Lecture 5: Procedure Calls

Anne Bracy CS 3410 Computer Science Cornell University

CS61C : Machine Structures

Run-time Environment

Code Generation. Lecture 12

Lecture 5: Procedure Calls

From Code to Program: CALL Con'nued (Linking, and Loading)

More C functions and Big Picture [MIPSc Notes]

Lecture 7: Procedures

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Lec 10: Assembler. Announcements

CSE Lecture In Class Example Handout

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Lecture 7: Procedures and Program Execution Preview

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

Common Problems on Homework

CS 316: Procedure Calls/Pipelining

Instruction Set Architectures (4)

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

CS61C : Machine Structures

ECE260: Fundamentals of Computer Engineering

ECE 15B COMPUTER ORGANIZATION

We can emit stack-machine-style code for expressions via recursion

CS61C : Machine Structures

MIPS Functions and the Runtime Stack

Compilers and computer architecture: A realistic compiler to MIPS

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

CS 61c: Great Ideas in Computer Architecture

Course Administration

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

MIPS Procedure Calls. Lecture 6 CS301

Assembler. #13 Running a Program II

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

Lecture 10: Program Development versus Execution Environment

MIPS Assembly (FuncDons)

UCB CS61C : Machine Structures

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

SPIM S20: A MIPS R2000 Simulator

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

EE 361 University of Hawaii Fall

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

Storage in Programs. largest. address. address

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

CS64 Week 5 Lecture 1. Kyle Dewey

a) Do exercise (5th Edition Patterson & Hennessy). Note: Branches are calculated in the execution stage.

Systems Architecture I

MIPS Functions and Instruction Formats

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Procedure Call and Return Procedure call

CSC 2400: Computing Systems. X86 Assembly: Function Calls

LAB C Translating Utility Classes

EE 109 Unit 15 Subroutines and Stacks

CSC 2400: Computer Systems. Using the Stack for Function Calls

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Transcription:

CSC258: Computer Organization Functions and the Compiler Tool Chain 1

A Note about This Week s Quiz There were very few exercises this week, except for writing substantial pieces of code. Instead, I provided questions in the reading guide. I would treat these as the set of questions that should be prepared for the quizzes.

Discussion: Reading Quiz 1. What are the names of the tools in the compiler toolchain? (And what is the rough responsibility of each?) 2. What does the jal (jump and link) instruction do? 3. List the expectations we, as programmers, have of functions. Here s one a. After a function executes, control returns to the instruction after the function call at the call site. b. 3

Discussion: Reading Quiz 1. What are the names of the tools in the compiler toolchain? (And what is the rough responsibility of each?) 2. What does the jal (jump and link) instruction do? 3. List the expectations we, as programmers, have of functions. Here s one a. After a function executes, control returns to the instruction after the function call at the call site. b. 4

The Compiler Toolchain 5

Questions to Consider What are the components of the compiler toolchain? What are their inputs and outputs? Why does the assembler take two passes? Why does the linker need to be able to patch addresses? Why don t we do any type checking when combining files (with the linker)? 6

Disclaimer These slides present an idealized compiler toolchain. In the real world, the responsibilities and distinctions between components are blurred. For example, the linker and loader may be the same application! The compiler and assembler may also be combined. 7

The Compiler Toolchain We really on a sequence of tools to convert a high level language (like C) to machine code. Compiler Assembler Compiler Assembler Linker Loader 8

The Loader Once a program has been built into an executable, it needs to be loaded into memory to run. Kernel virtual memory Stack The loader s job is to allocate space for the program and to place it in memory. The code and data segments are defined in the executable. To learn more: CSC369 (operating systems) Memory mapped region for shared libraries Heap (created at runtime by malloc) Static Data (Data Segment) Code (Text Segment) Unused 9

The Linker We like to be able to write a program in multiple files. This lets us structure the code logically (in modules). It allows us to divide the work. It reduces compilation time, as only the files that are modified need to be re-compiled. The linker combines multiple files into a single executable. This requires that it patch symbol values. 10

Assembling and Linking $ cat other.c $ cat main.c #include <stdio.h> int main(int argc, char* argv[]) { int x = ret_value(6, 3); printf("%d\n", x); return x; } $ gcc -c main.c $ gcc -c other.c # These made the compiler and assembler create object files --.o files. $ gcc other.o main.o # This invokes the linker to combine the files. $./a.out 6 $ # We explored adding prototypes (including erroneous ones) and found that the linker never complained! $ # It s the compiler s job to find type errors. The linker does not check, so by default, the linker doesn t warn you if two functions don t match in type! 11

The Assembler The assembler converts assembly code into machine code. Assemblers are architecture specific. There are MIPS assemblers, x86 assemblers, etc. Sometimes, the code being translated is not assembly -- it s some other intermediate form produced by the compiler. i.e., LLVM s typed intermediate language 12

The Compiler The compiler s job is to convert high level code to assembly code. To learn more: CSC488 (compilers) Image courtesy: http://commons.wikimedia.org/wiki/file:multipass_compiler.svg 13

Compiling int main(int argc, char* argv[]) { return 0; } $ gcc -S -march=mips32 main.c $ # Don t try that at home. Or do, but expect it that it might fail. I installed a version of gcc to cross-compile to MIPS. $ cat main.s.file 1 "main.c".section.mdebug.abi32.previous.abicalls.text.align 2.globl main.ent main main: addiu $sp,$sp,-8 sw $fp,0($sp) move $fp,$sp sw $4,8($fp) sw $5,12($fp) move $2,$0 move $sp,$fp lw $fp,0($sp) addiu $sp,$sp,8 j $31 nop.set macro.set reorder.end main.size main,.-main.ident "GCC: (GNU) 4.1.2" 14

One Extra Stage: Preprocessing #define RETVAL 3 int main(int argc, char* argv[]) { return RETVAL; } $ gcc -E main.c # 1 "main.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "main.c" int main(int argc, char* argv[]) { return 3; } 15

Functions 16

Discussion: Reading Quiz 1. What are the names of the tools in the compiler toolchain? (And what is the rough responsibility of each?) 2. What does the jal (jump and link) instruction do? 3. List the expectations we, as programmers, have of functions. Here s one a. After a function executes, control returns to the instruction after the function call at the call site. b. 17

Key Questions What steps are required on the caller s side to invoke a function? What steps are required on the callee s side to execute and return from the function? 18

Functions int my_func(int x, int y) {... return z; } Functions allow us to structure -- and reuse -- code Functions are governed by conventions that were affected by hardware limitations: Only a single value is returned, arguments may be passed, local variables do not escape the environment,... They are also constrained by the compiler. We need firm rules about how to transfer control to and from functions. 19

Implementing Functions What does the toolchain tell us about functions? Functions may be defined in multiple files. They may be invoked in files that do not contain the definitions. The location of the function will be patched during linking At the compilation stage, we need a description of the function and a standard way to call it. We need conventions, and we need a prototype. 20

A Function in C In C, the declaration is the prototype. It allows the invocation of the function without the definition. What do we expect to occur when the Declaration: int my_func(int, int); Definition: int my_func(int x, int y) {... return z; } Use: int my_var = my_func(3, 4); function is called? 21

Function Requirements Every function call needs its own distinct set of local variables, and these variables disappear when the function ends. Variables are located in a stack frame. Functions must be invoked on arguments. The argument values must be transferred. Every function call needs to return to the location that invoked it. We must store a return address and return value. 22

Warning: Conventions Differ! The whole point of a convention is to have an agreement about the interface. but there have been several versions of MIPS. Different tools will target different versions. In particular, in the lab (next week), I am asking you to implement the version in A.6 of Larus s MIPS reference. The gcc version we will explore today differs slightly. 23

Key Elements The frame pointer is used to point to the top of the stack frame. The stack pointer points to the bottom of the frame. (Why do we need both?) The caller must save some registers (the caller saves ), and the callee must save other registers (the callee saves ). The callee usually also stores arguments on the stack. Sometimes it also saves the $ra. When must it do so? The return value is placed in $v0. The callee then calls jr $ra to return back to the caller. 24

Key Elements: Caller Ensure that critical registers like $ra have been saved. Save caller-save registers. Place arguments into $a0-$a3 and on the stack. Jump-and-link to the function.... On return, save the return value. Remove arguments from the stack. Restore caller-save registers. 25

Key Elements: Callee Allocate all of the space needed for the stack. If calling a function: store $ra and, if used, $fp. It s good practice to save arguments, too. Move the $fp. Store callee-save registers.... Place the return value in $v0. Restore callee-saves. Tear down the stack. 26

Caller- and Callee- Save Registers Why should we separate the register file into two sets of registers? 27

Caller- and Callee- Save Registers Why should we separate the register file into two sets of registers? Performance! Caller-save registers are for very short-term temporaries. The caller should not often need to save them. Callee-saves are for values that need to exist across function calls. The callee only needs to save them if they are used. 28

Exceptions 29

Key Questions What is the purpose of an exception? What is a system call? How are function calls and exceptions similar? How are they different? How is an exception invoked? How do you return? 30

System Calls A system call is just a function call... that invokes the operating system. Printing, getting input, and allocating memory are all system call operations. We can t just give user programs control over thse operations because they must be shared across multiple users. 31

Invoking a System Call For security, the operating system must be able to access memory and execute instructions that the user cannot. Otherwise, what would stop the user from just using a resource, rather than asking the OS to do so? A system call is invoked by placing arguments in the registers and then causing an exception. The exception places the processor in a privileged mode - - one that can act as the OS. 32

Exceptions in MIPS An exception can be caused by a program or by an event. For example, when you divide by zero, that causes an exception. syscall also creates an exception. When a syscall occurs, the cause of the exception is placed in a special register -- accessed using mfc0. Then, an exception handler is invoked. System calls are sent directly to a syscall handler. 33

Exception Registers Source: Larus, Assemblers, Linkers, and the SPIM Simulator. http://pages.cs.wisc.edu/~larus/hp_appa.pdf 34

Things to Remember The exception handler is just assembly code. It s like any other function! It must not cause an error. It should leave the machine just as it was -- except for fixing whatever condition caused the exception. It can t change the stack. It shouldn t junk any registers. The exception handler you ll use in the last lab saves two registers in the data segment. Those are the only registers you may use. 35