/
MethodBuilder.java
741 lines (589 loc) · 16.5 KB
/
MethodBuilder.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
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
package org.isk.jvmhardcore.pjba.builder;
import org.isk.jvmhardcore.pjba.instruction.Instructions;
import org.isk.jvmhardcore.pjba.structure.Instruction;
import org.isk.jvmhardcore.pjba.structure.Method;
import org.isk.jvmhardcore.pjba.structure.attribute.Code;
public class MethodBuilder {
final private ClassFileBuilder classFileBuilder;
final private Code code;
public MethodBuilder(ClassFileBuilder classFileBuilder, Method method, int parametersCount) {
this.classFileBuilder = classFileBuilder;
final int codeAttributeIndex = this.classFileBuilder.getClassFile().addConstantUTF8(Code.ATTRIBUTE_NAME);
this.code = new Code(codeAttributeIndex);
this.code.setParameterCount(parametersCount);
method.addAttibute(this.code);
}
public MethodBuilder instruction(Instruction instruction) {
this.code.addInstruction(instruction);
return this;
}
public MethodBuilder nop() {
this.code.addInstruction(Instructions.NOP);
return this;
}
public MethodBuilder aconst_null() {
this.code.addInstruction(Instructions.ACONST_NULL);
return this;
}
public MethodBuilder iconst_m1() {
this.code.addInstruction(Instructions.ICONST_M1);
return this;
}
public MethodBuilder iconst_0() {
this.code.addInstruction(Instructions.ICONST_0);
return this;
}
public MethodBuilder iconst_1() {
this.code.addInstruction(Instructions.ICONST_1);
return this;
}
public MethodBuilder iconst_2() {
this.code.addInstruction(Instructions.ICONST_2);
return this;
}
public MethodBuilder iconst_3() {
this.code.addInstruction(Instructions.ICONST_3);
return this;
}
public MethodBuilder iconst_4() {
this.code.addInstruction(Instructions.ICONST_4);
return this;
}
public MethodBuilder iconst_5() {
this.code.addInstruction(Instructions.ICONST_5);
return this;
}
public MethodBuilder lconst_0() {
this.code.addInstruction(Instructions.LCONST_0);
return this;
}
public MethodBuilder lconst_1() {
this.code.addInstruction(Instructions.LCONST_1);
return this;
}
public MethodBuilder fconst_0() {
this.code.addInstruction(Instructions.FCONST_0);
return this;
}
public MethodBuilder fconst_1() {
this.code.addInstruction(Instructions.FCONST_1);
return this;
}
public MethodBuilder fconst_2() {
this.code.addInstruction(Instructions.FCONST_2);
return this;
}
public MethodBuilder dconst_0() {
this.code.addInstruction(Instructions.DCONST_0);
return this;
}
public MethodBuilder dconst_1() {
this.code.addInstruction(Instructions.DCONST_1);
return this;
}
public MethodBuilder bipush(byte value) {
this.code.addInstruction(Instructions.bipush(value));
return this;
}
public MethodBuilder sipush(short value) {
this.code.addInstruction(Instructions.sipush(value));
return this;
}
public MethodBuilder ldc(String value) {
final int utf8Index = this.classFileBuilder.getClassFile().addConstantUTF8(value);
final int indexInCP = this.classFileBuilder.getClassFile().addConstantString(utf8Index);
this.internalLdc(indexInCP);
return this;
}
public MethodBuilder ldc(int value) {
final int indexInCP = this.classFileBuilder.getClassFile().addConstantInteger(value);
this.internalLdc(indexInCP);
return this;
}
public MethodBuilder ldc(float value) {
final int indexInCP = this.classFileBuilder.getClassFile().addConstantFloat(value);
this.internalLdc(indexInCP);
return this;
}
public MethodBuilder ldc(long value) {
final int indexInCP = this.classFileBuilder.getClassFile().addConstantLong(value);
this.code.addInstruction(Instructions.ldc2_w((short) indexInCP));
return this;
}
public MethodBuilder ldc(double value) {
final int indexInCP = this.classFileBuilder.getClassFile().addConstantDouble(value);
this.code.addInstruction(Instructions.ldc2_w((short) indexInCP));
return this;
}
private void internalLdc(final int indexInCP) {
if (indexInCP > 255) { // 255 = Byte.MAX_UNSIGNED_VALUE
this.code.addInstruction(Instructions.ldc_w((short) indexInCP));
} else {
this.code.addInstruction(Instructions.ldc((byte) indexInCP));
}
}
public MethodBuilder iload(byte indexInLV) {
this.code.addInstruction(Instructions.iload(indexInLV));
return this;
}
public MethodBuilder lload(byte indexInLV) {
this.code.addInstruction(Instructions.lload(indexInLV));
return this;
}
public MethodBuilder fload(byte indexInLV) {
this.code.addInstruction(Instructions.fload(indexInLV));
return this;
}
public MethodBuilder dload(byte indexInLV) {
this.code.addInstruction(Instructions.dload(indexInLV));
return this;
}
public MethodBuilder aload(byte indexInLV) {
this.code.addInstruction(Instructions.aload(indexInLV));
return this;
}
public MethodBuilder iload_0() {
this.code.addInstruction(Instructions.ILOAD_0);
return this;
}
public MethodBuilder iload_1() {
this.code.addInstruction(Instructions.ILOAD_1);
return this;
}
public MethodBuilder iload_2() {
this.code.addInstruction(Instructions.ILOAD_2);
return this;
}
public MethodBuilder iload_3() {
this.code.addInstruction(Instructions.ILOAD_3);
return this;
}
public MethodBuilder lload_0() {
this.code.addInstruction(Instructions.LLOAD_0);
return this;
}
public MethodBuilder lload_1() {
this.code.addInstruction(Instructions.LLOAD_1);
return this;
}
public MethodBuilder lload_2() {
this.code.addInstruction(Instructions.LLOAD_2);
return this;
}
public MethodBuilder lload_3() {
this.code.addInstruction(Instructions.LLOAD_3);
return this;
}
public MethodBuilder fload_0() {
this.code.addInstruction(Instructions.FLOAD_0);
return this;
}
public MethodBuilder fload_1() {
this.code.addInstruction(Instructions.FLOAD_1);
return this;
}
public MethodBuilder fload_2() {
this.code.addInstruction(Instructions.FLOAD_2);
return this;
}
public MethodBuilder fload_3() {
this.code.addInstruction(Instructions.FLOAD_3);
return this;
}
public MethodBuilder dload_0() {
this.code.addInstruction(Instructions.DLOAD_0);
return this;
}
public MethodBuilder dload_1() {
this.code.addInstruction(Instructions.DLOAD_1);
return this;
}
public MethodBuilder dload_2() {
this.code.addInstruction(Instructions.DLOAD_2);
return this;
}
public MethodBuilder dload_3() {
this.code.addInstruction(Instructions.DLOAD_3);
return this;
}
public MethodBuilder aload_0() {
this.code.addInstruction(Instructions.ALOAD_0);
return this;
}
public MethodBuilder aload_1() {
this.code.addInstruction(Instructions.ALOAD_1);
return this;
}
public MethodBuilder aload_2() {
this.code.addInstruction(Instructions.ALOAD_2);
return this;
}
public MethodBuilder aload_3() {
this.code.addInstruction(Instructions.ALOAD_3);
return this;
}
public MethodBuilder istore(byte indexInLV) {
this.code.addInstruction(Instructions.istore(indexInLV));
return this;
}
public MethodBuilder lstore(byte indexInLV) {
this.code.addInstruction(Instructions.lstore(indexInLV));
return this;
}
public MethodBuilder fstore(byte indexInLV) {
this.code.addInstruction(Instructions.fstore(indexInLV));
return this;
}
public MethodBuilder dstore(byte indexInLV) {
this.code.addInstruction(Instructions.dstore(indexInLV));
return this;
}
public MethodBuilder astore(byte indexInLV) {
this.code.addInstruction(Instructions.astore(indexInLV));
return this;
}
public MethodBuilder istore_0() {
this.code.addInstruction(Instructions.ISTORE_0);
return this;
}
public MethodBuilder istore_1() {
this.code.addInstruction(Instructions.ISTORE_1);
return this;
}
public MethodBuilder istore_2() {
this.code.addInstruction(Instructions.ISTORE_2);
return this;
}
public MethodBuilder istore_3() {
this.code.addInstruction(Instructions.ISTORE_3);
return this;
}
public MethodBuilder lstore_0() {
this.code.addInstruction(Instructions.LSTORE_0);
return this;
}
public MethodBuilder lstore_1() {
this.code.addInstruction(Instructions.LSTORE_1);
return this;
}
public MethodBuilder lstore_2() {
this.code.addInstruction(Instructions.LSTORE_2);
return this;
}
public MethodBuilder lstore_3() {
this.code.addInstruction(Instructions.LSTORE_3);
return this;
}
public MethodBuilder fstore_0() {
this.code.addInstruction(Instructions.FSTORE_0);
return this;
}
public MethodBuilder fstore_1() {
this.code.addInstruction(Instructions.FSTORE_1);
return this;
}
public MethodBuilder fstore_2() {
this.code.addInstruction(Instructions.FSTORE_2);
return this;
}
public MethodBuilder fstore_3() {
this.code.addInstruction(Instructions.FSTORE_3);
return this;
}
public MethodBuilder dstore_0() {
this.code.addInstruction(Instructions.DSTORE_0);
return this;
}
public MethodBuilder dstore_1() {
this.code.addInstruction(Instructions.DSTORE_1);
return this;
}
public MethodBuilder dstore_2() {
this.code.addInstruction(Instructions.DSTORE_2);
return this;
}
public MethodBuilder dstore_3() {
this.code.addInstruction(Instructions.DSTORE_3);
return this;
}
public MethodBuilder astore_0() {
this.code.addInstruction(Instructions.ASTORE_0);
return this;
}
public MethodBuilder astore_1() {
this.code.addInstruction(Instructions.ASTORE_1);
return this;
}
public MethodBuilder astore_2() {
this.code.addInstruction(Instructions.ASTORE_2);
return this;
}
public MethodBuilder astore_3() {
this.code.addInstruction(Instructions.ASTORE_3);
return this;
}
public MethodBuilder pop() {
this.code.addInstruction(Instructions.POP);
return this;
}
public MethodBuilder pop2() {
this.code.addInstruction(Instructions.POP2);
return this;
}
public MethodBuilder dup() {
this.code.addInstruction(Instructions.DUP);
return this;
}
public MethodBuilder dup_x1() {
this.code.addInstruction(Instructions.DUP_X1);
return this;
}
public MethodBuilder dup_x2() {
this.code.addInstruction(Instructions.DUP_X2);
return this;
}
public MethodBuilder dup2() {
this.code.addInstruction(Instructions.DUP2);
return this;
}
public MethodBuilder dup2_x1() {
this.code.addInstruction(Instructions.DUP2_X1);
return this;
}
public MethodBuilder dup2_x2() {
this.code.addInstruction(Instructions.DUP2_X2);
return this;
}
public MethodBuilder swap() {
this.code.addInstruction(Instructions.SWAP);
return this;
}
public MethodBuilder iadd() {
this.code.addInstruction(Instructions.IADD);
return this;
}
public MethodBuilder ladd() {
this.code.addInstruction(Instructions.LADD);
return this;
}
public MethodBuilder fadd() {
this.code.addInstruction(Instructions.FADD);
return this;
}
public MethodBuilder dadd() {
this.code.addInstruction(Instructions.DADD);
return this;
}
public MethodBuilder isub() {
this.code.addInstruction(Instructions.ISUB);
return this;
}
public MethodBuilder lsub() {
this.code.addInstruction(Instructions.LSUB);
return this;
}
public MethodBuilder fsub() {
this.code.addInstruction(Instructions.FSUB);
return this;
}
public MethodBuilder dsub() {
this.code.addInstruction(Instructions.DSUB);
return this;
}
public MethodBuilder imul() {
this.code.addInstruction(Instructions.IMUL);
return this;
}
public MethodBuilder lmul() {
this.code.addInstruction(Instructions.LMUL);
return this;
}
public MethodBuilder fmul() {
this.code.addInstruction(Instructions.FMUL);
return this;
}
public MethodBuilder dmul() {
this.code.addInstruction(Instructions.DMUL);
return this;
}
public MethodBuilder idiv() {
this.code.addInstruction(Instructions.IDIV);
return this;
}
public MethodBuilder ldiv() {
this.code.addInstruction(Instructions.LDIV);
return this;
}
public MethodBuilder fdiv() {
this.code.addInstruction(Instructions.FDIV);
return this;
}
public MethodBuilder ddiv() {
this.code.addInstruction(Instructions.DDIV);
return this;
}
public MethodBuilder irem() {
this.code.addInstruction(Instructions.IREM);
return this;
}
public MethodBuilder lrem() {
this.code.addInstruction(Instructions.LREM);
return this;
}
public MethodBuilder frem() {
this.code.addInstruction(Instructions.FREM);
return this;
}
public MethodBuilder drem() {
this.code.addInstruction(Instructions.DREM);
return this;
}
public MethodBuilder ineg() {
this.code.addInstruction(Instructions.INEG);
return this;
}
public MethodBuilder lneg() {
this.code.addInstruction(Instructions.LNEG);
return this;
}
public MethodBuilder fneg() {
this.code.addInstruction(Instructions.FNEG);
return this;
}
public MethodBuilder dneg() {
this.code.addInstruction(Instructions.DNEG);
return this;
}
public MethodBuilder ishl() {
this.code.addInstruction(Instructions.ISHL);
return this;
}
public MethodBuilder lshl() {
this.code.addInstruction(Instructions.LSHL);
return this;
}
public MethodBuilder ishr() {
this.code.addInstruction(Instructions.ISHR);
return this;
}
public MethodBuilder lshr() {
this.code.addInstruction(Instructions.LSHR);
return this;
}
public MethodBuilder iushr() {
this.code.addInstruction(Instructions.IUSHR);
return this;
}
public MethodBuilder lushr() {
this.code.addInstruction(Instructions.LUSHR);
return this;
}
public MethodBuilder iand() {
this.code.addInstruction(Instructions.IAND);
return this;
}
public MethodBuilder land() {
this.code.addInstruction(Instructions.LAND);
return this;
}
public MethodBuilder ior() {
this.code.addInstruction(Instructions.IOR);
return this;
}
public MethodBuilder lor() {
this.code.addInstruction(Instructions.LOR);
return this;
}
public MethodBuilder ixor() {
this.code.addInstruction(Instructions.IXOR);
return this;
}
public MethodBuilder lxor() {
this.code.addInstruction(Instructions.LXOR);
return this;
}
public MethodBuilder i2l() {
this.code.addInstruction(Instructions.I2L);
return this;
}
public MethodBuilder i2f() {
this.code.addInstruction(Instructions.I2F);
return this;
}
public MethodBuilder i2d() {
this.code.addInstruction(Instructions.I2D);
return this;
}
public MethodBuilder l2i() {
this.code.addInstruction(Instructions.L2I);
return this;
}
public MethodBuilder l2f() {
this.code.addInstruction(Instructions.L2F);
return this;
}
public MethodBuilder l2d() {
this.code.addInstruction(Instructions.L2D);
return this;
}
public MethodBuilder f2i() {
this.code.addInstruction(Instructions.F2I);
return this;
}
public MethodBuilder f2l() {
this.code.addInstruction(Instructions.F2L);
return this;
}
public MethodBuilder f2d() {
this.code.addInstruction(Instructions.F2D);
return this;
}
public MethodBuilder d2i() {
this.code.addInstruction(Instructions.D2I);
return this;
}
public MethodBuilder d2l() {
this.code.addInstruction(Instructions.D2L);
return this;
}
public MethodBuilder d2f() {
this.code.addInstruction(Instructions.D2F);
return this;
}
public MethodBuilder i2b() {
this.code.addInstruction(Instructions.I2B);
return this;
}
public MethodBuilder i2c() {
this.code.addInstruction(Instructions.I2C);
return this;
}
public MethodBuilder i2s() {
this.code.addInstruction(Instructions.I2S);
return this;
}
public ClassFileBuilder ireturn() {
this.code.addInstruction(Instructions.IRETURN);
return this.classFileBuilder;
}
public ClassFileBuilder lreturn() {
this.code.addInstruction(Instructions.LRETURN);
return this.classFileBuilder;
}
public ClassFileBuilder freturn() {
this.code.addInstruction(Instructions.FRETURN);
return this.classFileBuilder;
}
public ClassFileBuilder dreturn() {
this.code.addInstruction(Instructions.DRETURN);
return this.classFileBuilder;
}
public ClassFileBuilder areturn() {
this.code.addInstruction(Instructions.ARETURN);
return this.classFileBuilder;
}
public ClassFileBuilder return_() {
this.code.addInstruction(Instructions.RETURN);
return this.classFileBuilder;
}
}