forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassFileAssembler.java
671 lines (562 loc) · 17.1 KB
/
ClassFileAssembler.java
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
/*
* Copyright (c) 2001, 2004, Oracle 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. Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.reflect;
class ClassFileAssembler implements ClassFileConstants {
private ByteVector vec;
private short cpIdx = 0;
public ClassFileAssembler() {
this(ByteVectorFactory.create());
}
public ClassFileAssembler(ByteVector vec) {
this.vec = vec;
}
public ByteVector getData() {
return vec;
}
/** Length in bytes */
public short getLength() {
return (short) vec.getLength();
}
public void emitMagicAndVersion() {
emitInt(0xCAFEBABE);
emitShort((short) 0);
emitShort((short) 49);
}
public void emitInt(int val) {
emitByte((byte) (val >> 24));
emitByte((byte) ((val >> 16) & 0xFF));
emitByte((byte) ((val >> 8) & 0xFF));
emitByte((byte) (val & 0xFF));
}
public void emitShort(short val) {
emitByte((byte) ((val >> 8) & 0xFF));
emitByte((byte) (val & 0xFF));
}
// Support for labels; package-private
void emitShort(short bci, short val) {
vec.put(bci, (byte) ((val >> 8) & 0xFF));
vec.put(bci + 1, (byte) (val & 0xFF));
}
public void emitByte(byte val) {
vec.add(val);
}
public void append(ClassFileAssembler asm) {
append(asm.vec);
}
public void append(ByteVector vec) {
for (int i = 0; i < vec.getLength(); i++) {
emitByte(vec.get(i));
}
}
/** Keeps track of the current (one-based) constant pool index;
incremented after emitting one of the following constant pool
entries. Can fetch the current constant pool index for use in
later entries. Index points at the last valid constant pool
entry; initially invalid. It is illegal to fetch the constant
pool index before emitting at least one constant pool entry. */
public short cpi() {
if (cpIdx == 0) {
throw new RuntimeException("Illegal use of ClassFileAssembler");
}
return cpIdx;
}
public void emitConstantPoolUTF8(String str) {
// NOTE: can not use str.getBytes("UTF-8") here because of
// bootstrapping issues with the character set converters.
byte[] bytes = UTF8.encode(str);
emitByte(CONSTANT_Utf8);
emitShort((short) bytes.length);
for (int i = 0; i < bytes.length; i++) {
emitByte(bytes[i]);
}
cpIdx++;
}
public void emitConstantPoolClass(short index) {
emitByte(CONSTANT_Class);
emitShort(index);
cpIdx++;
}
public void emitConstantPoolNameAndType(short nameIndex, short typeIndex) {
emitByte(CONSTANT_NameAndType);
emitShort(nameIndex);
emitShort(typeIndex);
cpIdx++;
}
public void emitConstantPoolFieldref
(short classIndex, short nameAndTypeIndex)
{
emitByte(CONSTANT_Fieldref);
emitShort(classIndex);
emitShort(nameAndTypeIndex);
cpIdx++;
}
public void emitConstantPoolMethodref
(short classIndex, short nameAndTypeIndex)
{
emitByte(CONSTANT_Methodref);
emitShort(classIndex);
emitShort(nameAndTypeIndex);
cpIdx++;
}
public void emitConstantPoolInterfaceMethodref
(short classIndex, short nameAndTypeIndex)
{
emitByte(CONSTANT_InterfaceMethodref);
emitShort(classIndex);
emitShort(nameAndTypeIndex);
cpIdx++;
}
public void emitConstantPoolString(short utf8Index) {
emitByte(CONSTANT_String);
emitShort(utf8Index);
cpIdx++;
}
//----------------------------------------------------------------------
// Opcodes. Keeps track of maximum stack and locals. Make a new
// assembler for each piece of assembled code, then append the
// result to the previous assembler's class file.
//
private int stack = 0;
private int maxStack = 0;
private int maxLocals = 0;
private void incStack() {
setStack(stack + 1);
}
private void decStack() {
--stack;
}
public short getMaxStack() {
return (short) maxStack;
}
public short getMaxLocals() {
return (short) maxLocals;
}
/** It's necessary to be able to specify the number of arguments at
the beginning of the method (which translates to the initial
value of max locals) */
public void setMaxLocals(int maxLocals) {
this.maxLocals = maxLocals;
}
/** Needed to do flow control. Returns current stack depth. */
public int getStack() {
return stack;
}
/** Needed to do flow control. */
public void setStack(int value) {
stack = value;
if (stack > maxStack) {
maxStack = stack;
}
}
///////////////
// Constants //
///////////////
public void opc_aconst_null() {
emitByte(opc_aconst_null);
incStack();
}
public void opc_sipush(short constant) {
emitByte(opc_sipush);
emitShort(constant);
incStack();
}
public void opc_ldc(byte cpIdx) {
emitByte(opc_ldc);
emitByte(cpIdx);
incStack();
}
/////////////////////////////////////
// Local variable loads and stores //
/////////////////////////////////////
public void opc_iload_0() {
emitByte(opc_iload_0);
if (maxLocals < 1) maxLocals = 1;
incStack();
}
public void opc_iload_1() {
emitByte(opc_iload_1);
if (maxLocals < 2) maxLocals = 2;
incStack();
}
public void opc_iload_2() {
emitByte(opc_iload_2);
if (maxLocals < 3) maxLocals = 3;
incStack();
}
public void opc_iload_3() {
emitByte(opc_iload_3);
if (maxLocals < 4) maxLocals = 4;
incStack();
}
public void opc_lload_0() {
emitByte(opc_lload_0);
if (maxLocals < 2) maxLocals = 2;
incStack();
incStack();
}
public void opc_lload_1() {
emitByte(opc_lload_1);
if (maxLocals < 3) maxLocals = 3;
incStack();
incStack();
}
public void opc_lload_2() {
emitByte(opc_lload_2);
if (maxLocals < 4) maxLocals = 4;
incStack();
incStack();
}
public void opc_lload_3() {
emitByte(opc_lload_3);
if (maxLocals < 5) maxLocals = 5;
incStack();
incStack();
}
public void opc_fload_0() {
emitByte(opc_fload_0);
if (maxLocals < 1) maxLocals = 1;
incStack();
}
public void opc_fload_1() {
emitByte(opc_fload_1);
if (maxLocals < 2) maxLocals = 2;
incStack();
}
public void opc_fload_2() {
emitByte(opc_fload_2);
if (maxLocals < 3) maxLocals = 3;
incStack();
}
public void opc_fload_3() {
emitByte(opc_fload_3);
if (maxLocals < 4) maxLocals = 4;
incStack();
}
public void opc_dload_0() {
emitByte(opc_dload_0);
if (maxLocals < 2) maxLocals = 2;
incStack();
incStack();
}
public void opc_dload_1() {
emitByte(opc_dload_1);
if (maxLocals < 3) maxLocals = 3;
incStack();
incStack();
}
public void opc_dload_2() {
emitByte(opc_dload_2);
if (maxLocals < 4) maxLocals = 4;
incStack();
incStack();
}
public void opc_dload_3() {
emitByte(opc_dload_3);
if (maxLocals < 5) maxLocals = 5;
incStack();
incStack();
}
public void opc_aload_0() {
emitByte(opc_aload_0);
if (maxLocals < 1) maxLocals = 1;
incStack();
}
public void opc_aload_1() {
emitByte(opc_aload_1);
if (maxLocals < 2) maxLocals = 2;
incStack();
}
public void opc_aload_2() {
emitByte(opc_aload_2);
if (maxLocals < 3) maxLocals = 3;
incStack();
}
public void opc_aload_3() {
emitByte(opc_aload_3);
if (maxLocals < 4) maxLocals = 4;
incStack();
}
public void opc_aaload() {
emitByte(opc_aaload);
decStack();
}
public void opc_astore_0() {
emitByte(opc_astore_0);
if (maxLocals < 1) maxLocals = 1;
decStack();
}
public void opc_astore_1() {
emitByte(opc_astore_1);
if (maxLocals < 2) maxLocals = 2;
decStack();
}
public void opc_astore_2() {
emitByte(opc_astore_2);
if (maxLocals < 3) maxLocals = 3;
decStack();
}
public void opc_astore_3() {
emitByte(opc_astore_3);
if (maxLocals < 4) maxLocals = 4;
decStack();
}
////////////////////////
// Stack manipulation //
////////////////////////
public void opc_pop() {
emitByte(opc_pop);
decStack();
}
public void opc_dup() {
emitByte(opc_dup);
incStack();
}
public void opc_dup_x1() {
emitByte(opc_dup_x1);
incStack();
}
public void opc_swap() {
emitByte(opc_swap);
}
///////////////////////////////
// Widening conversions only //
///////////////////////////////
public void opc_i2l() {
emitByte(opc_i2l);
}
public void opc_i2f() {
emitByte(opc_i2f);
}
public void opc_i2d() {
emitByte(opc_i2d);
}
public void opc_l2f() {
emitByte(opc_l2f);
}
public void opc_l2d() {
emitByte(opc_l2d);
}
public void opc_f2d() {
emitByte(opc_f2d);
}
//////////////////
// Control flow //
//////////////////
public void opc_ifeq(short bciOffset) {
emitByte(opc_ifeq);
emitShort(bciOffset);
decStack();
}
/** Control flow with forward-reference BCI. Stack assumes
straight-through control flow. */
public void opc_ifeq(Label l) {
short instrBCI = getLength();
emitByte(opc_ifeq);
l.add(this, instrBCI, getLength(), getStack() - 1);
emitShort((short) -1); // Must be patched later
}
public void opc_if_icmpeq(short bciOffset) {
emitByte(opc_if_icmpeq);
emitShort(bciOffset);
setStack(getStack() - 2);
}
/** Control flow with forward-reference BCI. Stack assumes straight
control flow. */
public void opc_if_icmpeq(Label l) {
short instrBCI = getLength();
emitByte(opc_if_icmpeq);
l.add(this, instrBCI, getLength(), getStack() - 2);
emitShort((short) -1); // Must be patched later
}
public void opc_goto(short bciOffset) {
emitByte(opc_goto);
emitShort(bciOffset);
}
/** Control flow with forward-reference BCI. Stack assumes straight
control flow. */
public void opc_goto(Label l) {
short instrBCI = getLength();
emitByte(opc_goto);
l.add(this, instrBCI, getLength(), getStack());
emitShort((short) -1); // Must be patched later
}
public void opc_ifnull(short bciOffset) {
emitByte(opc_ifnull);
emitShort(bciOffset);
decStack();
}
/** Control flow with forward-reference BCI. Stack assumes straight
control flow. */
public void opc_ifnull(Label l) {
short instrBCI = getLength();
emitByte(opc_ifnull);
l.add(this, instrBCI, getLength(), getStack() - 1);
emitShort((short) -1); // Must be patched later
decStack();
}
public void opc_ifnonnull(short bciOffset) {
emitByte(opc_ifnonnull);
emitShort(bciOffset);
decStack();
}
/** Control flow with forward-reference BCI. Stack assumes straight
control flow. */
public void opc_ifnonnull(Label l) {
short instrBCI = getLength();
emitByte(opc_ifnonnull);
l.add(this, instrBCI, getLength(), getStack() - 1);
emitShort((short) -1); // Must be patched later
decStack();
}
/////////////////////////
// Return instructions //
/////////////////////////
public void opc_ireturn() {
emitByte(opc_ireturn);
setStack(0);
}
public void opc_lreturn() {
emitByte(opc_lreturn);
setStack(0);
}
public void opc_freturn() {
emitByte(opc_freturn);
setStack(0);
}
public void opc_dreturn() {
emitByte(opc_dreturn);
setStack(0);
}
public void opc_areturn() {
emitByte(opc_areturn);
setStack(0);
}
public void opc_return() {
emitByte(opc_return);
setStack(0);
}
//////////////////////
// Field operations //
//////////////////////
public void opc_getstatic(short fieldIndex, int fieldSizeInStackSlots) {
emitByte(opc_getstatic);
emitShort(fieldIndex);
setStack(getStack() + fieldSizeInStackSlots);
}
public void opc_putstatic(short fieldIndex, int fieldSizeInStackSlots) {
emitByte(opc_putstatic);
emitShort(fieldIndex);
setStack(getStack() - fieldSizeInStackSlots);
}
public void opc_getfield(short fieldIndex, int fieldSizeInStackSlots) {
emitByte(opc_getfield);
emitShort(fieldIndex);
setStack(getStack() + fieldSizeInStackSlots - 1);
}
public void opc_putfield(short fieldIndex, int fieldSizeInStackSlots) {
emitByte(opc_putfield);
emitShort(fieldIndex);
setStack(getStack() - fieldSizeInStackSlots - 1);
}
////////////////////////
// Method invocations //
////////////////////////
/** Long and double arguments and return types count as 2 arguments;
other values count as 1. */
public void opc_invokevirtual(short methodIndex,
int numArgs,
int numReturnValues)
{
emitByte(opc_invokevirtual);
emitShort(methodIndex);
setStack(getStack() - numArgs - 1 + numReturnValues);
}
/** Long and double arguments and return types count as 2 arguments;
other values count as 1. */
public void opc_invokespecial(short methodIndex,
int numArgs,
int numReturnValues)
{
emitByte(opc_invokespecial);
emitShort(methodIndex);
setStack(getStack() - numArgs - 1 + numReturnValues);
}
/** Long and double arguments and return types count as 2 arguments;
other values count as 1. */
public void opc_invokestatic(short methodIndex,
int numArgs,
int numReturnValues)
{
emitByte(opc_invokestatic);
emitShort(methodIndex);
setStack(getStack() - numArgs + numReturnValues);
}
/** Long and double arguments and return types count as 2 arguments;
other values count as 1. */
public void opc_invokeinterface(short methodIndex,
int numArgs,
byte count,
int numReturnValues)
{
emitByte(opc_invokeinterface);
emitShort(methodIndex);
emitByte(count);
emitByte((byte) 0);
setStack(getStack() - numArgs - 1 + numReturnValues);
}
//////////////////
// Array length //
//////////////////
public void opc_arraylength() {
emitByte(opc_arraylength);
}
/////////
// New //
/////////
public void opc_new(short classIndex) {
emitByte(opc_new);
emitShort(classIndex);
incStack();
}
////////////
// Athrow //
////////////
public void opc_athrow() {
emitByte(opc_athrow);
setStack(1);
}
//////////////////////////////
// Checkcast and instanceof //
//////////////////////////////
/** Assumes the checkcast succeeds */
public void opc_checkcast(short classIndex) {
emitByte(opc_checkcast);
emitShort(classIndex);
}
public void opc_instanceof(short classIndex) {
emitByte(opc_instanceof);
emitShort(classIndex);
}
}