1 %{ 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 * 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <string.h> 29 #include <stdlib.h> 30 #include <stdio.h> 31 #include <assert.h> 32 #include <ctype.h> 33 #include <errno.h> 34 35 #include <dt_impl.h> 36 #include <dt_grammar.h> 37 #include <dt_parser.h> 38 #include <dt_string.h> 39 40 /* 41 * We need to undefine lex's input and unput macros so that references to these 42 * call the functions provided at the end of this source file. 43 */ 44 #if defined(sun) 45 #undef input 46 #undef unput 47 #else 48 /* 49 * Define YY_INPUT for flex since input() can't be re-defined. 50 */ 51 #define YY_INPUT(buf,result,max_size) \ 52 if (yypcb->pcb_fileptr != NULL) { \ 53 if (((result = fread(buf, 1, max_size, yypcb->pcb_fileptr)) == 0) \ 54 && ferror(yypcb->pcb_fileptr)) \ 55 longjmp(yypcb->pcb_jmpbuf, EDT_FIO); \ 56 } else { \ 57 int n; \ 58 for (n = 0; n < max_size && \ 59 yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen; n++) \ 60 buf[n] = *yypcb->pcb_strptr++; \ 61 result = n; \ 62 } 63 /* 64 * Do not EOF let tokens to be put back. This does not work with flex. 65 * On the other hand, leaving current buffer in same state it was when 66 * last EOF was received guarantees that input() will keep returning EOF 67 * for all subsequent invocations, which is the effect desired. 68 */ 69 #undef unput 70 #define unput(c) \ 71 do { \ 72 int _c = c; \ 73 if (_c != EOF) \ 74 yyunput(_c, yytext_ptr); \ 75 } while(0) 76 #endif 77 78 static int id_or_type(const char *); 79 #if defined(sun) 80 static int input(void); 81 static void unput(int); 82 #endif 83 84 /* 85 * We first define a set of labeled states for use in the D lexer and then a 86 * set of regular expressions to simplify things below. The lexer states are: 87 * 88 * S0 - D program clause and expression lexing 89 * S1 - D comments (i.e. skip everything until end of comment) 90 * S2 - D program outer scope (probe specifiers and declarations) 91 * S3 - D control line parsing (i.e. after ^# is seen but before \n) 92 * S4 - D control line scan (locate control directives only and invoke S3) 93 */ 94 %} 95 96 %e 1500 /* maximum nodes */ 97 %p 3700 /* maximum positions */ 98 %n 600 /* maximum states */ 99 100 %s S0 S1 S2 S3 S4 101 102 RGX_AGG "@"[a-zA-Z_][0-9a-zA-Z_]* 103 RGX_PSPEC [-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]* 104 RGX_IDENT [a-zA-Z_`][0-9a-zA-Z_`]* 105 RGX_INT ([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]? 106 RGX_FP ([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]? 107 RGX_WS [\f\n\r\t\v ] 108 RGX_STR ([^"\\\n]|\\[^"\n]|\\\")* 109 RGX_CHR ([^'\\\n]|\\[^'\n]|\\')* 110 RGX_INTERP ^[\f\t\v ]*#!.* 111 RGX_CTL ^[\f\t\v ]*# 112 113 %% 114 115 %{ 116 117 /* 118 * We insert a special prologue into yylex() itself: if the pcb contains a 119 * context token, we return that prior to running the normal lexer. This 120 * allows libdtrace to force yacc into one of our three parsing contexts: D 121 * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE). 122 * Once the token is returned, we clear it so this only happens once. 123 */ 124 if (yypcb->pcb_token != 0) { 125 int tok = yypcb->pcb_token; 126 yypcb->pcb_token = 0; 127 return (tok); 128 } 129 130 %} 131 132 <S0>auto return (DT_KEY_AUTO); 133 <S0>break return (DT_KEY_BREAK); 134 <S0>case return (DT_KEY_CASE); 135 <S0>char return (DT_KEY_CHAR); 136 <S0>const return (DT_KEY_CONST); 137 <S0>continue return (DT_KEY_CONTINUE); 138 <S0>counter return (DT_KEY_COUNTER); 139 <S0>default return (DT_KEY_DEFAULT); 140 <S0>do return (DT_KEY_DO); 141 <S0>double return (DT_KEY_DOUBLE); 142 <S0>else return (DT_KEY_ELSE); 143 <S0>enum return (DT_KEY_ENUM); 144 <S0>extern return (DT_KEY_EXTERN); 145 <S0>float return (DT_KEY_FLOAT); 146 <S0>for return (DT_KEY_FOR); 147 <S0>goto return (DT_KEY_GOTO); 148 <S0>if return (DT_KEY_IF); 149 <S0>import return (DT_KEY_IMPORT); 150 <S0>inline return (DT_KEY_INLINE); 151 <S0>int return (DT_KEY_INT); 152 <S0>long return (DT_KEY_LONG); 153 <S0>offsetof return (DT_TOK_OFFSETOF); 154 <S0>probe return (DT_KEY_PROBE); 155 <S0>provider return (DT_KEY_PROVIDER); 156 <S0>register return (DT_KEY_REGISTER); 157 <S0>restrict return (DT_KEY_RESTRICT); 158 <S0>return return (DT_KEY_RETURN); 159 <S0>self return (DT_KEY_SELF); 160 <S0>short return (DT_KEY_SHORT); 161 <S0>signed return (DT_KEY_SIGNED); 162 <S0>sizeof return (DT_TOK_SIZEOF); 163 <S0>static return (DT_KEY_STATIC); 164 <S0>string return (DT_KEY_STRING); 165 <S0>stringof return (DT_TOK_STRINGOF); 166 <S0>struct return (DT_KEY_STRUCT); 167 <S0>switch return (DT_KEY_SWITCH); 168 <S0>this return (DT_KEY_THIS); 169 <S0>translator return (DT_KEY_XLATOR); 170 <S0>typedef return (DT_KEY_TYPEDEF); 171 <S0>union return (DT_KEY_UNION); 172 <S0>unsigned return (DT_KEY_UNSIGNED); 173 <S0>void return (DT_KEY_VOID); 174 <S0>volatile return (DT_KEY_VOLATILE); 175 <S0>while return (DT_KEY_WHILE); 176 <S0>xlate return (DT_TOK_XLATE); 177 178 <S2>auto { yybegin(YYS_EXPR); return (DT_KEY_AUTO); } 179 <S2>char { yybegin(YYS_EXPR); return (DT_KEY_CHAR); } 180 <S2>const { yybegin(YYS_EXPR); return (DT_KEY_CONST); } 181 <S2>counter { yybegin(YYS_DEFINE); return (DT_KEY_COUNTER); } 182 <S2>double { yybegin(YYS_EXPR); return (DT_KEY_DOUBLE); } 183 <S2>enum { yybegin(YYS_EXPR); return (DT_KEY_ENUM); } 184 <S2>extern { yybegin(YYS_EXPR); return (DT_KEY_EXTERN); } 185 <S2>float { yybegin(YYS_EXPR); return (DT_KEY_FLOAT); } 186 <S2>import { yybegin(YYS_EXPR); return (DT_KEY_IMPORT); } 187 <S2>inline { yybegin(YYS_DEFINE); return (DT_KEY_INLINE); } 188 <S2>int { yybegin(YYS_EXPR); return (DT_KEY_INT); } 189 <S2>long { yybegin(YYS_EXPR); return (DT_KEY_LONG); } 190 <S2>provider { yybegin(YYS_DEFINE); return (DT_KEY_PROVIDER); } 191 <S2>register { yybegin(YYS_EXPR); return (DT_KEY_REGISTER); } 192 <S2>restrict { yybegin(YYS_EXPR); return (DT_KEY_RESTRICT); } 193 <S2>self { yybegin(YYS_EXPR); return (DT_KEY_SELF); } 194 <S2>short { yybegin(YYS_EXPR); return (DT_KEY_SHORT); } 195 <S2>signed { yybegin(YYS_EXPR); return (DT_KEY_SIGNED); } 196 <S2>static { yybegin(YYS_EXPR); return (DT_KEY_STATIC); } 197 <S2>string { yybegin(YYS_EXPR); return (DT_KEY_STRING); } 198 <S2>struct { yybegin(YYS_EXPR); return (DT_KEY_STRUCT); } 199 <S2>this { yybegin(YYS_EXPR); return (DT_KEY_THIS); } 200 <S2>translator { yybegin(YYS_DEFINE); return (DT_KEY_XLATOR); } 201 <S2>typedef { yybegin(YYS_EXPR); return (DT_KEY_TYPEDEF); } 202 <S2>union { yybegin(YYS_EXPR); return (DT_KEY_UNION); } 203 <S2>unsigned { yybegin(YYS_EXPR); return (DT_KEY_UNSIGNED); } 204 <S2>void { yybegin(YYS_EXPR); return (DT_KEY_VOID); } 205 <S2>volatile { yybegin(YYS_EXPR); return (DT_KEY_VOLATILE); } 206 207 <S0>"$$"[0-9]+ { 208 int i = atoi(yytext + 2); 209 char *v = ""; 210 211 /* 212 * A macro argument reference substitutes the text of 213 * an argument in place of the current token. When we 214 * see $$<d> we fetch the saved string from pcb_sargv 215 * (or use the default argument if the option has been 216 * set and the argument hasn't been specified) and 217 * return a token corresponding to this string. 218 */ 219 if (i < 0 || (i >= yypcb->pcb_sargc && 220 !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) { 221 xyerror(D_MACRO_UNDEF, "macro argument %s is " 222 "not defined\n", yytext); 223 } 224 225 if (i < yypcb->pcb_sargc) { 226 v = yypcb->pcb_sargv[i]; /* get val from pcb */ 227 yypcb->pcb_sflagv[i] |= DT_IDFLG_REF; 228 } 229 230 if ((yylval.l_str = strdup(v)) == NULL) 231 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 232 233 (void) stresc2chr(yylval.l_str); 234 return (DT_TOK_STRING); 235 } 236 237 <S0>"$"[0-9]+ { 238 int i = atoi(yytext + 1); 239 char *p, *v = "0"; 240 241 /* 242 * A macro argument reference substitutes the text of 243 * one identifier or integer pattern for another. When 244 * we see $<d> we fetch the saved string from pcb_sargv 245 * (or use the default argument if the option has been 246 * set and the argument hasn't been specified) and 247 * return a token corresponding to this string. 248 */ 249 if (i < 0 || (i >= yypcb->pcb_sargc && 250 !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) { 251 xyerror(D_MACRO_UNDEF, "macro argument %s is " 252 "not defined\n", yytext); 253 } 254 255 if (i < yypcb->pcb_sargc) { 256 v = yypcb->pcb_sargv[i]; /* get val from pcb */ 257 yypcb->pcb_sflagv[i] |= DT_IDFLG_REF; 258 } 259 260 /* 261 * If the macro text is not a valid integer or ident, 262 * then we treat it as a string. The string may be 263 * optionally enclosed in quotes, which we strip. 264 */ 265 if (strbadidnum(v)) { 266 size_t len = strlen(v); 267 268 if (len != 1 && *v == '"' && v[len - 1] == '"') 269 yylval.l_str = strndup(v + 1, len - 2); 270 else 271 yylval.l_str = strndup(v, len); 272 273 if (yylval.l_str == NULL) 274 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 275 276 (void) stresc2chr(yylval.l_str); 277 return (DT_TOK_STRING); 278 } 279 280 /* 281 * If the macro text is not a string an begins with a 282 * digit or a +/- sign, process it as an integer token. 283 */ 284 if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') { 285 if (isdigit(v[0])) 286 yyintprefix = 0; 287 else 288 yyintprefix = *v++; 289 290 errno = 0; 291 yylval.l_int = strtoull(v, &p, 0); 292 (void) strncpy(yyintsuffix, p, 293 sizeof (yyintsuffix)); 294 yyintdecimal = *v != '0'; 295 296 if (errno == ERANGE) { 297 xyerror(D_MACRO_OFLOW, "macro argument" 298 " %s constant %s results in integer" 299 " overflow\n", yytext, v); 300 } 301 302 return (DT_TOK_INT); 303 } 304 305 return (id_or_type(v)); 306 } 307 308 <S0>"$$"{RGX_IDENT} { 309 dt_ident_t *idp = dt_idhash_lookup( 310 yypcb->pcb_hdl->dt_macros, yytext + 2); 311 312 char s[16]; /* enough for UINT_MAX + \0 */ 313 314 if (idp == NULL) { 315 xyerror(D_MACRO_UNDEF, "macro variable %s " 316 "is not defined\n", yytext); 317 } 318 319 /* 320 * For the moment, all current macro variables are of 321 * type id_t (refer to dtrace_update() for details). 322 */ 323 (void) snprintf(s, sizeof (s), "%u", idp->di_id); 324 if ((yylval.l_str = strdup(s)) == NULL) 325 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 326 327 return (DT_TOK_STRING); 328 } 329 330 <S0>"$"{RGX_IDENT} { 331 dt_ident_t *idp = dt_idhash_lookup( 332 yypcb->pcb_hdl->dt_macros, yytext + 1); 333 334 if (idp == NULL) { 335 xyerror(D_MACRO_UNDEF, "macro variable %s " 336 "is not defined\n", yytext); 337 } 338 339 /* 340 * For the moment, all current macro variables are of 341 * type id_t (refer to dtrace_update() for details). 342 */ 343 yylval.l_int = (intmax_t)(int)idp->di_id; 344 yyintprefix = 0; 345 yyintsuffix[0] = '\0'; 346 yyintdecimal = 1; 347 348 return (DT_TOK_INT); 349 } 350 351 <S0>{RGX_IDENT} { 352 return (id_or_type(yytext)); 353 } 354 355 <S0>{RGX_AGG} { 356 if ((yylval.l_str = strdup(yytext)) == NULL) 357 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 358 return (DT_TOK_AGG); 359 } 360 361 <S0>"@" { 362 if ((yylval.l_str = strdup("@_")) == NULL) 363 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 364 return (DT_TOK_AGG); 365 } 366 367 <S0>{RGX_INT} | 368 <S2>{RGX_INT} | 369 <S3>{RGX_INT} { 370 char *p; 371 372 errno = 0; 373 yylval.l_int = strtoull(yytext, &p, 0); 374 yyintprefix = 0; 375 (void) strncpy(yyintsuffix, p, sizeof (yyintsuffix)); 376 yyintdecimal = yytext[0] != '0'; 377 378 if (errno == ERANGE) { 379 xyerror(D_INT_OFLOW, "constant %s results in " 380 "integer overflow\n", yytext); 381 } 382 383 if (*p != '\0' && strchr("uUlL", *p) == NULL) { 384 xyerror(D_INT_DIGIT, "constant %s contains " 385 "invalid digit %c\n", yytext, *p); 386 } 387 388 if ((YYSTATE) != S3) 389 return (DT_TOK_INT); 390 391 yypragma = dt_node_link(yypragma, 392 dt_node_int(yylval.l_int)); 393 } 394 395 <S0>{RGX_FP} yyerror("floating-point constants are not permitted\n"); 396 397 <S0>\"{RGX_STR}$ | 398 <S3>\"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal"); 399 400 <S0>\"{RGX_STR}\" | 401 <S3>\"{RGX_STR}\" { 402 /* 403 * Quoted string -- convert C escape sequences and 404 * return the string as a token. 405 */ 406 yylval.l_str = strndup(yytext + 1, yyleng - 2); 407 408 if (yylval.l_str == NULL) 409 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 410 411 (void) stresc2chr(yylval.l_str); 412 if ((YYSTATE) != S3) 413 return (DT_TOK_STRING); 414 415 yypragma = dt_node_link(yypragma, 416 dt_node_string(yylval.l_str)); 417 } 418 419 <S0>'{RGX_CHR}$ xyerror(D_CHR_NL, "newline encountered in character constant"); 420 421 <S0>'{RGX_CHR}' { 422 char *s, *p, *q; 423 size_t nbytes; 424 425 /* 426 * Character constant -- convert C escape sequences and 427 * return the character as an integer immediate value. 428 */ 429 if (yyleng == 2) 430 xyerror(D_CHR_NULL, "empty character constant"); 431 432 s = yytext + 1; 433 yytext[yyleng - 1] = '\0'; 434 nbytes = stresc2chr(s); 435 yylval.l_int = 0; 436 yyintprefix = 0; 437 yyintsuffix[0] = '\0'; 438 yyintdecimal = 1; 439 440 if (nbytes > sizeof (yylval.l_int)) { 441 xyerror(D_CHR_OFLOW, "character constant is " 442 "too long"); 443 } 444 #if BYTE_ORDER == _LITTLE_ENDIAN 445 p = ((char *)&yylval.l_int) + nbytes - 1; 446 for (q = s; nbytes != 0; nbytes--) 447 *p-- = *q++; 448 #else 449 bcopy(s, ((char *)&yylval.l_int) + 450 sizeof (yylval.l_int) - nbytes, nbytes); 451 #endif 452 return (DT_TOK_INT); 453 } 454 455 <S0>"/*" | 456 <S2>"/*" { 457 yypcb->pcb_cstate = (YYSTATE); 458 BEGIN(S1); 459 } 460 461 <S0>{RGX_INTERP} | 462 <S2>{RGX_INTERP} ; /* discard any #! lines */ 463 464 <S0>{RGX_CTL} | 465 <S2>{RGX_CTL} | 466 <S4>{RGX_CTL} { 467 assert(yypragma == NULL); 468 yypcb->pcb_cstate = (YYSTATE); 469 BEGIN(S3); 470 } 471 472 <S4>. ; /* discard */ 473 <S4>"\n" ; /* discard */ 474 475 <S0>"/" { 476 int c, tok; 477 478 /* 479 * The use of "/" as the predicate delimiter and as the 480 * integer division symbol requires special lookahead 481 * to avoid a shift/reduce conflict in the D grammar. 482 * We look ahead to the next non-whitespace character. 483 * If we encounter EOF, ";", "{", or "/", then this "/" 484 * closes the predicate and we return DT_TOK_EPRED. 485 * If we encounter anything else, it's DT_TOK_DIV. 486 */ 487 while ((c = input()) != 0) { 488 if (strchr("\f\n\r\t\v ", c) == NULL) 489 break; 490 } 491 492 if (c == 0 || c == ';' || c == '{' || c == '/') { 493 if (yypcb->pcb_parens != 0) { 494 yyerror("closing ) expected in " 495 "predicate before /\n"); 496 } 497 if (yypcb->pcb_brackets != 0) { 498 yyerror("closing ] expected in " 499 "predicate before /\n"); 500 } 501 tok = DT_TOK_EPRED; 502 } else 503 tok = DT_TOK_DIV; 504 505 unput(c); 506 return (tok); 507 } 508 509 <S0>"(" { 510 yypcb->pcb_parens++; 511 return (DT_TOK_LPAR); 512 } 513 514 <S0>")" { 515 if (--yypcb->pcb_parens < 0) 516 yyerror("extra ) in input stream\n"); 517 return (DT_TOK_RPAR); 518 } 519 520 <S0>"[" { 521 yypcb->pcb_brackets++; 522 return (DT_TOK_LBRAC); 523 } 524 525 <S0>"]" { 526 if (--yypcb->pcb_brackets < 0) 527 yyerror("extra ] in input stream\n"); 528 return (DT_TOK_RBRAC); 529 } 530 531 <S0>"{" | 532 <S2>"{" { 533 yypcb->pcb_braces++; 534 return ('{'); 535 } 536 537 <S0>"}" { 538 if (--yypcb->pcb_braces < 0) 539 yyerror("extra } in input stream\n"); 540 return ('}'); 541 } 542 543 <S0>"|" return (DT_TOK_BOR); 544 <S0>"^" return (DT_TOK_XOR); 545 <S0>"&" return (DT_TOK_BAND); 546 <S0>"&&" return (DT_TOK_LAND); 547 <S0>"^^" return (DT_TOK_LXOR); 548 <S0>"||" return (DT_TOK_LOR); 549 <S0>"==" return (DT_TOK_EQU); 550 <S0>"!=" return (DT_TOK_NEQ); 551 <S0>"<" return (DT_TOK_LT); 552 <S0>"<=" return (DT_TOK_LE); 553 <S0>">" return (DT_TOK_GT); 554 <S0>">=" return (DT_TOK_GE); 555 <S0>"<<" return (DT_TOK_LSH); 556 <S0>">>" return (DT_TOK_RSH); 557 <S0>"+" return (DT_TOK_ADD); 558 <S0>"-" return (DT_TOK_SUB); 559 <S0>"*" return (DT_TOK_MUL); 560 <S0>"%" return (DT_TOK_MOD); 561 <S0>"~" return (DT_TOK_BNEG); 562 <S0>"!" return (DT_TOK_LNEG); 563 <S0>"?" return (DT_TOK_QUESTION); 564 <S0>":" return (DT_TOK_COLON); 565 <S0>"." return (DT_TOK_DOT); 566 <S0>"->" return (DT_TOK_PTR); 567 <S0>"=" return (DT_TOK_ASGN); 568 <S0>"+=" return (DT_TOK_ADD_EQ); 569 <S0>"-=" return (DT_TOK_SUB_EQ); 570 <S0>"*=" return (DT_TOK_MUL_EQ); 571 <S0>"/=" return (DT_TOK_DIV_EQ); 572 <S0>"%=" return (DT_TOK_MOD_EQ); 573 <S0>"&=" return (DT_TOK_AND_EQ); 574 <S0>"^=" return (DT_TOK_XOR_EQ); 575 <S0>"|=" return (DT_TOK_OR_EQ); 576 <S0>"<<=" return (DT_TOK_LSH_EQ); 577 <S0>">>=" return (DT_TOK_RSH_EQ); 578 <S0>"++" return (DT_TOK_ADDADD); 579 <S0>"--" return (DT_TOK_SUBSUB); 580 <S0>"..." return (DT_TOK_ELLIPSIS); 581 <S0>"," return (DT_TOK_COMMA); 582 <S0>";" return (';'); 583 <S0>{RGX_WS} ; /* discard */ 584 <S0>"\\"\n ; /* discard */ 585 <S0>. yyerror("syntax error near \"%c\"\n", yytext[0]); 586 587 <S1>"/*" yyerror("/* encountered inside a comment\n"); 588 <S1>"*/" BEGIN(yypcb->pcb_cstate); 589 <S1>.|\n ; /* discard */ 590 591 <S2>{RGX_PSPEC} { 592 /* 593 * S2 has an ambiguity because RGX_PSPEC includes '*' 594 * as a glob character and '*' also can be DT_TOK_STAR. 595 * Since lex always matches the longest token, this 596 * rule can be matched by an input string like "int*", 597 * which could begin a global variable declaration such 598 * as "int*x;" or could begin a RGX_PSPEC with globbing 599 * such as "int* { trace(timestamp); }". If C_PSPEC is 600 * not set, we must resolve the ambiguity in favor of 601 * the type and perform lexer pushback if the fragment 602 * before '*' or entire fragment matches a type name. 603 * If C_PSPEC is set, we always return a PSPEC token. 604 * If C_PSPEC is off, the user can avoid ambiguity by 605 * including a ':' delimiter in the specifier, which 606 * they should be doing anyway to specify the provider. 607 */ 608 if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) && 609 strchr(yytext, ':') == NULL) { 610 611 char *p = strchr(yytext, '*'); 612 char *q = yytext + yyleng - 1; 613 614 if (p != NULL && p > yytext) 615 *p = '\0'; /* prune yytext */ 616 617 if (dt_type_lookup(yytext, NULL) == 0) { 618 yylval.l_str = strdup(yytext); 619 620 if (yylval.l_str == NULL) { 621 longjmp(yypcb->pcb_jmpbuf, 622 EDT_NOMEM); 623 } 624 625 if (p != NULL && p > yytext) { 626 for (*p = '*'; q >= p; q--) 627 unput(*q); 628 } 629 630 yybegin(YYS_EXPR); 631 return (DT_TOK_TNAME); 632 } 633 634 if (p != NULL && p > yytext) 635 *p = '*'; /* restore yytext */ 636 } 637 638 if ((yylval.l_str = strdup(yytext)) == NULL) 639 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 640 641 return (DT_TOK_PSPEC); 642 } 643 644 <S2>"/" return (DT_TOK_DIV); 645 <S2>"," return (DT_TOK_COMMA); 646 647 <S2>{RGX_WS} ; /* discard */ 648 <S2>. yyerror("syntax error near \"%c\"\n", yytext[0]); 649 650 <S3>\n { 651 dt_pragma(yypragma); 652 yypragma = NULL; 653 BEGIN(yypcb->pcb_cstate); 654 } 655 656 <S3>[\f\t\v ]+ ; /* discard */ 657 658 <S3>[^\f\n\t\v "]+ { 659 dt_node_t *dnp; 660 661 if ((yylval.l_str = strdup(yytext)) == NULL) 662 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 663 664 /* 665 * We want to call dt_node_ident() here, but we can't 666 * because it will expand inlined identifiers, which we 667 * don't want to do from #pragma context in order to 668 * support pragmas that apply to the ident itself. We 669 * call dt_node_string() and then reset dn_op instead. 670 */ 671 dnp = dt_node_string(yylval.l_str); 672 dnp->dn_kind = DT_NODE_IDENT; 673 dnp->dn_op = DT_TOK_IDENT; 674 yypragma = dt_node_link(yypragma, dnp); 675 } 676 677 <S3>. yyerror("syntax error near \"%c\"\n", yytext[0]); 678 679 %% 680 681 /* 682 * yybegin provides a wrapper for use from C code around the lex BEGIN() macro. 683 * We use two main states for lexing because probe descriptions use a syntax 684 * that is incompatible with the normal D tokens (e.g. names can contain "-"). 685 * yybegin also handles the job of switching between two lists of dt_nodes 686 * as we allocate persistent definitions, like inlines, and transient nodes 687 * that will be freed once we are done parsing the current program file. 688 */ 689 void 690 yybegin(yystate_t state) 691 { 692 #ifdef YYDEBUG 693 yydebug = _dtrace_debug; 694 #endif 695 if (yypcb->pcb_yystate == state) 696 return; /* nothing to do if we're in the state already */ 697 698 if (yypcb->pcb_yystate == YYS_DEFINE) { 699 yypcb->pcb_list = yypcb->pcb_hold; 700 yypcb->pcb_hold = NULL; 701 } 702 703 switch (state) { 704 case YYS_CLAUSE: 705 BEGIN(S2); 706 break; 707 case YYS_DEFINE: 708 assert(yypcb->pcb_hold == NULL); 709 yypcb->pcb_hold = yypcb->pcb_list; 710 yypcb->pcb_list = NULL; 711 /*FALLTHRU*/ 712 case YYS_EXPR: 713 BEGIN(S0); 714 break; 715 case YYS_DONE: 716 break; 717 case YYS_CONTROL: 718 BEGIN(S4); 719 break; 720 default: 721 xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state); 722 } 723 724 yypcb->pcb_yystate = state; 725 } 726 727 void 728 yyinit(dt_pcb_t *pcb) 729 { 730 yypcb = pcb; 731 yylineno = 1; 732 yypragma = NULL; 733 #if defined(sun) 734 yysptr = yysbuf; 735 #endif 736 } 737 738 /* 739 * Given a lexeme 's' (typically yytext), set yylval and return an appropriate 740 * token to the parser indicating either an identifier or a typedef name. 741 * User-defined global variables always take precedence over types, but we do 742 * use some heuristics because D programs can look at an ever-changing set of 743 * kernel types and also can implicitly instantiate variables by assignment, 744 * unlike in C. The code here is ordered carefully as lookups are not cheap. 745 */ 746 static int 747 id_or_type(const char *s) 748 { 749 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 750 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl; 751 int c0, c1, ttok = DT_TOK_TNAME; 752 dt_ident_t *idp; 753 754 if ((s = yylval.l_str = strdup(s)) == NULL) 755 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 756 757 /* 758 * If the lexeme is a global variable or likely identifier or *not* a 759 * type_name, then it is an identifier token. 760 */ 761 if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL || 762 dt_idhash_lookup(yypcb->pcb_idents, s) != NULL || 763 dt_type_lookup(s, NULL) != 0) 764 return (DT_TOK_IDENT); 765 766 /* 767 * If we're in the midst of parsing a declaration and a type_specifier 768 * has already been shifted, then return DT_TOK_IDENT instead of TNAME. 769 * This semantic is necessary to permit valid ISO C code such as: 770 * 771 * typedef int foo; 772 * struct s { foo foo; }; 773 * 774 * without causing shift/reduce conflicts in the direct_declarator part 775 * of the grammar. The result is that we must check for conflicting 776 * redeclarations of the same identifier as part of dt_node_decl(). 777 */ 778 if (ddp != NULL && ddp->dd_name != NULL) 779 return (DT_TOK_IDENT); 780 781 /* 782 * If the lexeme is a type name and we are not in a program clause, 783 * then always interpret it as a type and return DT_TOK_TNAME. 784 */ 785 if ((YYSTATE) != S0) 786 return (DT_TOK_TNAME); 787 788 /* 789 * If the lexeme matches a type name but is in a program clause, then 790 * it could be a type or it could be an undefined variable. Peek at 791 * the next token to decide. If we see ++, --, [, or =, we know there 792 * might be an assignment that is trying to create a global variable, 793 * so we optimistically return DT_TOK_IDENT. There is no harm in being 794 * wrong: a type_name followed by ++, --, [, or = is a syntax error. 795 */ 796 while ((c0 = input()) != 0) { 797 if (strchr("\f\n\r\t\v ", c0) == NULL) 798 break; 799 } 800 801 switch (c0) { 802 case '+': 803 case '-': 804 if ((c1 = input()) == c0) 805 ttok = DT_TOK_IDENT; 806 unput(c1); 807 break; 808 809 case '=': 810 if ((c1 = input()) != c0) 811 ttok = DT_TOK_IDENT; 812 unput(c1); 813 break; 814 case '[': 815 ttok = DT_TOK_IDENT; 816 break; 817 } 818 819 if (ttok == DT_TOK_IDENT) { 820 idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0, 821 0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen); 822 823 if (idp == NULL) 824 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 825 } 826 827 unput(c0); 828 return (ttok); 829 } 830 831 #if defined(sun) 832 static int 833 input(void) 834 { 835 int c; 836 837 if (yysptr > yysbuf) 838 c = *--yysptr; 839 else if (yypcb->pcb_fileptr != NULL) 840 c = fgetc(yypcb->pcb_fileptr); 841 else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen) 842 c = *yypcb->pcb_strptr++; 843 else 844 c = EOF; 845 846 if (c == '\n') 847 yylineno++; 848 849 if (c != EOF) 850 return (c); 851 852 if ((YYSTATE) == S1) 853 yyerror("end-of-file encountered before matching */\n"); 854 855 if ((YYSTATE) == S3) 856 yyerror("end-of-file encountered before end of control line\n"); 857 858 if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr)) 859 longjmp(yypcb->pcb_jmpbuf, EDT_FIO); 860 861 return (0); /* EOF */ 862 } 863 864 static void 865 unput(int c) 866 { 867 if (c == '\n') 868 yylineno--; 869 870 *yysptr++ = c; 871 yytchar = c; 872 } 873 #endif 874