Code generation scheme for RCMA

Size: px
Start display at page:

Download "Code generation scheme for RCMA"

Transcription

1 Code generation scheme for RCMA Axel Simon July 5th, Revised Specification of the R-CMa We detail what constitutes the Register C-Machine (R-CMa ) and its operations in turn We then detail how the different constructs of C are translated 11 The Principle of the R-CMa The R-CMa consists of a code segment, and a unified memory area containing the stack and heap In addition, the R-CMa has two infinite sets of registers, namely: local registers, denoted R 1, R 2, R i,, and global registers, denote R 0, R 1, R j, C S 0 1 PC 0 SP R loc R 1 R glob R 0 R 6 R 4 The local registers are used to keep the contents of function-local variables, for intermediate results during the evaluation of non-trivial expressions and as a backup for parameter variables 1

2 The global registers are used to store parameters that are passed to a function We assume that the calling convention is that all registers are caller-saved, that is, every function must backup its parameters (that are passed in global registers) before calling any other function As a consequence, the global registers are all dead while the body of a function is executing Only when a call is made are they set to pass the arguments to the callee After the call returns, the global register R 0 contains the result Since passing parameters and returning function results is all that global registers are used for, the term global is a bit misleading More accurate names for local and global registers would be intra-procedural and inter-procedural registers, but these names are a bit of mouth full, so we stick to local and global In order to relate variables with memory cells in S and with the two register files, we use the address map ρ : Var {G, L, RG, RL} N which maps each variable to a tag in {G, L, RG, RL} and a number Specifically: ρ x = G, a indicates that x is stored at the address a ρ x = L, a indicates that x is stored at the address F P + a ρ x = RG, a indicates that x is stored in the global register R a (note that a 0 in this case) ρ x = RL, a indicates that x is stored in the local register R a (note that a > 0 in this case) In principle, the location of a variable may change, depending on the current location of the P C, for example, a global variable might be temporarily stored in a local register However, such optimization is not done by the translation presented here Given the address environment ρ, we specify the translation of C statements and expressions using the following three functions: code i s ρ: generate code for the statement s code i R e ρ: generate code that writes the r-value of expression e into R i code i L e ρ: generate code that writes the l-value of expression e into R i Note: During the translation of code, we retain the invariant that all registers with indices j < i are occupied (or, at least, cannot be trivially reused) and that all registers k i are dead Thus, the functions above cannot be called with any i but with the index such that the above invariant holds 2

3 12 Instruction set of the R-CMa We defined the following binary arithmetic instructions We follow the Intel convention of first giving the result register, then the argument registers add R i R j R k sub R i R j R k div R i R j R k mul R i R j R k mod R i R j R k R i R j + R k R i R j R k R i R j /R k R i R j R k R i sgn(r k )k where R j = n R k + k n 0, 0 k < R k le R i R j R k R i if R j < R k then 1 else 0 gr R i R j R k R i if R j > R k then 1 else 0 eq R i R j R k R i if R j = R k then 1 else 0 leq R i R j R k R i if R j R k then 1 else 0 geq R i R j R k R i if R j R k then 1 else 0 and R i R j R k R i R j & R k // bit-wise and or R i R j R k R i R j R k // bit-wise or In addition to the binary operation, we define the following two unary operations: not R i R j R k R i if R j = 0 then 1 else 0 neg R i R j R k R i R j We define the following instructions to load values from the store instruction semantics intuition load R i R j R i S[R j ] load a value from an address loadc R i c R i c load a constant loadrc R i c R i F P + c load a constant relative to FP loada R i c R i S[c] load a global variable loadr R i c R i S[F P + c] load a local variable The instructions for storing values in memory defined as follows We mention the move instruction here to avoid confusion with the move instruction of the original C-Machine where it was only used to copy whole structure For the R-CMa we overload this instruction to also copy the value from one register to another 3

