<commit_before>#include <regex>
#include <map>
#include <cstdint>
#include <cstdio>
#include <bitset>
#include <string>

#include "Assembler.hpp"
#include "Assembly.hpp"

using namespace std;

const int Assembler::RS = 0;
const int Assembler::RT = 1;
const int Assembler::RD = 2;

Assembler::Assembler() {

}

Assembler::Assembler(const vector<string> &instOriginal) {
    this->instOriginal = instOriginal;

    const static regex rTypeExp ("(addu|add|subu|sub|and|or|xor|nor|sltu|slt|sllv|srlv|srav|jr)\\b");
    const static regex shiftTypeExp ("(sll|srl|sra)\\b");
    const static regex iTypeExp ("(addiu|addi|andi|ori|xori|lui|lw|sw|beq|bne|sltiu|slti)\\b");
    const static regex jTypeExp ("(jal|j)\\b");

    const static regex operandExp ("\\$(\\w+),\\s*\\$(\\w+),\\s*\\$(\\w+)");
    const static regex operandWithImmExp ("\\$(\\w+),\\s*\\$(\\w+),\\s*(\\d+)");
    const static map<string, int> funcMap = {
        {"addu",    0b100001},
        {"add",     0b100000},
        {"sub",     0b100010},
        {"subu",    0b100011},
        {"and",     0b100100},
        {"or",      0b100101},
        {"xor",     0b100110},
        {"nor",     0b100111},
        {"slt",     0b101010},
        {"sltu",    0b101011},
        {"sll",     0b000000},
        {"srl",     0b000010},
        {"sra",     0b000011},
        {"sllv",    0b000100},
        {"srlv",    0b000110},
        {"srav",    0b000111},
        {"jr",      0b001000}
    };

    for (auto inst: this->instOriginal) {
        uint32_t opcode, rs, rt, rd, shamt, func;
        smatch match;

        if (regex_search(inst, match, rTypeExp)) {
            opcode = 0b000000;
            string funcName = match[0];

            string rsName, rtName, rdName;
            smatch operands;
            if (regex_search(inst, operands, operandExp)) {
                rsName = operands[2];
                rtName = operands[3];
                rdName = operands[1];
                rs = getOperand(rsName);
                rt = getOperand(rtName);
                rd = getOperand(rdName);
                shamt = 0;
                func = funcMap.at(funcName);
            } else {
                cout << "Syntax error with statement:" << endl;
                cout << inst << endl;
            }
        } else if (regex_search(inst, match, shiftTypeExp)) {
            opcode = 0b000000;
            string funcName = match[0];

            string rtName, rdName, immName;
            smatch operandWithImm;
            if (regex_search(inst, operandWithImm, operandWithImmExp)) {
                rdName = operandWithImm[2];
                rtName = operandWithImm[1];
                immName = operandWithImm[3];
                rs = 0b00000;
                rd = getOperand(rdName);
                rt = getOperand(rtName);
                shamt = stoul(immName);
                func = funcMap.at(funcName);
            } else {
                cout << "Syntax error with statement:" << endl;
                cout << inst << endl;
            }
        }

        uint32_t instruction = opcode;
        instruction = (instruction << 5) | rs;
        instruction = (instruction << 5) | rt;
        instruction = (instruction << 5) | rd;
        instruction = (instruction << 5) | shamt;
        instruction = (instruction << 6) | func;

        instAssembled.push_back(Assembly(instruction));
    }
}

vector<Assembly> Assembler::getInstAssembled() {
    return instAssembled;
}

