aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-08-18 03:59:58 +0900
committernsfisis <nsfisis@gmail.com>2025-08-18 03:59:58 +0900
commit991245b69c53b1e2f708af34b6f829182ebf5cf2 (patch)
tree59ac5aa154ed0c60d036bacba1fdb9171f18fb94
parent2a7e1667f70c5381d3b939324cc647e51134b15c (diff)
downloadducc-991245b69c53b1e2f708af34b6f829182ebf5cf2.tar.gz
ducc-991245b69c53b1e2f708af34b6f829182ebf5cf2.tar.zst
ducc-991245b69c53b1e2f708af34b6f829182ebf5cf2.zip
refactor: define va_start() as macro instead of special form
-rw-r--r--codegen.c6
-rw-r--r--include/stdarg.h9
-rw-r--r--parse.c4
-rw-r--r--preprocess.c5
-rw-r--r--tests/063.sh23
-rw-r--r--tokenize.c3
6 files changed, 12 insertions, 38 deletions
diff --git a/codegen.c b/codegen.c
index 4fdfe46..7b9e88c 100644
--- a/codegen.c
+++ b/codegen.c
@@ -297,8 +297,8 @@ void codegen_assign_expr(CodeGen* g, AstNode* ast) {
void codegen_func_call(CodeGen* g, AstNode* ast) {
const char* func_name = ast->name;
- if (strcmp(func_name, "va_start") == 0) {
- printf(" # va_start BEGIN\n");
+ if (strcmp(func_name, "__ducc_va_start") == 0) {
+ printf(" # __ducc_va_start BEGIN\n");
for (int i = 0; i < 6; ++i) {
printf(" mov rax, %s\n", param_reg(i));
printf(" mov [rbp-%d], rax\n", 8 + (LVAR_MAX - 4 - i) * 8);
@@ -315,7 +315,7 @@ void codegen_func_call(CodeGen* g, AstNode* ast) {
printf(" mov rdi, rbp\n");
printf(" sub rdi, %d\n", 8 + (LVAR_MAX - 4) * 8);
printf(" mov QWORD PTR [rax+16], rdi\n");
- printf(" # va_start END\n");
+ printf(" # __ducc_va_start END\n");
return;
}
diff --git a/include/stdarg.h b/include/stdarg.h
index 43138b6..bdc4101 100644
--- a/include/stdarg.h
+++ b/include/stdarg.h
@@ -3,7 +3,7 @@
// (With LP64 and ILP32 Programming Models)
// Version 1.0
// Figure 3.34: va_list Type Declaration
-struct __va_list__ {
+struct __ducc_va_list {
// unsigned int gp_offset;
// unsigned int fp_offset;
int gp_offset;
@@ -12,9 +12,10 @@ struct __va_list__ {
void* reg_save_area;
};
// ducc currently does not support array type.
-// typedef struct __va_list__ va_list[1];
-typedef struct __va_list__* va_list;
+// typedef struct __ducc_va_list va_list[1];
+typedef struct __ducc_va_list* va_list;
+
+#define va_start(args, start) __ducc_va_start(args, start)
-// va_start() is currently implemented as a special form due to the limitation of #define macro.
void va_end(va_list args) {
}
diff --git a/parse.c b/parse.c
index 1e27b5a..7262c6c 100644
--- a/parse.c
+++ b/parse.c
@@ -60,7 +60,7 @@ Parser* parser_new(TokenArray* tokens) {
p->typedefs = calloc(64, sizeof(AstNode));
p->str_literals = calloc(1024, sizeof(char*));
- p->funcs[p->n_funcs].name = "va_start";
+ p->funcs[p->n_funcs].name = "__ducc_va_start";
p->funcs[p->n_funcs].ty = calloc(1, sizeof(Type));
p->funcs[p->n_funcs].ty->kind = TypeKind_void;
++p->n_funcs;
@@ -285,7 +285,7 @@ AstNode* parse_primary_expr(Parser* p) {
AstNode* e = parse_expr(p);
expect(p, TokenKind_paren_r);
return e;
- } else if (t->kind == TokenKind_ident || t->kind == TokenKind_va_start) {
+ } else if (t->kind == TokenKind_ident) {
const char* name = t->value.string;
if (peek_token(p)->kind == TokenKind_paren_l) {
diff --git a/preprocess.c b/preprocess.c
index 056422f..efa12b2 100644
--- a/preprocess.c
+++ b/preprocess.c
@@ -108,9 +108,6 @@ enum TokenKind {
TokenKind_star,
TokenKind_tilde,
TokenKind_xor,
-
- // va_start() is currently implemented as a special form due to the current limitation of #define macro.
- TokenKind_va_start,
};
typedef enum TokenKind TokenKind;
@@ -327,8 +324,6 @@ const char* token_kind_stringify(TokenKind k) {
return "~";
else if (k == TokenKind_xor)
return "^";
- else if (k == TokenKind_va_start)
- return "va_start";
else
unreachable();
}
diff --git a/tests/063.sh b/tests/063.sh
index f5f9505..a3095e8 100644
--- a/tests/063.sh
+++ b/tests/063.sh
@@ -6,28 +6,9 @@ cat <<'EOF' > expected
EOF
bash ../../test_diff.sh <<'EOF'
-int fprintf();
+#include <stdarg.h>
-// System V Application Binary Interface
-// AMD64 Architecture Processor Supplement
-// (With LP64 and ILP32 Programming Models)
-// Version 1.0
-// Figure 3.34: va_list Type Declaration
-struct __va_list__ {
- // unsigned int gp_offset;
- // unsigned int fp_offset;
- int gp_offset;
- int fp_offset;
- void* overflow_arg_area;
- void* reg_save_area;
-};
-// ducc currently does not support array type.
-// typedef struct __va_list__ va_list[1];
-typedef struct __va_list__* va_list;
-
-// va_start() is currently implemented as a special form due to the limitation of #define macro.
-void va_end(va_list args) {
-}
+int fprintf();
struct FILE;
typedef struct FILE FILE;
diff --git a/tokenize.c b/tokenize.c
index d6bd7a6..9b4042d 100644
--- a/tokenize.c
+++ b/tokenize.c
@@ -121,9 +121,6 @@ void tokenize_all(Lexer* l) {
tok->kind = TokenKind_keyword__Complex;
} else if (strcmp(pp_tok->value.string, "_Imaginary") == 0) {
tok->kind = TokenKind_keyword__Imaginary;
- } else if (strcmp(pp_tok->value.string, "va_start") == 0) {
- tok->kind = TokenKind_va_start;
- tok->value = pp_tok->value;
} else {
tok->kind = TokenKind_ident;
tok->value = pp_tok->value;