4 instruction semantics intuition store R i R j S[R i ] R j store the value R j at address R i storea c R i S[c] R i write global variable storer c R i S[F P + c] R i write local variable move R i R j R i R j copy a register move k R j [S[SP + i] S[R j + i]] k 1 i=0 copy k values onto the stack SP SP + k In order to implement the control flow of a program, we define the following branch instructions: instruction semantics intuition jump A P C A jump to address A jumpz R i A if R i = 0 then P C A jump if R i = 0 jumpi R i A P C A + R i jump indirectly In order to implement function calls, the corresponding setup of the stack frame and the passing of parameters, we define the following instructions: instruction semantics intuition saveloc R a R b [S[SP + i] R a+i ] (b a) i=0 back registers R a, R b SP SP + (1 + b a) note: if b < a this is a no-op restoreloc R a R b SP SP (1 + b a) restore registers R a, R b [R a+i S[SP + i]] 0 i=(b a) note: if b < a this is a no-op call R i S[SP ] P C; SP SP + 1; P C R i ; F P SP call function at address R i In case parameters must be passed on the stack or if registers need to be spilled onto the stack, we defined the following instructions: instruction semantics intuition push R i S[SP ] R i ; SP SP + 1 push the register R i pop R i R i S[SP ]; SP SP 1 pop into register R i pop k SP SP k pop k values off the stack 4

