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

Similar documents
AVR and MeggyJr Simple

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

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

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

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

CSE Lecture In Class Example Handout

The Activation Record (AR)

Implementing Procedure Calls

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Run-time Environments

Run-time Environments

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

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

SPIM Procedure Calls

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

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

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

MIPS Functions and Instruction Formats

G Programming Languages - Fall 2012

Code Generation. Lecture 12

Course Administration

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

CS 316: Procedure Calls/Pipelining

CS453 Visitor patterns Type checking and Code Generation

Code Generation. Lecture 19

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

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

MIPS Procedure Calls. Lecture 6 CS301

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

Compilers and computer architecture: A realistic compiler to MIPS

CIT Week13 Lecture

System Software Assignment 1 Runtime Support for Procedures

Subprograms, Subroutines, and Functions

CS64 Week 5 Lecture 1. Kyle Dewey

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

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

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

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

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

CS453 Register Allocation

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

Anne Bracy CS 3410 Computer Science Cornell University

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

CS61C : Machine Structures

Run-time Environment

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

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

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Assembly Language: Function Calls

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

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

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

Assembly Language: Function Calls" Goals of this Lecture"

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

CS453 CLASSES, VARIABLES, ASSIGNMENTS

Project Compiler. CS031 TA Help Session November 28, 2011

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

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

Assembly Language: Function Calls" Goals of this Lecture"

MIPS Functions and the Runtime Stack

Lecture 5: Procedure Calls

ECE232: Hardware Organization and Design

Chapter 10 Memory Model for Program Execution. Problem

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

Functions in MIPS. Functions in MIPS 1

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Anne Bracy CS 3410 Computer Science Cornell University

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.

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

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Wednesday, October 15, 14. Functions

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

Assembly Programming (III)

Lecture 7: Procedures and Program Execution Preview

Storage in Programs. largest. address. address

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

Control Instructions

Lecture 5: Procedure Calls

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Procedures and Stacks

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

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

Implementing Classes, Arrays, and Assignments

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

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

Lecture 7: Binding Time and Storage

Subprograms: Arguments

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

Procedure Call. Procedure Call CS 217. Involves following actions

Run-time Environments - 2

Data Structure Layout. In HERA/Assembly

CSE Lecture In Class Example Handout

Lec 10: Assembler. Announcements

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value

Hakim Weatherspoon CS 3410 Computer Science Cornell University

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

Transcription:

Plan PA3 and PA4 Look at PA3 peer reviews and some code. PA3 demos Make sure to indicate group(s) you were in and groups you are building off of in README. You must cite other people s code if you use it! Cannot just turn in another group s PA3. Must implement some PA4 features. Regression testing: Demo of how to use the regress.sh script. Code generation for function/method calls and definitions Can do MOST of the code generation before having a symbol table Analyze what nodes in the AST are affected Examples CS453 Lecture Code Generation for Method Calls 1

Regress.sh script Setup Create a Test directory with some name (TestPA4Compiler/) Copy the regress.sh script into TestPA4Compiler/ and make it executable Create a stack project somewhere else: stack new MJCPA4 simple Edit the cabal file to include containers and have exec be mjc stack build and then stack install Copy mjc binary into TestPA4Compiler/ Copy MJSIM.jar into same directory. Create a WorkingTestCases/ directory in TestPA4Compiler/. Put some test cases into WorkingTestCases/. Put a copy of the meggy/ Java-only sub directory into TestPA4Compiler/../regress.sh CS453 Lecture Predictive Parsing 2

