1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Chapter 6 Intermediate Code Generation
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
1 Compiler Construction Intermediate Code Generation.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Chapter 6 Type Checking Section 0 Overview
Type Checking  Legality checks  Operator determination  Overload resolution.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Types Type = Why? a set of values
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
COP4020 Programming Languages
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
1 February 17, February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Lecture 8 Semantic Analysis.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design – CSE 504 Type Checking
Semantics Analysis.
Semantic Analysis Type Checking
Context-Sensitive Analysis
Constructing Precedence Table
Syntax-Directed Translation Part I
Semantic Analysis Chapter 6.
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Static Checking and Type Systems
Chapter 6 Intermediate-Code Generation
Semantic Analysis Chapter 6.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Compiler Construction
Syntax-Directed Translation Part I
Compiler Construction
Presentation transcript:

1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005

2 The Structure of our Compiler Revisited Lexical analyzer Syntax-directed static checker Character stream Token stream Java bytecode Yacc specification JVM specificationLex specification Syntax-directed translator Type checking Code generation

3 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics, which are checked at compile time Runtime checking: dynamic semantics are checked at run time by special code generated by the compiler

4 Static Checking Typical examples of static checking are –Type checks –Flow-of-control checks –Uniqueness checks –Name-related checks

5 Type Checks, Overloading, Coercion, and Polymorphism int op(int), op(float); int f(float); int a, c[10], d; d = c+d;// FAIL *d = a;// FAIL a = op(d);// OK: overloading (C++) a = f(d);// OK: coersion vector v;// OK: template instantiation