uint32_t Assembler::getOperand(string &operandName) {
    const static string regNames[] = {
        "zero", "at",
        "v0", "v1", "a0", "a1", "a2", "a3",
        "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
        "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
        "t8", "t9",
        "k0", "k1",<commit_msg>Add Jr asm && reconstruction<commit_after>#include <regex>
#include <map>
#include <cstdint>
#include <cstdio>
#include <bitset>
#include <string>

#include "Assembler.hpp"
#include "Assembly.hpp"

using namespace std;

const int Assembler::RS = 0;
const int Assembler::RT = 1;
const int Assembler::RD = 2;

Assembler::Assembler() {

}

Assembler::Assembler(const vector<string> &instOriginal) {
    this->instOriginal = instOriginal;

    const static regex rTypeExp ("(addu|add|subu|sub|and|or|xor|nor|sltu|slt|sllv|srlv|srav|jr|sll|srl|sra)\\b");
    const static regex iTypeExp ("(addiu|addi|andi|ori|xori|lui|lw|sw|beq|bne|sltiu|slti)\\b");
    const static regex jTypeExp ("(jal|j)\\b");

    const static regex operandExp ("\\$(\\w+)");
    const static regex operandsExp ("\\$(\\w+),\\s*\\$(\\w+),\\s*\\$(\\w+)");
    const static regex operandsWithImmExp ("\\$(\\w+),\\s*\\$(\\w+),\\s*(\\d+)");
    const static map<string, int> funcMap = {
        {"addu",    0b100001},
        {"add",     0b100000},
        {"sub",     0b100010},
        {"subu",    0b100011},
        {"and",     0b100100},
        {"or",      0b100101},
        {"xor",     0b100110},
        {"nor",     0b100111},
        {"slt",     0b101010},
        {"sltu",    0b101011},
        {"sll",     0b000000},
        {"srl",     0b000010},
        {"sra",     0b000011},
        {"sllv",    0b000100},
        {"srlv",    0b000110},
        {"srav",    0b000111},
        {"jr",      0b001000}
    };

    for (auto inst: this->instOriginal) {
        uint32_t opcode, rs, rt, rd, shamt, func;
        smatch match;

        if (regex_search(inst, match, rTypeExp)) {
            // R-type

            opcode = 0b000000;
            string funcName = match[0];

            string rsName, rtName, rdName, immName;
            smatch operands, operandsWithImm, operand;

            if (regex_search(inst, operands, operandsExp)) {
                // Regular R-type
                // i.e. Assembly with rs, rt, rd

                rsName = operands[2];
                rtName = operands[3];
                rdName = operands[1];
                rs = getOperand(rsName);
                rt = getOperand(rtName);
                rd = getOperand(rdName);
                shamt = 0b00000;
                func = funcMap.at(funcName);
            } else if (regex_search(inst, operandsWithImm, operandsWithImmExp)) {
                // Shift R-type
                // i.e. Assembly with rt, rd, shamt

                rdName = operandsWithImm[2];
                rtName = operandsWithImm[1];
                immName = operandsWithImm[3];
                rs = 0b00000;
                rd = getOperand(rdName);
                rt = getOperand(rtName);
                shamt = stoul(immName);
                func = funcMap.at(funcName);
            } else if (regex_search(inst, operand, operandExp)) {
                // JR

                rsName = operand[1];
                rs = getOperand(rsName);
                rd = 0b00000;
                rt = 0b00000;
                shamt = 0b00000;
                func = funcMap.at(funcName);
            } else {
                // Syntax Error

                cout << "Syntax error with statement:" << endl;
                cout << inst << endl;
            }
        }

        uint32_t instruction = opcode;
        instruction = (instruction << 5) | rs;
        instruction = (instruction << 5) | rt;
        instruction = (instruction << 5) | rd;
        instruction = (instruction << 5) | shamt;
        instruction = (instruction << 6) | func;

        instAssembled.push_back(Assembly(instruction));
    }
}

vector<Assembly> Assembler::getInstAssembled() {
    return instAssembled;
}

uint32_t Assembler::getOperand(string &operandName) {
    const static string regNames[] = {
        "zero", "at",
        "v0", "v1", "a0", "a1", "a2", "a3",
        "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
        "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
        "t8", "t9",
        "k0", "k1",
{'fraction_non_alphanumeric': 0.15917531053912154, 'fraction_numerical': 0.05019848892303752, 'mean_word_length': 1.8503649635036497, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 25, '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': '5855232', 'n_tokens_mistral': 2909, 'n_tokens_neox': 2600, 'n_words': 563}