MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Similar documents
MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Intermediate Formats. Martin Rinard Laboratory for Computer Science

Intermediate Formats. for object oriented languages

Intermediate Formats. for object oriented languages

A Short Summary of Javali

IC Language Specification

(Not Quite) Minijava

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 5 Object-Oriented Programming

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Chapter 14 Abstract Classes and Interfaces

What are the characteristics of Object Oriented programming language?

C++ Important Questions with Answers

Inheritance and object compatibility

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

1. Every program must have at least one class declaration. (*) 2. Every class declared in a program must have a distinct identifier.

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

Semantic Analysis and Type Checking

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Java Fundamentals (II)

2.4 Structuring programs

ENCE 688R Civil Information Systems. The Java Language. Mark Austin.

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

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

Strict Inheritance. Object-Oriented Programming Winter

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Compiler Construction I

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

The SPL Programming Language Reference Manual

Intermediate Code Generation Part II

CSE 431S Type Checking. Washington University Spring 2013

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Reference Grammar Meta-notation: hfooi means foo is a nonterminal. foo (in bold font) means that foo is a terminal i.e., a token or a part of a token.

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Java Primer 1: Types, Classes and Operators

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Types. What is a type?

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

Types, Values and Variables (Chapter 4, JLS)

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Java Object Oriented Design. CSC207 Fall 2014

Derived and abstract data types. TDT4205 Lecture 15

Polymorphism and Interfaces. CGS 3416 Spring 2018

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

OBJECT ORIENTED PROGRAMMING USING C++

Points To Remember for SCJP

Strict Inheritance. Object-Oriented Programming Spring 2008

Inheritance & Polymorphism

Lecture Set 4: More About Methods and More About Operators

CS250 Final Review Questions

Type Checking. Error Checking

Expressions & Assignment Statements

Arrays Classes & Methods, Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Compiler Construction 2009/2010 Polymorphic Types and Generics

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Inheritance and Polymorphism

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

1 Lexical Considerations

Compiler Construction 2016/2017 Polymorphic Types and Generics

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

301AA - Advanced Programming [AP-2017]

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Chapter 6 Introduction to Defining Classes

Operators and Expressions

type conversion polymorphism (intro only) Class class

Basic concepts. Chapter Toplevel loop

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

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

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods. Dan Grossman Autumn 2018

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Lexical Considerations

Extending Classes (contd.) (Chapter 15) Questions:

Lecture Set 4: More About Methods and More About Operators

Chapter 7. Expressions and Assignment Statements ISBN

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

INHERITANCE. Spring 2019

Decaf Language Reference

Pointers. Pointers. Pointers (cont) CS 217

Binding and Variables

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CSE P 501 Exam 8/5/04

CMSC330 Fall 2013 Practice Problems 6 Solutions

Syntax Errors; Static Semantics

The Decaf Language. 1 Lexical considerations

Transcription:

MIT 6.035 Semantic Analysis Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Error Issue Have assumed no problems in building IR But are many static checks that need to be done as part of translation Called Semantic Analysis

Goal of Semantic Analysis Ensure that program obeys certain kinds of sanity checks all used variables are defined types are used correctly method calls have correct number and types of parameters and return value Checked when build IR Driven by symbol tables

Symbol Table Summary Program Symbol Table (Class Descriptors) Class Descriptors Field Symbol Table (Field Descriptors) Field Symbol Table for SuperClass Method Symbol Table (Method Descriptors) Method Symbol Table for Superclass Method Descriptors Local Variable Symbol Table (Local Variable Descriptors) Parameter Symbol Table (Parameter Descriptors) Field Symbol Table of Receiver Class Local, Parameter and Field Descriptors Type Descriptors in Type Symbol Table or Class Descriptors

class descriptor for vector method descriptor for add type symbol table int int [] boolean boolean [] vector [] v add method symbol table field symbol table field descriptor x this i local symbol table code for add method int descriptor array descriptor boolean descriptor array descriptor array descriptor parameter symbol table parameter descriptor this descriptor local descriptor vector class_decl field_decl int v []

Translating from Abstract Syntax Trees to Symbol Tables

Intermediate Representation for Classes class vector { int v[]; } void add(int x) { int i; i = 0; } while (i < v.length) { v[i] = v[i]+x; i = i+1; } class_decl vector field_decl method_decl statements int v add param_decl var_decl int x int i

class_decl vector field_decl method_decl statements class symbol table int v add param_decl var_decl int x int i

class_decl vector field_decl method_decl statements vector class symbol table int v add param_decl var_decl int x int i class descriptor for vector

class_decl vector field_decl method_decl statements vector class symbol table int v add param_decl var_decl int x int i v field descriptor class descriptor for vector

class_decl vector field_decl method_decl statements vector class symbol table int v add param_decl var_decl int x int i v field descriptor class descriptor for vector add this this descriptor Method descriptor for add

class_decl vector field_decl method_decl statements vector class symbol table int v add param_decl var_decl int x int i v field descriptor class descriptor for vector add x this parameter descriptor this descriptor Method descriptor for add

class_decl vector field_decl method_decl statements vector class symbol table int v add param_decl var_decl int x int i v field descriptor class descriptor for vector add x this parameter descriptor this descriptor Method descriptor for add i local descriptor

