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