James Courtier-Dutton via llvm-dev
2020-Aug-01 20:25 UTC
[llvm-dev] Accessing constant contents
Hi, I have a global variable in LLVM IR: @mem8 = private unnamed_addr constant [9 x i8] c"Hello12\0A\00", align 1 I have loaded the module, so now have: llvm::Value *value1; ... value1->print(llvm::outs()); That will output: @mem8 = private unnamed_addr constant [9 x i8] c"Hello12\0A\00", align 1 What method should I use to access the contents of the constant. I.e. I want the "Hello12" bytes contained in @mem8 to be put in a std::vector<int8_t> I can access the Type hierarchy, but cannot find how to access the value contents. Kind Regards James
Johannes Doerfert via llvm-dev
2020-Aug-01 22:32 UTC
[llvm-dev] Accessing constant contents
Hi James, I haven't tried it but if my memory is not failing me you should be able to ask for the initializer of the llvm::GlobalVariable, which is a constant. That should actually be a llvm::ConstantDataSequential in this case which has methods like isString, getString, getCString,... Oh, and your value1 is the llvm::GlobalVariable I am talking about, so something along the lines of: auto *GV = cast<GlobalVariable>(value1); auto *CDS = cast<ConstantDataSequential>(GV->getInitializer()); CDS->getString(); Hope this helps, Johannes On 8/1/20 3:25 PM, James Courtier-Dutton via llvm-dev wrote:> Hi, > > I have a global variable in LLVM IR: > > @mem8 = private unnamed_addr constant [9 x i8] c"Hello12\0A\00", align 1 > > I have loaded the module, so now have: > llvm::Value *value1; > ... > value1->print(llvm::outs()); > That will output: > @mem8 = private unnamed_addr constant [9 x i8] c"Hello12\0A\00", align 1 > > What method should I use to access the contents of the constant. > I.e. I want the "Hello12" bytes contained in @mem8 to be put in a > std::vector<int8_t> > > I can access the Type hierarchy, but cannot find how to access the > value contents. > > Kind Regards > > James > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Krzysztof Parzyszek via llvm-dev
2020-Aug-03 17:05 UTC
[llvm-dev] Accessing constant contents
The "hello" string is a part of the GlobalVariable's initializer. You can use GlobalVariable::getInitializer to get a Constant with the initializer value, and then, depending on the actual type of it you can get the contents of it. -- Krzysztof Parzyszek kparzysz at quicinc.com AI tools development -----Original Message----- From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of James Courtier-Dutton via llvm-dev Sent: Saturday, August 1, 2020 3:25 PM To: llvm-dev <llvm-dev at lists.llvm.org> Subject: [EXT] [llvm-dev] Accessing constant contents Hi, I have a global variable in LLVM IR: @mem8 = private unnamed_addr constant [9 x i8] c"Hello12\0A\00", align 1 I have loaded the module, so now have: llvm::Value *value1; ... value1->print(llvm::outs()); That will output: @mem8 = private unnamed_addr constant [9 x i8] c"Hello12\0A\00", align 1 What method should I use to access the contents of the constant. I.e. I want the "Hello12" bytes contained in @mem8 to be put in a std::vector<int8_t> I can access the Type hierarchy, but cannot find how to access the value contents. Kind Regards James _______________________________________________ LLVM Developers mailing list llvm-dev at lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev