Skip to content

Support for Float literals #7

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
merged 8 commits into from
Mar 12, 2018
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
1 change: 1 addition & 0 deletions include/swift/WALASupport/InstrKindInfoGetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class InstrKindInfoGetter {
jobject handleIntegerLiteralInst();
jobject handleStringLiteralInst();
jobject handleConstStringLiteralInst();
jobject handleFloatLiteralInst();
jobject handleProjectBoxInst();
jobject handleDebugValueInst();
jobject handleFunctionRefInst();
Expand Down
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
38 changes: 36 additions & 2 deletions lib/WALASupport/InstrKindInfoGetter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,40 @@ jobject InstrKindInfoGetter::handleIntegerLiteralInst() {
return node;
}

jobject InstrKindInfoGetter::handleFloatLiteralInst() {
if (outs != NULL) {
*outs << "<< FloatLiteralInst >>" << "\n";
}
jobject node = nullptr;
FloatLiteralInst* castInst = cast<FloatLiteralInst>(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<char, 128> 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) {
Expand Down Expand Up @@ -813,7 +847,7 @@ SILInstructionKind InstrKindInfoGetter::get() {
}

case SILInstructionKind::FloatLiteralInst: {
*outs << "<< FloatLiteralInst >>" << "\n";
node = handleFloatLiteralInst();
break;
}

Expand Down Expand Up @@ -1364,4 +1398,4 @@ SILInstructionKind InstrKindInfoGetter::get() {

*outs << *instr << "\n";
return instrKind;
}
}
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