diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-05-11 15:10:23 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-05-11 15:10:23 +0200 |
| commit | 543b8c7287ac1248453a59cd3d759ce5ffeec4a3 (patch) | |
| tree | d152fcb8bedf413841536109a5a612c2a4186bb3 | |
| parent | 992764412966eeff4296e086a3717053d86cae5e (diff) | |
MHR-21: Work in progress code generator.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
| -rw-r--r-- | Doxyfile | 2 | ||||
| -rw-r--r-- | Examples/ExampleCPlusPlus.cxx | 11 | ||||
| -rw-r--r-- | Examples/ExampleCPlusPlus.s | 14 | ||||
| -rw-r--r-- | Headers/ParserKit.hpp | 9 | ||||
| -rw-r--r-- | ReadMe.md | 2 | ||||
| -rw-r--r-- | SDK/__mpcc_power.inc | 2 | ||||
| -rw-r--r-- | Sources/64x0-cc.cc | 2 | ||||
| -rw-r--r-- | Sources/cplusplus.cc | 75 | ||||
| -rw-r--r-- | posix.make | 7 | ||||
| -rw-r--r-- | win64.make | 4 |
10 files changed, 103 insertions, 25 deletions
@@ -54,7 +54,7 @@ PROJECT_NUMBER = 2.0.0 # for a project that appears at the top of each page and should give viewer a # quick idea about the purpose of the project. Keep the description short. -PROJECT_BRIEF = "CodeTools" +PROJECT_BRIEF = "MPCC" # With the PROJECT_LOGO tag one can specify a logo or an icon that is included # in the documentation. The maximum height of the logo should not exceed 55 diff --git a/Examples/ExampleCPlusPlus.cxx b/Examples/ExampleCPlusPlus.cxx new file mode 100644 index 0000000..38bf0c5 --- /dev/null +++ b/Examples/ExampleCPlusPlus.cxx @@ -0,0 +1,11 @@ +int main(int argc, char const *argv[]) +{ + struct ExampleCPlusPlus + { + int example_data; + }* example; + + example->example_data = 0; + + return 0; +} diff --git a/Examples/ExampleCPlusPlus.s b/Examples/ExampleCPlusPlus.s new file mode 100644 index 0000000..328bce7 --- /dev/null +++ b/Examples/ExampleCPlusPlus.s @@ -0,0 +1,14 @@ +; Path: Examples/ExampleCPlusPlus.cxx +; Language: MPCC assembly. (Generated from C++) +; Date: 2024-5-11 + +#bits 64 + +#org 0x1000000 + +
+
+lea %LEFT+%OFFSET
+
+mov rax, %A0
+ret
\ No newline at end of file diff --git a/Headers/ParserKit.hpp b/Headers/ParserKit.hpp index 60ac892..f14ed69 100644 --- a/Headers/ParserKit.hpp +++ b/Headers/ParserKit.hpp @@ -43,11 +43,13 @@ namespace ParserKit struct CompilerKeyword; /// we want to do that because to separate keywords. - enum + enum KeywordKind { eKeywordKindNamespace, - eKeywordKindFunction, + eKeywordKindFunctionStart, + eKeywordKindFunctionEnd, eKeywordKindVariable, + eKeywordKindType, eKeywordKindExpressionBegin, eKeywordKindExpressionEnd, eKeywordKindArgSeparator, @@ -65,13 +67,14 @@ namespace ParserKit eKeywordKindEndInstr, eKeywordKindSpecifier, eKeywordKindInvalid, + eKeywordKindReturn, }; /// \brief Compiler keyword information struct. struct CompilerKeyword { std::string keyword_name; - int32_t keyword_kind = eKeywordKindInvalid; + KeywordKind keyword_kind = eKeywordKindInvalid; }; struct SyntaxLeafList final { @@ -1,4 +1,4 @@ -# CodeTools +# MPCC Start by cloning the repo: diff --git a/SDK/__mpcc_power.inc b/SDK/__mpcc_power.inc index 96403a4..9e4928c 100644 --- a/SDK/__mpcc_power.inc +++ b/SDK/__mpcc_power.inc @@ -1,5 +1,5 @@ # Path: SDK/__mpcc_power.inc -# Language: Mahrouss POWER Assembly support for GNU. +# Language: MPCC POWER Assembly support for GNU. # Build Date: 2024-6-4 %ifdef __CODETOOLS__ diff --git a/Sources/64x0-cc.cc b/Sources/64x0-cc.cc index 5566666..b7ea908 100644 --- a/Sources/64x0-cc.cc +++ b/Sources/64x0-cc.cc @@ -26,7 +26,7 @@ #define kOk 0 /* C driver */ -/* This is part of CodeTools C SDK. */ +/* This is part of MPCC C SDK. */ /* (c) SoftwareLabs */ /// @author Amlal El Mahrouss (amlel) diff --git a/Sources/cplusplus.cc b/Sources/cplusplus.cc index 23df90e..b61b1d2 100644 --- a/Sources/cplusplus.cc +++ b/Sources/cplusplus.cc @@ -30,7 +30,7 @@ #define kOk 0 /* SoftwareLabs C++ driver */ -/* This is part of CodeTools C++ compiler. */ +/* This is part of MPCC C++ compiler. */ /* (c) SoftwareLabs */ // @author Amlal El Mahrouss (amlel) @@ -94,9 +94,9 @@ static Int32 kAcceptableErrors = 0; namespace detail { - /// @brief prints an error into stdout. - /// @param reason the reason of the error. - /// @param file where does it originate from? + /// @brief prints an error into stdout. + /// @param reason the reason of the error. + /// @param file where does it originate from? void print_error(std::string reason, std::string file) noexcept { if (reason[0] == '\n') @@ -205,7 +205,7 @@ namespace detail const char* CompilerBackendCPlusPlus::Language() { - return "C++"; + return "ISO C++"; } ///////////////////////////////////////////////////////////////////////////////////////// @@ -243,19 +243,58 @@ bool CompilerBackendCPlusPlus::Compile(const std::string& text, if (!found) { - detail::print_error("syntax error: " + text, file); + for (size_t i = 0; i < text.size(); i++) + { + if (isalnum(text[i])) + { + detail::print_error("syntax error: " + text, file); + return false; + } + } } - // TODO: sort keywords + static bool shouldEmitWarning = false; for (auto& keyword : keywords_list) { + if (shouldEmitWarning) + { + if (keyword.first.keyword_kind == ParserKit::KeywordKind::eKeywordKindBodyEnd) + shouldEmitWarning = false; + + if (shouldEmitWarning) + { + detail::print_error("code after return: " + text, file); + } + } + auto syntax_tree = ParserKit::SyntaxLeafList::SyntaxLeaf(); + switch (keyword.first.keyword_kind) + { + case ParserKit::KeywordKind::eKeywordKindAccess: + case ParserKit::KeywordKind::eKeywordKindPtrAccess: + kState.fSyntaxTree->fLeafList[kState.fSyntaxTree->fLeafList.size() - 1].fUserValue = "lea %LEFT+%OFFSET"; // MPCC assembly stipulates this. + break; + case ParserKit::KeywordKind::eKeywordKindEndInstr: + syntax_tree.fUserValue = "\r\n"; + break; + case ParserKit::KeywordKind::eKeywordKindVariableAssign: + syntax_tree.fUserValue = "mov %LEFT, %RIGHT"; + break; + case ParserKit::KeywordKind::eKeywordKindReturn: + syntax_tree.fUserValue = "mov rax, %A0\r\nret"; + shouldEmitWarning = true; + break; + default: + break; + } + syntax_tree.fUserData = keyword.first; kState.fSyntaxTree->fLeafList.emplace_back(syntax_tree); } +_MpccOkay: return true; } @@ -303,8 +342,6 @@ public: dest += ch; } - /* According to PEF ABI. */ - std::vector<const char*> exts = kAsmFileExts; dest += exts[3]; @@ -314,7 +351,7 @@ public: (*kState.fOutputAssembly) << "; Path: " << src_file << "\n"; (*kState.fOutputAssembly) - << "; Language: CodeTools assembly. (Generated from C++)\n"; + << "; Language: MPCC assembly. (Generated from C++)\n"; (*kState.fOutputAssembly) << "; Date: " << fmt << "\n\n"; (*kState.fOutputAssembly) << "#bits 64\n\n#org 0x1000000" << "\n\n"; @@ -330,7 +367,7 @@ public: while (std::getline(src_fp, source)) { // Compile into an object file. - kCompilerBackend->Compile(source.c_str(), src.data()); + kCompilerBackend->Compile(source.c_str(), src.c_str()); } for (auto& ast : kState.fSyntaxTree->fLeafList) @@ -351,7 +388,7 @@ static void cxx_print_help() { kSplashCxx(); kPrintF("%s", "No help available, see:\r\n"); - kPrintF("%s", "www.el-mahrouss-logic.com/developer/newos/cplusplus\r\n"); + kPrintF("%s", "www.el-mahrouss-logic.com/softwarelabs/developer/newos/cplusplus\r\n"); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -370,9 +407,18 @@ MPCC_MODULE(CompilerCPlusPlus) kKeywords.push_back({.keyword_name = "namespace", .keyword_kind = ParserKit::eKeywordKindNamespace}); kKeywords.push_back({.keyword_name = "typedef", .keyword_kind = ParserKit::eKeywordKindTypedef}); kKeywords.push_back({.keyword_name = "using", .keyword_kind = ParserKit::eKeywordKindTypedef}); - kKeywords.push_back({.keyword_name = "}", .keyword_kind = ParserKit::eKeywordKindBodyStart}); - kKeywords.push_back({.keyword_name = "{", .keyword_kind = ParserKit::eKeywordKindBodyEnd}); + kKeywords.push_back({.keyword_name = "{", .keyword_kind = ParserKit::eKeywordKindBodyStart}); + kKeywords.push_back({.keyword_name = "}", .keyword_kind = ParserKit::eKeywordKindBodyEnd}); kKeywords.push_back({.keyword_name = "auto", .keyword_kind = ParserKit::eKeywordKindVariable}); + kKeywords.push_back({.keyword_name = "int", .keyword_kind = ParserKit::eKeywordKindType}); + kKeywords.push_back({.keyword_name = "unsigned", .keyword_kind = ParserKit::eKeywordKindType}); + kKeywords.push_back({.keyword_name = "short", .keyword_kind = ParserKit::eKeywordKindType}); + kKeywords.push_back({.keyword_name = "(", .keyword_kind = ParserKit::eKeywordKindFunctionStart}); + kKeywords.push_back({.keyword_name = ")", .keyword_kind = ParserKit::eKeywordKindFunctionEnd}); + kKeywords.push_back({.keyword_name = "char", .keyword_kind = ParserKit::eKeywordKindType}); + kKeywords.push_back({.keyword_name = "long", .keyword_kind = ParserKit::eKeywordKindType}); + kKeywords.push_back({.keyword_name = "float", .keyword_kind = ParserKit::eKeywordKindType}); + kKeywords.push_back({.keyword_name = "double", .keyword_kind = ParserKit::eKeywordKindType}); kKeywords.push_back({.keyword_name = "=", .keyword_kind = ParserKit::eKeywordKindVariableAssign}); kKeywords.push_back({.keyword_name = "const", .keyword_kind = ParserKit::eKeywordKindConstant}); kKeywords.push_back({.keyword_name = "->", .keyword_kind = ParserKit::eKeywordKindPtrAccess}); @@ -384,6 +430,7 @@ MPCC_MODULE(CompilerCPlusPlus) kKeywords.push_back({.keyword_name = "private:", .keyword_kind = ParserKit::eKeywordKindSpecifier}); kKeywords.push_back({.keyword_name = "protected:", .keyword_kind = ParserKit::eKeywordKindSpecifier}); kKeywords.push_back({.keyword_name = "final", .keyword_kind = ParserKit::eKeywordKindSpecifier}); + kKeywords.push_back({.keyword_name = "return", .keyword_kind = ParserKit::eKeywordKindReturn}); kFactory.Mount(new AssemblyMountpointClang()); kCompilerBackend = new CompilerBackendCPlusPlus(); @@ -14,6 +14,7 @@ LINK_OUTPUT=Output/link.exec LINK_ALT_OUTPUT=Output/64link.exec LINK_ALT_3_OUTPUT=Output/i64link.exec LINK_ALT_2_OUTPUT=Output/32link.exec +LINK_ALT_4_OUTPUT=Output/ppclink.exec PP_SRC=Sources/bpp.cc PP_OUTPUT=Output/bpp.exec @@ -67,11 +68,12 @@ linker: cp $(LINK_OUTPUT) $(LINK_ALT_OUTPUT) cp $(LINK_OUTPUT) $(LINK_ALT_2_OUTPUT) cp $(LINK_OUTPUT) $(LINK_ALT_3_OUTPUT) + cp $(LINK_OUTPUT) $(LINK_ALT_4_OUTPUT) .PHONY: help help: - @echo "Compiler - Mahrouss Compilers." - @echo "Preprocessor - Mahrouss Preprocessors." + @echo "Compiler - MPCC Compiler Suite." + @echo "Preprocessor - MPCC Preprocessor Suite." @echo "linker - SoftwareLabs Linkers." @echo "clean - Clean objects and executables." @@ -84,6 +86,7 @@ clean: rm -f $(IASM_OUTPUT) rm -f $(LINK_OUTPUT) rm -rf *.obj + rm -rf Output/*.exec rm -rf *.exec # Last rev 8-1-24 @@ -78,8 +78,8 @@ linker: .PHONY: help help: - @echo "Compiler - Mahrouss Compilers." - @echo "Preprocessor - Mahrouss Preprocessors." + @echo "Compiler - MPCC Compiler Suite." + @echo "Preprocessor - MPCC Preprocessor Suite." @echo "linker - SoftwareLabs Linkers." @echo "clean - Clean objects and executables." |
