Print this page
new smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/expression.h
+++ new/usr/src/tools/smatch/src/expression.h
1 1 #ifndef EXPRESSION_H
2 2 #define EXPRESSION_H
3 3 /*
4 4 * sparse/expression.h
5 5 *
6 6 * Copyright (C) 2003 Transmeta Corp.
7 7 * 2003 Linus Torvalds
8 8 *
9 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 10 * of this software and associated documentation files (the "Software"), to deal
11 11 * in the Software without restriction, including without limitation the rights
12 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 13 * copies of the Software, and to permit persons to whom the Software is
14 14 * furnished to do so, subject to the following conditions:
15 15 *
16 16 * The above copyright notice and this permission notice shall be included in
17 17 * all copies or substantial portions of the Software.
18 18 *
19 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 25 * THE SOFTWARE.
26 26 *
27 27 * Declarations and helper functions for expression parsing.
28 28 */
29 29
30 30 #include "allocate.h"
31 31 #include "lib.h"
32 32 #include "symbol.h"
33 33
34 34 struct expression_list;
35 35
36 36 enum expression_type {
37 37 EXPR_VALUE = 1,
38 38 EXPR_STRING,
39 39 EXPR_SYMBOL,
40 40 EXPR_TYPE,
41 41 EXPR_BINOP,
42 42 EXPR_ASSIGNMENT,
43 43 EXPR_LOGICAL,
44 44 EXPR_DEREF,
45 45 EXPR_PREOP,
46 46 EXPR_POSTOP,
47 47 EXPR_CAST,
48 48 EXPR_FORCE_CAST,
49 49 EXPR_IMPLIED_CAST,
50 50 EXPR_SIZEOF,
51 51 EXPR_ALIGNOF,
52 52 EXPR_PTRSIZEOF,
53 53 EXPR_CONDITIONAL,
54 54 EXPR_SELECT, // a "safe" conditional expression
55 55 EXPR_STATEMENT,
56 56 EXPR_CALL,
↓ open down ↓ |
56 lines elided |
↑ open up ↑ |
57 57 EXPR_COMMA,
58 58 EXPR_COMPARE,
59 59 EXPR_LABEL,
60 60 EXPR_INITIALIZER, // initializer list
61 61 EXPR_IDENTIFIER, // identifier in initializer
62 62 EXPR_INDEX, // index in initializer
63 63 EXPR_POS, // position in initializer
64 64 EXPR_FVALUE,
65 65 EXPR_SLICE,
66 66 EXPR_OFFSETOF,
67 + EXPR_ASM_OPERAND,
67 68 };
68 69
69 70
70 71 /*
71 72 * Flags for tracking the promotion of constness related attributes
72 73 * from subexpressions to their parents.
73 74 *
74 75 * The flags are not independent as one might imply another.
75 76 * The implications are as follows:
76 77 * - CEF_INT, CEF_ENUM and
77 78 * CEF_CHAR imply CEF_ICE.
78 79 *
79 80 * Use the CEF_*_SET_MASK and CEF_*_CLEAR_MASK
80 81 * helper macros defined below to set or clear one of these flags.
81 82 */
82 83 enum constexpr_flag {
83 84 CEF_NONE = 0,
84 85 /*
85 86 * A constant in the sense of [6.4.4]:
86 87 * - Integer constant [6.4.4.1]
87 88 * - Floating point constant [6.4.4.2]
88 89 * - Enumeration constant [6.4.4.3]
89 90 * - Character constant [6.4.4.4]
90 91 */
91 92 CEF_INT = (1 << 0),
92 93 CEF_FLOAT = (1 << 1),
93 94 CEF_ENUM = (1 << 2),
94 95 CEF_CHAR = (1 << 3),
95 96
96 97 /*
97 98 * A constant expression in the sense of [6.6]:
98 99 * - integer constant expression [6.6(6)]
99 100 * - arithmetic constant expression [6.6(8)]
100 101 * - address constant [6.6(9)]
101 102 */
102 103 CEF_ICE = (1 << 4),
103 104 CEF_ACE = (1 << 5),
104 105 CEF_ADDR = (1 << 6),
105 106
106 107 /* integer constant expression => arithmetic constant expression */
107 108 CEF_SET_ICE = (CEF_ICE | CEF_ACE),
108 109
109 110 /* integer constant => integer constant expression */
110 111 CEF_SET_INT = (CEF_INT | CEF_SET_ICE),
111 112
112 113 /* floating point constant => arithmetic constant expression */
113 114 CEF_SET_FLOAT = (CEF_FLOAT | CEF_ACE),
114 115
115 116 /* enumeration constant => integer constant expression */
116 117 CEF_SET_ENUM = (CEF_ENUM | CEF_SET_ICE),
117 118
118 119 /* character constant => integer constant expression */
119 120 CEF_SET_CHAR = (CEF_CHAR | CEF_SET_ICE),
120 121
121 122 /*
122 123 * Remove any "Constant" [6.4.4] flag, but retain the "constant
123 124 * expression" [6.6] flags.
124 125 */
125 126 CEF_CONST_MASK = (CEF_INT | CEF_FLOAT | CEF_CHAR),
126 127
127 128 /*
128 129 * not an integer constant expression => neither of integer,
129 130 * enumeration and character constant
130 131 */
131 132 CEF_CLR_ICE = (CEF_ICE | CEF_INT | CEF_ENUM | CEF_CHAR),
132 133 };
133 134
134 135 enum {
135 136 Handled = 1 << 0,
136 137 Fake = 1 << 1,
137 138 }; /* for expr->flags */
138 139
139 140 enum {
140 141 Taint_comma = 1,
141 142 }; /* for expr->taint */
142 143
143 144 struct expression {
144 145 enum expression_type type:8;
145 146 unsigned flags:8;
146 147 unsigned smatch_flags:16;
147 148 int op;
148 149 struct position pos;
149 150 struct symbol *ctype;
150 151 unsigned long parent;
151 152 union {
152 153 // EXPR_VALUE
153 154 struct {
154 155 unsigned long long value;
155 156 unsigned taint;
156 157 };
157 158
158 159 // EXPR_FVALUE
159 160 long double fvalue;
160 161
161 162 // EXPR_STRING
162 163 struct {
163 164 int wide;
164 165 struct string *string;
165 166 };
166 167
167 168 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
168 169 struct /* unop */ {
169 170 struct expression *unop;
170 171 unsigned long op_value;
171 172 };
172 173
173 174 // EXPR_SYMBOL, EXPR_TYPE
174 175 struct /* symbol_arg */ {
175 176 struct symbol *symbol;
176 177 struct ident *symbol_name;
177 178 };
178 179
179 180 // EXPR_STATEMENT
180 181 struct statement *statement;
181 182
182 183 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
183 184 struct /* binop_arg */ {
184 185 struct expression *left, *right;
185 186 };
186 187 // EXPR_DEREF
↓ open down ↓ |
110 lines elided |
↑ open up ↑ |
187 188 struct /* deref_arg */ {
188 189 struct expression *deref;
189 190 struct ident *member;
190 191 int member_offset;
191 192 };
192 193 // EXPR_SLICE
193 194 struct /* slice */ {
194 195 struct expression *base;
195 196 unsigned r_bitpos, r_nrbits;
196 197 };
197 - // EXPR_CAST and EXPR_SIZEOF
198 + // EXPR_CAST, EXPR_FORCE_CAST, EXPR_IMPLIED_CAST,
199 + // EXPR_SIZEOF, EXPR_ALIGNOF and EXPR_PTRSIZEOF
198 200 struct /* cast_arg */ {
199 201 struct symbol *cast_type;
200 202 struct expression *cast_expression;
201 203 };
202 204 // EXPR_CONDITIONAL
203 205 // EXPR_SELECT
204 206 struct /* conditional_expr */ {
205 207 struct expression *conditional, *cond_true, *cond_false;
206 208 };
207 209 // EXPR_CALL
208 210 struct /* call_expr */ {
209 211 struct expression *fn;
210 212 struct expression_list *args;
211 213 };
212 214 // EXPR_LABEL
213 215 struct /* label_expr */ {
214 216 struct symbol *label_symbol;
215 217 };
216 218 // EXPR_INITIALIZER
217 219 struct expression_list *expr_list;
218 220 // EXPR_IDENTIFIER
219 221 struct /* ident_expr */ {
220 222 int offset;
221 223 struct ident *expr_ident;
222 224 struct symbol *field;
223 225 struct expression *ident_expression;
224 226 };
225 227 // EXPR_INDEX
226 228 struct /* index_expr */ {
227 229 unsigned int idx_from, idx_to;
228 230 struct expression *idx_expression;
229 231 };
230 232 // EXPR_POS
231 233 struct /* initpos_expr */ {
232 234 unsigned int init_offset, init_nr;
233 235 struct expression *init_expr;
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
234 236 };
235 237 // EXPR_OFFSETOF
236 238 struct {
237 239 struct symbol *in;
238 240 struct expression *down;
239 241 union {
240 242 struct ident *ident;
241 243 struct expression *index;
242 244 };
243 245 };
246 + // EXPR_ASM_OPERAND
247 + struct {
248 + struct ident *name;
249 + struct expression *constraint;
250 + struct expression *expr;
251 + };
244 252 };
245 253 };
246 254
247 -/* Constant expression values */
248 -int is_zero_constant(struct expression *);
255 +///
256 +// Constant expression values
257 +// --------------------------
258 +
259 +///
260 +// test if an expression evaluates to the constant ``0``.
261 +// @return: ``1`` if @expr evaluate to ``0``,
262 +// ``0`` otherwise.
263 +int is_zero_constant(struct expression *expr);
264 +
265 +///
266 +// test the compile time truth value of an expression
267 +// @return:
268 +// * ``-1`` if @expr is not constant,
269 +// * ``0`` or ``1`` depending on the truth value of @expr.
249 270 int expr_truth_value(struct expression *expr);
271 +
250 272 long long get_expression_value(struct expression *);
251 273 long long const_expression_value(struct expression *);
252 274 long long get_expression_value_silent(struct expression *expr);
253 275
254 276 /* Expression parsing */
255 277 struct token *parse_expression(struct token *token, struct expression **tree);
256 278 struct token *conditional_expression(struct token *token, struct expression **tree);
257 279 struct token *primary_expression(struct token *token, struct expression **tree);
258 280 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
259 281 struct token *assignment_expression(struct token *token, struct expression **tree);
260 282
261 283 extern void evaluate_symbol_list(struct symbol_list *list);
262 284 extern struct symbol *evaluate_statement(struct statement *stmt);
263 285 extern struct symbol *evaluate_expression(struct expression *);
264 286 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset);
265 287
266 288 extern int expand_symbol(struct symbol *);
267 289
268 290 static inline struct expression *alloc_expression(struct position pos, int type)
269 291 {
270 292 struct expression *expr = __alloc_expression(0);
271 293 expr->type = type;
272 294 expr->pos = pos;
273 295 expr->flags = CEF_NONE;
274 296 return expr;
275 297 }
276 298
277 299 static inline struct expression *alloc_const_expression(struct position pos, int value)
278 300 {
279 301 struct expression *expr = __alloc_expression(0);
280 302 expr->type = EXPR_VALUE;
281 303 expr->pos = pos;
282 304 expr->value = value;
283 305 expr->ctype = &int_ctype;
284 306 expr->flags = CEF_SET_INT;
285 307 return expr;
286 308 }
287 309
288 310 /* Type name parsing */
289 311 struct token *typename(struct token *, struct symbol **, int *);
290 312
291 313 static inline int lookup_type(struct token *token)
292 314 {
293 315 if (token->pos.type == TOKEN_IDENT) {
294 316 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
295 317 return sym && (sym->namespace & NS_TYPEDEF);
296 318 }
297 319 return 0;
298 320 }
299 321
300 322 /* Statement parsing */
301 323 struct statement *alloc_statement(struct position pos, int type);
302 324 struct token *initializer(struct expression **tree, struct token *token);
303 325 struct token *compound_statement(struct token *, struct statement *);
304 326
305 327 /* The preprocessor calls this 'constant_expression()' */
306 328 #define constant_expression(token,tree) conditional_expression(token, tree)
307 329
308 330 /* Cast folding of constant values.. */
309 331 void cast_value(struct expression *expr, struct symbol *newtype,
310 332 struct expression *old, struct symbol *oldtype);
311 333
312 334 #endif
↓ open down ↓ |
53 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX