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" 10 #define IN_EXPR_Y 1 5 #define IN_EXPR_Y 1 11 #include "expr.h" 6 #include "expr.h" 12 #include "expr-bison.h" !! 7 #include <string.h> 13 int expr_lex(YYSTYPE * yylval_param , void *yy << 14 %} << 15 8 16 %define api.pure full !! 9 #define MAXIDLEN 256 >> 10 %} 17 11 >> 12 %pure-parser 18 %parse-param { double *final_val } 13 %parse-param { double *final_val } 19 %parse-param { struct expr_parse_ctx *ctx } !! 14 %parse-param { struct parse_ctx *ctx } 20 %parse-param { bool compute_ids } !! 15 %parse-param { const char **pp } 21 %parse-param {void *scanner} !! 16 %lex-param { const char **pp } 22 %lex-param {void* scanner} << 23 17 24 %union { 18 %union { 25 double num; !! 19 double num; 26 char *str; !! 20 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 } 21 } 43 22 44 %token ID NUMBER MIN MAX IF ELSE LITERAL D_RAT !! 23 %token <num> NUMBER 45 %left MIN MAX IF !! 24 %token <id> ID 46 %left '|' 25 %left '|' 47 %left '^' 26 %left '^' 48 %left '&' 27 %left '&' 49 %left '<' '>' << 50 %left '-' '+' 28 %left '-' '+' 51 %left '*' '/' '%' 29 %left '*' '/' '%' 52 %left NEG NOT 30 %left NEG NOT 53 %type <num> NUMBER LITERAL !! 31 %type <num> expr 54 %type <str> ID << 55 %destructor { free ($$); } <str> << 56 %type <ids> expr if_expr << 57 %destructor { ids__free($$.ids); } <ids> << 58 32 59 %{ 33 %{ 60 static void expr_error(double *final_val __may !! 34 static int expr__lex(YYSTYPE *res, const char **pp); 61 struct expr_parse_ctx * !! 35 62 bool compute_ids __mayb !! 36 static void expr__error(double *final_val __maybe_unused, 63 void *scanner __maybe_u !! 37 struct parse_ctx *ctx __maybe_unused, >> 38 const char **pp __maybe_unused, 64 const char *s) 39 const char *s) 65 { 40 { 66 pr_debug("%s\n", s); 41 pr_debug("%s\n", s); 67 } 42 } 68 43 69 /* !! 44 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 { 45 { 83 struct ids result = { !! 46 int i; 84 .val = BOTTOM, << 85 .ids = ids__union(ids1.ids, id << 86 }; << 87 return result; << 88 } << 89 47 90 static struct ids handle_id(struct expr_parse_ !! 48 for (i = 0; i < ctx->num_ids; i++) { 91 bool compute_ids, !! 49 if (!strcasecmp(ctx->ids[i].name, id)) { 92 { !! 50 *val = ctx->ids[i].val; 93 struct ids result; !! 51 return 0; 94 << 95 if (!compute_ids) { << 96 /* << 97 * Compute the event's value f << 98 * it isn't used to compute th << 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 } 52 } 121 } 53 } 122 return result; !! 54 return -1; 123 } 55 } 124 56 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 %} 57 %} 145 %% 58 %% 146 59 147 start: if_expr !! 60 all_expr: expr { *final_val = $1; } 148 { !! 61 ; 149 if (compute_ids) << 150 ctx->ids = ids__union($1.ids, << 151 62 152 if (final_val) !! 63 expr: NUMBER 153 *final_val = $1.val; !! 64 | ID { if (lookup_id(ctx, $1, &$$) < 0) { 154 } !! 65 pr_debug("%s not found", $1); 155 ; !! 66 YYABORT; >> 67 } >> 68 } >> 69 | expr '+' expr { $$ = $1 + $3; } >> 70 | expr '-' expr { $$ = $1 - $3; } >> 71 | expr '*' expr { $$ = $1 * $3; } >> 72 | expr '/' expr { if ($3 == 0) YYABORT; $$ = $1 / $3; } >> 73 | expr '%' expr { if ((long)$3 == 0) YYABORT; $$ = (long)$1 % (long)$3; } >> 74 | '-' expr %prec NEG { $$ = -$2; } >> 75 | '(' expr ')' { $$ = $2; } >> 76 ; 156 77 157 if_expr: expr IF expr ELSE if_expr !! 78 %% 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 79 199 expr: NUMBER !! 80 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 { 81 { 249 if (is_const($1.val) && is_const($3.va !! 82 char *dst = res->id; 250 assert($1.ids == NULL); !! 83 const char *s = p; 251 assert($3.ids == NULL); !! 84 252 $$.val = (fpclassify($1.val) ! !! 85 while (isalnum(*p) || *p == '_' || *p == '.') { 253 $$.ids = NULL; !! 86 if (p - s >= MAXIDLEN) 254 } else if (is_const($1.val)) { !! 87 return -1; 255 assert($1.ids == NULL); !! 88 *dst++ = *p++; 256 if (fpclassify($1.val) != FP_Z !! 89 } 257 $$ = $3; !! 90 *dst = 0; 258 } else { !! 91 *pp = p; 259 $$.val = 0; !! 92 return ID; 260 $$.ids = NULL; !! 93 } 261 ids__free($3.ids); !! 94 262 } !! 95 static int expr__lex(YYSTYPE *res, const char **pp) 263 } else if (is_const($3.val)) { !! 96 { 264 assert($3.ids == NULL); !! 97 int tok; 265 if (fpclassify($3.val) != FP_Z !! 98 const char *s; 266 $$ = $1; !! 99 const char *p = *pp; 267 } else { !! 100 268 $$.val = 0; !! 101 while (isspace(*p)) 269 $$.ids = NULL; !! 102 p++; 270 ids__free($1.ids); !! 103 s = p; 271 } !! 104 switch (*p++) { 272 } else { !! 105 case 'a' ... 'z': 273 $$ = union_expr($1, $3); !! 106 case 'A' ... 'Z': 274 } !! 107 return expr__symbol(res, p - 1, pp); 275 } !! 108 case '0' ... '9': case '.': 276 | expr '^' expr !! 109 res->num = strtod(s, (char **)&p); 277 { !! 110 tok = NUMBER; 278 if (is_const($1.val) && is_const($3.va !! 111 break; 279 assert($1.ids == NULL); !! 112 default: 280 assert($3.ids == NULL); !! 113 tok = *s; 281 $$.val = (fpclassify($1.val) = !! 114 break; 282 $$.ids = NULL; !! 115 } 283 } else { !! 116 *pp = p; 284 $$ = union_expr($1, $3); !! 117 return tok; 285 } !! 118 } 286 } !! 119 287 | expr '<' expr { BINARY_OP($$, <, $1, $3); } !! 120 /* Caller must make sure id is allocated */ 288 | expr '>' expr { BINARY_OP($$, >, $1, $3); } !! 121 void expr__add_id(struct parse_ctx *ctx, const char *name, double val) 289 | expr '+' expr { BINARY_OP($$, +, $1, $3); } !! 122 { 290 | expr '-' expr { BINARY_OP($$, -, $1, $3); } !! 123 int idx; 291 | expr '*' expr { BINARY_OP($$, *, $1, $3); } !! 124 assert(ctx->num_ids < MAX_PARSE_ID); 292 | expr '/' expr !! 125 idx = ctx->num_ids++; 293 { !! 126 ctx->ids[idx].name = name; 294 if (fpclassify($3.val) == FP_ZERO) { !! 127 ctx->ids[idx].val = val; 295 pr_debug("division by zero\n") !! 128 } 296 assert($3.ids == NULL); !! 129 297 if (compute_ids) !! 130 void expr__ctx_init(struct parse_ctx *ctx) 298 ids__free($1.ids); !! 131 { 299 $$.val = NAN; !! 132 ctx->num_ids = 0; 300 $$.ids = NULL; !! 133 } 301 } else if (!compute_ids || (is_const($ !! 134 302 assert($1.ids == NULL); !! 135 int expr__find_other(const char *p, const char *one, const char ***other, 303 assert($3.ids == NULL); !! 136 int *num_otherp) 304 $$.val = $1.val / $3.val; !! 137 { 305 $$.ids = NULL; !! 138 const char *orig = p; 306 } else { !! 139 int err = -1; 307 /* LHS and/or RHS need computi !! 140 int num_other; 308 $$ = union_expr($1, $3); !! 141 309 } !! 142 *other = malloc((EXPR_MAX_OTHER + 1) * sizeof(char *)); 310 } !! 143 if (!*other) 311 | expr '%' expr !! 144 return -1; 312 { !! 145 313 if (fpclassify($3.val) == FP_ZERO) { !! 146 num_other = 0; 314 pr_debug("division by zero\n") !! 147 for (;;) { 315 YYABORT; !! 148 YYSTYPE val; 316 } else if (!compute_ids || (is_const($ !! 149 int tok = expr__lex(&val, &p); 317 assert($1.ids == NULL); !! 150 if (tok == 0) { 318 assert($3.ids == NULL); !! 151 err = 0; 319 $$.val = (long)$1.val % (long) !! 152 break; 320 $$.ids = NULL; !! 153 } 321 } else { !! 154 if (tok == ID && strcasecmp(one, val.id)) { 322 /* LHS and/or RHS need computi !! 155 if (num_other >= EXPR_MAX_OTHER - 1) { 323 $$ = union_expr($1, $3); !! 156 pr_debug("Too many extra events in %s\n", orig); 324 } !! 157 break; 325 } !! 158 } 326 | D_RATIO '(' expr ',' expr ')' !! 159 (*other)[num_other] = strdup(val.id); 327 { !! 160 if (!(*other)[num_other]) 328 if (fpclassify($5.val) == FP_ZERO) { !! 161 return -1; 329 /* !! 162 num_other++; 330 * Division by constant zero a !! 163 } 331 * are necessary. !! 164 } 332 */ !! 165 (*other)[num_other] = NULL; 333 assert($5.ids == NULL); !! 166 *num_otherp = num_other; 334 $$.val = 0.0; !! 167 if (err) { 335 $$.ids = NULL; !! 168 *num_otherp = 0; 336 ids__free($3.ids); !! 169 free(*other); 337 } else if (!compute_ids || (is_const($ !! 170 *other = NULL; 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 { << 358 if (!compute_ids) { << 359 $$.val = $3.val < $5.val ? $3. << 360 $$.ids = NULL; << 361 } else { << 362 $$ = union_expr($3, $5); << 363 } << 364 } << 365 | MAX '(' expr ',' expr ')' << 366 { << 367 if (!compute_ids) { << 368 $$.val = $3.val > $5.val ? $3. << 369 $$.ids = NULL; << 370 } else { << 371 $$ = union_expr($3, $5); << 372 } 171 } >> 172 return err; 373 } 173 } 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.