Skip to content

Commit b7343f9

Browse files
committed
Compiler: control global data initialization with $force-initializers
* no longer force global data initialization unless specified in recipe.txt
1 parent f9fddf6 commit b7343f9

File tree

14 files changed

+44
-22
lines changed

14 files changed

+44
-22
lines changed

common/build_target.c2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public type Target struct @(opaque) {
104104
Kind kind;
105105

106106
bool disable_asserts;
107+
bool force_initializers;
107108
bool no_libc;
108109
BackEndKind backend;
109110
bool backend_no_build;
@@ -177,6 +178,10 @@ public fn bool Target.hasAsserts(const Target* t) { return !t.disable_asserts; }
177178

178179
public type Visitor fn void (void* arg, u32 name, Kind kind);
179180

181+
public fn void Target.enableForceInitializers(Target* t) { t.force_initializers = true; }
182+
183+
public fn bool Target.hasForceInitializers(const Target* t) { return t.force_initializers; }
184+
180185
public fn void Target.visitLibs(const Target* t, Visitor visitor, void* arg) {
181186
t.libs.visit(visitor, arg);
182187
}

compiler/c2recipe_parser.c2

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Kind enum u8 {
4444
Warnings,
4545
Backend,
4646
DisableAsserts,
47+
ForceInitializers,
4748
NoLibc,
4849
Config,
4950
Export,
@@ -67,6 +68,7 @@ const char*[Kind] kind_names = {
6768
"$warnings",
6869
"$backend",
6970
"$disable-asserts",
71+
"$force-initializers",
7072
"$nolibc",
7173
"$config",
7274
"$export",
@@ -182,7 +184,6 @@ fn void Parser.error(Parser* p, const char* format @(printf_format), ...) @(nore
182184

183185
char[256] locstr;
184186
p.sm.loc2str(p.token.loc, locstr, elemsof(locstr));
185-
186187
if (color.useColor()) {
187188
fprintf(stderr, "%s: %serror:%s %s\n", locstr, color.Red.str(), color.Normal.str(), msg);
188189
} else {
@@ -418,6 +419,9 @@ fn void Parser.lex_option(Parser* p, Token* result) {
418419
case "disable-asserts":
419420
result.kind = DisableAsserts;
420421
break;
422+
case "force-initializers":
423+
result.kind = Kind.ForceInitializers;
424+
break;
421425
case "nolibc":
422426
result.kind = NoLibc;
423427
break;
@@ -480,6 +484,7 @@ fn void Parser.parseTop(Parser* p) {
480484
case Warnings:
481485
case Backend:
482486
case DisableAsserts:
487+
case ForceInitializers:
483488
case NoLibc:
484489
p.error("must be inside target");
485490
break;
@@ -732,6 +737,11 @@ fn void Parser.parseTarget(Parser* p) {
732737
p.consumeToken();
733738
p.target.disableAsserts();
734739
break;
740+
case ForceInitializers:
741+
if (files_started) p.error("$force-initializers must come before files");
742+
p.consumeToken();
743+
p.target.enableForceInitializers();
744+
break;
735745
case NoLibc:
736746
if (files_started) p.error("$nolibc must come before files");
737747
p.consumeToken();

compiler/compiler_generate.c2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ fn void Compiler.generate(Compiler* c, const char* target_name, const char* outp
8282
c.mainFunc,
8383
&asm_files,
8484
c.target.hasAsserts(),
85+
c.target.hasForceInitializers(),
8586
c.target.getFastBuild() | c.opts.fast_build,
8687
c.opts.asan, c.opts.msan, c.opts.ubsan,
8788
c.opts.test_mode, c.opts.trace_calls);

generator/c/c_generator.c2

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type Generator struct {
7474
const build_file.Info* build_info;
7575
const target_info.Info* targetInfo;
7676
bool enable_asserts;
77+
bool force_initializers;
7778
bool fast_build;
7879
bool asan;
7980
bool msan;
@@ -612,13 +613,16 @@ fn bool Generator.emitGlobalVarDecl(Generator* gen, string_buffer.Buf* out, Decl
612613
// generate definition to c file
613614
if (!d.isExported() && !emit_header) out.add("static ");
614615
gen.emitGlobalVarDeclCommon(out, d);
615-
out.add(" = ");
616616
Expr* ie = vd.getInit();
617617
if (ie) {
618+
out.add(" = ");
618619
gen.emitConstExpr(out, ie, Assignment);
619620
} else {
620621
// auto-initialize (only required for embedded targets)
621-
gen.emitAutoInit(out, d.getType());
622+
if (gen.force_initializers) {
623+
out.add(" = ");
624+
gen.emitAutoInit(out, d.getType());
625+
}
622626
}
623627
out.add(";\n");
624628
}
@@ -1284,6 +1288,7 @@ public fn void generate(string_pool.Pool* astPool,
12841288
Decl* mainFunc,
12851289
string_list.List* asm_files,
12861290
bool enable_asserts,
1291+
bool force_initializers,
12871292
bool fast_build, bool asan, bool msan, bool ubsan,
12881293
bool test_mode, bool trace_calls)
12891294
{
@@ -1298,6 +1303,7 @@ public fn void generate(string_pool.Pool* astPool,
12981303
gen.init(astPool, target, kind, output_dir, dir, diags, sm, build_info, mainFunc);
12991304
gen.auxPool = auxPool;
13001305
gen.enable_asserts = enable_asserts;
1306+
gen.force_initializers = force_initializers;
13011307
gen.fast_build = fast_build;
13021308
gen.asan = asan;
13031309
gen.msan = msan;

test/c_generator/attributes/exported_not_static.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public fn i32 main(i32 argc, const i8** argv)
2020

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

23-
static int32_t file1_a = 0;
24-
int32_t file1_b = 0;
25-
static int32_t file1_c = 0;
23+
static int32_t file1_a;
24+
int32_t file1_b;
25+
static int32_t file1_c;
2626

2727
static void file1_f(void);
2828
void file1_g(void);

test/c_generator/constants/string_literals.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static const char test1_BBB[4] = "bbb";
3535
static char test1_ddd[4] = "ddd";
3636
static char test1_eee[4] = "eee";
3737
static const char* test1_fff = "fff";
38-
static const char* test1_ggg = NULL;
39-
static char test1_hhh[3] = { };
40-
static char* test1_iii = NULL;
38+
static const char* test1_ggg;
39+
static char test1_hhh[3];
40+
static char* test1_iii;
4141

test/c_generator/functions/struct_functions/global.c2t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct test_Type_ {
2525
int32_t member;
2626
};
2727

28-
static test_Type test_t = { };
28+
static test_Type test_t;
2929

3030
static void test_Type_init(test_Type* _arg0)
3131
{

test/c_generator/module/static_single_module.c2t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public i32 y2;
1717

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

20-
static int32_t test1_x1 = 0;
21-
int32_t test1_y1 = 0;
20+
static int32_t test1_x1;
21+
int32_t test1_y1;
2222

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

test/c_generator/module/variable_fast_build.c2t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public char[] y2 = { 1, 2, 3, 4 }
1313
// @expect{atleast, cgen/test1.c}
1414
#include "test1.h"
1515

16-
static int32_t test1_x1 = 0;
17-
int32_t test1_y1 = 0;
16+
static int32_t test1_x1;
17+
int32_t test1_y1;
1818
char test1_y2[4] = { 1, 2, 3, 4 };
1919

2020
// @expect{atleast, cgen/test1.h}

test/c_generator/stmts/local_array_decl.c2t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public fn i32 main() {
2020

2121
#define test_Size 20
2222

23-
static int32_t test_board[20][20] = { };
23+
static int32_t test_board[20][20];
2424

2525
static void test_func1(void);
2626

0 commit comments

Comments
 (0)