1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: optimize mark_var_used() etc

This commit is contained in:
Alexander Medvednikov 2019-09-23 22:47:09 +03:00
parent 8d52d877fb
commit f27f3515ae
4 changed files with 26 additions and 19 deletions

View File

@ -69,27 +69,24 @@ fn (p mut Parser) open_scope() {
} }
fn (p mut Parser) mark_var_used(v Var) { fn (p mut Parser) mark_var_used(v Var) {
for i, vv in p.local_vars { if v.idx == -1 {
if vv.name == v.name { return
p.local_vars[i].is_used = true }
} p.local_vars[v.idx].is_used = true
}
} }
fn (p mut Parser) mark_var_returned(v Var) { fn (p mut Parser) mark_var_returned(v Var) {
for i, vv in p.local_vars { if v.idx == -1 || v.idx >= p.local_vars.len {
if vv.name == v.name { return
p.local_vars[i].is_returned = true }
} p.local_vars[v.idx].is_returned = true
}
} }
fn (p mut Parser) mark_var_changed(v Var) { fn (p mut Parser) mark_var_changed(v Var) {
for i, vv in p.local_vars { if v.idx == -1 || v.idx >= p.local_vars.len {
if vv.name == v.name { return
p.local_vars[i].is_changed = true }
} p.local_vars[v.idx].is_changed = true
}
} }
fn (p mut Parser) known_var(name string) bool { fn (p mut Parser) known_var(name string) bool {
@ -100,7 +97,7 @@ fn (p mut Parser) known_var(name string) bool {
} }
fn (p mut Parser) register_var(v Var) { fn (p mut Parser) register_var(v Var) {
mut new_var := {v | scope_level: p.cur_fn.scope_level} mut new_var := {v | idx: p.var_idx, scope_level: p.cur_fn.scope_level}
if v.line_nr == 0 { if v.line_nr == 0 {
spos := p.scanner.get_scanner_pos() spos := p.scanner.get_scanner_pos()
new_var.scanner_pos = spos new_var.scanner_pos = spos

View File

@ -241,8 +241,7 @@ fn (v mut V) compile() {
for file in v.files { for file in v.files {
mut p := v.new_parser(file) mut p := v.new_parser(file)
p.parse(.decl) p.parse(.decl)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
} }
// Main pass // Main pass
cgen.pass = Pass.main cgen.pass = Pass.main
@ -308,6 +307,7 @@ fn (v mut V) compile() {
for file in v.files { for file in v.files {
mut p := v.new_parser(file) mut p := v.new_parser(file)
p.parse(.main) p.parse(.main)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
// p.g.gen_x64() // p.g.gen_x64()
// Format all files (don't format automatically generated vlib headers) // Format all files (don't format automatically generated vlib headers)
if !v.pref.nofmt && !file.contains('/vlib/') { if !v.pref.nofmt && !file.contains('/vlib/') {
@ -574,11 +574,13 @@ fn (v mut V) add_v_files_to_compile() {
for file in v.files { for file in v.files {
mut p := v.new_parser(file) mut p := v.new_parser(file)
p.parse(.imports) p.parse(.imports)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
} }
// Parse user imports // Parse user imports
for file in user_files { for file in user_files {
mut p := v.new_parser(file) mut p := v.new_parser(file)
p.parse(.imports) p.parse(.imports)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
} }
// Parse lib imports // Parse lib imports
/* /*
@ -597,6 +599,8 @@ fn (v mut V) add_v_files_to_compile() {
for file in vfiles { for file in vfiles {
mut p := v.new_parser(file, Pass.imports) mut p := v.new_parser(file, Pass.imports)
p.parse() p.parse()
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
} }
} }
} }
@ -615,6 +619,7 @@ fn (v mut V) add_v_files_to_compile() {
for file in vfiles { for file in vfiles {
mut p := v.new_parser(file) mut p := v.new_parser(file)
p.parse(.imports) p.parse(.imports)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
} }
} }
if v.pref.is_verbose { if v.pref.is_verbose {

View File

@ -1777,7 +1777,9 @@ fn (p mut Parser) name_expr() string {
fn (p mut Parser) var_expr(v Var) string { fn (p mut Parser) var_expr(v Var) string {
p.log('\nvar_expr() v.name="$v.name" v.typ="$v.typ"') p.log('\nvar_expr() v.name="$v.name" v.typ="$v.typ"')
// println('var expr is_tmp=$p.cgen.is_tmp\n') // println('var expr is_tmp=$p.cgen.is_tmp\n')
p.mark_var_used(v) if !v.is_const {
p.mark_var_used(v)
}
fn_ph := p.cgen.add_placeholder() fn_ph := p.cgen.add_placeholder()
p.expr_var = v p.expr_var = v
p.gen(p.table.var_cgen_name(v.name)) p.gen(p.table.var_cgen_name(v.name))

View File

@ -62,6 +62,7 @@ struct Var {
mut: mut:
typ string typ string
name string name string
idx int // index in the local_vars array
is_arg bool is_arg bool
is_const bool is_const bool
args []Var // function args args []Var // function args
@ -288,6 +289,7 @@ fn (t mut Table) register_const(name, typ, mod string) {
typ: typ typ: typ
is_const: true is_const: true
mod: mod mod: mod
idx: -1
} }
} }
@ -300,6 +302,7 @@ fn (p mut Parser) register_global(name, typ string) {
is_global: true is_global: true
mod: p.mod mod: p.mod
is_mut: true is_mut: true
idx: -1
} }
} }