00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include "CommaRT.h"
00010 #include "DomainInstance.h"
00011 
00012 using namespace comma;
00013 
00014 using llvm::dyn_cast;
00015 using llvm::cast;
00016 using llvm::isa;
00017 
00018 DomainInstance::DomainInstance(CommaRT &CRT)
00019     : CRT(CRT),
00020       CG(CRT.getCodeGen()),
00021       TD(CG.getTargetData()),
00022       theType(CG.getOpaqueTy()) { }
00023 
00024 void DomainInstance::init()
00025 {
00026     std::vector<const llvm::Type*> members;
00027 
00028     const llvm::PointerType *InfoPtrTy;
00029     const llvm::PointerType *InstancePtrTy;
00030 
00031     InfoPtrTy = CRT.getType<CommaRT::CRT_DomainInfo>();
00032     InstancePtrTy = llvm::PointerType::getUnqual(theType.get());
00033 
00034     members.push_back(InfoPtrTy);
00035     members.push_back(theType.get());
00036     members.push_back(CG.getPointerType(InstancePtrTy));
00037     members.push_back(CG.getPointerType(InstancePtrTy));
00038 
00039     llvm::StructType *InstanceTy = CG.getStructTy(members);
00040     cast<llvm::OpaqueType>(theType.get())->refineAbstractTypeTo(InstanceTy);
00041 }
00042 
00043 const std::string DomainInstance::theTypeName("comma_instance_t");
00044 
00045 const llvm::StructType *DomainInstance::getType() const
00046 {
00047     return cast<llvm::StructType>(theType.get());
00048 }
00049 
00050 const llvm::PointerType *DomainInstance::getPointerTypeTo() const
00051 {
00052     return llvm::PointerType::getUnqual(theType.get());
00053 }
00054 
00056 llvm::Value *DomainInstance::loadInfo(llvm::IRBuilder<> &builder,
00057                                       llvm::Value *Instance) const
00058 {
00059     assert(Instance->getType() == getPointerTypeTo() &&
00060            "Wrong type of LLVM Value!");
00061 
00062     llvm::Value *InfoAddr = builder.CreateStructGEP(Instance, Info);
00063     return builder.CreateLoad(InfoAddr);
00064 }
00065 
00068 llvm::Value *DomainInstance::loadParamVec(llvm::IRBuilder<> &builder,
00069                                           llvm::Value *instance) const
00070 {
00071     assert(instance->getType() == getPointerTypeTo() &&
00072            "Wrong type of LLVM Value!");
00073 
00074     llvm::Value *paramsAddr = builder.CreateStructGEP(instance, Params);
00075     return builder.CreateLoad(paramsAddr);
00076 }
00077 
00080 llvm::Value *DomainInstance::loadParam(llvm::IRBuilder<> &builder,
00081                                        llvm::Value *instance,
00082                                        unsigned paramIdx) const
00083 {
00084     assert(instance->getType() == getPointerTypeTo() &&
00085            "Wrong type of LLVM Value!");
00086 
00087     llvm::Value *index = llvm::ConstantInt::get(CG.getInt32Ty(), paramIdx);
00088 
00089     llvm::Value *paramVec = loadParamVec(builder, instance);
00090     llvm::Value *viewAddr = builder.CreateGEP(paramVec, index);
00091     return builder.CreateLoad(viewAddr);
00092 }
00093 
00094 llvm::Value *DomainInstance::loadLocalVec(llvm::IRBuilder<> &builder,
00095                                           llvm::Value *instance) const
00096 {
00097     llvm::Value *localVecAddr = builder.CreateStructGEP(instance, Requirements);
00098     return builder.CreateLoad(localVecAddr);
00099 }
00100 
00101 void DomainInstance::setLocalVec(llvm::IRBuilder<> &builder,
00102                                  llvm::Value *instance,
00103                                  llvm::Value *vec) const
00104 {
00105     llvm::Value *dst = builder.CreateStructGEP(instance, Requirements);
00106     builder.CreateStore(vec, dst);
00107 }
00108 
00109 llvm::Value *DomainInstance::loadLocalInstance(llvm::IRBuilder<> &builder,
00110                                                llvm::Value *instance,
00111                                                unsigned ID) const
00112 {
00113     assert(instance->getType() == getPointerTypeTo() &&
00114            "Wrong type of LLVM value!");
00115 
00116     
00117     llvm::Value *elt = loadLocalVec(builder, instance);
00118 
00119     
00120     llvm::Value *index = llvm::ConstantInt::get(CG.getInt32Ty(), ID);
00121     elt = builder.CreateGEP(elt, index);
00122 
00123     
00124     return builder.CreateLoad(elt);
00125 }