subramanyam
2010-May-11 04:51 UTC
[LLVMdev] How to create Global Variables using LLVM API?
I am new to LLVM API. and I am experimenting with it. I want to create Global Variables in a module. I am able to create local variables using IRBuilder class. I tried using GlobalVariable class to create global variables. but it doesn't work. my code looks like this: Module* mod = new Module("test", getGlobalContext()); Constant* c = mod->getOrInsertFunction("main", IntegerType::get(getGlobalContext(), 32), NULL); Function* main = cast<Function> (c); main->setCallingConv(CallingConv::C); Twine s("foo"); StringRef s1("foo"); Constant *cons = ConstantArray::get(getGlobalContext(),s1, true); GlobalVariable val(*mod, (Type*) ArrayType::get(Type::getInt8Ty(getGlobalContext()), 4), true,GlobalValue::ExternalLinkage, cons, s); Application stops running after reaching the last line. can somebody help me out here? -- View this message in context: http://old.nabble.com/How-to-create-Global-Variables-using-LLVM-API--tp28519938p28519938.html Sent from the LLVM - Dev mailing list archive at Nabble.com.
Nick Lewycky
2010-May-11 05:11 UTC
[LLVMdev] How to create Global Variables using LLVM API?
subramanyam wrote:> > I am new to LLVM API. and I am experimenting with it. > I want to create Global Variables in a module. > I am able to create local variables using IRBuilder class. I tried using > GlobalVariable class to create global variables. > but it doesn't work. my code looks like this: > > Module* mod = new Module("test", getGlobalContext()); > Constant* c = mod->getOrInsertFunction("main", > IntegerType::get(getGlobalContext(), 32), NULL); > Function* main = cast<Function> (c); > main->setCallingConv(CallingConv::C); > Twine s("foo"); > StringRef s1("foo"); > Constant *cons = ConstantArray::get(getGlobalContext(),s1, true); > GlobalVariable val(*mod, (Type*) > ArrayType::get(Type::getInt8Ty(getGlobalContext()), 4), > true,GlobalValue::ExternalLinkage, cons, s);Firstly, don't cast to Type*. You need to #include "llvm/DerivedTypes.h" instead. But that's not your bug. Don't allocate GlobalVariable val on the stack. You need to 'new' it, ala. 'GlobalVariable *val = new GlobalVariable(...);' I haven't tried running your code, but give that a shot! Nick> > > Application stops running after reaching the last line. can somebody help me > out here? > >
subramanyam
2010-May-12 05:45 UTC
[LLVMdev] How to create Global Variables using LLVM API?
Thanks. It works. Actually, the signature of the constructor given in the online documentation is different from the actual source code. Nick Lewycky wrote:> > subramanyam wrote: >> >> I am new to LLVM API. and I am experimenting with it. >> I want to create Global Variables in a module. >> I am able to create local variables using IRBuilder class. I tried using >> GlobalVariable class to create global variables. >> but it doesn't work. my code looks like this: >> >> Module* mod = new Module("test", getGlobalContext()); >> Constant* c = mod->getOrInsertFunction("main", >> IntegerType::get(getGlobalContext(), 32), NULL); >> Function* main = cast<Function> (c); >> main->setCallingConv(CallingConv::C); >> Twine s("foo"); >> StringRef s1("foo"); >> Constant *cons = ConstantArray::get(getGlobalContext(),s1, true); >> GlobalVariable val(*mod, (Type*) >> ArrayType::get(Type::getInt8Ty(getGlobalContext()), 4), >> true,GlobalValue::ExternalLinkage, cons, s); > > Firstly, don't cast to Type*. You need to #include "llvm/DerivedTypes.h" > instead. But that's not your bug. > > Don't allocate GlobalVariable val on the stack. You need to 'new' it, > ala. 'GlobalVariable *val = new GlobalVariable(...);' > > I haven't tried running your code, but give that a shot! > > Nick > > > >> >> >> Application stops running after reaching the last line. can somebody help >> me >> out here? >> >> > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >-- View this message in context: http://old.nabble.com/How-to-create-Global-Variables-using-LLVM-API--tp28519938p28532205.html Sent from the LLVM - Dev mailing list archive at Nabble.com.
Maybe Matching Threads
- [LLVMdev] How to create Global Variables using LLVM API?
- [LLVMdev] How to create Global Variables using LLVM API?
- [BUG] Incorrect ASCII escape characters on Mac
- [LLVMdev] Calling a function with a pointer to a global char array as argument
- How to create a sprintf call in IR