/**
* @brief Converts the given LLVM select instruction @a inst into an expression
*        in BIR.
*
* Note that @a inst type is @c llvm::User instead of @c llvm::SelectInst
* because this method can handle also constant select expressions.
*/
ShPtr<Expression> LLVMInstructionConverter::convertSelectInstToExpression(
		llvm::User &inst) {
	auto cond = getConverter()->convertValueToExpression(inst.getOperand(0));
	auto trueValue = getConverter()->convertValueToExpression(inst.getOperand(1));
	auto falseValue = getConverter()->convertValueToExpression(inst.getOperand(2));
	return TernaryOpExpr::create(cond, trueValue, falseValue);
}
/**
* @brief Converts the given LLVM getelementptr instruction @a inst into
*        an expression in BIR.
*
* Note that @a inst type is @c llvm::User instead of @c llvm::GetElementPtrInst
* because this method can handle also constant getelementptr expression.
*/
ShPtr<Expression> LLVMInstructionConverter::convertGetElementPtrToExpression(
		llvm::User &inst) {
	auto pointedValue = inst.getOperand(0);

	if (auto globVar = llvm::dyn_cast<llvm::GlobalVariable>(pointedValue)) {
		if (getConverter()->storesStringLiteral(*globVar)) {
			return getInitializerAsConstString(globVar);
		}
	}

	auto it = llvm::gep_type_begin(inst);
	auto e = llvm::gep_type_end(inst);
	if (it != e) {
		ShPtr<Expression> base;
		auto index = getConverter()->convertValueToExpression(it.getOperand());
		auto cInt = cast<ConstInt>(index);
		if (cInt && cInt->isZero()) {
			base = getConverter()->convertValueToExpressionDirectly(pointedValue);
			++it;
		} else {
			base = getConverter()->convertValueToExpression(pointedValue);
		}

		return convertGEPIndices(base, it, e);
	}

	FAIL("unsupported getelementptr instruction: " << inst);
	return nullptr;
}
/**
* @brief Converts the given LLVM call instruction @a inst into an expression in BIR.
*/
ShPtr<CallExpr> LLVMInstructionConverter::convertCallInstToCallExpr(llvm::CallInst &inst) {
	ExprVector args;
	for (auto &arg: inst.arg_operands()) {
		args.push_back(getConverter()->convertValueToExpression(arg));
	}

	auto calledExpr = getConverter()->convertValueToExpression(inst.getCalledValue());
	return CallExpr::create(calledExpr, args);
}
/**
* @brief Converts the given LLVM global value @a globVar into an expression in BIR.
*
* @par Preconditions
*  - @a globVar is non-null
*/
ShPtr<Expression> LLVMConstantConverter::convertToExpression(
		llvm::GlobalVariable *globVar) {
	PRECONDITION_NON_NULL(globVar);

	if (getConverter()->storesStringLiteral(*globVar)) {
		return getInitializerAsConstString(globVar);
	}

	return getConverter()->convertValueToExpression(globVar);
}
/**
* @brief Converts the given LLVM fcmp instruction @a inst with compare predicate
*        @a predicate into an expression in BIR.
*
* Note that @a inst type is @c llvm::User instead of @c llvm::FCmpInst because
* this method can handle also constant floating-point compare expressions.
*/
ShPtr<Expression> LLVMInstructionConverter::convertFCmpInstToExpression(
		llvm::User &inst, unsigned predicate) {
	auto op1 = getConverter()->convertValueToExpression(inst.getOperand(0));
	auto op2 = getConverter()->convertValueToExpression(inst.getOperand(1));
	auto expr = fcmpConverter->convertToExpression(op1, op2, predicate);
	if (!expr) {
		FAIL("unsupported fcmp predicate: " << inst);
	}

	return expr;
}
/**
* @brief Converts the given LLVM icmp instruction @a inst with compare predicate
*        @a predicate into an expression in BIR.
*
* Note that @a inst type is @c llvm::User instead of @c llvm::ICmpInst because
* this method can handle also constant integral compare expressions.
*/
ShPtr<Expression> LLVMInstructionConverter::convertICmpInstToExpression(
		llvm::User &inst, unsigned predicate) {
	auto op1 = getConverter()->convertValueToExpression(inst.getOperand(0));
	auto op2 = getConverter()->convertValueToExpression(inst.getOperand(1));

	switch (predicate) {
		case llvm::CmpInst::Predicate::ICMP_EQ:
			return EqOpExpr::create(op1, op2);

		case llvm::CmpInst::Predicate::ICMP_NE:
			return NeqOpExpr::create(op1, op2);

		case llvm::CmpInst::Predicate::ICMP_UGT:
			return GtOpExpr::create(op1, op2,
				GtOpExpr::Variant::UCmp);

		case llvm::CmpInst::Predicate::ICMP_UGE:
			return GtEqOpExpr::create(op1, op2,
				GtEqOpExpr::Variant::UCmp);

		case llvm::CmpInst::Predicate::ICMP_ULT:
			return LtOpExpr::create(op1, op2,
				LtOpExpr::Variant::UCmp);

		case llvm::CmpInst::Predicate::ICMP_ULE:
			return LtEqOpExpr::create(op1, op2,
				LtEqOpExpr::Variant::UCmp);

		case llvm::CmpInst::Predicate::ICMP_SGT:
			return GtOpExpr::create(op1, op2,
				GtOpExpr::Variant::SCmp);

		case llvm::CmpInst::Predicate::ICMP_SGE:
			return GtEqOpExpr::create(op1, op2,
				GtEqOpExpr::Variant::SCmp);

		case llvm::CmpInst::Predicate::ICMP_SLT:
			return LtOpExpr::create(op1, op2,
				LtOpExpr::Variant::SCmp);

		case llvm::CmpInst::Predicate::ICMP_SLE:
			return LtEqOpExpr::create(op1, op2,
				LtEqOpExpr::Variant::SCmp);

		default:
			FAIL("unsupported icmp predicate: " << inst);
			return nullptr;
	}

	return nullptr;
}
Beispiel #7
0
QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length, QTextCodec::ConverterState *state) const
{
    UConverter *conv = getConverter(state);

    int requiredLength = UCNV_GET_MAX_BYTES_FOR_STRING(length, ucnv_getMaxCharSize(conv));
    QByteArray string(requiredLength, Qt::Uninitialized);

    const UChar *uc = (const UChar *)unicode;
    const UChar *end = uc + length;
    int convertedChars = 0;
    while (1) {
        char *ch = (char *)string.data();
        char *chEnd = ch + string.length();
        ch += convertedChars;
        UErrorCode error = U_ZERO_ERROR;
        ucnv_fromUnicode(conv,
                         &ch, chEnd,
                         &uc, end,
                         0, false, &error);
        if (!U_SUCCESS(error))
            qDebug() << "convertFromUnicode failed:" << u_errorName(error);
        convertedChars = ch - string.data();
        if (uc >= end)
            break;
        string.resize(string.length()*2);
    }
    string.resize(convertedChars);

    if (!state)
        ucnv_close(conv);

    return string;
}
/**
* @brief Converts the given LLVM constant @a constant into an expression in BIR.
*
* @par Preconditions
*  - @a constant is non-null
*/
ShPtr<Expression> LLVMConstantConverter::convertToExpression(llvm::Constant *constant) {
	PRECONDITION_NON_NULL(constant);

	if (auto cInt = llvm::dyn_cast<llvm::ConstantInt>(constant)) {
		return convertToExpression(cInt);
	} else if (auto cFloat = llvm::dyn_cast<llvm::ConstantFP>(constant)) {
		return convertToExpression(cFloat);
	} else if (auto cArray = llvm::dyn_cast<llvm::ConstantArray>(constant)) {
		return convertToExpression(cArray);
	} else if (auto cArray = llvm::dyn_cast<llvm::ConstantDataArray>(constant)) {
		return convertToExpression(cArray);
	} else if (auto cStruct = llvm::dyn_cast<llvm::ConstantStruct>(constant)) {
		return convertToExpression(cStruct);
	} else if (auto cpn = llvm::dyn_cast<llvm::ConstantPointerNull>(constant)) {
		return convertToExpression(cpn);
	} else if (auto globVar = llvm::dyn_cast<llvm::GlobalVariable>(constant)) {
		return convertToExpression(globVar);
	} else if (auto func = llvm::dyn_cast<llvm::Function>(constant)) {
		return getConverter()->convertValueToExpression(func);
	} else if (auto caz = llvm::dyn_cast<llvm::ConstantAggregateZero>(constant)) {
		return convertZeroInitializer(caz->getType());
	} else if (auto undef = llvm::dyn_cast<llvm::UndefValue>(constant)) {
		return convertZeroInitializer(undef->getType());
	} else if (auto cExpr = llvm::dyn_cast<llvm::ConstantExpr>(constant)) {
		return instConverter->convertConstExprToExpression(cExpr);
	}

	FAIL("unsupported constant: " << const_cast<llvm::Constant &>(*constant));
	return nullptr;
}
Beispiel #9
0
QString QIcuCodec::convertToUnicode(const char *chars, int length, QTextCodec::ConverterState *state) const
{
    UConverter *conv = getConverter(state);

    QString string(length + 2, Qt::Uninitialized);

    const char *end = chars + length;
    int convertedChars = 0;
    while (1) {
        UChar *uc = (UChar *)string.data();
        UChar *ucEnd = uc + string.length();
        uc += convertedChars;
        UErrorCode error = U_ZERO_ERROR;
        ucnv_toUnicode(conv,
                       &uc, ucEnd,
                       &chars, end,
                       0, false, &error);
        if (!U_SUCCESS(error) && error != U_BUFFER_OVERFLOW_ERROR) {
            qDebug() << "convertToUnicode failed:" << u_errorName(error);
            break;
        }

        convertedChars = uc - (UChar *)string.data();
        if (chars >= end)
            break;
        string.resize(string.length()*2);
    }
    string.resize(convertedChars);

    if (!state)
        ucnv_close(conv);
    return string;
}
XMLObject DependencyXMLConverterDB::convertDependency(
  RCP<const Dependency> dependency,
  const XMLParameterListWriter::EntryIDsMap& entryIDsMap,
  ValidatortoIDMap& validatorIDsMap)
{
  return getConverter(*dependency)->fromDependencytoXML(
    dependency, entryIDsMap, validatorIDsMap);
}
 /**
  * \brief Converts the given ParameterEntry to XML.
  */
 static XMLObject convertEntry(
   RCP<const ParameterEntry> entry, 
   const std::string& name,
   const ParameterEntry::ParameterEntryID& id,
   const ValidatortoIDMap& validatorIDsMap)
 {
   return getConverter(entry)->fromParameterEntrytoXML(
     entry, name, id, validatorIDsMap);
 }
