/*
 * passmanager-full.cpp
 *
 * Author: Volodymyr Melnychenko <v.melnychenko@xreate.org>
 * Created on January 27, 2017, 7:10 PM
 */

#include "passmanager.h"
#include "llvmlayer.h"
#include "pass/compilepass.h"
#include "pass/adhocpass.h"
#include "pass/cfapass.h"
#include "pass/dfapass.h"
#include "pass/interpretationpass.h"
#include "pass/versionspass.h"

using namespace xreate;

void PassManager::runWithoutCompilation(){
    if (flagIsProcessed) return;

    CFAPass* passCFG = new CFAPass(this);

        //TODO is it really DFGPass needs CFGpass?
    this->registerPass(new DFAPass(this), PassId::DFGPass, passCFG);
    this->registerPass(passCFG, PassId::CFGPass);
    this->registerPass(new AdhocPass(this), PassId::AdhocPass);
    this->registerPass(new InterpretationPass(this), PassId::InterpretationPass);
    this->registerPass(new VersionsPass(this), PassId::VersionsPass);

    this->executePasses();

    CompilePass::prepareQueries(clasp);
    clasp->run();
    flagIsProcessed = true;
}

void*
PassManager::run()
{
    runWithoutCompilation();

    CompilePass* compiler = new CompilePass(this);
    compiler->run();

    llvm->print();
    llvm->initJit();
    return llvm->getFunctionPointer(compiler->getEntryFunction());
}