Skip to content

Commit 2a8935d

Browse files
authored
Merge pull request #15 from bryantam/wala-feature/FloatLiterals
Handle Float Literal using BigDecimal
2 parents e24a2f2 + cd46644 commit 2a8935d

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

include/swift/WALASupport/WALAWalker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class WALAIntegration {
5555
void print(jobject obj);
5656

5757
jobject makePosition(int, int, int, int);
58+
59+
jobject makeBigDecimal(const char *, int);
5860

5961
WALAIntegration(JNIEnv *, Exceptions &, const char *);
6062
};

lib/WALASupport/InstrKindInfoGetter.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,26 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() {
229229
if (outs != NULL) {
230230
*outs << "<< FloatLiteralInst >>" << "\n";
231231
}
232+
jobject node = nullptr;
232233
FloatLiteralInst* castInst = cast<FloatLiteralInst>(instr);
233234
APFloat value = castInst->getValue();
234-
bool APFLosesInfo;
235-
value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
236-
jobject node = (*wala)->makeConstant(value.convertToDouble());
237-
nodeMap->insert(std::make_pair(castInst, node));
235+
236+
if (value.isFinite()) {
237+
// To BigDecimal
238+
SmallVector<char, 128> buf;
239+
value.toString(buf);
240+
jobject bigDecimal = (*wala).makeBigDecimal(buf.data(), buf.size());
241+
node = (*wala)->makeConstant(bigDecimal);
242+
nodeMap->insert(std::make_pair(castInst, node));
243+
}
244+
else {
245+
// Infinity of NaN, convert to double
246+
// as BigDecimal constructor cannot accept strings of these
247+
bool APFLosesInfo;
248+
value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
249+
node = (*wala)->makeConstant(value.convertToDouble());
250+
nodeMap->insert(std::make_pair(castInst, node));
251+
}
238252
return node;
239253
}
240254

lib/WALASupport/WALAWalker.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ jobject WALAIntegration::makePosition(int fl, int fc, int ll, int lc) {
5454
return result;
5555
}
5656

57+
jobject WALAIntegration::makeBigDecimal(const char *strData, int strLen) {
58+
char *safeData = strndup(strData, strLen);
59+
jobject val = java_env->NewStringUTF(safeData);
60+
delete safeData;
61+
jclass bigDecimalCls = java_env->FindClass("java/math/BigDecimal");
62+
THROW_ANY_EXCEPTION(cpp_ex);
63+
jmethodID bigDecimalInit = java_env->GetMethodID(bigDecimalCls,
64+
"<init>", "(Ljava/lang/String;)V");
65+
THROW_ANY_EXCEPTION(cpp_ex);
66+
jobject bigDecimal = java_env->NewObject(bigDecimalCls, bigDecimalInit, val);
67+
THROW_ANY_EXCEPTION(cpp_ex);
68+
java_env->DeleteLocalRef(val);
69+
return bigDecimal;
70+
}
71+
5772
void WALAIntegration::print(jobject obj) {
5873
print_object(java_env, obj);
5974
THROW_ANY_EXCEPTION(cpp_ex);

0 commit comments

Comments
 (0)