Type Conversion in Compiler Design

Type Conversions

  • Conversion of one data type to another automatically by the compiler is called "Type Conversion".
  • For Example, Consider expressions like x + i, where x is of type float and i is of type integer.
  • Since the representation of integers and floating-point numbers is different within a computer and different machine instructions are used for operations on integers and floats, the compiler may need to convert one of the operands of + to ensure that both operands are of the same type when the addition occurs.
  • Suppose that integers are converted to floats when necessary, using a unary operator (float).

For example, the integer 2 is converted to a float in the code for the expression 2 * 3 .14:

t1 = (float) 2 
t2 = t1 * 3.14
  • The attribute E .type, whose value is either integer or float.
  • The rule associated with E → >E1 + E2 builds on the pseudocode
if (E1.type = integer and E2.type = integer)          E.type = integer;

else if (E1.type = float and E2. type = integer) E.type = float;

else if (E1.type = integer and E2. type = float) E.type = float;

else if (E1.type = float and E2. type = float) E.type = float;
  • Type conversion rules vary from language to language. The rules for Java in the distinguish between widening conversions, which are intended to preserve information, and narrowing conversions, which can lose information.
 Widening and Narrowing Conversions

Widening and Narrowing Conversions

 Type Conversions

Type Conversions


  • When the type conversion is performed automatically by the compiler without the programmer’s intervention, the type conversion is referred to as implicit type conversion.
  • Implicit type conversions, also called coercions, are limited in many languages to widening conversions. Conversion is said to be explicit if the programmer must write something to cause the conversion. Explicit conversions are also called casts.
  • The semantic action for checking E → E1 + E2 uses two functions:
    • max(t1 ,t2) takes two types t1 and t2 and returns the maximum (or least upper bound) of the two types in the widening hierarchy. It declares an error if either t1 or t2 is not in the hierarchy. e.g., if either type is an array or a pointer type.
    • widen(a, t, w) generates type conversions if needed to widen an address a of type t into a value of type w. It returns a itself if t and w are the same type. Otherwise, it generates an instruction to do the conversion and place the result in a temporary t, which is returned as the result.

Pseudocode for widen, assuming that the only types are integer and float.

Addr widen(Addr a, Type t, Type w)
     if ( t = w ) return a;
     else if ( t = integer and w = float )
        temp = new Temp(); 
        gen(temp '=' '(float)' a); 
        return temp;
    else error;
  • Introducing type conversions into expression evaluation
E -> E1 + E2 { E.type = max(E1.type,E2.type);

a1 = widen(E1. addr, E1 .type, E.type);

a2 = widen(E2. addr, E2 .type, E.type);

E.addr = new Temp();

gen(E. addr '=' a1 '+' a2); }
 Convert Variable into Specified DataType

Convert Variable into Specified DataType


  • Consider expressions formed by applying an arithmetic operator op to constants and identifiers, as in the grammar. Suppose there are two types - real and integer, with integers converted to reals when necessary. Attribute type of nonterminal E can be either integer or real, and the type-checking rules are shown, function lookup(e) returns the type saved in the symbol table entry pointed to by e.
Production Semantic Rule
E -> num E.type = integer
E -> num.num E.type = real
E -> id E.type = lookup(id.entry)
E -> E1 op E2 E.type = if ( E1.type = integer and E2.type = integer )

                then integer

else if ( E1.type = integer and E2.type = real )

                then real

else if ( E1.type = real and E2.type = integer )

                 then real

else if ( E1.type = real and E2.type = real )

                 then real

else type_error

Related Searches to Type Conversion in Compiler Design