summaryrefslogtreecommitdiff
path: root/tools/perf/util/expr.y
blob: a30b825adb7ba6995f871346127784be69d3574f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Simple expression parser */
%{
#define YYDEBUG 1
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include "util/debug.h"
#define IN_EXPR_Y 1
#include "expr.h"
%}

%define api.pure full

%parse-param { double *final_val }
%parse-param { struct expr_parse_ctx *ctx }
%parse-param { bool compute_ids }
%parse-param {void *scanner}
%lex-param {void* scanner}

%union {
	double	 num;
	char	*str;
	struct ids {
		/*
		 * When creating ids, holds the working set of event ids. NULL
		 * implies the set is empty.
		 */
		struct hashmap *ids;
		/*
		 * The metric value. When not creating ids this is the value
		 * read from a counter, a constant or some computed value. When
		 * creating ids the value is either a constant or BOTTOM. NAN is
		 * used as the special BOTTOM value, representing a "set of all
		 * values" case.
		 */
		double val;
	} ids;
}

%token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT EXPR_ERROR
%left MIN MAX IF
%left '|'
%left '^'
%left '&'
%left '<' '>'
%left '-' '+'
%left '*' '/' '%'
%left NEG NOT
%type <num> NUMBER LITERAL
%type <str> ID
%destructor { free ($$); } <str>
%type <ids> expr if_expr
%destructor { ids__free($$.ids); } <ids>

%{
static void expr_error(double *final_val __maybe_unused,
		       struct expr_parse_ctx *ctx __maybe_unused,
		       bool compute_ids __maybe_unused,
		       void *scanner,
		       const char *s)
{
	pr_debug("%s\n", s);
}

/*
 * During compute ids, the special "bottom" value uses NAN to represent the set
 * of all values. NAN is selected as it isn't a useful constant value.
 */
#define BOTTOM NAN

/* During computing ids, does val represent a constant (non-BOTTOM) value? */
static bool is_const(double val)
{
	return isfinite(val);
}

static struct ids union_expr(struct ids ids1, struct ids ids2)
{
	struct ids result = {
		.val = BOTTOM,
		.ids = ids__union(ids1.ids, ids2.ids),
	};
	return result;
}

static struct ids handle_id(struct expr_parse_ctx *ctx, char *id,
			    bool compute_ids, bool source_count)
{
	struct ids result;

	if (!compute_ids) {
		/*
		 * Compute the event's value from ID. If the ID isn't known then
		 * it isn't used to compute the formula so set to NAN.
		 */
		struct expr_id_data *data;

		result.val = NAN;
		if (expr__resolve_id(ctx, id, &data) == 0) {
			result.val = source_count
				? expr_id_data__source_count(data)
				: expr_id_data__value(data);
		}
		result.ids = NULL;
		free(id);
	} else {
		/*
		 * Set the value to BOTTOM to show that any value is possible
		 * when the event is computed. Create a set of just the ID.
		 */
		result.val = BOTTOM;
		result.ids = ids__new();
		if (!result.ids || ids__insert(result.ids, id)) {
			pr_err("Error creating IDs for '%s'", id);
			free(id);
		}
	}
	return result;
}

/*
 * If we're not computing ids or $1 and $3 are constants, compute the new
 * constant value using OP. Its invariant that there are no ids.  If computing
 * ids for non-constants union the set of IDs that must be computed.
 */
#define BINARY_LONG_OP(RESULT, OP, LHS, RHS)				\
	if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
		assert(LHS.ids == NULL);				\
		assert(RHS.ids == NULL);				\
		RESULT.val = (long)LHS.val OP (long)RHS.val;		\
		RESULT.ids = NULL;					\
	} else {							\
	        RESULT = union_expr(LHS, RHS);				\
	}

#define BINARY_OP(RESULT, OP, LHS, RHS)					\
	if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
		assert(LHS.ids == NULL);				\
		assert(RHS.ids == NULL);				\
		RESULT.val = LHS.val OP RHS.val;			\
		RESULT.ids = NULL;					\
	} else {							\
	        RESULT = union_expr(LHS, RHS);				\
	}

%}
%%

start: if_expr
{
	if (compute_ids)
		ctx->ids = ids__union($1.ids, ctx->ids);

	if (final_val)
		*final_val = $1.val;
}
;

