Loop IR insertion

Hi,

I am trying to insert a Loop IR into the existed bytecode file.

insertion part by C code,

char *p[n]; // pointer array for storing the address of strings
int i;
for(i=0;i<n;i++){

(p[i])[2] = (p[i])[2] ^ 0x27;

}

My questions are

  1. for local variable ‘char *p[n]’ , it is represented by IR as alloca …, so could I insert local variable (pointer array) directly as same way as insert global variable or I have to use new allocaInst to construct it.

  2. from instructions.h, there are some instructions class, but could I insert Xor instruction? or could I insert all IR instruction as I want? if so, would you like to give me some hints or examples.

Thanks

Hi,

I am trying to insert a Loop IR into the existed bytecode file.

insertion part by C code,

char *p[n]; // pointer array for storing the address of strings
int i;
for(i=0;i<n;i++){

(p[i])[2] = (p[i])[2] ^ 0x27;

}

My questions are

1. for local variable 'char *p[n]' , it is represented by IR as alloca
...., so could I insert local variable (pointer array) directly as
same way as insert global variable or I have to use new allocaInst to
construct it.

If n is constant and you know its value then you can use alloca.
Otherwise you need to use malloc to allocate the memory for p.

2. from instructions.h, there are some instructions class, but could I
insert Xor instruction? or could I insert all IR instruction as I
want? if so, would you like to give me some hints or examples.

Yes, you can insert any instructions you like. Typically the way to do
this is with a basic block so they go at the end of the basic block.
There are lots of static methods in the instruction classes to help you
do this.

For example, look at the examples/HowToUseJIT.cpp example. Here's a
snippet from that program:

// because of the last argument.
  BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
                                                                                
  // Get pointers to the constant `1'.
  Value *One = ConstantSInt::get(Type::IntTy, 1);
                                                                                
  // Get pointers to the integer argument of the add1 function...
  assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
  Argument *ArgX = Add1F->abegin(); // Get the arg
  ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
                                                                                
  // Create the add instruction, inserting it into the end of BB.
  Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
                                                                                
  // Create the return instruction and add it to the basic block
  new ReturnInst(Add, BB);

Note that the last two lines show two different ways of creating
instructions: one with the static BinaryOperator::createAdd instruction,
the other directly allocating the instruction. In both cases the
BasicBlock, BB, is the last parameter which says "put the instruction at
the end of the basic block BB"

Thanks

Hope this helps.

I am trying to insert a Loop IR into the existed bytecode file.

insertion part by C code,

char *p[n]; // pointer array for storing the address of strings
int i;
for(i=0;i<n;i++){
(p[i])[2] = (p[i])[2] ^ 0x27;
}

2. from instructions.h, there are some instructions class, but could I
insert Xor instruction?

See llvm/include/llvm/InstrTypes.h and Instruction.def, which together
provide BinaryOperator::createXor() .

or could I insert all IR instruction as I want? if so, would you like
to give me some hints or examples.

I'm not sure what you're asking here, but if you are wondering how to
express the C code in LLVM, consider using llvm-gcc to compile it
without optimizations (-Wa,-disable-opt -Wl,-disable-opt) and see what
it produces.