1 /* Simple expression parser */ 1 /* Simple expression parser */ 2 %{ 2 %{ 3 #ifndef NDEBUG !! 3 #include "util.h" 4 #define YYDEBUG 1 << 5 #endif << 6 #include <assert.h> << 7 #include <math.h> << 8 #include <stdlib.h> << 9 #include "util/debug.h" 4 #include "util/debug.h" >> 5 #include <stdlib.h> // strtod() 10 #define IN_EXPR_Y 1 6 #define IN_EXPR_Y 1 11 #include "expr.h" 7 #include "expr.h" 12 #include "expr-bison.h" !! 8 #include "smt.h" 13 int expr_lex(YYSTYPE * yylval_param , void *yy !! 9 #include <assert.h> >> 10 #include <string.h> >> 11 >> 12 #define MAXIDLEN 256 14 %} 13 %} 15 14 16 %define api.pure full 15 %define api.pure full 17 16 18 %parse-param { double *final_val } 17 %parse-param { double *final_val } 19 %parse-param { struct expr_parse_ctx *ctx } !! 18 %parse-param { struct parse_ctx *ctx } 20 %parse-param { bool compute_ids } !! 19 %parse-param { const char **pp } 21 %parse-param {void *scanner} !! 20 %lex-param { const char **pp } 22 %lex-param {void* scanner} << 23 21 24 %union { 22 %union { 25 double num; !! 23 double num; 26 char *str; !! 24 char id[MAXIDLEN+1]; 27 struct ids { << 28 /* << 29 * When creating ids, holds th << 30 * implies the set is empty. << 31 */ << 32 struct hashmap *ids; << 33 /* << 34 * The metric value. When not << 35 * read from a counter, a cons << 36 * creating ids the value is e << 37 * used as the special BOTTOM << 38 * values" case. << 39 */ << 40 double val; << 41 } ids; << 42 } 25 } 43 26 44 %token ID NUMBER MIN MAX IF ELSE LITERAL D_RAT !! 27 %token <num> NUMBER >> 28 %token <id> ID >> 29 %token MIN MAX IF ELSE SMT_ON 45 %left MIN MAX IF 30 %left MIN MAX IF 46 %left '|' 31 %left '|' 47 %left '^' 32 %left '^' 48 %left '&' 33 %left '&' 49 %left '<' '>' << 50 %left '-' '+' 34 %left '-' '+' 51 %left '*' '/' '%' 35 %left '*' '/' '%' 52 %left NEG NOT 36 %left NEG NOT 53 %type <num> NUMBER LITERAL !! 37 %type <num> expr if_expr 54 %type <str> ID << 55 %destructor { free ($$); } <str> << 56 %type <ids> expr if_expr << 57 %destructor { ids__free($$.ids); } <ids> << 58 38 59 %{ 39 %{ 60 static void expr_error(double *final_val __may !! 40 static int expr__lex(YYSTYPE *res, const char **pp); 61 struct expr_parse_ctx * !! 41 62 bool compute_ids __mayb !! 42 static void expr__error(double *final_val __maybe_unused, 63 void *scanner __maybe_u !! 43 struct parse_ctx *ctx __maybe_unused, >> 44 const char **pp __maybe_unused, 64 const char *s) 45 const char *s) 65 { 46 { 66 pr_debug("%s\n", s); 47 pr_debug("%s\n", s); 67 } 48 } 68 49 69 /* !! 50 static int lookup_id(struct parse_ctx *ctx, char *id, double *val) 70 * During compute ids, the special "bottom" va << 71 * of all values. NAN is selected as it isn't << 72 */ << 73 #define BOTTOM NAN << 74 << 75 /* During computing ids, does val represent a << 76 static bool is_const(double val) << 77 { << 78 return isfinite(val); << 79 } << 80 << 81 static struct ids union_expr(struct ids ids1, << 82 { << 83 struct ids result = { << 84 .val = BOTTOM, << 85 .ids = ids__union(ids1.ids, id << 86 }; << 87 return result; << 88 } << 89 << 90 static struct ids handle_id(struct expr_parse_ << 91 bool compute_ids, << 92 { 51 { 93 struct ids result; !! 52 int i; 94 53 95 if (!compute_ids) { !! 54 for (i = 0; i < ctx->num_ids; i++) { 96 /* !! 55 if (!strcasecmp(ctx->ids[i].name, id)) { 97 * Compute the event's value f !! 56 *val = ctx->ids[i].val; 98 * it isn't used to compute th !! 57 return 0; 99 */ << 100 struct expr_id_data *data; << 101 << 102 result.val = NAN; << 103 if (expr__resolve_id(ctx, id, << 104 result.val = source_co << 105 ? expr_id_data << 106 : expr_id_data << 107 } << 108 result.ids = NULL; << 109 free(id); << 110 } else { << 111 /* << 112 * Set the value to BOTTOM to << 113 * when the event is computed. << 114 */ << 115 result.val = BOTTOM; << 116 result.ids = ids__new(); << 117 if (!result.ids || ids__insert << 118 pr_err("Error creating << 119 free(id); << 120 } 58 } 121 } 59 } 122 return result; !! 60 return -1; 123 } 61 } 124 62 125 /* << 126 * If we're not computing ids or $1 and $3 are << 127 * constant value using OP. Its invariant that << 128 * ids for non-constants union the set of IDs << 129 */ << 130 #define BINARY_OP(RESULT, OP, LHS, RHS) << 131 if (!compute_ids || (is_const(LHS.val) << 132 assert(LHS.ids == NULL); << 133 assert(RHS.ids == NULL); << 134 if (isnan(LHS.val) || isnan(RH << 135 RESULT.val = NAN; << 136 } else { << 137 RESULT.val = LHS.val O << 138 } << 139 RESULT.ids = NULL; << 140 } else { << 141 RESULT = union_expr(LHS, RHS); << 142 } << 143 << 144 %} 63 %} 145 %% 64 %% 146 65 147 start: if_expr !! 66 all_expr: if_expr { *final_val = $1; } 148 { !! 67 ; 149 if (compute_ids) << 150 ctx->ids = ids__union($1.ids, << 151 68 152 if (final_val) !! 69 if_expr: 153 *final_val = $1.val; !! 70 expr IF expr ELSE expr { $$ = $3 ? $1 : $5; } 154 } !! 71 | expr 155 ; !! 72 ; >> 73 >> 74 expr: NUMBER >> 75 | ID { if (lookup_id(ctx, $1, &$$) < 0) { >> 76 pr_debug("%s not found\n", $1); >> 77 YYABORT; >> 78 } >> 79 } >> 80 | expr '|' expr { $$ = (long)$1 | (long)$3; } >> 81 | expr '&' expr { $$ = (long)$1 & (long)$3; } >> 82 | expr '^' expr { $$ = (long)$1 ^ (long)$3; } >> 83 | expr '+' expr { $$ = $1 + $3; } >> 84 | expr '-' expr { $$ = $1 - $3; } >> 85 | expr '*' expr { $$ = $1 * $3; } >> 86 | expr '/' expr { if ($3 == 0) YYABORT; $$ = $1 / $3; } >> 87 | expr '%' expr { if ((long)$3 == 0) YYABORT; $$ = (long)$1 % (long)$3; } >> 88 | '-' expr %prec NEG { $$ = -$2; } >> 89 | '(' if_expr ')' { $$ = $2; } >> 90 | MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; } >> 91 | MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; } >> 92 | SMT_ON { $$ = smt_on() > 0; } >> 93 ; 156 94 157 if_expr: expr IF expr ELSE if_expr !! 95 %% 158 { << 159 if (fpclassify($3.val) == FP_ZERO) { << 160 /* << 161 * The IF expression evaluated << 162 * ELSE and discard everything << 163 */ << 164 $$.val = $5.val; << 165 $$.ids = $5.ids; << 166 ids__free($1.ids); << 167 ids__free($3.ids); << 168 } else if (!compute_ids || is_const($3 << 169 /* << 170 * If ids aren't computed then << 171 * ids are being computed and << 172 * constant, then also evaluat << 173 */ << 174 $$.val = $1.val; << 175 $$.ids = $1.ids; << 176 ids__free($3.ids); << 177 ids__free($5.ids); << 178 } else if ($1.val == $5.val) { << 179 /* << 180 * LHS == RHS, so both are an << 181 * evaluate any events. << 182 */ << 183 $$.val = $1.val; << 184 $$.ids = NULL; << 185 ids__free($1.ids); << 186 ids__free($3.ids); << 187 ids__free($5.ids); << 188 } else { << 189 /* << 190 * Value is either the LHS or << 191 * to compute it. << 192 */ << 193 $$ = union_expr($1, union_expr << 194 } << 195 } << 196 | expr << 197 ; << 198 96 199 expr: NUMBER !! 97 static int expr__symbol(YYSTYPE *res, const char *p, const char **pp) 200 { << 201 $$.val = $1; << 202 $$.ids = NULL; << 203 } << 204 | ID { $$ = handle_ << 205 | SOURCE_COUNT '(' ID ')' { $$ = handle_ << 206 | HAS_EVENT '(' ID ')' << 207 { << 208 $$.val = expr__has_event(ctx, compute_ << 209 $$.ids = NULL; << 210 free($3); << 211 } << 212 | STRCMP_CPUID_STR '(' ID ')' << 213 { << 214 $$.val = expr__strcmp_cpuid_str(ctx, c << 215 $$.ids = NULL; << 216 free($3); << 217 } << 218 | expr '|' expr << 219 { << 220 if (is_const($1.val) && is_const($3.va << 221 assert($1.ids == NULL); << 222 assert($3.ids == NULL); << 223 $$.ids = NULL; << 224 $$.val = (fpclassify($1.val) = << 225 } else if (is_const($1.val)) { << 226 assert($1.ids == NULL); << 227 if (fpclassify($1.val) == FP_Z << 228 $$ = $3; << 229 } else { << 230 $$.val = 1; << 231 $$.ids = NULL; << 232 ids__free($3.ids); << 233 } << 234 } else if (is_const($3.val)) { << 235 assert($3.ids == NULL); << 236 if (fpclassify($3.val) == FP_Z << 237 $$ = $1; << 238 } else { << 239 $$.val = 1; << 240 $$.ids = NULL; << 241 ids__free($1.ids); << 242 } << 243 } else { << 244 $$ = union_expr($1, $3); << 245 } << 246 } << 247 | expr '&' expr << 248 { << 249 if (is_const($1.val) && is_const($3.va << 250 assert($1.ids == NULL); << 251 assert($3.ids == NULL); << 252 $$.val = (fpclassify($1.val) ! << 253 $$.ids = NULL; << 254 } else if (is_const($1.val)) { << 255 assert($1.ids == NULL); << 256 if (fpclassify($1.val) != FP_Z << 257 $$ = $3; << 258 } else { << 259 $$.val = 0; << 260 $$.ids = NULL; << 261 ids__free($3.ids); << 262 } << 263 } else if (is_const($3.val)) { << 264 assert($3.ids == NULL); << 265 if (fpclassify($3.val) != FP_Z << 266 $$ = $1; << 267 } else { << 268 $$.val = 0; << 269 $$.ids = NULL; << 270 ids__free($1.ids); << 271 } << 272 } else { << 273 $$ = union_expr($1, $3); << 274 } << 275 } << 276 | expr '^' expr << 277 { << 278 if (is_const($1.val) && is_const($3.va << 279 assert($1.ids == NULL); << 280 assert($3.ids == NULL); << 281 $$.val = (fpclassify($1.val) = << 282 $$.ids = NULL; << 283 } else { << 284 $$ = union_expr($1, $3); << 285 } << 286 } << 287 | expr '<' expr { BINARY_OP($$, <, $1, $3); } << 288 | expr '>' expr { BINARY_OP($$, >, $1, $3); } << 289 | expr '+' expr { BINARY_OP($$, +, $1, $3); } << 290 | expr '-' expr { BINARY_OP($$, -, $1, $3); } << 291 | expr '*' expr { BINARY_OP($$, *, $1, $3); } << 292 | expr '/' expr << 293 { << 294 if (fpclassify($3.val) == FP_ZERO) { << 295 pr_debug("division by zero\n") << 296 assert($3.ids == NULL); << 297 if (compute_ids) << 298 ids__free($1.ids); << 299 $$.val = NAN; << 300 $$.ids = NULL; << 301 } else if (!compute_ids || (is_const($ << 302 assert($1.ids == NULL); << 303 assert($3.ids == NULL); << 304 $$.val = $1.val / $3.val; << 305 $$.ids = NULL; << 306 } else { << 307 /* LHS and/or RHS need computi << 308 $$ = union_expr($1, $3); << 309 } << 310 } << 311 | expr '%' expr << 312 { << 313 if (fpclassify($3.val) == FP_ZERO) { << 314 pr_debug("division by zero\n") << 315 YYABORT; << 316 } else if (!compute_ids || (is_const($ << 317 assert($1.ids == NULL); << 318 assert($3.ids == NULL); << 319 $$.val = (long)$1.val % (long) << 320 $$.ids = NULL; << 321 } else { << 322 /* LHS and/or RHS need computi << 323 $$ = union_expr($1, $3); << 324 } << 325 } << 326 | D_RATIO '(' expr ',' expr ')' << 327 { << 328 if (fpclassify($5.val) == FP_ZERO) { << 329 /* << 330 * Division by constant zero a << 331 * are necessary. << 332 */ << 333 assert($5.ids == NULL); << 334 $$.val = 0.0; << 335 $$.ids = NULL; << 336 ids__free($3.ids); << 337 } else if (!compute_ids || (is_const($ << 338 assert($3.ids == NULL); << 339 assert($5.ids == NULL); << 340 $$.val = $3.val / $5.val; << 341 $$.ids = NULL; << 342 } else { << 343 /* LHS and/or RHS need computi << 344 $$ = union_expr($3, $5); << 345 } << 346 } << 347 | '-' expr %prec NEG << 348 { << 349 $$.val = -$2.val; << 350 $$.ids = $2.ids; << 351 } << 352 | '(' if_expr ')' << 353 { << 354 $$ = $2; << 355 } << 356 | MIN '(' expr ',' expr ')' << 357 { 98 { 358 if (!compute_ids) { !! 99 char *dst = res->id; 359 $$.val = $3.val < $5.val ? $3. !! 100 const char *s = p; 360 $$.ids = NULL; !! 101 361 } else { !! 102 if (*p == '#') 362 $$ = union_expr($3, $5); !! 103 *dst++ = *p++; 363 } !! 104 364 } !! 105 while (isalnum(*p) || *p == '_' || *p == '.' || *p == ':' || *p == '@' || *p == '\\') { 365 | MAX '(' expr ',' expr ')' !! 106 if (p - s >= MAXIDLEN) 366 { !! 107 return -1; 367 if (!compute_ids) { !! 108 /* 368 $$.val = $3.val > $5.val ? $3. !! 109 * Allow @ instead of / to be able to specify pmu/event/ without 369 $$.ids = NULL; !! 110 * conflicts with normal division. 370 } else { !! 111 */ 371 $$ = union_expr($3, $5); !! 112 if (*p == '@') >> 113 *dst++ = '/'; >> 114 else if (*p == '\\') >> 115 *dst++ = *++p; >> 116 else >> 117 *dst++ = *p; >> 118 p++; >> 119 } >> 120 *dst = 0; >> 121 *pp = p; >> 122 dst = res->id; >> 123 switch (dst[0]) { >> 124 case 'm': >> 125 if (!strcmp(dst, "min")) >> 126 return MIN; >> 127 if (!strcmp(dst, "max")) >> 128 return MAX; >> 129 break; >> 130 case 'i': >> 131 if (!strcmp(dst, "if")) >> 132 return IF; >> 133 break; >> 134 case 'e': >> 135 if (!strcmp(dst, "else")) >> 136 return ELSE; >> 137 break; >> 138 case '#': >> 139 if (!strcasecmp(dst, "#smt_on")) >> 140 return SMT_ON; >> 141 break; >> 142 } >> 143 return ID; >> 144 } >> 145 >> 146 static int expr__lex(YYSTYPE *res, const char **pp) >> 147 { >> 148 int tok; >> 149 const char *s; >> 150 const char *p = *pp; >> 151 >> 152 while (isspace(*p)) >> 153 p++; >> 154 s = p; >> 155 switch (*p++) { >> 156 case '#': >> 157 case 'a' ... 'z': >> 158 case 'A' ... 'Z': >> 159 return expr__symbol(res, p - 1, pp); >> 160 case '0' ... '9': case '.': >> 161 res->num = strtod(s, (char **)&p); >> 162 tok = NUMBER; >> 163 break; >> 164 default: >> 165 tok = *s; >> 166 break; >> 167 } >> 168 *pp = p; >> 169 return tok; >> 170 } >> 171 >> 172 /* Caller must make sure id is allocated */ >> 173 void expr__add_id(struct parse_ctx *ctx, const char *name, double val) >> 174 { >> 175 int idx; >> 176 assert(ctx->num_ids < MAX_PARSE_ID); >> 177 idx = ctx->num_ids++; >> 178 ctx->ids[idx].name = name; >> 179 ctx->ids[idx].val = val; >> 180 } >> 181 >> 182 void expr__ctx_init(struct parse_ctx *ctx) >> 183 { >> 184 ctx->num_ids = 0; >> 185 } >> 186 >> 187 static bool already_seen(const char *val, const char *one, const char **other, >> 188 int num_other) >> 189 { >> 190 int i; >> 191 >> 192 if (one && !strcasecmp(one, val)) >> 193 return true; >> 194 for (i = 0; i < num_other; i++) >> 195 if (!strcasecmp(other[i], val)) >> 196 return true; >> 197 return false; >> 198 } >> 199 >> 200 int expr__find_other(const char *p, const char *one, const char ***other, >> 201 int *num_otherp) >> 202 { >> 203 const char *orig = p; >> 204 int err = -1; >> 205 int num_other; >> 206 >> 207 *other = malloc((EXPR_MAX_OTHER + 1) * sizeof(char *)); >> 208 if (!*other) >> 209 return -1; >> 210 >> 211 num_other = 0; >> 212 for (;;) { >> 213 YYSTYPE val; >> 214 int tok = expr__lex(&val, &p); >> 215 if (tok == 0) { >> 216 err = 0; >> 217 break; >> 218 } >> 219 if (tok == ID && !already_seen(val.id, one, *other, num_other)) { >> 220 if (num_other >= EXPR_MAX_OTHER - 1) { >> 221 pr_debug("Too many extra events in %s\n", orig); >> 222 break; >> 223 } >> 224 (*other)[num_other] = strdup(val.id); >> 225 if (!(*other)[num_other]) >> 226 return -1; >> 227 num_other++; >> 228 } >> 229 } >> 230 (*other)[num_other] = NULL; >> 231 *num_otherp = num_other; >> 232 if (err) { >> 233 *num_otherp = 0; >> 234 free(*other); >> 235 *other = NULL; 372 } 236 } >> 237 return err; 373 } 238 } 374 | LITERAL << 375 { << 376 $$.val = $1; << 377 $$.ids = NULL; << 378 } << 379 ; << 380 << 381 %% <<
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.