cgen: reset g.tmp_count at the start of each fn (minimise v.c commit delta)

pull/10679/head
Delyan Angelov 2021-07-06 14:40:20 +03:00
parent 4169602a46
commit 6dde9f76c6
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 13 additions and 2 deletions

View File

@ -67,8 +67,9 @@ mut:
file &ast.File
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
last_fn_c_name string
tmp_count int // counter for unique tmp vars (_tmp1, tmp2 etc)
tmp_count int // counter for unique tmp vars (_tmp1, _tmp2 etc); resets at the start of each fn.
tmp_count2 int // a separate tmp var counter for autofree fn calls
tmp_count_declarations int // counter for unique tmp names (_d1, _d2 etc); does NOT reset, used for C declarations
is_assign_lhs bool // inside left part of assign expr (for array_set(), etc)
discard_or_result bool // do not safe last ExprStmt of `or` block in tmp variable to defer ongoing expr usage
is_void_expr_stmt bool // ExprStmt whos result is discarded
@ -962,6 +963,11 @@ pub fn (mut g Gen) new_tmp_var() string {
return '_t$g.tmp_count'
}
pub fn (mut g Gen) new_tmp_declaration_name() string {
g.tmp_count_declarations++
return '_d$g.tmp_count_declarations'
}
pub fn (mut g Gen) current_tmp_var() string {
return '_t$g.tmp_count'
}

View File

@ -332,6 +332,11 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
// we could be in an anon fn so save outer fn defer stmts
prev_defer_stmts := g.defer_stmts
g.defer_stmts = []
ctmp := g.tmp_count
g.tmp_count = 0
defer {
g.tmp_count = ctmp
}
g.stmts(node.stmts)
if node.is_noreturn {
g.writeln('\twhile(1);')
@ -409,7 +414,7 @@ fn (mut g Gen) fn_args(args []ast.Param, is_variadic bool, scope &ast.Scope) ([]
g.write('void')
}
for i, arg in args {
mut caname := if arg.name == '_' { g.new_tmp_var() } else { c_name(arg.name) }
mut caname := if arg.name == '_' { g.new_tmp_declaration_name() } else { c_name(arg.name) }
typ := g.unwrap_generic(arg.typ)
arg_type_sym := g.table.get_type_symbol(typ)
mut arg_type_name := g.typ(typ) // util.no_dots(arg_type_sym.name)