]> git.sur5r.net Git - cc65/blob - src/cc65/testexpr.c
Move the test flags into the Flags bitset of struct ExprDesc
[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 Expr;
57     unsigned Result;
58
59     /* Read a boolean expression */
60     BoolExpr (hie0, &Expr);
61
62     /* Check for a constant expression */
63     if (ED_IsConstAbs (&Expr)) {
64
65         /* Result is constant, so we know the outcome */
66         Result = (Expr.Val != 0);
67
68         /* Constant rvalue */
69         if (!Invert && Expr.Val == 0) {
70             g_jump (Label);
71             Warning ("Unreachable code");
72         } else if (Invert && Expr.Val != 0) {
73             g_jump (Label);
74         }
75
76     } else {
77
78         /* Result is unknown */
79         Result = TESTEXPR_UNKNOWN;
80
81         /* If the expr hasn't set condition codes, set the force-test flag */
82         if (!ED_IsTested (&Expr)) {
83             ED_MarkForTest (&Expr);
84         }
85
86         /* Load the value into the primary register */
87         ExprLoad (CF_FORCECHAR, &Expr);
88
89         /* Generate the jump */
90         if (Invert) {
91             g_truejump (CF_NONE, Label);
92         } else {
93             g_falsejump (CF_NONE, Label);
94         }
95     }
96
97     /* Return the result */
98     return Result;
99 }
100
101
102
103 unsigned TestInParens (unsigned Label, int Invert)
104 /* Evaluate a boolean test expression in parenthesis and jump depending on
105  * the result of the test * and on Invert. The function returns one of the
106  * TESTEXPR_xx codes defined above. If the jump is always true, a warning is
107  * output.
108  */
109 {
110     unsigned Result;
111
112     /* Eat the parenthesis */
113     ConsumeLParen ();
114
115     /* Do the test */
116     Result = Test (Label, Invert);
117
118     /* Check for the closing brace */
119     ConsumeRParen ();
120
121     /* Return the result of the expression */
122     return Result;
123 }
124
125
126
127