CA Compiler Construction

Similar documents
Run-time Environment

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

ECE260: Fundamentals of Computer Engineering

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

ECE232: Hardware Organization and Design

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

System Software Assignment 1 Runtime Support for Procedures

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

Course Administration

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

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

Functions in MIPS. Functions in MIPS 1

Topic 7: Activation Records

Implementing Procedure Calls

Functions and Procedures

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

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

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

Compilers and Code Optimization EDOARDO FUSELLA

SPIM Procedure Calls

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

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

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

Control Instructions

Today. Putting it all together

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

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

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

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

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

Scope, Functions, and Storage Management

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

CSE Lecture In Class Example Handout

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

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.

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

Programming Languages

Concepts Introduced in Chapter 7

Instruction Set Architectures (4)

MIPS Functions and Instruction Formats

Compilation 2014 Activation Records (Part 1)

MIPS Procedure Calls. Lecture 6 CS301

MIPS Functions and the Runtime Stack

CS 314 Principles of Programming Languages. Lecture 13

Run-time Environments - 2

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

G Programming Languages - Fall 2012

Compiling Code, Procedures and Stacks

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

Compilers and computer architecture: A realistic compiler to MIPS

Chapter 2A Instructions: Language of the Computer

Code Generation. Lecture 19

Run-Time Environments

CS A331 Programming Language Concepts

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

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.

Chap. 8 :: Subroutines and Control Abstraction

Quiz for Chapter 2 Instructions: Language of the Computer3.10

Procedure Call and Return Procedure call

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

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

CS 316: Procedure Calls/Pipelining

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

Run-time Environments

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Run-time Environments

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

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

Runtime Environments I. Basilio B. Fraguela

ECE331: Hardware Organization and Design

Anne Bracy CS 3410 Computer Science Cornell University

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

Chapter 9 :: Subroutines and Control Abstraction

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

Compilation 2014 Activation Records

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

EE 109 Unit 15 Subroutines and Stacks

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

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Anne Bracy CS 3410 Computer Science Cornell University

CIT Week13 Lecture

Thomas Polzer Institut für Technische Informatik

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

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

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

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

Midterm II CS164, Spring 2006

Control Abstraction. Hwansoo Han

Run Time Environments

Procedure and Object- Oriented Abstraction

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

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

Procedures and Stacks

Storage in Programs. largest. address. address

Code Generation & Parameter Passing

Code Generation. Lecture 12

Lecture 7: Procedures and Program Execution Preview

CS 61c: Great Ideas in Computer Architecture

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

Transcription:

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 a convention that defined the environment in which a procedure is executed. It can be considered as a contract between the caller and callee. It covers how data is passed from the caller to the callee and how data is returned from the callee to the caller. It also describes how local data is stored and managed in a procedure. allow: separate compilation of procedures; build large programs reasonable compilation times the use of libraries. use system libraries combine procedures written in different languages.

(2) Unfortunately there is no standard Runtime Environment. Each compiler may have its own Runtime Environment, but then you can only link with procedures compiled with the same modules. An Operating System vendor can define a Runtime Environment for its system libraries. Then a compiler vendor either has to use the same Runtime Environment, or write libraries that convert between the different. The key challenges in designing a Runtime Environment are: to handle nested procedures; and be as efficient as possible. are very architecture specific. We will focus on an implementation of a MIPS Runtime Environment to highlight the major aspects of a Runtime Environment. Number Name Purpose MIPS Registers 0 $0 Always contains the constant 0. 1 $at Assembler Temporary. Used by assembler to expand pseudo-ops. 2-3 $v0-$v1 Returned Value of a procedure. If value is contained in one word then only $v0 is significant. 4-7 $0-$a3 Argument Registers. These contain the first 4 arguments of a procedure call. 8-15, 24, 25 $t0-$t9 Temporary Registers 16-23 $s0-$s7 Saved Registers 26-27 $k0-$k1 Kernel Registers. Do not use! 28 $gp Global Pointer. Used to access global static variables. 29 $sp Stack Pointer 30 $fp Frame Pointer 31 $ra Return Address Shaded rows must be preserved across procedure calls.

