Type Checking. Chapter 6, Section 6.3, 6.5

Similar documents
Intermediate Code Generation Part II

CSE302: Compiler Design

5. Syntax-Directed Definitions & Type Analysis

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Primitive Data Types: Intro

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Intermediate Code Generation

Static Checking and Type Systems

CSC 467 Lecture 13-14: Semantic Analysis

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

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

Intermediate Code Generation

Intermediate Code Generation

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

ECE 122 Engineering Problem Solving with Java

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

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Intermediate Code Generation

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

intermediate-code Generation

CS313D: ADVANCED PROGRAMMING LANGUAGE

1.3b Type Conversion

Declaration and Memory

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Computational Expression

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

Introduction to Programming Using Java (98-388)

LECTURE 3. Compiler Phases

Motivation was to facilitate development of systems software, especially OS development.

Compilers. Compiler Construction Tutorial The Front-end

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

1 Lexical Considerations

Programming Lecture 3

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Compiler Design (40-414)

CSE 431S Type Checking. Washington University Spring 2013

VARIABLES AND TYPES CITS1001

Time : 1 Hour Max Marks : 30

Formal Languages and Compilers Lecture X Intermediate Code Generation

The Warhol Language Reference Manual

Full file at

Type Checking. Error Checking

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Motivation was to facilitate development of systems software, especially OS development.

Intermediate-Code Generat ion

LECTURE NOTES ON COMPILER DESIGN P a g e 2

ESc101 : Fundamental of Computing

Program Fundamentals

Full file at

Operators and Expressions

Lexical Considerations

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Principles of Programming Languages

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

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

Concepts Introduced in Chapter 6

Language Reference Manual simplicity

Intermediate Representations

CSc 453 Intermediate Code Generation

Java Fall 2018 Margaret Reid-Miller

Lexical Considerations

Lecture 12: Data Types (and Some Leftover ML)

Java Primer 1: Types, Classes and Operators

CS242 COMPUTER PROGRAMMING

Syntax-Directed Translation

The SPL Programming Language Reference Manual

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Data Conversion & Scanner Class

Values and Variables 1 / 30

A variable is a name for a location in memory A variable must be declared

Type systems. Static typing

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

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

More Programming Constructs -- Introduction

Programming Languages Third Edition. Chapter 7 Basic Semantics

Lecture Set 4: More About Methods and More About Operators

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

Principles of Compiler Design

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

A Simple Syntax-Directed Translator

Semantic Analysis. Role of Semantic Analysis

Chapter 2. Elementary Programming

Lecture Set 4: More About Methods and More About Operators

Lecture #23: Conversion and Type Inference

Syntax-Directed Translation Part I

CS2141 Software Development using C/C++ C++ Basics

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Final CSE 131B Spring 2004

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

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CS110: PROGRAMMING LANGUAGE I

Fundamental of Programming (C)

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Dixita Kagathara Page 1

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Transcription:

Type Checking Chapter 6, Section 6.3, 6.5

Inside the Compiler: Front End Lexical analyzer (aka scanner) Converts ASCII or Unicode to a stream of tokens Syntax analyzer (aka parser) Creates a parse tree from the token stream Semantic analyzer Type checking and conversions; other semantic checks Generator of intermediate code Creates lower-level intermediate representation (IR): e.g., three-address code 2

Types in Compilers Type checking: at compile time, guarantee that the run-time behavior of the program will be correct The type of the operands match the type of the operator (e.g., in Java && requires boolean operands) The types of actual parameters in a function call match the types of the formal parameters Many other examples based on the type system of the language Code generation Allocation of memory based on types (e.g., how many bytes do we need for a struct with an int and a float?) Insert explicit type conversions 3

4 Outline Analysis of declarations Representation of types Memory allocation based on types Type checking What is the type of an expression, given the types of its subexpressions? (synthesized attributes) Is there a type error in the program? Implicit type conversions: not in the source code, but must be accounted for during type checking and code generation E.g., for float x, z; int y; z = x+y; we need to generate t=(float)y; z = x+t; three-address instructions