5 13 Code Generation for the R-CMa 131 Expressions and Assignments Expressions containing binary operators are translated as follows: code i R e 1 op e 2 ρ = code i R e 1 ρ code i+1 R e 2 ρ op R i R i R i+1 This function could be specialized to check if one or both arguments are registers and, in that case, to simply use that register rather than to generate code that stores the value of that register into R i resp R i+1 This optimization already saves a lot of move instructions This optimization also applies to unary operations which, in general, are translated as follows: code i R op e ρ = codei R e ρ op R i R i The value and address of a variable are translated as follows: loada R i a if ρ x = G, a code i R x ρ = loadr R i a if ρ x = L, a move R i R j if ρ x = r, j r {RG, RL} code i L x ρ = { loadc Ri a if ρ x = G, a loadrc R i a if ρ x = L, a Note that the latter function is undefined for a register since a register has no l-value The fact that a register has no l-value manifests itself in the translation of an assignment statement, which is, in general, defined as follows: code i R e 1 = e 2 ρ = code i R e 2 ρ code i+1 L e 1 ρ store R i+1 R i In case the left-hand-side is a register, we need to apply the following special case: 5

6 code i R x = e 2 ρ = code i R e 2 ρ move R j R i Note that the assignment above is an expression In general, an assignment can be derived as a statement, so we have to define: code i e 1 = e 2 ρ = code i R e 1 = e 2 ρ However, this is a special case of the following translation: code i e ρ = code i R e ρ Note here that the result of calculating e is ignored In the translation of the C-Machine we had a pop instruction here to explicitly ignore the result Here, the register R i which holds the result is simply dead in the following code 132 Pointer and Pointer Arithmetic Pointers and pointer arithmetic can be translated using the following definitions: creation of pointers: code i R &e ρ = code i L e ρ reading from a pointer: code i R e ρ = code i L e ρ load R i R i writing to a pointer: code i L e ρ = code i R e ρ 6

7 133 Translation of Control-Flow Translation of if ( c ) tt else ee is as follows: code i if(c) tt else ee ρ = code i R c ρ jumpz R i A code i tt ρ jump B A : code i ee ρ B : The translation of loops is as follows: code i while(e) s ρ = A : code i R e ρ jumpz R i B code i s ρ jump A B : code i for(e 1 ; e 2 ; e 3 ) s ρ = code i R e 1 ρ A : code i R e 2 ρ jumpz R i B code i s ρ code i R e 3 ρ jump A B : A switch statement with a sequence of cases that use consecutive constants, ie: 7

8 switch (e) { case c 0 : s 0 ; break ; } case c k 1 : s k 1 ; break ; d e f a u l t : s ; break ; with c i + 1 = c i+1 for i = [0, k 1] is translated into a body code i s ρ = code i R e ρ check i c 0 c k 1 B A 0 : code i s 0 ρ jump D A k 1 : code i s k 1 ρ jump D a jump table B : jump A 0 C : jump A k 1 and the range-check macro which is defined as follows: check i l u B = loadc R i+1 l geq R i+2 R i R i+1 jumpz R i+2 E sub R i R i R i+1 loadc R i+1 k E : D : geq R i+2 R i R i+1 jumpz R i+2 D loadc R i k jumpi R i B 8

9 134 Function Calls and Return Function calls are translated by evaluating each argument and assigning it to a global register R i Since the arguments may contain other function calls (and indeed the function g may be an expression containing function calls), the global registers can only be set once all arguments and g are evaluated Before the actual call, all local registers that are in use at this point (except for R i ) are backed up onto the stack The registers R i R i+n are not saved since they are not needed after the function call returns Upon the return of the function the saved registers are restored and the result of the function, which is in R 0, is written to R i which is the register into which the result of the function call is to be saved code i R g(e 1, e n ) ρ = code i R g ρ code i+1 R code i+n R e 1 ρ e n ρ move R 1 R i+1 move R n R i+n saveloc R 1 R i 1 mark call R i restoreloc R 1 R i 1 move R i R 0 Note: If additional parameters have to be a passed on the stack (for instance structs) they need to be pushed on the stack using push R i for basic types and move k R i for C structs A return statement is translated as follows: code i return e ρ = code i R e ρ move R 0 R i return In case the function returns no value, the following translation suffices: code i return ρ = return 9

10 135 Whole Functions and Programs A complete function is translated as follows: code 1 R t r f(args){decls ss} ρ = enter q move R l+1 R 1 move R l+n R n code l+n+1 ss ρ return Here, the value q denotes the maximum stack space used by the function If no local variables are stored on the stack and no arguments to functions are ever passed on the stack, then q = 3 which allows for storing the special registers F P, EP and P C Note that a function is always translated so that the first available register is R 1, that is, no register is used so far We assumed that the local variables of the function extend ρ to ρ and take up l local registers, namely R 1, R l The move instructions save all parameters R 1, R n to the function in local registers R l+1, R l+n This ensures that other functions within this function can be called without worrying about the global registers In case the translated function is a leaf function, the global registers do not have to be saved The translation of the body of the function may therefore use temporary registers starting from l + n + 1 The return statement may be omitted if the function returns a value and does so correctly on all of the paths through the function A program P = F 1 ; F n must contain the function F i at address _f i and a single main function at address _main It can be translated as follows: code 1 P ρ = enter (k + 3) alloc k loadc R 1 _main mark _f 1 : _f n : call R 1 halt code 1 F 1 ρ code 1 F n ρ 10

11 Here, k denotes the size of the global variables which are assumed to be all allocated on the stack In principle, it would be possible to allocated global variables in k local registers, but this would require that each function is translated using code k R rather than code1 R We omitted this possibility since global variables normally reside in memory Keeping global variables in registers would make the independent translation of different modules difficult since it is not clear what k is nor is it possible to determine within a single module if the address of a global variable has been taken anywhere in the program (in which case it must reside in memory) Note that the only live local register at the point of the call is R 1 However, this register contains the address of _main which is not needed after the call Thus, no local register needs to be saved and the saveloc and restoreloc instructions can be omitted 11

Compiler Construction I

Compiler Construction I TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Compiler Construction I Dr. Michael Petter, Dr. Axel Simon SoSe 2014 1 / 104 Topic: Semantic Analysis 2 / 104 Topic: Code Synthesis 3 / 104 Generating

More information

Compiler Construction I

Compiler Construction I TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Compiler Construction I Dr. Michael Petter, Dr. Axel Simon SoSe 2013 1 / 108 Organizing Master or Bachelor in the 6th Semester with 5 ECTS Prerequisites

More information

CMa simple C Abstract Machine

CMa simple C Abstract Machine CMa simple C Abstract Machine CMa architecture An abstract machine has set of instructions which can be executed in an abstract hardware. The abstract hardware may be seen as a collection of certain data

More information

Helmut Seidl. Virtual Machines. München. Summer 2014

Helmut Seidl. Virtual Machines. München. Summer 2014 Helmut Seidl Virtual Machines München Summer 2014 1 0 Introduction Principle of Interpretation: Program + Input Interpreter Output Advantage: No precomputation on the program text == no/short startup-time

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

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

SOURCE LANGUAGE DESCRIPTION

SOURCE LANGUAGE DESCRIPTION 1. Simple Integer Language (SIL) SOURCE LANGUAGE DESCRIPTION The language specification given here is informal and gives a lot of flexibility for the designer to write the grammatical specifications to

More information

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

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} Subroutines Also called procedures or functions Example C code: int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} // subroutine converts Celsius to kelvin int celtokel(int i) { return (i

