[LLVMdev] The question about how to refer an element in a pointer array by a dynamic index by IR

John Criswell criswell at uiuc.edu
Fri Jul 2 07:31:13 PDT 2010


Dear Michael,

I didn't see a response to your email on llvmdev, so I thought I'd take 
a shot at answering it.

Michael.Kang wrote:
> Now I am trying to use LLVM to write a simulator and take LLVM as IR
> to dynamic translate machine code,
> I encounter some issues when try to write some IR to express writting
> a value to the reigster.
> The situation is described as the following description.
>
> write_reg is my function for writing "v" to the register indicated by
> "index".  ptr_gpr is the struct pointer for my general register file.
>
>
> write_reg(cpu_t *cpu, uint32_t index, Value *v,BasicBlock *bb ){
>        Value **regs = cpu->ptr_gpr;
> }
>   

So, if I understand correctly, you are generating the code for 
write_reg() at run-time, and you're wanting to know how to generate code 
that indexes into your register file structure, correct?

This basically boils down to a "How do I use GetElementPtr?" question.  
To answer that, I'll just redirect you to the GetElementPtr document 
(http://llvm.org/docs/GetElementPtr.html).  That document describes, in 
detail, how to use the GEP instruction.

You may also want to write some example C code that does the indexing 
operation you want and then compile it to LLVM assembly using llvm-gcc 
(llvm-gcc -emit-llvm -S file.c, I believe).  That'll give you an example 
of the LLVM code that you want to generate.

-- John T.

> If index do not need to do any runtime calculation, I can write the
> following sentence to fulfil my requirement.
>
>  new StoreInst(v, regs[index], bb);
>
> But if I need to do some runtime calculation for index such as
> modular. How to deal that?
> 1. Use IR to calculate index by some
> "BinaryOperator::Create(Instruction::Add, a, b, "", bb)" sentences.
> 2. Use the above index to refer the register in "regs".
> Should I use GEP in second step ? And how to use it?
>
> Thanks
> MK
>
>   




More information about the llvm-dev mailing list