Compiling Code, Procedures and Stacks

Similar documents
Procedures and Stacks

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

MIPS Functions and the Runtime Stack

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Lecture 5: Procedure Calls

Instruction Set Architectures (4)

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

Control Instructions

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

RISC-V Assembly and Binary Notation

6.004 Tutorial Problems L3 Procedures and Stacks

Computer Architecture

EE 361 University of Hawaii Fall

Chapter 2A Instructions: Language of the Computer

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

CENG3420 Lecture 03 Review

MIPS%Assembly% E155%

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

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CS 61c: Great Ideas in Computer Architecture

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Implementing Procedure Calls

SPIM Instruction Set

Lectures 3-4: MIPS instructions

Lecture 5: Procedure Calls

MIPS Procedure Calls. Lecture 6 CS301

Instructions: Assembly Language

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Course Administration

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

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.

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

CSE Lecture In Class Example Handout

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

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

Announcements. EE108B Lecture MIPS Assembly Language III. MIPS Machine Instruction Review: Instruction Format Summary

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

EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design

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

ECE232: Hardware Organization and Design

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

Lecture 6: Assembly Programs

MIPS Instruction Set

ECE260: Fundamentals of Computer Engineering

Thomas Polzer Institut für Technische Informatik

ECE331: Hardware Organization and Design

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

ECE 30 Introduction to Computer Engineering

CS/COE1541: Introduction to Computer Architecture

Storage in Programs. largest. address. address

Lecture 4: MIPS Instruction Set

Chapter 2: Instructions:

Topic Notes: MIPS Instruction Set Architecture

Chapter 3. Instructions:

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

CS 316: Procedure Calls/Pipelining

CA Compiler Construction

1 /15 2 /20 3 /20 4 /25 5 /20

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

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

Functions in MIPS. Functions in MIPS 1

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

Programmable Machines

Programmable Machines

ECE331: Hardware Organization and Design

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

Computer Organization MIPS ISA

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

Course Administration

1 /20 2 /18 3 /20 4 /18 5 /24

CS222: MIPS Instruction Set

Anne Bracy CS 3410 Computer Science Cornell University

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

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

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

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

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

MIPS Functions and Instruction Formats

Computer Architecture. The Language of the Machine

Compiling Techniques

Common Problems on Homework

Lec 10: Assembler. Announcements

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

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

MIPS Assembly Language Guide

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

Systems I. Machine-Level Programming V: Procedures

EEC 581 Computer Architecture Lecture 1 Review MIPS

MIPS Assembly (Functions)

Transcription:

Compiling Code, Procedures and Stacks L03-1

RISC-V Recap Computational Instructions executed by ALU Register-Register: op dest, src1, src2 Register-Immediate: op dest, src1, const Control flow instructions Unconditional: jal and jalr Conditional: comp src1, src2, label Loads and Stores lw dest, offset(base) sw src, offset(base) Base is a register, offset is a small constant Pseudoinstructions Shorthand for other instructions L03-2

Dealing with Constants Assume a is in register x1 and b is in x2. Small constants (12-bit) can be handled via Register- Immediate ALU operations Execute a = b+3 addi x1, x2, 3 Execute a = b+0x123456 Largest 12 bit 2 s complement constant is 2 11-1 = 2047 (0x7FF) Use li pseudoinstruction to set register to large constant lui x4, 0x123 x4 = 0x123000 li x4, 0x123456 addi x4, x4, 0x456 Can also use li pseudoinstruction for small constants li x4, 0x12 addi x4, x0, 0x12 L03-3

Compiling Simple Expressions Assign variables to registers Translate operators into computational instructions Use register-immediate instructions to handle operations with small constants Use the li pseudoinstruction for large constants Example C code int x, y, z; y = (x + 3) (y + 123456); z = (x * 4) ^ y; RISC-V Assembly // x: x10, y: x11, z: x12 // x13, x14 used for temporaries addi x13, x10, 3 li x14, 123456 add x14, x11, x14 or x11, x13, x14 slli x13, x10, 2 xor x12, x13, x11 L03-4

