How to define string type in getOrInsertFunction() llvm?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 02:36:23

问题


I'm new to llvm and was trying to do instrument. But I found LLVM API only has primitive types, like: getInt32Ty(Ctx).. What i want to do use getOrInsertFunction(),the function argument type is string type.As is known, when argument type is int, we can do it like is :

 LLVMContext &Ctx = F.getContext();
  Constant *logFunc = F.getParent()->getOrInsertFunction(
    "logop", Type::getVoidTy(Ctx), Type::getInt32Ty(Ctx), NULL );

Type::getInt32Ty(Ctx) is the function argument type (int), what i want to do is:

getOrInsertFunction(
    "logop", Type::getVoidTy(Ctx), string type, NULL);

The string type i don't know how to define it .In short, could you please tell me how to define it, thanks!


回答1:


LLVM IR does not define any special string or char types.

Usually either [N x i8] or i8* are used, but it's really up to you - for example, Java-style strings will probably be a struct with some i32 for string length and an i16* for the UTF-16 code points.

LLVM IR does have a "string literal" which is typed as an i8 array - for example c"hello world\0A\00" is [13 x i8]. But that doesn't dictate what string form you should be using.

Keep in mind that if your function is supposed to interop with something, e.g. a hosting C++ application, then you need to use the same string type - in that case whatever std::string is compiling to. You can use Clang or this online demo to check what that type is.



来源:https://stackoverflow.com/questions/35893006/how-to-define-string-type-in-getorinsertfunction-llvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!