cgen: reset g.tmp_count at the start of each fn (minimise v.c commit delta)
parent
4169602a46
commit
6dde9f76c6
|
@ -67,8 +67,9 @@ mut:
|
||||||
file &ast.File
|
file &ast.File
|
||||||
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
|
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
|
||||||
last_fn_c_name string
|
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_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)
|
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
|
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
|
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'
|
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 {
|
pub fn (mut g Gen) current_tmp_var() string {
|
||||||
return '_t$g.tmp_count'
|
return '_t$g.tmp_count'
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
// we could be in an anon fn so save outer fn defer stmts
|
||||||
prev_defer_stmts := g.defer_stmts
|
prev_defer_stmts := g.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)
|
g.stmts(node.stmts)
|
||||||
if node.is_noreturn {
|
if node.is_noreturn {
|
||||||
g.writeln('\twhile(1);')
|
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')
|
g.write('void')
|
||||||
}
|
}
|
||||||
for i, arg in args {
|
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)
|
typ := g.unwrap_generic(arg.typ)
|
||||||
arg_type_sym := g.table.get_type_symbol(typ)
|
arg_type_sym := g.table.get_type_symbol(typ)
|
||||||
mut arg_type_name := g.typ(typ) // util.no_dots(arg_type_sym.name)
|
mut arg_type_name := g.typ(typ) // util.no_dots(arg_type_sym.name)
|
||||||
|
|
Loading…
Reference in New Issue