Compiling Conditionals if statements can be compiled using branches: C code if (expr) { if-body RISC-V Assembly (compile expr into xn) beqz xn, endif (compile if-body) endif: Example: Compile the following C code int x, y; if (x < y) { y = y x; // x: x10, y: x11 slt x12, x10, x11 beqz x12, endif sub x11, x11, x10 endif: We can sometimes combine expr and the branch bge x10, x11, endif sub x11, x11, x10 endif: L03-5

Compiling Conditionals if-else statements are similar: C code if (expr) { if-body else { else-body RISC-V Assembly (compile expr into xn) beqz xn, else (compile if-body) j endif else: (compile else-body) endif: L03-6

Compiling Loops Loops can be compiled using backward branches: C code while (expr) { while-body Can you write a version that executes fewer instructions? RISC-V Assembly while: (compile expr into xn) beqz xn, endwhile (compile while-body) j while endwhile: // Version with one branch // or jump per iteration j compare loop: (compile while-body) compare: (compile expr into xn) bnez xn, loop L03-7

Putting it all together C code while (x!= y) { if (x > y) { x = x y; else { y = y x; RISC-V Assembly // x: x10, y: x11 j compare loop: (compile while-body) compare: bne x10, x11, loop L03-8

Putting it all together C code while (x!= y) { if (x > y) { x = x y; else { y = y x; RISC-V Assembly // x: x10, y: x11 j compare loop: ble x10, x11 else sub x10, x10, x11 j endif else: sub x11, x11, x10 endif: compare: bne x10, x11, loop L03-9

Procedures C code int gcd(int a, int b) { int x = a; int y = b; while (x!= y) { if (x > y) { x = x y; else { y = y x; return x; RISC-V Assembly // x: x10, y: x11 j compare loop: ble x10, x11 else sub x10, x10, x11 j endif else: sub x11, x11, x10 endif: compare: bne x10, x11, loop L03-10

Procedures Procedure (a.k.a. function or subroutine): Reusable code fragment that performs a specific task Single named entry point Zero or more formal parameters Local storage Returns to the caller when finished Using procedures enables abstraction and reuse Compose large programs from collections of simple procedures int gcd(int a, int b) { int x = a; int y = b; while (x!= y) { if (x > y) { x = x y; else { y = y x; return x; bool coprimes(int a, int b) { return gcd(a, b) == 1; coprimes(5, 10); // false coprimes(9, 10); // true L03-11

Managing a procedure s register space A caller uses the same register set as the called procedure We could have a convention regarding how the registers are divided between the caller and callee Not satisfactory If proc A calls proc B calls proc C, it would be difficult to manage such a division of register space, which was scarce to begin with Better solution, a caller should not rely on how the called procedure manages its register space Either the caller or the callee saves the caller s registers in memory and restores them when the procedure call has completed execution L03-12

Implementing procedures A caller needs to pass parameters to the called procedure, as well as get results back from the called procedure both are done through registers A procedure can be called from many different places The caller can get to the called procedure code simply by executing a unconditional jump instruction However, to return to the correct place in the calling procedure, the called procedure has to know which of the possible return addresses it should use [0x100] j sum [0x678] j sum sum: j? 0x104? 0x67C? Return address must be saved and passed to the called procedure! L03-13

Procedure Linking How to transfer control to callee and back to caller? proc_call: jal ra, label 1. Stores address of proc_call + 4 in register ra (return address register) decided by convention 2. Jumps to instruction at address label where label is the name of the procedure 3. After executing procedure, j ra to return to caller and continue execution [0x100] jal ra, sum [0x678] jal ra, sum ra = 0x104 ra = 0x67C sum: jr ra 1 st time: j 0x104 2 nd time: j 0x67C L03-14

Procedure calls: Complications Suppose proc A calls proc B calls C a single return address register won t work; the return address for proc B would wipe out the return address for proc a! a similar complication arises in the memory space where the registers of proc A are saved this space has to be different from the place where the registers of proc B are saved L03-15

Procedure Storage Needs Basic requirements for procedure calls: Input arguments Return address Results Local storage: Variables that compiler can t fit in registers Space to save caller s register values for registers that we overwrite Each procedure call has its own instance of all this data known as the procedure s activation record. L03-16

Insight (ca. 1960): We Need a Stack! Need data structure to hold activation records Activation records are allocated and deallocated in last-in-first-out (LIFO) order Stack: push, pop, access to top element proc C proc B proc B proc B proc A proc A proc A proc A proc A We only need to access to the activation record of the currently executing procedure L03-17

RISC-V Stack Stack is in memory à need a register to point to it In RISC-V, stack pointer sp is x2 Stack grows down from higher to lower addresses Push decreases sp Pop increases sp sp points to top of stack (last pushed element) Discipline: Can use stack at any time, but leave it as you found it! R[sp] Lower Addresses unused space stacked data stacked data stacked data stacked data Higher Addresses PUSH L03-18

Using the stack Sample entry sequence addi sp, sp, -N sw ra, 0(sp) sw a0, 4(sp) Corresponding Exit sequence lw ra, 0(sp) lw a0, 4(sp) addi sp, sp, N L03-19

Calling Convention The calling convention specifies rules for register usage across procedures RISC-V calling convention gives symbolic names to registers x0-x31 to denote their role: Symbolic name Registers Description Saver a0 to a7 x10 to x17 Function arguments Caller a0 and a1 x10 and x11 Function return values Caller ra x1 Return address Caller t0 to t6 x5-7, x28-31 Temporaries Caller s0 to s11 x8-9, x18-27 Saved registers Callee sp x2 Stack pointer Callee gp x3 Global pointer --- tp x4 Thread pointer --- zero x0 Hardwired zero --- L03-20

Caller-Saved vs Callee-Saved Registers A caller-saved register is not preserved across function calls (callee can overwrite it) If caller wants to preserve its value, it must save it on the stack before transferring control to the callee argument registers (an), return address (ra), and temporary registers (tn) A callee-saved register is preserved across function calls If callee wants to use it, it must save its value on stack and restore it before returning control to the caller Saved registers (sn), stack pointer (sp) L03-21

Example: Using callee-saved registers Implement f using s0 and s1 to store temporary values f: addi sp, sp, -8 sw s0, 4(sp) sw s1, 0(sp) addi s0, a0, 3 li s1, 123456 add s1, a1, s1 or a0, s0, s1 lw s1, 0(sp) lw s0, 4(sp) addi sp, sp, 8 ret int f(int x, int y) { return (x + 3) (y + 123456); // allocate 2 words (8 bytes) on stack // save s0 // save s1 // restore s1 // restore s0 // deallocate 2 words from stack // (restore sp) L03-22

Example: Using callee-saved registers Stack contents: Before call to f During call to f After call to f R[sp] unused space R[sp] Saved s1 Saved s0 R[sp] Saved s1 Saved s0 L03-23

Example: Using caller-saved registers Caller int x = 1; int y = 2; int z = sum(x, y); int w = sum(z, y); li a0, 1 li a1, 2 addi sp, sp, -8 sw ra, 0(sp) sw a1, 4(sp) // save y jal ra, sum // a0 = sum(x, y) lw a1, 4(sp) // restore y jal ra, sum // a0 = sum(z, y) lw ra, 0(sp) addi sp, sp, 8 Callee int sum(int a, int b) { return a + b; sum: add a0, a0, a1 ret Why did we save and restore a1? Callee may have modified a1 (caller doesn t see implementation of sum!) L03-24

Thank you! L03-25