Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ast/ast.c2
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public type AST struct @(opaque) {
attr_table.Table* attrs;
}

#if ARCH_32BIT
static_assert(132, sizeof(AST));
#else
static_assert(144+64, sizeof(AST));
#endif

fn AST* AST.create(string_pool.Pool* auxPool, u32 name, Module* mod, bool is_interface, bool is_generated) {
AST* a = stdlib.calloc(1, sizeof(AST));
Expand Down
4 changes: 4 additions & 0 deletions ast/member_expr.c2
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ type MemberRef union {
usize bits;
}

#if ARCH_32BIT
static_assert(4, sizeof(MemberRef));
#else
static_assert(8, sizeof(MemberRef));
#endif

public type MemberExpr struct @(opaque) {
// base.loc points to the beginning of the member name in refs[1]
Expand Down
4 changes: 4 additions & 0 deletions ast/module.c2
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public type Module struct @(opaque) {
SymbolTable symbols;
InstanceTable instances;
}
#if ARCH_32BIT
static_assert(56, sizeof(Module));
#else
static_assert(80, sizeof(Module));
#endif

// Note: name must be allocated in target StringPool
public fn Module* Module.create(ast_context.Context* c, u32 name_idx, bool is_external) {
Expand Down
4 changes: 4 additions & 0 deletions ast/struct_type_decl.c2
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public type FieldInitField struct {
// 1 byte padding
Decl* decl; // can point to anonymous sub-struct members as well
}
#if ARCH_32BIT
static_assert(20, sizeof(FieldInitField));
#else
static_assert(24, sizeof(FieldInitField));
#endif

public fn bool FieldInitField.isZeroSizeBitfield(const FieldInitField* f) {
return f.is_bitfield && f.bitfield_width == 0;
Expand Down
5 changes: 5 additions & 0 deletions ast/type_ref.c2
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public type Ref struct {
u32 name_idx;
Decl* decl; // set during analysis, or during parsing for Function
}

#if ARCH_32BIT
static_assert(12, sizeof(Ref));
#else
static_assert(16, sizeof(Ref));
#endif

public fn u32 Ref.getNameIdx(const Ref* r) {
return r.name_idx;
Expand Down
68 changes: 65 additions & 3 deletions ast/utils.c2
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,72 @@ import stdio;
import stdlib;
import string;

#if Arch32
#else
#if ARCH_32BIT
static_assert(20, sizeof(Decl));
static_assert(32, sizeof(AliasTypeDecl));
static_assert(40, sizeof(EnumTypeDecl));
static_assert(24, sizeof(FunctionTypeDecl));
static_assert(32, sizeof(VarDecl));
static_assert(40, sizeof(EnumConstantDecl));
static_assert(32, sizeof(ImportDecl));
static_assert(36, sizeof(StructTypeDecl));
static_assert(64, sizeof(FunctionDecl));

static_assert(8, sizeof(Stmt));
static_assert(12, sizeof(GotoStmt));
static_assert(16, sizeof(LabelStmt));
static_assert(16, sizeof(AssertStmt));
static_assert(8, sizeof(DeclStmt));
static_assert(12, sizeof(SwitchStmt));
static_assert(20, sizeof(AsmStmt));
static_assert(16, sizeof(IfStmt));
static_assert(16, sizeof(WhileStmt));
static_assert(24, sizeof(ForStmt));
static_assert(8, sizeof(BreakStmt));
static_assert(8, sizeof(CompoundStmt));
static_assert(8, sizeof(ContinueStmt));
static_assert(8, sizeof(FallthroughStmt));
static_assert(8, sizeof(ReturnStmt));

static_assert(12, sizeof(Expr));
static_assert(12, sizeof(BooleanLiteral));
static_assert(12, sizeof(CharLiteral));
static_assert(20, sizeof(MemberExpr));
static_assert(12, sizeof(NilExpr));
static_assert(16, sizeof(IdentifierExpr));
static_assert(16, sizeof(ImplicitCastExpr));
static_assert(20, sizeof(InitListExpr));
static_assert(24, sizeof(IntegerLiteral));
static_assert(16, sizeof(ParenExpr));
static_assert(20, sizeof(StringLiteral));
static_assert(24, sizeof(TypeExpr));
static_assert(16, sizeof(UnaryOperator));
static_assert(24, sizeof(ArrayDesignatedInitExpr));
static_assert(20, sizeof(ArraySubscriptExpr));
static_assert(20, sizeof(BinaryOperator));
static_assert(20, sizeof(BitOffsetExpr));
static_assert(24, sizeof(CallExpr));
static_assert(32, sizeof(ExplicitCastExpr));
static_assert(24, sizeof(BuiltinExpr));
static_assert(24, sizeof(FieldDesignatedInitExpr));
static_assert(28, sizeof(ConditionalOperator));
static_assert(24, sizeof(NamedArgument));
static_assert(20, sizeof(AlternateExpr));

static_assert(4, sizeof(QualType));
static_assert(12, sizeof(Type));
static_assert(12, sizeof(BuiltinType));
static_assert(16, sizeof(EnumType));
static_assert(16, sizeof(FunctionType));
static_assert(16, sizeof(PointerType));
static_assert(16, sizeof(StructType));
static_assert(20, sizeof(ArrayType));

static_assert(12, sizeof(ArrayValue));
static_assert(8, sizeof(SwitchCase));
static_assert(16, sizeof(StaticAssert));
static_assert(8, sizeof(TypeRef));
#else
static_assert(24, sizeof(Decl));
static_assert(32, sizeof(AliasTypeDecl));
static_assert(64, sizeof(EnumTypeDecl));
Expand Down Expand Up @@ -92,7 +155,6 @@ static_assert(16, sizeof(ArrayValue));
static_assert(8, sizeof(SwitchCase));
static_assert(24, sizeof(StaticAssert));
static_assert(8, sizeof(TypeRef));

#endif

public const QualType QualType_Invalid = { }
Expand Down
8 changes: 8 additions & 0 deletions ast/var_decl.c2
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,22 @@ type BitFieldInfo struct {
BitFieldLayout layout;
// u16 pad
}
#if ARCH_32BIT
static_assert(12, sizeof(BitFieldInfo));
#else
static_assert(16, sizeof(BitFieldInfo));
#endif

type VarDeclInit struct {
Expr* expr;
SrcLoc loc;
// u32 pad
}
#if ARCH_32BIT
static_assert(8, sizeof(VarDeclInit));
#else
static_assert(16, sizeof(VarDeclInit));
#endif

public type VarDecl struct @(opaque) {
Decl base;
Expand Down
4 changes: 4 additions & 0 deletions ast_utils/attr.c2
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public type Attr struct {
AttrValueKind value_kind;
AttrValue value;
}
#if ARCH_32BIT
static_assert(24, sizeof(Attr));
#else
static_assert(32, sizeof(Attr));
#endif

// Note: only meant for printing (since not allocated in StringPool)
public fn const char* Attr.kind2name(const Attr* a) {
Expand Down
4 changes: 4 additions & 0 deletions ast_utils/string_buffer.c2
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public type Buf struct {
u32 indent_step;
}

#if ARCH_32BIT
static_assert(20, sizeof(Buf));
#else
static_assert(24, sizeof(Buf));
#endif

public fn Buf* Buf.init(Buf* buf, char* data, u32 capacity, bool grow, bool use_colors, u32 indent_step) {
assert(capacity);
Expand Down
4 changes: 4 additions & 0 deletions ast_utils/string_pool.c2
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public type Pool struct @(opaque) {
u32 entry_capacity;
HashEntry* entries; // contain hash table followed by collision lists
}
#if ARCH_32BIT
static_assert(56, sizeof(Pool));
#else
static_assert(64, sizeof(Pool));
#endif

public fn Pool* create(u32 data_capacity, u32 hash_size) {
Pool* p = stdlib.calloc(1, sizeof(Pool));
Expand Down
49 changes: 35 additions & 14 deletions bootstrap/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

typedef unsigned int u32;
int verbose;
int m32;
int ptr_width = sizeof(void*) * 8;
char cmdline[128];

int pf(const char *fmt, ...) {
int res;
Expand All @@ -35,9 +38,16 @@ void nothing(void *p) {

void preprocess_header(const char *header_name) {
char cmd[200];
printf("// preprocessor output of <%s>\n{\n", header_name);
fflush(stdout);
snprintf(cmd, sizeof(cmd), "echo '#include <%s>' | cc -E -", header_name);
if (m32) {
printf("// preprocessor output of <%s> (%d-bits, -m32, #undef __LP64__)\n{\n", header_name, ptr_width);
fflush(stdout);
snprintf(cmd, sizeof(cmd), "echo '#undef __LP64__\n#include <%s>' | cc -m32 -E -",
header_name);
} else {
printf("// preprocessor output of <%s> (%d-bits)\n{\n", header_name, ptr_width);
fflush(stdout);
snprintf(cmd, sizeof(cmd), "echo '#include <%s>' | cc -E -", header_name);
}
if (!verbose) {
strcat(cmd, " | grep -v '^#'");
strcat(cmd, " | tr -s '\n'");
Expand All @@ -53,11 +63,20 @@ int main(int argc, char *argv[]) {
jmp_buf buf;
setjmp(buf);

m32 = (ptr_width == 32);

strcpy(cmdline, argv[0]);
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-v")) {
strcat(cmdline, " -v");
verbose = 1;
continue;
}
if (!strcmp(argv[i], "-m32")) {
strcat(cmdline, " -m32");
m32 = 1;
continue;
}
fprintf(stderr, "globals: invalid argument '%s'\n", argv[i]);
return 1;
}
Expand All @@ -67,7 +86,7 @@ int main(int argc, char *argv[]) {
nothing(stderr_);

printf("// ----------------------------------------------------------------\n");
printf("// Output of global header processor\n{\n");
printf("// Output of %s (%d-bits)\n{\n", cmdline, ptr_width);
//printf("// preprocessor output of cc -E - < globals.c\n{\n");
//fflush(stdout);
//system("cc -E - < globals.c");
Expand All @@ -76,8 +95,18 @@ int main(int argc, char *argv[]) {
#define xstr(x) #x
#define str(x) xstr(x)

preprocess_header("sys/types.h");

printf("// libc/sys_types.c2i\n{\n"); {
preprocess_header("sys/types.h");
}
printf("}\n");
printf("// libc/sys_time.c2i\n{\n"); {
preprocess_header("sys/time.h");
}
printf("}\n");
printf("// libc/sys_stat.c2i\n{\n"); {
preprocess_header("sys/stat.h");
}
printf("}\n");
printf("// libc/c2_assert.c2i\n{\n"); {
preprocess_header("assert.h");
}
Expand Down Expand Up @@ -1092,14 +1121,6 @@ int main(int argc, char *argv[]) {
preprocess_header("sys/socket.h");
}
printf("}\n");
printf("// libc/sys_stat.c2i\n{\n"); {
preprocess_header("sys/stat.h");
}
printf("}\n");
printf("// libc/sys_time.c2i\n{\n"); {
preprocess_header("sys/time.h");
}
printf("}\n");
printf("// libc/sys_utsname.c2i\n{\n"); {
preprocess_header("sys/utsname.h");
}
Expand Down
1 change: 1 addition & 0 deletions common/process_utils.c2
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import stdio local;
import stdlib local;
import string local;
import sys_stat local;
import sys_types local; // pid_t
import unistd local;

const u32 MAX_ARG_LEN = 512;
Expand Down
26 changes: 14 additions & 12 deletions common/target_info.c2
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ module target_info;
import stdio local;
import string local;

public type Arch enum u8 (const char* const name) {
Unknown : { "unknown" },
I686 : { "i686" },
Arm : { "arm" },
X86_64 : { "x86_64" },
Amd64 : { "amd64" },
Arm64 : { "arm64" },
Riscv_32 : { "riscv32" },
Riscv_64 : { "riscv64" },
public type Arch enum u8 (const char* const name, Arch m32_version) {
Unknown : { "unknown", Unknown },
I386 : { "i386", I386 },
I686 : { "i686", I686 },
Arm : { "arm", Arm },
X86_64 : { "x86_64", I686 },
Amd64 : { "amd64", I686 },
Arm64 : { "arm64", Arm },
Riscv_32 : { "riscv32", Riscv_32 },
Riscv_64 : { "riscv64", Riscv_32 },
}

public type System enum u8 (const char* const name) {
Expand Down Expand Up @@ -60,9 +61,9 @@ public fn System str2sys(const char* name) {
return Unknown;
}

public fn Arch str2arch(const char* name) {
for (Arch i = Arch.min; i <= Arch.max; i++) {
if (strcasecmp(i.name, name) == 0) return i;
public fn Arch str2arch(const char* name, bool m32 = false) {
for (Arch a = Arch.min; a <= Arch.max; a++) {
if (strcasecmp(a.name, name) == 0) return m32 ? a.m32_version : a;
}
return Unknown;
}
Expand Down Expand Up @@ -99,6 +100,7 @@ public fn void Info.init(Info* info) {
info.ptrWidth = 64;
info.longWidth = 64;
break;
case I386:
case I686:
case Arm:
info.ptrWidth = 32;
Expand Down
4 changes: 2 additions & 2 deletions examples/common/logger.c2
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import stdio local;
import stdlib local;
import sys_time local;
import unistd;
import libc_time;
import libc_time local;

public type Level enum u8 {
Fatal = 0,
Expand Down Expand Up @@ -151,7 +151,7 @@ fn void Log.internal(Log* log, Level level, const char* color_, const char* form
Timeval tv;
gettimeofday(&tv, nil);
Time now = tv.tv_sec;
libc_time.Tm* now2 = libc_time.localtime(&now);
Tm* now2 = localtime(&now);
cp += sprintf(cp, "%02d/%02d/%02d %02d:%02d:%02d.%d",
now2.tm_year % 100, now2.tm_mon + 1, now2.tm_mday,
now2.tm_hour, now2.tm_min, now2.tm_sec, tv.tv_usec / 1000);
Expand Down
4 changes: 4 additions & 0 deletions generator/ir/field_struct_layouter.c2
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ type FieldInit struct {
const ast.FieldInitField* info;
ast.Expr* expr;
}
#if ARCH_32BIT
static_assert(8, sizeof(FieldInit));
#else
static_assert(16, sizeof(FieldInit));
#endif

type FieldStructLayouter struct {
Generator* gen;
Expand Down
1 change: 1 addition & 0 deletions ir/context_target.c2
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import target_info;
fn const ir_tools.Target* getArchTarget(const target_info.Info* info) {
switch (info.arch) {
case Unknown:
case I386:
case I686:
break;
case Arm:
Expand Down
Loading