-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCompileExistsDelete.java
More file actions
509 lines (486 loc) · 21.5 KB
/
CompileExistsDelete.java
File metadata and controls
509 lines (486 loc) · 21.5 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
package org.perlonjava.backend.bytecode;
import org.perlonjava.frontend.astnode.*;
import org.perlonjava.runtime.runtimetypes.NameNormalizer;
import org.perlonjava.runtime.runtimetypes.RuntimeContextType;
import java.util.ArrayList;
import java.util.List;
public class CompileExistsDelete {
public static void visitExists(BytecodeCompiler bc, OperatorNode node) {
if (node.operand == null || !(node.operand instanceof ListNode list) || list.elements.isEmpty()) {
bc.throwCompilerException("exists requires an argument");
return;
}
Node arg = list.elements.get(0);
if (arg instanceof BinaryOperatorNode binOp) {
switch (binOp.operator) {
case "{" -> visitExistsHash(bc, node, binOp);
case "[" -> visitExistsArray(bc, node, binOp);
case "->" -> visitExistsArrow(bc, node, binOp);
default -> visitExistsGeneric(bc, arg);
}
} else {
visitExistsGeneric(bc, arg);
}
}
private static void visitExistsHash(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess) {
int hashReg = resolveHashFromBinaryOp(bc, hashAccess, node.getIndex());
int keyReg = compileHashKey(bc, hashAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_EXISTS);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
}
private static void visitExistsArray(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess) {
int arrayReg = compileArrayForExistsDelete(bc, arrayAccess, node.getIndex());
int indexReg = compileArrayIndex(bc, arrayAccess);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_EXISTS);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
}
private static void visitExistsArrow(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrowAccess) {
if (arrowAccess.right instanceof HashLiteralNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int hashReg = derefHash(bc, refReg, node.getIndex());
int keyReg = compileHashKey(bc, arrowAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_EXISTS);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
} else if (arrowAccess.right instanceof ArrayLiteralNode indexNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int arrayReg = derefArray(bc, refReg, node.getIndex());
if (indexNode.elements.isEmpty()) {
bc.throwCompilerException("Array index required for exists");
return;
}
// Compile index in SCALAR context
bc.compileNode(indexNode.elements.get(0), -1, RuntimeContextType.SCALAR);
int indexReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_EXISTS);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
} else {
visitExistsGeneric(bc, arrowAccess);
}
}
private static void visitExistsGeneric(BytecodeCompiler bc, Node arg) {
arg.accept(bc);
int argReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.EXISTS);
bc.emitReg(rd);
bc.emitReg(argReg);
bc.lastResultReg = rd;
}
public static void visitDelete(BytecodeCompiler bc, OperatorNode node) {
if (node.operand == null || !(node.operand instanceof ListNode list) || list.elements.isEmpty()) {
bc.throwCompilerException("delete requires an argument");
return;
}
Node arg = list.elements.get(0);
if (arg instanceof BinaryOperatorNode binOp) {
switch (binOp.operator) {
case "{" -> visitDeleteHash(bc, node, binOp);
case "[" -> visitDeleteArray(bc, node, binOp);
case "->" -> visitDeleteArrow(bc, node, binOp);
default -> visitDeleteGeneric(bc, arg);
}
} else {
visitDeleteGeneric(bc, arg);
}
}
private static void visitDeleteHash(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess) {
if (hashAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("@")) {
visitDeleteHashSlice(bc, node, hashAccess, leftOp);
return;
}
if (hashAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("%")) {
visitDeleteHashKVSlice(bc, node, hashAccess, leftOp);
return;
}
int hashReg = resolveHashFromBinaryOp(bc, hashAccess, node.getIndex());
int keyReg = compileHashKey(bc, hashAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_DELETE);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
}
private static void visitDeleteHashSlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess, OperatorNode leftOp) {
int hashReg;
if (leftOp.operand instanceof IdentifierNode id) {
String hashVarName = "%" + id.name;
if (bc.hasVariable(hashVarName)) {
hashReg = bc.getVariableRegister(hashVarName);
} else {
hashReg = bc.allocateRegister();
String globalHashName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalHashName);
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(hashReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Hash slice delete requires identifier");
return;
}
if (!(hashAccess.right instanceof HashLiteralNode keysNode)) {
bc.throwCompilerException("Hash slice delete requires HashLiteralNode");
return;
}
List<Integer> keyRegs = new ArrayList<>();
for (Node keyElement : keysNode.elements) {
if (keyElement instanceof IdentifierNode keyId) {
int keyReg = bc.allocateRegister();
int keyIdx = bc.addToStringPool(keyId.name);
bc.emit(Opcodes.LOAD_STRING);
bc.emitReg(keyReg);
bc.emit(keyIdx);
keyRegs.add(keyReg);
} else {
// Compile key in SCALAR context
bc.compileNode(keyElement, -1, RuntimeContextType.SCALAR);
keyRegs.add(bc.lastResultReg);
}
}
int keysListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(keysListReg);
bc.emit(keyRegs.size());
for (int keyReg : keyRegs) {
bc.emitReg(keyReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_SLICE_DELETE);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keysListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteHashKVSlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess, OperatorNode leftOp) {
int hashReg;
if (leftOp.operand instanceof IdentifierNode id) {
String hashVarName = "%" + id.name;
if (bc.hasVariable(hashVarName)) {
hashReg = bc.getVariableRegister(hashVarName);
} else {
hashReg = bc.allocateRegister();
String globalHashName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalHashName);
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(hashReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Hash kv-slice delete requires identifier");
return;
}
if (!(hashAccess.right instanceof HashLiteralNode keysNode)) {
bc.throwCompilerException("Hash kv-slice delete requires HashLiteralNode");
return;
}
List<Integer> keyRegs = new ArrayList<>();
for (Node keyElement : keysNode.elements) {
if (keyElement instanceof IdentifierNode keyId) {
int keyReg = bc.allocateRegister();
int keyIdx = bc.addToStringPool(keyId.name);
bc.emit(Opcodes.LOAD_STRING);
bc.emitReg(keyReg);
bc.emit(keyIdx);
keyRegs.add(keyReg);
} else {
// Compile key in SCALAR context
bc.compileNode(keyElement, -1, RuntimeContextType.SCALAR);
keyRegs.add(bc.lastResultReg);
}
}
int keysListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(keysListReg);
bc.emit(keyRegs.size());
for (int keyReg : keyRegs) {
bc.emitReg(keyReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_KV_SLICE_DELETE);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keysListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteArray(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess) {
if (arrayAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("@")) {
visitDeleteArraySlice(bc, node, arrayAccess, leftOp);
return;
}
if (arrayAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("%")) {
visitDeleteArrayKVSlice(bc, node, arrayAccess, leftOp);
return;
}
int arrayReg = compileArrayForExistsDelete(bc, arrayAccess, node.getIndex());
int indexReg = compileArrayIndex(bc, arrayAccess);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_DELETE);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
}
private static void visitDeleteArraySlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess, OperatorNode leftOp) {
int arrayReg;
if (leftOp.operand instanceof IdentifierNode id) {
String arrayVarName = "@" + id.name;
if (bc.hasVariable(arrayVarName)) {
arrayReg = bc.getVariableRegister(arrayVarName);
} else {
arrayReg = bc.allocateRegister();
String globalArrayName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalArrayName);
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Array slice delete requires identifier");
return;
}
if (!(arrayAccess.right instanceof ArrayLiteralNode indicesNode)) {
bc.throwCompilerException("Array slice delete requires ArrayLiteralNode");
return;
}
List<Integer> indexRegs = new ArrayList<>();
for (Node indexElement : indicesNode.elements) {
// Compile index in SCALAR context
bc.compileNode(indexElement, -1, RuntimeContextType.SCALAR);
indexRegs.add(bc.lastResultReg);
}
int indicesListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(indicesListReg);
bc.emit(indexRegs.size());
for (int indexReg : indexRegs) {
bc.emitReg(indexReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_SLICE_DELETE);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indicesListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteArrayKVSlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess, OperatorNode leftOp) {
int arrayReg;
if (leftOp.operand instanceof IdentifierNode id) {
String arrayVarName = "@" + id.name;
if (bc.hasVariable(arrayVarName)) {
arrayReg = bc.getVariableRegister(arrayVarName);
} else {
arrayReg = bc.allocateRegister();
String globalArrayName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalArrayName);
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Array kv-slice delete requires identifier");
return;
}
if (!(arrayAccess.right instanceof ArrayLiteralNode indicesNode)) {
bc.throwCompilerException("Array kv-slice delete requires ArrayLiteralNode");
return;
}
List<Integer> indexRegs = new ArrayList<>();
for (Node indexElement : indicesNode.elements) {
// Compile index in SCALAR context
bc.compileNode(indexElement, -1, RuntimeContextType.SCALAR);
indexRegs.add(bc.lastResultReg);
}
int indicesListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(indicesListReg);
bc.emit(indexRegs.size());
for (int indexReg : indexRegs) {
bc.emitReg(indexReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_KV_SLICE_DELETE);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indicesListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteArrow(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrowAccess) {
if (arrowAccess.right instanceof HashLiteralNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int hashReg = derefHash(bc, refReg, node.getIndex());
int keyReg = compileHashKey(bc, arrowAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_DELETE);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
} else if (arrowAccess.right instanceof ArrayLiteralNode indexNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int arrayReg = derefArray(bc, refReg, node.getIndex());
if (indexNode.elements.isEmpty()) {
bc.throwCompilerException("Array index required for delete");
return;
}
// Compile index in SCALAR context
bc.compileNode(indexNode.elements.get(0), -1, RuntimeContextType.SCALAR);
int indexReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_DELETE);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
} else {
visitDeleteGeneric(bc, arrowAccess);
}
}
private static void visitDeleteGeneric(BytecodeCompiler bc, Node arg) {
arg.accept(bc);
int argReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.DELETE);
bc.emitReg(rd);
bc.emitReg(argReg);
bc.lastResultReg = rd;
}
private static int resolveHashFromBinaryOp(BytecodeCompiler bc, BinaryOperatorNode hashAccess, int tokenIndex) {
if (hashAccess.left instanceof OperatorNode leftOp) {
if (leftOp.operator.equals("$") && leftOp.operand instanceof IdentifierNode id) {
String hashVarName = "%" + id.name;
if (bc.hasVariable(hashVarName)) {
return bc.getVariableRegister(hashVarName);
}
int hashReg = bc.allocateRegister();
String globalHashName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalHashName);
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(hashReg);
bc.emit(nameIdx);
return hashReg;
} else {
// Compile operand in SCALAR context to ensure we get a result register
bc.compileNode(leftOp.operand, -1, RuntimeContextType.SCALAR);
int scalarReg = bc.lastResultReg;
return derefHash(bc, scalarReg, tokenIndex);
}
} else if (hashAccess.left instanceof BinaryOperatorNode) {
// Compile in SCALAR context to ensure we get a result register
bc.compileNode(hashAccess.left, -1, RuntimeContextType.SCALAR);
int scalarReg = bc.lastResultReg;
return derefHash(bc, scalarReg, tokenIndex);
}
bc.throwCompilerException("Hash access requires variable or expression on left side");
return -1;
}
private static int derefHash(BytecodeCompiler bc, int scalarReg, int tokenIndex) {
int hashReg = bc.allocateRegister();
if (bc.isStrictRefsEnabled()) {
bc.emitWithToken(Opcodes.DEREF_HASH, tokenIndex);
bc.emitReg(hashReg);
bc.emitReg(scalarReg);
} else {
int pkgIdx = bc.addToStringPool(bc.getCurrentPackage());
bc.emitWithToken(Opcodes.DEREF_HASH_NONSTRICT, tokenIndex);
bc.emitReg(hashReg);
bc.emitReg(scalarReg);
bc.emit(pkgIdx);
}
return hashReg;
}
private static int derefArray(BytecodeCompiler bc, int scalarReg, int tokenIndex) {
int arrayReg = bc.allocateRegister();
if (bc.isStrictRefsEnabled()) {
bc.emitWithToken(Opcodes.DEREF_ARRAY, tokenIndex);
bc.emitReg(arrayReg);
bc.emitReg(scalarReg);
} else {
int pkgIdx = bc.addToStringPool(bc.getCurrentPackage());
bc.emitWithToken(Opcodes.DEREF_ARRAY_NONSTRICT, tokenIndex);
bc.emitReg(arrayReg);
bc.emitReg(scalarReg);
bc.emit(pkgIdx);
}
return arrayReg;
}
private static int compileHashKey(BytecodeCompiler bc, Node keySpec) {
if (keySpec instanceof HashLiteralNode keyNode && !keyNode.elements.isEmpty()) {
Node keyElement = keyNode.elements.get(0);
if (keyElement instanceof IdentifierNode id) {
int keyReg = bc.allocateRegister();
int keyIdx = bc.addToStringPool(id.name);
bc.emit(Opcodes.LOAD_STRING);
bc.emitReg(keyReg);
bc.emit(keyIdx);
return keyReg;
} else {
// Compile in scalar context to ensure we get a RuntimeScalar
bc.compileNode(keyElement, -1, RuntimeContextType.SCALAR);
return bc.lastResultReg;
}
} else {
// Compile in scalar context to ensure we get a RuntimeScalar
bc.compileNode(keySpec, -1, RuntimeContextType.SCALAR);
return bc.lastResultReg;
}
}
private static int compileArrayForExistsDelete(BytecodeCompiler bc, BinaryOperatorNode arrayAccess, int tokenIndex) {
if (!(arrayAccess.left instanceof OperatorNode leftOp) || !leftOp.operator.equals("$")
|| !(leftOp.operand instanceof IdentifierNode)) {
bc.throwCompilerException("Array exists/delete requires simple array variable");
return -1;
}
String varName = ((IdentifierNode) leftOp.operand).name;
String arrayVarName = "@" + varName;
if (bc.currentSubroutineBeginId != 0 && bc.currentSubroutineClosureVars != null
&& bc.currentSubroutineClosureVars.contains(arrayVarName)) {
int arrayReg = bc.allocateRegister();
int nameIdx = bc.addToStringPool(arrayVarName);
bc.emitWithToken(Opcodes.RETRIEVE_BEGIN_ARRAY, tokenIndex);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
bc.emit(bc.currentSubroutineBeginId);
return arrayReg;
} else if (bc.hasVariable(arrayVarName)) {
return bc.getVariableRegister(arrayVarName);
} else {
int arrayReg = bc.allocateRegister();
String globalArrayName = NameNormalizer.normalizeVariableName(varName, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalArrayName);
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
return arrayReg;
}
}
private static int compileArrayIndex(BytecodeCompiler bc, BinaryOperatorNode arrayAccess) {
if (!(arrayAccess.right instanceof ArrayLiteralNode indexNode) || indexNode.elements.isEmpty()) {
bc.throwCompilerException("Array exists/delete requires index");
return -1;
}
// Compile in scalar context to ensure we get a RuntimeScalar
bc.compileNode(indexNode.elements.get(0), -1, RuntimeContextType.SCALAR);
return bc.lastResultReg;
}
}