More information

Talen en Compilers. Johan Jeuring , period 2. December 15, Department of Information and Computing Sciences Utrecht University

Talen en Compilers. Johan Jeuring , period 2. December 15, Department of Information and Computing Sciences Utrecht University Talen en Compilers 2016-2017, period 2 Johan Jeuring Department of Information and Computing Sciences Utrecht University December 15, 2016 9 Simple stack machine 9-1 Recap: Semantic functions In the previous

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

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

CODE GENERATION Monday, May 31, 2010

CODE GENERATION Monday, May 31, 2010 CODE GENERATION memory management returned value actual parameters commonly placed in registers (when possible) optional control link optional access link saved machine status local data temporaries A.R.

More information

Intermediate Representations

Intermediate Representations Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address

More information

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

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space Memory Usage A software convention stack

More information

Virtual Machine Tutorial

Virtual Machine Tutorial Virtual Machine Tutorial CSA2201 Compiler Techniques Gordon Mangion Virtual Machine A software implementation of a computing environment in which an operating system or program can be installed and run.

More information

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

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

Module 27 Switch-case statements and Run-time storage management

Module 27 Switch-case statements and Run-time storage management Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss

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

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

Chapter 9 :: Subroutines and Control Abstraction

Chapter 9 :: Subroutines and Control Abstraction Chapter 9 :: Subroutines and Control Abstraction Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

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

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

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

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

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

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12 Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12 Goals for Today Calling Convention for Procedure Calls Enable code to be reused by allowing

More information

Topic 3-a. Calling Convention 2/29/2008 1

Topic 3-a. Calling Convention 2/29/2008 1 Topic 3-a Calling Convention 2/29/2008 1 Calling Convention Calling convention is a standardized method for a program to pass parameters to a procedure and receive result values back from it. 2/29/2008

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

Support for high-level languages

Support for high-level languages Outline: Support for high-level languages memory organization ARM data types conditional statements & loop structures the ARM Procedure Call Standard hands-on: writing & debugging C programs 2005 PEVE

More information

ECE260: Fundamentals of Computer Engineering

ECE260: Fundamentals of Computer Engineering Supporting Nested Procedures James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy Memory Layout

More information

Run-time Environment

Run-time Environment Run-time Environment Prof. James L. Frankel Harvard University Version of 3:08 PM 20-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Storage Organization Automatic objects are

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

Topic 7: Activation Records

Topic 7: Activation Records Topic 7: Activation Records Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Storage Organization Stack Free Memory Heap Static Code 2 ELF file format example Executable Object

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

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

Compiler Architecture

Compiler Architecture Code Generation 1 Compiler Architecture Source language Scanner (lexical analysis) Tokens Parser (syntax analysis) Syntactic structure Semantic Analysis (IC generator) Intermediate Language Code Optimizer

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

Summary: Direct Code Generation

Summary: Direct Code Generation Summary: Direct Code Generation 1 Direct Code Generation Code generation involves the generation of the target representation (object code) from the annotated parse tree (or Abstract Syntactic Tree, AST)

More information

7 Translation to Intermediate Code

7 Translation to Intermediate Code 7 Translation to Intermediate Code ( 7. Translation to Intermediate Code, p. 150) This chpater marks the transition from the source program analysis phase to the target program synthesis phase. All static

More information

Project 3 Due October 21, 2015, 11:59:59pm

