-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInterpretedCode.java
More file actions
639 lines (613 loc) · 30.2 KB
/
InterpretedCode.java
File metadata and controls
639 lines (613 loc) · 30.2 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
package org.perlonjava.interpreter;
import org.perlonjava.runtime.*;
/**
* Interpreted bytecode that extends RuntimeCode.
*
* This class represents Perl code that is interpreted rather than compiled to JVM bytecode.
* It is COMPLETELY INDISTINGUISHABLE from compiled RuntimeCode to the rest of the system:
* - Can be stored in global variables ($::func)
* - Can be passed as code references
* - Can capture variables (closures work both directions)
* - Can be used in method dispatch, overload, @ISA, etc.
*
* The ONLY difference is the execution engine:
* - Compiled RuntimeCode uses MethodHandle to invoke JVM bytecode
* - InterpretedCode overrides apply() to dispatch to BytecodeInterpreter
*/
public class InterpretedCode extends RuntimeCode {
// Bytecode and metadata
public final byte[] bytecode; // Instruction opcodes (compact)
public final Object[] constants; // Constant pool (RuntimeBase objects)
public final String[] stringPool; // String constants (variable names, etc.)
public final int maxRegisters; // Number of registers needed
public final RuntimeBase[] capturedVars; // Closure support (captured from outer scope)
// Debug information (optional)
public final String sourceName; // Source file name (for stack traces)
public final int sourceLine; // Source line number
public final java.util.Map<Integer, Integer> pcToTokenIndex; // Map bytecode PC to tokenIndex for error reporting
/**
* Constructor for InterpretedCode.
*
* @param bytecode The bytecode instructions
* @param constants Constant pool (RuntimeBase objects)
* @param stringPool String constants (variable names, etc.)
* @param maxRegisters Number of registers needed for execution
* @param capturedVars Captured variables for closure support (may be null)
* @param sourceName Source file name for debugging
* @param sourceLine Source line number for debugging
* @param pcToTokenIndex Map from bytecode PC to AST tokenIndex for error reporting
*/
public InterpretedCode(byte[] bytecode, Object[] constants, String[] stringPool,
int maxRegisters, RuntimeBase[] capturedVars,
String sourceName, int sourceLine,
java.util.Map<Integer, Integer> pcToTokenIndex) {
super(null, new java.util.ArrayList<>()); // Call RuntimeCode constructor with null prototype, empty attributes
this.bytecode = bytecode;
this.constants = constants;
this.stringPool = stringPool;
this.maxRegisters = maxRegisters;
this.capturedVars = capturedVars;
this.sourceName = sourceName;
this.sourceLine = sourceLine;
this.pcToTokenIndex = pcToTokenIndex;
}
/**
* Override RuntimeCode.apply() to dispatch to interpreter.
*
* This is the ONLY method that differs from compiled RuntimeCode.
* The API signature is IDENTICAL, ensuring perfect compatibility.
*
* @param args The arguments array (@_)
* @param callContext The calling context (VOID/SCALAR/LIST)
* @return RuntimeList containing the result (may be RuntimeControlFlowList)
*/
@Override
public RuntimeList apply(RuntimeArray args, int callContext) {
// Dispatch to interpreter (not compiled bytecode)
return BytecodeInterpreter.execute(this, args, callContext);
}
/**
* Override RuntimeCode.apply() with subroutine name.
*
* @param subroutineName The subroutine name (for stack traces)
* @param args The arguments array (@_)
* @param callContext The calling context
* @return RuntimeList containing the result
*/
@Override
public RuntimeList apply(String subroutineName, RuntimeArray args, int callContext) {
// Dispatch to interpreter with subroutine name for stack traces
return BytecodeInterpreter.execute(this, args, callContext, subroutineName);
}
/**
* Override RuntimeCode.defined() to return true for InterpretedCode.
* InterpretedCode doesn't use methodHandle, so the parent defined() check fails.
* But InterpretedCode instances are always "defined" - they contain executable bytecode.
*/
@Override
public boolean defined() {
return true;
}
/**
* Create an InterpretedCode with captured variables (for closures).
*
* @param capturedVars The variables to capture from outer scope
* @return A new InterpretedCode with captured variables
*/
public InterpretedCode withCapturedVars(RuntimeBase[] capturedVars) {
return new InterpretedCode(
this.bytecode,
this.constants,
this.stringPool,
this.maxRegisters,
capturedVars, // New captured vars
this.sourceName,
this.sourceLine,
this.pcToTokenIndex // Preserve token index map
);
}
/**
* Register this InterpretedCode as a global named subroutine.
* This allows compiled code to call interpreted closures seamlessly.
*
* @param name Subroutine name (e.g., "main::closure_123")
* @return RuntimeScalar CODE reference to this InterpretedCode
*/
public RuntimeScalar registerAsNamedSub(String name) {
// Extract package and sub name
int lastColonIndex = name.lastIndexOf("::");
if (lastColonIndex > 0) {
this.packageName = name.substring(0, lastColonIndex);
this.subName = name.substring(lastColonIndex + 2);
} else {
this.packageName = "main";
this.subName = name;
}
// Register in global code refs (creates or gets existing RuntimeScalar)
// Then set its value to this InterpretedCode
RuntimeScalar codeRef = GlobalVariable.getGlobalCodeRef(name);
codeRef.set(new RuntimeScalar(this));
return codeRef;
}
/**
* Get a human-readable representation for debugging.
*/
@Override
public String toString() {
return "InterpretedCode{" +
"sourceName='" + sourceName + '\'' +
", sourceLine=" + sourceLine +
", bytecode.length=" + bytecode.length +
", maxRegisters=" + maxRegisters +
", hasCapturedVars=" + (capturedVars != null && capturedVars.length > 0) +
'}';
}
/**
* Disassemble bytecode for debugging and optimization analysis.
*/
public String disassemble() {
StringBuilder sb = new StringBuilder();
sb.append("=== Bytecode Disassembly ===\n");
sb.append("Source: ").append(sourceName).append(":").append(sourceLine).append("\n");
sb.append("Registers: ").append(maxRegisters).append("\n");
sb.append("Bytecode length: ").append(bytecode.length).append(" bytes\n\n");
int pc = 0;
while (pc < bytecode.length) {
int startPc = pc;
byte opcode = bytecode[pc++];
sb.append(String.format("%4d: ", startPc));
switch (opcode) {
case Opcodes.NOP:
sb.append("NOP\n");
break;
case Opcodes.RETURN:
sb.append("RETURN r").append(bytecode[pc++] & 0xFF).append("\n");
break;
case Opcodes.GOTO:
sb.append("GOTO ").append(readInt(bytecode, pc)).append("\n");
pc += 4;
break;
case Opcodes.GOTO_IF_FALSE:
int condReg = bytecode[pc++] & 0xFF;
int target = readInt(bytecode, pc);
pc += 4;
sb.append("GOTO_IF_FALSE r").append(condReg).append(" -> ").append(target).append("\n");
break;
case Opcodes.GOTO_IF_TRUE:
condReg = bytecode[pc++] & 0xFF;
target = readInt(bytecode, pc);
pc += 4;
sb.append("GOTO_IF_TRUE r").append(condReg).append(" -> ").append(target).append("\n");
break;
case Opcodes.MOVE:
int dest = bytecode[pc++] & 0xFF;
int src = bytecode[pc++] & 0xFF;
sb.append("MOVE r").append(dest).append(" = r").append(src).append("\n");
break;
case Opcodes.LOAD_CONST:
int rd = bytecode[pc++] & 0xFF;
int constIdx = bytecode[pc++] & 0xFF;
sb.append("LOAD_CONST r").append(rd).append(" = constants[").append(constIdx).append("]");
if (constants != null && constIdx < constants.length) {
Object obj = constants[constIdx];
sb.append(" (");
if (obj instanceof RuntimeScalar) {
RuntimeScalar scalar = (RuntimeScalar) obj;
sb.append("RuntimeScalar{type=").append(scalar.type).append(", value=").append(scalar.value.getClass().getSimpleName()).append("}");
} else {
sb.append(obj);
}
sb.append(")");
}
sb.append("\n");
break;
case Opcodes.LOAD_INT:
rd = bytecode[pc++] & 0xFF;
int value = readInt(bytecode, pc);
pc += 4;
sb.append("LOAD_INT r").append(rd).append(" = ").append(value).append("\n");
break;
case Opcodes.LOAD_STRING:
rd = bytecode[pc++] & 0xFF;
int strIdx = bytecode[pc++] & 0xFF;
sb.append("LOAD_STRING r").append(rd).append(" = \"");
if (stringPool != null && strIdx < stringPool.length) {
String str = stringPool[strIdx];
// Escape special characters for readability
str = str.replace("\\", "\\\\")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t")
.replace("\"", "\\\"");
sb.append(str);
}
sb.append("\"\n");
break;
case Opcodes.LOAD_UNDEF:
rd = bytecode[pc++] & 0xFF;
sb.append("LOAD_UNDEF r").append(rd).append("\n");
break;
case Opcodes.LOAD_GLOBAL_SCALAR:
rd = bytecode[pc++] & 0xFF;
int nameIdx = bytecode[pc++] & 0xFF;
sb.append("LOAD_GLOBAL_SCALAR r").append(rd).append(" = $").append(stringPool[nameIdx]).append("\n");
break;
case Opcodes.STORE_GLOBAL_SCALAR:
nameIdx = bytecode[pc++] & 0xFF;
int srcReg = bytecode[pc++] & 0xFF;
sb.append("STORE_GLOBAL_SCALAR $").append(stringPool[nameIdx]).append(" = r").append(srcReg).append("\n");
break;
case Opcodes.ADD_SCALAR:
rd = bytecode[pc++] & 0xFF;
int rs1 = bytecode[pc++] & 0xFF;
int rs2 = bytecode[pc++] & 0xFF;
sb.append("ADD_SCALAR r").append(rd).append(" = r").append(rs1).append(" + r").append(rs2).append("\n");
break;
case Opcodes.ADD_SCALAR_INT:
rd = bytecode[pc++] & 0xFF;
int rs = bytecode[pc++] & 0xFF;
int imm = readInt(bytecode, pc);
pc += 4;
sb.append("ADD_SCALAR_INT r").append(rd).append(" = r").append(rs).append(" + ").append(imm).append("\n");
break;
case Opcodes.LT_NUM:
rd = bytecode[pc++] & 0xFF;
rs1 = bytecode[pc++] & 0xFF;
rs2 = bytecode[pc++] & 0xFF;
sb.append("LT_NUM r").append(rd).append(" = r").append(rs1).append(" < r").append(rs2).append("\n");
break;
case Opcodes.GT_NUM:
rd = bytecode[pc++] & 0xFF;
rs1 = bytecode[pc++] & 0xFF;
rs2 = bytecode[pc++] & 0xFF;
sb.append("GT_NUM r").append(rd).append(" = r").append(rs1).append(" > r").append(rs2).append("\n");
break;
case Opcodes.NE_NUM:
rd = bytecode[pc++] & 0xFF;
rs1 = bytecode[pc++] & 0xFF;
rs2 = bytecode[pc++] & 0xFF;
sb.append("NE_NUM r").append(rd).append(" = r").append(rs1).append(" != r").append(rs2).append("\n");
break;
case Opcodes.INC_REG:
rd = bytecode[pc++] & 0xFF;
sb.append("INC_REG r").append(rd).append("++\n");
break;
case Opcodes.DEC_REG:
rd = bytecode[pc++] & 0xFF;
sb.append("DEC_REG r").append(rd).append("--\n");
break;
case Opcodes.ADD_ASSIGN:
rd = bytecode[pc++] & 0xFF;
rs = bytecode[pc++] & 0xFF;
sb.append("ADD_ASSIGN r").append(rd).append(" += r").append(rs).append("\n");
break;
case Opcodes.ADD_ASSIGN_INT:
rd = bytecode[pc++] & 0xFF;
imm = readInt(bytecode, pc);
pc += 4;
sb.append("ADD_ASSIGN_INT r").append(rd).append(" += ").append(imm).append("\n");
break;
case Opcodes.PRE_AUTOINCREMENT:
rd = bytecode[pc++] & 0xFF;
sb.append("PRE_AUTOINCREMENT ++r").append(rd).append("\n");
break;
case Opcodes.POST_AUTOINCREMENT:
rd = bytecode[pc++] & 0xFF;
sb.append("POST_AUTOINCREMENT r").append(rd).append("++\n");
break;
case Opcodes.PRE_AUTODECREMENT:
rd = bytecode[pc++] & 0xFF;
sb.append("PRE_AUTODECREMENT --r").append(rd).append("\n");
break;
case Opcodes.POST_AUTODECREMENT:
rd = bytecode[pc++] & 0xFF;
sb.append("POST_AUTODECREMENT r").append(rd).append("--\n");
break;
case Opcodes.PRINT: {
int contentReg = bytecode[pc++] & 0xFF;
int filehandleReg = bytecode[pc++] & 0xFF;
sb.append("PRINT r").append(contentReg).append(", fh=r").append(filehandleReg).append("\n");
break;
}
case Opcodes.SAY: {
int contentReg = bytecode[pc++] & 0xFF;
int filehandleReg = bytecode[pc++] & 0xFF;
sb.append("SAY r").append(contentReg).append(", fh=r").append(filehandleReg).append("\n");
break;
}
case Opcodes.CREATE_REF:
rd = bytecode[pc++] & 0xFF;
rs = bytecode[pc++] & 0xFF;
sb.append("CREATE_REF r").append(rd).append(" = \\r").append(rs).append("\n");
break;
case Opcodes.DEREF:
rd = bytecode[pc++] & 0xFF;
rs = bytecode[pc++] & 0xFF;
sb.append("DEREF r").append(rd).append(" = ${r").append(rs).append("}\n");
break;
case Opcodes.GET_TYPE:
rd = bytecode[pc++] & 0xFF;
rs = bytecode[pc++] & 0xFF;
sb.append("GET_TYPE r").append(rd).append(" = type(r").append(rs).append(")\n");
break;
case Opcodes.DIE:
rs = bytecode[pc++] & 0xFF;
sb.append("DIE r").append(rs).append("\n");
break;
case Opcodes.WARN:
rs = bytecode[pc++] & 0xFF;
sb.append("WARN r").append(rs).append("\n");
break;
case Opcodes.EVAL_TRY: {
int catchOffsetHigh = bytecode[pc++] & 0xFF;
int catchOffsetLow = bytecode[pc++] & 0xFF;
int catchOffset = (catchOffsetHigh << 8) | catchOffsetLow;
int tryPc = pc - 3;
int catchPc = tryPc + catchOffset;
sb.append("EVAL_TRY catch_at=").append(catchPc).append("\n");
break;
}
case Opcodes.EVAL_END:
sb.append("EVAL_END\n");
break;
case Opcodes.EVAL_CATCH:
rd = bytecode[pc++] & 0xFF;
sb.append("EVAL_CATCH r").append(rd).append("\n");
break;
case Opcodes.ARRAY_GET:
rd = bytecode[pc++] & 0xFF;
int arrayReg = bytecode[pc++] & 0xFF;
int indexReg = bytecode[pc++] & 0xFF;
sb.append("ARRAY_GET r").append(rd).append(" = r").append(arrayReg).append("[r").append(indexReg).append("]\n");
break;
case Opcodes.ARRAY_SIZE:
rd = bytecode[pc++] & 0xFF;
arrayReg = bytecode[pc++] & 0xFF;
sb.append("ARRAY_SIZE r").append(rd).append(" = size(r").append(arrayReg).append(")\n");
break;
case Opcodes.CREATE_ARRAY:
rd = bytecode[pc++] & 0xFF;
int listSourceReg = bytecode[pc++] & 0xFF;
sb.append("CREATE_ARRAY r").append(rd).append(" = array(r").append(listSourceReg).append(")\n");
break;
case Opcodes.HASH_GET:
rd = bytecode[pc++] & 0xFF;
int hashGetReg = bytecode[pc++] & 0xFF;
int keyGetReg = bytecode[pc++] & 0xFF;
sb.append("HASH_GET r").append(rd).append(" = r").append(hashGetReg).append("{r").append(keyGetReg).append("}\n");
break;
case Opcodes.HASH_SET:
int hashSetReg = bytecode[pc++] & 0xFF;
int keySetReg = bytecode[pc++] & 0xFF;
int valueSetReg = bytecode[pc++] & 0xFF;
sb.append("HASH_SET r").append(hashSetReg).append("{r").append(keySetReg).append("} = r").append(valueSetReg).append("\n");
break;
case Opcodes.HASH_EXISTS:
rd = bytecode[pc++] & 0xFF;
int hashExistsReg = bytecode[pc++] & 0xFF;
int keyExistsReg = bytecode[pc++] & 0xFF;
sb.append("HASH_EXISTS r").append(rd).append(" = exists r").append(hashExistsReg).append("{r").append(keyExistsReg).append("}\n");
break;
case Opcodes.HASH_DELETE:
rd = bytecode[pc++] & 0xFF;
int hashDeleteReg = bytecode[pc++] & 0xFF;
int keyDeleteReg = bytecode[pc++] & 0xFF;
sb.append("HASH_DELETE r").append(rd).append(" = delete r").append(hashDeleteReg).append("{r").append(keyDeleteReg).append("}\n");
break;
case Opcodes.HASH_KEYS:
rd = bytecode[pc++] & 0xFF;
int hashKeysReg = bytecode[pc++] & 0xFF;
sb.append("HASH_KEYS r").append(rd).append(" = keys(r").append(hashKeysReg).append(")\n");
break;
case Opcodes.HASH_VALUES:
rd = bytecode[pc++] & 0xFF;
int hashValuesReg = bytecode[pc++] & 0xFF;
sb.append("HASH_VALUES r").append(rd).append(" = values(r").append(hashValuesReg).append(")\n");
break;
case Opcodes.CREATE_LIST: {
rd = bytecode[pc++] & 0xFF;
int listCount = bytecode[pc++] & 0xFF;
sb.append("CREATE_LIST r").append(rd).append(" = [");
for (int i = 0; i < listCount; i++) {
if (i > 0) sb.append(", ");
int listRs = bytecode[pc++] & 0xFF;
sb.append("r").append(listRs);
}
sb.append("]\n");
break;
}
case Opcodes.CALL_SUB:
rd = bytecode[pc++] & 0xFF;
int coderefReg = bytecode[pc++] & 0xFF;
int argsReg = bytecode[pc++] & 0xFF;
int ctx = bytecode[pc++] & 0xFF;
sb.append("CALL_SUB r").append(rd).append(" = r").append(coderefReg)
.append("->(r").append(argsReg).append(", ctx=").append(ctx).append(")\n");
break;
case Opcodes.JOIN:
rd = bytecode[pc++] & 0xFF;
int separatorReg = bytecode[pc++] & 0xFF;
int listReg = bytecode[pc++] & 0xFF;
sb.append("JOIN r").append(rd).append(" = join(r").append(separatorReg)
.append(", r").append(listReg).append(")\n");
break;
case Opcodes.SELECT:
rd = bytecode[pc++] & 0xFF;
listReg = bytecode[pc++] & 0xFF;
sb.append("SELECT r").append(rd).append(" = select(r").append(listReg).append(")\n");
break;
case Opcodes.RANGE:
rd = bytecode[pc++] & 0xFF;
int startReg = bytecode[pc++] & 0xFF;
int endReg = bytecode[pc++] & 0xFF;
sb.append("RANGE r").append(rd).append(" = r").append(startReg).append("..r").append(endReg).append("\n");
break;
case Opcodes.CREATE_HASH:
rd = bytecode[pc++] & 0xFF;
int hashListReg = bytecode[pc++] & 0xFF;
sb.append("CREATE_HASH r").append(rd).append(" = hash_ref(r").append(hashListReg).append(")\n");
break;
case Opcodes.RAND:
rd = bytecode[pc++] & 0xFF;
int maxReg = bytecode[pc++] & 0xFF;
sb.append("RAND r").append(rd).append(" = rand(r").append(maxReg).append(")\n");
break;
case Opcodes.MAP:
rd = bytecode[pc++] & 0xFF;
rs1 = bytecode[pc++] & 0xFF; // list register
rs2 = bytecode[pc++] & 0xFF; // closure register
int mapCtx = bytecode[pc++] & 0xFF; // context
sb.append("MAP r").append(rd).append(" = map(r").append(rs1)
.append(", r").append(rs2).append(", ctx=").append(mapCtx).append(")\n");
break;
case Opcodes.NEW_ARRAY:
rd = bytecode[pc++] & 0xFF;
sb.append("NEW_ARRAY r").append(rd).append(" = new RuntimeArray()\n");
break;
case Opcodes.NEW_HASH:
rd = bytecode[pc++] & 0xFF;
sb.append("NEW_HASH r").append(rd).append(" = new RuntimeHash()\n");
break;
case Opcodes.ARRAY_SET_FROM_LIST:
rs1 = bytecode[pc++] & 0xFF; // array register
rs2 = bytecode[pc++] & 0xFF; // list register
sb.append("ARRAY_SET_FROM_LIST r").append(rs1).append(".setFromList(r").append(rs2).append(")\n");
break;
case Opcodes.HASH_SET_FROM_LIST:
rs1 = bytecode[pc++] & 0xFF; // hash register
rs2 = bytecode[pc++] & 0xFF; // list register
sb.append("HASH_SET_FROM_LIST r").append(rs1).append(".setFromList(r").append(rs2).append(")\n");
break;
case Opcodes.STORE_GLOBAL_CODE:
int codeNameIdx = bytecode[pc++] & 0xFF;
rs = bytecode[pc++] & 0xFF;
sb.append("STORE_GLOBAL_CODE '").append(stringPool[codeNameIdx]).append("' = r").append(rs).append("\n");
break;
case Opcodes.CREATE_CLOSURE:
rd = bytecode[pc++] & 0xFF;
int templateIdx = bytecode[pc++] & 0xFF;
int numCaptures = bytecode[pc++] & 0xFF;
sb.append("CREATE_CLOSURE r").append(rd).append(" = closure(template[").append(templateIdx).append("], captures=[");
for (int i = 0; i < numCaptures; i++) {
if (i > 0) sb.append(", ");
int captureReg = bytecode[pc++] & 0xFF;
sb.append("r").append(captureReg);
}
sb.append("])\n");
break;
case Opcodes.NOT:
rd = bytecode[pc++] & 0xFF;
rs = bytecode[pc++] & 0xFF;
sb.append("NOT r").append(rd).append(" = !r").append(rs).append("\n");
break;
case Opcodes.SLOW_OP: {
int slowOpId = bytecode[pc++] & 0xFF;
String opName = SlowOpcodeHandler.getSlowOpName(slowOpId);
sb.append("SLOW_OP ").append(opName).append(" (id=").append(slowOpId).append(")");
// Decode operands for known SLOW_OPs
switch (slowOpId) {
case Opcodes.SLOWOP_EVAL_STRING:
// Format: [rd] [rs_string]
rd = bytecode[pc++] & 0xFF;
rs = bytecode[pc++] & 0xFF;
sb.append(" r").append(rd).append(" = eval(r").append(rs).append(")");
break;
case Opcodes.SLOWOP_SELECT:
// Format: [rd] [rs_list]
rd = bytecode[pc++] & 0xFF;
rs = bytecode[pc++] & 0xFF;
sb.append(" r").append(rd).append(" = select(r").append(rs).append(")");
break;
case Opcodes.SLOWOP_LOAD_GLOB:
// Format: [rd] [name_idx]
rd = bytecode[pc++] & 0xFF;
int globNameIdx = bytecode[pc++] & 0xFF;
String globName = stringPool[globNameIdx];
sb.append(" r").append(rd).append(" = *").append(globName);
break;
case Opcodes.SLOWOP_RETRIEVE_BEGIN_SCALAR:
case Opcodes.SLOWOP_RETRIEVE_BEGIN_ARRAY:
case Opcodes.SLOWOP_RETRIEVE_BEGIN_HASH:
// Format: [rd] [name_idx] [begin_id]
rd = bytecode[pc++] & 0xFF;
int varNameIdx = bytecode[pc++] & 0xFF;
int beginId = bytecode[pc++] & 0xFF;
String varName = stringPool[varNameIdx];
sb.append(" r").append(rd).append(" = ").append(varName)
.append(" (BEGIN_").append(beginId).append(")");
break;
case Opcodes.SLOWOP_LOCAL_SCALAR:
// Format: [rd] [name_idx]
rd = bytecode[pc++] & 0xFF;
int localNameIdx = bytecode[pc++] & 0xFF;
String localVarName = stringPool[localNameIdx];
sb.append(" r").append(rd).append(" = local ").append(localVarName);
break;
default:
sb.append(" (operands not decoded)");
break;
}
sb.append("\n");
break;
}
default:
sb.append("UNKNOWN(").append(opcode & 0xFF).append(")\n");
break;
}
}
return sb.toString();
}
private static int readInt(byte[] bytecode, int pc) {
return ((bytecode[pc] & 0xFF) << 24) |
((bytecode[pc + 1] & 0xFF) << 16) |
((bytecode[pc + 2] & 0xFF) << 8) |
(bytecode[pc + 3] & 0xFF);
}
/**
* Builder class for constructing InterpretedCode instances.
*/
public static class Builder {
private byte[] bytecode;
private Object[] constants = new Object[0];
private String[] stringPool = new String[0];
private int maxRegisters = 10;
private RuntimeBase[] capturedVars = null;
private String sourceName = "<eval>";
private int sourceLine = 1;
public Builder bytecode(byte[] bytecode) {
this.bytecode = bytecode;
return this;
}
public Builder constants(Object[] constants) {
this.constants = constants;
return this;
}
public Builder stringPool(String[] stringPool) {
this.stringPool = stringPool;
return this;
}
public Builder maxRegisters(int maxRegisters) {
this.maxRegisters = maxRegisters;
return this;
}
public Builder capturedVars(RuntimeBase[] capturedVars) {
this.capturedVars = capturedVars;
return this;
}
public Builder sourceName(String sourceName) {
this.sourceName = sourceName;
return this;
}
public Builder sourceLine(int sourceLine) {
this.sourceLine = sourceLine;
return this;
}
public InterpretedCode build() {
if (bytecode == null) {
throw new IllegalStateException("Bytecode is required");
}
return new InterpretedCode(bytecode, constants, stringPool, maxRegisters,
capturedVars, sourceName, sourceLine, null);
}
}
}