-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEmitBinaryOperator.java
More file actions
256 lines (232 loc) · 11.7 KB
/
EmitBinaryOperator.java
File metadata and controls
256 lines (232 loc) · 11.7 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
package org.perlonjava.codegen;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.perlonjava.astnode.BinaryOperatorNode;
import org.perlonjava.astnode.IdentifierNode;
import org.perlonjava.astnode.NumberNode;
import org.perlonjava.astnode.StringNode;
import org.perlonjava.astvisitor.EmitterVisitor;
import org.perlonjava.operators.OperatorHandler;
import org.perlonjava.perlmodule.Strict;
import org.perlonjava.runtime.RuntimeContextType;
import org.perlonjava.runtime.ScalarUtils;
import static org.perlonjava.codegen.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
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/operators/MathOperators",
"integerModulus",
"(Lorg/perlonjava/runtime/RuntimeScalar;Lorg/perlonjava/runtime/RuntimeScalar;)Lorg/perlonjava/runtime/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/operators/MathOperators",
"integerDivide",
"(Lorg/perlonjava/runtime/RuntimeScalar;Lorg/perlonjava/runtime/RuntimeScalar;)Lorg/perlonjava/runtime/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/operators/BitwiseOperators",
"integerShiftLeft",
"(Lorg/perlonjava/runtime/RuntimeScalar;Lorg/perlonjava/runtime/RuntimeScalar;)Lorg/perlonjava/runtime/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/operators/BitwiseOperators",
"integerShiftRight",
"(Lorg/perlonjava/runtime/RuntimeScalar;Lorg/perlonjava/runtime/RuntimeScalar;)Lorg/perlonjava/runtime/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 `+=`
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
String baseOperator = node.operator.substring(0, node.operator.length() - 1);
// Create a BinaryOperatorNode for the base operation
BinaryOperatorNode baseOpNode = new BinaryOperatorNode(
baseOperator,
node.left,
node.right,
node.tokenIndex
);
EmitOperator.emitOperator(baseOpNode, scalarVisitor);
// assign to the Lvalue
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/perlonjava/runtime/RuntimeScalar", "set", "(Lorg/perlonjava/runtime/RuntimeScalar;)Lorg/perlonjava/runtime/RuntimeScalar;", 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);
}
}
}