Skip to content

Commit a429fdb

Browse files
Merge pull request #7 from themaplelab/wala-feature/FloatLiterals
Support for Float literals
2 parents 05cd768 + eb862aa commit a429fdb

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

include/swift/WALASupport/InstrKindInfoGetter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class InstrKindInfoGetter {
4343
jobject handleIntegerLiteralInst();
4444
jobject handleStringLiteralInst();
4545
jobject handleConstStringLiteralInst();
46+
jobject handleFloatLiteralInst();
4647
jobject handleProjectBoxInst();
4748
jobject handleDebugValueInst();
4849
jobject handleFunctionRefInst();

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: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,40 @@ jobject InstrKindInfoGetter::handleIntegerLiteralInst() {
235235
return Node;
236236
}
237237

238+
jobject InstrKindInfoGetter::handleFloatLiteralInst() {
239+
if (outs != NULL) {
240+
*outs << "<< FloatLiteralInst >>" << "\n";
241+
}
242+
jobject node = nullptr;
243+
FloatLiteralInst* castInst = cast<FloatLiteralInst>(instr);
244+
APFloat value = castInst->getValue();
245+
246+
if (&value.getSemantics() == &APFloat::IEEEsingle()) {
247+
// To Float
248+
node = (*wala)->makeConstant(value.convertToFloat());
249+
}
250+
else if (&value.getSemantics() == &APFloat::IEEEdouble()) {
251+
// To Double
252+
node = (*wala)->makeConstant(value.convertToDouble());
253+
}
254+
else if (value.isFinite()) {
255+
// To BigDecimal
256+
SmallVector<char, 128> buf;
257+
value.toString(buf);
258+
jobject bigDecimal = (*wala).makeBigDecimal(buf.data(), buf.size());
259+
node = (*wala)->makeConstant(bigDecimal);
260+
}
261+
else {
262+
// Infinity or NaN, convert to double
263+
// as BigDecimal constructor cannot accept strings of these
264+
bool APFLosesInfo;
265+
value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo);
266+
node = (*wala)->makeConstant(value.convertToDouble());
267+
}
268+
nodeMap->insert(std::make_pair(castInst, node));
269+
return node;
270+
}
271+
238272
jobject InstrKindInfoGetter::handleStringLiteralInst() {
239273
// ValueKind indentifier
240274
if (outs != NULL) {
@@ -840,7 +874,7 @@ SILInstructionKind InstrKindInfoGetter::get() {
840874
}
841875

842876
case SILInstructionKind::FloatLiteralInst: {
843-
*outs << "<< FloatLiteralInst >>" << "\n";
877+
node = handleFloatLiteralInst();
844878
break;
845879
}
846880

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)