-
Notifications
You must be signed in to change notification settings - Fork 4
/
compiler.c
1109 lines (936 loc) · 27.6 KB
/
compiler.c
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
/*
This file is part of shuJIT,
Just In Time compiler for Sun Java Virtual Machine.
Copyright (C) 1998,1999,2000,2001,2002,2003,2004 Kazuyuki Shudo
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id$
*/
#include <stdlib.h> // for getenv(),atoi(),atexit()
#include <sys/stat.h> // for gdbm_open(), stat()
#include <unistd.h> // for stat()
#include <string.h> // for memset()
#include "compiler.h"
#ifdef HAVE_POSIX_SCHED
# include <sched.h>
#endif
#if defined(EXC_BY_SIGNAL) || defined(GET_SIGCONTEXT)
# include <signal.h>
#endif // EXC_BY_SIGNAL || GET_SIGCONTEXT
#if JDK_VER >= 12
# include "jit.h" // for JITInterface, JITInterface6
#endif
#ifdef CODE_DB
# include <fcntl.h> // for O_...
# include <dlfcn.h> // for dl*()
#endif // CODE_DB
#ifdef METAVM
# include "jni.h"
# include "metavm/metavm.h" // for macro METAVM_PKG
#endif
//
// Global Variables
//
#if defined(linux) && (JDK_VER >= 12)
extern int _JVM_native_threads;
#endif
sys_mon_t *global_monitor;
#if JDK_VER >= 12
bool_t executejava_in_asm;
#endif
#ifndef IGNORE_DISABLE
bool_t compiler_enabled = TRUE;
#endif
// for strictfp
bool_t is_fpupc_double = FALSE;
#ifdef CODE_DB
# ifdef GDBM
# include <gdbm.h>
GDBM_FILE db = NULL;
# else
# include <ndbm.h>
DBM *db = NULL;
# endif
int db_page = -1;
#endif // CODE_DB
ClassClass *classJavaLangNoClassDefFoundError;
ClassClass *classJavaLangNoSuchFieldError;
ClassClass *classJavaLangNoSuchMethodError;
struct bool_opt_entry { char *name; int num; };
struct bool_opt_entry bool_opt_entry[] = {
{"quiet", OPT_QUIET},
{"igndisable", OPT_IGNDISABLE},
{"outcode", OPT_OUTCODE},
{"codesize", OPT_CODESIZE},
{"cmplatload", OPT_CMPLATLOAD},
{"cmplclinit", OPT_CMPLCLINIT},
{"ignstrictfp", OPT_IGNSTRICTFP},
{"frcstrictfp", OPT_FRCSTRICTFP},
{"fpsingle", OPT_FPSINGLE},
{"fpextended", OPT_FPEXTENDED},
#ifdef HAVE_POSIX_SCHED
{"sched_fifo", OPT_SCHED_FIFO},
{"sched_rr", OPT_SCHED_RR},
#endif
#ifdef CODE_DB
{"codedb", OPT_CODEDB},
#endif
{"ignlock", OPT_IGNLOCK},
{NULL, -1}
};
int options = 0;
int opt_systhreshold = LAZY_COMPILATION_THRESHOLD_FOR_SYS;
int opt_userthreshold = LAZY_COMPILATION_THRESHOLD_FOR_USER;
#ifdef METHOD_INLINING
int opt_inlining_maxlen = METHOD_INLINING_MAXLEN;
int opt_inlining_depth = METHOD_INLINING_DEPTH;
#endif
void *sym_compileAndInvokeMethod, *sym_invokeJITCompiledMethod;
void *sym_invokeJavaMethod, *sym_invokeSynchronizedJavaMethod;
void *sym_invokeAbstractMethod;
void *sym_invokeNativeMethod, *sym_invokeSynchronizedNativeMethod;
void *sym_invokeJNINativeMethod, *sym_invokeJNISynchronizedNativeMethod;
void *sym_invokeLazyNativeMethod;
#if JDK_VER >= 12
uint32_t sym_invokeJNI_min, sym_invokeJNI_max;
#endif
#ifdef CODE_DB
# ifdef GDBM
GDBM_FILE (*sym_dbm_open)(char *,int,int,int,void (*)());
void (*sym_dbm_close)(GDBM_FILE);
int (*sym_dbm_store)(GDBM_FILE,datum,datum,int);
datum (*sym_dbm_fetch)(GDBM_FILE,datum);
void (*sym_dbm_sync)(GDBM_FILE);
# else
DBM *(*sym_dbm_open)(const char *,int,int);
void (*sym_dbm_close)(DBM *);
int (*sym_dbm_store)(DBM *,datum,datum,int);
datum (*sym_dbm_fetch)(DBM *,datum);
# endif
#endif // CODE_DB
//
// Local Functions
//
static void initializeClassHook(ClassClass *);
static void freeClass(ClassClass *);
static unsigned char *compiledCodePC(JavaFrame *, struct methodblock *);
static bool_t pcInCompiledCode(unsigned char *pc, struct methodblock *);
#ifdef DIRECT_INVOCATION
static JavaFrame *framePrev(JavaFrame *frame, JavaFrame *frame_buf);
#endif // DIRECT_INVOCATION
#if JDK_VER >= 12
static void *frameID(JavaFrame *frame);
#endif
#ifndef IGNORE_DISABLE
static void compilerEnable();
static void compilerDisable();
#endif // IGNORE_DISABLE
static bool_t compileClass(ClassClass *);
static bool_t compileClasses(Hjava_lang_String *);
static HObject *compilerCommand(HObject *);
/*
* Initialization of the compiler.
* This method is called by JVM.
*/
void java_lang_Compiler_start(
#if JDK_VER < 12
void ** vector
#else
JITInterface *jitinterface
#endif
) {
ExecEnv *ee = EE();
int version;
int compilerVersion;
#ifdef COMPILE_DEBUG
printf("java_lang_Compiler_start() called.\n");
printf(" ee: 0x%x\n", ee);
# if defined(linux) && (JDK_VER >= 12)
printf(" _JVM_native_threads: %s\n", _JVM_native_threads ? "true":"false");
# endif
fflush(stdout);
#endif
// prepare the global monitor
global_monitor = (sys_mon_t *)sysCalloc(1, sysMonitorSizeof());
sysMonitorInit(global_monitor);
#if JDK_VER >= 12
// judge whether interpreter (asm or C) is used
# ifdef DEBUG
executejava_in_asm = FALSE;
# else
if (pExecuteJava == ExecuteJava_C)
executejava_in_asm = FALSE;
else if (pExecuteJava == ExecuteJava)
executejava_in_asm = TRUE;
else {
printf("FATAL: could not determine whether interpreter is used, C version or asm version.\n");
JVM_Exit(1);
}
# endif // DEBUG
#ifdef COMPILE_DEBUG
printf("interpreter: ");
if (executejava_in_asm)
printf("asm version.\n");
else
printf("C version.\n");
fflush(stdout);
#endif
#endif // JDK_VER
#if JDK_VER < 12
// prevent unloading of java.lang.Compiler class
{
ClassClass *compiler_cb = FindClass(ee, "java/lang/Compiler", TRUE);
if (compiler_cb == NULL) {
printf("FATAL: cannot find java.lang.Compiler class.\n");
if (exceptionOccurred(ee)) {
JHandle *exc = ee->exception.exc;
if (exc != NULL)
fprintf(stderr, "%s\n", cbName(exc->methods->classdescriptor));
#if JDK_VER >= 12
printStackTrace(ee, 100, NULL);
#endif
fflush(stderr);
}
JVM_Exit(1);
}
CCSet(compiler_cb, Sticky);
}
#endif
#ifdef METAVM
// force JVM to load Proxy class
{
ClassClass *proxy_cb;
#if 1
proxy_cb = FindClass(ee, METAVM_PKG "Proxy", TRUE);
#else
proxy_cb = LoadClassLocally(METAVM_PKG "Proxy");
#endif
if (proxy_cb == NULL) {
printf("FATAL: cannot find " METAVM_PKG "Proxy class.\n");
#if JDK_VER >= 12
printf("You have to place MetaVM classes in JDK_DIR/jre/classes.\n");
#endif
fflush(stdout);
JVM_Exit(1);
}
// prevent unloading
CCSet(proxy_cb, Sticky);
}
#endif // METAVM
// version check
#if JDK_VER < 12
version = *(int *)vector[0];
#else
version = *(jitinterface->JavaVersion);
#endif
#ifdef COMPILE_DEBUG
printf(" version num. of class file format: %d.%d\n",
(version >> 16) & 0xff, version & 0xffff);
#endif // COMPILE_DEBUG
compilerVersion = version >> 24;
if (compilerVersion != COMPILER_VERSION) {
printf("warning: version num. of compiler interface is not %d: %d\n",
COMPILER_VERSION, compilerVersion);
}
// get options
{
char *opts = getenv("JAVA_COMPILER_OPT");
char *opt;
if (opts) {
#ifdef RUNTIME_DEBUG
printf(" JAVA_COMPILER_OPT: %s\n", opts);
fflush(stdout);
#endif
opt = strtok(opts, ", ");
while (opt) {
int i = 0;
char *opt_name;
while (opt_name = bool_opt_entry[i].name, opt_name) {
if (!strcmp(opt, opt_name)) {
OPT_SET(bool_opt_entry[i].num);
if (!OPT_SETQ(OPT_QUIET)) printf(" option: %s\n", opt_name);
break;
}
i++;
}
if (!strncmp(opt, "systhreshold=", 13)) {
opt_systhreshold = atoi(opt + 13);
if (!OPT_SETQ(OPT_QUIET))
printf(" option: systhreshold = %d\n", opt_systhreshold);
}
if (!strncmp(opt, "userthreshold=", 14)) {
opt_userthreshold = atoi(opt + 14);
if (!OPT_SETQ(OPT_QUIET))
printf(" option: userthreshold = %d\n", opt_userthreshold);
}
#ifdef METHOD_INLINING
else if (!strncmp(opt, "inlinemaxlen=", 13)) {
opt_inlining_maxlen = atoi(opt + 13);
if (!OPT_SETQ(OPT_QUIET))
printf(" option: inlinemaxlen = %d\n", opt_inlining_maxlen);
}
else if (!strncmp(opt, "inlinedepth=", 12)) {
opt_inlining_depth = atoi(opt + 12);
if (!OPT_SETQ(OPT_QUIET))
printf(" option: inlinedepth = %d\n", opt_inlining_depth);
}
#endif // METHOD_INLINING
opt = strtok(NULL, ", ");
}
fflush(stdout);
}
}
#ifdef HAVE_POSIX_SCHED
// set scheduler
if (OPT_SETQ(OPT_SCHED_FIFO) || OPT_SETQ(OPT_SCHED_RR)) {
int policy;
struct sched_param schedp;
if (OPT_SETQ(OPT_SCHED_FIFO))
policy = SCHED_FIFO;
else if (OPT_SETQ(OPT_SCHED_FIFO))
policy = SCHED_RR;
else {
/* NOTREACHED */
policy = SCHED_OTHER;
}
memset((void *)&schedp, 0, sizeof(schedp));
schedp.sched_priority = sched_get_priority_max(policy);
if (sched_setscheduler(0 /* self */, policy, &schedp) != 0) {
perror("sched_setscheduler");
printf("FATAL: `sched_fifo' and `sched_rr' options cannot work.\n");
printf(" These options will need the root privilege.\n");
JVM_Exit(1);
}
}
#endif
// show credit
if (!OPT_SETQ(OPT_QUIET)) {
fprintf(stderr, CREDIT);
fflush(stderr);
}
// set rounding precision
{
unsigned short cw;
asm("fnstcw %0" : "=m"(cw));
cw &= ~0x0300;
if (OPT_SETQ(OPT_FPEXTENDED))
cw |= 0x0300;
else if (!OPT_SETQ(OPT_FPSINGLE))
cw |= 0x0200;
asm("fldcw %0" : : "m"(cw));
}
// prohibit generating lossy opcodes
UseLosslessQuickOpcodes = TRUE;
// resolve symbols
#define RESOLVE_A_INVOKER(NAME) \
if (!(sym_##NAME = (void *)symbolInSystemClassLoader(#NAME))) {\
printf("FATAL: cannot resolve a symbol: " #NAME "\n");\
JVM_Exit(1);\
}
RESOLVE_A_INVOKER(compileAndInvokeMethod);
RESOLVE_A_INVOKER(invokeJITCompiledMethod);
RESOLVE_A_INVOKER(invokeJavaMethod);
RESOLVE_A_INVOKER(invokeSynchronizedJavaMethod);
RESOLVE_A_INVOKER(invokeAbstractMethod);
RESOLVE_A_INVOKER(invokeNativeMethod);
RESOLVE_A_INVOKER(invokeSynchronizedNativeMethod);
RESOLVE_A_INVOKER(invokeJNINativeMethod);
RESOLVE_A_INVOKER(invokeJNISynchronizedNativeMethod);
RESOLVE_A_INVOKER(invokeLazyNativeMethod);
#if JDK_VER >= 12
{
char *custom_invoker_names[] = {
// in TERSE_SIG_* (see include-old/signature.h)
"\x1\x8\x8\xb\x8", "\x1\x1\xb\x1", "\xb\x1", "\x1\x8\x8\xb\xa",
"\xb\x2", "\x1\x8\x1\x8\x8\xb\xa", "\x1\xb\x1", "\x3\xb\x3",
"\xb\xa", "\x8\x8\x8\x8\xb\xa", "\x1\xb\xa", "\x8\xb\xa", "\xb\x8",
"\xb\x4", NULL
// in string (see src/share/javavm/include/invokers.txt)
// "OII_I", "OO_O", "V_O", "OII_V",
// "V_J", "OIOII_V", "O_O", "D_D",
// "V_V", "IIII_V", "O_V", "I_V", "V_I",
// "V_Z", NULL
};
uint32_t sym;
char *p;
int i;
sym_invokeJNI_min = (uint32_t)-1;
sym_invokeJNI_max = 0;
i = 0;
while (p = custom_invoker_names[i]) {
sym = (uint32_t)getCustomInvoker(p);
if (sym) {
if (sym > sym_invokeJNI_max) sym_invokeJNI_max = sym;
if (sym < sym_invokeJNI_min) sym_invokeJNI_min = sym;
}
i++;
}
}
#endif // JDK_VER
#ifdef COMPILE_DEBUG
# define SHOW_A_INVOKER(NAME) \
printf(" " #NAME ":\t0x%08x\n", (int)sym_##NAME)
printf("symbols:\n");
SHOW_A_INVOKER(compileAndInvokeMethod);
SHOW_A_INVOKER(invokeJITCompiledMethod);
SHOW_A_INVOKER(invokeJavaMethod);
SHOW_A_INVOKER(invokeSynchronizedJavaMethod);
SHOW_A_INVOKER(invokeAbstractMethod);
SHOW_A_INVOKER(invokeNativeMethod);
SHOW_A_INVOKER(invokeSynchronizedNativeMethod);
SHOW_A_INVOKER(invokeJNINativeMethod);
SHOW_A_INVOKER(invokeJNISynchronizedNativeMethod);
SHOW_A_INVOKER(invokeLazyNativeMethod);
#endif
// find classes
#define FIND_A_CLASS(NAME) \
if ((classJavaLang##NAME =\
FindClass(ee, JAVAPKG #NAME, TRUE)) == NULL) {\
printf("FATAL: cannot find the class: " #NAME "\n");\
JVM_Exit(1);\
}
FIND_A_CLASS(NoClassDefFoundError);
FIND_A_CLASS(NoSuchFieldError);
FIND_A_CLASS(NoSuchMethodError);
// initialize all classes already loaded
if (!OPT_SETQ(OPT_DONTCMPLVMCLZ)) {
ClassClass **clazzptr;
int i;
#if JDK_VER >= 12
BINCLASS_LOCK(sysThreadSelf());
#else
BINCLASS_LOCK();
#endif
#ifdef COMPILE_DEBUG
printf("%d classes is already loaded.\n", nbinclasses);
fflush(stdout);
#endif
// initialize
for (i = nbinclasses, clazzptr = binclasses; --i >= 0; clazzptr++) {
#ifdef COMPILE_DEBUG
printf("init: %x %s\n", *clazzptr, cbName(*clazzptr));
fflush(stdout);
#endif
initializeClassForJIT(*clazzptr, FALSE, TRUE);
}
if (OPT_SETQ(OPT_CMPLATLOAD)) {
// compile
ClassClass **classes =
(ClassClass **)sysMalloc(sizeof(ClassClass *) * nbinclasses);
memcpy(classes, binclasses, sizeof(ClassClass *) * nbinclasses);
for (i = nbinclasses, clazzptr = classes; --i >= 0; clazzptr++) {
#ifdef COMPILE_DEBUG
printf("compile: %s\n", cbName(*clazzptr)); fflush(stdout);
#endif
compileClass(*clazzptr);
}
sysFree(classes);
}
#if JDK_VER >= 12
else {
// compile java.lang.ref.*
ClassClass **classes =
(ClassClass **)sysMalloc(sizeof(ClassClass *) * nbinclasses);
memcpy(classes, binclasses, sizeof(ClassClass *) * nbinclasses);
for (i = nbinclasses, clazzptr = classes; --i >= 0; clazzptr++) {
if (!strncmp(cbName(*clazzptr), "java/lang/ref/", 14)) {
#ifdef COMPILE_DEBUG
printf("compile: %s\n", cbName(*clazzptr)); fflush(stdout);
#endif
compileClass(*clazzptr);
}
}
sysFree(classes);
}
#endif
#if JDK_VER >= 12
BINCLASS_UNLOCK(sysThreadSelf());
#else
BINCLASS_UNLOCK();
#endif
}
// initialize for signal handler
#if (defined(EXC_BY_SIGNAL) || defined(GET_SIGCONTEXT)) && defined(SEARCH_SIGCONTEXT)
# if JDK_VER < 12
*(char **)vector[3] = (char *)examineSigcontextNestCount;
# else
*((JITInterface6 *)jitinterface)->p_CompiledCodeSignalHandler =
examineSigcontextNestCount;
# endif
asm(".globl " SYMBOL(exam_nest) "\n\t"
"int $3\n\t"
SYMBOL(exam_nest) ":"
# if JDK_VER < 12
: : "m" (*(char **)vector[3])
# else
: : "m" (*((JITInterface6 *)jitinterface)->p_CompiledCodeSignalHandler)
# endif
// prevent elimination of the above assignment of the signal handler
// gcc 3.4.2 eliminate it if there is no reference to the pointer
);
# if JDK_VER < 12
*(char **)vector[3] = NULL;
# else
*((JITInterface6 *)jitinterface)->p_CompiledCodeSignalHandler = NULL;
# endif
if (sc_nest < 0) {
printf("FATAL: cannot examine the offset of signal context.\n");
JVM_Exit(1);
}
#endif // SIGNAL
// set up link vector
#if JDK_VER < 12
*(char **)vector[1] = (char *)initializeClassHook;
*(char **)vector[2] = (char *)sym_invokeJITCompiledMethod;
*(char **)vector[3] = (char *)signalHandler;
*(char **)vector[4] = (char *)freeClass;
*(char **)vector[5] = (char *)compileClass;
*(char **)vector[6] = (char *)compileClasses;
# ifndef IGNORE_DISABLE
if (!OPT_SETQ(OPT_IGNDISABLE)) {
*(char **)vector[7] = (char *)compilerEnable;
*(char **)vector[8] = (char *)compilerDisable;
}
# endif // IGNORE_DISABLE
*(char **)vector[10] = (char *)pcInCompiledCode;
*(char **)vector[11] = (char *)compiledCodePC;
# ifdef DIRECT_INVOCATION
*(char **)vector[70] = (char *)framePrev;
# endif // DIRECT_INVOCATION
# ifdef COMPILE_DEBUG
{
int i;
for (i = 1; i <= 8; i++)
printf("*vector[%d]: 0x%08x\n", i, (int)*(char **)vector[i]);
printf("*vector[10]: 0x%08x\n", i, (int)*(char **)vector[10]);
printf("*vector[11]: 0x%08x\n", i, (int)*(char **)vector[11]);
printf("*vector[70]: 0x%08x\n", i, (int)*(char **)vector[70]);
fflush(stdout);
}
# endif // COMPILE_DEBUG
#else // JDK_VER
{
JITInterface6 *ji6 = (JITInterface6 *)jitinterface;
*ji6->p_InitializeForCompiler = initializeClassHook;
*ji6->p_invokeCompiledMethod = sym_invokeJITCompiledMethod;
*ji6->p_CompiledCodeSignalHandler = signalHandler;
*ji6->p_CompilerFreeClass = freeClass;
*ji6->p_CompilerCompileClass = compileClass;
*ji6->p_CompilerCompileClasses = compileClasses;
#ifndef IGNORE_DISABLE
if (!OPT_SETQ(OPT_IGNDISABLE)) {
*ji6->p_CompilerEnable = compilerEnable;
*ji6->p_CompilerDisable = compilerDisable;
}
#endif // IGNORE_DISABLE
*ji6->p_PCinCompiledCode = pcInCompiledCode;
*ji6->p_CompiledCodePC = compiledCodePC;
#ifdef DIRECT_INVOCATION
*ji6->p_CompiledFramePrev = framePrev;
#endif // DIRECT_INVOCATION
*ji6->p_CompiledFrameID = frameID; // only for JDK >= 1.2
}
#endif // JDK_VER
#ifdef CODE_DB
// open DB and page file
if (OPT_SETQ(OPT_CODEDB)) {
void *dl_dbm;
if (!(dl_dbm = dlopen(LIBDBM, RTLD_LAZY))) {
fputs(dlerror(), stderr); fputc('\n', stderr);
fprintf(stderr, "failed to open " LIBDBM ".\n");
goto codedb_init_fail;
}
#ifdef GDBM
sym_dbm_open = (GDBM_FILE (*)(char *,int,int,int,void (*)()))
dlsym(dl_dbm, "gdbm_open");
sym_dbm_close = (void (*)(GDBM_FILE))dlsym(dl_dbm, "gdbm_close");
sym_dbm_store = (int (*)(GDBM_FILE,datum,datum,int))
dlsym(dl_dbm, "gdbm_store");
sym_dbm_fetch = (datum (*)(GDBM_FILE,datum))dlsym(dl_dbm, "gdbm_fetch");
sym_dbm_sync = (void (*)(GDBM_FILE))dlsym(dl_dbm, "gdbm_sync");
#else
sym_dbm_open = (DBM *(*)(const char *,int,int))dlsym(dl_dbm, "dbm_open");
sym_dbm_close = (void (*)(DBM *))dlsym(dl_dbm, "dbm_close");
sym_dbm_store = (int (*)(DBM *,datum,datum,int))dlsym(dl_dbm, "dbm_store");
sym_dbm_fetch = (datum (*)(DBM *,datum))dlsym(dl_dbm, "dbm_fetch");
#endif
if (!(((int32_t)sym_dbm_open) && ((int32_t)sym_dbm_close) &&
((int32_t)sym_dbm_store) && ((int32_t)sym_dbm_fetch))
#ifdef GDBM
&& ((int32_t)sym_dbm_sync)
#endif
) {
fprintf(stderr, "cannot get symbols to handle DBM.\n");
goto codedb_init_fail;
}
if ((db_page = open(CODEDB_PAGE,
O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
perror("open"); goto codedb_init_fail;
}
#ifdef GDBM
if (!(db = sym_dbm_open(CODEDB_DB, 512,
GDBM_WRCREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, NULL))) {
perror("gdbm_open"); goto codedb_init_fail;
}
#else
if (!(db = sym_dbm_open(CODEDB_DB, O_RDWR|O_CREAT, 0))) {
perror("dbm_open"); goto codedb_init_fail;
}
#endif
goto codedb_init_done;
codedb_init_fail:
fprintf(stderr, "disable codedb.\n"); OPT_RESET(OPT_CODEDB);
if (db_page >= 0) close(db_page);
JVM_Exit(1);
}
codedb_init_done:
#endif // CODE_DB
#if defined(EXC_BY_SIGNAL) && defined(COUNT_EXC_SIGNAL)
// for printing the number of exceptions by signal
atexit(showExcSignalCount);
#endif
// for strictfp
// check the FPU roundig precision
{
uint16_t cw;
asm("fstcw %0" : "=m" (cw));
is_fpupc_double = ((cw & 0x0300) == 0x0200);
#if defined(RUNTIME_DEBUG) || defined(COMPILE_DEBUG)
printf("FPU rounding precision: %sdouble.\n",
(is_fpupc_double?"":"not "));
#endif
}
#ifdef METAVM
// set remote flag on
REMOTE_ADDR(ee) = NULL;
REMOTE_FLAG_ON(ee);
#endif // METAVM
#ifdef COMPILE_DEBUG
printf("java_lang_Compiler_start() done.\n");
fflush(stdout);
#endif
}
/*
* Initialize the class at the time one is loaded.
*/
void initializeClassForJIT(ClassClass *cb,
bool_t linkNative, bool_t initInvoker) {
struct methodblock *mb;
int mb_count;
ExecEnv *ee = EE();
#ifdef METAVM
// force a loaded class to implement java.io.Serializable
{
JNIEnv *env = EE2JNIEnv(ee);
static ClassClass *clz_Serializable = NULL;
if (!clz_Serializable) {
jclass jclz_Ser = (*env)->FindClass(env, "java/io/Serializable");
(*env)->NewGlobalRef(env, jclz_Ser);
clz_Serializable = (ClassClass *)DeRef(env, jclz_Ser);
}
if (!cbIsInterface(cb) && (cb != classJavaLangObject))
forceToImplement(ee, cb, clz_Serializable);
}
#endif // METAVM
mb = cbMethods(cb);
mb_count = cbMethodsCount(cb);
for (; mb_count-- > 0; mb++) {
int access = mb->fb.access;
if (access & ACC_ABSTRACT) continue;
// initialize mb->CompiledCodeInfo
// if (mb->CompiledCodeInfo == NULL)
if (prepareCompiledCodeInfo(ee, mb) == NULL) {
printf("FATAL: could not create CompiledCodeInfo for %s#%s %s.\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
JVM_Exit(1);
}
if (access & ACC_NATIVE) {
#if defined(METAVM) && (JDK_VER >= 12)
if (linkNative) {
// link native code
void *code;
LINKCLASS_LOCK(EE2SysThread(ee));
code = mb->code;
LINKCLASS_UNLOCK(EE2SysThread(ee));
if (code == NULL) {
uint32_t isJNI;
char orig = GET_REMOTE_FLAG(ee);
REMOTE_FLAG_OFF(ee);
code = dynoLink(mb, &isJNI); // link, which executes Java code
SET_REMOTE_FLAG(ee, orig);
if (code != NULL) {
LINKCLASS_LOCK(EE2SysThread(ee));
if (mb->code == NULL) {
mb->code = code;
if (isJNI) { // JNI style
if (mb->fb.access & ACC_SYNCHRONIZED)
mb->invoker = invokeJNISynchronizedNativeMethod;
else {
Invoker inv = getCustomInvoker(methodTerseSig(mb));
if (inv) mb->invoker = inv;
else mb->invoker = invokeJNINativeMethod;
}
}
else { // old style
mb->invoker = (mb->fb.access & ACC_SYNCHRONIZED) ?
invokeSynchronizedNativeMethod : invokeNativeMethod;
}
}
LINKCLASS_UNLOCK(EE2SysThread(ee));
}
}
# ifdef COMPILE_DEBUG
if (mb->code == NULL) {
// fail in linking
// because the native library has not been loaded yet.
printf("could not link now: %s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
fflush(stdout);
}
# endif
} // if (linkNative)
#endif // METAVM && JDK_VER >= 12
continue;
}
if (!initInvoker) continue;
// initialize invoker
if ((OPT_SETQ(OPT_CMPLCLINIT) || strcmp(mb->fb.name, "<clinit>")) &&
(mb->CompiledCode == NULL)) { // not compiled yet
// assume that CompiledCode of not-yet-compiled method is NULL
mb->invoker = sym_compileAndInvokeMethod;
}
}
}
//
// Local Functions
//
static void initializeClassHook(ClassClass *cb) {
#ifdef COMPILE_DEBUG
printf("initializeClassHook(%s) called.\n", cbName(cb));
fflush(stdout);
#endif
initializeClassForJIT(cb, TRUE, TRUE);
if (OPT_SETQ(OPT_CMPLATLOAD)) { // have to done after initialization
compileClass(cb);
}
#ifdef COMPILE_DEBUG
printf("initializeClassHook(%s) done.\n", cbName(cb));
fflush(stdout);
#endif
}
/*
* Freeing class stuffs related to the compiler.
*/
static void freeClass(ClassClass *cb) {
struct methodblock *mb = cbMethods(cb);
struct methodblock *mb_end = mb + cbMethodsCount(cb);
#ifdef COMPILE_DEBUG
printf("freeClass() called.\n");
fflush(stdout);
#endif
for (; mb < mb_end; mb++)
freeMethod(mb);
}
static unsigned char *compiledCodePC(JavaFrame *frame,struct methodblock *mb) {
#ifdef DIRECT_INVOCATION
// in case of DIRECT_INVOCATION, frame->lastpc is not up-to-date.
return mb->code;
#else
return (frame ? frame->lastpc : mb->code);
#endif // DIRECT_INVOCATION
}
static bool_t pcInCompiledCode(
unsigned char *pc /* mb->lastpc */, struct methodblock *mb) {
#if 1 // mb->lastpc is a pc on bytecode
unsigned int off = pc - mb->code;
return (off < mb->code_length);
#else // mb->lastpc is a pc on compiled code
CodeInfo *info = (CodeInfo *)mb->CompiledCodeInfo;
if (info) {
unsigned char *compiled_code = (unsigned char *)mb->CompiledCode;
unsigned char *limit = compiled_code + info->code_size;
if (compiled_code != limit) {
if ((compiled_code <= pc) && (pc < limit)) return TRUE;
}
}
return FALSE;
#endif
}
#ifdef DIRECT_INVOCATION
#undef FRAMEPREV_DEBUG
static JavaFrame *framePrev(JavaFrame *frame, JavaFrame *frame_buf) {
struct methodblock *mb = frame->current_method;
uint32_t *basep;
unsigned char *ret_addr;
extern void invokeJIT_compiled_done(), candi_compiled_done();
#ifdef FRAMEPREV_DEBUG
{
printf(" | framePrev() called.\n");
printf(" | %s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
fflush(stdout);
}
#endif
{
unsigned char *lastpc = frame->lastpc;
sysAssert(mb->code != NULL);
if ((mb->code <= lastpc) && (lastpc < mb->code + mb->code_length)) {
// this frame is for interpreter
#ifdef FRAMEPREV_DEBUG
printf(" | this frame created by interpreter.\n");
fflush(stdout);
#endif
return frame->prev;
}
}
basep = (uint32_t *)(frame->vars);
#ifdef FRAMEPREV_DEBUG
printf(" | basep: %x\n", basep);
fflush(stdout);
#endif
if (frame != frame_buf) { // first time
memset((void *)frame_buf, 0, sizeof(JavaFrame));
frame_buf->prev = frame->prev;
}
ret_addr = (unsigned char *)basep[1];
if ((ret_addr == (unsigned char *)invokeJIT_compiled_done) ||
(ret_addr == (unsigned char *)candi_compiled_done)) {
#ifdef FRAMEPREV_DEBUG
printf(" | limit.\n\n");
fflush(stdout);
#endif
return frame->prev;
}
basep = (uint32_t *)basep[0];
frame_buf->vars = (stack_item *)basep;
frame_buf->current_method = (struct methodblock *)basep[3];
#ifdef FRAMEPREV_DEBUG
{
printf(" | method: %x\n", mb);
fflush(stdout);
printf(" | buf->cur_method: %s#%s %s\n\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
fflush(stdout);
}
#endif
return frame_buf;
}
#endif // DIRECT_INVOCATION
#if JDK_VER >= 12
static void *frameID(JavaFrame *frame) {
return (void *)frame->vars;
}
#endif
#ifndef IGNORE_DISABLE
/*
* Enable and Disable the compiler.
*/
static void compilerEnable() {
#ifdef RUNTIME_DEBUG
printf("compilerEnable() called.\n"); fflush(stdout);
#endif
compiler_enabled = TRUE;
}
/*
* Enable and Disable the compiler.
*/
static void compilerDisable() {
#ifdef RUNTIME_DEBUG
printf("compilerDisable() called.\n"); fflush(stdout);
#endif
compiler_enabled = FALSE;
}
#endif // IGNORE_DISABLE