summaryrefslogtreecommitdiffhomepage
path: root/CompilerDriver
diff options
context:
space:
mode:
Diffstat (limited to 'CompilerDriver')
-rw-r--r--CompilerDriver/.gitignore1
-rw-r--r--CompilerDriver/cc.cc22
-rw-r--r--CompilerDriver/ccplus.cc136
-rw-r--r--CompilerDriver/cpp.cc38
-rw-r--r--CompilerDriver/ld.cc50
-rw-r--r--CompilerDriver/makefile10
-rw-r--r--CompilerDriver/masm.cc121
7 files changed, 200 insertions, 178 deletions
diff --git a/CompilerDriver/.gitignore b/CompilerDriver/.gitignore
index 23a095f..021e796 100644
--- a/CompilerDriver/.gitignore
+++ b/CompilerDriver/.gitignore
@@ -15,5 +15,6 @@ bin/SourceUnitTest/*.cxx.pp
bin/SourceUnitTest/*.cxx
bin/SourceUnitTest/*.masm
bin/SourceUnitTest/*.h
+bin/SourceUnitTest/*.64x
*.cxx.pp \ No newline at end of file
diff --git a/CompilerDriver/cc.cc b/CompilerDriver/cc.cc
index 4135be0..d3f48d8 100644
--- a/CompilerDriver/cc.cc
+++ b/CompilerDriver/cc.cc
@@ -13,8 +13,8 @@
#include <fstream>
#include <iostream>
#include <uuid/uuid.h>
-#include <C++Kit/AsmKit/Arch/64k.hpp>
-#include <C++Kit/ParserKit.hpp>
+#include <CompilerKit/AsmKit/Arch/64k.hpp>
+#include <CompilerKit/ParserKit.hpp>
#define kOk 0
@@ -134,7 +134,7 @@ static std::string kRegisterPrefix = kAsmRegisterPrefix;
/////////////////////////////////////////
static std::vector<std::string> kFileList;
-static CxxKit::AssemblyFactory kFactory;
+static CompilerKit::AssemblyFactory kFactory;
static bool kInStruct = false;
static bool kOnWhileLoop = false;
static bool kOnForLoop = false;
@@ -1619,7 +1619,7 @@ next:
/////////////////////////////////////////////////////////////////////////////////////////
-class AssemblyMountpointClang final : public CxxKit::AssemblyMountpoint
+class AssemblyMountpointClang final : public CompilerKit::AssemblyMountpoint
{
public:
explicit AssemblyMountpointClang() = default;
@@ -1627,9 +1627,9 @@ public:
CXXKIT_COPY_DEFAULT(AssemblyMountpointClang);
- [[maybe_unused]] static Int32 Arch() noexcept { return CxxKit::AssemblyFactory::kArchRISCV; }
+ [[maybe_unused]] static Int32 Arch() noexcept { return CompilerKit::AssemblyFactory::kArchRISCV; }
- Int32 CompileToFormat(CxxKit::StringView& src, Int32 arch) override
+ Int32 CompileToFormat(CompilerKit::StringView& src, Int32 arch) override
{
if (arch != AssemblyMountpointClang::Arch())
return -1;
@@ -1657,7 +1657,7 @@ public:
kState.fOutputAssembly = std::make_unique<std::ofstream>(dest);
- auto fmt = CxxKit::current_date();
+ auto fmt = CompilerKit::current_date();
(*kState.fOutputAssembly) << "# Path: " << src_file << "\n";
(*kState.fOutputAssembly) << "# Language: MP-UX Assembly\n";
@@ -1688,6 +1688,10 @@ public:
std::vector<std::string> keywords = { "ldw", "stw", "lda", "sta", "add", "dec", "mv"};
+ ///
+ /// Replace, optimize, fix assembly output.
+ ///
+
for (auto& leaf : kState.fSyntaxTree->fLeafList)
{
for (auto& keyword : keywords)
@@ -1835,7 +1839,7 @@ int main(int argc, char** argv)
delete kFactory.Unmount();
kFactory.Mount(new AssemblyMountpointClang());
- kMachine = CxxKit::AssemblyFactory::kArchRISCV;
+ kMachine = CompilerKit::AssemblyFactory::kArchRISCV;
continue;
}
@@ -1875,7 +1879,7 @@ int main(int argc, char** argv)
kFileList.emplace_back(argv[index]);
- CxxKit::StringView srcFile = CxxKit::StringBuilder::Construct(argv[index]);
+ CompilerKit::StringView srcFile = CompilerKit::StringBuilder::Construct(argv[index]);
if (strstr(argv[index], kExt) == nullptr)
{
diff --git a/CompilerDriver/ccplus.cc b/CompilerDriver/ccplus.cc
index c87b8ee..fc8ab3a 100644
--- a/CompilerDriver/ccplus.cc
+++ b/CompilerDriver/ccplus.cc
@@ -15,8 +15,8 @@
#include <stack>
#include <utility>
#include <uuid/uuid.h>
-#include <C++Kit/AsmKit/Arch/64k.hpp>
-#include <C++Kit/ParserKit.hpp>
+#include <CompilerKit/AsmKit/Arch/64k.hpp>
+#include <CompilerKit/ParserKit.hpp>
#define kOk 0
@@ -131,7 +131,7 @@ static std::vector<std::string> kKeywords;
/////////////////////////////////////////
static std::vector<std::string> kFileList;
-static CxxKit::AssemblyFactory kFactory;
+static CompilerKit::AssemblyFactory kFactory;
static bool kInStruct = false;
static bool kOnWhileLoop = false;
static bool kOnForLoop = false;
@@ -220,7 +220,7 @@ bool CompilerBackendClang::Compile(const std::string& text, const char* file)
auto syntax_tree = ParserKit::SyntaxLeafList::SyntaxLeaf();
syntax_tree.fUserData = text;
- kState.fSyntaxTree->fLeafList.push_back(syntax_tree);
+ kState.fSyntaxTree->fLeafList.emplace_back(syntax_tree);
std::string text_cpy = text;
@@ -230,7 +230,7 @@ bool CompilerBackendClang::Compile(const std::string& text, const char* file)
{
while (text_cpy.find(keyword) != std::string::npos)
{
- keywords_list.push_back(std::make_pair(keyword, index));
+ keywords_list.emplace_back(std::make_pair(keyword, index));
++index;
text_cpy.erase(text_cpy.find(keyword), keyword.size());
@@ -242,7 +242,7 @@ bool CompilerBackendClang::Compile(const std::string& text, const char* file)
for (auto& keyword : keywords_list)
{
syntax_tree.fUserData = keyword.first;
- kState.fSyntaxTree->fLeafList.push_back(syntax_tree);
+ kState.fSyntaxTree->fLeafList.emplace_back(syntax_tree);
std::cout << keyword.first << "\n";
}
@@ -258,7 +258,7 @@ bool CompilerBackendClang::Compile(const std::string& text, const char* file)
/////////////////////////////////////////////////////////////////////////////////////////
-class AssemblyMountpointClang final : public CxxKit::AssemblyMountpoint
+class AssemblyMountpointClang final : public CompilerKit::AssemblyMountpoint
{
public:
explicit AssemblyMountpointClang() = default;
@@ -266,9 +266,9 @@ public:
CXXKIT_COPY_DEFAULT(AssemblyMountpointClang);
- [[maybe_unused]] static Int32 Arch() noexcept { return CxxKit::AssemblyFactory::kArchRISCV; }
+ [[maybe_unused]] static Int32 Arch() noexcept { return CompilerKit::AssemblyFactory::kArchRISCV; }
- Int32 CompileToFormat(CxxKit::StringView& src, Int32 arch) override
+ Int32 CompileToFormat(CompilerKit::StringView& src, Int32 arch) override
{
if (arch != AssemblyMountpointClang::Arch())
return -1;
@@ -296,7 +296,7 @@ public:
kState.fOutputAssembly = std::make_unique<std::ofstream>(dest);
- auto fmt = CxxKit::current_date();
+ auto fmt = CompilerKit::current_date();
(*kState.fOutputAssembly) << "# Path: " << src_file << "\n";
(*kState.fOutputAssembly) << "# Language: MP-UX Assembly\n";
@@ -304,7 +304,7 @@ public:
ParserKit::SyntaxLeafList syntax;
- kState.fSyntaxTreeList.push_back(syntax);
+ kState.fSyntaxTreeList.emplace_back(syntax);
kState.fSyntaxTree = &kState.fSyntaxTreeList[kState.fSyntaxTreeList.size() - 1];
std::string source;
@@ -338,7 +338,7 @@ public:
{
if (leaf.fUserData == "{")
{
- scope.push_back({});
+ scope.emplace_back();
}
if (leaf.fUserData == "{")
@@ -447,7 +447,7 @@ public:
continue;
}
- lines.push_back(leaf.fUserData);
+ lines.emplace_back(leaf.fUserData);
}
for (auto& leaf : kState.fSyntaxTree->fLeafList)
@@ -483,59 +483,59 @@ static void cxx_print_help()
int main(int argc, char** argv)
{
- kKeywords.push_back("auto");
- kKeywords.push_back("else");
- kKeywords.push_back("break");
- kKeywords.push_back("switch");
- kKeywords.push_back("enum");
- kKeywords.push_back("register");
- kKeywords.push_back("do");
- kKeywords.push_back("return");
- kKeywords.push_back("if");
- kKeywords.push_back("default");
- kKeywords.push_back("struct");
- kKeywords.push_back("_Packed");
- kKeywords.push_back("extern");
- kKeywords.push_back("volatile");
- kKeywords.push_back("static");
- kKeywords.push_back("for");
- kKeywords.push_back("class");
- kKeywords.push_back("{");
- kKeywords.push_back("}");
- kKeywords.push_back("(");
- kKeywords.push_back(")");
- kKeywords.push_back("char");
- kKeywords.push_back("int");
- kKeywords.push_back("short");
- kKeywords.push_back("long");
- kKeywords.push_back("float");
- kKeywords.push_back("double");
- kKeywords.push_back("unsigned");
- kKeywords.push_back("__export__");
- kKeywords.push_back("__packed__");
- kKeywords.push_back("namespace");
- kKeywords.push_back("while");
- kKeywords.push_back("sizeof");
- kKeywords.push_back("private");
- kKeywords.push_back("->");
- kKeywords.push_back(".");
- kKeywords.push_back("::");
- kKeywords.push_back("*");
- kKeywords.push_back("+");
- kKeywords.push_back("-");
- kKeywords.push_back("/");
- kKeywords.push_back("=");
- kKeywords.push_back("==");
- kKeywords.push_back("!=");
- kKeywords.push_back(">=");
- kKeywords.push_back("<=");
- kKeywords.push_back(">");
- kKeywords.push_back("<");
- kKeywords.push_back(":");
- kKeywords.push_back(",");
- kKeywords.push_back(";");
- kKeywords.push_back("public");
- kKeywords.push_back("protected");
+ kKeywords.emplace_back("auto");
+ kKeywords.emplace_back("else");
+ kKeywords.emplace_back("break");
+ kKeywords.emplace_back("switch");
+ kKeywords.emplace_back("enum");
+ kKeywords.emplace_back("register");
+ kKeywords.emplace_back("do");
+ kKeywords.emplace_back("return");
+ kKeywords.emplace_back("if");
+ kKeywords.emplace_back("default");
+ kKeywords.emplace_back("struct");
+ kKeywords.emplace_back("_Packed");
+ kKeywords.emplace_back("extern");
+ kKeywords.emplace_back("volatile");
+ kKeywords.emplace_back("static");
+ kKeywords.emplace_back("for");
+ kKeywords.emplace_back("class");
+ kKeywords.emplace_back("{");
+ kKeywords.emplace_back("}");
+ kKeywords.emplace_back("(");
+ kKeywords.emplace_back(")");
+ kKeywords.emplace_back("char");
+ kKeywords.emplace_back("int");
+ kKeywords.emplace_back("short");
+ kKeywords.emplace_back("long");
+ kKeywords.emplace_back("float");
+ kKeywords.emplace_back("double");
+ kKeywords.emplace_back("unsigned");
+ kKeywords.emplace_back("__export__");
+ kKeywords.emplace_back("__packed__");
+ kKeywords.emplace_back("namespace");
+ kKeywords.emplace_back("while");
+ kKeywords.emplace_back("sizeof");
+ kKeywords.emplace_back("private");
+ kKeywords.emplace_back("->");
+ kKeywords.emplace_back(".");
+ kKeywords.emplace_back("::");
+ kKeywords.emplace_back("*");
+ kKeywords.emplace_back("+");
+ kKeywords.emplace_back("-");
+ kKeywords.emplace_back("/");
+ kKeywords.emplace_back("=");
+ kKeywords.emplace_back("==");
+ kKeywords.emplace_back("!=");
+ kKeywords.emplace_back(">=");
+ kKeywords.emplace_back("<=");
+ kKeywords.emplace_back(">");
+ kKeywords.emplace_back("<");
+ kKeywords.emplace_back(":");
+ kKeywords.emplace_back(",");
+ kKeywords.emplace_back(";");
+ kKeywords.emplace_back("public");
+ kKeywords.emplace_back("protected");
bool skip = false;
@@ -584,7 +584,7 @@ int main(int argc, char** argv)
delete kFactory.Unmount();
kFactory.Mount(new AssemblyMountpointClang());
- kMachine = CxxKit::AssemblyFactory::kArchRISCV;
+ kMachine = CompilerKit::AssemblyFactory::kArchRISCV;
continue;
}
@@ -624,7 +624,7 @@ int main(int argc, char** argv)
kFileList.emplace_back(argv[index]);
- CxxKit::StringView srcFile = CxxKit::StringBuilder::Construct(argv[index]);
+ CompilerKit::StringView srcFile = CompilerKit::StringBuilder::Construct(argv[index]);
if (strstr(argv[index], kExt) == nullptr)
{
diff --git a/CompilerDriver/cpp.cc b/CompilerDriver/cpp.cc
index 29507b7..3b6d914 100644
--- a/CompilerDriver/cpp.cc
+++ b/CompilerDriver/cpp.cc
@@ -7,8 +7,8 @@
* ========================================================
*/
-#include <C++Kit/StdKit/ErrorID.hpp>
-#include <C++Kit/ParserKit.hpp>
+#include <CompilerKit/StdKit/ErrorID.hpp>
+#include <CompilerKit/ParserKit.hpp>
#include <sstream>
#include <iostream>
#include <fstream>
@@ -58,8 +58,8 @@ namespace details
CXXKIT_COPY_DEFAULT(cpp_pragma);
- std::string fMacroName{ "" };
- Int32(*fParse)(std::string& line, std::ifstream& hdr_file, std::ofstream& pp_out);
+ std::string fMacroName;
+ cpp_parser_fn_t fParse;
};
}
@@ -144,15 +144,15 @@ int32_t cpp_parse_if_condition(details::cpp_macro_condition& cond,
std::string number;
- for (auto& macro : kMacros)
+ for (auto& macro_num : kMacros)
{
- if (substr_macro.find(macro.fName) != std::string::npos)
+ if (substr_macro.find(macro_num.fName) != std::string::npos)
{
- for (size_t i = 0; i < macro.fName.size(); ++i)
+ for (size_t i = 0; i < macro_num.fName.size(); ++i)
{
- if (isdigit(macro.fValue[i]))
+ if (isdigit(macro_num.fValue[i]))
{
- number += macro.fValue[i];
+ number += macro_num.fValue[i];
}
else
{
@@ -287,9 +287,7 @@ void cpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out)
std::string line_after_include;
bool inactive_code = false;
- bool comment = false;
bool defined = false;
- bool else_branch = false;
try
{
@@ -473,15 +471,15 @@ void cpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out)
std::string symbol;
- for (size_t i = 0; i < macro.fValue.size(); i++)
+ for (char i : macro.fValue)
{
- if (macro.fValue[i] == '(')
+ if (i == '(')
break;
- if (macro.fValue[i] == '\\')
+ if (i == '\\')
continue;
- symbol += macro.fValue[i];
+ symbol += i;
}
hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), symbol);
@@ -596,8 +594,6 @@ void cpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out)
continue;
}
-
- else_branch = true;
}
else if (hdr_line[0] == '#' &&
hdr_line.find("ifdef") != std::string::npos)
@@ -845,7 +841,11 @@ kIncludeFile:
for (auto& include : kIncludes)
{
- std::ifstream header(include + '/' + path);
+ std::string header_path = include;
+ header_path.push_back('/');
+ header_path += path;
+
+ std::ifstream header(header_path);
if (!header.is_open())
continue;
@@ -857,7 +857,7 @@ kIncludeFile:
break;
}
- if (open == false)
+ if (!open)
{
throw std::runtime_error("cpp: no such include file: " + path);
}
diff --git a/CompilerDriver/ld.cc b/CompilerDriver/ld.cc
index f3a2225..e32c03d 100644
--- a/CompilerDriver/ld.cc
+++ b/CompilerDriver/ld.cc
@@ -16,17 +16,17 @@
// It will be loaded when program will start up!
// Unlike $$dynamic$$ these containers will be loaded before CUS will do its job.
-#include <C++Kit/StdKit/ErrorID.hpp>
+#include <CompilerKit/StdKit/ErrorID.hpp>
#include <fstream>
#include <iostream>
#include <uuid/uuid.h>
//! Portable Executable Format
-#include <C++Kit/StdKit/PEF.hpp>
+#include <CompilerKit/StdKit/PEF.hpp>
//! Advanced Executable Object Format
-#include <C++Kit/StdKit/AE.hpp>
+#include <CompilerKit/StdKit/AE.hpp>
//! @brief standard PEF entry.
#define kPefStart "__start"
@@ -47,15 +47,15 @@
enum { kAbiMpUx = 0x5046 /* PF */ };
-std::ofstream& operator<<(std::ofstream& fp, CxxKit::PEFContainer& container)
+std::ofstream& operator<<(std::ofstream& fp, CompilerKit::PEFContainer& container)
{
- fp.write((char*)&container, sizeof(CxxKit::PEFContainer));
+ fp.write((char*)&container, sizeof(CompilerKit::PEFContainer));
return fp;
}
-std::ofstream& operator<<(std::ofstream& fp, CxxKit::PEFCommandHeader& container)
+std::ofstream& operator<<(std::ofstream& fp, CompilerKit::PEFCommandHeader& container)
{
- fp.write((char*)&container, sizeof(CxxKit::PEFCommandHeader));
+ fp.write((char*)&container, sizeof(CompilerKit::PEFCommandHeader));
return fp;
}
@@ -109,7 +109,7 @@ int main(int argc, char** argv)
//
else if (StringCompare(argv[i], "-m64000") == 0)
{
- kArch = CxxKit::kPefArch64000;
+ kArch = CompilerKit::kPefArch64000;
continue;
}
@@ -184,12 +184,12 @@ int main(int argc, char** argv)
return CXXKIT_EXEC_ERROR;
}
- CxxKit::PEFContainer pef_container{};
+ CompilerKit::PEFContainer pef_container{};
int32_t archs = kArch;
pef_container.Count = 0UL;
- pef_container.Kind = CxxKit::kPefKindExec;
+ pef_container.Kind = CompilerKit::kPefKindExec;
pef_container.SubCpu = kSubArch;
pef_container.Linker = kPefLinkerNumId; // Western Company Linker
pef_container.Abi = kAbi; // Multi-Processor UX ABI
@@ -200,7 +200,7 @@ int main(int argc, char** argv)
// specify the start address, can be 0x10000
pef_container.Start = kPefDeaultOrg;
- pef_container.HdrSz = sizeof(CxxKit::PEFContainer);
+ pef_container.HdrSz = sizeof(CompilerKit::PEFContainer);
std::ofstream output_fc(kOutput, std::ofstream::binary);
@@ -216,18 +216,18 @@ int main(int argc, char** argv)
//! Read AE to convert as PEF.
- std::vector<CxxKit::PEFCommandHeader> pef_command_hdrs;
+ std::vector<CompilerKit::PEFCommandHeader> pef_command_hdrs;
for (const auto& i : kObjectList)
{
if (!std::filesystem::exists(i))
continue;
- CxxKit::AEHeader hdr{};
+ CompilerKit::AEHeader hdr{};
std::ifstream input_object(i, std::ifstream::binary);
- input_object.read((char*)&hdr, sizeof(CxxKit::AEHeader));
+ input_object.read((char*)&hdr, sizeof(CompilerKit::AEHeader));
auto ae_header = hdr;
@@ -257,7 +257,7 @@ int main(int argc, char** argv)
if (ae_header.fMagic[0] == kAEMag0 &&
ae_header.fMagic[1] == kAEMag1 &&
- ae_header.fSize == sizeof(CxxKit::AEHeader))
+ ae_header.fSize == sizeof(CompilerKit::AEHeader))
{
// append arch type to archs varaible.
archs |= ae_header.fArch;
@@ -268,16 +268,16 @@ int main(int argc, char** argv)
pef_container.Count = cnt;
- char* raw_ae_records = new char[cnt * sizeof(CxxKit::AERecordHeader)];
- memset(raw_ae_records, 0, cnt * sizeof(CxxKit::AERecordHeader));
+ char* raw_ae_records = new char[cnt * sizeof(CompilerKit::AERecordHeader)];
+ memset(raw_ae_records, 0, cnt * sizeof(CompilerKit::AERecordHeader));
- input_object.read(raw_ae_records, std::streamsize(cnt * sizeof(CxxKit::AERecordHeader)));
+ input_object.read(raw_ae_records, std::streamsize(cnt * sizeof(CompilerKit::AERecordHeader)));
- auto* ae_records = (CxxKit::AERecordHeader*)raw_ae_records;
+ auto* ae_records = (CompilerKit::AERecordHeader*)raw_ae_records;
for (size_t ae_record_index = 0; ae_record_index < cnt; ++ae_record_index)
{
- CxxKit::PEFCommandHeader command_header{ 0 };
+ CompilerKit::PEFCommandHeader command_header{ 0 };
memcpy(command_header.Name, ae_records[ae_record_index].fName, kPefNameLen);
@@ -440,7 +440,7 @@ ld_continue_search:
// step 4: write some pef commands.
- CxxKit::PEFCommandHeader date_header{};
+ CompilerKit::PEFCommandHeader date_header{};
time_t timestamp = time(nullptr);
@@ -450,24 +450,24 @@ ld_continue_search:
strcpy(date_header.Name, timestamp_str.c_str());
date_header.Flags = 0;
- date_header.Kind = CxxKit::kPefData;
+ date_header.Kind = CompilerKit::kPefData;
date_header.Offset = output_fc.tellp();
date_header.Size = timestamp_str.size();
output_fc << date_header;
- CxxKit::PEFCommandHeader abi_header{};
+ CompilerKit::PEFCommandHeader abi_header{};
memcpy(abi_header.Name, kPefAbiId, strlen(kPefAbiId));
abi_header.Size = strlen(kPefAbiId);
abi_header.Offset = output_fc.tellp();
abi_header.Flags = 0;
- abi_header.Kind = CxxKit::kPefLinkerID;
+ abi_header.Kind = CompilerKit::kPefLinkerID;
output_fc << abi_header;
- CxxKit::PEFCommandHeader uuid_header{};
+ CompilerKit::PEFCommandHeader uuid_header{};
uuid_t uuid{ 0 };
uuid_generate_random(uuid);
diff --git a/CompilerDriver/makefile b/CompilerDriver/makefile
index e1060d5..9dffbf1 100644
--- a/CompilerDriver/makefile
+++ b/CompilerDriver/makefile
@@ -8,7 +8,7 @@
#
LINK_CC=g++ -std=c++20
-LINK_INC=-I../ -I../C++Kit
+LINK_INC=-I../ -I../CompilerKit
LINK_SRC=ld.cc
LINK_OUTPUT=bin/ld
LINK_ALT_OUTPUT=bin/mld
@@ -19,16 +19,16 @@ PP_OUTPUT=bin/cpp
CC2_OUTPUT=bin/cppfront
CC2_SRC=cc2/source/cppfront.cpp
-CC_SRC=ccplus.cc ../C++Kit/StdKit/*.cc ../C++Kit/AsmKit/*.cc
+CC_SRC=ccplus.cc ../CompilerKit/StdKit/*.cc ../CompilerKit/AsmKit/*.cc
CC_OUTPUT=bin/ccplus
-CC_SRC=cc.cc ../C++Kit/StdKit/*.cc ../C++Kit/AsmKit/*.cc
+CC_SRC=cc.cc ../CompilerKit/StdKit/*.cc ../CompilerKit/AsmKit/*.cc
CC_OUTPUT=bin/cc
-CXX_SRC=ccplus.cc ../C++Kit/StdKit/*.cc ../C++Kit/AsmKit/*.cc
+CXX_SRC=ccplus.cc ../CompilerKit/StdKit/*.cc ../CompilerKit/AsmKit/*.cc
CXX_OUTPUT=bin/ccplus
-MASM_SRC=masm.cc ../C++Kit/StdKit/*.cc ../C++Kit/AsmKit/*.cc
+MASM_SRC=masm.cc ../CompilerKit/StdKit/*.cc ../CompilerKit/AsmKit/*.cc
MASM_OUTPUT=bin/masm
.PHONY: all
diff --git a/CompilerDriver/masm.cc b/CompilerDriver/masm.cc
index 0675dba..f60c716 100644
--- a/CompilerDriver/masm.cc
+++ b/CompilerDriver/masm.cc
@@ -18,10 +18,10 @@
/////////////////////////////////////////////////////////////////////////////////////////
-#include <C++Kit/AsmKit/Arch/64k.hpp>
-#include <C++Kit/ParserKit.hpp>
-#include <C++Kit/StdKit/PEF.hpp>
-#include <C++Kit/StdKit/AE.hpp>
+#include <CompilerKit/AsmKit/Arch/64k.hpp>
+#include <CompilerKit/ParserKit.hpp>
+#include <CompilerKit/StdKit/PEF.hpp>
+#include <CompilerKit/StdKit/AE.hpp>
#include <filesystem>
#include <iostream>
#include <fstream>
@@ -39,7 +39,7 @@
#define kStdOut (std::cout << kWhite)
-static char kOutputArch = CxxKit::kPefArch64000;
+static char kOutputArch = CompilerKit::kPefArch64000;
//! base relocation address for every mp-ux app.
static UInt32 kErrorLimit = 10;
@@ -50,9 +50,9 @@ static std::size_t kCounter = 1UL;
static bool kVerbose = false;
static std::vector<char> kBytes;
-static CxxKit::AERecordHeader kCurrentRecord{ .fName = "", .fKind = CxxKit::kPefCode, .fSize = 0, .fOffset = 0 };
+static CompilerKit::AERecordHeader kCurrentRecord{ .fName = "", .fKind = CompilerKit::kPefCode, .fSize = 0, .fOffset = 0 };
-static std::vector<CxxKit::AERecordHeader> kRecords;
+static std::vector<CompilerKit::AERecordHeader> kRecords;
static std::vector<std::string> kUndefinedSymbols;
static const std::string kUndefinedSymbol = ":ld:";
@@ -95,16 +95,16 @@ namespace detail
// provide operator<< for AE
-std::ofstream& operator<<(std::ofstream& fp, CxxKit::AEHeader& container)
+std::ofstream& operator<<(std::ofstream& fp, CompilerKit::AEHeader& container)
{
- fp.write((char*)&container, sizeof(CxxKit::AEHeader));
+ fp.write((char*)&container, sizeof(CompilerKit::AEHeader));
return fp;
}
-std::ofstream& operator<<(std::ofstream& fp, CxxKit::AERecordHeader& container)
+std::ofstream& operator<<(std::ofstream& fp, CompilerKit::AERecordHeader& container)
{
- fp.write((char*)&container, sizeof(CxxKit::AERecordHeader));
+ fp.write((char*)&container, sizeof(CompilerKit::AERecordHeader));
return fp;
}
@@ -139,7 +139,7 @@ int main(int argc, char** argv)
else if (strcmp(argv[i], "-m64000") == 0 ||
strcmp(argv[i], "-m64x0") == 0)
{
- kOutputArch = CxxKit::kPefArch64000;
+ kOutputArch = CompilerKit::kPefArch64000;
if (kVerbose)
{
@@ -184,13 +184,13 @@ int main(int argc, char** argv)
std::string line;
- CxxKit::AEHeader hdr{ 0 };
+ CompilerKit::AEHeader hdr{ 0 };
memset(hdr.fPad, kAEInvalidOpcode, kAEPad);
hdr.fMagic[0] = kAEMag0;
hdr.fMagic[1] = kAEMag1;
- hdr.fSize = sizeof(CxxKit::AEHeader);
+ hdr.fSize = sizeof(CompilerKit::AEHeader);
hdr.fArch = kOutputArch;
/////////////////////////////////////////////////////////////////////////////////////////
@@ -253,7 +253,7 @@ int main(int argc, char** argv)
if (kVerbose)
kStdOut << "masm: wrote record " << rec.fName << " to file...\n";
- rec.fFlags |= CxxKit::kKindRelocationAtRuntime;
+ rec.fFlags |= CompilerKit::kKindRelocationAtRuntime;
rec.fOffset = record_count;
++record_count;
@@ -265,7 +265,7 @@ int main(int argc, char** argv)
for (auto& sym : kUndefinedSymbols)
{
- CxxKit::AERecordHeader _record_hdr{ 0 };
+ CompilerKit::AERecordHeader _record_hdr{ 0 };
if (kVerbose)
kStdOut << "masm: wrote symbol " << sym << " to file...\n";
@@ -353,17 +353,17 @@ static bool masm_read_attributes(std::string& line)
if (name.find(".text") != std::string::npos)
{
// data is treated as code.
- kCurrentRecord.fKind = CxxKit::kPefCode;
+ kCurrentRecord.fKind = CompilerKit::kPefCode;
}
else if (name.find(".data") != std::string::npos)
{
// no code will be executed from here.
- kCurrentRecord.fKind = CxxKit::kPefData;
+ kCurrentRecord.fKind = CompilerKit::kPefData;
}
else if (name.find(".page_zero") != std::string::npos)
{
// this is a bss section.
- kCurrentRecord.fKind = CxxKit::kPefZero;
+ kCurrentRecord.fKind = CompilerKit::kPefZero;
}
// this is a special case for the start stub.
@@ -371,7 +371,7 @@ static bool masm_read_attributes(std::string& line)
if (name == "__start")
{
- kCurrentRecord.fKind = CxxKit::kPefCode;
+ kCurrentRecord.fKind = CompilerKit::kPefCode;
}
// now we can tell the code size of the previous kCurrentRecord.
@@ -410,17 +410,17 @@ static bool masm_read_attributes(std::string& line)
if (name.find(".text") != std::string::npos)
{
// data is treated as code.
- kCurrentRecord.fKind = CxxKit::kPefCode;
+ kCurrentRecord.fKind = CompilerKit::kPefCode;
}
else if (name.find(".data") != std::string::npos)
{
// no code will be executed from here.
- kCurrentRecord.fKind = CxxKit::kPefData;
+ kCurrentRecord.fKind = CompilerKit::kPefData;
}
else if (name.find(".page_zero") != std::string::npos)
{
// this is a bss section.
- kCurrentRecord.fKind = CxxKit::kPefZero;
+ kCurrentRecord.fKind = CompilerKit::kPefZero;
}
// this is a special case for the start stub.
@@ -428,7 +428,7 @@ static bool masm_read_attributes(std::string& line)
if (name == "__start")
{
- kCurrentRecord.fKind = CxxKit::kPefCode;
+ kCurrentRecord.fKind = CompilerKit::kPefCode;
}
// now we can tell the code size of the previous kCurrentRecord.
@@ -563,15 +563,14 @@ static std::string masm_check_line(std::string& line, const std::string& file)
std::vector<std::string> opcodes_list = { "jb", "psh", "stw", "ldw", "lda", "sta" };
- for (auto& opcodes : kOpcodes64x0)
+ for (auto& opcode64x0 : kOpcodes64x0)
{
- if (line.find(opcodes.fName) != std::string::npos)
+ if (line.find(opcode64x0.fName) != std::string::npos)
{
for (auto& op : opcodes_list)
{
- if (line == op ||
- line.find(op) != std::string::npos &&
- !isspace(line[line.find(op) + op.size()]))
+ // if only instruction found.
+ if (line == op)
{
err_str += "\nmalformed ";
err_str += op;
@@ -580,11 +579,20 @@ static std::string masm_check_line(std::string& line, const std::string& file)
}
}
+ // if it is like that -> addr1, 0x0
+ if (!isspace(line[line.find(opcode64x0.fName) + strlen(opcode64x0.fName)]))
+ {
+ err_str += "\nmissing space between ";
+ err_str += opcode64x0.fName;
+ err_str += " and operands.\nhere -> ";
+ err_str += line;
+ }
+
return err_str;
}
}
- err_str += "Unknown syntax: ";
+ err_str += "unknown syntax: ";
err_str += line;
return err_str;
@@ -626,8 +634,6 @@ static bool masm_write_number(std::size_t pos, std::string& jump_label)
{
detail::print_error("invalid hex number: " + jump_label, "masm");
throw std::runtime_error("invalid_hex");
-
- return false;
}
}
@@ -656,8 +662,6 @@ static bool masm_write_number(std::size_t pos, std::string& jump_label)
{
detail::print_error("invalid binary number: " + jump_label, "masm");
throw std::runtime_error("invalid_bin");
-
- return false;
}
}
@@ -686,8 +690,6 @@ static bool masm_write_number(std::size_t pos, std::string& jump_label)
{
detail::print_error("invalid octal number: " + jump_label, "masm");
throw std::runtime_error("invalid_octal");
-
- return false;
}
}
@@ -747,19 +749,23 @@ static bool masm_write_number(std::size_t pos, std::string& jump_label)
static void masm_read_instruction(std::string& line, const std::string& file)
{
- for (auto& opcodes : kOpcodes64x0)
+ if (ParserKit::find_word(line, "__export"))
+ return;
+
+ for (auto& opcode64x0 : kOpcodes64x0)
{
- if (ParserKit::find_word(line, opcodes.fName))
+ // strict check here
+ if (ParserKit::find_word(line, opcode64x0.fName))
{
- std::string name(opcodes.fName);
+ std::string name(opcode64x0.fName);
std::string jump_label, cpy_jump_label;
- kBytes.emplace_back(opcodes.fOpcode);
- kBytes.emplace_back(opcodes.fFunct3);
- kBytes.emplace_back(opcodes.fFunct7);
+ kBytes.emplace_back(opcode64x0.fOpcode);
+ kBytes.emplace_back(opcode64x0.fFunct3);
+ kBytes.emplace_back(opcode64x0.fFunct7);
// check funct7
- switch (opcodes.fFunct7)
+ switch (opcode64x0.fFunct7)
{
// reg to reg means register to register transfer operation.
case kAsmRegToReg:
@@ -784,6 +790,16 @@ static void masm_read_instruction(std::string& line, const std::string& file)
if (isdigit(line[line_index + 2]))
reg_str += line[line_index + 2];
+
+ if (kOutputArch == CompilerKit::kPefArch64000)
+ {
+ if (isdigit(line[line_index + 3]))
+ {
+ reg_str += line[line_index + 3];
+ detail::print_error("invalid register index, r" + reg_str + "\nnote: 64x0 accepts registers from r0 to r20.", file);
+ throw std::runtime_error("invalid_register_index");
+ }
+ }
std::size_t reg_index = strtoq(
reg_str.c_str(),
@@ -808,7 +824,7 @@ static void masm_read_instruction(std::string& line, const std::string& file)
}
// we're not in immediate addressing, reg to reg.
- if (opcodes.fFunct7 != kAsmImmediate)
+ if (opcode64x0.fFunct7 != kAsmImmediate)
{
// remember! register to register!
if (found_some == 1)
@@ -846,13 +862,17 @@ static void masm_read_instruction(std::string& line, const std::string& file)
name == "stw" ||
name == "ldw" ||
name == "lda" ||
- name == "sta")
+ name == "sta" ||
+ name == "add" ||
+ name == "dec")
{
auto where_string = name;
if (name == "stw" ||
name == "ldw" ||
- name == "lda")
+ name == "lda" ||
+ name == "add" ||
+ name == "dec")
where_string = ",";
jump_label = line.substr(line.find(where_string) + where_string.size());
@@ -878,8 +898,6 @@ static void masm_read_instruction(std::string& line, const std::string& file)
{
detail::print_error("invalid combination of opcode and operands.\nhere ->" + line, file);
throw std::runtime_error("invalid_comb_op_ops");
-
- break;
}
goto masm_write_label;
@@ -891,12 +909,11 @@ static void masm_read_instruction(std::string& line, const std::string& file)
{
detail::print_error("invalid usage __import on 'sta', here: " + line, file);
throw std::runtime_error("invalid_sta_usage");
- break;
}
}
}
- // if jump to branch
+ // This is the case where we jump to a label, it is also used as a goto.
if (name == "jb")
{
masm_write_label:
@@ -908,14 +925,14 @@ masm_write_label:
cpy_jump_label.find("__import") == std::string::npos &&
name == "jb")
{
- detail::print_error("__import not found on jump label, please add one.", file.c_str());
+ 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)
{
if (name == "sta")
{
- detail::print_error("__import is not allowed on a sta operation.", file.c_str());
+ detail::print_error("__import is not allowed on a sta operation.", file);
throw std::runtime_error("import_sta_op");
}