Programming Language Dilemma Fall 2002 Lecture 1 Introduction to Compilation. Compilation As Translation. Starting Point

Size: px
Start display at page:

Download "Programming Language Dilemma Fall 2002 Lecture 1 Introduction to Compilation. Compilation As Translation. Starting Point"

Transcription

1 Programming Language Dilemma Fall 2002 Lecture 1 Introduction to Compilation Martin Rinard Laborator for Computer Science Massachusetts Institute of Technolog Stored program computer How to instruct computer what to do? Need a program that computer can execute Must be written in machine language Unproductive to code in machine language Design a higher level language Implement higher level language Alternative 1: Interpreter Alternative 2: Compiler Compilation As Translation Starting Point Source Program in Some Programming Language Ending Point Compiler Generated Program in Machine Language Starting Point Standard imperative language (Java,C, C++) State Variables Structures Arras Computation Expressions (arithmetic, logical, etc.) Assignment statements Control flow (conditionals, loops) Procedures Ending Point State (SPARC) (32 bit resses, bte ressable) 32 Integer Registers g0-g7: global registers g0 reads as 0, writes have no effect o0-o7: output registers l0-l7: local registers i0-i7: input registers Condition Codes Indicate results of integer operations Used in branch instructions Ending Point Computation (SPARC) <r>,<reg> st <reg>, <r> <binar op> <src1>, <src2>, <dst> <comp op> <src1>, <src2> <branch op> <ress> Conditional Unconditional 1

2 Exploring Compiler Behavior Start with sample input programs Compile to assembler using cc S Tr to match up source code with generated code State Translation Variables (global, local, parameters) Structures and arras Computation Translation Expression evaluation and assignment Flow of control constructs Procedure call and return Implementing Global Variables Allocate memor location for variable When program accesses variable, compiler generates load and store instructions int a, b, c; a b c int a, b, c; a = b + c; Implementing Global Variables.align 8.common a,4,4.common b,4,4.common c,4,4 Allocate storage for a,b, and c sethi %hi(b),%l0 or %l0,%lo(b),%l0 [%l0+0],%l2 sethi %hi(c),%l0 or %l0,%lo(c),%l0 [%l0+0],%l1 %l2,%l1,%l1 sethi %hi(a),%l0 or %l0,%lo(a),%l0 st %l1,[%l0+0] Load value of b into l0 Load value of c into l1 Add l0 and l1 Store result into a Sethi Instruction Encode parts of ress in instruction stream Machine code format of sethi instruction: 00 5 bit <reg> bit <immediate> Effect of sethi instruction: Replace top 22 bits of <reg> with <immediate> Set bottom 10 bits of <reg> to zero Example of general theme store constant values in immediate fies of instructions Implementing Local Variables Concept of procedure call stack Each procedure invocation has state Local variables Return ress Stores state in frame New frame allocated for each call Frames usuall allocated on call stack Call stack allocated at top of memor Call stack grows down Implementing Local Variables Frame pointer register points to current frame Decreases at calls (stack grows down) Increases on returns Local variables allocated in frame int a, b, c; Frame for invocation of proc Frame for caller of proc Local variables of proc a b c Return r Registers fp 2

