提交 8f817716 编写于 作者: 3 3dgen

Ref WIP

上级 e11c8012
// 版权 @2022 凹语言 作者。保留所有权利。
package wir
import "github.com/wa-lang/wa/internal/backends/compiler_wat/wir/wat"
......
// 版权 @2022 凹语言 作者。保留所有权利。
package wir
import (
......@@ -100,3 +102,15 @@ func EmitLoad(addr Value) (insts []wat.Inst, ret_type ValueType) {
}
return
}
func EmitStore(value, addr Value) (insts []wat.Inst) {
switch addr := addr.(type) {
case *VarRef:
insts = addr.EmitStore(value)
default:
logger.Fatalf("Todo %v", addr)
}
return
}
// 版权 @2022 凹语言 作者。保留所有权利。
package wir
import (
......
......@@ -35,12 +35,33 @@ func (v *varBlock) EmitRelease() []wat.Inst {
return insts
}
func (v *varBlock) emitLoad(addr Value) []wat.Inst {
logger.Fatal("Todo")
return nil
//if !addr.Type().(Pointer).Base.Equal(v.Type()) {
// logger.Fatal("Type not match")
// return nil
//}
insts := addr.EmitGet()
insts = append(insts, wat.NewInstLoad(wat.I32{}, 0, 1))
return insts
}
func (v *varBlock) emitStore(addr Value) []wat.Inst {
logger.Fatal("Todo")
return nil
//if !addr.Type().(Pointer).Base.Equal(v.Type()) {
// logger.Fatal("Type not match")
// return nil
//}
insts := v.EmitGet()
insts = append(insts, wat.NewInstCall("$wa.RT.Block.Retain"))
insts = append(insts, wat.NewInstDrop())
NewVar("", v.kind, v.Type()).emitLoad(addr)
insts = append(insts, wat.NewInstCall("$wa.RT.Block.Release"))
insts = append(insts, addr.EmitGet()...)
insts = append(insts, v.EmitGet()...)
insts = append(insts, wat.NewInstStore(toWatType(v.Type()), 0, 1))
return insts
}
/**************************************
......@@ -139,18 +160,19 @@ func (v *VarRef) EmitGet() []wat.Inst { return v.underlying.EmitGet() }
func (v *VarRef) EmitSet() []wat.Inst { return v.underlying.EmitSet() }
func (v *VarRef) EmitRelease() []wat.Inst { return v.underlying.EmitRelease() }
func (v *VarRef) emitLoad(addr Value) []wat.Inst {
logger.Fatal("Todo")
return nil
return v.underlying.emitLoad(addr)
}
func (v *VarRef) emitStore(addr Value) []wat.Inst {
logger.Fatal("Todo")
return nil
return v.underlying.emitStore(addr)
}
func (v *VarRef) EmitLoad() []wat.Inst {
t := NewVar("", v.kind, v.Type().(Ref).Base)
return t.emitLoad(v.underlying.Extract("data"))
}
func (v *VarRef) EmitStore() []wat.Inst {
t := NewVar("", v.kind, v.Type().(Ref).Base)
return t.emitStore(v.underlying.Extract("data"))
func (v *VarRef) EmitStore(d Value) []wat.Inst {
if !d.Type().Equal(v.Type().(Ref).Base) {
logger.Fatal("Type not match")
return nil
}
return d.emitStore(v.underlying.Extract("data"))
}
......@@ -12,48 +12,48 @@ type anInstruction struct {
func (i *anInstruction) isInstruction() {}
/**************************************
InstAdd:
instAdd:
**************************************/
type InstAdd struct {
type instAdd struct {
anInstruction
typ ValueType
}
func NewInstAdd(t ValueType) *InstAdd { return &InstAdd{typ: t} }
func (i *InstAdd) Format(indent string) string { return indent + i.typ.Name() + ".add" }
func NewInstAdd(t ValueType) *instAdd { return &instAdd{typ: t} }
func (i *instAdd) Format(indent string) string { return indent + i.typ.Name() + ".add" }
/**************************************
InstSub:
instSub:
**************************************/
type InstSub struct {
type instSub struct {
anInstruction
typ ValueType
}
func NewInstSub(t ValueType) *InstSub { return &InstSub{typ: t} }
func (i *InstSub) Format(indent string) string { return indent + i.typ.Name() + ".sub" }
func NewInstSub(t ValueType) *instSub { return &instSub{typ: t} }
func (i *instSub) Format(indent string) string { return indent + i.typ.Name() + ".sub" }
/**************************************
InstMul:
instMul:
**************************************/
type InstMul struct {
type instMul struct {
anInstruction
typ ValueType
}
func NewInstMul(t ValueType) *InstMul { return &InstMul{typ: t} }
func (i *InstMul) Format(indent string) string { return indent + i.typ.Name() + ".mul" }
func NewInstMul(t ValueType) *instMul { return &instMul{typ: t} }
func (i *instMul) Format(indent string) string { return indent + i.typ.Name() + ".mul" }
/**************************************
InstDiv:
instDiv:
**************************************/
type InstDiv struct {
type instDiv struct {
anInstruction
typ ValueType
}
func NewInstDiv(t ValueType) *InstDiv { return &InstDiv{typ: t} }
func (i *InstDiv) Format(indent string) string {
func NewInstDiv(t ValueType) *instDiv { return &instDiv{typ: t} }
func (i *instDiv) Format(indent string) string {
switch i.typ.(type) {
case I32:
return indent + "i32.div_s"
......@@ -79,15 +79,15 @@ func (i *InstDiv) Format(indent string) string {
}
/**************************************
InstRem:
instRem:
**************************************/
type InstRem struct {
type instRem struct {
anInstruction
typ ValueType
}
func NewInstRem(t ValueType) *InstRem { return &InstRem{typ: t} }
func (i *InstRem) Format(indent string) string {
func NewInstRem(t ValueType) *instRem { return &instRem{typ: t} }
func (i *instRem) Format(indent string) string {
switch i.typ.(type) {
case I32:
return indent + "i32.rem_s"
......
......@@ -5,37 +5,37 @@ package wat
import "github.com/wa-lang/wa/internal/logger"
/**************************************
InstEq:
instEq:
**************************************/
type InstEq struct {
type instEq struct {
anInstruction
typ ValueType
}
func NewInstEq(t ValueType) *InstEq { return &InstEq{typ: t} }
func (i *InstEq) Format(indent string) string { return indent + i.typ.Name() + ".eq" }
func NewInstEq(t ValueType) *instEq { return &instEq{typ: t} }
func (i *instEq) Format(indent string) string { return indent + i.typ.Name() + ".eq" }
/**************************************
InstNe:
instNe:
**************************************/
type InstNe struct {
type instNe struct {
anInstruction
typ ValueType
}
func NewInstNe(t ValueType) *InstNe { return &InstNe{typ: t} }
func (i *InstNe) Format(indent string) string { return indent + i.typ.Name() + ".ne" }
func NewInstNe(t ValueType) *instNe { return &instNe{typ: t} }
func (i *instNe) Format(indent string) string { return indent + i.typ.Name() + ".ne" }
/**************************************
InstLt:
instLt:
**************************************/
type InstLt struct {
type instLt struct {
anInstruction
typ ValueType
}
func NewInstLt(t ValueType) *InstLt { return &InstLt{typ: t} }
func (i *InstLt) Format(indent string) string {
func NewInstLt(t ValueType) *instLt { return &instLt{typ: t} }
func (i *instLt) Format(indent string) string {
switch i.typ.(type) {
case I32:
return indent + "i32.lt_s"
......@@ -60,15 +60,15 @@ func (i *InstLt) Format(indent string) string {
}
/**************************************
InstGt:
instGt:
**************************************/
type InstGt struct {
type instGt struct {
anInstruction
typ ValueType
}
func NewInstGt(t ValueType) *InstGt { return &InstGt{typ: t} }
func (i *InstGt) Format(indent string) string {
func NewInstGt(t ValueType) *instGt { return &instGt{typ: t} }
func (i *instGt) Format(indent string) string {
switch i.typ.(type) {
case I32:
return indent + "i32.gt_s"
......@@ -93,15 +93,15 @@ func (i *InstGt) Format(indent string) string {
}
/**************************************
InstLe:
instLe:
**************************************/
type InstLe struct {
type instLe struct {
anInstruction
typ ValueType
}
func NewInstLe(t ValueType) *InstLe { return &InstLe{typ: t} }
func (i *InstLe) Format(indent string) string {
func NewInstLe(t ValueType) *instLe { return &instLe{typ: t} }
func (i *instLe) Format(indent string) string {
switch i.typ.(type) {
case I32:
return indent + "i32.le_s"
......@@ -126,15 +126,15 @@ func (i *InstLe) Format(indent string) string {
}
/**************************************
InstGe:
instGe:
**************************************/
type InstGe struct {
type instGe struct {
anInstruction
typ ValueType
}
func NewInstGe(t ValueType) *InstGe { return &InstGe{typ: t} }
func (i *InstGe) Format(indent string) string {
func NewInstGe(t ValueType) *instGe { return &instGe{typ: t} }
func (i *instGe) Format(indent string) string {
switch i.typ.(type) {
case I32:
return indent + "i32.ge_s"
......
......@@ -3,17 +3,17 @@
package wat
/**************************************
InstConst:
instConst:
**************************************/
type InstConst struct {
type instConst struct {
anInstruction
typ ValueType
literal string
}
func NewInstConst(typ ValueType, literal string) *InstConst {
return &InstConst{typ: typ, literal: literal}
func NewInstConst(typ ValueType, literal string) *instConst {
return &instConst{typ: typ, literal: literal}
}
func (i *InstConst) Format(indent string) string {
func (i *instConst) Format(indent string) string {
return indent + i.typ.Name() + ".const " + i.literal
}
......@@ -5,27 +5,27 @@ package wat
import "strconv"
/**************************************
InstCall:
instCall:
**************************************/
type InstCall struct {
type instCall struct {
anInstruction
name string
}
func NewInstCall(name string) *InstCall { return &InstCall{name: name} }
func (i *InstCall) Format(indent string) string { return indent + "call $" + i.name }
func NewInstCall(name string) *instCall { return &instCall{name: name} }
func (i *instCall) Format(indent string) string { return indent + "call $" + i.name }
/**************************************
InstBlock:
instBlock:
**************************************/
type InstBlock struct {
type instBlock struct {
anInstruction
name string
Insts []Inst
}
func NewInstBlock(name string) *InstBlock { return &InstBlock{name: name} }
func (i *InstBlock) Format(indent string) string {
func NewInstBlock(name string) *instBlock { return &instBlock{name: name} }
func (i *instBlock) Format(indent string) string {
s := indent + "(block $"
s += i.name + "\n"
for _, v := range i.Insts {
......@@ -36,16 +36,16 @@ func (i *InstBlock) Format(indent string) string {
}
/**************************************
InstLoop:
instLoop:
**************************************/
type InstLoop struct {
type instLoop struct {
anInstruction
name string
Insts []Inst
}
func NewInstLoop(name string) *InstLoop { return &InstLoop{name: name} }
func (i *InstLoop) Format(indent string) string {
func NewInstLoop(name string) *instLoop { return &instLoop{name: name} }
func (i *instLoop) Format(indent string) string {
s := indent + "(loop $"
s += i.name + "\n"
for _, v := range i.Insts {
......@@ -56,26 +56,26 @@ func (i *InstLoop) Format(indent string) string {
}
/**************************************
InstBr:
instBr:
**************************************/
type InstBr struct {
type instBr struct {
anInstruction
Name string
}
func NewInstBr(name string) *InstBr { return &InstBr{Name: name} }
func (i *InstBr) Format(indent string) string { return indent + "br $" + i.Name }
func NewInstBr(name string) *instBr { return &instBr{Name: name} }
func (i *instBr) Format(indent string) string { return indent + "br $" + i.Name }
/**************************************
InstBrTable:
instBrTable:
**************************************/
type InstBrTable struct {
type instBrTable struct {
anInstruction
Table []int
}
func NewInstBrTable(t []int) *InstBrTable { return &InstBrTable{Table: t} }
func (i *InstBrTable) Format(indent string) string {
func NewInstBrTable(t []int) *instBrTable { return &instBrTable{Table: t} }
func (i *instBrTable) Format(indent string) string {
s := indent + "br_table"
for _, v := range i.Table {
s += " " + strconv.Itoa(v)
......@@ -84,19 +84,19 @@ func (i *InstBrTable) Format(indent string) string {
}
/**************************************
InstIf:
instIf:
**************************************/
type InstIf struct {
type instIf struct {
anInstruction
True []Inst
False []Inst
Ret []ValueType
}
func NewInstIf(instsTrue, instsFalse []Inst, ret []ValueType) *InstIf {
return &InstIf{True: instsTrue, False: instsFalse, Ret: ret}
func NewInstIf(instsTrue, instsFalse []Inst, ret []ValueType) *instIf {
return &instIf{True: instsTrue, False: instsFalse, Ret: ret}
}
func (i *InstIf) Format(indent string) string {
func (i *instIf) Format(indent string) string {
s := indent + "if"
if len(i.Ret) > 0 {
s += " (result"
......@@ -119,11 +119,11 @@ func (i *InstIf) Format(indent string) string {
}
/**************************************
InstReturn:
instReturn:
**************************************/
type InstReturn struct {
type instReturn struct {
anInstruction
}
func NewInstReturn() *InstReturn { return &InstReturn{} }
func (i *InstReturn) Format(indent string) string { return indent + "return" }
func NewInstReturn() *instReturn { return &instReturn{} }
func (i *instReturn) Format(indent string) string { return indent + "return" }
......@@ -5,33 +5,33 @@ package wat
import "strconv"
/**************************************
InstLoad:
instLoad:
**************************************/
type InstLoad struct {
type instLoad struct {
anInstruction
typ ValueType
offset, align int
}
func NewInstLoad(typ ValueType, offset int, align int) *InstLoad {
return &InstLoad{typ: typ, offset: offset, align: align}
func NewInstLoad(typ ValueType, offset int, align int) *instLoad {
return &instLoad{typ: typ, offset: offset, align: align}
}
func (i *InstLoad) Format(indent string) string {
func (i *instLoad) Format(indent string) string {
return indent + i.typ.Name() + ".load offset=" + strconv.Itoa(i.offset) + " align=" + strconv.Itoa(i.align)
}
/**************************************
InstStore:
instStore:
**************************************/
type InstStore struct {
type instStore struct {
anInstruction
typ ValueType
offset, align int
}
func NewInstStore(typ ValueType, offset int, align int) *InstStore {
return &InstStore{typ: typ, offset: offset, align: align}
func NewInstStore(typ ValueType, offset int, align int) *instStore {
return &instStore{typ: typ, offset: offset, align: align}
}
func (i *InstStore) Format(indent string) string {
func (i *instStore) Format(indent string) string {
return indent + i.typ.Name() + ".store offset=" + strconv.Itoa(i.offset) + " align=" + strconv.Itoa(i.align)
}
package wat
/**************************************
instDrop:
**************************************/
type instDrop struct {
anInstruction
}
func NewInstDrop() *instDrop { return &instDrop{} }
func (i *instDrop) Format(indent string) string { return indent + "drop" }
/**************************************
comment:
**************************************/
type comment struct {
anInstruction
name string
}
func NewComment(name string) *comment { return &comment{name: name} }
func (i *comment) Format(indent string) string { return indent + ";;" + i.name }
......@@ -3,45 +3,45 @@
package wat
/**************************************
InstGetLocal:
instGetLocal:
**************************************/
type InstGetLocal struct {
type instGetLocal struct {
anInstruction
name string
}
func NewInstGetLocal(name string) *InstGetLocal { return &InstGetLocal{name: name} }
func (i *InstGetLocal) Format(indent string) string { return indent + "local.get $" + i.name }
func NewInstGetLocal(name string) *instGetLocal { return &instGetLocal{name: name} }
func (i *instGetLocal) Format(indent string) string { return indent + "local.get $" + i.name }
/**************************************
InstSetLocal:
instSetLocal:
**************************************/
type InstSetLocal struct {
type instSetLocal struct {
anInstruction
name string
}
func NewInstSetLocal(name string) *InstSetLocal { return &InstSetLocal{name: name} }
func (i *InstSetLocal) Format(indent string) string { return indent + "local.set $" + i.name }
func NewInstSetLocal(name string) *instSetLocal { return &instSetLocal{name: name} }
func (i *instSetLocal) Format(indent string) string { return indent + "local.set $" + i.name }
/**************************************
InstGetGlobal:
instGetGlobal:
**************************************/
type InstGetGlobal struct {
type instGetGlobal struct {
anInstruction
name string
}
func NewInstGetGlobal(name string) *InstGetGlobal { return &InstGetGlobal{name: name} }
func (i *InstGetGlobal) Format(indent string) string { return indent + "global.get $" + i.name }
func NewInstGetGlobal(name string) *instGetGlobal { return &instGetGlobal{name: name} }
func (i *instGetGlobal) Format(indent string) string { return indent + "global.get $" + i.name }
/**************************************
InstSetGlobal:
instSetGlobal:
**************************************/
type InstSetGlobal struct {
type instSetGlobal struct {
anInstruction
name string
}
func NewInstSetGlobal(name string) *InstSetGlobal { return &InstSetGlobal{name: name} }
func (i *InstSetGlobal) Format(indent string) string { return indent + "global.set $" + i.name }
func NewInstSetGlobal(name string) *instSetGlobal { return &instSetGlobal{name: name} }
func (i *instSetGlobal) Format(indent string) string { return indent + "global.set $" + i.name }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册