|
137 | 137 | import com.oracle.graal.python.nodes.expression.BinaryArithmeticFactory;
|
138 | 138 | import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
|
139 | 139 | import com.oracle.graal.python.nodes.expression.BinaryComparisonNodeFactory;
|
| 140 | +import com.oracle.graal.python.nodes.expression.BinaryOp; |
140 | 141 | import com.oracle.graal.python.nodes.expression.CoerceToBooleanNode;
|
141 | 142 | import com.oracle.graal.python.nodes.expression.CoerceToBooleanNodeFactory;
|
142 | 143 | import com.oracle.graal.python.nodes.expression.ContainsNode;
|
|
148 | 149 | import com.oracle.graal.python.nodes.expression.UnaryArithmetic.NegNode;
|
149 | 150 | import com.oracle.graal.python.nodes.expression.UnaryArithmetic.PosNode;
|
150 | 151 | import com.oracle.graal.python.nodes.expression.UnaryArithmeticFactory;
|
| 152 | +import com.oracle.graal.python.nodes.expression.UnaryOpNode; |
151 | 153 | import com.oracle.graal.python.nodes.frame.DeleteGlobalNode;
|
152 | 154 | import com.oracle.graal.python.nodes.frame.DeleteGlobalNodeGen;
|
153 | 155 | import com.oracle.graal.python.nodes.frame.ReadGlobalOrBuiltinNode;
|
@@ -303,6 +305,96 @@ public final class PBytecodeRootNode extends PRootNode implements BytecodeOSRNod
|
303 | 305 | private static final GetNameFromLocalsNode UNCACHED_GET_NAME_FROM_LOCALS = GetNameFromLocalsNode.getUncached();
|
304 | 306 | private static final NodeSupplier<GetNameFromLocalsNode> NODE_GET_NAME_FROM_LOCALS = GetNameFromLocalsNode::create;
|
305 | 307 |
|
| 308 | + private static final IntNodeFunction<UnaryOpNode> UNARY_OP_FACTORY = (int op) -> { |
| 309 | + switch (op) { |
| 310 | + case UnaryOpsConstants.NOT: |
| 311 | + return CoerceToBooleanNode.createIfFalseNode(); |
| 312 | + case UnaryOpsConstants.POSITIVE: |
| 313 | + return PosNode.create(); |
| 314 | + case UnaryOpsConstants.NEGATIVE: |
| 315 | + return NegNode.create(); |
| 316 | + case UnaryOpsConstants.INVERT: |
| 317 | + return InvertNode.create(); |
| 318 | + default: |
| 319 | + throw CompilerDirectives.shouldNotReachHere(); |
| 320 | + } |
| 321 | + }; |
| 322 | + |
| 323 | + private static final IntNodeFunction<Node> BINARY_OP_FACTORY = (int op) -> { |
| 324 | + switch (op) { |
| 325 | + case BinaryOpsConstants.ADD: |
| 326 | + return BinaryArithmetic.Add.create(); |
| 327 | + case BinaryOpsConstants.SUB: |
| 328 | + return BinaryArithmetic.Sub.create(); |
| 329 | + case BinaryOpsConstants.MUL: |
| 330 | + return BinaryArithmetic.Mul.create(); |
| 331 | + case BinaryOpsConstants.TRUEDIV: |
| 332 | + return BinaryArithmetic.TrueDiv.create(); |
| 333 | + case BinaryOpsConstants.FLOORDIV: |
| 334 | + return BinaryArithmetic.FloorDiv.create(); |
| 335 | + case BinaryOpsConstants.MOD: |
| 336 | + return BinaryArithmetic.Mod.create(); |
| 337 | + case BinaryOpsConstants.LSHIFT: |
| 338 | + return BinaryArithmetic.LShift.create(); |
| 339 | + case BinaryOpsConstants.RSHIFT: |
| 340 | + return BinaryArithmetic.RShift.create(); |
| 341 | + case BinaryOpsConstants.AND: |
| 342 | + return BinaryArithmetic.And.create(); |
| 343 | + case BinaryOpsConstants.OR: |
| 344 | + return BinaryArithmetic.Or.create(); |
| 345 | + case BinaryOpsConstants.XOR: |
| 346 | + return BinaryArithmetic.Xor.create(); |
| 347 | + case BinaryOpsConstants.POW: |
| 348 | + return BinaryArithmetic.Pow.create(); |
| 349 | + case BinaryOpsConstants.MATMUL: |
| 350 | + return BinaryArithmetic.MatMul.create(); |
| 351 | + case BinaryOpsConstants.INPLACE_ADD: |
| 352 | + return InplaceArithmetic.IAdd.create(); |
| 353 | + case BinaryOpsConstants.INPLACE_SUB: |
| 354 | + return InplaceArithmetic.ISub.create(); |
| 355 | + case BinaryOpsConstants.INPLACE_MUL: |
| 356 | + return InplaceArithmetic.IMul.create(); |
| 357 | + case BinaryOpsConstants.INPLACE_TRUEDIV: |
| 358 | + return InplaceArithmetic.ITrueDiv.create(); |
| 359 | + case BinaryOpsConstants.INPLACE_FLOORDIV: |
| 360 | + return InplaceArithmetic.IFloorDiv.create(); |
| 361 | + case BinaryOpsConstants.INPLACE_MOD: |
| 362 | + return InplaceArithmetic.IMod.create(); |
| 363 | + case BinaryOpsConstants.INPLACE_LSHIFT: |
| 364 | + return InplaceArithmetic.ILShift.create(); |
| 365 | + case BinaryOpsConstants.INPLACE_RSHIFT: |
| 366 | + return InplaceArithmetic.IRShift.create(); |
| 367 | + case BinaryOpsConstants.INPLACE_AND: |
| 368 | + return InplaceArithmetic.IAnd.create(); |
| 369 | + case BinaryOpsConstants.INPLACE_OR: |
| 370 | + return InplaceArithmetic.IOr.create(); |
| 371 | + case BinaryOpsConstants.INPLACE_XOR: |
| 372 | + return InplaceArithmetic.IXor.create(); |
| 373 | + case BinaryOpsConstants.INPLACE_POW: |
| 374 | + return InplaceArithmetic.IPow.create(); |
| 375 | + case BinaryOpsConstants.INPLACE_MATMUL: |
| 376 | + return InplaceArithmetic.IMatMul.create(); |
| 377 | + case BinaryOpsConstants.EQ: |
| 378 | + return BinaryComparisonNode.EqNode.create(); |
| 379 | + case BinaryOpsConstants.NE: |
| 380 | + return BinaryComparisonNode.NeNode.create(); |
| 381 | + case BinaryOpsConstants.LT: |
| 382 | + return BinaryComparisonNode.LtNode.create(); |
| 383 | + case BinaryOpsConstants.LE: |
| 384 | + return BinaryComparisonNode.LeNode.create(); |
| 385 | + case BinaryOpsConstants.GT: |
| 386 | + return BinaryComparisonNode.GtNode.create(); |
| 387 | + case BinaryOpsConstants.GE: |
| 388 | + return BinaryComparisonNode.GeNode.create(); |
| 389 | + case BinaryOpsConstants.IS: |
| 390 | + return IsNode.create(); |
| 391 | + case BinaryOpsConstants.IN: |
| 392 | + return ContainsNode.create(); |
| 393 | + default: |
| 394 | + throw CompilerDirectives.shouldNotReachHere(); |
| 395 | + } |
| 396 | + }; |
| 397 | + |
306 | 398 | /*
|
307 | 399 | * Create fake GeneratorControlData just to maintain the same generator frame layout as AST
|
308 | 400 | * interpreter. TODO remove
|
@@ -483,6 +575,11 @@ private interface NodeFunction<A, T> {
|
483 | 575 | T apply(A argument);
|
484 | 576 | }
|
485 | 577 |
|
| 578 | + @FunctionalInterface |
| 579 | + private static interface IntNodeFunction<T extends Node> { |
| 580 | + T apply(int argument); |
| 581 | + } |
| 582 | + |
486 | 583 | @SuppressWarnings("unchecked")
|
487 | 584 | private <A, T extends Node> T insertChildNode(Node[] nodes, int nodeIndex, Class<? extends T> cachedClass, NodeFunction<A, T> nodeSupplier, A argument) {
|
488 | 585 | Node node = nodes[nodeIndex];
|
@@ -512,6 +609,23 @@ private <A, T extends Node> T insertChildNode(Node[] nodes, int nodeIndex, T unc
|
512 | 609 | return CompilerDirectives.castExact(doInsertChildNode(nodes, nodeIndex, nodeSupplier, argument), cachedClass);
|
513 | 610 | }
|
514 | 611 |
|
| 612 | + @SuppressWarnings("unchecked") |
| 613 | + private <T extends Node> T insertChildNodeInt(Node[] nodes, int nodeIndex, IntNodeFunction<T> nodeSupplier, int argument) { |
| 614 | + Node node = nodes[nodeIndex]; |
| 615 | + if (node != null) { |
| 616 | + return (T) node; |
| 617 | + } |
| 618 | + return doInsertChildNodeInt(nodes, nodeIndex, nodeSupplier, argument); |
| 619 | + } |
| 620 | + |
| 621 | + @SuppressWarnings("unchecked") |
| 622 | + private <T extends Node> T doInsertChildNodeInt(Node[] nodes, int nodeIndex, IntNodeFunction<T> nodeSupplier, int argument) { |
| 623 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 624 | + T newNode = nodeSupplier.apply(argument); |
| 625 | + nodes[nodeIndex] = insert(newNode); |
| 626 | + return newNode; |
| 627 | + } |
| 628 | + |
515 | 629 | @SuppressWarnings("unchecked")
|
516 | 630 | private <T extends Node, U extends T> U insertChildNode(Node[] nodes, int nodeIndex, Class<U> cachedClass, NodeSupplier<T> nodeSupplier) {
|
517 | 631 | Node node = nodes[nodeIndex];
|
@@ -943,12 +1057,20 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Frame s
|
943 | 1057 | break;
|
944 | 1058 | case OpCodesConstants.UNARY_OP: {
|
945 | 1059 | int op = Byte.toUnsignedInt(localBC[++bci]);
|
946 |
| - bytecodeUnaryOp(virtualFrame, stackFrame, stackTop, localNodes, beginBci, op); |
| 1060 | + UnaryOpNode opNode = insertChildNodeInt(localNodes, bci, UNARY_OP_FACTORY, op); |
| 1061 | + Object value = stackFrame.getObject(stackTop); |
| 1062 | + Object result = opNode.execute(virtualFrame, value); |
| 1063 | + stackFrame.setObject(stackTop, result); |
947 | 1064 | break;
|
948 | 1065 | }
|
949 | 1066 | case OpCodesConstants.BINARY_OP: {
|
950 | 1067 | int op = Byte.toUnsignedInt(localBC[++bci]);
|
951 |
| - stackTop = bytecodeBinaryOp(virtualFrame, stackFrame, stackTop, localNodes, beginBci, op); |
| 1068 | + BinaryOp opNode = (BinaryOp) insertChildNodeInt(localNodes, bci, BINARY_OP_FACTORY, op); |
| 1069 | + Object right = stackFrame.getObject(stackTop); |
| 1070 | + stackFrame.setObject(stackTop--, null); |
| 1071 | + Object left = stackFrame.getObject(stackTop); |
| 1072 | + Object result = opNode.executeObject(virtualFrame, left, right); |
| 1073 | + stackFrame.setObject(stackTop, result); |
952 | 1074 | break;
|
953 | 1075 | }
|
954 | 1076 | case OpCodesConstants.BINARY_SUBSCR: {
|
|
0 commit comments