Skip to content

Commit 35cefca

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

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;
@@ -179,6 +180,10 @@ public fn void Target.disableAsserts(Target* t) { t.disable_asserts = true; }
179180

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

183+
public fn void Target.enableForceInitializers(Target* t) { t.force_initializers = true; }
184+
185+
public fn bool Target.hasForceInitializers(const Target* t) { return t.force_initializers; }
186+
182187
public fn void Target.visitLibs(const Target* t, library_list.Visitor visitor, void* arg) {
183188
t.libs.visit(visitor, arg);
184189
}

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 {
@@ -436,6 +437,9 @@ fn void Parser.lex_option(Parser* p, Token* result) {
436437
case "disable-asserts":
437438
result.kind = Kind.DisableAsserts;
438439
break;
440+
case "force-initializers":
441+
result.kind = Kind.ForceInitializers;
442+
break;
439443
case "nolibc":
440444
result.kind = Kind.NoLibc;
441445
break;
@@ -495,6 +499,7 @@ fn void Parser.parseTop(Parser* p) {
495499
case Warnings:
496500
case Backend:
497501
case DisableAsserts:
502+
case ForceInitializers:
498503
case NoLibc:
499504
p.error("must be inside target");
500505
break;
@@ -737,6 +742,11 @@ fn void Parser.parseTarget(Parser* p) {
737742
p.consumeToken();
738743
p.target.disableAsserts();
739744
break;
745+
case ForceInitializers:
746+
if (files_started) p.error("$force-initializers must come before files");
747+
p.consumeToken();
748+
p.target.enableForceInitializers();
749+
break;
740750
case NoLibc:
741751
if (files_started) p.error("$nolibc must come before files");
742752
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
@@ -75,6 +75,7 @@ type Generator struct {
7575
const build_file.Info* build_info;
7676
const target_info.Info* targetInfo;
7777
bool enable_asserts;
78+
bool force_initializers;
7879
bool fast_build;
7980
bool asan;
8081
bool msan;
@@ -605,13 +606,16 @@ fn bool Generator.emitGlobalVarDecl(Generator* gen, string_buffer.Buf* out, Decl
605606
// generate definition to c file
606607
if (!d.isExported() && !emit_header) out.add("static ");
607608
gen.emitGlobalVarDeclCommon(out, d);
608-
out.add(" = ");
609609
Expr* ie = vd.getInit();
610610
if (ie) {
611+
out.add(" = ");
611612
gen.emitConstExpr(out, ie, Assignment);
612613
} else {
613614
// auto-initialize (only required for embedded targets)
614-
gen.emitAutoInit(out, d.getType());
615+
if (gen.force_initializers) {
616+
out.add(" = ");
617+
gen.emitAutoInit(out, d.getType());
618+
}
615619
}
616620
out.add(";\n");
617621
}
@@ -1264,6 +1268,7 @@ public fn void generate(string_pool.Pool* astPool,
12641268
Decl* mainFunc,
12651269
string_list.List* asm_files,
12661270
bool enable_asserts,
1271+
bool force_initializers,
12671272
bool fast_build, bool asan, bool msan, bool ubsan,
12681273
bool test_mode, bool trace_calls)
12691274
{
@@ -1278,6 +1283,7 @@ public fn void generate(string_pool.Pool* astPool,
12781283
gen.init(astPool, target, kind, output_dir, dir, diags, sm, build_info, mainFunc);
12791284
gen.auxPool = auxPool;
12801285
gen.enable_asserts = enable_asserts;
1286+
gen.force_initializers = force_initializers;
12811287
gen.fast_build = fast_build;
12821288
gen.asan = asan;
12831289
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)