]> git.sur5r.net Git - cc65/blob - src/cc65/testexpr.c
Changed the expression parser to return the lvalue flag as part of the
[cc65] / src / cc65 / testexpr.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                testexpr.c                                 */
4 /*                                                                           */
5 /*                        Test an expression and jump                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2004      Ullrich von Bassewitz                                       */
10 /*               Römerstraße 52                                              */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include "codegen.h"
37 #include "error.h"
38 #include "expr.h"
39 #include "scanner.h"
40 #include "testexpr.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   Code                                    */
46 /*****************************************************************************/
47
48
49
50 unsigned Test (unsigned Label, int Invert)
51 /* Evaluate a boolean test expression and jump depending on the result of
52  * the test and on Invert. The function returns one of the TESTEXPR_xx codes
53  * defined above. If the jump is always true, a warning is output.
54  */
55 {
56     ExprDesc lval;
57     unsigned Result;
58
59     /* Evaluate the expression */
60     expr (hie0, InitExprDesc (&lval));
61
62     /* Check for a boolean expression */
63     CheckBoolExpr (&lval);
64
65     /* Check for a constant expression */
66     if (ED_IsRVal (&lval) && lval.Flags == E_MCONST) {
67
68         /* Result is constant, so we know the outcome */
69         Result = (lval.ConstVal != 0);
70
71         /* Constant rvalue */
72         if (!Invert && lval.ConstVal == 0) {
73             g_jump (Label);
74             Warning ("Unreachable code");
75         } else if (Invert && lval.ConstVal != 0) {
76             g_jump (Label);
77         }
78
79     } else {
80
81         /* Result is unknown */
82         Result = TESTEXPR_UNKNOWN;
83
84         /* If the expr hasn't set condition codes, set the force-test flag */
85         if ((lval.Test & E_CC) == 0) {
86             lval.Test |= E_FORCETEST;
87         }
88
89         /* Load the value into the primary register */
90         ExprLoad (CF_FORCECHAR, &lval);
91
92         /* Generate the jump */
93         if (Invert) {
94             g_truejump (CF_NONE, Label);
95         } else {
96             g_falsejump (CF_NONE, Label);
97         }
98     }
99
100     /* Return the result */
101     return Result;
102 }
103
104
105
106 unsigned TestInParens (unsigned Label, int Invert)
107 /* Evaluate a boolean test expression in parenthesis and jump depending on
108  * the result of the test * and on Invert. The function returns one of the
109  * TESTEXPR_xx codes defined above. If the jump is always true, a warning is
110  * output.
111  */
112 {
113     unsigned Result;
114
115     /* Eat the parenthesis */
116     ConsumeLParen ();
117
118     /* Do the test */
119     Result = Test (Label, Invert);
120
121     /* Check for the closing brace */
122     ConsumeRParen ();
123
124     /* Return the result of the expression */
125     return Result;
126 }
127
128
129
130