package som.primitives.arithmetic;

import java.math.BigInteger;

import som.primitives.Primitive;
import som.vmobjects.SClass;
import som.vmobjects.SSymbol;

import com.oracle.truffle.api.ExactMath;
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
import com.oracle.truffle.api.dsl.Specialization;


@GenerateNodeFactory
@Primitive({"int:add:", "double:add:"})
public abstract class AdditionPrim extends ArithmeticPrim {
  @Specialization(rewriteOn = ArithmeticException.class)
  public final long doLong(final long left, final long argument) {
    return ExactMath.addExact(left, argument);
  }

  @Specialization
  public final BigInteger doLongWithOverflow(final long left, final long argument) {
    return BigInteger.valueOf(left).add(BigInteger.valueOf(argument));
  }

  @Specialization
  public final Object doBigInteger(final BigInteger left, final BigInteger right) {
    BigInteger result = left.add(right);
    return reduceToLongIfPossible(result);
  }

  @Specialization
  public final double doDouble(final double left, final double right) {
    return right + left;
  }

  @Specialization
  public final String doString(final String left, final String right) {
    return left + right;
  }

  @Specialization
  public final String doSSymbol(final SSymbol left, final SSymbol right) {
    return left.getString() + right.getString();
  }

  @Specialization
  public final String doSSymbol(final SSymbol left, final String right) {
    return left.getString() + right;
  }

  @Specialization
  public final Object doLong(final long left, final BigInteger argument) {
    return doBigInteger(BigInteger.valueOf(left), argument);
  }

  @Specialization
  public final double doLong(final long left, final double argument) {
    return doDouble(left, argument);
  }

  @Specialization
  public final Object doBigInteger(final BigInteger left, final long right) {
    return doBigInteger(left, BigInteger.valueOf(right));
  }

  @Specialization
  public final double doDouble(final double left, final long right) {
    return doDouble(left, (double) right);
  }

  @Specialization
  public final String doString(final String left, final long right) {
    return left + right;
  }

  @Specialization
  public final String doString(final String left, final SClass right) {
    return left + right.getName().getString();
  }

  @Specialization
  public final String doString(final String left, final SSymbol right) {
    return left + right.getString();
  }
}

@@ -152,16 +152,82 @@
 ymbol;%0A%0A
+import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;%0A
 import c
@@ -259,16 +259,16 @@
 ctMath;%0A
-
 import c
@@ -1163,32 +1163,51 @@
 @Specialization%0A
+  @TruffleBoundary%0A
   public final S
@@ -1302,32 +1302,51 @@
 @Specialization%0A
+  @TruffleBoundary%0A
   public final S
@@ -1468,32 +1468,51 @@
 @Specialization%0A
+  @TruffleBoundary%0A
   public final S
@@ -2206,32 +2206,51 @@
 @Specialization%0A
+  @TruffleBoundary%0A
   public final S
@@ -2343,32 +2343,51 @@
 @Specialization%0A
+  @TruffleBoundary%0A
   public final S
@@ -2486,32 +2486,32 @@
 tString();%0A  %7D%0A%0A
-
   @Specializatio
@@ -2504,32 +2504,51 @@
 @Specialization%0A
+  @TruffleBoundary%0A
   public final S

Add @TruffelBoundary to string addition