Download Documentation for Pic Micro Pascal
Transcript
PIC MICRO PASCAL V1.6 - USER MANUAL 4 Statements 4.1 Assignments and expressions PMP parses expressions containing constants, variables and function calls, and obeys the normal rules of precedence. PMP has no type checking for simple variables, so any combination of types may be used (possible truncation will produce a warning). Boolean variables or bits are internally used as single bits but if used in non-boolean expressions they are converted by an implicit ORD to byte values of TRUE (1) or FALSE (0). In non-strict mode, for boolean expressions any value other than zero is treated as TRUE, so this construction is allowed, even for non-boolean variables: IF Variable THEN It will generate the same code than: IF Variable <> 0 THEN Use of simple constants may be optimized by PMP: A "while <Constant expression evaluated to false> do <block>" will not generate any code. A "if <Constant expression evaluated to false> then <block>" will not generate any code, if an "else <block>" exists, only this "else <block>" will be generated. A "while <Constant expression evaluated to true> do <block>" will generate only <block> that loops forever. This may help for conditional compilation if configuration constants are used (may be used the same way as $IFDEF/$ENDIF blocks). In PMP two boolean constants are predefined: TRUE = 1 and FALSE = 0. Since there is no type checking on booleans in non-strict mode, either TRUE and FALSE or 0 and 1 may be used for booleans. The syntax for a variable assignment is: <Variable> := <Expression>; Where: <Expression> ::= <Relation> | <Term> (+ | - <Term>)* <Relation> ::= <BoolExpression> (<BoolOp> <BoolExpression>) <Term> ::= <Factor> (* | DIV | / | MOD <Factor>)* <Factor> ::= (+ | -)* <Constant> | (<Expression>) | <Variable> | <Variable>.<bit> | <Function Call> <Constant> ::= any valid numeric or character constant <BoolExpression> ::= <Expression> (<relop> <Expression>) <Relop> ::= = | <> | < | > | <= | >= | IN <Boolop> ::= AND | OR | XOR | SHL | SHR <Function Call> ::= <Function Name> ((<FunctionArgs>)) <Function Name> ::= <UserFunctionName> | <BuiltinFunctionName> 4.1.1 SHR & SHL "normal" behaviors As in standard Pascal, SHR and SHL applied to signed numbers implies an automatic cast to unsigned, so shifts are always "logical" - never "arithmetic". This is Pascal standard. Period. This explanation is to anticipate discussions on this subject. The C standard says there is no standard for >> and << operators on signed numbers. This is left "implementation dependent" and the consequence is that their behaviors differ between compilers and the subject generates long discussions in forums and discussion lists... Note that the PMP optimizer engine is smart enough to use "logical shifts" or "arithmetic shifts" when an unsigned or a signed number DIV by a power of two is required. Direct use of SHR will not improve the code efficiency. PMP also uses SHL to multiply by a power of two in some circumstances. Document revision: A S TATEM ENTS - ASSIGNM ENTS AND EXPRESSIONS Page 59/101