forked from ReadyTalk/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1966 lines (1580 loc) · 57.6 KB
/
main.cpp
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2008-2013, Avian Contributors
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 <avian/vm/heap/heap.h>
#include "avian/heapwalk.h"
#include "avian/common.h"
#include "avian/machine.h"
#include "avian/util.h"
#include <avian/util/stream.h>
#include <avian/vm/codegen/assembler.h>
#include <avian/vm/codegen/promise.h>
#include "avian/target.h"
#include <avian/tools/object-writer/tools.h>
#include <avian/util/runtime-array.h>
#include "avian/lzma.h"
#include <avian/util/arg-parser.h>
#include <avian/util/abort.h>
// since we aren't linking against libstdc++, we must implement this
// ourselves:
extern "C" void __cxa_pure_virtual(void) { abort(); }
using namespace vm;
using namespace avian::tools;
using namespace avian::util;
using namespace avian::codegen;
namespace {
const unsigned HeapCapacity = 512 * 1024 * 1024;
const unsigned TargetFixieSizeInBytes = 8 + (TargetBytesPerWord * 2);
const unsigned TargetFixieSizeInWords = ceilingDivide
(TargetFixieSizeInBytes, TargetBytesPerWord);
const unsigned TargetFixieAge = 0;
const unsigned TargetFixieFlags = 2;
const unsigned TargetFixieSize = 4;
const bool DebugNativeTarget = false;
enum Type {
Type_none,
Type_pad,
Type_object,
Type_object_nogc,
Type_int8_t,
Type_uint8_t,
Type_int16_t,
Type_uint16_t,
Type_int32_t,
Type_uint32_t,
Type_intptr_t,
Type_uintptr_t,
Type_int64_t,
Type_int64_t_pad,
Type_uint64_t,
Type_float,
Type_double,
Type_double_pad,
Type_word,
Type_array
};
class Field {
public:
Type type;
unsigned buildOffset;
unsigned buildSize;
unsigned targetOffset;
unsigned targetSize;
};
void
init(Field* f, Type type, unsigned buildOffset, unsigned buildSize,
unsigned targetOffset, unsigned targetSize)
{
f->type = type;
f->buildOffset = buildOffset;
f->buildSize = buildSize;
f->targetOffset = targetOffset;
f->targetSize = targetSize;
}
class TypeMap {
public:
enum Kind {
NormalKind,
SingletonKind,
PoolKind
};
TypeMap(unsigned buildFixedSizeInWords, unsigned targetFixedSizeInWords,
unsigned fixedFieldCount, Kind kind = NormalKind,
unsigned buildArrayElementSizeInBytes = 0,
unsigned targetArrayElementSizeInBytes = 0,
Type arrayElementType = Type_none):
buildFixedSizeInWords(buildFixedSizeInWords),
targetFixedSizeInWords(targetFixedSizeInWords),
fixedFieldCount(fixedFieldCount),
buildArrayElementSizeInBytes(buildArrayElementSizeInBytes),
targetArrayElementSizeInBytes(targetArrayElementSizeInBytes),
arrayElementType(arrayElementType),
kind(kind)
{ }
uintptr_t* targetFixedOffsets() {
return reinterpret_cast<uintptr_t*>(this + 1);
}
Field* fixedFields() {
return reinterpret_cast<Field*>
(targetFixedOffsets() + (buildFixedSizeInWords * BytesPerWord));
}
static unsigned sizeInBytes(unsigned buildFixedSizeInWords,
unsigned fixedFieldCount)
{
return sizeof(TypeMap)
+ (buildFixedSizeInWords * BytesPerWord * BytesPerWord)
+ (sizeof(Field) * fixedFieldCount);
}
unsigned buildFixedSizeInWords;
unsigned targetFixedSizeInWords;
unsigned fixedFieldCount;
unsigned buildArrayElementSizeInBytes;
unsigned targetArrayElementSizeInBytes;
Type arrayElementType;
Kind kind;
};
// Notes on immutable references in the heap image:
//
// One of the advantages of a bootimage-based build is that reduces
// the overhead of major GCs at runtime since we can avoid scanning
// the pre-built heap image entirely. However, this only works if we
// can ensure that no part of the heap image (with exceptions noted
// below) ever points to runtime-allocated objects. Therefore (most)
// references in the heap image are considered immutable, and any
// attempt to update them at runtime will cause the process to abort.
//
// However, some references in the heap image really must be updated
// at runtime: e.g. the static field table for each class. Therefore,
// we allocate these as "fixed" objects, subject to mark-and-sweep
// collection, instead of as "copyable" objects subject to copying
// collection. This strategy avoids the necessity of maintaining
// "dirty reference" bitsets at runtime for the entire heap image;
// each fixed object has its own bitset specific to that object.
//
// In addition to the "fixed" object solution, there are other
// strategies available to avoid attempts to update immutable
// references at runtime:
//
// * Table-based: use a lazily-updated array or vector to associate
// runtime data with heap image objects (see
// e.g. getClassRuntimeData in machine.cpp).
//
// * Update references at build time: for example, we set the names
// of primitive classes before generating the heap image so that we
// need not populate them lazily at runtime.
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
getNonStaticFields(Thread* t, object typeMaps, object c, object fields,
unsigned* count, object* array)
{
PROTECT(t, typeMaps);
PROTECT(t, c);
PROTECT(t, fields);
*array = hashMapFind(t, typeMaps, c, objectHash, objectEqual);
if (*array) {
*count += reinterpret_cast<TypeMap*>(&byteArrayBody(t, *array, 0))
->fixedFieldCount;
} else {
if (classSuper(t, c)) {
fields = getNonStaticFields
(t, typeMaps, classSuper(t, c), fields, count, array);
}
if (classFieldTable(t, c)) {
for (unsigned i = 0; i < arrayLength(t, classFieldTable(t, c)); ++i) {
object field = arrayBody(t, classFieldTable(t, c), i);
if ((fieldFlags(t, field) & ACC_STATIC) == 0) {
++ (*count);
fields = vectorAppend(t, fields, field);
}
}
}
}
return vectorAppend(t, fields, 0);
}
object
allFields(Thread* t, object typeMaps, object c, unsigned* count, object* array)
{
PROTECT(t, typeMaps);
PROTECT(t, c);
object fields = makeVector(t, 0, 0);
PROTECT(t, fields);
*array = hashMapFind(t, typeMaps, c, objectHash, objectEqual);
bool includeMembers;
if (*array) {
includeMembers = false;
*count += reinterpret_cast<TypeMap*>(&byteArrayBody(t, *array, 0))
->fixedFieldCount;
} else {
includeMembers = true;
if (classSuper(t, c)) {
fields = getNonStaticFields
(t, typeMaps, classSuper(t, c), fields, count, array);
}
}
if (classFieldTable(t, c)) {
for (unsigned i = 0; i < arrayLength(t, classFieldTable(t, c)); ++i) {
object field = arrayBody(t, classFieldTable(t, c), i);
if (includeMembers or (fieldFlags(t, field) & ACC_STATIC)) {
++ (*count);
fields = vectorAppend(t, fields, field);
}
}
}
return fields;
}
TypeMap*
classTypeMap(Thread* t, object typeMaps, object p)
{
return reinterpret_cast<TypeMap*>
(&byteArrayBody
(t, hashMapFind(t, typeMaps, p, objectHash, objectEqual), 0));
}
TypeMap*
typeMap(Thread* t, object typeMaps, object p)
{
return reinterpret_cast<TypeMap*>
(&byteArrayBody
(t, objectClass(t, p) == type(t, Machine::SingletonType)
? hashMapFind(t, typeMaps, p, objectHash, objectEqual)
: hashMapFind(t, typeMaps, objectClass(t, p), objectHash, objectEqual),
0));
}
unsigned
targetFieldOffset(Thread* t, object typeMaps, object field)
{
// if (strcmp(reinterpret_cast<const char*>
// (&byteArrayBody(t, className(t, fieldClass(t, field)), 0)),
// "java/lang/Throwable") == 0) trap();
return ((fieldFlags(t, field) & ACC_STATIC)
? typeMap(t, typeMaps, classStaticTable(t, fieldClass(t, field)))
: classTypeMap(t, typeMaps, fieldClass(t, field)))
->targetFixedOffsets()[fieldOffset(t, field)];
}
object
makeCodeImage(Thread* t, Zone* zone, BootImage* image, uint8_t* code,
const char* className, const char* methodName,
const char* methodSpec, object typeMaps)
{
PROTECT(t, typeMaps);
t->m->classpath->interceptMethods(t);
object constants = 0;
PROTECT(t, constants);
object calls = 0;
PROTECT(t, calls);
object methods = 0;
PROTECT(t, methods);
DelayedPromise* addresses = 0;
class MyOffsetResolver: public OffsetResolver {
public:
MyOffsetResolver(object* typeMaps): typeMaps(typeMaps) { }
virtual unsigned fieldOffset(Thread* t, object field) {
return targetFieldOffset(t, *typeMaps, field);
}
object* typeMaps;
} resolver(&typeMaps);
Finder* finder = static_cast<Finder*>
(systemClassLoaderFinder(t, root(t, Machine::BootLoader)));
for (Finder::Iterator it(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))
{
// fprintf(stderr, "pass 1 %.*s\n", nameSize - 6, name);
object c = resolveSystemClass
(t, root(t, Machine::BootLoader),
makeByteArray(t, "%.*s", nameSize - 6, name), true);
PROTECT(t, c);
System::Region* region = finder->find(name);
{ THREAD_RESOURCE(t, System::Region*, region, region->dispose());
class Client: public Stream::Client {
public:
Client(Thread* t): t(t) { }
virtual void NO_RETURN handleError() {
abort(t);
}
private:
Thread* t;
} client(t);
Stream s(&client, region->start(), region->length());
uint32_t magic = s.read4();
expect(t, magic == 0xCAFEBABE);
s.read2(); // minor version
s.read2(); // major version
unsigned count = s.read2() - 1;
if (count) {
THREAD_RUNTIME_ARRAY(t, Type, types, count + 2);
RUNTIME_ARRAY_BODY(types)[0] = Type_object;
RUNTIME_ARRAY_BODY(types)[1] = Type_intptr_t;
for (unsigned i = 2; i < count + 2; ++i) {
switch (s.read1()) {
case CONSTANT_Class:
case CONSTANT_String:
RUNTIME_ARRAY_BODY(types)[i] = Type_object;
s.skip(2);
break;
case CONSTANT_Integer:
case CONSTANT_Float:
RUNTIME_ARRAY_BODY(types)[i] = Type_int32_t;
s.skip(4);
break;
case CONSTANT_NameAndType:
case CONSTANT_Fieldref:
case CONSTANT_Methodref:
case CONSTANT_InterfaceMethodref:
RUNTIME_ARRAY_BODY(types)[i] = Type_object;
s.skip(4);
break;
case CONSTANT_Long:
RUNTIME_ARRAY_BODY(types)[i++] = Type_int64_t;
RUNTIME_ARRAY_BODY(types)[i] = Type_int64_t_pad;
s.skip(8);
break;
case CONSTANT_Double:
RUNTIME_ARRAY_BODY(types)[i++] = Type_double;
RUNTIME_ARRAY_BODY(types)[i] = Type_double_pad;
s.skip(8);
break;
case CONSTANT_Utf8:
RUNTIME_ARRAY_BODY(types)[i] = Type_object;
s.skip(s.read2());
break;
default: abort(t);
}
}
object array = makeByteArray
(t, TypeMap::sizeInBytes(count + 2, count + 2));
TypeMap* map = new (&byteArrayBody(t, array, 0)) TypeMap
(count + 2, count + 2, count + 2, TypeMap::PoolKind);
for (unsigned i = 0; i < count + 2; ++i) {
expect(t, i < map->buildFixedSizeInWords);
map->targetFixedOffsets()[i * BytesPerWord]
= i * TargetBytesPerWord;
init(new (map->fixedFields() + i) Field, RUNTIME_ARRAY_BODY(types)[i],
i * BytesPerWord, BytesPerWord, i * TargetBytesPerWord,
TargetBytesPerWord);
}
hashMapInsert
(t, typeMaps, hashMapFind
(t, root(t, Machine::PoolMap), c, objectHash, objectEqual), array,
objectHash);
}
}
{ object array = 0;
PROTECT(t, array);
unsigned count = 0;
object fields = allFields(t, typeMaps, c, &count, &array);
PROTECT(t, fields);
THREAD_RUNTIME_ARRAY(t, Field, memberFields, count + 1);
unsigned memberIndex;
unsigned buildMemberOffset;
unsigned targetMemberOffset;
if (array) {
memberIndex = 0;
buildMemberOffset = 0;
targetMemberOffset = 0;
TypeMap* map = reinterpret_cast<TypeMap*>
(&byteArrayBody(t, array, 0));
for (unsigned j = 0; j < map->fixedFieldCount; ++j) {
Field* f = map->fixedFields() + j;
RUNTIME_ARRAY_BODY(memberFields)[memberIndex] = *f;
targetMemberOffset = f->targetOffset + f->targetSize;
++ memberIndex;
}
} else {
init(new (RUNTIME_ARRAY_BODY(memberFields)) Field, Type_object, 0,
BytesPerWord, 0, TargetBytesPerWord);
memberIndex = 1;
buildMemberOffset = BytesPerWord;
targetMemberOffset = TargetBytesPerWord;
}
const unsigned StaticHeader = 3;
THREAD_RUNTIME_ARRAY(t, Field, staticFields, count + StaticHeader);
init(new (RUNTIME_ARRAY_BODY(staticFields)) Field, Type_object, 0,
BytesPerWord, 0, TargetBytesPerWord);
init(new (RUNTIME_ARRAY_BODY(staticFields) + 1) Field, Type_intptr_t,
BytesPerWord, BytesPerWord, TargetBytesPerWord,
TargetBytesPerWord);
init(new (RUNTIME_ARRAY_BODY(staticFields) + 2) Field, Type_object,
BytesPerWord * 2, BytesPerWord, TargetBytesPerWord * 2,
TargetBytesPerWord);
unsigned staticIndex = StaticHeader;
unsigned buildStaticOffset = BytesPerWord * StaticHeader;
unsigned targetStaticOffset = TargetBytesPerWord * StaticHeader;
for (unsigned i = 0; i < vectorSize(t, fields); ++i) {
object field = vectorBody(t, fields, i);
if (field) {
unsigned buildSize = fieldSize(t, fieldCode(t, field));
unsigned targetSize = buildSize;
Type type;
switch (fieldCode(t, field)) {
case ObjectField:
type = Type_object;
targetSize = TargetBytesPerWord;
break;
case ByteField:
case BooleanField:
type = Type_int8_t;
break;
case CharField:
case ShortField:
type = Type_int8_t;
break;
case FloatField:
case IntField:
type = Type_int32_t;
break;
case LongField:
case DoubleField:
type = Type_int64_t;
break;
default: abort(t);
}
if (fieldFlags(t, field) & ACC_STATIC) {
targetStaticOffset = pad(targetStaticOffset, targetSize);
buildStaticOffset = fieldOffset(t, field);
init(new (RUNTIME_ARRAY_BODY(staticFields) + staticIndex) Field,
type, buildStaticOffset, buildSize, targetStaticOffset,
targetSize);
targetStaticOffset += targetSize;
++ staticIndex;
} else {
targetMemberOffset = pad(targetMemberOffset, targetSize);
buildMemberOffset = fieldOffset(t, field);
init(new (RUNTIME_ARRAY_BODY(memberFields) + memberIndex) Field,
type, buildMemberOffset, buildSize, targetMemberOffset,
targetSize);
targetMemberOffset += targetSize;
++ memberIndex;
}
} else {
targetMemberOffset = pad(targetMemberOffset, TargetBytesPerWord);
}
}
if (hashMapFind(t, typeMaps, c, objectHash, objectEqual) == 0) {
object array = makeByteArray
(t, TypeMap::sizeInBytes
(ceilingDivide(classFixedSize(t, c), BytesPerWord), memberIndex));
TypeMap* map = new (&byteArrayBody(t, array, 0)) TypeMap
(ceilingDivide(classFixedSize(t, c), BytesPerWord),
ceilingDivide(targetMemberOffset, TargetBytesPerWord), memberIndex);
for (unsigned i = 0; i < memberIndex; ++i) {
Field* f = RUNTIME_ARRAY_BODY(memberFields) + i;
expect(t, f->buildOffset
< map->buildFixedSizeInWords * BytesPerWord);
map->targetFixedOffsets()[f->buildOffset] = f->targetOffset;
map->fixedFields()[i] = *f;
}
hashMapInsert(t, typeMaps, c, array, objectHash);
}
if (classStaticTable(t, c)) {
object array = makeByteArray
(t, TypeMap::sizeInBytes
(singletonCount(t, classStaticTable(t, c)) + 2, staticIndex));
TypeMap* map = new (&byteArrayBody(t, array, 0)) TypeMap
(singletonCount(t, classStaticTable(t, c)) + 2,
ceilingDivide(targetStaticOffset, TargetBytesPerWord), staticIndex,
TypeMap::SingletonKind);
for (unsigned i = 0; i < staticIndex; ++i) {
Field* f = RUNTIME_ARRAY_BODY(staticFields) + i;
expect(t, f->buildOffset
< map->buildFixedSizeInWords * BytesPerWord);
map->targetFixedOffsets()[f->buildOffset] = f->targetOffset;
map->fixedFields()[i] = *f;
}
hashMapInsert
(t, typeMaps, classStaticTable(t, c), array, objectHash);
}
}
}
}
for (Finder::Iterator it(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))
{
// fprintf(stderr, "pass 2 %.*s\n", nameSize - 6, name);
object c = resolveSystemClass
(t, root(t, Machine::BootLoader),
makeByteArray(t, "%.*s", nameSize - 6, name), true);
PROTECT(t, c);
if (classMethodTable(t, c)) {
for (unsigned i = 0; i < arrayLength(t, classMethodTable(t, c)); ++i) {
object method = arrayBody(t, classMethodTable(t, c), i);
if (((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)))
{
if (methodCode(t, method)
or (methodFlags(t, method) & ACC_NATIVE))
{
PROTECT(t, method);
t->m->processor->compileMethod
(t, zone, &constants, &calls, &addresses, method, &resolver);
if (methodCode(t, method)) {
methods = makePair(t, method, methods);
}
}
object addendum = methodAddendum(t, method);
if (addendum and methodAddendumExceptionTable(t, addendum)) {
PROTECT(t, addendum);
// resolve exception types now to avoid trying to update
// immutable references at runtime
for (unsigned i = 0; i < shortArrayLength
(t, methodAddendumExceptionTable(t, addendum)); ++i)
{
uint16_t index = shortArrayBody
(t, methodAddendumExceptionTable(t, addendum), i) - 1;
object o = singletonObject
(t, addendumPool(t, addendum), index);
if (objectClass(t, o) == type(t, Machine::ReferenceType)) {
o = resolveClass
(t, root(t, Machine::BootLoader), referenceName(t, o));
set(t, addendumPool(t, addendum),
SingletonBody + (index * BytesPerWord), o);
}
}
}
}
}
}
}
}
for (; calls; calls = tripleThird(t, calls)) {
object method = tripleFirst(t, calls);
uintptr_t address;
if (methodFlags(t, method) & ACC_NATIVE) {
address = reinterpret_cast<uintptr_t>(code + image->thunks.native.start);
} else {
address = codeCompiled(t, methodCode(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());
expect(t, value >= code);
addresses->listener->resolve
(static_cast<target_intptr_t>(value - code), 0);
}
for (; methods; methods = pairSecond(t, methods)) {
codeCompiled(t, methodCode(t, pairFirst(t, methods)))
-= reinterpret_cast<uintptr_t>(code);
}
t->m->processor->normalizeVirtualThunks(t);
return constants;
}
void
visitRoots(Thread* t, BootImage* image, HeapWalker* w, object constants)
{
Machine* m = t->m;
for (HashMapIterator it(t, classLoaderMap(t, root(t, Machine::BootLoader)));
it.hasMore();)
{
w->visitRoot(tripleSecond(t, it.next()));
}
image->bootLoader = w->visitRoot(root(t, Machine::BootLoader));
image->appLoader = w->visitRoot(root(t, Machine::AppLoader));
image->types = w->visitRoot(m->types);
m->processor->visitRoots(t, w);
for (; constants; constants = tripleThird(t, constants)) {
w->visitRoot(tripleFirst(t, constants));
}
}
unsigned
targetOffset(Thread* t, object typeMaps, object p, unsigned offset)
{
TypeMap* map = typeMap(t, typeMaps, p);
if (map->targetArrayElementSizeInBytes
and offset >= map->buildFixedSizeInWords * BytesPerWord)
{
return (map->targetFixedSizeInWords * TargetBytesPerWord)
+ (((offset - (map->buildFixedSizeInWords * BytesPerWord))
/ map->buildArrayElementSizeInBytes)
* map->targetArrayElementSizeInBytes);
} else {
return map->targetFixedOffsets()[offset];
}
}
unsigned
targetSize(Thread* t, object typeMaps, object p)
{
TypeMap* map = typeMap(t, typeMaps, p);
if (map->targetArrayElementSizeInBytes) {
return map->targetFixedSizeInWords
+ ceilingDivide(map->targetArrayElementSizeInBytes
* fieldAtOffset<uintptr_t>
(p, (map->buildFixedSizeInWords - 1) * BytesPerWord),
TargetBytesPerWord);
} else {
switch (map->kind) {
case TypeMap::NormalKind:
return map->targetFixedSizeInWords;
case TypeMap::SingletonKind:
return map->targetFixedSizeInWords + singletonMaskSize
(map->targetFixedSizeInWords - 2, TargetBitsPerWord);
case TypeMap::PoolKind: {
unsigned maskSize = poolMaskSize
(map->targetFixedSizeInWords - 2, TargetBitsPerWord);
return map->targetFixedSizeInWords + maskSize + singletonMaskSize
(map->targetFixedSizeInWords - 2 + maskSize, TargetBitsPerWord);
}
default: abort(t);
}
}
}
unsigned
objectMaskCount(TypeMap* map)
{
unsigned count = map->targetFixedSizeInWords;
if (map->targetArrayElementSizeInBytes) {
++ count;
}
return count;
}
unsigned
targetSize(Thread* t, object typeMaps, object referer, unsigned refererOffset,
object p)
{
if (referer
and objectClass(t, referer) == type(t, Machine::ClassType)
and (refererOffset * BytesPerWord) == ClassObjectMask)
{
return (TargetBytesPerWord * 2)
+ pad
(ceilingDivide
(objectMaskCount
(classTypeMap(t, typeMaps, referer)), 32) * 4, TargetBytesPerWord);
} else {
return targetSize(t, typeMaps, p);
}
}
void
copy(Thread* t, uint8_t* src, uint8_t* dst, Type type)
{
switch (type) {
case Type_int8_t:
memcpy(dst, src, 1);
break;
case Type_int16_t: {
int16_t s; memcpy(&s, src, 2);
int16_t d = targetV2(s);
memcpy(dst, &d, 2);
} break;
case Type_int32_t:
case Type_float: {
int32_t s; memcpy(&s, src, 4);
int32_t d = targetV4(s);
memcpy(dst, &d, 4);
} break;
case Type_int64_t:
case Type_double: {
int64_t s; memcpy(&s, src, 8);
int64_t d = targetV8(s);
memcpy(dst, &d, 8);
} break;
case Type_int64_t_pad:
case Type_double_pad:
break;
case Type_intptr_t: {
intptr_t s; memcpy(&s, src, BytesPerWord);
target_intptr_t d = targetVW(s);
memcpy(dst, &d, TargetBytesPerWord);
} break;
case Type_object: {
memset(dst, 0, TargetBytesPerWord);
} break;
default: abort(t);
}
}
bool
nonObjectsEqual(uint8_t* src, uint8_t* dst, Type type)
{
switch (type) {
case Type_int8_t:
return memcmp(dst, src, 1) == 0;
case Type_int16_t:
return memcmp(dst, src, 2) == 0;
case Type_int32_t:
case Type_float:
return memcmp(dst, src, 4) == 0;
case Type_int64_t:
case Type_double:
return memcmp(dst, src, 8) == 0;
case Type_int64_t_pad:
case Type_double_pad:
return true;
case Type_intptr_t:
return memcmp(dst, src, BytesPerWord) == 0;
case Type_object:
case Type_object_nogc:
return true;
default: abort();
}
}
bool
nonObjectsEqual(TypeMap* map, uint8_t* src, uint8_t* dst)
{
for (unsigned i = 0; i < map->fixedFieldCount; ++i) {
Field* field = map->fixedFields() + i;
if (not nonObjectsEqual
(src + field->buildOffset, dst + field->targetOffset, field->type))
{
return false;
}
}
if (map->targetArrayElementSizeInBytes) {
unsigned fixedSize = map->buildFixedSizeInWords * BytesPerWord;
unsigned count = fieldAtOffset<uintptr_t>(src, fixedSize - BytesPerWord);
for (unsigned i = 0; i < count; ++i) {
if (not nonObjectsEqual
(src + fixedSize + (i * map->buildArrayElementSizeInBytes),
dst + (map->targetFixedSizeInWords * TargetBytesPerWord)
+ (i * map->targetArrayElementSizeInBytes), map->arrayElementType))
{
return false;
}
}
}
return true;
}
void
copy(Thread* t, object typeMaps, object p, uint8_t* dst)
{
TypeMap* map = typeMap(t, typeMaps, p);
uint8_t* src = reinterpret_cast<uint8_t*>(p);
for (unsigned i = 0; i < map->fixedFieldCount; ++i) {
Field* field = map->fixedFields() + i;
if (field->type > Type_array) abort(t);
copy(t, src + field->buildOffset, dst + field->targetOffset, field->type);
}
if (map->targetArrayElementSizeInBytes) {
unsigned fixedSize = map->buildFixedSizeInWords * BytesPerWord;
unsigned count = fieldAtOffset<uintptr_t>(p, fixedSize - BytesPerWord);
for (unsigned i = 0; i < count; ++i) {
copy(t, src + fixedSize + (i * map->buildArrayElementSizeInBytes),
dst + (map->targetFixedSizeInWords * TargetBytesPerWord)
+ (i * map->targetArrayElementSizeInBytes), map->arrayElementType);
}
if (objectClass(t, p) == type(t, Machine::ClassType)) {
uint16_t fixedSize;
uint8_t arrayElementSize;
object array = hashMapFind(t, typeMaps, p, objectHash, objectEqual);
if (array) {
TypeMap* classMap = reinterpret_cast<TypeMap*>
(&byteArrayBody(t, array, 0));
fixedSize = targetV2
(classMap->targetFixedSizeInWords * TargetBytesPerWord);
arrayElementSize = classMap->targetArrayElementSizeInBytes;
} else if (classFixedSize(t, p) == BytesPerWord * 2
and classArrayElementSize(t, p) == BytesPerWord)
{
fixedSize = targetV2(TargetBytesPerWord * 2);
arrayElementSize = TargetBytesPerWord;
} else {
fixedSize = 0;
arrayElementSize = 0;
}
if (fixedSize) {
memcpy(dst + TargetClassFixedSize, &fixedSize, 2);
memcpy(dst + TargetClassArrayElementSize, &arrayElementSize, 1);
}
// if (strcmp("vm::lineNumberTable",
// reinterpret_cast<const char*>(&byteArrayBody(t, className(t, p), 0))) == 0) trap();
}
} else {
switch (map->kind) {
case TypeMap::NormalKind:
if (objectClass(t, p) == type(t, Machine::FieldType)) {
uint16_t offset = targetV2(targetFieldOffset(t, typeMaps, p));
memcpy(dst + TargetFieldOffset, &offset, 2);
}
break;
case TypeMap::SingletonKind: {
unsigned maskSize = singletonMaskSize
(map->targetFixedSizeInWords - 2, TargetBitsPerWord);
target_uintptr_t targetLength = targetVW
(map->targetFixedSizeInWords - 2 + maskSize);
memcpy(dst + TargetBytesPerWord, &targetLength, TargetBytesPerWord);
uint8_t* mask = dst + (map->targetFixedSizeInWords * TargetBytesPerWord);
memset(mask, 0, maskSize * TargetBytesPerWord);
for (unsigned i = 0; i < map->fixedFieldCount; ++i) {
Field* field = map->fixedFields() + i;
if (field->type == Type_object) {
unsigned offset = field->targetOffset / TargetBytesPerWord;
reinterpret_cast<uint32_t*>(mask)[offset / 32]
|= targetV4(static_cast<uint32_t>(1) << (offset % 32));
}
}
if (DebugNativeTarget) {