Skip to content

Commit 4187f63

Browse files
committed
fix C externs
1 parent e09d9ed commit 4187f63

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

examples/c_interop.cal

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include "cores/select.cal"
2+
include "std/io.cal"
3+
include "std/nullterm.cal"
4+
5+
extern C i32 puts addr end
6+
7+
"Hello!\0" Array.elements + @ puts drop

source/backends/linux86.d

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -327,30 +327,36 @@ class BackendLinux86 : CompilerBackend {
327327
}
328328
else if (word.type == WordType.C) {
329329
if (word.params.length >= 1) {
330-
output ~= "sub r15, 8\n";
331-
output ~= "mov rdi, [r15]\n";
330+
output ~= format(
331+
"mov rdi, [r15 - %d]\n", word.params.length * 8
332+
);
332333
}
333334
if (word.params.length >= 2) {
334-
output ~= "sub r15, 8\n";
335-
output ~= "mov rsi, [r15]\n";
335+
output ~= format(
336+
"mov rsi, [r15 - %d]\n", (word.params.length - 1) * 8
337+
);
336338
}
337339
if (word.params.length >= 3) {
338-
output ~= "sub r15, 8\n";
339-
output ~= "mov rdx, [r15]\n";
340+
output ~= format(
341+
"mov rdx, [r15 - %d]\n", (word.params.length - 2) * 8
342+
);
340343
}
341344
if (word.params.length >= 4) {
342-
output ~= "sub r15, 8\n";
343-
output ~= "mov rcx, [r15]\n";
345+
output ~= format(
346+
"mov rcx, [r15 - %d]\n", (word.params.length - 3) * 8
347+
);
344348
}
345349
if (word.params.length >= 5) {
346-
output ~= "sub r15, 8\n";
347-
output ~= "mov r8, [r15]\n";
350+
output ~= format(
351+
"mov r8, [r15 - %d]\n", (word.params.length - 4) * 8
352+
);
348353
}
349354
if (word.params.length >= 6) {
350-
output ~= "sub r15, 8\n";
351-
output ~= "mov r9, [r15]\n";
355+
output ~= format(
356+
"mov r9, [r15 - %d]\n", (word.params.length - 5) * 8
357+
);
352358
}
353-
359+
output ~= format("sub r15, %d\n", word.params.length * 8);
354360

355361
output ~= format("call %s\n", node.name);
356362

0 commit comments

Comments
 (0)