bool
XWindowsClipboard::addSimpleRequest(Window requestor,
				Atom target, ::Time time, Atom property)
{
	// obsolete requestors may supply a None property.  in
	// that case we use the target as the property to store
	// the conversion.
	if (property == None) {
		property = target;
	}

	// handle targets
	String data;
	Atom type  = None;
	int format = 0;
	if (target == m_atomTargets) {
		type = getTargetsData(data, &format);
	}
	else if (target == m_atomTimestamp) {
		type = getTimestampData(data, &format);
	}
	else {
		IXWindowsClipboardConverter* converter = getConverter(target);
		if (converter != NULL) {
			IClipboard::EFormat clipboardFormat = converter->getFormat();
			if (m_added[clipboardFormat]) {
				try {
					data   = converter->fromIClipboard(m_data[clipboardFormat]);
					format = converter->getDataSize();
					type   = converter->getAtom();
				}
				catch (...) {
					// ignore -- cannot convert
				}
			}
		}
	}

	if (type != None) {
		// success
		LOG((CLOG_DEBUG1 "success"));
		insertReply(new Reply(requestor, target, time,
								property, data, type, format));
		return true;
	}
	else {
		// failure
		LOG((CLOG_DEBUG1 "failed"));
		insertReply(new Reply(requestor, target, time));
		return false;
	}
}
/**
* @brief Converts indices of LLVM getelementptr instruction.
*
* @param[in] base Pointed operand of LLVM getelementptr instruction converted
*                 to an expression in BIR.
* @param[in] start First index of LLVM getelementptr instruction to be converted.
* @param[in] end End of iterator through LLVM getelementptr instruction indices.
*/
ShPtr<Expression> LLVMInstructionConverter::convertGEPIndices(ShPtr<Expression> base,
		llvm::gep_type_iterator start, llvm::gep_type_iterator end) {
	auto indexOp = base;
	for (auto i = start; i != end; ++i) {
		auto index = getConverter()->convertValueToExpression(i.getOperand());
		if (i->isStructTy()) {
			auto indexInt = ucast<ConstInt>(index);
			indexOp = StructIndexOpExpr::create(indexOp, indexInt);
		} else {
			indexOp = ArrayIndexOpExpr::create(indexOp, index);
		}
	}

	return AddressOpExpr::create(indexOp);
}
Beispiel #14
0
SpecificConverter::SpecificConverter(const char* typeName)
    : m_type(InvalidConversion)
{
    m_converter = getConverter(typeName);
    if (!m_converter)
        return;
    int len = strlen(typeName);
    char lastChar = typeName[len -1];
    if (lastChar == '&') {
        m_type = ReferenceConversion;
    } else if (lastChar == '*' || pythonTypeIsObjectType(m_converter)) {
        m_type = PointerConversion;
    } else {
        m_type = CopyConversion;
    }
}
Beispiel #15
0
ViewPtr<object::Object> Simulation::query(const PositionVector& position) const noexcept
{
#ifdef CECE_ENABLE_BOX2D_PHYSICS
    const auto pos = getConverter().convertPosition(position);
    QueryCallback query(pos);

    b2AABB aabb;
    b2Vec2 d;
    d.Set(0.001f, 0.001f);
    aabb.lowerBound = pos - d;
    aabb.upperBound = pos + d;

    m_world.QueryAABB(&query, aabb);

    // Find object
    for (const auto& object : m_objects)
    {
        if (object->getBody() == query.getObject())
            return object.ptr;
    }
#endif

    return nullptr;
}
 /**
  * \brief Converts XML to a ParameterEntry.
  */
 static ParameterEntry convertXML(const XMLObject& xmlObj)
 {
   return getConverter(xmlObj)->fromXMLtoParameterEntry(xmlObj);
 }
	void convert(const String & from_charset, const String & to_charset,
		const ColumnString::Chars_t & from_chars, const ColumnString::Offsets_t & from_offsets,
		ColumnString::Chars_t & to_chars, ColumnString::Offsets_t & to_offsets)
	{
		auto converter = getConverter(CharsetsFromTo(from_charset, to_charset));
		iconv_t iconv_state = converter->impl;

		to_chars.resize(from_chars.size());
		to_offsets.resize(from_offsets.size());

		ColumnString::Offset_t current_from_offset = 0;
		ColumnString::Offset_t current_to_offset = 0;

		size_t size = from_offsets.size();

		for (size_t i = 0; i < size; ++i)
		{
			size_t from_string_size = from_offsets[i] - current_from_offset - 1;

			/// We assume that empty string is empty in every charset.
			if (0 != from_string_size)
			{
				/// reset state of iconv
				size_t res = iconv(iconv_state, nullptr, nullptr, nullptr, nullptr);
				if (static_cast<size_t>(-1) == res)
					throwFromErrno("Cannot reset iconv", ErrorCodes::CANNOT_ICONV);

				/// perform conversion; resize output buffer and continue if required

				char * in_buf = const_cast<char *>(reinterpret_cast<const char *>(&from_chars[current_from_offset]));
				size_t in_bytes_left = from_string_size;

				char * out_buf = reinterpret_cast<char *>(&to_chars[current_to_offset]);
				size_t out_bytes_left = to_chars.size() - current_to_offset;

				while (in_bytes_left)
				{
					size_t res = iconv(iconv_state, &in_buf, &in_bytes_left, &out_buf, &out_bytes_left);
					current_to_offset = to_chars.size() - out_bytes_left;

					if (static_cast<size_t>(-1) == res)
					{
						if (E2BIG == errno)
						{
							to_chars.resize(to_chars.size() * 2);
							out_buf = reinterpret_cast<char *>(&to_chars[current_to_offset]);
							out_bytes_left = to_chars.size() - current_to_offset;
							continue;
						}

						throwFromErrno("Cannot convert charset", ErrorCodes::CANNOT_ICONV);
					}
				}
			}

			if (to_chars.size() < current_to_offset + 1)
				to_chars.resize(current_to_offset + 1);

			to_chars[current_to_offset] = 0;

			++current_to_offset;
			to_offsets[i] = current_to_offset;

			current_from_offset = from_offsets[i];
		}

		to_chars.resize(current_to_offset);
	}
