简体   繁体   English

llvm:将 i8* 类型值转换为 double 类型值

[英]llvm: cast i8* type value to double type value

I have a char pointer (Type::getInt8PtrTy(Context) ) and trying to convert a value (string in other words) which holds this pointer into double type (Type::getDoubleTy(Context)).我有一个 char 指针 (Type::getInt8PtrTy(Context) ) 并尝试将保存该指针的值( 换句话说,字符串) 转换为双精度类型 (Type::getDoubleTy(Context))。 The way I tried is:我尝试的方法是:

Value* stringValue = ... // Value which type Type::getInt8PtrTy(Context)
auto doubleValue = CastInst::CreatePointerCast(stringValue, Type::getDoubleTy(Context), Builder.GetInsertBlock());
AllocaInst *alloca = .. //alloc memory for element which type is Type::getDoubleTy(Context)
Builder.CreateStore(doubleValue, alloca);

The problem is when I'm trying to print doubleValue using external C function (printf), looks like that I got the address but not double value.问题是当我尝试使用外部 C function (printf) 打印 doubleValue 时,看起来我得到了地址但没有 double 值。 In other words, I want to write code which do the same like C atoi function.换句话说,我想编写与 C atoi function 一样的代码。 I'm using C++ API.我正在使用 C++ API。

I would be grateful for the answers.我将不胜感激。

atoi doesn't simply cast a variable to a different type. atoi不会简单地将变量转换为不同的类型。 It actually converts a string value into an integer by means of some algorithm.它实际上通过某种算法将字符串值转换为 integer。

If you want to do the same, you'll have to include a function implementing that algorithm in your bitcode and then use call instruction on your pointer to get the double value.如果你想做同样的事情,你必须在你的位码中包含一个实现该算法的 function,然后在你的指针上使用call指令来获取double精度值。

If you really want to interpret string's data as double, use bitcast to cast the pointer from i8* to double* and then use load instruction.如果您真的想将字符串的数据解释为 double,请使用bitcast将指针从i8*转换为double* ,然后使用load指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM