Functions: flow of control (foc)

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

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Intermediate Code Generation

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

Principles of Compiler Design

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Run Time Environments

Formal Languages and Compilers Lecture X Intermediate Code Generation

Scope, Functions, and Storage Management

Lecture 9: Procedures & Functions. CS 540 George Mason University

TACi: Three-Address Code Interpreter (version 1.0)

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

Functional Programming. Pure Functional Programming

& Simulator. Three-Address Instructions. Three-Address Instructions. Three-Address Instructions. Structure of an Assembly Program.

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

G Programming Languages - Fall 2012

CSc 453 Intermediate Code Generation

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

Virtual Machine Tutorial

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Lecture 5: Procedure Calls

Addressing modes, Procedure calls and the Stack Frame. Eric McCreath

Topic 7: Activation Records

Intermediate Representa.on

ECE232: Hardware Organization and Design

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Memory Management and Run-Time Systems

Run-time Environments - 2

MaMa a simple abstract machine for functional languages

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Page No 1 (Please look at the next page )

Recap: Functions as first-class values

Compiler Construction I

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

Certified Memory Usage Analysis

Implementing Subprograms

IR Generation. May 13, Monday, May 13, 13

Intermediate Representations

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

Intermediate Representations & Symbol Tables

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

Concepts Introduced in Chapter 6

Course Administration

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

Variation of Pointers

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

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

The role of semantic analysis in a compiler

CS2210: Compiler Construction. Code Generation

Three-Address Code IR

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Intermediate Code Generation

Intermediate Code Generation

ECS 142 Project: Code generation hints

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

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

Implementing Subprograms

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Oct 13, 2015 Robert Heckendorn University of Idaho ======================================================================

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Lecture08: Scope and Lexical Address

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

Computer Architecture and Organization. Instruction Sets: Addressing Modes and Formats

Operating Systems Principles. Segmentation & Shared Memory

Variables and Bindings

Lectures Basics in Procedural Programming: Machinery

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Intermediate Code Generation

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

University of Arizona, Department of Computer Science. CSc 520 Assignment 4 Due noon, Wed, Apr 6 15% Christian Collberg March 23, 2005

What is SaM? SaM is a simple stack machine designed to introduce you to compilers in a few lectures SaM I: written by Dr. Keshav Pingali around 2000

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

Concepts Introduced in Chapter 6

Begin at the beginning

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

William Stallings Computer Organization and Architecture 8 th Edition. Chapter 11 Instruction Sets: Addressing Modes and Formats

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

Exam 3 Chapters 7 & 9

Architecture II. Computer Systems Laboratory Sungkyunkwan University

The PCAT Programming Language Reference Manual

CS536 Spring 2011 FINAL ID: Page 2 of 11

MIPS Instruction Set Architecture (2)

CMa simple C Abstract Machine

Quadsim Version 2.1 Student Manual

Intermediate Code Generation

COMP520 - GoLite Type Checking Specification

Intermediate Programming, Spring 2017*

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

Informatica 3 Marcello Restelli

Lecture 6: Assembly Programs

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

Principles of Programming Languages 2017W, Functional Programming

Lecture 5: Procedure Calls

Transcription:

Functions: flow of control (foc) Essentially the handling of functions is a flow of control (foc) problem (use goto) The two additional points are 1. Parameter passing 2. Return value passing function call next instruction halt (return) function return Code space pc 1

Types of Three Address Code Statements (ASU1 Ch 8.1) assignment x := y op z (op: binary / logical operation) assignment x := op y (op: unary / logical operation) copy x := y unconditional jump goto L conditional jump if x relop y goto L function/procedure param x / call p / return y indexed assignment x := y[i] / x[i] := y address assignment x := &y pointer assignment x := *y / *x := y see ASU pp 467-468 for details 2

Environments: Data Space There are 2 aspects to consider 1. The environment where the call takes place 2. The environment of the function For recursive functions (1) and (2) are the same EXCEPT for the initial call For recursive functions there will be a new INSTANCE of the environment on the stack The Symbol Table DESCRIBES the environment The INSTANCES of the environment are in either 1. The static data area - for the program (activation record) 2. The stack - for function calls (stack frame) 3

Function calls The function call is handled in 3 parts 1. Pre call Create (allocate) the stack space Evaluate & copy the actual parameters Save the return address in the stack Save the return value address in the stack Save the address of the calling environment (dynamic pointer) Save the address of the enclosing environment (static pointer) Set the current environment to f s environment (on the stack) 2. Call and execute the function Goto the start of the function code (call p goto) (pc = addr(f)) 3. Post call Set-up the return value Goto the instruction following the function call (return goto) 4

Function Call - example Function definition: int function f(int fp 1, bool fp 2 ); {fn body} Formal parameters fp 1, fp 2 (identifier) Function call (is also an expression) x := f (2 * a, b and c); Actual parameters 2 * a, b and c (expression) t1 := 2*a; t2 := b and c; t3 := f(t1, t2); x := t3; 5