Stack Frames In most modern programming languages, a procedure can define local variables that are created when the procedure in entered and destroyed when the procedure is exited. If a procedure is nested, particularly if it is recursive, there can be several instances of variable with the same name. Each instance is local to the instance of the procedure that declares it. As these instances of the procedures local variables go out of scope (are destroyed) in last-in/first-out manner, a stack is the natural data structure to use to manage the. Caveat: In languages that support nested functions and functions returned as results (higher-order functions), then a local variable may need to exist after its declaring function has returned. In these situations we can t use stacks. Stack Frames (2) Each time a procedure is called a stack frame is created by the caller. The stack frame contains: Space to store the arguments passed to the procedure. Space to save the saved registers ($s0-$s7). Space to store the procedures return address. arg n-1... (arg3) (arg2) (arg1) (arg0) sp+fsize $fp sp+fsize-4 local data m-1... local data 0 pad $ra $s7... $s0 arg n-1 sp+20... sp+16 arg4 sp+12 (arg3) sp+8 (arg2) sp+4 (arg1) Space for local data. sp (arg0) incoming outgoing previous stack frame current stack frame

Stack Frames (3) Arguments The stack frame contains space to store the arguments passed to the procedure by the current procedure. The first 4 words (arg0, arg1, arg2 and arg3) are not used by the caller as these arguments are passed in registers $a0 to $a3. Space is reserved for them in the stack frame in case the callee needs to spill them. While early (70 s and 80 s) machine passed the arguments on the stack, modern machines use registers as empirical studies have shown that most procedures have 4 or less arguments. But what is the procedure f calls another procedure g? It will have to save its arguments (f s arguments) into the stack frame. Doesn t this lose the time we saved by putting the arguments in registers? Yes, except for leaf procedures (procedures that do not call other procedures) and most procedures are leaf procedures. Stack Frames (4) Callee Saved Registers This is space for the callee to save the values of any of the saved registers ($s0 to $s7) whose value may change during the execution of the procedure. On exit from the procedure, the callee restores the original values of $s0 to $s7. Return Address This is used by the callee to store the value of the return address register, $ra. It is copied back to $ra just before the callee returns. Pad and Local Data This is used for local variables and any temporary registers that need to be preserved across procedure calls. The Pad is inserted before the Local Data to ensure that the Local Data starts on a double word boundary (i.e. that the stack frame size is a multiple of 8). Also the Local Data needs to be padded to ensure its size is also a multiple of 8.

Frame Pointer If function g calls function f, then on entry to f, the stack pointer, $sp, points to the first argument that g passed to f. f s first task is to allocate a stack frame by simply subtracting the framesize from the stack pointer. The existing frame pointer, $fp, is stored in the stack frame and the new $fp then becomes the old stack pointer, $sp, and this is used to access the incoming arguments. When f exists, $fp is copied to $sp and $fp is restored to its previous value. When the framesize is fixed, then we do not need to use a frame pointer since $fp = $sp + framesize and we can calculate the constant offsets to the $sp. However, sometimes the framesize is not known until late in the compilation process when the number of saved registers and temporaries can be determined. But we may need offset to the formal arguments and local variables earlier much earlier, so we put them near the frame pointer at offset that we know. Static Links In some block structured languages (such as C, java, ML, etc.) inner procedures can use variables of outer procedures. In order to enable a procedure to access the variables of its enclosing procedure, whenever a procedure is called it is passed a pointer to the stack frame of the most recent invocation of the function that lexically encloses it. This is called a static link. In order to find an outer variable, it may be necessary to chain through a series of static links. Dynamic links refer to the most recent invocation of the procedure that invoked the current procedure.

