提交 9c08cd98 编写于 作者: 3 3dgen

区分Scope的“局部变量”和“临时变量”,二者区别为局部变量出现于Scope头部

上级 c859e7f9
......@@ -17,14 +17,16 @@ type Scope struct {
//在该域中定义的结构体
Structs []*StructDecl
//在该域中定义的变量
Vars []*VarDecl
locals []*VarDecl
//在该域中定义的函数
Funcs []*FuncDecl
//域中包含的语句
Stmts []Stmt
//在该域中定义的变量(含局部变量和临时变量,局部变量位于域头部结构体之后,临时变量可出现于域内任意位置)
Vars []*VarDecl
}
//实现Stmt接口
......@@ -51,13 +53,20 @@ func (s *Scope) AddTupleDecl(t ctypes.Tuple) *StructDecl {
return &decl
}
func (s *Scope) AddVarDecl(name string, t ctypes.Type) *VarDeclStmt {
func (s *Scope) AddTempVarDecl(name string, t ctypes.Type) *VarDeclStmt {
decl := NewVarDeclStmt(name, t)
s.Stmts = append(s.Stmts, decl)
s.Vars = append(s.Vars, &decl.VarDecl)
return decl
}
func (s *Scope) AddLocalVarDecl(name string, t ctypes.Type) *VarDecl {
decl := NewVarDecl(name, t)
s.locals = append(s.locals, decl)
s.Vars = append(s.Vars, decl)
return decl
}
func (s *Scope) AddFuncDecl(name string, result ctypes.Type, params []VarDecl) *FuncDecl {
decl := NewFuncDecl(name, result, params)
s.Funcs = append(s.Funcs, decl)
......@@ -156,11 +165,11 @@ func (s *Scope) CIRString() string {
buf.WriteString(";\n")
}
//for _, v := range s.Vars {
// buf.WriteString(genIndent(indent))
// buf.WriteString(v.CIRString())
// buf.WriteString(";\n")
//}
for _, v := range s.locals {
buf.WriteString(genIndent(indent))
buf.WriteString(v.CIRString())
buf.WriteString(";\n")
}
for _, v := range s.Funcs {
b := v.Body
......
......@@ -147,7 +147,7 @@ func (g *functionGenerator) genFunction(f *ssa.Function) {
g.compiler.curScope = fn.Body
defer func() { g.compiler.curScope = fn.Body.Parent }()
g.var_cur_block = &g.compiler.curScope.AddVarDecl("$T_Block_Current", ctypes.Uint32).Var
g.var_cur_block = &g.compiler.curScope.AddTempVarDecl("$T_Block_Current", ctypes.Uint32).Var
for _, b := range f.Blocks {
g.genBlock(b)
......@@ -200,7 +200,7 @@ func (g *functionGenerator) createInstruction(inst ssa.Instruction) {
if v.Type().Equal(ctypes.Void) {
g.compiler.curScope.AddExprStmt(v)
} else {
r := g.compiler.curScope.AddVarDecl(g.genRegister(), v.Type())
r := g.compiler.curScope.AddTempVarDecl(g.genRegister(), v.Type())
r.AssociatedSSAObj = inst
r.InitVal = v
}
......@@ -338,14 +338,14 @@ func (g *functionGenerator) genAlloc(inst *ssa.Alloc) {
if inst.Heap {
ref_type := ctypes.NewRefType(cir.ToCType(inst.Type().(*types.Pointer).Elem()))
v := cir.NewRawExpr(ref_type.CIRString()+"::New()", ref_type)
r := g.compiler.curScope.AddVarDecl(g.genRegister(), v.Type())
r := g.compiler.curScope.AddTempVarDecl(g.genRegister(), v.Type())
r.AssociatedSSAObj = inst
r.InitVal = v
return
}
c_type := cir.ToCType(inst.Type().(*types.Pointer).Elem())
r := g.compiler.curScope.AddVarDecl(g.genRegister(), c_type)
r := g.compiler.curScope.AddTempVarDecl(g.genRegister(), c_type)
switch c_type := c_type.(type) {
case *ctypes.Array:
r.AssociatedSSAObj = inst
......@@ -354,7 +354,7 @@ func (g *functionGenerator) genAlloc(inst *ssa.Alloc) {
r.AssociatedSSAObj = inst
default:
rt := g.compiler.curScope.AddVarDecl(g.genRegister(), ctypes.NewPointerType(c_type))
rt := g.compiler.curScope.AddTempVarDecl(g.genRegister(), ctypes.NewPointerType(c_type))
rt.InitVal = cir.NewGetaddrExpr(&r.Var)
rt.AssociatedSSAObj = inst
}
......@@ -509,7 +509,7 @@ func (g *functionGenerator) genPrint(v cir.Expr) cir.Expr {
}
func (g *functionGenerator) genPhi(inst *ssa.Phi) {
r := &g.compiler.curScope.AddVarDecl(g.genRegister(), cir.ToCType(inst.Type())).Var
r := &g.compiler.curScope.AddTempVarDecl(g.genRegister(), cir.ToCType(inst.Type())).Var
r.AssociatedSSAObj = inst
var edges []cir.PhiEdge
......
......@@ -14,13 +14,13 @@ func (p *CompilerC) compileGlobal(g *ssa.Global) {
switch c_type.(type) {
case *ctypes.Array:
p.curScope.AddVarDecl(g.Name(), c_type).Var.AssociatedSSAObj = g
p.curScope.AddLocalVarDecl(g.Name(), c_type).Var.AssociatedSSAObj = g
default:
c_name := "$Global_" + g.Name()
c_var := &p.curScope.AddVarDecl(c_name, c_type).Var
c_var := &p.curScope.AddLocalVarDecl(c_name, c_type).Var
w_var_decl := p.curScope.AddVarDecl(g.Name(), ctypes.NewPointerType(c_type))
w_var_decl := p.curScope.AddLocalVarDecl(g.Name(), ctypes.NewPointerType(c_type))
w_var_decl.InitVal = cir.NewGetaddrExpr(c_var)
w_var_decl.Var.AssociatedSSAObj = g
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册