void test_dup() { // Create a realistic MethodContext // Is there a better way to do this? Task* task = Task::create(state); // create a target CM CompiledMethod* target = CompiledMethod::create(state); target->iseq(state, InstructionSequence::create(state, 1)); target->iseq()->opcodes()->put(state, 0, Fixnum::from(InstructionSequence::insn_ret)); target->total_args(state, Fixnum::from(0)); target->required_args(state, target->total_args()); target->stack_size(state, Fixnum::from(10)); target->formalize(state); // create a containing CM CompiledMethod* cm = CompiledMethod::create(state); cm->iseq(state, InstructionSequence::create(state, 10)); cm->stack_size(state, Fixnum::from(10)); cm->local_count(state, Fixnum::from(0)); cm->literals(state, Tuple::create(state, 10)); Symbol* name = state->symbol("blah"); G(true_class)->method_table()->store(state, name, target); SendSite* ss = SendSite::create(state, name); cm->literals()->put(state, 0, ss); cm->formalize(state); MethodContext* ctx = MethodContext::create(state, Qnil, cm); task->make_active(ctx); task->push(Qtrue); // Dup right before we run so we can compare later MethodContext* dup = ctx->dup(state); // Compare the dup'd with the original TS_ASSERT_EQUALS(ctx->sender(), dup->sender()); TS_ASSERT_EQUALS(dup, dup->home()); TS_ASSERT_EQUALS(ctx->self(), dup->self()); TS_ASSERT_EQUALS(ctx->cm(), dup->cm()); TS_ASSERT_EQUALS(ctx->module(), dup->module()); TS_ASSERT_EQUALS(ctx->block(), dup->block()); TS_ASSERT_EQUALS(ctx->name(), dup->name()); TS_ASSERT_EQUALS(ctx->vmm, dup->vmm); TS_ASSERT_EQUALS(ctx->ip, dup->ip); TS_ASSERT_EQUALS(ctx->args, dup->args); TS_ASSERT_EQUALS(ctx->stack_size, dup->stack_size); TS_ASSERT_SAME_DATA(&ctx->js, &dup->js, sizeof(dup->js)); }
void test_cmethod() { std::string str = "M\n1\nn\nx\n12\nobject_equal\nx\n4\ntest\ni\n1\n0\nI\na\nI\n0\nI\n0\nI\n0\nn\np\n2\nI\n1\nI\n2\np\n1\np\n3\nI\n0\nI\n1\nI\n1\nx\n8\nnot_real\np\n1\nx\n4\nblah\n"; mar->sstream.str(str); Object* obj = mar->unmarshal(); TS_ASSERT(kind_of<CompiledMethod>(obj)); CompiledMethod* cm = as<CompiledMethod>(obj); TS_ASSERT_EQUALS(cm->ivars(), Qnil); TS_ASSERT_EQUALS(cm->primitive(), state->symbol("object_equal")); TS_ASSERT_EQUALS(cm->name(), state->symbol("test")); TS_ASSERT(tuple_equals(cm->iseq()->opcodes(), Tuple::from(state, 1, Fixnum::from(0)))); TS_ASSERT_EQUALS(cm->stack_size(), Fixnum::from(10)); TS_ASSERT_EQUALS(cm->local_count(), Fixnum::from(0)); TS_ASSERT_EQUALS(cm->required_args(), Fixnum::from(0)); TS_ASSERT_EQUALS(cm->total_args(), Fixnum::from(0)); TS_ASSERT_EQUALS(cm->splat(), Qnil); TS_ASSERT(tuple_equals(cm->literals(), Tuple::from(state, 2, Fixnum::from(1), Fixnum::from(2)))); TS_ASSERT(tuple_equals(cm->lines(), Tuple::from(state, 1, Tuple::from(state, 3, Fixnum::from(0), Fixnum::from(1), Fixnum::from(1))))); TS_ASSERT_EQUALS(cm->file(), state->symbol("not_real")); TS_ASSERT(tuple_equals(cm->local_names(), Tuple::from(state, 1, state->symbol("blah")))); }
void test_set_breakpoint_flags() { CompiledMethod* cm = CompiledMethod::create(state); Tuple* tup = Tuple::from(state, 1, state->symbol("@blah")); cm->literals(state, tup); InstructionSequence* iseq = InstructionSequence::create(state, 3); iseq->opcodes()->put(state, 0, Fixnum::from(InstructionSequence::insn_push_ivar)); iseq->opcodes()->put(state, 1, Fixnum::from(0)); iseq->opcodes()->put(state, 2, Fixnum::from(InstructionSequence::insn_push_nil)); cm->iseq(state, iseq); VMMethod* vmm = new VMMethod(state, cm); vmm->set_breakpoint_flags(state, 0, 1 << 24); TS_ASSERT_EQUALS(vmm->opcodes[0], (1U << 24) | static_cast<unsigned int>(InstructionSequence::insn_push_ivar)); vmm->set_breakpoint_flags(state, 0, 7 << 24); TS_ASSERT_EQUALS(vmm->opcodes[0], (7U << 24) | static_cast<unsigned int>(InstructionSequence::insn_push_ivar)); vmm->set_breakpoint_flags(state, 0, 0); TS_ASSERT_EQUALS(vmm->opcodes[0], static_cast<unsigned int>(InstructionSequence::insn_push_ivar)); vmm->set_breakpoint_flags(state, 1, 1); TS_ASSERT_EQUALS(vmm->opcodes[1], 0U); }
void test_specialize_transforms_ivars_to_slots() { CompiledMethod* cm = CompiledMethod::create(state); Tuple* tup = Tuple::from(state, 1, state->symbol("@blah")); cm->literals(state, tup); InstructionSequence* iseq = InstructionSequence::create(state, 3); iseq->opcodes()->put(state, 0, Fixnum::from(InstructionSequence::insn_push_ivar)); iseq->opcodes()->put(state, 1, Fixnum::from(0)); iseq->opcodes()->put(state, 2, Fixnum::from(InstructionSequence::insn_push_nil)); cm->iseq(state, iseq); VMMethod* vmm = new VMMethod(state, cm); Object::Info ti(ObjectType); ti.slots[state->symbol("@blah")->index()] = 5; ti.slot_locations.resize(6); ti.slot_locations[5] = 33; vmm->specialize(state, cm, &ti); TS_ASSERT_EQUALS(vmm->total, 3U); TS_ASSERT_EQUALS(vmm->opcodes[0], static_cast<unsigned int>(InstructionSequence::insn_push_my_offset)); TS_ASSERT_EQUALS(vmm->opcodes[1], 33U); TS_ASSERT_EQUALS(vmm->opcodes[2], static_cast<unsigned int>(InstructionSequence::insn_push_nil)); }
void CompiledMethod::Info::show(STATE, Object* self, int level) { CompiledMethod* cm = as<CompiledMethod>(self); class_header(state, self); indent_attribute(++level, "file"); cm->file()->show(state, level); indent_attribute(level, "iseq"); cm->iseq()->show(state, level); indent_attribute(level, "lines"); cm->lines()->show_simple(state, level); indent_attribute(level, "literals"); cm->literals()->show_simple(state, level); indent_attribute(level, "local_count"); cm->local_count()->show(state, level); indent_attribute(level, "local_names"); cm->local_names()->show_simple(state, level); indent_attribute(level, "name"); cm->name()->show(state, level); indent_attribute(level, "required_args"); cm->required_args()->show(state, level); indent_attribute(level, "scope"); cm->scope()->show(state, level); indent_attribute(level, "splat"); cm->splat()->show(state, level); indent_attribute(level, "stack_size"); cm->stack_size()->show(state, level); indent_attribute(level, "total_args"); cm->total_args()->show(state, level); #ifdef ENABLE_LLVM if(cm->backend_method_ && cm->backend_method_->jitted()) { llvm::outs() << "<LLVM>\n" << *cm->backend_method_->llvm_function() << "</LLVM>\n<MachineCode>\n"; LLVMState::show_machine_code( cm->backend_method_->jitted_impl(), cm->backend_method_->jitted_bytes()); llvm::outs() << "</MachineCode>\n"; } #endif close_body(level); }
CompiledMethod* UnMarshaller::get_cmethod() { size_t ver; stream >> ver; CompiledMethod* cm = CompiledMethod::create(state); cm->ivars(state, unmarshal()); cm->primitive(state, (Symbol*)unmarshal()); cm->name(state, (Symbol*)unmarshal()); cm->iseq(state, (InstructionSequence*)unmarshal()); cm->stack_size(state, (Fixnum*)unmarshal()); cm->local_count(state, (Fixnum*)unmarshal()); cm->required_args(state, (Fixnum*)unmarshal()); cm->total_args(state, (Fixnum*)unmarshal()); cm->splat(state, unmarshal()); cm->literals(state, (Tuple*)unmarshal()); cm->exceptions(state, (Tuple*)unmarshal()); cm->lines(state, (Tuple*)unmarshal()); cm->file(state, (Symbol*)unmarshal()); cm->local_names(state, (Tuple*)unmarshal()); cm->post_marshal(state); return cm; }
void test_create() { CompiledMethod* cm = CompiledMethod::create(state); Tuple* tup = Tuple::from(state, 1, state->symbol("blah")); cm->literals(state, tup); InstructionSequence* iseq = InstructionSequence::create(state, 1); iseq->opcodes()->put(state, 0, Fixnum::from(0)); cm->iseq(state, iseq); VMMethod* vmm = new VMMethod(state, cm); TS_ASSERT_EQUALS(vmm->total, 1U); TS_ASSERT_EQUALS(vmm->opcodes[0], 0U); }
void test_validate_ip() { CompiledMethod* cm = CompiledMethod::create(state); Tuple* tup = Tuple::from(state, 1, state->symbol("@blah")); cm->literals(state, tup); InstructionSequence* iseq = InstructionSequence::create(state, 3); iseq->opcodes()->put(state, 0, Fixnum::from(InstructionSequence::insn_push_ivar)); iseq->opcodes()->put(state, 1, Fixnum::from(0)); iseq->opcodes()->put(state, 2, Fixnum::from(InstructionSequence::insn_push_nil)); cm->iseq(state, iseq); VMMethod* vmm = new VMMethod(state, cm); TS_ASSERT_EQUALS(vmm->validate_ip(state, 0), true); TS_ASSERT_EQUALS(vmm->validate_ip(state, 1), false); TS_ASSERT_EQUALS(vmm->validate_ip(state, 2), true); }
void CompiledMethod::Info::show(STATE, Object* self, int level) { CompiledMethod* cm = as<CompiledMethod>(self); class_header(state, self); indent_attribute(++level, "file"); cm->file()->show(state, level); indent_attribute(level, "iseq"); cm->iseq()->show(state, level); indent_attribute(level, "lines"); cm->lines()->show_simple(state, level); indent_attribute(level, "literals"); cm->literals()->show_simple(state, level); indent_attribute(level, "local_count"); cm->local_count()->show(state, level); indent_attribute(level, "local_names"); cm->local_names()->show_simple(state, level); indent_attribute(level, "name"); cm->name()->show(state, level); indent_attribute(level, "required_args"); cm->required_args()->show(state, level); indent_attribute(level, "scope"); cm->scope()->show(state, level); indent_attribute(level, "splat"); cm->splat()->show(state, level); indent_attribute(level, "stack_size"); cm->stack_size()->show(state, level); indent_attribute(level, "total_args"); cm->total_args()->show(state, level); indent_attribute(level, "internalized"); if(!cm->backend_method_) { std::cout << "no\n"; } else { std::cout << "yes\n"; #ifdef ENABLE_LLVM VMMethod* v = cm->backend_method(); for(int i = 0; i < VMMethod::cMaxSpecializations; i++) { if(!v->specializations[i].jit_data) continue; llvm::Function* func = v->specializations[i].jit_data->llvm_function(); llvm::outs() << "<LLVM>\n" << *func << "</LLVM>\n<MachineCode>\n"; LLVMState::show_machine_code( v->specializations[i].jit_data->native_func(), v->specializations[i].jit_data->native_size()); llvm::outs() << "</MachineCode>\n"; } #endif } close_body(level); }
void test_get_breakpoint_flags() { CompiledMethod* cm = CompiledMethod::create(state); Tuple* tup = Tuple::from(state, 1, state->symbol("@blah")); cm->literals(state, tup); InstructionSequence* iseq = InstructionSequence::create(state, 3); iseq->opcodes()->put(state, 0, Fixnum::from(InstructionSequence::insn_push_ivar)); iseq->opcodes()->put(state, 1, Fixnum::from(0)); iseq->opcodes()->put(state, 2, Fixnum::from(4 << 24 | InstructionSequence::insn_push_nil)); cm->iseq(state, iseq); VMMethod* vmm = new VMMethod(state, cm); TS_ASSERT_EQUALS(vmm->get_breakpoint_flags(state, 0), 0U); TS_ASSERT_EQUALS(vmm->get_breakpoint_flags(state, 2), (4U << 24)); TS_ASSERT_EQUALS(vmm->get_breakpoint_flags(state, 1), 0U); }
void CompiledMethod::Info::show(STATE, Object* self, int level) { CompiledMethod* cm = as<CompiledMethod>(self); class_header(state, self); indent_attribute(++level, "exceptions"); cm->exceptions()->show_simple(state, level); indent_attribute(level, "file"); cm->file()->show(state, level); indent_attribute(level, "iseq"); cm->iseq()->show(state, level); indent_attribute(level, "lines"); cm->lines()->show_simple(state, level); indent_attribute(level, "literals"); cm->literals()->show_simple(state, level); indent_attribute(level, "local_count"); cm->local_count()->show(state, level); indent_attribute(level, "local_names"); cm->local_names()->show_simple(state, level); indent_attribute(level, "name"); cm->name()->show(state, level); indent_attribute(level, "required_args"); cm->required_args()->show(state, level); indent_attribute(level, "scope"); cm->scope()->show(state, level); indent_attribute(level, "splat"); cm->splat()->show(state, level); indent_attribute(level, "stack_size"); cm->stack_size()->show(state, level); indent_attribute(level, "total_args"); cm->total_args()->show(state, level); close_body(level); }