Skip to content

Commit 0e2a5cd

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

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
@@ -106,6 +106,7 @@ public type Target struct @(opaque) {
106106
Kind kind;
107107

108108
bool disable_asserts;
109+
bool force_initializers;
109110
bool no_libc;
110111
BackEndKind backend;
111112
bool backend_no_build;
@@ -177,6 +178,10 @@ public fn void Target.disableAsserts(Target* t) { t.disable_asserts = true; }
177178

178179
public fn bool Target.hasAsserts(const Target* t) { return !t.disable_asserts; }
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, library_list.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
@@ -43,6 +43,7 @@ type Kind enum u8 {
4343
Warnings,
4444
Backend,
4545
DisableAsserts,
46+
ForceInitializers,
4647
NoLibc,
4748
Config,
4849
Export,
@@ -65,6 +66,7 @@ const char*[] kind_names = {
6566
"$warnings",
6667
"$backend",
6768
"$disable-asserts",
69+
"$force-initializers",
6870
"$nolibc",
6971
"$config",
7072
"$export",
@@ -200,7 +202,6 @@ fn void Parser.error(Parser* p, const char* format @(printf_format), ...) @(nore
200202

201203
char[256] locstr;
202204
p.sm.loc2str(p.token.loc, locstr, elemsof(locstr));
203-
204205
if (color.useColor()) {
205206
fprintf(stderr, "%s: %serror:%s %s\n", locstr, color.Red, color.Normal, msg);
206207
} else {
@@ -419,6 +420,9 @@ fn void Parser.lex_option(Parser* p, Token* result) {
419420
case "disable-asserts":
420421
result.kind = Kind.DisableAsserts;
421422
break;
423+
case "force-initializers":
424+
result.kind = Kind.ForceInitializers;
425+
break;
422426
case "nolibc":
423427
result.kind = Kind.NoLibc;
424428
break;
@@ -478,6 +482,7 @@ fn void Parser.parseTop(Parser* p) {
478482
case Warnings:
479483
case Backend:
480484
case DisableAsserts:
485+
case ForceInitializers:
481486
case NoLibc:
482487
p.error("must be inside target");
483488
break;
@@ -712,6 +717,11 @@ fn void Parser.parseTarget(Parser* p) {
712717
p.consumeToken();
713718
p.target.disableAsserts();
714719
break;
720+
case ForceInitializers:
721+
if (files_started) p.error("$force-initializers must come before files");
722+
p.consumeToken();
723+
p.target.enableForceInitializers();
724+
break;
715725
case NoLibc:
716726
if (files_started) p.error("$nolibc must come before files");
717727
p.consumeToken();

compiler/compiler.c2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ fn void Compiler.build(Compiler* c,
515515
c.mainFunc,
516516
&asm_files,
517517
c.target.hasAsserts(),
518+
c.target.hasForceInitializers(),
518519
c.target.getFastBuild() | c.opts.fast_build,
519520
c.opts.asan, c.opts.msan, c.opts.ubsan,
520521
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;
@@ -604,13 +605,16 @@ fn bool Generator.emitGlobalVarDecl(Generator* gen, string_buffer.Buf* out, Decl
604605
// generate definition to c file
605606
if (!d.isExported() && !emit_header) out.add("static ");
606607
gen.emitGlobalVarDeclCommon(out, d);
607-
out.add(" = ");
608608
Expr* ie = vd.getInit();
609609
if (ie) {
610+
out.add(" = ");
610611
gen.emitConstExpr(out, ie);
611612
} else {
612613
// auto-initialize (only required for embedded targets)
613-
gen.emitAutoInit(out, d.getType());
614+
if (gen.force_initializers) {
615+
out.add(" = ");
616+
gen.emitAutoInit(out, d.getType());
617+
}
614618
}
615619
out.add(";\n\n");
616620
}
@@ -1263,6 +1267,7 @@ public fn void generate(string_pool.Pool* astPool,
12631267
Decl* mainFunc,
12641268
string_list.List* asm_files,
12651269
bool enable_asserts,
1270+
bool force_initializers,
12661271
bool fast_build, bool asan, bool msan, bool ubsan,
12671272
bool test_mode, bool trace_calls)
12681273
{
@@ -1277,6 +1282,7 @@ public fn void generate(string_pool.Pool* astPool,
12771282
gen.init(astPool, target, kind, output_dir, dir, diags, sm, build_info, mainFunc);
12781283
gen.auxPool = auxPool;
12791284
gen.enable_asserts = enable_asserts;
1285+
gen.force_initializers = force_initializers;
12801286
gen.fast_build = fast_build;
12811287
gen.asan = asan;
12821288
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)