xref: /openbmc/linux/tools/perf/util/expr.y (revision e8069f5a)
1 /* Simple expression parser */
2 %{
3 #define YYDEBUG 1
4 #include <assert.h>
5 #include <math.h>
6 #include <stdlib.h>
7 #include "util/debug.h"
8 #define IN_EXPR_Y 1
9 #include "expr.h"
10 %}
11 
12 %define api.pure full
13 
14 %parse-param { double *final_val }
15 %parse-param { struct expr_parse_ctx *ctx }
16 %parse-param { bool compute_ids }
17 %parse-param {void *scanner}
18 %lex-param {void* scanner}
19 
20 %union {
21 	double	 num;
22 	char	*str;
23 	struct ids {
24 		/*
25 		 * When creating ids, holds the working set of event ids. NULL
26 		 * implies the set is empty.
27 		 */
28 		struct hashmap *ids;
29 		/*
30 		 * The metric value. When not creating ids this is the value
31 		 * read from a counter, a constant or some computed value. When
32 		 * creating ids the value is either a constant or BOTTOM. NAN is
33 		 * used as the special BOTTOM value, representing a "set of all
34 		 * values" case.
35 		 */
36 		double val;
37 	} ids;
38 }
39 
40 %token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT EXPR_ERROR
41 %left MIN MAX IF
42 %left '|'
43 %left '^'
44 %left '&'
45 %left '<' '>'
46 %left '-' '+'
47 %left '*' '/' '%'
48 %left NEG NOT
49 %type <num> NUMBER LITERAL
50 %type <str> ID
51 %destructor { free ($$); } <str>
52 %type <ids> expr if_expr
53 %destructor { ids__free($$.ids); } <ids>
54 
55 %{
56 static void expr_error(double *final_val __maybe_unused,
57 		       struct expr_parse_ctx *ctx __maybe_unused,
58 		       bool compute_ids __maybe_unused,
59 		       void *scanner,
60 		       const char *s)
61 {
62 	pr_debug("%s\n", s);
63 }
64 
65 /*
66  * During compute ids, the special "bottom" value uses NAN to represent the set
67  * of all values. NAN is selected as it isn't a useful constant value.
68  */
69 #define BOTTOM NAN
70 
71 /* During computing ids, does val represent a constant (non-BOTTOM) value? */
72 static bool is_const(double val)
73 {
74 	return isfinite(val);
75 }
76 
77 static struct ids union_expr(struct ids ids1, struct ids ids2)
78 {
79 	struct ids result = {
80 		.val = BOTTOM,
81 		.ids = ids__union(ids1.ids, ids2.ids),
82 	};
83 	return result;
84 }
85 
86 static struct ids handle_id(struct expr_parse_ctx *ctx, char *id,
87 			    bool compute_ids, bool source_count)
88 {
89 	struct ids result;
90 
91 	if (!compute_ids) {
92 		/*
93 		 * Compute the event's value from ID. If the ID isn't known then
94 		 * it isn't used to compute the formula so set to NAN.
95 		 */
96 		struct expr_id_data *data;
97 
98 		result.val = NAN;
99 		if (expr__resolve_id(ctx, id, &data) == 0) {
100 			result.val = source_count
101 				? expr_id_data__source_count(data)
102 				: expr_id_data__value(data);
103 		}
104 		result.ids = NULL;
105 		free(id);
106 	} else {
107 		/*
108 		 * Set the value to BOTTOM to show that any value is possible
109 		 * when the event is computed. Create a set of just the ID.
110 		 */
111 		result.val = BOTTOM;
112 		result.ids = ids__new();
113 		if (!result.ids || ids__insert(result.ids, id)) {
114 			pr_err("Error creating IDs for '%s'", id);
115 			free(id);
116 		}
117 	}
118 	return result;
119 }
120 
121 /*
122  * If we're not computing ids or $1 and $3 are constants, compute the new
123  * constant value using OP. Its invariant that there are no ids.  If computing
124  * ids for non-constants union the set of IDs that must be computed.
125  */
126 #define BINARY_OP(RESULT, OP, LHS, RHS)					\
127 	if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
128 		assert(LHS.ids == NULL);				\
129 		assert(RHS.ids == NULL);				\
130 		if (isnan(LHS.val) || isnan(RHS.val)) {			\
131 			RESULT.val = NAN;				\
132 		} else {						\
133 			RESULT.val = LHS.val OP RHS.val;		\
134 		}							\
135 		RESULT.ids = NULL;					\
136 	} else {							\
137 	        RESULT = union_expr(LHS, RHS);				\
138 	}
139 
140 %}
141 %%
142 
143 start: if_expr
144 {
145 	if (compute_ids)
146 		ctx->ids = ids__union($1.ids, ctx->ids);
147 
148 	if (final_val)
149 		*final_val = $1.val;
150 }
151 ;
152 
153 if_expr: expr IF expr ELSE if_expr
154 {
155 	if (fpclassify($3.val) == FP_ZERO) {
156 		/*
157 		 * The IF expression evaluated to 0 so treat as false, take the
158 		 * ELSE and discard everything else.
159 		 */
160 		$$.val = $5.val;
161 		$$.ids = $5.ids;
162 		ids__free($1.ids);
163 		ids__free($3.ids);
164 	} else if (!compute_ids || is_const($3.val)) {
165 		/*
166 		 * If ids aren't computed then treat the expression as true. If
167 		 * ids are being computed and the IF expr is a non-zero
168 		 * constant, then also evaluate the true case.
169 		 */
170 		$$.val = $1.val;
171 		$$.ids = $1.ids;
172 		ids__free($3.ids);
173 		ids__free($5.ids);
174 	} else if ($1.val == $5.val) {
175 		/*
176 		 * LHS == RHS, so both are an identical constant. No need to
177 		 * evaluate any events.
178 		 */
179 		$$.val = $1.val;
180 		$$.ids = NULL;
181 		ids__free($1.ids);
182 		ids__free($3.ids);
183 		ids__free($5.ids);
184 	} else {
185 		/*
186 		 * Value is either the LHS or RHS and we need the IF expression
187 		 * to compute it.
188 		 */
189 		$$ = union_expr($1, union_expr($3, $5));
190 	}
191 }
192 | expr
193 ;
194 
195 expr: NUMBER
196 {
197 	$$.val = $1;
198 	$$.ids = NULL;
199 }
200 | ID				{ $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
201 | SOURCE_COUNT '(' ID ')'	{ $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
202 | expr '|' expr
203 {
204 	if (is_const($1.val) && is_const($3.val)) {
205 		assert($1.ids == NULL);
206 		assert($3.ids == NULL);
207 		$$.ids = NULL;
208 		$$.val = (fpclassify($1.val) == FP_ZERO && fpclassify($3.val) == FP_ZERO) ? 0 : 1;
209 	} else if (is_const($1.val)) {
210 		assert($1.ids == NULL);
211 		if (fpclassify($1.val) == FP_ZERO) {
212 			$$ = $3;
213 		} else {
214 			$$.val = 1;
215 			$$.ids = NULL;
216 			ids__free($3.ids);
217 		}
218 	} else if (is_const($3.val)) {
219 		assert($3.ids == NULL);
220 		if (fpclassify($3.val) == FP_ZERO) {
221 			$$ = $1;
222 		} else {
223 			$$.val = 1;
224 			$$.ids = NULL;
225 			ids__free($1.ids);
226 		}
227 	} else {
228 		$$ = union_expr($1, $3);
229 	}
230 }
231 | expr '&' expr
232 {
233 	if (is_const($1.val) && is_const($3.val)) {
234 		assert($1.ids == NULL);
235 		assert($3.ids == NULL);
236 		$$.val = (fpclassify($1.val) != FP_ZERO && fpclassify($3.val) != FP_ZERO) ? 1 : 0;
237 		$$.ids = NULL;
238 	} else if (is_const($1.val)) {
239 		assert($1.ids == NULL);
240 		if (fpclassify($1.val) != FP_ZERO) {
241 			$$ = $3;
242 		} else {
243 			$$.val = 0;
244 			$$.ids = NULL;
245 			ids__free($3.ids);
246 		}
247 	} else if (is_const($3.val)) {
248 		assert($3.ids == NULL);
249 		if (fpclassify($3.val) != FP_ZERO) {
250 			$$ = $1;
251 		} else {
252 			$$.val = 0;
253 			$$.ids = NULL;
254 			ids__free($1.ids);
255 		}
256 	} else {
257 		$$ = union_expr($1, $3);
258 	}
259 }
260 | expr '^' expr
261 {
262 	if (is_const($1.val) && is_const($3.val)) {
263 		assert($1.ids == NULL);
264 		assert($3.ids == NULL);
265 		$$.val = (fpclassify($1.val) == FP_ZERO) != (fpclassify($3.val) == FP_ZERO) ? 1 : 0;
266 		$$.ids = NULL;
267 	} else {
268 		$$ = union_expr($1, $3);
269 	}
270 }
271 | expr '<' expr { BINARY_OP($$, <, $1, $3); }
272 | expr '>' expr { BINARY_OP($$, >, $1, $3); }
273 | expr '+' expr { BINARY_OP($$, +, $1, $3); }
274 | expr '-' expr { BINARY_OP($$, -, $1, $3); }
275 | expr '*' expr { BINARY_OP($$, *, $1, $3); }
276 | expr '/' expr
277 {
278 	if (fpclassify($3.val) == FP_ZERO) {
279 		pr_debug("division by zero\n");
280 		assert($3.ids == NULL);
281 		if (compute_ids)
282 			ids__free($1.ids);
283 		$$.val = NAN;
284 		$$.ids = NULL;
285 	} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
286 		assert($1.ids == NULL);
287 		assert($3.ids == NULL);
288 		$$.val = $1.val / $3.val;
289 		$$.ids = NULL;
290 	} else {
291 		/* LHS and/or RHS need computing from event IDs so union. */
292 		$$ = union_expr($1, $3);
293 	}
294 }
295 | expr '%' expr
296 {
297 	if (fpclassify($3.val) == FP_ZERO) {
298 		pr_debug("division by zero\n");
299 		YYABORT;
300 	} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
301 		assert($1.ids == NULL);
302 		assert($3.ids == NULL);
303 		$$.val = (long)$1.val % (long)$3.val;
304 		$$.ids = NULL;
305 	} else {
306 		/* LHS and/or RHS need computing from event IDs so union. */
307 		$$ = union_expr($1, $3);
308 	}
309 }
310 | D_RATIO '(' expr ',' expr ')'
311 {
312 	if (fpclassify($5.val) == FP_ZERO) {
313 		/*
314 		 * Division by constant zero always yields zero and no events
315 		 * are necessary.
316 		 */
317 		assert($5.ids == NULL);
318 		$$.val = 0.0;
319 		$$.ids = NULL;
320 		ids__free($3.ids);
321 	} else if (!compute_ids || (is_const($3.val) && is_const($5.val))) {
322 		assert($3.ids == NULL);
323 		assert($5.ids == NULL);
324 		$$.val = $3.val / $5.val;
325 		$$.ids = NULL;
326 	} else {
327 		/* LHS and/or RHS need computing from event IDs so union. */
328 		$$ = union_expr($3, $5);
329 	}
330 }
331 | '-' expr %prec NEG
332 {
333 	$$.val = -$2.val;
334 	$$.ids = $2.ids;
335 }
336 | '(' if_expr ')'
337 {
338 	$$ = $2;
339 }
340 | MIN '(' expr ',' expr ')'
341 {
342 	if (!compute_ids) {
343 		$$.val = $3.val < $5.val ? $3.val : $5.val;
344 		$$.ids = NULL;
345 	} else {
346 		$$ = union_expr($3, $5);
347 	}
348 }
349 | MAX '(' expr ',' expr ')'
350 {
351 	if (!compute_ids) {
352 		$$.val = $3.val > $5.val ? $3.val : $5.val;
353 		$$.ids = NULL;
354 	} else {
355 		$$ = union_expr($3, $5);
356 	}
357 }
358 | LITERAL
359 {
360 	$$.val = $1;
361 	$$.ids = NULL;
362 }
363 ;
364 
365 %%
366