LLVM 字符串值对象:如何从值中检索字符串?
当从现有 AST 构建 IR 时,我的 AST 有一些字符串值(在编译时它们是从 std::string
构建的),我想将它们安全地设置为 llvm::用作表达式一部分的值
。
在这种情况下,我不需要在运行时绑定字符串,因为字符串值仅用于在编译时将内容解析为变量、函数或类(该语言不支持本机字符串类型)。
将字符串内容保持为 llvm::Value
并且仍然能够在编译的后期阶段(构建嵌套表达式时)检索它的最佳方法是什么?
更具体地说,如果我将 llvm::Value 设置为:
llvm::Value* v = llvm::ConstantArray::get(llvmContext, myString.c_str());
如何安全地检索字符串值? llvm::ConstantArray
是包装字符串的适当方法吗?
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.
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).
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)?
More concretely, if I set the llvm::Value
with:
llvm::Value* v = llvm::ConstantArray::get(llvmContext, myString.c_str());
How do I safely retrieve the string value? Is llvm::ConstantArray
the appropriate way to wrap strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,ConstantArray 就是您应该在这里使用的。为了稍后检索该值,只需使用 ConstantArray::getAsCString()。如果您打开了断言,它将断言是否出现问题(例如,您将尝试从数组中获取没有零终止符的字符串)。
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).
在 C 代码上运行 http://llvm.org/demo/
char *x = "asdf ";
给出:基本上,要获取字符串的地址,您必须构建一个包含它的全局。如果您无法弄清楚,可以将 http://llvm.org/demo/ 切换为输出 C++ API 调用如何做到这一点。
Running http://llvm.org/demo/ on the C code
char *x = "asdf";
gives:Basically, to get the address of a string, you have to build a global containing it. You can switch http://llvm.org/demo/ to output C++ API calls if you have trouble figuring out how to do that.