问题
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