5 Type Expressions What is a type and how do we represent it inside a compiler? We will use type expressions for this A basic type is a type expression (e.g., boolean, char, byte, integer, float, void) An array type constructor, applied to a number and a type expression, is a type expression E.g., array(10,integer) or array(1024,array(24,char)) A record type constructor, applied to a list of pairs (field name, type expression), is a type expression E.g., record { x:float, y:float, rgb:array(3,byte) } could be the type expression for a C struct with fields x,y for point coordinates and field rgb for RGB point color

6 Type Expressions An function type constructor, applied to two type expressions, is a type expression E.g., array(10,float) float for a function that computes the sum of all array elements and returns it A tuple type constructor, applied to a list of type expressions E.g., record { x:float, y:float, rgb:array(3,byte) } float record { x:float, y:float, rgb:array(3,byte) } is a function taking two parameters: a record and a float Type expressions can naturally be represented with trees or DAGs (similarly to ASTs/DAGs for expressions appearing in the source code; see Fig. 6.14 in book)

Simplified Grammar for Declarations D T id ; D 1 addtype(id.symtbl_entry, T.type) ε T B C C.base = B.type T.type = C.type B int B.type = integer float B.type = float bool B.type = boolean C [ const ] C 1 C 1.base = C.base C.type = array(const.lexval, C 1.type) ε C.type = C.base 7 Assume that lexical analysis and parsing have populated the symbol table will all names id Helper function addtype should ensure that only one type is declared for a particular id Things get more complicated when we have multiple scopes

8 D T id ; D 1 ε T B C How Much Memory? addwidth(id.symtbl_entry, T.width) C.basew = B.width T.width = C.width B int B.width = 4 float B.width = 8 bool B.width = 1 C [ const ] C 1 C 1.basew = C.basew C.width = const.lexval * C 1.width ε C.width = C.basew Note: there may be hardware alignment constraints e.g., each integer must start at an address divisible by 4; for record { integer, boolean, integer } padding may be needed; we ignore such issues here

Type Checking Now, look at expressions/statements to see if declared types are consistent with variable usage Many checks of the form if (type expression 1 == type expression 2) OK otherwise report type error When are two type expressions equivalent? Structural equivalence: the underlying type expression DAGs are the same Other options: e.g., name equivalence Checking: (1) types of subexpressions OK? (2) decide the type of the whole expression 9

Simplified Syntax for Expressions S E ; E id int_const float_const true false E E 1 +E 2 E 1 <E 2 E 1 E 2 E - E 1! E 1 E id [E 1 ] E (E 1 ) E E 1 = E 2 10

11 SDD for Type Checking S E ; S.type = void E E 1 + E 2 If (E 1.type is not integer or float) type error If (E 2.type is not integer or float) type error If (E 1.type is not equivalent to E 2.type) type error E.type = E 1.type E E 1 < E 2 If (E 1.type is not integer or float) type error If (E 2.type is not integer or float) type error If (E 1.type is not equivalent to E 2.type) type error E.type = boolean E E 1 E 2 If (E 1.type is not boolean) type error If (E 2.type is not boolean) type error E.type = boolean

SDD for Type Checking E - E 1 If (E 1.type is not integer or float) type error E.type = E 1.type E! E 2 If (E 1.type is not boolean) type error E.type = boolean E id E.type = gettype(id.symtbl_entry) E int_const E.type = integer E float_const E.type = float E true E.type = boolean 12

SDD for Type Checking E id [ E 1 ] If (gettype(id.symtbl_entry) is not array(x,y)) type error If (E 1.type is not integer) type error E.type = Y E (E 1 ) E.type = E 1.type E E 1 = E 2 If (E 1.type is not equivalent to E 2.type) type error E.type = E 1.type Also need to check that the left-hand-side of an assignment operator has an l-value. Add a synthesized attribute lvalue which is true for id and id [ E 1 ] and is false for all other expressions. In the rule for E 1 = E 2 check that E 1.lvalue is true 13

