diff --git a/hphp/runtime/base/tv_helpers.cpp b/hphp/runtime/base/tv_helpers.cpp index be66a3daf..c4241fdfe 100644 --- a/hphp/runtime/base/tv_helpers.cpp +++ b/hphp/runtime/base/tv_helpers.cpp @@ -110,79 +110,10 @@ void tvCastToBooleanInPlace(TypedValue* tv) { tv->m_type = KindOfBoolean; } -void tvCastToInt64InPlace(TypedValue* tv, int base /* = 10 */) { - assert(tvIsPlausible(tv)); - tvUnboxIfNeeded(tv); - int64_t i; - switch (tv->m_type) { - case KindOfUninit: - case KindOfNull: - tv->m_data.num = 0LL; - // Fall through - case KindOfBoolean: - assert(tv->m_data.num == 0LL || tv->m_data.num == 1LL); - tv->m_type = KindOfInt64; - // Fall through - case KindOfInt64: - return; - case KindOfDouble: { - i = toInt64(tv->m_data.dbl); - break; - } - case KindOfStaticString: i = (tv->m_data.pstr->toInt64(base)); break; - case KindOfString: { - i = (tv->m_data.pstr->toInt64(base)); - tvDecRefStr(tv); - break; - } - case KindOfArray: { - i = (tv->m_data.parr->empty() ? 0LL : 1LL); - tvDecRefArr(tv); - break; - } - case KindOfObject: { - i = (tv->m_data.pobj ? tv->m_data.pobj->o_toInt64() : 0LL); - tvDecRefObj(tv); - break; - } - default: assert(false); i = 0LL; break; - } - tv->m_data.num = i; - tv->m_type = KindOfInt64; -} - -int64_t tvCastToInt64(TypedValue* tv, int base /* = 10 */) { - assert(tvIsPlausible(tv)); - if (tv->m_type == KindOfRef) { - tv = tv->m_data.pref->tv(); - } - - switch (tv->m_type) { - case KindOfUninit: - case KindOfNull: - return 0; - case KindOfBoolean: - assert(tv->m_data.num == 0LL || tv->m_data.num == 1LL); - // Fall through - case KindOfInt64: - return tv->m_data.num; - case KindOfDouble: - return toInt64(tv->m_data.dbl); - case KindOfStaticString: - case KindOfString: - return tv->m_data.pstr->toInt64(base); - case KindOfArray: - return tv->m_data.parr->empty() ? 0 : 1; - case KindOfObject: - return tv->m_data.pobj->o_toInt64(); - default: - not_reached(); - } -} - void tvCastToDoubleInPlace(TypedValue* tv) { assert(tvIsPlausible(tv)); tvUnboxIfNeeded(tv); + double d; switch (tv->m_type) { case KindOfUninit: @@ -208,6 +139,39 @@ void tvCastToDoubleInPlace(TypedValue* tv) { tv->m_type = KindOfDouble; } +void cellCastToInt64InPlace(Cell* cell) { + assert(cellIsPlausible(cell)); + + int64_t i; + switch (cell->m_type) { + case KindOfUninit: + case KindOfNull: + cell->m_data.num = 0LL; + // Fall through + case KindOfBoolean: + assert(cell->m_data.num == 0LL || cell->m_data.num == 1LL); + cell->m_type = KindOfInt64; + // Fall through + case KindOfInt64: + return; + case KindOfDouble: { + i = toInt64(cell->m_data.dbl); + break; + } + case KindOfStaticString: i = (cell->m_data.pstr->toInt64()); break; + case KindOfString: { + i = cell->m_data.pstr->toInt64(); + tvDecRefStr(cell); + break; + } +} + +void tvCastToInt64InPlace(TypedValue* tv) { + assert(tvIsPlausible(tv)); + tvUnboxIfNeeded(tv); + cellCastToInt64InPlace(tv); +} + double tvCastToDouble(TypedValue* tv) { assert(tvIsPlausible(tv)); if (tv->m_type == KindOfRef) { diff --git a/hphp/runtime/base/tv_helpers.h b/hphp/runtime/base/tv_helpers.h index 2968e89ee..b85613142 100644 --- a/hphp/runtime/base/tv_helpers.h +++ b/hphp/runtime/base/tv_helpers.h @@ -464,8 +464,7 @@ void tvUnboxIfNeeded(TypedValue* tv); * old value, if necessary). */ void tvCastToBooleanInPlace(TypedValue* tv); -void tvCastToInt64InPlace(TypedValue* tv, int base = 10); -int64_t tvCastToInt64(TypedValue* tv, int base = 10); +void tvCastToInt64InPlace(TypedValue* tv); void tvCastToDoubleInPlace(TypedValue* tv); double tvCastToDouble(TypedValue* tv); void tvCastToStringInPlace(TypedValue* tv); diff --git a/hphp/runtime/vm/jit/codegen.cpp b/hphp/runtime/vm/jit/codegen.cpp index 9a347ae3a..7764da04d 100644 --- a/hphp/runtime/vm/jit/codegen.cpp +++ b/hphp/runtime/vm/jit/codegen.cpp @@ -3597,8 +3597,6 @@ void CodeGenerator::cgCastStk(IRInstruction *inst) { if (type.subtypeOf(Type::Bool)) { tvCastHelper = (TCA)tvCastToBooleanInPlace; } else if (type.subtypeOf(Type::Int)) { - // if casting to integer, pass 10 as the base for the conversion - args.imm(10); tvCastHelper = (TCA)tvCastToInt64InPlace; } else if (type.subtypeOf(Type::Dbl)) { tvCastHelper = (TCA)tvCastToDoubleInPlace; diff --git a/hphp/runtime/vm/jit/translator-runtime.cpp b/hphp/runtime/vm/jit/translator-runtime.cpp index 6b9663f36..bc4828bbd 100644 --- a/hphp/runtime/vm/jit/translator-runtime.cpp +++ b/hphp/runtime/vm/jit/translator-runtime.cpp @@ -158,7 +158,8 @@ int64_t convStrToIntHelper(const StringData* s) { } int64_t convCellToIntHelper(TypedValue tv) { - return tvCastToInt64(&tv); + // TODO call cellToInt directly from the TC. + return cellToInt(tv); } ObjectData* convCellToObjHelper(TypedValue tv) { diff --git a/hphp/runtime/vm/member_operations.h b/hphp/runtime/vm/member_operations.h index 4fa7f5c7b..2f06762e3 100644 --- a/hphp/runtime/vm/member_operations.h +++ b/hphp/runtime/vm/member_operations.h @@ -24,6 +24,7 @@ #include "hphp/runtime/vm/core_types.h" #include "hphp/runtime/vm/runtime.h" #include "hphp/runtime/ext/ext_collections.h" +#include "hphp/runtime/base/tv_conversions.h" namespace HPHP { @@ -642,11 +643,7 @@ template inline int64_t castKeyToInt(TypedValue* key) { TypedValue scratch; initScratchKey(scratch, key); - if (key->m_type == KindOfInt64) { - return key->m_data.num; - } else { - return tvCastToInt64(key); - } + return cellToInt(*tvToCell(key)); } template<>