<commit_before>#include <assert.h>
#include "CParCodeGen.h"

using namespace common;
using namespace std;
using namespace codegen;

CParCodeGen::CParCodeGen(parser::Driver &Drv)
    : CCodeGen(Drv) {}


void CParCodeGen::visit(Program &Node) {
    Prog = &Node;
    Function *Main = nullptr;

    *Header << "#include \"src/runtime/task.h\" " << endl;

    // Generate the standard functionality for every program
    generateStd();

    // Find the main function
    for (auto &Decl : Node.Decls) {
        if (typeid(*Decl.get()) == typeid(Function) &&
            ((Function *)Decl.get())->Id == "main") {
            Main = (Function *)Decl.get();
            break;
        }
    }

    if (!Main)
        throw "No main, help!";

    // Get the type of main, so that return type of main is generated
    string MainSig = getType(Main->Signature);

    // Generate real main function, that calls the users main
    string StrListName = getList(StringList);

    *Output << "int main(int argc, char** argv) { " << endl
            << "    " << StrListName << " *args = " << GGenerated << GCreate << StrListName << "(0); " << endl
            << "    int i; " << endl
            << " " << endl
            << "    for(i = argc - 1; i >= 0; i--) { " << endl
            << "        args = " << GGenerated << GAdd << StrListName << "(args, " << GGenerated
                                            << GCreate << GString << "(argv[i])); " << endl
            << "    } " << endl
            << " " << endl
            << "    " << MainSig << GArg << "* main_args = malloc(sizeof(" << MainSig << GArg << ")); " << endl
            << "    main_args->" << GGenerated << GArg << "0 = args; " << endl
            << "    task_t *main_task = taskcreate((void (*)(void*))" << GUser << GMain << "."
                                    << GParallel << ", (void *)main_args); " << endl
            << "    rmain(4, main_task); " << endl
            << endl
            << "    " << Prints[Main->Signature.Subtypes.back()] << "(main_args->" << GGenerated << GRes << "); " << endl
            << "    printf(\"\\n\");" << endl
            << "    taskdealloc(main_task); " << endl
            << "    return 0; " << endl
            << "} " << endl
            << " " << endl;


    for (auto &Func : Node.Decls) {
        Func->accept(*this);
        *Output << endl;
        outputBuffer();
    }
}

void CParCodeGen::visit(Function &Node) {
    string ParFunc;
    string SeqFunc;
    string ArgType;
    string Signature = getEnvironment(Node.Signature);

    CurrentArg = Signature + GArg;

    CurFunc = &Node;

    ParFunc = "void " + GUser + GParallel + Node.Id + "(task_t *t)";
    SeqFunc = getType(Node.Signature.Subtypes.back()) + " " + GUser + GSequential + Node.Id + "(";

    for (size_t i = 0; i < Node.Signature.Subtypes.size() - 1; ++i) {
        SeqFunc += getType(Node.Signature.Subtypes[i]) + " " + GGenerated + GArg + to_string(i);

        if (i < Node.Signature.Subtypes.size() - 2)
            SeqFunc += ", ";
    }

    SeqFunc += ")";

    *Header << ParFunc << ";" << endl;
    *Header << SeqFunc << ";" << endl;
    *Header << Signature << " " << GUser << Node.Id  << " = { " << GUser << GParallel << Node.Id << ", "
                                                                << GUser << GSequential << Node.Id << " };" << endl;

    GenerateParallel = true;
    *Output << ParFunc << " { " << endl;

    for (size_t i = 0; i < Node.Signature.Subtypes.size() - 1; ++i) {<commit_msg>cparcodegen: changed a few names, to avoid name collition in the code generated<commit_after>#include <assert.h>
#include "CParCodeGen.h"

using namespace common;
using namespace std;
using namespace codegen;

CParCodeGen::CParCodeGen(parser::Driver &Drv)
    : CCodeGen(Drv) {}


void CParCodeGen::visit(Program &Node) {
    Prog = &Node;
    Function *Main = nullptr;

    *Header << "#include \"src/runtime/task.h\" " << endl;

    // Generate the standard functionality for every program
    generateStd();

    // Find the main function
    for (auto &Decl : Node.Decls) {
        if (typeid(*Decl.get()) == typeid(Function) &&
            ((Function *)Decl.get())->Id == "main") {
            Main = (Function *)Decl.get();
            break;
        }
    }

    if (!Main)
        throw "No main, help!";

    // Get the type of main, so that return type of main is generated
    string MainSig = getType(Main->Signature);

    // Generate real main function, that calls the users main
    string StrListName = getList(StringList);

    *Output << "int main(int argc, char** argv) { " << endl
            << "    " << StrListName << " *args = " << GGenerated << GCreate << StrListName << "(0); " << endl
            << "    int i; " << endl
            << " " << endl
            << "    for(i = argc - 1; i >= 0; i--) { " << endl
            << "        args = " << GGenerated << GAdd << StrListName << "(args, " << GGenerated
                                            << GCreate << GString << "(argv[i])); " << endl
            << "    } " << endl
            << " " << endl
            << "    " << MainSig << GArg << "* main_args = malloc(sizeof(" << MainSig << GArg << ")); " << endl
            << "    main_args->" << GGenerated << GArg << "0 = args; " << endl
            << "    task_t *main_task = taskcreate((void (*)(void*))" << GUser << GMain << "."
                                    << GParallel << ", (void *)main_args); " << endl
            << "    rmain(4, main_task); " << endl
            << endl
            << "    " << Prints[Main->Signature.Subtypes.back()] << "(main_args->" << GGenerated << GRes << "); " << endl
            << "    printf(\"\\n\");" << endl
            << "    taskdealloc(main_task); " << endl
            << "    return 0; " << endl
            << "} " << endl
            << " " << endl;


    for (auto &Func : Node.Decls) {
        Func->accept(*this);
        *Output << endl;
        outputBuffer();
    }
}

void CParCodeGen::visit(Function &Node) {
    string ParFunc;
    string SeqFunc;
    string ArgType;
    string Signature = getEnvironment(Node.Signature);

    CurrentArg = Signature + GArg;

    CurFunc = &Node;

    ParFunc = "void " + GGenerated + GParallel + Node.Id + "(task_t *t)";
    SeqFunc = getType(Node.Signature.Subtypes.back()) + " " + GGenerated + GSequential + Node.Id + "(";

    for (size_t i = 0; i < Node.Signature.Subtypes.size() - 1; ++i) {
        SeqFunc += getType(Node.Signature.Subtypes[i]) + " " + GGenerated + GArg + to_string(i);

        if (i < Node.Signature.Subtypes.size() - 2)
            SeqFunc += ", ";
    }

    SeqFunc += ")";

    *Header << ParFunc << ";" << endl;
    *Header << SeqFunc << ";" << endl;
    *Header << Signature << " " << GUser << Node.Id  << " = { " << GGenerated << GParallel << Node.Id << ", "
                                                                << GGenerated << GSequential << Node.Id << " };" << endl;

    GenerateParallel = true;
    *Output << ParFunc << " { " << endl;

    for (size_t i = 0; i < Node.Signature.Subtypes.size() - 1; ++i) {
{'fraction_non_alphanumeric': 0.18299549549549549, 'fraction_numerical': 0.003096846846846847, 'mean_word_length': 1.8917378917378918, 'pattern_counts': {'":': 0, '<': 395, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2209525', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2142, 'n_words': 516}