<commit_before>/*
 * codegen.cpp
 *
 *  Created on: 2013-03-15
 *      Author: <NAME>
 *
 */
///Contains Methods which generate code C++ from  the VRIR
#include <codegen.hpp>
#include<vrloopinfo.hpp>
#include "vrPrelimBCE.hpp"
#include<sstream>
#define MEM_OPTMISE
#ifdef MEM_OPTMISE
bool memOptmise = true;
#else 
bool memOptmise = false;
#endif
#define PRELIM_BOUNDS
#ifdef PRELIM_BOUNDS
bool prelim_bounds = true;
#else 
bool prelim_bounds = true;
#endif

using namespace VRaptor;
using namespace std;

Context::Context() {
	arrayFlag = false;
}

bool Context::isArray() {
	return arrayFlag;
}

void Context::resetArrayFlag() {
	arrayFlag = false;
}

void Context::setArrayFlag() {
	arrayFlag = true;
}
vector<string> Context::getAllStmt() {
	return list;
}

void Context::addStmt(const string stmt) {
	list.push_back(stmt);
}

void Context::addStmtVec(vector<string> vec){

	list.insert(list.end(),vec.begin(),vec.end());
}

void VCompiler::genHeaderFile(VModule *vm) {
	headerCntxt.addStmt("#include\"math.h\" \n");
	headerCntxt.addStmt("#include\"matrix_ops.hpp\"\n");
	headerCntxt.addStmt("#include\"library_ops.hpp\"\n");
	headerCntxt.addStmt("#include\"vrbinding.hpp\"\n");
	vector<VFunction*> funcList = vm->getFns();
	for(int i = 0; i<funcList.size(); i++) {
		if(funcList[i]->getName().compare("pForFunc") == 0) {
			continue;
		}
		if(hasMultRet(funcList[i])) {
			headerCntxt.addStmt(genRetStruct(funcList[i]));
		}
		
	}
	for(int i = 0; i < funcList.size(); i++) {
			if(funcList[i]->getName().compare("pForFunc") == 0) {
					continue;
			}
			headerCntxt.addStmt(genFuncHeader(funcList[i])+ ";\n");
	}
}

void VCompiler::initLibCallSet() {
    libCallSet.insert(LibCallExpr::LIB_MATMULT);
    libCallSet.insert(LibCallExpr::LIB_PLUS);
    libCallSet.insert(LibCallExpr::LIB_MINUS);
    libCallSet.insert(LibCallExpr::LIB_MULT);
    libCallSet.insert(LibCallExpr::LIB_DIV);
    libCallSet.insert(LibCallExpr::LIB_TRANS);
    /* libCallSet.insert(LibCallExpr::LIB_MLDIV); */
    /* libCallSet.insert(LibCallExpr::LIB_MRDIV); */
}
void VCompiler::setOpenMpFlag(bool val){
	enableOpenMP = true;
}
Context VCompiler::moduleCodeGen(VModule *vm) {
	Context cntxt;
	mapper.init();
    currModule = vm;
	setOpenMpFlag(true);
	cntxt.addStmt("#include \""+ moduleName+"Impl.hpp\"\n ");
	vector<VFunction*> funcList = vm->getFns();
#ifdef DEBUG
	std::cout<<"generating header file"<<std::endl;
#endif
	genHeaderFile(vm);
#ifdef DEBUG
	std::cout<<"generating code in module "<<std::endl;
#endif 
	for (int i = 0; i < funcList.size(); i++) {
		std::cout<<"Generating code for "<<funcList[i]->getName()<<std::endl;
		if(funcList[i]->getName().compare("pForFunc") == 0) {
			continue;
		}
		currFunction = funcList[i];
        std::map<StmtPtr,LoopInfo*> infoMap;
        std::set<StmtPtr>topLoops; 
        LoopInfo::getLoopInfoMap(funcList[i], infoMap, topLoops);
        VRaptor::prelimBCE(funcList[i], infoMap);
		Context tempCntxt = funcCodeGen(funcList[i]);
		for (int j = 0; j < tempCntxt.getAllStmt().size(); j++) {
			cntxt.addStmt(tempCntxt.getAllStmt()[j]);
		}
	}
	return cntxt;
}

std::string VCompiler::genFuncStructName(VFunction * func) {
	return genFuncStructName(func->getName());
}

std::string VCompiler::genFuncStructName(std::string funcName) {
    return std::string("struct_") + funcName + "_ret";
}
std::string VCompiler::genFuncStructName(FunCallExprPtr expr) {
    return genFuncStructName(expr->getName()); 
}
std::string VCompiler::genStructData() {
	return "ret_data";
}

