Skip to content

Commit

Permalink
linux86: command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
yeti0904 committed Jun 16, 2024
1 parent ee4c0bc commit 56359b5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion source/backends/linux86.d
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class BackendLinux86 : CompilerBackend {
foreach (name, ref type ; types) {
NewConst(format("%s.sizeof", name), cast(long) type.size);
}

globals["__linux86_argv"] = Global(types["addr"], false, 0);
globals["__linux86_argc"] = Global(types["cell"], false, 0);
}

override void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
Expand Down Expand Up @@ -136,7 +139,7 @@ class BackendLinux86 : CompilerBackend {
// platform
"Linux86", "Linux", "LittleEndian", "16Bit", "32Bit", "64Bit",
// features
"IO", "Exit", "Time", "File"
"IO", "Exit", "Time", "File", "Args"
];

override string[] FinalCommands() {
Expand Down Expand Up @@ -179,6 +182,12 @@ class BackendLinux86 : CompilerBackend {
output ~= "section .text\n";
output ~= "_start:\n";

// get argv and argc
output ~= "mov rsi, [rsp + 8]\n";
output ~= format("mov [__global_%s], rsi\n", Sanitise("__linux86_argv"));
output ~= "mov rsi, [rsp]\n";
output ~= format("mov [__global_%s], rsi\n", Sanitise("__linux86_argc"));

// allocate data stack
output ~= "sub rsp, 4096\n"; // 512 cells
output ~= "mov r15, rsp\n";
Expand Down

0 comments on commit 56359b5

Please sign in to comment.