CS A331 Programming Language Concepts

Similar documents
Programming Languages

Chap. 8 :: Subroutines and Control Abstraction

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

Chapter 9 :: Subroutines and Control Abstraction

Control Abstraction. Hwansoo Han

CS A331 Programming Language Concepts

CA Compiler Construction

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

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

G Programming Languages - Fall 2012

Run-time Environment

LECTURE 19. Subroutines and Parameter Passing

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins

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

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

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Implementing Procedure Calls

ECE232: Hardware Organization and Design

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

Instruction Set Architectures (4)

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

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

Functions and Procedures

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

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

button.c The little button that wouldn t

Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly

Anne Bracy CS 3410 Computer Science Cornell University

CS A331 Programming Language Concepts

Lecture 7: Binding Time and Storage

Lecture 7: Procedures and Program Execution Preview

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

Anne Bracy CS 3410 Computer Science Cornell University

System Software Assignment 1 Runtime Support for Procedures

SPIM Procedure Calls

ECE260: Fundamentals of Computer Engineering

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

Concepts Introduced in Chapter 7

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

CS 314 Principles of Programming Languages. Lecture 13

Support for high-level languages

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

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

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

Midterm II CS164, Spring 2006

The Activation Record (AR)

CS 2210 Programming Project (Part IV)

ARM Assembly Programming

Topic 7: Activation Records

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

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

MIPS Functions and Instruction Formats

ARM Assembly Programming

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

Procedures and Stacks

Compilation 2014 Activation Records (Part 1)

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

Anne Bracy CS 3410 Computer Science Cornell University

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Course Administration

Implementing Subroutines. Outline [1]

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

MIPS Procedure Calls. Lecture 6 CS301

CS 61c: Great Ideas in Computer Architecture

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

ECE251: Tuesday September 11

CIT Week13 Lecture

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Plan. Regression testing: Demo of how to use the regress.sh script.

CS153: Compilers Lecture 8: Compiling Calls

Runtime Environments I. Basilio B. Fraguela

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

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

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

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

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

Lecture 5: Procedure Calls

CS 314 Principles of Programming Languages. Lecture 11

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

Systems Architecture The Stack and Subroutines

CS61C : Machine Structures

Principles of Programming Languages

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

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

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Scope: Global and Local. Concept of Scope of Variable

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

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

Compilers and Code Optimization EDOARDO FUSELLA

Language of the Machine Recursive functions

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.

CENG3420 Lecture 03 Review

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

CS64 Week 5 Lecture 1. Kyle Dewey

Transcription:

CS A331 Programming Language Concepts Lecture 9 Subroutines March 17, 2014 Sam Siewert

Purpose of a Subroutine Control Abstraction perform a well defined operation E.g. transform a color image into a graymap Compute the root of a function by Regula Falsi method Sort a list records by key numbers Compared to Data Abstraction represent a collection of information E.g. Structure or record in a list, graph or tree abstract data type Array of intensity data for a color image Look-up table Use of Stack for local variables (function scope) and passing of parameters mapping actual to formal arguments Sam Siewert 2

Subroutine Stack Fundamentals Recall Program Use of Memory Code segment, constants generally read-only Data segment, Global memory global variables, arrays, structures File or module scope Application global (frowned upon) Lives in Data Segment Heap memory allocation during run time E.g. C malloc or API to Heap Manager E.g. C++ new object instantiation Stack segment local variables in a subroutine, parameters Recall scoping rules for local/global variables Pass by value and pass by reference Sam Siewert 3

Subroutine Stack Visibility Subroutines Can Be Module/file global Or, Nested and Local to Another Subroutine Sam Siewert 4

Simple C Local Function Even C Has Simple Locally Visible Functions Modules and Object Orientation Provide More Sophisticated Scoping and Function/Method Visibility Link Error if We Call localfunct() in main() #include <stdio.h> #include <string.h> void globalfunct(void) { } void localfunct(void) { printf("local function\n"); } printf("global function\n"); localfunct(); void main(void) { globalfunct(); // not visible here, so can't call, // but global can // localfunct(); Sam Siewert 5 }

Recall ASM and Machine Frame/Stack Stack address (sp, r13) Program Counter (pc, r15) Frame address (fp) Link Register (lr, r14) Support ARM ABI gcda: Copy Stack Ptr Push return info Argument #1 Argument #2.text.align 2.global gcda.type gcda, %function mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 sub sp, sp, #8 str r0, [fp, #-16] str r1, [fp, #-20] Intra- Procedure Call (ip, r12) Pop return info Return from function END2: sub sp, fp, #12 ldmfd sp, {fp, sp, lr} bx lr Sam Siewert 6

