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
|
#include "stdafx.h"
#include "Bool.h"
#include "Function.h"
#include "Number.h"
#include "Core/Io/Serialization.h"
namespace storm {
using namespace code;
Type *createBool(Str *name, Size size, GcType *type) {
return new (name) BoolType(name, type);
}
static void boolAnd(InlineParams p) {
if (p.result->needed()) {
Operand result = p.result->location(p.state);
*p.state->l << mov(result, p.param(0));
*p.state->l << band(result, p.param(1));
}
}
static void boolOr(InlineParams p) {
if (p.result->needed()) {
Operand result = p.result->location(p.state);
*p.state->l << mov(result, p.param(0));
*p.state->l << bor(result, p.param(1));
}
}
static void boolEq(InlineParams p) {
if (p.result->needed()) {
Operand result = p.result->location(p.state);
*p.state->l << cmp(p.param(0), p.param(1));
*p.state->l << setCond(result, ifEqual);
}
}
static void boolNeq(InlineParams p) {
if (p.result->needed()) {
Operand result = p.result->location(p.state);
*p.state->l << cmp(p.param(0), p.param(1));
*p.state->l << setCond(result, ifNotEqual);
}
}
static void boolNot(InlineParams p) {
if (p.result->needed()) {
Operand result = p.result->location(p.state);
*p.state->l << cmp(p.param(0), byteConst(0));
*p.state->l << setCond(result, ifEqual);
}
}
static void boolAssign(InlineParams p) {
p.allocRegs(0);
Reg dest = p.regParam(0);
*p.state->l << mov(byteRel(dest, Offset()), p.param(1));
if (p.result->needed()) {
if (p.result->type().ref) {
if (!p.result->suggest(p.state, p.originalParam(0)))
*p.state->l << mov(p.result->location(p.state), dest);
} else {
if (!p.result->suggest(p.state, p.param(1)))
*p.state->l << mov(p.result->location(p.state), p.param(1));
}
}
}
static void boolCopyCtor(InlineParams p) {
p.allocRegs(0, 1);
*p.state->l << mov(byteRel(p.regParam(0), Offset()), byteRel(p.regParam(1), Offset()));
}
static void boolInit(InlineParams p) {
p.allocRegs(0);
*p.state->l << mov(byteRel(p.regParam(0), Offset()), byteConst(0));
}
static Bool boolRead(IStream *from) {
return from->readBool();
}
static Bool boolReadS(ObjIStream *from) {
Bool r;
from->readPrimitiveValue(boolId, &r);
return r;
}
static void boolWrite(Bool v, OStream *to) {
to->writeBool(v);
}
static void boolWriteS(Bool v, ObjOStream *to) {
to->startPrimitive(boolId);
to->to->writeBool(v);
to->end();
}
static void boolToS(Bool &v, StrBuf *to) {
*to << v;
}
BoolType::BoolType(Str *name, GcType *type) : Type(name, typeValue | typeFinal, Size::sByte, type, null) {}
Bool BoolType::loadAll() {
Array<Value> *r = new (this) Array<Value>(1, Value(this, true));
Array<Value> *v = new (this) Array<Value>(1, Value(this, false));
Array<Value> *rr = new (this) Array<Value>(2, Value(this, true));
Array<Value> *vv = new (this) Array<Value>(2, Value(this, false));
Array<Value> *rv = new (this) Array<Value>(2, Value(this, true));
rv->at(1) = Value(this);
add(inlinedFunction(engine, Value(this), S("&"), vv, fnPtr(engine, &boolAnd))->makePure());
add(inlinedFunction(engine, Value(this), S("|"), vv, fnPtr(engine, &boolOr))->makePure());
add(inlinedFunction(engine, Value(this), S("=="), vv, fnPtr(engine, &boolEq))->makePure());
add(inlinedFunction(engine, Value(this), S("!="), vv, fnPtr(engine, &boolNeq))->makePure());
add(inlinedFunction(engine, Value(this), S("!"), v, fnPtr(engine, &boolNot))->makePure());
add(inlinedFunction(engine, Value(), Type::CTOR, rr, fnPtr(engine, &boolCopyCtor))->makePure());
add(inlinedFunction(engine, Value(), Type::CTOR, r, fnPtr(engine, &boolInit))->makePure());
add(inlinedFunction(engine, Value(this, true), S("="), rv, fnPtr(engine, &boolAssign))->makePure());
Array<Value> *rs = new (this) Array<Value>(2, Value(this, true));
rs->at(1) = StormInfo<StrBuf>::type(engine);
add(nativeFunction(engine, Value(), S("toS"), rs, address(&boolToS)));
add(inlinedFunction(engine, Value(StormInfo<Byte>::type(engine)), S("byte"), v, fnPtr(engine, &ucast))->makePure());
add(inlinedFunction(engine, Value(StormInfo<Nat>::type(engine)), S("nat"), v, fnPtr(engine, &ucast))->makePure());
add(inlinedFunction(engine, Value(StormInfo<Word>::type(engine)), S("word"), v, fnPtr(engine, &ucast))->makePure());
Array<Value> *is = new (this) Array<Value>(1, Value(StormInfo<IStream>::type(engine)));
add(nativeFunction(engine, Value(this), S("read"), is, address(&boolRead)));
is = new (this) Array<Value>(1, Value(StormInfo<ObjIStream>::type(engine)));
add(nativeFunction(engine, Value(this), S("read"), is, address(&boolReadS)));
Array<Value> *os = new (this) Array<Value>(2, Value(this, false));
os->at(1) = Value(StormInfo<OStream>::type(engine));
add(nativeFunction(engine, Value(), S("write"), os, address(&boolWrite)));
os = new (this) Array<Value>(2, Value(this, false));
os->at(1) = Value(StormInfo<ObjOStream>::type(engine));
add(nativeFunction(engine, Value(), S("write"), os, address(&boolWriteS)));
return Type::loadAll();
}
}
|