From 1fb385af6e036588a0b807aa51d74dcf34f841cd Mon Sep 17 00:00:00 2001 From: Mengchi Zhang Date: Fri, 17 May 2019 10:59:27 -0400 Subject: Move ptx parser to reentrant Signed-off-by: Mengchi Zhang --- src/cuda-sim/ptx_parser.cc | 168 ++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 80 deletions(-) (limited to 'src/cuda-sim/ptx_parser.cc') diff --git a/src/cuda-sim/ptx_parser.cc b/src/cuda-sim/ptx_parser.cc index 25758dd..a1f6e00 100644 --- a/src/cuda-sim/ptx_parser.cc +++ b/src/cuda-sim/ptx_parser.cc @@ -27,13 +27,17 @@ #include "ptx_parser.h" #include "ptx_ir.h" + +typedef void * yyscan_t; #include "ptx.tab.h" #include -extern int ptx_error( const char *s ); -extern int ptx_lineno; -extern int ptx_parse(); -extern FILE *ptx_in; +extern int ptx_get_lineno (yyscan_t yyscanner ); +extern int ptx_error( yyscan_t yyscanner, const char *s ); +extern int ptx_lex_init(yyscan_t* scanner); +extern void ptx_set_in(FILE * _in_str ,yyscan_t yyscanner ); +extern int ptx_parse(yyscan_t scanner, ptx_recognizer* recognizer); +extern int ptx_lex_destroy(yyscan_t scanner); static const struct core_config *g_shader_core_config; void set_ptx_warp_size(const struct core_config * warp_size) @@ -80,7 +84,7 @@ std::list g_scalar_type; #define PTX_PARSE_DPRINTF(...) \ if( g_debug_ir_generation ) { \ - printf(" %s:%u => ",g_filename,ptx_lineno); \ + printf(" %s:%u => ",g_filename,ptx_get_lineno(scanner)); \ printf(" (%s:%u) ", __FILE__, __LINE__); \ printf(__VA_ARGS__); \ printf("\n"); \ @@ -109,7 +113,7 @@ void read_parser_environment_variables() } } -void init_directive_state() +void ptx_recognizer::init_directive_state() { PTX_PARSE_DPRINTF("init_directive_state"); g_space_spec=undefined_space; @@ -125,7 +129,7 @@ void init_directive_state() g_last_symbol = NULL; } -void init_instruction_state() +void ptx_recognizer::init_instruction_state() { PTX_PARSE_DPRINTF("init_instruction_state"); g_pred = NULL; @@ -149,7 +153,6 @@ symbol_table *init_parser( const char *ptx_filename ) /*else { g_global_symbol_table = g_current_symbol_table = new symbol_table("global",0,g_global_allfiles_symbol_table); }*/ - ptx_lineno = 1; #define DEF(X,Y) g_ptx_token_decode[X] = Y; #include "ptx_parser_decode.def" @@ -169,18 +172,23 @@ symbol_table *init_parser( const char *ptx_filename ) g_ptx_token_decode[generic_space] = "generic_space"; g_ptx_token_decode[instruction_space] = "instruction_space"; - init_directive_state(); - init_instruction_state(); + ptx_recognizer recognizer; + ptx_lex_init(&(recognizer.scanner)); + recognizer.init_directive_state(); + recognizer.init_instruction_state(); + FILE *ptx_in; ptx_in = fopen(ptx_filename, "r"); - ptx_parse(); + ptx_set_in(ptx_in, recognizer.scanner); + ptx_parse(recognizer.scanner, &recognizer); + ptx_lex_destroy(recognizer.scanner); fclose(ptx_in); return g_global_symbol_table; } static int g_entry_point; -void start_function( int entry_point ) +void ptx_recognizer::start_function( int entry_point ) { PTX_PARSE_DPRINTF("start_function"); init_directive_state(); @@ -194,7 +202,7 @@ char *g_add_identifier_cached__identifier = NULL; int g_add_identifier_cached__array_dim; int g_add_identifier_cached__array_ident; -void add_function_name( const char *name ) +void ptx_recognizer::add_function_name( const char *name ) { PTX_PARSE_DPRINTF("add_function_name %s %s", name, ((g_entry_point==1)?"(entrypoint)":((g_entry_point==2)?"(extern)":""))); bool prior_decl = g_global_symbol_table->add_function_decl( name, g_entry_point, &g_func_info, &g_current_symbol_table ); @@ -210,21 +218,21 @@ void add_function_name( const char *name ) if( prior_decl ) { g_func_info->remove_args(); } - g_global_symbol_table->add_function( g_func_info, g_filename, ptx_lineno ); + g_global_symbol_table->add_function( g_func_info, g_filename, ptx_get_lineno(scanner) ); } //Jin: handle instruction group for cdp -void start_inst_group() { +void ptx_recognizer::start_inst_group() { PTX_PARSE_DPRINTF("start_instruction_group"); g_current_symbol_table = g_current_symbol_table->start_inst_group(); } -void end_inst_group() { +void ptx_recognizer::end_inst_group() { PTX_PARSE_DPRINTF("end_instruction_group"); g_current_symbol_table = g_current_symbol_table->end_inst_group(); } -void add_directive() +void ptx_recognizer::add_directive() { PTX_PARSE_DPRINTF("add_directive"); init_directive_state(); @@ -232,7 +240,7 @@ void add_directive() #define mymax(a,b) ((a)>(b)?(a):(b)) -void end_function() +void ptx_recognizer::end_function() { PTX_PARSE_DPRINTF("end_function"); @@ -250,7 +258,7 @@ void end_function() #define parse_error(msg, ...) parse_error_impl(__FILE__,__LINE__, msg, ##__VA_ARGS__) #define parse_assert(cond,msg, ...) parse_assert_impl((cond),__FILE__,__LINE__, msg, ##__VA_ARGS__) -void parse_error_impl( const char *file, unsigned line, const char *msg, ... ) +void ptx_recognizer::parse_error_impl( const char *file, unsigned line, const char *msg, ... ) { va_list ap; char buf[1024]; @@ -259,13 +267,13 @@ void parse_error_impl( const char *file, unsigned line, const char *msg, ... ) va_end(ap); g_error_detected = 1; - printf("%s:%u: Parse error: %s (%s:%u)\n\n", g_filename, ptx_lineno, buf, file, line); - ptx_error(NULL); + printf("%s:%u: Parse error: %s (%s:%u)\n\n", g_filename, ptx_get_lineno(scanner), buf, file, line); + ptx_error(scanner, NULL); abort(); exit(1); } -void parse_assert_impl( int test_value, const char *file, unsigned line, const char *msg, ... ) +void ptx_recognizer::parse_assert_impl( int test_value, const char *file, unsigned line, const char *msg, ... ) { va_list ap; char buf[1024]; @@ -280,7 +288,7 @@ void parse_assert_impl( int test_value, const char *file, unsigned line, const c extern char linebuf[4096]; -void set_return() +void ptx_recognizer::set_return() { parse_assert( (g_opcode == CALL_OP || g_opcode == CALLP_OP), "only call can have return value"); g_operands.front().set_return(); @@ -300,7 +308,7 @@ const ptx_instruction *ptx_instruction_lookup( const char *filename, unsigned li return l->second; } -void add_instruction() +void ptx_recognizer::add_instruction() { PTX_PARSE_DPRINTF("add_instruction: %s", ((g_opcode>0)?g_opcode_string[g_opcode]:"