- // For now we just allocate some memory on the heap, this can be
- // dramatically improved.
- const Type *Ty = ((Value*)Constants[i])->getType();
- void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
- TheVM->InitializeMemory(Constants[i], Addr);
+ const Type *Ty = Constants[i]->getType();
+ unsigned Size = TheJIT->getTargetData().getTypeSize(Ty);
+ unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
+ // Make sure to take into account the alignment requirements of the type.
+ TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1);
+
+ // Remember the offset this element lives at.
+ ConstantOffset.push_back(TotalSize);
+ TotalSize += Size; // Reserve space for the constant.
+ }
+
+ // Now that we know how much memory to allocate, do so.
+ char *Pool = new char[TotalSize];
+
+ // Actually output all of the constants, and remember their addresses.
+ for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
+ void *Addr = Pool + ConstantOffset[i];
+ TheJIT->InitializeMemory(Constants[i], Addr);