LLVM字符串值对象:如何从值检索字符串? [英] LLVM String Value objects: How can I retrieve the String from a Value?
问题描述
当从现有的AST构建IR时,我的AST有一些字符串值(在编译时,它们是从 std :: string
构建的)他们安全地作为 llvm :: Value
用作表达式的一部分。
When building the IR from an existing AST, my AST has some string values (at compile-time they are built from std::string
) and I want to set them safely as llvm::Value
to use as a part of an expression.
在这种情况下,我不需要在运行时绑定字符串,因为字符串值只是为了解析东西作为变量,函数或类在编译-time(该语言不支持本地字符串类型)。
In this case, I don't need to bind the string at run-time, because string values are only meant to resolve stuff as variables, functions or classes at compile-time (the language doesn't support a native string type).
将字符串内容保存为的最佳方式llvm :: Value
Whats the best way to keep my string content as llvm::Value
and still be able to retrieve it at later stages of compilation (when the nesting expressions are built)?
llvm::Value* v = llvm::ConstantArray::get(llvmContext, myString.c_str());
如何安全地检索字符串值? $ llvm :: ConstantArray
是否适当的包装字符串的方法?
How do I safely retrieve the string value? Is llvm::ConstantArray
the appropriate way to wrap strings?
推荐答案
是的,ConstantArray是你应该在这里使用。为了以后检索值,只需使用ConstantArray :: getAsCString()。如果你打开了断言,它会断言是否出现错误(例如,你将尝试从数组w / o零终止符获取字符串)。
Yes, ConstantArray is what you should use here. In order to retrieve the value later just use ConstantArray::getAsCString(). If you have assertions turned on, it will assert if something will went wrong (e.g. you will try to grab string from the array w/o zero terminator).
这篇关于LLVM字符串值对象:如何从值检索字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!