/* Locates all of the attributes. Puts them onto a flattened, ordered * list of attributes (populating the passed flat_list). Also builds * the index mapping for doing named lookups. Note index is not related * to the storage position. */ static MVMObject * index_mapping_and_flat_list(MVMThreadContext *tc, MVMObject *mro, MVMCStructREPRData *repr_data) { MVMInstance *instance = tc->instance; MVMObject *flat_list, *class_list, *attr_map_list; MVMint32 num_classes, i, current_slot = 0; MVMCStructNameMap *result; MVMint32 mro_idx = MVM_repr_elems(tc, mro); MVM_gc_root_temp_push(tc, (MVMCollectable **)&mro); flat_list = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_array_type); MVM_gc_root_temp_push(tc, (MVMCollectable **)&flat_list); class_list = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_array_type); MVM_gc_root_temp_push(tc, (MVMCollectable **)&class_list); attr_map_list = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_array_type); MVM_gc_root_temp_push(tc, (MVMCollectable **)&attr_map_list); /* Walk through the parents list. */ while (mro_idx) { /* Get current class in MRO. */ MVMObject *type_info = MVM_repr_at_pos_o(tc, mro, --mro_idx); MVMObject *current_class = MVM_repr_at_pos_o(tc, type_info, 0); /* Get its local parents; make sure we're not doing MI. */ MVMObject *parents = MVM_repr_at_pos_o(tc, type_info, 2); MVMint32 num_parents = MVM_repr_elems(tc, parents); if (num_parents <= 1) { /* Get attributes and iterate over them. */ MVMObject *attributes = MVM_repr_at_pos_o(tc, type_info, 1); MVMIter * const attr_iter = (MVMIter *)MVM_iter(tc, attributes); MVMObject *attr_map = NULL; if (MVM_iter_istrue(tc, attr_iter)) { MVM_gc_root_temp_push(tc, (MVMCollectable **)&attr_iter); attr_map = MVM_repr_alloc_init(tc, MVM_hll_current(tc)->slurpy_hash_type); MVM_gc_root_temp_push(tc, (MVMCollectable **)&attr_map); } while (MVM_iter_istrue(tc, attr_iter)) { MVMObject *current_slot_obj = MVM_repr_box_int(tc, MVM_hll_current(tc)->int_box_type, current_slot); MVMObject *attr, *name_obj; MVMString *name; MVM_repr_shift_o(tc, (MVMObject *)attr_iter); /* Get attribute. */ attr = MVM_iterval(tc, attr_iter); /* Get its name. */ name_obj = MVM_repr_at_key_o(tc, attr, instance->str_consts.name); name = MVM_repr_get_str(tc, name_obj); MVM_repr_bind_key_o(tc, attr_map, name, current_slot_obj); current_slot++; /* Push attr onto the flat list. */ MVM_repr_push_o(tc, flat_list, attr); } if (attr_map) { MVM_gc_root_temp_pop_n(tc, 2); } /* Add to class list and map list. */ MVM_repr_push_o(tc, class_list, current_class); MVM_repr_push_o(tc, attr_map_list, attr_map); } else { MVM_exception_throw_adhoc(tc, "CStruct representation does not support multiple inheritance"); } } MVM_gc_root_temp_pop_n(tc, 4); /* We can now form the name map. */ num_classes = MVM_repr_elems(tc, class_list); result = (MVMCStructNameMap *) MVM_malloc(sizeof(MVMCStructNameMap) * (1 + num_classes)); for (i = 0; i < num_classes; i++) { result[i].class_key = MVM_repr_at_pos_o(tc, class_list, i); result[i].name_map = MVM_repr_at_pos_o(tc, attr_map_list, i); } /* set the end to be NULL, it's useful for iteration. */ result[i].class_key = NULL; repr_data->name_to_index_mapping = result; return flat_list; }
/* Create a new instance of the VM. */ MVMInstance * MVM_vm_create_instance(void) { MVMInstance *instance; char *spesh_log, *spesh_nodelay, *spesh_disable, *spesh_inline_disable, *spesh_osr_disable; char *jit_log, *jit_disable, *jit_bytecode_dir; char *dynvar_log; int init_stat; /* Set up instance data structure. */ instance = MVM_calloc(1, sizeof(MVMInstance)); /* Create the main thread's ThreadContext and stash it. */ instance->main_thread = MVM_tc_create(instance); instance->main_thread->thread_id = 1; /* No user threads when we start, and next thread to be created gets ID 2 * (the main thread got ID 1). */ instance->num_user_threads = 0; MVM_store(&instance->next_user_thread_id, 2); /* Set up the permanent roots storage. */ instance->num_permroots = 0; instance->alloc_permroots = 16; instance->permroots = MVM_malloc(sizeof(MVMCollectable **) * instance->alloc_permroots); instance->permroot_descriptions = MVM_malloc(sizeof(char *) * instance->alloc_permroots); init_mutex(instance->mutex_permroots, "permanent roots"); /* Create fixed size allocator. */ instance->fsa = MVM_fixed_size_create(instance->main_thread); /* Set up REPR registry mutex. */ init_mutex(instance->mutex_repr_registry, "REPR registry"); /* Set up HLL config mutex. */ init_mutex(instance->mutex_hllconfigs, "hll configs"); /* Set up DLL registry mutex. */ init_mutex(instance->mutex_dll_registry, "REPR registry"); /* Set up extension registry mutex. */ init_mutex(instance->mutex_ext_registry, "extension registry"); /* Set up extension op registry mutex. */ init_mutex(instance->mutex_extop_registry, "extension op registry"); /* Set up weak reference hash mutex. */ init_mutex(instance->mutex_sc_weakhash, "sc weakhash"); /* Set up loaded compunits hash mutex. */ init_mutex(instance->mutex_loaded_compunits, "loaded compunits"); /* Set up container registry mutex. */ init_mutex(instance->mutex_container_registry, "container registry"); /* Set up persistent object ID hash mutex. */ init_mutex(instance->mutex_object_ids, "object ID hash"); /* Allocate all things during following setup steps directly in gen2, as * they will have program lifetime. */ MVM_gc_allocate_gen2_default_set(instance->main_thread); /* Set up integer constant and string cache. */ init_mutex(instance->mutex_int_const_cache, "int constant cache"); instance->int_const_cache = MVM_calloc(1, sizeof(MVMIntConstCache)); instance->int_to_str_cache = MVM_calloc(MVM_INT_TO_STR_CACHE_SIZE, sizeof(MVMString *)); /* Bootstrap 6model. It is assumed the GC will not be called during this. */ MVM_6model_bootstrap(instance->main_thread); /* Fix up main thread's usecapture. */ instance->main_thread->cur_usecapture = MVM_repr_alloc_init(instance->main_thread, instance->CallCapture); /* Initialize event loop thread starting mutex. */ init_mutex(instance->mutex_event_loop_start, "event loop thread start"); /* Create main thread object, and also make it the start of the all threads * linked list. */ MVM_store(&instance->threads, (instance->main_thread->thread_obj = (MVMThread *) REPR(instance->boot_types.BOOTThread)->allocate( instance->main_thread, STABLE(instance->boot_types.BOOTThread)))); instance->threads->body.stage = MVM_thread_stage_started; instance->threads->body.tc = instance->main_thread; instance->threads->body.native_thread_id = MVM_platform_thread_id(); instance->threads->body.thread_id = instance->main_thread->thread_id; /* Create compiler registry */ instance->compiler_registry = MVM_repr_alloc_init(instance->main_thread, instance->boot_types.BOOTHash); /* Set up compiler registr mutex. */ init_mutex(instance->mutex_compiler_registry, "compiler registry"); /* Create hll symbol tables */ instance->hll_syms = MVM_repr_alloc_init(instance->main_thread, instance->boot_types.BOOTHash); /* Set up hll symbol tables mutex. */ init_mutex(instance->mutex_hll_syms, "hll syms"); /* Initialize Unicode database */ MVM_unicode_init(instance->main_thread); /* Initialize string cclass handling. */ MVM_string_cclass_init(instance->main_thread); /* Create callsite intern pool. */ instance->callsite_interns = MVM_calloc(1, sizeof(MVMCallsiteInterns)); init_mutex(instance->mutex_callsite_interns, "callsite interns"); /* There's some callsites we statically use all over the place. Intern * them, so that spesh may end up optimizing more "internal" stuff. */ MVM_callsite_initialize_common(instance->main_thread); /* Multi-cache additions mutex. */ init_mutex(instance->mutex_multi_cache_add, "multi-cache addition"); /* Current instrumentation level starts at 1; used to trigger all frames * to be verified before their first run. */ instance->instrumentation_level = 1; /* Mutex for spesh installations, and check if we've a file we * should log specializations to. */ init_mutex(instance->mutex_spesh_install, "spesh installations"); spesh_log = getenv("MVM_SPESH_LOG"); if (spesh_log && strlen(spesh_log)) instance->spesh_log_fh = fopen_perhaps_with_pid(spesh_log, "w"); spesh_disable = getenv("MVM_SPESH_DISABLE"); if (!spesh_disable || strlen(spesh_disable) == 0) { instance->spesh_enabled = 1; spesh_inline_disable = getenv("MVM_SPESH_INLINE_DISABLE"); if (!spesh_inline_disable || strlen(spesh_inline_disable) == 0) instance->spesh_inline_enabled = 1; spesh_osr_disable = getenv("MVM_SPESH_OSR_DISABLE"); if (!spesh_osr_disable || strlen(spesh_osr_disable) == 0) instance->spesh_osr_enabled = 1; } /* Should we specialize without warm up delays? Used to find bugs in the * specializer and JIT. */ spesh_nodelay = getenv("MVM_SPESH_NODELAY"); if (spesh_nodelay && strlen(spesh_nodelay)) { instance->spesh_nodelay = 1; } /* JIT environment/logging setup. */ jit_disable = getenv("MVM_JIT_DISABLE"); if (!jit_disable || strlen(jit_disable) == 0) instance->jit_enabled = 1; jit_log = getenv("MVM_JIT_LOG"); if (jit_log && strlen(jit_log)) instance->jit_log_fh = fopen_perhaps_with_pid(jit_log, "w"); jit_bytecode_dir = getenv("MVM_JIT_BYTECODE_DIR"); if (jit_bytecode_dir && strlen(jit_bytecode_dir)) { char *bytecode_map_name = MVM_malloc(strlen(jit_bytecode_dir) + strlen("/jit-map.txt") + 1); sprintf(bytecode_map_name, "%s/jit-map.txt", jit_bytecode_dir); instance->jit_bytecode_map = fopen(bytecode_map_name, "w"); instance->jit_bytecode_dir = jit_bytecode_dir; MVM_free(bytecode_map_name); } instance->jit_seq_nr = 0; /* Various kinds of debugging that can be enabled. */ dynvar_log = getenv("MVM_DYNVAR_LOG"); if (dynvar_log && strlen(dynvar_log)) { instance->dynvar_log_fh = fopen_perhaps_with_pid(dynvar_log, "w"); fprintf(instance->dynvar_log_fh, "+ x 0 0 0 0 0 %llu\n", uv_hrtime()); fflush(instance->dynvar_log_fh); instance->dynvar_log_lasttime = uv_hrtime(); } else instance->dynvar_log_fh = NULL; instance->nfa_debug_enabled = getenv("MVM_NFA_DEB") ? 1 : 0; if (getenv("MVM_CROSS_THREAD_WRITE_LOG")) { instance->cross_thread_write_logging = 1; instance->cross_thread_write_logging_include_locked = getenv("MVM_CROSS_THREAD_WRITE_LOG_INCLUDE_LOCKED") ? 1 : 0; instance->instrumentation_level++; init_mutex(instance->mutex_cross_thread_write_logging, "cross thread write logging output"); } else { instance->cross_thread_write_logging = 0; } /* Set up NFG state mutation mutex. */ instance->nfg = calloc(1, sizeof(MVMNFGState)); init_mutex(instance->nfg->update_mutex, "NFG update mutex"); /* Create std[in/out/err]. */ setup_std_handles(instance->main_thread); /* Back to nursery allocation, now we're set up. */ MVM_gc_allocate_gen2_default_clear(instance->main_thread); return instance; }
/* Generate bytecode from a spesh graph. */ MVMSpeshCode * MVM_spesh_codegen(MVMThreadContext *tc, MVMSpeshGraph *g) { MVMSpeshCode *res; MVMSpeshBB *bb; MVMint32 i, hanlen; /* Initialize writer state. */ SpeshWriterState *ws = MVM_malloc(sizeof(SpeshWriterState)); ws->bytecode_pos = 0; ws->bytecode_alloc = 1024; ws->bytecode = MVM_malloc(ws->bytecode_alloc); ws->bb_offsets = MVM_malloc(g->num_bbs * sizeof(MVMint32)); ws->num_fixups = 0; ws->alloc_fixups = 64; ws->fixup_locations = MVM_malloc(ws->alloc_fixups * sizeof(MVMint32)); ws->fixup_bbs = MVM_malloc(ws->alloc_fixups * sizeof(MVMSpeshBB *)); for (i = 0; i < g->num_bbs; i++) ws->bb_offsets[i] = -1; /* Create copy of handlers, and -1 all offsets so we can catch missing * updates. */ hanlen = g->num_handlers * sizeof(MVMFrameHandler); if (hanlen) { ws->handlers = MVM_malloc(hanlen); memcpy(ws->handlers, g->handlers, hanlen); for (i = 0; i < g->num_handlers; i++) { ws->handlers[i].start_offset = -1; ws->handlers[i].end_offset = -1; ws->handlers[i].goto_offset = -1; } } else { ws->handlers = NULL; } /* -1 all the deopt targets, so we'll easily catch those that don't get * mapped if we try to use them. Same for inlines. */ for (i = 0; i < g->num_deopt_addrs; i++) g->deopt_addrs[i * 2 + 1] = -1; for (i = 0; i < g->num_inlines; i++) { g->inlines[i].start = -1; g->inlines[i].end = -1; } /* Write out each of the basic blocks, in linear order. Skip the first, * dummy, block. */ bb = g->entry->linear_next; while (bb) { ws->bb_offsets[bb->idx] = ws->bytecode_pos; write_instructions(tc, g, ws, bb); bb = bb->linear_next; } /* Fixup labels we were too early for. */ for (i = 0; i < ws->num_fixups; i++) *((MVMuint32 *)(ws->bytecode + ws->fixup_locations[i])) = ws->bb_offsets[ws->fixup_bbs[i]->idx]; /* Ensure all handlers got fixed up. */ for (i = 0; i < g->num_handlers; i++) { if (ws->handlers[i].start_offset == -1 || ws->handlers[i].end_offset == -1 || ws->handlers[i].goto_offset == -1) MVM_oops(tc, "Spesh: failed to fix up handlers (%d, %d, %d)", (int)ws->handlers[i].start_offset, (int)ws->handlers[i].end_offset, (int)ws->handlers[i].goto_offset); } /* Ensure all inlines got fixed up. */ for (i = 0; i < g->num_inlines; i++) if (g->inlines[i].start == -1 || g->inlines[i].end == -1) MVM_oops(tc, "Spesh: failed to fix up inline %d", i); /* Produce result data structure. */ res = MVM_malloc(sizeof(MVMSpeshCode)); res->bytecode = ws->bytecode; res->bytecode_size = ws->bytecode_pos; res->handlers = ws->handlers; /* Cleanup. */ MVM_free(ws->bb_offsets); MVM_free(ws->fixup_locations); MVM_free(ws->fixup_bbs); MVM_free(ws); return res; }
/* Returns a list of hashes containing file, line, sub and annotations. */ MVMObject * MVM_exception_backtrace(MVMThreadContext *tc, MVMObject *ex_obj) { MVMFrame *cur_frame; MVMObject *arr = NULL, *annotations = NULL, *row = NULL, *value = NULL; MVMuint32 count = 0; MVMString *k_file = NULL, *k_line = NULL, *k_sub = NULL, *k_anno = NULL; if (IS_CONCRETE(ex_obj) && REPR(ex_obj)->ID == MVM_REPR_ID_MVMException) cur_frame = ((MVMException *)ex_obj)->body.origin; else MVM_exception_throw_adhoc(tc, "Op 'backtrace' needs an exception object"); MVM_gc_root_temp_push(tc, (MVMCollectable **)&arr); MVM_gc_root_temp_push(tc, (MVMCollectable **)&annotations); MVM_gc_root_temp_push(tc, (MVMCollectable **)&row); MVM_gc_root_temp_push(tc, (MVMCollectable **)&value); MVM_gc_root_temp_push(tc, (MVMCollectable **)&k_file); MVM_gc_root_temp_push(tc, (MVMCollectable **)&k_line); MVM_gc_root_temp_push(tc, (MVMCollectable **)&k_sub); MVM_gc_root_temp_push(tc, (MVMCollectable **)&k_anno); k_file = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "file"); k_line = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "line"); k_sub = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "sub"); k_anno = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "annotations"); arr = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTArray); while (cur_frame != NULL) { MVMuint8 *cur_op = count ? cur_frame->return_address : cur_frame->throw_address; MVMuint32 offset = cur_op - cur_frame->effective_bytecode; MVMBytecodeAnnotation *annot = MVM_bytecode_resolve_annotation(tc, &cur_frame->static_info->body, offset > 0 ? offset - 1 : 0); MVMint32 fshi = annot ? (MVMint32)annot->filename_string_heap_index : -1; char *line_number = MVM_malloc(16); snprintf(line_number, 16, "%d", annot ? annot->line_number : 1); /* annotations hash will contain "file" and "line" */ annotations = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTHash); /* file */ if (fshi >= 0 && fshi < cur_frame->static_info->body.cu->body.num_strings) value = MVM_repr_box_str(tc, MVM_hll_current(tc)->str_box_type, cur_frame->static_info->body.cu->body.strings[fshi]); else value = MVM_repr_box_str(tc, MVM_hll_current(tc)->str_box_type, cur_frame->static_info->body.cu->body.filename); MVM_repr_bind_key_o(tc, annotations, k_file, value); /* line */ value = (MVMObject *)MVM_string_ascii_decode_nt(tc, tc->instance->VMString, line_number); value = MVM_repr_box_str(tc, MVM_hll_current(tc)->str_box_type, (MVMString *)value); MVM_repr_bind_key_o(tc, annotations, k_line, value); MVM_free(line_number); /* row will contain "sub" and "annotations" */ row = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTHash); MVM_repr_bind_key_o(tc, row, k_sub, cur_frame->code_ref); MVM_repr_bind_key_o(tc, row, k_anno, annotations); MVM_repr_push_o(tc, arr, row); MVM_free(annot); cur_frame = cur_frame->caller; while (cur_frame && cur_frame->static_info->body.is_thunk) cur_frame = cur_frame->caller; count++; } MVM_gc_root_temp_pop_n(tc, 8); return arr; }
/* Merges the inlinee's spesh graph into the inliner. */ static void merge_graph(MVMThreadContext *tc, MVMSpeshGraph *inliner, MVMSpeshGraph *inlinee, MVMCode *inlinee_code, MVMSpeshIns *invoke_ins) { MVMSpeshFacts **merged_facts; MVMuint16 *merged_fact_counts; MVMint32 i, total_inlines, orig_deopt_addrs; /* If the inliner and inlinee are from different compilation units, we * potentially have to fix up extra things. */ MVMint32 same_comp_unit = inliner->sf->body.cu == inlinee->sf->body.cu; /* Renumber the locals, lexicals, and basic blocks of the inlinee; also * re-write any indexes in annotations that need it. */ MVMSpeshBB *bb = inlinee->entry; while (bb) { MVMSpeshIns *ins = bb->first_ins; while (ins) { MVMuint16 opcode = ins->info->opcode; MVMSpeshAnn *ann = ins->annotations; while (ann) { switch (ann->type) { case MVM_SPESH_ANN_FH_START: case MVM_SPESH_ANN_FH_END: case MVM_SPESH_ANN_FH_GOTO: ann->data.frame_handler_index += inliner->num_handlers; break; case MVM_SPESH_ANN_DEOPT_INLINE: ann->data.deopt_idx += inliner->num_deopt_addrs; break; case MVM_SPESH_ANN_INLINE_START: case MVM_SPESH_ANN_INLINE_END: ann->data.inline_idx += inliner->num_inlines; break; } ann = ann->next; } if (opcode == MVM_SSA_PHI) { for (i = 0; i < ins->info->num_operands; i++) ins->operands[i].reg.orig += inliner->num_locals; } else { for (i = 0; i < ins->info->num_operands; i++) { MVMuint8 flags = ins->info->operands[i]; switch (flags & MVM_operand_rw_mask) { case MVM_operand_read_reg: case MVM_operand_write_reg: ins->operands[i].reg.orig += inliner->num_locals; break; case MVM_operand_read_lex: case MVM_operand_write_lex: ins->operands[i].lex.idx += inliner->num_lexicals; break; default: { MVMuint32 type = flags & MVM_operand_type_mask; if (type == MVM_operand_spesh_slot) { ins->operands[i].lit_i16 += inliner->num_spesh_slots; } else if (type == MVM_operand_callsite) { if (!same_comp_unit) fix_callsite(tc, inliner, inlinee, &(ins->operands[i])); } else if (type == MVM_operand_coderef) { if (!same_comp_unit) fix_coderef(tc, inliner, inlinee, &(ins->operands[i])); } else if (type == MVM_operand_str) { if (!same_comp_unit) fix_str(tc, inliner, inlinee, &(ins->operands[i])); } break; } } } } ins = ins->next; } bb->idx += inliner->num_bbs - 1; /* -1 as we won't include entry */ bb->inlined = 1; bb = bb->linear_next; } /* Incorporate the basic blocks by concatening them onto the end of the * linear_next chain of the inliner; skip the inlinee's fake entry BB. */ bb = inliner->entry; while (bb) { if (!bb->linear_next) { /* Found the end; insert and we're done. */ bb->linear_next = inlinee->entry->linear_next; bb = NULL; } else { bb = bb->linear_next; } } /* Merge facts. */ merged_facts = MVM_spesh_alloc(tc, inliner, (inliner->num_locals + inlinee->num_locals) * sizeof(MVMSpeshFacts *)); memcpy(merged_facts, inliner->facts, inliner->num_locals * sizeof(MVMSpeshFacts *)); memcpy(merged_facts + inliner->num_locals, inlinee->facts, inlinee->num_locals * sizeof(MVMSpeshFacts *)); inliner->facts = merged_facts; merged_fact_counts = MVM_spesh_alloc(tc, inliner, (inliner->num_locals + inlinee->num_locals) * sizeof(MVMuint16)); memcpy(merged_fact_counts, inliner->fact_counts, inliner->num_locals * sizeof(MVMuint16)); memcpy(merged_fact_counts + inliner->num_locals, inlinee->fact_counts, inlinee->num_locals * sizeof(MVMuint16)); inliner->fact_counts = merged_fact_counts; /* Copy over spesh slots. */ for (i = 0; i < inlinee->num_spesh_slots; i++) MVM_spesh_add_spesh_slot(tc, inliner, inlinee->spesh_slots[i]); /* If they are from separate compilation units, make another pass through * to fix up on wvals. Note we can't do this in the first pass as we must * not modify the spesh slots once we've got started with the rewrites. * Now we've resolved all that, we're good to map wvals elsewhere into * some extra spesh slots. */ if (!same_comp_unit) { bb = inlinee->entry; while (bb) { MVMSpeshIns *ins = bb->first_ins; while (ins) { MVMuint16 opcode = ins->info->opcode; if (opcode == MVM_OP_wval || opcode == MVM_OP_wval_wide) fix_wval(tc, inliner, inlinee, ins); ins = ins->next; } bb = bb->linear_next; } } /* Merge de-opt tables, if needed. */ orig_deopt_addrs = inliner->num_deopt_addrs; if (inlinee->num_deopt_addrs) { assert(inlinee->deopt_addrs != inliner->deopt_addrs); inliner->alloc_deopt_addrs += inlinee->alloc_deopt_addrs; if (inliner->deopt_addrs) inliner->deopt_addrs = MVM_realloc(inliner->deopt_addrs, inliner->alloc_deopt_addrs * sizeof(MVMint32) * 2); else inliner->deopt_addrs = MVM_malloc(inliner->alloc_deopt_addrs * sizeof(MVMint32) * 2); memcpy(inliner->deopt_addrs + inliner->num_deopt_addrs * 2, inlinee->deopt_addrs, inlinee->alloc_deopt_addrs * sizeof(MVMint32) * 2); inliner->num_deopt_addrs += inlinee->num_deopt_addrs; } /* Merge inlines table, and add us an entry too. */ total_inlines = inliner->num_inlines + inlinee->num_inlines + 1; inliner->inlines = inliner->num_inlines ? MVM_realloc(inliner->inlines, total_inlines * sizeof(MVMSpeshInline)) : MVM_malloc(total_inlines * sizeof(MVMSpeshInline)); memcpy(inliner->inlines + inliner->num_inlines, inlinee->inlines, inlinee->num_inlines * sizeof(MVMSpeshInline)); for (i = inliner->num_inlines; i < total_inlines - 1; i++) { inliner->inlines[i].locals_start += inliner->num_locals; inliner->inlines[i].lexicals_start += inliner->num_lexicals; inliner->inlines[i].return_deopt_idx += orig_deopt_addrs; } inliner->inlines[total_inlines - 1].code = inlinee_code; inliner->inlines[total_inlines - 1].g = inlinee; inliner->inlines[total_inlines - 1].locals_start = inliner->num_locals; inliner->inlines[total_inlines - 1].lexicals_start = inliner->num_lexicals; switch (invoke_ins->info->opcode) { case MVM_OP_invoke_v: inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_VOID; break; case MVM_OP_invoke_o: inliner->inlines[total_inlines - 1].res_reg = invoke_ins->operands[0].reg.orig; inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_OBJ; break; case MVM_OP_invoke_i: inliner->inlines[total_inlines - 1].res_reg = invoke_ins->operands[0].reg.orig; inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_INT; break; case MVM_OP_invoke_n: inliner->inlines[total_inlines - 1].res_reg = invoke_ins->operands[0].reg.orig; inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_NUM; break; case MVM_OP_invoke_s: inliner->inlines[total_inlines - 1].res_reg = invoke_ins->operands[0].reg.orig; inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_STR; break; default: MVM_oops(tc, "Spesh inline: unknown invoke instruction"); } inliner->inlines[total_inlines - 1].return_deopt_idx = return_deopt_idx(tc, invoke_ins); inliner->num_inlines = total_inlines; /* Create/update per-specialization local and lexical type maps. */ if (!inliner->local_types) { MVMint32 local_types_size = inliner->num_locals * sizeof(MVMuint16); inliner->local_types = MVM_malloc(local_types_size); memcpy(inliner->local_types, inliner->sf->body.local_types, local_types_size); } inliner->local_types = MVM_realloc(inliner->local_types, (inliner->num_locals + inlinee->num_locals) * sizeof(MVMuint16)); memcpy(inliner->local_types + inliner->num_locals, inlinee->local_types ? inlinee->local_types : inlinee->sf->body.local_types, inlinee->num_locals * sizeof(MVMuint16)); if (!inliner->lexical_types) { MVMint32 lexical_types_size = inliner->num_lexicals * sizeof(MVMuint16); inliner->lexical_types = MVM_malloc(lexical_types_size); memcpy(inliner->lexical_types, inliner->sf->body.lexical_types, lexical_types_size); } inliner->lexical_types = MVM_realloc(inliner->lexical_types, (inliner->num_lexicals + inlinee->num_lexicals) * sizeof(MVMuint16)); memcpy(inliner->lexical_types + inliner->num_lexicals, inlinee->lexical_types ? inlinee->lexical_types : inlinee->sf->body.lexical_types, inlinee->num_lexicals * sizeof(MVMuint16)); /* Merge handlers. */ if (inlinee->num_handlers) { MVMuint32 total_handlers = inliner->num_handlers + inlinee->num_handlers; if (inliner->handlers == inliner->sf->body.handlers) { /* Original handlers table; need a copy. */ MVMFrameHandler *new_handlers = MVM_malloc(total_handlers * sizeof(MVMFrameHandler)); memcpy(new_handlers, inliner->handlers, inliner->num_handlers * sizeof(MVMFrameHandler)); inliner->handlers = new_handlers; } else { /* Probably already did some inlines into this frame; resize. */ inliner->handlers = MVM_realloc(inliner->handlers, total_handlers * sizeof(MVMFrameHandler)); } memcpy(inliner->handlers + inliner->num_handlers, inlinee->handlers, inlinee->num_handlers * sizeof(MVMFrameHandler)); for (i = inliner->num_handlers; i < total_handlers; i++) { inliner->handlers[i].block_reg += inliner->num_locals; inliner->handlers[i].label_reg += inliner->num_locals; } } /* Update total locals, lexicals, basic blocks, and handlers of the * inliner. */ inliner->num_bbs += inlinee->num_bbs - 1; inliner->num_locals += inlinee->num_locals; inliner->num_lexicals += inlinee->num_lexicals; inliner->num_handlers += inlinee->num_handlers; }
MVMint64 MVM_proc_spawn(MVMThreadContext *tc, MVMObject *argv, MVMString *cwd, MVMObject *env, MVMObject *in, MVMObject *out, MVMObject *err, MVMint64 flags) { MVMint64 result = 0, spawn_result; uv_process_t *process = MVM_calloc(1, sizeof(uv_process_t)); uv_process_options_t process_options = {0}; uv_stdio_container_t process_stdio[3]; int i; char * const _cwd = MVM_string_utf8_c8_encode_C_string(tc, cwd); const MVMuint64 size = MVM_repr_elems(tc, env); MVMIter * const iter = (MVMIter *)MVM_iter(tc, env); char **_env = MVM_malloc((size + 1) * sizeof(char *)); const MVMuint64 arg_size = MVM_repr_elems(tc, argv); char **args = MVM_malloc((arg_size + 1) * sizeof(char *)); MVMRegister reg; i = 0; while(i < arg_size) { REPR(argv)->pos_funcs.at_pos(tc, STABLE(argv), argv, OBJECT_BODY(argv), i, ®, MVM_reg_obj); args[i++] = MVM_string_utf8_c8_encode_C_string(tc, MVM_repr_get_str(tc, reg.o)); } args[arg_size] = NULL; INIT_ENV(); setup_process_stdio(tc, in, process, &process_stdio[0], 0, flags, "spawn"); setup_process_stdio(tc, out, process, &process_stdio[1], 1, flags >> 3, "spawn"); setup_process_stdio(tc, err, process, &process_stdio[2], 2, flags >> 6, "spawn"); process_options.stdio = process_stdio; process_options.file = arg_size ? args[0] : NULL; process_options.args = args; process_options.cwd = _cwd; process_options.flags = UV_PROCESS_WINDOWS_HIDE; process_options.env = _env; process_options.stdio_count = 3; process_options.exit_cb = spawn_on_exit; if (flags & (MVM_PIPE_CAPTURE_IN | MVM_PIPE_CAPTURE_OUT | MVM_PIPE_CAPTURE_ERR)) { process->data = MVM_calloc(1, sizeof(MVMint64)); uv_ref((uv_handle_t *)process); spawn_result = uv_spawn(tc->loop, process, &process_options); if (spawn_result) result = spawn_result; } else { process->data = &result; uv_ref((uv_handle_t *)process); spawn_result = uv_spawn(tc->loop, process, &process_options); if (spawn_result) result = spawn_result; else uv_run(tc->loop, UV_RUN_DEFAULT); } FREE_ENV(); MVM_free(_cwd); uv_unref((uv_handle_t *)process); i = 0; while(args[i]) MVM_free(args[i++]); MVM_free(args); return result; }
void MVM_6model_istype(MVMThreadContext *tc, MVMObject *obj, MVMObject *type, MVMRegister *res) { MVMObject **cache; MVMSTable *st; MVMint64 mode; /* Null never type-checks. */ if (MVM_is_null(tc, obj)) { res->i64 = 0; return; } st = STABLE(obj); mode = STABLE(type)->mode_flags & MVM_TYPE_CHECK_CACHE_FLAG_MASK; cache = st->type_check_cache; if (cache) { /* We have the cache, so just look for the type object we * want to be in there. */ MVMint64 elems = STABLE(obj)->type_check_cache_length; MVMint64 i; for (i = 0; i < elems; i++) { if (cache[i] == type) { res->i64 = 1; return; } } /* If the type check cache is definitive, we're done. */ if ((mode & MVM_TYPE_CHECK_CACHE_THEN_METHOD) == 0 && (mode & MVM_TYPE_CHECK_NEEDS_ACCEPTS) == 0) { res->i64 = 0; return; } } /* If we get here, need to call .^type_check on the value we're * checking, unless it's an accepts check. */ if (!cache || (mode & MVM_TYPE_CHECK_CACHE_THEN_METHOD)) { MVMObject *HOW = MVM_6model_get_how(tc, st); MVMObject *meth = MVM_6model_find_method_cache_only(tc, HOW, tc->instance->str_consts.type_check); if (!MVM_is_null(tc, meth)) { /* Set up the call, using the result register as the target. */ MVMObject *code = MVM_frame_find_invokee(tc, meth, NULL); MVM_args_setup_thunk(tc, res, MVM_RETURN_INT, &tc_callsite); tc->cur_frame->args[0].o = HOW; tc->cur_frame->args[1].o = obj; tc->cur_frame->args[2].o = type; if (mode & MVM_TYPE_CHECK_NEEDS_ACCEPTS) { AcceptsTypeSRData *atd = MVM_malloc(sizeof(AcceptsTypeSRData)); atd->obj = obj; atd->type = type; atd->res = res; tc->cur_frame->special_return = accepts_type_sr; tc->cur_frame->special_return_data = atd; tc->cur_frame->mark_special_return_data = mark_sr_data; } STABLE(code)->invoke(tc, code, &tc_callsite, tc->cur_frame->args); return; } } /* If the flag to call .accepts_type on the target value is set, do so. */ if (mode & MVM_TYPE_CHECK_NEEDS_ACCEPTS) { do_accepts_type_check(tc, obj, type, res); } else { /* If all else fails... */ res->i64 = 0; } }
/* Dissects the bytecode stream and hands back a reader pointing to the * various parts of it. */ static ReaderState * dissect_bytecode(MVMThreadContext *tc, MVMCompUnit *cu) { MVMCompUnitBody *cu_body = &cu->body; ReaderState *rs = NULL; MVMuint32 version, offset, size; /* Sanity checks. */ if (cu_body->data_size < HEADER_SIZE) MVM_exception_throw_adhoc(tc, "Bytecode stream shorter than header"); if (memcmp(cu_body->data_start, "MOARVM\r\n", 8) != 0) MVM_exception_throw_adhoc(tc, "Bytecode stream corrupt (missing magic string)"); version = read_int32(cu_body->data_start, 8); if (version < MIN_BYTECODE_VERSION) MVM_exception_throw_adhoc(tc, "Bytecode stream version too low"); if (version > MAX_BYTECODE_VERSION) MVM_exception_throw_adhoc(tc, "Bytecode stream version too high"); /* Allocate reader state. */ rs = MVM_malloc(sizeof(ReaderState)); memset(rs, 0, sizeof(ReaderState)); rs->version = version; cu->body.bytecode_version = version; /* Locate SC dependencies segment. */ offset = read_int32(cu_body->data_start, SCDEP_HEADER_OFFSET); if (offset > cu_body->data_size) { cleanup_all(tc, rs); MVM_exception_throw_adhoc(tc, "Serialization contexts segment starts after end of stream"); } rs->sc_seg = cu_body->data_start + offset; rs->expected_scs = read_int32(cu_body->data_start, SCDEP_HEADER_OFFSET + 4); /* Locate extension ops segment. */ offset = read_int32(cu_body->data_start, EXTOP_HEADER_OFFSET); if (offset > cu_body->data_size) { cleanup_all(tc, rs); MVM_exception_throw_adhoc(tc, "Extension ops segment starts after end of stream"); } rs->extop_seg = cu_body->data_start + offset; rs->expected_extops = read_int32(cu_body->data_start, EXTOP_HEADER_OFFSET + 4); /* Locate frames segment. */ offset = read_int32(cu_body->data_start, FRAME_HEADER_OFFSET); if (offset > cu_body->data_size) { cleanup_all(tc, rs); MVM_exception_throw_adhoc(tc, "Frames segment starts after end of stream"); } rs->frame_seg = cu_body->data_start + offset; rs->expected_frames = read_int32(cu_body->data_start, FRAME_HEADER_OFFSET + 4); /* Locate callsites segment. */ offset = read_int32(cu_body->data_start, CALLSITE_HEADER_OFFSET); if (offset > cu_body->data_size) { cleanup_all(tc, rs); MVM_exception_throw_adhoc(tc, "Callsites segment starts after end of stream"); } rs->callsite_seg = cu_body->data_start + offset; rs->expected_callsites = read_int32(cu_body->data_start, CALLSITE_HEADER_OFFSET + 4); /* Locate strings segment. */ offset = read_int32(cu_body->data_start, STRING_HEADER_OFFSET); if (offset > cu_body->data_size) { cleanup_all(tc, rs); MVM_exception_throw_adhoc(tc, "Strings segment starts after end of stream"); } rs->string_seg = cu_body->data_start + offset; rs->expected_strings = read_int32(cu_body->data_start, STRING_HEADER_OFFSET + 4); /* Get SC data, if any. */ offset = read_int32(cu_body->data_start, SCDATA_HEADER_OFFSET); size = read_int32(cu_body->data_start, SCDATA_HEADER_OFFSET + 4); if (offset > cu_body->data_size || offset + size > cu_body->data_size) { cleanup_all(tc, rs); MVM_exception_throw_adhoc(tc, "Serialized data segment overflows end of stream"); } if (offset) { cu_body->serialized = cu_body->data_start + offset; cu_body->serialized_size = size; } /* Locate bytecode segment. */ offset = read_int32(cu_body->data_start, BYTECODE_HEADER_OFFSET); size = read_int32(cu_body->data_start, BYTECODE_HEADER_OFFSET + 4); if (offset > cu_body->data_size || offset + size > cu_body->data_size) { cleanup_all(tc, rs); MVM_exception_throw_adhoc(tc, "Bytecode segment overflows end of stream"); } rs->bytecode_seg = cu_body->data_start + offset; rs->bytecode_size = size; /* Locate annotations segment. */ offset = read_int32(cu_body->data_start, ANNOTATION_HEADER_OFFSET); size = read_int32(cu_body->data_start, ANNOTATION_HEADER_OFFSET + 4); if (offset > cu_body->data_size || offset + size > cu_body->data_size) { cleanup_all(tc, rs); MVM_exception_throw_adhoc(tc, "Annotation segment overflows end of stream"); } rs->annotation_seg = cu_body->data_start + offset; rs->annotation_size = size; /* Locate HLL name */ rs->hll_str_idx = read_int32(cu_body->data_start, HLL_NAME_HEADER_OFFSET); /* Locate special frame indexes. Note, they are 0 for none, and the * index + 1 if there is one. */ rs->main_frame = read_int32(cu_body->data_start, SPECIAL_FRAME_HEADER_OFFSET); rs->load_frame = read_int32(cu_body->data_start, SPECIAL_FRAME_HEADER_OFFSET + 4); rs->deserialize_frame = read_int32(cu_body->data_start, SPECIAL_FRAME_HEADER_OFFSET + 8); if (rs->main_frame > rs->expected_frames || rs->load_frame > rs->expected_frames || rs->deserialize_frame > rs->expected_frames) { MVM_exception_throw_adhoc(tc, "Special frame index out of bounds"); } return rs; }
/* Create a new instance of the VM. */ MVMInstance * MVM_vm_create_instance(void) { MVMInstance *instance; char *spesh_log, *spesh_disable, *spesh_inline_disable, *spesh_osr_disable; char *jit_log, *jit_disable, *jit_bytecode_dir; char *dynvar_log; int init_stat; /* Set up instance data structure. */ instance = MVM_calloc(1, sizeof(MVMInstance)); /* Create the main thread's ThreadContext and stash it. */ instance->main_thread = MVM_tc_create(instance); instance->main_thread->thread_id = 1; /* No user threads when we start, and next thread to be created gets ID 2 * (the main thread got ID 1). */ instance->num_user_threads = 0; MVM_store(&instance->next_user_thread_id, 2); /* Set up the permanent roots storage. */ instance->num_permroots = 0; instance->alloc_permroots = 16; instance->permroots = MVM_malloc(sizeof(MVMCollectable **) * instance->alloc_permroots); init_mutex(instance->mutex_permroots, "permanent roots"); /* Create fixed size allocator. */ instance->fsa = MVM_fixed_size_create(instance->main_thread); /* Set up REPR registry mutex. */ init_mutex(instance->mutex_repr_registry, "REPR registry"); /* Set up HLL config mutex. */ init_mutex(instance->mutex_hllconfigs, "hll configs"); /* Set up DLL registry mutex. */ init_mutex(instance->mutex_dll_registry, "REPR registry"); /* Set up extension registry mutex. */ init_mutex(instance->mutex_ext_registry, "extension registry"); /* Set up extension op registry mutex. */ init_mutex(instance->mutex_extop_registry, "extension op registry"); /* Set up weak reference hash mutex. */ init_mutex(instance->mutex_sc_weakhash, "sc weakhash"); /* Set up loaded compunits hash mutex. */ init_mutex(instance->mutex_loaded_compunits, "loaded compunits"); /* Set up container registry mutex. */ init_mutex(instance->mutex_container_registry, "container registry"); /* Set up persistent object ID hash mutex. */ init_mutex(instance->mutex_object_ids, "object ID hash"); /* Allocate all things during following setup steps directly in gen2, as * they will have program lifetime. */ MVM_gc_allocate_gen2_default_set(instance->main_thread); init_mutex(instance->mutex_int_const_cache, "int constant cache"); instance->int_const_cache = MVM_calloc(1, sizeof(MVMIntConstCache)); /* Bootstrap 6model. It is assumed the GC will not be called during this. */ MVM_6model_bootstrap(instance->main_thread); /* Fix up main thread's usecapture. */ instance->main_thread->cur_usecapture = MVM_repr_alloc_init(instance->main_thread, instance->CallCapture); /* Initialize event loop thread starting mutex. */ init_mutex(instance->mutex_event_loop_start, "event loop thread start"); /* Create main thread object, and also make it the start of the all threads * linked list. */ MVM_store(&instance->threads, (instance->main_thread->thread_obj = (MVMThread *) REPR(instance->boot_types.BOOTThread)->allocate( instance->main_thread, STABLE(instance->boot_types.BOOTThread)))); instance->threads->body.stage = MVM_thread_stage_started; instance->threads->body.tc = instance->main_thread; instance->threads->body.thread_id = uv_thread_self(); /* Create compiler registry */ instance->compiler_registry = MVM_repr_alloc_init(instance->main_thread, instance->boot_types.BOOTHash); /* Set up compiler registr mutex. */ init_mutex(instance->mutex_compiler_registry, "compiler registry"); /* Create hll symbol tables */ instance->hll_syms = MVM_repr_alloc_init(instance->main_thread, instance->boot_types.BOOTHash); /* Set up hll symbol tables mutex. */ init_mutex(instance->mutex_hll_syms, "hll syms"); /* Initialize string cclass handling. */ MVM_string_cclass_init(instance->main_thread); /* Create callsite intern pool. */ instance->callsite_interns = MVM_calloc(1, sizeof(MVMCallsiteInterns)); init_mutex(instance->mutex_callsite_interns, "callsite interns"); /* Allocate int to str cache. */ instance->int_to_str_cache = MVM_calloc(MVM_INT_TO_STR_CACHE_SIZE, sizeof(MVMString *)); /* There's some callsites we statically use all over the place. Intern * them, so that spesh may end up optimizing more "internal" stuff. */ MVM_callsite_initialize_common(instance); /* Mutex for spesh installations, and check if we've a file we * should log specializations to. */ init_mutex(instance->mutex_spesh_install, "spesh installations"); spesh_log = getenv("MVM_SPESH_LOG"); if (spesh_log && strlen(spesh_log)) instance->spesh_log_fh = fopen(spesh_log, "w"); spesh_disable = getenv("MVM_SPESH_DISABLE"); if (!spesh_disable || strlen(spesh_disable) == 0) { instance->spesh_enabled = 1; spesh_inline_disable = getenv("MVM_SPESH_INLINE_DISABLE"); if (!spesh_inline_disable || strlen(spesh_inline_disable) == 0) instance->spesh_inline_enabled = 1; spesh_osr_disable = getenv("MVM_SPESH_OSR_DISABLE"); if (!spesh_osr_disable || strlen(spesh_osr_disable) == 0) instance->spesh_osr_enabled = 1; } jit_disable = getenv("MVM_JIT_DISABLE"); if (!jit_disable || strlen(jit_disable) == 0) instance->jit_enabled = 1; jit_log = getenv("MVM_JIT_LOG"); if (jit_log && strlen(jit_log)) instance->jit_log_fh = fopen(jit_log, "w"); jit_bytecode_dir = getenv("MVM_JIT_BYTECODE_DIR"); if (jit_bytecode_dir && strlen(jit_bytecode_dir)) instance->jit_bytecode_dir = jit_bytecode_dir; dynvar_log = getenv("MVM_DYNVAR_LOG"); if (dynvar_log && strlen(dynvar_log)) instance->dynvar_log_fh = fopen(dynvar_log, "w"); else instance->dynvar_log_fh = NULL; /* Create std[in/out/err]. */ setup_std_handles(instance->main_thread); /* Current instrumentation level starts at 1; used to trigger all frames * to be verified before their first run. */ instance->instrumentation_level = 1; /* Back to nursery allocation, now we're set up. */ MVM_gc_allocate_gen2_default_clear(instance->main_thread); return instance; }
/* Validate that a static frame's bytecode is executable by the interpreter. */ void MVM_validate_static_frame(MVMThreadContext *tc, MVMStaticFrame *static_frame) { MVMStaticFrameBody *fb = &static_frame->body; Validator val[1]; val->tc = tc; val->cu = fb->cu; val->frame = static_frame; val->loc_count = fb->num_locals; val->loc_types = fb->local_types; val->bc_size = fb->bytecode_size; val->src_cur_op = fb->bytecode; val->src_bc_end = fb->bytecode + fb->bytecode_size; val->labels = MVM_calloc(fb->bytecode_size, 1); val->cur_info = NULL; val->cur_mark = NULL; val->cur_instr = 0; val->cur_call = NULL; val->cur_arg = 0; val->expected_named_arg = 0; val->remaining_positionals = 0; val->remaining_jumplabels = 0; val->reg_type_var = 0; #ifdef MVM_BIGENDIAN assert(fb->bytecode == fb->orig_bytecode); val->bc_start = MVM_malloc(fb->bytecode_size); memset(val->bc_start, 0xDB, fb->bytecode_size); fb->bytecode = val->bc_start; #else val->bc_start = fb->bytecode; #endif val->bc_end = val->bc_start + fb->bytecode_size; val->cur_op = val->bc_start; while (val->cur_op < val->bc_end) { read_op(val); if (val->cur_mark && *(val->cur_mark) == 's') fail(val, MSG(val, "Illegal appearance of spesh op")); switch (val->cur_mark[0]) { case MARK_regular: case MARK_special: validate_operands(val); break; case MARK_sequence: validate_sequence(val); break; case MARK_head: validate_block(val); break; default: fail_illegal_mark(val); } } validate_branch_targets(val); validate_final_return(val); /* Validation successful. Cache the located instruction offsets. */ fb->instr_offsets = val->labels; }
/* Takes information about the incoming callsite and arguments, and performs * various optimizations based on that information. */ void MVM_spesh_args(MVMThreadContext *tc, MVMSpeshGraph *g, MVMCallsite *cs, MVMRegister *args) { /* We need to identify the various arg-related instructions in the graph, * then manipulate them as a whole. */ MVMSpeshIns *checkarity_ins = NULL; MVMSpeshBB *checkarity_bb = NULL; MVMSpeshIns *paramnamesused_ins = NULL; MVMSpeshBB *paramnamesused_bb = NULL; MVMSpeshIns *param_sp_ins = NULL; MVMSpeshBB *param_sp_bb = NULL; MVMSpeshIns *param_sn_ins = NULL; MVMSpeshBB *param_sn_bb = NULL; MVMSpeshIns **pos_ins = MVM_calloc(MAX_POS_ARGS, sizeof(MVMSpeshIns *)); MVMSpeshBB **pos_bb = MVM_calloc(MAX_POS_ARGS, sizeof(MVMSpeshBB *)); MVMuint8 *pos_added = MVM_calloc(MAX_POS_ARGS, sizeof(MVMuint8)); MVMSpeshIns **named_ins = MVM_calloc(MAX_NAMED_ARGS, sizeof(MVMSpeshIns *)); MVMSpeshBB **named_bb = MVM_calloc(MAX_NAMED_ARGS, sizeof(MVMSpeshBB *)); MVMSpeshIns **used_ins = MVM_calloc(MAX_NAMED_ARGS, sizeof(MVMSpeshIns *)); MVMint32 req_max = -1; MVMint32 opt_min = -1; MVMint32 opt_max = -1; MVMint32 num_named = 0; MVMint32 named_used = 0; MVMint32 got_named = cs->num_pos != cs->arg_count; /* Walk through the graph, looking for arg related instructions. */ MVMSpeshBB *bb = g->entry; while (bb) { MVMSpeshIns *ins = bb->first_ins; while (ins) { switch (ins->info->opcode) { case MVM_OP_checkarity: if (checkarity_ins) goto cleanup; /* Dupe; weird; bail out! */ checkarity_ins = ins; checkarity_bb = bb; break; case MVM_OP_param_rp_i: case MVM_OP_param_rp_n: case MVM_OP_param_rp_s: case MVM_OP_param_rp_o: { /* Required positional. */ MVMint16 idx = ins->operands[1].lit_i16; if (idx < 0 || idx >= MAX_POS_ARGS) goto cleanup; if (pos_ins[idx]) /* Dupe; weird. */ goto cleanup; pos_ins[idx] = ins; pos_bb[idx] = bb; if (idx > req_max) req_max = idx; break; } case MVM_OP_param_op_i: case MVM_OP_param_op_n: case MVM_OP_param_op_s: case MVM_OP_param_op_o: { /* Optional Positional int/num/string/object */ MVMint16 idx = ins->operands[1].lit_i16; if (idx < 0 || idx >= MAX_POS_ARGS) goto cleanup; if (pos_ins[idx]) /* Dupe; weird. */ goto cleanup; pos_ins[idx] = ins; pos_bb[idx] = bb; if (idx > opt_max) opt_max = idx; if (opt_min == -1 || idx < opt_min) opt_min = idx; break; } case MVM_OP_param_on_i: case MVM_OP_param_on_n: case MVM_OP_param_on_s: case MVM_OP_param_on_o: case MVM_OP_param_rn_i: case MVM_OP_param_rn_n: case MVM_OP_param_rn_s: case MVM_OP_param_rn_o: /* Named (optional or required). */ if (num_named == MAX_NAMED_ARGS) goto cleanup; named_ins[num_named] = ins; named_bb[num_named] = bb; num_named++; break; case MVM_OP_param_sp: param_sp_ins = ins; param_sp_bb = bb; break; case MVM_OP_param_sn: param_sn_ins = ins; param_sn_bb = bb; break; case MVM_OP_usecapture: case MVM_OP_savecapture: /* Require full args processing context for now; bail. */ goto cleanup; case MVM_OP_paramnamesused: if (paramnamesused_ins) goto cleanup; /* Dupe; weird; bail out! */ paramnamesused_ins = ins; paramnamesused_bb = bb; break; } ins = ins->next; } bb = bb->linear_next; } /* If we didn't find a checkarity instruction, bail. */ if (!checkarity_ins) goto cleanup; /* If required and optional aren't contiguous, bail. */ if (opt_min >= 0 && req_max + 1 != opt_min) goto cleanup; /* If the number of passed args is in range... */ if (cs->num_pos >= req_max + 1 && (opt_max < 0 || cs->num_pos <= opt_max + 1)) { /* Ensure we've got all the arg fetch instructions we need, and that * types match or it's a box/unbox. */ MVMint32 i; for (i = 0; i < cs->num_pos; i++) { MVMCallsiteEntry arg_flag = cs->arg_flags[i]; if (!pos_ins[i]) goto cleanup; switch (pos_ins[i]->info->opcode) { case MVM_OP_param_rp_i: case MVM_OP_param_op_i: if (arg_flag != MVM_CALLSITE_ARG_INT) if (arg_flag != MVM_CALLSITE_ARG_OBJ || prim_spec(tc, args[i].o) != MVM_STORAGE_SPEC_BP_INT) goto cleanup; break; case MVM_OP_param_rp_n: case MVM_OP_param_op_n: if (arg_flag != MVM_CALLSITE_ARG_NUM) if (arg_flag != MVM_CALLSITE_ARG_OBJ || prim_spec(tc, args[i].o) != MVM_STORAGE_SPEC_BP_NUM) goto cleanup; break; case MVM_OP_param_rp_s: case MVM_OP_param_op_s: if (arg_flag != MVM_CALLSITE_ARG_STR) if (arg_flag != MVM_CALLSITE_ARG_OBJ || prim_spec(tc, args[i].o) != MVM_STORAGE_SPEC_BP_STR) goto cleanup; break; case MVM_OP_param_rp_o: case MVM_OP_param_op_o: if (arg_flag != MVM_CALLSITE_ARG_OBJ && arg_flag != MVM_CALLSITE_ARG_INT && arg_flag != MVM_CALLSITE_ARG_NUM && arg_flag != MVM_CALLSITE_ARG_STR) goto cleanup; break; } } /* If we know there's no incoming nameds we can always turn param_sn into a * simple hash creation. This will typically be further lowered in optimize. */ if (param_sn_ins && !got_named) { MVMObject *hash_type = g->sf->body.cu->body.hll_config->slurpy_hash_type; if (REPR(hash_type)->ID == MVM_REPR_ID_MVMHash) { MVMSpeshOperand target = param_sn_ins->operands[0]; param_sn_ins->info = MVM_op_get_op(MVM_OP_sp_fastcreate); param_sn_ins->operands = MVM_spesh_alloc(tc, g, 3 * sizeof(MVMSpeshOperand)); param_sn_ins->operands[0] = target; param_sn_ins->operands[1].lit_i16 = sizeof(MVMHash); param_sn_ins->operands[2].lit_i16 = MVM_spesh_add_spesh_slot(tc, g, (MVMCollectable *)STABLE(hash_type)); } else { goto cleanup; } } /* We can optimize. Toss checkarity. */ MVM_spesh_manipulate_delete_ins(tc, g, checkarity_bb, checkarity_ins); /* Re-write the passed required positionals to spesh ops, and store * any gurads. */ if (cs->arg_count) g->arg_guards = MVM_malloc(2 * cs->arg_count * sizeof(MVMSpeshGuard)); for (i = 0; i < cs->num_pos; i++) { MVMCallsiteEntry arg_flag = cs->arg_flags[i]; switch (pos_ins[i]->info->opcode) { case MVM_OP_param_rp_i: case MVM_OP_param_op_i: if (arg_flag == MVM_CALLSITE_ARG_INT) { pos_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_i); } else { pos_unbox(tc, g, pos_bb[i], pos_ins[i], MVM_op_get_op(MVM_OP_unbox_i)); pos_added[i]++; if (args[i].o) add_guards_and_facts(tc, g, i, args[i].o, pos_ins[i]); } break; case MVM_OP_param_rp_n: case MVM_OP_param_op_n: if (arg_flag == MVM_CALLSITE_ARG_NUM) { pos_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_n); } else { pos_unbox(tc, g, pos_bb[i], pos_ins[i], MVM_op_get_op(MVM_OP_unbox_n)); pos_added[i]++; if (args[i].o) add_guards_and_facts(tc, g, i, args[i].o, pos_ins[i]); } break; case MVM_OP_param_rp_s: case MVM_OP_param_op_s: if (arg_flag == MVM_CALLSITE_ARG_STR) { pos_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_s); } else { pos_unbox(tc, g, pos_bb[i], pos_ins[i], MVM_op_get_op(MVM_OP_unbox_s)); pos_added[i]++; if (args[i].o) add_guards_and_facts(tc, g, i, args[i].o, pos_ins[i]); } break; case MVM_OP_param_rp_o: case MVM_OP_param_op_o: if (arg_flag == MVM_CALLSITE_ARG_OBJ) { pos_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_o); if (args[i].o) add_guards_and_facts(tc, g, i, args[i].o, pos_ins[i]); } else if (arg_flag == MVM_CALLSITE_ARG_INT) { pos_box(tc, g, pos_bb[i], pos_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_i), MVM_op_get_op(MVM_OP_box_i), MVM_op_get_op(MVM_OP_sp_getarg_i), MVM_reg_int64); pos_added[i] += 2; } else if (arg_flag == MVM_CALLSITE_ARG_NUM) { pos_box(tc, g, pos_bb[i], pos_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_n), MVM_op_get_op(MVM_OP_box_n), MVM_op_get_op(MVM_OP_sp_getarg_n), MVM_reg_num64); pos_added[i] += 2; } else if (arg_flag == MVM_CALLSITE_ARG_STR) { pos_box(tc, g, pos_bb[i], pos_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_s), MVM_op_get_op(MVM_OP_box_s), MVM_op_get_op(MVM_OP_sp_getarg_s), MVM_reg_str); pos_added[i] += 2; } break; } pos_ins[i]->operands[1].lit_i16 = (MVMint16)i; } /* Now consider any optionals. */ if (opt_min >= 0) { for (i = opt_min; i <= opt_max; i++) { MVMuint8 passed = i < cs->num_pos; if (passed) { /* If we know the argument has been passed, then add a goto * to the "passed" code. */ MVMSpeshIns *after = pos_ins[i]; while (pos_added[i]--) after = after->next; MVM_spesh_manipulate_insert_goto(tc, g, pos_bb[i], after, pos_ins[i]->operands[2].ins_bb); /* Inserting an unconditional goto makes the linear_next BB * unreachable, so we remove it from the succ list. */ MVM_spesh_manipulate_remove_successor(tc, pos_bb[i], pos_bb[i]->linear_next); } else { /* If we didn't pass this, just fall through the original * operation and we'll get the default value set. */ MVM_spesh_manipulate_delete_ins(tc, g, pos_bb[i], pos_ins[i]); MVM_spesh_manipulate_remove_successor(tc, pos_bb[i], pos_ins[i]->operands[2].ins_bb); } } } /* Now consider any nameds. */ for (i = 0; i < num_named; i++) { /* See if the arg was passed. */ MVMString *arg_name = MVM_spesh_get_string(tc, g, named_ins[i]->operands[1]); MVMint32 passed_nameds = (cs->arg_count - cs->num_pos) / 2; MVMint32 cs_flags = cs->num_pos + passed_nameds; MVMint32 cur_idx = 0; MVMint32 cur_named = 0; MVMuint8 found_flag = 0; MVMint32 found_idx = -1; MVMint32 j; for (j = 0; j < cs_flags; j++) { if (cs->arg_flags[j] & MVM_CALLSITE_ARG_NAMED) { if (MVM_string_equal(tc, arg_name, cs->arg_names[cur_named])) { /* Found it. */ found_flag = cs->arg_flags[j]; found_idx = cur_idx; break; } cur_idx += 2; cur_named++; } else { cur_idx++; } } /* Now go by instruction. */ switch (named_ins[i]->info->opcode) { case MVM_OP_param_rn_i: if (found_idx == -1) goto cleanup; if (found_flag & MVM_CALLSITE_ARG_INT) { named_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_i); named_ins[i]->operands[1].lit_i16 = found_idx + 1; used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i], cur_named); } else if (found_flag & MVM_CALLSITE_ARG_OBJ && prim_spec(tc, args[found_idx].o) == MVM_STORAGE_SPEC_BP_INT) { named_ins[i]->operands[1].lit_i16 = found_idx + 1; pos_unbox(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_unbox_i)); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i]->next, cur_named); } named_used++; break; case MVM_OP_param_rn_n: if (found_idx == -1) goto cleanup; if (found_flag & MVM_CALLSITE_ARG_NUM) { named_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_n); named_ins[i]->operands[1].lit_i16 = found_idx + 1; used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i], cur_named); } else if (found_flag & MVM_CALLSITE_ARG_OBJ && prim_spec(tc, args[found_idx].o) == MVM_STORAGE_SPEC_BP_NUM) { named_ins[i]->operands[1].lit_i16 = found_idx + 1; pos_unbox(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_unbox_n)); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i]->next, cur_named); } named_used++; break; case MVM_OP_param_rn_s: if (found_idx == -1) goto cleanup; if (found_flag & MVM_CALLSITE_ARG_STR) { named_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_s); named_ins[i]->operands[1].lit_i16 = found_idx + 1; used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i], cur_named); } else if (found_flag & MVM_CALLSITE_ARG_OBJ && prim_spec(tc, args[found_idx].o) == MVM_STORAGE_SPEC_BP_STR) { named_ins[i]->operands[1].lit_i16 = found_idx + 1; pos_unbox(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_unbox_s)); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i]->next, cur_named); } named_used++; break; case MVM_OP_param_rn_o: if (found_idx == -1) goto cleanup; if (found_flag & MVM_CALLSITE_ARG_OBJ) { MVMuint16 arg_idx = found_idx + 1; named_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_o); named_ins[i]->operands[1].lit_i16 = arg_idx; used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i], cur_named); if (args[arg_idx].o) add_guards_and_facts(tc, g, arg_idx, args[arg_idx].o, named_ins[i]); } else if (found_flag & (MVM_CALLSITE_ARG_INT | MVM_CALLSITE_ARG_NUM | MVM_CALLSITE_ARG_STR)) { MVMuint16 arg_idx = found_idx + 1; named_ins[i]->operands[1].lit_i16 = arg_idx; if (found_flag & MVM_CALLSITE_ARG_INT) pos_box(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_i), MVM_op_get_op(MVM_OP_box_i), MVM_op_get_op(MVM_OP_sp_getarg_i), MVM_reg_int64); else if (found_flag & MVM_CALLSITE_ARG_NUM) pos_box(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_n), MVM_op_get_op(MVM_OP_box_n), MVM_op_get_op(MVM_OP_sp_getarg_n), MVM_reg_num64); else if (found_flag & MVM_CALLSITE_ARG_STR) pos_box(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_s), MVM_op_get_op(MVM_OP_box_s), MVM_op_get_op(MVM_OP_sp_getarg_s), MVM_reg_str); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i]->next->next, cur_named); } named_used++; break; case MVM_OP_param_on_i: if (found_idx == -1) { MVM_spesh_manipulate_delete_ins(tc, g, named_bb[i], named_ins[i]); } else if (found_flag & MVM_CALLSITE_ARG_INT) { named_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_i); named_ins[i]->operands[1].lit_i16 = found_idx + 1; MVM_spesh_manipulate_insert_goto(tc, g, named_bb[i], named_ins[i], named_ins[i]->operands[2].ins_bb); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i], cur_named); named_used++; } else if (found_flag & MVM_CALLSITE_ARG_OBJ && prim_spec(tc, args[found_idx].o) == MVM_STORAGE_SPEC_BP_INT) { named_ins[i]->operands[1].lit_i16 = found_idx + 1; pos_unbox(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_unbox_i)); MVM_spesh_manipulate_insert_goto(tc, g, named_bb[i], named_ins[i]->next, named_ins[i]->operands[2].ins_bb); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i]->next, cur_named); named_used++; } break; case MVM_OP_param_on_n: if (found_idx == -1) { MVM_spesh_manipulate_delete_ins(tc, g, named_bb[i], named_ins[i]); } else if (found_flag & MVM_CALLSITE_ARG_NUM) { named_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_n); named_ins[i]->operands[1].lit_i16 = found_idx + 1; MVM_spesh_manipulate_insert_goto(tc, g, named_bb[i], named_ins[i], named_ins[i]->operands[2].ins_bb); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i], cur_named); named_used++; } else if (found_flag & MVM_CALLSITE_ARG_OBJ && prim_spec(tc, args[found_idx].o) == MVM_STORAGE_SPEC_BP_NUM) { named_ins[i]->operands[1].lit_i16 = found_idx + 1; pos_unbox(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_unbox_n)); MVM_spesh_manipulate_insert_goto(tc, g, named_bb[i], named_ins[i]->next, named_ins[i]->operands[2].ins_bb); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i]->next, cur_named); named_used++; } break; case MVM_OP_param_on_s: if (found_idx == -1) { MVM_spesh_manipulate_delete_ins(tc, g, named_bb[i], named_ins[i]); } else if (found_flag & MVM_CALLSITE_ARG_STR) { named_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_s); named_ins[i]->operands[1].lit_i16 = found_idx + 1; MVM_spesh_manipulate_insert_goto(tc, g, named_bb[i], named_ins[i], named_ins[i]->operands[2].ins_bb); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i], cur_named); named_used++; } else if (found_flag & MVM_CALLSITE_ARG_OBJ && prim_spec(tc, args[found_idx].o) == MVM_STORAGE_SPEC_BP_STR) { named_ins[i]->operands[1].lit_i16 = found_idx + 1; pos_unbox(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_unbox_s)); MVM_spesh_manipulate_insert_goto(tc, g, named_bb[i], named_ins[i]->next, named_ins[i]->operands[2].ins_bb); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i]->next, cur_named); named_used++; } break; case MVM_OP_param_on_o: if (found_idx == -1) { MVM_spesh_manipulate_delete_ins(tc, g, named_bb[i], named_ins[i]); } else if (found_flag & MVM_CALLSITE_ARG_OBJ) { MVMuint16 arg_idx = found_idx + 1; named_ins[i]->info = MVM_op_get_op(MVM_OP_sp_getarg_o); named_ins[i]->operands[1].lit_i16 = arg_idx; MVM_spesh_manipulate_insert_goto(tc, g, named_bb[i], named_ins[i], named_ins[i]->operands[2].ins_bb); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i], cur_named); if (args[arg_idx].o) add_guards_and_facts(tc, g, arg_idx, args[arg_idx].o, named_ins[i]); named_used++; } else if (found_flag & (MVM_CALLSITE_ARG_INT | MVM_CALLSITE_ARG_NUM | MVM_CALLSITE_ARG_STR)) { MVMuint16 arg_idx = found_idx + 1; named_ins[i]->operands[1].lit_i16 = arg_idx; if (found_flag & MVM_CALLSITE_ARG_INT) pos_box(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_i), MVM_op_get_op(MVM_OP_box_i), MVM_op_get_op(MVM_OP_sp_getarg_i), MVM_reg_int64); else if (found_flag & MVM_CALLSITE_ARG_NUM) pos_box(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_n), MVM_op_get_op(MVM_OP_box_n), MVM_op_get_op(MVM_OP_sp_getarg_n), MVM_reg_num64); else if (found_flag & MVM_CALLSITE_ARG_STR) pos_box(tc, g, named_bb[i], named_ins[i], MVM_op_get_op(MVM_OP_hllboxtype_s), MVM_op_get_op(MVM_OP_box_s), MVM_op_get_op(MVM_OP_sp_getarg_s), MVM_reg_str); MVM_spesh_manipulate_insert_goto(tc, g, named_bb[i], named_ins[i]->next->next, named_ins[i]->operands[2].ins_bb); used_ins[i] = add_named_used_ins(tc, g, named_bb[i], named_ins[i]->next->next, cur_named); named_used++; } break; } } /* If we had no nameds or we used them all, can toss namesused, and we * don't need to mark used after all. */ if (paramnamesused_ins && num_named == named_used) { MVM_spesh_manipulate_delete_ins(tc, g, paramnamesused_bb, paramnamesused_ins); for (i = 0; i < num_named; i++) if (used_ins[i]) MVM_spesh_manipulate_delete_ins(tc, g, named_bb[i], used_ins[i]); } } cleanup: MVM_free(pos_ins); MVM_free(pos_bb); MVM_free(pos_added); MVM_free(named_ins); MVM_free(named_bb); }
/* Read a bunch of bytes into the current decode stream. Returns true if we * read some data, and false if we hit EOF. */ static void on_alloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { size_t size = suggested_size > 0 ? suggested_size : 4; buf->base = MVM_malloc(size); buf->len = size; }
MVMint64 socket_read_bytes(MVMThreadContext *tc, MVMOSHandle *h, char **buf, MVMint64 bytes) { MVMIOSyncSocketData *data = (MVMIOSyncSocketData *)h->body.data; char *use_last_packet = NULL; MVMuint16 use_last_packet_start, use_last_packet_end; /* If at EOF, nothing more to do. */ if (data->eof) { *buf = NULL; return 0; } /* See if there's anything in the packet buffer. */ if (data->last_packet) { MVMuint16 last_remaining = data->last_packet_end - data->last_packet_start; if (bytes <= last_remaining) { /* There's enough, and it's sufficient for the request. Extract it * and return, discarding the last packet buffer if we drain it. */ *buf = MVM_malloc(bytes); memcpy(*buf, data->last_packet + data->last_packet_start, bytes); if (bytes == last_remaining) { MVM_free(data->last_packet); data->last_packet = NULL; } else { data->last_packet_start += bytes; } return bytes; } else { /* Something, but not enough. Take the last packet for use, then * we'll read another one. */ use_last_packet = data->last_packet; use_last_packet_start = data->last_packet_start; use_last_packet_end = data->last_packet_end; data->last_packet = NULL; } } /* If we get here, we need to read another packet. */ read_one_packet(tc, data); /* Now assemble the result. */ if (data->last_packet && use_last_packet) { /* Need to assemble it from two places. */ MVMuint32 last_available = use_last_packet_end - use_last_packet_start; MVMuint32 available = last_available + data->last_packet_end; if (bytes > available) bytes = available; *buf = MVM_malloc(bytes); memcpy(*buf, use_last_packet + use_last_packet_start, last_available); memcpy(*buf + last_available, data->last_packet, bytes - last_available); if (bytes == available) { /* We used all of the just-read packet. */ MVM_free(data->last_packet); data->last_packet = NULL; } else { /* Still something left in the just-read packet for next time. */ data->last_packet_start += bytes - last_available; } } else if (data->last_packet) { /* Only data from the just-read packet. */ if (bytes >= data->last_packet_end) { /* We need all of it, so no copying needed, just hand it back. */ *buf = data->last_packet; bytes = data->last_packet_end; data->last_packet = NULL; } else { /* Only need some of it. */ *buf = MVM_malloc(bytes); memcpy(*buf, data->last_packet, bytes); data->last_packet_start += bytes; } } else if (use_last_packet) { /* Nothing read this time, so at the end. Drain previous packet data * and mark EOF. */ bytes = use_last_packet_end - use_last_packet_start; *buf = MVM_malloc(bytes); memcpy(*buf, use_last_packet + use_last_packet_start, bytes); data->eof = 1; } else { /* Nothing to hand back; at EOF. */ *buf = NULL; bytes = 0; data->eof = 1; } return bytes; }
void MVM_coerce_istrue(MVMThreadContext *tc, MVMObject *obj, MVMRegister *res_reg, MVMuint8 *true_addr, MVMuint8 *false_addr, MVMuint8 flip) { MVMint64 result; if (MVM_is_null(tc, obj)) { result = 0; } else { MVMBoolificationSpec *bs = obj->st->boolification_spec; switch (bs == NULL ? MVM_BOOL_MODE_NOT_TYPE_OBJECT : bs->mode) { case MVM_BOOL_MODE_CALL_METHOD: { MVMObject *code = MVM_frame_find_invokee(tc, bs->method, NULL); MVMCallsite *inv_arg_callsite = MVM_callsite_get_common(tc, MVM_CALLSITE_ID_INV_ARG); if (res_reg) { /* We need to do the invocation, and set this register * the result. Then we just do the call. For the flip * case, just set up special return handler to flip * the register. */ MVM_args_setup_thunk(tc, res_reg, MVM_RETURN_INT, inv_arg_callsite); tc->cur_frame->args[0].o = obj; if (flip) { tc->cur_frame->special_return = flip_return; tc->cur_frame->special_return_data = res_reg; } STABLE(code)->invoke(tc, code, inv_arg_callsite, tc->cur_frame->args); } else { /* Need to set up special return hook. */ BoolMethReturnData *data = MVM_malloc(sizeof(BoolMethReturnData)); data->true_addr = true_addr; data->false_addr = false_addr; data->flip = flip; tc->cur_frame->special_return = boolify_return; tc->cur_frame->special_return_data = data; MVM_args_setup_thunk(tc, &data->res_reg, MVM_RETURN_INT, inv_arg_callsite); tc->cur_frame->args[0].o = obj; STABLE(code)->invoke(tc, code, inv_arg_callsite, tc->cur_frame->args); return; } break; } case MVM_BOOL_MODE_UNBOX_INT: result = !IS_CONCRETE(obj) || REPR(obj)->box_funcs.get_int(tc, STABLE(obj), obj, OBJECT_BODY(obj)) == 0 ? 0 : 1; break; case MVM_BOOL_MODE_UNBOX_NUM: result = !IS_CONCRETE(obj) || REPR(obj)->box_funcs.get_num(tc, STABLE(obj), obj, OBJECT_BODY(obj)) == 0.0 ? 0 : 1; break; case MVM_BOOL_MODE_UNBOX_STR_NOT_EMPTY: { MVMString *str; if (!IS_CONCRETE(obj)) { result = 0; break; } str = REPR(obj)->box_funcs.get_str(tc, STABLE(obj), obj, OBJECT_BODY(obj)); result = MVM_coerce_istrue_s(tc, str); break; } case MVM_BOOL_MODE_UNBOX_STR_NOT_EMPTY_OR_ZERO: { MVMString *str; if (!IS_CONCRETE(obj)) { result = 0; break; } str = REPR(obj)->box_funcs.get_str(tc, STABLE(obj), obj, OBJECT_BODY(obj)); result = str == NULL || !IS_CONCRETE(str) || (MVM_string_graphs(tc, str) == 1 && MVM_string_get_grapheme_at_nocheck(tc, str, 0) == 48) ? 0 : 1; break; } case MVM_BOOL_MODE_NOT_TYPE_OBJECT: result = !IS_CONCRETE(obj) ? 0 : 1; break; case MVM_BOOL_MODE_BIGINT: result = IS_CONCRETE(obj) ? MVM_bigint_bool(tc, obj) : 0; break; case MVM_BOOL_MODE_ITER: result = IS_CONCRETE(obj) ? MVM_iter_istrue(tc, (MVMIter *)obj) : 0; break; case MVM_BOOL_MODE_HAS_ELEMS: result = IS_CONCRETE(obj) ? MVM_repr_elems(tc, obj) != 0 : 0; break; default: MVM_exception_throw_adhoc(tc, "Invalid boolification spec mode used"); } } if (flip) result = result ? 0 : 1; if (res_reg) { res_reg->i64 = result; } else { if (result) *(tc->interp_cur_op) = true_addr; else *(tc->interp_cur_op) = false_addr; } }
/* This works out an allocation strategy for the object. It takes care of * "inlining" storage of attributes that are natively typed, as well as * noting unbox targets. */ static void compute_allocation_strategy(MVMThreadContext *tc, MVMObject *repr_info, MVMCStructREPRData *repr_data) { /* Compute index mapping table and get flat list of attributes. */ MVMObject *flat_list = index_mapping_and_flat_list(tc, repr_info, repr_data); /* If we have no attributes in the index mapping, then just the header. */ if (repr_data->name_to_index_mapping[0].class_key == NULL) { repr_data->struct_size = 1; /* avoid 0-byte malloc */ } /* Otherwise, we need to compute the allocation strategy. */ else { /* We track the size of the struct, which is what we'll want offsets into. */ MVMint32 cur_size = 0; /* The structure itself will be the multiple of its biggest element in size. * So we keep track of that biggest element. */ MVMint32 multiple_of = 1; /* Get number of attributes and set up various counters. */ MVMint32 num_attrs = MVM_repr_elems(tc, flat_list); MVMint32 info_alloc = num_attrs == 0 ? 1 : num_attrs; MVMint32 cur_obj_attr = 0; MVMint32 cur_init_slot = 0; MVMint32 i; /* Allocate location/offset arrays and GC mark info arrays. */ repr_data->num_attributes = num_attrs; repr_data->attribute_locations = (MVMint32 *) MVM_malloc(info_alloc * sizeof(MVMint32)); repr_data->struct_offsets = (MVMint32 *) MVM_malloc(info_alloc * sizeof(MVMint32)); repr_data->flattened_stables = (MVMSTable **) MVM_calloc(info_alloc, sizeof(MVMObject *)); repr_data->member_types = (MVMObject **) MVM_calloc(info_alloc, sizeof(MVMObject *)); /* Go over the attributes and arrange their allocation. */ for (i = 0; i < num_attrs; i++) { /* Fetch its type; see if it's some kind of unboxed type. */ MVMObject *attr = MVM_repr_at_pos_o(tc, flat_list, i); MVMObject *type = MVM_repr_at_key_o(tc, attr, tc->instance->str_consts.type); MVMObject *inlined_val = MVM_repr_at_key_o(tc, attr, tc->instance->str_consts.inlined); MVMint64 inlined = !MVM_is_null(tc, inlined_val) && MVM_repr_get_int(tc, inlined_val); MVMint32 bits = sizeof(void *) * 8; MVMint32 align = ALIGNOF(void *); if (!MVM_is_null(tc, type)) { /* See if it's a type that we know how to handle in a C struct. */ const MVMStorageSpec *spec = REPR(type)->get_storage_spec(tc, STABLE(type)); MVMint32 type_id = REPR(type)->ID; if (spec->inlineable == MVM_STORAGE_SPEC_INLINED && (spec->boxed_primitive == MVM_STORAGE_SPEC_BP_INT || spec->boxed_primitive == MVM_STORAGE_SPEC_BP_NUM)) { /* It's a boxed int or num; pretty easy. It'll just live in the * body of the struct. Instead of masking in i here (which * would be the parallel to how we handle boxed types) we * repurpose it to store the bit-width of the type, so * that get_attribute_ref can find it later. */ bits = spec->bits; align = spec->align; repr_data->attribute_locations[i] = (bits << MVM_CSTRUCT_ATTR_SHIFT) | MVM_CSTRUCT_ATTR_IN_STRUCT; repr_data->flattened_stables[i] = STABLE(type); if (REPR(type)->initialize) { if (!repr_data->initialize_slots) repr_data->initialize_slots = (MVMint32 *) MVM_calloc(info_alloc + 1, sizeof(MVMint32)); repr_data->initialize_slots[cur_init_slot] = i; cur_init_slot++; } } else if (spec->can_box & MVM_STORAGE_SPEC_CAN_BOX_STR) { /* It's a string of some kind. */ repr_data->num_child_objs++; repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CSTRUCT_ATTR_SHIFT) | MVM_CSTRUCT_ATTR_STRING; repr_data->member_types[i] = type; repr_data->flattened_stables[i] = STABLE(type); if (REPR(type)->initialize) { if (!repr_data->initialize_slots) repr_data->initialize_slots = (MVMint32 *) MVM_calloc(info_alloc + 1, sizeof(MVMint32)); repr_data->initialize_slots[cur_init_slot] = i; cur_init_slot++; } } else if (type_id == MVM_REPR_ID_MVMCArray) { /* It's a CArray of some kind. */ repr_data->num_child_objs++; repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CSTRUCT_ATTR_SHIFT) | MVM_CSTRUCT_ATTR_CARRAY; repr_data->member_types[i] = type; } else if (type_id == MVM_REPR_ID_MVMCStruct) { /* It's a CStruct. */ repr_data->num_child_objs++; repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CSTRUCT_ATTR_SHIFT) | MVM_CSTRUCT_ATTR_CSTRUCT; repr_data->member_types[i] = type; if (inlined) { MVMCStructREPRData *cstruct_repr_data = (MVMCStructREPRData *)STABLE(type)->REPR_data; bits = cstruct_repr_data->struct_size * 8; align = cstruct_repr_data->struct_size; repr_data->attribute_locations[i] |= MVM_CSTRUCT_ATTR_INLINED; } } else if (type_id == MVM_REPR_ID_MVMCPPStruct) { /* It's a CPPStruct. */ repr_data->num_child_objs++; repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CSTRUCT_ATTR_SHIFT) | MVM_CSTRUCT_ATTR_CPPSTRUCT; repr_data->member_types[i] = type; if (inlined) { MVMCPPStructREPRData *cppstruct_repr_data = (MVMCPPStructREPRData *)STABLE(type)->REPR_data; bits = cppstruct_repr_data->struct_size * 8; align = cppstruct_repr_data->struct_size; repr_data->attribute_locations[i] |= MVM_CSTRUCT_ATTR_INLINED; } } else if (type_id == MVM_REPR_ID_MVMCUnion) { /* It's a CUnion. */ repr_data->num_child_objs++; repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CSTRUCT_ATTR_SHIFT) | MVM_CSTRUCT_ATTR_CUNION; repr_data->member_types[i] = type; if (inlined) { MVMCUnionREPRData *cunion_repr_data = (MVMCUnionREPRData *)STABLE(type)->REPR_data; bits = cunion_repr_data->struct_size * 8; align = cunion_repr_data->struct_size; repr_data->attribute_locations[i] |= MVM_CSTRUCT_ATTR_INLINED; } } else if (type_id == MVM_REPR_ID_MVMCPointer) { /* It's a CPointer. */ repr_data->num_child_objs++; repr_data->attribute_locations[i] = (cur_obj_attr++ << MVM_CSTRUCT_ATTR_SHIFT) | MVM_CSTRUCT_ATTR_CPTR; repr_data->member_types[i] = type; } else { MVM_exception_throw_adhoc(tc, "CStruct representation only handles int, num, CArray, CPointer, CStruct, CPPStruct and CUnion"); } } else { MVM_exception_throw_adhoc(tc, "CStruct representation requires the types of all attributes to be specified"); } if (bits % 8) { MVM_exception_throw_adhoc(tc, "CStruct only supports native types that are a multiple of 8 bits wide (was passed: %"PRId32")", bits); } /* Do allocation. */ /* C structure needs careful alignment. If cur_size is not aligned * to align bytes (cur_size % align), make sure it is before we * add the next element. */ if (cur_size % align) { cur_size += align - cur_size % align; } repr_data->struct_offsets[i] = cur_size; cur_size += bits / 8; if (bits / 8 > multiple_of) multiple_of = bits / 8; } /* Finally, put computed allocation size in place; it's body size plus * header size. Also number of markables and sentinels. */ if (multiple_of > sizeof(void *)) multiple_of = sizeof(void *); repr_data->struct_size = ceil((double)cur_size / (double)multiple_of) * multiple_of; if (repr_data->initialize_slots) repr_data->initialize_slots[cur_init_slot] = -1; } }
char * MVM_bytecode_dump(MVMThreadContext *tc, MVMCompUnit *cu) { MVMuint32 s = 1024; MVMuint32 l = 0; MVMuint32 i, j, k; char *o = MVM_calloc(s, sizeof(char)); char ***frame_lexicals = MVM_malloc(sizeof(char **) * cu->body.num_frames); MVMString *name = MVM_string_utf8_decode(tc, tc->instance->VMString, "", 0); a("\nMoarVM dump of binary compilation unit:\n\n"); for (k = 0; k < cu->body.num_scs; k++) { char *tmpstr = MVM_string_utf8_encode_C_string( tc, MVM_cu_string(tc, cu, cu->body.sc_handle_idxs[k])); a(" SC_%u : %s\n", k, tmpstr); MVM_free(tmpstr); } for (k = 0; k < cu->body.num_callsites; k++) { MVMCallsite *callsite = cu->body.callsites[k]; MVMuint16 arg_count = callsite->arg_count; MVMuint16 nameds_count = 0; a(" Callsite_%u :\n", k); a(" num_pos: %d\n", callsite->num_pos); a(" arg_count: %u\n", arg_count); for (j = 0, i = 0; j < arg_count; j++) { MVMCallsiteEntry csitee = callsite->arg_flags[i++]; a(" Arg %u :", i); if (csitee & MVM_CALLSITE_ARG_NAMED) { if (callsite->arg_names) { char *arg_name = MVM_string_utf8_encode_C_string(tc, callsite->arg_names[nameds_count++]); a(" named(%s)", arg_name); MVM_free(arg_name); } else { a(" named"); } j++; } else if (csitee & MVM_CALLSITE_ARG_FLAT_NAMED) { a(" flatnamed"); } else if (csitee & MVM_CALLSITE_ARG_FLAT) { a(" flat"); } else a(" positional"); if (csitee & MVM_CALLSITE_ARG_OBJ) a(" obj"); else if (csitee & MVM_CALLSITE_ARG_INT) a(" int"); else if (csitee & MVM_CALLSITE_ARG_NUM) a(" num"); else if (csitee & MVM_CALLSITE_ARG_STR) a(" str"); if (csitee & MVM_CALLSITE_ARG_FLAT) a(" flat"); a("\n"); } } for (k = 0; k < cu->body.num_frames; k++) MVM_bytecode_finish_frame(tc, cu, get_frame(tc, cu, k), 1); for (k = 0; k < cu->body.num_frames; k++) { MVMStaticFrame *frame = get_frame(tc, cu, k); MVMLexicalRegistry *current; char **lexicals; if (!frame->body.fully_deserialized) { MVM_bytecode_finish_frame(tc, cu, frame, 1); } lexicals = (char **)MVM_malloc(sizeof(char *) * frame->body.num_lexicals); frame_lexicals[k] = lexicals; HASH_ITER(tc, hash_handle, frame->body.lexical_names, current, { name->body.storage.blob_32 = (MVMint32 *)current->hash_handle.key; name->body.num_graphs = (MVMuint32)current->hash_handle.keylen / sizeof(MVMGrapheme32); lexicals[current->value] = MVM_string_utf8_encode_C_string(tc, name); });
MVMint64 MVM_proc_shell(MVMThreadContext *tc, MVMString *cmd, MVMString *cwd, MVMObject *env, MVMObject *in, MVMObject *out, MVMObject *err, MVMint64 flags) { MVMint64 result = 0, spawn_result; uv_process_t *process = MVM_calloc(1, sizeof(uv_process_t)); uv_process_options_t process_options = {0}; uv_stdio_container_t process_stdio[3]; int i; char * const cmdin = MVM_string_utf8_c8_encode_C_string(tc, cmd); char * const _cwd = MVM_string_utf8_c8_encode_C_string(tc, cwd); const MVMuint64 size = MVM_repr_elems(tc, env); MVMIter * const iter = (MVMIter *)MVM_iter(tc, env); char **_env = MVM_malloc((size + 1) * sizeof(char *)); #ifdef _WIN32 const MVMuint16 acp = GetACP(); /* We should get ACP at runtime. */ char * const _cmd = ANSIToUTF8(acp, getenv("ComSpec")); char *args[3]; args[0] = "/c"; args[1] = cmdin; args[2] = NULL; #else char * const _cmd = "/bin/sh"; char *args[4]; args[0] = "/bin/sh"; args[1] = "-c"; args[2] = cmdin; args[3] = NULL; #endif INIT_ENV(); setup_process_stdio(tc, in, process, &process_stdio[0], 0, flags, "shell"); setup_process_stdio(tc, out, process, &process_stdio[1], 1, flags >> 3, "shell"); setup_process_stdio(tc, err, process, &process_stdio[2], 2, flags >> 6, "shell"); process_options.stdio = process_stdio; process_options.file = _cmd; process_options.args = args; process_options.cwd = _cwd; process_options.flags = UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS | UV_PROCESS_WINDOWS_HIDE; process_options.env = _env; process_options.stdio_count = 3; process_options.exit_cb = spawn_on_exit; if (flags & (MVM_PIPE_CAPTURE_IN | MVM_PIPE_CAPTURE_OUT | MVM_PIPE_CAPTURE_ERR)) { process->data = MVM_calloc(1, sizeof(MVMint64)); uv_ref((uv_handle_t *)process); spawn_result = uv_spawn(tc->loop, process, &process_options); if (spawn_result) result = spawn_result; } else { process->data = &result; uv_ref((uv_handle_t *)process); spawn_result = uv_spawn(tc->loop, process, &process_options); if (spawn_result) result = spawn_result; else uv_run(tc->loop, UV_RUN_DEFAULT); } FREE_ENV(); MVM_free(_cwd); #ifdef _WIN32 MVM_free(_cmd); #endif MVM_free(cmdin); uv_unref((uv_handle_t *)process); return result; }
static void bytecode_dump_frame_internal(MVMThreadContext *tc, MVMStaticFrame *frame, MVMSpeshCandidate *maybe_candidate, MVMuint8 *frame_cur_op, char ***frame_lexicals, char **oo, MVMuint32 *os, MVMuint32 *ol) { /* since "references" are not a thing in C, keep a local copy of these * and update the passed-in pointers at the end of the function */ char *o = *oo; MVMuint32 s = *os; MVMuint32 l = *ol; MVMuint32 i, j, k; /* mostly stolen from validation.c */ MVMStaticFrame *static_frame = frame; MVMuint32 bytecode_size = maybe_candidate ? maybe_candidate->bytecode_size : static_frame->body.bytecode_size; MVMuint8 *bytecode_start = maybe_candidate ? maybe_candidate->bytecode : static_frame->body.bytecode; MVMuint8 *bytecode_end = bytecode_start + bytecode_size; /* current position in the bytestream */ MVMuint8 *cur_op = bytecode_start; /* positions in the bytestream that are starts of ops and goto targets */ MVMuint8 *labels = MVM_calloc(1, bytecode_size); MVMuint32 *jumps = MVM_calloc(1, sizeof(MVMuint32) * bytecode_size); char **lines = MVM_malloc(sizeof(char *) * bytecode_size); MVMuint32 *linelocs = MVM_malloc(sizeof(MVMuint32) * bytecode_size); MVMuint32 lineno = 0; MVMuint32 lineloc; MVMuint16 op_num; const MVMOpInfo *op_info; MVMuint32 operand_size = 0; unsigned char op_rw; unsigned char op_type; unsigned char op_flags; MVMOpInfo tmp_extop_info; /* stash the outer output buffer */ MVMuint32 sP = s; MVMuint32 lP = l; char *oP = o; char *tmpstr; char mark_this_line = 0; MVMCompUnit *cu = static_frame->body.cu; while (cur_op < bytecode_end - 1) { /* allocate a line buffer */ s = 200; l = 0; o = MVM_calloc(s, sizeof(char)); lineloc = cur_op - bytecode_start; /* mark that this line starts at this point in the bytestream */ linelocs[lineno] = lineloc; /* mark that this point in the bytestream is an op boundary */ labels[lineloc] |= MVM_val_op_boundary; mark_this_line = 0; if (frame_cur_op) { if (frame_cur_op == cur_op || frame_cur_op == cur_op + 2) { mark_this_line = 1; } } if (mark_this_line) { a("-> "); } else { a(" "); } op_num = *((MVMint16 *)cur_op); cur_op += 2; if (op_num < MVM_OP_EXT_BASE) { op_info = MVM_op_get_op(op_num); if (op_info) a("%-18s ", op_info->name); else a("invalid OP "); } else { MVMint16 ext_op_num = op_num - MVM_OP_EXT_BASE; if (0 <= ext_op_num && ext_op_num < cu->body.num_extops) { MVMExtOpRecord r = cu->body.extops[ext_op_num]; MVMuint8 j; memset(&tmp_extop_info, 0, sizeof(MVMOpInfo)); tmp_extop_info.name = MVM_string_utf8_encode_C_string(tc, r.name); memcpy(tmp_extop_info.operands, r.operand_descriptor, 8); for (j = 0; j < 8; j++) if (tmp_extop_info.operands[j]) tmp_extop_info.num_operands++; else break; op_info = &tmp_extop_info; a("%-12s ", tmp_extop_info.name); MVM_free((void *)tmp_extop_info.name); tmp_extop_info.name = NULL; } else { a("Extension op %d out of range", (int)op_num); } } if (!op_info) continue; for (i = 0; i < op_info->num_operands; i++) { if (i) a(", "); op_flags = op_info->operands[i]; op_rw = op_flags & MVM_operand_rw_mask; op_type = op_flags & MVM_operand_type_mask; if (op_rw == MVM_operand_literal) { switch (op_type) { case MVM_operand_int8: operand_size = 1; a("%"PRId8, GET_I8(cur_op, 0)); break; case MVM_operand_int16: operand_size = 2; a("%"PRId16, GET_I16(cur_op, 0)); break; case MVM_operand_int32: operand_size = 4; a("%"PRId32, GET_I32(cur_op, 0)); break; case MVM_operand_int64: operand_size = 8; a("%"PRId64, MVM_BC_get_I64(cur_op, 0)); break; case MVM_operand_uint8: operand_size = 1; a("%"PRIu8, GET_I8(cur_op, 0)); break; case MVM_operand_uint16: operand_size = 2; a("%"PRIu16, GET_I16(cur_op, 0)); break; case MVM_operand_uint32: operand_size = 4; a("%"PRIu32, GET_I32(cur_op, 0)); break; case MVM_operand_uint64: operand_size = 8; a("%"PRIu64, MVM_BC_get_I64(cur_op, 0)); break; case MVM_operand_num32: operand_size = 4; a("%f", GET_N32(cur_op, 0)); break; case MVM_operand_num64: operand_size = 8; a("%f", MVM_BC_get_N64(cur_op, 0)); break; case MVM_operand_callsite: operand_size = 2; a("Callsite_%"PRIu16, GET_UI16(cur_op, 0)); break; case MVM_operand_coderef: operand_size = 2; a("Frame_%"PRIu16, GET_UI16(cur_op, 0)); break; case MVM_operand_str: operand_size = 4; if (GET_UI32(cur_op, 0) < cu->body.num_strings) { tmpstr = MVM_string_utf8_encode_C_string( tc, MVM_cu_string(tc, cu, GET_UI32(cur_op, 0))); /* XXX C-string-literal escape the \ and ' and line breaks and non-ascii someday */ a("'%s'", tmpstr); MVM_free(tmpstr); } else a("invalid string index: %d", GET_UI32(cur_op, 0)); break; case MVM_operand_ins: operand_size = 4; /* luckily all the ins operands are at the end of op operands, so I can wait to resolve the label to the end. */ if (GET_UI32(cur_op, 0) < bytecode_size) { labels[GET_UI32(cur_op, 0)] |= MVM_val_branch_target; jumps[lineno] = GET_UI32(cur_op, 0); } break; case MVM_operand_obj: /* not sure what a literal object is */ operand_size = 4; break; case MVM_operand_spesh_slot: operand_size = 2; a("sslot(%d)", GET_UI16(cur_op, 0)); break; default: fprintf(stderr, "what is an operand of type %d??\n", op_type); abort(); /* never reached, silence compiler warnings */ } } else if (op_rw == MVM_operand_read_reg || op_rw == MVM_operand_write_reg) { /* register operand */ MVMuint8 frame_has_inlines = maybe_candidate && maybe_candidate->num_inlines ? 1 : 0; MVMuint16 *local_types = frame_has_inlines ? maybe_candidate->local_types : frame->body.local_types; MVMuint16 num_locals = frame_has_inlines ? maybe_candidate->num_locals : frame->body.num_locals; operand_size = 2; a("loc_%u_%s", GET_REG(cur_op, 0), get_typename(local_types[GET_REG(cur_op, 0)])); } else if (op_rw == MVM_operand_read_lex || op_rw == MVM_operand_write_lex) { /* lexical operand */ MVMuint16 idx, frames, m; MVMStaticFrame *applicable_frame = static_frame; operand_size = 4; idx = GET_UI16(cur_op, 0); frames = GET_UI16(cur_op, 2); m = frames; while (m > 0) { applicable_frame = applicable_frame->body.outer; m--; } /* inefficient, I know. should use a hash. */ for (m = 0; m < cu->body.num_frames; m++) { if (get_frame(tc, cu, m) == applicable_frame) { char *lexname = frame_lexicals ? frame_lexicals[m][idx] : "lex??"; a("lex_Frame_%u_%s_%s", m, lexname, get_typename(applicable_frame->body.lexical_types[idx])); } } } cur_op += operand_size; } lines[lineno++] = o; } { MVMuint32 *linelabels = MVM_calloc(lineno, sizeof(MVMuint32)); MVMuint32 byte_offset = 0; MVMuint32 line_number = 0; MVMuint32 label_number = 1; MVMuint32 *annotations = MVM_calloc(lineno, sizeof(MVMuint32)); for (; byte_offset < bytecode_size; byte_offset++) { if (labels[byte_offset] & MVM_val_branch_target) { /* found a byte_offset where a label should be. now crawl up through the lines to find which line starts there */ while (line_number < lineno && linelocs[line_number] != byte_offset) line_number++; if (line_number < lineno) linelabels[line_number] = label_number++; } } o = oP; l = lP; s = sP; i = 0; /* resolve annotation line numbers */ for (j = 0; j < frame->body.num_annotations; j++) { MVMuint32 ann_offset = GET_UI32(frame->body.annotations_data, j*12); for (; i < lineno; i++) { if (linelocs[i] == ann_offset) { annotations[i] = j + 1; break; } } } for (j = 0; j < lineno; j++) { if (annotations[j]) { MVMuint16 shi = GET_UI16(frame->body.annotations_data + 4, (annotations[j] - 1)*12); tmpstr = MVM_string_utf8_encode_C_string( tc, MVM_cu_string(tc, cu, shi < cu->body.num_strings ? shi : 0)); a(" annotation: %s:%u\n", tmpstr, GET_UI32(frame->body.annotations_data, (annotations[j] - 1)*12 + 8)); MVM_free(tmpstr); } if (linelabels[j]) a(" label_%u:\n", linelabels[j]); a("%05d %s", j, lines[j]); MVM_free(lines[j]); if (jumps[j]) { /* horribly inefficient for large frames. again, should use a hash */ line_number = 0; while (line_number < lineno && linelocs[line_number] != jumps[j]) line_number++; if (line_number < lineno) a("label_%u(%05u)", linelabels[line_number], line_number); else a("label (invalid: %05u)", jumps[j]); } a("\n"); } MVM_free(lines); MVM_free(jumps); MVM_free(linelocs); MVM_free(linelabels); MVM_free(labels); MVM_free(annotations); } *oo = o; *os = s; *ol = l; }
static MVMString * take_chars(MVMThreadContext *tc, MVMDecodeStream *ds, MVMint32 chars, MVMint32 exclude) { MVMString *result; MVMint32 found = 0; MVMint32 result_found = 0; MVMint32 result_chars = chars - exclude; if (result_chars < 0) MVM_exception_throw_adhoc(tc, "DecodeStream take_chars: chars - exclude < 0 should never happen"); result = (MVMString *)MVM_repr_alloc_init(tc, tc->instance->VMString); result->body.storage_type = MVM_STRING_GRAPHEME_32; result->body.num_graphs = result_chars; /* In the best case, the head char buffer has exactly what we need. This * will typically happen when it a steady state of decoding lines. */ if (ds->chars_head->length == chars && ds->chars_head_pos == 0) { MVMDecodeStreamChars *cur_chars = ds->chars_head; result->body.storage.blob_32 = cur_chars->chars; ds->chars_head = cur_chars->next; if (ds->chars_head == NULL) ds->chars_tail = NULL; free_chars(tc, ds, cur_chars); } /* Otherwise, need to take and copy. */ else { result->body.storage.blob_32 = MVM_malloc(result_chars * sizeof(MVMGrapheme32)); while (found < chars) { MVMDecodeStreamChars *cur_chars = ds->chars_head; MVMint32 available = cur_chars->length - ds->chars_head_pos; if (available <= chars - found) { /* We need all that's left in this buffer and likely * more. */ MVMDecodeStreamChars *next_chars = cur_chars->next; if (available <= result_chars - result_found) { memcpy(result->body.storage.blob_32 + result_found, cur_chars->chars + ds->chars_head_pos, available * sizeof(MVMGrapheme32)); result_found += available; } else { MVMint32 to_copy = result_chars - result_found; memcpy(result->body.storage.blob_32 + result_found, cur_chars->chars + ds->chars_head_pos, to_copy * sizeof(MVMGrapheme32)); result_found += to_copy; } found += available; MVM_free(cur_chars->chars); free_chars(tc, ds, cur_chars); ds->chars_head = next_chars; ds->chars_head_pos = 0; if (ds->chars_head == NULL) ds->chars_tail = NULL; } else { /* There's enough in this buffer to satisfy us, and we'll leave * some behind. */ MVMint32 take = chars - found; MVMint32 to_copy = result_chars - result_found; memcpy(result->body.storage.blob_32 + result_found, cur_chars->chars + ds->chars_head_pos, to_copy * sizeof(MVMGrapheme32)); result_found += to_copy; found += take; ds->chars_head_pos += take; } } } return result; }
/* Decodes all the buffers, producing a string containing all the decoded * characters. */ MVMString * MVM_string_decodestream_get_all(MVMThreadContext *tc, MVMDecodeStream *ds) { MVMString *result = (MVMString *)MVM_repr_alloc_init(tc, tc->instance->VMString); result->body.storage_type = MVM_STRING_GRAPHEME_32; /* Decode all the things. */ run_decode(tc, ds, NULL, NULL); /* If there's no codepoint buffer, then return the empty string. */ if (!ds->chars_head) { result->body.storage.blob_32 = NULL; result->body.num_graphs = 0; } /* If there's exactly one resulting codepoint buffer and we swallowed none * of it, just use it. */ else if (ds->chars_head == ds->chars_tail && ds->chars_head_pos == 0) { /* Set up result string. */ result->body.storage.blob_32 = ds->chars_head->chars; result->body.num_graphs = ds->chars_head->length; /* Don't free the buffer's memory itself, just the holder, as we * stole that for the buffer into the string above. */ MVM_free(ds->chars_head); ds->chars_head = ds->chars_tail = NULL; } /* Otherwise, need to assemble all the things. */ else { /* Calculate length. */ MVMint32 length = 0, pos = 0; MVMDecodeStreamChars *cur_chars = ds->chars_head; while (cur_chars) { if (cur_chars == ds->chars_head) length += cur_chars->length - ds->chars_head_pos; else length += cur_chars->length; cur_chars = cur_chars->next; } /* Allocate a result buffer of the right size. */ result->body.storage.blob_32 = MVM_malloc(length * sizeof(MVMGrapheme32)); result->body.num_graphs = length; /* Copy all the things into the target, freeing as we go. */ cur_chars = ds->chars_head; while (cur_chars) { if (cur_chars == ds->chars_head) { MVMint32 to_copy = ds->chars_head->length - ds->chars_head_pos; memcpy(result->body.storage.blob_32 + pos, cur_chars->chars + ds->chars_head_pos, cur_chars->length * sizeof(MVMGrapheme32)); pos += to_copy; } else { memcpy(result->body.storage.blob_32 + pos, cur_chars->chars, cur_chars->length * sizeof(MVMGrapheme32)); pos += cur_chars->length; } cur_chars = cur_chars->next; } ds->chars_head = ds->chars_tail = NULL; } return result; }