Skip to content

Commit

Permalink
Add -fcommon and -fno-common flags
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Sep 30, 2020
1 parent b0d061d commit f284c82
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions chibicc.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,5 @@ int str_width(char *p, int len);
//

extern StringArray include_paths;
extern bool opt_fcommon;
extern char *base_file;
2 changes: 1 addition & 1 deletion codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ static void emit_data(Obj *prog) {
? MAX(16, var->align) : var->align;
println(" .align %d", align);

if (var->is_tentative) {
if (opt_fcommon && var->is_tentative) {
println(" .comm %s, %d, %d", var->name, var->ty->size, align);
continue;
}
Expand Down
11 changes: 11 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "chibicc.h"

StringArray include_paths;
bool opt_fcommon = true;

static bool opt_E;
static bool opt_S;
Expand Down Expand Up @@ -90,6 +91,16 @@ static void parse_args(int argc, char **argv) {
continue;
}

if (!strcmp(argv[i], "-fcommon")) {
opt_fcommon = true;
continue;
}

if (!strcmp(argv[i], "-fno-common")) {
opt_fcommon = false;
continue;
}

if (!strcmp(argv[i], "-c")) {
opt_c = true;
continue;
Expand Down
11 changes: 11 additions & 0 deletions test/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,15 @@ check -idirafter
echo "#include \"idirafter\"" | $chibicc -idirafter $tmp/dir1 -I$tmp/dir2 -E - | grep -q bar
check -idirafter

# -fcommon
echo 'int foo;' | $chibicc -S -o- - | grep -q '\.comm foo'
check '-fcommon (default)'

echo 'int foo;' | $chibicc -fcommon -S -o- - | grep -q '\.comm foo'
check '-fcommon'

# -fno-common
echo 'int foo;' | $chibicc -fno-common -S -o- - | grep -q '^foo:'
check '-fno-common'

echo OK

0 comments on commit f284c82

Please sign in to comment.