Static Links (2) Setting static links links: If the callee is nested directly within the caller, set its static link to point to the caller s frame pointer (or stack pointer). If the callee has the same nesting level as the caller, set its static link to point to wherever the caller s static link points. There are other ways, such as displays and lambda lifting to handle block structured languages. Example Consider the follow code i n t g ( i n t x, i n t y ) { i n t a [ 3 2 ] ; }... ( c a l c u l a t e u s i n g x, y, and a ) a [ 1 ] = f ( y, x, a [ 2 ], a [ 3 ], a [ 4 ] ) ; a [ 0 ] = f ( x, y, a [ 1 ], a [ 2 ], a [ 3 ] ) ; r e t u r n a [ 0 ] ; If only $s0, $s1 and $s3 need to be preserved in g it s stack frame is: sp+156 sp+152 sp+148 sp+144 sp+20 sp+16 sp+12 sp+8 sp+4 sp y x $fp a[31]... a[0] pad $ra $s3 $s1 $s0 arg4 (arg3) (arg2) (arg1) (arg0)

Example (2) The generated MIPS code could be: g : # s t a r t o f p r o l o g u e sw $fp, 4( $sp ) lw $fp, $sp a d d i u $sp, $sp, ( 152) # push s t a c k frame # w/o o u t g o i n g arguments sw $ra, 12( $sp ) # s a v e r e t u r n a d d r e s s sw $s0, 0( $sp ) # s a v e c a l l e e s a v e r e g i s t e r s sw $s1, 4{ $sp ) sw $s3, 8( $sp ) # end o f p r o l o g u e # s t a r t o f t h e body o f p r o c e d u r e g... # s a v e $a0 and $a1 i n c a l l e r s t a c k frame sw $a0, 0( $ f p ) # s a v e $a0 ( v a r i a b l e x ) sw $a1, 4( $ f p ) # s a v e $a1 ( v a r i a b l e y ) Example (3) # f i r s t c a l l to p r o c e d u r e f lw $t0, 36( $sp ) # arg4 i s v a r i a b l e a [ 4 ] sw $t0, 4( $sp ) # p a s s e d i n s t a c k frame lw $a3, 36( $sp ) # arg3 i s v a r i a b l e a [ 3 ] lw $a2, 36( $sp ) # arg2 i s v a r i a b l e a [ 2 ] lw $a1, 0( $ f p ) # arg1 i s v a r i a b l e x lw $a0, 4( $ f p ) # arg0 i s v a r i a b l e y j a l f l a $sp, 20( $sp ) # a d j u s t sp by 4n sw $v0, 24( $sp ) # s t o r e r e s u l t o f f i n t o a [ 1 ]

Example (4) # second c a l l to p r o c e d u r e f lw $t0, 32( $sp ) # arg4 i s v a r i a b l e a [ 3 ] sw $t0, 4( $sp ) # p a s s e d i n s t a c k frame lw $a3, 32( $sp ) # arg3 i s v a r i a b l e a [ 2 ] lw $a2, 32( $sp ) # arg2 i s v a r i a b l e a [ 1 ] lw $a1, 4( $ f p ) # arg1 i s v a r i a b l e y lw $a0, 0( $ f p ) # arg0 i s v a r i a b l e x j a l f l a $sp, 20( $sp ) # a d j u s t sp by 4n sw $v0, 20( $sp ) # s t o r e r e s u l t o f f i n t o a [ 0 ] Example (5) # l o a d r e s u l t s o f g lw $v0, 20( $sp ) # r e s u l t i s a [ 0 ] # end o f t h e body o f p r o c e d u r e g # s t a r t o f e p i l o g u e lw $s0, 0( $sp ) # r e s t o r e c a l l e e s a v e r e g i s t e r s lw $s1, 4( $sp ) lw $s3, 8( $sp ) lw $ra, 12( $sp ) # r e s t o r e r e t u r n a d d r e s s a d d i u $sp, $sp, 152 #pop s t a c k frame lw $fp, 4( $sp ) # r e t o r e frame p o i n t e r j r $ r a