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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
#include "stdafx.h"
#include "WeakCast.h"
#include "Named.h"
#include "Core/Variant.h"
#include "Compiler/Exception.h"
#include "Compiler/Engine.h"
#include "Compiler/Lib/Maybe.h"
namespace storm {
namespace bs {
WeakCast::WeakCast() {}
MAYBE(LocalVar *) STORM_FN WeakCast::result() {
if (created)
return created;
Str *name = null;
SrcPos pos;
if (varName) {
name = varName->v;
pos = varName->pos;
} else {
name = overwrite();
pos = this->pos();
}
if (!name)
return null;
created = new (this) LocalVar(name, resultType(), pos, false);
return created;
}
MAYBE(Str *) WeakCast::overwrite() {
return null;
}
void WeakCast::name(syntax::SStr *name) {
varName = name;
}
void WeakCast::output(StrBuf *to) const {
if (varName) {
*to << varName->v;
*to << S(" = ");
}
}
MAYBE(Str *) WeakCast::defaultOverwrite(Expr *expr) {
if (LocalVarAccess *var = as<LocalVarAccess>(expr))
return new (this) Str(*var->var->name);
if (MemberVarAccess *var = as<MemberVarAccess>(expr))
if (var->implicitMember())
return var->var->name;
if (FnCall *fn = as<FnCall>(expr)) {
if (fn->function()->params->count() == 0)
return fn->function()->name;
else if (fn->function()->params->count() == 1 && fn->implicitMember())
return fn->function()->name;
}
return null;
}
void WeakCast::trueCode(CodeGen *state) {
if (LocalVar *var = result())
initCode(state, var);
}
/**
* 'as' cast.
*/
WeakAsCast::WeakAsCast(Block *block, Expr *expr, SrcName *type)
: expr(expr), to(block->scope.value(type)) {
if (to == Value())
throw new (this) SyntaxError(expr->pos, S("Can not cast to void."));
}
WeakAsCast::WeakAsCast(Block *block, Expr *expr, Value to)
: expr(expr), to(to) {
if (to == Value())
throw new (this) SyntaxError(expr->pos, S("Can not cast to void."));
}
SrcPos WeakAsCast::pos() {
return expr->largePos();
}
MAYBE(Str *) WeakAsCast::overwrite() {
return defaultOverwrite(expr);
}
Value WeakAsCast::resultType() {
return to;
}
void WeakAsCast::toS(StrBuf *to) const {
output(to);
*to << expr << S(" as ") << this->to;
}
/**
* Downcast.
*/
WeakDowncast::WeakDowncast(Block *block, Expr *expr, SrcName *type) : WeakAsCast(block, expr, type) {
Value from = expr->result().type();
if (isMaybe(from))
from = unwrapMaybe(from);
if (!from.isObject())
throw new (this) SyntaxError(expr->pos, S("The WeakDowncast class can only be used with object types."));
if (!to.type->isA(from.type)) {
Str *msg = TO_S(engine(), S("Condition is always false. ") << to
<< S(" does not inherit from ") << from << S("."));
throw new (this) SyntaxError(expr->pos, msg);
}
}
WeakDowncast::WeakDowncast(Block *block, Expr *expr, Value to) : WeakAsCast(block, expr, to) {
Value from = expr->result().type();
if (isMaybe(from))
from = unwrapMaybe(from);
if (!from.isObject())
throw new (this) SyntaxError(expr->pos, S("The WeakDowncast class can only be used with object types."));
if (!to.type->isA(from.type)) {
Str *msg = TO_S(engine(), S("Condition is always false. ") << to
<< S(" does not inherit from ") << from << S("."));
throw new (this) SyntaxError(expr->pos, msg);
}
}
void WeakDowncast::code(CodeGen *state, CodeResult *boolResult) {
using namespace code;
// Get the value...
Value fromType = expr->result().type();
CodeResult *from = new (this) CodeResult(fromType, state->block);
expr->code(state, from);
// Remember where we stored the value...
exprVar = from->location(state);
exprVarRef = fromType.ref;
// Load into eax...
if (fromType.ref) {
*state->l << mov(ptrA, exprVar);
*state->l << mov(ptrA, ptrRel(ptrA, Offset()));
} else {
*state->l << mov(ptrA, exprVar);
}
// Call the 'as' function.
*state->l << fnParam(engine().ptrDesc(), ptrA);
*state->l << fnParam(engine().ptrDesc(), to.type->typeRef());
*state->l << fnCall(engine().ref(builtin::as), false, engine().ptrDesc(), ptrA);
*state->l << cmp(ptrA, ptrConst(Offset()));
*state->l << setCond(al, ifNotEqual);
*state->l << mov(boolResult->location(state), al);
}
void WeakDowncast::initCode(CodeGen *state, LocalVar *var) {
using namespace code;
// Now we are ready to update the variable!
if (exprVarRef) {
*state->l << mov(ptrA, exprVar);
*state->l << mov(var->var.v, ptrRel(ptrA, Offset()));
} else {
*state->l << mov(var->var.v, exprVar);
}
var->var.created(state);
}
/**
* VariantCast.
*/
WeakVariantCast::WeakVariantCast(Block *block, Expr *expr, SrcName *type) : WeakAsCast(block, expr, type) {
variantType = StormInfo<Variant>::type(engine());
if (expr->result().type().type != variantType)
throw new (this) SyntaxError(expr->pos, S("Can not use WeakVariantCast with anything except variants."));
}
WeakVariantCast::WeakVariantCast(Block *block, Expr *expr, Value to) : WeakAsCast(block, expr, to) {
variantType = StormInfo<Variant>::type(engine());
if (expr->result().type().type != variantType)
throw new (this) SyntaxError(expr->pos, S("Can not use WeakVariantCast with anything except variants."));
}
void WeakVariantCast::code(CodeGen *state, CodeResult *boolResult) {
using namespace code;
// Get the source value. Try to get a reference to avoid copying.
Value fromType = expr->result().type();
CodeResult *from = new (this) CodeResult(fromType, state->block);
expr->code(state, from);
exprVar = from->location(state);
exprVarRef = fromType.ref;
// Call the 'as' function to see if the value is of the correct type.
Array<Value> *params = new (this) Array<Value>();
params->push(thisPtr(variantType));
params->push(StormInfo<Type>::type(engine()));
Function *hasFn = as<Function>(variantType->find(S("has"), params, Scope()));
if (!hasFn)
throw new (this) InternalError(S("Can not find Variant::has()."));
Array<Operand> *operands = new (this) Array<Operand>();
if (exprVarRef) {
operands->push(exprVar);
} else {
*state->l << lea(ptrA, exprVar);
operands->push(ptrA);
}
operands->push(to.type->typeRef());
hasFn->localCall(state, operands, boolResult, false);
}
void WeakVariantCast::initCode(CodeGen *state, LocalVar *var) {
using namespace code;
// Now we can copy the data from the Variant into our variable!
if (to.isObject()) {
// Objects are quite easy to deal with: the Variant simply contains a pointer to the object.
if (exprVarRef) {
*state->l << mov(ptrA, exprVar);
*state->l << mov(var->var.v, ptrRel(ptrA, Offset()));
} else {
*state->l << mov(var->var.v, exprVar);
}
} else {
// Values are a bit trickier. The Variant contains a pointer to an array of size 1
// that contains the data. Load the array ptr into ptrA.
if (exprVarRef) {
*state->l << mov(ptrA, exprVar);
*state->l << mov(ptrA, ptrRel(ptrA, Offset()));
} else {
*state->l << mov(ptrA, exprVar);
}
// Adjust ptrA so that it skips the array header:
*state->l << add(ptrA, ptrConst(Size::sPtr * 2));
// Now, we can copy the data!
if (to.isAsmType()) {
*state->l << mov(var->var.v, xRel(to.size(), ptrA, Offset()));
} else {
*state->l << lea(ptrC, var->var.v);
*state->l << fnParam(engine().ptrDesc(), ptrC);
*state->l << fnParam(engine().ptrDesc(), ptrA);
*state->l << fnCall(to.copyCtor(), true);
}
}
var->var.created(state);
}
/**
* MaybeCast.
*/
WeakMaybeCast::WeakMaybeCast(Expr *expr) : WeakCast(), expr(expr) {}
SrcPos WeakMaybeCast::pos() {
return expr->largePos();
}
MAYBE(Str *) WeakMaybeCast::overwrite() {
return defaultOverwrite(expr);
}
Value WeakMaybeCast::resultType() {
return unwrapMaybe(expr->result().type());
}
void WeakMaybeCast::code(CodeGen *state, CodeResult *boolResult) {
exprType = expr->result().type();
if (MaybeClassType *c = as<MaybeClassType>(exprType.type)) {
classCode(c, state, boolResult);
} else if (MaybeValueType *v = as<MaybeValueType>(exprType.type)) {
valueCode(v, state, boolResult);
}
}
void WeakMaybeCast::initCode(CodeGen *state, LocalVar *var) {
if (MaybeClassType *c = as<MaybeClassType>(exprType.type)) {
classInit(c, state, var);
} else if (MaybeValueType *v = as<MaybeValueType>(exprType.type)) {
valueInit(v, state, var);
}
}
void WeakMaybeCast::classCode(MaybeClassType *c, CodeGen *state, CodeResult *boolResult) {
using namespace code;
// Store directly in the result variable if possible. We create it earlier so that this
// is possible. It is not a problem since it is a pointer variable anyway.
LocalVar *var = result();
CodeResult *from;
if (var) {
var->create(state);
from = new (this) CodeResult(Value(c), var->var);
} else {
from = new (this) CodeResult(Value(c), state->block);
}
expr->code(state, from);
// Check if it is null.
*state->l << cmp(from->location(state), ptrConst(Offset()));
*state->l << setCond(boolResult->location(state), ifNotEqual);
}
void WeakMaybeCast::classInit(MaybeClassType *c, CodeGen *state, LocalVar *var) {
// Nothing to do: we do everything in 'classCode' to save local variables.
(void)c;
(void)state;
(void)var;
}
void WeakMaybeCast::valueCode(MaybeValueType *c, CodeGen *state, CodeResult *boolResult) {
using namespace code;
// We're asking for a reference if possible so we don't have to copy.
CodeResult *from = new (this) CodeResult(exprType, state->block);
expr->code(state, from);
// Check if it is null.
exprVar = from->location(state);
if (exprType.ref)
*state->l << mov(ptrC, exprVar);
else
*state->l << lea(ptrC, exprVar);
*state->l << cmp(byteRel(ptrC, c->boolOffset()), byteConst(0));
*state->l << setCond(boolResult->location(state), ifNotEqual);
}
void WeakMaybeCast::valueInit(MaybeValueType *c, CodeGen *state, LocalVar *var) {
using namespace code;
if (exprType.ref)
*state->l << mov(ptrC, exprVar);
else
*state->l << lea(ptrC, exprVar);
*state->l << lea(ptrA, var->var.v);
if (c->param().isAsmType()) {
Size sz = c->param().size();
*state->l << mov(xRel(sz, ptrA, Offset()), xRel(sz, ptrC, Offset()));
} else {
*state->l << fnParam(engine().ptrDesc(), ptrA);
*state->l << fnParam(engine().ptrDesc(), ptrC);
*state->l << fnCall(c->param().copyCtor(), true);
}
var->var.created(state);
}
void WeakMaybeCast::toS(StrBuf *to) const {
output(to);
*to << expr;
}
/**
* Helpers.
*/
WeakCast *weakAsCast(Block *block, Expr *expr, SrcName *type) {
Value exprType = expr->result().type();
// Variant cast:
if (exprType.type == StormInfo<Variant>::type(expr->engine())) {
return new (expr) WeakVariantCast(block, expr, type);
}
// Downcast:
{
Value noMaybe = unwrapMaybe(exprType);
if (noMaybe.isObject()) {
return new (expr) WeakDowncast(block, expr, type);
}
}
// Nothing useful to give.
const wchar *msg = S("The 'as' operator can only be used for class types, actor types, and Variants.");
throw new (expr) SyntaxError(expr->pos, msg);
}
}
}
|