[LLVMdev] JIT and array pointers
dummy1 at boxpl.com
dummy1 at boxpl.com
Sun Apr 10 19:00:23 PDT 2005
On Sun, 2005-04-10 at 19:47 -0500, Chris Lattner wrote:
> On Mon, 11 Apr 2005, Paul wrote:
> > I'm trying to pass an array pointer to my run-time generated module, and
> > after a long time of searching for the answer, the only thing I got was
> > a headache. Basically I have a few arrays (few megabytes which will
> > sometimes be accessed as 8 / 16 / 32 / 64 bit binary / fp values), that
> > will be modified by both the generated module, and my main c program, so
> > I would like to put those into global variables. I also tried passing it
> > as a function argument (with argument type PointerType::get
> > (Type::UIntTy)), and failed miserably. I feel lost. Right now I got only
> > a small module with some simple operations (big thanks to the examples,
> > they really help a lot). Maybe its because I'm not a c++ guy, but I
> > really want to use llvm, especially because of it's nice job at
> > optimizing code. And I only need a few basic building blocks to get
> > things started.
>
> There are many possible ways to do this, can you be a bit more specific
> about what you're trying to do?
>
> -Chris
>
Here is a basic example:
============================================
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");
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]
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);
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.
More information about the llvm-dev
mailing list