Skip to content

Handle Float Literal using BigDecimal #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/WALASupport/WALAWalker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
};
Expand Down
22 changes: 18 additions & 4 deletions lib/WALASupport/InstrKindInfoGetter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,26 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() {
if (outs != NULL) {
*outs << "<< FloatLiteralInst >>" << "\n";
}
jobject node = nullptr;
FloatLiteralInst* castInst = cast<FloatLiteralInst>(instr);
APFloat value = castInst->getValue();
bool APFLosesInfo;
value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
jobject node = (*wala)->makeConstant(value.convertToDouble());
nodeMap->insert(std::make_pair(castInst, node));

if (value.isFinite()) {
// To BigDecimal
SmallVector<char, 128> buf;
value.toString(buf);
jobject bigDecimal = (*wala).makeBigDecimal(buf.data(), buf.size());
node = (*wala)->makeConstant(bigDecimal);
nodeMap->insert(std::make_pair(castInst, node));
}
else {
// Infinity of 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;
}

Expand Down
15 changes: 15 additions & 0 deletions lib/WALASupport/WALAWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
"<init>", "(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);
Expand Down