[LLVMdev] How to create global string array? (user question)
Yuri
yuri at rawbw.com
Tue Jun 1 12:34:14 PDT 2010
I am trying to create such module with API (it's equivalent to c++:
const char* ss[] = {"s1","s2"};):
@ss = global [2 x i8*] [i8* getelementptr inbounds ([3 x i8]* @.str1,
i32 0, i32 0), i8* getelementptr inbounds ([3 x i8]* @.str2, i32 0, i32
0)] ; <[2 x i8*]*> [#uses=0]
@.str1 = private constant [3 x i8] c"s1\00", align 1 ; <[3 x i8]*> [#uses=1]
@.str2 = private constant [3 x i8] c"s2\00", align 1 ; <[3 x i8]*> [#uses=1]
Code below doesn't compile because to initialize GlobalVariable I need
ConstantArray*, ConstantArray::get requires vector<Constant*>, but
getelementptr is an instruction and it's not Constant*, but Value*.
What is the correct way to create such module? It's really tye
Yuri
--- c++ code ---
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/Analysis/Verifier.h"
#include <stdio.h>
#include <vector>
using namespace llvm;
using namespace std;
Constant* mk_string(Module *M, LLVMContext &Context, const string &svalue) {
ArrayType *Ty = ArrayType::get(Type::getInt8Ty(Context),svalue.size()+1);
GlobalVariable *GV = new llvm::GlobalVariable(
*M,
Ty,
true,
GlobalValue::InternalLinkage,
ConstantArray::get(Context,svalue),
"str",
0,
false,
0);
vector<Constant*> idxs;
idxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), 0, false));
idxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), 0, false));
return (GetElementPtrInst::Create(
GV,
idxs.begin(),
idxs.end(),
"gepi",
(BasicBlock*)0
));
}
main() {
LLVMContext Context;
Module *M = new Module("test", Context);
//
vector<Constant*> strs;
strs.push_back(mk_string(M, Context, "s1"));
strs.push_back(mk_string(M, Context, "s2"));
ArrayType *Ty =
ArrayType::get(Type::getInt8Ty(Context)->getPointerTo(),2);
new llvm::GlobalVariable(
*M,
Ty,
true,
GlobalValue::InternalLinkage,
ConstantArray::get(Ty,strs),
"ss",
0,
false,
0);
verifyModule(*M);
delete M;
}
More information about the llvm-dev
mailing list