Ch6 - 1 ! Static Checking and Type Systems! Chapter 6!...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 The Structure of our Compiler Revisited Lexical analyzer Syntax-directed static checker Character stream Token stream Java bytecode Yacc speciFcation JVM speciFcation Lex speciFcation Syntax-directed translator Type checking Code generation
Background image of page 2
3 Static versus Dynamic Checking Static checking : the compiler enforces programming language’s static semantics – Program properties that can be checked at compile time Dynamic semantics : checked at run time – Compiler generates veriFcation code to enforce programming language’s dynamic semantics
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Static Checking • Typical examples of static checking are – Type checks – Flow-of-control checks – Uniqueness checks – Name-related checks
Background image of page 4
5 Type Checking, Overloading, Coercion, Polymorphism class X { virtual int m(); } *x; class Y: public X { virtual int m(); } *y; int op(int), op(float); int f(float); int a, c[10], d; d = c + d; // FAIL *d = a; // FAIL a = op(d); // OK : static overloading (C++) a = f(d); // OK : coersion of d to float a = x->m(); // OK : dynamic binding (C++) vector<int> v; // OK : template instantiation
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 } }
Background image of page 6
7 Uniqueness Checks myfunc() { int i, j, i; // ERROR } cnufym(int a, int a) // ERROR { } struct myrec { int name; }; struct myrec // ERROR { int id; };
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Name-Related Checks LoopA: for (int I = 0; I < n; I++) { … if (a[I] == 0) break LoopB; // Java labeled loop }
Background image of page 8
9 One-Pass versus Multi-Pass Static Checking One-pass compiler : static checking in C, Pascal, Fortran, and many other languages is performed in one pass while intermediate code is generated – In±uences design of a language: placement constraints Multi-pass compiler : static checking in Ada, Java, and C# is performed in a separate phase, sometimes by traversing a syntax tree multiple times
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Type Expressions Type expressions are used in declarations and type casts to defne 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
Background image of page 10
11 Graph Representations for Type Expressions int *f(char*,char*) fun args pointer char int pointer char pointer Tree forms fun args pointer char int pointer DAGs
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Cyclic Graph Representations struct Node { int val; struct Node *next; }; struct val pointer int Internal compiler representation of the Node type: cyclic graph next Source program
Background image of page 12
13 Name Equivalence • Each type name is a distinct type, even when the type expressions that the names refer to are the same • Types are identical only if names match • Used by Pascal (inconsistently)
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 50

Ch6 - 1 ! Static Checking and Type Systems! Chapter 6!...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online