Types and Declarations in Intermediate Code Generation
Types and Declarations in Intermediate Code Generation
Code Generation
Table of contents.
1. Introduction.
2. Type expressions.
3. Type equivalence.
4. Declarations.
5. Storage layout for local names.
6. Sequences and declarations.
7. Records and classes fields.
8. Summary.
9. References.
Type expressions
Types have a structure we will represent using type expressions, a type
expression can either be formed by applying a type constructor operator
to a type expression or can be a basic type.
Basic types are determined by the language that is being checked.
An example;
We have an array of type int[2][3] that is read as array of 2 arrays of 3
integers each. It is written as a type expression as follows; array(2,
array(3, integer))
We represent it as a tree as shown below (1);
Type equivalence
Type-checking rules are of the form; 'if two type expressions are equal
then return a certain type, else return an error'.
When similar names are used for type expressions and other
subsequent type expressions, ambiguities arise.
The problem is whether a name in a type expression represents itself or
represents an abbreviation for another type expression.
When representing type expressions using graphs, we say that two
types are structurally equivalent if and only if either of the conditions is
true;
They have the same basic type.
They are formed by applying a similar constructor to structurally
equivalent types.
One is a type name that denotes the other.
If we treat type names as standing for themselves, then the first two
conditions in the definition above lead to the name equivalence of type
expressions.
Declarations
We learn about types and declarations using simplified grammar that
declares a single name at a time.
We have the following grammar; (2)
The non-terminal D generates a sequence of declarations. Non-
terminal T generates basic types, array, or record types.
The non-terminal B generates a basic type either an int or float.
The non-terminal C generates strings of zero or more integers, each
surrounded by brackets.
An array type consists of the basic type specified by B followed by array
components specified by a non-terminal C. A record type is a sequence
of declarations for fields of the record surrounded by curly braces.