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
|
#include "stdafx.h"
#include "Runtime.h"
#include "Gc/Gc.h"
#include "Type.h"
#include "Engine.h"
#include "Core/Str.h"
#include "Core/Io/StdStream.h"
#include "Gc/Code.h"
#include "StdIoThread.h"
namespace storm {
namespace runtime {
/**
* Implements the functions declared in 'Core/Runtime.h' for the compiler.
*/
Type *cppType(Engine &e, Nat id) {
return e.cppType(id);
}
Type *cppTemplateVa(Engine &e, Nat id, Nat count, va_list l) {
const nat maxCount = 16;
assert(count < maxCount, L"Too many template parameters used: " + ::toS(count) + L" max " + ::toS(maxCount));
TemplateList *tList = e.cppTemplate(id);
if (!tList)
return null;
Nat params[maxCount];
for (nat i = 0; i < count; i++)
params[i] = va_arg(l, Nat);
return tList->find(params, count);
}
const Handle &typeHandle(Type *t) {
return t->handle();
}
const Handle &voidHandle(Engine &e) {
return e.voidHandle();
}
const Handle &refObjHandle(Engine &e) {
return e.refObjHandle();
}
// Find the current parent type for 'type' that matches 'vtable'.
// This is the slow path of 'typeOf'.
static Type *findCurrentParent(Type *type, const void *vtable) {
for (Type *curr = type->super(); curr; curr = curr->super()) {
if (curr->hasVTable(vtable))
return curr;
}
// Note: Looking up the top type will fail sometimes, for example when we have
// created a type from an .so file, so that we don't have the same instance of the
// vtable as in Storm's metadata (e.g. creating Str from a library). In those cases,
// just return the original one.
//
// Important note: this will never happen for Storm types, since we maintain exactly one
// vtable for those types! We could start trying to do C++-style dynamic casts using the
// typeinfo in the vtable. That would, however, likely be too slow.
return type;
}
Type *typeOf(const RootObject *o) {
Type *type = Gc::typeOf(o)->type;
// Note: 'type' here is the *allocated* type of the object. It does not correspond to
// the current type of the object. This is given by the vtable (they differ during
// object construction or destruction, before all constructors are finished). As such,
// inspect vtables to assess the situation. Important: we want the common case where
// 'type' is indeed the correct answer to be the fast path here. We don't expect
// downcasting during construction to be common, but it should be correct!
const void *vtable = vtable::from(o);
if (type->hasVTable(vtable))
return type;
return findCurrentParent(type, vtable);
}
Type *allocTypeOf(const RootObject *o) {
return Gc::typeOf(o)->type;
}
const GcType *typeGc(Type *t) {
return t->gcType();
}
Str *typeName(Type *t) {
return t->shortIdentifier();
}
Str *typeIdentifier(Type *t) {
return mangleName(t->path());
}
static Type *CODECALL fromIdentifierI(Str *name) {
Engine &e = name->engine();
return lookupMangledName(e.scope(), name);
}
MAYBE(Type *) fromIdentifier(Str *name) {
// Note: This needs to be thread-safe!
// It is called during deserialization (for example) to find types. This may be
// done on other threads than the compiler thread.
const os::Thread &t = Compiler::thread(name->engine())->thread();
if (t != os::Thread::current()) {
os::Future<Type *> f;
os::FnCall<Type *> p = os::fnCall().add(name);
os::UThread::spawn(address(&fromIdentifierI), false, p, f, &t);
return f.result(&updateFutureExceptions, null);
} else {
return fromIdentifierI(name);
}
}
bool isValue(Type *t) {
return (t->typeFlags() & typeValue) != 0;
}
const GcType *gcTypeOf(const void *alloc) {
return Gc::typeOf(alloc);
}
bool isA(const Type *a, const Type *b) {
return a->chain->isA(b);
}
bool isA(const RootObject *a, const Type *t) {
return typeOf(a)->chain->isA(t);
}
Engine &allocEngine(const RootObject *o) {
return Gc::typeOf(o)->type->engine;
}
void *allocRaw(Engine &e, const GcType *type) {
return e.gc.alloc(type);
}
void *allocStaticRaw(Engine &e, const GcType *type) {
return e.gc.allocStatic(type);
}
GcArray<Byte> *allocBuffer(Engine &e, size_t count) {
return e.gc.allocBuffer(count);
}
static NOINLINE void allocFailSize(const GcType *t, size_t size) {
PLN(L"Invalid type description found! " << size << L" vs " << t->stride);
debugAssertFailed();
}
static NOINLINE void allocFailType(const GcType *t, Type *type) {
PLN(L"Invalid type reference found! GcType: " << (void *)t->type << L", actual: " << (void *)type);
debugAssertFailed();
}
void *allocObject(size_t size, Type *type) {
const GcType *t = type->gcType();
#ifdef DEBUG
// Try to keep the failure case out of the hot code, even in debug mode.
if (size > t->stride)
allocFailSize(t, size);
if (t->type != type)
allocFailType(t, type);
#endif
return type->engine.gc.alloc(t);
}
void *allocArray(Engine &e, const GcType *type, size_t count) {
return e.gc.allocArray(type, count);
}
void *allocArrayRehash(Engine &e, const GcType *type, size_t count) {
return e.gc.allocArrayRehash(type, count);
}
void *allocWeakArray(Engine &e, size_t count) {
return e.gc.allocWeakArray(count);
}
void *allocWeakArrayRehash(Engine &e, size_t count) {
return e.gc.allocWeakArrayRehash(count);
}
GcWatch *createWatch(Engine &e) {
return e.gc.createWatch();
}
void *allocCode(Engine &e, size_t code, size_t refs) {
return e.gc.allocCode(code, refs);
}
size_t codeSize(const void *code) {
return Gc::codeSize(code);
}
GcCode *codeRefs(void *code) {
return Gc::codeRefs(code);
}
void codeUpdatePtrs(void *code) {
gccode::updatePtrs(code, Gc::codeRefs(code));
}
void setVTable(RootObject *object) {
allocTypeOf(object)->vtable()->insert(object);
}
bool liveObject(RootObject *object) {
return Gc::liveObject(object);
}
os::ThreadGroup &threadGroup(Engine &e) {
return e.threadGroup;
}
util::Lock &threadLock(Engine &e) {
return e.threadLock;
}
void attachThread(Engine &e) {
e.gc.attachThread();
}
void detachThread(Engine &e, const os::Thread &thread) {
e.gc.detachThread(thread);
}
void reattachThread(Engine &e, const os::Thread &thread) {
e.gc.reattachThread(thread);
}
void postStdRequest(Engine &e, StdRequest *request) {
e.stdIo()->post(request);
}
RootObject *cloneObject(RootObject *obj) {
if (obj == null)
return null;
// Nothing needs to be done for TObjects.
if (TObject *t = as<TObject>(obj))
return t;
return cloneObjectEnv(obj, new (obj) CloneEnv());
}
RootObject *cloneObjectEnv(RootObject *obj, CloneEnv *env) {
if (obj == null)
return null;
// Nothing needs to be done for TObjects.
if (TObject *t = as<TObject>(obj))
return t;
Object *src = (Object *)obj;
// For robustness:
if (!env)
env = new (obj) CloneEnv();
if (Object *prev = env->cloned(src))
return prev;
Type *t = typeOf(src);
const GcType *gcType = t->gcType();
void *mem = t->engine.gc.alloc(gcType);
Type::CopyCtorFn ctor = t->rawCopyConstructor();
if (ctor) {
(*ctor)(mem, src);
} else {
// No copy constructor... Well, then we do it the hard way!
memcpy(mem, src, gcType->stride);
}
Object *result = (Object *)mem;
env->cloned(src, result);
// Note: needs to happen *after* cloned is called. Otherwise we don't handle cycles.
result->deepCopy(env);
return result;
}
void checkObject(Engine &e, const void *obj) {
e.gc.checkMemory(obj, false);
}
// Only used from Code/, not a part of the public API.
Gc &engineGc(Engine &e) {
return e.gc;
}
}
}
|