Ejemplo n.º 1
0
 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");
   }
 }
Ejemplo n.º 2
0
    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;
      }
    }