summaryrefslogtreecommitdiffhomepage
path: root/dev
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-12-25 18:00:02 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-12-25 18:00:29 +0100
commitc2046f25120d8c39b36cb81459f3370c8a5f1fa3 (patch)
tree77fd2e2c52457d6c48386f51ec5e4409f6de4957 /dev
parent40665a65f74ba2c928b2abc4a38317555ffd3e18 (diff)
FIX: Important fixes to C++ and Also prompting assembly for jump
instructions. TODO: need to support arguments stack (PEF calling convention) when generating code. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev')
-rw-r--r--dev/ToolchainKit/src/Assembler64x0.cc64
-rw-r--r--dev/ToolchainKit/src/AssemblerAMD64.cc62
-rw-r--r--dev/ToolchainKit/src/AssemblerPower.cc46
-rw-r--r--dev/ToolchainKit/src/CCompiler64x0.cc10
-rw-r--r--dev/ToolchainKit/src/CCompilerPower64.cc10
-rw-r--r--dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc66
-rw-r--r--dev/ToolchainKit/src/CPlusPlusCompilerPreProcessor.cc2
-rw-r--r--dev/ToolchainKit/src/CPlusPlusRuleChecker.cc8
-rw-r--r--dev/ToolchainKit/src/Detail/AsmUtils.h12
9 files changed, 152 insertions, 128 deletions
diff --git a/dev/ToolchainKit/src/Assembler64x0.cc b/dev/ToolchainKit/src/Assembler64x0.cc
index db05e2e..fe97e09 100644
--- a/dev/ToolchainKit/src/Assembler64x0.cc
+++ b/dev/ToolchainKit/src/Assembler64x0.cc
@@ -23,7 +23,7 @@
#include <ToolchainKit/Parser.h>
#include <ToolchainKit/NFC/AE.h>
#include <ToolchainKit/NFC/PEF.h>
-#include <Algorithms>
+#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
@@ -74,16 +74,16 @@ static bool asm_read_attributes(std::string& line);
namespace Details
{
- void print_error_asm(std::string reason, std::string file) noexcept
+ void print_error(std::string reason, std::string file) noexcept
{
if (reason[0] == '\n')
reason.erase(0, 1);
- kStdErr << kRed << "[ ToolchainKit ] " << kWhite
+ kStdErr << kRed << "[ TQC++ ] " << kWhite
<< ((file == "ToolchainKit") ? "InternalErrorException: "
: ("FileException{ " + file + " }: "))
<< kBlank << std::endl;
- kStdErr << kRed << "[ ToolchainKit ] " << kWhite << reason << kBlank << std::endl;
+ kStdErr << kRed << "[ TQC++ ] " << kWhite << reason << kBlank << std::endl;
if (kAcceptableErrors > kErrorLimit)
std::exit(3);
@@ -91,17 +91,17 @@ namespace Details
++kAcceptableErrors;
}
- void print_warning_asm(std::string reason, std::string file) noexcept
+ void print_warning(std::string reason, std::string file) noexcept
{
if (reason[0] == '\n')
reason.erase(0, 1);
if (!file.empty())
{
- kStdOut << kYellow << "[ ToolchainKit ] " << kWhite << file << kBlank << std::endl;
+ kStdOut << kYellow << "[ TQC++ ] " << kWhite << file << kBlank << std::endl;
}
- kStdOut << kYellow << "[ ToolchainKit ] " << kWhite << reason << kBlank << std::endl;
+ kStdOut << kYellow << "[ TQC++ ] " << kWhite << reason << kBlank << std::endl;
}
} // namespace Details
@@ -201,7 +201,7 @@ TOOLCHAINKIT_MODULE(AssemblerMain64x0)
{
if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty())
{
- Details::print_error_asm(ln, argv[i]);
+ Details::print_error(ln, argv[i]);
continue;
}
@@ -215,7 +215,7 @@ TOOLCHAINKIT_MODULE(AssemblerMain64x0)
if (kVerbose)
{
std::string what = e.what();
- Details::print_warning_asm("exit because of: " + what, "ToolchainKit");
+ Details::print_warning("exit because of: " + what, "ToolchainKit");
}
std::filesystem::remove(object_output);
@@ -347,7 +347,7 @@ static bool asm_read_attributes(std::string& line)
{
if (kOutputAsBinary)
{
- Details::print_error_asm("Invalid extern_segment directive in flat binary mode.",
+ Details::print_error("Invalid extern_segment directive in flat binary mode.",
"ToolchainKit");
throw std::runtime_error("invalid_extern_segment_bin");
}
@@ -357,7 +357,7 @@ static bool asm_read_attributes(std::string& line)
/// sanity check to avoid stupid linker errors.
if (name.size() == 0)
{
- Details::print_error_asm("Invalid extern_segment", "power-as");
+ Details::print_error("Invalid extern_segment", "power-as");
throw std::runtime_error("invalid_extern_segment");
}
@@ -420,7 +420,7 @@ static bool asm_read_attributes(std::string& line)
{
if (kOutputAsBinary)
{
- Details::print_error_asm("Invalid public_segment directive in flat binary mode.",
+ Details::print_error("Invalid public_segment directive in flat binary mode.",
"ToolchainKit");
throw std::runtime_error("invalid_public_segment_bin");
}
@@ -493,7 +493,7 @@ static bool asm_read_attributes(std::string& line)
// \brief algorithms and helpers.
-namespace Details::Algorithms
+namespace Details::algorithm
{
// \brief authorize a brief set of characters.
static inline bool is_not_alnum_space(char c)
@@ -508,7 +508,7 @@ namespace Details::Algorithms
{
return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end();
}
-} // namespace Details::Algorithms
+} // namespace Details::algorithm
/////////////////////////////////////////////////////////////////////////////////////////
@@ -536,7 +536,7 @@ std::string ToolchainKit::Encoder64x0::CheckLine(std::string& line,
else
{
// now check the line for validity
- if (!Details::Algorithms::is_valid_64x0(line))
+ if (!Details::algorithm::is_valid_64x0(line))
{
err_str = "Line contains non alphanumeric characters.\nhere -> ";
err_str += line;
@@ -546,7 +546,7 @@ std::string ToolchainKit::Encoder64x0::CheckLine(std::string& line,
return err_str;
}
- if (!Details::Algorithms::is_valid_64x0(line))
+ if (!Details::algorithm::is_valid_64x0(line))
{
err_str = "Line contains non alphanumeric characters.\nhere -> ";
err_str += line;
@@ -664,7 +664,7 @@ bool ToolchainKit::Encoder64x0::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid hex number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid hex number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_hex_number");
}
}
@@ -691,7 +691,7 @@ bool ToolchainKit::Encoder64x0::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid binary number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid binary number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -718,7 +718,7 @@ bool ToolchainKit::Encoder64x0::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid octal number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid octal number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -786,7 +786,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
{
// strict check here
if (ToolchainKit::find_word(line, opcode64x0.fName) &&
- Details::Algorithms::is_valid_64x0(line))
+ Details::algorithm::is_valid_64x0(line))
{
std::string name(opcode64x0.fName);
std::string jump_label, cpy_jump_label;
@@ -830,7 +830,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
isdigit(line[line_index + 2]))
{
reg_str += line[line_index + 3];
- Details::print_error_asm(
+ Details::print_error(
"invalid register index, r" + reg_str +
"\nnote: The 64x0 accepts registers from r0 to r20.",
file);
@@ -843,7 +843,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
if (reg_index > kAsmRegisterLimit)
{
- Details::print_error_asm("invalid register index, r" + reg_str,
+ Details::print_error("invalid register index, r" + reg_str,
file);
throw std::runtime_error("invalid_register_index");
}
@@ -866,7 +866,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
// remember! register to register!
if (found_some == 1)
{
- Details::print_error_asm(
+ Details::print_error(
"Too few registers.\ntip: each Assembler64x0 register "
"starts with 'r'.\nline: " +
line,
@@ -878,21 +878,21 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
if (found_some < 1 && name != "ldw" && name != "lda" &&
name != "stw")
{
- Details::print_error_asm(
+ Details::print_error(
"invalid combination of opcode and registers.\nline: " + line,
file);
throw std::runtime_error("invalid_comb_op_reg");
}
else if (found_some == 1 && name == "add")
{
- Details::print_error_asm(
+ Details::print_error(
"invalid combination of opcode and registers.\nline: " + line,
file);
throw std::runtime_error("invalid_comb_op_reg");
}
else if (found_some == 1 && name == "sub")
{
- Details::print_error_asm(
+ Details::print_error(
"invalid combination of opcode and registers.\nline: " + line,
file);
throw std::runtime_error("invalid_comb_op_reg");
@@ -900,7 +900,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
if (found_some > 0 && name == "pop")
{
- Details::print_error_asm(
+ Details::print_error(
"invalid combination for opcode 'pop'.\ntip: it expects "
"nothing.\nline: " +
line,
@@ -941,7 +941,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
{
if (found_sym)
{
- Details::print_error_asm(
+ Details::print_error(
"invalid combination of opcode and operands.\nhere -> " +
jump_label,
file);
@@ -974,7 +974,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
// sta expects this: sta 0x000000, r0
if (name == "sta")
{
- Details::print_error_asm(
+ Details::print_error(
"invalid combination of opcode and operands.\nHere ->" + line,
file);
throw std::runtime_error("invalid_comb_op_ops");
@@ -985,7 +985,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
if (name == "sta" &&
cpy_jump_label.find("extern_segment ") != std::string::npos)
{
- Details::print_error_asm("invalid usage extern_segment on 'sta', here: " + line,
+ Details::print_error("invalid usage extern_segment on 'sta', here: " + line,
file);
throw std::runtime_error("invalid_sta_usage");
}
@@ -1007,7 +1007,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
if (name == "sta")
{
- Details::print_error_asm("extern_segment is not allowed on a sta operation.",
+ Details::print_error("extern_segment is not allowed on a sta operation.",
file);
throw std::runtime_error("extern_segment_sta_op");
}
@@ -1067,7 +1067,7 @@ bool ToolchainKit::Encoder64x0::WriteLine(std::string& line,
if (cpy_jump_label.size() < 1)
{
- Details::print_error_asm("label is empty, can't jump on it.", file);
+ Details::print_error("label is empty, can't jump on it.", file);
throw std::runtime_error("label_empty");
}
diff --git a/dev/ToolchainKit/src/AssemblerAMD64.cc b/dev/ToolchainKit/src/AssemblerAMD64.cc
index 54ee144..ea2fea5 100644
--- a/dev/ToolchainKit/src/AssemblerAMD64.cc
+++ b/dev/ToolchainKit/src/AssemblerAMD64.cc
@@ -30,7 +30,7 @@
#include <ToolchainKit/Parser.h>
#include <ToolchainKit/NFC/AE.h>
#include <ToolchainKit/NFC/PEF.h>
-#include <Algorithms>
+#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <fstream>
@@ -189,7 +189,7 @@ TOOLCHAINKIT_MODULE(AssemblerAMD64)
std::ifstream file_ptr(argv[i]);
std::ofstream file_ptr_out(object_output, std::ofstream::binary);
- kStdOut << "AssemblerAMD64: assembling: " << argv[i] << "\n";
+ kStdOut << "AssemblerAMD64: Assembling: " << argv[i] << "\n";
if (file_ptr_out.bad())
{
@@ -230,7 +230,7 @@ TOOLCHAINKIT_MODULE(AssemblerAMD64)
{
if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty())
{
- Details::print_error_asm(ln, argv[i]);
+ Details::print_error(ln, argv[i]);
continue;
}
@@ -244,7 +244,7 @@ TOOLCHAINKIT_MODULE(AssemblerAMD64)
if (kVerbose)
{
std::string what = e.what();
- Details::print_warning_asm("exit because of: " + what, "ToolchainKit");
+ Details::print_warning("exit because of: " + what, "ToolchainKit");
}
try
@@ -391,7 +391,7 @@ static bool asm_read_attributes(std::string& line)
{
if (kOutputAsBinary)
{
- Details::print_error_asm("Invalid directive in flat binary mode.", "ToolchainKit");
+ Details::print_error("Invalid directive in flat binary mode.", "ToolchainKit");
throw std::runtime_error("invalid_extern_segment_bin");
}
@@ -399,7 +399,7 @@ static bool asm_read_attributes(std::string& line)
if (name.size() == 0)
{
- Details::print_error_asm("Invalid extern_segment", "power-as");
+ Details::print_error("Invalid extern_segment", "power-as");
throw std::runtime_error("invalid_extern_segment");
}
@@ -462,7 +462,7 @@ static bool asm_read_attributes(std::string& line)
{
if (kOutputAsBinary)
{
- Details::print_error_asm("Invalid directive in flat binary mode.", "ToolchainKit");
+ Details::print_error("Invalid directive in flat binary mode.", "ToolchainKit");
throw std::runtime_error("invalid_public_segment_bin");
}
@@ -479,7 +479,7 @@ static bool asm_read_attributes(std::string& line)
if (std::find(kDefinedSymbols.begin(), kDefinedSymbols.end(), name) !=
kDefinedSymbols.end())
{
- Details::print_error_asm("Symbol already defined.", "ToolchainKit");
+ Details::print_error("Symbol already defined.", "ToolchainKit");
throw std::runtime_error("invalid_public_segment_bin");
}
@@ -543,7 +543,7 @@ static bool asm_read_attributes(std::string& line)
// \brief algorithms and helpers.
-namespace Details::Algorithms
+namespace Details::algorithm
{
// \brief authorize a brief set of characters.
static inline bool is_not_valid(char c)
@@ -561,7 +561,7 @@ namespace Details::Algorithms
{
return std::find_if(str.begin(), str.end(), is_not_valid) == str.end();
}
-} // namespace Details::Algorithms
+} // namespace Details::algorithm
/////////////////////////////////////////////////////////////////////////////////////////
@@ -586,7 +586,7 @@ std::string ToolchainKit::EncoderAMD64::CheckLine(std::string& line,
else
{
// now check the line for validity
- if (!Details::Algorithms::is_valid_amd64(line))
+ if (!Details::algorithm::is_valid_amd64(line))
{
err_str = "Line contains non valid characters.\nhere -> ";
err_str += line;
@@ -666,7 +666,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid hex number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid hex number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_hex");
}
}
@@ -696,7 +696,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid binary number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid binary number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -726,7 +726,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid octal number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid octal number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -919,7 +919,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber16(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid hex number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid hex number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_hex");
}
}
@@ -949,7 +949,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber16(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid binary number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid binary number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -979,7 +979,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber16(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid octal number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid octal number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -1051,7 +1051,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber8(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid hex number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid hex number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_hex");
}
}
@@ -1075,7 +1075,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber8(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid binary number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid binary number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -1099,7 +1099,7 @@ bool ToolchainKit::EncoderAMD64::WriteNumber8(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid octal number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid octal number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -1187,7 +1187,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
{
// strict check here
if (ToolchainKit::find_word(line, opcodeAMD64.fName) &&
- Details::Algorithms::is_valid_amd64(line))
+ Details::algorithm::is_valid_amd64(line))
{
foundInstruction = true;
std::string name(opcodeAMD64.fName);
@@ -1202,7 +1202,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
if (substr.find(",") == std::string::npos)
{
- Details::print_error_asm("Syntax error: missing right operand.", "ToolchainKit");
+ Details::print_error("Syntax error: missing right operand.", "ToolchainKit");
throw std::runtime_error("syntax_err");
}
@@ -1231,7 +1231,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
{
if (registerName[0] == 'r')
{
- Details::print_error_asm(
+ Details::print_error(
"invalid size for register, current bit width is: " +
std::to_string(kRegisterBitWidth),
file);
@@ -1286,7 +1286,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
{
if (hasRBasedRegs)
{
- Details::print_error_asm(
+ Details::print_error(
"Invalid combination of operands and registers.", "ToolchainKit");
throw std::runtime_error("comb_op_reg");
}
@@ -1323,7 +1323,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
if (currentRegList[1].fName[0] == 'r' &&
currentRegList[0].fName[0] == 'e')
{
- Details::print_error_asm("Invalid combination of operands and registers.",
+ Details::print_error("Invalid combination of operands and registers.",
"ToolchainKit");
throw std::runtime_error("comb_op_reg");
}
@@ -1331,7 +1331,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
if (currentRegList[0].fName[0] == 'r' &&
currentRegList[1].fName[0] == 'e')
{
- Details::print_error_asm("Invalid combination of operands and registers.",
+ Details::print_error("Invalid combination of operands and registers.",
"ToolchainKit");
throw std::runtime_error("comb_op_reg");
}
@@ -1341,7 +1341,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
if (currentRegList[0].fName[0] == 'r' ||
currentRegList[0].fName[0] == 'e')
{
- Details::print_error_asm("Invalid combination of operands and registers.",
+ Details::print_error("Invalid combination of operands and registers.",
"ToolchainKit");
throw std::runtime_error("comb_op_reg");
}
@@ -1349,7 +1349,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
if (currentRegList[1].fName[0] == 'r' ||
currentRegList[1].fName[0] == 'e')
{
- Details::print_error_asm("Invalid combination of operands and registers.",
+ Details::print_error("Invalid combination of operands and registers.",
"ToolchainKit");
throw std::runtime_error("comb_op_reg");
}
@@ -1359,7 +1359,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
if (currentRegList[0].fName[0] != 'r' ||
currentRegList[0].fName[0] == 'e')
{
- Details::print_error_asm("Invalid combination of operands and registers.",
+ Details::print_error("Invalid combination of operands and registers.",
"ToolchainKit");
throw std::runtime_error("comb_op_reg");
}
@@ -1367,7 +1367,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
if (currentRegList[1].fName[0] != 'r' ||
currentRegList[1].fName[0] == 'e')
{
- Details::print_error_asm("Invalid combination of operands and registers.",
+ Details::print_error("Invalid combination of operands and registers.",
"ToolchainKit");
throw std::runtime_error("comb_op_reg");
}
@@ -1413,7 +1413,7 @@ bool ToolchainKit::EncoderAMD64::WriteLine(std::string& line,
{
if (foundInstruction)
{
- Details::print_error_asm("Syntax error: " + line, "ToolchainKit");
+ Details::print_error("Syntax error: " + line, "ToolchainKit");
throw std::runtime_error("syntax_err");
}
diff --git a/dev/ToolchainKit/src/AssemblerPower.cc b/dev/ToolchainKit/src/AssemblerPower.cc
index b126848..6740630 100644
--- a/dev/ToolchainKit/src/AssemblerPower.cc
+++ b/dev/ToolchainKit/src/AssemblerPower.cc
@@ -24,7 +24,7 @@
#include <ToolchainKit/NFC/AE.h>
#include <ToolchainKit/Version.h>
#include <filesystem>
-#include <Algorithms>
+#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
@@ -171,7 +171,7 @@ TOOLCHAINKIT_MODULE(AssemblerMainPower64)
{
if (auto ln = asm64.CheckLine(line, argv[i]); !ln.empty())
{
- Details::print_error_asm(ln, argv[i]);
+ Details::print_error(ln, argv[i]);
continue;
}
@@ -185,7 +185,7 @@ TOOLCHAINKIT_MODULE(AssemblerMainPower64)
if (kVerbose)
{
std::string what = e.what();
- Details::print_warning_asm("exit because of: " + what, "ToolchainKit");
+ Details::print_warning("exit because of: " + what, "ToolchainKit");
}
std::filesystem::remove(object_output);
@@ -317,7 +317,7 @@ static bool asm_read_attributes(std::string& line)
{
if (kOutputAsBinary)
{
- Details::print_error_asm("Invalid extern_segment directive in flat binary mode.",
+ Details::print_error("Invalid extern_segment directive in flat binary mode.",
"ToolchainKit");
throw std::runtime_error("invalid_extern_segment_bin");
}
@@ -326,7 +326,7 @@ static bool asm_read_attributes(std::string& line)
if (name.size() == 0)
{
- Details::print_error_asm("Invalid extern_segment", "ToolchainKit");
+ Details::print_error("Invalid extern_segment", "ToolchainKit");
throw std::runtime_error("invalid_extern_segment");
}
@@ -389,7 +389,7 @@ static bool asm_read_attributes(std::string& line)
{
if (kOutputAsBinary)
{
- Details::print_error_asm("Invalid public_segment directive in flat binary mode.",
+ Details::print_error("Invalid public_segment directive in flat binary mode.",
"ToolchainKit");
throw std::runtime_error("invalid_public_segment_bin");
}
@@ -462,7 +462,7 @@ static bool asm_read_attributes(std::string& line)
// \brief algorithms and helpers.
-namespace Details::Algorithms
+namespace Details::algorithm
{
// \brief authorize a brief set of characters.
static inline bool is_not_alnum_space(char c)
@@ -477,7 +477,7 @@ namespace Details::Algorithms
{
return std::find_if(str.begin(), str.end(), is_not_alnum_space) == str.end();
}
-} // namespace Details::Algorithms
+} // namespace Details::algorithm
/////////////////////////////////////////////////////////////////////////////////////////
@@ -505,7 +505,7 @@ std::string ToolchainKit::EncoderPowerPC::CheckLine(std::string& line,
else
{
/// does the line contains valid input?
- if (!Details::Algorithms::is_valid_power64(line))
+ if (!Details::algorithm::is_valid_power64(line))
{
err_str = "Line contains non alphanumeric characters.\nhere -> ";
err_str += line;
@@ -515,7 +515,7 @@ std::string ToolchainKit::EncoderPowerPC::CheckLine(std::string& line,
return err_str;
}
- if (!Details::Algorithms::is_valid_power64(line))
+ if (!Details::algorithm::is_valid_power64(line))
{
err_str = "Line contains non alphanumeric characters.\nhere -> ";
err_str += line;
@@ -630,7 +630,7 @@ bool ToolchainKit::EncoderPowerPC::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid hex number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid hex number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_hex");
}
}
@@ -657,7 +657,7 @@ bool ToolchainKit::EncoderPowerPC::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid binary number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid binary number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -684,7 +684,7 @@ bool ToolchainKit::EncoderPowerPC::WriteNumber(const std::size_t& pos,
{
if (errno != 0)
{
- Details::print_error_asm("invalid octal number: " + jump_label, "ToolchainKit");
+ Details::print_error("invalid octal number: " + jump_label, "ToolchainKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -747,7 +747,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
{
if (ToolchainKit::find_word(line, "public_segment"))
return true;
- if (!Details::Algorithms::is_valid_power64(line))
+ if (!Details::algorithm::is_valid_power64(line))
return true;
for (auto& opcodePPC : kOpcodesPowerPC)
@@ -820,7 +820,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
isdigit(line[line_index + 2]))
{
reg_str += line[line_index + 3];
- Details::print_error_asm(
+ Details::print_error(
"invalid register index, r" + reg_str +
"\nnote: The POWER accepts registers from r0 to r32.",
file);
@@ -832,7 +832,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
if (reg_index > kAsmRegisterLimit)
{
- Details::print_error_asm("invalid register index, r" + reg_str,
+ Details::print_error("invalid register index, r" + reg_str,
file);
throw std::runtime_error("invalid_register_index");
}
@@ -858,7 +858,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
{
if (num.number[i] > 0)
{
- Details::print_warning_asm("number overflow on li operation.",
+ Details::print_warning("number overflow on li operation.",
file);
break;
}
@@ -948,7 +948,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
if (found_some_count > 3)
{
- Details::print_error_asm("Too much registers. -> " + line, file);
+ Details::print_error("Too much registers. -> " + line, file);
throw std::runtime_error("too_much_regs");
}
}
@@ -959,7 +959,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
if (found_some_count > 3)
{
- Details::print_error_asm("Too much registers. -> " + line, file);
+ Details::print_error("Too much registers. -> " + line, file);
throw std::runtime_error("too_much_regs");
}
}
@@ -980,7 +980,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
if (found_some_count > 1)
{
- Details::print_error_asm("Too much registers. -> " + line, file);
+ Details::print_error("Too much registers. -> " + line, file);
throw std::runtime_error("too_much_regs");
}
@@ -1052,7 +1052,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
{
if (register_count == 1)
{
- Details::print_error_asm("Too few registers. -> " + line, file);
+ Details::print_error("Too few registers. -> " + line, file);
throw std::runtime_error("too_few_registers");
}
}
@@ -1063,7 +1063,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
// remember! register to register!
if (found_some_count == 1)
{
- Details::print_error_asm(
+ Details::print_error(
"Unrecognized register found.\ntip: each AssemblerPower register "
"starts with 'r'.\nline: " +
line,
@@ -1075,7 +1075,7 @@ bool ToolchainKit::EncoderPowerPC::WriteLine(std::string& line,
if (found_some_count < 1 && name[0] != 'l' && name[0] != 's')
{
- Details::print_error_asm(
+ Details::print_error(
"invalid combination of opcode and registers.\nline: " + line,
file);
throw std::runtime_error("invalid_comb_op_reg");
diff --git a/dev/ToolchainKit/src/CCompiler64x0.cc b/dev/ToolchainKit/src/CCompiler64x0.cc
index 43cb648..e1a67ea 100644
--- a/dev/ToolchainKit/src/CCompiler64x0.cc
+++ b/dev/ToolchainKit/src/CCompiler64x0.cc
@@ -99,7 +99,7 @@ namespace Details
/// @brief prints an error into stdout.
/// @param reason the reason of the error.
/// @param file where does it originate from?
- void print_error_asm(std::string reason, std::string file) noexcept;
+ void print_error(std::string reason, std::string file) noexcept;
struct CompilerType final
{
@@ -402,7 +402,7 @@ bool CompilerFrontend64x0::Compile(const std::string text, const std::string fil
if (textBuffer[text_index] == '=' && kInStruct)
{
- Details::print_error_asm("assignement of value in struct " + textBuffer,
+ Details::print_error("assignement of value in struct " + textBuffer,
file);
continue;
}
@@ -1360,7 +1360,7 @@ public:
}
else
{
- Details::print_error_asm(err, src.data());
+ Details::print_error(err, src.data());
}
}
@@ -1567,7 +1567,7 @@ TOOLCHAINKIT_MODULE(NewOSCompilerCLang64x0)
std::string err = "Unknown command: ";
err += argv[index];
- Details::print_error_asm(err, "cc");
+ Details::print_error(err, "cc");
continue;
}
@@ -1580,7 +1580,7 @@ TOOLCHAINKIT_MODULE(NewOSCompilerCLang64x0)
{
if (kState.fVerbose)
{
- Details::print_error_asm(srcFile + " is not a valid C source.\n", "cc");
+ Details::print_error(srcFile + " is not a valid C source.\n", "cc");
}
return 1;
diff --git a/dev/ToolchainKit/src/CCompilerPower64.cc b/dev/ToolchainKit/src/CCompilerPower64.cc
index 8cd1a80..95fee28 100644
--- a/dev/ToolchainKit/src/CCompilerPower64.cc
+++ b/dev/ToolchainKit/src/CCompilerPower64.cc
@@ -90,7 +90,7 @@ namespace Details
/// @brief prints an error into stdout.
/// @param reason the reason of the error.
/// @param file where does it originate from?
- void print_error_asm(std::string reason, std::string file) noexcept;
+ void print_error(std::string reason, std::string file) noexcept;
struct CompilerType final
{
@@ -413,7 +413,7 @@ bool CompilerFrontendPower64::Compile(const std::string text, const std::string
if (textBuffer[text_index] == '=' && kInStruct)
{
- Details::print_error_asm(
+ Details::print_error(
"assignement of value inside a struct " + textBuffer, file);
continue;
}
@@ -1383,7 +1383,7 @@ public:
}
else
{
- Details::print_error_asm(err, src.data());
+ Details::print_error(err, src.data());
}
}
@@ -1587,7 +1587,7 @@ TOOLCHAINKIT_MODULE(NewOSCompilerCLangPowerPC)
std::string err = "Unknown command: ";
err += argv[index];
- Details::print_error_asm(err, "cc");
+ Details::print_error(err, "cc");
continue;
}
@@ -1600,7 +1600,7 @@ TOOLCHAINKIT_MODULE(NewOSCompilerCLangPowerPC)
{
if (kState.fVerbose)
{
- Details::print_error_asm(srcFile + " is not a valid C source.\n", "cc");
+ Details::print_error(srcFile + " is not a valid C source.\n", "cc");
}
return 1;
diff --git a/dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc b/dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc
index de29672..2ef9327 100644
--- a/dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc
+++ b/dev/ToolchainKit/src/CPlusPlusCompilerAMD64.cc
@@ -115,7 +115,7 @@ namespace Details
/// @brief prints an error into stdout.
/// @param reason the reason of the error.
/// @param file where does it originate from?
- void print_error_asm(std::string reason, std::string file) noexcept;
+ void print_error(std::string reason, std::string file) noexcept;
struct CompilerType final
{
@@ -277,7 +277,7 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
{
if (isalnum(text[i]))
{
- Details::print_error_asm("syntax error: " + text, file);
+ Details::print_error("syntax error: " + text, file);
return false;
}
}
@@ -378,11 +378,6 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
break;
}
case ToolchainKit::KeywordKind::eKeywordKindFunctionStart: {
- if (text.ends_with(";"))
- {
- break;
- }
-
for (auto& ch : text)
{
if (isdigit(ch))
@@ -394,20 +389,55 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
goto accept;
dont_accept:
- return true;
+ return false;
accept:
std::string fnName = text;
+ size_t indexFnName = 0;
- for (auto& ch : fnName)
+ // this one is for the type.
+ for (auto& ch : text)
{
+ ++indexFnName;
+
+ if (ch == '\t')
+ break;
+
if (ch == ' ')
- ch = '_';
+ break;
}
- syntax_tree.fUserValue = "public_segment .code64 __TOOLCHAINKIT_" + fnName + "\n";
+ fnName = text.substr(indexFnName);
+
+ if (text.ends_with(";"))
+ goto tk_write_assembly;
+ else if (text.size() <= indexFnName)
+ Details::print_error("Invalid function name: " + fnName, file);
+ indexFnName = 0;
+
+ for (auto& ch : fnName)
+ {
+ if (ch == ' ' ||
+ ch == '\t')
+ {
+ if (fnName[indexFnName - 1] != ')')
+ Details::print_error("Invalid function name: " + fnName, file);
+
+ if ((indexFnName + 1) != fnName.size())
+ Details::print_error("Extra characters after function name: " + fnName, file);
+ }
+
+ ++indexFnName;
+ }
+
+ syntax_tree.fUserValue = "public_segment .code64 __TOOLCHAINKIT_" + fnName + "\n";
++kFunctionEmbedLevel;
+
+ break;
+
+tk_write_assembly:
+ syntax_tree.fUserValue = "jmp __TOOLCHAINKIT_" + fnName + "\n";
}
case ToolchainKit::KeywordKind::eKeywordKindFunctionEnd: {
if (text.ends_with(";"))
@@ -580,7 +610,7 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
goto done;
}
- Details::print_error_asm("Variable not declared: " + varName, file);
+ Details::print_error("Variable not declared: " + varName, file);
return false;
}
@@ -696,7 +726,7 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
if (syntax_tree.fUserValue.empty())
{
- Details::print_error_asm("Variable not declared: " + varErrCpy, file);
+ Details::print_error("Variable not declared: " + varErrCpy, file);
}
break;
@@ -727,7 +757,7 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
if (syntax_tree.fUserValue.empty())
{
- Details::print_error_asm("Variable not declared: " + subText, file);
+ Details::print_error("Variable not declared: " + subText, file);
}
}
else
@@ -749,8 +779,10 @@ bool CompilerFrontendCPlusPlus::Compile(const std::string text,
}
}
default:
+ {
break;
}
+ }
syntax_tree.fUserData = keyword.first;
kState.fSyntaxTree->fLeafList.push_back(syntax_tree);
@@ -1015,7 +1047,7 @@ TOOLCHAINKIT_MODULE(CompilerCPlusPlusX8664)
std::string err = "Unknown option: ";
err += argv[index];
- Details::print_error_asm(err, "c++-drv");
+ Details::print_error(err, "c++-drv");
continue;
}
@@ -1040,13 +1072,13 @@ TOOLCHAINKIT_MODULE(CompilerCPlusPlusX8664)
{
if (kState.fVerbose)
{
- Details::print_error_asm(argv_i + " is not a valid C++ source.\n", "c++-drv");
+ Details::print_error(argv_i + " is not a valid C++ source.\n", "c++-drv");
}
return 1;
}
- std::cout << "CPlusPlusCompilerAMD64: building: " << argv[index] << std::endl;
+ std::cout << "CPlusPlusCompilerAMD64: Building: " << argv[index] << std::endl;
if (kFactory.Compile(argv_i, kMachine) != kExitOK)
return 1;
diff --git a/dev/ToolchainKit/src/CPlusPlusCompilerPreProcessor.cc b/dev/ToolchainKit/src/CPlusPlusCompilerPreProcessor.cc
index 232ebfb..3550a4c 100644
--- a/dev/ToolchainKit/src/CPlusPlusCompilerPreProcessor.cc
+++ b/dev/ToolchainKit/src/CPlusPlusCompilerPreProcessor.cc
@@ -11,7 +11,7 @@
#include <ToolchainKit/Parser.h>
#include <ToolchainKit/NFC/ErrorID.h>
-#include <Algorithms>
+#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
diff --git a/dev/ToolchainKit/src/CPlusPlusRuleChecker.cc b/dev/ToolchainKit/src/CPlusPlusRuleChecker.cc
deleted file mode 100644
index 671e224..0000000
--- a/dev/ToolchainKit/src/CPlusPlusRuleChecker.cc
+++ /dev/null
@@ -1,8 +0,0 @@
-/*
- * ========================================================
- *
- * CPlusPlusRuleChecker
- * Copyright (C) 2024 Theater Quality Inc, all rights reserved.
- *
- * ========================================================
- */
diff --git a/dev/ToolchainKit/src/Detail/AsmUtils.h b/dev/ToolchainKit/src/Detail/AsmUtils.h
index ff308d3..369e415 100644
--- a/dev/ToolchainKit/src/Detail/AsmUtils.h
+++ b/dev/ToolchainKit/src/Detail/AsmUtils.h
@@ -13,8 +13,8 @@ using namespace ToolchainKit;
namespace Details
{
- extern void print_error_asm(std::string reason, std::string file) noexcept;
- extern void print_warning_asm(std::string reason, std::string file) noexcept;
+ extern void print_error(std::string reason, std::string file) noexcept;
+ extern void print_warning(std::string reason, std::string file) noexcept;
} // namespace Details
/// @brief Get Number from lineBuffer.
@@ -37,7 +37,7 @@ static NumberCast32 GetNumber32(std::string lineBuffer, std::string numberKey)
{
if (errno != 0)
{
- Details::print_error_asm("invalid hex number: " + lineBuffer, "ToolchainKit");
+ Details::print_error("invalid hex number: " + lineBuffer, "ToolchainKit");
throw std::runtime_error("invalid_hex");
}
}
@@ -57,7 +57,7 @@ static NumberCast32 GetNumber32(std::string lineBuffer, std::string numberKey)
{
if (errno != 0)
{
- Details::print_error_asm("invalid binary number:" + lineBuffer, "ToolchainKit");
+ Details::print_error("invalid binary number:" + lineBuffer, "ToolchainKit");
throw std::runtime_error("invalid_bin");
}
}
@@ -77,7 +77,7 @@ static NumberCast32 GetNumber32(std::string lineBuffer, std::string numberKey)
{
if (errno != 0)
{
- Details::print_error_asm("invalid octal number: " + lineBuffer, "ToolchainKit");
+ Details::print_error("invalid octal number: " + lineBuffer, "ToolchainKit");
throw std::runtime_error("invalid_octal");
}
}
@@ -97,7 +97,7 @@ static NumberCast32 GetNumber32(std::string lineBuffer, std::string numberKey)
{
if (errno != 0)
{
- Details::print_error_asm("invalid hex number: " + lineBuffer, "ToolchainKit");
+ Details::print_error("invalid hex number: " + lineBuffer, "ToolchainKit");
throw std::runtime_error("invalid_hex");
}
}