Skip to content

Commit

Permalink
Make union spacers consist of struct with static array rather than pl…
Browse files Browse the repository at this point in the history
…ain static array.

Static arrays are an awkward type in C.
  • Loading branch information
FeepingCreature committed Sep 23, 2022
1 parent d3d8ee8 commit ea0e706
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/backend/c.nt
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ string typeFmtInternal(CBackendModule mod, BackendType type, bool indirect) {
else return "VOIDTYPE";
}
if (auto backendSpacerType = type.instanceOf(BackendSpacerType)) {
// Naked arrays are awkward in C, so make sure it's bundled in a struct.
string mangle = typeMangle(type);
string typestr = "arr_" ~ mangle;
if (mod.declared(typestr)) return typestr;
string def = "typedef char " ~ typestr ~ "[" ~ ltoa(backendSpacerType.size_) ~ "] " ~
string def = "typedef struct { char value[" ~ ltoa(backendSpacerType.size_) ~ "]; } " ~ typestr ~ " " ~
"__attribute__ ((aligned (" ~ itoa(backendSpacerType.alignment_) ~ ")));";
mod.output.print(def);
mod.globals.add(Global(typestr, ""));
Expand Down Expand Up @@ -595,7 +596,8 @@ class CBackendFunction : BackendFunction {
if (type.size(mod.platform) > 0) {
string typestr = typeFmt(mod, type);
mut string init = "0";
if (type.instanceOf(BackendStructType)) init = "{0}";
if (type.instanceOf(BackendStructType) || type.instanceOf(BackendSpacerType))
init = "{0}";
ret = Value(typestr, init, exactType=false);
}
else ret = Value.none;
Expand Down Expand Up @@ -717,7 +719,8 @@ class CBackendFunction : BackendFunction {
string typefmt = typeFmt(mod, type);
if (type.size(mod.platform) > 0) {
mut string init = "0";
if (type.instanceOf(BackendStructType)) init = "{0}";
if (type.instanceOf(BackendStructType) || type.instanceOf(BackendSpacerType))
init = "{0}";
this.print(typefmt ~ " reg" ~ itoa(regId) ~ "_frame = " ~ init ~ ";");
ret = Value(typefmt ~ "*", "&reg" ~ itoa(regId) ~ "_frame", exactType=true);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/neat/runtime.nt
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ string ltoa(long l) {
}

void assert(int test) {
import neat.runtime.stdlib : exit, fprintf, stderr;
import neat.runtime.stdlib : exit, fputs, stderr;

if (!test) {
fprintf(stderr, "Assertion failed! Aborting.\n".ptr);
fputs("Assertion failed! Aborting.\n".ptr, stderr);
exit(1);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/neat/runtime/stdlib.nt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module neat.runtime.stdlib;

extern(C) int printf(char* format, ...);
extern(C) int fprintf(void* handle, char* format, ...);
extern(C) int fputs(char *str, void* handle);
extern(C) int snprintf(char* str, size_t size, char* format, ...);
extern(C) int fflush(void* handle);
extern(C) void* stdin;
Expand Down

0 comments on commit ea0e706

Please sign in to comment.