6 Flow-of-Control Checks myfunc() { … break; // ERROR } myfunc() { … switch (a) { case 0: … break; // OK case 1: … } myfunc() { … while (n) { … if (i>10) break; // OK }

7 Uniqueness Checks myfunc() { int i, j, i; // ERROR … } cnufym(int a, int a) // ERROR { … } struct myrec { int name; }; struct myrec // ERROR { int id; };

8 Name-Related Checks LoopA: for (int I = 0; I < n; I++) { … if (a[I] == 0) break LoopB; … }

9 One-Pass versus Multi-Pass Static Checking One-pass compiler: static checking for C, Pascal, Fortran, and many other languages can be performed in one pass while at the same time intermediate code is generated Multi-pass compiler: static checking for Ada, Java, and C# is performed in a separate phase, sometimes requiring traversing the syntax tree multiple times

10 Type Expressions Type expressions are used in declarations and type casts to define or refer to a type –Primitive types, such as int and bool –Type constructors, such as pointer-to, array-of, records and classes, templates, and functions –Type names, such as typedefs in C and named types in Pascal, refer to type expressions

11 Graph Representations for Type Expressions int *f(char*,char*) fun argspointer char intpointer char pointer Tree forms fun argspointer char intpointer DAGs

12 Cyclic Graph Representations struct Node { int val; struct Node *next; }; struct val pointer int Cyclic graph next

13 Name Equivalence Each type name is a distinct type, even when the type expressions the names refer to are the same Types are identical only if names match Used by Pascal (inconsistently) type link = ^node; var next : link; last : link; p : ^node; q, r : ^node; With name equivalence in Pascal: p ≠ next p ≠ last p = q = r next = last

14 Structural Equivalence of Type Expressions Two types are the same if they are structurally identical Used in C, Java, C# struct valnext int pointer struct val int pointer = next

15 Structural Equivalence of Type Expressions (cont’d) Two structurally equivalent type expressions have the same pointer address when constructing graphs by sharing nodes struct val int pointers p struct Node { int val; struct Node *next; }; struct Node s, *p; … p = &s; // OK … *p = s; // OK next &s *p

16 Constructing Type Graphs in Yacc Type *mkint() construct int node if not already constructed Type *mkarr(Type*,int) construct array-of-type node if not already constructed Type *mkptr(Type*) construct pointer-of-type node if not already constructed

17 Syntax-Directed Definitions for Constructing Type Graphs in Yacc %union { Symbol *sym; int num; Type *typ; } %token INT %token ID %token NUM %type type % decl : type ID { addtype($2, $1); } | type ID ‘[’ NUM ‘]’ { addtype($2, mkarr($1, $4)); } ; type : INT { $$ = mkint(); } | type ‘*’ { $$ = mkptr($1); } | /* empty */ { $$ = mkint(); } ;

18 Type Systems A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules, for example “if both operands of addition are of type integer, then the result is of type integer” Formal type system rules: Post system

19 Type Rules in Post System Notation E  e 1 : integerE  e 2 : integer E  e 1 + e 2 : integer Environment E maps variables v to types T: E(v) = T E(v) = T E  v : T E  e : T E  v := e E(v) = T

20 A Simple Language Example P  D ; S D  D ; D | id : T T  boolean | char | integer | array [ num ] of T | ^ T S  id := E | if E then S | while E do S | S ; S E  true | false | literal | num | id | E and E | E mod E | E [ E ] | E ^

21 Simple Language Example: Declarations D  id : T{ addtype(id.entry, T.type) } T  boolean{ T.type := boolean } T  char{ T.type := char } T  integer{ T.type := integer } T  array [ num ] of T 1 { T.type := array(1..num.val, T 1.type) } T  ^ T 1 { T.type := pointer(T 1 )

22 Simple Language Example: Statements S  id := E{ S.type := if id.type = E.type then void else type_error } S  if E then S 1 { S.type := if E.type = boolean then S 1.type else type_error } S  while E do S 1 { S.type := if E.type = boolean then S 1.type else type_error } S  S 1 ; S 2 { S.type := if S 1.type = void and S 2.type = void then void else type_error }

23 Simple Language Example: Expressions E  true{ E.type = boolean } E  false { E.type = boolean } E  literal { E.type = char } E  num{ E.type = integer } E  id{ E.type = lookup(id.entry) } …

24 Simple Language Example: Expressions (cont’d) E  E 1 and E 2 { E.type := if E 1.type = boolean and E 2.type = boolean then boolean else type_error } E  E 1 mod E 2 { E.type := if E 1.type = integer and E 2.type = integer then integer else type_error } E  E 1 [ E 2 ] { E.type := if E 1.type = array(s, t) and E 2.type = integer then t else type_error } E  E 1 ^{ E.type := if E 1.type = pointer(t) then t else type_error }

25 Simple Language Example: Adding Functions T  T 1 -> T 2 { T.type := function(T 1.type, T 2.type) } E  E 1 ( E 2 ) { E.type := if E 1.type = function(s, t) and E 2.type = s then t else type_error } Example: v : integer; odd : integer -> boolean; if odd(3) then v := 1;

26 Syntax-Directed Definitions for Type Checking in Yacc %{ enum Types {Tint, Tfloat, Tpointer, Tarray, … }; typedef struct Type { enum Types type; struct Type *child; } Type; %} %union { Type *typ; } %type expr % expr : expr ‘+’ expr { if ($1.type != Tint || $3.type != Tint) semerror(“non-int operands in +”); $$ = mkint(); emit(iadd); }

27 Type Conversion and Coercion Type conversion is explicit, for example using type casts Type coercion is implicitly performed by the compiler Both require a type system to check and infer types for (sub)expressions

28 Syntax-Directed Definitions for Type Coercion in Yacc %{ … %} % expr : expr ‘+’ expr { if ($1.type == Tint && $3.type == Tint) { $$ = mkint(); emit(iadd); } else if ($1.type == Tfloat && $3.type == Tfloat) { $$ = mkfloat(); emit(fadd); } else if ($1.type == Tfloat && $3.type == Tint) { $$ = mkfloat(); emit(i2f); emit(fadd); } else if ($1.type == Tint && $3.type == Tfloat) { $$ = mkfloat(); emit(swap); emit(i2f); emit(fadd); } else semerror(“type error in +”); $$ = mkint(); }

29 Syntax-Directed Definitions for L-Values and R-Values in Yacc %{ typedef struct Node { Type *typ; int islval; } Node; %} %union { Node *rec; } %type expr % expr : expr ‘+’ expr { if ($1.typ->type != Tint || $3.typ->type != Tint) semerror(“non-int operands in +”); $$.typ = mkint(); $$.islval = FALSE; emit(…); } | expr ‘=’ expr { if (!$1.islval || $1.typ != $3.typ) semerror(“invalid assignment”); $$.typ = $1.typ; $$.islval = FALSE; emit(…); } | ID { $$.typ = lookup($1); $$.islval = TRUE; emit(…); }