Ignore:
Timestamp:
Jan 1, 2015, 2:23:50 PM (4 years ago)
Author:
cameron
Message:

Create ability to install arbitrary external functions

Location:
icGREP/icgrep-devel/icgrep/pablo
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r4378 r4382  
    6060#include "llvm/Support/CommandLine.h"
    6161
    62 #include "unicode_categories.h"
    6362
    6463cl::OptionCategory eIRDumpOptions("LLVM IR Dump Options", "These options control dumping of LLVM IR.");
     
    107106    delete mMod;
    108107}
     108   
     109void PabloCompiler::InstallExternalFunction(std::string C_fn_name, void * fn_ptr) {
     110    mExternalMap.insert(std::make_pair(C_fn_name, fn_ptr));
     111}
     112
    109113
    110114LLVM_Gen_RetVal PabloCompiler::compile(PabloBlock & pb)
     
    467471    for (auto mapping : mCalleeMap) {
    468472        const String * callee = mapping.first;
    469         void * callee_ptr = nullptr;
    470         #define CHECK_GENERAL_CODE_CATEGORY(SUFFIX) \
    471             if (callee->str() == #SUFFIX) { \
    472                 callee_ptr = (void*)&__get_gc_##SUFFIX; \
    473             } else
    474         CHECK_GENERAL_CODE_CATEGORY(Cc)
    475         CHECK_GENERAL_CODE_CATEGORY(Cf)
    476         CHECK_GENERAL_CODE_CATEGORY(Cn)
    477         CHECK_GENERAL_CODE_CATEGORY(Co)
    478         CHECK_GENERAL_CODE_CATEGORY(Cs)
    479         CHECK_GENERAL_CODE_CATEGORY(Ll)
    480         CHECK_GENERAL_CODE_CATEGORY(Lm)
    481         CHECK_GENERAL_CODE_CATEGORY(Lo)
    482         CHECK_GENERAL_CODE_CATEGORY(Lt)
    483         CHECK_GENERAL_CODE_CATEGORY(Lu)
    484         CHECK_GENERAL_CODE_CATEGORY(Mc)
    485         CHECK_GENERAL_CODE_CATEGORY(Me)
    486         CHECK_GENERAL_CODE_CATEGORY(Mn)
    487         CHECK_GENERAL_CODE_CATEGORY(Nd)
    488         CHECK_GENERAL_CODE_CATEGORY(Nl)
    489         CHECK_GENERAL_CODE_CATEGORY(No)
    490         CHECK_GENERAL_CODE_CATEGORY(Pc)
    491         CHECK_GENERAL_CODE_CATEGORY(Pd)
    492         CHECK_GENERAL_CODE_CATEGORY(Pe)
    493         CHECK_GENERAL_CODE_CATEGORY(Pf)
    494         CHECK_GENERAL_CODE_CATEGORY(Pi)
    495         CHECK_GENERAL_CODE_CATEGORY(Po)
    496         CHECK_GENERAL_CODE_CATEGORY(Ps)
    497         CHECK_GENERAL_CODE_CATEGORY(Sc)
    498         CHECK_GENERAL_CODE_CATEGORY(Sk)
    499         CHECK_GENERAL_CODE_CATEGORY(Sm)
    500         CHECK_GENERAL_CODE_CATEGORY(So)
    501         CHECK_GENERAL_CODE_CATEGORY(Zl)
    502         CHECK_GENERAL_CODE_CATEGORY(Zp)
    503         CHECK_GENERAL_CODE_CATEGORY(Zs)
    504         // OTHERWISE ...
    505         throw std::runtime_error("Unknown unicode category \"" + callee->str() + "\"");
    506         #undef CHECK_GENERAL_CODE_CATEGORY
    507         Value * unicodeCategory = mMod->getOrInsertFunction("__get_gc_" + callee->str(), mBitBlockType, mBasisBitsInputPtr, NULL);
    508         if (LLVM_UNLIKELY(unicodeCategory == nullptr)) {
    509             throw std::runtime_error("Could not create static method call for unicode category \"" + callee->str() + "\"");
    510         }
    511         mExecutionEngine->addGlobalMapping(cast<GlobalValue>(unicodeCategory), callee_ptr);
    512         mCalleeMap[callee] = unicodeCategory;
     473        //std::cerr << callee->str() << " to be declared\n";
     474        auto ei = mExternalMap.find(callee->str());
     475        if (ei != mExternalMap.end()) {
     476            void * fn_ptr = ei->second;
     477            //std::cerr << "Ptr found:" <<  std::hex << ((intptr_t) fn_ptr) << std::endl;
     478            Value * externalValue = mMod->getOrInsertFunction(callee->str(), mBitBlockType, mBasisBitsInputPtr, NULL);
     479            if (LLVM_UNLIKELY(externalValue == nullptr)) {
     480                throw std::runtime_error("Could not create static method call for external function \"" + callee->str() + "\"");
     481            }
     482            mExecutionEngine->addGlobalMapping(cast<GlobalValue>(externalValue), fn_ptr);
     483            mCalleeMap[callee] = externalValue;
     484        }
     485        else {
     486            throw std::runtime_error("External function \"" + callee->str() + "\" not installed");
     487        }
    513488    }
    514489}
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.h

    r4378 r4382  
    6363    PabloCompiler(const std::vector<Var *> & basisBitVars);
    6464    ~PabloCompiler();
     65    void InstallExternalFunction(std::string C_fn_name, void * fn_ptr);
    6566    LLVM_Gen_RetVal compile(PabloBlock & pb);
    6667private:
     
    131132    unsigned                            mMaxNestingDepth;
    132133
     134    std::map<std::string, void *>       mExternalMap;
    133135    StringToValueMap                    mCalleeMap;
    134136};
Note: See TracChangeset for help on using the changeset viewer.