Skip to content

Commit 60b8fe4

Browse files
committed
fix CompileAddr
1 parent e56616b commit 60b8fe4

File tree

5 files changed

+81
-19
lines changed

5 files changed

+81
-19
lines changed

source/backends/lua.d

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,14 +785,25 @@ class BackendLua : CompilerBackend {
785785
auto var = GetGlobal(name);
786786
auto extra = cast(GlobalExtra*) var.extra;
787787

788-
output ~= format("mem[dsp] = %d\n", extra.addr + offset);
789-
output ~= "dsp = dsp + 1\n";
788+
if (var.type.ptr) {
789+
output ~= format("mem[dsp] = mem[%d] + %d\n", extra.addr, offset);
790+
}
791+
else {
792+
output ~= format("mem[dsp] = %d\n", extra.addr + offset);
793+
}
790794
}
791795
else if (VariableExists(name)) {
792796
auto var = GetVariable(name);
793797

794-
output ~= format("mem[dsp] = vsp + %d\n", var.offset + offset);
795-
output ~= "dsp = dsp + 1";
798+
if (var.type.ptr) {
799+
writeln("HELLO!!!", var.offset, offset);
800+
output ~= format(
801+
"mem[dsp] = mem[vsp + %d] + %d\n", var.offset, offset
802+
);
803+
}
804+
else {
805+
output ~= format("mem[dsp] = vsp + %d\n", var.offset + offset);
806+
}
796807
}
797808
else {
798809
Error(node.error, "Variable '%s' does not exist", name);

source/backends/rm86.d

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,19 +889,34 @@ class BackendRM86 : CompilerBackend {
889889
if (GlobalExists(name)) {
890890
auto var = GetGlobal(name);
891891

892-
output ~= format(
893-
"lea ax, [__global_%s + %d]\n", name.Sanitise(), offset
894-
);
892+
if (var.type.ptr) {
893+
output ~= format("mov bx, [__global_%s]\n", name.Sanitise());
894+
output ~= format("lea ax, [bx + %d]\n", offset);
895+
}
896+
else {
897+
output ~= format(
898+
"lea ax, [__global_%s + %d]\n", name.Sanitise(), offset
899+
);
900+
}
901+
895902
output ~= "mov [si], ax\n";
896903
output ~= "add si, 2\n";
897904
}
898905
else if (VariableExists(name)) {
899906
auto var = GetVariable(name);
900907

901-
output ~= "mov ax, si\n";
902-
if (var.offset > 0) {
903-
output ~= format("add ax, %d\n", var.offset + offset);
908+
if (var.type.ptr) {
909+
output ~= "mov di, sp\n";
910+
output ~= format("mov bx, [di + %d]\n", var.offset);
911+
output ~= format("lea ax, [bx + %d]\n", offset);
904912
}
913+
else {
914+
output ~= "mov ax, sp\n";
915+
if (var.offset > 0) {
916+
output ~= format("add ax, %d\n", var.offset + offset);
917+
}
918+
}
919+
905920
output ~= "mov [si], ax\n";
906921
output ~= "add si, 2\n";
907922
}

source/backends/uxn.d

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,11 +819,27 @@ class BackendUXN : CompilerBackend {
819819

820820
if (GlobalExists(name)) {
821821
auto var = GetGlobal(name);
822-
output ~= format(";global_%s #%.4x ADD2\n", name.Sanitise(), offset);
822+
823+
if (var.type.ptr) {
824+
output ~= format(
825+
";global_%s LDA2 #%.4x ADD2\n", name.Sanitise(), offset
826+
);
827+
}
828+
else {
829+
output ~= format(";global_%s #%.4x ADD2\n", name.Sanitise(), offset);
830+
}
823831
}
824832
else if (VariableExists(name)) {
825833
auto var = GetVariable(name);
826-
output ~= format(".vsp LDZ2 #%.4x ADD2\n", var.offset + offset);
834+
835+
if (var.type.ptr) {
836+
output ~= format(
837+
".vsp LDZ2 #%.4x ADD2 LDA2 #%.4x ADD2\n", var.offset, offset
838+
);
839+
}
840+
else {
841+
output ~= format(".vsp LDZ2 #%.4x ADD2\n", var.offset + offset);
842+
}
827843
}
828844
else {
829845
Error(node.error, "Variable '%s' does not exist", name);

source/backends/x86_64.d

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,19 +1203,39 @@ class BackendX86_64 : CompilerBackend {
12031203
if (GlobalExists(name)) {
12041204
auto var = GetGlobal(name);
12051205

1206-
output ~= format(
1207-
"lea rax, qword [__global_%s + %d]\n", name.Sanitise(), offset
1208-
);
1206+
if (var.type.ptr) {
1207+
output ~= format("mov rax, [__global_%s]\n", name.Sanitise());
1208+
1209+
if (offset > 0) {
1210+
output ~= format("lea rax, [rax + %d]\n", offset);
1211+
}
1212+
}
1213+
else {
1214+
output ~= format(
1215+
"lea rax, qword [__global_%s + %d]\n", name.Sanitise(), offset
1216+
);
1217+
}
1218+
12091219
output ~= "mov [r15], rax\n";
12101220
output ~= "add r15, 8\n";
12111221
}
12121222
else if (VariableExists(name)) {
12131223
auto var = GetVariable(name);
12141224

1215-
output ~= "mov rdi, rsp\n";
1216-
if (var.offset > 0) {
1217-
output ~= format("add rdi, %d\n", var.offset + offset);
1225+
if (var.type.ptr) {
1226+
output ~= format("mov rdi, [rsp + %d]\n", var.offset);
1227+
1228+
if (offset > 0) {
1229+
output ~= format("lea rdi, [rdi + %d]\n", offset);
1230+
}
12181231
}
1232+
else {
1233+
output ~= "mov rdi, rsp\n";
1234+
if (var.offset > 0) {
1235+
output ~= format("add rdi, %d\n", var.offset + offset);
1236+
}
1237+
}
1238+
12191239
output ~= "mov [r15], rdi\n";
12201240
output ~= "add r15, 8\n";
12211241
}

0 commit comments

Comments
 (0)