Program & Diagram ST and stack: X A program X y=1, z=2: int int function A(p:int, q:int) h: int; { h = p + q; return h;} yintdo z int DO A int FO p, q int PO h int DO Memory Code A & X AR for X SF for A { y = A(y, z); } DO = Data Object FO = Function Object (CODE) PO = Parameter Object (DATA) 6

Execution sequence 7 106: st = ST(X) 107: ar = AR-X 108: pc = instr 1 X 109: t2 = y 110: aplist += t2 111: t3 = z 112: aplist += t3 113: t4 = A(aplist) 114: st = ST(A) 115: ar = SF-A (alloc) 116: p = t2 117: q = t3 118: rtval = &t4 119: rtaddr = 125 120: sptr = AR-X 121: dptr = AR-X 122: st = st 123: ar = ar 124: pc = instr 1 A 100: t1 = p + q 101: h = t1 102: t4 = h 103: st = parent(st) 104: ar = dptr 105: pc = rtaddr 125: y = t4 126: halt st = symbol table pc = program counter ar = activation record rtval = return value rtaddr = return address sptr = static pointer dptr = dynamic pointer Addresses are symbolic: Assume absolute addresses on execution pc, st, ar registers

Comments 1. 106-108: the pc, st and ar must be initialised on the start of X 2. 109-112: the actual parameters are evaluated 3. 113: is the call to A 4. 114: there are 2 environments, X (st, ar) and A (st, ar ) 5. 115: the stack frame for A is allocated 6. 116-117: the formal parameters are bound 7. 118-121: the stack frame is initialised 8. 122-124: switch to the new environment (A) 9. 100-102: execute A 10. 103-105: prepare the return 11. 125-126: finish the execution of X 8

Code-A = 100 ST-A = 200 AR-A = 800 Code-X = 106 ST-X = 400 AR-X = 600 9 name type role offset name type role offset parent Ptr-ST ptr nil parent Ptr-ST ptr 400 y int var 4 p int param 4 z int var 8 q int param 8 t2 int var 12 h int var 12 t3 int var 16 t1 int ar 16 t4 int var 20 sptr ptr ptr 20 A int fn 100 dptr ptr ptr 24 rtval int val 28 ST-X ST-A rtadd ptr ptr 32

Execution sequence 10 106: st = ST(X) 107: ar = AR-X 108: pc = instr 1 X 109: t2 = y 110: aplist += t2 111: t3 = z 112: aplist += t3 113: t4 = A(aplist) 114: st = ST(A) 115: ar = SF-A (alloc) 116: p = t2 117: q = t3 118: rtval = t4 119: rtaddr = 125 120: sptr = AR-X 121: dptr = AR-X 122: st = st 123: ar = ar 124: pc = instr 1 A 100: t1 = p + q 101: h = t1 102: t4 = h 103: st = parent(st) 104: ar = dptr 105: pc = rtaddr 125: y = t4 126: halt st = symbol table pc = program counter ar = activation record rtval = return value rtaddr = return address sptr = static pointer dptr = dynamic pointer Addresses are symbolic: Assume absolute addresses on execution pc, st, ar registers

Execution sequence 11 106: st = 400 107: ar = 600 108: pc = 109 109: 612 = 604 110: aplist += 612 111: 616 = 608 112: aplist += 616 113: 620 = A(aplist) 114: st = 200 115: ar = 800 (alloc) 116: 804 = 612 117: 808 = 616 118: 828 = 429 119: 832 = 125 120: 820 = 600 121: 824 = 600 122: st = 200 123: ar = 800 124: pc = 100 100: 816 = 804 + 808 101: 812 = 816 102: 620 = 812 103: st = 400 104: ar = 600 105: pc = 125 125: 604 = 620 126: halt st = symbol table pc = program counter ar = activation record rtval = return value rtaddr = return address sptr = static pointer dptr = dynamic pointer Addresses are symbolic: Assume absolute addresses on execution pc, st, ar registers

Comments Aplist (actual parameter list) has to be placed somewhere! The run-time system needs some mechanism for this Aplist = (612, 616) The number of formal parameters (2) is known from ST-A In the call y = A(y, z), y and z represent expressions hence t2 = y, t3 = z The actual/formal parameter binding is by position The alternative would be by name y = A(q = z, p = y) In 113 = A(Aplist) the AR-A needs to be allocated only then can the absolute addresses for variables etc in A be calculated The activation record for X is known at load time 12

Summary The above discussion is a MODEL The main points in a function call/return are Context switch from X to A call Allocate the stack frame (activation record for A AR-A) In AR-A save the return address & return value In AR-A save the static & dynamic pointers Set st, pc and ar (context switch) Context switch from A to X return Copy back the return value Set st, pc and ar (context switch) pc code ar data st describes ar 13