[fpc-devel] Types and Casts handling in FreePascal Expressions
Florian Klämpfl
florian at freepascal.org
Wed Apr 20 10:10:08 CEST 2011
Am 20.04.2011 10:01, schrieb Pocket MicroTechnics Support:
> Hello,
> I have just a little question about expressions and types/cast management
> in FreePascal.
>
> if I have a mixed expression with:
> i, j, n : byte
> k : word
>
> n := i + k - j;
>
> in this case, how does the compiler handle this situation?
> it evaluated
> Add i , k -> result
> and then
> sub result, j
Everything is evaluated with the native integer size, when assigning,
the expression is converted and range checked if needed.
>
> how the types are handled?
Thumb rules:
* evaluate ordinal expressions with the native integer size of the cpu,
this is also what standard pascal does
* use a type which contains both involved operand types
* the result type of an expression does not depend on it's context
Unfortunatly, these rules have a lot of exceptions but the meta rule is
to avoid that joe programmer can shoot himself into the foot.
> does the compiler cast i to a word, add it (word addition) to k, and cast
> j to word, do substraction and then the result is tranformed back to a
> Byte to do the assignment?
>
> What I mean, does the compiler handle types locally by casting at each
> node, and force the cast at assignment node?
Yes.
More information about the fpc-devel
mailing list