Class SLAddNode

All Implemented Interfaces:
com.oracle.truffle.api.instrumentation.InstrumentableNode, com.oracle.truffle.api.nodes.NodeInterface, Cloneable

public abstract class SLAddNode extends SLBinaryNode
SL node that performs the "+" operation, which performs addition on arbitrary precision numbers, as well as String concatenation if one of the operands is a String.

Type specialization on the input values is essential for the performance. This is achieved via node rewriting: specialized subclasses handle just a single type, so that the generic node that can handle all types is used only in cases where different types were encountered. The subclasses are automatically generated by the Truffle DSL. In addition, a factory class is generated that provides, e.g., node creation.

  • Constructor Details

    • SLAddNode

      public SLAddNode()
  • Method Details

    • doLong

      public static long doLong(long left, long right)
      Specialization for primitive long values. This is the fast path of the arbitrary-precision arithmetic. We need to check for overflows of the addition, and switch to the slow path. Therefore, we use an addition method that throws an exception on overflow. The rewriteOn attribute on the Specialization annotation automatically triggers the node rewriting on the exception.

      In compiled code, addExact is compiled to efficient machine code that uses the processor's overflow flag. Therefore, this method is compiled to only two machine code instructions on the fast path.

      This specialization is automatically selected by the Truffle DSL if both the left and right operand are long values.

    • doSLBigInteger

      public static SLBigInteger doSLBigInteger(SLBigInteger left, SLBigInteger right)
      This is the slow path of the arbitrary-precision arithmetic. The SLBigInteger type of Java is doing everything we need.

      This specialization is automatically selected by the Truffle DSL if both the left and right operand are SLBigInteger values. Because the type system defines an implicit conversion from long to SLBigInteger in SLTypes.castBigNumber(long), this specialization is also taken if the left or the right operand is a long value. Because the long specialization} has the rewriteOn attribute, this specialization is also taken if both input values are long values but the primitive addition overflows.

    • doInteropBigInteger

      public static SLBigInteger doInteropBigInteger(Object left, Object right, com.oracle.truffle.api.interop.InteropLibrary leftLibrary, com.oracle.truffle.api.interop.InteropLibrary rightLibrary)
      This is the most general slow path of the arbitrary-precision arithmetic. In addition to what doSLBigInteger(SLBigInteger, SLBigInteger) can handle, it also handles foreign objects that fit into BigInteger, e.g. host objects representing BigInteger instances or big integer representations from other languages.

      This specialization is automatically selected by the Truffle DSL if both the left and the right operand fit into BigInteger, but at least one of them cannot be coverted to SLBigInteger by implicit conversion. Once this specialization has been selected, it replaces the doSLBigInteger(SLBigInteger, SLBigInteger) specialization which is then never used again.

    • doString

      public static com.oracle.truffle.api.strings.TruffleString doString(Object left, Object right, com.oracle.truffle.api.nodes.Node node, SLToTruffleStringNode toTruffleStringNodeLeft, SLToTruffleStringNode toTruffleStringNodeRight, com.oracle.truffle.api.strings.TruffleString.ConcatNode concatNode)
      Specialization for TruffleString concatenation. The SL specification says that TruffleString concatenation works if either the left or the right operand is a TruffleString. The non-string operand is converted then automatically converted to a TruffleString.

      To implement these semantics, we tell the Truffle DSL to use a custom guard. The guard function is defined in this class, but could also be in any superclass.

    • isString

      public static boolean isString(Object a, Object b)
      Guard for TruffleString concatenation: returns true if either the left or the right operand is a TruffleString.
    • typeError

      public static Object typeError(Object left, Object right, com.oracle.truffle.api.nodes.Node node)