value.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include "value.h"
  22. #include <boost/format.hpp>
  23. #include <map>
  24. #include <sstream>
  25. #include <string>
  26. static int dupindex = 0;
  27. static std::map<std::string, int> binaryOpPrecedence;
  28. static std::map<std::string, std::string> negateMap;
  29. void initPrecedence() {
  30. binaryOpPrecedence["||"] = kLogicalOrPrecedence;
  31. binaryOpPrecedence["&&"] = kLogicalAndPrecedence;
  32. binaryOpPrecedence["|"] = kBitwiseOrPrecedence;
  33. binaryOpPrecedence["^"] = kBitwiseXorPrecedence;
  34. binaryOpPrecedence["&"] = kBitwiseAndPrecedence;
  35. binaryOpPrecedence["=="] = kEqualityOpPrecedence;
  36. binaryOpPrecedence["!="] = kEqualityOpPrecedence;
  37. binaryOpPrecedence["<"] = kRelationOpPrecedence;
  38. binaryOpPrecedence["<="] = kRelationOpPrecedence;
  39. binaryOpPrecedence[">="] = kRelationOpPrecedence;
  40. binaryOpPrecedence[">"] = kRelationOpPrecedence;
  41. binaryOpPrecedence["<<"] = kShiftOpPrecedence;
  42. binaryOpPrecedence[">>"] = kShiftOpPrecedence;
  43. binaryOpPrecedence["+"] = kAddOpPrecedence;
  44. binaryOpPrecedence["-"] = kAddOpPrecedence;
  45. binaryOpPrecedence["*"] = kMultOpPrecedence;
  46. binaryOpPrecedence["/"] = kMultOpPrecedence;
  47. binaryOpPrecedence["%"] = kMultOpPrecedence;
  48. }
  49. void initNegateMap() {
  50. negateMap["=="] = "!=";
  51. negateMap["!="] = "==";
  52. negateMap["<"] = ">=";
  53. negateMap["<="] = ">";
  54. negateMap[">="] = "<";
  55. negateMap[">"] = "<=";
  56. }
  57. bool Value::isInteger() {
  58. return false;
  59. }
  60. bool Value::isAddress() {
  61. return false;
  62. }
  63. bool Value::isSignedValue(){
  64. throw WrongTypeException();
  65. }
  66. int32 Value::getSigned(){
  67. throw WrongTypeException();
  68. }
  69. uint32 Value::getUnsigned(){
  70. throw WrongTypeException();
  71. }
  72. ValuePtr Value::dup(std::ostream &output) {
  73. ValuePtr dupValue = new DupValue(++dupindex);
  74. output << dupValue << " = " << this << ";";
  75. return dupValue;
  76. }
  77. ValuePtr Value::negate(){
  78. return new NegatedValue(this);
  79. }
  80. std::string Value::getString() const {
  81. std::stringstream s;
  82. print(s);
  83. return s.str();
  84. }
  85. int Value::precedence() const {
  86. return kNoPrecedence;
  87. }
  88. bool IntValue::isInteger() {
  89. return true;
  90. }
  91. bool IntValue::isSignedValue(){
  92. return _isSigned;
  93. }
  94. int32 IntValue::getSigned() {
  95. return _val;
  96. }
  97. uint32 IntValue::getUnsigned(){
  98. return (uint32)_val;
  99. }
  100. ValuePtr IntValue::dup(std::ostream&) {
  101. return new IntValue(_val, _isSigned);
  102. }
  103. std::ostream &IntValue::print(std::ostream &output) const {
  104. if (_isSigned)
  105. output << (int32)_val;
  106. else
  107. output << (uint32)_val;
  108. return output;
  109. }
  110. bool AddressValue::isAddress() {
  111. return true;
  112. }
  113. int32 AddressValue::getSigned(){
  114. throw WrongTypeException();
  115. }
  116. ValuePtr AddressValue::dup(std::ostream&) {
  117. return new AddressValue(_val);
  118. }
  119. std::ostream &AddressValue::print(std::ostream &output) const {
  120. return output << boost::format("0x%X") % _val;
  121. }
  122. bool RelAddressValue::isAddress() {
  123. return true;
  124. }
  125. uint32 RelAddressValue::getUnsigned(){
  126. return _baseaddr + _val;
  127. }
  128. ValuePtr RelAddressValue::dup(std::ostream&) {
  129. return new RelAddressValue(_baseaddr, _val);
  130. }
  131. std::ostream &RelAddressValue::print(std::ostream &output) const {
  132. if (_val < 0)
  133. return output << boost::format("-0x%X") % -_val;
  134. return output << boost::format("+0x%X") % _val;
  135. }
  136. ValuePtr DupValue::dup(std::ostream&) {
  137. return this;
  138. }
  139. std::ostream &DupValue::print(std::ostream &output) const {
  140. return output << "temp" << _idx;
  141. }
  142. std::ostream &StringValue::print(std::ostream &output) const {
  143. return output << "\"" << _str << "\"";
  144. }
  145. std::ostream& UnqotedStringValue::print(std::ostream& output) const
  146. {
  147. return output << _str;
  148. }
  149. std::ostream &VarValue::print(std::ostream &output) const {
  150. return output << _varName;
  151. }
  152. std::ostream &ArrayValue::print(std::ostream &output) const {
  153. output << _varName;
  154. for (ValueList::const_iterator i = _idxs.begin(); i != _idxs.end(); ++i)
  155. output << "[" << *i << "]";
  156. return output;
  157. }
  158. std::ostream &BinaryOpValue::print(std::ostream &output) const {
  159. if (_lhs->precedence() > precedence())
  160. output << "(" << _lhs << ")";
  161. else
  162. output << _lhs;
  163. output << " " << _op << " ";
  164. if (_rhs->precedence() > precedence())
  165. output << "(" << _rhs << ")";
  166. else
  167. output << _rhs;
  168. return output;
  169. }
  170. int BinaryOpValue::precedence() const {
  171. if (binaryOpPrecedence.empty())
  172. initPrecedence();
  173. return binaryOpPrecedence[_op];
  174. }
  175. ValuePtr BinaryOpValue::negate(){
  176. if (negateMap.empty())
  177. initNegateMap();
  178. if (negateMap.find(_op) == negateMap.end())
  179. return Value::negate();
  180. else
  181. return new BinaryOpValue(_lhs, _rhs, negateMap[_op]);
  182. }
  183. std::ostream &UnaryOpValue::print(std::ostream &output) const {
  184. if (!_isPostfix)
  185. output << _op;
  186. if (_operand->precedence() > precedence())
  187. output << "(" << _operand << ")";
  188. else
  189. output << _operand;
  190. if (_isPostfix)
  191. output << _op;
  192. return output;
  193. }
  194. int UnaryOpValue::precedence() const {
  195. return kUnaryOpPrecedence;
  196. }
  197. ValuePtr NegatedValue::negate(){
  198. return _operand;
  199. }
  200. std::ostream &CallValue::print(std::ostream &output) const {
  201. output << _funcName << "(";
  202. for (ValueList::const_iterator i = _args.begin(); i != _args.end(); ++i) {
  203. if (i != _args.begin())
  204. output << ", ";
  205. output << *i;
  206. }
  207. output << ")";
  208. return output;
  209. }