Skip to content

Commit 25ade14

Browse files
author
Joel Dice
committed
lots of bugfixes and refactoring
1 parent 4a1dd3a commit 25ade14

File tree

13 files changed

+812
-597
lines changed

13 files changed

+812
-597
lines changed

classpath/java/lang/SystemClassLoader.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import java.net.MalformedURLException;
1515

1616
public class SystemClassLoader extends ClassLoader {
17-
private Object map;
18-
1917
protected native Class findClass(String name) throws ClassNotFoundException;
2018

2119
protected native Class findLoadedClass(String name);

src/assembler.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class Promise {
8181
class Listener {
8282
public:
8383
virtual void* resolve(int64_t value) = 0;
84+
85+
Listener* next;
8486
};
8587

8688
virtual int64_t value() = 0;
@@ -118,7 +120,10 @@ class ListenPromise: public Promise {
118120
}
119121

120122
virtual Listener* listen(unsigned sizeInBytes) {
121-
return listener = static_cast<Listener*>(allocator->allocate(sizeInBytes));
123+
Listener* l = static_cast<Listener*>(allocator->allocate(sizeInBytes));
124+
l->next = listener;
125+
listener = l;
126+
return l;
122127
}
123128

124129
System* s;

src/bootimage.cpp

Lines changed: 111 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,16 @@ makeCodeImage(Thread* t, Zone* zone, BootImage* image, uint8_t* code,
6767
}
6868

6969
for (; calls; calls = tripleThird(t, calls)) {
70+
object method = tripleFirst(t, calls);
71+
uintptr_t address;
72+
if (methodFlags(t, method) & ACC_NATIVE) {
73+
address = reinterpret_cast<uintptr_t>(code + image->nativeThunk);
74+
} else {
75+
address = methodCompiled(t, method);
76+
}
77+
7078
static_cast<ListenPromise*>(pointerValue(t, tripleSecond(t, calls)))
71-
->listener->resolve(methodCompiled(t, tripleFirst(t, calls)));
79+
->listener->resolve(address);
7280
}
7381

7482
image->codeSize = size;
@@ -80,6 +88,7 @@ unsigned
8088
objectSize(Thread* t, object o)
8189
{
8290
assert(t, not objectExtended(t, o));
91+
8392
return baseSize(t, o, objectClass(t, o));
8493
}
8594

@@ -88,8 +97,11 @@ visitRoots(Thread* t, BootImage* image, HeapWalker* w, object constants)
8897
{
8998
Machine* m = t->m;
9099

100+
for (HashMapIterator it(t, m->classMap); it.hasMore();) {
101+
w->visitRoot(tripleSecond(t, it.next()));
102+
}
103+
91104
image->loader = w->visitRoot(m->loader);
92-
image->stringMap = w->visitRoot(m->stringMap);
93105
image->types = w->visitRoot(m->types);
94106

95107
m->processor->visitRoots(image, w);
@@ -99,51 +111,63 @@ visitRoots(Thread* t, BootImage* image, HeapWalker* w, object constants)
99111
}
100112
}
101113

102-
void
103-
visitReference(Thread* t, HeapWalker* w, uintptr_t* heap, uintptr_t* map,
104-
object r)
105-
{
106-
int target = w->map()->find(jreferenceTarget(t, r));
107-
assert(t, target > 0);
108-
109-
int reference = w->map()->find(r);
110-
assert(t, reference > 0);
111-
112-
unsigned index = reference - 1 + (JreferenceTarget / BytesPerWord);
113-
markBit(map, index);
114-
heap[index] = target;
115-
}
116-
117114
HeapWalker*
118115
makeHeapImage(Thread* t, BootImage* image, uintptr_t* heap, uintptr_t* map,
119116
unsigned capacity, object constants)
120117
{
121118
class Visitor: public HeapVisitor {
122119
public:
123120
Visitor(Thread* t, uintptr_t* heap, uintptr_t* map, unsigned capacity):
124-
t(t), current(0), heap(heap), map(map), position(0), capacity(capacity)
121+
t(t), currentObject(0), currentNumber(0), currentOffset(0), heap(heap),
122+
map(map), position(0), capacity(capacity)
125123
{ }
126124

127125
void visit(unsigned number) {
128-
if (current) {
129-
if (number) markBit(map, current - 1);
130-
heap[current - 1] = number;
126+
if (currentObject) {
127+
unsigned offset = currentNumber - 1 + currentOffset;
128+
unsigned mark = heap[offset] & (~PointerMask);
129+
unsigned value = number | (mark << BootShift);
130+
131+
if (value) markBit(map, offset);
132+
133+
heap[offset] = value;
131134
}
132135
}
133136

134137
virtual void root() {
135-
current = 0;
138+
currentObject = 0;
136139
}
137140

138141
virtual unsigned visitNew(object p) {
139142
if (p) {
140143
unsigned size = objectSize(t, p);
141-
assert(t, position + size < capacity);
142144

143-
memcpy(heap + position, p, size * BytesPerWord);
145+
unsigned number;
146+
if (currentObject
147+
and (currentOffset * BytesPerWord) == ClassStaticTable)
148+
{
149+
FixedAllocator allocator
150+
(t, reinterpret_cast<uint8_t*>(heap + position),
151+
(capacity - position) * BytesPerWord);
152+
153+
unsigned totalInBytes;
154+
uintptr_t* dst = static_cast<uintptr_t*>
155+
(t->m->heap->allocateImmortalFixed
156+
(&allocator, size, true, &totalInBytes));
157+
158+
memcpy(dst, p, size * BytesPerWord);
144159

145-
unsigned number = position + 1;
146-
position += size;
160+
dst[0] |= FixedMark;
161+
162+
number = (dst - heap) + 1;
163+
position += ceiling(totalInBytes, BytesPerWord);
164+
} else {
165+
assert(t, position + size < capacity);
166+
memcpy(heap + position, p, size * BytesPerWord);
167+
168+
number = position + 1;
169+
position += size;
170+
}
147171

148172
visit(number);
149173

@@ -157,16 +181,20 @@ makeHeapImage(Thread* t, BootImage* image, uintptr_t* heap, uintptr_t* map,
157181
visit(number);
158182
}
159183

160-
virtual void push(object, unsigned number, unsigned offset) {
161-
current = number + offset;
184+
virtual void push(object object, unsigned number, unsigned offset) {
185+
currentObject = object;
186+
currentNumber = number;
187+
currentOffset = offset;
162188
}
163189

164190
virtual void pop() {
165-
current = 0;
191+
currentObject = 0;
166192
}
167193

168194
Thread* t;
169-
unsigned current;
195+
object currentObject;
196+
unsigned currentNumber;
197+
unsigned currentOffset;
170198
uintptr_t* heap;
171199
uintptr_t* map;
172200
unsigned position;
@@ -176,14 +204,6 @@ makeHeapImage(Thread* t, BootImage* image, uintptr_t* heap, uintptr_t* map,
176204
HeapWalker* w = makeHeapWalker(t, &visitor);
177205
visitRoots(t, image, w, constants);
178206

179-
for (object r = t->m->weakReferences; r; r = jreferenceVmNext(t, r)) {
180-
visitReference(t, w, heap, map, r);
181-
}
182-
183-
for (object r = t->m->tenuredWeakReferences; r; r = jreferenceVmNext(t, r)) {
184-
visitReference(t, w, heap, map, r);
185-
}
186-
187207
image->heapSize = visitor.position * BytesPerWord;
188208

189209
return w;
@@ -197,14 +217,18 @@ updateConstants(Thread* t, object constants, uint8_t* code, uintptr_t* codeMap,
197217
unsigned target = heapTable->find(tripleFirst(t, constants));
198218
assert(t, target > 0);
199219

200-
void* dst = static_cast<ListenPromise*>
201-
(pointerValue(t, tripleSecond(t, constants)))->listener->resolve(target);
220+
for (Promise::Listener* pl = static_cast<ListenPromise*>
221+
(pointerValue(t, tripleSecond(t, constants)))->listener;
222+
pl; pl = pl->next)
223+
{
224+
void* dst = pl->resolve(target);
202225

203-
assert(t, reinterpret_cast<intptr_t>(dst)
204-
>= reinterpret_cast<intptr_t>(code));
226+
assert(t, reinterpret_cast<intptr_t>(dst)
227+
>= reinterpret_cast<intptr_t>(code));
205228

206-
markBit(codeMap, reinterpret_cast<intptr_t>(dst)
207-
- reinterpret_cast<intptr_t>(code));
229+
markBit(codeMap, reinterpret_cast<intptr_t>(dst)
230+
- reinterpret_cast<intptr_t>(code));
231+
}
208232
}
209233
}
210234

@@ -227,6 +251,7 @@ writeBootImage(Thread* t, FILE* out)
227251
memset(codeMap, 0, codeMapSize(CodeCapacity));
228252

229253
object constants = makeCodeImage(t, &zone, &image, code, CodeCapacity);
254+
PROTECT(t, constants);
230255

231256
const unsigned HeapCapacity = 32 * 1024 * 1024;
232257
uintptr_t* heap = static_cast<uintptr_t*>
@@ -235,25 +260,64 @@ writeBootImage(Thread* t, FILE* out)
235260
(t->m->heap->allocate(heapMapSize(HeapCapacity)));
236261
memset(heapMap, 0, heapMapSize(HeapCapacity));
237262

238-
PROTECT(t, constants);
239263
collect(t, Heap::MajorCollection);
240264

241265
HeapWalker* heapWalker = makeHeapImage
242266
(t, &image, heap, heapMap, HeapCapacity, constants);
243267

244268
updateConstants(t, constants, code, codeMap, heapWalker->map());
245269

270+
image.classCount = hashMapSize(t, t->m->classMap);
271+
unsigned* classTable = static_cast<unsigned*>
272+
(t->m->heap->allocate(image.classCount * sizeof(unsigned)));
273+
274+
{ unsigned i = 0;
275+
for (HashMapIterator it(t, t->m->classMap); it.hasMore();) {
276+
classTable[i++] = heapWalker->map()->find(tripleSecond(t, it.next()));
277+
}
278+
}
279+
280+
image.stringCount = hashMapSize(t, t->m->stringMap);
281+
unsigned* stringTable = static_cast<unsigned*>
282+
(t->m->heap->allocate(image.stringCount * sizeof(unsigned)));
283+
284+
{ unsigned i = 0;
285+
for (HashMapIterator it(t, t->m->stringMap); it.hasMore();) {
286+
stringTable[i++] = heapWalker->map()->find
287+
(jreferenceTarget(t, tripleFirst(t, it.next())));
288+
}
289+
}
290+
291+
unsigned* callTable = t->m->processor->makeCallTable
292+
(t, &image, heapWalker, code);
293+
246294
heapWalker->dispose();
247295

248296
image.magic = BootImage::Magic;
249297
image.codeBase = reinterpret_cast<uintptr_t>(code);
250298

251-
fprintf(stderr, "heap size %d code size %d\n",
252-
image.heapSize, image.codeSize);
299+
fprintf(stderr, "class count %d string count %d call count %d\n"
300+
"heap size %d code size %d\n",
301+
image.classCount, image.stringCount, image.callCount, image.heapSize,
302+
image.codeSize);
253303

254304
if (true) {
255305
fwrite(&image, sizeof(BootImage), 1, out);
256306

307+
fwrite(classTable, image.classCount * sizeof(unsigned), 1, out);
308+
fwrite(stringTable, image.stringCount * sizeof(unsigned), 1, out);
309+
fwrite(callTable, image.callCount * sizeof(unsigned) * 2, 1, out);
310+
311+
unsigned offset = (image.classCount * sizeof(unsigned))
312+
+ (image.stringCount * sizeof(unsigned))
313+
+ (image.callCount * sizeof(unsigned) * 2);
314+
315+
while (offset % BytesPerWord) {
316+
uint8_t c = 0;
317+
fwrite(&c, 1, 1, out);
318+
++ offset;
319+
}
320+
257321
fwrite(heapMap, pad(heapMapSize(image.heapSize)), 1, out);
258322
fwrite(heap, pad(image.heapSize), 1, out);
259323

src/bootimage.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
namespace vm {
1717

18+
const unsigned BootMask = (~static_cast<unsigned>(0)) / BytesPerWord;
19+
20+
const unsigned BootShift = 32 - log(BytesPerWord);
21+
1822
class BootImage {
1923
public:
2024
static const unsigned Magic = 0x22377322;
@@ -24,11 +28,12 @@ class BootImage {
2428
unsigned heapSize;
2529
unsigned codeSize;
2630

31+
unsigned classCount;
32+
unsigned stringCount;
33+
unsigned callCount;
34+
2735
unsigned loader;
28-
unsigned stringMap;
2936
unsigned types;
30-
31-
unsigned callTable;
3237
unsigned methodTree;
3338
unsigned methodTreeSentinal;
3439

0 commit comments

Comments
 (0)