diff --git a/include/swift/WALASupport/InstrKindInfoGetter.h b/include/swift/WALASupport/InstrKindInfoGetter.h index 511bead31d4ed..2ddee148cf8ef 100644 --- a/include/swift/WALASupport/InstrKindInfoGetter.h +++ b/include/swift/WALASupport/InstrKindInfoGetter.h @@ -47,6 +47,7 @@ class InstrKindInfoGetter { jobject handleIntegerLiteralInst(); jobject handleStringLiteralInst(); jobject handleConstStringLiteralInst(); + jobject handleFloatLiteralInst(); jobject handleProjectBoxInst(); jobject handleDebugValueInst(); jobject handleFunctionRefInst(); diff --git a/include/swift/WALASupport/WALAWalker.h b/include/swift/WALASupport/WALAWalker.h index ed20d7aec6d9d..45e01068807e0 100644 --- a/include/swift/WALASupport/WALAWalker.h +++ b/include/swift/WALASupport/WALAWalker.h @@ -55,6 +55,8 @@ class WALAIntegration { void print(jobject obj); jobject makePosition(int, int, int, int); + + jobject makeBigDecimal(const char *, int); WALAIntegration(JNIEnv *, Exceptions &, const char *); }; diff --git a/lib/WALASupport/InstrKindInfoGetter.cpp b/lib/WALASupport/InstrKindInfoGetter.cpp index 0ad5b79ba100e..a8d9a11c5b4f9 100644 --- a/lib/WALASupport/InstrKindInfoGetter.cpp +++ b/lib/WALASupport/InstrKindInfoGetter.cpp @@ -225,6 +225,40 @@ jobject InstrKindInfoGetter::handleIntegerLiteralInst() { return node; } +jobject InstrKindInfoGetter::handleFloatLiteralInst() { + if (outs != NULL) { + *outs << "<< FloatLiteralInst >>" << "\n"; + } + jobject node = nullptr; + FloatLiteralInst* castInst = cast(instr); + APFloat value = castInst->getValue(); + + if (&value.getSemantics() == &APFloat::IEEEsingle()) { + // To Float + node = (*wala)->makeConstant(value.convertToFloat()); + } + else if (&value.getSemantics() == &APFloat::IEEEdouble()) { + // To Double + node = (*wala)->makeConstant(value.convertToDouble()); + } + else if (value.isFinite()) { + // To BigDecimal + SmallVector buf; + value.toString(buf); + jobject bigDecimal = (*wala).makeBigDecimal(buf.data(), buf.size()); + node = (*wala)->makeConstant(bigDecimal); + } + else { + // Infinity or NaN, convert to double + // as BigDecimal constructor cannot accept strings of these + bool APFLosesInfo; + value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); + node = (*wala)->makeConstant(value.convertToDouble()); + } + nodeMap->insert(std::make_pair(castInst, node)); + return node; +} + jobject InstrKindInfoGetter::handleStringLiteralInst() { // ValueKind indentifier if (outs != NULL) { @@ -813,7 +847,7 @@ SILInstructionKind InstrKindInfoGetter::get() { } case SILInstructionKind::FloatLiteralInst: { - *outs << "<< FloatLiteralInst >>" << "\n"; + node = handleFloatLiteralInst(); break; } @@ -1364,4 +1398,4 @@ SILInstructionKind InstrKindInfoGetter::get() { *outs << *instr << "\n"; return instrKind; -} \ No newline at end of file +} diff --git a/lib/WALASupport/WALAWalker.cpp b/lib/WALASupport/WALAWalker.cpp index 803ae1620220a..10ab1aa491fab 100644 --- a/lib/WALASupport/WALAWalker.cpp +++ b/lib/WALASupport/WALAWalker.cpp @@ -54,6 +54,21 @@ jobject WALAIntegration::makePosition(int fl, int fc, int ll, int lc) { return result; } +jobject WALAIntegration::makeBigDecimal(const char *strData, int strLen) { + char *safeData = strndup(strData, strLen); + jobject val = java_env->NewStringUTF(safeData); + delete safeData; + jclass bigDecimalCls = java_env->FindClass("java/math/BigDecimal"); + THROW_ANY_EXCEPTION(cpp_ex); + jmethodID bigDecimalInit = java_env->GetMethodID(bigDecimalCls, + "", "(Ljava/lang/String;)V"); + THROW_ANY_EXCEPTION(cpp_ex); + jobject bigDecimal = java_env->NewObject(bigDecimalCls, bigDecimalInit, val); + THROW_ANY_EXCEPTION(cpp_ex); + java_env->DeleteLocalRef(val); + return bigDecimal; +} + void WALAIntegration::print(jobject obj) { print_object(java_env, obj); THROW_ANY_EXCEPTION(cpp_ex);