xref: /openbmc/linux/tools/perf/util/expr.y (revision a8e4e880834b5dc53ff6b4cfc9f4268e61399976)
107516736SAndi Kleen /* Simple expression parser */
207516736SAndi Kleen %{
326226a97SJiri Olsa #define YYDEBUG 1
43f965a7dSIan Rogers #include <assert.h>
5edfe7f55SIan Rogers #include <math.h>
607516736SAndi Kleen #include "util/debug.h"
77f8fdcbbSIan Rogers #include "smt.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 
403f965a7dSIan Rogers %token ID NUMBER MIN MAX IF ELSE SMT_ON D_RATIO 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
49aed0d6f8SIan Rogers %type <num> NUMBER
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 
86970f7afeSIan Rogers /*
87970f7afeSIan Rogers  * If we're not computing ids or $1 and $3 are constants, compute the new
88970f7afeSIan Rogers  * constant value using OP. Its invariant that there are no ids.  If computing
89970f7afeSIan Rogers  * ids for non-constants union the set of IDs that must be computed.
90970f7afeSIan Rogers  */
91e87576c5SIan Rogers #define BINARY_LONG_OP(RESULT, OP, LHS, RHS)				\
92970f7afeSIan Rogers 	if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
93970f7afeSIan Rogers 		assert(LHS.ids == NULL);				\
94970f7afeSIan Rogers 		assert(RHS.ids == NULL);				\
953f965a7dSIan Rogers 		RESULT.val = (long)LHS.val OP (long)RHS.val;		\
963f965a7dSIan Rogers 		RESULT.ids = NULL;					\
973f965a7dSIan Rogers 	} else {							\
983f965a7dSIan Rogers 	        RESULT = union_expr(LHS, RHS);				\
993f965a7dSIan Rogers 	}
100e87576c5SIan Rogers 
101e87576c5SIan Rogers #define BINARY_OP(RESULT, OP, LHS, RHS)					\
102970f7afeSIan Rogers 	if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
103970f7afeSIan Rogers 		assert(LHS.ids == NULL);				\
104970f7afeSIan Rogers 		assert(RHS.ids == NULL);				\
1053f965a7dSIan Rogers 		RESULT.val = LHS.val OP RHS.val;			\
1063f965a7dSIan Rogers 		RESULT.ids = NULL;					\
1073f965a7dSIan Rogers 	} else {							\
1083f965a7dSIan Rogers 	        RESULT = union_expr(LHS, RHS);				\
1093f965a7dSIan Rogers 	}
110e87576c5SIan Rogers 
11107516736SAndi Kleen %}
11207516736SAndi Kleen %%
11307516736SAndi Kleen 
1143f965a7dSIan Rogers start: if_expr
11526226a97SJiri Olsa {
1163f965a7dSIan Rogers 	if (compute_ids)
1173f965a7dSIan Rogers 		ctx->ids = ids__union($1.ids, ctx->ids);
11826226a97SJiri Olsa 
1193f965a7dSIan Rogers 	if (final_val)
1203f965a7dSIan Rogers 		*final_val = $1.val;
1213f965a7dSIan Rogers }
1223f965a7dSIan Rogers ;
123d73bad06SAndi Kleen 
124c924e0ccSIan Rogers if_expr: expr IF expr ELSE expr
125c924e0ccSIan Rogers {
1263f965a7dSIan Rogers 	if (fpclassify($3.val) == FP_ZERO) {
127*a8e4e880SIan Rogers 		/*
128*a8e4e880SIan Rogers 		 * The IF expression evaluated to 0 so treat as false, take the
129*a8e4e880SIan Rogers 		 * ELSE and discard everything else.
130*a8e4e880SIan Rogers 		 */
1313f965a7dSIan Rogers 		$$.val = $5.val;
132*a8e4e880SIan Rogers 		$$.ids = $5.ids;
133*a8e4e880SIan Rogers 		ids__free($1.ids);
134*a8e4e880SIan Rogers 		ids__free($3.ids);
135*a8e4e880SIan Rogers 	} else if (!compute_ids || is_const($3.val)) {
136*a8e4e880SIan Rogers 		/*
137*a8e4e880SIan Rogers 		 * If ids aren't computed then treat the expression as true. If
138*a8e4e880SIan Rogers 		 * ids are being computed and the IF expr is a non-zero
139*a8e4e880SIan Rogers 		 * constant, then also evaluate the true case.
140*a8e4e880SIan Rogers 		 */
1413f965a7dSIan Rogers 		$$.val = $1.val;
142*a8e4e880SIan Rogers 		$$.ids = $1.ids;
143*a8e4e880SIan Rogers 		ids__free($3.ids);
144*a8e4e880SIan Rogers 		ids__free($5.ids);
1453f965a7dSIan Rogers 	} else {
146*a8e4e880SIan Rogers 		/*
147*a8e4e880SIan Rogers 		 * Value is either the LHS or RHS and we need the IF expression
148*a8e4e880SIan Rogers 		 * to compute it.
149*a8e4e880SIan Rogers 		 */
1503f965a7dSIan Rogers 		$$ = union_expr($1, union_expr($3, $5));
1513f965a7dSIan Rogers 	}
152c924e0ccSIan Rogers }
153d73bad06SAndi Kleen | expr
15407516736SAndi Kleen ;
15507516736SAndi Kleen 
15607516736SAndi Kleen expr: NUMBER
157c924e0ccSIan Rogers {
1583f965a7dSIan Rogers 	$$.val = $1;
1593f965a7dSIan Rogers 	$$.ids = NULL;
160c924e0ccSIan Rogers }
161c924e0ccSIan Rogers | ID
162c924e0ccSIan Rogers {
1633f965a7dSIan Rogers 	if (!compute_ids) {
1643f965a7dSIan Rogers 		/*
1653f965a7dSIan Rogers 		 * Compute the event's value from ID. If the ID isn't known then
1663f965a7dSIan Rogers 		 * it isn't used to compute the formula so set to NAN.
1673f965a7dSIan Rogers 		 */
1685c5f5e83SJiri Olsa 		struct expr_id_data *data;
1695c5f5e83SJiri Olsa 
1703f965a7dSIan Rogers 		$$.val = NAN;
171edfe7f55SIan Rogers 		if (expr__resolve_id(ctx, $1, &data) == 0)
1723f965a7dSIan Rogers 			$$.val = expr_id_data__value(data);
173edfe7f55SIan Rogers 
1743f965a7dSIan Rogers 		$$.ids = NULL;
17563657578SIan Rogers 		free($1);
1763f965a7dSIan Rogers 	} else {
1773f965a7dSIan Rogers 		/*
1783f965a7dSIan Rogers 		 * Set the value to BOTTOM to show that any value is possible
1793f965a7dSIan Rogers 		 * when the event is computed. Create a set of just the ID.
1803f965a7dSIan Rogers 		 */
1813f965a7dSIan Rogers 		$$.val = BOTTOM;
1823f965a7dSIan Rogers 		$$.ids = ids__new();
1833f965a7dSIan Rogers 		if (!$$.ids || ids__insert($$.ids, $1, ctx->parent))
1843f965a7dSIan Rogers 			YYABORT;
1853f965a7dSIan Rogers 	}
18607516736SAndi Kleen }
187e87576c5SIan Rogers | expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); }
188e87576c5SIan Rogers | expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); }
189e87576c5SIan Rogers | expr '^' expr { BINARY_LONG_OP($$, ^, $1, $3); }
190e87576c5SIan Rogers | expr '<' expr { BINARY_OP($$, <, $1, $3); }
191e87576c5SIan Rogers | expr '>' expr { BINARY_OP($$, >, $1, $3); }
192e87576c5SIan Rogers | expr '+' expr { BINARY_OP($$, +, $1, $3); }
193e87576c5SIan Rogers | expr '-' expr { BINARY_OP($$, -, $1, $3); }
194e87576c5SIan Rogers | expr '*' expr { BINARY_OP($$, *, $1, $3); }
195c924e0ccSIan Rogers | expr '/' expr
196c924e0ccSIan Rogers {
1973f965a7dSIan Rogers 	if (fpclassify($3.val) == FP_ZERO) {
1989be27a5dSIan Rogers 		pr_debug("division by zero\n");
1999be27a5dSIan Rogers 		YYABORT;
200970f7afeSIan Rogers 	} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
201970f7afeSIan Rogers 		assert($1.ids == NULL);
202970f7afeSIan Rogers 		assert($3.ids == NULL);
2033f965a7dSIan Rogers 		$$.val = $1.val / $3.val;
2043f965a7dSIan Rogers 		$$.ids = NULL;
2053f965a7dSIan Rogers 	} else {
206970f7afeSIan Rogers 		/* LHS and/or RHS need computing from event IDs so union. */
2073f965a7dSIan Rogers 		$$ = union_expr($1, $3);
2083f965a7dSIan Rogers 	}
2099be27a5dSIan Rogers }
210c924e0ccSIan Rogers | expr '%' expr
211c924e0ccSIan Rogers {
2123f965a7dSIan Rogers 	if (fpclassify($3.val) == FP_ZERO) {
2139be27a5dSIan Rogers 		pr_debug("division by zero\n");
2149be27a5dSIan Rogers 		YYABORT;
215970f7afeSIan Rogers 	} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
216970f7afeSIan Rogers 		assert($1.ids == NULL);
217970f7afeSIan Rogers 		assert($3.ids == NULL);
2183f965a7dSIan Rogers 		$$.val = (long)$1.val % (long)$3.val;
2193f965a7dSIan Rogers 		$$.ids = NULL;
2203f965a7dSIan Rogers 	} else {
221970f7afeSIan Rogers 		/* LHS and/or RHS need computing from event IDs so union. */
2223f965a7dSIan Rogers 		$$ = union_expr($1, $3);
2233f965a7dSIan Rogers 	}
2249be27a5dSIan Rogers }
225c924e0ccSIan Rogers | D_RATIO '(' expr ',' expr ')'
226c924e0ccSIan Rogers {
2273f965a7dSIan Rogers 	if (fpclassify($5.val) == FP_ZERO) {
228970f7afeSIan Rogers 		/*
229970f7afeSIan Rogers 		 * Division by constant zero always yields zero and no events
230970f7afeSIan Rogers 		 * are necessary.
231970f7afeSIan Rogers 		 */
232970f7afeSIan Rogers 		assert($5.ids == NULL);
2333f965a7dSIan Rogers 		$$.val = 0.0;
234970f7afeSIan Rogers 		$$.ids = NULL;
235970f7afeSIan Rogers 		ids__free($3.ids);
236970f7afeSIan Rogers 	} else if (!compute_ids || (is_const($3.val) && is_const($5.val))) {
237970f7afeSIan Rogers 		assert($3.ids == NULL);
238970f7afeSIan Rogers 		assert($5.ids == NULL);
2393f965a7dSIan Rogers 		$$.val = $3.val / $5.val;
240970f7afeSIan Rogers 		$$.ids = NULL;
2413f965a7dSIan Rogers 	} else {
242970f7afeSIan Rogers 		/* LHS and/or RHS need computing from event IDs so union. */
2433f965a7dSIan Rogers 		$$ = union_expr($3, $5);
2447f8fdcbbSIan Rogers 	}
2457f8fdcbbSIan Rogers }
246c924e0ccSIan Rogers | '-' expr %prec NEG
247c924e0ccSIan Rogers {
2483f965a7dSIan Rogers 	$$.val = -$2.val;
2493f965a7dSIan Rogers 	$$.ids = $2.ids;
250c924e0ccSIan Rogers }
251c924e0ccSIan Rogers | '(' if_expr ')'
252c924e0ccSIan Rogers {
253c924e0ccSIan Rogers 	$$ = $2;
254c924e0ccSIan Rogers }
255c924e0ccSIan Rogers | MIN '(' expr ',' expr ')'
256c924e0ccSIan Rogers {
2573f965a7dSIan Rogers 	if (!compute_ids) {
2583f965a7dSIan Rogers 		$$.val = $3.val < $5.val ? $3.val : $5.val;
2593f965a7dSIan Rogers 		$$.ids = NULL;
2603f965a7dSIan Rogers 	} else {
2613f965a7dSIan Rogers 		$$ = union_expr($3, $5);
2623f965a7dSIan Rogers 	}
263c924e0ccSIan Rogers }
264c924e0ccSIan Rogers | MAX '(' expr ',' expr ')'
265c924e0ccSIan Rogers {
2663f965a7dSIan Rogers 	if (!compute_ids) {
2673f965a7dSIan Rogers 		$$.val = $3.val > $5.val ? $3.val : $5.val;
2683f965a7dSIan Rogers 		$$.ids = NULL;
2693f965a7dSIan Rogers 	} else {
2703f965a7dSIan Rogers 		$$ = union_expr($3, $5);
2713f965a7dSIan Rogers 	}
272c924e0ccSIan Rogers }
273c924e0ccSIan Rogers | SMT_ON
274c924e0ccSIan Rogers {
2753f965a7dSIan Rogers 	$$.val = smt_on() > 0 ? 1.0 : 0.0;
2763f965a7dSIan Rogers 	$$.ids = NULL;
277c924e0ccSIan Rogers }
27807516736SAndi Kleen ;
27907516736SAndi Kleen 
28007516736SAndi Kleen %%
281