fix bootimage build

This commit is contained in:
Joel Dice 2009-05-31 21:16:58 -06:00
parent 0debcf0f16
commit 11e61543a3
4 changed files with 70 additions and 76 deletions

View File

@ -33,12 +33,9 @@ endsWith(const char* suffix, const char* s, unsigned length)
object object
makeCodeImage(Thread* t, Zone* zone, BootImage* image, uint8_t* code, makeCodeImage(Thread* t, Zone* zone, BootImage* image, uint8_t* code,
unsigned capacity, uintptr_t* codeMap, const char* className, uintptr_t* codeMap, const char* className,
const char* methodName, const char* methodSpec) const char* methodName, const char* methodSpec)
{ {
unsigned size = 0;
t->m->processor->initialize(t, image, code, &size, capacity);
object constants = 0; object constants = 0;
PROTECT(t, constants); PROTECT(t, constants);
@ -118,8 +115,6 @@ makeCodeImage(Thread* t, Zone* zone, BootImage* image, uint8_t* code,
- reinterpret_cast<intptr_t>(code)); - reinterpret_cast<intptr_t>(code));
} }
image->codeSize = size;
return constants; return constants;
} }
@ -143,7 +138,7 @@ visitRoots(Thread* t, BootImage* image, HeapWalker* w, object constants)
image->loader = w->visitRoot(m->loader); image->loader = w->visitRoot(m->loader);
image->types = w->visitRoot(m->types); image->types = w->visitRoot(m->types);
m->processor->visitRoots(image, w); m->processor->visitRoots(w);
for (; constants; constants = tripleThird(t, constants)) { for (; constants; constants = tripleThird(t, constants)) {
w->visitRoot(tripleFirst(t, constants)); w->visitRoot(tripleFirst(t, constants));
@ -186,7 +181,7 @@ makeHeapImage(Thread* t, BootImage* image, uintptr_t* heap, uintptr_t* map,
and (currentOffset * BytesPerWord) == ClassStaticTable) and (currentOffset * BytesPerWord) == ClassStaticTable)
{ {
FixedAllocator allocator FixedAllocator allocator
(t, reinterpret_cast<uint8_t*>(heap + position), (t->m->system, reinterpret_cast<uint8_t*>(heap + position),
(capacity - position) * BytesPerWord); (capacity - position) * BytesPerWord);
unsigned totalInBytes; unsigned totalInBytes;
@ -284,21 +279,18 @@ offset(object a, uintptr_t* b)
} }
void void
writeBootImage(Thread* t, FILE* out, const char* className, writeBootImage(Thread* t, FILE* out, BootImage* image, uint8_t* code,
unsigned codeCapacity, const char* className,
const char* methodName, const char* methodSpec) const char* methodName, const char* methodSpec)
{ {
Zone zone(t->m->system, t->m->heap, 64 * 1024); Zone zone(t->m->system, t->m->heap, 64 * 1024);
BootImage image;
const unsigned CodeCapacity = 32 * 1024 * 1024;
uint8_t* code = static_cast<uint8_t*>(t->m->heap->allocate(CodeCapacity));
uintptr_t* codeMap = static_cast<uintptr_t*> uintptr_t* codeMap = static_cast<uintptr_t*>
(t->m->heap->allocate(codeMapSize(CodeCapacity))); (t->m->heap->allocate(codeMapSize(codeCapacity)));
memset(codeMap, 0, codeMapSize(CodeCapacity)); memset(codeMap, 0, codeMapSize(codeCapacity));
object constants = makeCodeImage object constants = makeCodeImage
(t, &zone, &image, code, CodeCapacity, codeMap, className, methodName, (t, &zone, image, code, codeMap, className, methodName, methodSpec);
methodSpec);
if (t->exception) return; if (t->exception) return;
@ -314,13 +306,13 @@ writeBootImage(Thread* t, FILE* out, const char* className,
collect(t, Heap::MajorCollection); collect(t, Heap::MajorCollection);
HeapWalker* heapWalker = makeHeapImage HeapWalker* heapWalker = makeHeapImage
(t, &image, heap, heapMap, HeapCapacity, constants); (t, image, heap, heapMap, HeapCapacity, constants);
updateConstants(t, constants, code, codeMap, heapWalker->map()); updateConstants(t, constants, code, codeMap, heapWalker->map());
image.classCount = hashMapSize(t, t->m->classMap); image->classCount = hashMapSize(t, t->m->classMap);
unsigned* classTable = static_cast<unsigned*> unsigned* classTable = static_cast<unsigned*>
(t->m->heap->allocate(image.classCount * sizeof(unsigned))); (t->m->heap->allocate(image->classCount * sizeof(unsigned)));
{ unsigned i = 0; { unsigned i = 0;
for (HashMapIterator it(t, t->m->classMap); it.hasMore();) { for (HashMapIterator it(t, t->m->classMap); it.hasMore();) {
@ -328,9 +320,9 @@ writeBootImage(Thread* t, FILE* out, const char* className,
} }
} }
image.stringCount = hashMapSize(t, t->m->stringMap); image->stringCount = hashMapSize(t, t->m->stringMap);
unsigned* stringTable = static_cast<unsigned*> unsigned* stringTable = static_cast<unsigned*>
(t->m->heap->allocate(image.stringCount * sizeof(unsigned))); (t->m->heap->allocate(image->stringCount * sizeof(unsigned)));
{ unsigned i = 0; { unsigned i = 0;
for (HashMapIterator it(t, t->m->stringMap); it.hasMore();) { for (HashMapIterator it(t, t->m->stringMap); it.hasMore();) {
@ -339,29 +331,28 @@ writeBootImage(Thread* t, FILE* out, const char* className,
} }
} }
unsigned* callTable = t->m->processor->makeCallTable unsigned* callTable = t->m->processor->makeCallTable(t, heapWalker);
(t, &image, heapWalker, code);
heapWalker->dispose(); heapWalker->dispose();
image.magic = BootImage::Magic; image->magic = BootImage::Magic;
image.codeBase = reinterpret_cast<uintptr_t>(code); image->codeBase = reinterpret_cast<uintptr_t>(code);
fprintf(stderr, "class count %d string count %d call count %d\n" fprintf(stderr, "class count %d string count %d call count %d\n"
"heap size %d code size %d\n", "heap size %d code size %d\n",
image.classCount, image.stringCount, image.callCount, image.heapSize, image->classCount, image->stringCount, image->callCount,
image.codeSize); image->heapSize, image->codeSize);
if (true) { if (true) {
fwrite(&image, sizeof(BootImage), 1, out); fwrite(image, sizeof(BootImage), 1, out);
fwrite(classTable, image.classCount * sizeof(unsigned), 1, out); fwrite(classTable, image->classCount * sizeof(unsigned), 1, out);
fwrite(stringTable, image.stringCount * sizeof(unsigned), 1, out); fwrite(stringTable, image->stringCount * sizeof(unsigned), 1, out);
fwrite(callTable, image.callCount * sizeof(unsigned) * 2, 1, out); fwrite(callTable, image->callCount * sizeof(unsigned) * 2, 1, out);
unsigned offset = (image.classCount * sizeof(unsigned)) unsigned offset = (image->classCount * sizeof(unsigned))
+ (image.stringCount * sizeof(unsigned)) + (image->stringCount * sizeof(unsigned))
+ (image.callCount * sizeof(unsigned) * 2); + (image->callCount * sizeof(unsigned) * 2);
while (offset % BytesPerWord) { while (offset % BytesPerWord) {
uint8_t c = 0; uint8_t c = 0;
@ -369,11 +360,11 @@ writeBootImage(Thread* t, FILE* out, const char* className,
++ offset; ++ offset;
} }
fwrite(heapMap, pad(heapMapSize(image.heapSize)), 1, out); fwrite(heapMap, pad(heapMapSize(image->heapSize)), 1, out);
fwrite(heap, pad(image.heapSize), 1, out); fwrite(heap, pad(image->heapSize), 1, out);
fwrite(codeMap, pad(codeMapSize(image.codeSize)), 1, out); fwrite(codeMap, pad(codeMapSize(image->codeSize)), 1, out);
fwrite(code, pad(image.codeSize), 1, out); fwrite(code, pad(image->codeSize), 1, out);
} }
} }
@ -392,6 +383,12 @@ main(int ac, const char** av)
Heap* h = makeHeap(s, 128 * 1024 * 1024); Heap* h = makeHeap(s, 128 * 1024 * 1024);
Finder* f = makeFinder(s, av[1], 0); Finder* f = makeFinder(s, av[1], 0);
Processor* p = makeProcessor(s, h); Processor* p = makeProcessor(s, h);
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); Machine* m = new (h->allocate(sizeof(Machine))) Machine(s, h, f, p, 0, 0);
Thread* t = p->makeThread(m, 0, 0); Thread* t = p->makeThread(m, 0, 0);
@ -399,8 +396,8 @@ main(int ac, const char** av)
enter(t, Thread::IdleState); enter(t, Thread::IdleState);
writeBootImage writeBootImage
(t, stdout, (ac > 2 ? av[2] : 0), (ac > 3 ? av[3] : 0), (t, stdout, &image, code, CodeCapacity,
(ac > 4 ? av[4] : 0)); (ac > 2 ? av[2] : 0), (ac > 3 ? av[3] : 0), (ac > 4 ? av[4] : 0));
if (t->exception) { if (t->exception) {
printTrace(t, t->exception); printTrace(t, t->exception);

View File

@ -5884,8 +5884,7 @@ boot(MyThread* t, BootImage* image);
class MyProcessor; class MyProcessor;
void void
compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p, compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p);
BootImage* image, uint8_t* imageBase);
MyProcessor* MyProcessor*
processor(MyThread* t); processor(MyThread* t);
@ -5909,7 +5908,8 @@ class MyProcessor: public Processor {
receiveMethod(0), receiveMethod(0),
windMethod(0), windMethod(0),
rewindMethod(0), rewindMethod(0),
codeAllocator(s, 0, 0) codeAllocator(s, 0, 0),
bootImage(0)
{ } { }
virtual Thread* virtual Thread*
@ -6248,14 +6248,10 @@ class MyProcessor: public Processor {
return visitor.trace; return visitor.trace;
} }
virtual void initialize(Thread* vmt, BootImage* image, uint8_t* code, virtual void initialize(BootImage* image, uint8_t* code, unsigned capacity) {
unsigned capacity) bootImage = image;
{
codeAllocator.base = code; codeAllocator.base = code;
codeAllocator.capacity = capacity; codeAllocator.capacity = capacity;
::compileThunks
(static_cast<MyThread*>(vmt), &codeAllocator, this, image, code);
} }
virtual void compileMethod(Thread* vmt, Zone* zone, object* constants, virtual void compileMethod(Thread* vmt, Zone* zone, object* constants,
@ -6272,16 +6268,15 @@ class MyProcessor: public Processor {
*addresses = bootContext.addresses; *addresses = bootContext.addresses;
} }
virtual void visitRoots(BootImage* image, HeapWalker* w) { virtual void visitRoots(HeapWalker* w) {
image->methodTree = w->visitRoot(methodTree); bootImage->methodTree = w->visitRoot(methodTree);
image->methodTreeSentinal = w->visitRoot(methodTreeSentinal); bootImage->methodTreeSentinal = w->visitRoot(methodTreeSentinal);
image->virtualThunks = w->visitRoot(virtualThunks); bootImage->virtualThunks = w->visitRoot(virtualThunks);
} }
virtual unsigned* makeCallTable(Thread* t, BootImage* image, HeapWalker* w, virtual unsigned* makeCallTable(Thread* t, HeapWalker* w) {
uint8_t* code) bootImage->codeSize = codeAllocator.offset;
{ bootImage->callCount = callTableSize;
image->callCount = callTableSize;
unsigned* table = static_cast<unsigned*> unsigned* table = static_cast<unsigned*>
(t->m->heap->allocate(callTableSize * sizeof(unsigned) * 2)); (t->m->heap->allocate(callTableSize * sizeof(unsigned) * 2));
@ -6290,7 +6285,7 @@ class MyProcessor: public Processor {
for (unsigned i = 0; i < arrayLength(t, callTable); ++i) { for (unsigned i = 0; i < arrayLength(t, callTable); ++i) {
for (object p = arrayBody(t, callTable, i); p; p = callNodeNext(t, p)) { for (object p = arrayBody(t, callTable, i); p; p = callNodeNext(t, p)) {
table[index++] = callNodeAddress(t, p) table[index++] = callNodeAddress(t, p)
- reinterpret_cast<uintptr_t>(code); - reinterpret_cast<uintptr_t>(codeAllocator.base);
table[index++] = w->map()->find(callNodeTarget(t, p)) table[index++] = w->map()->find(callNodeTarget(t, p))
| (static_cast<unsigned>(callNodeFlags(t, p)) << BootShift); | (static_cast<unsigned>(callNodeFlags(t, p)) << BootShift);
} }
@ -6300,9 +6295,11 @@ class MyProcessor: public Processor {
} }
virtual void boot(Thread* t, BootImage* image) { virtual void boot(Thread* t, BootImage* image) {
codeAllocator.base = static_cast<uint8_t*> if (codeAllocator.base == 0) {
(s->tryAllocateExecutable(ExecutableAreaSizeInBytes)); codeAllocator.base = static_cast<uint8_t*>
codeAllocator.capacity = ExecutableAreaSizeInBytes; (s->tryAllocateExecutable(ExecutableAreaSizeInBytes));
codeAllocator.capacity = ExecutableAreaSizeInBytes;
}
if (image) { if (image) {
::boot(static_cast<MyThread*>(t), image); ::boot(static_cast<MyThread*>(t), image);
@ -6313,7 +6310,7 @@ class MyProcessor: public Processor {
set(t, methodTree, TreeNodeLeft, methodTreeSentinal); set(t, methodTree, TreeNodeLeft, methodTreeSentinal);
set(t, methodTree, TreeNodeRight, methodTreeSentinal); set(t, methodTree, TreeNodeRight, methodTreeSentinal);
::compileThunks(static_cast<MyThread*>(t), &codeAllocator, this, 0, 0); ::compileThunks(static_cast<MyThread*>(t), &codeAllocator, this);
} }
segFaultHandler.m = t->m; segFaultHandler.m = t->m;
@ -6389,6 +6386,7 @@ class MyProcessor: public Processor {
object rewindMethod; object rewindMethod;
SegFaultHandler segFaultHandler; SegFaultHandler segFaultHandler;
FixedAllocator codeAllocator; FixedAllocator codeAllocator;
BootImage* bootImage;
}; };
object object
@ -6697,9 +6695,9 @@ fixupVirtualThunks(MyThread* t, BootImage* image, uint8_t* code)
MyProcessor* p = processor(t); MyProcessor* p = processor(t);
for (unsigned i = 0; i < wordArrayLength(t, p->virtualThunks); ++i) { for (unsigned i = 0; i < wordArrayLength(t, p->virtualThunks); ++i) {
if (wordArrayBody(t, p->virtualThunks, 0)) { if (wordArrayBody(t, p->virtualThunks, i)) {
wordArrayBody(t, p->virtualThunks, 0) wordArrayBody(t, p->virtualThunks, i)
= (wordArrayBody(t, p->virtualThunks, 0) - image->codeBase) = (wordArrayBody(t, p->virtualThunks, i) - image->codeBase)
+ reinterpret_cast<uintptr_t>(code); + reinterpret_cast<uintptr_t>(code);
} }
} }
@ -6779,8 +6777,7 @@ getThunk(MyThread* t, Thunk thunk)
} }
void void
compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p, compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p)
BootImage* image, uint8_t* imageBase)
{ {
class ThunkContext { class ThunkContext {
public: public:
@ -6905,6 +6902,9 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p,
p->defaultThunk = finish p->defaultThunk = finish
(t, allocator, defaultContext.context.assembler, "default"); (t, allocator, defaultContext.context.assembler, "default");
BootImage* image = p->bootImage;
uint8_t* imageBase = p->codeAllocator.base;
{ void* call; { void* call;
defaultContext.promise.listener->resolve defaultContext.promise.listener->resolve
(reinterpret_cast<intptr_t>(voidPointer(compileMethod)), &call); (reinterpret_cast<intptr_t>(voidPointer(compileMethod)), &call);

View File

@ -3217,7 +3217,7 @@ class MyProcessor: public Processor {
return 0; return 0;
} }
virtual void initialize(vm::Thread*, BootImage*, uint8_t*, unsigned) { virtual void initialize(BootImage*, uint8_t*, unsigned) {
abort(s); abort(s);
} }
@ -3227,13 +3227,11 @@ class MyProcessor: public Processor {
abort(s); abort(s);
} }
virtual void visitRoots(BootImage*, HeapWalker*) { virtual void visitRoots(HeapWalker*) {
abort(s); abort(s);
} }
virtual unsigned* makeCallTable(vm::Thread*, BootImage*, HeapWalker*, virtual unsigned* makeCallTable(vm::Thread*, HeapWalker*) {
uint8_t*)
{
abort(s); abort(s);
} }

View File

@ -117,18 +117,17 @@ class Processor {
getStackTrace(Thread* t, Thread* target) = 0; getStackTrace(Thread* t, Thread* target) = 0;
virtual void virtual void
initialize(Thread* t, BootImage* image, uint8_t* code, initialize(BootImage* image, uint8_t* code, unsigned capacity) = 0;
unsigned capacity) = 0;
virtual void virtual void
compileMethod(Thread* t, Zone* zone, object* constants, object* calls, compileMethod(Thread* t, Zone* zone, object* constants, object* calls,
DelayedPromise** addresses, object method) = 0; DelayedPromise** addresses, object method) = 0;
virtual void virtual void
visitRoots(BootImage* image, HeapWalker* w) = 0; visitRoots(HeapWalker* w) = 0;
virtual unsigned* virtual unsigned*
makeCallTable(Thread* t, BootImage* image, HeapWalker* w, uint8_t* code) = 0; makeCallTable(Thread* t, HeapWalker* w) = 0;
virtual void virtual void
boot(Thread* t, BootImage* image) = 0; boot(Thread* t, BootImage* image) = 0;