Skip to content

Commit adf2324

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 890f3a4 commit adf2324

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

common/build_target.c2

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

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

compiler/compiler.c2

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ public fn void build(string_pool.Pool* auxPool,
8989
build_file.Info* build_info, // can be nil
9090
build_target.Target* target,
9191
const Options* opts,
92+
string_list.List* features,
9293
PluginHandler* pluginHandler)
9394
{
9495
Compiler c = {}
9596
plugin_info.Info info = {}
9697

97-
c.build(auxPool, sm, diags, build_info, target, opts, pluginHandler, &info);
98+
c.build(auxPool, sm, diags, build_info, target, opts, features, pluginHandler, &info);
9899

99100
if (opts.print_reports) {
100101
c.sm.report(opts.print_reports > 1);
@@ -170,6 +171,7 @@ fn void Compiler.build(Compiler* c,
170171
build_file.Info* build_info, // can be nil
171172
build_target.Target* target,
172173
const Options* opts,
174+
string_list.List* features,
173175
PluginHandler* pluginHandler,
174176
plugin_info.Info* info)
175177
{
@@ -188,7 +190,7 @@ fn void Compiler.build(Compiler* c,
188190
c.target.disableWarnings();
189191
c.target.disableAsserts();
190192
c.removeFeature("AstStatistics");
191-
c.addFeature("BOOTSTRAP", "1");
193+
c.addFeature("BOOTSTRAP");
192194
}
193195

194196
if (opts.no_build) {
@@ -269,18 +271,23 @@ fn void Compiler.build(Compiler* c,
269271
c.addGlobalDefine("SYSTEM", c.targetInfo.getSystemName());
270272
c.addGlobalDefine("ARCH", c.targetInfo.getArchName());
271273
if (c.targetInfo.intWidth == 64) {
272-
c.addFeature("ARCH_64BIT", "1");
274+
c.addFeature("ARCH_64BIT");
273275
} else {
274-
c.addFeature("ARCH_32BIT", "1");
276+
c.addFeature("ARCH_32BIT");
275277
}
276-
if (opts.asan) c.addFeature("__ASAN__", "1");
277-
if (opts.msan) c.addFeature("__MSAN__", "1");
278-
if (opts.ubsan) c.addFeature("__UBSAN__", "1");
278+
if (opts.asan) c.addFeature("__ASAN__");
279+
if (opts.msan) c.addFeature("__MSAN__");
280+
if (opts.ubsan) c.addFeature("__UBSAN__");
279281

280-
c.addFeature("USE_NATIVE_CTYPES", "1");
282+
for (u32 i = 0; i < features.length(); i++) {
283+
c.addFeature(features.get(i));
284+
}
285+
286+
c.addFeature("USE_NATIVE_CTYPES");
281287
#ifndef BOOTSTRAP
282-
c.addFeature("EXPERIMENTAL", "1");
288+
c.addFeature("EXPERIMENTAL");
283289
#endif
290+
284291
c.parser = c2_parser.create(sm,
285292
diags,
286293
c.astPool,
@@ -478,7 +485,7 @@ fn void Compiler.findTopModule(void* arg, ast.Module* m) {
478485
c.mainFunc = c.analyser.findMain(m, c.main_idx);
479486
}
480487

481-
fn void Compiler.addFeature(Compiler* c, const char* str, const char* value) {
488+
fn void Compiler.addFeature(Compiler* c, const char* str) {
482489
// TODO: handle value
483490
c.target.addFeature(c.auxPool.addStr(str, true));
484491
}
@@ -489,13 +496,13 @@ fn void Compiler.removeFeature(Compiler* c, const char* str) {
489496

490497
fn void Compiler.addGlobalDefine(Compiler* c, const char* prefix, const char* tail) {
491498
char[32] tmp;
492-
stdio.snprintf(tmp, 32, "%s_%s", prefix, tail);
499+
stdio.snprintf(tmp, elemsof(tmp), "%s_%s", prefix, tail);
493500
for (usize i = 0; tmp[i]; i++) {
494501
u8 ch = (u8)tmp[i];
495502
tmp[i] = (ch == '-') ? '_' : (char)ctype.toupper(ch);
496503
}
497504

498-
c.addFeature(tmp, "1");
505+
c.addFeature(tmp);
499506
}
500507

501508
// 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) {
@@ -178,6 +181,7 @@ const char[] Usage_help =
178181
" -A print Library ASTs\n"
179182
" -b [file] use specified build file\n"
180183
" -d [dir] change to [dir] first\n"
184+
" -Dfeature define global feature\n"
181185
" -h print this help\n"
182186
" -i use IR backend\n"
183187
" -I use IR backend and print generated IR\n"
@@ -322,14 +326,22 @@ fn void parse_arguments(ArgumentParser *ap, compiler.Options* comp_opts, Options
322326
if (arg[1] == '-') {
323327
parse_long_opt(ap, arg, comp_opts, opts);
324328
} else {
325-
if (strlen(arg) != 2) ap.unknownOption(arg);
329+
const char *argarg = nil;
330+
if (memchr("Ddbo", arg[1], 4)) { // needs argument
331+
argarg = arg[2] ? &arg[2] : ap.getOptionArgument(arg);
332+
} else if (strlen(arg) != 2) {
333+
ap.unknownOption(arg);
334+
}
326335
switch (arg[1]) {
327336
case '0': // 'hidden' feature, print AST early after parsing, then quit
328337
comp_opts.print_ast_early = true;
329338
break;
330339
case 'A':
331340
comp_opts.print_lib_ast = true;
332341
break;
342+
case 'D':
343+
opts.features.addStr(argarg);
344+
break;
333345
case 'I':
334346
opts.use_ir_backend = true;
335347
comp_opts.print_ir = true;
@@ -344,10 +356,10 @@ fn void parse_arguments(ArgumentParser *ap, compiler.Options* comp_opts, Options
344356
comp_opts.print_ast = true;
345357
break;
346358
case 'b':
347-
opts.build_file = ap.getOptionArgument(arg);
359+
opts.build_file = argarg;
348360
break;
349361
case 'd':
350-
opts.other_dir = ap.getOptionArgument(arg);
362+
opts.other_dir = argarg;
351363
break;
352364
case '?':
353365
case 'h':
@@ -360,7 +372,7 @@ fn void parse_arguments(ArgumentParser *ap, compiler.Options* comp_opts, Options
360372
comp_opts.print_modules = true;
361373
break;
362374
case 'o':
363-
opts.output_name = ap.getOptionArgument(arg);
375+
opts.output_name = argarg;
364376
break;
365377
case 'r':
366378
comp_opts.print_reports += 1;
@@ -623,7 +635,7 @@ fn bool Context.build_target(Context* c,
623635
}
624636
}
625637

626-
compiler.build(c.auxPool, c.sm, c.diags, c.build_info, target, &c.comp_opts, &c.pluginHandler);
638+
compiler.build(c.auxPool, c.sm, c.diags, c.build_info, target, &c.comp_opts, &c.opts.features, &c.pluginHandler);
627639

628640
// TODO unload target-specific plugins?
629641
// TODO fix build_file

0 commit comments

Comments
 (0)