static void interp_mem_stats_opcode_enter (const vm_instr_t *instrs_p, vm_instr_counter_t instr_position, mem_heap_stats_t *out_heap_stats_p, mem_pools_stats_t *out_pools_stats_p) { if (likely (!interp_mem_stats_enabled)) { return; } const uint32_t indentation = JERRY_MIN (interp_mem_stats_print_indentation, INTERP_MEM_PRINT_INDENTATION_MAX); char indent_prefix[INTERP_MEM_PRINT_INDENTATION_MAX + 2]; memset (indent_prefix, ' ', sizeof (indent_prefix)); indent_prefix[indentation] = '|'; indent_prefix[indentation + 1] = '\0'; interp_mem_get_stats (out_heap_stats_p, out_pools_stats_p, true, false); vm_instr_t instr = vm_get_instr (instrs_p, instr_position); printf ("%s-- Opcode: %s (position %u) --\n", indent_prefix, __op_names[instr.op_idx], (uint32_t) instr_position); interp_mem_stats_print_indentation += INTERP_MEM_PRINT_INDENTATION_STEP; }
/** * Fill parameters' list */ void fill_params_list (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */ ecma_length_t params_number, /**< number of parameters */ ecma_string_t* params_names[]) /**< out: parameters' names */ { uint32_t param_index; for (param_index = 0; param_index < params_number; param_index++) { vm_instr_t next_instr = vm_get_instr (frame_ctx_p->instrs_p, frame_ctx_p->pos); JERRY_ASSERT (next_instr.op_idx == VM_OP_META); JERRY_ASSERT (next_instr.data.meta.type == OPCODE_META_TYPE_VARG); const lit_cpointer_t param_name_lit_idx = serializer_get_literal_cp_by_uid (next_instr.data.meta.data_1, frame_ctx_p->instrs_p, frame_ctx_p->pos); params_names[param_index] = ecma_new_ecma_string_from_lit_cp (param_name_lit_idx); frame_ctx_p->pos++; } JERRY_ASSERT (param_index == params_number); } /* fill_params_list */
/** * Get scope code flags from instruction at specified position * * @return mask of scope code flags */ opcode_scope_code_flags_t vm_get_scope_flags (const vm_instr_t *instrs_p, /**< byte-code array */ vm_instr_counter_t counter) /**< instruction counter */ { vm_instr_t flags_instr = vm_get_instr (instrs_p, counter); JERRY_ASSERT (flags_instr.op_idx == VM_OP_META && flags_instr.data.meta.type == OPCODE_META_TYPE_SCOPE_CODE_FLAGS); return (opcode_scope_code_flags_t) flags_instr.data.meta.data_1; } /* vm_get_scope_flags */
/** * Fill arguments' list * * @return empty completion value if argument list was filled successfully, * otherwise - not normal completion value indicating completion type * of last expression evaluated */ ecma_completion_value_t fill_varg_list (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */ ecma_length_t args_number, /**< number of arguments */ ecma_value_t arg_values[], /**< out: arguments' values */ ecma_length_t *out_arg_number_p) /**< out: number of arguments successfully read */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); ecma_length_t arg_index; for (arg_index = 0; arg_index < args_number && ecma_is_completion_value_empty (ret_value); ) { ecma_completion_value_t evaluate_arg_completion = vm_loop (frame_ctx_p, NULL); if (ecma_is_completion_value_empty (evaluate_arg_completion)) { vm_instr_t next_instr = vm_get_instr (frame_ctx_p->instrs_p, frame_ctx_p->pos); JERRY_ASSERT (next_instr.op_idx == VM_OP_META); JERRY_ASSERT (next_instr.data.meta.type == OPCODE_META_TYPE_VARG); const idx_t varg_var_idx = next_instr.data.meta.data_1; ecma_completion_value_t get_arg_completion = get_variable_value (frame_ctx_p, varg_var_idx, false); if (ecma_is_completion_value_normal (get_arg_completion)) { arg_values[arg_index++] = ecma_get_completion_value_value (get_arg_completion); } else { JERRY_ASSERT (ecma_is_completion_value_throw (get_arg_completion)); ret_value = get_arg_completion; } } else { JERRY_ASSERT (ecma_is_completion_value_throw (evaluate_arg_completion)); ret_value = evaluate_arg_completion; } frame_ctx_p->pos++; } *out_arg_number_p = arg_index; return ret_value; } /* fill_varg_list */
/** * 'Try' opcode handler. * * See also: ECMA-262 v5, 12.14 * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t opfunc_try_block (vm_instr_t instr, /**< instruction */ vm_frame_ctx_t *frame_ctx_p) /**< interpreter context */ { const vm_idx_t block_end_oc_idx_1 = instr.data.try_block.oc_idx_1; const vm_idx_t block_end_oc_idx_2 = instr.data.try_block.oc_idx_2; const vm_instr_counter_t try_end_oc = (vm_instr_counter_t) ( vm_calc_instr_counter_from_idx_idx (block_end_oc_idx_1, block_end_oc_idx_2) + frame_ctx_p->pos); frame_ctx_p->pos++; vm_run_scope_t run_scope_try = { frame_ctx_p->pos, try_end_oc }; ecma_completion_value_t try_completion = vm_loop (frame_ctx_p, &run_scope_try); JERRY_ASSERT ((!ecma_is_completion_value_empty (try_completion) && frame_ctx_p->pos <= try_end_oc) || (ecma_is_completion_value_empty (try_completion) && frame_ctx_p->pos == try_end_oc)); frame_ctx_p->pos = try_end_oc; vm_instr_t next_instr = vm_get_instr (frame_ctx_p->bytecode_header_p->instrs_p, frame_ctx_p->pos); JERRY_ASSERT (next_instr.op_idx == VM_OP_META); if (next_instr.data.meta.type == OPCODE_META_TYPE_CATCH) { const vm_instr_counter_t catch_end_oc = (vm_instr_counter_t) ( vm_read_instr_counter_from_meta (OPCODE_META_TYPE_CATCH, frame_ctx_p) + frame_ctx_p->pos); frame_ctx_p->pos++; if (ecma_is_completion_value_throw (try_completion)) { next_instr = vm_get_instr (frame_ctx_p->bytecode_header_p->instrs_p, frame_ctx_p->pos); JERRY_ASSERT (next_instr.op_idx == VM_OP_META); JERRY_ASSERT (next_instr.data.meta.type == OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER); lit_cpointer_t catch_exc_val_var_name_lit_cp = serializer_get_literal_cp_by_uid (next_instr.data.meta.data_1, frame_ctx_p->bytecode_header_p, frame_ctx_p->pos); frame_ctx_p->pos++; ecma_string_t *catch_exc_var_name_str_p = ecma_new_ecma_string_from_lit_cp (catch_exc_val_var_name_lit_cp); ecma_object_t *old_env_p = frame_ctx_p->lex_env_p; ecma_object_t *catch_env_p = ecma_create_decl_lex_env (old_env_p); ecma_completion_value_t completion = ecma_op_create_mutable_binding (catch_env_p, catch_exc_var_name_str_p, false); JERRY_ASSERT (ecma_is_completion_value_empty (completion)); completion = ecma_op_set_mutable_binding (catch_env_p, catch_exc_var_name_str_p, ecma_get_completion_value_value (try_completion), false); JERRY_ASSERT (ecma_is_completion_value_empty (completion)); ecma_deref_ecma_string (catch_exc_var_name_str_p); frame_ctx_p->lex_env_p = catch_env_p; ecma_free_completion_value (try_completion); vm_run_scope_t run_scope_catch = { frame_ctx_p->pos, catch_end_oc }; try_completion = vm_loop (frame_ctx_p, &run_scope_catch); frame_ctx_p->lex_env_p = old_env_p; ecma_deref_object (catch_env_p); JERRY_ASSERT ((!ecma_is_completion_value_empty (try_completion) && frame_ctx_p->pos <= catch_end_oc) || (ecma_is_completion_value_empty (try_completion) && frame_ctx_p->pos == catch_end_oc)); } frame_ctx_p->pos = catch_end_oc; } next_instr = vm_get_instr (frame_ctx_p->bytecode_header_p->instrs_p, frame_ctx_p->pos); JERRY_ASSERT (next_instr.op_idx == VM_OP_META); if (next_instr.data.meta.type == OPCODE_META_TYPE_FINALLY) { const vm_instr_counter_t finally_end_oc = (vm_instr_counter_t) ( vm_read_instr_counter_from_meta (OPCODE_META_TYPE_FINALLY, frame_ctx_p) + frame_ctx_p->pos); frame_ctx_p->pos++; vm_run_scope_t run_scope_finally = { frame_ctx_p->pos, finally_end_oc }; ecma_completion_value_t finally_completion = vm_loop (frame_ctx_p, &run_scope_finally); JERRY_ASSERT ((!ecma_is_completion_value_empty (finally_completion) && frame_ctx_p->pos <= finally_end_oc) || (ecma_is_completion_value_empty (finally_completion) && frame_ctx_p->pos == finally_end_oc)); frame_ctx_p->pos = finally_end_oc; if (!ecma_is_completion_value_empty (finally_completion)) { ecma_free_completion_value (try_completion); try_completion = finally_completion; } } next_instr = vm_get_instr (frame_ctx_p->bytecode_header_p->instrs_p, frame_ctx_p->pos++); JERRY_ASSERT (next_instr.op_idx == VM_OP_META); JERRY_ASSERT (next_instr.data.meta.type == OPCODE_META_TYPE_END_TRY_CATCH_FINALLY); return try_completion; } /* opfunc_try_block */
/** * 'for-in' opcode handler * * See also: * ECMA-262 v5, 12.6.4 * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t opfunc_for_in (vm_instr_t instr, /**< instruction */ vm_frame_ctx_t *int_data_p) /**< interpreter context */ { const idx_t expr_idx = instr.data.for_in.expr; const idx_t block_end_oc_idx_1 = instr.data.for_in.oc_idx_1; const idx_t block_end_oc_idx_2 = instr.data.for_in.oc_idx_2; const vm_instr_counter_t for_in_end_oc = (vm_instr_counter_t) ( vm_calc_instr_counter_from_idx_idx (block_end_oc_idx_1, block_end_oc_idx_2) + int_data_p->pos); ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); /* 1., 2. */ ECMA_TRY_CATCH (expr_value, get_variable_value (int_data_p, expr_idx, false), ret_value); int_data_p->pos++; vm_instr_t meta_instr = vm_get_instr (int_data_p->instrs_p, for_in_end_oc); JERRY_ASSERT (meta_instr.op_idx == VM_OP_META); JERRY_ASSERT (meta_instr.data.meta.type == OPCODE_META_TYPE_END_FOR_IN); /* 3. */ if (!ecma_is_value_undefined (expr_value) && !ecma_is_value_null (expr_value)) { /* 4. */ ECMA_TRY_CATCH (obj_expr_value, ecma_op_to_object (expr_value), ret_value); ecma_object_t *obj_p = ecma_get_object_from_value (obj_expr_value); ecma_collection_iterator_t names_iterator; ecma_collection_header_t *names_p = vm_helper_for_in_enumerate_properties_names (obj_p); if (names_p != NULL) { ecma_collection_iterator_init (&names_iterator, names_p); const vm_instr_counter_t for_in_body_begin_oc = int_data_p->pos; const vm_instr_counter_t for_in_body_end_oc = for_in_end_oc; while (ecma_collection_iterator_next (&names_iterator)) { ecma_value_t name_value = *names_iterator.current_value_p; ecma_string_t *name_p = ecma_get_string_from_value (name_value); if (ecma_op_object_get_property (obj_p, name_p) != NULL) { ecma_completion_value_t completion = set_variable_value (int_data_p, int_data_p->pos, OPCODE_REG_SPECIAL_FOR_IN_PROPERTY_NAME, name_value); JERRY_ASSERT (ecma_is_completion_value_empty (completion)); vm_run_scope_t run_scope_for_in = { for_in_body_begin_oc, for_in_body_end_oc }; ecma_completion_value_t for_in_body_completion = vm_loop (int_data_p, &run_scope_for_in); if (ecma_is_completion_value_empty (for_in_body_completion)) { JERRY_ASSERT (int_data_p->pos == for_in_body_end_oc); int_data_p->pos = for_in_body_begin_oc; } else { JERRY_ASSERT (ecma_is_completion_value_throw (for_in_body_completion) || ecma_is_completion_value_return (for_in_body_completion) || ecma_is_completion_value_jump (for_in_body_completion)); JERRY_ASSERT (int_data_p->pos <= for_in_body_end_oc); ret_value = for_in_body_completion; break; } } } ecma_free_values_collection (names_p, true); } ECMA_FINALIZE (obj_expr_value); } int_data_p->pos = (vm_instr_counter_t) (for_in_end_oc + 1u); ECMA_FINALIZE (expr_value); return ret_value; } /* opfunc_for_in */
static void interp_mem_stats_opcode_exit (vm_frame_ctx_t *frame_ctx_p, vm_instr_counter_t instr_position, mem_heap_stats_t *heap_stats_before_p, mem_pools_stats_t *pools_stats_before_p) { if (likely (!interp_mem_stats_enabled)) { return; } interp_mem_stats_print_indentation -= INTERP_MEM_PRINT_INDENTATION_STEP; const uint32_t indentation = JERRY_MIN (interp_mem_stats_print_indentation, INTERP_MEM_PRINT_INDENTATION_MAX); char indent_prefix[INTERP_MEM_PRINT_INDENTATION_MAX + 2]; memset (indent_prefix, ' ', sizeof (indent_prefix)); indent_prefix[indentation] = '|'; indent_prefix[indentation + 1] = '\0'; mem_heap_stats_t heap_stats_after; mem_pools_stats_t pools_stats_after; interp_mem_get_stats (&heap_stats_after, &pools_stats_after, false, true); frame_ctx_p->context_peak_allocated_heap_bytes = JERRY_MAX (frame_ctx_p->context_peak_allocated_heap_bytes, heap_stats_after.allocated_bytes); frame_ctx_p->context_peak_waste_heap_bytes = JERRY_MAX (frame_ctx_p->context_peak_waste_heap_bytes, heap_stats_after.waste_bytes); frame_ctx_p->context_peak_pools_count = JERRY_MAX (frame_ctx_p->context_peak_pools_count, pools_stats_after.pools_count); frame_ctx_p->context_peak_allocated_pool_chunks = JERRY_MAX (frame_ctx_p->context_peak_allocated_pool_chunks, pools_stats_after.allocated_chunks); vm_instr_t instr = vm_get_instr (frame_ctx_p->instrs_p, instr_position); printf ("%s Allocated heap bytes: %5u -> %5u (%+5d, local %5u, peak %5u)\n", indent_prefix, (uint32_t) heap_stats_before_p->allocated_bytes, (uint32_t) heap_stats_after.allocated_bytes, (uint32_t) (heap_stats_after.allocated_bytes - heap_stats_before_p->allocated_bytes), (uint32_t) (heap_stats_after.peak_allocated_bytes - JERRY_MAX (heap_stats_before_p->allocated_bytes, heap_stats_after.allocated_bytes)), (uint32_t) heap_stats_after.global_peak_allocated_bytes); if (heap_stats_before_p->waste_bytes != heap_stats_after.waste_bytes) { printf ("%s Waste heap bytes: %5u -> %5u (%+5d, local %5u, peak %5u)\n", indent_prefix, (uint32_t) heap_stats_before_p->waste_bytes, (uint32_t) heap_stats_after.waste_bytes, (uint32_t) (heap_stats_after.waste_bytes - heap_stats_before_p->waste_bytes), (uint32_t) (heap_stats_after.peak_waste_bytes - JERRY_MAX (heap_stats_before_p->waste_bytes, heap_stats_after.waste_bytes)), (uint32_t) heap_stats_after.global_peak_waste_bytes); } if (pools_stats_before_p->pools_count != pools_stats_after.pools_count) { printf ("%s Pools: %5u -> %5u (%+5d, local %5u, peak %5u)\n", indent_prefix, (uint32_t) pools_stats_before_p->pools_count, (uint32_t) pools_stats_after.pools_count, (uint32_t) (pools_stats_after.pools_count - pools_stats_before_p->pools_count), (uint32_t) (pools_stats_after.peak_pools_count - JERRY_MAX (pools_stats_before_p->pools_count, pools_stats_after.pools_count)), (uint32_t) pools_stats_after.global_peak_pools_count); } if (pools_stats_before_p->allocated_chunks != pools_stats_after.allocated_chunks) { printf ("%s Allocated pool chunks: %5u -> %5u (%+5d, local %5u, peak %5u)\n", indent_prefix, (uint32_t) pools_stats_before_p->allocated_chunks, (uint32_t) pools_stats_after.allocated_chunks, (uint32_t) (pools_stats_after.allocated_chunks - pools_stats_before_p->allocated_chunks), (uint32_t) (pools_stats_after.peak_allocated_chunks - JERRY_MAX (pools_stats_before_p->allocated_chunks, pools_stats_after.allocated_chunks)), (uint32_t) pools_stats_after.global_peak_allocated_chunks); } printf ("%s-- End of execution of opcode %s (position %u) --\n\n", indent_prefix, __op_names[instr.op_idx], instr_position); }