RE: Question about Global Variable

Thanks for your reply.

After I change Cstr to gI, it compiled successfully. Thanks again.

Another question is for constructing getelementpt.

// C code
char gStrA[10] = "test str"; // here is Global variable,gStrA and initializer "test str"
char gStrB[10]= "test str2";
main(){
    int = i;
    char *pGVars[20]; // here, the pGVar is for storing the address of each initializer, i.e. "test str"
    pGVars[0] = gStrA;
    pGVars[1] = gStrB;

    // change the content of gStrA
    for(i=0;i<5;i++){
        pGVars[0][i] ^= 0x27;
   }
}

the following IR code is the loop part code which is corresponding to the above C code
no_exit: ; preds = %loopentry
        %tmp.31 = getelementptr [3 x sbyte*]* %pGVars, int 0, int 0 ; <sbyte**> [#uses=1]
        %tmp.32 = load sbyte** %tmp.31 ; <sbyte*> [#uses=1]
        %tmp.33 = load int* %i ; <int> [#uses=1]
        %tmp.34 = getelementptr sbyte* %tmp.32, int %tmp.33 ; <sbyte*> [#uses=1]

I am trying to construct all those IR instructions, for the first three, it is ok, as the following
       
GetElementPtrInst *GEP = new GetElementPtrInst( pGVars, idxVec, "GetAryElement", InsertPos);
Value *ldAddr = new LoadInst(GEP,"LdAryAddr",InsertPos);
Value *ldAddr1 = new LoadInst(numidx, "idx_i", InsertPos);

After this I need construct another getelementptr, and from the above IR code, the second getelementptr take the result of load as parameters. But there is no such prototype construction api for this case. The correct one should be as the following,

GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
      const std::string &Name = "", Instruction *InsertBefore =0);
  GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, // or Idx1, Idx2
      const std::string &Name, BasicBlock *InsertAtEnd);

How could I create the fourth IR ? Thanks

        %tmp.35 = getelementptr [3 x sbyte*]* %pGVars, int 0, int 0 ; <sbyte**> [#uses=1]
        %tmp.36 = load sbyte** %tmp.35 ; <sbyte*> [#uses=1]
        %tmp.37 = load int* %i ; <int> [#uses=1]
        %tmp.38 = getelementptr sbyte* %tmp.36, int %tmp.37 ; <sbyte*> [#uses=1]
        %tmp.39 = load sbyte* %tmp.38 ; <sbyte> [#uses=1]
        %tmp.40 = xor sbyte %tmp.39, 39 ; <sbyte> [#uses=1]
        store sbyte %tmp.40, sbyte* %tmp.34
        %tmp.41 = load int* %i ; <int> [#uses=1]
        %inc = add int %tmp.41, 1 ; <int> [#uses=1]
        store int %inc, int* %i
        br label %loopentry

Qiuyu

the following IR code is the loop part code which is corresponding to the above C code

...

       %tmp.34 = getelementptr sbyte* %tmp.32, int %tmp.33 ; <sbyte*> [#uses=1]

I am trying to construct all those IR instructions, for the first three, it is ok, as the following
Value *ldAddr1 = new LoadInst(numidx, "idx_i", InsertPos);

After this I need construct another getelementptr, and from the above IR code, the second getelementptr take the result of load as parameters. But there is no such prototype construction api for this case. The correct one should be as the following,

GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
     const std::string &Name = "", Instruction *InsertBefore =0);
GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, // or Idx1, Idx2
     const std::string &Name, BasicBlock *InsertAtEnd);

How could I create the fourth IR ? Thanks

You can use either of those two constructors, e.g. something like this:

std::vector<Value*> Idxs;
Idxs.push_back(ldAddr1);
Value *GEP2 = new GetElementPtrInst(ldAddr, "whatever", Idxs, InsertPos);

-Chris