From a7ae218e89859b5a5b8433f57029590b8877fc18 Mon Sep 17 00:00:00 2001 From: Mengchi Zhang Date: Sun, 21 Apr 2019 15:55:21 -0400 Subject: Move cuobjdump parser to reentrant Signed-off-by: Mengchi Zhang --- libcuda/cuda_runtime_api.cc | 15 ++++++++++--- libcuda/cuobjdump.l | 52 ++++++++++++++++++++++----------------------- libcuda/cuobjdump.y | 8 +++++-- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/libcuda/cuda_runtime_api.cc b/libcuda/cuda_runtime_api.cc index 27644b3..ef5abce 100644 --- a/libcuda/cuda_runtime_api.cc +++ b/libcuda/cuda_runtime_api.cc @@ -1935,7 +1935,10 @@ void setCuobjdumpsassfilename(const char* filename){ } (dynamic_cast(cuobjdumpSectionList.front()))->setSASSfilename(filename); } -extern int cuobjdump_parse(); +typedef void * yyscan_t; +extern int cuobjdump_lex_init(yyscan_t* scanner); +extern int cuobjdump_parse(yyscan_t scanner); +extern int cuobjdump_lex_destroy(yyscan_t scanner); extern FILE *cuobjdump_in; //! Return the executable file of the process containing the PTX/SASS code @@ -2147,7 +2150,10 @@ void extract_code_using_cuobjdump(){ printf("Parsing file %s\n", fname); cuobjdump_in = fopen(fname, "r"); - cuobjdump_parse(); + yyscan_t scanner; + cuobjdump_lex_init(&scanner); + cuobjdump_parse(scanner); + cuobjdump_lex_destroy(scanner); fclose(cuobjdump_in); printf("Done parsing!!!\n"); } else { @@ -2196,7 +2202,10 @@ void extract_code_using_cuobjdump(){ std::cout << "Trying to parse " << libcodfn.str() << std::endl; cuobjdump_in = fopen(libcodfn.str().c_str(), "r"); - cuobjdump_parse(); + yyscan_t scanner; + cuobjdump_lex_init(&scanner); + cuobjdump_parse(scanner); + cuobjdump_lex_destroy(scanner); fclose(cuobjdump_in); std::getline(libsf, line); } diff --git a/libcuda/cuobjdump.l b/libcuda/cuobjdump.l index 2b0dac8..9a468ba 100644 --- a/libcuda/cuobjdump.l +++ b/libcuda/cuobjdump.l @@ -38,9 +38,7 @@ #define YYDEBUG 1 -#define yylval cuobjdump_lval - -void cuobjdump_error(const char*); +void print_error(const char*, const char*, int); %} %option stack @@ -48,6 +46,8 @@ void cuobjdump_error(const char*); %option yylineno %option nounput +%option bison-bridge +%option reentrant %s ptxcode %s sasscode @@ -69,54 +69,54 @@ newlines {newline}+ "ptxasOptions"{notnewline}*{newline} -[1-9]{numeric}* yylval.string_value = strdup(yytext); return DECIMAL; +[1-9]{numeric}* yylval->string_value = strdup(yytext); return DECIMAL; "has debug info"{newline} "Fatbin ptx code:"{newline} { - yy_push_state(ptxcode); - yy_push_state(identifier); - yy_push_state(ptxheader); - yylval.string_value = strdup(yytext); + yy_push_state(ptxcode, yyscanner); + yy_push_state(identifier, yyscanner); + yy_push_state(ptxheader, yyscanner); + yylval->string_value = strdup(yytext); return PTXHEADER; } "Fatbin elf code:"{newline} { - yy_push_state(elfcode); - yy_push_state(identifier); - yy_push_state(elfheader); - yylval.string_value = strdup(yytext); + yy_push_state(elfcode, yyscanner); + yy_push_state(identifier, yyscanner); + yy_push_state(elfheader, yyscanner); + yylval->string_value = strdup(yytext); return ELFHEADER; } /*PTX code tokens*/ -{notnewline}*{newline} yylval.string_value = strdup(yytext); return PTXLINE; +{notnewline}*{newline} yylval->string_value = strdup(yytext); return PTXLINE; /*ELF code tokens*/ {whitespace}*"code for sm_"{numeric}+{newline} { BEGIN(sasscode); - yylval.string_value = strdup(yytext); + yylval->string_value = strdup(yytext); return SASSLINE; } -{notnewline}*{newline} yylval.string_value = strdup(yytext); return ELFLINE; +{notnewline}*{newline} yylval->string_value = strdup(yytext); return ELFLINE; /*SASS code tokens*/ -{notnewline}*{newline} yylval.string_value = strdup(yytext); return SASSLINE; +{notnewline}*{newline} yylval->string_value = strdup(yytext); return SASSLINE; {newline}"compressed"{newline} BEGIN(conidentifier); return H_COMPRESSED; {newline}"identifier = " BEGIN(endidentifier); return H_IDENTIFIER; -{newline}{newline} yy_pop_state(); +{newline}{newline} yy_pop_state(yyscanner); "identifier = " BEGIN(endidentifier); return H_IDENTIFIER; -{newline} yy_pop_state(); +{newline} yy_pop_state(yyscanner); -{notnewline}+ yylval.string_value = strdup(yytext); yy_pop_state(); return FILENAME; +{notnewline}+ yylval->string_value = strdup(yytext); yy_pop_state(yyscanner); return FILENAME; /*Header tokens*/ -[[:alnum:]_]+ yylval.string_value = strdup(yytext); return IDENTIFIER; +[[:alnum:]_]+ yylval->string_value = strdup(yytext); return IDENTIFIER; "================" return H_SEPARATOR; "arch = " return H_ARCH; "code version = " return H_CODEVERSION; @@ -128,7 +128,7 @@ newlines {newline}+ /*Header tokens*/ -[[:alnum:]_]+ yylval.string_value = strdup(yytext); return IDENTIFIER; +[[:alnum:]_]+ yylval->string_value = strdup(yytext); return IDENTIFIER; "================" return H_SEPARATOR; "arch = " return H_ARCH; "code version = " return H_CODEVERSION; @@ -143,7 +143,7 @@ newlines {newline}+ /* Looking for the identifier (filename) then the header is done */ -{notnewline}+ yylval.string_value = strdup(yytext); yy_pop_state(); return IDENTIFIER; +{notnewline}+ yylval->string_value = strdup(yytext); yy_pop_state(yyscanner); return IDENTIFIER; @@ -153,11 +153,11 @@ newlines {newline}+ <> BEGIN(INITIAL);return 0; /*No other rule matched. Throw an error*/ -. cuobjdump_error("Invalid token"); +. print_error("Invalid token", yytext, yylineno); %% -void cuobjdump_error(const char* message) +void print_error(const char* message, const char* text, int lineno) { - printf(" %s near \"%s\"",message, yytext); - printf(" on line %i\n",yylineno); + printf(" %s near \"%s\"",message, text); + printf(" on line %i\n",lineno); } diff --git a/libcuda/cuobjdump.y b/libcuda/cuobjdump.y index 31760f7..eea0a1f 100644 --- a/libcuda/cuobjdump.y +++ b/libcuda/cuobjdump.y @@ -29,8 +29,8 @@ %{ #include -int yylex(void); -void yyerror(const char*); +typedef void * yyscan_t; + extern void addCuobjdumpSection(int sectiontype); void setCuobjdumparch(const char* arch); void setCuobjdumpidentifier(const char* identifier); @@ -44,6 +44,10 @@ FILE *elffile; FILE *sassfile; char filename [1024]; %} +%define api.pure full +%parse-param {yyscan_t scanner} +%lex-param {yyscan_t scanner} + %union { char* string_value; } -- cgit v1.3