if_expr: expr IF expr ELSE expr
{
	if (fpclassify($3.val) == FP_ZERO) {
		/*
		 * The IF expression evaluated to 0 so treat as false, take the
		 * ELSE and discard everything else.
		 */
		$$.val = $5.val;
		$$.ids = $5.ids;
		ids__free($1.ids);
		ids__free($3.ids);
	} else if (!compute_ids || is_const($3.val)) {
		/*
		 * If ids aren't computed then treat the expression as true. If
		 * ids are being computed and the IF expr is a non-zero
		 * constant, then also evaluate the true case.
		 */
		$$.val = $1.val;
		$$.ids = $1.ids;
		ids__free($3.ids);
		ids__free($5.ids);
	} else if ($1.val == $5.val) {
		/*
		 * LHS == RHS, so both are an identical constant. No need to
		 * evaluate any events.
		 */
		$$.val = $1.val;
		$$.ids = NULL;
		ids__free($1.ids);
		ids__free($3.ids);
		ids__free($5.ids);
	} else {
		/*
		 * Value is either the LHS or RHS and we need the IF expression
		 * to compute it.
		 */
		$$ = union_expr($1, union_expr($3, $5));
	}
}
| expr
;

expr: NUMBER
{
	$$.val = $1;
	$$.ids = NULL;
}
| ID				{ $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
| SOURCE_COUNT '(' ID ')'	{ $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
| expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); }
| expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); }
| expr '^' expr { BINARY_LONG_OP($$, ^, $1, $3); }
| expr '<' expr { BINARY_OP($$, <, $1, $3); }
| expr '>' expr { BINARY_OP($$, >, $1, $3); }
| expr '+' expr { BINARY_OP($$, +, $1, $3); }
| expr '-' expr { BINARY_OP($$, -, $1, $3); }
| expr '*' expr { BINARY_OP($$, *, $1, $3); }
| expr '/' expr
{
	if (fpclassify($3.val) == FP_ZERO) {
		pr_debug("division by zero\n");
		YYABORT;
	} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
		assert($1.ids == NULL);
		assert($3.ids == NULL);
		$$.val = $1.val / $3.val;
		$$.ids = NULL;
	} else {
		/* LHS and/or RHS need computing from event IDs so union. */
		$$ = union_expr($1, $3);
	}
}
| expr '%' expr
{
	if (fpclassify($3.val) == FP_ZERO) {
		pr_debug("division by zero\n");
		YYABORT;
	} else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
		assert($1.ids == NULL);
		assert($3.ids == NULL);
		$$.val = (long)$1.val % (long)$3.val;
		$$.ids = NULL;
	} else {
		/* LHS and/or RHS need computing from event IDs so union. */
		$$ = union_expr($1, $3);
	}
}
| D_RATIO '(' expr ',' expr ')'
{
	if (fpclassify($5.val) == FP_ZERO) {
		/*
		 * Division by constant zero always yields zero and no events
		 * are necessary.
		 */
		assert($5.ids == NULL);
		$$.val = 0.0;
		$$.ids = NULL;
		ids__free($3.ids);
	} else if (!compute_ids || (is_const($3.val) && is_const($5.val))) {
		assert($3.ids == NULL);
		assert($5.ids == NULL);
		$$.val = $3.val / $5.val;
		$$.ids = NULL;
	} else {
		/* LHS and/or RHS need computing from event IDs so union. */
		$$ = union_expr($3, $5);
	}
}
| '-' expr %prec NEG
{
	$$.val = -$2.val;
	$$.ids = $2.ids;
}
| '(' if_expr ')'
{
	$$ = $2;
}
| MIN '(' expr ',' expr ')'
{
	if (!compute_ids) {
		$$.val = $3.val < $5.val ? $3.val : $5.val;
		$$.ids = NULL;
	} else {
		$$ = union_expr($3, $5);
	}
}
| MAX '(' expr ',' expr ')'
{
	if (!compute_ids) {
		$$.val = $3.val > $5.val ? $3.val : $5.val;
		$$.ids = NULL;
	} else {
		$$ = union_expr($3, $5);
	}
}
| LITERAL
{
	$$.val = $1;
	$$.ids = NULL;
}
;

%%