Implementing Classes, Arrays, and Assignments

Similar documents
Implementing Classes and Assignments

CS453 CLASSES, VARIABLES, ASSIGNMENTS

CS453 Arrays in Java in general

Project Compiler. CS031 TA Help Session November 28, 2011

LECTURE 3. Compiler Phases

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

AVR and MeggyJr Simple

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS453 Visitor patterns Type checking and Code Generation

CS 230 Programming Languages

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Pointers II. Class 31

Syntax Errors; Static Semantics

CS 432 Fall Mike Lam, Professor. Code Generation

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Intermediate Code Generation

Intermediate Formats. for object oriented languages

Intermediate Code Generation

Intermediate Formats. for object oriented languages

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions

ECEN 449 Microprocessor System Design. Review of C Programming

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Semantic actions for declarations and expressions. Monday, September 28, 15

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

CIT Week13 Lecture

CS453 Compiler Construction

Lecture 3: C Programm

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

In Java we have the keyword null, which is the value of an uninitialized reference type

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

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Run-time Environments - 2

CS111: PROGRAMMING LANGUAGE II

Intermediate Formats. Martin Rinard Laboratory for Computer Science

Nested Loops. A loop can be nested inside another loop.

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Programming refresher and intro to C programming

Types II. Hwansoo Han

Array. Prepared By - Rifat Shahriyar

CSCE 314 Programming Languages. Type System

CS 536 Midterm Exam Spring 2013

However, in C we can group related variables together into something called a struct.

CS 415 Midterm Exam Spring 2002

ECE 2400 Computer Systems Programming, Fall 2017 Prelim 1 Prep

Final CSE 131B Spring 2004

Contents. 8-1 Copyright (c) N. Afshartous

COS 140: Foundations of Computer Science

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Compilers CS S-05 Semantic Analysis

CSE 401 Final Exam. December 16, 2010

CSE 401/M501 Compilers

CS 11 C track: lecture 5

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Intermediate Representations

CS110D: PROGRAMMING LANGUAGE I

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Data Transfer Instructions

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

CA Compiler Construction

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

Semantic Analysis. Lecture 9. February 7, 2018

Class Information ANNOUCEMENTS

Assembler Programming. Lecture 10

Structures and Pointers

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

The role of semantic analysis in a compiler

Run-Time Data Structures

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

a data type is Types

CS558 Programming Languages

CS2210: Compiler Construction. Code Generation

C++ for Java Programmers

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

Intermediate Programming, Spring 2017*

CSE P 501 Exam 8/5/04

Java Memory Management

SYSC 2006 C Winter 2012

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CSE 303: Concepts and Tools for Software Development

Chapter 5 Names, Bindings, Type Checking, and Scopes

Computer Science II (20082) Week 1: Review and Inheritance

CS201 Some Important Definitions

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

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Runtime Environment. Part II May 8th Wednesday, May 8, 13

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Edward Garcia (ewg2115) Naveen Revanna (nr2443) Niket Kandya (nk2531) Sean Yeh (smy2112)

COS 140: Foundations of Computer Science

Transcription:

Implementing Classes, Arrays, and Assignments Logistics PA4 peer reviews are due Saturday HW9 is due Monday PA5 is due December 5th Will talk about monad implementation at some point, until then check out paper Imperative functional programming by Simon L. Peyton Jones and Philip Wadler if you are curious. Implementing Classes, Arrays, and Assignments (1) Memory model for classes and arrays (2) Type Checking (3) Code Generation CS453 Lecture Code Generation for Classes, Variables, and Arrays 1

PA5 Overview Goals Expansion of lexer, parser, AST, and symbol table for objects, assignment statements, and arrays Type checking for objects, assignment statements, and arrays Code generation for objects, assignment statements, and arrays New pieces of grammar Variable declarations Assignment statements Object creation Member variables Array creation and usage CS453 Lecture Code Generation for Classes, Variables, and Arrays 2

Objects (aka records, structs,...) An object is a collection of data, usually related in some way Each piece of data might have a name (or field name) Haskell data types can use position or names data Point = Pt Float Float -- OR data Point = Pt {pointx, pointy :: Float} pointx :: Point -> Float pointy :: Point -> Float Object memory model Object instances are created with heap allocations. Each object instance places fields in same location. CS453 Lecture Code Generation for Classes, Variables, and Arrays 3

Exercise: draw a memory map (RTS and heap) class PA5obj { class C { public static void main(string[] whatever) { new C().setP((byte)3,(byte)7,Meggy.Color.BLUE); } } Ind oy; public void setp(byte x, byte y, Meggy.Color c) { Ind ox; ox = new Ind(); ox.put(x); oy = new Ind(); oy.put(y); /* Here 3 */ } } class Ind{ byte _i; public void put(byte i){ _i = i; /* Here 1,2 */ } public byte get(){ return _i; } } 1: just after ox.put() has executed (but not returned ) 2: just after oy.put() has executed (but not returned ) 3: just after oy.put() has returned CS453 Lecture Code Generation for Classes, Variables, and Arrays 4

