System Software Assignment 1 Runtime Support for Procedures

Similar documents
CA Compiler Construction

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Run-time Environments - 2

Run-time Environments

Run-time Environments

G Programming Languages - Fall 2012

Wednesday, October 15, 14. Functions

Procedure and Object- Oriented Abstraction

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

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

CIT Week13 Lecture

Run-Time Environments

Systems I. Machine-Level Programming V: Procedures

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

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

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Implementing Subprograms

THEORY OF COMPILATION

Topic 7: Activation Records

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

Implementing Subroutines. Outline [1]

Chapter 10. Implementing Subprograms ISBN

Lecture08: Scope and Lexical Address

Principles of Programming Languages

Run-time Environment

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

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

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

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Implementing Subprograms

Assembly III: Procedures. Jo, Heeseung

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

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

CS 314 Principles of Programming Languages. Lecture 13

Programming Languages: Lecture 12

Course Administration

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

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

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

Functions and Procedures

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

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

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

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

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

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

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

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

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

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

Procedure Call. Procedure Call CS 217. Involves following actions

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

CSE 3302 Notes 5: Memory Management

Today's Topics. CISC 458 Winter J.R. Cordy

Compilers and Code Optimization EDOARDO FUSELLA

Procedure-Calling Conventions October 30

CSCI Compiler Design

Code Generation & Parameter Passing

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

Lexical Considerations

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

CS429: Computer Organization and Architecture

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

CSCI565 Compiler Design

CS213. Machine-Level Programming III: Procedures

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

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS558 Programming Languages

Memory Management and Run-Time Systems

Machine Programming 3: Procedures

MIPS Functions and the Runtime Stack

Lexical Considerations

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

CSc 520 Principles of Programming Languages

Concepts Introduced in Chapter 7

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.

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

1 Lexical Considerations

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

Run-time Environments - 3

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

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

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

ECE260: Fundamentals of Computer Engineering

Chapter 10 Memory Model for Program Execution. Problem

Programming Languages

Scope, Functions, and Storage Management

Procedures: Advanced Topics Chapter 12

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

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

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

Code Generation. Lecture 12

Storage in Programs. largest. address. address

Transcription:

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 a language to handle the access to data in outer procedures. In each procedure stack frame we store an additional pointer (the static link or SL) to mark where the outer procedure stack frame is (note that this is not always the same as the caller). local variables dynamic link return address static link (SL) parameters Figure 1: Procedure activation frame. Figure 1 shows the procedure activation frame for an Oberon (or Pascal) procedure: in addition to the dynamic link and return address it includes the static link pointer to the outer procedure. Figure 2 shows a sample program with several levels of nested procedures. Figure 3 shows the stack layout after calling the procedures S, Q, Q, P, and E. The dynamic link pointer chain (formed by the frame pointers) links the frames in the activation order (E P Q Q S) while the static link pointer chain links the frames to their parent, or enclosing directory (E S, P Q, and Q S). Table 1 shows some examples procedure calls and accesses to local variables at various levels. 2. In Oberon only global procedures can be used as value for procedure variables (e.g., VAR proc var: PROCEDURE foo(i: INTEGER);). Explain, from the run-time system point of view, the reasons to introduce such a limitation. A local procedure assumes a context to be available through the static link (SL) pointer, but when invoked from a procedure variable the context may not exists; to avoid a security hole in the system, such procedures cannot be passed as variable. Figure 4 depicts a simple module with a procedure variable x and two nested procedures P and Q. In the procedure P we assign Q to the global variable x and after the execution of P x correspond to the inner procedure Q. This does not make any sense since Q only exists in the scope of Q. 1

MODULE Test; (* level 0 *) PROCEDURE S(); (* level 1 *) VAR a: INTEGER; PROCEDURE E(); (* level 2 *) (* the stack coming from *) (* S, Q, Q, P is depicted *) (* on the right *) END E; PROCEDURE Q(); (* level 2 *) VAR k: INTEGER; PROCEDURE P(); (* level 3 *) VAR j: INTEGER; E() END P; S a: INTEGER E Q k: INTEGER P j: INTEGER IF b THEN Q() ELSE P() END END Q; Q() END S; END Test. Figure 2: Nested procedures example. 2

E P Q Q S dynamic link static link Figure 3: Stack layout. Table 1: Local variables access examples Calling convention Access convention one level deeper: S Q S: a := 42; push FP mov a[fp], 42 call Q same level: E Q E: a := 42; push 8[FP] mov R0, 8[FP] call Q mov a[r0], 42 one level higher: P Q P: a := 42; mov R0, 8[FP] mov R0, 8[FP] push 8[R0] mov R1, 8[R0] call Q mov a[r1], 42 3

