%{ /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END * * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #pragma ident "%Z%%M% %I% %E% SMI" #include #include #include #include #include #include #include #include #include #include /* * We need to undefine lex's input and unput macros so that references to these * call the functions provided at the end of this source file. */ #undef input #undef unput static int id_or_type(const char *); static int input(void); static void unput(int); /* * We first define a set of labeled states for use in the D lexer and then a * set of regular expressions to simplify things below. The lexer states are: * * S0 - D program clause and expression lexing * S1 - D comments (i.e. skip everything until end of comment) * S2 - D program outer scope (probe specifiers and declarations) * S3 - D control line parsing (i.e. after ^# is seen but before \n) */ %} %e 1400 /* maximum nodes */ %p 3700 /* maximum positions */ %s S0 S1 S2 S3 RGX_AGG "@"[a-zA-Z_][0-9a-zA-Z_]* RGX_PSPEC [-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]* RGX_IDENT [a-zA-Z_`][0-9a-zA-Z_`]* RGX_INT ([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]? RGX_FP ([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]? RGX_WS [\f\n\r\t\v ] RGX_STR ([^"\\\n]|\\[^"\n]|\\\")* RGX_CHR ([^'\\\n]|\\[^'\n]|\\')* RGX_INTERP ^[\f\t\v ]*#!.* RGX_CTL ^[\f\t\v ]*# %% %{ /* * We insert a special prologue into yylex() itself: if the pcb contains a * context token, we return that prior to running the normal lexer. This * allows libdtrace to force yacc into one of our three parsing contexts: D * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE). * Once the token is returned, we clear it so this only happens once. */ if (yypcb->pcb_token != 0) { int tok = yypcb->pcb_token; yypcb->pcb_token = 0; return (tok); } %} auto return (DT_KEY_AUTO); break return (DT_KEY_BREAK); case return (DT_KEY_CASE); char return (DT_KEY_CHAR); const return (DT_KEY_CONST); continue return (DT_KEY_CONTINUE); counter return (DT_KEY_COUNTER); default return (DT_KEY_DEFAULT); do return (DT_KEY_DO); double return (DT_KEY_DOUBLE); else return (DT_KEY_ELSE); enum return (DT_KEY_ENUM); extern return (DT_KEY_EXTERN); float return (DT_KEY_FLOAT); for return (DT_KEY_FOR); goto return (DT_KEY_GOTO); if return (DT_KEY_IF); import return (DT_KEY_IMPORT); inline return (DT_KEY_INLINE); int return (DT_KEY_INT); long return (DT_KEY_LONG); offsetof return (DT_TOK_OFFSETOF); probe return (DT_KEY_PROBE); provider return (DT_KEY_PROVIDER); register return (DT_KEY_REGISTER); restrict return (DT_KEY_RESTRICT); return return (DT_KEY_RETURN); self return (DT_KEY_SELF); short return (DT_KEY_SHORT); signed return (DT_KEY_SIGNED); sizeof return (DT_TOK_SIZEOF); static return (DT_KEY_STATIC); string return (DT_KEY_STRING); stringof return (DT_TOK_STRINGOF); struct return (DT_KEY_STRUCT); switch return (DT_KEY_SWITCH); this return (DT_KEY_THIS); translator return (DT_KEY_XLATOR); typedef return (DT_KEY_TYPEDEF); union return (DT_KEY_UNION); unsigned return (DT_KEY_UNSIGNED); void return (DT_KEY_VOID); volatile return (DT_KEY_VOLATILE); while return (DT_KEY_WHILE); xlate return (DT_TOK_XLATE); auto { yybegin(YYS_EXPR); return (DT_KEY_AUTO); } char { yybegin(YYS_EXPR); return (DT_KEY_CHAR); } const { yybegin(YYS_EXPR); return (DT_KEY_CONST); } counter { yybegin(YYS_DEFINE); return (DT_KEY_COUNTER); } double { yybegin(YYS_EXPR); return (DT_KEY_DOUBLE); } enum { yybegin(YYS_EXPR); return (DT_KEY_ENUM); } extern { yybegin(YYS_EXPR); return (DT_KEY_EXTERN); } float { yybegin(YYS_EXPR); return (DT_KEY_FLOAT); } import { yybegin(YYS_EXPR); return (DT_KEY_IMPORT); } inline { yybegin(YYS_DEFINE); return (DT_KEY_INLINE); } int { yybegin(YYS_EXPR); return (DT_KEY_INT); } long { yybegin(YYS_EXPR); return (DT_KEY_LONG); } provider { yybegin(YYS_DEFINE); return (DT_KEY_PROVIDER); } register { yybegin(YYS_EXPR); return (DT_KEY_REGISTER); } restrict { yybegin(YYS_EXPR); return (DT_KEY_RESTRICT); } self { yybegin(YYS_EXPR); return (DT_KEY_SELF); } short { yybegin(YYS_EXPR); return (DT_KEY_SHORT); } signed { yybegin(YYS_EXPR); return (DT_KEY_SIGNED); } static { yybegin(YYS_EXPR); return (DT_KEY_STATIC); } string { yybegin(YYS_EXPR); return (DT_KEY_STRING); } struct { yybegin(YYS_EXPR); return (DT_KEY_STRUCT); } this { yybegin(YYS_EXPR); return (DT_KEY_THIS); } translator { yybegin(YYS_DEFINE); return (DT_KEY_XLATOR); } typedef { yybegin(YYS_EXPR); return (DT_KEY_TYPEDEF); } union { yybegin(YYS_EXPR); return (DT_KEY_UNION); } unsigned { yybegin(YYS_EXPR); return (DT_KEY_UNSIGNED); } void { yybegin(YYS_EXPR); return (DT_KEY_VOID); } volatile { yybegin(YYS_EXPR); return (DT_KEY_VOLATILE); } "$$"[0-9]+ { int i = atoi(yytext + 2); char *v = ""; /* * A macro argument reference substitutes the text of * an argument in place of the current token. When we * see $$ we fetch the saved string from pcb_sargv * (or use the default argument if the option has been * set and the argument hasn't been specified) and * return a token corresponding to this string. */ if (i < 0 || (i >= yypcb->pcb_sargc && !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) { xyerror(D_MACRO_UNDEF, "macro argument %s is " "not defined\n", yytext); } if (i < yypcb->pcb_sargc) { v = yypcb->pcb_sargv[i]; /* get val from pcb */ yypcb->pcb_sflagv[i] |= DT_IDFLG_REF; } if ((yylval.l_str = strdup(v)) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); (void) stresc2chr(yylval.l_str); return (DT_TOK_STRING); } "$"[0-9]+ { int i = atoi(yytext + 1); char *p, *v = "0"; /* * A macro argument reference substitutes the text of * one identifier or integer pattern for another. When * we see $ we fetch the saved string from pcb_sargv * (or use the default argument if the option has been * set and the argument hasn't been specified) and * return a token corresponding to this string. */ if (i < 0 || (i >= yypcb->pcb_sargc && !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) { xyerror(D_MACRO_UNDEF, "macro argument %s is " "not defined\n", yytext); } if (i < yypcb->pcb_sargc) { v = yypcb->pcb_sargv[i]; /* get val from pcb */ yypcb->pcb_sflagv[i] |= DT_IDFLG_REF; } /* * If the macro text is not a valid integer or ident, * then we treat it as a string. The string may be * optionally enclosed in quotes, which we strip. */ if (strbadidnum(v)) { size_t len = strlen(v); if (len != 1 && *v == '"' && v[len - 1] == '"') yylval.l_str = strndup(v + 1, len - 2); else yylval.l_str = strndup(v, len); if (yylval.l_str == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); (void) stresc2chr(yylval.l_str); return (DT_TOK_STRING); } /* * If the macro text is not a string an begins with a * digit or a +/- sign, process it as an integer token. */ if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') { if (isdigit(v[0])) yyintprefix = 0; else yyintprefix = *v++; errno = 0; yylval.l_int = strtoull(v, &p, 0); (void) strncpy(yyintsuffix, p, sizeof (yyintsuffix)); yyintdecimal = *v != '0'; if (errno == ERANGE) { xyerror(D_MACRO_OFLOW, "macro argument" " %s constant %s results in integer" " overflow\n", yytext, v); } return (DT_TOK_INT); } return (id_or_type(v)); } "$$"{RGX_IDENT} { dt_ident_t *idp = dt_idhash_lookup( yypcb->pcb_hdl->dt_macros, yytext + 2); char s[16]; /* enough for UINT_MAX + \0 */ if (idp == NULL) { xyerror(D_MACRO_UNDEF, "macro variable %s " "is not defined\n", yytext); } /* * For the moment, all current macro variables are of * type id_t (refer to dtrace_update() for details). */ (void) snprintf(s, sizeof (s), "%u", idp->di_id); if ((yylval.l_str = strdup(s)) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); return (DT_TOK_STRING); } "$"{RGX_IDENT} { dt_ident_t *idp = dt_idhash_lookup( yypcb->pcb_hdl->dt_macros, yytext + 1); if (idp == NULL) { xyerror(D_MACRO_UNDEF, "macro variable %s " "is not defined\n", yytext); } /* * For the moment, all current macro variables are of * type id_t (refer to dtrace_update() for details). */ yylval.l_int = (intmax_t)(int)idp->di_id; yyintprefix = 0; yyintsuffix[0] = '\0'; yyintdecimal = 1; return (DT_TOK_INT); } {RGX_IDENT} { return (id_or_type(yytext)); } {RGX_AGG} { if ((yylval.l_str = strdup(yytext)) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); return (DT_TOK_AGG); } "@" { if ((yylval.l_str = strdup("@_")) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); return (DT_TOK_AGG); } {RGX_INT} | {RGX_INT} | {RGX_INT} { char *p; errno = 0; yylval.l_int = strtoull(yytext, &p, 0); yyintprefix = 0; (void) strncpy(yyintsuffix, p, sizeof (yyintsuffix)); yyintdecimal = yytext[0] != '0'; if (errno == ERANGE) { xyerror(D_INT_OFLOW, "constant %s results in " "integer overflow\n", yytext); } if (*p != '\0' && strchr("uUlL", *p) == NULL) { xyerror(D_INT_DIGIT, "constant %s contains " "invalid digit %c\n", yytext, *p); } if ((YYSTATE) != S3) return (DT_TOK_INT); yypragma = dt_node_link(yypragma, dt_node_int(yylval.l_int)); } {RGX_FP} yyerror("floating-point constants are not permitted\n"); \"{RGX_STR}$ | \"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal"); \"{RGX_STR}\" | \"{RGX_STR}\" { /* * Quoted string -- convert C escape sequences and * return the string as a token. */ yylval.l_str = strndup(yytext + 1, yyleng - 2); if (yylval.l_str == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); (void) stresc2chr(yylval.l_str); if ((YYSTATE) != S3) return (DT_TOK_STRING); yypragma = dt_node_link(yypragma, dt_node_string(yylval.l_str)); } '{RGX_CHR}$ xyerror(D_CHR_NL, "newline encountered in character constant"); '{RGX_CHR}' { char *s, *p, *q; size_t nbytes; /* * Character constant -- convert C escape sequences and * return the character as an integer immediate value. */ if (yyleng == 2) xyerror(D_CHR_NULL, "empty character constant"); s = yytext + 1; yytext[yyleng - 1] = '\0'; nbytes = stresc2chr(s); yylval.l_int = 0; yyintprefix = 0; yyintsuffix[0] = '\0'; yyintdecimal = 1; if (nbytes > sizeof (yylval.l_int)) { xyerror(D_CHR_OFLOW, "character constant is " "too long"); } #ifdef _LITTLE_ENDIAN p = ((char *)&yylval.l_int) + nbytes - 1; for (q = s; nbytes != 0; nbytes--) *p-- = *q++; #else bcopy(s, ((char *)&yylval.l_int) + sizeof (yylval.l_int) - nbytes, nbytes); #endif return (DT_TOK_INT); } "/*" | "/*" { yypcb->pcb_cstate = (YYSTATE); BEGIN(S1); } {RGX_INTERP} | {RGX_INTERP} ; /* discard any #! lines */ {RGX_CTL} | {RGX_CTL} { assert(yypragma == NULL); yypcb->pcb_cstate = (YYSTATE); BEGIN(S3); } "/" { int c, tok; /* * The use of "/" as the predicate delimiter and as the * integer division symbol requires special lookahead * to avoid a shift/reduce conflict in the D grammar. * We look ahead to the next non-whitespace character. * If we encounter EOF, ";", "{", or "/", then this "/" * closes the predicate and we return DT_TOK_EPRED. * If we encounter anything else, it's DT_TOK_DIV. */ while ((c = input()) != 0) { if (strchr("\f\n\r\t\v ", c) == NULL) break; } if (c == 0 || c == ';' || c == '{' || c == '/') { if (yypcb->pcb_parens != 0) { yyerror("closing ) expected in " "predicate before /\n"); } if (yypcb->pcb_brackets != 0) { yyerror("closing ] expected in " "predicate before /\n"); } tok = DT_TOK_EPRED; } else tok = DT_TOK_DIV; unput(c); return (tok); } "(" { yypcb->pcb_parens++; return (DT_TOK_LPAR); } ")" { if (--yypcb->pcb_parens < 0) yyerror("extra ) in input stream\n"); return (DT_TOK_RPAR); } "[" { yypcb->pcb_brackets++; return (DT_TOK_LBRAC); } "]" { if (--yypcb->pcb_brackets < 0) yyerror("extra ] in input stream\n"); return (DT_TOK_RBRAC); } "{" | "{" { yypcb->pcb_braces++; return ('{'); } "}" { if (--yypcb->pcb_braces < 0) yyerror("extra } in input stream\n"); return ('}'); } "|" return (DT_TOK_BOR); "^" return (DT_TOK_XOR); "&" return (DT_TOK_BAND); "&&" return (DT_TOK_LAND); "^^" return (DT_TOK_LXOR); "||" return (DT_TOK_LOR); "==" return (DT_TOK_EQU); "!=" return (DT_TOK_NEQ); "<" return (DT_TOK_LT); "<=" return (DT_TOK_LE); ">" return (DT_TOK_GT); ">=" return (DT_TOK_GE); "<<" return (DT_TOK_LSH); ">>" return (DT_TOK_RSH); "+" return (DT_TOK_ADD); "-" return (DT_TOK_SUB); "*" return (DT_TOK_MUL); "%" return (DT_TOK_MOD); "~" return (DT_TOK_BNEG); "!" return (DT_TOK_LNEG); "?" return (DT_TOK_QUESTION); ":" return (DT_TOK_COLON); "." return (DT_TOK_DOT); "->" return (DT_TOK_PTR); "=" return (DT_TOK_ASGN); "+=" return (DT_TOK_ADD_EQ); "-=" return (DT_TOK_SUB_EQ); "*=" return (DT_TOK_MUL_EQ); "/=" return (DT_TOK_DIV_EQ); "%=" return (DT_TOK_MOD_EQ); "&=" return (DT_TOK_AND_EQ); "^=" return (DT_TOK_XOR_EQ); "|=" return (DT_TOK_OR_EQ); "<<=" return (DT_TOK_LSH_EQ); ">>=" return (DT_TOK_RSH_EQ); "++" return (DT_TOK_ADDADD); "--" return (DT_TOK_SUBSUB); "..." return (DT_TOK_ELLIPSIS); "," return (DT_TOK_COMMA); ";" return (';'); {RGX_WS} ; /* discard */ "\\"\n ; /* discard */ . yyerror("syntax error near \"%c\"\n", yytext[0]); "*/" BEGIN(yypcb->pcb_cstate); .|\n ; /* discard */ {RGX_PSPEC} { /* * S2 has an ambiguity because RGX_PSPEC includes '*' * as a glob character and '*' also can be DT_TOK_STAR. * Since lex always matches the longest token, this * rule can be matched by an input string like "int*", * which could begin a global variable declaration such * as "int*x;" or could begin a RGX_PSPEC with globbing * such as "int* { trace(timestamp); }". If C_PSPEC is * not set, we must resolve the ambiguity in favor of * the type and perform lexer pushback if the fragment * before '*' or entire fragment matches a type name. * If C_PSPEC is set, we always return a PSPEC token. * If C_PSPEC is off, the user can avoid ambiguity by * including a ':' delimiter in the specifier, which * they should be doing anyway to specify the provider. */ if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) && strchr(yytext, ':') == NULL) { char *p = strchr(yytext, '*'); char *q = yytext + yyleng - 1; if (p != NULL && p > yytext) *p = '\0'; /* prune yytext */ if (dt_type_lookup(yytext, NULL) == 0) { yylval.l_str = strdup(yytext); if (yylval.l_str == NULL) { longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); } if (p != NULL && p > yytext) { for (*p = '*'; q >= p; q--) unput(*q); } yybegin(YYS_EXPR); return (DT_TOK_TNAME); } if (p != NULL && p > yytext) *p = '*'; /* restore yytext */ } if ((yylval.l_str = strdup(yytext)) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); return (DT_TOK_PSPEC); } "/" return (DT_TOK_DIV); "," return (DT_TOK_COMMA); {RGX_WS} ; /* discard */ . yyerror("syntax error near \"%c\"\n", yytext[0]); \n { dt_pragma(yypragma); yypragma = NULL; BEGIN(yypcb->pcb_cstate); } [\f\t\v ]+ ; /* discard */ [^\f\n\t\v "]+ { dt_node_t *dnp; if ((yylval.l_str = strdup(yytext)) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); /* * We want to call dt_node_ident() here, but we can't * because it will expand inlined identifiers, which we * don't want to do from #pragma context in order to * support pragmas that apply to the ident itself. We * call dt_node_string() and then reset dn_op instead. */ dnp = dt_node_string(yylval.l_str); dnp->dn_kind = DT_NODE_IDENT; dnp->dn_op = DT_TOK_IDENT; yypragma = dt_node_link(yypragma, dnp); } . yyerror("syntax error near \"%c\"\n", yytext[0]); %% /* * yybegin provides a wrapper for use from C code around the lex BEGIN() macro. * We use two main states for lexing because probe descriptions use a syntax * that is incompatible with the normal D tokens (e.g. names can contain "-"). * yybegin also handles the job of switching between two lists of dt_nodes * as we allocate persistent definitions, like inlines, and transient nodes * that will be freed once we are done parsing the current program file. */ void yybegin(yystate_t state) { #ifdef YYDEBUG yydebug = _dtrace_debug; #endif if (yypcb->pcb_yystate == state) return; /* nothing to do if we're in the state already */ if (yypcb->pcb_yystate == YYS_DEFINE) { yypcb->pcb_list = yypcb->pcb_hold; yypcb->pcb_hold = NULL; } switch (state) { case YYS_CLAUSE: BEGIN(S2); break; case YYS_DEFINE: assert(yypcb->pcb_hold == NULL); yypcb->pcb_hold = yypcb->pcb_list; yypcb->pcb_list = NULL; /*FALLTHRU*/ case YYS_EXPR: BEGIN(S0); break; case YYS_DONE: break; default: xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state); } yypcb->pcb_yystate = state; } void yyinit(dt_pcb_t *pcb) { yypcb = pcb; yylineno = 1; yypragma = NULL; yysptr = yysbuf; } /* * Given a lexeme 's' (typically yytext), set yylval and return an appropriate * token to the parser indicating either an identifier or a typedef name. * User-defined global variables always take precedence over types, but we do * use some heuristics because D programs can look at an ever-changing set of * kernel types and also can implicitly instantiate variables by assignment, * unlike in C. The code here is ordered carefully as lookups are not cheap. */ static int id_or_type(const char *s) { dtrace_hdl_t *dtp = yypcb->pcb_hdl; int c0, c1, ttok = DT_TOK_TNAME; dt_ident_t *idp; if ((s = yylval.l_str = strdup(s)) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); /* * If the lexeme is a global variable or likely identifier or *not* a * type_name, then it is an identifier token. */ if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL || dt_idhash_lookup(yypcb->pcb_idents, s) != NULL || dt_type_lookup(s, NULL) != 0) return (DT_TOK_IDENT); /* * If the lexeme is a type name and we are not in a program clause, * then always interpret it as a type and return DT_TOK_TNAME. */ if ((YYSTATE) != S0) return (DT_TOK_TNAME); /* * If the lexeme matches a type name but is in a program clause, then * it could be a type or it could be an undefined variable. Peek at * the next token to decide. If we see ++, --, [, or =, we know there * might be an assignment that is trying to create a global variable, * so we optimistically return DT_TOK_IDENT. There is no harm in being * wrong: a type_name followed by ++, --, [, or = is a syntax error. */ while ((c0 = input()) != 0) { if (strchr("\f\n\r\t\v ", c0) == NULL) break; } switch (c0) { case '+': case '-': if ((c1 = input()) == c0) ttok = DT_TOK_IDENT; unput(c1); break; case '=': if ((c1 = input()) != c0) ttok = DT_TOK_IDENT; unput(c1); break; case '[': ttok = DT_TOK_IDENT; break; } if (ttok == DT_TOK_IDENT) { idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0, 0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen); if (idp == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); } unput(c0); return (ttok); } static int input(void) { int c; if (yysptr > yysbuf) c = *--yysptr; else if (yypcb->pcb_fileptr != NULL) c = fgetc(yypcb->pcb_fileptr); else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen) c = *yypcb->pcb_strptr++; else c = EOF; if (c == '\n') yylineno++; if (c != EOF) return (c); if ((YYSTATE) == S1) yyerror("end-of-file encountered before matching */\n"); if ((YYSTATE) == S3) yyerror("end-of-file encountered before end of control line\n"); if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr)) longjmp(yypcb->pcb_jmpbuf, EDT_FIO); return (0); /* EOF */ } static void unput(int c) { if (c == '\n') yylineno--; *yysptr++ = c; yytchar = c; }