3 Implementing Local Variables int a, b, c; a = b + c; st [%fp-12],%l0 [%fp-16],%l1 %l0,%l1,%l0 %l0,[%fp-8] Note: %fp is same as %i6 Points to frame for procedure Implementing Structures Structures contain several fies Each structure tpicall stored in a contiguous block of memor tpedef struct { int x,, z; foo; foo *p; z x p Implementing Structures Optimized Version tpedef struct { int x,, z; foo; foo *f; f->x = f->+f->z; st [%fp-8],%l0 %l0,4,%l0 [%l0+0],%l2 [%fp-8],%l0 %l0,8,%l0 [%l0+0],%l1 %l2,%l1,%l1 [%fp-8],%l0 %l1,[%l0+0] Compute ress of f-> load f-> Compute ress of f->z load f->z values Store into f->x tpedef struct { int x,, z; foo; foo *f; f->x = f->+f->z; st [%fp-4],%o0 [%o0+4],%o1 [%o0+8],%o2 %o1,%o2,%o1 %o1,[%o0] Alignment, Ping, and Packing Ping and Packing Example Machines often have alignment requirements Integers (4 btes) must start at 4-bte aligned ress (bottom 2 bits 0) Shorts (2 btes) must start at 2-bte aligned ress (bottom bit 0) Alignment requirements raise issues Ping between fies to ensure alignment Fie packing to minimize memor usage tpedef struct { int w; char x; int ; char z; foo; foo *p; Naïve Laout z x w Packed Laout (4 bte savings) x, z w p p 3

4 Implementing Arras Allocate memor locations for arra elements Elements stored contiguousl int a[4]; a[3] a[2] a[1] a[0] int a[4]; int i, j; i = a[j]; Implementing Arras [%fp-12],%l0 sll %l0,2,%l0 sethi %hi(a),%l1 or %l1,%lo(a),%l1 %l0,%l1,%l0 [%l0+0],%l0 st %l0,[%fp-8] Compute ress of a[j] load a[j] into l0 store l0 into i ress of a[j] = ress of a[0] + (4 * j) = a + (4 * j) Expression Evaluation Implementing Expression Evaluation Evaluate subexpressions, combine to get value of outer expression Must alwas have values of operands in registers Final result placed in register int x; int a,b; a = 3; b = 2; x = (a+b)-(a b); mov 3,%l0 st %l0,[%fp-8] mov 2,%l0 st %l0,[%fp-12] [%fp-8],%l0 [%fp-12],%l1 %l0,%l1,%l2 [%fp-8],%l0 [%fp-12],%l1 or %l0,%l1,%l1 sub %l2,%l1,%l1 sethi %hi(x),%l0 or %l0,%lo(x),%l0 st %l1,[%l0+0] Initialize a and b Load a and b Add a and b to l2 Load a and b Or a and b to l1 Compute l1=l2-l1 Load ress of x Store result in x Implementation Issues Optimized Implementation Generating a linear sequence of instructions to compute a nested expression Allocate storage for temporar values Tpicall registers, but there are a limited number of registers for machine Ma need to store temporaries in memor Expression evaluation order affects number of values ou need to keep around In man cases, ma be able to staticall compute value of subexpressions int x; int a,b; a = 3; b = 2; x = (a+b)-(a b); or %g0,2,%g2 sethi %hi(x),%g1 st %g2,[%g1+%lo(x)] 4

5 Flow of Control Convert structured flow of control to branch statements Two pervasive shapes if C then A else B Code to evaluate C Code to execute A Code to execute B Code after if statement while C A Code to evaluate C Code to execute A Code after while statement int a, b; if (a) { b = 0; else { b = 1; Conditional Example [%g1+%lo(a)],%g1 cmp %g1,0 be.l1 sethi %hi(b),%g1 br.l2 st %g0,[%g1+%lo(b)].l1: or %g0,1,%g2 st %g2,[%g1+%lo(b)].l2 retl Optimized Conditional Example Apparent Anomal in Code int a, b; if (a) { b = 0; else { b = 1; [%g1+%lo(a)],%g1 cmp %g1,0 be.l1 sethi %hi(b),%g1 retl st %g0,[%g1+%lo(b)].l1: or %g0,1,%g2 retl st %g2,[%g1+%lo(b)] int a, b; if (a) { b = 0; else { b = 1; [%g1+%lo(a)],%g1 cmp %g1,0 be.l1 sethi %hi(b),%g1 br.l2 // branch over else part st %g0,[%g1+%lo(b)] // store value into b for then part.l1: or %g0,1,%g2 st %g2,[%g1+%lo(b)].l2 retl Branch appears before store Wh will b get correct value? Concept of Branch Dela Slots In SPARC architecture, instruction after branch executes even if branch is taken! be.l1 sethi %hi(b),%g1 This instruction executes even if the branch is taken! Wh do this? It improved the performance of the initial version of the processor Compiler cou handle the complexit What if there is no instruction to execute?! Instruction Scheduling Branch dela slots are special case of instruction scheduling Instruction scheduling packs instructions together for concurrent/pipelined execution Sophisticated part of compilation Moves work from hardware to compiler Illustrates rarit of direct assembl coding Required for IA-64 to work well 5

6 int a[10]; int n = 10; int i; i = 0; while (i < n) { a[i]++; i++; Implementation of Loops Initialize i to 0 and n to 10 Load i and n Branch to end if i >= n Compute ress of a[i] Load a[i] Increment value Store back into a[i] Increment i Branch back to top if i < n int a[10]; int n = 10; int i; i = 0; while (i < n) { a[i]++; i++; Implementation mov 10,%l0 st %l0,[%fp-8] mov 0,%l0 st %l0,[%fp-12] [%fp-12],%l1 [%fp-8],%l0 cmp %l1,%l0 bge.l18.l16: sll [%fp-12],%l0 %l0,2,%l0 sethi %hi(a),%l1 or %l1,%lo(a),%l1 %l0,%l1,%l0 st %l0,[%fp-16] [%fp-16],%l0 [%l0+0],%l0 %l0,1,%l1 [%fp-16],%l0 st %l1,[%l0+0] [%fp-12],%l0 %l0,1,%l0 st %l0,[%fp-12] [%fp-12],%l1 [%fp-8],%l0 cmp %l1,%l0 bl.l16.l18 Optimizations Keep i and ress of a[i] in registers Compute ress of a[0] before loop bod, increment b 4 in loop bod Don t store n in memor or register, just use 10 whenever ou see it Omit initial branch at top of loop Optimized Implementation int a[10]; int n = 10; int i; i = 0; while (i < n) { a[i]++; i++; %g1,%lo(a),%g1 or %g0,0,%g2 [%g1],%g3.l : %g3,1,%g3 st %g3,[%g1] %g2,1,%g2 %g1,4,%g1 cmp %g2,10 bl,a.l [%g1],%g3.l : Load base ress of a Init i Load a[i] Increment and store a[i] Update i and ptr to a[i] Loop back Load a[i] int a[10]; int n = 10; int i; i = 0; while (i < n) { a[i]++; i++; More Optimizations %g1,%lo(a),%g1 or %g0,0,%g2 [%g1],%g3.l : %g3,1,%g3 st %g3,[%g1] %g2,1,%g2 %g1,4,%g1 cmp %g1,10 a+40 bl,a.l [%g1],%g3.l : Load base ress of a Init i Load a[i] Increment and store a[i] Update i and ptr to a[i] Loop back Load a[i] Procedure Call Protocol between caller and callee Heavil architecture dependent SPARC concepts Caller actions Store parameters in o0-o6 Jump to callee, storing PC in o7 Get return result in o0 Callee actions Get parameters in i0-i6, PC from caller in i7 Put return result in i0 Use PC from caller to return back to caller 6

7 Register Windows Parameter issue Caller puts parameters in o0-06 Callee expects parameters in i0-i6 Return result issue Callee puts return result in i0 Caller expects result in o0 Wh? Register windows! Conceptuall, have an overlapping stack of register windows Visual Register Windows Prev i0-i7 l0-l7 o0-o7 Current i0-i7 l0-l7 o0-o7 Have current window i0-i7 of current window are same as o0-o7 of previous window o0-o7 of current window are same as i0-i7 of next window g0-g7 Next i0-i7 l0-l7 o0-o7 Register Window Instructions save %sp, <num>, %sp Pushes current window on stack Allocates new window (%o0-%o7 become %i0-%i7, new l0-l7 and o0-o7) Sets %sp (%o6) in new window to %sp in o window plus <num> Note that %o6 in o window becomes %i6 in new window (%sp becomes %fp) restore Pops current window (%i0-%i7 become %o0-%o7) Stack and Frame Pointers save %sp, -12, %sp (In practice, need at least btes to O Reg Win leave space for reg saves) %fp = %i6 New Reg Win %sp = %o6 %fp = %i6 %sp = %o6 Procedure Call Example Procedure Example Standard Prologue save st %sp,-104,%sp %i0,[%fp+68] New Reg Win, frame Store param int n; bar() { foo(n); sethi %hi(n),%l0 or %l0,%lo(n),%l0 [%l0+0],%l0 mov %l0,%o0 call foo Load n Set up parameter Call foo (stores PC of call instruction into %o7) int foo(int n) { return n+1; Standard Epilogue [%fp+68],%l0 %l0,1,%l0 st %l0,[%fp-4] Compute result ba.l13.l13: [%fp-4],%l0 mov %l0,%i0 Load result jmp %i7+8 Return to caller restore Restore Reg Win & frame 7

8 int foo(int n) { return n+1; Optimized Leaf Procedure Punt register windows completel Just compute in window from caller jmp %o7+8 %o0,1,%o0 Return to caller Compute result Complex Design Need for separate compilation Must be standard call/return protocol Need for performance Parameters/return value passed in registers Supports efficient caller/callee linkage Protocol supports tailored code generation Caller does not set up register window, frame pointer, or stack pointer for callee Enables leaf procedure optimizations Compiler hides all this from programmer! Objects and Inheritance Ke Consideration: Subtping Object consists of State (fies of object) Behavior (methods) Inheritance Augment base class with new fies Augment behavior of base class with new methods Override some methods of base class If B inherits from A, then Anwhere program declares object of tpe A Must be able to execute with object of tpe B Implementing Object State (Single Inheritance Onl) Extension of structure approach Store fies in contiguous block of memor Fies of extending class stored after fies of base class fies x of B w fies z of A Ke propert - fies from base class stored at same offset in all objects that inherit from base class void p(a) { a.f(); Virtual Function Calls (Single Inheritance Onl) A a = new A(); int i = p(); // p() calls f() in class A B b = new B(); int j = p(); // p() calls f() in class B Different executions of this call site ma invoke different methods Invoked method depends on class of object 8

9 void p(a) { a.f(); Vtable Approach Each object has reference to a vtable One vtable per class Vtable contains pointers to methods for all objects of its class Vtable for A objects Vtable for B objects z x w z vtable ptr vtable ptr A object B object void p(a) { a.f(); Virtual Function Calls Calling convention stas same But call site determines ress of the invoked method dnamicall Generated Code at Call Site [%i0],%o1 [%o1],%o1 call %o1,0 Load vtable ptr Load function ptr Indirect call Summar Compiler responsibilities Data laout and access Global and local variables, parameters Structures, arras, and objects Expression evaluation Flow of control Procedure and method calls Hide low-level machine complexities Optimizations 9

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu) Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality:

More information

Code Generation. Lecture 19

Code Generation. Lecture 19 Code Generation Lecture 19 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

Procedure Call. Procedure Call CS 217. Involves following actions

Procedure Call. Procedure Call CS 217. Involves following actions Procedure Call CS 217 Procedure Call Involves following actions pass arguments save a return address transfer control to callee transfer control back to caller return results int add(int a, int b) { return

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields Classes Implementing Objects Components fields/instance variables values differ from to usuall mutable methods values shared b all s of a class usuall immutable component visibilit: public/private/protected

More information

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

More information

Course Administration

Course Administration Fall 2018 EE 3613: Computer Organization Chapter 2: Instruction Set Architecture Introduction 4/4 Avinash Karanth Department of Electrical Engineering & Computer Science Ohio University, Athens, Ohio 45701

More information

System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

More information

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today: Winter 2006-2007 Compiler Construction T11 Activation records + Introduction to x86 assembly Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University Today ic IC Language Lexical Analysis

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Procedure and Object- Oriented Abstraction

Procedure and Object- Oriented Abstraction Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1 Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks

More information

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls Lecture 5: Procedure Calls Today s topics: Procedure calls and register saving conventions 1 Example Convert to assembly: while (save[i] == k) i += 1; i and k are in $s3 and $s5 and base of array save[]

More information

Review of Activation Frames. FP of caller Y X Return value A B C

Review of Activation Frames. FP of caller Y X Return value A B C Review of Activation Frames In general, activation frames are organized like this: HI LO Bookkeeping/preserved registers I/O parameters Locals and temps RA of caller FP of caller Y X Return value A B C

More information

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

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

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

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls Lecture 5: Procedure Calls Today s topics: Memory layout, numbers, control instructions Procedure calls 1 Memory Organization The space allocated on stack by a procedure is termed the activation record

More information

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup The remote testing experiment. It works! Coverage - Score plot 0.9 Code Generation 0.7 0.6 Lecture 12 Coverage 0.5 0.4 0.3 Series1 Poly. (Series1) 0.2 0.1 0 0 10 20 30 40 50 Score CS 164 Lecture 14 Fall

More information

Code Generation Super Lectures

Code Generation Super Lectures Code Generation Super Lectures Huge One-Slide Summary Assembly language is untyped, unstructured, low-level and imperative. In a load-store architecture, instructions operate on registers (which are like

More information

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine Lecture Outline Code Generation Lecture 12 Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A

More information

Code Generation. Lecture 30

Code Generation. Lecture 30 Code Generation Lecture 30 (based on slides by R. Bodik) 11/14/06 Prof. Hilfinger CS164 Lecture 30 1 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source

More information

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

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 Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than (slt) compares its source registers

More information

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

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary Control Instructions Computer Organization Architectures for Embedded Computing Thursday, 26 September 2013 Many slides adapted from: Computer Organization and Design, Patterson & Hennessy 4th Edition,

More information

Control Instructions

Control Instructions Control Instructions Tuesday 22 September 15 Many slides adapted from: and Design, Patterson & Hennessy 5th Edition, 2014, MK and from Prof. Mary Jane Irwin, PSU Summary Previous Class Instruction Set

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design ECE232: Hardware Organization and Design Lecture 6: Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Procedures have different names in different languages Java:

More information

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines Lecture Outline Code Generation Lecture 30 (based on slides by R. Bodik) Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation of the

More information

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

More information

Lecture 4: Instruction Set Design/Pipelining

Lecture 4: Instruction Set Design/Pipelining Lecture 4: Instruction Set Design/Pipelining Instruction set design (Sections 2.9-2.12) control instructions instruction encoding Basic pipelining implementation (Section A.1) 1 Control Transfer Instructions

More information

Lecture 6: Assembly Programs

Lecture 6: Assembly Programs Lecture 6: Assembly Programs Today s topics: Procedures Examples Large constants The compilation process A full example 1 Procedures Local variables, AR, $fp, $sp Scratchpad and saves/restores, $fp Arguments

More information

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

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention Runtime Environment The Procedure Abstraction and Separate Compilation Topics we will cover The procedure abstraction and linkage conventions Runtime storage convention Non-local data access (brief) These

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

CENG3420 Lecture 03 Review

CENG3420 Lecture 03 Review CENG3420 Lecture 03 Review Bei Yu byu@cse.cuhk.edu.hk 2017 Spring 1 / 38 CISC vs. RISC Complex Instruction Set Computer (CISC) Lots of instructions of variable size, very memory optimal, typically less

More information

Computer Architecture

Computer Architecture Computer Architecture Chapter 2 Instructions: Language of the Computer Fall 2005 Department of Computer Science Kent State University Assembly Language Encodes machine instructions using symbols and numbers

More information

Storage in Programs. largest. address. address

Storage in Programs. largest. address. address Storage in rograms Almost all operand storage used by programs is provided by memory. Even though registers are more efficiently accessed by instructions, there are too few registers to hold the stored

More information

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

CSC 8400: Computer Systems. Using the Stack for Function Calls CSC 84: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015 Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative

More information

Wednesday, February 19, 2014

Wednesday, February 19, 2014 Wednesda, Februar 19, 2014 Topics for toda Solutions to HW #2 Topics for Eam #1 Chapter 6: Mapping High-level to assembl-level The Pep/8 run-time stack Stack-relative addressing (,s) SP manipulation Stack

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-08 In Class Example Handout Part A: J-Type Example: If you look in your book at the syntax for j (an unconditional jump instruction), you see something like: e.g. j addr would seemingly

More information

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

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V. Runtime management CS3300 - Compiler Design Runtime management V Krishna Nandivada IIT Madras Copyright c 2001 by Antony L Hosking Permission to make digital or hard copies of part or all of this work

More information

CA Compiler Construction

CA Compiler Construction CA4003 - Compiler Construction David Sinclair When procedure A calls procedure B, we name procedure A the caller and procedure B the callee. A Runtime Environment, also called an Activation Record, is

More information

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

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook) Lecture 2 Instructions: Language of the Computer (Chapter 2 of the textbook) Instructions: tell computers what to do Chapter 2 Instructions: Language of the Computer 2 Introduction Chapter 2.1 Chapter

More information

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

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI. CSCI 402: Computer Architectures Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI Recall Big endian, little endian Memory alignment Unsigned

More information

SPIM Procedure Calls

SPIM Procedure Calls SPIM Procedure Calls 22C:60 Jonathan Hall March 29, 2008 1 Motivation We would like to create procedures that are easy to use and easy to read. To this end we will discuss standard conventions as it relates

More information

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls Announcements HW1 is due on this Friday (Sept 12 th ) Appendix A is very helpful to HW1. Check out system calls on Page A-48. Ask TA (Liquan chen: liquan@ece.rutgers.edu) about homework related questions.

More information

Chapter 2A Instructions: Language of the Computer

Chapter 2A Instructions: Language of the Computer Chapter 2A Instructions: Language of the Computer Copyright 2009 Elsevier, Inc. All rights reserved. Instruction Set The repertoire of instructions of a computer Different computers have different instruction

More information

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1 Code Generation Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation

More information

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Compilation /15a Lecture 7. Activation Records Noam Rinetzky Compilation 0368-3133 2014/15a Lecture 7 Activation Records Noam Rinetzky 1 Code generation for procedure calls (+ a few words on the runtime system) 2 Code generation for procedure calls Compile time

More information

CSCE 5610: Computer Architecture

CSCE 5610: Computer Architecture HW #1 1.3, 1.5, 1.9, 1.12 Due: Sept 12, 2018 Review: Execution time of a program Arithmetic Average, Weighted Arithmetic Average Geometric Mean Benchmarks, kernels and synthetic benchmarks Computing CPI

More information

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

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont ) Chapter 2 Computer Abstractions and Technology Lesson 4: MIPS (cont ) Logical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left >>> srl Bitwise

More information

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6.

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6. Code Generation Dragon: Ch 7. 8. (Just part of it) Holub: Ch 6. Compilation Processes Again Choice of Intermediate Code Representation (IR) IR examples Parse tree Three address code (e.g., x := y op z)

More information

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

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine Machine Language Instructions Introduction Instructions Words of a language understood by machine Instruction set Vocabulary of the machine Current goal: to relate a high level language to instruction

More information

CSE 504: Compiler Design. Runtime Environments

CSE 504: Compiler Design. Runtime Environments Runtime Environments Pradipta De pradipta.de@sunykorea.ac.kr Current Topic Procedure Abstractions Mechanisms to manage procedures and procedure calls from compiler s perspective Runtime Environment Choices

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #9: MIPS Procedures 2006-07-11 CS 61C L09 MIPS Procedures (1) Andy Carle C functions main() { int i,j,k,m;... i = mult(j,k);... m =

More information

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1 CSE P 501 Compilers x86 Lite for Compiler Writers Hal Perkins Autumn 2011 10/25/2011 2002-11 Hal Perkins & UW CSE J-1 Agenda Learn/review x86 architecture Core 32-bit part only for now Ignore crufty, backward-compatible

More information

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

W4118: PC Hardware and x86. Junfeng Yang

W4118: PC Hardware and x86. Junfeng Yang W4118: PC Hardware and x86 Junfeng Yang A PC How to make it do something useful? 2 Outline PC organization x86 instruction set gcc calling conventions PC emulation 3 PC board 4 PC organization One or more

More information

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008 Math 230 Assembly Programming (AKA Computer Organization) Spring 2008 MIPS Intro II Lect 10 Feb 15, 2008 Adapted from slides developed for: Mary J. Irwin PSU CSE331 Dave Patterson s UCB CS152 M230 L10.1

More information

Procedure Call and Return Procedure call

Procedure Call and Return Procedure call Procedures int len(char *s) { for (int l=0; *s!= \0 ; s++) l++; main return l; } void reverse(char *s, char *r) { char *p, *t; int l = len(s); reverse(s,r) N/A *(r+l) = \0 ; reverse l--; for (p=s+l t=r;

More information

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

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types 1 Lecture 08 Introduction to the MIPS ISA + Procedure Calls in MIPS Longer instructions = more bits to address registers MIPS Datapath 6 bit opcodes... 2 MIPS Instructions are 32 bits More ways to address

More information

CS 2210 Programming Project (Part IV)

CS 2210 Programming Project (Part IV) CS 2210 Programming Project (Part IV) April 25, 2018 Code Generation This project is intended to give you experience in writing a code generator as well as bring together the various issues of code generation

More information

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

Implementing Procedure Calls

Implementing Procedure Calls 1 / 39 Implementing Procedure Calls February 18 22, 2013 2 / 39 Outline Intro to procedure calls Caller vs. callee Procedure call basics Calling conventions The stack Interacting with the stack Structure

More information

MIPS Functions and the Runtime Stack

MIPS Functions and the Runtime Stack MIPS Functions and the Runtime Stack COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline

More information

Winter Compiler Construction T10 IR part 3 + Activation records. Today. LIR language

Winter Compiler Construction T10 IR part 3 + Activation records. Today. LIR language Winter 2006-2007 Compiler Construction T10 IR part 3 + Activation records Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University Today ic IC Language Lexical Analysis Syntax Analysis

More information

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (! Lecture Outline Code Generation (II) Adapted from Lectures b Profs. Ale Aiken and George Necula (UCB) Allocating temporaries in the Activation Record Let s optimie cgen a little Code generation for OO

More information

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout Tiger Runtime Environments Compile-time environments are just symbol tables; they are used to assist static semantic analysis, code generation and code optimization. Run-time environments are about how

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture MIPS Functions July 1, 2014 Review I RISC Design Principles Smaller is faster: 32 registers, fewer instructions Keep it simple: rigid syntax, fixed instruction length MIPS Registers: $s0-$s7,$t0-$t9, $0

More information

Lectures 3-4: MIPS instructions

Lectures 3-4: MIPS instructions Lectures 3-4: MIPS instructions Motivation Learn how a processor s native language looks like Discover the most important software-hardware interface MIPS Microprocessor without Interlocked Pipeline Stages

More information

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code Code Generation Intermediate Code? Assembly Code Object Code (Machine Code) 1 Intermediate Code P-Code Three Address Code 2 Compiler Construction F6S 1 Intermediate Representation Abstract Syntax Tree

More information

Implementing Functions at the Machine Level

Implementing Functions at the Machine Level Subroutines/Functions Implementing Functions at the Machine Level A subroutine is a program fragment that... Resides in user space (i.e, not in OS) Performs a well-defined task Is invoked (called) by a

More information

CS 316: Procedure Calls/Pipelining

CS 316: Procedure Calls/Pipelining CS 316: Procedure Calls/Pipelining Kavita Bala Fall 2007 Computer Science Cornell University Announcements PA 3 IS out today Lectures on it this Fri and next Tue/Thu Due on the Friday after Fall break

More information

231 Spring Final Exam Name:

231 Spring Final Exam Name: 231 Spring 2010 -- Final Exam Name: No calculators. Matching. Indicate the letter of the best description. (1 pt. each) 1. address 2. object code 3. condition code 4. byte 5. ASCII 6. local variable 7..global

More information

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d CPSC 213 Introduction to Computer Systems Unit 1d Static Control Flow 1 Reading Companion 2.7.1-2.7.3, 2.7.5-2.7.6 Textbook 3.6.1-3.6.5 2 Control Flow The flow of control is the sequence of instruction

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 10 Activation Records THEORY OF COMPILATION EranYahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec10.pptx Reference: Dragon 7.1,7.2. MCD 6.3,6.4.2 1 You are here Compiler txt Source Lexical

More information

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

CSC 2400: Computer Systems. Using the Stack for Function Calls CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

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.

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. 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.5 6 compute jump/branch targets memory PC +4 new pc Instruction Fetch

More information

Lec 10: Assembler. Announcements

Lec 10: Assembler. Announcements Lec 10: Assembler Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University Announcements HW 2 is out Due Wed after Fall Break Robot-wide paths PA 1 is due next Wed Don t use incrementor 4 times

More information

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

More information

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

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives: CPS311 Lecture: Procedures Last revised 9/9/13 Objectives: 1. To introduce general issues that any architecture must address in terms of calling/returning from procedures, passing parameters (including

More information

EE 361 University of Hawaii Fall

EE 361 University of Hawaii Fall C functions Road Map Computation flow Implementation using MIPS instructions Useful new instructions Addressing modes Stack data structure 1 EE 361 University of Hawaii Implementation of C functions and

More information

Instruction Set Principles and Examples. Appendix B

Instruction Set Principles and Examples. Appendix B Instruction Set Principles and Examples Appendix B Outline What is Instruction Set Architecture? Classifying ISA Elements of ISA Programming Registers Type and Size of Operands Addressing Modes Types of

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18 General issues Section 9.1 Target language: absolute machine language all addresses refer to actual addresses program placed in a fixed location in memory relocatable machine language (object modules)

More information

Object Oriented Languages. Hwansoo Han

Object Oriented Languages. Hwansoo Han Object Oriented Languages Hwansoo Han Object-Oriented Languages An object is an abstract data tpe Encapsulates data, operations and internal state behind a simple, consistent interface. z Data Code Data

More information

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE 1 ARCHITECTURE MODEL The basic instruction set of a computer is comprised of sequences of REGISTER TRANSFERS. Example: Add A, B, C Register B # A

More information

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro Math 230 Assembly Programming (AKA Computer Organization) Spring 2008 MIPS Intro Adapted from slides developed for: Mary J. Irwin PSU CSE331 Dave Patterson s UCB CS152 M230 L09.1 Smith Spring 2008 MIPS

More information

LECTURE 19. Subroutines and Parameter Passing

LECTURE 19. Subroutines and Parameter Passing LECTURE 19 Subroutines and Parameter Passing ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments behind a simple name. Data abstraction: hide data

More information

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.

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. CSC258 Week 10 Logistics 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. Quiz review A word-addressable RAM

More information

Monday, September 28, 2015

Monday, September 28, 2015 Monda, September 28, 2015 Topics for toda Chapter 6: Mapping High-level to assembl-level The Pep/8 run-time stack (6.1) Stack-relative addressing (,s) SP manipulation Stack as scratch space Global variables

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"

More information

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

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring 2009 Topic Notes: MIPS Programming We spent some time looking at the MIPS Instruction Set Architecture. We will now consider

More information

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100 ECE 2035(B) Programming for Hardware/Software Systems Fall 2013 Exam Two October 22 nd 2013 Name: Q1: /20 Q2: /30 Q3: /24 Q4: /26 Total: /100 1/6 For functional call related questions, let s assume the

More information

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

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer Computer Architecture Chapter 2-2 Instructions: Language of the Computer 1 Procedures A major program structuring mechanism Calling & returning from a procedure requires a protocol. The protocol is a sequence

More information

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d CPSC 213 Introduction to Computer Systems Unit 1d Static Control Flow 1 Reading Companion 2.7.1-2.7.3, 2.7.5-2.7.6 Textbook 3.6.1-3.6.5 2 Control Flow The flow of control is the sequence of instruction

More information

CPSC 213. Introduction to Computer Systems. Readings for Next 2 Lectures. Loops (S5-loop) Control Flow. Static Control Flow. Unit 1d.

CPSC 213. Introduction to Computer Systems. Readings for Next 2 Lectures. Loops (S5-loop) Control Flow. Static Control Flow. Unit 1d. Readings for Next 2 Lectures Textbook CPSC 213 Condition Codes - Loops 3.6.1-3.6.5 Introduction to Computer Systems Unit 1d Static Control Flow Control Flow 1 Loops (S5-loop) 2 The flow of control is the

More information

Instructions: Assembly Language

Instructions: Assembly Language Chapter 2 Instructions: Assembly Language Reading: The corresponding chapter in the 2nd edition is Chapter 3, in the 3rd edition it is Chapter 2 and Appendix A and in the 4th edition it is Chapter 2 and

More information

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA. Today s topics CS/COE1541: Introduction to Computer Architecture MIPS operations and operands MIPS registers Memory view Instruction encoding A Review of MIPS ISA Sangyeun Cho Arithmetic operations Logic

More information

MIPS Procedure Calls. Lecture 6 CS301

MIPS Procedure Calls. Lecture 6 CS301 MIPS Procedure Calls Lecture 6 CS301 Function Call Steps Place parameters in accessible location Transfer control to function Acquire storage for procedure variables Perform calculations in function Place

More information