00001 //===-- codegen/CodeGenCapsule.cpp ---------------------------- -*- C++ -*-===// 00002 // 00003 // This file is distributed under the MIT license. See LICENSE.txt for details. 00004 // 00005 // Copyright (C) 2009, Stephen Wilson 00006 // 00007 //===----------------------------------------------------------------------===// 00008 00009 #include "comma/ast/Decl.h" 00010 #include "comma/codegen/CodeGen.h" 00011 #include "comma/codegen/CodeGenCapsule.h" 00012 #include "comma/codegen/CodeGenRoutine.h" 00013 00014 using namespace comma; 00015 00016 using llvm::dyn_cast; 00017 using llvm::cast; 00018 using llvm::isa; 00019 00020 CodeGenCapsule::CodeGenCapsule(CodeGen &CG, Domoid *domoid) 00021 : CG(CG), 00022 capsule(domoid), 00023 linkName(CodeGen::getLinkName(domoid)) 00024 { 00025 emit(); 00026 } 00027 00028 void CodeGenCapsule::emit() 00029 { 00030 // Declare every subroutine in the add. 00031 if (AddDecl *add = capsule->getImplementation()) { 00032 typedef DeclRegion::DeclIter iterator; 00033 for (iterator iter = add->beginDecls(); 00034 iter != add->endDecls(); ++iter) { 00035 if (SubroutineDecl *SR = dyn_cast<SubroutineDecl>(*iter)) { 00036 CodeGenRoutine CGR(*this); 00037 CGR.declareSubroutine(SR); 00038 } 00039 } 00040 } 00041 00042 // Codegen each subroutine. 00043 if (AddDecl *add = capsule->getImplementation()) { 00044 typedef DeclRegion::DeclIter iterator; 00045 for (iterator iter = add->beginDecls(); 00046 iter != add->endDecls(); ++iter) { 00047 if (SubroutineDecl *SR = dyn_cast<SubroutineDecl>(*iter)) { 00048 CodeGenRoutine CGR(*this); 00049 CGR.emitSubroutine(SR); 00050 } 00051 } 00052 } 00053 } 00054 00055 unsigned CodeGenCapsule::addCapsuleDependency(DomainInstanceDecl *instance) 00056 { 00057 // If the given instance is parameterized, insert each argument as a 00058 // dependency, ignoring abstract domains (the formal parameters of a functor 00059 // need not be recorded). 00060 if (instance->isParameterized()) { 00061 typedef DomainInstanceDecl::arg_iterator iterator; 00062 for (iterator iter = instance->beginArguments(); 00063 iter != instance->endArguments(); ++iter) { 00064 DomainType *argTy = cast<DomainType>(*iter); 00065 if (!argTy->isAbstract()) { 00066 DomainInstanceDecl *argInstance = argTy->getInstanceDecl(); 00067 assert(argInstance && "Bad domain type!"); 00068 requiredInstances.insert(argInstance); 00069 } 00070 } 00071 } 00072 return requiredInstances.insert(instance); 00073 } 00074