<commit_before>#ifndef HAXOR_COMPILER_HH
#define HAXOR_COMPILER_HH

#include <string>
#include <vector>
#include <iostream>
#include <map>
#include "haxor/hc/node/all.hh"
#include "haxor/hc/lexer.hh"
#include "haxor/common/cmd.hh"
#include "haxor/common/hdr.hh"
#include "parser.hh"

namespace haxor {
  class instr_spec {
    public:
    instr_spec(enum cmd cmd, std::string args, bool rel, bool x8) {
      this->cmd = cmd;
      this->args = args;
      this->rel = rel;
      this->x8 = x8;
    }
    instr_spec(enum cmd cmd, std::string args) {
      this->cmd = cmd;
      this->args = args;
      this->rel = false;
      this->x8 = false;
    }

    enum cmd cmd;
    std::string args;
    bool rel;
    bool x8;
  };

  class compiler {
    friend class parser;

    public:
    compiler();
    void compile(const std::string &filename);

    private:
    void set_ast(std::vector<node_base*> *ast);
    std::string& get_filename();

    void verify_code();
    void unwind_pseudo();
    void mark_sections();
    void order_sections();
    void calc_addresses();
    void collect_labels();
    hdr_t build_hdr();
    int64_t calc_bss_size();
    uint8_t resolve_reg(const std::string &name);
    std::string format_location(const class location &location);

    class lexer lexer;
    class parser parser;

    std::string filename;
    std::vector<node_base*> *ast;
    std::map<std::string, word_t> labels;

    std::map<std::string, uint8_t> reg_aliases = {
      { "$zero", reg_zero }, { "$at",  reg_accumulator },
      { "$a0",   2 }, { "$a1",  3 }, { "$a2",  4 }, { "$a3",  5 }, { "$a4",  6 },
      { "$a5",   7 }, { "$a6",  8 }, { "$a7",  9 }, { "$a8", 10 }, { "$a9", 11 },
      { "$t0",  12 }, { "$t1", 13 }, { "$t2", 14 }, { "$t3", 15 }, { "$t4", 16 },
      { "$t5",  17 }, { "$t6", 18 }, { "$t7", 19 }, { "$t8", 20 }, { "$t9", 21 },
      { "$sp",  reg_stack }, { "$ra", reg_return }, { "$sc", reg_syscall }
    };

    std::map<std::string, instr_spec> instr_list = {
      { "nop",     instr_spec(cmd_nop,     "") },
      { "exiti",   instr_spec(cmd_exiti,   "i") },
      { "syscall", instr_spec(cmd_syscall, "") },

      { "add",     instr_spec(cmd_add,     "rrr") },
      { "addi",    instr_spec(cmd_addi,    "rri") },
      { "sub",     instr_spec(cmd_sub,     "rrr") },
      { "mult",    instr_spec(cmd_mult,    "rrr") },
      { "div",     instr_spec(cmd_div,     "rrr") },
      { "mod",     instr_spec(cmd_mod,     "rrr") },<commit_msg>break it into multiple lines for readibility<commit_after>#ifndef HAXOR_COMPILER_HH
#define HAXOR_COMPILER_HH

#include <string>
#include <vector>
#include <iostream>
#include <map>
#include "haxor/hc/node/all.hh"
#include "haxor/hc/lexer.hh"
#include "haxor/common/cmd.hh"
#include "haxor/common/hdr.hh"
#include "parser.hh"

namespace haxor {
  class instr_spec {
    public:
    instr_spec(enum cmd cmd, std::string args, bool rel, bool x8) {
      this->cmd = cmd;
      this->args = args;
      this->rel = rel;
      this->x8 = x8;
    }
    instr_spec(enum cmd cmd, std::string args) {
      this->cmd = cmd;
      this->args = args;
      this->rel = false;
      this->x8 = false;
    }

    enum cmd cmd;
    std::string args;
    bool rel;
    bool x8;
  };

  class compiler {
    friend class parser;

    public:
    compiler();
    void compile(const std::string &filename);

    private:
    void set_ast(std::vector<node_base*> *ast);
    std::string& get_filename();

    void verify_code();
    void unwind_pseudo();
    void mark_sections();
    void order_sections();
    void calc_addresses();
    void collect_labels();
    hdr_t build_hdr();
    int64_t calc_bss_size();
    uint8_t resolve_reg(const std::string &name);
    std::string format_location(const class location &location);

    class lexer lexer;
    class parser parser;

    std::string filename;
    std::vector<node_base*> *ast;
    std::map<std::string, word_t> labels;

    std::map<std::string, uint8_t> reg_aliases = {
      { "$zero", reg_zero },
      { "$at",   reg_accumulator },
      { "$a0",    2 },
      { "$a1",    3 },
      { "$a2",    4 },
      { "$a3",    5 },
      { "$a4",    6 },
      { "$a5",    7 },
      { "$a6",    8 },
      { "$a7" ,   9 },
      { "$a8",   10 },
      { "$a9",   11 },
      { "$t0",   12 },
      { "$t1",   13 },
      { "$t2",   14 },
      { "$t3",   15 },
      { "$t4",   16 },
      { "$t5",   17 },
      { "$t6",   18 },
      { "$t7",   19 },
      { "$t8",   20 },
      { "$t9",   21 },
      { "$sp",   reg_stack },
      { "$ra",   reg_return },
      { "$sc",   reg_syscall }
    };

    std::map<std::string, instr_spec> instr_list = {
      { "nop",     instr_spec(cmd_nop,     "") },
      { "exiti",   instr_spec(cmd_exiti,   "i") },
      { "syscall", instr_spec(cmd_syscall, "") },

      { "add",     instr_spec(cmd_add,     "rrr") },
      { "addi",    instr_spec(cmd_addi,    "rri") },
      { "sub",     instr_spec(cmd_sub,     "rrr") },
      { "mult",    instr_spec(cmd_mult,    "rrr") },
      { "div",     instr_spec(cmd_div,     "rrr") },
      { "mod",     instr_spec(cmd_mod,     "rrr") },
{'fraction_non_alphanumeric': 0.18773125608568647, 'fraction_numerical': 0.023758519961051605, 'mean_word_length': 2.39009900990099, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 37, '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': '1427656', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1908, 'n_words': 410}