Intermediate Representation for Code while while (i < v.length) v[i] = v[i]+x; ldl < len ldf ldf sta + ldl lda ldf ldl ldp field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; ldl field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; ldl ldf field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; ldl len ldf field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; < ldl len ldf field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; < ldl len ldf ldf field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; < ldl len ldf ldf ldl field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; < ldl len ldf ldf lda ldl field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; < ldl len ldf ldf lda ldl ldp field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; < + ldl len ldf lda ldf ldl ldp field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; ldl < len ldf ldf + lda ldf ldl ldp field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; ldl < len ldf ldf + ldl lda ldf ldl ldp field descriptor for v local descriptor for i parameter descriptor for x

while (i < v.length) v[i] = v[i]+x; ldl < len ldf ldf sta + ldl lda ldf ldl ldp field descriptor for v local descriptor for i parameter descriptor for x

while while (i < v.length) v[i] = v[i]+x; ldl < len ldf ldf sta + ldl lda ldf ldl ldp field descriptor for v local descriptor for i parameter descriptor for x

while while (i < v.length) v[i] = v[i]+x; ldl < len ldf ldf sta + ldl lda ldf ldl ldp field descriptor for v local descriptor for i parameter descriptor for x

Abbreviated Notation < ldl i while len ldf v ldf v sta + ldl i lda ldp x ldf v ldl i while (i < v.length) v[i] = v[i]+x;

Parameter Descriptors When build parameter descriptor, have name of type name of parameter What is the check? Must make sure name of type identifies a valid type look up name in type symbol table if not there, look up name in program symbol table (might be a class type) if not there, fails semantic check

Local Descriptors When build local descriptor, have name of type name of local What is the check? Must make sure name of type identifies a valid type look up name in type symbol table if not there, look up name in program symbol table (might be a class type) if not there, fails semantic check

Local Symbol Table When build local symbol table, have a list of local descriptors What to check for? duplicate variable names shadowed variable names When to check? when insert descriptor into local symbol table Parameter and field symbol tables similar

Class Descriptor When build class descriptor, have class name and name of superclass field symbol table method symbol table What to check? Superclass name corresponds to actual class No name clashes between field names of subclass and superclasses Overridden methods match parameters and return type declarations of superclass

Load Instruction What does compiler have? Variable name. What does it do? Look up variable name. If in local symbol table, reference local descriptor If in parameter symbol table, reference parameter descriptor If in field symbol table, reference field descriptor If not found, semantic error

Load Array Instruction What does compiler have? Variable name Array index expression What does compiler do? Look up variable name (if not there, semantic error) Check type of expression (if not integer, semantic error)

Add Operations What does compiler have? two expressions What can go wrong? expressions have wrong type must both be integers (for example) So compiler checks type of expressions load instructions record type of accessed variable operations record type of produced expression so just check types, if wrong, semantic error

Type Inference for Add Operations Most languages let you add floats, ints, doubles What are issues? Types of result of add operation Coercions on operands of add operation Standard rules usually apply If add an int and a float, coerce the int to a float, do the add with the floats, and the result is a float. If add a float and a double, coerce the float to a double, do the add with the doubles, result is double

Add Rules Basic Principle: Hierarchy of number types (int, then float, then double) All coercions go up hierarchy int to float; int, float to double Result is type of operand highest up in hierarchy int + float is float, int + double is double, float + double is double Interesting oddity: C converts float procedure arguments to doubles. Why?

Type Inference Infer types without explicit type declarations Add is very restricted case of type inference Big topic in recent programming language research How many type declarations can you omit? Tied to polymorphism

Equality Expressions If build expression A = B, must check compatibility B must be compatible with A Can always substitute a B for an A B satisfies all the requirements for an A B can do at least as much as A Int compatible with Int Float compatible with Int, Int compatible with Float Class D compatible with Class C if D inherits from C (but not vice-versa)

Inheritance Example - Point Class class point { int c; int getcolor() { return(c); } int distance() { return(0); } }

Point Subclasses class cartesianpoint extends point{ int x, y; int distance() { return(x*x + y*y); } } class polarpoint extends point { int r, t; int distance() { return(r*r); } int angle() { return(t); } }

Object Interfaces Point - getcolor(); distance(); CartesianPoint getcolor(); distance(); PolarPoint getcolor(); distance(); angle(); Semantic check Use type declarations Check that object implements every invoked method Point p = new PolarPoint(); p.distance(); // checks p.angle(); // does not check

Legal and Illegal Code Sequences Legal code sequences Point p = new Point(); p.distance(); Point p = new CartesianPoint(); p.distance(); PolarPoint o = new PolarPoint(); o.angle(); Point p; PolarPoint o; p = o; Illegal code sequences Point p = new PolarPoint(); p.angle(); Point p; PolarPoint o; o = p;

Store Instruction What does compiler have? Variable name Expression What does it do? Look up variable name. If in local symbol table, reference local descriptor If in parameter symbol table, error If in field symbol table, reference field descriptor If not found, semantic error Check type of variable name against type of expression If variable type not compatible with expression type, error

Store Array Instruction What does compiler have? Variable name, array index expression Expression What does it do? Look up variable name. If in local symbol table, reference local descriptor If in parameter symbol table, error If in field symbol table, reference field descriptor If not found, semantic error Check that type of array index expression is integer Check type of variable name against type of expression If variable element type not compatible with expression type, error

Method Invocations What does compiler have? method name, receiver expression, actual parameters Checks: receiver expression is class type method name is defined in receiver s class type types of actual parameters match types of formal parameters What does match mean? same type? compatible type?

Semantic Check Summary Do semantic checks when build IR Many correspond to making sure entities are there to build correct IR Others correspond to simple sanity checks Each language has a list that must be checked Can flag many potential errors at compile time