23-01-2013, 12:07 PM
type checking &intermediate code generation
type checking.ppt (Size: 1.55 MB / Downloads: 33)
Type checking: static checking
Compilers need to check that the source program adheres
Syntactic
parser
Semantics
Need large knowledge base
Need to know the representation of values
Structure of the computations
Checking?
Type Checking ?
The processes of identifying errors in a program based on explicitly or implicitly stated type information
Static (Compile time) vs. Dynamic (Run-time)
Strongly typed language vs. weakly type language
Kind Checking
The processes of identifying errors in a program based on stated kind information
Variables
Functions/procedures
A simple scenario
Consider a simple variable x and its corresponding executable code.
To emit the code, compiler needs to answer these questions:
What kind of value must be kept in x?
How big is x?
Is x ID?
Is x function?
Is x local?
Is x global?
Compiler uses
declaration (e.g., C)
Type checker
A type checker?
Verifies that the type of a construct matches the expected type at any given context
E.g.,
operator mod in Pascal requires integer operands
dereferencing (i.e, getting the value of data structure using pointer) is applied only to pointer
indexing is applied only to array
Why do we need type information?
Type information is needed by code generator
Operator overloading (different implementation /ad hoc polymorphism)
A symbol that represents different operators in different context
e.g “+” operators can be applied to Integers, real, and strings
Polymorphism(multiform)
Differs from overloading
Refers to the situation in which a body of function is executed with arguments of several types
Inferring types for expression
The simplest form for type inference occurs when the compiler can assign a type to each element in an expression (i.e., each leaf in Parse Tree for an expression)
It requires to build parse tree and assign a type to each value in the expression during a simple postorder tree walk
For each expression
It requires define/use for all variables
It infers types for all constants
It requires type information about all function
It Includes Error handling rules/routines
The above process should let the compiler to detect every violation of an inference rule and report it at compile time