提交 ce6cdae5 编写于 作者: 3 3dgen

Ref类型更名为SPtr

上级 cd33d191
......@@ -25,7 +25,7 @@ type Compiler struct {
func New() *Compiler {
p := new(Compiler)
p.module = wir.NewModule()
p.module.AddGlobal("$wa.RT.closure_data", p.module.GenValueType_Ref(p.module.VOID), false, nil)
p.module.AddGlobal("$wa.RT.closure_data", p.module.GenValueType_SPtr(p.module.VOID), false, nil)
wir.SetCurrentModule(p.module)
return p
}
......
......@@ -1113,7 +1113,7 @@ func (g *functionGenerator) genSizer(f *ssa.Function) *wir.Function {
return nil
}
value_type := g.tLib.compile(f.Params[0].Type())
t_size := value_type.(*wir.Ref).Base.Size()
t_size := value_type.(*wir.SPtr).Base.Size()
wir_fn.Insts = append(wir_fn.Insts, wir.NewConst(strconv.Itoa(t_size), g.module.I32).EmitPush()...)
......
......@@ -106,7 +106,7 @@ func (tLib *typeLib) compile(from types.Type) wir.ValueType {
}
case *types.Pointer:
newType = tLib.module.GenValueType_Ref(tLib.compile(t.Elem()))
newType = tLib.module.GenValueType_SPtr(tLib.compile(t.Elem()))
uncommanFlag = true
case *types.Named:
......@@ -152,7 +152,7 @@ func (tLib *typeLib) compile(from types.Type) wir.ValueType {
method.Name = ut.Method(i).Name()
var fnSig wir.FnSig
fnSig.Params = append(fnSig.Params, tLib.module.GenValueType_Ref(tLib.module.VOID))
fnSig.Params = append(fnSig.Params, tLib.module.GenValueType_SPtr(tLib.module.VOID))
fnSig.Params = append(fnSig.Params, method.Sig.Params...)
fnSig.Results = method.Sig.Results
......
......@@ -131,9 +131,9 @@ func (m *Module) EmitLoad(addr Value) (insts []wat.Inst, ret_type ValueType) {
default:
switch addr := addr.(type) {
case *aRef:
case *aSPtr:
insts = append(insts, addr.emitGetValue()...)
ret_type = addr.Type().(*Ref).Base
ret_type = addr.Type().(*SPtr).Base
case *aPtr:
insts = append(insts, addr.emitGetValue()...)
......@@ -165,9 +165,9 @@ func (m *Module) EmitStore(addr, value Value) (insts []wat.Inst) {
default:
switch addr := addr.(type) {
case *aRef:
case *aSPtr:
if value == nil {
zero_value := NewConst("0", addr.Type().(*Ref).Base)
zero_value := NewConst("0", addr.Type().(*SPtr).Base)
insts = append(insts, addr.emitSetValue(zero_value)...)
} else {
insts = append(insts, addr.emitSetValue(value)...)
......@@ -190,7 +190,7 @@ func (m *Module) EmitStore(addr, value Value) (insts []wat.Inst) {
}
func (m *Module) EmitHeapAlloc(typ ValueType) (insts []wat.Inst, ret_type ValueType) {
ref_typ := m.GenValueType_Ref(typ)
ref_typ := m.GenValueType_SPtr(typ)
ret_type = ref_typ
insts = ref_typ.emitHeapAlloc()
return
......@@ -229,9 +229,9 @@ func (m *Module) EmitGenFieldAddr(x Value, field_name string) (insts []wat.Inst,
insts = append(insts, x.EmitPush()...)
var field *StructField
switch addr := x.(type) {
case *aRef:
field = addr.Type().(*Ref).Base.(*Struct).findFieldByName(field_name)
ret_type = m.GenValueType_Ref(field.Type())
case *aSPtr:
field = addr.Type().(*SPtr).Base.(*Struct).findFieldByName(field_name)
ret_type = m.GenValueType_SPtr(field.Type())
case *aPtr:
field = addr.Type().(*Ptr).Base.(*Struct).findFieldByName(field_name)
ret_type = m.GenValueType_Ptr(field.Type())
......@@ -265,15 +265,15 @@ func (m *Module) EmitGenIndexAddr(x, id Value) (insts []wat.Inst, ret_type Value
logger.Fatalf("Todo: %T", typ)
}
case *aRef:
switch typ := x.Type().(*Ref).Base.(type) {
case *aSPtr:
switch typ := x.Type().(*SPtr).Base.(type) {
case *Array:
insts = append(insts, x.EmitPush()...)
insts = append(insts, NewConst(strconv.Itoa(typ.Base.Size()), m.I32).EmitPush()...)
insts = append(insts, id.EmitPush()...)
insts = append(insts, wat.NewInstMul(wat.I32{}))
insts = append(insts, wat.NewInstAdd(wat.I32{}))
ret_type = m.GenValueType_Ref(typ.Base)
ret_type = m.GenValueType_SPtr(typ.Base)
default:
logger.Fatalf("Todo: %T", typ)
......@@ -287,7 +287,7 @@ func (m *Module) EmitGenIndexAddr(x, id Value) (insts []wat.Inst, ret_type Value
insts = append(insts, id.EmitPush()...)
insts = append(insts, wat.NewInstMul(wat.I32{}))
insts = append(insts, wat.NewInstAdd(wat.I32{}))
ret_type = m.GenValueType_Ref(base_type)
ret_type = m.GenValueType_SPtr(base_type)
default:
logger.Fatalf("Todo: %T", x)
......@@ -323,8 +323,8 @@ func (m *Module) EmitGenSlice(x, low, high Value) (insts []wat.Inst, ret_type Va
insts = x.emitSub(low, high)
ret_type = x.Type()
case *aRef:
switch btype := x.Type().(*Ref).Base.(type) {
case *aSPtr:
switch btype := x.Type().(*SPtr).Base.(type) {
case *Slice:
slt := m.GenValueType_Slice(btype.Base)
insts = slt.emitGenFromRefOfSlice(x, low, high)
......@@ -442,14 +442,14 @@ func (m *Module) EmitGenMakeInterface(x Value, itype ValueType) (insts []wat.Ins
x_type := x.Type()
m.markConcreteTypeUsed(x_type)
m.markInterfaceUsed(itype)
x_ref_type := m.GenValueType_Ref(x_type)
x_ref_type := m.GenValueType_SPtr(x_type)
return itype.(*Interface).emitGenMake(x, x_ref_type)
}
func (m *Module) EmitInvoke(i Value, params []Value, mid int, typeName string) (insts []wat.Inst) {
iface := i.(*aInterface)
insts = append(insts, iface.Extract("data").(*aRef).emitGetValue()...)
insts = append(insts, iface.Extract("data").(*aSPtr).emitGetValue()...)
for _, v := range params {
insts = append(insts, v.EmitPush()...)
......
......@@ -220,8 +220,8 @@ func (m *Module) genGlobalAlloc() *Function {
continue
}
ref := g.v.(*aRef)
t := ref.Type().(*Ref).Base
ref := g.v.(*aSPtr)
t := ref.Type().(*SPtr).Base
f.Insts = append(f.Insts, wat.NewInstConst(wat.I32{}, strconv.Itoa(t.Size())))
f.Insts = append(f.Insts, wat.NewInstCall("$waHeapAlloc"))
f.Insts = append(f.Insts, ref.Extract("data").EmitPop()...)
......@@ -464,12 +464,12 @@ func (m *Module) buildTypeInfo(t ValueType) int {
typ.addr = m.DataSeg.Append(_slice.Bin(), 8)
return typ.addr
case *Ref:
_ref := NewConst("0", m.types_map["runtime._refType"]).(*aStruct)
typ.addr = m.DataSeg.Alloc(len(_ref.Bin()), 8)
case *SPtr:
_sptr := NewConst("0", m.types_map["runtime._sptrType"]).(*aStruct)
typ.addr = m.DataSeg.Alloc(len(_sptr.Bin()), 8)
_ref.setFieldConstValue("$_type", _type)
_ref.setFieldConstValue("elemType", NewConst(strconv.Itoa(m.buildTypeInfo(typ.Base)), m.UPTR))
_sptr.setFieldConstValue("$_type", _type)
_sptr.setFieldConstValue("elemType", NewConst(strconv.Itoa(m.buildTypeInfo(typ.Base)), m.UPTR))
if len(typ.methods) > 0 {
_uncommon := NewConst("0", m.types_map["runtime._uncommonType"]).(*aStruct)
_uncommon.setFieldConstValue("methodCount", NewConst(strconv.Itoa(len(typ.methods)), m.U32))
......@@ -481,10 +481,10 @@ func (m *Module) buildTypeInfo(t ValueType) int {
_method.setFieldConstValue("fnID", NewConst(strconv.Itoa(m.AddTableElem(method.FullFnName)), m.U32))
_uncommon_bin = append(_uncommon_bin, _method.Bin()...)
}
_ref.setFieldConstValue("uncommon", NewConst(strconv.Itoa(m.DataSeg.Append(_uncommon_bin, 8)), m.UPTR))
_sptr.setFieldConstValue("uncommon", NewConst(strconv.Itoa(m.DataSeg.Append(_uncommon_bin, 8)), m.UPTR))
}
m.DataSeg.Set(_ref.Bin(), typ.addr)
m.DataSeg.Set(_sptr.Bin(), typ.addr)
return typ.addr
case *Closure:
......
......@@ -273,7 +273,7 @@ func ExtractField(x Value, field_name string) Value {
case *aStruct:
return x.Extract(field_name)
case *aRef:
case *aSPtr:
return x.Extract(field_name)
case *aClosure:
......
......@@ -36,8 +36,8 @@ func newValue(name string, kind ValueKind, typ ValueType) Value {
case *Block:
return newValue_Block(name, kind, typ)
case *Ref:
return newValue_Ref(name, kind, typ)
case *SPtr:
return newValue_SPtr(name, kind, typ)
case *Array:
return newValue_Array(name, kind, typ)
......
......@@ -76,7 +76,7 @@ func (m *Module) GenValueType_Closure(sig FnSig) *Closure {
closure_t.underlying = m.genInternalStruct(closure_t.Name() + ".underlying")
closure_t.underlying.AppendField(m.NewStructField("fn_index", m.U32))
closure_t.underlying.AppendField(m.NewStructField("data", m.GenValueType_Ref(m.VOID)))
closure_t.underlying.AppendField(m.NewStructField("data", m.GenValueType_SPtr(m.VOID)))
closure_t.underlying.Finish()
m.addValueType(&closure_t)
......
......@@ -27,7 +27,7 @@ func (m *Module) GenValueType_Interface(name string) *Interface {
interface_t.name = name
interface_t.underlying = m.genInternalStruct(interface_t.Name() + ".underlying")
interface_t.underlying.AppendField(m.NewStructField("data", m.GenValueType_Ref(m.GenValueType_Ref(m.VOID))))
interface_t.underlying.AppendField(m.NewStructField("data", m.GenValueType_SPtr(m.GenValueType_SPtr(m.VOID))))
interface_t.underlying.AppendField(m.NewStructField("itab", m.U32))
interface_t.underlying.Finish()
......@@ -52,7 +52,7 @@ func (t *Interface) EmitLoadFromAddr(addr Value, offset int) []wat.Inst {
return t.underlying.EmitLoadFromAddr(addr, offset)
}
func (t *Interface) emitGenMake(x Value, x_ref *Ref) (insts []wat.Inst) {
func (t *Interface) emitGenMake(x Value, x_ref *SPtr) (insts []wat.Inst) {
insts = append(insts, x_ref.emitHeapAlloc()...)
insts = append(insts, x.emitStore(0)...)
......
......@@ -63,7 +63,7 @@ func (t *Slice) EmitLoadFromAddr(addr Value, offset int) []wat.Inst {
}
/*这个函数极其不优雅*/
func (t *Slice) emitGenFromRefOfSlice(x *aRef, low, high Value) (insts []wat.Inst) {
func (t *Slice) emitGenFromRefOfSlice(x *aSPtr, low, high Value) (insts []wat.Inst) {
//block
insts = append(insts, x.Extract("data").EmitPush()...)
insts = append(insts, wat.NewInstLoad(wat.U32{}, 0, 1))
......@@ -101,7 +101,7 @@ func (t *Slice) emitGenFromRefOfSlice(x *aRef, low, high Value) (insts []wat.Ins
return
}
func (t *Slice) emitGenFromRefOfArray(x *aRef, low, high Value) (insts []wat.Inst) {
func (t *Slice) emitGenFromRefOfArray(x *aSPtr, low, high Value) (insts []wat.Inst) {
//block
insts = append(insts, x.Extract("block").EmitPush()...)
......@@ -116,7 +116,7 @@ func (t *Slice) emitGenFromRefOfArray(x *aRef, low, high Value) (insts []wat.Ins
insts = append(insts, wat.NewInstMul(wat.U32{}))
insts = append(insts, wat.NewInstAdd(wat.U32{}))
array_len := NewConst(strconv.Itoa(x.Type().(*Ref).Base.(*Array).Capacity), t._u32)
array_len := NewConst(strconv.Itoa(x.Type().(*SPtr).Base.(*Array).Capacity), t._u32)
//len:
if high == nil {
......
......@@ -10,9 +10,9 @@ import (
)
/**************************************
Ref:
SPtr:
**************************************/
type Ref struct {
type SPtr struct {
tCommon
Base ValueType
underlying *Struct
......@@ -20,40 +20,40 @@ type Ref struct {
_void ValueType
}
func (m *Module) GenValueType_Ref(base ValueType) *Ref {
ref_t := Ref{Base: base}
t, ok := m.findValueType(ref_t.Name())
func (m *Module) GenValueType_SPtr(base ValueType) *SPtr {
sptr_t := SPtr{Base: base}
t, ok := m.findValueType(sptr_t.Name())
if ok {
return t.(*Ref)
return t.(*SPtr)
}
ref_t._base_block = m.GenValueType_Block(base)
ref_t._void = m.VOID
sptr_t._base_block = m.GenValueType_Block(base)
sptr_t._void = m.VOID
base_ptr := m.GenValueType_Ptr(base)
ref_t.underlying = m.genInternalStruct(ref_t.Name() + ".underlying")
ref_t.underlying.AppendField(m.NewStructField("block", ref_t._base_block))
ref_t.underlying.AppendField(m.NewStructField("data", base_ptr))
ref_t.underlying.Finish()
sptr_t.underlying = m.genInternalStruct(sptr_t.Name() + ".underlying")
sptr_t.underlying.AppendField(m.NewStructField("block", sptr_t._base_block))
sptr_t.underlying.AppendField(m.NewStructField("data", base_ptr))
sptr_t.underlying.Finish()
m.addValueType(&ref_t)
return &ref_t
m.addValueType(&sptr_t)
return &sptr_t
}
func (t *Ref) Name() string { return t.Base.Name() + ".$ref" }
func (t *Ref) Size() int { return t.underlying.Size() }
func (t *Ref) align() int { return t.underlying.align() }
func (t *Ref) Kind() TypeKind { return kRef }
func (t *Ref) onFree() int { return t.underlying.onFree() }
func (t *Ref) Raw() []wat.ValueType { return t.underlying.Raw() }
func (t *Ref) Equal(u ValueType) bool {
if ut, ok := u.(*Ref); ok {
func (t *SPtr) Name() string { return t.Base.Name() + ".$sptr" }
func (t *SPtr) Size() int { return t.underlying.Size() }
func (t *SPtr) align() int { return t.underlying.align() }
func (t *SPtr) Kind() TypeKind { return kSPtr }
func (t *SPtr) onFree() int { return t.underlying.onFree() }
func (t *SPtr) Raw() []wat.ValueType { return t.underlying.Raw() }
func (t *SPtr) Equal(u ValueType) bool {
if ut, ok := u.(*SPtr); ok {
return t.Base.Equal(ut.Base)
}
return false
}
func (t *Ref) emitHeapAlloc() (insts []wat.Inst) {
func (t *SPtr) emitHeapAlloc() (insts []wat.Inst) {
//insts = append(insts, wat.NewBlank())
//insts = append(insts, wat.NewComment("Ref.emitHeapAlloc start"))
......@@ -68,7 +68,7 @@ func (t *Ref) emitHeapAlloc() (insts []wat.Inst) {
return
}
func (t *Ref) emitStackAlloc() (insts []wat.Inst) {
func (t *SPtr) emitStackAlloc() (insts []wat.Inst) {
//insts = append(insts, wat.NewBlank())
//insts = append(insts, wat.NewComment("Ref.emitStackAlloc start"))
......@@ -83,42 +83,42 @@ func (t *Ref) emitStackAlloc() (insts []wat.Inst) {
return
}
func (t *Ref) EmitLoadFromAddr(addr Value, offset int) []wat.Inst {
func (t *SPtr) EmitLoadFromAddr(addr Value, offset int) []wat.Inst {
return t.underlying.EmitLoadFromAddr(addr, offset)
}
/**************************************
aRef:
aSPtr:
**************************************/
type aRef struct {
type aSPtr struct {
aStruct
typ *Ref
typ *SPtr
}
func newValue_Ref(name string, kind ValueKind, typ *Ref) *aRef {
var v aRef
func newValue_SPtr(name string, kind ValueKind, typ *SPtr) *aSPtr {
var v aSPtr
v.typ = typ
v.aStruct = *newValue_Struct(name, kind, typ.underlying)
return &v
}
func (v *aRef) Type() ValueType { return v.typ }
func (v *aSPtr) Type() ValueType { return v.typ }
func (v *aRef) raw() []wat.Value { return v.aStruct.raw() }
func (v *aRef) EmitInit() []wat.Inst { return v.aStruct.EmitInit() }
func (v *aRef) EmitPush() []wat.Inst { return v.aStruct.EmitPush() }
func (v *aRef) EmitPop() []wat.Inst { return v.aStruct.EmitPop() }
func (v *aRef) EmitRelease() []wat.Inst { return v.aStruct.EmitRelease() }
func (v *aSPtr) raw() []wat.Value { return v.aStruct.raw() }
func (v *aSPtr) EmitInit() []wat.Inst { return v.aStruct.EmitInit() }
func (v *aSPtr) EmitPush() []wat.Inst { return v.aStruct.EmitPush() }
func (v *aSPtr) EmitPop() []wat.Inst { return v.aStruct.EmitPop() }
func (v *aSPtr) EmitRelease() []wat.Inst { return v.aStruct.EmitRelease() }
func (v *aRef) emitStoreToAddr(addr Value, offset int) []wat.Inst {
func (v *aSPtr) emitStoreToAddr(addr Value, offset int) []wat.Inst {
return v.aStruct.emitStoreToAddr(addr, offset)
}
func (v *aRef) emitGetValue() []wat.Inst {
func (v *aSPtr) emitGetValue() []wat.Inst {
return v.typ.Base.EmitLoadFromAddr(v.aStruct.Extract("data"), 0)
}
func (v *aRef) emitSetValue(d Value) []wat.Inst {
func (v *aSPtr) emitSetValue(d Value) []wat.Inst {
if !d.Type().Equal(v.typ.Base) && !v.typ.Base.Equal(v.typ._void) {
logger.Fatal("Type not match")
return nil
......
......@@ -27,12 +27,13 @@ const (
kBlock
kStruct
kTuple
kRef
kSPtr
kString
kSlice
kArray
kMap
kInterface
kRef
)
func toWatType(t ValueType) wat.ValueType {
......
......@@ -11,7 +11,7 @@ type _type struct {
name: string
}
type _refType struct {
type _sptrType struct {
_type
elemType: uintptr //@_type
uncommon: uintptr //@_uncommonType
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册