Support file-scope functions

This commit is contained in:
Rui Ueyama 2019-08-20 08:33:52 +09:00
parent 7938157072
commit ce61154cf5
4 changed files with 6 additions and 1 deletions

View File

@ -201,6 +201,7 @@ struct Function {
Function *next;
char *name;
VarList *params;
bool is_static;
Node *node;
VarList *locals;

View File

@ -574,6 +574,7 @@ void emit_text(Program *prog) {
printf(".text\n");
for (Function *fn = prog->fns; fn; fn = fn->next) {
if (!fn->is_static)
printf(".global %s\n", fn->name);
printf("%s:\n", fn->name);
funcname = fn->name;

View File

@ -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();

2
tests
View File

@ -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;");