commit 311546ad7061499199658d915ab9edcb4df146a9
parent 62727304ba2258d24d069146c6f1859ca8eebdfc
Author: erai <erai@omiltem.net>
Date: Sat, 16 Mar 2024 11:39:45 -0400
argc, argv, envp
Diffstat:
M | cc0.c | | | 35 | +++++++++++++++++++++++++++++++++-- |
M | cc1.c | | | 41 | ++++++++++++++++++++++++++++++++++++----- |
M | cc2 | | | 0 | |
3 files changed, 69 insertions(+), 7 deletions(-)
diff --git a/cc0.c b/cc0.c
@@ -3478,9 +3478,40 @@ emit_pop(int n)
}
void
-emit_preamble(int n)
+emit_preamble(int n, int start)
{
int i;
+ if (start) {
+ // xor rbp, rbp
+ emit(0x48);
+ emit(0x31);
+ emit(0xed);
+ // mov rdi, [rsp]
+ emit(0x48);
+ emit(0x8b);
+ emit(0x3c);
+ emit(0x24);
+ // lea rsi, [rsp + 8]
+ emit(0x48);
+ emit(0x8d);
+ emit(0x74);
+ emit(0x24);
+ emit(0x08);
+ // lea rdx, [rsi + rdi * 8 + 8]
+ emit(0x48);
+ emit(0x8d);
+ emit(0x54);
+ emit(0xfe);
+ emit(0x08);
+ // push rdx
+ emit(0x52);
+ // push rsi
+ emit(0x56);
+ // push rdi
+ emit(0x57);
+ // push rbp
+ emit(0x55);
+ }
// push rbp
emit(0x55);
// mov rbp, rsp
@@ -4651,7 +4682,7 @@ tfunc(struct decl *d)
fixup_label(d->label);
- emit_preamble(d->preamble);
+ emit_preamble(d->preamble, !cmp(d->name, (unsigned char *)"_start"));
tstmt(d->body);
diff --git a/cc1.c b/cc1.c
@@ -214,8 +214,8 @@ exit(n: int): void {
syscall(60, n, 0, 0, 0, 0, 0);
}
-_start(): void {
- main();
+_start(argc: int, argv: **byte, envp: **byte): void {
+ main(argc, argv, envp);
exit(0);
}
@@ -2441,7 +2441,7 @@ compile_func(c: *compiler, d: *decl): void {
// Compile the function body
emit_str(c, d.name);
fixup_label(c, d.func_label);
- emit_preamble(c, offset);
+ emit_preamble(c, offset, !strcmp(d.name, "_start"));
compile_stmt(c, d, d.func_def.b, 0:*label, 0:*label);
emit_num(c, 0);
emit_ret(c);
@@ -3679,8 +3679,39 @@ emit_pop(c: *compiler, n: int): void {
emit(c, n >> 24);
}
-emit_preamble(c: *compiler, n: int): void {
+emit_preamble(c: *compiler, n: int, start: int): void {
var i: int;
+ if (start) {
+ // xor rbp, rbp
+ emit(c, 0x48);
+ emit(c, 0x31);
+ emit(c, 0xed);
+ // mov rdi, [rsp]
+ emit(c, 0x48);
+ emit(c, 0x8b);
+ emit(c, 0x3c);
+ emit(c, 0x24);
+ // lea rsi, [rsp + 8]
+ emit(c, 0x48);
+ emit(c, 0x8d);
+ emit(c, 0x74);
+ emit(c, 0x24);
+ emit(c, 0x08);
+ // lea rdx, [rsi + rdi * 8 + 8]
+ emit(c, 0x48);
+ emit(c, 0x8d);
+ emit(c, 0x54);
+ emit(c, 0xfe);
+ emit(c, 0x08);
+ // push rdx
+ emit(c, 0x52);
+ // push rsi
+ emit(c, 0x56);
+ // push rdi
+ emit(c, 0x57);
+ // push rbp
+ emit(c, 0x55);
+ }
// push rbp
emit(c, 0x55);
// mov rbp, rsp
@@ -4382,7 +4413,7 @@ writeout(c: *compiler): void {
}
}
-main(): void {
+main(argc: int, argv: **byte, envp: **byte): void {
var c: compiler;
var p: *node;
comp_setup(&c);
diff --git a/cc2 b/cc2
Binary files differ.