xref: /openbmc/linux/tools/perf/util/expr.y (revision 4a4a9bf9075fbc753ab20f05347fd1482d4801e4)
107516736SAndi Kleen /* Simple expression parser */
207516736SAndi Kleen %{
326226a97SJiri Olsa #define YYDEBUG 1
43f965a7dSIan Rogers #include <assert.h>
5edfe7f55SIan Rogers #include <math.h>
61e7ab829SIan Rogers #include <stdlib.h>
707516736SAndi Kleen #include "util/debug.h"
807516736SAndi Kleen #define IN_EXPR_Y 1
907516736SAndi Kleen #include "expr.h"
1007516736SAndi Kleen %}
1107516736SAndi Kleen 
12fc8c0a99SJiri Olsa %define api.pure full
13fc8c0a99SJiri Olsa 
1407516736SAndi Kleen %parse-param { double *final_val }
15aecce63eSJiri Olsa %parse-param { struct expr_parse_ctx *ctx }
163f965a7dSIan Rogers %parse-param { bool compute_ids }
1726226a97SJiri Olsa %parse-param {void *scanner}
1826226a97SJiri Olsa %lex-param {void* scanner}
1907516736SAndi Kleen 
2007516736SAndi Kleen %union {
2107516736SAndi Kleen 	double	 num;
2226226a97SJiri Olsa 	char	*str;
233f965a7dSIan Rogers 	struct ids {
243f965a7dSIan Rogers 		/*
253f965a7dSIan Rogers 		 * When creating ids, holds the working set of event ids. NULL
263f965a7dSIan Rogers 		 * implies the set is empty.
273f965a7dSIan Rogers 		 */
283f965a7dSIan Rogers 		struct hashmap *ids;
293f965a7dSIan Rogers 		/*
303f965a7dSIan Rogers 		 * The metric value. When not creating ids this is the value
313f965a7dSIan Rogers 		 * read from a counter, a constant or some computed value. When
323f965a7dSIan Rogers 		 * creating ids the value is either a constant or BOTTOM. NAN is
333f965a7dSIan Rogers 		 * used as the special BOTTOM value, representing a "set of all
343f965a7dSIan Rogers 		 * values" case.
353f965a7dSIan Rogers 		 */
363f965a7dSIan Rogers 		double val;
373f965a7dSIan Rogers 	} ids;
3807516736SAndi Kleen }
3907516736SAndi Kleen 
40*4a4a9bf9SIan Rogers %token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT HAS_EVENT EXPR_ERROR
41d73bad06SAndi Kleen %left MIN MAX IF
4207516736SAndi Kleen %left '|'
4307516736SAndi Kleen %left '^'
4407516736SAndi Kleen %left '&'
45ff1a12f9SIan Rogers %left '<' '>'
4607516736SAndi Kleen %left '-' '+'
4707516736SAndi Kleen %left '*' '/' '%'
4807516736SAndi Kleen %left NEG NOT
493613f6c1SIan Rogers %type <num> NUMBER LITERAL
50aed0d6f8SIan Rogers %type <str> ID
51aed0d6f8SIan Rogers %destructor { free ($$); } <str>
523f965a7dSIan Rogers %type <ids> expr if_expr
533f965a7dSIan Rogers %destructor { ids__free($$.ids); } <ids>
5407516736SAndi Kleen 
5507516736SAndi Kleen %{
5626226a97SJiri Olsa static void expr_error(double *final_val __maybe_unused,
57aecce63eSJiri Olsa 		       struct expr_parse_ctx *ctx __maybe_unused,
583f965a7dSIan Rogers 		       bool compute_ids __maybe_unused,
5926226a97SJiri Olsa 		       void *scanner,
6007516736SAndi Kleen 		       const char *s)
6107516736SAndi Kleen {
6207516736SAndi Kleen 	pr_debug("%s\n", s);
6307516736SAndi Kleen }
6407516736SAndi Kleen 
653f965a7dSIan Rogers /*
663f965a7dSIan Rogers  * During compute ids, the special "bottom" value uses NAN to represent the set
673f965a7dSIan Rogers  * of all values. NAN is selected as it isn't a useful constant value.
683f965a7dSIan Rogers  */
693f965a7dSIan Rogers #define BOTTOM NAN
703f965a7dSIan Rogers 
71970f7afeSIan Rogers /* During computing ids, does val represent a constant (non-BOTTOM) value? */
72970f7afeSIan Rogers static bool is_const(double val)
73970f7afeSIan Rogers {
74970f7afeSIan Rogers 	return isfinite(val);
75970f7afeSIan Rogers }
76970f7afeSIan Rogers 
773f965a7dSIan Rogers static struct ids union_expr(struct ids ids1, struct ids ids2)
783f965a7dSIan Rogers {
793f965a7dSIan Rogers 	struct ids result = {
803f965a7dSIan Rogers 		.val = BOTTOM,
813f965a7dSIan Rogers 		.ids = ids__union(ids1.ids, ids2.ids),
823f965a7dSIan Rogers 	};
833f965a7dSIan Rogers 	return result;
843f965a7dSIan Rogers }
853f965a7dSIan Rogers 
861e7ab829SIan Rogers static struct ids handle_id(struct expr_parse_ctx *ctx, char *id,
879aba0adaSIan Rogers 			    bool compute_ids, bool source_count)
881e7ab829SIan Rogers {
891e7ab829SIan Rogers 	struct ids result;
901e7ab829SIan Rogers 
911e7ab829SIan Rogers 	if (!compute_ids) {
921e7ab829SIan Rogers 		/*
931e7ab829SIan Rogers 		 * Compute the event's value from ID. If the ID isn't known then
941e7ab829SIan Rogers 		 * it isn't used to compute the formula so set to NAN.
951e7ab829SIan Rogers 		 */
961e7ab829SIan Rogers 		struct expr_id_data *data;
971e7ab829SIan Rogers 
981e7ab829SIan Rogers 		result.val = NAN;
999aba0adaSIan Rogers 		if (expr__resolve_id(ctx, id, &data) == 0) {
1009aba0adaSIan Rogers 			result.val = source_count
1019aba0adaSIan Rogers 				? expr_id_data__source_count(data)
1029aba0adaSIan Rogers 				: expr_id_data__value(data);
1039aba0adaSIan Rogers 		}
1041e7ab829SIan Rogers 		result.ids = NULL;
1051e7ab829SIan Rogers 		free(id);
1061e7ab829SIan Rogers 	} else {
1071e7ab829SIan Rogers 		/*
1081e7ab829SIan Rogers 		 * Set the value to BOTTOM to show that any value is possible
1091e7ab829SIan Rogers 		 * when the event is computed. Create a set of just the ID.
1101e7ab829SIan Rogers 		 */
1111e7ab829SIan Rogers 		result.val = BOTTOM;
1121e7ab829SIan Rogers 		result.ids = ids__new();
1131e7ab829SIan Rogers 		if (!result.ids || ids__insert(result.ids, id)) {
1141e7ab829SIan Rogers 			pr_err("Error creating IDs for '%s'", id);
1151e7ab829SIan Rogers 			free(id);
1161e7ab829SIan Rogers 		}
1171e7ab829SIan Rogers 	}
1181e7ab829SIan Rogers 	return result;
1191e7ab829SIan Rogers }
1201e7ab829SIan Rogers 
121970f7afeSIan Rogers /*
122970f7afeSIan Rogers  * If we're not computing ids or $1 and $3 are constants, compute the new
123970f7afeSIan Rogers  * constant value using OP. Its invariant that there are no ids.  If computing
124970f7afeSIan Rogers  * ids for non-constants union the set of IDs that must be computed.
125970f7afeSIan Rogers  */
126e87576c5SIan Rogers #define BINARY_OP(RESULT, OP, LHS, RHS)					\
127970f7afeSIan Rogers 	if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
128970f7afeSIan Rogers 		assert(LHS.ids == NULL);				\
129970f7afeSIan Rogers 		assert(RHS.ids == NULL);				\
13079802934SIan Rogers 		if (isnan(LHS.val) || isnan(RHS.val)) {			\
13179802934SIan Rogers 			RESULT.val = NAN;				\
13279802934SIan Rogers 		} else {						\
1333f965a7dSIan Rogers 			RESULT.val = LHS.val OP RHS.val;		\
13479802934SIan Rogers 		}							\
1353f965a7dSIan Rogers 		RESULT.ids = NULL;					\
1363f965a7dSIan Rogers 	} else {							\
1373f965a7dSIan Rogers 	        RESULT = union_expr(LHS, RHS);				\
1383f965a7dSIan Rogers 	}
139e87576c5SIan Rogers 
14007516736SAndi Kleen %}
14107516736SAndi Kleen %%
14207516736SAndi Kleen 
1433f965a7dSIan Rogers start: if_expr
14426226a97SJiri Olsa {
1453f965a7dSIan Rogers 	if (compute_ids)
1463f965a7dSIan Rogers 		ctx->ids = ids__union($1.ids, ctx->ids);
14726226a97SJiri Olsa 
1483f965a7dSIan Rogers 	if (final_val)
1493f965a7dSIan Rogers 		*final_val = $1.val;
1503f965a7dSIan Rogers }
1513f965a7dSIan Rogers ;
152d73bad06SAndi Kleen 
1534b65fc7bSIan Rogers if_expr: expr IF expr ELSE if_expr
154c924e0ccSIan Rogers {
1553f965a7dSIan Rogers 	if (fpclassify($3.val) == FP_ZERO) {
156a8e4e880SIan Rogers 		/*
157a8e4e880SIan Rogers 		 * The IF expression evaluated to 0 so treat as false, take the
158a8e4e880SIan Rogers 		 * ELSE and discard everything else.
159a8e4e880SIan Rogers 		 */
1603f965a7dSIan Rogers 		$$.val = $5.val;
161a8e4e880SIan Rogers 		$$.ids = $5.ids;
162a8e4e880SIan Rogers 		ids__free($1.ids);
163a8e4e880SIan Rogers 		ids__free($3.ids);
164a8e4e880SIan Rogers 	} else if (!compute_ids || is_const($3.val)) {
165a8e4e880SIan Rogers 		/*
166a8e4e880SIan Rogers 		 * If ids aren't computed then treat the expression as true. If
167a8e4e880SIan Rogers 		 * ids are being computed and the IF expr is a non-zero
168a8e4e880SIan Rogers 		 * constant, then also evaluate the true case.
169a8e4e880SIan Rogers 		 */
1703f965a7dSIan Rogers 		$$.val = $1.val;
171a8e4e880SIan Rogers 		$$.ids = $1.ids;
172a8e4e880SIan Rogers 		ids__free($3.ids);
173a8e4e880SIan Rogers 		ids__free($5.ids);
17494886961SIan Rogers 	} else if ($1.val == $5.val) {
17594886961SIan Rogers 		/*
17694886961SIan Rogers 		 * LHS == RHS, so both are an identical constant. No need to
17794886961SIan Rogers 		 * evaluate any events.
17894886961SIan Rogers 		 */
17994886961SIan Rogers 		$$.val = $1.val;
18094886961SIan Rogers 		$$.ids = NULL;
18194886961SIan Rogers 		ids__free($1.ids);
18294886961SIan Rogers 		ids__free($3.ids);
18394886961SIan Rogers 		ids__free($5.ids);
1843f965a7dSIan Rogers 	} else {
185a8e4e880SIan Rogers 		/*
186a8e4e880SIan Rogers 		 * Value is either the LHS or RHS and we need the IF expression
187a8e4e880SIan Rogers 		 * to compute it.
188a8e4e880SIan Rogers 		 */
1893f965a7dSIan Rogers 		$$ = union_expr($1, union_expr($3, $5));
1903f965a7dSIan Rogers 	}
191c924e0ccSIan Rogers }
192d73bad06SAndi Kleen | expr
19307516736SAndi Kleen ;
19407516736SAndi Kleen 
19507516736SAndi Kleen expr: NUMBER
196c924e0ccSIan Rogers {
1973f965a7dSIan Rogers 	$$.val = $1;
1983f965a7dSIan Rogers 	$$.ids = NULL;
199c924e0ccSIan Rogers }
2009aba0adaSIan Rogers | ID				{ $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
2019aba0adaSIan Rogers | SOURCE_COUNT '(' ID ')'	{ $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
202*4a4a9bf9SIan Rogers | HAS_EVENT '(' ID ')'
203*4a4a9bf9SIan Rogers {
204*4a4a9bf9SIan Rogers 	$$.val = expr__has_event(ctx, compute_ids, $3);
205*4a4a9bf9SIan Rogers 	$$.ids = NULL;
206*4a4a9bf9SIan Rogers 	free($3);
207*4a4a9bf9SIan Rogers }
2086f765bbbSIan Rogers | expr '|' expr
2096f765bbbSIan Rogers {
2106f765bbbSIan Rogers 	if (is_const($1.val) && is_const($3.val)) {
2116f765bbbSIan Rogers 		assert($1.ids == NULL);
2126f765bbbSIan Rogers 		assert($3.ids == NULL);
2136f765bbbSIan Rogers 		$$.ids = NULL;
2146f765bbbSIan Rogers 		$$.val = (fpclassify($1.val) == FP_ZERO && fpclassify($3.val) == FP_ZERO) ? 0 : 1;
2156f765bbbSIan Rogers 	} else if (is_const($1.val)) {
2166f765bbbSIan Rogers 		assert($1.ids == NULL);
2176f765bbbSIan Rogers 		if (fpclassify($1.val) == FP_ZERO) {
2186f765bbbSIan Rogers 			$$ = $3;
2196f765bbbSIan Rogers 		} else {
2206f765bbbSIan Rogers 			$$.val = 1;
2216f765bbbSIan Rogers 			$$.ids = NULL;
2226f765bbbSIan Rogers 			ids__free($3.ids);
2236f765bbbSIan Rogers 		}
2246f765bbbSIan Rogers 	} else if (is_const($3.val)) {
2256f765bbbSIan Rogers 		assert($3.ids == NULL);
2266f765bbbSIan Rogers 		if (fpclassify($3.val) == FP_ZERO) {
2276f765bbbSIan Rogers 			$$ = $1;
2286f765bbbSIan Rogers 		} else {
2296f765bbbSIan Rogers 			$$.val = 1;
2306f765bbbSIan Rogers 			$$.ids = NULL;
2316f765bbbSIan Rogers 			ids__free($1.ids);
2326f765bbbSIan Rogers 		}
2336f765bbbSIan Rogers 	} else {
2346f765bbbSIan Rogers 		$$ = union_expr($1, $3);
2356f765bbbSIan Rogers 	}
2366f765bbbSIan Rogers }
2376f765bbbSIan Rogers | expr '&' expr
2386f765bbbSIan Rogers {
2396f765bbbSIan Rogers 	if (is_const($1.val) && is_const($3.val)) {
2406f765bbbSIan Rogers 		assert($1.ids == NULL);
2416f765bbbSIan Rogers 		assert($3.ids == NULL);
2426f765bbbSIan Rogers 		$$.val = (fpclassify($1.val) != FP_ZERO && fpclassify($3.val) != FP_ZERO) ? 1 : 0;
2436f765bbbSIan Rogers 		$$.ids = NULL;
2446f765bbbSIan Rogers 	} else if (is_const($1.val)) {
2456f765bbbSIan Rogers 		assert($1.ids == NULL);
2466f765bbbSIan Rogers 		if (fpclassify($1.val) != FP_ZERO) {
2476f765bbbSIan Rogers 			$$ = $3;
2486f765bbbSIan Rogers 		} else {
2496f765bbbSIan Rogers 			$$.val = 0;
2506f765bbbSIan Rogers 			$$.ids = NULL;
2516f765bbbSIan Rogers 			ids__free($3.ids);
2526f765bbbSIan Rogers 		}
2536f765bbbSIan Rogers 	} else if (is_const($3.val)) {
2546f765bbbSIan Rogers 		assert($3.ids == NULL);
2556f765bbbSIan Rogers 		if (fpclassify($3.val) != FP_ZERO) {
2566f765bbbSIan Rogers 			$$ = $1;
2576f765bbbSIan Rogers 		} else {
2586f765bbbSIan Rogers 			$$.val = 0;
2596f765bbbSIan Rogers 			$$.ids = NULL;
2606f765bbbSIan Rogers 			ids__free($1.ids);
2616f765bbbSIan Rogers 		}
2626f765bbbSIan Rogers 	} else {
2636f765bbbSIan Rogers 		$$ = union_expr($1, $3);
2646f765bbbSIan Rogers 	}
2656f765bbbSIan Rogers }
2666f765bbbSIan Rogers | expr '^' expr
2676f765bbbSIan Rogers {
2686f765bbbSIan Rogers 	if (is_const($1.val) && is_const($3.val)) {
2696f765bbbSIan Rogers 		assert($1.ids == NULL);
2706f765bbbSIan Rogers 		assert($3.ids == NULL);
2716f765bbbSIan Rogers 		$$.val = (fpclassify($1.val) == FP_ZERO) != (fpclassify($3.val) == FP_ZERO) ? 1 : 0;
2726f765bbbSIan Rogers 		$$.ids = NULL;
2736f765bbbSIan Rogers 	} else {
2746f765bbbSIan Rogers 		$$ = union_expr($1, $3);
2756f765bbbSIan Rogers 	}
2766f765bbbSIan Rogers }
277e87576c5SIan Rogers | expr '<' expr { BINARY_OP($$, <, $1, $3); }
278e87576c5SIan Rogers | expr '>' expr { BINARY_OP($$, >, $1, $3); }
279e87576c5SIan Rogers | expr '+' expr { BINARY_OP($$, +, $1, $3); }
280e87576c5SIan Rogers | expr '-' expr { BINARY_OP($$, -, $1, $3); }
281e87576c5SIan Rogers | expr '*' expr { BINARY_OP($$, *, $1, $3); }
282c924e0ccSIan Rogers | expr '/' expr
283c924e0ccSIan Rogers {
2843f965a7dSIan Rogers 	if (fpclassify($3.val) == FP_ZERO) {
2859be27a5dSIan Rogers 		pr_debug("division by zero\n");
2862a939c86SIan Rogers 		assert($3.ids == NULL);
2872a939c86SIan Rogers 		if (compute_ids)
2882a939c86SIan Rogers 			ids__free($1.ids);
2892a939c86SIan Rogers 		$$.val = NAN;
2902a939c86SIan Rogers 		$$.ids = NULL;
291970f7afeSIan Rogers 	} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
292970f7afeSIan Rogers 		assert($1.ids == NULL);
293970f7afeSIan Rogers 		assert($3.ids == NULL);
2943f965a7dSIan Rogers 		$$.val = $1.val / $3.val;
2953f965a7dSIan Rogers 		$$.ids = NULL;
2963f965a7dSIan Rogers 	} else {
297970f7afeSIan Rogers 		/* LHS and/or RHS need computing from event IDs so union. */
2983f965a7dSIan Rogers 		$$ = union_expr($1, $3);
2993f965a7dSIan Rogers 	}
3009be27a5dSIan Rogers }
301c924e0ccSIan Rogers | expr '%' expr
302c924e0ccSIan Rogers {
3033f965a7dSIan Rogers 	if (fpclassify($3.val) == FP_ZERO) {
3049be27a5dSIan Rogers 		pr_debug("division by zero\n");
3059be27a5dSIan Rogers 		YYABORT;
306970f7afeSIan Rogers 	} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
307970f7afeSIan Rogers 		assert($1.ids == NULL);
308970f7afeSIan Rogers 		assert($3.ids == NULL);
3093f965a7dSIan Rogers 		$$.val = (long)$1.val % (long)$3.val;
3103f965a7dSIan Rogers 		$$.ids = NULL;
3113f965a7dSIan Rogers 	} else {
312970f7afeSIan Rogers 		/* LHS and/or RHS need computing from event IDs so union. */
3133f965a7dSIan Rogers 		$$ = union_expr($1, $3);
3143f965a7dSIan Rogers 	}
3159be27a5dSIan Rogers }
316c924e0ccSIan Rogers | D_RATIO '(' expr ',' expr ')'
317c924e0ccSIan Rogers {
3183f965a7dSIan Rogers 	if (fpclassify($5.val) == FP_ZERO) {
319970f7afeSIan Rogers 		/*
320970f7afeSIan Rogers 		 * Division by constant zero always yields zero and no events
321970f7afeSIan Rogers 		 * are necessary.
322970f7afeSIan Rogers 		 */
323970f7afeSIan Rogers 		assert($5.ids == NULL);
3243f965a7dSIan Rogers 		$$.val = 0.0;
325970f7afeSIan Rogers 		$$.ids = NULL;
326970f7afeSIan Rogers 		ids__free($3.ids);
327970f7afeSIan Rogers 	} else if (!compute_ids || (is_const($3.val) && is_const($5.val))) {
328970f7afeSIan Rogers 		assert($3.ids == NULL);
329970f7afeSIan Rogers 		assert($5.ids == NULL);
3303f965a7dSIan Rogers 		$$.val = $3.val / $5.val;
331970f7afeSIan Rogers 		$$.ids = NULL;
3323f965a7dSIan Rogers 	} else {
333970f7afeSIan Rogers 		/* LHS and/or RHS need computing from event IDs so union. */
3343f965a7dSIan Rogers 		$$ = union_expr($3, $5);
3357f8fdcbbSIan Rogers 	}
3367f8fdcbbSIan Rogers }
337c924e0ccSIan Rogers | '-' expr %prec NEG
338c924e0ccSIan Rogers {
3393f965a7dSIan Rogers 	$$.val = -$2.val;
3403f965a7dSIan Rogers 	$$.ids = $2.ids;
341c924e0ccSIan Rogers }
342c924e0ccSIan Rogers | '(' if_expr ')'
343c924e0ccSIan Rogers {
344c924e0ccSIan Rogers 	$$ = $2;
345c924e0ccSIan Rogers }
346c924e0ccSIan Rogers | MIN '(' expr ',' expr ')'
347c924e0ccSIan Rogers {
3483f965a7dSIan Rogers 	if (!compute_ids) {
3493f965a7dSIan Rogers 		$$.val = $3.val < $5.val ? $3.val : $5.val;
3503f965a7dSIan Rogers 		$$.ids = NULL;
3513f965a7dSIan Rogers 	} else {
3523f965a7dSIan Rogers 		$$ = union_expr($3, $5);
3533f965a7dSIan Rogers 	}
354c924e0ccSIan Rogers }
355c924e0ccSIan Rogers | MAX '(' expr ',' expr ')'
356c924e0ccSIan Rogers {
3573f965a7dSIan Rogers 	if (!compute_ids) {
3583f965a7dSIan Rogers 		$$.val = $3.val > $5.val ? $3.val : $5.val;
3593f965a7dSIan Rogers 		$$.ids = NULL;
3603f965a7dSIan Rogers 	} else {
3613f965a7dSIan Rogers 		$$ = union_expr($3, $5);
3623f965a7dSIan Rogers 	}
363c924e0ccSIan Rogers }
3643613f6c1SIan Rogers | LITERAL
365c924e0ccSIan Rogers {
3663613f6c1SIan Rogers 	$$.val = $1;
3673f965a7dSIan Rogers 	$$.ids = NULL;
368c924e0ccSIan Rogers }
36907516736SAndi Kleen ;
37007516736SAndi Kleen 
37107516736SAndi Kleen %%
372