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) {
for i, vv in p.local_vars {
if vv.name == v.name {
p.local_vars[i].is_used = true
}
}
if v.idx == -1 {
return
}
p.local_vars[v.idx].is_used = true
}
fn (p mut Parser) mark_var_returned(v Var) {
for i, vv in p.local_vars {
if vv.name == v.name {
p.local_vars[i].is_returned = true
}
}
if v.idx == -1 || v.idx >= p.local_vars.len {
return
}
p.local_vars[v.idx].is_returned = true
}
fn (p mut Parser) mark_var_changed(v Var) {
for i, vv in p.local_vars {
if vv.name == v.name {
p.local_vars[i].is_changed = true
}
}
if v.idx == -1 || v.idx >= p.local_vars.len {
return
}
p.local_vars[v.idx].is_changed = true
}
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) {
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 {
spos := p.scanner.get_scanner_pos()
new_var.scanner_pos = spos

View File

@ -241,8 +241,7 @@ fn (v mut V) compile() {
for file in v.files {
mut p := v.new_parser(file)
p.parse(.decl)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
}
// Main pass
cgen.pass = Pass.main
@ -308,6 +307,7 @@ fn (v mut V) compile() {
for file in v.files {
mut p := v.new_parser(file)
p.parse(.main)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
// p.g.gen_x64()
// Format all files (don't format automatically generated vlib headers)
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 {
mut p := v.new_parser(file)
p.parse(.imports)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
}
// Parse user imports
for file in user_files {
mut p := v.new_parser(file)
p.parse(.imports)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
}
// Parse lib imports
/*
@ -597,6 +599,8 @@ fn (v mut V) add_v_files_to_compile() {
for file in vfiles {
mut p := v.new_parser(file, Pass.imports)
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 {
mut p := v.new_parser(file)
p.parse(.imports)
if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
}
}
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 {
p.log('\nvar_expr() v.name="$v.name" v.typ="$v.typ"')
// 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()
p.expr_var = v
p.gen(p.table.var_cgen_name(v.name))

View File

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