From 0fa21580fa6d673bcf221d5942ce8fc26bb5448b Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 9 Oct 2019 00:29:14 -0400 Subject: Fix function argument offsets --- pc.y | 4 ++++ tree.c | 26 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pc.y b/pc.y index d891260..1de6065 100644 --- a/pc.y +++ b/pc.y @@ -247,6 +247,7 @@ sub_prog_head tmp->var_type = $5; } + update_offsets($3, -1); free_tree($3); @@ -275,6 +276,8 @@ sub_prog_head assert(tmp->func_info->argv = malloc(i * sizeof(int))); assert(!set_func_types($3, tmp->func_info->argv, i)); + + update_offsets($3, -1); free_tree($3); $$ = mktree(PROC, mkid(tmp), NULL); @@ -284,6 +287,7 @@ sub_prog_head arguments :'(' param_list ')' { + cur_scope->offset -= count_args($2); $$ = $2; } |/*empty*/{ diff --git a/tree.c b/tree.c index dabbb39..92a45df 100644 --- a/tree.c +++ b/tree.c @@ -61,6 +61,28 @@ ptree *l, *r; return p; } +int update_offsets(list, i) +ptree *list; +int i; +{ + if (!list) { + return i; + } + + if (list->type != LIST) { + if (list->type == ID && list->attr.nval) { + list->attr.nval->offset = i--; + return i; + } else { + yyerror("updating offsets\n"); + } + } + + i = update_offsets(list->l, i); + i = update_offsets(list->r, i); + return i; +} + void update_type_info(list, t) ptree *list, *t; { @@ -189,7 +211,7 @@ int spaces; if (t->attr.nval->func_info) fprintf(stderr, "\t %d", t->attr.nval->func_info->argc); - fprintf(stderr, "]"); + fprintf(stderr, "] O: %d", t->attr.nval->offset); break; case INUM: fprintf(stderr, "[INUM: %d]", t->attr.ival); @@ -238,7 +260,7 @@ int spaces; fprintf(stderr, "[?: %d]", t->type); yyerror("Error in tree_print"); } - fprintf(stderr," %d: L %d\n", t->ret_type, t->label); + fprintf(stderr,", T: %d, L: %d\n", t->ret_type, t->label); aux_tree_print(t->l, spaces + 2); aux_tree_print(t->r, spaces + 2); } -- cgit v1.1