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
|
#include "stdafx.h"
#include "Params.h"
#include "Exception.h"
namespace code {
Param::Param() {
clear();
}
Param::Param(Nat id, Primitive p, Bool use64) {
set(id, p.size(), use64, use64 ? p.offset().v64() : p.offset().v32(), false);
}
Param::Param(Nat id, Size size, Bool use64, Nat offset, Bool inMemory) {
set(id, size, use64, offset, inMemory);
}
Param Param::withId(Nat id) {
Param copy = *this;
copy.data &= ~Nat(0xFF << 1);
copy.data |= (Nat(id) & 0xFF) << 1;
return copy;
}
Bool Param::empty() const {
return data == 0xFFFFFFFE;
}
void Param::clear() {
set(0xFF, Size(), true, 0xFFFFFFFF, false);
}
void Param::set(Nat id, Size size, Bool use64, Nat offset, Bool inMemory) {
if (use64)
dsize = (size.size64() << 4) | size.align64();
else
dsize = (size.size32() << 4) | size.align32();
data = Nat(inMemory);
data |= (id & 0xFF) << 1;
data |= (offset & 0x7FFFFF) << 9;
}
wostream &operator <<(wostream &to, Param p) {
if (p.empty()) {
to << L"empty";
} else {
to << L"#" << p.id() << L"+" << p.offset() << L"," << p.size();
}
if (p.inMemory())
to << L"(in memory)";
return to;
}
void Param::toS(StrBuf *to) const {
if (empty()) {
*to << S("empty");
} else {
*to << S("#") << id() << S("+") << offset() << S(",") << size();
}
if (inMemory())
*to << S("(in memory)");
}
const GcType Params::paramType = {
GcType::tArray,
null,
null,
sizeof(Param),
0,
{},
};
const GcType Params::stackParamType = {
GcType::tArray,
null,
null,
sizeof(StackParam),
0,
{},
};
static void clear(GcArray<Param> *array) {
for (size_t i = array->filled; i < array->count; i++)
array->v[i].clear();
}
Params::Params(Nat intCount, Nat realCount, Nat stackParamAlign, Nat stackAlign) {
this->integer = runtime::allocArray<Param>(engine(), ¶mType, intCount);
this->real = runtime::allocArray<Param>(engine(), ¶mType, realCount);
this->integer->filled = 0;
this->real->filled = 0;
clear(this->integer);
clear(this->real);
this->stackPar = null;
this->stackSize = 0;
this->stackData = (stackAlign & 0xFF) | (stackParamAlign & 0xFF) << 8;
}
void Params::result(TypeDesc *type) {
if (PrimitiveDesc *p = as<PrimitiveDesc>(type)) {
resultPrimitive(p->v);
} else if (ComplexDesc *c = as<ComplexDesc>(type)) {
resultComplex(c);
} else if (SimpleDesc *s = as<SimpleDesc>(type)) {
resultSimple(s);
} else {
throw new (this) InvalidValue(TO_S(this, S("Unknown type description found: ") << type));
}
}
void Params::result(Primitive p) {
resultPrimitive(p);
}
void Params::add(Nat id, TypeDesc *type) {
if (PrimitiveDesc *p = as<PrimitiveDesc>(type)) {
addPrimitive(id, p->v);
} else if (ComplexDesc *c = as<ComplexDesc>(type)) {
addComplex(id, c);
} else if (SimpleDesc *s = as<SimpleDesc>(type)) {
addSimple(id, s);
} else {
throw new (this) InvalidValue(TO_S(this, S("Unknown type description found: ") << type));
}
}
static void bumpFilled(GcArray<Param> *in) {
while (in->filled < in->count) {
if (in->v[in->filled].any())
in->filled++;
else
break;
}
}
void Params::add(Nat id, Primitive p) {
addPrimitive(id, p);
}
void Params::addInt(Param param) {
if (integer->filled < integer->count) {
integer->v[integer->filled++] = param;
bumpFilled(integer); // If another parameter was added.
} else {
addStack(param);
}
}
void Params::addReal(Param param) {
if (!unifiedIntFpRegs()) {
// Normal case:
if (real->filled < real->count)
real->v[real->filled++] = param;
else
addStack(param);
} else {
// Unified case, use 'filled' from 'integer'.
if (integer->filled < real->count) {
real->v[integer->filled++] = param;
bumpFilled(integer);
} else {
addStack(param);
}
}
}
void Params::addStack(Param param) {
// Align argument properly.
// Note: parameter size does not matter if we use 32- or 64- version since they are merged inside Param.
stackSize = roundUp(stackSize, param.size().align64());
if (!stackPar || stackPar->count == stackPar->filled) {
Nat newSize = stackPar ? Nat(stackPar->filled) * 2 : 10;
GcArray<StackParam> *copy = runtime::allocArray<StackParam>(engine(), &stackParamType, newSize);
copy->filled = 0;
if (stackPar) {
memcpy(copy->v, stackPar->v, sizeof(StackParam) * stackPar->filled);
copy->filled = stackPar->filled;
}
stackPar = copy;
}
stackPar->v[stackPar->filled].param = param;
stackPar->v[stackPar->filled].offset = stackSize;
stackPar->filled++;
// Update size. Minimum alignment is 8.
stackSize += roundUp(param.size().aligned().size64(), stackParamAlign());
}
void Params::addInt(Nat at, Param param) {
if (at < integer->count) {
integer->v[at] = param;
bumpFilled(integer);
}
}
Bool Params::hasInt(Nat space) {
return integer->filled + space <= integer->count;
}
Bool Params::hasReal(Nat space) {
return real->filled + space <= real->count;
}
void Params::toS(StrBuf *to) const {
*to << S("Parameters:");
if (!unifiedIntFpRegs()) {
for (Nat i = 0; i < integer->filled; i++)
*to << S("\n") << name(registerSrc(i)) << S(":") << integer->v[i];
for (Nat i = 0; i < real->filled; i++)
*to << S("\n") << name(registerSrc(i + Nat(integer->count))) << S(":") << real->v[i];
} else {
for (Nat i = 0; i < integer->filled; i++) {
*to << S("\n");
if (integer->v[i].any())
*to << name(registerSrc(i)) << S(":") << integer->v[i];
else
*to << name(registerSrc(i + Nat(integer->count))) << S(":") << real->v[i];
}
}
if (stackCount() > 0) {
*to << S("\nOn stack:");
for (Nat i = 0; i < stackCount(); i++) {
*to << S("\n ") << stackParam(i) << S("@");
Nat offset = stackOffset(i);
if (offset < 256)
*to << hex(Byte(offset));
else
*to << hex(offset);
}
}
}
Param Params::totalParam(Nat n) const {
if (n < integer->count)
return integer->v[n];
n -= Nat(integer->count);
if (n < real->count)
return real->v[n];
n -= Nat(real->count);
if (stackPar && n < stackPar->filled)
return stackPar->v[n].param;
assert(false, L"Out of bounds.");
return Param();
}
const GcType Result::dataType = {
GcType::tArray,
null,
null,
sizeof(Data),
0,
{},
};
Result::Result() : memReg(noReg), regs(null) {}
Result Result::inMemory(Reg reg) {
Result r;
r.memReg = reg;
return r;
}
Result Result::inRegisters(EnginePtr e, Nat count) {
Result r;
r.regs = runtime::allocArray<Data>(e, &dataType, count);
return r;
}
Result Result::inRegister(EnginePtr e, Reg reg) {
Result r;
r.regs = runtime::allocArray<Data>(e, &dataType, 1);
r.regs->v[0].reg = reg;
r.regs->v[0].offset = 0;
r.regs->filled = 1;
return r;
}
void Result::putRegister(Reg reg, Nat offset) {
if (!regs)
return;
if (regs->filled < regs->count) {
regs->v[regs->filled].reg = reg;
regs->v[regs->filled].offset = offset;
regs->filled++;
}
}
wostream &operator <<(wostream &to, Result r) {
if (r.memoryRegister() != noReg) {
to << L"in memory: " << name(r.memoryRegister());
} else if (r.registerCount() > 0) {
for (Nat i = 0; i < r.registerCount(); i++)
to << name(r.registerAt(i)) << L": @" << r.registerOffset(i) << L"\n";
} else {
to << L"(empty)";
}
return to;
}
void Result::toS(StrBuf *to) const {
if (memoryRegister() != noReg) {
*to << S("in memory: ") << name(memoryRegister());
} else if (registerCount() > 0) {
for (Nat i = 0; i < registerCount(); i++)
*to << name(registerAt(i)) << S(": @") << registerOffset(i) << S("\n");
} else {
*to << S("(empty)");
}
}
}
|