Skip to content

Commit 7c921af

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

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;
@@ -172,6 +173,10 @@ public fn bool Target.hasAsserts(const Target* t) { return !t.disable_asserts; }
172173

173174
public type Visitor fn void (void* arg, u32 name, Kind kind);
174175

176+
public fn void Target.enableForceInitializers(Target* t) { t.force_initializers = true; }
177+
178+
public fn bool Target.hasForceInitializers(const Target* t) { return t.force_initializers; }
179+
175180
public fn void Target.visitLibs(const Target* t, Visitor visitor, void* arg) {
176181
t.libs.visit(visitor, arg);
177182
}

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,
@@ -66,6 +67,7 @@ const char*[] kind_names = {
6667
"$warnings",
6768
"$backend",
6869
"$disable-asserts",
70+
"$force-initializers",
6971
"$nolibc",
7072
"$config",
7173
"$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 = Kind.DisableAsserts;
420421
break;
422+
case "force-initializers":
423+
result.kind = Kind.ForceInitializers;
424+
break;
421425
case "nolibc":
422426
result.kind = Kind.NoLibc;
423427
break;
@@ -477,6 +481,7 @@ fn void Parser.parseTop(Parser* p) {
477481
case Warnings:
478482
case Backend:
479483
case DisableAsserts:
484+
case ForceInitializers:
480485
case NoLibc:
481486
p.error("must be inside target");
482487
break;
@@ -722,6 +727,11 @@ fn void Parser.parseTarget(Parser* p) {
722727
p.consumeToken();
723728
p.target.disableAsserts();
724729
break;
730+
case ForceInitializers:
731+
if (files_started) p.error("$force-initializers must come before files");
732+
p.consumeToken();
733+
p.target.enableForceInitializers();
734+
break;
725735
case NoLibc:
726736
if (files_started) p.error("$nolibc must come before files");
727737
p.consumeToken();

compiler/compiler_generate.c2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn void Compiler.generate(Compiler* c, const char* target_name, const char* outp
7474
c.mainFunc,
7575
&asm_files,
7676
c.target.hasAsserts(),
77+
c.target.hasForceInitializers(),
7778
c.target.getFastBuild() | c.opts.fast_build,
7879
c.opts.asan, c.opts.msan, c.opts.ubsan,
7980
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;
@@ -609,13 +610,16 @@ fn bool Generator.emitGlobalVarDecl(Generator* gen, string_buffer.Buf* out, Decl
609610
// generate definition to c file
610611
if (!d.isExported() && !emit_header) out.add("static ");
611612
gen.emitGlobalVarDeclCommon(out, d);
612-
out.add(" = ");
613613
Expr* ie = vd.getInit();
614614
if (ie) {
615+
out.add(" = ");
615616
gen.emitConstExpr(out, ie, Assignment);
616617
} else {
617618
// auto-initialize (only required for embedded targets)
618-
gen.emitAutoInit(out, d.getType());
619+
if (gen.force_initializers) {
620+
out.add(" = ");
621+
gen.emitAutoInit(out, d.getType());
622+
}
619623
}
620624
out.add(";\n");
621625
}
@@ -1270,6 +1274,7 @@ public fn void generate(string_pool.Pool* astPool,
12701274
Decl* mainFunc,
12711275
string_list.List* asm_files,
12721276
bool enable_asserts,
1277+
bool force_initializers,
12731278
bool fast_build, bool asan, bool msan, bool ubsan,
12741279
bool test_mode, bool trace_calls)
12751280
{
@@ -1284,6 +1289,7 @@ public fn void generate(string_pool.Pool* astPool,
12841289
gen.init(astPool, target, kind, output_dir, dir, diags, sm, build_info, mainFunc);
12851290
gen.auxPool = auxPool;
12861291
gen.enable_asserts = enable_asserts;
1292+
gen.force_initializers = force_initializers;
12871293
gen.fast_build = fast_build;
12881294
gen.asan = asan;
12891295
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)