Filter: fixed excessive stack allocation in functions with args but no local vars

This commit is contained in:
Maria Matejka 2019-07-15 15:06:52 +02:00
parent 0da06b7103
commit c29d73a06a

View file

@ -453,7 +453,7 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
%type <f> filter where_filter %type <f> filter where_filter
%type <fl> filter_body function_body %type <fl> filter_body function_body
%type <flv> lvalue %type <flv> lvalue
%type <i> type function_params declsn decls %type <i> type function_args function_vars
%type <ecs> ec_kind %type <ecs> ec_kind
%type <fret> break_command %type <fret> break_command
%type <i32> cnum %type <i32> cnum
@ -553,25 +553,30 @@ type:
} }
; ;
/* Declarations with ';' at the end */ function_argsn:
decls: /* EMPTY */
/* EMPTY */ { $$ = 0; } | function_argsn type CF_SYM_VOID ';' {
| declsn ';' { $$ = $1; } if ($3->scope->slots >= 0xfe) cf_error("Too many declarations, at most 255 allowed");
cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++);
}
; ;
/* Declarations that have no ';' at the end. */ function_args:
declsn: '(' ')' { $$ = 0; }
type CF_SYM_VOID { | '(' function_argsn type CF_SYM_VOID ')' {
cf_define_symbol($2, SYM_VARIABLE | $1, offset, $2->scope->slots++);
$$ = $2->scope->slots;
}
| declsn ';' type CF_SYM_VOID {
if ($4->scope->slots >= 0xff) cf_error("Too many declarations, at most 255 allowed");
cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++); cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++);
$$ = $4->scope->slots; $$ = $4->scope->slots;
} }
; ;
function_vars:
/* EMPTY */ { $$ = 0; }
| function_vars type CF_SYM_VOID ';' {
cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++);
$$ = $1 + 1;
}
;
filter_body: function_body ; filter_body: function_body ;
filter: filter:
@ -593,13 +598,8 @@ where_filter:
} }
; ;
function_params:
'(' declsn ')' { $$ = $2; }
| '(' ')' { $$ = 0; }
;
function_body: function_body:
decls '{' cmds '}' { function_vars '{' cmds '}' {
$$ = f_linearize($3); $$ = f_linearize($3);
$$->vars = $1; $$->vars = $1;
} }
@ -610,8 +610,8 @@ function_def:
FUNCTION CF_SYM_VOID { DBG( "Beginning of function %s\n", $2->name ); FUNCTION CF_SYM_VOID { DBG( "Beginning of function %s\n", $2->name );
$2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL); $2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL);
cf_push_scope($2); cf_push_scope($2);
} function_params function_body { } function_args function_body {
$5->vars -= $4; DBG("Definition of function %s with %u args and %u local vars.\n", $2->name, $4, $5->vars);
$5->args = $4; $5->args = $4;
$2->function = $5; $2->function = $5;
cf_pop_scope(); cf_pop_scope();