简体   繁体   English

LLVM:转换类型的简单方法?

[英]LLVM: simple way to cast types?

I'm looking for a simple way to cast types using the LLVM C++ api.我正在寻找一种使用 LLVM C++ api 转换类型的简单方法。 What I'm trying to avoid is endless nested if-else checks what will pick the right IR cast instruction.我要避免的是无休止的嵌套 if-else 检查什么会选择正确的 IR cast 指令。 Here's an example code that I'd like to get working:这是我想开始工作的示例代码:

int x = 0;
int64_t y = (int64_t) x;

Is there a way to cast types without checking what type it is?有没有办法在不检查类型的情况下转换类型? Obviously a type conversion from a struct to a float is stupid and should fail but how can I simply pick the right instruction?显然,从结构到浮点的类型转换是愚蠢的,应该会失败,但我怎样才能简单地选择正确的指令呢?

Also I'm using this as a reference: https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io/en/latest/basic-constructs/casts.html because the official LLVM docs aren't very helpful.我也将其用作参考: https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io/en/latest/basic-constructs/casts.html因为官方的 LLVM 文档不是很有帮助。

The different instructions do different things, as in, produce different bits in the output.不同的指令做不同的事情,例如,在 output 中产生不同的位。 There are utility functions like "CastInst::CreateIntegerCast" and "CastInst::CreateTruncOrBitCast" which might be the sort of thing you want?有像“CastInst::CreateIntegerCast”和“CastInst::CreateTruncOrBitCast”这样的实用函数,这可能是你想要的那种东西? See llvm.org/doxygen/classllvm_1_1CastInst.html.请参阅 llvm.org/doxygen/classllvm_1_1CastInst.html。

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

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