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
|
#include "stdafx.h"
#include "Output.h"
#include "Gc/Code.h"
#include "Core/Runtime.h"
#include "Core/GcCode.h"
#include "Core/Exception.h"
#include "Utils/Bitwise.h"
#include "Utils/Cache.h"
namespace code {
void Output::putByte(Byte b) {
assert(false);
}
void Output::putInt(Nat w) {
assert(false);
}
void Output::putLong(Word w) {
assert(false);
}
void Output::putPtr(Word w) {
assert(false);
}
void Output::align(Nat to) {
assert(false);
}
void Output::finish() {}
void Output::putGc(GcCodeRef::Kind kind, Nat size, Word w) {
assert(false);
}
void Output::putGc(GcCodeRef::Kind kind, Nat size, Ref ref) {
putGc(kind, size, Word(ref.address()));
markGcRef(ref);
}
void Output::markGc(GcCodeRef::Kind kind, Nat size, Word w) {
assert(false);
}
void Output::markGc(GcCodeRef::Kind kind, Nat size, Ref ref) {
markGc(kind, size, Word(ref.address()));
markGcRef(ref);
}
void Output::putGcPtr(Word w) {
assert(false);
}
void Output::putGcRelative(Word w) {
assert(false);
}
void Output::putRelativeStatic(Word w) {
assert(false);
}
void Output::putPtrSelf(Word w) {
assert(false);
}
Nat Output::tell() const {
assert(false);
return 0;
}
void Output::putSize(Word w, Size size) {
if (size == Size::sByte)
putByte(Byte(w));
else if (size == Size::sInt)
putInt(Nat(w));
else if (size == Size::sLong)
putLong(w);
else if (size == Size::sPtr)
putPtr(w);
else
throw new (this) InternalError(S("Unknown size passed to putSize."));
}
void Output::mark(Label lbl) {
markLabel(lbl.id);
}
void Output::mark(MAYBE(Array<Label> *) lbl) {
if (!lbl)
return;
for (nat i = 0; i < lbl->count(); i++)
mark(lbl->at(i));
}
void Output::putRelative(Label lbl) {
putInt(toRelative(labelOffset(lbl.id)));
}
void Output::putRelative(Label lbl, Nat offset) {
putInt(toRelative(labelOffset(lbl.id) + offset));
}
void Output::putOffset(Label lbl) {
putInt(labelOffset(lbl.id));
}
void Output::putAddress(Label lbl) {
Word start = (Word)codePtr();
Nat offset = labelOffset(lbl.id);
putPtrSelf(start + offset);
}
Nat Output::offset(Label lbl) {
return labelOffset(lbl.id);
}
void Output::putRelative(Ref ref) {
putGcRelative(Word(ref.address()));
markGcRef(ref);
}
void Output::putAddress(Ref ref) {
putGcPtr(Word(ref.address()));
markGcRef(ref);
}
void Output::putInt(OffsetRef ref) {
putInt(0);
markRef(ref, 0);
}
void Output::putPtr(OffsetRef ref) {
putPtr(0);
markRef(ref, 1);
}
void Output::markOffset(Nat data, OffsetRef ref) {
markRef(ref, data);
}
void Output::putObject(RootObject *obj) {
putGcPtr(Word(obj));
}
void Output::putObjRelative(Ref ref) {
putGc(GcCodeRef::relativeHere, sizeof(Int), Word(ref.address()));
markGcRef(ref);
}
void Output::putObjRelative(RootObject *obj) {
putGc(GcCodeRef::relativeHere, sizeof(Int), Word(obj));
}
void Output::markJump(Label to) {
// Note: LabelOutput is set up so that this works properly.
if (labelOffset(to.id) < tell())
putGc(GcCodeRef::backEdge, 0, Word(labelOffset(to.id)));
}
void Output::markLabel(Nat id) {
assert(false);
}
void Output::markGcRef(Ref ref) {}
void Output::markRef(OffsetRef ref, Nat ptr) {}
void *Output::codePtr() const {
assert(false);
return null;
}
Nat Output::labelOffset(Nat id) {
assert(false);
return 0;
}
Nat Output::toRelative(Nat offset) {
assert(false);
return 0;
}
void Output::setFrameOffset(Offset offset) {}
void Output::setFrameRegister(Reg reg) {}
void Output::setFrame(Reg reg, Offset offset) {}
void Output::markSaved(Reg reg, Offset offset) {}
void Output::markFrameAlloc(Offset size) {}
void Output::markPrologEnd() {}
void Output::markReturnAuth() {}
/**
* Label output.
*/
LabelOutput::LabelOutput(Nat ptrSize) : offsets(new (engine()) Array<Nat>()), ptrSize(ptrSize), size(0), refs(0) {}
void LabelOutput::putByte(Byte b) {
size += 1;
}
void LabelOutput::putInt(Nat w) {
size += 4;
}
void LabelOutput::putLong(Word w) {
size += 8;
}
void LabelOutput::putPtr(Word w) {
size += ptrSize;
}
void LabelOutput::align(Nat to) {
size = roundUp(size, to);
}
void LabelOutput::putGc(GcCodeRef::Kind kind, Nat size, Word w) {
this->size += size;
refs++;
}
void LabelOutput::markGc(GcCodeRef::Kind kind, Nat size, Word w) {
refs++;
}
void LabelOutput::putGcPtr(Word w) {
size += ptrSize;
refs++;
}
void LabelOutput::putGcRelative(Word w) {
size += ptrSize;
refs++;
}
void LabelOutput::putRelativeStatic(Word w) {
size += ptrSize;
refs++;
}
void LabelOutput::putPtrSelf(Word w) {
size += ptrSize;
refs++;
}
Nat LabelOutput::tell() const {
return size;
}
void *LabelOutput::codePtr() const {
return null;
}
void LabelOutput::markLabel(Nat id) {
while (offsets->count() <= id)
offsets->push(Nat(-1));
offsets->at(id) = tell();
}
Nat LabelOutput::labelOffset(Nat id) {
if (id < offsets->count())
return offsets->at(id);
else
return Nat(-1);
}
Nat LabelOutput::toRelative(Nat offset) {
// Irrelevant here.
return 0;
}
/**
* Code output.
*/
CodeOutput::CodeOutput() {}
void *CodeOutput::codePtr() const {
return null;
}
void CodeOutput::finish() {
// Invalidate icache if needed.
void *code = codePtr();
if (code) {
invalidateICache(code, (byte *)code + runtime::codeSize(code));
clearLocalICache();
}
}
/**
* Updater.
*/
CodeUpdater::CodeUpdater(Ref src, Content *inside, void *code, Nat slot)
: Reference(src, inside), code(code), slot(slot) {
moved(address());
}
void CodeUpdater::moved(const void *newAddr) {
GcCode *refs = runtime::codeRefs(code);
GcCodeRef &ref = refs->refs[slot];
atomicWrite(ref.pointer, (void *)newAddr);
storm::gccode::writePtr(code, slot);
}
CodeOffsetUpdater::CodeOffsetUpdater(OffsetRef src, Content *inside, void *code, Nat codeOffset, Nat isPtr)
: OffsetReference(src, inside), code(code), codeOffset(codeOffset), ptr(isPtr != 0) {
moved(offset());
}
void CodeOffsetUpdater::moved(Offset offset) {
void *target = (byte *)code + codeOffset;
if (ptr)
atomicWrite(*(size_t *)target, size_t(Long(offset.current())));
else
atomicWrite(*(Nat *)target, Nat(offset.current()));
}
}
|