void DisassemblerContext::setup_term(const ValuePtr<>& term) { if (!m_visited_terms.insert(term).second) return; setup_term_name(term); switch (term->term_type()) { case term_apply: { ValuePtr<ApplyType> apply = value_cast<ApplyType>(term); setup_term(apply->recursive()); for (std::vector<ValuePtr<> >::const_iterator ii = apply->parameters().begin(), ie = apply->parameters().end(); ii != ie; ++ii) setup_term(*ii); break; } case term_exists: { ValuePtr<Exists> exists = value_cast<Exists>(term); setup_term(exists->result()); break; } case term_parameter_placeholder: case term_recursive_parameter: { setup_term(term->type()); break; } case term_functional: { class MyVisitor : public FunctionalValueVisitor { DisassemblerContext *m_self; public: MyVisitor(DisassemblerContext *self) : m_self(self) {} virtual void next(const ValuePtr<>& v) {if (v) m_self->setup_term(v);} }; MyVisitor my_visitor(this); value_cast<FunctionalValue>(term)->functional_visit(my_visitor); break; } case term_function_type: { ValuePtr<FunctionType> cast_term = value_cast<FunctionType>(term); const std::vector<ParameterType>& parameter_types = cast_term->parameter_types(); for (std::vector<ParameterType>::const_iterator ii = parameter_types.begin(), ie = parameter_types.end(); ii != ie; ++ii) setup_term(ii->value); setup_term(cast_term->result_type().value); break; } default: return; // Skip adding to definition list } if (TermDefinitionList *dl = term_definition_list(term)) dl->push_back(term); }
void DisassemblerContext::run_term(const ValuePtr<>& term) { switch (term->term_type()) { case term_function: { ValuePtr<Function> function = value_cast<Function>(term); setup_term_name(function); setup_function(function); build_unique_names(); print_definitions(m_global_definitions, "", true); print_function(function); break; } case term_block: { ValuePtr<Block> block = value_cast<Block>(term); m_in_function_mode = true; setup_term_name(block); setup_block(block); build_unique_names(); print_block(block, m_global_definitions); break; } default: m_in_function_mode = true; setup_term_definition(term); build_unique_names(); print_definitions(m_global_definitions); switch (term->term_type()) { case term_instruction: case term_phi: case term_function_parameter: case term_recursive: print_term_definition(term, true); break; default: break; } break; } }
void DisassemblerContext::print_term(const ValuePtr<>& term, bool bracket) { if (!term) { *m_output << "NULL"; return; } TermNameMap::iterator name_it = m_names.find(term); if (name_it != m_names.end()) { *m_output << TermNamePrinter(&name_it->second->name); return; } switch (term->term_type()) { case term_functional: { print_functional_term(value_cast<FunctionalValue>(term), bracket); break; } case term_function_type: { if (bracket) *m_output << '('; print_function_type_term(value_cast<FunctionType>(term), bracket); if (bracket) *m_output << ')'; break; } case term_apply: { print_apply_term(value_cast<ApplyType>(term), bracket); break; } case term_exists: { print_exists(value_cast<Exists>(term), bracket); break; } case term_recursive: *m_output << "[recursive]"; break; case term_recursive_parameter: *m_output << "[recursive parameter]"; break; case term_parameter_placeholder: *m_output << "[parameter placeholder]"; break; case term_upref_null: *m_output << "upref_null"; break; default: PSI_FAIL("unexpected term type - this term should have had a name assigned"); } }
void DisassemblerContext::print_term_definition(const ValuePtr<>& term, bool global) { *m_output << name(term) << " = "; switch (term->term_type()) { case term_functional: { if (global) *m_output << "define "; print_functional_term(value_cast<FunctionalValue>(term), false); *m_output << ";\n"; break; } case term_function_type: { if (global) *m_output << "define "; print_function_type_term(value_cast<FunctionType>(term), false); *m_output << ";\n"; break; } case term_instruction: { print_instruction_term(value_cast<Instruction>(term)); break; } case term_phi: { print_phi_term(value_cast<Phi>(term)); break; } case term_global_variable: { ValuePtr<GlobalVariable> gvar = value_cast<GlobalVariable>(term); *m_output << "global "; if (gvar->constant()) *m_output << "const "; print_term(gvar->value_type(), true); if (gvar->value()) { *m_output << ' '; print_term(gvar->value(), true); } *m_output << ";\n"; return; } case term_function: { print_function(value_cast<Function>(term)); return; } case term_function_parameter: { ValuePtr<FunctionParameter> parameter = value_cast<FunctionParameter>(term); ValuePtr<Function> function = parameter->function(); unsigned n = 0; for (Function::ParameterList::const_iterator ii = function->parameters().begin(), ie = function->parameters().end(); ii != ie; ++ii, ++n) { if (parameter == *ii) { *m_output << "[function parameter " << n << "]\n"; return; } } *m_output << "[invalid function parameter]\n"; return; } case term_apply: { if (global) *m_output << "define "; print_apply_term(value_cast<ApplyType>(term), false); *m_output << ";\n"; return; } case term_exists: { if (global) *m_output << "define "; print_exists(value_cast<Exists>(term), false); *m_output << ";\n"; return; } case term_recursive: { print_recursive(value_cast<RecursiveType>(term)); return; } case term_recursive_parameter: *m_output << "[recursive parameter]\n"; return; case term_parameter_placeholder: *m_output << "[parameter placeholder]\n"; return; case term_upref_null: *m_output << "upref_null\n"; return; default: PSI_FAIL("unexpected term type - cannot print a definition"); } }
void DisassemblerContext::setup_term_definition(const ValuePtr<>& term) { if (!m_defined_terms.insert(term).second) return; setup_term_name(term); switch (term->term_type()) { case term_recursive: { ValuePtr<RecursiveType> recursive = value_cast<RecursiveType>(term); for (RecursiveType::ParameterList::const_iterator ii = recursive->parameters().begin(), ie = recursive->parameters().end(); ii != ie; ++ii) { setup_term_name(*ii); setup_term((*ii)->type()); } if (recursive->result()) setup_term(recursive->result()); break; } case term_global_variable: { ValuePtr<GlobalVariable> gvar = value_cast<GlobalVariable>(term); setup_term(gvar->value_type()); if (gvar->value()) setup_term(gvar->value()); break; } case term_function: { ValuePtr<Function> function = value_cast<Function>(term); setup_function(function); break; } case term_function_parameter: { ValuePtr<FunctionParameter> param = value_cast<FunctionParameter>(term); setup_term(param->type()); break; } case term_instruction: { class MyVisitor : public InstructionVisitor { DisassemblerContext *m_self; public: MyVisitor(DisassemblerContext *self) : m_self(self) {} virtual void next(ValuePtr<>& v) {if (v) m_self->setup_term(v);} }; MyVisitor my_visitor(this); ValuePtr<Instruction> insn = value_cast<Instruction>(term); insn->instruction_visit(my_visitor); m_local_definitions[insn->block()].push_back(insn); break; } case term_phi: { ValuePtr<Phi> phi = value_cast<Phi>(term); const std::vector<PhiEdge>& edges = phi->edges(); for (std::vector<PhiEdge>::const_iterator ii = edges.begin(), ie = edges.end(); ii != ie; ++ii) { setup_term(ii->block); setup_term(ii->value); } break; } default: setup_term(term); break; } }