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
5 changes: 5 additions & 0 deletions common/build_target.c2
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public type Target struct @(opaque) {
Kind kind;

bool disable_asserts;
bool force_initializers;
bool no_libc;
BackEndKind backend;
bool backend_no_build;
Expand Down Expand Up @@ -179,6 +180,10 @@ public fn void Target.disableAsserts(Target* t) { t.disable_asserts = true; }

public fn bool Target.hasAsserts(const Target* t) { return !t.disable_asserts; }

public fn void Target.enableForceInitializers(Target* t) { t.force_initializers = true; }

public fn bool Target.hasForceInitializers(const Target* t) { return t.force_initializers; }

public fn void Target.visitLibs(const Target* t, library_list.Visitor visitor, void* arg) {
t.libs.visit(visitor, arg);
}
Expand Down
12 changes: 11 additions & 1 deletion compiler/c2recipe_parser.c2
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Kind enum u8 {
Warnings,
Backend,
DisableAsserts,
ForceInitializers,
NoLibc,
Config,
Export,
Expand All @@ -65,6 +66,7 @@ const char*[] kind_names = {
"$warnings",
"$backend",
"$disable-asserts",
"$force-initializers",
"$nolibc",
"$config",
"$export",
Expand Down Expand Up @@ -200,7 +202,6 @@ fn void Parser.error(Parser* p, const char* format @(printf_format), ...) @(nore

char[256] locstr;
p.sm.loc2str(p.token.loc, locstr, elemsof(locstr));

if (color.useColor()) {
fprintf(stderr, "%s: %serror:%s %s\n", locstr, color.Red, color.Normal, msg);
} else {
Expand Down Expand Up @@ -436,6 +437,9 @@ fn void Parser.lex_option(Parser* p, Token* result) {
case "disable-asserts":
result.kind = Kind.DisableAsserts;
break;
case "force-initializers":
result.kind = Kind.ForceInitializers;
break;
case "nolibc":
result.kind = Kind.NoLibc;
break;
Expand Down Expand Up @@ -495,6 +499,7 @@ fn void Parser.parseTop(Parser* p) {
case Warnings:
case Backend:
case DisableAsserts:
case ForceInitializers:
case NoLibc:
p.error("must be inside target");
break;
Expand Down Expand Up @@ -737,6 +742,11 @@ fn void Parser.parseTarget(Parser* p) {
p.consumeToken();
p.target.disableAsserts();
break;
case ForceInitializers:
if (files_started) p.error("$force-initializers must come before files");
p.consumeToken();
p.target.enableForceInitializers();
break;
case NoLibc:
if (files_started) p.error("$nolibc must come before files");
p.consumeToken();
Expand Down
1 change: 1 addition & 0 deletions compiler/compiler.c2
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ fn void Compiler.build(Compiler* c,
c.mainFunc,
&asm_files,
c.target.hasAsserts(),
c.target.hasForceInitializers(),
c.target.getFastBuild() | c.opts.fast_build,
c.opts.asan, c.opts.msan, c.opts.ubsan,
c.opts.test_mode, c.opts.trace_calls);
Expand Down
10 changes: 8 additions & 2 deletions generator/c/c_generator.c2
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Generator struct {
const build_file.Info* build_info;
const target_info.Info* targetInfo;
bool enable_asserts;
bool force_initializers;
bool fast_build;
bool asan;
bool msan;
Expand Down Expand Up @@ -610,13 +611,16 @@ fn bool Generator.emitGlobalVarDecl(Generator* gen, string_buffer.Buf* out, Decl
// generate definition to c file
if (!d.isExported() && !emit_header) out.add("static ");
gen.emitGlobalVarDeclCommon(out, d);
out.add(" = ");
Expr* ie = vd.getInit();
if (ie) {
out.add(" = ");
gen.emitConstExpr(out, ie, Assignment);
} else {
// auto-initialize (only required for embedded targets)
gen.emitAutoInit(out, d.getType());
if (gen.force_initializers) {
out.add(" = ");
gen.emitAutoInit(out, d.getType());
}
}
out.add(";\n");
}
Expand Down Expand Up @@ -1272,6 +1276,7 @@ public fn void generate(string_pool.Pool* astPool,
Decl* mainFunc,
string_list.List* asm_files,
bool enable_asserts,
bool force_initializers,
bool fast_build, bool asan, bool msan, bool ubsan,
bool test_mode, bool trace_calls)
{
Expand All @@ -1286,6 +1291,7 @@ public fn void generate(string_pool.Pool* astPool,
gen.init(astPool, target, kind, output_dir, dir, diags, sm, build_info, mainFunc);
gen.auxPool = auxPool;
gen.enable_asserts = enable_asserts;
gen.force_initializers = force_initializers;
gen.fast_build = fast_build;
gen.asan = asan;
gen.msan = msan;
Expand Down
6 changes: 3 additions & 3 deletions test/c_generator/attributes/exported_not_static.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public fn i32 main(i32 argc, const i8** argv)

// @expect{atleast, cgen/build.c}

static int32_t file1_a = 0;
int32_t file1_b = 0;
static int32_t file1_c = 0;
static int32_t file1_a;
int32_t file1_b;
static int32_t file1_c;

static void file1_f(void);
void file1_g(void);
Expand Down
6 changes: 3 additions & 3 deletions test/c_generator/constants/string_literals.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static const char test1_BBB[4] = "bbb";
static char test1_ddd[4] = "ddd";
static char test1_eee[4] = "eee";
static const char* test1_fff = "fff";
static const char* test1_ggg = NULL;
static char test1_hhh[3] = { };
static char* test1_iii = NULL;
static const char* test1_ggg;
static char test1_hhh[3];
static char* test1_iii;

2 changes: 1 addition & 1 deletion test/c_generator/functions/struct_functions/global.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct test_Type_ {
int32_t member;
};

static test_Type test_t = { };
static test_Type test_t;

static void test_Type_init(test_Type* _arg0)
{
Expand Down
4 changes: 2 additions & 2 deletions test/c_generator/module/static_single_module.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public i32 y2;

// @expect{atleast, cgen/build.c}

static int32_t test1_x1 = 0;
int32_t test1_y1 = 0;
static int32_t test1_x1;
int32_t test1_y1;

// test2 symbols are not generated, because they are unused

Expand Down
4 changes: 2 additions & 2 deletions test/c_generator/module/variable_fast_build.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public char[] y2 = { 1, 2, 3, 4 }
// @expect{atleast, cgen/test1.c}
#include "test1.h"

static int32_t test1_x1 = 0;
int32_t test1_y1 = 0;
static int32_t test1_x1;
int32_t test1_y1;
char test1_y2[4] = { 1, 2, 3, 4 };

// @expect{atleast, cgen/test1.h}
Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/stmts/local_array_decl.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public fn i32 main() {

#define test_Size 20

static int32_t test_board[20][20] = { };
static int32_t test_board[20][20];

static void test_func1(void);

Expand Down
2 changes: 1 addition & 1 deletion test/c_generator/types/const_pointer_type.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ const Struct* p;

// @expect{atleast, cgen/build.c}

static const test_Struct* test_p = NULL;
static const test_Struct* test_p;

2 changes: 1 addition & 1 deletion test/c_generator/types/qualifiers.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ const volatile u32 C = 2;
// @expect{atleast, cgen/build.c}

#define test_A 1
static volatile uint16_t test_b = 0;
static volatile uint16_t test_b;
static const volatile uint32_t test_C = 2;

2 changes: 1 addition & 1 deletion test/c_generator/variables/initialize_2d_array.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ module test;
i32[3][2] a;

// @expect{atleast, cgen/build.c}
static int32_t test_a[3][2] = { };
static int32_t test_a[3][2];

8 changes: 4 additions & 4 deletions test/c_generator/variables/should_initalize_globals.c2t
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ struct test_Foo_ {
_Bool b;
};

static test_Foo test_f = { };
static test_Foo test_f;

static int32_t test_i = 0;
static int32_t test_i;

static test_Foo test_fs[2] = { };
static test_Foo test_fs[2];

static int32_t test_is[2] = { };
static int32_t test_is[2];