14 Implicit Type Conversions Values of one type are silently converted to another type e.g. addition: 3.0 + 4 : converts 4 to 4.0 Our earlier typechecking rules imply that operator + has types int int int and float float float In a context where the type of an expression is not appropriate either an automatic conversion to another type is performed automatically or if not possible: compile-time error Implications for code generation: E.g., for float x, z; int y; z = x+y; need to generate t=(float)y; z = x+t; three-address instructions

Example: Conversions in Java Widening: converting a value into a larger type Widening primitive conversions in Java byte to short, int, long, float, or double short to int, long, float, or double char to int, long, float, or double int to long, float, or double long to float or double float to double integral type to integral type and float to double do not lose any information 15

Example: Conversions in Java Language Spec says Conversion of an int or long value to float, or of a long value to double, may result in loss of precision The result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode 16

17 Example: Conversions in Java Assignment conversion: when the value of an expression is assigned to a variable, convert the value to the type of the variable Method invocation conversion: applied to each argument value in a method or constructor invocation The type of the argument expression must be converted to the type of the corresponding formal parameter Typecasting conversion: applied to the operand of a typecast operator: (float) 5 Note: this is explicit rather than implicit: the programmer writes code to request the conversion

18 Example: Conversions in Java Numeric promotion: converts operands of a numeric operator to a common type Example: binary numeric promotion e.g. +, -, *, etc. If either operand is double, the other is converted to double Otherwise, if either operand is of type float, the other is converted to float Otherwise, if either operand is of type long, the other is converted to long Otherwise, both are converted to type int Note: there is a type lattice; double is the top element; binary numeric promotion is the least upper bound of the operand types (Fig. 6.25)

Example: Conversions in Java Narrowing: converting a value into a smaller type Narrowing primitive conversions in Java e.g. long to byte, short, char, or int float to byte, short, char, int, or long double to byte, short, char, int, long, or float More cases in the Language Spec Examples of loss of information int to short loses high bits int not fitting in byte changes sign and magnitude double too small for float underflows to zero 19

20 Back to Our Simplified Language Let s allow implicit widening conversions from integer to float. What will get affected? Old rule for E E 1 + E 2 If (E 1.type is not integer or float) type error If (E 2.type is not integer or float) type error If (E 1.type is not equivalent to E 2.type) type error E.type = E 1.type New rule First two checks are the same E.type = E 1.type, if E 2.type is integer E.type = float, otherwise Same for E E 1 < E 2

Modified Code Generation Old rule for E E 1 + E 2 E.addr = newtemp() and E.code = E 1.code E 2.code E.addr "=" E 1.addr "+" E 2.addr New rule E.addr = newtemp() and E.code = E 1.code E 2.code E.addr "=" widen(e 1.addr, E 1.type, E.type) "+" widen(e 2.addr, E 2.type, E.type) Helper function widen(addr, t, w) If t=w return addr Create floating-point temp, create and append new typecast instruction temp = (float) addr, return temp float x, z; int y; x = y+z; becomes t1=(float)y; t2=t1+z; x=t2; 21

How About Assignments? Old rule for E E 1 = E 2 If (E 1.type is not equivalent to E 2.type) type error E.type = E 1.type New rule (assignment conversion, as in Java: right-hand-side value will be converted to the type of the left-hand side expression) E.type = E 1.type Code generation for E E 1 = E 2 Old: E.code = "=" E 2.addr and E.addr = E 2.addr New: E.code = "=" widen(e 2.addr,E 2.type,E 1.type) and E.addr = address returned by widen 22 float x; int y, z; x = y+z; becomes t1=y+z; t2=(float)t1; x=t2;