diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-08-25 00:41:34 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-08-25 00:41:34 +0900 |
| commit | 37d184d14136c9a9b5bdf3b62df87deea12dc947 (patch) | |
| tree | e2899375984950f6cf6cf7a1fc6ca1b43a77b9f5 | |
| parent | 7f29d50e4558a700b7611dc72e87e7922ac6a345 (diff) | |
| download | ducc-37d184d14136c9a9b5bdf3b62df87deea12dc947.tar.gz ducc-37d184d14136c9a9b5bdf3b62df87deea12dc947.tar.zst ducc-37d184d14136c9a9b5bdf3b62df87deea12dc947.zip | |
feat: add static to file-local functions
| -rw-r--r-- | src/codegen.c | 72 | ||||
| -rw-r--r-- | src/parse.c | 196 | ||||
| -rw-r--r-- | src/preprocess.c | 110 | ||||
| -rw-r--r-- | src/sys.c | 2 | ||||
| -rw-r--r-- | src/tokenize.c | 4 |
5 files changed, 192 insertions, 192 deletions
diff --git a/src/codegen.c b/src/codegen.c index 82c9c96..19d7079 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -16,7 +16,7 @@ struct CodeGen { }; typedef struct CodeGen CodeGen; -CodeGen* codegen_new(FILE* out) { +static CodeGen* codegen_new(FILE* out) { CodeGen* g = calloc(1, sizeof(CodeGen)); g->out = out; g->next_label = 1; @@ -24,16 +24,16 @@ CodeGen* codegen_new(FILE* out) { return g; } -int codegen_new_label(CodeGen* g) { +static int codegen_new_label(CodeGen* g) { int new_label = g->next_label; ++g->next_label; return new_label; } -void codegen_expr(CodeGen* g, AstNode* ast, GenMode gen_mode); -void codegen_stmt(CodeGen* g, AstNode* ast); +static void codegen_expr(CodeGen* g, AstNode* ast, GenMode gen_mode); +static void codegen_stmt(CodeGen* g, AstNode* ast); -const char* param_reg(int n) { +static const char* param_reg(int n) { if (n == 0) { return "rdi"; } else if (n == 1) { @@ -51,7 +51,7 @@ const char* param_reg(int n) { } } -void codegen_func_prologue(CodeGen* g, AstNode* ast) { +static void codegen_func_prologue(CodeGen* g, AstNode* ast) { fprintf(g->out, " push rbp\n"); fprintf(g->out, " mov rbp, rsp\n"); for (int i = 0; i < ast->node_params->node_len; ++i) { @@ -60,22 +60,22 @@ void codegen_func_prologue(CodeGen* g, AstNode* ast) { fprintf(g->out, " sub rsp, %d\n", ast->node_stack_size); } -void codegen_func_epilogue(CodeGen* g, AstNode* ast) { +static void codegen_func_epilogue(CodeGen* g, AstNode* ast) { fprintf(g->out, " mov rsp, rbp\n"); fprintf(g->out, " pop rbp\n"); fprintf(g->out, " ret\n"); } -void codegen_int_expr(CodeGen* g, AstNode* ast) { +static void codegen_int_expr(CodeGen* g, AstNode* ast) { fprintf(g->out, " push %d\n", ast->node_int_value); } -void codegen_str_expr(CodeGen* g, AstNode* ast) { +static void codegen_str_expr(CodeGen* g, AstNode* ast) { fprintf(g->out, " mov rax, OFFSET FLAG:.Lstr__%d\n", ast->node_idx); fprintf(g->out, " push rax\n"); } -void codegen_unary_expr(CodeGen* g, AstNode* ast) { +static void codegen_unary_expr(CodeGen* g, AstNode* ast) { codegen_expr(g, ast->node_operand, GenMode_rval); if (ast->node_op == TokenKind_not) { fprintf(g->out, " pop rax\n"); @@ -89,12 +89,12 @@ void codegen_unary_expr(CodeGen* g, AstNode* ast) { } } -void codegen_ref_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { +static void codegen_ref_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { codegen_expr(g, ast->node_operand, GenMode_lval); } // "reg" stores the address of the expression to be pushed. -void codegen_push_expr(CodeGen* g, const char* reg, int size) { +static void codegen_push_expr(CodeGen* g, const char* reg, int size) { if (size == 1) { fprintf(g->out, " movsx %s, BYTE PTR [%s]\n", reg, reg); fprintf(g->out, " push %s\n", reg); @@ -119,7 +119,7 @@ void codegen_push_expr(CodeGen* g, const char* reg, int size) { } } -void codegen_lval2rval(CodeGen* g, Type* ty) { +static void codegen_lval2rval(CodeGen* g, Type* ty) { if (ty->kind == TypeKind_array) { return; } @@ -128,14 +128,14 @@ void codegen_lval2rval(CodeGen* g, Type* ty) { codegen_push_expr(g, "rax", type_sizeof(ty)); } -void codegen_deref_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { +static void codegen_deref_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { codegen_expr(g, ast->node_operand, GenMode_rval); if (gen_mode == GenMode_rval) { codegen_lval2rval(g, ast->node_operand->ty->base); } } -void codegen_logical_expr(CodeGen* g, AstNode* ast) { +static void codegen_logical_expr(CodeGen* g, AstNode* ast) { int label = codegen_new_label(g); if (ast->node_op == TokenKind_andand) { @@ -161,7 +161,7 @@ void codegen_logical_expr(CodeGen* g, AstNode* ast) { } } -void codegen_binary_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { +static void codegen_binary_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { codegen_expr(g, ast->node_lhs, gen_mode); codegen_expr(g, ast->node_rhs, gen_mode); fprintf(g->out, " pop rdi\n"); @@ -210,7 +210,7 @@ void codegen_binary_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { fprintf(g->out, " push rax\n"); } -void codegen_cond_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { +static void codegen_cond_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { int label = codegen_new_label(g); codegen_expr(g, ast->node_cond, GenMode_rval); @@ -224,7 +224,7 @@ void codegen_cond_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { fprintf(g->out, ".Lend%d:\n", label); } -void codegen_assign_expr_helper(CodeGen* g, AstNode* ast) { +static void codegen_assign_expr_helper(CodeGen* g, AstNode* ast) { if (ast->node_op == TokenKind_assign) { return; } @@ -254,7 +254,7 @@ void codegen_assign_expr_helper(CodeGen* g, AstNode* ast) { fprintf(g->out, " push rax\n"); } -void codegen_assign_expr(CodeGen* g, AstNode* ast) { +static void codegen_assign_expr(CodeGen* g, AstNode* ast) { int sizeof_lhs = type_sizeof(ast->node_lhs->ty); int sizeof_rhs = type_sizeof(ast->node_rhs->ty); @@ -301,7 +301,7 @@ void codegen_assign_expr(CodeGen* g, AstNode* ast) { } } -void codegen_func_call(CodeGen* g, AstNode* ast) { +static void codegen_func_call(CodeGen* g, AstNode* ast) { const char* func_name = ast->name; if (strcmp(func_name, "__ducc_va_start") == 0) { @@ -359,7 +359,7 @@ void codegen_func_call(CodeGen* g, AstNode* ast) { fprintf(g->out, ".Lend%d:\n", label); } -void codegen_lvar(CodeGen* g, AstNode* ast, GenMode gen_mode) { +static void codegen_lvar(CodeGen* g, AstNode* ast, GenMode gen_mode) { fprintf(g->out, " mov rax, rbp\n"); fprintf(g->out, " sub rax, %d\n", ast->node_stack_offset); fprintf(g->out, " push rax\n"); @@ -368,7 +368,7 @@ void codegen_lvar(CodeGen* g, AstNode* ast, GenMode gen_mode) { } } -void codegen_gvar(CodeGen* g, AstNode* ast, GenMode gen_mode) { +static void codegen_gvar(CodeGen* g, AstNode* ast, GenMode gen_mode) { fprintf(g->out, " lea rax, %s[rip]\n", ast->name); fprintf(g->out, " push rax\n"); if (gen_mode == GenMode_rval) { @@ -376,7 +376,7 @@ void codegen_gvar(CodeGen* g, AstNode* ast, GenMode gen_mode) { } } -void codegen_composite_expr(CodeGen* g, AstNode* ast) { +static void codegen_composite_expr(CodeGen* g, AstNode* ast) { // Standard C does not have composite expression, but ducc internally has. for (int i = 0; i < ast->node_len; ++i) { AstNode* expr = ast->node_items + i; @@ -388,7 +388,7 @@ void codegen_composite_expr(CodeGen* g, AstNode* ast) { } } -void codegen_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { +static void codegen_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { if (ast->kind == AstNodeKind_int_expr) { codegen_int_expr(g, ast); } else if (ast->kind == AstNodeKind_str_expr) { @@ -420,7 +420,7 @@ void codegen_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) { } } -void codegen_return_stmt(CodeGen* g, AstNode* ast) { +static void codegen_return_stmt(CodeGen* g, AstNode* ast) { if (ast->node_expr) { codegen_expr(g, ast->node_expr, GenMode_rval); fprintf(g->out, " pop rax\n"); @@ -428,7 +428,7 @@ void codegen_return_stmt(CodeGen* g, AstNode* ast) { codegen_func_epilogue(g, ast); } -void codegen_if_stmt(CodeGen* g, AstNode* ast) { +static void codegen_if_stmt(CodeGen* g, AstNode* ast) { int label = codegen_new_label(g); codegen_expr(g, ast->node_cond, GenMode_rval); @@ -444,7 +444,7 @@ void codegen_if_stmt(CodeGen* g, AstNode* ast) { fprintf(g->out, ".Lend%d:\n", label); } -void codegen_for_stmt(CodeGen* g, AstNode* ast) { +static void codegen_for_stmt(CodeGen* g, AstNode* ast) { int label = codegen_new_label(g); ++g->loop_labels; *g->loop_labels = label; @@ -470,7 +470,7 @@ void codegen_for_stmt(CodeGen* g, AstNode* ast) { --g->loop_labels; } -void codegen_do_while_stmt(CodeGen* g, AstNode* ast) { +static void codegen_do_while_stmt(CodeGen* g, AstNode* ast) { int label = codegen_new_label(g); ++g->loop_labels; *g->loop_labels = label; @@ -488,36 +488,36 @@ void codegen_do_while_stmt(CodeGen* g, AstNode* ast) { --g->loop_labels; } -void codegen_break_stmt(CodeGen* g, AstNode* ast) { +static void codegen_break_stmt(CodeGen* g, AstNode* ast) { int label = *g->loop_labels; fprintf(g->out, " jmp .Lend%d\n", label); } -void codegen_continue_stmt(CodeGen* g, AstNode* ast) { +static void codegen_continue_stmt(CodeGen* g, AstNode* ast) { int label = *g->loop_labels; fprintf(g->out, " jmp .Lcontinue%d\n", label); } -void codegen_expr_stmt(CodeGen* g, AstNode* ast) { +static void codegen_expr_stmt(CodeGen* g, AstNode* ast) { codegen_expr(g, ast->node_expr, GenMode_rval); // TODO: the expression on the stack can be more than 8 bytes. fprintf(g->out, " pop rax\n"); } -void codegen_var_decl(CodeGen* g, AstNode* ast) { +static void codegen_var_decl(CodeGen* g, AstNode* ast) { } -void codegen_nop(CodeGen* g, AstNode* ast) { +static void codegen_nop(CodeGen* g, AstNode* ast) { } -void codegen_block_stmt(CodeGen* g, AstNode* ast) { +static void codegen_block_stmt(CodeGen* g, AstNode* ast) { for (int i = 0; i < ast->node_len; ++i) { AstNode* stmt = ast->node_items + i; codegen_stmt(g, stmt); } } -void codegen_stmt(CodeGen* g, AstNode* ast) { +static void codegen_stmt(CodeGen* g, AstNode* ast) { if (ast->kind == AstNodeKind_list) { codegen_block_stmt(g, ast); } else if (ast->kind == AstNodeKind_return_stmt) { @@ -543,7 +543,7 @@ void codegen_stmt(CodeGen* g, AstNode* ast) { } } -void codegen_func(CodeGen* g, AstNode* ast) { +static void codegen_func(CodeGen* g, AstNode* ast) { g->current_func = ast; if (!ast->node_function_is_static) { diff --git a/src/parse.c b/src/parse.c index 7750f40..ee194ae 100644 --- a/src/parse.c +++ b/src/parse.c @@ -16,13 +16,13 @@ struct LocalVarArray { }; typedef struct LocalVarArray LocalVarArray; -void lvars_init(LocalVarArray* lvars) { +static void lvars_init(LocalVarArray* lvars) { lvars->len = 0; lvars->capacity = 4; lvars->data = calloc(lvars->capacity, sizeof(LocalVar)); } -void lvars_reserve(LocalVarArray* lvars, size_t size) { +static void lvars_reserve(LocalVarArray* lvars, size_t size) { if (size <= lvars->capacity) return; while (lvars->capacity < size) { @@ -32,7 +32,7 @@ void lvars_reserve(LocalVarArray* lvars, size_t size) { memset(lvars->data + lvars->len, 0, (lvars->capacity - lvars->len) * sizeof(LocalVar)); } -LocalVar* lvars_push_new(LocalVarArray* lvars) { +static LocalVar* lvars_push_new(LocalVarArray* lvars) { lvars_reserve(lvars, lvars->len + 1); return &lvars->data[lvars->len++]; } @@ -50,13 +50,13 @@ struct ScopedSymbolArray { }; typedef struct ScopedSymbolArray ScopedSymbolArray; -void scopedsymbols_init(ScopedSymbolArray* syms) { +static void scopedsymbols_init(ScopedSymbolArray* syms) { syms->len = 0; syms->capacity = 4; syms->data = calloc(syms->capacity, sizeof(ScopedSymbol)); } -void scopedsymbols_reserve(ScopedSymbolArray* syms, size_t size) { +static void scopedsymbols_reserve(ScopedSymbolArray* syms, size_t size) { if (size <= syms->capacity) return; while (syms->capacity < size) { @@ -66,7 +66,7 @@ void scopedsymbols_reserve(ScopedSymbolArray* syms, size_t size) { memset(syms->data + syms->len, 0, (syms->capacity - syms->len) * sizeof(ScopedSymbol)); } -ScopedSymbol* scopedsymbols_push_new(ScopedSymbolArray* syms) { +static ScopedSymbol* scopedsymbols_push_new(ScopedSymbolArray* syms) { scopedsymbols_reserve(syms, syms->len + 1); return &syms->data[syms->len++]; } @@ -90,13 +90,13 @@ struct GlobalVarArray { }; typedef struct GlobalVarArray GlobalVarArray; -void gvars_init(GlobalVarArray* gvars) { +static void gvars_init(GlobalVarArray* gvars) { gvars->len = 0; gvars->capacity = 4; gvars->data = calloc(gvars->capacity, sizeof(GlobalVar)); } -void gvars_reserve(GlobalVarArray* gvars, size_t size) { +static void gvars_reserve(GlobalVarArray* gvars, size_t size) { if (size <= gvars->capacity) return; while (gvars->capacity < size) { @@ -106,7 +106,7 @@ void gvars_reserve(GlobalVarArray* gvars, size_t size) { memset(gvars->data + gvars->len, 0, (gvars->capacity - gvars->len) * sizeof(GlobalVar)); } -GlobalVar* gvars_push_new(GlobalVarArray* gvars) { +static GlobalVar* gvars_push_new(GlobalVarArray* gvars) { gvars_reserve(gvars, gvars->len + 1); return &gvars->data[gvars->len++]; } @@ -124,13 +124,13 @@ struct FuncArray { }; typedef struct FuncArray FuncArray; -void funcs_init(FuncArray* funcs) { +static void funcs_init(FuncArray* funcs) { funcs->len = 0; funcs->capacity = 32; funcs->data = calloc(funcs->capacity, sizeof(Func)); } -void funcs_reserve(FuncArray* funcs, size_t size) { +static void funcs_reserve(FuncArray* funcs, size_t size) { if (size <= funcs->capacity) return; while (funcs->capacity < size) { @@ -140,7 +140,7 @@ void funcs_reserve(FuncArray* funcs, size_t size) { memset(funcs->data + funcs->len, 0, (funcs->capacity - funcs->len) * sizeof(Func)); } -Func* funcs_push_new(FuncArray* funcs) { +static Func* funcs_push_new(FuncArray* funcs) { funcs_reserve(funcs, funcs->len + 1); return &funcs->data[funcs->len++]; } @@ -160,7 +160,7 @@ struct Parser { }; typedef struct Parser Parser; -Parser* parser_new(TokenArray* tokens) { +static Parser* parser_new(TokenArray* tokens) { Parser* p = calloc(1, sizeof(Parser)); p->tokens = tokens; gvars_init(&p->gvars); @@ -179,15 +179,15 @@ Parser* parser_new(TokenArray* tokens) { return p; } -Token* peek_token(Parser* p) { +static Token* peek_token(Parser* p) { return &p->tokens->data[p->pos]; } -Token* next_token(Parser* p) { +static Token* next_token(Parser* p) { return &p->tokens->data[p->pos++]; } -Token* consume_token_if(Parser* p, TokenKind expected) { +static Token* consume_token_if(Parser* p, TokenKind expected) { if (peek_token(p)->kind == expected) { return next_token(p); } else { @@ -195,11 +195,11 @@ Token* consume_token_if(Parser* p, TokenKind expected) { } } -BOOL eof(Parser* p) { +static BOOL eof(Parser* p) { return peek_token(p)->kind != TokenKind_eof; } -Token* expect(Parser* p, TokenKind expected) { +static Token* expect(Parser* p, TokenKind expected) { Token* t = next_token(p); if (t->kind == expected) { return t; @@ -208,7 +208,7 @@ Token* expect(Parser* p, TokenKind expected) { token_stringify(t)); } -int find_lvar_in_scope(Parser* p, Scope* scope, const char* name) { +static int find_lvar_in_scope(Parser* p, Scope* scope, const char* name) { for (int i = 0; i < scope->syms.len; ++i) { ScopedSymbol* sym = &scope->syms.data[i]; if (sym->name && strcmp(sym->name, name) == 0) { @@ -218,11 +218,11 @@ int find_lvar_in_scope(Parser* p, Scope* scope, const char* name) { return -1; } -int find_lvar_in_current_scope(Parser* p, const char* name) { +static int find_lvar_in_current_scope(Parser* p, const char* name) { return find_lvar_in_scope(p, p->scope, name); } -int find_lvar(Parser* p, const char* name) { +static int find_lvar(Parser* p, const char* name) { Scope* scope = p->scope; while (scope) { int idx = find_lvar_in_scope(p, scope, name); @@ -233,7 +233,7 @@ int find_lvar(Parser* p, const char* name) { return -1; } -int calc_stack_offset(Parser* p, Type* ty, BOOL is_param) { +static int calc_stack_offset(Parser* p, Type* ty, BOOL is_param) { int align; if (is_param) { if (8 < type_sizeof(ty) || 8 < type_alignof(ty)) { @@ -255,7 +255,7 @@ int calc_stack_offset(Parser* p, Type* ty, BOOL is_param) { return to_aligned(offset, align); } -int add_lvar(Parser* p, const char* name, Type* ty, BOOL is_param) { +static int add_lvar(Parser* p, const char* name, Type* ty, BOOL is_param) { int stack_offset = calc_stack_offset(p, ty, is_param); LocalVar* lvar = lvars_push_new(&p->lvars); lvar->name = name; @@ -267,7 +267,7 @@ int add_lvar(Parser* p, const char* name, Type* ty, BOOL is_param) { return stack_offset; } -AstNode* generate_temporary_lvar(Parser* p, Type* ty) { +static AstNode* generate_temporary_lvar(Parser* p, Type* ty) { int stack_offset = add_lvar(p, NULL, ty, FALSE); AstNode* lvar = ast_new(AstNodeKind_lvar); lvar->name = NULL; @@ -276,7 +276,7 @@ AstNode* generate_temporary_lvar(Parser* p, Type* ty) { return lvar; } -int find_gvar(Parser* p, const char* name) { +static int find_gvar(Parser* p, const char* name) { for (int i = 0; i < p->gvars.len; ++i) { if (strcmp(p->gvars.data[i].name, name) == 0) { return i; @@ -285,7 +285,7 @@ int find_gvar(Parser* p, const char* name) { return -1; } -int find_func(Parser* p, const char* name) { +static int find_func(Parser* p, const char* name) { for (int i = 0; i < p->funcs.len; ++i) { if (strcmp(p->funcs.data[i].name, name) == 0) { return i; @@ -294,7 +294,7 @@ int find_func(Parser* p, const char* name) { return -1; } -int find_struct(Parser* p, const char* name) { +static int find_struct(Parser* p, const char* name) { for (int i = 0; i < p->structs->node_len; ++i) { if (strcmp(p->structs->node_items[i].name, name) == 0) { return i; @@ -303,7 +303,7 @@ int find_struct(Parser* p, const char* name) { return -1; } -int find_union(Parser* p, const char* name) { +static int find_union(Parser* p, const char* name) { for (int i = 0; i < p->unions->node_len; ++i) { if (strcmp(p->unions->node_items[i].name, name) == 0) { return i; @@ -312,7 +312,7 @@ int find_union(Parser* p, const char* name) { return -1; } -int find_enum(Parser* p, const char* name) { +static int find_enum(Parser* p, const char* name) { for (int i = 0; i < p->enums->node_len; ++i) { if (strcmp(p->enums->node_items[i].name, name) == 0) { return i; @@ -321,7 +321,7 @@ int find_enum(Parser* p, const char* name) { return -1; } -int find_enum_member(Parser* p, const char* name) { +static int find_enum_member(Parser* p, const char* name) { for (int i = 0; i < p->enums->node_len; ++i) { for (int j = 0; j < p->enums->node_items[i].node_members->node_len; ++j) { if (strcmp(p->enums->node_items[i].node_members->node_items[j].name, name) == 0) { @@ -332,7 +332,7 @@ int find_enum_member(Parser* p, const char* name) { return -1; } -int find_typedef(Parser* p, const char* name) { +static int find_typedef(Parser* p, const char* name) { for (int i = 0; i < p->typedefs->node_len; ++i) { if (strcmp(p->typedefs->node_items[i].name, name) == 0) { return i; @@ -341,39 +341,39 @@ int find_typedef(Parser* p, const char* name) { return -1; } -void enter_scope(Parser* p) { +static void enter_scope(Parser* p) { Scope* outer_scope = p->scope; p->scope = calloc(1, sizeof(Scope)); p->scope->outer = outer_scope; scopedsymbols_init(&p->scope->syms); } -void leave_scope(Parser* p) { +static void leave_scope(Parser* p) { p->scope = p->scope->outer; } -void enter_func(Parser* p) { +static void enter_func(Parser* p) { lvars_init(&p->lvars); enter_scope(p); } -void leave_func(Parser* p) { +static void leave_func(Parser* p) { leave_scope(p); } -AstNode* parse_assignment_expr(Parser* p); -AstNode* parse_expr(Parser* p); -AstNode* parse_stmt(Parser* p); +static AstNode* parse_assignment_expr(Parser* p); +static AstNode* parse_expr(Parser* p); +static AstNode* parse_stmt(Parser* p); -const char* parse_ident(Parser* p) { +static const char* parse_ident(Parser* p) { return expect(p, TokenKind_ident)->value.string; } -int register_str_literal(Parser* p, const char* s) { +static int register_str_literal(Parser* p, const char* s) { return strings_push(&p->str_literals, s); } -AstNode* parse_primary_expr(Parser* p) { +static AstNode* parse_primary_expr(Parser* p) { Token* t = next_token(p); if (t->kind == TokenKind_literal_int) { return ast_new_int(t->value.integer); @@ -431,7 +431,7 @@ AstNode* parse_primary_expr(Parser* p) { } } -AstNode* parse_arg_list(Parser* p) { +static AstNode* parse_arg_list(Parser* p) { AstNode* list = ast_new_list(6); while (peek_token(p)->kind != TokenKind_paren_r) { AstNode* arg = parse_assignment_expr(p); @@ -450,7 +450,7 @@ AstNode* parse_arg_list(Parser* p) { // tmp1 = &e; tmp2 = *tmp1; *tmp1 += 1; tmp2 // e-- // tmp1 = &e; tmp2 = *tmp1; *tmp1 -= 1; tmp2 -AstNode* create_new_postfix_inc_or_dec(Parser* p, AstNode* e, TokenKind op) { +static AstNode* create_new_postfix_inc_or_dec(Parser* p, AstNode* e, TokenKind op) { AstNode* tmp1_lvar = generate_temporary_lvar(p, type_new_ptr(e->ty)); AstNode* tmp2_lvar = generate_temporary_lvar(p, e->ty); @@ -473,7 +473,7 @@ AstNode* create_new_postfix_inc_or_dec(Parser* p, AstNode* e, TokenKind op) { return ret; } -AstNode* parse_postfix_expr(Parser* p) { +static AstNode* parse_postfix_expr(Parser* p) { AstNode* ret = parse_primary_expr(p); while (1) { TokenKind tk = peek_token(p)->kind; @@ -503,7 +503,7 @@ AstNode* parse_postfix_expr(Parser* p) { return ret; } -BOOL is_type_token(Parser* p, Token* token) { +static BOOL is_type_token(Parser* p, Token* token) { if (token->kind == TokenKind_keyword_int || token->kind == TokenKind_keyword_short || token->kind == TokenKind_keyword_long || token->kind == TokenKind_keyword_char || token->kind == TokenKind_keyword_void || token->kind == TokenKind_keyword_enum || @@ -517,7 +517,7 @@ BOOL is_type_token(Parser* p, Token* token) { return find_typedef(p, token->value.string) != -1; } -Type* parse_type(Parser* p) { +static Type* parse_type(Parser* p) { Token* t = next_token(p); BOOL has_static = FALSE; while (1) { @@ -590,7 +590,7 @@ Type* parse_type(Parser* p) { return ty; } -AstNode* parse_prefix_expr(Parser* p) { +static AstNode* parse_prefix_expr(Parser* p) { TokenKind op = peek_token(p)->kind; if (consume_token_if(p, TokenKind_minus)) { AstNode* operand = parse_prefix_expr(p); @@ -635,7 +635,7 @@ AstNode* parse_prefix_expr(Parser* p) { return parse_postfix_expr(p); } -AstNode* parse_multiplicative_expr(Parser* p) { +static AstNode* parse_multiplicative_expr(Parser* p) { AstNode* lhs = parse_prefix_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -650,7 +650,7 @@ AstNode* parse_multiplicative_expr(Parser* p) { return lhs; } -AstNode* parse_additive_expr(Parser* p) { +static AstNode* parse_additive_expr(Parser* p) { AstNode* lhs = parse_multiplicative_expr(p); while (1) { if (consume_token_if(p, TokenKind_plus)) { @@ -689,7 +689,7 @@ AstNode* parse_additive_expr(Parser* p) { return lhs; } -AstNode* parse_shift_expr(Parser* p) { +static AstNode* parse_shift_expr(Parser* p) { AstNode* lhs = parse_additive_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -704,7 +704,7 @@ AstNode* parse_shift_expr(Parser* p) { return lhs; } -AstNode* parse_relational_expr(Parser* p) { +static AstNode* parse_relational_expr(Parser* p) { AstNode* lhs = parse_shift_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -725,7 +725,7 @@ AstNode* parse_relational_expr(Parser* p) { return lhs; } -AstNode* parse_equality_expr(Parser* p) { +static AstNode* parse_equality_expr(Parser* p) { AstNode* lhs = parse_relational_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -740,7 +740,7 @@ AstNode* parse_equality_expr(Parser* p) { return lhs; } -AstNode* parse_bitwise_or_expr(Parser* p) { +static AstNode* parse_bitwise_or_expr(Parser* p) { AstNode* lhs = parse_equality_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -755,7 +755,7 @@ AstNode* parse_bitwise_or_expr(Parser* p) { return lhs; } -AstNode* parse_logical_and_expr(Parser* p) { +static AstNode* parse_logical_and_expr(Parser* p) { AstNode* lhs = parse_bitwise_or_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -774,7 +774,7 @@ AstNode* parse_logical_and_expr(Parser* p) { return lhs; } -AstNode* parse_logical_or_expr(Parser* p) { +static AstNode* parse_logical_or_expr(Parser* p) { AstNode* lhs = parse_logical_and_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -793,7 +793,7 @@ AstNode* parse_logical_or_expr(Parser* p) { return lhs; } -AstNode* parse_conditional_expr(Parser* p) { +static AstNode* parse_conditional_expr(Parser* p) { AstNode* e = parse_logical_or_expr(p); if (consume_token_if(p, TokenKind_question)) { AstNode* then_expr = parse_expr(p); @@ -812,11 +812,11 @@ AstNode* parse_conditional_expr(Parser* p) { // constant-expression: // conditional-expression -AstNode* parse_constant_expression(Parser* p) { +static AstNode* parse_constant_expression(Parser* p) { return parse_conditional_expr(p); } -AstNode* parse_assignment_expr(Parser* p) { +static AstNode* parse_assignment_expr(Parser* p) { AstNode* lhs = parse_conditional_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -839,7 +839,7 @@ AstNode* parse_assignment_expr(Parser* p) { return lhs; } -AstNode* parse_comma_expr(Parser* p) { +static AstNode* parse_comma_expr(Parser* p) { AstNode* lhs = parse_assignment_expr(p); while (1) { TokenKind op = peek_token(p)->kind; @@ -857,11 +857,11 @@ AstNode* parse_comma_expr(Parser* p) { return lhs; } -AstNode* parse_expr(Parser* p) { +static AstNode* parse_expr(Parser* p) { return parse_comma_expr(p); } -AstNode* parse_return_stmt(Parser* p) { +static AstNode* parse_return_stmt(Parser* p) { expect(p, TokenKind_keyword_return); if (consume_token_if(p, TokenKind_semicolon)) { return ast_new(AstNodeKind_return_stmt); @@ -875,7 +875,7 @@ AstNode* parse_return_stmt(Parser* p) { return ret; } -AstNode* parse_if_stmt(Parser* p) { +static AstNode* parse_if_stmt(Parser* p) { expect(p, TokenKind_keyword_if); expect(p, TokenKind_paren_l); AstNode* cond = parse_expr(p); @@ -896,7 +896,7 @@ AstNode* parse_if_stmt(Parser* p) { // pointer: // '*' TODO attribute-specifier-sequence_opt TODO type-qualifier-list_opt // '*' TODO attribute-specifier-sequence_opt TODO type-qualifier-list_opt pointer -Type* parse_pointer_opt(Parser* p, Type* ty) { +static Type* parse_pointer_opt(Parser* p, Type* ty) { while (peek_token(p)->kind == TokenKind_star) { next_token(p); ty = type_new_ptr(ty); @@ -909,7 +909,7 @@ Type* parse_pointer_opt(Parser* p, Type* ty) { // TODO direct-declarator '[' 'static' type-qualifier-list_opt assignment-expression_opt ']' // TODO direct-declarator '[' type-qualifier-list 'static' assignment-expression_opt ']' // TODO direct-declarator '[' type-qualifier-list_opt '*' ']' -Type* parse_array_declarator_suffix(Parser* p, Type* ty) { +static Type* parse_array_declarator_suffix(Parser* p, Type* ty) { next_token(p); // skip '[' AstNode* size_expr = parse_expr(p); @@ -927,7 +927,7 @@ Type* parse_array_declarator_suffix(Parser* p, Type* ty) { // TODO '(' declarator ')' // array-declarator TODO attribute-specifier-sequence_opt // TODO function-declarator TODO attribute-specifier-sequence_opt -AstNode* parse_direct_declarator(Parser* p, Type* ty) { +static AstNode* parse_direct_declarator(Parser* p, Type* ty) { const char* name = parse_ident(p); while (1) { if (peek_token(p)->kind == TokenKind_bracket_l) { @@ -945,7 +945,7 @@ AstNode* parse_direct_declarator(Parser* p, Type* ty) { // declarator: // pointer_opt direct-declarator -AstNode* parse_declarator(Parser* p, Type* ty) { +static AstNode* parse_declarator(Parser* p, Type* ty) { ty = parse_pointer_opt(p, ty); return parse_direct_declarator(p, ty); } @@ -953,14 +953,14 @@ AstNode* parse_declarator(Parser* p, Type* ty) { // initializer: // assignment-expression // TODO braced-initializer -AstNode* parse_initializer(Parser* p) { +static AstNode* parse_initializer(Parser* p) { return parse_assignment_expr(p); } // init-declarator: // declarator // declarator '=' initializer -AstNode* parse_init_declarator(Parser* p, Type* ty) { +static AstNode* parse_init_declarator(Parser* p, Type* ty) { AstNode* decl = parse_declarator(p, ty); AstNode* init = NULL; if (consume_token_if(p, TokenKind_assign)) { @@ -973,7 +973,7 @@ AstNode* parse_init_declarator(Parser* p, Type* ty) { // init-declarator-list: // init-declarator // init-declarator-list ',' init-declarator -AstNode* parse_init_declarator_list(Parser* p, Type* ty) { +static AstNode* parse_init_declarator_list(Parser* p, Type* ty) { AstNode* list = ast_new_list(1); while (1) { AstNode* d = parse_init_declarator(p, ty); @@ -985,7 +985,7 @@ AstNode* parse_init_declarator_list(Parser* p, Type* ty) { return list; } -AstNode* parse_var_decl(Parser* p) { +static AstNode* parse_var_decl(Parser* p) { Type* base_ty = parse_type(p); if (type_is_unsized(base_ty)) { fatal_error("parse_var_decl: invalid type for variable"); @@ -1018,7 +1018,7 @@ AstNode* parse_var_decl(Parser* p) { return decls; } -AstNode* parse_for_stmt(Parser* p) { +static AstNode* parse_for_stmt(Parser* p) { expect(p, TokenKind_keyword_for); expect(p, TokenKind_paren_l); AstNode* init = NULL; @@ -1056,7 +1056,7 @@ AstNode* parse_for_stmt(Parser* p) { return stmt; } -AstNode* parse_while_stmt(Parser* p) { +static AstNode* parse_while_stmt(Parser* p) { expect(p, TokenKind_keyword_while); expect(p, TokenKind_paren_l); AstNode* cond = parse_expr(p); @@ -1069,7 +1069,7 @@ AstNode* parse_while_stmt(Parser* p) { return stmt; } -AstNode* parse_do_while_stmt(Parser* p) { +static AstNode* parse_do_while_stmt(Parser* p) { expect(p, TokenKind_keyword_do); AstNode* body = parse_stmt(p); expect(p, TokenKind_keyword_while); @@ -1084,19 +1084,19 @@ AstNode* parse_do_while_stmt(Parser* p) { return stmt; } -AstNode* parse_break_stmt(Parser* p) { +static AstNode* parse_break_stmt(Parser* p) { expect(p, TokenKind_keyword_break); expect(p, TokenKind_semicolon); return ast_new(AstNodeKind_break_stmt); } -AstNode* parse_continue_stmt(Parser* p) { +static AstNode* parse_continue_stmt(Parser* p) { expect(p, TokenKind_keyword_continue); expect(p, TokenKind_semicolon); return ast_new(AstNodeKind_continue_stmt); } -AstNode* parse_expr_stmt(Parser* p) { +static AstNode* parse_expr_stmt(Parser* p) { AstNode* e = parse_expr(p); expect(p, TokenKind_semicolon); AstNode* stmt = ast_new(AstNodeKind_expr_stmt); @@ -1104,7 +1104,7 @@ AstNode* parse_expr_stmt(Parser* p) { return stmt; } -AstNode* parse_block_stmt(Parser* p) { +static AstNode* parse_block_stmt(Parser* p) { AstNode* list = ast_new_list(4); expect(p, TokenKind_brace_l); enter_scope(p); @@ -1117,12 +1117,12 @@ AstNode* parse_block_stmt(Parser* p) { return list; } -AstNode* parse_empty_stmt(Parser* p) { +static AstNode* parse_empty_stmt(Parser* p) { consume_token_if(p, TokenKind_semicolon); return ast_new(AstNodeKind_nop); } -AstNode* parse_stmt(Parser* p) { +static AstNode* parse_stmt(Parser* p) { Token* t = peek_token(p); if (t->kind == TokenKind_keyword_return) { return parse_return_stmt(p); @@ -1149,20 +1149,20 @@ AstNode* parse_stmt(Parser* p) { } } -void register_params(Parser* p, AstNode* params) { +static void register_params(Parser* p, AstNode* params) { for (int i = 0; i < params->node_len; ++i) { AstNode* param = params->node_items + i; add_lvar(p, param->name, param->ty, TRUE); } } -void register_func(Parser* p, const char* name, Type* ty) { +static void register_func(Parser* p, const char* name, Type* ty) { Func* func = funcs_push_new(&p->funcs); func->name = name; func->ty = ty; } -AstNode* parse_param(Parser* p) { +static AstNode* parse_param(Parser* p) { Type* ty = parse_type(p); const char* name = NULL; TokenKind tk = peek_token(p)->kind; @@ -1177,7 +1177,7 @@ AstNode* parse_param(Parser* p) { return param; } -AstNode* parse_param_list(Parser* p) { +static AstNode* parse_param_list(Parser* p) { BOOL has_void = FALSE; AstNode* list = ast_new_list(6); while (peek_token(p)->kind != TokenKind_paren_r) { @@ -1203,7 +1203,7 @@ AstNode* parse_param_list(Parser* p) { return list; } -AstNode* parse_global_var_decl(Parser* p) { +static AstNode* parse_global_var_decl(Parser* p) { Type* base_ty = parse_type(p); if (type_is_unsized(base_ty)) { fatal_error("parse_global_var_decl: invalid type for variable"); @@ -1228,7 +1228,7 @@ AstNode* parse_global_var_decl(Parser* p) { return decls; } -AstNode* parse_func_decl_or_def(Parser* p) { +static AstNode* parse_func_decl_or_def(Parser* p) { // TODO: remove it. int start_pos = p->pos; @@ -1267,7 +1267,7 @@ AstNode* parse_func_decl_or_def(Parser* p) { return func; } -AstNode* parse_struct_member(Parser* p) { +static AstNode* parse_struct_member(Parser* p) { Type* ty = parse_type(p); const char* name = parse_ident(p); expect(p, TokenKind_semicolon); @@ -1277,7 +1277,7 @@ AstNode* parse_struct_member(Parser* p) { return member; } -AstNode* parse_struct_members(Parser* p) { +static AstNode* parse_struct_members(Parser* p) { AstNode* list = ast_new_list(4); while (peek_token(p)->kind != TokenKind_brace_r) { AstNode* member = parse_struct_member(p); @@ -1286,7 +1286,7 @@ AstNode* parse_struct_members(Parser* p) { return list; } -AstNode* parse_struct_decl_or_def(Parser* p) { +static AstNode* parse_struct_decl_or_def(Parser* p) { expect(p, TokenKind_keyword_struct); const char* name = parse_ident(p); @@ -1316,7 +1316,7 @@ AstNode* parse_struct_decl_or_def(Parser* p) { return &p->structs->node_items[struct_idx]; } -AstNode* parse_union_member(Parser* p) { +static AstNode* parse_union_member(Parser* p) { Type* ty = parse_type(p); const char* name = parse_ident(p); expect(p, TokenKind_semicolon); @@ -1326,7 +1326,7 @@ AstNode* parse_union_member(Parser* p) { return member; } -AstNode* parse_union_members(Parser* p) { +static AstNode* parse_union_members(Parser* p) { AstNode* list = ast_new_list(4); while (peek_token(p)->kind != TokenKind_brace_r) { AstNode* member = parse_union_member(p); @@ -1335,7 +1335,7 @@ AstNode* parse_union_members(Parser* p) { return list; } -AstNode* parse_union_decl_or_def(Parser* p) { +static AstNode* parse_union_decl_or_def(Parser* p) { expect(p, TokenKind_keyword_union); const char* name = parse_ident(p); @@ -1365,14 +1365,14 @@ AstNode* parse_union_decl_or_def(Parser* p) { return &p->unions->node_items[union_idx]; } -AstNode* parse_enum_member(Parser* p) { +static AstNode* parse_enum_member(Parser* p) { const char* name = parse_ident(p); AstNode* member = ast_new(AstNodeKind_enum_member); member->name = name; return member; } -AstNode* parse_enum_members(Parser* p) { +static AstNode* parse_enum_members(Parser* p) { int next_value = 0; AstNode* list = ast_new_list(16); while (peek_token(p)->kind != TokenKind_brace_r) { @@ -1387,7 +1387,7 @@ AstNode* parse_enum_members(Parser* p) { return list; } -AstNode* parse_enum_def(Parser* p) { +static AstNode* parse_enum_def(Parser* p) { expect(p, TokenKind_keyword_enum); const char* name = parse_ident(p); @@ -1413,7 +1413,7 @@ AstNode* parse_enum_def(Parser* p) { return &p->enums->node_items[enum_idx]; } -AstNode* parse_typedef_decl(Parser* p) { +static AstNode* parse_typedef_decl(Parser* p) { expect(p, TokenKind_keyword_typedef); Type* ty = parse_type(p); const char* name = parse_ident(p); @@ -1425,7 +1425,7 @@ AstNode* parse_typedef_decl(Parser* p) { return decl; } -AstNode* parse_extern_var_decl(Parser* p) { +static AstNode* parse_extern_var_decl(Parser* p) { expect(p, TokenKind_keyword_extern); Type* ty = parse_type(p); if (type_is_unsized(ty)) { @@ -1444,7 +1444,7 @@ AstNode* parse_extern_var_decl(Parser* p) { return ast_new(AstNodeKind_gvar_decl); } -AstNode* parse_toplevel(Parser* p) { +static AstNode* parse_toplevel(Parser* p) { TokenKind tk = peek_token(p)->kind; if (tk == TokenKind_keyword_struct) { return parse_struct_decl_or_def(p); @@ -1484,7 +1484,7 @@ Program* parse(TokenArray* tokens) { return prog; } -int eval(AstNode* e) { +static int eval(AstNode* e) { if (e->kind == AstNodeKind_int_expr) { return e->node_int_value; } else if (e->kind == AstNodeKind_unary_expr) { diff --git a/src/preprocess.c b/src/preprocess.c index 8c4d096..85f7320 100644 --- a/src/preprocess.c +++ b/src/preprocess.c @@ -1,5 +1,6 @@ #include "preprocess.h" #include "common.h" +#include "parse.h" #include "sys.h" const char* token_kind_stringify(TokenKind k) { @@ -345,7 +346,7 @@ struct Macro { }; typedef struct Macro Macro; -int macro_find_param(Macro* macro, Token* tok) { +static int macro_find_param(Macro* macro, Token* tok) { if (tok->kind != TokenKind_ident) return -1; @@ -364,7 +365,7 @@ struct MacroArray { }; typedef struct MacroArray MacroArray; -MacroArray* macros_new() { +static MacroArray* macros_new() { MacroArray* macros = calloc(1, sizeof(MacroArray)); macros->len = 0; macros->capacity = 8; @@ -372,7 +373,7 @@ MacroArray* macros_new() { return macros; } -void macros_reserve(MacroArray* macros, size_t size) { +static void macros_reserve(MacroArray* macros, size_t size) { if (size <= macros->capacity) return; while (macros->capacity < size) { @@ -382,12 +383,12 @@ void macros_reserve(MacroArray* macros, size_t size) { memset(macros->data + macros->len, 0, (macros->capacity - macros->len) * sizeof(Macro)); } -Macro* macros_push_new(MacroArray* macros) { +static Macro* macros_push_new(MacroArray* macros) { macros_reserve(macros, macros->len + 1); return ¯os->data[macros->len++]; } -void macros_dump(MacroArray* macros) { +static void macros_dump(MacroArray* macros) { fprintf(stderr, "MacroArray {\n"); fprintf(stderr, " len = %zu\n", macros->len); fprintf(stderr, " data = [\n"); @@ -403,7 +404,7 @@ void macros_dump(MacroArray* macros) { fprintf(stderr, "}\n"); } -void add_predefined_macros(MacroArray* macros) { +static void add_predefined_macros(MacroArray* macros) { Macro* m; m = macros_push_new(macros); @@ -435,7 +436,7 @@ struct MacroArgArray { }; typedef struct MacroArgArray MacroArgArray; -MacroArgArray* macroargs_new() { +static MacroArgArray* macroargs_new() { MacroArgArray* macroargs = calloc(1, sizeof(MacroArgArray)); macroargs->len = 0; macroargs->capacity = 2; @@ -443,7 +444,7 @@ MacroArgArray* macroargs_new() { return macroargs; } -void macroargs_reserve(MacroArgArray* macroargs, size_t size) { +static void macroargs_reserve(MacroArgArray* macroargs, size_t size) { if (size <= macroargs->capacity) return; while (macroargs->capacity < size) { @@ -453,7 +454,7 @@ void macroargs_reserve(MacroArgArray* macroargs, size_t size) { memset(macroargs->data + macroargs->len, 0, (macroargs->capacity - macroargs->len) * sizeof(MacroArg)); } -MacroArg* macroargs_push_new(MacroArgArray* macroargs) { +static MacroArg* macroargs_push_new(MacroArgArray* macroargs) { macroargs_reserve(macroargs, macroargs->len + 1); return ¯oargs->data[macroargs->len++]; } @@ -466,7 +467,7 @@ struct PpLexer { }; typedef struct PpLexer PpLexer; -PpLexer* pplexer_new(InFile* src) { +static PpLexer* pplexer_new(InFile* src) { PpLexer* ppl = calloc(1, sizeof(PpLexer)); ppl->src = src; @@ -478,7 +479,7 @@ PpLexer* pplexer_new(InFile* src) { return ppl; } -TokenKind pplexer_tokenize_pp_directive(PpLexer* ppl) { +static TokenKind pplexer_tokenize_pp_directive(PpLexer* ppl) { // Skip whitespaces after '#'. char c; while (isspace((c = infile_peek_char(ppl->src)))) { @@ -538,7 +539,7 @@ TokenKind pplexer_tokenize_pp_directive(PpLexer* ppl) { } } -void pplexer_tokenize_all(PpLexer* ppl) { +static void pplexer_tokenize_all(PpLexer* ppl) { while (!infile_eof(ppl->src)) { Token* tok = tokens_push_new(ppl->pp_tokens); tok->loc = ppl->src->loc; @@ -836,7 +837,7 @@ void pplexer_tokenize_all(PpLexer* ppl) { eof_tok->kind = TokenKind_eof; } -TokenArray* pp_tokenize(InFile* src) { +static TokenArray* pp_tokenize(InFile* src) { PpLexer* ppl = pplexer_new(src); pplexer_tokenize_all(ppl); return ppl->pp_tokens; @@ -853,9 +854,9 @@ struct Preprocessor { }; typedef struct Preprocessor Preprocessor; -TokenArray* do_preprocess(InFile* src, int depth, MacroArray* macros); +static TokenArray* do_preprocess(InFile* src, int depth, MacroArray* macros); -Preprocessor* preprocessor_new(TokenArray* pp_tokens, int include_depth, MacroArray* macros) { +static Preprocessor* preprocessor_new(TokenArray* pp_tokens, int include_depth, MacroArray* macros) { if (include_depth >= 32) { fatal_error("include depth limit exceeded"); } @@ -869,23 +870,23 @@ Preprocessor* preprocessor_new(TokenArray* pp_tokens, int include_depth, MacroAr return pp; } -Token* pp_token_at(Preprocessor* pp, int i) { +static Token* pp_token_at(Preprocessor* pp, int i) { return &pp->pp_tokens->data[i]; } -Token* peek_pp_token(Preprocessor* pp) { +static Token* peek_pp_token(Preprocessor* pp) { return pp_token_at(pp, pp->pos); } -Token* next_pp_token(Preprocessor* pp) { +static Token* next_pp_token(Preprocessor* pp) { return pp_token_at(pp, pp->pos++); } -BOOL pp_eof(Preprocessor* pp) { +static BOOL pp_eof(Preprocessor* pp) { return peek_pp_token(pp)->kind == TokenKind_eof; } -int find_macro(Preprocessor* pp, const char* name) { +static int find_macro(Preprocessor* pp, const char* name) { for (int i = 0; i < pp->macros->len; ++i) { if (pp->macros->data[i].kind == MacroKind_undef) continue; @@ -896,28 +897,28 @@ int find_macro(Preprocessor* pp, const char* name) { return -1; } -void undef_macro(Preprocessor* pp, int idx) { +static void undef_macro(Preprocessor* pp, int idx) { pp->macros->data[idx].kind = MacroKind_undef; // TODO: Can predefined macro like __FILE__ be undefined? } -void add_include_path(Preprocessor* pp, char* include_path) { +static void add_include_path(Preprocessor* pp, char* include_path) { pp->include_paths[pp->n_include_paths] = include_path; ++pp->n_include_paths; } -BOOL skip_pp_tokens(Preprocessor* pp) { +static BOOL skip_pp_tokens(Preprocessor* pp) { // TODO: support nested #if return pp->skip_pp_tokens; } -void skip_whitespaces(Preprocessor* pp) { +static void skip_whitespaces(Preprocessor* pp) { while (!pp_eof(pp) && peek_pp_token(pp)->kind == TokenKind_whitespace) { next_pp_token(pp); } } -void seek_to_next_newline(Preprocessor* pp) { +static void seek_to_next_newline(Preprocessor* pp) { while (!pp_eof(pp)) { Token* tok = peek_pp_token(pp); if (tok->kind == TokenKind_newline) { @@ -927,38 +928,37 @@ void seek_to_next_newline(Preprocessor* pp) { } } -void make_token_whitespace(Token* tok) { +static void make_token_whitespace(Token* tok) { tok->kind = TokenKind_whitespace; tok->value.string = NULL; } -void remove_directive_tokens(Preprocessor* pp, int start, int end) { +static void remove_directive_tokens(Preprocessor* pp, int start, int end) { for (int i = start; i < end; ++i) { make_token_whitespace(pp_token_at(pp, i)); } } -void process_endif_directive(Preprocessor* pp, int directive_token_pos) { +static void process_endif_directive(Preprocessor* pp, int directive_token_pos) { next_pp_token(pp); pp->skip_pp_tokens = FALSE; remove_directive_tokens(pp, directive_token_pos, pp->pos); } -void process_else_directive(Preprocessor* pp, int directive_token_pos) { +static void process_else_directive(Preprocessor* pp, int directive_token_pos) { next_pp_token(pp); pp->skip_pp_tokens = !pp->skip_pp_tokens; remove_directive_tokens(pp, directive_token_pos, pp->pos); } -void process_elif_directive(Preprocessor* pp, int directive_token_pos) { +static void process_elif_directive(Preprocessor* pp, int directive_token_pos) { unimplemented(); } -BOOL pp_eval_constant_expression(TokenArray*); -int replace_pp_tokens(Preprocessor*, int, int, TokenArray*); -BOOL expand_macro(Preprocessor*); +static int replace_pp_tokens(Preprocessor*, int, int, TokenArray*); +static BOOL expand_macro(Preprocessor*); -void process_if_directive(Preprocessor* pp, int directive_token_pos) { +static void process_if_directive(Preprocessor* pp, int directive_token_pos) { next_pp_token(pp); int condition_expression_start_pos = pp->pos; @@ -1041,7 +1041,7 @@ void process_if_directive(Preprocessor* pp, int directive_token_pos) { remove_directive_tokens(pp, directive_token_pos, pp->pos); } -void process_ifdef_directive(Preprocessor* pp, int directive_token_pos) { +static void process_ifdef_directive(Preprocessor* pp, int directive_token_pos) { next_pp_token(pp); skip_whitespaces(pp); Token* macro_name = peek_pp_token(pp); @@ -1052,7 +1052,7 @@ void process_ifdef_directive(Preprocessor* pp, int directive_token_pos) { remove_directive_tokens(pp, directive_token_pos, pp->pos); } -void process_ifndef_directive(Preprocessor* pp, int directive_token_pos) { +static void process_ifndef_directive(Preprocessor* pp, int directive_token_pos) { next_pp_token(pp); skip_whitespaces(pp); Token* macro_name = peek_pp_token(pp); @@ -1063,7 +1063,7 @@ void process_ifndef_directive(Preprocessor* pp, int directive_token_pos) { remove_directive_tokens(pp, directive_token_pos, pp->pos); } -Token* read_include_header_name(Preprocessor* pp) { +static Token* read_include_header_name(Preprocessor* pp) { Token* tok = next_pp_token(pp); if (tok->kind != TokenKind_header_name) { fatal_error("%s:%d: invalid #include", tok->loc.filename, tok->loc.line); @@ -1071,7 +1071,7 @@ Token* read_include_header_name(Preprocessor* pp) { return tok; } -const char* resolve_include_name(Preprocessor* pp, const Token* include_name_token) { +static const char* resolve_include_name(Preprocessor* pp, const Token* include_name_token) { const char* include_name = include_name_token->value.string; if (include_name[0] == '"') { char* current_filename = strdup(include_name_token->loc.filename); @@ -1091,7 +1091,7 @@ const char* resolve_include_name(Preprocessor* pp, const Token* include_name_tok } } -int replace_pp_tokens(Preprocessor* pp, int dest_start, int dest_end, TokenArray* source_tokens) { +static int replace_pp_tokens(Preprocessor* pp, int dest_start, int dest_end, TokenArray* source_tokens) { int n_tokens_to_remove = dest_end - dest_start; int n_tokens_after_dest = pp->pp_tokens->len - dest_end; int shift_amount; @@ -1117,14 +1117,14 @@ int replace_pp_tokens(Preprocessor* pp, int dest_start, int dest_end, TokenArray return dest_start + source_tokens->len; } -int replace_single_pp_token(Preprocessor* pp, int dest, Token* source_tok) { +static int replace_single_pp_token(Preprocessor* pp, int dest, Token* source_tok) { TokenArray tokens; tokens_init(&tokens, 1); *tokens_push_new(&tokens) = *source_tok; replace_pp_tokens(pp, dest, dest + 1, &tokens); } -void expand_include_directive(Preprocessor* pp, int directive_token_pos, const char* include_name) { +static void expand_include_directive(Preprocessor* pp, int directive_token_pos, const char* include_name) { InFile* include_source = infile_open(include_name); if (!include_source) { fatal_error("cannot open include file: %s", include_name); @@ -1135,7 +1135,7 @@ void expand_include_directive(Preprocessor* pp, int directive_token_pos, const c pp->pos = replace_pp_tokens(pp, directive_token_pos, pp->pos, include_pp_tokens); } -void process_include_directive(Preprocessor* pp, int directive_token_pos) { +static void process_include_directive(Preprocessor* pp, int directive_token_pos) { next_pp_token(pp); skip_whitespaces(pp); Token* include_name = read_include_header_name(pp); @@ -1148,7 +1148,7 @@ void process_include_directive(Preprocessor* pp, int directive_token_pos) { // ws ::= many0(<whitespace>) // macro-parameters ::= '(' <ws> opt(<identifier> <ws> many0(',' <ws> <identifier> <ws>)) ')' -TokenArray* pp_parse_macro_parameters(Preprocessor* pp) { +static TokenArray* pp_parse_macro_parameters(Preprocessor* pp) { TokenArray* parameters = calloc(1, sizeof(TokenArray)); tokens_init(parameters, 2); @@ -1176,7 +1176,7 @@ TokenArray* pp_parse_macro_parameters(Preprocessor* pp) { return parameters; } -void process_define_directive(Preprocessor* pp, int directive_token_pos) { +static void process_define_directive(Preprocessor* pp, int directive_token_pos) { next_pp_token(pp); skip_whitespaces(pp); Token* macro_name = next_pp_token(pp); @@ -1220,7 +1220,7 @@ void process_define_directive(Preprocessor* pp, int directive_token_pos) { remove_directive_tokens(pp, directive_token_pos, pp->pos); } -void process_undef_directive(Preprocessor* pp, int directive_token_pos) { +static void process_undef_directive(Preprocessor* pp, int directive_token_pos) { next_pp_token(pp); skip_whitespaces(pp); Token* macro_name = peek_pp_token(pp); @@ -1234,21 +1234,21 @@ void process_undef_directive(Preprocessor* pp, int directive_token_pos) { remove_directive_tokens(pp, directive_token_pos, pp->pos); } -void process_line_directive(Preprocessor* pp, int directive_token_pos) { +static void process_line_directive(Preprocessor* pp, int directive_token_pos) { unimplemented(); } -void process_error_directive(Preprocessor* pp, int directive_token_pos) { +static void process_error_directive(Preprocessor* pp, int directive_token_pos) { unimplemented(); } -void process_pragma_directive(Preprocessor* pp, int directive_token_pos) { +static void process_pragma_directive(Preprocessor* pp, int directive_token_pos) { unimplemented(); } // ws ::= many0(<Whitespace>) // macro-arguments ::= '(' <ws> opt(<any-token> <ws> many0(',' <ws> <any-token> <ws>)) ')' -MacroArgArray* pp_parse_macro_arguments(Preprocessor* pp) { +static MacroArgArray* pp_parse_macro_arguments(Preprocessor* pp) { MacroArgArray* args = macroargs_new(); Token* tok = next_pp_token(pp); @@ -1279,7 +1279,7 @@ MacroArgArray* pp_parse_macro_arguments(Preprocessor* pp) { return args; } -BOOL expand_macro(Preprocessor* pp) { +static BOOL expand_macro(Preprocessor* pp) { int macro_name_pos = pp->pos; Token* macro_name = next_pp_token(pp); int macro_idx = find_macro(pp, macro_name->value.string); @@ -1329,7 +1329,7 @@ BOOL expand_macro(Preprocessor* pp) { return TRUE; } -void process_pp_directive(Preprocessor* pp) { +static void process_pp_directive(Preprocessor* pp) { int first_token_pos = pp->pos; Token* tok = peek_pp_token(pp); if (tok->kind == TokenKind_pp_directive_endif) { @@ -1371,13 +1371,13 @@ void process_pp_directive(Preprocessor* pp) { } } -void process_pp_directives(Preprocessor* pp) { +static void process_pp_directives(Preprocessor* pp) { while (!pp_eof(pp)) { process_pp_directive(pp); } } -void pp_dump(Token* t, BOOL include_whitespace) { +static void pp_dump(Token* t, BOOL include_whitespace) { for (; t->kind != TokenKind_eof; ++t) { if (t->kind == TokenKind_whitespace && !include_whitespace) { continue; @@ -1386,14 +1386,14 @@ void pp_dump(Token* t, BOOL include_whitespace) { } } -char* get_ducc_include_path() { +static char* get_ducc_include_path() { const char* self_dir = get_self_dir(); char* buf = calloc(strlen(self_dir) + strlen("/../include") + 1, sizeof(char)); sprintf(buf, "%s/../include", self_dir); return buf; } -TokenArray* do_preprocess(InFile* src, int depth, MacroArray* macros) { +static TokenArray* do_preprocess(InFile* src, int depth, MacroArray* macros) { TokenArray* pp_tokens = pp_tokenize(src); Preprocessor* pp = preprocessor_new(pp_tokens, depth, macros); add_include_path(pp, get_ducc_include_path()); @@ -1,7 +1,7 @@ #include "sys.h" #include "std.h" -char* get_self_path() { +static char* get_self_path() { char* buf = calloc(PATH_MAX, sizeof(char)); ssize_t len = readlink("/proc/self/exe", buf, PATH_MAX - 1); if (len == -1 || len == PATH_MAX - 1) { diff --git a/src/tokenize.c b/src/tokenize.c index 0fb126f..352755b 100644 --- a/src/tokenize.c +++ b/src/tokenize.c @@ -7,7 +7,7 @@ struct Lexer { }; typedef struct Lexer Lexer; -Lexer* lexer_new(TokenArray* pp_tokens) { +static Lexer* lexer_new(TokenArray* pp_tokens) { Lexer* l = calloc(1, sizeof(Lexer)); l->src = pp_tokens; l->tokens = calloc(1, sizeof(TokenArray)); @@ -16,7 +16,7 @@ Lexer* lexer_new(TokenArray* pp_tokens) { return l; } -void tokenize_all(Lexer* l) { +static void tokenize_all(Lexer* l) { for (int pos = 0; pos < l->src->len; ++pos) { Token* pp_tok = &l->src->data[pos]; TokenKind k = pp_tok->kind; |
