Loading...
1/* Simple expression parser */
2%{
3#include "util.h"
4#include "util/debug.h"
5#include <stdlib.h> // strtod()
6#define IN_EXPR_Y 1
7#include "expr.h"
8#include "smt.h"
9#include <assert.h>
10#include <string.h>
11
12#define MAXIDLEN 256
13%}
14
15%pure-parser
16%parse-param { double *final_val }
17%parse-param { struct parse_ctx *ctx }
18%parse-param { const char **pp }
19%lex-param { const char **pp }
20
21%union {
22 double num;
23 char id[MAXIDLEN+1];
24}
25
26%token <num> NUMBER
27%token <id> ID
28%token MIN MAX IF ELSE SMT_ON
29%left MIN MAX IF
30%left '|'
31%left '^'
32%left '&'
33%left '-' '+'
34%left '*' '/' '%'
35%left NEG NOT
36%type <num> expr if_expr
37
38%{
39static int expr__lex(YYSTYPE *res, const char **pp);
40
41static void expr__error(double *final_val __maybe_unused,
42 struct parse_ctx *ctx __maybe_unused,
43 const char **pp __maybe_unused,
44 const char *s)
45{
46 pr_debug("%s\n", s);
47}
48
49static int lookup_id(struct parse_ctx *ctx, char *id, double *val)
50{
51 int i;
52
53 for (i = 0; i < ctx->num_ids; i++) {
54 if (!strcasecmp(ctx->ids[i].name, id)) {
55 *val = ctx->ids[i].val;
56 return 0;
57 }
58 }
59 return -1;
60}
61
62%}
63%%
64
65all_expr: if_expr { *final_val = $1; }
66 ;
67
68if_expr:
69 expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
70 | expr
71 ;
72
73expr: NUMBER
74 | ID { if (lookup_id(ctx, $1, &$$) < 0) {
75 pr_debug("%s not found\n", $1);
76 YYABORT;
77 }
78 }
79 | expr '|' expr { $$ = (long)$1 | (long)$3; }
80 | expr '&' expr { $$ = (long)$1 & (long)$3; }
81 | expr '^' expr { $$ = (long)$1 ^ (long)$3; }
82 | expr '+' expr { $$ = $1 + $3; }
83 | expr '-' expr { $$ = $1 - $3; }
84 | expr '*' expr { $$ = $1 * $3; }
85 | expr '/' expr { if ($3 == 0) YYABORT; $$ = $1 / $3; }
86 | expr '%' expr { if ((long)$3 == 0) YYABORT; $$ = (long)$1 % (long)$3; }
87 | '-' expr %prec NEG { $$ = -$2; }
88 | '(' if_expr ')' { $$ = $2; }
89 | MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
90 | MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; }
91 | SMT_ON { $$ = smt_on() > 0; }
92 ;
93
94%%
95
96static int expr__symbol(YYSTYPE *res, const char *p, const char **pp)
97{
98 char *dst = res->id;
99 const char *s = p;
100
101 if (*p == '#')
102 *dst++ = *p++;
103
104 while (isalnum(*p) || *p == '_' || *p == '.' || *p == ':' || *p == '@' || *p == '\\') {
105 if (p - s >= MAXIDLEN)
106 return -1;
107 /*
108 * Allow @ instead of / to be able to specify pmu/event/ without
109 * conflicts with normal division.
110 */
111 if (*p == '@')
112 *dst++ = '/';
113 else if (*p == '\\')
114 *dst++ = *++p;
115 else
116 *dst++ = *p;
117 p++;
118 }
119 *dst = 0;
120 *pp = p;
121 dst = res->id;
122 switch (dst[0]) {
123 case 'm':
124 if (!strcmp(dst, "min"))
125 return MIN;
126 if (!strcmp(dst, "max"))
127 return MAX;
128 break;
129 case 'i':
130 if (!strcmp(dst, "if"))
131 return IF;
132 break;
133 case 'e':
134 if (!strcmp(dst, "else"))
135 return ELSE;
136 break;
137 case '#':
138 if (!strcasecmp(dst, "#smt_on"))
139 return SMT_ON;
140 break;
141 }
142 return ID;
143}
144
145static int expr__lex(YYSTYPE *res, const char **pp)
146{
147 int tok;
148 const char *s;
149 const char *p = *pp;
150
151 while (isspace(*p))
152 p++;
153 s = p;
154 switch (*p++) {
155 case '#':
156 case 'a' ... 'z':
157 case 'A' ... 'Z':
158 return expr__symbol(res, p - 1, pp);
159 case '0' ... '9': case '.':
160 res->num = strtod(s, (char **)&p);
161 tok = NUMBER;
162 break;
163 default:
164 tok = *s;
165 break;
166 }
167 *pp = p;
168 return tok;
169}
170
171/* Caller must make sure id is allocated */
172void expr__add_id(struct parse_ctx *ctx, const char *name, double val)
173{
174 int idx;
175 assert(ctx->num_ids < MAX_PARSE_ID);
176 idx = ctx->num_ids++;
177 ctx->ids[idx].name = name;
178 ctx->ids[idx].val = val;
179}
180
181void expr__ctx_init(struct parse_ctx *ctx)
182{
183 ctx->num_ids = 0;
184}
185
186static bool already_seen(const char *val, const char *one, const char **other,
187 int num_other)
188{
189 int i;
190
191 if (one && !strcasecmp(one, val))
192 return true;
193 for (i = 0; i < num_other; i++)
194 if (!strcasecmp(other[i], val))
195 return true;
196 return false;
197}
198
199int expr__find_other(const char *p, const char *one, const char ***other,
200 int *num_otherp)
201{
202 const char *orig = p;
203 int err = -1;
204 int num_other;
205
206 *other = malloc((EXPR_MAX_OTHER + 1) * sizeof(char *));
207 if (!*other)
208 return -1;
209
210 num_other = 0;
211 for (;;) {
212 YYSTYPE val;
213 int tok = expr__lex(&val, &p);
214 if (tok == 0) {
215 err = 0;
216 break;
217 }
218 if (tok == ID && !already_seen(val.id, one, *other, num_other)) {
219 if (num_other >= EXPR_MAX_OTHER - 1) {
220 pr_debug("Too many extra events in %s\n", orig);
221 break;
222 }
223 (*other)[num_other] = strdup(val.id);
224 if (!(*other)[num_other])
225 return -1;
226 num_other++;
227 }
228 }
229 (*other)[num_other] = NULL;
230 *num_otherp = num_other;
231 if (err) {
232 *num_otherp = 0;
233 free(*other);
234 *other = NULL;
235 }
236 return err;
237}
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%{
56static 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? */
72static bool is_const(double val)
73{
74 return isfinite(val);
75}
76
77static 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
86static 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_LONG_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 RESULT.val = (long)LHS.val OP (long)RHS.val; \
131 RESULT.ids = NULL; \
132 } else { \
133 RESULT = union_expr(LHS, RHS); \
134 }
135
136#define BINARY_OP(RESULT, OP, LHS, RHS) \
137 if (!compute_ids || (is_const(LHS.val) && is_const(RHS.val))) { \
138 assert(LHS.ids == NULL); \
139 assert(RHS.ids == NULL); \
140 RESULT.val = LHS.val OP RHS.val; \
141 RESULT.ids = NULL; \
142 } else { \
143 RESULT = union_expr(LHS, RHS); \
144 }
145
146%}
147%%
148
149start: if_expr
150{
151 if (compute_ids)
152 ctx->ids = ids__union($1.ids, ctx->ids);
153
154 if (final_val)
155 *final_val = $1.val;
156}
157;
158
159if_expr: expr IF expr ELSE if_expr
160{
161 if (fpclassify($3.val) == FP_ZERO) {
162 /*
163 * The IF expression evaluated to 0 so treat as false, take the
164 * ELSE and discard everything else.
165 */
166 $$.val = $5.val;
167 $$.ids = $5.ids;
168 ids__free($1.ids);
169 ids__free($3.ids);
170 } else if (!compute_ids || is_const($3.val)) {
171 /*
172 * If ids aren't computed then treat the expression as true. If
173 * ids are being computed and the IF expr is a non-zero
174 * constant, then also evaluate the true case.
175 */
176 $$.val = $1.val;
177 $$.ids = $1.ids;
178 ids__free($3.ids);
179 ids__free($5.ids);
180 } else if ($1.val == $5.val) {
181 /*
182 * LHS == RHS, so both are an identical constant. No need to
183 * evaluate any events.
184 */
185 $$.val = $1.val;
186 $$.ids = NULL;
187 ids__free($1.ids);
188 ids__free($3.ids);
189 ids__free($5.ids);
190 } else {
191 /*
192 * Value is either the LHS or RHS and we need the IF expression
193 * to compute it.
194 */
195 $$ = union_expr($1, union_expr($3, $5));
196 }
197}
198| expr
199;
200
201expr: NUMBER
202{
203 $$.val = $1;
204 $$.ids = NULL;
205}
206| ID { $$ = handle_id(ctx, $1, compute_ids, /*source_count=*/false); }
207| SOURCE_COUNT '(' ID ')' { $$ = handle_id(ctx, $3, compute_ids, /*source_count=*/true); }
208| expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); }
209| expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); }
210| expr '^' expr { BINARY_LONG_OP($$, ^, $1, $3); }
211| expr '<' expr { BINARY_OP($$, <, $1, $3); }
212| expr '>' expr { BINARY_OP($$, >, $1, $3); }
213| expr '+' expr { BINARY_OP($$, +, $1, $3); }
214| expr '-' expr { BINARY_OP($$, -, $1, $3); }
215| expr '*' expr { BINARY_OP($$, *, $1, $3); }
216| expr '/' expr
217{
218 if (fpclassify($3.val) == FP_ZERO) {
219 pr_debug("division by zero\n");
220 YYABORT;
221 } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
222 assert($1.ids == NULL);
223 assert($3.ids == NULL);
224 $$.val = $1.val / $3.val;
225 $$.ids = NULL;
226 } else {
227 /* LHS and/or RHS need computing from event IDs so union. */
228 $$ = union_expr($1, $3);
229 }
230}
231| expr '%' expr
232{
233 if (fpclassify($3.val) == FP_ZERO) {
234 pr_debug("division by zero\n");
235 YYABORT;
236 } else if (!compute_ids || (is_const($1.val) && is_const($3.val))) {
237 assert($1.ids == NULL);
238 assert($3.ids == NULL);
239 $$.val = (long)$1.val % (long)$3.val;
240 $$.ids = NULL;
241 } else {
242 /* LHS and/or RHS need computing from event IDs so union. */
243 $$ = union_expr($1, $3);
244 }
245}
246| D_RATIO '(' expr ',' expr ')'
247{
248 if (fpclassify($5.val) == FP_ZERO) {
249 /*
250 * Division by constant zero always yields zero and no events
251 * are necessary.
252 */
253 assert($5.ids == NULL);
254 $$.val = 0.0;
255 $$.ids = NULL;
256 ids__free($3.ids);
257 } else if (!compute_ids || (is_const($3.val) && is_const($5.val))) {
258 assert($3.ids == NULL);
259 assert($5.ids == NULL);
260 $$.val = $3.val / $5.val;
261 $$.ids = NULL;
262 } else {
263 /* LHS and/or RHS need computing from event IDs so union. */
264 $$ = union_expr($3, $5);
265 }
266}
267| '-' expr %prec NEG
268{
269 $$.val = -$2.val;
270 $$.ids = $2.ids;
271}
272| '(' if_expr ')'
273{
274 $$ = $2;
275}
276| MIN '(' expr ',' expr ')'
277{
278 if (!compute_ids) {
279 $$.val = $3.val < $5.val ? $3.val : $5.val;
280 $$.ids = NULL;
281 } else {
282 $$ = union_expr($3, $5);
283 }
284}
285| MAX '(' expr ',' expr ')'
286{
287 if (!compute_ids) {
288 $$.val = $3.val > $5.val ? $3.val : $5.val;
289 $$.ids = NULL;
290 } else {
291 $$ = union_expr($3, $5);
292 }
293}
294| LITERAL
295{
296 $$.val = $1;
297 $$.ids = NULL;
298}
299;
300
301%%