Skip to content

Commit 5ebcd76

Browse files
committed
Compiler: simplify argument parsing, add -Dxxx for features
* simplify option parsing, add `argarg` for options with an argument * add `-DFeature` to define features from the command line * simplify `Compiler.addFeature()`: feature values are not supported yet and could be specified as `Feature=value` later * simplify Makefile for bootstrap phases
1 parent 9deb2a9 commit 5ebcd76

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ c2c: $(C2C)
2626
@$(C2C) --version
2727

2828
$(C2C): output/bootstrap/bootstrap $(C2C_DEPS)
29-
@echo "---- running (bootstrapped$(C2FLAGS)) c2c ----"
30-
@output/bootstrap/bootstrap c2c $(C2FLAGS) --fast --noplugins
31-
@mv output/c2c/c2c output/bootstrap/c2c
32-
@echo "---- running c2c (no plugins$(C2FLAGS)) ----"
33-
@output/bootstrap/c2c $(C2FLAGS) --noplugins --fast c2c $(PLUGINS)
29+
@echo "---- running bootstrap$(C2FLAGS) c2c ----"
30+
@output/bootstrap/bootstrap $(C2FLAGS) c2c -o c2c-bootstrap --fast --noplugins
31+
@echo "---- running c2c-bootstrap (no plugins$(C2FLAGS)) ----"
32+
@output/c2c-bootstrap/c2c-bootstrap $(C2FLAGS) --fast --noplugins c2c $(PLUGINS)
3433
@./install_plugins.sh
3534
@echo "---- running c2c (optimized with plugins$(C2FLAGS)) ----"
3635
@output/c2c/c2c $(C2FLAGS)

