-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSlowOpcodeHandler.java
More file actions
1372 lines (1150 loc) · 45.8 KB
/
SlowOpcodeHandler.java
File metadata and controls
1372 lines (1150 loc) · 45.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.*;
import java.util.Map;
/**
* Handler for rarely-used operations called directly by BytecodeInterpreter.
*
* <p>This class provides execution methods for "cold path" operations that are
* called directly from BytecodeInterpreter helper methods (executeSliceOps,
* executeListOps, executeSystemOps, executeMiscOps).</p>
*
* <h2>Architecture (Phase 5)</h2>
* <p>As of Phase 5, this class uses <strong>direct method calls</strong> instead of
* the old SLOWOP_* ID dispatch mechanism:</p>
*
* <pre>
* BytecodeInterpreter:
* switch (opcode) {
* case Opcodes.DEREF_ARRAY:
* return SlowOpcodeHandler.executeDerefArray(bytecode, pc, registers);
* case Opcodes.ARRAY_SLICE:
* return SlowOpcodeHandler.executeArraySlice(bytecode, pc, registers);
* ...
* }
* </pre>
*
* <h2>Benefits of Direct Calls</h2>
* <ul>
* <li>Eliminates SLOWOP_* ID indirection (no ID lookup overhead)</li>
* <li>Simpler architecture: direct method calls instead of dispatch table</li>
* <li>Better JIT optimization: methods can be inlined by C2 compiler</li>
* <li>Clearer code: obvious which method handles each opcode</li>
* </ul>
*
* <h2>Performance Characteristics</h2>
* <p>Each operation adds one static method call (~5ns overhead) compared to
* inline handling in main execute() method. This is acceptable since these
* operations are rarely used (typically <1% of execution time).</p>
*
* <h2>Adding New Operations</h2>
* <ol>
* <li>Add opcode constant in Opcodes.java (next available opcode)</li>
* <li>Add public static executeXxx() method in this class</li>
* <li>Add case in BytecodeInterpreter helper method to call executeXxx()</li>
* <li>Add disassembly case in InterpretedCode.java</li>
* <li>Add emission logic in BytecodeCompiler.java</li>
* </ol>
*
* @see BytecodeInterpreter
* @see Opcodes
*/
public class SlowOpcodeHandler {
private static final boolean EVAL_TRACE =
System.getenv("JPERL_EVAL_TRACE") != null;
private SlowOpcodeHandler() {
// Utility class - no instantiation
}
// =================================================================
// SLICE AND DEREFERENCE OPERATIONS
// =================================================================
// =================================================================
private static void evalTrace(String msg) {
if (EVAL_TRACE) {
System.err.println("[eval-trace] " + msg);
}
}
/**
* SLOW_GETPPID: rd = getppid()
* Format: [SLOW_GETPPID] [rd]
* Effect: Gets parent process ID
*/
public static int executeGetppid(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
// Java 9+ has ProcessHandle.current().parent()
// For now, return 1 (init process)
registers[rd] = new RuntimeScalar(1);
return pc;
}
/**
* HASH_KEYVALUE_SLICE: rd = hash.getKeyValueSlice(keys_list)
* Format: [HASH_KEYVALUE_SLICE] [rd] [hashReg] [keysListReg]
* Effect: Returns alternating key/value pairs for the given keys.
*/
public static int executeHashKeyValueSlice(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
int keysListReg = bytecode[pc++];
RuntimeHash hash = (RuntimeHash) registers[hashReg];
RuntimeList keysList = (RuntimeList) registers[keysListReg];
registers[rd] = hash.getKeyValueSlice(keysList);
return pc;
}
/**
* SLOW_FORK: rd = fork()
* Format: [SLOW_FORK] [rd]
* Effect: Forks process (not supported in Java)
*/
public static int executeFork(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
// fork() is not supported in Java - return -1 (error)
// Real implementation would need JNI or native library
registers[rd] = new RuntimeScalar(-1);
GlobalVariable.getGlobalVariable("main::!").set("fork not supported on this platform");
return pc;
}
/**
* SLOW_SEMGET: rd = semget(key, nsems, flags)
* Format: [SLOW_SEMGET] [rd] [rs_key] [rs_nsems] [rs_flags]
* Effect: Gets semaphore set identifier
*/
public static int executeSemget(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int keyReg = bytecode[pc++];
int nsemsReg = bytecode[pc++];
int flagsReg = bytecode[pc++];
// TODO: Implement via JNI or java.nio
throw new UnsupportedOperationException("semget() not yet implemented");
}
/**
* SLOW_SEMOP: rd = semop(semid, opstring)
* Format: [SLOW_SEMOP] [rd] [rs_semid] [rs_opstring]
* Effect: Performs semaphore operations
*/
public static int executeSemop(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int semidReg = bytecode[pc++];
int opstringReg = bytecode[pc++];
// TODO: Implement via JNI
throw new UnsupportedOperationException("semop() not yet implemented");
}
/**
* SLOW_MSGGET: rd = msgget(key, flags)
* Format: [SLOW_MSGGET] [rd] [rs_key] [rs_flags]
* Effect: Gets message queue identifier
*/
public static int executeMsgget(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int keyReg = bytecode[pc++];
int flagsReg = bytecode[pc++];
// TODO: Implement via JNI
throw new UnsupportedOperationException("msgget() not yet implemented");
}
/**
* SLOW_MSGSND: rd = msgsnd(id, msg, flags)
* Format: [SLOW_MSGSND] [rd] [rs_id] [rs_msg] [rs_flags]
* Effect: Sends message to queue
*/
public static int executeMsgsnd(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int idReg = bytecode[pc++];
int msgReg = bytecode[pc++];
int flagsReg = bytecode[pc++];
// TODO: Implement via JNI
throw new UnsupportedOperationException("msgsnd() not yet implemented");
}
/**
* SLOW_MSGRCV: rd = msgrcv(id, size, type, flags)
* Format: [SLOW_MSGRCV] [rd] [rs_id] [rs_size] [rs_type] [rs_flags]
* Effect: Receives message from queue
*/
public static int executeMsgrcv(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int idReg = bytecode[pc++];
int sizeReg = bytecode[pc++];
int typeReg = bytecode[pc++];
int flagsReg = bytecode[pc++];
// TODO: Implement via JNI
throw new UnsupportedOperationException("msgrcv() not yet implemented");
}
/**
* SLOW_SHMGET: rd = shmget(key, size, flags)
* Format: [SLOW_SHMGET] [rd] [rs_key] [rs_size] [rs_flags]
* Effect: Gets shared memory segment
*/
public static int executeShmget(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int keyReg = bytecode[pc++];
int sizeReg = bytecode[pc++];
int flagsReg = bytecode[pc++];
// TODO: Implement via JNI or java.nio.MappedByteBuffer
throw new UnsupportedOperationException("shmget() not yet implemented");
}
/**
* SLOW_SHMREAD: rd = shmread(id, pos, size)
* Format: [SLOW_SHMREAD] [rd] [rs_id] [rs_pos] [rs_size]
* Effect: Reads from shared memory
*/
public static int executeShmread(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int idReg = bytecode[pc++];
int posReg = bytecode[pc++];
int sizeReg = bytecode[pc++];
// TODO: Implement via JNI
throw new UnsupportedOperationException("shmread() not yet implemented");
}
/**
* SLOW_SHMWRITE: shmwrite(id, pos, string)
* Format: [SLOW_SHMWRITE] [rs_id] [rs_pos] [rs_string]
* Effect: Writes to shared memory
*/
public static int executeShmwrite(int[] bytecode, int pc, RuntimeBase[] registers) {
int idReg = bytecode[pc++];
int posReg = bytecode[pc++];
int stringReg = bytecode[pc++];
// TODO: Implement via JNI
throw new UnsupportedOperationException("shmwrite() not yet implemented");
}
/**
* SLOW_SYSCALL: rd = syscall(number, args...)
* Format: [SLOW_SYSCALL] [rd] [rs_number] [arg_count] [rs_arg1] [rs_arg2] ...
* Effect: Makes arbitrary system call
*/
public static int executeSyscall(int[] bytecode, int pc, RuntimeBase[] registers) {
int rd = bytecode[pc++];
int numberReg = bytecode[pc++];
int argCount = bytecode[pc++];
// Collect arguments
RuntimeBase[] args = new RuntimeBase[argCount + 1];
args[0] = registers[numberReg]; // syscall number
for (int i = 0; i < argCount; i++) {
args[i + 1] = registers[bytecode[pc++]];
}
// Call SyscallOperator
registers[rd] = SyscallOperator.syscall(RuntimeContextType.SCALAR, args);
return pc;
}
/**
* SLOW_EVAL_STRING: rd = eval(rs_string)
* Format: [SLOW_EVAL_STRING] [rd] [rs_string]
* Effect: Dynamically evaluates Perl code string
*/
public static int executeEvalString(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int stringReg = bytecode[pc++];
int evalCallContext = RuntimeContextType.SCALAR;
if (pc < bytecode.length) {
evalCallContext = bytecode[pc++];
}
if (evalCallContext == RuntimeContextType.RUNTIME) evalCallContext = ((RuntimeScalar) registers[2]).getInt();
int evalSiteIndex = -1;
if (pc < bytecode.length) {
evalSiteIndex = bytecode[pc++];
}
// Look up per-eval-site variable registry (scope-correct mapping)
Map<String, Integer> siteRegistry = null;
if (evalSiteIndex >= 0 && code.evalSiteRegistries != null
&& evalSiteIndex < code.evalSiteRegistries.size()) {
siteRegistry = code.evalSiteRegistries.get(evalSiteIndex);
}
// Look up per-eval-site pragma flags (strict/feature at compile time of eval site)
int siteStrictOptions = -1;
int siteFeatureFlags = -1;
if (evalSiteIndex >= 0 && code.evalSitePragmaFlags != null
&& evalSiteIndex < code.evalSitePragmaFlags.size()) {
int[] pragmaFlags = code.evalSitePragmaFlags.get(evalSiteIndex);
siteStrictOptions = pragmaFlags[0];
siteFeatureFlags = pragmaFlags[1];
}
RuntimeBase codeValue = registers[stringReg];
RuntimeScalar codeScalar;
if (codeValue instanceof RuntimeScalar) {
codeScalar = (RuntimeScalar) codeValue;
} else {
codeScalar = codeValue.scalar();
}
String perlCode = codeScalar.toString();
evalTrace("EVAL_STRING opcode enter rd=r" + rd + " strReg=r" + stringReg +
" ctx=" + evalCallContext + " evalSite=" + evalSiteIndex +
" src=" + (code != null ? code.sourceName : "null"));
int callContext = evalCallContext;
if (registers[2] instanceof RuntimeScalar rs) {
if (callContext == 0 && rs.value != null) {
callContext = rs.getInt();
}
}
if (callContext == RuntimeContextType.LIST) {
RuntimeList result = EvalStringHandler.evalStringList(
perlCode,
code,
registers,
code.sourceName,
code.sourceLine,
callContext,
siteRegistry,
siteStrictOptions,
siteFeatureFlags
);
registers[rd] = result;
evalTrace("EVAL_STRING opcode exit LIST stored=" + (registers[rd] != null ? registers[rd].getClass().getSimpleName() : "null") +
" scalar=" + result.scalar().toString());
} else {
RuntimeScalar result = EvalStringHandler.evalStringList(
perlCode,
code,
registers,
code.sourceName,
code.sourceLine,
callContext,
siteRegistry,
siteStrictOptions,
siteFeatureFlags
).scalar();
registers[rd] = result;
evalTrace("EVAL_STRING opcode exit SCALAR/VOID stored=" + (registers[rd] != null ? registers[rd].getClass().getSimpleName() : "null") +
" val=" + result.toString() + " bool=" + result.getBoolean());
}
return pc;
}
/**
* SLOW_SELECT: rd = select(listReg)
* Format: [SLOW_SELECT] [rd] [rs_list]
* Effect: Sets or gets the default output filehandle
*/
public static int executeSelect(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int listReg = bytecode[pc++];
RuntimeList list = (RuntimeList) registers[listReg];
// Call IOOperator.select() which handles the logic
RuntimeScalar result = IOOperator.select(
list,
RuntimeContextType.SCALAR
);
registers[rd] = result;
return pc;
}
/**
* SLOW_LOAD_GLOB: rd = getGlobalIO(name).createDetachedCopy()
* Format: [SLOW_LOAD_GLOB] [rd] [name_idx]
* Effect: Loads a glob/filehandle from global variables
*
* <p><b>IMPORTANT:</b> This returns a detached copy of the glob.
* This is crucial for the {@code do { local *FH; *FH }} pattern used to create anonymous
* filehandles. The detached copy captures the current IO slot, so that when the local
* scope ends and restores the global glob, the captured copy retains its IO.
*
* <p>If we returned the global glob directly, the copy would only be made when the
* glob is assigned to a variable (in RuntimeScalar constructor), which happens AFTER
* the local scope ends, and by that time the IO would have been restored to the original.
*/
public static int executeLoadGlob(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int nameIdx = bytecode[pc++];
String globName = code.stringPool[nameIdx];
// Call GlobalVariable.getGlobalIO() to get the RuntimeGlob
RuntimeGlob glob = GlobalVariable.getGlobalIO(globName);
// Return a detached copy to preserve IO during local scope
registers[rd] = glob.createDetachedCopy();
return pc;
}
/**
* LOAD_GLOB_DYNAMIC: rd = GlobalVariable.getGlobalIO(normalize(nameReg, pkg))
* Format: LOAD_GLOB_DYNAMIC rd nameReg pkgIdx
* Effect: Loads a glob by runtime name — used for *{"name"} = value symbolic assignment
*/
public static int executeLoadGlobDynamic(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int nameReg = bytecode[pc++];
int pkgIdx = bytecode[pc++];
String pkg = code.stringPool[pkgIdx];
String name = registers[nameReg].toString();
String globalName = NameNormalizer.normalizeVariableName(name, pkg);
registers[rd] = GlobalVariable.getGlobalIO(globalName);
return pc;
}
/**
* DEREF_SCALAR_STRICT: rd = rs.scalarDeref()
* Format: DEREF_SCALAR_STRICT rd rs
* Matches JVM path: scalarDeref() — throws for non-refs under strict refs.
*/
public static int executeDerefScalarStrict(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
registers[rd] = registers[rs].scalar().scalarDeref();
return pc;
}
/**
* DEREF_SCALAR_NONSTRICT: rd = rs.scalarDerefNonStrict(pkg)
* Format: DEREF_SCALAR_NONSTRICT rd rs pkgIdx
* Matches JVM path: scalarDerefNonStrict(pkg) — allows symbolic refs.
*/
public static int executeDerefScalarNonStrict(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
int pkgIdx = bytecode[pc++];
String pkg = code.stringPool[pkgIdx];
registers[rd] = registers[rs].scalar().scalarDerefNonStrict(pkg);
return pc;
}
/**
* DEREF_GLOB: rd = rs.globDeref()
* Format: DEREF_GLOB rd rs pkgIdx
* Effect: Dereferences a scalar as a glob (strict refs — throws for strings)
*/
public static int executeDerefGlob(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
int pkgIdx = bytecode[pc++];
registers[rd] = registers[rs].scalar().globDeref();
return pc;
}
/**
* DEREF_GLOB_NONSTRICT: rd = rs.globDerefNonStrict(pkg)
* Format: DEREF_GLOB_NONSTRICT rd rs pkgIdx
* Effect: Dereferences a scalar as a glob (no strict refs — allows symbolic names)
*/
public static int executeDerefGlobNonStrict(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int rs = bytecode[pc++];
int pkgIdx = bytecode[pc++];
String pkg = code.stringPool[pkgIdx];
registers[rd] = registers[rs].scalar().globDerefNonStrict(pkg);
return pc;
}
/**
* Sleep for specified seconds.
* Format: [rd] [rs_seconds]
*
* @param bytecode The bytecode array
* @param pc The program counter
* @param registers The register file
* @return The new program counter
*/
public static int executeSleep(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int secondsReg = bytecode[pc++];
// Convert to scalar (handles both RuntimeScalar and RuntimeList)
RuntimeBase secondsBase = registers[secondsReg];
RuntimeScalar seconds = secondsBase.scalar();
// Call Time.sleep()
RuntimeScalar result = Time.sleep(seconds);
registers[rd] = result;
return pc;
}
public static int executeAlarm(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int secondsReg = bytecode[pc++];
RuntimeScalar seconds = registers[secondsReg].scalar();
registers[rd] = Time.alarm(RuntimeContextType.SCALAR, seconds);
return pc;
}
/**
* Dereference array reference for multidimensional array access.
* Handles: $array[0][1] which is really $array[0]->[1]
*
* @param bytecode The bytecode array
* @param pc Program counter (points after slowOpId)
* @param registers Register array
* @return Updated program counter
*/
public static int executeDerefArray(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int scalarReg = bytecode[pc++];
RuntimeBase scalarBase = registers[scalarReg];
// If it's already an array, use it directly
if (scalarBase instanceof RuntimeArray) {
registers[rd] = scalarBase;
return pc;
}
if (scalarBase instanceof RuntimeList) {
RuntimeArray arr = new RuntimeArray();
scalarBase.addToArray(arr);
registers[rd] = arr;
return pc;
}
// Otherwise, dereference as array reference
RuntimeScalar scalar = scalarBase.scalar();
// Get the dereferenced array using Perl's array dereference semantics
RuntimeArray array = scalar.arrayDeref();
registers[rd] = array;
return pc;
}
/**
* SLOWOP_RETRIEVE_BEGIN_SCALAR: Retrieve persistent scalar from BEGIN block
* Format: [SLOWOP_RETRIEVE_BEGIN_SCALAR] [rd] [nameIdx] [begin_id]
* Effect: rd = PersistentVariable.retrieveBeginScalar(stringPool[nameIdx], begin_id)
*/
public static int executeRetrieveBeginScalar(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int nameIdx = bytecode[pc++];
int beginId = bytecode[pc++];
String varName = code.stringPool[nameIdx];
RuntimeScalar result = PersistentVariable.retrieveBeginScalar(varName, beginId);
registers[rd] = result;
return pc;
}
/**
* SLOWOP_RETRIEVE_BEGIN_ARRAY: Retrieve persistent array from BEGIN block
* Format: [SLOWOP_RETRIEVE_BEGIN_ARRAY] [rd] [nameIdx] [begin_id]
* Effect: rd = PersistentVariable.retrieveBeginArray(stringPool[nameIdx], begin_id)
*/
public static int executeRetrieveBeginArray(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int nameIdx = bytecode[pc++];
int beginId = bytecode[pc++];
String varName = code.stringPool[nameIdx];
RuntimeArray result = PersistentVariable.retrieveBeginArray(varName, beginId);
registers[rd] = result;
return pc;
}
/**
* SLOWOP_RETRIEVE_BEGIN_HASH: Retrieve persistent hash from BEGIN block
* Format: [SLOWOP_RETRIEVE_BEGIN_HASH] [rd] [nameIdx] [begin_id]
* Effect: rd = PersistentVariable.retrieveBeginHash(stringPool[nameIdx], begin_id)
*/
public static int executeRetrieveBeginHash(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int nameIdx = bytecode[pc++];
int beginId = bytecode[pc++];
String varName = code.stringPool[nameIdx];
RuntimeHash result = PersistentVariable.retrieveBeginHash(varName, beginId);
registers[rd] = result;
return pc;
}
/**
* SLOWOP_LOCAL_SCALAR: Temporarily localize a global scalar variable
* Format: [SLOWOP_LOCAL_SCALAR] [rd] [nameIdx]
* Effect: rd = GlobalRuntimeScalar.makeLocal(stringPool[nameIdx])
*/
public static int executeLocalScalar(
int[] bytecode,
int pc,
RuntimeBase[] registers,
InterpretedCode code) {
int rd = bytecode[pc++];
int nameIdx = bytecode[pc++];
String varName = code.stringPool[nameIdx];
RuntimeScalar result = GlobalRuntimeScalar.makeLocal(varName);
registers[rd] = result;
return pc;
}
/**
* SLOWOP_SPLICE: Splice array operation
* Format: [SLOWOP_SPLICE] [rd] [arrayReg] [argsReg] [context]
* Effect: rd = Operator.splice(registers[arrayReg], registers[argsReg])
* In scalar context, returns last element removed (or undef if no elements removed)
*/
public static int executeSplice(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
int argsReg = bytecode[pc++];
int context = bytecode[pc++];
if (context == RuntimeContextType.RUNTIME) context = ((RuntimeScalar) registers[2]).getInt();
RuntimeArray array = (RuntimeArray) registers[arrayReg];
RuntimeList args = (RuntimeList) registers[argsReg];
RuntimeList result = Operator.splice(array, args);
// In scalar context, return last element removed (Perl semantics)
if (context == RuntimeContextType.SCALAR) {
if (result.elements.isEmpty()) {
registers[rd] = new RuntimeScalar(); // undef
} else {
registers[rd] = result.elements.get(result.elements.size() - 1);
}
} else {
registers[rd] = result;
}
return pc;
}
/**
* SLOWOP_ARRAY_SLICE: Get array slice
* Format: [SLOWOP_ARRAY_SLICE] [rd] [arrayReg] [indicesReg]
* Effect: rd = array.getSlice(indices)
*/
public static int executeArraySlice(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int arrayReg = bytecode[pc++];
int indicesReg = bytecode[pc++];
RuntimeArray array = (RuntimeArray) registers[arrayReg];
RuntimeList indices = (RuntimeList) registers[indicesReg];
RuntimeList result = array.getSlice(indices);
registers[rd] = result;
return pc;
}
/**
* SLOW_REVERSE: rd = Operator.reverse(ctx, args...)
* Format: [SLOW_REVERSE] [rd] [argsReg] [ctx]
* Effect: rd = Operator.reverse(ctx, args...)
*/
public static int executeReverse(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int argsReg = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
RuntimeList argsList = (RuntimeList) registers[argsReg];
RuntimeBase[] args = argsList.elements.toArray(new RuntimeBase[0]);
RuntimeBase result = Operator.reverse(ctx, args);
registers[rd] = result;
return pc;
}
/**
* SLOW_ARRAY_SLICE_SET: array.setSlice(indices, values)
* Format: [SLOW_ARRAY_SLICE_SET] [arrayReg] [indicesReg] [valuesReg]
* Effect: Sets array elements at indices to values
*/
public static int executeArraySliceSet(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int arrayReg = bytecode[pc++];
int indicesReg = bytecode[pc++];
int valuesReg = bytecode[pc++];
RuntimeArray array = (RuntimeArray) registers[arrayReg];
RuntimeList indices = (RuntimeList) registers[indicesReg];
RuntimeBase valuesBase = registers[valuesReg];
// Materialize values into a flat list using addToArray (handles PerlRange, etc.)
RuntimeArray valuesArray = new RuntimeArray();
valuesBase.addToArray(valuesArray);
RuntimeList values = new RuntimeList();
values.elements.addAll(valuesArray.elements);
array.setSlice(indices, values);
return pc;
}
/**
* SLOW_SPLIT: rd = Operator.split(pattern, args, ctx)
* Format: [SLOW_SPLIT] [rd] [patternReg] [argsReg] [ctx]
* Effect: rd = Operator.split(pattern, args, ctx)
*/
public static int executeSplit(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int patternReg = bytecode[pc++];
int argsReg = bytecode[pc++];
int ctx = bytecode[pc++];
if (ctx == RuntimeContextType.RUNTIME) ctx = ((RuntimeScalar) registers[2]).getInt();
RuntimeScalar pattern = (RuntimeScalar) registers[patternReg];
RuntimeBase argsBase = registers[argsReg];
RuntimeList args = (argsBase instanceof RuntimeList)
? (RuntimeList) argsBase
: new RuntimeList(argsBase.scalar());
RuntimeList result = Operator.split(pattern, args, ctx);
registers[rd] = result;
return pc;
}
/**
* SLOW_EXISTS: rd = exists operand
* Format: [SLOW_EXISTS] [rd] [operandReg]
* Effect: rd = exists operand (fallback for non-simple cases like exists &sub)
*/
public static int executeExists(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int operandReg = bytecode[pc++];
RuntimeBase operand = registers[operandReg];
RuntimeScalar scalar = (operand instanceof RuntimeScalar)
? (RuntimeScalar) operand
: operand.scalar();
// Delegate to the same runtime method the JVM backend uses.
// For CODE types, this checks RuntimeCode.defined().
// For string types, this looks up in globalCodeRefs.
registers[rd] = GlobalVariable.existsGlobalCodeRefAsScalar(scalar);
return pc;
}
/**
* SLOW_DELETE: rd = delete operand
* Format: [SLOW_DELETE] [rd] [operandReg]
* Effect: rd = delete operand (fallback for non-simple cases like delete &sub)
*/
public static int executeDelete(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int operandReg = bytecode[pc++];
RuntimeBase operand = registers[operandReg];
RuntimeScalar scalar = (operand instanceof RuntimeScalar)
? (RuntimeScalar) operand
: operand.scalar();
// Delegate to the same runtime method the JVM backend uses.
registers[rd] = GlobalVariable.deleteGlobalCodeRefAsScalar(scalar);
return pc;
}
/**
* Dereference hash reference for hashref access.
* Handles: $hashref->{key} where $hashref contains a hash reference
*
* @param bytecode The bytecode array
* @param pc Program counter (points after slowOpId)
* @param registers Register array
* @return Updated program counter
*/
public static int executeDerefHash(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int scalarReg = bytecode[pc++];
RuntimeBase scalarBase = registers[scalarReg];
// If it's already a hash, use it directly
if (scalarBase instanceof RuntimeHash) {
registers[rd] = scalarBase;
return pc;
}
// Otherwise, dereference as hash reference
RuntimeScalar scalar = scalarBase.scalar();
// Get the dereferenced hash using Perl's hash dereference semantics
RuntimeHash hash = scalar.hashDeref();
registers[rd] = hash;
return pc;
}
public static int executeDerefHashNonStrict(int[] bytecode, int pc, RuntimeBase[] registers, InterpretedCode code) {
int rd = bytecode[pc++];
int scalarReg = bytecode[pc++];
int pkgIdx = bytecode[pc++];
String pkg = code.stringPool[pkgIdx];
RuntimeBase scalarBase = registers[scalarReg];
if (scalarBase instanceof RuntimeHash) {
registers[rd] = scalarBase;
return pc;
}
RuntimeScalar scalar = scalarBase.scalar();
registers[rd] = scalar.hashDerefNonStrict(pkg);
return pc;
}
public static int executeDerefArrayNonStrict(int[] bytecode, int pc, RuntimeBase[] registers, InterpretedCode code) {
int rd = bytecode[pc++];
int scalarReg = bytecode[pc++];
int pkgIdx = bytecode[pc++];
String pkg = code.stringPool[pkgIdx];
RuntimeBase scalarBase = registers[scalarReg];
if (scalarBase instanceof RuntimeArray) {
registers[rd] = scalarBase;
return pc;
}
if (scalarBase instanceof RuntimeList) {
RuntimeArray arr = new RuntimeArray();
scalarBase.addToArray(arr);
registers[rd] = arr;
return pc;
}
RuntimeScalar scalar = scalarBase.scalar();
registers[rd] = scalar.arrayDerefNonStrict(pkg);
return pc;
}
/**
* SLOW_HASH_SLICE: rd = hash.getSlice(keys_list)
* Format: [SLOW_HASH_SLICE] [rd] [hashReg] [keysListReg]
* Effect: rd = RuntimeArray of values for the given keys
*/
public static int executeHashSlice(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
int keysListReg = bytecode[pc++];
RuntimeHash hash = (RuntimeHash) registers[hashReg];
RuntimeList keysList = (RuntimeList) registers[keysListReg];
// Get values for all keys
RuntimeList valuesList = hash.getSlice(keysList);
// Convert to RuntimeArray for array assignment
RuntimeArray result = new RuntimeArray();
for (RuntimeBase elem : valuesList.elements) {
result.elements.add(elem.scalar());
}
registers[rd] = result;
return pc;
}
/**
* SLOW_HASH_SLICE_DELETE: rd = hash.deleteSlice(keys_list)
* Format: [SLOW_HASH_SLICE_DELETE] [rd] [hashReg] [keysListReg]
* Effect: rd = RuntimeList of deleted values
*/
public static int executeHashSliceDelete(
int[] bytecode,
int pc,
RuntimeBase[] registers) {
int rd = bytecode[pc++];
int hashReg = bytecode[pc++];
int keysListReg = bytecode[pc++];
RuntimeHash hash = (RuntimeHash) registers[hashReg];
RuntimeList keysList = (RuntimeList) registers[keysListReg];
// Delete values for all keys and return them
RuntimeList deletedValuesList = hash.deleteSlice(keysList);
// Convert to RuntimeArray for array assignment
RuntimeArray result = new RuntimeArray();
for (RuntimeBase elem : deletedValuesList.elements) {
result.elements.add(elem.scalar());
}
registers[rd] = result;
return pc;
}
/**