From b26dda933fe84d54cad47762ae80be3acdfda91b Mon Sep 17 00:00:00 2001 From: Tor Aamodt Date: Sun, 8 Aug 2010 14:28:20 -0800 Subject: refactoring: moving PTX parsing functions into ptx_parser.* [git-p4: depot-paths = "//depot/gpgpu_sim_research/fermi/distribution/": change = 7167] --- src/cuda-sim/ptx_ir.cc | 675 +------------------------------------------------ 1 file changed, 7 insertions(+), 668 deletions(-) (limited to 'src/cuda-sim/ptx_ir.cc') diff --git a/src/cuda-sim/ptx_ir.cc b/src/cuda-sim/ptx_ir.cc index 51ad05f..cd70daf 100644 --- a/src/cuda-sim/ptx_ir.cc +++ b/src/cuda-sim/ptx_ir.cc @@ -61,6 +61,7 @@ * Vancouver, BC V6T 1Z4 */ +#include "ptx_parser.h" #include "ptx_ir.h" #include "ptx.tab.h" #include "opcodes.h" @@ -69,612 +70,8 @@ #include #include #include -#include #include "cuda-sim.h" -#include "../option_parser.h" - -extern "C" int ptx_error( const char *s ); -extern int ptx_lineno; - -// the program intermediate representation... -symbol_table *g_global_symbol_table = NULL; -std::map g_sym_name_to_symbol_table; -static symbol_table *g_current_symbol_table = NULL; -static std::list g_instructions; -static symbol *g_last_symbol = NULL; - -int g_error_detected = 0; - -// type specifier stuff: -memory_space_t g_space_spec = undefined_space; -int g_scalar_type_spec = -1; -int g_vector_spec = -1; -int g_alignment_spec = -1; -int g_extern_spec = 0; - -// variable declaration stuff: -type_info *g_var_type = NULL; - -// instruction definition stuff: -const symbol *g_pred; -int g_neg_pred; -symbol *g_label; -int g_opcode = -1; -std::list g_operands; -std::list g_options; -std::list g_scalar_type; - -#define DPRINTF(...) \ - if( g_debug_ir_generation ) { \ - printf(" %s:%u => ",g_filename,ptx_lineno); \ - printf(" (%s:%u) ", __FILE__, __LINE__); \ - printf(__VA_ARGS__); \ - printf("\n"); \ - fflush(stdout); \ - } - -unsigned g_entry_func_param_index=0; -function_info *g_func_info = NULL; -function_info *g_entrypoint_func_info = NULL; -symbol_table *g_entrypoint_symbol_table = NULL; -std::map g_ptx_token_decode; -operand_info g_return_var; - -void init_parser() -{ - g_global_symbol_table = g_current_symbol_table = new symbol_table("global",0,NULL); - ptx_lineno = 1; - -#define DEF(X,Y) g_ptx_token_decode[X] = Y; -#include "ptx_parser_decode.def" -#undef DEF -} - -void init_directive_state() -{ - DPRINTF("init_directive_state"); - g_space_spec=undefined_space; - g_scalar_type_spec=-1; - g_vector_spec=-1; - g_opcode=-1; - g_alignment_spec = -1; - g_extern_spec = 0; - g_scalar_type.clear(); - g_operands.clear(); - g_last_symbol = NULL; -} - -void init_instruction_state() -{ - DPRINTF("init_instruction_state"); - g_pred = NULL; - g_neg_pred = 0; - g_label = NULL; - g_opcode = -1; - g_options.clear(); - g_return_var = operand_info(); - init_directive_state(); -} - -extern void register_ptx_function( const char *name, function_info *impl, symbol_table *symtab ); - -static int g_entry_point; - -void start_function( int entry_point ) -{ - DPRINTF("start_function"); - init_directive_state(); - init_instruction_state(); - g_entry_point = entry_point; - g_func_info = NULL; - g_entry_func_param_index=0; -} - -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 ) -{ - 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 ); - if( g_entry_point ) { - g_entrypoint_func_info = g_func_info; - g_entrypoint_symbol_table = g_current_symbol_table; - } - if( g_add_identifier_cached__identifier ) { - add_identifier( g_add_identifier_cached__identifier, - g_add_identifier_cached__array_dim, - g_add_identifier_cached__array_ident ); - free( g_add_identifier_cached__identifier ); - g_add_identifier_cached__identifier = NULL; - g_func_info->add_return_var( g_last_symbol ); - init_directive_state(); - } - if( prior_decl ) { - g_func_info->remove_args(); - } - g_global_symbol_table->add_function( g_func_info ); -} - -void add_directive() -{ - DPRINTF("add_directive"); - init_directive_state(); -} - -#define mymax(a,b) ((a)>(b)?(a):(b)) - -void end_function() -{ - DPRINTF("end_function"); - - init_directive_state(); - init_instruction_state(); - g_max_regs_per_thread = mymax( g_max_regs_per_thread, (g_current_symbol_table->next_reg_num()-1)); - g_func_info->add_inst( g_instructions ); - g_instructions.clear(); - gpgpu_ptx_assemble( g_func_info->get_name(), g_func_info ); - g_current_symbol_table = g_global_symbol_table; - - DPRINTF("function %s, PC = %d\n", g_func_info->get_name().c_str(), g_func_info->get_start_PC()); -} - -#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, ... ) -{ - va_list ap; - char buf[1024]; - va_start(ap,msg); - vsnprintf(buf,1024,msg,ap); - 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); - abort(); - exit(1); -} - -void parse_assert_impl( int test_value, const char *file, unsigned line, const char *msg, ... ) -{ - va_list ap; - char buf[1024]; - va_start(ap,msg); - vsnprintf(buf,1024,msg,ap); - va_end(ap); - - if ( test_value == 0 ) - parse_error_impl(file,line, msg); -} - -extern "C" char linebuf[1024]; - - -void set_return() -{ - parse_assert( (g_opcode == CALL_OP), "only call can have return value"); - g_operands.front().set_return(); - g_return_var = g_operands.front(); -} - -std::map > g_inst_lookup; - -const ptx_instruction *ptx_instruction_lookup( const char *filename, unsigned linenumber ) -{ - std::map >::iterator f=g_inst_lookup.find(filename); - if( f == g_inst_lookup.end() ) - return NULL; - std::map::iterator l=f->second.find(linenumber); - if( l == f->second.end() ) - return NULL; - return l->second; -} - -void add_instruction() -{ - DPRINTF("add_instruction: %s", ((g_opcode>0)?g_opcode_string[g_opcode]:"