Skip to content

Commit eaf4098

Browse files
committed
fix the compiler not erroring when you try to push an array/structure value inside a structure
1 parent 199333d commit eaf4098

File tree

6 files changed

+69
-7
lines changed

6 files changed

+69
-7
lines changed

source/backends/arm64.d

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ class BackendARM64 : CompilerBackend {
571571
string name = node.name[0 .. node.name.countUntil(".")];
572572
auto structVar = GetStructVariable(node, node.name);
573573

574+
if (structVar.structure) {
575+
Error(node.error, "Can't push the value of an array or structure");
576+
}
577+
574578
if (GlobalExists(name)) {
575579
auto var = GetGlobal(name);
576580
PushGlobalValue(node, var, structVar.size, structVar.offset, true, var.type.ptr);
@@ -1283,6 +1287,10 @@ class BackendARM64 : CompilerBackend {
12831287
string name = node.var[0 .. node.var.countUntil(".")];
12841288
auto structVar = GetStructVariable(node, node.var);
12851289

1290+
if (structVar.structure) {
1291+
Error(node.error, "Can't push the value of an array or structure");
1292+
}
1293+
12861294
if (VariableExists(name)) {
12871295
auto var = GetVariable(name);
12881296
SetVariable(node, var, structVar.size, structVar.offset, true, var.type.ptr);

source/backends/lua.d

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ class BackendLua : CompilerBackend {
292292
string name = node.name[0 .. node.name.countUntil(".")];
293293
auto structVar = GetStructVariable(node, node.name);
294294

295+
if (structVar.structure) {
296+
Error(node.error, "Can't push the value of an array or structure");
297+
}
298+
295299
if (GlobalExists(name)) {
296300
auto var = GetGlobal(name);
297301

@@ -957,6 +961,10 @@ class BackendLua : CompilerBackend {
957961
string name = node.var[0 .. node.var.countUntil(".")];
958962
auto structVar = GetStructVariable(node, node.var);
959963

964+
if (structVar.structure) {
965+
Error(node.error, "Can't push the value of an array or structure");
966+
}
967+
960968
if (VariableExists(name)) {
961969
auto var = GetVariable(name);
962970

source/backends/rm86.d

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ class BackendRM86 : CompilerBackend {
386386
string name = node.name[0 .. node.name.countUntil(".")];
387387
auto structVar = GetStructVariable(node, node.name);
388388

389+
if (structVar.structure) {
390+
Error(node.error, "Can't push the value of an array or structure");
391+
}
392+
389393
if (GlobalExists(name)) {
390394
auto var = GetGlobal(name);
391395

@@ -1094,6 +1098,10 @@ class BackendRM86 : CompilerBackend {
10941098
string name = node.var[0 .. node.var.countUntil(".")];
10951099
auto structVar = GetStructVariable(node, node.var);
10961100

1101+
if (structVar.structure) {
1102+
Error(node.error, "Can't push the value of an array or structure");
1103+
}
1104+
10971105
if (VariableExists(name)) {
10981106
auto var = GetVariable(name);
10991107

source/backends/uxn.d

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import std.conv;
44
import std.stdio;
55
import std.range;
66
import std.format;
7+
import std.string;
78
import std.algorithm;
89
import callisto.util;
910
import callisto.error;
@@ -29,6 +30,7 @@ class BackendUXN : CompilerBackend {
2930
bool inWhile;
3031
uint currentLoop;
3132
uint tempLabelNum;
33+
string assembler = "uxnasm";
3234

3335
this() {
3436
output = new Output();
@@ -87,7 +89,7 @@ class BackendUXN : CompilerBackend {
8789

8890
override string[] FinalCommands() => [
8991
format("mv %s %s.tal", compiler.outFile, compiler.outFile),
90-
format("uxnasm %s.tal %s", compiler.outFile, compiler.outFile),
92+
format("%s %s.tal %s", assembler, compiler.outFile, compiler.outFile),
9193
keepAssembly? "" : format("rm %s.tal", compiler.outFile)
9294
];
9395

@@ -108,7 +110,23 @@ class BackendUXN : CompilerBackend {
108110
|c0 @DateTime &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1
109111
";
110112

111-
override bool HandleOption(string opt, ref string[] versions, Preprocessor preproc) => false;
113+
override bool HandleOption(string opt, ref string[] versions, Preprocessor preproc) {
114+
if (opt.canFind('=')) {
115+
string key = opt[0 .. opt.indexOf('=')];
116+
string val = opt[opt.indexOf('=') + 1 .. $];
117+
118+
switch (key) {
119+
case "asm": {
120+
assembler = val;
121+
return true;
122+
}
123+
default: return false;
124+
}
125+
}
126+
else {
127+
return false;
128+
}
129+
}
112130

113131
override void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
114132
consts[name] = Constant(new IntegerNode(error, value));
@@ -371,6 +389,10 @@ class BackendUXN : CompilerBackend {
371389
string name = node.name[0 .. node.name.countUntil(".")];
372390
auto structVar = GetStructVariable(node, node.name);
373391

392+
if (structVar.structure) {
393+
Error(node.error, "Can't push the value of an array or structure");
394+
}
395+
374396
if (GlobalExists(name)) {
375397
auto var = GetGlobal(name);
376398

@@ -1040,6 +1062,10 @@ class BackendUXN : CompilerBackend {
10401062
string name = node.var[0 .. node.var.countUntil(".")];
10411063
auto structVar = GetStructVariable(node, node.var);
10421064

1065+
if (structVar.structure) {
1066+
Error(node.error, "Can't push the value of an array or structure");
1067+
}
1068+
10431069
if (VariableExists(name)) {
10441070
auto var = GetVariable(name);
10451071

source/backends/x86_64.d

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,10 @@ class BackendX86_64 : CompilerBackend {
759759
string name = node.name[0 .. node.name.countUntil(".")];
760760
auto structVar = GetStructVariable(node, node.name);
761761

762+
if (structVar.structure) {
763+
Error(node.error, "Can't push the value of an array or structure");
764+
}
765+
762766
if (GlobalExists(name)) {
763767
auto var = GetGlobal(name);
764768

@@ -1561,6 +1565,10 @@ class BackendX86_64 : CompilerBackend {
15611565
string name = node.var[0 .. node.var.countUntil(".")];
15621566
auto structVar = GetStructVariable(node, node.var);
15631567

1568+
if (structVar.structure) {
1569+
Error(node.error, "Can't push the value of an array or structure");
1570+
}
1571+
15641572
if (VariableExists(name)) {
15651573
auto var = GetVariable(name);
15661574

source/compiler.d

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ struct StructEntry {
1919
UsedType type;
2020
string name;
2121
bool array;
22-
size_t size;
22+
private size_t size;
2323
size_t offset;
24+
25+
size_t Size() => array? size * type.Size() : type.Size();
2426
}
2527

2628
struct StructVariable {
2729
size_t size;
2830
size_t offset; // Relative to root struct, unlike StructEntry
31+
bool structure; // includes defined structs and arrays
2932
}
3033

3134
struct Type {
@@ -188,7 +191,7 @@ class CompilerBackend {
188191
memberType.ptr = member.type.ptr;
189192

190193
auto newMember = StructEntry(
191-
memberType, member.name, member.array, memberType.Size(), offset
194+
memberType, member.name, member.array, member.size, offset
192195
);
193196
entries ~= newMember;
194197
members ~= member.name;
@@ -197,8 +200,7 @@ class CompilerBackend {
197200
copiesOfMe ~= &entries[$ - 1].type;
198201
}
199202

200-
offset += newMember.array?
201-
newMember.type.Size() * newMember.size : newMember.type.Size();
203+
offset += newMember.Size();
202204
}
203205

204206
foreach (ref member ; entries) {
@@ -398,7 +400,9 @@ class CompilerBackend {
398400
offset += structure[index].offset;
399401
size_t size = structure[index].type.Size();
400402

401-
return StructVariable(size, offset);
403+
return StructVariable(
404+
size, offset, structure[index].array || structure[index].type.isStruct
405+
);
402406
}
403407

404408
final size_t GetStackSize() {

0 commit comments

Comments
 (0)