[LLVMdev] JIT and array pointers
Chris Lattner
sabre at nondot.org
Sun Apr 10 20:23:25 PDT 2005
On Mon, 11 Apr 2005, dummy1 at boxpl.com wrote:
>> There are many possible ways to do this, can you be a bit more specific
>> about what you're trying to do?
> Here is a basic example:
Ah, ok, I see what you're trying to do. Below is some *pseudo* code for
the basic idea:
> ============================================
> unsigned int buff[4096];
>
> int main (void)
> {
> buff[0] = 1;
>
> // compile and execute code that will change buff[0] to 2
> Value *c0 = ConstantUInt::get (Type::UIntTy, 0);
> Value *c2 = ConstantUInt::get (Type::UIntTy, 2);
>
> Module *m = new Module ("test_module");
Here, create an LLVM GlobalVariable 'BuffGV' of the appropriate type, and
insert it into m:
GlobalVariable *BuffGV = new GlobalVariable([4096 x uint], false,
GlobalVariable::ExternalLinkage, 0, "Buff", m);
> Function *f = m->getOrInsertFunction ("test_function", Type::VoidTy,
> 0);
> BasicBlock *b = new BasicBlock ("entry_block", f);
>
> // Here is the problem.
> // I need to get the buff pointer into variable, which I can
> // pass to GetElementPtrInst
> // Value *pbuff = ??
> // -> %pbuff = external global [4096 x uint]
pbuff = BuffGV;
> Value *ptr = new GetElementPtrInst (pbuff, c0, c0, "ptr", b);
> // -> %ptr = getelementptr [4096 x uint]* %buff, int 0, int 0
> new StoreInst (c2, ptr, b);
> // -> store uint 2, uint* %ptr
> new ReturnInst (b);
>
> ExistingModuleProvider *mp = new ExistingModuleProvider (m);
> ExecutionEngine *ee = ExecutionEngine::create (mp, false);
Tell the EE that buffGV -> buff:
ee->addGlobalMapping(BuffGV, buff);
Then you should be good to go!
-Chris
> cout << "constructed module:\n\n" << *m << "\n\n";
>
> vector <GenericValue> args;
> GenericValue ret = ee->runFunction (f, args);
>
> printf ("buff[0] is now: %d\n", buff[0]);
> return 0;
> }
> ============================================
>
> I've been trying to do it with GlobalVariable. It seems like a
> completely basic thing, but I just can't get it to work.
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev
>
-Chris
--
http://nondot.org/sabre/
http://llvm.cs.uiuc.edu/
More information about the llvm-dev
mailing list