[LLVMdev] How to read memory data througn adress of unsigned long

Frits van Bommel fvbommel at gmail.com
Sat Mar 26 00:52:33 PDT 2011


On Sat, Mar 26, 2011 at 6:13 AM, Reid Kleckner <reid.kleckner at gmail.com> wrote:
> On Fri, Mar 25, 2011 at 8:53 PM, Michael.Kang <blackfin.kang at gmail.com> wrote:
>> I also try to use IntToPtrInstr to do some transform as the following:
>> ################################
>>      Type const *intptr_type =
>> cpu->dyncom_engine->exec_engine->getTargetData()->getIntPtrType(_CTX());
>>       Value* ptr = new IntToPtrInst(v, intptr_type, "", bb);
>>       Value* data = new LoadInst(ptr, "", false, bb);
>>  ########################################
>>  still encounter segmentation fault
>>
>> Any person can give me some hints for my case? Thanks in advance. I
>> have tried a long time  with different ways.
>
> Not sure why, but using a debug build would be helpful and using
> IRBuilder would help make your code less error-prone.

getIntPtrType() doesn't return a pointer type, it returns an integer
type that's at least as big as a pointer on the target in question. In
other words: it returns a type that can be used as C's intptr_t.

Micheal, you need to create an actual pointer type based on the type
of value you want to load. For example, to load a 32-bit integer:
  Type* T = Type::getInt32Ty(_CTX())->getPointerTo();
  Value* ptr = new IntToPtrInst(v, T, "", bb);
  Value* data = new LoadInst(ptr, "", false, bb);

I agree that it's better to use IRBuilder though: it makes things
easier (no need to keep passing 'bb', for example) and can do some
automatic early constant folding for you (if you want).
And it definitely helps to compile with assertions enabled so you get
more meaningful errors when you make mistakes.




More information about the llvm-dev mailing list