aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-08-25 00:31:25 +0900
committernsfisis <nsfisis@gmail.com>2025-08-25 00:41:12 +0900
commit7f29d50e4558a700b7611dc72e87e7922ac6a345 (patch)
treeb4a8b7c41457d209eb91e29f23044610d6b4b700 /src
parent36da139565202a9a39d1e4261ab8bf950041518b (diff)
downloadducc-7f29d50e4558a700b7611dc72e87e7922ac6a345.tar.gz
ducc-7f29d50e4558a700b7611dc72e87e7922ac6a345.tar.zst
ducc-7f29d50e4558a700b7611dc72e87e7922ac6a345.zip
feat: support static function
Diffstat (limited to 'src')
-rw-r--r--src/ast.h2
-rw-r--r--src/codegen.c4
-rw-r--r--src/parse.c14
3 files changed, 17 insertions, 3 deletions
diff --git a/src/ast.h b/src/ast.h
index 0711e44..90fdeb0 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -29,6 +29,7 @@ struct Type {
struct Type* base;
int array_size;
struct AstNode* def;
+ BOOL is_static;
};
typedef struct Type Type;
@@ -118,6 +119,7 @@ typedef enum AstNodeKind AstNodeKind;
#define node_op __i1
#define node_stack_offset __i1
#define node_stack_size __i1
+#define node_function_is_static __i2
struct AstNode {
AstNodeKind kind;
diff --git a/src/codegen.c b/src/codegen.c
index da2cd17..82c9c96 100644
--- a/src/codegen.c
+++ b/src/codegen.c
@@ -546,7 +546,9 @@ void codegen_stmt(CodeGen* g, AstNode* ast) {
void codegen_func(CodeGen* g, AstNode* ast) {
g->current_func = ast;
- fprintf(g->out, ".globl %s\n", ast->name);
+ if (!ast->node_function_is_static) {
+ fprintf(g->out, ".globl %s\n", ast->name);
+ }
fprintf(g->out, "%s:\n", ast->name);
codegen_func_prologue(g, ast);
diff --git a/src/parse.c b/src/parse.c
index 882d769..7750f40 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -519,8 +519,16 @@ BOOL is_type_token(Parser* p, Token* token) {
Type* parse_type(Parser* p) {
Token* t = next_token(p);
- if (t->kind == TokenKind_keyword_const || t->kind == TokenKind_keyword_static) {
- t = next_token(p);
+ BOOL has_static = FALSE;
+ while (1) {
+ if (t->kind == TokenKind_keyword_const) {
+ t = next_token(p);
+ } else if (t->kind == TokenKind_keyword_static) {
+ t = next_token(p);
+ has_static = TRUE;
+ } else {
+ break;
+ }
}
if (!is_type_token(p, t)) {
fatal_error("%s:%d: parse_type: expected type, but got '%s'", t->loc.filename, t->loc.line, token_stringify(t));
@@ -578,6 +586,7 @@ Type* parse_type(Parser* p) {
}
ty = type_new_ptr(ty);
}
+ ty->is_static = has_static;
return ty;
}
@@ -1254,6 +1263,7 @@ AstNode* parse_func_decl_or_def(Parser* p) {
func->node_stack_size =
p->lvars.data[p->lvars.len - 1].stack_offset + type_sizeof(p->lvars.data[p->lvars.len - 1].ty);
}
+ func->node_function_is_static = ty->is_static;
return func;
}