Value.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "decompiler/Value.h"
  16. #include <boost/format.hpp>
  17. #include <map>
  18. #include <sstream>
  19. #include <string>
  20. static int dupindex = 0;
  21. static std::map<std::string, int> binary_op_precedence;
  22. static std::map<std::string, std::string> negate_map;
  23. void InitPrecedence() {
  24. binary_op_precedence["||"] = PRECEDENCE_LOGIC_OR;
  25. binary_op_precedence["&&"] = PRECEDENCE_LOGIC_AND;
  26. binary_op_precedence["|"] = PRECEDENCE_BIT_OR;
  27. binary_op_precedence["^"] = PRECEDENCE_BIT_XOR;
  28. binary_op_precedence["&"] = PRECEDENCE_BIT_AND;
  29. binary_op_precedence["=="] = PRECEDENCE_EQUALITY;
  30. binary_op_precedence["!="] = PRECEDENCE_EQUALITY;
  31. binary_op_precedence["<"] = PRECEDENCE_RELATION;
  32. binary_op_precedence["<="] = PRECEDENCE_RELATION;
  33. binary_op_precedence[">="] = PRECEDENCE_RELATION;
  34. binary_op_precedence[">"] = PRECEDENCE_RELATION;
  35. binary_op_precedence["<<"] = PRECEDENCE_SHIFT;
  36. binary_op_precedence[">>"] = PRECEDENCE_SHIFT;
  37. binary_op_precedence["+"] = PRECEDENCE_ADD;
  38. binary_op_precedence["-"] = PRECEDENCE_ADD;
  39. binary_op_precedence["*"] = PRECEDENCE_MULT;
  40. binary_op_precedence["/"] = PRECEDENCE_MULT;
  41. binary_op_precedence["%"] = PRECEDENCE_MULT;
  42. }
  43. void InitNegateMap() {
  44. negate_map["=="] = "!=";
  45. negate_map["!="] = "==";
  46. negate_map["<"] = ">=";
  47. negate_map["<="] = ">";
  48. negate_map[">="] = "<";
  49. negate_map[">"] = "<=";
  50. }
  51. Value::~Value(){}
  52. bool Value::IsInteger(){return false;}
  53. bool Value::IsAddress(){return false;}
  54. bool Value::IsSignedValue(){throw WrongTypeException();}
  55. int32 Value::GetSigned(){throw WrongTypeException();}
  56. uint32 Value::GetUnsigned(){throw WrongTypeException();}
  57. ValuePtr Value::Dup(std::ostream &output) {
  58. ValuePtr dup_value = new DupValue(++dupindex);
  59. output << dup_value << " = " << this << ";";
  60. return dup_value;
  61. }
  62. ValuePtr Value::Negate(){return new NegatedValue(this);}
  63. std::string Value::GetString() const {
  64. std::stringstream s;
  65. Print(s);
  66. return s.str();
  67. }
  68. int Value::GetPrecedence() const{return PRECEDENCE_NO;}
  69. IntValue::IntValue(int32 val, bool is_signed) : val_(val), signed_(is_signed){}
  70. IntValue::IntValue(uint32 val, bool is_signed) : val_(val), signed_(is_signed){}
  71. bool IntValue::IsInteger(){return true;}
  72. bool IntValue::IsSignedValue(){return signed_;}
  73. int32 IntValue::GetSigned(){return val_;}
  74. uint32 IntValue::GetUnsigned(){return (uint32)val_;}
  75. ValuePtr IntValue::Dup(std::ostream&){return new IntValue(val_, signed_);}
  76. std::ostream &IntValue::Print(std::ostream &output) const{
  77. if (signed_) output << (int32)val_;
  78. else output << (uint32)val_;
  79. return output;
  80. }
  81. AddressValue::AddressValue(uint32 addr): IntValue(addr, false){}
  82. bool AddressValue::IsAddress(){return true;}
  83. int32 AddressValue::GetSigned(){throw WrongTypeException();}
  84. ValuePtr AddressValue::Dup(std::ostream&){return new AddressValue(val_);}
  85. std::ostream &AddressValue::Print(std::ostream &output) const{
  86. return output << boost::format("0x%X") % val_;
  87. }
  88. RelAddressValue::RelAddressValue(uint32 base_addr, int32 offset):
  89. IntValue(offset, true), base_addr_(base_addr){};
  90. bool RelAddressValue::IsAddress(){return true;}
  91. uint32 RelAddressValue::GetUnsigned(){return base_addr_ + val_;}
  92. ValuePtr RelAddressValue::Dup(std::ostream&){return new RelAddressValue(base_addr_, val_);}
  93. std::ostream &RelAddressValue::Print(std::ostream &output) const{
  94. if (val_ < 0) return output << boost::format("-0x%X") % -val_;
  95. return output << boost::format("+0x%X") % val_;
  96. }
  97. DupValue::DupValue(int index): index_(index){}
  98. ValuePtr DupValue::Dup(std::ostream&){return this;}
  99. std::ostream &DupValue::Print(std::ostream &output) const{return output << "temp" << index_;}
  100. StringValue::StringValue(std::string str): str_(str){}
  101. std::ostream &StringValue::Print(std::ostream& output) const{
  102. return output << "\"" << str_ << "\"";
  103. }
  104. UnquotedStringValue::UnquotedStringValue(std::string str): StringValue(str){}
  105. std::ostream& UnquotedStringValue::Print(std::ostream& output) const{return output << str_;}
  106. VarValue::VarValue(std::string name): name_(name){}
  107. std::ostream &VarValue::Print(std::ostream& output) const{return output << name_;}
  108. ArrayValue::ArrayValue(const std::string& name, const ValueList& indexes):
  109. VarValue(name), indexes_(indexes){}
  110. std::ostream &ArrayValue::Print(std::ostream &output) const {
  111. output << name_;
  112. for (ValueList::const_iterator i = indexes_.begin(); i != indexes_.end(); ++ i)
  113. output << "[" << *i << "]";
  114. return output;
  115. }
  116. BinaryOpValue::BinaryOpValue(ValuePtr left, ValuePtr right, std::string oper):
  117. left_val_(left), right_val_(right), oper_(oper){}
  118. std::ostream &BinaryOpValue::Print(std::ostream &output) const {
  119. if (left_val_->GetPrecedence() > GetPrecedence()) output << "(" << left_val_ << ")";
  120. else output << left_val_;
  121. output << " " << oper_ << " ";
  122. if (right_val_->GetPrecedence() > GetPrecedence()) output << "(" << right_val_ << ")";
  123. else output << right_val_;
  124. return output;
  125. }
  126. int BinaryOpValue::GetPrecedence() const {
  127. if (binary_op_precedence.empty()) InitPrecedence();
  128. return binary_op_precedence[oper_];
  129. }
  130. ValuePtr BinaryOpValue::Negate(){
  131. if (negate_map.empty()) InitNegateMap();
  132. if (negate_map.find(oper_) == negate_map.end()) return Value::Negate();
  133. else return new BinaryOpValue(left_val_, right_val_, negate_map[oper_]);
  134. }
  135. UnaryOpValue::UnaryOpValue(ValuePtr operand, std::string oper, bool postfix):
  136. operand_(operand), oper_(oper), postfix_(postfix){}
  137. std::ostream &UnaryOpValue::Print(std::ostream &output) const {
  138. if (!postfix_) output << oper_;
  139. if (operand_->GetPrecedence() > GetPrecedence()) output << "(" << operand_ << ")";
  140. else output << operand_;
  141. if (postfix_) output << oper_;
  142. return output;
  143. }
  144. int UnaryOpValue::GetPrecedence() const {return PRECEDENCE_UNARY;}
  145. NegatedValue::NegatedValue(ValuePtr val): UnaryOpValue(val, "!", false){}
  146. ValuePtr NegatedValue::Negate(){return operand_;}
  147. CallValue::CallValue(std::string function, ValueList args): function_(function), args_(args){}
  148. std::ostream &CallValue::Print(std::ostream &output) const {
  149. output << function_ << "(";
  150. for (ValueList::const_iterator i = args_.begin(); i != args_.end(); ++i) {
  151. if (i != args_.begin()) output << ", ";
  152. output << *i;
  153. }
  154. output << ")";
  155. return output;
  156. }