std::string VCompiler::genConstructor(VFunction *func,SymTable *symTable) {
	std::string structName = genFuncStructName(func);
	//generate function arguments
	std::string constructorStr = structName + "(";
	FuncTypePtr funcType = static_cast<FuncTypePtr>(func->getType());
	VTypePtr vtype = funcType->getReturnType(0);
	std::string dataType = vTypeCodeGen(vtype,symTable,false).getAllStmt()[0];
	constructorStr+= dataType + " " +genStructData() + "0";
	for(int i = 1; i < funcType->getNumReturns(); i++) {
		 vtype = funcType->getReturnType(i);<commit_msg>conditions on Prelim bounds check<commit_after>/*
 * codegen.cpp
 *
 *  Created on: 2013-03-15
 *      Author: <NAME>
 *
 */
///Contains Methods which generate code C++ from  the VRIR
#include <codegen.hpp>
#include<vrloopinfo.hpp>
#include "vrPrelimBCE.hpp"
#include<sstream>
#define MEM_OPTMISE
#ifdef MEM_OPTMISE
bool memOptmise = true;
#else 
bool memOptmise = false;
#endif
#define PRELIM_BOUNDS
#ifdef PRELIM_BOUNDS
bool prelim_bounds = true;
#else 
bool prelim_bounds = true;
#endif

using namespace VRaptor;
using namespace std;

Context::Context() {
	arrayFlag = false;
}

bool Context::isArray() {
	return arrayFlag;
}

void Context::resetArrayFlag() {
	arrayFlag = false;
}

void Context::setArrayFlag() {
	arrayFlag = true;
}
vector<string> Context::getAllStmt() {
	return list;
}

void Context::addStmt(const string stmt) {
	list.push_back(stmt);
}

void Context::addStmtVec(vector<string> vec){

	list.insert(list.end(),vec.begin(),vec.end());
}

void VCompiler::genHeaderFile(VModule *vm) {
	headerCntxt.addStmt("#include\"math.h\" \n");
	headerCntxt.addStmt("#include\"matrix_ops.hpp\"\n");
	headerCntxt.addStmt("#include\"library_ops.hpp\"\n");
	headerCntxt.addStmt("#include\"vrbinding.hpp\"\n");
	vector<VFunction*> funcList = vm->getFns();
	for(int i = 0; i<funcList.size(); i++) {
		if(funcList[i]->getName().compare("pForFunc") == 0) {
			continue;
		}
		if(hasMultRet(funcList[i])) {
			headerCntxt.addStmt(genRetStruct(funcList[i]));
		}
		
	}
	for(int i = 0; i < funcList.size(); i++) {
			if(funcList[i]->getName().compare("pForFunc") == 0) {
					continue;
			}
			headerCntxt.addStmt(genFuncHeader(funcList[i])+ ";\n");
	}
}

void VCompiler::initLibCallSet() {
    libCallSet.insert(LibCallExpr::LIB_MATMULT);
    libCallSet.insert(LibCallExpr::LIB_PLUS);
    libCallSet.insert(LibCallExpr::LIB_MINUS);
    libCallSet.insert(LibCallExpr::LIB_MULT);
    libCallSet.insert(LibCallExpr::LIB_DIV);
    libCallSet.insert(LibCallExpr::LIB_TRANS);
    /* libCallSet.insert(LibCallExpr::LIB_MLDIV); */
    /* libCallSet.insert(LibCallExpr::LIB_MRDIV); */
}
void VCompiler::setOpenMpFlag(bool val){
    enableOpenMP = true;
}
Context VCompiler::moduleCodeGen(VModule *vm) {
    Context cntxt;
    mapper.init();
    currModule = vm;
    setOpenMpFlag(true);
    cntxt.addStmt("#include \""+ moduleName+"Impl.hpp\"\n ");
    vector<VFunction*> funcList = vm->getFns();
#ifdef DEBUG
    std::cout<<"generating header file"<<std::endl;
#endif
    genHeaderFile(vm);
#ifdef DEBUG
    std::cout<<"generating code in module "<<std::endl;
#endif 
    for (int i = 0; i < funcList.size(); i++) {
        std::cout<<"Generating code for "<<funcList[i]->getName()<<std::endl;
        if(funcList[i]->getName().compare("pForFunc") == 0) {
            continue;
        }
        currFunction = funcList[i];
        std::map<StmtPtr,LoopInfo*> infoMap;
        std::set<StmtPtr>topLoops; 
        LoopInfo::getLoopInfoMap(funcList[i], infoMap, topLoops);
        if(prelim_bounds) {
            VRaptor::prelimBCE(funcList[i], infoMap);
        }
        Context tempCntxt = funcCodeGen(funcList[i]);
        for (int j = 0; j < tempCntxt.getAllStmt().size(); j++) {
            cntxt.addStmt(tempCntxt.getAllStmt()[j]);
        }
    }
    return cntxt;
}

std::string VCompiler::genFuncStructName(VFunction * func) {
	return genFuncStructName(func->getName());
}

std::string VCompiler::genFuncStructName(std::string funcName) {
    return std::string("struct_") + funcName + "_ret";
}
std::string VCompiler::genFuncStructName(FunCallExprPtr expr) {
    return genFuncStructName(expr->getName()); 
}
std::string VCompiler::genStructData() {
	return "ret_data";
}

std::string VCompiler::genConstructor(VFunction *func,SymTable *symTable) {
	std::string structName = genFuncStructName(func);
	//generate function arguments
	std::string constructorStr = structName + "(";
	FuncTypePtr funcType = static_cast<FuncTypePtr>(func->getType());
	VTypePtr vtype = funcType->getReturnType(0);
	std::string dataType = vTypeCodeGen(vtype,symTable,false).getAllStmt()[0];
	constructorStr+= dataType + " " +genStructData() + "0";
	for(int i = 1; i < funcType->getNumReturns(); i++) {
		 vtype = funcType->getReturnType(i);
{'fraction_non_alphanumeric': 0.16843885935234412, 'fraction_numerical': 0.00459159014016433, 'mean_word_length': 5.015261627906977, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1725430', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2974, 'n_words': 500}