-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEmitBinaryOperator.java
More file actions
344 lines (308 loc) · 16.5 KB
/
EmitBinaryOperator.java
File metadata and controls
344 lines (308 loc) · 16.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
package org.perlonjava.backend.jvm;
import org.perlonjava.app.cli.CompilerOptions;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.perlonjava.frontend.analysis.EmitterVisitor;
import org.perlonjava.frontend.astnode.BinaryOperatorNode;
import org.perlonjava.frontend.astnode.IdentifierNode;
import org.perlonjava.frontend.astnode.NumberNode;
import org.perlonjava.frontend.astnode.StringNode;
import org.perlonjava.runtime.operators.OperatorHandler;
import org.perlonjava.runtime.perlmodule.Strict;
import org.perlonjava.runtime.runtimetypes.RuntimeContextType;
import org.perlonjava.runtime.runtimetypes.ScalarUtils;
import static org.perlonjava.backend.jvm.EmitOperator.emitOperator;
public class EmitBinaryOperator {
static final boolean ENABLE_SPILL_BINARY_LHS = true;
static void handleBinaryOperator(EmitterVisitor emitterVisitor, BinaryOperatorNode node, OperatorHandler operatorHandler) {
EmitterVisitor scalarVisitor =
emitterVisitor.with(RuntimeContextType.SCALAR); // execute operands in scalar context
if (CompilerOptions.DEBUG_ENABLED) emitterVisitor.ctx.logDebug("handleBinaryOperator: " + node.toString());
// Optimization
if ((node.operator.equals("+")
|| node.operator.equals("-")
|| node.operator.equals("=="))
&& node.right instanceof NumberNode right) {
String value = right.value;
boolean isInteger = ScalarUtils.isInteger(value);
if (isInteger) {
node.left.accept(scalarVisitor); // target - left parameter
int intValue = Integer.parseInt(value);
emitterVisitor.ctx.mv.visitLdcInsn(intValue);
emitterVisitor.ctx.mv.visitMethodInsn(
operatorHandler.methodType(),
operatorHandler.className(),
operatorHandler.methodName(),
operatorHandler.getDescriptorWithIntParameter(),
false);
EmitOperator.handleVoidContext(emitterVisitor);
return;
}
}
var right = node.right;
// Special case for `isa` - left side can be bareword
if (node.operator.equals("isa") && right instanceof IdentifierNode identifierNode) {
right = new StringNode(identifierNode.name, node.tokenIndex);
}
// Special case for modulus, division, and shift operators under "use integer"
if (emitterVisitor.ctx.symbolTable.isStrictOptionEnabled(Strict.HINT_INTEGER)) {
if (node.operator.equals("%")) {
// Use integer modulus when "use integer" is in effect
MethodVisitor mv = emitterVisitor.ctx.mv;
if (ENABLE_SPILL_BINARY_LHS) {
node.left.accept(scalarVisitor);
int leftSlot = emitterVisitor.ctx.javaClassInfo.acquireSpillSlot();
boolean pooled = leftSlot >= 0;
if (!pooled) {
leftSlot = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
}
mv.visitVarInsn(Opcodes.ASTORE, leftSlot);
right.accept(scalarVisitor);
mv.visitVarInsn(Opcodes.ALOAD, leftSlot);
mv.visitInsn(Opcodes.SWAP);
if (pooled) {
emitterVisitor.ctx.javaClassInfo.releaseSpillSlot();
}
} else {
node.left.accept(scalarVisitor); // left parameter
right.accept(scalarVisitor); // right parameter
}
emitterVisitor.ctx.mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
"org/perlonjava/runtime/operators/MathOperators",
"integerModulus",
"(Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;)Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;",
false);
EmitOperator.handleVoidContext(emitterVisitor);
return;
} else if (node.operator.equals("/")) {
// Use integer division when "use integer" is in effect
MethodVisitor mv = emitterVisitor.ctx.mv;
if (ENABLE_SPILL_BINARY_LHS) {
node.left.accept(scalarVisitor);
int leftSlot = emitterVisitor.ctx.javaClassInfo.acquireSpillSlot();
boolean pooled = leftSlot >= 0;
if (!pooled) {
leftSlot = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
}
mv.visitVarInsn(Opcodes.ASTORE, leftSlot);
right.accept(scalarVisitor);
mv.visitVarInsn(Opcodes.ALOAD, leftSlot);
mv.visitInsn(Opcodes.SWAP);
if (pooled) {
emitterVisitor.ctx.javaClassInfo.releaseSpillSlot();
}
} else {
node.left.accept(scalarVisitor); // left parameter
right.accept(scalarVisitor); // right parameter
}
emitterVisitor.ctx.mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
"org/perlonjava/runtime/operators/MathOperators",
"integerDivide",
"(Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;)Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;",
false);
EmitOperator.handleVoidContext(emitterVisitor);
return;
} else if (node.operator.equals("<<")) {
// Use integer left shift when "use integer" is in effect
MethodVisitor mv = emitterVisitor.ctx.mv;
if (ENABLE_SPILL_BINARY_LHS) {
node.left.accept(scalarVisitor);
int leftSlot = emitterVisitor.ctx.javaClassInfo.acquireSpillSlot();
boolean pooled = leftSlot >= 0;
if (!pooled) {
leftSlot = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
}
mv.visitVarInsn(Opcodes.ASTORE, leftSlot);
right.accept(scalarVisitor);
mv.visitVarInsn(Opcodes.ALOAD, leftSlot);
mv.visitInsn(Opcodes.SWAP);
if (pooled) {
emitterVisitor.ctx.javaClassInfo.releaseSpillSlot();
}
} else {
node.left.accept(scalarVisitor); // left parameter
right.accept(scalarVisitor); // right parameter
}
emitterVisitor.ctx.mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
"org/perlonjava/runtime/operators/BitwiseOperators",
"integerShiftLeft",
"(Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;)Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;",
false);
EmitOperator.handleVoidContext(emitterVisitor);
return;
} else if (node.operator.equals(">>")) {
// Use integer right shift when "use integer" is in effect
MethodVisitor mv = emitterVisitor.ctx.mv;
if (ENABLE_SPILL_BINARY_LHS) {
node.left.accept(scalarVisitor);
int leftSlot = emitterVisitor.ctx.javaClassInfo.acquireSpillSlot();
boolean pooled = leftSlot >= 0;
if (!pooled) {
leftSlot = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
}
mv.visitVarInsn(Opcodes.ASTORE, leftSlot);
right.accept(scalarVisitor);
mv.visitVarInsn(Opcodes.ALOAD, leftSlot);
mv.visitInsn(Opcodes.SWAP);
if (pooled) {
emitterVisitor.ctx.javaClassInfo.releaseSpillSlot();
}
} else {
node.left.accept(scalarVisitor); // left parameter
right.accept(scalarVisitor); // right parameter
}
emitterVisitor.ctx.mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
"org/perlonjava/runtime/operators/BitwiseOperators",
"integerShiftRight",
"(Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;)Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;",
false);
EmitOperator.handleVoidContext(emitterVisitor);
return;
}
}
MethodVisitor mv = emitterVisitor.ctx.mv;
node.left.accept(scalarVisitor); // left parameter
int leftSlot = emitterVisitor.ctx.javaClassInfo.acquireSpillSlot();
boolean pooled = leftSlot >= 0;
if (!pooled) {
leftSlot = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
}
mv.visitVarInsn(Opcodes.ASTORE, leftSlot);
right.accept(scalarVisitor); // right parameter
mv.visitVarInsn(Opcodes.ALOAD, leftSlot);
mv.visitInsn(Opcodes.SWAP);
if (pooled) {
emitterVisitor.ctx.javaClassInfo.releaseSpillSlot();
}
// stack: [left, right]
emitOperator(node, emitterVisitor);
}
static void handleCompoundAssignment(EmitterVisitor emitterVisitor, BinaryOperatorNode node) {
// Compound assignment operators like `+=`, `-=`, etc.
// These now have proper overload support via MathOperators.*Assign() methods
// Operators that SHOULD warn for uninitialized: * / ** << >> x &
// Operators that should NOT warn: + - . | ^ && ||
boolean shouldUseWarnVariant = switch (node.operator) {
case "*=", "/=", "%=", "**=", "<<=", ">>=", "x=", "&=" -> true;
default -> false;
};
// Check if we have an operator handler for this compound operator
// Under "use integer", use the integer warn variant for /=
boolean isInteger = emitterVisitor.ctx.symbolTable.isStrictOptionEnabled(Strict.HINT_INTEGER);
OperatorHandler operatorHandler;
if (shouldUseWarnVariant && isInteger && node.operator.equals("/=")) {
operatorHandler = OperatorHandler.get("/=_int_warn");
} else {
operatorHandler = shouldUseWarnVariant
? OperatorHandler.getWarn(node.operator)
: OperatorHandler.get(node.operator);
}
if (operatorHandler != null) {
// Use the new *Assign methods which check for compound overloads first
EmitterVisitor scalarVisitor =
emitterVisitor.with(RuntimeContextType.SCALAR);
MethodVisitor mv = emitterVisitor.ctx.mv;
// We need to properly handle the lvalue by using spill slots
// This ensures the same object is both read and written
node.left.accept(scalarVisitor); // target - left parameter
int leftSlot = emitterVisitor.ctx.javaClassInfo.acquireSpillSlot();
boolean pooledLeft = leftSlot >= 0;
if (!pooledLeft) {
leftSlot = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
}
mv.visitVarInsn(Opcodes.ASTORE, leftSlot);
node.right.accept(scalarVisitor); // right parameter
mv.visitVarInsn(Opcodes.ALOAD, leftSlot);
mv.visitInsn(Opcodes.SWAP); // swap so args are in right order (left, right)
if (pooledLeft) {
emitterVisitor.ctx.javaClassInfo.releaseSpillSlot();
}
// Call the *Assign method (e.g., MathOperators.addAssign)
// This modifies arg1 in place and returns it
mv.visitMethodInsn(
operatorHandler.methodType(),
operatorHandler.className(),
operatorHandler.methodName(),
operatorHandler.descriptor(),
false);
EmitOperator.handleVoidContext(emitterVisitor);
} else {
// Fallback for operators that don't have handlers yet (e.g., **=, <<=, etc.)
// Use the old approach: strip = and call base operator, then assign
EmitterVisitor scalarVisitor =
emitterVisitor.with(RuntimeContextType.SCALAR); // execute operands in scalar context
MethodVisitor mv = emitterVisitor.ctx.mv;
node.left.accept(scalarVisitor); // target - left parameter
int leftSlot = emitterVisitor.ctx.javaClassInfo.acquireSpillSlot();
boolean pooledLeft = leftSlot >= 0;
if (!pooledLeft) {
leftSlot = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
}
mv.visitVarInsn(Opcodes.ASTORE, leftSlot);
node.right.accept(scalarVisitor); // right parameter
int rightSlot = emitterVisitor.ctx.javaClassInfo.acquireSpillSlot();
boolean pooledRight = rightSlot >= 0;
if (!pooledRight) {
rightSlot = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
}
mv.visitVarInsn(Opcodes.ASTORE, rightSlot);
mv.visitVarInsn(Opcodes.ALOAD, leftSlot);
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(Opcodes.ALOAD, rightSlot);
if (pooledRight) {
emitterVisitor.ctx.javaClassInfo.releaseSpillSlot();
}
if (pooledLeft) {
emitterVisitor.ctx.javaClassInfo.releaseSpillSlot();
}
// perform the operation
// Note: operands are already on the stack (left DUPped, then right)
String baseOperator = node.operator.substring(0, node.operator.length() - 1);
// Get the operator handler for the base operator, use warn variant only for certain ops
OperatorHandler baseOpHandler = shouldUseWarnVariant
? OperatorHandler.getWarn(baseOperator)
: OperatorHandler.get(baseOperator);
if (baseOpHandler == null) {
baseOpHandler = OperatorHandler.get(baseOperator);
}
if (baseOpHandler != null) {
mv.visitMethodInsn(
baseOpHandler.methodType(),
baseOpHandler.className(),
baseOpHandler.methodName(),
baseOpHandler.descriptor(),
false);
} else {
throw new RuntimeException("No operator handler found for base operator: " + baseOperator);
}
// assign to the Lvalue
// For .= use setPreservingByteString to prevent UTF-8 flag contamination of binary buffers
if (node.operator.equals(".=")) {
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/perlonjava/runtime/runtimetypes/RuntimeScalar", "setPreservingByteString", "(Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;)Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;", false);
} else {
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/perlonjava/runtime/runtimetypes/RuntimeScalar", "set", "(Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;)Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;", false);
}
// For string concat assign (.=), invalidate pos() since string was modified
if (node.operator.equals(".=")) {
mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn(Opcodes.INVOKESTATIC,
"org/perlonjava/runtime/runtimetypes/RuntimePosLvalue",
"invalidatePos",
"(Lorg/perlonjava/runtime/runtimetypes/RuntimeScalar;)V",
false);
}
EmitOperator.handleVoidContext(emitterVisitor);
}
}
static void handleRangeOrFlipFlop(EmitterVisitor emitterVisitor, BinaryOperatorNode node) {
if (emitterVisitor.ctx.contextType == RuntimeContextType.SCALAR) {
EmitLogicalOperator.emitFlipFlopOperator(emitterVisitor, node);
} else {
EmitOperator.handleRangeOperator(emitterVisitor, node);
}
}
}