common/build_target.c2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public fn u32 Target.numAsmFiles(const Target* t) { return t.asm_files.getCount(
156156
public fn const string_list.List* Target.getFeatures(const Target* t) { return &t.features; }
157157

158158
public fn void Target.addFeature(Target* t, u32 feature) {
159+
// TODO: handle value
159160
t.features.add(feature);
160161
}
161162

compiler/compiler.c2

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ public fn void build(string_pool.Pool* auxPool,
8787
build_file.Info* build_info, // can be nil
8888
build_target.Target* target,
8989
const Options* opts,
90+
string_list.List* features,
9091
PluginHandler* pluginHandler)
9192
{
9293
Compiler c = {}
9394
plugin_info.Info info = {}
9495

95-
c.build(auxPool, sm, diags, build_info, target, opts, pluginHandler, &info);
96+
c.build(auxPool, sm, diags, build_info, target, opts, features, pluginHandler, &info);
9697

9798
if (opts.print_reports) {
9899
c.sm.report(opts.print_reports > 1);
@@ -168,6 +169,7 @@ fn void Compiler.build(Compiler* c,
168169
build_file.Info* build_info, // can be nil
169170
build_target.Target* target,
170171
const Options* opts,
172+
string_list.List* features,
171173
PluginHandler* pluginHandler,
172174
plugin_info.Info* info)
173175
{
@@ -255,15 +257,19 @@ fn void Compiler.build(Compiler* c,
255257
c.addGlobalDefine("SYSTEM", c.targetInfo.getSystemName());
256258
c.addGlobalDefine("ARCH", c.targetInfo.getArchName());
257259
if (c.targetInfo.intWidth == 64) {
258-
c.addFeature("ARCH_64BIT", "1");
260+
c.addFeature("ARCH_64BIT");
259261
} else {
260-
c.addFeature("ARCH_32BIT", "1");
262+
c.addFeature("ARCH_32BIT");
261263
}
262-
if (opts.asan) c.addFeature("__ASAN__", "1");
263-
if (opts.msan) c.addFeature("__MSAN__", "1");
264-
if (opts.ubsan) c.addFeature("__UBSAN__", "1");
264+
if (opts.asan) c.addFeature("__ASAN__");
265+
if (opts.msan) c.addFeature("__MSAN__");
266+
if (opts.ubsan) c.addFeature("__UBSAN__");
265267

266-
c.addFeature("USE_NATIVE_CTYPES", "1");
268+
for (u32 i = 0; i < features.length(); i++) {
269+
c.addFeature(features.get(i));
270+
}
271+
272+
c.addFeature("USE_NATIVE_CTYPES");
267273

268274
c.parser = c2_parser.create(sm,
269275
diags,
@@ -463,20 +469,20 @@ fn void Compiler.findTopModule(void* arg, ast.Module* m) {
463469
c.mainFunc = c.analyser.findMain(m, c.main_idx);
464470
}
465471

466-
fn void Compiler.addFeature(Compiler* c, const char* str, const char* value) {
472+
fn void Compiler.addFeature(Compiler* c, const char* str) {
467473
// TODO: handle value
468474
c.target.addFeature(c.auxPool.addStr(str, true));
469475
}
470476

471477
fn void Compiler.addGlobalDefine(Compiler* c, const char* prefix, const char* tail) {
472478
char[32] tmp;
473-
stdio.snprintf(tmp, 32, "%s_%s", prefix, tail);
479+
stdio.snprintf(tmp, elemsof(tmp), "%s_%s", prefix, tail);
474480
for (usize i = 0; tmp[i]; i++) {
475481
u8 ch = (u8)tmp[i];
476482
tmp[i] = (ch == '-') ? '_' : (char)ctype.toupper(ch);
477483
}
478484

479-
c.addFeature(tmp, "1");
485+
c.addFeature(tmp);
480486
}
481487

482488
// NOTE: paths are not 0-terminated

compiler/main.c2

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ type Options struct {
5757
const char* output_name;
5858
string_list.List targets;
5959
string_list.List files;
60+
string_list.List features;
6061
}
6162

6263
fn void Options.init(Options* opts, string_pool.Pool* pool) {
6364
memset(opts, 0, sizeof(Options));
6465
opts.targets.init(pool);
6566
opts.files.init(pool);
67+
opts.features.init(pool);
6668
}
6769

6870
fn void Options.free(Options *opts) {
6971
opts.targets.free();
7072
opts.files.free();
73+
opts.features.free();
7174
}
7275

7376
fn void write_file_or_die(const char* filename, string_buffer.Buf* buf) {
@@ -177,6 +180,7 @@ const char[] Usage_help =
177180
" -A print Library ASTs\n"
178181
" -b [file] use specified build file\n"
179182
" -d [dir] change to [dir] first\n"
183+
" -Dfeature define global feature\n"
180184
" -h print this help\n"
181185
" -i use IR backend\n"
182186
" -I use IR backend and print generated IR\n"
@@ -313,14 +317,22 @@ fn void parse_arguments(ArgumentParser *ap, compiler.Options* comp_opts, Options
313317
if (arg[1] == '-') {
314318
parse_long_opt(ap, arg, comp_opts, opts);
315319
} else {
316-
if (strlen(arg) != 2) ap.unknownOption(arg);
320+
const char *argarg = nil;
321+
if (memchr("Ddbo", arg[1], 4)) { // needs argument
322+
argarg = arg[2] ? &arg[2] : ap.getOptionArgument(arg);
323+
} else if (strlen(arg) != 2) {
324+
ap.unknownOption(arg);
325+
}
317326
switch (arg[1]) {
318327
case '0': // 'hidden' feature, print AST early after parsing, then quit
319328
comp_opts.print_ast_early = true;
320329
break;
321330
case 'A':
322331
comp_opts.print_lib_ast = true;
323332
break;
333+
case 'D':
334+
opts.features.addStr(argarg);
335+
break;
324336
case 'I':
325337
opts.use_ir_backend = true;
326338
comp_opts.print_ir = true;
@@ -335,10 +347,10 @@ fn void parse_arguments(ArgumentParser *ap, compiler.Options* comp_opts, Options
335347
comp_opts.print_ast = true;
336348
break;
337349
case 'b':
338-
opts.build_file = ap.getOptionArgument(arg);
350+
opts.build_file = argarg;
339351
break;
340352
case 'd':
341-
opts.other_dir = ap.getOptionArgument(arg);
353+
opts.other_dir = argarg;
342354
break;
343355
case '?':
344356
case 'h':
@@ -351,7 +363,7 @@ fn void parse_arguments(ArgumentParser *ap, compiler.Options* comp_opts, Options
351363
comp_opts.print_modules = true;
352364
break;
353365
case 'o':
354-
opts.output_name = ap.getOptionArgument(arg);
366+
opts.output_name = argarg;
355367
break;
356368
case 'r':
357369
comp_opts.print_reports += 1;
@@ -614,7 +626,7 @@ fn bool Context.build_target(Context* c,
614626
}
615627
}
616628

617-
compiler.build(c.auxPool, c.sm, c.diags, c.build_info, target, &c.comp_opts, &c.pluginHandler);
629+
compiler.build(c.auxPool, c.sm, c.diags, c.build_info, target, &c.comp_opts, &c.opts.features, &c.pluginHandler);
618630

619631
// TODO unload target-specific plugins?
620632
// TODO fix build_file

0 commit comments

Comments
 (0)