-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCompileAssignment.java
More file actions
1715 lines (1537 loc) · 97.9 KB
/
CompileAssignment.java
File metadata and controls
1715 lines (1537 loc) · 97.9 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.frontend.analysis.ConstantFoldingVisitor;
import org.perlonjava.frontend.analysis.LValueVisitor;
import org.perlonjava.frontend.astnode.*;
import org.perlonjava.runtime.runtimetypes.NameNormalizer;
import org.perlonjava.runtime.runtimetypes.RuntimeCode;
import org.perlonjava.runtime.runtimetypes.RuntimeContextType;
import java.util.ArrayList;
import java.util.List;
public class CompileAssignment {
private static boolean handleLocalAssignment(BytecodeCompiler bc, BinaryOperatorNode node, OperatorNode leftOp, int rhsContext) {
if (!leftOp.operator.equals("local")) return false;
Node localOperand = leftOp.operand;
// General fallback for any BinaryOperatorNode lvalue (matches JVM backend behavior)
// Handles: local $hash{key} = v, local $array[i] = v, local $obj->method->{key} = v, etc.
if (localOperand instanceof BinaryOperatorNode binOp) {
bc.compileNode(binOp, -1, rhsContext);
int elemReg = bc.lastResultReg;
bc.emit(Opcodes.PUSH_LOCAL_VARIABLE);
bc.emitReg(elemReg);
bc.compileNode(node.right, -1, rhsContext);
int valueReg = bc.lastResultReg;
bc.emit(Opcodes.SET_SCALAR);
bc.emitReg(elemReg);
bc.emitReg(valueReg);
bc.lastResultReg = elemReg;
return true;
}
if (localOperand instanceof OperatorNode sigilOp) {
String sigil = sigilOp.operator;
if ((sigil.equals("$") || sigil.equals("@") || sigil.equals("%") || sigil.equals("*"))
&& sigilOp.operand instanceof IdentifierNode idNode) {
String varName = sigil + idNode.name;
if (bc.hasVariable(varName) && !bc.isOurVariable(varName) && !bc.isReservedVariable(varName)) {
bc.throwCompilerException("Can't localize lexical variable " + varName);
return true;
}
bc.compileNode(node.right, -1, rhsContext);
int valueReg = bc.lastResultReg;
String globalVarName = NameNormalizer.normalizeVariableName(idNode.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalVarName);
int localReg = bc.allocateRegister();
switch (sigil) {
case "$" -> {
bc.emitWithToken(Opcodes.LOCAL_SCALAR, node.getIndex());
bc.emitReg(localReg);
bc.emit(nameIdx);
bc.emit(Opcodes.SET_SCALAR);
bc.emitReg(localReg);
bc.emitReg(valueReg);
}
case "@" -> {
// For reserved variables like @_, use register-based localization
if (bc.isReservedVariable(varName)) {
int regIdx = bc.getVariableRegister(varName);
bc.emit(Opcodes.PUSH_LOCAL_VARIABLE);
bc.emitReg(regIdx);
bc.emit(Opcodes.ARRAY_SET_FROM_LIST);
bc.emitReg(regIdx);
bc.emitReg(valueReg);
bc.lastResultReg = regIdx;
return true;
}
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(localReg);
bc.emit(nameIdx);
bc.emit(Opcodes.PUSH_LOCAL_VARIABLE);
bc.emitReg(localReg);
bc.emit(Opcodes.ARRAY_SET_FROM_LIST);
bc.emitReg(localReg);
bc.emitReg(valueReg);
}
case "%" -> {
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(localReg);
bc.emit(nameIdx);
bc.emit(Opcodes.PUSH_LOCAL_VARIABLE);
bc.emitReg(localReg);
bc.emit(Opcodes.HASH_SET_FROM_LIST);
bc.emitReg(localReg);
bc.emitReg(valueReg);
}
case "*" -> {
bc.emitWithToken(Opcodes.LOCAL_GLOB, node.getIndex());
bc.emitReg(localReg);
bc.emit(nameIdx);
bc.emit(Opcodes.STORE_GLOB);
bc.emitReg(localReg);
bc.emitReg(valueReg);
}
}
bc.lastResultReg = localReg;
return true;
}
// Handle dynamic glob names: local *$probe = sub { ... }
if (sigil.equals("*") && !(sigilOp.operand instanceof IdentifierNode)) {
// Compile the glob name expression (e.g., $probe)
bc.compileNode(sigilOp.operand, -1, RuntimeContextType.SCALAR);
int nameScalarReg = bc.lastResultReg;
// Load the glob using dynamic name
int globReg = bc.allocateRegister();
int pkgIdx = bc.addToStringPool(bc.getCurrentPackage());
bc.emitWithToken(Opcodes.LOAD_GLOB_DYNAMIC, node.getIndex());
bc.emitReg(globReg);
bc.emitReg(nameScalarReg);
bc.emit(pkgIdx);
// Push the glob onto the local stack
bc.emit(Opcodes.PUSH_LOCAL_VARIABLE);
bc.emitReg(globReg);
// Compile the RHS value
bc.compileNode(node.right, -1, rhsContext);
int valueReg = bc.lastResultReg;
// Store value to glob
bc.emit(Opcodes.STORE_GLOB);
bc.emitReg(globReg);
bc.emitReg(valueReg);
bc.lastResultReg = globReg;
return true;
}
if (sigil.equals("our") && sigilOp.operand instanceof OperatorNode innerSigilOp
&& innerSigilOp.operand instanceof IdentifierNode idNode) {
return handleLocalOurAssignment(bc, node, innerSigilOp, idNode, rhsContext);
}
}
if (localOperand instanceof ListNode listNode) {
return handleLocalListAssignment(bc, node, listNode, rhsContext);
}
return false;
}
private static boolean handleLocalOurAssignment(BytecodeCompiler bc, BinaryOperatorNode node,
OperatorNode innerSigilOp, IdentifierNode idNode, int rhsContext) {
String innerSigil = innerSigilOp.operator;
String varName = innerSigil + idNode.name;
String globalVarName = NameNormalizer.normalizeVariableName(idNode.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalVarName);
int ourReg = bc.hasVariable(varName) ? bc.getVariableRegister(varName) : bc.addVariable(varName, "our");
bc.compileNode(node.right, -1, rhsContext);
int valueReg = bc.lastResultReg;
int localReg = bc.allocateRegister();
switch (innerSigil) {
case "$" -> {
bc.emit(Opcodes.LOAD_GLOBAL_SCALAR);
bc.emitReg(ourReg);
bc.emit(nameIdx);
bc.emitWithToken(Opcodes.LOCAL_SCALAR, node.getIndex());
bc.emitReg(localReg);
bc.emit(nameIdx);
bc.emit(Opcodes.SET_SCALAR);
bc.emitReg(localReg);
bc.emitReg(valueReg);
bc.emit(Opcodes.LOAD_GLOBAL_SCALAR);
bc.emitReg(ourReg);
bc.emit(nameIdx);
}
case "@" -> {
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(ourReg);
bc.emit(nameIdx);
bc.emitWithToken(Opcodes.LOCAL_ARRAY, node.getIndex());
bc.emitReg(localReg);
bc.emit(nameIdx);
bc.emit(Opcodes.ARRAY_SET_FROM_LIST);
bc.emitReg(localReg);
bc.emitReg(valueReg);
}
case "%" -> {
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(ourReg);
bc.emit(nameIdx);
bc.emitWithToken(Opcodes.LOCAL_HASH, node.getIndex());
bc.emitReg(localReg);
bc.emit(nameIdx);
bc.emit(Opcodes.HASH_SET_FROM_LIST);
bc.emitReg(localReg);
bc.emitReg(valueReg);
}
default -> {
bc.throwCompilerException("Unsupported variable type in local our: " + innerSigil);
return true;
}
}
bc.lastResultReg = localReg;
return true;
}
private static boolean tryAddAssignOptimization(BytecodeCompiler bc, BinaryOperatorNode node, int rhsContext) {
if (!(node.left instanceof OperatorNode leftOp) || !(node.right instanceof BinaryOperatorNode rightBin)) return false;
if (!leftOp.operator.equals("$") || !(leftOp.operand instanceof IdentifierNode leftId)) return false;
if (!rightBin.operator.equals("+") || !(rightBin.left instanceof OperatorNode rightLeftOp)) return false;
if (!rightLeftOp.operator.equals("$") || !(rightLeftOp.operand instanceof IdentifierNode rightLeftId)) return false;
String leftVarName = "$" + leftId.name;
String rightLeftVarName = "$" + rightLeftId.name;
boolean isCaptured = bc.capturedVarIndices != null && bc.capturedVarIndices.containsKey(leftVarName);
if (!leftVarName.equals(rightLeftVarName) || !bc.hasVariable(leftVarName) || isCaptured) return false;
int targetReg = bc.getVariableRegister(leftVarName);
bc.compileNode(rightBin.right, -1, rhsContext);
int rhsReg = bc.lastResultReg;
bc.emit(Opcodes.ADD_ASSIGN);
bc.emitReg(targetReg);
bc.emitReg(rhsReg);
bc.lastResultReg = targetReg;
return true;
}
private static boolean handleLocalListAssignment(BytecodeCompiler bc, BinaryOperatorNode node,
ListNode listNode, int rhsContext) {
if (listNode.elements.size() == 1) {
Node element = listNode.elements.get(0);
if (element instanceof OperatorNode sigilOp && sigilOp.operator.equals("$")
&& sigilOp.operand instanceof IdentifierNode idNode) {
String varName = "$" + idNode.name;
if (bc.hasVariable(varName)) {
bc.throwCompilerException("Can't localize lexical variable " + varName);
return true;
}
bc.compileNode(node.right, -1, rhsContext);
int valueReg = bc.lastResultReg;
String globalVarName = NameNormalizer.normalizeVariableName(idNode.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalVarName);
int localReg = bc.allocateRegister();
bc.emitWithToken(Opcodes.LOCAL_SCALAR, node.getIndex());
bc.emitReg(localReg);
bc.emit(nameIdx);
bc.emit(Opcodes.SET_SCALAR);
bc.emitReg(localReg);
bc.emitReg(valueReg);
bc.lastResultReg = localReg;
return true;
}
}
bc.compileNode(node.right, -1, rhsContext);
int valueReg = bc.lastResultReg;
for (int i = 0; i < listNode.elements.size(); i++) {
Node element = listNode.elements.get(i);
if (element instanceof OperatorNode sigilOp && sigilOp.operator.equals("$")
&& sigilOp.operand instanceof IdentifierNode idNode) {
String varName = "$" + idNode.name;
if (bc.hasVariable(varName)) {
bc.throwCompilerException("Can't localize lexical variable " + varName);
return true;
}
String globalVarName = NameNormalizer.normalizeVariableName(idNode.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalVarName);
int localReg = bc.allocateRegister();
bc.emitWithToken(Opcodes.LOCAL_SCALAR, node.getIndex());
bc.emitReg(localReg);
bc.emit(nameIdx);
int elemReg = bc.allocateRegister();
bc.emit(Opcodes.ARRAY_GET);
bc.emitReg(elemReg);
bc.emitReg(valueReg);
bc.emitInt(i);
bc.emit(Opcodes.SET_SCALAR);
bc.emitReg(localReg);
bc.emitReg(elemReg);
if (i == 0) bc.lastResultReg = localReg;
}
}
return true;
}
/**
* Helper method to compile assignment operators (=).
* Extracted from visit(BinaryOperatorNode) to reduce method size.
* Handles all forms of assignment including my/our/local, scalars, arrays, hashes, and slices.
*/
public static void compileAssignmentOperator(BytecodeCompiler bytecodeCompiler, BinaryOperatorNode node) {
// Determine the calling context for the RHS based on LHS type
// Use LValueVisitor to properly determine context for all LHS patterns
// including $array[index], $hash{key}, etc.
int rhsContext = LValueVisitor.getContext(node);
if (rhsContext == RuntimeContextType.VOID) {
// VOID means not a valid L-value, but we still compile it - default to LIST
rhsContext = RuntimeContextType.LIST;
}
// Set the context for subroutine calls in RHS
int outerContext = bytecodeCompiler.currentCallContext;
// Special case: my $x = value
if (node.left instanceof OperatorNode leftOp) {
if (leftOp.operator.equals("my") || leftOp.operator.equals("state")) {
// Extract variable name from "my"/"state" operand
Node myOperand = leftOp.operand;
// Handle my $x (where $x is OperatorNode("$", IdentifierNode("x")))
if (myOperand instanceof OperatorNode sigilOp) {
if (sigilOp.operator.equals("$") && sigilOp.operand instanceof IdentifierNode) {
String varName = "$" + ((IdentifierNode) sigilOp.operand).name;
Integer beginIdObj = RuntimeCode.evalBeginIds.get(sigilOp);
if (beginIdObj != null) {
int beginId = beginIdObj;
int nameIdx = bytecodeCompiler.addToStringPool(varName);
int reg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emitWithToken(Opcodes.RETRIEVE_BEGIN_SCALAR, node.getIndex());
bytecodeCompiler.emitReg(reg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emit(beginId);
// Now register contains a reference to the persistent RuntimeScalar
// Store the initializer value INTO that RuntimeScalar
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int valueReg = bytecodeCompiler.lastResultReg;
// Set the value in the persistent scalar using SET_SCALAR
// This calls .set() on the RuntimeScalar without overwriting the reference
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(reg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.registerVariable(varName, reg);
bytecodeCompiler.lastResultReg = reg;
return;
}
// Regular lexical variable (not captured)
// Compile RHS first, before adding variable to scope,
// so that `my $x = $x` reads the outer $x on the RHS
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int valueReg = bytecodeCompiler.lastResultReg;
// Now allocate register for new lexical variable and add to symbol table
int reg = bytecodeCompiler.addVariable(varName, "my");
bytecodeCompiler.emit(Opcodes.MY_SCALAR);
bytecodeCompiler.emitReg(reg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = reg;
return;
} else if (sigilOp.operator.equals("@") && sigilOp.operand instanceof IdentifierNode) {
// Handle my @array = ...
String varName = "@" + ((IdentifierNode) sigilOp.operand).name;
Integer beginIdArr = RuntimeCode.evalBeginIds.get(sigilOp);
if (beginIdArr != null) {
int beginId = beginIdArr;
int nameIdx = bytecodeCompiler.addToStringPool(varName);
int arrayReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emitWithToken(Opcodes.RETRIEVE_BEGIN_ARRAY, node.getIndex());
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emit(beginId);
// Compile RHS (should evaluate to a list)
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int listReg = bytecodeCompiler.lastResultReg;
// Populate array from list
bytecodeCompiler.emit(Opcodes.ARRAY_SET_FROM_LIST);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emitReg(listReg);
bytecodeCompiler.registerVariable(varName, arrayReg);
if (rhsContext == RuntimeContextType.SCALAR) {
int countReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.ARRAY_SIZE);
bytecodeCompiler.emitReg(countReg);
bytecodeCompiler.emitReg(listReg);
bytecodeCompiler.lastResultReg = countReg;
} else {
bytecodeCompiler.lastResultReg = arrayReg;
}
return;
}
int arrayReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int listReg = bytecodeCompiler.lastResultReg;
bytecodeCompiler.registerVariable(varName, arrayReg);
bytecodeCompiler.emit(Opcodes.NEW_ARRAY);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emit(Opcodes.ARRAY_SET_FROM_LIST);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emitReg(listReg);
if (rhsContext == RuntimeContextType.SCALAR) {
int countReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.ARRAY_SIZE);
bytecodeCompiler.emitReg(countReg);
bytecodeCompiler.emitReg(listReg);
bytecodeCompiler.lastResultReg = countReg;
} else {
bytecodeCompiler.lastResultReg = arrayReg;
}
return;
} else if (sigilOp.operator.equals("%") && sigilOp.operand instanceof IdentifierNode) {
// Handle my %hash = ...
String varName = "%" + ((IdentifierNode) sigilOp.operand).name;
Integer beginIdHash = RuntimeCode.evalBeginIds.get(sigilOp);
if (beginIdHash != null) {
int beginId = beginIdHash;
int nameIdx = bytecodeCompiler.addToStringPool(varName);
int hashReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emitWithToken(Opcodes.RETRIEVE_BEGIN_HASH, node.getIndex());
bytecodeCompiler.emitReg(hashReg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emit(beginId);
// Compile RHS (should evaluate to a list)
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int listReg = bytecodeCompiler.lastResultReg;
// Populate hash from list
bytecodeCompiler.emit(Opcodes.HASH_SET_FROM_LIST);
bytecodeCompiler.emitReg(hashReg);
bytecodeCompiler.emitReg(listReg);
bytecodeCompiler.registerVariable(varName, hashReg);
bytecodeCompiler.lastResultReg = hashReg;
return;
}
// Regular lexical hash (not captured)
// Allocate register but don't add to scope yet,
// so that `my %h = %h` reads the outer %h on the RHS
int hashReg = bytecodeCompiler.allocateRegister();
// Compile RHS first, before adding variable to scope
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int listReg = bytecodeCompiler.lastResultReg;
// Now add to symbol table and create hash
bytecodeCompiler.registerVariable(varName, hashReg);
bytecodeCompiler.emit(Opcodes.NEW_HASH);
bytecodeCompiler.emitReg(hashReg);
// Populate hash from list
bytecodeCompiler.emit(Opcodes.HASH_SET_FROM_LIST);
bytecodeCompiler.emitReg(hashReg);
bytecodeCompiler.emitReg(listReg);
bytecodeCompiler.lastResultReg = hashReg;
return;
}
}
// Handle my x (direct identifier without sigil)
if (myOperand instanceof IdentifierNode) {
String varName = ((IdentifierNode) myOperand).name;
// Compile RHS first, before adding variable to scope
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int valueReg = bytecodeCompiler.lastResultReg;
// Now allocate register and add to symbol table
int reg = bytecodeCompiler.addVariable(varName, "my");
bytecodeCompiler.emit(Opcodes.MY_SCALAR);
bytecodeCompiler.emitReg(reg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = reg;
return;
}
// Handle my ($x, $y, @rest) = ... - list declaration with assignment
// Uses SET_FROM_LIST to match JVM backend's setFromList() semantics
if (myOperand instanceof ListNode listNode) {
// Compile RHS first
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int listReg = bytecodeCompiler.lastResultReg;
// Convert to list if needed
int rhsListReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.SCALAR_TO_LIST);
bytecodeCompiler.emitReg(rhsListReg);
bytecodeCompiler.emitReg(listReg);
// Declare all variables and collect their registers
List<Integer> varRegs = new ArrayList<>();
for (int i = 0; i < listNode.elements.size(); i++) {
Node element = listNode.elements.get(i);
if (element instanceof OperatorNode sigilOp) {
String sigil = sigilOp.operator;
if (sigilOp.operand instanceof IdentifierNode) {
String varName = sigil + ((IdentifierNode) sigilOp.operand).name;
int varReg;
Integer beginIdList = RuntimeCode.evalBeginIds.get(sigilOp);
if (beginIdList != null) {
int beginId = beginIdList;
int nameIdx = bytecodeCompiler.addToStringPool(varName);
varReg = bytecodeCompiler.allocateRegister();
switch (sigil) {
case "$" -> {
bytecodeCompiler.emitWithToken(Opcodes.RETRIEVE_BEGIN_SCALAR, node.getIndex());
bytecodeCompiler.emitReg(varReg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emit(beginId);
}
case "@" -> {
bytecodeCompiler.emitWithToken(Opcodes.RETRIEVE_BEGIN_ARRAY, node.getIndex());
bytecodeCompiler.emitReg(varReg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emit(beginId);
}
case "%" -> {
bytecodeCompiler.emitWithToken(Opcodes.RETRIEVE_BEGIN_HASH, node.getIndex());
bytecodeCompiler.emitReg(varReg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emit(beginId);
}
}
bytecodeCompiler.registerVariable(varName, varReg);
} else {
varReg = bytecodeCompiler.addVariable(varName, "my");
switch (sigil) {
case "$" -> {
bytecodeCompiler.emit(Opcodes.LOAD_UNDEF);
bytecodeCompiler.emitReg(varReg);
}
case "@" -> {
bytecodeCompiler.emit(Opcodes.NEW_ARRAY);
bytecodeCompiler.emitReg(varReg);
}
case "%" -> {
bytecodeCompiler.emit(Opcodes.NEW_HASH);
bytecodeCompiler.emitReg(varReg);
}
}
}
varRegs.add(varReg);
}
}
}
// Build LHS list and assign via SET_FROM_LIST
int lhsListReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.CREATE_LIST);
bytecodeCompiler.emitReg(lhsListReg);
bytecodeCompiler.emit(varRegs.size());
for (int reg : varRegs) {
bytecodeCompiler.emitReg(reg);
}
int resultReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.SET_FROM_LIST);
bytecodeCompiler.emitReg(resultReg);
bytecodeCompiler.emitReg(lhsListReg);
bytecodeCompiler.emitReg(rhsListReg);
bytecodeCompiler.lastResultReg = resultReg;
return;
}
}
// Special case: local $x = value
if (handleLocalAssignment(bytecodeCompiler, node, leftOp, rhsContext)) {
return;
}
}
// Regular assignment: $x = value
// OPTIMIZATION: Detect $x = $x + $y and emit ADD_ASSIGN instead of ADD_SCALAR + ALIAS
if (tryAddAssignOptimization(bytecodeCompiler, node, rhsContext)) {
return;
}
// Handle ${block} = value and $$var = value (symbolic references)
// We need to evaluate the LHS FIRST to get the variable name,
// then evaluate the RHS, to ensure the RHS doesn't clobber the LHS registers
if (node.left instanceof OperatorNode leftOp && leftOp.operator.equals("$")) {
boolean strictRefsEnabled = bytecodeCompiler.isStrictRefsEnabled();
if (leftOp.operand instanceof BlockNode block) {
// ${block} = value — mirrors JVM EmitVariable.java case "$"
bytecodeCompiler.compileNode(block, -1, rhsContext);
int nameReg = bytecodeCompiler.lastResultReg;
// Deref to get lvalue target (strict or non-strict)
int derefReg = bytecodeCompiler.allocateRegister();
if (strictRefsEnabled) {
bytecodeCompiler.emitWithToken(Opcodes.DEREF_SCALAR_STRICT, node.getIndex());
bytecodeCompiler.emitReg(derefReg);
bytecodeCompiler.emitReg(nameReg);
} else {
int pkgIdx = bytecodeCompiler.addToStringPool(bytecodeCompiler.getCurrentPackage());
bytecodeCompiler.emitWithToken(Opcodes.DEREF_SCALAR_NONSTRICT, node.getIndex());
bytecodeCompiler.emitReg(derefReg);
bytecodeCompiler.emitReg(nameReg);
bytecodeCompiler.emit(pkgIdx);
}
// Now compile the RHS and assign
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int valueReg = bytecodeCompiler.lastResultReg;
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(derefReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = valueReg;
return;
} else if (leftOp.operand instanceof OperatorNode) {
// $$var = value — mirrors JVM EmitVariable.java case "$"
bytecodeCompiler.compileNode(leftOp.operand, -1, rhsContext);
int nameReg = bytecodeCompiler.lastResultReg;
int derefReg = bytecodeCompiler.allocateRegister();
if (strictRefsEnabled) {
bytecodeCompiler.emitWithToken(Opcodes.DEREF_SCALAR_STRICT, node.getIndex());
bytecodeCompiler.emitReg(derefReg);
bytecodeCompiler.emitReg(nameReg);
} else {
int pkgIdx = bytecodeCompiler.addToStringPool(bytecodeCompiler.getCurrentPackage());
bytecodeCompiler.emitWithToken(Opcodes.DEREF_SCALAR_NONSTRICT, node.getIndex());
bytecodeCompiler.emitReg(derefReg);
bytecodeCompiler.emitReg(nameReg);
bytecodeCompiler.emit(pkgIdx);
}
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int valueReg = bytecodeCompiler.lastResultReg;
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(derefReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = valueReg;
return;
}
}
// Regular assignment: $x = value (no optimization)
// Compile RHS first
bytecodeCompiler.compileNode(node.right, -1, rhsContext);
int valueReg = bytecodeCompiler.lastResultReg;
// Assign to LHS
if (node.left instanceof OperatorNode leftOp) {
if (leftOp.operator.equals("$") && leftOp.operand instanceof IdentifierNode) {
String varName = "$" + ((IdentifierNode) leftOp.operand).name;
if (bytecodeCompiler.hasVariable(varName)) {
// Lexical variable - check if it's captured
int targetReg = bytecodeCompiler.getVariableRegister(varName);
if ((bytecodeCompiler.capturedVarIndices != null && bytecodeCompiler.capturedVarIndices.containsKey(varName))
|| bytecodeCompiler.closureCapturedVarNames.contains(varName)) {
// Captured variable - use SET_SCALAR to preserve aliasing.
// LOAD_UNDEF would replace the register with a new RuntimeScalar,
// breaking the shared reference that closures depend on.
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(targetReg);
bytecodeCompiler.emitReg(valueReg);
} else {
// Regular lexical - create a fresh RuntimeScalar, then copy the value into it.
// LOAD_UNDEF allocates a new mutable RuntimeScalar in the target register;
// SET_SCALAR copies the source value into it.
// This avoids two bugs:
// - ALIAS shares constants from the pool, corrupting them on later mutation
// - SET_SCALAR alone modifies the existing object in-place, which breaks
// 'local' variable restoration when the register was shared
bytecodeCompiler.emit(Opcodes.LOAD_UNDEF);
bytecodeCompiler.emitReg(targetReg);
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(targetReg);
bytecodeCompiler.emitReg(valueReg);
}
bytecodeCompiler.lastResultReg = targetReg;
} else {
// Global variable
// Check strict vars before assignment
if (bytecodeCompiler.shouldBlockGlobalUnderStrictVars(varName)) {
bytecodeCompiler.throwCompilerException("Global symbol \"" + varName + "\" requires explicit package name");
}
// Strip sigil and normalize name (e.g., "$x" → "main::x")
String bareVarName = varName.substring(1); // Remove sigil
String normalizedName = NameNormalizer.normalizeVariableName(bareVarName, bytecodeCompiler.getCurrentPackage());
int nameIdx = bytecodeCompiler.addToStringPool(normalizedName);
bytecodeCompiler.emit(Opcodes.STORE_GLOBAL_SCALAR);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emitReg(valueReg);
// Return the global lvalue so ($_ = "x") =~ s/// modifies $_ in-place
int lvalReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.LOAD_GLOBAL_SCALAR);
bytecodeCompiler.emitReg(lvalReg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.lastResultReg = lvalReg;
}
} else if (leftOp.operator.equals("@") && leftOp.operand instanceof IdentifierNode) {
// Array assignment: @array = ...
String varName = "@" + ((IdentifierNode) leftOp.operand).name;
int arrayReg;
if (bytecodeCompiler.currentSubroutineBeginId != 0 && bytecodeCompiler.currentSubroutineClosureVars != null
&& bytecodeCompiler.currentSubroutineClosureVars.contains(varName)) {
arrayReg = bytecodeCompiler.allocateRegister();
int nameIdx = bytecodeCompiler.addToStringPool(varName);
bytecodeCompiler.emitWithToken(Opcodes.RETRIEVE_BEGIN_ARRAY, node.getIndex());
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emit(bytecodeCompiler.currentSubroutineBeginId);
} else if (bytecodeCompiler.hasVariable(varName)) {
arrayReg = bytecodeCompiler.getVariableRegister(varName);
} else {
arrayReg = bytecodeCompiler.allocateRegister();
String globalArrayName = NameNormalizer.normalizeVariableName(((IdentifierNode) leftOp.operand).name, bytecodeCompiler.getCurrentPackage());
int nameIdx = bytecodeCompiler.addToStringPool(globalArrayName);
bytecodeCompiler.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emit(nameIdx);
}
// Populate array from list using setFromList
bytecodeCompiler.emit(Opcodes.ARRAY_SET_FROM_LIST);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emitReg(valueReg);
// In scalar context, return the array size; in list context, return the array
if (outerContext == RuntimeContextType.SCALAR) {
// Convert array to scalar (returns size)
int sizeReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.ARRAY_SIZE);
bytecodeCompiler.emitReg(sizeReg);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.lastResultReg = sizeReg;
} else {
bytecodeCompiler.lastResultReg = arrayReg;
}
} else if (leftOp.operator.equals("%") && leftOp.operand instanceof IdentifierNode) {
// Hash assignment: %hash = ...
String varName = "%" + ((IdentifierNode) leftOp.operand).name;
int hashReg;
if (bytecodeCompiler.currentSubroutineBeginId != 0 && bytecodeCompiler.currentSubroutineClosureVars != null
&& bytecodeCompiler.currentSubroutineClosureVars.contains(varName)) {
hashReg = bytecodeCompiler.allocateRegister();
int nameIdx = bytecodeCompiler.addToStringPool(varName);
bytecodeCompiler.emitWithToken(Opcodes.RETRIEVE_BEGIN_HASH, node.getIndex());
bytecodeCompiler.emitReg(hashReg);
bytecodeCompiler.emit(nameIdx);
bytecodeCompiler.emit(bytecodeCompiler.currentSubroutineBeginId);
} else if (bytecodeCompiler.hasVariable(varName)) {
hashReg = bytecodeCompiler.getVariableRegister(varName);
} else {
hashReg = bytecodeCompiler.allocateRegister();
String globalHashName = NameNormalizer.normalizeVariableName(((IdentifierNode) leftOp.operand).name, bytecodeCompiler.getCurrentPackage());
int nameIdx = bytecodeCompiler.addToStringPool(globalHashName);
bytecodeCompiler.emit(Opcodes.LOAD_GLOBAL_HASH);
bytecodeCompiler.emitReg(hashReg);
bytecodeCompiler.emit(nameIdx);
}
// Populate hash from list using setFromList
bytecodeCompiler.emit(Opcodes.HASH_SET_FROM_LIST);
bytecodeCompiler.emitReg(hashReg);
bytecodeCompiler.emitReg(valueReg);
// In scalar context, return the hash size; in list context, return the hash
if (outerContext == RuntimeContextType.SCALAR) {
// Convert hash to scalar (returns bucket info like "3/8")
int sizeReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.ARRAY_SIZE);
bytecodeCompiler.emitReg(sizeReg);
bytecodeCompiler.emitReg(hashReg);
bytecodeCompiler.lastResultReg = sizeReg;
} else {
bytecodeCompiler.lastResultReg = hashReg;
}
} else if (leftOp.operator.equals("our")) {
// Assignment to our variable: our $x = value or our @x = value or our %x = value
// Compile the our declaration first (which loads the global into a register)
bytecodeCompiler.compileNode(leftOp, -1, rhsContext);
int targetReg = bytecodeCompiler.lastResultReg;
// Now assign the RHS value to the target register
// The target register contains either a scalar, array, or hash
// We need to determine which and use the appropriate assignment
// Extract the sigil from our operand
if (leftOp.operand instanceof OperatorNode sigilOp) {
String sigil = sigilOp.operator;
if (sigil.equals("$")) {
// Scalar: use SET_SCALAR to modify value without breaking alias
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(targetReg);
bytecodeCompiler.emitReg(valueReg);
} else if (sigil.equals("@")) {
// Array: use ARRAY_SET_FROM_LIST
bytecodeCompiler.emit(Opcodes.ARRAY_SET_FROM_LIST);
bytecodeCompiler.emitReg(targetReg);
bytecodeCompiler.emitReg(valueReg);
} else if (sigil.equals("%")) {
// Hash: use HASH_SET_FROM_LIST
bytecodeCompiler.emit(Opcodes.HASH_SET_FROM_LIST);
bytecodeCompiler.emitReg(targetReg);
bytecodeCompiler.emitReg(valueReg);
}
} else if (leftOp.operand instanceof ListNode listNode) {
// our ($a, $b) = ... - list declaration with assignment
// Uses SET_FROM_LIST to match JVM backend's setFromList() semantics
// Convert RHS to list
int rhsListReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.SCALAR_TO_LIST);
bytecodeCompiler.emitReg(rhsListReg);
bytecodeCompiler.emitReg(valueReg);
// Collect variable registers (already declared by our visitor)
List<Integer> varRegs = new ArrayList<>();
for (int i = 0; i < listNode.elements.size(); i++) {
Node element = listNode.elements.get(i);
if (element instanceof OperatorNode sigilOp) {
String sigil = sigilOp.operator;
if (sigilOp.operand instanceof IdentifierNode) {
String varName = sigil + ((IdentifierNode) sigilOp.operand).name;
varRegs.add(bytecodeCompiler.getVariableRegister(varName));
}
}
}
// Build LHS list and assign via SET_FROM_LIST
int lhsListReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.CREATE_LIST);
bytecodeCompiler.emitReg(lhsListReg);
bytecodeCompiler.emit(varRegs.size());
for (int reg : varRegs) {
bytecodeCompiler.emitReg(reg);
}
int resultReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.SET_FROM_LIST);
bytecodeCompiler.emitReg(resultReg);
bytecodeCompiler.emitReg(lhsListReg);
bytecodeCompiler.emitReg(rhsListReg);
bytecodeCompiler.lastResultReg = resultReg;
return;
}
bytecodeCompiler.lastResultReg = targetReg;
} else if (leftOp.operator.equals("*") && leftOp.operand instanceof IdentifierNode) {
// Typeglob assignment: *foo = value
String varName = ((IdentifierNode) leftOp.operand).name;
String globalName = NameNormalizer.normalizeVariableName(varName, bytecodeCompiler.getCurrentPackage());
int nameIdx = bytecodeCompiler.addToStringPool(globalName);
// Load the glob
int globReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.LOAD_GLOB);
bytecodeCompiler.emitReg(globReg);
bytecodeCompiler.emit(nameIdx);
// Store value to glob
bytecodeCompiler.emit(Opcodes.STORE_GLOB);
bytecodeCompiler.emitReg(globReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = globReg;
} else if (leftOp.operator.equals("*") && leftOp.operand instanceof BlockNode) {
// Symbolic typeglob assignment: *{"name"} = value (no strict refs)
// Evaluate the block to get the glob name as a scalar, then load glob by name
bytecodeCompiler.compileNode(leftOp.operand, -1, rhsContext);
int nameScalarReg = bytecodeCompiler.lastResultReg;
int globReg = bytecodeCompiler.allocateRegister();
int pkgIdx = bytecodeCompiler.addToStringPool(bytecodeCompiler.getCurrentPackage());
bytecodeCompiler.emitWithToken(Opcodes.LOAD_GLOB_DYNAMIC, node.getIndex());
bytecodeCompiler.emitReg(globReg);
bytecodeCompiler.emitReg(nameScalarReg);
bytecodeCompiler.emit(pkgIdx);
// Store value to glob
bytecodeCompiler.emit(Opcodes.STORE_GLOB);
bytecodeCompiler.emitReg(globReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = globReg;
} else if (leftOp.operator.equals("*")) {
// Glob assignment where the glob comes from an expression, e.g. $ref->** = ...
// or 'name'->** = ...
// Compile the glob expression to obtain the RuntimeGlob, then store through it.
bytecodeCompiler.compileNode(leftOp, -1, rhsContext);
int globReg = bytecodeCompiler.lastResultReg;
bytecodeCompiler.emit(Opcodes.STORE_GLOB);
bytecodeCompiler.emitReg(globReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = globReg;
} else if (leftOp.operator.equals("pos")) {
// pos($var) = value - lvalue assignment to regex position
// pos() returns a PosLvalueScalar that can be assigned to
bytecodeCompiler.compileNode(node.left, -1, rhsContext);
int lvalueReg = bytecodeCompiler.lastResultReg;
// Use SET_SCALAR to assign through the lvalue
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(lvalueReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = valueReg;
} else if (leftOp.operator.equals("substr")) {
bytecodeCompiler.compileNode(node.left, -1, rhsContext);
int lvalueReg = bytecodeCompiler.lastResultReg;
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(lvalueReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = valueReg;
} else if (leftOp.operator.equals("vec")) {
// vec($x, offset, bits) = value - lvalue assignment to bit vector
// vec() returns a RuntimeVecLvalue that can be assigned to
bytecodeCompiler.compileNode(node.left, -1, rhsContext);
int lvalueReg = bytecodeCompiler.lastResultReg;
bytecodeCompiler.emit(Opcodes.SET_SCALAR);
bytecodeCompiler.emitReg(lvalueReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = valueReg;
} else if (leftOp.operator.equals("@") && (leftOp.operand instanceof OperatorNode || leftOp.operand instanceof BlockNode)) {
// Array dereference assignment: @$r = ... or @{expr} = ...
// The operand should evaluate to an array reference
boolean isSimpleScalarDeref = leftOp.operand instanceof OperatorNode derefOp && derefOp.operator.equals("$");
if (isSimpleScalarDeref || leftOp.operand instanceof BlockNode) {
// Compile the operand to get the array reference
bytecodeCompiler.compileNode(leftOp.operand, -1, RuntimeContextType.SCALAR);
int scalarRefReg = bytecodeCompiler.lastResultReg;
// Dereference to get the actual array
int arrayReg = bytecodeCompiler.allocateRegister();
if (bytecodeCompiler.isStrictRefsEnabled()) {
bytecodeCompiler.emitWithToken(Opcodes.DEREF_ARRAY, node.getIndex());
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emitReg(scalarRefReg);
} else {
int pkgIdx = bytecodeCompiler.addToStringPool(bytecodeCompiler.getCurrentPackage());
bytecodeCompiler.emitWithToken(Opcodes.DEREF_ARRAY_NONSTRICT, node.getIndex());
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emitReg(scalarRefReg);
bytecodeCompiler.emit(pkgIdx);
}
// Assign the value to the dereferenced array
bytecodeCompiler.emit(Opcodes.ARRAY_SET_FROM_LIST);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emitReg(valueReg);
// In scalar context, return array size; in list context, return the array
if (outerContext == RuntimeContextType.SCALAR) {
int sizeReg = bytecodeCompiler.allocateRegister();
bytecodeCompiler.emit(Opcodes.ARRAY_SIZE);
bytecodeCompiler.emitReg(sizeReg);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.lastResultReg = sizeReg;
} else {
bytecodeCompiler.lastResultReg = arrayReg;
}
} else {
bytecodeCompiler.throwCompilerException("Assignment to unsupported array dereference");
}
} else if (leftOp.operator.equals("$#")) {
int arrayReg = CompileAssignment.resolveArrayForDollarHash(bytecodeCompiler, leftOp);
bytecodeCompiler.emit(Opcodes.SET_ARRAY_LAST_INDEX);
bytecodeCompiler.emitReg(arrayReg);
bytecodeCompiler.emitReg(valueReg);
bytecodeCompiler.lastResultReg = valueReg;
} else if (leftOp.operator.equals("%") && (leftOp.operand instanceof OperatorNode || leftOp.operand instanceof BlockNode)) {
// Hash dereference assignment: %$r = ... or %{expr} = ...
// The operand should evaluate to a hash reference
boolean isSimpleScalarDeref = leftOp.operand instanceof OperatorNode derefOp && derefOp.operator.equals("$");
if (isSimpleScalarDeref || leftOp.operand instanceof BlockNode) {
// Compile the operand to get the hash reference
bytecodeCompiler.compileNode(leftOp.operand, -1, RuntimeContextType.SCALAR);
int scalarRefReg = bytecodeCompiler.lastResultReg;
// Dereference to get the actual hash
int hashReg = bytecodeCompiler.allocateRegister();