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

v2: cleanup old & unused code, add new simple tmp vars in cgen

This commit is contained in:
joe-conigliaro 2020-02-18 13:17:21 +11:00 committed by GitHub
parent 2e1dbd9f5a
commit ecb0af36b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 209 deletions

View File

@ -13,6 +13,7 @@ struct Gen {
table &table.Table
mut:
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
tmp_count int
}
pub fn cgen(files []ast.File, table &table.Table) string {
@ -39,6 +40,15 @@ pub fn (g mut Gen) writeln(s string) {
g.out.writeln(s)
}
pub fn (g mut Gen) new_tmp_var() string {
g.tmp_count++
return 'tmp$g.tmp_count'
}
pub fn (g mut Gen) reset_tmp_count() {
g.tmp_count = 0
}
fn (g mut Gen) stmts(stmts []ast.Stmt) {
for stmt in stmts {
g.stmt(stmt)
@ -60,6 +70,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
}
}
ast.FnDecl {
g.reset_tmp_count()
g.fn_decl = it // &it
is_main := it.name == 'main'
if is_main {
@ -286,11 +297,10 @@ fn (g mut Gen) expr(node ast.Expr) {
ast.IfExpr {
// If expression? Assign the value to a temp var.
// Previously ?: was used, but it's too unreliable.
// ti := g.table.refresh_ti(it.ti)
type_sym := g.table.get_type_symbol(it.typ)
mut tmp := ''
if type_sym.kind != .void {
tmp = g.table.new_tmp_var()
tmp = g.new_tmp_var()
// g.writeln('$ti.name $tmp;')
}
g.write('if (')
@ -317,7 +327,7 @@ fn (g mut Gen) expr(node ast.Expr) {
type_sym := g.table.get_type_symbol(it.typ)
mut tmp := ''
if type_sym.kind != .void {
tmp = g.table.new_tmp_var()
tmp = g.new_tmp_var()
}
g.write('$type_sym.name $tmp = ')
g.expr(it.cond)

View File

@ -112,12 +112,12 @@ void println(string s) {
void matches() {
int a = 100;
int tmp3 = a;
if tmp3 == 10{
int tmp1 = a;
if tmp1 == 10{
println(tos3("10"));
}
if tmp3 == 20{
if tmp1 == 20{
int k = a + 1;
}

View File

@ -149,7 +149,6 @@ pub fn (p mut Parser) close_scope() {
pub fn (p mut Parser) parse_block() []ast.Stmt {
p.open_scope()
p.table.open_scope()
p.check(.lcbr)
mut stmts := []ast.Stmt
if p.tok.kind != .rcbr {
@ -164,7 +163,6 @@ pub fn (p mut Parser) parse_block() []ast.Stmt {
p.check(.rcbr)
println('parse block')
p.close_scope()
p.table.close_scope()
// println('nr exprs in block = $exprs.len')
return stmts
}
@ -789,7 +787,6 @@ fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) ast.Expr {
field_name := p.check_name()
if field_name == 'filter' {
p.open_scope()
p.table.open_scope()
p.filter(left_type)
}
// Method call
@ -823,7 +820,6 @@ fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) ast.Expr {
node = sel_expr
if field_name == 'filter' {
p.close_scope()
p.table.close_scope()
}
return node
}
@ -872,13 +868,11 @@ fn (p mut Parser) enum_val() (ast.Expr,table.Type) {
fn (p mut Parser) for_statement() ast.Stmt {
p.check(.key_for)
p.open_scope()
p.table.open_scope()
// defer { p.table.close_scope() }
// defer { p.close_scope() }
// Infinite loop
if p.tok.kind == .lcbr {
stmts := p.parse_block()
p.close_scope()
p.table.close_scope()
return ast.ForStmt{
stmts: stmts
pos: p.tok.position()
@ -917,7 +911,6 @@ fn (p mut Parser) for_statement() ast.Stmt {
}
stmts := p.parse_block()
p.close_scope()
p.table.close_scope()
return ast.ForCStmt{
stmts: stmts
init: init
@ -981,7 +974,6 @@ fn (p mut Parser) for_statement() ast.Stmt {
stmts := p.parse_block()
// println('nr stmts=$stmts.len')
p.close_scope()
p.table.close_scope()
return ast.ForStmt{
stmts: stmts
pos: p.tok.position()
@ -991,7 +983,6 @@ fn (p mut Parser) for_statement() ast.Stmt {
cond,_ := p.expr(0)
stmts := p.parse_block()
p.close_scope()
p.table.close_scope()
return ast.ForStmt{
cond: cond
stmts: stmts

View File

@ -11,13 +11,9 @@ pub mut:
types []TypeSymbol
// type_idxs Hashmap
type_idxs map[string]int
local_vars []Var
scope_level int
var_idx int
// fns Hashmap
fns map[string]Fn
consts map[string]Var
tmp_cnt int
imports []string // List of all imports
modules []string // List of all modules registered by the application
}
@ -51,33 +47,6 @@ pub fn new_table() &Table {
return t
}
pub fn (t &Table) find_var_idx(name string) int {
for i, var in t.local_vars {
if var.name == name {
return i
}
}
return -1
}
pub fn (t &Table) find_var(name string) ?Var {
for i in 0 .. t.var_idx {
if t.local_vars[i].name == name {
return t.local_vars[i]
}
}
/*
// println(t.names)
for var in t.local_vars {
if var.name == name {
return var
}
}
*/
return none
}
pub fn (t mut Table) register_const(v Var) {
t.consts[v.name] = v
}
@ -91,86 +60,9 @@ pub fn (t mut Table) register_global(name string, typ Type) {
// mod: p.mod
// is_mut: true
// idx: -1
}
}
pub fn (t mut Table) register_var(v Var) {
typ_sym := t.get_type_symbol(v.typ)
println('register_var: $v.name - $typ_sym.name')
new_var := {
v |
idx:t.var_idx,
scope_level:t.scope_level
}
// t.local_vars << v
/*
if v.line_nr == 0 {
new_var.token_idx = p.cur_tok_index()
new_var.line_nr = p.cur_tok().line_nr
}
*/
// Expand the array
if t.var_idx >= t.local_vars.len {
t.local_vars << new_var
}
else {
t.local_vars[t.var_idx] = new_var
}
t.var_idx++
}
pub fn (t mut Table) open_scope() {
t.scope_level++
}
pub fn (t mut Table) close_scope() {
// println('close_scope level=$f.scope_level var_idx=$f.var_idx')
// Move back `var_idx` (pointer to the end of the array) till we reach
// the previous scope level. This effectivly deletes (closes) current
// scope.
mut i := t.var_idx - 1
for ; i >= 0; i-- {
var := t.local_vars[i]
/*
if p.pref.autofree && (v.is_alloc || (v.is_arg && v.typ == 'string')) {
// && !p.pref.is_test {
p.free_var(v)
}
*/
// if p.fileis('mem.v') {
// println(v.name + ' $v.is_arg scope=$v.scope_level cur=$p.cur_fn.scope_level')}
if var.scope_level != t.scope_level {
// && !v.is_arg {
break
}
}
/*
if p.cur_fn.defer_text.last() != '' {
p.genln(p.cur_fn.defer_text.last())
// p.cur_fn.defer_text[f] = ''
}
*/
t.scope_level--
// p.cur_fn.defer_text = p.cur_fn.defer_text[..p.cur_fn.scope_level + 1]
t.var_idx = i + 1
// println('close_scope new var_idx=$f.var_idx\n')
}
pub fn (p mut Table) clear_vars() {
// shared a := [1, 2, 3]
p.var_idx = 0
if p.local_vars.len > 0 {
// ///if p.pref.autofree {
// p.local_vars.free()
// ///}
p.local_vars = []
}
p.tmp_cnt = 0
}
pub fn (t &Table) find_fn(name string) ?Fn {
f := t.fns[name]
if f.name.str != 0 {
@ -204,11 +96,6 @@ pub fn (t &Table) register_method(typ &TypeSymbol, new_fn Fn) bool {
return true
}
pub fn (t mut Table) new_tmp_var() string {
t.tmp_cnt++
return 'tmp$t.tmp_cnt'
}
pub fn (t &TypeSymbol) has_method(name string) bool {
t.find_method(name) or {
return false
@ -500,86 +387,6 @@ pub fn (t &Table) check(got, expected Type) bool {
return true
}
/*
[inline]
pub fn (t &Table) get_expr_typ(expr ast.Expr) TypeSymbol {
match expr {
ast.ArrayInit {
return it.typ
}
ast.IndexExpr {
return t.get_expr_typ(it.left)
}
ast.CallExpr {
func := t.find_fn(it.name) or {
return void_typ
}
return func.return_typ
}
ast.MethodCallExpr {
ti := t.get_expr_typ(it.expr)
func := t.find_method(typ.idx, it.name) or {
return void_type
}
return func.return_typ
}
ast.Ident {
if it.kind == .variable {
info := it.info as ast.IdentVar
if info.typ.kind != .unresolved {
return info.ti
}
return t.get_expr_typ(info.expr)
}
return types.void_typ
}
ast.StructInit {
return it.ti
}
ast.StringLiteral {
return types.string_typ
}
ast.IntegerLiteral {
return types.int_typ
}
ast.SelectorExpr {
ti := t.get_expr_typ(it.expr)
kind := t.types[typ.idx].kind
if typ.kind == .placeholder {
println(' ##### PH $typ.name')
}
if !(kind in [.placeholder, .struct_]) {
return types.void_typ
}
struct_ := t.types[typ.idx]
struct_info := struct_.info as types.Struct
for field in struct_info.fields {
if field.name == it.field {
return field.ti
}
}
if struct_.parent_idx != 0 {
parent := t.types[struct_.parent_idx]
parent_info := parent.info as types.Struct
for field in parent_info.fields {
if field.name == it.field {
return field.ti
}
}
}
return types.void_typ
}
ast.InfixExpr {
return t.get_expr_typ(it.left)
}
else {
return types.void_typ
}
}
}
*/
pub fn (t &Table) known_import(name string) bool {
for i in t.imports {
if i.all_after('.') == name {