diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-08 12:16:57 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-08 12:18:14 +0100 |
| commit | 7cb3e2de0a51c9ce1a0f1918fc8f4af62f2d348d (patch) | |
| tree | 7e392a11ec422db3557bc46580c639a745faa159 /CompilerDriver | |
| parent | ffe20a577c74652f4ca37545ba2f1cbaa56025d0 (diff) | |
\ccplus: name mangling, assignement, return, done.
We need control flow, loops, classes ane namespaces.
\masm: fix assembler export and import keyword.
Add a space to each of them so that the search will only match 'export
.text foo_bar'.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'CompilerDriver')
| -rw-r--r-- | CompilerDriver/ccplus.cc | 21 | ||||
| -rw-r--r-- | CompilerDriver/masm.cc | 28 |
2 files changed, 29 insertions, 20 deletions
diff --git a/CompilerDriver/ccplus.cc b/CompilerDriver/ccplus.cc index d022671..898182e 100644 --- a/CompilerDriver/ccplus.cc +++ b/CompilerDriver/ccplus.cc @@ -308,10 +308,10 @@ public: struct scope_type { std::vector<std::string> vals; - int reg_cnt{ 0 }; - int id{ 0 }; + std::size_t reg_cnt{ kRegisterCounter }; + std::size_t id{ 0 }; - bool operator==(const scope_type& typ) { return typ.id == id; } + bool operator==(const scope_type& typ) const { return typ.id == id; } }; std::vector<scope_type> scope; @@ -437,6 +437,13 @@ public: if (val.find(";") != std::string::npos) val.erase(val.find(";"), 1); + while (val.find(" ") != std::string::npos) + val.erase(val.find(" "), 1); + + if (isalnum(val[0]) && + !isdigit(val[0])) + val.insert(0, "import "); + leaf.fUserValue.replace(leaf.fUserValue.find("%s1"), strlen("%s1"), val); } } @@ -490,7 +497,7 @@ public: if (leaf.fUserData == "return") { - leaf.fUserValue = "ldw r19, %s\njlr"; + leaf.fUserValue = "mv r19, %s\njlr"; if (!lines.empty()) { @@ -503,7 +510,7 @@ public: val.erase(val.find(";"), 1); std::string val_reg; - std::size_t& reg_cnt = kRegisterCounter; + std::size_t reg_cnt = kRegisterCounter; for (int i = ln.find(leaf.fUserData) + leaf.fUserData.size(); i < ln.size(); ++i) { @@ -520,7 +527,6 @@ public: ln[i] == '|' || ln[i] == ';') { - std::cout << val_reg; val.replace(val.find(val_reg), val_reg.size(), "r" + std::to_string(reg_cnt)); val_reg.clear(); ++reg_cnt; @@ -537,6 +543,9 @@ public: val_reg += ln[i]; } + while (val.find(" ") != std::string::npos) + val.erase(val.find(" "), 1); + leaf.fUserValue.replace(leaf.fUserValue.find("%s"), strlen("%s"), val); } } diff --git a/CompilerDriver/masm.cc b/CompilerDriver/masm.cc index a558373..053238c 100644 --- a/CompilerDriver/masm.cc +++ b/CompilerDriver/masm.cc @@ -332,9 +332,9 @@ static bool masm_read_attributes(std::string& line) { // import is the opposite of export, it signals to the ld // that we need this symbol. - if (ParserKit::find_word(line, "import")) + if (ParserKit::find_word(line, "import ")) { - auto name = line.substr(line.find("import") + strlen("import")); + auto name = line.substr(line.find("import ") + strlen("import ")); std::string result = std::to_string(name.size()); result += kUndefinedSymbol; @@ -393,9 +393,9 @@ static bool masm_read_attributes(std::string& line) // export is a special keyword used by masm to tell the AE output stage to mark this section as a header. // it currently supports .text, .data., page_zero - if (ParserKit::find_word(line, "export")) + if (ParserKit::find_word(line, "export ")) { - auto name = line.substr(line.find("export") + strlen("export")); + auto name = line.substr(line.find("export ") + strlen("export ")); for (char& j : name) { @@ -465,8 +465,8 @@ namespace detail::algorithm bool is_valid(const std::string &str) { - if (ParserKit::find_word(str, "export") || - ParserKit::find_word(str, "import")) + if (ParserKit::find_word(str, "export ") || + ParserKit::find_word(str, "import ")) return true; return find_if(str.begin(), str.end(), is_not_alnum_space) == str.end(); @@ -489,8 +489,8 @@ static std::string masm_check_line(std::string& line, const std::string& file) line.erase(line.find('\t'), 1); if (line.empty() || - ParserKit::find_word(line, "import") || - ParserKit::find_word(line, "export") || + ParserKit::find_word(line, "import ") || + ParserKit::find_word(line, "export ") || ParserKit::find_word(line, "#") || ParserKit::find_word(line, ";") || ParserKit::find_word(line, "layout")) @@ -757,7 +757,7 @@ static bool masm_write_number(const std::size_t& pos, std::string& jump_label) static void masm_read_instruction(std::string& line, const std::string& file) { - if (ParserKit::find_word(line, "export")) + if (ParserKit::find_word(line, "export ")) return; for (auto& opcode64x0 : kOpcodes64x0) @@ -926,7 +926,7 @@ static void masm_read_instruction(std::string& line, const std::string& file) else { if (name == "sta" && - cpy_jump_label.find("import") != std::string::npos) + cpy_jump_label.find("import ") != std::string::npos) { detail::print_error("invalid usage import on 'sta', here: " + line, file); throw std::runtime_error("invalid_sta_usage"); @@ -941,15 +941,15 @@ masm_write_label: if (cpy_jump_label.find('\n') != std::string::npos) cpy_jump_label.erase(cpy_jump_label.find('\n'), 1); - if (cpy_jump_label.find("import") == std::string::npos && + if (cpy_jump_label.find("import ") == std::string::npos && name == "psh" || - cpy_jump_label.find("import") == std::string::npos && + cpy_jump_label.find("import ") == std::string::npos && name == "jb") { detail::print_error("import not found on jump label, please add one.", file); throw std::runtime_error("import_jmp_lbl"); } - else if (cpy_jump_label.find("import") != std::string::npos) + else if (cpy_jump_label.find("import ") != std::string::npos) { if (name == "sta") { @@ -957,7 +957,7 @@ masm_write_label: throw std::runtime_error("import_sta_op"); } - cpy_jump_label.erase(cpy_jump_label.find("import"), strlen("import")); + cpy_jump_label.erase(cpy_jump_label.find("import "), strlen("import ")); } while (cpy_jump_label.find(' ') != std::string::npos) |
