-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOpcodeHandlerExtended.java
More file actions
1027 lines (924 loc) · 36.1 KB
/
OpcodeHandlerExtended.java
File metadata and controls
1027 lines (924 loc) · 36.1 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
package org.perlonjava.backend.bytecode;
import org.perlonjava.runtime.operators.*;
import org.perlonjava.runtime.regex.RuntimeRegex;
import org.perlonjava.runtime.runtimetypes.*;
/**
* Extended opcode handlers for recently added operations.
* <p>
* Extracted from BytecodeInterpreter.execute() to reduce method size
* and keep it under the 8KB JIT compilation limit.
* <p>
* Handles: SPRINTF, CHOP, GET_REPLACEMENT_REGEX, SUBSTR_VAR, and other
* less-frequently-used string/regex operations.
*/
public class OpcodeHandlerExtended {
/**
* Execute sprintf operation.
* Format: SPRINTF rd formatReg argsListReg
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeSprintf(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int formatReg = bytecode[pc++];
int argsListReg = bytecode[pc++];
RuntimeScalar format = (RuntimeScalar) registers[formatReg];
RuntimeList argsList = (RuntimeList) registers[argsListReg];
registers[rd] = SprintfOperator.sprintf(format, argsList);
return pc;
}
/**
* Execute chop operation.
* Format: CHOP rd scalarReg
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeChop(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int scalarReg = bytecode[pc++];
registers[rd] = registers[scalarReg].chop();
return pc;
}
/**
* Execute get replacement regex operation.
* Format: GET_REPLACEMENT_REGEX rd pattern_reg replacement_reg flags_reg args_reg
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeGetReplacementRegex(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int patternReg = bytecode[pc++];
int replacementReg = bytecode[pc++];
int flagsReg = bytecode[pc++];
int argsReg = bytecode[pc++];
RuntimeScalar pattern = (RuntimeScalar) registers[patternReg];
RuntimeScalar replacement = (RuntimeScalar) registers[replacementReg];
RuntimeScalar flags = (RuntimeScalar) registers[flagsReg];
RuntimeArray callerArgs = (registers[argsReg] instanceof RuntimeArray) ? (RuntimeArray) registers[argsReg] : new RuntimeArray();
registers[rd] = RuntimeRegex.getReplacementRegex(pattern, replacement, flags, callerArgs);
return pc;
}
/**
* Execute substr with variable arguments.
* Format: SUBSTR_VAR rd argsListReg ctx
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeSubstrVar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int argsListReg = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
RuntimeList argsList = (RuntimeList) registers[argsListReg];
RuntimeBase[] substrArgs = argsList.elements.toArray(new RuntimeBase[0]);
registers[rd] = Operator.substr(ctx, substrArgs);
return pc;
}
/**
* Execute substr with variable args, no warning.
* Format: SUBSTR_VAR_NO_WARN rd argsListReg ctx
* Used when 'no warnings "substr"' is in effect at compile time.
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeSubstrVarNoWarn(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int argsListReg = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
RuntimeList argsList = (RuntimeList) registers[argsListReg];
RuntimeBase[] substrArgs = argsList.elements.toArray(new RuntimeBase[0]);
registers[rd] = Operator.substrNoWarn(ctx, substrArgs);
return pc;
}
/**
* Execute repeat assign operation.
* Format: REPEAT_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeRepeatAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeBase result = Operator.repeat(
registers[rd],
(RuntimeScalar) registers[rs],
1 // scalar context
);
((RuntimeScalar) registers[rd]).set((RuntimeScalar) result);
return pc;
}
/**
* Execute power assign operation.
* Format: POW_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executePowAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeBase val1 = registers[rd];
RuntimeBase val2 = registers[rs];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
// Use warn variant - it checks at runtime if warnings are enabled
RuntimeScalar result = MathOperators.powWarn(s1, s2);
((RuntimeScalar) registers[rd]).set(result);
return pc;
}
/**
* Execute left shift assign operation.
* Format: LEFT_SHIFT_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeLeftShiftAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar s1 = (RuntimeScalar) registers[rd];
RuntimeScalar s2 = (RuntimeScalar) registers[rs];
RuntimeScalar result = BitwiseOperators.shiftLeft(s1, s2);
s1.set(result);
return pc;
}
/**
* Execute right shift assign operation.
* Format: RIGHT_SHIFT_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeRightShiftAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar s1 = (RuntimeScalar) registers[rd];
RuntimeScalar s2 = (RuntimeScalar) registers[rs];
RuntimeScalar result = BitwiseOperators.shiftRight(s1, s2);
s1.set(result);
return pc;
}
/**
* Execute logical AND assign operation.
* Format: LOGICAL_AND_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeLogicalAndAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar s1 = registers[rd].scalar();
if (!s1.getBoolean()) {
return pc;
}
RuntimeScalar s2 = registers[rs].scalar();
((RuntimeScalar) registers[rd]).set(s2);
return pc;
}
/**
* Execute logical OR assign operation.
* Format: LOGICAL_OR_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeLogicalOrAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar s1 = registers[rd].scalar();
if (s1.getBoolean()) {
return pc;
}
RuntimeScalar s2 = registers[rs].scalar();
((RuntimeScalar) registers[rd]).set(s2);
return pc;
}
public static int executeDefinedOrAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar s1 = registers[rd].scalar();
if (s1.getDefinedBoolean()) {
return pc;
}
RuntimeScalar s2 = registers[rs].scalar();
((RuntimeScalar) registers[rd]).set(s2);
return pc;
}
/**
* Execute string concatenation assign operation.
* Format: STRING_CONCAT_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeStringConcatAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar target = (RuntimeScalar) registers[rd];
RuntimeScalar result = StringOperators.stringConcat(
target,
(RuntimeScalar) registers[rs]
);
target.set(result);
// Invalidate pos() - any string modification should reset pos to undef
RuntimePosLvalue.invalidatePos(target);
return pc;
}
/**
* Execute bitwise AND assign operation.
* Format: BITWISE_AND_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeBitwiseAndAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar result = BitwiseOperators.bitwiseAnd(
(RuntimeScalar) registers[rd],
(RuntimeScalar) registers[rs]
);
((RuntimeScalar) registers[rd]).set(result);
return pc;
}
/**
* Execute bitwise OR assign operation.
* Format: BITWISE_OR_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeBitwiseOrAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar result = BitwiseOperators.bitwiseOr(
(RuntimeScalar) registers[rd],
(RuntimeScalar) registers[rs]
);
((RuntimeScalar) registers[rd]).set(result);
return pc;
}
/**
* Execute bitwise XOR assign operation.
* Format: BITWISE_XOR_ASSIGN rd rs
*
* @param bytecode The bytecode array
* @param pc Current program counter
* @param registers Register file
* @return Updated program counter
*/
public static int executeBitwiseXorAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar result = BitwiseOperators.bitwiseXor(
(RuntimeScalar) registers[rd],
(RuntimeScalar) registers[rs]
);
((RuntimeScalar) registers[rd]).set(result);
return pc;
}
public static int executeStringBitwiseAndAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar result = BitwiseOperators.bitwiseAndDot(
(RuntimeScalar) registers[rd],
(RuntimeScalar) registers[rs]
);
((RuntimeScalar) registers[rd]).set(result);
return pc;
}
public static int executeStringBitwiseOrAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar result = BitwiseOperators.bitwiseOrDot(
(RuntimeScalar) registers[rd],
(RuntimeScalar) registers[rs]
);
((RuntimeScalar) registers[rd]).set(result);
return pc;
}
public static int executeStringBitwiseXorAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
RuntimeScalar result = BitwiseOperators.bitwiseXorDot(
(RuntimeScalar) registers[rd],
(RuntimeScalar) registers[rs]
);
((RuntimeScalar) registers[rd]).set(result);
return pc;
}
// Bitwise binary operators (non-assignment)
/**
* Execute bitwise AND binary operation.
* Format: BITWISE_AND_BINARY rd rs1 rs2
*/
public static int executeBitwiseAndBinary(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
registers[rd] = BitwiseOperators.bitwiseAnd(
registers[rs1].scalar(),
registers[rs2].scalar()
);
return pc;
}
/**
* Execute bitwise OR binary operation.
* Format: BITWISE_OR_BINARY rd rs1 rs2
*/
public static int executeBitwiseOrBinary(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
registers[rd] = BitwiseOperators.bitwiseOr(
registers[rs1].scalar(),
registers[rs2].scalar()
);
return pc;
}
/**
* Execute bitwise XOR binary operation.
* Format: BITWISE_XOR_BINARY rd rs1 rs2
*/
public static int executeBitwiseXorBinary(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
registers[rd] = BitwiseOperators.bitwiseXor(
registers[rs1].scalar(),
registers[rs2].scalar()
);
return pc;
}
/**
* Execute string bitwise AND operation.
* Format: STRING_BITWISE_AND rd rs1 rs2
*/
public static int executeStringBitwiseAnd(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
registers[rd] = BitwiseOperators.bitwiseAndDot(
registers[rs1].scalar(),
registers[rs2].scalar()
);
return pc;
}
/**
* Execute string bitwise OR operation.
* Format: STRING_BITWISE_OR rd rs1 rs2
*/
public static int executeStringBitwiseOr(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
registers[rd] = BitwiseOperators.bitwiseOrDot(
registers[rs1].scalar(),
registers[rs2].scalar()
);
return pc;
}
/**
* Execute string bitwise XOR operation.
* Format: STRING_BITWISE_XOR rd rs1 rs2
*/
public static int executeStringBitwiseXor(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
registers[rd] = BitwiseOperators.bitwiseXorDot(
registers[rs1].scalar(),
registers[rs2].scalar()
);
return pc;
}
/**
* Execute bitwise NOT binary operation.
* Format: BITWISE_NOT_BINARY rd rs
*/
public static int executeBitwiseNotBinary(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = BitwiseOperators.bitwiseNotBinary(registers[rs].scalar());
return pc;
}
/**
* Execute bitwise NOT string operation.
* Format: BITWISE_NOT_STRING rd rs
*/
public static int executeBitwiseNotString(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = BitwiseOperators.bitwiseNotDot(registers[rs].scalar());
return pc;
}
/**
* Execute stat operation.
* Format: STAT rd rs ctx
*/
public static int executeStat(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
registers[rd] = Stat.stat((RuntimeScalar) registers[rs], ctx);
return pc;
}
/**
* Execute lstat operation.
* Format: LSTAT rd rs ctx
*/
public static int executeLstat(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
registers[rd] = Stat.lstat((RuntimeScalar) registers[rs], ctx);
return pc;
}
public static int executeStatLastHandle(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
registers[rd] = Stat.statLastHandle(ctx);
return pc;
}
public static int executeLstatLastHandle(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
registers[rd] = Stat.lstatLastHandle(ctx);
return pc;
}
/**
* Execute print operation.
* Format: PRINT contentReg filehandleReg
*/
public static int executePrint(int[] bytecode, int pc, RuntimeBase[] registers) {
int contentReg = bytecode[pc++];
int filehandleReg = bytecode[pc++];
Object val = registers[contentReg];
// Filehandle should be scalar - convert if needed
RuntimeBase fhBase = registers[filehandleReg];
RuntimeScalar fh = (fhBase instanceof RuntimeScalar)
? (RuntimeScalar) fhBase
: fhBase.scalar();
RuntimeList list;
if (val instanceof RuntimeList) {
list = (RuntimeList) val;
} else if (val instanceof RuntimeArray) {
// Convert RuntimeArray to RuntimeList
list = new RuntimeList();
for (RuntimeScalar elem : (RuntimeArray) val) {
list.add(elem);
}
} else if (val instanceof RuntimeScalar) {
// Convert scalar to single-element list
list = new RuntimeList();
list.add((RuntimeScalar) val);
} else {
list = new RuntimeList();
}
// Call IOOperator.print()
IOOperator.print(list, fh);
return pc;
}
/**
* Execute say operation.
* Format: SAY contentReg filehandleReg
*/
public static int executeSay(int[] bytecode, int pc, RuntimeBase[] registers) {
int contentReg = bytecode[pc++];
int filehandleReg = bytecode[pc++];
Object val = registers[contentReg];
// Filehandle should be scalar - convert if needed
RuntimeBase fhBase = registers[filehandleReg];
RuntimeScalar fh = (fhBase instanceof RuntimeScalar)
? (RuntimeScalar) fhBase
: fhBase.scalar();
RuntimeList list;
if (val instanceof RuntimeList) {
list = (RuntimeList) val;
} else if (val instanceof RuntimeArray) {
// Convert RuntimeArray to RuntimeList
list = new RuntimeList();
for (RuntimeScalar elem : (RuntimeArray) val) {
list.add(elem);
}
} else if (val instanceof RuntimeScalar) {
// Convert scalar to single-element list
list = new RuntimeList();
list.add((RuntimeScalar) val);
} else {
list = new RuntimeList();
}
// Call IOOperator.say()
IOOperator.say(list, fh);
return pc;
}
/**
* Execute chomp operation.
* Format: CHOMP rd rs
*/
public static int executeChomp(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = registers[rs].chomp();
return pc;
}
/**
* Execute wantarray operation.
* Format: WANTARRAY rd wantarrayReg
*/
public static int executeWantarray(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int wantarrayReg = bytecode[pc++];
int ctx = ((RuntimeScalar) registers[wantarrayReg]).getInt();
registers[rd] = Operator.wantarray(ctx);
return pc;
}
/**
* Execute require operation.
* Format: REQUIRE rd rs
*/
public static int executeRequire(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = ModuleOperators.require((RuntimeScalar) registers[rs]);
return pc;
}
/**
* Execute pos operation.
* Format: POS rd rs
*/
public static int executePos(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = ((RuntimeScalar) registers[rs]).pos();
return pc;
}
/**
* Execute index operation.
* Format: INDEX rd strReg substrReg posReg
*/
public static int executeIndex(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int strReg = bytecode[pc++];
int substrReg = bytecode[pc++];
int posReg = bytecode[pc++];
registers[rd] = StringOperators.index(
(RuntimeScalar) registers[strReg],
(RuntimeScalar) registers[substrReg],
(RuntimeScalar) registers[posReg]
);
return pc;
}
/**
* Execute rindex operation.
* Format: RINDEX rd strReg substrReg posReg
*/
public static int executeRindex(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int strReg = bytecode[pc++];
int substrReg = bytecode[pc++];
int posReg = bytecode[pc++];
registers[rd] = StringOperators.rindex(
(RuntimeScalar) registers[strReg],
(RuntimeScalar) registers[substrReg],
(RuntimeScalar) registers[posReg]
);
return pc;
}
/**
* Execute pre-increment operation.
* Format: PRE_AUTOINCREMENT rd
*/
public static int executePreAutoIncrement(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
((RuntimeScalar) registers[rd]).preAutoIncrement();
return pc;
}
/**
* Execute post-increment operation.
* Format: POST_AUTOINCREMENT rd rs
*/
public static int executePostAutoIncrement(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rs])) {
registers[rs] = BytecodeInterpreter.ensureMutableScalar(registers[rs]);
}
registers[rd] = ((RuntimeScalar) registers[rs]).postAutoIncrement();
return pc;
}
/**
* Execute pre-decrement operation.
* Format: PRE_AUTODECREMENT rd
*/
public static int executePreAutoDecrement(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {
registers[rd] = BytecodeInterpreter.ensureMutableScalar(registers[rd]);
}
((RuntimeScalar) registers[rd]).preAutoDecrement();
return pc;
}
/**
* Execute post-decrement operation.
* Format: POST_AUTODECREMENT rd rs
*/
public static int executePostAutoDecrement(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rs])) {
registers[rs] = BytecodeInterpreter.ensureMutableScalar(registers[rs]);
}
registers[rd] = ((RuntimeScalar) registers[rs]).postAutoDecrement();
return pc;
}
/**
* Execute open operation.
* Format: OPEN rd ctx argsReg
*/
public static int executeOpen(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
int argsReg = bytecode[pc++];
RuntimeArray argsArray = (RuntimeArray) registers[argsReg];
RuntimeBase[] argsVarargs = argsArray.elements.toArray(new RuntimeBase[0]);
registers[rd] = IOOperator.open(ctx, argsVarargs);
return pc;
}
/**
* Execute readline operation.
* Format: READLINE rd fhReg ctx
*/
public static int executeReadline(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int fhReg = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
RuntimeScalar fh = (RuntimeScalar) registers[fhReg];
// Diamond operator <> passes a plain string scalar (not a glob/IO).
// Route to DiamondIO.readline which manages @ARGV / STDIN iteration.
if (fh.getRuntimeIO() == null) {
registers[rd] = DiamondIO.readline(fh, ctx);
} else {
registers[rd] = Readline.readline(fh, ctx);
}
return pc;
}
/**
* Execute match regex operation.
* Format: MATCH_REGEX rd stringReg regexReg ctx
*/
public static int executeMatchRegex(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int stringReg = bytecode[pc++];
int regexReg = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
registers[rd] = RuntimeRegex.matchRegex(
(RuntimeScalar) registers[regexReg],
(RuntimeScalar) registers[stringReg],
ctx
);
return pc;
}
/**
* Execute negated match regex operation.
* Format: MATCH_REGEX_NOT rd stringReg regexReg ctx
*/
public static int executeMatchRegexNot(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int stringReg = bytecode[pc++];
int regexReg = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
RuntimeBase matchResult = RuntimeRegex.matchRegex(
(RuntimeScalar) registers[regexReg],
(RuntimeScalar) registers[stringReg],
ctx
);
// Negate the boolean result
registers[rd] = new RuntimeScalar(matchResult.scalar().getBoolean() ? 0 : 1);
return pc;
}
/**
* Execute create closure operation.
* Format: CREATE_CLOSURE rd template_idx num_captures reg1 reg2 ...
*/
public static int executeCreateClosure(int[] bytecode, int pc, RuntimeBase[] registers, InterpretedCode code) {
int rd = bytecode[pc++];
int templateIdx = bytecode[pc++];
int numCaptures = bytecode[pc++];
// Get the template InterpretedCode from constants
InterpretedCode template = (InterpretedCode) code.constants[templateIdx];
// Capture the current register values
RuntimeBase[] capturedVars = new RuntimeBase[numCaptures];
for (int i = 0; i < numCaptures; i++) {
int captureReg = bytecode[pc++];
capturedVars[i] = registers[captureReg];
}
// Create a new InterpretedCode with the captured variables
InterpretedCode closureCode = template.withCapturedVars(capturedVars);
// Wrap in RuntimeScalar and set __SUB__ for self-reference
RuntimeScalar codeRef = new RuntimeScalar(closureCode);
closureCode.__SUB__ = codeRef;
registers[rd] = codeRef;
return pc;
}
/**
* Execute iterator create operation.
* Format: ITERATOR_CREATE rd rs
*/
public static int executeIteratorCreate(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeBase iterable = registers[rs];
java.util.Iterator<RuntimeScalar> iterator = iterable.iterator();
// Store iterator as a constant (preserve the Iterator object)
// Wrap in RuntimeScalar for storage
registers[rd] = new RuntimeScalar(iterator);
return pc;
}
/**
* Execute iterator has next operation.
* Format: ITERATOR_HAS_NEXT rd iterReg
*/
public static int executeIteratorHasNext(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int iterReg = bytecode[pc++];
RuntimeScalar iterScalar = (RuntimeScalar) registers[iterReg];
@SuppressWarnings("unchecked")
java.util.Iterator<RuntimeScalar> iterator =
(java.util.Iterator<RuntimeScalar>) iterScalar.value;
boolean hasNext = iterator.hasNext();
registers[rd] = hasNext ? RuntimeScalarCache.scalarTrue : RuntimeScalarCache.scalarFalse;
return pc;
}
/**
* Execute iterator next operation.
* Format: ITERATOR_NEXT rd iterReg
*/
public static int executeIteratorNext(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int iterReg = bytecode[pc++];
RuntimeScalar iterScalar = (RuntimeScalar) registers[iterReg];
@SuppressWarnings("unchecked")
java.util.Iterator<RuntimeScalar> iterator =
(java.util.Iterator<RuntimeScalar>) iterScalar.value;
RuntimeScalar next = iterator.next();
registers[rd] = BytecodeInterpreter.isImmutableProxy(next) ? BytecodeInterpreter.ensureMutableScalar(next) : next;
return pc;
}
/**
* Execute subtract assign operation.
* Format: SUBTRACT_ASSIGN rd rs
*/
public static int executeSubtractAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeBase val1 = registers[rd];
if (BytecodeInterpreter.isImmutableProxy(val1)) {
val1 = BytecodeInterpreter.ensureMutableScalar(val1);
registers[rd] = val1;
}
RuntimeBase val2 = registers[rs];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
// Note: -= does NOT warn for uninitialized values in Perl
registers[rd] = MathOperators.subtractAssign(s1, s2);
return pc;
}
/**
* Execute multiply assign operation.
* Format: MULTIPLY_ASSIGN rd rs
*/
public static int executeMultiplyAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeBase val1 = registers[rd];
if (BytecodeInterpreter.isImmutableProxy(val1)) {
val1 = BytecodeInterpreter.ensureMutableScalar(val1);
registers[rd] = val1;
}
RuntimeBase val2 = registers[rs];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
registers[rd] = MathOperators.multiplyAssignWarn(s1, s2);
return pc;
}
/**
* Execute divide assign operation.
* Format: DIVIDE_ASSIGN rd rs
*/
public static int executeDivideAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
if (BytecodeInterpreter.isImmutableProxy(registers[rd])) {