-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInlineOpcodeHandler.java
More file actions
1483 lines (1319 loc) · 57.8 KB
/
InlineOpcodeHandler.java
File metadata and controls
1483 lines (1319 loc) · 57.8 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.runtimetypes.*;
/**
* Inline opcode handlers for arithmetic, shift, collection, and list operations.
* <p>
* Extracted from BytecodeInterpreter.execute() to reduce method size
* and keep it under the 8KB JIT compilation limit.
* <p>
* Handles: arithmetic ops, shift ops, integer assign ops, array/hash operations,
* and list operations (CREATE_LIST, JOIN, SELECT, RANGE, MAP, GREP, SORT, etc.)
*/
public class InlineOpcodeHandler {
// =========================================================================
// Helper methods (duplicated from BytecodeInterpreter)
// =========================================================================
/**
* Check if a value is an immutable proxy (RuntimeScalarReadOnly or ScalarSpecialVariable).
* These cannot be mutated in place.
*/
static boolean isImmutableProxy(RuntimeBase val) {
if (val instanceof ReadOnlyAlias) return false;
return val instanceof RuntimeScalarReadOnly || val instanceof ScalarSpecialVariable;
}
/**
* Create a mutable copy of a value if it is an immutable proxy.
* For RuntimeScalarReadOnly: copies type and value into a fresh RuntimeScalar.
* For ScalarSpecialVariable: resolves via getValueAsScalar(), then copies.
* For anything else: casts directly to RuntimeScalar.
*/
static RuntimeScalar ensureMutableScalar(RuntimeBase val) {
if (val instanceof RuntimeScalarReadOnly ro) {
RuntimeScalar copy = new RuntimeScalar();
copy.type = ro.type;
copy.value = ro.value;
return copy;
}
if (val instanceof ScalarSpecialVariable sv) {
RuntimeScalar src = sv.getValueAsScalar();
RuntimeScalar copy = new RuntimeScalar();
copy.type = src.type;
copy.value = src.value;
return copy;
}
return (RuntimeScalar) val;
}
// =========================================================================
// ARITHMETIC OPERATORS
// =========================================================================
/**
* Addition: rd = rs1 + rs2
* Format: ADD_SCALAR rd rs1 rs2
*/
public static int executeAddScalar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase val1 = registers[rs1];
RuntimeBase val2 = registers[rs2];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
registers[rd] = MathOperators.add(s1, s2);
return pc;
}
/**
* Subtraction: rd = rs1 - rs2
* Format: SUB_SCALAR rd rs1 rs2
*/
public static int executeSubScalar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase val1 = registers[rs1];
RuntimeBase val2 = registers[rs2];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
registers[rd] = MathOperators.subtract(s1, s2);
return pc;
}
/**
* Multiplication: rd = rs1 * rs2
* Format: MUL_SCALAR rd rs1 rs2
*/
public static int executeMulScalar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase val1 = registers[rs1];
RuntimeBase val2 = registers[rs2];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
registers[rd] = MathOperators.multiply(s1, s2);
return pc;
}
/**
* Division: rd = rs1 / rs2
* Format: DIV_SCALAR rd rs1 rs2
*/
public static int executeDivScalar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase val1 = registers[rs1];
RuntimeBase val2 = registers[rs2];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
registers[rd] = MathOperators.divide(s1, s2);
return pc;
}
/**
* Modulus: rd = rs1 % rs2
* Format: MOD_SCALAR rd rs1 rs2
*/
public static int executeModScalar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase val1 = registers[rs1];
RuntimeBase val2 = registers[rs2];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
registers[rd] = MathOperators.modulus(s1, s2);
return pc;
}
/**
* Exponentiation: rd = rs1 ** rs2
* Format: POW_SCALAR rd rs1 rs2
*/
public static int executePowScalar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase val1 = registers[rs1];
RuntimeBase val2 = registers[rs2];
RuntimeScalar s1 = (val1 instanceof RuntimeScalar) ? (RuntimeScalar) val1 : val1.scalar();
RuntimeScalar s2 = (val2 instanceof RuntimeScalar) ? (RuntimeScalar) val2 : val2.scalar();
registers[rd] = MathOperators.pow(s1, s2);
return pc;
}
/**
* Negation: rd = -rs
* Format: NEG_SCALAR rd rs
*/
public static int executeNegScalar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = MathOperators.unaryMinus((RuntimeScalar) registers[rs]);
return pc;
}
/**
* Arithmetic without overload dispatch.
* Used when {@code no overloading} is in effect at compile time.
* Format: OPCODE rd rs1 rs2
*/
public static int executeAddNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase v1 = registers[rs1];
RuntimeBase v2 = registers[rs2];
RuntimeScalar s1 = (v1 instanceof RuntimeScalar) ? (RuntimeScalar) v1 : v1.scalar();
RuntimeScalar s2 = (v2 instanceof RuntimeScalar) ? (RuntimeScalar) v2 : v2.scalar();
registers[rd] = MathOperators.addNoOverload(s1, s2);
return pc;
}
public static int executeSubNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase v1 = registers[rs1];
RuntimeBase v2 = registers[rs2];
RuntimeScalar s1 = (v1 instanceof RuntimeScalar) ? (RuntimeScalar) v1 : v1.scalar();
RuntimeScalar s2 = (v2 instanceof RuntimeScalar) ? (RuntimeScalar) v2 : v2.scalar();
registers[rd] = MathOperators.subtractNoOverload(s1, s2);
return pc;
}
public static int executeMulNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase v1 = registers[rs1];
RuntimeBase v2 = registers[rs2];
RuntimeScalar s1 = (v1 instanceof RuntimeScalar) ? (RuntimeScalar) v1 : v1.scalar();
RuntimeScalar s2 = (v2 instanceof RuntimeScalar) ? (RuntimeScalar) v2 : v2.scalar();
registers[rd] = MathOperators.multiplyNoOverload(s1, s2);
return pc;
}
public static int executeDivNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase v1 = registers[rs1];
RuntimeBase v2 = registers[rs2];
RuntimeScalar s1 = (v1 instanceof RuntimeScalar) ? (RuntimeScalar) v1 : v1.scalar();
RuntimeScalar s2 = (v2 instanceof RuntimeScalar) ? (RuntimeScalar) v2 : v2.scalar();
registers[rd] = MathOperators.divideNoOverload(s1, s2);
return pc;
}
public static int executeModNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase v1 = registers[rs1];
RuntimeBase v2 = registers[rs2];
RuntimeScalar s1 = (v1 instanceof RuntimeScalar) ? (RuntimeScalar) v1 : v1.scalar();
RuntimeScalar s2 = (v2 instanceof RuntimeScalar) ? (RuntimeScalar) v2 : v2.scalar();
registers[rd] = MathOperators.modulusNoOverload(s1, s2);
return pc;
}
public static int executePowNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase v1 = registers[rs1];
RuntimeBase v2 = registers[rs2];
RuntimeScalar s1 = (v1 instanceof RuntimeScalar) ? (RuntimeScalar) v1 : v1.scalar();
RuntimeScalar s2 = (v2 instanceof RuntimeScalar) ? (RuntimeScalar) v2 : v2.scalar();
registers[rd] = MathOperators.powNoOverload(s1, s2);
return pc;
}
public static int executeNegNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = MathOperators.unaryMinusNoOverload((RuntimeScalar) registers[rs]);
return pc;
}
/**
* Addition with immediate: rd = rs + immediate
* Format: ADD_SCALAR_INT rd rs immediate
*/
public static int executeAddScalarInt(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
int immediate = bytecode[pc];
pc += 1;
registers[rd] = MathOperators.add(
(RuntimeScalar) registers[rs],
immediate
);
return pc;
}
/**
* String concatenation: rd = rs1 . rs2
* Format: CONCAT rd rs1 rs2
*/
public static int executeConcat(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase concatLeft = registers[rs1];
RuntimeBase concatRight = registers[rs2];
registers[rd] = StringOperators.stringConcat(
concatLeft instanceof RuntimeScalar ? (RuntimeScalar) concatLeft : concatLeft.scalar(),
concatRight instanceof RuntimeScalar ? (RuntimeScalar) concatRight : concatRight.scalar()
);
return pc;
}
/**
* String/list repetition: rd = rs1 x rs2
* Format: REPEAT rd rs1 rs2
*/
public static int executeRepeat(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase countVal = registers[rs2];
RuntimeScalar count = (countVal instanceof RuntimeScalar)
? (RuntimeScalar) countVal
: countVal.scalar();
int repeatCtx = (registers[rs1] instanceof RuntimeScalar)
? RuntimeContextType.SCALAR : RuntimeContextType.LIST;
registers[rd] = Operator.repeat(registers[rs1], count, repeatCtx);
return pc;
}
/**
* String length: rd = length(rs)
* Format: LENGTH rd rs
*/
public static int executeLength(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = StringOperators.length((RuntimeScalar) registers[rs]);
return pc;
}
// =========================================================================
// SHIFT OPERATIONS
// =========================================================================
/**
* Left shift: rd = rs1 << rs2
* Format: LEFT_SHIFT rd rs1 rs2
*/
public static int executeLeftShift(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeScalar s1 = (RuntimeScalar) registers[rs1];
RuntimeScalar s2 = (RuntimeScalar) registers[rs2];
registers[rd] = BitwiseOperators.shiftLeft(s1, s2);
return pc;
}
/**
* Right shift: rd = rs1 >> rs2
* Format: RIGHT_SHIFT rd rs1 rs2
*/
public static int executeRightShift(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeScalar s1 = (RuntimeScalar) registers[rs1];
RuntimeScalar s2 = (RuntimeScalar) registers[rs2];
registers[rd] = BitwiseOperators.shiftRight(s1, s2);
return pc;
}
/**
* Integer left shift: rd = rs1 << rs2 (integer semantics)
* Format: INTEGER_LEFT_SHIFT rd rs1 rs2
*/
public static int executeIntegerLeftShift(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeScalar s1 = (registers[rs1] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs1] : registers[rs1].scalar();
RuntimeScalar s2 = (registers[rs2] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs2] : registers[rs2].scalar();
registers[rd] = BitwiseOperators.integerShiftLeft(s1, s2);
return pc;
}
/**
* Integer right shift: rd = rs1 >> rs2 (integer semantics)
* Format: INTEGER_RIGHT_SHIFT rd rs1 rs2
*/
public static int executeIntegerRightShift(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeScalar s1 = (registers[rs1] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs1] : registers[rs1].scalar();
RuntimeScalar s2 = (registers[rs2] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs2] : registers[rs2].scalar();
registers[rd] = BitwiseOperators.integerShiftRight(s1, s2);
return pc;
}
/**
* Integer division: rd = rs1 / rs2 (integer semantics)
* Format: INTEGER_DIV rd rs1 rs2
*/
public static int executeIntegerDiv(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeScalar s1 = (registers[rs1] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs1] : registers[rs1].scalar();
RuntimeScalar s2 = (registers[rs2] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs2] : registers[rs2].scalar();
registers[rd] = MathOperators.integerDivide(s1, s2);
return pc;
}
/**
* Integer modulus: rd = rs1 % rs2 (integer semantics)
* Format: INTEGER_MOD rd rs1 rs2
*/
public static int executeIntegerMod(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeScalar s1 = (registers[rs1] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs1] : registers[rs1].scalar();
RuntimeScalar s2 = (registers[rs2] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs2] : registers[rs2].scalar();
registers[rd] = MathOperators.integerModulus(s1, s2);
return pc;
}
// =========================================================================
// INTEGER ASSIGN OPERATIONS
// =========================================================================
/**
* Integer left shift assign: rd <<= rs (integer semantics, in-place)
* Format: INTEGER_LEFT_SHIFT_ASSIGN rd rs
*/
public static int executeIntegerLeftShiftAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeScalar s1 = (RuntimeScalar) registers[rd];
s1.set(BitwiseOperators.integerShiftLeft(s1, (RuntimeScalar) registers[rs]));
return pc;
}
/**
* Integer right shift assign: rd >>= rs (integer semantics, in-place)
* Format: INTEGER_RIGHT_SHIFT_ASSIGN rd rs
*/
public static int executeIntegerRightShiftAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeScalar s1 = (RuntimeScalar) registers[rd];
s1.set(BitwiseOperators.integerShiftRight(s1, (RuntimeScalar) registers[rs]));
return pc;
}
/**
* Integer division assign: rd /= rs (integer semantics, in-place)
* Format: INTEGER_DIV_ASSIGN rd rs
*/
public static int executeIntegerDivAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeScalar s1 = (RuntimeScalar) registers[rd];
RuntimeScalar s2 = (registers[rs] instanceof RuntimeScalar) ? (RuntimeScalar) registers[rs] : registers[rs].scalar();
registers[rd] = MathOperators.integerDivideAssignWarn(s1, s2);
return pc;
}
/**
* Integer modulus assign: rd %= rs (integer semantics, in-place)
* Format: INTEGER_MOD_ASSIGN rd rs
*/
public static int executeIntegerModAssign(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeScalar s1 = (RuntimeScalar) registers[rd];
s1.set(MathOperators.integerModulus(s1, (RuntimeScalar) registers[rs]));
return pc;
}
// =========================================================================
// ARRAY OPERATIONS
// =========================================================================
/**
* Array element access: rd = array[index]
* Format: ARRAY_GET rd arrayReg indexReg
*/
public static int executeArrayGet(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
int indexReg = bytecode[pc++];
RuntimeBase arrayBase = registers[arrayReg];
RuntimeScalar idx = (RuntimeScalar) registers[indexReg];
if (arrayBase instanceof RuntimeArray arr) {
registers[rd] = arr.get(idx.getInt());
} else if (arrayBase instanceof RuntimeList list) {
int index = idx.getInt();
if (index < 0) index = list.elements.size() + index;
registers[rd] = (index >= 0 && index < list.elements.size())
? list.elements.get(index)
: new RuntimeScalar();
} else {
throw new RuntimeException("ARRAY_GET: register " + arrayReg + " contains " +
(arrayBase == null ? "null" : arrayBase.getClass().getName()) +
" instead of RuntimeArray or RuntimeList");
}
return pc;
}
/**
* Array element store: array[index] = value, returns the lvalue (element)
* Format: ARRAY_SET rd arrayReg indexReg valueReg
*/
public static int executeArraySet(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
int indexReg = bytecode[pc++];
int valueReg = bytecode[pc++];
RuntimeArray arr = (RuntimeArray) registers[arrayReg];
RuntimeScalar idx = (RuntimeScalar) registers[indexReg];
RuntimeBase valueBase = registers[valueReg];
RuntimeScalar val = (valueBase instanceof RuntimeScalar)
? (RuntimeScalar) valueBase : valueBase.scalar();
RuntimeScalar element = arr.get(idx.getInt());
element.set(val);
registers[rd] = element;
return pc;
}
/**
* Array push: push(@array, value)
* Format: ARRAY_PUSH arrayReg valueReg
*/
public static int executeArrayPush(int[] bytecode, int pc, RuntimeBase[] registers) {
int arrayReg = bytecode[pc++];
int valueReg = bytecode[pc++];
RuntimeArray arr = getArrayFromRegister(registers, arrayReg);
RuntimeBase val = registers[valueReg];
arr.push(val);
return pc;
}
/**
* Helper to get RuntimeArray from a register, handling RuntimeList conversion.
*/
private static RuntimeArray getArrayFromRegister(RuntimeBase[] registers, int arrayReg) {
RuntimeBase arrayBase = registers[arrayReg];
if (arrayBase instanceof RuntimeArray) {
return (RuntimeArray) arrayBase;
} else if (arrayBase instanceof RuntimeList) {
// Convert RuntimeList to RuntimeArray (defensive handling)
RuntimeArray arr = new RuntimeArray();
arrayBase.addToArray(arr);
registers[arrayReg] = arr;
return arr;
} else {
// Fallback: try to get as array via dereference
RuntimeArray arr = arrayBase.scalar().arrayDeref();
registers[arrayReg] = arr;
return arr;
}
}
/**
* Array pop: rd = pop(@array)
* Format: ARRAY_POP rd arrayReg
*/
public static int executeArrayPop(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
RuntimeArray arr = getArrayFromRegister(registers, arrayReg);
registers[rd] = RuntimeArray.pop(arr);
return pc;
}
/**
* Array shift: rd = shift(@array)
* Format: ARRAY_SHIFT rd arrayReg
*/
public static int executeArrayShift(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
RuntimeArray arr = getArrayFromRegister(registers, arrayReg);
registers[rd] = RuntimeArray.shift(arr);
return pc;
}
/**
* Array unshift: unshift(@array, value)
* Format: ARRAY_UNSHIFT arrayReg valueReg
*/
public static int executeArrayUnshift(int[] bytecode, int pc, RuntimeBase[] registers) {
int arrayReg = bytecode[pc++];
int valueReg = bytecode[pc++];
RuntimeArray arr = getArrayFromRegister(registers, arrayReg);
RuntimeBase val = registers[valueReg];
RuntimeArray.unshift(arr, val);
return pc;
}
/**
* Scalar conversion: rd = operand.scalar()
* Converts arrays to count, lists to last element, scalars to self.
* Format: ARRAY_SIZE rd operandReg
*/
public static int executeArraySize(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int operandReg = bytecode[pc++];
RuntimeBase operand = registers[operandReg];
registers[rd] = operand.scalar();
return pc;
}
/**
* Set array last index: $#array = value
* Format: SET_ARRAY_LAST_INDEX arrayReg valueReg
*/
public static int executeSetArrayLastIndex(int[] bytecode, int pc, RuntimeBase[] registers) {
int arrayReg = bytecode[pc++];
int valueReg = bytecode[pc++];
RuntimeArray.indexLastElem((RuntimeArray) registers[arrayReg])
.set(((RuntimeScalar) registers[valueReg]));
return pc;
}
/**
* Create array reference from list: rd = new RuntimeArray(rs_list).createReference()
* Array literals always return references in Perl.
* Format: CREATE_ARRAY rd listReg
*/
public static int executeCreateArray(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int listReg = bytecode[pc++];
RuntimeBase source = registers[listReg];
RuntimeArray array;
if (source instanceof RuntimeArray) {
array = (RuntimeArray) source;
} else {
RuntimeList list = source.getList();
array = new RuntimeArray(list);
}
registers[rd] = array.createReferenceWithTrackedElements();
return pc;
}
// =========================================================================
/**
* Hash element access: rd = hash{key}
* Format: HASH_GET rd hashReg keyReg
*/
public static int executeHashGet(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
int keyReg = bytecode[pc++];
RuntimeHash hash = (RuntimeHash) registers[hashReg];
RuntimeScalar key = (RuntimeScalar) registers[keyReg];
registers[rd] = hash.get(key);
return pc;
}
/**
* Hash element store: hash{key} = value, returns the lvalue (element)
* Creates a fresh copy to prevent aliasing bugs.
* Uses addToScalar to resolve special variables ($1, $2, etc.)
* Format: HASH_SET rd hashReg keyReg valueReg
*/
public static int executeHashSet(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
int keyReg = bytecode[pc++];
int valueReg = bytecode[pc++];
RuntimeHash hash = (RuntimeHash) registers[hashReg];
RuntimeScalar key = (RuntimeScalar) registers[keyReg];
RuntimeBase valBase = registers[valueReg];
RuntimeScalar val = (valBase instanceof RuntimeScalar) ? (RuntimeScalar) valBase : valBase.scalar();
RuntimeScalar copy = new RuntimeScalar();
val.addToScalar(copy);
hash.put(key.toString(), copy);
registers[rd] = copy;
return pc;
}
/**
* Check if hash key exists: rd = exists $hash{key}
* Format: HASH_EXISTS rd hashReg keyReg
*/
public static int executeHashExists(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
int keyReg = bytecode[pc++];
RuntimeHash hash = (RuntimeHash) registers[hashReg];
RuntimeScalar key = (RuntimeScalar) registers[keyReg];
registers[rd] = hash.exists(key);
return pc;
}
/**
* Delete hash key: rd = delete $hash{key}
* Format: HASH_DELETE rd hashReg keyReg
*/
public static int executeHashDelete(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
int keyReg = bytecode[pc++];
RuntimeHash hash = (RuntimeHash) registers[hashReg];
RuntimeScalar key = (RuntimeScalar) registers[keyReg];
registers[rd] = hash.delete(key);
return pc;
}
/**
* Check if array index exists: rd = exists $array[index]
* Format: ARRAY_EXISTS rd arrayReg indexReg
*/
public static int executeArrayExists(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
int indexReg = bytecode[pc++];
RuntimeArray array = (RuntimeArray) registers[arrayReg];
RuntimeScalar index = (RuntimeScalar) registers[indexReg];
registers[rd] = array.exists(index);
return pc;
}
/**
* Delete array element: rd = delete $array[index]
* Format: ARRAY_DELETE rd arrayReg indexReg
*/
public static int executeArrayDelete(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
int indexReg = bytecode[pc++];
RuntimeArray array = (RuntimeArray) registers[arrayReg];
RuntimeScalar index = (RuntimeScalar) registers[indexReg];
registers[rd] = array.delete(index);
return pc;
}
/**
* Delete local hash key: rd = delete local $hash{key}
* Format: HASH_DELETE_LOCAL rd hashReg keyReg
*/
public static int executeHashDeleteLocal(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
int keyReg = bytecode[pc++];
RuntimeHash hash = (RuntimeHash) registers[hashReg];
RuntimeScalar key = (RuntimeScalar) registers[keyReg];
registers[rd] = hash.deleteLocal(key);
return pc;
}
/**
* Delete local array element: rd = delete local $array[index]
* Format: ARRAY_DELETE_LOCAL rd arrayReg indexReg
*/
public static int executeArrayDeleteLocal(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
int indexReg = bytecode[pc++];
RuntimeArray array = (RuntimeArray) registers[arrayReg];
RuntimeScalar index = (RuntimeScalar) registers[indexReg];
registers[rd] = array.deleteLocal(index);
return pc;
}
/**
* Get hash keys: rd = keys %hash
* Calls .keys() on RuntimeBase for proper error handling on non-hash types.
* Format: HASH_KEYS rd hashReg
*/
public static int executeHashKeys(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
registers[rd] = registers[hashReg].keys();
return pc;
}
/**
* Get hash values: rd = values %hash
* Calls .values() on RuntimeBase for proper error handling on non-hash types.
* Format: HASH_VALUES rd hashReg
*/
public static int executeHashValues(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
registers[rd] = registers[hashReg].values();
return pc;
}
// =========================================================================
// LIST OPERATIONS
// =========================================================================
/**
* Count the total flattened elements of a list/array/hash/scalar.
* Uses countElements() for recursive flattening (hashes contribute 2*keys elements).
* Format: LIST_TO_COUNT rd rs
*/
public static int executeListToCount(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeBase val = registers[rs];
registers[rd] = new RuntimeScalar(val.countElements());
return pc;
}
/**
* Convert list to scalar context: returns last element (Perl list-in-scalar semantics).
* Format: LIST_TO_SCALAR rd rs
*/
public static int executeListToScalar(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = registers[rs].scalar();
return pc;
}
/**
* Convert value to RuntimeList, preserving aggregate types (PerlRange, RuntimeArray)
* so that consumers like Pack.pack() can iterate them via RuntimeList's iterator.
* Format: SCALAR_TO_LIST rd rs
*/
public static int executeScalarToList(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
RuntimeBase val = registers[rs];
if (val instanceof RuntimeList) {
registers[rd] = val;
} else if (val instanceof RuntimeScalar) {
RuntimeList list = new RuntimeList();
list.elements.add(val);
registers[rd] = list;
} else {
// RuntimeArray, PerlRange, etc. - wrap in list, preserving type
RuntimeList list = new RuntimeList();
list.elements.add(val);
registers[rd] = list;
}
return pc;
}
/**
* Create RuntimeList from registers.
* Format: CREATE_LIST rd count rs1 rs2 ... rsN
*/
public static int executeCreateList(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int count = bytecode[pc++];
if (count == 0) {
// Empty list - fastest path
registers[rd] = new RuntimeList();
} else if (count == 1) {
// Single element - avoid loop overhead
int rs = bytecode[pc++];
RuntimeList list = new RuntimeList();
list.add(registers[rs]);
registers[rd] = list;
} else {
// Multiple elements - preallocate and populate
RuntimeList list = new RuntimeList();
for (int i = 0; i < count; i++) {
int rs = bytecode[pc++];
list.add(registers[rs]);
}
registers[rd] = list;
}
return pc;
}
/**
* Convert array/hash to scalar if wantarray indicates scalar context.
* Format: SCALAR_IF_WANTARRAY rd rs wantarray_reg
* This mirrors the JVM backend's emitRuntimeContextConversion() exactly.
*/
public static int executeScalarIfWantarray(int[] bytecode, int pc, RuntimeBase[] registers, int callContext) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
// wantarray_reg is not used - we use callContext directly (same as JVM's ILOAD 2)
pc++; // Skip wantarray_reg operand
RuntimeBase val = registers[rs];
// If scalar context and value is array or hash, call .scalar()
if (callContext == RuntimeContextType.SCALAR &&
(val instanceof RuntimeArray || val instanceof RuntimeHash)) {
registers[rd] = val.scalar();
} else {
registers[rd] = val;
}
return pc;
}
/**
* String join: rd = join(separator, list)
* Format: JOIN rd separatorReg listReg
*/
public static int executeJoin(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int separatorReg = bytecode[pc++];
int listReg = bytecode[pc++];
RuntimeBase separatorBase = registers[separatorReg];
RuntimeScalar separator = (separatorBase instanceof RuntimeScalar)
? (RuntimeScalar) separatorBase
: separatorBase.scalar();
RuntimeBase list = registers[listReg];
registers[rd] = StringOperators.joinForInterpolation(separator, list);
return pc;
}
/**
* String join without overload dispatch: rd = joinNoOverload(separator, list)
* Format: JOIN_NO_OVERLOAD rd separatorReg listReg
* Used when 'no overloading' is in effect at compile time.
*/
public static int executeJoinNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int separatorReg = bytecode[pc++];
int listReg = bytecode[pc++];
RuntimeBase separatorBase = registers[separatorReg];
RuntimeScalar separator = (separatorBase instanceof RuntimeScalar)
? (RuntimeScalar) separatorBase
: separatorBase.scalar();
RuntimeBase list = registers[listReg];
registers[rd] = StringOperators.joinNoOverload(separator, list);
return pc;
}
/**
* String concatenation without overload dispatch: rd = stringConcatNoOverload(rs1, rs2)
* Format: CONCAT_NO_OVERLOAD rd rs1 rs2
* Used when 'no overloading' is in effect at compile time.
*/
public static int executeConcatNoOverload(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs1 = bytecode[pc++];
int rs2 = bytecode[pc++];
RuntimeBase concatLeft = registers[rs1];
RuntimeBase concatRight = registers[rs2];
registers[rd] = StringOperators.stringConcatNoOverload(
concatLeft instanceof RuntimeScalar ? (RuntimeScalar) concatLeft : concatLeft.scalar(),
concatRight instanceof RuntimeScalar ? (RuntimeScalar) concatRight : concatRight.scalar()
);
return pc;
}
/**
* Select default output filehandle: rd = IOOperator.select(list, SCALAR)
* Format: SELECT rd listReg
*/
public static int executeSelect(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int listReg = bytecode[pc++];
RuntimeBase listBase = registers[listReg];
RuntimeList list = (listBase instanceof RuntimeList rl)
? rl : listBase.getList();
RuntimeScalar result = IOOperator.select(list, RuntimeContextType.SCALAR);
registers[rd] = result;
return pc;
}
/**
* Create range: rd = PerlRange.createRange(rs_start, rs_end)
* Format: RANGE rd startReg endReg
*/
public static int executeRange(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int startReg = bytecode[pc++];
int endReg = bytecode[pc++];
RuntimeBase startBase = registers[startReg];
RuntimeBase endBase = registers[endReg];
RuntimeScalar start = (startBase instanceof RuntimeScalar) ? (RuntimeScalar) startBase :
(startBase == null) ? new RuntimeScalar() : startBase.scalar();
RuntimeScalar end = (endBase instanceof RuntimeScalar) ? (RuntimeScalar) endBase :
(endBase == null) ? new RuntimeScalar() : endBase.scalar();
PerlRange range = PerlRange.createRange(start, end);
registers[rd] = range;
return pc;
}
/**
* Create hash reference from list: rd = RuntimeHash.createHash(rs_list).createReference()
* Hash literals always return references in Perl.
* Format: CREATE_HASH rd listReg
*/
public static int executeCreateHash(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int listReg = bytecode[pc++];
RuntimeBase list = registers[listReg];
RuntimeHash hash = RuntimeHash.createHash(list);
registers[rd] = hash.createReferenceWithTrackedElements();
return pc;
}
/**
* Random number: rd = Random.rand(max)
* Format: RAND rd maxReg