Docsity

Type Checking - Advanced Compiler Design - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Type Checking - Advanced Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Compiler Design โ€“ CSE 504

Type Checking

Static Checking

  • Static (Semantic) Checks
    • Type checks: operator applied to incompatible operands?
    • Flow of control checks: break (outside while?)
    • Uniqueness checks: labels in case statements
    • Name related checks: same name?

Token Stream Parser^

Static Checker

Intermediate Code Generator

Abstract Syntax Tree

Decorated Abstract Syntax Tree

Intermediate Code

Type Systems

  • A collection of rules for assigning type expressions to the various parts of a program.
  • Based on: Syntactic constructs, notion of a type.
  • Example: If both operators of โ€œ+โ€, โ€œ-โ€, โ€œ*โ€ are of type integer then so is the result.
  • Type Checker: An implementation of a type system.
    • Syntax Directed.
  • Sound Type System: eliminates the need for checking type errors during run time.

Type Expressions

โ€ข Implicit Assumptions:

  • Each program has a type
  • Types have a structure

Boolean Character Real Integer Enumerations Sub-ranges Void Error Variables Names

Basic Types Type Constructors Arrays Records Sets Pointers Functions

Expressions

Statements

Type Expressions Grammar

Type -> int | float | char | โ€ฆ

| void

| error

| name

| variable

| array( size, Type)

| record( (name, Type)*)

| pointer( Type)

| tuple((Type)*)

| arrow(Type, Type)

Basic Types

Structured Types

A Simple Typed Language

Program -> Declaration; Statement

Declaration -> Declaration; Declaration

| id: Type

Statement -> Statement; Statement

| id := Expression

| if Expression then Statement

| while Expression do Statement

Expression -> literal | num | id

| Expression mod Expression

| E[E] | E โ†‘ | E (E)

Type Checking Expressions

E -> E1 [E2] {E.type = if E1.type = array(S, T) &

E2.type = int then T else error}

E -> *E1 {E.type = if E1.type = pointer(T) then T

else error}

E -> &E1 {E.type = pointer(E1.tye)}

E -> E1 (E2) {E.type = if (E1.type = arrow(S, T) &

E2.type = S, then T else err}

E -> (E1, E2) {E.type = tuple(E1.type, E2.type)}

Type Checking Statements

S -> id := E {S.type := if id.type = E.type

then void else error}

S -> if E then S1 {S.type := if E.type = boolean

then S1.type else error}

S -> while E do S1 {S.type := if E.type = boolean

then S1.type}

S -> S1; S2 {S.type := if S1.type = void โˆง

S2.type = void then void else error}

Structural Equivalence

  • Definition: by Induction
    • Same basic type (basis)
    • Same constructor applied to SE Type (induction step)
    • Same DAG Representation
  • In Practice: modifications are needed
    • Do not include array bounds โ€“ when they are passed as parameters
    • Other applied representations (More compact)
  • Can be applied to: Tree/ DAG
    • Does not check for cycles
    • Later improve it.

Algorithm Testing Structural

Equivalence

function stequiv(s, t): boolean

{

if (s & t are of the same basic type) return true;

if (s = array(s1, s2) & t = array(t1, t2)) return equal(s1, t1) & stequiv(s2, t2);

if (s = tuple(s1, s2) & t = tuple(t1, t2)) return stequiv(s1, t1) & stequiv(s2, t2);

if (s = arrow(s1, s2) & t = arrow(t1, t2)) return stequiv(s1, t1) & stequiv(s2, t2);

if (s = pointer(s1) & t = pointer(t1)) return stequiv(s1, t1);

}

Recursive Types in C

  • C Policy: avoid cycles in type graphs by:
    • Using structural equivalence for all types
    • Except for records -> name equivalence
  • Example:
    • struct cell {int info; struct cell * next;}
  • Name use: name cell becomes part of the type of the record.
    • Use the acyclic representation
    • Names declared before use โ€“ except for pointers to records.
    • Cycles โ€“ potential due to pointers in records
    • Testing for structural equivalence stops when a record constructor is reached ~ same named record type?

Overloading Functions & Operators

  • Overloaded Symbol: one that has different meanings depending on its context
  • Example: Addition operator +
  • Resolving (operator identification): overloading is resolved when a unique meaning is determined.
  • Context: it is not always possible to resolve overloading by looking only the arguments of a function - Set of possible types - Context (inherited attribute) necessary

Narrowing Down Types

Eโ€™ -> E {Eโ€™.types = E. types

E.unique = if Eโ€™.types = {t} then t else error}

E -> id {E.types = lookup(id.entry)}

E -> E1(E2) {E.types = {sโ€™ | โˆƒ s โˆˆ E2.types and S->sโ€™ โˆˆ

E1.types} t = E.unique S = {s | s โˆˆ E2.types and S->t โˆˆE1.types} E2.unique = if S ={s} the S else error E1.unique = if S = {s} the S->t else error

Polymorphic Functions

  • Defn: a piece of code (functions, operators) that can be executed with arguments of different types.
  • Examples: Built in Operator indexing arrays, pointer manipulation
  • Why use them: facilitate manipulation of data structures regardless of types.
  • Example HL:

fun length(lptr) = if null (lptr) then 0 else length(+l(lptr)) + 1