00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "DomainInstance.h"
00010 #include "DomainView.h"
00011 #include "comma/codegen/CommaRT.h"
00012
00013 using namespace comma;
00014
00015 using llvm::dyn_cast;
00016 using llvm::cast;
00017 using llvm::isa;
00018
00019 DomainView::DomainView(CommaRT &CRT)
00020 : CRT(CRT),
00021 CG(CRT.getCodeGen()),
00022 TD(CG.getTargetData()),
00023 theType(llvm::OpaqueType::get()) { }
00024
00025 void DomainView::init()
00026 {
00027 std::vector<const llvm::Type*> members;
00028 const llvm::Type* IntPtrTy = TD.getIntPtrType();
00029
00030 members.push_back(CRT.getType<CommaRT::CRT_DomainInstance>());
00031 members.push_back(IntPtrTy);
00032
00033 llvm::StructType *ViewTy = llvm::StructType::get(members);
00034 cast<llvm::OpaqueType>(theType.get())->refineAbstractTypeTo(ViewTy);
00035 }
00036
00037 const std::string DomainView::theTypeName("comma_domain_view_t");
00038
00039 const llvm::StructType *DomainView::getType() const
00040 {
00041 return cast<llvm::StructType>(theType.get());
00042 }
00043
00044 const llvm::PointerType *DomainView::getPointerTypeTo() const
00045 {
00046 return llvm::PointerType::getUnqual(theType.get());
00047 }
00048
00050 llvm::Value *DomainView::loadInstance(llvm::IRBuilder<> &builder,
00051 llvm::Value *DView) const
00052 {
00053 assert(DView->getType() == getPointerTypeTo() &&
00054 "Wrong type of LLVM Value!");
00055
00056 llvm::Value *instanceAddr = builder.CreateStructGEP(DView, Instance);
00057 return builder.CreateLoad(instanceAddr);
00058 }
00059
00061 llvm::Value *DomainView::loadIndex(llvm::IRBuilder<> &builder,
00062 llvm::Value *view) const
00063 {
00064 assert(view->getType() == getPointerTypeTo() &&
00065 "Wrong type of LLVM Value!");
00066
00067 llvm::Value *indexAddr = builder.CreateStructGEP(view, Index);
00068 return builder.CreateLoad(indexAddr);
00069 }
00070
00071
00074 llvm::Value *DomainView::downcast(llvm::IRBuilder<> &builder,
00075 llvm::Value *view,
00076 unsigned sigIndex) const
00077 {
00078 assert(view->getType() == getPointerTypeTo() &&
00079 "Wrong type of LLVM Value!");
00080
00081
00082
00083 if (sigIndex == 0)
00084 return view;
00085
00086 const DomainInstance *DInstance = CRT.getDomainInstance();
00087
00088 llvm::Value *sigIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, sigIndex);
00089
00090 llvm::Value *instance = loadInstance(builder, view);
00091 llvm::Value *index = builder.CreateAdd(loadIndex(builder, view), sigIdx);
00092 return DInstance->loadView(builder, instance, index);
00093 }
00094