aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-05-03 17:17:44 +0900
committernsfisis <nsfisis@gmail.com>2025-05-03 17:17:44 +0900
commit374076ce1e9edce0469847c1615be21d100275cd (patch)
tree8c84f606aaee324c6df181c051cb1c38613bd720
parent0b4298dd860e7e57a2964828fd8473e7b9cb80a7 (diff)
downloadP4Dcc-374076ce1e9edce0469847c1615be21d100275cd.tar.gz
P4Dcc-374076ce1e9edce0469847c1615be21d100275cd.tar.zst
P4Dcc-374076ce1e9edce0469847c1615be21d100275cd.zip
increase maximum number of local variables
-rw-r--r--main.c17
-rw-r--r--tests/009.sh37
2 files changed, 48 insertions, 6 deletions
diff --git a/main.c b/main.c
index 2239fb6..d2018ec 100644
--- a/main.c
+++ b/main.c
@@ -312,10 +312,16 @@ AST* ast_new_assign_expr(int op, AST* lhs, AST* rhs) {
return e;
}
+#define LVAR_MAX 32
+
+typedef struct LVar {
+ char* name;
+} LVAR;
+
typedef struct Parser {
TOKEN* tokens;
int pos;
- char** locals;
+ LVAR* locals;
int n_locals;
} PARSER;
@@ -352,7 +358,7 @@ TOKEN* expect(PARSER* p, int expected) {
int parse_find_lvar(PARSER* p, char* name) {
int i;
for (i = 0; i < p->n_locals; i++) {
- if (strcmp(p->locals[i], name) == 0) {
+ if (strcmp(p->locals[i].name, name) == 0) {
return i;
}
}
@@ -520,7 +526,7 @@ AST* parse_var_decl(PARSER* p) {
sprintf(buf, "parse_var_decl: %s redeclared", name);
fatal_error(buf);
}
- p->locals[p->n_locals] = name;
+ p->locals[p->n_locals].name = name;
p->n_locals += 1;
return decl;
@@ -561,7 +567,7 @@ AST* parse_block_stmt(PARSER* p) {
}
void parse_enter_func(PARSER* p) {
- p->locals = calloc(32, sizeof(TOKEN*));
+ p->locals = calloc(LVAR_MAX, sizeof(LVAR));
p->n_locals = 0;
}
@@ -623,8 +629,7 @@ void gen_func_prologue(CODEGEN* g, AST* ast) {
printf(" # gen_func_prologue\n");
printf(" push rbp\n");
printf(" mov rbp, rsp\n");
- // TODO 16
- printf(" sub rsp, 16\n");
+ printf(" sub rsp, %d\n", 8 * LVAR_MAX);
}
void gen_func_epilogue(CODEGEN* g, AST* ast) {
diff --git a/tests/009.sh b/tests/009.sh
new file mode 100644
index 0000000..5ebcd0a
--- /dev/null
+++ b/tests/009.sh
@@ -0,0 +1,37 @@
+set -e
+
+bash ../../test_exit_code.sh 45 <<'EOF'
+int main() {
+ int a1;
+ int a2;
+ int a3;
+ int a4;
+ int a5;
+ int a6;
+ int a7;
+ int a8;
+ int a9;
+
+ a1 = 1;
+ a2 = 2;
+ a3 = 3;
+ a4 = 4;
+ a5 = 5;
+ a6 = 6;
+ a7 = 7;
+ a8 = 8;
+ a9 = 9;
+
+ return
+ a1 +
+ a2 +
+ a3 +
+ a4 +
+ a5 +
+ a6 +
+ a7 +
+ a8 +
+ a9 +
+ 0;
+}
+EOF