/**
* @brief Converts the given LLVM int to FP cast instruction @a inst into an int
*        to FP cast expression in BIR.
*
* Note that @a inst type is @c llvm::User instead of @c llvm::CastInst
* because this method can handle also constant cast expressions.
*
* @param[in] inst Given LLVM int to FP cast instruction.
* @param[in] variant Variant of int to FP cast expression in BIR.
*/
ShPtr<Expression> LLVMInstructionConverter::convertIntToFPInstToExpression(
		llvm::User &inst, IntToFPCastExpr::Variant variant) {
	auto op = getConverter()->convertValueToExpression(inst.getOperand(0));
	auto dstType = getConverter()->convertType(inst.getType());
	return IntToFPCastExpr::create(op, dstType, variant);
}
ShPtr<Expression> LLVMInstructionConverter::convertCastInstToExpression(llvm::User &inst) {
	auto op = getConverter()->convertValueToExpression(inst.getOperand(0));
	auto dstType = getConverter()->convertType(inst.getType());
	return T::create(op, dstType);
}
Beispiel #20
0
	ForecastPointTest() :
		collector(getConverter())
	{}
Beispiel #21
0
PyTypeObject* getPythonTypeObject(const char* typeName)
{
    return getPythonTypeObject(getConverter(typeName));
}
bool PropertyConverterManager::canConvert(const std::string &srcClassIdentifier,
                                          const std::string &dstClassIdentifier) const {
    return getConverter(srcClassIdentifier, dstClassIdentifier) != nullptr;
}
/**
* @brief Converts the given LLVM binary operation @a inst with opcode @a opcode
*        into an expression in BIR.
*
* Note that @a inst type is @c llvm::User instead of @c llvm::BinaryOperator
* because this method can handle also constant binary expressions.
*/
ShPtr<Expression> LLVMInstructionConverter::convertBinaryOpToExpression(
		llvm::User &inst, unsigned opcode) {
	auto op1 = getConverter()->convertValueToExpression(inst.getOperand(0));
	auto op2 = getConverter()->convertValueToExpression(inst.getOperand(1));

	switch (opcode) {
		case llvm::Instruction::Add:
		case llvm::Instruction::FAdd:
			return AddOpExpr::create(op1, op2);

		case llvm::Instruction::Sub:
		case llvm::Instruction::FSub:
			return SubOpExpr::create(op1, op2);

		case llvm::Instruction::Mul:
		case llvm::Instruction::FMul:
			return MulOpExpr::create(op1, op2);

		case llvm::Instruction::UDiv:
			return DivOpExpr::create(op1, op2,
				DivOpExpr::Variant::UDiv);

		case llvm::Instruction::SDiv:
			return DivOpExpr::create(op1, op2,
				DivOpExpr::Variant::SDiv);

		case llvm::Instruction::FDiv:
			return DivOpExpr::create(op1, op2,
				DivOpExpr::Variant::FDiv);

		case llvm::Instruction::URem:
			return ModOpExpr::create(op1, op2,
				ModOpExpr::Variant::UMod);

		case llvm::Instruction::SRem:
			return ModOpExpr::create(op1, op2,
				ModOpExpr::Variant::SMod);

		case llvm::Instruction::FRem:
			return ModOpExpr::create(op1, op2,
				ModOpExpr::Variant::FMod);

		case llvm::Instruction::Shl:
			return BitShlOpExpr::create(op1, op2);

		case llvm::Instruction::LShr:
			return BitShrOpExpr::create(op1, op2,
				BitShrOpExpr::Variant::Logical);

		case llvm::Instruction::AShr:
			return BitShrOpExpr::create(op1, op2,
				BitShrOpExpr::Variant::Arithmetical);

		case llvm::Instruction::And:
			return BitAndOpExpr::create(op1, op2);

		case llvm::Instruction::Or:
			return BitOrOpExpr::create(op1, op2);

		case llvm::Instruction::Xor:
			return BitXorOpExpr::create(op1, op2);

		default:
			FAIL("unsupported binary operator: " << inst);
			return nullptr;
	}

	return nullptr;
}
/**
* @brief Converts the given LLVM extractvalue instruction @a inst into
*        an expression in BIR.
*/
ShPtr<Expression> LLVMInstructionConverter::visitExtractValueInst(
		llvm::ExtractValueInst &inst) {
	auto type = llvm::cast<llvm::CompositeType>(inst.getAggregateOperand()->getType());
	auto base = getConverter()->convertValueToExpression(inst.getAggregateOperand());
	return generateAccessToAggregateType(type, base, inst.getIndices());
}
bool PropertyConverterManager::canConvert(const Property *srcProperty,
                                          const Property *dstProperty) const {
    return getConverter(srcProperty->getClassIdentifier(), dstProperty->getClassIdentifier()) !=
           nullptr;
}
const PropertyConverter *PropertyConverterManager::getConverter(const Property *srcProperty,
                                                                const Property *dstProperty) const {
    return getConverter(srcProperty->getClassIdentifier(), dstProperty->getClassIdentifier());
}