-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompile.c
4231 lines (3736 loc) · 108 KB
/
compile.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,2005 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 <string.h> // for memcpy(), strlen()
#include <stdlib.h> // for malloc()
#if defined(__FreeBSD__) || defined(__NetBSD__)
# include <sys/types.h> // for a type u_long
#endif
#include <limits.h> // for INT_MAX
#ifdef _WIN32
# include <winsock2.h>
#else
# include <netinet/in.h> // for ntohl()
#endif
#include "compiler.h"
#include "constants.h"
#include "code.h" // for macro STSTA
#ifdef METHOD_INLINING
# include "stack.h"
#endif
#ifdef COUNT_TSC
# include "x86tsc.h"
#endif
#include <string.h>
#ifdef _WIN32
# include <limits.h>
// undefine the ntohl macro defined by JDK
# ifdef ntohl
# undef ntohl
# endif
#endif
//
// Global Variables
//
unsigned char *compiledcode_min, *compiledcode_max;
//
// Local Functions
//
static int makePCTable(CompilerContext *cc);
static int processAnOpcode(CompilerContext *cc, int opcode, int byteoff);
static void makeBlockStructure(CompilerContext *cc);
static void updateStates(CompilerContext *cc);
static int writeCode(CompilerContext *cc);
static void resolveJumpInstructions(CompilerContext *cc);
static void resolveExcRetSwitch(CompilerContext *cc);
static int resolveDynamicConstants(CompilerContext *cc);
static void makeExcTable(CompilerContext *cc);
static void resolveFunctionSymbols(CompilerContext *cc);
/*
* Compile a specified method.
*
* returns: true if compile failed.
*/
int compileMethod(struct methodblock *mb, CompilationStage target_stage) {
CompilerContext *cc;
CodeInfo *info = (CodeInfo *)mb->CompiledCodeInfo;
#ifdef COUNT_TSC
int i;
#endif
#ifdef COMPILE_DEBUG
int compile_debug;
#endif
// for exclusion
#if JDK_VER >= 12
sys_thread_t *self;
#endif
sys_mon_t *mon;
if ((mb->fb.access & (ACC_ABSTRACT | ACC_NATIVE)) ||
(mb->invoker == sym_invokeJITCompiledMethod)) {
// needless to compile
#ifdef COMPILE_DEBUG
if (compile_debug) {
printf(" needless to compile: %s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
fflush(stdout);
}
#endif
return 0;
}
if (mb->code == NULL) { /* mb->code is NULL */
#ifdef COMPILE_DEBUG
printf(" mb->code is NULL: %s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
fflush(stdout);
#endif
return 1; // failure
}
if (info == NULL) {
#ifdef COMPILE_DEBUG
if (compile_debug) {
printf(" (cmplMtd(): method->CompiledCodeInfo is NULL)\n");
fflush(stdout);
}
#endif
if ((info = prepareCompiledCodeInfo(EE(), mb)) == NULL)
return 1; // failure
}
cc = getCompilerContext(mb);
if (cc->stage >= target_stage) return 0;
#if JDK_VER >= 12
self = EE2SysThread(cc->ee);
#endif
#ifdef COMPILE_DEBUG
compile_debug = cc->compile_debug;
if (compile_debug) {
int acc = mb->fb.access;
printf("\n");
printf("cmplMtd() called.\n %s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
printf(" cur stage: %d, target stage: %d\n", cc->stage, target_stage);
showCompilerContext(cc, " ");
printf(" access: 0x%x", acc);
if (acc & ACC_NATIVE) printf(" native");
// if (acc & ACC_MACHINE_COMPILED) printf(" machine_compiled");
if (acc & ACC_PUBLIC) printf(" public");
if (acc & ACC_PRIVATE) printf(" private");
if (acc & ACC_PROTECTED) printf(" protected");
if (acc & ACC_STATIC) printf(" static");
if (acc & ACC_FINAL) printf(" final");
if (acc & ACC_SYNCHRONIZED) printf(" synchronized");
if (acc & ACC_ABSTRACT) printf(" abstract");
if (acc & ACC_STRICT) printf(" strictfp");
printf("\n");
printf(" nlocals: %d\n", mb->nlocals);
printf(" maxstack: %d\n", mb->maxstack);
# ifdef METAVM
printf(" remote flag: 0x%x (%x)\n", GET_REMOTE_FLAG(cc->ee) & 0xff, EE());
printf(" remote addr: 0x%x\n", REMOTE_ADDR(cc->ee));
printf(" excKind: %d\n", cc->ee->exceptionKind);
# endif
fflush(stdout);
}
#endif // COMPILE_DEBUG
mon = info->monitor;
#ifdef COMPILE_DEBUG
if (compile_debug) {
printf(" monitor: 0x%x\n", mon);
#if JDK_VER >= 12
printf(" thread: 0x%x\n", self);
#endif
fflush(stdout);
}
#endif
SYS_MONITOR_ENTER(self, mon); // lock
if (cc->stage == STAGE_DONE)
goto stage_done;
mb->invoker = access2invoker(mb->fb.access);
// jump into an intermediate stage
if (cc->stage == STAGE_INTERNAL_CODE)
goto stage_internal_code;
else if (cc->stage == STAGE_STATIC_PART)
goto stage_static_part;
#ifdef CODE_DB
else if (OPT_SETQ(OPT_CODEDB)) {
if (readCompiledCode(db, db_page, cc)) {
cc->stage = STAGE_STATIC_PART;
goto stage_static_part;
}
} // if (OPT_CODEDB)
#endif
// compile
#ifdef COUNT_TSC
# define CALL_RDTSC(N) cc->tsc[N] = rdtsc()
#else
# define CALL_RDTSC(N)
#endif
CALL_RDTSC(0);
CALL_RDTSC(0);
if (makePCTable(cc)) goto compile_failed;
CALL_RDTSC(1);
makeBlockStructure(cc);
CALL_RDTSC(2);
#ifdef OPTIMIZE_INTERNAL_CODE
peepholeOptimization(cc);
#endif
#ifdef METHOD_INLINING
if (!(mb->fb.access & ACC_SYNCHRONIZED) && // not synchronized
(mb->exception_table_length == 0) && // don't have exc. table
(cc->may_jump == FALSE) && // don't contain any jump
(pctableLen(cc) <= opt_inlining_maxlen)) {
size_t copysize = sizeof(pcentry) * pctableLen(cc);
info->inlineability = INLINE_MAY;
// preserve generated internal instructions for inlining
info->pctable = sysMalloc(copysize);
memcpy(info->pctable, cc->pctable, copysize);
info->pctablelen = cc->pctablelen;
}
else
info->inlineability = INLINE_DONT;
#endif
cc->stage = STAGE_INTERNAL_CODE;
stage_internal_code:
if (cc->stage >= target_stage) {
mb->invoker = sym_compileAndInvokeMethod;
goto stage_done;
}
CALL_RDTSC(3);
methodInlining(cc);
CALL_RDTSC(4);
#ifdef EAGER_COMPILATION
eagerCompilation(cc);
#endif
CALL_RDTSC(5);
updateStates(cc);
CALL_RDTSC(6);
if (writeCode(cc)) goto compile_failed;
CALL_RDTSC(7);
resolveJumpInstructions(cc);
CALL_RDTSC(8);
resolveExcRetSwitch(cc);
CALL_RDTSC(9);
// set generated code to methodblock
{
int code_size = cc->bufp - cc->buffer;
#ifdef COMPILE_DEBUG
if (compile_debug) {
printf("cmplMtd(): generated code size = 0x%x(%d)\n",
code_size, code_size);
}
#endif
info->code_size = code_size;
{
unsigned char *code = (void *)sysMalloc(code_size);
memcpy(code, cc->buffer, code_size);
mb->CompiledCode = code;
if (code < compiledcode_min)
compiledcode_min = code;
if (compiledcode_max < (code + code_size))
compiledcode_max = code + code_size;
}
#ifdef COMPILE_DEBUG
if (compile_debug) {
printf(" mb->CompiledCode: 0x%08x\n", (int)mb->CompiledCode);
fflush(stdout);
}
#endif
}
#ifdef CODE_DB
if (OPT_SETQ(OPT_CODEDB))
writeCompiledCode(db, db_page, cc);
#endif // CODE_DB
cc->stage = STAGE_STATIC_PART;
stage_static_part:
if (cc->stage >= target_stage) {
mb->invoker = sym_compileAndInvokeMethod;
goto stage_done;
}
CALL_RDTSC(10);
resolveDynamicConstants(cc);
CALL_RDTSC(11);
makeExcTable(cc);
CALL_RDTSC(12);
resolveFunctionSymbols(cc);
CALL_RDTSC(13);
#ifdef DIRECT_INVOCATION
mb->fb.access |= ACC_MACHINE_COMPILED;
#endif
mb->invoker =
(bool_t (*)(JHandle *, struct methodblock *, int, struct execenv *))
sym_invokeJITCompiledMethod;
// invokeJITCompiledMethod() is in invoker.c
#ifdef COUNT_TSC
printf("%s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
{
unsigned long long int diff, all;
all = cc->tsc[N_TSC] - cc->tsc[0];
printf(" all : %7llu\n", all);
for (i = 1; i <= N_TSC; i++) {
diff = cc->tsc[i] - cc->tsc[i - 1];
printf(" tsc[%2d]: %7llu %4.1f \%\n", i, diff,
(double)diff * 100.0 / all);
}
}
#endif
// write code size
if (OPT_SETQ(OPT_CODESIZE)) {
static FILE *sizefp = NULL;
if (!sizefp) {
if (!(sizefp = fopen(CODESIZE_FNAME, "w"))) {
perror("fopen");
OPT_RESET(OPT_CODESIZE);
goto codesize_open_failed;
}
fprintf(sizefp, "# num_bytecode"
" size_bytecode"
" size_native_code"
#ifdef EXC_BY_SIGNAL
" num_throw_entry"
" size_throw_entry"
#endif
" class_name#method_name signature\n");
}
fprintf(sizefp, "%d", cc->ninsn);
fprintf(sizefp, "\t%lu", mb->code_length);
fprintf(sizefp, "\t%d", info->code_size);
#ifdef EXC_BY_SIGNAL
fprintf(sizefp, "\t%d", info->throwtablelen);
fprintf(sizefp, "\t%d", info->throwtablelen * sizeof(throwentry));
#endif
fprintf(sizefp, "\t%s#%s %s",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
// class name, method name, signature
fprintf(sizefp, "\n");
fflush(sizefp);
}
codesize_open_failed:
// write generated code to the file code_<classname><methodname>.c
if (OPT_SETQ(OPT_OUTCODE)) {
FILE *codefp;
char funcname[256];
char fname[256];
char *p;
unsigned char *u;
int i;
snprintf(funcname, 256, "%s_%s%s",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
for (p = funcname; *p != '\0'; p++) {
switch (*p) {
case '(':
case ')':
*p = '_'; break;
case '/':
case ';': case '<': case '>':
*p = '_'; break;
case '[': *p = '_'; break;
}
}
snprintf(fname, 256, "code-%s.S", funcname);
if (!(codefp = fopen(fname, "w"))) {
perror("fopen"); goto code_open_failed;
}
#if defined(_WIN32) || ((defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(__ELF__))
fprintf(codefp, ".globl _%s\n", funcname);
#else
fprintf(codefp, ".globl %s\n", funcname);
#endif
#ifdef linux
fprintf(codefp, ".align 16,0x90\n");
#endif
fprintf(codefp, "%s:", funcname);
fprintf(codefp, ".byte ");
u = (unsigned char *)mb->CompiledCode;
fprintf(codefp, "%d", u[0]);
for (i = 1; i < info->code_size; i++) fprintf(codefp, ",%d", u[i]);
fprintf(codefp, "\n");
fclose(codefp);
}
code_open_failed:
cc->stage = STAGE_DONE;
releaseCompilerContext(cc);
stage_done:
SYS_MONITOR_EXIT(self, mon); // unlock
#ifdef COMPILE_DEBUG
if (compile_debug) {
printf("cmplMtd() done.\n %s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
printf(" code: 0x%08x, size: 0x%x(%d)\n",
(int)mb->CompiledCode, info->code_size, info->code_size);
fflush(stdout);
}
#endif
return 0;
compile_failed:
SYS_MONITOR_EXIT(self, mon); // unlock
#ifdef COMPILE_DEBUG
printf("compileMethod() failed.\n %s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
fflush(stdout);
#endif
#ifdef DIRECT_INVOCATION
mb->fb.access &= ~ACC_MACHINE_COMPILED;
#endif
// mb->invoker = access2invoker(mb->fb.access); /* needless */
releaseCompilerContext(cc);
return 1;
}
/*
* Freeing method related stuffs.
*/
void freeMethod(struct methodblock *mb) {
void *code;
#ifdef COMPILE_DEBUG
printf("freeMethod(): ");
if (mb)
printf("%s#%s %s\n",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
else
printf("(null)\n");
fflush(stdout);
#endif
freeCompiledCodeInfo(mb->CompiledCodeInfo);
mb->CompiledCodeInfo = NULL;
code = mb->CompiledCode;
if (code) sysFree(code);
mb->CompiledCode = NULL;
}
/*
* 1st pass of compilation.
* generate native code from byte code.
*
* returns: true if an exception occurred.
*/
static int makePCTable(CompilerContext *cc) {
struct methodblock *mb = cc->mb;
unsigned char *methodcode = mb->code;
int endoff = mb->code_length;
int32_t byteoff = 0;
int byteinc;
int operand;
cp_item_type *constant_pool = cbConstantPool(fieldclass(&mb->fb));
unsigned char *type_table =
constant_pool[CONSTANT_POOL_TYPE_TABLE_INDEX].type;
CodeInfo *info = (CodeInfo *)(mb->CompiledCodeInfo);
#if 0
int32_t invocation_count = info->invocation_count;
#endif
int remaked = 0; // makePCTable() is retried once at least
int opcode;
#ifdef COMPILE_DEBUG
int compile_debug = cc->compile_debug;
#endif
makepctable_start:
processAnOpcode(cc, opc_methodhead, -1);
#ifdef METAVM
if (!strcmp(cbName(fieldclass(&mb->fb)), JAVAPKG "Thread") &&
!strcmp(mb->fb.name, "run")) {
#ifdef COMPILE_DEBUG
printf("generate `metavm_init' for Thread#run.\n"); fflush(stdout);
#endif
processAnOpcode(cc, opc_metavm_init, -1);
}
#endif // METAVM
#ifndef USE_SSE2
if (((mb->fb.access & ACC_STRICT) && !OPT_SETQ(OPT_IGNSTRICTFP)) ||
OPT_SETQ(OPT_FRCSTRICTFP)) {
if (!is_fpupc_double) {
processAnOpcode(cc, opc_fppc_save, -1);
processAnOpcode(cc, opc_fppc_double, -1);
}
processAnOpcode(cc, opc_strict_enter, -1);
}
#endif // !USE_SSE2
if ((mb->fb.access & ACC_SYNCHRONIZED) && !OPT_SETQ(OPT_IGNLOCK)) {
if (mb->fb.access & ACC_STATIC)
processAnOpcode(cc, opc_sync_static_enter, -1);
else
processAnOpcode(cc, opc_sync_obj_enter, -1);
}
processAnOpcode(cc, opc_start, -1);
while (byteoff < endoff) {
cc->ninsn++;
opcode = *(methodcode + byteoff);
// decompose an opcode to some micro opcodes
switch (opcode) {
case opc_iaload: case opc_laload: case opc_faload: case opc_daload:
case opc_aaload: case opc_baload: case opc_caload: case opc_saload:
processAnOpcode(cc, opc_fill_cache, byteoff);
processAnOpcode(cc, opc_array_check, byteoff);
byteoff += processAnOpcode(cc, opcode, byteoff);
break;
case opc_lstore: case opc_dstore:
case opc_lstore_0: case opc_dstore_0:
case opc_lstore_1: case opc_dstore_1:
case opc_lstore_2: case opc_dstore_2:
case opc_lstore_3: case opc_dstore_3:
processAnOpcode(cc, opc_fill_cache, byteoff);
byteoff += processAnOpcode(cc, opcode, byteoff);
break;
case opc_iastore: case opc_fastore:
case opc_aastore: case opc_bastore: case opc_castore: case opc_sastore:
processAnOpcode(cc, opc_iastore1, byteoff);
processAnOpcode(cc, opc_fill_cache, byteoff);
processAnOpcode(cc, opc_array_check, byteoff);
byteoff += processAnOpcode(cc, opcode, byteoff);
break;
case opc_lastore: case opc_dastore:
processAnOpcode(cc, opc_fill_cache, byteoff);
byteoff += processAnOpcode(cc, opcode, byteoff);
break;
case opc_fadd: case opc_fsub:
{
int opcode_fld = ((opcode == opc_fadd) ? opc_fld : opc_fld4);
processAnOpcode(cc, opc_flush_cache, byteoff);
processAnOpcode(cc, opcode_fld, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_fst, byteoff);
byteoff += byteinc;
}
break;
case opc_fmul: case opc_fdiv:
{
int opcode_fld = ((opcode == opc_fmul) ? opc_fld : opc_fld4);
#if !defined(USE_SSE2) || !defined(OMIT_SCALING_SINGLE_PRECISION)
if (OPT_SETQ(OPT_FRCSTRICTFP) ||
(!OPT_SETQ(OPT_IGNSTRICTFP) && (mb->fb.access & ACC_STRICT))) {
processAnOpcode(cc, opc_flush_cache, byteoff);
processAnOpcode(cc, opc_strict_fprep, byteoff);
processAnOpcode(cc, opcode_fld, byteoff);
processAnOpcode(cc, opc_strict_fscdown, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_strict_fscup, byteoff);
processAnOpcode(cc, opc_fst, byteoff);
processAnOpcode(cc, opc_strict_fsettle, byteoff);
}
else
#endif // !USE_SSE2
{
processAnOpcode(cc, opc_flush_cache, byteoff);
processAnOpcode(cc, opcode_fld, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_fst, byteoff);
}
byteoff += byteinc;
}
break;
case opc_dadd: case opc_dsub:
{
int opcode_dld = ((opcode == opc_dadd) ? opc_dld : opc_dld8);
processAnOpcode(cc, opc_flush_cache, byteoff);
processAnOpcode(cc, opcode_dld, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_dst, byteoff);
byteoff += byteinc;
}
break;
case opc_dmul: case opc_ddiv:
{
int opcode_dld = ((opcode == opc_dmul) ? opc_dld : opc_dld8);
#ifndef USE_SSE2
if (OPT_SETQ(OPT_FRCSTRICTFP) ||
(!OPT_SETQ(OPT_IGNSTRICTFP) && (mb->fb.access & ACC_STRICT))) {
processAnOpcode(cc, opc_flush_cache, byteoff);
processAnOpcode(cc, opc_strict_dprep, byteoff);
processAnOpcode(cc, opcode_dld, byteoff);
processAnOpcode(cc, opc_strict_dscdown, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_strict_dscup, byteoff);
processAnOpcode(cc, opc_dst, byteoff);
processAnOpcode(cc, opc_strict_dsettle, byteoff);
}
else
#endif // !USE_SSE2
{
processAnOpcode(cc, opc_flush_cache, byteoff);
processAnOpcode(cc, opcode_dld, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_dst, byteoff);
}
byteoff += byteinc;
}
break;
case opc_i2f:
#ifndef USE_SSE2
case opc_frem:
case opc_l2f:
#endif
processAnOpcode(cc, opc_flush_cache, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_fst, byteoff);
byteoff += byteinc;
break;
case opc_drem:
processAnOpcode(cc, opc_fill_cache, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
#ifndef USE_SSE2
processAnOpcode(cc, opc_dst, byteoff);
#endif
byteoff += byteinc;
break;
case opc_i2d:
#ifndef USE_SSE2
case opc_l2d:
#endif
processAnOpcode(cc, opc_flush_cache, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_dst, byteoff);
byteoff += byteinc;
break;
#ifdef USE_SSE2
case opc_frem:
case opc_l2f:
case opc_l2d:
processAnOpcode(cc, opc_flush_cache, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
byteoff += byteinc;
break;
#endif
case opc_f2d:
processAnOpcode(cc, opc_flush_cache, byteoff);
processAnOpcode(cc, opc_fld, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_dst, byteoff);
byteoff += byteinc;
break;
case opc_d2f:
processAnOpcode(cc, opc_flush_cache, byteoff);
processAnOpcode(cc, opc_dld, byteoff);
byteinc = processAnOpcode(cc, opcode, byteoff);
processAnOpcode(cc, opc_fst, byteoff);
byteoff += byteinc;
break;
case opc_ireturn: case opc_freturn: case opc_areturn:
processAnOpcode(cc, opc_stateto1, byteoff);
processAnOpcode(cc, opc_return, byteoff);
byteoff++;
break;
case opc_lreturn: case opc_dreturn:
processAnOpcode(cc, opc_stateto4, byteoff);
processAnOpcode(cc, opc_return, byteoff);
byteoff++;
break;
case opc_invokevirtual: case opc_invokevirtual_quick_w:
case opc_invokespecial:
case opc_invokenonvirtual_quick: case opc_invokesuper_quick:
case opc_invokestatic: case opc_invokestatic_quick:
case opc_invokeinterface: case opc_invokeinterface_quick:
switch (opcode) {
case opc_invokevirtual: case opc_invokevirtual_quick_w:
opcode = opc_invokevirtual;
break;
case opc_invokespecial:
case opc_invokenonvirtual_quick: case opc_invokesuper_quick:
opcode = opc_invokespecial;
break;
case opc_invokestatic: case opc_invokestatic_quick:
opcode = opc_invokestatic;
break;
case opc_invokeinterface: case opc_invokeinterface_quick:
opcode = opc_invokeinterface;
}
operand = GET_UINT16(methodcode + byteoff + 1);
byteinc = opcode_length[opcode];
// throwing NoClassDefFoundError if the callee method cannot be resolved
if (!CONSTANT_POOL_TYPE_TABLE_IS_RESOLVED(type_table, operand)) {
if (!ResolveClassConstantFromClass2(fieldclass(&mb->fb), operand,
cc->ee, (1 << CONSTANT_Methodref) |
(1 << CONSTANT_InterfaceMethodref), FALSE)) {
JHandle *exc;
#ifdef COMPILE_DEBUG
printf("resolution failed (cp index: %d).\n", operand);
fflush(stdout);
#endif
sysAssert(exceptionOccurred(cc->ee));
exc = cc->ee->exception.exc;
sysAssert(exc != NULL);
if (obj_classblock(exc) == classJavaLangNoClassDefFoundError) {
exceptionClear(cc->ee);
processAnOpcode(cc, opc_throw_noclassdef, byteoff);
goto makepc_invoke_done;
}
else if (obj_classblock(exc) == classJavaLangNoSuchMethodError) {
exceptionClear(cc->ee);
processAnOpcode(cc, opc_throw_nomethod, byteoff);
goto makepc_invoke_done;
}
return 1;
}
}
{
struct methodblock *src_mb;
struct fieldblock *method_fb;
ClassClass *src_clazz, *method_clazz;
// check IllegalAccessError
if ((src_mb = cc->mb) == NULL) {
#ifdef COMPILE_DEBUG
printf("Illegal method invocation: methodblock is null.\n");
fflush(stdout);
#endif
processAnOpcode(cc, opc_throw_illegalaccess, byteoff);
goto makepc_invoke_done;
}
method_fb = &constant_pool[operand].mb->fb;
src_clazz = fieldclass(&src_mb->fb);
method_clazz = fieldclass(method_fb);
#ifndef SLACK_ACCESS_CONTROL
// check private and protected from other packages
if (!src_mb ||
(!VerifyFieldAccess(
src_clazz, method_clazz,
method_fb->access, FALSE))) {
char *src_clazz_name, *method_clazz_name;
char *src_p, *method_p;
int len = 0;
src_clazz_name = cbName(src_clazz);
method_clazz_name = cbName(method_clazz);
#ifdef COMPILE_DEBUG
printf("Illegal method invocation:\n"); fflush(stdout);
printf("\tfrom: %s#%s\n",
src_clazz_name, src_mb->fb.name);
printf("\tto : %s#%s\n",
method_clazz_name, method_fb->name);
#endif
// but, allow access to/from inner class
src_p = strchr(src_clazz_name, '$');
method_p = strchr(method_clazz_name, '$');
if (src_p) {
if (!method_p) len = src_p - src_clazz_name;
}
else {
if (method_p) len = method_p - method_clazz_name;
}
if (len &&
#if JDK_VER >= 12
(cbProtectionDomain(src_clazz) == cbProtectionDomain(method_clazz)) &&
(cbLoader(src_clazz) == cbLoader(method_clazz))
#else
TRUE
#endif // JDK_VER >= 12
) {
if (!strncmp(src_clazz_name, method_clazz_name, len)) {
#ifdef COMPILE_DEBUG
printf(" VerifyFieldAccess returned FALSE, "
"but the access is to/from inner class.\n");
fflush(stdout);
#endif
goto check_illacc_done;
}
}
#ifdef COMPILE_DEBUG
printf(" A private or protected method invoked.\n");
fflush(stdout);
#endif
processAnOpcode(cc, opc_throw_illegalaccess, byteoff);
goto makepc_invoke_done;
}
#endif // SLACK_ACCESS_CONTROL
}
check_illacc_done:
switch (opcode) {
case opc_invokevirtual:
{
struct methodblock *method = constant_pool[operand].mb;
ClassClass *clz = fieldclass(&method->fb);
if ((method->fb.access & ACC_PRIVATE) ||
((method->fb.access | cbAccess(clz)) & ACC_FINAL)) {
// the method is private and the method or the class is final
#ifdef COMPILE_DEBUG
if (compile_debug) {
printf(" adjust invokevirtual private method to "
"invokespecial.\n");
fflush(stdout);
}
#endif
opcode = opc_invokespecial;
goto makepc_invokespecial;
}
processAnOpcode(cc, opc_stateto0, byteoff);
processAnOpcode(cc, opc_inv_head, byteoff);
processAnOpcode(cc, opc_inv_vir_obj, byteoff);
if (!strcmp(cbName(clz), "java/lang/Object"))
processAnOpcode(cc, opc_invokevirtual, byteoff);
else
processAnOpcode(cc, opc_invokevirtual_obj, byteoff);
#ifdef METAVM
processAnOpcode(cc, opc_inv_metavm, byteoff);
#endif
processAnOpcode(cc, opc_inv_vir_varspace, byteoff);
processAnOpcode(cc, opc_invoke_core, byteoff);
}
break;
case opc_invokespecial:
case opc_invokestatic:
makepc_invokespecial:
// translate invokevirtual Object#<init> to invokeignored_quick
{
struct methodblock *method = constant_pool[operand].mb;
if (!(method->fb.access & (ACC_NATIVE | ACC_ABSTRACT)) &&
(method->code[0] == opc_return)) {
unsigned char *bytepc = methodcode + byteoff;
int toNullCheck = ((method->fb.access & ACC_STATIC) == 0)
#if JDK_VER >= 12
&& (strcmp(method->fb.name, "<init>") != 0)
#endif
;
// condition: !static && !constructor
// omit null checks for constructors if JDK is 1.2 or later
#ifdef NO_NULL_AND_ARRAY_CHECK
processAnOpcode(cc, opc_invokeignored_static_quick, byteoff);
#else
if (toNullCheck) {
processAnOpcode(cc, opc_invokeignored_quick, byteoff);
}
else {
opcode = ((CB_INITIALIZED(fieldclass(&method->fb))) ?
opc_invokeignored_static_quick : opc_invokeignored_static);
processAnOpcode(cc, opcode, byteoff);
}
#endif // NO_NULL_AND_ARRAY_CHECK
// rewrite bytecode instruction
#if JDK_VER >= 12
CODE_LOCK(EE2SysThread(cc->ee));
#else
BINCLASS_LOCK();
#endif // JDK_VER
bytepc[0] = opc_invokeignored_quick;
bytepc[1] = method->args_size;
bytepc[2] = (unsigned char)toNullCheck;
// indicates whether to be checked or not
#if JDK_VER >= 12
CODE_UNLOCK(EE2SysThread(cc->ee));
#else
BINCLASS_UNLOCK();
#endif // JDK_VER
goto makepc_invoke_done;
}
}
#ifdef ELIMINATE_TAIL_RECURSION
{
struct methodblock *method = constant_pool[operand].mb;
if ((cc->mb /* not mb */ == method) && // recursive call
(!(method->fb.access & ACC_SYNCHRONIZED))) { // not synchronized
int next_bytepc = byteoff + byteinc;
int next_opcode = *(methodcode + next_bytepc);
while ((next_opcode == opc_goto) || (next_opcode == opc_goto_w)) {
if (next_opcode == opc_goto) {
next_bytepc += GET_INT16(methodcode + next_bytepc + 1);
}
else {
next_bytepc += GET_INT32(methodcode + next_bytepc + 1);
}
next_opcode = *(methodcode + next_bytepc);
}
if ((opc_ireturn <= next_opcode) && (next_opcode <= opc_return)) {
// next insn. is return
if (searchCatchFrame(cc->ee, cc->mb, byteoff
# ifdef RUNTIME_DEBUG
, COMPILE_DEBUG
# endif
) == NULL) {
// the call is not in a try clause
// tail recursion
int reccall_opcode;
# ifdef COMPILE_DEBUG
if (compile_debug) {
printf("tail recursion.\n");
printf(" args_size: %d\n", method->args_size);
fflush(stdout);
}
# endif
switch (method->args_size) {