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