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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
#include "stdafx.h"
#include "Expr.h"
#include "Core/Str.h"
#include "Compiler/Exception.h"
#include "Compiler/Type.h"
namespace storm {
namespace bs {
Expr::Expr(SrcPos pos) : pos(pos) {}
ExprResult Expr::result() {
return ExprResult();
}
void Expr::code(CodeGen *to, CodeResult *var) {
assert(!var->needed());
}
Int Expr::castPenalty(Value to) {
return -1;
}
Bool Expr::isolate() {
return true;
}
SrcPos Expr::largePos() {
return pos;
}
Bool Expr::thisVariable() const {
return false;
}
/**
* Numeric literals.
*/
NumLiteral::NumLiteral(SrcPos pos, Int v) : Expr(pos), intValue(v), isInt(true), isSigned(true) {}
NumLiteral::NumLiteral(SrcPos pos, Long v) : Expr(pos), intValue(v), isInt(true), isSigned(true) {}
NumLiteral::NumLiteral(SrcPos pos, Word v) : Expr(pos), intValue(v), isInt(true), isSigned(false) {}
NumLiteral::NumLiteral(SrcPos pos, Double v) : Expr(pos), floatValue(v), isInt(false), isSigned(true) {}
void NumLiteral::setType(Str *suffix) {
Str::Iter b = suffix->begin();
if (b == suffix->end())
throw new (this) InternalError(S("Suffixes passed to NumLiteral may not be empty."));
Char ch = *b;
if (++b != suffix->end())
throw new (this) InternalError(S("Suffixes passed to NumLiteral must be exactly one character long!"));
if (isInt) {
switch (ch.codepoint()) {
case 'b':
type = StormInfo<Byte>::type(engine());
return;
case 'i':
if (isSigned) {
type = StormInfo<Int>::type(engine());
return;
}
break;
case 'n':
type = StormInfo<Nat>::type(engine());
return;
case 'l':
if (isSigned) {
type = StormInfo<Long>::type(engine());
return;
}
break;
case 'w':
type = StormInfo<Word>::type(engine());
return;
case 'f':
if (isSigned) {
type = StormInfo<Float>::type(engine());
return;
}
break;
case 'd':
if (isSigned) {
type = StormInfo<Double>::type(engine());
return;
}
break;
}
} else {
switch (ch.codepoint()) {
case 'f':
type = StormInfo<Float>::type(engine());
return;
case 'd':
type = StormInfo<Double>::type(engine());
return;
}
}
throw new (this) SyntaxError(pos, TO_S(engine(), S("Invalid suffix: ") << suffix));
}
void NumLiteral::toS(StrBuf *to) const {
if (isInt) {
if (isSigned) {
*to << intValue;
} else {
*to << S("0x") << hex((Word)intValue);
}
} else {
*to << floatValue;
}
if (type)
*to << S(" (") << type->name << S(")");
}
ExprResult NumLiteral::result() {
if (type)
return Value(type);
if (isInt)
if (isSigned)
return Value(StormInfo<Int>::type(engine()));
else
return Value(StormInfo<Nat>::type(engine()));
else
return Value(StormInfo<Float>::type(engine()));
}
Int NumLiteral::castPenalty(Value to) {
if (type)
return -1;
if (to.ref)
return -1;
Engine &e = engine();
if (isInt) {
// Prefer bigger types if multiple are possible.
if (to.type == StormInfo<Long>::type(e) && isSigned)
return 1;
if (to.type == StormInfo<Word>::type(e))
return 1;
if (to.type == StormInfo<Int>::type(e) && (abs(intValue) & 0x7FFFFFFF) == abs(intValue) && isSigned)
return 2;
if (to.type == StormInfo<Nat>::type(e) && (intValue & 0xFFFFFFFF) == intValue)
return 2;
if (to.type == StormInfo<Byte>::type(e) && (intValue & 0xFF) == intValue)
return 3;
if (to.type == StormInfo<Double>::type(e) && (abs(intValue) & 0x3FFFFFFFFFFFF) == abs(intValue) && isSigned)
// We allow up to 52 bits to automatically cast.
return 3;
if (to.type == StormInfo<Float>::type(e) && (abs(intValue) & 0xFFFF) == abs(intValue) && isSigned)
// We allow up to 16 bits to automatically cast.
return 4;
} else {
// We're fine with using doubles as well.
if (to.type == StormInfo<Double>::type(e))
return 1;
}
return -1;
}
void NumLiteral::code(CodeGen *s, CodeResult *r) {
using namespace code;
if (!r->needed())
return;
if (isInt)
intCode(s, r);
else
floatCode(s, r);
}
void NumLiteral::intCode(CodeGen *s, CodeResult *r) {
using namespace code;
code::Var to = r->location(s);
Type *t = r->type().type;
Engine &e = engine();
if (t == StormInfo<Int>::type(e))
*s->l << mov(to, intConst(int(intValue)));
else if (t == StormInfo<Nat>::type(e))
*s->l << mov(to, natConst(nat(intValue)));
else if (t == StormInfo<Byte>::type(e))
*s->l << mov(to, byteConst(byte(intValue)));
else if (t == StormInfo<Float>::type(e))
*s->l << mov(to, floatConst(float(intValue)));
else if (t == StormInfo<Double>::type(e))
*s->l << mov(to, doubleConst(double(intValue)));
else if (t == StormInfo<Long>::type(e))
*s->l << mov(to, longConst(Long(intValue)));
else if (t == StormInfo<Word>::type(e))
*s->l << mov(to, wordConst(Word(intValue)));
else
throw new (this) InternalError(S("Unknown type for an integer constant."));
r->created(s);
}
void NumLiteral::floatCode(CodeGen *s, CodeResult *r) {
using namespace code;
code::Var to = r->location(s);
Type *t = r->type().type;
Engine &e = engine();
if (t == StormInfo<Float>::type(e))
*s->l << mov(to, floatConst(float(floatValue)));
else if (t == StormInfo<Double>::type(e))
*s->l << mov(to, doubleConst(floatValue));
else
throw new (this) InternalError(S("Unknown type for a float constant."));
r->created(s);
}
NumLiteral *intConstant(SrcPos pos, Str *v) {
return new (v) NumLiteral(pos, v->toLong());
}
NumLiteral *floatConstant(SrcPos pos, Str *v) {
return new (v) NumLiteral(pos, v->toDouble());
}
NumLiteral *hexConstant(SrcPos pos, Str *v) {
return new (v) NumLiteral(pos, v->hexToWord());
}
/**
* String literals.
*/
StrLiteral::StrLiteral(SrcPos pos, Str *value) : Expr(pos), value(value) {}
ExprResult StrLiteral::result() {
return Value(StormInfo<Str>::type(engine()));
}
void StrLiteral::code(CodeGen *s, CodeResult *r) {
if (!r->needed())
return;
// We can store the string as an object inside the code.
code::Var to = r->location(s);
*s->l << code::mov(to, code::objPtr(value));
r->created(s);
}
void StrLiteral::toS(StrBuf *to) const {
*to << S("\"") << value->escape(Char('"'), Char('$')) << S("\"");
}
StrLiteral *strConstant(syntax::SStr *v) {
return strConstant(v->pos, v->v);
}
StrLiteral *strConstant(SrcPos pos, Str *v) {
return new (v) StrLiteral(pos, v->unescape(Char('"'), Char('$')));
}
StrLiteral *rawStrConstant(SrcPos pos, Str *v) {
return new (v) StrLiteral(pos, v);
}
/**
* Boolean literals.
*/
BoolLiteral::BoolLiteral(SrcPos pos, Bool value) : Expr(pos), value(value) {}
ExprResult BoolLiteral::result() {
return Value(StormInfo<Bool>::type(engine()));
}
void BoolLiteral::code(CodeGen *s, CodeResult *r) {
if (!r->needed())
return;
code::Var to = r->location(s);
*s->l << code::mov(to, code::byteConst(value ? 1 : 0));
r->created(s);
}
void BoolLiteral::toS(StrBuf *to) const {
*to << (value ? S("true") : S("false"));
}
/**
* Character literals.
*/
CharLiteral::CharLiteral(SrcPos pos, Char value) : Expr(pos), value(value) {}
ExprResult CharLiteral::result() {
return Value(StormInfo<Char>::type(engine()));
}
void CharLiteral::code(CodeGen *s, CodeResult *r) {
if (!r->needed())
return;
// Note: We know that the representation of Char is simple enough to allow this even
// though it is a class.
code::Var to = r->location(s);
*s->l << code::mov(to, code::natConst(value.codepoint()));
r->created(s);
}
void CharLiteral::toS(StrBuf *to) const {
Str *x = TO_S(this, value);
*to << S("'") << x->escape(Char('\'')) << S("'");
}
CharLiteral *charConstant(SrcPos pos, Str *value) {
value = value->unescape(Char('\''));
Str::Iter x = value->begin();
CharLiteral *result = new (value) CharLiteral(pos, x.v());
++x;
if (x != value->end())
throw new (value) SyntaxError(pos, S("Expected a single character, but the supplied literal contains multiple characters."));
return result;
}
/**
* Dummy expression.
*/
DummyExpr::DummyExpr(SrcPos pos, Value type) : Expr(pos), type(type) {}
ExprResult DummyExpr::result() {
return type;
}
void DummyExpr::code(CodeGen *s, CodeResult *r) {
throw new (this) InternalError(S("Tried to generate code from a DummyExpr!"));
}
}
}
|