CD Notes
Comprehensive guide to type conversions: implicit coercion rules, explicit casting, type promotion hierarchies, compatibility checking, code generation for conversions, practical compiler implementation strategies.
Fundamentals
Type conversion is the transformation of a value from one type to another. Type coercion is implicit conversion done automatically by the compiler.
Two Categories
| - Promotion: smaller | larger (safe) |
| - Example: int | float (no precision loss typically) |
| - Conversion: any | any |
| - Example: float | int (may lose precision) |
| - Syntax | (target_type) expression |
Type Promotion Hierarchy
Arithmetic Type Promotion
Type hierarchy (safe promotion)
char → short → int → long → float → double
Rules
1. int smaller than type of other: promote to other
2. At least one float: promote int to float
3. At least one double: promote to double
Example
int + float → float + float → float
short + int → int + int → int
Promotion Algorithm
When combining types in binary operation
Type combine(Type a, Type b):
if a == b:
return a
if a > b in hierarchy: // b smaller
return a
else:
return b
Example
short + float: float > short, return float
int + double: double > int, return double
Type Compatibility Checking
Checking Rules
| For assignment | value_type must be compatible with target_type |
| 1. Exact match | same type |
| 2. Implicit promotion | value_type can promote to target_type |
| 3. Pointer compatibility (if allowed) | pointer types match or convert |
| int x = 5.0; // float | int (truncation, allowed) |
| float y = 10; // int | float (promotion, allowed) |
| int z = "hello"; // string | int (NOT compatible, error) |
Code Generation for Conversions
Generating Conversion Code
For implicit promotion
Expr1 (type t1) op Expr2 (type t2)
If t1 < t2 in hierarchy:
code = expr1_code + promote(t1, t2) + expr2_code + op
Example (C)
int x = 5;
float y = 3.0;
result = x + y; // x promoted to float
Generated
float temp = (float) x; // conversion code
result = temp + y;
Conversion Instructions
| int | float: cvtsi2ss (convert scalar int to float) |
| float | int: cvttss2si (convert with truncation) |
| char | int: movzx (zero-extend) |
| int | char: mov al, (lower byte) |
Explicit Casting
Cast Syntax and Semantics
Syntax
(target_type) expression
Semantics
1. Evaluate expression
2. Convert result to target_type
3. Use converted value
Example
(int) 3.7 → 3 (truncation)
(float) 5 → 5.0 (promotion)
(char) 1000 → 232 (truncation to 8-bit)
Unsafe vs Safe Casts
Safe casts (no information loss)
int → float
char → int
Unsafe casts (information loss possible)
float → int (fractional part lost)
double → float (precision lost)
int → char (high bits lost)
Compiler warnings for unsafe casts
Practical Type System Examples
Java Type Conversion Rules
C Type Conversion Rules
| char/short + int | int |
| int + float | float |
| float + double | double |
Interview Q&A
Q1: What's the difference between implicit coercion and explicit casting?
A: Implicit coercion happens automatically when types mix; explicit casting is programmer-requested using (type) syntax. Coercion is safe by design; casting can be unsafe (checked at compile-time with warnings).
Q2: Why do some conversions require explicit casting while others are implicit?
A: Implicit conversions must be "safe" (no information loss under normal circumstances). Conversions like float→int require explicit casting because precision/values may be lost, forcing programmer to acknowledge the risk.
Q3: How do compilers decide promotion order in complex expressions?
A: Left-to-right with operator precedence. For each binary operation, types combine using the promotion hierarchy. If ambiguity exists, use explicit parentheses or casts.
Q4: What code generation is needed for type conversions?
A: Instructions like cvtsi2ss (signed int to float), cvttss2si (truncate float to int), or simple register moves for compatible sizes. Inserted between evaluation of operands and the operation itself.
Q5: Can all type conversions be done at compile-time?
A: Not reliably for all values. While type structure is known, actual values may require runtime conversion (e.g., converting user input). Conservative approach: assume some conversions are runtime operations.
Q6: Why is overflow/underflow not checked during type conversion?
A: For performance. Checking all conversions for overflow adds overhead. Most systems assume programmer responsibility or use safe languages (Java checks array bounds but not integer overflow).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Type Conversion and Coercion in Compiler Semantic Analysis.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, semantic, analysis, type, conversion
Related Compiler Design Topics