-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathByteCodeClass.java
More file actions
2169 lines (1955 loc) · 77.9 KB
/
ByteCodeClass.java
File metadata and controls
2169 lines (1955 loc) · 77.9 KB
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) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.tools.translator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/**
* Parsed class file
*
* @author Shai Almog
*/
public class ByteCodeClass {
/**
* @param isAnonymous the isAnonymous to set
*/
public void setIsAnonymous(boolean isAnonymous) {
this.isAnonymous = isAnonymous;
}
/**
* @param isSynthetic the isSynthetic to set
*/
public void setIsSynthetic(boolean isSynthetic) {
this.isSynthetic = isSynthetic;
}
/**
* @param isAnnotation the isAnnotation to set
*/
public void setIsAnnotation(boolean isAnnotation) {
this.isAnnotation = isAnnotation;
}
private List<ByteCodeField> fullFieldList;
private List<ByteCodeField> staticFieldList;
private Set<String> dependsClassesInterfaces = new TreeSet<String>();
private Set<String> exportsClassesInterfaces = new TreeSet<String>();
private List<BytecodeMethod> methods = new ArrayList<BytecodeMethod>();
private List<ByteCodeField> fields = new ArrayList<ByteCodeField>();
private String clsName;
private String originalClassName;
private String baseClass;
private String concreteClass;
private List<String> baseInterfaces;
private boolean isInterface;
private boolean isAbstract;
private boolean isSynthetic;
private boolean isAnnotation;
private boolean isAnonymous;
private boolean eliminated;
private static boolean saveUnitTests;
private boolean isUnitTest;
private static Set<String> arrayTypes = new TreeSet<String>();
private ByteCodeClass baseClassObject;
private List<ByteCodeClass> baseInterfacesObject;
List<BytecodeMethod> virtualMethodList;
private String sourceFile;
private int classOffset;
private boolean marked;
private static ByteCodeClass mainClass;
private boolean finalClass;
private boolean isEnum;
private static Set<String> writableFields = new HashSet<String>();
static void cleanup() {
arrayTypes.clear();
writableFields.clear();
mainClass = null;
saveUnitTests = false;
}
/**
*
* @param clsName Class name with mangling. e.g. java_lang_String
* @param originalClassName Classname without mangling. e.g. java/lang/String
*/
public ByteCodeClass(String clsName, String originalClassName) {
this.clsName = clsName;
this.originalClassName = originalClassName;
}
/**
* Checks if this class has been eliminated.
* @return
*/
public boolean isEliminated() {
return eliminated;
}
/**
* Marks class as eliminated. Will recursively set all class methods
* as eliminated too.
* @param eliminated True to set eliminated.
* @return Number of methods that were newly marked as eliminated by this call.
*/
public int setEliminated(boolean eliminated) {
int nfound = 0;
if (this.eliminated) return nfound;
this.eliminated = eliminated;
if (eliminated) {
for (BytecodeMethod m : methods) {
if (!m.isEliminated()) {
m.setEliminated(true);
nfound++;
}
}
}
return nfound;
}
/**
* Class name in original JVM format: e.g. java/lang/String
* @return
*/
public String getOriginalClassName() {
return originalClassName;
}
static ByteCodeClass getMainClass() {
return mainClass;
}
static void setSaveUnitTests(boolean save) {
saveUnitTests = save;
}
public void addMethod(BytecodeMethod m) {
if(m.isMain()) {
if (mainClass == null) {
mainClass = this;
} else {
throw new RuntimeException("Multiple main classes: "+mainClass.clsName+" and "+this.clsName);
}
}
m.setSourceFile(sourceFile);
m.setForceVirtual(isInterface);
methods.add(m);
}
public void addField(ByteCodeField m) {
fields.add(m);
}
public String generateCSharpCode() {
return "";
}
public String generateJavascriptCode(List<ByteCodeClass> allClasses) {
return JavascriptMethodGenerator.generateClassJavascript(this, allClasses);
}
public void addWritableField(String field) {
writableFields.add(field);
}
/**
* Marks dependencies in this class based on the provided classes in this round of optimization.
* @param lst The list of classes that are available in this optimization step.
* @param nativeSources Array of native sources in this round. Used to check if native files reference
* this class or methods.
*/
public static void markDependencies(List<ByteCodeClass> lst, String[] nativeSources) {
mainClass.markDependent(lst);
for(ByteCodeClass bc : lst) {
if (bc.marked) {
continue;
}
if (bc.isEliminated()) {
continue;
}
if(bc.clsName.equals("java_lang_Boolean")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_String")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_Integer")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_Byte")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_Short")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_Character")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_Thread")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_Long")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_Double")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_Float")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_lang_StackOverflowError")) {
bc.markDependent(lst);
continue;
}
if(bc.clsName.equals("java_text_DateFormat")) {
bc.markDependent(lst);
continue;
}
if (bc.getUsedByNative() == UsedByNativeResult.Unknown) {
// We don't yet know if this class is used by native
// calculate it now.
bc.calcUsedByNative(nativeSources);
}
if(bc.getUsedByNative() == UsedByNativeResult.Used){
bc.markDependent(lst);
continue;
}
if(saveUnitTests && bc.isUnitTest) {
bc.markDependent(lst);
continue;
}
}
// mark all non-final classes that aren't inherited as final for use in
// additional optimizations
for(ByteCodeClass bc : lst) {
if(bc.isFinalClass() || bc.isInterface || bc.isIsAbstract()) {
continue;
}
boolean found = false;
for(ByteCodeClass bk : lst) {
if(bk.baseClassObject == bc) {
found = true;
break;
}
}
if(!found) {
bc.setFinalClass(true);
}
}
// we try to disable the "virtual" aspect of methods where possible
for(ByteCodeClass bc : lst) {
if(bc.isFinalClass()) {
for(BytecodeMethod meth : bc.methods) {
if(meth.canBeVirtual() && !bc.isMethodFromBaseOrInterface(meth)) {
meth.setVirtualOverriden(true);
}
}
}
}
}
public boolean isMethodPrivate(String name, String desc) {
for (BytecodeMethod meth : methods) {
if (meth.getMethodName().equals(name) && desc.equals(meth.getSignature())) {
return meth.isPrivate();
}
}
return false;
}
public ByteCodeClass findMethodOwner(String name, String desc) {
return findMethodOwner(name, desc, new HashSet<ByteCodeClass>());
}
private ByteCodeClass findMethodOwner(String name, String desc, Set<ByteCodeClass> visited) {
if (!visited.add(this)) {
return null;
}
BytecodeMethod declaredMethod = findDeclaredMethod(name, desc);
if (declaredMethod != null && !declaredMethod.isAbstract()) {
return this;
}
if (baseClassObject != null) {
ByteCodeClass owner = baseClassObject.findMethodOwner(name, desc, visited);
if (owner != null) {
return owner;
}
}
if (baseInterfacesObject != null) {
for (ByteCodeClass iface : baseInterfacesObject) {
ByteCodeClass owner = iface.findMethodOwner(name, desc, visited);
if (owner != null) {
return owner;
}
}
}
return null;
}
private BytecodeMethod findDeclaredMethod(String name, String desc) {
for (BytecodeMethod meth : methods) {
if (meth.getMethodName().equals(name) && desc.equals(meth.getSignature())) {
return meth;
}
}
return null;
}
public boolean hasDeclaredNonAbstractMethod(String name, String desc) {
BytecodeMethod declaredMethod = findDeclaredMethod(name, desc);
return declaredMethod != null && !declaredMethod.isAbstract();
}
public void unmark() {
marked = false;
}
private void markDependent(List<ByteCodeClass> lst) {
if(marked) {
return;
}
marked = true;
// make sure the method/classname are in the constant pool so we can later
// look them up in case of a stack trace exception
Parser.addToConstantPool(clsName);
for(BytecodeMethod bm : methods) {
if(!bm.isEliminated()) {
Parser.addToConstantPool(bm.getMethodName());
bm.addToConstantPool();
}
}
for(String s : dependsClassesInterfaces) {
ByteCodeClass cls = findClass(s, lst);
// annotation can be null
if(cls != null) {
cls.markDependent(lst);
}
}
}
public static List<ByteCodeClass> clearUnmarked(List<ByteCodeClass> lst) {
List<ByteCodeClass> response = new ArrayList<ByteCodeClass>();
for(ByteCodeClass bc : lst) {
if(bc.marked) {
response.add(bc);
}
}
return response;
}
private ByteCodeClass findClass(String s, List<ByteCodeClass> lst) {
for(ByteCodeClass c : lst) {
if(c.clsName.equals(s)) {
return c;
}
}
return null;
}
public void updateAllDependencies() {
dependsClassesInterfaces.clear();
exportsClassesInterfaces.clear();
dependsClassesInterfaces.add("java_lang_NullPointerException");
setBaseClass(baseClass);
if (isAnnotation) {
dependsClassesInterfaces.add("java_lang_annotation_Annotation");
}
for(String s : baseInterfaces) {
s = s.replace('/', '_').replace('$', '_');
if(!dependsClassesInterfaces.contains(s)) {
dependsClassesInterfaces.add(s);
}
exportsClassesInterfaces.add(s);
}
if(virtualMethodList != null) {
virtualMethodList.clear();
} else {
virtualMethodList = new ArrayList<BytecodeMethod>();
}
fillVirtualMethodTable(virtualMethodList);
for(BytecodeMethod m : methods) {
if(m.isEliminated()) {
continue;
}
for(String s : m.getDependentClasses()) {
if(!dependsClassesInterfaces.contains(s)) {
dependsClassesInterfaces.add(s);
}
}
//for (String s : m.getExportedClasses()) {
// exportsClassesInterfaces.add(s);
//}
}
for(ByteCodeField m : fields) {
for(String s : m.getDependentClasses()) {
if(!dependsClassesInterfaces.contains(s)) {
dependsClassesInterfaces.add(s);
}
}
//for (String s : m.getExportedClasses()) {
// exportsClassesInterfaces.add(s);
//}
}
}
private boolean isMethodFromBaseOrInterface(BytecodeMethod bm) {
if(baseInterfacesObject != null) {
for(ByteCodeClass bi : baseInterfacesObject) {
if(bi.getMethods().contains(bm)) {
return true;
}
if(bi.getBaseClassObject() != null) {
boolean b = bi.isMethodFromBaseOrInterface(bm);
if(b) {
return true;
}
}
}
}
if(baseClassObject != null) {
if(baseClassObject.getMethods().contains(bm)) {
return true;
}
return baseClassObject.isMethodFromBaseOrInterface(bm);
}
return false;
}
private boolean hasDefaultConstructor() {
for(BytecodeMethod bm : methods) {
if(bm.isDefaultConstructor()) {
return true;
}
}
return false;
}
private boolean hasFinalizer() {
for(BytecodeMethod bm : methods) {
if(bm.isFinalizer()) {
return true;
}
}
return false;
}
private boolean isInterfaceInHierarchy(String className) {
if (clsName.equals(className)) {
return true;
}
if (baseInterfacesObject != null) {
for (ByteCodeClass bc : baseInterfacesObject) {
if (bc.isInterfaceInHierarchy(className)) {
return true;
}
}
}
return false;
}
public static void addArrayType(String type, int dimenstions) {
String arr = dimenstions + "_" + type;
if(!arrayTypes.contains(arr)) {
arrayTypes.add(arr);
}
}
public String generateCCode(List<ByteCodeClass> allClasses) {
StringBuilder b = new StringBuilder();
b.append("#include \"");
b.append(clsName);
b.append(".h\"\n");
for(String s : dependsClassesInterfaces) {
if (exportsClassesInterfaces.contains(s)) {
continue;
}
b.append("#include \"");
b.append(s);
b.append(".h\"\n");
}
b.append("const struct clazz *base_interfaces_for_");
b.append(clsName);
b.append("[] = {");
boolean first = true;
for(String ints : baseInterfaces) {
if(!first) {
b.append(", ");
}
first = false;
b.append("&class__");
b.append(ints.replace('/', '_').replace('$', '_'));
}
b.append("};\n");
// class struct, contains vtable, static fields and meta data (class name), type info etc.
b.append("struct clazz class__");
b.append(clsName);
b.append(" = {\n");
// object fields so class will be compatible to object
if(clsName.equals("java_lang_Class")) {
b.append(" DEBUG_GC_INIT 0, 999999, 0, 0, 0, 0, &__FINALIZER_");
} else {
b.append(" DEBUG_GC_INIT &class__java_lang_Class, 999999, 0, 0, 0, 0, &__FINALIZER_");
}
b.append(clsName);
b.append(" ,0 , &__GC_MARK_");
b.append(clsName);
// initialized defaults to false
b.append(", 0, ");
// the numberic id of the class
b.append("cn1_class_id_");
b.append(clsName);
b.append(", ");
// name of the class
b.append("\"");
b.append(clsName.replace('_', '.'));
b.append("\", ");
// is array class type
b.append("0, ");
// array type dimensions
b.append("0, ");
// array internal type
b.append("0, ");
// primitive type
b.append("JAVA_FALSE, ");
// reference to the base class
if(baseClass != null) {
b.append("&class__");
b.append(baseClass.replace('/', '_').replace('$', '_'));
} else {
b.append("(const struct clazz*)0");
}
b.append(", ");
// references to the base interfaces
b.append("base_interfaces_for_");
b.append(clsName);
b.append(", ");
// number of base interfaces
b.append(baseInterfaces.size());
// new instance function pointer
if(!isInterface && !isAbstract && hasDefaultConstructor()) {
b.append(", &__NEW_INSTANCE_");
b.append(clsName);
} else {
b.append(", 0");
}
// vtable
b.append(", 0\n");
if (isEnum) {
b.append(", &__VALUE_OF_");
b.append(clsName);
} else {
b.append(", 0");
}
/*
JAVA_BOOLEAN isSynthetic;
JAVA_BOOLEAN isInterface;
JAVA_BOOLEAN isAnonymous;
JAVA_BOOLEAN isAnnotation;
*/
b
.append(", ")
.append(isSynthetic?"JAVA_TRUE":"0")
.append(", ")
.append(isInterface?"JAVA_TRUE":"0")
.append(", ")
.append(isAnonymous?"JAVA_TRUE":"0")
.append(", ")
.append(isAnnotation?"JAVA_TRUE":"0")
.append(", ")
.append(getArrayClazz(1));
b.append("};\n\n");
// create class objects for 1 - 3 dimension arrays
for(int iter = 1 ; iter < 4 ; iter++) {
if(!(arrayTypes.contains(iter + "_" + clsName) || arrayTypes.contains((iter + 1) + "_" + clsName) ||
arrayTypes.contains((iter + 2) + "_" + clsName))) {
continue;
}
b.append("struct clazz class_array");
b.append(iter);
b.append("__");
b.append(clsName);
if(clsName.equals("java_lang_Class")) {
b.append(" = {\n DEBUG_GC_INIT 0, 999999, 0, 0, 0, 0, 0, &arrayFinalizerFunction, &gcMarkArrayObject, 0, cn1_array_");
} else {
b.append(" = {\n DEBUG_GC_INIT &class__java_lang_Class, 999999, 0, 0, 0, 0, 0, &arrayFinalizerFunction, &gcMarkArrayObject, 0, cn1_array_");
}
b.append(iter);
b.append("_id_");
b.append(clsName);
b.append(", \"");
b.append(clsName.replace('_', '.'));
for (int arrayDim = 0; arrayDim < iter; arrayDim++) {
b.append("[]");
}
b.append("\", ");
// array class type, dimension & internal type
b.append("JAVA_TRUE, ");
b.append(iter);
b.append(", &class__");
b.append(clsName);
/*
JAVA_BOOLEAN primitiveType;
const struct clazz* baseClass;
const struct clazz** baseInterfaces;
const int baseInterfaceCount;
void* newInstanceFp;
// virtual method table lookup
void** vtable;
void* enumValueOfFp;
JAVA_BOOLEAN isSynthetic;
JAVA_BOOLEAN isInterface;
JAVA_BOOLEAN isAnonymous;
JAVA_BOOLEAN isAnnotation;
*/
// primitive type is always false here object is always the base class of the array it has no base interfaces
b.append(", JAVA_FALSE, &class__java_lang_Object, EMPTY_INTERFACES, 0, ");
// new instance function pointer and vtable
b.append("0, 0, 0, 0, 0, 0, 0, "+getArrayClazz(iter+1)+"\n};\n\n");
}
staticFieldList = new ArrayList<ByteCodeField>();
buildStaticFieldList(staticFieldList);
String enumValuesField = null;
// static fields for the class
for(ByteCodeField bf : staticFieldList) {
if(bf.isStaticField() && bf.getClsName().equals(clsName)) {
if (isEnum && ("_VALUES".equals(bf.getFieldName().replace('$','_')) || "ENUM_VALUES".equals(bf.getFieldName().replace('$','_')))) {
enumValuesField = bf.getFieldName();
}
if(bf.isFinal() && bf.getValue() != null && !writableFields.contains(bf.getFieldName())) {
// static getter
b.append(bf.getCDefinition());
b.append(" get_static_");
b.append(clsName);
b.append("_");
b.append(bf.getFieldName().replace('$', '_'));
b.append("() {\n return ");
if(bf.getValue() instanceof String) {
b.append("STRING_FROM_CONSTANT_POOL_OFFSET(");
b.append(Parser.addToConstantPool((String)bf.getValue()));
b.append(") /* ");
b.append(String.valueOf(bf.getValue()).replace("*/", "* /"));
b.append(" */");
} else {
if(bf.getValue() instanceof Number) {
if(bf.getValue() instanceof Double) {
Double d = ((Double)bf.getValue());
if(d.isNaN()) {
b.append("0.0/0.0");
} else {
if(d.isInfinite()) {
if(d.doubleValue() > 0) {
b.append("1.0f / 0.0f");
} else {
b.append("-1.0f / 0.0f");
}
} else {
b.append(bf.getValue());
}
}
} else {
if(bf.getValue() instanceof Float) {
Float d = ((Float)bf.getValue());
if(d.isNaN()) {
b.append("0.0/0.0");
} else {
if(d.isInfinite()) {
if(d.floatValue() > 0) {
b.append("1.0f / 0.0f");
} else {
b.append("-1.0f / 0.0f");
}
} else {
b.append(bf.getValue());
}
}
} else {
b.append(bf.getValue());
}
}
} else {
if(bf.getValue() instanceof Boolean) {
if(((Boolean)bf.getValue()).booleanValue()) {
b.append("JAVA_TRUE");
} else {
b.append("JAVA_FALSE");
}
} else {
b.append("JAVA_NULL");
}
}
}
b.append(";\n}\n\n");
} else {
b.append(bf.getCStorageDefinition());
b.append(" STATIC_FIELD_");
b.append(clsName);
b.append("_");
b.append(bf.getFieldName());
if (bf.isVolatile()) {
b.append(" = ATOMIC_VAR_INIT(0);\n");
} else {
b.append(" = 0;\n");
}
// static getter
b.append(bf.getCDefinition());
b.append(" get_static_");
b.append(clsName);
b.append("_");
b.append(bf.getFieldName().replace('$', '_'));
b.append("() {\n __STATIC_INITIALIZER_");
b.append(bf.getClsName());
if (bf.isVolatile()) {
b.append("(getThreadLocalData());\n return atomic_load_explicit(&STATIC_FIELD_");
b.append(bf.getClsName());
b.append("_");
b.append(bf.getFieldName());
b.append(", memory_order_acquire);\n}\n\n");
} else {
b.append("(getThreadLocalData());\n return STATIC_FIELD_");
b.append(bf.getClsName());
b.append("_");
b.append(bf.getFieldName());
b.append(";\n}\n\n");
}
// Static object fields may interact with heap bookkeeping, so they keep thread context.
b.append("void set_static_");
b.append(clsName);
b.append("_");
b.append(bf.getFieldName().replace('$', '_'));
b.append("(");
if (bf.isObjectType()) {
b.append("CODENAME_ONE_THREAD_STATE, ");
}
b.append(bf.getCDefinition());
b.append(" __cn1StaticVal) {\n __STATIC_INITIALIZER_");
b.append(bf.getClsName());
if (bf.isObjectType()) {
b.append("(threadStateData);\n ");
} else {
b.append("(getThreadLocalData());\n ");
}
if (bf.isVolatile()) {
b.append("atomic_store_explicit(&STATIC_FIELD_");
b.append(bf.getClsName());
b.append("_");
b.append(bf.getFieldName());
b.append(", __cn1StaticVal, memory_order_release);");
} else {
b.append("STATIC_FIELD_");
b.append(bf.getClsName());
b.append("_");
b.append(bf.getFieldName());
b.append(" = __cn1StaticVal;");
}
if(bf.shouldRemoveFromHeapCollection()) {
if(bf.getType() != null && bf.getType().endsWith("String")) {
b.append("\n removeObjectFromHeapCollection(threadStateData, __cn1StaticVal);\n if(__cn1StaticVal != 0) {\n removeObjectFromHeapCollection(threadStateData, ((struct obj__java_lang_String*)__cn1StaticVal)->java_lang_String_value);\n }\n}\n\n");
} else {
b.append("\n removeObjectFromHeapCollection(threadStateData, __cn1StaticVal);\n}\n\n");
}
} else {
b.append("\n}\n\n");
}
}
}
}
if(isInterface) {
b.append("int **classToInterfaceMap_");
b.append(clsName);
b.append(";\n");
}
fullFieldList = new ArrayList<ByteCodeField>();
buildInstanceFieldList(fullFieldList);
String nullCheck = "";
if (System.getProperty("fieldNullChecks", "false").equals("true")) {
nullCheck = "if(__cn1T == JAVA_NULL){throwException(getThreadLocalData(), __NEW_INSTANCE_java_lang_NullPointerException(getThreadLocalData()));}\n";
}
for(ByteCodeField fld : fullFieldList) {
b.append(fld.getCDefinition());
b.append(" get_field_");
b.append(clsName);
b.append("_");
b.append(fld.getFieldName());
b.append("(JAVA_OBJECT __cn1T) {\n ").append(nullCheck).append(" ");
if (fld.isVolatile()) {
b.append("return atomic_load_explicit(&((struct obj__");
b.append(clsName);
b.append("*)__cn1T)->");
b.append(fld.getClsName());
b.append("_");
b.append(fld.getFieldName());
b.append(", memory_order_acquire);\n}\n\n");
} else {
b.append("return ((struct obj__");
b.append(clsName);
b.append("*)__cn1T)->");
b.append(fld.getClsName());
b.append("_");
b.append(fld.getFieldName());
b.append(";\n}\n\n");
}
// Instance field setters don't use thread context directly.
b.append("void set_field_");
b.append(clsName);
b.append("_");
b.append(fld.getFieldName());
b.append("(");
b.append(fld.getCDefinition());
if(fld.isObjectType()) {
b.append(" __cn1Val, JAVA_OBJECT __cn1T) {\n ").append(nullCheck).append(" ");
} else {
b.append(" __cn1Val, JAVA_OBJECT __cn1T) {\n ").append(nullCheck).append(" ");
}
if (fld.isVolatile()) {
b.append("atomic_store_explicit(&((struct obj__");
b.append(clsName);
b.append("*)__cn1T)->");
b.append(fld.getClsName());
b.append("_");
b.append(fld.getFieldName());
b.append(", __cn1Val, memory_order_release);\n}\n\n");
} else {
b.append("((struct obj__");
b.append(clsName);
b.append("*)__cn1T)->");
b.append(fld.getClsName());
b.append("_");
b.append(fld.getFieldName());
b.append(" = __cn1Val;\n}\n\n");
}
}
// finalizer and GC_RELEASE to cleanup variables
b.append("JAVA_VOID __FINALIZER_");
b.append(clsName);
b.append("(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT objToDelete) {\n");
if(hasFinalizer()) {
b.append(" ");
b.append(clsName);
b.append("_finalize__(threadStateData, objToDelete);\n");
}
// invoke the finalize method of the base
if(baseClass != null) {
b.append(" __FINALIZER_");
b.append(baseClass.replace('/', '_').replace('$', '_'));
b.append("(threadStateData, objToDelete);\n");
}
b.append("}\n\n");
// mark function for the GC mark cycle to tag the objects that are reachable
b.append("void __GC_MARK_");
b.append(clsName);
b.append("(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT objToMark, JAVA_BOOLEAN force) {\n struct obj__");
b.append(clsName);
b.append("* objInstance = (struct obj__");
b.append(clsName);
b.append("*)objToMark;\n");
for(ByteCodeField fld : fullFieldList) {
if(!fld.isStaticField() && fld.isObjectType() && fld.getClsName().equals(clsName)) {
b.append(" gcMarkObject(threadStateData, ");
if (fld.isVolatile()) {
b.append("atomic_load_explicit(&objInstance->");
b.append(fld.getClsName());
b.append("_");
b.append(fld.getFieldName());
b.append(", memory_order_acquire)");
} else {
b.append("objInstance->");
b.append(fld.getClsName());
b.append("_");
b.append(fld.getFieldName());
}
b.append(", force);\n");
}
}
// invoke the mark method of the base
if(baseClass != null) {
b.append(" __GC_MARK_");
b.append(baseClass.replace('/', '_').replace('$', '_'));
b.append("(threadStateData, objToMark, force);\n");
} else {
// we can do this in Object.java only since all code will reach here eventually
b.append(" objToMark->__codenameOneGcMark = currentGcMarkValue;\n");
}
b.append("}\n\n");
// initialize object instances
if(!isInterface && !isAbstract) {
b.append("JAVA_OBJECT __NEW_");
b.append(clsName);
b.append("(CODENAME_ONE_THREAD_STATE) {\n __STATIC_INITIALIZER_");
b.append(clsName);
b.append("(threadStateData);\n JAVA_OBJECT o = codenameOneGcMalloc(threadStateData, sizeof(struct obj__");
b.append(clsName);
b.append("), &class__");
b.append(clsName);
b.append(");\n return o;\n}\n\n");
if(hasDefaultConstructor()) {
b.append("JAVA_OBJECT __NEW_INSTANCE_");
b.append(clsName);
b.append("(CODENAME_ONE_THREAD_STATE) {\n __STATIC_INITIALIZER_");
b.append(clsName);
b.append("(threadStateData);\n JAVA_OBJECT o = codenameOneGcMalloc(threadStateData, sizeof(struct obj__");
b.append(clsName);
b.append("), &class__");
b.append(clsName);
b.append(");\n");
b.append(clsName);
b.append("___INIT____(threadStateData, o);\n return o;\n}\n\n");
}
}
if(arrayTypes.contains("1_" + clsName) || arrayTypes.contains("2_" + clsName) || arrayTypes.contains("3_" + clsName)) {
b.append("JAVA_OBJECT __NEW_ARRAY_");