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

What are the characteristics of Object Oriented programming language?

Chapter 5 Object-Oriented Programming

C++ Important Questions with Answers

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

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

Inheritance and object compatibility

Compiler Construction I

Intermediate Code Generation Part II

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Java Object Oriented Design. CSC207 Fall 2014

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

Java Fundamentals (II)

Compiler Construction 2009/2010 Polymorphic Types and Generics

2.4 Structuring programs

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

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

Arrays Classes & Methods, Inheritance

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

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

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

Types. What is a type?

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

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

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

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

Semantic Analysis and Type Checking

Types, Values and Variables (Chapter 4, JLS)

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

CSE 431S Type Checking. Washington University Spring 2013

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

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

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

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

Java Primer 1: Types, Classes and Operators

Strict Inheritance. Object-Oriented Programming Winter

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

Chapter 14 Abstract Classes and Interfaces

Inheritance & Polymorphism

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

Compiler Construction 2016/2017 Polymorphic Types and Generics

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

The SPL Programming Language Reference Manual

Type Checking. Error Checking

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

Motivation for typed languages

Absolute C++ Walter Savitch

2. Reachability in garbage collection is just an approximation of garbage.

Operators and Expressions

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

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

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

Chapter 6 Introduction to Defining Classes

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

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.

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

type conversion polymorphism (intro only) Class class

Decaf Language Reference

Expressions and Casting

Building Java Programs

Type Checking. Chapter 6, Section 6.3, 6.5

Inheritance and Interfaces

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

MiniJava Parser and AST. Winter /27/ Hal Perkins & UW CSE E1-1

ITI Introduction to Computing II

Pierce Ch. 3, 8, 11, 15. Type Systems

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

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Type Analysis. Type Checking vs. Type Inference

Inclusion Polymorphism

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Interface evolution via public defender methods

Intermediate Code Generation

COS 320. Compiling Techniques

ITI Introduction to Computing II

Binding and Variables

Summary of Go Syntax /

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

Strict Inheritance. Object-Oriented Programming Spring 2008

Some instance messages and methods

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

OBJECT ORIENTED PROGRAMMING USING C++

Compiler Practical 2013 Inheritance (Part 1)

Simple Component Writer's Guide

Expressions & Assignment Statements

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

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

2. The object-oriented paradigm

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 int x var_decl int i

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

class_decl vector field_decl method_decl statements vector class symbol table int v add param_decl int x var_decl 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 int x var_decl 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 int x var_decl 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 int x var_decl 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; < len sta + lda 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; 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; len field descriptor for v local descriptor for i parameter descriptor for x

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

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

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

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

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

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

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

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

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

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

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

Abbreviated Notation < i while len v v sta + i lda ldp x v 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 A compatible with B or B compatible with A Int compatible with Int Class C compatible with Class D if C inherits from D (but not vice-versa)

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