Project 3 Due October 21, 2015, 11:59:59pm Project 3 Due October 21, 2015, 11:59:59pm 1 Introduction In this project, you will implement RubeVM, a virtual machine for a simple bytecode language. Later in the semester, you will compile Rube (a simplified

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

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University CS558 Programming Languages Winter 2018 Lecture 4a Andrew Tolmach Portland State University 1994-2018 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program Fall 2006 CS333: Computer Architecture University of Virginia Computer Science Michele Co Announcements Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly Homework #2 Due next Wednesday, Sept.

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

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem: Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd

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

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

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles. Virtual Machine Part II: Program Control Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 8:

More information

Assembly Language Manual for the STACK Computer

Assembly Language Manual for the STACK Computer Computer Science 301 1 Assembly Language Manual for the STACK Computer Assembly language programmers should read the hardware description of the STACK computer together with information about the effect

More information

Compilers and Code Optimization EDOARDO FUSELLA

Compilers and Code Optimization EDOARDO FUSELLA Compilers and Code Optimization EDOARDO FUSELLA Contents Data memory layout Instruction selection Register allocation Data memory layout Memory Hierarchy Capacity vs access speed Main memory Classes of

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

Instruction Set Architectures (4)

Instruction Set Architectures (4) Computer Architecture Week 06 Instruction Set Architectures (4) College of Information Science and Engineering Ritsumeikan University subroutines functions, procedures remember the next instruction s address

More information

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

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS Lectures 5 Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS 1 OOPS - What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; } 2

More information

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture ECE468 Computer Organization & Architecture MIPS Instruction Set Architecture ECE468 Lec4.1 MIPS R2000 / R3000 Registers 32-bit machine --> Programmable storage 2^32 x bytes 31 x 32-bit GPRs (R0 = 0) 32

More information

MaMa a simple abstract machine for functional languages

