@@ -697,10 +697,42 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
697697 wasm_exec_env_set_cur_frame (exec_env , prev_frame );
698698}
699699
700+ #if WASM_ENABLE_OPCODE_COUNTER != 0
701+ typedef struct OpcodeInfo {
702+ char * name ;
703+ uint64 count ;
704+ } OpcodeInfo ;
705+
706+ #define HANDLE_OPCODE (op ) { #op, 0 }
707+ DEFINE_GOTO_TABLE (OpcodeInfo , opcode_table );
708+ #undef HANDLE_OPCODE
709+
710+ static void
711+ wasm_interp_dump_op_count ()
712+ {
713+ uint32 i ;
714+ uint64 total_count = 0 ;
715+ for (i = 0 ; i < WASM_OP_IMPDEP ; i ++ )
716+ total_count += opcode_table [i ].count ;
717+
718+ printf ("total opcode count: %ld\n" , total_count );
719+ for (i = 0 ; i < WASM_OP_IMPDEP ; i ++ )
720+ if (opcode_table [i ].count > 0 )
721+ printf ("\t\t%s count:\t\t%ld,\t\t%.2f%%\n" ,
722+ opcode_table [i ].name , opcode_table [i ].count ,
723+ opcode_table [i ].count * 100.0f / total_count );
724+ }
725+ #endif
726+
727+
700728#if WASM_ENABLE_LABELS_AS_VALUES != 0
701729
702730//#define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n");h_##opcode
731+ #if WASM_ENABLE_OPCODE_COUNTER != 0
732+ #define HANDLE_OP (opcode ) HANDLE_##opcode:opcode_table[opcode].count++;h_##opcode
733+ #else
703734#define HANDLE_OP (opcode ) HANDLE_##opcode
735+ #endif
704736#if WASM_ENABLE_FAST_INTERP == 0
705737#define FETCH_OPCODE_AND_DISPATCH () goto *handle_table[*frame_ip++]
706738#else
@@ -765,7 +797,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
765797
766798#if WASM_ENABLE_LABELS_AS_VALUES != 0
767799 #define HANDLE_OPCODE (op ) &&HANDLE_##op
768- DEFINE_GOTO_TABLE (handle_table );
800+ DEFINE_GOTO_TABLE (const void * , handle_table );
769801 #undef HANDLE_OPCODE
770802#if WASM_ENABLE_FAST_INTERP != 0
771803 if (exec_env == NULL ) {
@@ -2287,4 +2319,7 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst,
22872319
22882320 wasm_exec_env_set_cur_frame (exec_env , prev_frame );
22892321 FREE_FRAME (exec_env , frame );
2322+ #if WASM_ENABLE_OPCODE_COUNTER != 0
2323+ wasm_interp_dump_op_count ();
2324+ #endif
22902325}
0 commit comments