/** RecursiveCount Example in MeggyJava *Recursively put BLUE pixels in (2,), (1,), (,) */ import meggy.meggy; class RecursiveCount { public static void main(string[] whatever){ new Foo().count((byte)); class Foo { public void count(byte p) { // if haven t reached 2, // recursively call on count + 1 // call setpixel at (p,) CS453 Lecture Code Generation for Method Calls 3

Recall AVR-GCC Calling Convention Calling Convention for AVR-GCC Pass parameters in registers r24, r25 for parameter 1 r22, r23 for parameter 2 r16, r17 for parameter 5 r8, r9 for parameter 9 Pass return values in register(s), r24, r25 Call and return instructions implicitly store and use return address on stack Push and pop keep track of the stack pointer, which points at next open slot Frame pointer is managed internally by each function CS453 Lecture Code Generation for Method Calls 4

Code generation for Function/Method Calls Already did code generation for Meggy.setPixel() Meggy.delay() Meggy.checkButton() Meggy.getPixel() How did the above work? How did we know the types for the actual argument expressions? How can we know the types for user-defined functions? Return value? What are the relevant AST nodes for method/function calls? CS453 Lecture Code Generation for Method Calls 5

call, return, return address Call and return instructions manipulate the RTS implicitly. A call instruction: RA: call clnmfnm pushes RA (return address) on the RTS and jumps to clnmfnm. A return instruction: ret pops the return address off the RTS and jumps to it. CS453 Lecture Code Generation for Method Calls 6

Stack Pointer vs Frame Pointer Stack Pointer used to evaluate expressions moves around while executing a function body points at first available open slot in the Run Time Stack Frame Pointer used to address parameters and locals does not vary during the execution of a function body gets updated at the beginning of a method call and reset and end Notice that Run Time Stack actually grows Down in memory (in spite of pictures on following slides), so when offsetting off frame pointer use Y+1, Y+2 for this, Y+3 for first parameter if byte, Y+3 and Y+4 if int, etc. CS453 Lecture Code Generation for Method Calls 7

Outline of Code to Generate at a Function Call # for each actual expression, pop it from the run-time stack into # appropriate register(s) for parameter pass pop r?? pop r??... # call the function call classnamefuncname à next sequential instruction = return address # If we are an expression, then push the return value # onto the stack. push r25 # only have this if have a 2 byte return value push r24 CS453 Lecture Code Generation for Method Calls 8

Code Generation at the Method/Function Definitions Where should the code be generated for method/function definitions?.text.global methodname.type methodname, @function methodname: # push callers frame pointer push r29 push r28 # make room for parameter(s) (and locals) ldi r24, push r24 # store parameters into stack... # make callee s frame pointer copy of stack pointer in r28, SP_L in r29, SP_H CS453 Lecture Code Generation for Method Calls 9

Code Generation at the Method/Function Definitions Epilogue # handle return value # pop parameters off stack # restore the frame pointer # return ret.size methodname,.-methodname CS453 Lecture Code Generation for Method Calls 1

Calling convention Caller: 1. gather actual params on the RTS - push receiver (receiver = this in callee) - eval and push explicit parameters 2,3, 2. call - pop actuals in reg (pair)s - call fname (fname = classname+funcname) 3. on return (1) push return value on stack Callee: 1. push old FP (r28, r29) 2. make space for frame multiple push -s 3. copy SP à FP in r28, SP_L in r29, SP_H 4. populate frame (Reg à Y+offset) 5. execute body may push return value 6. may get return value into r24(25) 7. clear frame space (undo 2) 8. pop FP into r28,r29 9. ret CS453 Lecture Code Generation for Classes and Variables 11

PA4simple.java example: call mfp 1. caller pushes actual params new C() =, (byte) 3 (byte)7 (byte)blue new C().setP((byte)3,(byte)7,Meggy.Color.BLUE); and pops them into r18: BLUE r2: 7 r22: 3 r24(25) newc() 2. caller performs RA: SP CALL CsetP SP mfp CsetP: callee 1. saves mfp on RTS 2. makes space for parameters by pushing SP, FP -s 3. copies SP to FP mfp callee 4. populates stack frame 5. executes body mfp RA ( 6. in case of return pops ret expr into r24(25) ) 3 7 BLUE mfp CS453 Lecture Code Generation for Classes and Variables 12 RA FP

PA4simple.java example: Returning control to caller 3 7 SP, FP SP mfp BLUE mfp RA callee (. in case of return pops return expr and puts it in r24(25) ) 1. pops frame off RTS exposing mfp 2. pops mfp into FP exposing RA 3. executes ret, which pops caller 1. resumes execution at RA (2. If return value pushes it on stack.) RA and jumps to it CS453 Lecture Code Generation for Classes and Variables 13

/** RecursiveCount Example in MeggyJava *Recursively put BLUE pixels in (2,), (1,), (,) */ import meggy.meggy; class RecursiveCount { public static void main(string[] whatever){ new Foo().count((byte)); class Foo { public void count(byte p) { if (p<2) { this.count(p+1); Meggy.setPixel(p,(byte),Meggy.Color.BLUE); CS453 Lecture Code Generation for Method Calls 14

Summation Example in MeggyJava import meggy.meggy; class RecursiveSum { public static void main(string[] whatever){ Meggy.setPixel((byte)(new Foo().sum(3)),...); class Foo { public int sum(int x) { int result; if (<x) { result = this.sum(p-1) + x; else { result = ; return result; CS453 Lecture Code Generation for Method Calls 15

CS453 Lecture Code Generation for Method Calls 16

Visualize RTS, heap Visualize the run-time stack for RecursiveCount example. Recursively put BLUE pixels in (2,), (1,), (,). Do it, do it. Every call has an implicit first parameter this: the receiver object associated with the call. In PA4 this is just a place holder (no instance variables or locals yet). In PA5 heap objects of type C contain instance variables. Heap and RTS: RTS has locals that may refer to heap objects: C x = new C(init); Issues you don t need to worry about: RTS overflow, Garbage collection C object on heap C x RTS CS453 Lecture Code Generation for Method Calls 17