MaMa a simple abstract machine for functional languages MaMa a simple abstract machine for functional languages Functional Language PuF We will consider a mini-laguage of "Pure Functions" PuF. Programs are expressions e in form: e ::= b j x j ( 1 e) j (e 1

More information

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR.

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR. CS 4120 Lecture 14 Syntax-directed translation 26 September 2011 Lecturer: Andrew Myers We want to translate from a high-level programming into an intermediate representation (IR). This lecture introduces

More information

Practical Malware Analysis

Practical Malware Analysis Practical Malware Analysis Ch 4: A Crash Course in x86 Disassembly Revised 1-16-7 Basic Techniques Basic static analysis Looks at malware from the outside Basic dynamic analysis Only shows you how the

More information

CS453 Register Allocation

CS453 Register Allocation CS453 Register Allocation Quick quiz In C, how can r=(a+b)+(c+d) be evaluated? e.g. as follows? t1 = a+d t2 = b+c r = t1+t2 (page 207 of ANSI and traditional C ref. manual 3 rd edition) How about in Java?

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

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

Compiler Optimization and Code Generation

Compiler Optimization and Code Generation Compiler Optimization and Code Generation Professor: Sc.D., Professor Vazgen elikyan 1 Course Overview ntroduction: Overview of Optimizations 1 lecture ntermediate-code Generation 2 lectures achine-ndependent

More information

Compiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine.

Compiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine. This lecture Compiler construction Lecture 6: Code generation for x86 Magnus Myreen Spring 2018 Chalmers University of Technology Gothenburg University x86 architecture s Some x86 instructions From LLVM

More information

Code Generation & Parameter Passing

Code Generation & Parameter Passing Code Generation & Parameter Passing Lecture Outline 1. Allocating temporaries in the activation record Let s optimize our code generator a bit 2. A deeper look into calling sequences Caller/Callee responsibilities

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XX April 4 th, 2006 1 Roadmap Subroutines Allocation Strategies Calling Sequences Parameter Passing Generic Subroutines Exception Handling Co-routines 2 1 Review

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

Computer Architecture /

Computer Architecture / Computer Architecture 02-201 / 02-601 The Conceptual Architecture of a Computer PC CPU register 0 register 1 register 2 registers hold small amounts of data for processing by the CPU Reading / writing

More information

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

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes Chapter 2 Instructions: Language of the Computer Adapted by Paulo Lopes Instruction Set The repertoire of instructions of a computer Different computers have different instruction sets But with many aspects

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

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

CS143 - Written Assignment 4 Reference Solutions

CS143 - Written Assignment 4 Reference Solutions CS143 - Written Assignment 4 Reference Solutions 1. Consider the following program in Cool, representing a slightly over-engineered implementation which calculates the factorial of 3 using an operator

More information

Control Abstraction. Hwansoo Han

Control Abstraction. Hwansoo Han Control Abstraction Hwansoo Han Review of Static Allocation Static allocation strategies Code Global variables Own variables (live within an encapsulation - static in C) Explicit constants (including strings,

More information

Chap. 8 :: Subroutines and Control Abstraction

Chap. 8 :: Subroutines and Control Abstraction Chap. 8 :: Subroutines and Control Abstraction Michael L. Scott Programming Language Theory 2015, kkman@sangji.ac.kr 1 Review Of Stack Layout Allocation strategies Static Code Globals Own variables Explicit

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

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

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.

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. MIPS Programming This is your crash course in assembler programming; you will teach yourself how to program in assembler for the MIPS processor. You will learn how to use the instruction set summary to

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

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

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

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions Procedure Calls A procedure of a subroutine is like an agent which needs certain information to perform a

More information

CS153: Compilers Lecture 8: Compiling Calls

CS153: Compilers Lecture 8: Compiling Calls CS153: Compilers Lecture 8: Compiling Calls Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 2 out Due Thu Oct 4 (7 days) Project 3 out Due Tuesday Oct 9 (12 days) Reminder:

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2018 Lecture 4 LAST TIME Enhanced our processor design in several ways Added branching support Allows programs where work is proportional to the input values

More information

Instruction Set Architecture

Instruction Set Architecture Computer Architecture Instruction Set Architecture Lynn Choi Korea University Machine Language Programming language High-level programming languages Procedural languages: C, PASCAL, FORTRAN Object-oriented

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

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language CS 4110 Programming Languages & Logics Lecture 35 Typed Assembly Language 1 December 2014 Announcements 2 Foster Office Hours today 4-5pm Optional Homework 10 out Final practice problems out this week

More information

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers Where we are CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 13: Intermediate Code 25 Sep 09 Source code (character stream) Token stream Abstract syntax tree Abstract syntax tree

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

Virtual Machine (Part II)

Virtual Machine (Part II) Harvard University CS 101 Fall 2005, Shimon Schocken Virtual Machine (Part II) Elements of Computing Systems 1 Virtual Machine II (Ch. 8) Where we are at: Human Thought Abstract design Chapters 9, 12 H.L.

More information

This section provides some reminders and some terminology with which you might not be familiar.

This section provides some reminders and some terminology with which you might not be familiar. Chapter 3: Functions 3.1 Introduction The previous chapter assumed that all of your Bali code would be written inside a sole main function. But, as you have learned from previous programming courses, modularizing

More information

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

Lecture 5. Announcements: Today: Finish up functions in MIPS Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In

More information

(Not Quite) Minijava

(Not Quite) Minijava (Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

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

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

Admin CS41B MACHINE. Examples from this lecture. CS41B machine 2/16/16 CPU. David Kauchak CS 52 Spring 2016

Admin CS41B MACHINE. Examples from this lecture. CS41B machine 2/16/16 CPU. David Kauchak CS 52 Spring 2016 Admin! Midterm Thursday! Review question sessions tonight and Wednesday! Assignment 3? CS41B MACHINE! Assignment 4 out soon! Due Monday 2/29 David Kauchak CS 52 Spring 2016 Examples from this lecture CS41B

More information

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

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University. Instructions: ti Language of the Computer Rui Wang, Assistant professor Dept. of Information and Communication Tongji University it Email: ruiwang@tongji.edu.cn Computer Hierarchy Levels Language understood

More information

Levels of Programming. Registers

Levels of Programming. Registers Levels of Programming COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1. Arithmetic Instructions 2.

More information

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas This Lecture G53CMP: Lecture 14 Run-Time Organisation I Henrik Nilsson University of Nottingham, UK One aspect of run-time organisation: stack-based storage allocation Lifetime and storage Basic stack

More information

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37 G53CMP: Lecture 14 Run-Time Organisation I Henrik Nilsson University of Nottingham, UK G53CMP: Lecture 14 p.1/37 This Lecture One aspect of run-time organisation: stack-based storage allocation Lifetime

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

COMPUTER ORGANIZATION AND DESIGN

COMPUTER ORGANIZATION AND DESIGN COMPUTER ORGANIZATION AND DESIGN 5 th The Hardware/Software Interface Edition Chapter 2 Instructions: Language of the Computer 2.1 Introduction Instruction Set The repertoire of instructions of a computer

More information