corda/src/bootimage.cpp

420 lines
12 KiB
C++
Raw Normal View History

2009-03-15 18:02:36 +00:00
/* Copyright (c) 2008-2009, Avian Contributors
2008-11-23 23:58:01 +00:00
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#include "bootimage.h"
#include "heapwalk.h"
#include "common.h"
#include "machine.h"
#include "util.h"
2008-11-23 23:58:01 +00:00
#include "assembler.h"
// since we aren't linking against libstdc++, we must implement this
// ourselves:
extern "C" void __cxa_pure_virtual(void) { abort(); }
using namespace vm;
namespace {
bool
endsWith(const char* suffix, const char* s, unsigned length)
{
unsigned suffixLength = strlen(suffix);
return length >= suffixLength
and memcmp(suffix, s + (length - suffixLength), suffixLength) == 0;
}
object
makeCodeImage(Thread* t, Zone* zone, BootImage* image, uint8_t* code,
2009-06-01 03:16:58 +00:00
uintptr_t* codeMap, const char* className,
const char* methodName, const char* methodSpec)
{
object constants = 0;
PROTECT(t, constants);
object calls = 0;
PROTECT(t, calls);
DelayedPromise* addresses = 0;
for (Finder::Iterator it(t->m->finder); it.hasMore();) {
unsigned nameSize = 0;
const char* name = it.next(&nameSize);
if (endsWith(".class", name, nameSize)
and (className == 0 or strncmp(name, className, nameSize - 6) == 0))
{
2009-03-07 00:10:31 +00:00
// fprintf(stderr, "%.*s\n", nameSize - 6, name);
2009-09-03 15:06:04 +00:00
object c = resolveSystemClass
2008-11-28 04:44:04 +00:00
(t, makeByteArray(t, "%.*s", nameSize - 6, name));
2009-03-07 00:10:31 +00:00
if (t->exception) return 0;
PROTECT(t, c);
2008-11-28 04:44:04 +00:00
if (classMethodTable(t, c)) {
for (unsigned i = 0; i < arrayLength(t, classMethodTable(t, c)); ++i) {
object method = arrayBody(t, classMethodTable(t, c), i);
if ((methodCode(t, method) or (methodFlags(t, method) & ACC_NATIVE))
and ((methodName == 0
or strcmp
(reinterpret_cast<char*>
(&byteArrayBody
(t, vm::methodName(t, method), 0)), methodName) == 0)
and (methodSpec == 0
or strcmp
(reinterpret_cast<char*>
(&byteArrayBody
(t, vm::methodSpec(t, method), 0)), methodSpec)
== 0)))
{
2008-11-28 04:44:04 +00:00
t->m->processor->compileMethod
(t, zone, &constants, &calls, &addresses, method);
2008-11-28 04:44:04 +00:00
}
}
}
}
}
for (; calls; calls = tripleThird(t, calls)) {
2008-12-02 02:38:00 +00:00
object method = tripleFirst(t, calls);
uintptr_t address;
if (methodFlags(t, method) & ACC_NATIVE) {
address = reinterpret_cast<uintptr_t>(code + image->nativeThunk);
} else {
address = methodCompiled(t, method);
}
static_cast<ListenPromise*>(pointerValue(t, tripleSecond(t, calls)))
->listener->resolve(address, 0);
}
for (; addresses; addresses = addresses->next) {
uint8_t* value = reinterpret_cast<uint8_t*>(addresses->basis->value());
assert(t, value >= code);
void* location;
bool flat = addresses->listener->resolve(0, &location);
uintptr_t offset = value - code;
if (flat) {
offset |= BootFlatConstant;
}
memcpy(location, &offset, BytesPerWord);
assert(t, reinterpret_cast<intptr_t>(location)
>= reinterpret_cast<intptr_t>(code));
markBit(codeMap, reinterpret_cast<intptr_t>(location)
- reinterpret_cast<intptr_t>(code));
}
return constants;
}
unsigned
objectSize(Thread* t, object o)
{
assert(t, not objectExtended(t, o));
2008-12-02 02:38:00 +00:00
return baseSize(t, o, objectClass(t, o));
}
2008-11-23 23:58:01 +00:00
void
visitRoots(Thread* t, BootImage* image, HeapWalker* w, object constants)
2008-11-23 23:58:01 +00:00
{
Machine* m = t->m;
2008-12-02 02:38:00 +00:00
for (HashMapIterator it(t, m->classMap); it.hasMore();) {
w->visitRoot(tripleSecond(t, it.next()));
}
2008-11-23 23:58:01 +00:00
image->loader = w->visitRoot(m->loader);
image->types = w->visitRoot(m->types);
2009-06-01 03:16:58 +00:00
m->processor->visitRoots(w);
for (; constants; constants = tripleThird(t, constants)) {
w->visitRoot(tripleFirst(t, constants));
}
2008-11-23 23:58:01 +00:00
}
HeapWalker*
makeHeapImage(Thread* t, BootImage* image, uintptr_t* heap, uintptr_t* map,
unsigned capacity, object constants)
{
2008-11-23 23:58:01 +00:00
class Visitor: public HeapVisitor {
public:
2008-11-23 23:58:01 +00:00
Visitor(Thread* t, uintptr_t* heap, uintptr_t* map, unsigned capacity):
2008-12-02 02:38:00 +00:00
t(t), currentObject(0), currentNumber(0), currentOffset(0), heap(heap),
map(map), position(0), capacity(capacity)
{ }
2008-11-28 04:44:04 +00:00
void visit(unsigned number) {
2008-12-02 02:38:00 +00:00
if (currentObject) {
unsigned offset = currentNumber - 1 + currentOffset;
unsigned mark = heap[offset] & (~PointerMask);
unsigned value = number | (mark << BootShift);
if (value) markBit(map, offset);
heap[offset] = value;
}
}
virtual void root() {
2008-12-02 02:38:00 +00:00
currentObject = 0;
}
virtual unsigned visitNew(object p) {
if (p) {
unsigned size = objectSize(t, p);
2008-12-02 02:38:00 +00:00
unsigned number;
if (currentObject
and (currentOffset * BytesPerWord) == ClassStaticTable)
{
FixedAllocator allocator
2009-06-01 03:16:58 +00:00
(t->m->system, reinterpret_cast<uint8_t*>(heap + position),
2008-12-02 02:38:00 +00:00
(capacity - position) * BytesPerWord);
unsigned totalInBytes;
uintptr_t* dst = static_cast<uintptr_t*>
(t->m->heap->allocateImmortalFixed
(&allocator, size, true, &totalInBytes));
memcpy(dst, p, size * BytesPerWord);
2008-12-02 02:38:00 +00:00
dst[0] |= FixedMark;
number = (dst - heap) + 1;
position += ceiling(totalInBytes, BytesPerWord);
} else {
assert(t, position + size < capacity);
memcpy(heap + position, p, size * BytesPerWord);
number = position + 1;
position += size;
}
2008-11-28 04:44:04 +00:00
visit(number);
return number;
} else {
return 0;
}
}
virtual void visitOld(object, unsigned number) {
2008-11-28 04:44:04 +00:00
visit(number);
}
2008-12-02 02:38:00 +00:00
virtual void push(object object, unsigned number, unsigned offset) {
currentObject = object;
currentNumber = number;
currentOffset = offset;
}
virtual void pop() {
2008-12-02 02:38:00 +00:00
currentObject = 0;
}
Thread* t;
2008-12-02 02:38:00 +00:00
object currentObject;
unsigned currentNumber;
unsigned currentOffset;
uintptr_t* heap;
uintptr_t* map;
unsigned position;
unsigned capacity;
2008-11-23 23:58:01 +00:00
} visitor(t, heap, map, capacity / BytesPerWord);
2008-11-23 23:58:01 +00:00
HeapWalker* w = makeHeapWalker(t, &visitor);
visitRoots(t, image, w, constants);
2008-11-23 23:58:01 +00:00
image->heapSize = visitor.position * BytesPerWord;
2008-11-23 23:58:01 +00:00
return w;
}
void
updateConstants(Thread* t, object constants, uint8_t* code, uintptr_t* codeMap,
HeapMap* heapTable)
{
for (; constants; constants = tripleThird(t, constants)) {
unsigned target = heapTable->find(tripleFirst(t, constants));
assert(t, target > 0);
2008-12-02 02:38:00 +00:00
for (Promise::Listener* pl = static_cast<ListenPromise*>
(pointerValue(t, tripleSecond(t, constants)))->listener;
pl; pl = pl->next)
{
void* location;
bool flat = pl->resolve(0, &location);
uintptr_t offset = target | BootHeapOffset;
if (flat) {
offset |= BootFlatConstant;
}
memcpy(location, &offset, BytesPerWord);
assert(t, reinterpret_cast<intptr_t>(location)
2008-12-02 02:38:00 +00:00
>= reinterpret_cast<intptr_t>(code));
markBit(codeMap, reinterpret_cast<intptr_t>(location)
2008-12-02 02:38:00 +00:00
- reinterpret_cast<intptr_t>(code));
}
}
}
unsigned
offset(object a, uintptr_t* b)
{
return reinterpret_cast<uintptr_t>(b) - reinterpret_cast<uintptr_t>(a);
}
void
2009-06-01 03:16:58 +00:00
writeBootImage(Thread* t, FILE* out, BootImage* image, uint8_t* code,
unsigned codeCapacity, const char* className,
const char* methodName, const char* methodSpec)
{
Zone zone(t->m->system, t->m->heap, 64 * 1024);
uintptr_t* codeMap = static_cast<uintptr_t*>
2009-06-01 03:16:58 +00:00
(t->m->heap->allocate(codeMapSize(codeCapacity)));
memset(codeMap, 0, codeMapSize(codeCapacity));
object constants = makeCodeImage
2009-06-01 03:16:58 +00:00
(t, &zone, image, code, codeMap, className, methodName, methodSpec);
2009-03-07 00:10:31 +00:00
if (t->exception) return;
2008-12-02 02:38:00 +00:00
PROTECT(t, constants);
const unsigned HeapCapacity = 32 * 1024 * 1024;
uintptr_t* heap = static_cast<uintptr_t*>
(t->m->heap->allocate(HeapCapacity));
uintptr_t* heapMap = static_cast<uintptr_t*>
(t->m->heap->allocate(heapMapSize(HeapCapacity)));
memset(heapMap, 0, heapMapSize(HeapCapacity));
// this map will not be used when the bootimage is loaded, so
// there's no need to preserve it:
t->m->byteArrayMap = makeWeakHashMap(t, 0, 0);
2008-11-30 01:39:42 +00:00
collect(t, Heap::MajorCollection);
2008-11-23 23:58:01 +00:00
HeapWalker* heapWalker = makeHeapImage
2009-06-01 03:16:58 +00:00
(t, image, heap, heapMap, HeapCapacity, constants);
updateConstants(t, constants, code, codeMap, heapWalker->map());
2009-06-01 03:16:58 +00:00
image->classCount = hashMapSize(t, t->m->classMap);
2008-12-02 02:38:00 +00:00
unsigned* classTable = static_cast<unsigned*>
2009-06-01 03:16:58 +00:00
(t->m->heap->allocate(image->classCount * sizeof(unsigned)));
2008-12-02 02:38:00 +00:00
{ unsigned i = 0;
for (HashMapIterator it(t, t->m->classMap); it.hasMore();) {
classTable[i++] = heapWalker->map()->find(tripleSecond(t, it.next()));
}
}
2009-06-01 03:16:58 +00:00
image->stringCount = hashMapSize(t, t->m->stringMap);
2008-12-02 02:38:00 +00:00
unsigned* stringTable = static_cast<unsigned*>
2009-06-01 03:16:58 +00:00
(t->m->heap->allocate(image->stringCount * sizeof(unsigned)));
2008-12-02 02:38:00 +00:00
{ unsigned i = 0;
for (HashMapIterator it(t, t->m->stringMap); it.hasMore();) {
stringTable[i++] = heapWalker->map()->find
(jreferenceTarget(t, tripleFirst(t, it.next())));
}
}
2009-06-01 03:16:58 +00:00
unsigned* callTable = t->m->processor->makeCallTable(t, heapWalker);
2008-12-02 02:38:00 +00:00
2008-11-23 23:58:01 +00:00
heapWalker->dispose();
2009-06-01 03:16:58 +00:00
image->magic = BootImage::Magic;
image->codeBase = reinterpret_cast<uintptr_t>(code);
2008-11-30 01:39:42 +00:00
2008-12-02 02:38:00 +00:00
fprintf(stderr, "class count %d string count %d call count %d\n"
"heap size %d code size %d\n",
2009-06-01 03:16:58 +00:00
image->classCount, image->stringCount, image->callCount,
image->heapSize, image->codeSize);
2008-11-30 01:39:42 +00:00
if (true) {
2009-06-01 03:16:58 +00:00
fwrite(image, sizeof(BootImage), 1, out);
2009-06-01 03:16:58 +00:00
fwrite(classTable, image->classCount * sizeof(unsigned), 1, out);
fwrite(stringTable, image->stringCount * sizeof(unsigned), 1, out);
fwrite(callTable, image->callCount * sizeof(unsigned) * 2, 1, out);
2008-12-02 02:38:00 +00:00
2009-06-01 03:16:58 +00:00
unsigned offset = (image->classCount * sizeof(unsigned))
+ (image->stringCount * sizeof(unsigned))
+ (image->callCount * sizeof(unsigned) * 2);
2008-12-02 02:38:00 +00:00
while (offset % BytesPerWord) {
uint8_t c = 0;
fwrite(&c, 1, 1, out);
++ offset;
}
2009-06-01 03:16:58 +00:00
fwrite(heapMap, pad(heapMapSize(image->heapSize)), 1, out);
fwrite(heap, pad(image->heapSize), 1, out);
2009-06-01 03:16:58 +00:00
fwrite(codeMap, pad(codeMapSize(image->codeSize)), 1, out);
fwrite(code, pad(image->codeSize), 1, out);
}
}
} // namespace
int
main(int ac, const char** av)
{
2009-06-01 18:12:29 +00:00
if (ac < 3 or ac > 6) {
fprintf(stderr, "usage: %s <classpath> <output file> "
"[<class name> [<method name> [<method spec>]]]\n", av[0]);
return -1;
}
System* s = makeSystem(0);
Heap* h = makeHeap(s, 128 * 1024 * 1024);
2008-11-28 04:44:04 +00:00
Finder* f = makeFinder(s, av[1], 0);
Processor* p = makeProcessor(s, h, false);
2009-06-01 03:16:58 +00:00
BootImage image;
const unsigned CodeCapacity = 32 * 1024 * 1024;
uint8_t* code = static_cast<uint8_t*>(h->allocate(CodeCapacity));
p->initialize(&image, code, CodeCapacity);
Machine* m = new (h->allocate(sizeof(Machine))) Machine(s, h, f, p, 0, 0);
Thread* t = p->makeThread(m, 0, 0);
enter(t, Thread::ActiveState);
enter(t, Thread::IdleState);
2009-09-03 15:06:04 +00:00
FILE* output = vm::fopen(av[2], "wb");
2009-06-01 18:12:29 +00:00
if (output == 0) {
fprintf(stderr, "unable to open %s\n", av[2]);
return -1;
}
writeBootImage
2009-06-01 18:12:29 +00:00
(t, output, &image, code, CodeCapacity,
(ac > 3 ? av[3] : 0), (ac > 4 ? av[4] : 0), (ac > 5 ? av[5] : 0));
fclose(output);
2009-03-07 00:10:31 +00:00
if (t->exception) {
printTrace(t, t->exception);
}
return 0;
}