1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
1 Code generation Our book's target machine (appendix A): opcode source1, source2, destination add r1, r2, r3 addI r1, c, r2 loadI c, r2 load r1, r2 loadAI.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
10/6: Lecture Topics Procedure call Calling conventions The stack
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
Run time vs. Compile time
Semantics of Calls and Returns
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
The Procedure Abstraction, Part IV Addressability Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
ISBN Chapter 10 Implementing Subprograms.
1 March 16, March 16, 2016March 16, 2016March 16, 2016 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel: (800)
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Computer Architecture & Operations I
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
The Procedure Abstraction Part IV: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights.
Functions and Procedures
Run-Time Environments
Chapter 9 :: Subroutines and Control Abstraction
MIPS Instructions.
Run-Time Environments
Implementing Subprograms
Runtime Environments What is in the memory?.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links are created. –How do we use them to access non-locals? The compiler knows the scope s of a variable The compiler knows the current scope t Follow s-t links

2 Handling nested procedures Method 1 : static (access) links –Setting the 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 to

3 Handling nested procedures Method 2 : Displays –A Display encodes the static link info in an array. –The ith element of the array points to the frame of the most recent procedure at scope level i –How? When a new stack frame is created for a procedure at nesting level i, –save the current value of D[i] in the new stack frame (to be restored on exit) –set D[i] to the new stack frame

4 Handling nested procedures Displays vs. Static links –criteria added overhead space nesting depth frequency of non-local accesses

5 Parameter passing By value –actual parameter is copied By reference –address of actual parameter is stored By value-result –call by value, AND –the values of the formal parameters are copied back into the actual parameters. –Example: int a; void test(int x) { x = 2; a = 0; } int main () { a = 1; test(a);

6 Stack maintenance Calling sequence : –code executed by the caller before and after a call –code executed by the callee at the beginning –code executed by the callee at the end

7 Stack maintenance A typical calling sequence : 1.Caller assembles arguments and transfers control evaluate arguments place arguments in stack frame and/or registers save caller-saved registers save return address jump to callee's first instruction

8 Stack maintenance A typical calling sequence : 2.Callee saves info on entry allocate memory for stack frame, update stack pointer save callee-saved registers save old frame pointer update frame pointer 3.Callee executes

9 Stack maintenance A typical calling sequence : 4.Callee restores info on exit and returns control place return value in appropriate location restore callee-saved registers restore frame pointer pop the stack frame jump to return address 5.Caller restores info restore caller-saved registers

10 Code generation Our book's target machine (appendix A): –opcode source1, source2, destination add r1, r2, r3 addI r1, c, r2 loadI c, r2 load r1, r2 loadAI r1, c, r2 loadAO r1, r2, r3 i2i r1, r2 cmp_LE r1, r2, r3 cbr r1, l1, l2 jump r1

11 Code generation Let's start with some examples. –Generate code from a tree representing x = a+2 - (c+d-4) Issues: –which children should go first? –what if we already had a-c in a register? »Does it make a difference if a and c are floating point as opposed to integer? –Generate code for a case statement –Generate code for w = w*2*x*y*zw = w*2*x*y*z

12 Code generation Code generation = –instruction selection –instruction scheduling –register allocation

13 Instruction selection IR to assembly Why is it an issue? –Example: copy a value from r1 to r2 Let me count the ways... –Criteria –How hard is it? –Use a cost model to choose. –How about register usage?

14 Instruction selection How hard is it? –Can make locally optimal choices –Global optimality is NP-complete Criteria –speed of generated code –size of generated code –power consumption Considering registers –Assume enough registers are available, let register allocator figure it out.

15 Instruction scheduling Reorder instructions to hide latencies. Example: (1) r1 (4) addr1, r1, r1 (5) r2 (8) multr1, r2, r1 (9) r2 (12) mult r1, r2, r1 (13) loadAI r2 (16) multr1, r2, r1 (18) storeAIr1, memory ops : 3 cycles multiply : 2 cycles everything else: 1 cycle

16 Instruction scheduling Reorder instructions to hide latencies. Example: (1) r1 (4) addr1, r1, r1 (5) r2 (8) multr1, r2, r1 (9) r2 (12) mult r1, r2, r1 (13) loadAI r2 (16) multr1, r2, r1 (18) storeAIr1, (1) r1 (2) r2 (3) r3 (4) addr1, r1, r1 (5) multr1, r2, r1 (6) loadAI r2 (7) mult r1, r3, r1 (9) multr1, r2, r1 (11) storeAIr1,

17 Instruction scheduling Reorder instructions to hide latencies. Example2: (1) r1 (4) multr1, r1, r1 (6) mult r1, r1, r1 (8) multr1, r1, r1 (10) storeAIr1,

18 Instruction scheduling Reorder instructions to hide latencies. –We need to collect dependence info Scheduling affects register lifetimes ==> different demand for registers –Should we do register allocation before or after? How hard is it? –more than one instructions may be ready –too many variables may be live at the same time –NP-complete!

19 Register allocation Consists of two parts: –register allocation –register assignment Goal : minimize spills How hard is it? –BB w/ one size of data: polynomial –otherwise, NP-complete based on graph coloring.