Arrays An array is a collection of items of the same type - so that the address of an element can be computed from the start address and the index (efficiency) - index: int (or int derivative type like unsigned or byte ) Once an array is allocated, the sizes of its dimensions do not change (as opposed to ArrayLists, Lists, ) Java arrays are one dimensional - higher dimensional arrays are arrays of arrays these are sometimes called ragged arrays, as the lengths sub arrays can differ - as opposed to rectangular arrays in Fortran CS453 Lecture Code Generation for Classes, Variables, and Arrays 5

Array representations 1. store length with array elements e.g. at the front of the array - this is nice for Java arrays the array is now represented by its start address - the address of the length field when allocating and indexing in such arrays this length field must be taken into account (added / skipped over) 2. Have a separate array descriptor with index ranges for all dimensions widths in bytes in each dimension (some representation of) start address CS453 Lecture Code Generation for Classes, Variables, and Arrays 6

PA6Rainbow.java class PA6rainbow { public static void main(string[] whatever){{ // display a rainbow on row 5 new Rainbow().run((byte)5); }}} class Rainbow { Meggy.Color [] p; public void run(byte row) { p = new Meggy.Color [8]; } p[0] = Meggy.Color.RED; p[1] = Meggy.Color.ORANGE; p[2] = Meggy.Color.YELLOW; p[3] = Meggy.Color.GREEN; p[4] = Meggy.Color.BLUE; p[5] = Meggy.Color.VIOLET; p[6] = Meggy.Color.WHITE; p[7] = Meggy.Color.DARK; Meggy.setPixel((byte)2, (byte)3, p[0]); Meggy.setPixel((byte)2, (byte)4, p[4]); this.displayrow(row, p); public void displayrow(byte row, Meggy.Color [] a) { int i; i=0; while (i<8) { Meggy.setPixel((byte)i, row, a[i]); } } } i = i+1; CS453 Lecture Arrays 7

Implementing type checking for PA5 MeggyJava Visitor over AST will check for type errors at each AST node Syntax AST node id = Exp ; AssignStatement(id, Exp) [LINENUM,POSNUM] Undeclared variable VARNAME [LINENUM,POSNUM] Invalid expression type assigned to variable VARNAME public Type name(...) {...return Exp; } MethodDecl(name, Stms, Exp) [LINENUM,POSNUM] Invalid type returned from method METHODNAME Exp. name ( Args ) CallExp(name, Args) [LINENUM,POSNUM] Receiver of method call must be a class type [LINENUM,POSNUM] Method METHODNAME does not exist [LINENUM,POSNUM] Method METHODNAME requires exactly NUM arguments [LINENUM,POSNUM] Invalid argument type for method METHODNAME CS453 Lecture Code Generation for Classes, Variables, and Arrays 8

Error message for symbols redeclared within same scope Class ID ClassDecl public Type ID MethodDecl Type ID; VarDecl [LINENUM,POSNUM] Redefined symbol VARNAME CS453 Lecture Code Generation for Classes, Variables, and Arrays 9

Code Gen for Classes and Local variables Method activation records on run-time stack Parameters will still have locations in the activation record. Local variables will also have locations in the activation record. Member variables will be stored in object instances The new expression should cause a call to malloc. Member variables will have offsets within an object instance. The this variable will contain a pointer to the object instance. CS453 Lecture Code Generation for Classes, Variables, and Arrays 10

BuildSymTable for vardecl VarDecl create VarSTE if it is a member variable make the base Z (r31:r30) make the offset the current class offset (ClassSTE will need this) increment the class offset/size with the size of the variable else if it is a local make the base Y (r29:r28) make the offset the current method offset (MethodSTE) increment the method offset/size with the size of the variable CS453 Lecture Code Generation for Classes, Variables, and Arrays 11

Code Generation for method call and this CallExp 1) Use checktypes to determine class type or receiver. 2) Look up the ClassSTE from the receiver type. 3) Then lookup the MethodSTE from the ClassSTE scope. 2) Generate code that pops parameters off the stack and into the appropriate registers from right to left. Receiver reference is the first parameter (this), last pushed. 3) Generate code that calls the mangled method name. 4) Generate code that pushes the return value back on the stack. ThisExp 1) push the value of the "this" parameter onto the run-time stack load this into r31:30 and then push it CS453 Lecture Code Generation for Classes, Variables, and Arrays 12

Code Generation for IdExp and assignstmt IdExp 1) Lookup id in symbol table to get VarSTE 2) If the VarSTE is a member variable 2a) Look up VarSTE for "this" and generate code that loads the value of "this" into registers r31:r30. 3) load variable into a register(s) using the base+offset from VarSTE. 4) Push the variable value on the stack. AssignStatement 1) Lookup id in symbol table to get VarSTE 2) If the VarSTE is a member variable 2a) Look up VarSTE for "this" and generate code that loads the value of "this" into registers r31:r30. 3) store value of expression on top of run-time stack into base+offset from VarSTE CS453 Lecture Code Generation for Classes, Variables, and Arrays 13