Trailing-Edge
-
PDP-10 Archives
-
SRI_NIC_PERM_FS_1_19910112
-
kcc-6/kcc/cnvtyp.doc
There are 2 other files named cnvtyp.doc in the archive. Click here to see a list.
This file attempts to summarize C's type conversion possibilities.
The 17 fundamental basic types:
Void Vo
Function Fn
Aggregate: | Array types Ar
| Structure/Union types St
Scalar: | Pointer types Pt
| Enum types En
| Arithmetic: | Integral: (un)signed char/short/int/long S/U C/S/I/L
| Floating-Point: float/double Fl/Db
Type conversions:
Key: - Illegal conversion.
. Allowed conversion.
I Identity (trivial conversion). No representation change.
nc No representation change
ze Zero-extend
se Sign-extend
tr Truncate
Note that the nc, ze, se, and tr conversions all assume 2s complement
representation.
To \ From
\ SC UC SS US SI UI SL UL Fl Db En Pt Ar St Fn Vo
SC I nc tr tr tr tr tr tr . . . . - - - -
UC nc I tr tr tr tr tr tr . . . . - - - -
SS se ze I nc tr tr tr tr . . . . - - - -
US se ze nc I tr tr tr tr . . . . - - - -
SI se ze se ze I nc tr tr . . . . - - - -
UI se ze se ze nc I tr tr . . . . - - - -
SL se ze se ze se ze I nc . . . . - - - -
UL se ze se ze se ze nc I . . . . - - - -
Fl . . . . . . . . I . - - - - - -
Db . . . . . . . . . I - - - - - -
En . . . . . . . . - - I - - - - -
Pt . . . . . . . . - - - . . - . -
Ar - - - - - - - - - - - - - - - -
St - - - - - - - - - - - - - * - - ; * = only I allowed
Fn - - - - - - - - - - - - - - - -
Vo . . . . . . . . . . . . . . . .
CONVERSION TYPES:
CAST conversions: [H&S 6.10]
May perform any of the allowed conversions.
ASSIGNMENT conversions: [H&S 6.11]
Attempts to convert right-side to type of left-side.
Left Side Type Right Side Type
any arithmetic type any arithmetic type
any pointer type the integer constant 0
pointer to T array of T
pointer to function function
USUAL UNARY conversions: [H&S 6.12]
Original operand type Converted type
char, short int
unsigned char unsigned
unsigned short unsigned
float double
"array of T" "pointer to T"
"function returning T" "pointer to function returning T"
USUAL BINARY conversions: [H&S 6.13]
Rather complicated. Basically applies only to arithmetic operands.
[0] If either operand is NOT of arithmetic type, no conversion is done.
[1] If both have same type, also no conversion; done.
[2] If one is "double" then the other is converted to that type; done.
[3] If one is "unsigned long" then the other is converted to that type; done.
[4] If one is "long" and the other is "unsigned"
then both are converted to "unsigned long"; done.
[5] If one is "long" then the other is converted to that type; done.
[6] If one is "unsigned" then the other is converted to that type; done.
[7] Error if we get this far.
FUNCTION ARGUMENT conversions: [H&S 6.14]
Same as usual unary conversions.
FUNCTION PARAMETER and RETURN-VALUE conversions: [H&S 6.15, 9.4]
Similar to unary, but read up on them.
In general, the parser both examines all potential conversions (explicit
or implicit) for legality, AND inserts appropriate cast operations into
the parse tree for the benefit of the code generator. Normally the
parser contains the rules for determining which conversions apply,
and the generator contains the knowledge of how to output code for
specific conversions.
This is done by means of the N_CAST operator, which specifies an
expression and a cast type value. However, for the compound assignment
operators things are more complicated.
The compound assignment operators. (10)
*=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
a += b is like a = a + b except that "a" is evaluated only once.
Need to apply three possible conversions:
a = (conv)( (conv)a + (conv)b )
1 2 3
The conversions for 2 and 3 can be specified with the usual N_CAST
structure. However, the conversion for 1 (the resulting expression) is
stored in the "Nascast" component of the operator node (which will be
one of the above 10 operators - a TKTY_ASOP type of operator).
Example:
short a; char b; a += b;
has three conversions
a = (short)((int)a + (int)b);
Nop: Q_ASPLUS
Nascast: INT to SHORT
Nleft:--------------------------------> Nop: N_CAST
Nright: ------> Nop: N_CAST Ncast: SHORT to INT
Ncast: CHAR to INT Nleft: -----> "a"
Nleft: ------> "b"
However, there are a few cases where the parser cannot provide the
generator with this guidance, and the generator has to know about the
conversion rules itself. These cases are the pre/post
increment/decrement operators: ++(), ()++, --(), and ()--.
Note that
++(exp) is the same as (exp) += 1.
--(exp) is the same as (exp) -= 1.
so that it would be possible to convert the prefix form into a
compound assignment form, which is already fully specified by the parser.
However, for the time being the generator is expected to understand
how to do the appropriate conversions for these four operators.