From ce61154cf542e630bc3e40262fdacdf20bf91b90 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 20 Aug 2019 08:33:52 +0900 Subject: [PATCH] Support file-scope functions --- chibicc.h | 1 + codegen.c | 3 ++- parse.c | 1 + tests | 2 ++ 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/chibicc.h b/chibicc.h index 80e608f..e754cb3 100644 --- a/chibicc.h +++ b/chibicc.h @@ -201,6 +201,7 @@ struct Function { Function *next; char *name; VarList *params; + bool is_static; Node *node; VarList *locals; diff --git a/codegen.c b/codegen.c index d2205f4..a143b01 100644 --- a/codegen.c +++ b/codegen.c @@ -574,7 +574,8 @@ void emit_text(Program *prog) { printf(".text\n"); for (Function *fn = prog->fns; fn; fn = fn->next) { - printf(".global %s\n", fn->name); + if (!fn->is_static) + printf(".global %s\n", fn->name); printf("%s:\n", fn->name); funcname = fn->name; diff --git a/parse.c b/parse.c index e1930fa..fda2592 100644 --- a/parse.c +++ b/parse.c @@ -575,6 +575,7 @@ Function *function() { // Construct a function object Function *fn = calloc(1, sizeof(Function)); fn->name = name; + fn->is_static = ty->is_static; expect("("); fn->params = read_func_params(); diff --git a/tests b/tests index 8b329de..336c5f1 100644 --- a/tests +++ b/tests @@ -96,6 +96,8 @@ int param_decay(int x[]) { return x[0]; } void voidfn(void) {} +static int static_fn(void) {} + int main() { assert(8, ({ int a=3; int z=5; a+z; }), "int a=3; int z=5; a+z;");