summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-13 16:08:44 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-13 16:09:45 +0100
commit2a97666a74018379396d12dfae09cf10a6231a20 (patch)
treeac87971c7a5dc3c129b688fbfe293a65feb3891a
parentb11aa11199cfe873946bc032e031f139348eafd6 (diff)
64asm/64ld: Use origin offset when not specify 'import' and if symbol is
known. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com> Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
-rw-r--r--64x0-Drivers/64asm.cc80
-rw-r--r--64x0-Drivers/64ld.cc42
-rw-r--r--64x0-Drivers/bin/Source/decrement_until_zero.64x20
-rw-r--r--CompilerKit/AsmKit/Arch/64k.hpp8
4 files changed, 84 insertions, 66 deletions
diff --git a/64x0-Drivers/64asm.cc b/64x0-Drivers/64asm.cc
index 8c5cd53..e82585f 100644
--- a/64x0-Drivers/64asm.cc
+++ b/64x0-Drivers/64asm.cc
@@ -244,7 +244,7 @@ MODULE(Assembler64x0)
for (auto &rec : kRecords)
{
if (kVerbose)
- kStdOut << "64asm: wrote record " << rec.fName << " to file...\n";
+ kStdOut << "64asm: Wrote record " << rec.fName << " to file...\n";
rec.fFlags |= CompilerKit::kKindRelocationAtRuntime;
rec.fOffset = record_count;
@@ -261,7 +261,7 @@ MODULE(Assembler64x0)
CompilerKit::AERecordHeader _record_hdr{0};
if (kVerbose)
- kStdOut << "64asm: wrote symbol " << sym << " to file...\n";
+ kStdOut << "64asm: Wrote symbol " << sym << " to file...\n";
_record_hdr.fKind = kAEInvalidOpcode;
_record_hdr.fSize = sym.size();
@@ -295,13 +295,13 @@ MODULE(Assembler64x0)
}
if (kVerbose)
- kStdOut << "64asm: wrote program bytes to file...\n";
+ kStdOut << "64asm: Wrote program bytes to file...\n";
file_ptr_out.flush();
file_ptr_out.close();
if (kVerbose)
- kStdOut << "64asm: exit succeeded with code 0.\n";
+ kStdOut << "64asm: Exit succeeded with code 0.\n";
return 0;
}
@@ -309,7 +309,7 @@ MODULE(Assembler64x0)
asm_fail_exit:
if (kVerbose)
- kStdOut << "64asm: exit failed with code -1.\n";
+ kStdOut << "64asm: Exit failed with code -1.\n";
return -1;
}
@@ -479,23 +479,16 @@ namespace detail::algorithm
/////////////////////////////////////////////////////////////////////////////////////////
-std::string CompilerKit::PlatformAssembler64x0::CheckLine(std::string &line, const std::string &file)
+std::string CompilerKit::PlatformAssembler64x0::CheckLine(std::string& line, const std::string &file)
{
- (void)file;
-
std::string err_str;
- while (line.find('\t') != std::string::npos)
- 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"))
+ ParserKit::find_word(line, ";"))
{
-
if (line.find('#') != std::string::npos)
{
line.erase(line.find('#'));
@@ -612,9 +605,6 @@ std::string CompilerKit::PlatformAssembler64x0::CheckLine(std::string &line, con
}
}
- err_str += "unknown syntax: ";
- err_str += line;
-
return err_str;
}
@@ -801,7 +791,7 @@ bool CompilerKit::PlatformAssembler64x0::WriteLine(std::string &line, const std:
isdigit(line[line_index + 2]))
{
reg_str += line[line_index + 3];
- detail::print_error("invalid register index, r" + reg_str + "\nnote: 64x0 accepts registers from r0 to r20.", file);
+ detail::print_error("invalid register index, r" + reg_str + "\nnote: The 64x0 accepts registers from r0 to r20.", file);
throw std::runtime_error("invalid_register_index");
}
}
@@ -823,12 +813,8 @@ bool CompilerKit::PlatformAssembler64x0::WriteLine(std::string &line, const std:
if (kVerbose)
{
- if (kOutputArch == CompilerKit::kPefArch64000)
- kStdOut << "64asm: 64x0 register found: " << register_syntax << "\n";
- else
- kStdOut << "64asm: register found: " << register_syntax << "\n";
-
- kStdOut << "64asm: Number of registers: " << found_some << "\n";
+ kStdOut << "64asm: Register found: " << register_syntax << "\n";
+ kStdOut << "64asm: Register amount in instruction: " << found_some << "\n";
}
}
}
@@ -839,7 +825,7 @@ bool CompilerKit::PlatformAssembler64x0::WriteLine(std::string &line, const std:
// remember! register to register!
if (found_some == 1)
{
- detail::print_error("unrecognized register found.\ntip: each 64asm register starts with 'r'.\nline: " + line, file);
+ detail::print_error("Unrecognized register found.\ntip: each 64asm register starts with 'r'.\nline: " + line, file);
throw std::runtime_error("not_a_register");
}
}
@@ -961,14 +947,32 @@ bool CompilerKit::PlatformAssembler64x0::WriteLine(std::string &line, const std:
}
// This is the case where we jump to a label, it is also used as a goto.
- if (name == "jb")
+ if (name == "jb" ||
+ name == "lda" ||
+ name == "sta")
{
asm_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 &&
- name == "jb")
+ if (cpy_jump_label.find("import") != std::string::npos)
+ {
+ if (name == "sta")
+ {
+ detail::print_error("import is not allowed on a sta operation.", file);
+ throw std::runtime_error("import_sta_op");
+ }
+ else
+ {
+ goto asm_end_label_cpy;
+ }
+
+ cpy_jump_label.erase(cpy_jump_label.find("import"), strlen("import"));
+ }
+
+ if (name == "jb" ||
+ name == "lda" ||
+ name == "sta")
{
for (auto &label : kOriginLabel)
{
@@ -976,7 +980,7 @@ asm_write_label:
{
if (kVerbose)
{
- kStdOut << "64asm: verbose: set label "
+ kStdOut << "64asm: Replace label "
<< cpy_jump_label
<< " to address: "
<< label.second
@@ -995,19 +999,9 @@ asm_write_label:
}
}
- detail::print_error("import not found on jump label, please add one.", file);
+ detail::print_error(cpy_jump_label + " 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);
- throw std::runtime_error("import_sta_op");
- }
-
- cpy_jump_label.erase(cpy_jump_label.find("import"), strlen("import"));
- }
if (cpy_jump_label.size() < 1)
{
@@ -1052,4 +1046,4 @@ asm_end_label_cpy:
return true;
}
-// Last rev 8-1-24 \ No newline at end of file
+// Last rev 13-1-24 \ No newline at end of file
diff --git a/64x0-Drivers/64ld.cc b/64x0-Drivers/64ld.cc
index bc98837..2aa7e96 100644
--- a/64x0-Drivers/64ld.cc
+++ b/64x0-Drivers/64ld.cc
@@ -134,7 +134,7 @@ MODULE(Linker64x0)
{
if (argv[i][0] == '-')
{
- kStdOut << "ld: unknown flag: " << argv[i] << "\n";
+ kStdOut << "64ld: unknown flag: " << argv[i] << "\n";
return -CXXKIT_EXEC_ERROR;
}
@@ -147,7 +147,7 @@ MODULE(Linker64x0)
// sanity check.
if (kObjectList.empty())
{
- kStdOut << "ld: no input files." << std::endl;
+ kStdOut << "64ld: no input files." << std::endl;
return CXXKIT_EXEC_ERROR;
}
else
@@ -159,7 +159,7 @@ MODULE(Linker64x0)
{
// if filesystem doesn't find file
// -> throw error.
- kStdOut << "ld: no such file: " << obj << std::endl;
+ kStdOut << "64ld: no such file: " << obj << std::endl;
return CXXKIT_EXEC_ERROR;
}
}
@@ -168,7 +168,7 @@ MODULE(Linker64x0)
// PEF expects a valid architecture when outputing a binary.
if (kArch == 0)
{
- kStdOut << "ld: no target architecture set, can't continue." << std::endl;
+ kStdOut << "64ld: no target architecture set, can't continue." << std::endl;
return CXXKIT_EXEC_ERROR;
}
@@ -196,7 +196,7 @@ MODULE(Linker64x0)
{
if (kVerbose)
{
- kStdOut << "ld: error: " << strerror(errno) << "\n";
+ kStdOut << "64ld: error: " << strerror(errno) << "\n";
}
return -CXXKIT_FILE_NOT_FOUND;
@@ -222,14 +222,14 @@ MODULE(Linker64x0)
if (ae_header.fArch != kArch)
{
if (kVerbose)
- kStdOut << "ld: pef: is a fat binary? : ";
+ kStdOut << "64ld: pef: is a fat binary? : ";
if (!kFatBinaryEnable)
{
if (kVerbose)
kStdOut << "no.\n";
- kStdOut << "ld: error: object " << i << " is a different kind of architecture and output isn't treated as FAT binary." << std::endl;
+ kStdOut << "64ld: error: object " << i << " is a different kind of architecture and output isn't treated as FAT binary." << std::endl;
std::remove(kOutput.c_str());
return -CXXKIT_FAT_ERROR;
@@ -252,7 +252,7 @@ MODULE(Linker64x0)
std::size_t cnt = ae_header.fCount;
if (kVerbose)
- kStdOut << "ld: object header found, record count: " << cnt << "\n";
+ kStdOut << "64ld: object header found, record count: " << cnt << "\n";
pef_container.Count = cnt;
@@ -301,7 +301,7 @@ ld_mark_header:
command_header.Size = ae_records[ae_record_index].fSize;
if (kVerbose)
- kStdOut << "ld: object record: " << ae_records[ae_record_index].fName << " was marked.\n";
+ kStdOut << "64ld: object record: " << ae_records[ae_record_index].fName << " was marked.\n";
pef_command_hdrs.emplace_back(command_header);
}
@@ -322,7 +322,7 @@ ld_mark_header:
continue;
}
- kStdOut << "ld: not an object: " << i << std::endl;
+ kStdOut << "64ld: not an object: " << i << std::endl;
std::remove(kOutput.c_str());
// don't continue, it is a fatal error.
@@ -335,7 +335,7 @@ ld_mark_header:
if (kVerbose)
{
- kStdOut << "ld: pef: wrote container header.\n";
+ kStdOut << "64ld: pef: wrote container header.\n";
}
output_fc.seekp(std::streamsize(pef_container.HdrSz));
@@ -354,7 +354,7 @@ ld_mark_header:
std::string::npos)
{
if (kVerbose)
- kStdOut << "ld: found undefined symbol: " << pef_command_hdr.Name << "\n";
+ kStdOut << "64ld: found undefined symbol: " << pef_command_hdr.Name << "\n";
if (auto it = std::find(not_found.begin(), not_found.end(), std::string(pef_command_hdr.Name));
it == not_found.end())
@@ -405,7 +405,7 @@ ld_mark_header:
not_found.erase(it);
if (kVerbose)
- kStdOut << "ld: found symbol: " << pef_command_hdr.Name << "\n";
+ kStdOut << "64ld: found symbol: " << pef_command_hdr.Name << "\n";
break;
}
@@ -421,9 +421,9 @@ ld_continue_search:
if (!kStartFound && is_executable)
{
if (kVerbose)
- kStdOut << "ld: undefined symbol: __start, you may have forget to link against your runtime library.\n";
+ kStdOut << "64ld: undefined symbol: __start, you may have forget to link against your runtime library.\n";
- kStdOut << "ld: undefined entrypoint " << kPefStart << " for executable " << kOutput << "\n";
+ kStdOut << "64ld: undefined entrypoint " << kPefStart << " for executable " << kOutput << "\n";
}
// step 4: write some pef commands.
@@ -521,7 +521,7 @@ ld_continue_search:
}
if (kVerbose)
- kStdOut << "ld: found duplicate symbol: " << pef_command_hdr.Name << "\n";
+ kStdOut << "64ld: found duplicate symbol: " << pef_command_hdr.Name << "\n";
kDuplicateSymbols = true;
}
@@ -532,7 +532,7 @@ ld_continue_search:
{
for (auto& symbol : duplicate_symbols)
{
- kStdOut << "ld: multiple symbols of " << symbol << ".\n";
+ kStdOut << "64ld: multiple symbols of " << symbol << ".\n";
}
std::remove(kOutput.c_str());
@@ -547,7 +547,7 @@ ld_continue_search:
}
if (kVerbose)
- kStdOut << "ld: wrote code for: " << kOutput << "\n";
+ kStdOut << "64ld: wrote code for: " << kOutput << "\n";
// step 3: check if we have those symbols
@@ -566,7 +566,7 @@ ld_continue_search:
{
for (auto& unreferenced_symbol : unreferenced_symbols)
{
- kStdOut << "ld: undefined symbol " << unreferenced_symbol << "\n";
+ kStdOut << "64ld: undefined symbol " << unreferenced_symbol << "\n";
}
}
@@ -576,7 +576,7 @@ ld_continue_search:
!unreferenced_symbols.empty())
{
if (kVerbose)
- kStdOut << "ld: code for: " << kOutput << ", is corrupt, removing file...\n";
+ kStdOut << "64ld: code for: " << kOutput << ", is corrupt, removing file...\n";
std::remove(kOutput.c_str());
return -CXXKIT_EXEC_ERROR;
@@ -585,4 +585,4 @@ ld_continue_search:
return 0;
}
-// Last rev 8-1-24 \ No newline at end of file
+// Last rev 13-1-24 \ No newline at end of file
diff --git a/64x0-Drivers/bin/Source/decrement_until_zero.64x b/64x0-Drivers/bin/Source/decrement_until_zero.64x
new file mode 100644
index 0000000..f342396
--- /dev/null
+++ b/64x0-Drivers/bin/Source/decrement_until_zero.64x
@@ -0,0 +1,20 @@
+;; org: 0
+
+export .text dec_until_zero_return
+ pla
+ jlr
+
+export .text dec_until_zero
+ pha
+ ldw r2, 10
+ ldw r1, 1
+ lda r3, dec_until_zero_return
+export .text dec_until_zero_loop
+ dec r2, r1
+ beq r2, r0, r3
+ jb dec_until_zero_loop
+
+export .text __start
+ jb dec_until_zero
+ jrl
+ jlr \ No newline at end of file
diff --git a/CompilerKit/AsmKit/Arch/64k.hpp b/CompilerKit/AsmKit/Arch/64k.hpp
index b54161d..b070332 100644
--- a/CompilerKit/AsmKit/Arch/64k.hpp
+++ b/CompilerKit/AsmKit/Arch/64k.hpp
@@ -56,7 +56,8 @@ inline std::vector<CpuCode64x0> kOpcodes64x0 = {
kAsmOpcodeDecl("addc", 0b0101011, 0b110, kAsmImmediate)
kAsmOpcodeDecl("decc", 0b0101011, 0b111, kAsmImmediate)
kAsmOpcodeDecl("int", 0b1110011, 0b00, kAsmSyscall)
- kAsmOpcodeDecl("syscall", 0b1110011, 0b00, kAsmSyscall)
+ kAsmOpcodeDecl("sysenter", 0b1110011, 0b01, kAsmSyscall)
+ kAsmOpcodeDecl("sysexit", 0b1110011, 0b10, kAsmSyscall)
kAsmOpcodeDecl("pha", 0b1110011, 0b00, kAsmNoArgs)
kAsmOpcodeDecl("pla", 0b1110011, 0b01, kAsmNoArgs)
};
@@ -69,6 +70,9 @@ inline std::vector<CpuCode64x0> kOpcodes64x0 = {
#define kAsmFloatRegisterPrefix "f"
#define kAsmFloatRegisterLimit 10
+#define kAsmFloatZeroRegister 0
+#define kAsmZeroRegister 0
+
#define kAsmRegisterPrefix "r"
#define kAsmRegisterLimit 20
#define kAsmPcRegister 17
@@ -80,7 +84,7 @@ inline std::vector<CpuCode64x0> kOpcodes64x0 = {
/////////////////////////////////////////////////////////////////////////////
-// SYSTEM CALL ADDRESSING
+// SYSTEM CALL/JUMP ADDRESSING
// | OPCODE | FUNCT3 | FUNCT7 | OFF |