MODULE Test; VAR x: PROCEDURE(); PROCEDURE P(); PROCEDURE Q(); END Q; x := Q (* this is an illegal statement *) END P; P(); (* now x points to an invalid location! *) END Figure 4: Function pointer example. 4

Exercise 2: C calling conventions The C language supports open parameter lists (e.g., void foo(int a, int b,...);), in other words: the number of arguments is not fix and can be larger than the specified parameters (in our example two or more). 1. Which is the formula to compute the address of the parameters on the stack? Let s look at an example using the printf function (in C) which has an open list of parameters. printf("%i %i %i %i", p0, p1, p2, p3); If we pass the parameters p i left to right (like in Java or Oberon) the address of a parameter will be (Figure 5 shows the stack layout): addr(p i ) := F P + padding + size(p n ) + size(p n 1 ) +... + size(p i+1 ) FP RET pn... p1 p0 Figure 5: Left to right parameter passing. To compute the address of a parameter we need the total number (n) of actual parameters which is unknown at compile time. If we pass the parameters right to left (like in C) the address of a parameter will be (Figure 6 shows the stack layout): addr(p i ) := F P + padding + size(p 0 ) + size(p 1 ) +... + size(p i 1 ) posing no problem at compile time! FP RET p0 p1 p2... Figure 6: Right to left parameter passing. 2. Give the implementation of the actions call and return in pseudocode. 5

The only problem that is generated by the presence of a variable number of parameters is the size of procedure frame is not known at compile time. When a procedure returns, the parameters must be removed from the stack but their is unknown. To avoid this problem the old stack pointer is stored on the stack (as a last hidden parameter) and later used to restore the stack after the called procedure has returned. This is the implementation of call and return with a variable number of parameters (the problem s specific code is highlighted in bold) The only change in the implementation is of course the order of the parameters and the additional storage of the stack pointer. Call Return save registers push parameters (right to left) push SP save PC branch save FP FP := SP allocate locals Remove Locals restore FP restore PC restore the SP (remove parameters) restore registers 6

Exercise 3: Parameter passing 1. Why do compilers typically allocate space for arguments in the stack, even when they pass them in registers? In this way when a parameter has to be written to memory (if and additional register is needed) it can be written in the stack as all the other parameters without having to handle it in a special way. Let s look at an example: on the PowerPC architecture registers 3 to 10 can be used to pass arguments. void foo(int a, int b, int c) { while (a > 0) foo(a-1, b, c); } At the first call of foo the three parameters will be stored to R3-R5. When foo calls itself recursively the registers R3 to R5 are again needed to store the parameters. The old values of R3,R4 and R5 have to be written back to memory. In this case the three registers can safely be written on the stack as any other parameter. 2. How can complex variables (arrays and records) be passed to a procedure. Discuss two possibilities and comment their efficiency A first simple possibility is to copy these structures on the stack. This approach has two major drawbacks: a lot of time is required to copy all the data at every procedure invocation, and huge stack frames are generated (as an example the size of an ARRAY 1024,1024 OF LONGREAL is 8MB). Some languages only allow this type of variables to be passed by reference eliminating the problem. Some other language allow to pass records and arrays by reference read-only: in this way the structures can be passed by reference avoiding the copy. When the callee wants to write on these structures the data is copied to another location (lazy copy) 3. Which is the problem when passing a constant by reference? How could this problem be avoided? If a constant is passed by reference, the callee could modify it since the address of the memory location is passed on the stack and the called procedure cannot be aware of the fact the actual parameter is, in fact, a constant. To avoid the problem a constant should be copied to a temporary location and the address of this temporary location should be passed to the callee. 7

Exercise 4: Return values Functions can return a value to the caller (e.g., PROCEDURE foo(): INTEGER; in Oberon or int foo(); in C). In Oberon it is only possible to return a scalar value or a pointer, other languages like C allow more complex structures to be returned. 1. Describe a simple way to return a value from the callee to the caller in Oberon (scalars and pointers only). When only scalars and pointers can be returned the maximum size of the returned variable is known and fix (normally four or eight bytes). In this case one or two registers can be reserved to this scope (on PowerPC usually R3, on Intel EAX). 2. Explain what should be done to return complex structures like records and arrays. This can be solved in two ways: either the caller gives the address of the structure that will be used by return to the callee (this corresponds to an hidden parameter), or the callee returns a pointer to a static variable where the structure is stored. 8