Skip to content

Commit fd68c0c

Browse files
committed
properly fixed exceptions on x86_64
1 parent a747bd6 commit fd68c0c

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

source/backends/arm64.d

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ private struct Word {
2626
Node[] inlineNodes;
2727
bool error;
2828
Type[] params;
29-
size_t numReturns;
30-
29+
3130
// for C words
3231
Type ret;
3332
bool isVoid;
@@ -583,8 +582,7 @@ class BackendARM64 : CompilerBackend {
583582
}
584583

585584
words[node.name] = Word(
586-
WordType.Callisto, true, node.nodes, node.errors, params,
587-
node.returnTypes.length
585+
WordType.Callisto, true, node.nodes, node.errors, params
588586
);
589587
}
590588
else {
@@ -593,7 +591,7 @@ class BackendARM64 : CompilerBackend {
593591

594592
words[node.name] = Word(
595593
node.raw? WordType.Raw : WordType.Callisto , false, [], node.errors,
596-
params, node.returnTypes.length
594+
params,
597595
);
598596

599597
string symbol =
@@ -1234,8 +1232,10 @@ class BackendARM64 : CompilerBackend {
12341232
Error(node.error, "Non-callisto functions can't throw");
12351233
}
12361234

1237-
if (word.params.length > 0) {
1238-
output ~= format("sub x21, x19, #%d\n", word.params.length * 8);
1235+
size_t paramSize = word.params.length * 8;
1236+
1237+
if (paramSize != 0) {
1238+
output ~= format("sub x21, x19, #%d\n", paramSize);
12391239
output ~= "str x21, [x20, #-8]!\n";
12401240
}
12411241
else {
@@ -1251,7 +1251,7 @@ class BackendARM64 : CompilerBackend {
12511251
output ~= format("bl __func__%s\n", node.func.Sanitise());
12521252
}
12531253

1254-
output ~= "ldr x21, [x20], #8\n";
1254+
output ~= "ldr x19, [x20], #8\n";
12551255

12561256
++ blockCounter;
12571257

source/backends/rm86.d

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ private struct Word {
1717
Node[] inlineNodes;
1818
bool error;
1919
Type[] params;
20-
size_t numReturns;
21-
22-
int StackEffect() => (-(cast(int) params.length) + (cast(int) numReturns));
2320
}
2421

2522
private struct RM86Opts {
@@ -282,20 +279,26 @@ class BackendRM86 : CompilerBackend {
282279
}
283280
else {
284281
if (word.raw) {
285-
int stackEffect = word.StackEffect() * 8;
286-
287-
if (stackEffect != 0) {
288-
output ~= format("lea ax, [si + %d]\n", stackEffect);
289-
output ~= "push ax\n";
290-
}
291-
else {
292-
output ~= "push si\n";
293-
}
294-
295282
output ~= format("call %s\n", node.name);
296283
}
297284
else {
285+
if (words[thisFunc].error) {
286+
size_t paramSize = word.params.length * 2;
287+
288+
if (paramSize != 0) {
289+
output ~= format("lea ax, [si - %d]\n", paramSize);
290+
output ~= "push ax\n";
291+
}
292+
else {
293+
output ~= "push si\n";
294+
}
295+
}
296+
298297
output ~= format("call __func__%s\n", node.name.Sanitise());
298+
299+
if (words[thisFunc].error) {
300+
output ~= "pop r15\n";
301+
}
299302
}
300303
}
301304

@@ -1022,7 +1025,7 @@ class BackendRM86 : CompilerBackend {
10221025
output ~= format("call __func__%s\n", node.func.Sanitise());
10231026
}
10241027

1025-
output ~= "pop di\n";
1028+
output ~= "pop si\n";
10261029

10271030
output ~= format("mov ax, [__global_%s]\n", Sanitise("_cal_exception"));
10281031
output ~= "cmp ax, 0\n";

source/backends/x86_64.d

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ private struct Word {
2525
Node[] inlineNodes;
2626
bool error;
2727
Type[] params;
28-
size_t numReturns;
2928

3029
// for C words
3130
Type ret;
3231
bool isVoid;
3332
string symbolName;
34-
35-
int StackEffect() => (-(cast(int) params.length) + (cast(int) numReturns));
3633
}
3734

3835
class BackendX86_64 : CompilerBackend {
@@ -525,7 +522,23 @@ class BackendX86_64 : CompilerBackend {
525522
}
526523
}
527524
else {
525+
if (words[thisFunc].error) {
526+
size_t paramSize = word.params.length * 8;
527+
528+
if (paramSize != 0) {
529+
output ~= format("lea r14, [r15 - %d]\n", paramSize);
530+
output ~= "push r14\n";
531+
}
532+
else {
533+
output ~= "push r15\n";
534+
}
535+
}
536+
528537
output ~= format("call __func__%s\n", node.name.Sanitise());
538+
539+
if (words[thisFunc].error) {
540+
output ~= "pop r15\n";
541+
}
529542
}
530543
}
531544

@@ -622,7 +635,7 @@ class BackendX86_64 : CompilerBackend {
622635
}
623636

624637
words[node.name] = Word(
625-
WordType.Callisto, true, node.nodes, node.errors, params, node.returnTypes.length
638+
WordType.Callisto, true, node.nodes, node.errors, params
626639
);
627640
}
628641
else {
@@ -631,7 +644,7 @@ class BackendX86_64 : CompilerBackend {
631644

632645
words[node.name] = Word(
633646
node.raw? WordType.Raw : WordType.Callisto , false, [], node.errors,
634-
params, node.returnTypes.length
647+
params
635648
);
636649

637650
string symbol =
@@ -1290,10 +1303,10 @@ class BackendX86_64 : CompilerBackend {
12901303
Error(node.error, "Non-callisto functions can't throw");
12911304
}
12921305

1293-
int stackEffect = word.StackEffect() * 8;
1306+
size_t paramSize = word.params.length * 8;
12941307

1295-
if (stackEffect != 0) {
1296-
output ~= format("lea r14, [r15 + %d]\n", stackEffect);
1308+
if (paramSize != 0) {
1309+
output ~= format("lea r14, [r15 - %d]\n", paramSize);
12971310
output ~= "push r14\n";
12981311
}
12991312
else {
@@ -1309,7 +1322,7 @@ class BackendX86_64 : CompilerBackend {
13091322
output ~= format("call __func__%s\n", node.func.Sanitise());
13101323
}
13111324

1312-
output ~= "pop r15\n";
1325+
output ~= "pop r14\n";
13131326

13141327
++ blockCounter;
13151328

@@ -1318,7 +1331,7 @@ class BackendX86_64 : CompilerBackend {
13181331
output ~= format("je __catch_%d_end\n", blockCounter);
13191332

13201333
// function errored, assume that all it did was consume parameters
1321-
output ~= format("lea r15, [r15 - %d]\n", stackEffect);
1334+
output ~= "mov r15, r14\n";
13221335

13231336
// create scope
13241337
auto oldVars = variables.dup;

0 commit comments

Comments
 (0)