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?
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
Thanks. It works.
Actually, the signature of the constructor given in the online documentation
is different from the actual source code.
Nick Lewycky wrote:
Hi subramanyam,
Actually, the signature of the constructor given in the online documentation
is different from the actual source code.
which bit of the documentation? Any chance of a patch that fixes it?
Ciao,
Duncan.
I am using llvm-2.6.
In the online documentation, the signature is
GlobalVariable::GlobalVariable ( const Type * Ty,
bool isConstant,
LinkageTypes Linkage,
Constant * Initializer = 0,
const Twine & Name = "",
bool ThreadLocal = false,
unsigned AddressSpace = 0
)
the link to the documenation is
http://llvm.org/doxygen/classllvm_1_1GlobalVariable.html
There are two constructors given. Second constructor signature is same as
the source code. The first one differs though.
In the source code the prototype of the constructor is at line 53 in
GlobalVariable.h file.
The signature is
GlobalVariable(LLVMContext &Context, const Type *Ty, bool isConstant,
LinkageTypes Linkage,
Constant *Initializer = 0, const Twine &Name = "",
bool ThreadLocal = false, unsigned AddressSpace = 0);
Duncan Sands wrote:
subramanyam wrote:
I am using llvm-2.6. In the online documentation, the signature is GlobalVariable::GlobalVariable ( const Type * Ty,
bool isConstant,
LinkageTypes Linkage,
Constant * Initializer = 0,
const Twine & Name = "",
bool ThreadLocal = false,
unsigned AddressSpace = 0 )
the link to the documenation is
http://llvm.org/doxygen/classllvm_1_1GlobalVariable.html
The doxygen docs on the web page are updated every evening to reflect new changes in mainline LLVM. If the API for GlobalVariables has changed, then there will be a mismatch between LLVM 2.6 and what is on the web.
Thankfully, API changes aren't *that* frequent (although when they do occur, they are not always timed to my liking).
It has been recommended to me to use the IRBuilder class to create LLVM IR instead of using the various IR classes directly. I've been told that IRBuilder is less likely to change from release to release. I haven't tried it myself yet, but you may want to consider it for your project.
-- John T.