Skip to content

Commit 018e61b

Browse files
committed
Fix inline subs not releasing their stack slots for reuse
1 parent 90de9d9 commit 018e61b

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

thecl/ecsparse.y

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,8 +1793,10 @@ static void instr_create_inline_call(
17931793
* gets written to, or the passed parameter is an expression.
17941794
* We will use a param_replace array to replace all argument variable references from the code of copied inline sub. */
17951795
thecl_param_t** param_replace = calloc(sub->arity, sizeof(thecl_param_t*));
1796+
thecl_variable_t** param_replace_vars = malloc(sub->arity * sizeof(thecl_variable_t*));
17961797
thecl_variable_t* var;
17971798
i = 0;
1799+
size_t replaced_params = 0;
17981800

17991801
list_for_each(params, param) { /* It has alredy been verified that param amount is correct. */
18001802
var = sub->vars[i];
@@ -1818,6 +1820,7 @@ static void instr_create_inline_call(
18181820
strcpy(buf, name);
18191821
strcat(buf, var->name);
18201822
thecl_variable_t* var = var_create(state, state->current_sub, buf, param->type);
1823+
param_replace_vars[replaced_params++] = var;
18211824
thecl_param_t* new_param = param_new(param->type);
18221825
new_param->stack = 1;
18231826
if (new_param->type == 'S')
@@ -1859,6 +1862,7 @@ static void instr_create_inline_call(
18591862
thecl_variable_t* var_new = var_create(state, state->current_sub, buf, var->type);
18601863
stack_replace[i - sub->arity] = var_new;
18611864
}
1865+
size_t stack_replace_count = (sub->stack / 4) - sub->arity;
18621866

18631867
/* Temprary label list that modifications will be apply to when needed.
18641868
* Content of this list will be later copied into the sub that created the inline call. */
@@ -1997,10 +2001,12 @@ static void instr_create_inline_call(
19972001

19982002
scope_finish(state);
19992003

2000-
/* We have to mark variables that were marked as unused in the inline sub
2001-
* as unused in the current sub as well. */
2002-
for (size_t v=sub->arity; v<sub->var_count; ++v) {
2003-
stack_replace[v - sub->arity]->is_unused = sub->vars[v]->is_unused;
2004+
/* Allow next created vars to reuse stack offsets. */
2005+
for (size_t v=0; v<stack_replace_count; ++v) {
2006+
stack_replace[v]->is_unused = true;
2007+
}
2008+
for (size_t v=0; v<replaced_params; ++v) {
2009+
param_replace_vars[v]->is_unused = true;
20042010
}
20052011

20062012
/* Free stuff. */
@@ -2015,6 +2021,7 @@ static void instr_create_inline_call(
20152021
if (params_org == NULL)
20162022
free(params);
20172023
free(param_replace);
2024+
free(param_replace_vars);
20182025
free(stack_replace);
20192026
}
20202027

0 commit comments

Comments
 (0)