ARM Prolog and Epilog EABI http://www.cse.uaa.alaska.edu/~ssiewert/a331_code/lcm/lcmarm.s http://en.wikipedia.org/wiki/calling_convention#arm r15 is the program counter. r14 is the link register. (The BL instruction, used in a subroutine call, stores the return address in this register). r13 is the stack pointer. r12 is the Intra-Procedure-call scratch register. r4 to r11: used to hold local variables. r0 to r3: used to hold argument values passed to a subroutine, and also hold results returned from a subroutine. If the type of value returned is too large to fit in r0 to r3,, then the caller must allocate space for that value at run time, and pass a pointer in r0. The ARM calling convention mandates using a full-descending stack. In the prolog, push r4 to r11 to the stack to save if used, and push the return address in r14, to the stack. (This can be done with a single STM instruction). copy any passed arguments (in r0 to r3) to the local scratch registers (r4 to r11) or use allocate other local variables to the remaining local scratch registers (r4 to r11) if needed Compute and call other sub-routines using BL instruction (note r0 r3 and r12, r14 are not preserved) Put result in r0 In the epilog, pull r4 to r11 from the stack to restore if needed, and pull the return address to the program counter r15. (This can be done with a single LDM instruction). Sam Siewert 7

Underlying ISA Supports Stack Parameters to subroutines (pass by value or reference) Local variables Temporary variables Scoping Operating System, PL Runtime, and API Libraries Using Globally Reserved Memory Support Heap Runtime Allocation Must Free at Runtime or Count on Garbage Collection Sam Siewert 8

PLP Stack Layout Contents of a stack frame bookkeeping return PC (dynamic link) saved registers line number --- FOR DEBUGGING ( g ) saved display entries [avoid chaining for non-local variables in nested subroutines] Page 383, bottom Page 387, Maintaining the Static Chain Page 389 In More Depth static link arguments and returns local variables Temporaries Static Chain Page 385, Figure 8.1, Examples 8.3 and 8.4 Sam Siewert 9

C Does Not Support Subroutine Nesting PLP, Page 389, In More Depth, Last sentence http://www.cse.uaa.alaska.edu/~ssiewert/a331_code/subroutines/subs p385.c We can define functions inside functions, but what are the visibility and scoping rules? GCC Does Support Nested Functions for C (not C++) - http://gcc.gnu.org/onlinedocs/gcc/nested-functions.html Visual Studio Does not try same code in VS2010 1> nesting-simple.cpp 1>c:\users\ssiewert\documents\visual studio 2010\projects\nesting-simple\nestingsimple\nesting-simple.cpp(16): error C2601: 'localfunct_e_in_a' : local function definitions are illegal 2010\projects\nesting-simple\nesting-simple\nesting-simple.cpp(43): error C3861: 'localfunct_e_in_a': identifier not found 1> 1>Build FAILED. http://c-faq.com/misc/nestfcns.html http://en.wikipedia.org/wiki/nested_function Due to nesting, the scope of the nested function is inside the enclosing function. This means that it can access local variables and other local functions in the enclosing function, while all of this is invisible outside the enclosing function. Sam Siewert 10

E.g. MIPS, ARM ABI Calling Conventions Maintenance of stack is responsibility of calling sequence and subroutine prolog and epilog space is saved by putting as much in the prolog and epilog as possible time may be saved by putting stuff in the caller instead, where more information may be known Common strategy is to divide registers into caller-saves and callee-saves sets caller uses the "callee-saves" registers first "caller-saves" registers if necessary Sam Siewert 11

MIPS Calling Convention (reminder) Caller saves into the temporaries and locals area any callersaves registers whose values will be needed after the call puts up to 4 small arguments into registers $4-$7 (a0-a3) it depends on the types of the parameters and the order in which they appear in the argument list puts the rest of the arguments into the arg build area at the top of the stack frame does jal, which puts return address into register ra and branches note that jal, like all branches, has a delay slot Sam Siewert 12

MIPS Convention From PLP In prolog, Callee subtracts framesize from sp saves callee-saves registers used anywhere inside callee copies sp to fp In epilog, Callee puts return value into registers (mem if large) copies fp into sp (see below for rationale) restores saved registers using sp as base adds to sp to deallocate frame does jra After call, Caller moves return value from register to wherever it's needed (if appropriate) restores caller-saves registers lazily over time, as their values are needed All arguments have space in the stack, whether passed in registers or not Sam Siewert 13

MIPS Convention - PLP Many parts of the calling sequence, prologue, and/or epilogue can be omitted in common cases particularly LEAF routines (those that don't call other routines) leaving things out saves time simple leaf routines don't use the stack - don't even use memory and are exceptionally fast This is Also True for ARM E.g. http://www.cse.uaa.alaska.edu/~ssiewert/a331_c ode/subroutines/ Sam Siewert 14

Exploration Questions Why Does ANSI C not support Nested Functions? Why does GCC support anyway? Why does GCC also not support in C++ too? Does Java Support Nested Functions? C#? Python? Ada? Others? Why are Nested Functions Useful? Sam Siewert 15

Discussion - Need for New PLs? Reflective programming (Java library) Observing and Modifying Programs While Running RTI (Real-Time Innovations Stethoscope - http://www.rti.com/company/news/ss4-3.html ) Kernel Shark for Linux System Code Systemtap Approximate Computing (No Specific Language) Reconfigurable Computing (Verilog and VHDL) Co-Processors (CUDA extension to C, OpenCL) Thomas Standish on Extensible Programming Languages Paraphrase Extension Orthophrase Extension Metaphrase Extension Libraries (not one of the 3, but commonly used) Sam Siewert 16

Parameter Passing Part B Mapping of Actual to Formal Arguments By Value C Default By Value/Result (Copying), Scripting Languages References (Aliases) Using Pointers or Access/Ref Operators in Strongly Typed PLs Closure/name Sam Siewert 17